import { createHash } from 'node:crypto';
import { getRetrievalPolicy } from './policy.js';
export class LlmIntentExtractor {
    models;
    cache;
    constructor(models, cache) {
        this.models = models;
        this.cache = cache;
    }
    async extract(input) {
        if (!this.models.extractPromptIntent)
            return undefined;
        const ttl = getRetrievalPolicy().promptPreprocessing.intent.cacheTtlSeconds;
        const key = `prompt_intent:${createHash('sha256').update(input.prompt).digest('hex')}`;
        let cached;
        try {
            cached = await this.cache.getJson(key);
        }
        catch (error) {
            console.error('[llm-intent] cache read failed; continuing uncached.', error);
        }
        if (cached)
            return { ...cached, cacheHit: true };
        const verdict = await this.models.extractPromptIntent(input);
        try {
            await this.cache.setJson(key, verdict, ttl);
        }
        catch (error) {
            console.error('[llm-intent] cache write failed; ignoring.', error);
        }
        return { ...verdict, cacheHit: false };
    }
}
