@@ -13,7 +13,6 @@ import {
1313 } from "@earendil-works/pi-ai";
1414 import type { Session } from "./session.ts";
1515 import { acceptsImages, executeTool, type ToolDetails, type ToolResult } from "./tools/index.ts";
16-import type { ToolName } from "./config.ts";
1716
1817 export type Phase = "preparing" | "waitingModel" | "streaming" | "runningTool" | "pausing" | "idle";
1918
@@ -21,15 +20,15 @@ export type AgentEvent =
2120 | { type: "phase"; phase: Phase; detail?: string }
2221 | { type: "text"; delta: string }
2322 | { type: "reasoning"; delta: string }
24- | { type: "toolCall"; name: string; callId: string; arguments: JsonObject }
25- | { type: "toolOutput"; name: string; callId: string; chunk: string }
26- | { type: "toolResult"; name: string; callId: string; text: string; isError: boolean }
23+ | { type: "toolCall"; name: string; arguments: JsonObject }
24+ | { type: "toolOutput"; chunk: string }
25+ | { type: "toolResult"; name: string; text: string; isError: boolean }
2726 | { type: "message"; message: AssistantMessage }
2827 | { type: "error"; message: string }
2928 | { type: "cancelled" }
3029 | { type: "complete" };
3130
32-export interface Interaction {
31+interface Interaction {
3332 isPauseRequested(): boolean;
3433 clearPause(): void;
3534 requestSteering(): Promise<string>;
@@ -47,12 +46,11 @@ export interface AgentOptions {
4746 model: Model<Api>;
4847 systemPrompt: string;
4948 tools: Tool[];
50- toolNames: ToolName[];
5149 thinkingEffort: ModelThinkingLevel;
5250 session: Session;
5351 }
5452
55-export interface AgentRun extends AgentOptions {
53+interface AgentRun extends AgentOptions {
5654 messages: Message[];
5755 signal: AbortSignal;
5856 interaction: Interaction;
@@ -63,9 +61,8 @@ export async function runAgentTurn(run: AgentRun): Promise<void> {
6361 const { messages, session, signal, interaction, onEvent } = run;
6462
6563 /** Appends a user message the model reads at the next step boundary. */
66- const steer = (texts: string[]): void => {
67- const content = texts.filter((text) => text.length > 0).join("\n");
68- if (content.length === 0) return;
64+ const steer = (content: string): void => {
65+ if (content === "") return;
6966 const message: UserMessage = { role: "user", content, timestamp: Date.now() };
7067 messages.push(message);
7168 session.appendMessage(message);
@@ -74,28 +71,29 @@ export async function runAgentTurn(run: AgentRun): Promise<void> {
7471 /**
7572 * Returns the steering typed while paused, or ""; the caller decides where
7673 * it lands, because a user message between an assistant turn and its tool
77- * results is rejected by the provider.
74+ * results is rejected by the provider. Null once the turn has been aborted;
75+ * every step boundary is a cancellation point.
7876 */
79- const pause = async (): Promise<string> => {
80- if (!interaction.isPauseRequested()) return "";
81- interaction.clearPause();
82- onEvent({ type: "phase", phase: "pausing" });
83- const steering = await interaction.requestSteering();
84- onEvent({ type: "phase", phase: "idle" });
85- return steering;
77+ const pauseStep = async (): Promise<string | null> => {
78+ if (signal.aborted) return null;
79+ if (interaction.isPauseRequested()) {
80+ interaction.clearPause();
81+ onEvent({ type: "phase", phase: "pausing" });
82+ const steering = await interaction.requestSteering();
83+ onEvent({ type: "phase", phase: "idle" });
84+ if (signal.aborted) return null;
85+ return steering;
86+ }
87+ return "";
8688 };
8789
8890 for (;;) {
89- if (signal.aborted) {
90- onEvent({ type: "cancelled" });
91- return;
92- }
93- const steering = await pause();
94- if (signal.aborted) {
91+ const steering = await pauseStep();
92+ if (steering === null) {
9593 onEvent({ type: "cancelled" });
9694 return;
9795 }
98- steer([steering]);
96+ steer(steering);
9997
10098 onEvent({ type: "phase", phase: "preparing" });
10199 session.appendRequest({
@@ -130,7 +128,6 @@ export async function runAgentTurn(run: AgentRun): Promise<void> {
130128 onEvent({
131129 type: "toolCall",
132130 name: event.toolCall.name,
133- callId: event.toolCall.id,
134131 arguments: event.toolCall.arguments,
135132 });
136133 }
@@ -165,28 +162,24 @@ export async function runAgentTurn(run: AgentRun): Promise<void> {
165162 // tool results.
166163 const held: string[] = [];
167164 for (const call of toolCalls) {
168- if (signal.aborted) {
169- onEvent({ type: "cancelled" });
170- return;
171- }
172- const steering = await pause();
173- if (signal.aborted) {
165+ const steering = await pauseStep();
166+ if (steering === null) {
174167 onEvent({ type: "cancelled" });
175168 return;
176169 }
177170 if (steering !== "") held.push(steering);
178171 onEvent({ type: "phase", phase: "runningTool", detail: call.name });
179172
180- const tool = run.toolNames.find((name) => name === call.name);
173+ const tool = run.tools.find((candidate) => candidate.name === call.name);
181174 let result: ToolResult;
182175 if (tool === undefined) {
183176 result = { text: `unknown tool: ${call.name}`, isError: true };
184177 } else {
185178 try {
186- result = await executeTool(tool, call, {
179+ result = await executeTool(call, {
187180 signal,
188181 supportsImages: acceptsImages(run.model),
189- onOutput: (chunk) => onEvent({ type: "toolOutput", name: call.name, callId: call.id, chunk }),
182+ onOutput: (chunk) => onEvent({ type: "toolOutput", chunk }),
190183 });
191184 } catch (error) {
192185 result = { text: (error as Error).message, isError: true };
@@ -204,8 +197,8 @@ export async function runAgentTurn(run: AgentRun): Promise<void> {
204197 };
205198 messages.push(toolMessage);
206199 session.appendMessage(toolMessage);
207- onEvent({ type: "toolResult", name: call.name, callId: call.id, text: result.text, isError: result.isError });
200+ onEvent({ type: "toolResult", name: call.name, text: result.text, isError: result.isError });
208201 }
209- steer(held);
202+ steer(held.join("\n"));
210203 }
211204 }