@ZhenLuoTTAWDTT on X: with Codex on magpie the model list stops short, and the models past it can't be chosen. Cause, from the clients' own code: the Codex app (ChatGPT.app 26.1013.40934, app-initial-90eb42395b7d.js) and the VS Code extension (openai.chatgpt 26.51021.22027, model-queries-667cf4bd657d.js) both ask their app-server for model/list with includeHidden, cursor null and limit 100 (e?.limit??100) and never ask for nextCursor. codex-rs's list_models pages the list sorted by priority, hidden entries counted, so anything past the 100th is in no picker there. The CLI's /model (tui model_popups.rs) lists every preset in a scrolling popup and has no cap. magpie truncated nothing; its models are numbered after the account's own, so a user with many models lost everything past ~100 in the app, while hidden entries (codex-auto-review and the like) took slots at the top. Fix: codexcat.Fit ranks the list last of all, on every route magpie hands Codex a list (/backend-api/codex/models after the user's own order, /v1/codex/models before its 1 MiB cut, and the model_catalog_json file via CatalogUsed): hidden entries go after every listed one, and with more than 100 listed, the models Codex used in the last 30 days (usage.CodexUsed, most recent first, at most 100; on /v1/codex/models only the asking key's, CodexUsedBy) are brought into the first 100 in the order they stood. The rest keep their order. The list's ETag and X-Models-Etag carry a tag of the used set (codexcat.UsedTag), so Codex asks again when it uses a model it hadn't; CodexUsed is read at most once a minute, or at once when Append writes a Codex call to a model it doesn't hold. Documented in docs/subsystems/gateway-routing.md. Tests (each red on the old code, broken by hand: Fit a no-op, the gateway calls and the used tag removed, Append's codexUsedNew removed): TestFitKeepsUsedModelsWhereTheCodexAppReads, TestFitLeavesAShortListsOrder, TestCatalogUsedFits, TestUsedTagIsTheSet (codexcat), TestCodexUsed (usage), TestCodexModelListKeepsUsedModelsInTheAppsHundred and TestCodexCatalogKeepsTheKeysUsedModelsFirst (gateway). Real run: codex-cli 0.162.1's app-server in a sandbox CODEX_HOME (never ~/.codex) with a model_catalog_json of 3 hidden entries at priority 1-3 and 150 listed models, driven over stdio with the app's own request (model/list includeHidden, limit 100). Before: page 1 held 100 of 153, 3 of them hidden, the last many/model-096, nextCursor "100". The same catalog put through Fit with many/model-140 used: page 1 has no hidden entry and ends with many/model-140. The TUI's /model scrolled to all 150 (tmux). Run with this change one4ca47b0f: gofmt -l clean; go vet -tags nogui with GOOS=linux and GOOS=windows on codexcat, usage, gateway and agent clean. go test -count=1 -timeout 45m: codexcat ok, usage ok; -run 'Codex|Fit|Used|ModelList|Catalog' on gateway, usage and agent ok (122s, 11s, 80s). The whole gateway and agent packages ran too, but the machine's disk was full under other sessions' runs (209M to 2.5G free), and 959 lines of that log are "no space left on device". Of the reds, three are not that: TestFolderVarsAreListed is #3884 (CARGO_HOME, red on main sincee4552cca9); TestHistoryKeepsDaysAndDropsOld (gateway) and TestSuffixModesReachAgentFiles (agent) failed beside half-written settings files and pass alone 3 of 3 at this commit. Not run: the full ./... suite (no room on the disk); the Codex app and VS Code extension themselves against a magpie with this change (their request was read from their bundles and replayed against a real app-server). Rebased onca8a6f346(agent rename and Codex app route, claude_runs in gateway) and run again: GOOS=windows go vet on agent and gateway clean; -run 'Codex|Fit|Used|ModelList|Catalog' ok on codexcat, usage and gateway (75s), and with |Rename ok on agent (68s). Then rebased on94c2aaecb(usage rename) and usage run whole again: ok (88s).
90 lines
3.2 KiB
Go
90 lines
3.2 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): the ones
|
|
// this key's Codex used are among them, and kept when the end is cut
|
|
entries := codexcat.Entries(ms, 0)
|
|
codexcat.Fit(entries, usage.CodexUsedBy(access.Caller(r.Context()).KeyID))
|
|
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
|
|
}
|