Elffuss BraidHub

kikocis/continue public

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