Add a tidier FsToolSet

This commit is contained in:
Alex Gleason committed 2025-07-17 13:46:55 -05:00
1 parent 475b998c1a
commit 0ae7a45dd8
6 files changed
+115 -84

No files matched your search

+37
View File
@@ -0,0 +1,37 @@
import type LightningFS from '@isomorphic-git/lightning-fs';
import { join } from "@std/path/posix/join";
import { tool } from "ai";
import z from 'zod';
export class FsToolSet {
constructor(readonly fs: LightningFS.PromisifiedFS, private cwd?: string) {}
readonly readFile = tool({
description: 'Read the contents of a file in the project',
parameters: FsToolSet.readFileSchema,
execute: (args) => {
const filepath = join(this.cwd || '', args.filePath);
return this.fs.readFile(filepath, 'utf8');
},
});
readonly writeFile = tool({
description: 'Write content to a file in the project',
parameters: FsToolSet.writeFileSchema,
execute: async (args) => {
const filepath = join(this.cwd || '', args.filePath);
await this.fs.writeFile(filepath, args.content, 'utf8');
return `File ${args.filePath} written successfully`;
},
});
static readFileSchema = z.object({
filePath: z.string(),
content: z.string().optional(),
});
static writeFileSchema = z.object({
filePath: z.string(),
content: z.string(),
});
}
+32 -7
View File
@@ -17,29 +17,54 @@ class ContextualAITools {
async readFile(filePath: string): Promise<string> {
const projectId = this.getCurrentProjectId();
return await fsManager.readFile(projectId, filePath);
try {
return await fsManager.readFile(projectId, filePath);
} catch (error) {
const fsError = error as NodeJS.ErrnoException;
throw new Error(`Failed to read file "${filePath}": ${fsError.message}`);
}
}
async writeFile(filePath: string, content: string): Promise<{ success: boolean; message: string }> {
const projectId = this.getCurrentProjectId();
await fsManager.writeFile(projectId, filePath, content);
return { success: true, message: `File ${filePath} written successfully` };
try {
await fsManager.writeFile(projectId, filePath, content);
return { success: true, message: `File ${filePath} written successfully` };
} catch (error) {
const fsError = error as NodeJS.ErrnoException;
throw new Error(`Failed to write file "${filePath}": ${fsError.message}`);
}
}
async deleteFile(filePath: string): Promise<{ success: boolean; message: string }> {
const projectId = this.getCurrentProjectId();
await fsManager.deleteFile(projectId, filePath);
return { success: true, message: `File ${filePath} deleted successfully` };
try {
await fsManager.deleteFile(projectId, filePath);
return { success: true, message: `File ${filePath} deleted successfully` };
} catch (error) {
const fsError = error as NodeJS.ErrnoException;
throw new Error(`Failed to delete file "${filePath}": ${fsError.message}`);
}
}
async listFiles(dirPath: string = ''): Promise<string[]> {
const projectId = this.getCurrentProjectId();
return await fsManager.listFiles(projectId, dirPath);
try {
return await fsManager.listFiles(projectId, dirPath);
} catch (error) {
const fsError = error as NodeJS.ErrnoException;
throw new Error(`Failed to list files in "${dirPath}": ${fsError.message}`);
}
}
async fileExists(filePath: string): Promise<boolean> {
const projectId = this.getCurrentProjectId();
return await fsManager.fileExists(projectId, filePath);
try {
return await fsManager.fileExists(projectId, filePath);
} catch (error) {
const fsError = error as NodeJS.ErrnoException;
throw new Error(`Failed to check if file exists "${filePath}": ${fsError.message}`);
}
}
async buildProject(): Promise<{ success: boolean; message: string; url: string }> {
+23 -6
View File
@@ -17,8 +17,8 @@ export interface Project {
}
export class FileSystemManager {
private fs: LightningFS;
private dir: string;
fs: LightningFS;
dir: string;
constructor() {
this.fs = new LightningFS('shakespeare-fs');
@@ -197,7 +197,15 @@ export default defineConfig({
async readFile(projectId: string, filePath: string): Promise<string> {
const fullPath = `${this.dir}/${projectId}/${filePath}`;
return await this.fs.promises.readFile(fullPath, 'utf8');
try {
return await this.fs.promises.readFile(fullPath, 'utf8');
} catch (error) {
const fsError = error as NodeJS.ErrnoException;
if (fsError.code === 'ENOENT') {
throw new Error(`File not found: ${filePath}`);
}
throw error;
}
}
async writeFile(projectId: string, filePath: string, content: string): Promise<void> {
@@ -220,8 +228,12 @@ export default defineConfig({
const fullPath = `${this.dir}/${projectId}/${dirPath}`;
try {
return await this.fs.promises.readdir(fullPath);
} catch {
return [];
} catch (error) {
const fsError = error as NodeJS.ErrnoException;
if (fsError.code === 'ENOENT') {
return [];
}
throw error;
}
}
@@ -230,7 +242,12 @@ export default defineConfig({
const fullPath = `${this.dir}/${projectId}/${filePath}`;
await this.fs.promises.stat(fullPath);
return true;
} catch {
} catch (error) {
const fsError = error as NodeJS.ErrnoException;
if (fsError.code === 'ENOENT') {
return false;
}
// For other errors, we still want to know the file doesn't exist
return false;
}
}