Skip to content

feat: support paid-only optimized API routes - #6586

Closed
AmPlace wants to merge 1 commit into
QuantumNous:mainfrom
AmPlace:feat/paid-optimized-api-route
Closed

feat: support paid-only optimized API routes#6586
AmPlace wants to merge 1 commit into
QuantumNous:mainfrom
AmPlace:feat/paid-optimized-api-route

Conversation

@AmPlace

@AmPlace AmPlace commented Aug 1, 2026

Copy link
Copy Markdown

⚠️ 提交说明 / PR Notice

Important

  • 本 PR 的实现与测试由 OpenAI Codex 辅助完成;我已检查代码范围、权限边界和测试结果,并人工整理了以下说明。

📝 变更描述 / 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

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

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

  • 人工确认: 我已检查实现并人工整理本描述,没有直接粘贴未经处理的输出。
  • 非重复提交: 已搜索现有 Issues 和 PRs,未发现同类线路级分组倍率限制。
  • Bug fix 说明: 本 PR 是已关联 Issue 的新功能,不将设计取舍标记为 Bug fix。
  • 变更理解: 已检查 TokenAuth、Auto 初选/亲和/重试、模型列表和公开 Midjourney 图片端点的调用路径。
  • 范围聚焦: 仅包含线路标记、分组倍率限制、Auto 候选过滤、测试和部署文档。
  • 本地验证: 已运行相关 Go vet、测试和包构建。
  • 安全合规: 不包含凭据、真实域名或私有服务地址;请求头只启用额外限制,不授予权限。

📸 运行证明 / Proof of Work

$ go vet ./middleware ./service ./controller ./router
(no output)

$ go test ./middleware ./service ./controller ./router -count=1
ok github.com/QuantumNous/new-api/middleware
ok github.com/QuantumNous/new-api/service
ok github.com/QuantumNous/new-api/controller
ok github.com/QuantumNous/new-api/router

$ go build ./middleware ./service ./controller ./router
(no output)

$ git diff --check
(no output)

回归测试覆盖普通入口不变、免费/付费/特殊倍率、GET 与图片接口、Dashboard 会话回退、Auto 有/无付费候选组、Auto 候选过滤以及优化线路上的 Midjourney 条件鉴权。

Summary by CodeRabbit

  • New Features

    • Added a paid optimized API route that permits access only through eligible paid groups.
    • Added optimized-route protection for API requests and Midjourney image requests.
    • Added localized access-denial messages in English, Simplified Chinese, and Traditional Chinese.
  • Bug Fixes

    • Improved automatic group selection to respect request-specific routing and exclude unavailable free groups on optimized routes.
  • Documentation

    • Added configuration, security, deployment, testing, and rollback guidance for paid optimized routes.

@coderabbitai

coderabbitai Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

The change adds a paid optimized route marker, request-aware authentication, zero-ratio group rejection, filtered auto group selection, Midjourney route protection, translations, documentation, and tests.

Changes

Paid optimized route

Layer / File(s) Summary
Route access checks
constant/context_key.go, middleware/paid_optimized_route.go, middleware/auth.go, router/relay-router.go, i18n/*, middleware/paid_optimized_route_test.go, docs/paid-optimized-api-route.md
Marked requests use paid-route authentication and are rejected with 403 when the selected group is unavailable or has a zero ratio. Ordinary requests keep existing behavior. The Midjourney image route uses the conditional authentication middleware.
Request-aware group selection
service/group.go, service/channel_select.go, middleware/distributor.go, controller/model.go, service/group_test.go
GetUserAutoGroupForRequest preserves ordinary-route groups and filters non-positive-ratio groups on paid optimized routes. Channel selection, affinity selection, and model listing use the request-aware lookup.

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
Loading

Possibly related PRs

Suggested reviewers: calcium-ion

Poem

I’m a rabbit guarding the optimized way,
Zero-ratio groups must wait today.
Auto groups now choose what can pass,
Marked requests meet a paid-route class.
Ordinary paths remain unchanged—
Hop, hop, access rules rearranged!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 43.75% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 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 primary change: support for paid-only optimized API routes.
Linked Issues check ✅ Passed The implementation addresses the requirements in issue #6585, including route marking, paid-group filtering, authentication, Midjourney protection, tests, and documentation.
Out of Scope Changes check ✅ Passed The code, tests, translations, documentation, and routing changes directly support the requirements in issue #6585.
✨ 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.

@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: 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

📥 Commits

Reviewing files that changed from the base of the PR and between cfaba1d and a41d31a.

📒 Files selected for processing (15)
  • constant/context_key.go
  • controller/model.go
  • docs/paid-optimized-api-route.md
  • i18n/keys.go
  • i18n/locales/en.yaml
  • i18n/locales/zh-CN.yaml
  • i18n/locales/zh-TW.yaml
  • middleware/auth.go
  • middleware/distributor.go
  • middleware/paid_optimized_route.go
  • middleware/paid_optimized_route_test.go
  • router/relay-router.go
  • service/channel_select.go
  • service/group.go
  • service/group_test.go

Comment thread i18n/locales/en.yaml
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"

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.

🎯 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.

Suggested change
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.

Comment thread i18n/locales/zh-TW.yaml
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 權杖沒有可用於付費最佳化線路的付費分組"

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.

📐 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.

Suggested change
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.

@AmPlace

AmPlace commented Aug 1, 2026

Copy link
Copy Markdown
Author

该功能更偏向特定运营场景,暂时保留在 fork 中使用,先关闭 PR 释放并发名额。实现分支和提交继续保留;如果维护者对功能请求有兴趣,可按反馈精简后重新打开。

@AmPlace AmPlace closed this Aug 1, 2026
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.

支持按分组倍率限制独立优化 API 入口

1 participant