* refactor: consolidate relay protocol converters * refactor relayconvert text converters * feat: refine relay converters and advanced custom routing * refactor: enhance logging and add thought signature handling for Gemini requests * refactor: enhance channel cache and pricing endpoint handling for advanced custom models * feat: preserve billing usage semantics * feat: add protocol-aware billing usage * Delete useless files * chore: update action versions in workflow files * chore: update Docker action versions in workflow files * fix: harden billing usage settlement and hot-path route matching - estimate Gemini completion tokens locally when billable usageMetadata is prompt-only but output content was received (e.g. client aborts the stream before the final chunk), and rebuild the attached billing_usage as estimated so settlement does not bill zero output tokens - guard NewClaudeMessagesBillingUsage against all-zero ClaudeUsage, matching the OpenAI/Gemini constructors, so a zero billing_usage cannot override a non-zero top-level usage during settlement - cache compiled advanced-custom route model regexes; they run on the request hot path and were recompiled per request - move the effectiveBillingUsage remap to PostTextConsumeQuota only, and document that calculateTextQuotaSummary expects remapped usage - document the updatePricingLock -> channelSyncLock lock ordering that InitChannelCache/CacheUpdateChannel rely on, and the aux-struct pitfall in GeminiChatResponse.UnmarshalJSON
70 lines
1.7 KiB
Go
70 lines
1.7 KiB
Go
package service
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/QuantumNous/new-api/dto"
|
|
relaycommon "github.com/QuantumNous/new-api/relay/common"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestResponseConverterFacades(t *testing.T) {
|
|
cache5m, cache1h := NormalizeCacheCreationSplit(10, 3, 2)
|
|
assert.Equal(t, 8, cache5m)
|
|
assert.Equal(t, 2, cache1h)
|
|
|
|
chatResp := &dto.OpenAITextResponse{
|
|
Id: "chatcmpl_1",
|
|
Model: "gpt-test",
|
|
Choices: []dto.OpenAITextResponseChoice{
|
|
{
|
|
Message: dto.Message{
|
|
Role: "assistant",
|
|
Content: "hello",
|
|
},
|
|
FinishReason: "stop",
|
|
},
|
|
},
|
|
}
|
|
|
|
claudeResp := ResponseOpenAI2Claude(chatResp, &relaycommon.RelayInfo{})
|
|
require.NotNil(t, claudeResp)
|
|
assert.Equal(t, "message", claudeResp.Type)
|
|
|
|
geminiResp := ResponseOpenAI2Gemini(chatResp, &relaycommon.RelayInfo{})
|
|
require.NotNil(t, geminiResp)
|
|
require.Len(t, geminiResp.Candidates, 1)
|
|
}
|
|
|
|
func TestStreamResponseConverterFacades(t *testing.T) {
|
|
info := &relaycommon.RelayInfo{
|
|
SendResponseCount: 1,
|
|
ClaudeConvertInfo: &relaycommon.ClaudeConvertInfo{
|
|
LastMessagesType: relaycommon.LastMessageTypeNone,
|
|
},
|
|
}
|
|
streamResp := &dto.ChatCompletionsStreamResponse{
|
|
Id: "chatcmpl_1",
|
|
Model: "gpt-test",
|
|
Choices: []dto.ChatCompletionsStreamResponseChoice{
|
|
{
|
|
Delta: dto.ChatCompletionsStreamResponseChoiceDelta{
|
|
Content: ptrValue("hello"),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
claudeResponses := StreamResponseOpenAI2Claude(streamResp, info)
|
|
require.NotEmpty(t, claudeResponses)
|
|
|
|
geminiResp := StreamResponseOpenAI2Gemini(streamResp, &relaycommon.RelayInfo{})
|
|
require.NotNil(t, geminiResp)
|
|
require.Len(t, geminiResp.Candidates, 1)
|
|
}
|
|
|
|
func ptrValue[T any](value T) *T {
|
|
return &value
|
|
}
|