diff --git a/logger/logger.go b/logger/logger.go index 90cf500..6835098 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -79,9 +79,11 @@ func logHelper(ctx context.Context, level string, msg string) { if level == loggerINFO { writer = gin.DefaultWriter } - id := ctx.Value(common.RequestIdKey) - if id == nil { - id = "SYSTEM" + id := "SYSTEM" + if ctx != nil { + if v := ctx.Value(common.RequestIdKey); v != nil { + id = fmt.Sprintf("%v", v) + } } now := time.Now() _, _ = fmt.Fprintf(writer, "[%s] %v | %s | %s \n", level, now.Format("2006/01/02 - 15:04:05"), id, msg) diff --git a/relay/channel/api_request.go b/relay/channel/api_request.go index 09ca855..407ca2d 100644 --- a/relay/channel/api_request.go +++ b/relay/channel/api_request.go @@ -282,7 +282,7 @@ func DoApiRequest(a Adaptor, c *gin.Context, info *common.RelayInfo, requestBody return nil, fmt.Errorf("get request url failed: %w", err) } if common2.DebugEnabled { - println("fullRequestURL:", fullRequestURL) + common2.SysLog("fullRequestURL: " + fullRequestURL) } req, err := http.NewRequest(c.Request.Method, fullRequestURL, requestBody) if err != nil { @@ -313,7 +313,7 @@ func DoFormRequest(a Adaptor, c *gin.Context, info *common.RelayInfo, requestBod return nil, fmt.Errorf("get request url failed: %w", err) } if common2.DebugEnabled { - println("fullRequestURL:", fullRequestURL) + common2.SysLog("fullRequestURL: " + fullRequestURL) } req, err := http.NewRequest(c.Request.Method, fullRequestURL, requestBody) if err != nil { diff --git a/relay/claude_handler.go b/relay/claude_handler.go index eedd54d..d74fa57 100644 --- a/relay/claude_handler.go +++ b/relay/claude_handler.go @@ -160,7 +160,7 @@ func ClaudeHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *typ } if common.DebugEnabled { - println("requestBody: ", string(jsonData)) + common.SysLog("claude requestBody: " + string(jsonData)) } requestBody = bytes.NewBuffer(jsonData) } diff --git a/relay/helper/price.go b/relay/helper/price.go index 7138b18..83a8b35 100644 --- a/relay/helper/price.go +++ b/relay/helper/price.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/QuantumNous/new-api/common" + "github.com/QuantumNous/new-api/constant" "github.com/QuantumNous/new-api/logger" "github.com/QuantumNous/new-api/model" relaycommon "github.com/QuantumNous/new-api/relay/common" @@ -52,10 +53,16 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens var cacheRatio, cacheCreationRatio, imageRatio, audioRatio, audioCompletionRatio float64 // 尝试获取渠道定价(优先于全局定价) - channelMetaAvailable := info != nil && info.ChannelMeta != nil && info.ChannelId > 0 - if channelMetaAvailable { - cp, found := model.GetEffectivePricing(info.OriginModelName, info.ChannelId) - if found { + // ChannelMeta 在 InitChannelMeta 之前为 nil,但 Distribute 已将 channelId 写入 context + channelId := 0 + if info != nil && info.ChannelMeta != nil && info.ChannelId > 0 { + channelId = info.ChannelId + } else { + channelId = common.GetContextKeyInt(c, constant.ContextKeyChannelId) + } + if channelId > 0 { + cp, found := model.GetEffectivePricing(info.OriginModelName, channelId) + if found && cp != nil { modelRatio = cp.ModelRatio completionRatio = cp.CompletionRatio modelPrice = cp.ModelPrice @@ -67,9 +74,6 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens imageRatio = cp.ImageRatio audioRatio = cp.AudioRatio audioCompletionRatio = cp.AudioCompletionRatio - if common.DebugEnabled { - println(fmt.Sprintf("[ChannelPricing] hit: model=%s channel=%d source=cache", info.OriginModelName, info.ChannelId)) - } } }