-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreadFile.ts
36 lines (31 loc) · 898 Bytes
/
readFile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { readFile as fsReadFile } from "fs/promises";
import { z } from "zod";
import { Tool } from "../openai/tool.utils.js";
const argsSchema = z.object({
relativeFilePath: z
.string()
.describe("The path to the file relative to the project root"),
prefixWithLineNumbers: z
.boolean()
.optional()
.describe("Prefix each line with its line number. Use it for git patches"),
});
type Args = z.input<typeof argsSchema>;
export default {
name: "readFile",
description: "Reads a file",
argsSchema,
async call(args: Args) {
let fileContent = await fsReadFile(args.relativeFilePath, "utf-8");
if (args.prefixWithLineNumbers) {
fileContent = fileContent
.split("\n")
.map((line, index) => `${index + 1}:${line}`)
.join("\n");
}
return {
success: true,
output: fileContent,
};
},
} satisfies Tool<Args>;