Files
magpie/internal/gateway/codex_desktop.go
T
yetone add927e604 codex: the Codex app beside the ChatGPT sign-in lists magpie's models too
laddieyao (Discord #general, 10-27): with Codex joined to magpie beside
its ChatGPT sign-in, the Codex desktop app's model picker showed OpenAI's
GPT models alone, while the Codex CLI listed magpie's. The /models
request does reach magpie's gateway and its answer holds magpie's
models. ChatGPT.app 26.1013's picker then narrows app-server model/list
to the models a Statsig config (107580212, use_hidden_models /
available_models) names, while Codex is on its built-in provider, unless
Codex reads its list from a file (model_catalog_json,
hasConfiguredModelCatalog) or names a provider of its own. The CLI has
no such filter.

Where the Codex app is installed (codexReaders has an "app"), and only
joined/beside the sign-in (not magpie as Codex's provider, not account
failover alone), magpie writes the list it hands Codex at
/backend-api/codex/models to ~/.codex/magpie-desktop-models.json and
names it with model_catalog_json. Codex is still on its built-in
provider. With a catalog named Codex asks for no list, so magpie asks
the backend itself, with Codex's live sign-in (provider.CodexLiveList):
its entries go in verbatim, base_instructions and all, merged as the
gateway merges them (codexMerged, factored out of codexModels: picked,
hidden, window, V1, AutoReview edits), magpie's models after them.

- A list not known (not asked yet, or the backend failing) is unknown:
  the file as it was stays, never an empty list; a key naming a file
  that is gone goes, as Codex loads no config then.
- Written via temp+rename at Join, set, settle and every Sync (so a
  change of magpie's models, picks or hidden models rewrites it, as
  SyncCatalog runs on them); the backend list is asked again hourly
  (5 min after a failure) and a change syncs again.
- A model_catalog_json of the user's own stands; none is written beside.
- magpie API, the app gone, or unwire take the key and the file out;
  unroute never restores the desk file as the user's catalog.
- connectinfo counts the file, so a running app on an old list reads
  as needing a restart.
- CLI-only machines: unchanged, the live /models path as before.

Run:
- Real account, sandbox HOME (copy of auth.json, chatgpt Pro; live
  ~/.codex untouched): Join wrote model_catalog_json and the file with
  the real backend's 10 entries (gpt-6.1-sol ... codex-auto-review, each
  with base_instructions) plus relay/claude-opus-5.5 and
  relay/claude-sonnet-4-5. Then the real app-server bundled in
  ChatGPT.app (codex 0.163.0-alpha.10.2, CODEX_HOME sandbox, base URL
  pointed at a dead port) model/list returned all 12, relay models
  visible, and config/read showed model_catalog_json set, which is what
  the app's picker checks to skip its filter.
- New tests: TestCodexDesktopListBesideSignIn, ...CLIOnly,
  ...KeepsTheirs (internal/agent), TestCodexDesktopCatalog
  (internal/gateway).
- Broken by hand, each went red: deskList a no-op (all three agent
  tests), the user's-own branch removed (KeepsTheirs), the gone-file
  branch removed (KeepsTheirs), the keep-last-good rule broken in
  codexDeskRefresh (TestCodexDesktopCatalog).
