-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathfsTools.js
More file actions
123 lines (113 loc) · 3.4 KB
/
fsTools.js
File metadata and controls
123 lines (113 loc) · 3.4 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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");
}
// Check if it's an image file
const imageExts = ['.png', '.jpg', '.jpeg', '.gif', '.svg', '.webp', '.ico', '.bmp'];
const isImage = imageExts.some(ext => filePath.toLowerCase().endsWith(ext));
if (isImage) {
// Read as buffer and convert to base64
const buffer = await fs.readFile(resolvePath(filePath));
const base64 = buffer.toString('base64');
// Get mime type
const ext = filePath.split('.').pop().toLowerCase();
const mimeTypes = {
'png': 'image/png',
'jpg': 'image/jpeg',
'jpeg': 'image/jpeg',
'gif': 'image/gif',
'svg': 'image/svg+xml',
'webp': 'image/webp',
'ico': 'image/x-icon',
'bmp': 'image/bmp'
};
const mimeType = mimeTypes[ext] || 'image/png';
return {
content: [
{
type: "text",
text: JSON.stringify({
type: 'image',
mimeType,
dataUrl: `data:${mimeType};base64,${base64}`
}),
},
],
};
}
// Read as text for non-image files
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: JSON.stringify({
type: '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}`,
},
],
};
};