import type { AgentToolResult } from "@earendil-works/pi-agent-core"; import { type ExtensionAPI, type ExtensionContext, defineTool } from "@earendil-works/pi-coding-agent"; import { Type } from "typebox"; import { WORKER_WAIT_MS, type SleepRequest } from "../scheduler/index.ts"; export interface WaitForAgentDeps { enabled: () => boolean; blocked: () => boolean; activeRuns: (ctx: ExtensionContext) => number; requestSleep: (request: SleepRequest) => void; } export function registerWaitForAgentTool(pi: ExtensionAPI, deps: WaitForAgentDeps): void { pi.registerTool( defineTool({ name: "wait_for_agent", label: "Wait for Agent", description: "End this supervision turn and wait for the active worker's next completion, attention event, or five-minute review. Use after direct event inspection shows the worker is on track. Use this tool for worker waits.", parameters: Type.Object({}), execute: async (_id, _params, _signal, _onUpdate, ctx): Promise> => { if (!deps.enabled()) throw new Error("wait_for_agent: AGI mode is not enabled."); if (deps.blocked()) throw new Error("wait_for_agent: you reported blocked. Ask the user for the needed decision instead of waiting."); if (deps.activeRuns(ctx) === 0) throw new Error("wait_for_agent: there is no active worker to wait for."); deps.requestSleep({ source: "worker" }); return { content: [{ type: "text", text: "Waiting for the active worker's next completion, attention event, or trajectory review." }], details: { waitForAgent: { ms: WORKER_WAIT_MS } }, terminate: true, }; }, }), ); }