- go test -tags nogui -timeout 45m ./internal/agent ./internal/gateway
  ./internal/provider ./internal/cli at bc0a99bb6 (on 68bbcc01c):
  agent, gateway, cli ok; provider red on TestUnreadLogins...
  (synctest crash, #3632) and three TestFetch* tests (#3854), both red
  the same at origin/main 68bbcc01c / 5a3292354 run alone, both filed.
- Rebased over e7f21dba5 (Disconnect keeps a hand-set catalog): the
  conflict in unroute merged (the desk key counts as no catalog of the
  user's), and -run 'CodexDesktop|Disconnect|Unwire|Catalog'
  ./internal/agent ok. The packages' run at the final head could not
  build: the machine's disk was full (other sessions).
- gofmt -l clean; go vet ./... on darwin, GOOS=linux, GOOS=windows ok.

Not tried: clicking the real desktop app's picker (the app-server
answer and config were checked, not the GUI); the Windows Store app
(its readers get no version but count as the app; not run there); a
backend list change rewriting the file while the app runs (the app
needs reopening to read a new file, which connectinfo's restart notice
covers).
2026-10-27 18:38:27 +08:00

102 lines
3.3 KiB
Go

package gateway
import (
"bytes"
"context"
"encoding/json"
"log"
"sync"
"time"
"github.com/yetone/magpie/internal/provider"
)
// The Codex app's picker shows only the models of Codex's list that a
// flag of OpenAI's names, for Codex on its built-in provider, unless Codex
// reads its list from a file (model_catalog_json): beside the ChatGPT
// sign-in it showed GPT alone, magpie's models in the list it was handed
// all the same (laddieyao on Discord). The CLI shows the whole list. So
// where the Codex app is installed, magpie writes the list it hands Codex
// into a file for Codex to read (agent.CodexDesktopList), and Codex asks
// for no list then: the backend's part of it is asked for here, with
// Codex's sign-in, and kept.
// CodexDesktopChanged is called when the backend's list for the Codex app
// changed, for the file to be written again (agent.SyncCatalogSoon).
var CodexDesktopChanged func()
// codexDeskFetch asks the backend for its list (a test's stands in).
var codexDeskFetch = provider.CodexLiveList
const (
// codexDeskFor is how long a list the backend gave stands before it
// is asked for again; codexDeskRetry how long after one it didn't
codexDeskFor = time.Hour
codexDeskRetry = 5 * time.Minute
codexDeskWait = 30 * time.Second
)
var codexDesk struct {
sync.Mutex
body []byte
got, tried time.Time
asking bool
done chan struct{} // closed as the ask under way ends
}
// CodexDesktopCatalog is the list a Codex beside its ChatGPT sign-in is
// handed (/backend-api/codex/models), as a file Codex reads with
// model_catalog_json: the backend's entries as it sent them, narrowed and
// set as the codex provider has them, magpie's after them. ok is false
// while the backend's list is not known: none asked for yet, or none it
// gave. That is unknown, never an empty list, and the file as it was
// stands. The backend is asked again in the background when its list is
// old, CodexDesktopChanged told when it changed.
func CodexDesktopCatalog() (b []byte, ok bool) {
codexDesk.Lock()
body := codexDesk.body
stale := body == nil || time.Since(codexDesk.got) >= codexDeskFor
if stale && !codexDesk.asking && time.Since(codexDesk.tried) >= codexDeskRetry {
codexDesk.asking, codexDesk.tried = true, time.Now()
codexDesk.done = make(chan struct{})
go codexDeskRefresh(codexDesk.done)
}
codexDesk.Unlock()
if body == nil {
return nil, false
}
// each caller's own copy: codexMerged writes into the entries
var list struct {
Models []any `json:"models"`
}
if json.Unmarshal(body, &list) != nil || len(list.Models) == 0 {
return nil, false
}
b, err := json.MarshalIndent(map[string]any{"models": codexMerged(list.Models, false)}, "", " ")
if err != nil {
return nil, false
}
return b, true
}
func codexDeskRefresh(done chan struct{}) {
ctx, cancel := context.WithTimeout(context.Background(), codexDeskWait)
defer cancel()
b, err := codexDeskFetch(ctx)
codexDesk.Lock()
changed := false
if err == nil {
changed = !bytes.Equal(b, codexDesk.body)
codexDesk.body, codexDesk.got = b, time.Now()
}
codexDesk.asking = false
codexDesk.Unlock()
if err != nil {
log.Println("codex: the ChatGPT backend's model list for the Codex app:", err)
}
if changed && CodexDesktopChanged != nil {
CodexDesktopChanged()
}
close(done)
}