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

@@ -872,17 +872,7 @@ type anthropicMetadataPayload struct {
// replaceModelInBody 替换请求体中的model字段
// 优先使用定点修改,尽量保持客户端原始字段顺序。
func (s *GatewayService) 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)
}
type claudeOAuthNormalizeOptions struct {
@@ -7794,11 +7784,8 @@ func (s *GatewayService) RecordUsage(ctx context.Context, input *RecordUsageInpu
}
var err error
if s.resolver != nil && apiKey.Group != nil {
var groupID *int64
if apiKey.Group != nil {
gid := apiKey.Group.ID
groupID = &gid
}
gid := apiKey.Group.ID
groupID := &gid
cost, err = s.billingService.CalculateCostUnified(CostInput{
Ctx: ctx,
Model: billingModel,
@@ -8184,7 +8171,7 @@ func (s *GatewayService) ResolveChannelMapping(ctx context.Context, groupID int6
// ReplaceModelInBody 替换请求体中的模型名(导出供 handler 使用)
func (s *GatewayService) ReplaceModelInBody(body []byte, newModel string) []byte {
return s.replaceModelInBody(body, newModel)
return ReplaceModelInBody(body, newModel)
}
// IsModelRestricted 检查模型是否被渠道限制
@@ -8198,14 +8185,10 @@ func (s *GatewayService) IsModelRestricted(ctx context.Context, groupID int64, m
// ResolveChannelMappingAndRestrict 解析渠道映射并检查模型限制。
// 返回映射结果和是否被限制。
func (s *GatewayService) 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)
}
// ForwardCountTokens 转发 count_tokens 请求到上游 API