|
| 1 | +/** |
| 2 | + * Yollomi text-to-image / image-to-image generation. |
| 3 | + * |
| 4 | + * Uses per-model routes exactly like the frontend: |
| 5 | + * POST /api/ai/z-image-turbo { prompt, width, height, ... } |
| 6 | + * POST /api/ai/nano-banana { prompt, aspect_ratio, ... } |
| 7 | + * POST /api/ai/flux-2-pro { prompt, aspectRatio, imageUrl?, ... } |
| 8 | + */ |
| 9 | + |
| 10 | +import * as path from 'node:path'; |
| 11 | +import chalk from 'chalk'; |
| 12 | +import { cli, Strategy } from '../../registry.js'; |
| 13 | +import { CliError } from '../../errors.js'; |
| 14 | +import { YOLLOMI_DOMAIN, yollomiPost, resolveImageInput, downloadOutput, fmtBytes, MODEL_ROUTES } from './utils.js'; |
| 15 | + |
| 16 | +function getDimensions(ratio: string): { width: number; height: number } { |
| 17 | + const map: Record<string, [number, number]> = { |
| 18 | + '1:1': [1024, 1024], '16:9': [1344, 768], '9:16': [768, 1344], |
| 19 | + '4:3': [1152, 896], '3:4': [896, 1152], |
| 20 | + }; |
| 21 | + const [w, h] = map[ratio] || [1024, 1024]; |
| 22 | + return { width: w, height: h }; |
| 23 | +} |
| 24 | + |
| 25 | +cli({ |
| 26 | + site: 'yollomi', |
| 27 | + name: 'generate', |
| 28 | + description: 'Generate images with AI (text-to-image or image-to-image)', |
| 29 | + domain: YOLLOMI_DOMAIN, |
| 30 | + strategy: Strategy.COOKIE, |
| 31 | + args: [ |
| 32 | + { name: 'prompt', positional: true, required: true, help: 'Text prompt describing the image' }, |
| 33 | + { name: 'model', default: 'z-image-turbo', help: 'Model ID (z-image-turbo, flux-schnell, nano-banana, flux-2-pro, ...)' }, |
| 34 | + { name: 'ratio', default: '1:1', choices: ['1:1', '16:9', '9:16', '4:3', '3:4'], help: 'Aspect ratio' }, |
| 35 | + { name: 'image', help: 'Input image URL for image-to-image (upload via "opencli yollomi upload" first)' }, |
| 36 | + { name: 'output', default: './yollomi-output', help: 'Output directory' }, |
| 37 | + { name: 'no-download', type: 'boolean', default: false, help: 'Only show URLs, skip download' }, |
| 38 | + ], |
| 39 | + columns: ['index', 'status', 'file', 'size', 'url'], |
| 40 | + func: async (page, kwargs) => { |
| 41 | + const prompt = kwargs.prompt as string; |
| 42 | + const modelId = kwargs.model as string; |
| 43 | + const ratio = kwargs.ratio as string; |
| 44 | + |
| 45 | + const apiPath = MODEL_ROUTES[modelId]; |
| 46 | + if (!apiPath) throw new CliError('INVALID_MODEL', `Unknown model: ${modelId}`, 'Run "opencli yollomi models --type image" to see available models'); |
| 47 | + |
| 48 | + let body: Record<string, unknown>; |
| 49 | + |
| 50 | + if (modelId === 'z-image-turbo') { |
| 51 | + const { width, height } = getDimensions(ratio); |
| 52 | + body = { prompt, width, height, output_format: 'jpg', output_quality: 85, guidance_scale: 0, num_inference_steps: 8 }; |
| 53 | + } else if (modelId === 'flux-2-pro') { |
| 54 | + body = { prompt, aspectRatio: ratio, outputNumber: 1 }; |
| 55 | + if (kwargs.image) body.imageUrl = kwargs.image as string; |
| 56 | + } else if (modelId === 'flux-kontext-pro') { |
| 57 | + body = { prompt, output_format: 'jpg' }; |
| 58 | + if (kwargs.image) body.imageUrl = kwargs.image as string; |
| 59 | + if (ratio !== '1:1') body.aspect_ratio = ratio; |
| 60 | + } else { |
| 61 | + body = { prompt, aspect_ratio: ratio }; |
| 62 | + if (kwargs.image) body.imageUrl = kwargs.image as string; |
| 63 | + } |
| 64 | + |
| 65 | + process.stderr.write(chalk.dim(`Generating with ${modelId}...\n`)); |
| 66 | + const data = await yollomiPost(page, apiPath, body); |
| 67 | + |
| 68 | + const images: string[] = data.images || (data.image ? [data.image] : []); |
| 69 | + if (!images.length) throw new CliError('EMPTY_RESPONSE', 'No images returned', 'Try a different prompt or model'); |
| 70 | + |
| 71 | + const noDownload = kwargs['no-download'] as boolean; |
| 72 | + const outputDir = kwargs.output as string; |
| 73 | + const results: any[] = []; |
| 74 | + |
| 75 | + for (let i = 0; i < images.length; i++) { |
| 76 | + const url = images[i]; |
| 77 | + if (noDownload) { |
| 78 | + results.push({ index: i + 1, status: 'generated', file: '-', size: '-', url }); |
| 79 | + continue; |
| 80 | + } |
| 81 | + try { |
| 82 | + const urlPath = (() => { try { return new URL(url).pathname; } catch { return url; } })(); |
| 83 | + const ext = urlPath.endsWith('.png') || urlPath.endsWith('.webp') ? urlPath.slice(urlPath.lastIndexOf('.')) : '.jpg'; |
| 84 | + const filename = `yollomi_${modelId}_${Date.now()}_${i + 1}${ext}`; |
| 85 | + const { path: fp, size } = await downloadOutput(url, outputDir, filename); |
| 86 | + results.push({ index: i + 1, status: 'saved', file: path.relative('.', fp), size: fmtBytes(size), url }); |
| 87 | + } catch { |
| 88 | + results.push({ index: i + 1, status: 'download-failed', file: '-', size: '-', url }); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if (data.remainingCredits !== undefined) process.stderr.write(chalk.dim(`Credits remaining: ${data.remainingCredits}\n`)); |
| 93 | + return results; |
| 94 | + }, |
| 95 | +}); |
0 commit comments