/** * Intercom tools injected into child AgentSessions. * * When `allowIntercom: true`, the parent registers these tools on the child so * the child can talk back to the parent mid-run: * * - ask_parent : blocking — child pauses until the parent replies via * `reply_subagent`. Use for genuine blockers. * - notify_parent : non-blocking one-way message (a finding / status). * Parent sees it on its next turn; child keeps running. * - update_progress: lightweight phase hint, updates the task's progress text. * * The handlers close over the SubagentManager so child→parent traffic flows * through the central registry (and can wake the parent via sendMessage). */ import { StringEnum } from "@earendil-works/pi-ai"; import type { ToolDefinition } from "@earendil-works/pi-coding-agent"; import { Type } from "typebox"; export interface IntercomHandlers { /** Block the child until the parent replies. Resolves with the reply text. */ onAskParent(taskId: string, question: string): Promise; /** Fire-and-forget message to the parent. */ onNotifyParent(taskId: string, message: string, level: "info" | "warning" | "error"): void; /** Update the task's live progress label. */ onUpdateProgress(taskId: string, phase: string, note?: string): void; } /** * Build the intercom tool set for one child task. Each tool is bound to the * given taskId so the manager routes traffic correctly. */ export function createIntercomTools(taskId: string, handlers: IntercomHandlers): ToolDefinition[] { return [ { name: "ask_parent", label: "Ask Parent", description: "Ask the parent agent a clarifying question and BLOCK until it replies. Use sparingly — only when you genuinely cannot proceed without information only the parent has (e.g. ambiguous requirements, missing credentials). Prefer figuring it out yourself first.", promptSnippet: "Ask the parent agent a question when truly blocked.", promptGuidelines: [ "Use ask_parent only as a last resort when blocked on information only the parent has. Attempt to resolve ambiguity yourself first.", "Ask one focused question at a time. The parent's reply resumes your work.", ], parameters: Type.Object({ question: Type.String({ description: "A single, focused question for the parent agent" }), }), async execute(_toolCallId, params) { const { question } = params as { question: string }; const answer = await handlers.onAskParent(taskId, question); return { content: [{ type: "text" as const, text: answer || "(parent gave no answer)" }], details: {} }; }, }, { name: "notify_parent", label: "Notify Parent", description: "Send a non-blocking message to the parent agent (a finding, a risk, a heads-up). Does NOT wait for a reply — your run continues immediately. The parent will see this on its next turn.", promptSnippet: "Send the parent a non-blocking update or finding.", parameters: Type.Object({ message: Type.String({ description: "The message content for the parent" }), level: Type.Optional(StringEnum(["info", "warning", "error"] as const, { default: "info" })), }), async execute(_toolCallId, params) { const { message, level } = params as { message: string; level?: "info" | "warning" | "error" }; handlers.onNotifyParent(taskId, message, level ?? "info"); return { content: [{ type: "text" as const, text: "Sent." }], details: {} }; }, }, { name: "update_progress", label: "Update Progress", description: "Report your current work phase to the parent. Lightweight and non-blocking; helps the parent track progress without waiting.", promptSnippet: "Report current work phase to the parent.", parameters: Type.Object({ phase: Type.String({ description: "Short label for the current phase, e.g. 'reviewing auth module'" }), note: Type.Optional(Type.String({ description: "Optional extra detail" })), }), async execute(_toolCallId, params) { const { phase, note } = params as { phase: string; note?: string }; handlers.onUpdateProgress(taskId, phase, note); return { content: [{ type: "text" as const, text: "Progress updated." }], details: {} }; }, }, ]; }