From aed1900364e002d7614e5c068b070d6e8f751c4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E4=BC=AF=E6=B6=9B?= <351175318@qq.com> Date: Mon, 5 Jan 2026 22:57:26 +0800 Subject: [PATCH] =?UTF-8?q?fix(task):=20=E4=BF=AE=E5=A4=8D=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20auto=20=E5=88=86=E7=BB=84=E6=97=B6=20Task=20Relay?= =?UTF-8?q?=20=E4=B8=8D=E8=AE=B0=E5=BD=95=E6=97=A5=E5=BF=97=E5=92=8C?= =?UTF-8?q?=E4=B8=8D=E6=89=A3=E8=B4=B9=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题描述: - 使用 auto 分组的令牌调用 /v1/videos 等 Task 接口时,虽然任务能成功创建, 但使用日志不显示记录,且不会扣费 根本原因: - Distribute 中间件在选择渠道后,会将实际选中的分组存储在 ContextKeyAutoGroup 中 - 但 RelayTaskSubmit 函数没有从 context 中读取这个值来更新 info.UsingGroup - 导致 info.UsingGroup 始终是 "auto" 而不是实际选中的分组(如 "sora2逆") - 当 auto 分组的倍率配置为 0 时,quota 计算结果为 0 - 日志记录条件 "if quota != 0" 不满足,导致日志不记录、不扣费 修复方案: - 在 RelayTaskSubmit 函数中计算分组倍率之前,添加从 ContextKeyAutoGroup 获取实际分组的逻辑 - 使用安全的类型断言,避免潜在的 panic 风险 影响范围: - 仅影响 Task Relay 流程(/v1/videos, /suno, /kling 等接口) - 不影响使用具体分组令牌的调用 - 不影响其他 Relay 类型(chat/completions 等已有类似处理逻辑) --- relay/relay_task.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/relay/relay_task.go b/relay/relay_task.go index 04a905c7f..61588f93b 100644 --- a/relay/relay_task.go +++ b/relay/relay_task.go @@ -150,6 +150,14 @@ func RelayTaskSubmit(c *gin.Context, info *relaycommon.RelayInfo) (taskErr *dto. } } + // 处理 auto 分组:从 context 获取实际选中的分组 + // 当使用 auto 分组时,Distribute 中间件会将实际选中的分组存储在 ContextKeyAutoGroup 中 + if autoGroup, exists := common.GetContextKey(c, constant.ContextKeyAutoGroup); exists { + if groupStr, ok := autoGroup.(string); ok && groupStr != "" { + info.UsingGroup = groupStr + } + } + // 预扣 groupRatio := ratio_setting.GetGroupRatio(info.UsingGroup) var ratio float64