@@ -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