contaigents
Version:
Modular AI Content Ecosystem with Audio Generation
122 lines (121 loc) ⢠6.07 kB
JavaScript
import { ChatService } from '../services/chatService.js';
import { ImageContextService } from '../services/ImageContextService.js';
import path from 'path';
import fs from 'fs/promises';
export async function testImageContext() {
console.log('š¼ļø Testing Image Context Integration');
console.log('==================================================\n');
const baseDir = process.cwd();
const imageContextService = new ImageContextService(baseDir);
const chatService = new ChatService(baseDir);
// Test 1: Image Reference Detection
console.log('š Test 1: Image Reference Detection');
const testMessages = [
"analyze image.jpg",
"what's in demo/test_ref_image.jpeg?",
"look at ./screenshots/ui.png and tell me what you see",
"check this image: ../demo/test_ref_image.jpeg",
"can you examine photo.webp for any issues?",
"just a regular message without images",
"process the file data.txt" // Should not detect as image
];
for (const message of testMessages) {
const hasImages = imageContextService.hasImageReferences(message);
const references = imageContextService.extractImageReferences(message);
console.log(`š "${message}"`);
console.log(` Has images: ${hasImages ? 'ā
' : 'ā'}`);
if (references.length > 0) {
console.log(` References: ${references.join(', ')}`);
}
console.log('');
}
// Test 2: Image Processing
console.log('š Test 2: Image Processing');
// Check if test image exists
const testImagePath = '../demo/test_ref_image.jpeg';
try {
await fs.access(path.join(baseDir, testImagePath));
console.log(`ā
Test image found: ${testImagePath}`);
const processedImages = await imageContextService.processImages([testImagePath]);
if (processedImages.length > 0) {
const image = processedImages[0];
console.log(`š Processed image details:`);
console.log(` Path: ${image.path}`);
console.log(` Size: ${image.size} bytes`);
console.log(` MIME Type: ${image.mimeType}`);
console.log(` Base64 length: ${image.base64Data.length} characters`);
console.log(` Description: ${image.description}`);
}
}
catch (error) {
console.log(`ā Test image not found: ${testImagePath}`);
console.log(' Skipping image processing test');
}
console.log('');
// Test 3: Chat Integration with Images
console.log('š Test 3: Chat Integration with Images');
try {
// Create a chat session
const session = await chatService.createSession();
console.log(`š¬ Created chat session: ${session.id}`);
// Test message with image reference
const messageWithImage = "analyze ../demo/test_ref_image.jpeg and describe what you see";
console.log(`š¤ Sending message: "${messageWithImage}"`);
// This will test the image detection and processing in the chat service
// Note: This might fail if Gemini is not configured, but we can see the image processing
try {
const response = await chatService.sendMessage(session.id, messageWithImage);
console.log(`š„ Response received: ${response.content.substring(0, 200)}...`);
}
catch (error) {
console.log(`ā ļø Chat response failed (likely due to LLM configuration): ${error.message}`);
console.log(' But image processing should have been logged above');
}
}
catch (error) {
console.log(`ā Chat integration test failed: ${error.message}`);
}
console.log('');
// Test 4: Multiple Images
console.log('š Test 4: Multiple Image Detection');
const multiImageMessage = "compare image1.jpg and image2.png, also check screenshot.webp";
const multiReferences = imageContextService.extractImageReferences(multiImageMessage);
console.log(`š Message: "${multiImageMessage}"`);
console.log(`šø Detected ${multiReferences.length} images: ${multiReferences.join(', ')}`);
console.log('');
// Test 5: Edge Cases
console.log('š Test 5: Edge Cases');
const edgeCases = [
'file.jpg.txt', // Should not be detected
'image.JPG', // Case insensitive
'"path with spaces/image.png"', // Quoted paths
'check image: ./folder/photo.jpeg', // After colon
'no images here at all', // No images
'fake.exe' // Wrong extension
];
for (const testCase of edgeCases) {
const detected = imageContextService.extractImageReferences(testCase);
console.log(`š "${testCase}" ā ${detected.length > 0 ? detected.join(', ') : 'No images'}`);
}
console.log('\nš Image Context Tests Completed!');
console.log('\nš” Usage Examples:');
console.log(' ⢠"analyze screenshot.png" - Direct image reference');
console.log(' ⢠"what\'s in ./images/photo.jpg?" - Path with directory');
console.log(' ⢠"check this image: demo/test.jpeg" - After keyword');
console.log(' ⢠"compare image1.png and image2.jpg" - Multiple images');
console.log('\nš§ Supported Features:');
console.log(' ⢠Automatic image detection in chat messages');
console.log(' ⢠Base64 encoding for LLM context');
console.log(' ⢠Support for JPG, PNG, GIF, WEBP, BMP, TIFF, SVG');
console.log(' ⢠Path validation and security checks');
console.log(' ⢠File size limits (20MB max)');
console.log(' ⢠Integration with Gemini Vision API');
console.log('\nšÆ Next Steps:');
console.log(' ⢠Configure Gemini API key: contaigents configure');
console.log(' ⢠Try: "analyze demo/test_ref_image.jpeg"');
console.log(' ⢠Use with any image file in your project');
}
// Run the test if this file is executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
testImageContext().catch(console.error);
}