diff --git a/docs/subsystems/providers-accounts.md b/docs/subsystems/providers-accounts.md index 163cef27d..f10eefd15 100644 --- a/docs/subsystems/providers-accounts.md +++ b/docs/subsystems/providers-accounts.md @@ -120,6 +120,24 @@ list has no separate retrieval kind: these entries remain in `Catalog` and agent model lists, just as on the remote. Keeping them makes retrieval routing possible; their presence does not mean they support conversations. +A remote magpie's models carry the entries that magpie hands its own Codex +on another computer: the list fetch asks `/v1/codex/models` once more with +Codex's `client_version` and the remote's key (`codexEntries` in +[`codex_entries.go`](../../internal/provider/codex_entries.go), +`TestRemoteMagpieHandsOnItsCodexEntries`), and `codexcat.listedEntry` +hands each entry on under the local id, its `" via magpie"` said once. A +ChatGPT account the remote holds is Codex's own model here too: an entry +whose slug is `codex/` under an id ending in it +(`office/codex/gpt-6-astra`, `codexcat.OwnSlug`) takes this computer's +Codex cache entry for the slug, as a local account's `codex/` does, +and counts as Codex's own where the Codex app reads the list (`OwnIn`, +`TestRemoteChatGPTModelIsCodexOwn`). Before this, the remote's ChatGPT +models reached Codex as entries magpie made up for a third party's model +(Darkf1ames on X). What stays apart from a local account: Codex names +magpie its provider for them, and a remote with no Codex cache of its own +hands on entries made from the bundled instructions, without the +backend's other fields. + `DecidesModel` uses the remote's explicit decision marker, so Clef and custom names work and a chat model named Jev Router stays a chat model. `Deciders` lists, and the gateway answers, these decisions even when the user's local diff --git a/internal/codexcat/codexcat.go b/internal/codexcat/codexcat.go index af4289fe2..cf56d8c44 100644 --- a/internal/codexcat/codexcat.go +++ b/internal/codexcat/codexcat.go @@ -216,7 +216,7 @@ func CatalogAt(ms []catalog.Model, effort string) []byte { return CatalogUsed(ms // them (FitOwn). func CatalogUsed(ms []catalog.Model, effort string, used []string) []byte { entries := entriesAt(ms, 0, effort) - FitOwn(entries, used, ChatGPTOwn) + FitOwn(entries, used, OwnIn(ms)) out := struct { Models []any `json:"models"` }{Models: entries} @@ -233,6 +233,44 @@ func CatalogUsed(ms []catalog.Model, effort string, used []string) []byte { // AppList. func ChatGPTOwn(slug string) bool { return strings.HasPrefix(slug, "codex/") } +// OwnSlug is the slug of Codex's own model m is, when a ChatGPT account +// serves it: one magpie holds (codex/), or one another magpie holds, +// whose entry for its own Codex names it codex/ (office/codex/, +// provider.codexEntries). Such a model is given Codex's own entry for the +// slug where this computer's Codex has one, as a ChatGPT account here is, +// and is one of Codex's own models where the Codex app reads them (FitOwn). +func OwnSlug(m catalog.Model) (string, bool) { + if slug, ok := strings.CutPrefix(m.ID, "codex/"); ok { + return slug, true + } + if len(m.Codex) == 0 { + return "", false + } + var e struct { + Slug string `json:"slug"` + } + if json.Unmarshal(m.Codex, &e) != nil { + return "", false + } + slug, ok := strings.CutPrefix(e.Slug, "codex/") + if !ok || slug == "" || !strings.HasSuffix(m.ID, "/"+e.Slug) { + return "", false + } + return slug, true +} + +// OwnIn is ChatGPTOwn for ms: a slug of theirs that is Codex's own model +// (OwnSlug), another magpie's ChatGPT account's too. +func OwnIn(ms []catalog.Model) func(slug string) bool { + remote := map[string]bool{} + for _, m := range ms { + if _, ok := OwnSlug(m); ok { + remote[m.ID] = true + } + } + return func(slug string) bool { return ChatGPTOwn(slug) || remote[slug] } +} + // level, tier and model are an entry of models.json as Entries writes it. type level struct { Effort string `json:"effort"` @@ -364,7 +402,8 @@ func entriesAt(ms []catalog.Model, after int, effort string) []any { autoReview := AutoReviewModel() var entries []any for i, m := range ms { - if raw, ok := own[strings.TrimPrefix(m.ID, "codex/")]; ok && strings.HasPrefix(m.ID, "codex/") { + if slug, ok := OwnSlug(m); ok && own[slug] != nil { + raw := own[slug] e := ownEntry(raw, m.ID, shownName(m), after+i+1) // the window as magpie resolves it — the one the user set, // else the account's list's — not the cache's, which is what @@ -1280,7 +1319,7 @@ func listedEntry(m catalog.Model, priority int, work settings.Settings, autoRevi e["input_modalities"] = slices.DeleteFunc(slices.Clone(in), func(x any) bool { return x == "image" }) } } - if v1 && (m.Fast || m.Seals) { + if _, own := OwnSlug(m); v1 && (m.Fast || m.Seals || own) { Stamp(e) } else if _, ok := e["multi_agent_version"]; !ok && m.AgentsV2 { e["multi_agent_version"] = "v2" diff --git a/internal/codexcat/remote_entry_test.go b/internal/codexcat/remote_entry_test.go index b1d31d268..850ef0054 100644 --- a/internal/codexcat/remote_entry_test.go +++ b/internal/codexcat/remote_entry_test.go @@ -4,6 +4,7 @@ import ( "encoding/json" "os" "path/filepath" + "strings" "testing" "github.com/yetone/magpie/internal/catalog" @@ -37,3 +38,48 @@ func TestRemoteMagpieEntry(t *testing.T) { t.Errorf("office/deepseek/deepseek-v4's description %q", d) } } + +// Another magpie's ChatGPT account's model (its entry's slug codex/) +// is Codex's own, as a ChatGPT account's here is: given this computer's +// Codex's entry for the slug where its cache has one, model_messages and +// all, which the remote, often a server with no Codex, couldn't give. +// A third party's model named like one (relay/gpt-6-astra) is not. +func TestRemoteChatGPTModelIsCodexOwn(t *testing.T) { + h := t.TempDir() + t.Setenv("HOME", h) + t.Setenv("USERPROFILE", h) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config")) + t.Setenv("CODEX_HOME", filepath.Join(h, ".codex")) + os.MkdirAll(filepath.Join(h, ".codex"), 0o755) + cache := `{"etag":"e1","models":[{"slug":"gpt-6-astra","display_name":"GPT-6-Astra","description":"Latest frontier agentic coding model.","base_instructions":"You are Codex","model_messages":{"instructions_template":"T"},"shell_type":"shell_command","supported_reasoning_levels":[{"effort":"low","description":"Fast"}],"visibility":"list"}]}` + if err := os.WriteFile(catalog.CodexModelsCache(), []byte(cache), 0o644); err != nil { + t.Fatal(err) + } + remote := json.RawMessage(`{"slug":"codex/gpt-6-astra","description":"GPT-6-Astra via magpie","base_instructions":"You are Codex","shell_type":"shell_command","supported_reasoning_levels":[{"effort":"low","description":"Fast"}]}`) + ms := []catalog.Model{ + {ID: "office/codex/gpt-6-astra", Name: "GPT-6-Astra", Efforts: []string{"low"}, Codex: remote}, + {ID: "relay/gpt-6-astra", Name: "GPT-6-Astra", Efforts: []string{"low"}}, + } + var got struct { + Models []map[string]any `json:"models"` + } + json.Unmarshal(Catalog(ms), &got) + by := map[string]map[string]any{} + for _, e := range got.Models { + slug, _ := e["slug"].(string) + by[slug] = e + } + if msgs, _ := by["office/codex/gpt-6-astra"]["model_messages"].(map[string]any); msgs["instructions_template"] != "T" { + t.Errorf("office/codex/gpt-6-astra: %v", by["office/codex/gpt-6-astra"]) + } + if d := by["office/codex/gpt-6-astra"]["description"]; d != "Latest frontier agentic coding model." { + t.Errorf("office/codex/gpt-6-astra's description %q, want Codex's own", d) + } + if d, _ := by["relay/gpt-6-astra"]["description"].(string); !strings.HasSuffix(d, " via magpie") { + t.Errorf("relay/gpt-6-astra took Codex's own entry: %v", by["relay/gpt-6-astra"]) + } + own := OwnIn(ms) + if !own("office/codex/gpt-6-astra") || !own("codex/gpt-6-sol") || own("relay/gpt-6-astra") { + t.Error("OwnIn") + } +} diff --git a/internal/gateway/codex_catalog.go b/internal/gateway/codex_catalog.go index 845abc8aa..0d374dd7a 100644 --- a/internal/gateway/codex_catalog.go +++ b/internal/gateway/codex_catalog.go @@ -67,7 +67,7 @@ func (s *Server) codexCatalog(w http.ResponseWriter, r *http.Request) { // 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) + codexcat.FitOwn(entries, usage.CodexUsedBy(access.Caller(r.Context()).KeyID), codexcat.OwnIn(ms)) for _, e := range entries { var one bytes.Buffer enc := json.NewEncoder(&one)