contaigents
Version:
Modular AI Content Ecosystem with Audio Generation
132 lines (124 loc) โข 4.78 kB
JavaScript
import { ToolManager } from '../services/tools/ToolManager.js';
import fs from 'fs/promises';
import path from 'path';
async function testXMLToolResponseFormat() {
console.log('๐งช Testing XML Tool Response Format');
console.log('='.repeat(50));
// Create a test directory
const testDir = path.join(process.cwd(), 'test-xml-responses');
await fs.mkdir(testDir, { recursive: true });
const toolManager = new ToolManager(testDir);
// Create a test file for reading
const testFilePath = path.join(testDir, 'test-content.md');
await fs.writeFile(testFilePath, `# Test Content
This is a test file for demonstrating XML tool responses.
## Features
- Structured XML responses
- ID matching between calls and responses
- Clean error handling
- CDATA support for complex content
`);
console.log('\n๐ Test Case 1: Successful File Read');
console.log('-'.repeat(30));
// Test successful tool execution
const readToolCall = {
tool_name: 'read_file',
parameters: { file_path: 'test-content.md' },
id: '1'
};
const readResult = await toolManager.executeTool(readToolCall);
const readResponse = {
id: readToolCall.id,
tool_call: readToolCall,
result: readResult
};
const xmlResponse1 = toolManager.generateToolResponse(readResponse);
console.log('Generated XML Response:');
console.log(xmlResponse1);
console.log('\n๐ Test Case 2: File Creation');
console.log('-'.repeat(30));
// Test file creation
const writeToolCall = {
tool_name: 'write_file',
parameters: {
operation_type: 'create',
file_path: 'new-test.md',
content: `# New Test File
This file was created to test XML tool responses.
## Complex Content
- "Quoted text"
- [Brackets] and {braces}
- JSON: {"test": true, "array": [1, 2, 3]}
- <XML> tags and special characters: & < >
`
},
id: '2'
};
const writeResult = await toolManager.executeTool(writeToolCall);
const writeResponse = {
id: writeToolCall.id,
tool_call: writeToolCall,
result: writeResult
};
const xmlResponse2 = toolManager.generateToolResponse(writeResponse);
console.log('Generated XML Response:');
console.log(xmlResponse2);
console.log('\n๐ Test Case 3: Error Handling');
console.log('-'.repeat(30));
// Test error case
const errorToolCall = {
tool_name: 'read_file',
parameters: { file_path: 'nonexistent-file.md' },
id: '3'
};
const errorResult = await toolManager.executeTool(errorToolCall);
const errorResponse = {
id: errorToolCall.id,
tool_call: errorToolCall,
result: errorResult
};
const xmlResponse3 = toolManager.generateToolResponse(errorResponse);
console.log('Generated XML Response:');
console.log(xmlResponse3);
console.log('\n๐ Test Case 4: Multiple Tool Responses');
console.log('-'.repeat(30));
// Test multiple responses
const multipleResponses = [readResponse, writeResponse, errorResponse];
const combinedXmlResponses = toolManager.generateToolResponses(multipleResponses);
console.log('Combined XML Responses:');
console.log(combinedXmlResponses);
console.log('\n๐ Test Case 5: Tool Call Parsing with IDs');
console.log('-'.repeat(30));
const sampleContent = `I'll help you with multiple file operations.
<tool_call name="read_file" id="1">
<file_path>README.md</file_path>
</tool_call>
<tool_call name="write_file" id="2">
<operation_type>create</operation_type>
<file_path>output.md</file_path>
<content><![CDATA[
# Output File
This contains complex content with "quotes" and [brackets].
JSON: {"key": "value", "numbers": [1, 2, 3]}
]]></content>
</tool_call>
Both operations should complete successfully.`;
const parsedCalls = toolManager.parseToolCalls(sampleContent);
console.log('Parsed Tool Calls:');
parsedCalls.forEach((call, index) => {
console.log(`${index + 1}. Tool: ${call.tool_name}, ID: ${call.id || 'none'}`);
console.log(` Parameters:`, call.parameters);
});
// Cleanup
await fs.rmdir(testDir, { recursive: true });
console.log('\n๐ XML Tool Response Testing Complete!');
console.log('\n๐ฏ Key Features Demonstrated:');
console.log('- โ
XML tool responses with matching IDs');
console.log('- โ
Structured result formatting');
console.log('- โ
Error handling in XML format');
console.log('- โ
Multiple response generation');
console.log('- โ
Tool call parsing with ID support');
console.log('- โ
CDATA handling for complex content');
}
// Run the test
testXMLToolResponseFormat().catch(console.error);