Skip to content

fix(task): 修复使用 auto 分组时 Task Relay 不记录日志和不扣费的问题 - #2588

Merged
xyfacai merged 1 commit into
QuantumNous:mainfrom
147API:fix/auto-group-task-logging
Jan 6, 2026
Merged

fix(task): 修复使用 auto 分组时 Task Relay 不记录日志和不扣费的问题#2588
xyfacai merged 1 commit into
QuantumNous:mainfrom
147API:fix/auto-group-task-logging

Conversation

@147API

@147API 147API commented Jan 5, 2026

Copy link
Copy Markdown
Contributor

问题描述:

  • 使用 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 等已有类似处理逻辑)

Summary by CodeRabbit

  • Bug Fixes
    • Improved auto grouping to correctly recognize and apply the active group selection from runtime context, ensuring accurate group assignment and pricing calculations.

✏️ Tip: You can customize this high-level summary in your review settings.

问题描述:
- 使用 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 等已有类似处理逻辑)
@coderabbitai

coderabbitai Bot commented Jan 5, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

This PR adds context-aware auto-grouping handling to relay task processing. It reads the selected group from context using ContextKeyAutoGroup and updates info.UsingGroup with that value before downstream group ratio and price calculations, effectively applying runtime group selection overrides.

Changes

Cohort / File(s) Change Summary
Auto-grouping context handling
relay/relay_task.go
Added logic to read auto-selected group from context (ContextKeyAutoGroup) and conditionally set info.UsingGroup if the context value is a non-empty string, overriding initial group assignment before price/ratio calculations.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • PR #2030: Related through group selection propagation—complements this PR by deriving and propagating Task.Group from the UsingGroup value set here.

Suggested labels

Ready to merge

Suggested reviewers

  • seefs001

Poem

🐰 A clever context rabbit bounds through the relay,
Reading groups from shadows, choosing which way,
Auto-grouping whispers guide the task's fate,
Before prices dance, the group is set straight! ✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: fixing Task Relay to properly record logs and charge fees when using auto grouping.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.UsingGroup is currently "auto" before overwriting it. This prevents unexpected behavior if ContextKeyAutoGroup is 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

📥 Commits

Reviewing files that changed from the base of the PR and between f8938a8 and aed1900.

📒 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 ContextKeyAutoGroup from context and assigning it to info.UsingGroup. This auto-group logic is unique to relay_task.go (which handles task relay for video operations), not chat/completions or other relay types. While ContextKeyAutoGroup is set in service/channel_select.go and used in service/quota.go for pricing calculations, no other relay handler reads it to update UsingGroup. Other handlers receive relayInfo with UsingGroup already populated through different initialization paths.

@xyfacai
xyfacai merged commit 022fbce into QuantumNous:main Jan 6, 2026
1 check passed
ennnnny pushed a commit to ennnnny/new-api that referenced this pull request Mar 17, 2026
…task-logging

fix(task): 修复使用 auto 分组时 Task Relay 不记录日志和不扣费的问题
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants