mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-09-12 23:30:35 +00:00
* feat(relaykit): preserve hosted tools across conversions - add protocol-neutral hosted-tool DTOs, conversion metadata, and loss policies - bridge citations, grounding metadata, and hosted-tool stream lifecycles - document the public conversion behavior and channel policy controls * refactor(relaykit): normalize reasoning and thinking intent - centralize provider-neutral reasoning intent, effort, and budget mappings - parse model suffixes at the host entry boundary while preserving provider-owned tails - keep adaptive Claude thinking and explicit zero-token compatibility consistent * fix(billing): preserve authoritative usage across relay hops - carry native BillingUsage sidecars through direct and streamed protocol bridges - merge partial and terminal usage monotonically with safe fallback settlement - retain cache metadata, penultimate usage, and per-call Gemini tool surcharges * feat(relay): bridge Responses with Claude and Gemini protocols - add direct request, response, and stream converters across supported relay formats - expose Claude count_tokens and Chat-to-Responses compatibility endpoints - carry conversion diagnostics through the host while retaining the curated public goldens * fix(relay): wire relaykit conversions into host channels - connect handlers, adaptors, and channel settings to the standalone conversion layer - keep model mapping, pricing identity, retries, and provider-specific suffix behavior aligned - ignore local audit artifacts and retain focused public regression coverage
91 lines
2.7 KiB
Go
91 lines
2.7 KiB
Go
package claude
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
relaycommon "github.com/QuantumNous/new-api/relay/common"
|
|
"github.com/QuantumNous/new-api/relay/helper"
|
|
"github.com/QuantumNous/new-api/relaykit/dto"
|
|
"github.com/QuantumNous/new-api/setting/model_setting"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestConvertClaudeRequestTreatsZeroMaxTokensAsUnset(t *testing.T) {
|
|
zero := uint(0)
|
|
req := &dto.ClaudeRequest{
|
|
Model: "claude-sonnet-4-5",
|
|
MaxTokens: &zero,
|
|
Messages: []dto.ClaudeMessage{
|
|
{Role: "user", Content: "hello"},
|
|
},
|
|
}
|
|
info := &relaycommon.RelayInfo{
|
|
ChannelMeta: &relaycommon.ChannelMeta{
|
|
UpstreamModelName: "claude-sonnet-4-5",
|
|
},
|
|
}
|
|
|
|
out, err := (&Adaptor{}).ConvertClaudeRequest(nil, info, req)
|
|
require.NoError(t, err)
|
|
converted, ok := out.(*dto.ClaudeRequest)
|
|
require.True(t, ok)
|
|
require.NotNil(t, converted.MaxTokens)
|
|
assert.Equal(t, uint(model_setting.GetClaudeSettings().GetDefaultMaxTokens(req.Model)), *converted.MaxTokens)
|
|
}
|
|
|
|
func TestConvertClaudeRequestZeroMaxTokensStillRaisesThinkingBudget(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
|
|
|
zero := uint(0)
|
|
original := &dto.ClaudeRequest{
|
|
Model: "claude-3-7-sonnet-thinking",
|
|
MaxTokens: &zero,
|
|
Messages: []dto.ClaudeMessage{
|
|
{Role: "user", Content: "hello"},
|
|
},
|
|
}
|
|
info := &relaycommon.RelayInfo{
|
|
OriginModelName: "claude-3-7-sonnet-thinking",
|
|
Request: original,
|
|
ChannelMeta: &relaycommon.ChannelMeta{
|
|
UpstreamModelName: "claude-3-7-sonnet-thinking",
|
|
},
|
|
}
|
|
outbound, err := common.DeepCopy(original)
|
|
require.NoError(t, err)
|
|
require.NoError(t, helper.ModelMappedHelper(c, info, outbound))
|
|
require.NoError(t, helper.ApplyReasoningModelSuffix(info, outbound))
|
|
|
|
out, err := (&Adaptor{}).ConvertClaudeRequest(nil, info, outbound)
|
|
require.NoError(t, err)
|
|
converted, ok := out.(*dto.ClaudeRequest)
|
|
require.True(t, ok)
|
|
assert.Equal(t, "claude-3-7-sonnet", converted.Model)
|
|
require.NotNil(t, converted.Thinking)
|
|
require.NotNil(t, converted.MaxTokens)
|
|
assert.Greater(t, *converted.MaxTokens, uint(1024))
|
|
}
|
|
|
|
func TestConvertClaudeRequestDoesNotOverwriteTrimmedUpstreamModelName(t *testing.T) {
|
|
req := &dto.ClaudeRequest{
|
|
Model: "claude-3-7-sonnet-thinking",
|
|
Messages: []dto.ClaudeMessage{
|
|
{Role: "user", Content: "hello"},
|
|
},
|
|
}
|
|
info := &relaycommon.RelayInfo{
|
|
ChannelMeta: &relaycommon.ChannelMeta{
|
|
UpstreamModelName: "claude-3-7-sonnet",
|
|
},
|
|
}
|
|
|
|
_, err := (&Adaptor{}).ConvertClaudeRequest(nil, info, req)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "claude-3-7-sonnet", info.UpstreamModelName)
|
|
}
|