UNPKG

contaigents

Version:

Modular AI Content Ecosystem with Audio Generation

116 lines (103 loc) โ€ข 3.74 kB
import { ToolManager } from '../services/tools/ToolManager.js'; import fs from 'fs/promises'; import path from 'path'; async function testToolCallParsing() { console.log('๐Ÿงช Testing Tool Call Parsing'); console.log('='.repeat(50)); // Create a test directory const testDir = path.join(process.cwd(), 'test-xml-tools'); await fs.mkdir(testDir, { recursive: true }); const toolManager = new ToolManager(testDir); // Test cases with different formats const testCases = [ { name: 'Simple XML Tool Call', content: `I'll read the README file for you. <tool_call name="read_file" id="1"> <file_path>README.md</file_path> </tool_call> Let me know what you think!` }, { name: 'XML Tool Call with Complex Content', content: `I'll create a new file with complex content. <tool_call name="write_file" id="2"> <operation_type>create</operation_type> <file_path>complex-example.md</file_path> <content><![CDATA[ # Complex Content Example This content has: - "Quotes" and 'single quotes' - [Brackets] and {braces} - <HTML tags> and XML - JSON: {"key": "value", "array": [1, 2, 3]} - Multiple lines with various characters! ## Code Example function test() { console.log("Hello [world]!"); } ]]></content> </tool_call> The file has been created with complex content.` }, { name: 'Multiple XML Tool Calls', content: `First, let me read a file: <tool_call name="read_file" id="3"> <file_path>README.md</file_path> </tool_call> Then create another file: <tool_call name="write_file" id="4"> <operation_type>create</operation_type> <file_path>multiple-test.txt</file_path> <content>Multiple XML tool calls test</content> </tool_call> Both tool calls should work together.` }, { name: 'XML with Numeric and Boolean Parameters', content: `Testing different parameter types: <tool_call name="write_file"> <operation_type>partial</operation_type> <file_path>test-file.md</file_path> <start_line>5</start_line> <end_line>10</end_line> <content>Updated content for lines 5-10</content> </tool_call> Parameters should be parsed correctly.` } ]; for (const testCase of testCases) { console.log(`\n๐Ÿ“‹ Test: ${testCase.name}`); console.log('-'.repeat(30)); const toolCalls = toolManager.parseToolCalls(testCase.content); console.log(`Found ${toolCalls.length} tool call(s):`); toolCalls.forEach((call, index) => { console.log(` ${index + 1}. Tool: ${call.tool_name}`); console.log(` Parameters:`, JSON.stringify(call.parameters, null, 2)); }); if (toolCalls.length === 0) { console.log(' โŒ No tool calls found'); } else { console.log(' โœ… Tool calls parsed successfully'); } } // Test tool instructions generation console.log('\n๐Ÿ“– Generated Tool Instructions:'); console.log('='.repeat(50)); const instructions = toolManager.generateToolInstructions(); console.log(instructions); // Cleanup await fs.rmdir(testDir, { recursive: true }); console.log('\n๐ŸŽ‰ Tool Call Testing Complete!'); console.log('\n๐ŸŽฏ Key Benefits of XML Format:'); console.log('- โœ… Reliable parsing (no JSON escaping issues)'); console.log('- โœ… CDATA support for complex content'); console.log('- โœ… Multiline content handling'); console.log('- โœ… Better error recovery'); console.log('- โœ… Cleaner syntax for LLMs to generate'); console.log('- โœ… Extensible and future-proof'); } // Run the test testToolCallParsing().catch(console.error);