"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodexRunner = void 0;
const logger_1 = require("../logger");
const platform_1 = require("../platform");
/**
 * OpenAI Codex CLI runner. Uses `codex exec` for non-interactive agent runs.
 * Install: npm install -g @openai/codex. Set OPENAI_API_KEY or use codex login.
 */
class CodexRunner {
    constructor() {
        this.name = 'codex';
    }
    isAvailable() {
        return (0, platform_1.commandExists)('codex');
    }
    async run(prompt, notes) {
        const fullPrompt = notes ? `${prompt}\n\nAdditional context:\n${notes}` : prompt;
        logger_1.logger.info('Running Codex CLI (exec)...');
        logger_1.logger.debug(`Prompt: ${fullPrompt.slice(0, 200)}...`);
        const cwd = process.cwd();
        const args = [
            'exec',
            '--ask-for-approval', 'never',
            '--sandbox', 'workspace-write',
            '--cd', cwd,
            fullPrompt,
        ];
        const result = (0, platform_1.spawnCommand)('codex', args, {
            encoding: 'utf8',
            timeout: 10 * 60 * 1000,
            cwd,
            env: (0, platform_1.getUserShellEnv)(),
        });
        const success = result.status === 0;
        const output = result.stdout || '';
        const error = result.stderr || '';
        if (!success) {
            logger_1.logger.warn(`Codex exited with status ${result.status}`);
            if (error)
                logger_1.logger.warn(`codex stderr: ${error.slice(0, 500)}`);
            if (result.error)
                logger_1.logger.warn(`codex spawn error: ${result.error.message}`);
        }
        return { success, output, error };
    }
}
exports.CodexRunner = CodexRunner;
//# sourceMappingURL=codex.js.map