From 187bfd649bc907f08bd97372934a56675bac4f02 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 17 Jul 2025 12:11:40 -0500 Subject: [PATCH] Fix tool calls using the wrong directory --- src/components/Shakespeare/ChatPane.tsx | 70 ++++-------- src/lib/ai-chat.ts | 67 ++++------- src/lib/ai-tools.ts | 142 +++++++++++++++--------- 3 files changed, 131 insertions(+), 148 deletions(-) diff --git a/src/components/Shakespeare/ChatPane.tsx b/src/components/Shakespeare/ChatPane.tsx index 6b02c5fd..fd67ab34 100644 --- a/src/components/Shakespeare/ChatPane.tsx +++ b/src/components/Shakespeare/ChatPane.tsx @@ -7,7 +7,7 @@ import { Card } from '@/components/ui/card'; import { Send, Bot, User, Settings } from 'lucide-react'; import { cn } from '@/lib/utils'; import { useAISettings } from '@/contexts/AISettingsContext'; -import { aiTools } from '@/lib/ai-tools'; +import { contextualAITools } from '@/lib/ai-tools'; import { z } from 'zod'; interface ToolCall { @@ -37,6 +37,9 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) { const { settings, isConfigured } = useAISettings(); useEffect(() => { + // Set the current project ID for contextual AI tools + contextualAITools.setCurrentProjectId(projectId); + // Add welcome message setMessages([ { @@ -46,7 +49,7 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) { timestamp: new Date(), }, ]); - }, [projectName]); + }, [projectName, projectId]); useEffect(() => { if (scrollAreaRef.current) { @@ -60,14 +63,11 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) { } const fileOperationSchema = z.object({ - projectId: z.string(), filePath: z.string(), content: z.string().optional(), }); - const buildSchema = z.object({ - projectId: z.string(), - }); + const buildSchema = z.object({}); // Create a custom fetch function for the AI provider const customFetch = async (url: string, options: RequestInit) => { @@ -92,88 +92,64 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) { return streamText({ model: provider, messages, + maxSteps: 100, tools: { readFile: tool({ description: 'Read the contents of a file in the project', parameters: fileOperationSchema, - execute: async ({ projectId, filePath }) => { - return await aiTools.executeOperation({ - type: 'read', - projectId, - filePath, - }); + execute: async ({ filePath }) => { + return await contextualAITools.readFile(filePath); }, }), writeFile: tool({ description: 'Write content to a file in the project', parameters: fileOperationSchema, - execute: async ({ projectId, filePath, content }) => { - return await aiTools.executeOperation({ - type: 'write', - projectId, - filePath, - content: content || '', - }); + execute: async ({ filePath, content }) => { + return await contextualAITools.writeFile(filePath, content || ''); }, }), deleteFile: tool({ description: 'Delete a file from the project', parameters: fileOperationSchema, - execute: async ({ projectId, filePath }) => { - return await aiTools.executeOperation({ - type: 'delete', - projectId, - filePath, - }); + execute: async ({ filePath }) => { + return await contextualAITools.deleteFile(filePath); }, }), listFiles: tool({ description: 'List files in a directory', parameters: fileOperationSchema, - execute: async ({ projectId, filePath }) => { - return await aiTools.executeOperation({ - type: 'list', - projectId, - filePath, - }); + execute: async ({ filePath }) => { + return await contextualAITools.listFiles(filePath); }, }), fileExists: tool({ description: 'Check if a file exists', parameters: fileOperationSchema, - execute: async ({ projectId, filePath }) => { - return await aiTools.executeOperation({ - type: 'exists', - projectId, - filePath, - }); + execute: async ({ filePath }) => { + return await contextualAITools.fileExists(filePath); }, }), buildProject: tool({ description: 'Build the project using Vite', parameters: buildSchema, - execute: async ({ projectId }) => { - return await aiTools.executeOperation({ - type: 'build', - projectId, - }); + execute: async () => { + return await contextualAITools.buildProject(); }, }), getProjectStructure: tool({ description: 'Get the complete file structure of the project', parameters: buildSchema, - execute: async ({ projectId }) => { - return await aiTools.getProjectStructure(projectId); + execute: async () => { + return await contextualAITools.getProjectStructure(); }, }), searchFiles: tool({ description: 'Search for files containing specific text', parameters: z.object({ - projectId: z.string(), query: z.string(), }), - execute: async ({ projectId, query }) => { - return await aiTools.searchFiles(projectId, query); + execute: async ({ query }) => { + return await contextualAITools.searchFiles(query); }, }), }, diff --git a/src/lib/ai-chat.ts b/src/lib/ai-chat.ts index b634be06..db3bb67e 100644 --- a/src/lib/ai-chat.ts +++ b/src/lib/ai-chat.ts @@ -1,19 +1,19 @@ import { openai } from '@ai-sdk/openai'; import { streamText, tool } from 'ai'; import { z } from 'zod'; -import { aiTools } from './ai-tools'; +import { contextualAITools } from './ai-tools'; const fileOperationSchema = z.object({ - projectId: z.string(), filePath: z.string(), content: z.string().optional(), }); -const buildSchema = z.object({ - projectId: z.string(), -}); +const buildSchema = z.object({}); export async function createAIChat(projectId: string, messages: Array<{ role: 'user' | 'assistant'; content: string }>) { + // Set the project context for the AI tools + contextualAITools.setCurrentProjectId(projectId); + const result = await streamText({ model: openai('gpt-4-turbo-preview'), messages, @@ -21,84 +21,59 @@ export async function createAIChat(projectId: string, messages: Array<{ role: 'u readFile: tool({ description: 'Read the contents of a file in the project', parameters: fileOperationSchema, - execute: async ({ projectId, filePath }) => { - return await aiTools.executeOperation({ - type: 'read', - projectId, - filePath, - }); + execute: async ({ filePath }) => { + return await contextualAITools.readFile(filePath); }, }), writeFile: tool({ description: 'Write content to a file in the project', parameters: fileOperationSchema, - execute: async ({ projectId, filePath, content }) => { - return await aiTools.executeOperation({ - type: 'write', - projectId, - filePath, - content: content || '', - }); + execute: async ({ filePath, content }) => { + return await contextualAITools.writeFile(filePath, content || ''); }, }), deleteFile: tool({ description: 'Delete a file from the project', parameters: fileOperationSchema, - execute: async ({ projectId, filePath }) => { - return await aiTools.executeOperation({ - type: 'delete', - projectId, - filePath, - }); + execute: async ({ filePath }) => { + return await contextualAITools.deleteFile(filePath); }, }), listFiles: tool({ description: 'List files in a directory', parameters: fileOperationSchema, - execute: async ({ projectId, filePath }) => { - return await aiTools.executeOperation({ - type: 'list', - projectId, - filePath, - }); + execute: async ({ filePath }) => { + return await contextualAITools.listFiles(filePath); }, }), fileExists: tool({ description: 'Check if a file exists', parameters: fileOperationSchema, - execute: async ({ projectId, filePath }) => { - return await aiTools.executeOperation({ - type: 'exists', - projectId, - filePath, - }); + execute: async ({ filePath }) => { + return await contextualAITools.fileExists(filePath); }, }), buildProject: tool({ description: 'Build the project using Vite', parameters: buildSchema, - execute: async ({ projectId }) => { - return await aiTools.executeOperation({ - type: 'build', - projectId, - }); + execute: async () => { + return await contextualAITools.buildProject(); }, }), getProjectStructure: tool({ description: 'Get the complete file structure of the project', parameters: buildSchema, - execute: async ({ projectId }) => { - return await aiTools.getProjectStructure(projectId); + execute: async () => { + return await contextualAITools.getProjectStructure(); }, }), searchFiles: tool({ description: 'Search for files containing specific text', parameters: z.object({ - projectId: z.string(), query: z.string(), }), - execute: async ({ projectId, query }) => { - return await aiTools.searchFiles(projectId, query); + execute: async ({ query }) => { + return await contextualAITools.searchFiles(query); }, }), }, diff --git a/src/lib/ai-tools.ts b/src/lib/ai-tools.ts index 969ad2c3..d1993a8a 100644 --- a/src/lib/ai-tools.ts +++ b/src/lib/ai-tools.ts @@ -1,77 +1,50 @@ import { fsManager } from './fs'; -export interface FileOperation { - type: 'read' | 'write' | 'delete' | 'list' | 'exists'; - projectId: string; - filePath: string; - content?: string; -} +// Context-aware AI tools that automatically use the current project ID +class ContextualAITools { + private currentProjectId: string | null = null; -export interface BuildOperation { - type: 'build'; - projectId: string; -} - -export type AIToolOperation = FileOperation | BuildOperation; - -export class AITools { - async executeOperation(operation: AIToolOperation): Promise { - 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 { type: string }).type}`); - } - } catch (error) { - return { error: error.message }; - } + setCurrentProjectId(projectId: string) { + this.currentProjectId = projectId; } - private async readFile(projectId: string, filePath: string): Promise { + 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(); return await fsManager.readFile(projectId, filePath); } - private async writeFile(projectId: string, filePath: string, content: string): Promise { + 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` }; } - private async deleteFile(projectId: string, filePath: string): Promise { + 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` }; } - private async listFiles(projectId: string, dirPath: string = ''): Promise { + async listFiles(dirPath: string = ''): Promise { + const projectId = this.getCurrentProjectId(); return await fsManager.listFiles(projectId, dirPath); } - private async fileExists(projectId: string, filePath: string): Promise { + async fileExists(filePath: string): Promise { + const projectId = this.getCurrentProjectId(); return await fsManager.fileExists(projectId, filePath); } - private async buildProject(projectId: string): Promise<{ success: boolean; message: string; url: string }> { + async buildProject(): Promise<{ success: boolean; message: string; url: string }> { + const projectId = this.getCurrentProjectId(); // TODO: Implement Vite build in browser - // For now, return a stub return { success: true, message: 'Build started (stub implementation)', @@ -79,7 +52,8 @@ export class AITools { }; } - async getProjectStructure(projectId: string): Promise> { + async getProjectStructure(): Promise> { + const projectId = this.getCurrentProjectId(); const structure: Record = {}; async function buildStructure(currentPath: string, obj: Record) { @@ -108,7 +82,8 @@ export class AITools { return structure; } - async searchFiles(projectId: string, query: string): Promise> { + async searchFiles(query: string): Promise> { + const projectId = this.getCurrentProjectId(); const results: Array<{path: string, content: string}> = []; async function searchDirectory(dirPath: string) { @@ -139,4 +114,61 @@ export class AITools { } } +// Create a singleton instance +export const contextualAITools = new ContextualAITools(); + +// Backward compatibility: export the old interface for existing code +interface LegacyOperation { + type: string; + projectId?: string; + filePath?: string; + content?: string; + query?: string; +} + +export class AITools { + async executeOperation(operation: LegacyOperation): Promise { + try { + switch (operation.type) { + case 'read': + if (!operation.filePath) throw new Error('filePath is required'); + return await 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 contextualAITools.writeFile(operation.filePath, operation.content); + + case 'delete': + if (!operation.filePath) throw new Error('filePath is required'); + return await contextualAITools.deleteFile(operation.filePath); + + case 'list': + return await contextualAITools.listFiles(operation.filePath); + + case 'exists': + if (!operation.filePath) throw new Error('filePath is required'); + return await contextualAITools.fileExists(operation.filePath); + + case 'build': + return await contextualAITools.buildProject(); + + case 'getProjectStructure': + return await contextualAITools.getProjectStructure(); + + case 'searchFiles': + if (!operation.query) throw new Error('query is required'); + return await contextualAITools.searchFiles(operation.query); + + default: + throw new Error(`Unknown operation type: ${operation.type}`); + } + } catch (error) { + return { error: error.message }; + } + } +} + export const aiTools = new AITools(); \ No newline at end of file