feat: add read_file tool

This commit is contained in:
2026-05-31 12:11:09 -05:00
parent 3f0d9a80c7
commit 0dc0f7eb53
5 changed files with 49 additions and 10 deletions
+38
View File
@@ -0,0 +1,38 @@
import { readFileSync } from "node:fs";
import { type } from "arktype";
import type { ToolContext } from "./server.ts";
import { execute, tool } from "./shared.ts";
export const ReadFileParams = type({
path: type.string.describe("absolute path to the file to read"),
"start_line?": type.number.describe("start line, 1-based inclusive (default: 1)"),
"end_line?": type.number.describe("end line, 1-based inclusive (default: end of file)"),
});
export function ReadFileTool(_ctx: ToolContext) {
return tool({
name: "read_file",
description:
"Read lines from a file. Use this to read sections of the PR diff returned by checkout_pr. " +
"Example: `read_file({ path: diffPath, start_line: 5, end_line: 42 })`.",
parameters: ReadFileParams,
execute: execute(async ({ path, start_line, end_line }) => {
let content: string;
try {
content = readFileSync(path, "utf-8");
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
throw new Error(`failed to read ${path}: ${msg}`);
}
if (start_line === undefined && end_line === undefined) {
return { content };
}
const lines = content.split("\n");
const start = Math.max(0, (start_line ?? 1) - 1);
const end = Math.min(lines.length, end_line ?? lines.length);
return { content: lines.slice(start, end).join("\n") };
}),
});
}