64875
Mc
mini-coder / files
mini-coder / src / tools / index.ts
1.8 KB
1import type { Api, Model, Tool } from "@earendil-works/pi-ai"; 2import type { ToolName } from "../config.ts"; 3import type { ToolContext, ToolResult } from "./common.ts"; 4import { BASH_PARAMS, bash, type BashArgs } from "./bash.ts"; 5import { EDIT_PARAMS, edit, type EditArgs } from "./edit.ts"; 6import { READ_PARAMS, read, type ReadArgs } from "./read.ts"; 7 8export type { FileDiff, ToolResult } from "./common.ts"; 9 10/** Whether a model accepts image input. Drives `read`'s description and results. */ 11export function acceptsImages(model: Model<Api>): boolean { 12 return model.input.includes("image"); 13} 14 15const READ_DESCRIPTION = "Read a file. Returns its text. Prefer bash for search, ranges, or binary files."; 16const READ_IMAGE_DESCRIPTION = 17 "Read a file. Returns its text, or the image itself when the file is a png, jpg, or webp. " + 18 "Prefer bash for search, ranges, or binary files."; 19 20export function toolSchemas(names: ToolName[], withImages: boolean): Tool[] { 21 const tools: Record<ToolName, Tool> = { 22 edit: { 23 name: "edit", 24 description: 25 "Edit a file by exact text replacement. oldText must occur exactly once. " + 26 "With empty oldText, create a new file (fails if it exists).", 27 parameters: EDIT_PARAMS, 28 }, 29 read: { 30 name: "read", 31 description: withImages ? READ_IMAGE_DESCRIPTION : READ_DESCRIPTION, 32 parameters: READ_PARAMS, 33 }, 34 bash: { 35 name: "bash", 36 description: "Run a bash command in the current working directory.", 37 parameters: BASH_PARAMS, 38 }, 39 }; 40 return names.map((name) => tools[name]); 41} 42 43export async function executeTool(name: string, args: unknown, ctx: ToolContext): Promise<ToolResult> { 44 if (name === "edit") return edit(args as EditArgs, ctx.signal); 45 if (name === "read") return read(args as ReadArgs, ctx); 46 return bash(args as BashArgs, ctx); 47}