@@ -1,120 +1,28 @@
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";
46
57 const HOME = homedir();
68
7-function toolGlyph(name: string): string {
8- if (name === "read") return G.read;
9- if (name === "create" || name === "replace" || name === "insert")
10- return G.write;
11- if (name === "shell") return G.run;
12- if (name === "subagent") return G.agent;
13- if (name.startsWith("mcp_")) return G.mcp;
14- return G.info;
15-}
16-
17-function toolCallLine(name: string, args: unknown): string {
18- const a =
19- args && typeof args === "object" ? (args as Record<string, unknown>) : {};
20-
21- if (name === "subagent") {
22- const prompt = typeof a.prompt === "string" ? a.prompt : "";
23- const short = prompt.length > 60 ? `${prompt.slice(0, 57)}…` : prompt;
24- const agentName =
25- typeof a.agentName === "string" && a.agentName ? a.agentName : "";
26- const label = agentName ? ` ${c.dim(c.cyan(`[@${agentName}]`))}` : "";
27- return `${G.agent}${label} ${c.dim("—")} ${short}`;
28- }
29- if (name === "read") {
30- const line = Number.isFinite(a.line as number) ? Number(a.line) : null;
31- const count = Number.isFinite(a.count as number) ? Number(a.count) : null;
32- const range =
33- line || count ? c.dim(`:${line ?? 1}${count ? `+${count}` : ""}`) : "";
34- return `${G.read} ${c.dim("read")} ${String(a.path ?? "")}${range}`;
35- }
36- if (name === "create") {
37- return `${G.write} ${c.dim("create")} ${c.bold(String(a.path ?? ""))}`;
38- }
39- if (name === "replace") {
40- const range = a.endAnchor
41- ? c.dim(` ${a.startAnchor}–${a.endAnchor}`)
42- : c.dim(` ${a.startAnchor}`);
43- const verb =
44- a.newContent === undefined || a.newContent === "" ? "delete" : "replace";
45- return `${G.write} ${c.dim(verb)} ${c.bold(String(a.path ?? ""))}${range}`;
46- }
47- if (name === "insert") {
48- return `${G.write} ${c.dim(`insert ${a.position ?? ""}`)} ${c.bold(String(a.path ?? ""))}${c.dim(` @ ${a.anchor}`)}`;
49- }
50- if (name === "shell") {
51- const cmd = String(a.command ?? "");
52- const shortCmd = cmd.length > 72 ? `${cmd.slice(0, 69)}…` : cmd;
53- return `${G.run} ${shortCmd}`;
54- }
55- if (name.startsWith("mcp_")) {
56- return `${G.mcp} ${c.dim(name)}`;
57- }
58- return `${toolGlyph(name)} ${c.dim(name)}`;
59-}
60-
619 export function renderToolCall(toolName: string, args: unknown): void {
62- writeln(` ${toolCallLine(toolName, args)}`);
10+ writeln(` ${buildToolCallLine(toolName, args)}`);
6311 }
12+
6413 export function renderHook(
6514 toolName: string,
6615 scriptPath: string,
6716 success: boolean,
6817 ): void {
6918 const short = scriptPath.replace(HOME, "~");
70-
7119 if (success) {
7220 writeln(` ${G.ok} ${c.dim(`hook post-${toolName}`)}`);
73- } else {
74- writeln(
75- ` ${G.err} ${c.red(`hook post-${toolName} failed`)} ${c.dim(short)}`,
76- );
77- }
78-}
79-
80-function renderDiff(diff: string): void {
81- if (!diff || diff === "(no changes)") return;
82- for (const line of diff.split("\n")) {
83- if (line.startsWith("+++") || line.startsWith("---")) {
84- writeln(` ${c.dim(line)}`);
85- } else if (line.startsWith("+")) {
86- writeln(` ${c.green(line)}`);
87- } else if (line.startsWith("-")) {
88- writeln(` ${c.red(line)}`);
89- } else if (line.startsWith("@@")) {
90- writeln(` ${c.cyan(line)}`);
91- } else {
92- writeln(` ${c.dim(line)}`);
93- }
21+ return;
9422 }
95-}
96-
97-function formatErrorBadge(result: unknown): string {
98- const msg =
99- typeof result === "string"
100- ? result
101- : result instanceof Error
102- ? result.message
103- : JSON.stringify(result);
104- const oneLiner = msg.split("\n")[0] ?? msg;
105- return `${G.err} ${c.red(oneLiner)}`;
106-}
107-
108-function formatShellBadge(r: {
109- timedOut: boolean;
110- success: boolean;
111- exitCode: number;
112-}): string {
113- return r.timedOut
114- ? c.yellow("timeout")
115- : r.success
116- ? c.green(`✔ ${r.exitCode}`)
117- : c.red(`✖ ${r.exitCode}`);
23+ writeln(
24+ ` ${G.err} ${c.red(`hook post-${toolName} failed`)} ${c.dim(short)}`,
25+ );
11826 }
11927
12028 export function renderToolResult(
@@ -122,195 +30,5 @@ export function renderToolResult(
12230 result: unknown,
12331 isError: boolean,
12432 ): void {
125- if (isError) {
126- writeln(` ${formatErrorBadge(result)}`);
127- return;
128- }
129-
130- if (toolName === "read") {
131- const r = result as {
132- path: string;
133- line: number;
134- totalLines: number;
135- truncated: boolean;
136- content?: string;
137- };
138- const linesReturned = r.content ? r.content.split("\n").length : 0;
139- const endLine = linesReturned > 0 ? r.line + linesReturned - 1 : r.line;
140- const range =
141- r.line === 1 && endLine === r.totalLines
142- ? `${r.totalLines} lines`
143- : `lines ${r.line}–${endLine} of ${r.totalLines}`;
144- writeln(
145- ` ${G.info} ${c.dim(`${r.path} ${range}${r.truncated ? " (truncated)" : ""}`)}`,
146- );
147- return;
148- }
149-
150- if (toolName === "create") {
151- const r = result as { path: string; diff: string; created: boolean };
152- const verb = r.created ? c.green("created") : c.dim("overwritten");
153- writeln(` ${G.ok} ${verb} ${r.path}`);
154- renderDiff(r.diff);
155- return;
156- }
157-
158- if (toolName === "replace" || toolName === "insert") {
159- const r = result as { path: string; diff: string; deleted?: boolean };
160- const verb =
161- toolName === "insert" ? "inserted" : r.deleted ? "deleted" : "replaced";
162- writeln(` ${G.ok} ${c.dim(verb)} ${r.path}`);
163- renderDiff(r.diff);
164- return;
165- }
166-
167- if (toolName === "shell") {
168- const r = result as {
169- stdout: string;
170- stderr: string;
171- exitCode: number;
172- success: boolean;
173- timedOut: boolean;
174- };
175-
176- const badge = formatShellBadge(r);
177-
178- writeln(` ${badge}`);
179-
180- const outLines = r.stdout ? r.stdout.split("\n") : [];
181- const errLines = r.stderr ? r.stderr.split("\n") : [];
182-
183- // Show stdout (up to 20 lines)
184- for (const line of outLines.slice(0, 20)) {
185- writeln(` ${c.dim("│")} ${line}`);
186- }
187- if (outLines.length > 20)
188- writeln(` ${c.dim(`│ … +${outLines.length - 20} lines`)}`);
189-
190- // Show stderr (up to 8 lines), only if non-empty
191- for (const line of errLines.slice(0, 8)) {
192- if (line.trim()) writeln(` ${c.red("│")} ${c.dim(line)}`);
193- }
194- if (errLines.length > 8)
195- writeln(` ${c.red(`│ … +${errLines.length - 8} lines`)}`);
196- return;
197- }
198-
199- if (toolName === "subagent") {
200- const r = result as {
201- inputTokens?: number;
202- outputTokens?: number;
203- agentName?: string;
204- };
205- const label = r.agentName ? ` ${c.dim(c.cyan(`[@${r.agentName}]`))}` : "";
206- writeln(
207- ` ${G.agent}${label} ${c.dim(`subagent done (${r.inputTokens ?? 0}in / ${r.outputTokens ?? 0}out tokens)`)}`,
208- );
209-
210- return;
211- }
212-
213- if (toolName === "readSkill") {
214- const r = result as {
215- skill?: {
216- name?: string;
217- description?: string;
218- source?: "local" | "global";
219- };
220- };
221- if (!r.skill) {
222- writeln(` ${G.info} ${c.dim("skill")} ${c.dim("(not found)")}`);
223- return;
224- }
225- const name = r.skill.name ?? "(unknown)";
226- const source = r.skill.source ?? "unknown";
227- const description = r.skill.description?.trim();
228- const descPart = description
229- ? ` ${c.dim("·")} ${c.dim(description.length > 60 ? `${description.slice(0, 57)}…` : description)}`
230- : "";
231- writeln(
232- ` ${G.info} ${c.dim("skill")} ${name} ${c.dim("·")} ${c.dim(source)}${descPart}`,
233- );
234- return;
235- }
236-
237- if (toolName === "webSearch") {
238- const r = result as {
239- results?: Array<{ title?: string; url?: string; score?: number }>;
240- };
241- if (Array.isArray(r?.results)) {
242- if (r.results.length === 0) {
243- writeln(` ${G.info} ${c.dim("no results")}`);
244- return;
245- }
246-
247- for (const item of r.results.slice(0, 5)) {
248- const title = (item.title?.trim() || item.url || "(untitled)").replace(
249- /\s+/g,
250- " ",
251- );
252- const score =
253- typeof item.score === "number"
254- ? c.dim(` (${item.score.toFixed(2)})`)
255- : "";
256- writeln(` ${c.dim("•")} ${title}${score}`);
257- if (item.url) writeln(` ${c.dim(item.url)}`);
258- }
259-
260- if (r.results.length > 5) {
261- writeln(` ${c.dim(` +${r.results.length - 5} more`)}`);
262- }
263- return;
264- }
265- }
266-
267- if (toolName === "webContent") {
268- const r = result as {
269- results?: Array<{ url?: string; title?: string; text?: string }>;
270- };
271- if (Array.isArray(r?.results)) {
272- if (r.results.length === 0) {
273- writeln(` ${G.info} ${c.dim("no pages")}`);
274- return;
275- }
276-
277- for (const item of r.results.slice(0, 3)) {
278- const title = (item.title?.trim() || item.url || "(untitled)").replace(
279- /\s+/g,
280- " ",
281- );
282- writeln(` ${c.dim("•")} ${title}`);
283- if (item.url) writeln(` ${c.dim(item.url)}`);
284- const preview = (item.text ?? "").replace(/\s+/g, " ").trim();
285- if (preview) {
286- const trimmed =
287- preview.length > 220 ? `${preview.slice(0, 217)}…` : preview;
288- writeln(` ${c.dim(trimmed)}`);
289- }
290- }
291-
292- if (r.results.length > 3) {
293- writeln(` ${c.dim(` +${r.results.length - 3} more`)}`);
294- }
295- return;
296- }
297- }
298-
299- // MCP tools — show content blocks
300- if (toolName.startsWith("mcp_")) {
301- const content = Array.isArray(result) ? result : [result];
302- for (const block of (
303- content as Array<{ type?: string; text?: string }>
304- ).slice(0, 5)) {
305- if (block?.type === "text" && block.text) {
306- const lines = block.text.split("\n").slice(0, 6);
307- for (const l of lines) writeln(` ${c.dim("│")} ${l}`);
308- }
309- }
310- return;
311- }
312-
313- // Generic fallback — one-line JSON summary
314- const text = JSON.stringify(result);
315- writeln(` ${c.dim(text.length > 120 ? `${text.slice(0, 117)}…` : text)}`);
33+ renderResultImpl(toolName, result, isError);
31634 }