ac4af582301a4e8a29245dbba767eba31334ddf9
5
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
c056b7c8c3 |
provider: a Responses provider's turns may go over the Responses WebSocket (#5781)
A provider the user added with an http(s) Responses address can now send its Responses turns over the Responses WebSocket that magpie already speaks to chatgpt.com (codexSocket), with `socket=auto|on|off`: - off, or absent: today's behaviour exactly. Provider.Via hides any socket value an outer context carried, so such a provider never dials. - auto: tries the socket, falls back to HTTP when the handshake is refused or the socket ends before the reply, and speaks HTTP to a host for 10 minutes once it refused the handshake (codexShun). - on: the same fallback, but never shuns; the socket is tried each turn. Provider.Via puts the provider's Responses host and path in the request's context (withSocket); codexSocket's gate is provider.SocketTurn, which takes a ChatGPT turn as before, or a POST to that host at <Responses>/responses. The rest is the ChatGPT leg's: the kept socket and its delta (codexKeepKey now also hashes the scheme, X-Api-Key and Api-Key), the error frames, 1009 handling. A provider's reply with no Content-Type is marked text/event-stream, since it is read as one. Routing, keys, limits, usage and masking are unchanged: only the transport under the request differs. Set with `magpie provider set <id> socket=auto|on|off` (refused on a provider with an account, no http(s) Responses address, or Vertex, TakesSocket), saved in providers.json as "socket", shown by `provider show`, and in the editor's Advanced as a WebSocket tick beside HTTP version (ticked saves auto, keeps a CLI's on, unticked saves off; shown only while the Responses address is http(s) and there is no account). docs/subsystems/providers-accounts.md describes it. app.js pays for its rows by three long comments cut to one line (renderMove, the vendors' status pages, header rows): the core's scripts are 478 bytes smaller than on |
||
|
|
dec791f849 |
gateway: a Codex conversation's next turn goes on its kept socket as a delta
rem_thank on X: Codex sends a turn on its kept WebSocket as only its new input after previous_response_id, while magpie opened a socket per turn to chatgpt.com and sent the whole conversation, which a long one meets as 1009 (message too big). A conversation's socket is now kept between turns (codex_socket_keep.go), as codex-rs's WebsocketSession and get_incremental_items do it: - kept for a turn that names its conversation (session-id/thread-id, else prompt_cache_key) and ended response.completed with an id, under a key of host, path, a digest of Authorization and chatgpt-account-id, the conversation ids and the subagent headers, so another account's turn never finds the socket nor names its response; - taken out of the pool by the next turn, so two turns at once each have a socket and the last to end is kept; - let go after 10 min idle, at 55 min from the handshake (the backend ends at 60), and the oldest past 32; the closes run outside the pool's lock, and no lock is held across a read; - the turn goes as a delta when every field but input, client_metadata and stream_options is as the last turn sent it and its input starts with the last input and then that turn's output_item.done items (compared without id, status, nulls, empty lists, annotations, logprobs); anything else goes whole on the kept socket. It is compared on the body as it goes, after masking, so a turn masked otherwise goes whole; - a kept socket that fails before the reply is handed on (closed while kept, the write fails, an error frame first such as previous_response_not_found or the connection limit) is closed and the turn goes whole on a new socket, and from there over HTTP as before. The socket's dial context is detached from the request (the handshake alone ends with it) and cancelled when the socket closes. The gateway's tests drain the pool in socketOn's cleanup. Tests (codex_socket_keep_test.go): the delta on one socket and an edited history or another model whole on it; previous_response_not_found asked again whole on a new socket; a socket closed meanwhile; another account gets its own socket; idle let-go and no keep without a conversation; two turns at once take the kept socket once; a turn after another reply goes whole. Run (first at origin/main |
||
|
|
c57a5f7f58 |
gateway: a vendor's redirect to another origin is sent no provider key
Go's http.Client follows up to 10 redirects to any host, https to http included, and on a hop to another domain drops only Authorization, Cookie and WWW-Authenticate. So a vendor (or anything answering at its URL) that redirected elsewhere got x-api-key, x-goog-api-key, api-key and a provider's own headers sent there; Authorization too when only the port differed, and all of them in the clear on an https to http hop on the same host. The class is fixed where the requests leave: - provider.KeepsCredentials is a CheckRedirect that follows a redirect only to the request's own origin (scheme, host, port; or http to https on the same host and default ports), at most 10, and gives any other 3xx back as the answer (http.ErrUseLastResponse). - provider.Keyed(c) is a copy of c with c's own CheckRedirect, then KeepsCredentials. Provider.do, the choke point of every p.Do, sends through it, and every http.DefaultClient.Do in internal/provider that carries a credential is Keyed(http.DefaultClient).Do. A copy per call, since netproxy.Install sets the default client's transport at run time. Left alone: Grok's public download, Antigravity's version URL and cline.go's unsigned read. - the gateway's client (s.client, every vendor call), the Codex relay's and the Codex websocket's take KeepsCredentials, and so does chatgpt_api's siwcClient. The native route needs nothing more: the vendor's 3xx comes back to the caller as it came, Location included, and the caller decides. Translated routes don't follow either, rather than following with credentials stripped: it is the rule search_api.go had already (#1580), no vendor magpie speaks to was found redirecting its API to another origin, a stripped request elsewhere would only fail with a confusing 401, and the vendor-URL downloads (video, images) are fetched unsigned through vendorClient, so they are not affected. Tests: TestRedirectKeepsTheKeyAtItsOrigin (307 and 302 elsewhere with Authorization, x-api-key, x-goog-api-key, api-key and a custom header; a same-origin redirect followed with every key; three clients), TestRedirectNeverDowngrades (an https vendor's redirect to http on its own host and port), TestSameOrigin, TestKeyedKeepsTheClientsRule, TestNativeRedirectKeepsTheKey (Anthropic and Gemini providers with an own header) and TestChatRedirectKeepsTheKey. Broken by hand: with Provider.do back on client.Do and s.client without CheckRedirect, the thief got every key header, /away answered 200, the https vendor's /plain was followed (400 from the TLS port spoken to in http), the native route's claudeish and gem answered the thief's {"stolen":true} with Authorization, X-Api-Key and X-Relay-Secret sent there, and the chat route sent Api-Key, Authorization and X-Relay-Secret to it. TestSameOrigin and TestKeyedKeepsTheClientsRule don't depend on the wiring and passed. Run on mote at the pushed head 883690a0b (this commit and the next, on origin/main |
||
|
|
3e9efe1d51 |
gateway: a Codex turn chatgpt.com's WebSocket refuses as too big goes over HTTP (Lamrfu)
Lamrfu on Discord, after updating: the Codex desktop app through magpie
kept showing "正在重新连接 2/5" with
unexpected status 502 Bad Gateway: Codex: Post ".../backend-api/codex/responses":
codex websocket: failed to get reader: received close frame:
status = StatusMessageTooBig and reason = ""
Cause.
|
||
|
|
a2e98cd799 |
gateway: a streamed Codex turn goes to ChatGPT over its WebSocket (brickhao)
brickhao on Discord: long Codex turns through magpie died with "stream
closed before response.completed". Their network (FlClash) cuts a
streamed HTTP reply from chatgpt.com at about 120 s: 120.7 s through
magpie, 121.5 s over HTTP without magpie. Codex's own WebSocket to the
same backend ran to the end twice (124.5 s, 131.4 s). magpie has no
120 s timer of its own (no WriteTimeout, 10 min to the first header).
The cut is on the leg from magpie to chatgpt.com, which was always
HTTP. Codex answers magpie's 426 by falling back to HTTP, and over
loopback that is harmless.
What changed: codexSocket, first in upstreamTransport, sends a POST to
CodexBase/responses with "stream":true over the backend's WebSocket, as
codex-rs does (ResponseCreateWsRequest, build_websocket_headers).
- Handshake: the turn's headers plus OpenAI-Beta:
responses_websockets=2026-02-06. x-codex-turn-state and the
responses-lite header go in client_metadata instead. The message is
the body (zstd decoded) with "type":"response.create" ahead of it.
- Frames come back as the SSE the HTTP route sends.
codex.rate_limits and codex.response.metadata are also given as the
x-codex-* and x-codex-turn-state headers. websocket_timing is dropped.
- An error frame ahead of the reply becomes its HTTP status and body,
so account rests and failover work as before.
- These go over HTTP: a refused handshake (and the host stays on HTTP
for 10 min, unless the refusal was 401/429/5xx),
websocket_connection_limit_reached, and a body that isn't a streamed
turn.
- A socket closed before the end reads as a cut stream.
- Both Codex's own sign-in passthrough and magpie's ChatGPT accounts
take this path. Codex identity, instructions and prompts are
unchanged. coder/websocket is now a direct dependency (it was
indirect).
Run:
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows ok.
- go test -race -count=3 of the five new tests: ok.
- go test -timeout 45m at 1a5911612 (rebased on origin/main
|