xonecas.io
64875
Mc

mini-coder / commit 75631ae

refactor(cli): split tool result renderers by domain

xonecas2026-03-14 75631aeec95011e80e7d278ec07b2563c699509e parent 501746f

10 files changed

src/cli/tool-result-handlers.ts+1−265
@@ -1,265 +1 @@
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 stdoutNormalized = r.stdout.replace(/[\r\n]+$/, "");
68- const stderrNormalized = r.stderr.replace(/[\r\n]+$/, "");
69- const stdoutLines = stdoutNormalized
70- ? stdoutNormalized.split(/\r?\n/).length
71- : 0;
72- const stderrLines = stderrNormalized
73- ? stderrNormalized.split(/\r?\n/).length
74- : 0;
75- const stdoutSingleLine =
76- stdoutLines === 1 ? (stdoutNormalized.split(/\r?\n/)[0] ?? "") : null;
77-
78- writeln(
79- ` ${badge} ${c.dim(`exit ${r.exitCode} · stdout ${stdoutLines}L · stderr ${stderrLines}L`)}`,
80- );
81-
82- if (
83- r.success &&
84- !r.timedOut &&
85- stderrLines === 0 &&
86- stdoutSingleLine !== null
87- ) {
88- const compact =
89- stdoutSingleLine.length > 100
90- ? `${stdoutSingleLine.slice(0, 97)}…`
91- : stdoutSingleLine;
92- if (compact.length > 0) {
93- writeln(` ${c.dim(`stdout: ${compact}`)}`);
94- }
95- return true;
96- }
97-
98- if (!r.success || r.timedOut) {
99- writePreviewLines({
100- label: "stderr",
101- value: r.stderr,
102- lineColor: c.red,
103- maxLines: 6,
104- });
105- writePreviewLines({
106- label: "stdout",
107- value: r.stdout,
108- lineColor: c.dim,
109- maxLines: 4,
110- });
111- return true;
112- }
113-
114- if (stderrLines > 0) {
115- writePreviewLines({
116- label: "stderr",
117- value: r.stderr,
118- lineColor: c.red,
119- maxLines: 4,
120- });
121- }
122-
123- if (stdoutLines > 0 && stdoutLines <= 3) {
124- writePreviewLines({
125- label: "stdout",
126- value: r.stdout,
127- lineColor: c.dim,
128- maxLines: 3,
129- });
130- } else if (stdoutLines > 3) {
131- writeln(` ${c.dim(`stdout omitted (${stdoutLines} lines)`)}`);
132- }
133-
134- return true;
135-}
136-
137-function renderSubagentResult(result: unknown): boolean {
138- const r = result as {
139- inputTokens?: number;
140- outputTokens?: number;
141- agentName?: string;
142- };
143- if (!r || typeof r !== "object") return false;
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 true;
149-}
150-
151-function renderReadSkillResult(result: unknown): boolean {
152- const r = result as {
153- skill?: {
154- name?: string;
155- description?: string;
156- source?: "local" | "global";
157- };
158- };
159- if (!r || typeof r !== "object") return false;
160- if (!r.skill) {
161- writeln(` ${G.info} ${c.dim("skill")} ${c.dim("(not found)")}`);
162- return true;
163- }
164- const name = r.skill.name ?? "(unknown)";
165- const source = r.skill.source ?? "unknown";
166- const description = r.skill.description?.trim();
167- const descPart = description
168- ? ` ${c.dim("·")} ${c.dim(description.length > 60 ? `${description.slice(0, 57)}…` : description)}`
169- : "";
170- writeln(
171- ` ${G.info} ${c.dim("skill")} ${name} ${c.dim("·")} ${c.dim(source)}${descPart}`,
172- );
173- return true;
174-}
175-
176-function renderWebSearchResult(result: unknown): boolean {
177- const r = result as {
178- results?: Array<{ title?: string; url?: string; score?: number }>;
179- };
180- if (!Array.isArray(r?.results)) return false;
181- if (r.results.length === 0) {
182- writeln(` ${G.info} ${c.dim("no results")}`);
183- return true;
184- }
185-
186- for (const item of r.results.slice(0, 5)) {
187- const title = (item.title?.trim() || item.url || "(untitled)").replace(
188- /\s+/g,
189- " ",
190- );
191- const score =
192- typeof item.score === "number"
193- ? c.dim(` (${item.score.toFixed(2)})`)
194- : "";
195- writeln(` ${c.dim("•")} ${title}${score}`);
196- if (item.url) writeln(` ${c.dim(item.url)}`);
197- }
198-
199- if (r.results.length > 5) {
200- writeln(` ${c.dim(` +${r.results.length - 5} more`)}`);
201- }
202- return true;
203-}
204-
205-function renderWebContentResult(result: unknown): boolean {
206- const r = result as {
207- results?: Array<{ url?: string; title?: string; text?: string }>;
208- };
209- if (!Array.isArray(r?.results)) return false;
210- if (r.results.length === 0) {
211- writeln(` ${G.info} ${c.dim("no pages")}`);
212- return true;
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 true;
234-}
235-
236-function renderMcpResult(result: unknown): boolean {
237- const content = Array.isArray(result) ? result : [result];
238- for (const block of (
239- content as Array<{ type?: string; text?: string }>
240- ).slice(0, 5)) {
241- if (block?.type === "text" && block.text) {
242- const lines = block.text.split("\n").slice(0, 6);
243- for (const line of lines) writeln(` ${c.dim("│")} ${line}`);
244- }
245- }
246- return true;
247-}
248-
249-export function renderToolResultByName(
250- toolName: string,
251- result: unknown,
252-): boolean {
253- if (toolName === "read") return renderReadResult(result);
254- if (toolName === "create") return renderCreateResult(result);
255- if (toolName === "replace" || toolName === "insert") {
256- return renderReplaceOrInsertResult(toolName, result);
257- }
258- if (toolName === "shell") return renderShellResult(result);
259- if (toolName === "subagent") return renderSubagentResult(result);
260- if (toolName === "readSkill") return renderReadSkillResult(result);
261- if (toolName === "webSearch") return renderWebSearchResult(result);
262- if (toolName === "webContent") return renderWebContentResult(result);
263- if (toolName.startsWith("mcp_")) return renderMcpResult(result);
264- return false;
265-}
1+export { renderToolResultByName } from "./tool-results/index.ts";
src/cli/tool-results/file-edits.ts+25−0
@@ -0,0 +1,25 @@
1+import * as c from "yoctocolors";
2+import { G, writeln } from "../output.ts";
3+import { renderDiff } from "../tool-result-shared.ts";
4+
5+export function renderCreateResult(result: unknown): boolean {
6+ const r = result as { path: string; diff: string; created: boolean };
7+ if (!r || typeof r.path !== "string") return false;
8+ const verb = r.created ? c.green("created") : c.dim("overwritten");
9+ writeln(` ${G.ok} ${verb} ${r.path}`);
10+ renderDiff(r.diff);
11+ return true;
12+}
13+
14+export function renderReplaceOrInsertResult(
15+ toolName: string,
16+ result: unknown,
17+): boolean {
18+ const r = result as { path: string; diff: string; deleted?: boolean };
19+ if (!r || typeof r.path !== "string") return false;
20+ const verb =
21+ toolName === "insert" ? "inserted" : r.deleted ? "deleted" : "replaced";
22+ writeln(` ${G.ok} ${c.dim(verb)} ${r.path}`);
23+ renderDiff(r.diff);
24+ return true;
25+}
src/cli/tool-results/index.ts+36−0
@@ -0,0 +1,36 @@
1+import {
2+ renderCreateResult,
3+ renderReplaceOrInsertResult,
4+} from "./file-edits.ts";
5+import { renderMcpResult } from "./mcp.ts";
6+import { renderReadResult } from "./read.ts";
7+import { renderShellResult } from "./shell.ts";
8+import { renderReadSkillResult } from "./skills.ts";
9+import { renderSubagentResult } from "./subagent.ts";
10+import type { ToolResultRenderer } from "./types.ts";
11+import { renderWebContentResult, renderWebSearchResult } from "./web.ts";
12+
13+const TOOL_RESULT_RENDERERS: Readonly<Record<string, ToolResultRenderer>> = {
14+ read: renderReadResult,
15+ create: renderCreateResult,
16+ replace: (result) => renderReplaceOrInsertResult("replace", result),
17+ insert: (result) => renderReplaceOrInsertResult("insert", result),
18+ shell: renderShellResult,
19+ subagent: renderSubagentResult,
20+ readSkill: renderReadSkillResult,
21+ webSearch: renderWebSearchResult,
22+ webContent: renderWebContentResult,
23+};
24+
25+export function renderToolResultByName(
26+ toolName: string,
27+ result: unknown,
28+): boolean {
29+ if (toolName.startsWith("mcp_")) {
30+ return renderMcpResult(result);
31+ }
32+
33+ const renderer = TOOL_RESULT_RENDERERS[toolName];
34+ if (!renderer) return false;
35+ return renderer(result, toolName);
36+}
src/cli/tool-results/mcp.ts+15−0
@@ -0,0 +1,15 @@
1+import * as c from "yoctocolors";
2+import { writeln } from "../output.ts";
3+
4+export function renderMcpResult(result: unknown): boolean {
5+ const content = Array.isArray(result) ? result : [result];
6+ for (const block of (
7+ content as Array<{ type?: string; text?: string }>
8+ ).slice(0, 5)) {
9+ if (block?.type === "text" && block.text) {
10+ const lines = block.text.split("\n").slice(0, 6);
11+ for (const line of lines) writeln(` ${c.dim("│")} ${line}`);
12+ }
13+ }
14+ return true;
15+}
src/cli/tool-results/read.ts+24−0
@@ -0,0 +1,24 @@
1+import * as c from "yoctocolors";
2+import { G, writeln } from "../output.ts";
3+
4+export function renderReadResult(result: unknown): boolean {
5+ const r = result as {
6+ path: string;
7+ line: number;
8+ totalLines: number;
9+ truncated: boolean;
10+ content?: string;
11+ };
12+ if (!r || typeof r.path !== "string") return false;
13+
14+ const linesReturned = r.content ? r.content.split("\n").length : 0;
15+ const endLine = linesReturned > 0 ? r.line + linesReturned - 1 : r.line;
16+ const range =
17+ r.line === 1 && endLine === r.totalLines
18+ ? `${r.totalLines} lines`
19+ : `lines ${r.line}–${endLine} of ${r.totalLines}`;
20+ writeln(
21+ ` ${G.info} ${c.dim(`${r.path} ${range}${r.truncated ? " (truncated)" : ""}`)}`,
22+ );
23+ return true;
24+}
src/cli/tool-results/shell.ts+91−0
@@ -0,0 +1,91 @@
1+import * as c from "yoctocolors";
2+import { writeln } from "../output.ts";
3+import { writePreviewLines } from "../tool-result-shared.ts";
4+
5+export function renderShellResult(result: unknown): boolean {
6+ const r = result as {
7+ stdout: string;
8+ stderr: string;
9+ exitCode: number;
10+ success: boolean;
11+ timedOut: boolean;
12+ };
13+ if (!r || typeof r.stdout !== "string" || typeof r.stderr !== "string") {
14+ return false;
15+ }
16+
17+ const badge = r.timedOut
18+ ? c.yellow("timeout")
19+ : r.success
20+ ? c.green("success")
21+ : c.red("error");
22+
23+ const stdoutNormalized = r.stdout.replace(/[\r\n]+$/, "");
24+ const stderrNormalized = r.stderr.replace(/[\r\n]+$/, "");
25+ const stdoutLines = stdoutNormalized
26+ ? stdoutNormalized.split(/\r?\n/).length
27+ : 0;
28+ const stderrLines = stderrNormalized
29+ ? stderrNormalized.split(/\r?\n/).length
30+ : 0;
31+ const stdoutSingleLine =
32+ stdoutLines === 1 ? (stdoutNormalized.split(/\r?\n/)[0] ?? "") : null;
33+
34+ writeln(
35+ ` ${badge} ${c.dim(`exit ${r.exitCode} · stdout ${stdoutLines}L · stderr ${stderrLines}L`)}`,
36+ );
37+
38+ if (
39+ r.success &&
40+ !r.timedOut &&
41+ stderrLines === 0 &&
42+ stdoutSingleLine !== null
43+ ) {
44+ const compact =
45+ stdoutSingleLine.length > 100
46+ ? `${stdoutSingleLine.slice(0, 97)}…`
47+ : stdoutSingleLine;
48+ if (compact.length > 0) {
49+ writeln(` ${c.dim(`stdout: ${compact}`)}`);
50+ }
51+ return true;
52+ }
53+
54+ if (!r.success || r.timedOut) {
55+ writePreviewLines({
56+ label: "stderr",
57+ value: r.stderr,
58+ lineColor: c.red,
59+ maxLines: 6,
60+ });
61+ writePreviewLines({
62+ label: "stdout",
63+ value: r.stdout,
64+ lineColor: c.dim,
65+ maxLines: 4,
66+ });
67+ return true;
68+ }
69+
70+ if (stderrLines > 0) {
71+ writePreviewLines({
72+ label: "stderr",
73+ value: r.stderr,
74+ lineColor: c.red,
75+ maxLines: 4,
76+ });
77+ }
78+
79+ if (stdoutLines > 0 && stdoutLines <= 3) {
80+ writePreviewLines({
81+ label: "stdout",
82+ value: r.stdout,
83+ lineColor: c.dim,
84+ maxLines: 3,
85+ });
86+ } else if (stdoutLines > 3) {
87+ writeln(` ${c.dim(`stdout omitted (${stdoutLines} lines)`)}`);
88+ }
89+
90+ return true;
91+}
src/cli/tool-results/skills.ts+27−0
@@ -0,0 +1,27 @@
1+import * as c from "yoctocolors";
2+import { G, writeln } from "../output.ts";
3+
4+export function renderReadSkillResult(result: unknown): boolean {
5+ const r = result as {
6+ skill?: {
7+ name?: string;
8+ description?: string;
9+ source?: "local" | "global";
10+ };
11+ };
12+ if (!r || typeof r !== "object") return false;
13+ if (!r.skill) {
14+ writeln(` ${G.info} ${c.dim("skill")} ${c.dim("(not found)")}`);
15+ return true;
16+ }
17+ const name = r.skill.name ?? "(unknown)";
18+ const source = r.skill.source ?? "unknown";
19+ const description = r.skill.description?.trim();
20+ const descPart = description
21+ ? ` ${c.dim("·")} ${c.dim(description.length > 60 ? `${description.slice(0, 57)}…` : description)}`
22+ : "";
23+ writeln(
24+ ` ${G.info} ${c.dim("skill")} ${name} ${c.dim("·")} ${c.dim(source)}${descPart}`,
25+ );
26+ return true;
27+}
src/cli/tool-results/subagent.ts+16−0
@@ -0,0 +1,16 @@
1+import * as c from "yoctocolors";
2+import { G, writeln } from "../output.ts";
3+
4+export function renderSubagentResult(result: unknown): boolean {
5+ const r = result as {
6+ inputTokens?: number;
7+ outputTokens?: number;
8+ agentName?: string;
9+ };
10+ if (!r || typeof r !== "object") return false;
11+ const label = r.agentName ? ` ${c.dim(c.cyan(`[@${r.agentName}]`))}` : "";
12+ writeln(
13+ ` ${G.agent}${label} ${c.dim(`subagent done (${r.inputTokens ?? 0}in / ${r.outputTokens ?? 0}out tokens)`)}`,
14+ );
15+ return true;
16+}
src/cli/tool-results/types.ts+4−0
@@ -0,0 +1,4 @@
1+export type ToolResultRenderer = (
2+ result: unknown,
3+ toolName?: string,
4+) => boolean;
src/cli/tool-results/web.ts+62−0
@@ -0,0 +1,62 @@
1+import * as c from "yoctocolors";
2+import { G, writeln } from "../output.ts";
3+
4+export function renderWebSearchResult(result: unknown): boolean {
5+ const r = result as {
6+ results?: Array<{ title?: string; url?: string; score?: number }>;
7+ };
8+ if (!Array.isArray(r?.results)) return false;
9+ if (r.results.length === 0) {
10+ writeln(` ${G.info} ${c.dim("no results")}`);
11+ return true;
12+ }
13+
14+ for (const item of r.results.slice(0, 5)) {
15+ const title = (item.title?.trim() || item.url || "(untitled)").replace(
16+ /\s+/g,
17+ " ",
18+ );
19+ const score =
20+ typeof item.score === "number"
21+ ? c.dim(` (${item.score.toFixed(2)})`)
22+ : "";
23+ writeln(` ${c.dim("•")} ${title}${score}`);
24+ if (item.url) writeln(` ${c.dim(item.url)}`);
25+ }
26+
27+ if (r.results.length > 5) {
28+ writeln(` ${c.dim(` +${r.results.length - 5} more`)}`);
29+ }
30+ return true;
31+}
32+
33+export function renderWebContentResult(result: unknown): boolean {
34+ const r = result as {
35+ results?: Array<{ url?: string; title?: string; text?: string }>;
36+ };
37+ if (!Array.isArray(r?.results)) return false;
38+ if (r.results.length === 0) {
39+ writeln(` ${G.info} ${c.dim("no pages")}`);
40+ return true;
41+ }
42+
43+ for (const item of r.results.slice(0, 3)) {
44+ const title = (item.title?.trim() || item.url || "(untitled)").replace(
45+ /\s+/g,
46+ " ",
47+ );
48+ writeln(` ${c.dim("•")} ${title}`);
49+ if (item.url) writeln(` ${c.dim(item.url)}`);
50+ const preview = (item.text ?? "").replace(/\s+/g, " ").trim();
51+ if (preview) {
52+ const trimmed =
53+ preview.length > 220 ? `${preview.slice(0, 217)}…` : preview;
54+ writeln(` ${c.dim(trimmed)}`);
55+ }
56+ }
57+
58+ if (r.results.length > 3) {
59+ writeln(` ${c.dim(` +${r.results.length - 3} more`)}`);
60+ }
61+ return true;
62+}