fix(canvas): Grok 图片生成 - 移除冲突的 size 参数并锁定请求体格式
Co-authored-by: yepwait <1239738103@qq.com> Original-PR: https://github.com/ddcat-ai/open-ai-canvas/pull/209
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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"`
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user