docs(coding-agent): simplify tool-override example to use built-in renderer

This commit is contained in:
Mario Zechner 2026-01-08 13:27:03 +01:00
parent bbf0d92e8c
commit 550742ca98

View file

@ -13,20 +13,15 @@
* 2. Block access to sensitive paths (e.g., .env files) * 2. Block access to sensitive paths (e.g., .env files)
* 3. Delegate to the original read implementation for allowed files * 3. Delegate to the original read implementation for allowed files
* *
* Since no custom renderCall/renderResult are provided, the built-in renderer
* is used automatically (syntax highlighting, line numbers, truncation warnings).
*
* Usage: * Usage:
* pi --no-tools --tools bash,edit,write -e ./tool-override.ts * pi -e ./tool-override.ts
*
* The --no-tools flag disables all built-in tools, then --tools adds back the ones
* we want (excluding read, which we're overriding). The extension provides our
* custom read implementation.
*
* Alternatively, without --no-tools the extension's read tool will override the
* built-in read tool automatically.
*/ */
import type { TextContent } from "@mariozechner/pi-ai"; import type { TextContent } from "@mariozechner/pi-ai";
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent"; import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
import { Text } from "@mariozechner/pi-tui";
import { Type } from "@sinclair/typebox"; import { Type } from "@sinclair/typebox";
import { appendFileSync, constants, readFileSync } from "fs"; import { appendFileSync, constants, readFileSync } from "fs";
import { access, readFile } from "fs/promises"; import { access, readFile } from "fs/promises";
@ -128,38 +123,8 @@ export default function (pi: ExtensionAPI) {
} }
}, },
// Custom rendering to show it's the audited version // No renderCall/renderResult - uses built-in renderer automatically
renderCall(args, theme) { // (syntax highlighting, line numbers, truncation warnings, etc.)
return new Text(theme.fg("toolTitle", theme.bold("read ")) + theme.fg("accent", args.path), 0, 0);
},
renderResult(result, { expanded }, theme) {
const content = result.content[0];
if (content?.type === "text" && content.text.startsWith("Access denied")) {
return new Text(theme.fg("error", "Access denied (sensitive file)"), 0, 0);
}
if (content?.type === "text" && content.text.startsWith("Error")) {
return new Text(theme.fg("error", content.text), 0, 0);
}
// Show preview of content
if (content?.type === "text") {
const lines = content.text.split("\n");
const preview = lines.slice(0, expanded ? 10 : 3);
let text = theme.fg("success", `Read ${lines.length} lines`);
if (expanded) {
for (const line of preview) {
text += `\n${theme.fg("dim", line.slice(0, 100))}`;
}
if (lines.length > 10) {
text += `\n${theme.fg("muted", `... ${lines.length - 10} more lines`)}`;
}
}
return new Text(text, 0, 0);
}
return new Text(theme.fg("dim", "Read complete"), 0, 0);
},
}); });
// Also register a command to view the access log // Also register a command to view the access log