Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

feat(account): 支持账号级别自定义 User-Agent#760

Open
CharZhou wants to merge 9 commits intoWei-Shaw:mainfrom
CharZhou:feature/account-user-agent
Open

feat(account): 支持账号级别自定义 User-Agent#760
CharZhou wants to merge 9 commits intoWei-Shaw:mainfrom
CharZhou:feature/account-user-agent

Conversation

@CharZhou
Copy link

@CharZhou CharZhou commented Mar 4, 2026

背景

关联 Issue #753

管理员希望能在账号维度配置发往上游 API 的 HTTP User-Agent,以满足不同账号使用不同客户端特征的需求。

本次改动

数据库

  • 新增 accounts.user_agent 字段(nullable VARCHAR(200))
  • 迁移文件:backend/migrations/065_add_account_user_agent.sql

后端

  • ent schema:新增 user_agent 字段定义及生成代码
  • repository 层:Create/Update 时写入 user_agent;entity→service 映射
  • OAuth 客户端:Claude / OpenAI / Gemini OAuth token 操作(exchange/refresh)支持账号级 UA 覆盖
  • 网关代理请求:所有平台(Anthropic / OpenAI / Gemini)的网关转发请求支持账号级 UA 覆盖
    • Anthropic API key passthrough
    • Anthropic OAuth 主路径(无 fingerprint 时覆盖,有 fingerprint 时由 identity_service 管理)
    • OpenAI passthrough + OAuth 主路径(优先 user_agent 字段,降级到旧 credentials.user_agent 兼容)
    • Gemini code assist 请求(有值则覆盖,无值降级到 GeminiCLIUserAgent 默认值)
  • handler 层:Create/Update API 接收 user_agent 字段
  • 响应 DTOGET /accounts 及详情接口返回 user_agent 字段
  • service 层normalizeUserAgent() 处理空字符串(清空逻辑)

前端

  • EditAccountModal.vue 新增 User-Agent 可选输入框
  • TypeScript 类型定义新增 user_agent?: string

文档

  • README.md 新增 Custom User-Agent 功能说明

优先级规则

平台 优先级
Anthropic 账号 user_agent > 客户端透传 UA > Go 默认
OpenAI/Codex 账号 user_agent > credentials.user_agent(旧字段兼容)> ForceCodexCLI > OAuth 兜底
Gemini 账号 user_agent > GeminiCLIUserAgent 默认值

不覆盖: Sora(固定设备指纹 UA)

测试

  • go build ./...
  • go test -tags unit ./...
  • API 集成测试:创建/更新/查询账号 user_agent 字段均正常 ✅
  • Mock 上游服务器验证:账号设置 UA 后,上游收到的请求头为账号配置值;清除后透传客户端 UA ✅

小黑 and others added 8 commits March 4, 2026 13:08
- Added user_agent field to Account schema (backend/ent/schema/account.go)
- Added user_agent to Account TypeScript interface (frontend/src/types/index.ts)
- Added IMPLEMENTATION_NOTES.md with integration guide for maintainer

Backend HTTP header integration is documented in IMPLEMENTATION_NOTES.md.

Closes Wei-Shaw#753
- Added user_agent field to Account schema (ent auto-generated)
- Updated ClaudeOAuthClient / OpenAIOAuthClient / GeminiCliCodeAssistClient interfaces
- RefreshAccountToken reads account.UserAgent and passes to OAuth client
- go build ./... passes

Closes Wei-Shaw#753
- Repository layer: read userAgent param and set HTTP header
- Frontend: add User-Agent input field in account edit form

Closes Wei-Shaw#753
- Verified ent migration schema includes user_agent field
- Added SQL migration 065_add_account_user_agent.sql for production deploy
- Added feature documentation to README Features section
- Ready for production deployment

Closes Wei-Shaw#753
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Anthropic passthrough: account user_agent overrides client-forwarded UA
- Anthropic OAuth: account user_agent applied when no TLS fingerprint active
- OpenAI (passthrough + standard): account user_agent takes priority over legacy credentials.user_agent
- Gemini: account user_agent overrides default GeminiCLIUserAgent

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@CharZhou
Copy link
Author

CharZhou commented Mar 5, 2026

🖼️ UI 效果截图

编辑账号 - User-Agent 字段展示

在账号管理的编辑账号 modal 中,新增了 User-Agent(可选) 字段:

  • 留空时使用系统默认值
  • 填入自定义值后,该账号的所有上游 API 请求将使用指定的 User-Agent

User-Agent 字段(空)

填入自定义 User-Agent 值

User-Agent 字段(已填写)

本地完整功能测试通过:字段展示正常、保存成功、无报错。

Copy link

@6639835 6639835 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, ty

…r-agent

# Conflicts:
#	frontend/src/types/index.ts
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.

2 participants