import { z } from "zod"; import { getInternalBridgeConfig } from "./internalBridge.js"; import { tool, type ToolContext } from "./tool.js"; type Operation = "send" | "reply"; // This helper is not a model-facing tool. Each registered tool fixes its operation // and input contract; the bridge resolves identity, project, run and thread scope. export function agentMessageTool(operation: Operation, definition: { description: string; args: Args; }) { const inputSchema = z.object(definition.args).strict(); return tool({ ...definition, async execute(args, context: ToolContext) { const input = inputSchema.parse(args); const { baseUrl, internalToken } = getInternalBridgeConfig(); const response = await fetch(`${baseUrl}/internal/agent-messages/${operation}`, { method: "POST", headers: { "Content-Type": "application/json", "x-agent-internal-token": internalToken }, signal: context.signal, body: JSON.stringify({ session_id: context.sessionID, input }), }); const text = await response.text(); if (!response.ok) throw new Error(`agent message ${operation} failed (${response.status}): ${text}`); return text; }, }); }