Skip to content

feat: support request_header key source - #4903

Merged
Calcium-Ion merged 3 commits into
QuantumNous:mainfrom
seefs001:feature/channel-affinity-request-header
May 17, 2026
Merged

feat: support request_header key source#4903
Calcium-Ion merged 3 commits into
QuantumNous:mainfrom
seefs001:feature/channel-affinity-request-header

Conversation

@seefs001

@seefs001 seefs001 commented May 16, 2026

Copy link
Copy Markdown
Collaborator

⚠️ 提交说明 / PR Notice

Important

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

📝 变更描述 / Description

(简述:做了什么?为什么这样改能生效?请基于你对代码逻辑的理解来写,避免粘贴未经整理的内容)
渠道亲和配置规则支持配置请求头

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 IssuesPRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

(请在此粘贴截图、关键日志或测试报告,以证明变更生效)

Summary by CodeRabbit

  • New Features
    • Added support for a request_header key source for channel affinity, including UI support and updated validation; header values are trimmed when read.
  • Tests
    • Added unit tests covering header extraction and affinity resolution based on request headers.

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 16, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 601905dc-4201-4356-b20a-bcde58d782e6

📥 Commits

Reviewing files that changed from the base of the PR and between 755d497 and 21b28a6.

📒 Files selected for processing (2)
  • web/default/src/features/system-settings/general/channel-affinity/rule-editor-dialog.tsx
  • web/default/src/features/system-settings/general/channel-affinity/types.ts
✅ Files skipped from review due to trivial changes (1)
  • web/default/src/features/system-settings/general/channel-affinity/rule-editor-dialog.tsx

Walkthrough

This PR adds support for extracting channel affinity keys from HTTP request headers. The backend extracts and trims header values based on a configured key name, while the frontend UI allows users to select and configure header-based affinity rules with appropriate validation and guidance.

Changes

Request Header Channel Affinity

Layer / File(s) Summary
Backend request header extraction and integration
service/channel_affinity.go, service/channel_affinity_template_test.go
extractChannelAffinityValue reads headers from Gin context with nil/empty guards and trims whitespace. New unit test verifies extraction and trimming from X-Affinity-Key header. Integration test configures a ChannelAffinityRule with request_header source, caches the result, and validates GetPreferredChannelByAffinity returns the correct channel and context metadata.
Frontend channel affinity settings UI
web/classic/src/pages/Setting/Operation/SettingsChannelAffinity.jsx, web/default/src/features/system-settings/general/channel-affinity/types.ts, web/default/src/features/system-settings/general/channel-affinity/rule-editor-dialog.tsx, setting/operation_setting/channel_affinity_setting.go
Settings form adds request_header to the selectable key source types. Validation logic accepts the new type alongside context-based types. Description text and input placeholder are updated to document header-based configuration and suggest X-Affinity-Key as the example header name. Client-side KeySource.type union includes 'request_header'.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐇 I nibble headers, soft and neat,
I trim the corners, make the key complete,
From X-Affinity-Key I gently peep,
Cached and guided, channels I keep,
A happy hop — affinity now talks sweet.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% 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 'feat: support request_header key source' clearly and concisely summarizes the main change—adding support for request headers as a key source for channel affinity.
Linked Issues check ✅ Passed The PR successfully implements the core requirement from issue #4896: enabling HTTP request headers as a key source for channel affinity, with supporting tests and UI updates across both backend and frontend.
Out of Scope Changes check ✅ Passed All changes are directly related to implementing the request_header key source feature, with no unrelated modifications detected in the backend logic, tests, or frontend components.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

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

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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.

