Skip to content

Commit bb8408c

Browse files
committed
fix(codex): backfill streaming response output
1 parent 8f4a4ea commit bb8408c

2 files changed

Lines changed: 103 additions & 2 deletions

File tree

internal/runtime/executor/codex_executor.go

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,48 @@ const (
3636

3737
var dataTag = []byte("data:")
3838

39+
// Streamed Codex responses may emit response.output_item.done events while leaving
40+
// response.completed.response.output empty. Keep the stream path aligned with the
41+
// already-patched non-stream path by reconstructing response.output from those items.
42+
func collectCodexOutputItemDone(eventData []byte, outputItemsByIndex map[int64][]byte, outputItemsFallback *[][]byte) {
43+
itemResult := gjson.GetBytes(eventData, "item")
44+
if !itemResult.Exists() || itemResult.Type != gjson.JSON {
45+
return
46+
}
47+
outputIndexResult := gjson.GetBytes(eventData, "output_index")
48+
if outputIndexResult.Exists() {
49+
outputItemsByIndex[outputIndexResult.Int()] = []byte(itemResult.Raw)
50+
return
51+
}
52+
*outputItemsFallback = append(*outputItemsFallback, []byte(itemResult.Raw))
53+
}
54+
55+
func patchCodexCompletedOutput(eventData []byte, outputItemsByIndex map[int64][]byte, outputItemsFallback [][]byte) []byte {
56+
outputResult := gjson.GetBytes(eventData, "response.output")
57+
shouldPatchOutput := (!outputResult.Exists() || !outputResult.IsArray() || len(outputResult.Array()) == 0) && (len(outputItemsByIndex) > 0 || len(outputItemsFallback) > 0)
58+
if !shouldPatchOutput {
59+
return eventData
60+
}
61+
62+
completedDataPatched := eventData
63+
completedDataPatched, _ = sjson.SetRawBytes(completedDataPatched, "response.output", []byte(`[]`))
64+
65+
indexes := make([]int64, 0, len(outputItemsByIndex))
66+
for idx := range outputItemsByIndex {
67+
indexes = append(indexes, idx)
68+
}
69+
sort.Slice(indexes, func(i, j int) bool {
70+
return indexes[i] < indexes[j]
71+
})
72+
for _, idx := range indexes {
73+
completedDataPatched, _ = sjson.SetRawBytes(completedDataPatched, "response.output.-1", outputItemsByIndex[idx])
74+
}
75+
for _, item := range outputItemsFallback {
76+
completedDataPatched, _ = sjson.SetRawBytes(completedDataPatched, "response.output.-1", item)
77+
}
78+
return completedDataPatched
79+
}
80+
3981
// CodexExecutor is a stateless executor for Codex (OpenAI Responses API entrypoint).
4082
// If api_key is unavailable on auth, it falls back to legacy via ClientAdapter.
4183
type CodexExecutor struct {
@@ -414,20 +456,28 @@ func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au
414456
scanner := bufio.NewScanner(httpResp.Body)
415457
scanner.Buffer(nil, 52_428_800) // 50MB
416458
var param any
459+
outputItemsByIndex := make(map[int64][]byte)
460+
var outputItemsFallback [][]byte
417461
for scanner.Scan() {
418462
line := scanner.Bytes()
419463
helps.AppendAPIResponseChunk(ctx, e.cfg, line)
464+
translatedLine := bytes.Clone(line)
420465

421466
if bytes.HasPrefix(line, dataTag) {
422467
data := bytes.TrimSpace(line[5:])
423-
if gjson.GetBytes(data, "type").String() == "response.completed" {
468+
switch gjson.GetBytes(data, "type").String() {
469+
case "response.output_item.done":
470+
collectCodexOutputItemDone(data, outputItemsByIndex, &outputItemsFallback)
471+
case "response.completed":
424472
if detail, ok := helps.ParseCodexUsage(data); ok {
425473
reporter.Publish(ctx, detail)
426474
}
475+
data = patchCodexCompletedOutput(data, outputItemsByIndex, outputItemsFallback)
476+
translatedLine = append([]byte("data: "), data...)
427477
}
428478
}
429479

430-
chunks := sdktranslator.TranslateStream(ctx, to, from, req.Model, originalPayload, body, bytes.Clone(line), &param)
480+
chunks := sdktranslator.TranslateStream(ctx, to, from, req.Model, originalPayload, body, translatedLine, &param)
431481
for i := range chunks {
432482
out <- cliproxyexecutor.StreamChunk{Payload: chunks[i]}
433483
}

internal/runtime/executor/codex_executor_stream_output_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package executor
22

33
import (
4+
"bytes"
45
"context"
56
"net/http"
67
"net/http/httptest"
@@ -44,3 +45,53 @@ func TestCodexExecutorExecute_EmptyStreamCompletionOutputUsesOutputItemDone(t *t
4445
t.Fatalf("choices.0.message.content = %q, want %q; payload=%s", gotContent, "ok", string(resp.Payload))
4546
}
4647
}
48+
49+
func TestCodexExecutorExecuteStream_EmptyStreamCompletionOutputUsesOutputItemDone(t *testing.T) {
50+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
51+
w.Header().Set("Content-Type", "text/event-stream")
52+
_, _ = w.Write([]byte("data: {\"type\":\"response.output_item.done\",\"item\":{\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"type\":\"output_text\",\"text\":\"ok\"}]},\"output_index\":0}\n"))
53+
_, _ = w.Write([]byte("data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_1\",\"object\":\"response\",\"created_at\":1775555723,\"status\":\"completed\",\"model\":\"gpt-5.4-mini-2026-03-17\",\"output\":[],\"usage\":{\"input_tokens\":8,\"output_tokens\":28,\"total_tokens\":36}}}\n\n"))
54+
}))
55+
defer server.Close()
56+
57+
executor := NewCodexExecutor(&config.Config{})
58+
auth := &cliproxyauth.Auth{Attributes: map[string]string{
59+
"base_url": server.URL,
60+
"api_key": "test",
61+
}}
62+
63+
result, err := executor.ExecuteStream(context.Background(), auth, cliproxyexecutor.Request{
64+
Model: "gpt-5.4-mini",
65+
Payload: []byte(`{"model":"gpt-5.4-mini","input":"Say ok"}`),
66+
}, cliproxyexecutor.Options{
67+
SourceFormat: sdktranslator.FromString("openai-response"),
68+
Stream: true,
69+
})
70+
if err != nil {
71+
t.Fatalf("ExecuteStream error: %v", err)
72+
}
73+
74+
var completed []byte
75+
for chunk := range result.Chunks {
76+
if chunk.Err != nil {
77+
t.Fatalf("stream chunk error: %v", chunk.Err)
78+
}
79+
payload := bytes.TrimSpace(chunk.Payload)
80+
if !bytes.HasPrefix(payload, []byte("data:")) {
81+
continue
82+
}
83+
data := bytes.TrimSpace(payload[5:])
84+
if gjson.GetBytes(data, "type").String() == "response.completed" {
85+
completed = append([]byte(nil), data...)
86+
}
87+
}
88+
89+
if len(completed) == 0 {
90+
t.Fatal("missing response.completed chunk")
91+
}
92+
93+
gotContent := gjson.GetBytes(completed, "response.output.0.content.0.text").String()
94+
if gotContent != "ok" {
95+
t.Fatalf("response.output[0].content[0].text = %q, want %q; completed=%s", gotContent, "ok", string(completed))
96+
}
97+
}

0 commit comments

Comments
 (0)