| 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 = { |
| |
| |
| |
| |
| execute: async (args, context) => { |
| const { prompt } = args; |
| const { filePath, mimeType } = context; |
| |
| if (!config.ai.openRouter.apiKey) { |
| return { error: "OpenRouter API Key is missing." }; |
| } |
|
|
| |
| 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, |
| }; |
|
|
| |
| 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 |
| }); |
|
|
| 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; |
| } |
| } |
|
|
| return { error: "All image generation models failed. Please try again later." }; |
| } |
| }; |
|
|