Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
gateway: a streamed Codex turn goes to ChatGPT over its WebSocket (brickhao)
yetonea2e98cd79925
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.mdindex 658c9bced..058bec024 100644--- a/docs/subsystems/gateway-routing.md+++ b/docs/subsystems/gateway-routing.md@@ -221,7 +221,9 @@ A remote provider serves the conversation, token-counting, System One,embeddings, rerank, speech-to-text, image generation/edit and videocreation/status/content APIs. Gemini clients enter through the local Gemini adapter and follow thesame model routing as other conversation clients. Responses uses HTTP/SSE;-its WebSocket route still answers 426. Discovery and older-peer behavior+its WebSocket route still answers 426 (magpie's own leg to the ChatGPT+backend is a WebSocket for a streamed turn: see+[Providers and accounts](providers-accounts.md)). Discovery and older-peer behaviorare described in [Providers and accounts](providers-accounts.md#remote-magpie).`GET /v1/magpie/limit` (`keyLimit` in[`key_limit.go`](../../internal/gateway/key_limit.go)) answers the limit ofdiff --git a/docs/subsystems/providers-accounts.md b/docs/subsystems/providers-accounts.mdindex 227ed69d5..c01c94204 100644--- a/docs/subsystems/providers-accounts.md+++ b/docs/subsystems/providers-accounts.md@@ -211,6 +211,7 @@ and `TestHistoricalWindowHoldsUsesEachWindowsOwnReading`.- A Copilot account is known by its GitHub login and host (`CopilotAccountName`, #1220): `mona` on github.com, `[email protected]` on an enterprise's GHE.com. That name is its `logins.json` user, its row in every list, its usage card and its per-account routing settings, and it is what `SwitchLogin`, `SetLoginOn` and `ForgetLogin` take, so one login on two hosts is two accounts, neither written over by the other nor dropped as the editors' own. One saved before on an enterprise's host under its login alone is read under its name with the host (`copilotSavedName`), keeping its id; the editors' own remembered so is renamed in place, still hidden if it was (`copilotRenamed`). Either rename takes the account's per-account settings in `providers.json` (proxy, models, cap, concurrency) from the bare login to the new name before the new name is written to `logins.json` (`renameSettled`): a setting the new name has already stays, and while `providers.json` can't be read or written the account stays saved under its old name, its settings with it. `magpie accounts switch|forget copilot` takes the full name, the login with `--host <name>.ghe.com`, or the login alone when it names one account.- A Codex account is known by its email, with the plan for a workspace seat (`codexUser`), and a second seat of one email in another workspace by that name with its workspace after it (`codexName`): `[email protected] · Team · ws-two`. The account Codex is signed in to goes by the name it is saved under (`codexLiveName`) everywhere: its provider's `Account.User` (`codexAccount`), `liveLogin`, and its session identity (`SessionIdentities`). Its allowance, rests and sticks (`restKey`), cap, proxy, model list, a gateway key's account list (`LoginID`) and the Routing page all go by that name, so two seats of one email are never read as one (#1424).- Every request to the ChatGPT backend (`CodexBase`'s host) goes over HTTP/1.1, as Codex itself speaks to it: each transport [`netproxy.Dispatch`](../../internal/netproxy/netproxy.go) makes (`http.DefaultClient`'s, the gateway's) sends a request `netproxy.HTTP1` names through an HTTP/1.1 copy of itself whose TLS hello offers no h2, and the provider package sets `HTTP1` to `codexBackend` ([codex_http1.go](../../internal/provider/codex_http1.go)). That covers the gateway's turns, the models list, the warm-up, the allowance, its resets and spending one, through the proxy in force, an account's own included. Every other vendor is still offered HTTP/2.+- A streamed turn to the ChatGPT backend (POST `CodexBase`/responses with `"stream":true`) goes over the backend's WebSocket, as Codex's own client sends it, and comes back as the server-sent events the HTTP route answers with (`codexSocket` in [codex_socket.go](../../internal/gateway/codex_socket.go), first in the gateway's `upstreamTransport`, so Codex's own sign-in passed through and magpie's ChatGPT accounts both take it, through the proxy in force). On some networks a streamed HTTP reply from chatgpt.com is cut at about 120 s, before `response.completed`, while the WebSocket runs to the end (brickhao on Discord). The handshake carries the turn's headers with `OpenAI-Beta: responses_websockets=2026-02-06`, without `x-codex-turn-state` and the responses-lite header, which go in the message's `client_metadata` as Codex puts them; the message is the body with `"type":"response.create"` ahead of it (a zstd body is decoded first). `codex.rate_limits` and `codex.response.metadata`, read ahead of the reply, are also told as the `x-codex-*` and `x-codex-turn-state` headers the HTTP route carries; `responsesapi.websocket_timing` is dropped. An `error` frame ahead of the reply is that status with its error as the body; `websocket_connection_limit_reached`, a refused handshake or a body that isn't a streamed turn goes over HTTP instead, and a host that refused the handshake (other than 401, 429 or 5xx) is spoken HTTP for 10 minutes (`codexSocketShun`). A socket closed before the reply's end reads as a cut stream (`io.ErrUnexpectedEOF`). Codex still speaks HTTP to magpie: magpie answers its WebSocket handshake 426, and Codex falls back for the session; only the leg to the vendor changed. The gateway's tests turn it off in TestMain (`codexSocketOff`); codex_socket_test's fakes speak it.- A provider the user added may have its certificate left unchecked (`InsecureTLS`, the editor's Certificate row, #2383: a vLLM on the LAN, Ollama behind a self-signed cert, a company CA). Only one with a base URL of the user's own takes it (`TakesInsecureTLS`: no account, no preset, not Vertex, not a subscription's or plugin's id); `Save` refuses it on any other, and one written into providers.json by hand is ignored. `Provider.Via` then adds `netproxy.WithUncheckedTLS` with the hosts of its own base URLs (`UncheckedHosts`: Chat, Responses, Anthropic, Gemini, Decide), so every request made on its behalf through a `Dispatch` transport (the gateway's turns, count_tokens, models, test and detect, balance) skips the check for those hosts alone, on a transport kept apart for each (`route.unchecked`). A redirect or a models, balance or image URL on another host is checked, and `netproxy.With` drops the leave for a request made for another provider. An `https://` proxy would be dialled with the same TLS config, so its certificate would go unchecked too: such a request is refused (`ErrUncheckedOverHTTPSProxy`); an `http://` or `socks5://` proxy is taken. Backup, export and sync carry the field with the provider; an import link doesn't.- The Copilot account the editors and CLI keep is read by `copilotLogin` ([account.go](../../internal/provider/account.go)): an editor's github.com sign-in in `~/.config/github-copilot/apps.json` or `hosts.json` first, then the Copilot CLI's, then an editor's on GHE.com. When GitHub refuses the editor token's trade for a session token (401 or 403, `copilotRefusals`, retried after an hour), the CLI's sign-in of the same login on the same host takes its place, sent as the CLI sends it, from that request on and on the Usage card (`copilotStandIn`); a CLI signed in to another account is never used for it, and the error names the editors' file (#1238). A sign-in from magpie of the account whose editor token is refused is kept and stands for it (`copilotProbeEditor`, `addCopilotLogin`); while the editor token works, it is let go as the agent's own, as before.- A keychain read that may have shown the user a dialog is not asked again for ten minutes after it was refused (`keychainDeniedTTL` in [account.go](../../internal/provider/account.go)): Copilot's token read backs a denial off (`copilotCLISecret`), and the Cursor token (`cursorKeychainToken`) and Claude Code's item (`readClaudeKeychainItem`, through `claudeCredential`'s cache) do too (#2408). 44, the item not being there, is no refusal and keeps the short TTL, so a sign-in made in the agent is still seen within it; a sign-in or out in magpie reads afresh (`forgetCursorStatus`, `forgetClaudeCredential`), backoff or not.diff --git a/go.mod b/go.modindex 750d5010d..b54469913 100644--- a/go.mod+++ b/go.mod@@ -7,8 +7,10 @@ require (github.com/charmbracelet/bubbles v0.21.1github.com/charmbracelet/bubbletea v1.3.10github.com/charmbracelet/lipgloss v1.1.1-0.20250404203927-76690c660834+ github.com/coder/websocket v1.8.14github.com/go-ole/go-ole v1.3.0github.com/godbus/dbus/v5 v5.2.2+ github.com/google/uuid v1.6.0github.com/klauspost/compress v1.18.3github.com/pelletier/go-toml/v2 v2.2.4github.com/tidwall/gjson v1.19.0@@ -35,10 +37,8 @@ require (github.com/clipperhouse/displaywidth v0.9.0 // indirectgithub.com/clipperhouse/stringish v0.1.1 // indirectgithub.com/clipperhouse/uax29/v2 v2.5.0 // indirect- github.com/coder/websocket v1.8.14 // indirectgithub.com/dustin/go-humanize v1.0.1 // indirectgithub.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect- github.com/google/uuid v1.6.0 // indirectgithub.com/lucasb-eyer/go-colorful v1.3.0 // indirectgithub.com/mattn/go-colorable v0.1.14 // indirectgithub.com/mattn/go-isatty v0.0.20 // indirectdiff --git a/internal/gateway/codex_socket.go b/internal/gateway/codex_socket.gonew file mode 100644index 000000000..4a9263890--- /dev/null+++ b/internal/gateway/codex_socket.go@@ -0,0 +1,529 @@+package gateway++import (+ "bytes"+ "context"+ "encoding/json"+ "errors"+ "fmt"+ "io"+ "log"+ "net/http"+ "strconv"+ "strings"+ "sync"+ "sync/atomic"+ "time"++ "github.com/coder/websocket"+ "github.com/tidwall/gjson"++ "github.com/yetone/magpie/internal/provider"+)++// A streamed turn to the ChatGPT backend (CodexBase/responses) goes over+// the backend's WebSocket, as Codex's own client sends it, and comes back+// to magpie's caller as the server-sent events the HTTP route answers with.+// On some networks a streamed HTTP reply from chatgpt.com is cut at about+// 120 s, before response.completed, while the same turn over the WebSocket+// runs to its end (brickhao on Discord: 120.7 s through magpie, 121.5 s+// over HTTP without it, both cut; 124.5 s and 131.4 s over the WebSocket,+// both whole). Codex itself still speaks HTTP to magpie, over loopback:+// it is the leg from magpie to the vendor that was cut.+//+// What goes on the socket is what Codex 0.16x sends there (codex-rs+// ResponseCreateWsRequest, build_websocket_headers): the handshake carries+// the turn's headers with OpenAI-Beta: responses_websockets=2026-02-06 and+// neither x-codex-turn-state nor the responses-lite header, which go in+// the message's client_metadata instead; the message is the HTTP body with+// "type":"response.create" ahead of it. Every event comes back a frame;+// codex.rate_limits and codex.response.metadata, which the HTTP route says+// in headers, are told in headers too (x-codex-*, x-codex-turn-state).+//+// Anything the socket can't do falls back to the HTTP request as it was:+// a refused handshake (and that host is spoken HTTP for codexSocketShun),+// a connection limit, a body that isn't a streamed turn.++// codexSocketOff turns the socket off: the gateway's tests' fakes of the+// ChatGPT backend speak HTTP, so TestMain sets it, and codex_socket_test+// clears it.+var codexSocketOff atomic.Bool++// codexSocketBeta is the OpenAI-Beta Codex's WebSocket handshake carries.+const codexSocketBeta = "responses_websockets=2026-02-06"++// codexSocketShun is how long a host that refused the handshake is+// spoken HTTP before it is tried again.+var codexSocketShun = 10 * time.Minute++// codexSocketDial bounds the handshake, as Codex's own+// websocket_connect_timeout does.+var codexSocketDial = 30 * time.Second++// codexSocketPrelude is how many frames ahead of the reply proper are read+// for headers before the reply is handed on.+const codexSocketPrelude = 8++var (+ codexShunMu sync.Mutex+ codexShun = map[string]time.Time{}+)++func codexShunned(host string) bool {+ codexShunMu.Lock()+ defer codexShunMu.Unlock()+ until, ok := codexShun[host]+ if ok && time.Now().After(until) {+ delete(codexShun, host)+ return false+ }+ return ok+}++func shunCodexSocket(host string, why string) {+ codexShunMu.Lock()+ _, was := codexShun[host]+ codexShun[host] = time.Now().Add(codexSocketShun)+ codexShunMu.Unlock()+ if !was {+ log.Printf("codex: %s refused the WebSocket (%s); its turns go over HTTP for %s", host, why, codexSocketShun)+ }+}++// codexSocket sends a streamed turn to the ChatGPT backend over its+// WebSocket, and every other request through next.+type codexSocket struct{ next http.RoundTripper }++func (t codexSocket) RoundTrip(req *http.Request) (*http.Response, error) {+ if codexSocketOff.Load() || req.Method != http.MethodPost || req.GetBody == nil ||+ !provider.CodexTurnURL(req.URL) || codexShunned(req.URL.Host) {+ return t.next.RoundTrip(req)+ }+ msg, ok := codexSocketMessage(req)+ if !ok {+ return t.next.RoundTrip(req)+ }+ res, err := t.socket(req, msg)+ if errors.Is(err, errCodexSocketFallBack) {+ return t.next.RoundTrip(req)+ }+ return res, err+}++// errCodexSocketFallBack says the turn goes over HTTP after all.+var errCodexSocketFallBack = errors.New("codex: over HTTP")++// codexSocketMessage is req's turn as the socket's response.create+// message, and false for a body that isn't a streamed turn.+func codexSocketMessage(req *http.Request) ([]byte, bool) {+ rc, err := req.GetBody()+ if err != nil {+ return nil, false+ }+ defer rc.Close()+ plain := rc+ if enc := req.Header.Get("Content-Encoding"); enc != "" {+ if !strings.EqualFold(enc, "zstd") {+ return nil, false+ }+ d := &http.Request{Header: http.Header{"Content-Encoding": {enc}}, Body: rc}+ if plain, err = decodedBody(d); err != nil {+ return nil, false+ }+ defer plain.Close()+ }+ body, err := io.ReadAll(io.LimitReader(plain, defaultBodyLimit+1))+ if err != nil || len(body) > defaultBodyLimit {+ return nil, false+ }+ body = bytes.TrimSpace(body)+ if len(body) < 2 || body[0] != '{' || !gjson.ValidBytes(body) {+ return nil, false+ }+ if !gjson.GetBytes(body, "stream").Bool() || gjson.GetBytes(body, "type").Exists() {+ return nil, false+ }+ // what Codex says in a header over HTTP and in client_metadata here+ meta := map[string]string{}+ if v := req.Header.Get("X-Codex-Turn-State"); v != "" {+ meta["x-codex-turn-state"] = v+ }+ if strings.EqualFold(req.Header.Get("X-OpenAI-Internal-Codex-Responses-Lite"), "true") {+ meta["ws_request_header_x_openai_internal_codex_responses_lite"] = "true"+ }+ if len(meta) > 0 {+ if body, ok := withClientMetadata(body, meta); ok {+ return codexCreate(body), true+ }+ return nil, false+ }+ return codexCreate(body), true+}++// codexCreate is body, a JSON object, as a response.create message.+func codexCreate(body []byte) []byte {+ rest := bytes.TrimSpace(body[1:])+ out := make([]byte, 0, len(body)+32)+ out = append(out, `{"type":"response.create"`...)+ if len(rest) > 0 && rest[0] != '}' {+ out = append(out, ',')+ }+ return append(out, rest...)+}++// withClientMetadata is body with meta's keys set in its client_metadata,+// every other byte as it was.+func withClientMetadata(body []byte, meta map[string]string) ([]byte, bool) {+ cm := gjson.GetBytes(body, "client_metadata")+ have := map[string]any{}+ if cm.Exists() && cm.Type != gjson.Null {+ if !cm.IsObject() || json.Unmarshal([]byte(cm.Raw), &have) != nil {+ return nil, false+ }+ }+ for k, v := range meta {+ if _, ok := have[k]; !ok {+ have[k] = v+ }+ }+ raw, err := json.Marshal(have)+ if err != nil {+ return nil, false+ }+ if cm.Exists() && cm.Index > 0 {+ out := make([]byte, 0, len(body)+len(raw))+ out = append(out, body[:cm.Index]...)+ out = append(out, raw...)+ return append(out, body[cm.Index+len(cm.Raw):]...), true+ }+ end := bytes.LastIndexByte(body, '}')+ out := make([]byte, 0, len(body)+len(raw)+20)+ out = append(out, bytes.TrimRight(body[:end], " \t\r\n")...)+ if out[len(out)-1] != '{' {+ out = append(out, ',')+ }+ out = append(out, `"client_metadata":`...)+ out = append(out, raw...)+ return append(out, '}'), true+}++// codexSocketHeaders are req's headers as the handshake carries them.+func codexSocketHeaders(req *http.Request) http.Header {+ h := http.Header{}+ for k, vs := range req.Header {+ switch http.CanonicalHeaderKey(k) {+ case "Content-Type", "Content-Length", "Content-Encoding", "Accept", "Accept-Encoding",+ "X-Idempotency-Key", "Idempotency-Key", "X-Codex-Turn-State",+ "X-Openai-Internal-Codex-Responses-Lite", "Host", "Openai-Beta":+ continue+ }+ if hopHeader(k) || strings.HasPrefix(http.CanonicalHeaderKey(k), "Sec-Websocket-") {+ continue+ }+ h[k] = vs+ }+ h.Set("OpenAI-Beta", codexSocketBeta)+ return h+}++func (t codexSocket) socket(req *http.Request, msg []byte) (*http.Response, error) {+ ctx, cancel := context.WithCancel(req.Context())+ timer := time.AfterFunc(codexSocketDial, cancel)+ u := *req.URL+ c, hs, err := websocket.Dial(ctx, u.String(), &websocket.DialOptions{+ HTTPClient: &http.Client{Transport: t.next},+ HTTPHeader: codexSocketHeaders(req),+ CompressionMode: websocket.CompressionNoContextTakeover,+ })+ stopped := timer.Stop()+ if err != nil {+ cancel()+ if req.Context().Err() != nil {+ return nil, req.Context().Err()+ }+ switch {+ case hs == nil:+ shunCodexSocket(u.Host, err.Error())+ case hs.StatusCode == http.StatusUnauthorized || hs.StatusCode == http.StatusTooManyRequests ||+ hs.StatusCode >= 500:+ // the account's or a passing fault: HTTP says it as it is+ default:+ shunCodexSocket(u.Host, hs.Status)+ }+ return nil, errCodexSocketFallBack+ }+ if !stopped {+ c.CloseNow()+ cancel()+ return nil, errCodexSocketFallBack+ }+ c.SetReadLimit(-1)+ if err := c.Write(ctx, websocket.MessageText, msg); err != nil {+ c.CloseNow()+ cancel()+ if req.Context().Err() != nil {+ return nil, req.Context().Err()+ }+ return nil, fmt.Errorf("codex websocket: %w", err)+ }+ header := http.Header{}+ for k, vs := range hs.Header {+ switch http.CanonicalHeaderKey(k) {+ case "Connection", "Upgrade", "Content-Length", "Content-Type":+ continue+ }+ if strings.HasPrefix(http.CanonicalHeaderKey(k), "Sec-Websocket-") {+ continue+ }+ header[k] = vs+ }+ body := &codexSocketBody{c: c, ctx: ctx, cancel: cancel}+ for range codexSocketPrelude {+ frame, err := body.frame()+ if err != nil {+ body.Close()+ if req.Context().Err() != nil {+ return nil, req.Context().Err()+ }+ if errors.Is(err, io.EOF) && len(body.pending) == 0 {+ return nil, errors.New("codex websocket: closed before the reply began")+ }+ return nil, err+ }+ if frame == nil { // dropped+ continue+ }+ kind := gjson.GetBytes(frame, "type").String()+ switch kind {+ case "codex.rate_limits":+ codexLimitHeaders(header, frame)+ case "codex.response.metadata":+ gjson.GetBytes(frame, "headers").ForEach(func(k, v gjson.Result) bool {+ if header.Get(k.String()) == "" {+ header.Set(k.String(), v.String())+ }+ return true+ })+ case "error":+ // ahead of the reply: it stands for the HTTP route's status+ if res, fall := codexSocketError(req, header, frame); fall || res != nil {+ body.Close()+ if fall {+ return nil, errCodexSocketFallBack+ }+ return res, nil+ }+ }+ body.pending = append(body.pending, sseEvent(kind, frame)...)+ if kind != "codex.rate_limits" && kind != "codex.response.metadata" {+ break+ }+ }+ return &http.Response{+ Status: "200 OK",+ StatusCode: http.StatusOK,+ Proto: "HTTP/1.1",+ ProtoMajor: 1,+ ProtoMinor: 1,+ Header: header,+ Body: body,+ ContentLength: -1,+ Request: req,+ }, nil+}++// codexSocketError is the HTTP reply an error frame that comes ahead of+// the reply stands for, and true when the turn is to go over HTTP+// instead; neither for one the stream carries on with.+func codexSocketError(req *http.Request, header http.Header, frame []byte) (*http.Response, bool) {+ code := gjson.GetBytes(frame, "error.code").String()+ if code == "websocket_connection_limit_reached" {+ return nil, true+ }+ status := int(gjson.GetBytes(frame, "status").Int())+ if status == 0 {+ status = int(gjson.GetBytes(frame, "status_code").Int())+ }+ if status < 400 || status > 599 {+ return nil, false+ }+ h := header.Clone()+ for _, path := range []string{"headers", "error.headers"} {+ gjson.GetBytes(frame, path).ForEach(func(k, v gjson.Result) bool {+ h.Set(k.String(), v.String())+ return true+ })+ }+ h.Set("Content-Type", "application/json")+ var b []byte+ if e := gjson.GetBytes(frame, "error"); e.IsObject() {+ b = []byte(`{"error":` + e.Raw + `}`)+ } else {+ b = frame+ }+ return &http.Response{+ Status: fmt.Sprintf("%d %s", status, http.StatusText(status)),+ StatusCode: status,+ Proto: "HTTP/1.1",+ ProtoMajor: 1,+ ProtoMinor: 1,+ Header: h,+ Body: io.NopCloser(bytes.NewReader(b)),+ ContentLength: int64(len(b)),+ Request: req,+ }, false+}++// codexLimitHeaders tells a codex.rate_limits event in the x-codex-*+// headers the HTTP route carries (codex-api's parse_rate_limit_for_limit),+// each only where the handshake didn't say it already.+func codexLimitHeaders(h http.Header, frame []byte) {+ ev := gjson.ParseBytes(frame)+ name := ev.Get("metered_limit_name").String()+ if name == "" {+ name = ev.Get("limit_name").String()+ }+ name = strings.ReplaceAll(strings.ToLower(strings.TrimSpace(name)), "_", "-")+ if name == "" {+ name = "codex"+ }+ set := func(k, v string) {+ if v != "" && h.Get(k) == "" {+ h.Set(k, v)+ }+ }+ num := func(r gjson.Result) string {+ if !r.Exists() || r.Type != gjson.Number {+ return ""+ }+ return strconv.FormatFloat(r.Float(), 'f', -1, 64)+ }+ if p := ev.Get("plan_type").String(); p != "" {+ set("x-codex-plan-type", p)+ }+ for _, which := range []string{"primary", "secondary"} {+ w := ev.Get("rate_limits." + which)+ if !w.IsObject() {+ continue+ }+ pre := "x-" + name + "-" + which + "-"+ set(pre+"used-percent", num(w.Get("used_percent")))+ set(pre+"window-minutes", num(w.Get("window_minutes")))+ set(pre+"reset-at", num(w.Get("reset_at")))+ set(pre+"reset-after-seconds", num(w.Get("reset_after_seconds")))+ }+ if cr := ev.Get("credits"); cr.IsObject() {+ tf := func(r gjson.Result) string {+ if r.Type == gjson.True {+ return "True"+ }+ if r.Type == gjson.False {+ return "False"+ }+ return ""+ }+ set("x-codex-credits-has-credits", tf(cr.Get("has_credits")))+ set("x-codex-credits-unlimited", tf(cr.Get("unlimited")))+ if b := cr.Get("balance"); b.Exists() && b.Type != gjson.Null {+ set("x-codex-credits-balance", b.String())+ }+ }+}++// sseEvent is a frame as the HTTP route sends it.+func sseEvent(kind string, frame []byte) []byte {+ if bytes.ContainsAny(frame, "\r\n") {+ var b bytes.Buffer+ if json.Compact(&b, frame) == nil {+ frame = b.Bytes()+ }+ }+ out := make([]byte, 0, len(kind)+len(frame)+18)+ if kind != "" {+ out = append(out, "event: "...)+ out = append(out, kind...)+ out = append(out, '\n')+ }+ out = append(out, "data: "...)+ out = append(out, frame...)+ return append(out, "\n\n"...)+}++// codexSocketBody is the socket's frames read as server-sent events.+type codexSocketBody struct {+ c *websocket.Conn+ ctx context.Context+ cancel context.CancelFunc+ pending []byte+ done bool+ err error+ closed atomic.Bool+}++// frame is the socket's next frame, nil for one the reply leaves out.+func (b *codexSocketBody) frame() ([]byte, error) {+ if b.done {+ return nil, io.EOF+ }+ typ, data, err := b.c.Read(b.ctx)+ if err != nil {+ b.done = true+ if websocket.CloseStatus(err) == websocket.StatusNormalClosure {+ return nil, io.EOF+ }+ if b.ctx.Err() != nil && b.closed.Load() {+ return nil, io.ErrClosedPipe+ }+ return nil, fmt.Errorf("codex websocket: %w", err)+ }+ if typ != websocket.MessageText {+ return nil, nil+ }+ data = bytes.TrimSpace(data)+ switch gjson.GetBytes(data, "type").String() {+ case "responsesapi.websocket_timing":+ return nil, nil+ case "response.completed", "response.failed", "response.incomplete", "response.done", "error":+ b.done = true+ go b.c.Close(websocket.StatusNormalClosure, "")+ }+ return data, nil+}++func (b *codexSocketBody) Read(p []byte) (int, error) {+ for len(b.pending) == 0 {+ if b.err != nil {+ return 0, b.err+ }+ if b.done {+ return 0, io.EOF+ }+ frame, err := b.frame()+ if err != nil {+ if errors.Is(err, io.EOF) {+ // the backend ended the socket before the reply's end:+ // the stream is cut, as an HTTP reply cut short is+ err = io.ErrUnexpectedEOF+ }+ b.err = err+ continue+ }+ if frame != nil {+ b.pending = sseEvent(gjson.GetBytes(frame, "type").String(), frame)+ }+ }+ n := copy(p, b.pending)+ b.pending = b.pending[n:]+ return n, nil+}++func (b *codexSocketBody) Close() error {+ if b.closed.Swap(true) {+ return nil+ }+ if !b.done {+ b.c.CloseNow()+ }+ b.cancel()+ return nil+}diff --git a/internal/gateway/codex_socket_test.go b/internal/gateway/codex_socket_test.gonew file mode 100644index 000000000..b91027270--- /dev/null+++ b/internal/gateway/codex_socket_test.go@@ -0,0 +1,300 @@+package gateway++import (+ "errors"+ "io"+ "net/http"+ "net/http/httptest"+ "strings"+ "sync"+ "sync/atomic"+ "testing"++ "github.com/coder/websocket"+ "github.com/tidwall/gjson"++ "github.com/yetone/magpie/internal/provider"+)++// socketTurn is what a fake ChatGPT backend saw of one turn.+type socketTurn struct {+ header http.Header+ msg []byte+}++// fakeSocketBackend stands in for the ChatGPT backend: a turn over its+// WebSocket is answered with frames(the turn); one over HTTP with a reply+// cut before response.completed, as brickhao's network cut every streamed+// HTTP reply from chatgpt.com at about 120 s. refuse answers the+// handshake with that status instead.+type fakeSocketBackend struct {+ mu sync.Mutex+ sockets []socketTurn+ posts int+ refuse int+ frames func(socketTurn) []string+}++func (f *fakeSocketBackend) ServeHTTP(w http.ResponseWriter, r *http.Request) {+ if !strings.HasSuffix(r.URL.Path, "/backend-api/codex/responses") {+ http.NotFound(w, r)+ return+ }+ if strings.EqualFold(r.Header.Get("Upgrade"), "websocket") {+ if f.refuse != 0 {+ http.Error(w, "no sockets here", f.refuse)+ return+ }+ c, err := websocket.Accept(w, r, nil)+ if err != nil {+ return+ }+ defer c.CloseNow()+ c.SetReadLimit(-1)+ _, msg, err := c.Read(r.Context())+ if err != nil {+ return+ }+ turn := socketTurn{header: r.Header.Clone(), msg: msg}+ f.mu.Lock()+ f.sockets = append(f.sockets, turn)+ f.mu.Unlock()+ for _, fr := range f.frames(turn) {+ if c.Write(r.Context(), websocket.MessageText, []byte(fr)) != nil {+ return+ }+ }+ c.Close(websocket.StatusNormalClosure, "")+ return+ }+ io.ReadAll(r.Body)+ f.mu.Lock()+ f.posts+++ f.mu.Unlock()+ w.Header()["Content-Type"] = nil+ io.WriteString(w, sse(+ `event: response.created`+"\n"+`data: {"type":"response.created","response":{"id":"r1"}}`,+ `event: response.output_text.delta`+"\n"+`data: {"type":"response.output_text.delta","delta":"po"}`))+ // cut here, before response.completed+}++// counts are the turns seen over HTTP and over the socket.+func (f *fakeSocketBackend) counts() (posts, sockets int) {+ f.mu.Lock()+ defer f.mu.Unlock()+ return f.posts, len(f.sockets)+}++func socketOn(t *testing.T) {+ t.Helper()+ codexSocketOff.Store(false)+ t.Cleanup(func() {+ codexSocketOff.Store(true)+ codexShunMu.Lock()+ clear(codexShun)+ codexShunMu.Unlock()+ })+}++// a whole reply, as the backend sends it over its socket+func wholeReply(socketTurn) []string {+ return []string{+ `{"type":"codex.rate_limits","plan_type":"plus","rate_limits":{"allowed":true,"limit_reached":false,"primary":{"used_percent":42,"window_minutes":300,"reset_after_seconds":600,"reset_at":1792000000},"secondary":{"used_percent":7.5,"window_minutes":10080,"reset_after_seconds":6000,"reset_at":1792500000}},"credits":{"has_credits":false,"unlimited":false,"balance":"0"}}`,+ `{"type":"codex.response.metadata","headers":{"x-codex-turn-state":"ts-1","x-models-etag":"W/\"e9\""}}`,+ `{"type":"response.created","response":{"id":"r1","model":"gpt-5.5"}}`,+ `{"type":"responsesapi.websocket_timing","timing":{"ms":3}}`,+ "{\"type\":\"response.output_text.delta\",\n\"delta\":\"pong\"}",+ `{"type":"response.completed","response":{"id":"r1","usage":{"input_tokens":7,"output_tokens":1}}}`,+ }+}++// brickhao on Discord: a Codex turn through magpie that streams past+// about 120 s was cut ("stream closed before response.completed") on a+// network that cuts chatgpt.com's long HTTP replies, while Codex's own+// WebSocket to the same backend ran to the end. magpie now sends a+// streamed turn over the backend's WebSocket, as Codex does, and hands+// Codex the same events, and the headers the HTTP route says, over HTTP.+func TestCodexTurnGoesOverWebSocket(t *testing.T) {+ socketOn(t)+ defer provider.CodexZstdForTests(true)() // as the real turn goes+ f := &fakeSocketBackend{frames: wholeReply}+ chatgpt(t, f.ServeHTTP)++ rec := httptest.NewRecorder()+ req := httptest.NewRequest("POST", CodexPath+"/responses", strings.NewReader(+ `{"model":"gpt-5.5","stream":true,"input":"ping","client_metadata":{"session_id":"s1"}}`))+ req.Header.Set("Authorization", "Bearer chatgpt-token")+ req.Header.Set("chatgpt-account-id", "acct-1")+ req.Header.Set("Accept", "text/event-stream")+ req.Header.Set("X-Codex-Turn-State", "ts-0")+ req.Header.Set("X-OpenAI-Internal-Codex-Responses-Lite", "true")+ New().Handler().ServeHTTP(rec, req)++ body := rec.Body.String()+ if rec.Code != 200 || !strings.Contains(body, "pong") || !strings.Contains(body, "event: response.completed\ndata: ") {+ t.Fatalf("%d, a reply without its end: %s", rec.Code, body)+ }+ if strings.Contains(body, "websocket_timing") {+ t.Errorf("the socket's timing frame reached Codex: %s", body)+ }+ if posts, sockets := f.counts(); posts != 0 || sockets != 1 {+ t.Fatalf("%d HTTP posts, %d sockets; want the turn on one socket", posts, sockets)+ }+ for k, want := range map[string]string{+ "x-codex-turn-state": "ts-1",+ "x-codex-primary-used-percent": "42",+ "x-codex-primary-window-minutes": "300",+ "x-codex-primary-reset-at": "1792000000",+ "x-codex-secondary-used-percent": "7.5",+ "x-codex-plan-type": "plus",+ "x-codex-credits-has-credits": "False",+ } {+ if got := rec.Header().Get(k); got != want {+ t.Errorf("%s: %q, want %q", k, got, want)+ }+ }+ // magpie's own models are added to the backend's etag+ if got := rec.Header().Get("x-models-etag"); !strings.HasPrefix(got, `W/"e9`) {+ t.Errorf("x-models-etag %q, not the backend's", got)+ }+ turn := f.sockets[0]+ if got := turn.header.Get("OpenAI-Beta"); got != codexSocketBeta {+ t.Errorf("OpenAI-Beta %q", got)+ }+ if turn.header.Get("Authorization") != "Bearer chatgpt-token" || turn.header.Get("chatgpt-account-id") != "acct-1" {+ t.Errorf("handshake lost the sign-in: %v", turn.header)+ }+ for _, k := range []string{"X-Codex-Turn-State", "X-OpenAI-Internal-Codex-Responses-Lite", "Content-Encoding", "Accept"} {+ if v := turn.header.Get(k); v != "" {+ t.Errorf("handshake carries %s: %q", k, v)+ }+ }+ m := gjson.ParseBytes(turn.msg)+ if m.Get("type").String() != "response.create" || !strings.HasPrefix(string(turn.msg), `{"type":"response.create",`) ||+ m.Get("model").String() != "gpt-5.5" || !m.Get("stream").Bool() || m.Get("input").String() != "ping" {+ t.Errorf("message %s", turn.msg)+ }+ if m.Get("client_metadata.session_id").String() != "s1" ||+ m.Get("client_metadata.x-codex-turn-state").String() != "ts-0" ||+ m.Get("client_metadata.ws_request_header_x_openai_internal_codex_responses_lite").String() != "true" {+ t.Errorf("client_metadata %s", m.Get("client_metadata").Raw)+ }+}++// A backend that won't take the socket is asked over HTTP, the same turn,+// and isn't asked for a socket again for a while.+func TestCodexSocketRefusedGoesOverHTTP(t *testing.T) {+ socketOn(t)+ f := &fakeSocketBackend{refuse: http.StatusForbidden}+ chatgpt(t, f.ServeHTTP)+ var handshakes atomic.Int32+ up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if r.Header.Get("Upgrade") != "" {+ handshakes.Add(1)+ }+ f.ServeHTTP(w, r)+ }))+ t.Cleanup(up.Close)+ provider.CodexBase = up.URL + "/backend-api/codex"+ for i := range 2 {+ code, body := codexPost(t, `{"model":"gpt-5.5","stream":true,"input":"ping"}`)+ if code != 200 || !strings.Contains(body, `"delta":"po"`) {+ t.Fatalf("turn %d: %d %s", i, code, body)+ }+ }+ if posts, _ := f.counts(); posts != 2 || handshakes.Load() != 1 {+ t.Fatalf("%d posts, %d handshakes; want 2 posts and one handshake", posts, handshakes.Load())+ }+}++// An error the backend says on the socket before the reply is the status+// it would have answered over HTTP: a used-up account (429) rests and the+// turn goes on to the next account, whose turn goes over the socket too.+func TestCodexSocketErrorIsItsStatus(t *testing.T) {+ codexSignedIn(t, "[email protected]")+ socketOn(t)+ var tried []string // under f.mu+ var f *fakeSocketBackend+ f = &fakeSocketBackend{frames: func(turn socketTurn) []string {+ f.mu.Lock()+ tried = append(tried, turn.header.Get("chatgpt-account-id"))+ f.mu.Unlock()+ if turn.header.Get("chatgpt-account-id") == "acct-1" {+ return []string{`{"type":"error","status":429,"error":{"type":"usage_limit_reached","message":"The usage limit has been reached","plan_type":"plus","resets_in_seconds":7200},"headers":{"x-codex-primary-used-percent":"100"}}`}+ }+ return wholeReply(turn)+ }}+ up := httptest.NewServer(f)+ t.Cleanup(up.Close)+ was := provider.CodexBase+ provider.CodexBase = up.URL + "/backend-api/codex"+ t.Cleanup(func() { provider.CodexBase = was })++ code, body := codexPost(t, `{"model":"gpt-5.5","stream":true,"input":"ping"}`)+ if code != 200 || !strings.Contains(body, "pong") || !strings.Contains(body, "response.completed") {+ t.Fatalf("%d %s", code, body)+ }+ posts, _ := f.counts()+ f.mu.Lock()+ got := strings.Join(tried, ",")+ f.mu.Unlock()+ if got != "acct-1,acct-2" || posts != 0 {+ t.Fatalf("tried %v over the socket, %d posts", got, posts)+ }+ // the reply the backend's transport hands on is that status+ req, _ := http.NewRequest("POST", provider.CodexBase+"/responses", strings.NewReader(`{"model":"gpt-5.5","stream":true,"input":"ping"}`))+ req.Header.Set("chatgpt-account-id", "acct-1")+ res, err := New().client.Do(req)+ if err != nil {+ t.Fatal(err)+ }+ b, _ := io.ReadAll(res.Body)+ res.Body.Close()+ if res.StatusCode != 429 || gjson.GetBytes(b, "error.type").String() != "usage_limit_reached" ||+ res.Header.Get("x-codex-primary-used-percent") != "100" {+ t.Errorf("%d %v %s; want the frame's 429, its headers and error", res.StatusCode, res.Header, b)+ }+ restingUntil.Lock()+ n := len(restingUntil.m)+ restingUntil.Unlock()+ if n == 0 {+ t.Error("the used-up account doesn't rest")+ }+}++// A socket the backend closes before the reply's end is a cut stream,+// not a whole one.+func TestCodexSocketClosedEarlyIsCut(t *testing.T) {+ socketOn(t)+ f := &fakeSocketBackend{frames: func(socketTurn) []string {+ return wholeReply(socketTurn{})[:5]+ }}+ chatgpt(t, f.ServeHTTP)+ res, err := New().client.Post(provider.CodexBase+"/responses", "application/json",+ strings.NewReader(`{"model":"gpt-5.5","stream":true,"input":"ping"}`))+ if err != nil {+ t.Fatal(err)+ }+ defer res.Body.Close()+ b, err := io.ReadAll(res.Body)+ if !errors.Is(err, io.ErrUnexpectedEOF) || !strings.Contains(string(b), "pong") {+ t.Fatalf("read %q, %v; want the reply so far and a cut", b, err)+ }+}++// A request that isn't a streamed turn goes over HTTP as before.+func TestCodexSocketOnlyForStreamedTurns(t *testing.T) {+ socketOn(t)+ f := &fakeSocketBackend{frames: wholeReply}+ chatgpt(t, f.ServeHTTP)+ res, err := New().client.Post(provider.CodexBase+"/responses", "application/json",+ strings.NewReader(`{"model":"gpt-5.5","stream":false,"input":"ping"}`))+ if err != nil {+ t.Fatal(err)+ }+ io.ReadAll(res.Body)+ res.Body.Close()+ if posts, sockets := f.counts(); posts != 1 || sockets != 0 {+ t.Fatalf("%d posts, %d sockets", posts, sockets)+ }+}diff --git a/internal/gateway/main_test.go b/internal/gateway/main_test.goindex 1ad3c7a7c..fc2801fe1 100644--- a/internal/gateway/main_test.go+++ b/internal/gateway/main_test.go@@ -82,6 +82,8 @@ func isolatedTests(m *testing.M) (int, error) {// The fakes of ChatGPT's backend read turns plain; codex_zstd_test's// send them zstd, as the real one is sent.provider.CodexZstdForTests(false)+ // and speak HTTP: codex_socket_test's fakes speak the WebSocket+ codexSocketOff.Store(true)// Finding an inert CLI also prevents discovery from falling back to// one installed at an absolute system path. Tests can prepend their// own fakes, as fakeClaude does, and still use ordinary shell tools.diff --git a/internal/gateway/upstream_transport.go b/internal/gateway/upstream_transport.goindex 8bacd8337..91a3c76b4 100644--- a/internal/gateway/upstream_transport.go+++ b/internal/gateway/upstream_transport.go@@ -14,7 +14,8 @@ var upstreamTLS *tls.Config// upstreamTransport is the gateway's transport to the vendors: a provider// with a proxy of its own is sent through a transport kept for that proxy-// (#237), and the ChatGPT backend over HTTP/1.1 (netproxy.HTTP1). Every+// (#237), and the ChatGPT backend over HTTP/1.1 (netproxy.HTTP1), a+// streamed turn to it over its WebSocket (codexSocket). Every// other vendor is spoken HTTP/2 to wherever it offers it. A request that// finds its kept-alive connection dropped is sent again on a new one// (replayable). A vendor is given upstreamHeaderWait to begin answering,@@ -23,7 +24,7 @@ var upstreamTLS *tls.Config// Image generation timeout gives it (drawWithin), up to 60 minutes.func upstreamTransport() http.RoundTripper {return drawsApart{- any: replayable{netproxy.Dispatch(vendorTransport(upstreamHeaderWait))},+ any: codexSocket{replayable{netproxy.Dispatch(vendorTransport(upstreamHeaderWait))}},draws: replayable{netproxy.Dispatch(vendorTransport(0))},}}diff --git a/internal/provider/codex_zstd.go b/internal/provider/codex_zstd.goindex 2ca1d7256..1f2e7371d 100644--- a/internal/provider/codex_zstd.go+++ b/internal/provider/codex_zstd.go@@ -90,3 +90,7 @@ func codexTurnURL(u *url.URL) bool {base, err := url.Parse(CodexBase)return err == nil && u != nil && strings.EqualFold(u.Host, base.Host) && u.Path == base.Path+"/responses"}++// CodexTurnURL says whether u is CodexBase's /responses, where Codex sends+// a turn.+func CodexTurnURL(u *url.URL) bool { return codexTurnURL(u) }