Skip to content

Fix/subscription base group ratio - #6312

Closed
lengxiaoyu6 wants to merge 17 commits into
QuantumNous:mainfrom
lengxiaoyu6:fix/subscription-base-group-ratio
Closed

Fix/subscription base group ratio#6312
lengxiaoyu6 wants to merge 17 commits into
QuantumNous:mainfrom
lengxiaoyu6:fix/subscription-base-group-ratio

Conversation

@lengxiaoyu6

@lengxiaoyu6 lengxiaoyu6 commented Jul 19, 2026

Copy link
Copy Markdown

⚠️ 提交说明 / PR Notice

Important

  • 请提供人工撰写的简洁摘要,避免直接粘贴未经整理的 AI 输出。

Note: 本 PR 代码为 AI-assisted 实现,已由提交者审阅与本地验证。

📝 变更描述 / Description

订阅计费时不应再叠乘分组倍率,否则高分组(如 vip=2)会加速消耗订阅额度,相当于多扣。

修复:

  • 资金来源实际走 订阅 时,分组倍率固定为 1(按模型基础价扣订阅池)
  • 预扣额度按原分组倍率还原:preConsume / groupRatio
  • 同步 PriceData、阶梯计费 snapshot,保证结算/日志一致
  • 订阅预扣失败回退钱包时,恢复原分组倍率
  • Realtime WSS、任务按 token 重算等路径对订阅同样按倍率 1
  • VERSION2026071820260719

未改: 钱包计费仍使用正常分组倍率。

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix)
  • ✨ 新功能 (New feature)
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

  • Closes # (如有)

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述。
  • 非重复提交: 已搜索 Issues / PRs(请自行再确认)。
  • Bug fix 说明: 订阅叠分组倍率导致多扣,属计费错误。
  • 变更理解: 已理解影响范围(订阅路径预扣/结算/任务重算)。
  • 范围聚焦: 以订阅倍率为核心;若相对 origin/main 含 status-code 映射 commit,请在 review 时一并确认。
  • 本地验证: 单测 TestAdjustQuotaToSubscriptionBaseRateTestApplyAndRestoreSubscriptionBaseGroupRatio 通过;go build ./service 通过。
  • 安全合规: 无敏感凭据。

📸 运行证明 / Proof of Work

PASS TestAdjustQuotaToSubscriptionBaseRate
PASS TestApplyAndRestoreSubscriptionBaseGroupRatio
go build ./service  # OK
VERSION=20260719

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->

## Summary by CodeRabbit

* **New Features**
  * Customize error responses by upstream HTTP status code, including status, message, type, and code.
  * View cache tokens and cache hit rates in dashboards, usage logs, and model statistics.
  * Restrict subscription plans and deductions to selected billing groups.
  * Display system notices and announcement timelines on custom home pages.
* **Bug Fixes**
  * Improved channel validation and error handling, including safer handling of invalid configurations.
  * Improved subscription billing and quota calculations for group-specific plans.
* **Documentation**
  * Added translations for new cache, error-mapping, and billing-group features.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

# Conflicts:
#	.gitignore
#	web/bun.lock
#	web/default/src/features/home/index.tsx
# Conflicts:
#	web/default/src/i18n/locales/zh-TW.json
# Conflicts:
#	web/default/src/features/dashboard/components/models/log-stat-cards.tsx
#	web/default/src/features/dashboard/index.tsx
#	web/default/src/features/home/index.tsx
Allow plans to limit which UsingGroup requests deduct subscription quota;
empty allowed_groups keeps all groups eligible, otherwise unmatched requests fall back to wallet.
feat: scope subscription quota deduction to allowed groups
Record mapped user-facing status/message in error logs without mutating the original error used for retry and auto-disable decisions.
Subscription funding should charge at model base cost (group ratio 1)
so group markup does not over-drain the subscription pool. Bump VERSION to 20260719.
@coderabbitai

coderabbitai Bot commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1950be97-ba57-49a8-a101-b40144935840

📥 Commits

Reviewing files that changed from the base of the PR and between 5a6c53d and 536c36b.

