diff --git a/docs/subsystems/providers-accounts.md b/docs/subsystems/providers-accounts.md index fca125915..f1d28bc2c 100644 --- a/docs/subsystems/providers-accounts.md +++ b/docs/subsystems/providers-accounts.md @@ -49,6 +49,7 @@ keys and accounts per request is in [Gateway routing and fallback](gateway-routi | Allowance | `Quotas` asks every vendor at once: subscription windows (`SubscriptionUsage`), key-bought plans (`PlanQuotas`) and key balances (`KeyBalances`). The Usage page, `magpie quota --json` and `GET /v1/magpie/quotas` read it, each window with its whole-window estimate (`Holds`/`HoldsWhy`, `usage.WithWindowHolds`, which `quotaReport` reaches through the `ReckonHolds` hook since package provider can't import usage, #2333). A single card can be read again on its own (#840). A remote magpie's cards come from what it has kept (`CachedCards`, `GET /v1/magpie/quotas/cards`) and are never read there for this computer except by a card's refresh (`POST /v1/magpie/quotas/refresh`, throttled per card). What it has kept is the Usage page's cards with the readings made behind the requests since laid over them, a later one over an earlier by `readSeq` (`cards_kept.go`): each account's `LoginUsage` reading (the used-up switch, the Codex warm-up, routing's `Allowances`), and each key's `KeyAllowance` windows on its plan or balance card. A Claude account's windows Claude Code told as it answered (`NoteClaudeLimits`, kept in `claudeUsage` with their own `heardSeq`, which drops the account's `LoginUsage` reading) stand on its card over any reading started before them (`withHeardClaude`, #2206), so a magpie only requests reach still tells them. An account or key read that way and never on the page gets a card from that reading, and after a restart, before either is read again, the cards come from `quotas.json`, dated (`AsOf`). A magpie serving only other magpies has nobody on its page (#1313). Each key of a remote-magpie provider also gets a card of its own, under `/~keys`, one section per key (#1547, `remoteKeyCards` in `remote_key_limits.go`): the gateway key's limit as that magpie holds it, read from `GET /v1/magpie/limit` asked with that key alone, so a key never sees another's numbers. It shows a window per cap (tokens, spend) named for the period, used against the cap, with its reset, plus `GatewayKey` (what is left, calls, a cycle of days, used up); a key held to no limit shows `Unlimited`. It is kept for `remoteCardsFor` like the remote's cards and read again by the card's refresh (`?provider=/~keys&user=`). A read that fails keeps the last numbers, dated (`AsOf`); with none the card says why, never zero or no limit. A remote from before `/v1/magpie/limit` (404) says it is too old to tell (`errRemoteNoKeyLimit`). A remote that holds the key to a budget, models or accounts answers its cards 403 `gateway_key_held` (#1981, `errKeyHeld`): the cards kept from before are dropped and only the key's own card shows. Its owner can let the key be told the cards of what it may use (#2216, `magpie gateway-key quotas on`); the remote then answers those, and 403 as before when that is none. A remote's cards are kept by provider and key (`remoteCardsID`), so a changed key never shows what the old one was told, and a read is kept only over one asked before it (`keepRemoteCards`). Cursor, Grok and a Kiro key keep the page's cards only. With Settings → Allowances → *Allowance reads* at *When I ask* (`QuotaReads == "asked"`, #1518: magpie started at login before the proxy app read by itself, direct), only a read the user asked for goes out: the Usage page or the tray panel's Allowances opened (no more than once in 30s, `quotasOpened`, #1588) or refreshed (`?asked=1`), a card's refresh (`RefreshUsage`), `magpie quota`, `magpie accounts`, `magpie quota wait` and the TUI's quotas (`Asked`). Any other read (`quotaReading` → `holdUnasked`) carries `netproxy.Hold`, so its requests fail before they leave, and every reader gives back what it last read, dated, or "not read yet" (`errNotAsked`): routing, the used-up switch, the menu bar and alerts go by that. An account refused for its quota (`StaleAllowance` → `markStaleRead`) or a key refused (`StaleKeyAllowance` → `refused`) is still read, once (`unheld`). Warm-up and check-in are not reads and are unchanged. | [`cards_kept.go`](../../internal/provider/cards_kept.go), [`quotas.go`](../../internal/provider/quotas.go), [`subscription_usage.go`](../../internal/provider/subscription_usage.go), [`planquota.go`](../../internal/provider/planquota.go), [`balance.go`](../../internal/provider/balance.go), [`quota_refresh.go`](../../internal/provider/quota_refresh.go), [`remote_quotas.go`](../../internal/provider/remote_quotas.go), [`remote_key_limits.go`](../../internal/provider/remote_key_limits.go), [`quota_asked.go`](../../internal/provider/quota_asked.go) | | A remote magpie's prices | A local magpie asks a remote magpie's `/v1/models` with `X-Magpie-Prices` (`PricesHeader`, sent by `listHeaders`). The remote answers each model's `magpie_price`: what its own Usage page counts the model at (`EntryPriceIn`: the price its user set, else its provider's list price at its price rate; a routing group only when every member costs the same). It tells it only to a request from its own computer or one let in with a gateway key (`local` or `sharedWith`); a list that didn't ask gets no `magpie_price` (a remote request without a valid key never reaches the list: `lanGuard` refuses it); every list told to those callers also carries the same price as OpenRouter's `pricing`, for agents ([gateway routing](gateway-routing.md)). The local keeps the price with the fetched list (`catalog.Model.Price`; a value that isn't a price, or has a negative part, is dropped, the model kept) and uses it as that provider's list price (`ListPrice` → `remotePrice`). A price the local user set (`provider/model`, `provider/*`, `*/model`) still comes first, and the local provider's own price rate still applies on top. A remote from before this sends no field and the models stay priced as before; an older local sends no header and is sent nothing new. The model editor says the greyed price is the other magpie's (`remoteList`). | [`remote_magpie.go`](../../internal/provider/remote_magpie.go), [`models.go`](../../internal/provider/models.go), [`gateway.go`](../../internal/gateway/gateway.go) (`models`), [`live.go`](../../internal/catalog/live.go) | | A remote magpie's service tiers | A remote magpie's `/v1/models` gives each model the `service_tiers` its own Codex is offered on it (`codexTiers` → `codexcat.ServiceTiers`, from the same `CodexCatalog` and `codexcat.Entries` as its `/v1/codex/models`: Fast on a ChatGPT account's GPT model, the tiers of Codex's own entry on a relay's, none on a model it offers none). The local keeps the ids (`catalog.Model.Tiers` → `Entry.Tiers`), `codexListed` hands them on for the remote's models, and `codexcat.Entries` offers them to the local Codex with Codex's own names (`namedTiers`). The gateway sends the tier Codex asks for to a remote magpie as it was asked (`Request.OwnTier`, as for a provider added by its address; dropped and not sent again if refused), and the remote serves it as its own Codex's turn (#1234). A remote from before this sends no tiers, and its models are offered none, as before. The list gives every client the tiers, not only a magpie asking with `X-Magpie-Drawers`: Cindy shows its Fast switch on a model whose `service_tiers` hold `priority` (#1914, `TestModelsListTiersForEveryClient`). | [`codexcat.go`](../../internal/codexcat/codexcat.go), [`codex_models.go`](../../internal/provider/codex_models.go), [`codex_catalog.go`](../../internal/gateway/codex_catalog.go), [`gateway.go`](../../internal/gateway/gateway.go), [`live.go`](../../internal/catalog/live.go) | +| A relay's Codex entries | A provider the user added by its address with a Responses URL may list its models in Codex's own shape (`slug`, `supported_reasoning_levels`, `shell_type`, `base_instructions`: `catalog.CodexEntry`), plainly or when asked with Codex's `client_version`, as sub2api does (ctopsc on Discord: GPT-6.1 Terra was handed models.dev's 922000 window where the relay said 272000). `fetchOne` keeps such a row whole (`catalog.Model.Codex`, with its window, max window and inputs read off it), and when the plain list has none it asks the same URL once more with `client_version` (`codexEntries`): only where the list is fetched (add, key change, Refresh), never per Codex poll. The answer only adds entries to models the plain list has; one that fails or can't be read keeps the last list's entries (failed read = unknown), one that answers plainly says there are none. `codexcat.listedEntry` hands Codex the relay's entry as given, `base_instructions` included, with magpie's slug, name, priority and " via magpie" description; the user's window, compaction, levels and image setting still win (levels keep the relay's descriptions). A saved list from before is enriched at its next Refresh. `TestCodexListedEntry`, `TestCodexListedEntryInPlainList`. | [`codex_entries.go`](../../internal/provider/codex_entries.go), [`live.go`](../../internal/catalog/live.go), [`codexcat.go`](../../internal/codexcat/codexcat.go) | | An endpoint's catalog | A key's provider with no catalog of its own (none named, none from a preset) is priced from the models.dev catalog whose `api` is one of its endpoints (`EndpointCatalog`, `catalog.AtEndpoint`; a trailing slash and case don't count, and an Anthropic base, kept as its root, is also matched with its `/v1`). Only when exactly one catalog is at that address, and it isn't a plan's (an id with `-plan`, or a `membershipCatalogs` one): a plan lists its membership's $0, and two catalogs at one address can't be told apart. A subscription's account never gets one. A provider at AMD's own endpoint is priced from `amd`, which lists Qwen3.8-27B at $0, a price, where its maker sells no such model and it had none (#2052); a relay's endpoint matches nothing and its model stays unpriced, never priced at a reseller's guess. `magpie model price` names the catalog, and says how to give a price when none is known. | [`models.go`](../../internal/provider/models.go), [`catalog.go`](../../internal/catalog/catalog.go) | | A membership's $0 catalog | models.dev lists every model of Kimi Code's plans (`kimi-code-plan-global`, `kimi-code-plan-cn`) and of Xiaomi MiMo's Token Plans (`xiaomi-token-plan-cn`, `-sgp`, `-ams`) at $0: what the membership charges per token, not what the model costs. `membershipCatalogs` are never read for a price (`priceCatalogs`, `makerPriceCatalogs`), so a Xiaomi MiMo model is priced at `xiaomi`'s list price at every region, and each region of the preset names its own catalog, pay as you go `xiaomi` alone (#1873). A Kimi Code provider's model is priced at the Kimi API model it is (`kimiCodeAPI`, from Kimi Code's docs: k3 and k3-256k are kimi-k3, kimi-for-coding-highspeed is kimi-k2.7-code-highspeed), and kimi-for-coding (K2.8 Preview, not on the API) is unpriced rather than free (#1370). The alias is Kimi Code's alone (`kimiCodePlans`): a relay's own `k3` keeps its own price. A catalog other than these that lists a model at $0 still prices it at $0: models.dev has truly free models (nvidia, modelscope). The Requests list names the API model it was priced as (`PricedNameFor`). | [`models.go`](../../internal/provider/models.go) | | A plugin's own price | A model no catalog prices (models.dev has no vendor's own model: Devin's SWE-2 and SWE-1.7 Lightning, Cursor's auto, Qoder's efficient) is priced at what its plugin lists it at (`ListPrice` → `pluginListPrice`, `plugin.Model.Cost`, read by `pluginPrice`: an input or output above 0, OpenCode's 0 being none), else at $0 when the plugin says the plan serves it free (`pluginFree`, `plugin.Model.Free`: Devin's Free cost tier, Qoder's and WorkBuddy's x0). Both come after every catalog, so a price a catalog gives is kept over the plugin's. Devin's plugin (0.1.18) lists each paid model at the price Devin's GetCliModelConfigs gives it (input, cached input and output per 1M tokens); a moved Devin user's calls on Devin's own models had no cost on Usage before (太陽と向日葵 on Discord, `TestPluginFreeModelCostsNothing`). A plugin that gives no price and doesn't say free leaves the model unpriced. | [`models.go`](../../internal/provider/models.go), [`plugins.go`](../../internal/provider/plugins.go), [`providers.go`](../../internal/plugin/providers.go) | diff --git a/internal/agent/route.go b/internal/agent/route.go index 30f283348..a198c07db 100644 --- a/internal/agent/route.go +++ b/internal/agent/route.go @@ -242,6 +242,11 @@ func magpieModels(agent string) []catalog.Model { // lists offer them: Fast in the Codex CLI magpie writes // magpie-models.json for (#1530) provider.CodexTiers(&m, e, members) + if agent == "codex" { + // the entry its vendor lists the model under in Codex's own + // shape, handed on as it is (codexcat) + m.Codex = e.Codex + } // the gateway paths it goes on untranslated, as /v1/models says // them: what an agent plugin's connect is told (cleary5779) m.Endpoints = provider.NativeEndpoints(e) diff --git a/internal/catalog/catalog.go b/internal/catalog/catalog.go index 8eeb07a6f..80d4e486b 100644 --- a/internal/catalog/catalog.go +++ b/internal/catalog/catalog.go @@ -129,6 +129,44 @@ type Model struct { // doesn't, or didn't say. A plugin's model it says searches by itself // (m.searches) is "native" too. WebSearch string `json:",omitempty"` + // Codex, on a model a vendor lists the way Codex's own catalog does + // (a sub2api asked with Codex's client_version), is that entry as the + // vendor gave it (CodexEntry): Codex is handed it as it is, its + // instructions and window and all, rather than an entry magpie made up + // from models.dev (ctopsc on Discord). Its context_window, + // max_context_window, input_modalities and reasoning levels are read + // into the fields above too, so the rest of magpie says what it says. + Codex json.RawMessage `json:",omitempty"` +} + +// CodexLevels are the efforts of a Codex catalog entry's +// supported_reasoning_levels; false when raw is no such entry. +func CodexLevels(raw json.RawMessage) ([]string, bool) { + if len(raw) == 0 || !CodexEntry(raw) { + return nil, false + } + var e struct { + Levels any `json:"supported_reasoning_levels"` + } + json.Unmarshal(raw, &e) + return levelsOf(e.Levels), true +} + +// CodexEntry reports whether raw, one row of a model list, is a model as +// Codex's own catalog lists it (/backend-api/codex/models, codex-rs's +// ModelInfo): a slug, its reasoning levels, its shell and its +// instructions. A plain OpenAI list's row has none of them. +func CodexEntry(raw json.RawMessage) bool { + var e struct { + Slug string `json:"slug"` + Levels json.RawMessage `json:"supported_reasoning_levels"` + Shell json.RawMessage `json:"shell_type"` + Instructions json.RawMessage `json:"base_instructions"` + } + if json.Unmarshal(raw, &e) != nil { + return false + } + return e.Slug != "" && len(e.Levels) > 0 && len(e.Shell) > 0 && len(e.Instructions) > 0 } func imageInput(modalities []string) *bool { diff --git a/internal/catalog/live.go b/internal/catalog/live.go index cc8671cb8..778d05824 100644 --- a/internal/catalog/live.go +++ b/internal/catalog/live.go @@ -555,8 +555,16 @@ func fetchPage(ctx context.Context, url, key string, anthropic bool, headers map return nil, false, "", fmt.Errorf("%s: not a model list", url) } rows := v.Data + // each row as it was sent too, for one that is a Codex catalog entry + // (Model.Codex): rows decoded, so these decode alike + var raw struct { + Data []json.RawMessage `json:"data"` + Models []json.RawMessage `json:"models"` + } + json.Unmarshal(b, &raw) + raws := raw.Data if len(rows) == 0 { - rows = v.Models + rows, raws = v.Models, raw.Models } // A list that says it has more is a page of the vendor's catalog, not // the catalog: a model the user picked that lives on a later page is @@ -569,11 +577,18 @@ func fetchPage(ctx context.Context, url, key string, anthropic bool, headers map // Anthropic's and OpenAI's /v1/models both page with has_more and // last_id. var out []Model - for _, r := range rows { + for i, r := range rows { id := r.ID if id == "" { id = r.Name } + var codex json.RawMessage + if i < len(raws) && CodexEntry(raws[i]) { + codex = raws[i] + if id == "" { + id = r.Slug // Codex's catalog names a model by its slug alone + } + } if id == "" { continue } @@ -629,6 +644,9 @@ func fetchPage(ctx context.Context, url, key string, anthropic bool, headers map if len(r.Price) > 0 && json.Unmarshal(r.Price, &pr) == nil && pr != nil && pr.sane() { m.Price = pr } + if codex != nil { + codexFacts(&m, r, codex) + } out = append(out, m) } return out, v.HasMore, v.LastID, nil @@ -703,6 +721,37 @@ type liveModel struct { // the protocol family PipeLLM routes the model by: openai, anthropic // or gemini TypeTarget string `json:"type_target"` + // a Codex catalog entry's (CodexEntry): its id, its window and the + // most it may be raised to, and what it takes. any, as an odd value + // mustn't lose the whole list + Slug string `json:"slug"` + Window any `json:"context_window"` + MaxWindow any `json:"max_context_window"` + Inputs any `json:"input_modalities"` +} + +// codexFacts reads what a Codex catalog entry says of its model into m: +// the window, the most it may be raised to and whether it takes images, +// where the row says nothing else of them. The entry itself is kept +// whole in m.Codex. +func codexFacts(m *Model, r liveModel, raw json.RawMessage) { + m.Codex = raw + if n, ok := r.Window.(float64); ok && n > 0 && m.Context == 0 { + m.Context = int(n) + } + if n, ok := r.MaxWindow.(float64); ok && int(n) > m.Context { + m.MaxContext = int(n) + } + if xs, ok := r.Inputs.([]any); ok && m.InputModalities == nil { + in := []string{} + for _, x := range xs { + if s, ok := x.(string); ok { + in = append(in, s) + } + } + m.InputModalities, m.ImageInput = in, imageInput(in) + m.Images = *m.ImageInput + } } // tiersOf are the ids of a list's service_tiers, as Codex's catalog diff --git a/internal/codexcat/codexcat.go b/internal/codexcat/codexcat.go index 0c291e3a6..f080e9978 100644 --- a/internal/codexcat/codexcat.go +++ b/internal/codexcat/codexcat.go @@ -211,6 +211,10 @@ func entriesAt(ms []catalog.Model, after int, effort string) []any { entries = append(entries, e) continue } + if e, ok := listedEntry(m, after+i+1, work, autoReview, v1, effort); ok { + entries = append(entries, e) + continue + } e := model{ Slug: m.ID, DisplayName: m.Name, Description: m.Name + " via magpie", Instructions: Prompt, Efforts: []level{}, @@ -720,6 +724,90 @@ func ownEntry(raw map[string]any, id, name string, priority int) map[string]any return e } +// listedEntry is the entry m's vendor lists it under in Codex's own shape +// (catalog.Model.Codex, a sub2api's), under magpie's id: as the vendor +// gave it — its instructions, tools, truncation, tiers and the rest, which +// magpie neither guesses nor rewrites — but for what the user set. A +// window, compaction or levels the user gave the model, its images +// switched, and the auto-review model picked in magpie, are said as the +// entries magpie makes say them; with none of them it is the vendor's +// entry, its description ending " via magpie" so it is told from Codex's +// own (catalog.MagpieAdded). False when m has none, or one magpie can't +// read, which is then made as any other model's. +func listedEntry(m catalog.Model, priority int, work settings.Settings, autoReview string, v1 bool, effort string) (map[string]any, bool) { + var raw map[string]any + if len(m.Codex) == 0 || json.Unmarshal(m.Codex, &raw) != nil || raw == nil { + return nil, false + } + e := ownEntry(raw, m.ID, m.Name, priority) + desc, _ := e["description"].(string) + e["description"] = cmp.Or(strings.TrimSpace(desc), m.Name) + " via magpie" + if c := m.Context; c > 0 { + // the window magpie resolves (the user's, else this entry's own), + // and where the user has Codex compact it, theirs alone: not + // magpie's default working window, as the vendor said its own + Window(e, c, 0) + w := c + if m.Compact > 0 { + w = min(c, m.Compact) + } else if work.CompactAt > 0 { + w = work.Working(c) + } + if w < c { + e["context_window"] = w + } + } + // the levels, where the user's differ from the vendor's: those the + // vendor lists kept as it wrote them, with their descriptions + listed, _ := e["supported_reasoning_levels"].([]any) + byEffort := map[string]any{} + var had []string + for _, l := range listed { + o, _ := l.(map[string]any) + if ef, _ := o["effort"].(string); ef != "" { + byEffort[ef] = l + had = append(had, ef) + } + } + if !slices.Equal(had, m.Efforts) { + levels := []any{} + for _, ef := range m.Efforts { + l, ok := byEffort[ef] + if !ok { + l = map[string]any{"effort": ef} + } + levels = append(levels, l) + } + e["supported_reasoning_levels"] = levels + if d, _ := e["default_reasoning_level"].(string); !slices.Contains(m.Efforts, d) { + if len(m.Efforts) == 0 { + e["default_reasoning_level"] = nil + } else { + e["default_reasoning_level"] = ModelDefault(m.Efforts, effort) + } + } + } + // images, where the user's say (or magpie describing them) differs + if in, ok := e["input_modalities"].([]any); ok { + has := slices.Contains(in, any("image")) + if m.Images && !has { + e["input_modalities"] = append(slices.Clone(in), "image") + } else if !m.Images && has { + e["input_modalities"] = slices.DeleteFunc(slices.Clone(in), func(x any) bool { return x == "image" }) + } + } + if v1 && m.Fast { + Stamp(e) + } else if _, ok := e["multi_agent_version"]; !ok && m.AgentsV2 { + e["multi_agent_version"] = "v2" + } + AutoReview(e, autoReview) + if autoReview == settings.CodexAutoReviewSession { + e["auto_review_model_override"] = m.ID + } + return e, true +} + // Window has one of Codex's own entries say a context window of n tokens // (none: as it says), and most at the most it may be raised to (0: as it // says). Only those two fields change. An entry whose max_context_window is diff --git a/internal/gateway/codex_listed_entry_test.go b/internal/gateway/codex_listed_entry_test.go new file mode 100644 index 000000000..906399c13 --- /dev/null +++ b/internal/gateway/codex_listed_entry_test.go @@ -0,0 +1,223 @@ +package gateway + +import ( + "context" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "reflect" + "strings" + "sync/atomic" + "testing" + + "github.com/yetone/magpie/internal/codexcat" + "github.com/yetone/magpie/internal/provider" +) + +// terraEntry is one model as a relay built for Codex (sub2api) lists it +// when asked with Codex's client_version: Codex's own catalog shape +// (codex-rs ModelInfo), with values magpie never writes itself, so an +// entry made up by magpie can't pass for it. +const terraEntry = `{"slug":"gpt-6.1-terra","display_name":"GPT-6.1 Terra","description":"Frontier agentic coding model.",` + + `"default_reasoning_level":"medium","supported_reasoning_levels":[` + + `{"effort":"low","description":"Fast responses with lighter reasoning"},` + + `{"effort":"medium","description":"Balances speed and reasoning depth for everyday tasks"},` + + `{"effort":"high","description":"Greater reasoning depth for complex problems"},` + + `{"effort":"xhigh","description":"Extra high reasoning depth for complex problems"}],` + + `"shell_type":"shell_command","visibility":"list","supported_in_api":true,"priority":3,` + + `"availability_nux":null,"upgrade":null,` + + `"base_instructions":"You are Codex, based on GPT-6.1 Terra. You are running as a coding agent in the Codex CLI on a user's computer.",` + + `"model_messages":{"instructions_template":"You are Codex, a coding agent based on GPT-6.1 Terra.\n\n{{ personality }}","instructions_variables":{"personality_default":"","personality_friendly":"You optimize for team morale.","personality_pragmatic":"You are a deeply pragmatic engineer."}},` + + `"supports_reasoning_summaries":true,"default_reasoning_summary":"none","support_verbosity":true,"default_verbosity":"low",` + + `"apply_patch_tool_type":"freeform","web_search_tool_type":"text_and_image","truncation_policy":{"mode":"tokens","limit":12000},` + + `"supports_parallel_tool_calls":true,"supports_image_detail_original":true,"context_window":272000,"max_context_window":1000000,` + + `"auto_compact_token_limit":null,"effective_context_window_percent":95,"experimental_supported_tools":[],` + + `"input_modalities":["text","image"],"prefer_websockets":true,"supports_search_tool":true,` + + `"service_tiers":[{"id":"priority","name":"Fast","description":"1.5x faster, more usage"}]}` + +// relayList is a fake sub2api: /v1/models lists plainly (id, owner, +// display name), and in Codex's shape when asked with client_version, as +// sub2api's modelsHandler does. manifest is what the second answers. +type relayList struct { + manifest atomic.Value // string; "" answers 404 + plain atomic.Value // string + asked atomic.Int32 // client_version asks +} + +func newRelay(t *testing.T) (*relayList, string) { + r := &relayList{} + r.manifest.Store(`{"models":[` + terraEntry + `]}`) + r.plain.Store(`{"object":"list","data":[` + + `{"id":"gpt-6.1-terra","object":"model","created":1798761600,"owned_by":"openai","display_name":"GPT-6.1 Terra"},` + + `{"id":"glm-5","object":"model","created":1798761600,"owned_by":"zhipu","display_name":"GLM-5"}]}`) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + if req.URL.Path != "/v1/models" { + http.NotFound(w, req) + return + } + w.Header().Set("Content-Type", "application/json") + if req.URL.Query().Get("client_version") != "" { + r.asked.Add(1) + m, _ := r.manifest.Load().(string) + if m == "" { + w.WriteHeader(http.StatusNotFound) + io.WriteString(w, `{"error":{"message":"not found"}}`) + return + } + io.WriteString(w, m) + return + } + io.WriteString(w, r.plain.Load().(string)) + })) + t.Cleanup(srv.Close) + return r, srv.URL + "/v1" +} + +// codexEntry is the entry of id in the catalog a Codex naming magpie its +// model_provider reads (/v1/codex/models). +func codexEntry(t *testing.T, id string) map[string]any { + t.Helper() + rec := httptest.NewRecorder() + New().Handler().ServeHTTP(rec, httptest.NewRequest("GET", CodexCatalogPath+"?client_version=0.164.0", nil)) + if rec.Code != 200 { + t.Fatalf("%d %s", rec.Code, rec.Body) + } + var list struct { + Models []map[string]any `json:"models"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &list); err != nil { + t.Fatal(err) + } + for _, e := range list.Models { + if e["slug"] == id { + return e + } + } + t.Fatalf("no %s in %s", id, rec.Body) + return nil +} + +func fetchRelay(t *testing.T) { + t.Helper() + p, err := provider.Find("relay") + if err != nil { + t.Fatal(err) + } + if _, err := p.Fetch(context.Background()); err != nil { + t.Fatal(err) + } +} + +// A provider the user added by its address, a relay that lists its models +// in Codex's own shape when asked as Codex asks (sub2api, ctopsc on +// Discord), has each model's entry handed to Codex as the relay gave it — +// its instructions, window, levels, tools and tiers — and not one magpie +// made up from models.dev's guess for the id. The user's own window and +// levels still win, a model the shaped list leaves out is made as before, +// and a shaped list that can't be read keeps what the last one said. +func TestCodexListedEntry(t *testing.T) { + fresh(t) + relay, base := newRelay(t) + if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Models: []string{"gpt-6.1-terra", "glm-5"}, Responses: base}); err != nil { + t.Fatal(err) + } + fetchRelay(t) + if n := relay.asked.Load(); n != 1 { + t.Fatalf("asked with client_version %d times on one fetch, want 1", n) + } + var upstream map[string]any + json.Unmarshal([]byte(terraEntry), &upstream) + verbatim := func(t *testing.T, e map[string]any, except ...string) { + t.Helper() + for k, v := range upstream { + switch k { + case "slug", "display_name", "priority", "description", "availability_nux", "upgrade": + continue // magpie's id, name and place; the notices name slugs it doesn't have + } + if strings.Contains(strings.Join(except, ","), k) { + continue + } + if !reflect.DeepEqual(e[k], v) { + t.Errorf("%s = %v, the relay said %v", k, e[k], v) + } + } + } + + t.Run("the relay's entry", func(t *testing.T) { + e := codexEntry(t, "relay/gpt-6.1-terra") + verbatim(t, e) + if d, _ := e["description"].(string); d != "Frontier agentic coding model. via magpie" { + t.Errorf("description %q, want the relay's with via magpie", d) + } + // the one it doesn't list in Codex's shape is made as before + g := codexEntry(t, "relay/glm-5") + if g["base_instructions"] != codexcat.Prompt { + t.Errorf("glm-5's instructions aren't magpie's own: %.80v", g["base_instructions"]) + } + }) + + t.Run("a shaped list that can't be read keeps the last", func(t *testing.T) { + relay.manifest.Store("") + fetchRelay(t) + verbatim(t, codexEntry(t, "relay/gpt-6.1-terra")) + // nor does one with no model in it (an odd shape) + relay.manifest.Store(`{"models":"none"}`) + fetchRelay(t) + verbatim(t, codexEntry(t, "relay/gpt-6.1-terra")) + // one that answers plainly (a relay that ignores client_version) + // says it has none in Codex's shape + relay.manifest.Store(relay.plain.Load()) + fetchRelay(t) + if e := codexEntry(t, "relay/gpt-6.1-terra"); e["base_instructions"] != codexcat.Prompt { + t.Errorf("an entry Codex can't read was kept: %.80v", e["base_instructions"]) + } + relay.manifest.Store(`{"models":[` + terraEntry + `]}`) + fetchRelay(t) + }) + + t.Run("the user's window and levels win", func(t *testing.T) { + p, _ := provider.Find("relay") + p.Contexts = map[string]int{"gpt-6.1-terra": 400000} + if err := provider.Save(*p); err != nil { + t.Fatal(err) + } + if err := provider.SetModelEfforts("relay/gpt-6.1-terra", []string{"low", "high"}); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { provider.SetModelEfforts("relay/gpt-6.1-terra", nil) }) + e := codexEntry(t, "relay/gpt-6.1-terra") + verbatim(t, e, "context_window", "supported_reasoning_levels", "default_reasoning_level") + if w, _ := e["context_window"].(float64); w != 400000 { + t.Errorf("context_window %v, want the user's 400000", e["context_window"]) + } + levels, _ := json.Marshal(e["supported_reasoning_levels"]) + if want := `[{"description":"Fast responses with lighter reasoning","effort":"low"},{"description":"Greater reasoning depth for complex problems","effort":"high"}]`; string(levels) != want { + t.Errorf("levels %s, want %s", levels, want) + } + if d, _ := e["default_reasoning_level"].(string); d != "low" && d != "high" { + t.Errorf("default_reasoning_level %q, not one of the user's levels", d) + } + }) +} + +// A relay whose plain list is already in Codex's shape is asked once. +func TestCodexListedEntryInPlainList(t *testing.T) { + fresh(t) + relay, base := newRelay(t) + relay.plain.Store(`{"models":[` + terraEntry + `]}`) + if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Models: []string{"gpt-6.1-terra"}, Responses: base}); err != nil { + t.Fatal(err) + } + fetchRelay(t) + if n := relay.asked.Load(); n != 0 { + t.Errorf("asked again with client_version %d times, want none", n) + } + e := codexEntry(t, "relay/gpt-6.1-terra") + if e["base_instructions"] != "You are Codex, based on GPT-6.1 Terra. You are running as a coding agent in the Codex CLI on a user's computer." { + t.Errorf("instructions %.80v, want the relay's", e["base_instructions"]) + } + if w, _ := e["context_window"].(float64); w != 272000 { + t.Errorf("context_window %v, want the relay's 272000", e["context_window"]) + } +} diff --git a/internal/provider/codex_entries.go b/internal/provider/codex_entries.go new file mode 100644 index 000000000..b0ecbb3fa --- /dev/null +++ b/internal/provider/codex_entries.go @@ -0,0 +1,78 @@ +package provider + +import ( + "context" + "net/url" + "slices" + + "github.com/yetone/magpie/internal/catalog" +) + +// codexEntries are ms, the models a provider the user added by its address +// lists at its Responses base, with the entry each has in Codex's own shape +// where the vendor gives one (catalog.Model.Codex). A relay built for Codex +// (sub2api) lists its models plainly at /v1/models, ids and owners alone, +// and in Codex's shape — window, levels, instructions, tools — when asked +// with the client_version Codex asks with; magpie read the plain list, so +// Codex was handed windows and levels models.dev guessed for the ids +// (gpt-6.1-terra at 922000) where the relay had said its own (ctopsc on +// Discord). So the list is asked once more as Codex asks it, at the URL it +// answered at, when that list is fetched: a request more where the +// provider is added, its key changed or its list refreshed, never one +// each time Codex asks magpie for its list. The answer only adds to +// the plain list: a model it has that the plain list hasn't is not added, +// as the plain list is the one magpie has always served (a relay's image +// models are in it alone). A list already in Codex's shape is not asked +// again. One that can't be read says nothing new: each model keeps the +// entry the last list gave it, as a base that can't be asked keeps its +// models (#904); one read that is a plain list says the vendor gives none. +func (p Provider) codexEntries(ctx context.Context, list string, ms []catalog.Model) []catalog.Model { + if p.Preset != "" || p.Account != nil || slices.ContainsFunc(ms, func(m catalog.Model) bool { return len(m.Codex) > 0 }) { + return ms + } + u, err := url.Parse(list) + if err != nil { + return ms + } + q := u.Query() + q.Set("client_version", codexVersion()) + u.RawQuery = q.Encode() + got, err := catalog.FetchURL(ctx, u.String(), p.Key, false, p.listHeadersAt(list)) + if err != nil { + got, _, _ = catalog.Live(p.ID) + } + byID := map[string]catalog.Model{} + for _, m := range got { + if len(m.Codex) > 0 { + byID[m.ID] = m + } + } + if len(byID) == 0 { + return ms + } + out := slices.Clone(ms) + for i := range out { + if c, ok := byID[out[i].ID]; ok { + withCodex(&out[i], c) + } + } + return out +} + +// withCodex gives m the Codex entry c has, and what that entry says of the +// model where m's own list said nothing. +func withCodex(m *catalog.Model, c catalog.Model) { + m.Codex = c.Codex + if m.Context == 0 { + m.Context = c.Context + } + if m.MaxContext == 0 { + m.MaxContext = c.MaxContext + } + if len(m.Efforts) == 0 { + m.Efforts = c.Efforts + } + if m.ImageInput == nil && c.ImageInput != nil { + m.ImageInput, m.Images, m.InputModalities = c.ImageInput, c.Images, c.InputModalities + } +} diff --git a/internal/provider/codex_models.go b/internal/provider/codex_models.go index c5bbe4967..c3ce338da 100644 --- a/internal/provider/codex_models.go +++ b/internal/provider/codex_models.go @@ -672,7 +672,7 @@ func codexListed(shown []Entry, members func(id string) []Member, own bool) []ca if CodexOwn(e) && !own { continue } - m := catalog.Model{ID: e.ID, Name: labels[i], Efforts: e.Efforts, Images: e.Images || seen, Context: e.Context, AgentsV2: e.AgentsV2} + m := catalog.Model{ID: e.ID, Name: labels[i], Efforts: e.Efforts, Images: e.Images || seen, Context: e.Context, AgentsV2: e.AgentsV2, Codex: e.Codex} m.Compact = CompactFor(s, e, find) CodexTiers(&m, e, members) ms = append(ms, m) diff --git a/internal/provider/models.go b/internal/provider/models.go index 3dac0af9f..485471e94 100644 --- a/internal/provider/models.go +++ b/internal/provider/models.go @@ -3,6 +3,7 @@ package provider import ( "cmp" "context" + "encoding/json" "errors" "fmt" "log" @@ -702,6 +703,7 @@ func (p Provider) fetchOne(ctx context.Context) (fetched, error) { // the lists already fetched for this key, by the URL each answered at: // two of the provider's bases that are one list are one request (#1006) listed := catalog.NewListed() + codexAt := "" // the list URL the Responses base answered at for _, proto := range p.Speaks() { protoBase := p.Base(proto) if proto == Gemini { @@ -782,6 +784,9 @@ func (p Provider) fetchOne(ctx context.Context) (fetched, error) { dead = append(dead, protoBase) continue } + if proto == Responses { + codexAt = u // asked again as Codex asks it (codexEntries) + } if got[u] { // one list, already asked and merged under another base's // spelling of the same URL: not a second endpoint (#1006) @@ -821,6 +826,9 @@ func (p Provider) fetchOne(ctx context.Context) (fetched, error) { } sides = append(sides, saved...) } + if codexAt != "" { + out = p.codexEntries(ctx, codexAt, out) + } // the image models its list leaves out (AIHubMix's gpt-image-2) // the parts are kept only where several bases were asked: a single one // that fails is the fetch that fails, and its list is kept as it is @@ -2432,6 +2440,9 @@ type Entry struct { // models.dev says it, "" when neither does: what a group's template // weighs a newer model by (grouptemplate.go) Released string `json:"-"` + // Codex is the entry the provider's own list gives the model in + // Codex's shape (catalog.Model's), handed to Codex as it is + Codex json.RawMessage `json:"-"` } // Catalog lists the routing groups, then every exposed model of every ready @@ -2562,7 +2573,12 @@ func entryFor(p Provider, m catalog.Model, s settings.Settings) Entry { name := cmp.Or(m.Name, m.ID) e := Entry{ID: p.ID + "/" + m.ID, Model: m.ID, Family: p.Family, Name: name, Efforts: effortsOf(m), Provider: p, Images: images, ImageInput: imageInput, Context: ctx, WindowSet: p.ContextOf(m.ID) > 0, Output: output, OutputSet: outputOf(s, p.ID, m.ID) > 0, Free: m.Free, Rate: m.Rate, RateWas: m.RateWas, Tiers: m.Tiers, Released: m.Released, - Inputs: m.InputModalities, Outputs: m.OutputModalities} + Inputs: m.InputModalities, Outputs: m.OutputModalities, Codex: m.Codex} + if lv, ok := catalog.CodexLevels(m.Codex); ok { + // the levels the vendor lists the model at in Codex's shape are + // its word, over models.dev's for the same id (Decorate) + e.Efforts = lv + } if n, ok := modelNameIn(s.ModelNames, p.ID, m.ID); ok { e.Name, e.Default = n, name } else { @@ -2581,7 +2597,7 @@ func entryFor(p Provider, m catalog.Model, s settings.Settings) Entry { // already. Its subagents take their tasks as text from a magpie-served // lead, so Codex is told V2 for it, where Ultra hands work to them // (codexcat.Entries). - if p.Account == nil || p.Account.Agent != "codex" { + if (p.Account == nil || p.Account.Agent != "codex") && len(m.Codex) == 0 { e.Efforts = withUltra(m.ID, e.Efforts) e.AgentsV2 = slices.Contains(e.Efforts, "ultra") }