/**
 * CursorAgentExecutor - Cursor Agent CLI 执行器
 * 参考 vibe-kanban Rust 实现: crates/executors/src/executors/cursor.rs
 */
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import { AgentType } from '../types/index.js';
import { which } from '../utils/index.js';
import { BaseExecutor, AgentCapability, } from './base.executor.js';
import { CommandBuilder, applyOverrides } from './command-builder.js';
/**
 * 获取基础命令
 */
function getBaseCommand() {
    return 'cursor-agent';
}
export class CursorAgentExecutor extends BaseExecutor {
    agentType = AgentType.CURSOR_AGENT;
    displayName = 'Cursor Agent';
    config;
    constructor(config = {}) {
        super();
        this.config = config;
        this.cmdOverrides = config.cmd;
    }
    /**
     * 获取可用性信息
     */
    async getAvailabilityInfo() {
        // 检查 cursor-agent 命令是否可用
        const cursorAgentPath = await which(getBaseCommand());
        if (!cursorAgentPath) {
            return { type: 'NOT_FOUND', error: 'Cursor Agent CLI not installed. Run: curl https://cursor.com/install -fsS | bash' };
        }
        // 检查 MCP 配置文件是否存在（作为安装标识）
        const mcpConfigPath = this.getDefaultMcpConfigPath();
        if (mcpConfigPath && fs.existsSync(mcpConfigPath)) {
            return { type: 'INSTALLATION_FOUND' };
        }
        // 命令存在但没有配置文件，也视为已安装
        return { type: 'INSTALLATION_FOUND' };
    }
    /**
     * 获取 Agent 能力
     */
    getCapabilities() {
        return [AgentCapability.SETUP_HELPER];
    }
    /**
     * 获取默认 MCP 配置路径
     */
    getDefaultMcpConfigPath() {
        return path.join(os.homedir(), '.cursor', 'mcp.json');
    }
    /**
     * 构建命令
     */
    buildCommandBuilder() {
        let builder = CommandBuilder.new(getBaseCommand());
        // 基础参数: -p 表示从 stdin 读取 prompt, --output-format=stream-json 输出 JSON 流
        builder.setParams(['-p', '--output-format=stream-json']);
        // 强制模式
        if (this.config.force) {
            builder.extendParams(['--force']);
        }
        // 模型选择
        if (this.config.model) {
            builder.extendParams(['--model', this.config.model]);
        }
        // 应用覆盖
        return applyOverrides(builder, this.cmdOverrides);
    }
    /**
     * 启动新会话
     * Cursor Agent 通过 stdin 接收 prompt，不同于 Claude Code 通过参数传递
     */
    async spawn(config) {
        const commandBuilder = this.buildCommandBuilder();
        const commandParts = commandBuilder.buildInitial();
        // 组合 prompt
        const prompt = this.combinePrompt(config.prompt);
        const newConfig = { ...config, prompt };
        return this.spawnInternal(newConfig, commandParts);
    }
    /**
     * 继续现有会话
     * Cursor Agent 使用 --resume 参数恢复会话
     */
    async spawnFollowUp(config, sessionId, _resetToMessageId) {
        const commandBuilder = this.buildCommandBuilder();
        const additionalArgs = ['--resume', sessionId];
        const commandParts = commandBuilder.buildFollowUp(additionalArgs);
        // 组合 prompt
        const prompt = this.combinePrompt(config.prompt);
        const newConfig = { ...config, prompt };
        return this.spawnInternal(newConfig, commandParts);
    }
    /**
     * 组合 prompt
     */
    combinePrompt(prompt) {
        if (this.config.appendPrompt) {
            return `${prompt}${this.config.appendPrompt}`;
        }
        return prompt;
    }
}
//# sourceMappingURL=cursor-agent.executor.js.map