mirror of
https://github.com/zyycn/codex-proxy-rs.git
synced 2026-11-21 16:01:32 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e9627168a7 | ||
|
|
5a4f49036b | ||
|
|
8041ddab65 | ||
|
|
e645d6d4c9 | ||
|
|
5079700190 | ||
|
|
4db65d8e12 | ||
|
|
da866de2c7 | ||
|
|
a770419818 | ||
|
|
5b37b574e6 | ||
|
|
0df6c40091 | ||
|
|
4b5443d2f2 | ||
|
|
b02f13c9cc | ||
|
|
878d2858e8 | ||
|
|
369f17d654 | ||
|
|
3709ed2484 | ||
|
|
48a5c9d690 | ||
|
|
6fc6283f3a | ||
|
|
605b60e58f | ||
|
|
1bf948a24f | ||
|
|
d90b9d129e | ||
|
|
4b1cae89b2 | ||
|
|
6cfe7ce2fc | ||
|
|
1427dfc64a | ||
|
|
9996fae5b6 | ||
|
|
6420d86aa6 | ||
|
|
f8fa386a7e | ||
|
|
7c2c532b44 | ||
|
|
188a6817d1 | ||
|
|
d44bc64d1a | ||
|
|
6006cc8394 | ||
|
|
e7f8b5a354 | ||
|
|
d62868b692 | ||
|
|
0922f3486c | ||
|
|
f4058c1d2e | ||
|
|
8253fbc497 | ||
|
|
193e83f9f3 | ||
|
|
a9e8fa8a44 | ||
|
|
437322ee4b | ||
|
|
843e1367bd | ||
|
|
ab1bf4372c | ||
|
|
dca9806961 | ||
|
|
15ed54d452 | ||
|
|
050214860e | ||
|
|
d917f948cf | ||
|
|
3d834c9e93 | ||
|
|
8d282dcdf9 | ||
|
|
38446e8a0c | ||
|
|
1173f6263d | ||
|
|
f4bed2077f | ||
|
|
4a8b839780 | ||
|
|
f02bc80f15 | ||
|
|
32a95a9702 | ||
|
|
cc80401d5e | ||
|
|
8cc6b50d85 | ||
|
|
83521e82f5 | ||
|
|
ba8745a714 | ||
|
|
84bcd0e1c0 | ||
|
|
7459c16a3a | ||
|
|
3b4fb390c7 | ||
|
|
bb440a3cbb | ||
|
|
89b6d2256f | ||
|
|
ba2afe67d0 | ||
|
|
976359ad26 | ||
|
|
832858dade | ||
|
|
4d4c1e48ce | ||
|
|
9f873f5ac2 | ||
|
|
4bc59f32a1 | ||
|
|
4c5d2bc97e | ||
|
|
5033d9f19a |
@@ -1,6 +1,6 @@
|
||||
---
|
||||
name: cpr-dev-guide
|
||||
description: Codex Proxy RS 开发、修复、排障、代码审查和文档修改入口;页面、样式、文案、接口、调度、数据库及宿主 SDK、Runtime、插件接入均适用;PR、Issue、独立网关插件与发版叠加对应技能
|
||||
description: Codex Proxy RS 的需求评估、排障、开发、代码审查与文档维护入口
|
||||
---
|
||||
|
||||
# 开发指南
|
||||
@@ -12,39 +12,37 @@ description: Codex Proxy RS 开发、修复、排障、代码审查和文档修
|
||||
- 先写读者需要知道的事实或动作。删掉一句不影响理解、判断或操作,就删;标题、正文和通知不重复同一信息
|
||||
- 保留会改变决定的条件、权限、后果和恢复步骤。错误通知只说已知问题,有明确办法才补一句;后端返回的提示在后端改,前端不重复解释。上游原文、用户内容和诊断事实保持原样
|
||||
|
||||
## 核实问题根因与宿主职责
|
||||
|
||||
评估功能建议、排障、开发和审查时,先按[问题与方案依据](../../../CONTRIBUTING.md#问题与方案依据)核实使用者要完成什么、当前行为造成了什么影响。反馈直接给出改法时,先弄清提出这个改法的原因
|
||||
|
||||
排查故障时,从症状沿实际调用链回溯,定位行为最早偏离预期的环节,用源码、诊断或对照复现说明触发条件如何导致结果。原因尚未确定时,区分已知事实与假设,指出哪项证据能区分可能原因。确认根因后,在负责该行为的模块修复,用原触发场景验证
|
||||
|
||||
涉及 Codex 时,以对应版本的官方源码核实客户端行为,默认透明转发,只做必要适配。建议宿主接管已有决定时,指出需要它介入的网关职责或实际约束。若只是个别部署希望采用不同策略,说明不建议内置的原因。实现可行性不能证明宿主应当承担这项需求
|
||||
|
||||
## 先确定领域与必读规范
|
||||
|
||||
按用户意图、受影响行为与对象初判,定位后按实际文件复核,不要求用户说出“前端”“后端”或技能名。**所有命中行共同构成本次必读规范,先读完再确定方案、修改或给出审查结论**;跨领域叠加,不能只选最像当前步骤的一行
|
||||
|
||||
职责边界决定问题由谁修复;即使代码位于账号、调度或 API 模块,只要影响插件接入或为插件增加处理,也命中插件边界。确定方案时说明相关业务事实的归属及已有实现的复用依据,不能以功能恢复或测试通过替代职责判断
|
||||
按受影响行为和实际文件执行下表所有命中项。流程引用首次完整读取,长文档按标题定位指定章节
|
||||
|
||||
| 领域或操作,行为影响与文件路径满足其一即可 | 确定方案前读取与执行 |
|
||||
| --- | --- |
|
||||
| 调整页面、弹窗、表单、按钮、文案、样式或交互;目标在 `frontend/`、`modules/ui/`、插件前端,或是 `.vue` 文件 | [前端流程](references/frontend.md),局部修改同样按影响范围检查组件复用、文案、Token、样式、视觉状态和运行证据 |
|
||||
| 开发、排查或审查插件接入、宿主 SDK、Runtime、扩展接口或独立网关插件;目标在 `backend/crates/gateway-plugin/`、`modules/plugins/` | 完整读取[插件职责边界](references/plugin-boundaries.md)及所用 [SDK](../../../backend/crates/gateway-plugin/sdk/README.md) 类型与能力合同;宿主维护自身合同、资源与稳定性,插件业务错误在插件侧修复,不在宿主、SDK 或 Runtime 中补偿;独立插件开发、排查、审查另用 [cpr-plugin-dev](../cpr-plugin-dev/SKILL.md) |
|
||||
| 插件接入、SDK、Runtime、扩展接口或独立网关插件,含账号、调度或 API 中为插件新增的处理;目标在 `backend/crates/gateway-plugin/`、`modules/plugins/` | [插件职责边界](references/plugin-boundaries.md)及所用 [SDK](../../../backend/crates/gateway-plugin/sdk/README.md) 类型与能力合同;独立插件开发、排查或审查同时执行 [cpr-plugin-dev](../cpr-plugin-dev/SKILL.md) |
|
||||
| 开发、排查或审查服务端接口、路由、调度、账号与用量规则、持久化、后台任务或协议行为;目标是 `backend/` 中的实现、配置、迁移,或 Rust 源码 | [Workspace 边界](../../../docs/architecture.md#3-workspace-边界)的模块与依赖规则、所属模块及[后端自审](../../../docs/architecture.md#后端自审);Rust 使用 `$rust-best-practices` 和下方后端注释要求,宿主 Rust 同时读取 [Rust 模块组织约定](../../../docs/architecture.md#32-rust-模块组织约定) |
|
||||
| 修改页面接口调用,或开发、排查、审查面向管理端或模型客户端的 HTTP/WS API 合同 | [API](../../../docs/api.md)对应章节;优先复用已有接口,核对调用方 → 服务端解析 → 实际查询或执行,不能只凭前端类型认定参数生效;按实际改动叠加前后端规范 |
|
||||
| 新增、修改或审查客户端可见错误、失败提示、后台任务状态文案 | 下方[错误文案](#错误文案),并按实际出口核对[错误与诊断三层边界](../../../docs/architecture.md#错误与诊断三层边界) |
|
||||
| 模块或调用链变更 | [Workspace 边界](../../../docs/architecture.md#3-workspace-边界)及所属模块章节,不通读整份架构 |
|
||||
| 新增、调整、清理或审查测试 | 下方[测试维护](#测试维护),按当前行为、职责归属与已有覆盖判断保留、合并或删除 |
|
||||
| 修改部署行为、`deploy/` 配置或数据库迁移 | 按范围读取[部署](../../../deploy/README.md)、[迁移](../../../backend/migrations/README.md)对应章节 |
|
||||
| 修改或审查文档;实现或审查用法、公开合同、职责、约束或运维操作的变化 | [文档检查](references/documentation.md)及相关章节,尚无文档差异时也检查是否漏更;纯文档任务不默认读取代码开发、页面验收或发布流程 |
|
||||
| 修改或审查文档;实现或审查用法、公开合同、职责、约束或运维操作的变化 | [文档检查](references/documentation.md),按最终行为核对是否漏更;纯文档任务只补读核实所述事实需要的领域条款 |
|
||||
| 代码审查,包括 PR 审查 | [审查标准](../../../CONTRIBUTING.md#审查标准),叠加被审查行为所属领域的规范;审查默认只读 |
|
||||
| PR / Issue / 发版 | 按当前意图选 [PR](../cpr-github-pr/SKILL.md)、[Issue](../cpr-github-issue/SKILL.md)或[发版](../cpr-release/SKILL.md),普通提交不创建 PR |
|
||||
|
||||
纯文档任务按文档分支执行;核实文中的合同与职责时读对应领域条款,不因提及代码路径或语言加载实现、构建与页面验收流程
|
||||
|
||||
首次命中的流程引用完整读取,并落实其中指定的必读章节;长文档先用 `rg -n '^#{1,4} ' 文件` 定位,分段读取指定章节,避免输出截断。`#章节` 不会自动截取,“按需”仅用于未命中的领域和后续操作资料,不能省略必读规范;读取记录与验证证据的复用按 [任务延续与收尾](../../../AGENTS.md#任务延续与收尾) 执行
|
||||
|
||||
## 参考仓库的优先级
|
||||
|
||||
开发、排障和代码审查先按[问题与方案依据](../../../CONTRIBUTING.md#问题与方案依据)核实问题与方案。涉及 Codex 行为时执行其中的官方行为优先约束:对应版本的官方源码优先,以透明转发和最小必要适配为默认方案,先判断偏离是否必要再评价实现
|
||||
|
||||
## 执行与收尾
|
||||
|
||||
1. 遵守已提供的 [AGENTS.md](../../../AGENTS.md),按其 CodeGraph 约定定位代码;确认工作区、任务范围与已有改动,找到事实归属、同类实现和调用链,补齐上表命中规范及[项目约定](../../../CONTRIBUTING.md#项目约定)对应条目后再修改
|
||||
2. 以最终差异重新匹配上表,补齐新增领域的规范并检查实际实现,再处理本次引入的职责越界、重复规则、冗余状态与无依据分支;删除或替换功能时一并清理失效的配置、类型、调用、测试与文档,已部署数据和公开合同仍遵守所属迁移及兼容规则;仅审查时报告,不自行修复
|
||||
3. 按最终行为执行[文档检查](references/documentation.md),核对所属正文及相关概述、示例和引用,在本任务内完成必要同步;不以尚无文档差异跳过检查。内部重构等未改变文档所述事实时无需改文档;常规文档只写当前状态,普通开发不自动修改 `release/notes.md`
|
||||
3. 在本任务内同步受影响的文档、示例和引用
|
||||
4. 按变更范围执行[验证](../../../CONTRIBUTING.md#验证)及其停止条件,后端命令见[验证命令](../../../docs/architecture.md#验证命令);复用仍适用的验证证据与用户已确定的验收分工,区分通过、失败、跳过与未执行
|
||||
5. 交付前按[首要写作要求](#首要写作要求言简意赅)通读本次新增或修改的文字。交代实际结果与验证缺口;前端按[前端流程](references/frontend.md)核对运行证据,后端按上表检查生命周期,不把构建通过当作行为验收
|
||||
6. 按授权完成提交与交付;提交前按[提交约定](../../../CONTRIBUTING.md#项目约定)准备提交信息与实际共同作者署名,提交后核对实际提交消息,PR 的 AI 披露不能替代提交署名
|
||||
|
||||
@@ -7,32 +7,11 @@
|
||||
- 以当前目标分支及任务内工作区的实现、配置和有效运行证据核实行为;提交记录用于发现待核实的主题,不能直接当作现行事实或按提交顺序组织正文。按 [CodeGraph 约定](../../../../AGENTS.md#codegraph)同步索引后定位,证据不足时报告缺口,不把计划或推测写成已支持能力
|
||||
- 开发任务按最终行为检查哪些用法、合同、职责、约束或运维操作需要同步,即使尚无 Markdown 差异也要检查;独立文档更新按用户范围核对现行说明,不等到发版时再补普通开发所需的文档
|
||||
- 沿同一事实查找所属正文及相关概述、示例和引用,重点核对“支持/不支持”等能力声明、默认值、配置模板和命令。概述保留必要摘要并链接详细合同;不能只更新详细章节,留下 README 或示例中的矛盾说明,也不扩展为无关全仓改写
|
||||
|
||||
## 写之前
|
||||
|
||||
先确定读者、必须说明的当前事实与所属章节,再决定是否需要修改及详略;文档职责与更新条件按上节核对。涉及合同或职责说明时,补读所属架构、API 或 SDK 的对应条款,核实事实;不因文档提及实现而执行无关的开发与构建流程
|
||||
|
||||
在所属章节就地替换失效说明,先组织读者现在需要的操作、结果和限制,不从变更摘要改写正文。仅介绍被替代实现、修复动机或修改经过的段落直接删除;删掉“新增”“此前”“不再”等词后仍在回答变更经过的内容,也不保留
|
||||
|
||||
## 逐段验收
|
||||
|
||||
检查工作区和暂存区中本任务的最终文档差异,并连同受影响章节及相关概述、示例通读,按内容用途处理,不只搜索关键词:
|
||||
|
||||
| 段落回答的问题 | 处理 |
|
||||
| --- | --- |
|
||||
| 现在如何使用、配置或理解结果 | 保留当前事实与必要边界,放回所属章节 |
|
||||
| 谁负责状态、事务、资源和失败处理 | 放入所属架构章节,避免在 API 或部署文档复述实现 |
|
||||
| 这次做了什么、以前怎样、为什么这样修、跑过哪些测试 | 移入 PR、Issue 或任务报告,不以换成现在时的方式留在常规文档 |
|
||||
| 特定配置或数据状态下如何升级、恢复 | 写明适用条件、操作及限制,删除演变过程 |
|
||||
| 别处已完整说明的事实 | 合并或改为必要摘要与链接 |
|
||||
|
||||
脱离提交日期、Issue 和本次任务背景后,读者仍应能独立理解正文
|
||||
|
||||
业务中的请求历史、审计记录、旧数据兼容与回滚操作是当前功能,不能因含“历史”“旧”等词而删除
|
||||
- 涉及合同或职责时核对所属架构、API 或 SDK 条款,在所属章节就地修订。审计记录、旧数据兼容和回滚仍是现行能力时,保留读者需要的说明
|
||||
|
||||
## 交付检查
|
||||
|
||||
- 完成上述逐段验收及[行文检查](../../../../CONTRIBUTING.md#层次与行文),发现问题先处理,未完成内容审查不能称文档已检查
|
||||
- 通读工作区与暂存区中本任务的最终差异、受影响章节及相关概述和示例,按[层次与行文](../../../../CONTRIBUTING.md#层次与行文)逐段验收
|
||||
- 检查相对链接、章节锚点、表格与命令示例,`git diff --check` 或构建通过不能代替内容审查
|
||||
- 交付时简述已同步的文档范围;涉及更新条件却未改文档时,说明现有内容为何仍准确或还有什么缺口。仅报告实际核对范围,不把链接检查通过称为事实核实,也不把检查记录写进常规文档
|
||||
|
||||
|
||||
@@ -32,7 +32,8 @@
|
||||
对照最终差异和实际页面核对适用项,不以读取过规范代替检查:
|
||||
|
||||
- **复用与范围**:是否沿用参考页面和基础组件,已有组件能承载时不手写替代控件;共用缺陷按职责修复,修复后清理本次已失效的页面补丁,不顺带重排无关区域或新增重复入口
|
||||
- **文案与层级**:标点是否合规,标题、状态和帮助是否重复,详细说明与当前决策必需提示的位置是否合适
|
||||
- **核对位置与对齐**时,结合周围内容查看实际页面或 PR 截图,确认操作归属一眼可辨,对照相邻行列检查边缘、文字基线和组间留白。发现错位时指出具体控件、对齐参照和受影响的状态
|
||||
- **精简文案**,删去重复说明,压短必要提示,检查自写文案中的句号和分号
|
||||
- **样式与状态**:是否使用对应 Token 与 utility,局部样式是否符合适用边界,受影响的主题、窄屏及交互状态是否正确
|
||||
- **查询与动作**:区分首次加载、刷新、分页、单行与批量操作,核对 loading、禁用状态的影响范围;沿用请求层错误处理,不在页面叠加同一错误的提示
|
||||
- **运行证据**:按界面验证核对截图与实际交互,证据对应当前实现;页面 PR 的可访问图片和 Draft 条件按该节执行
|
||||
|
||||
@@ -11,20 +11,18 @@ description: 草拟、创建、补充或回复 GitHub Issue,包括缺陷、功
|
||||
|
||||
草拟只准备正文,明确创建或提交才发布;已有授权直接执行,自动命中技能本身不授权远程写入
|
||||
|
||||
补充已有 Issue 先读原帖和讨论,按授权更新或追加,不重复新建;无法推断的目标或关键事实才向用户补齐
|
||||
补充已有 Issue 按授权更新原帖或追加评论
|
||||
|
||||
## 按动作读取与核实
|
||||
|
||||
目标、授权、事实与脱敏要求适用于草拟、提交和回复;按下列动作读取必需规则与证据,再组织正文。症状反馈可保留未知根因,进入技术排查或方案判断时补齐对应规范
|
||||
|
||||
- 新建 Issue 时,读取本仓库的[提交 Issue](../../../CONTRIBUTING.md#提交-issue)及本次匹配的一张表单:[接口问题](../../../.github/ISSUE_TEMPLATE/api-bug-report.yml)、[常规问题](../../../.github/ISSUE_TEMPLATE/general-bug-report.yml)或[功能建议](../../../.github/ISSUE_TEMPLATE/feature-request.yml);协作规则提案可用普通 Issue
|
||||
- 回复时读取原帖、相关讨论与支撑答复的证据,按下方回复口吻组织内容,不套用新建 Issue 的表单
|
||||
- 补充或回复时先读原帖和讨论,按 [GitHub 回复口吻](../../../CONTRIBUTING.md#github-回复口吻)决定答复内容和是否追问
|
||||
- 其他仓库读取它自己的相关规则与实际模板,长文档先定位标题再截取,不预读全部模板或 CONTRIBUTING
|
||||
- 搜索同场景 open/closed Issues、相关 PR 和发布记录,区分主分支修复与已发布修复,不凭标题判断重复;没有新证据时返回已有链接,用户仍要求新建则按其要求处理
|
||||
- 新建或判断重复时查找同场景的 open/closed Issues 和相关 PR,按实际场景核对。已有相同反馈且无新证据时返回原链接,用户仍要求新建则按其要求处理。答复修复状态时核对发布记录,区分已合入和已发布
|
||||
- 需要技术排查、判断实现方案或修改时执行 [cpr-dev-guide](../cpr-dev-guide/SKILL.md)的领域必读,不能仅凭症状建议跨职责补偿;提交症状反馈不以找到根因为前提
|
||||
- 引用上游行为或提出方案时执行[问题与方案依据](../../../CONTRIBUTING.md#问题与方案依据),注明适用版本、来源与缺口
|
||||
|
||||
## 正文与证据
|
||||
## 新建 Issue 的正文
|
||||
|
||||
标题说明症状或期望行为,区分发生问题的部署版本、实测结果、预期结果与根因假设
|
||||
|
||||
@@ -34,22 +32,14 @@ YAML 表单按 `attributes.label` 和字段顺序组织正文,手工核对必
|
||||
|
||||
未知信息如实标明,影响理解或复现的关键事实先补齐,不提交原始 YAML、占位答案或未确认的勾选项
|
||||
|
||||
日志和附件仅含定位所需的脱敏片段,不公开凭据、完整配置或请求转储,不擅自修改代理、TUN、证书环境取证
|
||||
|
||||
附件须获授权且可访问,本机路径不算附件;labels 和 assignees 仅按模板或用户要求设置,并确认实际存在
|
||||
labels 和 assignees 仅按模板或用户要求设置,并确认实际存在
|
||||
|
||||
Issue 可记录复现、排查和变更经过;涉及常规文档修改时执行[文档检查](../cpr-dev-guide/references/documentation.md)
|
||||
|
||||
## 回复口吻
|
||||
|
||||
草拟、发布或修改 Issue 回复时,按 [GitHub 回复口吻](../../../CONTRIBUTING.md#github-回复口吻)将核实结果写成面向提问者的交流答复,具体口吻规则集中维护在该节
|
||||
|
||||
按平常回帖的口吻写,语气自然、委婉,说清判断和原因,少用铺垫、转折和自我说明;暂不采纳或提出不同意见时说明具体原因
|
||||
|
||||
发送或更新前从提问者视角通读:判断和原因是否清楚,建议是否便于尝试,补充材料的用途是否明确。删去不影响理解的客套、重复解释和过程说明;发现生硬时调整整句,不能仅加缓和词,保留已核实的结论
|
||||
|
||||
## 发布与收尾
|
||||
|
||||
日志和附件只含定位所需的脱敏片段,不公开凭据、完整配置或请求转储,不擅自修改代理、TUN、证书环境取证。附件须获授权且可访问,本机路径不算附件
|
||||
|
||||
使用可用连接器或 `gh`,不为提交反馈安装工具、修改仓库配置或启用服务
|
||||
|
||||
多行正文通过结构化字段或 UTF-8 文件传递,以下变量须先从已确认的目标与正文赋值:
|
||||
@@ -58,6 +48,6 @@ Issue 可记录复现、排查和变更经过;涉及常规文档修改时执
|
||||
gh issue create --repo "$issue_repo" --title "$issue_title" --body-file "$issue_body_file"
|
||||
```
|
||||
|
||||
发布前核对目标、必填内容、脱敏与重复记录,更新时保留无关原文;不擅自评论、重开或关闭重复 Issue
|
||||
发布前核对目标和脱敏内容,新建时确认必填项,更新时保留无关原文;不擅自评论、重开或关闭重复 Issue
|
||||
|
||||
超时或结果不明确时先查询远端再重试,发布后回读确认并返回链接;仅草拟或发布失败时提供正文并注明未提交
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
interface:
|
||||
display_name: "cpr-github-issue"
|
||||
short_description: "整理、提交或补充 Issue,核对复现、重复反馈与发布范围"
|
||||
default_prompt: "使用 $cpr-github-issue 按当前请求整理、提交或补充 Issue,核对目标、复现证据、重复记录和脱敏,不将草拟自动变成发布。"
|
||||
short_description: "草拟、提交或回复 GitHub Issue,处理缺陷反馈和功能建议"
|
||||
default_prompt: "使用 $cpr-github-issue 按当前请求整理反馈或回复已有 Issue"
|
||||
policy:
|
||||
allow_implicit_invocation: true
|
||||
|
||||
@@ -17,8 +17,6 @@ description: 准备、创建、更新或审查 GitHub PR,含回复讨论与处
|
||||
|
||||
为本仓库拟写或更新 PR 标题时,完整标题使用英文,沿用提交信息的 Conventional Commits 格式,例如 `fix(store): preserve client key ledgers after deletion`
|
||||
|
||||
涉及上游行为的判断按[问题与方案依据](../../../CONTRIBUTING.md#问题与方案依据)执行
|
||||
|
||||
## 按模式读取
|
||||
|
||||
先完成上述范围判断,再按当前动作与差异涉及的领域叠加下表。命中的流程引用完整读取,长文档读取指定章节;只复用已实际读取且仍适用的内容,单纯整理正文或回复不扩展为完整实现审查
|
||||
@@ -28,22 +26,17 @@ description: 准备、创建、更新或审查 GitHub PR,含回复讨论与处
|
||||
| 只整理标题或描述 | 当前 PR、最终 diff、实际模板、[AI 披露](../../../CONTRIBUTING.md#ai-使用披露),在授权范围内改文案,不自行修改实现;不读取推送或审查流程 |
|
||||
| 创建或更新 PR | [创建流程](references/create.md)、[PR 流程](../../../CONTRIBUTING.md#pr-流程)与[规则文件保护](../../../CONTRIBUTING.md#规则文件保护) |
|
||||
| 审查 PR | [审查流程](references/review.md)、[审查标准](../../../CONTRIBUTING.md#审查标准) |
|
||||
| 回复讨论或审查意见 | 目标评论、相关上下文与实际处理结果,按下方回复口吻答复;仅回复不加载创建或完整审查流程 |
|
||||
| 回复讨论或审查意见 | 读取目标评论和相关上下文,按[GitHub 回复口吻](../../../CONTRIBUTING.md#github-回复口吻)答复实际处理结果 |
|
||||
| 实现自审、审查或修改代码与文档 | 执行 [cpr-dev-guide](../cpr-dev-guide/SKILL.md)的领域必读与对应检查,包括后端、插件、测试和 API 等命中项;只读审查同样执行,不能仅核对 PR 正文与 CI |
|
||||
| 含前端改动 | [前端流程](../cpr-dev-guide/references/frontend.md),核对适用规范与当前实现的运行证据 |
|
||||
| 含文档改动 | [文档检查](../cpr-dev-guide/references/documentation.md) |
|
||||
|
||||
## 回复口吻
|
||||
|
||||
草拟和发布 PR 讨论、审查意见的回复时,执行 [GitHub 回复口吻](../../../CONTRIBUTING.md#github-回复口吻)
|
||||
|
||||
## 创建与审查的公共核对
|
||||
|
||||
- 按[问题与方案依据](../../../CONTRIBUTING.md#问题与方案依据)核实触发条件、发生版本与目标分支现状,分别判断问题是否成立、是否属于目标项目职责及方案是否必要
|
||||
- 核对最新目标分支及同场景 open/closed PR、Issue,按实际行为和测试判断重复,不凭标题下结论,也不擅自关闭他人 PR
|
||||
- 自审或审查覆盖完整差异与调用链,前端与文档按模式表检查
|
||||
- 核对[AI 使用披露](../../../CONTRIBUTING.md#ai-使用披露),填写实际工具与模型,不猜型号;必需截图或型号待补时保留 Draft
|
||||
- 验证结果对应实际 head,区分通过、失败、跳过与未执行;无文档改动时只核对现有说明是否失真
|
||||
- 自审或审查覆盖完整差异和调用链,按[验证](../../../CONTRIBUTING.md#验证)执行适用检查,确认结果对应当前 head
|
||||
- 核对[界面验证](../../../CONTRIBUTING.md#界面验证)要求的截图和 [AI 使用披露](../../../CONTRIBUTING.md#ai-使用披露),必需材料待补时保留 Draft
|
||||
|
||||
远程操作优先使用可用连接器,`gh` 显式指定 `--repo`,多行正文使用结构化字段或 `--body-file`
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
interface:
|
||||
display_name: "cpr-github-pr"
|
||||
short_description: "提交、更新与审查 PR,核对问题、自审、页面截图和 AI 模型披露"
|
||||
default_prompt: "使用 $cpr-github-pr 按当前请求准备、提交、更新或审查 PR,实现自审与审查叠加开发指南的领域规范,核实问题、方案、验证、页面截图和 AI 模型披露。"
|
||||
short_description: "准备、更新或审查 GitHub PR,回复讨论和审查意见"
|
||||
default_prompt: "使用 $cpr-github-pr 按当前请求准备、更新、审查 PR 或回复讨论"
|
||||
policy:
|
||||
allow_implicit_invocation: true
|
||||
|
||||
@@ -6,16 +6,12 @@
|
||||
不用提交日志拼接正文,也不假定仓库一定有模板。
|
||||
2. 更新并确认 base/head,查看相对 merge-base 的完整差异和待提交内容;核对仓库的受保护文件与准入规则。
|
||||
只纳入任务范围的文件和提交;需要整理时用独立功能分支,保留用户其他改动,不擅自改写已发布历史。
|
||||
3. 已完成的改动也须按入口公共核对复核问题与方案。原问题不成立时不包装成已修复的缺陷,证据不足时保留结论。
|
||||
实现与自审按 [cpr-dev-guide](../../cpr-dev-guide/SKILL.md)执行,包括适用的后端生命周期和前端检查;需要修正时只修改已授权范围。
|
||||
4. 查找同一来源仓库、head 和 base 的现有 PR;已存在则复用其链接,不重复创建。
|
||||
3. 查找同一来源仓库、head 和 base 的现有 PR;已存在则复用其链接,不重复创建。
|
||||
更新现有标题或正文须在本次整理/更新请求范围内。
|
||||
5. 按[验证](../../../../CONTRIBUTING.md#验证)执行适用检查,记录对应提交或工作区状态;文档、页面证据与模型披露按入口核对。
|
||||
6. 将任务范围内的提交推送到正确远端,核对远端 head。标题遵循项目约定,正文先说明问题成立的依据和最终行为,
|
||||
再给必要的设计取舍、自审结论、验证与遗留事项,按模板附页面截图和 AI 使用情况。涉及上游行为时,按[问题与方案依据](../../../../CONTRIBUTING.md#问题与方案依据)写明官方源码位置、版本、处理方式及本次方案关系。按规模压缩,不强填无关章节。
|
||||
7. 明确要求草稿或仍有阻塞项时创建或保持 Draft PR,并解释原因;必需截图或 AI 模型披露待补时也不标为可审阅。具备审查条件且未要求草稿时创建普通 PR。
|
||||
4. 将任务范围内的提交推送到正确远端,核对远端 head。按实际模板写明问题依据、最终行为、自审与验证结果,附上适用的审阅材料
|
||||
5. 明确要求草稿或仍有阻塞项时创建或保持 Draft PR,并说明原因;具备审查条件且未要求草稿时创建普通 PR。
|
||||
GitHub Draft 状态与“只写本地草稿正文”不同,后者不创建远程对象。
|
||||
8. 创建或更新后回读正文与附件,核对链接、base、head、草稿状态、模型披露和最新检查结果。网络超时或结果不确定时先查询远端,再决定是否重试。
|
||||
6. 创建或更新后回读正文与附件,核对链接、base、head、草稿状态、模型披露和最新检查结果。网络超时或结果不确定时先查询远端,再决定是否重试。
|
||||
|
||||
CLI 示例中的变量必须先根据上述检查赋值;`pr_body_file` 是精确写入 UTF-8 正文的临时文件:
|
||||
|
||||
|
||||
@@ -4,20 +4,10 @@
|
||||
|
||||
1. 获取 PR 的 base/head SHA、描述、文件差异、已有讨论及 CI 状态。记录实际审查的 head,使用 merge-base 差异。
|
||||
需要源码上下文时先遵守目标仓库的定位规则;需检出时使用隔离工作区,保留用户当前分支和运行环境。
|
||||
按实际行为与文件执行 [cpr-dev-guide](../../cpr-dev-guide/SKILL.md)的领域必读,再判断方案与实现;只读审查也核对职责边界与生命周期。
|
||||
2. 按入口公共核对在目标分支基线核实问题,区分原有问题、环境或配置差异与 PR 新引入的问题,说明主分支已覆盖的部分。
|
||||
3. 按[问题与方案依据](../../../../CONTRIBUTING.md#问题与方案依据)评估解决方向,涉及上游时说明官方处理方式及本次方案关系;缺少必要性依据的偏离明确指出,不因测试通过就认定方案合理。
|
||||
4. 按[审查标准](../../../../CONTRIBUTING.md#审查标准)追踪完整调用链,找到共享业务事实、同类实现及已有验证。
|
||||
对既有特殊分支查清服务的场景,再判断本次改动是否引入或加重问题。
|
||||
主动检查本次变更新引入或加重的冗余与代码异味,说明具体位置、维护成本或行为影响及最小修复方向;
|
||||
不能仅因功能可用、CI 通过而略过,也不把个人写法偏好判为缺陷。
|
||||
5. 前端改动执行[前端流程](../../cpr-dev-guide/references/frontend.md),浏览器可用时检查相关交互;缺少必需截图时要求补齐并列为未具备审阅条件。
|
||||
6. 核对 CI 与本地证据是否覆盖当前 head;根据风险补充必要验证,不触发无关部署或真实账号业务请求。
|
||||
文档差异按[文档检查](../../cpr-dev-guide/references/documentation.md)核对最终 diff。
|
||||
核对 AI 使用与模型披露,缺项明确指出。工具不可用、集成测试跳过或环境不足时具体说明影响范围。
|
||||
7. 按审查标准用中文输出问题是否成立、方案是否合理及对应依据,再列实现问题与验证缺口;复用建议指出具体已有实现。
|
||||
没有问题的类别可省略,未发现问题时直接说明并交代覆盖范围。
|
||||
8. 结束前核对 head 是否变化。发生变化时检查新增差异并更新结论,或明确结论只适用于原 SHA。
|
||||
2. 对照目标分支区分原有问题、环境差异和本次引入或加重的问题。修改既有特殊分支时,查清它原先服务的场景
|
||||
3. 根据风险补充必要验证。浏览器可用时实测受影响的交互,避免触发无关部署或真实账号业务请求
|
||||
4. 用中文说明问题是否成立、方案是否合理,再报告可定位的实现问题与验证缺口。建议复用时指出已有实现;未发现问题时交代实际覆盖范围
|
||||
5. 结束前复核 head。发生变化时检查新增差异并更新结论,或明确结论只适用于原 SHA
|
||||
|
||||
审查本身不修改源码、不发表评论、不提交 GitHub 的批准或请求修改状态。
|
||||
用户明确要求发布时,先复核当前 head 和重复评论,再通过结构化字段或 `--body-file` 发布对应内容;
|
||||
|
||||
@@ -1,17 +1,13 @@
|
||||
---
|
||||
name: cpr-plugin-dev
|
||||
description: 开发、修复、排查、审查或打包 Codex Proxy RS 独立网关插件,含处理器、清单与插件页面;仅改宿主能力时使用 cpr-dev-guide 的插件边界,不适用于 Codex 插件或 Vue 插件
|
||||
description: Codex Proxy RS 独立网关插件的开发、排障、审查与打包
|
||||
---
|
||||
|
||||
# 网关插件开发
|
||||
|
||||
## 先读职责边界
|
||||
|
||||
插件在独立项目中通过公开 SDK 实现业务,插件业务错误在插件侧修复;宿主维护自身合同、资源与稳定性,不猜测插件意图、不补造业务数据,不在宿主、SDK 或 Runtime 中增加插件专属补偿
|
||||
|
||||
开发、排查和审查先完整读取[插件职责边界](../cpr-dev-guide/references/plugin-boundaries.md),再选择实现步骤;已有处理器修复和页面改动也执行,不以是否新增能力为条件。需要新增宿主能力时,先界定可独立使用的基础接口及其职责;必要的协议保护与资源回收仍由宿主负责
|
||||
|
||||
同时执行 [cpr-dev-guide](../cpr-dev-guide/SKILL.md)的领域必读与收尾要求;已实际读取且仍适用的内容复用,不递归重读入口。仅构建、打包或分发已有插件时按对应操作读取,若需诊断或修改行为,再补齐开发分支
|
||||
开发、排查或审查独立插件时,执行 [cpr-dev-guide](../cpr-dev-guide/SKILL.md) 并先读[插件职责边界](../cpr-dev-guide/references/plugin-boundaries.md),包括修改已有处理器或页面。仅构建、打包或分发已有插件时,按下表读取对应步骤
|
||||
|
||||
## 范围与合同
|
||||
|
||||
|
||||
@@ -61,6 +61,6 @@ release/publish "$release_version"
|
||||
工作流附加的安装说明和自动提交列表按其当前配置处理,不用它们替代手写的中文变更说明。
|
||||
- 回读 `main:release/version.yaml` 的 `published`。正式版参与同一大版本四个普通通道的比较;RC 参与 RC、Beta、Alpha,Beta 参与 Beta、Alpha,Alpha 只参与自身。实验版只推进 `exp["X.Y.Z"]`。有通道推进时应有 `chore(release): update release channels to <version>` 提交;重复执行或较旧版本未推进任何通道时无提交
|
||||
- 通道表写入失败时,确认该版本已公开、工作流能写入 `main`,再按已有授权执行 `gh workflow run release.yml --ref main -f tag="v$release_version" -f index_only=true`。恢复任务重新校验公开附件、读取并合并当前清单,不重新打包或改 tag
|
||||
- v3.42.0 在线升级:首个包含新更新器的 3.x 正式版公开并通过校验后,从该次通道提交取出 `release/version.yaml`,将顶层 `version` 设为该正式版,作为同名附件上传到该 Release。回读附件,确认 3.x 稳定通道指向该版本且安装包可用,再手动将已有 `update-index` 正文改为 `{"current":"<该正式版>"}`。此后保留该页与附件,无需再编辑
|
||||
- 保留 v3.42.0 在线升级使用的 `update-index` 页面和 v3.42.1 的 `version.yaml` 附件
|
||||
- 返回版本、tag 对应提交、Release/工作流链接及实际结果。失败、仍在构建或缺失产物时明确说明,不能称为发布成功;
|
||||
Release 和镜像已生成也不表示用户的运行实例已经升级。
|
||||
|
||||
@@ -35,7 +35,7 @@ jobs:
|
||||
CPR_TEST_REDIS_URL: redis://127.0.0.1:6379
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:18-bookworm@sha256:bbf83594798e144bff365a9c191c5d779f181fb3135902064ab8ff11918020fc
|
||||
image: postgres:18-bookworm@sha256:194e55feb2597b1465dbee81cc9fd8d204566cd2eb42c8d22fd6c931a38781b1
|
||||
env:
|
||||
POSTGRES_DB: codex_proxy
|
||||
POSTGRES_USER: codex_proxy
|
||||
@@ -49,7 +49,7 @@ jobs:
|
||||
--health-timeout 5s
|
||||
--health-retries 10
|
||||
redis:
|
||||
image: redis:8-alpine@sha256:0bf6577d7774e592b29d093e3ac9390eb91e13d72c21e506afce736de5e0eeb0
|
||||
image: redis:8-alpine@sha256:7958cfbb9d152f87dadfbc89f1b0b15938f0b81ff8982feb327476f4911935c3
|
||||
ports:
|
||||
- 6379:6379
|
||||
options: >-
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
| 任务意图与常见说法 | 必用技能 |
|
||||
| --- | --- |
|
||||
| 开发、修复、排障、代码审查、改文档;包括“改弹窗/样式/文案”“修接口/调度/数据库”等省略请求 | [cpr-dev-guide](.agents/skills/cpr-dev-guide/SKILL.md),按实际操作和文件匹配规范,跨前后端同时执行 |
|
||||
| 需求评估、排障、开发、修复、代码审查、改文档,包括“看下这个 Issue”“改弹窗/样式/文案”“修接口/调度/数据库” | [cpr-dev-guide](.agents/skills/cpr-dev-guide/SKILL.md),按实际操作和文件匹配规范,跨前后端同时执行 |
|
||||
| 提交/提个 PR、准备或更新 PR、看下 PR、回复 PR 讨论、处理 PR 审查意见 | [cpr-github-pr](.agents/skills/cpr-github-pr/SKILL.md);实现自审、审查或修改同时执行 cpr-dev-guide |
|
||||
| 提 Issue、反馈到仓库、把问题或建议整理成 Issue、补充或回复 Issue | [cpr-github-issue](.agents/skills/cpr-github-issue/SKILL.md) |
|
||||
| 写、修或审查独立网关插件,排查插件接入、打包插件 | [cpr-plugin-dev](.agents/skills/cpr-plugin-dev/SKILL.md),同时执行 cpr-dev-guide;仅改宿主能力时由 cpr-dev-guide 加载插件边界 |
|
||||
|
||||
+4
-10
@@ -21,17 +21,11 @@
|
||||
|
||||
### GitHub 回复口吻
|
||||
|
||||
AI 草拟、发布或修改 Issue、PR 讨论及审查意见的回复时,以一起排查问题、讨论方案的口吻交流:
|
||||
Issue 和 PR 回帖直接回应对方遇到的问题,有不同意见时结合具体场景说明理由。语气自然、委婉,用短句,少用句号。说明个人判断或行动时用“我”,确需提问时称呼一次“佬”
|
||||
|
||||
- 围绕问题、事实和结论展开,能自然省略主语时省略;需要说明个人判断或行动归属时用“我”,避免反复以“我”起句或强调自己,不用“我们”“本助手”代称自己
|
||||
- 普通回复直接回应问题,不加问候语或称呼;向对方提问时称呼“佬”,同一条回复不重复称呼
|
||||
- 先接住对方想完成的事和遇到的困难,再给出核实结果与建议;回复让对方感到问题被理解、有下一步可做,避免一上来归类、纠正或划定责任
|
||||
- 给出操作建议时说明它能帮助判断什么,措辞自然、委婉,不靠堆叠缓和词修饰命令
|
||||
- 回应已有能力、暂不支持或未采纳的建议时,结合场景说明可用入口或具体原因,有可行替代办法时一并说明,再交代必要边界;避免只回复“已有功能”“重复问题”“不在范围内”等结论
|
||||
- 需要补充材料或配合操作时,先利用已有信息,只询问仍缺少的部分,说明用途和最小范围;例如“佬,方便再观察几次吗?如果还出现,可以附一下对应请求的诊断包,便于对照终态用量”
|
||||
- 澄清误解或提出不同意见时,从观察和依据说起,照顾对方的使用环境与理解路径,避免责备、反问或居高临下的判断;已确认的事实保持明确,尚未确认的原因保留余地
|
||||
- 使用自然短句,简单回复用短段,并列信息较多时再分点;少用句号,避免客服套话、机械模板和空泛保证
|
||||
- 排查、复现、修改和验证的完成状态与实际一致;未采纳的意见说明依据,未确认、待处理或未验证的内容如实交代,说清结论、限制和下一步
|
||||
- 用法问题给出操作入口,功能取舍或关闭 Issue 说明与该场景相关的原因,有可行替代办法时再提。问题已答清即可结束
|
||||
- 排障先看原帖、已有材料和自己能核实的实现或发布记录。回复只附有助于理解结论或采取行动的排查信息,如相关版本、复现结果或脱敏片段。区分推测和实测
|
||||
- 只有缺失信息会改变判断或处理方式,且只能由对方提供时才追问。说清需要哪一项、用来区分什么情况
|
||||
|
||||
## 问题与方案依据
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
[](https://github.com/zyycn/codex-proxy-rs/pkgs/container/codex-proxy-rs)
|
||||
[](LICENSE)
|
||||
|
||||
[快速预览](#快速预览) · [快速开始](#快速开始) · [客户端接入](#客户端接入) · [文档](#文档) · [社区](#社区) · [许可证](#许可证)
|
||||
[快速预览](#快速预览) · [快速开始](#快速开始) · [文档](#文档) · [社区](#社区) · [贡献者](#贡献者) · [Star History](#star-history) · [支持项目](#支持项目) · [许可证](#许可证)
|
||||
|
||||
</div>
|
||||
|
||||
@@ -21,105 +21,28 @@
|
||||
|
||||
## 快速预览
|
||||
|
||||
无需部署即可体验管理端的系统概览、账号分组、代理管理与用量统计:
|
||||
|
||||
| 登录信息 | 默认值 |
|
||||
| --- | --- |
|
||||
| 地址 | <https://codex-proxy-rs.ainz.cc> |
|
||||
| 登录身份 | 管理员 |
|
||||
| 账号 | `admin@cpr.local` |
|
||||
| 密码 | `039c18de2aeac46d23ead7766bb07bbbe3747233c66a128e` |
|
||||
|
||||
预览服务运行已发布版本,展示的账号、代理与使用记录均为模拟数据,每天北京时间 `00:00` 自动生成当天数据
|
||||
|
||||
这是公开共享的功能预览环境,不提供真实模型调用;请勿导入真实账号、密钥或其他敏感信息
|
||||
|
||||
## 快速开始
|
||||
|
||||
使用 Docker Compose 部署版本固定的发布镜像,同时启动 PostgreSQL 和 Redis
|
||||
|
||||
以下命令适用于 Linux amd64/arm64,需要 Docker Engine、Docker Compose Plugin、curl 和 OpenSSL
|
||||
|
||||
macOS 从[发行页](https://github.com/zyycn/codex-proxy-rs/releases)下载对应的归档:Intel 机型选 `darwin_amd64`,Apple Silicon 机型选 `darwin_arm64`。
|
||||
Windows x64 / ARM64 的安装步骤见 [Windows 二进制部署](deploy/README.md#windows-二进制部署)
|
||||
|
||||
已有部署请先看 [升级说明](deploy/README.md#镜像升级与源码构建),不要覆盖原配置
|
||||
|
||||
### 一键安装
|
||||
|
||||
请确保当前用户能访问 Docker,并可通过 `sudo` 或 root 设置目录权限
|
||||
在已安装 Docker Engine、Docker Compose Plugin、curl 和 OpenSSL 的 Linux amd64/arm64 主机上运行
|
||||
|
||||
```bash
|
||||
curl -fsSL https://raw.githubusercontent.com/zyycn/codex-proxy-rs/main/deploy/install.sh -o install.sh && bash install.sh
|
||||
```
|
||||
|
||||
[安装脚本](deploy/install.sh) 默认安装到当前目录下的 `codex-proxy-rs/`,下载同一正式 Release 的部署文件,
|
||||
自动生成密码、设置目录权限并启动服务
|
||||
|
||||
完成后保存显示的访问地址和管理员密码,再[登录管理端](#登录管理端)并[添加账号与客户端密钥](#添加账号与客户端密钥)
|
||||
|
||||
可在执行 `bash install.sh` 时传入环境变量:
|
||||
|
||||
| 变量 | 用途 | 默认值 |
|
||||
| --- | --- | --- |
|
||||
| `INSTALL_DIR` | 安装目录,建议使用绝对路径 | 当前目录下的 `codex-proxy-rs/` |
|
||||
| `CPR_RELEASE_TAG` | 指定发布标签 | 最新正式版本 |
|
||||
| `ADMIN_PASSWORD` | 管理员初始密码,至少 12 位,不能包含 `$`,不能使用常见弱口令 | 随机生成 |
|
||||
|
||||
例如,自定义安装目录:
|
||||
|
||||
```bash
|
||||
INSTALL_DIR="$HOME/services/codex-proxy-rs" bash install.sh
|
||||
```
|
||||
|
||||
重复运行时请使用同一安装目录,检测到 `deploy/config.yaml` 后,脚本保留现有配置和部署文件,
|
||||
忽略传入的管理员密码,也不执行版本升级
|
||||
|
||||
### 手动安装
|
||||
|
||||
自行下载部署文件、配置密码和启动服务的完整步骤见 [部署文档](deploy/README.md#手动安装)
|
||||
|
||||
### 登录管理端
|
||||
|
||||
部署完成后,打开 `http://127.0.0.1:8080`,使用 `admin@cpr.local` 和管理员密码登录
|
||||
|
||||
API Key 持有者可在登录页切换身份,查看自己的用量与额度,并通过「密钥配置」复制 Codex 配置或导入 CC Switch
|
||||
|
||||
默认地址只能在服务器本机访问,从其他设备使用时,需要配置
|
||||
[HTTPS 反向代理](deploy/README.md#公网访问)
|
||||
|
||||
### 添加账号与客户端密钥
|
||||
|
||||
1. 在「账号」中添加账号,完成授权或导入
|
||||
2. 按需建立账号分组,再创建客户端密钥并选择可用分组,**不选分组表示可使用全部账号**
|
||||
3. [连接客户端](#客户端接入)
|
||||
|
||||
## 客户端接入
|
||||
|
||||
**Codex CLI / 桌面端**:在「使用密钥」中按操作系统复制配置,合并配置后重启 Codex;也可通过「导入到」把服务连接与密钥添加到 CC Switch
|
||||
|
||||
完整步骤、生图配置与排障见[客户端配置](deploy/README.md#客户端配置)
|
||||
|
||||
**其他 API 客户端**:API Key 填写管理端创建的客户端密钥。按客户端自动追加的请求路径填写 Base URL;远程接入时将本机地址换成服务器的 HTTPS 地址
|
||||
|
||||
| 客户端自动追加的路径 | Base URL(本机部署) |
|
||||
| --- | --- |
|
||||
| OpenAI Responses:`/responses`;Chat Completions:`/chat/completions` | `http://127.0.0.1:8080/v1` |
|
||||
| Anthropic Messages:`/v1/messages` | `http://127.0.0.1:8080` |
|
||||
|
||||
查询该密钥可见的模型目录:
|
||||
|
||||
```bash
|
||||
curl http://127.0.0.1:8080/v1/models \
|
||||
-H 'Authorization: Bearer <client-api-key>'
|
||||
```
|
||||
[安装脚本选项](deploy/README.md#安装脚本选项) · [macOS 发行包](https://github.com/zyycn/codex-proxy-rs/releases) · [Windows 安装](deploy/README.md#windows-二进制部署) · [已有部署升级](deploy/README.md#镜像升级与源码构建)
|
||||
|
||||
## 文档
|
||||
|
||||
| 任务 | 文档 |
|
||||
| 主题 | 文档 |
|
||||
| --- | --- |
|
||||
| 部署与使用 | [部署、备份与恢复](deploy/README.md) · [客户端接入与生图](deploy/README.md#客户端配置) |
|
||||
| 接口集成 | [API 参考](docs/api.md) · [模型定价](docs/api.md#模型定价) |
|
||||
| 部署与使用 | [部署、备份与恢复](deploy/README.md) · [客户端接入](deploy/README.md#客户端配置) |
|
||||
| 接口集成 | [API 参考](docs/api.md) · [模型目录](docs/api.md#模型目录) · [模型定价](docs/api.md#模型定价) |
|
||||
| 使用插件 | [安装、配置与使用](docs/plugins.md) |
|
||||
| 开发插件 | [SDK 与合同](backend/crates/gateway-plugin/sdk/README.md) · [打包工具](backend/apps/plugin-cli/README.md) · [官方示例](https://github.com/zyycn/codex-proxy-plugins/blob/main/README.md) |
|
||||
| 开发共用组件 | [组件库说明](https://github.com/zyycn/codex-proxy-ui/blob/main/README.md) · [组件参考](https://github.com/zyycn/codex-proxy-ui/blob/main/playground/components/index.md) · [Playground](https://zyycn.github.io/codex-proxy-ui/) |
|
||||
@@ -132,6 +55,28 @@ curl http://127.0.0.1:8080/v1/models \
|
||||
|
||||
感谢 [LINUX DO](https://linux.do) 社区提供开放、友善的技术交流平台
|
||||
|
||||
## 贡献者
|
||||
|
||||
[<img src="https://contrib.rocks/image?repo=zyycn/codex-proxy-rs&columns=10" alt="查看 GitHub 贡献者列表" width="480">](https://github.com/zyycn/codex-proxy-rs/graphs/contributors)
|
||||
|
||||
## Star History
|
||||
|
||||
<a href="https://simrepo.dera.page/#zyycn/codex-proxy-rs&Date">
|
||||
<picture>
|
||||
<source media="(prefers-color-scheme: dark)" srcset="https://simrepo.dera.page/svg?repos=zyycn/codex-proxy-rs&type=Date&theme=dark">
|
||||
<img src="https://simrepo.dera.page/svg?repos=zyycn/codex-proxy-rs&type=Date" alt="Codex Proxy RS 的 GitHub Star 增长趋势" width="560">
|
||||
</picture>
|
||||
</a>
|
||||
|
||||
## 支持项目
|
||||
|
||||
如果 Codex Proxy RS 帮助到你了,欢迎请我喝杯咖啡 ☕
|
||||
|
||||
[](https://ko-fi.com/zyycn)
|
||||
|
||||
[<img src="docs/donations/wechat-tips.png" alt="WeChat appreciation code" width="138">](docs/donations/wechat-tips.png)
|
||||
[<img src="docs/donations/alipay.png" alt="Alipay QR code" width="138">](docs/donations/alipay.png)
|
||||
|
||||
## 许可证
|
||||
|
||||
本项目基于 [Apache License 2.0](LICENSE) 开源
|
||||
|
||||
Generated
+6
-6
@@ -3006,9 +3006,9 @@ checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
|
||||
|
||||
[[package]]
|
||||
name = "openssl"
|
||||
version = "0.10.80"
|
||||
version = "0.10.82"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a45fa2aa886c42762255da344f0a0d313e254066c46aad76f300c3d3da62d967"
|
||||
checksum = "4de8111e94a56adca7ebfe6c365b6d42f99deb69b74b6becacd4204e2f193aa2"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"cfg-if",
|
||||
@@ -3052,9 +3052,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "openssl-sys"
|
||||
version = "0.9.116"
|
||||
version = "0.9.118"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f28a22dc7140cda5f096e5e7724a6962ca81a7f8bfd2979f9b18c11af56318c4"
|
||||
checksum = "19c413d6ec9ebaf5e1c829d355cb3e9f783778d700f135be0921d52fce3c60ae"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"libc",
|
||||
@@ -4797,7 +4797,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "tokio-tungstenite"
|
||||
version = "0.28.0"
|
||||
source = "git+https://github.com/openai-oss-forks/tokio-tungstenite?rev=0e5b2d73aa18dd9f0a50ee9ff199d5aef7594186#0e5b2d73aa18dd9f0a50ee9ff199d5aef7594186"
|
||||
source = "git+https://github.com/openai-oss-forks/tokio-tungstenite?rev=7447c427513cfe685f236e1ac0513c4ea3dd9c0b#7447c427513cfe685f236e1ac0513c4ea3dd9c0b"
|
||||
dependencies = [
|
||||
"futures-util",
|
||||
"log",
|
||||
@@ -5006,7 +5006,7 @@ checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b"
|
||||
[[package]]
|
||||
name = "tungstenite"
|
||||
version = "0.27.0"
|
||||
source = "git+https://github.com/openai-oss-forks/tungstenite-rs?rev=4fffad30fe373adbdcffab9545e9e9bf4f2fc19f#4fffad30fe373adbdcffab9545e9e9bf4f2fc19f"
|
||||
source = "git+https://github.com/openai-oss-forks/tungstenite-rs?rev=bc589d684755a8c02da0fc7e58d0861cbfc306f1#bc589d684755a8c02da0fc7e58d0861cbfc306f1"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"data-encoding",
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
# Codex Proxy Plugin CLI
|
||||
|
||||
`cpr-plugin` 校验并规范化作者清单、收集声明的资源,生成可安装归档及 SHA-256 校验文件。
|
||||
输出合同与 [SDK 版本](../../crates/gateway-plugin/sdk/README.md)一致
|
||||
`cpr-plugin` 按 [SDK 合同](../../crates/gateway-plugin/sdk/README.md)校验清单,将插件可执行文件与声明的资源打包,并输出 SHA-256 校验文件
|
||||
|
||||
## 安装
|
||||
|
||||
@@ -11,7 +10,7 @@
|
||||
cargo +1.99.1 install --locked --path backend/apps/plugin-cli --root /path/to/local-tools
|
||||
```
|
||||
|
||||
将 `/path/to/local-tools/bin` 加入 `PATH`,或使用可执行文件的完整路径;工具链与宿主 [验证命令](../../../docs/architecture.md#验证命令)一致
|
||||
将 `/path/to/local-tools/bin` 加入 `PATH`,或使用可执行文件的完整路径
|
||||
|
||||
## 打包
|
||||
|
||||
@@ -26,24 +25,20 @@ cpr-plugin package \
|
||||
--output-dir /path/to/output
|
||||
```
|
||||
|
||||
`--resource-map web=web/dist` 将包内 `web/` 前缀映射到作者清单所在目录下的 `web/dist/`。
|
||||
可重复传入不同前缀,源目录必须位于项目内;没有对应资源时省略该参数,未使用的映射会被拒绝。
|
||||
未映射的资源按清单中的相对路径读取,只收集 `resources` 声明的文件
|
||||
资源文件须在 `resources` 中声明,默认从清单所在目录按相对路径读取。
|
||||
`--resource-map web=web/dist` 从项目内的 `web/dist/` 读取包内 `web/` 资源。可为不同前缀重复传参,未匹配任何资源的映射会报错
|
||||
|
||||
输出为 `<插件 ID>-<版本>-<目标 triple>.tar.gz` 及同名 `.tar.gz.sha256`;归档根目录直接包含 `plugin.json`、可执行文件和声明资源。
|
||||
输出为 `<插件 ID>-<版本>-<目标 triple>.tar.gz` 及对应的 `.tar.gz.sha256`。归档根目录包含 `plugin.json`、可执行文件和声明资源。
|
||||
输入清单及生成后的清单均不得超过 64 KiB,压缩包上限为 32 MiB,展开归档上限为 128 MiB
|
||||
|
||||
作者清单不填写 `package`;字段省略规则、能力版本和挂载阶段见 [SDK 扩展项声明](../../crates/gateway-plugin/sdk/docs/manifest.md#扩展项简写)。
|
||||
CLI 生成规范化后的 `plugin.json`,补入协议版本、目标平台和文件摘要。打包成功只证明输入符合包合同,实际启动与能力仍需在目标宿主验证
|
||||
作者清单须省略 `package`。CLI 将协议版本、目标平台和文件摘要写入 `plugin.json`。可省略的声明字段见 [SDK 扩展项声明](../../crates/gateway-plugin/sdk/docs/manifest.md#扩展项简写)
|
||||
|
||||
支持 `x86_64-unknown-linux-gnu`、`aarch64-unknown-linux-gnu` 和 `aarch64-apple-darwin`。
|
||||
完整开发示例在独立仓库 `codex-proxy-plugins` 的 `examples/workbench`,可通过[子模块](../../../docs/development.md#源码联调)检出;本工具不负责源码构建、上传或启用插件
|
||||
打包后在目标宿主验证启动和插件功能。完整示例见插件[子模块](../../../docs/development.md#源码联调)的 `examples/workbench`
|
||||
|
||||
### 图标资源
|
||||
|
||||
在作者清单的 `icon` 中指定图标,并在 `resources` 中登记对应路径与 MIME 类型,配置方式见
|
||||
[SDK 插件图标](../../crates/gateway-plugin/sdk/docs/manifest.md#插件图标)。图标与其他声明资源一样参与打包和摘要计算,
|
||||
无需额外命令参数;工具保留文件原始内容,宿主安装时验证图标内容与资源限制
|
||||
按 [SDK 插件图标](../../crates/gateway-plugin/sdk/docs/manifest.md#插件图标)填写 `icon` 和 `resources`。CLI 自动打包原文件并计算摘要,宿主在安装时校验
|
||||
|
||||
## 开发
|
||||
|
||||
|
||||
@@ -183,7 +183,7 @@ async fn initialize_inner(
|
||||
PluginDistributionPorts::new(plugin_distribution, store.proxies()),
|
||||
snapshot.clone(),
|
||||
plugin_preparation,
|
||||
published_snapshot,
|
||||
published_snapshot.clone(),
|
||||
store.plugin_state(),
|
||||
)),
|
||||
key_usage,
|
||||
@@ -201,6 +201,7 @@ async fn initialize_inner(
|
||||
store.account_groups(),
|
||||
store.account_runtime(),
|
||||
snapshot.clone(),
|
||||
published_snapshot,
|
||||
)),
|
||||
client_keys: Arc::new(DefaultClientKeyService::new(
|
||||
store.client_keys(),
|
||||
|
||||
@@ -4,7 +4,7 @@ use std::collections::BTreeMap;
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use gateway_core::{
|
||||
account::{AccountStatusFacts, FastMode},
|
||||
account::{AccountModelAccess, AccountStatusFacts, FastMode},
|
||||
routing::AccountGroupId,
|
||||
};
|
||||
|
||||
@@ -14,15 +14,25 @@ use super::{PageSize, Revision, observability::DecimalAmount};
|
||||
pub fn validate_group_fields(
|
||||
name: &str,
|
||||
description: Option<&str>,
|
||||
) -> Result<(), super::AdminModelError> {
|
||||
if name.trim() != name
|
||||
|| name.is_empty()
|
||||
|| name.chars().count() > 100
|
||||
|| name.chars().any(char::is_control)
|
||||
|| description
|
||||
.is_some_and(|value| value.len() > 4096 || value.chars().any(char::is_control))
|
||||
{
|
||||
return Err(super::AdminModelError::InvalidAccountGroupFields);
|
||||
) -> Result<(), super::AdminError> {
|
||||
if name.trim().is_empty() {
|
||||
return Err(super::AdminError::invalid("请输入账号组名称"));
|
||||
}
|
||||
if name.chars().count() > 100 {
|
||||
return Err(super::AdminError::invalid("账号组名称最多 100 个字符"));
|
||||
}
|
||||
if name.trim() != name || name.chars().any(char::is_control) {
|
||||
return Err(super::AdminError::invalid(
|
||||
"账号组名称不能包含首尾空白或控制字符",
|
||||
));
|
||||
}
|
||||
if let Some(description) = description {
|
||||
if description.len() > 4096 {
|
||||
return Err(super::AdminError::invalid("账号组描述最多 4096 字节"));
|
||||
}
|
||||
if description.chars().any(char::is_control) {
|
||||
return Err(super::AdminError::invalid("账号组描述不能包含控制字符"));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -102,6 +112,8 @@ pub struct AccountGroupListQuery {
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct AccountGroupRecord {
|
||||
pub allow_image_generation: bool,
|
||||
pub allow_subagents: bool,
|
||||
pub model_access: AccountModelAccess,
|
||||
pub fast_mode: FastMode,
|
||||
pub id: AccountGroupId,
|
||||
pub name: String,
|
||||
@@ -142,6 +154,8 @@ pub struct AccountGroupOptionsPage {
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct CreateAccountGroup {
|
||||
pub allow_image_generation: bool,
|
||||
pub allow_subagents: bool,
|
||||
pub model_access: AccountModelAccess,
|
||||
pub fast_mode: FastMode,
|
||||
pub name: String,
|
||||
pub description: Option<String>,
|
||||
@@ -152,6 +166,8 @@ pub struct CreateAccountGroup {
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct NewAccountGroup {
|
||||
pub allow_image_generation: bool,
|
||||
pub allow_subagents: bool,
|
||||
pub model_access: AccountModelAccess,
|
||||
pub fast_mode: FastMode,
|
||||
pub id: AccountGroupId,
|
||||
pub name: String,
|
||||
@@ -163,6 +179,8 @@ pub struct NewAccountGroup {
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct UpdateAccountGroup {
|
||||
pub allow_image_generation: Option<bool>,
|
||||
pub allow_subagents: Option<bool>,
|
||||
pub model_access: Option<AccountModelAccess>,
|
||||
pub fast_mode: Option<FastMode>,
|
||||
pub id: AccountGroupId,
|
||||
pub name: String,
|
||||
|
||||
@@ -36,6 +36,7 @@ pub enum MutationAuditOperation {
|
||||
PluginSourceCredentialCreate,
|
||||
PluginSourceCredentialDelete,
|
||||
ClientApiKeyUpdateBudgetLimits,
|
||||
ClientApiKeyBatchUpdate,
|
||||
ClientApiKeyCreate,
|
||||
ClientApiKeyUpdate,
|
||||
ClientApiKeyEnabled { enabled: bool },
|
||||
@@ -97,6 +98,7 @@ impl MutationAuditOperation {
|
||||
Self::PluginSourceCredentialCreate => ("create", "plugin_source_credential"),
|
||||
Self::PluginSourceCredentialDelete => ("delete", "plugin_source_credential"),
|
||||
Self::ClientApiKeyUpdateBudgetLimits => ("update_budget_limits", "client_api_key"),
|
||||
Self::ClientApiKeyBatchUpdate => ("batch_update", "client_api_key"),
|
||||
Self::ClientApiKeyCreate => ("create", "client_api_key"),
|
||||
Self::ClientApiKeyUpdate => ("update", "client_api_key"),
|
||||
Self::ClientApiKeyEnabled { enabled } => {
|
||||
|
||||
@@ -218,6 +218,66 @@ pub struct UpdateClientKey {
|
||||
pub weekly_limit_usd: Option<gateway_core::metering::Decimal>,
|
||||
}
|
||||
|
||||
/// 批量编辑只覆盖明确选择的互联选项
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct ClientPeerSharingPatch {
|
||||
pub enabled: Option<bool>,
|
||||
pub share_pool: Option<bool>,
|
||||
pub share_quota: Option<bool>,
|
||||
pub share_plans: Option<bool>,
|
||||
}
|
||||
|
||||
impl ClientPeerSharingPatch {
|
||||
#[must_use]
|
||||
pub fn is_empty(self) -> bool {
|
||||
self == Self::default()
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn apply(
|
||||
self,
|
||||
current: gateway_core::policy::ClientPeerSharing,
|
||||
) -> gateway_core::policy::ClientPeerSharing {
|
||||
gateway_core::policy::ClientPeerSharing {
|
||||
enabled: self.enabled.unwrap_or(current.enabled),
|
||||
share_pool: self.share_pool.unwrap_or(current.share_pool),
|
||||
share_quota: self.share_quota.unwrap_or(current.share_quota),
|
||||
share_plans: self.share_plans.unwrap_or(current.share_plans),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 对各个密钥独立应用同一份局部设置
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct BatchUpdateClientKeys {
|
||||
pub ids: Vec<ClientApiKeyId>,
|
||||
pub group_ids: Option<Vec<AccountGroupId>>,
|
||||
pub enabled: Option<bool>,
|
||||
pub max_concurrency: Option<u64>,
|
||||
pub requests_per_minute: Option<u64>,
|
||||
pub daily_limit_usd: Option<gateway_core::metering::Decimal>,
|
||||
pub weekly_limit_usd: Option<gateway_core::metering::Decimal>,
|
||||
pub peer_sharing: ClientPeerSharingPatch,
|
||||
pub request_profile_override_updates: ProviderRequestProfileOverrideUpdates,
|
||||
pub installation_seed: Option<uuid::Uuid>,
|
||||
}
|
||||
|
||||
impl BatchUpdateClientKeys {
|
||||
#[must_use]
|
||||
pub fn has_changes(&self) -> bool {
|
||||
self.group_ids.is_some()
|
||||
|| self.enabled.is_some()
|
||||
|| self.max_concurrency.is_some()
|
||||
|| self.requests_per_minute.is_some()
|
||||
|| self.daily_limit_usd.is_some()
|
||||
|| self.weekly_limit_usd.is_some()
|
||||
|| !self.peer_sharing.is_empty()
|
||||
|| !self.request_profile_override_updates.is_empty()
|
||||
|| self.installation_seed.is_some()
|
||||
}
|
||||
}
|
||||
|
||||
/// 修改 Client Key 启用状态
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct SetClientKeyEnabled {
|
||||
@@ -257,9 +317,69 @@ impl ClientKeyBudgetWindowPeriod {
|
||||
}
|
||||
}
|
||||
|
||||
/// 手动对齐只需要账号身份,列表不读取凭据、额度或用量
|
||||
#[derive(serde::Serialize, Debug)]
|
||||
pub struct BudgetReferenceAccount {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub email: Option<String>,
|
||||
pub provider: String,
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize, Debug)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct BudgetReferenceAccountPage {
|
||||
pub items: Vec<BudgetReferenceAccount>,
|
||||
pub next_cursor: Option<String>,
|
||||
}
|
||||
|
||||
/// 管理员可预览的账号窗口保留 Provider 的稳定标识
|
||||
pub struct ClientKeyBudgetReference {
|
||||
pub key: String,
|
||||
pub label: String,
|
||||
pub period: ClientKeyBudgetWindowPeriod,
|
||||
pub bounds: ExplicitClientKeyBudgetWindow,
|
||||
}
|
||||
|
||||
impl ClientKeyBudgetReference {
|
||||
/// 只取整账号当前的日/周窗口,对齐结束后恢复密钥自身周期
|
||||
pub fn from_current_window(
|
||||
window: super::provider_credentials::ProviderQuotaWindow,
|
||||
now: DateTime<Utc>,
|
||||
) -> Option<Self> {
|
||||
use super::provider_credentials::QuotaLocalUsageAttribution;
|
||||
if window.local_usage_attribution != QuotaLocalUsageAttribution::AccountWide
|
||||
|| window
|
||||
.upstream
|
||||
.as_ref()
|
||||
.is_some_and(|upstream| upstream.stale)
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let period = match window.window_seconds? {
|
||||
86_400 => ClientKeyBudgetWindowPeriod::Daily,
|
||||
604_800 => ClientKeyBudgetWindowPeriod::Weekly,
|
||||
_ => return None,
|
||||
};
|
||||
let (starts_at, ends_at) = window.observed_bounds()?;
|
||||
(starts_at..ends_at).contains(&now).then_some(Self {
|
||||
key: window.key,
|
||||
label: window.label,
|
||||
period,
|
||||
bounds: ExplicitClientKeyBudgetWindow {
|
||||
starts_at,
|
||||
ends_at,
|
||||
resume_calendar: true,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// 按请求完成时间归属的左闭右开预算窗口
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct ExplicitClientKeyBudgetWindow {
|
||||
/// 一次性对齐的窗口到期后恢复原生滚动
|
||||
pub resume_calendar: bool,
|
||||
pub starts_at: DateTime<Utc>,
|
||||
pub ends_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ pub mod provider_credentials;
|
||||
pub mod proxies;
|
||||
pub mod quota_forecast;
|
||||
pub mod quota_forecast_sampling;
|
||||
pub mod quota_history;
|
||||
pub mod reset_credit_automation;
|
||||
pub mod retention;
|
||||
pub mod settings;
|
||||
@@ -235,8 +236,6 @@ pub enum AdminModelError {
|
||||
InvalidPageSize(u16),
|
||||
#[error("observability page size {0} is outside 1..=100")]
|
||||
InvalidObservabilityPageSize(u16),
|
||||
#[error("account group name or description is invalid")]
|
||||
InvalidAccountGroupFields,
|
||||
#[error("client key group IDs are duplicated or exceed 1000 entries")]
|
||||
InvalidClientKeyGroups,
|
||||
#[error("client key page size must be inside 1..=65535")]
|
||||
|
||||
@@ -158,7 +158,7 @@ impl OtherRequestOutcome {
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
|
||||
pub struct UsageFilter {
|
||||
pub client_api_key_name: Option<String>,
|
||||
pub provider_account_name: Option<String>,
|
||||
pub provider_account_search: Option<String>,
|
||||
pub client_api_key_ref: Option<String>,
|
||||
pub request_id: Option<String>,
|
||||
pub provider_account_ref: Option<String>,
|
||||
@@ -180,7 +180,7 @@ pub struct UsageFilter {
|
||||
pub enum UsageRecordStatus {
|
||||
#[default]
|
||||
Success,
|
||||
Error,
|
||||
Error(Option<UsageErrorOrigin>),
|
||||
All,
|
||||
}
|
||||
|
||||
@@ -648,6 +648,56 @@ pub struct AttemptMetrics {
|
||||
pub costs: Vec<CurrencyCost>,
|
||||
}
|
||||
|
||||
/// 管理员按当前指标排列模型与趋势,值只来自固定的聚合列
|
||||
#[derive(Debug, Clone, Copy, Default, serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum KeyUsageMetric {
|
||||
#[default]
|
||||
CostUsd,
|
||||
Tokens,
|
||||
Requests,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct KeyUsageTotals {
|
||||
pub requests: u64,
|
||||
pub tokens: Option<u64>,
|
||||
pub cost_usd: Option<String>,
|
||||
pub has_missing_cost: bool,
|
||||
pub has_estimated_cost: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct KeyModelRow {
|
||||
pub model: String,
|
||||
pub totals: KeyUsageTotals,
|
||||
pub peak_requests: u64,
|
||||
pub active_days: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct KeyTrendPoint {
|
||||
pub date: chrono::NaiveDate,
|
||||
pub value: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct KeyTrendSeries {
|
||||
/// 空字符串表示未记录模型,None 表示合并的其他模型
|
||||
pub model: Option<String>,
|
||||
pub points: Vec<KeyTrendPoint>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
|
||||
pub struct KeyStatisticsOverview {
|
||||
pub totals: KeyUsageTotals,
|
||||
pub series: Vec<KeyTrendSeries>,
|
||||
#[serde(rename = "modelCount")]
|
||||
pub model_count: u64,
|
||||
}
|
||||
|
||||
/// 时间序列粒度
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum Granularity {
|
||||
@@ -814,6 +864,17 @@ pub enum UsageErrorOrigin {
|
||||
Unknown,
|
||||
}
|
||||
|
||||
/// 来源规则所依赖的已保存事实,供内存投影与持久层分页查询共同解释
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum UsageErrorEvidence {
|
||||
ProviderErrorCode,
|
||||
UpstreamStatusAtLeast(u16),
|
||||
SavedUpstreamErrorOrClose,
|
||||
ErrorKind(&'static [&'static str]),
|
||||
Continuation(&'static [&'static str]),
|
||||
ConnectionExit(&'static [&'static str]),
|
||||
}
|
||||
|
||||
impl UsageErrorOrigin {
|
||||
#[must_use]
|
||||
pub const fn as_str(self) -> &'static str {
|
||||
|
||||
@@ -700,6 +700,7 @@ pub struct PreparedCredentialRotationFacts {
|
||||
pub preserve_profile: bool,
|
||||
/// 连接配置变更保留当前凭据健康状态与错误事实
|
||||
pub preserve_credential_state: bool,
|
||||
pub preserve_access_token_expiry: bool,
|
||||
pub provider_material: ProviderDocument,
|
||||
pub has_refresh_token: bool,
|
||||
pub access_token_expires_at: Option<DateTime<Utc>>,
|
||||
@@ -819,6 +820,14 @@ pub struct ProviderQuotaWindow {
|
||||
pub provider_data: Option<ProviderDocument>,
|
||||
}
|
||||
|
||||
impl ProviderQuotaWindow {
|
||||
pub(crate) fn observed_bounds(&self) -> Option<(DateTime<Utc>, DateTime<Utc>)> {
|
||||
let ends_at = self.reset_at?;
|
||||
let duration = chrono::Duration::try_seconds(i64::try_from(self.window_seconds?).ok()?)?;
|
||||
Some((ends_at.checked_sub_signed(duration)?, ends_at))
|
||||
}
|
||||
}
|
||||
|
||||
/// 账号用量统计周期,完整周/月额度优先,随后使用 Provider 提供的本地滚动日统计
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub enum AccountUsagePeriod {
|
||||
|
||||
@@ -9,7 +9,6 @@ use super::{PageSize, Revision, account_groups::AccountGroupRef};
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum AccountProxySelection {
|
||||
Direct,
|
||||
Url(OutboundProxy),
|
||||
Saved(String),
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
//! 单个额度桶的历史观测;原始文档只在宿主内交给 Provider 解释
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::{observability::TimeRange, provider_credentials::ProviderDocument};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct QuotaHistoryQuery {
|
||||
pub window_key: String,
|
||||
pub range: TimeRange,
|
||||
pub cursor: Option<QuotaHistoryCursor>,
|
||||
}
|
||||
|
||||
impl QuotaHistoryQuery {
|
||||
/// 起点基线最多回看一个查询区间
|
||||
pub fn scan_start(&self) -> DateTime<Utc> {
|
||||
self.range
|
||||
.start
|
||||
.checked_sub_signed(self.range.end - self.range.start)
|
||||
.unwrap_or(DateTime::<Utc>::MIN_UTC)
|
||||
}
|
||||
}
|
||||
|
||||
/// 完成时间可能相同,继续读取时保留数据库精度和请求 ID
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct QuotaHistoryCursor {
|
||||
pub completed_at: DateTime<Utc>,
|
||||
pub id: String,
|
||||
}
|
||||
|
||||
pub struct QuotaDocumentPage {
|
||||
pub documents: Vec<(DateTime<Utc>, ProviderDocument)>,
|
||||
pub next_cursor: Option<QuotaHistoryCursor>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct QuotaReading {
|
||||
pub plan_type: Option<String>,
|
||||
pub used_percent: f64,
|
||||
pub reset_at: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
pub type QuotaHistoryParser = fn(&ProviderDocument, &str) -> Option<QuotaReading>;
|
||||
|
||||
pub struct QuotaHistoryPoint {
|
||||
pub observed_at: DateTime<Utc>,
|
||||
pub reading: QuotaReading,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct QuotaHistoryPage {
|
||||
pub samples: Vec<QuotaHistoryPoint>,
|
||||
pub baseline: Option<QuotaHistoryPoint>,
|
||||
pub next_cursor: Option<QuotaHistoryCursor>,
|
||||
}
|
||||
|
||||
impl QuotaHistoryPage {
|
||||
pub fn from_documents(
|
||||
page: QuotaDocumentPage,
|
||||
query: &QuotaHistoryQuery,
|
||||
parse: QuotaHistoryParser,
|
||||
) -> Self {
|
||||
let mut result = Self {
|
||||
next_cursor: page.next_cursor,
|
||||
..Self::default()
|
||||
};
|
||||
for (observed_at, document) in page.documents {
|
||||
let Some(reading) = parse(&document, &query.window_key) else {
|
||||
continue;
|
||||
};
|
||||
let point = QuotaHistoryPoint {
|
||||
observed_at,
|
||||
reading,
|
||||
};
|
||||
if observed_at < query.range.start {
|
||||
// 倒序扫描遇到的第一个同桶读数就是最近基线,无需再向前翻页
|
||||
result.baseline = Some(point);
|
||||
result.next_cursor = None;
|
||||
break;
|
||||
}
|
||||
result.samples.push(point);
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
@@ -45,6 +45,8 @@ pub use gateway_core::account::RotationStrategy;
|
||||
/// 运行设置的共同值;版本、Provider 画像更新和数据库秘密由外层类型拥有
|
||||
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
pub struct RuntimeSettingsValues {
|
||||
pub openai_oauth_guardian_review_model: bool,
|
||||
pub openai_request_country: gateway_core::account::OpenAiRequestCountry,
|
||||
pub codex_privacy_policy: gateway_core::settings::privacy::CodexPrivacyPolicy,
|
||||
pub request_location_enabled: bool,
|
||||
pub request_location: gateway_core::account::RequestLocation,
|
||||
@@ -73,7 +75,6 @@ pub struct RuntimeSettingsValues {
|
||||
pub account_warmup_enabled: bool,
|
||||
pub account_warmup_cron: String,
|
||||
pub account_warmup_model: Option<String>,
|
||||
#[serde(default)]
|
||||
pub reset_credit_on_low_weekly_quota: bool,
|
||||
pub reset_credit_lead_minutes: u32,
|
||||
pub reset_credit_max_weekly_remaining_percent: Option<u32>,
|
||||
@@ -200,6 +201,9 @@ impl RuntimeSettingsValues {
|
||||
{
|
||||
return Err("reset_credit_max_weekly_remaining_percent");
|
||||
}
|
||||
self.openai_request_country
|
||||
.validate()
|
||||
.map_err(|_| "openai_request_country")?;
|
||||
self.request_location
|
||||
.validate()
|
||||
.map_err(|_| "request_location")?;
|
||||
|
||||
@@ -34,6 +34,12 @@ pub trait PluginAccountAccess: Send + Sync {
|
||||
Err(AdminError::unavailable("插件额度事实查询暂不可用"))
|
||||
}
|
||||
|
||||
async fn quota_history(
|
||||
&self,
|
||||
account: &ProviderAccountId,
|
||||
query: crate::model::quota_history::QuotaHistoryQuery,
|
||||
) -> Result<crate::model::quota_history::QuotaHistoryPage, AdminError>;
|
||||
|
||||
/// 通过 Provider 原生管理路径刷新额度;不向插件暴露凭据或执行额度重置
|
||||
async fn refresh_quota(
|
||||
&self,
|
||||
|
||||
@@ -231,6 +231,19 @@ pub trait ProviderAdmin: Send + Sync {
|
||||
self.preview_client_profile(key)
|
||||
}
|
||||
|
||||
/// 在批量更新中合并本地身份;缺省、清除和替换分别保留各自语义,不能执行 I/O
|
||||
fn merge_batch_key_profile(
|
||||
&self,
|
||||
current: Option<&gateway_core::account::OpaqueProviderData>,
|
||||
update: Option<&Option<gateway_core::account::OpaqueProviderData>>,
|
||||
installation_seed: Option<uuid::Uuid>,
|
||||
) -> Result<Option<gateway_core::account::OpaqueProviderData>, ProviderAdminError> {
|
||||
if installation_seed.is_some() {
|
||||
return Err(ProviderAdminError::new(ProviderAdminErrorKind::Unsupported));
|
||||
}
|
||||
Ok(update.map_or_else(|| current.cloned(), Clone::clone))
|
||||
}
|
||||
|
||||
/// 提供最终套餐展示名称;未覆盖时采用通用的大驼峰格式
|
||||
fn plan_type_display(&self, plan_type: &str) -> String {
|
||||
default_plan_type_display(plan_type)
|
||||
@@ -349,6 +362,11 @@ pub trait ProviderAdmin: Send + Sync {
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// 历史文档由 Provider 按稳定窗口键解释,不支持的 Provider 不扫描请求记录
|
||||
fn quota_history_parser(&self) -> Option<crate::model::quota_history::QuotaHistoryParser> {
|
||||
None
|
||||
}
|
||||
|
||||
/// 历史观测的协议字段仅由具体 Provider 解释;不支持时保留累计估算
|
||||
fn quota_forecast_observation(
|
||||
&self,
|
||||
|
||||
@@ -22,10 +22,11 @@ use crate::model::{
|
||||
},
|
||||
auth::{AdminAuditEvent, AuthSession},
|
||||
client_keys::{
|
||||
ClientKeyBudgetMutationOrigin, ClientKeyBudgetWindow, ClientKeyBudgetWindowPeriod,
|
||||
ClientKeyListQuery, ClientKeyPage, ClientKeyRecord, ClientKeySecret, DeleteClientKey,
|
||||
NewClientKey, ResetClientKeyBudget, SetClientKeyBudgetWindow, SetClientKeyEnabled,
|
||||
UpdateClientKey, UpdateClientKeyBudgetLimits,
|
||||
BatchUpdateClientKeys, BudgetReferenceAccountPage, ClientKeyBudgetMutationOrigin,
|
||||
ClientKeyBudgetWindow, ClientKeyBudgetWindowPeriod, ClientKeyListQuery, ClientKeyPage,
|
||||
ClientKeyRecord, ClientKeySecret, DeleteClientKey, NewClientKey, ResetClientKeyBudget,
|
||||
SetClientKeyBudgetWindow, SetClientKeyEnabled, UpdateClientKey,
|
||||
UpdateClientKeyBudgetLimits,
|
||||
},
|
||||
observability::{
|
||||
DashboardObservation, DashboardQuery, DiagnosticDimension, DiagnosticsObservation,
|
||||
@@ -109,6 +110,13 @@ impl AdminStoreError {
|
||||
|
||||
pub type AdminStoreResult<T> = Result<T, AdminStoreError>;
|
||||
|
||||
/// 在持有密钥行锁时用 Provider 准备好的纯合并操作更新原有画像
|
||||
pub type BatchClientKeyProfileMerger = Box<
|
||||
dyn Fn(&mut crate::model::client_keys::ProviderRequestProfileOverrides) -> AdminStoreResult<()>
|
||||
+ Send
|
||||
+ Sync,
|
||||
>;
|
||||
|
||||
/// 账号目录与公共账号写操作
|
||||
#[async_trait]
|
||||
pub trait AccountStore:
|
||||
@@ -180,6 +188,16 @@ pub trait AccountStore:
|
||||
windows: &[AccountUsageWindowQuery],
|
||||
) -> AdminStoreResult<Vec<AccountUsageWindowResult>>;
|
||||
|
||||
/// 按完成时间倒序读取一页保留的请求观测,不按本地计费结果筛选
|
||||
async fn quota_documents(
|
||||
&self,
|
||||
account: &gateway_core::account::ProviderAccountId,
|
||||
provider: &gateway_core::routing::ProviderKind,
|
||||
created_at: DateTime<Utc>,
|
||||
range: TimeRange,
|
||||
before: Option<&crate::model::quota_history::QuotaHistoryCursor>,
|
||||
) -> AdminStoreResult<crate::model::quota_history::QuotaDocumentPage>;
|
||||
|
||||
/// 从同一数据库语句取得截止快照的累计用量和有界历史观测
|
||||
async fn load_quota_forecast_history(
|
||||
&self,
|
||||
@@ -367,6 +385,18 @@ pub trait AuthStore: Send + Sync {
|
||||
/// Client API Key 资料读取与管理写入
|
||||
#[async_trait]
|
||||
pub trait ClientKeyStore: Send + Sync {
|
||||
async fn client_key_budget(
|
||||
&self,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
) -> AdminStoreResult<Option<gateway_core::engine::budget::ClientBudgetStatus>>;
|
||||
|
||||
async fn budget_reference_accounts(
|
||||
&self,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
search: &str,
|
||||
after: &str,
|
||||
) -> AdminStoreResult<BudgetReferenceAccountPage>;
|
||||
|
||||
async fn client_key_budget_window(
|
||||
&self,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
@@ -405,6 +435,13 @@ pub trait ClientKeyStore: Send + Sync {
|
||||
context: &MutationContext,
|
||||
) -> AdminStoreResult<(Revision, ClientKeyRecord)>;
|
||||
|
||||
async fn batch_update_client_keys(
|
||||
&self,
|
||||
command: BatchUpdateClientKeys,
|
||||
merge_profiles: BatchClientKeyProfileMerger,
|
||||
context: &MutationContext,
|
||||
) -> AdminStoreResult<Revision>;
|
||||
|
||||
async fn set_client_key_enabled(
|
||||
&self,
|
||||
command: SetClientKeyEnabled,
|
||||
@@ -470,6 +507,7 @@ pub trait AccountGroupStore: Send + Sync {
|
||||
context: &MutationContext,
|
||||
) -> AdminStoreResult<AccountGroupMutation>;
|
||||
|
||||
/// 仍有 API Key 引用分组时返回 Conflict
|
||||
async fn delete_account_group(
|
||||
&self,
|
||||
command: DeleteAccountGroup,
|
||||
@@ -507,6 +545,19 @@ pub trait ObservabilityStore: Send + Sync {
|
||||
granularity: Granularity,
|
||||
) -> AdminStoreResult<Vec<RequestMetricPoint>>;
|
||||
|
||||
async fn client_key_statistics(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
query: crate::model::observability::KeyUsageMetric,
|
||||
) -> AdminStoreResult<crate::model::observability::KeyStatisticsOverview>;
|
||||
|
||||
async fn client_key_models(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
) -> AdminStoreResult<Vec<crate::model::observability::KeyModelRow>>;
|
||||
|
||||
async fn usage_trend(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
|
||||
@@ -9,8 +9,8 @@ use std::{
|
||||
use async_trait::async_trait;
|
||||
use gateway_core::{
|
||||
account::{AccountStatus, resolve_account_status},
|
||||
routing::AccountGroupId,
|
||||
runtime::SnapshotControl,
|
||||
routing::{AccountGroupId, UpstreamModelId},
|
||||
runtime::{RuntimeSnapshotHandle, SnapshotControl},
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
@@ -24,7 +24,7 @@ use crate::{
|
||||
NewAccountGroup, SetAccountGroupEnabled, UpdateAccountGroup,
|
||||
},
|
||||
},
|
||||
ports::store::{AccountGroupStore, AccountRuntimeStore},
|
||||
ports::store::{AccountGroupStore, AccountRuntimeStore, AdminStoreErrorKind},
|
||||
};
|
||||
|
||||
use super::{map_store_error, publish_committed};
|
||||
@@ -32,6 +32,7 @@ use super::{map_store_error, publish_committed};
|
||||
/// 面向 API 的账号分组管理服务
|
||||
#[async_trait]
|
||||
pub trait AccountGroupService: Send + Sync {
|
||||
fn models(&self, group: &AccountGroupId) -> Result<Vec<UpstreamModelId>, AdminError>;
|
||||
async fn list(&self, query: AccountGroupListQuery) -> Result<AccountGroupPage, AdminError>;
|
||||
async fn list_options(
|
||||
&self,
|
||||
@@ -64,6 +65,7 @@ pub(crate) struct DefaultAccountGroupService {
|
||||
store: Arc<dyn AccountGroupStore>,
|
||||
runtime: Arc<dyn AccountRuntimeStore>,
|
||||
snapshot: Arc<dyn SnapshotControl>,
|
||||
published_snapshot: RuntimeSnapshotHandle,
|
||||
}
|
||||
|
||||
impl DefaultAccountGroupService {
|
||||
@@ -73,12 +75,14 @@ impl DefaultAccountGroupService {
|
||||
store: Arc<dyn AccountGroupStore>,
|
||||
runtime: Arc<dyn AccountRuntimeStore>,
|
||||
snapshot: Arc<dyn SnapshotControl>,
|
||||
published_snapshot: RuntimeSnapshotHandle,
|
||||
) -> Self {
|
||||
Self {
|
||||
upstream_info,
|
||||
store,
|
||||
runtime,
|
||||
snapshot,
|
||||
published_snapshot,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,6 +135,14 @@ impl DefaultAccountGroupService {
|
||||
|
||||
#[async_trait]
|
||||
impl AccountGroupService for DefaultAccountGroupService {
|
||||
fn models(&self, group: &AccountGroupId) -> Result<Vec<UpstreamModelId>, AdminError> {
|
||||
let snapshot = self
|
||||
.published_snapshot
|
||||
.acquire()
|
||||
.map_err(|_| AdminError::unavailable("模型目录尚未就绪"))?;
|
||||
Ok(snapshot.upstream_models_for_group(group))
|
||||
}
|
||||
|
||||
async fn list(&self, query: AccountGroupListQuery) -> Result<AccountGroupPage, AdminError> {
|
||||
let mut page = self
|
||||
.store
|
||||
@@ -159,8 +171,7 @@ impl AccountGroupService for DefaultAccountGroupService {
|
||||
crate::model::account_groups::validate_group_fields(
|
||||
&command.name,
|
||||
command.description.as_deref(),
|
||||
)
|
||||
.map_err(|_| AdminError::invalid("账号组请求不合法"))?;
|
||||
)?;
|
||||
let id = AccountGroupId::new(format!("grp_{}", Uuid::now_v7().simple()))
|
||||
.map_err(|_| AdminError::internal("创建账号组 ID 失败"))?;
|
||||
self.publish(
|
||||
@@ -168,6 +179,8 @@ impl AccountGroupService for DefaultAccountGroupService {
|
||||
.create_account_group(
|
||||
NewAccountGroup {
|
||||
allow_image_generation: command.allow_image_generation,
|
||||
allow_subagents: command.allow_subagents,
|
||||
model_access: command.model_access,
|
||||
id,
|
||||
name: command.name,
|
||||
description: command.description,
|
||||
@@ -189,8 +202,7 @@ impl AccountGroupService for DefaultAccountGroupService {
|
||||
crate::model::account_groups::validate_group_fields(
|
||||
&command.name,
|
||||
command.description.as_deref(),
|
||||
)
|
||||
.map_err(|_| AdminError::invalid("账号组请求不合法"))?;
|
||||
)?;
|
||||
self.publish(self.store.update_account_group(command, context).await)
|
||||
.await
|
||||
}
|
||||
@@ -209,8 +221,12 @@ impl AccountGroupService for DefaultAccountGroupService {
|
||||
context: &MutationContext,
|
||||
command: DeleteAccountGroup,
|
||||
) -> Result<AccountGroupMutation, AdminError> {
|
||||
self.publish(self.store.delete_account_group(command, context).await)
|
||||
.await
|
||||
match self.store.delete_account_group(command, context).await {
|
||||
Err(error) if error.kind() == AdminStoreErrorKind::Conflict => {
|
||||
Err(AdminError::conflict("分组仍绑定 API Key,请先解除绑定").with_source(error))
|
||||
}
|
||||
result => self.publish(result).await,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,11 +20,13 @@ use gateway_core::{
|
||||
use crate::{
|
||||
model::{
|
||||
AdminError, MutationContext,
|
||||
account_groups::AccountGroupRef,
|
||||
accounts::{
|
||||
AccountConnectionTestEvent, AccountConnectionTestEventStream, AccountListQuery,
|
||||
AccountPageItem, AccountUpdateResult, AccountUsage, AccountUsageWindowQuery,
|
||||
AccountsUpdateResult, BatchUpdateAccounts, UpdateAccount,
|
||||
},
|
||||
client_keys::ClientKeyBudgetReference,
|
||||
observability::TimeRange,
|
||||
provider_credentials::{
|
||||
AccountDirectoryItem, AccountDirectoryPage, AccountExportBundle, AccountPersonalInfo,
|
||||
@@ -107,6 +109,12 @@ pub trait AccountsService: Send + Sync {
|
||||
refresh: bool,
|
||||
) -> Result<AccountDirectoryItem, AdminError>;
|
||||
|
||||
async fn key_budget_references(
|
||||
&self,
|
||||
account_id: &ProviderAccountId,
|
||||
key_groups: &[AccountGroupRef],
|
||||
) -> Result<Vec<ClientKeyBudgetReference>, AdminError>;
|
||||
|
||||
async fn quota_forecast(
|
||||
&self,
|
||||
account_id: &ProviderAccountId,
|
||||
@@ -972,6 +980,48 @@ impl AccountsService for DefaultAccountsService {
|
||||
self.load_directory_item(account_id, refresh).await
|
||||
}
|
||||
|
||||
async fn key_budget_references(
|
||||
&self,
|
||||
account_id: &ProviderAccountId,
|
||||
key_groups: &[AccountGroupRef],
|
||||
) -> Result<Vec<ClientKeyBudgetReference>, AdminError> {
|
||||
let (stored, provider) = self.provider_for_account(account_id).await?;
|
||||
if !key_groups.is_empty()
|
||||
&& !stored.account.groups.iter().any(|account_group| {
|
||||
account_group.enabled && key_groups.iter().any(|group| group.id == account_group.id)
|
||||
})
|
||||
{
|
||||
return Err(AdminError::bad_request("参考账号不在此密钥的账号范围内"));
|
||||
}
|
||||
// 只取已观测的额度边界,预览与确认都不触发上游刷新或账号用量聚合
|
||||
let quota = match super::peer_link::upstream::read_quota(
|
||||
self.upstream_info.as_deref(),
|
||||
provider.as_ref(),
|
||||
&stored.account,
|
||||
ProviderQuotaRequest {
|
||||
account_id: account_id.clone(),
|
||||
refresh: false,
|
||||
rolling_usage: None,
|
||||
},
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(quota) => quota,
|
||||
Err(error)
|
||||
if error.kind() == crate::ports::provider::ProviderAdminErrorKind::Unsupported =>
|
||||
{
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
Err(error) => return Err(map_provider_error(error, "provider quota")),
|
||||
};
|
||||
let now = Utc::now();
|
||||
Ok(quota
|
||||
.windows
|
||||
.into_iter()
|
||||
.filter_map(|window| ClientKeyBudgetReference::from_current_window(window, now))
|
||||
.collect())
|
||||
}
|
||||
|
||||
async fn quota_forecast(
|
||||
&self,
|
||||
account_id: &ProviderAccountId,
|
||||
@@ -1304,9 +1354,7 @@ fn quota_usage_window(
|
||||
if window.local_usage_attribution != QuotaLocalUsageAttribution::AccountWide {
|
||||
return None;
|
||||
}
|
||||
let reset_at = window.reset_at?;
|
||||
let seconds = i64::try_from(window.window_seconds?).ok()?;
|
||||
let start = reset_at.checked_sub_signed(Duration::try_seconds(seconds)?)?;
|
||||
let (start, reset_at) = window.observed_bounds()?;
|
||||
let range = TimeRange::new(start, reset_at).ok()?;
|
||||
// 上游百分比以该 reset 边界定义;以当前时间回推会让本地 Token 属于另一窗口
|
||||
Some(AccountUsageWindowQuery {
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
//! Client API Key 管理用例
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::{collections::BTreeSet, sync::Arc};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};
|
||||
use gateway_core::{
|
||||
engine::budget::ClientBudgetStatus, policy::ClientApiKeyId, runtime::SnapshotControl,
|
||||
account::OpaqueProviderData, engine::budget::ClientBudgetStatus, policy::ClientApiKeyId,
|
||||
routing::ProviderKind, runtime::SnapshotControl,
|
||||
};
|
||||
use rand_core::{OsRng, RngCore as _};
|
||||
use uuid::Uuid;
|
||||
@@ -14,15 +15,17 @@ use crate::{
|
||||
model::{
|
||||
AdminError, MutationContext,
|
||||
client_keys::{
|
||||
ClientKeyBudgetMutationOrigin, ClientKeyBudgetWindow, ClientKeyBudgetWindowPeriod,
|
||||
ClientKeyCursorValue, ClientKeyListQuery, ClientKeyMutation, ClientKeyPage,
|
||||
ClientKeyRecord, ClientKeySecret, ClientKeySortField, CreateClientKey,
|
||||
CreatedClientKey, DeleteClientKey, NewClientKey, ResetClientKeyBudget,
|
||||
SetClientKeyBudgetWindow, SetClientKeyEnabled, UpdateClientKey,
|
||||
BatchUpdateClientKeys, BudgetReferenceAccountPage, ClientKeyBudgetMutationOrigin,
|
||||
ClientKeyBudgetWindow, ClientKeyBudgetWindowPeriod, ClientKeyCursorValue,
|
||||
ClientKeyListQuery, ClientKeyMutation, ClientKeyPage, ClientKeyRecord, ClientKeySecret,
|
||||
ClientKeySortField, CreateClientKey, CreatedClientKey, DeleteClientKey, NewClientKey,
|
||||
ResetClientKeyBudget, SetClientKeyBudgetWindow, SetClientKeyEnabled, UpdateClientKey,
|
||||
UpdateClientKeyBudgetLimits,
|
||||
},
|
||||
},
|
||||
ports::store::{AdminStoreError, AdminStoreErrorKind, ClientKeyStore},
|
||||
ports::store::{
|
||||
AdminStoreError, AdminStoreErrorKind, BatchClientKeyProfileMerger, ClientKeyStore,
|
||||
},
|
||||
};
|
||||
|
||||
use super::{map_store_error, publish_committed};
|
||||
@@ -30,6 +33,13 @@ use super::{map_store_error, publish_committed};
|
||||
/// API 消费的 Client Key 管理服务
|
||||
#[async_trait]
|
||||
pub trait ClientKeyService: Send + Sync {
|
||||
async fn budget_reference_accounts(
|
||||
&self,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
search: &str,
|
||||
after: &str,
|
||||
) -> Result<BudgetReferenceAccountPage, AdminError>;
|
||||
|
||||
async fn budget_window(
|
||||
&self,
|
||||
id: &ClientApiKeyId,
|
||||
@@ -55,6 +65,11 @@ pub trait ClientKeyService: Send + Sync {
|
||||
context: &MutationContext,
|
||||
command: UpdateClientKey,
|
||||
) -> Result<ClientKeyMutation, AdminError>;
|
||||
async fn batch_update(
|
||||
&self,
|
||||
context: &MutationContext,
|
||||
command: BatchUpdateClientKeys,
|
||||
) -> Result<crate::model::Revision, AdminError>;
|
||||
async fn set_enabled(
|
||||
&self,
|
||||
context: &MutationContext,
|
||||
@@ -99,10 +114,123 @@ impl DefaultClientKeyService {
|
||||
providers,
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_profile(
|
||||
&self,
|
||||
kind: &ProviderKind,
|
||||
profile: &OpaqueProviderData,
|
||||
) -> Result<(), AdminError> {
|
||||
self.providers
|
||||
.require(kind)
|
||||
.and_then(|provider| provider.preview_key_client_profile(profile, None))
|
||||
.map(|_| ())
|
||||
.map_err(|error| super::map_provider_error(error, "client profile"))
|
||||
}
|
||||
|
||||
fn batch_profile_merger(
|
||||
&self,
|
||||
command: &BatchUpdateClientKeys,
|
||||
) -> Result<BatchClientKeyProfileMerger, AdminError> {
|
||||
let mut kinds = command
|
||||
.request_profile_override_updates
|
||||
.keys()
|
||||
.cloned()
|
||||
.collect::<BTreeSet<_>>();
|
||||
let seed_kind = ProviderKind::new("openai").expect("static Provider kind is valid");
|
||||
if command.installation_seed.is_some() {
|
||||
kinds.insert(seed_kind.clone());
|
||||
}
|
||||
let mut changes = Vec::with_capacity(kinds.len());
|
||||
for kind in kinds {
|
||||
let provider = self
|
||||
.providers
|
||||
.require(&kind)
|
||||
.map_err(|error| super::map_provider_error(error, "client profile"))?;
|
||||
let update = command.request_profile_override_updates.get(&kind).cloned();
|
||||
let seed = if kind == seed_kind {
|
||||
command.installation_seed
|
||||
} else {
|
||||
None
|
||||
};
|
||||
// 先校验输入,行锁内只依据各把密钥的现值完成合并
|
||||
if let Some(Some(profile)) = update.as_ref() {
|
||||
provider
|
||||
.merge_batch_key_profile(None, update.as_ref(), seed)
|
||||
.and_then(|_| provider.preview_key_client_profile(profile, None))
|
||||
.map_err(|error| super::map_provider_error(error, "client profile"))?;
|
||||
}
|
||||
changes.push((kind, provider, update, seed));
|
||||
}
|
||||
Ok(Box::new(move |profiles| {
|
||||
for (kind, provider, update, seed) in &changes {
|
||||
let next = provider
|
||||
.merge_batch_key_profile(profiles.get(kind), update.as_ref(), *seed)
|
||||
.map_err(|error| {
|
||||
AdminStoreError::new(
|
||||
AdminStoreErrorKind::Invalid,
|
||||
"client profile",
|
||||
"invalid stored client profile",
|
||||
)
|
||||
.with_source(error)
|
||||
})?;
|
||||
if let Some(profile) = next {
|
||||
profiles.insert(kind.clone(), profile);
|
||||
} else {
|
||||
profiles.remove(kind);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ClientKeyService for DefaultClientKeyService {
|
||||
async fn batch_update(
|
||||
&self,
|
||||
context: &MutationContext,
|
||||
command: BatchUpdateClientKeys,
|
||||
) -> Result<crate::model::Revision, AdminError> {
|
||||
if command.ids.is_empty()
|
||||
|| command.ids.len() > 1000
|
||||
|| command
|
||||
.ids
|
||||
.iter()
|
||||
.collect::<std::collections::BTreeSet<_>>()
|
||||
.len()
|
||||
!= command.ids.len()
|
||||
{
|
||||
return Err(AdminError::invalid("请选择 1 至 1000 个不同的 API Key"));
|
||||
}
|
||||
if !command.has_changes() {
|
||||
return Err(AdminError::invalid("请至少选择一项要修改的密钥设置"));
|
||||
}
|
||||
if let Some(groups) = &command.group_ids {
|
||||
crate::model::client_keys::validate_group_ids(groups)
|
||||
.map_err(|_| AdminError::invalid("API Key 分组不合法"))?;
|
||||
}
|
||||
let merge_profiles = self.batch_profile_merger(&command)?;
|
||||
let revision = self
|
||||
.store
|
||||
.batch_update_client_keys(command, merge_profiles, context)
|
||||
.await
|
||||
.map_err(|error| map_store_error(error, "client API key"))?;
|
||||
publish_committed(self.snapshot.as_ref(), revision).await?;
|
||||
Ok(revision)
|
||||
}
|
||||
|
||||
async fn budget_reference_accounts(
|
||||
&self,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
search: &str,
|
||||
after: &str,
|
||||
) -> Result<BudgetReferenceAccountPage, AdminError> {
|
||||
self.store
|
||||
.budget_reference_accounts(id, search.trim(), after)
|
||||
.await
|
||||
.map_err(|error| map_store_error(error, "client API key"))
|
||||
}
|
||||
|
||||
async fn budget_window(
|
||||
&self,
|
||||
id: &ClientApiKeyId,
|
||||
@@ -141,7 +269,11 @@ impl ClientKeyService for DefaultClientKeyService {
|
||||
}
|
||||
|
||||
async fn budget(&self, id: &ClientApiKeyId) -> Result<ClientBudgetStatus, AdminError> {
|
||||
self.get(id).await.map(|key| key.budget)
|
||||
self.store
|
||||
.client_key_budget(id)
|
||||
.await
|
||||
.map_err(|error| map_store_error(error, "client API key"))?
|
||||
.ok_or_else(|| AdminError::not_found("API Key 不存在"))
|
||||
}
|
||||
|
||||
async fn update_budget_limits(
|
||||
@@ -203,10 +335,7 @@ impl ClientKeyService for DefaultClientKeyService {
|
||||
crate::model::client_keys::validate_group_ids(&command.group_ids)
|
||||
.map_err(|_| AdminError::invalid("API Key 分组不合法"))?;
|
||||
for (provider, profile) in &command.request_profile_overrides {
|
||||
self.providers
|
||||
.require(provider)
|
||||
.and_then(|provider| provider.preview_key_client_profile(profile, None))
|
||||
.map_err(|error| super::map_provider_error(error, "client profile"))?;
|
||||
self.validate_profile(provider, profile)?;
|
||||
}
|
||||
validate_peer_sharing(command.peer_sharing, &command.group_ids)?;
|
||||
let id = ClientApiKeyId::new(format!("key_{}", Uuid::now_v7().simple()))
|
||||
@@ -250,10 +379,7 @@ impl ClientKeyService for DefaultClientKeyService {
|
||||
.map_err(|_| AdminError::invalid("API Key 分组不合法"))?;
|
||||
for (provider, profile) in &command.request_profile_override_updates {
|
||||
if let Some(profile) = profile {
|
||||
self.providers
|
||||
.require(provider)
|
||||
.and_then(|provider| provider.preview_key_client_profile(profile, None))
|
||||
.map_err(|error| super::map_provider_error(error, "client profile"))?;
|
||||
self.validate_profile(provider, profile)?;
|
||||
}
|
||||
}
|
||||
if let Some(sharing) = command.peer_sharing {
|
||||
@@ -314,7 +440,6 @@ impl ClientKeyService for DefaultClientKeyService {
|
||||
|
||||
fn map_client_key_write_error(error: AdminStoreError) -> AdminError {
|
||||
match error.kind() {
|
||||
AdminStoreErrorKind::DuplicateName => AdminError::conflict("名称已存在"),
|
||||
AdminStoreErrorKind::Conflict => AdminError::conflict("API Key 已存在,请使用其他密钥"),
|
||||
_ => map_store_error(error, "client API key"),
|
||||
}
|
||||
|
||||
@@ -150,7 +150,6 @@ pub(super) async fn pending_authorization(
|
||||
None => {
|
||||
use crate::model::proxies::AccountProxySelection;
|
||||
proxy = match &command.outbound_proxy {
|
||||
Some(AccountProxySelection::Url(value)) => Some(value.clone()),
|
||||
Some(AccountProxySelection::Saved(id)) => {
|
||||
let record = proxies
|
||||
.get(id)
|
||||
|
||||
@@ -4,20 +4,15 @@ use crate::model::{AdminError, AdminErrorKind};
|
||||
use crate::ports::store::{AdminStoreError, AdminStoreErrorKind};
|
||||
|
||||
pub(super) fn map_store_error(error: AdminStoreError, resource: &'static str) -> AdminError {
|
||||
let kind = match error.kind() {
|
||||
AdminStoreErrorKind::Invalid => AdminErrorKind::Invalid,
|
||||
AdminStoreErrorKind::NotFound => AdminErrorKind::NotFound,
|
||||
AdminStoreErrorKind::StaleRevision
|
||||
| AdminStoreErrorKind::DuplicateName
|
||||
| AdminStoreErrorKind::Conflict => AdminErrorKind::Conflict,
|
||||
AdminStoreErrorKind::Unavailable => AdminErrorKind::Unavailable,
|
||||
};
|
||||
let message = match kind {
|
||||
AdminErrorKind::Invalid => "请求参数不合法",
|
||||
AdminErrorKind::NotFound => "请求的资源不存在",
|
||||
AdminErrorKind::Conflict => "当前资源状态冲突,请刷新后重试",
|
||||
AdminErrorKind::Unavailable => "依赖服务暂不可用",
|
||||
_ => "服务内部错误",
|
||||
let (kind, message) = match error.kind() {
|
||||
AdminStoreErrorKind::Invalid => (AdminErrorKind::Invalid, "请求参数不合法"),
|
||||
AdminStoreErrorKind::NotFound => (AdminErrorKind::NotFound, "请求的资源不存在"),
|
||||
AdminStoreErrorKind::StaleRevision => {
|
||||
(AdminErrorKind::Conflict, "数据已更新,请刷新后再操作")
|
||||
}
|
||||
AdminStoreErrorKind::DuplicateName => (AdminErrorKind::Conflict, "名称已存在"),
|
||||
AdminStoreErrorKind::Conflict => (AdminErrorKind::Conflict, "当前资源状态不允许此操作"),
|
||||
AdminStoreErrorKind::Unavailable => (AdminErrorKind::Unavailable, "存储服务暂不可用"),
|
||||
};
|
||||
AdminError::new(kind, message).with_source(OperationFailure {
|
||||
operation: resource,
|
||||
@@ -49,9 +44,8 @@ pub(super) fn map_provider_error(
|
||||
ProviderAdminErrorKind::Conflict => "服务商资源状态冲突,请刷新后重试",
|
||||
ProviderAdminErrorKind::BadGateway => "上游服务请求失败",
|
||||
ProviderAdminErrorKind::Ambiguous => "上游执行结果未知,请刷新状态后再决定是否重试",
|
||||
ProviderAdminErrorKind::Unavailable | ProviderAdminErrorKind::CredentialRefreshRequired => {
|
||||
"服务商暂不可用"
|
||||
}
|
||||
ProviderAdminErrorKind::Unavailable => "服务商暂不可用",
|
||||
ProviderAdminErrorKind::CredentialRefreshRequired => "账号凭据需要刷新",
|
||||
ProviderAdminErrorKind::Internal => "服务内部错误",
|
||||
};
|
||||
AdminError::new(kind, error.public_message().unwrap_or(message)).with_source(OperationFailure {
|
||||
|
||||
@@ -225,7 +225,7 @@ impl KeyUsageService for DefaultKeyUsageService {
|
||||
.list_usage_records(UsageQuery {
|
||||
status: match query.kind {
|
||||
KeyUsageRecordKind::Success => UsageRecordStatus::Success,
|
||||
KeyUsageRecordKind::Error => UsageRecordStatus::Error,
|
||||
KeyUsageRecordKind::Error => UsageRecordStatus::Error(None),
|
||||
KeyUsageRecordKind::All => UsageRecordStatus::All,
|
||||
},
|
||||
range: query.usage.range,
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
//! 从已保存的错误事实投影管理端来源,不参与执行错误分类或重试
|
||||
|
||||
use crate::model::observability::{UsageErrorOrigin, UsageListRecord, UsageRecord};
|
||||
use crate::model::observability::{
|
||||
UsageErrorEvidence, UsageErrorOrigin, UsageListRecord, UsageRecord,
|
||||
};
|
||||
|
||||
impl UsageListRecord {
|
||||
#[must_use]
|
||||
@@ -47,107 +49,124 @@ fn error_origin(
|
||||
continuation_reason: Option<&str>,
|
||||
connection_exit_reason: Option<&str>,
|
||||
) -> UsageErrorOrigin {
|
||||
if has_upstream_error
|
||||
|| upstream_code.is_some_and(|code| !code.is_empty())
|
||||
|| websocket_close_code.is_some()
|
||||
|| upstream_status_code.is_some_and(|status| status >= 400)
|
||||
|| matches!(
|
||||
continuation_reason,
|
||||
Some("upstream_rejected" | "quota_exhausted")
|
||||
)
|
||||
|| matches!(
|
||||
error_kind,
|
||||
// 这里匹配内部 errorKind;message_too_big 只由收到的上游 close 1009 产生
|
||||
Some("permission_denied" | "message_too_big" | "upstream_capacity_unavailable")
|
||||
)
|
||||
{
|
||||
return UsageErrorOrigin::Upstream;
|
||||
}
|
||||
if matches!(
|
||||
continuation_reason,
|
||||
Some(
|
||||
"scope_unavailable"
|
||||
| "pool_unavailable"
|
||||
| "fresh_connection_required"
|
||||
| "connection_busy"
|
||||
| "latest_response_mismatch"
|
||||
| "reused_connection_lost"
|
||||
| "transport_payload_too_large"
|
||||
)
|
||||
) || matches!(
|
||||
error_kind,
|
||||
Some(
|
||||
"process_interrupted"
|
||||
| "process_terminated"
|
||||
| "internal_error"
|
||||
| "model_not_found"
|
||||
| "no_available_provider"
|
||||
| "no_eligible_account"
|
||||
| "account_capacity_unavailable"
|
||||
| "middleware_capacity_unavailable"
|
||||
| "concurrency_queue_full"
|
||||
| "concurrency_queue_timeout"
|
||||
| "provider_infrastructure_unavailable"
|
||||
| "session_policy_restricted"
|
||||
| "request_policy_denied"
|
||||
| "policy_denied"
|
||||
| "budget_exhausted"
|
||||
)
|
||||
) || matches!(
|
||||
connection_exit_reason,
|
||||
Some(
|
||||
"command_channel_closed"
|
||||
| "local_close"
|
||||
| "message_receiver_closed"
|
||||
| "client_cancelled"
|
||||
| "downstream_send_failed"
|
||||
| "downstream_connection_closed"
|
||||
| "downstream_rebound"
|
||||
| "pool_shutdown"
|
||||
| "max_age_expired"
|
||||
| "idle_timeout_expired"
|
||||
| "proxy_capacity_evicted"
|
||||
| "search_activation_required"
|
||||
)
|
||||
) {
|
||||
return UsageErrorOrigin::Proxy;
|
||||
}
|
||||
if matches!(
|
||||
connection_exit_reason,
|
||||
Some(
|
||||
"upstream_close"
|
||||
| "normal_close"
|
||||
| "unexpected_binary_event"
|
||||
| "incomplete_response"
|
||||
| "failed_response"
|
||||
)
|
||||
) {
|
||||
return UsageErrorOrigin::Upstream;
|
||||
}
|
||||
if error_kind == Some("transport")
|
||||
|| matches!(
|
||||
connection_exit_reason,
|
||||
Some(
|
||||
"eof"
|
||||
| "unexpected_eof"
|
||||
| "transport_error"
|
||||
| "connection_reset"
|
||||
| "connection_aborted"
|
||||
| "broken_pipe"
|
||||
| "tcp_reset"
|
||||
| "reset_without_closing_handshake"
|
||||
| "transport_timeout"
|
||||
| "outbound_transport_error"
|
||||
| "receive_idle_timeout"
|
||||
| "pong_timeout"
|
||||
| "ping_write_timeout"
|
||||
| "pong_write_timeout"
|
||||
)
|
||||
)
|
||||
{
|
||||
return UsageErrorOrigin::Connection;
|
||||
}
|
||||
// invalid_request、protocol、rate_limited 等分类同时用于本地和上游失败
|
||||
// 发送状态与笼统的连接退出摘要也不能替代缺失的来源事实
|
||||
UsageErrorOrigin::Unknown
|
||||
use UsageErrorEvidence::*;
|
||||
UsageErrorOrigin::RULES
|
||||
.iter()
|
||||
.find(|(evidence, _)| match evidence {
|
||||
ProviderErrorCode => upstream_code.is_some_and(|code| !code.is_empty()),
|
||||
UpstreamStatusAtLeast(min) => upstream_status_code.is_some_and(|status| status >= *min),
|
||||
SavedUpstreamErrorOrClose => has_upstream_error || websocket_close_code.is_some(),
|
||||
ErrorKind(values) => error_kind.is_some_and(|value| values.contains(&value)),
|
||||
Continuation(values) => {
|
||||
continuation_reason.is_some_and(|value| values.contains(&value))
|
||||
}
|
||||
ConnectionExit(values) => {
|
||||
connection_exit_reason.is_some_and(|value| values.contains(&value))
|
||||
}
|
||||
})
|
||||
.map_or(UsageErrorOrigin::Unknown, |(_, origin)| *origin)
|
||||
}
|
||||
|
||||
impl UsageErrorOrigin {
|
||||
/// 第一条命中的规则决定来源;含义不唯一的分类在缺少其他证据时保留 unknown
|
||||
pub const RULES: &'static [(UsageErrorEvidence, Self)] = {
|
||||
use UsageErrorEvidence::*;
|
||||
&[
|
||||
(ProviderErrorCode, Self::Upstream),
|
||||
(UpstreamStatusAtLeast(400), Self::Upstream),
|
||||
(
|
||||
Continuation(&["upstream_rejected", "quota_exhausted"]),
|
||||
Self::Upstream,
|
||||
),
|
||||
// message_too_big 只由收到的上游 close 1009 产生
|
||||
(
|
||||
ErrorKind(&[
|
||||
"permission_denied",
|
||||
"message_too_big",
|
||||
"upstream_capacity_unavailable",
|
||||
]),
|
||||
Self::Upstream,
|
||||
),
|
||||
(SavedUpstreamErrorOrClose, Self::Upstream),
|
||||
(
|
||||
Continuation(&[
|
||||
"scope_unavailable",
|
||||
"pool_unavailable",
|
||||
"fresh_connection_required",
|
||||
"connection_busy",
|
||||
"latest_response_mismatch",
|
||||
"reused_connection_lost",
|
||||
"transport_payload_too_large",
|
||||
]),
|
||||
Self::Proxy,
|
||||
),
|
||||
(
|
||||
ErrorKind(&[
|
||||
"process_interrupted",
|
||||
"process_terminated",
|
||||
"internal_error",
|
||||
"model_not_found",
|
||||
"no_available_provider",
|
||||
"no_eligible_account",
|
||||
"account_capacity_unavailable",
|
||||
"middleware_capacity_unavailable",
|
||||
"concurrency_queue_full",
|
||||
"concurrency_queue_timeout",
|
||||
"provider_infrastructure_unavailable",
|
||||
"session_policy_restricted",
|
||||
"request_policy_denied",
|
||||
"policy_denied",
|
||||
"budget_exhausted",
|
||||
]),
|
||||
Self::Proxy,
|
||||
),
|
||||
(
|
||||
ConnectionExit(&[
|
||||
"command_channel_closed",
|
||||
"local_close",
|
||||
"message_receiver_closed",
|
||||
"client_cancelled",
|
||||
"downstream_send_failed",
|
||||
"downstream_connection_closed",
|
||||
"downstream_rebound",
|
||||
"pool_shutdown",
|
||||
"max_age_expired",
|
||||
"idle_timeout_expired",
|
||||
"proxy_capacity_evicted",
|
||||
"search_activation_required",
|
||||
]),
|
||||
Self::Proxy,
|
||||
),
|
||||
(
|
||||
ConnectionExit(&[
|
||||
"upstream_close",
|
||||
"normal_close",
|
||||
"unexpected_binary_event",
|
||||
"incomplete_response",
|
||||
"failed_response",
|
||||
]),
|
||||
Self::Upstream,
|
||||
),
|
||||
(ErrorKind(&["transport"]), Self::Connection),
|
||||
(
|
||||
ConnectionExit(&[
|
||||
"eof",
|
||||
"unexpected_eof",
|
||||
"transport_error",
|
||||
"connection_reset",
|
||||
"connection_aborted",
|
||||
"broken_pipe",
|
||||
"tcp_reset",
|
||||
"reset_without_closing_handshake",
|
||||
"transport_timeout",
|
||||
"outbound_transport_error",
|
||||
"receive_idle_timeout",
|
||||
"pong_timeout",
|
||||
"ping_write_timeout",
|
||||
"pong_write_timeout",
|
||||
]),
|
||||
Self::Connection,
|
||||
),
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
@@ -60,6 +60,19 @@ pub trait ObservabilityService: Send + Sync {
|
||||
-> Result<Trend, AdminError>;
|
||||
async fn usage_records(&self, query: UsageQuery) -> Result<UsagePage, AdminError>;
|
||||
async fn usage_record_detail(&self, request_id: &str) -> Result<UsageDetail, AdminError>;
|
||||
async fn client_key_statistics(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
query: crate::model::observability::KeyUsageMetric,
|
||||
) -> Result<crate::model::observability::KeyStatisticsOverview, AdminError>;
|
||||
|
||||
async fn client_key_models(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
) -> Result<Vec<crate::model::observability::KeyModelRow>, AdminError>;
|
||||
|
||||
async fn usage_summary(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
@@ -287,6 +300,29 @@ impl ObservabilityService for DefaultObservabilityService {
|
||||
Ok(detail)
|
||||
}
|
||||
|
||||
async fn client_key_statistics(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
query: crate::model::observability::KeyUsageMetric,
|
||||
) -> Result<crate::model::observability::KeyStatisticsOverview, AdminError> {
|
||||
self.store
|
||||
.client_key_statistics(range, id, query)
|
||||
.await
|
||||
.map_err(|error| map_store_error(error, "client key statistics"))
|
||||
}
|
||||
|
||||
async fn client_key_models(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
) -> Result<Vec<crate::model::observability::KeyModelRow>, AdminError> {
|
||||
self.store
|
||||
.client_key_models(range, id)
|
||||
.await
|
||||
.map_err(|error| map_store_error(error, "client key statistics"))
|
||||
}
|
||||
|
||||
async fn usage_summary(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
|
||||
@@ -226,6 +226,23 @@ impl ObservabilityStore for CachedObservabilityStore {
|
||||
) -> AdminStoreResult<Vec<RequestMetricPoint>> {
|
||||
self.inner.dashboard_trend(range, granularity).await
|
||||
}
|
||||
async fn client_key_statistics(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
query: crate::model::observability::KeyUsageMetric,
|
||||
) -> AdminStoreResult<crate::model::observability::KeyStatisticsOverview> {
|
||||
self.inner.client_key_statistics(range, id, query).await
|
||||
}
|
||||
|
||||
async fn client_key_models(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
) -> AdminStoreResult<Vec<crate::model::observability::KeyModelRow>> {
|
||||
self.inner.client_key_models(range, id).await
|
||||
}
|
||||
|
||||
async fn usage_trend(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
|
||||
@@ -126,6 +126,46 @@ impl PluginAccountAccess for DefaultPluginAccountAccess {
|
||||
self.quota(account_id, false).await
|
||||
}
|
||||
|
||||
async fn quota_history(
|
||||
&self,
|
||||
account_id: &ProviderAccountId,
|
||||
query: crate::model::quota_history::QuotaHistoryQuery,
|
||||
) -> Result<crate::model::quota_history::QuotaHistoryPage, AdminError> {
|
||||
use crate::model::{observability::TimeRange, quota_history::QuotaHistoryPage};
|
||||
|
||||
let account = self
|
||||
.accounts
|
||||
.load_account_record(account_id)
|
||||
.await
|
||||
.map_err(|error| map_store_error(error, "plugin quota history"))?
|
||||
.ok_or_else(|| AdminError::not_found("账号不存在"))?;
|
||||
let Some(parse) = self
|
||||
.provider(&account.provider_kind)?
|
||||
.quota_history_parser()
|
||||
else {
|
||||
return Ok(QuotaHistoryPage::default());
|
||||
};
|
||||
let start = query.scan_start().max(account.created_at);
|
||||
if start >= query.range.end {
|
||||
return Ok(QuotaHistoryPage::default());
|
||||
}
|
||||
let page = self
|
||||
.accounts
|
||||
.quota_documents(
|
||||
account_id,
|
||||
&account.provider_kind,
|
||||
account.created_at,
|
||||
TimeRange {
|
||||
start,
|
||||
end: query.range.end,
|
||||
},
|
||||
query.cursor.as_ref(),
|
||||
)
|
||||
.await
|
||||
.map_err(|error| map_store_error(error, "plugin quota history"))?;
|
||||
Ok(QuotaHistoryPage::from_documents(page, &query, parse))
|
||||
}
|
||||
|
||||
async fn refresh_quota(
|
||||
&self,
|
||||
account_id: &ProviderAccountId,
|
||||
|
||||
@@ -53,6 +53,8 @@ impl PluginResourceAccess for DefaultPluginResourceAccess {
|
||||
resource_key,
|
||||
NewAccountGroup {
|
||||
allow_image_generation: command.allow_image_generation,
|
||||
allow_subagents: command.allow_subagents,
|
||||
model_access: command.model_access,
|
||||
id,
|
||||
name: command.name,
|
||||
description: command.description,
|
||||
|
||||
@@ -116,10 +116,10 @@ fn validate_settings(command: &ReplaceRuntimeSettings) -> Result<(), AdminError>
|
||||
.map(|(requested, upstream)| (requested.as_str(), upstream.as_str())),
|
||||
)
|
||||
.map_err(|_| AdminError::invalid("运行时设置不满足约束"))?;
|
||||
command
|
||||
.values
|
||||
.validate()
|
||||
.map_err(|_| AdminError::invalid("运行时设置不满足约束"))
|
||||
command.values.validate().map_err(|field| match field {
|
||||
"openai_request_country" => AdminError::invalid("请求国家必须是两位大写 ASCII 字母"),
|
||||
_ => AdminError::invalid("运行时设置不满足约束"),
|
||||
})
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
@@ -267,7 +267,7 @@ impl SettingsService for DefaultSettingsService {
|
||||
)
|
||||
})
|
||||
{
|
||||
return Err(AdminError::invalid("xAI 不支持此价格档位"));
|
||||
return Err(AdminError::invalid("当前平台不支持此价格档位"));
|
||||
}
|
||||
if !pricing.bands.contains_key("standard")
|
||||
&& command
|
||||
|
||||
@@ -335,3 +335,42 @@ fn pending_mutation_v1_should_keep_existing_storage_fields_and_reject_invalid_do
|
||||
document["schema_version"] = json!(2);
|
||||
assert!(PendingAuthorizationMutation::from_storage_v1(document).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn manual_key_alignment_only_accepts_the_current_account_wide_day_or_week() {
|
||||
use chrono::{Duration, Utc};
|
||||
use gateway_admin::model::{
|
||||
client_keys::{ClientKeyBudgetReference, ClientKeyBudgetWindowPeriod},
|
||||
upstream_info::UpstreamQuotaWindow,
|
||||
};
|
||||
let now = Utc::now();
|
||||
for (seconds, period) in [
|
||||
(86_400, ClientKeyBudgetWindowPeriod::Daily),
|
||||
(604_800, ClientKeyBudgetWindowPeriod::Weekly),
|
||||
] {
|
||||
let mut window = usage_window("account", "quota", seconds);
|
||||
let end = now + Duration::hours(2);
|
||||
window.reset_at = Some(end);
|
||||
let reference = ClientKeyBudgetReference::from_current_window(window.clone(), now).unwrap();
|
||||
let selected = reference.period;
|
||||
let bounds = reference.bounds;
|
||||
assert_eq!(selected, period);
|
||||
assert_eq!(bounds.starts_at, end - Duration::seconds(seconds as i64));
|
||||
assert_eq!(bounds.ends_at, end);
|
||||
assert!(bounds.resume_calendar);
|
||||
assert!(ClientKeyBudgetReference::from_current_window(window.clone(), end).is_none());
|
||||
window.upstream = Some(UpstreamQuotaWindow {
|
||||
account_id: None,
|
||||
observed_at: Some(now),
|
||||
stale: true,
|
||||
amount: None,
|
||||
});
|
||||
assert!(ClientKeyBudgetReference::from_current_window(window.clone(), now).is_none());
|
||||
window.upstream = None;
|
||||
window.local_usage_attribution = QuotaLocalUsageAttribution::Unavailable;
|
||||
assert!(ClientKeyBudgetReference::from_current_window(window.clone(), now).is_none());
|
||||
}
|
||||
let mut short = usage_window("primary", "short", 18_000);
|
||||
short.reset_at = Some(now + Duration::hours(1));
|
||||
assert!(ClientKeyBudgetReference::from_current_window(short, now).is_none());
|
||||
}
|
||||
|
||||
@@ -344,6 +344,8 @@ fn group_record() -> AccountGroupRecord {
|
||||
let now = Utc::now();
|
||||
AccountGroupRecord {
|
||||
allow_image_generation: true,
|
||||
allow_subagents: true,
|
||||
model_access: Default::default(),
|
||||
fast_mode: FastMode::Default,
|
||||
id: group_id(),
|
||||
name: "Primary".to_owned(),
|
||||
@@ -420,6 +422,8 @@ async fn invalid_group_fields_are_rejected_before_store_mutations() {
|
||||
&context,
|
||||
CreateAccountGroup {
|
||||
allow_image_generation: true,
|
||||
allow_subagents: true,
|
||||
model_access: Default::default(),
|
||||
fast_mode: FastMode::Default,
|
||||
name: " invalid".to_owned(),
|
||||
description: None,
|
||||
@@ -434,6 +438,8 @@ async fn invalid_group_fields_are_rejected_before_store_mutations() {
|
||||
&context,
|
||||
UpdateAccountGroup {
|
||||
allow_image_generation: None,
|
||||
allow_subagents: None,
|
||||
model_access: None,
|
||||
fast_mode: None,
|
||||
id: record.id,
|
||||
name: "valid".to_owned(),
|
||||
|
||||
@@ -266,6 +266,7 @@ impl FakeProviderAdmin {
|
||||
plan_type: account.plan_type.clone(),
|
||||
preserve_profile: false,
|
||||
preserve_credential_state: false,
|
||||
preserve_access_token_expiry: false,
|
||||
provider_material: document(),
|
||||
has_refresh_token: account.has_refresh_token,
|
||||
access_token_expires_at: account
|
||||
@@ -845,6 +846,17 @@ impl AccountStore for FakeAccountStore {
|
||||
.clone())
|
||||
}
|
||||
|
||||
async fn quota_documents(
|
||||
&self,
|
||||
_: &gateway_core::account::ProviderAccountId,
|
||||
_: &gateway_core::routing::ProviderKind,
|
||||
_: chrono::DateTime<chrono::Utc>,
|
||||
_: gateway_admin::model::observability::TimeRange,
|
||||
_: Option<&gateway_admin::model::quota_history::QuotaHistoryCursor>,
|
||||
) -> AdminStoreResult<gateway_admin::model::quota_history::QuotaDocumentPage> {
|
||||
unreachable!("测试不读取历史额度文档")
|
||||
}
|
||||
|
||||
async fn load_quota_forecast_history(
|
||||
&self,
|
||||
window: &AccountUsageWindowQuery,
|
||||
@@ -1162,6 +1174,8 @@ impl SettingsStore for StaticSettingsStore {
|
||||
rotation_strategy: RotationStrategy::Smart,
|
||||
updated_at: Utc::now(),
|
||||
values: gateway_admin::model::settings::RuntimeSettingsValues {
|
||||
openai_oauth_guardian_review_model: false,
|
||||
openai_request_country: Default::default(),
|
||||
codex_privacy_policy: Default::default(),
|
||||
request_location_enabled: false,
|
||||
request_location: Default::default(),
|
||||
@@ -3502,6 +3516,7 @@ fn plugin_rotation_facts(account: &AccountRecord) -> PreparedCredentialRotationF
|
||||
plan_type: account.plan_type.clone(),
|
||||
preserve_profile: true,
|
||||
preserve_credential_state: false,
|
||||
preserve_access_token_expiry: false,
|
||||
provider_material: document(),
|
||||
has_refresh_token: account.has_refresh_token,
|
||||
access_token_expires_at: account.access_token_expires_at,
|
||||
|
||||
@@ -35,6 +35,26 @@ struct TestClientKeyStore {
|
||||
|
||||
#[async_trait]
|
||||
impl ClientKeyStore for TestClientKeyStore {
|
||||
async fn budget_reference_accounts(
|
||||
&self,
|
||||
_: &gateway_core::policy::ClientApiKeyId,
|
||||
_: &str,
|
||||
_: &str,
|
||||
) -> gateway_admin::ports::store::AdminStoreResult<
|
||||
gateway_admin::model::client_keys::BudgetReferenceAccountPage,
|
||||
> {
|
||||
Err(unused())
|
||||
}
|
||||
|
||||
async fn batch_update_client_keys(
|
||||
&self,
|
||||
_: gateway_admin::model::client_keys::BatchUpdateClientKeys,
|
||||
_: gateway_admin::ports::store::BatchClientKeyProfileMerger,
|
||||
_: &MutationContext,
|
||||
) -> AdminStoreResult<Revision> {
|
||||
Err(unused())
|
||||
}
|
||||
|
||||
async fn client_key_budget_window(
|
||||
&self,
|
||||
_: &gateway_core::policy::ClientApiKeyId,
|
||||
@@ -80,6 +100,15 @@ impl ClientKeyStore for TestClientKeyStore {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn client_key_budget(
|
||||
&self,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
) -> AdminStoreResult<Option<gateway_core::engine::budget::ClientBudgetStatus>> {
|
||||
self.get_client_key(id)
|
||||
.await
|
||||
.map(|key| key.map(|row| row.budget))
|
||||
}
|
||||
|
||||
async fn get_client_key(
|
||||
&self,
|
||||
_: &ClientApiKeyId,
|
||||
@@ -172,6 +201,34 @@ impl ClientKeyStore for TestClientKeyStore {
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn batch_keys_require_a_change_and_unique_targets() {
|
||||
use gateway_admin::model::client_keys::BatchUpdateClientKeys;
|
||||
let services = super::AdminHarness::new()
|
||||
.client_keys(Arc::new(TestClientKeyStore::default()))
|
||||
.build()
|
||||
.await;
|
||||
let id = ClientApiKeyId::new("key_a").unwrap();
|
||||
for patch in [
|
||||
BatchUpdateClientKeys {
|
||||
ids: vec![id.clone()],
|
||||
..Default::default()
|
||||
},
|
||||
BatchUpdateClientKeys {
|
||||
ids: vec![id.clone(), id],
|
||||
enabled: Some(false),
|
||||
..Default::default()
|
||||
},
|
||||
] {
|
||||
let error = services
|
||||
.client_keys()
|
||||
.batch_update(&mutation_context(), patch)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert_eq!(error.kind(), AdminErrorKind::Invalid);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn reset_budget_forwards_scope_and_returns_only_key_identity() {
|
||||
use gateway_admin::model::client_keys::{ClientKeyBudgetPeriod, ResetClientKeyBudget};
|
||||
|
||||
@@ -107,8 +107,7 @@ async fn openai_import_should_expose_only_explicit_public_errors_without_committ
|
||||
] {
|
||||
let events = events();
|
||||
let provider = FakeProviderAdmin::new("openai", events.clone());
|
||||
provider
|
||||
.fail_next_with_public_message(provider_kind, "Codex PAT 验证失败,请检查令牌后重试");
|
||||
provider.fail_next_with_public_message(provider_kind, "PAT 验证失败,请检查令牌");
|
||||
let store = FakeAccountStore::new("openai", events.clone());
|
||||
let services = service(provider, store.clone()).await;
|
||||
let error = services
|
||||
@@ -124,7 +123,7 @@ async fn openai_import_should_expose_only_explicit_public_errors_without_committ
|
||||
.await
|
||||
.expect_err("validation must fail before committing accounts");
|
||||
assert_eq!(error.kind(), admin_kind);
|
||||
assert_eq!(error.to_string(), "Codex PAT 验证失败,请检查令牌后重试");
|
||||
assert_eq!(error.to_string(), "PAT 验证失败,请检查令牌");
|
||||
assert!(!format!("{error:?}").contains("secret token"));
|
||||
assert_eq!(recorded(&events), ["provider.prepare_import"]);
|
||||
assert!(store.audit_requests().is_empty());
|
||||
|
||||
@@ -33,6 +33,8 @@ fn runtime_settings(enabled: bool, probe_enabled: bool) -> RuntimeSettings {
|
||||
rotation_strategy: gateway_admin::model::settings::RotationStrategy::Smart,
|
||||
updated_at: Utc::now(),
|
||||
values: gateway_admin::model::settings::RuntimeSettingsValues {
|
||||
openai_oauth_guardian_review_model: false,
|
||||
openai_request_country: Default::default(),
|
||||
codex_privacy_policy: Default::default(),
|
||||
request_location_enabled: false,
|
||||
request_location: Default::default(),
|
||||
|
||||
@@ -546,6 +546,17 @@ impl AccountStore for UnavailableStore {
|
||||
Err(unavailable("account quota window usage"))
|
||||
}
|
||||
|
||||
async fn quota_documents(
|
||||
&self,
|
||||
_: &gateway_core::account::ProviderAccountId,
|
||||
_: &gateway_core::routing::ProviderKind,
|
||||
_: chrono::DateTime<chrono::Utc>,
|
||||
_: gateway_admin::model::observability::TimeRange,
|
||||
_: Option<&gateway_admin::model::quota_history::QuotaHistoryCursor>,
|
||||
) -> AdminStoreResult<gateway_admin::model::quota_history::QuotaDocumentPage> {
|
||||
unreachable!("测试不读取历史额度文档")
|
||||
}
|
||||
|
||||
async fn load_quota_forecast_history(
|
||||
&self,
|
||||
_: &AccountUsageWindowQuery,
|
||||
@@ -708,6 +719,26 @@ impl AccountRuntimeStore for UnavailableStore {
|
||||
|
||||
#[async_trait]
|
||||
impl ClientKeyStore for UnavailableStore {
|
||||
async fn budget_reference_accounts(
|
||||
&self,
|
||||
_: &gateway_core::policy::ClientApiKeyId,
|
||||
_: &str,
|
||||
_: &str,
|
||||
) -> gateway_admin::ports::store::AdminStoreResult<
|
||||
gateway_admin::model::client_keys::BudgetReferenceAccountPage,
|
||||
> {
|
||||
Err(unavailable("reference accounts"))
|
||||
}
|
||||
|
||||
async fn batch_update_client_keys(
|
||||
&self,
|
||||
_: gateway_admin::model::client_keys::BatchUpdateClientKeys,
|
||||
_: gateway_admin::ports::store::BatchClientKeyProfileMerger,
|
||||
_: &MutationContext,
|
||||
) -> AdminStoreResult<Revision> {
|
||||
Err(unavailable("client key batch update"))
|
||||
}
|
||||
|
||||
async fn client_key_budget_window(
|
||||
&self,
|
||||
_: &gateway_core::policy::ClientApiKeyId,
|
||||
@@ -752,6 +783,15 @@ impl ClientKeyStore for UnavailableStore {
|
||||
Err(unavailable("client key budget reset"))
|
||||
}
|
||||
|
||||
async fn client_key_budget(
|
||||
&self,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
) -> AdminStoreResult<Option<gateway_core::engine::budget::ClientBudgetStatus>> {
|
||||
self.get_client_key(id)
|
||||
.await
|
||||
.map(|key| key.map(|row| row.budget))
|
||||
}
|
||||
|
||||
async fn get_client_key(
|
||||
&self,
|
||||
_: &ClientApiKeyId,
|
||||
@@ -833,6 +873,23 @@ impl ObservabilityStore for UnavailableStore {
|
||||
Err(unavailable("dashboard trend"))
|
||||
}
|
||||
|
||||
async fn client_key_statistics(
|
||||
&self,
|
||||
_: TimeRange,
|
||||
_: &gateway_core::policy::ClientApiKeyId,
|
||||
_: gateway_admin::model::observability::KeyUsageMetric,
|
||||
) -> AdminStoreResult<gateway_admin::model::observability::KeyStatisticsOverview> {
|
||||
Ok(Default::default())
|
||||
}
|
||||
|
||||
async fn client_key_models(
|
||||
&self,
|
||||
_: TimeRange,
|
||||
_: &gateway_core::policy::ClientApiKeyId,
|
||||
) -> AdminStoreResult<Vec<gateway_admin::model::observability::KeyModelRow>> {
|
||||
Ok(Default::default())
|
||||
}
|
||||
|
||||
async fn usage_trend(
|
||||
&self,
|
||||
_: TimeRange,
|
||||
|
||||
@@ -1266,6 +1266,23 @@ impl ObservabilityStore for FixtureObservabilityStore {
|
||||
Ok(self.trend.lock().expect("trend").clone())
|
||||
}
|
||||
|
||||
async fn client_key_statistics(
|
||||
&self,
|
||||
_: TimeRange,
|
||||
_: &gateway_core::policy::ClientApiKeyId,
|
||||
_: gateway_admin::model::observability::KeyUsageMetric,
|
||||
) -> AdminStoreResult<gateway_admin::model::observability::KeyStatisticsOverview> {
|
||||
Ok(Default::default())
|
||||
}
|
||||
|
||||
async fn client_key_models(
|
||||
&self,
|
||||
_: TimeRange,
|
||||
_: &gateway_core::policy::ClientApiKeyId,
|
||||
) -> AdminStoreResult<Vec<gateway_admin::model::observability::KeyModelRow>> {
|
||||
Ok(Default::default())
|
||||
}
|
||||
|
||||
async fn usage_trend(
|
||||
&self,
|
||||
_: TimeRange,
|
||||
@@ -1414,6 +1431,8 @@ impl SettingsStore for FixtureSettingsStore {
|
||||
rotation_strategy: RotationStrategy::Smart,
|
||||
updated_at: Utc::now(),
|
||||
values: gateway_admin::model::settings::RuntimeSettingsValues {
|
||||
openai_oauth_guardian_review_model: false,
|
||||
openai_request_country: Default::default(),
|
||||
codex_privacy_policy: Default::default(),
|
||||
request_location_enabled: false,
|
||||
request_location: Default::default(),
|
||||
|
||||
@@ -254,7 +254,7 @@ fn sdk_settings_contract_matches_host_declarations() {
|
||||
),
|
||||
(
|
||||
"../gateway-core/src/account/location.rs",
|
||||
&["RequestLocation"][..],
|
||||
&["RequestLocation", "OpenAiRequestCountry"][..],
|
||||
),
|
||||
(
|
||||
"../gateway-core/src/account/scheduling.rs",
|
||||
|
||||
@@ -60,7 +60,7 @@ async fn explicit_service_calls_freeze_empty_plan_and_cancel_before_entering_ter
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
// 内部 pricing 不再单独进入插件链;显式调用只解析一次发布集合
|
||||
// 每次显式调价只解析一次插件发布集合
|
||||
assert_eq!(resolutions.load(Ordering::SeqCst), expected);
|
||||
}
|
||||
store.updated.lock().unwrap().take();
|
||||
@@ -325,6 +325,8 @@ async fn settings_should_reject_zero_refresh_margin_before_store_call() {
|
||||
model_mappings: Default::default(),
|
||||
rotation_strategy: RotationStrategy::Smart,
|
||||
values: gateway_admin::model::settings::RuntimeSettingsValues {
|
||||
openai_oauth_guardian_review_model: true,
|
||||
openai_request_country: Default::default(),
|
||||
codex_privacy_policy: Default::default(),
|
||||
request_location_enabled: false,
|
||||
request_location: Default::default(),
|
||||
|
||||
@@ -21,12 +21,15 @@ use gateway_admin::model::{
|
||||
UpdateAccountGroup,
|
||||
},
|
||||
};
|
||||
use gateway_core::{account::FastMode, routing::AccountGroupId};
|
||||
use gateway_core::{
|
||||
account::{AccountModelAccess, FastMode},
|
||||
routing::AccountGroupId,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::{
|
||||
AdminAuth, AdminEnvelope, AdminError, AdminJson, AdminQuery, AdminResponse, PageMeta,
|
||||
WireValidationError, wire::map_admin_service_error,
|
||||
WireValidationError, accounts::AccountModelView, wire::map_admin_service_error,
|
||||
};
|
||||
|
||||
/// 账号、代理与 Client Key 共用的分组引用
|
||||
@@ -51,7 +54,6 @@ impl From<gateway_admin::model::account_groups::AccountGroupRef> for AccountGrou
|
||||
}
|
||||
|
||||
const DEFAULT_PAGE_SIZE: u32 = 50;
|
||||
const MAX_PAGE_SIZE: u32 = 200;
|
||||
|
||||
#[derive(Debug, Clone, Default, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
@@ -66,7 +68,7 @@ impl ListAccountGroupsQuery {
|
||||
fn into_command(self) -> Result<AccountGroupListQuery, WireValidationError> {
|
||||
let page = self.page.unwrap_or(1);
|
||||
let page_size = self.page_size.unwrap_or(DEFAULT_PAGE_SIZE);
|
||||
if page == 0 || page_size == 0 || page_size > MAX_PAGE_SIZE {
|
||||
if page == 0 {
|
||||
return Err(WireValidationError::new("page"));
|
||||
}
|
||||
let search = self
|
||||
@@ -91,15 +93,19 @@ impl ListAccountGroupsQuery {
|
||||
}
|
||||
}
|
||||
|
||||
const fn default_allow_image_generation() -> bool {
|
||||
const fn default_allowed() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
struct CreateAccountGroupRequest {
|
||||
#[serde(default = "default_allow_image_generation")]
|
||||
#[serde(default = "default_allowed")]
|
||||
allow_image_generation: bool,
|
||||
#[serde(default = "default_allowed")]
|
||||
allow_subagents: bool,
|
||||
#[serde(default)]
|
||||
model_access: AccountModelAccess,
|
||||
#[serde(default)]
|
||||
fast_mode: FastMode,
|
||||
name: String,
|
||||
@@ -111,6 +117,8 @@ struct CreateAccountGroupRequest {
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
struct UpdateAccountGroupRequest {
|
||||
allow_image_generation: Option<bool>,
|
||||
allow_subagents: Option<bool>,
|
||||
model_access: Option<AccountModelAccess>,
|
||||
fast_mode: Option<FastMode>,
|
||||
id: String,
|
||||
name: String,
|
||||
@@ -128,6 +136,8 @@ struct AccountGroupIdRequest {
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct AccountGroupView {
|
||||
allow_image_generation: bool,
|
||||
allow_subagents: bool,
|
||||
model_access: AccountModelAccess,
|
||||
fast_mode: FastMode,
|
||||
id: String,
|
||||
name: String,
|
||||
@@ -176,6 +186,8 @@ impl From<(AccountGroupRecord, crate::time::TimePresenter)> for AccountGroupView
|
||||
description: record.description,
|
||||
fast_mode: record.fast_mode,
|
||||
allow_image_generation: record.allow_image_generation,
|
||||
allow_subagents: record.allow_subagents,
|
||||
model_access: record.model_access,
|
||||
color: record.color.as_str().to_owned(),
|
||||
enabled: record.enabled,
|
||||
member_count: record.member_count,
|
||||
@@ -305,6 +317,7 @@ where
|
||||
{
|
||||
Router::new()
|
||||
.route("/api/admin/account-groups", get(list::<S>))
|
||||
.route("/api/admin/account-groups/models", get(models::<S>))
|
||||
.route("/api/admin/account-groups/options", get(list_options::<S>))
|
||||
.route("/api/admin/account-groups/create", post(create::<S>))
|
||||
.route("/api/admin/account-groups/update", post(update::<S>))
|
||||
@@ -333,6 +346,32 @@ where
|
||||
))
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct AccountGroupModelsData {
|
||||
models: Vec<AccountModelView>,
|
||||
}
|
||||
|
||||
async fn models<S>(
|
||||
_auth: AdminAuth,
|
||||
State(state): State<S>,
|
||||
AdminQuery(query): AdminQuery<AccountGroupIdRequest>,
|
||||
) -> Result<impl IntoResponse, AdminError>
|
||||
where
|
||||
S: SessionState + Send + Sync,
|
||||
{
|
||||
let models = state
|
||||
.admin_services()
|
||||
.account_groups()
|
||||
.models(&group_id(query.id)?)
|
||||
.map_err(map_service_error)?;
|
||||
Ok(AdminResponse::new(
|
||||
StatusCode::OK,
|
||||
AdminEnvelope::ok(AccountGroupModelsData {
|
||||
models: models.into_iter().map(AccountModelView::from).collect(),
|
||||
}),
|
||||
))
|
||||
}
|
||||
|
||||
async fn list<S>(
|
||||
_auth: AdminAuth,
|
||||
State(state): State<S>,
|
||||
@@ -372,6 +411,8 @@ where
|
||||
&auth.context().mutation_context(),
|
||||
CreateAccountGroup {
|
||||
allow_image_generation: request.allow_image_generation,
|
||||
allow_subagents: request.allow_subagents,
|
||||
model_access: request.model_access,
|
||||
name: request.name,
|
||||
description: request.description,
|
||||
fast_mode: request.fast_mode,
|
||||
@@ -401,6 +442,8 @@ where
|
||||
&auth.context().mutation_context(),
|
||||
UpdateAccountGroup {
|
||||
allow_image_generation: request.allow_image_generation,
|
||||
allow_subagents: request.allow_subagents,
|
||||
model_access: request.model_access,
|
||||
id: group_id(request.id)?,
|
||||
name: request.name,
|
||||
description: request.description,
|
||||
@@ -507,11 +550,16 @@ fn group_color(value: &str) -> Result<AccountGroupColor, AdminError> {
|
||||
|
||||
fn validate_group_fields(name: &str, description: Option<&str>) -> Result<(), AdminError> {
|
||||
gateway_admin::model::account_groups::validate_group_fields(name, description)
|
||||
.map_err(|_| AdminError::bad_request("账号组请求不合法"))
|
||||
.map_err(map_admin_service_error)
|
||||
}
|
||||
|
||||
fn map_wire_error(_: WireValidationError) -> AdminError {
|
||||
AdminError::bad_request("账号组查询参数不合法")
|
||||
fn map_wire_error(error: WireValidationError) -> AdminError {
|
||||
match error.field() {
|
||||
"page" => AdminError::bad_request("页码须大于 0"),
|
||||
"pageSize" => AdminError::bad_request(format!("每页数量须为 1 至 {}", PageSize::MAX)),
|
||||
"search" => AdminError::bad_request("搜索内容最多 256 字节,不能包含控制字符"),
|
||||
field => AdminError::bad_request(format!("{field} 字段不合法")),
|
||||
}
|
||||
}
|
||||
|
||||
fn map_service_error(error: gateway_admin::model::AdminError) -> AdminError {
|
||||
|
||||
@@ -118,7 +118,6 @@ pub struct StartAccountAuthorizationRequest {
|
||||
pub provider: String,
|
||||
pub name: String,
|
||||
pub account_id: Option<String>,
|
||||
pub outbound_proxy_url: Option<super::wire::AccountProxyUpdate>,
|
||||
}
|
||||
|
||||
impl StartAccountAuthorizationRequest {
|
||||
@@ -148,10 +147,7 @@ impl StartAccountAuthorizationRequest {
|
||||
context,
|
||||
name: self.name.trim().to_owned(),
|
||||
reauthorization,
|
||||
outbound_proxy: super::wire::proxy_selection(
|
||||
self.outbound_proxy_id,
|
||||
self.outbound_proxy_url,
|
||||
)?,
|
||||
outbound_proxy: super::wire::proxy_selection(self.outbound_proxy_id)?,
|
||||
},
|
||||
))
|
||||
}
|
||||
@@ -219,7 +215,6 @@ fn validate_authorization_flow(
|
||||
pub struct UpdateAccountRequest {
|
||||
pub connection: Option<AccountConnectionUpdateRequest>,
|
||||
pub outbound_proxy_id: Option<String>,
|
||||
pub outbound_proxy_url: Option<super::wire::AccountProxyUpdate>,
|
||||
pub account_id: String,
|
||||
pub name: Option<String>,
|
||||
pub notes: Option<String>,
|
||||
@@ -255,10 +250,7 @@ impl UpdateAccountRequest {
|
||||
.connection
|
||||
.map(AccountConnectionUpdateRequest::into_document);
|
||||
let settings = UpdateAccount {
|
||||
outbound_proxy: super::wire::proxy_selection(
|
||||
self.outbound_proxy_id,
|
||||
self.outbound_proxy_url,
|
||||
)?,
|
||||
outbound_proxy: super::wire::proxy_selection(self.outbound_proxy_id)?,
|
||||
account_id: self.account_id,
|
||||
name: self.name.map(|name| name.trim().to_owned()),
|
||||
notes: self.notes,
|
||||
|
||||
@@ -53,13 +53,7 @@ pub(super) fn account_models_data(
|
||||
models: result
|
||||
.models
|
||||
.into_iter()
|
||||
.map(|model| {
|
||||
let id = model.id.to_string();
|
||||
AccountModelView {
|
||||
label: id.clone(),
|
||||
id,
|
||||
}
|
||||
})
|
||||
.map(|model| AccountModelView::from(model.id))
|
||||
.collect(),
|
||||
observed_at: observed_at.map(|value| time.rfc3339(&value)),
|
||||
observed_at_display: observed_at
|
||||
|
||||
@@ -2,46 +2,22 @@
|
||||
|
||||
use super::*;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AccountProxyUpdate(pub Option<gateway_core::account::OutboundProxy>);
|
||||
|
||||
impl<'de> Deserialize<'de> for AccountProxyUpdate {
|
||||
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
let value = String::deserialize(deserializer)?;
|
||||
if value.is_empty() {
|
||||
return Ok(Self(None));
|
||||
}
|
||||
gateway_core::account::OutboundProxy::parse(&value)
|
||||
.map(|proxy| Self(Some(proxy)))
|
||||
.map_err(serde::de::Error::custom)
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn proxy_selection(
|
||||
id: Option<String>,
|
||||
url: Option<AccountProxyUpdate>,
|
||||
) -> Result<Option<gateway_admin::model::proxies::AccountProxySelection>, WireValidationError> {
|
||||
use gateway_admin::model::proxies::AccountProxySelection;
|
||||
match (id, url) {
|
||||
(Some(_), Some(_)) => Err(WireValidationError::new("outboundProxyId")),
|
||||
(Some(id), None) if id.is_empty() => Ok(Some(AccountProxySelection::Direct)),
|
||||
(Some(id), None) => {
|
||||
if id.len() > 128
|
||||
|| !id
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-'))
|
||||
{
|
||||
return Err(WireValidationError::new("outboundProxyId"));
|
||||
}
|
||||
Ok(Some(AccountProxySelection::Saved(id)))
|
||||
}
|
||||
(None, Some(value)) => Ok(Some(
|
||||
value
|
||||
.0
|
||||
.map_or(AccountProxySelection::Direct, AccountProxySelection::Url),
|
||||
)),
|
||||
(None, None) => Ok(None),
|
||||
let Some(id) = id else { return Ok(None) };
|
||||
if id.is_empty() {
|
||||
return Ok(Some(AccountProxySelection::Direct));
|
||||
}
|
||||
if id.len() > 128
|
||||
|| !id
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-'))
|
||||
{
|
||||
return Err(WireValidationError::new("outboundProxyId"));
|
||||
}
|
||||
Ok(Some(AccountProxySelection::Saved(id)))
|
||||
}
|
||||
|
||||
/// 账号列表查询参数
|
||||
@@ -64,7 +40,6 @@ pub struct BatchUpdateAccountsRequest {
|
||||
pub quota_settings: Option<gateway_core::account::AccountQuotaSettings>,
|
||||
pub auto_use_quota_credits: Option<bool>,
|
||||
pub outbound_proxy_id: Option<String>,
|
||||
pub outbound_proxy_url: Option<AccountProxyUpdate>,
|
||||
pub account_ids: Vec<String>,
|
||||
pub enabled: Option<bool>,
|
||||
#[serde(default, deserialize_with = "deserialize_optional_nullable_limit")]
|
||||
@@ -127,7 +102,6 @@ impl BatchUpdateAccountsRequest {
|
||||
&& self.group_ids.is_none()
|
||||
&& self.model_access.is_none()
|
||||
&& self.outbound_proxy_id.is_none()
|
||||
&& self.outbound_proxy_url.is_none()
|
||||
{
|
||||
return Err(WireValidationError::new("accountSettings"));
|
||||
}
|
||||
@@ -139,7 +113,7 @@ impl BatchUpdateAccountsRequest {
|
||||
Ok(BatchUpdateAccounts {
|
||||
quota_settings: self.quota_settings,
|
||||
auto_use_quota_credits: self.auto_use_quota_credits,
|
||||
outbound_proxy: proxy_selection(self.outbound_proxy_id, self.outbound_proxy_url)?,
|
||||
outbound_proxy: proxy_selection(self.outbound_proxy_id)?,
|
||||
account_ids: self.account_ids,
|
||||
enabled: self.enabled,
|
||||
concurrency_limit: self
|
||||
@@ -743,6 +717,16 @@ pub struct AccountModelView {
|
||||
pub label: String,
|
||||
}
|
||||
|
||||
impl From<UpstreamModelId> for AccountModelView {
|
||||
fn from(model: UpstreamModelId) -> Self {
|
||||
let id = model.to_string();
|
||||
Self {
|
||||
label: id.clone(),
|
||||
id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AccountModelsData {
|
||||
|
||||
@@ -0,0 +1,291 @@
|
||||
//! 管理员查看单个密钥的模型统计与手动对齐本期额度的接口
|
||||
|
||||
use axum::{Router, extract::State, http::StatusCode, response::IntoResponse, routing::get};
|
||||
use chrono::{DateTime, Utc};
|
||||
use gateway_admin::model::observability::{KeyStatisticsOverview, KeyUsageMetric, TimeRange};
|
||||
use gateway_admin::{
|
||||
AdminServices,
|
||||
model::client_keys::{
|
||||
ClientKeyBudgetMutationOrigin, ClientKeyBudgetReference, ClientKeyBudgetWindowPeriod,
|
||||
SetClientKeyBudgetWindow,
|
||||
},
|
||||
};
|
||||
use gateway_core::{account::ProviderAccountId, policy::ClientApiKeyId};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::{
|
||||
AdminAuth, AdminEnvelope, AdminError, AdminJson, AdminQuery, AdminResponse,
|
||||
client_keys::ClientBudgetView, wire::map_admin_service_error,
|
||||
};
|
||||
use crate::{auth::SessionState, time::TimePresenter};
|
||||
|
||||
pub(super) fn router<S: SessionState + Clone + Send + Sync + 'static>() -> Router<S> {
|
||||
Router::new()
|
||||
.route(
|
||||
"/api/admin/client-keys/budget-reference-accounts",
|
||||
get(accounts::<S>),
|
||||
)
|
||||
.route("/api/admin/client-keys/statistics", get(statistics::<S>))
|
||||
.route("/api/admin/client-keys/statistics/models", get(models::<S>))
|
||||
.route(
|
||||
"/api/admin/client-keys/budget-alignment",
|
||||
get(preview::<S>).post(align::<S>),
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
struct StatisticsQuery {
|
||||
id: String,
|
||||
period: Option<String>,
|
||||
#[serde(default)]
|
||||
metric: KeyUsageMetric,
|
||||
as_of: Option<i64>,
|
||||
}
|
||||
|
||||
fn key_id(value: String) -> Result<ClientApiKeyId, AdminError> {
|
||||
ClientApiKeyId::new(value).map_err(|_| AdminError::bad_request("API Key ID 无效"))
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Statistics {
|
||||
budget: ClientBudgetView,
|
||||
dates: Vec<chrono::NaiveDate>,
|
||||
#[serde(rename = "asOf")]
|
||||
as_of: i64,
|
||||
#[serde(flatten)]
|
||||
overview: KeyStatisticsOverview,
|
||||
}
|
||||
|
||||
async fn statistics<S: SessionState>(
|
||||
_auth: AdminAuth,
|
||||
State(state): State<S>,
|
||||
AdminQuery(query): AdminQuery<StatisticsQuery>,
|
||||
) -> Result<impl IntoResponse, AdminError> {
|
||||
let services = state.admin_services();
|
||||
let id = key_id(query.id.clone())?;
|
||||
let tz = services.timezone();
|
||||
let range = statistics_range(&query, tz)?;
|
||||
let budget = services
|
||||
.client_keys()
|
||||
.budget(&id)
|
||||
.await
|
||||
.map_err(map_admin_service_error)?;
|
||||
let overview = services
|
||||
.observability()
|
||||
.client_key_statistics(range, &id, query.metric)
|
||||
.await
|
||||
.map_err(map_admin_service_error)?;
|
||||
let end = tz.local(range.end).date_naive();
|
||||
let dates = tz
|
||||
.local(range.start)
|
||||
.date_naive()
|
||||
.iter_days()
|
||||
.take_while(|date| *date <= end)
|
||||
.collect();
|
||||
Ok(AdminResponse::new(
|
||||
StatusCode::OK,
|
||||
AdminEnvelope::ok(Statistics {
|
||||
budget: ClientBudgetView::from((budget, TimePresenter::new(tz))),
|
||||
dates,
|
||||
as_of: range.end.timestamp_millis(),
|
||||
overview,
|
||||
}),
|
||||
))
|
||||
}
|
||||
|
||||
fn statistics_range(
|
||||
query: &StatisticsQuery,
|
||||
tz: gateway_core::time::DeploymentTimeZone,
|
||||
) -> Result<TimeRange, AdminError> {
|
||||
let period = query.period.as_deref().unwrap_or("7d");
|
||||
if !matches!(period, "7d" | "30d") {
|
||||
return Err(AdminError::bad_request("统计周期须为 7 天或 30 天"));
|
||||
}
|
||||
crate::time::query_range(
|
||||
None,
|
||||
None,
|
||||
Some(period),
|
||||
Some(query.as_of.unwrap_or_else(|| Utc::now().timestamp_millis())),
|
||||
tz,
|
||||
"7d",
|
||||
)
|
||||
.map_err(|_| AdminError::bad_request("统计时间范围无效"))
|
||||
}
|
||||
|
||||
async fn models<S: SessionState>(
|
||||
_auth: AdminAuth,
|
||||
State(state): State<S>,
|
||||
AdminQuery(query): AdminQuery<StatisticsQuery>,
|
||||
) -> Result<impl IntoResponse, AdminError> {
|
||||
let services = state.admin_services();
|
||||
let range = statistics_range(&query, services.timezone())?;
|
||||
let id = key_id(query.id)?;
|
||||
services
|
||||
.client_keys()
|
||||
.budget(&id)
|
||||
.await
|
||||
.map_err(map_admin_service_error)?;
|
||||
let items = services
|
||||
.observability()
|
||||
.client_key_models(range, &id)
|
||||
.await
|
||||
.map_err(map_admin_service_error)?;
|
||||
Ok(AdminResponse::new(StatusCode::OK, AdminEnvelope::ok(items)))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct AccountSearch {
|
||||
id: String,
|
||||
#[serde(default)]
|
||||
search: String,
|
||||
#[serde(default)]
|
||||
cursor: String,
|
||||
}
|
||||
|
||||
async fn accounts<S: SessionState>(
|
||||
_auth: AdminAuth,
|
||||
State(state): State<S>,
|
||||
AdminQuery(query): AdminQuery<AccountSearch>,
|
||||
) -> Result<impl IntoResponse, AdminError> {
|
||||
let page = state
|
||||
.admin_services()
|
||||
.client_keys()
|
||||
.budget_reference_accounts(&key_id(query.id)?, &query.search, &query.cursor)
|
||||
.await
|
||||
.map_err(map_admin_service_error)?;
|
||||
Ok(AdminResponse::new(StatusCode::OK, AdminEnvelope::ok(page)))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
struct ReferenceQuery {
|
||||
id: String,
|
||||
account_id: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct ReferenceWindow {
|
||||
key: String,
|
||||
label: String,
|
||||
period: &'static str,
|
||||
starts_at: DateTime<Utc>,
|
||||
ends_at: DateTime<Utc>,
|
||||
starts_at_display: String,
|
||||
ends_at_display: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Preview {
|
||||
revision: u64,
|
||||
windows: Vec<ReferenceWindow>,
|
||||
}
|
||||
|
||||
async fn reference_windows(
|
||||
services: &AdminServices,
|
||||
id: &ClientApiKeyId,
|
||||
account: String,
|
||||
) -> Result<Vec<ClientKeyBudgetReference>, AdminError> {
|
||||
let account_id =
|
||||
ProviderAccountId::new(account).map_err(|_| AdminError::bad_request("参考账号 ID 无效"))?;
|
||||
let key = services
|
||||
.client_keys()
|
||||
.get(id)
|
||||
.await
|
||||
.map_err(map_admin_service_error)?;
|
||||
services
|
||||
.accounts()
|
||||
.key_budget_references(&account_id, &key.groups)
|
||||
.await
|
||||
.map_err(map_admin_service_error)
|
||||
}
|
||||
|
||||
async fn preview<S: SessionState>(
|
||||
_auth: AdminAuth,
|
||||
State(state): State<S>,
|
||||
AdminQuery(query): AdminQuery<ReferenceQuery>,
|
||||
) -> Result<impl IntoResponse, AdminError> {
|
||||
let services = state.admin_services();
|
||||
let id = key_id(query.id)?;
|
||||
let time = TimePresenter::new(services.timezone());
|
||||
let windows = reference_windows(services, &id, query.account_id)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(
|
||||
|ClientKeyBudgetReference {
|
||||
key,
|
||||
label,
|
||||
period,
|
||||
bounds,
|
||||
}| ReferenceWindow {
|
||||
key,
|
||||
label,
|
||||
period: period.as_str(),
|
||||
starts_at: bounds.starts_at,
|
||||
ends_at: bounds.ends_at,
|
||||
starts_at_display: time.datetime(&bounds.starts_at),
|
||||
ends_at_display: time.datetime(&bounds.ends_at),
|
||||
},
|
||||
)
|
||||
.collect();
|
||||
// 日周窗口共享配置版本,普通请求的消费累计不会让预览失效
|
||||
let revision = services
|
||||
.client_keys()
|
||||
.budget_window(&id, ClientKeyBudgetWindowPeriod::Weekly)
|
||||
.await
|
||||
.map_err(map_admin_service_error)?
|
||||
.revision;
|
||||
Ok(AdminResponse::new(
|
||||
StatusCode::OK,
|
||||
AdminEnvelope::ok(Preview { revision, windows }),
|
||||
))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
struct AlignRequest {
|
||||
id: String,
|
||||
account_id: String,
|
||||
window_key: String,
|
||||
starts_at: DateTime<Utc>,
|
||||
ends_at: DateTime<Utc>,
|
||||
revision: u64,
|
||||
}
|
||||
|
||||
async fn align<S: SessionState>(
|
||||
auth: AdminAuth,
|
||||
State(state): State<S>,
|
||||
AdminJson(input): AdminJson<AlignRequest>,
|
||||
) -> Result<impl IntoResponse, AdminError> {
|
||||
let services = state.admin_services();
|
||||
let id = key_id(input.id)?;
|
||||
let reference = reference_windows(services, &id, input.account_id)
|
||||
.await?
|
||||
.into_iter()
|
||||
.find(|reference| {
|
||||
reference.key == input.window_key
|
||||
&& reference.bounds.starts_at == input.starts_at
|
||||
&& reference.bounds.ends_at == input.ends_at
|
||||
})
|
||||
.ok_or_else(|| AdminError::conflict("参考窗口已变化,请重新预览"))?;
|
||||
let revision = services
|
||||
.client_keys()
|
||||
.set_budget_window(
|
||||
&auth.context().mutation_context(),
|
||||
SetClientKeyBudgetWindow {
|
||||
id,
|
||||
period: reference.period,
|
||||
expected_revision: input.revision,
|
||||
window: Some(reference.bounds),
|
||||
},
|
||||
ClientKeyBudgetMutationOrigin::Admin,
|
||||
)
|
||||
.await
|
||||
.map_err(map_admin_service_error)?;
|
||||
Ok(AdminResponse::new(
|
||||
StatusCode::OK,
|
||||
AdminEnvelope::ok(revision),
|
||||
))
|
||||
}
|
||||
@@ -7,11 +7,12 @@ use std::{collections::BTreeMap, fmt};
|
||||
use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};
|
||||
use chrono::{DateTime, Utc};
|
||||
use gateway_admin::model::client_keys::{
|
||||
ClientKeyBudgetPeriod, ClientKeyCursor, ClientKeyCursorValue as DomainCursorValue,
|
||||
ClientKeyListQuery, ClientKeyMutation, ClientKeyPage, ClientKeyPageSize, ClientKeyRecord,
|
||||
ClientKeySecret, ClientKeySort as DomainSort, ClientKeySortField as DomainSortField,
|
||||
CreateClientKey, CreatedClientKey, DeleteClientKey, ResetClientKeyBudget, SetClientKeyEnabled,
|
||||
SortDirection, UpdateClientKey,
|
||||
BatchUpdateClientKeys, ClientKeyBudgetPeriod, ClientKeyCursor,
|
||||
ClientKeyCursorValue as DomainCursorValue, ClientKeyListQuery, ClientKeyMutation,
|
||||
ClientKeyPage, ClientKeyPageSize, ClientKeyRecord, ClientKeySecret,
|
||||
ClientKeySort as DomainSort, ClientKeySortField as DomainSortField, CreateClientKey,
|
||||
CreatedClientKey, DeleteClientKey, ResetClientKeyBudget, SetClientKeyEnabled, SortDirection,
|
||||
UpdateClientKey,
|
||||
};
|
||||
use gateway_core::{
|
||||
engine::budget::ClientBudgetLimits,
|
||||
@@ -156,8 +157,6 @@ pub struct CreateClientKeyRequest {
|
||||
peer_sharing: gateway_core::policy::ClientPeerSharing,
|
||||
#[serde(default)]
|
||||
provider_request_profile_overrides: ProviderRequestProfileOverrides,
|
||||
openai_client_profile_override: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
xai_client_profile_override: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
custom_key: Option<String>,
|
||||
name: String,
|
||||
label: Option<String>,
|
||||
@@ -186,11 +185,8 @@ impl CreateClientKeyRequest {
|
||||
let group_ids = validate_group_ids(self.group_ids)?;
|
||||
validate_limit(self.max_concurrency, "maxConcurrency")?;
|
||||
validate_limit(self.requests_per_minute, "requestsPerMinute")?;
|
||||
let request_profile_overrides = normalize_request_profile_overrides(
|
||||
self.provider_request_profile_overrides,
|
||||
self.openai_client_profile_override,
|
||||
self.xai_client_profile_override,
|
||||
)?;
|
||||
let request_profile_overrides =
|
||||
normalize_request_profile_overrides(self.provider_request_profile_overrides)?;
|
||||
Ok(CreateClientKey {
|
||||
peer_sharing: self.peer_sharing,
|
||||
request_profile_overrides,
|
||||
@@ -223,10 +219,6 @@ pub struct UpdateClientKeyRequest {
|
||||
peer_sharing: Option<gateway_core::policy::ClientPeerSharing>,
|
||||
#[serde(default)]
|
||||
provider_request_profile_overrides: ProviderRequestProfileOverrideUpdates,
|
||||
#[serde(default, deserialize_with = "deserialize_profile_override")]
|
||||
openai_client_profile_override: Option<Option<serde_json::Map<String, serde_json::Value>>>,
|
||||
#[serde(default, deserialize_with = "deserialize_profile_override")]
|
||||
xai_client_profile_override: Option<Option<serde_json::Map<String, serde_json::Value>>>,
|
||||
id: String,
|
||||
name: String,
|
||||
label: Option<String>,
|
||||
@@ -246,11 +238,8 @@ impl UpdateClientKeyRequest {
|
||||
let group_ids = validate_group_ids(self.group_ids)?;
|
||||
validate_limit(self.max_concurrency, "maxConcurrency")?;
|
||||
validate_limit(self.requests_per_minute, "requestsPerMinute")?;
|
||||
let request_profile_override_updates = normalize_request_profile_override_updates(
|
||||
self.provider_request_profile_overrides,
|
||||
self.openai_client_profile_override,
|
||||
self.xai_client_profile_override,
|
||||
)?;
|
||||
let request_profile_override_updates =
|
||||
normalize_request_profile_override_updates(self.provider_request_profile_overrides)?;
|
||||
Ok(UpdateClientKey {
|
||||
peer_sharing: self.peer_sharing,
|
||||
request_profile_override_updates,
|
||||
@@ -335,16 +324,54 @@ impl ClientKeyMutationRequest {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(super) struct ClientBudgetView {
|
||||
daily_limit_usd: String,
|
||||
weekly_limit_usd: String,
|
||||
daily_used_usd: String,
|
||||
weekly_used_usd: String,
|
||||
daily_resets_at: Option<DateTime<Utc>>,
|
||||
daily_resets_at_display: Option<String>,
|
||||
weekly_resets_at: Option<DateTime<Utc>>,
|
||||
weekly_resets_at_display: Option<String>,
|
||||
}
|
||||
|
||||
impl
|
||||
From<(
|
||||
gateway_core::engine::budget::ClientBudgetStatus,
|
||||
crate::time::TimePresenter,
|
||||
)> for ClientBudgetView
|
||||
{
|
||||
fn from(
|
||||
(budget, time): (
|
||||
gateway_core::engine::budget::ClientBudgetStatus,
|
||||
crate::time::TimePresenter,
|
||||
),
|
||||
) -> Self {
|
||||
Self {
|
||||
daily_limit_usd: budget.limits.daily_usd.canonical(),
|
||||
weekly_limit_usd: budget.limits.weekly_usd.canonical(),
|
||||
daily_used_usd: budget.daily_used_usd.canonical(),
|
||||
weekly_used_usd: budget.weekly_used_usd.canonical(),
|
||||
daily_resets_at_display: budget
|
||||
.daily_resets_at()
|
||||
.map(|value| time.datetime(&value.into())),
|
||||
daily_resets_at: budget.daily_resets_at().map(DateTime::from),
|
||||
weekly_resets_at_display: budget
|
||||
.weekly_resets_at()
|
||||
.map(|value| time.datetime(&value.into())),
|
||||
weekly_resets_at: budget.weekly_resets_at().map(DateTime::from),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 不含完整 Key 的管理端安全视图
|
||||
#[derive(Debug, Clone, PartialEq, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ClientKeyView {
|
||||
peer_sharing: gateway_core::policy::ClientPeerSharing,
|
||||
provider_request_profile_overrides: ProviderRequestProfileOverrides,
|
||||
/// 固定兼容字段;值始终从 provider_request_profile_overrides 派生
|
||||
openai_client_profile_override: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
/// 固定兼容字段;值始终从 provider_request_profile_overrides 派生
|
||||
xai_client_profile_override: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
id: String,
|
||||
name: String,
|
||||
label: Option<String>,
|
||||
@@ -355,14 +382,8 @@ pub struct ClientKeyView {
|
||||
enabled: bool,
|
||||
max_concurrency: u64,
|
||||
requests_per_minute: u64,
|
||||
daily_limit_usd: String,
|
||||
weekly_limit_usd: String,
|
||||
daily_used_usd: String,
|
||||
weekly_used_usd: String,
|
||||
daily_resets_at: Option<DateTime<Utc>>,
|
||||
daily_resets_at_display: Option<String>,
|
||||
weekly_resets_at: Option<DateTime<Utc>>,
|
||||
weekly_resets_at_display: Option<String>,
|
||||
#[serde(flatten)]
|
||||
budget: ClientBudgetView,
|
||||
created_at: DateTime<Utc>,
|
||||
created_at_display: String,
|
||||
updated_at: DateTime<Utc>,
|
||||
@@ -385,10 +406,6 @@ impl From<(ClientKeyRecord, crate::time::TimePresenter)> for ClientKeyView {
|
||||
.map(|(provider, profile)| (provider.as_str().to_owned(), profile.into_inner()))
|
||||
.collect::<ProviderRequestProfileOverrides>();
|
||||
Self {
|
||||
openai_client_profile_override: provider_request_profile_overrides
|
||||
.get("openai")
|
||||
.cloned(),
|
||||
xai_client_profile_override: provider_request_profile_overrides.get("xai").cloned(),
|
||||
provider_request_profile_overrides,
|
||||
peer_sharing: record.peer_sharing,
|
||||
id: record.id.to_string(),
|
||||
@@ -409,20 +426,7 @@ impl From<(ClientKeyRecord, crate::time::TimePresenter)> for ClientKeyView {
|
||||
enabled: record.enabled,
|
||||
max_concurrency: record.limits.max_concurrency,
|
||||
requests_per_minute: record.limits.requests_per_minute,
|
||||
daily_limit_usd: record.budget.limits.daily_usd.canonical(),
|
||||
weekly_limit_usd: record.budget.limits.weekly_usd.canonical(),
|
||||
daily_used_usd: record.budget.daily_used_usd.canonical(),
|
||||
weekly_used_usd: record.budget.weekly_used_usd.canonical(),
|
||||
daily_resets_at_display: record
|
||||
.budget
|
||||
.daily_resets_at()
|
||||
.map(|value| time.datetime(&value.into())),
|
||||
daily_resets_at: record.budget.daily_resets_at().map(DateTime::from),
|
||||
weekly_resets_at_display: record
|
||||
.budget
|
||||
.weekly_resets_at()
|
||||
.map(|value| time.datetime(&value.into())),
|
||||
weekly_resets_at: record.budget.weekly_resets_at().map(DateTime::from),
|
||||
budget: ClientBudgetView::from((record.budget, time)),
|
||||
created_at_display: time.datetime(&record.created_at),
|
||||
created_at: record.created_at,
|
||||
updated_at_display: time.datetime(&record.updated_at),
|
||||
@@ -739,11 +743,9 @@ fn validate_limit(value: u64, field: &'static str) -> Result<(), WireValidationE
|
||||
|
||||
fn normalize_request_profile_overrides(
|
||||
profiles: ProviderRequestProfileOverrides,
|
||||
openai: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
xai: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
) -> Result<gateway_admin::model::client_keys::ProviderRequestProfileOverrides, WireValidationError>
|
||||
{
|
||||
let mut normalized = profiles
|
||||
profiles
|
||||
.into_iter()
|
||||
.map(|(provider, profile)| {
|
||||
if !matches!(provider.as_str(), "openai" | "xai") {
|
||||
@@ -756,35 +758,16 @@ fn normalize_request_profile_overrides(
|
||||
gateway_core::account::OpaqueProviderData::new(profile),
|
||||
))
|
||||
})
|
||||
.collect::<Result<gateway_admin::model::client_keys::ProviderRequestProfileOverrides, _>>(
|
||||
)?;
|
||||
for (provider, profile) in [("openai", openai), ("xai", xai)] {
|
||||
let Some(profile) = profile else {
|
||||
continue;
|
||||
};
|
||||
validate_request_profile_size(&profile)?;
|
||||
let provider = ProviderKind::new(provider).expect("static Provider kind is valid");
|
||||
let profile = gateway_core::account::OpaqueProviderData::new(profile);
|
||||
if normalized
|
||||
.get(&provider)
|
||||
.is_some_and(|current| current != &profile)
|
||||
{
|
||||
return Err(WireValidationError::new("providerRequestProfileOverrides"));
|
||||
}
|
||||
normalized.insert(provider, profile);
|
||||
}
|
||||
Ok(normalized)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn normalize_request_profile_override_updates(
|
||||
profiles: ProviderRequestProfileOverrideUpdates,
|
||||
openai: Option<Option<serde_json::Map<String, serde_json::Value>>>,
|
||||
xai: Option<Option<serde_json::Map<String, serde_json::Value>>>,
|
||||
) -> Result<
|
||||
gateway_admin::model::client_keys::ProviderRequestProfileOverrideUpdates,
|
||||
WireValidationError,
|
||||
> {
|
||||
let mut normalized = profiles
|
||||
profiles
|
||||
.into_iter()
|
||||
.map(|(provider, profile)| {
|
||||
if !matches!(provider.as_str(), "openai" | "xai") {
|
||||
@@ -794,34 +777,12 @@ fn normalize_request_profile_override_updates(
|
||||
validate_request_profile_size(profile)?;
|
||||
}
|
||||
Ok((
|
||||
ProviderKind::new(provider).map_err(|_| {
|
||||
WireValidationError::new("providerRequestProfileOverrides")
|
||||
})?,
|
||||
ProviderKind::new(provider)
|
||||
.map_err(|_| WireValidationError::new("providerRequestProfileOverrides"))?,
|
||||
profile.map(gateway_core::account::OpaqueProviderData::new),
|
||||
))
|
||||
})
|
||||
.collect::<Result<
|
||||
gateway_admin::model::client_keys::ProviderRequestProfileOverrideUpdates,
|
||||
_,
|
||||
>>()?;
|
||||
for (provider, profile) in [("openai", openai), ("xai", xai)] {
|
||||
let Some(profile) = profile else {
|
||||
continue;
|
||||
};
|
||||
if let Some(profile) = profile.as_ref() {
|
||||
validate_request_profile_size(profile)?;
|
||||
}
|
||||
let provider = ProviderKind::new(provider).expect("static Provider kind is valid");
|
||||
let profile = profile.map(gateway_core::account::OpaqueProviderData::new);
|
||||
if normalized
|
||||
.get(&provider)
|
||||
.is_some_and(|current| current != &profile)
|
||||
{
|
||||
return Err(WireValidationError::new("providerRequestProfileOverrides"));
|
||||
}
|
||||
normalized.insert(provider, profile);
|
||||
}
|
||||
Ok(normalized)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn validate_request_profile_size(
|
||||
@@ -866,7 +827,12 @@ where
|
||||
S: SessionState + Clone + Send + Sync + 'static,
|
||||
{
|
||||
Router::new()
|
||||
.merge(super::client_key_usage::router::<S>())
|
||||
.route("/api/admin/client-keys", get(list_client_keys::<S>))
|
||||
.route(
|
||||
"/api/admin/client-keys/batch-update",
|
||||
post(batch_update_client_keys::<S>),
|
||||
)
|
||||
.route(
|
||||
"/api/admin/client-keys/create",
|
||||
post(create_client_key::<S>),
|
||||
@@ -913,6 +879,94 @@ where
|
||||
Ok(AdminResponse::new(StatusCode::OK, AdminEnvelope::ok(data)))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct BatchUpdateClientKeysRequest {
|
||||
ids: Vec<String>,
|
||||
group_ids: Option<Vec<String>>,
|
||||
enabled: Option<bool>,
|
||||
max_concurrency: Option<u64>,
|
||||
requests_per_minute: Option<u64>,
|
||||
daily_limit_usd: Option<String>,
|
||||
weekly_limit_usd: Option<String>,
|
||||
peer_sharing: Option<gateway_admin::model::client_keys::ClientPeerSharingPatch>,
|
||||
#[serde(default)]
|
||||
provider_request_profile_overrides: ProviderRequestProfileOverrideUpdates,
|
||||
installation_seed: Option<String>,
|
||||
}
|
||||
|
||||
impl BatchUpdateClientKeysRequest {
|
||||
/// 复用单把密钥的字段校验,缺省和显式零值分别保留
|
||||
pub fn into_command(self) -> Result<BatchUpdateClientKeys, WireValidationError> {
|
||||
if let Some(limit) = self.max_concurrency {
|
||||
validate_limit(limit, "maxConcurrency")?;
|
||||
}
|
||||
if let Some(limit) = self.requests_per_minute {
|
||||
validate_limit(limit, "requestsPerMinute")?;
|
||||
}
|
||||
let installation_seed = self
|
||||
.installation_seed
|
||||
.map(|value| {
|
||||
if value.len() != uuid::fmt::Hyphenated::LENGTH {
|
||||
return Err(WireValidationError::new("installationSeed"));
|
||||
}
|
||||
uuid::Uuid::try_parse(&value)
|
||||
.map_err(|_| WireValidationError::new("installationSeed"))
|
||||
})
|
||||
.transpose()?;
|
||||
Ok(BatchUpdateClientKeys {
|
||||
ids: self
|
||||
.ids
|
||||
.into_iter()
|
||||
.map(|id| client_key_id(id, "id"))
|
||||
.collect::<Result<_, _>>()?,
|
||||
group_ids: self.group_ids.map(validate_group_ids).transpose()?,
|
||||
enabled: self.enabled,
|
||||
max_concurrency: self.max_concurrency,
|
||||
requests_per_minute: self.requests_per_minute,
|
||||
daily_limit_usd: parse_budget(self.daily_limit_usd, "dailyLimitUsd")?,
|
||||
weekly_limit_usd: parse_budget(self.weekly_limit_usd, "weeklyLimitUsd")?,
|
||||
peer_sharing: self.peer_sharing.unwrap_or_default(),
|
||||
request_profile_override_updates: normalize_request_profile_override_updates(
|
||||
self.provider_request_profile_overrides,
|
||||
)?,
|
||||
installation_seed,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct BatchUpdatedClientKeysData {
|
||||
config_revision: gateway_admin::model::Revision,
|
||||
updated: usize,
|
||||
}
|
||||
|
||||
async fn batch_update_client_keys<S>(
|
||||
auth: AdminAuth,
|
||||
State(state): State<S>,
|
||||
AdminJson(request): AdminJson<BatchUpdateClientKeysRequest>,
|
||||
) -> Result<impl IntoResponse, AdminError>
|
||||
where
|
||||
S: SessionState + Send + Sync,
|
||||
{
|
||||
let command = request.into_command().map_err(map_wire_error)?;
|
||||
let updated = command.ids.len();
|
||||
let config_revision = state
|
||||
.admin_services()
|
||||
.client_keys()
|
||||
.batch_update(&auth.context().mutation_context(), command)
|
||||
.await
|
||||
.map_err(map_service_error)?;
|
||||
Ok(AdminResponse::new(
|
||||
StatusCode::OK,
|
||||
AdminEnvelope::ok(BatchUpdatedClientKeysData {
|
||||
config_revision,
|
||||
updated,
|
||||
}),
|
||||
))
|
||||
}
|
||||
|
||||
async fn create_client_key<S>(
|
||||
auth: AdminAuth,
|
||||
State(state): State<S>,
|
||||
@@ -1087,12 +1141,13 @@ fn map_wire_error(error: WireValidationError) -> AdminError {
|
||||
"requestsPerMinute" => "每分钟请求上限超出支持范围",
|
||||
"dailyLimitUsd" => "每日美元限额须非负,整数和小数各最多 10 位",
|
||||
"weeklyLimitUsd" => "每周美元限额须非负,整数和小数各最多 10 位",
|
||||
"providerRequestProfileOverrides" => "API Key 客户端身份配置无效或字段冲突",
|
||||
"providerRequestProfileOverrides" => "API Key 客户端身份配置无效",
|
||||
"installationSeed" => "安装身份种子必须是带连字符的 UUID",
|
||||
"limit" => "每页数量须为 1 至 65535",
|
||||
"search" => "搜索内容过长或包含控制字符",
|
||||
"sortBy" => "排序字段不受支持",
|
||||
"sortDirection" => "排序方向必须为 asc(升序)或 desc(降序)",
|
||||
_ => "API Key 请求参数不合法",
|
||||
field => return AdminError::bad_request(format!("{field} 字段不合法")),
|
||||
};
|
||||
AdminError::bad_request(message)
|
||||
}
|
||||
@@ -1100,10 +1155,3 @@ fn map_wire_error(error: WireValidationError) -> AdminError {
|
||||
fn map_service_error(error: gateway_admin::model::AdminError) -> AdminError {
|
||||
map_admin_service_error(error)
|
||||
}
|
||||
|
||||
// 省略表示不修改,null 表示恢复跟随通用设置
|
||||
fn deserialize_profile_override<'de, D: serde::Deserializer<'de>>(
|
||||
deserializer: D,
|
||||
) -> Result<Option<Option<serde_json::Map<String, serde_json::Value>>>, D::Error> {
|
||||
Option::<serde_json::Map<String, serde_json::Value>>::deserialize(deserializer).map(Some)
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ pub mod account_groups;
|
||||
pub mod accounts;
|
||||
pub mod auth;
|
||||
pub mod backups;
|
||||
mod client_key_usage;
|
||||
pub mod client_keys;
|
||||
mod compression;
|
||||
pub(crate) mod diagnostics;
|
||||
|
||||
@@ -925,25 +925,9 @@ pub(crate) fn dashboard_view(
|
||||
.filter(|window| window.used_percent.is_none())
|
||||
.and_then(|window| window.local_usage.as_ref())
|
||||
.map(|usage| usage.request_count);
|
||||
let (metric, metric_value_raw, metric_label, metric_value) = match local_requests {
|
||||
Some(count) => (
|
||||
DashboardAccountMetric::Requests,
|
||||
Some(count),
|
||||
"次数".to_owned(),
|
||||
if count > 0 {
|
||||
format_compact_number(count)
|
||||
} else {
|
||||
"—".to_owned()
|
||||
},
|
||||
),
|
||||
None => (
|
||||
DashboardAccountMetric::Tokens,
|
||||
credential.total_tokens,
|
||||
"今日 Token".to_owned(),
|
||||
credential
|
||||
.total_tokens
|
||||
.map_or_else(|| "—".to_owned(), format_compact_number),
|
||||
),
|
||||
let (metric, metric_value_raw) = match local_requests {
|
||||
Some(count) => (DashboardAccountMetric::Requests, Some(count)),
|
||||
None => (DashboardAccountMetric::Tokens, credential.total_tokens),
|
||||
};
|
||||
account_usage_views.push(DashboardAccountUsageView {
|
||||
upstream: credential.upstream.map(|info| {
|
||||
@@ -978,13 +962,10 @@ pub(crate) fn dashboard_view(
|
||||
.map(|window| crate::admin::accounts::quota_window_view(window, time)),
|
||||
metric,
|
||||
metric_value_raw,
|
||||
metric_label,
|
||||
metric_value,
|
||||
last_used_at: credential.last_used_at,
|
||||
last_used_relative: credential
|
||||
.last_used_at
|
||||
.map(|value| time.relative_view(value, range.end)),
|
||||
last_used: time.relative_optional(credential.last_used_at, range.end),
|
||||
});
|
||||
}
|
||||
let unavailable_accounts = provider_accounts
|
||||
@@ -1410,7 +1391,14 @@ pub(crate) fn map_wire_error(error: WireValidationError) -> AdminError {
|
||||
"dimension" => "诊断维度不合法",
|
||||
"id" => "用量记录 ID 不能为空",
|
||||
"cursor" => "观测数据游标不合法",
|
||||
_ => "观测查询参数不合法",
|
||||
"currentPage" => "页码须大于 0",
|
||||
"pageSize" => {
|
||||
return AdminError::bad_request(format!("每页数量须为 1 至 {}", DomainPageSize::MAX));
|
||||
}
|
||||
"status" => "请求状态须为 success、error 或 all",
|
||||
"errorOrigin" => "错误来源须为 proxy、upstream、connection 或 unknown",
|
||||
"errorOriginStatus" => "仅错误请求支持按错误来源筛选",
|
||||
field => return AdminError::bad_request(format!("{field} 字段不合法")),
|
||||
};
|
||||
AdminError::bad_request(message)
|
||||
}
|
||||
|
||||
@@ -32,8 +32,9 @@ impl DashboardQuery {
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct UsageQuery {
|
||||
pub client_api_key_name: Option<String>,
|
||||
pub account_name: Option<String>,
|
||||
pub account_search: Option<String>,
|
||||
pub status: Option<String>,
|
||||
pub error_origin: Option<String>,
|
||||
pub current_page: Option<u32>,
|
||||
pub page_size: Option<u16>,
|
||||
pub provider: Option<String>,
|
||||
@@ -339,7 +340,7 @@ pub(crate) fn dashboard_today_range(
|
||||
pub(crate) fn usage_filter(query: &UsageQuery) -> domain::UsageFilter {
|
||||
domain::UsageFilter {
|
||||
client_api_key_name: non_empty(query.client_api_key_name.clone()),
|
||||
provider_account_name: non_empty(query.account_name.clone()),
|
||||
provider_account_search: non_empty(query.account_search.clone()),
|
||||
provider_kind: non_empty(query.provider.clone()),
|
||||
model_contains: non_empty(query.model.clone()),
|
||||
..Default::default()
|
||||
@@ -354,11 +355,21 @@ pub(crate) fn usage_command(
|
||||
let page_size_value =
|
||||
DomainPageSize::new(page_size).map_err(|_| WireValidationError::new("pageSize"))?;
|
||||
Ok(domain::UsageQuery {
|
||||
status: match query.status.as_deref() {
|
||||
None | Some("success") => domain::UsageRecordStatus::Success,
|
||||
Some("error") => domain::UsageRecordStatus::Error,
|
||||
Some("all") => domain::UsageRecordStatus::All,
|
||||
Some(_) => return Err(WireValidationError::new("status")),
|
||||
status: match (query.status.as_deref(), query.error_origin.as_deref()) {
|
||||
(None | Some("success"), None) => domain::UsageRecordStatus::Success,
|
||||
(Some("all"), None) => domain::UsageRecordStatus::All,
|
||||
(Some("error"), origin) => domain::UsageRecordStatus::Error(match origin {
|
||||
None => None,
|
||||
Some("proxy") => Some(domain::UsageErrorOrigin::Proxy),
|
||||
Some("upstream") => Some(domain::UsageErrorOrigin::Upstream),
|
||||
Some("connection") => Some(domain::UsageErrorOrigin::Connection),
|
||||
Some("unknown") => Some(domain::UsageErrorOrigin::Unknown),
|
||||
Some(_) => return Err(WireValidationError::new("errorOrigin")),
|
||||
}),
|
||||
(None | Some("success" | "all"), Some(_)) => {
|
||||
return Err(WireValidationError::new("errorOriginStatus"));
|
||||
}
|
||||
(Some(_), _) => return Err(WireValidationError::new("status")),
|
||||
},
|
||||
range: usage_range(
|
||||
query.start_time.as_deref(),
|
||||
|
||||
@@ -447,11 +447,8 @@ pub struct DashboardAccountUsageView {
|
||||
pub usage_window: Option<crate::admin::accounts::AccountQuotaWindowView>,
|
||||
pub metric: DashboardAccountMetric,
|
||||
pub metric_value_raw: Option<u64>,
|
||||
pub metric_label: String,
|
||||
pub metric_value: String,
|
||||
pub last_used_at: Option<DateTime<Utc>>,
|
||||
pub last_used_relative: Option<crate::time::RelativeTimeView>,
|
||||
pub last_used: String,
|
||||
}
|
||||
|
||||
/// Provider 账号池的持久事实汇总
|
||||
|
||||
@@ -20,9 +20,24 @@ use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::{
|
||||
AdminAuth, AdminEnvelope, AdminError, AdminJson, AdminQuery, AdminResponse, PageMeta,
|
||||
accounts::{AccountGroupRefView, AccountProxyUpdate},
|
||||
accounts::AccountGroupRefView,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
struct ProxyUrl(Option<gateway_core::account::OutboundProxy>);
|
||||
|
||||
impl<'de> Deserialize<'de> for ProxyUrl {
|
||||
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
let value = String::deserialize(deserializer)?;
|
||||
if value.is_empty() {
|
||||
return Ok(Self(None));
|
||||
}
|
||||
gateway_core::account::OutboundProxy::parse(&value)
|
||||
.map(|proxy| Self(Some(proxy)))
|
||||
.map_err(serde::de::Error::custom)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
struct ListQuery {
|
||||
@@ -56,7 +71,7 @@ struct CreateRequest {
|
||||
auto_location: bool,
|
||||
location: Option<gateway_core::account::RequestLocation>,
|
||||
name: String,
|
||||
proxy_url: AccountProxyUpdate,
|
||||
proxy_url: ProxyUrl,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
@@ -70,7 +85,7 @@ struct UpdateRequest {
|
||||
id: String,
|
||||
revision: u64,
|
||||
name: String,
|
||||
proxy_url: Option<AccountProxyUpdate>,
|
||||
proxy_url: Option<ProxyUrl>,
|
||||
}
|
||||
|
||||
fn deserialize_location_update<'de, D>(
|
||||
@@ -105,7 +120,7 @@ struct ProbeRequest {
|
||||
use_deployment_proxy: bool,
|
||||
#[serde(default)]
|
||||
detect_location: bool,
|
||||
proxy_url: AccountProxyUpdate,
|
||||
proxy_url: ProxyUrl,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! Runtime settings、旧设置页聚合投影与明文 Admin API Key wire
|
||||
//! 管理端运行设置、模型定价与 Admin API Key 的 HTTP 接口
|
||||
|
||||
use crate::auth::SessionState;
|
||||
|
||||
@@ -36,6 +36,8 @@ pub type ProviderRequestProfileUpdates =
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct RuntimeSettingsFields {
|
||||
pub openai_oauth_guardian_review_model: bool,
|
||||
pub openai_request_country: gateway_core::account::OpenAiRequestCountry,
|
||||
pub codex_privacy_policy: gateway_core::settings::privacy::CodexPrivacyPolicy,
|
||||
pub request_location_enabled: bool,
|
||||
pub request_location: gateway_core::account::RequestLocation,
|
||||
@@ -64,7 +66,6 @@ pub struct RuntimeSettingsFields {
|
||||
pub account_warmup_enabled: bool,
|
||||
pub account_warmup_cron: String,
|
||||
pub account_warmup_model: Option<String>,
|
||||
#[serde(default)]
|
||||
pub reset_credit_on_low_weekly_quota: bool,
|
||||
pub reset_credit_lead_minutes: u32,
|
||||
pub reset_credit_max_weekly_remaining_percent: Option<u32>,
|
||||
@@ -80,10 +81,6 @@ pub struct RuntimeSettingsView {
|
||||
pub smart_scheduling_defaults: gateway_core::account::SmartSchedulingConfig,
|
||||
pub quota_reset_window_weights_defaults: gateway_core::account::ResetWindowWeights,
|
||||
pub provider_request_profiles: ProviderRequestProfiles,
|
||||
/// 固定兼容字段;值始终从 provider_request_profiles 派生
|
||||
pub openai_client_profile: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
/// 固定兼容字段;值始终从 provider_request_profiles 派生
|
||||
pub xai_client_profile: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
pub model_mappings: ModelMappings,
|
||||
pub rotation_strategy: String,
|
||||
pub updated_at: DateTime<Utc>,
|
||||
@@ -99,12 +96,6 @@ pub struct UpdateRuntimeSettingsRequest {
|
||||
pub config_revision: u64,
|
||||
#[serde(default)]
|
||||
pub provider_request_profiles: ProviderRequestProfileUpdates,
|
||||
/// 兼容既有 wire;与泛化字段冲突时拒绝整个请求
|
||||
#[serde(default, deserialize_with = "deserialize_profile_update")]
|
||||
pub openai_client_profile: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
/// 兼容既有 wire;与泛化字段冲突时拒绝整个请求
|
||||
#[serde(default, deserialize_with = "deserialize_profile_update")]
|
||||
pub xai_client_profile: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
pub model_mappings: ModelMappings,
|
||||
pub rotation_strategy: String,
|
||||
}
|
||||
@@ -121,11 +112,8 @@ impl UpdateRuntimeSettingsRequest {
|
||||
|
||||
fn into_command(self) -> Result<ReplaceRuntimeSettings, WireValidationError> {
|
||||
validate_model_mappings(&self.model_mappings)?;
|
||||
let request_profile_updates = normalize_request_profile_updates(
|
||||
self.provider_request_profiles,
|
||||
self.openai_client_profile,
|
||||
self.xai_client_profile,
|
||||
)?;
|
||||
let request_profile_updates =
|
||||
normalize_request_profile_updates(self.provider_request_profiles)?;
|
||||
Ok(ReplaceRuntimeSettings {
|
||||
expected_revision: gateway_admin::model::Revision::new(self.config_revision)
|
||||
.map_err(|_| WireValidationError::new("configRevision"))?,
|
||||
@@ -147,8 +135,6 @@ impl From<(RuntimeSettings, crate::time::TimePresenter)> for RuntimeSettingsView
|
||||
.collect::<ProviderRequestProfiles>();
|
||||
Self {
|
||||
config_revision: settings.config_revision.get(),
|
||||
openai_client_profile: provider_request_profiles.get("openai").cloned(),
|
||||
xai_client_profile: provider_request_profiles.get("xai").cloned(),
|
||||
provider_request_profiles,
|
||||
model_mappings: wire_model_mappings(settings.model_mappings),
|
||||
smart_scheduling_defaults: gateway_core::account::SmartSchedulingConfig::default(),
|
||||
@@ -595,8 +581,9 @@ fn map_wire_error(error: WireValidationError) -> AdminError {
|
||||
"settingsUsageRetentionOverflow" => "用量记录保留天数不合法".to_owned(),
|
||||
"settingsOpsRetentionOverflow" => "运维事件保留天数不合法".to_owned(),
|
||||
"settingsAuditRetentionOverflow" => "审计记录保留天数不合法".to_owned(),
|
||||
"openaiRequestCountry" => "自定义请求国家必须是两位大写 ASCII 字母".to_owned(),
|
||||
"requestLocation" => "请求位置的国家代码、地区或城市不合法".to_owned(),
|
||||
"providerRequestProfiles" => "服务商客户端身份配置无效或字段冲突".to_owned(),
|
||||
"providerRequestProfiles" => "服务商客户端身份配置无效".to_owned(),
|
||||
"minCodexDesktopVersion" => "Codex Desktop 最低版本格式不合法".to_owned(),
|
||||
"minCodexCliVersion" => "Codex CLI 最低版本格式不合法".to_owned(),
|
||||
field => format!("{field} 字段不合法"),
|
||||
@@ -610,10 +597,8 @@ fn map_service_error(error: gateway_admin::model::AdminError) -> AdminError {
|
||||
|
||||
fn normalize_request_profile_updates(
|
||||
profiles: ProviderRequestProfileUpdates,
|
||||
openai: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
xai: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
) -> Result<gateway_admin::model::settings::ProviderRequestProfileUpdates, WireValidationError> {
|
||||
let mut normalized = profiles
|
||||
profiles
|
||||
.into_iter()
|
||||
.map(|(provider, profile)| {
|
||||
if !matches!(provider.as_str(), "openai" | "xai") {
|
||||
@@ -626,30 +611,7 @@ fn normalize_request_profile_updates(
|
||||
profile.map(gateway_core::account::OpaqueProviderData::new),
|
||||
))
|
||||
})
|
||||
.collect::<Result<gateway_admin::model::settings::ProviderRequestProfileUpdates, _>>()?;
|
||||
for (provider, profile) in [("openai", openai), ("xai", xai)] {
|
||||
let Some(profile) = profile else {
|
||||
continue;
|
||||
};
|
||||
let provider = gateway_core::routing::ProviderKind::new(provider)
|
||||
.expect("static Provider kind is valid");
|
||||
let profile = gateway_core::account::OpaqueProviderData::new(profile);
|
||||
if normalized
|
||||
.get(&provider)
|
||||
.is_some_and(|current| current.as_ref() != Some(&profile))
|
||||
{
|
||||
return Err(WireValidationError::new("providerRequestProfiles"));
|
||||
}
|
||||
normalized.insert(provider, Some(profile));
|
||||
}
|
||||
Ok(normalized)
|
||||
}
|
||||
|
||||
// 字段省略时保留已有配置;显式 null 不能清空唯一的通用默认
|
||||
fn deserialize_profile_update<'de, D: serde::Deserializer<'de>>(
|
||||
deserializer: D,
|
||||
) -> Result<Option<serde_json::Map<String, serde_json::Value>>, D::Error> {
|
||||
serde_json::Map::<String, serde_json::Value>::deserialize(deserializer).map(Some)
|
||||
.collect()
|
||||
}
|
||||
|
||||
async fn detect_location<S>(
|
||||
@@ -746,6 +708,8 @@ impl RuntimeSettingsFields {
|
||||
self,
|
||||
) -> Result<gateway_admin::model::settings::RuntimeSettingsValues, WireValidationError> {
|
||||
let values = gateway_admin::model::settings::RuntimeSettingsValues {
|
||||
openai_oauth_guardian_review_model: self.openai_oauth_guardian_review_model,
|
||||
openai_request_country: self.openai_request_country,
|
||||
codex_privacy_policy: self.codex_privacy_policy,
|
||||
request_location_enabled: self.request_location_enabled,
|
||||
request_location: self
|
||||
@@ -795,6 +759,8 @@ impl RuntimeSettingsFields {
|
||||
impl From<gateway_admin::model::settings::RuntimeSettingsValues> for RuntimeSettingsFields {
|
||||
fn from(settings: gateway_admin::model::settings::RuntimeSettingsValues) -> Self {
|
||||
crate::admin::settings::RuntimeSettingsFields {
|
||||
openai_oauth_guardian_review_model: settings.openai_oauth_guardian_review_model,
|
||||
openai_request_country: settings.openai_request_country,
|
||||
codex_privacy_policy: settings.codex_privacy_policy,
|
||||
request_location_enabled: settings.request_location_enabled,
|
||||
request_location: settings.request_location,
|
||||
@@ -848,6 +814,7 @@ fn settings_validation_error(field: &'static str) -> WireValidationError {
|
||||
"max_waiting_per_key" => "maxWaitingPerKey",
|
||||
"min_codex_cli_version" => "minCodexCliVersion",
|
||||
"min_codex_desktop_version" => "minCodexDesktopVersion",
|
||||
"openai_request_country" => "openaiRequestCountry",
|
||||
"openai_account_affinity" => "openaiAccountAffinity",
|
||||
"openai_guardian_reserved_concurrency" => "openaiGuardianReservedConcurrency",
|
||||
"openai_session_affinity_ttl_hours" => "openaiSessionAffinityTtlHours",
|
||||
|
||||
@@ -215,12 +215,12 @@ const METHOD_NOT_ALLOWED: AdminErrorSpec = AdminErrorSpec::new(
|
||||
const TOO_MANY_LOGIN_ATTEMPTS: AdminErrorSpec = AdminErrorSpec::new(
|
||||
StatusCode::TOO_MANY_REQUESTS,
|
||||
AdminErrorCode::TOO_MANY_LOGIN_ATTEMPTS,
|
||||
"登录尝试过多,请稍后重试",
|
||||
"登录尝试过多",
|
||||
);
|
||||
const RATE_LIMITED: AdminErrorSpec = AdminErrorSpec::new(
|
||||
StatusCode::TOO_MANY_REQUESTS,
|
||||
AdminErrorCode::RATE_LIMITED,
|
||||
"请求过于频繁,请稍后重试",
|
||||
"请求过于频繁",
|
||||
);
|
||||
const INTERNAL: AdminErrorSpec = AdminErrorSpec::new(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
|
||||
@@ -178,7 +178,7 @@ pub(crate) fn client_access_error_response(error: ClientAccessError) -> Response
|
||||
response
|
||||
}
|
||||
|
||||
/// Desktop 优先于内嵌 CLI/Core;未知客户端及未提供应用版本的手机远程客户端返回 `None`
|
||||
/// 识别 Desktop 应用或 CLI/Core;未知客户端及未提供应用版本的手机远程客户端返回 `None`
|
||||
#[must_use]
|
||||
pub fn identify_codex_client(headers: &HeaderMap) -> Option<IdentifiedCodexClient> {
|
||||
const MAXIMUM_HEADER_LENGTH: usize = 4096;
|
||||
@@ -196,25 +196,35 @@ pub fn identify_codex_client(headers: &HeaderMap) -> Option<IdentifiedCodexClien
|
||||
"codex_mcp_server",
|
||||
];
|
||||
|
||||
let originator = bounded_ascii_header(headers, "originator", MAXIMUM_HEADER_LENGTH);
|
||||
let user_agent = bounded_ascii_header(headers, "user-agent", MAXIMUM_HEADER_LENGTH);
|
||||
let is_desktop = originator.is_some_and(|value| value.eq_ignore_ascii_case("Codex Desktop"))
|
||||
let ascii_header = |name: &'static str| headers.get(name).and_then(|value| value.to_str().ok());
|
||||
let raw_user_agent = ascii_header("user-agent");
|
||||
// HeaderValue::to_str 只接受 ASCII,可以按字节截取搜索范围
|
||||
let user_agent = raw_user_agent.map(|ua| &ua[..ua.len().min(MAXIMUM_HEADER_LENGTH)]);
|
||||
let is_desktop = ascii_header("originator")
|
||||
.is_some_and(|value| value.eq_ignore_ascii_case("Codex Desktop"))
|
||||
|| user_agent.is_some_and(|value| contains_ascii_case_insensitive(value, "Codex Desktop"));
|
||||
if is_desktop {
|
||||
let explicit_version = bounded_ascii_header(headers, "version", MAXIMUM_HEADER_LENGTH);
|
||||
let version_header = headers.get("version");
|
||||
let user_agent_version = user_agent.and_then(desktop_version_from_user_agent);
|
||||
// 手机远程初始化会覆盖进程级 UA 后缀,保留的 Desktop/Core 前缀不代表应用版本
|
||||
// 仅缺失版本信息时免于门禁;已提供的版本即使非法,也继续按 Desktop 校验
|
||||
if !headers.contains_key("version")
|
||||
&& user_agent_version.is_none()
|
||||
&& headers
|
||||
.get("user-agent")
|
||||
.is_some_and(|value| value.as_bytes().len() <= MAXIMUM_HEADER_LENGTH)
|
||||
&& user_agent.is_some_and(has_chatgpt_remote_user_agent_suffix)
|
||||
if user_agent_version.is_none()
|
||||
&& let Some(ua) = raw_user_agent.filter(|ua| ua.len() <= MAXIMUM_HEADER_LENGTH)
|
||||
&& let Some(client) = user_agent_client_name(ua)
|
||||
{
|
||||
return None;
|
||||
// 共享 App Server 可保留 Desktop 进程名,TUI 请求的 version 头也可能重复 Core 版本
|
||||
if client.eq_ignore_ascii_case("codex-tui")
|
||||
&& let Some(core) = desktop_core_version(ua)
|
||||
&& version_header.is_none_or(|value| value.as_bytes() == core.as_bytes())
|
||||
{
|
||||
return Some(IdentifiedCodexClient {
|
||||
kind: CodexClientKind::Cli,
|
||||
version: CodexClientVersion::parse(core).ok(),
|
||||
});
|
||||
}
|
||||
if version_header.is_none() && is_chatgpt_remote_client(client) {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
let version = match explicit_version {
|
||||
let version = match version_header.and_then(|value| value.to_str().ok()) {
|
||||
Some(value) => CodexClientVersion::parse(value).ok(),
|
||||
None => user_agent_version.and_then(|value| CodexClientVersion::parse(value).ok()),
|
||||
};
|
||||
@@ -244,18 +254,6 @@ pub fn identify_codex_client(headers: &HeaderMap) -> Option<IdentifiedCodexClien
|
||||
None
|
||||
}
|
||||
|
||||
fn bounded_ascii_header<'a>(
|
||||
headers: &'a HeaderMap,
|
||||
name: &'static str,
|
||||
maximum_length: usize,
|
||||
) -> Option<&'a str> {
|
||||
let value = headers.get(name)?.to_str().ok()?;
|
||||
if !value.is_ascii() {
|
||||
return None;
|
||||
}
|
||||
Some(&value[..value.len().min(maximum_length)])
|
||||
}
|
||||
|
||||
fn desktop_version_from_user_agent(user_agent: &str) -> Option<&str> {
|
||||
let marker = "Codex Desktop;";
|
||||
let start = find_ascii_case_insensitive(user_agent, marker)? + marker.len();
|
||||
@@ -266,27 +264,29 @@ fn desktop_version_from_user_agent(user_agent: &str) -> Option<&str> {
|
||||
.next()
|
||||
}
|
||||
|
||||
fn has_chatgpt_remote_user_agent_suffix(user_agent: &str) -> bool {
|
||||
let Some((_, suffix)) = user_agent
|
||||
.strip_suffix(')')
|
||||
.and_then(|value| value.rsplit_once(" ("))
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
let Some((name, version)) = suffix.split_once("; ") else {
|
||||
return false;
|
||||
};
|
||||
let Some(platform) = name
|
||||
.strip_prefix("codex_chatgpt_")
|
||||
.and_then(|value| value.strip_suffix("_remote"))
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
[platform, version].into_iter().all(|value| {
|
||||
fn desktop_core_version(user_agent: &str) -> Option<&str> {
|
||||
let (product, rest) = user_agent.split_once('/')?;
|
||||
let (version, _) = rest.split_once(' ')?;
|
||||
product
|
||||
.eq_ignore_ascii_case("Codex Desktop")
|
||||
.then_some(version)
|
||||
}
|
||||
|
||||
fn user_agent_client_name(user_agent: &str) -> Option<&str> {
|
||||
let (_, suffix) = user_agent.strip_suffix(')')?.rsplit_once(" (")?;
|
||||
let (name, version) = suffix.split_once("; ")?;
|
||||
let valid_token = |value: &str| {
|
||||
!value.is_empty()
|
||||
&& !value.contains(['(', ')', ';'])
|
||||
&& !value.bytes().any(|byte| byte.is_ascii_whitespace())
|
||||
})
|
||||
};
|
||||
(valid_token(name) && valid_token(version)).then_some(name)
|
||||
}
|
||||
|
||||
fn is_chatgpt_remote_client(name: &str) -> bool {
|
||||
name.strip_prefix("codex_chatgpt_")
|
||||
.and_then(|value| value.strip_suffix("_remote"))
|
||||
.is_some_and(|platform| !platform.is_empty())
|
||||
}
|
||||
|
||||
fn contains_ascii_case_insensitive(haystack: &str, needle: &str) -> bool {
|
||||
|
||||
@@ -115,9 +115,14 @@ async fn models_response(
|
||||
}
|
||||
})
|
||||
.collect::<Result<Vec<_>, ()>>();
|
||||
let Ok(models) = models else {
|
||||
let Ok(mut models) = models else {
|
||||
return catalog_unavailable_response();
|
||||
};
|
||||
if !client.policy().account_scope().allows_subagents() {
|
||||
for model in &mut models {
|
||||
model["multi_agent_version"] = json!("disabled");
|
||||
}
|
||||
}
|
||||
return (
|
||||
StatusCode::OK,
|
||||
// 目录经过账号选择/别名/多 Provider 聚合,不能借用上游 ETag 表示改写后的正文
|
||||
|
||||
@@ -19,7 +19,6 @@ use crate::openai::generation::{
|
||||
};
|
||||
|
||||
const OPENAI_SUBAGENT_KEY: &str = "x-openai-subagent";
|
||||
const CODEX_TURN_METADATA_KEY: &str = "x-codex-turn-metadata";
|
||||
const PASSTHROUGH_HEADERS_CONTEXT_KEY: &str = "opaque_request_headers";
|
||||
const DOWNSTREAM_WEBSOCKET_CONNECTION_ID_CONTEXT_KEY: &str = "downstream_websocket_connection_id";
|
||||
|
||||
@@ -34,11 +33,11 @@ pub(super) enum RequestDecodeSource {
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct OpenAiRequestHeaders {
|
||||
guardian: Option<String>,
|
||||
ambiguous_guardian_headers: bool,
|
||||
turn_state: Option<String>,
|
||||
turn_metadata: Option<String>,
|
||||
beta_features: Option<String>,
|
||||
version: Option<String>,
|
||||
include_timing_metrics: Option<String>,
|
||||
codex_window_id: Option<String>,
|
||||
parent_thread_id: Option<String>,
|
||||
downstream_websocket_connection_id: Option<String>,
|
||||
@@ -60,11 +59,20 @@ impl OpenAiRequestHeaders {
|
||||
#[must_use]
|
||||
pub fn from_headers(headers: &HeaderMap) -> Self {
|
||||
Self {
|
||||
guardian: header_string(headers, "x-codex-guardian"),
|
||||
ambiguous_guardian_headers: [
|
||||
"x-codex-guardian",
|
||||
OPENAI_SUBAGENT_KEY,
|
||||
"x-codex-turn-metadata",
|
||||
]
|
||||
.into_iter()
|
||||
.any(|name| {
|
||||
let mut values = headers.get_all(name).iter();
|
||||
values.next().is_some_and(|v| v.to_str().is_err()) || values.next().is_some()
|
||||
}),
|
||||
turn_state: header_string(headers, "x-codex-turn-state"),
|
||||
turn_metadata: header_string(headers, "x-codex-turn-metadata"),
|
||||
beta_features: header_string(headers, "x-codex-beta-features"),
|
||||
version: header_string(headers, "version"),
|
||||
include_timing_metrics: header_string(headers, "x-responsesapi-include-timing-metrics"),
|
||||
codex_window_id: header_string(headers, "x-codex-window-id"),
|
||||
parent_thread_id: header_string(headers, "x-codex-parent-thread-id"),
|
||||
downstream_websocket_connection_id: None,
|
||||
@@ -92,7 +100,46 @@ impl OpenAiRequestHeaders {
|
||||
self
|
||||
}
|
||||
|
||||
fn apply_subagent(&self, body: &mut Map<String, Value>) {
|
||||
fn apply_subagent(
|
||||
&self,
|
||||
body: &mut Map<String, Value>,
|
||||
context: &mut Map<String, Value>,
|
||||
source: RequestDecodeSource,
|
||||
) {
|
||||
if matches!(source, RequestDecodeSource::WebSocketFrame)
|
||||
&& let Some(frame_agent) = body
|
||||
.get("client_metadata")
|
||||
.and_then(|m| m.get(OPENAI_SUBAGENT_KEY))
|
||||
.and_then(Value::as_str)
|
||||
&& (frame_agent == "guardian"
|
||||
|| self.subagent.as_deref() == Some("guardian")
|
||||
|| self.guardian.is_some())
|
||||
{
|
||||
// 帧内声明优先;旧 opening 的不透明副本也不能在 Provider 再度覆盖
|
||||
let stale_subagent = self
|
||||
.subagent
|
||||
.as_deref()
|
||||
.is_some_and(|opening| opening != frame_agent);
|
||||
let stale_guardian = self.guardian.as_deref().is_some_and(|opening| {
|
||||
frame_agent != "guardian"
|
||||
|| gateway_protocol::openai::codex_guardian_role(body, context)
|
||||
.is_some_and(|role| role.header() != Some(opening))
|
||||
});
|
||||
if stale_subagent {
|
||||
context.insert("subagent".into(), Value::from(frame_agent));
|
||||
}
|
||||
if (stale_subagent || stale_guardian)
|
||||
&& let Some(Value::Array(headers)) =
|
||||
context.get_mut(PASSTHROUGH_HEADERS_CONTEXT_KEY)
|
||||
{
|
||||
headers.retain(|pair| match pair.get(0).and_then(Value::as_str) {
|
||||
Some(OPENAI_SUBAGENT_KEY) => !stale_subagent,
|
||||
Some("x-codex-guardian") => !stale_guardian,
|
||||
_ => true,
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
if let Some(subagent) = self.subagent.as_deref() {
|
||||
inject_subagent_metadata(body, subagent);
|
||||
}
|
||||
@@ -120,22 +167,19 @@ impl OpenAiRequestHeaders {
|
||||
fn protocol_context(
|
||||
&self,
|
||||
use_websocket: Option<bool>,
|
||||
frame_turn_metadata: Option<&String>,
|
||||
source: RequestDecodeSource,
|
||||
) -> Map<String, Value> {
|
||||
let mut context = Map::new();
|
||||
insert_protocol_context(&mut context, "turn_state", self.turn_state.as_ref());
|
||||
insert_protocol_context(
|
||||
&mut context,
|
||||
"turn_metadata",
|
||||
frame_turn_metadata.or(self.turn_metadata.as_ref()),
|
||||
);
|
||||
insert_protocol_context(&mut context, "beta_features", self.beta_features.as_ref());
|
||||
insert_protocol_context(&mut context, "turn_metadata", self.turn_metadata.as_ref());
|
||||
insert_protocol_context(&mut context, "subagent", self.subagent.as_ref());
|
||||
if matches!(source, RequestDecodeSource::WebSocketFrame) {
|
||||
context.insert(
|
||||
gateway_protocol::openai::WEBSOCKET_FRAME_CONTEXT_KEY.to_owned(),
|
||||
Value::Bool(true),
|
||||
);
|
||||
}
|
||||
insert_protocol_context(&mut context, "version", self.version.as_ref());
|
||||
insert_protocol_context(
|
||||
&mut context,
|
||||
"include_timing_metrics",
|
||||
self.include_timing_metrics.as_ref(),
|
||||
);
|
||||
insert_protocol_context(
|
||||
&mut context,
|
||||
"codex_window_id",
|
||||
@@ -337,12 +381,14 @@ pub(super) fn decode_request_object(
|
||||
.map(|response_id| ContinuationIntent::PreviousResponseId(response_id.to_owned()))
|
||||
.unwrap_or(ContinuationIntent::None);
|
||||
|
||||
request_headers.apply_subagent(&mut object);
|
||||
let frame_turn_metadata = match source {
|
||||
RequestDecodeSource::Http => None,
|
||||
RequestDecodeSource::WebSocketFrame => frame_turn_metadata(&object),
|
||||
};
|
||||
let protocol_context = request_headers.protocol_context(use_websocket, frame_turn_metadata);
|
||||
let mut protocol_context = request_headers.protocol_context(use_websocket, source);
|
||||
gateway_protocol::openai::capture_codex_guardian_identity(
|
||||
&object,
|
||||
&mut protocol_context,
|
||||
request_headers.guardian.as_deref(),
|
||||
request_headers.ambiguous_guardian_headers,
|
||||
);
|
||||
request_headers.apply_subagent(&mut object, &mut protocol_context, source);
|
||||
|
||||
let semantics =
|
||||
gateway_protocol::openai::codex_responses_request_semantics(&object, &protocol_context);
|
||||
@@ -381,16 +427,6 @@ pub(super) fn decode_request_object(
|
||||
})
|
||||
}
|
||||
|
||||
fn frame_turn_metadata(body: &Map<String, Value>) -> Option<&String> {
|
||||
body.get("client_metadata")
|
||||
.and_then(Value::as_object)
|
||||
.and_then(|metadata| metadata.get(CODEX_TURN_METADATA_KEY))
|
||||
.and_then(|value| match value {
|
||||
Value::String(value) if !value.trim().is_empty() => Some(value),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
fn inject_subagent_metadata(body: &mut Map<String, Value>, subagent: &str) {
|
||||
let metadata = body
|
||||
.entry("client_metadata".to_owned())
|
||||
|
||||
@@ -74,13 +74,11 @@ pub(crate) async fn responses_websocket(
|
||||
&headers,
|
||||
connect_info.map(|Extension(ConnectInfo(address))| address),
|
||||
);
|
||||
let request_headers = OpenAiRequestHeaders::from_headers(&headers);
|
||||
ResponsesWebSocketAdapter::new(service).upgrade_with_client_context(
|
||||
websocket,
|
||||
client,
|
||||
client_ip,
|
||||
user_agent,
|
||||
request_headers,
|
||||
headers,
|
||||
middleware.map(|Extension(plan)| plan),
|
||||
lifetime.map(|Extension(lifetime)| lifetime),
|
||||
@@ -107,7 +105,6 @@ impl ResponsesWebSocketAdapter {
|
||||
client: AuthenticatedClient,
|
||||
client_ip: Option<IpAddr>,
|
||||
user_agent: Option<String>,
|
||||
request_headers: OpenAiRequestHeaders,
|
||||
raw_headers: HeaderMap,
|
||||
middleware: Option<FrozenMiddlewarePlan>,
|
||||
lifetime: Option<Arc<crate::middleware::CallLifetime>>,
|
||||
@@ -130,7 +127,6 @@ impl ResponsesWebSocketAdapter {
|
||||
connection_id,
|
||||
client_ip,
|
||||
user_agent,
|
||||
request_headers,
|
||||
raw_headers,
|
||||
lifecycle: self.service.lifecycle(),
|
||||
connection_guard,
|
||||
@@ -150,7 +146,6 @@ struct ResponsesWebSocketSession {
|
||||
connection_id: String,
|
||||
client_ip: Option<IpAddr>,
|
||||
user_agent: Option<String>,
|
||||
request_headers: OpenAiRequestHeaders,
|
||||
raw_headers: HeaderMap,
|
||||
lifecycle: Arc<dyn ConnectionLifecycle>,
|
||||
connection_guard: Box<dyn ConnectionGuard>,
|
||||
@@ -164,8 +159,7 @@ async fn serve_responses_websocket(socket: Socket, session: ResponsesWebSocketSe
|
||||
connection_id,
|
||||
client_ip,
|
||||
user_agent,
|
||||
request_headers,
|
||||
raw_headers,
|
||||
mut raw_headers,
|
||||
lifecycle,
|
||||
connection_guard,
|
||||
middleware,
|
||||
@@ -178,8 +172,6 @@ async fn serve_responses_websocket(socket: Socket, session: ResponsesWebSocketSe
|
||||
let cancellation = lifecycle.cancellation().child_token();
|
||||
let _connection_cancellation =
|
||||
crate::middleware::websocket::CancelOnDrop(Some(cancellation.clone()));
|
||||
let request_headers =
|
||||
request_headers.with_downstream_websocket_connection_id(connection_id.clone());
|
||||
// 帧中间件可能改写终态,此时不能据原始响应关闭下游连接
|
||||
let reconnect_for_replay = !middleware
|
||||
.as_ref()
|
||||
@@ -241,31 +233,32 @@ async fn serve_responses_websocket(socket: Socket, session: ResponsesWebSocketSe
|
||||
continue;
|
||||
}
|
||||
request_count = request_count.saturating_add(1);
|
||||
// 初步解码只保留路由事实,避免整份正文跨越准入等待和响应交付
|
||||
let model_hint = match decode_response_create_with_context(&payload, &request_headers) {
|
||||
Ok(decoded) => Some(decoded.metadata().requested_model().to_owned()),
|
||||
Err(error) => {
|
||||
trace_rejected_request(
|
||||
&correlation_id,
|
||||
connection.id(),
|
||||
&payload,
|
||||
"decode",
|
||||
&error.to_string(),
|
||||
);
|
||||
if send_protocol_error(
|
||||
&mut connection,
|
||||
StatusCode::BAD_REQUEST,
|
||||
error.protocol_body(),
|
||||
&correlation_id,
|
||||
)
|
||||
.await
|
||||
== ForwardOutcome::Disconnect
|
||||
{
|
||||
break;
|
||||
// 初步解码只取帧中的模型,实际执行使用该帧当前生效的请求头
|
||||
let model_hint =
|
||||
match decode_response_create_with_context(&payload, &OpenAiRequestHeaders::default()) {
|
||||
Ok(decoded) => Some(decoded.metadata().requested_model().to_owned()),
|
||||
Err(error) => {
|
||||
trace_rejected_request(
|
||||
&correlation_id,
|
||||
connection.id(),
|
||||
&payload,
|
||||
"decode",
|
||||
&error.to_string(),
|
||||
);
|
||||
if send_protocol_error(
|
||||
&mut connection,
|
||||
StatusCode::BAD_REQUEST,
|
||||
error.protocol_body(),
|
||||
&correlation_id,
|
||||
)
|
||||
.await
|
||||
== ForwardOutcome::Disconnect
|
||||
{
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
};
|
||||
};
|
||||
// deadline 与 Text 可能同时就绪;在任何上游执行开始前再次封住该竞争窗口
|
||||
if connection.is_expired() {
|
||||
trace_rejected_request(
|
||||
@@ -336,6 +329,16 @@ async fn serve_responses_websocket(socket: Socket, session: ResponsesWebSocketSe
|
||||
headers: encode_headers(&raw_headers),
|
||||
body: Bytes::from(payload),
|
||||
};
|
||||
// 握手的轮次头只属于首次执行,复用连接时由各 response.create 携带本轮数据
|
||||
raw_headers.remove("x-codex-turn-state");
|
||||
raw_headers.remove("x-codex-turn-metadata");
|
||||
raw_headers.remove("x-codex-guardian");
|
||||
if raw_headers
|
||||
.get("x-openai-subagent")
|
||||
.is_some_and(|value| value == "guardian")
|
||||
{
|
||||
raw_headers.remove("x-openai-subagent");
|
||||
}
|
||||
let service_for_terminal = service.clone();
|
||||
let replay_for_terminal = replay.clone();
|
||||
let connection_id_for_terminal = connection.id().to_owned();
|
||||
|
||||
@@ -19,8 +19,8 @@ use super::{
|
||||
|
||||
use crate::ApiState;
|
||||
|
||||
// 公开协议路径由 API 层统一定义;Core 使用操作类型,不根据这些路径分派业务
|
||||
pub(super) const RESPONSES_PATH: &str = "/v1/responses";
|
||||
// Core 使用操作类型,不根据这些协议路径分派业务
|
||||
pub(super) use gateway_protocol::openai::RESPONSES_PATH;
|
||||
pub(super) const MESSAGES_PATH: &str = "/v1/messages";
|
||||
pub(super) const MESSAGE_TOKENS_PATH: &str = "/v1/messages/count_tokens";
|
||||
pub(super) const CHAT_COMPLETIONS_PATH: &str = "/v1/chat/completions";
|
||||
|
||||
@@ -78,6 +78,58 @@ async fn options_route_should_return_only_lightweight_group_fields() {
|
||||
assert_eq!(value["data"]["configRevision"], 7);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn models_route_reads_the_published_group_catalog() {
|
||||
use gateway_core::{
|
||||
account::ProviderAccountId,
|
||||
routing::{
|
||||
ConfigRevision, ModelCapabilities, ProviderKind, ProviderModel, RuntimeAccount,
|
||||
RuntimeAccountDirectory, RuntimeSnapshot, UpstreamModelId,
|
||||
},
|
||||
};
|
||||
use std::{
|
||||
collections::{BTreeMap, BTreeSet},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
let fixture = authenticated_fixture().await;
|
||||
let uri = format!("/api/admin/account-groups/models?id={PRIMARY_GROUP_ID}");
|
||||
let unavailable = request(router(&fixture), Method::GET, &uri, None, true).await;
|
||||
assert_eq!(unavailable.status(), StatusCode::SERVICE_UNAVAILABLE);
|
||||
let provider = ProviderKind::new("openai").unwrap();
|
||||
let snapshot = RuntimeSnapshot::new(
|
||||
ConfigRevision::new(7).unwrap(),
|
||||
gateway_core::settings::SettingsValues::new(1, "smart", Default::default(), None, None),
|
||||
vec![provider.clone()],
|
||||
vec![ProviderModel::new(
|
||||
provider.clone(),
|
||||
UpstreamModelId::new("model-a").unwrap(),
|
||||
ModelCapabilities::new(
|
||||
BTreeSet::from([gateway_core::operation::OperationKind::Generate]),
|
||||
None,
|
||||
),
|
||||
)],
|
||||
vec![],
|
||||
)
|
||||
.unwrap()
|
||||
.with_account_directory(Arc::new(RuntimeAccountDirectory::new(BTreeMap::from([(
|
||||
ProviderAccountId::new("acct_member").unwrap(),
|
||||
RuntimeAccount::new(
|
||||
provider,
|
||||
BTreeSet::from([super::group_id(PRIMARY_GROUP_ID)]),
|
||||
),
|
||||
)]))));
|
||||
fixture.published_snapshot.publish(snapshot);
|
||||
let response = request(router(&fixture), Method::GET, &uri, None, true).await;
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
assert_eq!(
|
||||
response_json(response).await["data"],
|
||||
json!({"models":[{"id":"model-a", "label":"model-a"}]})
|
||||
);
|
||||
let unauthorized = request(router(&fixture), Method::GET, &uri, None, false).await;
|
||||
assert_eq!(unauthorized.status(), StatusCode::UNAUTHORIZED);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn create_route_should_create_an_empty_group() {
|
||||
let fixture = authenticated_fixture().await;
|
||||
@@ -106,6 +158,11 @@ async fn create_route_should_create_an_empty_group() {
|
||||
assert_eq!(value["data"]["record"]["color"], "#A855F780");
|
||||
assert_eq!(value["data"]["record"]["fastMode"], "default");
|
||||
assert_eq!(value["data"]["record"]["allowImageGeneration"], true);
|
||||
assert_eq!(value["data"]["record"]["allowSubagents"], true);
|
||||
assert_eq!(
|
||||
value["data"]["record"]["modelAccess"],
|
||||
json!({"mode":"all","models":[]})
|
||||
);
|
||||
assert_eq!(value["data"]["configRevision"], 8);
|
||||
}
|
||||
|
||||
@@ -189,6 +246,8 @@ async fn delete_route_should_return_deleted_id_without_a_live_record() {
|
||||
async fn list_route_should_reject_unknown_and_invalid_pagination_fields() {
|
||||
let fixture = authenticated_fixture().await;
|
||||
for uri in [
|
||||
"/api/admin/account-groups/models",
|
||||
"/api/admin/account-groups/models?id=bad",
|
||||
"/api/admin/account-groups?other=true",
|
||||
"/api/admin/account-groups?page=0",
|
||||
"/api/admin/account-groups?pageSize=0",
|
||||
@@ -215,7 +274,7 @@ async fn mutation_route_should_reject_unknown_json_fields() {
|
||||
"name": "Unknown field",
|
||||
"description": null,
|
||||
"color": "#2563EBFF",
|
||||
"providerKind": "openai"
|
||||
"unknownField": true
|
||||
})),
|
||||
true,
|
||||
)
|
||||
@@ -232,7 +291,7 @@ async fn create_route_should_reject_six_digit_color_without_alpha() {
|
||||
Method::POST,
|
||||
"/api/admin/account-groups/create",
|
||||
Some(json!({
|
||||
"name": "Opaque legacy color",
|
||||
"name": "Color without alpha",
|
||||
"description": null,
|
||||
"color": "#2563EB"
|
||||
})),
|
||||
@@ -345,6 +404,8 @@ async fn fast_mode_group_updates_preserve_omitted_and_null_values() {
|
||||
for (value, expected) in [
|
||||
(Some(json!("disabled")), "disabled"),
|
||||
(None, "disabled"),
|
||||
(Some(json!("ultrafast")), "ultrafast"),
|
||||
(None, "ultrafast"),
|
||||
(Some(json!("enabled")), "enabled"),
|
||||
(None, "enabled"),
|
||||
(Some(Value::Null), "enabled"),
|
||||
@@ -376,6 +437,7 @@ async fn fast_mode_rejects_invalid_values_and_creates_groups_in_each_mode() {
|
||||
for (mode, expected) in [
|
||||
(json!("enabled"), StatusCode::CREATED),
|
||||
(json!("disabled"), StatusCode::CREATED),
|
||||
(json!("ultrafast"), StatusCode::CREATED),
|
||||
(json!("default"), StatusCode::CREATED),
|
||||
(json!("force"), StatusCode::UNPROCESSABLE_ENTITY),
|
||||
(json!(true), StatusCode::BAD_REQUEST),
|
||||
@@ -430,3 +492,53 @@ async fn image_generation_flag_round_trips_and_omitted_updates_preserve_it() {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn subagent_and_model_policies_round_trip_independently() {
|
||||
let fixture = authenticated_fixture().await;
|
||||
let policy = json!({"mode":"allowlist", "models":["model-a"]});
|
||||
let created = request(
|
||||
router(&fixture),
|
||||
Method::POST,
|
||||
"/api/admin/account-groups/create",
|
||||
Some(json!({
|
||||
"name":"Model policy", "color":"#2563EBFF", "modelAccess":policy, "allowSubagents":false
|
||||
})),
|
||||
true,
|
||||
)
|
||||
.await;
|
||||
assert_eq!(created.status(), StatusCode::CREATED);
|
||||
let created = response_json(created).await;
|
||||
assert_eq!(created["data"]["record"]["modelAccess"], policy);
|
||||
assert_eq!(created["data"]["record"]["allowSubagents"], false);
|
||||
let mut update = json!({"id":created["data"]["id"], "name":"Model policy", "color":"#2563EBFF", "allowSubagents":true});
|
||||
let send = |body| {
|
||||
request(
|
||||
router(&fixture),
|
||||
Method::POST,
|
||||
"/api/admin/account-groups/update",
|
||||
Some(body),
|
||||
true,
|
||||
)
|
||||
};
|
||||
let changed = send(update.clone()).await;
|
||||
assert_eq!(changed.status(), StatusCode::OK);
|
||||
let changed = response_json(changed).await;
|
||||
assert_eq!(changed["data"]["record"]["modelAccess"], policy);
|
||||
assert_eq!(changed["data"]["record"]["allowSubagents"], true);
|
||||
update.as_object_mut().unwrap().remove("allowSubagents");
|
||||
update["modelAccess"] = json!({"mode":"denylist","models":["model-b"]});
|
||||
let changed = send(update.clone()).await;
|
||||
assert_eq!(changed.status(), StatusCode::OK);
|
||||
let changed = response_json(changed).await;
|
||||
assert_eq!(
|
||||
changed["data"]["record"]["modelAccess"],
|
||||
update["modelAccess"]
|
||||
);
|
||||
assert_eq!(changed["data"]["record"]["allowSubagents"], true);
|
||||
update["modelAccess"] = json!({"mode":"allowlist","models":[]});
|
||||
assert_eq!(
|
||||
send(update).await.status(),
|
||||
StatusCode::UNPROCESSABLE_ENTITY
|
||||
);
|
||||
}
|
||||
|
||||
@@ -341,7 +341,7 @@ async fn account_quota_refresh_time_uses_server_clock_and_deployment_timezone()
|
||||
));
|
||||
let future = now + Duration::days(1);
|
||||
let future_display = zone.local(future).format("%Y-%m-%d %H:%M:%S").to_string();
|
||||
for (observed_at, expected, legacy) in [
|
||||
for (observed_at, expected, display) in [
|
||||
(None, json!(null), "—"),
|
||||
(Some(now), json!({ "kind": "just_now" }), "刚刚"),
|
||||
(
|
||||
@@ -389,7 +389,7 @@ async fn account_quota_refresh_time_uses_server_clock_and_deployment_timezone()
|
||||
let value: serde_json::Value = serde_json::from_slice(&body).unwrap();
|
||||
let quota = &value["data"]["items"][0]["quota"];
|
||||
assert_eq!(quota["refreshedAtRelative"], expected);
|
||||
assert_eq!(quota["refreshedAtDisplay"], legacy);
|
||||
assert_eq!(quota["refreshedAtDisplay"], display);
|
||||
let refreshed_at = quota["refreshedAt"]
|
||||
.as_str()
|
||||
.map(|value| DateTime::parse_from_rfc3339(value).unwrap().to_utc());
|
||||
|
||||
@@ -12,6 +12,63 @@ use serde_json::json;
|
||||
|
||||
use super::{AdminTestFixture, AdminTestState};
|
||||
|
||||
#[test]
|
||||
fn batch_key_form_preserves_omissions_and_accepts_explicit_zero_false_and_inheritance() {
|
||||
use gateway_api::admin::client_keys::BatchUpdateClientKeysRequest;
|
||||
use gateway_core::routing::ProviderKind;
|
||||
let parse = |value| {
|
||||
serde_json::from_value::<BatchUpdateClientKeysRequest>(value)
|
||||
.unwrap()
|
||||
.into_command()
|
||||
};
|
||||
let patch = parse(json!({
|
||||
"ids":["key_a","key_b"], "enabled":false,
|
||||
"maxConcurrency":0, "dailyLimitUsd":"0", "weeklyLimitUsd":"1.75",
|
||||
"peerSharing":{"shareQuota":false}, "providerRequestProfileOverrides":{"xai":null}
|
||||
}))
|
||||
.unwrap();
|
||||
assert_eq!(patch.enabled, Some(false));
|
||||
assert_eq!(patch.max_concurrency, Some(0));
|
||||
assert_eq!(patch.daily_limit_usd.unwrap().canonical(), "0");
|
||||
assert_eq!(patch.weekly_limit_usd.unwrap().canonical(), "1.75");
|
||||
assert_eq!(patch.peer_sharing.share_quota, Some(false));
|
||||
assert_eq!(patch.peer_sharing.enabled, None);
|
||||
assert_eq!(patch.requests_per_minute, None);
|
||||
assert_eq!(patch.group_ids, None);
|
||||
assert_eq!(patch.installation_seed, None);
|
||||
assert_eq!(
|
||||
patch
|
||||
.request_profile_override_updates
|
||||
.get(&ProviderKind::new("xai").unwrap()),
|
||||
Some(&None)
|
||||
);
|
||||
|
||||
for (field, value) in [
|
||||
("dailyLimitUsd", json!("-0.01")),
|
||||
("weeklyLimitUsd", json!("1.00000000001")),
|
||||
("maxConcurrency", json!(u64::MAX)),
|
||||
("requestsPerMinute", json!(u64::MAX)),
|
||||
(
|
||||
"installationSeed",
|
||||
json!("A3A4E0BDF84447068DF8BFF10DE95B3B"),
|
||||
),
|
||||
("providerRequestProfileOverrides", json!({"unknown": null})),
|
||||
] {
|
||||
let mut payload = json!({ "ids":["key_a"] });
|
||||
payload[field] = value;
|
||||
assert_eq!(parse(payload).unwrap_err().field(), field);
|
||||
}
|
||||
let seed = "A3A4E0BD-F844-4706-8DF8-BFF10DE95B3B";
|
||||
assert_eq!(
|
||||
parse(json!({"ids":["key_a"], "installationSeed":seed}))
|
||||
.unwrap()
|
||||
.installation_seed
|
||||
.unwrap()
|
||||
.to_string(),
|
||||
seed.to_lowercase()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reset_budget_requires_an_explicit_supported_period_and_valid_key() {
|
||||
use gateway_api::admin::client_keys::ResetClientKeyBudgetRequest;
|
||||
@@ -572,45 +629,26 @@ async fn list_route_should_accept_the_full_nonzero_u16_page_size() {
|
||||
|
||||
#[test]
|
||||
fn profile_override_distinguishes_omission_from_explicit_inheritance() {
|
||||
let mut payload = json!({"id":"key_profile", "name":"profile", "groupIds":[], "maxConcurrency":0, "requestsPerMinute":0});
|
||||
let decode = |body| {
|
||||
let mut command = serde_json::from_value::<UpdateClientKeyRequest>(body)
|
||||
.unwrap()
|
||||
.into_command()
|
||||
.unwrap();
|
||||
command
|
||||
.request_profile_override_updates
|
||||
.remove(&gateway_core::routing::ProviderKind::new("openai").expect("Provider kind"))
|
||||
};
|
||||
assert_eq!(decode(payload.clone()), None);
|
||||
payload["openaiClientProfileOverride"] = json!(null);
|
||||
assert_eq!(decode(payload.clone()), Some(None));
|
||||
payload["openaiClientProfileOverride"] =
|
||||
json!({"client":"cli", "platform":"linux", "versionMode":"latest"});
|
||||
assert!(decode(payload).unwrap().is_some());
|
||||
for provider in ["openai", "xai"] {
|
||||
let decode = |body| {
|
||||
serde_json::from_value::<UpdateClientKeyRequest>(body)
|
||||
.unwrap()
|
||||
.into_command()
|
||||
.unwrap()
|
||||
.request_profile_override_updates
|
||||
.remove(&gateway_core::routing::ProviderKind::new(provider).unwrap())
|
||||
};
|
||||
let mut payload = json!({"id":"key_profile", "name":"profile", "groupIds":[], "maxConcurrency":0, "requestsPerMinute":0});
|
||||
assert_eq!(decode(payload.clone()), None);
|
||||
payload["providerRequestProfileOverrides"] = json!({(provider):null});
|
||||
assert_eq!(decode(payload.clone()), Some(None));
|
||||
payload["providerRequestProfileOverrides"][provider] = json!({"versionMode":"latest"});
|
||||
assert!(decode(payload).unwrap().is_some());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xai_profile_override_distinguishes_omission_from_explicit_inheritance() {
|
||||
let mut payload = json!({"id":"key_profile", "name":"profile", "groupIds":[], "maxConcurrency":0, "requestsPerMinute":0});
|
||||
let decode = |body| {
|
||||
let mut command = serde_json::from_value::<UpdateClientKeyRequest>(body)
|
||||
.unwrap()
|
||||
.into_command()
|
||||
.unwrap();
|
||||
command
|
||||
.request_profile_override_updates
|
||||
.remove(&gateway_core::routing::ProviderKind::new("xai").expect("Provider kind"))
|
||||
};
|
||||
assert_eq!(decode(payload.clone()), None);
|
||||
payload["xaiClientProfileOverride"] = json!(null);
|
||||
assert_eq!(decode(payload.clone()), Some(None));
|
||||
payload["xaiClientProfileOverride"] = json!({"versionMode":"latest"});
|
||||
assert!(decode(payload).unwrap().is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generic_profile_overrides_accept_native_providers_and_reject_legacy_conflicts() {
|
||||
fn profile_overrides_accept_native_providers_and_reject_unknown_providers() {
|
||||
let profile = json!({"preset":"desktop"});
|
||||
let xai = json!({"preset":"managed"});
|
||||
let request: CreateClientKeyRequest = serde_json::from_value(json!({
|
||||
@@ -622,7 +660,6 @@ fn generic_profile_overrides_accept_native_providers_and_reject_legacy_conflicts
|
||||
"openai": profile,
|
||||
"xai": xai,
|
||||
},
|
||||
"openaiClientProfileOverride": {"preset":"desktop"},
|
||||
}))
|
||||
.unwrap();
|
||||
let command = request.into_command().unwrap();
|
||||
@@ -633,20 +670,6 @@ fn generic_profile_overrides_accept_native_providers_and_reject_legacy_conflicts
|
||||
.contains_key(&gateway_core::routing::ProviderKind::new("xai").expect("Provider kind"))
|
||||
);
|
||||
|
||||
let conflict: CreateClientKeyRequest = serde_json::from_value(json!({
|
||||
"name":"profile",
|
||||
"groupIds":[],
|
||||
"maxConcurrency":0,
|
||||
"requestsPerMinute":0,
|
||||
"providerRequestProfileOverrides": {"openai":{"preset":"desktop"}},
|
||||
"openaiClientProfileOverride": {"preset":"cli"},
|
||||
}))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
conflict.into_command().unwrap_err().field(),
|
||||
"providerRequestProfileOverrides"
|
||||
);
|
||||
|
||||
let unknown: CreateClientKeyRequest = serde_json::from_value(json!({
|
||||
"name":"profile",
|
||||
"groupIds":[],
|
||||
@@ -675,20 +698,80 @@ fn generic_profile_overrides_accept_native_providers_and_reject_legacy_conflicts
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generic_profile_override_clear_rejects_conflicting_legacy_update() {
|
||||
let request: UpdateClientKeyRequest = serde_json::from_value(json!({
|
||||
"id":"key_profile",
|
||||
"name":"profile",
|
||||
"groupIds":[],
|
||||
"maxConcurrency":0,
|
||||
"requestsPerMinute":0,
|
||||
"providerRequestProfileOverrides": {"xai":null},
|
||||
"xaiClientProfileOverride": {"preset":"desktop"},
|
||||
}))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
request.into_command().unwrap_err().field(),
|
||||
"providerRequestProfileOverrides"
|
||||
);
|
||||
#[tokio::test]
|
||||
async fn key_statistics_route_returns_calendar_days_and_current_budget_only_to_admin() {
|
||||
use crate::support::{cookie_request, key_fixture, response_json};
|
||||
use axum::http::{Method, StatusCode};
|
||||
use gateway_core::policy::ClientApiKeyId;
|
||||
use tower::ServiceExt as _;
|
||||
|
||||
let fixture = key_fixture().await;
|
||||
fixture.auth.insert_session("stats-admin");
|
||||
let record = fixture
|
||||
.services
|
||||
.client_keys()
|
||||
.reveal(&ClientApiKeyId::new("key-42").unwrap())
|
||||
.await
|
||||
.unwrap()
|
||||
.record;
|
||||
*fixture.client_key.lock().unwrap() = Some(record);
|
||||
let app = client_keys::router::<AdminTestState>().with_state(fixture.state());
|
||||
for (query, cookie, status, days) in [
|
||||
("id=key-42", "", StatusCode::UNAUTHORIZED, 0),
|
||||
(
|
||||
"id=missing",
|
||||
"cpr_session=stats-admin",
|
||||
StatusCode::NOT_FOUND,
|
||||
0,
|
||||
),
|
||||
(
|
||||
"id=key-42&period=today",
|
||||
"cpr_session=stats-admin",
|
||||
StatusCode::BAD_REQUEST,
|
||||
0,
|
||||
),
|
||||
("id=key-42", "cpr_session=stats-admin", StatusCode::OK, 7),
|
||||
(
|
||||
"id=key-42&period=30d",
|
||||
"cpr_session=stats-admin",
|
||||
StatusCode::OK,
|
||||
30,
|
||||
),
|
||||
] {
|
||||
let mut request = cookie_request(
|
||||
Method::GET,
|
||||
&format!("/api/admin/client-keys/statistics?{query}"),
|
||||
cookie,
|
||||
);
|
||||
request
|
||||
.headers_mut()
|
||||
.insert("x-request-id", "req_stats".parse().unwrap());
|
||||
let response = app.clone().oneshot(request).await.unwrap();
|
||||
assert_eq!(response.status(), status);
|
||||
if days > 0 {
|
||||
let body = response_json(response).await;
|
||||
assert_eq!(body["data"]["dates"].as_array().unwrap().len(), days);
|
||||
assert!(body["data"]["budget"]["dailyUsedUsd"].is_string());
|
||||
assert!(body["data"]["series"].as_array().unwrap().is_empty());
|
||||
assert_eq!(body["data"]["modelCount"], 0);
|
||||
}
|
||||
}
|
||||
for (cookie, status) in [
|
||||
("", StatusCode::UNAUTHORIZED),
|
||||
("cpr_session=stats-admin", StatusCode::OK),
|
||||
] {
|
||||
let mut request = cookie_request(
|
||||
Method::GET,
|
||||
"/api/admin/client-keys/statistics/models?id=key-42",
|
||||
cookie,
|
||||
);
|
||||
request
|
||||
.headers_mut()
|
||||
.insert("x-request-id", "req_models".parse().unwrap());
|
||||
let response = app.clone().oneshot(request).await.unwrap();
|
||||
assert_eq!(response.status(), status);
|
||||
if status == StatusCode::OK {
|
||||
assert_eq!(response_json(response).await["data"], serde_json::json!([]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -442,29 +442,14 @@ mod provider {
|
||||
*fixture.account.lock().unwrap() = Some(account(provider));
|
||||
for (kind, status, code, message) in [
|
||||
(Kind::Invalid, 400, 40001, "刷新令牌已被使用,请重新授权"),
|
||||
(
|
||||
Kind::Conflict,
|
||||
409,
|
||||
40901,
|
||||
"令牌刷新繁忙,请等待当前刷新完成后重试",
|
||||
),
|
||||
(
|
||||
Kind::BadGateway,
|
||||
502,
|
||||
50201,
|
||||
"OpenAI 返回的 Codex PAT 身份资料不完整或格式无效,请稍后重试",
|
||||
),
|
||||
(
|
||||
Kind::Unavailable,
|
||||
503,
|
||||
50301,
|
||||
"暂时无法向 OpenAI 验证 Codex PAT,请稍后重试",
|
||||
),
|
||||
(Kind::Conflict, 409, 40901, "令牌刷新繁忙,请稍后重试"),
|
||||
(Kind::BadGateway, 502, 50201, "PAT 身份信息无效"),
|
||||
(Kind::Unavailable, 503, 50301, "PAT 验证服务暂不可用"),
|
||||
(
|
||||
Kind::Ambiguous,
|
||||
502,
|
||||
50202,
|
||||
"令牌刷新结果未知,请先核对账号状态,不要立即重复刷新",
|
||||
"刷新结果未知,核对账号状态前请勿重试",
|
||||
),
|
||||
] {
|
||||
*fixture.provider_error.lock().unwrap() = Some(
|
||||
@@ -513,6 +498,12 @@ mod provider {
|
||||
(Kind::Unsupported, 400, 40001, "该服务商不支持此操作"),
|
||||
(Kind::BadGateway, 502, 50201, "上游服务请求失败"),
|
||||
(Kind::Unavailable, 503, 50301, "服务商暂不可用"),
|
||||
(
|
||||
Kind::CredentialRefreshRequired,
|
||||
503,
|
||||
50301,
|
||||
"账号凭据需要刷新",
|
||||
),
|
||||
(
|
||||
Kind::Ambiguous,
|
||||
502,
|
||||
|
||||
@@ -641,43 +641,7 @@ impl SettingsStore for MemorySettingsStore {
|
||||
model_mappings: command.model_mappings,
|
||||
rotation_strategy: command.rotation_strategy,
|
||||
updated_at: Utc::now(),
|
||||
values: gateway_admin::model::settings::RuntimeSettingsValues {
|
||||
codex_privacy_policy: command.values.codex_privacy_policy,
|
||||
request_location_enabled: command.values.request_location_enabled,
|
||||
request_location: command.values.request_location,
|
||||
refresh_margin_seconds: command.values.refresh_margin_seconds,
|
||||
refresh_concurrency: command.values.refresh_concurrency,
|
||||
max_concurrent_per_account: command.values.max_concurrent_per_account,
|
||||
max_waiting_per_key: command.values.max_waiting_per_key,
|
||||
max_waiting_per_account: command.values.max_waiting_per_account,
|
||||
key_queue_timeout_seconds: command.values.key_queue_timeout_seconds,
|
||||
account_queue_timeout_seconds: command.values.account_queue_timeout_seconds,
|
||||
switch_account_on_queue_failure: command.values.switch_account_on_queue_failure,
|
||||
max_retry_after_seconds: command.values.max_retry_after_seconds,
|
||||
openai_guardian_reserved_concurrency: command
|
||||
.values
|
||||
.openai_guardian_reserved_concurrency,
|
||||
openai_account_affinity: command.values.openai_account_affinity,
|
||||
max_account_rotations: command.values.max_account_rotations,
|
||||
openai_session_affinity_ttl_hours: command.values.openai_session_affinity_ttl_hours,
|
||||
responses_max_decompressed_body_bytes: command
|
||||
.values
|
||||
.responses_max_decompressed_body_bytes,
|
||||
smart_scheduling: command.values.smart_scheduling,
|
||||
quota_reset_window_weights: command.values.quota_reset_window_weights,
|
||||
min_codex_desktop_version: command.values.min_codex_desktop_version,
|
||||
min_codex_cli_version: command.values.min_codex_cli_version,
|
||||
usage_retention_days: command.values.usage_retention_days,
|
||||
ops_event_retention_days: command.values.ops_event_retention_days,
|
||||
audit_retention_days: command.values.audit_retention_days,
|
||||
account_error_protection: command.values.account_error_protection,
|
||||
account_warmup_enabled: false,
|
||||
account_warmup_cron: "0 8 * * *".to_owned(),
|
||||
account_warmup_model: None,
|
||||
reset_credit_lead_minutes: 10,
|
||||
reset_credit_on_low_weekly_quota: false,
|
||||
reset_credit_max_weekly_remaining_percent: None,
|
||||
},
|
||||
values: command.values,
|
||||
};
|
||||
*settings = updated.clone();
|
||||
Ok(updated)
|
||||
@@ -745,6 +709,8 @@ impl MemoryAccountGroupStore {
|
||||
primary_id.clone(),
|
||||
AccountGroupRecord {
|
||||
allow_image_generation: true,
|
||||
allow_subagents: true,
|
||||
model_access: Default::default(),
|
||||
fast_mode: FastMode::Default,
|
||||
id: primary_id,
|
||||
name: "Alpha routing".to_owned(),
|
||||
@@ -768,6 +734,8 @@ impl MemoryAccountGroupStore {
|
||||
secondary_id.clone(),
|
||||
AccountGroupRecord {
|
||||
allow_image_generation: true,
|
||||
allow_subagents: true,
|
||||
model_access: Default::default(),
|
||||
fast_mode: FastMode::Default,
|
||||
id: secondary_id,
|
||||
name: "Beta routing".to_owned(),
|
||||
@@ -931,6 +899,8 @@ impl AccountGroupStore for MemoryAccountGroupStore {
|
||||
let now = Utc::now();
|
||||
let record = AccountGroupRecord {
|
||||
allow_image_generation: command.allow_image_generation,
|
||||
allow_subagents: command.allow_subagents,
|
||||
model_access: command.model_access,
|
||||
fast_mode: command.fast_mode,
|
||||
id: command.id.clone(),
|
||||
name: command.name,
|
||||
@@ -966,6 +936,12 @@ impl AccountGroupStore for MemoryAccountGroupStore {
|
||||
if let Some(fast_mode) = command.fast_mode {
|
||||
record.fast_mode = fast_mode;
|
||||
}
|
||||
if let Some(allowed) = command.allow_subagents {
|
||||
record.allow_subagents = allowed;
|
||||
}
|
||||
if let Some(model_access) = command.model_access {
|
||||
record.model_access = model_access;
|
||||
}
|
||||
if let Some(allowed) = command.allow_image_generation {
|
||||
record.allow_image_generation = allowed;
|
||||
}
|
||||
@@ -1020,6 +996,30 @@ fn mutation(
|
||||
|
||||
#[async_trait]
|
||||
impl ClientKeyStore for MemoryClientKeyStore {
|
||||
async fn budget_reference_accounts(
|
||||
&self,
|
||||
_: &gateway_core::policy::ClientApiKeyId,
|
||||
_: &str,
|
||||
_: &str,
|
||||
) -> gateway_admin::ports::store::AdminStoreResult<
|
||||
gateway_admin::model::client_keys::BudgetReferenceAccountPage,
|
||||
> {
|
||||
Err(unavailable("reference accounts"))
|
||||
}
|
||||
|
||||
async fn batch_update_client_keys(
|
||||
&self,
|
||||
_: gateway_admin::model::client_keys::BatchUpdateClientKeys,
|
||||
_: gateway_admin::ports::store::BatchClientKeyProfileMerger,
|
||||
_: &MutationContext,
|
||||
) -> AdminStoreResult<Revision> {
|
||||
Err(AdminStoreError::new(
|
||||
AdminStoreErrorKind::Unavailable,
|
||||
"client key",
|
||||
"unused batch update",
|
||||
))
|
||||
}
|
||||
|
||||
async fn client_key_budget_window(
|
||||
&self,
|
||||
_: &gateway_core::policy::ClientApiKeyId,
|
||||
@@ -1088,6 +1088,15 @@ impl ClientKeyStore for MemoryClientKeyStore {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn client_key_budget(
|
||||
&self,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
) -> AdminStoreResult<Option<gateway_core::engine::budget::ClientBudgetStatus>> {
|
||||
self.get_client_key(id)
|
||||
.await
|
||||
.map(|key| key.map(|row| row.budget))
|
||||
}
|
||||
|
||||
async fn get_client_key(
|
||||
&self,
|
||||
id: &ClientApiKeyId,
|
||||
@@ -1300,6 +1309,17 @@ impl AccountStore for UnusedStore {
|
||||
Err(unavailable("account quota window usage"))
|
||||
}
|
||||
|
||||
async fn quota_documents(
|
||||
&self,
|
||||
_: &gateway_core::account::ProviderAccountId,
|
||||
_: &gateway_core::routing::ProviderKind,
|
||||
_: chrono::DateTime<chrono::Utc>,
|
||||
_: gateway_admin::model::observability::TimeRange,
|
||||
_: Option<&gateway_admin::model::quota_history::QuotaHistoryCursor>,
|
||||
) -> AdminStoreResult<gateway_admin::model::quota_history::QuotaDocumentPage> {
|
||||
unreachable!("测试不读取历史额度文档")
|
||||
}
|
||||
|
||||
async fn load_quota_forecast_history(
|
||||
&self,
|
||||
_: &AccountUsageWindowQuery,
|
||||
@@ -1532,6 +1552,23 @@ impl ObservabilityStore for UnusedStore {
|
||||
Err(unavailable("dashboard trend"))
|
||||
}
|
||||
|
||||
async fn client_key_statistics(
|
||||
&self,
|
||||
_: TimeRange,
|
||||
_: &gateway_core::policy::ClientApiKeyId,
|
||||
_: gateway_admin::model::observability::KeyUsageMetric,
|
||||
) -> AdminStoreResult<gateway_admin::model::observability::KeyStatisticsOverview> {
|
||||
Ok(Default::default())
|
||||
}
|
||||
|
||||
async fn client_key_models(
|
||||
&self,
|
||||
_: TimeRange,
|
||||
_: &gateway_core::policy::ClientApiKeyId,
|
||||
) -> AdminStoreResult<Vec<gateway_admin::model::observability::KeyModelRow>> {
|
||||
Ok(Default::default())
|
||||
}
|
||||
|
||||
async fn usage_trend(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
@@ -1923,6 +1960,8 @@ fn test_runtime_settings() -> RuntimeSettings {
|
||||
rotation_strategy: RotationStrategy::Smart,
|
||||
updated_at: Utc::now(),
|
||||
values: gateway_admin::model::settings::RuntimeSettingsValues {
|
||||
openai_oauth_guardian_review_model: true,
|
||||
openai_request_country: Default::default(),
|
||||
codex_privacy_policy: Default::default(),
|
||||
request_location_enabled: false,
|
||||
request_location: Default::default(),
|
||||
|
||||
@@ -917,7 +917,7 @@ pub(super) fn usage_record_with_account(
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn usage_route_should_forward_name_and_model_contains_filters() {
|
||||
async fn usage_route_should_forward_record_text_filters() {
|
||||
use axum::{
|
||||
body::Body,
|
||||
http::{Request, StatusCode, header},
|
||||
@@ -933,7 +933,7 @@ async fn usage_route_should_forward_name_and_model_contains_filters() {
|
||||
.with_state(fixture.state())
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri("/api/admin/usage/records?model=%20coding%20&clientApiKeyName=%20Key%20%25%20&accountName=%20Account%20Name%20")
|
||||
.uri("/api/admin/usage/records?model=%20coding%20&clientApiKeyName=%20Key%20%25%20&accountSearch=%20alice%40%20")
|
||||
.header(header::COOKIE, "cpr_session=valid-session")
|
||||
.header("x-request-id", "req_usage_other_outcome")
|
||||
.body(Body::empty())
|
||||
@@ -947,10 +947,7 @@ async fn usage_route_should_forward_name_and_model_contains_filters() {
|
||||
let filter = &observations.records.last().expect("usage query").filter;
|
||||
assert_eq!(filter.client_api_key_name.as_deref(), Some("Key %"));
|
||||
assert_eq!(filter.model_contains.as_deref(), Some("coding"));
|
||||
assert_eq!(
|
||||
filter.provider_account_name.as_deref(),
|
||||
Some("Account Name")
|
||||
);
|
||||
assert_eq!(filter.provider_account_search.as_deref(), Some("alice@"));
|
||||
assert!(filter.client_api_key_ref.is_none());
|
||||
assert!(filter.provider_account_ref.is_none());
|
||||
}
|
||||
@@ -1729,6 +1726,8 @@ async fn observability_queries_reject_group_filters_and_analytics_search() {
|
||||
"routingGroupRefs=group-a",
|
||||
),
|
||||
("/api/admin/usage/records/summary", "search=request-prefix"),
|
||||
("/api/admin/usage/records/summary", "errorOrigin=proxy"),
|
||||
("/api/admin/usage/insights/overview", "errorOrigin=proxy"),
|
||||
(
|
||||
"/api/admin/usage/insights/overview",
|
||||
"search=request-prefix",
|
||||
@@ -1823,27 +1822,50 @@ pub(super) fn ops_error_record() -> gateway_admin::model::observability::OpsErro
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn usage_status_query_accepts_three_modes_and_rejects_unknown_values() {
|
||||
async fn usage_record_query_accepts_error_sources_only_for_error_status() {
|
||||
use crate::admin::{AdminTestFixture, AdminTestState};
|
||||
use axum::{
|
||||
body::{Body, to_bytes},
|
||||
http::{Request, StatusCode, header},
|
||||
};
|
||||
use gateway_admin::model::observability::{UsageErrorOrigin, UsageRecordStatus};
|
||||
use gateway_api::admin::observability;
|
||||
use tower::ServiceExt as _;
|
||||
let fixture = AdminTestFixture::new().await;
|
||||
fixture.auth.insert_session("valid-session");
|
||||
for (status, expected) in [
|
||||
("success", StatusCode::OK),
|
||||
("error", StatusCode::OK),
|
||||
("all", StatusCode::OK),
|
||||
("failed", StatusCode::BAD_REQUEST),
|
||||
for (query, expected) in [
|
||||
("", Some(UsageRecordStatus::Success)),
|
||||
("status=success", Some(UsageRecordStatus::Success)),
|
||||
("status=error", Some(UsageRecordStatus::Error(None))),
|
||||
("status=all", Some(UsageRecordStatus::All)),
|
||||
(
|
||||
"status=error&errorOrigin=proxy",
|
||||
Some(UsageRecordStatus::Error(Some(UsageErrorOrigin::Proxy))),
|
||||
),
|
||||
(
|
||||
"status=error&errorOrigin=upstream",
|
||||
Some(UsageRecordStatus::Error(Some(UsageErrorOrigin::Upstream))),
|
||||
),
|
||||
(
|
||||
"status=error&errorOrigin=connection",
|
||||
Some(UsageRecordStatus::Error(Some(UsageErrorOrigin::Connection))),
|
||||
),
|
||||
(
|
||||
"status=error&errorOrigin=unknown",
|
||||
Some(UsageRecordStatus::Error(Some(UsageErrorOrigin::Unknown))),
|
||||
),
|
||||
("status=failed", None),
|
||||
("errorOrigin=proxy", None),
|
||||
("status=success&errorOrigin=proxy", None),
|
||||
("status=all&errorOrigin=unknown", None),
|
||||
("status=error&errorOrigin=other", None),
|
||||
("status=error&errorOrigin=", None),
|
||||
] {
|
||||
let response = observability::router::<AdminTestState>()
|
||||
.with_state(fixture.state())
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri(format!("/api/admin/usage/records?status={status}"))
|
||||
.uri(format!("/api/admin/usage/records?{query}"))
|
||||
.header(header::COOKIE, "cpr_session=valid-session")
|
||||
.header("x-request-id", "usage_status_fixture")
|
||||
.body(Body::empty())
|
||||
@@ -1855,17 +1877,15 @@ async fn usage_status_query_accepts_three_modes_and_rejects_unknown_values() {
|
||||
let body = to_bytes(response.into_body(), 1024 * 1024).await.unwrap();
|
||||
assert_eq!(
|
||||
actual,
|
||||
expected,
|
||||
"{status}: {}",
|
||||
if expected.is_some() {
|
||||
StatusCode::OK
|
||||
} else {
|
||||
StatusCode::BAD_REQUEST
|
||||
},
|
||||
"{query}: {}",
|
||||
String::from_utf8_lossy(&body)
|
||||
);
|
||||
if expected == StatusCode::OK {
|
||||
use gateway_admin::model::observability::UsageRecordStatus;
|
||||
let status_value = match status {
|
||||
"success" => UsageRecordStatus::Success,
|
||||
"error" => UsageRecordStatus::Error,
|
||||
_ => UsageRecordStatus::All,
|
||||
};
|
||||
if let Some(status_value) = expected {
|
||||
assert_eq!(
|
||||
fixture
|
||||
.observations
|
||||
@@ -2019,16 +2039,15 @@ async fn dashboard_summary_identifies_trend_metrics_and_account_usage_values() {
|
||||
assert_eq!(status, StatusCode::OK, "{kind}: {value}");
|
||||
let accounts = value["data"]["accountUsage"].as_array().unwrap();
|
||||
assert_eq!(accounts.len(), 5);
|
||||
for (account, (metric, raw, display, last_used_at)) in accounts.iter().zip([
|
||||
("tokens", Some(2_000), "2K", Some(now)),
|
||||
("tokens", Some(0), "0", None),
|
||||
("tokens", None, "—", None),
|
||||
("requests", Some(9), "9", Some(now)),
|
||||
("requests", Some(0), "—", None),
|
||||
for (account, (metric, raw, last_used_at)) in accounts.iter().zip([
|
||||
("tokens", Some(2_000), Some(now)),
|
||||
("tokens", Some(0), None),
|
||||
("tokens", None, None),
|
||||
("requests", Some(9), Some(now)),
|
||||
("requests", Some(0), None),
|
||||
]) {
|
||||
assert_eq!(account["metric"], metric);
|
||||
assert_eq!(account["metricValueRaw"], json!(raw));
|
||||
assert_eq!(account["metricValue"], display);
|
||||
assert_eq!(account["lastUsedAt"], json!(last_used_at));
|
||||
}
|
||||
let summary = value["data"]["trend"]["summary"].as_array().unwrap();
|
||||
@@ -2062,50 +2081,42 @@ async fn dashboard_relative_time_preserves_server_anchor_boundaries_and_deployme
|
||||
("Asia/Kathmandu", "2026-06-15 05:45:01"),
|
||||
("America/New_York", "2026-06-14 20:00:01"),
|
||||
] {
|
||||
for (elapsed_ms, expected, legacy) in [
|
||||
(None, json!(null), "—"),
|
||||
for (elapsed_ms, expected) in [
|
||||
(None, json!(null)),
|
||||
(
|
||||
Some(-1_000),
|
||||
json!({ "kind": "absolute", "value": future_display }),
|
||||
future_display,
|
||||
),
|
||||
(Some(-1), json!({ "kind": "just_now" }), "刚刚"),
|
||||
(Some(0), json!({ "kind": "just_now" }), "刚刚"),
|
||||
(Some(59_999), json!({ "kind": "just_now" }), "刚刚"),
|
||||
(Some(-1), json!({ "kind": "just_now" })),
|
||||
(Some(0), json!({ "kind": "just_now" })),
|
||||
(Some(59_999), json!({ "kind": "just_now" })),
|
||||
(
|
||||
Some(60_000),
|
||||
json!({ "kind": "elapsed", "value": 1, "unit": "minute" }),
|
||||
"1 分钟前",
|
||||
),
|
||||
(
|
||||
Some(119_999),
|
||||
json!({ "kind": "elapsed", "value": 1, "unit": "minute" }),
|
||||
"1 分钟前",
|
||||
),
|
||||
(
|
||||
Some(3_599_999),
|
||||
json!({ "kind": "elapsed", "value": 59, "unit": "minute" }),
|
||||
"59 分钟前",
|
||||
),
|
||||
(
|
||||
Some(3_600_000),
|
||||
json!({ "kind": "elapsed", "value": 1, "unit": "hour" }),
|
||||
"1 小时前",
|
||||
),
|
||||
(
|
||||
Some(86_399_999),
|
||||
json!({ "kind": "elapsed", "value": 23, "unit": "hour" }),
|
||||
"23 小时前",
|
||||
),
|
||||
(
|
||||
Some(86_400_000),
|
||||
json!({ "kind": "elapsed", "value": 1, "unit": "day" }),
|
||||
"1 天前",
|
||||
),
|
||||
(
|
||||
Some(34_560_000_000),
|
||||
json!({ "kind": "elapsed", "value": 400, "unit": "day" }),
|
||||
"400 天前",
|
||||
),
|
||||
] {
|
||||
// 每个边界使用独立缓存,避免相同锚点复用上一轮观测数据
|
||||
@@ -2177,7 +2188,6 @@ async fn dashboard_relative_time_preserves_server_anchor_boundaries_and_deployme
|
||||
account["lastUsedRelative"], expected,
|
||||
"{zone}: {elapsed_ms:?}"
|
||||
);
|
||||
assert_eq!(account["lastUsed"], legacy, "{zone}: {elapsed_ms:?}");
|
||||
assert_eq!(account["lastUsedAt"], json!(last_used_at));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +40,8 @@ async fn response_json(response: axum::response::Response) -> Value {
|
||||
fn update_body() -> Value {
|
||||
json!({
|
||||
"configRevision": 7,
|
||||
"openaiOauthGuardianReviewModel": true,
|
||||
"openaiRequestCountry": {"mode": "follow_location"},
|
||||
"codexPrivacyPolicy": {"enabled":false,"onError":"skip_rule","rules":[]},
|
||||
"requestLocationEnabled": false,
|
||||
"requestLocation": {"country":"US", "region":"Ohio", "city":"Piketon", "timezone":"America/New_York"},
|
||||
@@ -73,6 +75,7 @@ fn update_body() -> Value {
|
||||
"accountWarmupEnabled": false,
|
||||
"accountWarmupCron": "0 8 * * *",
|
||||
"accountWarmupModel": null,
|
||||
"resetCreditOnLowWeeklyQuota": false,
|
||||
"resetCreditLeadMinutes": 10,
|
||||
"resetCreditMaxWeeklyRemainingPercent": null
|
||||
})
|
||||
@@ -431,7 +434,7 @@ fn settings_request_accepts_unlimited_default_account_concurrency() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn settings_request_validates_cron_and_allows_disabled_empty_schedule() {
|
||||
fn warmup_cron_is_required_and_may_be_empty_only_when_disabled() {
|
||||
for (enabled, cron, valid) in [
|
||||
(false, "", true),
|
||||
(true, "", false),
|
||||
@@ -461,10 +464,9 @@ fn settings_request_validates_cron_and_allows_disabled_empty_schedule() {
|
||||
let request: UpdateRuntimeSettingsRequest = serde_json::from_value(oversized).unwrap();
|
||||
assert_eq!(request.validate().unwrap_err().field(), "accountWarmupCron");
|
||||
|
||||
let mut legacy = update_body();
|
||||
legacy.as_object_mut().unwrap().remove("accountWarmupCron");
|
||||
legacy["accountWarmupScheduleTime"] = json!("08:00");
|
||||
assert!(serde_json::from_value::<UpdateRuntimeSettingsRequest>(legacy).is_err());
|
||||
let mut missing = update_body();
|
||||
missing.as_object_mut().unwrap().remove("accountWarmupCron");
|
||||
assert!(serde_json::from_value::<UpdateRuntimeSettingsRequest>(missing).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -537,6 +539,17 @@ fn settings_service_contract_flattens_values_and_rejects_unknown_fields() {
|
||||
serde_json::from_value::<ReplaceRuntimeSettings>(wire.clone()).unwrap(),
|
||||
command
|
||||
);
|
||||
for field in [
|
||||
"openai_oauth_guardian_review_model",
|
||||
"openai_request_country",
|
||||
"reset_credit_on_low_weekly_quota",
|
||||
] {
|
||||
let mut incomplete = wire.clone();
|
||||
incomplete.as_object_mut().unwrap().remove(field);
|
||||
assert!(serde_json::from_value::<ReplaceRuntimeSettings>(incomplete.clone()).is_err());
|
||||
incomplete[field] = Value::Null;
|
||||
assert!(serde_json::from_value::<ReplaceRuntimeSettings>(incomplete).is_err());
|
||||
}
|
||||
wire["unexpected_setting"] = json!(true);
|
||||
assert!(serde_json::from_value::<ReplaceRuntimeSettings>(wire).is_err());
|
||||
}
|
||||
@@ -705,8 +718,6 @@ async fn admin_auth_should_accept_a_configured_request_id_header_name() {
|
||||
|
||||
let fixture = AdminTestFixture::new().await;
|
||||
fixture.auth.insert_session("valid-session");
|
||||
// 部署把 api.request_id_header 改名后,注入的 header 不再叫 x-request-id;
|
||||
// 管理请求仍须拿到请求上下文,而不是退化为 500
|
||||
let custom = HeaderName::from_static("x-trace-id");
|
||||
let app = app(fixture.state()).layer(SetRequestIdLayer::new(custom, MakeRequestUuid));
|
||||
let unlabelled = Request::builder()
|
||||
@@ -905,33 +916,19 @@ async fn settings_update_rejects_unknown_fields_without_writing() {
|
||||
assert_eq!(*fixture.settings.settings.lock().unwrap(), initial);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn global_profile_can_be_omitted_but_cannot_be_cleared() {
|
||||
for field in ["openaiClientProfile", "xaiClientProfile"] {
|
||||
let mut body = update_body();
|
||||
body.as_object_mut().unwrap().remove(field);
|
||||
assert!(serde_json::from_value::<UpdateRuntimeSettingsRequest>(body.clone()).is_ok());
|
||||
body[field] = json!(null);
|
||||
assert!(serde_json::from_value::<UpdateRuntimeSettingsRequest>(body.clone()).is_err());
|
||||
body[field] = json!({"versionMode":"latest"});
|
||||
assert!(serde_json::from_value::<UpdateRuntimeSettingsRequest>(body).is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn generic_global_profiles_decode_native_providers_and_reject_legacy_conflicts() {
|
||||
let mut body = update_body();
|
||||
body["providerRequestProfiles"] = json!({
|
||||
"openai":{"preset":"desktop"},
|
||||
"xai":{"preset":"managed"},
|
||||
});
|
||||
body["openaiClientProfile"] = json!({"preset":"desktop"});
|
||||
let decoded = serde_json::from_value::<UpdateRuntimeSettingsRequest>(body.clone()).unwrap();
|
||||
assert!(decoded.provider_request_profiles.contains_key("xai"));
|
||||
|
||||
body["openaiClientProfile"] = json!({"preset":"cli"});
|
||||
async fn global_profiles_map_only_updates_submitted_providers() {
|
||||
let fixture = AdminTestFixture::new().await;
|
||||
fixture.auth.insert_session("valid-session");
|
||||
{
|
||||
let mut settings = fixture.settings.settings.lock().unwrap();
|
||||
settings.request_profiles = serde_json::from_value(json!({
|
||||
"openai":{"preset":"desktop"}, "xai":{"versionMode":"latest"}
|
||||
}))
|
||||
.unwrap();
|
||||
}
|
||||
let mut body = update_body();
|
||||
body["providerRequestProfiles"] = json!({"xai":null});
|
||||
let response = app(fixture.state())
|
||||
.oneshot(request(
|
||||
Method::POST,
|
||||
@@ -940,7 +937,12 @@ async fn generic_global_profiles_decode_native_providers_and_reject_legacy_confl
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let saved = response_json(response).await;
|
||||
assert_eq!(
|
||||
saved["data"]["providerRequestProfiles"],
|
||||
json!({"openai":{"preset":"desktop"}})
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -1419,6 +1421,99 @@ async fn settings_update_rejects_a_stale_version_without_replacing_the_saved_val
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn runtime_settings_reject_incomplete_and_stale_replacements() {
|
||||
let fixture = AdminTestFixture::new().await;
|
||||
fixture.auth.insert_session("valid-session");
|
||||
let read = || app(fixture.state()).oneshot(request(Method::GET, "/api/admin/settings", None));
|
||||
let save = |body| {
|
||||
app(fixture.state()).oneshot(request(
|
||||
Method::POST,
|
||||
"/api/admin/settings/update",
|
||||
Some(body),
|
||||
))
|
||||
};
|
||||
let initial = response_json(read().await.unwrap()).await["data"].clone();
|
||||
assert_eq!(initial["openaiOauthGuardianReviewModel"], true);
|
||||
assert_eq!(
|
||||
initial["openaiRequestCountry"],
|
||||
json!({"mode":"follow_location"})
|
||||
);
|
||||
let mut body = update_body();
|
||||
body["configRevision"] = initial["configRevision"].clone();
|
||||
body["openaiOauthGuardianReviewModel"] = json!(false);
|
||||
body["openaiRequestCountry"] = json!({"mode":"custom","country":"CA"});
|
||||
body["resetCreditOnLowWeeklyQuota"] = json!(true);
|
||||
let response = save(body.clone()).await.unwrap();
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let saved = response_json(response).await["data"].clone();
|
||||
assert_eq!(saved["resetCreditOnLowWeeklyQuota"], true);
|
||||
assert_eq!(saved["openaiOauthGuardianReviewModel"], false);
|
||||
assert_eq!(saved["openaiRequestCountry"], body["openaiRequestCountry"]);
|
||||
|
||||
for field in [
|
||||
"openaiOauthGuardianReviewModel",
|
||||
"openaiRequestCountry",
|
||||
"resetCreditOnLowWeeklyQuota",
|
||||
] {
|
||||
let mut incomplete = update_body();
|
||||
incomplete["configRevision"] = saved["configRevision"].clone();
|
||||
incomplete.as_object_mut().unwrap().remove(field);
|
||||
assert_eq!(
|
||||
save(incomplete.clone()).await.unwrap().status(),
|
||||
StatusCode::UNPROCESSABLE_ENTITY
|
||||
);
|
||||
incomplete[field] = Value::Null;
|
||||
assert_eq!(
|
||||
save(incomplete).await.unwrap().status(),
|
||||
StatusCode::UNPROCESSABLE_ENTITY
|
||||
);
|
||||
}
|
||||
body["openaiOauthGuardianReviewModel"] = json!(true);
|
||||
body["openaiRequestCountry"] = json!({"mode":"passthrough"});
|
||||
assert_eq!(
|
||||
save(body.clone()).await.unwrap().status(),
|
||||
StatusCode::CONFLICT
|
||||
);
|
||||
let current = response_json(read().await.unwrap()).await["data"].clone();
|
||||
assert_eq!(current, saved);
|
||||
body["configRevision"] = current["configRevision"].clone();
|
||||
let response = save(body).await.unwrap();
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let changed = response_json(response).await["data"].clone();
|
||||
assert_eq!(changed["openaiOauthGuardianReviewModel"], true);
|
||||
assert_eq!(
|
||||
changed["openaiRequestCountry"],
|
||||
json!({"mode":"passthrough"})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn openai_request_settings_validate_modes_and_country_codes() {
|
||||
for invalid in [
|
||||
json!(null),
|
||||
json!({"mode":"custom"}),
|
||||
json!({"mode":"custom","country":"ca"}),
|
||||
json!({"mode":"custom","country":"USA"}),
|
||||
json!({"mode":"custom","country":"国"}),
|
||||
json!({"mode":"follow_location","country":"CA"}),
|
||||
json!({"mode":"passthrough","country":"CA"}),
|
||||
json!({"mode":"default","country":"CA"}),
|
||||
json!({"mode":"unknown"}),
|
||||
] {
|
||||
let mut body = update_body();
|
||||
body["openaiRequestCountry"] = invalid.clone();
|
||||
if let Ok(request) = serde_json::from_value::<UpdateRuntimeSettingsRequest>(body) {
|
||||
assert!(request.validate().is_err(), "{invalid}");
|
||||
}
|
||||
}
|
||||
for invalid in [json!(null), json!("true"), json!(1)] {
|
||||
let mut body = update_body();
|
||||
body["openaiOauthGuardianReviewModel"] = invalid;
|
||||
assert!(serde_json::from_value::<UpdateRuntimeSettingsRequest>(body).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn guardian_reservation_round_trips_and_rejects_invalid_values() {
|
||||
let fixture = AdminTestFixture::new().await;
|
||||
|
||||
@@ -198,7 +198,7 @@ mod response {
|
||||
response_json(generic).await,
|
||||
json!({
|
||||
"code": 42902,
|
||||
"message": "请求过于频繁,请稍后重试",
|
||||
"message": "请求过于频繁",
|
||||
"data": null
|
||||
})
|
||||
);
|
||||
@@ -212,7 +212,7 @@ mod response {
|
||||
response_json(login).await,
|
||||
json!({
|
||||
"code": 42901,
|
||||
"message": "登录尝试过多,请稍后重试",
|
||||
"message": "登录尝试过多",
|
||||
"data": null
|
||||
})
|
||||
);
|
||||
|
||||
@@ -345,7 +345,7 @@ async fn records_keep_pagination_and_hide_admin_and_upstream_data() {
|
||||
assert_eq!(data.records.len(), 3);
|
||||
for (query, status) in data.records.iter().zip([
|
||||
UsageRecordStatus::Success,
|
||||
UsageRecordStatus::Error,
|
||||
UsageRecordStatus::Error(None),
|
||||
UsageRecordStatus::All,
|
||||
]) {
|
||||
assert_eq!(query.status, status);
|
||||
|
||||
@@ -228,6 +228,46 @@ fn desktop_headers_should_take_precedence_over_embedded_cli_marker() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn desktop_hosted_tui_should_use_core_unless_a_desktop_version_is_supplied() {
|
||||
for (core, version_header, kind, version) in [
|
||||
("0.170.0", None, CodexClientKind::Cli, Some("0.170.0")),
|
||||
(
|
||||
"0.170.0",
|
||||
Some("0.170.0"),
|
||||
CodexClientKind::Cli,
|
||||
Some("0.170.0"),
|
||||
),
|
||||
("invalid", None, CodexClientKind::Cli, None),
|
||||
(
|
||||
"0.170.0",
|
||||
Some("26.1023.51421"),
|
||||
CodexClientKind::Desktop,
|
||||
Some("26.1023.51421"),
|
||||
),
|
||||
("0.170.0", Some("invalid"), CodexClientKind::Desktop, None),
|
||||
] {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert("originator", HeaderValue::from_static("Codex Desktop"));
|
||||
headers.insert(
|
||||
"user-agent",
|
||||
HeaderValue::from_str(&format!(
|
||||
"Codex Desktop/{core} (Ubuntu 26.4.0; aarch64) xterm-256color (codex-tui; 0.169.0)"
|
||||
))
|
||||
.unwrap(),
|
||||
);
|
||||
if let Some(value) = version_header {
|
||||
headers.insert("version", HeaderValue::from_static(value));
|
||||
}
|
||||
let client = identify_codex_client(&headers).unwrap();
|
||||
assert_eq!(client.kind(), kind);
|
||||
assert_eq!(
|
||||
client.version().map(ToString::to_string).as_deref(),
|
||||
version
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn official_core_clients_should_use_the_core_version_instead_of_the_app_suffix() {
|
||||
for product in [
|
||||
@@ -322,7 +362,7 @@ fn chatgpt_remote_desktop_without_app_version_should_not_use_a_version_gate() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chatgpt_remote_marker_should_require_a_complete_exact_client_suffix() {
|
||||
fn desktop_should_require_a_complete_exact_client_suffix() {
|
||||
for suffix in [
|
||||
"(codex_chatgpt_android_remote_extra; dev)",
|
||||
"(unofficial_codex_chatgpt_ios_remote; dev)",
|
||||
@@ -340,6 +380,9 @@ fn chatgpt_remote_marker_should_require_a_complete_exact_client_suffix() {
|
||||
"(Codex Desktop; ) (codex_chatgpt_android_remote; dev)",
|
||||
"(Codex Desktop; invalid) (codex_chatgpt_ios_remote; dev)",
|
||||
"(Codex Desktop)",
|
||||
"(codex-tui-custom; 0.169.0)",
|
||||
"(codex-tui; 0.169.0) trailing",
|
||||
"(codex-tui; )",
|
||||
] {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
@@ -395,19 +438,23 @@ fn chatgpt_remote_marker_should_not_override_an_existing_desktop_version_suffix(
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chatgpt_remote_suffix_created_by_header_truncation_should_not_skip_the_gate() {
|
||||
let suffix = " (codex_chatgpt_android_remote; dev)";
|
||||
let mut user_agent = "Codex Desktop/0.154.0-alpha.6.2 ".to_owned();
|
||||
user_agent.push_str(&"x".repeat(4096 - user_agent.len() - suffix.len()));
|
||||
user_agent.push_str(suffix);
|
||||
user_agent.push_str(" (another_client; dev)");
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert("user-agent", HeaderValue::from_str(&user_agent).unwrap());
|
||||
fn truncated_client_suffix_should_not_change_the_desktop_gate() {
|
||||
for suffix in [
|
||||
" (codex_chatgpt_android_remote; dev)",
|
||||
" (codex-tui; 0.169.0)",
|
||||
] {
|
||||
let mut user_agent = "Codex Desktop/0.154.0-alpha.6.2 ".to_owned();
|
||||
user_agent.push_str(&"x".repeat(4096 - user_agent.len() - suffix.len()));
|
||||
user_agent.push_str(suffix);
|
||||
user_agent.push_str(" (another_client; dev)");
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert("user-agent", HeaderValue::from_str(&user_agent).unwrap());
|
||||
|
||||
let client =
|
||||
identify_codex_client(&headers).expect("incomplete headers still require a version");
|
||||
assert_eq!(client.kind(), CodexClientKind::Desktop);
|
||||
assert!(client.version().is_none());
|
||||
let client =
|
||||
identify_codex_client(&headers).expect("incomplete headers still require a version");
|
||||
assert_eq!(client.kind(), CodexClientKind::Desktop);
|
||||
assert!(client.version().is_none());
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
+61
-15
@@ -1,4 +1,4 @@
|
||||
//! 真实 HTTP / WebSocket 路由与 Core 执行入口共同拒绝生图,不触发准入或上游
|
||||
//! 真实 HTTP / WebSocket 路由与 Core 执行入口共同执行分组功能授权,不触发准入或上游
|
||||
|
||||
use super::*;
|
||||
use axum::{
|
||||
@@ -11,14 +11,15 @@ use std::{sync::Arc, time::Duration};
|
||||
use tokio_tungstenite::tungstenite::{Message, client::IntoClientRequest};
|
||||
use tower::ServiceExt;
|
||||
|
||||
fn execution() -> Arc<DefaultExecutionService> {
|
||||
pub(super) fn execution() -> Arc<DefaultExecutionService> {
|
||||
Arc::new(DefaultExecutionService::new(
|
||||
RuntimeSnapshotHandle::new(snapshot_with_image_policy(
|
||||
"sk_image_policy",
|
||||
RuntimeSnapshotHandle::new(snapshot_with_group_permissions(
|
||||
"sk_group_permissions",
|
||||
"openai",
|
||||
"key_image_policy",
|
||||
"key_group_permissions",
|
||||
&["acct_api_test"],
|
||||
false,
|
||||
false,
|
||||
)),
|
||||
Arc::new(UnusedExecutionStore),
|
||||
ProviderRegistry::default(),
|
||||
@@ -54,7 +55,7 @@ async fn denied_image_requests_return_403_before_admission_or_upstream() {
|
||||
.clone()
|
||||
.oneshot(
|
||||
Request::post(path)
|
||||
.header(AUTHORIZATION, "Bearer sk_image_policy")
|
||||
.header(AUTHORIZATION, "Bearer sk_group_permissions")
|
||||
.header("content-type", "application/json")
|
||||
.body(Body::from(body.to_string()))
|
||||
.unwrap(),
|
||||
@@ -71,7 +72,7 @@ async fn denied_image_requests_return_403_before_admission_or_upstream() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn each_websocket_create_enforces_image_policy() {
|
||||
async fn each_websocket_create_enforces_group_permissions() {
|
||||
let app = api_router(execution()).await;
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
@@ -81,18 +82,33 @@ async fn each_websocket_create_enforces_image_policy() {
|
||||
let mut request = format!("ws://{address}/v1/responses")
|
||||
.into_client_request()
|
||||
.unwrap();
|
||||
request
|
||||
.headers_mut()
|
||||
.insert(AUTHORIZATION, "Bearer sk_image_policy".parse().unwrap());
|
||||
request.headers_mut().insert(
|
||||
AUTHORIZATION,
|
||||
"Bearer sk_group_permissions".parse().unwrap(),
|
||||
);
|
||||
let (mut socket, _) = tokio_tungstenite::connect_async(request).await.unwrap();
|
||||
for image_fields in [
|
||||
json!({"tools":[{"type":"image_generation"}]}),
|
||||
json!({"tool_choice":{"type":"image_generation"}}),
|
||||
for (fields, code) in [
|
||||
(
|
||||
json!({"tools":[{"type":"image_generation"}]}),
|
||||
"image_generation_disabled",
|
||||
),
|
||||
(
|
||||
json!({"client_metadata":{"x-codex-turn-metadata":r#"{"subagent_kind":"thread_spawn"}"#}}),
|
||||
"subagents_disabled",
|
||||
),
|
||||
(
|
||||
json!({"tool_choice":{"type":"image_generation"}}),
|
||||
"image_generation_disabled",
|
||||
),
|
||||
(
|
||||
json!({"client_metadata":{"x-openai-subagent":"collab_spawn"}}),
|
||||
"subagents_disabled",
|
||||
),
|
||||
] {
|
||||
let mut body = json!({"type":"response.create","model":"model-a","input":"test"});
|
||||
body.as_object_mut()
|
||||
.unwrap()
|
||||
.extend(image_fields.as_object().unwrap().clone());
|
||||
.extend(fields.as_object().unwrap().clone());
|
||||
socket
|
||||
.send(Message::Text(body.to_string().into()))
|
||||
.await
|
||||
@@ -105,8 +121,38 @@ async fn each_websocket_create_enforces_image_policy() {
|
||||
let body: Value = serde_json::from_str(frame.to_text().unwrap()).unwrap();
|
||||
assert_eq!(body["type"], "error", "{body}");
|
||||
assert_eq!(body["status"], 403);
|
||||
assert_eq!(body["error"]["code"], "image_generation_disabled");
|
||||
assert_eq!(body["error"]["code"], code);
|
||||
}
|
||||
socket.close(None).await.unwrap();
|
||||
server.abort();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn collaboration_requests_are_denied_before_admission_or_upstream() {
|
||||
let app = api_router(execution()).await;
|
||||
for (header, value) in [
|
||||
("x-openai-subagent", "collab_spawn"),
|
||||
(
|
||||
"x-codex-turn-metadata",
|
||||
r#"{"subagent_kind":"thread_spawn"}"#,
|
||||
),
|
||||
] {
|
||||
let response = app
|
||||
.clone()
|
||||
.oneshot(
|
||||
Request::post("/v1/responses")
|
||||
.header(AUTHORIZATION, "Bearer sk_group_permissions")
|
||||
.header("content-type", "application/json")
|
||||
.header(header, value)
|
||||
.body(Body::from(r#"{"model":"model-a","input":"test"}"#))
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(response.status(), StatusCode::FORBIDDEN);
|
||||
let body: Value =
|
||||
serde_json::from_slice(&to_bytes(response.into_body(), usize::MAX).await.unwrap())
|
||||
.unwrap();
|
||||
assert_eq!(body["error"]["code"], "subagents_disabled");
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ mod auth;
|
||||
mod chat_completions;
|
||||
mod endpoint;
|
||||
mod error;
|
||||
mod image_policy;
|
||||
mod group_permissions;
|
||||
mod images;
|
||||
mod live;
|
||||
mod messages;
|
||||
@@ -300,15 +300,23 @@ fn snapshot_with_accounts(
|
||||
client_key_id: &str,
|
||||
accounts: &[&str],
|
||||
) -> RuntimeSnapshot {
|
||||
snapshot_with_image_policy(plaintext, provider_name, client_key_id, accounts, true)
|
||||
snapshot_with_group_permissions(
|
||||
plaintext,
|
||||
provider_name,
|
||||
client_key_id,
|
||||
accounts,
|
||||
true,
|
||||
true,
|
||||
)
|
||||
}
|
||||
|
||||
fn snapshot_with_image_policy(
|
||||
fn snapshot_with_group_permissions(
|
||||
plaintext: &str,
|
||||
provider_name: &str,
|
||||
client_key_id: &str,
|
||||
accounts: &[&str],
|
||||
allow_image_generation: bool,
|
||||
allow_subagents: bool,
|
||||
) -> RuntimeSnapshot {
|
||||
let provider = ProviderKind::new(provider_name).expect("provider");
|
||||
let account_directory = Arc::new(RuntimeAccountDirectory::new(BTreeMap::from_iter(
|
||||
@@ -345,7 +353,8 @@ fn snapshot_with_image_policy(
|
||||
Arc::clone(&account_directory),
|
||||
ClientRoutingScope::all_accounts(),
|
||||
)
|
||||
.with_image_generation(allow_image_generation),
|
||||
.with_image_generation(allow_image_generation)
|
||||
.with_subagents(allow_subagents),
|
||||
),
|
||||
true,
|
||||
RateLimits::unlimited(),
|
||||
|
||||
@@ -406,13 +406,13 @@ async fn model_query_middleware_short_circuit_sets_json_content_type_without_cal
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn native_catalog_preserves_complete_objects_and_only_rewrites_alias_slug() {
|
||||
async fn native_catalog_preserves_upstream_fields_except_alias_and_group_subagent_policy() {
|
||||
let original = serde_json::json!({
|
||||
"slug": "gpt-native", "display_name": "Official name", "priority": 47,
|
||||
"base_instructions": "Official instructions\nDo not replace.",
|
||||
"model_messages": {"token_budget": "upstream template", "future_section": [null, {"a": true}]},
|
||||
"service_tiers": [{"id": "priority", "name": "Fast", "description": "Official speed tier"}],
|
||||
"tool_mode": "code_mode_only", "use_responses_lite": true,
|
||||
"tool_mode": "code_mode_only", "use_responses_lite": true, "multi_agent_version": "v2",
|
||||
"supports_reasoning_summary_parameter": true, "default_reasoning_summary": "detailed",
|
||||
"future_field": {"nested": [1, null, "value"]}, "explicit_null": null
|
||||
});
|
||||
@@ -424,35 +424,49 @@ async fn native_catalog_preserves_complete_objects_and_only_rewrites_alias_slug(
|
||||
)
|
||||
.expect("payload"),
|
||||
};
|
||||
let execution = Arc::new(ModelsExecution {
|
||||
client: authenticated_client("sk_models_test"),
|
||||
presentation: None,
|
||||
native: Some(Ok(vec![entry("gpt-native"), entry("my-alias")])),
|
||||
requested_versions: Mutex::default(),
|
||||
middleware: None,
|
||||
});
|
||||
let response = api_router(execution.clone())
|
||||
.await
|
||||
.oneshot(authorized_request(
|
||||
"/v1/models?client_version=0.154.0-alpha.3",
|
||||
))
|
||||
.await
|
||||
.expect("response");
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
assert_eq!(response.headers()["cache-control"], "private, no-store");
|
||||
assert!(response.headers().get("etag").is_none());
|
||||
let body = to_bytes(response.into_body(), usize::MAX)
|
||||
.await
|
||||
.expect("body");
|
||||
let value: serde_json::Value = serde_json::from_slice(&body).expect("JSON");
|
||||
assert_eq!(value["models"][0], original);
|
||||
let mut alias = original;
|
||||
alias["slug"] = serde_json::json!("my-alias");
|
||||
assert_eq!(value["models"][1], alias);
|
||||
assert_eq!(
|
||||
*execution.requested_versions.lock().expect("versions"),
|
||||
["0.154.0-alpha.3"]
|
||||
);
|
||||
for (allowed, client) in [
|
||||
(true, authenticated_client("sk_models_test")),
|
||||
(
|
||||
false,
|
||||
super::group_permissions::execution()
|
||||
.authenticate("sk_group_permissions")
|
||||
.unwrap(),
|
||||
),
|
||||
] {
|
||||
let execution = Arc::new(ModelsExecution {
|
||||
client,
|
||||
presentation: None,
|
||||
native: Some(Ok(vec![entry("gpt-native"), entry("my-alias")])),
|
||||
requested_versions: Mutex::default(),
|
||||
middleware: None,
|
||||
});
|
||||
let response = api_router(execution.clone())
|
||||
.await
|
||||
.oneshot(authorized_request(
|
||||
"/v1/models?client_version=0.154.0-alpha.3",
|
||||
))
|
||||
.await
|
||||
.expect("response");
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
assert_eq!(response.headers()["cache-control"], "private, no-store");
|
||||
assert!(response.headers().get("etag").is_none());
|
||||
let body = to_bytes(response.into_body(), usize::MAX)
|
||||
.await
|
||||
.expect("body");
|
||||
let value: serde_json::Value = serde_json::from_slice(&body).expect("JSON");
|
||||
let mut expected = original.clone();
|
||||
if !allowed {
|
||||
expected["multi_agent_version"] = serde_json::json!("disabled");
|
||||
}
|
||||
assert_eq!(value["models"][0], expected);
|
||||
let mut alias = expected;
|
||||
alias["slug"] = serde_json::json!("my-alias");
|
||||
assert_eq!(value["models"][1], alias);
|
||||
assert_eq!(
|
||||
*execution.requested_versions.lock().expect("versions"),
|
||||
["0.154.0-alpha.3"]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
@@ -743,7 +743,7 @@ async fn request_context_should_resolve_forwarded_precedence_and_peer_fallback()
|
||||
operation_kind: OperationKind::Generate,
|
||||
input: Some(json!("hello")),
|
||||
client_metadata: None,
|
||||
// 客户端 User-Agent 仅用于本地展示,不再透传给上游指纹上下文
|
||||
// 客户端 User-Agent 仅用于本地展示
|
||||
protocol_context: Some(json!({"opaque_request_headers": [
|
||||
["cf-connecting-ip", STANDARD.encode(b"198.51.100.1")],
|
||||
["x-real-ip", STANDARD.encode(b"198.51.100.2")],
|
||||
@@ -1378,27 +1378,40 @@ async fn streaming_error_after_initial_keepalive_should_be_a_terminal_sse_failur
|
||||
}
|
||||
|
||||
#[tokio::test(start_paused = true)]
|
||||
async fn streaming_late_upstream_error_should_preserve_extended_error_fields() {
|
||||
async fn streaming_bad_request_preserves_extended_error_fields_before_and_after_keepalive() {
|
||||
let mut error = json!({
|
||||
"type": "invalid_request_error", "code": "misalignment_policy_violation",
|
||||
"message": "synthetic policy error", "misalignment": {"category": "synthetic"},
|
||||
"extension": {"opaque": true}
|
||||
});
|
||||
let failure = ProviderError::new(ProviderErrorKind::InvalidRequest, UpstreamSendState::Sent)
|
||||
.with_client_visible_upstream_response(
|
||||
ClientVisibleUpstreamResponse::new(
|
||||
400,
|
||||
Some(b"application/json".to_vec()),
|
||||
Bytes::from(json!({"error": error}).to_string()),
|
||||
let failure = || {
|
||||
ProviderError::new(ProviderErrorKind::InvalidRequest, UpstreamSendState::Sent)
|
||||
.with_client_visible_upstream_response(
|
||||
ClientVisibleUpstreamResponse::new(
|
||||
400,
|
||||
Some(b"application/json".to_vec()),
|
||||
Bytes::from(json!({"error": error}).to_string()),
|
||||
)
|
||||
.with_headers(headers(&[("retry-after", "7")])),
|
||||
)
|
||||
.with_headers(headers(&[("retry-after", "7")])),
|
||||
);
|
||||
error["headers"] = json!({"retry-after": "7"});
|
||||
};
|
||||
let immediate = FakeSession::streaming(
|
||||
Arc::new(Trace::default()),
|
||||
vec![NextStep::Error(EngineError::Provider(failure()))],
|
||||
);
|
||||
let response = stream_execution_response(Box::new(immediate), None).await;
|
||||
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
|
||||
assert_eq!(response.headers()[CONTENT_TYPE], "application/json");
|
||||
let bytes = to_bytes(response.into_body(), usize::MAX).await.unwrap();
|
||||
assert_eq!(
|
||||
serde_json::from_slice::<Value>(&bytes).unwrap(),
|
||||
json!({"error": error})
|
||||
);
|
||||
let session = FakeSession::streaming(
|
||||
Arc::new(Trace::default()),
|
||||
vec![NextStep::DelayedError(
|
||||
Duration::from_secs(30),
|
||||
EngineError::Provider(failure),
|
||||
EngineError::Provider(failure()),
|
||||
)],
|
||||
);
|
||||
let response = stream_execution_response(Box::new(session), None).await;
|
||||
@@ -1410,6 +1423,7 @@ async fn streaming_late_upstream_error_should_preserve_extended_error_fields() {
|
||||
.find(|event| event.event.as_deref() == Some("response.failed"))
|
||||
.unwrap();
|
||||
let payload: Value = serde_json::from_str(&failed.data).unwrap();
|
||||
error["headers"] = json!({"retry-after": "7"});
|
||||
assert_eq!(payload["response"]["error"], error);
|
||||
assert!(bytes.ends_with(b"data: [DONE]\n\n"));
|
||||
}
|
||||
@@ -2954,20 +2968,21 @@ struct SessionExecution {
|
||||
#[tokio::test]
|
||||
async fn official_core_clients_should_be_version_checked_before_starting_responses() {
|
||||
let admin = crate::admin::AdminTestFixture::new().await.services;
|
||||
for product in [
|
||||
"codex-tui",
|
||||
"codex_exec",
|
||||
"codex_vscode",
|
||||
"codex_sdk_ts",
|
||||
"codex_python_sdk",
|
||||
for (product, client) in [
|
||||
("codex-tui", "codex-tui"),
|
||||
("codex_exec", "codex_exec"),
|
||||
("codex_vscode", "codex_vscode"),
|
||||
("codex_sdk_ts", "codex_sdk_ts"),
|
||||
("codex_python_sdk", "codex_python_sdk"),
|
||||
("Codex Desktop", "codex-tui"),
|
||||
] {
|
||||
for (version, allowed) in [("0.158.0", false), ("0.162.0", true)] {
|
||||
for (version, allowed) in [("0.168.0", false), ("0.170.0", true)] {
|
||||
let trace = Arc::new(Trace::default());
|
||||
let execution = Arc::new(SessionExecution {
|
||||
client: authenticated_client_with_min_versions(
|
||||
"sk_version_test",
|
||||
None,
|
||||
Some("0.162.0"),
|
||||
Some("26.1023.51421"),
|
||||
Some("0.170.0"),
|
||||
),
|
||||
session: Mutex::new(Some(Box::new(FakeSession::streaming(
|
||||
Arc::clone(&trace),
|
||||
@@ -2985,7 +3000,7 @@ async fn official_core_clients_should_be_version_checked_before_starting_respons
|
||||
let request = Request::post("/v1/responses")
|
||||
.header(AUTHORIZATION, "Bearer sk_version_test")
|
||||
.header("user-agent", format!(
|
||||
"{product}/{version} (Mac OS 26.6.2; arm64) Apple_Terminal ({product}; 0.165.0)"
|
||||
"{product}/{version} (Ubuntu 26.4.0; aarch64) xterm-256color ({client}; 0.169.0)"
|
||||
))
|
||||
.body(Body::from(json!({"model": "model-a", "input": "synthetic", "stream": true}).to_string()))
|
||||
.unwrap();
|
||||
@@ -3006,8 +3021,9 @@ async fn official_core_clients_should_be_version_checked_before_starting_respons
|
||||
assert_eq!(status, StatusCode::BAD_REQUEST, "{product}/{version}");
|
||||
let value: Value = serde_json::from_slice(&body).unwrap();
|
||||
assert_eq!(value["error"]["code"], "client_version_too_old");
|
||||
assert_eq!(value["error"]["client"], "codex_cli");
|
||||
assert_eq!(value["error"]["current_version"], version);
|
||||
assert_eq!(value["error"]["min_version"], "0.162.0");
|
||||
assert_eq!(value["error"]["min_version"], "0.170.0");
|
||||
assert!(execution.session.lock().unwrap().is_some());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,215 @@ fn generate_request(body: Value) -> DecodedGenerationRequest {
|
||||
decode_request(body.to_string().as_bytes()).expect("test request should decode")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn guardian_roles_should_use_complete_current_request_identity_before_header_projection() {
|
||||
use gateway_protocol::openai::{CodexGuardianRole::*, codex_guardian_role};
|
||||
|
||||
let review = r#"{"thread_source":"guardian_review"}"#;
|
||||
let classification =
|
||||
r#"{"thread_source":"guardian_classifier","turn_trigger":"guardian_classifier"}"#;
|
||||
let housekeeping = r#"{"turn_trigger":"guardian_housekeeper"}"#;
|
||||
for (websocket, body_agent, body_turn, header_agent, header_turn, explicit, expected) in [
|
||||
(
|
||||
false,
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Some(Reviewer),
|
||||
),
|
||||
(
|
||||
false,
|
||||
None,
|
||||
None,
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
None,
|
||||
Some(Reviewer),
|
||||
),
|
||||
(
|
||||
false,
|
||||
None,
|
||||
Some(review),
|
||||
Some("guardian"),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
),
|
||||
(
|
||||
false,
|
||||
Some("other"),
|
||||
Some(review),
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
None,
|
||||
None,
|
||||
),
|
||||
(
|
||||
false,
|
||||
Some("guardian"),
|
||||
Some(classification),
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
None,
|
||||
None,
|
||||
),
|
||||
(
|
||||
false,
|
||||
Some("guardian"),
|
||||
Some("invalid"),
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
None,
|
||||
None,
|
||||
),
|
||||
(
|
||||
false,
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
None,
|
||||
None,
|
||||
Some("classifier"),
|
||||
None,
|
||||
),
|
||||
(false, None, None, None, None, Some("reviewer"), None),
|
||||
(
|
||||
false,
|
||||
Some("guardian"),
|
||||
Some(classification),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Some(Classifier),
|
||||
),
|
||||
(
|
||||
false,
|
||||
Some("guardian"),
|
||||
Some(housekeeping),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Some(Housekeeper),
|
||||
),
|
||||
(
|
||||
true,
|
||||
None,
|
||||
None,
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
Some("reviewer"),
|
||||
None,
|
||||
),
|
||||
(
|
||||
true,
|
||||
None,
|
||||
Some(review),
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
None,
|
||||
None,
|
||||
),
|
||||
(
|
||||
true,
|
||||
Some("guardian"),
|
||||
Some(classification),
|
||||
Some("stale_agent"),
|
||||
Some(review),
|
||||
Some("reviewer"),
|
||||
Some(Classifier),
|
||||
),
|
||||
(
|
||||
true,
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Some(Reviewer),
|
||||
),
|
||||
] {
|
||||
let mut body =
|
||||
json!({"model":"gpt-5.6-luna","input":[],"generate":false,"client_metadata":{}});
|
||||
if let Some(agent) = body_agent {
|
||||
body["client_metadata"]["x-openai-subagent"] = json!(agent);
|
||||
}
|
||||
if let Some(turn) = body_turn {
|
||||
body["client_metadata"]["x-codex-turn-metadata"] = json!(turn);
|
||||
}
|
||||
let mut headers = HeaderMap::new();
|
||||
for (name, value) in [
|
||||
("x-openai-subagent", header_agent),
|
||||
("x-codex-turn-metadata", header_turn),
|
||||
("x-codex-guardian", explicit),
|
||||
] {
|
||||
if let Some(value) = value {
|
||||
headers.insert(name, HeaderValue::from_str(value).unwrap());
|
||||
}
|
||||
}
|
||||
let decoded = if websocket {
|
||||
body["type"] = json!("response.create");
|
||||
decode_response_create_with_context(
|
||||
&body.to_string(),
|
||||
&OpenAiRequestHeaders::from_headers(&headers),
|
||||
)
|
||||
.unwrap()
|
||||
} else {
|
||||
decode_request_with_headers(body.to_string().as_bytes(), &headers, 64 * 1024).unwrap()
|
||||
};
|
||||
let payload = generate_operation(&decoded).protocol_payload();
|
||||
if !websocket && matches!(expected, Some(Classifier | Housekeeper)) {
|
||||
assert!(!payload.context().contains_key("use_websocket"));
|
||||
assert!(
|
||||
!payload
|
||||
.context()
|
||||
.contains_key(gateway_protocol::openai::WEBSOCKET_FRAME_CONTEXT_KEY)
|
||||
);
|
||||
}
|
||||
if websocket && header_agent == Some("stale_agent") {
|
||||
assert_eq!(
|
||||
payload.body()["client_metadata"]["x-openai-subagent"],
|
||||
"guardian"
|
||||
);
|
||||
let opaque = payload.context()["opaque_request_headers"]
|
||||
.as_array()
|
||||
.unwrap();
|
||||
assert!(!opaque.iter().any(|pair| matches!(
|
||||
pair[0].as_str(),
|
||||
Some("x-codex-guardian" | "x-openai-subagent")
|
||||
)));
|
||||
}
|
||||
assert_eq!(
|
||||
codex_guardian_role(payload.body(), payload.context()),
|
||||
expected,
|
||||
"body={body}, headers={headers:?}"
|
||||
);
|
||||
}
|
||||
let mut repeated = HeaderMap::new();
|
||||
repeated.append("x-openai-subagent", HeaderValue::from_static("guardian"));
|
||||
repeated.append("x-openai-subagent", HeaderValue::from_static("other"));
|
||||
repeated.insert("x-codex-turn-metadata", HeaderValue::from_static(review));
|
||||
let decoded = decode_request_with_headers(
|
||||
br#"{"model":"gpt-5.6-luna","input":[]}"#,
|
||||
&repeated,
|
||||
64 * 1024,
|
||||
)
|
||||
.unwrap();
|
||||
let payload = generate_operation(&decoded).protocol_payload();
|
||||
assert_eq!(codex_guardian_role(payload.body(), payload.context()), None);
|
||||
for malformed in [
|
||||
r#"{"thread_source":"guardian_classifier","thread_source":"guardian_review"}"#,
|
||||
r#"{"thread_source":null}"#,
|
||||
r#"["guardian_review"]"#,
|
||||
] {
|
||||
let mut body = json!({"model":"gpt-5.6-luna","input":[],"client_metadata":{"x-openai-subagent":"guardian"}});
|
||||
body["client_metadata"]["x-codex-turn-metadata"] = json!(malformed);
|
||||
let decoded = generate_request(body);
|
||||
let payload = generate_operation(&decoded).protocol_payload();
|
||||
assert_eq!(codex_guardian_role(payload.body(), payload.context()), None);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decoder_should_attach_only_identified_codex_inference_turns() {
|
||||
let body = json!({"model":"smart-code", "input":"hello", "client_metadata": {
|
||||
@@ -492,7 +701,7 @@ fn decoder_should_strip_http_transport_but_leave_source_headers_for_provider() {
|
||||
HeaderValue::from_static(r#"{"scheme":"https"}"#),
|
||||
);
|
||||
headers.insert("accept-encoding", HeaderValue::from_static("br, gzip"));
|
||||
// 正文未压缩;入口现在消费 Content-Encoding,但仍不得向上游透传
|
||||
// identity 与未压缩的正文匹配,Content-Encoding 由入口消费
|
||||
headers.insert("content-encoding", HeaderValue::from_static("identity"));
|
||||
headers.insert("x-openai-future-mode", HeaderValue::from_static("keep"));
|
||||
|
||||
|
||||
+1
-1
@@ -358,7 +358,7 @@ async fn rewrite_upgrade_and_control_active_response_keep_http_context_until_con
|
||||
);
|
||||
let (trace, mut socket, server) =
|
||||
start_active_response_with(Some(frozen), "/custom/websocket", "codex-tui/0.166.0").await;
|
||||
// 升级后的默认会话仍持有入口上下文;101 正文结束不再提前取消它
|
||||
// 101 响应完成后,WebSocket 会话继续持有入口上下文
|
||||
assert!(
|
||||
!plan
|
||||
.http_cancellation
|
||||
|
||||
@@ -255,7 +255,7 @@ impl Stream for TestSocket {
|
||||
|
||||
fn poll_next(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let event = self.incoming.poll_recv(context);
|
||||
// 模拟 Axum transport 的自动 Pong;pump 不再重复实现协议层应答
|
||||
// 模拟 Axum 传输层收到 Ping 时自动回复 Pong
|
||||
if let Poll::Ready(Some(Ok(Message::Ping(payload)))) = &event {
|
||||
self.written
|
||||
.send(Message::Pong(payload.clone()))
|
||||
|
||||
@@ -45,6 +45,14 @@ impl Drop for TestServer {
|
||||
async fn connect(
|
||||
trace: Arc<AtomicFailureTrace>,
|
||||
response_headers: Vec<ProviderResponseHeader>,
|
||||
) -> (TestSocket, TestServer) {
|
||||
connect_with_headers(trace, response_headers, axum::http::HeaderMap::new()).await
|
||||
}
|
||||
|
||||
async fn connect_with_headers(
|
||||
trace: Arc<AtomicFailureTrace>,
|
||||
response_headers: Vec<ProviderResponseHeader>,
|
||||
opening_headers: axum::http::HeaderMap,
|
||||
) -> (TestSocket, TestServer) {
|
||||
let execution = Arc::new(AtomicFailureExecution {
|
||||
client: authenticated_client("sk_ws_atomic"),
|
||||
@@ -66,6 +74,7 @@ async fn connect(
|
||||
request
|
||||
.headers_mut()
|
||||
.insert(AUTHORIZATION, "Bearer sk_ws_atomic".parse().unwrap());
|
||||
request.headers_mut().extend(opening_headers);
|
||||
let (socket, response) = connect_async(request).await.expect("upgrade WebSocket");
|
||||
assert_eq!(response.status(), StatusCode::SWITCHING_PROTOCOLS);
|
||||
(socket, server)
|
||||
@@ -518,13 +527,42 @@ async fn errors_on_a_reused_connection_keep_their_own_upstream_ids() {
|
||||
initial_errors: Mutex::new(VecDeque::from(errors)),
|
||||
..AtomicFailureTrace::default()
|
||||
});
|
||||
let (mut socket, _server) = connect(trace, Vec::new()).await;
|
||||
let mut opening = axum::http::HeaderMap::new();
|
||||
opening.insert(
|
||||
"x-codex-turn-state",
|
||||
axum::http::HeaderValue::from_static("opening"),
|
||||
);
|
||||
opening.insert(
|
||||
"x-codex-turn-metadata",
|
||||
axum::http::HeaderValue::from_static(r#"{"request_kind":"turn"}"#),
|
||||
);
|
||||
opening.insert(
|
||||
"x-openai-subagent",
|
||||
axum::http::HeaderValue::from_static("guardian"),
|
||||
);
|
||||
opening.insert(
|
||||
"x-codex-guardian",
|
||||
axum::http::HeaderValue::from_static("reviewer"),
|
||||
);
|
||||
let (mut socket, _server) = connect_with_headers(Arc::clone(&trace), Vec::new(), opening).await;
|
||||
for request_id in ["req_first", "req_second"] {
|
||||
send_request(&mut socket).await;
|
||||
let error = next_error(&mut socket).await;
|
||||
assert_eq!(error["headers"]["x-request-id"], request_id);
|
||||
assert_eq!(error["headers"]["x-gateway-request-id"], "req_ws_atomic");
|
||||
}
|
||||
assert_eq!(
|
||||
*trace.request_turn_headers.lock().unwrap(),
|
||||
[
|
||||
[
|
||||
Some("opening".to_owned()),
|
||||
Some(r#"{"request_kind":"turn"}"#.to_owned()),
|
||||
Some("guardian".to_owned()),
|
||||
Some("incomplete".to_owned()),
|
||||
],
|
||||
[None, None, None, None]
|
||||
]
|
||||
);
|
||||
assert_no_duplicate_failure(&mut socket).await;
|
||||
socket.close(None).await.unwrap();
|
||||
}
|
||||
|
||||
@@ -168,21 +168,31 @@ fn response_create_should_preserve_compaction_trigger_for_openai() {
|
||||
|
||||
#[test]
|
||||
fn response_create_should_prefer_frame_turn_metadata_over_opening_headers() {
|
||||
let frame = r#"{"request_kind":"turn","tool_namespaces_info":{"workspace":{}}}"#;
|
||||
let opening = r#"{"request_kind":"compaction"}"#;
|
||||
let decoded = decode_response_create_with_turn_header(
|
||||
json!({
|
||||
"type": "response.create",
|
||||
"model": "smart-code",
|
||||
"input": "hello",
|
||||
"client_metadata": {
|
||||
"x-codex-turn-metadata": r#"{"request_kind":"turn"}"#
|
||||
"x-codex-turn-metadata": frame
|
||||
}
|
||||
}),
|
||||
r#"{"request_kind":"compaction"}"#,
|
||||
opening,
|
||||
);
|
||||
let Operation::Generate(request) = decoded.operation() else {
|
||||
panic!("Responses must map to Generate");
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
request.protocol_payload().context()["turn_metadata"],
|
||||
opening
|
||||
);
|
||||
assert_eq!(
|
||||
request.protocol_payload().body()["client_metadata"]["x-codex-turn-metadata"],
|
||||
frame
|
||||
);
|
||||
let semantics = codex_responses_request_semantics(
|
||||
request.protocol_payload().body(),
|
||||
request.protocol_payload().context(),
|
||||
@@ -308,6 +318,7 @@ struct AtomicFailureTrace {
|
||||
committed: AtomicBool,
|
||||
finalized: AtomicBool,
|
||||
downstream_websocket_connection_ids: Mutex<Vec<String>>,
|
||||
request_turn_headers: Mutex<Vec<[Option<String>; 4]>>,
|
||||
initial_errors: Mutex<std::collections::VecDeque<EngineError>>,
|
||||
first_batch: Mutex<Option<CoordinatedEvent>>,
|
||||
session_drops: Arc<AtomicUsize>,
|
||||
@@ -437,6 +448,16 @@ impl ExecutionService for AtomicFailureExecution {
|
||||
.get("downstream_websocket_connection_id")
|
||||
.and_then(Value::as_str)
|
||||
{
|
||||
let context = generate.protocol_payload().context();
|
||||
self.trace.request_turn_headers.lock().unwrap().push(
|
||||
[
|
||||
"turn_state",
|
||||
"turn_metadata",
|
||||
"subagent",
|
||||
"codex_guardian_identity",
|
||||
]
|
||||
.map(|key| context.get(key).and_then(Value::as_str).map(str::to_owned)),
|
||||
);
|
||||
self.trace
|
||||
.downstream_websocket_connection_ids
|
||||
.lock()
|
||||
@@ -1211,12 +1232,16 @@ fn upgrade_request(authorization: &str) -> Request<Body> {
|
||||
|
||||
#[tokio::test]
|
||||
async fn official_core_version_gate_should_run_before_websocket_upgrade() {
|
||||
let app = api_router(ModelsExecution::with_cli_min("0.162.0")).await;
|
||||
let app = api_router(ModelsExecution::with_cli_min("0.170.0")).await;
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
|
||||
|
||||
for version in ["0.158.0", "0.162.0"] {
|
||||
for (product, version) in [
|
||||
("codex-tui", "0.158.0"),
|
||||
("Codex Desktop", "0.168.0"),
|
||||
("Codex Desktop", "0.170.0"),
|
||||
] {
|
||||
let mut request = format!("ws://{address}/v1/responses")
|
||||
.into_client_request()
|
||||
.unwrap();
|
||||
@@ -1227,12 +1252,12 @@ async fn official_core_version_gate_should_run_before_websocket_upgrade() {
|
||||
request.headers_mut().insert(
|
||||
"user-agent",
|
||||
HeaderValue::from_str(&format!(
|
||||
"codex-tui/{version} (Mac OS 26.6.2; arm64) Apple_Terminal (codex-tui; 0.165.0)"
|
||||
"{product}/{version} (Ubuntu 26.4.0; aarch64) xterm-256color (codex-tui; 0.169.0)"
|
||||
))
|
||||
.unwrap(),
|
||||
);
|
||||
let result = connect_async(request).await;
|
||||
if version == "0.162.0" {
|
||||
if version == "0.170.0" {
|
||||
let (mut socket, response) = result.expect("current Core upgrades successfully");
|
||||
assert_eq!(response.status(), StatusCode::SWITCHING_PROTOCOLS);
|
||||
socket.close(None).await.unwrap();
|
||||
@@ -1245,8 +1270,9 @@ async fn official_core_version_gate_should_run_before_websocket_upgrade() {
|
||||
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
|
||||
let value: Value = serde_json::from_slice(response.body().as_deref().unwrap()).unwrap();
|
||||
assert_eq!(value["error"]["code"], "client_version_too_old");
|
||||
assert_eq!(value["error"]["current_version"], "0.158.0");
|
||||
assert_eq!(value["error"]["min_version"], "0.162.0");
|
||||
assert_eq!(value["error"]["client"], "codex_cli");
|
||||
assert_eq!(value["error"]["current_version"], version);
|
||||
assert_eq!(value["error"]["min_version"], "0.170.0");
|
||||
}
|
||||
}
|
||||
server.abort();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! 账号分组的 Fast 三态策略与多分组优先级
|
||||
//! 账号分组的快速模式策略与多分组优先级
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -10,6 +10,7 @@ pub enum FastMode {
|
||||
Default,
|
||||
Enabled,
|
||||
Disabled,
|
||||
Ultrafast,
|
||||
}
|
||||
|
||||
impl FastMode {
|
||||
@@ -18,6 +19,7 @@ impl FastMode {
|
||||
match self {
|
||||
Self::Default => "default",
|
||||
Self::Enabled => "enabled",
|
||||
Self::Ultrafast => "ultrafast",
|
||||
Self::Disabled => "disabled",
|
||||
}
|
||||
}
|
||||
@@ -27,17 +29,19 @@ impl FastMode {
|
||||
match value {
|
||||
"default" => Some(Self::Default),
|
||||
"enabled" => Some(Self::Enabled),
|
||||
"ultrafast" => Some(Self::Ultrafast),
|
||||
"disabled" => Some(Self::Disabled),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// 多分组冲突时关闭优先于开启,默认不覆盖其他分组
|
||||
/// 多分组冲突时禁用优先;同时强制两种加速档位时选用较低的 Fast
|
||||
#[must_use]
|
||||
pub const fn merge(self, other: Self) -> Self {
|
||||
match (self, other) {
|
||||
(Self::Disabled, _) | (_, Self::Disabled) => Self::Disabled,
|
||||
(Self::Enabled, _) | (_, Self::Enabled) => Self::Enabled,
|
||||
(Self::Ultrafast, _) | (_, Self::Ultrafast) => Self::Ultrafast,
|
||||
_ => Self::Default,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,31 @@ pub struct RequestLocation {
|
||||
pub timezone: chrono_tz::Tz,
|
||||
}
|
||||
|
||||
/// 独立选择 OpenAI 请求的国家字段,仅自定义模式需要国家代码
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
|
||||
#[serde(tag = "mode", rename_all = "snake_case", deny_unknown_fields)]
|
||||
pub enum OpenAiRequestCountry {
|
||||
Default {},
|
||||
Passthrough {},
|
||||
FollowLocation {},
|
||||
Custom { country: String },
|
||||
}
|
||||
|
||||
impl Default for OpenAiRequestCountry {
|
||||
fn default() -> Self {
|
||||
Self::FollowLocation {}
|
||||
}
|
||||
}
|
||||
|
||||
impl OpenAiRequestCountry {
|
||||
pub fn validate(&self) -> Result<(), InvalidRequestLocation> {
|
||||
if let Self::Custom { country } = self {
|
||||
validate_country(country)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum InvalidRequestLocation {
|
||||
#[error("country must contain two uppercase ASCII letters")]
|
||||
@@ -34,9 +59,7 @@ impl Default for RequestLocation {
|
||||
|
||||
impl RequestLocation {
|
||||
pub fn validate(&self) -> Result<(), InvalidRequestLocation> {
|
||||
if self.country.len() != 2 || !self.country.bytes().all(|byte| byte.is_ascii_uppercase()) {
|
||||
return Err(InvalidRequestLocation::Country);
|
||||
}
|
||||
validate_country(&self.country)?;
|
||||
for (value, error) in [
|
||||
(&self.region, InvalidRequestLocation::Region),
|
||||
(&self.city, InvalidRequestLocation::City),
|
||||
@@ -59,3 +82,11 @@ impl RequestLocation {
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_country(country: &str) -> Result<(), InvalidRequestLocation> {
|
||||
if country.len() == 2 && country.bytes().all(|byte| byte.is_ascii_uppercase()) {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(InvalidRequestLocation::Country)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ pub use error_protection::*;
|
||||
mod fast_mode;
|
||||
pub use fast_mode::FastMode;
|
||||
mod location;
|
||||
pub use location::{InvalidRequestLocation, RequestLocation};
|
||||
pub use location::{InvalidRequestLocation, OpenAiRequestCountry, RequestLocation};
|
||||
mod model;
|
||||
mod model_access;
|
||||
pub use model_access::{
|
||||
|
||||
@@ -1292,6 +1292,7 @@ pub struct CredentialCasUpdateParts {
|
||||
pub expected_revision: CredentialRevision,
|
||||
pub profile: ProviderAccountUpdate,
|
||||
pub preserve_profile: bool,
|
||||
pub preserve_access_token_expiry: bool,
|
||||
pub credential: PlaintextCredential,
|
||||
pub has_refresh_token: bool,
|
||||
pub access_token_expires_at: Option<SystemTime>,
|
||||
@@ -1306,6 +1307,7 @@ pub struct CredentialCasUpdate {
|
||||
expected_revision: CredentialRevision,
|
||||
profile: ProviderAccountUpdate,
|
||||
preserve_profile: bool,
|
||||
preserve_access_token_expiry: bool,
|
||||
credential: PlaintextCredential,
|
||||
has_refresh_token: bool,
|
||||
access_token_expires_at: Option<SystemTime>,
|
||||
@@ -1321,6 +1323,10 @@ impl fmt::Debug for CredentialCasUpdate {
|
||||
.field("expected_revision", &self.expected_revision)
|
||||
.field("profile", &self.profile)
|
||||
.field("preserve_profile", &self.preserve_profile)
|
||||
.field(
|
||||
"preserve_access_token_expiry",
|
||||
&self.preserve_access_token_expiry,
|
||||
)
|
||||
.field("credential", &self.credential)
|
||||
.field("has_refresh_token", &self.has_refresh_token)
|
||||
.field("access_token_expires_at", &self.access_token_expires_at)
|
||||
@@ -1356,6 +1362,7 @@ impl CredentialCasUpdate {
|
||||
expected_revision,
|
||||
profile,
|
||||
preserve_profile: false,
|
||||
preserve_access_token_expiry: false,
|
||||
credential,
|
||||
has_refresh_token,
|
||||
access_token_expires_at,
|
||||
@@ -1371,6 +1378,13 @@ impl CredentialCasUpdate {
|
||||
self
|
||||
}
|
||||
|
||||
/// 沿用同一访问令牌时,保留提交时的有效期,避免覆盖在途收到的撤销事实
|
||||
#[must_use]
|
||||
pub const fn preserving_access_token_expiry(mut self) -> Self {
|
||||
self.preserve_access_token_expiry = true;
|
||||
self
|
||||
}
|
||||
|
||||
/// 将刷新调度与账号错误事实放入同一个 revision CAS
|
||||
#[must_use]
|
||||
pub fn with_account_state(
|
||||
@@ -1437,6 +1451,7 @@ impl CredentialCasUpdate {
|
||||
expected_revision: self.expected_revision,
|
||||
profile: self.profile,
|
||||
preserve_profile: self.preserve_profile,
|
||||
preserve_access_token_expiry: self.preserve_access_token_expiry,
|
||||
credential: self.credential,
|
||||
has_refresh_token: self.has_refresh_token,
|
||||
access_token_expires_at: self.access_token_expires_at,
|
||||
|
||||
@@ -88,6 +88,12 @@ impl AccountModelAccess {
|
||||
AccountModelAccessMode::Denylist => !self.models.contains(upstream_model),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn allows_optional_model(&self, upstream_model: Option<&str>) -> bool {
|
||||
upstream_model.map_or(self.mode == AccountModelAccessMode::All, |model| {
|
||||
self.allows(model)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! 请求认证时冻结的账号范围与目录,不依赖路由选择器
|
||||
|
||||
use super::{FastMode, ProviderAccountId};
|
||||
use super::{AccountModelAccess, FastMode, ProviderAccountId};
|
||||
use crate::identity::ProviderKind;
|
||||
use crate::validation::{IdentifierError, RoutingError};
|
||||
use std::{
|
||||
@@ -107,6 +107,7 @@ pub struct RuntimeAccountDirectory {
|
||||
accounts: BTreeMap<ProviderAccountId, RuntimeAccount>,
|
||||
providers_with_accounts: BTreeSet<ProviderKind>,
|
||||
providers_by_group: BTreeMap<AccountGroupId, BTreeSet<ProviderKind>>,
|
||||
group_model_access: BTreeMap<AccountGroupId, AccountModelAccess>,
|
||||
}
|
||||
|
||||
impl RuntimeAccountDirectory {
|
||||
@@ -127,9 +128,28 @@ impl RuntimeAccountDirectory {
|
||||
accounts,
|
||||
providers_with_accounts,
|
||||
providers_by_group,
|
||||
group_model_access: BTreeMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn with_group_model_access(
|
||||
mut self,
|
||||
policies: BTreeMap<AccountGroupId, AccountModelAccess>,
|
||||
) -> Self {
|
||||
self.group_model_access = policies;
|
||||
self
|
||||
}
|
||||
|
||||
pub(crate) fn accounts_in_group(
|
||||
&self,
|
||||
group: &AccountGroupId,
|
||||
) -> impl Iterator<Item = (&ProviderAccountId, &RuntimeAccount)> {
|
||||
self.accounts
|
||||
.iter()
|
||||
.filter(move |(_, account)| account.group_ids.contains(group))
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn account(&self, account_id: &ProviderAccountId) -> Option<&RuntimeAccount> {
|
||||
self.accounts.get(account_id)
|
||||
@@ -267,6 +287,7 @@ impl ClientRoutingScope {
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct FrozenAccountScope {
|
||||
allow_image_generation: bool,
|
||||
allow_subagents: bool,
|
||||
fast_mode: FastMode,
|
||||
request_profiles: BTreeMap<ProviderKind, super::OpaqueProviderData>,
|
||||
directory: Arc<RuntimeAccountDirectory>,
|
||||
@@ -303,6 +324,11 @@ impl FrozenAccountScope {
|
||||
self.allow_image_generation
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn allows_subagents(&self) -> bool {
|
||||
self.allow_subagents
|
||||
}
|
||||
|
||||
/// 与 Key 授权范围一同冻结;具体字段只由对应 Provider 解释
|
||||
#[must_use]
|
||||
pub fn with_request_profiles(
|
||||
@@ -323,6 +349,12 @@ impl FrozenAccountScope {
|
||||
&self.request_profiles
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn with_subagents(mut self, allowed: bool) -> Self {
|
||||
self.allow_subagents = allowed;
|
||||
self
|
||||
}
|
||||
|
||||
/// Key 绑定分组的冻结 Fast 策略,与账号成员资格无关
|
||||
#[must_use]
|
||||
pub const fn with_fast_mode(mut self, fast_mode: FastMode) -> Self {
|
||||
@@ -343,6 +375,7 @@ impl FrozenAccountScope {
|
||||
};
|
||||
Self {
|
||||
allow_image_generation: true,
|
||||
allow_subagents: true,
|
||||
fast_mode: FastMode::Default,
|
||||
request_profiles: BTreeMap::new(),
|
||||
directory,
|
||||
@@ -399,6 +432,14 @@ impl FrozenAccountScope {
|
||||
|
||||
#[must_use]
|
||||
pub fn allows(&self, account_id: &ProviderAccountId) -> bool {
|
||||
self.allows_with_policy(account_id, |_| true)
|
||||
}
|
||||
|
||||
fn allows_with_policy(
|
||||
&self,
|
||||
account_id: &ProviderAccountId,
|
||||
accepts: impl Fn(&AccountModelAccess) -> bool,
|
||||
) -> bool {
|
||||
let Some(account) = self.directory.account(account_id) else {
|
||||
return false;
|
||||
};
|
||||
@@ -408,6 +449,7 @@ impl FrozenAccountScope {
|
||||
.allowed_account_ids
|
||||
.as_ref()
|
||||
.is_some_and(|accounts| !accounts.contains(account_id))
|
||||
|| !accepts(account.model_access())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -417,18 +459,43 @@ impl FrozenAccountScope {
|
||||
enabled_group_ids, ..
|
||||
} => account
|
||||
.group_ids()
|
||||
.iter()
|
||||
.any(|group_id| enabled_group_ids.contains(group_id)),
|
||||
.intersection(enabled_group_ids)
|
||||
.any(|id| {
|
||||
self.directory
|
||||
.group_model_access
|
||||
.get(id)
|
||||
.is_none_or(&accepts)
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn allows_model(&self, account_id: &ProviderAccountId, upstream_model: &str) -> bool {
|
||||
self.allows(account_id)
|
||||
&& self
|
||||
.directory
|
||||
.account(account_id)
|
||||
.is_some_and(|account| account.model_access.allows(upstream_model))
|
||||
self.allows_with_policy(account_id, |access| access.allows(upstream_model))
|
||||
}
|
||||
|
||||
/// 不猜测原生端点的默认模型,账号与授权它的分组均须允许任意模型
|
||||
#[must_use]
|
||||
pub fn allows_unidentified_model(&self, account_id: &ProviderAccountId) -> bool {
|
||||
self.allows_with_policy(account_id, |access| access.allows_optional_model(None))
|
||||
}
|
||||
|
||||
/// 只报告所有已启用绑定分组均明确拒绝模型的情况,空池沿用原路由错误
|
||||
#[must_use]
|
||||
pub fn groups_deny_model(&self, upstream_model: Option<&str>) -> bool {
|
||||
let ClientRoutingScope::Restricted {
|
||||
enabled_group_ids, ..
|
||||
} = &self.client_scope
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
!enabled_group_ids.is_empty()
|
||||
&& enabled_group_ids.iter().all(|id| {
|
||||
self.directory
|
||||
.group_model_access
|
||||
.get(id)
|
||||
.is_some_and(|access| !access.allows_optional_model(upstream_model))
|
||||
})
|
||||
}
|
||||
|
||||
/// 目录按整个授权账号池过滤,不能只看用于获取元数据的账号政策
|
||||
|
||||
@@ -173,6 +173,15 @@ pub trait ProviderAccountStore: Send + Sync {
|
||||
change: QuotaAccessChange,
|
||||
) -> Result<QuotaWriteOutcome, StoreError>;
|
||||
|
||||
/// 按请求使用的凭据版本缩短访问令牌有效期,保留在途续期的版本和重试计划
|
||||
async fn expire_access_token(
|
||||
&self,
|
||||
_account: &ProviderAccount,
|
||||
_observed_at: std::time::SystemTime,
|
||||
) -> Result<bool, StoreError> {
|
||||
Err(StoreError::new(crate::error::StoreErrorKind::InvalidData))
|
||||
}
|
||||
|
||||
async fn apply_state_change(&self, change: AccountStateChange) -> Result<(), StoreError>;
|
||||
|
||||
async fn update_account(&self, update: ProviderAccountUpdate) -> Result<(), StoreError>;
|
||||
|
||||
@@ -37,7 +37,7 @@ use crate::event::{
|
||||
use crate::lifecycle::{CancellationToken, Deadline, LeaseGuard};
|
||||
use crate::metering::Decimal;
|
||||
use crate::operation::{Operation, ProviderSessionState};
|
||||
use crate::routing::RoutingPlan;
|
||||
use crate::routing::{ProviderCandidate, RoutingPlan};
|
||||
use futures::future::{BoxFuture, Fuse};
|
||||
use futures::{FutureExt, StreamExt, pin_mut, select_biased};
|
||||
use futures_timer::Delay;
|
||||
@@ -1171,6 +1171,7 @@ where
|
||||
.with_fast_mode(self.plan.fast_mode())
|
||||
.with_pricing(self.plan.pricing())
|
||||
.with_request_location(self.plan.request_location().cloned())
|
||||
.with_openai_request_country(self.plan.openai_request_country().clone())
|
||||
.with_privacy(self.plan.privacy())
|
||||
.with_request_body_limit(self.plan.request_body_limit())
|
||||
.with_concurrency_wait_budget(self.concurrency_wait_budget.clone())
|
||||
@@ -1239,142 +1240,11 @@ where
|
||||
}
|
||||
ProviderBoundary::Result(result) => match *result {
|
||||
Ok(stream) => stream,
|
||||
Err(mut error) => {
|
||||
attempt_trace.record_provider_failure(&error);
|
||||
if self.continuation_attempt == ContinuationAttempt::RotateForClientReplay
|
||||
&& error.continuation_recovery_disposition()
|
||||
== Some(ContinuationRecoveryDisposition::ClientReplayRequired)
|
||||
&& let Some(original) = self.last_retryable_failure.take()
|
||||
{
|
||||
// 迁移只改变客户端恢复指令;失败分类、发送事实与运维正文仍属于原请求
|
||||
error = original.with_client_recovery_from(error);
|
||||
self.last_retryable_failure_events.clear();
|
||||
}
|
||||
let connection_fallback = self.connection_rotation.is_some()
|
||||
&& self
|
||||
.last_retryable_failure
|
||||
.as_ref()
|
||||
.and_then(ProviderError::unreachable_proxy)
|
||||
.is_none()
|
||||
&& !self.connection_budget.exhausted()
|
||||
&& !error.retry_is_prohibited()
|
||||
&& error.send_state() == UpstreamSendState::NotSent
|
||||
&& matches!(
|
||||
error.kind(),
|
||||
ProviderErrorKind::NoEligibleAccount
|
||||
| ProviderErrorKind::AccountCapacityUnavailable
|
||||
| ProviderErrorKind::QuotaExhausted
|
||||
| ProviderErrorKind::ConcurrencyQueueFull
|
||||
| ProviderErrorKind::ConcurrencyQueueTimeout
|
||||
);
|
||||
let continuation_retry = !error.retry_is_prohibited()
|
||||
&& self.prepare_unavailable_native_continuation_replay(&error);
|
||||
let candidate_retry = !error.retry_is_prohibited() && !connection_fallback && !continuation_retry
|
||||
&& matches!(
|
||||
error.kind(),
|
||||
ProviderErrorKind::AccountCapacityUnavailable
|
||||
| ProviderErrorKind::NoEligibleAccount
|
||||
| ProviderErrorKind::QuotaExhausted
|
||||
| ProviderErrorKind::ProviderInfrastructureUnavailable
|
||||
| ProviderErrorKind::ConcurrencyQueueFull
|
||||
| ProviderErrorKind::ConcurrencyQueueTimeout
|
||||
)
|
||||
&& error.send_state() == UpstreamSendState::NotSent
|
||||
&& self.current_send_state() == UpstreamSendState::NotSent
|
||||
&& matches!(
|
||||
self.continuation_attempt,
|
||||
ContinuationAttempt::None | ContinuationAttempt::ReplayAny
|
||||
)
|
||||
// 跨 Provider 候选推进必然换号(账号行按 Provider 隔离),
|
||||
// 预算耗尽后不再推进,交回容量类失败的原有终态语义。
|
||||
&& self.has_account_rotation_budget()
|
||||
&& self.advance_provider_candidate();
|
||||
let retryable = self
|
||||
.apply_retry_policy(super::policy::RetryFacts {
|
||||
attempt_index: next_attempt,
|
||||
provider: candidate.provider().clone(),
|
||||
model: candidate
|
||||
.upstream_model()
|
||||
.map(|model| model.as_str().to_owned()),
|
||||
error_kind: error.kind(),
|
||||
upstream_status: error.upstream_status(),
|
||||
send_state: error.send_state(),
|
||||
remaining_routing_attempts: self
|
||||
.plan
|
||||
.max_attempts()
|
||||
.get()
|
||||
.saturating_sub(self.routing_attempts),
|
||||
remaining_deadline: Duration::ZERO,
|
||||
retry_allowed: connection_fallback
|
||||
|| continuation_retry
|
||||
|| candidate_retry,
|
||||
})
|
||||
.await?;
|
||||
attempt_trace.record("retry.decided", json!({
|
||||
"phase": "prepare", "retryable": retryable,
|
||||
"continuationRetry": continuation_retry, "candidateRetry": candidate_retry,
|
||||
"connectionFallback": connection_fallback,
|
||||
"retryProhibited": error.retry_is_prohibited(),
|
||||
"sendState": error.send_state().as_str(),
|
||||
"requestSendState": self.current_send_state().as_str(),
|
||||
"continuation": format!("{:?}", self.continuation_attempt),
|
||||
"accountRotations": self.account_rotations,
|
||||
"maxAccountRotations": self.plan.account_selection_policy().max_account_rotations(),
|
||||
"remainingRoutingAttempts": self.plan.max_attempts().get().saturating_sub(self.routing_attempts),
|
||||
}));
|
||||
if retryable {
|
||||
if connection_fallback {
|
||||
let recovery = self
|
||||
.connection_rotation
|
||||
.take()
|
||||
.expect("pending connection rotation");
|
||||
self.excluded_accounts.remove(&recovery.account);
|
||||
self.pending_retry = Some(recovery);
|
||||
} else if candidate_retry {
|
||||
// 原账号只属于原 Provider,跨候选后不能拿旧钉选作恢复兜底
|
||||
self.connection_rotation = None;
|
||||
}
|
||||
return Ok(Some(PullOutcome::AttemptDiscarded));
|
||||
}
|
||||
if matches!(
|
||||
error.kind(),
|
||||
ProviderErrorKind::AccountCapacityUnavailable
|
||||
| ProviderErrorKind::NoEligibleAccount
|
||||
| ProviderErrorKind::SessionPolicyRestricted
|
||||
| ProviderErrorKind::QuotaExhausted
|
||||
| ProviderErrorKind::ConcurrencyQueueFull
|
||||
| ProviderErrorKind::ConcurrencyQueueTimeout
|
||||
) && let Some(last_failure) = self.last_retryable_failure.take()
|
||||
{
|
||||
let send_state = self.current_send_state();
|
||||
let mut events = std::mem::take(&mut self.last_retryable_failure_events);
|
||||
if !events.is_empty() {
|
||||
self.observe_atomic_terminal_events(&mut events);
|
||||
self.budget_attempt_already_counted = true;
|
||||
return Ok(Some(PullOutcome::TerminalFailure {
|
||||
events,
|
||||
error: last_failure,
|
||||
send_state,
|
||||
}));
|
||||
}
|
||||
self.finish_provider_error(&last_failure).await?;
|
||||
return Err(provider_engine_error(last_failure));
|
||||
}
|
||||
if !(matches!(
|
||||
error.kind(),
|
||||
ProviderErrorKind::AccountCapacityUnavailable
|
||||
| ProviderErrorKind::NoEligibleAccount
|
||||
| ProviderErrorKind::SessionPolicyRestricted
|
||||
| ProviderErrorKind::QuotaExhausted
|
||||
| ProviderErrorKind::ProviderInfrastructureUnavailable
|
||||
| ProviderErrorKind::ConcurrencyQueueFull
|
||||
| ProviderErrorKind::ConcurrencyQueueTimeout
|
||||
) && error.send_state() == UpstreamSendState::NotSent)
|
||||
{
|
||||
self.record_provider_failure(candidate.provider().clone());
|
||||
}
|
||||
self.finish_provider_error(&error).await?;
|
||||
return Err(provider_engine_error(error));
|
||||
Err(error) => {
|
||||
return self
|
||||
.handle_prepare_failure(error, &candidate, next_attempt, &attempt_trace)
|
||||
.await
|
||||
.map(Some);
|
||||
}
|
||||
},
|
||||
};
|
||||
@@ -1560,6 +1430,131 @@ where
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
async fn handle_prepare_failure(
|
||||
&mut self,
|
||||
mut error: ProviderError,
|
||||
candidate: &ProviderCandidate,
|
||||
next_attempt: NonZeroU32,
|
||||
attempt_trace: &TraceContext,
|
||||
) -> Result<PullOutcome, EngineError> {
|
||||
attempt_trace.record_provider_failure(&error);
|
||||
if self.continuation_attempt == ContinuationAttempt::RotateForClientReplay
|
||||
&& error.continuation_recovery_disposition()
|
||||
== Some(ContinuationRecoveryDisposition::ClientReplayRequired)
|
||||
&& let Some(original) = self.last_retryable_failure.take()
|
||||
{
|
||||
// 迁移只改变客户端恢复指令;失败分类、发送事实与运维正文仍属于原请求
|
||||
error = original.with_client_recovery_from(error);
|
||||
self.last_retryable_failure_events.clear();
|
||||
}
|
||||
let kind = error.kind();
|
||||
let account_unavailable = matches!(
|
||||
kind,
|
||||
ProviderErrorKind::NoEligibleAccount
|
||||
| ProviderErrorKind::AccountCapacityUnavailable
|
||||
| ProviderErrorKind::QuotaExhausted
|
||||
| ProviderErrorKind::ConcurrencyQueueFull
|
||||
| ProviderErrorKind::ConcurrencyQueueTimeout
|
||||
);
|
||||
let infrastructure_unavailable =
|
||||
kind == ProviderErrorKind::ProviderInfrastructureUnavailable;
|
||||
let selection_failed =
|
||||
account_unavailable || kind == ProviderErrorKind::SessionPolicyRestricted;
|
||||
let not_sent = error.send_state() == UpstreamSendState::NotSent;
|
||||
let retry_allowed = !error.retry_is_prohibited();
|
||||
let connection_fallback = self.connection_rotation.is_some()
|
||||
&& self
|
||||
.last_retryable_failure
|
||||
.as_ref()
|
||||
.and_then(ProviderError::unreachable_proxy)
|
||||
.is_none()
|
||||
&& !self.connection_budget.exhausted()
|
||||
&& retry_allowed
|
||||
&& not_sent
|
||||
&& account_unavailable;
|
||||
let continuation_retry =
|
||||
retry_allowed && self.prepare_unavailable_native_continuation_replay(&error);
|
||||
let candidate_retry = retry_allowed
|
||||
&& !connection_fallback
|
||||
&& !continuation_retry
|
||||
&& (account_unavailable || infrastructure_unavailable)
|
||||
&& not_sent
|
||||
&& self.current_send_state() == UpstreamSendState::NotSent
|
||||
&& matches!(
|
||||
self.continuation_attempt,
|
||||
ContinuationAttempt::None | ContinuationAttempt::ReplayAny
|
||||
)
|
||||
// 跨 Provider 候选推进必然换号(账号行按 Provider 隔离),
|
||||
// 预算耗尽后不再推进,交回容量类失败的原有终态语义
|
||||
&& self.has_account_rotation_budget()
|
||||
&& self.advance_provider_candidate();
|
||||
let retryable = self
|
||||
.apply_retry_policy(super::policy::RetryFacts {
|
||||
attempt_index: next_attempt,
|
||||
provider: candidate.provider().clone(),
|
||||
model: candidate
|
||||
.upstream_model()
|
||||
.map(|model| model.as_str().to_owned()),
|
||||
error_kind: kind,
|
||||
upstream_status: error.upstream_status(),
|
||||
send_state: error.send_state(),
|
||||
remaining_routing_attempts: self
|
||||
.plan
|
||||
.max_attempts()
|
||||
.get()
|
||||
.saturating_sub(self.routing_attempts),
|
||||
remaining_deadline: Duration::ZERO,
|
||||
retry_allowed: connection_fallback || continuation_retry || candidate_retry,
|
||||
})
|
||||
.await?;
|
||||
attempt_trace.record("retry.decided", json!({
|
||||
"phase": "prepare", "retryable": retryable,
|
||||
"continuationRetry": continuation_retry, "candidateRetry": candidate_retry,
|
||||
"connectionFallback": connection_fallback,
|
||||
"retryProhibited": error.retry_is_prohibited(),
|
||||
"sendState": error.send_state().as_str(),
|
||||
"requestSendState": self.current_send_state().as_str(),
|
||||
"continuation": format!("{:?}", self.continuation_attempt),
|
||||
"accountRotations": self.account_rotations,
|
||||
"maxAccountRotations": self.plan.account_selection_policy().max_account_rotations(),
|
||||
"remainingRoutingAttempts": self.plan.max_attempts().get().saturating_sub(self.routing_attempts),
|
||||
}));
|
||||
if retryable {
|
||||
if connection_fallback {
|
||||
let recovery = self
|
||||
.connection_rotation
|
||||
.take()
|
||||
.expect("pending connection rotation");
|
||||
self.excluded_accounts.remove(&recovery.account);
|
||||
self.pending_retry = Some(recovery);
|
||||
} else if candidate_retry {
|
||||
// 原账号只属于原 Provider,跨候选后不能拿旧钉选作恢复兜底
|
||||
self.connection_rotation = None;
|
||||
}
|
||||
return Ok(PullOutcome::AttemptDiscarded);
|
||||
}
|
||||
if selection_failed && let Some(last_failure) = self.last_retryable_failure.take() {
|
||||
let send_state = self.current_send_state();
|
||||
let mut events = std::mem::take(&mut self.last_retryable_failure_events);
|
||||
if !events.is_empty() {
|
||||
self.observe_atomic_terminal_events(&mut events);
|
||||
self.budget_attempt_already_counted = true;
|
||||
return Ok(PullOutcome::TerminalFailure {
|
||||
events,
|
||||
error: last_failure,
|
||||
send_state,
|
||||
});
|
||||
}
|
||||
self.finish_provider_error(&last_failure).await?;
|
||||
return Err(provider_engine_error(last_failure));
|
||||
}
|
||||
if !(not_sent && (selection_failed || infrastructure_unavailable)) {
|
||||
self.record_provider_failure(candidate.provider().clone());
|
||||
}
|
||||
self.finish_provider_error(&error).await?;
|
||||
Err(provider_engine_error(error))
|
||||
}
|
||||
|
||||
fn advance_provider_candidate(&mut self) -> bool {
|
||||
let Some(next) = self.candidate_index.checked_add(1) else {
|
||||
return false;
|
||||
@@ -1947,7 +1942,7 @@ where
|
||||
let store = Arc::clone(self.engine.store());
|
||||
let request_id = self.request_id.clone();
|
||||
let persisted = self.request_persisted;
|
||||
// 错误、账号 lease 与决策 future 都跨 next_event 的取消保留
|
||||
// 错误、尝试记录与决策 future 都跨 next_event 的取消保留
|
||||
// 在持久化及策略回调完成前,不能再次拉流或建立新的 attempt
|
||||
self.current = Some(current);
|
||||
self.pending_stream_failure = Some(PendingStreamFailure {
|
||||
@@ -2063,7 +2058,11 @@ where
|
||||
.pending_terminal_failure
|
||||
.as_ref()
|
||||
.ok_or(EngineError::NoActiveAttempt)?;
|
||||
if pending.events.is_empty() {
|
||||
// 在保活提交前冻结 HTTP 错误,否则必须交付原流内事件
|
||||
if pending.events.is_empty()
|
||||
|| (pending.error.client_visible_upstream_response().is_some()
|
||||
&& self.response_commit.finish())
|
||||
{
|
||||
return Err(provider_engine_error(
|
||||
self.finalize_pending_terminal_failure().await?,
|
||||
));
|
||||
@@ -2440,6 +2439,9 @@ where
|
||||
}
|
||||
|
||||
async fn finish_interruption(&mut self, error: &EngineError) -> Result<(), EngineError> {
|
||||
if let Some(current) = self.current.as_mut() {
|
||||
current.stream.abort();
|
||||
}
|
||||
// 结算 I/O 可能继续等待,先冻结交付事实以保持终态与实际 HTTP 状态一致
|
||||
self.response_commit.finish();
|
||||
let (outcome, gateway_error) = match error {
|
||||
|
||||
@@ -42,9 +42,12 @@ impl ResponseCommit {
|
||||
self.committed_at().is_some()
|
||||
}
|
||||
|
||||
pub(crate) fn finish(&self) {
|
||||
// 执行已结束且未提交时保留原 HTTP 错误,不能因外层插件迟迟返回再改成 200
|
||||
let _ = self.first.set(TransportState::Finished);
|
||||
/// 冻结交付状态,返回是否仍可发送独立的 HTTP 错误响应
|
||||
pub(crate) fn finish(&self) -> bool {
|
||||
matches!(
|
||||
self.first.get_or_init(|| TransportState::Finished),
|
||||
TransportState::Finished
|
||||
)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
|
||||
@@ -24,6 +24,11 @@ pub(super) fn authentication_gateway_error(error: ClientAuthenticationError) ->
|
||||
|
||||
pub(super) fn map_routing_error(error: crate::validation::RoutingError) -> GatewayError {
|
||||
match error {
|
||||
crate::validation::RoutingError::GroupModelDenied => GatewayError::new(
|
||||
GatewayErrorKind::PolicyDenied,
|
||||
crate::validation::GROUP_MODEL_DENIED_MESSAGE,
|
||||
)
|
||||
.with_client_code(crate::validation::GROUP_MODEL_DENIED_CODE),
|
||||
crate::validation::RoutingError::ModelNotFound {
|
||||
model,
|
||||
mapped_model,
|
||||
|
||||
@@ -21,7 +21,7 @@ use crate::{
|
||||
concurrency::{CapacityWait, ConcurrencyWaitBudget, ConcurrencyWaitQueue},
|
||||
engine::{
|
||||
AttemptCoordinator, EngineError, ExecutionStore, GatewayEngine, ModelRequestId,
|
||||
NewModelRequest, ProbeFailure, ProviderAccountId, UpstreamSendState,
|
||||
ModelRouteScope, NewModelRequest, ProbeFailure, ProviderAccountId, UpstreamSendState,
|
||||
admission::{
|
||||
ClientAdmissionDecision, ClientAdmissionPort, ClientAdmissionRejection,
|
||||
ClientAdmissionRequest,
|
||||
@@ -62,7 +62,7 @@ use crate::{
|
||||
policy::{ClientApiKeyId, ClientPolicy},
|
||||
provider_ports::{ProviderSessionAffinityPort, ProviderStoreErrorKind},
|
||||
routing::{
|
||||
FrozenAccountScope, ProviderCatalogUnavailable, ProviderModelContent,
|
||||
DirectAccountModel, FrozenAccountScope, ProviderCatalogUnavailable, ProviderModelContent,
|
||||
PublicModelDescriptor, PublicModelId, RoutingContext, RuntimeSnapshot, UpstreamModelId,
|
||||
request_settings::RequestSettings,
|
||||
},
|
||||
@@ -478,9 +478,10 @@ impl DefaultExecutionService {
|
||||
let budget_key_id = request.client.policy.key_id().clone();
|
||||
let mut entered_execution = false;
|
||||
let result = async {
|
||||
crate::engine::image_generation::check_image_generation(
|
||||
authorization.account_scope.allows_image_generation(),
|
||||
crate::engine::group_permissions::check_group_permissions(
|
||||
&authorization.account_scope,
|
||||
&request.operation,
|
||||
&[],
|
||||
)?;
|
||||
if authorization.cancellation.is_cancelled() {
|
||||
return Err(GatewayError::new(
|
||||
@@ -507,6 +508,48 @@ impl DefaultExecutionService {
|
||||
}
|
||||
routing_context.required_provider = Some(provider);
|
||||
}
|
||||
let continuation = match request.metadata.previous_response_id.as_ref() {
|
||||
Some(previous) => {
|
||||
let resolve = self
|
||||
.continuation
|
||||
.resolve(request.client.policy.key_id(), previous)
|
||||
.fuse();
|
||||
let timeout = Delay::new(COORDINATION_TIMEOUT).fuse();
|
||||
pin_mut!(resolve, timeout);
|
||||
let pin = select_biased! {
|
||||
result = resolve => match result {
|
||||
Ok(pin) => pin,
|
||||
Err(error)
|
||||
if error.kind() == NativeContinuationStoreErrorKind::Unavailable =>
|
||||
{
|
||||
tracing::debug!(request_id = request_id.as_str(), %error, "Continuation affinity 查询失败,退化为外部续接");
|
||||
None
|
||||
}
|
||||
Err(error)
|
||||
if error.kind() == NativeContinuationStoreErrorKind::OwnershipMismatch =>
|
||||
{
|
||||
return Err(GatewayError::new(GatewayErrorKind::PolicyDenied, "Continuation does not belong to this client API key."));
|
||||
}
|
||||
Err(error) => {
|
||||
tracing::warn!(request_id = request_id.as_str(), %error, "Continuation affinity 记录无效,已拒绝续接");
|
||||
return Err(GatewayError::new(GatewayErrorKind::Internal, "Continuation state is invalid."));
|
||||
}
|
||||
},
|
||||
_ = timeout => None,
|
||||
};
|
||||
Some(match pin {
|
||||
Some(pin) if !pin.matches_client(request.client.policy.key_id()) => {
|
||||
return Err(GatewayError::new(GatewayErrorKind::PolicyDenied, "Continuation does not belong to this client API key."));
|
||||
}
|
||||
Some(pin) if !authorization.account_scope.allows(pin.account()) => {
|
||||
return Err(GatewayError::new(GatewayErrorKind::PolicyDenied, "Continuation account is outside the client account scope."));
|
||||
}
|
||||
Some(pin) => ContinuationBinding::Pinned(pin),
|
||||
None => ContinuationBinding::External(previous.clone()),
|
||||
})
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
let account_scope = Arc::clone(&authorization.account_scope);
|
||||
let plan = match &request.target {
|
||||
ExecutionTarget::ProviderEndpoint {
|
||||
@@ -573,65 +616,66 @@ impl DefaultExecutionService {
|
||||
}
|
||||
}
|
||||
}
|
||||
request.client.snapshot.plan(
|
||||
&target_model,
|
||||
&request.operation,
|
||||
account_scope,
|
||||
&target_context,
|
||||
)
|
||||
let pin = continuation.as_ref().and_then(ContinuationBinding::pinned);
|
||||
let extension_route = ModelRouteScope {
|
||||
client_api_key: request.client.policy.key_id(),
|
||||
account_groups: &account_group_ids,
|
||||
extensions: &authorization.extension_scope,
|
||||
model: public_model.as_str(),
|
||||
};
|
||||
let direct = request.client.snapshot
|
||||
.available_provider_kinds(&account_scope, &target_context)
|
||||
.find_map(|kind| {
|
||||
let provider = self.providers.get(kind)?;
|
||||
let previous = pin.filter(|pin| pin.provider() == kind)
|
||||
.and_then(NativeContinuationPin::session_state);
|
||||
let model = provider.direct_account_model(
|
||||
&target_model,
|
||||
&request.operation,
|
||||
previous,
|
||||
request.client.snapshot.settings(),
|
||||
)?;
|
||||
middleware.as_ref()
|
||||
.is_none_or(|plan| plan.preserves_model_bindings(extension_route, kind, &model))
|
||||
.then(|| (kind.clone(), model))
|
||||
});
|
||||
if let Some((kind, model)) = direct {
|
||||
let account_scope = match authorization.required_account.as_ref() {
|
||||
Some(id) => Arc::new(account_scope.restricted_to(&Default::default(), &[id.clone()].into())),
|
||||
None => account_scope,
|
||||
};
|
||||
request.client.snapshot.plan_with_direct_account_model(
|
||||
&target_model,
|
||||
&request.operation,
|
||||
account_scope,
|
||||
&target_context,
|
||||
DirectAccountModel {
|
||||
provider: &kind,
|
||||
model: &model,
|
||||
continuation_owner: pin.map(NativeContinuationPin::account),
|
||||
},
|
||||
|account| upstream_adapters.as_ref().is_none_or(|plan| {
|
||||
plan.uses_native_upstream(extension_route, &kind, account)
|
||||
}),
|
||||
)
|
||||
} else {
|
||||
request.client.snapshot.plan(
|
||||
&target_model,
|
||||
&request.operation,
|
||||
account_scope,
|
||||
&target_context,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
.map_err(map_routing_error)?;
|
||||
let continuation = match request.metadata.previous_response_id.as_ref() {
|
||||
Some(previous) => {
|
||||
let resolve = self
|
||||
.continuation
|
||||
.resolve(request.client.policy.key_id(), previous)
|
||||
.fuse();
|
||||
let timeout = Delay::new(COORDINATION_TIMEOUT).fuse();
|
||||
pin_mut!(resolve, timeout);
|
||||
let pin = select_biased! {
|
||||
result = resolve => match result {
|
||||
Ok(pin) => pin,
|
||||
Err(error)
|
||||
if error.kind() == NativeContinuationStoreErrorKind::Unavailable =>
|
||||
{
|
||||
tracing::debug!(request_id = request_id.as_str(), %error, "Continuation affinity 查询失败,退化为外部续接");
|
||||
None
|
||||
}
|
||||
Err(error)
|
||||
if error.kind() == NativeContinuationStoreErrorKind::OwnershipMismatch =>
|
||||
{
|
||||
return Err(GatewayError::new(GatewayErrorKind::PolicyDenied, "Continuation does not belong to this client API key."));
|
||||
}
|
||||
Err(error) => {
|
||||
tracing::warn!(request_id = request_id.as_str(), %error, "Continuation affinity 记录无效,已拒绝续接");
|
||||
return Err(GatewayError::new(GatewayErrorKind::Internal, "Continuation state is invalid."));
|
||||
}
|
||||
},
|
||||
_ = timeout => None,
|
||||
};
|
||||
Some(match pin {
|
||||
Some(pin) if !pin.matches_client(request.client.policy.key_id()) => {
|
||||
return Err(GatewayError::new(GatewayErrorKind::PolicyDenied, "Continuation does not belong to this client API key."));
|
||||
}
|
||||
Some(pin) if !authorization.account_scope.allows(pin.account()) => {
|
||||
return Err(GatewayError::new(GatewayErrorKind::PolicyDenied, "Continuation account is outside the client account scope."));
|
||||
}
|
||||
Some(pin)
|
||||
if !plan.candidates().iter().any(|candidate| candidate.provider() == pin.provider()) =>
|
||||
{
|
||||
return Err(GatewayError::new(GatewayErrorKind::NoAvailableProvider, "Continuation provider is not available."));
|
||||
}
|
||||
Some(pin) => {
|
||||
attach_continuation_session_state(&mut request.operation, &pin);
|
||||
ContinuationBinding::Pinned(pin)
|
||||
}
|
||||
None => ContinuationBinding::External(previous.clone()),
|
||||
})
|
||||
if let Some(pin) = continuation.as_ref().and_then(ContinuationBinding::pinned) {
|
||||
if !plan.candidates().iter().any(|candidate| candidate.provider() == pin.provider()
|
||||
&& candidate.account_scope().allows(pin.account())) {
|
||||
return Err(GatewayError::new(GatewayErrorKind::NoAvailableProvider, "Continuation provider is not available."));
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
attach_continuation_session_state(&mut request.operation, pin);
|
||||
}
|
||||
if start_guard.is_none() {
|
||||
start_guard = Some(
|
||||
self.prepare_execution_start(&request, &request_id, &mut authorization)
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user