// tools/agent-swarm-status.ts — Handler for agent_swarm_status MCP tool (SPEC-079 AC-11)
import { getStatus, listSessions } from '../engine/orchestrator/index.js';
/**
 * Handle the agent_swarm_status tool invocation.
 * Returns status of orchestration sessions, optionally filtered.
 */
export function handleAgentSwarmStatus(input) {
    // If sessionId is provided, return that specific session
    if (input.sessionId) {
        return getSessionStatus(input.sessionId, input.agentId);
    }
    // Otherwise, list all sessions with optional filter
    return listSessionsStatus(input.status, input.agentId);
}
/**
 * Get status of a specific session.
 */
function getSessionStatus(sessionId, agentIdFilter) {
    const session = getStatus(sessionId);
    if (!session) {
        return {
            content: [{ type: 'text', text: `Error: Session '${sessionId}' not found.` }],
            isError: true,
        };
    }
    const lines = [
        '## Orchestration Session Status',
        '',
        `**Session**: ${session.id}`,
        `**Topology**: ${session.topology}`,
        `**Status**: ${session.status}`,
        `**Started**: ${session.startedAt}`,
        session.completedAt ? `**Completed**: ${session.completedAt}` : '',
        '',
        '### Agents',
    ];
    const agents = agentIdFilter
        ? session.agents.filter((a) => a.agentId === agentIdFilter)
        : session.agents;
    for (const agent of agents) {
        lines.push(`- **${agent.agentId}**: ${agent.status} (retries: ${String(agent.retryCount)})`);
        if (agent.tokensUsed) {
            lines.push(`  - Tokens: ${String(agent.tokensUsed)}`);
        }
    }
    lines.push('', '### Tasks');
    for (const task of session.tasks) {
        if (agentIdFilter && task.agentId !== agentIdFilter) {
            continue;
        }
        lines.push(`- **${task.taskId}** → ${task.agentId}: priority ${String(task.priority)}`);
    }
    // Metrics
    const completed = session.agents.filter((a) => a.status === 'completed').length;
    const failed = session.agents.filter((a) => a.status === 'failed').length;
    const total = session.agents.length;
    const successRate = total > 0 ? Math.round((completed / total) * 100) : 0;
    lines.push('', '### Metrics');
    lines.push(`- Success rate: ${String(successRate)}%`);
    lines.push(`- Completed: ${String(completed)}/${String(total)}`);
    lines.push(`- Failed: ${String(failed)}/${String(total)}`);
    const totalTokens = session.agents.reduce((sum, a) => sum + (a.tokensUsed ?? 0), 0);
    if (totalTokens > 0) {
        lines.push(`- Total tokens: ${String(totalTokens)}`);
    }
    return {
        content: [{ type: 'text', text: lines.filter(Boolean).join('\n') }],
    };
}
/**
 * List all sessions with optional status filter.
 */
function listSessionsStatus(statusFilter, agentIdFilter) {
    let allSessions = listSessions(statusFilter);
    if (agentIdFilter) {
        allSessions = allSessions.filter((s) => s.agents.some((a) => a.agentId === agentIdFilter));
    }
    if (allSessions.length === 0) {
        const filterMsg = statusFilter ? ` with status '${statusFilter}'` : '';
        return {
            content: [{ type: 'text', text: `No orchestration sessions found${filterMsg}.` }],
        };
    }
    const lines = [`## Orchestration Sessions (${String(allSessions.length)})`, ''];
    for (const session of allSessions) {
        const agentCount = session.agents.length;
        const taskCount = session.tasks.length;
        lines.push(`- **${session.id}**: ${session.topology} | ${session.status} | ${String(agentCount)} agents | ${String(taskCount)} tasks | ${session.startedAt}`);
    }
    return {
        content: [{ type: 'text', text: lines.join('\n') }],
    };
}
//# sourceMappingURL=agent-swarm-status.js.map