gateway: a Responses WebSocket turn streams without the HTTP stream field (#5291)

OpenAI's WebSocket mode doesn't use the HTTP stream field: a
response.create leaves it out and is streamed all the same. magpie made
each frame into the HTTP body its route would have had and kept the
field as the frame said it, so a client that left it out (M4n5ter on X,
AnacondaKC's Niko in #5291) got the provider's reply unstreamed and
every frame of it at the end. Codex sends stream:true itself, so it
never showed there.

turn now sets stream:true on every generating turn, whatever the frame
says (left out or false). It is the one place the three socket routes
(/v1/responses, /responses and Codex's backend route) make their body,
so all three are covered; a warmup (generate false) still answers
locally before it, and HTTP POSTs are unchanged.

Ported from AnacondaKC's patch in #5291 (62e3fd1ad), with these
differences: the test runs on /v1/responses and /responses, with stream
left out and with stream:false, checks the body sent upstream, and
TestCodexSocketStreamsWithoutStreamField covers Codex's route.

Tests: TestResponsesSocketStreamsWithoutStreamField and
TestCodexSocketStreamsWithoutStreamField, both red with responses_ws.go
at origin/main 8ed2ae030 ("no text delta before the upstream
completed"; "sent to ChatGPT unstreamed"), green with the change (on
mica). go vet ./internal/gateway for darwin, linux (nogui) and windows
on mote; gofmt -l clean.

Full suite (go test -tags nogui -count=1 -timeout 45m ./...) runs on
mica after the push; its result goes on #5291.

Not tried: a real client (Niko or the reporter's) against a real
provider.
This commit is contained in:
yetone
2026-11-14 11:05:49 +08:00
parent 8ed2ae030c
commit e0889719c8
3 changed files with 115 additions and 1 deletions
+6 -1
View File
@@ -2917,7 +2917,12 @@ serves it on `/v1/responses`, `/responses` and the Codex backend's
stands for: the upgrade's own request (URL, Host, address, headers but
the socket's own and `OpenAI-Beta: responses_websockets`) with the frame
as its body, `type`, `previous_response_id` and `generate` taken out, and
`client_metadata`'s turn state and responses-lite flag as the
`stream: true` set on the internal HTTP request. OpenAI's
[WebSocket mode](https://developers.openai.com/api/docs/guides/websocket-mode)
does not use the HTTP `stream` field: a client omitting it still gets
incremental events before the reply completes
(`TestResponsesSocketStreamsWithoutStreamField`).
`client_metadata`'s turn state and responses-lite flag become the
`X-Codex-Turn-State` and `X-OpenAI-Internal-Codex-Responses-Lite`
headers Codex sends over HTTP. It goes through the whole handler the
upgrade came in on (`socketEntry`, outside `Front` and `Handler`), so
+2
View File
@@ -492,6 +492,8 @@ func (rs *responsesSocket) turn(ctx context.Context, t *socketTurnRun, frame []b
delete(q, "type")
delete(q, "previous_response_id")
delete(q, "generate")
// WebSocket mode is always streamed; clients omit the HTTP stream field.
q["stream"] = json.RawMessage("true")
body, err := turnBody(q, input)
if err != nil {
rs.writeError(ctx, http.StatusBadRequest, "invalid_request_error", "invalid_json", err.Error(), "")
+107
View File
@@ -1,6 +1,7 @@
package gateway
import (
"cmp"
"context"
"encoding/json"
"fmt"
@@ -179,6 +180,112 @@ func create(prev, input string, extra ...string) string {
return f + "}"
}
// A response.create streams whatever it says of stream (#5291):
// OpenAI's WebSocket mode doesn't use the HTTP field, so a client leaves
// it out, and the turn went to the provider unstreamed, its reply coming
// back whole at the end. The provider here holds its reply open after
// the first delta, which must reach the client before it finishes.
func TestResponsesSocketStreamsWithoutStreamField(t *testing.T) {
fresh(t)
for _, path := range []string{"/v1/responses", "/responses"} {
for _, stream := range []string{"", `"stream":false,`} {
name := path + " " + cmp.Or(stream, "no stream")
finish := make(chan struct{}, 1)
u := &respUp{}
u.reply = func(w http.ResponseWriter, r *http.Request, n int) {
item := `{"type":"message","id":"msg_1","role":"assistant","status":"completed","content":[{"type":"output_text","text":"hello"}]}`
response := `{"id":"resp_up1","object":"response","status":"completed","output":[` + item + `],"usage":{"input_tokens":300,"output_tokens":50,"total_tokens":350}}`
if !gjson.GetBytes(u.body(n-1), "stream").Bool() {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, response)
return
}
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(
`data: {"type":"response.created","response":{"id":"resp_up1","object":"response","status":"in_progress","output":[]}}`,
`data: {"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":"msg_1","role":"assistant","status":"in_progress","content":[]}}`,
`data: {"type":"response.output_text.delta","item_id":"msg_1","output_index":0,"content_index":0,"delta":"hello"}`))
w.(http.Flusher).Flush()
select {
case <-finish:
case <-r.Context().Done():
return
}
io.WriteString(w, sse(
`data: {"type":"response.output_item.done","output_index":0,"item":`+item+`}`,
`data: {"type":"response.completed","response":`+response+`}`))
}
sockUp(t, u)
_, ts := sockServer(t)
c, _, err := dialSock(t, ts, path, nil)
if err != nil {
t.Fatalf("%s: %v", name, err)
}
c.send(`{"type":"response.create","model":"fake/m1",` + stream + `"store":false,"input":[` + userHi + `]}`)
got, err := c.until("response.output_text.delta", "response.completed", "response.failed", "error")
if err != nil {
close(finish)
t.Fatalf("%s: waiting for a text delta: %v after %v", name, err, got)
}
if delta := last(got); delta.Get("type").String() != "response.output_text.delta" || delta.Get("delta").String() != "hello" {
close(finish)
t.Fatalf("%s: no text delta before the upstream completed: %v", name, got)
}
finish <- struct{}{}
got, err = c.until("response.completed", "response.failed", "error")
if err != nil {
t.Fatalf("%s: %v", name, err)
}
end := last(got)
if end.Get("type").String() != "response.completed" || end.Get("response.output.0.content.0.text").String() != "hello" || end.Get("response.usage.total_tokens").Int() != 350 {
t.Fatalf("%s: completed reply: %v", name, got)
}
if !gjson.GetBytes(u.body(0), "stream").Bool() {
t.Errorf("%s: upstream body %s", name, u.body(0))
}
}
}
}
// The same on Codex's own route (#5291): a frame without stream reaches
// the ChatGPT backend as a streamed turn.
func TestCodexSocketStreamsWithoutStreamField(t *testing.T) {
setup(t, provider.Chat, &fake{t: t})
bodies := make(chan []byte, 1)
chatgpt(t, func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
select {
case bodies <- b:
default:
}
w.Header().Set("Content-Type", "text/event-stream")
item := `{"type":"message","role":"assistant","content":[{"type":"output_text","text":"ok"}]}`
io.WriteString(w, sse(
`data: {"type":"response.created","response":{"id":"resp_c1"}}`,
`data: {"type":"response.output_item.done","output_index":0,"item":`+item+`}`,
`data: {"type":"response.completed","response":{"id":"resp_c1","usage":{"input_tokens":9,"output_tokens":2}}}`))
})
_, ts := sockServer(t)
h := http.Header{"Authorization": {"Bearer chatgpt-token"}, "Chatgpt-Account-Id": {"acct-1"},
"Openai-Beta": {"responses_websockets=2026-02-06"}, "User-Agent": {"codex_cli_rs/0.166.0"}}
c, _, err := dialSock(t, ts, CodexPath+"/responses", h)
if err != nil {
t.Fatal(err)
}
got := c.turn(`{"type":"response.create","model":"gpt-5.5","store":false,"input":[` + userHi + `]}`)
if last(got).Get("type").String() != "response.completed" {
t.Fatalf("%v", got)
}
select {
case b := <-bodies:
if !gjson.GetBytes(b, "stream").Bool() {
t.Errorf("sent to ChatGPT unstreamed: %s", b)
}
default:
t.Fatal("nothing reached ChatGPT")
}
}
// Codex's socket is served (#4058): the upgrade is taken on each
// Responses route; a turn naming the reply before it, with only what
// came after, reaches the provider as the whole conversation, the reply