mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-20 15:06:36 +00:00
Merge branch 'feature/channel' into release/custom-0.1.105
# Conflicts: # backend/cmd/server/VERSION # backend/internal/handler/admin/channel_handler.go # backend/internal/service/channel.go
This commit is contained in:
commit
6b68152540
24 files changed
+782
-209
No files matched your search
@@ -158,6 +158,24 @@ func (h *GatewayHandler) Messages(c *gin.Context) {
|
||||
reqStream := parsedReq.Stream
|
||||
reqLog = reqLog.With(zap.String("model", reqModel), zap.Bool("stream", reqStream))
|
||||
|
||||
// 解析渠道级模型映射
|
||||
var channelMapping service.ChannelMappingResult
|
||||
if apiKey.GroupID != nil {
|
||||
channelMapping = h.gatewayService.ResolveChannelMapping(c.Request.Context(), *apiKey.GroupID, reqModel)
|
||||
}
|
||||
|
||||
// 渠道模型限制检查
|
||||
if apiKey.GroupID != nil {
|
||||
checkModel := reqModel
|
||||
if channelMapping.Mapped {
|
||||
checkModel = channelMapping.MappedModel
|
||||
}
|
||||
if h.gatewayService.IsModelRestricted(c.Request.Context(), *apiKey.GroupID, checkModel) {
|
||||
h.errorResponse(c, http.StatusForbidden, "invalid_request_error", "Model not available in current channel: "+reqModel)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 设置 max_tokens=1 + haiku 探测请求标识到 context 中
|
||||
// 必须在 SetClaudeCodeClientContext 之前设置,因为 ClaudeCodeValidator 需要读取此标识进行绕过判断
|
||||
if isMaxTokensOneHaikuRequest(reqModel, parsedReq.MaxTokens, reqStream) {
|
||||
@@ -479,6 +497,21 @@ func (h *GatewayHandler) Messages(c *gin.Context) {
|
||||
RequestPayloadHash: requestPayloadHash,
|
||||
ForceCacheBilling: fs.ForceCacheBilling,
|
||||
APIKeyService: h.apiKeyService,
|
||||
ChannelID: channelMapping.ChannelID,
|
||||
OriginalModel: reqModel,
|
||||
BillingModelSource: channelMapping.BillingModelSource,
|
||||
ModelMappingChain: func() string {
|
||||
if !channelMapping.Mapped {
|
||||
if result.UpstreamModel != "" && result.UpstreamModel != result.Model {
|
||||
return reqModel + "→" + result.UpstreamModel
|
||||
}
|
||||
return ""
|
||||
}
|
||||
if result.UpstreamModel != "" && result.UpstreamModel != channelMapping.MappedModel {
|
||||
return reqModel + "→" + channelMapping.MappedModel + "→" + result.UpstreamModel
|
||||
}
|
||||
return reqModel + "→" + channelMapping.MappedModel
|
||||
}(),
|
||||
}); err != nil {
|
||||
logger.L().With(
|
||||
zap.String("component", "handler.gateway.messages"),
|
||||
@@ -669,6 +702,13 @@ func (h *GatewayHandler) Messages(c *gin.Context) {
|
||||
parsedReq.OnUpstreamAccepted = queueRelease
|
||||
// ===== 用户消息串行队列 END =====
|
||||
|
||||
// 应用渠道模型映射到请求
|
||||
if channelMapping.Mapped {
|
||||
parsedReq.Model = channelMapping.MappedModel
|
||||
parsedReq.Body = h.gatewayService.ReplaceModelInBody(parsedReq.Body, channelMapping.MappedModel)
|
||||
body = h.gatewayService.ReplaceModelInBody(body, channelMapping.MappedModel)
|
||||
}
|
||||
|
||||
// 转发请求 - 根据账号平台分流
|
||||
c.Set("parsed_request", parsedReq)
|
||||
var result *service.ForwardResult
|
||||
@@ -821,6 +861,21 @@ func (h *GatewayHandler) Messages(c *gin.Context) {
|
||||
RequestPayloadHash: requestPayloadHash,
|
||||
ForceCacheBilling: fs.ForceCacheBilling,
|
||||
APIKeyService: h.apiKeyService,
|
||||
ChannelID: channelMapping.ChannelID,
|
||||
OriginalModel: reqModel,
|
||||
BillingModelSource: channelMapping.BillingModelSource,
|
||||
ModelMappingChain: func() string {
|
||||
if !channelMapping.Mapped {
|
||||
if result.UpstreamModel != "" && result.UpstreamModel != result.Model {
|
||||
return reqModel + "→" + result.UpstreamModel
|
||||
}
|
||||
return ""
|
||||
}
|
||||
if result.UpstreamModel != "" && result.UpstreamModel != channelMapping.MappedModel {
|
||||
return reqModel + "→" + channelMapping.MappedModel + "→" + result.UpstreamModel
|
||||
}
|
||||
return reqModel + "→" + channelMapping.MappedModel
|
||||
}(),
|
||||
}); err != nil {
|
||||
logger.L().With(
|
||||
zap.String("component", "handler.gateway.messages"),
|
||||
|
||||
Reference in new issue
Block a user