From d5fea7f02e616a54cad596eefab7c6ea4b825f63 Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 3 Nov 2026 02:12:31 +0800 Subject: [PATCH] gateway: a relay whose list names gpt-image-2 under a namespace draws it (#4432) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit loscoy's retest (v0.1.2670) still got 404 "magpie knows no model "gpt-image-2" to draw with" for the bare name a Codex harness asks for: the relay's cached list names the family under a namespace (openai/gpt-image-1, openai/gpt-image-2 …), and drawsFamily, which a130035a0 narrowed to a drawer whose id starts gpt-image-, read those as none of the family, so the relay that draws was passed over. drawsFamily now reads each drawer by the part of its id past the last "/". The narrowing stands: only a relay whose own list draws a GPT Image takes the family. A new drawnAs sends the relay the id its list names the model by (openai/gpt-image-2 for gpt-image-2), on both the images route (resolveDrawing) and Codex's native image tool (drawingModel); a name the list doesn't carry goes as asked, for the vendor to serve or refuse in its own words. ggbdpq's patch (via gua2100) sent the bare name upstream; loscoy asked whether the namespace had to be kept, and a relay that lists only openai/… may not know the bare one, so the listed id is sent. Run at this tree on mote: go test ./internal/gateway -run 'Draw|Drawing|Relay|Codex|UpBytes' -count=1: ok. gofmt -l clean. Broken by hand: drawsFamily back to the whole id: TestRelayDrawsThe GPTImageFamilyFromANamespacedList, TestCodexNativeDrawsItsOwnModelOnA NamespacedRelay and TestCodexNativeDrawsByTheRelaysNamespacedName red; drawnAs returning the bare id: the first and last red. Not tried: loscoy's real relay; the namespaced id is what its list says, not seen on its wire. Co-authored-by: ggbdpq --- internal/gateway/draw.go | 30 +++++++-- internal/gateway/drawing_resolve_test.go | 83 ++++++++++++++++++++++++ internal/gateway/drawing_route.go | 2 +- 3 files changed, 107 insertions(+), 8 deletions(-) diff --git a/internal/gateway/draw.go b/internal/gateway/draw.go index 47551a88e..faa1d1083 100644 --- a/internal/gateway/draw.go +++ b/internal/gateway/draw.go @@ -166,22 +166,36 @@ func resolveDrawing(id string) (provider.Provider, string, bool) { // (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 p, drawnAs(p, id), true } } return provider.Resolve(id) } +// drawnAs is the id p is asked to draw id, a bare name of the family +// (drawsFamily), by: the one p's own list names it by under a namespace +// (openai/gpt-image-2 for gpt-image-2, loscoy's relay, #4432), as p +// serves it, else id as asked. +func drawnAs(p provider.Provider, id string) string { + for _, m := range Drawers(p) { + if m.ID != id && strings.HasSuffix(m.ID, "/"+id) { + return m.ID + } + } + return 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, and any gpt-image-* on a keyed relay at a Responses // base (a Codex backend relay such as sub2api) whose own list draws one of -// that family already (#4432). 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. A relay whose list draws no -// GPT Image isn't taken to: a Codex turn on it draws with the Settings -// pick, as it did (TestCodexDrawingFallsBackToImageSetting). +// that family already (#4432), by the name past the namespace its list may +// carry (openai/gpt-image-2, loscoy's). 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. A relay whose list +// draws no GPT Image isn't taken to: a Codex turn on it draws with the +// Settings pick, as it did (TestCodexDrawingFallsBackToImageSetting). func drawsFamily(p provider.Provider, id string) bool { switch { case drawsGrok(p): @@ -189,7 +203,9 @@ func drawsFamily(p provider.Provider, id string) bool { case drawsCodex(p): return strings.HasPrefix(id, codexDrawerFamily) case p.Account == nil && p.Base(provider.Responses) != "" && strings.HasPrefix(id, codexDrawerFamily): - return slices.ContainsFunc(Drawers(p), func(m catalog.Model) bool { return strings.HasPrefix(m.ID, codexDrawerFamily) }) + return slices.ContainsFunc(Drawers(p), func(m catalog.Model) bool { + return strings.HasPrefix(m.ID[strings.LastIndexByte(m.ID, '/')+1:], codexDrawerFamily) + }) } return false } diff --git a/internal/gateway/drawing_resolve_test.go b/internal/gateway/drawing_resolve_test.go index a81d31828..29e187976 100644 --- a/internal/gateway/drawing_resolve_test.go +++ b/internal/gateway/drawing_resolve_test.go @@ -174,6 +174,89 @@ func TestRelayDrawingNoGPTImageClaimsNoneOfTheFamily(t *testing.T) { } } +// loscoy's relay (v0.1.2670) namespaces its list — openai/gpt-image-1, +// openai/gpt-image-2 and the rest, all Draws (#4432): the family is the +// name's own past the namespace, as the bare one a Codex harness asks +// its provider for. A relay whose list draws none of the family is still +// not taken to (TestRelayDrawingNoGPTImageClaimsNoneOfTheFamily). +func TestRelayDrawsTheGPTImageFamilyFromANamespacedList(t *testing.T) { + fresh(t) + e := &easel{} + up := httptest.NewServer(e) + defer up.Close() + p := provider.Provider{ID: "relay", Name: "Relay", Responses: up.URL + "/v1", Key: "key"} + if err := provider.Save(p); err != nil { + t.Fatal(err) + } + if err := catalog.SaveLive(p.ID, p.Responses, []catalog.Model{ + {ID: "text"}, + {ID: "openai/gpt-image-1", Draws: true}, + {ID: "openai/gpt-image-1.5", Draws: true}, + {ID: "openai/gpt-image-2", Draws: true}, + {ID: "openai/gpt-image-2.5-flare", Draws: true}, + }); err != nil { + t.Fatal(err) + } + s := New() + // the name its list has it by goes upstream, as the relay serves it; + // one the list doesn't name goes as asked, for the relay to answer + for _, c := range []struct{ asked, want string }{ + {"gpt-image-2", "openai/gpt-image-2"}, + {"gpt-image-future", "gpt-image-future"}, + } { + before := len(e.got("/v1/images/generations")) + code, a, raw := postImages(t, s, "/v1/images/generations", "application/json", `{"model":"`+c.asked+`","prompt":"a magpie"}`) + if code != 200 || a.Model != "relay/"+c.want { + t.Fatalf("%s: %d %s", c.asked, code, raw) + } + requests := e.got("/v1/images/generations") + if len(requests) != before+1 || !strings.Contains(requests[before], `"model":"`+c.want+`"`) { + t.Fatalf("%s: asked %v", c.asked, requests) + } + } +} + +// Codex's native image tool draws with its turn's provider's own list, +// namespaced or not: the bare gpt-image-2 it asks for draws there as +// itself, not as the Settings' pick instead of it (#4432). +func TestCodexNativeDrawsItsOwnModelOnANamespacedRelay(t *testing.T) { + fresh(t) + relay := drawingRelay(t, "relay", "openai/gpt-image-2.5-flare") + st := settings.Load() + st.ImageGen = "relay/openai/gpt-image-2.5-flare" + if err := settings.Save(st); err != nil { + t.Fatal(err) + } + s := New() + drawingMainTurn(t, s, "relay/text", "session-a", "turn-a", "", "magpie-codex") + code, answer, raw := drawingToolCall(t, s, "/v1/images/generations", "gpt-image-2", "", "turn-a", "magpie-codex") + if code != 200 || answer.Model != "relay/gpt-image-2" { + t.Fatalf("%d %s", code, raw) + } + requests := relay.got("/v1/images/generations") + if len(requests) != 1 || !strings.Contains(requests[0], `"model":"gpt-image-2"`) { + t.Fatalf("asked %v", requests) + } +} + +// Codex's native image tool on a turn whose relay lists the one it asks +// for under a namespace draws it by that listed name (loscoy's +// openai/gpt-image-2, #4432), as the relay serves it. +func TestCodexNativeDrawsByTheRelaysNamespacedName(t *testing.T) { + fresh(t) + relay := drawingRelay(t, "relay", "openai/gpt-image-2.5-flare", "openai/gpt-image-2") + s := New() + drawingMainTurn(t, s, "relay/text", "session-a", "turn-a", "", "magpie-codex") + code, answer, raw := drawingToolCall(t, s, "/v1/images/generations", "gpt-image-2", "", "turn-a", "magpie-codex") + if code != 200 || answer.Model != "relay/openai/gpt-image-2" { + t.Fatalf("%d %s", code, raw) + } + requests := relay.got("/v1/images/generations") + if len(requests) != 1 || !strings.Contains(requests[0], `"model":"openai/gpt-image-2"`) { + t.Fatalf("asked %v", requests) + } +} + // Codex's native image tool asks its turn's provider for the one it names // (gpt-image-2, #870): on a Codex backend whose list names another of the // family, it draws there as the one it asked for, not as magpie's pick diff --git a/internal/gateway/drawing_route.go b/internal/gateway/drawing_route.go index 201ae5979..628b82596 100644 --- a/internal/gateway/drawing_route.go +++ b/internal/gateway/drawing_route.go @@ -19,7 +19,7 @@ func (server *Server) drawingModel(request *http.Request, id string) (provider.P } } if drawsFamily(*matched, id) { - return *matched, id, true + return *matched, drawnAs(*matched, id), true } } }