File size: 2,478 Bytes
ce37a9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const axios = require('axios');
const config = require('../config');
const fs = require('fs-extra');
const path = require('path');

const MODELS = [
    "black-forest-labs/flux.2-klein-4b",
    "bytedance-seed/seedream-4.5",
    "black-forest-labs/flux.2-max"
];

module.exports = {
    /**
     * @param {object} args - { prompt }
     * @param {object} context - { filePath, mimeType } for Image-to-Image
     */
    execute: async (args, context) => {
        const { prompt } = args;
        const { filePath, mimeType } = context;
        
        if (!config.ai.openRouter.apiKey) {
            return { error: "OpenRouter API Key is missing." };
        }

        // Prepare image data if exists (Image-to-Image / Reference)
        let imageData = null;
        if (filePath && fs.existsSync(filePath) && mimeType.startsWith('image/')) {
            imageData = fs.readFileSync(filePath).toString('base64');
        }

        for (const model of MODELS) {
            try {
                console.log(`Attempting image generation with model: ${model}...`);
                
                const payload = {
                    model: model,
                    prompt: prompt,
                };

                // Add reference image for multimodal models if available
                if (imageData) {
                    payload.images = [imageData];
                }

                const response = await axios.post('https://openrouter.ai/api/v1/images/generations', payload, {
                    headers: {
                        'Authorization': `Bearer ${config.ai.openRouter.apiKey}`,
                        'Content-Type': 'application/json'
                    },
                    timeout: 60000 // Image gen can be slow
                });

                if (response.data && response.data.data && response.data.data[0]) {
                    const imageUrl = response.data.data[0].url;
                    return { 
                        success: true, 
                        imageUrl: imageUrl, 
                        modelUsed: model,
                        message: `Image generated using ${model}` 
                    };
                }
            } catch (error) {
                console.warn(`Model ${model} failed:`, error.response?.data || error.message);
                continue; // Try next model
            }
        }

        return { error: "All image generation models failed. Please try again later." };
    }
};