* 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
55 lines
1.9 KiB
Go
55 lines
1.9 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/QuantumNous/new-api/dto"
|
|
relaycommon "github.com/QuantumNous/new-api/relay/common"
|
|
"github.com/QuantumNous/new-api/service/relayconvert"
|
|
"github.com/QuantumNous/new-api/types"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func init() {
|
|
relayconvert.SetMediaResolver(relayconvert.MediaResolver{
|
|
GetBase64Data: GetBase64Data,
|
|
DecodeBase64FileData: DecodeBase64FileData,
|
|
})
|
|
}
|
|
|
|
func ConvertRequest(c *gin.Context, info *relaycommon.RelayInfo, target types.RelayFormat, request any) (*relayconvert.RequestResult, error) {
|
|
return relayconvert.ConvertRequest(c, info, target, request)
|
|
}
|
|
|
|
func ConvertRequestByID(c *gin.Context, info *relaycommon.RelayInfo, converter string, request any) (*relayconvert.RequestResult, error) {
|
|
return relayconvert.ConvertRequestByID(c, info, converter, request)
|
|
}
|
|
|
|
func ConvertRequestVia(c *gin.Context, info *relaycommon.RelayInfo, request any, path ...types.RelayFormat) (*relayconvert.RequestResult, error) {
|
|
return relayconvert.ConvertRequestVia(c, info, request, path...)
|
|
}
|
|
|
|
func ClaudeToOpenAIRequest(claudeRequest dto.ClaudeRequest, info *relaycommon.RelayInfo) (*dto.GeneralOpenAIRequest, error) {
|
|
result, err := ConvertRequest(nil, info, types.RelayFormatOpenAI, &claudeRequest)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
openAIRequest, ok := result.Value.(*dto.GeneralOpenAIRequest)
|
|
if !ok {
|
|
return nil, fmt.Errorf("expected OpenAI chat completions request, got %T", result.Value)
|
|
}
|
|
return openAIRequest, nil
|
|
}
|
|
|
|
func GeminiToOpenAIRequest(geminiRequest *dto.GeminiChatRequest, info *relaycommon.RelayInfo) (*dto.GeneralOpenAIRequest, error) {
|
|
result, err := ConvertRequest(nil, info, types.RelayFormatOpenAI, geminiRequest)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
openAIRequest, ok := result.Value.(*dto.GeneralOpenAIRequest)
|
|
if !ok {
|
|
return nil, fmt.Errorf("expected OpenAI chat completions request, got %T", result.Value)
|
|
}
|
|
return openAIRequest, nil
|
|
}
|