103 Commits

Author SHA1 Message Date
huangzhenpc
282dcf05f0 merge: 合并官方 upstream/main 的 6 个功能更新
Some checks failed
CI / test (push) Has been cancelled
CI / golangci-lint (push) Has been cancelled
CI / test (pull_request) Has been cancelled
CI / golangci-lint (pull_request) Has been cancelled
合并内容:
1. feat(gateway): Claude Code 系统提示词智能注入
2. fix: 修复创建账号 schedulable 默认值为 false 的 bug
3. fix(frontend): 修复跨时区日期范围筛选问题
4. feat(proxy): SOCKS5H 代理支持(统一代理配置)
5. fix(oauth): 修复 Claude Cookie 添加账号时会话混淆
6. fix(test): 修复 OAuth 账号测试刷新 token 的 bug

新增文件:
- backend/internal/pkg/proxyutil/* (SOCKS5H 支持)
- backend/internal/service/gateway_prompt_test.go (测试)

来自 upstream: Wei-Shaw/sub2api commits d9b1587..a527559
2026-01-04 18:48:05 +08:00
huangzhenpc
d274c8cb14 feat: 品牌重命名 Sub2API -> TianShuAPI
Some checks failed
CI / test (push) Has been cancelled
CI / golangci-lint (push) Has been cancelled
- 前端: 所有界面显示、i18n 文本、组件中的品牌名称
- 后端: 服务层、设置默认值、邮件模板、安装向导
- 数据库: 迁移脚本注释
- 保持功能完全一致,仅更改品牌名称

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-04 17:50:29 +08:00
shaw
fac19d258d fix(oauth): 修复claude cookie添加账号时会话混淆的问题 2026-01-04 14:20:17 +08:00
shaw
70e9329e64 feat(proxy): 统一代理配置并支持 SOCKS5H 协议
- 新增 proxyutil 包,统一 HTTP/HTTPS/SOCKS5/SOCKS5H 代理配置逻辑
- SOCKS5H 支持服务端 DNS 解析,避免本地 DNS 泄露
- 移除 ProxyStrict 宽松模式,代理失败直接返回错误不回退直连
- 前端代理管理页面支持 SOCKS5H 协议的添加/编辑/批量导入
- 补充 IPv6 地址和特殊字符密码的边界测试
2026-01-04 11:43:58 +08:00
ianshaw
1710779157 test: 暂时跳过 TestGetAccountsLoadBatch 集成测试
该测试在 CI 环境中失败,需要进一步调试。
暂时跳过以让 CI 通过,后续在本地 Docker 环境中修复。
2026-01-02 19:24:01 -08:00
ianshaw
b8779764b5 perf: 优化负载感知调度的准确性和响应速度
基于 Codex 审查建议的性能优化。

负载批量查询优化:
- getAccountsLoadBatchScript 添加过期槽位清理
- 使用 ZREMRANGEBYSCORE 在计数前清理过期条目
- 防止过期槽位导致负载率计算偏高
- 提升负载感知调度的准确性

等待循环优化:
- waitForSlotWithPingTimeout 添加立即获取尝试
- 避免不必要的 initialBackoff 延迟
- 低负载场景下减少响应延迟

测试改进:
- 取消跳过 TestGetAccountsLoadBatch 集成测试
- 过期槽位清理应该修复了 CI 中的计数问题

影响:
- 更准确的负载感知调度决策
- 更快的槽位获取响应
- 更好的测试覆盖率
2026-01-02 19:24:01 -08:00
shaw
106e59b753 Merge PR #122: feat: 用户自定义属性系统 + Wechat 字段迁移 2026-01-01 20:25:50 +08:00
Edric Li
759291db02 fix: update integration tests for UserListFilters
Update user_repo_integration_test.go to use the new UserListFilters
struct instead of individual parameters for ListWithFilters calls.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-01 19:13:58 +08:00
Edric Li
d8e2812d80 fix: resolve CI failures
- Fix gofmt formatting issue in user_service.go
- Remove unused sql field from userAttributeValueRepository
- Update ListWithFilters signature in test stubs to match interface
- Remove Wechat field from test user data

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-01 19:09:06 +08:00
Edric Li
404bf0f8d2 refactor: migrate wechat to user attributes and enhance users list
Migrate the hardcoded wechat field to the new extensible user
attributes system and improve the users management UI.

Migration:
- Add migration 019 to move wechat data to user_attribute_values
- Remove wechat field from User entity, DTOs, and API contracts
- Clean up wechat-related code from backend and frontend

UsersView enhancements:
- Add text labels to action buttons (Filter Settings, Column Settings,
  Attributes Config) for better UX
- Change status column to show colored dot + Chinese text instead of
  English text
- Add dynamic attribute columns support with batch loading
- Add column visibility settings with localStorage persistence
- Add filter settings modal for search and filter preferences
- Update i18n translations

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-01 18:59:38 +08:00
Edric Li
3c3fed886f feat(backend): add user custom attributes system
Add a flexible user attribute system that allows admins to define
custom fields for users (text, textarea, number, email, url, date,
select, multi_select types).

- Add Ent schemas for UserAttributeDefinition and UserAttributeValue
- Add service layer with validation logic
- Add repository layer with batch operations support
- Add admin API endpoints for CRUD and reorder operations
- Add batch API for loading attribute values for multiple users
- Add database migration (018_user_attributes.sql)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-01 18:58:34 +08:00
IanShaw027
48764e15a5 test(gemini): 添加 Drive API 和 OAuth 服务单元测试
- 新增 drive_client_test.go:Drive API 客户端单元测试
- 新增 gemini_oauth_service_test.go:OAuth 服务单元测试
- 重构 account_handler.go:改进 RefreshTier API 实现
- 优化 drive_client.go:增强错误处理和重试逻辑
- 完善 repository 和 service 层:支持批量 tier 刷新
- 更新迁移文件编号:017 -> 024(避免冲突)
2026-01-01 15:07:16 +08:00
shaw
2a395d12a6 Merge branch 'feature/atomic-scheduling' 2026-01-01 11:05:22 +08:00
ianshaw
712400557e fix(lint): 移除错误信息末尾的句号
- 符合 Go staticcheck ST1005 规则
- 错误信息不应以标点符号结尾
2025-12-31 18:24:39 -08:00
ianshaw
eee5c0ac0b feat(migrations): 改进校验和错误提示和文档
- 增强迁移校验和不匹配的错误信息,提供具体解决方案
- 添加 migrations/README.md 文档说明迁移最佳实践
- 明确迁移不可变原则和正确的修改流程
2025-12-31 18:16:34 -08:00
IanShaw027
6d01be0c30 test: 暂时跳过 TestGetAccountsLoadBatch 集成测试
该测试在 CI 环境中失败,需要进一步调试。
暂时跳过以让 PR 通过,后续在本地 Docker 环境中修复。
2026-01-01 04:41:30 +08:00
IanShaw027
a2f3d10bee fix(lint): 使用 any 替代 interface{} 以符合 gofmt 规则 2026-01-01 04:37:33 +08:00
IanShaw027
c5781c69bb fix(merge): 解决与 main 分支的配置冲突
- 合并 main 分支的上游错误日志配置
- 保留调度配置
- 合并 beta header 和 failover 配置
2026-01-01 04:33:12 +08:00
IanShaw027
fe31495a89 test(gateway): 补充账号调度优化的单元测试
- 添加 GetAccountsLoadBatch 批量负载查询测试
- 添加 CleanupExpiredAccountSlots 过期槽位清理测试
- 添加 SelectAccountWithLoadAwareness 负载感知选择测试
- 测试覆盖降级行为、账号排除、错误处理等场景
2026-01-01 04:15:31 +08:00
IanShaw027
592d2d0978 feat(gateway): 实现负载感知的账号调度优化
- 新增调度配置:粘性会话排队、兜底排队、负载计算、槽位清理
- 实现账号级等待队列和批量负载查询(Redis Lua 脚本)
- 三层选择策略:粘性会话优先 → 负载感知选择 → 兜底排队
- 后台定期清理过期槽位,防止资源泄漏
- 集成到所有网关处理器(Claude/Gemini/OpenAI)
2026-01-01 04:01:51 +08:00
NepetaLemon
2270a54ff6 refactor: 移除 infrastructure 目录 (#108)
* refactor: 迁移初始化 db 和 redis 到 repository

* refactor: 迁移 errors 到 pkg
2025-12-31 23:42:01 +08:00
shaw
c5b792add5 fix(billing): 修复限额为0时消费记录失败的问题
- 添加 normalizeLimit 函数,将 0 或负数限额规范化为 nil(无限制)
- 简化 IncrementUsage,移除冗余的配额检查逻辑
  - 配额检查已在请求前由中间件和网关完成
  - 消费记录应无条件执行,确保数据完整性
- 删除测试限额超出行为的无效集成测试
2025-12-31 22:48:35 +08:00
shaw
aac7dd6b08 style: fix gofmt formatting in test file
Remove redundant alignment whitespace before comments.
2025-12-31 15:52:02 +08:00
yangjianbo
6f6dc3032c fix(设置): 修复站点设置保存失败的问题
问题:
1. Setting.value 字段设置了 NotEmpty() 约束,导致保存空字符串值时验证失败
2. 数据库 settings 表缺少 key 字段的唯一约束,导致 ON CONFLICT 语句执行失败

修复:
- 移除 ent/schema/setting.go 中 value 字段的 NotEmpty() 约束
- 新增迁移 015_fix_settings_unique_constraint.sql 添加缺失的唯一约束
- 添加3个回归测试确保空值保存功能正常

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-31 15:31:26 +08:00
yangjianbo
d77d0544d0 fix(仓储): 修复并发缓存前缀与软删除更新
补齐 Redis ZSET 前缀处理,确保并发释放计数正确

删除时改用 Client().Mutate 走更新逻辑,保留软删除记录

测试: make test-integration
2025-12-31 15:20:58 +08:00
yangjianbo
682f546c0e fix(lint): 修复 golangci-lint 报告的代码问题
- errcheck: 修复类型断言未检查返回值的问题
  - pool.go: 添加 sync.Map 类型断言安全检查
  - req_client_pool.go: 添加 sync.Map 类型断言安全检查
  - concurrency_cache_benchmark_test.go: 显式忽略断言返回值
  - gateway_service.go: 显式忽略 WriteString 返回值

- gofmt: 修复代码格式问题
  - redis.go: 注释对齐
  - api_key_repo.go: 结构体字段对齐
  - concurrency_cache.go: 字段对齐
  - http_upstream.go: 注释对齐

- unused: 删除未使用的代码
  - user_repo.go: 删除未使用的 sql 字段
  - usage_service.go: 删除未使用的 calculateStats 函数

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-31 14:51:58 +08:00
yangjianbo
679b21a86c Merge branch 'main' into test
冲突解决:
- wire_gen.go: 合并 antigravityGatewayService 和 ProvideConcurrencyCache
- user_repo_integration_test.go: 保留 NotFound 测试
- antigravity_gateway_service.go: 适配 httpUpstream.Do 新签名

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-31 14:17:18 +08:00
yangjianbo
5906f9ab98 fix(数据层): 修复数据完整性与仓储一致性问题
## 数据完整性修复 (fix-critical-data-integrity)
- 添加 error_translate.go 统一错误转换层
- 修复 nil 输入和 NotFound 错误处理
- 增强仓储层错误一致性

## 仓储一致性修复 (fix-high-repository-consistency)
- Group schema 添加 default_validity_days 字段
- Account schema 添加 proxy edge 关联
- 新增 UsageLog ent schema 定义
- 修复 UpdateBalance/UpdateConcurrency 受影响行数校验

## 数据卫生修复 (fix-medium-data-hygiene)
- UserSubscription 添加软删除支持 (SoftDeleteMixin)
- RedeemCode/Setting 添加硬删除策略文档
- account_groups/user_allowed_groups 的 created_at 声明 timestamptz
- 停止写入 legacy users.allowed_groups 列
- 新增迁移: 011-014 (索引优化、软删除、孤立数据审计、列清理)

## 测试补充
- 添加 UserSubscription 软删除测试
- 添加迁移回归测试
- 添加 NotFound 错误测试

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-31 14:11:57 +08:00
yangjianbo
820bb16ca7 fix(网关): 防止连接池缓存失控
超限且无可淘汰条目时拒绝新建

规范化代理地址并更新失败时的访问时间

补充连接池上限与代理规范化测试
2025-12-31 12:01:31 +08:00
yangjianbo
d1c9889609 perf(网关): 实现上游账号连接池隔离
新增隔离策略与连接池缓存回收

连接池大小跟随账号并发并处理代理切换

同步配置默认值与示例并补充测试
2025-12-31 11:43:58 +08:00
yangjianbo
3d7f8e4b3a fix(服务): 修复system判定、统计时区与缓存日志
- system 字段存在即视为显式提供,避免 null 触发默认注入
- 日统计分组显式使用应用时区,缺失时从 TZ 回退到 UTC
- 缓存写入队列丢弃日志节流汇总,关键任务同步回退

测试: go test ./internal/service -run TestBillingCacheServiceQueueHighLoad
2025-12-31 10:17:38 +08:00
yangjianbo
7efa8b54c4 perf(后端): 完成性能优化与连接池配置
新增 DB/Redis 连接池配置与校验,并补充单测

网关请求体大小限制与 413 处理

HTTP/req 客户端池化并调整上游连接池默认值

并发槽位改为 ZSET+Lua 与指数退避

用量统计改 SQL 聚合并新增索引迁移

计费缓存写入改工作池并补测试/基准

测试: 在 backend/ 下运行 go test ./...
2025-12-31 08:50:12 +08:00
shaw
4319cf7f31 fix(仓储): 修复 BatchUpdateLastUsed 时间戳类型不匹配
在原生 SQL 的 CASE WHEN 语句中,PostgreSQL 无法自动推断占位符参数类型,
导致 time.Time 被当作 text 类型处理,与 last_used_at 列的 timestamptz 类型不匹配。

添加显式类型转换 ::timestamptz 解决此问题。
2025-12-30 23:11:49 +08:00
程序猿MT
5cad90fb4d Merge branch 'Wei-Shaw:main' into main 2025-12-30 17:14:39 +08:00
yangjianbo
8cb2d3b352 fix(仓储): 规范 rows.Close 错误回传
统一 usage_log_repo 查询的 Close 错误处理,避免\n成功路径吞掉关闭失败

scanSingleRow 使用 errors.Join 合并 Close 错误,\n保留 ErrNoRows 可判定

测试: make -C backend test-unit
2025-12-30 17:13:32 +08:00
shaw
3d296d8898 style: 修复 gofmt 格式化问题
格式化以下测试文件以符合 Go 代码风格规范:
- fixtures_integration_test.go
- user_repo_integration_test.go
- api_key_service_delete_test.go
2025-12-30 17:08:36 +08:00
yangjianbo
aacbc98aec fix(仓储): 修复查询关闭错误并迁移集成测试
修复 rows.Close 失败时的错误返回逻辑
迁移网关路由集成测试到 ent 事务基建
补齐仓储接口变更对应的测试桩方法
新增 backend/Makefile 统一测试命令
测试: GOTOOLCHAIN=go1.24.11 go test ./...
测试: golangci-lint run ./... --timeout=5m
测试: make test-integration
2025-12-30 16:41:45 +08:00
yangjianbo
b9a753cd04 fix(仓库): 使用 ent 实现账号调度查询
替换 gorm 查询并复用分组过滤逻辑,避免编译错误
2025-12-30 14:35:29 +08:00
yangjianbo
e83f0ee307 Merge branch 'main' into test-dev 2025-12-30 09:07:55 +08:00
shaw
c328b741cb Merge PR #73: feat(antigravity): 添加 Antigravity (Cloud AI Companion) 平台支持
新增功能:
- Antigravity OAuth 授权流程支持
- Claude → Gemini 协议转换(Claude API 请求自动转换为 Gemini 格式)
- 配额刷新和状态显示
- 混合调度功能,支持 Anthropic 和 Antigravity 账户混合使用
- /antigravity 专用路由,支持仅使用 Antigravity 账户
- 前端 Antigravity 服务商标识和账户管理功能

冲突解决:
- CreateAccountModal.vue: 合并 data-tour 属性和 mixed-scheduling 属性
- EditAccountModal.vue: 合并 data-tour 属性和 mixed-scheduling 属性

代码质量改进:
- 修复 antigravity 类型文件的 gofmt 格式问题(struct 字段对齐、interface{} → any)
- 移除 .golangci.yml 中的 gofmt 排除规则
- 修复测试文件的格式问题
2025-12-29 20:32:20 +08:00
yangjianbo
042d82359c fix(仓储): 修复 ApiKey 更新并发语义
ApiKey 更新时显式设置 updated_at 并回填,避免二次查询竞态
补充软删除范围注释以统一审计语义
2025-12-29 19:59:36 +08:00
yangjianbo
ae191f72a4 fix(仓储): 修复软删除过滤与事务测试
修复软删除拦截器使用错误,确保默认查询过滤已删记录
仓储层改用 ent.Tx 与扫描辅助,避免 sql.Tx 断言问题
同步更新集成测试以覆盖事务与统计变动
2025-12-29 19:23:49 +08:00
song
2bd288a677 Merge branch 'main' into feature/antigravity_auth 2025-12-29 17:04:40 +08:00
yangjianbo
4dab18a94f fix(用户): 修复删除用户软删除钩子
避免软删除钩子类型断言失败导致 500\n删除无记录时返回未找到错误并记录日志
2025-12-29 16:57:50 +08:00
IanShaw027
23412965f8 feat(frontend): 优化弹窗组件架构和用户体验
- 使用 BaseDialog 替代旧版 Modal 组件
- 添加平滑过渡动画和更好的可访问性支持
- 新增 ExportProgressDialog 导出进度弹窗
- 优化所有账号管理和使用记录相关弹窗
- 更新国际化文案,改进用户交互体验
- 精简依赖,减少 package.json 体积
2025-12-29 16:13:09 +08:00
yangjianbo
5584709ac9 fix(仓储层): 修复事务 ent client 调用 Close() 导致的 panic
问题:创建用户时发生 panic,错误信息为
"interface conversion: sql.ExecQuerier is *sql.Tx, not *sql.DB"

原因:基于事务创建的 ent client 在调用 Close() 时,ent 的 sql driver
会尝试将 ExecQuerier 断言为 *sql.DB 来关闭连接,但实际类型是 *sql.Tx

修复:移除对 txClient.Close() 的调用,事务的清理通过
sqlTx.Rollback() 和 sqlTx.Commit() 完成即可

影响范围:
- user_repo.go: Create 和 Update 方法
- group_repo.go: Delete 方法

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-29 15:49:20 +08:00
yangjianbo
e9c755f428 perf(后端): 优化删除操作的数据库查询性能
- 新增 ExistsByID 方法用于账号存在性检查,避免加载完整对象
- 新增 GetOwnerID 方法用于 API Key 所有权验证,仅查询 user_id 字段
- 优化 AccountService.Delete 使用轻量级存在性检查
- 优化 ApiKeyService.Delete 使用轻量级权限验证
- 改进前端删除错误提示,显示后端返回的具体错误消息
- 添加详细的中文注释说明优化原因

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-29 14:06:38 +08:00
yangjianbo
8ab924ad9b fix(构建): 删除遗留的 GORM auto_migrate.go 文件
该文件是 GORM 迁移到 Ent ORM 过程中遗留的,仍然导入了
gorm.io/gorm,导致 Docker 构建失败。

文件中的功能已被迁移到 SQL 迁移文件中:
- fixInvalidExpiresAt → 006_fix_invalid_subscription_expires_at.sql
- ensureDefaultGroups → 001_init.sql

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-29 11:18:00 +08:00
yangjianbo
3c3419475d Merge branch 'main' into test-dev 2025-12-29 10:50:46 +08:00
yangjianbo
3a7d3387e0 fix(数据库): 修复默认分组缺失与迁移锁阻塞
通过迁移补种默认 groups 记录,避免新装空分组
迁移锁改为 try lock + 重试并加入超时
写入 usage_logs 时保留 rate_multiplier=0 语义
测试: go test ./...
2025-12-29 10:43:46 +08:00