fix(channel): 全平台渠道映射覆盖 + 公共函数抽取 + 死代码清理

- 4个缺失handler入口添加渠道映射+限制检查(ChatCompletions/Responses/Gemini)
- 模型限制错误信息优化,区分"模型不可用"和"无账号"
- OpenAI RecordUsage RequestedModel 改用 OriginalModel
- ResolveChannelMappingAndRestrict/ReplaceModelInBody 抽取到 ChannelService 消除跨service重复
- validateNoDuplicateModels 按 platform:model 去重
- 删除 Channel.ResolveMappedModel 死代码和 CalculateCostWithChannel Deprecated方法
- 移除冗余nil检查,抽取 validatePricingBillingMode 公共校验
This commit is contained in:
erio
2026-03-31 15:26:20 +08:00
parent 4ea8b4cb4f
commit eb385457b2
12 changed files with 149 additions and 116 deletions

View File

@@ -416,29 +416,15 @@ func (s *OpenAIGatewayService) IsModelRestricted(ctx context.Context, groupID in
// ResolveChannelMappingAndRestrict 解析渠道映射并检查模型限制。
// 返回映射结果和是否被限制。
func (s *OpenAIGatewayService) ResolveChannelMappingAndRestrict(ctx context.Context, groupID *int64, model string) (ChannelMappingResult, bool) {
var mapping ChannelMappingResult
mapping.MappedModel = model
if groupID == nil {
return mapping, false
if s.channelService == nil {
return ChannelMappingResult{MappedModel: model}, false
}
mapping = s.ResolveChannelMapping(ctx, *groupID, model)
restricted := s.IsModelRestricted(ctx, *groupID, mapping.MappedModel)
return mapping, restricted
return s.channelService.ResolveChannelMappingAndRestrict(ctx, groupID, model)
}
// ReplaceModelInBody 替换请求体中的 JSON model 字段(通用 gjson/sjson 实现)。
func (s *OpenAIGatewayService) ReplaceModelInBody(body []byte, newModel string) []byte {
if len(body) == 0 {
return body
}
if current := gjson.GetBytes(body, "model"); current.Exists() && current.String() == newModel {
return body
}
newBody, err := sjson.SetBytes(body, "model", newModel)
if err != nil {
return body
}
return newBody
return ReplaceModelInBody(body, newModel)
}
func (s *OpenAIGatewayService) getCodexSnapshotThrottle() *accountWriteThrottle {
@@ -4249,13 +4235,20 @@ func (s *OpenAIGatewayService) RecordUsage(ctx context.Context, input *OpenAIRec
durationMs := int(result.Duration.Milliseconds())
accountRateMultiplier := account.BillingRateMultiplier()
requestID := resolveUsageBillingRequestID(ctx, result.RequestID)
// 确定 RequestedModel渠道映射前的原始模型
requestedModel := result.Model
if input.OriginalModel != "" {
requestedModel = input.OriginalModel
}
usageLog := &UsageLog{
UserID: user.ID,
APIKeyID: apiKey.ID,
AccountID: account.ID,
RequestID: requestID,
Model: result.Model,
RequestedModel: result.Model,
RequestedModel: requestedModel,
UpstreamModel: optionalNonEqualStringPtr(result.UpstreamModel, result.Model),
ServiceTier: result.ServiceTier,
ReasoningEffort: result.ReasoningEffort,