import { toNumber } from "./shared.js";
export const parseCodexUsageFromJsonl = (output) => {
    const lines = output
        .split(/\r?\n/)
        .map((line) => line.trim())
        .filter((line) => line.length > 0);
    let usage = null;
    let costUsd = null;
    for (const line of lines) {
        if (!line.startsWith("{"))
            continue;
        try {
            const parsed = JSON.parse(line);
            const candidates = [
                parsed.usage,
                parsed.response?.usage,
                parsed.metrics?.usage,
            ].filter(Boolean);
            for (const candidate of candidates) {
                const input = toNumber(candidate.input_tokens) ??
                    toNumber(candidate.prompt_tokens) ??
                    toNumber(candidate.inputTokens) ??
                    null;
                const outputTokens = toNumber(candidate.output_tokens) ??
                    toNumber(candidate.completion_tokens) ??
                    toNumber(candidate.outputTokens) ??
                    null;
                const totalTokens = toNumber(candidate.total_tokens) ??
                    toNumber(candidate.totalTokens) ??
                    (typeof input === "number" && typeof outputTokens === "number"
                        ? input + outputTokens
                        : null);
                if (input !== null || outputTokens !== null || totalTokens !== null) {
                    usage = { promptTokens: input, completionTokens: outputTokens, totalTokens };
                }
            }
            if (costUsd === null) {
                const costValue = toNumber(parsed.cost_usd) ??
                    toNumber(parsed.usage?.cost_usd) ??
                    null;
                if (typeof costValue === "number")
                    costUsd = costValue;
            }
        }
        catch {
            // Ignore malformed JSON lines.
        }
    }
    return { usage, costUsd };
};
function extractCodexTextFromContentBlocks(blocks) {
    if (!Array.isArray(blocks))
        return null;
    const text = blocks
        .flatMap((block) => {
        if (!block || typeof block !== "object")
            return [];
        const record = block;
        if (typeof record.text === "string" && record.text.trim().length > 0)
            return [record.text];
        if (!Array.isArray(record.content))
            return [];
        return record.content
            .map((part) => {
            if (!part || typeof part !== "object")
                return "";
            const partRecord = part;
            return typeof partRecord.text === "string" ? partRecord.text : "";
        })
            .filter((part) => part.trim().length > 0);
    })
        .join("");
    return text.trim().length > 0 ? text.trim() : null;
}
function extractCodexTextEvent(parsed) {
    const type = typeof parsed.type === "string" ? parsed.type : "";
    if (type === "response.output_text.delta" && typeof parsed.delta === "string") {
        return { deltaText: parsed.delta, fullText: null };
    }
    if (type === "response.output_text.done") {
        const text = typeof parsed.text === "string"
            ? parsed.text
            : typeof parsed.delta === "string"
                ? parsed.delta
                : null;
        return { deltaText: null, fullText: text };
    }
    if (typeof parsed.output_text === "string" && parsed.output_text.trim().length > 0) {
        return { deltaText: null, fullText: parsed.output_text.trim() };
    }
    const response = parsed.response;
    if (response && typeof response === "object") {
        const record = response;
        if (typeof record.output_text === "string" && record.output_text.trim().length > 0) {
            return { deltaText: null, fullText: record.output_text.trim() };
        }
        const output = extractCodexTextFromContentBlocks(record.output);
        if (output)
            return { deltaText: null, fullText: output };
    }
    for (const key of ["message", "item"]) {
        const value = parsed[key];
        if (!value || typeof value !== "object")
            continue;
        const output = extractCodexTextFromContentBlocks([value]);
        if (output)
            return { deltaText: null, fullText: output };
    }
    return { deltaText: null, fullText: null };
}
export function parseCodexOutputFromJsonl(output) {
    const trimmed = output.trim();
    if (!trimmed)
        return { text: null, sawStructuredEvent: false };
    const lines = trimmed
        .split(/\r?\n/)
        .map((line) => line.trim())
        .filter((line) => line.length > 0);
    const deltaParts = [];
    let fullText = null;
    let sawStructuredEvent = false;
    for (const line of lines) {
        if (!line.startsWith("{"))
            continue;
        try {
            const parsed = JSON.parse(line);
            sawStructuredEvent = true;
            const event = extractCodexTextEvent(parsed);
            if (typeof event.deltaText === "string" && event.deltaText.length > 0) {
                deltaParts.push(event.deltaText);
            }
            else if (!fullText && typeof event.fullText === "string" && event.fullText.length > 0) {
                fullText = event.fullText;
            }
        }
        catch {
            // Ignore malformed JSON lines.
        }
    }
    const deltaText = deltaParts.join("").trim();
    return { text: deltaText || fullText, sawStructuredEvent };
}
//# sourceMappingURL=codex.js.map