-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy pathfsTools.js
More file actions
82 lines (76 loc) · 2.11 KB
/
fsTools.js
File metadata and controls
82 lines (76 loc) · 2.11 KB
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
80
81
82
import fs from "fs-extra";
import { resolvePath, isSafePath } from "../utils/pathHelper.js";
import { decodeDsSource } from "../utils/dsHelper.js";
export const list_directory = async (args) => {
const dirPath = args?.path || ".";
if (!isSafePath(dirPath)) {
throw new Error("Access denied");
}
const fullPath = resolvePath(dirPath);
const files = await fs.readdir(fullPath, { withFileTypes: true });
return {
content: [
{
type: "text",
text: JSON.stringify(
files.map((f) => ({
name: f.name,
isDirectory: f.isDirectory(),
})),
null,
2
),
},
],
};
};
export const read_file = async (args) => {
const filePath = args?.path;
if (!filePath || !isSafePath(filePath)) {
throw new Error("Invalid path");
}
let content = await fs.readFile(resolvePath(filePath), "utf-8");
// Attempt to decode if it's a JS file (for DS sources)
if (filePath.endsWith('.js')) {
content = await decodeDsSource(content);
}
return {
content: [
{
type: "text",
text: content,
},
],
};
};
export const write_file = async (args) => {
const filePath = args?.path;
const content = args?.content;
if (!filePath || !isSafePath(filePath)) {
throw new Error("Invalid path");
}
await fs.outputFile(resolvePath(filePath), content);
return {
content: [
{
type: "text",
text: `Successfully wrote to ${filePath}`,
},
],
};
};
export const delete_file = async (args) => {
const filePath = args?.path;
if (!filePath || !isSafePath(filePath)) {
throw new Error("Invalid path");
}
await fs.remove(resolvePath(filePath));
return {
content: [
{
type: "text",
text: `Successfully deleted ${filePath}`,
},
],
};
};