contaigents
Version:
Modular AI Content Ecosystem with Audio Generation
111 lines (103 loc) โข 4.58 kB
JavaScript
import { ChatService } from '../services/chatService.js';
import fs from 'fs/promises';
import path from 'path';
// Mock LLM that generates XML tool calls
class MockLLMWithXMLTools {
async executePrompt(prompt, _options) {
console.log('๐ค Mock LLM received prompt with length:', prompt.length);
if (prompt.includes('read the README')) {
return {
content: `I'll help you read the README file using the new XML format.
<tool_call name="read_file">
<file_path>README.md</file_path>
</tool_call>
The file content will be displayed above.`
};
}
if (prompt.includes('create a test file')) {
return {
content: `I'll create a test file with complex content using XML format.
<tool_call name="write_file">
<operation_type>create</operation_type>
<file_path>xml-test.md</file_path>
<content><![CDATA[
# XML Tool Call Test
This file was created using the new XML tool call format!
Features demonstrated:
- "Quotes" and 'single quotes'
- [Brackets] and {braces}
- JSON: {"test": true, "array": [1, 2, 3]}
- Multiline content
- Special characters: <>&
## Code Example
function xmlTest() {
console.log("XML tool calls work great!");
}
]]></content>
</tool_call>
The test file has been created successfully with complex content!`
};
}
return {
content: 'I understand your request. How can I help you with file operations?'
};
}
}
async function testChatServiceWithXML() {
console.log('๐งช Testing ChatService with XML Tool Calls');
console.log('='.repeat(50));
// Create a test directory
const testDir = path.join(process.cwd(), 'test-chat-xml');
await fs.mkdir(testDir, { recursive: true });
// Create a test README file
await fs.writeFile(path.join(testDir, 'README.md'), '# Test Project\n\nThis is a test README file for XML tool call demonstration.\n');
try {
// Initialize ChatService with test directory
const chatService = new ChatService(testDir);
// Temporarily replace the LLM factory to use our mock
const originalGetConfiguredProvider = (await import('../services/llm/LLMFactory.js')).LLMFactory.getConfiguredProvider;
(await import('../services/llm/LLMFactory.js')).LLMFactory.getConfiguredProvider = async () => new MockLLMWithXMLTools();
console.log('\n๐ Test 1: XML Tool Call for File Reading');
console.log('โ'.repeat(40));
const session1 = chatService.createSession({
systemPrompt: 'You are a helpful assistant with file access tools. Use XML format for tool calls.'
});
const response1 = await chatService.sendMessage(session1.id, 'Please read the README file');
console.log('๐ Response:');
console.log(response1.content);
console.log('\n๐ Test 2: XML Tool Call for File Creation with Complex Content');
console.log('โ'.repeat(40));
const session2 = chatService.createSession({
systemPrompt: 'You are a helpful assistant with file access tools. Use XML format for tool calls.'
});
const response2 = await chatService.sendMessage(session2.id, 'Please create a test file with complex content');
console.log('๐ Response:');
console.log(response2.content);
// Verify the file was created
const createdFile = path.join(testDir, 'xml-test.md');
try {
const fileContent = await fs.readFile(createdFile, 'utf-8');
console.log('\nโ
Created file content:');
console.log('โ'.repeat(30));
console.log(fileContent);
}
catch (error) {
console.log('โ File was not created:', error);
}
// Restore original LLM factory
(await import('../services/llm/LLMFactory.js')).LLMFactory.getConfiguredProvider = originalGetConfiguredProvider;
console.log('\n๐ ChatService XML Tool Call Testing Complete!');
console.log('\n๐ฏ Key Findings:');
console.log('- โ
XML tool calls are parsed correctly');
console.log('- โ
CDATA sections handle complex content');
console.log('- โ
Tool execution works seamlessly');
console.log('- โ
Results are integrated into responses');
console.log('- โ
File operations are executed successfully');
}
finally {
// Cleanup
await fs.rm(testDir, { recursive: true, force: true });
}
}
// Run the test
testChatServiceWithXML().catch(console.error);