mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-09-11 14:41:21 +00:00
Deferred follow-ups from the relaykit-tools review cycle, verified by live end-to-end billing tests: - billing: normalize Gemini modality keys consistently between stream merge and settlement (case/whitespace variants no longer drop independent audio/image pricing) and sum duplicate modality entries on both paths - billing: sync legacy flat Claude cache-creation fields from the CacheCreation sub-object (including zeroing) and fall back to flat fields only when the snapshot never carried a sub-object, closing a stale 1h-cache overcharge path in cascaded deployments - relay: move Chat-to-Claude and Chat-to-Gemini stream conversion state from gin.Context onto RelayInfo and reset it with SendResponseCount in InitChannelMeta, so channel retries start clean while per-request state (stream error collection, conversion diagnostics, channel chain, billing accumulators) survives - relay: Claude channel now serves Gemini-format clients (request via registry conversion, response and stream composed through the Chat pivot), removing the last unimplemented conversion direction - relaykit: recognize legacy pseudo tool names (googleSearch, codeExecution, urlContext) in the toolconv decode stage and drop the string-matching bypass in the Chat-to-Gemini converter; native Gemini tool output is restored and non-Gemini targets follow standard loss diagnostics - relaykit: attach upstream Gemini usage (with billing_usage sidecar) to intermediate stream chunks so converted Claude streams report upstream truth from message_start, and preserve the sidecar through Claude stream usage merges; billing settlement unchanged - billing: clamp negative Total-Prompt completion derivation, OR the Estimated flag across cross-dialect snapshot replacement, and fill canonical OpenAI prompt details via field-wise merge
173 lines
5.2 KiB
Go
173 lines
5.2 KiB
Go
package claude
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"github.com/QuantumNous/new-api/relay/channel"
|
|
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/QuantumNous/new-api/service"
|
|
"github.com/QuantumNous/new-api/setting/model_setting"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Adaptor struct {
|
|
}
|
|
|
|
func (a *Adaptor) ConvertGeminiRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeminiChatRequest) (any, error) {
|
|
if request == nil {
|
|
return nil, errors.New("request is nil")
|
|
}
|
|
result, err := service.ConvertRequest(c, info, types.RelayFormatClaude, request)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return result.Value, nil
|
|
}
|
|
|
|
func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.ClaudeRequest) (any, error) {
|
|
if request.MaxTokens != nil && *request.MaxTokens == 0 {
|
|
request.MaxTokens = nil
|
|
}
|
|
if err := relayconvert.ApplyClaudeThinkingModel(request, info); err != nil {
|
|
return nil, err
|
|
}
|
|
if request.MaxTokens == nil {
|
|
defaultMaxTokens := uint(model_setting.GetClaudeSettings().GetDefaultMaxTokens(request.Model))
|
|
request.MaxTokens = &defaultMaxTokens
|
|
}
|
|
// ApplyClaudeThinkingModel no longer rewrites request.Model. Do not write
|
|
// a still-suffixed name back over the entry-normalized UpstreamModelName
|
|
// (AWS/Vertex look up getAwsModelID / claudeModelMap from that field).
|
|
if info.UpstreamModelName == "" {
|
|
info.UpstreamModelName = request.Model
|
|
}
|
|
return request, nil
|
|
}
|
|
|
|
func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
|
|
//TODO implement me
|
|
return nil, errors.New("not implemented")
|
|
}
|
|
|
|
func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
|
|
//TODO implement me
|
|
return nil, errors.New("not implemented")
|
|
}
|
|
|
|
func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
|
|
}
|
|
|
|
func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
|
|
requestURL := fmt.Sprintf("%s/v1/messages", info.ChannelBaseUrl)
|
|
if !shouldAppendClaudeBetaQuery(info) {
|
|
return requestURL, nil
|
|
}
|
|
|
|
parsedURL, err := url.Parse(requestURL)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
query := parsedURL.Query()
|
|
query.Set("beta", "true")
|
|
parsedURL.RawQuery = query.Encode()
|
|
return parsedURL.String(), nil
|
|
}
|
|
|
|
func shouldAppendClaudeBetaQuery(info *relaycommon.RelayInfo) bool {
|
|
if info == nil {
|
|
return false
|
|
}
|
|
if info.IsClaudeBetaQuery {
|
|
return true
|
|
}
|
|
if info.ChannelOtherSettings.ClaudeBetaQuery {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func CommonClaudeHeadersOperation(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) {
|
|
// common headers operation
|
|
anthropicBeta := c.Request.Header.Get("anthropic-beta")
|
|
if anthropicBeta != "" {
|
|
req.Set("anthropic-beta", anthropicBeta)
|
|
}
|
|
model_setting.GetClaudeSettings().WriteHeaders(info.OriginModelName, req)
|
|
}
|
|
|
|
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
|
|
channel.SetupApiRequestHeader(info, c, req)
|
|
req.Set("x-api-key", info.ApiKey)
|
|
anthropicVersion := c.Request.Header.Get("anthropic-version")
|
|
if anthropicVersion == "" {
|
|
anthropicVersion = "2023-06-01"
|
|
}
|
|
req.Set("anthropic-version", anthropicVersion)
|
|
CommonClaudeHeadersOperation(c, req, info)
|
|
return nil
|
|
}
|
|
|
|
func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
|
|
if request == nil {
|
|
return nil, errors.New("request is nil")
|
|
}
|
|
result, err := service.ConvertRequest(c, info, types.RelayFormatClaude, request)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return result.Value, nil
|
|
}
|
|
|
|
func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
|
|
//TODO implement me
|
|
return nil, errors.New("not implemented")
|
|
}
|
|
|
|
func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
|
|
result, err := service.ConvertRequest(c, info, types.RelayFormatClaude, &request)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
claudeRequest, ok := result.Value.(*dto.ClaudeRequest)
|
|
if !ok {
|
|
return nil, fmt.Errorf("expected Anthropic Messages request, got %T", result.Value)
|
|
}
|
|
return claudeRequest, nil
|
|
}
|
|
|
|
func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
|
|
return channel.DoApiRequest(a, c, info, requestBody)
|
|
}
|
|
|
|
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
|
|
info.FinalRequestRelayFormat = types.RelayFormatClaude
|
|
if info.RelayFormat == types.RelayFormatOpenAIResponses && info.IsStream {
|
|
return ClaudeResponsesStreamHandler(c, resp, info)
|
|
}
|
|
if info.IsStream {
|
|
return ClaudeStreamHandler(c, resp, info)
|
|
} else {
|
|
return ClaudeHandler(c, resp, info)
|
|
}
|
|
}
|
|
|
|
func (a *Adaptor) GetModelList() []string {
|
|
return ModelList
|
|
}
|
|
|
|
func (a *Adaptor) GetChannelName() string {
|
|
return ChannelName
|
|
}
|