Elffuss BraidHub

kikocis/continue public

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