mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-09-11 14:41:21 +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
73 lines
2.5 KiB
Go
73 lines
2.5 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
relaycommon "github.com/QuantumNous/new-api/relay/common"
|
|
"github.com/QuantumNous/new-api/relaykit/dto"
|
|
"github.com/QuantumNous/new-api/relaykit/relayconvert"
|
|
"github.com/QuantumNous/new-api/relaykit/types"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func init() {
|
|
relayconvert.SetMediaResolver(relayconvert.MediaResolver{
|
|
// relayconvert is gin-free; recover the gin context when the caller
|
|
// passed one so file caching/cleanup keeps working.
|
|
GetBase64Data: func(ctx context.Context, source types.FileSource, reason ...string) (string, string, error) {
|
|
ginCtx, _ := ctx.(*gin.Context)
|
|
return GetBase64Data(ginCtx, source, reason...)
|
|
},
|
|
DecodeBase64FileData: DecodeBase64FileData,
|
|
})
|
|
}
|
|
|
|
func ConvertRequest(c *gin.Context, info *relaycommon.RelayInfo, target types.RelayFormat, request any) (*relayconvert.RequestResult, error) {
|
|
result, err := relayconvert.ConvertRequest(c, info, target, request)
|
|
if result != nil {
|
|
info.RecordConversionDiagnostics(c, result.Diagnostics)
|
|
}
|
|
return result, err
|
|
}
|
|
|
|
func ConvertRequestByID(c *gin.Context, info *relaycommon.RelayInfo, converter string, request any) (*relayconvert.RequestResult, error) {
|
|
result, err := relayconvert.ConvertRequestByID(c, info, converter, request)
|
|
if result != nil {
|
|
info.RecordConversionDiagnostics(c, result.Diagnostics)
|
|
}
|
|
return result, err
|
|
}
|
|
|
|
func ConvertRequestVia(c *gin.Context, info *relaycommon.RelayInfo, request any, path ...types.RelayFormat) (*relayconvert.RequestResult, error) {
|
|
result, err := relayconvert.ConvertRequestVia(c, info, request, path...)
|
|
if result != nil {
|
|
info.RecordConversionDiagnostics(c, result.Diagnostics)
|
|
}
|
|
return result, err
|
|
}
|
|
|
|
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
|
|
}
|