/**
 * Multi-agent handoff — Agent → Agent execution with history forwarding.
 *
 * Sprint-563: B2 — Multi-agent handoff() + asAgentTool() + handoffChain anti-cycle.
 *
 * handoff() wraps AgentsClient.execute with history forwarding (messages[]).
 * handoffChain: string[] prevents infinite cycles (HandoffCycleError if agent
 *   appears twice in the chain).
 * asAgentTool(descriptor, client) → AgentTool capability for MCP consumption.
 */
export class HandoffCycleError extends Error {
    constructor(agentId, chain) {
        super(`Handoff cycle detected: agent "${agentId}" already in chain [${chain.join(" → ")} → ${agentId}]. Aborting.`);
        this.name = "HandoffCycleError";
    }
}
/**
 * Execute a handoff to another agent. Forwards messages[] and checks for cycles.
 * Throws HandoffCycleError if the target agent is already in the chain.
 */
export async function handoff(opts, client) {
    const { targetAgentId, messages, handoffChain } = opts;
    if (handoffChain.includes(targetAgentId)) {
        throw new HandoffCycleError(targetAgentId, handoffChain);
    }
    const result = await client.execute(targetAgentId, {
        messages,
        handoffChain: [...handoffChain, targetAgentId],
    });
    return {
        childRunId: result.runId,
        nextChain: [...handoffChain, targetAgentId],
        accepted: true,
    };
}
/**
 * Build a CycleEventV011 'handoff_initiated' event.
 */
export function handoffToEvent(targetAgentId, childRunId, handoffChain, runId, cycleIndex) {
    return {
        type: "handoff_initiated",
        runId,
        cycleIndex,
        targetAgentId,
        childRunId,
        handoffChain,
        ts: Date.now(),
    };
}
export async function asAgentTool(descriptor, client) {
    return {
        name: `agent_${descriptor.agentId.replace(/[^a-zA-Z0-9_]/g, "_")}`,
        description: `Handoff to agent: ${descriptor.name} — ${descriptor.description}`,
        inputSchema: {
            type: "object",
            properties: {
                messages: {
                    type: "array",
                    items: {
                        type: "object",
                        properties: {
                            role: { type: "string", enum: ["system", "user", "assistant", "tool"] },
                            content: { type: "string" },
                        },
                        required: ["role", "content"],
                    },
                    description: "Conversation history to forward to the agent",
                },
                handoffChain: {
                    type: "array",
                    items: { type: "string" },
                    description: "Chain of agents that have already participated (anti-cycle)",
                },
            },
            required: ["messages"],
            additionalProperties: false,
        },
        async handler(input) {
            return client.execute(descriptor.agentId, {
                messages: input.messages,
                handoffChain: input.handoffChain ?? [],
            });
        },
    };
}
//# sourceMappingURL=handoff.js.map