Shakespeare

This commit is contained in:
Alex Gleason committed 2025-07-16 14:32:02 -05:00
1 parent ec5d8de6c2
commit f7f76fe14f
14 files changed
+2405 -8

No files matched your search

+141
View File
@@ -0,0 +1,141 @@
import { fsManager } from './fs';
export interface FileOperation {
type: 'read' | 'write' | 'delete' | 'list' | 'exists';
projectId: string;
filePath: string;
content?: string;
}
export interface BuildOperation {
type: 'build';
projectId: string;
}
export type AIToolOperation = FileOperation | BuildOperation;
export class AITools {
async executeOperation(operation: AIToolOperation): Promise<any> {
try {
switch (operation.type) {
case 'read':
return await this.readFile(operation.projectId, operation.filePath);
case 'write':
if (!operation.content) {
throw new Error('Content is required for write operations');
}
await this.writeFile(operation.projectId, operation.filePath, operation.content);
return { success: true, message: 'File written successfully' };
case 'delete':
await this.deleteFile(operation.projectId, operation.filePath);
return { success: true, message: 'File deleted successfully' };
case 'list':
return await this.listFiles(operation.projectId, operation.filePath);
case 'exists':
return await this.fileExists(operation.projectId, operation.filePath);
case 'build':
return await this.buildProject(operation.projectId);
default:
throw new Error(`Unknown operation type: ${(operation as any).type}`);
}
} catch (error) {
return { error: error.message };
}
}
private async readFile(projectId: string, filePath: string): Promise<string> {
return await fsManager.readFile(projectId, filePath);
}
private async writeFile(projectId: string, filePath: string, content: string): Promise<void> {
await fsManager.writeFile(projectId, filePath, content);
}
private async deleteFile(projectId: string, filePath: string): Promise<void> {
await fsManager.deleteFile(projectId, filePath);
}
private async listFiles(projectId: string, dirPath: string = ''): Promise<string[]> {
return await fsManager.listFiles(projectId, dirPath);
}
private async fileExists(projectId: string, filePath: string): Promise<boolean> {
return await fsManager.fileExists(projectId, filePath);
}
private async buildProject(projectId: string): Promise<any> {
// TODO: Implement Vite build in browser
// For now, return a stub
return {
success: true,
message: 'Build started (stub implementation)',
url: `/preview/${projectId}`
};
}
async getProjectStructure(projectId: string): Promise<any> {
const structure: any = {};
async function buildStructure(currentPath: string, obj: any) {
const items = await fsManager.listFiles(projectId, currentPath);
for (const item of items) {
const itemPath = currentPath ? `${currentPath}/${item}` : item;
const fullPath = `${fsManager['dir']}/${projectId}/${itemPath}`;
try {
const stat = await fsManager['fs'].promises.stat(fullPath);
if (stat.isDirectory()) {
obj[item] = {};
await buildStructure(itemPath, obj[item]);
} else {
obj[item] = 'file';
}
} catch (error) {
// Skip inaccessible items
}
}
}
await buildStructure('', structure);
return structure;
}
async searchFiles(projectId: string, query: string): Promise<Array<{path: string, content: string}>> {
const results: Array<{path: string, content: string}> = [];
async function searchDirectory(dirPath: string) {
const items = await fsManager.listFiles(projectId, dirPath);
for (const item of items) {
const itemPath = dirPath ? `${dirPath}/${item}` : item;
const fullPath = `${fsManager['dir']}/${projectId}/${itemPath}`;
try {
const stat = await fsManager['fs'].promises.stat(fullPath);
if (stat.isDirectory()) {
await searchDirectory(itemPath);
} else {
const content = await fsManager.readFile(projectId, itemPath);
if (content.toLowerCase().includes(query.toLowerCase())) {
results.push({ path: itemPath, content });
}
}
} catch (error) {
// Skip inaccessible files
}
}
}
await searchDirectory('');
return results;
}
}
export const aiTools = new AITools();