UNPKG

contaigents

Version:

Modular AI Content Ecosystem with Audio Generation

271 lines (270 loc) โ€ข 10.9 kB
#!/usr/bin/env node /** * Image Generation Tool Demo * * This script demonstrates the ImageGenerationTool in various scenarios: * 1. Basic image generation * 2. Multi-tool workflow (read file โ†’ generate image) * 3. Error handling scenarios * 4. XML tool call parsing and response formatting */ import { ToolManager } from '../services/tools/ToolManager.js'; import fs from 'fs/promises'; import path from 'path'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); // Demo configuration const DEMO_BASE_DIR = path.join(__dirname, 'demo_image_output'); const API_KEY = process.env.GEMINI_API_KEY; class ImageGenerationDemo { constructor() { this.toolManager = new ToolManager(DEMO_BASE_DIR); } async setup() { console.log('๐ŸŽจ Setting up Image Generation Tool Demo'); console.log('='.repeat(50)); // Create demo directory await fs.mkdir(DEMO_BASE_DIR, { recursive: true }); // Create a sample text file for reading const samplePrompt = `A futuristic AI assistant robot working at a computer, digital art style, vibrant colors, high tech environment with holographic displays`; await fs.writeFile(path.join(DEMO_BASE_DIR, 'sample_prompt.txt'), samplePrompt); console.log(`๐Ÿ“ Demo directory created: ${DEMO_BASE_DIR}`); console.log(`๐Ÿ“ Sample prompt file created: sample_prompt.txt`); console.log(''); } async demonstrateBasicImageGeneration() { console.log('๐ŸŽจ Demo 1: Basic Image Generation'); console.log('-'.repeat(30)); const toolCall = { tool_name: 'image_generation', parameters: { prompt: 'A beautiful sunset over mountains, digital art style, vibrant colors', output_path: 'demo/basic_sunset.png', provider: 'gemini', style: 'digital_art' }, id: 'demo_1' }; console.log('๐Ÿ“‹ Tool Call:'); console.log(this.formatToolCallXML(toolCall)); console.log(''); const result = await this.toolManager.executeTool(toolCall); const response = { id: toolCall.id, tool_call: toolCall, result }; console.log('๐Ÿ“ค Tool Response:'); console.log(this.formatToolResponseXML(response)); console.log(''); } async demonstrateMultiToolWorkflow() { console.log('๐Ÿ”„ Demo 2: Multi-Tool Workflow (Read File โ†’ Generate Image)'); console.log('-'.repeat(50)); // Step 1: Read the sample prompt file const readToolCall = { tool_name: 'read_file', parameters: { file_path: 'sample_prompt.txt' }, id: 'demo_2a' }; console.log('๐Ÿ“– Step 1: Reading prompt file'); console.log('Tool Call:'); console.log(this.formatToolCallXML(readToolCall)); console.log(''); const readResult = await this.toolManager.executeTool(readToolCall); const readResponse = { id: readToolCall.id, tool_call: readToolCall, result: readResult }; console.log('Tool Response:'); console.log(this.formatToolResponseXML(readResponse)); console.log(''); if (readResult.success) { // Step 2: Generate image from the file content const imageToolCall = { tool_name: 'image_generation', parameters: { prompt: readResult.data.content, output_path: 'demo/ai_assistant_robot.png', provider: 'gemini', width: 1024, height: 1024 }, id: 'demo_2b' }; console.log('๐ŸŽจ Step 2: Generating image from file prompt'); console.log('Tool Call:'); console.log(this.formatToolCallXML(imageToolCall)); console.log(''); const imageResult = await this.toolManager.executeTool(imageToolCall); const imageResponse = { id: imageToolCall.id, tool_call: imageToolCall, result: imageResult }; console.log('Tool Response:'); console.log(this.formatToolResponseXML(imageResponse)); console.log(''); } } async demonstrateErrorHandling() { console.log('โŒ Demo 3: Error Handling Scenarios'); console.log('-'.repeat(35)); // Error 1: Missing required parameter console.log('Error Scenario 1: Missing required parameter'); const errorCall1 = { tool_name: 'image_generation', parameters: { output_path: 'error_test.png' // Missing 'prompt' parameter }, id: 'error_1' }; const errorResult1 = await this.toolManager.executeTool(errorCall1); console.log('Result:', errorResult1.error); console.log(''); // Error 2: Path traversal attempt console.log('Error Scenario 2: Path traversal attempt'); const errorCall2 = { tool_name: 'image_generation', parameters: { prompt: 'A simple test image', output_path: '../../../etc/passwd.png' }, id: 'error_2' }; const errorResult2 = await this.toolManager.executeTool(errorCall2); console.log('Result:', errorResult2.error); console.log(''); // Error 3: No API key (if not set) if (!API_KEY) { console.log('Error Scenario 3: Missing API key'); const errorCall3 = { tool_name: 'image_generation', parameters: { prompt: 'A test image', output_path: 'test.png' }, id: 'error_3' }; const errorResult3 = await this.toolManager.executeTool(errorCall3); console.log('Result:', errorResult3.error); console.log(''); } } async demonstrateAgentConversation() { console.log('๐Ÿ’ฌ Demo 4: Agent Conversation Simulation'); console.log('-'.repeat(40)); console.log('User: "Can you create a visual representation of a modern workspace?"'); console.log(''); console.log('Agent: "I\'ll generate an image of a modern workspace for you."'); console.log(''); // Execute the actual workflow const imageCall = { tool_name: 'image_generation', parameters: { prompt: 'A modern minimalist workspace with a clean desk, laptop, plants, natural lighting, Scandinavian design, professional photography style', output_path: 'workspace/modern_office.png', provider: 'gemini', style: 'photographic', width: 1920, height: 1080 }, id: 'workspace_1' }; const imageResult = await this.toolManager.executeTool(imageCall); if (imageResult.success) { console.log('Agent Final Response:'); console.log(`"I've created a beautiful image of a modern workspace! The image has been saved to \`${imageResult.data.output_file_path}\` (${this.formatFileSize(imageResult.data.file_size)}, ${imageResult.data.dimensions}). The image features a clean, minimalist design with natural lighting that should inspire productivity."`); } else { console.log('Agent Error Response:'); console.log(`"I encountered an error while generating the image: ${imageResult.error}"`); } console.log(''); } formatToolCallXML(toolCall) { let xml = `<tool_call name="${toolCall.tool_name}" id="${toolCall.id}">`; for (const [key, value] of Object.entries(toolCall.parameters)) { if (typeof value === 'string' && (value.includes('\n') || value.includes('<') || value.includes('>'))) { xml += `\n <${key}><![CDATA[${value}]]></${key}>`; } else { xml += `\n <${key}>${value}</${key}>`; } } xml += '\n</tool_call>'; return xml; } formatToolResponseXML(response) { let xml = `<tool_response id="${response.id}">`; xml += `\n <tool_call name="${response.tool_call.tool_name}">`; for (const [key, value] of Object.entries(response.tool_call.parameters)) { xml += `\n <${key}>${value}</${key}>`; } xml += '\n </tool_call>'; xml += '\n <result>'; if (response.result.success) { xml += '\n <success>true</success>'; if (response.result.message) { xml += `\n <message>${response.result.message}</message>`; } if (response.result.data) { xml += '\n <data>'; for (const [key, value] of Object.entries(response.result.data)) { xml += `\n <${key}>${value}</${key}>`; } xml += '\n </data>'; } } else { xml += '\n <success>false</success>'; xml += `\n <error>${response.result.error}</error>`; } xml += '\n </result>'; xml += '\n</tool_response>'; return xml; } formatFileSize(bytes) { if (bytes === 0) return '0 B'; const k = 1024; const sizes = ['B', 'KB', 'MB', 'GB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i]; } async cleanup() { console.log('๐Ÿงน Demo completed! Check the output files in:'); console.log(` ${DEMO_BASE_DIR}`); console.log(''); console.log('Generated files may include:'); console.log(' - demo/basic_sunset.png'); console.log(' - demo/ai_assistant_robot.png'); console.log(' - workspace/modern_office.png'); console.log(''); } } // Run the demo async function runDemo() { const demo = new ImageGenerationDemo(); try { await demo.setup(); await demo.demonstrateBasicImageGeneration(); await demo.demonstrateMultiToolWorkflow(); await demo.demonstrateErrorHandling(); await demo.demonstrateAgentConversation(); await demo.cleanup(); } catch (error) { console.error('โŒ Demo failed:', error); process.exit(1); } } // Run if called directly if (import.meta.url === `file://${process.argv[1]}`) { runDemo(); }