refactor(responses): remove compact model suffix handling (#6770)

This commit is contained in:
Seefs
2026-08-11 13:42:32 +08:00
committed by GitHub
parent 253a74dd1b
commit bb234ff418
10 changed files with 21 additions and 133 deletions
+3 -23
View File
@@ -27,7 +27,6 @@ import (
"github.com/QuantumNous/new-api/relaykit/types"
"github.com/QuantumNous/new-api/service"
"github.com/QuantumNous/new-api/setting/operation_setting"
"github.com/QuantumNous/new-api/setting/ratio_setting"
hosttypes "github.com/QuantumNous/new-api/types"
"github.com/samber/lo"
@@ -42,14 +41,11 @@ type testResult struct {
newAPIError *types.NewAPIError
}
func normalizeChannelTestEndpoint(channel *model.Channel, modelName, endpointType string) string {
func normalizeChannelTestEndpoint(channel *model.Channel, endpointType string) string {
normalized := strings.TrimSpace(endpointType)
if normalized != "" {
return normalized
}
if strings.HasSuffix(modelName, ratio_setting.CompactModelSuffix) {
return string(constant.EndpointTypeOpenAIResponseCompact)
}
if channel != nil && channel.Type == constant.ChannelTypeCodex {
return string(constant.EndpointTypeOpenAIResponse)
}
@@ -111,7 +107,7 @@ func testChannel(ctx context.Context, channel *model.Channel, testUserID int, te
}
}
endpointType = normalizeChannelTestEndpoint(channel, testModel, endpointType)
endpointType = normalizeChannelTestEndpoint(channel, endpointType)
requestPath := "/v1/chat/completions"
@@ -146,20 +142,12 @@ func testChannel(ctx context.Context, channel *model.Channel, testUserID int, te
requestPath = "/v1/responses"
}
// responses compaction models (must use /v1/responses/compact)
if strings.HasSuffix(testModel, ratio_setting.CompactModelSuffix) {
requestPath = "/v1/responses/compact"
}
}
// Gemini 原生流式通过 URL action:streamGenerateContent)表达而非请求体字段,
// GeminiChatRequest.IsStream 依据请求 URL 判定,合成请求路径需与生产入口保持一致
if isStream && constant.EndpointType(endpointType) == constant.EndpointTypeGemini {
requestPath = strings.Replace(requestPath, ":generateContent", ":streamGenerateContent", 1)
}
if strings.HasPrefix(requestPath, "/v1/responses/compact") {
testModel = ratio_setting.WithCompactModelSuffix(testModel)
}
c.Request = httptest.NewRequestWithContext(ctx, http.MethodPost, requestPath, nil)
cache, err := model.GetUserCache(testUserID)
@@ -277,7 +265,7 @@ func testChannel(ctx context.Context, channel *model.Channel, testUserID int, te
apiType, _ := common.ChannelType2APIType(channel.Type)
if info.RelayMode == relayconstant.RelayModeResponsesCompact &&
!common.IsResponsesCompactAPIType(apiType) {
!common.SupportsResponsesCompact(channel.Type, apiType) {
return testResult{
context: c,
localErr: fmt.Errorf("responses compaction test is not supported for api type %d", apiType),
@@ -806,14 +794,6 @@ func buildTestRequest(model string, endpointType string, channel *model.Channel,
}
}
// Responses compaction models (must use /v1/responses/compact)
if strings.HasSuffix(model, ratio_setting.CompactModelSuffix) {
return &dto.OpenAIResponsesCompactionRequest{
Model: model,
Input: testResponsesInput,
}
}
// Responses-only models (e.g. codex series)
if strings.Contains(strings.ToLower(model), "codex") {
return &dto.OpenAIResponsesRequest{
+13 -11
View File
@@ -95,23 +95,25 @@ func TestNewAPIChannelRegistration(t *testing.T) {
assert.Empty(t, constant.ChannelBaseURLs[constant.ChannelTypeNewAPI])
}
func TestResponsesCompactAPITypeSupport(t *testing.T) {
func TestResponsesCompactChannelSupport(t *testing.T) {
tests := []struct {
name string
apiType int
want bool
name string
channelType int
apiType int
want bool
}{
{name: "OpenAI", apiType: constant.APITypeOpenAI, want: true},
{name: "Codex", apiType: constant.APITypeCodex, want: true},
{name: "Advanced Custom", apiType: constant.APITypeAdvancedCustom, want: true},
{name: "Sub2API", apiType: constant.APITypeSub2API, want: true},
{name: "New API", apiType: constant.APITypeNewAPI, want: true},
{name: "Anthropic", apiType: constant.APITypeAnthropic, want: false},
{name: "OpenAI", channelType: constant.ChannelTypeOpenAI, apiType: constant.APITypeOpenAI, want: true},
{name: "Azure", channelType: constant.ChannelTypeAzure, apiType: constant.APITypeOpenAI, want: true},
{name: "Codex", channelType: constant.ChannelTypeCodex, apiType: constant.APITypeCodex, want: true},
{name: "Advanced Custom", channelType: constant.ChannelTypeAdvancedCustom, apiType: constant.APITypeAdvancedCustom, want: true},
{name: "Sub2API", channelType: constant.ChannelTypeSub2API, apiType: constant.APITypeSub2API, want: true},
{name: "New API", channelType: constant.ChannelTypeNewAPI, apiType: constant.APITypeNewAPI, want: true},
{name: "Anthropic", channelType: constant.ChannelTypeAnthropic, apiType: constant.APITypeAnthropic, want: false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.want, common.IsResponsesCompactAPIType(test.apiType))
assert.Equal(t, test.want, common.SupportsResponsesCompact(test.channelType, test.apiType))
})
}
}