import { Layer, createMemory } from './memory.js';
import { writeEntry } from './store.js';
const EXTRACTION_PROMPT = `You are extracting factual statements from a conversation or memory entry. Extract 1-8 standalone factual statements that would be useful to remember later.

Rules:
- Each fact must be a complete, standalone sentence
- Include the speaker's name in every fact (e.g. "Alice prefers..." not "She prefers...")
- Preserve specific details: names, numbers, dates, paths, IDs
- Return a JSON array of objects with: content (string), tags (array of "speaker:X" and "topic:Y" strings), valence ("neutral", "positive", "negative", or "critical")
- Return an empty array [] for small talk with no extractable facts
- Output ONLY the JSON array, no preamble or explanation

Input text:
`;
export async function extractFacts(text, opts) {
    const model = opts.model ?? 'claude-sonnet-4-6';
    const fetchFn = opts.fetcher ?? fetch;
    let res;
    try {
        res = await fetchFn('https://api.anthropic.com/v1/messages', {
            method: 'POST',
            headers: {
                'content-type': 'application/json',
                'x-api-key': opts.apiKey,
                'anthropic-version': '2023-06-01',
            },
            body: JSON.stringify({
                model,
                max_tokens: 1200,
                messages: [{ role: 'user', content: EXTRACTION_PROMPT + text }],
            }),
        });
    }
    catch {
        return [];
    }
    if (!res.ok)
        return [];
    try {
        const data = await res.json();
        const raw = data.content?.[0]?.text?.trim() ?? '';
        if (!raw)
            return [];
        const parsed = JSON.parse(raw);
        if (!Array.isArray(parsed))
            return [];
        const validValences = new Set(['neutral', 'positive', 'negative', 'critical']);
        const facts = [];
        for (const item of parsed) {
            if (facts.length >= 8)
                break;
            if (!item || typeof item.content !== 'string' || item.content.length < 3)
                continue;
            const tags = Array.isArray(item.tags)
                ? item.tags.filter((t) => typeof t === 'string')
                : [];
            const valence = validValences.has(item.valence)
                ? item.valence
                : 'neutral';
            facts.push({ content: item.content, tags, valence });
        }
        return facts;
    }
    catch {
        return [];
    }
}
const INHERITABLE_PREFIXES = ['conv:', 'session:', 'scope:', 'path:'];
export function storeExtractedFacts(hippoRoot, source, facts) {
    const inheritedTags = source.tags.filter((t) => INHERITABLE_PREFIXES.some((p) => t.startsWith(p)));
    const entries = [];
    for (const fact of facts) {
        const tags = ['extracted', ...inheritedTags, ...fact.tags];
        const entry = createMemory(fact.content, {
            layer: Layer.Semantic,
            tags,
            emotional_valence: fact.valence,
            confidence: 'inferred',
            source: source.source,
            extracted_from: source.id,
        });
        writeEntry(hippoRoot, entry);
        entries.push(entry);
    }
    return entries;
}
//# sourceMappingURL=extract.js.map