mirror of
https://github.com/router-for-me/CLIProxyAPIPlus.git
synced 2026-04-26 06:56:09 +00:00
fix(openai): improve error handling for response conversion failures
This commit is contained in:
@@ -236,11 +236,7 @@ func parseOpenAIStreamUsage(line []byte) (usage.Detail, bool) {
|
|||||||
return detail, true
|
return detail, true
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseOpenAIResponsesUsage(data []byte) usage.Detail {
|
func parseOpenAIResponsesUsageDetail(usageNode gjson.Result) usage.Detail {
|
||||||
usageNode := gjson.ParseBytes(data).Get("usage")
|
|
||||||
if !usageNode.Exists() {
|
|
||||||
return usage.Detail{}
|
|
||||||
}
|
|
||||||
detail := usage.Detail{
|
detail := usage.Detail{
|
||||||
InputTokens: usageNode.Get("input_tokens").Int(),
|
InputTokens: usageNode.Get("input_tokens").Int(),
|
||||||
OutputTokens: usageNode.Get("output_tokens").Int(),
|
OutputTokens: usageNode.Get("output_tokens").Int(),
|
||||||
@@ -258,6 +254,14 @@ func parseOpenAIResponsesUsage(data []byte) usage.Detail {
|
|||||||
return detail
|
return detail
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseOpenAIResponsesUsage(data []byte) usage.Detail {
|
||||||
|
usageNode := gjson.ParseBytes(data).Get("usage")
|
||||||
|
if !usageNode.Exists() {
|
||||||
|
return usage.Detail{}
|
||||||
|
}
|
||||||
|
return parseOpenAIResponsesUsageDetail(usageNode)
|
||||||
|
}
|
||||||
|
|
||||||
func parseOpenAIResponsesStreamUsage(line []byte) (usage.Detail, bool) {
|
func parseOpenAIResponsesStreamUsage(line []byte) (usage.Detail, bool) {
|
||||||
payload := jsonPayload(line)
|
payload := jsonPayload(line)
|
||||||
if len(payload) == 0 || !gjson.ValidBytes(payload) {
|
if len(payload) == 0 || !gjson.ValidBytes(payload) {
|
||||||
@@ -267,21 +271,7 @@ func parseOpenAIResponsesStreamUsage(line []byte) (usage.Detail, bool) {
|
|||||||
if !usageNode.Exists() {
|
if !usageNode.Exists() {
|
||||||
return usage.Detail{}, false
|
return usage.Detail{}, false
|
||||||
}
|
}
|
||||||
detail := usage.Detail{
|
return parseOpenAIResponsesUsageDetail(usageNode), true
|
||||||
InputTokens: usageNode.Get("input_tokens").Int(),
|
|
||||||
OutputTokens: usageNode.Get("output_tokens").Int(),
|
|
||||||
TotalTokens: usageNode.Get("total_tokens").Int(),
|
|
||||||
}
|
|
||||||
if detail.TotalTokens == 0 {
|
|
||||||
detail.TotalTokens = detail.InputTokens + detail.OutputTokens
|
|
||||||
}
|
|
||||||
if cached := usageNode.Get("input_tokens_details.cached_tokens"); cached.Exists() {
|
|
||||||
detail.CachedTokens = cached.Int()
|
|
||||||
}
|
|
||||||
if reasoning := usageNode.Get("output_tokens_details.reasoning_tokens"); reasoning.Exists() {
|
|
||||||
detail.ReasoningTokens = reasoning.Int()
|
|
||||||
}
|
|
||||||
return detail, true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseClaudeUsage(data []byte) usage.Detail {
|
func parseClaudeUsage(data []byte) usage.Detail {
|
||||||
|
|||||||
@@ -536,7 +536,12 @@ func (h *OpenAIAPIHandler) handleNonStreamingResponseViaResponses(c *gin.Context
|
|||||||
}
|
}
|
||||||
converted := convertResponsesObjectToChatCompletion(cliCtx, modelName, originalChatJSON, rawJSON, resp)
|
converted := convertResponsesObjectToChatCompletion(cliCtx, modelName, originalChatJSON, rawJSON, resp)
|
||||||
if converted == nil {
|
if converted == nil {
|
||||||
converted = resp
|
h.WriteErrorResponse(c, &interfaces.ErrorMessage{
|
||||||
|
StatusCode: http.StatusInternalServerError,
|
||||||
|
Error: fmt.Errorf("failed to convert response to chat completion format"),
|
||||||
|
})
|
||||||
|
cliCancel(fmt.Errorf("response conversion failed"))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
_, _ = c.Writer.Write(converted)
|
_, _ = c.Writer.Write(converted)
|
||||||
cliCancel()
|
cliCancel()
|
||||||
|
|||||||
@@ -145,8 +145,11 @@ func (h *OpenAIResponsesAPIHandler) handleNonStreamingResponseViaChat(c *gin.Con
|
|||||||
var param any
|
var param any
|
||||||
converted := responsesconverter.ConvertOpenAIChatCompletionsResponseToOpenAIResponsesNonStream(cliCtx, modelName, originalResponsesJSON, originalResponsesJSON, resp, ¶m)
|
converted := responsesconverter.ConvertOpenAIChatCompletionsResponseToOpenAIResponsesNonStream(cliCtx, modelName, originalResponsesJSON, originalResponsesJSON, resp, ¶m)
|
||||||
if converted == "" {
|
if converted == "" {
|
||||||
_, _ = c.Writer.Write(resp)
|
h.WriteErrorResponse(c, &interfaces.ErrorMessage{
|
||||||
cliCancel()
|
StatusCode: http.StatusInternalServerError,
|
||||||
|
Error: fmt.Errorf("failed to convert chat completion response to responses format"),
|
||||||
|
})
|
||||||
|
cliCancel(fmt.Errorf("response conversion failed"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, _ = c.Writer.Write([]byte(converted))
|
_, _ = c.Writer.Write([]byte(converted))
|
||||||
|
|||||||
Reference in New Issue
Block a user