@@ -5,12 +5,11 @@ import {
55 type AssistantMessage,
66 type AssistantMessageEvent,
77 type Context,
8- completeSimple,
98 type Message,
109 streamSimple,
1110 type ToolResultMessage,
1211 } from "@mariozechner/pi-ai";
13-import { simpleGit, type StatusResult } from "simple-git";
12+import { type StatusResult, simpleGit } from "simple-git";
1413 import { parseSkillFrontmatter } from "./shared";
1514 import type { CliOptions, ToolAndRunner } from "./types";
1615
@@ -76,11 +75,11 @@ export const TASK_PROMPT = `${IDENTITY_PROMPT}
7675
7776 async function getEnvPrompt() {
7877 // TODO: What else do the agents always check before answering every time?
79- let gitStatus: StatusResult | {nogit: string};
78+ let gitStatus: StatusResult | { nogit: string };
8079 try {
8180 gitStatus = await simpleGit().status();
8281 } catch (_) {
83- gitStatus = {nogit: "No git repo in this folder."};
82+ gitStatus = { nogit: "No git repo in this folder." };
8483 }
8584 const envStatus = JSON.stringify(
8685 {
@@ -216,6 +215,7 @@ export async function streamAgent(
216215 const onAbort = () => {
217216 controller?.abort();
218217 };
218+
219219 // Wire the global abort signal to each turn http requests
220220 abortController?.signal.addEventListener("abort", onAbort);
221221
@@ -237,11 +237,12 @@ export async function streamAgent(
237237
238238 // Check if the agent is done, or an error/abort happened before moving
239239 // on towards tool calling.
240- if (["stop", "error", "aborted"].includes(finalMessage.stopReason)) {
240+ if (
241+ ["stop", "error", "aborted", "length"].includes(finalMessage.stopReason)
242+ ) {
241243 // Agent is done, send the final message, and current context.
242244 completeFn?.(finalMessage, context);
243- abortController?.signal.removeEventListener("abort", onAbort);
244- return;
245+ break;
245246 }
246247
247248 // Call tolls and per tool result callback.
@@ -250,14 +251,27 @@ export async function streamAgent(
250251 );
251252
252253 for (const call of toolCalls) {
254+ let output = "";
255+ let isError = false;
253256 const toolDef = tools.find((i) => i.tool.name === call.name);
254- const result = (await toolDef?.runner(call.arguments)) || "";
257+ if (toolDef) {
258+ try {
259+ output = await toolDef.runner(call.arguments);
260+ } catch (err) {
261+ const error = err instanceof Error ? err.message : "Unknown error";
262+ output = `Tool call failed: ${error}`;
263+ isError = true;
264+ }
265+ } else {
266+ output = `Unknown tool: ${call.name}`;
267+ }
268+
255269 const msg: ToolResultMessage = {
256270 role: "toolResult",
257271 toolCallId: call.id,
258272 toolName: call.name,
259- content: [{ type: "text", text: result }],
260- isError: false,
273+ content: [{ type: "text", text: output }],
274+ isError,
261275 timestamp: Date.now(),
262276 };
263277 context.messages.push(msg);
@@ -265,15 +279,6 @@ export async function streamAgent(
265279 // Update with tool results.
266280 toolsFn?.(msg, context);
267281 }
268-
269- if (toolCalls.length > 0) {
270- const cont = await completeSimple(options.model, context, {
271- apiKey,
272- reasoning: options.effort,
273- signal: controller.signal,
274- });
275- context.messages.push(cont);
276- }
277282 }
278283 } finally {
279284 abortController?.signal.removeEventListener("abort", onAbort);