UNPKG

contaigents

Version:

Modular AI Content Ecosystem with Audio Generation

217 lines (205 loc) โ€ข 8.31 kB
import { ToolManager } from '../services/tools/ToolManager.js'; import fs from 'fs/promises'; import path from 'path'; async function testCombinedTools() { console.log('๐Ÿงช Testing Combined Read/Write Tool Workflow...\n'); const testDir = path.join(process.cwd(), 'test-combined-tools'); const toolManager = new ToolManager(process.cwd()); try { // Setup test environment await fs.mkdir(testDir, { recursive: true }); console.log('โœ… Created test directory:', testDir); // Step 1: Create an initial file console.log('\n๐Ÿ“ Step 1: Creating initial file...'); const createResult = await toolManager.executeTool({ tool_name: 'write_file', parameters: { operation_type: 'create', file_path: 'test-combined-tools/document.md', content: `# Project Documentation ## Overview This is the initial version of our project documentation. ## Features - Basic functionality - Simple structure ## TODO - Add more details - Include examples ` } }); if (createResult.success) { console.log('โœ… Initial file created successfully!'); console.log('๐Ÿ’ฌ Message:', createResult.message); } else { console.log('โŒ Failed to create initial file:', createResult.error); return; } // Step 2: Read the file to verify content console.log('\n๐Ÿ“– Step 2: Reading the created file...'); const readResult = await toolManager.executeTool({ tool_name: 'read_file', parameters: { file_path: 'test-combined-tools/document.md' } }); if (readResult.success) { console.log('โœ… File read successfully!'); console.log('๐Ÿ“„ Content length:', readResult.data?.content.length); console.log('๐Ÿ“ First 100 characters:', readResult.data?.content.substring(0, 100) + '...'); } else { console.log('โŒ Failed to read file:', readResult.error); return; } // Step 3: Update specific sections using partial write console.log('\nโœ๏ธ Step 3: Updating the Features section (lines 6-8)...'); const updateResult = await toolManager.executeTool({ tool_name: 'write_file', parameters: { operation_type: 'partial', file_path: 'test-combined-tools/document.md', content: `## Features - Advanced functionality - Robust architecture - Tool integration - File operations - Backup system`, start_line: 6, end_line: 8, create_backup: true } }); if (updateResult.success) { console.log('โœ… Features section updated successfully!'); console.log('๐Ÿ’ฌ Message:', updateResult.message); } else { console.log('โŒ Failed to update features section:', updateResult.error); return; } // Step 4: Read the updated file console.log('\n๐Ÿ“– Step 4: Reading the updated file...'); const readUpdatedResult = await toolManager.executeTool({ tool_name: 'read_file', parameters: { file_path: 'test-combined-tools/document.md' } }); if (readUpdatedResult.success) { console.log('โœ… Updated file read successfully!'); console.log('๐Ÿ“„ Updated content:'); console.log('โ”€'.repeat(50)); console.log(readUpdatedResult.data?.content); console.log('โ”€'.repeat(50)); } else { console.log('โŒ Failed to read updated file:', readUpdatedResult.error); return; } // Step 5: Add a new section at the end console.log('\nโž• Step 5: Adding a new section at the end...'); const addSectionResult = await toolManager.executeTool({ tool_name: 'write_file', parameters: { operation_type: 'partial', file_path: 'test-combined-tools/document.md', content: ` ## Implementation Details This section was added using the partial write functionality. ### Tool Architecture - FileReadTool: Handles file reading operations - FileWriteTool: Handles file writing operations - ToolManager: Orchestrates tool execution ### Usage Examples \`\`\`javascript // Reading a file const result = await toolManager.executeTool({ tool_name: 'read_file', parameters: { file_path: 'document.md' } }); // Writing to a file const writeResult = await toolManager.executeTool({ tool_name: 'write_file', parameters: { operation_type: 'create', file_path: 'new-file.md', content: 'Hello World!' } }); \`\`\` ## Conclusion The combined tool workflow demonstrates seamless integration between reading and writing operations. `, start_line: 15, end_line: 15, create_backup: false } }); if (addSectionResult.success) { console.log('โœ… New section added successfully!'); console.log('๐Ÿ’ฌ Message:', addSectionResult.message); } else { console.log('โŒ Failed to add new section:', addSectionResult.error); return; } // Step 6: Final read to show complete document console.log('\n๐Ÿ“– Step 6: Reading the final document...'); const finalReadResult = await toolManager.executeTool({ tool_name: 'read_file', parameters: { file_path: 'test-combined-tools/document.md' } }); if (finalReadResult.success) { console.log('โœ… Final document read successfully!'); console.log('๐Ÿ“Š Final stats:'); console.log(` - Lines: ${finalReadResult.data?.fileInfo.lines}`); console.log(` - Characters: ${finalReadResult.data?.fileInfo.size}`); console.log(` - File type: ${finalReadResult.data?.fileInfo.extension}`); } else { console.log('โŒ Failed to read final document:', finalReadResult.error); } // Step 7: Test tool call parsing with multiple tools console.log('\n๐Ÿ” Step 7: Testing tool call parsing with multiple tools...'); const sampleResponse = `I'll help you work with your files. Let me first read the document and then update it. [TOOL:read_file:{"file_path":"test-combined-tools/document.md"}] Now I'll add a summary section: [TOOL:write_file:{"operation_type":"partial","file_path":"test-combined-tools/document.md","content":"## Summary\\nThis document demonstrates the tool architecture.","start_line":1,"end_line":1}] The operations have been completed successfully.`; const parsedCalls = toolManager.parseToolCalls(sampleResponse); console.log('โœ… Parsed tool calls:', parsedCalls.length); parsedCalls.forEach((call, index) => { console.log(` ${index + 1}. ${call.tool_name} with parameters:`, Object.keys(call.parameters)); }); console.log('\n๐ŸŽ‰ Combined tools test completed successfully!'); console.log('\n๐ŸŽฏ Workflow Demonstrated:'); console.log('- โœ… File creation with write_file'); console.log('- โœ… Content verification with read_file'); console.log('- โœ… Partial updates with line-specific writes'); console.log('- โœ… Backup creation during modifications'); console.log('- โœ… Multi-tool parsing and execution'); console.log('- โœ… Seamless read/write integration'); } catch (error) { console.error('โŒ Combined tools test failed:', error); } finally { // Cleanup try { await fs.rm(testDir, { recursive: true, force: true }); console.log('\n๐Ÿงน Cleaned up test files'); } catch (cleanupError) { console.error('Failed to cleanup:', cleanupError); } } } // Run the test if this file is executed directly if (import.meta.url === `file://${process.argv[1]}`) { testCombinedTools().catch(console.error); } export { testCombinedTools };