UNPKG

contaigents

Version:

Modular AI Content Ecosystem with Audio Generation

149 lines (144 loc) • 6.95 kB
import { ChatService } from '../services/chatService.js'; import { ToolManager } from '../services/tools/ToolManager.js'; import fs from 'fs/promises'; async function debugChatServiceWrite() { console.log('šŸ” Debugging ChatService File Write Issue...\n'); try { // Test 1: Check current README.md content console.log('šŸ“– Test 1: Current README.md content'); try { const currentContent = await fs.readFile('README.md', 'utf-8'); console.log('Current content:'); console.log('─'.repeat(30)); console.log(currentContent); console.log('─'.repeat(30)); console.log(`Length: ${currentContent.length} characters`); console.log(`Lines: ${currentContent.split('\n').length}`); } catch (error) { console.log('āŒ Error reading README.md:', error); } // Test 2: Test ToolManager directly console.log('\n⚔ Test 2: Direct ToolManager test'); const toolManager = new ToolManager(process.cwd()); // Test appending to README.md console.log('Testing direct tool execution...'); const directResult = await toolManager.executeTool({ tool_name: 'write_file', parameters: { operation_type: 'partial', file_path: 'README.md', content: 'Hello World\nHey sooraj', start_line: 1, end_line: 1, create_backup: true } }); console.log('Direct tool result:'); console.log('Success:', directResult.success); console.log('Message:', directResult.message); console.log('Error:', directResult.error); console.log('Data:', directResult.data); // Test 3: Check README.md content after direct tool execution console.log('\nšŸ“– Test 3: README.md content after direct tool execution'); try { const updatedContent = await fs.readFile('README.md', 'utf-8'); console.log('Updated content:'); console.log('─'.repeat(30)); console.log(updatedContent); console.log('─'.repeat(30)); console.log(`Length: ${updatedContent.length} characters`); console.log(`Lines: ${updatedContent.split('\n').length}`); } catch (error) { console.log('āŒ Error reading updated README.md:', error); } // Test 4: Test tool call parsing console.log('\nšŸ” Test 4: Tool call parsing test'); const testResponse = `I'll add "Hey sooraj" to the README.md file. [TOOL:write_file:{"operation_type":"partial","file_path":"README.md","content":"Hello World\\nHey sooraj","start_line":1,"end_line":1}] The line has been added successfully.`; console.log('Test response:'); console.log(testResponse); const parsedCalls = toolManager.parseToolCalls(testResponse); console.log(`\nParsed ${parsedCalls.length} tool calls:`); parsedCalls.forEach((call, index) => { console.log(`${index + 1}. Tool: ${call.tool_name}`); console.log(` Parameters:`, call.parameters); }); // Test 5: Execute parsed tool calls console.log('\n⚔ Test 5: Execute parsed tool calls'); for (let i = 0; i < parsedCalls.length; i++) { const call = parsedCalls[i]; console.log(`Executing ${call.tool_name}...`); const result = await toolManager.executeTool(call); console.log(`Result: ${result.success ? 'āœ…' : 'āŒ'} ${result.message || result.error}`); } // Test 6: Final README.md content check console.log('\nšŸ“– Test 6: Final README.md content'); try { const finalContent = await fs.readFile('README.md', 'utf-8'); console.log('Final content:'); console.log('─'.repeat(30)); console.log(finalContent); console.log('─'.repeat(30)); console.log(`Length: ${finalContent.length} characters`); console.log(`Lines: ${finalContent.split('\n').length}`); } catch (error) { console.log('āŒ Error reading final README.md:', error); } // Test 7: Test ChatService with mock LLM console.log('\nšŸ¤– Test 7: ChatService with mock LLM'); class MockLLM { async executePrompt(prompt) { return { content: `I'll add "Hey sooraj" to the README.md file. [TOOL:write_file:{"operation_type":"partial","file_path":"README.md","content":"Hello World\\nHey sooraj","start_line":1,"end_line":1,"create_backup":true}] The line has been added successfully.` }; } } // Temporarily replace LLM factory const LLMFactory = (await import('../services/llm/LLMFactory.js')).LLMFactory; const originalGetConfiguredProvider = LLMFactory.getConfiguredProvider; LLMFactory.getConfiguredProvider = async () => new MockLLM(); const chatService = new ChatService(process.cwd()); const session = chatService.createSession(); console.log('Sending message to ChatService...'); const response = await chatService.sendMessage(session.id, 'Add "Hey sooraj" to README.md'); console.log('ChatService response:'); console.log('─'.repeat(50)); console.log(response.content); console.log('─'.repeat(50)); // Restore original LLM factory LLMFactory.getConfiguredProvider = originalGetConfiguredProvider; // Test 8: Final final README.md content check console.log('\nšŸ“– Test 8: README.md content after ChatService'); try { const chatServiceContent = await fs.readFile('README.md', 'utf-8'); console.log('Content after ChatService:'); console.log('─'.repeat(30)); console.log(chatServiceContent); console.log('─'.repeat(30)); console.log(`Length: ${chatServiceContent.length} characters`); console.log(`Lines: ${chatServiceContent.split('\n').length}`); } catch (error) { console.log('āŒ Error reading README.md after ChatService:', error); } console.log('\nšŸŽÆ Debug Summary:'); console.log('- Direct tool execution should show if the tool works'); console.log('- Tool call parsing should show if parsing works'); console.log('- ChatService test should show the full flow'); console.log('- File content checks show actual file changes'); } catch (error) { console.error('āŒ Debug test failed:', error); } } // Run the debug if this file is executed directly if (import.meta.url === `file://${process.argv[1]}`) { debugChatServiceWrite().catch(console.error); } export { debugChatServiceWrite };