"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AntigravityRunner = void 0;
const logger_1 = require("../logger");
const platform_1 = require("../platform");
/**
 * Google Antigravity agent runner. Uses the `agy` CLI (or `antigravity` on some
 * installs). Opens the workspace and runs the agent with the prompt on stdin.
 * See https://antigravity.codes and antigravity.google/download.
 */
class AntigravityRunner {
    constructor() {
        this.name = 'antigravity';
    }
    isAvailable() {
        return (0, platform_1.commandExists)('agy') || (0, platform_1.commandExists)('antigravity');
    }
    async run(prompt, notes) {
        const fullPrompt = notes ? `${prompt}\n\nAdditional context:\n${notes}` : prompt;
        logger_1.logger.info('Running Antigravity agent...');
        logger_1.logger.debug(`Prompt: ${fullPrompt.slice(0, 200)}...`);
        const cwd = process.cwd();
        const bin = (0, platform_1.commandExists)('agy') ? 'agy' : 'antigravity';
        const baseArgs = ['.'];
        const attempts = [
            ['--agent', '--print', ...baseArgs],
            ['--print', ...baseArgs],
            baseArgs,
        ];
        let result = (0, platform_1.spawnCommand)(bin, 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)(bin, 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(`Antigravity exited with status ${result.status}`);
            if (error)
                logger_1.logger.warn(`antigravity stderr: ${error.slice(0, 500)}`);
            if (result.error)
                logger_1.logger.warn(`antigravity spawn error: ${result.error.message}`);
        }
        return { success, output, error };
    }
}
exports.AntigravityRunner = AntigravityRunner;
//# sourceMappingURL=antigravity.js.map