Skip to content

Commit b641d90

Browse files
committed
refactor(translator): streamline Codex response handling and remove redundant code - Updated `ConvertCodexResponseToOpenAIResponses` logic for clarity and consistency. - Simplified `ConvertCodexResponseToOpenAIResponsesNonStream` by removing unnecessary buffer setup and scanner logic. - Switched to using `sjson.SetRaw` for improved processing of raw input strings.
1 parent 32d01a6 commit b641d90

File tree

2 files changed

+12
-29
lines changed

2 files changed

+12
-29
lines changed

internal/translator/codex/openai/responses/codex_openai-responses_request.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func ConvertOpenAIResponsesRequestToCodex(modelName string, inputRawJSON []byte,
4040
inputResults = inputResult.Array()
4141
} else if inputResult.Type == gjson.String {
4242
newInput := `[{"type":"message","role":"user","content":[{"type":"input_text","text":""}]}]`
43-
newInput, _ = sjson.Set(newInput, "0.content.0.text", inputResult.String())
43+
newInput, _ = sjson.SetRaw(newInput, "0.content.0.text", inputResult.Raw)
4444
inputResults = gjson.Parse(newInput).Array()
4545
}
4646
} else {
Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package responses
22

33
import (
4-
"bufio"
54
"bytes"
65
"context"
76
"fmt"
@@ -12,6 +11,7 @@ import (
1211

1312
// ConvertCodexResponseToOpenAIResponses converts OpenAI Chat Completions streaming chunks
1413
// to OpenAI Responses SSE events (response.*).
14+
1515
func ConvertCodexResponseToOpenAIResponses(ctx context.Context, modelName string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) []string {
1616
if bytes.HasPrefix(rawJSON, []byte("data:")) {
1717
rawJSON = bytes.TrimSpace(rawJSON[5:])
@@ -21,39 +21,22 @@ func ConvertCodexResponseToOpenAIResponses(ctx context.Context, modelName string
2121
rawJSON, _ = sjson.SetBytes(rawJSON, "response.instructions", gjson.GetBytes(originalRequestRawJSON, "instructions").String())
2222
}
2323
}
24-
return []string{fmt.Sprintf("data: %s", string(rawJSON))}
24+
out := fmt.Sprintf("data: %s", string(rawJSON))
25+
return []string{out}
2526
}
2627
return []string{string(rawJSON)}
2728
}
2829

2930
// ConvertCodexResponseToOpenAIResponsesNonStream builds a single Responses JSON
3031
// from a non-streaming OpenAI Chat Completions response.
3132
func ConvertCodexResponseToOpenAIResponsesNonStream(_ context.Context, modelName string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, _ *any) string {
32-
scanner := bufio.NewScanner(bytes.NewReader(rawJSON))
33-
buffer := make([]byte, 20_971_520)
34-
scanner.Buffer(buffer, 20_971_520)
35-
dataTag := []byte("data:")
36-
for scanner.Scan() {
37-
line := scanner.Bytes()
38-
39-
if !bytes.HasPrefix(line, dataTag) {
40-
continue
41-
}
42-
line = bytes.TrimSpace(line[5:])
43-
44-
rootResult := gjson.ParseBytes(line)
45-
// Verify this is a response.completed event
46-
47-
if rootResult.Get("type").String() != "response.completed" {
48-
49-
continue
50-
}
51-
responseResult := rootResult.Get("response")
52-
template := responseResult.Raw
53-
54-
template, _ = sjson.Set(template, "instructions", gjson.GetBytes(originalRequestRawJSON, "instructions").String())
55-
56-
return template
33+
rootResult := gjson.ParseBytes(rawJSON)
34+
// Verify this is a response.completed event
35+
if rootResult.Get("type").String() != "response.completed" {
36+
return ""
5737
}
58-
return ""
38+
responseResult := rootResult.Get("response")
39+
template := responseResult.Raw
40+
template, _ = sjson.Set(template, "instructions", gjson.GetBytes(originalRequestRawJSON, "instructions").String())
41+
return template
5942
}

0 commit comments

Comments
 (0)