yangjianbo
10636d8a1f
fix(codex): 修复额度窗口过期展示并补齐高覆盖测试
...
- 后端新增绝对重置时间字段计算(codex_5h_reset_at/codex_7d_reset_at)
- 前端统一窗口解析逻辑:绝对时间优先,updated_at+seconds 回退,过期自动归零
- 新增后端与前端单元测试,覆盖关键边界与异常场景
2026-02-22 21:04:52 +08:00
yangjianbo
d04b47b3ca
feat(backend): 提交后端审计修复与配套测试改动
2026-02-14 11:23:10 +08:00
yangjianbo
57e8abcb63
fix(openai): 自动透传预检 instructions 并本地 403 拦截
2026-02-14 10:49:01 +08:00
yangjianbo
ed31c54961
fix(openai): 拒绝日志记录原始 User-Agent 便于攻击研判
2026-02-14 09:59:19 +08:00
yangjianbo
4bfa69bffa
fix(openai): 仅记录 codex_cli_only 拒绝日志并输出详细 User-Agent
2026-02-14 09:53:17 +08:00
yangjianbo
f96acf6e27
fix(ops): 修复日志级别过滤并增强OpenAI错误诊断日志
...
- 移除 warn 级别下 access info 的强制入库补写,确保运行时日志级别真实生效
- 将 OpenAI fallback matched 与 passthrough 断流提示按需求降级为 info
- 为 codex_cli_only 与 instructions required 场景补充请求诊断字段(含 User-Agent)
- 出于安全考虑移除请求体预览,仅保留 request_body_size 与白名单头信息
- 新增/更新回归测试,覆盖 Forward 入口到日志落库链路
2026-02-13 19:27:07 +08:00
yangjianbo
2459eafb71
feat: 完善日志
2026-02-13 13:35:47 +08:00
yangjianbo
a9518cc5be
feat(openai): 增加 OAuth 账号 Codex 官方客户端限制开关
...
新增 codex_cli_only 开关并默认关闭,关闭时完全绕过限制逻辑。
在 OpenAI 网关引入统一检测入口,集中判定账号类型、开关与客户端族。
开启后仅放行 codex_cli_rs、codex_vscode、codex_app 客户端家族。
补充后端判定与网关分支测试,并在前端创建/编辑页增加开关配置与回显。
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com >
2026-02-12 22:32:59 +08:00
yangjianbo
2f190d812a
fix(openai): 透传OAuth强制store/stream并修复Codex识别
2026-02-12 21:02:52 +08:00
yangjianbo
d411cf4472
fix(openai): 收敛自动透传请求头并增强 OAuth 安全兜底
2026-02-12 20:12:15 +08:00
yangjianbo
584cfc3db2
chore(logging): 完成后端日志审计与结构化迁移
...
- 将高密度服务与处理器日志迁移到新日志系统(LegacyPrintf/结构化日志)
- 增加 stdlog bridge 与兼容测试,保留旧日志捕获能力
- 将 OpenAI 断流告警改为结构化 Warn 并改造对应测试为 sink 捕获
- 补齐后端相关文件 logger 引用并通过全量 go test
2026-02-12 19:01:09 +08:00
yangjianbo
ed2eba9028
fix(gateway): 默认过滤OpenAI透传超时头并补充断流告警
2026-02-12 14:16:18 +08:00
yangjianbo
6533a4647d
fix(openai): 增强自动透传命中日志
2026-02-12 11:41:06 +08:00
yangjianbo
9c910c2049
feat(openai): 支持自动透传开关并透传 User-Agent
...
- OpenAI OAuth/API Key 统一支持自动透传开关,编辑页可开关\n- 透传模式仅替换认证并保留计费/并发/审计,修复 API Key responses 端点拼接\n- Usage 页面显示原始 User-Agent 且不截断,补充回归测试与清单
2026-02-12 10:56:07 +08:00
yangjianbo
61a2bf469a
feat(openai): 极致优化 OAuth 链路并补齐性能守护
...
- 优化 /v1/responses 热路径,减少重复解析与不必要拷贝\n- 优化并发与 token 竞争路径并补齐运行指标\n- 补充 OpenAI/Ops 相关单元测试与回归用例\n- 新增灰度阈值守护与压测脚本,支撑发布验收
2026-02-12 09:41:37 +08:00
yangjianbo
a88bb8684f
fix(openai): 修复 OAuth 透传流式断开与压缩头问题
...
- 透传流式在客户端断开后继续 drain 上游并解析 usage,避免计费信息丢失
- 阻断透传 accept-encoding,避免压缩响应影响 SSE/usage 解析
- 阻断 proxy-authorization,避免透传代理鉴权信息
- 补充回归测试:请求头阻断与断流后 usage 采集
2026-02-11 22:17:38 +08:00
yangjianbo
f1e884ce2b
feat(openai): 增加 OAuth 透传开关
...
- 仅对 Codex CLI 且账号开启时走原样透传(只替换认证)
- 透传模式禁用工具修正/模型替换,并旁路解析 usage 用于计费
- 管理后台增加开关与文案,ops upstream error 记录 passthrough 标记
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com >
2026-02-11 00:59:39 +08:00
yangjianbo
58912d4ac5
perf(backend): 使用 gjson/sjson 优化热路径 JSON 处理
...
将 API 网关热路径中的 json.Unmarshal+json.Marshal 替换为 gjson 零拷贝查询和 sjson 精准写入:
- unwrapV1InternalResponse 性能提升 22x(4009ns→182ns),内存分配减少 28.5x
- unwrapGeminiResponse、extractGeminiUsage、estimateGeminiCountTokens、ParseGeminiRateLimitResetTime 改为接收 []byte 使用 gjson 提取
- ParseGatewayRequest 的 model/stream/metadata/thinking/max_tokens 改用 gjson 类型安全提取
- Handler 层(sora/openai)改用 gjson 提取字段、sjson 注入/修改字段,移除 map[string]any 中间变量
- Sora Client 响应解析改用 gjson ForEach 遍历,减少内存分配
- 新增约 100 个单元测试用例,所有改动函数覆盖率 >85%
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com >
2026-02-10 08:59:30 +08:00
yangjianbo
16131c3d3f
Merge branch 'main' of https://github.com/mt21625457/aicodex2api
2026-02-09 20:26:03 +08:00
erio
1af06aed96
feat: shuffle accounts within same sort group to prevent thundering herd
...
Add post-sort shuffle for accounts with identical (priority, loadRate,
lastUsedAt) to break deterministic ordering when concurrent requests
read the same scheduler snapshot. Applies to both Antigravity and
OpenAI scheduling paths, plus the sortAccountsByPriorityAndLastUsed
helper.
Keeps upstream CallCount/ModelLoadInfo scheduling intact; shuffle is
additive and only randomises within equivalent-rank groups.
2026-02-09 07:33:17 +08:00
yangjianbo
a14dfb769a
Merge branch 'dev-release'
2026-02-07 19:58:00 +08:00
yangjianbo
8226a4ce4d
perf(service): 优化 model 替换函数,用 gjson/sjson 替代全量 JSON 序列化
...
SSE 热路径中 replaceModelInSSELine 和 replaceModelInResponseBody 原来
使用 json.Unmarshal/Marshal 对每个事件做全量反序列化再序列化,现改为
gjson.Get/sjson.Set 精确字段操作,消除 O(n) 中间 map 分配,保持 JSON
字段顺序不变。涉及 OpenAIGatewayService 和 GatewayService 两个服务。
新增 23 个单元测试覆盖:顶层/嵌套 model 替换、不匹配跳过、空行/[DONE]/
非法 JSON 等边界情况。
Fixes: P1-08
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com >
2026-02-07 17:09:55 +08:00
yangjianbo
a9e256ce8c
fix(openai): 修复 usage 为空导致 panic(P0-02)
2026-02-07 16:15:30 +08:00
erio
edb0937024
fix: restore non-failover error passthrough from 7b156489
2026-02-07 14:24:55 +08:00
erio
5e98445b22
feat(antigravity): comprehensive enhancements - model mapping, rate limiting, scheduling & ops
...
Key changes:
- Upgrade model mapping: Opus 4.5 → Opus 4.6-thinking with precise matching
- Unified rate limiting: scope-level → model-level with Redis snapshot sync
- Load-balanced scheduling by call count with smart retry mechanism
- Force cache billing support
- Model identity injection in prompts with leak prevention
- Thinking mode auto-handling (max_tokens/budget_tokens fix)
- Frontend: whitelist mode toggle, model mapping validation, status indicators
- Gemini session fallback with Redis Trie O(L) matching
- Ops: enhanced concurrency monitoring, account availability, retry logic
- Migration scripts: 049-051 for model mapping unification
2026-02-07 12:31:10 +08:00
shaw
7b1564898b
fix: make error passthrough effective for non-failover upstream errors
2026-02-07 10:25:56 +08:00
yangjianbo
ce764bf2d9
feat(gateway): 支持强制 Codex CLI 模式并伪装 UA
...
- Codex CLI 请求仅使用内置 instructions,不再读取 opencode 缓存/回源\n- 新增 gateway.force_codex_cli(环境变量 GATEWAY_FORCE_CODEX_CLI)\n- ForceCodexCLI=true 时转发上游强制 User-Agent=codex_cli_rs/0.0.0\n- 更新 deploy 示例配置
2026-02-07 09:21:15 +08:00
yangjianbo
d71537d431
perf(service): SSE Scanner buffer 改用 sync.Pool 复用,减少高并发 GC 压力
...
将流式响应中 bufio.Scanner 的 64KB buffer 从每次 make 分配改为
sync.Pool 复用,统一切片表达式为 [:0]、变量命名为 scanBuf,
并补充对应的单元测试。
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com >
2026-02-06 22:55:12 +08:00
shaw
39e05a2dad
feat: 新增全局错误透传规则功能
...
支持管理员配置上游错误如何返回给客户端:
- 新增 ErrorPassthroughRule 数据模型和 Ent Schema
- 实现规则的 CRUD API(/admin/error-passthrough-rules)
- 支持按错误码、关键词匹配,支持 any/all 匹配模式
- 支持按平台过滤(anthropic/openai/gemini/antigravity)
- 支持透传或自定义响应状态码和错误消息
- 实现两级缓存(Redis + 本地内存)和多实例同步
- 集成到 gateway_handler 的错误处理流程
- 新增前端管理界面组件
- 新增单元测试覆盖核心匹配逻辑
优化:
- 移除 refreshLocalCache 中的冗余排序(数据库已排序)
- 后端 Validate() 增加匹配条件非空校验
2026-02-05 21:52:54 +08:00
Payne Fu
fecfaae8dc
fix: remove unsupported safety_identifier and previous_response_id fields from upstream requests
...
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com >
2026-02-04 15:56:01 +08:00
liuxiongfeng
9a48b2e942
fix(openai): 统一 OAuth instructions 处理逻辑,修复 Codex CLI 400 错误
...
- 修改 applyCodexOAuthTransform 函数签名,增加 isCodexCLI 参数
- 移除 && !isCodexCLI 条件,对所有 OAuth 请求统一处理
- 新增 applyInstructions/applyCodexCLIInstructions/applyOpenCodeInstructions 辅助函数
- 新增 isInstructionsEmpty 函数检查 instructions 字段是否为空
- 添加 Codex CLI 和非 Codex CLI 场景的测试用例
逻辑说明:
- Codex CLI + 有 instructions: 保持不变
- Codex CLI + 无 instructions: 补充 opencode 指令
- 非 Codex CLI: 使用 opencode 指令覆盖
2026-02-03 21:22:33 +08:00
bayma888
6146be1474
feat(api-key): add independent quota and expiration support
...
This feature allows API Keys to have their own quota limits and expiration
times, independent of the user's balance.
Backend:
- Add quota, quota_used, expires_at fields to api_key schema
- Implement IsExpired() and IsQuotaExhausted() checks in middleware
- Add ResetQuota and ClearExpiration API endpoints
- Integrate quota billing in gateway handlers (OpenAI, Anthropic, Gemini)
- Include quota/expiration fields in auth cache for performance
- Expiration check returns 403, quota exhausted returns 429
Frontend:
- Add quota and expiration inputs to key create/edit dialog
- Add quick-select buttons for expiration (+7, +30, +90 days)
- Add reset quota confirmation dialog
- Add expires_at column to keys list
- Add i18n translations for new features (en/zh)
Migration:
- Add 045_add_api_key_quota.sql for new columns
2026-02-03 19:49:31 +08:00
Wesley Liddick
bb3df5785a
Merge pull request #322 from xlx0852/main
...
fix: 混合渠道警告确认框和过滤 prompt_cache_retention 参数
2026-02-03 15:13:10 +08:00
Wesley Liddick
df4c0adf0b
Merge pull request #463 from DuckyProject/feat/usage-records-codex-reasoning-effort
...
feat(usage): add reasoning effort column
2026-02-03 14:57:45 +08:00
ducky
53ee6383db
feat(usage): add reasoning effort column
2026-02-03 14:36:29 +08:00
cyhhao
a161fcc89b
Merge branch 'main' of github.com:Wei-Shaw/sub2api
2026-01-26 10:44:38 +08:00
shaw
74e05b83ea
fix(ratelimit): 修复 OpenAI 账号限流倒计时计算错误
...
- 解析 x-codex-* 响应头获取正确的重置时间
- 7d 限制用尽时使用 codex_7d_reset_after_seconds
- 提取 Normalize() 方法统一窗口规范化逻辑
2026-01-25 13:32:08 +08:00
cyhhao
65e69738cc
Merge branch 'main' of github.com:Wei-Shaw/sub2api
2026-01-20 22:46:23 +08:00
cyhhao
c8e2f614fa
Merge branch 'main' of github.com:Wei-Shaw/sub2api
2026-01-20 13:53:32 +08:00
yangjianbo
f6ed3d1456
Merge branch 'test' into dev
2026-01-20 11:59:13 +08:00
yangjianbo
91f01309da
fix(调度): 完善粘性会话清理与账号调度刷新
...
- Update/BulkUpdate 按不可调度字段触发缓存刷新
- GatewayCache 支持多前缀会话键清理
- 模型路由与混合调度优化粘性会话处理
- 补充调度与缓存相关测试覆盖
2026-01-20 11:40:55 +08:00
cyhhao
26298c4a5f
fix(openai): emit OpenAI-compatible SSE error events
2026-01-19 13:53:39 +08:00
cyhhao
6901b64fce
merge: sync upstream changes
2026-01-17 18:30:16 +08:00
nick8802754751
4e75d8fda9
fix: 添加混合渠道警告确认框和过滤 prompt_cache_retention 参数
...
- 前端: EditAccountModal 和 CreateAccountModal 添加 409 mixed_channel_warning 处理
- 前端: 弹出确认框让用户确认混合渠道风险
- 后端: 过滤 OpenAI 请求中的 prompt_cache_retention 参数(上游不支持)
- 添加中英文翻译
Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
2026-01-17 16:06:44 +08:00
IanShaw027
ae21db77ec
fix(openai): 使用 prompt_cache_key 兜底粘性会话
...
opencode 请求不带 session_id/conversation_id,导致粘性会话失效。现在按 header 优先、prompt_cache_key 兜底生成 session hash,并补充单测验证优先级。
2026-01-17 02:31:16 +08:00
IanShaw027
539b41f421
feat(openai): 添加Codex工具调用自动修正功能
...
实现了完整的Codex工具调用拦截和自动修正系统,解决OpenCode使用Codex模型时的工具调用兼容性问题。
**核心功能:**
1. **工具名称自动映射**
- apply_patch/applyPatch → edit
- update_plan/updatePlan → todowrite
- read_plan/readPlan → todoread
- search_files/searchFiles → grep
- list_files/listFiles → glob
- read_file/readFile → read
- write_file/writeFile → write
- execute_bash/executeBash/exec_bash/execBash → bash
2. **工具参数自动修正**
- bash: 自动移除不支持的 workdir/work_dir 参数
- edit: 自动将 path 参数重命名为 file_path
- 支持 JSON 字符串和对象两种参数格式
3. **流式响应集成**
- 在 SSE 数据流中实时修正工具调用
- 支持多种 JSON 结构(tool_calls, function_call, delta, choices等)
- 不影响响应性能和用户体验
4. **统计和监控**
- 记录每次工具修正的详细信息
- 提供修正统计数据查询
- 便于问题排查和性能优化
**实现文件:**
- `openai_tool_corrector.go`: 工具修正核心逻辑(250行)
- `openai_tool_corrector_test.go`: 完整的单元测试(380+行)
- `openai_gateway_service.go`: 流式响应集成
- `openai_gateway_service_tool_correction_test.go`: 集成测试
**测试覆盖:**
- 工具名称映射测试(18个映射规则)
- 参数修正测试(bash workdir、edit path等)
- SSE数据修正测试(多种JSON结构)
- 统计功能测试
- 所有测试通过 ✅
**解决的问题:**
修复了 OpenCode 使用 sub2api 中转 Codex 时,因工具名称和参数不兼容导致的工具调用失败问题。
Codex 模型有时会忽略指令文件中的工具映射说明,导致调用不存在的工具(如 apply_patch)。
现在通过流式响应拦截,自动将错误的工具调用修正为 OpenCode 兼容的格式。
**参考文档:**
- OpenCode 工具规范: https://opencode.ai/docs/
- Codex Bridge 指令: backend/internal/service/prompts/codex_opencode_bridge.txt
2026-01-15 23:52:50 +08:00
cyhhao
c11f14f3a0
fix(gateway): drain upstream after client disconnect
2026-01-15 21:51:14 +08:00
cyhhao
98b65e67f2
fix(gateway): avoid injecting invalid SSE on client cancel
2026-01-15 21:42:13 +08:00
yangjianbo
f862ddc9ff
style: 修复 gofmt 格式化问题
...
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com >
2026-01-15 19:42:18 +08:00
yangjianbo
1820389a05
feat(网关): 引入 OpenAI/Claude OAuth token 缓存
...
新增 OpenAI/Claude TokenProvider 与缓存键生成
扩展 OAuth 缓存失效覆盖更多平台
统一 OAuth 缓存前缀与依赖注入
2026-01-15 18:27:06 +08:00