-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathVfsToolsModule.ts
More file actions
318 lines (305 loc) · 9.85 KB
/
VfsToolsModule.ts
File metadata and controls
318 lines (305 loc) · 9.85 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// src/controls/modules/VfsToolsModule.ts
// FULL FILE
import { type ControlModule } from "@/types/litechat/control";
import {
type LiteChatModApi,
type ReadonlyChatContextSnapshot,
} from "@/types/litechat/modding";
import * as VfsOps from "@/lib/litechat/vfs-operations";
import { z } from "zod";
import { Tool } from "ai";
import { normalizePath, joinPath } from "@/lib/litechat/file-manager-utils";
const listFilesSchema = z.object({
path: z
.string()
.optional()
.describe(
"The directory path to list within the VFS. Defaults to the current directory if omitted."
),
});
const readFileSchema = z.object({
path: z.string().describe("The path of the file to read within the VFS."),
encoding: z
.enum(["utf-8", "base64"])
.optional()
.default("utf-8")
.describe("Encoding for reading the file (utf-8 or base64)."),
});
const writeFileSchema = z.object({
path: z.string().describe("The path where the file should be written."),
content: z.string().describe("The content to write to the file."),
encoding: z
.enum(["utf-8", "base64"])
.optional()
.default("utf-8")
.describe("Encoding of the provided content (utf-8 or base64)."),
});
const deleteFileSchema = z.object({
path: z.string().describe("The path of the file or directory to delete."),
recursive: z
.boolean()
.optional()
.default(false)
.describe("Whether to delete directories recursively."),
});
const createDirectorySchema = z.object({
path: z
.string()
.describe("The path of the directory to create (including parents)."),
});
const renameSchema = z.object({
oldPath: z.string().describe("The current path of the item to rename."),
newName: z.string().describe("The new name for the item."),
});
type ToolContext = ReadonlyChatContextSnapshot & {
fsInstance?: typeof VfsOps.VFS;
};
export class VfsToolsModule implements ControlModule {
readonly id = "core-vfs-tools";
private unregisterCallbacks: (() => void)[] = [];
async initialize(_modApi: LiteChatModApi): Promise<void> {
// modApi parameter is available here if needed for initialization logic
console.log(`[${this.id}] Initialized.`);
}
register(modApi: LiteChatModApi): void {
if (this.unregisterCallbacks.length > 0) {
console.warn(`[${this.id}] Already registered. Skipping.`);
return;
}
console.log(`[${this.id}] Registering Core VFS Tools...`);
const listFilesTool: Tool<any> = {
description:
"List files and directories in a specified VFS path, or the current path if none is given.",
inputSchema: listFilesSchema,
};
this.unregisterCallbacks.push(
modApi.registerTool(
"vfsListFiles",
listFilesTool,
async (
{ path }: z.infer<typeof listFilesSchema>,
context: ToolContext
) => {
const fsInstance = context?.fsInstance;
if (!fsInstance) {
return {
success: false,
error: "Filesystem instance not available in context.",
};
}
const targetPath = normalizePath(path || "/");
try {
const entries = await VfsOps.listFilesOp(targetPath, {
fsInstance,
});
return {
success: true,
path: targetPath,
entries: entries.map((e) => ({
name: e.name,
type: e.isDirectory ? "folder" : "file",
size: e.size,
lastModified: e.lastModified.toISOString(),
})),
};
} catch (e: any) {
return { success: false, path: targetPath, error: e.message };
}
}
)
);
const readFileTool: Tool<any> = {
description: "Read the content of a file from the VFS.",
inputSchema: readFileSchema,
};
this.unregisterCallbacks.push(
modApi.registerTool(
"vfsReadFile",
readFileTool,
async (
{ path, encoding }: z.infer<typeof readFileSchema>,
context: ToolContext
) => {
const fsInstance = context?.fsInstance;
if (!fsInstance) {
return {
success: false,
error: "Filesystem instance not available in context.",
};
}
const normalizedPath = normalizePath(path);
try {
const contentBytes = await VfsOps.readFileOp(normalizedPath, {
fsInstance,
});
let content: string;
if (encoding === "base64") {
content = btoa(String.fromCharCode(...contentBytes));
} else {
content = new TextDecoder().decode(contentBytes);
}
return { success: true, path: normalizedPath, content, encoding };
} catch (e: any) {
return { success: false, path: normalizedPath, error: e.message };
}
}
)
);
const writeFileTool: Tool<any> = {
description: "Write content to a file in the VFS.",
inputSchema: writeFileSchema,
};
this.unregisterCallbacks.push(
modApi.registerTool(
"vfsWriteFile",
writeFileTool,
async (
{ path, content, encoding }: z.infer<typeof writeFileSchema>,
context: ToolContext
) => {
const fsInstance = context?.fsInstance;
if (!fsInstance) {
return {
success: false,
error: "Filesystem instance not available in context.",
};
}
const normalizedPath = normalizePath(path);
try {
let dataToWrite: Uint8Array | string;
if (encoding === "base64") {
const binaryString = atob(content);
const len = binaryString.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
dataToWrite = bytes;
} else {
dataToWrite = content;
}
await VfsOps.writeFileOp(normalizedPath, dataToWrite, {
fsInstance,
});
return { success: true, path: normalizedPath };
} catch (e: any) {
return { success: false, path: normalizedPath, error: e.message };
}
}
)
);
const deleteFileTool: Tool<any> = {
description: "Delete a file or directory from the VFS.",
inputSchema: deleteFileSchema,
};
this.unregisterCallbacks.push(
modApi.registerTool(
"vfsDelete",
deleteFileTool,
async (
{ path, recursive }: z.infer<typeof deleteFileSchema>,
context: ToolContext
) => {
const fsInstance = context?.fsInstance;
if (!fsInstance) {
return {
success: false,
error: "Filesystem instance not available in context.",
};
}
const normalizedPath = normalizePath(path);
try {
await VfsOps.deleteItemOp(normalizedPath, recursive, {
fsInstance,
});
return { success: true, path: normalizedPath };
} catch (e: any) {
return { success: false, path: normalizedPath, error: e.message };
}
}
)
);
const createDirectoryTool: Tool<any> = {
description: "Create a directory (including parents) in the VFS.",
inputSchema: createDirectorySchema,
};
this.unregisterCallbacks.push(
modApi.registerTool(
"vfsCreateDirectory",
createDirectoryTool,
async (
{ path }: z.infer<typeof createDirectorySchema>,
context: ToolContext
) => {
const fsInstance = context?.fsInstance;
if (!fsInstance) {
return {
success: false,
error: "Filesystem instance not available in context.",
};
}
const normalizedPath = normalizePath(path);
try {
await VfsOps.createDirectoryOp(normalizedPath, { fsInstance });
return { success: true, path: normalizedPath };
} catch (e: any) {
return { success: false, path: normalizedPath, error: e.message };
}
}
)
);
const renameTool: Tool<any> = {
description: "Rename a file or directory in the VFS.",
inputSchema: renameSchema,
};
this.unregisterCallbacks.push(
modApi.registerTool(
"vfsRename",
renameTool,
async (
{ oldPath, newName }: z.infer<typeof renameSchema>,
context: ToolContext
) => {
const fsInstance = context?.fsInstance;
if (!fsInstance) {
return {
success: false,
error: "Filesystem instance not available in context.",
};
}
const normalizedOldPath = normalizePath(oldPath);
const parentPath = normalizePath(
normalizedOldPath.substring(
0,
normalizedOldPath.lastIndexOf("/")
) || "/"
);
const normalizedNewPath = joinPath(parentPath, newName);
try {
await VfsOps.renameOp(normalizedOldPath, normalizedNewPath, {
fsInstance,
});
return {
success: true,
oldPath: normalizedOldPath,
newPath: normalizedNewPath,
};
} catch (e: any) {
return {
success: false,
oldPath: normalizedOldPath,
newName: newName,
error: e.message,
};
}
}
)
);
console.log(`[${this.id}] Core VFS Tools Registered.`);
}
destroy(): void {
this.unregisterCallbacks.forEach((unsub) => unsub());
this.unregisterCallbacks = [];
console.log(`[${this.id}] Destroyed.`);
}
}