Files
magpie/internal/gateway/codex_catalog.go
T
yetone 87a983423e codexcat: Codex's own GPT models stay in the Codex app's first 100 whatever the user's order (maiya on Discord)
maiya (Discord): after updating, the Codex app lists magpie's models and
none of Codex's own GPT ones.

Cause: the Codex app and its VS Code extension read the first 100 of the
list by priority (codexcat.AppList). codexMerged ranks the list by the
user's order from the Agents page (codexcat.Order, #855), where a model
the order doesn't name goes after every one it does, and codexcat.Fit
only brought the models Codex used back into the 100. The GUI saves the
whole shown list as the order, so an order saved while Codex's list had
no Codex section (the codex provider kept for routing groups before
a3ee00c05, Codex not yet signed in, or a GPT model OpenAI added since)
names 100+ of magpie's models and none of the account's: every unused
GPT model then landed at 101+ and was in no picker of the app. Since
add927e60 the app's desk list (CodexDesktopCatalog) is codexMerged's
too, so this reaches the app beside the ChatGPT sign-in, not only the
backend list.

Fix: codexcat.FitOwn keeps Codex's own listed models in the first 100
ahead of the used ones, in the order they stood; hidden entries still go
last, and a model the user hid in Codex's list was already taken out
before (CodexNativeHidden/CodexNativePicked). codexMerged passes the
backend's slugs; the siblings, /v1/codex/models (codexCatalog) and the
model_catalog_json file (CatalogUsed), pass a ChatGPT account's codex/...
entries (codexcat.ChatGPTOwn). Fit is FitOwn with none. Documented in
docs/subsystems/gateway-routing.md.

Tests:
- gateway TestCodexModelListKeepsOwnModelsInTheAppsHundred: the exact
  setup (ChatGPT sign-in, 130 magpie models, a Codex order naming all 130
  and no GPT model, the backend listing gpt-6-sol, gpt-5.5 and the hidden
  codex-auto-review): on 298bfce59 gpt-6-sol and gpt-5.5 were at 131 and
  132; now in the first 100, auto-review last, magpie's first 98 in the
  user's order.
- codexcat TestFitOwnKeepsCodexsOwnInTheAppsHundred and
  TestCatalogUsedKeepsChatGPTOwn.

Broken by hand: FitOwn with own forced to nil: all three red (131/132,
priority 131); restored, green.

Checked and not real (no change): CodexNativeHidden hiding an own model
while another ChatGPT entry for it is picked. There is one ChatGPT
provider ("codex"), one entry per slug however many accounts are saved,
and catalogOf adds an unlisted own entry once. A probe test (two
accounts, only-picked with both picked, a group that Unlists gpt-5.5, a
plain group, the provider Unlisted, back to hide mode) kept both slugs
out of CodexNativeHidden and in /backend-api/codex/models at every step
on 298bfce59.

Run (mote-run, this tree on 298bfce59): gofmt -l clean; go vet,
GOOS=linux go vet -tags nogui and GOOS=windows go vet on codexcat and
gateway: ok; go test -tags nogui -count=1 -timeout 45m ./internal/codexcat
./internal/gateway ./internal/provider ./internal/agent: all ok
(gateway 633s). Rebased onto b119f8777 (it changed codex_backend.go
elsewhere): gofmt, the three vets and go test ./internal/codexcat
./internal/gateway again: ok (gateway 627s).
Rebased again onto b5d2858f4 (internal/usage), then 40f206dff (vertex): gofmt, vets, codexcat and
usage ok, gateway -run Codex ok (54s); the whole gateway package not rerun.
Not run: the full suite (another session's full run was on mote).
Not tried: a real Codex app or VS Code extension, or maiya's own
settings (her order and provider setup are inferred, not seen).
2026-10-29 00:37:44 +08:00

91 lines
3.3 KiB
Go

package gateway
import (
"bytes"
"encoding/json"
"log"
"net/http"
"strconv"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/codexcat"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/usage"
)
// CodexCatalogPath is Codex's model list in its own shape, for a Codex that
// names magpie its model_provider (base_url <gateway>/v1) on another
// computer, with a gateway key (#1281): its model_catalog_url. /v1/models
// stays OpenAI's list, which Codex can't read, and CodexPath's list is the
// ChatGPT backend's, for a Codex signed in to ChatGPT here.
const CodexCatalogPath = "/v1/codex/models"
// codexCatalogMost is the most of a catalog Codex reads from a
// model_catalog_url (MAX_MODEL_CATALOG_BYTES in codex-rs's models
// endpoint): past it the whole list fails to load and Codex's picker has
// none of magpie's models.
const codexCatalogMost = 1 << 20
// codexCatalog answers CodexCatalogPath: the models magpie shows Codex, as
// the model_catalog_json magpie writes for a Codex here describes them,
// kept to the models a gateway key may use (keyAllowed, as /v1/models is).
// Codex's ?client_version= changes nothing. Entries that would take the
// answer past codexCatalogMost are left out, the last first, and the
// answer says how many in X-Magpie-Left-Out.
func (s *Server) codexCatalog(w http.ResponseWriter, r *http.Request) {
// the catalog, the groups and the windows read the providers over and
// over: held, they are built once (#746)
defer provider.Hold()()
shown, _ := provider.CatalogFor("codex")
ms := provider.CodexCatalog(keyAllowed(r, shown))
head, tail := []byte(`{"models":[`), []byte("]}\n")
var b bytes.Buffer
b.Write(head)
n, out := 0, 0
// the Codex app reads the first 100 alone (codexcat.AppList): a ChatGPT
// account's own models and the ones this key's Codex used are among
// them, and kept when the end is cut
entries := codexcat.Entries(ms, 0)
codexcat.FitOwn(entries, usage.CodexUsedBy(access.Caller(r.Context()).KeyID), codexcat.ChatGPTOwn)
for _, e := range entries {
var one bytes.Buffer
enc := json.NewEncoder(&one)
enc.SetEscapeHTML(false)
if enc.Encode(e) != nil {
continue
}
raw := bytes.TrimRight(one.Bytes(), "\n")
if b.Len()+1+len(raw)+len(tail) > codexCatalogMost {
out++
continue
}
if n > 0 {
b.WriteByte(',')
}
b.Write(raw)
n++
}
b.Write(tail)
if out > 0 {
log.Printf("%s: %d of Codex's %d models left out, past the %d bytes Codex reads of a catalog; pick fewer for Codex on the Agents page, or hold the key to fewer", CodexCatalogPath, out, n+out, codexCatalogMost)
w.Header().Set("X-Magpie-Left-Out", strconv.Itoa(out))
}
w.Header().Set("Content-Type", "application/json")
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
}