35 lines
1.1 KiB
Go
35 lines
1.1 KiB
Go
package responses
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
|
|
"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) [][]byte {
|
|
if bytes.HasPrefix(rawJSON, []byte("data:")) {
|
|
rawJSON = bytes.TrimSpace(rawJSON[5:])
|
|
out := make([]byte, 0, len(rawJSON)+len("data: "))
|
|
out = append(out, []byte("data: ")...)
|
|
out = append(out, rawJSON...)
|
|
return [][]byte{out}
|
|
}
|
|
return [][]byte{rawJSON}
|
|
}
|
|
|
|
// ConvertCodexResponseToOpenAIResponsesNonStream builds a single Responses JSON
|
|
// from a non-streaming OpenAI Chat Completions response.
|
|
func ConvertCodexResponseToOpenAIResponsesNonStream(_ context.Context, _ string, _, _, rawJSON []byte, _ *any) []byte {
|
|
rootResult := gjson.ParseBytes(rawJSON)
|
|
// Verify this is a response.completed event
|
|
if rootResult.Get("type").String() != "response.completed" {
|
|
return []byte{}
|
|
}
|
|
responseResult := rootResult.Get("response")
|
|
return []byte(responseResult.Raw)
|
|
}
|