xonecas.io
64875
Mc

mini-coder / commit 433a825

feat: tool response streaming

xonecas2026-04-26 433a825a374bdac86e05ce99a1dd646b6ab6a6fc parent c62c160

12 files changed

benchmarks/terminal-bench-suite.sh+8−0
@@ -0,0 +1,8 @@
1+PYTHONPATH="$PWD" harbor run -y \
2+ --job-name "terminal-bench-suite-$(date +%F__%H-%M-%S)" \
3+ --jobs-dir "$PWD/benchmarks/jobs" \
4+ --dataset terminal-bench/terminal-bench-2 \
5+ --agent-import-path benchmarks.local-harbor-agent:LocalMiniCoderAgent \
6+ --n-attempts 2 \
7+ --n-concurrent 2 \
8+ --max-retries 0
bun.lock+6−6
@@ -5,8 +5,8 @@
55 "": {
66 "name": "mini-coder-next",
77 "dependencies": {
8- "@cel-tui/components": "^0.8.1",
9- "@cel-tui/core": "^0.8.1",
8+ "@cel-tui/components": "^0.8.2",
9+ "@cel-tui/core": "^0.8.2",
1010 "@mariozechner/pi-ai": "^0.70.2",
1111 "diff": "^9.0.0",
1212 "simple-git": "^3.36.0",
@@ -115,13 +115,13 @@
115115
116116 "@biomejs/cli-win32-x64": ["@biomejs/cli-win32-x64@2.4.13", "", { "os": "win32", "cpu": "x64" }, "sha512-tTcMkXyBrmHi9BfrD2VNHs/5rYIUKETqsBlYOvSAABwBkJhSDVb5e7wPukftsQbO3WzQkXe6kaztC6WtUOXSoQ=="],
117117
118- "@cel-tui/clew": ["@cel-tui/clew@0.8.2", "", { "peerDependencies": { "typescript": ">=5.0.0" } }, "sha512-7IYKc8Pe+CCUngw9jpgNDc6LgJoPEDUr0Pqu1uNrTdwFMBaiGirV8b5Z/UbQ7/CtIyPnVDyNO2qAY/wOJNGidg=="],
118+ "@cel-tui/clew": ["@cel-tui/clew@0.8.3", "", { "peerDependencies": { "typescript": ">=5.0.0" } }, "sha512-7OFUe8ECleXjq9+vv2EvDcTXsiWNSQqVALD1LC/Gi9DzVp1aw0S6SloJuAv1KUVkwjZxil+mvYlk2LoC28mETQ=="],
119119
120- "@cel-tui/components": ["@cel-tui/components@0.8.2", "", { "dependencies": { "@cel-tui/clew": "0.8.2", "@cel-tui/core": "0.8.2", "@cel-tui/types": "0.8.2", "yoctomarkdown": "^0.1.0" }, "peerDependencies": { "typescript": ">=5.0.0" } }, "sha512-mc1CLy/EzPIpK8kxTw4CSH8XMKoXOcqTdoUx158wfFyGNO/rROEjs941Qo/D5k+VhodUlzFMj+PMzQQjrdgpeg=="],
120+ "@cel-tui/components": ["@cel-tui/components@0.8.3", "", { "dependencies": { "@cel-tui/clew": "0.8.3", "@cel-tui/core": "0.8.3", "@cel-tui/types": "0.8.3", "yoctomarkdown": "^0.1.0" }, "peerDependencies": { "typescript": ">=5.0.0" } }, "sha512-FF3WTw9XxapNabb205U0uqu6fu3uRCsbuc3pQr/yixa5/amTWsqrcMvIM8ndC7q3dEJsQk4D5rfyOMVNnmKfnA=="],
121121
122- "@cel-tui/core": ["@cel-tui/core@0.8.2", "", { "dependencies": { "@cel-tui/types": "0.8.2", "get-east-asian-width": "^1.5.0" }, "peerDependencies": { "typescript": ">=5.0.0" } }, "sha512-VMrPiqYHJKpqj1cCgUni8aM4etzG5y60tmsTBcDrhGisbS02GUpv6pmXp95U0O6VVLLYzmHtoXIwAyOTfWNRjA=="],
122+ "@cel-tui/core": ["@cel-tui/core@0.8.3", "", { "dependencies": { "@cel-tui/types": "0.8.3", "get-east-asian-width": "^1.5.0" }, "peerDependencies": { "typescript": ">=5.0.0" } }, "sha512-rC6nqza70HTvJGoY5cm86nwBMRnfim3g4Ysns+HebYkjwXv4a0VIcLmRNQFATNq4R/Xtg7+ba/qzsqJvTY22IA=="],
123123
124- "@cel-tui/types": ["@cel-tui/types@0.8.2", "", { "peerDependencies": { "typescript": ">=5.0.0" } }, "sha512-OwaX9IZHnJO0QJ9TtKx/9LhUG/E9lWds40fkq8kgLOxcb4DHBs2xRKCBoQKOh98zsitD5NLXl7125C6Dlbk+FA=="],
124+ "@cel-tui/types": ["@cel-tui/types@0.8.3", "", { "peerDependencies": { "typescript": ">=5.0.0" } }, "sha512-yEKXCY16U/W4A4JaVysv2/x/IBEHQAfb93CzL6HugN+N15ETB3Vn8tZrQwdyrOT/9WdlCQkeBH8Vj7BJHs4Hkw=="],
125125
126126 "@google/genai": ["@google/genai@1.50.1", "", { "dependencies": { "google-auth-library": "^10.3.0", "p-retry": "^4.6.2", "protobufjs": "^7.5.4", "ws": "^8.18.0" }, "peerDependencies": { "@modelcontextprotocol/sdk": "^1.25.2" }, "optionalPeers": ["@modelcontextprotocol/sdk"] }, "sha512-YbkX7H9+1Pt8wOt7DDREy8XSoiL6fRDzZQRyaVBarFf8MR3zHGqVdvM4cLbDXqPhxqvegZShgfxb8kw9C7YhAQ=="],
127127
package.json+3−3
@@ -9,7 +9,7 @@
99 "scripts": {
1010 "dev": "bun run bin/mc.ts",
1111 "check": "bun run lint && bun run typecheck",
12- "lint": "bun run biome check --write .",
12+ "lint": "bun run biome check .",
1313 "typecheck": "bun run tsc -p tsconfig.json --noEmit",
1414 "format": "bun run prettier --write *.md && bun run biome format --write . && bun run biome check --write ."
1515 },
@@ -20,8 +20,8 @@
2020 "typescript": "^6.0.3"
2121 },
2222 "dependencies": {
23- "@cel-tui/components": "^0.8.2",
24- "@cel-tui/core": "^0.8.2",
23+ "@cel-tui/components": "^0.8.3",
24+ "@cel-tui/core": "^0.8.3",
2525 "@mariozechner/pi-ai": "^0.70.2",
2626 "diff": "^9.0.0",
2727 "simple-git": "^3.36.0",
src/agent.ts+104−90
@@ -2,8 +2,6 @@ import { readdir } from "node:fs/promises";
22 import { homedir, platform } from "node:os";
33 import { join } from "node:path";
44 import {
5- type AssistantMessage,
6- type AssistantMessageEvent,
75 type Context,
86 type Message,
97 streamSimple,
@@ -12,7 +10,7 @@ import {
1210 } from "@mariozechner/pi-ai";
1311 import { type StatusResult, simpleGit } from "simple-git";
1412 import { parseSkillFrontmatter } from "./shared";
15-import type { CliOptions, ToolAndRunner } from "./types";
13+import type { AgentStreamEvent, CliOptions, ToolAndRunner } from "./types";
1614
1715 const IDENTITY_PROMPT = `# You are "mini-coder", an efficient, elite coding agent.
1816
@@ -26,6 +24,7 @@ const IDENTITY_PROMPT = `# You are "mini-coder", an efficient, elite coding agen
2624 - Be efficient, don't get lost with tangents or satisfying your curiosity, root yourself on the user request.
2725 - Narrate your edits with small commentary messages during long tasks.
2826 - Do not over-scope your work, or add more scope during implementation.
27+- Avoid over-enginnering, hacks or creative solutions. The boring, simple and repliable is always preffered.
2928 - Focus on the user's request requirements to answer accurately and efficiently.
3029 - Once you've gathered enough information to complete the request, stop exploring and complete it.
3130 - Ensure that you fulfill the user's expectation, requirements and contract **exactly**.
@@ -211,28 +210,26 @@ export function insertToolUsageReminder(
211210 const toolCalls: ToolCall[] = [];
212211 messages.forEach((m) => {
213212 if (m.role === "assistant") {
214- const toolCalls = m.content.filter((b) => b.type === "toolCall");
215- toolCalls.push(...toolCalls);
213+ const toolCallsBlocks = m.content.filter((b) => b.type === "toolCall");
214+ toolCalls.push(...toolCallsBlocks);
216215 }
217216 });
217+ const recentToolCalls = toolCalls.slice(-budget);
218+ const taskSeen = recentToolCalls.some((call) => call.name === "task");
218219
219- let taskSeen = 0;
220- let idx = toolCalls.length;
221-
222- while (idx--) {
223- const name = toolCalls[idx].name;
224- if (name === "task") taskSeen++;
225- if (idx > budget && taskSeen < 1) break;
226- }
227-
228- if (taskSeen < 1) {
220+ if (toolCalls.length >= budget && !taskSeen) {
229221 output = `# System reminder:
230222
231- > You are in a tool loop. Stop and use the task tool instead.
223+> You are in a shell or edit tool calling loop poluting your context.
232224
233- ---
225+- Remember your tool choice heuristic.
226+- Plan your next actions, and use the task tool to continue.
227+- If you have completed the user request, complete your answer.
228+
229+---
234230
235- ${output}`;
231+${output}
232+`;
236233 }
237234
238235 toolMessage.content = [{ type: "text", text: output }];
@@ -240,96 +237,113 @@ export function insertToolUsageReminder(
240237 return toolMessage;
241238 }
242239
243-export async function streamAgent(
240+export async function* streamAgent(
244241 apiKey: string,
245242 tools: ToolAndRunner[],
246243 systemPrompt: string,
247244 messages: Message[],
248245 options: CliOptions,
249246 abortController?: AbortController,
250- streamFn?: (ev: AssistantMessageEvent, context: Context) => void,
251- toolsFn?: (tool: ToolResultMessage, context: Context) => ToolResultMessage,
252- completeFn?: (msg: AssistantMessage, context: Context) => void,
253-) {
247+): AsyncGenerator<AgentStreamEvent> {
254248 const context: Context = {
255249 systemPrompt: await buildSystemPrompt(systemPrompt),
256250 messages,
257251 tools: tools.map((t) => t.tool),
258252 };
259253
260- let controller: AbortController;
261- const onAbort = () => {
262- controller?.abort();
263- };
264-
265- // Wire the global abort signal to each turn http requests
266- abortController?.signal.addEventListener("abort", onAbort);
254+ while (true) {
255+ const s = streamSimple(options.model, context, {
256+ apiKey,
257+ reasoning: options.effort,
258+ signal: abortController?.signal,
259+ });
267260
268- try {
269- while (true) {
270- controller = new AbortController();
271- const s = streamSimple(options.model, context, {
272- apiKey,
273- reasoning: options.effort,
274- signal: controller.signal,
275- });
276-
277- for await (const ev of s) {
278- streamFn?.(ev, context);
279- }
261+ for await (const ev of s) {
262+ yield { type: "assistant", event: ev, context };
263+ }
280264
281- const finalMessage = await s.result();
282- context.messages.push(finalMessage);
283-
284- // Check if the agent is done, or an error/abort happened before moving
285- // on towards tool calling.
286- if (
287- ["stop", "error", "aborted", "length"].includes(finalMessage.stopReason)
288- ) {
289- // Agent is done, send the final message, and current context.
290- completeFn?.(finalMessage, context);
291- break;
292- }
265+ const finalMessage = await s.result();
266+ context.messages.push(finalMessage);
267+
268+ // Check if the agent is done, or an error/abort happened before moving
269+ // on towards tool calling.
270+ if (
271+ ["stop", "error", "aborted", "length"].includes(finalMessage.stopReason)
272+ ) {
273+ // Agent is done, send the final message, and current context.
274+ yield { type: "complete", message: finalMessage, context };
275+ break;
276+ }
293277
294- // Call tolls and per tool result callback.
295- const toolCalls = finalMessage.content.filter(
296- (msg) => msg.type === "toolCall",
297- );
298-
299- for (const call of toolCalls) {
300- let output = "";
301- let isError = false;
302- const toolDef = tools.find((i) => i.tool.name === call.name);
303- if (toolDef) {
304- try {
305- output = await toolDef.runner(call.arguments);
306- } catch (err) {
307- const error = err instanceof Error ? err.message : "Unknown error";
308- output = `Tool call failed: ${error}`;
309- isError = true;
278+ // TODO: Extract into it's own function
279+ // Call tolls and per tool result callback.
280+ const toolCalls = finalMessage.content.filter(
281+ (msg) => msg.type === "toolCall",
282+ );
283+
284+ for (const call of toolCalls) {
285+ let output = "";
286+ let isError = false;
287+
288+ const toolDef = tools.find((item) => item.tool.name === call.name);
289+ let msg: ToolResultMessage = {
290+ role: "toolResult",
291+ toolCallId: call.id,
292+ toolName: call.name,
293+ content: [{ type: "text", text: output }],
294+ isError,
295+ timestamp: Date.now(),
296+ };
297+
298+ if (!toolDef) {
299+ output = `Unknown tool: ${call.name}`;
300+ isError = true;
301+ } else {
302+ try {
303+ for await (const event of toolDef.runner(call.arguments)) {
304+ switch (event.type) {
305+ case "output":
306+ output += event.text;
307+ msg = {
308+ ...msg,
309+ content: [{ type: "text", text: event.text }],
310+ };
311+ yield {
312+ type: "tool_output",
313+ message: msg,
314+ context,
315+ };
316+ break;
317+
318+ case "result":
319+ output = event.text;
320+ break;
321+ }
310322 }
311- } else {
312- output = `Unknown tool: ${call.name}`;
323+ } catch (err) {
324+ const error = err instanceof Error ? err.message : "Unknown error";
325+ output = `Tool call failed: ${error}`;
326+ isError = true;
313327 }
314-
315- let msg: ToolResultMessage = {
316- role: "toolResult",
317- toolCallId: call.id,
318- toolName: call.name,
319- content: [{ type: "text", text: output }],
320- isError,
321- timestamp: Date.now(),
322- };
323-
324- // Update with tool results. let the user make changes if they want
325- if (toolsFn) {
326- msg = toolsFn(msg, context);
327- }
328-
329- context.messages.push(msg);
330328 }
329+
330+ msg = {
331+ role: "toolResult",
332+ toolCallId: call.id,
333+ toolName: call.name,
334+ content: [{ type: "text", text: output }],
335+ isError,
336+ timestamp: Date.now(),
337+ };
338+
339+ // Update with tool results. let the user make changes if they want
340+ context.messages.push(msg);
341+
342+ yield {
343+ type: "tool_result",
344+ message: msg,
345+ context,
346+ };
331347 }
332- } finally {
333- abortController?.signal.removeEventListener("abort", onAbort);
334348 }
335349 }
src/headless.ts+21−5
@@ -94,17 +94,33 @@ export async function streamHeadless(
9494 log({ msg: "mini-coder headless" });
9595 log({ msg: "-------------------" });
9696
97- await streamAgent(
97+ for await (const event of streamAgent(
9898 apiKey,
9999 tools,
100100 MAIN_PROMPT,
101101 messages,
102102 options,
103103 undefined,
104- onStream,
105- onTool,
106- onComplete,
107- );
104+ )) {
105+ switch (event.type) {
106+ case "assistant":
107+ onStream(event.event);
108+ break;
109+
110+ case "tool_output":
111+ // TODO:
112+ // onToolOutput(event);
113+ break;
114+
115+ case "tool_result":
116+ onTool(event.message);
117+ break;
118+
119+ case "complete":
120+ onComplete(event.message);
121+ break;
122+ }
123+ }
108124
109125 leave("Done.");
110126 }
src/tool-bash.ts+46−20
@@ -1,5 +1,6 @@
11 import { type Tool, Type } from "@mariozechner/pi-ai";
22 import { secureRandomString } from "./shared";
3+import type { ToolRunnerEvent } from "./types";
34
45 const OUTPUT_THRESHOLD = 16000;
56 const description = `## Bash CLI tool
@@ -35,48 +36,73 @@ export const bash: Tool = {
3536 }),
3637 };
3738
38-export async function runBashTool(args: Record<string, any>) {
39- const proc = Bun.spawn(["bash", "-c", args.command], {
39+export async function* runBashTool(
40+ args: Record<string, any>,
41+ signal?: AbortSignal,
42+): AsyncGenerator<ToolRunnerEvent> {
43+ // Redirect stderr into stdout for the whole shell session.
44+ const proc = Bun.spawn(["bash", "-c", `exec 2>&1; ${args.command}`], {
4045 stdout: "pipe",
4146 stderr: "pipe",
4247 env: {
4348 ...Bun.env,
4449 NO_COLOR: "1",
4550 },
51+ signal,
4652 });
4753
48- const [stdout, stderr, exitCode] = await Promise.all([
49- proc.stdout.text(),
50- proc.stderr.text(),
51- proc.exited,
52- ]);
54+ const decoder = new TextDecoder();
55+ const reader = proc.stdout.getReader();
5356
54- let out = `# EXIT CODE: ${exitCode}`;
55- if (stderr.length) {
56- out += `
57-# STDERR:
57+ let output = "";
58+ while (true) {
59+ const { done, value } = await reader.read();
5860
59-${Bun.stripANSI(stderr)}`;
61+ if (done) {
62+ const remaining = Bun.stripANSI(decoder.decode());
63+
64+ if (remaining.length) {
65+ output += remaining;
66+ yield { type: "output", text: remaining };
67+ }
68+ break;
69+ }
70+
71+ const text = Bun.stripANSI(decoder.decode(value, { stream: true }));
72+
73+ if (text.length) {
74+ output += text;
75+ yield { type: "output", text: text };
76+ }
6077 }
61- if (stdout.length) {
62- out += `
63-# STDOUT:
6478
65-${Bun.stripANSI(stdout)}`;
79+ const exitCode = await proc.exited;
80+
81+ let result = `# EXIT CODE: ${exitCode}`;
82+ if (output.length) {
83+ result += `
84+# OUTPUT:
85+
86+${output}`;
6687 }
6788
6889 // If `out` is too big, more than ~XXKB, write it to a temp file
6990 // And add that to the truncation label for the agent to be able
7091 // to continue the read with scans. This is to protect context,
7192 // not a general read guard. The hint is for the agent, not the TUI
72- if (out.length > OUTPUT_THRESHOLD) {
93+ if (result.length > OUTPUT_THRESHOLD) {
7394 const key = `${Date.now()}-${secureRandomString(4)}`;
7495 const pathname = `/tmp/bash_result_${key}.txt`;
75- await Bun.write(pathname, out);
76- out = `${out.substring(0, OUTPUT_THRESHOLD)}
96+ await Bun.write(pathname, result);
97+ result = `${result.substring(0, OUTPUT_THRESHOLD)}
7798
7899 Truncated at ~${OUTPUT_THRESHOLD / 1000}KB. Full output at ${pathname}`;
79100 }
80101
81- return out;
102+ yield {
103+ type: "result",
104+ text: result,
105+ };
106+
107+ return result;
82108 }
src/tool-edit.ts+32−7
@@ -1,6 +1,7 @@
11 import { isAbsolute, join } from "node:path";
22 import { type Tool, Type } from "@mariozechner/pi-ai";
33 import { createPatch } from "diff";
4+import type { ToolRunnerEvent } from "./types";
45
56 const description = `## Edit tool
67
@@ -41,7 +42,10 @@ function findAllIndexes(text: string, sub: string): number[] {
4142 return indexes;
4243 }
4344
44-export async function runEditTool(args: Record<string, any>) {
45+export async function* runEditTool(
46+ args: Record<string, any>,
47+ signal?: AbortSignal,
48+): AsyncGenerator<ToolRunnerEvent> {
4549 const filePath = isAbsolute(args.path)
4650 ? args.path
4751 : join(process.cwd(), args.path);
@@ -50,26 +54,37 @@ export async function runEditTool(args: Record<string, any>) {
5054
5155 if (args.oldText === "") {
5256 if (exists) {
53- return `File already exists: ${filePath}`;
57+ yield { type: "result", text: `File already exists: ${filePath}` };
58+ return;
5459 }
5560
5661 await Bun.write(file, args.newText);
57- return `File written: ${filePath}\n\n${args.newText}`;
62+ yield {
63+ type: "result",
64+ text: `File written: ${filePath}\n\n${args.newText}`,
65+ };
66+ return;
5867 }
5968
6069 if (!exists) {
61- return `File not found: ${filePath}`;
70+ yield { type: "result", text: `File not found: ${filePath}` };
71+ return;
6272 }
6373
6474 const content = await file.text();
6575 const matches = findAllIndexes(content, args.oldText);
6676
6777 if (matches.length === 0) {
68- return `Old text not found in: ${filePath}`;
78+ yield { type: "result", text: `Old text not found in: ${filePath}` };
79+ return;
6980 }
7081
7182 if (matches.length > 1) {
72- return `Multiple matches found in ${filePath}: ${matches.length} matches, be more specific and try again`;
83+ yield {
84+ type: "result",
85+ text: `Multiple matches found in ${filePath}: ${matches.length} matches, be more specific and try again`,
86+ };
87+ return;
7388 }
7489
7590 const idx = matches[0];
@@ -78,8 +93,18 @@ export async function runEditTool(args: Record<string, any>) {
7893 args.newText +
7994 content.slice(idx + args.oldText.length);
8095
96+ if (signal?.aborted) {
97+ yield { type: "result", text: "Aborted before write." };
98+ return;
99+ }
100+
81101 await file.write(updated);
82102 const patch = createPatch(filePath, content, updated);
83103
84- return `File edited: ${filePath}\n\n${patch}`;
104+ yield {
105+ type: "result",
106+ text: `File edited: ${filePath}\n\n${patch}`,
107+ };
108+
109+ return;
85110 }
src/tool-task.ts+41−24
@@ -1,5 +1,4 @@
11 import {
2- type AssistantMessage,
32 type Message,
43 type ThinkingLevel,
54 type Tool,
@@ -7,9 +6,10 @@ import {
76 } from "@mariozechner/pi-ai";
87 import { streamAgent, TASK_PROMPT } from "./agent";
98 import { getApiKey } from "./oauth";
9+import { elapsedTime, estimateTokens } from "./shared";
1010 import { bash, runBashTool } from "./tool-bash";
1111 import { edit, runEditTool } from "./tool-edit";
12-import type { CliOptions, ToolAndRunner } from "./types";
12+import type { CliOptions, ToolAndRunner, ToolRunnerEvent } from "./types";
1313
1414 const description = `## Task tool
1515
@@ -37,21 +37,27 @@ export const task: Tool = {
3737 }),
3838 };
3939
40-export async function runTaskTool(
40+export async function* runTaskTool(
4141 options: CliOptions & { abortController?: AbortController },
4242 args: Record<string, any>,
43-) {
43+): AsyncGenerator<ToolRunnerEvent> {
4444 const apiKey = await getApiKey(options);
4545 const tools: ToolAndRunner[] = [
46- { tool: bash, runner: runBashTool },
47- { tool: edit, runner: runEditTool },
46+ {
47+ tool: bash,
48+ runner: (args) => runBashTool(args, options.abortController?.signal),
49+ },
50+ {
51+ tool: edit,
52+ runner: (args) => runEditTool(args, options.abortController?.signal),
53+ },
4854 ];
4955 const messages: Message[] = [
5056 { role: "user", content: args.prompt, timestamp: Date.now() },
5157 ];
5258
5359 // For custom effort for speed and less surprises. Consider even using a `taskModel` config value.
54- // Also remove the piggy backed abortController.
60+ // Also remove the piggy backed abortController
5561 const taskOptions = {
5662 ...options,
5763 effort: "medium" as ThinkingLevel,
@@ -59,44 +65,55 @@ export async function runTaskTool(
5965 };
6066
6167 let output = "";
62- const onComplete = (ev: AssistantMessage) => {
63- output = ev.content
64- .filter((b) => b.type === "text")
65- .map((b) => b.text)
66- .join("\n");
67-
68- if (["stop", "error", "aborted"].includes(ev.stopReason)) {
69- if (ev.errorMessage)
70- output += `\n\nStopped: ${ev.stopReason}, message:\n${ev.errorMessage}`;
71- }
72- };
7368
7469 // We actually wrap this in case of exceptions, this breaks the let exceptions bubble
7570 // overall model at first glance, but it makes sense when you look at this like other
7671 // tools, if a tool fails it doesn't break the top level conversation, tool failures
7772 // are common.
7873 try {
79- await streamAgent(
74+ for await (const ev of streamAgent(
8075 apiKey,
8176 tools,
8277 TASK_PROMPT,
8378 messages,
8479 taskOptions,
8580 options.abortController,
86- undefined,
87- undefined,
88- onComplete,
89- );
81+ )) {
82+ if (ev.type === "assistant" && ev.event.type === "text_end") {
83+ output += ev.event.content;
84+ yield { type: "output", text: ev.event.content };
85+ }
86+
87+ if (ev.type === "tool_result") {
88+ const estimate = estimateTokens(JSON.stringify(ev.message.content))
89+ const elapsed = elapsedTime(Date.now() - ev.message.timestamp)
90+ const text = `Called ${ev.message.toolName}. ~(${estimate} tokens). ${elapsed} ago`;
91+ output += text;
92+ yield {
93+ type: "output",
94+ text,
95+ };
96+ }
97+
98+ if (ev.type === "complete") {
99+ if (["stop", "error", "aborted"].includes(ev.message.stopReason)) {
100+ if (ev.message.errorMessage)
101+ output += `\n\nStopped: ${ev.message.stopReason}, message:\n${ev.message.errorMessage}`;
102+ }
103+ }
104+ }
90105 } catch (err) {
91106 let errorText = "Unknown error.";
92107 if (err instanceof Error) {
93108 errorText = `Stopped: ${err.message}\n\n${err.stack ?? ""}`;
94109 }
95110
111+ yield { type: "result", text: `${output}\n\n${errorText}` };
112+
96113 return errorText;
97114 }
98115
99116 // TODO: Structured footer for output and edit diffs.
100-
117+ yield { type: "result", text: output };
101118 return output.trim();
102119 }
src/tui-components.ts+46−1
@@ -1,4 +1,4 @@
1-import { type Color, HStack, Text } from "@cel-tui/core";
1+import { type Color, HStack, type Node, Text } from "@cel-tui/core";
22 import { onceEvery } from "./shared";
33 import type { TUIState } from "./types";
44
@@ -28,6 +28,51 @@ export const theme = {
2828 bwhite: "color15" as Color,
2929 };
3030
31+// Momoization helper for cel-tui
32+type NodeMemoKey = string | number | symbol;
33+
34+type MemoizedNodeRenderer<T> = ((value: T) => Node) & {
35+ clear: () => void;
36+ delete: (value: T) => boolean;
37+};
38+
39+export function memoNodeByKey<T>(
40+ keyOf: (value: T) => NodeMemoKey,
41+ render: (value: T) => Node,
42+ options: { maxEntries?: number } = {},
43+): MemoizedNodeRenderer<T> {
44+ const maxEntries = options.maxEntries ?? 1000;
45+ const cache = new Map<NodeMemoKey, Node>();
46+
47+ const memoized = ((value: T): Node => {
48+ const key = keyOf(value);
49+ const cached = cache.get(key);
50+
51+ if (cached) {
52+ // Move to the end: simple LRU-ish behavior.
53+ cache.delete(key);
54+ cache.set(key, cached);
55+ return cached;
56+ }
57+
58+ const node = render(value);
59+ cache.set(key, node);
60+
61+ while (cache.size > maxEntries) {
62+ const oldest = cache.keys().next();
63+ if (oldest.done) break;
64+ cache.delete(oldest.value);
65+ }
66+
67+ return node;
68+ }) as MemoizedNodeRenderer<T>;
69+
70+ memoized.clear = () => cache.clear();
71+ memoized.delete = (value: T) => cache.delete(keyOf(value));
72+
73+ return memoized;
74+}
75+
3176 // TextPill("mini-coder", theme.bblack, theme.black),
3277 // TextPill("mini-coder", theme.bred, theme.red),
3378 // TextPill("mini-coder", theme.bgreen, theme.green),
src/tui-conversation.ts+23−28
@@ -20,13 +20,10 @@ function agentMessageNode(msg: AssistantMessage): Node {
2020
2121 const tokens = estimateTokens(block.thinking);
2222
23- return HStack({}, [
24- HStack({ width: 4 }, []),
25- Text(`Thinking... (~${tokens} tokens)`, {
26- fgColor: theme.bblack,
27- italic: true,
28- }),
29- ]);
23+ return Text(`Thinking... (~${tokens} tokens)`, {
24+ fgColor: theme.bblack,
25+ italic: true,
26+ });
3027 }
3128
3229 let text = "";
@@ -41,7 +38,8 @@ function agentMessageNode(msg: AssistantMessage): Node {
4138 text = block.arguments.prompt;
4239 node = SyntaxHighlight(text, "markdown");
4340 } else {
44- node = Text("Unknown tool");
41+ text = JSON.stringify(block.arguments);
42+ node = Text(text);
4543 }
4644
4745 return VStack({ padding: { x: 4 } }, [
@@ -61,7 +59,7 @@ function userMessageNode(msg: UserMessage): Node {
6159 text = msg.content
6260 .filter((b) => b.type === "text")
6361 .map((b) => b.text)
64- .join("\n");
62+ .join("");
6563 }
6664
6765 return VStack(
@@ -75,25 +73,22 @@ function userMessageNode(msg: UserMessage): Node {
7573 }
7674
7775 function toolMessageNode(msg: ToolResultMessage): Node {
78- let textBlocks = msg.content
79- .filter((b) => b.type === "text")
80- .map((b) => b.text)
81- .join("\n");
82-
83- const newlines = textBlocks.split("\n");
84- const max = 4;
85- if (newlines.length > max) {
86- textBlocks = `...\n${newlines.slice(-max).join("\n")}`;
87- }
88-
89- const charsPerLine = 100;
90- if (textBlocks.length > max * charsPerLine) {
91- textBlocks = `...${textBlocks.slice(-(max * charsPerLine))}`;
92- }
93-
94- return VStack({ gap: 1 }, [
95- Text(textBlocks, { wrap: "word", fgColor: theme.bblack }),
96- ]);
76+ // Output only shows last 10 lines of scroll.
77+ return VStack(
78+ {
79+ height: 10,
80+ padding: { x:4 },
81+ overflow: "scroll",
82+ scrollOffset: Infinity,
83+ onScroll: () => false,
84+ },
85+ msg.content.map((block) => {
86+ if (block.type === "text") {
87+ return Text(block.text, { wrap: "word", fgColor: theme.bblack });
88+ }
89+ return Text(""); // TODO: image case needs attention
90+ }),
91+ );
9792 }
9893
9994 function conversationMessageNode(msg: Message): Node {
src/tui.ts+64−12
@@ -119,13 +119,18 @@ export async function streamTUI(state: TUIState) {
119119
120120 const apiKey = await getApiKey(state.options);
121121 const tools: ToolAndRunner[] = [
122- { tool: bash, runner: runBashTool },
123- { tool: edit, runner: runEditTool },
122+ {
123+ tool: bash,
124+ runner: (args) => runBashTool(args, state.abortController?.signal),
125+ },
126+ {
127+ tool: edit,
128+ runner: (args) => runEditTool(args, state.abortController?.signal),
129+ },
124130 {
125131 tool: task,
126- runner: async (args: Record<string, any>) => {
127- return runTaskTool({ ...state.options, abortController }, args);
128- },
132+ runner: (args) =>
133+ runTaskTool({ ...state.options, abortController }, args),
129134 },
130135 ];
131136
@@ -168,11 +173,45 @@ export async function streamTUI(state: TUIState) {
168173 }
169174 };
170175
176+ const onToolOutput = (msg: ToolResultMessage, ctx: Context) => {
177+ const existingIdx = state.messages.findIndex(
178+ (m) => m.role === "toolResult" && msg.toolCallId === m.toolCallId,
179+ );
180+ const existing = state.messages[existingIdx];
181+
182+ if (!existing) {
183+ state.messages.push(msg);
184+ } else {
185+ state.messages[existingIdx] = {
186+ ...existing,
187+ content: [...existing.content, ...msg.content],
188+ isError: msg.isError,
189+ } as ToolResultMessage;
190+ }
191+
192+ state.contextSize = estimateTokens(JSON.stringify(ctx));
193+ };
194+
171195 const onTool = (tool: ToolResultMessage, ctx: Context) => {
196+ const existingIdx = state.messages.findIndex(
197+ (m) => m.role === "toolResult" && tool.toolCallId === m.toolCallId,
198+ );
199+ const existing = state.messages[existingIdx];
200+
172201 tool = insertToolUsageReminder(state.messages, tool);
173- state.messages.push(tool);
202+ ctx.messages[ctx.messages.length - 1] = tool;
203+
204+ if (!existing) {
205+ state.messages.push(tool);
206+ } else {
207+ state.messages[existingIdx] = {
208+ ...existing,
209+ content: tool.content,
210+ isError: tool.isError,
211+ } as ToolResultMessage;
212+ }
213+
174214 state.contextSize = estimateTokens(JSON.stringify(ctx));
175- return tool;
176215 };
177216
178217 const onComplete = (msg: AssistantMessage, ctx: Context) => {
@@ -185,17 +224,30 @@ export async function streamTUI(state: TUIState) {
185224 };
186225
187226 try {
188- await streamAgent(
227+ for await (const ev of streamAgent(
189228 apiKey,
190229 tools,
191230 MAIN_PROMPT,
192231 existingMessages,
193232 state.options,
194233 state.abortController,
195- onStream,
196- onTool,
197- onComplete,
198- );
234+ )) {
235+ switch (ev.type) {
236+ case "assistant":
237+ onStream(ev.event, ev.context);
238+ break;
239+ case "tool_output":
240+ // TODO:
241+ onToolOutput(ev.message, ev.context);
242+ break;
243+ case "tool_result":
244+ onTool(ev.message, ev.context);
245+ break;
246+ case "complete":
247+ onComplete(ev.message, ev.context);
248+ break;
249+ }
250+ }
199251 } finally {
200252 state.streaming = false;
201253 }
src/types.ts+37−2
@@ -1,4 +1,13 @@
1-import type { Message, Model, ThinkingLevel, Tool } from "@mariozechner/pi-ai";
1+import type {
2+ AssistantMessage,
3+ AssistantMessageEvent,
4+ Context,
5+ Message,
6+ Model,
7+ ThinkingLevel,
8+ Tool,
9+ ToolResultMessage,
10+} from "@mariozechner/pi-ai";
211 import type { OAuthCredentials } from "@mariozechner/pi-ai/oauth";
312
413 export type CliOptions = {
@@ -25,7 +34,33 @@ export type SavedOAuthAuth = OAuthCredentials & {
2534
2635 export type SavedOAuthCreds = Record<string, SavedOAuthAuth>;
2736
37+export type AgentStreamEvent =
38+ | {
39+ type: "assistant";
40+ event: AssistantMessageEvent;
41+ context: Context;
42+ }
43+ | {
44+ type: "tool_output";
45+ message: ToolResultMessage;
46+ context: Context;
47+ }
48+ | {
49+ type: "tool_result";
50+ message: ToolResultMessage;
51+ context: Context;
52+ }
53+ | {
54+ type: "complete";
55+ message: AssistantMessage;
56+ context: Context;
57+ };
58+
59+export type ToolRunnerEvent =
60+ | { type: "output"; text: string }
61+ | { type: "result"; text: string };
62+
2863 export type ToolAndRunner = {
2964 tool: Tool;
30- runner: (args: Record<string, any>) => string | Promise<string>;
65+ runner: (args: Record<string, any>) => AsyncGenerator<ToolRunnerEvent>;
3166 };