修复异步视频输出 Token 计费 - #6173
Conversation
WalkthroughThe change preserves ChangesToken billing settlement
Estimated code review effort: 4 (Complex) | ~45 minutes Possibly related PRs
Suggested reviewers: Sequence Diagram(s)sequenceDiagram
participant RelayTask
participant ModelPriceHelperPerCall
participant settleTaskBillingOnComplete
participant RecalculateTaskQuotaByTokens
participant RecalculateTaskQuota
RelayTask->>ModelPriceHelperPerCall: obtain PriceData
ModelPriceHelperPerCall-->>RelayTask: return ModelRatio and CompletionRatio
RelayTask->>settleTaskBillingOnComplete: persist billing snapshot
settleTaskBillingOnComplete->>RecalculateTaskQuotaByTokens: pass TotalTokens and CompletionTokens
RecalculateTaskQuotaByTokens->>RecalculateTaskQuota: settle calculated quota
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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.
🧹 Nitpick comments (1)
relay/helper/price_test.go (1)
301-304: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winUse
assertfor non-fatal value checks.Per repo test guideline, setup/fatal checks should use
require, but pure value assertions should useassertso all checks run and report together.As per coding guidelines: "New or substantially rewritten Go backend tests must use
requirefor setup and fatal assertions andassertfor non-fatal value checks."♻️ Proposed fix
- require.NoError(t, err) - require.False(t, priceData.UsePrice) - require.Equal(t, 0.5, priceData.ModelRatio) - require.Equal(t, 4.0, priceData.CompletionRatio) + require.NoError(t, err) + assert.False(t, priceData.UsePrice) + assert.Equal(t, 0.5, priceData.ModelRatio) + assert.Equal(t, 4.0, priceData.CompletionRatio)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@relay/helper/price_test.go` around lines 301 - 304, Update the value checks for priceData.UsePrice, priceData.ModelRatio, and priceData.CompletionRatio in this test to use assert instead of require, while keeping require.NoError for the setup/error check.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@relay/helper/price_test.go`:
- Around line 301-304: Update the value checks for priceData.UsePrice,
priceData.ModelRatio, and priceData.CompletionRatio in this test to use assert
instead of require, while keeping require.NoError for the setup/error check.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: cfb400a9-7659-4a4c-ba23-ba9ae54d8b5e
📒 Files selected for processing (7)
controller/relay.gomodel/task.gorelay/helper/price.gorelay/helper/price_test.goservice/task_billing.goservice/task_billing_test.goservice/task_polling.go
There was a problem hiding this comment.
🧹 Nitpick comments (1)
relay/helper/price.go (1)
230-239: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueRedundant
quota = 0inside the free-model check.
quotais already unconditionally set to0on line 232, so thequota = 0on line 236 is a no-op. OnlyfreeModel = truecarries new information. Consider removing the redundant assignment for clarity.♻️ Suggested cleanup
} else { // 按 Token 计费的异步任务只能在上游返回 usage 后确定费用,提交时不预扣。 // 任务完成后由 RecalculateTaskQuotaByTokens 按输入、输出 Token 差额结算。 quota = 0 modelPrice = -1 if !operation_setting.GetQuotaSetting().EnableFreeModelPreConsume { if groupRatioInfo.GroupRatio == 0 || modelRatio == 0 { - quota = 0 freeModel = true } } }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@relay/helper/price.go` around lines 230 - 239, Remove the redundant quota = 0 assignment inside the free-model condition surrounding groupRatioInfo.GroupRatio and modelRatio. Keep the unconditional quota reset and preserve freeModel = true behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@relay/helper/price.go`:
- Around line 230-239: Remove the redundant quota = 0 assignment inside the
free-model condition surrounding groupRatioInfo.GroupRatio and modelRatio. Keep
the unconditional quota reset and preserve freeModel = true behavior.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: f5b9e33c-b453-47ba-8ef0-9722797303c8
📒 Files selected for processing (2)
relay/helper/price.gorelay/helper/price_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
- relay/helper/price_test.go
移植上游 PR QuantumNous#6173(异步视频输出 Token 计费)与 QuantumNous#5831(计量外露)。 QuantumNous#6173:RecalculateTaskQuotaByTokens 此前把 totalTokens 整体按模型倍率 计价,输出 token 未乘补全倍率。现新增 completionTokens 入参,按 input + completion×completionRatio 计价;倍率优先取任务提交时的 BillingContext 快照而非当前配置,避免中途改价影响在途任务。 TaskBillingContext 新增 CompletionRatio(指针,兼容未保存该字段的历史 任务);未配置补全倍率的模型默认取 1,与旧行为等价。 QuantumNous#5831:任务计量经 X-New-Api-Usage 响应头透传给下游(中转套娃场景), body 保持纯 OpenAI 结构;ToOpenAIVideo 带出按秒计费的时长。 一处加固:OtherRatios["seconds"] 读自数据库,历史记录可能写于尚无时长 上界校验的版本,转 int 前先钳到 MaxTaskDurationSeconds——超出 int 范围的 float64 转换结果是未定义的。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
异步任务已经重构,辛苦重新看一下 |
Important
本变更由 AI 辅助实现,提交者已结合实际异步视频
usage与计费日志核对根因、结算公式和变更范围。📝 变更描述 / Description
异步视频任务完成时,原逻辑只按
total_tokens * ModelRatio重新结算。虽然任务适配器已解析completion_tokens,结算层没有使用该字段,也没有应用CompletionRatio,导致输出 Token 按输入价格计费。本 PR 做了三点调整:
total_tokens - completion_tokens计算输入 Token,输出 Token 应用CompletionRatio;completion_tokens时继续按total_tokens使用原有计费逻辑;🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
✅ 提交前检查项 / Checklist
usage和最终公式,并确认描述与改动一致。usage、现行公式和复现步骤。📸 运行证明 / Proof of Work
GitHub Actions 已执行:
目标测试任务通过,覆盖以下行为:
completion_tokens时回退到原有total_tokens计费;测试记录:https://github.com/zuiho-kai/new-api/actions/runs/29258682684/job/86845761821
Summary by CodeRabbit