📒 Files selected for processing (63)
  • .gitignore
  • Dockerfile
  • VERSION
  • constant/context_key.go
  • controller/channel-test.go
  • controller/channel.go
  • controller/channel_authz.go
  • controller/log.go
  • controller/relay.go
  • controller/subscription.go
  • controller/usedata.go
  • dto/task.go
  • middleware/distributor.go
  • model/channel.go
  • model/log.go
  • model/log_cache_test.go
  • model/main.go
  • model/subscription.go
  • model/subscription_group_test.go
  • relay/relay_task.go
  • router/api-router.go
  • service/billing_session.go
  • service/billing_session_subscription_ratio_test.go
  • service/channel.go
  • service/error.go
  • service/error_test.go
  • service/funding_source.go
  • service/quota.go
  • service/task_billing.go
  • types/error.go
  • web/.npmrc
  • web/classic/rsbuild.config.ts
  • web/classic/src/components/table/subscriptions/modals/AddEditSubscriptionModal.jsx
  • web/default/src/features/channels/components/drawers/channel-mutate-drawer.tsx
  • web/default/src/features/channels/lib/channel-form-errors.ts
  • web/default/src/features/channels/lib/channel-form.ts
  • web/default/src/features/channels/types.ts
  • web/default/src/features/dashboard/api.ts
  • web/default/src/features/dashboard/components/models/log-stat-cards.tsx
  • web/default/src/features/dashboard/hooks/use-dashboard-config.tsx
  • web/default/src/features/dashboard/index.tsx
  • web/default/src/features/dashboard/types.ts
  • web/default/src/features/home/components/home-notice-dialog.tsx
  • web/default/src/features/home/components/index.ts
  • web/default/src/features/home/index.tsx
  • web/default/src/features/subscriptions/components/dialogs/subscription-purchase-dialog.tsx
  • web/default/src/features/subscriptions/components/subscriptions-columns.tsx
  • web/default/src/features/subscriptions/components/subscriptions-mutate-drawer.tsx
  • web/default/src/features/subscriptions/lib/plan-form.ts
  • web/default/src/features/subscriptions/types.ts
  • web/default/src/features/usage-logs/components/columns/common-logs-columns.tsx
  • web/default/src/features/usage-logs/components/common-logs-stats.tsx
  • web/default/src/features/usage-logs/components/dialogs/details-dialog.tsx
  • web/default/src/features/usage-logs/constants.ts
  • web/default/src/features/usage-logs/lib/format.ts
  • web/default/src/features/usage-logs/types.ts
  • web/default/src/i18n/locales/en.json
  • web/default/src/i18n/locales/fr.json
  • web/default/src/i18n/locales/ja.json
  • web/default/src/i18n/locales/ru.json
  • web/default/src/i18n/locales/vi.json
  • web/default/src/i18n/locales/zh-TW.json
  • web/default/src/i18n/locales/zh.json

Walkthrough

This PR adds configurable upstream status-code response mappings, group-scoped subscription billing, cache-token analytics, a home notice dialog, and related API, UI, localization, persistence, and build configuration updates.

Changes

Status-code response mapping

Layer / File(s) Summary
Error contracts and mapping engine
types/error.go, service/error.go, dto/task.go
Errors retain original status codes and can apply mapped status, message, type, and code values to API and task responses.
Relay and channel integration
controller/relay.go, controller/channel.go, middleware/distributor.go, model/channel.go
Channel mappings are validated, propagated through context, applied to relay responses and logs, and excluded from sensitive-field classification.
Channel configuration UI
web/default/src/features/channels/..., web/default/src/i18n/locales/*
Channel forms validate, persist, display, and translate status-code response mappings.

Group-scoped subscription billing

Layer / File(s) Summary
Plan and subscription enforcement
model/subscription.go, model/main.go, controller/subscription.go
Plans and purchased subscriptions store normalized allowed groups, validate configured groups, and restrict subscription eligibility by using group.
Billing integration
service/billing_session.go, service/funding_source.go, service/quota.go, service/task_billing.go
Subscription billing uses a base group ratio, passes the active using group through pre-consumption, and restores wallet pricing when subscription pre-consumption fails.
Subscription management UI
web/default/src/features/subscriptions/..., web/classic/src/components/table/subscriptions/...
Subscription forms, purchase dialogs, and tables support selecting and displaying billing groups.

Cache analytics

Layer / File(s) Summary
Aggregation and API exposure
model/log.go, controller/log.go, controller/usedata.go, router/api-router.go
Cache tokens and hit rates are calculated from consumption logs and exposed through statistics and per-model cache endpoints.
Dashboard and usage displays
web/default/src/features/dashboard/..., web/default/src/features/usage-logs/...
Dashboard cards, log statistics, token breakdowns, and log rows display cache hit-rate metrics using normalized input-token semantics.
Cache calculation tests
model/log_cache_test.go
Tests cover Anthropic semantics, normalized total input, per-model aggregation, cache tokens, and hit-rate calculations.

Home notices and supporting configuration

Layer / File(s) Summary
Home notice dialog
web/default/src/features/home/...
The custom home URL branch renders a dialog containing notice markdown and announcement timeline tabs.
Build and release settings
Dockerfile, VERSION, .gitignore, web/.npmrc, web/classic/rsbuild.config.ts
Container module fetching, Debian package sources, version metadata, ignored tooling output, npm registry selection, and Classic Web date-fns resolution are updated.

Estimated code review effort: 5 (Critical) | ~120 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant ChannelContext
  participant Relay
  participant StatusMapping
  Client->>ChannelContext: Send request through selected channel
  ChannelContext->>Relay: Provide channel response mapping
  Relay->>StatusMapping: Apply mapping to upstream error
  StatusMapping-->>Relay: Return mapped response fields
  Relay-->>Client: Emit mapped error response
Loading
sequenceDiagram
  participant Dashboard
  participant CacheStatsAPI
  participant LogAggregation
  Dashboard->>CacheStatsAPI: Request model cache statistics
  CacheStatsAPI->>LogAggregation: Aggregate consumption logs
  LogAggregation-->>CacheStatsAPI: Return cache tokens and hit rates
  CacheStatsAPI-->>Dashboard: Return model statistics
Loading

Possibly related PRs

Suggested reviewers: calcium-ion

Poem

I’m a rabbit with mappings to share,
Cache-rate carrots float through the air.
Groups guide billing, notices gleam,
Errors now follow a mapped-upstream dream.
Hop, hop—builds retry bright,
And dashboards show cache light!

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

Warning

⚠️ This pull request shows signs of AI-generated slop (ai_padded_prose). It has been flagged by CodeRabbit slop detection and should be reviewed carefully.

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.

1 participant