mirror of
https://github.com/router-for-me/CLIProxyAPIPlus.git
synced 2026-03-08 06:43:41 +00:00
34 lines
1.0 KiB
Go
34 lines
1.0 KiB
Go
package responses
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
// ConvertCodexResponseToOpenAIResponses converts OpenAI Chat Completions streaming chunks
|
|
// to OpenAI Responses SSE events (response.*).
|
|
|
|
func ConvertCodexResponseToOpenAIResponses(_ context.Context, _ string, _, _, rawJSON []byte, _ *any) []string {
|
|
if bytes.HasPrefix(rawJSON, []byte("data:")) {
|
|
rawJSON = bytes.TrimSpace(rawJSON[5:])
|
|
out := fmt.Sprintf("data: %s", string(rawJSON))
|
|
return []string{out}
|
|
}
|
|
return []string{string(rawJSON)}
|
|
}
|
|
|
|
// ConvertCodexResponseToOpenAIResponsesNonStream builds a single Responses JSON
|
|
// from a non-streaming OpenAI Chat Completions response.
|
|
func ConvertCodexResponseToOpenAIResponsesNonStream(_ context.Context, _ string, _, _, rawJSON []byte, _ *any) string {
|
|
rootResult := gjson.ParseBytes(rawJSON)
|
|
// Verify this is a response.completed event
|
|
if rootResult.Get("type").String() != "response.completed" {
|
|
return ""
|
|
}
|
|
responseResult := rootResult.Get("response")
|
|
return responseResult.Raw
|
|
}
|