Commit 3a506f50 by Calcium-Ion Committed by GitHub

fix(openai): harden Chat-to-Responses compatibility (#5772)

Add a shared Responses-to-Chat stream state machine and use it from the OpenAI relay path. Preserve assistant text alongside tool calls, bind tool argument deltas by output_index, map incomplete finish reasons, support reasoning/custom tool events, and buffer upstream SSE for non-stream Chat clients.

Add deterministic service tests and relay SSE tests for the conversion path.

Related to #5745.
parent 626dadb5
......@@ -36,3 +36,7 @@ data/
token_estimator_test.go
skills-lock.json
.playwright-mcp
# Local-only live probes and scratch test workspaces.
.local-tests/
service/openaicompat/chat_responses_live_local_test.go
......@@ -334,7 +334,7 @@ func (o *OpenAIResponsesResponse) GetSize() string {
}
type IncompleteDetails struct {
Reasoning string `json:"reasoning"`
Reason string `json:"reason"`
}
type ResponsesOutput struct {
......
package openai
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/constant"
relaycommon "github.com/QuantumNous/new-api/relay/common"
"github.com/QuantumNous/new-api/types"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
)
func newResponsesChatTestContext(t *testing.T, body string, isStream bool) (*gin.Context, *httptest.ResponseRecorder, *http.Response, *relaycommon.RelayInfo) {
t.Helper()
recorder := httptest.NewRecorder()
c, _ := gin.CreateTestContext(recorder)
c.Request = httptest.NewRequest(http.MethodPost, "/v1/chat/completions", nil)
c.Set(common.RequestIdKey, "responses-test")
resp := &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(body)),
Header: http.Header{"Content-Type": []string{"text/event-stream"}},
}
info := &relaycommon.RelayInfo{
ChannelMeta: &relaycommon.ChannelMeta{UpstreamModelName: "gpt-test"},
IsStream: isStream,
RelayFormat: types.RelayFormatOpenAI,
ShouldIncludeUsage: true,
DisablePing: true,
}
return c, recorder, resp, info
}
func TestOaiResponsesToChatStreamHandlerConvertsSSEOrderAndUsage(t *testing.T) {
oldMode := gin.Mode()
gin.SetMode(gin.TestMode)
t.Cleanup(func() { gin.SetMode(oldMode) })
oldTimeout := constant.StreamingTimeout
constant.StreamingTimeout = 30
t.Cleanup(func() { constant.StreamingTimeout = oldTimeout })
body := strings.Join([]string{
`data: {"type":"response.created","response":{"id":"resp_1","model":"gpt-test","created_at":1710000000}}`,
`data: {"type":"response.output_text.delta","delta":"hello"}`,
`data: {"type":"response.output_item.added","output_index":1,"item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"lookup"}}`,
`data: {"type":"response.function_call_arguments.delta","output_index":1,"delta":"{\"q\":\"x\"}"}`,
`data: {"type":"response.completed","response":{"status":"completed","usage":{"input_tokens":2,"output_tokens":3,"total_tokens":5}}}`,
`data: [DONE]`,
``,
}, "\n")
c, recorder, resp, info := newResponsesChatTestContext(t, body, true)
usage, err := OaiResponsesToChatStreamHandler(c, info, resp)
require.Nil(t, err)
require.NotNil(t, usage)
require.Equal(t, 2, usage.PromptTokens)
require.Equal(t, 3, usage.CompletionTokens)
require.Equal(t, 5, usage.TotalTokens)
got := recorder.Body.String()
require.Equal(t, "text/event-stream", recorder.Header().Get("Content-Type"))
require.Contains(t, got, `"role":"assistant"`)
require.Contains(t, got, `"content":"hello"`)
require.Contains(t, got, `"name":"lookup"`)
require.Contains(t, got, `"arguments":"{\"q\":\"x\"}"`)
require.Contains(t, got, `"finish_reason":"tool_calls"`)
require.Contains(t, got, `"usage":{"prompt_tokens":2,"completion_tokens":3,"total_tokens":5`)
require.Contains(t, got, `data: [DONE]`)
requireOrderedSubstrings(t, got,
`"role":"assistant"`,
`"content":"hello"`,
`"name":"lookup"`,
`"arguments":"{\"q\":\"x\"}"`,
`"finish_reason":"tool_calls"`,
`"usage":{"prompt_tokens":2,"completion_tokens":3,"total_tokens":5`,
`data: [DONE]`,
)
}
func TestOaiResponsesToChatBufferedStreamHandlerReturnsJSONFromSSE(t *testing.T) {
oldMode := gin.Mode()
gin.SetMode(gin.TestMode)
t.Cleanup(func() { gin.SetMode(oldMode) })
body := strings.Join([]string{
`data: {"type":"response.output_text.delta","delta":"buffered text"}`,
`data: {"type":"response.output_item.added","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"lookup"}}`,
`data: {"type":"response.function_call_arguments.delta","output_index":0,"delta":"{\"q\":\"x\"}"}`,
`data: {"type":"response.done","response":{"model":"gpt-test","status":"completed","usage":{"input_tokens":1,"output_tokens":2,"total_tokens":3}}}`,
`data: [DONE]`,
``,
}, "\n")
c, recorder, resp, info := newResponsesChatTestContext(t, body, false)
usage, err := OaiResponsesToChatBufferedStreamHandler(c, info, resp)
require.Nil(t, err)
require.NotNil(t, usage)
require.Equal(t, 3, usage.TotalTokens)
got := recorder.Body.String()
require.NotContains(t, got, `data:`)
require.Contains(t, got, `"object":"chat.completion"`)
require.Contains(t, got, `"content":"buffered text"`)
require.Contains(t, got, `"name":"lookup"`)
require.Contains(t, got, `"arguments":"{\"q\":\"x\"}"`)
require.Contains(t, got, `"finish_reason":"tool_calls"`)
}
func requireOrderedSubstrings(t *testing.T, s string, parts ...string) {
t.Helper()
offset := 0
for _, part := range parts {
idx := strings.Index(s[offset:], part)
require.NotEqualf(t, -1, idx, "missing %q after byte offset %d", part, offset)
offset += idx + len(part)
}
}
......@@ -145,14 +145,16 @@ func chatCompletionsViaResponses(c *gin.Context, info *relaycommon.RelayInfo, ad
statusCodeMappingStr := c.GetString("status_code_mapping")
httpResp = resp.(*http.Response)
info.IsStream = info.IsStream || strings.HasPrefix(httpResp.Header.Get("Content-Type"), "text/event-stream")
clientStream := info.IsStream
upstreamStream := strings.HasPrefix(httpResp.Header.Get("Content-Type"), "text/event-stream")
info.IsStream = clientStream || upstreamStream
if httpResp.StatusCode != http.StatusOK {
newApiErr := service.RelayErrorHandler(c.Request.Context(), httpResp, false)
service.ResetStatusCode(newApiErr, statusCodeMappingStr)
return nil, newApiErr
}
if info.IsStream {
if upstreamStream && clientStream {
usage, newApiErr := openaichannel.OaiResponsesToChatStreamHandler(c, info, httpResp)
if newApiErr != nil {
service.ResetStatusCode(newApiErr, statusCodeMappingStr)
......@@ -160,6 +162,15 @@ func chatCompletionsViaResponses(c *gin.Context, info *relaycommon.RelayInfo, ad
}
return usage, nil
}
if upstreamStream {
info.IsStream = false
usage, newApiErr := openaichannel.OaiResponsesToChatBufferedStreamHandler(c, info, httpResp)
if newApiErr != nil {
service.ResetStatusCode(newApiErr, statusCodeMappingStr)
return nil, newApiErr
}
return usage, nil
}
usage, newApiErr := openaichannel.OaiResponsesToChatHandler(c, info, httpResp)
if newApiErr != nil {
......
......@@ -615,24 +615,25 @@ func ResponseOpenAI2Claude(openAIResponse *dto.OpenAITextResponse, info *relayco
}
for _, choice := range openAIResponse.Choices {
stopReason = stopReasonOpenAI2Claude(choice.FinishReason)
if choice.FinishReason == "tool_calls" {
for _, toolUse := range choice.Message.ParseToolCalls() {
claudeContent := dto.ClaudeMediaMessage{}
claudeContent.Type = "tool_use"
claudeContent.Id = toolUse.ID
claudeContent.Name = toolUse.Function.Name
var mapParams map[string]interface{}
if err := common.Unmarshal([]byte(toolUse.Function.Arguments), &mapParams); err == nil {
claudeContent.Input = mapParams
} else {
claudeContent.Input = toolUse.Function.Arguments
}
contents = append(contents, claudeContent)
}
} else {
textContent := choice.Message.StringContent()
toolCalls := choice.Message.ParseToolCalls()
if textContent != "" || len(toolCalls) == 0 {
claudeContent := dto.ClaudeMediaMessage{}
claudeContent.Type = "text"
claudeContent.SetText(choice.Message.StringContent())
claudeContent.SetText(textContent)
contents = append(contents, claudeContent)
}
for _, toolUse := range toolCalls {
claudeContent := dto.ClaudeMediaMessage{}
claudeContent.Type = "tool_use"
claudeContent.Id = toolUse.ID
claudeContent.Name = toolUse.Function.Name
var mapParams map[string]interface{}
if err := common.Unmarshal([]byte(toolUse.Function.Arguments), &mapParams); err == nil {
claudeContent.Input = mapParams
} else {
claudeContent.Input = toolUse.Function.Arguments
}
contents = append(contents, claudeContent)
}
}
......@@ -863,37 +864,32 @@ func ResponseOpenAI2Gemini(openAIResponse *dto.OpenAITextResponse, info *relayco
Parts: make([]dto.GeminiPart, 0),
}
// 处理工具调用
toolCalls := choice.Message.ParseToolCalls()
if len(toolCalls) > 0 {
for _, toolCall := range toolCalls {
// 解析参数
var args map[string]interface{}
if toolCall.Function.Arguments != "" {
if err := json.Unmarshal([]byte(toolCall.Function.Arguments), &args); err != nil {
args = map[string]interface{}{"arguments": toolCall.Function.Arguments}
}
} else {
args = make(map[string]interface{})
}
textContent := choice.Message.StringContent()
if textContent != "" {
part := dto.GeminiPart{
Text: textContent,
}
content.Parts = append(content.Parts, part)
}
part := dto.GeminiPart{
FunctionCall: &dto.FunctionCall{
FunctionName: toolCall.Function.Name,
Arguments: args,
},
toolCalls := choice.Message.ParseToolCalls()
for _, toolCall := range toolCalls {
var args map[string]interface{}
if toolCall.Function.Arguments != "" {
if err := common.Unmarshal([]byte(toolCall.Function.Arguments), &args); err != nil {
args = map[string]interface{}{"arguments": toolCall.Function.Arguments}
}
content.Parts = append(content.Parts, part)
} else {
args = make(map[string]interface{})
}
} else {
// 处理文本内容
textContent := choice.Message.StringContent()
if textContent != "" {
part := dto.GeminiPart{
Text: textContent,
}
content.Parts = append(content.Parts, part)
part := dto.GeminiPart{
FunctionCall: &dto.FunctionCall{
FunctionName: toolCall.Function.Name,
Arguments: args,
},
}
content.Parts = append(content.Parts, part)
}
candidate.Content = content
......
......@@ -13,6 +13,10 @@ func ResponsesResponseToChatCompletionsResponse(resp *dto.OpenAIResponsesRespons
return openaicompat.ResponsesResponseToChatCompletionsResponse(resp, id)
}
func ResponsesFinishReasonFromStatus(resp *dto.OpenAIResponsesResponse) (string, bool) {
return openaicompat.ResponsesFinishReasonFromStatus(resp)
}
func ExtractOutputTextFromResponses(resp *dto.OpenAIResponsesResponse) string {
return openaicompat.ExtractOutputTextFromResponses(resp)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment