fix: add gpt-image-2 high-resolution sizes

This commit is contained in:
linshen committed 2026-08-13 22:22:58 +08:00
1 parent 69a636921e
commit bb79051a88
2 files changed
+34 -8

No files matched your search

@@ -10,6 +10,19 @@ import type {
import { ImageError } from '../errors' import { ImageError } from '../errors'
import { IMAGE_ERROR_CODES } from '../../../constants/error-codes' import { IMAGE_ERROR_CODES } from '../../../constants/error-codes'
const GPT_IMAGE_2_SIZE_VALUES = [
'1024x1024',
'1536x1024',
'1024x1536',
'2048x2048',
'2048x1152',
'3840x2160',
'2160x3840',
'auto'
] as const
const GPT_IMAGE_2_BACKGROUND_VALUES = ['auto', 'opaque'] as const
export class OpenAIImageAdapter extends AbstractImageProviderAdapter { export class OpenAIImageAdapter extends AbstractImageProviderAdapter {
private static readonly FORCED_SINGLE_OUTPUT_PARAM_KEYS = ['n', 'batch_size', 'outputMimeType', 'response_format'] private static readonly FORCED_SINGLE_OUTPUT_PARAM_KEYS = ['n', 'batch_size', 'outputMimeType', 'response_format']
@@ -56,7 +69,7 @@ export class OpenAIImageAdapter extends AbstractImageProviderAdapter {
descriptionKey: 'image.params.size.description', descriptionKey: 'image.params.size.description',
type: 'string', type: 'string',
defaultValue: '1024x1024', defaultValue: '1024x1024',
allowedValues: ['1024x1024', '1536x1024', '1024x1536', 'auto'] allowedValues: [...GPT_IMAGE_2_SIZE_VALUES]
}, },
{ {
name: 'quality', name: 'quality',
@@ -72,7 +85,7 @@ export class OpenAIImageAdapter extends AbstractImageProviderAdapter {
descriptionKey: 'image.params.background.description', descriptionKey: 'image.params.background.description',
type: 'string', type: 'string',
defaultValue: 'auto', defaultValue: 'auto',
allowedValues: ['auto', 'transparent', 'opaque'] allowedValues: [...GPT_IMAGE_2_BACKGROUND_VALUES]
} }
], ],
defaultParameterValues: { defaultParameterValues: {
@@ -145,7 +158,7 @@ export class OpenAIImageAdapter extends AbstractImageProviderAdapter {
} }
protected getParameterDefinitions(_modelId: string): readonly ImageParameterDefinition[] { protected getParameterDefinitions(_modelId: string): readonly ImageParameterDefinition[] {
// GPT Image 1 使用统一的参数定义,n参数固定为1不暴露给用户 // GPT Image 2 使用统一的参数定义,n参数固定为1不暴露给用户
return [ return [
{ {
name: 'size', name: 'size',
@@ -153,7 +166,7 @@ export class OpenAIImageAdapter extends AbstractImageProviderAdapter {
descriptionKey: 'image.params.size.description', descriptionKey: 'image.params.size.description',
type: 'string', type: 'string',
defaultValue: '1024x1024', defaultValue: '1024x1024',
allowedValues: ['1024x1024', '1536x1024', '1024x1536', 'auto'] allowedValues: [...GPT_IMAGE_2_SIZE_VALUES]
}, },
{ {
name: 'quality', name: 'quality',
@@ -169,7 +182,7 @@ export class OpenAIImageAdapter extends AbstractImageProviderAdapter {
descriptionKey: 'image.params.background.description', descriptionKey: 'image.params.background.description',
type: 'string', type: 'string',
defaultValue: 'auto', defaultValue: 'auto',
allowedValues: ['auto', 'transparent', 'opaque'] allowedValues: [...GPT_IMAGE_2_BACKGROUND_VALUES]
} }
] ]
} }
@@ -54,20 +54,32 @@ describe('OpenAIImageAdapter', () => {
}) })
}) })
test('should include quality and size parameters', () => { test('should expose supported GPT Image 2 parameters', () => {
const models = adapter.getModels() const models = adapter.getModels()
const model = models.find(m => m.id === 'gpt-image-2') const model = models.find(m => m.id === 'gpt-image-2')
expect(model?.parameterDefinitions).toBeDefined() expect(model?.parameterDefinitions).toBeDefined()
const qualityParam = model?.parameterDefinitions?.find(p => p.name === 'quality') const qualityParam = model?.parameterDefinitions?.find(p => p.name === 'quality')
const sizeParam = model?.parameterDefinitions?.find(p => p.name === 'size') const sizeParam = model?.parameterDefinitions?.find(p => p.name === 'size')
const backgroundParam = model?.parameterDefinitions?.find(p => p.name === 'background')
expect(qualityParam).toBeDefined() expect(qualityParam).toBeDefined()
expect(qualityParam?.type).toBe('string') expect(qualityParam?.type).toBe('string')
expect(qualityParam?.allowedValues).toEqual(expect.arrayContaining(['auto', 'high', 'medium', 'low'])) expect(qualityParam?.allowedValues).toEqual(expect.arrayContaining(['auto', 'high', 'medium', 'low']))
expect(sizeParam).toBeDefined() expect(sizeParam).toBeDefined()
expect(sizeParam?.allowedValues).toContain('1024x1024') expect(sizeParam?.allowedValues).toEqual([
'1024x1024',
'1536x1024',
'1024x1536',
'2048x2048',
'2048x1152',
'3840x2160',
'2160x3840',
'auto'
])
expect(backgroundParam?.allowedValues).toEqual(['auto', 'opaque'])
}) })
}) })
@@ -136,7 +148,7 @@ describe('OpenAIImageAdapter', () => {
}, },
paramOverrides: { paramOverrides: {
quality: 'standard', quality: 'standard',
size: '1024x1024' size: '3840x2160'
} }
} }
@@ -173,6 +185,7 @@ describe('OpenAIImageAdapter', () => {
const [, options] = (global.fetch as any).mock.calls[0] const [, options] = (global.fetch as any).mock.calls[0]
const body = JSON.parse(options.body) const body = JSON.parse(options.body)
expect(body.size).toBe('3840x2160')
expect(body.response_format).toBeUndefined() expect(body.response_format).toBeUndefined()
}) })