"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CursorRunner = void 0;
const logger_1 = require("../logger");
const platform_1 = require("../platform");
/**
 * Cursor Agent CLI runner. Uses the `agent` binary on all platforms.
 * On Windows, the Cursor IDE is `cursor.exe` and does not support headless agent
 * mode; the separate Agent CLI must be installed (e.g. irm 'https://cursor.com/install?win32=true' | iex)
 * so that `agent` is in PATH.
 */
class CursorRunner {
    constructor() {
        this.name = 'cursor';
    }
    isAvailable() {
        return (0, platform_1.commandExists)('agent');
    }
    async run(prompt, notes) {
        const fullPrompt = notes ? `${prompt}\n\nAdditional context:\n${notes}` : prompt;
        logger_1.logger.info('Running Cursor Agent...');
        logger_1.logger.debug(`Prompt: ${fullPrompt.slice(0, 200)}...`);
        const cwd = process.cwd();
        const baseArgs = ['--print', '--force', '--trust', '--workspace', cwd];
        const attempts = [
            [...baseArgs, '--model', 'auto'],
            [...baseArgs, '--reasoning', 'auto'],
            baseArgs,
        ];
        let result = (0, platform_1.spawnCommand)('agent', attempts[0], {
            encoding: 'utf8',
            timeout: 10 * 60 * 1000,
            cwd,
            env: (0, platform_1.getUserShellEnv)(),
            input: fullPrompt,
        });
        for (let i = 1; i < attempts.length; i++) {
            if (result.status === 0)
                break;
            if (!(0, platform_1.shouldRetryAgentCliAttempt)(result.stderr || '', result.stdout || ''))
                break;
            result = (0, platform_1.spawnCommand)('agent', attempts[i], {
                encoding: 'utf8',
                timeout: 10 * 60 * 1000,
                cwd,
                env: (0, platform_1.getUserShellEnv)(),
                input: fullPrompt,
            });
        }
        const success = result.status === 0;
        const output = result.stdout || '';
        const error = result.stderr || '';
        if (!success) {
            logger_1.logger.warn(`Cursor Agent exited with status ${result.status}`);
            if (error)
                logger_1.logger.warn(`cursor stderr: ${error.slice(0, 500)}`);
            if (result.error)
                logger_1.logger.warn(`cursor spawn error: ${result.error.message}`);
        }
        return { success, output, error };
    }
}
exports.CursorRunner = CursorRunner;
//# sourceMappingURL=cursor.js.map