Grok Build, reading /v1/responses through async-openai's typed ResponseStreamEvent, lost whole turns on a ChatGPT account: the backend speaks part of the Codex protocol (codex.rate_limits, codex.response.metadata, response.metadata, a keepalive event), and the client read each as `unknown variant` and ended the turn. passthrough now runs responsesEnumTidy (responses_enum.go) last in its chain for a Responses stream to a client that isn't Codex (the fromCodex / agentOf pair codex_own_v1.go uses): every event whose `event:` name, or data `type` where it names none, is outside the OpenAI Responses enum is dropped; a keepalive goes on as an SSE comment so a quiet upstream still keeps the client's idle timer alive. sniff and codexLimitsTap read the upstream bytes before it, so failover and the account's windows are unchanged. Codex itself gets every event as it came. A plugin's Responses reply goes through the same passthrough, so it is covered too. The WebSocket route's synthesized header frames (socketTurnWriter.headerFrames) go to Codex alone too. The response.failed shape from the report (missing created_at / sequence_number) was already fixed on main by29bfd5dc8(plugins#852); crayonlu's test for it is kept. Ported from crayonlu's codex-lead-events branch (917878484, four commits), with the keepalive comment and OpenAI's audio events added to the enum. Run (mote, macOS arm64, Go 1.26.8): - focused: TestResponsesEventsOutsideTheEnumStayWithCodex, TestResponsesEnumAllowed, TestPassthroughResponsesEndsShort, TestSocketHeaderFramesOnlyForCodex, TestCodexSocketServed, TestResponsesEventsAreClientShaped: ok. - broken by hand: gateway.go and responses_ws.go back to origin/main with the tests kept: both foreign clients red (codex.rate_limits, codex.response.metadata, response.metadata, keepalive there=true, no keepalive comment) and the socket test red with the leaked frames; the Codex cases stay green. - gofmt -l clean; go vet -tags nogui ./internal/gateway on darwin, GOOS=linux and GOOS=windows ok. - go test -tags nogui -timeout 45m -count=1 ./internal/gateway: ok (2186s), at this tree on origin/mainc9d253e4a. - rebased on44d6dd75d(gateway.go changed upstream): the focused tests above with the new cap/group tests (TestCapGroup*, TestCodexOneAccountSpent*, TestPluginCapPool*) ok; vet ok. Not tried: a real ChatGPT account with Grok Build (a second sign-in can rotate the live refresh token); the full ./... suite (mote at load ~760 with a long suite-lock queue). Reported-by: crayonlu Reported-by: Jeo-Meek Co-authored-by: crayon <hicrayon@outlook.com>
139 lines
5.3 KiB
Go
139 lines
5.3 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"strings"
|
|
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
// responsesEnum is the OpenAI Responses stream event enum: the names a typed
|
|
// Responses client knows. Everything the ChatGPT backend says of its own
|
|
// that is not here — codex.rate_limits and codex.response.metadata (the
|
|
// account's windows, the model list's tag), response.metadata (the turn's
|
|
// headers on a socket, its moderation word on the HTTP route), keepalive (a
|
|
// quiet upstream's heartbeat) — is read as an unknown variant by such a
|
|
// client, which gives up on the whole turn. The list is the one the client
|
|
// names in "expected one of ...", with the audio events OpenAI's own SDK
|
|
// also names.
|
|
var responsesEnum = map[string]struct{}{
|
|
"response.created": {},
|
|
"response.in_progress": {},
|
|
"response.completed": {},
|
|
"response.failed": {},
|
|
"response.incomplete": {},
|
|
"response.output_item.added": {},
|
|
"response.output_item.done": {},
|
|
"response.content_part.added": {},
|
|
"response.content_part.done": {},
|
|
"response.output_text.delta": {},
|
|
"response.output_text.done": {},
|
|
"response.audio.delta": {},
|
|
"response.audio.done": {},
|
|
"response.audio.transcript.delta": {},
|
|
"response.audio.transcript.done": {},
|
|
"response.refusal.delta": {},
|
|
"response.refusal.done": {},
|
|
"response.function_call_arguments.delta": {},
|
|
"response.function_call_arguments.done": {},
|
|
"response.file_search_call.in_progress": {},
|
|
"response.file_search_call.searching": {},
|
|
"response.file_search_call.completed": {},
|
|
"response.web_search_call.in_progress": {},
|
|
"response.web_search_call.searching": {},
|
|
"response.web_search_call.completed": {},
|
|
"response.reasoning_summary_part.added": {},
|
|
"response.reasoning_summary_part.done": {},
|
|
"response.reasoning_summary_text.delta": {},
|
|
"response.reasoning_summary_text.done": {},
|
|
"response.reasoning_text.delta": {},
|
|
"response.reasoning_text.done": {},
|
|
"response.image_generation_call.completed": {},
|
|
"response.image_generation_call.generating": {},
|
|
"response.image_generation_call.in_progress": {},
|
|
"response.image_generation_call.partial_image": {},
|
|
"response.mcp_call_arguments.delta": {},
|
|
"response.mcp_call_arguments.done": {},
|
|
"response.mcp_call.completed": {},
|
|
"response.mcp_call.failed": {},
|
|
"response.mcp_call.in_progress": {},
|
|
"response.mcp_list_tools.completed": {},
|
|
"response.mcp_list_tools.failed": {},
|
|
"response.mcp_list_tools.in_progress": {},
|
|
"response.code_interpreter_call.in_progress": {},
|
|
"response.code_interpreter_call.interpreting": {},
|
|
"response.code_interpreter_call.completed": {},
|
|
"response.code_interpreter_call_code.delta": {},
|
|
"response.code_interpreter_call_code.done": {},
|
|
"response.output_text.annotation.added": {},
|
|
"response.queued": {},
|
|
"response.custom_tool_call_input.delta": {},
|
|
"response.custom_tool_call_input.done": {},
|
|
"error": {},
|
|
}
|
|
|
|
// responsesEnumTidy keeps events outside the Responses enum out of a reply
|
|
// to a client that isn't Codex. A client with a closed event enum reads one
|
|
// and gives up on the whole turn; Codex has names for the backend's own
|
|
// events and reads its limits from them, so its own request still gets every
|
|
// event as it came.
|
|
type responsesEnumTidy struct {
|
|
buf []byte
|
|
}
|
|
|
|
func (t *responsesEnumTidy) write(b []byte) []byte {
|
|
t.buf = append(t.buf, b...)
|
|
out, rest := tidyEvents(t.buf, t.event)
|
|
t.buf = rest
|
|
return out
|
|
}
|
|
|
|
func (t *responsesEnumTidy) flush() []byte {
|
|
b := t.buf
|
|
t.buf = nil
|
|
return b
|
|
}
|
|
|
|
// event is one whole SSE event, dropped when it is outside the enum. A
|
|
// keepalive goes on as an SSE comment, which every client skips, so a quiet
|
|
// upstream's heartbeat still keeps the client's idle timer from running out.
|
|
func (t *responsesEnumTidy) event(ev []byte) []byte {
|
|
if responsesEnumAllowed(ev) {
|
|
return ev
|
|
}
|
|
if sseEventName(ev) == "keepalive" {
|
|
return []byte(": keepalive\n\n")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// responsesEnumAllowed says an event may go to a Responses client: its SSE
|
|
// name, or the type its data carries where the stream names none, is in the
|
|
// enum. A frame with neither is a comment or a blank separator, not an
|
|
// event, and goes as it is.
|
|
func responsesEnumAllowed(ev []byte) bool {
|
|
name := sseEventName(ev)
|
|
if name == "" {
|
|
return true
|
|
}
|
|
_, ok := responsesEnum[name]
|
|
return ok
|
|
}
|
|
|
|
// sseEventName is a frame's `event:` name, else the `type` its data carries,
|
|
// else "".
|
|
func sseEventName(ev []byte) string {
|
|
for _, line := range bytes.Split(ev, []byte("\n")) {
|
|
v, ok := bytes.CutPrefix(bytes.TrimRight(line, "\r"), []byte("event:"))
|
|
if !ok {
|
|
continue
|
|
}
|
|
return strings.TrimSpace(string(v))
|
|
}
|
|
data := eventData(ev)
|
|
if len(data) == 0 {
|
|
return ""
|
|
}
|
|
return gjson.GetBytes(data, "type").String()
|
|
}
|