@@ -5,7 +5,7 @@ import { Terminal, expandTabs, sanitize, wrapLine, type Key } from "./term.ts";
55 import { Editor } from "./editor.ts";
66 import { completeCommand, findCommand, type CommandContext } from "./commands.ts";
77 import { MarkdownStream, TailStream, type BodyLine, type StreamRenderer } from "./stream.ts";
8-import { cyan, dim, green, red } from "./styles.ts";
8+import { blue, cyan, dim, green, red, teal } from "./styles.ts";
99 import { DIFF_ADD, DIFF_DELETE, NORMAL_BG, sgrBg, sgrPlain } from "./theme.ts";
1010 import { contextUsageLine, estimateContextTokens } from "./usage.ts";
1111
@@ -24,9 +24,17 @@ const ELIDED_TAIL = 4;
2424
2525 const BODY_PREFIX = " | ";
2626 const ERROR_PREFIX = " ! ";
27+/** The chrome prefix, styled: dim for output, red for errors; text stays `Normal`. */
28+const BODY_CHROME = dim(BODY_PREFIX);
29+const ERROR_CHROME = red(ERROR_PREFIX);
2730 const EXIT_LINE = /^exit code: (.+)$/;
2831 const EDIT_HEADER = /^(Index: |={3,}$|--- |\+\+\+ )/;
2932
33+/** The `-> <name>` head of a call line, in the tool accent; the args stay `Normal`. */
34+function callHead(name: string): string {
35+ return teal(`-> ${name}`);
36+}
37+
3038 function callSummary(name: string, args: JsonObject): string {
3139 if (name === "bash" && typeof args.command === "string") return args.command.replace(/\s*\n\s*/g, " ");
3240 if ((name === "edit" || name === "read") && typeof args.path === "string") return args.path;
@@ -55,7 +63,7 @@ function resultLines(name: string, text: string, isError: boolean): BodyLine[] {
5563 const exit = EXIT_LINE.exec(lines[lines.length - 1]);
5664 if (exit !== null) {
5765 lines.pop();
58- if (isError) lines.push(`exit ${exit[1]}`);
66+ if (isError) lines.push(red(`exit ${exit[1]}`));
5967 }
6068 } else if (name === "edit" && /^(edited|created) /.test(lines[0])) {
6169 lines.shift();
@@ -108,7 +116,7 @@ function bodyRows(lines: BodyLine[], width: number): string[] {
108116 const hidden = rows.length - ELIDED_HEAD - ELIDED_TAIL;
109117 return [
110118 ...rows.slice(0, ELIDED_HEAD),
111- `... ${hidden} lines not shown ...`,
119+ dim(`... ${hidden} lines not shown ...`),
112120 ...rows.slice(rows.length - ELIDED_TAIL),
113121 ];
114122 }
@@ -180,6 +188,7 @@ class Tui {
180188 private resolveExit: () => void = () => {};
181189 private phase: Phase = "idle";
182190 private detail: string | undefined;
191+ private writingTool: string | undefined;
183192 private active = false;
184193 private paused = false;
185194 private pauseRequested = false;
@@ -197,7 +206,6 @@ class Tui {
197206 // In-flight stream state, never persisted: all display-only.
198207 private readonly reply: StreamRenderer = new MarkdownStream();
199208 private readonly activity: StreamRenderer = new TailStream();
200- private pendingCalls: string[] = [];
201209 private streamed = "";
202210 private turnStart = 0;
203211 private frame = 0;
@@ -325,7 +333,7 @@ class Tui {
325333 });
326334 } catch (error) {
327335 this.separator = true;
328- this.push(`! ${(error as Error).message}`);
336+ this.push(red(`! ${(error as Error).message}`));
329337 } finally {
330338 this.active = false;
331339 this.abort = null;
@@ -370,12 +378,15 @@ class Tui {
370378 if (this.reply.pending().length === 0) this.activity.feed(event.delta);
371379 break;
372380 case "toolCall":
373- // Flush first so scrollback order matches execution order, then hold the
374- // call line until its result arrives: a message may carry several calls,
375- // all announced before any of them runs.
381+ // Flush first so scrollback order matches execution order, then commit
382+ // the call line: results land under the calls, in execution order.
383+ this.writingTool = undefined;
376384 this.commitLines(this.reply.flush());
377385 this.activity.reset();
378- this.pendingCalls.push(`-> ${event.name} ${callSummary(event.name, event.arguments)}`);
386+ this.commitCall(event.name, event.arguments);
387+ break;
388+ case "toolCallStart":
389+ this.writingTool = event.name;
379390 break;
380391 case "toolOutput":
381392 this.activity.feed(event.chunk);
@@ -388,10 +399,10 @@ class Tui {
388399 this.commitToolResult(event.name, event.text, event.isError);
389400 break;
390401 case "error":
391- this.endTurn(`! ${event.message}`);
402+ this.endTurn(red(`! ${event.message}`));
392403 break;
393404 case "cancelled":
394- this.endTurn("! cancelled");
405+ this.endTurn(red("! cancelled"));
395406 break;
396407 case "complete":
397408 this.endTurn(dim(`[complete · ${this.elapsed()}s]`));
@@ -404,10 +415,10 @@ class Tui {
404415 private endTurn(line: string): void {
405416 this.phase = "idle";
406417 this.detail = undefined;
418+ this.writingTool = undefined;
407419 this.paused = false;
408420 this.activity.reset();
409421 this.commitLines(this.reply.flush());
410- this.flushCalls();
411422 this.separator = true;
412423 this.push(line);
413424 }
@@ -418,8 +429,14 @@ class Tui {
418429
419430 private commitUser(text: string): void {
420431 this.separator = true;
421- this.commitLines(text.split("\n").map((line) => ({ text: `> ${line}` })));
432+ this.commitLines(text.split("\n").map((line) => ({ text: line, style: blue })));
433+ this.separator = true;
434+ }
435+
436+ /** The call line lands the moment the model finishes writing it, never held. */
437+ private commitCall(name: string, args: JsonObject): void {
422438 this.separator = true;
439+ this.commitLines([{ text: `${callHead(name)} ${callSummary(name, args)}` }]);
423440 }
424441
425442 /** Commits a line the model emitted without streaming it, plus the in-flight tail. */
@@ -438,29 +455,17 @@ class Tui {
438455 }
439456
440457 private commitToolResult(name: string, text: string, isError: boolean): void {
441- const call = this.pendingCalls.shift();
442- this.separator = true;
443- if (call !== undefined) this.push(call);
444458 const width = Math.max(1, this.term.width - BODY_PREFIX.length);
445459 const lines = resultLines(name, text, isError);
446460 // Diffs are shown in full; other tool bodies stay elided.
447461 const rows = name === "edit" ? renderRows(lines, width) : bodyRows(lines, width);
448462 for (let i = 0; i < rows.length; i++) {
449- const prefix = isError && i === rows.length - 1 ? ERROR_PREFIX : BODY_PREFIX;
463+ const prefix = isError && i === rows.length - 1 ? ERROR_CHROME : BODY_CHROME;
450464 this.push(prefix + rows[i]);
451465 }
452466 this.separator = true;
453467 }
454468
455- /** Commits any call line whose result never arrived, e.g. after a cancel. */
456- private flushCalls(): void {
457- for (const call of this.pendingCalls) {
458- this.separator = true;
459- this.push(call);
460- }
461- this.pendingCalls = [];
462- }
463-
464469 /** Commits logical lines through the same wrap-and-style step tool bodies use. */
465470 private commitLines(lines: BodyLine[]): void {
466471 for (const row of renderRows(lines, Math.max(1, this.term.width))) this.push(row);
@@ -517,7 +522,9 @@ class Tui {
517522 : this.phase === "waitingModel"
518523 ? "waiting for provider"
519524 : this.phase === "streaming"
520- ? "streaming"
525+ ? this.writingTool !== undefined
526+ ? `writing ${this.writingTool}`
527+ : "streaming"
521528 : `running ${this.detail ?? "tool"}`;
522529 return `${dim(`${SPINNER[this.frame % SPINNER.length]} ${label} · ${this.elapsed()}s`)} · ${usage}`;
523530 }