/**
 * Two-phase semantic prefetch — kicks off a background search on turn_end,
 * consumes the result on before_agent_start with a timeout.
 */
export class Prefetch {
    provider;
    userId;
    topK;
    pending = null;
    constructor(provider, userId, opts) {
        this.provider = provider;
        this.userId = userId;
        this.topK = opts.topK;
    }
    /** Phase 1: kick off a background search (called on turn_end with user message). */
    queue(query) {
        if (!query.trim())
            return;
        this.pending = this.provider.search(query, { userId: this.userId, topK: this.topK });
    }
    /** Phase 2: consume the result with a timeout (called on before_agent_start). */
    async consume(timeoutMs = 3000) {
        if (!this.pending)
            return '';
        const promise = this.pending;
        this.pending = null;
        try {
            const memories = await Promise.race([
                promise,
                new Promise((resolve) => setTimeout(() => resolve([]), timeoutMs)),
            ]);
            if (memories.length === 0)
                return '';
            const lines = memories.filter((m) => m.memory?.trim()).map((m) => `- ${m.memory.trim()}`);
            return lines.length > 0 ? `## Recalled Memories (Mem0)\n${lines.join('\n')}` : '';
        }
        catch {
            return '';
        }
    }
}
//# sourceMappingURL=prefetch.js.map