- logger: 增加 ctx nil 检查,避免系统级日志 panic - relay: 将 DebugEnabled 下的 println 替换为 SysLog - price: 修复渠道定价在 ChannelMeta 未初始化时的获取失败问题 Co-Authored-By: Claude <noreply@anthropic.com>master
| @@ -79,9 +79,11 @@ func logHelper(ctx context.Context, level string, msg string) { | |||||
| if level == loggerINFO { | if level == loggerINFO { | ||||
| writer = gin.DefaultWriter | 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() | now := time.Now() | ||||
| _, _ = fmt.Fprintf(writer, "[%s] %v | %s | %s \n", level, now.Format("2006/01/02 - 15:04:05"), id, msg) | _, _ = fmt.Fprintf(writer, "[%s] %v | %s | %s \n", level, now.Format("2006/01/02 - 15:04:05"), id, msg) | ||||
| @@ -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) | return nil, fmt.Errorf("get request url failed: %w", err) | ||||
| } | } | ||||
| if common2.DebugEnabled { | if common2.DebugEnabled { | ||||
| println("fullRequestURL:", fullRequestURL) | |||||
| common2.SysLog("fullRequestURL: " + fullRequestURL) | |||||
| } | } | ||||
| req, err := http.NewRequest(c.Request.Method, fullRequestURL, requestBody) | req, err := http.NewRequest(c.Request.Method, fullRequestURL, requestBody) | ||||
| if err != nil { | 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) | return nil, fmt.Errorf("get request url failed: %w", err) | ||||
| } | } | ||||
| if common2.DebugEnabled { | if common2.DebugEnabled { | ||||
| println("fullRequestURL:", fullRequestURL) | |||||
| common2.SysLog("fullRequestURL: " + fullRequestURL) | |||||
| } | } | ||||
| req, err := http.NewRequest(c.Request.Method, fullRequestURL, requestBody) | req, err := http.NewRequest(c.Request.Method, fullRequestURL, requestBody) | ||||
| if err != nil { | if err != nil { | ||||
| @@ -160,7 +160,7 @@ func ClaudeHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *typ | |||||
| } | } | ||||
| if common.DebugEnabled { | if common.DebugEnabled { | ||||
| println("requestBody: ", string(jsonData)) | |||||
| common.SysLog("claude requestBody: " + string(jsonData)) | |||||
| } | } | ||||
| requestBody = bytes.NewBuffer(jsonData) | requestBody = bytes.NewBuffer(jsonData) | ||||
| } | } | ||||
| @@ -4,6 +4,7 @@ import ( | |||||
| "fmt" | "fmt" | ||||
| "github.com/QuantumNous/new-api/common" | "github.com/QuantumNous/new-api/common" | ||||
| "github.com/QuantumNous/new-api/constant" | |||||
| "github.com/QuantumNous/new-api/logger" | "github.com/QuantumNous/new-api/logger" | ||||
| "github.com/QuantumNous/new-api/model" | "github.com/QuantumNous/new-api/model" | ||||
| relaycommon "github.com/QuantumNous/new-api/relay/common" | 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 | 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 | modelRatio = cp.ModelRatio | ||||
| completionRatio = cp.CompletionRatio | completionRatio = cp.CompletionRatio | ||||
| modelPrice = cp.ModelPrice | modelPrice = cp.ModelPrice | ||||
| @@ -67,9 +74,6 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens | |||||
| imageRatio = cp.ImageRatio | imageRatio = cp.ImageRatio | ||||
| audioRatio = cp.AudioRatio | audioRatio = cp.AudioRatio | ||||
| audioCompletionRatio = cp.AudioCompletionRatio | audioCompletionRatio = cp.AudioCompletionRatio | ||||
| if common.DebugEnabled { | |||||
| println(fmt.Sprintf("[ChannelPricing] hit: model=%s channel=%d source=cache", info.OriginModelName, info.ChannelId)) | |||||
| } | |||||
| } | } | ||||
| } | } | ||||