/**
 * /update state compiler
 *
 * Produces a compact, machine-friendly snapshot of the current working state so
 * future sessions can start from a small persisted summary instead of dragging
 * long conversational history forward.
 *
 * v1 is intentionally simple:
 *   - session stats (counts/tokens/cost)
 *   - current model
 *   - cwd + session file
 *   - browser capability flags inferred from live interactive-mode state
 *   - credential handle count
 *   - repo-local rules supplied by caller
 *   - optional user-provided focus text
 *
 * Written to: ~/.avis/self/memory/state/current-work.json
 */
import { existsSync, mkdirSync } from "node:fs";
import { writeFile } from "node:fs/promises";
import { dirname } from "node:path";
import { getCurrentWorkPath } from "../config.js";
export function getUpdateStatePath() {
    return getCurrentWorkPath();
}
export function compileUpdateState(input) {
    return {
        timestamp: new Date().toISOString(),
        workspace: input.cwd,
        session: {
            id: input.sessionId,
            name: input.sessionName,
            file: input.sessionFile,
        },
        model: input.model
            ? {
                provider: input.model.provider,
                id: input.model.id,
                name: input.model.name,
                reasoning: !!input.model.reasoning,
            }
            : undefined,
        stats: input.stats,
        capabilities: {
            browser: {
                enabled: !!input.browser?.enabled,
                sessionOpen: !!input.browser?.sessionOpen,
                safetyMode: input.browser?.safetyMode,
                credentialHandles: input.browser?.credentialHandles ?? 0,
            },
        },
        rules: input.rules ?? [],
        focus: input.focus,
        notes: input.notes ?? [],
    };
}
export async function writeUpdateState(state) {
    const outPath = getUpdateStatePath();
    const dir = dirname(outPath);
    if (!existsSync(dir))
        mkdirSync(dir, { recursive: true });
    await writeFile(outPath, `${JSON.stringify(state, null, 2)}\n`, "utf-8");
    return outPath;
}
//# sourceMappingURL=update-state.js.map