From 3bf924ef3afe8dcbd542443d47ecdab8ca41ae1e Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 20 Jul 2025 17:22:28 -0500 Subject: [PATCH] fs.ts -> ProjectsManager.ts --- src/hooks/useProjectsManager.ts | 6 +- src/lib/{fs.ts => ProjectsManager.ts} | 2 +- src/lib/ai-tools.ts | 215 -------------------------- src/pages/Index.tsx | 2 +- src/pages/ProjectView.tsx | 2 +- 5 files changed, 6 insertions(+), 221 deletions(-) rename src/lib/{fs.ts => ProjectsManager.ts} (99%) delete mode 100644 src/lib/ai-tools.ts diff --git a/src/hooks/useProjectsManager.ts b/src/hooks/useProjectsManager.ts index 75521fee..d7d133e6 100644 --- a/src/hooks/useProjectsManager.ts +++ b/src/hooks/useProjectsManager.ts @@ -1,5 +1,5 @@ import { useMemo } from 'react'; -import { ProjectsManager } from '@/lib/fs'; +import { ProjectsManager } from '@/lib/ProjectsManager'; import { useFS } from '@/hooks/useFS'; /** @@ -8,8 +8,8 @@ import { useFS } from '@/hooks/useFS'; */ export function useProjectsManager() { const { fs } = useFS(); - + const projectsManager = useMemo(() => new ProjectsManager(fs), [fs]); - + return projectsManager; } \ No newline at end of file diff --git a/src/lib/fs.ts b/src/lib/ProjectsManager.ts similarity index 99% rename from src/lib/fs.ts rename to src/lib/ProjectsManager.ts index 3f700a58..2776dc42 100644 --- a/src/lib/fs.ts +++ b/src/lib/ProjectsManager.ts @@ -198,4 +198,4 @@ export class ProjectsManager { // Ignore errors updating last modified } } -} +} \ No newline at end of file diff --git a/src/lib/ai-tools.ts b/src/lib/ai-tools.ts deleted file mode 100644 index 2ef9ef2c..00000000 --- a/src/lib/ai-tools.ts +++ /dev/null @@ -1,215 +0,0 @@ -import { ProjectsManager } from './fs'; - -// Context-aware AI tools that automatically use the current project ID -class ContextualAITools { - private currentProjectId: string | null = null; - private projectsManager: ProjectsManager; - - constructor(projectsManager: ProjectsManager) { - this.projectsManager = projectsManager; - } - - setCurrentProjectId(projectId: string) { - this.currentProjectId = projectId; - } - - getCurrentProjectId(): string { - if (!this.currentProjectId) { - throw new Error('No project context set. Please ensure you are in a project view.'); - } - return this.currentProjectId; - } - - async readFile(filePath: string): Promise { - const projectId = this.getCurrentProjectId(); - try { - return await this.projectsManager.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(); - try { - await this.projectsManager.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(); - try { - await this.projectsManager.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 { - const projectId = this.getCurrentProjectId(); - try { - return await this.projectsManager.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 { - const projectId = this.getCurrentProjectId(); - try { - return await this.projectsManager.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 }> { - const projectId = this.getCurrentProjectId(); - // TODO: Implement Vite build in browser - return { - success: true, - message: 'Build started (stub implementation)', - url: `/preview/${projectId}` - }; - } - - async getProjectStructure(): Promise> { - const projectId = this.getCurrentProjectId(); - const structure: Record = {}; - - const buildStructure = async (currentPath: string, obj: Record) => { - const items = await this.projectsManager.listFiles(projectId, currentPath); - - for (const item of items) { - const itemPath = currentPath ? `${currentPath}/${item}` : item; - const fullPath = `${this.projectsManager['dir']}/${projectId}/${itemPath}`; - - try { - const stat = await this.projectsManager['fs'].stat(fullPath); - if (stat.isDirectory()) { - const newObj: Record = {}; - obj[item] = newObj; - await buildStructure(itemPath, newObj); - } else { - obj[item] = 'file'; - } - } catch { - // Skip inaccessible items - } - } - }; - - await buildStructure('', structure); - return structure; - } - - async searchFiles(query: string): Promise> { - const projectId = this.getCurrentProjectId(); - const results: Array<{path: string, content: string}> = []; - - const searchDirectory = async (dirPath: string) => { - const items = await this.projectsManager.listFiles(projectId, dirPath); - - for (const item of items) { - const itemPath = dirPath ? `${dirPath}/${item}` : item; - const fullPath = `${this.projectsManager['dir']}/${projectId}/${itemPath}`; - - try { - const stat = await this.projectsManager['fs'].stat(fullPath); - if (stat.isDirectory()) { - await searchDirectory(itemPath); - } else { - const content = await this.projectsManager.readFile(projectId, itemPath); - if (content.toLowerCase().includes(query.toLowerCase())) { - results.push({ path: itemPath, content }); - } - } - } catch { - // Skip inaccessible files - } - } - }; - - await searchDirectory(''); - return results; - } -} - -// Factory function to create ContextualAITools with ProjectsManager -export function createContextualAITools(projectsManager: ProjectsManager): ContextualAITools { - return new ContextualAITools(projectsManager); -} - -// Backward compatibility: export the old interface for existing code -interface LegacyOperation { - type: string; - projectId?: string; - filePath?: string; - content?: string; - query?: string; -} - -export class AITools { - private contextualAITools: ContextualAITools; - - constructor(projectsManager: ProjectsManager) { - this.contextualAITools = new ContextualAITools(projectsManager); - } - - async executeOperation(operation: LegacyOperation): Promise { - try { - switch (operation.type) { - case 'read': - if (!operation.filePath) throw new Error('filePath is required'); - return await this.contextualAITools.readFile(operation.filePath); - - case 'write': - if (!operation.filePath) throw new Error('filePath is required'); - if (!operation.content) { - throw new Error('Content is required for write operations'); - } - return await this.contextualAITools.writeFile(operation.filePath, operation.content); - - case 'delete': - if (!operation.filePath) throw new Error('filePath is required'); - return await this.contextualAITools.deleteFile(operation.filePath); - - case 'list': - return await this.contextualAITools.listFiles(operation.filePath); - - case 'exists': - if (!operation.filePath) throw new Error('filePath is required'); - return await this.contextualAITools.fileExists(operation.filePath); - - case 'build': - return await this.contextualAITools.buildProject(); - - case 'getProjectStructure': - return await this.contextualAITools.getProjectStructure(); - - case 'searchFiles': - if (!operation.query) throw new Error('query is required'); - return await this.contextualAITools.searchFiles(operation.query); - - default: - throw new Error(`Unknown operation type: ${operation.type}`); - } - } catch (error) { - return { error: error.message }; - } - } -} - -// Factory function to create AITools with ProjectsManager -export function createAITools(projectsManager: ProjectsManager): AITools { - return new AITools(projectsManager); -} \ No newline at end of file diff --git a/src/pages/Index.tsx b/src/pages/Index.tsx index 7c37b239..58b7b4d3 100644 --- a/src/pages/Index.tsx +++ b/src/pages/Index.tsx @@ -1,6 +1,6 @@ import { useState, useEffect, useCallback } from 'react'; import { useNavigate } from 'react-router-dom'; -import { type Project } from '@/lib/fs'; +import { type Project } from '@/lib/ProjectsManager'; import { useProjectsManager } from '@/hooks/useProjectsManager'; import { Button } from '@/components/ui/button'; import { Textarea } from '@/components/ui/textarea'; diff --git a/src/pages/ProjectView.tsx b/src/pages/ProjectView.tsx index 89f2f613..632a46e3 100644 --- a/src/pages/ProjectView.tsx +++ b/src/pages/ProjectView.tsx @@ -1,6 +1,6 @@ import { useState, useEffect, useCallback } from 'react'; import { useParams } from 'react-router-dom'; -import { type Project } from '@/lib/fs'; +import { type Project } from '@/lib/ProjectsManager'; import { useProjectsManager } from '@/hooks/useProjectsManager'; import { ChatPane } from '@/components/Shakespeare/ChatPane'; import { PreviewPane } from '@/components/Shakespeare/PreviewPane';