xonecas.io
64875
Mc

mini-coder / commit 6d9ddb9

refactor: consolidate tool-render modules into single file

- Merge tool-render-call.ts, tool-render-result.ts into tool-render.ts
- Remove unnecessary tool-result-handlers.ts re-export layer
- Reduces total file count from 140 to 137

xonecas2026-03-14 6d9ddb9674624d9a1bba07bd4c52347e9975776a parent 298e264

6 files changed

src/cli/tool-render-call.test.ts+1−1
@@ -1,5 +1,5 @@
11 import { describe, expect, test } from "bun:test";
2-import { buildToolCallLine } from "./tool-render-call.ts";
2+import { buildToolCallLine } from "./tool-render.ts";
33
44 describe("buildToolCallLine", () => {
55 test("formats shell calls with truncated command previews", () => {
src/cli/tool-render-call.ts+0−66
@@ -1,66 +0,0 @@
1-import * as c from "yoctocolors";
2-import { G } from "./output.ts";
3-
4-function toolGlyph(name: string): string {
5- if (name === "read") return G.read;
6- if (name === "create" || name === "replace" || name === "insert")
7- return G.write;
8- if (name === "shell") return G.run;
9- if (name === "subagent") return G.agent;
10- if (name.startsWith("mcp_")) return G.mcp;
11- return G.info;
12-}
13-
14-export function buildToolCallLine(name: string, args: unknown): string {
15- const a =
16- args && typeof args === "object" ? (args as Record<string, unknown>) : {};
17-
18- if (name === "subagent") {
19- const prompt = typeof a.prompt === "string" ? a.prompt : "";
20- const short = prompt.length > 60 ? `${prompt.slice(0, 57)}…` : prompt;
21- const agentName =
22- typeof a.agentName === "string" && a.agentName ? a.agentName : "";
23- const label = agentName ? ` ${c.dim(c.cyan(`[@${agentName}]`))}` : "";
24- return `${G.agent}${label} ${c.dim("—")} ${short}`;
25- }
26-
27- if (name === "read") {
28- const line = Number.isFinite(a.line as number) ? Number(a.line) : null;
29- const count = Number.isFinite(a.count as number) ? Number(a.count) : null;
30- const range =
31- line || count ? c.dim(`:${line ?? 1}${count ? `+${count}` : ""}`) : "";
32- const path =
33- typeof a.path === "string" && a.path.length > 0 ? a.path : null;
34- return `${G.read} ${c.dim("read")}${path ? ` ${path}${range}` : ""}`;
35- }
36-
37- if (name === "create") {
38- return `${G.write} ${c.dim("create")} ${c.bold(String(a.path ?? ""))}`;
39- }
40-
41- if (name === "replace") {
42- const range = a.endAnchor
43- ? c.dim(` ${a.startAnchor}–${a.endAnchor}`)
44- : c.dim(` ${a.startAnchor}`);
45- const verb =
46- a.newContent === undefined || a.newContent === "" ? "delete" : "replace";
47- return `${G.write} ${c.dim(verb)} ${c.bold(String(a.path ?? ""))}${range}`;
48- }
49-
50- if (name === "insert") {
51- return `${G.write} ${c.dim(`insert ${a.position ?? ""}`)} ${c.bold(String(a.path ?? ""))}${c.dim(` @ ${a.anchor}`)}`;
52- }
53-
54- if (name === "shell") {
55- const cmd = String(a.command ?? "").trim();
56- if (!cmd) return `${G.run} ${c.dim("shell")}`;
57- const shortCmd = cmd.length > 72 ? `${cmd.slice(0, 69)}…` : cmd;
58- return `${G.run} ${shortCmd}`;
59- }
60-
61- if (name.startsWith("mcp_")) {
62- return `${G.mcp} ${c.dim(name)}`;
63- }
64-
65- return `${toolGlyph(name)} ${c.dim(name)}`;
66-}
src/cli/tool-render-result.test.ts+0−146
@@ -1,146 +0,0 @@
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).not.toContain("stdout (2 lines)");
39- expect(plain).not.toContain("stderr (1 lines)");
40- });
41-
42- test("keeps successful multi-line shell output compact", () => {
43- terminal.stdoutWrite = (chunk: string) => {
44- stdout += chunk;
45- };
46-
47- renderToolResult(
48- "shell",
49- {
50- stdout: "a\nb\nc\nd\ne",
51- stderr: "",
52- exitCode: 0,
53- success: true,
54- timedOut: false,
55- },
56- false,
57- );
58-
59- const plain = stripAnsi(stdout);
60- expect(plain).toContain("exit 0 · stdout 5L · stderr 0L");
61- expect(plain).not.toContain("stdout omitted");
62- expect(plain).not.toContain("stdout (5 lines)");
63- });
64-
65- test("renders compact one-line stdout for successful shell calls", () => {
66- terminal.stdoutWrite = (chunk: string) => {
67- stdout += chunk;
68- };
69-
70- renderToolResult(
71- "shell",
72- {
73- stdout: "/home/xonecas/src/mini-coder\n",
74- stderr: "",
75- exitCode: 0,
76- success: true,
77- timedOut: false,
78- },
79- false,
80- );
81-
82- const plain = stripAnsi(stdout);
83- expect(plain).toContain("exit 0 · stdout 1L · stderr 0L");
84- expect(plain).toContain("out: /home/xonecas/src/mini-coder");
85- expect(plain).not.toContain("stdout (1 lines)");
86- });
87-
88- test("renders file edit diff for create", () => {
89- terminal.stdoutWrite = (chunk: string) => {
90- stdout += chunk;
91- };
92-
93- renderToolResult(
94- "create",
95- {
96- path: "src/new.ts",
97- created: true,
98- diff: "@@ -0,0 +1 @@\n+console.log('ok')",
99- },
100- false,
101- );
102-
103- const plain = stripAnsi(stdout);
104- expect(plain).toContain("created src/new.ts");
105- expect(plain).toContain("@@ -0,0 +1 @@");
106- expect(plain).toContain("+console.log('ok')");
107- });
108-
109- test("truncates large file edit diffs to keep output compact", () => {
110- terminal.stdoutWrite = (chunk: string) => {
111- stdout += chunk;
112- };
113-
114- const diffLines = [
115- "@@ -0,0 +30 @@",
116- ...Array.from({ length: 30 }, (_, i) => `+line ${i + 1}`),
117- ];
118- renderToolResult(
119- "create",
120- {
121- path: "src/huge.ts",
122- created: true,
123- diff: diffLines.join("\n"),
124- },
125- false,
126- );
127-
128- const plain = stripAnsi(stdout);
129- expect(plain).toContain("created src/huge.ts");
130- expect(plain).toContain("+line 1");
131- expect(plain).toContain("+line 23");
132- expect(plain).toContain("… +7 more diff lines");
133- expect(plain).not.toContain("+line 30");
134- });
135-
136- test("renders one-line error when tool result fails", () => {
137- terminal.stdoutWrite = (chunk: string) => {
138- stdout += chunk;
139- };
140-
141- renderToolResult("read", "boom\ndetails", true);
142- const plain = stripAnsi(stdout);
143- expect(plain).toContain("✖ boom");
144- expect(plain).not.toContain("details");
145- });
146-});
src/cli/tool-render-result.ts+0−32
@@ -1,32 +0,0 @@
1-import * as c from "yoctocolors";
2-import { G, writeln } from "./output.ts";
3-import { renderToolResultByName } from "./tool-result-handlers.ts";
4-
5-function formatErrorBadge(result: unknown): string {
6- const msg =
7- typeof result === "string"
8- ? result
9- : result instanceof Error
10- ? result.message
11- : JSON.stringify(result);
12- const oneLiner = msg.split("\n")[0] ?? msg;
13- return `${G.err} ${c.red(oneLiner)}`;
14-}
15-
16-export function renderToolResult(
17- toolName: string,
18- result: unknown,
19- isError: boolean,
20-): void {
21- if (isError) {
22- writeln(` ${formatErrorBadge(result)}`);
23- return;
24- }
25-
26- if (renderToolResultByName(toolName, result)) {
27- return;
28- }
29-
30- const text = JSON.stringify(result);
31- writeln(` ${c.dim(text.length > 120 ? `${text.slice(0, 117)}…` : text)}`);
32-}
src/cli/tool-render.ts+100−10
@@ -1,15 +1,113 @@
11 import { homedir } from "node:os";
22 import * as c from "yoctocolors";
33 import { G, writeln } from "./output.ts";
4-import { buildToolCallLine } from "./tool-render-call.ts";
5-import { renderToolResult as renderResultImpl } from "./tool-render-result.ts";
4+import { renderToolResultByName } from "./tool-result-renderers.ts";
65
76 const HOME = homedir();
87
8+// ─── Tool call line rendering ─────────────────────────────────────────────────
9+
10+function toolGlyph(name: string): string {
11+ if (name === "read") return G.read;
12+ if (name === "create" || name === "replace" || name === "insert")
13+ return G.write;
14+ if (name === "shell") return G.run;
15+ if (name === "subagent") return G.agent;
16+ if (name.startsWith("mcp_")) return G.mcp;
17+ return G.info;
18+}
19+
20+export function buildToolCallLine(name: string, args: unknown): string {
21+ const a =
22+ args && typeof args === "object" ? (args as Record<string, unknown>) : {};
23+
24+ if (name === "subagent") {
25+ const prompt = typeof a.prompt === "string" ? a.prompt : "";
26+ const short = prompt.length > 60 ? `${prompt.slice(0, 57)}…` : prompt;
27+ const agentName =
28+ typeof a.agentName === "string" && a.agentName ? a.agentName : "";
29+ const label = agentName ? ` ${c.dim(c.cyan(`[@${agentName}]`))}` : "";
30+ return `${G.agent}${label} ${c.dim("—")} ${short}`;
31+ }
32+
33+ if (name === "read") {
34+ const line = Number.isFinite(a.line as number) ? Number(a.line) : null;
35+ const count = Number.isFinite(a.count as number) ? Number(a.count) : null;
36+ const range =
37+ line || count ? c.dim(`:${line ?? 1}${count ? `+${count}` : ""}`) : "";
38+ const path =
39+ typeof a.path === "string" && a.path.length > 0 ? a.path : null;
40+ return `${G.read} ${c.dim("read")}${path ? ` ${path}${range}` : ""}`;
41+ }
42+
43+ if (name === "create") {
44+ return `${G.write} ${c.dim("create")} ${c.bold(String(a.path ?? ""))}`;
45+ }
46+
47+ if (name === "replace") {
48+ const range = a.endAnchor
49+ ? c.dim(` ${a.startAnchor}–${a.endAnchor}`)
50+ : c.dim(` ${a.startAnchor}`);
51+ const verb =
52+ a.newContent === undefined || a.newContent === "" ? "delete" : "replace";
53+ return `${G.write} ${c.dim(verb)} ${c.bold(String(a.path ?? ""))}${range}`;
54+ }
55+
56+ if (name === "insert") {
57+ return `${G.write} ${c.dim(`insert ${a.position ?? ""}`)} ${c.bold(String(a.path ?? ""))}${c.dim(` @ ${a.anchor}`)}`;
58+ }
59+
60+ if (name === "shell") {
61+ const cmd = String(a.command ?? "").trim();
62+ if (!cmd) return `${G.run} ${c.dim("shell")}`;
63+ const shortCmd = cmd.length > 72 ? `${cmd.slice(0, 69)}…` : cmd;
64+ return `${G.run} ${shortCmd}`;
65+ }
66+
67+ if (name.startsWith("mcp_")) {
68+ return `${G.mcp} ${c.dim(name)}`;
69+ }
70+
71+ return `${toolGlyph(name)} ${c.dim(name)}`;
72+}
73+
974 export function renderToolCall(toolName: string, args: unknown): void {
1075 writeln(` ${buildToolCallLine(toolName, args)}`);
1176 }
1277
78+// ─── Tool result rendering ────────────────────────────────────────────────────
79+
80+function formatErrorBadge(result: unknown): string {
81+ const msg =
82+ typeof result === "string"
83+ ? result
84+ : result instanceof Error
85+ ? result.message
86+ : JSON.stringify(result);
87+ const oneLiner = msg.split("\n")[0] ?? msg;
88+ return `${G.err} ${c.red(oneLiner)}`;
89+}
90+
91+export function renderToolResult(
92+ toolName: string,
93+ result: unknown,
94+ isError: boolean,
95+): void {
96+ if (isError) {
97+ writeln(` ${formatErrorBadge(result)}`);
98+ return;
99+ }
100+
101+ if (renderToolResultByName(toolName, result)) {
102+ return;
103+ }
104+
105+ const text = JSON.stringify(result);
106+ writeln(` ${c.dim(text.length > 120 ? `${text.slice(0, 117)}…` : text)}`);
107+}
108+
109+// ─── Hook rendering ───────────────────────────────────────────────────────────
110+
13111 export function renderHook(
14112 toolName: string,
15113 scriptPath: string,
@@ -24,11 +122,3 @@ export function renderHook(
24122 ` ${G.err} ${c.red(`hook post-${toolName} failed`)} ${c.dim(short)}`,
25123 );
26124 }
27-
28-export function renderToolResult(
29- toolName: string,
30- result: unknown,
31- isError: boolean,
32-): void {
33- renderResultImpl(toolName, result, isError);
34-}
src/cli/tool-result-handlers.ts+0−1
@@ -1 +0,0 @@
1-export { renderToolResultByName } from "./tool-result-renderers.ts";