Files
magpie/internal/gateway/codex_own_v1.go
T
yetone 4586307de5 gateway: Codex's own model on a thread started on magpie goes on Codex's sign-in, not a 404
iuengei on Discord: after turning Codex's routing off and restarting,
Codex's official models answered "unexpected status 404 Not Found: magpie
knows no model \"gpt-6.1-sol\"", in ChatGPT and Always ChatGPT modes too.

A Codex thread keeps the provider it was started on (the
[model_providers.magpie] table stays for that reason). A thread started
while magpie was Codex's provider (magpie API, or the takeover of a
used-up account) goes on sending to /v1 after Disconnect or a switch back
to ChatGPT. An official model picked in it reaches serve as a bare
"gpt-6.1-sol"; with no ChatGPT account in magpie serving it (the Codex
sign-in removed from Providers, or subscription accounts off) it was
answered 404. /backend-api/codex relays such a model on Codex's own
sign-in; /v1 never did.

Now handle (Responses) puts such a request on the ChatGPT backend's route
(codexBackend) signed with Codex's own ChatGPT sign-in as Codex keeps it
(provider.CodexSignInAsKept, never renewed by magpie), magpie's key and
x-openai-actor-authorization left behind (codexOwnOffMagpie). Only for: a
bare model no provider, group or stand-in serves, not disabled or
switched off; one of Codex's own (its models cache, magpie's entries left
out) or OpenAI's in the catalog; from Codex on this computer (local); no
gateway key held to models or accounts. When the sign-in can't be had
(subscription accounts off, signed out, expired) the 404 says the thread
was started on magpie, why the sign-in can't be used, and to start a new
thread. Codex's identity and prompts are untouched.

Tests (internal/gateway/codex_own_v1_test.go): the reporter's case, a
sandbox HOME with Codex signed in, gpt-6.1-sol in its models cache, the
sign-in removed from magpie's Providers, Codex's request to /v1/responses
with magpie's key: was 404 "magpie knows no model \"gpt-6.1-sol\"", now
200 from a stand-in ChatGPT backend that got Codex's token and account
id, not magpie's key or header. Not sent: from another computer, a key
held to fake/*, a model that isn't OpenAI's (still 404). With accounts
off, a 404 that says why.

Run (on mote, at this commit's tree rebased on 173fb6ab0):
- gofmt -l internal/gateway internal/provider: clean
- go vet darwin, GOOS=linux -tags nogui, GOOS=windows for
  ./internal/gateway ./internal/provider: ok
- go test -timeout 45m ./internal/gateway ./internal/provider: ok (gateway 482s, provider 91s)
Broken by hand: the hook in handle off, GoesOnSignIn and SaysWhy are red
(the old 404); local() off, StaysHere/another_computer is red. The held
key is also kept off the relay by codexAccounts in codexBackend, so the
guard here off alone leaves that subtest green.
Not tried: a real Codex app thread against a real ChatGPT account (no
live ~/.codex touched).
2026-10-29 23:15:19 +08:00

111 lines
3.6 KiB
Go

package gateway
import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"strings"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/codexcat"
"github.com/yetone/magpie/internal/provider"
)
// codexSignInToken is Codex's ChatGPT sign-in as Codex keeps it, which
// tests set.
var codexSignInToken = provider.CodexSignInAsKept
// codexOwnOffMagpie is the request a Codex on this computer sent to /v1
// for one of its own models that magpie serves nothing for, put on the
// ChatGPT backend's route (CodexPath) and signed with Codex's own ChatGPT
// sign-in, as Codex's built-in provider would have sent it. A thread
// keeps the provider it was started on: one started while magpie was
// Codex's provider (its API, or the takeover of a used-up account) goes
// on sending to /v1 after Codex is disconnected or set back to ChatGPT,
// and an official model picked in it (gpt-6.1-sol) met "magpie knows no
// model" whenever no ChatGPT account in magpie serves it (iuengei on
// Discord). nil when that isn't the case: a model magpie serves, switched
// off, disabled or a group; a model not OpenAI's; a request from another
// computer or held by its gateway key. Codex's sign-in not to be had
// (subscription accounts off in magpie, signed out, expired), why is what
// the request is answered instead of the bare 404: what sends it here and
// what to do.
func codexOwnOffMagpie(r *http.Request, body []byte, model string) (to *http.Request, why string) {
if model == "" || strings.Contains(model, "/") || !local(r) {
return nil, ""
}
if !fromCodex(r.Header) && agentOf(r) != "codex" {
return nil, ""
}
if _, _, ok := provider.Resolve(model); ok {
return nil, ""
}
if provider.IsGroupRef(model) {
return nil, ""
}
if _, ok := provider.GroupFor(model); ok {
return nil, ""
}
if _, ok := provider.AutoStandIn(model); ok {
return nil, ""
}
if _, off := idOff(model); off {
return nil, ""
}
if _, off := provider.SwitchedOff(model); off {
return nil, ""
}
if _, keyHeld := keyHolds(r); keyHeld {
return nil, ""
}
if _, accHeld := accountHolds(r); accHeld {
return nil, ""
}
if !codexOwnModel(model) {
return nil, ""
}
tok, acct, err := codexSignInToken()
if err != nil || tok == "" {
if err == nil {
err = errors.New("Codex is signed out")
}
return nil, fmt.Sprintf("%q is one of Codex's own models, and this Codex thread was started while magpie was Codex's model provider, so it is still sent to magpie whatever Codex is set to now. "+
"No ChatGPT account in magpie serves it, and magpie can't send it on Codex's own ChatGPT sign-in: %v. "+
"Start a new thread in Codex for its own models, or add Codex's ChatGPT account in magpie (Providers)", model, err)
}
r2 := r.Clone(r.Context())
r2.URL.Path = CodexPath + "/responses"
r2.URL.RawPath = ""
r2.RequestURI = ""
r2.Body = io.NopCloser(bytes.NewReader(body))
r2.ContentLength = int64(len(body))
r2.Header.Del("Content-Encoding")
r2.Header.Del("Content-Length")
// magpie's key and the header that names magpie Codex's provider are
// magpie's own, never sent on to OpenAI
r2.Header.Del("X-Api-Key")
r2.Header.Del("X-Openai-Actor-Authorization")
r2.Header.Set("Authorization", "Bearer "+tok)
if acct != "" {
r2.Header.Set("Chatgpt-Account-Id", acct)
}
return r2, ""
}
// codexOwnModel: model is one of Codex's own, as the ChatGPT backend last
// listed it to Codex (models_cache.json, magpie's entries left out) or as
// the catalog has it for OpenAI.
func codexOwnModel(model string) bool {
if _, ok := codexcat.CacheEntries()[model]; ok {
return true
}
for _, m := range catalog.Provider("openai") {
if m.ID == model {
return true
}
}
return false
}