fix(task): 修复使用 auto 分组时 Task Relay 不记录日志和不扣费的问题 - #2588
Conversation
问题描述: - 使用 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 等已有类似处理逻辑)
WalkthroughThis PR adds context-aware auto-grouping handling to relay task processing. It reads the selected group from context using Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
relay/relay_task.go (1)
153-159: Well-implemented fix with safe type handling.The implementation correctly addresses the bug by retrieving the actual selected group from context before calculating group ratios. The safe type assertion and non-empty validation are properly implemented.
Optional: Add defensive check to only update when using "auto" group
For additional safety, consider checking if
info.UsingGroupis currently "auto" before overwriting it. This prevents unexpected behavior ifContextKeyAutoGroupis set in scenarios outside auto-grouping:// 处理 auto 分组:从 context 获取实际选中的分组 // 当使用 auto 分组时,Distribute 中间件会将实际选中的分组存储在 ContextKeyAutoGroup 中 -if autoGroup, exists := common.GetContextKey(c, constant.ContextKeyAutoGroup); exists { +if info.UsingGroup == "auto" { + if autoGroup, exists := common.GetContextKey(c, constant.ContextKeyAutoGroup); exists { - if groupStr, ok := autoGroup.(string); ok && groupStr != "" { - info.UsingGroup = groupStr + if groupStr, ok := autoGroup.(string); ok && groupStr != "" { + info.UsingGroup = groupStr + } } }This ensures the update only happens when the token is actually configured for auto-grouping.
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
relay/relay_task.go
🔇 Additional comments (1)
relay/relay_task.go (1)
153-159: The PR claim about existing similar auto-group handling in other relay types is inaccurate.The search reveals that no other relay handlers (compatible_handler, claude_handler, gemini_handler, image_handler, audio_handler, embedding_handler, rerank_handler, responses_handler) implement the pattern of reading
ContextKeyAutoGroupfrom context and assigning it toinfo.UsingGroup. This auto-group logic is unique torelay_task.go(which handles task relay for video operations), not chat/completions or other relay types. WhileContextKeyAutoGroupis set inservice/channel_select.goand used inservice/quota.gofor pricing calculations, no other relay handler reads it to updateUsingGroup. Other handlers receiverelayInfowithUsingGroupalready populated through different initialization paths.
…task-logging fix(task): 修复使用 auto 分组时 Task Relay 不记录日志和不扣费的问题
问题描述:
根本原因:
修复方案:
影响范围:
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.