/**
 * @module        acpCommand
 * @description   ACP 会话管理命令，支持创建、查询和控制 Agent 会话
 *
 * @author        eddy
 * @organization  wykj
 * @ownership     wykj/eddy
 *
 * @created       2026-03-27
 * @updated       2026-04-03
 *
 * @role          command
 * @layer         command
 * @boundedContext acp
 *
 * @trigger       sps acp <subcommand>
 * @inputs        project name, subcommand, positionals, flags
 * @outputs       ACP 会话状态信息（文本或 JSON）
 * @workflow      1. 解析子命令 → 2. 加载项目上下文 → 3. 创建 AgentRuntime → 4. 执行 ACP 操作
 */
import { ProjectContext } from '../core/context.js';
import { Logger } from '../core/logger.js';
import { createAgentRuntime } from '../providers/registry.js';
function parseTool(value) {
    if (!value)
        return undefined;
    if (value === 'claude')
        return value;
    return undefined;
}
function renderSessionSummary(session) {
    const run = session.currentRun
        ? `run=${session.currentRun.runId} ${session.currentRun.status} "${session.currentRun.promptPreview}"`
        : 'run=none';
    return `${session.slot} ${session.tool} session=${session.sessionId} slot=${session.status} remote=${session.sessionState} ${run}`;
}
export async function executeAcpCommand(project, subcommand, positionals, flags) {
    const log = new Logger('acp', project);
    const jsonOutput = !!flags.json;
    const ctx = ProjectContext.load(project);
    const runtime = createAgentRuntime(ctx);
    if (subcommand === 'ensure') {
        const slot = positionals[0];
        if (!slot) {
            throw new Error('Usage: sps acp ensure <project> <slot> [claude]');
        }
        const tool = parseTool(positionals[1]);
        const session = await runtime.ensureSession(slot, tool);
        if (jsonOutput) {
            console.log(JSON.stringify(session, null, 2));
        }
        else {
            log.ok(`ACP session ready: ${renderSessionSummary(session)}`);
        }
        return;
    }
    if (subcommand === 'run' || subcommand === 'prompt') {
        const slot = positionals[0];
        if (!slot) {
            throw new Error('Usage: sps acp run <project> <slot> [claude] "<prompt>"');
        }
        let tool = parseTool(positionals[1]);
        let promptStartIdx = 1;
        if (tool) {
            promptStartIdx = 2;
        }
        else {
            tool = undefined;
        }
        const prompt = positionals.slice(promptStartIdx).join(' ').trim();
        if (!prompt) {
            throw new Error('Prompt is required');
        }
        const session = await runtime.startRun(slot, prompt, tool);
        if (jsonOutput) {
            console.log(JSON.stringify(session, null, 2));
        }
        else {
            log.ok(`ACP run started: ${renderSessionSummary(session)}`);
        }
        return;
    }
    if (subcommand === 'status') {
        const slot = positionals[0];
        const state = await runtime.inspect(slot);
        if (jsonOutput) {
            console.log(JSON.stringify(state, null, 2));
        }
        else {
            const sessions = Object.values(state.sessions);
            if (sessions.length === 0) {
                log.info('No ACP sessions tracked');
            }
            else {
                for (const session of sessions) {
                    log.info(renderSessionSummary(session));
                }
            }
        }
        return;
    }
    if (subcommand === 'stop') {
        const slot = positionals[0];
        if (!slot) {
            throw new Error('Usage: sps acp stop <project> <slot>');
        }
        await runtime.stopSession(slot);
        if (jsonOutput) {
            console.log(JSON.stringify({ ok: true, project, slot }, null, 2));
        }
        else {
            log.ok(`Stopped ACP session for ${slot}`);
        }
        return;
    }
    if (subcommand === 'pending') {
        const state = await runtime.inspect();
        const pending = Object.values(state.sessions)
            .filter(s => s.pendingInput)
            .map(s => ({
            slot: s.slot,
            tool: s.tool,
            type: s.pendingInput.type,
            prompt: s.pendingInput.prompt,
            dangerous: s.pendingInput.dangerous || false,
            timestamp: s.pendingInput.timestamp,
        }));
        if (jsonOutput) {
            console.log(JSON.stringify(pending, null, 2));
        }
        else if (pending.length === 0) {
            log.info('No pending confirmations');
        }
        else {
            for (const p of pending) {
                const danger = p.dangerous ? '\x1b[31m DANGEROUS\x1b[0m' : '';
                log.warn(`${p.slot} | WAITING${danger} | ${p.prompt}`);
            }
            console.log(`\n  Respond with: sps acp respond ${project} <slot> "<response>"\n`);
        }
        return;
    }
    if (subcommand === 'respond') {
        const slot = positionals[0];
        const response = positionals.slice(1).join(' ').trim() || 'Y';
        if (!slot) {
            throw new Error('Usage: sps acp respond <project> <slot> "<response>"');
        }
        // Send response via ACP runtime
        await runtime.resumeRun(slot, response);
        if (jsonOutput) {
            console.log(JSON.stringify({ ok: true, project, slot, response }));
        }
        else {
            log.ok(`Sent "${response}" to ${slot}`);
        }
        return;
    }
    throw new Error('Usage: sps acp <ensure|run|prompt|status|stop|pending|respond> <project> [args...]');
}
//# sourceMappingURL=acpCommand.js.map