Codex decodes a model_catalog_url as ModelsResponse {models:[ModelInfo]}
(codex-rs model-provider/src/models_endpoint.rs), which /v1/models' OpenAI
list isn't, and /backend-api/codex/models relays to the ChatGPT backend,
which answers a gateway key (replaced by magpie's token) with 401. The new
route lists the models magpie shows Codex, as codexcat writes them for
model_catalog_json, kept to the key's models (keyAllowed, as /v1/models),
the ChatGPT account's own included (provider.CodexCatalog). Codex reads at
most 1 MiB of such a catalog (MAX_MODEL_CATALOG_BYTES); entries past it are
left out and counted in X-Magpie-Left-Out rather than failing the list.
Verified:
- TestCodexCatalogForAGatewayKey through lanGuard(Handler) from a LAN
address: required ModelInfo fields, unique slugs, held key sees only its
models, no key / wrong key 401, /v1/models unchanged. Without the route it
fails "404 ... magpie serves /v1/chat/completions ..."; with keyAllowed
removed it fails "held key: [plan/m1 spare/m2]".
- TestCodexCatalogFitsCodexsLimit: 300 models -> 127 in 1047147 bytes, 173
left out. Fails 404 without the route.
- Real Codex CLI 0.161.0 (npm) and 0.160.1 in sandbox CODEX_HOMEs against a
sandbox `magpie serve` (0.0.0.0:3591, LAN on, two gateway keys) over
10.0.0.111 with a fake chat upstream: models_cache.json holds the catalog
(held key: fake/fake-coder only; free key: both), no "Unknown model"
fallback warning, codex exec answered PONG-1281 through /v1/responses,
also with the reporter's requires_openai_auth = true. 0.160.1 needs
[features] api_key_model_discovery = true.
- go test -tags nogui ./internal/gateway ./internal/provider in a temp HOME:
ok. Full ./...: 44 packages ok (internal/agent had timed out once
under load; alone it passes in 45s). go vet; GOOS=linux and
GOOS=windows vet of gateway+provider and nogui builds.
Not verified: Codex's TUI /model picker driven by hand.
70 lines
2.4 KiB
Go
70 lines
2.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/yetone/magpie/internal/codexcat"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// 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
|
|
for _, e := range codexcat.Entries(ms, 0) {
|
|
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())
|
|
}
|