Elffuss BraidHub

kikocis/continue public

b4ebb5b5ebbb · 1.7 KiBRawPermalink
1import * as fs from "fs";
2
3import { throwIfFileIsSecurityConcern } from "core/indexing/ignore.js";
4
5import { markFileAsRead } from "./edit.js";
6import { formatToolArgument } from "./formatters.js";
7import { Tool } from "./types.js";
8
9export const readFileTool: Tool = {
10 name: "Read",
11 displayName: "Read",
12 description: "Read the contents of a file at the specified path",
13 parameters: {
14 filepath: {
15 type: "string",
16 description: "The path to the file to read",
17 required: true,
18 },
19 },
20 readonly: true,
21 isBuiltIn: true,
22 preprocess: async (args) => {
23 throwIfFileIsSecurityConcern(args.filepath);
24 return {
25 args,
26 preview: [
27 {
28 type: "text",
29 content: `Will read ${formatToolArgument(args.filepath)}`,
30 },
31 ],
32 };
33 },
34 run: async (args: { filepath: string }): Promise<string> => {
35 try {
36 let { filepath } = args;
37 if (filepath.startsWith("./")) {
38 filepath = filepath.slice(2);
39 }
40
41 if (!fs.existsSync(filepath)) {
42 return `Error: File does not exist: ${filepath}`;
43 }
44 const content = fs.readFileSync(filepath, "utf-8");
45 // Mark this file as read for the edit tool
46 markFileAsRead(filepath);
47
48 const lines = content.split("\n");
49 if (lines.length > 5000) {
50 const truncatedContent = lines.slice(0, 5000).join("\n");
51 return `Content of ${filepath} (truncated to first 5000 lines of ${lines.length} total):\n${truncatedContent}`;
52 }
53
54 return `Content of ${filepath}:\n${content}`;
55 } catch (error) {
56 return `Error reading file: ${
57 error instanceof Error ? error.message : String(error)
58 }`;
59 }
60 },
61};