mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 13:00:52 +08:00
Fixed #91
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.
This commit is contained in:
@@ -40,7 +40,7 @@ func ConvertOpenAIResponsesRequestToCodex(modelName string, inputRawJSON []byte,
|
|||||||
inputResults = inputResult.Array()
|
inputResults = inputResult.Array()
|
||||||
} else if inputResult.Type == gjson.String {
|
} else if inputResult.Type == gjson.String {
|
||||||
newInput := `[{"type":"message","role":"user","content":[{"type":"input_text","text":""}]}]`
|
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()
|
inputResults = gjson.Parse(newInput).Array()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package responses
|
package responses
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -12,6 +11,7 @@ import (
|
|||||||
|
|
||||||
// ConvertCodexResponseToOpenAIResponses converts OpenAI Chat Completions streaming chunks
|
// ConvertCodexResponseToOpenAIResponses converts OpenAI Chat Completions streaming chunks
|
||||||
// to OpenAI Responses SSE events (response.*).
|
// to OpenAI Responses SSE events (response.*).
|
||||||
|
|
||||||
func ConvertCodexResponseToOpenAIResponses(ctx context.Context, modelName string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) []string {
|
func ConvertCodexResponseToOpenAIResponses(ctx context.Context, modelName string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) []string {
|
||||||
if bytes.HasPrefix(rawJSON, []byte("data:")) {
|
if bytes.HasPrefix(rawJSON, []byte("data:")) {
|
||||||
rawJSON = bytes.TrimSpace(rawJSON[5:])
|
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())
|
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)}
|
return []string{string(rawJSON)}
|
||||||
}
|
}
|
||||||
@@ -29,31 +30,13 @@ func ConvertCodexResponseToOpenAIResponses(ctx context.Context, modelName string
|
|||||||
// ConvertCodexResponseToOpenAIResponsesNonStream builds a single Responses JSON
|
// ConvertCodexResponseToOpenAIResponsesNonStream builds a single Responses JSON
|
||||||
// from a non-streaming OpenAI Chat Completions response.
|
// from a non-streaming OpenAI Chat Completions response.
|
||||||
func ConvertCodexResponseToOpenAIResponsesNonStream(_ context.Context, modelName string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, _ *any) string {
|
func ConvertCodexResponseToOpenAIResponsesNonStream(_ context.Context, modelName string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, _ *any) string {
|
||||||
scanner := bufio.NewScanner(bytes.NewReader(rawJSON))
|
rootResult := gjson.ParseBytes(rawJSON)
|
||||||
buffer := make([]byte, 20_971_520)
|
// Verify this is a response.completed event
|
||||||
scanner.Buffer(buffer, 20_971_520)
|
if rootResult.Get("type").String() != "response.completed" {
|
||||||
dataTag := []byte("data:")
|
return ""
|
||||||
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
|
|
||||||
}
|
}
|
||||||
return ""
|
responseResult := rootResult.Get("response")
|
||||||
|
template := responseResult.Raw
|
||||||
|
template, _ = sjson.Set(template, "instructions", gjson.GetBytes(originalRequestRawJSON, "instructions").String())
|
||||||
|
return template
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user