@@ -1,33 +1,49 @@
1-import type { Tool, ToolCall } from "@earendil-works/pi-ai";
1+import type { Api, Model, Tool, ToolCall } from "@earendil-works/pi-ai";
22 import type { ToolName } from "../config.ts";
33 import { parseArgs, type ToolContext, type ToolResult } from "./common.ts";
44 import { BASH_PARAMS, bash } from "./bash.ts";
55 import { EDIT_PARAMS, edit } from "./edit.ts";
6+import { READ_PARAMS, read } from "./read.ts";
67
78 export type { ToolContext, ToolDetails, ToolResult } from "./common.ts";
89
9-const TOOL_SCHEMAS: Record<ToolName, Tool> = {
10- edit: {
11- name: "edit",
12- description:
13- "Edit a file by exact text replacement. oldText must occur exactly once. " +
14- "With empty oldText, create a new file (fails if it exists).",
15- parameters: EDIT_PARAMS,
16- },
17- bash: {
18- name: "bash",
19- description: "Run a bash command in the current working directory.",
20- parameters: BASH_PARAMS,
21- },
22-};
10+/** Whether a model accepts image input. Drives `read`'s description and results. */
11+export function acceptsImages(model: Model<Api>): boolean {
12+ return model.input.includes("image");
13+}
14+
15+const READ_DESCRIPTION = "Read a file. Returns its text. Prefer bash for search, ranges, or binary files.";
16+const READ_IMAGE_DESCRIPTION =
17+ "Read a file. Returns its text, or the image itself when the file is a png, jpg, gif, or webp. " +
18+ "Prefer bash for search, ranges, or binary files.";
2319
24-export function toolSchemas(names: ToolName[]): Tool[] {
25- return names.map((name) => TOOL_SCHEMAS[name]);
20+export 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]);
2641 }
2742
2843 export function executeTool(name: ToolName, call: ToolCall, ctx: ToolContext): Promise<ToolResult> {
2944 try {
3045 if (name === "edit") return Promise.resolve(edit(parseArgs(EDIT_PARAMS, call.arguments), ctx.signal));
46+ if (name === "read") return Promise.resolve(read(parseArgs(READ_PARAMS, call.arguments), ctx));
3147 return bash(parseArgs(BASH_PARAMS, call.arguments), ctx);
3248 } catch (error) {
3349 return Promise.resolve({ text: (error as Error).message, isError: true });