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.
84 lines
2.9 KiB
Go
84 lines
2.9 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())
|
|
}
|
|
|
|
// 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
|
|
}
|