-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathfs.ts
79 lines (61 loc) · 1.67 KB
/
fs.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { z } from "zod";
import { StructuredTool, ToolParams } from "@langchain/core/tools";
import { BaseFileStore } from "../stores/file/base.js";
/**
* Interface for parameters required by the ReadFileTool class.
*/
interface ReadFileParams extends ToolParams {
store: BaseFileStore;
}
/**
* Class for reading files from the disk. Extends the StructuredTool
* class.
*/
export class ReadFileTool extends StructuredTool {
static lc_name() {
return "ReadFileTool";
}
schema = z.object({
file_path: z.string().describe("name of file"),
});
name = "read_file";
description = "Read file from disk";
store: BaseFileStore;
constructor({ store }: ReadFileParams) {
super(...arguments);
this.store = store;
}
async _call({ file_path }: z.infer<typeof this.schema>) {
return await this.store.readFile(file_path);
}
}
/**
* Interface for parameters required by the WriteFileTool class.
*/
interface WriteFileParams extends ToolParams {
store: BaseFileStore;
}
/**
* Class for writing data to files on the disk. Extends the StructuredTool
* class.
*/
export class WriteFileTool extends StructuredTool {
static lc_name() {
return "WriteFileTool";
}
schema = z.object({
file_path: z.string().describe("name of file"),
text: z.string().describe("text to write to file"),
});
name = "write_file";
description = "Write file from disk";
store: BaseFileStore;
constructor({ store, ...rest }: WriteFileParams) {
super(rest);
this.store = store;
}
async _call({ file_path, text }: z.infer<typeof this.schema>) {
await this.store.writeFile(file_path, text);
return "File written to successfully.";
}
}