@@ -3,7 +3,7 @@ import { homedir } from "node:os";
33 import * as c from "yoctocolors";
44 import type { CoreMessage } from "../llm-api/turn.ts";
55 import type { TurnEvent } from "../llm-api/types.ts";
6-import type { SubagentOutput, SubagentToolEntry } from "../tools/subagent.ts";
6+import type { SubagentOutput } from "../tools/subagent.ts";
77 import { renderChunk } from "./markdown.ts";
88
99 const HOME = homedir();
@@ -305,12 +305,7 @@ function renderToolResultInline(
305305 }
306306
307307 if (toolName === "subagent") {
308- const r = result as { inputTokens?: number; outputTokens?: number };
309- if (r.inputTokens || r.outputTokens) {
310- writeln(
311- `${indent}${G.ok} ${c.dim(`↑${r.inputTokens ?? 0} ↓${r.outputTokens ?? 0}`)}`,
312- );
313- }
308+ // Activity and token counts are now streamed live.
314309 return;
315310 }
316311
@@ -334,32 +329,68 @@ function renderToolResultInline(
334329 );
335330 }
336331
337-/** Render a subagent's activity tree, indented under its parent tool-call line. */
338-export function renderSubagentActivity(
339- activity: SubagentToolEntry[],
332+export function formatSubagentLabel(
333+ laneId: number,
334+ parentLabel?: string,
335+): string {
336+ const numStr = parentLabel
337+ ? `${parentLabel.replace(/[\[\]]/g, "")}.${laneId}`
338+ : `${laneId}`;
339+ return c.dim(c.cyan(`[${numStr}]`));
340+}
340341
341- indent: string,
342- maxDepth: number,
342+const laneBuffers = new Map<number, string>();
343+
344+export function renderSubagentEvent(
345+ event: TurnEvent,
346+ opts: {
347+ laneId: number;
348+ parentLabel?: string | undefined;
349+ activeLanes: Set<number>;
350+ },
343351 ): void {
344- for (const entry of activity) {
345- writeln(`${indent}${toolCallLine(entry.toolName, entry.args)}`);
346-
347- // For nested subagents, recurse one more level (capped at maxDepth)
348- if (entry.toolName === "subagent" && maxDepth > 0) {
349- const nested = entry.result as {
350- activity?: SubagentToolEntry[];
351- };
352- if (nested?.activity?.length) {
353- renderSubagentActivity(nested.activity, `${indent} `, maxDepth - 1);
352+ const { laneId, parentLabel, activeLanes } = opts;
353+
354+ const labelStr = formatSubagentLabel(laneId, parentLabel);
355+ const prefix = activeLanes.size > 1 ? `${labelStr} ` : "";
356+
357+ if (event.type === "text-delta") {
358+ const buf = (laneBuffers.get(laneId) ?? "") + event.delta;
359+ const lines = buf.split("\n");
360+ if (lines.length > 1) {
361+ for (let i = 0; i < lines.length - 1; i++) {
362+ writeln(`${prefix}${lines[i]}`);
354363 }
364+ laneBuffers.set(laneId, lines[lines.length - 1] ?? "");
365+ } else {
366+ laneBuffers.set(laneId, buf);
355367 }
356-
368+ } else if (event.type === "tool-call-start") {
369+ writeln(`${prefix}${toolCallLine(event.toolName, event.args)}`);
370+ } else if (event.type === "tool-result") {
371+ // Use inlineResultSummary logic directly here
357372 renderToolResultInline(
358- entry.toolName,
359- entry.result,
360- entry.isError,
361- `${indent} `,
373+ event.toolName,
374+ event.result,
375+ event.isError,
376+ `${prefix} `,
362377 );
378+ } else if (event.type === "turn-complete") {
379+ // Flush buffer if any
380+ const buf = laneBuffers.get(laneId);
381+ if (buf) {
382+ writeln(`${prefix}${buf}`);
383+ laneBuffers.delete(laneId);
384+ }
385+ if (event.inputTokens > 0 || event.outputTokens > 0) {
386+ writeln(
387+ `${prefix}${c.dim(`↑${event.inputTokens} ↓${event.outputTokens}`)}`,
388+ );
389+ }
390+ } else if (event.type === "turn-error") {
391+ laneBuffers.delete(laneId);
392+ const msg = event.error.message.split("\n")[0] ?? event.error.message;
393+ writeln(`${prefix}${G.err} ${c.red(msg)}`);
363394 }
364395 }
365396
@@ -517,23 +548,7 @@ export function renderToolResult(
517548 }
518549
519550 if (toolName === "subagent") {
520- const r = result as SubagentOutput;
521-
522- // Render the tool call tree before showing the result text
523- if (r.activity?.length) {
524- renderSubagentActivity(r.activity, " ", 1);
525- }
526-
527- if (r.result) {
528- const lines = r.result.split("\n");
529- const preview = lines.slice(0, 8);
530- for (const line of preview) writeln(` ${c.dim("│")} ${line}`);
531- if (lines.length > 8)
532- writeln(` ${c.dim(`│ … +${lines.length - 8} lines`)}`);
533- }
534- if (r.inputTokens || r.outputTokens) {
535- writeln(` ${c.dim(`↑${r.inputTokens ?? 0} ↓${r.outputTokens ?? 0}`)}`);
536- }
551+ // Activity and tokens are streamed live.
537552 return;
538553 }
539554