diff --git a/internal/translator/codex/openai/responses/codex_openai-responses_request.go b/internal/translator/codex/openai/responses/codex_openai-responses_request.go index 081372de..fc08d7d2 100644 --- a/internal/translator/codex/openai/responses/codex_openai-responses_request.go +++ b/internal/translator/codex/openai/responses/codex_openai-responses_request.go @@ -40,7 +40,7 @@ func ConvertOpenAIResponsesRequestToCodex(modelName string, inputRawJSON []byte, inputResults = inputResult.Array() } else if inputResult.Type == gjson.String { newInput := `[{"type":"message","role":"user","content":[{"type":"input_text","text":""}]}]` - newInput, _ = sjson.Set(newInput, "0.content.0.text", inputResult.String()) + newInput, _ = sjson.SetRaw(newInput, "0.content.0.text", inputResult.Raw) inputResults = gjson.Parse(newInput).Array() } } else { diff --git a/internal/translator/codex/openai/responses/codex_openai-responses_response.go b/internal/translator/codex/openai/responses/codex_openai-responses_response.go index 8f8d5347..90c6d258 100644 --- a/internal/translator/codex/openai/responses/codex_openai-responses_response.go +++ b/internal/translator/codex/openai/responses/codex_openai-responses_response.go @@ -1,7 +1,6 @@ package responses import ( - "bufio" "bytes" "context" "fmt" @@ -12,6 +11,7 @@ import ( // ConvertCodexResponseToOpenAIResponses converts OpenAI Chat Completions streaming chunks // to OpenAI Responses SSE events (response.*). + func ConvertCodexResponseToOpenAIResponses(ctx context.Context, modelName string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) []string { if bytes.HasPrefix(rawJSON, []byte("data:")) { rawJSON = bytes.TrimSpace(rawJSON[5:]) @@ -21,7 +21,8 @@ func ConvertCodexResponseToOpenAIResponses(ctx context.Context, modelName string rawJSON, _ = sjson.SetBytes(rawJSON, "response.instructions", gjson.GetBytes(originalRequestRawJSON, "instructions").String()) } } - return []string{fmt.Sprintf("data: %s", string(rawJSON))} + out := fmt.Sprintf("data: %s", string(rawJSON)) + return []string{out} } return []string{string(rawJSON)} } @@ -29,31 +30,13 @@ func ConvertCodexResponseToOpenAIResponses(ctx context.Context, modelName string // ConvertCodexResponseToOpenAIResponsesNonStream builds a single Responses JSON // from a non-streaming OpenAI Chat Completions response. func ConvertCodexResponseToOpenAIResponsesNonStream(_ context.Context, modelName string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, _ *any) string { - scanner := bufio.NewScanner(bytes.NewReader(rawJSON)) - buffer := make([]byte, 20_971_520) - scanner.Buffer(buffer, 20_971_520) - dataTag := []byte("data:") - for scanner.Scan() { - line := scanner.Bytes() - - if !bytes.HasPrefix(line, dataTag) { - continue - } - line = bytes.TrimSpace(line[5:]) - - rootResult := gjson.ParseBytes(line) - // Verify this is a response.completed event - - if rootResult.Get("type").String() != "response.completed" { - - continue - } - responseResult := rootResult.Get("response") - template := responseResult.Raw - - template, _ = sjson.Set(template, "instructions", gjson.GetBytes(originalRequestRawJSON, "instructions").String()) - - return template + rootResult := gjson.ParseBytes(rawJSON) + // Verify this is a response.completed event + if rootResult.Get("type").String() != "response.completed" { + return "" } - return "" + responseResult := rootResult.Get("response") + template := responseResult.Raw + template, _ = sjson.Set(template, "instructions", gjson.GetBytes(originalRequestRawJSON, "instructions").String()) + return template }