xonecas.io
64875
Mc

mini-coder / commit 216fefc

refactor(cli): split tool result rendering into focused modules

xonecas2026-03-14 216fefc33b90f05299f3004c9bfad029ac05ad3d parent f6f685f

4 files changed

src/cli/tool-render-result.test.ts+73−0
@@ -0,0 +1,73 @@
1+import { afterEach, describe, expect, test } from "bun:test";
2+import { terminal } from "./terminal-io.ts";
3+import { renderToolResult } from "./tool-render-result.ts";
4+
5+const originalStdoutWrite = terminal.stdoutWrite.bind(terminal);
6+let stdout = "";
7+
8+function stripAnsi(input: string): string {
9+ const esc = String.fromCharCode(0x1b);
10+ return input.replace(new RegExp(`${esc}\\[[0-9;]*m`, "g"), "");
11+}
12+
13+afterEach(() => {
14+ stdout = "";
15+ terminal.stdoutWrite = originalStdoutWrite;
16+});
17+
18+describe("renderToolResult", () => {
19+ test("renders compact shell summary with stdout and stderr previews", () => {
20+ terminal.stdoutWrite = (chunk: string) => {
21+ stdout += chunk;
22+ };
23+
24+ renderToolResult(
25+ "shell",
26+ {
27+ stdout: "line 1\nline 2",
28+ stderr: "err 1",
29+ exitCode: 0,
30+ success: true,
31+ timedOut: false,
32+ },
33+ false,
34+ );
35+
36+ const plain = stripAnsi(stdout);
37+ expect(plain).toContain("exit 0 · stdout 2L · stderr 1L");
38+ expect(plain).toContain("stdout (2 lines)");
39+ expect(plain).toContain("stderr (1 lines)");
40+ });
41+
42+ test("renders file edit diff for create", () => {
43+ terminal.stdoutWrite = (chunk: string) => {
44+ stdout += chunk;
45+ };
46+
47+ renderToolResult(
48+ "create",
49+ {
50+ path: "src/new.ts",
51+ created: true,
52+ diff: "@@ -0,0 +1 @@\n+console.log('ok')",
53+ },
54+ false,
55+ );
56+
57+ const plain = stripAnsi(stdout);
58+ expect(plain).toContain("created src/new.ts");
59+ expect(plain).toContain("@@ -0,0 +1 @@");
60+ expect(plain).toContain("+console.log('ok')");
61+ });
62+
63+ test("renders one-line error when tool result fails", () => {
64+ terminal.stdoutWrite = (chunk: string) => {
65+ stdout += chunk;
66+ };
67+
68+ renderToolResult("read", "boom\ndetails", true);
69+ const plain = stripAnsi(stdout);
70+ expect(plain).toContain("✖ boom");
71+ expect(plain).not.toContain("details");
72+ });
73+});
src/cli/tool-render-result.ts+2−222
@@ -1,22 +1,6 @@
11 import * as c from "yoctocolors";
22 import { G, writeln } from "./output.ts";
3-
4-function renderDiff(diff: string): void {
5- if (!diff || diff === "(no changes)") return;
6- for (const line of diff.split("\n")) {
7- if (line.startsWith("+++") || line.startsWith("---")) {
8- writeln(` ${c.dim(line)}`);
9- } else if (line.startsWith("+")) {
10- writeln(` ${c.green(line)}`);
11- } else if (line.startsWith("-")) {
12- writeln(` ${c.red(line)}`);
13- } else if (line.startsWith("@@")) {
14- writeln(` ${c.cyan(line)}`);
15- } else {
16- writeln(` ${c.dim(line)}`);
17- }
18- }
19-}
3+import { renderToolResultByName } from "./tool-result-handlers.ts";
204
215 function formatErrorBadge(result: unknown): string {
226 const msg =
@@ -29,52 +13,6 @@ function formatErrorBadge(result: unknown): string {
2913 return `${G.err} ${c.red(oneLiner)}`;
3014 }
3115
32-function writeShellPreviewLines(
33- label: string,
34- value: string,
35- lineColor: (input: string) => string,
36- maxLines: number,
37-): void {
38- if (!value.trim()) return;
39- const lines = value.split("\n");
40- writeln(` ${c.dim(label)} ${c.dim(`(${lines.length} lines)`)}`);
41- for (const line of lines.slice(0, maxLines)) {
42- writeln(` ${lineColor("│")} ${line}`);
43- }
44- if (lines.length > maxLines) {
45- writeln(
46- ` ${lineColor("│")} ${c.dim(`… +${lines.length - maxLines} lines`)}`,
47- );
48- }
49-}
50-
51-function renderShellResult(result: {
52- stdout: string;
53- stderr: string;
54- exitCode: number;
55- success: boolean;
56- timedOut: boolean;
57-}): void {
58- const badge = result.timedOut
59- ? c.yellow("timeout")
60- : result.success
61- ? c.green("success")
62- : c.red("error");
63-
64- const stdoutLines = result.stdout.trim()
65- ? result.stdout.split("\n").length
66- : 0;
67- const stderrLines = result.stderr.trim()
68- ? result.stderr.split("\n").length
69- : 0;
70-
71- writeln(
72- ` ${badge} ${c.dim(`exit ${result.exitCode} · stdout ${stdoutLines}L · stderr ${stderrLines}L`)}`,
73- );
74- writeShellPreviewLines("stdout", result.stdout, c.dim, 6);
75- writeShellPreviewLines("stderr", result.stderr, c.red, 4);
76-}
77-
7816 export function renderToolResult(
7917 toolName: string,
8018 result: unknown,
@@ -85,165 +23,7 @@ export function renderToolResult(
8523 return;
8624 }
8725
88- if (toolName === "read") {
89- const r = result as {
90- path: string;
91- line: number;
92- totalLines: number;
93- truncated: boolean;
94- content?: string;
95- };
96- const linesReturned = r.content ? r.content.split("\n").length : 0;
97- const endLine = linesReturned > 0 ? r.line + linesReturned - 1 : r.line;
98- const range =
99- r.line === 1 && endLine === r.totalLines
100- ? `${r.totalLines} lines`
101- : `lines ${r.line}–${endLine} of ${r.totalLines}`;
102- writeln(
103- ` ${G.info} ${c.dim(`${r.path} ${range}${r.truncated ? " (truncated)" : ""}`)}`,
104- );
105- return;
106- }
107-
108- if (toolName === "create") {
109- const r = result as { path: string; diff: string; created: boolean };
110- const verb = r.created ? c.green("created") : c.dim("overwritten");
111- writeln(` ${G.ok} ${verb} ${r.path}`);
112- renderDiff(r.diff);
113- return;
114- }
115-
116- if (toolName === "replace" || toolName === "insert") {
117- const r = result as { path: string; diff: string; deleted?: boolean };
118- const verb =
119- toolName === "insert" ? "inserted" : r.deleted ? "deleted" : "replaced";
120- writeln(` ${G.ok} ${c.dim(verb)} ${r.path}`);
121- renderDiff(r.diff);
122- return;
123- }
124-
125- if (toolName === "shell") {
126- renderShellResult(
127- result as {
128- stdout: string;
129- stderr: string;
130- exitCode: number;
131- success: boolean;
132- timedOut: boolean;
133- },
134- );
135- return;
136- }
137-
138- if (toolName === "subagent") {
139- const r = result as {
140- inputTokens?: number;
141- outputTokens?: number;
142- agentName?: string;
143- };
144- const label = r.agentName ? ` ${c.dim(c.cyan(`[@${r.agentName}]`))}` : "";
145- writeln(
146- ` ${G.agent}${label} ${c.dim(`subagent done (${r.inputTokens ?? 0}in / ${r.outputTokens ?? 0}out tokens)`)}`,
147- );
148- return;
149- }
150-
151- if (toolName === "readSkill") {
152- const r = result as {
153- skill?: {
154- name?: string;
155- description?: string;
156- source?: "local" | "global";
157- };
158- };
159- if (!r.skill) {
160- writeln(` ${G.info} ${c.dim("skill")} ${c.dim("(not found)")}`);
161- return;
162- }
163- const name = r.skill.name ?? "(unknown)";
164- const source = r.skill.source ?? "unknown";
165- const description = r.skill.description?.trim();
166- const descPart = description
167- ? ` ${c.dim("·")} ${c.dim(description.length > 60 ? `${description.slice(0, 57)}…` : description)}`
168- : "";
169- writeln(
170- ` ${G.info} ${c.dim("skill")} ${name} ${c.dim("·")} ${c.dim(source)}${descPart}`,
171- );
172- return;
173- }
174-
175- if (toolName === "webSearch") {
176- const r = result as {
177- results?: Array<{ title?: string; url?: string; score?: number }>;
178- };
179- if (Array.isArray(r?.results)) {
180- if (r.results.length === 0) {
181- writeln(` ${G.info} ${c.dim("no results")}`);
182- return;
183- }
184-
185- for (const item of r.results.slice(0, 5)) {
186- const title = (item.title?.trim() || item.url || "(untitled)").replace(
187- /\s+/g,
188- " ",
189- );
190- const score =
191- typeof item.score === "number"
192- ? c.dim(` (${item.score.toFixed(2)})`)
193- : "";
194- writeln(` ${c.dim("•")} ${title}${score}`);
195- if (item.url) writeln(` ${c.dim(item.url)}`);
196- }
197-
198- if (r.results.length > 5) {
199- writeln(` ${c.dim(` +${r.results.length - 5} more`)}`);
200- }
201- return;
202- }
203- }
204-
205- if (toolName === "webContent") {
206- const r = result as {
207- results?: Array<{ url?: string; title?: string; text?: string }>;
208- };
209- if (Array.isArray(r?.results)) {
210- if (r.results.length === 0) {
211- writeln(` ${G.info} ${c.dim("no pages")}`);
212- return;
213- }
214-
215- for (const item of r.results.slice(0, 3)) {
216- const title = (item.title?.trim() || item.url || "(untitled)").replace(
217- /\s+/g,
218- " ",
219- );
220- writeln(` ${c.dim("•")} ${title}`);
221- if (item.url) writeln(` ${c.dim(item.url)}`);
222- const preview = (item.text ?? "").replace(/\s+/g, " ").trim();
223- if (preview) {
224- const trimmed =
225- preview.length > 220 ? `${preview.slice(0, 217)}…` : preview;
226- writeln(` ${c.dim(trimmed)}`);
227- }
228- }
229-
230- if (r.results.length > 3) {
231- writeln(` ${c.dim(` +${r.results.length - 3} more`)}`);
232- }
233- return;
234- }
235- }
236-
237- if (toolName.startsWith("mcp_")) {
238- const content = Array.isArray(result) ? result : [result];
239- for (const block of (
240- content as Array<{ type?: string; text?: string }>
241- ).slice(0, 5)) {
242- if (block?.type === "text" && block.text) {
243- const lines = block.text.split("\n").slice(0, 6);
244- for (const line of lines) writeln(` ${c.dim("│")} ${line}`);
245- }
246- }
26+ if (renderToolResultByName(toolName, result)) {
24727 return;
24828 }
24929
src/cli/tool-result-handlers.ts+216−0
@@ -0,0 +1,216 @@
1+import * as c from "yoctocolors";
2+import { G, writeln } from "./output.ts";
3+import { renderDiff, writePreviewLines } from "./tool-result-shared.ts";
4+
5+function renderReadResult(result: unknown): boolean {
6+ const r = result as {
7+ path: string;
8+ line: number;
9+ totalLines: number;
10+ truncated: boolean;
11+ content?: string;
12+ };
13+ if (!r || typeof r.path !== "string") return false;
14+
15+ const linesReturned = r.content ? r.content.split("\n").length : 0;
16+ const endLine = linesReturned > 0 ? r.line + linesReturned - 1 : r.line;
17+ const range =
18+ r.line === 1 && endLine === r.totalLines
19+ ? `${r.totalLines} lines`
20+ : `lines ${r.line}–${endLine} of ${r.totalLines}`;
21+ writeln(
22+ ` ${G.info} ${c.dim(`${r.path} ${range}${r.truncated ? " (truncated)" : ""}`)}`,
23+ );
24+ return true;
25+}
26+
27+function renderCreateResult(result: unknown): boolean {
28+ const r = result as { path: string; diff: string; created: boolean };
29+ if (!r || typeof r.path !== "string") return false;
30+ const verb = r.created ? c.green("created") : c.dim("overwritten");
31+ writeln(` ${G.ok} ${verb} ${r.path}`);
32+ renderDiff(r.diff);
33+ return true;
34+}
35+
36+function renderReplaceOrInsertResult(
37+ toolName: string,
38+ result: unknown,
39+): boolean {
40+ const r = result as { path: string; diff: string; deleted?: boolean };
41+ if (!r || typeof r.path !== "string") return false;
42+ const verb =
43+ toolName === "insert" ? "inserted" : r.deleted ? "deleted" : "replaced";
44+ writeln(` ${G.ok} ${c.dim(verb)} ${r.path}`);
45+ renderDiff(r.diff);
46+ return true;
47+}
48+
49+function renderShellResult(result: unknown): boolean {
50+ const r = result as {
51+ stdout: string;
52+ stderr: string;
53+ exitCode: number;
54+ success: boolean;
55+ timedOut: boolean;
56+ };
57+ if (!r || typeof r.stdout !== "string" || typeof r.stderr !== "string") {
58+ return false;
59+ }
60+
61+ const badge = r.timedOut
62+ ? c.yellow("timeout")
63+ : r.success
64+ ? c.green("success")
65+ : c.red("error");
66+
67+ const stdoutLines = r.stdout.trim() ? r.stdout.split("\n").length : 0;
68+ const stderrLines = r.stderr.trim() ? r.stderr.split("\n").length : 0;
69+
70+ writeln(
71+ ` ${badge} ${c.dim(`exit ${r.exitCode} · stdout ${stdoutLines}L · stderr ${stderrLines}L`)}`,
72+ );
73+ writePreviewLines({
74+ label: "stdout",
75+ value: r.stdout,
76+ lineColor: c.dim,
77+ maxLines: 6,
78+ });
79+ writePreviewLines({
80+ label: "stderr",
81+ value: r.stderr,
82+ lineColor: c.red,
83+ maxLines: 4,
84+ });
85+ return true;
86+}
87+
88+function renderSubagentResult(result: unknown): boolean {
89+ const r = result as {
90+ inputTokens?: number;
91+ outputTokens?: number;
92+ agentName?: string;
93+ };
94+ if (!r || typeof r !== "object") return false;
95+ const label = r.agentName ? ` ${c.dim(c.cyan(`[@${r.agentName}]`))}` : "";
96+ writeln(
97+ ` ${G.agent}${label} ${c.dim(`subagent done (${r.inputTokens ?? 0}in / ${r.outputTokens ?? 0}out tokens)`)}`,
98+ );
99+ return true;
100+}
101+
102+function renderReadSkillResult(result: unknown): boolean {
103+ const r = result as {
104+ skill?: {
105+ name?: string;
106+ description?: string;
107+ source?: "local" | "global";
108+ };
109+ };
110+ if (!r || typeof r !== "object") return false;
111+ if (!r.skill) {
112+ writeln(` ${G.info} ${c.dim("skill")} ${c.dim("(not found)")}`);
113+ return true;
114+ }
115+ const name = r.skill.name ?? "(unknown)";
116+ const source = r.skill.source ?? "unknown";
117+ const description = r.skill.description?.trim();
118+ const descPart = description
119+ ? ` ${c.dim("·")} ${c.dim(description.length > 60 ? `${description.slice(0, 57)}…` : description)}`
120+ : "";
121+ writeln(
122+ ` ${G.info} ${c.dim("skill")} ${name} ${c.dim("·")} ${c.dim(source)}${descPart}`,
123+ );
124+ return true;
125+}
126+
127+function renderWebSearchResult(result: unknown): boolean {
128+ const r = result as {
129+ results?: Array<{ title?: string; url?: string; score?: number }>;
130+ };
131+ if (!Array.isArray(r?.results)) return false;
132+ if (r.results.length === 0) {
133+ writeln(` ${G.info} ${c.dim("no results")}`);
134+ return true;
135+ }
136+
137+ for (const item of r.results.slice(0, 5)) {
138+ const title = (item.title?.trim() || item.url || "(untitled)").replace(
139+ /\s+/g,
140+ " ",
141+ );
142+ const score =
143+ typeof item.score === "number"
144+ ? c.dim(` (${item.score.toFixed(2)})`)
145+ : "";
146+ writeln(` ${c.dim("•")} ${title}${score}`);
147+ if (item.url) writeln(` ${c.dim(item.url)}`);
148+ }
149+
150+ if (r.results.length > 5) {
151+ writeln(` ${c.dim(` +${r.results.length - 5} more`)}`);
152+ }
153+ return true;
154+}
155+
156+function renderWebContentResult(result: unknown): boolean {
157+ const r = result as {
158+ results?: Array<{ url?: string; title?: string; text?: string }>;
159+ };
160+ if (!Array.isArray(r?.results)) return false;
161+ if (r.results.length === 0) {
162+ writeln(` ${G.info} ${c.dim("no pages")}`);
163+ return true;
164+ }
165+
166+ for (const item of r.results.slice(0, 3)) {
167+ const title = (item.title?.trim() || item.url || "(untitled)").replace(
168+ /\s+/g,
169+ " ",
170+ );
171+ writeln(` ${c.dim("•")} ${title}`);
172+ if (item.url) writeln(` ${c.dim(item.url)}`);
173+ const preview = (item.text ?? "").replace(/\s+/g, " ").trim();
174+ if (preview) {
175+ const trimmed =
176+ preview.length > 220 ? `${preview.slice(0, 217)}…` : preview;
177+ writeln(` ${c.dim(trimmed)}`);
178+ }
179+ }
180+
181+ if (r.results.length > 3) {
182+ writeln(` ${c.dim(` +${r.results.length - 3} more`)}`);
183+ }
184+ return true;
185+}
186+
187+function renderMcpResult(result: unknown): boolean {
188+ const content = Array.isArray(result) ? result : [result];
189+ for (const block of (
190+ content as Array<{ type?: string; text?: string }>
191+ ).slice(0, 5)) {
192+ if (block?.type === "text" && block.text) {
193+ const lines = block.text.split("\n").slice(0, 6);
194+ for (const line of lines) writeln(` ${c.dim("│")} ${line}`);
195+ }
196+ }
197+ return true;
198+}
199+
200+export function renderToolResultByName(
201+ toolName: string,
202+ result: unknown,
203+): boolean {
204+ if (toolName === "read") return renderReadResult(result);
205+ if (toolName === "create") return renderCreateResult(result);
206+ if (toolName === "replace" || toolName === "insert") {
207+ return renderReplaceOrInsertResult(toolName, result);
208+ }
209+ if (toolName === "shell") return renderShellResult(result);
210+ if (toolName === "subagent") return renderSubagentResult(result);
211+ if (toolName === "readSkill") return renderReadSkillResult(result);
212+ if (toolName === "webSearch") return renderWebSearchResult(result);
213+ if (toolName === "webContent") return renderWebContentResult(result);
214+ if (toolName.startsWith("mcp_")) return renderMcpResult(result);
215+ return false;
216+}
src/cli/tool-result-shared.ts+38−0
@@ -0,0 +1,38 @@
1+import * as c from "yoctocolors";
2+import { writeln } from "./output.ts";
3+
4+export function renderDiff(diff: string): void {
5+ if (!diff || diff === "(no changes)") return;
6+ for (const line of diff.split("\n")) {
7+ if (line.startsWith("+++") || line.startsWith("---")) {
8+ writeln(` ${c.dim(line)}`);
9+ } else if (line.startsWith("+")) {
10+ writeln(` ${c.green(line)}`);
11+ } else if (line.startsWith("-")) {
12+ writeln(` ${c.red(line)}`);
13+ } else if (line.startsWith("@@")) {
14+ writeln(` ${c.cyan(line)}`);
15+ } else {
16+ writeln(` ${c.dim(line)}`);
17+ }
18+ }
19+}
20+
21+export function writePreviewLines(opts: {
22+ label: string;
23+ value: string;
24+ lineColor: (input: string) => string;
25+ maxLines: number;
26+}): void {
27+ if (!opts.value.trim()) return;
28+ const lines = opts.value.split("\n");
29+ writeln(` ${c.dim(opts.label)} ${c.dim(`(${lines.length} lines)`)}`);
30+ for (const line of lines.slice(0, opts.maxLines)) {
31+ writeln(` ${opts.lineColor("│")} ${line}`);
32+ }
33+ if (lines.length > opts.maxLines) {
34+ writeln(
35+ ` ${opts.lineColor("│")} ${c.dim(`… +${lines.length - opts.maxLines} lines`)}`,
36+ );
37+ }
38+}