From 477aebded53eddb0f67b74be74c57f56f640b1ea Mon Sep 17 00:00:00 2001 From: yep <46060092+yepwait@users.noreply.github.com> Date: Fri, 14 Aug 2026 09:59:18 +0800 Subject: [PATCH] =?UTF-8?q?fix(canvas):=20Grok=20=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E7=94=9F=E6=88=90=20-=20=E7=A7=BB=E9=99=A4=E5=86=B2=E7=AA=81?= =?UTF-8?q?=E7=9A=84=20size=20=E5=8F=82=E6=95=B0=E5=B9=B6=E9=94=81?= =?UTF-8?q?=E5=AE=9A=E8=AF=B7=E6=B1=82=E4=BD=93=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: yepwait <1239738103@qq.com> Original-PR: https://github.com/ddcat-ai/open-ai-canvas/pull/209 --- backend/internal/service/provider.go | 6 +++--- backend/internal/service/provider_request_types.go | 1 - backend/internal/service/provider_test.go | 14 +++++++++++++- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/backend/internal/service/provider.go b/backend/internal/service/provider.go index cea374cd3..3131c4aa2 100644 --- a/backend/internal/service/provider.go +++ b/backend/internal/service/provider.go @@ -751,9 +751,9 @@ func grokImageRequestBody(input canvasGenerationInput) (grokImageRequest, string Prompt: withSystemPrompt(input.Config, input.Prompt), N: 1, ResponseFormat: "url", - Size: strings.TrimSpace(input.Config.Size), - AspectRatio: normalizeGrokImageAspectRatio(input.Config.Size), - Resolution: normalizeGrokImageResolution(input.Config.Quality), + // Grok 图片协议用 aspect_ratio 表达画布比例;同时发送 size 会被上游按 OpenAI 枚举校验并拒绝。 + AspectRatio: normalizeGrokImageAspectRatio(input.Config.Size), + Resolution: normalizeGrokImageResolution(input.Config.Quality), } if len(input.ReferenceImages) == 0 { return body, "/images/generations", nil diff --git a/backend/internal/service/provider_request_types.go b/backend/internal/service/provider_request_types.go index 9ce618fae..91e4e996d 100644 --- a/backend/internal/service/provider_request_types.go +++ b/backend/internal/service/provider_request_types.go @@ -59,7 +59,6 @@ type grokImageRequest struct { Image *grokImageInput `json:"image,omitempty"` N int `json:"n"` ResponseFormat string `json:"response_format"` - Size string `json:"size,omitempty"` AspectRatio string `json:"aspect_ratio,omitempty"` // Resolution 对应 xAI / grok2api 的 resolution(常见 1k / 2k)。 Resolution string `json:"resolution,omitempty"` diff --git a/backend/internal/service/provider_test.go b/backend/internal/service/provider_test.go index fbc3e7bb7..33953c687 100644 --- a/backend/internal/service/provider_test.go +++ b/backend/internal/service/provider_test.go @@ -229,9 +229,21 @@ func TestGrokImageRequestBodyMapsAspectRatio(t *testing.T) { if path != "/images/generations" { t.Fatalf("path = %q", path) } - if body.AspectRatio != "9:16" || body.Size != "9:16" || body.Resolution != "2k" { + if body.AspectRatio != "9:16" || body.Resolution != "2k" { t.Fatalf("body = %#v", body) } + encoded, err := json.Marshal(body) + if err != nil { + t.Fatalf("marshal body: %v", err) + } + var payload map[string]any + if err := json.Unmarshal(encoded, &payload); err != nil { + t.Fatalf("unmarshal body: %v", err) + } + // Grok 使用 aspect_ratio;测试锁定请求 JSON,防止非法 size 字段再次混入。 + if _, exists := payload["size"]; exists { + t.Fatalf("request body must not contain size: %s", encoded) + } if got := normalizeGrokImageAspectRatio("1280x720"); got != "16:9" { t.Fatalf("normalize 1280x720 = %q", got) }