/**
 * SessionJournal — compact action memory between bridge calls.
 *
 * After each completed Claude response, extracts "action statements" (created,
 * fixed, updated, etc.) and stores them per session key. Before the next call,
 * the journal can be injected as a context prefix so the model doesn't lose track
 * of what it has already done — especially in long multi-step workflows.
 */
const MAX_ENTRIES = 20;
const SESSION_TTL_MS = 60 * 60 * 1_000; // 1 hour inactivity
const ACTION_PATTERNS = [
    // "I created/wrote/added/…"
    /^I (?:created?|wrote|added|updated?|modified|deleted?|fixed|implemented|built|configured|installed|refactored?|moved?|renamed?) .{10,120}/gim,
    // "Successfully created/…"
    /^Successfully (?:created?|wrote|added|updated?|modified|fixed|implemented|built|configured|installed) .{10,120}/gim,
    // Leading past-tense verbs
    /^(?:Created?|Wrote|Added|Updated?|Modified|Fixed|Implemented|Built|Installed) .{10,120}/gim,
];
class SessionJournalStore {
    sessions = new Map();
    /** Extract action lines from a response and append to this session's journal. */
    record(key, responseText) {
        const found = new Set();
        for (const pattern of ACTION_PATTERNS) {
            // reset lastIndex for global patterns
            pattern.lastIndex = 0;
            let match;
            while ((match = pattern.exec(responseText)) !== null) {
                const line = match[0].trim().replace(/\s+/g, ' ').slice(0, 120);
                found.add(line);
            }
        }
        if (found.size === 0)
            return;
        const existing = this.sessions.get(key) ?? { entries: [], updatedAt: Date.now() };
        const newEntries = [...existing.entries, ...[...found].map(text => ({
                timestamp: Date.now(),
                text,
            }))];
        // Keep only the last MAX_ENTRIES
        const trimmed = newEntries.slice(-MAX_ENTRIES);
        this.sessions.set(key, { entries: trimmed, updatedAt: Date.now() });
    }
    /**
     * Build a compact context string to prepend to the next prompt.
     * Returns empty string if no journal exists or session has expired.
     */
    buildInjection(key) {
        const session = this.sessions.get(key);
        if (!session || session.entries.length === 0)
            return '';
        if (Date.now() - session.updatedAt > SESSION_TTL_MS) {
            this.sessions.delete(key);
            return '';
        }
        const lines = session.entries.map(e => `- ${e.text}`).join('\n');
        return `[Context — actions already completed in this session:\n${lines}]\n\n`;
    }
    /** Clear journal for a session (e.g., after a run completes). */
    clear(key) {
        this.sessions.delete(key);
    }
    /** Number of sessions currently tracked. */
    get size() {
        return this.sessions.size;
    }
}
export const sessionJournal = new SessionJournalStore();
//# sourceMappingURL=session-journal.js.map