images: every provider's image models and the Codex subscription's GPT Image 2 / 2.5 can draw (为什么生图模型只有这几个?只有OpenRouter这个provider才提供…CodeX Subscription里面也有GPT Image 2和GPT Image 2.5) — Settings → Images → Image generation listed only OpenRouter's, for two reasons: a vendor's model list was cut to the models agents chat with before it was kept, so a relay's gpt-image, flux or Gemini image models were gone by the time the picker looked; and a subscription was never asked. Now the list is kept whole, image models marked as drawing and still never offered to agents to chat with, and the picker has them per provider; a Codex (ChatGPT) account draws with GPT Image 2 and 2.5 on chatgpt.com's own images API (signed as Codex, generations and edits, an edit as JSON with each image a data URL, since the backend turns multipart away), and Automatic prefers it, as a plan's images come with it. AIHubMix answers a Gemini image model's chat with the image in multi_mod_content's inline_data, which is now read. Checked in a sandbox HOME against the real vendors: the picker lists OpenRouter's, AIHubMix's and Codex's, Automatic is GPT Image 2.5 · Codex; codex/gpt-image-2 drew a 1254² PNG and edited it (night-blue background), aihubmix/gemini-2.5-flash-image drew a PNG. The backend takes any model name, so whether 2.5 draws differently from 2 can't be told from outside
This commit is contained in:
@@ -50,6 +50,10 @@ type Model struct {
|
||||
// Fast is set on a model Codex may ask for priority processing (its
|
||||
// Fast mode): one a ChatGPT account serves.
|
||||
Fast bool `json:",omitempty"`
|
||||
// Draws is set on a vendor-listed model that makes images (gpt-image-1,
|
||||
// a relay's flux): kept with the list for Settings → Images, never
|
||||
// offered to agents as a model to talk to.
|
||||
Draws bool `json:",omitempty"`
|
||||
}
|
||||
|
||||
func imageInput(modalities []string) *bool {
|
||||
|
||||
@@ -32,17 +32,53 @@ type liveFile struct {
|
||||
Models []Model `json:"models"`
|
||||
}
|
||||
|
||||
// Live returns the model list last fetched from the provider, if any.
|
||||
func Live(provider string) (models []Model, fetched time.Time, ok bool) {
|
||||
f, err := filememo.Read("live models", LivePath(provider), func(b []byte) (liveFile, error) {
|
||||
func readLive(provider string) (liveFile, error) {
|
||||
return filememo.Read("live models", LivePath(provider), func(b []byte) (liveFile, error) {
|
||||
var f liveFile
|
||||
err := json.Unmarshal(b, &f)
|
||||
return f, err
|
||||
})
|
||||
if err != nil || len(f.Models) == 0 {
|
||||
}
|
||||
|
||||
// Live returns the model list last fetched from the provider, if any: the
|
||||
// models to talk to, without the ones that draw (LiveDrawers).
|
||||
func Live(provider string) (models []Model, fetched time.Time, ok bool) {
|
||||
f, err := readLive(provider)
|
||||
if err != nil {
|
||||
return nil, time.Time{}, false
|
||||
}
|
||||
return slices.Clone(f.Models), f.Fetched, true
|
||||
ms := Chat(f.Models)
|
||||
if len(ms) == 0 {
|
||||
return nil, time.Time{}, false
|
||||
}
|
||||
return ms, f.Fetched, true
|
||||
}
|
||||
|
||||
// LiveDrawers are the models in the provider's fetched list that make
|
||||
// images, in the list's order.
|
||||
func LiveDrawers(provider string) []Model {
|
||||
f, err := readLive(provider)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
var out []Model
|
||||
for _, m := range f.Models {
|
||||
if m.Draws {
|
||||
out = append(out, m)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Chat is ms without the models that draw.
|
||||
func Chat(ms []Model) []Model {
|
||||
out := make([]Model, 0, len(ms))
|
||||
for _, m := range ms {
|
||||
if !m.Draws {
|
||||
out = append(out, m)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// SaveLive stores a fetched list; an empty list forgets it.
|
||||
@@ -224,7 +260,13 @@ func fetchOne(ctx context.Context, url, key string, anthropic bool, headers map[
|
||||
if id == "" {
|
||||
id = r.Name
|
||||
}
|
||||
if id == "" || !textModel(mdModel{ID: id}) {
|
||||
if id == "" {
|
||||
continue
|
||||
}
|
||||
// a model that draws is kept, marked, for Settings → Images; any
|
||||
// other that isn't for text (embeddings, speech) is left out
|
||||
drawer := DrawsID(id) && !strings.Contains(strings.ToLower(id), "deep-research")
|
||||
if !drawer && !textModel(mdModel{ID: id}) {
|
||||
continue
|
||||
}
|
||||
name := r.DisplayName
|
||||
@@ -232,7 +274,7 @@ func fetchOne(ctx context.Context, url, key string, anthropic bool, headers map[
|
||||
name = id
|
||||
}
|
||||
input := imageInput(r.Modalities.Input)
|
||||
m := Model{ID: id, Name: name, ImageInput: input, APIs: EndpointAPIs(r.Endpoints)}
|
||||
m := Model{ID: id, Name: name, ImageInput: input, APIs: EndpointAPIs(r.Endpoints), Draws: drawer}
|
||||
if n, ok := r.ContextLength.(float64); ok && n > 0 {
|
||||
m.Context = int(n)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package gateway
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"cmp"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
@@ -59,11 +60,33 @@ func drawer() (string, bool) {
|
||||
return m, m != ""
|
||||
}
|
||||
|
||||
// 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).
|
||||
var codexDrawers = []catalog.Model{
|
||||
{ID: "gpt-image-2", Name: "GPT Image 2", Released: "2026-01-01"},
|
||||
{ID: "gpt-image-2.5", Name: "GPT Image 2.5", Released: "2026-06-01"},
|
||||
}
|
||||
|
||||
// drawsCodex is whether p is a ChatGPT account, which draws at its Codex
|
||||
// backend's images API.
|
||||
func drawsCodex(p provider.Provider) bool {
|
||||
return p.Account != nil && p.Account.Agent == "codex" && p.Base(provider.Responses) != ""
|
||||
}
|
||||
|
||||
// Drawers are the models a provider can draw with: its catalogs' models
|
||||
// that make images, and those of its own list named for images.
|
||||
// that make images, those its own model list names that do, and those of
|
||||
// its own picks named for images; a ChatGPT account's GPT Image.
|
||||
func Drawers(p provider.Provider) []catalog.Model {
|
||||
// a subscription is asked through its agent's own API, which draws
|
||||
// nothing magpie can ask for yet
|
||||
if drawsCodex(p) {
|
||||
out := slices.Clone(codexDrawers)
|
||||
for i := range out {
|
||||
out[i].Provider = p.ID
|
||||
}
|
||||
return out
|
||||
}
|
||||
// any other subscription is asked through its agent's own API, which
|
||||
// draws nothing magpie can ask for yet
|
||||
if p.Account != nil || p.Base(provider.Chat) == "" {
|
||||
return nil
|
||||
}
|
||||
@@ -77,6 +100,13 @@ func Drawers(p provider.Provider) []catalog.Model {
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, m := range catalog.LiveDrawers(p.ID) {
|
||||
if !have[m.ID] {
|
||||
have[m.ID] = true
|
||||
m.Provider = p.ID
|
||||
out = append(out, m)
|
||||
}
|
||||
}
|
||||
for _, id := range p.Models {
|
||||
if !have[id] && catalog.DrawsID(id) {
|
||||
have[id] = true
|
||||
@@ -87,8 +117,9 @@ func Drawers(p provider.Provider) []catalog.Model {
|
||||
}
|
||||
|
||||
// AutoDrawer is the model magpie draws with when the Settings name none:
|
||||
// the cheapest of the models the API keys set up can draw with, the newest
|
||||
// of those whose price isn't known. "" when none can.
|
||||
// a ChatGPT account's, which its plan pays for, else the cheapest of the
|
||||
// models the API keys set up can draw with, the newest of those whose price
|
||||
// isn't known. "" when none can.
|
||||
func AutoDrawer() string {
|
||||
best, bestCost, bestDate := "", 0.0, ""
|
||||
for _, p := range provider.All() {
|
||||
@@ -97,7 +128,10 @@ func AutoDrawer() string {
|
||||
}
|
||||
for _, m := range Drawers(p) {
|
||||
cost := 1e9
|
||||
if m.Price != nil {
|
||||
switch {
|
||||
case drawsCodex(p):
|
||||
cost = 0 // a ChatGPT plan's images come with it
|
||||
case m.Price != nil:
|
||||
cost = m.Price.Input + m.Price.Output
|
||||
}
|
||||
if best == "" || cost < bestCost || cost == bestCost && m.Released > bestDate {
|
||||
@@ -414,6 +448,9 @@ func viaFor(p provider.Provider, model string) drawVia {
|
||||
// draw asks the provider for d's images, on the API model draws on there,
|
||||
// and on the other when that one isn't served.
|
||||
func (s *Server) draw(ctx context.Context, p provider.Provider, model string, d drawing) (drawn, int, error) {
|
||||
if drawsCodex(p) {
|
||||
return s.drawImages(ctx, p, model, d)
|
||||
}
|
||||
if p.Base(provider.Chat) == "" {
|
||||
return drawn{}, 400, fmt.Errorf("%s can't draw: magpie draws only through an OpenAI-compatible API, and %s has none", p.Name, p.Name)
|
||||
}
|
||||
@@ -453,6 +490,12 @@ func (s *Server) send(ctx context.Context, p provider.Provider, url, contentType
|
||||
if err := p.Sign(ctx, req, provider.Chat, body); err != nil {
|
||||
return nil, 502, err
|
||||
}
|
||||
if drawsCodex(p) {
|
||||
// the images API answers in JSON, not the stream Codex's
|
||||
// signing asks of its Responses; the id is Codex CLI's own
|
||||
req.Header.Set("Accept", "application/json")
|
||||
req.Header.Set("x-codex-imagegen-request-id", newUUID())
|
||||
}
|
||||
} else {
|
||||
// Google's API keys go in their own header
|
||||
req.Header.Set("x-goog-api-key", p.Key)
|
||||
@@ -502,6 +545,9 @@ func vendorMessage(b []byte) string {
|
||||
// sent along.
|
||||
func (s *Server) drawImages(ctx context.Context, p provider.Provider, model string, d drawing) (drawn, int, error) {
|
||||
base := strings.TrimRight(p.Base(provider.Chat), "/")
|
||||
if drawsCodex(p) {
|
||||
base = strings.TrimRight(p.Base(provider.Responses), "/")
|
||||
}
|
||||
var body []byte
|
||||
var ct, url string
|
||||
m := strings.ToLower(model)
|
||||
@@ -518,6 +564,24 @@ func (s *Server) drawImages(ctx context.Context, p provider.Provider, model stri
|
||||
}
|
||||
body, _ = json.Marshal(req)
|
||||
ct, url = "application/json", base+"/images/generations"
|
||||
} else if drawsCodex(p) {
|
||||
// ChatGPT's backend takes an edit only as JSON, each image a data URL
|
||||
req := map[string]any{"model": model, "prompt": d.Prompt, "n": d.N}
|
||||
for k, v := range map[string]string{"size": d.Size, "quality": d.Quality, "background": d.Background, "output_format": d.Format} {
|
||||
if v != "" {
|
||||
req[k] = v
|
||||
}
|
||||
}
|
||||
var imgs []map[string]string
|
||||
for _, pic := range d.Images {
|
||||
imgs = append(imgs, map[string]string{"image_url": pic.dataURL()})
|
||||
}
|
||||
req["images"] = imgs
|
||||
if d.Mask != nil {
|
||||
req["mask"] = map[string]string{"image_url": d.Mask.dataURL()}
|
||||
}
|
||||
body, _ = json.Marshal(req)
|
||||
ct, url = "application/json", base+"/images/edits"
|
||||
} else {
|
||||
var buf bytes.Buffer
|
||||
mw := multipart.NewWriter(&buf)
|
||||
@@ -671,6 +735,13 @@ func (s *Server) drawChat(ctx context.Context, p provider.Provider, model string
|
||||
URL string `json:"url"`
|
||||
} `json:"image_url"`
|
||||
} `json:"images"`
|
||||
// AIHubMix's Gemini: the parts as Gemini gives them
|
||||
MultiMod []struct {
|
||||
InlineData struct {
|
||||
Data string `json:"data"`
|
||||
MimeType string `json:"mime_type"`
|
||||
} `json:"inline_data"`
|
||||
} `json:"multi_mod_content"`
|
||||
} `json:"message"`
|
||||
} `json:"choices"`
|
||||
Usage struct {
|
||||
@@ -690,6 +761,11 @@ func (s *Server) drawChat(ctx context.Context, p provider.Provider, model string
|
||||
for _, im := range msg.Images {
|
||||
srcs = append(srcs, im.ImageURL.URL)
|
||||
}
|
||||
for _, part := range msg.MultiMod {
|
||||
if in := part.InlineData; in.Data != "" {
|
||||
srcs = append(srcs, "data:"+cmp.Or(in.MimeType, "image/png")+";base64,"+in.Data)
|
||||
}
|
||||
}
|
||||
var text string
|
||||
if json.Unmarshal(msg.Content, &text) != nil {
|
||||
var parts []struct {
|
||||
|
||||
@@ -48,6 +48,11 @@ func (e *easel) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
if strings.Contains(string(body), "gemini") {
|
||||
// AIHubMix's shape: Gemini's parts, under multi_mod_content
|
||||
io.WriteString(w, `{"choices":[{"message":{"role":"assistant","content":"Here you go!","multi_mod_content":[{"text":"Here you go! ","inline_data":{}},{"inline_data":{"data":"`+b64+`","mime_type":"image/png"}}]}}]}`)
|
||||
return
|
||||
}
|
||||
io.WriteString(w, `{"choices":[{"message":{"role":"assistant","content":"Here it is.","images":[{"type":"image_url","image_url":{"url":"data:image/png;base64,`+b64+`"}}]}}],"usage":{"prompt_tokens":3,"completion_tokens":50}}`)
|
||||
default:
|
||||
w.WriteHeader(404)
|
||||
@@ -66,7 +71,7 @@ func easeled(t *testing.T) (*Server, *easel) {
|
||||
e := &easel{}
|
||||
up := httptest.NewServer(e)
|
||||
t.Cleanup(up.Close)
|
||||
if err := provider.Save(provider.Provider{ID: "art", Name: "Art", Chat: up.URL + "/v1", Key: "key", Models: []string{"text", "gpt-image-1", "painter-image-preview"}}); err != nil {
|
||||
if err := provider.Save(provider.Provider{ID: "art", Name: "Art", Chat: up.URL + "/v1", Key: "key", Models: []string{"text", "gpt-image-1", "painter-image-preview", "gemini-2.5-flash-image"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := catalog.SaveLive("art", up.URL+"/v1", []catalog.Model{{ID: "text"}}); err != nil {
|
||||
@@ -125,6 +130,17 @@ func TestChatModelDrawsWithModalities(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatModelsGeminiPartsAreImages(t *testing.T) {
|
||||
s, _ := easeled(t)
|
||||
code, a, raw := postImages(t, s, "/v1/images/generations", "application/json", `{"model":"art/gemini-2.5-flash-image","prompt":"a magpie"}`)
|
||||
if code != 200 || len(a.Data) != 1 || a.Text != "Here you go!" {
|
||||
t.Fatalf("%d %s", code, raw)
|
||||
}
|
||||
if b, _ := base64.StdEncoding.DecodeString(a.Data[0].B64); string(b) != string(pngBytes) {
|
||||
t.Fatalf("image %q", b)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEditSendsTheImages(t *testing.T) {
|
||||
s, e := easeled(t)
|
||||
img := "data:image/png;base64," + base64.StdEncoding.EncodeToString(pngBytes)
|
||||
@@ -224,3 +240,83 @@ func TestCatalogDrawers(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A vendor's own list that names image models: they draw, and they aren't
|
||||
// among the models offered to talk to.
|
||||
func TestListedImageModelsDraw(t *testing.T) {
|
||||
fresh(t)
|
||||
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
io.WriteString(w, `{"data":[{"id":"gpt-5.5"},{"id":"gpt-image-2"},{"id":"flux-kontext-pro"},{"id":"text-embedding-3-small"}]}`)
|
||||
}))
|
||||
defer up.Close()
|
||||
p := provider.Provider{ID: "relay", Name: "Relay", Chat: up.URL + "/v1", Key: "key"}
|
||||
if err := provider.Save(p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ms, err := p.Fetch(t.Context())
|
||||
if err != nil || len(ms) != 1 || ms[0].ID != "gpt-5.5" {
|
||||
t.Fatalf("fetched %v %v", ms, err)
|
||||
}
|
||||
if live, _, _ := catalog.Live("relay"); len(live) != 1 {
|
||||
t.Fatalf("live %v", live)
|
||||
}
|
||||
var ids []string
|
||||
for _, m := range Drawers(p) {
|
||||
ids = append(ids, m.ID)
|
||||
}
|
||||
if strings.Join(ids, ",") != "gpt-image-2,flux-kontext-pro" {
|
||||
t.Fatalf("drawers %v", ids)
|
||||
}
|
||||
}
|
||||
|
||||
// A ChatGPT account draws with GPT Image at its Codex backend's images API,
|
||||
// signed as Codex, and Automatic picks it, as the plan pays for it.
|
||||
func TestCodexAccountDraws(t *testing.T) {
|
||||
codexSignedIn(t)
|
||||
var mu sync.Mutex
|
||||
var paths, bodies []string
|
||||
var head http.Header
|
||||
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
b, _ := io.ReadAll(r.Body)
|
||||
mu.Lock()
|
||||
paths, bodies, head = append(paths, r.URL.Path), append(bodies, string(b)), r.Header.Clone()
|
||||
mu.Unlock()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
io.WriteString(w, `{"created":1,"output_format":"png","data":[{"b64_json":"`+base64.StdEncoding.EncodeToString(pngBytes)+`","generation_id":"g1"}],"usage":{"input_tokens":5,"output_tokens":196}}`)
|
||||
}))
|
||||
defer up.Close()
|
||||
was := provider.CodexBase
|
||||
provider.CodexBase = up.URL + "/backend-api/codex"
|
||||
defer func() { provider.CodexBase = was }()
|
||||
p, err := provider.Find("codex")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ds := Drawers(*p); len(ds) != 2 || ds[0].ID != "gpt-image-2" || ds[1].ID != "gpt-image-2.5" {
|
||||
t.Fatalf("drawers %v", ds)
|
||||
}
|
||||
if m, ok := drawer(); !ok || m != "codex/gpt-image-2.5" {
|
||||
t.Fatalf("drawer = %q %v", m, ok)
|
||||
}
|
||||
s := New()
|
||||
code, a, raw := postImages(t, s, "/v1/images/generations", "application/json", `{"model":"codex/gpt-image-2","prompt":"a magpie","size":"1024x1024"}`)
|
||||
if code != 200 || len(a.Data) != 1 || a.Usage.Output != 196 {
|
||||
t.Fatalf("%d %s", code, raw)
|
||||
}
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if paths[0] != "/backend-api/codex/images/generations" || !strings.Contains(bodies[0], `"model":"gpt-image-2"`) {
|
||||
t.Fatalf("asked %v %v", paths, bodies)
|
||||
}
|
||||
if head.Get("chatgpt-account-id") != "acct-1" || head.Get("Accept") != "application/json" || head.Get("originator") != "codex_cli_rs" || head.Get("x-codex-imagegen-request-id") == "" {
|
||||
t.Fatalf("headers %v", head)
|
||||
}
|
||||
// an edit goes as JSON, the image a data URL: the backend turns multipart away
|
||||
mu.Unlock()
|
||||
img := "data:image/png;base64," + base64.StdEncoding.EncodeToString(pngBytes)
|
||||
code, _, raw = postImages(t, s, "/v1/images/edits", "application/json", `{"model":"codex/gpt-image-2","prompt":"bluer","images":[{"image_url":"`+img+`"}]}`)
|
||||
mu.Lock()
|
||||
if code != 200 || paths[1] != "/backend-api/codex/images/edits" || head.Get("Content-Type") != "application/json" || !strings.Contains(bodies[1], `"images":[{"image_url":"data:image/png;base64,`) {
|
||||
t.Fatalf("%d %s; asked %v %s", code, raw, paths, head.Get("Content-Type"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,7 +105,7 @@ func (p Provider) Fetch(ctx context.Context) ([]catalog.Model, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ms, catalog.SaveLive(p.ID, base, ms)
|
||||
return catalog.Chat(ms), catalog.SaveLive(p.ID, base, ms)
|
||||
}
|
||||
|
||||
// fetchOne asks the first endpoint that answers, with p's key.
|
||||
@@ -217,6 +217,7 @@ func (p Provider) fixV1(base, at string) string {
|
||||
// key that can't be asked now keeps the models it saw last time.
|
||||
func (p Provider) fetchPerKey(ctx context.Context, keys []KeyAccount) ([]catalog.Model, error) {
|
||||
old, _, _ := catalog.Live(p.ID)
|
||||
old = append(old, catalog.LiveDrawers(p.ID)...)
|
||||
var out []catalog.Model
|
||||
at := map[string]int{}
|
||||
add := func(m catalog.Model, id string) {
|
||||
@@ -258,7 +259,7 @@ func (p Provider) fetchPerKey(ctx context.Context, keys []KeyAccount) ([]catalog
|
||||
if len(out) == 0 {
|
||||
return nil, lastErr
|
||||
}
|
||||
return out, catalog.SaveLive(p.ID, base, out)
|
||||
return catalog.Chat(out), catalog.SaveLive(p.ID, base, out)
|
||||
}
|
||||
|
||||
// An explicit text-only answer wins. Without one, an unknown answer stays
|
||||
|
||||
Reference in New Issue
Block a user