File size: 13,938 Bytes
f500658 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | /**
* Minimal Mode Example - Demonstrates a "minimal" tool display mode
*
* This extension overrides built-in tools to provide custom rendering:
* - Collapsed mode: Only shows the tool call (command/path), no output
* - Expanded mode: Shows full output like the built-in renderers
*
* This demonstrates how a "minimal mode" could work, where ctrl+o cycles through:
* - Standard: Shows truncated output (current default)
* - Expanded: Shows full output (current expanded)
* - Minimal: Shows only tool call, no output (this extension's collapsed mode)
*
* Usage:
* pi -e ./minimal-mode.ts
*
* Then use ctrl+o to toggle between minimal (collapsed) and full (expanded) views.
*/
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
import {
createBashTool,
createEditTool,
createFindTool,
createGrepTool,
createLsTool,
createReadTool,
createWriteTool,
} from "@earendil-works/pi-coding-agent";
import { Text } from "@earendil-works/pi-tui";
import { homedir } from "os";
/**
* Shorten a path by replacing home directory with ~
*/
function shortenPath(path: string): string {
const home = homedir();
if (path.startsWith(home)) {
return `~${path.slice(home.length)}`;
}
return path;
}
// Cache for built-in tools by cwd
const toolCache = new Map<string, ReturnType<typeof createBuiltInTools>>();
function createBuiltInTools(cwd: string) {
return {
read: createReadTool(cwd),
bash: createBashTool(cwd),
edit: createEditTool(cwd),
write: createWriteTool(cwd),
find: createFindTool(cwd),
grep: createGrepTool(cwd),
ls: createLsTool(cwd),
};
}
function getBuiltInTools(cwd: string) {
let tools = toolCache.get(cwd);
if (!tools) {
tools = createBuiltInTools(cwd);
toolCache.set(cwd, tools);
}
return tools;
}
export default function (pi: ExtensionAPI) {
// =========================================================================
// Read Tool
// =========================================================================
pi.registerTool({
name: "read",
label: "read",
description:
"Read the contents of a file. Supports text files and images (jpg, png, gif, webp). Images are sent as attachments. For text files, output is truncated to 2000 lines or 50KB (whichever is hit first). Use offset/limit for large files.",
parameters: getBuiltInTools(process.cwd()).read.parameters,
async execute(toolCallId, params, signal, onUpdate, ctx) {
const tools = getBuiltInTools(ctx.cwd);
return tools.read.execute(toolCallId, params, signal, onUpdate);
},
renderCall(args, theme, _context) {
const path = shortenPath(args.path || "");
let pathDisplay = path ? theme.fg("accent", path) : theme.fg("toolOutput", "...");
// Show line range if specified
if (args.offset !== undefined || args.limit !== undefined) {
const startLine = args.offset ?? 1;
const endLine = args.limit !== undefined ? startLine + args.limit - 1 : "";
pathDisplay += theme.fg("warning", `:${startLine}${endLine ? `-${endLine}` : ""}`);
}
return new Text(`${theme.fg("toolTitle", theme.bold("read"))} ${pathDisplay}`, 0, 0);
},
renderResult(result, { expanded }, theme, _context) {
// Minimal mode: show nothing in collapsed state
if (!expanded) {
return new Text("", 0, 0);
}
// Expanded mode: show full output
const textContent = result.content.find((c) => c.type === "text");
if (!textContent || textContent.type !== "text") {
return new Text("", 0, 0);
}
const lines = textContent.text.split("\n");
const output = lines.map((line) => theme.fg("toolOutput", line)).join("\n");
return new Text(`\n${output}`, 0, 0);
},
});
// =========================================================================
// Bash Tool
// =========================================================================
pi.registerTool({
name: "bash",
label: "bash",
description:
"Execute a bash command in the current working directory. Returns stdout and stderr. Output is truncated to last 2000 lines or 50KB (whichever is hit first).",
parameters: getBuiltInTools(process.cwd()).bash.parameters,
async execute(toolCallId, params, signal, onUpdate, ctx) {
const tools = getBuiltInTools(ctx.cwd);
return tools.bash.execute(toolCallId, params, signal, onUpdate);
},
renderCall(args, theme, _context) {
const command = args.command || "...";
const timeout = args.timeout as number | undefined;
const timeoutSuffix = timeout ? theme.fg("muted", ` (timeout ${timeout}s)`) : "";
return new Text(theme.fg("toolTitle", theme.bold(`$ ${command}`)) + timeoutSuffix, 0, 0);
},
renderResult(result, { expanded }, theme, _context) {
// Minimal mode: show nothing in collapsed state
if (!expanded) {
return new Text("", 0, 0);
}
// Expanded mode: show full output
const textContent = result.content.find((c) => c.type === "text");
if (!textContent || textContent.type !== "text") {
return new Text("", 0, 0);
}
const output = textContent.text
.trim()
.split("\n")
.map((line) => theme.fg("toolOutput", line))
.join("\n");
if (!output) {
return new Text("", 0, 0);
}
return new Text(`\n${output}`, 0, 0);
},
});
// =========================================================================
// Write Tool
// =========================================================================
pi.registerTool({
name: "write",
label: "write",
description:
"Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Automatically creates parent directories.",
parameters: getBuiltInTools(process.cwd()).write.parameters,
async execute(toolCallId, params, signal, onUpdate, ctx) {
const tools = getBuiltInTools(ctx.cwd);
return tools.write.execute(toolCallId, params, signal, onUpdate);
},
renderCall(args, theme, _context) {
const path = shortenPath(args.path || "");
const pathDisplay = path ? theme.fg("accent", path) : theme.fg("toolOutput", "...");
const lineCount = args.content ? args.content.split("\n").length : 0;
const lineInfo = lineCount > 0 ? theme.fg("muted", ` (${lineCount} lines)`) : "";
return new Text(`${theme.fg("toolTitle", theme.bold("write"))} ${pathDisplay}${lineInfo}`, 0, 0);
},
renderResult(result, { expanded }, theme, _context) {
// Minimal mode: show nothing (file was written)
if (!expanded) {
return new Text("", 0, 0);
}
// Expanded mode: show error if any
if (result.content.some((c) => c.type === "text" && c.text)) {
const textContent = result.content.find((c) => c.type === "text");
if (textContent?.type === "text" && textContent.text) {
return new Text(`\n${theme.fg("error", textContent.text)}`, 0, 0);
}
}
return new Text("", 0, 0);
},
});
// =========================================================================
// Edit Tool
// =========================================================================
pi.registerTool({
name: "edit",
label: "edit",
description:
"Edit a file by replacing exact text. The oldText must match exactly (including whitespace). Use this for precise, surgical edits.",
parameters: getBuiltInTools(process.cwd()).edit.parameters,
async execute(toolCallId, params, signal, onUpdate, ctx) {
const tools = getBuiltInTools(ctx.cwd);
return tools.edit.execute(toolCallId, params, signal, onUpdate);
},
renderCall(args, theme, _context) {
const path = shortenPath(args.path || "");
const pathDisplay = path ? theme.fg("accent", path) : theme.fg("toolOutput", "...");
return new Text(`${theme.fg("toolTitle", theme.bold("edit"))} ${pathDisplay}`, 0, 0);
},
renderResult(result, { expanded }, theme, _context) {
// Minimal mode: show nothing in collapsed state
if (!expanded) {
return new Text("", 0, 0);
}
// Expanded mode: show diff or error
const textContent = result.content.find((c) => c.type === "text");
if (!textContent || textContent.type !== "text") {
return new Text("", 0, 0);
}
// For errors, show the error message
const text = textContent.text;
if (text.includes("Error") || text.includes("error")) {
return new Text(`\n${theme.fg("error", text)}`, 0, 0);
}
// Otherwise show the text (would be nice to show actual diff here)
return new Text(`\n${theme.fg("toolOutput", text)}`, 0, 0);
},
});
// =========================================================================
// Find Tool
// =========================================================================
pi.registerTool({
name: "find",
label: "find",
description:
"Find files by name pattern (glob). Searches recursively from the specified path. Output limited to 200 results.",
parameters: getBuiltInTools(process.cwd()).find.parameters,
async execute(toolCallId, params, signal, onUpdate, ctx) {
const tools = getBuiltInTools(ctx.cwd);
return tools.find.execute(toolCallId, params, signal, onUpdate);
},
renderCall(args, theme, _context) {
const pattern = args.pattern || "";
const path = shortenPath(args.path || ".");
const limit = args.limit;
let text = `${theme.fg("toolTitle", theme.bold("find"))} ${theme.fg("accent", pattern)}`;
text += theme.fg("toolOutput", ` in ${path}`);
if (limit !== undefined) {
text += theme.fg("toolOutput", ` (limit ${limit})`);
}
return new Text(text, 0, 0);
},
renderResult(result, { expanded }, theme, _context) {
if (!expanded) {
// Minimal: just show count
const textContent = result.content.find((c) => c.type === "text");
if (textContent?.type === "text") {
const count = textContent.text.trim().split("\n").filter(Boolean).length;
if (count > 0) {
return new Text(theme.fg("muted", ` → ${count} files`), 0, 0);
}
}
return new Text("", 0, 0);
}
// Expanded: show full results
const textContent = result.content.find((c) => c.type === "text");
if (!textContent || textContent.type !== "text") {
return new Text("", 0, 0);
}
const output = textContent.text
.trim()
.split("\n")
.map((line) => theme.fg("toolOutput", line))
.join("\n");
return new Text(`\n${output}`, 0, 0);
},
});
// =========================================================================
// Grep Tool
// =========================================================================
pi.registerTool({
name: "grep",
label: "grep",
description:
"Search file contents by regex pattern. Uses ripgrep for fast searching. Output limited to 200 matches.",
parameters: getBuiltInTools(process.cwd()).grep.parameters,
async execute(toolCallId, params, signal, onUpdate, ctx) {
const tools = getBuiltInTools(ctx.cwd);
return tools.grep.execute(toolCallId, params, signal, onUpdate);
},
renderCall(args, theme, _context) {
const pattern = args.pattern || "";
const path = shortenPath(args.path || ".");
const glob = args.glob;
const limit = args.limit;
let text = `${theme.fg("toolTitle", theme.bold("grep"))} ${theme.fg("accent", `/${pattern}/`)}`;
text += theme.fg("toolOutput", ` in ${path}`);
if (glob) {
text += theme.fg("toolOutput", ` (${glob})`);
}
if (limit !== undefined) {
text += theme.fg("toolOutput", ` limit ${limit}`);
}
return new Text(text, 0, 0);
},
renderResult(result, { expanded }, theme, _context) {
if (!expanded) {
// Minimal: just show match count
const textContent = result.content.find((c) => c.type === "text");
if (textContent?.type === "text") {
const count = textContent.text.trim().split("\n").filter(Boolean).length;
if (count > 0) {
return new Text(theme.fg("muted", ` → ${count} matches`), 0, 0);
}
}
return new Text("", 0, 0);
}
// Expanded: show full results
const textContent = result.content.find((c) => c.type === "text");
if (!textContent || textContent.type !== "text") {
return new Text("", 0, 0);
}
const output = textContent.text
.trim()
.split("\n")
.map((line) => theme.fg("toolOutput", line))
.join("\n");
return new Text(`\n${output}`, 0, 0);
},
});
// =========================================================================
// Ls Tool
// =========================================================================
pi.registerTool({
name: "ls",
label: "ls",
description:
"List directory contents with file sizes. Shows files and directories with their sizes. Output limited to 500 entries.",
parameters: getBuiltInTools(process.cwd()).ls.parameters,
async execute(toolCallId, params, signal, onUpdate, ctx) {
const tools = getBuiltInTools(ctx.cwd);
return tools.ls.execute(toolCallId, params, signal, onUpdate);
},
renderCall(args, theme, _context) {
const path = shortenPath(args.path || ".");
const limit = args.limit;
let text = `${theme.fg("toolTitle", theme.bold("ls"))} ${theme.fg("accent", path)}`;
if (limit !== undefined) {
text += theme.fg("toolOutput", ` (limit ${limit})`);
}
return new Text(text, 0, 0);
},
renderResult(result, { expanded }, theme, _context) {
if (!expanded) {
// Minimal: just show entry count
const textContent = result.content.find((c) => c.type === "text");
if (textContent?.type === "text") {
const count = textContent.text.trim().split("\n").filter(Boolean).length;
if (count > 0) {
return new Text(theme.fg("muted", ` → ${count} entries`), 0, 0);
}
}
return new Text("", 0, 0);
}
// Expanded: show full listing
const textContent = result.content.find((c) => c.type === "text");
if (!textContent || textContent.type !== "text") {
return new Text("", 0, 0);
}
const output = textContent.text
.trim()
.split("\n")
.map((line) => theme.fg("toolOutput", line))
.join("\n");
return new Text(`\n${output}`, 0, 0);
},
});
}
|