// orchestrator/handoff-generator.ts — Handoff package generation (SPEC-079 AC-12)
// Generates complete context packages for each agent.
import { getAgentFiles, formatOwnershipContext } from './file-ownership.js';
/**
 * Generate a handoff package for a specific agent.
 * Includes context, files, restrictions, and success criteria.
 */
export function generateHandoffPackage(agent, assignment, ownershipMap, previousOutput) {
    const ownedFiles = getAgentFiles(ownershipMap, agent.id);
    const otherAgentFiles = Object.entries(ownershipMap)
        .filter(([, owner]) => owner !== agent.id)
        .map(([file]) => file);
    const restrictions = otherAgentFiles.map((f) => `DO NOT modify ${f} (owned by another agent)`);
    const ownershipContext = formatOwnershipContext(ownershipMap, agent.id);
    const contextParts = [ownershipContext, `\n## Task\n${assignment.context}`];
    if (previousOutput) {
        contextParts.push(`\n## Previous Agent Output\n${previousOutput}`);
    }
    if (assignment.dependencies.length > 0) {
        contextParts.push(`\n## Dependencies\nThis task depends on: ${assignment.dependencies.join(', ')}`);
    }
    return {
        agentId: agent.id,
        role: agent.capabilities[0] ?? 'general',
        filesToRead: [...ownedFiles, ...otherAgentFiles],
        filesToModify: ownedFiles,
        restrictions,
        instructions: assignment.context,
        context: contextParts.join('\n'),
        successCriteria: [
            `Complete task: ${assignment.context}`,
            `Only modify assigned files: ${ownedFiles.join(', ')}`,
            'Report any issues or blockers encountered',
        ],
        dependencies: assignment.dependencies,
    };
}
/**
 * Generate handoff packages for all agents in the session.
 */
export function generateAllHandoffs(agents, assignments, ownershipMap) {
    return assignments.map((assignment) => {
        const agent = agents.find((a) => a.id === assignment.agentId);
        const defaultAgent = {
            id: assignment.agentId,
            command: '',
            args: [],
            capabilities: [],
        };
        return generateHandoffPackage(agent ?? defaultAgent, assignment, ownershipMap);
    });
}
/**
 * Generate a pipeline handoff: output of previous agent becomes context for next.
 */
export function generatePipelineHandoff(agents, assignments, ownershipMap, previousOutputs) {
    return assignments.map((assignment, index) => {
        const agent = agents.find((a) => a.id === assignment.agentId);
        const defaultAgent = {
            id: assignment.agentId,
            command: '',
            args: [],
            capabilities: [],
        };
        // Get previous agent's output for pipeline chain
        let previousOutput;
        if (index > 0) {
            const prevAssignment = assignments[index - 1];
            if (prevAssignment) {
                previousOutput = previousOutputs.get(prevAssignment.agentId);
            }
        }
        return generateHandoffPackage(agent ?? defaultAgent, assignment, ownershipMap, previousOutput);
    });
}
/**
 * Format a handoff package as a markdown prompt string.
 * Language-agnostic format: works for any agent type.
 */
export function formatHandoffAsPrompt(handoff) {
    const lines = [
        `# Agent Handoff: ${handoff.agentId}`,
        `**Role**: ${handoff.role}`,
        '',
        handoff.context,
        '',
        '## Success Criteria',
        ...handoff.successCriteria.map((c) => `- ${c}`),
        '',
        '## Restrictions',
        ...(handoff.restrictions.length > 0 ? handoff.restrictions.map((r) => `- ${r}`) : ['- None']),
    ];
    return lines.join('\n');
}
//# sourceMappingURL=handoff-generator.js.map