Files
new-api/relaykit/relayconvert/tool_loss_policy_test.go
T
Calcium-Ion 0ed497f066 feat(relay): hosted-tool conversion fidelity, reasoning normalization, and billing usage integrity (#7137)
* 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
2026-09-01 21:53:35 +08:00

92 lines
2.8 KiB
Go

package relayconvert
import (
"testing"
"github.com/QuantumNous/new-api/relaykit/dto"
"github.com/QuantumNous/new-api/relaykit/relayconvert/convmeta"
kitutil "github.com/QuantumNous/new-api/relaykit/relayconvert/kitutil"
"github.com/QuantumNous/new-api/relaykit/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConvertRequestDefaultPolicyAllowsGeminiCodeExecution(t *testing.T) {
t.Parallel()
tools, err := kitutil.Marshal([]map[string]any{{"codeExecution": map[string]any{}}})
require.NoError(t, err)
req := &dto.GeminiChatRequest{
Contents: []dto.GeminiChatContent{
{Role: "user", Parts: []dto.GeminiPart{{Text: "run this"}}},
},
Tools: tools,
}
result, err := ConvertRequest(nil, nil, types.RelayFormatOpenAI, req)
require.NoError(t, err)
require.NotNil(t, result)
require.IsType(t, &dto.GeneralOpenAIRequest{}, result.Value)
assert.True(t, hasConversionDiagnosticCode(result.Diagnostics, "unsupported_hosted_tool"))
}
func TestConvertResponseStrictPolicyStillSucceedsOnContinuationLoss(t *testing.T) {
t.Parallel()
text := "hello"
resp := &dto.ClaudeResponse{
Id: "msg_1",
Type: "message",
Role: "assistant",
Model: "claude-test",
StopReason: "pause_turn",
Content: []dto.ClaudeMediaMessage{
{Type: "redacted_thinking", Data: "secret"},
{Type: "text", Text: &text},
},
}
info := &convmeta.Values{
Options: &convmeta.Options{ToolLossPolicy: types.ConversionLossPolicyStrict},
}
result, err := ConvertResponse(nil, info, types.RelayFormatOpenAI, resp)
require.NoError(t, err)
require.NotNil(t, result)
require.IsType(t, &dto.OpenAITextResponse{}, result.Value)
assert.True(t, hasConversionDiagnosticCode(result.Diagnostics, "continuation_state_lost"))
}
func TestConvertRequestSafePolicyReturnsConversionLossError(t *testing.T) {
t.Parallel()
tools, err := kitutil.Marshal([]map[string]any{{"codeExecution": map[string]any{}}})
require.NoError(t, err)
req := &dto.GeminiChatRequest{
Contents: []dto.GeminiChatContent{
{Role: "user", Parts: []dto.GeminiPart{{Text: "run this"}}},
},
Tools: tools,
}
info := &convmeta.Values{
Options: &convmeta.Options{ToolLossPolicy: types.ConversionLossPolicySafe},
}
result, err := ConvertRequest(nil, info, types.RelayFormatOpenAI, req)
require.Error(t, err)
var loss *types.ConversionLossError
require.ErrorAs(t, err, &loss)
require.NotEmpty(t, loss.Diagnostics)
require.NotNil(t, result)
assert.True(t, hasConversionDiagnosticCode(loss.Diagnostics, "unsupported_hosted_tool"))
assert.True(t, hasConversionDiagnosticCode(result.Diagnostics, "unsupported_hosted_tool"))
}
func hasConversionDiagnosticCode(diagnostics []types.ConversionDiagnostic, code string) bool {
for _, diagnostic := range diagnostics {
if diagnostic.Code == code {
return true
}
}
return false
}