import { toNumber } from "./cli-provider-output/shared.js";
export { parseCodexOutputFromJsonl, parseCodexUsageFromJsonl, } from "./cli-provider-output/codex.js";
export { parseOpenCodeOutputFromJsonl } from "./cli-provider-output/opencode.js";
export { parsePiOutputFromJsonl } from "./cli-provider-output/pi.js";
const JSON_RESULT_FIELDS = ["result", "response", "output", "message", "text"];
export function isJsonCliProvider(provider) {
    return !["codex", "openclaw", "opencode", "copilot", "agy", "pi"].includes(provider);
}
function parseJsonFromOutput(output) {
    const trimmed = output.trim();
    if (!trimmed)
        return null;
    if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
        try {
            return JSON.parse(trimmed);
        }
        catch {
            // Try the final JSON object after CLI diagnostics.
        }
    }
    const lastBraceIndex = trimmed.lastIndexOf("\n{");
    if (lastBraceIndex < 0)
        return null;
    try {
        return JSON.parse(trimmed.slice(lastBraceIndex + 1).trim());
    }
    catch {
        return null;
    }
}
function parseClaudeUsage(payload) {
    const usage = payload.usage;
    if (!usage || typeof usage !== "object")
        return null;
    const record = usage;
    const inputTokens = toNumber(record.input_tokens);
    const cacheCreationTokens = toNumber(record.cache_creation_input_tokens) ?? 0;
    const cacheReadTokens = toNumber(record.cache_read_input_tokens) ?? 0;
    const completionTokens = toNumber(record.output_tokens);
    if (inputTokens === null && completionTokens === null)
        return null;
    const promptTokens = inputTokens !== null ? inputTokens + cacheCreationTokens + cacheReadTokens : null;
    const totalTokens = typeof promptTokens === "number" && typeof completionTokens === "number"
        ? promptTokens + completionTokens
        : null;
    return { promptTokens, completionTokens, totalTokens };
}
function parseGeminiUsage(payload) {
    const stats = payload.stats;
    if (!stats || typeof stats !== "object")
        return null;
    const models = stats.models;
    if (!models || typeof models !== "object")
        return null;
    let promptSum = 0;
    let completionSum = 0;
    let totalSum = 0;
    let hasPrompt = false;
    let hasCompletion = false;
    let hasTotal = false;
    for (const entry of Object.values(models)) {
        if (!entry || typeof entry !== "object")
            continue;
        const tokens = entry.tokens;
        if (!tokens || typeof tokens !== "object")
            continue;
        const record = tokens;
        const prompt = toNumber(record.prompt);
        const candidates = toNumber(record.candidates);
        const total = toNumber(record.total);
        if (prompt !== null) {
            promptSum += prompt;
            hasPrompt = true;
        }
        if (candidates !== null) {
            completionSum += candidates;
            hasCompletion = true;
        }
        if (total !== null) {
            totalSum += total;
            hasTotal = true;
        }
    }
    if (!hasPrompt && !hasCompletion && !hasTotal)
        return null;
    const promptTokens = hasPrompt ? promptSum : null;
    const completionTokens = hasCompletion ? completionSum : null;
    const totalTokens = hasTotal && totalSum > 0
        ? totalSum
        : typeof promptTokens === "number" && typeof completionTokens === "number"
            ? promptTokens + completionTokens
            : null;
    return { promptTokens, completionTokens, totalTokens };
}
function extractJsonResultText(payload) {
    for (const key of JSON_RESULT_FIELDS) {
        const value = payload[key];
        if (typeof value === "string" && value.trim().length > 0)
            return value.trim();
    }
    return null;
}
export function parseJsonProviderOutput(args) {
    const trimmed = args.stdout.trim();
    if (!trimmed)
        throw new Error("CLI returned empty output");
    const parsed = parseJsonFromOutput(trimmed);
    if (parsed && typeof parsed === "object") {
        const payload = Array.isArray(parsed)
            ? (parsed.find((item) => item && typeof item === "object" && item.type === "result") ?? null)
            : parsed;
        if (payload) {
            const text = extractJsonResultText(payload);
            if (text) {
                return {
                    text,
                    usage: args.provider === "claude"
                        ? parseClaudeUsage(payload)
                        : args.provider === "gemini"
                            ? parseGeminiUsage(payload)
                            : null,
                    costUsd: args.provider === "claude" ? toNumber(payload.total_cost_usd) : null,
                };
            }
        }
    }
    return { text: trimmed, usage: null, costUsd: null };
}
//# sourceMappingURL=cli-provider-output.js.map