| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- /**
- * MiniMax Image Generation Adapter
- * Supports: text-to-image with aspect ratio control
- * API Docs: https://platform.minimaxi.com/docs/api-reference/image-generation-t2i
- */
- import type {
- ImageGenerationConfig,
- ImageGenerationOptions,
- ImageGenerationResult,
- } from '../types';
- const BASE_URL = 'https://api.minimaxi.com';
- export async function generateWithMiniMaxImage(
- config: ImageGenerationConfig,
- options: ImageGenerationOptions,
- ): Promise<ImageGenerationResult> {
- const baseUrl = (config.baseUrl || BASE_URL).replace(/\/$/, '');
- const model = config.model || 'image-01';
- const aspectRatio = options.aspectRatio || '1:1';
- const response = await fetch(`${baseUrl}/v1/image_generation`, {
- method: 'POST',
- headers: {
- Authorization: `Bearer ${config.apiKey}`,
- 'Content-Type': 'application/json; charset=utf-8',
- },
- body: JSON.stringify({
- model,
- prompt: options.prompt,
- negative_prompt: options.negativePrompt,
- aspect_ratio: aspectRatio,
- response_format: 'url',
- n: 1,
- prompt_optimizer: false,
- }),
- });
- if (!response.ok) {
- const errText = await response.text().catch(() => response.statusText);
- throw new Error(`MiniMax Image API error: ${errText}`);
- }
- const data = await response.json();
- // Check for error response
- if (data?.base_resp?.status_code !== 0 && data?.base_resp?.status_code !== undefined) {
- const code = data.base_resp.status_code;
- const msg = data.base_resp.status_msg || 'unknown error';
- throw new Error(`MiniMax Image API error ${code}: ${msg}`);
- }
- const imageUrls = data?.data?.image_urls;
- if (!imageUrls || imageUrls.length === 0) {
- throw new Error(`MiniMax Image: no image URLs returned. Response: ${JSON.stringify(data)}`);
- }
- const imageUrl = imageUrls[0];
- // Determine dimensions from aspect ratio
- let width = options.width || 1024;
- let height = options.height || 1024;
- if (!options.width && !options.height) {
- const [w, h] = aspectRatio.split(':').map(Number);
- if (w && h) {
- if (w > h) {
- width = 1024;
- height = Math.round((1024 * h) / w);
- } else {
- height = 1024;
- width = Math.round((1024 * w) / h);
- }
- }
- }
- return {
- url: imageUrl,
- width,
- height,
- };
- }
- export async function testMiniMaxImageConnectivity(
- config: ImageGenerationConfig,
- ): Promise<{ success: boolean; message: string }> {
- try {
- const baseUrl = (config.baseUrl || BASE_URL).replace(/\/$/, '');
- const response = await fetch(`${baseUrl}/v1/image_generation`, {
- method: 'POST',
- headers: {
- Authorization: `Bearer ${config.apiKey}`,
- 'Content-Type': 'application/json; charset=utf-8',
- },
- body: JSON.stringify({
- model: 'image-01',
- prompt: 'test',
- aspect_ratio: '1:1',
- n: 1,
- }),
- });
- if (response.ok) {
- return { success: true, message: 'MiniMax Image API connected' };
- }
- const errData = await response.json().catch(() => ({}));
- const msg = errData?.base_resp?.status_msg || response.statusText;
- return { success: false, message: `API error: ${msg}` };
- } catch (err) {
- return { success: false, message: `Connection failed: ${(err as Error).message}` };
- }
- }
|