import { DEFAULT_TIMEOUT_MS, errorMessage, interruptedExecResult, parseJson, spawnWithSigintPropagation, timeoutExecResult, } from './shared.js';
export async function geminiExecutor({ model, system, prompt, timeoutMs = DEFAULT_TIMEOUT_MS }) {
    const fullPrompt = system ? `${system}\n\n${prompt}` : prompt;
    const start = Date.now();
    try {
        const args = [];
        if (model)
            args.push('--model', model);
        const { child, done } = spawnWithSigintPropagation('gemini', args, {
            env: { ...process.env },
            timeoutMs,
        });
        child.stdin?.write(fullPrompt);
        child.stdin?.end();
        let output;
        try {
            const r = await done;
            output = r.stdout;
        }
        catch (err) {
            const details = err;
            if (details.killedByTimeout)
                return timeoutExecResult(timeoutMs, Date.now() - start);
            if (details.killedBySignal)
                return interruptedExecResult(Date.now() - start);
            // gemini 退出码非 0 但有 stdout 时仍按成功路径解析(原行为);只在 stdout 为空时才视为失败
            if (details.stdout && details.stdout.length > 0) {
                output = details.stdout;
            }
            else {
                throw err;
            }
        }
        const durationMs = Date.now() - start;
        let text = output;
        let inputTokens = 0;
        let outputTokens = 0;
        try {
            const data = parseJson(output);
            if (data.response)
                text = data.response;
            if (data.stats) {
                inputTokens = data.stats.inputTokens || 0;
                outputTokens = data.stats.outputTokens || 0;
            }
        }
        catch {
            text = output.trim();
        }
        return {
            ok: true,
            durationMs,
            durationApiMs: 0,
            inputTokens,
            outputTokens,
            cacheReadTokens: 0,
            cacheCreationTokens: 0,
            costUSD: 0,
            output: text,
            stopReason: 'end',
            numTurns: 1,
        };
    }
    catch (err) {
        const durationMs = Date.now() - start;
        return {
            ok: false,
            error: errorMessage(err),
            durationMs,
            durationApiMs: 0,
            inputTokens: 0,
            outputTokens: 0,
            cacheReadTokens: 0,
            cacheCreationTokens: 0,
            costUSD: 0,
            output: null,
            stopReason: 'error',
            numTurns: 0,
        };
    }
}
//# sourceMappingURL=gemini.js.map