contaigents
Version:
Modular AI Content Ecosystem with Audio Generation
154 lines (138 loc) โข 6.25 kB
JavaScript
import { ChatService } from '../services/chatService.js';
/**
* Test to demonstrate improved conversation flow with tool calls
* This test shows how the updated prompts ensure agents include tool calls
* when they need to continue the conversation thread
*/
// Mock LLM that demonstrates the improved behavior
class ImprovedConversationLLM {
constructor() {
this.callCount = 0;
}
async executePrompt(prompt, _options) {
this.callCount++;
console.log(`๐ค Mock LLM call #${this.callCount}`);
// First call: User asks to analyze and update README
if (prompt.includes('analyze project and update readme') && this.callCount === 1) {
// CORRECT behavior: Include tool calls immediately when acknowledging work to be done
return {
content: `I'll analyze the project structure and update the README accordingly. Let me start by reading the current README and package.json to understand the project setup.
<tool_call name="read_file" id="1">
<file_path>README.md</file_path>
</tool_call>
<tool_call name="read_file" id="2">
<file_path>package.json</file_path>
</tool_call>`
};
}
// Second call: After tool results, continue with more analysis
if (this.callCount === 2) {
return {
content: `Based on the files I've read, I can see this is a CLI tool. Let me also check the main entry point and any configuration files to get a complete picture.
<tool_call name="read_file" id="3">
<file_path>src/index.ts</file_path>
</tool_call>
<tool_call name="read_file" id="4">
<file_path>tsconfig.json</file_path>
</tool_call>`
};
}
// Third call: Final update after gathering all information
if (this.callCount === 3) {
return {
content: `Now I have a comprehensive understanding of the project. Let me update the README with detailed information about the project structure, setup, and usage.
<tool_call name="write_file" id="5">
<operation_type>update</operation_type>
<file_path>README.md</file_path>
<content><![CDATA[
# Contaigents CLI
A powerful CLI tool for content creation and management with AI assistance.
## Features
- Interactive chat with AI assistants
- File operations with tool architecture
- Content creation and editing capabilities
- Multi-turn conversation support
## Installation
\`\`\`bash
npm install
npm run build
npm link
\`\`\`
## Usage
\`\`\`bash
# Start interactive chat
contaigents chat
# Test tool architecture
contaigents test-tools
\`\`\`
## Project Structure
- \`src/index.ts\` - Main CLI entry point
- \`src/services/\` - Core services (chat, LLM, tools)
- \`src/tests/\` - Test files and demonstrations
]]></content>
</tool_call>`
};
}
// Fourth call: Final response without tool calls (conversation ends)
return {
content: `Perfect! I've successfully analyzed the project and updated the README.md file with comprehensive information about the project structure, features, installation, and usage instructions. The README now provides a clear overview for anyone wanting to understand or contribute to the Contaigents CLI project.`
};
}
}
export async function testImprovedConversationFlow() {
console.log("๐งช Testing Improved Conversation Flow...\n");
// Create a temporary directory for testing
const testDir = '/tmp/contaigents-conversation-test';
try {
// Create mock files for testing
const fs = await import('fs/promises');
await fs.mkdir(testDir, { recursive: true });
await fs.mkdir(`${testDir}/src`, { recursive: true });
await fs.writeFile(`${testDir}/README.md`, '# Basic README\nThis is a basic readme.');
await fs.writeFile(`${testDir}/package.json`, JSON.stringify({
name: 'test-project',
version: '1.0.0',
description: 'Test project'
}, null, 2));
await fs.writeFile(`${testDir}/src/index.ts`, 'console.log("Hello World");');
await fs.writeFile(`${testDir}/tsconfig.json`, '{"compilerOptions": {"target": "ES2020"}}');
// Create chat service with mock LLM
const chatService = new ChatService(testDir);
// Replace the LLM factory with our mock
chatService.getLLMProvider = async () => new ImprovedConversationLLM();
// Create session and test the improved conversation flow
const session = chatService.createSession({
systemPrompt: "You are a helpful assistant with access to file operations."
});
console.log("๐ User Request: 'analyze project and update readme'");
console.log("Expected: Agent should include tool calls in EVERY response until work is complete\n");
const response = await chatService.sendMessage(session.id, 'analyze project and update readme');
console.log("โ
Test completed successfully!");
console.log("๐ Final Response:", response.content.substring(0, 200) + "...");
// Verify the conversation had multiple turns
const messages = chatService.getSession(session.id)?.messages || [];
const assistantMessages = messages.filter(m => m.role === 'assistant');
console.log(`\n๐ Conversation Statistics:`);
console.log(`- Total assistant messages: ${assistantMessages.length}`);
console.log(`- Messages with tool calls: ${assistantMessages.filter(m => m.content.includes('<tool_call')).length}`);
console.log(`- Final message (no tool calls): ${assistantMessages[assistantMessages.length - 1]?.content.includes('<tool_call') ? 'No' : 'Yes'}`);
// Clean up
await fs.rm(testDir, { recursive: true, force: true });
}
catch (error) {
console.error("โ Test failed:", error);
throw error;
}
}
// Run the test if this file is executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
testImprovedConversationFlow()
.then(() => {
console.log("\n๐ All tests passed!");
process.exit(0);
})
.catch((error) => {
console.error("\n๐ฅ Test failed:", error);
process.exit(1);
});
}