chore(vendor): import pi 1.3.0

Imported as a linear vendor commit so forge rebases do not replay the upstream root tree.

git-subtree-dir: third_party/pi

git-subtree-split: 71023aec1546d6d677a65669c5665c0927ce0dd0
This commit is contained in:
2026-10-15 17:58:48 -04:00
parent 719370c8f7
commit 6081e3e325
1743 changed files with 205460 additions and 131705 deletions

View File

@@ -18,13 +18,14 @@
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
import {
createBashTool,
createEditTool,
createFindTool,
createGrepTool,
createLsTool,
createReadTool,
createWriteTool,
createBashToolDefinition,
createEditToolDefinition,
createFindToolDefinition,
createGrepToolDefinition,
createLsToolDefinition,
createReadToolDefinition,
createWriteToolDefinition,
formatReadLineRange,
} from "@earendil-works/pi-coding-agent";
import { Text } from "@earendil-works/pi-tui";
import { homedir } from "os";
@@ -40,55 +41,34 @@ function shortenPath(path: string): string {
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),
read: createReadToolDefinition(cwd),
bash: createBashToolDefinition(cwd),
edit: createEditToolDefinition(cwd),
write: createWriteToolDefinition(cwd),
find: createFindToolDefinition(cwd),
grep: createGrepToolDefinition(cwd),
ls: createLsToolDefinition(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) {
const tools = createBuiltInTools(process.cwd());
// =========================================================================
// 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);
},
...tools.read,
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}` : ""}`);
const lineRange = formatReadLineRange(args.offset, args.limit);
if (lineRange) {
pathDisplay += theme.fg("warning", lineRange);
}
return new Text(`${theme.fg("toolTitle", theme.bold("read"))} ${pathDisplay}`, 0, 0);
@@ -116,16 +96,7 @@ export default function (pi: ExtensionAPI) {
// 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);
},
...tools.bash,
renderCall(args, theme, _context) {
const command = args.command || "...";
@@ -165,16 +136,7 @@ export default function (pi: ExtensionAPI) {
// 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);
},
...tools.write,
renderCall(args, theme, _context) {
const path = shortenPath(args.path || "");
@@ -207,16 +169,8 @@ export default function (pi: ExtensionAPI) {
// 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);
},
...tools.edit,
renderShell: "default",
renderCall(args, theme, _context) {
const path = shortenPath(args.path || "");
@@ -252,16 +206,7 @@ export default function (pi: ExtensionAPI) {
// 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);
},
...tools.find,
renderCall(args, theme, _context) {
const pattern = args.pattern || "";
@@ -310,16 +255,7 @@ export default function (pi: ExtensionAPI) {
// 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);
},
...tools.grep,
renderCall(args, theme, _context) {
const pattern = args.pattern || "";
@@ -372,16 +308,7 @@ export default function (pi: ExtensionAPI) {
// 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);
},
...tools.ls,
renderCall(args, theme, _context) {
const path = shortenPath(args.path || ".");