From fcae505ff77523d8b2209741de8b7a31da3f2fac Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 10 Oct 2026 17:48:15 +0800 Subject: [PATCH] gateway, codexcat: Codex is offered Fast on a remote magpie's model where that magpie offers its own Codex Fast, and the tier asked reaches it (#1234, PennyVibe) remote-magpie/codex/gpt-6.1-sol had service_tiers [] in the local /v1/codex/models while the remote's own codex/gpt-6.1-sol had Fast: the local catalog only gave tiers to a provider added by its address (OwnTier, Preset == ""), and the gateway sent service_tier only to one. Now a remote magpie's /v1/models (asked with X-Magpie-Drawers) tells each model's service_tiers as its own Codex catalog offers them (codexcat.ServiceTiers over the same CodexCatalog/Entries as its /v1/codex/models). The local keeps the ids (catalog.Model.Tiers -> Entry.Tiers), codexListed hands them on for the remote's models, and codexcat.Entries offers them with Codex's own names. The gateway sends the tier Codex asked for to a remote magpie as it was asked (OwnTier, still dropped and remembered if refused); the remote serves it as its own Codex's turn. A remote from before this sends no tiers: unchanged. Run: - TestRemoteMagpieOffersItsTiers (gateway, the gateway as both computers over HTTP): fails without the codexListed change with `office/relay/gpt-6.1-sol offered "" ..., want "priority:Fast "`, and without the gateway change with `tier sent to the remote magpie [(none)], by it to its vendor [(none)]; want priority, priority`. - TestServiceTiersGoOnToAnotherMagpie (codexcat): a ChatGPT account's model with Codex's own entry tells priority+ultrafast, a relay's GPT Fast, glm none; fails when either the own-entry branch of ServiceTiers or the namedTiers branch of Entries is taken out. - gofmt -l clean; go vet -tags nogui for darwin, linux, windows on gateway, provider, codexcat, catalog, agent; go test -tags nogui of those five packages under a temp HOME: ok. Not tried with a real ChatGPT account behind a real remote magpie. --- docs/subsystems/providers-accounts.md | 1 + internal/catalog/catalog.go | 4 +- internal/catalog/live.go | 23 ++++ internal/codexcat/codexcat.go | 72 +++++++++++- internal/codexcat/own_tier_test.go | 42 +++++++ internal/gateway/codex_catalog.go | 14 +++ internal/gateway/gateway.go | 15 ++- internal/gateway/remote_magpie_tier_test.go | 123 ++++++++++++++++++++ internal/provider/codex_models.go | 5 + internal/provider/models.go | 5 +- 10 files changed, 299 insertions(+), 5 deletions(-) create mode 100644 internal/gateway/remote_magpie_tier_test.go diff --git a/docs/subsystems/providers-accounts.md b/docs/subsystems/providers-accounts.md index 12d51116f..dff1770eb 100644 --- a/docs/subsystems/providers-accounts.md +++ b/docs/subsystems/providers-accounts.md @@ -30,6 +30,7 @@ keys and accounts per request is in [Gateway routing and fallback](gateway-routi | Routing groups | User-made groups from the Routing view, and auto-found groups of the same model across providers (derived, never stored). Members are `provider/model[:effort]`. A group can nest another (`group/`); `SaveGroup` refuses loops. `Match` takes a glob or `re:` pattern (#766). A found group follows its providers: one switched off or removed leaves it, and one on again is back. A found group the user changed (an `auto-…` id stored) is read without the members of providers magpie no longer has (`withoutRemoved`, not written back, so a provider back under its id is back in it), except a manual group's pick, a rule's model, or all of them when none would be left. A switched-off provider's members stay in it, skipped by `membersIn`, and the Routing page says them as switched off (`memberJSON.providerOff`). The user's own groups keep their members. | [`group.go`](../../internal/provider/group.go) | | 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. 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. 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). Cursor, Grok and a Kiro key keep the page's cards only. | [`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) | | 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 prices (a remote request without a valid key never reaches the list: `lanGuard` refuses it). 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`, asked with `X-Magpie-Drawers` (`DrawersHeader`), 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. | [`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 membership's $0 catalog | models.dev lists every model of Kimi Code's plans (`kimi-code-plan-global`, `kimi-code-plan-cn`) at $0: what the membership charges per token, not what the model costs. `membershipCatalogs` are never read for a price (`priceCatalogs`, `makerPriceCatalogs`), so 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: a relay's own `k3` keeps its own price. The Requests list names the API model it was priced as (`PricedNameFor`). | [`models.go`](../../internal/provider/models.go) | | A key's own windows | A sub2api key given a spending limit per 5 hours, day or 7 days answers its `/v1/usage` with `rate_limits` and no `remaining` (`readSub2APIKeyLimits`). With the Balance URL set to it, the key's card shows them as the windows *5 hours*, *1 day*, *7 days*, in USD, beside a balance field the user wrote; *Check balance* and `magpie provider show` give what its fullest window has left (`Balance`). `KeyAllowance` gives routing the same windows: never waited for, read in the background at most once a minute, a failed read keeping the last, and seeded from the card's last reading (`quotas.json`) after a restart. A key bought as a plan (Kimi Code, GLM Coding Plan and Z.ai, MiniMax Coding Plan, OpenCode Go, Command Code: `planQuotaSourceOf`) gives routing its card's windows the same way (`planKeyWindows`, Zhipu's team quota behind a key with none of its own); the Usage page's read of its card is taken at once (`noteKeyAllowance`), and a key there that turns out pay-as-you-go, with no windows, is asked again only every 30 minutes (`keyNoWindowsAge`). StepFun's Step Plan is read through a sign-in, not the key, and isn't one of them. Nor are Volcengine Ark's Coding Plan and Agent Plan (#1427): their 5-hour, weekly and monthly windows are told only to the account's access key (`AccessKeyID`, `SecretAccessKey`), asked of `open.volcengineapi.com` (`GetCodingPlanUsage`, `GetAFPUsage`) and signed as Volcengine's own SDK signs (`volcSign`), so they are cards on the Usage page (`volcPlanQuotas` in `PlanQuotas`), never routing's windows. | [`sub2api_usage.go`](../../internal/provider/sub2api_usage.go), [`key_allowance.go`](../../internal/provider/key_allowance.go), [`volcengine_usage.go`](../../internal/provider/volcengine_usage.go) | | Last served | `served.json` records when each account, plan or key last answered, shown as `lastServedAt` (#570). | [`served.go`](../../internal/provider/served.go) | diff --git a/internal/catalog/catalog.go b/internal/catalog/catalog.go index 1c355235d..c55bc1a0f 100644 --- a/internal/catalog/catalog.go +++ b/internal/catalog/catalog.go @@ -63,7 +63,9 @@ type Model struct { // Fast mode): one a ChatGPT account serves. Fast bool `json:",omitempty"` // Tiers, in one ChatGPT account's own model list, are the service - // tiers its plan offers on the model ("priority", "ultrafast"). + // tiers its plan offers on the model ("priority", "ultrafast"); in + // another magpie's, the tiers it offers its Codex on the model + // (#1234). Tiers []string `json:",omitempty"` // OwnTier is set on a model of a provider the user added by its // address, which the gateway sends the tier Codex asks for as it is diff --git a/internal/catalog/live.go b/internal/catalog/live.go index f4a08e9e9..ec4a85790 100644 --- a/internal/catalog/live.go +++ b/internal/catalog/live.go @@ -328,6 +328,7 @@ func fetchOne(ctx context.Context, url, key string, anthropic bool, headers map[ if input != nil { m.Images = *input } + m.Tiers = tiersOf(r.Tiers) var pr *Price if len(r.Price) > 0 && json.Unmarshal(r.Price, &pr) == nil && pr != nil && pr.sane() { m.Price = pr @@ -393,6 +394,11 @@ type liveModel struct { // how another magpie searches the web for the model: "native" or // "magpie" (Model.WebSearch) WebSearch string `json:"web_search"` + // the service tiers another magpie offers its Codex on the model + // (Fast's "priority"), as Codex's catalog writes them ({"id":…}) or + // as plain ids: kept as Model.Tiers (#1234). any, as an odd value + // mustn't lose the whole list + Tiers any `json:"service_tiers"` // what another magpie counts a call to the model at, asked for with // its X-Magpie-Prices header: the price its user set, else its list // price. Raw, as an odd value mustn't lose the whole list @@ -402,6 +408,23 @@ type liveModel struct { TypeTarget string `json:"type_target"` } +// tiersOf are the ids of a list's service_tiers, as Codex's catalog +// writes them ([{"id":"priority","name":"Fast"}]) or as plain ids. +func tiersOf(v any) []string { + xs, _ := v.([]any) + var out []string + for _, x := range xs { + id, _ := x.(string) + if o, ok := x.(map[string]any); ok { + id, _ = o["id"].(string) + } + if id != "" && !slices.Contains(out, id) { + out = append(out, id) + } + } + return out +} + // levelsOf are the efforts of a list's supported_reasoning_levels, as // magpie and Codex write them ([{"effort":"high"}]) or as plain names. func levelsOf(v any) []string { diff --git a/internal/codexcat/codexcat.go b/internal/codexcat/codexcat.go index e8da9dee6..7c8ccf63a 100644 --- a/internal/codexcat/codexcat.go +++ b/internal/codexcat/codexcat.go @@ -5,10 +5,12 @@ package codexcat import ( "bytes" + "cmp" "crypto/sha256" _ "embed" "encoding/hex" "encoding/json" + "maps" "os" "path/filepath" "reflect" @@ -181,9 +183,13 @@ func Entries(ms []catalog.Model, after int) []any { // or a group one is in, gets the tier Codex's own catalog gives its // GPT models if slug, ok := strings.CutPrefix(m.ID, "codex/"); m.Fast || ok && strings.HasPrefix(slug, "gpt-") { - e.Tiers = append(e.Tiers, tier{ID: "priority", Name: "Fast", Description: "1.5x speed, increased usage"}) + e.Tiers = append(e.Tiers, fastTier) } else if m.OwnTier { e.Tiers = ownTiers(own, m) + } else if len(m.Tiers) > 0 { + // another magpie's model: the tiers it offers its own Codex + // (#1234) + e.Tiers = namedTiers(own, m.Tiers) } // an OpenAI model: a ChatGPT account's (codex/), or a group one is // in (Fast, see provider.codexListed) @@ -367,6 +373,70 @@ func ownTiers(own map[string]map[string]any, m catalog.Model) []tier { return []tier{} } +// fastTier is Fast as Codex's own catalog writes it on its GPT models. +var fastTier = tier{ID: "priority", Name: "Fast", Description: "1.5x speed, increased usage"} + +// namedTiers are the tiers ids name, each as Codex's own catalog writes it +// on any of its models, else by its id: Fast's "priority" as Codex names it. +func namedTiers(own map[string]map[string]any, ids []string) []tier { + ts := []tier{} + for _, id := range ids { + t := tier{ID: id, Name: id} + if id == fastTier.ID { + t = fastTier + } + found: + for _, slug := range slices.Sorted(maps.Keys(own)) { + raw, _ := own[slug]["service_tiers"].([]any) + for _, r := range raw { + o, _ := r.(map[string]any) + if o["id"] == id { + t.Name, _ = o["name"].(string) + t.Description, _ = o["description"].(string) + t.Name = cmp.Or(t.Name, id) + break found + } + } + } + ts = append(ts, t) + } + return ts +} + +// ServiceTiers are the service tiers Entries offers Codex on each of ms, +// by id, where it offers any: what this magpie's list tells another magpie +// that has it as its provider, so that one's Codex is offered them too +// (#1234). +func ServiceTiers(ms []catalog.Model) map[string][]tier { + out := map[string][]tier{} + for _, e := range Entries(ms, 0) { + switch e := e.(type) { + case *model: + if len(e.Tiers) > 0 { + out[e.Slug] = e.Tiers + } + case map[string]any: + slug, _ := e["slug"].(string) + raw, _ := e["service_tiers"].([]any) + var ts []tier + for _, r := range raw { + o, _ := r.(map[string]any) + id, _ := o["id"].(string) + if id == "" { + continue + } + name, _ := o["name"].(string) + desc, _ := o["description"].(string) + ts = append(ts, tier{ID: id, Name: name, Description: desc}) + } + if slug != "" && len(ts) > 0 { + out[slug] = ts + } + } + } + return out +} + // Order ranks entries — Codex's own, as the backend gives them, and // magpie's from Entries — in the order the user put them in (#855): Codex // lists its models by priority, lowest first. The ones at names go first, diff --git a/internal/codexcat/own_tier_test.go b/internal/codexcat/own_tier_test.go index c6f0f9f74..e2b48764c 100644 --- a/internal/codexcat/own_tier_test.go +++ b/internal/codexcat/own_tier_test.go @@ -47,3 +47,45 @@ func TestOwnProviderTiers(t *testing.T) { } } } + +// What this magpie tells another that has it as its provider: the tiers +// its Codex is offered on each model — a ChatGPT account's own model with +// those of Codex's own entry, Ultrafast among them, a relay's GPT model +// Fast, none on any other — and that one's Codex is offered the same on +// the model, with Codex's own names (#1234). +func TestServiceTiersGoOnToAnotherMagpie(t *testing.T) { + v1Home(t, `{"etag":"W/\"a\"","models":[{"slug":"gpt-6.1-sol","base_instructions":"x","service_tiers":[{"id":"priority","name":"Fast","description":"f"},{"id":"ultrafast","name":"Ultrafast","description":"u"}]}]}`) + here := ServiceTiers([]catalog.Model{ + {ID: "codex/gpt-6.1-sol", Name: "a"}, + {ID: "relay/gpt-5.5", Name: "b", OwnTier: true}, + {ID: "relay/glm-5", Name: "c", OwnTier: true}, + }) + ids := func(ts []tier) string { + s := "" + for _, x := range ts { + s += x.ID + ":" + x.Name + " " + } + return s + } + want := map[string]string{"codex/gpt-6.1-sol": "priority:Fast ultrafast:Ultrafast ", "relay/gpt-5.5": "priority:Fast ", "relay/glm-5": ""} + for k, v := range want { + if got := ids(here[k]); got != v { + t.Errorf("%s tells %q, want %q", k, got, v) + } + } + var there []catalog.Model + for _, id := range []string{"codex/gpt-6.1-sol", "relay/glm-5"} { + var list []string + for _, x := range here[id] { + list = append(list, x.ID) + } + there = append(there, catalog.Model{ID: "office/" + id, Name: id, Tiers: list}) + } + got := ServiceTiers(there) + if s := ids(got["office/codex/gpt-6.1-sol"]); s != "priority:Fast ultrafast:Ultrafast " { + t.Errorf("the other magpie's Codex offered %q", s) + } + if s := ids(got["office/relay/glm-5"]); s != "" { + t.Errorf("a model offered no tier there is offered %q", s) + } +} diff --git a/internal/gateway/codex_catalog.go b/internal/gateway/codex_catalog.go index 2fe6e0f8b..d236f9784 100644 --- a/internal/gateway/codex_catalog.go +++ b/internal/gateway/codex_catalog.go @@ -67,3 +67,17 @@ func (s *Server) codexCatalog(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Write(b.Bytes()) } + +// codexTiers are the service tiers Codex here is offered on each of shown, +// by id: Fast on a ChatGPT account's GPT model, the tiers of Codex's own +// entry on a relay's. Another magpie that has this one as its provider +// offers its Codex the same, and sends the tier asked on (#1234). +func codexTiers(shown []provider.Entry) map[string][]any { + out := map[string][]any{} + for id, ts := range codexcat.ServiceTiers(provider.CodexCatalog(shown)) { + for _, t := range ts { + out[id] = append(out[id], t) + } + } + return out +} diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 8b3f69a06..ee3f3a8f0 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -812,12 +812,21 @@ func (s *Server) models(w http.ResponseWriter, r *http.Request) { if prices { st, find = settings.Load(), provider.GroupFinder() } + // for another magpie: the tiers its Codex is offered on each + // model, as Codex here is (#1234) + var tiers map[string][]any + if magpie { + tiers = codexTiers(shown) + } for i, e := range shown { m := modelObject(e) if magpie { if how := webSearchOf(e, searches); how != "" { m["web_search"] = how } + if ts := tiers[e.ID]; len(ts) > 0 { + m["service_tiers"] = ts + } } if prices { if pr, ok := provider.EntryPriceIn(st, find, e); ok { @@ -3788,8 +3797,10 @@ func (s *Server) forwardTranslated(ctx context.Context, p provider.Provider, to } } // the tier the client asked for goes to a provider the user added - // by its address as it was asked (Request.Tier) - if own := p.Preset == "" && p.Account == nil && s.fits(p.ID, tierField, to); own != req.OwnTier { + // by its address as it was asked (Request.Tier), and to another + // magpie, which offered it and serves it as its own Codex would be + // (#1234) + if own := (p.Preset == "" && p.Account == nil || p.IsRemoteMagpie()) && s.fits(p.ID, tierField, to); own != req.OwnTier { r := *req r.OwnTier, req = own, &r } diff --git a/internal/gateway/remote_magpie_tier_test.go b/internal/gateway/remote_magpie_tier_test.go new file mode 100644 index 000000000..3501ecb87 --- /dev/null +++ b/internal/gateway/remote_magpie_tier_test.go @@ -0,0 +1,123 @@ +package gateway + +import ( + "context" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" + + "github.com/yetone/magpie/internal/provider" +) + +// Codex is offered Fast on another magpie's model where that magpie offers +// its own Codex Fast on it, and a turn in Fast mode goes on to that magpie +// with service_tier priority, which it sends its vendor as its own Codex's +// turn would be (PennyVibe, #1234: remote-magpie/codex/gpt-6.1-sol had no +// tiers while sub2api/gpt-6.1-sol had Fast). A model it offers no tier on +// is offered none here either. Here the gateway is both computers', as in +// TestRemoteMagpieNativeAPI: "office" is it, reached over HTTP, and its +// vendor is a relay the user added by its address. +func TestRemoteMagpieOffersItsTiers(t *testing.T) { + var mu sync.Mutex + var vendor, hops []string // service_tier as the vendor, and the remote magpie, were sent it + tierOf := func(b []byte) string { + var v struct { + Tier *string `json:"service_tier"` + } + json.Unmarshal(b, &v) + if v.Tier == nil { + return "(none)" + } + return *v.Tier + } + up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + b, _ := io.ReadAll(r.Body) + mu.Lock() + vendor = append(vendor, tierOf(b)) + mu.Unlock() + w.Header().Set("Content-Type", "text/event-stream") + io.WriteString(w, sse( + `data: {"type":"response.created","response":{"id":"resp_1","model":"gpt-6.1-sol"}}`, + `data: {"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":"m1","role":"assistant","content":[]}}`, + `data: {"type":"response.output_text.delta","output_index":0,"content_index":0,"delta":"hello"}`, + `data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","id":"m1","role":"assistant","content":[{"type":"output_text","text":"hello"}]}}`, + `data: {"type":"response.completed","response":{"id":"resp_1","model":"gpt-6.1-sol","usage":{"input_tokens":5,"output_tokens":2}}}`)) + })) + t.Cleanup(up.Close) + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + t.Setenv("XDG_CACHE_HOME", t.TempDir()) + if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Models: []string{"gpt-6.1-sol", "glm-5"}, Responses: up.URL + "/v1"}); err != nil { + t.Fatal(err) + } + h := New().Handler() + remote := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodPost { + b, _ := io.ReadAll(r.Body) + r.Body = io.NopCloser(strings.NewReader(string(b))) + mu.Lock() + hops = append(hops, tierOf(b)) + mu.Unlock() + } + h.ServeHTTP(w, r) + })) + t.Cleanup(remote.Close) + id, err := provider.Add(provider.Provider{ID: "office", Name: "Office", Preset: provider.RemoteMagpiePreset, Chat: strings.TrimPrefix(remote.URL, "http://")}) + if err != nil { + t.Fatal(err) + } + office, err := provider.Find(id) + if err != nil { + t.Fatal(err) + } + if _, err := office.Fetch(context.Background()); err != nil { + t.Fatal(err) + } + + // the catalog Codex here reads + rec := httptest.NewRecorder() + h.ServeHTTP(rec, httptest.NewRequest("GET", CodexCatalogPath, nil)) + var cat struct { + Models []struct { + Slug string `json:"slug"` + Tiers []struct { + ID string `json:"id"` + Name string `json:"name"` + } `json:"service_tiers"` + } `json:"models"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &cat); err != nil { + t.Fatalf("%v: %s", err, rec.Body) + } + offered := map[string]string{} + for _, m := range cat.Models { + s := "" + for _, x := range m.Tiers { + s += x.ID + ":" + x.Name + " " + } + offered[m.Slug] = s + } + for slug, want := range map[string]string{ + "relay/gpt-6.1-sol": "priority:Fast ", "office/relay/gpt-6.1-sol": "priority:Fast ", + "relay/glm-5": "", "office/relay/glm-5": "", + } { + if got, ok := offered[slug]; !ok || got != want { + t.Errorf("%s offered %q (listed %v), want %q", slug, got, ok, want) + } + } + + // a turn in Fast mode, as Codex sends it + ask := strings.Replace(codexFastAsk("priority"), `"model":"m1"`, `"model":"office/relay/gpt-6.1-sol"`, 1) + code, body := post(t, "/v1/responses", ask) + if code != 200 { + t.Fatalf("%d: %s", code, body) + } + mu.Lock() + defer mu.Unlock() + if len(hops) != 1 || hops[0] != "priority" || len(vendor) != 1 || vendor[0] != "priority" { + t.Errorf("tier sent to the remote magpie %v, by it to its vendor %v; want priority, priority", hops, vendor) + } +} diff --git a/internal/provider/codex_models.go b/internal/provider/codex_models.go index 2275881fb..93e33d469 100644 --- a/internal/provider/codex_models.go +++ b/internal/provider/codex_models.go @@ -580,6 +580,11 @@ func codexListed(shown []Entry, members func(id string) []Member, own bool) []ca // a provider the user added by its address is sent the tier Codex // asks for as it is (hsiangron on X) m.OwnTier = e.Group == "" && e.Provider.Preset == "" && e.Provider.Account == nil && e.Provider.ID != "" + // another magpie offers the tiers its own Codex is offered on the + // model, and is sent the one Codex asks for as it is (#1234) + if e.Group == "" && e.Provider.IsRemoteMagpie() { + m.Tiers = e.Tiers + } if e.Group != "" { for _, mb := range members(e.ID) { if a := mb.Provider.Account; a != nil && a.Agent == "codex" && strings.HasPrefix(mb.Model, "gpt-") { diff --git a/internal/provider/models.go b/internal/provider/models.go index 531f4b792..1565431e9 100644 --- a/internal/provider/models.go +++ b/internal/provider/models.go @@ -1223,6 +1223,9 @@ type Entry struct { // multi-agent V2 for it, so Ultra hands work to its agents, whose // tasks a magpie-served lead writes as text. AgentsV2 bool `json:"-"` + // Tiers are the service tiers its list offers Codex on the model: + // another magpie's, those it offers its own Codex (#1234) + Tiers []string `json:"-"` } // Catalog lists the routing groups, then every exposed model of every ready @@ -1298,7 +1301,7 @@ func entryFor(p Provider, m catalog.Model, s settings.Settings) Entry { // an agent's list showed the whole magpie// (#955) 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, Output: output, Free: m.Free, Rate: m.Rate, RateWas: m.RateWas} + Images: images, ImageInput: imageInput, Context: ctx, Output: output, Free: m.Free, Rate: m.Rate, RateWas: m.RateWas, Tiers: m.Tiers} if n, ok := modelNameIn(s.ModelNames, p.ID, m.ID); ok { e.Name, e.Default = n, name } else {