fix image generation provider settings

This commit is contained in:
ZegWe 2026-05-22 03:09:28 +08:00 committed by Xubin Ren
parent 65dff4f3a5
commit ffd85a8611
4 changed files with 34 additions and 3 deletions

View File

@ -768,6 +768,13 @@ _OPENAI_ASPECT_RATIO_SIZES = {
"3:4": "1024x1360", "3:4": "1024x1360",
"4:3": "1360x1024", "4:3": "1360x1024",
} }
_OPENAI_GPT_IMAGE_ASPECT_RATIO_SIZES = {
"1:1": "1024x1024",
"16:9": "1536x1024",
"9:16": "1024x1536",
"3:4": "1024x1536",
"4:3": "1536x1024",
}
class OpenAIImageGenerationClient(ImageGenerationProvider): class OpenAIImageGenerationClient(ImageGenerationProvider):
@ -825,7 +832,7 @@ class OpenAIImageGenerationClient(ImageGenerationProvider):
body["response_format"] = "b64_json" body["response_format"] = "b64_json"
body["n"] = 1 body["n"] = 1
size = _openai_size(aspect_ratio, image_size) size = _openai_size(clean_model, aspect_ratio, image_size)
if size: if size:
body["size"] = size body["size"] = size
@ -976,14 +983,20 @@ class CodexImageGenerationClient(ImageGenerationProvider):
def _openai_size( def _openai_size(
model: str,
aspect_ratio: str | None, aspect_ratio: str | None,
image_size: str | None, image_size: str | None,
) -> str: ) -> str:
"""Resolve aspect ratio or image_size to an OpenAI Images API size string.""" """Resolve aspect ratio or image_size to an OpenAI Images API size string."""
if image_size and "x" in image_size.lower(): if image_size and "x" in image_size.lower():
return image_size return image_size
if aspect_ratio and aspect_ratio in _OPENAI_ASPECT_RATIO_SIZES: sizes = (
return _OPENAI_ASPECT_RATIO_SIZES[aspect_ratio] _OPENAI_GPT_IMAGE_ASPECT_RATIO_SIZES
if model.startswith("gpt-image")
else _OPENAI_ASPECT_RATIO_SIZES
)
if aspect_ratio and aspect_ratio in sizes:
return sizes[aspect_ratio]
return "1024x1024" return "1024x1024"

View File

@ -73,12 +73,16 @@ def _mask_secret_hint(secret: str | None) -> str | None:
def _provider_requires_api_key(spec: Any) -> bool: def _provider_requires_api_key(spec: Any) -> bool:
if spec.backend == "azure_openai": if spec.backend == "azure_openai":
return True return True
if spec.is_oauth:
return False
if spec.is_local or spec.is_direct: if spec.is_local or spec.is_direct:
return False return False
return True return True
def _provider_configured_for_settings(spec: Any, provider_config: Any) -> bool: def _provider_configured_for_settings(spec: Any, provider_config: Any) -> bool:
if spec.is_oauth:
return True
if _provider_requires_api_key(spec): if _provider_requires_api_key(spec):
return bool(provider_config.api_key) return bool(provider_config.api_key)
return bool( return bool(

View File

@ -1055,6 +1055,7 @@ async def test_settings_api_returns_safe_subset_and_updates_whitelist(
} }
assert image_providers["openrouter"]["label"] == "OpenRouter" assert image_providers["openrouter"]["label"] == "OpenRouter"
assert image_providers["openrouter"]["configured"] is False assert image_providers["openrouter"]["configured"] is False
assert image_providers["openai_codex"]["configured"] is True
assert image_providers["gemini"]["label"] == "Gemini" assert image_providers["gemini"]["label"] == "Gemini"
assert body["runtime"]["config_path"] == str(config_path) assert body["runtime"]["config_path"] == str(config_path)
workspace_path = body["runtime"]["workspace_path"].replace("\\", "/") workspace_path = body["runtime"]["workspace_path"].replace("\\", "/")

View File

@ -624,6 +624,19 @@ async def test_openai_aspect_ratio_to_size() -> None:
assert fake.calls[0]["json"]["size"] == "1024x1024" assert fake.calls[0]["json"]["size"] == "1024x1024"
@pytest.mark.asyncio
async def test_openai_gpt_image_uses_supported_landscape_size() -> None:
fake = FakeClient(FakeResponse({"data": [{"b64_json": RAW_B64}]}))
client = OpenAIImageGenerationClient(
api_key="sk-openai-test",
client=fake, # type: ignore[arg-type]
)
await client.generate(prompt="draw", model="gpt-image-1", aspect_ratio="16:9")
assert fake.calls[0]["json"]["size"] == "1536x1024"
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_openai_default_size_when_no_aspect_ratio() -> None: async def test_openai_default_size_when_no_aspect_ratio() -> None:
fake = FakeClient(FakeResponse({"data": [{"b64_json": RAW_B64}]})) fake = FakeClient(FakeResponse({"data": [{"b64_json": RAW_B64}]}))