/**
 * Wraps a local CompactionLLMFn to additionally archive compaction summaries
 * to Brain (SemanticMemoryPort) after each compact() call.
 *
 * Brain archival is always fail-soft: if Brain is unavailable, the summary
 * is still returned to the caller — the compact never fails due to Brain.
 *
 * @param semanticMemory - Brain SemanticMemoryPort implementation
 * @param localLlmFn - Local LLM function (always called first)
 * @param sessionTag - Tag used to group summaries per session (e.g. runId)
 */
export function createBrainCompactionLlmFn(semanticMemory, localLlmFn, sessionTag) {
    return async (prompt) => {
        const summary = await localLlmFn(prompt);
        // Fail-soft: never throw if Brain is unavailable
        try {
            await semanticMemory.archive({
                content: summary,
                category: "session-compaction",
                tags: ["session-compaction", sessionTag],
                confidence: 0.9,
            });
        }
        catch {
            // Brain unavailable — summary still returned
        }
        return summary;
    };
}
/**
 * Restore prior session context from Brain.
 * Queries Brain for recent compaction summaries tagged with sessionTag or
 * the generic "session-compaction" category. Returns a formatted string
 * suitable for injection as a system prompt prefix, or null if nothing found.
 *
 * Always fail-soft: returns null on any error.
 *
 * @param semanticMemory - Brain SemanticMemoryPort implementation
 * @param sessionTag - Tag to filter summaries (e.g. agent runId or project slug)
 * @param limit - Max number of prior summaries to include (default: 5)
 */
export async function restoreSessionContext(semanticMemory, sessionTag, limit = 5) {
    try {
        const entries = await semanticMemory.query("session-compaction", {
            tags: ["session-compaction", sessionTag],
            limit,
        });
        if (entries.length === 0)
            return null;
        const body = entries.map((e) => e.content).join("\n\n---\n\n");
        return `## Long-term context (from prior sessions)\n\n${body}`;
    }
    catch {
        return null;
    }
}
//# sourceMappingURL=brain-connector.js.map