contaigents
Version:
Modular AI Content Ecosystem with Audio Generation
149 lines (144 loc) ⢠6.96 kB
JavaScript
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 = await 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 };