/**
 * GeminiCliExecutor - Gemini CLI 执行器
 * 参考 Rust 实现: crates/executors/src/executors/gemini.rs
 */
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import { AgentType } from '../types/index.js';
import { BaseExecutor, AgentCapability, } from './base.executor.js';
import { CommandBuilder, applyOverrides } from './command-builder.js';
/**
 * 获取基础命令
 */
function getBaseCommand() {
    return 'npx -y @google/gemini-cli@0.23.0';
}
export class GeminiCliExecutor extends BaseExecutor {
    agentType = AgentType.GEMINI_CLI;
    displayName = 'Gemini CLI';
    config;
    constructor(config = {}) {
        super();
        this.config = config;
        this.cmdOverrides = config.cmd;
    }
    /**
     * 获取可用性信息
     */
    async getAvailabilityInfo() {
        const oauthCredsPath = path.join(os.homedir(), '.gemini', 'oauth_creds.json');
        try {
            const stats = fs.statSync(oauthCredsPath);
            const timestamp = Math.floor(stats.mtimeMs / 1000);
            return {
                type: 'LOGIN_DETECTED',
                lastAuthTimestamp: timestamp,
            };
        }
        catch {
            // 检查 MCP 配置或安装标识
            const mcpConfigPath = this.getDefaultMcpConfigPath();
            const installationIdPath = path.join(os.homedir(), '.gemini', 'installation_id');
            const mcpConfigFound = mcpConfigPath && fs.existsSync(mcpConfigPath);
            const installationFound = fs.existsSync(installationIdPath);
            if (mcpConfigFound || installationFound) {
                return { type: 'INSTALLATION_FOUND' };
            }
            return { type: 'NOT_FOUND', error: 'Gemini CLI not configured' };
        }
    }
    /**
     * 获取 Agent 能力
     */
    getCapabilities() {
        return [AgentCapability.SESSION_FORK];
    }
    /**
     * 获取默认 MCP 配置路径
     */
    getDefaultMcpConfigPath() {
        return path.join(os.homedir(), '.gemini', 'settings.json');
    }
    /**
     * 构建命令
     */
    buildCommandBuilder() {
        let builder = CommandBuilder.new(getBaseCommand());
        // 模型选择
        if (this.config.model) {
            builder.extendParams(['--model', this.config.model]);
        }
        // YOLO 模式
        if (this.config.yolo) {
            builder.extendParams(['--yolo']);
            builder.extendParams(['--allowed-tools', 'run_shell_command']);
        }
        // 启用 ACP 协议
        builder.extendParams(['--experimental-acp']);
        // 应用覆盖
        return applyOverrides(builder, this.cmdOverrides);
    }
    /**
     * 启动新会话
     */
    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);
    }
    /**
     * 继续现有会话
     */
    async spawnFollowUp(config, sessionId, resetToMessageId) {
        const commandBuilder = this.buildCommandBuilder();
        // Gemini 的 follow-up 参数（根据 ACP 协议）
        const additionalArgs = [];
        // TODO: 添加 Gemini 特定的会话恢复参数
        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=gemini-cli.executor.js.map