feat: support paid-only optimized API routes - #6586
Conversation
WalkthroughThe change adds a paid optimized route marker, request-aware authentication, zero-ratio group rejection, filtered ChangesPaid optimized route
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant Client
participant ReverseProxy
participant TokenAuthOnPaidOptimizedRoute
participant rejectUnpaidOptimizedRoute
participant RelayMidjourneyImage
Client->>ReverseProxy: Send request
ReverseProxy->>TokenAuthOnPaidOptimizedRoute: Add X-NewAPI-Route: paid-optimized
TokenAuthOnPaidOptimizedRoute->>rejectUnpaidOptimizedRoute: Validate marked request
rejectUnpaidOptimizedRoute-->>TokenAuthOnPaidOptimizedRoute: Allow or return 403
TokenAuthOnPaidOptimizedRoute->>RelayMidjourneyImage: Continue allowed request
Possibly related PRs
Suggested reviewers: 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 Warning |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 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.
Inline comments:
In `@i18n/locales/en.yaml`:
- Line 275: Update the paid_optimized_route.free_group_denied locale message and
its corresponding locale entries to use group-neutral wording that refers to the
current group without mentioning a token group, preserving consistent
translations across locales.
In `@i18n/locales/zh-TW.yaml`:
- Line 275: Update the paid_optimized_route.auto_group_denied translation by
replacing the token term 權杖 with the existing locale term 令牌, matching the
wording used in nearby and related token messages.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: f009bfff-9cf0-4dbd-8695-efef92aa51c6
📒 Files selected for processing (15)
constant/context_key.gocontroller/model.godocs/paid-optimized-api-route.mdi18n/keys.goi18n/locales/en.yamli18n/locales/zh-CN.yamli18n/locales/zh-TW.yamlmiddleware/auth.gomiddleware/distributor.gomiddleware/paid_optimized_route.gomiddleware/paid_optimized_route_test.gorouter/relay-router.goservice/channel_select.goservice/group.goservice/group_test.go
| distributor.invalid_midjourney_request: "Invalid Midjourney request: {{.Error}}" | ||
| distributor.invalid_request_parse_model: "Invalid request, unable to parse model" | ||
| paid_optimized_route.auto_group_denied: "No paid auto group is available for the paid optimized route" | ||
| paid_optimized_route.free_group_denied: "The current token group cannot use the paid optimized route" |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Use a group-neutral denial message.
Line 275 also applies after Dashboard-session authentication in TokenOrUserAuth. That request has no current token group. Change the message to refer to the current group, then keep the corresponding locale messages consistent.
Proposed fix
-paid_optimized_route.free_group_denied: "The current token group cannot use the paid optimized route"
+paid_optimized_route.free_group_denied: "The current group cannot use the paid optimized route"📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| paid_optimized_route.free_group_denied: "The current token group cannot use the paid optimized route" | |
| paid_optimized_route.free_group_denied: "The current group cannot use the paid optimized route" |
🤖 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 `@i18n/locales/en.yaml` at line 275, Update the
paid_optimized_route.free_group_denied locale message and its corresponding
locale entries to use group-neutral wording that refers to the current group
without mentioning a token group, preserving consistent translations across
locales.
| distributor.no_available_channel: "分組 {{.Group}} 下模型 {{.Model}} 無可用管道(distributor)" | ||
| distributor.invalid_midjourney_request: "無效的midjourney請求,{{.Error}}" | ||
| distributor.invalid_request_parse_model: "無效的請求,無法解析模型" | ||
| paid_optimized_route.auto_group_denied: "目前 auto 權杖沒有可用於付費最佳化線路的付費分組" |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Use the existing token term.
Replace 權杖 with 令牌. The same locale uses 令牌 on Line 276 and throughout its token messages.
Proposed fix
-paid_optimized_route.auto_group_denied: "目前 auto 權杖沒有可用於付費最佳化線路的付費分組"
+paid_optimized_route.auto_group_denied: "目前 auto 令牌沒有可用於付費最佳化線路的付費分組"📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| paid_optimized_route.auto_group_denied: "目前 auto 權杖沒有可用於付費最佳化線路的付費分組" | |
| paid_optimized_route.auto_group_denied: "目前 auto 令牌沒有可用於付費最佳化線路的付費分組" |
🤖 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 `@i18n/locales/zh-TW.yaml` at line 275, Update the
paid_optimized_route.auto_group_denied translation by replacing the token term
權杖 with the existing locale term 令牌, matching the wording used in nearby and
related token messages.
|
该功能更偏向特定运营场景,暂时保留在 fork 中使用,先关闭 PR 释放并发名额。实现分支和提交继续保留;如果维护者对功能请求有兴趣,可按反馈精简后重新打开。 |
Important
📝 变更描述 / Description
本 PR 支持在同一 New API 实例前部署独立的高成本网络优化 API 入口,并只允许实际分组倍率大于 0 的流量使用。受信任反向代理通过覆盖写入
X-NewAPI-Route: paid-optimized启用限制;普通入口没有该标记,现有行为保持不变。权限判断复用现有
TokenAuth/TokenOrUserAuth已验证的用户组、令牌分组、实际使用分组和GetUserGroupRatio,不新增数据库字段、第二套分组配置或数据库查询。倍率小于等于 0 的请求会在渠道选择前返回403 access_denied。auto令牌不会被整体禁用:优化入口按用户特殊倍率过滤掉免费候选组,初次选择、渠道亲和、跨组重试和/v1/models共用过滤后的候选列表,避免重试重新进入免费组;普通入口继续使用完整 Auto 候选列表。普通入口原本公开的 Midjourney 图片端点保持公开,仅在优化线路标记存在时额外要求 API Key。随附文档说明反代覆盖头、API 白名单、安全边界、性能影响和回滚方式。
🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
✅ 提交前检查项 / Checklist
📸 运行证明 / Proof of Work
回归测试覆盖普通入口不变、免费/付费/特殊倍率、GET 与图片接口、Dashboard 会话回退、Auto 有/无付费候选组、Auto 候选过滤以及优化线路上的 Midjourney 条件鉴权。
Summary by CodeRabbit
New Features
Bug Fixes
Documentation