History

fix(codex): backfill streaming response output

stringer07 committed 6 months ago

Commit bb8408c1 parent 8f4a4ea2 files · +103 −2Browse files

Unified · Split

2 files
  1. internal/runtime/executor/codex_executor.go +52 −2
  2. internal/runtime/executor/codex_executor_stream_output_test.go +51 −0
internal/runtime/executor/codex_executor.go+52−2
@@ -36,6 +36,48 @@ const ( · 35 unmodified lines
36 36
37var dataTag = []byte("data:")37var dataTag = []byte("data:")
38 38
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.
42func 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
55func 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
39// CodexExecutor is a stateless executor for Codex (OpenAI Responses API entrypoint).81// CodexExecutor is a stateless executor for Codex (OpenAI Responses API entrypoint).
40// If api_key is unavailable on auth, it falls back to legacy via ClientAdapter.82// If api_key is unavailable on auth, it falls back to legacy via ClientAdapter.
41type CodexExecutor struct {83type CodexExecutor struct {
@@ -414,20 +456,28 @@ func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au · 372 unmodified lines
414 scanner := bufio.NewScanner(httpResp.Body)456 scanner := bufio.NewScanner(httpResp.Body)
415 scanner.Buffer(nil, 52_428_800) // 50MB457 scanner.Buffer(nil, 52_428_800) // 50MB
416 var param any458 var param any
459 outputItemsByIndex := make(map[int64][]byte)
460 var outputItemsFallback [][]byte
417 for scanner.Scan() {461 for scanner.Scan() {
418 line := scanner.Bytes()462 line := scanner.Bytes()
419 helps.AppendAPIResponseChunk(ctx, e.cfg, line)463 helps.AppendAPIResponseChunk(ctx, e.cfg, line)
464 translatedLine := bytes.Clone(line)
420 465
421 if bytes.HasPrefix(line, dataTag) {466 if bytes.HasPrefix(line, dataTag) {
422 data := bytes.TrimSpace(line[5:])467 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":
424 if detail, ok := helps.ParseCodexUsage(data); ok {472 if detail, ok := helps.ParseCodexUsage(data); ok {
425 reporter.Publish(ctx, detail)473 reporter.Publish(ctx, detail)
426 }474 }
475 data = patchCodexCompletedOutput(data, outputItemsByIndex, outputItemsFallback)
476 translatedLine = append([]byte("data: "), data...)
427 }477 }
428 }478 }
429 479
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)
431 for i := range chunks {481 for i := range chunks {
432 out <- cliproxyexecutor.StreamChunk{Payload: chunks[i]}482 out <- cliproxyexecutor.StreamChunk{Payload: chunks[i]}
433 }483 }
internal/runtime/executor/codex_executor_stream_output_test.go+51−0
@@ -1,6 +1,7 @@
1package executor1package executor
2 2
3import (3import (
4 "bytes"
4 "context"5 "context"
5 "net/http"6 "net/http"
6 "net/http/httptest"7 "net/http/httptest"
@@ -44,3 +45,53 @@ func TestCodexExecutorExecute_EmptyStreamCompletionOutputUsesOutputItemDone(t *t · 37 unmodified lines
44 t.Fatalf("choices.0.message.content = %q, want %q; payload=%s", gotContent, "ok", string(resp.Payload))45 t.Fatalf("choices.0.message.content = %q, want %q; payload=%s", gotContent, "ok", string(resp.Payload))
45 }46 }
46}47}
48
49func 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}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t