diff --git a/internal/gateway/draw.go b/internal/gateway/draw.go index 0ebb70a92..535e96558 100644 --- a/internal/gateway/draw.go +++ b/internal/gateway/draw.go @@ -90,9 +90,39 @@ func resolveDrawing(id string) (provider.Provider, string, bool) { } } } + // a subscription's newer image model, named before magpie lists it + // (grok-imagine-image-2.0, #1857): its vendor draws it, or says why not + for _, p := range provider.All() { + if p.On() && !p.DecideOnly() && drawsFamily(p, id) { + return p, id, true + } + } return provider.Resolve(id) } +// drawsFamily is whether a subscription whose image models magpie lists +// itself (codexDrawers, grokDrawers) draws id, a model of the family those +// are of: any grok-imagine-image* on a Grok subscription, any gpt-image-* +// on a ChatGPT account. The vendor's newer ones work before magpie names +// them, as grok-imagine-video* do for videos; one it doesn't serve is +// refused by the vendor, in its own words. +func drawsFamily(p provider.Provider, id string) bool { + switch { + case drawsGrok(p): + return strings.HasPrefix(id, grokDrawerFamily) + case drawsCodex(p): + return strings.HasPrefix(id, codexDrawerFamily) + } + return false +} + +// The families of the image models a ChatGPT account and a Grok +// subscription draw with (drawsFamily). +const ( + codexDrawerFamily = "gpt-image-" + grokDrawerFamily = "grok-imagine-image" +) + // codexDrawers are the image models a ChatGPT account draws with, at // its Codex backend's images API, as Codex CLI does (gpt-image-2 is the one // it asks for). diff --git a/internal/gateway/draw_test.go b/internal/gateway/draw_test.go index 660e5f318..00c13cc2c 100644 --- a/internal/gateway/draw_test.go +++ b/internal/gateway/draw_test.go @@ -575,6 +575,58 @@ func TestGrokVendorFailuresAreSaid(t *testing.T) { } } +// #1857: grok-imagine-image-2.0 named bare, as the vendor names it, drew +// nothing ("magpie knows no model ... to draw with") while +// grok/grok-imagine-image-2.0 drew: the bare name was looked up in +// grokDrawers alone. Any grok-imagine-image* goes to the Grok subscription +// now, as any grok-imagine-video* does for videos; a name of no image +// family is still turned away. +func TestGrokDrawsANewerModelByItsBareName(t *testing.T) { + grokSignedIn(t) + up := newGrokMedia(t) + s := New() + code, a, raw := postImages(t, s, "/v1/images/generations", "application/json", `{"model":"grok-imagine-image-2.0","prompt":"a red apple on a table","n":1,"size":"1024x1024"}`) + if code != 200 || len(a.Data) != 1 { + t.Fatalf("%d %s", code, raw) + } + if sent := up.body("/v1/images/generations", 0); !strings.Contains(sent, `"model":"grok-imagine-image-2.0"`) { + t.Fatalf("asked %s", sent) + } + code, _, raw = postImages(t, s, "/v1/images/generations", "application/json", `{"model":"grok-imagine-nothing","prompt":"a red apple"}`) + if code != 404 || !strings.Contains(raw, "knows no model") { + t.Fatalf("a name of no image family: %d %s", code, raw) + } +} + +// The ChatGPT account's sibling of #1857: a gpt-image-* magpie doesn't list +// yet, named bare, is asked of the account, not turned away by magpie. +func TestCodexDrawsANewerModelByItsBareName(t *testing.T) { + codexSignedIn(t) + var mu sync.Mutex + var bodies []string + up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + b, _ := io.ReadAll(r.Body) + mu.Lock() + bodies = append(bodies, string(b)) + mu.Unlock() + w.Header().Set("Content-Type", "application/json") + io.WriteString(w, `{"created":1,"output_format":"png","data":[{"b64_json":"`+base64.StdEncoding.EncodeToString(pngBytes)+`"}]}`) + })) + defer up.Close() + was := provider.CodexBase + provider.CodexBase = up.URL + "/backend-api/codex" + defer func() { provider.CodexBase = was }() + code, a, raw := postImages(t, New(), "/v1/images/generations", "application/json", `{"model":"gpt-image-3","prompt":"a magpie"}`) + if code != 200 || len(a.Data) != 1 { + t.Fatalf("%d %s", code, raw) + } + mu.Lock() + defer mu.Unlock() + if len(bodies) != 1 || !strings.Contains(bodies[0], `"model":"gpt-image-3"`) { + t.Fatalf("asked %v", bodies) + } +} + // #1837: Agnes answers its image model on chat completions with 400 "is an // image model. Use /v1/images/generations" (the reporter's words, as omp // showed them). Its name reads like gemini's -flash image models, which diff --git a/internal/gateway/drawing_route.go b/internal/gateway/drawing_route.go index 952696c4f..201ae5979 100644 --- a/internal/gateway/drawing_route.go +++ b/internal/gateway/drawing_route.go @@ -18,6 +18,9 @@ func (server *Server) drawingModel(request *http.Request, id string) (provider.P return *matched, id, true } } + if drawsFamily(*matched, id) { + return *matched, id, true + } } } if selected, ok := drawer(); ok { diff --git a/internal/gateway/plugin_grok_parity_test.go b/internal/gateway/plugin_grok_parity_test.go index 5851ac40a..114ebb043 100644 --- a/internal/gateway/plugin_grok_parity_test.go +++ b/internal/gateway/plugin_grok_parity_test.go @@ -79,9 +79,15 @@ func TestGrokPluginDraws(t *testing.T) { if code != 200 || len(a.Data) != 1 { t.Fatalf("%d %s", code, raw) } + // a newer image model by its bare name, as with the built-in (#1857, + // TestGrokDrawsANewerModelByItsBareName) + code, a, raw = postImages(t, New(), "/v1/images/generations", "application/json", `{"model":"grok-imagine-image-2.0","prompt":"a magpie"}`) + if code != 200 || len(a.Data) != 1 { + t.Fatalf("bare grok-imagine-image-2.0: %d %s", code, raw) + } mu.Lock() defer mu.Unlock() - if len(paths) != 1 || paths[0] != "/v1/images/generations" || !strings.Contains(bodies[0], `"model":"grok-imagine-image"`) || !strings.Contains(bodies[0], `"aspect_ratio":"16:9"`) { + if len(paths) != 2 || paths[0] != "/v1/images/generations" || !strings.Contains(bodies[0], `"model":"grok-imagine-image"`) || !strings.Contains(bodies[0], `"aspect_ratio":"16:9"`) || !strings.Contains(bodies[1], `"model":"grok-imagine-image-2.0"`) { t.Fatalf("asked %v %v", paths, bodies) } } diff --git a/internal/gateway/video.go b/internal/gateway/video.go index 2c32c2110..58895faaa 100644 --- a/internal/gateway/video.go +++ b/internal/gateway/video.go @@ -189,9 +189,19 @@ func bareVideomaker(name string) (provider.Provider, string, bool) { } } } + // any grok-imagine-video*, as videosCreate takes with the provider + // named: the vendor's newer ones work before magpie lists them (#1857) + for _, p := range provider.All() { + if p.On() && !p.DecideOnly() && drawsGrok(p) && strings.HasPrefix(name, grokVideoFamily) { + return p, name, true + } + } return provider.Provider{}, "", false } +// grokVideoFamily is the family of a Grok subscription's video models. +const grokVideoFamily = "grok-imagine-video" + // filming is one videos request, whichever shape it came in. type filming struct { Model, Prompt, Seconds, Size string @@ -674,7 +684,7 @@ func (s *Server) videosCreate(w http.ResponseWriter, r *http.Request) { custom := !remote && filmsCustom(p, model) volcengine := p.Preset == "volcengine" && slices.ContainsFunc(p.PlanVideos(), func(m catalog.Model) bool { return m.ID == model }) // any grok-imagine-video*, not only those listed: the vendor's newer ones work before magpie names them - if !remote && !custom && !volcengine && (!drawsGrok(p) || !strings.HasPrefix(model, "grok-imagine-video")) { + if !remote && !custom && !volcengine && (!drawsGrok(p) || !strings.HasPrefix(model, grokVideoFamily)) { fail(400, fmt.Sprintf(`%s/%s can't make videos: magpie makes videos with a Grok subscription's grok-imagine-video, Volcengine Agent Plan's Seedance models, another magpie's, and the models a provider's own model list marks "kind": "video"`, p.ID, model)) return } diff --git a/internal/gateway/video_test.go b/internal/gateway/video_test.go index 64adc8b60..b875c9e12 100644 --- a/internal/gateway/video_test.go +++ b/internal/gateway/video_test.go @@ -557,15 +557,17 @@ func TestVideoModelByItsBareName(t *testing.T) { grokSignedIn(t) up := newGrokMedia(t) s := New() - // in this order: the second request is the one the upstream's body is checked for - for _, name := range []string{"grok-imagine-video", "grok-imagine-video-1.5"} { + // grok-imagine-video-2.0 isn't listed (grokVideoModels): any + // grok-imagine-video* goes to Grok by its bare name, as with the + // provider named (#1857) + for _, name := range []string{"grok-imagine-video", "grok-imagine-video-2.0", "grok-imagine-video-1.5"} { code, obj, raw, _ := request(t, s, "POST", "/v1/videos", "application/json", `{"prompt":"a kite","model":"`+name+`"}`) if code != 200 || obj["model"] != "grok/"+name { t.Errorf("%s: %d %s", name, code, raw) } } - if !strings.Contains(up.body("/v1/videos/generations", 1), `"model":"grok-imagine-video-1.5"`) { - t.Fatalf("asked %s", up.body("/v1/videos/generations", 1)) + if !strings.Contains(up.body("/v1/videos/generations", 1), `"model":"grok-imagine-video-2.0"`) || !strings.Contains(up.body("/v1/videos/generations", 2), `"model":"grok-imagine-video-1.5"`) { + t.Fatalf("asked %s, %s", up.body("/v1/videos/generations", 1), up.body("/v1/videos/generations", 2)) } for name, c := range map[string]struct { code int