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) }