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.
This commit is contained in:
Calcium-Ion
2026-06-27 19:16:56 +08:00
committed by GitHub
parent 626dadb556
commit 3a506f50f0
9 changed files with 1516 additions and 470 deletions
@@ -0,0 +1,128 @@
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)
}
}