@Calcium-Ion
Calcium-Ion merged commit 68830e6 into QuantumNous:main May 17, 2026
1 check passed
yanshifly added a commit to 580ai/fresh-api that referenced this pull request May 21, 2026
合并上游约 40 个新提交,包含:
- feat: support request_header key source (QuantumNous#4903)
- feat: add model performance metrics (PerfMetric) to dashboard
- feat: require compliance confirmation for paid features
- feat: track upstream request ID and prevent response header override
- feat: add DeepChat deeplink support (QuantumNous#4668)
- fix: filter perf metrics summary by active groups
- fix: respect dashboard content visibility settings
- fix: expose param override audits for sensitive message fields
- fix: apply group filter to channel list queries
- fix: correct usage logs filtering
- fix: allow clearing channel remark
- fix: GetAllChannels ignores group filter parameter
- fix: enforce header nav access control
- fix: handle unlimited API key quota validation
- fix: use getUserGroups for ratio display
- fix: register_enabled in /api/status gate sign-up link
- fix(auth): localize reset password confirmation, aff_code field name
- fix(web): normalize model detail tabs layout, pagination button labels
- fix(web/default): various new UI fixes
- fix: logger.LogDebug 格式化参数优化
- chore: add LICENSE, NOTICE, THIRD-PARTY-LICENSES files
- perf: avoid eager formatting in debug log calls
- chore(deps): update axios 1.15.2, ip-address 10.2.0

保留自定义功能:
- AWS 错误日志自动下架模型 (AwsModelBan)
- Claude 批量自动调度账号 (channel_anthropic_batch)
- 渠道失败率/RPM 展示与计算 (ChannelAutoEnable, TokenDailyStat)
- 操作日志系统 (OperationLog)
- 域名首页内容管理 (DomainHomeContent)
- 升级系统提示词功能
- 日常业务管理员权限
- 易支付回调兼容
- 渠道分组顺序箭头显示 (render.jsx)
- TopupNoticeCard 公告
- 视频任务退款防重复 (task.Quota = 0)
- 请求头过滤 (minimax/tts.go, openai/audio.go)

文件路径调整:
- 上游将 web/src/ 重命名为 web/classic/src/,新增 web/default/(新主题)
- 13 个自定义前端文件已移至 web/classic/ 对应路径
- login.png 放至 web/classic/public/(被 classic 前端引用)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
yiranxiaohui added a commit to yiranxiaohui/new-api that referenced this pull request May 25, 2026
Range: 18282e6..3b9ed0a8 (upstream/main as of fetch)

Highlights:
- feat: support request_header key source (QuantumNous#4903)
- feat: Waffo Pancake gateway + admin catalog binding (QuantumNous#4935)
- perf: optimize request metadata extraction, drop dead batch
  helpers in relay/channel/openai/helper.go (QuantumNous#5009)
- perf: reduce heap residency for large base64 relay requests
- fix(channel): evict auto-disabled multi-key channels from cache (QuantumNous#4983)
- fix: resolve model owned_by from active channels (QuantumNous#4416) — introduces
  channelOwnerName/getPreferredModelOwners/buildOpenAIModel + ListModels refactor
- fix: GetAllChannels respects group filter (QuantumNous#4847, QuantumNous#4885)
- fix(auth): expose register_enabled, aff_code, localize reset (QuantumNous#4871, QuantumNous#4945, QuantumNous#4769)
- fix(webhook): processing + Waffo subscription compliance (QuantumNous#5047, QuantumNous#5038)
- refactor(ui): system settings drill-in sidebar + log filter responsiveness

Conflicts resolved:
- controller/model.go: kept local hiddenMappedModels filter
  (resolveAccessibleModelGroups + getHiddenMappedModelNamesForGroups)
  on top of upstream's ListModels refactor; adopted upstream
  channelOwnerName helper.
- relay/channel/openai/helper.go: adopted upstream (HEAD's
  processChatCompletions/processCompletions were dead code after
  upstream's perf refactor in QuantumNous#5009).

Local patches verified intact: Username + fillTopUpUsernames (model/topup.go,
locked by topup_username_test.go), HideUpstreamErrors, Claude developer-role
normalization, Gemini role fallback, Model Chat header nav entry,
channel affinity auto-clear.

Note: go build not run (no Go toolchain in this environment); CI to verify.

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
xyfacai pushed a commit to xyfacai/new-api that referenced this pull request May 30, 2026
* feat: support request_header key source in backend and settings UI

* feat: support request_header channel affinity source
isboyjc added a commit to isboyjc/amux-api that referenced this pull request May 31, 2026
Sync upstream QuantumNous/new-api 68830e6, adapted to the fork's Semi
web/src UI (upstream touched web/classic + web/default).
- channel_affinity.go: add a request_header key source that reads the
  affinity key from a request header (trimmed).
- Settings UI (SettingsChannelAffinity.jsx): add request_header to the
  key-source options, require a key for it, update the help text and
  placeholder (X-Affinity-Key).
- Port the two upstream tests (fork's test file already uses testify).
SamuelSxy pushed a commit to SamuelSxy/new-api-rh that referenced this pull request Jun 7, 2026
* feat: support request_header key source in backend and settings UI

* feat: support request_header channel affinity source
fx247562340 pushed a commit to fx247562340/vancine-platform that referenced this pull request Jun 11, 2026
* feat: support request_header key source in backend and settings UI

* feat: support request_header channel affinity source
330079598 pushed a commit to 330079598/new-api that referenced this pull request Aug 19, 2026
* feat: support request_header key source in backend and settings UI

* feat: support request_header channel affinity source
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.

基于 Header 作为 Key 的亲和性能力

2 participants