From 626933ff2bb1b06714bedbf23b3bb7feb68484a4 Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 25 Sep 2026 06:59:45 +0800 Subject: [PATCH] =?UTF-8?q?codex:=20a=20magpie=20model=20that=20takes=20im?= =?UTF-8?q?ages=20says=20so=20in=20the=20catalog=20magpie=20gives=20Codex,?= =?UTF-8?q?=20which=20otherwise=20drops=20an=20attached=20image=20before?= =?UTF-8?q?=20sending=20=E2=80=94=20every=20magpie=20model=20was=20listed?= =?UTF-8?q?=20as=20text-only,=20so=20a=20picture=20pasted=20into=20Codex?= =?UTF-8?q?=20never=20reached=20a=20vision=20model;=20whether=20a=20model?= =?UTF-8?q?=20takes=20images=20is=20what=20its=20vendor's=20models.dev=20e?= =?UTF-8?q?ntry=20says,=20or=20for=20one=20served=20by=20a=20vendor=20mode?= =?UTF-8?q?ls.dev=20doesn't=20list,=20what=20most=20of=20the=20providers?= =?UTF-8?q?=20serving=20that=20model=20say,=20and=20a=20routing=20group=20?= =?UTF-8?q?takes=20them=20only=20when=20every=20member=20does?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/agent/route.go | 2 +- internal/catalog/catalog.go | 46 ++++++++++++++++++++++++++++-- internal/catalog/catalog_test.go | 21 ++++++++++++++ internal/catalog/live.go | 1 + internal/codexcat/codexcat.go | 3 ++ internal/codexcat/codexcat_test.go | 17 +++++++++++ internal/gateway/codex_backend.go | 2 +- internal/provider/group.go | 6 ++-- internal/provider/group_test.go | 21 ++++++++++++++ internal/provider/models.go | 10 ++++--- 10 files changed, 119 insertions(+), 10 deletions(-) diff --git a/internal/agent/route.go b/internal/agent/route.go index cadc924ff..7d7fbecc4 100644 --- a/internal/agent/route.go +++ b/internal/agent/route.go @@ -74,7 +74,7 @@ func magpieModels() []catalog.Model { if e.Group != "" { by = "routing group" } - out = append(out, catalog.Model{ID: e.ID, Name: e.Name + " · " + by, Provider: firstOf(e.Provider.Catalogs()), Efforts: e.Efforts}) + out = append(out, catalog.Model{ID: e.ID, Name: e.Name + " · " + by, Provider: firstOf(e.Provider.Catalogs()), Efforts: e.Efforts, Images: e.Images}) } return out } diff --git a/internal/catalog/catalog.go b/internal/catalog/catalog.go index 732c168d5..dbd744e1d 100644 --- a/internal/catalog/catalog.go +++ b/internal/catalog/catalog.go @@ -11,6 +11,7 @@ import ( "net/http" "os" "path/filepath" + "slices" "sort" "strings" "sync" @@ -32,6 +33,8 @@ type Model struct { // APIs, when the vendor says, are the APIs the model is served on // ("chat", "responses", "anthropic"); empty is not known. APIs []string `json:",omitempty"` + // Images is set on a model that takes images as input. + Images bool `json:",omitempty"` } // Price is what a model costs, in USD per million tokens. @@ -66,6 +69,7 @@ type mdModel struct { Values []string `json:"values"` } `json:"reasoning_options"` Modalities struct { + Input []string `json:"input"` Output []string `json:"output"` } `json:"modalities"` Cost *Price `json:"cost"` @@ -76,6 +80,9 @@ const modelsDevURL = "https://models.dev/api.json" var ( once sync.Once mdev map[string]mdProvider + // images are the models, by bare id, most of the providers serving + // them say take images (a few mislabel a text model) + images map[string]bool syncMu sync.Mutex ) @@ -114,6 +121,22 @@ func load() map[string]mdProvider { var m map[string]mdProvider if json.Unmarshal(b, &m) == nil && len(m) > 0 { mdev = m + votes := map[string]int{} + for _, p := range m { + for id, x := range p.Models { + if slices.Contains(x.Modalities.Input, "image") { + votes[bareID(id)]++ + } else { + votes[bareID(id)]-- + } + } + } + images = map[string]bool{} + for id, v := range votes { + if v > 0 { + images[id] = true + } + } return } } @@ -124,7 +147,7 @@ func load() map[string]mdProvider { // Reset forgets the loaded catalog so the next call re-reads the cache. func Reset() { once = sync.Once{} - mdev = nil + mdev, images = nil, nil } // Sync downloads the models.dev catalog into CachePath. It serializes with @@ -216,7 +239,8 @@ func Provider(id string) []Model { if !textModel(m) { continue } - mm := Model{ID: m.ID, Name: m.Name, Provider: id, Released: m.ReleaseDate, Price: m.Cost, Temperature: m.Temperature} + mm := Model{ID: m.ID, Name: m.Name, Provider: id, Released: m.ReleaseDate, Price: m.Cost, Temperature: m.Temperature, + Images: slices.Contains(m.Modalities.Input, "image")} for _, r := range m.Reasoning { if r.Type == "effort" { mm.Efforts = r.Values @@ -239,6 +263,24 @@ func ProviderName(id string) string { return load()[id].Name } +// SeesImages reports whether models.dev says a model of this id takes +// images, as most of the providers it lists serving it do; for a vendor it +// doesn't list, serving a model it knows from others ("z-ai/glm-5.3" is +// glm-5.3). +func SeesImages(id string) bool { + load() + return images[bareID(id)] +} + +// bareID is a model's id without the vendor's prefix, lowercase. +func bareID(id string) string { + id = strings.ToLower(id) + if i := strings.LastIndexByte(id, '/'); i >= 0 { + id = id[i+1:] + } + return id +} + func textModel(m mdModel) bool { if len(m.Modalities.Output) > 0 { text := false diff --git a/internal/catalog/catalog_test.go b/internal/catalog/catalog_test.go index 48264eeb4..e953425ab 100644 --- a/internal/catalog/catalog_test.go +++ b/internal/catalog/catalog_test.go @@ -68,3 +68,24 @@ func TestDecorateCarriesTemperature(t *testing.T) { t.Fatalf("decorated: %+v", out[1]) } } + +// A model takes images where models.dev says so; one listed by several +// providers takes them as most of those say, whatever a vendor prefixes. +func TestImages(t *testing.T) { + writeCatalog(t, `{ + "a": {"models": { + "vision": {"id":"vision","name":"V","modalities":{"input":["text","image"],"output":["text"]}}, + "text": {"id":"text","name":"T","modalities":{"input":["text"],"output":["text"]}} + }}, + "b": {"models": {"text": {"id":"text","name":"T","modalities":{"input":["text"],"output":["text"]}}}}, + "c": {"models": {"org/text": {"id":"org/text","name":"T","modalities":{"input":["text","image"],"output":["text"]}}}} + }`) + for _, m := range Provider("a") { + if m.Images != (m.ID == "vision") { + t.Errorf("%s: images %v", m.ID, m.Images) + } + } + if !SeesImages("z-ai/Vision") || SeesImages("text") || SeesImages("unknown") { + t.Error("SeesImages") + } +} diff --git a/internal/catalog/live.go b/internal/catalog/live.go index c7e75a536..34d41b87b 100644 --- a/internal/catalog/live.go +++ b/internal/catalog/live.go @@ -192,6 +192,7 @@ func Decorate(live []Model, known []Model) []Model { m.Name = k.Name } m.Efforts, m.Released, m.Provider = k.Efforts, k.Released, k.Provider + m.Images = m.Images || k.Images if k.Temperature != nil { m.Temperature = k.Temperature } diff --git a/internal/codexcat/codexcat.go b/internal/codexcat/codexcat.go index e324600ee..7c3faffc9 100644 --- a/internal/codexcat/codexcat.go +++ b/internal/codexcat/codexcat.go @@ -86,6 +86,9 @@ func Entries(ms []catalog.Model, after int) []any { Shell: "unified_exec", Visibility: "list", InAPI: true, Priority: after + i + 1, ApplyPatch: "freeform", Tools: []string{}, Modalities: []string{"text"}, } + if m.Images { + e.Modalities = append(e.Modalities, "image") + } e.Truncation.Mode, e.Truncation.Limit = "tokens", 10000 for _, ef := range m.Efforts { e.Efforts = append(e.Efforts, level{Effort: ef}) diff --git a/internal/codexcat/codexcat_test.go b/internal/codexcat/codexcat_test.go index 34d4cb079..d2e1f284d 100644 --- a/internal/codexcat/codexcat_test.go +++ b/internal/codexcat/codexcat_test.go @@ -47,3 +47,20 @@ func TestCodexCatalogKeepsOwnEntries(t *testing.T) { t.Error("start-up notice kept") } } + +// A model that takes images says so, and Codex lets images be attached. +func TestCodexCatalogImages(t *testing.T) { + t.Setenv("HOME", t.TempDir()) + var got struct { + Models []struct { + Modalities []string `json:"input_modalities"` + } `json:"models"` + } + json.Unmarshal(Catalog([]catalog.Model{ + {ID: "a/text", Name: "text"}, + {ID: "a/vision", Name: "vision", Images: true}, + }), &got) + if len(got.Models) != 2 || len(got.Models[0].Modalities) != 1 || len(got.Models[1].Modalities) != 2 || got.Models[1].Modalities[1] != "image" { + t.Errorf("%+v", got.Models) + } +} diff --git a/internal/gateway/codex_backend.go b/internal/gateway/codex_backend.go index c895c939a..b0217389c 100644 --- a/internal/gateway/codex_backend.go +++ b/internal/gateway/codex_backend.go @@ -265,7 +265,7 @@ func (s *Server) codexModels(w http.ResponseWriter, r *http.Request) { if e.Group != "" { by = "routing group" } - ms = append(ms, catalog.Model{ID: e.ID, Name: e.Name + " · " + by, Efforts: e.Efforts}) + ms = append(ms, catalog.Model{ID: e.ID, Name: e.Name + " · " + by, Efforts: e.Efforts, Images: e.Images}) } if etag != "" { w.Header().Set("ETag", etag) diff --git a/internal/provider/group.go b/internal/provider/group.go index 09b7bd835..c73e3bb22 100644 --- a/internal/provider/group.go +++ b/internal/provider/group.go @@ -187,17 +187,19 @@ func groupEntries(entries []Entry) []Entry { if len(ms) == 0 { continue } - e := Entry{ID: GroupPrefix + g.ID, Model: ms[0].Model, Name: g.Name, Provider: ms[0].Provider, Group: g.ID} + e := Entry{ID: GroupPrefix + g.ID, Model: ms[0].Model, Name: g.Name, Provider: ms[0].Provider, Group: g.ID, Images: true} for i, m := range ms { if !slices.ContainsFunc(ms[:i], func(o Member) bool { return o.Provider.ID == m.Provider.ID }) { e.Icons = append(e.Icons, m.Provider.Icon) // each provider once, "" for one without } var efforts []string + images := false for _, x := range entries { if x.Provider.ID == m.Provider.ID && x.Model == m.Model { - efforts = x.Efforts + efforts, images = x.Efforts, x.Images } } + e.Images = e.Images && images if i == 0 { e.Efforts = efforts continue diff --git a/internal/provider/group_test.go b/internal/provider/group_test.go index 01cd6a017..e698a5067 100644 --- a/internal/provider/group_test.go +++ b/internal/provider/group_test.go @@ -31,3 +31,24 @@ func TestAutoGroupsSameModel(t *testing.T) { } } + +// A group takes images only when every member does: any of them may be +// the one that answers. +func TestGroupImages(t *testing.T) { + t.Setenv("HOME", t.TempDir()) + t.Setenv("XDG_CONFIG_HOME", "") + e := func(p, m string, images bool) Entry { + return Entry{ID: p + "/" + m, Model: m, Name: m, Provider: Provider{ID: p}, Images: images} + } + gs := groupEntries([]Entry{ + e("a", "kimi-k3", true), e("b", "kimi-k3", true), + e("a", "glm-5.3", true), e("b", "glm-5.3", false), + }) + got := map[string]bool{} + for _, g := range gs { + got[g.ID] = g.Images + } + if len(got) != 2 || !got[GroupPrefix+"auto-kimi-k3"] || got[GroupPrefix+"auto-glm-5-3"] { + t.Errorf("%v", got) + } +} diff --git a/internal/provider/models.go b/internal/provider/models.go index f19d72bbb..95840b73e 100644 --- a/internal/provider/models.go +++ b/internal/provider/models.go @@ -236,9 +236,10 @@ type Entry struct { Model string `json:"model"` // what magpie sends the vendor Name string `json:"name"` Efforts []string `json:"efforts,omitempty"` - Provider Provider `json:"-"` // a group's: its first member's - Group string `json:"group,omitempty"` // set on a routing group (group.go) - Icons []string `json:"-"` // a group's: its providers' icons, one per provider + Provider Provider `json:"-"` // a group's: its first member's + Group string `json:"group,omitempty"` // set on a routing group (group.go) + Icons []string `json:"-"` // a group's: its providers' icons, one per provider + Images bool `json:"images,omitempty"` // takes images as input (a group's: every member does) } // Catalog lists every exposed model of every ready provider, then the @@ -256,7 +257,8 @@ func providerEntries() []Entry { continue } for _, m := range p.Exposed() { - out = append(out, Entry{ID: p.ID + "/" + m.ID, Model: m.ID, Name: m.Name, Efforts: m.Efforts, Provider: p}) + out = append(out, Entry{ID: p.ID + "/" + m.ID, Model: m.ID, Name: m.Name, Efforts: m.Efforts, Provider: p, + Images: m.Images || catalog.SeesImages(m.ID)}) } } return out