mirror of
https://github.com/zyycn/codex-proxy-rs.git
synced 2026-11-21 16:01:32 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ba2afe67d0 | ||
|
|
976359ad26 | ||
|
|
832858dade | ||
|
|
4d4c1e48ce | ||
|
|
9f873f5ac2 | ||
|
|
4bc59f32a1 | ||
|
|
4c5d2bc97e | ||
|
|
5033d9f19a | ||
|
|
68b297fc88 | ||
|
|
1b96c775b2 | ||
|
|
b9ccc0b702 | ||
|
|
f9e364860a | ||
|
|
ceeb68ba82 | ||
|
|
2abbfb1fac | ||
|
|
5b7f307bb2 | ||
|
|
e64fb2d955 | ||
|
|
7ee9ea8b63 | ||
|
|
ce12ba3f43 | ||
|
|
ac4ea71cd2 | ||
|
|
77de86e0b6 | ||
|
|
644296bf66 | ||
|
|
367a62bd7d | ||
|
|
62f321c82b | ||
|
|
c40e2f524e | ||
|
|
0f5bf03e13 | ||
|
|
28c8c536c5 | ||
|
|
4ee9abf9d2 | ||
|
|
779ccf4e6c | ||
|
|
d13a4a2138 | ||
|
|
a0e7f38f7f | ||
|
|
41f97ac836 | ||
|
|
975b73b33b | ||
|
|
d787195f4e | ||
|
|
8681d6851e | ||
|
|
d99ecf9aaf | ||
|
|
8f40bfda7c | ||
|
|
72215fe62a | ||
|
|
f61995e61d | ||
|
|
f85d80d756 | ||
|
|
e1f84c210b | ||
|
|
0e02a056b5 | ||
|
|
1eac929e9d | ||
|
|
9b4a042be5 | ||
|
|
3bd042181e | ||
|
|
2cae28d7f4 | ||
|
|
bab781cbe0 |
@@ -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 和镜像已生成也不表示用户的运行实例已经升级。
|
||||
|
||||
@@ -125,7 +125,7 @@ jobs:
|
||||
run: cargo clippy --all-targets --all-features --locked -- -D warnings
|
||||
|
||||
- name: Setup nextest
|
||||
uses: taiki-e/install-action@3a3daafd3b7f23b8f65d9f5ba2db203fb59b79e2 # v2.90.2
|
||||
uses: taiki-e/install-action@cfdb7ed4ccc7c7dce37759eae89dc5674b469b39 # v2.90.3
|
||||
with:
|
||||
tool: cargo-nextest@0.9.143
|
||||
fallback: none
|
||||
|
||||
@@ -115,7 +115,7 @@ jobs:
|
||||
cache-bin: false
|
||||
save-if: ${{ github.event_name != 'pull_request' }}
|
||||
- name: Setup nextest
|
||||
uses: taiki-e/install-action@3a3daafd3b7f23b8f65d9f5ba2db203fb59b79e2 # v2.90.2
|
||||
uses: taiki-e/install-action@cfdb7ed4ccc7c7dce37759eae89dc5674b469b39 # v2.90.3
|
||||
with:
|
||||
tool: cargo-nextest@0.9.143
|
||||
fallback: none
|
||||
|
||||
@@ -34,7 +34,7 @@ jobs:
|
||||
ref: ${{ inputs.ref || github.event.pull_request.head.sha || github.sha }}
|
||||
|
||||
- name: Install cargo-audit
|
||||
uses: taiki-e/install-action@3a3daafd3b7f23b8f65d9f5ba2db203fb59b79e2 # v2.90.2
|
||||
uses: taiki-e/install-action@cfdb7ed4ccc7c7dce37759eae89dc5674b469b39 # v2.90.3
|
||||
with:
|
||||
tool: cargo-audit@0.22.2
|
||||
fallback: none
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
.env
|
||||
.env.*
|
||||
!.env.example
|
||||
!/frontend/.env
|
||||
|
||||
# Editors and operating systems
|
||||
/.idea/
|
||||
|
||||
@@ -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 说明与该场景相关的原因,有可行替代办法时再提。问题已答清即可结束
|
||||
- 排障先看原帖、已有材料和自己能核实的实现或发布记录。回复只附有助于理解结论或采取行动的排查信息,如相关版本、复现结果或脱敏片段。区分推测和实测
|
||||
- 只有缺失信息会改变判断或处理方式,且只能由对方提供时才追问。说清需要哪一项、用来区分什么情况
|
||||
|
||||
## 问题与方案依据
|
||||
|
||||
|
||||
Generated
+92
-60
@@ -204,7 +204,7 @@ checksum = "82f6aeea286b8eb4dd3431a1be1b59d290ace00f5bfd8e2a159bc2a05e2c1667"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 3.0.9",
|
||||
"syn 3.0.10",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -266,9 +266,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "aws-runtime"
|
||||
version = "1.10.2"
|
||||
version = "1.10.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a0cc2144962cc5e358467b16e3d82981b1d424a7c3d34602be0036103814a1a"
|
||||
checksum = "de8930c11353c72ae54aa24dc94d692df74d59814839cbaf8069d63dbcb4c519"
|
||||
dependencies = [
|
||||
"aws-credential-types",
|
||||
"aws-sigv4",
|
||||
@@ -292,9 +292,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "aws-sdk-s3"
|
||||
version = "1.157.0"
|
||||
version = "1.159.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6ea9794c741520d8baec9462bcd0ad12164d4f5f78d008a8da9d6a0b7222caa8"
|
||||
checksum = "8d5c431c5db2f1f6f536c2a19425575e28273859c6ab399d9ac957f73678c7db"
|
||||
dependencies = [
|
||||
"arc-swap",
|
||||
"aws-credential-types",
|
||||
@@ -318,7 +318,7 @@ dependencies = [
|
||||
"hmac",
|
||||
"http",
|
||||
"http-body",
|
||||
"lru",
|
||||
"lru 0.18.5",
|
||||
"percent-encoding",
|
||||
"regex-lite",
|
||||
"sha2",
|
||||
@@ -328,9 +328,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "aws-sigv4"
|
||||
version = "1.6.4"
|
||||
version = "1.6.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f385da9b7757ed4c8214797ad663ff534fb622006143530aa80e21f8e06ff85c"
|
||||
checksum = "57a4393e6addc3380632887b0998d06ad5f099ab5c478be0a3eeedecf8604161"
|
||||
dependencies = [
|
||||
"aws-credential-types",
|
||||
"aws-smithy-eventstream",
|
||||
@@ -361,9 +361,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "aws-smithy-checksums"
|
||||
version = "0.65.0"
|
||||
version = "0.65.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b67ecd999972b58e67cab052f5129906c08c25883bd0788ceefc55ef97d61307"
|
||||
checksum = "ab2f2d12a431aab3703d55277278bad45f02208569b11506220b95fdc1ef0507"
|
||||
dependencies = [
|
||||
"aws-smithy-http",
|
||||
"aws-smithy-types",
|
||||
@@ -449,18 +449,20 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "aws-smithy-observability"
|
||||
version = "0.3.0"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e86338c869539a581bf161247762a6e87f92c5c075060057b5ed6d06632ed0c"
|
||||
checksum = "a416257d7b1b0f32a517a470a8c1dd354d8c4aa697ccab81a11a1e6cfb7418e3"
|
||||
dependencies = [
|
||||
"aws-smithy-runtime-api",
|
||||
"aws-smithy-types",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aws-smithy-runtime"
|
||||
version = "1.16.2"
|
||||
version = "1.16.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4cb80fb19c084da5fca9d797845735e184445e5b958599440d67529bf6ef1b64"
|
||||
checksum = "5bc688673e282f42d3152dcd9cacc1a9a75734d7cd39d02e58989276fc750f3c"
|
||||
dependencies = [
|
||||
"aws-smithy-async",
|
||||
"aws-smithy-http",
|
||||
@@ -481,9 +483,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "aws-smithy-runtime-api"
|
||||
version = "1.19.2"
|
||||
version = "1.21.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8c4e85cffe9944b10d576b075432bd8f98dbc081e8681cb37d5b2d87dc27bc84"
|
||||
checksum = "0467a2414e84b1b01e37452896fb3562b93926e81c619eba6ecf27ce9e682a0e"
|
||||
dependencies = [
|
||||
"aws-smithy-async",
|
||||
"aws-smithy-runtime-api-macros",
|
||||
@@ -509,9 +511,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "aws-smithy-schema"
|
||||
version = "1.1.2"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9543fc722ed4865ab29fb66bd7ed0bef7b029d265ba8c587144511e30006d431"
|
||||
checksum = "f7ef30d39e131651359b60a1c560de9a9502a4c3754172ab2d6e8734fe21bcba"
|
||||
dependencies = [
|
||||
"aws-smithy-runtime-api",
|
||||
"aws-smithy-types",
|
||||
@@ -520,9 +522,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "aws-smithy-types"
|
||||
version = "1.9.1"
|
||||
version = "1.9.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf183d70774ea2cfcc101ca74a8884e4c76be8b9df65cd303f6a1175c88e8f80"
|
||||
checksum = "aff4bf014c512840b2bba80a20417b723cbd6a0a65c02e2d81f50b4268aedfc7"
|
||||
dependencies = [
|
||||
"base64-simd",
|
||||
"bytes",
|
||||
@@ -532,7 +534,6 @@ dependencies = [
|
||||
"http-body",
|
||||
"http-body-util",
|
||||
"itoa",
|
||||
"num-integer",
|
||||
"pin-project-lite",
|
||||
"ryu",
|
||||
"serde",
|
||||
@@ -870,7 +871,7 @@ dependencies = [
|
||||
"heck",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 3.0.9",
|
||||
"syn 3.0.10",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -924,7 +925,7 @@ dependencies = [
|
||||
"provider-xai",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"syn 3.0.9",
|
||||
"syn 3.0.10",
|
||||
"tempfile",
|
||||
"thiserror",
|
||||
"tokio",
|
||||
@@ -992,6 +993,15 @@ version = "0.10.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a6ef517f0926dd24a1582492c791b6a4818a4d94e789a334894aa15b0d12f55c"
|
||||
|
||||
[[package]]
|
||||
name = "constant_time_eq"
|
||||
version = "0.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9eb6feb175df65d0a3ccd3e73484e940d0ac664e8034f0519fd49a93db3071d9"
|
||||
dependencies = [
|
||||
"rustversion",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cookie"
|
||||
version = "0.18.2"
|
||||
@@ -1098,9 +1108,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "crc32fast"
|
||||
version = "1.5.4"
|
||||
version = "1.5.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5f97d3416d5337d15c4b0d78c8c91df922ca143f57ec20e2d1c188333047ef04"
|
||||
checksum = "2b929dd2a135771f40afabd1073f85eed59c800b7c61901e5c254270497dad64"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
@@ -1468,6 +1478,17 @@ dependencies = [
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fancy-regex"
|
||||
version = "0.20.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0a2e16b9a584b602f1e0e5e22c30ca0906786088c27500653e432d086def8b53"
|
||||
dependencies = [
|
||||
"bit-set",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fastrand"
|
||||
version = "2.4.1"
|
||||
@@ -1511,9 +1532,9 @@ checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582"
|
||||
|
||||
[[package]]
|
||||
name = "flate2"
|
||||
version = "1.1.10"
|
||||
version = "1.1.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6e634e2e0ebac1ee034020da1ca582e17ffe4e0f5e985823721e168928136dcb"
|
||||
checksum = "626d4928326b646e70f65ed36fc7cf9f206bdb68a0019ee7c6b4709da4e084c4"
|
||||
dependencies = [
|
||||
"crc32fast",
|
||||
"miniz_oxide 0.9.1",
|
||||
@@ -1671,7 +1692,7 @@ checksum = "9fb9654ba8355388abeb8dcb4fc62f511300867002afc858860463bdd9fe0c44"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 3.0.9",
|
||||
"syn 3.0.10",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1735,7 +1756,7 @@ dependencies = [
|
||||
"serde_json",
|
||||
"sha2",
|
||||
"subtle",
|
||||
"syn 3.0.9",
|
||||
"syn 3.0.10",
|
||||
"tempfile",
|
||||
"thiserror",
|
||||
"tokio",
|
||||
@@ -2526,15 +2547,15 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "jsonschema"
|
||||
version = "0.60.2"
|
||||
version = "0.61.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a42a11b47492e6a2cc76e9c7bcc8077776cb31dac444e0795d7f61bcc6441c08"
|
||||
checksum = "0e37f27df77c7887edbc5f5a59ecd8e5ec4a669319266ec1b61068667a603183"
|
||||
dependencies = [
|
||||
"ahash",
|
||||
"bytecount",
|
||||
"data-encoding",
|
||||
"email_address",
|
||||
"fancy-regex",
|
||||
"fancy-regex 0.20.2",
|
||||
"fraction",
|
||||
"getrandom 0.3.4",
|
||||
"itoa",
|
||||
@@ -2554,18 +2575,18 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "jsonschema-regex"
|
||||
version = "0.60.2"
|
||||
version = "0.61.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "107aeb6ff14b8deb5610096a8ce5c554ec89806b521147eaeb3cae05b07edb7a"
|
||||
checksum = "7e41151614b6c927e21982be8bef5a26741ae5a0ae6a781eedee2aa930a980e6"
|
||||
dependencies = [
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jsonschema-value"
|
||||
version = "0.60.2"
|
||||
version = "0.61.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e281d6b177bb4834f2ffd6f92aa0443cffe29c996b251c8d94eb6f456186feba"
|
||||
checksum = "1515b9a91134ef67d7bac95c2d76fbc7e383c4c55ee544204ceec8088f881fef"
|
||||
dependencies = [
|
||||
"ahash",
|
||||
"bytecount",
|
||||
@@ -2660,9 +2681,18 @@ checksum = "0ceec5bc11778974d1bcb055b18002eba7f4b3518b6a0081b3af5f21666da9ad"
|
||||
|
||||
[[package]]
|
||||
name = "lru"
|
||||
version = "0.18.3"
|
||||
version = "0.18.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0d317b4b9eb398e6acce275758ec6125535505e7a146fb1a9b8bda2451b0ff4c"
|
||||
checksum = "ef9ac18847474e638e3702b76c65d4eb93428471a74778ef0f1be711717f89b5"
|
||||
dependencies = [
|
||||
"hashbrown 0.17.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lru"
|
||||
version = "0.19.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4a9f71e3591c7ec44d3e563e87bf14ddc2bada8519dca046e5b0e560cd004e2d"
|
||||
dependencies = [
|
||||
"hashbrown 0.17.1",
|
||||
]
|
||||
@@ -3308,7 +3338,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2bfe0f4c752e450fc2faf62654f1c134747922825d5b04ca717b8874f41a40c0"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"syn 3.0.9",
|
||||
"syn 3.0.10",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3374,7 +3404,7 @@ dependencies = [
|
||||
"hyper",
|
||||
"hyper-rustls",
|
||||
"hyper-util",
|
||||
"lru",
|
||||
"lru 0.19.0",
|
||||
"native-tls",
|
||||
"openssl",
|
||||
"openssl-sys",
|
||||
@@ -3492,9 +3522,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "quinn-proto"
|
||||
version = "0.11.18"
|
||||
version = "0.11.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a9746dbde176634f4f2f1faf2404e30a31b2bc1e9cafb5329c95d8177a18c9fc"
|
||||
checksum = "0e750cca55fe4f0439a15d0bb529da9651e79993e8e72c61a899a36d462befbe"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"getrandom 0.4.3",
|
||||
@@ -3514,9 +3544,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "quinn-udp"
|
||||
version = "0.5.15"
|
||||
version = "0.5.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "35a133f956daabe89a61a685c2649f13d82d5aa4bd5d12d1277e1072a21c0694"
|
||||
checksum = "af66907df18639dcf4db56ca65490cabc4b27a97dbadd96f2926cca73298f016"
|
||||
dependencies = [
|
||||
"cfg_aliases",
|
||||
"libc",
|
||||
@@ -3673,14 +3703,14 @@ checksum = "92ecd8964f8453721699a1ed72037b0db49ce2f5a5138486ee89bed6f67cdf3a"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 3.0.9",
|
||||
"syn 3.0.10",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "referencing"
|
||||
version = "0.60.2"
|
||||
version = "0.61.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f42818036d3b37cda6bd4c494543158a36e431cb717015ef7c93acc6ba214d3b"
|
||||
checksum = "a028057ef852c58a42d75e09da87b5e733f4030f4c7411728955e4f87203a577"
|
||||
dependencies = [
|
||||
"ahash",
|
||||
"fluent-uri",
|
||||
@@ -3827,9 +3857,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "1.1.4"
|
||||
version = "1.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190"
|
||||
checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"errno",
|
||||
@@ -4043,7 +4073,7 @@ checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 3.0.9",
|
||||
"syn 3.0.10",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -4525,9 +4555,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "3.0.9"
|
||||
version = "3.0.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b"
|
||||
checksum = "4ae9ccbea3a7b0b09443036490c8f34a8b1b760fd345e5c0dae0aac718faea79"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@@ -4625,7 +4655,7 @@ checksum = "fe5197923287db20a58125f0bc85c062f7f2c892de97b18c356f9efb14b28524"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 3.0.9",
|
||||
"syn 3.0.10",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -4646,7 +4676,7 @@ dependencies = [
|
||||
"anyhow",
|
||||
"base64 0.22.1",
|
||||
"bstr",
|
||||
"fancy-regex",
|
||||
"fancy-regex 0.19.2",
|
||||
"regex",
|
||||
"rustc-hash",
|
||||
]
|
||||
@@ -5110,9 +5140,9 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "uuid"
|
||||
version = "1.29.0"
|
||||
version = "1.29.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4a0e2d11620c50695fe12bbbfde96b3477fe606e7284c22d505c12ce51214514"
|
||||
checksum = "bed99139f7dac9b5cfe5f17be4d66dc7079a34dee1d74bcc77217119240e267c"
|
||||
dependencies = [
|
||||
"getrandom 0.4.3",
|
||||
"js-sys",
|
||||
@@ -5715,10 +5745,11 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "zip"
|
||||
version = "9.4.0"
|
||||
version = "9.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3cec733c9820347a592f6375c3c0089b479ba0a0b1cbf3dd1958b02959b7c033"
|
||||
checksum = "e3476fd497ac8af5b1ec7b51acab7bef380f7e8dd8e0487595bafd839417a302"
|
||||
dependencies = [
|
||||
"constant_time_eq",
|
||||
"crc32fast",
|
||||
"flate2",
|
||||
"getrandom 0.4.3",
|
||||
@@ -5728,13 +5759,14 @@ dependencies = [
|
||||
"nix",
|
||||
"typed-path",
|
||||
"windows-sys 0.61.2",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zlib-rs"
|
||||
version = "0.6.6"
|
||||
version = "0.6.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b142a20ec14a91d5bc708c1dc21b080c550113d8aa77afa29635673a65dd02c5"
|
||||
checksum = "b268e58e7c693d7c271f93ffc4ba3b380412554231c85bf61ca7af91042a4112"
|
||||
|
||||
[[package]]
|
||||
name = "zmij"
|
||||
|
||||
@@ -81,6 +81,7 @@ fn core_value_owners_do_not_depend_on_execution_or_routing() {
|
||||
"account/selection.rs" => Some(&["account", "concurrency", "identity", "validation"]),
|
||||
"concurrency.rs" => Some(&["error"]),
|
||||
"account/store.rs" => Some(&["account", "error", "identity", "validation"]),
|
||||
"account/proxy.rs" => Some(&["upstream"]),
|
||||
path if path.starts_with("policy/") => {
|
||||
Some(&["account", "identity", "policy", "validation"])
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ pub use gateway_core::account::{
|
||||
/// 账号可用的管理操作
|
||||
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct ProviderAccountCapabilities {
|
||||
pub quota_limits_supported: bool,
|
||||
pub quota_settings_supported: bool,
|
||||
pub quota_credits: bool,
|
||||
pub quota: bool,
|
||||
pub quota_refresh: bool,
|
||||
@@ -113,7 +113,7 @@ pub enum AccountGroupFilter {
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct AccountRecord {
|
||||
pub quota_paused: bool,
|
||||
pub quota_limits: gateway_core::account::AccountQuotaLimits,
|
||||
pub quota_settings: gateway_core::account::AccountQuotaSettings,
|
||||
pub auto_use_quota_credits: bool,
|
||||
pub id: String,
|
||||
pub provider_kind: ProviderKind,
|
||||
@@ -254,7 +254,7 @@ pub struct AccountUpdateResult {
|
||||
/// 仅修改显式字段的批量账号设置命令
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct BatchUpdateAccounts {
|
||||
pub quota_limits: Option<gateway_core::account::AccountQuotaLimits>,
|
||||
pub quota_settings: Option<gateway_core::account::AccountQuotaSettings>,
|
||||
pub auto_use_quota_credits: Option<bool>,
|
||||
pub account_ids: Vec<String>,
|
||||
pub enabled: Option<bool>,
|
||||
@@ -268,6 +268,8 @@ pub struct BatchUpdateAccounts {
|
||||
/// 已按拟提交策略重算的账号暂停结果,以快照版本防止旧判断覆盖新观测
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct PreparedQuotaPause {
|
||||
pub settings: gateway_core::account::AccountQuotaSettings,
|
||||
pub previous_settings: gateway_core::account::AccountQuotaSettings,
|
||||
pub observed_at: Option<DateTime<Utc>>,
|
||||
pub revision: Revision,
|
||||
pub paused: bool,
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,7 @@ impl RetentionPolicy {
|
||||
audit_days: u32,
|
||||
) -> Result<(), &'static str> {
|
||||
for (valid, field) in [
|
||||
((1..=31).contains(&usage_days), "usage_retention_days"),
|
||||
(usage_days > 0, "usage_retention_days"),
|
||||
(ops_days > 0, "ops_event_retention_days"),
|
||||
(audit_days > 0, "audit_retention_days"),
|
||||
] {
|
||||
@@ -38,7 +38,7 @@ impl RetentionPolicy {
|
||||
pub fn try_new(usage_days: u32, ops_days: u32, audit_days: u32) -> Result<Self, AdminError> {
|
||||
if Self::validate_values(usage_days, ops_days, audit_days).is_err() {
|
||||
return Err(AdminError::invalid(
|
||||
"请求保留期必须为 1~31 天,事件与审计保留期必须为正数",
|
||||
"请求、事件与审计保留期必须为正整数天数",
|
||||
));
|
||||
}
|
||||
Ok(Self {
|
||||
|
||||
@@ -63,6 +63,7 @@ pub struct RuntimeSettingsValues {
|
||||
pub openai_session_affinity_ttl_hours: u32,
|
||||
pub responses_max_decompressed_body_bytes: u64,
|
||||
pub smart_scheduling: gateway_core::account::SmartSchedulingConfig,
|
||||
pub quota_reset_window_weights: gateway_core::account::ResetWindowWeights,
|
||||
pub min_codex_desktop_version: Option<String>,
|
||||
pub min_codex_cli_version: Option<String>,
|
||||
pub usage_retention_days: u32,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -349,6 +349,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,
|
||||
|
||||
@@ -180,6 +180,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,
|
||||
|
||||
@@ -759,7 +759,7 @@ impl AccountsService for DefaultAccountsService {
|
||||
self.accounts
|
||||
.batch_update_accounts(
|
||||
BatchUpdateAccounts {
|
||||
quota_limits: None,
|
||||
quota_settings: None,
|
||||
auto_use_quota_credits: None,
|
||||
account_ids: vec![account_id.to_string()],
|
||||
enabled: Some(true),
|
||||
@@ -846,20 +846,19 @@ impl AccountsService for DefaultAccountsService {
|
||||
let mut pauses = BTreeMap::new();
|
||||
for account_id in &account_ids {
|
||||
let (mut item, provider) = self.provider_for_account(account_id).await?;
|
||||
if let Some(limits) = &command.quota_limits {
|
||||
if let Some(settings) = &command.quota_settings {
|
||||
if !provider
|
||||
.account_capabilities(account_id, &item.account.authentication_kind)
|
||||
.quota_limits_supported
|
||||
.quota_settings_supported
|
||||
{
|
||||
return Err(AdminError::invalid("该账号不支持通用额度窗口上限"));
|
||||
return Err(AdminError::invalid("该账号不支持额度桶设置"));
|
||||
}
|
||||
let new_keys = limits
|
||||
.keys()
|
||||
.filter(|key| !item.account.quota_limits.contains(key))
|
||||
.map(str::to_owned)
|
||||
.collect::<Vec<_>>();
|
||||
if !limits.is_empty() {
|
||||
item.account.quota_limits = limits.clone();
|
||||
let previous_settings = item.account.quota_settings.clone();
|
||||
let settings = settings.clone();
|
||||
let mut observed_at = None;
|
||||
let mut paused = false;
|
||||
if !settings.is_empty() {
|
||||
item.account.quota_settings = settings.clone();
|
||||
let quota = provider
|
||||
.read_quotas(&[ProviderQuotaReadRequest {
|
||||
account: &item.account,
|
||||
@@ -870,24 +869,31 @@ impl AccountsService for DefaultAccountsService {
|
||||
.next()
|
||||
.ok_or_else(|| AdminError::invalid("缺少账号额度观测结果"))?
|
||||
.map_err(|error| map_provider_error(error, "provider quota"))?;
|
||||
if new_keys.iter().any(|key| {
|
||||
!quota.windows.iter().any(|window| {
|
||||
window.key == *key
|
||||
&& window.local_usage_attribution
|
||||
== QuotaLocalUsageAttribution::AccountWide
|
||||
})
|
||||
}) {
|
||||
return Err(AdminError::invalid("只能设置已观测的通用额度窗口"));
|
||||
for key in settings.keys() {
|
||||
let window = quota.windows.iter().find(|window| window.key == key);
|
||||
if window.is_none() && !previous_settings.contains(key) {
|
||||
return Err(AdminError::invalid("只能设置已观测的额度桶"));
|
||||
}
|
||||
if window.is_some_and(|window| {
|
||||
window.local_usage_attribution
|
||||
!= QuotaLocalUsageAttribution::AccountWide
|
||||
}) {
|
||||
return Err(AdminError::invalid("只能设置通用额度桶"));
|
||||
}
|
||||
}
|
||||
pauses.insert(
|
||||
item.account.id.clone(),
|
||||
crate::model::accounts::PreparedQuotaPause {
|
||||
revision: item.account.credential_revision,
|
||||
observed_at: quota.observed_at,
|
||||
paused: !quota.paused_windows.is_empty(),
|
||||
},
|
||||
);
|
||||
observed_at = quota.observed_at;
|
||||
paused = !quota.paused_windows.is_empty();
|
||||
}
|
||||
pauses.insert(
|
||||
item.account.id.clone(),
|
||||
crate::model::accounts::PreparedQuotaPause {
|
||||
settings,
|
||||
previous_settings,
|
||||
revision: item.account.credential_revision,
|
||||
observed_at,
|
||||
paused,
|
||||
},
|
||||
);
|
||||
}
|
||||
if command.auto_use_quota_credits.is_some()
|
||||
&& !provider
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -3,25 +3,15 @@
|
||||
use gateway_admin::model::retention::{RetentionPolicy, RetentionTarget};
|
||||
|
||||
#[test]
|
||||
fn retention_policy_rejects_invalid_windows_and_preserves_independent_windows() {
|
||||
for (usage, ops, audit) in [
|
||||
(0, 1, 1),
|
||||
(32, 1, 1),
|
||||
(u32::MAX, 1, 1),
|
||||
(1, 0, 1),
|
||||
(1, 1, 0),
|
||||
] {
|
||||
fn retention_policy_rejects_zero_for_each_target() {
|
||||
for (usage, ops, audit) in [(0, 1, 1), (1, 0, 1), (1, 1, 0)] {
|
||||
assert!(RetentionPolicy::try_new(usage, ops, audit).is_err());
|
||||
}
|
||||
let policy = RetentionPolicy::try_new(31, 7, 90).unwrap();
|
||||
assert_eq!(policy.days(RetentionTarget::ModelRequests), 31);
|
||||
assert_eq!(policy.days(RetentionTarget::OpsEvents), 7);
|
||||
assert_eq!(policy.days(RetentionTarget::AdminAuditEvents), 90);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn request_retention_accepts_short_windows_without_changing_other_targets() {
|
||||
for days in [1, 3, 7, 15, 30, 31] {
|
||||
fn request_retention_accepts_positive_windows_without_changing_other_targets() {
|
||||
for days in [1, 31, 32, 365, 3650, u32::MAX] {
|
||||
let policy = RetentionPolicy::try_new(days, 30, 90).unwrap();
|
||||
assert_eq!(policy.days(RetentionTarget::ModelRequests), days);
|
||||
assert_eq!(policy.days(RetentionTarget::OpsEvents), 30);
|
||||
|
||||
@@ -845,6 +845,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,
|
||||
@@ -1180,6 +1191,7 @@ impl SettingsStore for StaticSettingsStore {
|
||||
openai_session_affinity_ttl_hours: 24,
|
||||
responses_max_decompressed_body_bytes: 64 * 1024 * 1024,
|
||||
smart_scheduling: gateway_core::account::SmartSchedulingConfig::default(),
|
||||
quota_reset_window_weights: gateway_core::account::ResetWindowWeights::default(),
|
||||
min_codex_desktop_version: None,
|
||||
min_codex_cli_version: None,
|
||||
usage_retention_days: 30,
|
||||
@@ -1913,7 +1925,7 @@ async fn accounts_batch_update_should_commit_once_and_notify_each_provider() {
|
||||
.batch_update(
|
||||
&context("batch-update-request"),
|
||||
BatchUpdateAccounts {
|
||||
quota_limits: None,
|
||||
quota_settings: None,
|
||||
auto_use_quota_credits: None,
|
||||
model_access: Default::default(),
|
||||
outbound_proxy: None,
|
||||
@@ -3412,7 +3424,7 @@ pub(super) fn account_record(kind: &str) -> AccountRecord {
|
||||
let now = Utc::now();
|
||||
AccountRecord {
|
||||
quota_paused: false,
|
||||
quota_limits: Default::default(),
|
||||
quota_settings: Default::default(),
|
||||
auto_use_quota_credits: false,
|
||||
outbound_proxy_id: None,
|
||||
outbound_proxy_enabled: true,
|
||||
|
||||
@@ -51,6 +51,7 @@ fn runtime_settings(enabled: bool, probe_enabled: bool) -> RuntimeSettings {
|
||||
openai_session_affinity_ttl_hours: 24,
|
||||
responses_max_decompressed_body_bytes: 64 * 1024 * 1024,
|
||||
smart_scheduling: gateway_core::account::SmartSchedulingConfig::default(),
|
||||
quota_reset_window_weights: gateway_core::account::ResetWindowWeights::default(),
|
||||
min_codex_desktop_version: None,
|
||||
min_codex_cli_version: None,
|
||||
usage_retention_days: 31,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -1432,6 +1432,7 @@ impl SettingsStore for FixtureSettingsStore {
|
||||
openai_session_affinity_ttl_hours: 24,
|
||||
responses_max_decompressed_body_bytes: 64 * 1024 * 1024,
|
||||
smart_scheduling: gateway_core::account::SmartSchedulingConfig::default(),
|
||||
quota_reset_window_weights: gateway_core::account::ResetWindowWeights::default(),
|
||||
min_codex_desktop_version: None,
|
||||
min_codex_cli_version: None,
|
||||
usage_retention_days: 31,
|
||||
|
||||
@@ -257,8 +257,8 @@ fn sdk_settings_contract_matches_host_declarations() {
|
||||
&["RequestLocation"][..],
|
||||
),
|
||||
(
|
||||
"../gateway-core/src/account/smart_scheduling.rs",
|
||||
&["SmartSchedulingValues", "ResetWindowWeights"][..],
|
||||
"../gateway-core/src/account/scheduling.rs",
|
||||
&["SmartSchedulingValues", "ResetWindowValues"][..],
|
||||
),
|
||||
(
|
||||
"../gateway-core/src/metering/pricing.rs",
|
||||
@@ -326,10 +326,10 @@ fn sdk_settings_contract_matches_host_declarations() {
|
||||
}
|
||||
types.push(contract_type(
|
||||
item,
|
||||
if *name == "SmartSchedulingValues" {
|
||||
"SmartSchedulingConfig"
|
||||
} else {
|
||||
name
|
||||
match *name {
|
||||
"SmartSchedulingValues" => "SmartSchedulingConfig",
|
||||
"ResetWindowValues" => "ResetWindowWeights",
|
||||
_ => name,
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
@@ -343,6 +343,8 @@ async fn settings_should_reject_zero_refresh_margin_before_store_call() {
|
||||
openai_session_affinity_ttl_hours: 24,
|
||||
responses_max_decompressed_body_bytes: 64 * 1024 * 1024,
|
||||
smart_scheduling: gateway_core::account::SmartSchedulingConfig::default(),
|
||||
quota_reset_window_weights: gateway_core::account::ResetWindowWeights::default(
|
||||
),
|
||||
min_codex_desktop_version: None,
|
||||
min_codex_cli_version: None,
|
||||
usage_retention_days: 31,
|
||||
|
||||
@@ -117,7 +117,7 @@ pub(super) fn account_view(
|
||||
let (mut quota, refresh_token_expires_at) = account_quota_view(quota, cooldown, now, time);
|
||||
quota.protection_rules = protection_rules;
|
||||
AccountView {
|
||||
quota_limits: account.quota_limits,
|
||||
quota_settings: account.quota_settings,
|
||||
auto_use_quota_credits: account.auto_use_quota_credits,
|
||||
upstream,
|
||||
capabilities: capabilities.into(),
|
||||
|
||||
@@ -61,7 +61,7 @@ pub struct ListQuery {
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct BatchUpdateAccountsRequest {
|
||||
pub quota_limits: Option<gateway_core::account::AccountQuotaLimits>,
|
||||
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>,
|
||||
@@ -119,7 +119,7 @@ impl BatchUpdateAccountsRequest {
|
||||
.map(parse_concurrency_limit)
|
||||
.transpose()?;
|
||||
self.weight.map(parse_account_weight).transpose()?;
|
||||
if self.quota_limits.is_none()
|
||||
if self.quota_settings.is_none()
|
||||
&& self.auto_use_quota_credits.is_none()
|
||||
&& self.enabled.is_none()
|
||||
&& self.concurrency_limit.is_none()
|
||||
@@ -137,7 +137,7 @@ impl BatchUpdateAccountsRequest {
|
||||
pub(super) fn into_command(self) -> Result<BatchUpdateAccounts, WireValidationError> {
|
||||
self.validate()?;
|
||||
Ok(BatchUpdateAccounts {
|
||||
quota_limits: self.quota_limits,
|
||||
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)?,
|
||||
account_ids: self.account_ids,
|
||||
@@ -273,7 +273,7 @@ pub struct AccountSummaryView {
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AccountView {
|
||||
pub quota_limits: gateway_core::account::AccountQuotaLimits,
|
||||
pub quota_settings: gateway_core::account::AccountQuotaSettings,
|
||||
pub auto_use_quota_credits: bool,
|
||||
pub upstream: Option<super::AccountUpstreamInfoView>,
|
||||
pub capabilities: AccountCapabilitiesView,
|
||||
@@ -327,7 +327,7 @@ pub struct AccountCapacityView {
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AccountCapabilitiesView {
|
||||
pub quota_limits_supported: bool,
|
||||
pub quota_settings_supported: bool,
|
||||
pub quota_credits: bool,
|
||||
pub quota: bool,
|
||||
pub quota_refresh: bool,
|
||||
@@ -347,7 +347,7 @@ impl From<gateway_admin::model::accounts::ProviderAccountCapabilities> for Accou
|
||||
subscription: value.subscription,
|
||||
avatar: value.avatar,
|
||||
reset_credits: value.reset_credits,
|
||||
quota_limits_supported: value.quota_limits_supported,
|
||||
quota_settings_supported: value.quota_settings_supported,
|
||||
quota_credits: value.quota_credits,
|
||||
consume_reset_credit: value.consume_reset_credit,
|
||||
}
|
||||
|
||||
@@ -54,6 +54,7 @@ pub struct RuntimeSettingsFields {
|
||||
pub openai_session_affinity_ttl_hours: u32,
|
||||
pub responses_max_decompressed_body_bytes: u64,
|
||||
pub smart_scheduling: gateway_core::account::SmartSchedulingConfig,
|
||||
pub quota_reset_window_weights: gateway_core::account::ResetWindowWeights,
|
||||
pub min_codex_desktop_version: Option<String>,
|
||||
pub min_codex_cli_version: Option<String>,
|
||||
pub usage_retention_days: u64,
|
||||
@@ -77,6 +78,7 @@ pub struct RuntimeSettingsView {
|
||||
pub values: RuntimeSettingsFields,
|
||||
pub config_revision: u64,
|
||||
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>>,
|
||||
@@ -150,6 +152,8 @@ impl From<(RuntimeSettings, crate::time::TimePresenter)> for RuntimeSettingsView
|
||||
provider_request_profiles,
|
||||
model_mappings: wire_model_mappings(settings.model_mappings),
|
||||
smart_scheduling_defaults: gateway_core::account::SmartSchedulingConfig::default(),
|
||||
quota_reset_window_weights_defaults: gateway_core::account::ResetWindowWeights::default(
|
||||
),
|
||||
rotation_strategy: settings.rotation_strategy.as_str().to_owned(),
|
||||
updated_at_display: time.datetime(&settings.updated_at),
|
||||
updated_at: settings.updated_at,
|
||||
@@ -765,6 +769,7 @@ impl RuntimeSettingsFields {
|
||||
openai_session_affinity_ttl_hours: self.openai_session_affinity_ttl_hours,
|
||||
responses_max_decompressed_body_bytes: self.responses_max_decompressed_body_bytes,
|
||||
smart_scheduling: self.smart_scheduling,
|
||||
quota_reset_window_weights: self.quota_reset_window_weights,
|
||||
min_codex_desktop_version: self.min_codex_desktop_version,
|
||||
min_codex_cli_version: self.min_codex_cli_version,
|
||||
usage_retention_days: u32::try_from(self.usage_retention_days)
|
||||
@@ -808,6 +813,7 @@ impl From<gateway_admin::model::settings::RuntimeSettingsValues> for RuntimeSett
|
||||
openai_session_affinity_ttl_hours: settings.openai_session_affinity_ttl_hours,
|
||||
responses_max_decompressed_body_bytes: settings.responses_max_decompressed_body_bytes,
|
||||
smart_scheduling: settings.smart_scheduling,
|
||||
quota_reset_window_weights: settings.quota_reset_window_weights,
|
||||
min_codex_desktop_version: settings.min_codex_desktop_version,
|
||||
min_codex_cli_version: settings.min_codex_cli_version,
|
||||
usage_retention_days: u64::from(settings.usage_retention_days),
|
||||
|
||||
@@ -72,6 +72,10 @@ pub(crate) fn client_error_code(code: &str) -> &str {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn is_retry_after_header(name: &str) -> bool {
|
||||
name.eq_ignore_ascii_case("retry-after") || name.eq_ignore_ascii_case("retry-after-ms")
|
||||
}
|
||||
|
||||
/// 为 HTTP 错误或流内失败保留原有字段,仅投影客户端需要的重试信号
|
||||
pub(crate) fn capacity_error_for_client(data: &Value) -> Option<Value> {
|
||||
const CODE_PATHS: [&str; 2] = ["/error/code", "/response/error/code"];
|
||||
@@ -87,6 +91,12 @@ pub(crate) fn capacity_error_for_client(data: &Value) -> Option<Value> {
|
||||
return None;
|
||||
}
|
||||
let mut projected = data.clone();
|
||||
// 容量等待与换号已由网关执行;旧账号的等待提示不能再次阻塞客户端的新一轮路由
|
||||
for path in ["/headers", "/error/headers", "/response/error/headers"] {
|
||||
if let Some(Value::Object(headers)) = projected.pointer_mut(path) {
|
||||
headers.retain(|name, _| !is_retry_after_header(name));
|
||||
}
|
||||
}
|
||||
for path in CODE_PATHS {
|
||||
if let Some(code) = projected.pointer_mut(path)
|
||||
&& capacity_code(code)
|
||||
|
||||
@@ -15,7 +15,7 @@ use serde_json::{Value, json};
|
||||
|
||||
pub use gateway_core::engine::execution::gateway_error_from_engine;
|
||||
|
||||
use crate::client_error::{ProtocolErrorBody, capacity_error_for_client};
|
||||
use crate::client_error::{ProtocolErrorBody, capacity_error_for_client, is_retry_after_header};
|
||||
|
||||
pub use crate::client_error::{
|
||||
gateway_error_contract, gateway_error_response, openai_error_response,
|
||||
@@ -102,7 +102,9 @@ pub fn model_not_found_response() -> (StatusCode, Json<Value>) {
|
||||
/// 在下游尚未提交时交付 Provider 的 HTTP 失败响应,并应用客户端容量恢复合同
|
||||
pub fn engine_error_response(error: &EngineError) -> Response {
|
||||
let capacity_unavailable = matches!(error, EngineError::Provider(error)
|
||||
if error.kind() == ProviderErrorKind::UpstreamCapacityUnavailable);
|
||||
if error.kind() == ProviderErrorKind::UpstreamCapacityUnavailable
|
||||
&& error.client_visible_upstream_error().and_then(|detail| detail.code())
|
||||
!= Some("previous_response_not_found"));
|
||||
if let EngineError::Provider(error) = error
|
||||
&& let Some(upstream) = error.client_visible_upstream_response()
|
||||
&& let Ok(status) = StatusCode::from_u16(upstream.status())
|
||||
@@ -110,6 +112,7 @@ pub fn engine_error_response(error: &EngineError) -> Response {
|
||||
let projected = serde_json::from_slice::<Value>(upstream.body())
|
||||
.ok()
|
||||
.and_then(|body| capacity_error_for_client(&body));
|
||||
let suppress_retry_after = capacity_unavailable || projected.is_some();
|
||||
let mut response = match projected {
|
||||
Some(body) => {
|
||||
let mut response = Response::new(Body::from(body.to_string()));
|
||||
@@ -132,6 +135,9 @@ pub fn engine_error_response(error: &EngineError) -> Response {
|
||||
response.headers_mut().insert(CONTENT_TYPE, content_type);
|
||||
}
|
||||
for header in upstream.headers() {
|
||||
if suppress_retry_after && is_retry_after_header(header.name()) {
|
||||
continue;
|
||||
}
|
||||
let Ok(name) = HeaderName::from_bytes(header.name().as_bytes()) else {
|
||||
continue;
|
||||
};
|
||||
@@ -140,6 +146,13 @@ pub fn engine_error_response(error: &EngineError) -> Response {
|
||||
};
|
||||
response.headers_mut().append(name, value);
|
||||
}
|
||||
if !response.headers().contains_key("x-request-id")
|
||||
&& !response.headers().contains_key("x-oai-request-id")
|
||||
&& let Some(request_id) = error.upstream_request_id()
|
||||
&& let Ok(value) = HeaderValue::from_str(request_id.as_str())
|
||||
{
|
||||
response.headers_mut().insert("x-request-id", value);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
let mut response = gateway_error_response(&gateway_error_from_engine(error));
|
||||
|
||||
@@ -176,6 +176,7 @@ pub(super) fn initial_engine_error_event(
|
||||
}
|
||||
let status = if provider
|
||||
.is_some_and(|error| error.kind() == ProviderErrorKind::UpstreamCapacityUnavailable)
|
||||
&& gateway.client_error_code() != Some("previous_response_not_found")
|
||||
{
|
||||
StatusCode::SERVICE_UNAVAILABLE
|
||||
} else {
|
||||
|
||||
@@ -413,7 +413,7 @@ fn account_page_item(
|
||||
protection_rules: Vec::new(),
|
||||
account: AccountRecord {
|
||||
quota_paused: false,
|
||||
quota_limits: Default::default(),
|
||||
quota_settings: Default::default(),
|
||||
auto_use_quota_credits: false,
|
||||
outbound_proxy_id: None,
|
||||
outbound_proxy_enabled: true,
|
||||
|
||||
@@ -580,7 +580,7 @@ mod provider {
|
||||
},
|
||||
account: AccountRecord {
|
||||
quota_paused: false,
|
||||
quota_limits: Default::default(),
|
||||
quota_settings: Default::default(),
|
||||
auto_use_quota_credits: false,
|
||||
outbound_proxy_id: None,
|
||||
outbound_proxy_enabled: true,
|
||||
|
||||
@@ -664,6 +664,7 @@ impl SettingsStore for MemorySettingsStore {
|
||||
.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,
|
||||
@@ -1299,6 +1300,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,
|
||||
@@ -1940,6 +1952,7 @@ fn test_runtime_settings() -> RuntimeSettings {
|
||||
openai_session_affinity_ttl_hours: 24,
|
||||
responses_max_decompressed_body_bytes: 64 * 1024 * 1024,
|
||||
smart_scheduling: gateway_core::account::SmartSchedulingConfig::default(),
|
||||
quota_reset_window_weights: gateway_core::account::ResetWindowWeights::default(),
|
||||
min_codex_desktop_version: None,
|
||||
min_codex_cli_version: None,
|
||||
usage_retention_days: 31,
|
||||
|
||||
@@ -63,6 +63,7 @@ fn update_body() -> Value {
|
||||
"responsesMaxDecompressedBodyBytes": 67108864,
|
||||
"rotationStrategy": "round_robin",
|
||||
"smartScheduling": gateway_core::account::SmartSchedulingConfig::default(),
|
||||
"quotaResetWindowWeights": gateway_core::account::ResetWindowWeights::default(),
|
||||
"minCodexDesktopVersion": "26.825.6671",
|
||||
"minCodexCliVersion": "0.40.0",
|
||||
"usageRetentionDays": 31,
|
||||
@@ -264,12 +265,14 @@ fn error_protection_requires_an_explicit_policy_in_settings_replacement() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn smart_settings_round_trip_and_invalid_updates_leave_the_saved_value_intact() {
|
||||
async fn scheduling_settings_round_trip_and_invalid_updates_leave_the_saved_value_intact() {
|
||||
let fixture = AdminTestFixture::new().await;
|
||||
fixture.auth.insert_session("valid-session");
|
||||
let mut body = update_body();
|
||||
let custom = json!({"loadWeight": 2.0, "quotaWeight": 0.0, "healthWeight": 1.0, "latencyWeight": 0.5, "resetWeight": 1.2, "queueWeight": 2.3, "resetWindowWeights":{"fiveHour":0.0,"weekly":1.5,"monthly":0.0}});
|
||||
body["smartScheduling"] = custom.clone();
|
||||
let windows = json!({"fiveHour":2.5,"weekly":0.0,"monthly":1.0});
|
||||
body["quotaResetWindowWeights"] = windows.clone();
|
||||
let response = app(fixture.state())
|
||||
.oneshot(request(
|
||||
Method::POST,
|
||||
@@ -281,6 +284,7 @@ async fn smart_settings_round_trip_and_invalid_updates_leave_the_saved_value_int
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let response = response_json(response).await;
|
||||
assert_eq!(response["data"]["smartScheduling"], custom);
|
||||
assert_eq!(response["data"]["quotaResetWindowWeights"], windows);
|
||||
assert_eq!(
|
||||
response["data"]["smartSchedulingDefaults"],
|
||||
json!(gateway_core::account::SmartSchedulingConfig::default())
|
||||
@@ -301,7 +305,7 @@ async fn smart_settings_round_trip_and_invalid_updates_leave_the_saved_value_int
|
||||
missing.as_object_mut().unwrap().remove(field);
|
||||
invalid_values.push(missing);
|
||||
}
|
||||
for windows in [
|
||||
let invalid_windows = [
|
||||
json!({}),
|
||||
json!({"weekly":1}),
|
||||
json!(null),
|
||||
@@ -309,41 +313,53 @@ async fn smart_settings_round_trip_and_invalid_updates_leave_the_saved_value_int
|
||||
json!({"fiveHour":-1,"weekly":1,"monthly":0}),
|
||||
json!({"fiveHour":0,"weekly":1,"monthly":10.1}),
|
||||
json!({"fiveHour":0,"weekly":1,"monthly":0,"daily":1}),
|
||||
] {
|
||||
];
|
||||
for windows in &invalid_windows {
|
||||
let mut invalid = custom.clone();
|
||||
invalid["resetWindowWeights"] = windows;
|
||||
invalid["resetWindowWeights"] = windows.clone();
|
||||
invalid_values.push(invalid);
|
||||
}
|
||||
for invalid in invalid_values {
|
||||
body["smartScheduling"] = invalid;
|
||||
for (field, invalid) in invalid_values
|
||||
.into_iter()
|
||||
.map(|value| ("smartScheduling", value))
|
||||
.chain(
|
||||
invalid_windows
|
||||
.into_iter()
|
||||
.map(|value| ("quotaResetWindowWeights", value)),
|
||||
)
|
||||
{
|
||||
let mut invalid_body = body.clone();
|
||||
invalid_body[field] = invalid;
|
||||
let response = app(fixture.state())
|
||||
.oneshot(request(
|
||||
Method::POST,
|
||||
"/api/admin/settings/update",
|
||||
Some(body.clone()),
|
||||
Some(invalid_body),
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
for field in ["smartScheduling", "quotaResetWindowWeights"] {
|
||||
let mut missing = body.clone();
|
||||
missing.as_object_mut().unwrap().remove(field);
|
||||
let response = app(fixture.state())
|
||||
.oneshot(request(
|
||||
Method::POST,
|
||||
"/api/admin/settings/update",
|
||||
Some(missing),
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
body.as_object_mut().unwrap().remove("smartScheduling");
|
||||
let response = app(fixture.state())
|
||||
.oneshot(request(
|
||||
Method::POST,
|
||||
"/api/admin/settings/update",
|
||||
Some(body),
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
|
||||
let response = app(fixture.state())
|
||||
.oneshot(request(Method::GET, "/api/admin/settings", None))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
response_json(response).await["data"]["smartScheduling"],
|
||||
custom
|
||||
);
|
||||
let saved = response_json(response).await;
|
||||
assert_eq!(saved["data"]["smartScheduling"], custom);
|
||||
assert_eq!(saved["data"]["quotaResetWindowWeights"], windows);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -357,11 +373,11 @@ fn settings_request_should_reject_unknown_rotation_strategy() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn short_request_retention_round_trips_and_invalid_days_do_not_replace_it() {
|
||||
async fn request_retention_round_trips_and_invalid_days_do_not_replace_it() {
|
||||
let fixture = AdminTestFixture::new().await;
|
||||
fixture.auth.insert_session("valid-session");
|
||||
let mut body = update_body();
|
||||
for days in [1, 3, 7, 15, 30, 31] {
|
||||
for days in [1, 31, 32, 365, 3650, u32::MAX] {
|
||||
body["usageRetentionDays"] = json!(days);
|
||||
let response = app(fixture.state())
|
||||
.oneshot(request(
|
||||
@@ -378,8 +394,7 @@ async fn short_request_retention_round_trips_and_invalid_days_do_not_replace_it(
|
||||
}
|
||||
for (days, expected_status) in [
|
||||
(json!(0), StatusCode::BAD_REQUEST),
|
||||
(json!(32), StatusCode::BAD_REQUEST),
|
||||
(json!(u32::MAX), StatusCode::BAD_REQUEST),
|
||||
(json!(u64::from(u32::MAX) + 1), StatusCode::BAD_REQUEST),
|
||||
(json!(-1), StatusCode::UNPROCESSABLE_ENTITY),
|
||||
(json!(1.5), StatusCode::UNPROCESSABLE_ENTITY),
|
||||
(json!(null), StatusCode::UNPROCESSABLE_ENTITY),
|
||||
@@ -402,7 +417,7 @@ async fn short_request_retention_round_trips_and_invalid_days_do_not_replace_it(
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
assert_eq!(
|
||||
response_json(response).await["data"]["usageRetentionDays"],
|
||||
31
|
||||
u32::MAX
|
||||
);
|
||||
}
|
||||
|
||||
@@ -423,7 +438,12 @@ fn settings_request_validates_cron_and_allows_disabled_empty_schedule() {
|
||||
(false, "08:00", false),
|
||||
(true, "0 8 * * MON-FRI", true),
|
||||
(true, "60 8 * * *", false),
|
||||
(true, "0 8 * * *;30 13 * * *", true),
|
||||
(true, "0 8 * * *\n30 13 * * *", false),
|
||||
(true, "0 8 * * *;60 13 * * *", false),
|
||||
(false, "0 8 * * *;", false),
|
||||
(true, "0 8 * * *;;30 13 * * *", false),
|
||||
(true, "0 8 * * *\r\n30 13 * * *", false),
|
||||
] {
|
||||
let mut body = update_body();
|
||||
body["accountWarmupEnabled"] = json!(enabled);
|
||||
@@ -436,6 +456,11 @@ fn settings_request_validates_cron_and_allows_disabled_empty_schedule() {
|
||||
assert_eq!(error.field(), "accountWarmupCron");
|
||||
}
|
||||
}
|
||||
let mut oversized = update_body();
|
||||
oversized["accountWarmupCron"] = json!(["* * * * *"; 26].join(";"));
|
||||
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");
|
||||
|
||||
@@ -60,7 +60,8 @@ async fn capacity_errors_offer_client_retry_without_changing_upstream_facts() {
|
||||
for raw_response in [false, true] {
|
||||
let original = json!({"error": {
|
||||
"code": code, "type": "service_unavailable_error", "message": "busy",
|
||||
"param": "model", "future": {"keep": true}
|
||||
"param": "model", "future": {"keep": true},
|
||||
"headers": {"Retry-After":"309", "retry-after-ms":"309000", "x-request-id":"req_capacity"}
|
||||
}});
|
||||
let mut provider = ProviderError::new(
|
||||
ProviderErrorKind::UpstreamCapacityUnavailable,
|
||||
@@ -88,6 +89,10 @@ async fn capacity_errors_offer_client_retry_without_changing_upstream_facts() {
|
||||
Bytes::from_static(b"req_capacity"),
|
||||
),
|
||||
ProviderResponseHeader::new("retry-after", Bytes::from_static(b"7")),
|
||||
ProviderResponseHeader::new(
|
||||
"retry-after-ms",
|
||||
Bytes::from_static(b"7000"),
|
||||
),
|
||||
]),
|
||||
);
|
||||
}
|
||||
@@ -96,7 +101,8 @@ async fn capacity_errors_offer_client_retry_without_changing_upstream_facts() {
|
||||
assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
|
||||
if raw_response {
|
||||
assert_eq!(response.headers()["x-request-id"], "req_capacity");
|
||||
assert_eq!(response.headers()["retry-after"], "7");
|
||||
assert!(!response.headers().contains_key("retry-after"));
|
||||
assert!(!response.headers().contains_key("retry-after-ms"));
|
||||
}
|
||||
let body: Value = serde_json::from_slice(
|
||||
&to_bytes(response.into_body(), 4096)
|
||||
@@ -110,6 +116,7 @@ async fn capacity_errors_offer_client_retry_without_changing_upstream_facts() {
|
||||
if raw_response {
|
||||
let mut expected = original.clone();
|
||||
expected["error"]["code"] = json!("server_error");
|
||||
expected["error"]["headers"] = json!({"x-request-id":"req_capacity"});
|
||||
assert_eq!(body, expected);
|
||||
}
|
||||
let EngineError::Provider(provider) = error else {
|
||||
@@ -138,6 +145,38 @@ async fn capacity_errors_offer_client_retry_without_changing_upstream_facts() {
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn capacity_client_replay_preserves_recovery_status_and_failure_correlation() {
|
||||
use bytes::Bytes;
|
||||
use gateway_core::error::ClientVisibleUpstreamResponse;
|
||||
use gateway_core::upstream::OpaqueUpstreamValue;
|
||||
use std::time::Duration;
|
||||
|
||||
let error = EngineError::Provider(
|
||||
ProviderError::new(
|
||||
ProviderErrorKind::UpstreamCapacityUnavailable,
|
||||
UpstreamSendState::Sent,
|
||||
)
|
||||
.with_retry_after(Duration::from_secs(309))
|
||||
.with_upstream_request_id(OpaqueUpstreamValue::new("req_original_capacity"))
|
||||
.with_client_visible_upstream_error(ClientVisibleUpstreamError::new(
|
||||
"Resend full history.",
|
||||
Some("previous_response_not_found".to_owned()),
|
||||
Some("invalid_request_error".to_owned()),
|
||||
))
|
||||
.with_client_visible_upstream_response(ClientVisibleUpstreamResponse::new(
|
||||
400,
|
||||
Some(b"application/json".to_vec()),
|
||||
Bytes::from_static(br#"{"error":{"code":"previous_response_not_found"}}"#),
|
||||
)),
|
||||
);
|
||||
let response = engine_error_response(&error);
|
||||
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
|
||||
assert_eq!(response.headers()["x-request-id"], "req_original_capacity");
|
||||
assert!(!response.headers().contains_key("retry-after"));
|
||||
assert!(!response.headers().contains_key("retry-after-ms"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn classified_capacity_error_without_special_code_returns_retryable_http_status() {
|
||||
use bytes::Bytes;
|
||||
|
||||
@@ -4,6 +4,7 @@ use axum::{
|
||||
body::{Body, to_bytes},
|
||||
http::{Request, StatusCode},
|
||||
};
|
||||
use base64::{Engine as _, engine::general_purpose::STANDARD};
|
||||
use bytes::Bytes;
|
||||
use futures::{StreamExt as _, future::BoxFuture};
|
||||
use gateway_core::{
|
||||
@@ -122,10 +123,12 @@ async fn native_json_preserves_extensions_and_uses_client_key_authentication() {
|
||||
)]);
|
||||
let trace = session.trace.clone();
|
||||
let (router, execution) = fixture(session).await;
|
||||
let response = router
|
||||
.oneshot(request("/v1/messages", false))
|
||||
.await
|
||||
.unwrap();
|
||||
let mut incoming = request("/v1/messages", false);
|
||||
incoming.headers_mut().insert(
|
||||
"x-claude-code-session-id",
|
||||
"claude-session".parse().unwrap(),
|
||||
);
|
||||
let response = router.oneshot(incoming).await.unwrap();
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let bytes = to_bytes(response.into_body(), usize::MAX).await.unwrap();
|
||||
assert_eq!(bytes, raw);
|
||||
@@ -136,6 +139,15 @@ async fn native_json_preserves_extensions_and_uses_client_key_authentication() {
|
||||
panic!("generate")
|
||||
};
|
||||
assert_eq!(generation.protocol_payload().protocol(), "anthropic");
|
||||
assert!(
|
||||
generation.protocol_payload().context()["opaque_request_headers"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.contains(&json!([
|
||||
"x-claude-code-session-id",
|
||||
STANDARD.encode("claude-session")
|
||||
]))
|
||||
);
|
||||
assert_eq!(
|
||||
generation.protocol_payload().body()["future_extension"]["preserved"],
|
||||
true
|
||||
|
||||
@@ -99,7 +99,7 @@ async fn fixture(sharing: ClientPeerSharing, plaintext: &str) -> (AdminTestFixtu
|
||||
projection: resolve_account_status(&facts, now.into()),
|
||||
account: AccountRecord {
|
||||
quota_paused: false,
|
||||
quota_limits: Default::default(),
|
||||
quota_settings: Default::default(),
|
||||
auto_use_quota_credits: false,
|
||||
id: "acct_private_sentinel".into(),
|
||||
provider_kind: provider,
|
||||
|
||||
@@ -2164,13 +2164,14 @@ async fn streaming_upstream_wire_failure_should_not_be_rewritten_as_a_gateway_er
|
||||
|
||||
#[tokio::test]
|
||||
async fn streaming_upstream_error_event_should_be_translated_to_response_failed_for_sse() {
|
||||
for (error_type, code, client_code, kind, client_status) in [
|
||||
for (error_type, code, client_code, kind, client_status, keeps_retry_after) in [
|
||||
(
|
||||
"service_unavailable_error",
|
||||
"server_is_overloaded",
|
||||
"server_error",
|
||||
ProviderErrorKind::UpstreamCapacityUnavailable,
|
||||
503,
|
||||
false,
|
||||
),
|
||||
(
|
||||
"rate_limit_error",
|
||||
@@ -2178,13 +2179,14 @@ async fn streaming_upstream_error_event_should_be_translated_to_response_failed_
|
||||
"turn_rate_limit_exceeded",
|
||||
ProviderErrorKind::RateLimited,
|
||||
429,
|
||||
true,
|
||||
),
|
||||
] {
|
||||
let trace = Arc::new(Trace::default());
|
||||
let error_data = json!({
|
||||
"type": "error",
|
||||
"status": 429,
|
||||
"headers": {"Retry-After": "24"},
|
||||
"headers": {"Retry-After": "24", "retry-after-ms": "24000", "x-request-id": "req_test"},
|
||||
"error": {
|
||||
"type": error_type,
|
||||
"code": code,
|
||||
@@ -2254,6 +2256,31 @@ async fn streaming_upstream_error_event_should_be_translated_to_response_failed_
|
||||
.find(|event| event.event.as_deref() == Some("response.failed"))
|
||||
.expect("translated response.failed event");
|
||||
let failed: Value = serde_json::from_str(&failed.data).expect("response.failed JSON");
|
||||
let mut expected = json!({
|
||||
"type": "response.failed",
|
||||
"status": client_status,
|
||||
"headers": {"x-request-id": "req_test"},
|
||||
"response": {
|
||||
"id": "resp_test",
|
||||
"model": "public-model",
|
||||
"status": "failed",
|
||||
"error": {
|
||||
"type": error_type,
|
||||
"code": client_code,
|
||||
"message": "Our servers are currently overloaded. Please try again later.",
|
||||
"param": null,
|
||||
"future_error_field": {"keep": true}
|
||||
},
|
||||
"future_response_field": {"keep": true}
|
||||
},
|
||||
"sequence_number": 2,
|
||||
"future_event_field": {"keep": true}
|
||||
});
|
||||
if keeps_retry_after {
|
||||
expected["headers"]["Retry-After"] = json!("24");
|
||||
expected["headers"]["retry-after-ms"] = json!("24000");
|
||||
expected["response"]["error"]["headers"] = json!({"retry-after": "24"});
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
(
|
||||
@@ -2262,32 +2289,8 @@ async fn streaming_upstream_error_event_should_be_translated_to_response_failed_
|
||||
body.contains("event: error\n"),
|
||||
body.ends_with("data: [DONE]\n\n"),
|
||||
),
|
||||
(
|
||||
json!({
|
||||
"type": "response.failed",
|
||||
"status": client_status,
|
||||
"headers": {"Retry-After": "24"},
|
||||
"response": {
|
||||
"id": "resp_test",
|
||||
"model": "public-model",
|
||||
"status": "failed",
|
||||
"error": {
|
||||
"type": error_type,
|
||||
"code": client_code,
|
||||
"message": "Our servers are currently overloaded. Please try again later.",
|
||||
"param": null,
|
||||
"future_error_field": {"keep": true},
|
||||
"headers": {"retry-after": "24"}
|
||||
},
|
||||
"future_response_field": {"keep": true}
|
||||
},
|
||||
"sequence_number": 2,
|
||||
"future_event_field": {"keep": true}
|
||||
}),
|
||||
1,
|
||||
false,
|
||||
true,
|
||||
)
|
||||
(expected, 1, false, true),
|
||||
"{code}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1004,7 +1004,8 @@ fn capacity_failure_projection_preserves_wire_metadata_and_original_events() {
|
||||
("response.failed", false),
|
||||
] {
|
||||
for status in [None, Some(400), Some(429), Some(503)] {
|
||||
let error = json!({"code": code, "type": "service_unavailable_error", "message": "busy", "future": 42});
|
||||
let error = json!({"code": code, "type": "service_unavailable_error", "message": "busy", "future": 42,
|
||||
"headers":{"retry-after":"309", "Retry-After-Ms":"309000", "x-request-id":"req_capacity"}});
|
||||
let mut original = if event_type == "error" {
|
||||
json!({"type": event_type, "error": error, "sequence_number": 3})
|
||||
} else {
|
||||
@@ -1046,11 +1047,16 @@ fn capacity_failure_projection_preserves_wire_metadata_and_original_events() {
|
||||
assert_eq!(sse_data["response"]["error"]["code"], "server_error");
|
||||
assert_eq!(sse_data["response"]["error"]["message"], "busy");
|
||||
assert_eq!(sse_data["response"]["error"]["future"], 42);
|
||||
assert_eq!(
|
||||
sse_data["response"]["error"]["headers"],
|
||||
json!({"x-request-id":"req_capacity"})
|
||||
);
|
||||
let messages = ws.push_websocket(&event);
|
||||
let ws_data: Value = serde_json::from_str(&messages[0]).expect("JSON");
|
||||
if event_type == "error" && status.is_some() {
|
||||
let mut expected = original.clone();
|
||||
expected["error"]["code"] = json!("server_error");
|
||||
expected["error"]["headers"] = json!({"x-request-id":"req_capacity"});
|
||||
expected["status_code"] = json!(503);
|
||||
assert_eq!(ws_data, expected);
|
||||
} else {
|
||||
|
||||
@@ -285,6 +285,7 @@ async fn initial_capacity_failure_offers_client_retry_with_upstream_correlation(
|
||||
vec![
|
||||
header("x-request-id", b"req_capacity"),
|
||||
header("retry-after", b"7"),
|
||||
header("retry-after-ms", b"7000"),
|
||||
],
|
||||
)
|
||||
.with_client_visible_upstream_error(ClientVisibleUpstreamError::new(
|
||||
@@ -297,7 +298,8 @@ async fn initial_capacity_failure_offers_client_retry_with_upstream_correlation(
|
||||
assert_eq!(error["error"]["code"], "server_error");
|
||||
assert_eq!(error["error"]["message"], "busy");
|
||||
assert_eq!(error["headers"]["x-request-id"], "req_capacity");
|
||||
assert_eq!(error["headers"]["retry-after"], "7");
|
||||
assert!(error["headers"].get("retry-after").is_none());
|
||||
assert!(error["headers"].get("retry-after-ms").is_none());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,22 +13,28 @@ pub use model_access::{
|
||||
AccountModelAccess, AccountModelAccessMode, InvalidAccountModelAccess,
|
||||
MAX_ACCOUNT_ACCESS_MODELS,
|
||||
};
|
||||
mod quota_limits;
|
||||
pub use quota_limits::{AccountQuotaLimits, QuotaMergeBase, QuotaPauseDecision};
|
||||
mod quota_settings;
|
||||
pub use quota_settings::{
|
||||
AccountQuotaSettings, QuotaBucketSettings, QuotaBucketWarmup, QuotaMergeBase,
|
||||
QuotaPauseDecision,
|
||||
};
|
||||
mod proxy;
|
||||
pub use proxy::{InvalidOutboundProxy, OutboundProxy};
|
||||
mod scheduling;
|
||||
pub mod scope;
|
||||
mod selection;
|
||||
mod smart_scheduling;
|
||||
mod store;
|
||||
mod upstream;
|
||||
|
||||
pub use error::CredentialError;
|
||||
pub use model::*;
|
||||
pub use scheduling::{
|
||||
QuotaResetPeriod, ResetWindowWeights, SchedulingConfigError, SmartSchedulingConfig,
|
||||
};
|
||||
pub(crate) use selection::smart_score;
|
||||
pub use selection::*;
|
||||
pub use smart_scheduling::{QuotaResetPeriod, SmartSchedulingConfig, SmartSchedulingConfigError};
|
||||
pub use store::{
|
||||
AccountCatalogObservation, AccountCatalogOmissions, ProviderAccountStore, StoredAccountCatalog,
|
||||
AccountCatalogObservation, AccountCatalogOmissions, ProviderAccountStore, QuotaWarmupState,
|
||||
QuotaWarmupStateUpdate, StoredAccountCatalog,
|
||||
};
|
||||
pub use upstream::*;
|
||||
|
||||
@@ -828,7 +828,7 @@ pub struct ProviderAccount {
|
||||
enabled: bool,
|
||||
concurrency_limit: Option<AccountConcurrencyLimit>,
|
||||
weight: AccountWeight,
|
||||
quota_limits: super::AccountQuotaLimits,
|
||||
quota_settings: super::AccountQuotaSettings,
|
||||
quota_paused: bool,
|
||||
auto_use_quota_credits: bool,
|
||||
model_access: super::AccountModelAccess,
|
||||
@@ -869,7 +869,7 @@ impl ProviderAccount {
|
||||
enabled: true,
|
||||
concurrency_limit: None,
|
||||
weight: AccountWeight::DEFAULT,
|
||||
quota_limits: super::AccountQuotaLimits::disabled(),
|
||||
quota_settings: super::AccountQuotaSettings::disabled(),
|
||||
quota_paused: false,
|
||||
auto_use_quota_credits: false,
|
||||
model_access: super::AccountModelAccess::all(),
|
||||
@@ -886,16 +886,20 @@ impl ProviderAccount {
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn with_quota_limits(mut self, limits: super::AccountQuotaLimits, paused: bool) -> Self {
|
||||
pub fn with_quota_settings(
|
||||
mut self,
|
||||
limits: super::AccountQuotaSettings,
|
||||
paused: bool,
|
||||
) -> Self {
|
||||
// 只有限额账号需要在取得租约前检测额度窗口的并发更新
|
||||
self.quota_paused = paused;
|
||||
self.quota_limits = limits;
|
||||
self.quota_settings = limits;
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn quota_limits(&self) -> &super::AccountQuotaLimits {
|
||||
&self.quota_limits
|
||||
pub const fn quota_settings(&self) -> &super::AccountQuotaSettings {
|
||||
&self.quota_settings
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
use std::fmt;
|
||||
use url::Url;
|
||||
|
||||
use crate::upstream::ProxyEndpoint;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
pub struct OutboundProxy {
|
||||
url: Url,
|
||||
@@ -51,6 +53,26 @@ impl OutboundProxy {
|
||||
self.url.as_str()
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn dial_endpoint(&self) -> ProxyEndpoint {
|
||||
ProxyEndpoint::new(
|
||||
self.url.host_str().expect("validated proxy host"),
|
||||
self.url
|
||||
.port_or_known_default()
|
||||
.expect("validated proxy port"),
|
||||
)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn matches_endpoint(&self, endpoint: &ProxyEndpoint) -> bool {
|
||||
endpoint.matches(
|
||||
self.url.host_str().expect("validated proxy host"),
|
||||
self.url
|
||||
.port_or_known_default()
|
||||
.expect("validated proxy port"),
|
||||
)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn endpoint(&self) -> String {
|
||||
let mut url = self.url.clone();
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
//! 管理员按账号通用窗口设置的已用比例上限
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
|
||||
#[serde(try_from = "BTreeMap<String, u8>")]
|
||||
pub struct AccountQuotaLimits(BTreeMap<String, u8>);
|
||||
|
||||
impl AccountQuotaLimits {
|
||||
#[must_use]
|
||||
pub const fn disabled() -> Self {
|
||||
Self(BTreeMap::new())
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.0.is_empty()
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn contains(&self, key: &str) -> bool {
|
||||
self.0.contains_key(key)
|
||||
}
|
||||
|
||||
pub fn keys(&self) -> impl Iterator<Item = &str> {
|
||||
self.0.keys().map(String::as_str)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn reached(&self, key: &str, used: Option<f64>) -> bool {
|
||||
self.0
|
||||
.get(key)
|
||||
.zip(used)
|
||||
.is_some_and(|(&limit, used)| used >= f64::from(limit))
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<BTreeMap<String, u8>> for AccountQuotaLimits {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(limits: BTreeMap<String, u8>) -> Result<Self, Self::Error> {
|
||||
if limits.iter().any(|(key, limit)| {
|
||||
key.is_empty()
|
||||
|| key.len() > 256
|
||||
|| key.chars().any(char::is_control)
|
||||
|| !(1..=100).contains(limit)
|
||||
}) {
|
||||
return Err("额度窗口标识无效或上限不在 1~100 范围内");
|
||||
}
|
||||
Ok(Self(limits))
|
||||
}
|
||||
}
|
||||
|
||||
/// 合并时读到的快照版本,时间为空表示当时尚无快照
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct QuotaMergeBase {
|
||||
pub observed_at: Option<std::time::SystemTime>,
|
||||
}
|
||||
|
||||
/// 此次判断依据的策略,以及是否有通用窗口触发暂停
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct QuotaPauseDecision {
|
||||
/// 独立观测无需合并,依赖旧快照的判断必须随写入校验版本
|
||||
pub merge_base: Option<QuotaMergeBase>,
|
||||
pub limits: AccountQuotaLimits,
|
||||
pub paused: bool,
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
//! 管理员按额度窗口设置的使用上限与到期预热策略
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
|
||||
#[serde(try_from = "BTreeMap<String, QuotaBucketSettings>")]
|
||||
pub struct AccountQuotaSettings(BTreeMap<String, QuotaBucketSettings>);
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct QuotaBucketSettings {
|
||||
pub limit_percent: Option<u8>,
|
||||
pub warmup: Option<QuotaBucketWarmup>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct QuotaBucketWarmup {
|
||||
pub model: String,
|
||||
}
|
||||
|
||||
impl AccountQuotaSettings {
|
||||
#[must_use]
|
||||
pub const fn disabled() -> Self {
|
||||
Self(BTreeMap::new())
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.0.is_empty()
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> impl Iterator<Item = (&str, &QuotaBucketSettings)> {
|
||||
self.0
|
||||
.iter()
|
||||
.map(|(key, settings)| (key.as_str(), settings))
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn has_warmup(&self) -> bool {
|
||||
self.0.values().any(|settings| settings.warmup.is_some())
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn contains(&self, key: &str) -> bool {
|
||||
self.0.contains_key(key)
|
||||
}
|
||||
|
||||
pub fn keys(&self) -> impl Iterator<Item = &str> {
|
||||
self.0.keys().map(String::as_str)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn reached(&self, key: &str, used: Option<f64>) -> bool {
|
||||
self.0
|
||||
.get(key)
|
||||
.and_then(|settings| settings.limit_percent)
|
||||
.zip(used)
|
||||
.is_some_and(|(limit, used)| used >= f64::from(limit))
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<BTreeMap<String, QuotaBucketSettings>> for AccountQuotaSettings {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(limits: BTreeMap<String, QuotaBucketSettings>) -> Result<Self, Self::Error> {
|
||||
if limits.iter().any(|(key, settings)| {
|
||||
key.is_empty()
|
||||
|| key.len() > 256
|
||||
|| key.chars().any(char::is_control)
|
||||
|| settings
|
||||
.limit_percent
|
||||
.is_some_and(|limit| !(1..=100).contains(&limit))
|
||||
|| settings.warmup.as_ref().is_some_and(|warmup| {
|
||||
warmup.model.is_empty()
|
||||
|| warmup.model.len() > 128
|
||||
|| warmup.model.trim() != warmup.model
|
||||
|| warmup.model.chars().any(char::is_control)
|
||||
})
|
||||
}) {
|
||||
return Err("额度桶设置无效");
|
||||
}
|
||||
Ok(Self(limits))
|
||||
}
|
||||
}
|
||||
|
||||
/// 合并时读到的快照版本,时间为空表示当时尚无快照
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct QuotaMergeBase {
|
||||
pub observed_at: Option<std::time::SystemTime>,
|
||||
}
|
||||
|
||||
/// 此次判断依据的策略,以及是否有通用窗口触发暂停
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct QuotaPauseDecision {
|
||||
/// 独立观测无需合并,依赖旧快照的判断必须随写入校验版本
|
||||
pub merge_base: Option<QuotaMergeBase>,
|
||||
pub limits: AccountQuotaSettings,
|
||||
pub paused: bool,
|
||||
}
|
||||
+88
-44
@@ -1,4 +1,4 @@
|
||||
//! 智能调度参数;固定十分位保证快照可精确比较,浮点数只用于 wire 与评分
|
||||
//! 账号调度参数与共享重置窗口评分;固定十分位保证快照可精确比较,浮点数只用于 wire 与评分
|
||||
|
||||
use std::time::SystemTime;
|
||||
|
||||
@@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
|
||||
#[serde(into = "SmartSchedulingValues")]
|
||||
pub struct SmartSchedulingConfig {
|
||||
weights: [u8; 6],
|
||||
reset_windows: [u8; 3],
|
||||
reset_windows: ResetWindowWeights,
|
||||
}
|
||||
|
||||
/// 账号通用额度的周期;Provider 按上游周期事实投影,不依赖窗口名称和槽位
|
||||
@@ -30,9 +30,9 @@ impl QuotaResetPeriod {
|
||||
}
|
||||
}
|
||||
|
||||
fn weight_tenths(value: f64, field: &'static str) -> Result<u8, SmartSchedulingConfigError> {
|
||||
fn weight_tenths(value: f64, field: &'static str) -> Result<u8, SchedulingConfigError> {
|
||||
if !(0.0..=10.0).contains(&value) || (value * 10.0).round() / 10.0 != value {
|
||||
return Err(SmartSchedulingConfigError::InvalidWeight(field));
|
||||
return Err(SchedulingConfigError::InvalidWeight(field));
|
||||
}
|
||||
Ok((value * 10.0).round() as u8)
|
||||
}
|
||||
@@ -52,13 +52,13 @@ impl Default for SmartSchedulingConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
weights: [10, 8, 10, 5, 0, 0],
|
||||
reset_windows: [0; 3],
|
||||
reset_windows: ResetWindowWeights::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum SmartSchedulingConfigError {
|
||||
pub enum SchedulingConfigError {
|
||||
#[error("{0} must be between 0 and 10 with at most one decimal place")]
|
||||
InvalidWeight(&'static str),
|
||||
#[error("at least one smart scheduling weight must be positive")]
|
||||
@@ -67,7 +67,7 @@ pub enum SmartSchedulingConfigError {
|
||||
|
||||
impl SmartSchedulingConfig {
|
||||
/// 顺序为负载、剩余额度、健康、首输出延迟、额度重置和排队压力
|
||||
pub fn new(weights: [f64; 6]) -> Result<Self, SmartSchedulingConfigError> {
|
||||
pub fn new(weights: [f64; 6]) -> Result<Self, SchedulingConfigError> {
|
||||
let mut tenths = [0; 6];
|
||||
for (index, field) in [
|
||||
"loadWeight",
|
||||
@@ -83,7 +83,7 @@ impl SmartSchedulingConfig {
|
||||
tenths[index] = weight_tenths(weights[index], field)?;
|
||||
}
|
||||
if tenths == [0; 6] {
|
||||
return Err(SmartSchedulingConfigError::EmptyWeights);
|
||||
return Err(SchedulingConfigError::EmptyWeights);
|
||||
}
|
||||
Ok(Self {
|
||||
weights: tenths,
|
||||
@@ -97,35 +97,14 @@ impl SmartSchedulingConfig {
|
||||
}
|
||||
|
||||
fn reset_enabled(self) -> bool {
|
||||
self.weights[4] > 0 && self.reset_windows != [0; 3]
|
||||
self.weights[4] > 0 && self.reset_windows.enabled()
|
||||
}
|
||||
|
||||
pub(crate) fn reset_score(self, signals: &AccountRuntimeSignals, now: SystemTime) -> f64 {
|
||||
if !self.reset_enabled() {
|
||||
return 0.0;
|
||||
}
|
||||
let mut scores = [0.0_f64; 3];
|
||||
for window in signals.quota_reset_windows.as_deref().unwrap_or_default() {
|
||||
let Ok(remaining) = window.reset_at.duration_since(now) else {
|
||||
continue;
|
||||
};
|
||||
if remaining.is_zero() {
|
||||
continue;
|
||||
}
|
||||
let half = f64::from(window.period.seconds()) / 7.0;
|
||||
let score = half / (half + remaining.as_secs_f64());
|
||||
// 同一周期只计最近的有效重置,避免重复额度桶叠加得分
|
||||
let slot = &mut scores[window.period as usize];
|
||||
*slot = slot.max(score);
|
||||
}
|
||||
// 缺失周期得零分,分母固定,避免少报窗口反而提高排名
|
||||
let weights = self.reset_windows.map(f64::from);
|
||||
scores
|
||||
.into_iter()
|
||||
.zip(weights)
|
||||
.map(|(score, w)| score * w)
|
||||
.sum::<f64>()
|
||||
/ weights.into_iter().sum::<f64>()
|
||||
self.reset_windows.score(signals, now)
|
||||
}
|
||||
|
||||
pub(crate) fn score_tolerance(self) -> f64 {
|
||||
@@ -157,14 +136,14 @@ struct SmartSchedulingValues {
|
||||
|
||||
#[derive(Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
struct ResetWindowWeights {
|
||||
struct ResetWindowValues {
|
||||
five_hour: f64,
|
||||
weekly: f64,
|
||||
monthly: f64,
|
||||
}
|
||||
|
||||
impl TryFrom<SmartSchedulingValues> for SmartSchedulingConfig {
|
||||
type Error = SmartSchedulingConfigError;
|
||||
type Error = SchedulingConfigError;
|
||||
|
||||
fn try_from(value: SmartSchedulingValues) -> Result<Self, Self::Error> {
|
||||
let mut config = Self::new([
|
||||
@@ -175,12 +154,7 @@ impl TryFrom<SmartSchedulingValues> for SmartSchedulingConfig {
|
||||
value.reset_weight,
|
||||
value.queue_weight,
|
||||
])?;
|
||||
let windows = value.reset_window_weights;
|
||||
config.reset_windows = [
|
||||
weight_tenths(windows.five_hour, "fiveHour")?,
|
||||
weight_tenths(windows.weekly, "weekly")?,
|
||||
weight_tenths(windows.monthly, "monthly")?,
|
||||
];
|
||||
config.reset_windows = value.reset_window_weights;
|
||||
Ok(config)
|
||||
}
|
||||
}
|
||||
@@ -202,11 +176,81 @@ impl From<SmartSchedulingConfig> for SmartSchedulingValues {
|
||||
latency_weight,
|
||||
reset_weight,
|
||||
queue_weight,
|
||||
reset_window_weights: ResetWindowWeights {
|
||||
five_hour: f64::from(value.reset_windows[0]) / 10.0,
|
||||
weekly: f64::from(value.reset_windows[1]) / 10.0,
|
||||
monthly: f64::from(value.reset_windows[2]) / 10.0,
|
||||
},
|
||||
reset_window_weights: value.reset_windows,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 两种调度策略共用的重置窗口系数,各策略独立持有配置
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)]
|
||||
#[serde(into = "ResetWindowValues")]
|
||||
pub struct ResetWindowWeights {
|
||||
weights: [u8; 3],
|
||||
}
|
||||
|
||||
impl ResetWindowWeights {
|
||||
/// 顺序为 5 小时、周、月;全零表示未指定窗口
|
||||
pub fn new(weights: [f64; 3]) -> Result<Self, SchedulingConfigError> {
|
||||
Ok(Self {
|
||||
weights: [
|
||||
weight_tenths(weights[0], "fiveHour")?,
|
||||
weight_tenths(weights[1], "weekly")?,
|
||||
weight_tenths(weights[2], "monthly")?,
|
||||
],
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn enabled(self) -> bool {
|
||||
self.weights != [0; 3]
|
||||
}
|
||||
|
||||
pub(crate) fn score(self, signals: &AccountRuntimeSignals, now: SystemTime) -> f64 {
|
||||
if !self.enabled() {
|
||||
return 0.0;
|
||||
}
|
||||
let mut scores = [0.0_f64; 3];
|
||||
for window in signals.quota_reset_windows.as_deref().unwrap_or_default() {
|
||||
let Ok(remaining) = window.reset_at.duration_since(now) else {
|
||||
continue;
|
||||
};
|
||||
if remaining.is_zero() {
|
||||
continue;
|
||||
}
|
||||
let half = f64::from(window.period.seconds()) / 7.0;
|
||||
let score = half / (half + remaining.as_secs_f64());
|
||||
// 同一周期只计最近的有效重置,避免重复额度桶叠加得分
|
||||
let slot = &mut scores[window.period as usize];
|
||||
*slot = slot.max(score);
|
||||
}
|
||||
// 缺失周期得零分,分母固定,避免少报窗口反而提高排名
|
||||
let weights = self.weights.map(f64::from);
|
||||
scores
|
||||
.into_iter()
|
||||
.zip(weights)
|
||||
.map(|(score, w)| score * w)
|
||||
.sum::<f64>()
|
||||
/ weights.into_iter().sum::<f64>()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for ResetWindowWeights {
|
||||
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
// 与智能调度一致,先恢复 flatten 缓冲的小数再校验
|
||||
let value = serde_json::Value::deserialize(deserializer)?;
|
||||
let values =
|
||||
serde_json::from_value::<ResetWindowValues>(value).map_err(serde::de::Error::custom)?;
|
||||
Self::new([values.five_hour, values.weekly, values.monthly])
|
||||
.map_err(serde::de::Error::custom)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ResetWindowWeights> for ResetWindowValues {
|
||||
fn from(value: ResetWindowWeights) -> Self {
|
||||
let [five_hour, weekly, monthly] = value.weights.map(|weight| f64::from(weight) / 10.0);
|
||||
Self {
|
||||
five_hour,
|
||||
weekly,
|
||||
monthly,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,8 @@ use crate::concurrency::{CapacityWait, ConcurrencyQueuePolicy, QueueRejection};
|
||||
use crate::identity::ProviderKind;
|
||||
|
||||
use super::{
|
||||
AccountConcurrency, AccountStatus, ProviderAccount, ProviderAccountId, SmartSchedulingConfig,
|
||||
AccountConcurrency, AccountStatus, ProviderAccount, ProviderAccountId, ResetWindowWeights,
|
||||
SmartSchedulingConfig,
|
||||
};
|
||||
|
||||
/// `runtime_settings.rotation_strategy` 的稳定值
|
||||
@@ -90,6 +91,7 @@ impl AccountAffinity {
|
||||
pub struct AccountSelectionPolicy {
|
||||
strategy: RotationStrategy,
|
||||
smart_scheduling: SmartSchedulingConfig,
|
||||
quota_reset_window_weights: ResetWindowWeights,
|
||||
max_concurrent_per_account: AccountConcurrency,
|
||||
queue_policy: ConcurrencyQueuePolicy,
|
||||
openai_guardian_reserved_concurrency: u32,
|
||||
@@ -108,6 +110,7 @@ impl AccountSelectionPolicy {
|
||||
Self {
|
||||
strategy,
|
||||
smart_scheduling: SmartSchedulingConfig::default(),
|
||||
quota_reset_window_weights: ResetWindowWeights::default(),
|
||||
max_concurrent_per_account: max_concurrent_per_account.into(),
|
||||
openai_guardian_reserved_concurrency: 0,
|
||||
openai_account_affinity: AccountAffinity::default(),
|
||||
@@ -121,6 +124,17 @@ impl AccountSelectionPolicy {
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn with_quota_reset_window_weights(mut self, weights: ResetWindowWeights) -> Self {
|
||||
self.quota_reset_window_weights = weights;
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn quota_reset_window_weights(self) -> ResetWindowWeights {
|
||||
self.quota_reset_window_weights
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn with_openai_account_affinity(mut self, affinity: AccountAffinity) -> Self {
|
||||
self.openai_account_affinity = affinity;
|
||||
@@ -799,24 +813,31 @@ impl AccountSelector {
|
||||
let candidate = match context.policy.strategy() {
|
||||
RotationStrategy::QuotaResetPriority => {
|
||||
eligible.sort_by(|left, right| {
|
||||
(
|
||||
left.signals.quota_reset_at.is_none(),
|
||||
left.signals.quota_reset_at,
|
||||
)
|
||||
.cmp(&(
|
||||
right.signals.quota_reset_at.is_none(),
|
||||
right.signals.quota_reset_at,
|
||||
))
|
||||
.then_with(|| {
|
||||
capacity_utilization(left, context)
|
||||
.total_cmp(&capacity_utilization(right, context))
|
||||
})
|
||||
.then_with(|| {
|
||||
left.signals
|
||||
.last_started_at
|
||||
.cmp(&right.signals.last_started_at)
|
||||
})
|
||||
.then_with(|| left.account.id().cmp(right.account.id()))
|
||||
let windows = context.policy.quota_reset_window_weights();
|
||||
if windows.enabled() {
|
||||
windows
|
||||
.score(&right.signals, context.now)
|
||||
.total_cmp(&windows.score(&left.signals, context.now))
|
||||
} else {
|
||||
(
|
||||
left.signals.quota_reset_at.is_none(),
|
||||
left.signals.quota_reset_at,
|
||||
)
|
||||
.cmp(&(
|
||||
right.signals.quota_reset_at.is_none(),
|
||||
right.signals.quota_reset_at,
|
||||
))
|
||||
}
|
||||
.then_with(|| {
|
||||
capacity_utilization(left, context)
|
||||
.total_cmp(&capacity_utilization(right, context))
|
||||
})
|
||||
.then_with(|| {
|
||||
left.signals
|
||||
.last_started_at
|
||||
.cmp(&right.signals.last_started_at)
|
||||
})
|
||||
.then_with(|| left.account.id().cmp(right.account.id()))
|
||||
});
|
||||
eligible.first().copied()?
|
||||
}
|
||||
|
||||
@@ -28,17 +28,45 @@ pub struct StoredAccountCatalog {
|
||||
pub document: bytes::Bytes,
|
||||
}
|
||||
|
||||
/// 批量选号的存储优化:只有账号自己的当前版本、有效时间和目录摘要都匹配才可跳过
|
||||
/// 批量选号只按账号当前凭据和套餐的目录摘要排除候选
|
||||
pub struct AccountCatalogOmissions {
|
||||
pub documents: Vec<[u8; 32]>,
|
||||
pub exceptions: Vec<ProviderAccountId>,
|
||||
pub observed_after: std::time::SystemTime,
|
||||
pub as_of: std::time::SystemTime,
|
||||
}
|
||||
|
||||
/// Provider 自行解释预热进度;版本只用于并发领取和观测合并
|
||||
#[derive(Clone, Default)]
|
||||
pub struct QuotaWarmupState {
|
||||
pub version: u64,
|
||||
pub data: serde_json::Map<String, serde_json::Value>,
|
||||
}
|
||||
|
||||
pub struct QuotaWarmupStateUpdate<'a> {
|
||||
pub account: &'a ProviderAccount,
|
||||
pub expected_version: u64,
|
||||
pub expected_quota_observed_at: Option<std::time::SystemTime>,
|
||||
pub data: serde_json::Map<String, serde_json::Value>,
|
||||
}
|
||||
|
||||
/// `provider_accounts` 的数据库中立端口
|
||||
#[async_trait]
|
||||
pub trait ProviderAccountStore: Send + Sync {
|
||||
async fn load_quota_warmup_state(
|
||||
&self,
|
||||
_account: &ProviderAccount,
|
||||
) -> Result<Option<QuotaWarmupState>, StoreError> {
|
||||
Err(StoreError::new(crate::error::StoreErrorKind::InvalidData))
|
||||
}
|
||||
|
||||
/// 配置、凭据或进度变化均使领取失败,取消或崩溃后不能重放已领取的请求
|
||||
async fn compare_and_swap_quota_warmup_state(
|
||||
&self,
|
||||
_update: QuotaWarmupStateUpdate<'_>,
|
||||
) -> Result<bool, StoreError> {
|
||||
Err(StoreError::new(crate::error::StoreErrorKind::InvalidData))
|
||||
}
|
||||
|
||||
async fn load_account_catalogs(
|
||||
&self,
|
||||
provider: &ProviderKind,
|
||||
|
||||
@@ -367,6 +367,7 @@ where
|
||||
account_rotations: 0,
|
||||
candidate_index,
|
||||
excluded_accounts: BTreeSet::new(),
|
||||
excluded_proxy_endpoints: Vec::new(),
|
||||
credential_recovery_attempted_accounts: BTreeSet::new(),
|
||||
recovery_account: None,
|
||||
pending_retry: None,
|
||||
@@ -525,6 +526,7 @@ pub struct ResponseExecutionSession<S: ?Sized> {
|
||||
account_rotations: u32,
|
||||
candidate_index: usize,
|
||||
excluded_accounts: BTreeSet<crate::account::ProviderAccountId>,
|
||||
excluded_proxy_endpoints: Vec<crate::upstream::ProxyEndpoint>,
|
||||
credential_recovery_attempted_accounts: BTreeSet<crate::account::ProviderAccountId>,
|
||||
/// 凭据恢复后的一次性同账号钉选;只约束紧随其后的 replay attempt,
|
||||
/// attempt 建立时即被消费,后续可重试错误仍可换号消耗剩余重试预算
|
||||
@@ -1019,6 +1021,10 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
fn has_account_rotation_budget(&self) -> bool {
|
||||
self.account_rotations < self.plan.account_selection_policy().max_account_rotations()
|
||||
}
|
||||
|
||||
async fn prepare_attempt(&mut self) -> Result<Option<PullOutcome>, EngineError> {
|
||||
let pending_retry = self.pending_retry.take();
|
||||
let is_transport_recovery = pending_retry
|
||||
@@ -1094,7 +1100,7 @@ where
|
||||
),
|
||||
}
|
||||
};
|
||||
let account_context = match &self.account_selection {
|
||||
let mut account_context = match &self.account_selection {
|
||||
AccountSelection::Diagnostic(target) => AccountAttemptContext::diagnostic(
|
||||
self.excluded_accounts.clone(),
|
||||
target.clone(),
|
||||
@@ -1111,6 +1117,9 @@ where
|
||||
self.credential_recovery_attempted_accounts
|
||||
.contains(account)
|
||||
}));
|
||||
account_context
|
||||
.excluded_proxy_endpoints
|
||||
.clone_from(&self.excluded_proxy_endpoints);
|
||||
let provider = self
|
||||
.engine
|
||||
.providers()
|
||||
@@ -1230,9 +1239,23 @@ where
|
||||
}
|
||||
ProviderBoundary::Result(result) => match *result {
|
||||
Ok(stream) => stream,
|
||||
Err(error) => {
|
||||
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
|
||||
@@ -1264,7 +1287,7 @@ where
|
||||
)
|
||||
// 跨 Provider 候选推进必然换号(账号行按 Provider 隔离),
|
||||
// 预算耗尽后不再推进,交回容量类失败的原有终态语义。
|
||||
&& self.account_rotations < self.plan.account_selection_policy().max_account_rotations()
|
||||
&& self.has_account_rotation_budget()
|
||||
&& self.advance_provider_candidate();
|
||||
let retryable = self
|
||||
.apply_retry_policy(super::policy::RetryFacts {
|
||||
@@ -1668,10 +1691,8 @@ where
|
||||
&& self.routing_attempts < self.plan.max_attempts().get();
|
||||
// 只有尚未发送的逻辑请求进入新增策略;已有发送后安全恢复保持原有路由规则
|
||||
let connection_retry_requested = self.current_send_state() == UpstreamSendState::NotSent
|
||||
&& matches!(
|
||||
error.pre_delivery_retry(),
|
||||
Some(crate::error::PreDeliveryRetry::SameAccountConnectionRetry { .. })
|
||||
);
|
||||
&& self.continuation_attempt == ContinuationAttempt::None
|
||||
&& error.allows_connection_rotation();
|
||||
// 服务器建议不可截短
|
||||
// 超出上限,或上游拒绝继续重试当前账号时,跳过同账号等待
|
||||
// 较短的 Retry-After 也直接沿用安全换号或原错误出口
|
||||
@@ -1729,9 +1750,8 @@ where
|
||||
let can_rotate_connection = connection_retry_requested
|
||||
&& self.account_selection.required_account().is_none()
|
||||
&& self.routing_attempts < self.plan.max_attempts().get()
|
||||
&& self.account_rotations
|
||||
< self.plan.account_selection_policy().max_account_rotations();
|
||||
// A 先恢复一次,再利用预留预算重选;重选继续走已有降级传输,避免 WS 抢占最后一次 opening
|
||||
&& self.has_account_rotation_budget();
|
||||
// A 先恢复一次,再利用预留预算重选;下一次传输仍由 Provider 的恢复策略决定
|
||||
let connection_rotation = transport_recovery
|
||||
.filter(|_| can_rotate_connection && current.connection_rotation_reserved);
|
||||
let reserve_connection_rotation = can_rotate_connection
|
||||
@@ -1808,8 +1828,7 @@ where
|
||||
&& !is_return_error_retry(&error)
|
||||
&& transport_recovery.is_none()
|
||||
&& (ordinary_retry || account_rotation_retry)
|
||||
&& self.account_rotations
|
||||
< self.plan.account_selection_policy().max_account_rotations();
|
||||
&& self.has_account_rotation_budget();
|
||||
let retryable = !error.retry_is_prohibited()
|
||||
&& (continuation_retry
|
||||
|| same_account_retry
|
||||
@@ -2000,6 +2019,11 @@ where
|
||||
}
|
||||
StreamRetry::Pending(retry) => self.pending_retry = Some(retry),
|
||||
StreamRetry::ConnectionRotation(retry) => {
|
||||
if let Some(endpoint) = pending.error.unreachable_proxy()
|
||||
&& !self.excluded_proxy_endpoints.contains(endpoint)
|
||||
{
|
||||
self.excluded_proxy_endpoints.push(endpoint.clone());
|
||||
}
|
||||
self.excluded_accounts.insert(retry.account.clone());
|
||||
self.connection_rotation = Some(retry);
|
||||
}
|
||||
@@ -2186,6 +2210,26 @@ where
|
||||
}
|
||||
|
||||
match self.continuation_attempt {
|
||||
ContinuationAttempt::Native
|
||||
if error.continuation_recovery_disposition()
|
||||
== Some(ContinuationRecoveryDisposition::ClientReplayRequired)
|
||||
&& matches!(
|
||||
error.pre_delivery_retry(),
|
||||
Some(crate::error::PreDeliveryRetry::SameAccountTransientRetry {
|
||||
policy: crate::error::TransientRetryPolicy::RetryThenRotateAccount,
|
||||
..
|
||||
})
|
||||
)
|
||||
&& error
|
||||
.retry_after()
|
||||
.is_some_and(|delay| delay > self.plan.max_retry_after())
|
||||
&& !error.retry_is_prohibited()
|
||||
&& self.has_account_rotation_budget() =>
|
||||
{
|
||||
self.continuation_attempt = ContinuationAttempt::RotateForClientReplay;
|
||||
self.excluded_accounts
|
||||
.insert(current.metadata.provider_account_id().clone());
|
||||
}
|
||||
ContinuationAttempt::Native => match error.continuation_recovery_disposition() {
|
||||
Some(ContinuationRecoveryDisposition::RetryExactConnection) => {
|
||||
self.recovery_account = Some(current.metadata.provider_account_id().clone());
|
||||
@@ -2209,8 +2253,7 @@ where
|
||||
// 两个排除臂都必然换号;预算耗尽后不再排除当前账号做跨账号续写重放,
|
||||
// 落回不可重试路径以原始上游错误终态。
|
||||
ContinuationAttempt::ReplayOwner | ContinuationAttempt::ReplayAny
|
||||
if self.account_rotations
|
||||
>= self.plan.account_selection_policy().max_account_rotations() =>
|
||||
if !self.has_account_rotation_budget() =>
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -2223,7 +2266,7 @@ where
|
||||
self.excluded_accounts
|
||||
.insert(current.metadata.provider_account_id().clone());
|
||||
}
|
||||
ContinuationAttempt::None => return false,
|
||||
ContinuationAttempt::None | ContinuationAttempt::RotateForClientReplay => return false,
|
||||
}
|
||||
true
|
||||
}
|
||||
@@ -2262,7 +2305,7 @@ where
|
||||
return false;
|
||||
}
|
||||
// Native 期间仍可能配置为禁止换号,跨账号重放共用请求冻结的预算
|
||||
if self.account_rotations >= self.plan.account_selection_policy().max_account_rotations() {
|
||||
if !self.has_account_rotation_budget() {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -124,6 +124,7 @@ impl AttemptTrigger {
|
||||
/// 一旦设置,Provider 在本 attempt 内不得换号或切换 target
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct AccountAttemptContext {
|
||||
excluded_proxy_endpoints: Vec<crate::upstream::ProxyEndpoint>,
|
||||
excluded_accounts: BTreeSet<ProviderAccountId>,
|
||||
required_account: Option<ProviderAccountId>,
|
||||
state_owner: Option<ProviderAccountStateOwner>,
|
||||
@@ -140,6 +141,7 @@ impl AccountAttemptContext {
|
||||
state_owner: Option<ProviderAccountStateOwner>,
|
||||
) -> Self {
|
||||
Self {
|
||||
excluded_proxy_endpoints: Vec::new(),
|
||||
excluded_accounts,
|
||||
required_account,
|
||||
state_owner,
|
||||
@@ -160,6 +162,7 @@ impl AccountAttemptContext {
|
||||
) -> Self {
|
||||
let (required_account, eligibility) = target.into_selection();
|
||||
Self {
|
||||
excluded_proxy_endpoints: Vec::new(),
|
||||
excluded_accounts,
|
||||
required_account: Some(required_account),
|
||||
state_owner,
|
||||
@@ -265,6 +268,8 @@ pub enum ContinuationAttempt {
|
||||
///
|
||||
/// 该枚举本身不证明客户端 input 完整,也不授权 Provider 清除 previous-response
|
||||
ReplayAny,
|
||||
/// 仅迁移账号绑定,随后要求客户端携带完整历史重放;不得发送当前增量输入
|
||||
RotateForClientReplay,
|
||||
}
|
||||
|
||||
impl ContinuationAttempt {
|
||||
@@ -275,6 +280,7 @@ impl ContinuationAttempt {
|
||||
Self::Native => "native",
|
||||
Self::ReplayOwner => "replay_owner",
|
||||
Self::ReplayAny => "replay_any",
|
||||
Self::RotateForClientReplay => "rotate_for_client_replay",
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -593,7 +599,9 @@ impl AttemptContext {
|
||||
.account_state_owner()
|
||||
.filter(|owner| owner.provider() == provider)
|
||||
.map(|owner| owner.account()),
|
||||
ContinuationAttempt::None | ContinuationAttempt::ReplayAny => None,
|
||||
ContinuationAttempt::None
|
||||
| ContinuationAttempt::ReplayAny
|
||||
| ContinuationAttempt::RotateForClientReplay => None,
|
||||
});
|
||||
self.request.upstream_adapters.as_ref().map_or(
|
||||
Ok(upstream_adapter::UpstreamAdapterSelection::Native),
|
||||
@@ -896,6 +904,17 @@ impl AttemptContext {
|
||||
self.account.excluded_accounts()
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn excludes_proxy(&self, proxy: Option<&crate::account::OutboundProxy>) -> bool {
|
||||
!self.account.excluded_proxy_endpoints.is_empty()
|
||||
&& proxy.is_some_and(|proxy| {
|
||||
self.account
|
||||
.excluded_proxy_endpoints
|
||||
.iter()
|
||||
.any(|endpoint| proxy.matches_endpoint(endpoint))
|
||||
})
|
||||
}
|
||||
|
||||
/// 管理端诊断或请求局部恢复 attempt 强制使用的唯一账号
|
||||
#[must_use]
|
||||
pub const fn required_account(&self) -> Option<&ProviderAccountId> {
|
||||
@@ -963,11 +982,19 @@ impl AttemptContext {
|
||||
model: Option<&str>,
|
||||
candidates: &'a [AccountCandidate],
|
||||
context: &mut AccountSelectionContext,
|
||||
ownership: policy::AccountScheduleOwnership<'_>,
|
||||
) -> Result<Option<AccountSelection<'a>>, policy::AccountPolicyError> {
|
||||
match self.request.request_policy() {
|
||||
Some(policy) => {
|
||||
policy
|
||||
.select_account(self.attempt_index, provider, model, candidates, context)
|
||||
.select_account(
|
||||
self.attempt_index,
|
||||
provider,
|
||||
model,
|
||||
candidates,
|
||||
context,
|
||||
ownership,
|
||||
)
|
||||
.await
|
||||
}
|
||||
None => Ok(crate::account::AccountSelector.select(candidates, context)),
|
||||
|
||||
@@ -115,10 +115,29 @@ pub enum ModelRouteDecision {
|
||||
Reject,
|
||||
}
|
||||
|
||||
/// Provider 在本轮选号中已解析的归属,仅用于向插件投影候选关系
|
||||
#[derive(Debug, Default, Clone, Copy)]
|
||||
pub struct AccountScheduleOwnership<'a> {
|
||||
pub bound_account: Option<&'a ProviderAccountId>,
|
||||
pub follow_only: bool,
|
||||
pub pinned_account: Option<&'a ProviderAccountId>,
|
||||
}
|
||||
|
||||
/// 本轮内置选号如何对待这个候选;显式选号仍受宿主资格和固定归属约束
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum AccountScheduleRole {
|
||||
Ordinary,
|
||||
Preferred,
|
||||
FollowOnly,
|
||||
Pinned,
|
||||
}
|
||||
|
||||
/// 账号调度插件可见的单个候选事实,不包含凭据或账号资料
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct AccountScheduleCandidate {
|
||||
account_id: ProviderAccountId,
|
||||
session_bound: bool,
|
||||
role: AccountScheduleRole,
|
||||
available: bool,
|
||||
weight: u16,
|
||||
in_flight: u32,
|
||||
@@ -131,6 +150,16 @@ pub struct AccountScheduleCandidate {
|
||||
}
|
||||
|
||||
impl AccountScheduleCandidate {
|
||||
#[must_use]
|
||||
pub const fn session_bound(&self) -> bool {
|
||||
self.session_bound
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn role(&self) -> AccountScheduleRole {
|
||||
self.role
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn available(&self) -> bool {
|
||||
self.available
|
||||
@@ -469,6 +498,7 @@ impl RequestPolicyContext {
|
||||
model: Option<&str>,
|
||||
candidates: &'a [AccountCandidate],
|
||||
context: &mut AccountSelectionContext,
|
||||
ownership: AccountScheduleOwnership<'_>,
|
||||
) -> Result<Option<AccountSelection<'a>>, AccountPolicyError> {
|
||||
let eligible = AccountSelector.policy_candidates(candidates, context);
|
||||
if eligible.is_empty() {
|
||||
@@ -476,19 +506,33 @@ impl RequestPolicyContext {
|
||||
}
|
||||
let projected = eligible
|
||||
.iter()
|
||||
.map(|candidate| AccountScheduleCandidate {
|
||||
account_id: candidate.account.id().clone(),
|
||||
available: AccountSelector
|
||||
.scheduling_blocker(candidate, context)
|
||||
.is_none(),
|
||||
weight: candidate.account.weight().get(),
|
||||
in_flight: candidate.signals.in_flight,
|
||||
maximum_concurrency: context.concurrency_limit(candidate).get(),
|
||||
last_started_at: candidate.signals.last_started_at,
|
||||
quota_reset_at: candidate.signals.quota_reset_at,
|
||||
quota_remaining_rank: candidate.signals.quota_remaining_rank,
|
||||
failure_rate_basis_points: candidate.signals.failure_rate_basis_points,
|
||||
first_output_latency_ms: candidate.signals.first_output_latency_ms,
|
||||
.map(|candidate| {
|
||||
let id = Some(candidate.account.id());
|
||||
let session_bound = ownership.bound_account == id;
|
||||
AccountScheduleCandidate {
|
||||
session_bound,
|
||||
role: if ownership.pinned_account == id {
|
||||
AccountScheduleRole::Pinned
|
||||
} else if session_bound && ownership.follow_only {
|
||||
AccountScheduleRole::FollowOnly
|
||||
} else if context.preferred_account.as_ref() == id {
|
||||
AccountScheduleRole::Preferred
|
||||
} else {
|
||||
AccountScheduleRole::Ordinary
|
||||
},
|
||||
account_id: candidate.account.id().clone(),
|
||||
available: AccountSelector
|
||||
.scheduling_blocker(candidate, context)
|
||||
.is_none(),
|
||||
weight: candidate.account.weight().get(),
|
||||
in_flight: candidate.signals.in_flight,
|
||||
maximum_concurrency: context.concurrency_limit(candidate).get(),
|
||||
last_started_at: candidate.signals.last_started_at,
|
||||
quota_reset_at: candidate.signals.quota_reset_at,
|
||||
quota_remaining_rank: candidate.signals.quota_remaining_rank,
|
||||
failure_rate_basis_points: candidate.signals.failure_rate_basis_points,
|
||||
first_output_latency_ms: candidate.signals.first_output_latency_ms,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let decision = self
|
||||
|
||||
@@ -117,7 +117,8 @@ pub enum ContinuationFailure {
|
||||
pub enum ContinuationRecoveryDisposition {
|
||||
/// 只能再次取得同账号、同一精确连接,不能改变 continuation scope
|
||||
RetryExactConnection,
|
||||
/// 代理停止内部恢复,由客户端携带完整输入创建新链
|
||||
/// 当前增量不能由代理重放,由客户端携带完整输入创建新链
|
||||
/// 显式换号策略仍可迁移会话绑定,但不能发送增量输入
|
||||
ClientReplayRequired,
|
||||
/// Provider 已证明可以进入 owner 或其他合法目标的恢复流程
|
||||
ProviderReplayAllowed,
|
||||
@@ -135,6 +136,7 @@ pub enum TransientRetryPolicy {
|
||||
}
|
||||
|
||||
/// Provider 在下游尚未提交事件时请求的隐藏恢复方式
|
||||
/// 传输恢复默认保持原账号;明确允许建连换号时,Core 可在同账号恢复失败后重选
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum PreDeliveryRetry {
|
||||
/// 排除本次账号,按普通调度策略重新选号
|
||||
@@ -152,7 +154,7 @@ pub enum PreDeliveryRetry {
|
||||
/// 同账号恢复策略,不提供额外安全重放资格
|
||||
policy: TransientRetryPolicy,
|
||||
},
|
||||
/// 固定本次账号,并按 Provider 给出的序号重试当前传输
|
||||
/// 按 Provider 给出的序号重试当前传输
|
||||
/// 可用于发送结果不明确时的有界尽力恢复,不等于上游未执行的证明
|
||||
SameAccountTransportRetry {
|
||||
/// Provider-owned 传输重试序号,从 1 开始
|
||||
@@ -165,7 +167,7 @@ pub enum PreDeliveryRetry {
|
||||
/// Provider 明确指定实际失败的传输,避免 HTTP 恢复重新进入 WS
|
||||
transport: crate::upstream::AttemptTransport,
|
||||
},
|
||||
/// 固定本次账号,并要求 Provider 使用备用传输
|
||||
/// 要求 Provider 使用备用传输
|
||||
SameAccountTransportFallback,
|
||||
}
|
||||
|
||||
@@ -517,6 +519,7 @@ pub struct ProviderError {
|
||||
replay_safe: bool,
|
||||
retry_prohibited: bool,
|
||||
same_account_retry_blocked: bool,
|
||||
connection_rotation_allowed: bool,
|
||||
pre_delivery_retry: Option<Box<PreDeliveryRetry>>,
|
||||
credential_recovery_required: bool,
|
||||
retry_same_account: bool,
|
||||
@@ -538,6 +541,7 @@ struct ProviderErrorUpstreamValues {
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
struct ProviderErrorFailureObservation {
|
||||
unreachable_proxy: Option<crate::upstream::ProxyEndpoint>,
|
||||
continuation_unavailable_reason: Option<&'static str>,
|
||||
connection: Option<ProviderConnectionObservation>,
|
||||
}
|
||||
@@ -561,6 +565,7 @@ impl ProviderError {
|
||||
replay_safe: false,
|
||||
retry_prohibited: false,
|
||||
same_account_retry_blocked: false,
|
||||
connection_rotation_allowed: false,
|
||||
pre_delivery_retry: None,
|
||||
credential_recovery_required: false,
|
||||
retry_same_account: false,
|
||||
@@ -659,6 +664,20 @@ impl ProviderError {
|
||||
self
|
||||
}
|
||||
|
||||
/// 仅当连接账号绑定代理的入口失败时记录;不用于持久化或输出地址
|
||||
#[must_use]
|
||||
pub fn with_unreachable_proxy(mut self, endpoint: crate::upstream::ProxyEndpoint) -> Self {
|
||||
self.failure_observation_mut().unreachable_proxy = Some(endpoint);
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn unreachable_proxy(&self) -> Option<&crate::upstream::ProxyEndpoint> {
|
||||
self.failure_observation
|
||||
.as_deref()
|
||||
.and_then(|observation| observation.unreachable_proxy.as_ref())
|
||||
}
|
||||
|
||||
fn failure_observation_mut(&mut self) -> &mut ProviderErrorFailureObservation {
|
||||
self.failure_observation
|
||||
.get_or_insert_with(|| Box::new(ProviderErrorFailureObservation::default()))
|
||||
@@ -758,9 +777,21 @@ impl ProviderError {
|
||||
self.pre_delivery_retry = Some(Box::new(PreDeliveryRetry::SameAccountConnectionRetry {
|
||||
transport,
|
||||
}));
|
||||
self.allow_connection_rotation();
|
||||
self
|
||||
}
|
||||
|
||||
/// 允许 Core 为瞬时建连失败预留换号机会,不授予重放资格或增加传输重试次数
|
||||
pub fn allow_connection_rotation(&mut self) {
|
||||
self.connection_rotation_allowed = true;
|
||||
}
|
||||
|
||||
/// 供 Core 在传输仍可恢复时为换号预留建连预算
|
||||
#[must_use]
|
||||
pub const fn allows_connection_rotation(&self) -> bool {
|
||||
self.connection_rotation_allowed
|
||||
}
|
||||
|
||||
/// 要求 Core 在账号凭据已恢复后仅对原账号重放一次
|
||||
#[must_use]
|
||||
pub const fn with_same_account_retry(mut self) -> Self {
|
||||
@@ -993,6 +1024,15 @@ impl ProviderError {
|
||||
self.client_visible_upstream_response.as_deref()
|
||||
}
|
||||
|
||||
/// 绑定迁移完成后仅替换客户端恢复投影,保留原始失败的诊断与计费事实
|
||||
pub(crate) fn with_client_recovery_from(mut self, recovery: Self) -> Self {
|
||||
self.client_visible_upstream_error = recovery.client_visible_upstream_error;
|
||||
self.client_visible_upstream_response = recovery.client_visible_upstream_response;
|
||||
self.continuation_failure = recovery.continuation_failure;
|
||||
self.continuation_recovery_disposition = recovery.continuation_recovery_disposition;
|
||||
self
|
||||
}
|
||||
|
||||
/// 取走只供本次重试/提交决策使用的原子客户端事件
|
||||
///
|
||||
/// Core 必须在创建稳定快照、记录中间失败或构造终态前调用本方法,避免原始
|
||||
@@ -1027,6 +1067,7 @@ impl ProviderError {
|
||||
replay_safe: self.replay_safe,
|
||||
retry_prohibited: self.retry_prohibited,
|
||||
same_account_retry_blocked: self.same_account_retry_blocked,
|
||||
connection_rotation_allowed: self.connection_rotation_allowed,
|
||||
pre_delivery_retry: self.pre_delivery_retry.clone(),
|
||||
credential_recovery_required: self.credential_recovery_required,
|
||||
retry_same_account: self.retry_same_account,
|
||||
@@ -1070,6 +1111,10 @@ impl fmt::Debug for ProviderError {
|
||||
)
|
||||
.field("connection_observation", &self.connection_observation())
|
||||
.field("replay_safe", &self.replay_safe)
|
||||
.field(
|
||||
"connection_rotation_allowed",
|
||||
&self.connection_rotation_allowed,
|
||||
)
|
||||
.field("pre_delivery_retry", &self.pre_delivery_retry)
|
||||
.field(
|
||||
"credential_recovery_required",
|
||||
|
||||
@@ -5,6 +5,14 @@ use crate::{account::OpaqueProviderData, identity::ProviderKind, routing::Config
|
||||
use futures::future::BoxFuture;
|
||||
|
||||
pub trait ProviderRuntimePolicyPort: Send + Sync {
|
||||
/// 后台模型操作读取 Provider 全局画像,不读取单个 Client Key 的覆盖
|
||||
fn load_request_profile<'a>(
|
||||
&'a self,
|
||||
_provider: &'a ProviderKind,
|
||||
) -> BoxFuture<'a, Result<Option<OpaqueProviderData>, ProviderStoreError>> {
|
||||
Box::pin(async { Ok(None) })
|
||||
}
|
||||
|
||||
/// 原子推进预热执行游标,重启或时钟回拨后不重复领取已消费的时刻
|
||||
fn claim_warmup_slot<'a>(
|
||||
&'a self,
|
||||
@@ -52,7 +60,7 @@ pub trait ProviderRuntimePolicyPort: Send + Sync {
|
||||
}
|
||||
}
|
||||
|
||||
/// 校验五段 Cron 预热计划
|
||||
/// 校验单条五段 Cron 表达式
|
||||
#[must_use]
|
||||
pub fn valid_warmup_cron(value: &str) -> bool {
|
||||
parse_warmup_cron(value).is_some()
|
||||
@@ -73,7 +81,7 @@ fn parse_warmup_cron(value: &str) -> Option<cron::Schedule> {
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ProviderWarmupPolicy {
|
||||
enabled: bool,
|
||||
cron: String,
|
||||
schedules: Vec<cron::Schedule>,
|
||||
model: Option<String>,
|
||||
}
|
||||
|
||||
@@ -84,29 +92,47 @@ impl ProviderWarmupPolicy {
|
||||
cron: &str,
|
||||
model: Option<&str>,
|
||||
) -> Result<(), &'static str> {
|
||||
if (enabled || !cron.is_empty()) && !valid_warmup_cron(cron) {
|
||||
Self::validate_and_parse(enabled, cron, model).map(|_| ())
|
||||
}
|
||||
|
||||
fn validate_and_parse(
|
||||
enabled: bool,
|
||||
cron: &str,
|
||||
model: Option<&str>,
|
||||
) -> Result<Vec<cron::Schedule>, &'static str> {
|
||||
if cron.len() > 255 || (enabled && cron.is_empty()) {
|
||||
return Err("account_warmup_cron");
|
||||
}
|
||||
// 分号分隔独立表达式,逗号保留给 Cron 字段内的列表
|
||||
let schedules = if cron.is_empty() {
|
||||
Vec::new()
|
||||
} else {
|
||||
cron.split(';')
|
||||
.map(parse_warmup_cron)
|
||||
.collect::<Option<Vec<_>>>()
|
||||
.ok_or("account_warmup_cron")?
|
||||
};
|
||||
if (enabled && model.is_none()) || !valid_optional_probe_model(model) {
|
||||
return Err("account_warmup_model");
|
||||
}
|
||||
Ok(())
|
||||
Ok(schedules)
|
||||
}
|
||||
|
||||
pub fn try_new(
|
||||
enabled: bool,
|
||||
cron: String,
|
||||
cron: &str,
|
||||
model: Option<String>,
|
||||
) -> Result<Self, ProviderStoreError> {
|
||||
if Self::validate_values(enabled, &cron, model.as_deref()).is_err() {
|
||||
return Err(ProviderStoreError::new(
|
||||
ProviderStoreErrorKind::InvalidData,
|
||||
"validate warmup policy",
|
||||
));
|
||||
}
|
||||
let schedules =
|
||||
Self::validate_and_parse(enabled, cron, model.as_deref()).map_err(|_| {
|
||||
ProviderStoreError::new(
|
||||
ProviderStoreErrorKind::InvalidData,
|
||||
"validate warmup policy",
|
||||
)
|
||||
})?;
|
||||
Ok(Self {
|
||||
enabled,
|
||||
cron,
|
||||
schedules,
|
||||
model,
|
||||
})
|
||||
}
|
||||
@@ -115,7 +141,7 @@ impl ProviderWarmupPolicy {
|
||||
pub const fn disabled() -> Self {
|
||||
Self {
|
||||
enabled: false,
|
||||
cron: String::new(),
|
||||
schedules: Vec::new(),
|
||||
model: None,
|
||||
}
|
||||
}
|
||||
@@ -125,11 +151,6 @@ impl ProviderWarmupPolicy {
|
||||
self.enabled
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn cron(&self) -> &str {
|
||||
&self.cron
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn model(&self) -> Option<&str> {
|
||||
self.model.as_deref()
|
||||
@@ -138,7 +159,10 @@ impl ProviderWarmupPolicy {
|
||||
/// 匹配部署时区中的整分钟时刻,时区重叠及执行去重由 Worker 与存储端口负责
|
||||
#[must_use]
|
||||
pub fn matches_slot(&self, slot: chrono::NaiveDateTime) -> bool {
|
||||
parse_warmup_cron(&self.cron).is_some_and(|schedule| schedule.includes(slot.and_utc()))
|
||||
let instant = slot.and_utc();
|
||||
self.schedules
|
||||
.iter()
|
||||
.any(|schedule| schedule.includes(instant))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1247,13 +1247,13 @@ impl RuntimeSnapshot {
|
||||
operation: &Operation,
|
||||
context: &RoutingContext,
|
||||
) -> Result<RoutingPlan, RoutingError> {
|
||||
self.plan_inner(
|
||||
public_model,
|
||||
operation,
|
||||
self.all_account_scope(),
|
||||
context,
|
||||
false,
|
||||
)
|
||||
// 探测没有 Client Key,沿用同一发布快照的全局画像
|
||||
let scope = self
|
||||
.all_account_scope()
|
||||
.as_ref()
|
||||
.clone()
|
||||
.with_request_profiles(self.settings().request_profiles().clone());
|
||||
self.plan_inner(public_model, operation, Arc::new(scope), context, false)
|
||||
}
|
||||
|
||||
fn plan_inner(
|
||||
|
||||
@@ -101,6 +101,7 @@ impl CompiledSettings {
|
||||
u64::from(settings.openai_session_affinity_ttl_hours) * 3600,
|
||||
))
|
||||
.with_smart_scheduling(settings.smart_scheduling)
|
||||
.with_quota_reset_window_weights(settings.quota_reset_window_weights)
|
||||
.with_queue(ConcurrencyQueuePolicy {
|
||||
max_waiting: settings.max_waiting_per_account,
|
||||
timeout: Duration::from_secs(u64::from(settings.account_queue_timeout_seconds)),
|
||||
|
||||
@@ -30,6 +30,8 @@ pub struct SettingsValues {
|
||||
pub(crate) openai_session_affinity_ttl_hours: u32,
|
||||
pub(crate) responses_max_decompressed_body_bytes: u64,
|
||||
pub(crate) smart_scheduling: crate::account::SmartSchedulingConfig,
|
||||
#[serde(default)]
|
||||
pub(crate) quota_reset_window_weights: crate::account::ResetWindowWeights,
|
||||
pub(crate) rotation_strategy: String,
|
||||
pub(crate) model_mappings: Arc<BTreeMap<String, String>>,
|
||||
pub(crate) min_codex_desktop_version: Option<String>,
|
||||
@@ -111,6 +113,15 @@ impl SettingsValues {
|
||||
&self.request_profiles
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn with_quota_reset_window_weights(
|
||||
mut self,
|
||||
weights: crate::account::ResetWindowWeights,
|
||||
) -> Self {
|
||||
self.quota_reset_window_weights = weights;
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn with_smart_scheduling(
|
||||
mut self,
|
||||
@@ -201,6 +212,7 @@ impl SettingsValues {
|
||||
openai_session_affinity_ttl_hours: 24,
|
||||
responses_max_decompressed_body_bytes: 64 * 1024 * 1024,
|
||||
smart_scheduling: crate::account::SmartSchedulingConfig::default(),
|
||||
quota_reset_window_weights: crate::account::ResetWindowWeights::default(),
|
||||
rotation_strategy: rotation_strategy.into(),
|
||||
model_mappings: Arc::new(model_mappings),
|
||||
min_codex_desktop_version,
|
||||
|
||||
@@ -4,6 +4,32 @@ use std::{fmt, num::NonZeroU32};
|
||||
|
||||
use crate::validation::{IdentifierError, validate_text};
|
||||
|
||||
/// 拨号目标只按主机与端口识别;认证、协议与连接上限不改变 TCP 入口
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct ProxyEndpoint {
|
||||
host: String,
|
||||
port: u16,
|
||||
}
|
||||
|
||||
impl ProxyEndpoint {
|
||||
pub(crate) fn new(host: &str, port: u16) -> Self {
|
||||
Self {
|
||||
host: host.to_ascii_lowercase(),
|
||||
port,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn matches(&self, host: &str, port: u16) -> bool {
|
||||
self.port == port && host.eq_ignore_ascii_case(&self.host)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for ProxyEndpoint {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
formatter.write_str("ProxyEndpoint(<redacted>)")
|
||||
}
|
||||
}
|
||||
|
||||
/// 上游是否可能已经收到业务 payload
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum UpstreamSendState {
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
mod location;
|
||||
mod model_access;
|
||||
mod proxy;
|
||||
mod quota_reset_priority;
|
||||
mod quota_settings;
|
||||
mod scope;
|
||||
mod selection;
|
||||
mod selection_diagnostics;
|
||||
|
||||
@@ -15,6 +15,35 @@ fn proxy_endpoints_support_explicit_schemes_and_redact_credentials() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dial_endpoints_match_host_and_port_without_credentials_or_capacity() {
|
||||
for (source, same, different) in [
|
||||
(
|
||||
"http://user:password@Proxy.Example",
|
||||
"socks5://other:secret@proxy.example:80",
|
||||
"http://proxy.example:81",
|
||||
),
|
||||
(
|
||||
"https://user:password@[::1]",
|
||||
"http://other:secret@[::1]:443",
|
||||
"https://[::2]",
|
||||
),
|
||||
] {
|
||||
let endpoint = OutboundProxy::parse(source).unwrap().dial_endpoint();
|
||||
let same = OutboundProxy::parse(same)
|
||||
.unwrap()
|
||||
.with_max_websocket_connections(3);
|
||||
assert_eq!(endpoint, same.dial_endpoint());
|
||||
assert!(same.matches_endpoint(&endpoint));
|
||||
assert!(
|
||||
!OutboundProxy::parse(different)
|
||||
.unwrap()
|
||||
.matches_endpoint(&endpoint)
|
||||
);
|
||||
assert_eq!(format!("{endpoint:?}"), "ProxyEndpoint(<redacted>)");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_proxy_never_silently_becomes_direct() {
|
||||
for url in [
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
//! 验证重置窗口独立排序、相对系数及亲和与可用性边界
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use gateway_core::account::{
|
||||
AccountQuotaResetWindow, AccountSelector, QuotaResetPeriod, ResetWindowWeights,
|
||||
RotationStrategy,
|
||||
};
|
||||
|
||||
use super::{candidate, context};
|
||||
|
||||
#[test]
|
||||
fn selected_windows_rank_strictly_without_mixing_runtime_scores_or_rotating_near_ties() {
|
||||
let mut context = context(RotationStrategy::QuotaResetPriority);
|
||||
let mut early_weekly = candidate("acct_early_weekly", 2, Some(100));
|
||||
early_weekly.signals.failure_rate_basis_points = Some(9_000);
|
||||
early_weekly.signals.first_output_latency_ms = Some(20_000);
|
||||
let mut late_weekly = candidate("acct_late_weekly", 0, Some(9_900));
|
||||
late_weekly.signals.failure_rate_basis_points = Some(0);
|
||||
late_weekly.signals.first_output_latency_ms = Some(10);
|
||||
for (candidate, short, weekly) in [
|
||||
(&mut early_weekly, 14_400, 6 * 86_400),
|
||||
(&mut late_weekly, 3_600, 6 * 86_400 + 43_200),
|
||||
] {
|
||||
candidate.signals.quota_reset_at = Some(context.now + Duration::from_secs(short));
|
||||
candidate.signals.quota_reset_windows = Some(
|
||||
[
|
||||
(QuotaResetPeriod::FiveHour, short),
|
||||
(QuotaResetPeriod::Weekly, weekly),
|
||||
]
|
||||
.map(|(period, seconds)| AccountQuotaResetWindow {
|
||||
period,
|
||||
reset_at: context.now + Duration::from_secs(seconds),
|
||||
})
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
let mut candidates = [early_weekly, late_weekly];
|
||||
for (weights, expected) in [
|
||||
([0.0, 0.0, 0.0], "acct_late_weekly"),
|
||||
([1.0, 0.0, 0.0], "acct_late_weekly"),
|
||||
([0.0, 0.1, 0.0], "acct_early_weekly"),
|
||||
([0.0, 1.0, 0.0], "acct_early_weekly"),
|
||||
([0.0, 10.0, 0.0], "acct_early_weekly"),
|
||||
([1.0, 0.1, 0.0], "acct_late_weekly"),
|
||||
([10.0, 1.0, 0.0], "acct_late_weekly"),
|
||||
([0.1, 10.0, 0.0], "acct_early_weekly"),
|
||||
] {
|
||||
context.policy = context
|
||||
.policy
|
||||
.with_quota_reset_window_weights(ResetWindowWeights::new(weights).unwrap());
|
||||
for cursor in 0..6 {
|
||||
context.round_robin_cursor = cursor;
|
||||
assert_eq!(
|
||||
AccountSelector
|
||||
.select(&candidates, &context)
|
||||
.unwrap()
|
||||
.candidate()
|
||||
.account
|
||||
.id()
|
||||
.as_str(),
|
||||
expected,
|
||||
"weights {weights:?}, cursor {cursor}"
|
||||
);
|
||||
}
|
||||
}
|
||||
context.preferred_account = Some(candidates[1].account.id().clone());
|
||||
let selected = AccountSelector.select(&candidates, &context).unwrap();
|
||||
assert_eq!(
|
||||
selected.candidate().account.id().as_str(),
|
||||
"acct_late_weekly"
|
||||
);
|
||||
assert_eq!(
|
||||
selected.preferred(),
|
||||
gateway_core::account::PreferredAccountSelection::Hit
|
||||
);
|
||||
|
||||
context.preferred_account = None;
|
||||
candidates[0].signals.in_flight = 3;
|
||||
assert_eq!(
|
||||
AccountSelector
|
||||
.select(&candidates, &context)
|
||||
.unwrap()
|
||||
.candidate()
|
||||
.account
|
||||
.id()
|
||||
.as_str(),
|
||||
"acct_late_weekly"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_or_elapsed_selected_windows_do_not_fall_back_to_disabled_periods() {
|
||||
let mut context = context(RotationStrategy::QuotaResetPriority);
|
||||
context.policy = context
|
||||
.policy
|
||||
.with_quota_reset_window_weights(ResetWindowWeights::new([0.0, 0.0, 1.0]).unwrap());
|
||||
let mut known = candidate("acct_known_monthly", 1, None);
|
||||
known.signals.quota_reset_windows = Some(
|
||||
vec![AccountQuotaResetWindow {
|
||||
period: QuotaResetPeriod::Monthly,
|
||||
reset_at: context.now + Duration::from_secs(30 * 86_400),
|
||||
}]
|
||||
.into(),
|
||||
);
|
||||
for reset_at in [
|
||||
None,
|
||||
Some(context.now),
|
||||
Some(context.now - Duration::from_secs(1)),
|
||||
] {
|
||||
let mut missing = candidate("acct_earlier_other_period", 0, None);
|
||||
missing.signals.quota_reset_at = Some(context.now + Duration::from_secs(1));
|
||||
missing.signals.quota_reset_windows = Some(
|
||||
reset_at
|
||||
.into_iter()
|
||||
.map(|reset_at| AccountQuotaResetWindow {
|
||||
period: QuotaResetPeriod::Monthly,
|
||||
reset_at,
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.into(),
|
||||
);
|
||||
let candidates = [missing, known.clone()];
|
||||
assert_eq!(
|
||||
AccountSelector
|
||||
.select(&candidates, &context)
|
||||
.unwrap()
|
||||
.candidate()
|
||||
.account
|
||||
.id()
|
||||
.as_str(),
|
||||
"acct_known_monthly"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
//! 额度桶配置校验与上限、预热的独立行为
|
||||
|
||||
use gateway_core::account::AccountQuotaSettings;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn warmup_and_limit_can_be_configured_independently() {
|
||||
let settings: AccountQuotaSettings = serde_json::from_value(json!({
|
||||
"codex:18000s": {"warmup": {"model": "test-model"}},
|
||||
"codex:604800s": {"limitPercent": 95}
|
||||
}))
|
||||
.unwrap();
|
||||
assert!(settings.has_warmup());
|
||||
assert!(!settings.reached("codex:18000s", Some(100.0)));
|
||||
assert!(!settings.reached("codex:604800s", Some(94.9)));
|
||||
assert!(settings.reached("codex:604800s", Some(95.0)));
|
||||
assert!(!settings.reached("codex:604800s", None));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_bucket_settings_are_rejected() {
|
||||
for invalid in [
|
||||
json!(95),
|
||||
json!({"limitPercent": 0}),
|
||||
json!({"limitPercent": 101}),
|
||||
json!({"warmup": {}}),
|
||||
json!({"warmup": {"model": ""}}),
|
||||
json!({"warmup": {"model": " model"}}),
|
||||
json!({"warmup": {"model": "model\nname"}}),
|
||||
json!({"warmup": {"model": "x".repeat(129)}}),
|
||||
json!({"warmup": {"model": "model", "unexpected": true}}),
|
||||
] {
|
||||
assert!(
|
||||
serde_json::from_value::<AccountQuotaSettings>(json!({"codex:18000s": invalid}))
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1228,6 +1228,7 @@ fn plan_with_protection(
|
||||
None,
|
||||
)
|
||||
.with_smart_scheduling(account_selection_policy.smart_scheduling())
|
||||
.with_quota_reset_window_weights(account_selection_policy.quota_reset_window_weights())
|
||||
.with_max_account_rotations(account_selection_policy.max_account_rotations())
|
||||
.with_openai_account_affinity(account_selection_policy.openai_account_affinity())
|
||||
.with_openai_session_affinity_ttl_hours(
|
||||
@@ -2490,6 +2491,124 @@ fn native_continuation_uses_provider_defined_owner_then_cross_account_recovery()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_capacity_above_wait_limit_rotates_before_requesting_client_replay() {
|
||||
for (seconds, replacement, rotations, expected_attempts) in [
|
||||
(309, true, 1, 2),
|
||||
(309, false, 1, 2),
|
||||
(240, true, 1, 1),
|
||||
(309, true, 0, 1),
|
||||
] {
|
||||
let Operation::Generate(generate) = generate_operation() else {
|
||||
unreachable!()
|
||||
};
|
||||
let operation =
|
||||
Operation::Generate(generate.with_provider_session_state(
|
||||
ProviderSessionState::new("openai", Map::new()).unwrap(),
|
||||
));
|
||||
let route_plan = plan_with_protection(
|
||||
&operation,
|
||||
AccountSelectionPolicy::new(RotationStrategy::Smart, NonZeroU32::MIN)
|
||||
.with_max_account_rotations(rotations),
|
||||
Default::default(),
|
||||
FastMode::Default,
|
||||
BTreeMap::new(),
|
||||
Default::default(),
|
||||
240,
|
||||
);
|
||||
let original_body = Bytes::from_static(br#"{"error":{"code":"server_is_overloaded","headers":{"retry-after":"309","retry-after-ms":"309000"}}}"#);
|
||||
let original = transient_rejection()
|
||||
.with_retry_after(Duration::from_secs(seconds))
|
||||
.with_raw_upstream_error(gateway_core::error::RawUpstreamError::new(
|
||||
String::from_utf8(original_body.to_vec()).unwrap(),
|
||||
))
|
||||
.with_continuation_recovery_disposition(
|
||||
ContinuationRecoveryDisposition::ClientReplayRequired,
|
||||
);
|
||||
let recovery = if replacement {
|
||||
ProviderError::new(
|
||||
ProviderErrorKind::ContinuationRecoveryRequired,
|
||||
UpstreamSendState::NotSent,
|
||||
)
|
||||
.with_continuation_recovery_disposition(
|
||||
ContinuationRecoveryDisposition::ClientReplayRequired,
|
||||
)
|
||||
.with_client_visible_upstream_error(ClientVisibleUpstreamError::new(
|
||||
"Resend full history.",
|
||||
Some("previous_response_not_found".to_owned()),
|
||||
Some("invalid_request_error".to_owned()),
|
||||
))
|
||||
} else {
|
||||
ProviderError::new(
|
||||
ProviderErrorKind::NoEligibleAccount,
|
||||
UpstreamSendState::NotSent,
|
||||
)
|
||||
};
|
||||
let (coordinator, store, provider) = coordinator(vec![
|
||||
Script::Stream {
|
||||
account_id: "acct_one",
|
||||
items: vec![Err(original)],
|
||||
},
|
||||
Script::Error(recovery),
|
||||
]);
|
||||
let continuation = NativeContinuationPin::new(
|
||||
PreviousResponseId::new("client-previous"),
|
||||
PreviousResponseId::new("upstream-previous"),
|
||||
ClientApiKeyId::new("key_client_1").unwrap(),
|
||||
ProviderKind::new("openai").unwrap(),
|
||||
ProviderAccountId::new("acct_one").unwrap(),
|
||||
)
|
||||
.with_scope(gateway_core::engine::continuation::NativeContinuationScope::ConnectionLocal);
|
||||
let mut session = block_on(coordinator.start(
|
||||
model_request(&operation, SystemTime::now() + Duration::from_secs(30)),
|
||||
operation,
|
||||
route_plan,
|
||||
None,
|
||||
Some(ContinuationBinding::Pinned(continuation)),
|
||||
CancellationToken::new(),
|
||||
))
|
||||
.unwrap();
|
||||
let mut collection = Box::pin(session.collect_uncommitted());
|
||||
let std::task::Poll::Ready(Err(EngineError::Provider(error))) =
|
||||
block_on(async { futures::poll!(&mut collection) })
|
||||
else {
|
||||
panic!("native capacity recovery must not wait on the old account");
|
||||
};
|
||||
drop(collection);
|
||||
assert_eq!(error.kind(), ProviderErrorKind::UpstreamCapacityUnavailable);
|
||||
assert_eq!(error.send_state(), UpstreamSendState::Sent);
|
||||
assert_eq!(error.retry_after(), Some(Duration::from_secs(seconds)));
|
||||
assert_eq!(
|
||||
error.raw_upstream_error().unwrap().as_str().as_bytes(),
|
||||
original_body.as_ref()
|
||||
);
|
||||
assert_eq!(
|
||||
error
|
||||
.client_visible_upstream_error()
|
||||
.and_then(|detail| detail.code()),
|
||||
(replacement && expected_attempts == 2).then_some("previous_response_not_found")
|
||||
);
|
||||
let contexts = provider.contexts.lock().unwrap();
|
||||
assert_eq!(contexts.len(), expected_attempts);
|
||||
if contexts.len() == 2 {
|
||||
assert_eq!(
|
||||
contexts[1].continuation_attempt(),
|
||||
ContinuationAttempt::RotateForClientReplay
|
||||
);
|
||||
assert!(
|
||||
contexts[1]
|
||||
.excluded_accounts()
|
||||
.contains(&ProviderAccountId::new("acct_one").unwrap())
|
||||
);
|
||||
assert!(contexts[1].required_account().is_none());
|
||||
}
|
||||
assert_eq!(
|
||||
store.state.lock().unwrap().finalizations[0].attempt_count,
|
||||
1
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_continuation_client_replay_required_is_terminal() {
|
||||
for (kind, send_state) in [
|
||||
|
||||
@@ -2352,6 +2352,7 @@ impl Provider for PolicySelectingProvider {
|
||||
.map(UpstreamModelId::as_str),
|
||||
&candidates,
|
||||
&mut selection,
|
||||
Default::default(),
|
||||
)
|
||||
.await
|
||||
{
|
||||
@@ -3443,7 +3444,7 @@ fn provider_local_probe_failure_should_remain_distinct_from_upstream() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn diagnostic_probe_does_not_apply_data_plane_account_model_policy() {
|
||||
fn diagnostic_probe_uses_global_profile_without_data_plane_account_model_policy() {
|
||||
let provider = ProviderKind::new("openai").expect("provider");
|
||||
let account_id = ProviderAccountId::new("acct_probe").expect("account");
|
||||
let snapshot = probe_snapshot().with_account_directory(Arc::new(RuntimeAccountDirectory::new(
|
||||
@@ -3458,10 +3459,21 @@ fn diagnostic_probe_does_not_apply_data_plane_account_model_policy() {
|
||||
),
|
||||
)]),
|
||||
)));
|
||||
let profile = gateway_core::account::OpaqueProviderData::new(
|
||||
json!({"identity":"global-probe"})
|
||||
.as_object()
|
||||
.unwrap()
|
||||
.clone(),
|
||||
);
|
||||
let settings = snapshot
|
||||
.settings()
|
||||
.clone()
|
||||
.with_request_profiles(BTreeMap::from([(provider.clone(), profile.clone())]));
|
||||
let snapshot = snapshot.with_settings(&settings).unwrap();
|
||||
let service = DefaultExecutionService::new(
|
||||
RuntimeSnapshotHandle::new(snapshot),
|
||||
Arc::new(TrackingExecutionStore::default()),
|
||||
ProviderRegistry::new([Arc::new(LocalFailingProvider) as Arc<dyn Provider>])
|
||||
ProviderRegistry::new([Arc::new(ProfileCheckingProvider(profile)) as Arc<dyn Provider>])
|
||||
.expect("providers"),
|
||||
Arc::new(UnusedAdmissions),
|
||||
Arc::new(UnusedContinuation),
|
||||
@@ -3862,6 +3874,36 @@ impl Provider for FailingProvider {
|
||||
|
||||
struct LocalFailingProvider;
|
||||
|
||||
struct ProfileCheckingProvider(gateway_core::account::OpaqueProviderData);
|
||||
|
||||
#[async_trait]
|
||||
impl Provider for ProfileCheckingProvider {
|
||||
fn name(&self) -> &'static str {
|
||||
"openai"
|
||||
}
|
||||
|
||||
fn catalog_generation(&self) -> ProviderCatalogGeneration {
|
||||
ProviderCatalogGeneration::default()
|
||||
}
|
||||
|
||||
async fn query_model_capabilities(
|
||||
&self,
|
||||
) -> Result<Vec<ProviderModelCapabilities>, ProviderError> {
|
||||
Ok(Vec::new())
|
||||
}
|
||||
|
||||
async fn execute(
|
||||
self: Arc<Self>,
|
||||
request: ProviderRequest,
|
||||
context: AttemptContext,
|
||||
) -> Result<ProviderStream, ProviderError> {
|
||||
assert_eq!(context.request_profile(), Some(&self.0));
|
||||
Arc::new(LocalFailingProvider)
|
||||
.execute(request, context)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Provider for LocalFailingProvider {
|
||||
fn name(&self) -> &'static str {
|
||||
|
||||
@@ -131,6 +131,7 @@ fn scheduler_can_override_soft_affinity_and_reports_the_override() {
|
||||
Some("demo-model"),
|
||||
&candidates,
|
||||
&mut context(),
|
||||
Default::default(),
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
@@ -156,6 +157,7 @@ fn delegated_scheduler_keeps_builtin_affinity() {
|
||||
None,
|
||||
&candidates,
|
||||
&mut context(),
|
||||
Default::default(),
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
@@ -195,6 +197,7 @@ fn explicit_plugin_choices_override_native_soft_affinity() {
|
||||
None,
|
||||
&candidates,
|
||||
&mut selection_context,
|
||||
Default::default(),
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
@@ -226,6 +229,7 @@ fn scheduler_cannot_select_outside_the_hard_bound_candidate_pool() {
|
||||
None,
|
||||
&candidates,
|
||||
&mut context,
|
||||
Default::default(),
|
||||
)
|
||||
.await;
|
||||
assert!(matches!(result, Err(AccountPolicyError::Fault)));
|
||||
@@ -257,6 +261,7 @@ fn filtered_delegation_preserves_native_weights_and_affinity_on_remaining_accoun
|
||||
None,
|
||||
&candidates,
|
||||
&mut context,
|
||||
Default::default(),
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
@@ -298,7 +303,8 @@ fn filtered_delegation_covers_busy_candidates_and_does_not_persist_between_selec
|
||||
&ProviderKind::new("openai").unwrap(),
|
||||
None,
|
||||
&candidates,
|
||||
&mut selection
|
||||
&mut selection,
|
||||
Default::default(),
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
@@ -316,6 +322,7 @@ fn filtered_delegation_covers_busy_candidates_and_does_not_persist_between_selec
|
||||
None,
|
||||
&candidates,
|
||||
&mut context(),
|
||||
Default::default(),
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
@@ -344,6 +351,7 @@ fn filtering_all_candidates_is_terminal_even_when_the_only_candidate_is_busy_or_
|
||||
None,
|
||||
&candidates,
|
||||
&mut context,
|
||||
Default::default(),
|
||||
)
|
||||
.await;
|
||||
assert!(matches!(result, Err(AccountPolicyError::Filtered)));
|
||||
@@ -368,6 +376,7 @@ fn filtering_ids_outside_the_host_candidate_pool_is_a_policy_fault() {
|
||||
None,
|
||||
&candidates,
|
||||
&mut context,
|
||||
Default::default(),
|
||||
)
|
||||
.await;
|
||||
assert!(matches!(result, Err(AccountPolicyError::Fault)));
|
||||
@@ -386,6 +395,7 @@ fn empty_filter_preserves_native_affinity_and_busy_candidates_cannot_be_picked()
|
||||
None,
|
||||
&candidates,
|
||||
&mut context(),
|
||||
Default::default(),
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
@@ -400,6 +410,7 @@ fn empty_filter_preserves_native_affinity_and_busy_candidates_cannot_be_picked()
|
||||
None,
|
||||
&candidates,
|
||||
&mut context,
|
||||
Default::default(),
|
||||
)
|
||||
.await;
|
||||
assert!(matches!(result, Err(AccountPolicyError::Fault)));
|
||||
|
||||
@@ -252,14 +252,10 @@ fn warmup_cron_validation_and_calendar_matching() {
|
||||
] {
|
||||
assert!(!valid_warmup_cron(value), "{value:?}");
|
||||
}
|
||||
let policy = ProviderWarmupPolicy::try_new(
|
||||
true,
|
||||
"0 8,13 * * MON-FRI".to_owned(),
|
||||
Some("test-model".to_owned()),
|
||||
)
|
||||
.unwrap();
|
||||
let policy =
|
||||
ProviderWarmupPolicy::try_new(true, "0 8,13 * * MON-FRI", Some("test-model".to_owned()))
|
||||
.unwrap();
|
||||
assert!(policy.enabled());
|
||||
assert_eq!(policy.cron(), "0 8,13 * * MON-FRI");
|
||||
assert_eq!(policy.model(), Some("test-model"));
|
||||
for (day, hour, minute, expected) in [
|
||||
(9, 8, 0, true),
|
||||
@@ -275,19 +271,39 @@ fn warmup_cron_validation_and_calendar_matching() {
|
||||
.unwrap();
|
||||
assert_eq!(policy.matches_slot(slot), expected, "{slot}");
|
||||
}
|
||||
assert!(ProviderWarmupPolicy::try_new(false, String::new(), None).is_ok());
|
||||
assert!(
|
||||
ProviderWarmupPolicy::try_new(true, String::new(), Some("test-model".to_owned())).is_err()
|
||||
);
|
||||
assert!(ProviderWarmupPolicy::try_new(false, "08:00".to_owned(), None).is_err());
|
||||
assert!(ProviderWarmupPolicy::try_new(false, "", None).is_ok());
|
||||
assert!(ProviderWarmupPolicy::try_new(true, "", Some("test-model".to_owned())).is_err());
|
||||
assert!(ProviderWarmupPolicy::try_new(false, "08:00", None).is_err());
|
||||
assert!(!valid_warmup_cron(&format!("0{}8 * * *", " ".repeat(256))));
|
||||
let leap = ProviderWarmupPolicy::try_new(false, "0 0 29 FEB *".to_owned(), None).unwrap();
|
||||
let leap = ProviderWarmupPolicy::try_new(false, "0 0 29 FEB *", None).unwrap();
|
||||
let slot = NaiveDate::from_ymd_opt(2028, 2, 29)
|
||||
.unwrap()
|
||||
.and_hms_opt(0, 0, 0)
|
||||
.unwrap();
|
||||
assert!(leap.matches_slot(slot));
|
||||
assert!(!leap.matches_slot(slot + chrono::Duration::days(1)));
|
||||
assert!(ProviderWarmupPolicy::try_new(true, "invalid".to_owned(), None).is_err());
|
||||
assert!(ProviderWarmupPolicy::try_new(true, "0 8 * * *".to_owned(), None).is_err());
|
||||
assert!(ProviderWarmupPolicy::try_new(true, "invalid", None).is_err());
|
||||
assert!(ProviderWarmupPolicy::try_new(true, "0 8 * * *", None).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn warmup_matches_only_the_scheduled_hour_and_minute_pairs() {
|
||||
use chrono::NaiveDate;
|
||||
use gateway_core::provider_ports::ProviderWarmupPolicy;
|
||||
|
||||
let policy = ProviderWarmupPolicy::try_new(
|
||||
true,
|
||||
"58 5 * * *;59 10 * * *;0 16 * * *;1 21 * * *;0 16 * * MON-FRI",
|
||||
Some("test-model".to_owned()),
|
||||
)
|
||||
.unwrap();
|
||||
let date = NaiveDate::from_ymd_opt(2026, 11, 13).unwrap();
|
||||
for minute in 0..1440 {
|
||||
let slot = date.and_hms_opt(minute / 60, minute % 60, 0).unwrap();
|
||||
assert_eq!(
|
||||
policy.matches_slot(slot),
|
||||
[358, 659, 960, 1261].contains(&minute),
|
||||
"{slot}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -877,11 +877,15 @@ fn compiler_accepts_unlimited_default_account_concurrency() {
|
||||
|
||||
#[test]
|
||||
fn compiled_plans_keep_their_scheduling_config_after_a_new_snapshot_is_built() {
|
||||
use gateway_core::account::SmartSchedulingConfig;
|
||||
use gateway_core::account::{ResetWindowWeights, SmartSchedulingConfig};
|
||||
let configs = [
|
||||
SmartSchedulingConfig::default(),
|
||||
SmartSchedulingConfig::new([0.0, 2.0, 1.0, 0.5, 1.2, 2.3]).unwrap(),
|
||||
];
|
||||
let windows = [
|
||||
ResetWindowWeights::default(),
|
||||
ResetWindowWeights::new([0.0, 1.5, 0.0]).unwrap(),
|
||||
];
|
||||
let mut plans = Vec::new();
|
||||
for (index, config) in configs.into_iter().enumerate() {
|
||||
let facts = SnapshotFacts::new(
|
||||
@@ -889,6 +893,7 @@ fn compiled_plans_keep_their_scheduling_config_after_a_new_snapshot_is_built() {
|
||||
revision(index as u64 + 1),
|
||||
SettingsValues::new(3, "smart", BTreeMap::new(), None, None)
|
||||
.with_smart_scheduling(config)
|
||||
.with_quota_reset_window_weights(windows[index])
|
||||
.with_openai_guardian_reserved_concurrency(index as u32),
|
||||
vec![],
|
||||
vec![],
|
||||
@@ -921,6 +926,10 @@ fn compiled_plans_keep_their_scheduling_config_after_a_new_snapshot_is_built() {
|
||||
index as u32
|
||||
);
|
||||
assert_eq!(plan.account_selection_policy().smart_scheduling(), config);
|
||||
assert_eq!(
|
||||
plan.account_selection_policy().quota_reset_window_weights(),
|
||||
windows[index]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ gateway-plugin-sdk = { path = "../sdk", features = ["io"] }
|
||||
gateway-protocol = { path = "../../gateway-protocol" }
|
||||
hex = "0.4.3"
|
||||
image = { version = "0.25.8", default-features = false, features = ["bmp", "gif", "ico", "jpeg", "png", "webp"] }
|
||||
jsonschema = { version = "0.60.0", default-features = false }
|
||||
jsonschema = { version = "0.61.2", default-features = false }
|
||||
roxmltree = "0.21.1"
|
||||
semver = "1.0.27"
|
||||
secrecy = "0.10.3"
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
"protocol_versions": [7],
|
||||
"capabilities": [
|
||||
{ "capability": "frontend_authentication", "versions": [2] },
|
||||
{ "capability": "scheduler", "versions": [3] },
|
||||
{ "capability": "scheduler", "versions": [4] },
|
||||
{ "capability": "model_router", "versions": [2] },
|
||||
{ "capability": "model_catalog", "versions": [2] },
|
||||
{ "capability": "retry_policy", "versions": [2] },
|
||||
|
||||
@@ -7,8 +7,8 @@ use futures::future::BoxFuture;
|
||||
use gateway_core::{
|
||||
account::ProviderAccountId,
|
||||
engine::policy::{
|
||||
AccountScheduleDecision, AccountScheduleInput, ModelRouteDecision, ModelRouteInput,
|
||||
RequestPolicyFault, RequestPolicyPlan,
|
||||
AccountScheduleDecision, AccountScheduleInput, AccountScheduleRole, ModelRouteDecision,
|
||||
ModelRouteInput, RequestPolicyFault, RequestPolicyPlan,
|
||||
},
|
||||
identity::ProviderKind,
|
||||
operation::{Operation, ProviderHttpHeader},
|
||||
@@ -20,6 +20,7 @@ use gateway_plugin_sdk::{
|
||||
AccountScheduleCandidate as WireAccountScheduleCandidate,
|
||||
AccountScheduleDecision as WireAccountScheduleDecision,
|
||||
AccountScheduleRequest as WireAccountScheduleRequest,
|
||||
AccountScheduleRole as WireAccountScheduleRole,
|
||||
ModelRouteDecision as WireModelRouteDecision, ModelRouteRequest, PolicyHeader,
|
||||
},
|
||||
};
|
||||
@@ -183,6 +184,13 @@ async fn schedule_with_entry(
|
||||
.iter()
|
||||
.map(|candidate| WireAccountScheduleCandidate {
|
||||
account_id: candidate.account_id().as_str().to_owned(),
|
||||
session_bound: candidate.session_bound(),
|
||||
role: match candidate.role() {
|
||||
AccountScheduleRole::Ordinary => WireAccountScheduleRole::Ordinary,
|
||||
AccountScheduleRole::Preferred => WireAccountScheduleRole::Preferred,
|
||||
AccountScheduleRole::FollowOnly => WireAccountScheduleRole::FollowOnly,
|
||||
AccountScheduleRole::Pinned => WireAccountScheduleRole::Pinned,
|
||||
},
|
||||
available: candidate.available(),
|
||||
weight: candidate.weight(),
|
||||
in_flight: candidate.in_flight(),
|
||||
|
||||
@@ -98,6 +98,7 @@ impl PluginData {
|
||||
next_cursor: page.next_cursor.map(|id| id.as_str().to_owned()),
|
||||
})
|
||||
}
|
||||
data::QUOTA_HISTORY => return self.quota_history(payload).await,
|
||||
data::QUOTA_GET | data::QUOTA_REFRESH | data::QUOTA_DETAILS => {
|
||||
let accounts = self.accounts.upgrade().map_err(map_admin_error)?;
|
||||
let query: data::QuotaFactsQuery =
|
||||
@@ -162,4 +163,67 @@ impl PluginData {
|
||||
payload,
|
||||
})
|
||||
}
|
||||
|
||||
async fn quota_history(&self, payload: &[u8]) -> Result<RpcReply, PluginFault> {
|
||||
use gateway_admin::model::{
|
||||
observability::TimeRange,
|
||||
quota_history::{QuotaHistoryPoint, QuotaHistoryQuery},
|
||||
};
|
||||
|
||||
let wire: data::QuotaHistoryQuery =
|
||||
serde_json::from_slice(payload).map_err(|_| invalid())?;
|
||||
let id = ProviderAccountId::new(wire.account_id).map_err(|_| invalid())?;
|
||||
let time = |ms| chrono::DateTime::from_timestamp_millis(ms).ok_or_else(invalid);
|
||||
let range =
|
||||
TimeRange::new(time(wire.start_ms)?, time(wire.end_ms)?).map_err(|_| invalid())?;
|
||||
if wire.start_ms < 0
|
||||
|| wire.window_key.is_empty()
|
||||
|| wire.window_key.len() > 256
|
||||
|| wire.window_key.chars().any(char::is_control)
|
||||
{
|
||||
return Err(invalid());
|
||||
}
|
||||
let query = QuotaHistoryQuery {
|
||||
window_key: wire.window_key,
|
||||
range,
|
||||
cursor: wire
|
||||
.cursor
|
||||
.as_deref()
|
||||
.map(serde_json::from_str)
|
||||
.transpose()
|
||||
.map_err(|_| invalid())?,
|
||||
};
|
||||
if query.cursor.as_ref().is_some_and(|c| {
|
||||
c.completed_at >= range.end
|
||||
|| c.completed_at < query.scan_start()
|
||||
|| c.id.is_empty()
|
||||
|| c.id.contains('\0')
|
||||
}) {
|
||||
return Err(invalid());
|
||||
}
|
||||
let page = self
|
||||
.accounts
|
||||
.upgrade()
|
||||
.map_err(map_admin_error)?
|
||||
.quota_history(&id, query)
|
||||
.await
|
||||
.map_err(map_admin_error)?;
|
||||
let sample = |p: QuotaHistoryPoint| data::QuotaHistorySample {
|
||||
observed_at_ms: p.observed_at.timestamp_millis(),
|
||||
used_percent: p.reading.used_percent,
|
||||
reset_at_ms: p.reading.reset_at.map(|at| at.timestamp_millis()),
|
||||
plan_type: p.reading.plan_type,
|
||||
};
|
||||
super::admin::encode(&data::QuotaHistoryPage {
|
||||
schema_version: 1,
|
||||
samples: page.samples.into_iter().map(sample).collect(),
|
||||
baseline: page.baseline.map(sample),
|
||||
next_cursor: page
|
||||
.next_cursor
|
||||
.as_ref()
|
||||
.map(serde_json::to_string)
|
||||
.transpose()
|
||||
.map_err(|_| invalid())?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -216,6 +216,7 @@ impl CallbackHandler for PluginCallbacks {
|
||||
| gateway_plugin_sdk::call::data::KEYS_GET
|
||||
| gateway_plugin_sdk::call::data::QUOTA_GET
|
||||
| gateway_plugin_sdk::call::data::QUOTA_REFRESH
|
||||
| gateway_plugin_sdk::call::data::QUOTA_HISTORY
|
||||
| gateway_plugin_sdk::call::data::QUOTA_DETAILS
|
||||
) {
|
||||
return data.call(&method, params, &payload).await;
|
||||
|
||||
@@ -15,6 +15,17 @@ struct BudgetAccountAccess {
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl gateway_admin::ports::plugin_accounts::PluginAccountAccess for BudgetAccountAccess {
|
||||
async fn quota_history(
|
||||
&self,
|
||||
_: &gateway_core::account::ProviderAccountId,
|
||||
_: gateway_admin::model::quota_history::QuotaHistoryQuery,
|
||||
) -> Result<
|
||||
gateway_admin::model::quota_history::QuotaHistoryPage,
|
||||
gateway_admin::model::AdminError,
|
||||
> {
|
||||
unreachable!("账号提交不读取额度历史")
|
||||
}
|
||||
|
||||
async fn list(
|
||||
&self,
|
||||
_: gateway_admin::model::provider_credentials::PluginAccountListQuery,
|
||||
|
||||
@@ -33,7 +33,7 @@ fn account() -> AccountRecord {
|
||||
let at = "2026-01-01T00:00:00Z".parse().unwrap();
|
||||
AccountRecord {
|
||||
quota_paused: false,
|
||||
quota_limits: Default::default(),
|
||||
quota_settings: Default::default(),
|
||||
auto_use_quota_credits: false,
|
||||
outbound_proxy_id: None,
|
||||
outbound_proxy_enabled: true,
|
||||
@@ -75,6 +75,30 @@ fn account() -> AccountRecord {
|
||||
|
||||
#[async_trait]
|
||||
impl PluginAccountAccess for Facts {
|
||||
async fn quota_history(
|
||||
&self,
|
||||
id: &ProviderAccountId,
|
||||
query: gateway_admin::model::quota_history::QuotaHistoryQuery,
|
||||
) -> Result<gateway_admin::model::quota_history::QuotaHistoryPage, AdminError> {
|
||||
use gateway_admin::model::quota_history::{
|
||||
QuotaHistoryPage, QuotaHistoryPoint, QuotaReading,
|
||||
};
|
||||
assert_eq!(id.as_str(), "acct_facts");
|
||||
assert_eq!(query.window_key, "weekly");
|
||||
assert!(query.cursor.is_none());
|
||||
Ok(QuotaHistoryPage {
|
||||
samples: vec![QuotaHistoryPoint {
|
||||
observed_at: query.range.start,
|
||||
reading: QuotaReading {
|
||||
plan_type: None,
|
||||
used_percent: 12.5,
|
||||
reset_at: None,
|
||||
},
|
||||
}],
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
|
||||
async fn refresh_quota(
|
||||
&self,
|
||||
account: &ProviderAccountId,
|
||||
@@ -201,6 +225,9 @@ async fn management_facts_validate_queries_without_permission_declarations() {
|
||||
{"method":"host.data.accounts.list","query":{"provider_id":"openai","cursor":"acct_before","limit":1}},
|
||||
{"method":"host.data.quota.get","query":{"account_id":"acct_facts"}},
|
||||
{"method":"host.data.quota.details","query":{"account_id":"acct_facts"}},
|
||||
{"method":"host.data.quota.history","query":{"account_id":"acct_facts","window_key":"weekly","start_ms":1000,"end_ms":2000}},
|
||||
{"method":"host.data.quota.history","query":{"account_id":"acct_facts","window_key":"weekly","start_ms":2000,"end_ms":1000}},
|
||||
{"method":"host.data.quota.history","query":{"account_id":"acct_facts","window_key":"weekly","start_ms":1000,"end_ms":2000,"cursor":"invalid"}},
|
||||
{"method":"host.data.accounts.list","query":{"limit":0}},
|
||||
{"method":"host.data.accounts.list","query":{"limit":65535}},
|
||||
{"method":"host.data.quota.get","query":{"account_id":"acct_facts","sql":"SELECT secret"}}
|
||||
@@ -269,7 +296,12 @@ async fn management_facts_validate_queries_without_permission_declarations() {
|
||||
Some("123456789.123456789")
|
||||
);
|
||||
assert!(!result[2].to_string().contains("provider-internal-fixture"));
|
||||
for invalid in &result.as_array().unwrap()[3..] {
|
||||
let history: gateway_plugin_sdk::call::data::QuotaHistoryPage =
|
||||
serde_json::from_value(result[3].clone()).unwrap();
|
||||
assert_eq!(history.samples[0].used_percent, 12.5);
|
||||
assert_eq!(history.samples[0].observed_at_ms, 1000);
|
||||
assert_eq!(history.samples[0].reset_at_ms, None);
|
||||
for invalid in &result.as_array().unwrap()[4..] {
|
||||
assert_eq!(invalid["error"], "invalid_input");
|
||||
}
|
||||
|
||||
|
||||
@@ -682,6 +682,7 @@ async fn scheduler_cannot_pick_outside_the_host_projected_priority_tier() {
|
||||
Some("upstream-a"),
|
||||
&candidates,
|
||||
&mut selection,
|
||||
Default::default(),
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
|
||||
@@ -306,6 +306,7 @@ async fn data_plane_http_callbacks_keep_the_frozen_body_budget_across_publicatio
|
||||
None,
|
||||
&[candidate("acct_high", 100)],
|
||||
&mut selection_context(),
|
||||
Default::default(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! 验证真实插件进程的候选过滤、繁忙投影与非法决定边界
|
||||
|
||||
use super::*;
|
||||
use gateway_core::engine::policy::AccountPolicyError;
|
||||
use gateway_core::engine::policy::{AccountPolicyError, AccountScheduleOwnership};
|
||||
|
||||
#[tokio::test]
|
||||
async fn scheduler_filter_contract_covers_busy_candidates_and_rejects_out_of_scope_ids() {
|
||||
@@ -44,6 +44,10 @@ async fn scheduler_filter_contract_covers_busy_candidates_and_rejects_out_of_sco
|
||||
None,
|
||||
&candidates,
|
||||
&mut selection,
|
||||
AccountScheduleOwnership {
|
||||
bound_account: Some(candidates[0].account.id()),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await;
|
||||
match expected {
|
||||
@@ -60,8 +64,13 @@ async fn scheduler_filter_contract_covers_busy_candidates_and_rejects_out_of_sco
|
||||
request["request"]["candidates"].as_array().unwrap().len(),
|
||||
2
|
||||
);
|
||||
assert_eq!(request["request"]["candidates"][0]["available"], false);
|
||||
assert_eq!(request["request"]["candidates"][1]["available"], true);
|
||||
let candidates = &request["request"]["candidates"];
|
||||
assert_eq!(candidates[0]["available"], false);
|
||||
assert_eq!(candidates[0]["session_bound"], true);
|
||||
assert_eq!(candidates[0]["role"], "preferred");
|
||||
assert_eq!(candidates[1]["available"], true);
|
||||
assert_eq!(candidates[1]["session_bound"], false);
|
||||
assert_eq!(candidates[1]["role"], "ordinary");
|
||||
drop(policy);
|
||||
drop(generation);
|
||||
super::super::wait_until_empty(cache.path()).await;
|
||||
|
||||
@@ -107,6 +107,7 @@ async fn callbacks_are_dispatched_in_every_stage_without_permission_declarations
|
||||
"host.data.accounts.list",
|
||||
"host.data.keys.get",
|
||||
"host.data.quota.get",
|
||||
"host.data.quota.history",
|
||||
"host.groups.ensure",
|
||||
"host.groups.change_members",
|
||||
"host.keys.ensure",
|
||||
|
||||
@@ -23,7 +23,7 @@ SDK 包版本为 `0.1.0`,独立于网关版本,处于实验阶段。能力
|
||||
| `call::upstream_adapter` | 内置 OpenAI / xAI 的[受管上游适配器](docs/upstream-adapters.md)注册、执行、出站与续接合同 |
|
||||
| `call::policy` | 模型路由、账号调度与重试决策合同 |
|
||||
| `call::catalog` | 固定 Provider 的直接别名与适配模型注册合同,见[模型目录](docs/capabilities.md#模型目录) |
|
||||
| `call::data` | 最小账号事实与已有额度观测的只读合同 |
|
||||
| `call::data` | 账号事实、当前额度与历史额度观测 |
|
||||
| `call::observation` | 请求完成、用量、费用与实际上游 WebSocket 响应事件的统一观察合同 |
|
||||
| `call::host` | 账号、私有状态、受管 HTTP、模型、亲和、日志及调用内流的宿主回调合同 |
|
||||
| `call::management` | 管理 API、页面与资源声明、公开回调,以及 CLI 命令与待保存账号 |
|
||||
|
||||
@@ -89,6 +89,7 @@ Provider 固定为宿主内置的 OpenAI 与 xAI。宿主开放以下 11 类扩
|
||||
| `call.host.quota_details(query)` | `host.data.quota.details` | `QuotaFactsQuery { account_id }`:读取完整额度事实,包含窗口归属、触顶和点数 |
|
||||
| `call.host.check_access(query)` | `host.data.access.check` | `AccessQuery { client_key_id, account_ids, upstream_model }`:读取 Key 对指定账号及可选上游模型的冻结授权 |
|
||||
| `call.host.verify_client(request)` | `host.client.verify` | `VerifyClientRequest { authorization }`:复用入口认证,返回 `VerifiedClient { client_key_id }` |
|
||||
| `call.host.quota_history(query)` | `host.data.quota.history` | 按账号、窗口键和时间范围分页读取已保留的额度观测 |
|
||||
| `call.host.quota_facts(query)` | `host.data.quota.get` | `QuotaFactsQuery { account_id }`:读取 Provider 现有观测,不访问上游刷新 |
|
||||
| `call.host.key_facts(query)` | `host.data.keys.get` | `ClientKeyFactsQuery { client_key_id }`:读取当前 Key 的启用状态及显式分组 ID,不返回密钥 |
|
||||
|
||||
@@ -108,6 +109,16 @@ SDK 解析响应时忽略未知字段,包含账号分页、账号及额度窗
|
||||
`quota_details` 保留 Provider 已解释的 `limit_reached`、窗口 `group`、`label`、`limit_id`、`limit_name`、`role`
|
||||
及点数 `has_credits`、`unlimited`、`balance`;缺失值仍为 `null`,不返回上游原始文档,不发起刷新
|
||||
|
||||
`quota_history(QuotaHistoryQuery { account_id, window_key, start_ms, end_ms, cursor })` 按请求完成时间倒序返回
|
||||
`[start_ms, end_ms)` 内的同桶读数。窗口键与 `quota_details` 相同,区间最长 366 天。每个样本包含
|
||||
`observed_at_ms`、`used_percent` 和当时观测到的 `reset_at_ms`、`plan_type`,后两项可为 `null`。
|
||||
首次省略游标;即使 `samples` 为空,只要有 `next_cursor` 就沿用原查询继续。末页的 `baseline` 是起点前最近的同桶读数,
|
||||
最多向前查一个等长区间。例如查最近 48 小时,基线最多再向前找 48 小时;没有基线时返回 `null`
|
||||
|
||||
历史来自保留的已结束请求,不含独立额度刷新。Provider 未提供可解析的同桶观测时没有样本。
|
||||
缺样不代表零;跨重置或套餐变更不能直接相减。这是观测记录,不是精确消耗账本。
|
||||
分页不冻结数据,长区间建议在后台读取并复用结果
|
||||
|
||||
`verify_client` 使用当前请求设置和原认证计划,包含外部 principal 到 Key 的映射;没有 HTTP 入口时使用当前发布配置。
|
||||
认证可以调用认证插件,继续扣减父预算并响应取消;验证成功不做推理准入、账号租约、使用记录或计费。
|
||||
授权原文只作本次认证输入,不进入返回值
|
||||
@@ -653,9 +664,20 @@ middleware 的 `next` 最多调用一次,重试与额外模型执行通过独
|
||||
### 路由、调度与观察
|
||||
|
||||
`policy.route_model` 和 `policy.schedule_account` 只提出决定,宿主随后复核 Key 规则、模型能力、账号资格和租约。
|
||||
`scheduler` 必须声明 `version: 3`。`candidates` 已按 Key 范围、模型权限、账号状态和原生续写绑定收窄,
|
||||
`scheduler` 必须声明 `version: 4`。`candidates` 已按 Key 范围、模型权限、账号状态和原生续写绑定收窄,
|
||||
同时包含暂时繁忙但可以等待的账号;`available` 表示本次可以立即尝试租约,不保证租约一定成功
|
||||
|
||||
每个候选的 `session_bound` 表示它是否为 Provider 已查到的本次会话绑定账号。仅匹配缓存偏好不算会话绑定。`role` 表示本轮选号如何使用它:
|
||||
|
||||
| `role` | 含义 |
|
||||
| --- | --- |
|
||||
| `ordinary` | 普通候选。宽松模式下已有绑定也可以是普通候选 |
|
||||
| `preferred` | 内置选号优先考虑,允许选择其他账号。xAI 的会话偏好由评分得出,不表示已有持久绑定 |
|
||||
| `follow_only` | 委托内置选号时只跟随这个绑定账号,插件仍可显式选择其他合格候选 |
|
||||
| `pinned` | 本次尝试已固定账号,例如原生续接;插件不能改选其他账号 |
|
||||
|
||||
关系只标在本轮可见候选上,繁忙不抹去关系,候选中没有绑定标记也不能断定是新会话。角色不授予账号资格,不代表已取得租约
|
||||
|
||||
- `Pick { account_id }` 显式选号,只能指定 `available: true` 的候选
|
||||
- `Delegate` 将完整候选交给宿主现有调度
|
||||
- `DelegateFiltered { exclude_account_ids }` 排除列出的候选,再委托内置权重、Smart 评分和亲和选择;仅接受本次候选 ID,重复 ID 合并,空列表等同于 `Delegate`
|
||||
|
||||
@@ -54,7 +54,7 @@ RPC v7 的帧布局、编码与会话生命周期见[通信边界](../README.md#
|
||||
| 能力 | 当前版本 |
|
||||
| --- | --- |
|
||||
| `middleware` | `6` |
|
||||
| `scheduler` | `3` |
|
||||
| `scheduler` | `4` |
|
||||
| `upstream_adapter` | `5` |
|
||||
| `frontend_authentication`、`model_router`、`retry_policy`、`observer`、`management` | `2` |
|
||||
| `model_catalog` | `2` |
|
||||
|
||||
@@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize};
|
||||
|
||||
pub const ACCOUNTS_LIST: &str = "host.data.accounts.list";
|
||||
pub const QUOTA_REFRESH: &str = "host.quota_observations.refresh";
|
||||
pub const QUOTA_HISTORY: &str = "host.data.quota.history";
|
||||
pub const QUOTA_GET: &str = "host.data.quota.get";
|
||||
pub const KEYS_GET: &str = "host.data.keys.get";
|
||||
pub const CLIENT_VERIFY: &str = "host.client.verify";
|
||||
@@ -93,6 +94,35 @@ pub struct QuotaFactsQuery {
|
||||
pub account_id: String,
|
||||
}
|
||||
|
||||
/// UTC 毫秒半开区间;续页沿用原查询并原样传回游标
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct QuotaHistoryQuery {
|
||||
pub account_id: String,
|
||||
pub window_key: String,
|
||||
pub start_ms: i64,
|
||||
pub end_ms: i64,
|
||||
pub cursor: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct QuotaHistorySample {
|
||||
pub plan_type: Option<String>,
|
||||
pub observed_at_ms: i64,
|
||||
pub used_percent: f64,
|
||||
pub reset_at_ms: Option<i64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct QuotaHistoryPage {
|
||||
pub schema_version: u32,
|
||||
/// 请求完成时间倒序;空页也可能需要继续
|
||||
pub samples: Vec<QuotaHistorySample>,
|
||||
/// 最后一页才可能包含起点前的最近同桶观测
|
||||
pub baseline: Option<QuotaHistorySample>,
|
||||
pub next_cursor: Option<String>,
|
||||
}
|
||||
|
||||
/// Provider 已有快照的必要投影;空观测时间表示没有可用样本,不代表额度为零
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct QuotaFacts {
|
||||
|
||||
@@ -72,11 +72,23 @@ pub enum ModelRouteDecision {
|
||||
Reject,
|
||||
}
|
||||
|
||||
/// 宿主在本轮调度中赋予候选的角色,与实时容量和已有会话绑定分开
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum AccountScheduleRole {
|
||||
Ordinary,
|
||||
Preferred,
|
||||
FollowOnly,
|
||||
Pinned,
|
||||
}
|
||||
|
||||
/// 候选账号的即时调度指标;凭据和账号资料通过宿主账号接口读取
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct AccountScheduleCandidate {
|
||||
pub account_id: String,
|
||||
pub session_bound: bool,
|
||||
pub role: AccountScheduleRole,
|
||||
/// false 表示仅可等待,仍可被过滤,但不能作为本轮 Pick 的结果
|
||||
pub available: bool,
|
||||
pub weight: u16,
|
||||
|
||||
@@ -154,6 +154,7 @@ pub struct RuntimeSettings {
|
||||
pub openai_session_affinity_ttl_hours: u32,
|
||||
pub responses_max_decompressed_body_bytes: u64,
|
||||
pub smart_scheduling: SmartSchedulingConfig,
|
||||
pub quota_reset_window_weights: ResetWindowWeights,
|
||||
pub min_codex_desktop_version: Option<String>,
|
||||
pub min_codex_cli_version: Option<String>,
|
||||
pub usage_retention_days: u32,
|
||||
@@ -194,6 +195,7 @@ pub struct ReplaceRuntimeSettings {
|
||||
pub openai_session_affinity_ttl_hours: u32,
|
||||
pub responses_max_decompressed_body_bytes: u64,
|
||||
pub smart_scheduling: SmartSchedulingConfig,
|
||||
pub quota_reset_window_weights: ResetWindowWeights,
|
||||
pub min_codex_desktop_version: Option<String>,
|
||||
pub min_codex_cli_version: Option<String>,
|
||||
pub usage_retention_days: u32,
|
||||
@@ -405,6 +407,7 @@ impl From<RuntimeSettings> for ReplaceRuntimeSettings {
|
||||
openai_session_affinity_ttl_hours: settings.openai_session_affinity_ttl_hours,
|
||||
responses_max_decompressed_body_bytes: settings.responses_max_decompressed_body_bytes,
|
||||
smart_scheduling: settings.smart_scheduling,
|
||||
quota_reset_window_weights: settings.quota_reset_window_weights,
|
||||
min_codex_desktop_version: settings.min_codex_desktop_version,
|
||||
min_codex_cli_version: settings.min_codex_cli_version,
|
||||
usage_retention_days: settings.usage_retention_days,
|
||||
|
||||
@@ -33,7 +33,7 @@ impl Capability {
|
||||
| Self::RetryPolicy
|
||||
| Self::Observer
|
||||
| Self::Management => &[2],
|
||||
Self::Scheduler => &[3],
|
||||
Self::Scheduler => &[4],
|
||||
Self::UpstreamAdapter => &[5],
|
||||
Self::CommandLine | Self::Maintenance => &[1],
|
||||
}
|
||||
|
||||
@@ -75,6 +75,17 @@ impl HostClient {
|
||||
payload_call(self, data::QUOTA_GET, query).await
|
||||
}
|
||||
|
||||
/// 按账号、额度桶和时间范围读取已保留的观测,不刷新上游
|
||||
///
|
||||
/// # Errors
|
||||
/// 账号不存在、查询无效或宿主读取失败时返回错误
|
||||
pub async fn quota_history(
|
||||
&self,
|
||||
query: data::QuotaHistoryQuery,
|
||||
) -> Result<data::QuotaHistoryPage, PluginFault> {
|
||||
payload_call(self, data::QUOTA_HISTORY, query).await
|
||||
}
|
||||
|
||||
/// 读取已有额度窗口标签、触顶与点数事实,不触发刷新或补算
|
||||
///
|
||||
/// # Errors
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
//! 验证插件路由和账号调度决策的类型合同与附加字段透传边界
|
||||
|
||||
use gateway_plugin_sdk::call::policy::{
|
||||
AccountScheduleCandidate, AccountScheduleDecision, AccountScheduleRequest, ModelRouteDecision,
|
||||
ModelRouteRequest, PolicyHeader,
|
||||
AccountScheduleCandidate, AccountScheduleDecision, AccountScheduleRequest, AccountScheduleRole,
|
||||
ModelRouteDecision, ModelRouteRequest, PolicyHeader,
|
||||
};
|
||||
use serde_json::json;
|
||||
|
||||
@@ -49,6 +49,8 @@ fn route_and_scheduler_decisions_ignore_unused_fields() {
|
||||
model: Some("gpt-5".into()),
|
||||
candidates: vec![AccountScheduleCandidate {
|
||||
account_id: "acct_candidate".into(),
|
||||
session_bound: true,
|
||||
role: AccountScheduleRole::Preferred,
|
||||
available: true,
|
||||
weight: 10,
|
||||
in_flight: 1,
|
||||
|
||||
@@ -136,7 +136,7 @@ fn capability_contract_versions_are_checked_independently_of_rpc_version() {
|
||||
(Capability::Middleware, Stage::Request, 6, true),
|
||||
(Capability::Management, Stage::Management, 2, false),
|
||||
(Capability::UpstreamAdapter, Stage::Upstream, 5, true),
|
||||
(Capability::Scheduler, Stage::Scheduling, 3, false),
|
||||
(Capability::Scheduler, Stage::Scheduling, 4, false),
|
||||
(Capability::ModelCatalog, Stage::Registration, 2, false),
|
||||
] {
|
||||
for mut manifest in [source_manifest(), packaged_manifest()] {
|
||||
@@ -286,7 +286,7 @@ fn contribution_ids_are_owned_unique_and_stage_bounded() {
|
||||
Capability::Scheduler,
|
||||
ContributionDeclaration {
|
||||
id: "9acme.request-tags.scheduler".into(),
|
||||
version: 3,
|
||||
version: 4,
|
||||
stages: vec![Stage::Scheduling],
|
||||
input_formats: Vec::new(),
|
||||
output_formats: Vec::new(),
|
||||
|
||||
@@ -5,6 +5,7 @@ mod content;
|
||||
mod error;
|
||||
mod request;
|
||||
mod response;
|
||||
mod session;
|
||||
mod stream;
|
||||
mod thinking;
|
||||
mod tools;
|
||||
@@ -20,6 +21,7 @@ pub use request::{
|
||||
pub use response::{
|
||||
messages_to_responses_event, messages_to_responses_response, responses_to_messages_response,
|
||||
};
|
||||
pub use session::claude_session_id;
|
||||
pub use stream::{
|
||||
MessagesAccumulator, MessagesToResponsesStream, ResponsesToMessagesStream,
|
||||
message_to_stream_events,
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user