contaigents
Version:
Modular AI Content Ecosystem with Audio Generation
116 lines (103 loc) โข 3.74 kB
JavaScript
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);