Elffuss BraidHub

kikocis/continue public

e035bf028461 · 1.8 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 let { filepath } = args;
24 if (filepath.startsWith("./")) {
25 filepath = filepath.slice(2);
26 }
27 throwIfFileIsSecurityConcern(filepath);
28 return {
29 args,
30 preview: [
31 {
32 type: "text",
33 content: `Will read ${formatToolArgument(filepath)}`,
34 },
35 ],
36 };
37 },
38 run: async (args: { filepath: string }): Promise<string> => {
39 try {
40 let { filepath } = args;
41 if (filepath.startsWith("./")) {
42 filepath = filepath.slice(2);
43 }
44
45 if (!fs.existsSync(filepath)) {
46 return `Error: File does not exist: ${filepath}`;
47 }
48 const content = fs.readFileSync(filepath, "utf-8");
49 // Mark this file as read for the edit tool
50 markFileAsRead(filepath);
51
52 const lines = content.split("\n");
53 if (lines.length > 5000) {
54 const truncatedContent = lines.slice(0, 5000).join("\n");
55 return `Content of ${filepath} (truncated to first 5000 lines of ${lines.length} total):\n${truncatedContent}`;
56 }
57
58 return `Content of ${filepath}:\n${content}`;
59 } catch (error) {
60 return `Error reading file: ${
61 error instanceof Error ? error.message : String(error)
62 }`;
63 }
64 },
65};