/**
 * Parse Pi/GPT agent metadata from VisionClaw log lines ([pi-coding-*]).
 */
const PI_RESOURCES_RE = /\[pi-coding-resources\]\s+.*\bsession=([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\b/i;
const PI_MCPS_RE = /\[pi-coding-mcps\]\s+servers\(\d+\):\s*(.+)$/i;
const PI_SKILLS_RE = /\[pi-coding-skills\]\s+loaded\(\d+\):\s*(.+)$/i;
const TRANSCRIPT_FILE_RE = /([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.jsonl/i;
/** Session id used to index and match transcript turns (file UUID when known). */
export function piMatchingSessionId(init) {
    return init.transcriptSessionId ?? init.sessionId;
}
function splitCsvList(raw) {
    return raw
        .split(",")
        .map((s) => s.trim())
        .filter((s) => s.length > 0);
}
function parsePiResourcesEntry(entry) {
    const match = PI_RESOURCES_RE.exec(entry.message);
    if (!match)
        return null;
    return { sessionId: match[1], timestamp: entry.timestamp };
}
function parseTranscriptSessionIdFromEntry(entry) {
    const fromData = entry.data.sessionFile;
    if (typeof fromData === "string") {
        const match = TRANSCRIPT_FILE_RE.exec(fromData);
        if (match)
            return match[1];
    }
    const match = TRANSCRIPT_FILE_RE.exec(entry.message);
    return match?.[1];
}
function parsePiMcps(entry) {
    const match = PI_MCPS_RE.exec(entry.message);
    if (!match)
        return [];
    return splitCsvList(match[1]);
}
function parsePiSkills(entry) {
    const match = PI_SKILLS_RE.exec(entry.message);
    if (!match)
        return [];
    return splitCsvList(match[1]);
}
function stringListFromData(val) {
    if (!Array.isArray(val))
        return [];
    return val.filter((x) => typeof x === "string" && x.length > 0);
}
/** Active tools from `Pi Coding Agent resources loaded` structured log data. */
function parseActiveToolNamesFromEntry(entry) {
    const active = stringListFromData(entry.data.activeToolNames);
    if (active.length > 0)
        return active;
    return stringListFromData(entry.data.toolNames);
}
function isPiResourcesLoadedMessage(message) {
    return message.includes("Pi Coding Agent resources loaded");
}
/**
 * Collect Pi coding-agent init metadata from a wake cycle's log entries.
 * Merges mcps/skills lines that immediately follow a resources line.
 */
export function parsePiCodingInitsFromCycle(cycle) {
    const inits = [];
    const entries = cycle.entries;
    for (let i = 0; i < entries.length; i++) {
        const res = parsePiResourcesEntry(entries[i]);
        if (!res)
            continue;
        let tools = [];
        let mcpServers = [];
        let skills = [];
        let transcriptSessionId;
        for (let j = Math.max(0, i - 5); j < Math.min(i + 6, entries.length); j++) {
            const entry = entries[j];
            const msg = entry.message;
            const fileId = parseTranscriptSessionIdFromEntry(entry);
            if (fileId)
                transcriptSessionId = fileId;
            if (isPiResourcesLoadedMessage(msg)) {
                const active = parseActiveToolNamesFromEntry(entry);
                if (active.length > 0)
                    tools = active;
            }
            else if (PI_MCPS_RE.test(msg)) {
                mcpServers = parsePiMcps(entry);
            }
            else if (PI_SKILLS_RE.test(msg)) {
                skills = parsePiSkills(entry);
            }
            else if (j > i && PI_RESOURCES_RE.test(msg)) {
                break;
            }
        }
        if (tools.length === 0 && mcpServers.length > 0) {
            tools = [...mcpServers];
        }
        inits.push({
            sessionId: res.sessionId,
            transcriptSessionId,
            timestamp: res.timestamp,
            tools,
            mcpServers,
            skills,
        });
    }
    return inits;
}
/** First Pi init in the cycle (typical single-session turn). */
export function parsePiCodingInitFromCycle(cycle) {
    const inits = parsePiCodingInitsFromCycle(cycle);
    return inits.length > 0 ? inits[0] : null;
}
/**
 * Synthetic SDK-style system init for Viewer / downstream consumers.
 * `tools` lists all active tool names for the wake; `mcp_servers` lists MCP servers only.
 */
export function buildSyntheticPiSystemMessage(init) {
    const matchingId = piMatchingSessionId(init);
    const msg = {
        subtype: "init",
        session_id: matchingId,
        agent: "pi-coding",
        tools: [...init.tools],
        skills: [...init.skills],
        mcp_servers: [...init.mcpServers],
    };
    if (init.transcriptSessionId && init.transcriptSessionId !== init.sessionId) {
        msg.pi_runtime_session_id = init.sessionId;
    }
    return msg;
}
//# sourceMappingURL=pi-log-metadata.js.map