Skip to content

feat: provider custom user agent#406

Closed
FaulknerWu wants to merge 1 commit into
ding113:devfrom
FaulknerWu:feature/provider-custom-user-agent
Closed

feat: provider custom user agent#406
FaulknerWu wants to merge 1 commit into
ding113:devfrom
FaulknerWu:feature/provider-custom-user-agent

Conversation

@FaulknerWu

@FaulknerWu FaulknerWu commented Dec 22, 2025

Copy link
Copy Markdown

Summary

Add custom User-Agent support for Providers, allowing each upstream provider to be configured with its own UA string.

为 Provider 添加自定义 User-Agent 支持,允许为每个供应商配置独立的 UA 字符串。

Problem

Some third-party API relay services (中转站) reject requests when using Codex endpoints, returning errors about not being allowed to run on third-party platforms. For example, Packy's monthly Codex subscription would fail with this error. This happens because the User-Agent header identifies the request as coming from a proxy.

某些中转站的 Codex 配置会报错提示不能在第三方平台运行(如 Packy 的包月 Codex)。Claude Code 本身没有遇到这个问题,但 Codex 类型的供应商需要能够自定义 User-Agent 来绕过这些限制。

Solution

Added a userAgent field to providers with the following priority:

  1. provider.userAgent (if configured)
  2. Client's original User-Agent (passthrough)
  3. Default UA: claude-code-hub/{version}

User-Agent 优先级:provider.userAgent > 客户端原始 UA > 默认 UA(claude-code-hub/{version}

Related PRs:

Changes

Core Changes

File Changes
src/drizzle/schema.ts Add user_agent column to providers table
src/app/v1/_lib/proxy/forwarder.ts Implement UA priority logic, replacing Codex-specific handling
src/types/provider.ts Add userAgent field to Provider interfaces
src/repository/provider.ts Include userAgent in CRUD operations

Supporting Changes

File Changes
src/app/[locale]/settings/providers/_components/forms/provider-form.tsx Add form input for custom User-Agent
messages/*.json (5 files) i18n translations for EN, JA, RU, zh-CN, zh-TW
src/actions/providers.ts Include userAgent in provider display
src/repository/_shared/transformers.ts Add userAgent to transformer

Database Migration

File Description
drizzle/0039_wet_sasquatch.sql ALTER TABLE "providers" ADD COLUMN "user_agent" varchar(512);

Breaking Changes

Change Impact Migration
New user_agent column added to providers table None - nullable column with no default Run bun run db:migrate or enable AUTO_MIGRATE=true

Behavior Change

Before: Codex providers had special handling that used client UA or a hardcoded fallback (codex_cli_rs/0.55.0...)

After: All provider types use unified logic:

  • If provider.userAgent is set → use it
  • Else if client provides UA → passthrough
  • Else → use claude-code-hub/{version}

This change is backward compatible. Existing providers without userAgent configured will continue to work with client UA passthrough.

Testing

  • TypeScript type check passes
  • Two test failures noted but unrelated to this PR:
    • E2E test requires running server
    • TimeZone configuration issue (pre-existing in dev branch)

Checklist

  • Code follows project conventions
  • Self-review completed
  • Target branch is dev
  • Database migration included
  • i18n translations added (5 locales)

Description enhanced by Claude AI

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @FaulknerWu, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求引入了一项重要功能,允许用户为每个配置的供应商自定义User-Agent字符串。这一改进旨在解决与某些第三方服务(特别是Codex中转站)的兼容性问题,这些服务可能因特定的User-Agent要求而无法正常工作。通过提供灵活的User-Agent配置选项,系统能够更好地适应不同的外部API行为,从而提高服务的稳定性和可用性。

Highlights

  • 自定义User-Agent支持: 为Provider(供应商)添加了自定义User-Agent配置,允许为每个供应商设置独立的User-Agent字符串,以解决某些第三方中转站的兼容性问题。
  • User-Agent优先级逻辑: 实现了User-Agent的优先级逻辑:provider.userAgent(供应商配置)> 客户端原始User-Agent > 默认User-Agent。如果供应商未配置且客户端未提供,将使用默认User-Agent。
  • 数据库架构更新: 在providers表中新增了user_agent字段,用于存储自定义的User-Agent字符串。
  • 前端界面支持: 在供应商设置表单中添加了新的输入字段,允许用户在UI中配置和管理自定义User-Agent。
  • 国际化支持: 为新的User-Agent配置字段添加了多语言支持,包括英文、日文、俄文、简体中文和繁体中文。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist 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.

Code Review

本次 PR 新增了为 Provider 自定义 User-Agent 的功能,这是一个非常实用的改进,解决了部分中转服务商对 User-Agent 有特殊要求的痛点。代码实现清晰、完整,覆盖了数据库、后端逻辑、前端表单等各个层面。

主要变更点:

  1. 数据库与类型定义:在 providers 表中新增了 user_agent 字段,并同步更新了相关的类型定义,结构清晰。
  2. 核心逻辑:在 forwarder.ts 中实现了 User-Agent 的优先级处理逻辑(provider.userAgent > 客户端原始 UA > 默认 UA),实现方式正确且优雅地替代了之前针对 codex 的硬编码逻辑。
  3. 前端支持:在供应商设置表单中添加了自定义 User-Agent 的输入框,UI 实现简洁明了,并且对空字符串做了 null 处理,保证了数据一致性。
  4. 代码一致性:新功能的添加贯穿了整个代码库,从数据模型到 API 服务再到 UI,修改一致且完整。

总体来说,这是一个高质量的 PR,代码整洁,逻辑严谨,很好地解决了实际问题。我没有发现任何需要修改的问题。

@ding113

ding113 commented Dec 22, 2025

Copy link
Copy Markdown
Owner

Packy 的包月 Codex 问题,目前建议通过 供应商 instruction策略选择透传解决。
这是似乎和 UA 无关。并且 CCH 也不会改变客户端的 UA。

关于这一功能,会在后续版本重构供应商时考虑。感谢你的贡献。

@ding113 ding113 closed this Dec 22, 2025
@github-project-automation github-project-automation Bot moved this from Backlog to Done in Claude Code Hub Roadmap Dec 22, 2025
@FaulknerWu

Copy link
Copy Markdown
Author

Packy 的包月 Codex 问题,目前建议通过 供应商 instruction策略选择透传解决。 这是似乎和 UA 无关。并且 CCH 也不会改变客户端的 UA。

关于这一功能,会在后续版本重构供应商时考虑。感谢你的贡献。

我这边两家codex现在35版本都是被认为第三方客户端,你说的方法我也试过了没管用。我是今天早上在priv群里看到有人问这个然后提到crs正常,我才看了crs的代码,仿照写了自定义UA,并且没有对源UA再做一层overrides的操作。这个开发分支在我本地上是可以正常接入了,在没修改前本地开发分支也是不能接入的,我感觉很玄学,但是在我这边确实是管用了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants