/**
 * Shared types for the VisionClaw data-processing pipeline.
 */
import { parsePiCodingInitsFromCycle, piMatchingSessionId, } from "./pi-log-metadata.js";
// ---------------------------------------------------------------------------
// Wake cycle (segment of log entries between wake and result)
// ---------------------------------------------------------------------------
export class WakeCycle {
    entries = [];
    hasResult = false;
    hasInterrupts = false;
    get wakeEntry() {
        return this.entries.find((e) => e.category === "wake");
    }
    get resultEntry() {
        for (let i = this.entries.length - 1; i >= 0; i--) {
            if (this.entries[i].category === "result")
                return this.entries[i];
        }
        return undefined;
    }
    get trigger() {
        const w = this.wakeEntry;
        if (w?.data)
            return w.data.trigger ?? "unknown";
        return "unknown";
    }
    get incomingMessages() {
        return this.entries.filter((e) => e.category === "incoming");
    }
    get originalIncoming() {
        return this.entries.filter((e) => e.category === "incoming" && !e.message.includes("[interrupt]"));
    }
    get interruptIncoming() {
        return this.entries.filter((e) => e.category === "incoming" && e.message.includes("[interrupt]"));
    }
    get systemInitEntry() {
        return this.entries.find((e) => e.category === "system" && e.message.startsWith("System message: {"));
    }
    /**
     * Find the first `system_prompt` log entry in this cycle, which contains
     * the dynamically built system prompt captured at runtime.
     */
    get systemPromptEntry() {
        return this.entries.find((e) => e.category === "system_prompt");
    }
    /**
     * All system-init entries in this cycle, each representing one SDK query()
     * call.  In a dual-session setup, multiple inits with different session_ids
     * indicate session switches.
     */
    get systemInitEntries() {
        return this.entries.filter((e) => e.category === "system" && e.message.startsWith("System message: {"));
    }
    /**
     * Parse active session mode from the "send" entry's text.
     * Returns "general", "coding", or undefined if not a dual-session cycle.
     */
    get activeSessionMode() {
        const send = this.entries.find((e) => e.category === "send");
        if (!send)
            return undefined;
        const match = /\[Active session: (\w+)]/.exec(send.message);
        return match?.[1];
    }
    /**
     * Extract ordered session turns from system-init entries.
     * Each turn has a sessionId and the timestamp when the SDK query started.
     */
    getSessionTurns() {
        const turns = [];
        for (const e of this.systemInitEntries) {
            try {
                const json = e.message.slice("System message: ".length);
                const parsed = JSON.parse(json);
                if (parsed.subtype === "init" && parsed.session_id) {
                    turns.push({ sessionId: parsed.session_id, timestamp: e.timestamp });
                }
            }
            catch {
                // skip malformed
            }
        }
        if (turns.length > 0)
            return turns;
        const piInits = parsePiCodingInitsFromCycle(this);
        for (const init of piInits) {
            turns.push({ sessionId: piMatchingSessionId(init), timestamp: init.timestamp });
        }
        return turns;
    }
    /** Pi/GPT agent init metadata from [pi-coding-*] log lines. */
    getPiCodingInits() {
        return parsePiCodingInitsFromCycle(this);
    }
}
//# sourceMappingURL=types.js.map