From 76673e7de691444020f5fe727056d5adf7fca8bc Mon Sep 17 00:00:00 2001 From: shiyue Date: Wed, 11 Mar 2026 17:16:21 +0000 Subject: [PATCH] fix: support grok2 image size constraints for 3:2 and 2:3 aspect ratios --- src/lib/generator-api.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/lib/generator-api.ts b/src/lib/generator-api.ts index 63ef238..999cb39 100644 --- a/src/lib/generator-api.ts +++ b/src/lib/generator-api.ts @@ -26,9 +26,22 @@ const OFFICIAL_ONLY_PROVIDER_KEYS = new Set(['bailian', 'siliconflow']) /** * 将 aspectRatio 映射为 OpenAI 兼容的 size */ -function aspectRatioToOpenAISize(aspectRatio: string | undefined): string | undefined { +function aspectRatioToOpenAISize(aspectRatio: string | undefined, providerKey?: string): string | undefined { if (!aspectRatio) return undefined const ratio = aspectRatio.trim() + + // Grok2 支持的尺寸: 1024x1024, 1792x1024, 1024x1792, 1280x720, 720x1280 + if (providerKey === 'grok2') { + const grok2Mapping: Record = { + '1:1': '1024x1024', + '16:9': '1792x1024', + '9:16': '1024x1792', + '3:2': '1792x1024', // 使用 1792x1024 代替不支持的 1536x1024 + '2:3': '1024x1792', // 使用 1024x1792 代替不支持的 1024x1536 + } + return grok2Mapping[ratio] || undefined + } + // OpenAI 支持的尺寸: 1024x1024, 1792x1024, 1024x1792, 1536x1024, 1024x1536 const mapping: Record = { '1:1': '1024x1024', @@ -130,7 +143,7 @@ export async function generateImage( // OpenAI 兼容模式:将 aspectRatio 转换为 size let openaiCompatOptions = { ...generatorOptions } if (openaiCompatOptions.aspectRatio) { - const mappedSize = aspectRatioToOpenAISize(openaiCompatOptions.aspectRatio) + const mappedSize = aspectRatioToOpenAISize(openaiCompatOptions.aspectRatio, providerKey) if (mappedSize && !openaiCompatOptions.size) { openaiCompatOptions = { ...openaiCompatOptions, size: mappedSize } }