fix: support grok2 image size constraints for 3:2 and 2:3 aspect ratios
Some checks failed
Build & Push Docker Image / build-and-push (push) Has been cancelled

This commit is contained in:
shiyue
2026-03-11 17:16:21 +00:00
parent da38d50a10
commit 76673e7de6

View File

@@ -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<string, string> = {
'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<string, string> = {
'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 }
}