The instructions restore logic was originally needed when the proxy
injected custom instructions (per-model system prompts) into requests.
Since ac802a46 removed the injection system, the proxy no longer
modifies instructions before forwarding. The upstream response's
instructions field now matches the client's original value, making
the restore a no-op.
Also removes unused sjson import.
Closes router-for-me/CLIProxyAPI#1868
34 lines
1.1 KiB
Go
34 lines
1.1 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(ctx context.Context, modelName string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *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, modelName string, originalRequestRawJSON, requestRawJSON, 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
|
|
}
|