contaigents
Version:
Modular AI Content Ecosystem with Audio Generation
217 lines (205 loc) โข 8.31 kB
JavaScript
import { ToolManager } from '../services/tools/ToolManager.js';
import fs from 'fs/promises';
import path from 'path';
async function testCombinedTools() {
console.log('๐งช Testing Combined Read/Write Tool Workflow...\n');
const testDir = path.join(process.cwd(), 'test-combined-tools');
const toolManager = new ToolManager(process.cwd());
try {
// Setup test environment
await fs.mkdir(testDir, { recursive: true });
console.log('โ
Created test directory:', testDir);
// Step 1: Create an initial file
console.log('\n๐ Step 1: Creating initial file...');
const createResult = await toolManager.executeTool({
tool_name: 'write_file',
parameters: {
operation_type: 'create',
file_path: 'test-combined-tools/document.md',
content: `# Project Documentation
## Overview
This is the initial version of our project documentation.
## Features
- Basic functionality
- Simple structure
## TODO
- Add more details
- Include examples
`
}
});
if (createResult.success) {
console.log('โ
Initial file created successfully!');
console.log('๐ฌ Message:', createResult.message);
}
else {
console.log('โ Failed to create initial file:', createResult.error);
return;
}
// Step 2: Read the file to verify content
console.log('\n๐ Step 2: Reading the created file...');
const readResult = await toolManager.executeTool({
tool_name: 'read_file',
parameters: {
file_path: 'test-combined-tools/document.md'
}
});
if (readResult.success) {
console.log('โ
File read successfully!');
console.log('๐ Content length:', readResult.data?.content.length);
console.log('๐ First 100 characters:', readResult.data?.content.substring(0, 100) + '...');
}
else {
console.log('โ Failed to read file:', readResult.error);
return;
}
// Step 3: Update specific sections using partial write
console.log('\nโ๏ธ Step 3: Updating the Features section (lines 6-8)...');
const updateResult = await toolManager.executeTool({
tool_name: 'write_file',
parameters: {
operation_type: 'partial',
file_path: 'test-combined-tools/document.md',
content: `## Features
- Advanced functionality
- Robust architecture
- Tool integration
- File operations
- Backup system`,
start_line: 6,
end_line: 8,
create_backup: true
}
});
if (updateResult.success) {
console.log('โ
Features section updated successfully!');
console.log('๐ฌ Message:', updateResult.message);
}
else {
console.log('โ Failed to update features section:', updateResult.error);
return;
}
// Step 4: Read the updated file
console.log('\n๐ Step 4: Reading the updated file...');
const readUpdatedResult = await toolManager.executeTool({
tool_name: 'read_file',
parameters: {
file_path: 'test-combined-tools/document.md'
}
});
if (readUpdatedResult.success) {
console.log('โ
Updated file read successfully!');
console.log('๐ Updated content:');
console.log('โ'.repeat(50));
console.log(readUpdatedResult.data?.content);
console.log('โ'.repeat(50));
}
else {
console.log('โ Failed to read updated file:', readUpdatedResult.error);
return;
}
// Step 5: Add a new section at the end
console.log('\nโ Step 5: Adding a new section at the end...');
const addSectionResult = await toolManager.executeTool({
tool_name: 'write_file',
parameters: {
operation_type: 'partial',
file_path: 'test-combined-tools/document.md',
content: `
## Implementation Details
This section was added using the partial write functionality.
### Tool Architecture
- FileReadTool: Handles file reading operations
- FileWriteTool: Handles file writing operations
- ToolManager: Orchestrates tool execution
### Usage Examples
\`\`\`javascript
// Reading a file
const result = await toolManager.executeTool({
tool_name: 'read_file',
parameters: { file_path: 'document.md' }
});
// Writing to a file
const writeResult = await toolManager.executeTool({
tool_name: 'write_file',
parameters: {
operation_type: 'create',
file_path: 'new-file.md',
content: 'Hello World!'
}
});
\`\`\`
## Conclusion
The combined tool workflow demonstrates seamless integration between reading and writing operations.
`,
start_line: 15,
end_line: 15,
create_backup: false
}
});
if (addSectionResult.success) {
console.log('โ
New section added successfully!');
console.log('๐ฌ Message:', addSectionResult.message);
}
else {
console.log('โ Failed to add new section:', addSectionResult.error);
return;
}
// Step 6: Final read to show complete document
console.log('\n๐ Step 6: Reading the final document...');
const finalReadResult = await toolManager.executeTool({
tool_name: 'read_file',
parameters: {
file_path: 'test-combined-tools/document.md'
}
});
if (finalReadResult.success) {
console.log('โ
Final document read successfully!');
console.log('๐ Final stats:');
console.log(` - Lines: ${finalReadResult.data?.fileInfo.lines}`);
console.log(` - Characters: ${finalReadResult.data?.fileInfo.size}`);
console.log(` - File type: ${finalReadResult.data?.fileInfo.extension}`);
}
else {
console.log('โ Failed to read final document:', finalReadResult.error);
}
// Step 7: Test tool call parsing with multiple tools
console.log('\n๐ Step 7: Testing tool call parsing with multiple tools...');
const sampleResponse = `I'll help you work with your files. Let me first read the document and then update it.
[TOOL:read_file:{"file_path":"test-combined-tools/document.md"}]
Now I'll add a summary section:
[TOOL:write_file:{"operation_type":"partial","file_path":"test-combined-tools/document.md","content":"## Summary\\nThis document demonstrates the tool architecture.","start_line":1,"end_line":1}]
The operations have been completed successfully.`;
const parsedCalls = toolManager.parseToolCalls(sampleResponse);
console.log('โ
Parsed tool calls:', parsedCalls.length);
parsedCalls.forEach((call, index) => {
console.log(` ${index + 1}. ${call.tool_name} with parameters:`, Object.keys(call.parameters));
});
console.log('\n๐ Combined tools test completed successfully!');
console.log('\n๐ฏ Workflow Demonstrated:');
console.log('- โ
File creation with write_file');
console.log('- โ
Content verification with read_file');
console.log('- โ
Partial updates with line-specific writes');
console.log('- โ
Backup creation during modifications');
console.log('- โ
Multi-tool parsing and execution');
console.log('- โ
Seamless read/write integration');
}
catch (error) {
console.error('โ Combined tools test failed:', error);
}
finally {
// Cleanup
try {
await fs.rm(testDir, { recursive: true, force: true });
console.log('\n๐งน Cleaned up test files');
}
catch (cleanupError) {
console.error('Failed to cleanup:', cleanupError);
}
}
}
// Run the test if this file is executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
testCombinedTools().catch(console.error);
}
export { testCombinedTools };