@@ -10,10 +10,11 @@ import {
1010 type ToolCall,
1111 type ToolResultMessage,
1212 type UserMessage,
13+ validateToolCall,
1314 } from "@earendil-works/pi-ai";
1415 import type { Session } from "./session.ts";
1516 import type { ToolName } from "./config.ts";
16-import { acceptsImages, executeTool, type ToolDetails, type ToolResult } from "./tools/index.ts";
17+import { acceptsImages, executeTool, type ToolResult } from "./tools/index.ts";
1718
1819 export type Phase = "preparing" | "waitingModel" | "streaming" | "runningTool" | "pausing" | "idle";
1920
@@ -61,14 +62,6 @@ interface AgentRun extends AgentOptions {
6162 onEvent: (event: AgentEvent) => void;
6263 }
6364
64-/** The concatenated text blocks of an assistant message, ignoring the rest. */
65-export function assistantText(message: AssistantMessage): string {
66- return message.content
67- .filter((block) => block.type === "text")
68- .map((block) => block.text)
69- .join("");
70-}
71-
7265 export async function runAgentTurn(run: AgentRun): Promise<void> {
7366 const { messages, session, signal, interaction, onEvent } = run;
7467
@@ -189,28 +182,22 @@ export async function runAgentTurn(run: AgentRun): Promise<void> {
189182 if (steering !== "") held.push(steering);
190183 onEvent({ type: "phase", phase: "runningTool", detail: call.name });
191184
192- const tool = run.tools.find((candidate) => candidate.name === call.name);
193185 let result: ToolResult;
194- if (tool === undefined) {
195- result = { text: `unknown tool: ${call.name}`, isError: true };
196- } else {
197- try {
198- result = await executeTool(call, {
199- signal,
200- supportsImages: acceptsImages(run.model),
201- onOutput: (chunk) => onEvent({ type: "toolOutput", chunk }),
202- });
203- } catch (error) {
204- result = { text: (error as Error).message, isError: true };
205- }
186+ try {
187+ result = await executeTool(call.name, validateToolCall(run.tools, call), {
188+ signal,
189+ supportsImages: acceptsImages(run.model),
190+ onOutput: (chunk) => onEvent({ type: "toolOutput", chunk }),
191+ });
192+ } catch (error) {
193+ result = { text: (error as Error).message, isError: true };
206194 }
207195
208- const toolMessage: ToolResultMessage<ToolDetails> = {
196+ const toolMessage: ToolResultMessage = {
209197 role: "toolResult",
210198 toolCallId: call.id,
211199 toolName: call.name,
212200 content: [{ type: "text", text: result.text }, ...(result.images ?? [])],
213- details: result.details,
214201 isError: result.isError,
215202 timestamp: Date.now(),
216203 };