import { readJson, writeJson, projectDataDir } from '../storage/index.js';
import { routeTask, generateHandoff, detectConflicts, decomposeSpec, } from '../engine/multi-agent/index.js';
import { compactResult } from './output-formatter.js';
function registryPath(projectId) {
    return `${projectDataDir(projectId)}/capability-registry.json`;
}
async function loadRegistry(projectId) {
    return readJson(registryPath(projectId), []);
}
async function saveRegistry(projectId, registry) {
    await writeJson(registryPath(projectId), registry);
}
function text(content) {
    return compactResult(JSON.stringify(content, null, 2));
}
function err(message) {
    return { content: [{ type: 'text', text: message }], isError: true };
}
export async function handleOrchestrateAgents(args) {
    const projectId = args.projectId ?? 'global';
    switch (args.action) {
        case 'register-agent': {
            if (!args.agentId) {
                return err('agentId is required for register-agent. Provide a unique agent identifier.');
            }
            const capability = {
                agentId: args.agentId,
                role: args.role ?? 'implementer',
                ecosystems: args.ecosystems ?? [],
                filePatterns: args.filePatterns ?? [],
                maxConcurrency: args.maxConcurrency ?? 1,
                preferredModel: args.preferredModel ?? 'sonnet',
            };
            const registry = await loadRegistry(projectId);
            const idx = registry.findIndex((c) => c.agentId === args.agentId);
            if (idx >= 0) {
                registry[idx] = capability;
            }
            else {
                registry.push(capability);
            }
            await saveRegistry(projectId, registry);
            return text({
                registered: true,
                agentId: capability.agentId,
                role: capability.role,
                preferredModel: capability.preferredModel,
                totalRegistered: registry.length,
            });
        }
        case 'list-agents': {
            const registry = await loadRegistry(projectId);
            return text({ agents: registry, total: registry.length });
        }
        case 'route-task': {
            if (!args.taskDescription) {
                return err('taskDescription is required for route-task. Describe the task in natural language.');
            }
            const registry = await loadRegistry(projectId);
            const route = routeTask(args.taskDescription, registry);
            return text(route);
        }
        case 'decompose-spec': {
            const criteria = args.specCriteria ?? [];
            const agents = args.availableAgents ?? [];
            const assignments = decomposeSpec(criteria, agents);
            return text({ assignments, total: assignments.length });
        }
        case 'handoff': {
            if (!args.sessionContext) {
                return err('sessionContext is required for handoff. Provide fromAgent, toAgent, completedWork, decisions, fileOwnershipMap, and pendingWork.');
            }
            const handoff = generateHandoff(args.sessionContext);
            return text(handoff);
        }
        case 'conflict-check': {
            const plans = args.agentPlans ?? [];
            const report = detectConflicts(plans);
            return text(report);
        }
    }
}
//# sourceMappingURL=orchestrate-agents.js.map