Files
magpie/internal/catalog/catalog.go
T
yetone b6467c9551 codex: Ultra on GPT-6.1 Sol wherever it is served, Copilot's too, with Codex's agents taking parts of the task (#656) — Codex offers Ultra on a model whose entry lists "ultra", and on a thread at multi-agent V2 hands parts of the task to agents of its own; its catalog (models-manager/models.json) lists it for gpt-6.1-sol, gpt-6-sol, gpt-6-astra, gpt-5.6-sol and gpt-5.6-terra, not the Lunas, which stop at max. magpie listed Copilot's gpt-6.1-sol only up to max, so Codex had no Ultra for it. Now an entry for one of those models (by the id the vendor serves: openai/…, dated snapshots) that reaches max lists ultra too, in Codex's catalog and /v1/models, and its Codex entry says multi_agent_version v2, as Codex's own does; GPT-6 Luna still stops at max. A ChatGPT account's entry stays as the backend lists it, and a group with a ChatGPT account in it offers ultra but isn't told V2 (its lead's subagent tasks come sealed, #141); the "subagents on any model" setting still stamps v1 on OpenAI's entries. Ultra is no API's level: the gateway now always sends max for it (fitEffort), even to a model whose list says ultra. Subagent tasks with web_search kept (the translated path) were fixed in v0.1.689 (#613); TestCopilotUltraSubagentWithWebSearch now covers it against a stub Copilot: the lead's spawn_agent comes back with plain arguments and encrypted_function_args [], no "encrypted" goes upstream, and the subagent's agent_message reaches Copilot as text. TestCopilotSolOffersUltra (sol has ultra in /v1/models and Codex's catalog with v2, luna not; ultra goes to Copilot as max, with and without web_search) and TestUltraIsSentAsMax fail on the old code; TestUltraOnModelsThatOfferIt and TestUltraEntriesSayV2 cover entries, groups and the catalog. go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass. Not tried in the Codex App with a real Copilot account.
2026-10-03 16:08:26 +08:00

841 lines
26 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Package catalog knows which models exist. It reads the models.dev catalog
// (from magpie's own cache or OpenCode's), Codex's model cache, and falls back
// to a small built-in list so the picker is never empty.
package catalog
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"slices"
"sort"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/filememo"
)
// Model is one entry the picker can offer.
type Model struct {
ID string // e.g. "claude-sonnet-5"
Name string // display name
Provider string // models.dev provider id
Released string // YYYY-MM-DD, used for ordering
Efforts []string
Temperature *bool // false when the model refuses temperature/top_p
Price *Price // USD per million tokens, when models.dev lists it
// Keys, for a vendor whose keys each see models of their own, are the
// keys (by fingerprint) whose list has this one; empty is every key.
Keys []string `json:",omitempty"`
// 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"`
// ImageInput is the source's explicit answer; nil means it did not say.
ImageInput *bool `json:",omitempty"`
// Context is how many tokens a prompt may hold, when known: models.dev's
// input limit, else its context window.
Context int `json:",omitempty"`
// MaxContext is the most a prompt may hold when asked for, above
// Context: Codex's max_context_window (872k on GPT-6, 272k by default).
MaxContext int `json:",omitempty"`
// Output is the most tokens a reply may hold, when known.
Output int `json:",omitempty"`
// Fast is set on a model Codex may ask for priority processing (its
// Fast mode): one a ChatGPT account serves.
Fast bool `json:",omitempty"`
// AgentsV2 is set on a model Codex is told multi-agent V2 for, so its
// Ultra hands work to Codex's agents: one offering Ultra that no
// ChatGPT account answers for (provider.Entry's).
AgentsV2 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"`
// Films is set on a model another magpie lists as one it makes videos
// with (a Remote magpie's Grok Imagine Video): kept with the list for
// its videos API, never offered as a model to talk to or draw with.
Films bool `json:",omitempty"`
// Free is set on a model a subscription serves at no cost to its
// allowance: WorkBuddy's "credits": "x0.00".
Free bool `json:",omitempty"`
// Rate is what a request costs of a subscription's credits, as a
// multiple, when its vendor lists it: Qoder's price_factor (0.5),
// WorkBuddy's "credits": "x0.03". 0 is not listed, or Free.
Rate float64 `json:",omitempty"`
// RateWas is the rate before a discount running now, when the vendor
// says it: Qoder's Qwen3.8-Flash at 0× with 0.1× struck through.
RateWas float64 `json:",omitempty"`
// Reasoning is set on a model that thinks, whether or not it takes
// levels: mimo-v2.6-flash thinks with a switch alone (#402).
Reasoning bool `json:",omitempty"`
}
func imageInput(modalities []string) *bool {
if modalities == nil {
return nil
}
yes := slices.Contains(modalities, "image")
return &yes
}
// Price is what a model costs, in USD per million tokens.
type Price struct {
Input float64 `json:"input"`
Output float64 `json:"output"`
CacheRead float64 `json:"cache_read"`
CacheWrite float64 `json:"cache_write"`
}
// Cost of a call at this price. Reasoning tokens are billed as output by
// every vendor, and are already inside the output count.
func (p Price) Cost(input, output, cacheRead, cacheWrite int) float64 {
return (float64(input)*p.Input + float64(output)*p.Output +
float64(cacheRead)*p.CacheRead + float64(cacheWrite)*p.CacheWrite) / 1e6
}
type mdProvider struct {
ID string `json:"id"`
Name string `json:"name"`
Env []string `json:"env"`
Models map[string]mdModel `json:"models"`
}
type mdModel struct {
ID string `json:"id"`
Name string `json:"name"`
ReleaseDate string `json:"release_date"`
Temperature *bool `json:"temperature"` // false: rejects temperature/top_p
Thinks bool `json:"reasoning"`
Reasoning []struct {
Type string `json:"type"`
Values []string `json:"values"`
} `json:"reasoning_options"`
Modalities struct {
Input []string `json:"input"`
Output []string `json:"output"`
} `json:"modalities"`
Cost *Price `json:"cost"`
// Provider, on a model its vendor serves another way than the rest,
// names the SDK that talks to it ("@ai-sdk/openai": the Responses API)
Provider *struct {
NPM string `json:"npm"`
} `json:"provider"`
Limit struct {
Context int `json:"context"`
Input int `json:"input"`
Output int `json:"output"`
} `json:"limit"`
}
// efforts are the reasoning levels models.dev says the model takes.
func (m mdModel) efforts() []string {
var out []string
for _, r := range m.Reasoning {
if r.Type == "effort" {
out = r.Values
}
}
return out
}
// window is the tokens a prompt to m may hold: the input limit where
// models.dev gives one (gpt-5's 272K of its 400K), else the whole context.
func (m mdModel) window() int {
if m.Limit.Input > 0 {
return m.Limit.Input
}
return m.Limit.Context
}
var modelsDevURL = "https://models.dev/api.json" // a var for tests
var (
// loadMu guards loading and Reset: a background Sync (fresh.go) resets
// while a call is pricing its model
loadMu sync.Mutex
loaded bool
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
// windows are the models' context windows, by bare id, as most of the
// providers serving them give it
windows map[string]int
// outputs are the most tokens their replies may hold, likewise
outputs map[string]int
// efforts are the models' reasoning levels, by bare id, as most of the
// providers that give any for them give them
efforts map[string][]string
// thinks are the models, by bare id, most of the providers serving
// them say reason, levels or not
thinks map[string]bool
syncMu sync.Mutex
)
// CachePath is where `magpie sync` stores the models.dev catalog.
func CachePath() string { return filepath.Join(appdir.Cache(), "models.json") }
func opencodeCache() string {
home, _ := os.UserHomeDir()
return filepath.Join(home, ".cache", "opencode", "models.json")
}
// Source reports which catalog file is in use ("" when only built-ins are).
func Source() string {
for _, p := range []string{CachePath(), opencodeCache()} {
if _, err := os.Stat(p); err == nil {
return p
}
}
return ""
}
func load() map[string]mdProvider {
loadMu.Lock()
defer loadMu.Unlock()
if !loaded {
loaded = true
func() {
for _, p := range []string{CachePath(), opencodeCache()} {
b, err := os.ReadFile(p)
if err != nil {
continue
}
var m map[string]mdProvider
if json.Unmarshal(b, &m) == nil && len(m) > 0 {
mdev = m
votes, reasons := map[string]int{}, map[string]int{}
sizes, outs := map[string]map[int]int{}, map[string]map[int]int{}
levels := map[string]map[string]int{}
// one vote a provider for each list it gives a model: a
// gateway listing it under each host it routes to
// (llmgateway's deepinfra/…, xiaomi/…) votes once, not once
// a host
voted := map[string]bool{}
for pid, p := range m {
for id, x := range p.Models {
if e := x.efforts(); len(e) > 0 {
l := strings.Join(e, ",")
if levels[bareID(id)] == nil {
levels[bareID(id)] = map[string]int{}
}
if k := pid + "\x00" + bareID(id) + "\x00" + l; !voted[k] {
voted[k] = true
levels[bareID(id)][l]++
}
}
if x.Thinks || len(x.efforts()) > 0 {
reasons[bareID(id)]++
} else {
reasons[bareID(id)]--
}
if slices.Contains(x.Modalities.Input, "image") {
votes[bareID(id)]++
} else {
votes[bareID(id)]--
}
if w := x.window(); w > 0 {
if sizes[bareID(id)] == nil {
sizes[bareID(id)] = map[int]int{}
}
sizes[bareID(id)][w]++
}
if o := x.Limit.Output; o > 0 {
if outs[bareID(id)] == nil {
outs[bareID(id)] = map[int]int{}
}
outs[bareID(id)][o]++
}
}
}
windows = map[string]int{}
for id, by := range sizes {
windows[id] = mostGiven(by)
}
outputs = map[string]int{}
for id, by := range outs {
outputs[id] = mostGiven(by)
}
efforts = map[string][]string{}
for id, by := range levels {
efforts[id] = strings.Split(mostListed(by), ",")
}
thinks = map[string]bool{}
for id, v := range reasons {
if v > 0 {
thinks[id] = true
}
}
images = map[string]bool{}
for id, v := range votes {
if v > 0 {
images[id] = true
}
}
return
}
}
}()
}
return mdev
}
// Reset forgets the loaded catalog so the next call re-reads the cache.
func Reset() {
loadMu.Lock()
defer loadMu.Unlock()
loaded = false
mdev, images, windows, outputs, efforts, thinks = nil, nil, nil, nil, nil, nil
}
// Sync downloads the models.dev catalog into CachePath. It serializes with
// itself so a background refresh and a manual one cannot interleave writes.
func Sync(ctx context.Context) error {
syncMu.Lock()
defer syncMu.Unlock()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, modelsDevURL, nil)
if err != nil {
return err
}
req.Header.Set("User-Agent", "magpie")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("models.dev: %s", resp.Status)
}
b, err := io.ReadAll(io.LimitReader(resp.Body, 64<<20))
if err != nil {
return err
}
var probe map[string]mdProvider
if err := json.Unmarshal(b, &probe); err != nil || len(probe) == 0 {
return fmt.Errorf("models.dev: unexpected payload")
}
p := CachePath()
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
return err
}
if err := os.WriteFile(p, b, 0o644); err != nil {
return err
}
Reset()
Touched() // names, reasoning levels and context windows may be others
return nil
}
// Stale reports whether no catalog exists or the cache is older than a day
// (fresh.go).
func Stale() bool {
src := Source()
if src == "" {
return true
}
st, err := os.Stat(src)
return err != nil || time.Since(st.ModTime()) > staleAfter
}
// ProviderEnv lists the env vars that unlock a models.dev provider.
func ProviderEnv(provider string) []string {
if p, ok := load()[provider]; ok {
return p.Env
}
return nil
}
// ProviderAvailable is true when any of the provider's API-key env vars is set.
func ProviderAvailable(provider string) bool {
for _, e := range ProviderEnv(provider) {
if os.Getenv(e) != "" {
return true
}
}
return false
}
// PriceOf is the list price of a models.dev provider's model, if known.
func PriceOf(providerID, modelID string) (Price, bool) {
if p, ok := load()[providerID]; ok {
if m, ok := p.Models[modelID]; ok && m.Cost != nil {
return *m.Cost, true
}
}
return Price{}, false
}
// PricedBy is the list price the first of providers (models.dev ids)
// pricing a model of this id gives it: by its own key first, then by its
// id without a path or case ("openai/GPT-6-Sol" is gpt-6-sol), a Bedrock
// profile's geography, a "(variant)" or a ":tag".
func PricedBy(providers []string, id string) (Price, bool) {
all := load()
for _, pid := range providers {
if m, ok := all[pid].Models[id]; ok && m.Cost != nil {
return *m.Cost, true
}
}
b := bareID(id)
if r, ok := unprofiled(b); ok {
b = r
}
for _, want := range []string{b, cutAt(b, '('), cutAt(b, ':')} {
for _, pid := range providers {
keys := make([]string, 0, len(all[pid].Models))
for key, m := range all[pid].Models {
if m.Cost != nil && bareID(key) == want {
keys = append(keys, key)
}
}
if len(keys) > 0 {
slices.Sort(keys)
return *all[pid].Models[keys[0]].Cost, true
}
}
}
// and last with a dot and a dash taken for the same: Copilot and the
// relays spell Anthropic's models claude-opus-4.6, Anthropic's own entry
// claude-opus-4-6. Only where nothing above priced it, so a vendor that
// lists the id as given, at a price of its own, is still the one asked.
want := dashed(b)
for _, pid := range providers {
keys := []string{}
for key, m := range all[pid].Models {
if m.Cost != nil && dashed(bareID(key)) == want {
keys = append(keys, key)
}
}
if len(keys) > 0 {
slices.Sort(keys)
return *all[pid].Models[keys[0]].Cost, true
}
}
return Price{}, false
}
// dashed is an id with its dots as dashes: the spelling two ids are compared
// in when one vendor writes a model's version 4.6 and another 4-6.
func dashed(id string) string { return strings.ReplaceAll(id, ".", "-") }
// APIOf is the API a models.dev provider's model is served on, when the
// catalog says it's one of its own: "responses" or "anthropic" for a model
// OpenCode serves through OpenAI's or Anthropic's SDK, not the
// OpenAI-compatible one the rest of its models go through.
func APIOf(providerID, modelID string) string {
if p, ok := load()[providerID]; ok {
if m, ok := p.Models[modelID]; ok && m.Provider != nil {
switch m.Provider.NPM {
case "@ai-sdk/openai":
return "responses"
case "@ai-sdk/anthropic":
return "anthropic"
}
}
}
return ""
}
// Provider returns the text models of one models.dev provider, newest first.
// The list comes from the synced models.dev catalog; nothing is compiled in.
// A vendor's own /models answer, once fetched, is layered over it by the
// provider package (see provider.Provider.Available).
func Provider(id string) []Model {
p, ok := load()[id]
if !ok {
return nil
}
var out []Model
for _, m := range p.Models {
if !textModel(m) {
continue
}
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"), ImageInput: imageInput(m.Modalities.Input), Context: m.window(), Output: m.Limit.Output}
mm.Efforts = m.efforts()
mm.Reasoning = m.Thinks || len(mm.Efforts) > 0
out = append(out, mm)
}
sort.SliceStable(out, func(i, j int) bool {
if out[i].Released != out[j].Released {
return out[i].Released > out[j].Released
}
return out[i].ID < out[j].ID
})
return out
}
// ProviderName is a models.dev provider's display name ("GitHub Copilot"
// for "github-copilot"), or "" when the catalog doesn't know it.
func ProviderName(id string) string {
return load()[id].Name
}
// Thinks reports whether models.dev says a model of this id reasons, as
// most of the providers it lists serving it do, whether or not they give
// it levels; for a vendor it doesn't list, serving a model it knows from
// others.
func Thinks(id string) bool {
load()
return thinks[bareID(id)]
}
// Knows reports whether models.dev lists a model of this id at all, under
// any provider, as ContextOf and EffortsOf match it.
func Knows(id string) bool {
return ContextOf(id) > 0 || len(EffortsOf(id)) > 0 || Thinks(id)
}
// 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)]
}
// ContextOf is the context window models.dev gives a model of this id, as
// most of the providers it lists serving it do, or 0 when it doesn't know
// the model. Like SeesImages it reaches a vendor models.dev doesn't list: a
// proxy serving "openai/gpt-5.4", or "gpt-5.4(high)" with the reasoning
// level CLIProxyAPI takes in the id, which a static model list would
// otherwise leave at the agent's default.
func ContextOf(id string) int {
load()
b := bareID(id)
if w, ok := windows[b]; ok {
return w
}
if r, ok := unprofiled(b); ok {
return ContextOf(r)
}
if i := strings.IndexByte(b, '('); i > 0 && strings.HasSuffix(b, ")") {
b = b[:i]
if w, ok := windows[b]; ok {
return w
}
}
if i := strings.IndexByte(b, ':'); i > 0 { // ":free", ":batch"
return windows[b[:i]]
}
return 0
}
// OutputOf is the most tokens a reply from a model of this id may hold, as
// most of the providers serving it give it, or 0 when not known.
func OutputOf(id string) int {
load()
b := bareID(id)
if o, ok := outputs[b]; ok {
return o
}
if r, ok := unprofiled(b); ok {
return OutputOf(r)
}
if i := strings.IndexAny(b, "(:"); i > 0 {
return outputs[b[:i]]
}
return 0
}
// EffortsOf is the reasoning levels models.dev gives a model of this id, as
// most of the providers giving any for it do, or nil when none does. Like
// ContextOf it reaches a vendor models.dev doesn't list: a custom provider
// serving "glm-5.3-flash" takes the levels Z.ai and the others serving it
// say it does, where it would otherwise be taken for a model that doesn't
// reason, and an agent offer no levels for it.
func EffortsOf(id string) []string {
load()
b := bareID(id)
if e, ok := efforts[b]; ok {
return slices.Clone(e)
}
if r, ok := unprofiled(b); ok {
return EffortsOf(r)
}
if i := strings.IndexByte(b, '('); i > 0 && strings.HasSuffix(b, ")") {
b = b[:i]
if e, ok := efforts[b]; ok {
return slices.Clone(e)
}
}
if i := strings.IndexByte(b, ':'); i > 0 { // ":free", ":thinking"
return slices.Clone(efforts[b[:i]])
}
return nil
}
// ListedBy is the reasoning levels the first of providers (models.dev
// ids) listing a model of this id gives it — none, for one listed with a
// thinking switch alone or nothing at all — and whether any of them says.
// One listed with a thinking budget and no levels says nothing of them
// (Anthropic's claude-sonnet-4-5, whose budget an effort is sent as). The
// id is matched as EffortsOf matches it: without a vendor's prefix, in any
// case. It is how a model's maker is heard before its resellers: Xiaomi
// lists mimo-v2.6-flash with a switch alone, where gateways reselling it
// give levels up to max, which Xiaomi turns away (#214).
func ListedBy(providers []string, id string) ([]string, bool) {
all := load()
b := bareID(id)
if r, ok := unprofiled(b); ok {
b = r
}
for _, want := range []string{b, cutAt(b, '('), cutAt(b, ':')} {
for _, pid := range providers {
for key, m := range all[pid].Models {
if bareID(key) != want {
continue
}
if e := m.efforts(); len(e) > 0 || !m.budgeted() {
return slices.Clone(e), true
}
}
}
}
return nil, false
}
// budgeted reports whether models.dev says the model takes a thinking
// budget.
func (m mdModel) budgeted() bool {
for _, r := range m.Reasoning {
if r.Type == "budget_tokens" {
return true
}
}
return false
}
// cutAt is s before sep: "gpt-5.4(high)" is gpt-5.4, "glm-5:free" glm-5.
func cutAt(s string, sep byte) string {
if i := strings.IndexByte(s, sep); i > 0 {
return s[:i]
}
return s
}
// mostListed is the list most providers give; a tie goes to the shorter,
// then the first in order, so the answer doesn't change from run to run.
func mostListed(by map[string]int) string {
best, n := "", 0
for l, c := range by {
if c > n || c == n && (strings.Count(l, ",") < strings.Count(best, ",") || strings.Count(l, ",") == strings.Count(best, ",") && l < best) {
best, n = l, c
}
}
return best
}
// mostGiven is the size most providers give; a tie goes to the smaller,
// which a prompt fits either way.
func mostGiven(by map[int]int) int {
best, n := 0, 0
for w, c := range by {
if c > n || c == n && w < best {
best, n = w, c
}
}
return best
}
// 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
}
// bedrockGeos are the geographies a Bedrock inference profile's id starts
// with.
var bedrockGeos = []string{"global.", "us.", "us-gov.", "eu.", "apac.", "jp.", "au.", "ca.", "in."}
// unprofiled is a Bedrock inference profile's id without its geography
// (apac.anthropic.claude-opus-5-5 is anthropic.claude-opus-5-5): models.dev
// lists each model in only some of them, the model id in all.
func unprofiled(b string) (string, bool) {
for _, g := range bedrockGeos {
if r, ok := strings.CutPrefix(b, g); ok && strings.Contains(r, ".") {
return r, true
}
}
return "", false
}
func textModel(m mdModel) bool {
if len(m.Modalities.Output) > 0 {
text := false
for _, o := range m.Modalities.Output {
if o == "text" {
text = true
}
}
if !text {
return false
}
}
id := m.ID
for _, bad := range []string{"embed", "-tts", "image", "audio", "-live", "robotics", "computer-use", "deep-research", "transcribe", "realtime", "moderation", "whisper", "dall-e", "sora"} {
if strings.Contains(id, bad) {
return false
}
}
return true
}
// draws is whether a models.dev row is a model that makes images: one
// whose only output is images (gpt-image-1, imagen, flux), or that answers
// in text and images and is named for them (gemini-2.5-flash-image,
// gpt-5-image) — not a text model that can also put a chart in its answer
// (deep-research, openrouter/auto).
func draws(m mdModel) bool {
if !slices.Contains(m.Modalities.Output, "image") {
return false
}
id := strings.ToLower(m.ID)
if strings.Contains(id, "deep-research") || strings.HasSuffix(id, "/auto") {
return false
}
return !slices.Contains(m.Modalities.Output, "text") || DrawsID(id)
}
// DrawsID is whether a model's id names one that makes images, for a model
// the catalog doesn't know (one typed in, or a vendor's own list's).
func DrawsID(id string) bool {
id = strings.ToLower(id)
for _, w := range []string{"image", "imagen", "imagine", "dall-e", "flux", "seedream", "cogview", "stable-diffusion", "sdxl", "wanx", "kolors", "hidream"} {
if strings.Contains(id, w) {
return true
}
}
return false
}
// ImagesAPI is whether a model draws on an images API (/images/generations)
// rather than answering in chat with pictures: gpt-image, dall-e, imagen,
// flux, seedream… — not gemini-*-image or gpt-5-image, which chat.
func ImagesAPI(id string) bool {
id = strings.ToLower(id)
for _, w := range []string{"gpt-image", "chatgpt-image", "dall-e", "imagen", "imagine", "qwen-image", "wanx", "wan2", "seedream", "cogview", "flux", "stable-diffusion", "sdxl", "kolors", "hidream"} {
if strings.Contains(id, w) {
return true
}
}
return false
}
// Drawers are the models of one models.dev provider that make images,
// newest first.
func Drawers(id string) []Model {
p, ok := load()[id]
if !ok {
return nil
}
var out []Model
for _, m := range p.Models {
if !draws(m) {
continue
}
out = append(out, Model{ID: m.ID, Name: m.Name, Provider: id, Released: m.ReleaseDate, Price: m.Cost,
Images: slices.Contains(m.Modalities.Input, "image"), ImageInput: imageInput(m.Modalities.Input)})
}
sort.SliceStable(out, func(i, j int) bool {
if out[i].Released != out[j].Released {
return out[i].Released > out[j].Released
}
return out[i].ID < out[j].ID
})
return out
}
// Providers returns models.dev provider ids known to the catalog.
func Providers() []string {
var ids []string
for id := range load() {
ids = append(ids, id)
}
sort.Strings(ids)
return ids
}
// Codex returns the models Codex itself lists, straight from the cache the
// Codex CLI writes; there is no compiled-in list to fall back to.
func Codex() []Model {
home, _ := os.UserHomeDir()
out, _ := filememo.Read("codex models", filepath.Join(home, ".codex", "models_cache.json"), parseCodex)
return slices.Clone(out)
}
func parseCodex(b []byte) ([]Model, error) {
var cache struct {
ETag string `json:"etag"`
Models []struct {
Slug string `json:"slug"`
DisplayName string `json:"display_name"`
Description string `json:"description"`
Visibility string `json:"visibility"`
Priority int `json:"priority"`
Input []string `json:"input_modalities"`
Levels []struct {
Effort string `json:"effort"`
} `json:"supported_reasoning_levels"`
Context int `json:"context_window"`
Max int `json:"max_context_window"`
} `json:"models"`
}
if err := json.Unmarshal(b, &cache); err != nil || len(cache.Models) == 0 {
return nil, err
}
sort.SliceStable(cache.Models, func(i, j int) bool { return cache.Models[i].Priority < cache.Models[j].Priority })
var out []Model
for _, m := range cache.Models {
if m.Visibility == "hide" || MagpieAdded(cache.ETag, m.Slug, m.Description) {
continue
}
mm := Model{ID: m.Slug, Name: m.DisplayName, Provider: "openai", ImageInput: imageInput(m.Input)}
if m.Max > m.Context && m.Context > 0 {
mm.MaxContext = m.Max
}
if mm.ImageInput != nil {
mm.Images = *mm.ImageInput
}
for _, l := range m.Levels {
mm.Efforts = append(mm.Efforts, l.Effort)
}
out = append(out, mm)
}
return out, nil
}
// MagpieAdded reports whether an entry of Codex's models_cache.json is one
// of magpie's, not Codex's own: the list Codex keeps is the one it was last
// handed, and handed through the gateway it has magpie's models in it too
// ("group/semantic", "deepseek/deepseek-v4", "codex/gpt-5.5" — magpie's ids,
// which Codex's slugs never look like). Read back as Codex's own, they were
// listed under OpenAI, and kept there after Codex was routed elsewhere.
func MagpieAdded(etag, slug, description string) bool {
return strings.HasSuffix(description, " via magpie") ||
strings.Contains(etag, "+magpie-") && strings.Contains(slug, "/")
}
// Efforts returns the reasoning levels a model supports, if known.
func Efforts(models []Model, id string) []string {
for _, m := range models {
if m.ID == id && len(m.Efforts) > 0 {
return m.Efforts
}
}
return nil
}