Merge pull request #658 from moxi000/fix-responses-convert
Fix responses-format handling for chat completions(Support Cursor)
This commit is contained in:
@@ -17,6 +17,7 @@ import (
|
|||||||
. "github.com/router-for-me/CLIProxyAPI/v6/internal/constant"
|
. "github.com/router-for-me/CLIProxyAPI/v6/internal/constant"
|
||||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces"
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces"
|
||||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/registry"
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/registry"
|
||||||
|
responsesconverter "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/openai/openai/responses"
|
||||||
"github.com/router-for-me/CLIProxyAPI/v6/sdk/api/handlers"
|
"github.com/router-for-me/CLIProxyAPI/v6/sdk/api/handlers"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
"github.com/tidwall/sjson"
|
"github.com/tidwall/sjson"
|
||||||
@@ -109,7 +110,17 @@ func (h *OpenAIAPIHandler) ChatCompletions(c *gin.Context) {
|
|||||||
|
|
||||||
// Check if the client requested a streaming response.
|
// Check if the client requested a streaming response.
|
||||||
streamResult := gjson.GetBytes(rawJSON, "stream")
|
streamResult := gjson.GetBytes(rawJSON, "stream")
|
||||||
if streamResult.Type == gjson.True {
|
stream := streamResult.Type == gjson.True
|
||||||
|
|
||||||
|
// Some clients send OpenAI Responses-format payloads to /v1/chat/completions.
|
||||||
|
// Convert them to Chat Completions so downstream translators preserve tool metadata.
|
||||||
|
if shouldTreatAsResponsesFormat(rawJSON) {
|
||||||
|
modelName := gjson.GetBytes(rawJSON, "model").String()
|
||||||
|
rawJSON = responsesconverter.ConvertOpenAIResponsesRequestToOpenAIChatCompletions(modelName, rawJSON, stream)
|
||||||
|
stream = gjson.GetBytes(rawJSON, "stream").Bool()
|
||||||
|
}
|
||||||
|
|
||||||
|
if stream {
|
||||||
h.handleStreamingResponse(c, rawJSON)
|
h.handleStreamingResponse(c, rawJSON)
|
||||||
} else {
|
} else {
|
||||||
h.handleNonStreamingResponse(c, rawJSON)
|
h.handleNonStreamingResponse(c, rawJSON)
|
||||||
@@ -117,6 +128,21 @@ func (h *OpenAIAPIHandler) ChatCompletions(c *gin.Context) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// shouldTreatAsResponsesFormat detects OpenAI Responses-style payloads that are
|
||||||
|
// accidentally sent to the Chat Completions endpoint.
|
||||||
|
func shouldTreatAsResponsesFormat(rawJSON []byte) bool {
|
||||||
|
if gjson.GetBytes(rawJSON, "messages").Exists() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if gjson.GetBytes(rawJSON, "input").Exists() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if gjson.GetBytes(rawJSON, "instructions").Exists() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// Completions handles the /v1/completions endpoint.
|
// Completions handles the /v1/completions endpoint.
|
||||||
// It determines whether the request is for a streaming or non-streaming response
|
// It determines whether the request is for a streaming or non-streaming response
|
||||||
// and calls the appropriate handler based on the model provider.
|
// and calls the appropriate handler based on the model provider.
|
||||||
|
|||||||
Reference in New Issue
Block a user