gateway: a Grok subscription draws any grok-imagine-image* named bare, and makes any grok-imagine-video* (#1857)

A bare grok-imagine-image-2.0 on /v1/images/generations got 404 "magpie
knows no model ... to draw with", while grok/grok-imagine-image-2.0 drew:
a bare name was looked up only in grokDrawers, a fixed list of two. The
same held for a bare video name not in grokVideoModels, though with the
provider named videosCreate already took any grok-imagine-video*.

What changed
- draw.go: resolveDrawing, after an exact match in any provider's
  Drawers, sends a bare name of a subscription's image family to it
  (drawsFamily): grok-imagine-image* on a Grok subscription, gpt-image-*
  on a ChatGPT account (the sibling list, codexDrawers). The vendor draws
  it or refuses it in its own words. Exact matches still win, so an API
  key that lists the model keeps it; a name of no family is still 404.
- drawing_route.go: Codex's per-turn preferred provider takes the same
  family match.
- video.go: bareVideomaker takes any grok-imagine-video* on a Grok
  subscription, matching what videosCreate takes with the provider named.
- AutoDrawer and AutoVideomaker are unchanged (still the listed models).
- A moved Grok user goes through this same gateway code (drawsGrok covers
  the plugin's provider; the plugin only signs), so the Grok plugin needs
  no change and the mover's min stays.

What was run
- New TestGrokDrawsANewerModelByItsBareName (the reporter's request),
  TestCodexDrawsANewerModelByItsBareName, a bare grok-imagine-image-2.0
  in TestGrokPluginDraws (plugin parity) and grok-imagine-video-2.0 in
  TestVideoModelByItsBareName. With the family match switched off by hand
  all four fail with the 404 "magpie knows no model"; restored they pass.
- gofmt -l clean; go vet ./... on darwin, GOOS=linux -tags nogui,
  GOOS=windows; go build on darwin, linux (nogui), windows.
- go test -tags nogui -count=1 ./internal/gateway/... ./internal/imagemcp/...
  ./internal/provider/...: ok (sandbox HOME, private GOCACHE).

Not run
- Not tried against a real Grok or ChatGPT account: the request sent is
  the one the reporter already drew with as grok/grok-imagine-image-2.0;
  only which provider a bare name resolves to changed. Whether chatgpt.com
  serves any other gpt-image-* is the vendor's to answer.
- The full go test ./... was not run.
This commit is contained in:
yetone
2026-10-13 03:13:24 +08:00
parent 248a68598c
commit c83ab81dff
6 changed files with 109 additions and 6 deletions
+30
View File
@@ -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).
+52
View File
@@ -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
+3
View File
@@ -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 {
+7 -1
View File
@@ -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)
}
}
+11 -1
View File
@@ -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
}
+6 -4
View File
@@ -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