contaigents
Version:
Modular AI Content Ecosystem with Audio Generation
120 lines (105 loc) โข 3.89 kB
JavaScript
import { ToolManager } from '../services/tools/ToolManager.js';
import fs from 'fs/promises';
import path from 'path';
async function testXMLToolCallParsing() {
console.log('๐งช Testing XML 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">
<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">
<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: 'Legacy Bracket Format',
content: `Using the old format:
[TOOL:read_file:{"file_path":"README.md"}]
This should still work for backward compatibility.`
},
{
name: 'Mixed Formats',
content: `First, let me read a file:
<tool_call name="read_file">
<file_path>README.md</file_path>
</tool_call>
Then create another file using legacy format:
[TOOL:write_file:{"operation_type":"create","file_path":"legacy-test.txt","content":"Legacy format test"}]
Both formats 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๐ XML Tool Call Testing Complete!');
console.log('\n๐ฏ Key Benefits of XML Format:');
console.log('- โ
More 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('- โ
Backward compatibility with legacy format');
console.log('- โ
Cleaner syntax for LLMs to generate');
}
// Run the test
testXMLToolCallParsing().catch(console.error);