Fix tool calls using the wrong directory

This commit is contained in:
Alex Gleason committed 2025-07-17 12:11:40 -05:00
1 parent 96b7d31475
commit cdbcbfa3ee
3 files changed
+131 -148

No files matched your search

+23 -47
View File
@@ -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);
},
}),
},
+21 -46
View File
@@ -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);
},
}),
},
+87 -55
View File
@@ -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<unknown> {
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<string> {
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<string> {
const projectId = this.getCurrentProjectId();
return await fsManager.readFile(projectId, filePath);
}
private async writeFile(projectId: string, filePath: string, content: string): Promise<void> {
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<void> {
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<string[]> {
async listFiles(dirPath: string = ''): Promise<string[]> {
const projectId = this.getCurrentProjectId();
return await fsManager.listFiles(projectId, dirPath);
}
private async fileExists(projectId: string, filePath: string): Promise<boolean> {
async fileExists(filePath: string): Promise<boolean> {
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<Record<string, unknown>> {
async getProjectStructure(): Promise<Record<string, unknown>> {
const projectId = this.getCurrentProjectId();
const structure: Record<string, unknown> = {};
async function buildStructure(currentPath: string, obj: Record<string, unknown>) {
@@ -108,7 +82,8 @@ export class AITools {
return structure;
}
async searchFiles(projectId: string, query: string): Promise<Array<{path: string, content: string}>> {
async searchFiles(query: string): Promise<Array<{path: string, content: string}>> {
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<unknown> {
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();