mirror of
https://github.com/zyycn/codex-proxy-rs.git
synced 2026-11-21 16:01:32 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8b69fa4882 | ||
|
|
d05d38718e | ||
|
|
a1954c4746 | ||
|
|
636a3d2a26 | ||
|
|
e9627168a7 | ||
|
|
5a4f49036b | ||
|
|
8041ddab65 | ||
|
|
e645d6d4c9 | ||
|
|
5079700190 | ||
|
|
4db65d8e12 | ||
|
|
da866de2c7 | ||
|
|
a770419818 | ||
|
|
5b37b574e6 | ||
|
|
0df6c40091 | ||
|
|
4b5443d2f2 | ||
|
|
b02f13c9cc | ||
|
|
878d2858e8 | ||
|
|
369f17d654 | ||
|
|
3709ed2484 | ||
|
|
48a5c9d690 | ||
|
|
6fc6283f3a | ||
|
|
605b60e58f | ||
|
|
1bf948a24f | ||
|
|
d90b9d129e | ||
|
|
4b1cae89b2 | ||
|
|
6cfe7ce2fc | ||
|
|
1427dfc64a | ||
|
|
9996fae5b6 | ||
|
|
6420d86aa6 | ||
|
|
f8fa386a7e | ||
|
|
7c2c532b44 | ||
|
|
188a6817d1 | ||
|
|
d44bc64d1a | ||
|
|
6006cc8394 | ||
|
|
e7f8b5a354 | ||
|
|
d62868b692 | ||
|
|
0922f3486c | ||
|
|
f4058c1d2e | ||
|
|
8253fbc497 | ||
|
|
193e83f9f3 | ||
|
|
a9e8fa8a44 | ||
|
|
437322ee4b | ||
|
|
843e1367bd | ||
|
|
ab1bf4372c | ||
|
|
dca9806961 | ||
|
|
15ed54d452 | ||
|
|
050214860e | ||
|
|
d917f948cf | ||
|
|
3d834c9e93 | ||
|
|
8d282dcdf9 | ||
|
|
38446e8a0c | ||
|
|
1173f6263d | ||
|
|
f4bed2077f | ||
|
|
4a8b839780 | ||
|
|
f02bc80f15 | ||
|
|
32a95a9702 | ||
|
|
cc80401d5e | ||
|
|
8cc6b50d85 | ||
|
|
83521e82f5 | ||
|
|
ba8745a714 | ||
|
|
84bcd0e1c0 | ||
|
|
7459c16a3a | ||
|
|
3b4fb390c7 | ||
|
|
bb440a3cbb | ||
|
|
89b6d2256f |
@@ -35,7 +35,7 @@ jobs:
|
||||
CPR_TEST_REDIS_URL: redis://127.0.0.1:6379
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:18-bookworm@sha256:bbf83594798e144bff365a9c191c5d779f181fb3135902064ab8ff11918020fc
|
||||
image: postgres:18-bookworm@sha256:194e55feb2597b1465dbee81cc9fd8d204566cd2eb42c8d22fd6c931a38781b1
|
||||
env:
|
||||
POSTGRES_DB: codex_proxy
|
||||
POSTGRES_USER: codex_proxy
|
||||
@@ -49,7 +49,7 @@ jobs:
|
||||
--health-timeout 5s
|
||||
--health-retries 10
|
||||
redis:
|
||||
image: redis:8-alpine@sha256:0bf6577d7774e592b29d093e3ac9390eb91e13d72c21e506afce736de5e0eeb0
|
||||
image: redis:8-alpine@sha256:7958cfbb9d152f87dadfbc89f1b0b15938f0b81ff8982feb327476f4911935c3
|
||||
ports:
|
||||
- 6379:6379
|
||||
options: >-
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
[](https://github.com/zyycn/codex-proxy-rs/pkgs/container/codex-proxy-rs)
|
||||
[](LICENSE)
|
||||
|
||||
[快速预览](#快速预览) · [快速开始](#快速开始) · [客户端接入](#客户端接入) · [文档](#文档) · [社区](#社区) · [许可证](#许可证)
|
||||
[快速预览](#快速预览) · [快速开始](#快速开始) · [文档](#文档) · [社区](#社区) · [贡献者](#贡献者) · [Star History](#star-history) · [支持项目](#支持项目) · [许可证](#许可证)
|
||||
|
||||
</div>
|
||||
|
||||
@@ -21,105 +21,28 @@
|
||||
|
||||
## 快速预览
|
||||
|
||||
无需部署即可体验管理端的系统概览、账号分组、代理管理与用量统计:
|
||||
|
||||
| 登录信息 | 默认值 |
|
||||
| --- | --- |
|
||||
| 地址 | <https://codex-proxy-rs.ainz.cc> |
|
||||
| 登录身份 | 管理员 |
|
||||
| 账号 | `admin@cpr.local` |
|
||||
| 密码 | `039c18de2aeac46d23ead7766bb07bbbe3747233c66a128e` |
|
||||
|
||||
预览服务运行已发布版本,展示的账号、代理与使用记录均为模拟数据,每天北京时间 `00:00` 自动生成当天数据
|
||||
|
||||
这是公开共享的功能预览环境,不提供真实模型调用;请勿导入真实账号、密钥或其他敏感信息
|
||||
|
||||
## 快速开始
|
||||
|
||||
使用 Docker Compose 部署版本固定的发布镜像,同时启动 PostgreSQL 和 Redis
|
||||
|
||||
以下命令适用于 Linux amd64/arm64,需要 Docker Engine、Docker Compose Plugin、curl 和 OpenSSL
|
||||
|
||||
macOS 从[发行页](https://github.com/zyycn/codex-proxy-rs/releases)下载对应的归档:Intel 机型选 `darwin_amd64`,Apple Silicon 机型选 `darwin_arm64`。
|
||||
Windows x64 / ARM64 的安装步骤见 [Windows 二进制部署](deploy/README.md#windows-二进制部署)
|
||||
|
||||
已有部署请先看 [升级说明](deploy/README.md#镜像升级与源码构建),不要覆盖原配置
|
||||
|
||||
### 一键安装
|
||||
|
||||
请确保当前用户能访问 Docker,并可通过 `sudo` 或 root 设置目录权限
|
||||
在已安装 Docker Engine、Docker Compose Plugin、curl 和 OpenSSL 的 Linux amd64/arm64 主机上运行
|
||||
|
||||
```bash
|
||||
curl -fsSL https://raw.githubusercontent.com/zyycn/codex-proxy-rs/main/deploy/install.sh -o install.sh && bash install.sh
|
||||
```
|
||||
|
||||
[安装脚本](deploy/install.sh) 默认安装到当前目录下的 `codex-proxy-rs/`,下载同一正式 Release 的部署文件,
|
||||
自动生成密码、设置目录权限并启动服务
|
||||
|
||||
完成后保存显示的访问地址和管理员密码,再[登录管理端](#登录管理端)并[添加账号与客户端密钥](#添加账号与客户端密钥)
|
||||
|
||||
可在执行 `bash install.sh` 时传入环境变量:
|
||||
|
||||
| 变量 | 用途 | 默认值 |
|
||||
| --- | --- | --- |
|
||||
| `INSTALL_DIR` | 安装目录,建议使用绝对路径 | 当前目录下的 `codex-proxy-rs/` |
|
||||
| `CPR_RELEASE_TAG` | 指定发布标签 | 最新正式版本 |
|
||||
| `ADMIN_PASSWORD` | 管理员初始密码,至少 12 位,不能包含 `$`,不能使用常见弱口令 | 随机生成 |
|
||||
|
||||
例如,自定义安装目录:
|
||||
|
||||
```bash
|
||||
INSTALL_DIR="$HOME/services/codex-proxy-rs" bash install.sh
|
||||
```
|
||||
|
||||
重复运行时请使用同一安装目录,检测到 `deploy/config.yaml` 后,脚本保留现有配置和部署文件,
|
||||
忽略传入的管理员密码,也不执行版本升级
|
||||
|
||||
### 手动安装
|
||||
|
||||
自行下载部署文件、配置密码和启动服务的完整步骤见 [部署文档](deploy/README.md#手动安装)
|
||||
|
||||
### 登录管理端
|
||||
|
||||
部署完成后,打开 `http://127.0.0.1:8080`,使用 `admin@cpr.local` 和管理员密码登录
|
||||
|
||||
API Key 持有者可在登录页切换身份,查看自己的用量与额度,并通过「密钥配置」复制 Codex 配置或导入 CC Switch
|
||||
|
||||
默认地址只能在服务器本机访问,从其他设备使用时,需要配置
|
||||
[HTTPS 反向代理](deploy/README.md#公网访问)
|
||||
|
||||
### 添加账号与客户端密钥
|
||||
|
||||
1. 在「账号」中添加账号,完成授权或导入
|
||||
2. 按需建立账号分组,再创建客户端密钥并选择可用分组,**不选分组表示可使用全部账号**
|
||||
3. [连接客户端](#客户端接入)
|
||||
|
||||
## 客户端接入
|
||||
|
||||
**Codex CLI / 桌面端**:在「使用密钥」中按操作系统复制配置,合并配置后重启 Codex;也可通过「导入到」把服务连接与密钥添加到 CC Switch
|
||||
|
||||
完整步骤、生图配置与排障见[客户端配置](deploy/README.md#客户端配置)
|
||||
|
||||
**其他 API 客户端**:API Key 填写管理端创建的客户端密钥。按客户端自动追加的请求路径填写 Base URL;远程接入时将本机地址换成服务器的 HTTPS 地址
|
||||
|
||||
| 客户端自动追加的路径 | Base URL(本机部署) |
|
||||
| --- | --- |
|
||||
| OpenAI Responses:`/responses`;Chat Completions:`/chat/completions` | `http://127.0.0.1:8080/v1` |
|
||||
| Anthropic Messages:`/v1/messages` | `http://127.0.0.1:8080` |
|
||||
|
||||
查询该密钥可见的模型目录:
|
||||
|
||||
```bash
|
||||
curl http://127.0.0.1:8080/v1/models \
|
||||
-H 'Authorization: Bearer <client-api-key>'
|
||||
```
|
||||
[安装脚本选项](deploy/README.md#安装脚本选项) · [macOS 发行包](https://github.com/zyycn/codex-proxy-rs/releases) · [Windows 安装](deploy/README.md#windows-二进制部署) · [已有部署升级](deploy/README.md#镜像升级与源码构建)
|
||||
|
||||
## 文档
|
||||
|
||||
| 任务 | 文档 |
|
||||
| 主题 | 文档 |
|
||||
| --- | --- |
|
||||
| 部署与使用 | [部署、备份与恢复](deploy/README.md) · [客户端接入与生图](deploy/README.md#客户端配置) |
|
||||
| 接口集成 | [API 参考](docs/api.md) · [模型定价](docs/api.md#模型定价) |
|
||||
| 部署与使用 | [部署、备份与恢复](deploy/README.md) · [客户端接入](deploy/README.md#客户端配置) |
|
||||
| 接口集成 | [API 参考](docs/api.md) · [模型目录](docs/api.md#模型目录) · [模型定价](docs/api.md#模型定价) |
|
||||
| 使用插件 | [安装、配置与使用](docs/plugins.md) |
|
||||
| 开发插件 | [SDK 与合同](backend/crates/gateway-plugin/sdk/README.md) · [打包工具](backend/apps/plugin-cli/README.md) · [官方示例](https://github.com/zyycn/codex-proxy-plugins/blob/main/README.md) |
|
||||
| 开发共用组件 | [组件库说明](https://github.com/zyycn/codex-proxy-ui/blob/main/README.md) · [组件参考](https://github.com/zyycn/codex-proxy-ui/blob/main/playground/components/index.md) · [Playground](https://zyycn.github.io/codex-proxy-ui/) |
|
||||
@@ -132,6 +55,28 @@ curl http://127.0.0.1:8080/v1/models \
|
||||
|
||||
感谢 [LINUX DO](https://linux.do) 社区提供开放、友善的技术交流平台
|
||||
|
||||
## 贡献者
|
||||
|
||||
[<img src="https://contrib.rocks/image?repo=zyycn/codex-proxy-rs&columns=10" alt="查看 GitHub 贡献者列表" width="480">](https://github.com/zyycn/codex-proxy-rs/graphs/contributors)
|
||||
|
||||
## Star History
|
||||
|
||||
<a href="https://simrepo.dera.page/#zyycn/codex-proxy-rs&Date">
|
||||
<picture>
|
||||
<source media="(prefers-color-scheme: dark)" srcset="https://simrepo.dera.page/svg?repos=zyycn/codex-proxy-rs&type=Date&theme=dark">
|
||||
<img src="https://simrepo.dera.page/svg?repos=zyycn/codex-proxy-rs&type=Date" alt="Codex Proxy RS 的 GitHub Star 增长趋势" width="560">
|
||||
</picture>
|
||||
</a>
|
||||
|
||||
## 支持项目
|
||||
|
||||
如果 Codex Proxy RS 帮助到你了,欢迎请我喝杯咖啡 ☕
|
||||
|
||||
[](https://ko-fi.com/zyycn)
|
||||
|
||||
[<img src="docs/donations/wechat-tips.png" alt="WeChat appreciation code" width="138">](docs/donations/wechat-tips.png)
|
||||
[<img src="docs/donations/alipay.png" alt="Alipay QR code" width="138">](docs/donations/alipay.png)
|
||||
|
||||
## 许可证
|
||||
|
||||
本项目基于 [Apache License 2.0](LICENSE) 开源
|
||||
|
||||
Generated
+6
-6
@@ -3006,9 +3006,9 @@ checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
|
||||
|
||||
[[package]]
|
||||
name = "openssl"
|
||||
version = "0.10.80"
|
||||
version = "0.10.82"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a45fa2aa886c42762255da344f0a0d313e254066c46aad76f300c3d3da62d967"
|
||||
checksum = "4de8111e94a56adca7ebfe6c365b6d42f99deb69b74b6becacd4204e2f193aa2"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"cfg-if",
|
||||
@@ -3052,9 +3052,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "openssl-sys"
|
||||
version = "0.9.116"
|
||||
version = "0.9.118"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f28a22dc7140cda5f096e5e7724a6962ca81a7f8bfd2979f9b18c11af56318c4"
|
||||
checksum = "19c413d6ec9ebaf5e1c829d355cb3e9f783778d700f135be0921d52fce3c60ae"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"libc",
|
||||
@@ -4797,7 +4797,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "tokio-tungstenite"
|
||||
version = "0.28.0"
|
||||
source = "git+https://github.com/openai-oss-forks/tokio-tungstenite?rev=0e5b2d73aa18dd9f0a50ee9ff199d5aef7594186#0e5b2d73aa18dd9f0a50ee9ff199d5aef7594186"
|
||||
source = "git+https://github.com/openai-oss-forks/tokio-tungstenite?rev=7447c427513cfe685f236e1ac0513c4ea3dd9c0b#7447c427513cfe685f236e1ac0513c4ea3dd9c0b"
|
||||
dependencies = [
|
||||
"futures-util",
|
||||
"log",
|
||||
@@ -5006,7 +5006,7 @@ checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b"
|
||||
[[package]]
|
||||
name = "tungstenite"
|
||||
version = "0.27.0"
|
||||
source = "git+https://github.com/openai-oss-forks/tungstenite-rs?rev=4fffad30fe373adbdcffab9545e9e9bf4f2fc19f#4fffad30fe373adbdcffab9545e9e9bf4f2fc19f"
|
||||
source = "git+https://github.com/openai-oss-forks/tungstenite-rs?rev=bc589d684755a8c02da0fc7e58d0861cbfc306f1#bc589d684755a8c02da0fc7e58d0861cbfc306f1"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"data-encoding",
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
# Codex Proxy Plugin CLI
|
||||
|
||||
`cpr-plugin` 校验并规范化作者清单、收集声明的资源,生成可安装归档及 SHA-256 校验文件。
|
||||
输出合同与 [SDK 版本](../../crates/gateway-plugin/sdk/README.md)一致
|
||||
`cpr-plugin` 按 [SDK 合同](../../crates/gateway-plugin/sdk/README.md)校验清单,将插件可执行文件与声明的资源打包,并输出 SHA-256 校验文件
|
||||
|
||||
## 安装
|
||||
|
||||
@@ -11,7 +10,7 @@
|
||||
cargo +1.99.1 install --locked --path backend/apps/plugin-cli --root /path/to/local-tools
|
||||
```
|
||||
|
||||
将 `/path/to/local-tools/bin` 加入 `PATH`,或使用可执行文件的完整路径;工具链与宿主 [验证命令](../../../docs/architecture.md#验证命令)一致
|
||||
将 `/path/to/local-tools/bin` 加入 `PATH`,或使用可执行文件的完整路径
|
||||
|
||||
## 打包
|
||||
|
||||
@@ -26,24 +25,20 @@ cpr-plugin package \
|
||||
--output-dir /path/to/output
|
||||
```
|
||||
|
||||
`--resource-map web=web/dist` 将包内 `web/` 前缀映射到作者清单所在目录下的 `web/dist/`。
|
||||
可重复传入不同前缀,源目录必须位于项目内;没有对应资源时省略该参数,未使用的映射会被拒绝。
|
||||
未映射的资源按清单中的相对路径读取,只收集 `resources` 声明的文件
|
||||
资源文件须在 `resources` 中声明,默认从清单所在目录按相对路径读取。
|
||||
`--resource-map web=web/dist` 从项目内的 `web/dist/` 读取包内 `web/` 资源。可为不同前缀重复传参,未匹配任何资源的映射会报错
|
||||
|
||||
输出为 `<插件 ID>-<版本>-<目标 triple>.tar.gz` 及同名 `.tar.gz.sha256`;归档根目录直接包含 `plugin.json`、可执行文件和声明资源。
|
||||
输出为 `<插件 ID>-<版本>-<目标 triple>.tar.gz` 及对应的 `.tar.gz.sha256`。归档根目录包含 `plugin.json`、可执行文件和声明资源。
|
||||
输入清单及生成后的清单均不得超过 64 KiB,压缩包上限为 32 MiB,展开归档上限为 128 MiB
|
||||
|
||||
作者清单不填写 `package`;字段省略规则、能力版本和挂载阶段见 [SDK 扩展项声明](../../crates/gateway-plugin/sdk/docs/manifest.md#扩展项简写)。
|
||||
CLI 生成规范化后的 `plugin.json`,补入协议版本、目标平台和文件摘要。打包成功只证明输入符合包合同,实际启动与能力仍需在目标宿主验证
|
||||
作者清单须省略 `package`。CLI 将协议版本、目标平台和文件摘要写入 `plugin.json`。可省略的声明字段见 [SDK 扩展项声明](../../crates/gateway-plugin/sdk/docs/manifest.md#扩展项简写)
|
||||
|
||||
支持 `x86_64-unknown-linux-gnu`、`aarch64-unknown-linux-gnu` 和 `aarch64-apple-darwin`。
|
||||
完整开发示例在独立仓库 `codex-proxy-plugins` 的 `examples/workbench`,可通过[子模块](../../../docs/development.md#源码联调)检出;本工具不负责源码构建、上传或启用插件
|
||||
打包后在目标宿主验证启动和插件功能。完整示例见插件[子模块](../../../docs/development.md#源码联调)的 `examples/workbench`
|
||||
|
||||
### 图标资源
|
||||
|
||||
在作者清单的 `icon` 中指定图标,并在 `resources` 中登记对应路径与 MIME 类型,配置方式见
|
||||
[SDK 插件图标](../../crates/gateway-plugin/sdk/docs/manifest.md#插件图标)。图标与其他声明资源一样参与打包和摘要计算,
|
||||
无需额外命令参数;工具保留文件原始内容,宿主安装时验证图标内容与资源限制
|
||||
按 [SDK 插件图标](../../crates/gateway-plugin/sdk/docs/manifest.md#插件图标)填写 `icon` 和 `resources`。CLI 自动打包原文件并计算摘要,宿主在安装时校验
|
||||
|
||||
## 开发
|
||||
|
||||
|
||||
@@ -183,7 +183,7 @@ async fn initialize_inner(
|
||||
PluginDistributionPorts::new(plugin_distribution, store.proxies()),
|
||||
snapshot.clone(),
|
||||
plugin_preparation,
|
||||
published_snapshot,
|
||||
published_snapshot.clone(),
|
||||
store.plugin_state(),
|
||||
)),
|
||||
key_usage,
|
||||
@@ -201,6 +201,7 @@ async fn initialize_inner(
|
||||
store.account_groups(),
|
||||
store.account_runtime(),
|
||||
snapshot.clone(),
|
||||
published_snapshot,
|
||||
)),
|
||||
client_keys: Arc::new(DefaultClientKeyService::new(
|
||||
store.client_keys(),
|
||||
|
||||
@@ -4,7 +4,7 @@ use std::collections::BTreeMap;
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use gateway_core::{
|
||||
account::{AccountStatusFacts, FastMode},
|
||||
account::{AccountModelAccess, AccountStatusFacts, FastMode},
|
||||
routing::AccountGroupId,
|
||||
};
|
||||
|
||||
@@ -14,15 +14,25 @@ use super::{PageSize, Revision, observability::DecimalAmount};
|
||||
pub fn validate_group_fields(
|
||||
name: &str,
|
||||
description: Option<&str>,
|
||||
) -> Result<(), super::AdminModelError> {
|
||||
if name.trim() != name
|
||||
|| name.is_empty()
|
||||
|| name.chars().count() > 100
|
||||
|| name.chars().any(char::is_control)
|
||||
|| description
|
||||
.is_some_and(|value| value.len() > 4096 || value.chars().any(char::is_control))
|
||||
{
|
||||
return Err(super::AdminModelError::InvalidAccountGroupFields);
|
||||
) -> Result<(), super::AdminError> {
|
||||
if name.trim().is_empty() {
|
||||
return Err(super::AdminError::invalid("请输入账号组名称"));
|
||||
}
|
||||
if name.chars().count() > 100 {
|
||||
return Err(super::AdminError::invalid("账号组名称最多 100 个字符"));
|
||||
}
|
||||
if name.trim() != name || name.chars().any(char::is_control) {
|
||||
return Err(super::AdminError::invalid(
|
||||
"账号组名称不能包含首尾空白或控制字符",
|
||||
));
|
||||
}
|
||||
if let Some(description) = description {
|
||||
if description.len() > 4096 {
|
||||
return Err(super::AdminError::invalid("账号组描述最多 4096 字节"));
|
||||
}
|
||||
if description.chars().any(char::is_control) {
|
||||
return Err(super::AdminError::invalid("账号组描述不能包含控制字符"));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -102,6 +112,8 @@ pub struct AccountGroupListQuery {
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct AccountGroupRecord {
|
||||
pub allow_image_generation: bool,
|
||||
pub allow_subagents: bool,
|
||||
pub model_access: AccountModelAccess,
|
||||
pub fast_mode: FastMode,
|
||||
pub id: AccountGroupId,
|
||||
pub name: String,
|
||||
@@ -142,6 +154,8 @@ pub struct AccountGroupOptionsPage {
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct CreateAccountGroup {
|
||||
pub allow_image_generation: bool,
|
||||
pub allow_subagents: bool,
|
||||
pub model_access: AccountModelAccess,
|
||||
pub fast_mode: FastMode,
|
||||
pub name: String,
|
||||
pub description: Option<String>,
|
||||
@@ -152,6 +166,8 @@ pub struct CreateAccountGroup {
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct NewAccountGroup {
|
||||
pub allow_image_generation: bool,
|
||||
pub allow_subagents: bool,
|
||||
pub model_access: AccountModelAccess,
|
||||
pub fast_mode: FastMode,
|
||||
pub id: AccountGroupId,
|
||||
pub name: String,
|
||||
@@ -163,6 +179,8 @@ pub struct NewAccountGroup {
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct UpdateAccountGroup {
|
||||
pub allow_image_generation: Option<bool>,
|
||||
pub allow_subagents: Option<bool>,
|
||||
pub model_access: Option<AccountModelAccess>,
|
||||
pub fast_mode: Option<FastMode>,
|
||||
pub id: AccountGroupId,
|
||||
pub name: String,
|
||||
|
||||
@@ -36,6 +36,7 @@ pub enum MutationAuditOperation {
|
||||
PluginSourceCredentialCreate,
|
||||
PluginSourceCredentialDelete,
|
||||
ClientApiKeyUpdateBudgetLimits,
|
||||
ClientApiKeyBatchUpdate,
|
||||
ClientApiKeyCreate,
|
||||
ClientApiKeyUpdate,
|
||||
ClientApiKeyEnabled { enabled: bool },
|
||||
@@ -97,6 +98,7 @@ impl MutationAuditOperation {
|
||||
Self::PluginSourceCredentialCreate => ("create", "plugin_source_credential"),
|
||||
Self::PluginSourceCredentialDelete => ("delete", "plugin_source_credential"),
|
||||
Self::ClientApiKeyUpdateBudgetLimits => ("update_budget_limits", "client_api_key"),
|
||||
Self::ClientApiKeyBatchUpdate => ("batch_update", "client_api_key"),
|
||||
Self::ClientApiKeyCreate => ("create", "client_api_key"),
|
||||
Self::ClientApiKeyUpdate => ("update", "client_api_key"),
|
||||
Self::ClientApiKeyEnabled { enabled } => {
|
||||
|
||||
@@ -218,6 +218,66 @@ pub struct UpdateClientKey {
|
||||
pub weekly_limit_usd: Option<gateway_core::metering::Decimal>,
|
||||
}
|
||||
|
||||
/// 批量编辑只覆盖明确选择的互联选项
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct ClientPeerSharingPatch {
|
||||
pub enabled: Option<bool>,
|
||||
pub share_pool: Option<bool>,
|
||||
pub share_quota: Option<bool>,
|
||||
pub share_plans: Option<bool>,
|
||||
}
|
||||
|
||||
impl ClientPeerSharingPatch {
|
||||
#[must_use]
|
||||
pub fn is_empty(self) -> bool {
|
||||
self == Self::default()
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn apply(
|
||||
self,
|
||||
current: gateway_core::policy::ClientPeerSharing,
|
||||
) -> gateway_core::policy::ClientPeerSharing {
|
||||
gateway_core::policy::ClientPeerSharing {
|
||||
enabled: self.enabled.unwrap_or(current.enabled),
|
||||
share_pool: self.share_pool.unwrap_or(current.share_pool),
|
||||
share_quota: self.share_quota.unwrap_or(current.share_quota),
|
||||
share_plans: self.share_plans.unwrap_or(current.share_plans),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 对各个密钥独立应用同一份局部设置
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct BatchUpdateClientKeys {
|
||||
pub ids: Vec<ClientApiKeyId>,
|
||||
pub group_ids: Option<Vec<AccountGroupId>>,
|
||||
pub enabled: Option<bool>,
|
||||
pub max_concurrency: Option<u64>,
|
||||
pub requests_per_minute: Option<u64>,
|
||||
pub daily_limit_usd: Option<gateway_core::metering::Decimal>,
|
||||
pub weekly_limit_usd: Option<gateway_core::metering::Decimal>,
|
||||
pub peer_sharing: ClientPeerSharingPatch,
|
||||
pub request_profile_override_updates: ProviderRequestProfileOverrideUpdates,
|
||||
pub installation_seed: Option<uuid::Uuid>,
|
||||
}
|
||||
|
||||
impl BatchUpdateClientKeys {
|
||||
#[must_use]
|
||||
pub fn has_changes(&self) -> bool {
|
||||
self.group_ids.is_some()
|
||||
|| self.enabled.is_some()
|
||||
|| self.max_concurrency.is_some()
|
||||
|| self.requests_per_minute.is_some()
|
||||
|| self.daily_limit_usd.is_some()
|
||||
|| self.weekly_limit_usd.is_some()
|
||||
|| !self.peer_sharing.is_empty()
|
||||
|| !self.request_profile_override_updates.is_empty()
|
||||
|| self.installation_seed.is_some()
|
||||
}
|
||||
}
|
||||
|
||||
/// 修改 Client Key 启用状态
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct SetClientKeyEnabled {
|
||||
@@ -257,9 +317,69 @@ impl ClientKeyBudgetWindowPeriod {
|
||||
}
|
||||
}
|
||||
|
||||
/// 手动对齐只需要账号身份,列表不读取凭据、额度或用量
|
||||
#[derive(serde::Serialize, Debug)]
|
||||
pub struct BudgetReferenceAccount {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub email: Option<String>,
|
||||
pub provider: String,
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize, Debug)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct BudgetReferenceAccountPage {
|
||||
pub items: Vec<BudgetReferenceAccount>,
|
||||
pub next_cursor: Option<String>,
|
||||
}
|
||||
|
||||
/// 管理员可预览的账号窗口保留 Provider 的稳定标识
|
||||
pub struct ClientKeyBudgetReference {
|
||||
pub key: String,
|
||||
pub label: String,
|
||||
pub period: ClientKeyBudgetWindowPeriod,
|
||||
pub bounds: ExplicitClientKeyBudgetWindow,
|
||||
}
|
||||
|
||||
impl ClientKeyBudgetReference {
|
||||
/// 只取整账号当前的日/周窗口,对齐结束后恢复密钥自身周期
|
||||
pub fn from_current_window(
|
||||
window: super::provider_credentials::ProviderQuotaWindow,
|
||||
now: DateTime<Utc>,
|
||||
) -> Option<Self> {
|
||||
use super::provider_credentials::QuotaLocalUsageAttribution;
|
||||
if window.local_usage_attribution != QuotaLocalUsageAttribution::AccountWide
|
||||
|| window
|
||||
.upstream
|
||||
.as_ref()
|
||||
.is_some_and(|upstream| upstream.stale)
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let period = match window.window_seconds? {
|
||||
86_400 => ClientKeyBudgetWindowPeriod::Daily,
|
||||
604_800 => ClientKeyBudgetWindowPeriod::Weekly,
|
||||
_ => return None,
|
||||
};
|
||||
let (starts_at, ends_at) = window.observed_bounds()?;
|
||||
(starts_at..ends_at).contains(&now).then_some(Self {
|
||||
key: window.key,
|
||||
label: window.label,
|
||||
period,
|
||||
bounds: ExplicitClientKeyBudgetWindow {
|
||||
starts_at,
|
||||
ends_at,
|
||||
resume_calendar: true,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// 按请求完成时间归属的左闭右开预算窗口
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct ExplicitClientKeyBudgetWindow {
|
||||
/// 一次性对齐的窗口到期后恢复原生滚动
|
||||
pub resume_calendar: bool,
|
||||
pub starts_at: DateTime<Utc>,
|
||||
pub ends_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
@@ -236,8 +236,6 @@ pub enum AdminModelError {
|
||||
InvalidPageSize(u16),
|
||||
#[error("observability page size {0} is outside 1..=100")]
|
||||
InvalidObservabilityPageSize(u16),
|
||||
#[error("account group name or description is invalid")]
|
||||
InvalidAccountGroupFields,
|
||||
#[error("client key group IDs are duplicated or exceed 1000 entries")]
|
||||
InvalidClientKeyGroups,
|
||||
#[error("client key page size must be inside 1..=65535")]
|
||||
|
||||
@@ -158,7 +158,7 @@ impl OtherRequestOutcome {
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
|
||||
pub struct UsageFilter {
|
||||
pub client_api_key_name: Option<String>,
|
||||
pub provider_account_name: Option<String>,
|
||||
pub provider_account_search: Option<String>,
|
||||
pub client_api_key_ref: Option<String>,
|
||||
pub request_id: Option<String>,
|
||||
pub provider_account_ref: Option<String>,
|
||||
@@ -180,7 +180,7 @@ pub struct UsageFilter {
|
||||
pub enum UsageRecordStatus {
|
||||
#[default]
|
||||
Success,
|
||||
Error,
|
||||
Error(Option<UsageErrorOrigin>),
|
||||
All,
|
||||
}
|
||||
|
||||
@@ -648,6 +648,56 @@ pub struct AttemptMetrics {
|
||||
pub costs: Vec<CurrencyCost>,
|
||||
}
|
||||
|
||||
/// 管理员按当前指标排列模型与趋势,值只来自固定的聚合列
|
||||
#[derive(Debug, Clone, Copy, Default, serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum KeyUsageMetric {
|
||||
#[default]
|
||||
CostUsd,
|
||||
Tokens,
|
||||
Requests,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct KeyUsageTotals {
|
||||
pub requests: u64,
|
||||
pub tokens: Option<u64>,
|
||||
pub cost_usd: Option<String>,
|
||||
pub has_missing_cost: bool,
|
||||
pub has_estimated_cost: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct KeyModelRow {
|
||||
pub model: String,
|
||||
pub totals: KeyUsageTotals,
|
||||
pub peak_requests: u64,
|
||||
pub active_days: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct KeyTrendPoint {
|
||||
pub date: chrono::NaiveDate,
|
||||
pub value: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct KeyTrendSeries {
|
||||
/// 空字符串表示未记录模型,None 表示合并的其他模型
|
||||
pub model: Option<String>,
|
||||
pub points: Vec<KeyTrendPoint>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
|
||||
pub struct KeyStatisticsOverview {
|
||||
pub totals: KeyUsageTotals,
|
||||
pub series: Vec<KeyTrendSeries>,
|
||||
#[serde(rename = "modelCount")]
|
||||
pub model_count: u64,
|
||||
}
|
||||
|
||||
/// 时间序列粒度
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum Granularity {
|
||||
@@ -814,6 +864,17 @@ pub enum UsageErrorOrigin {
|
||||
Unknown,
|
||||
}
|
||||
|
||||
/// 来源规则所依赖的已保存事实,供内存投影与持久层分页查询共同解释
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum UsageErrorEvidence {
|
||||
ProviderErrorCode,
|
||||
UpstreamStatusAtLeast(u16),
|
||||
SavedUpstreamErrorOrClose,
|
||||
ErrorKind(&'static [&'static str]),
|
||||
Continuation(&'static [&'static str]),
|
||||
ConnectionExit(&'static [&'static str]),
|
||||
}
|
||||
|
||||
impl UsageErrorOrigin {
|
||||
#[must_use]
|
||||
pub const fn as_str(self) -> &'static str {
|
||||
|
||||
@@ -700,6 +700,7 @@ pub struct PreparedCredentialRotationFacts {
|
||||
pub preserve_profile: bool,
|
||||
/// 连接配置变更保留当前凭据健康状态与错误事实
|
||||
pub preserve_credential_state: bool,
|
||||
pub preserve_access_token_expiry: bool,
|
||||
pub provider_material: ProviderDocument,
|
||||
pub has_refresh_token: bool,
|
||||
pub access_token_expires_at: Option<DateTime<Utc>>,
|
||||
@@ -819,6 +820,14 @@ pub struct ProviderQuotaWindow {
|
||||
pub provider_data: Option<ProviderDocument>,
|
||||
}
|
||||
|
||||
impl ProviderQuotaWindow {
|
||||
pub(crate) fn observed_bounds(&self) -> Option<(DateTime<Utc>, DateTime<Utc>)> {
|
||||
let ends_at = self.reset_at?;
|
||||
let duration = chrono::Duration::try_seconds(i64::try_from(self.window_seconds?).ok()?)?;
|
||||
Some((ends_at.checked_sub_signed(duration)?, ends_at))
|
||||
}
|
||||
}
|
||||
|
||||
/// 账号用量统计周期,完整周/月额度优先,随后使用 Provider 提供的本地滚动日统计
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub enum AccountUsagePeriod {
|
||||
|
||||
@@ -9,7 +9,6 @@ use super::{PageSize, Revision, account_groups::AccountGroupRef};
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum AccountProxySelection {
|
||||
Direct,
|
||||
Url(OutboundProxy),
|
||||
Saved(String),
|
||||
}
|
||||
|
||||
|
||||
@@ -45,6 +45,8 @@ pub use gateway_core::account::RotationStrategy;
|
||||
/// 运行设置的共同值;版本、Provider 画像更新和数据库秘密由外层类型拥有
|
||||
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
pub struct RuntimeSettingsValues {
|
||||
pub openai_oauth_guardian_review_model: bool,
|
||||
pub openai_request_country: gateway_core::account::OpenAiRequestCountry,
|
||||
pub codex_privacy_policy: gateway_core::settings::privacy::CodexPrivacyPolicy,
|
||||
pub request_location_enabled: bool,
|
||||
pub request_location: gateway_core::account::RequestLocation,
|
||||
@@ -73,7 +75,6 @@ pub struct RuntimeSettingsValues {
|
||||
pub account_warmup_enabled: bool,
|
||||
pub account_warmup_cron: String,
|
||||
pub account_warmup_model: Option<String>,
|
||||
#[serde(default)]
|
||||
pub reset_credit_on_low_weekly_quota: bool,
|
||||
pub reset_credit_lead_minutes: u32,
|
||||
pub reset_credit_max_weekly_remaining_percent: Option<u32>,
|
||||
@@ -200,6 +201,9 @@ impl RuntimeSettingsValues {
|
||||
{
|
||||
return Err("reset_credit_max_weekly_remaining_percent");
|
||||
}
|
||||
self.openai_request_country
|
||||
.validate()
|
||||
.map_err(|_| "openai_request_country")?;
|
||||
self.request_location
|
||||
.validate()
|
||||
.map_err(|_| "request_location")?;
|
||||
|
||||
@@ -231,6 +231,19 @@ pub trait ProviderAdmin: Send + Sync {
|
||||
self.preview_client_profile(key)
|
||||
}
|
||||
|
||||
/// 在批量更新中合并本地身份;缺省、清除和替换分别保留各自语义,不能执行 I/O
|
||||
fn merge_batch_key_profile(
|
||||
&self,
|
||||
current: Option<&gateway_core::account::OpaqueProviderData>,
|
||||
update: Option<&Option<gateway_core::account::OpaqueProviderData>>,
|
||||
installation_seed: Option<uuid::Uuid>,
|
||||
) -> Result<Option<gateway_core::account::OpaqueProviderData>, ProviderAdminError> {
|
||||
if installation_seed.is_some() {
|
||||
return Err(ProviderAdminError::new(ProviderAdminErrorKind::Unsupported));
|
||||
}
|
||||
Ok(update.map_or_else(|| current.cloned(), Clone::clone))
|
||||
}
|
||||
|
||||
/// 提供最终套餐展示名称;未覆盖时采用通用的大驼峰格式
|
||||
fn plan_type_display(&self, plan_type: &str) -> String {
|
||||
default_plan_type_display(plan_type)
|
||||
|
||||
@@ -22,10 +22,11 @@ use crate::model::{
|
||||
},
|
||||
auth::{AdminAuditEvent, AuthSession},
|
||||
client_keys::{
|
||||
ClientKeyBudgetMutationOrigin, ClientKeyBudgetWindow, ClientKeyBudgetWindowPeriod,
|
||||
ClientKeyListQuery, ClientKeyPage, ClientKeyRecord, ClientKeySecret, DeleteClientKey,
|
||||
NewClientKey, ResetClientKeyBudget, SetClientKeyBudgetWindow, SetClientKeyEnabled,
|
||||
UpdateClientKey, UpdateClientKeyBudgetLimits,
|
||||
BatchUpdateClientKeys, BudgetReferenceAccountPage, ClientKeyBudgetMutationOrigin,
|
||||
ClientKeyBudgetWindow, ClientKeyBudgetWindowPeriod, ClientKeyListQuery, ClientKeyPage,
|
||||
ClientKeyRecord, ClientKeySecret, DeleteClientKey, NewClientKey, ResetClientKeyBudget,
|
||||
SetClientKeyBudgetWindow, SetClientKeyEnabled, UpdateClientKey,
|
||||
UpdateClientKeyBudgetLimits,
|
||||
},
|
||||
observability::{
|
||||
DashboardObservation, DashboardQuery, DiagnosticDimension, DiagnosticsObservation,
|
||||
@@ -109,6 +110,13 @@ impl AdminStoreError {
|
||||
|
||||
pub type AdminStoreResult<T> = Result<T, AdminStoreError>;
|
||||
|
||||
/// 在持有密钥行锁时用 Provider 准备好的纯合并操作更新原有画像
|
||||
pub type BatchClientKeyProfileMerger = Box<
|
||||
dyn Fn(&mut crate::model::client_keys::ProviderRequestProfileOverrides) -> AdminStoreResult<()>
|
||||
+ Send
|
||||
+ Sync,
|
||||
>;
|
||||
|
||||
/// 账号目录与公共账号写操作
|
||||
#[async_trait]
|
||||
pub trait AccountStore:
|
||||
@@ -377,6 +385,18 @@ pub trait AuthStore: Send + Sync {
|
||||
/// Client API Key 资料读取与管理写入
|
||||
#[async_trait]
|
||||
pub trait ClientKeyStore: Send + Sync {
|
||||
async fn client_key_budget(
|
||||
&self,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
) -> AdminStoreResult<Option<gateway_core::engine::budget::ClientBudgetStatus>>;
|
||||
|
||||
async fn budget_reference_accounts(
|
||||
&self,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
search: &str,
|
||||
after: &str,
|
||||
) -> AdminStoreResult<BudgetReferenceAccountPage>;
|
||||
|
||||
async fn client_key_budget_window(
|
||||
&self,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
@@ -415,6 +435,13 @@ pub trait ClientKeyStore: Send + Sync {
|
||||
context: &MutationContext,
|
||||
) -> AdminStoreResult<(Revision, ClientKeyRecord)>;
|
||||
|
||||
async fn batch_update_client_keys(
|
||||
&self,
|
||||
command: BatchUpdateClientKeys,
|
||||
merge_profiles: BatchClientKeyProfileMerger,
|
||||
context: &MutationContext,
|
||||
) -> AdminStoreResult<Revision>;
|
||||
|
||||
async fn set_client_key_enabled(
|
||||
&self,
|
||||
command: SetClientKeyEnabled,
|
||||
@@ -480,6 +507,7 @@ pub trait AccountGroupStore: Send + Sync {
|
||||
context: &MutationContext,
|
||||
) -> AdminStoreResult<AccountGroupMutation>;
|
||||
|
||||
/// 仍有 API Key 引用分组时返回 Conflict
|
||||
async fn delete_account_group(
|
||||
&self,
|
||||
command: DeleteAccountGroup,
|
||||
@@ -517,6 +545,19 @@ pub trait ObservabilityStore: Send + Sync {
|
||||
granularity: Granularity,
|
||||
) -> AdminStoreResult<Vec<RequestMetricPoint>>;
|
||||
|
||||
async fn client_key_statistics(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
query: crate::model::observability::KeyUsageMetric,
|
||||
) -> AdminStoreResult<crate::model::observability::KeyStatisticsOverview>;
|
||||
|
||||
async fn client_key_models(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
) -> AdminStoreResult<Vec<crate::model::observability::KeyModelRow>>;
|
||||
|
||||
async fn usage_trend(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
|
||||
@@ -9,8 +9,8 @@ use std::{
|
||||
use async_trait::async_trait;
|
||||
use gateway_core::{
|
||||
account::{AccountStatus, resolve_account_status},
|
||||
routing::AccountGroupId,
|
||||
runtime::SnapshotControl,
|
||||
routing::{AccountGroupId, UpstreamModelId},
|
||||
runtime::{RuntimeSnapshotHandle, SnapshotControl},
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
@@ -24,7 +24,7 @@ use crate::{
|
||||
NewAccountGroup, SetAccountGroupEnabled, UpdateAccountGroup,
|
||||
},
|
||||
},
|
||||
ports::store::{AccountGroupStore, AccountRuntimeStore},
|
||||
ports::store::{AccountGroupStore, AccountRuntimeStore, AdminStoreErrorKind},
|
||||
};
|
||||
|
||||
use super::{map_store_error, publish_committed};
|
||||
@@ -32,6 +32,7 @@ use super::{map_store_error, publish_committed};
|
||||
/// 面向 API 的账号分组管理服务
|
||||
#[async_trait]
|
||||
pub trait AccountGroupService: Send + Sync {
|
||||
fn models(&self, group: &AccountGroupId) -> Result<Vec<UpstreamModelId>, AdminError>;
|
||||
async fn list(&self, query: AccountGroupListQuery) -> Result<AccountGroupPage, AdminError>;
|
||||
async fn list_options(
|
||||
&self,
|
||||
@@ -64,6 +65,7 @@ pub(crate) struct DefaultAccountGroupService {
|
||||
store: Arc<dyn AccountGroupStore>,
|
||||
runtime: Arc<dyn AccountRuntimeStore>,
|
||||
snapshot: Arc<dyn SnapshotControl>,
|
||||
published_snapshot: RuntimeSnapshotHandle,
|
||||
}
|
||||
|
||||
impl DefaultAccountGroupService {
|
||||
@@ -73,12 +75,14 @@ impl DefaultAccountGroupService {
|
||||
store: Arc<dyn AccountGroupStore>,
|
||||
runtime: Arc<dyn AccountRuntimeStore>,
|
||||
snapshot: Arc<dyn SnapshotControl>,
|
||||
published_snapshot: RuntimeSnapshotHandle,
|
||||
) -> Self {
|
||||
Self {
|
||||
upstream_info,
|
||||
store,
|
||||
runtime,
|
||||
snapshot,
|
||||
published_snapshot,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,6 +135,14 @@ impl DefaultAccountGroupService {
|
||||
|
||||
#[async_trait]
|
||||
impl AccountGroupService for DefaultAccountGroupService {
|
||||
fn models(&self, group: &AccountGroupId) -> Result<Vec<UpstreamModelId>, AdminError> {
|
||||
let snapshot = self
|
||||
.published_snapshot
|
||||
.acquire()
|
||||
.map_err(|_| AdminError::unavailable("模型目录尚未就绪"))?;
|
||||
Ok(snapshot.upstream_models_for_group(group))
|
||||
}
|
||||
|
||||
async fn list(&self, query: AccountGroupListQuery) -> Result<AccountGroupPage, AdminError> {
|
||||
let mut page = self
|
||||
.store
|
||||
@@ -159,8 +171,7 @@ impl AccountGroupService for DefaultAccountGroupService {
|
||||
crate::model::account_groups::validate_group_fields(
|
||||
&command.name,
|
||||
command.description.as_deref(),
|
||||
)
|
||||
.map_err(|_| AdminError::invalid("账号组请求不合法"))?;
|
||||
)?;
|
||||
let id = AccountGroupId::new(format!("grp_{}", Uuid::now_v7().simple()))
|
||||
.map_err(|_| AdminError::internal("创建账号组 ID 失败"))?;
|
||||
self.publish(
|
||||
@@ -168,6 +179,8 @@ impl AccountGroupService for DefaultAccountGroupService {
|
||||
.create_account_group(
|
||||
NewAccountGroup {
|
||||
allow_image_generation: command.allow_image_generation,
|
||||
allow_subagents: command.allow_subagents,
|
||||
model_access: command.model_access,
|
||||
id,
|
||||
name: command.name,
|
||||
description: command.description,
|
||||
@@ -189,8 +202,7 @@ impl AccountGroupService for DefaultAccountGroupService {
|
||||
crate::model::account_groups::validate_group_fields(
|
||||
&command.name,
|
||||
command.description.as_deref(),
|
||||
)
|
||||
.map_err(|_| AdminError::invalid("账号组请求不合法"))?;
|
||||
)?;
|
||||
self.publish(self.store.update_account_group(command, context).await)
|
||||
.await
|
||||
}
|
||||
@@ -209,8 +221,12 @@ impl AccountGroupService for DefaultAccountGroupService {
|
||||
context: &MutationContext,
|
||||
command: DeleteAccountGroup,
|
||||
) -> Result<AccountGroupMutation, AdminError> {
|
||||
self.publish(self.store.delete_account_group(command, context).await)
|
||||
.await
|
||||
match self.store.delete_account_group(command, context).await {
|
||||
Err(error) if error.kind() == AdminStoreErrorKind::Conflict => {
|
||||
Err(AdminError::conflict("分组仍绑定 API Key,请先解除绑定").with_source(error))
|
||||
}
|
||||
result => self.publish(result).await,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,11 +20,13 @@ use gateway_core::{
|
||||
use crate::{
|
||||
model::{
|
||||
AdminError, MutationContext,
|
||||
account_groups::AccountGroupRef,
|
||||
accounts::{
|
||||
AccountConnectionTestEvent, AccountConnectionTestEventStream, AccountListQuery,
|
||||
AccountPageItem, AccountUpdateResult, AccountUsage, AccountUsageWindowQuery,
|
||||
AccountsUpdateResult, BatchUpdateAccounts, UpdateAccount,
|
||||
},
|
||||
client_keys::ClientKeyBudgetReference,
|
||||
observability::TimeRange,
|
||||
provider_credentials::{
|
||||
AccountDirectoryItem, AccountDirectoryPage, AccountExportBundle, AccountPersonalInfo,
|
||||
@@ -107,6 +109,12 @@ pub trait AccountsService: Send + Sync {
|
||||
refresh: bool,
|
||||
) -> Result<AccountDirectoryItem, AdminError>;
|
||||
|
||||
async fn key_budget_references(
|
||||
&self,
|
||||
account_id: &ProviderAccountId,
|
||||
key_groups: &[AccountGroupRef],
|
||||
) -> Result<Vec<ClientKeyBudgetReference>, AdminError>;
|
||||
|
||||
async fn quota_forecast(
|
||||
&self,
|
||||
account_id: &ProviderAccountId,
|
||||
@@ -972,6 +980,48 @@ impl AccountsService for DefaultAccountsService {
|
||||
self.load_directory_item(account_id, refresh).await
|
||||
}
|
||||
|
||||
async fn key_budget_references(
|
||||
&self,
|
||||
account_id: &ProviderAccountId,
|
||||
key_groups: &[AccountGroupRef],
|
||||
) -> Result<Vec<ClientKeyBudgetReference>, AdminError> {
|
||||
let (stored, provider) = self.provider_for_account(account_id).await?;
|
||||
if !key_groups.is_empty()
|
||||
&& !stored.account.groups.iter().any(|account_group| {
|
||||
account_group.enabled && key_groups.iter().any(|group| group.id == account_group.id)
|
||||
})
|
||||
{
|
||||
return Err(AdminError::bad_request("参考账号不在此密钥的账号范围内"));
|
||||
}
|
||||
// 只取已观测的额度边界,预览与确认都不触发上游刷新或账号用量聚合
|
||||
let quota = match super::peer_link::upstream::read_quota(
|
||||
self.upstream_info.as_deref(),
|
||||
provider.as_ref(),
|
||||
&stored.account,
|
||||
ProviderQuotaRequest {
|
||||
account_id: account_id.clone(),
|
||||
refresh: false,
|
||||
rolling_usage: None,
|
||||
},
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(quota) => quota,
|
||||
Err(error)
|
||||
if error.kind() == crate::ports::provider::ProviderAdminErrorKind::Unsupported =>
|
||||
{
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
Err(error) => return Err(map_provider_error(error, "provider quota")),
|
||||
};
|
||||
let now = Utc::now();
|
||||
Ok(quota
|
||||
.windows
|
||||
.into_iter()
|
||||
.filter_map(|window| ClientKeyBudgetReference::from_current_window(window, now))
|
||||
.collect())
|
||||
}
|
||||
|
||||
async fn quota_forecast(
|
||||
&self,
|
||||
account_id: &ProviderAccountId,
|
||||
@@ -1304,9 +1354,7 @@ fn quota_usage_window(
|
||||
if window.local_usage_attribution != QuotaLocalUsageAttribution::AccountWide {
|
||||
return None;
|
||||
}
|
||||
let reset_at = window.reset_at?;
|
||||
let seconds = i64::try_from(window.window_seconds?).ok()?;
|
||||
let start = reset_at.checked_sub_signed(Duration::try_seconds(seconds)?)?;
|
||||
let (start, reset_at) = window.observed_bounds()?;
|
||||
let range = TimeRange::new(start, reset_at).ok()?;
|
||||
// 上游百分比以该 reset 边界定义;以当前时间回推会让本地 Token 属于另一窗口
|
||||
Some(AccountUsageWindowQuery {
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
//! Client API Key 管理用例
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::{collections::BTreeSet, sync::Arc};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};
|
||||
use gateway_core::{
|
||||
engine::budget::ClientBudgetStatus, policy::ClientApiKeyId, runtime::SnapshotControl,
|
||||
account::OpaqueProviderData, engine::budget::ClientBudgetStatus, policy::ClientApiKeyId,
|
||||
routing::ProviderKind, runtime::SnapshotControl,
|
||||
};
|
||||
use rand_core::{OsRng, RngCore as _};
|
||||
use uuid::Uuid;
|
||||
@@ -14,15 +15,17 @@ use crate::{
|
||||
model::{
|
||||
AdminError, MutationContext,
|
||||
client_keys::{
|
||||
ClientKeyBudgetMutationOrigin, ClientKeyBudgetWindow, ClientKeyBudgetWindowPeriod,
|
||||
ClientKeyCursorValue, ClientKeyListQuery, ClientKeyMutation, ClientKeyPage,
|
||||
ClientKeyRecord, ClientKeySecret, ClientKeySortField, CreateClientKey,
|
||||
CreatedClientKey, DeleteClientKey, NewClientKey, ResetClientKeyBudget,
|
||||
SetClientKeyBudgetWindow, SetClientKeyEnabled, UpdateClientKey,
|
||||
BatchUpdateClientKeys, BudgetReferenceAccountPage, ClientKeyBudgetMutationOrigin,
|
||||
ClientKeyBudgetWindow, ClientKeyBudgetWindowPeriod, ClientKeyCursorValue,
|
||||
ClientKeyListQuery, ClientKeyMutation, ClientKeyPage, ClientKeyRecord, ClientKeySecret,
|
||||
ClientKeySortField, CreateClientKey, CreatedClientKey, DeleteClientKey, NewClientKey,
|
||||
ResetClientKeyBudget, SetClientKeyBudgetWindow, SetClientKeyEnabled, UpdateClientKey,
|
||||
UpdateClientKeyBudgetLimits,
|
||||
},
|
||||
},
|
||||
ports::store::{AdminStoreError, AdminStoreErrorKind, ClientKeyStore},
|
||||
ports::store::{
|
||||
AdminStoreError, AdminStoreErrorKind, BatchClientKeyProfileMerger, ClientKeyStore,
|
||||
},
|
||||
};
|
||||
|
||||
use super::{map_store_error, publish_committed};
|
||||
@@ -30,6 +33,13 @@ use super::{map_store_error, publish_committed};
|
||||
/// API 消费的 Client Key 管理服务
|
||||
#[async_trait]
|
||||
pub trait ClientKeyService: Send + Sync {
|
||||
async fn budget_reference_accounts(
|
||||
&self,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
search: &str,
|
||||
after: &str,
|
||||
) -> Result<BudgetReferenceAccountPage, AdminError>;
|
||||
|
||||
async fn budget_window(
|
||||
&self,
|
||||
id: &ClientApiKeyId,
|
||||
@@ -55,6 +65,11 @@ pub trait ClientKeyService: Send + Sync {
|
||||
context: &MutationContext,
|
||||
command: UpdateClientKey,
|
||||
) -> Result<ClientKeyMutation, AdminError>;
|
||||
async fn batch_update(
|
||||
&self,
|
||||
context: &MutationContext,
|
||||
command: BatchUpdateClientKeys,
|
||||
) -> Result<crate::model::Revision, AdminError>;
|
||||
async fn set_enabled(
|
||||
&self,
|
||||
context: &MutationContext,
|
||||
@@ -99,10 +114,123 @@ impl DefaultClientKeyService {
|
||||
providers,
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_profile(
|
||||
&self,
|
||||
kind: &ProviderKind,
|
||||
profile: &OpaqueProviderData,
|
||||
) -> Result<(), AdminError> {
|
||||
self.providers
|
||||
.require(kind)
|
||||
.and_then(|provider| provider.preview_key_client_profile(profile, None))
|
||||
.map(|_| ())
|
||||
.map_err(|error| super::map_provider_error(error, "client profile"))
|
||||
}
|
||||
|
||||
fn batch_profile_merger(
|
||||
&self,
|
||||
command: &BatchUpdateClientKeys,
|
||||
) -> Result<BatchClientKeyProfileMerger, AdminError> {
|
||||
let mut kinds = command
|
||||
.request_profile_override_updates
|
||||
.keys()
|
||||
.cloned()
|
||||
.collect::<BTreeSet<_>>();
|
||||
let seed_kind = ProviderKind::new("openai").expect("static Provider kind is valid");
|
||||
if command.installation_seed.is_some() {
|
||||
kinds.insert(seed_kind.clone());
|
||||
}
|
||||
let mut changes = Vec::with_capacity(kinds.len());
|
||||
for kind in kinds {
|
||||
let provider = self
|
||||
.providers
|
||||
.require(&kind)
|
||||
.map_err(|error| super::map_provider_error(error, "client profile"))?;
|
||||
let update = command.request_profile_override_updates.get(&kind).cloned();
|
||||
let seed = if kind == seed_kind {
|
||||
command.installation_seed
|
||||
} else {
|
||||
None
|
||||
};
|
||||
// 先校验输入,行锁内只依据各把密钥的现值完成合并
|
||||
if let Some(Some(profile)) = update.as_ref() {
|
||||
provider
|
||||
.merge_batch_key_profile(None, update.as_ref(), seed)
|
||||
.and_then(|_| provider.preview_key_client_profile(profile, None))
|
||||
.map_err(|error| super::map_provider_error(error, "client profile"))?;
|
||||
}
|
||||
changes.push((kind, provider, update, seed));
|
||||
}
|
||||
Ok(Box::new(move |profiles| {
|
||||
for (kind, provider, update, seed) in &changes {
|
||||
let next = provider
|
||||
.merge_batch_key_profile(profiles.get(kind), update.as_ref(), *seed)
|
||||
.map_err(|error| {
|
||||
AdminStoreError::new(
|
||||
AdminStoreErrorKind::Invalid,
|
||||
"client profile",
|
||||
"invalid stored client profile",
|
||||
)
|
||||
.with_source(error)
|
||||
})?;
|
||||
if let Some(profile) = next {
|
||||
profiles.insert(kind.clone(), profile);
|
||||
} else {
|
||||
profiles.remove(kind);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ClientKeyService for DefaultClientKeyService {
|
||||
async fn batch_update(
|
||||
&self,
|
||||
context: &MutationContext,
|
||||
command: BatchUpdateClientKeys,
|
||||
) -> Result<crate::model::Revision, AdminError> {
|
||||
if command.ids.is_empty()
|
||||
|| command.ids.len() > 1000
|
||||
|| command
|
||||
.ids
|
||||
.iter()
|
||||
.collect::<std::collections::BTreeSet<_>>()
|
||||
.len()
|
||||
!= command.ids.len()
|
||||
{
|
||||
return Err(AdminError::invalid("请选择 1 至 1000 个不同的 API Key"));
|
||||
}
|
||||
if !command.has_changes() {
|
||||
return Err(AdminError::invalid("请至少选择一项要修改的密钥设置"));
|
||||
}
|
||||
if let Some(groups) = &command.group_ids {
|
||||
crate::model::client_keys::validate_group_ids(groups)
|
||||
.map_err(|_| AdminError::invalid("API Key 分组不合法"))?;
|
||||
}
|
||||
let merge_profiles = self.batch_profile_merger(&command)?;
|
||||
let revision = self
|
||||
.store
|
||||
.batch_update_client_keys(command, merge_profiles, context)
|
||||
.await
|
||||
.map_err(|error| map_store_error(error, "client API key"))?;
|
||||
publish_committed(self.snapshot.as_ref(), revision).await?;
|
||||
Ok(revision)
|
||||
}
|
||||
|
||||
async fn budget_reference_accounts(
|
||||
&self,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
search: &str,
|
||||
after: &str,
|
||||
) -> Result<BudgetReferenceAccountPage, AdminError> {
|
||||
self.store
|
||||
.budget_reference_accounts(id, search.trim(), after)
|
||||
.await
|
||||
.map_err(|error| map_store_error(error, "client API key"))
|
||||
}
|
||||
|
||||
async fn budget_window(
|
||||
&self,
|
||||
id: &ClientApiKeyId,
|
||||
@@ -141,7 +269,11 @@ impl ClientKeyService for DefaultClientKeyService {
|
||||
}
|
||||
|
||||
async fn budget(&self, id: &ClientApiKeyId) -> Result<ClientBudgetStatus, AdminError> {
|
||||
self.get(id).await.map(|key| key.budget)
|
||||
self.store
|
||||
.client_key_budget(id)
|
||||
.await
|
||||
.map_err(|error| map_store_error(error, "client API key"))?
|
||||
.ok_or_else(|| AdminError::not_found("API Key 不存在"))
|
||||
}
|
||||
|
||||
async fn update_budget_limits(
|
||||
@@ -203,10 +335,7 @@ impl ClientKeyService for DefaultClientKeyService {
|
||||
crate::model::client_keys::validate_group_ids(&command.group_ids)
|
||||
.map_err(|_| AdminError::invalid("API Key 分组不合法"))?;
|
||||
for (provider, profile) in &command.request_profile_overrides {
|
||||
self.providers
|
||||
.require(provider)
|
||||
.and_then(|provider| provider.preview_key_client_profile(profile, None))
|
||||
.map_err(|error| super::map_provider_error(error, "client profile"))?;
|
||||
self.validate_profile(provider, profile)?;
|
||||
}
|
||||
validate_peer_sharing(command.peer_sharing, &command.group_ids)?;
|
||||
let id = ClientApiKeyId::new(format!("key_{}", Uuid::now_v7().simple()))
|
||||
@@ -250,10 +379,7 @@ impl ClientKeyService for DefaultClientKeyService {
|
||||
.map_err(|_| AdminError::invalid("API Key 分组不合法"))?;
|
||||
for (provider, profile) in &command.request_profile_override_updates {
|
||||
if let Some(profile) = profile {
|
||||
self.providers
|
||||
.require(provider)
|
||||
.and_then(|provider| provider.preview_key_client_profile(profile, None))
|
||||
.map_err(|error| super::map_provider_error(error, "client profile"))?;
|
||||
self.validate_profile(provider, profile)?;
|
||||
}
|
||||
}
|
||||
if let Some(sharing) = command.peer_sharing {
|
||||
@@ -314,7 +440,6 @@ impl ClientKeyService for DefaultClientKeyService {
|
||||
|
||||
fn map_client_key_write_error(error: AdminStoreError) -> AdminError {
|
||||
match error.kind() {
|
||||
AdminStoreErrorKind::DuplicateName => AdminError::conflict("名称已存在"),
|
||||
AdminStoreErrorKind::Conflict => AdminError::conflict("API Key 已存在,请使用其他密钥"),
|
||||
_ => map_store_error(error, "client API key"),
|
||||
}
|
||||
|
||||
@@ -150,7 +150,6 @@ pub(super) async fn pending_authorization(
|
||||
None => {
|
||||
use crate::model::proxies::AccountProxySelection;
|
||||
proxy = match &command.outbound_proxy {
|
||||
Some(AccountProxySelection::Url(value)) => Some(value.clone()),
|
||||
Some(AccountProxySelection::Saved(id)) => {
|
||||
let record = proxies
|
||||
.get(id)
|
||||
|
||||
@@ -4,20 +4,15 @@ use crate::model::{AdminError, AdminErrorKind};
|
||||
use crate::ports::store::{AdminStoreError, AdminStoreErrorKind};
|
||||
|
||||
pub(super) fn map_store_error(error: AdminStoreError, resource: &'static str) -> AdminError {
|
||||
let kind = match error.kind() {
|
||||
AdminStoreErrorKind::Invalid => AdminErrorKind::Invalid,
|
||||
AdminStoreErrorKind::NotFound => AdminErrorKind::NotFound,
|
||||
AdminStoreErrorKind::StaleRevision
|
||||
| AdminStoreErrorKind::DuplicateName
|
||||
| AdminStoreErrorKind::Conflict => AdminErrorKind::Conflict,
|
||||
AdminStoreErrorKind::Unavailable => AdminErrorKind::Unavailable,
|
||||
};
|
||||
let message = match kind {
|
||||
AdminErrorKind::Invalid => "请求参数不合法",
|
||||
AdminErrorKind::NotFound => "请求的资源不存在",
|
||||
AdminErrorKind::Conflict => "当前资源状态冲突,请刷新后重试",
|
||||
AdminErrorKind::Unavailable => "依赖服务暂不可用",
|
||||
_ => "服务内部错误",
|
||||
let (kind, message) = match error.kind() {
|
||||
AdminStoreErrorKind::Invalid => (AdminErrorKind::Invalid, "请求参数不合法"),
|
||||
AdminStoreErrorKind::NotFound => (AdminErrorKind::NotFound, "请求的资源不存在"),
|
||||
AdminStoreErrorKind::StaleRevision => {
|
||||
(AdminErrorKind::Conflict, "数据已更新,请刷新后再操作")
|
||||
}
|
||||
AdminStoreErrorKind::DuplicateName => (AdminErrorKind::Conflict, "名称已存在"),
|
||||
AdminStoreErrorKind::Conflict => (AdminErrorKind::Conflict, "当前资源状态不允许此操作"),
|
||||
AdminStoreErrorKind::Unavailable => (AdminErrorKind::Unavailable, "存储服务暂不可用"),
|
||||
};
|
||||
AdminError::new(kind, message).with_source(OperationFailure {
|
||||
operation: resource,
|
||||
@@ -49,9 +44,8 @@ pub(super) fn map_provider_error(
|
||||
ProviderAdminErrorKind::Conflict => "服务商资源状态冲突,请刷新后重试",
|
||||
ProviderAdminErrorKind::BadGateway => "上游服务请求失败",
|
||||
ProviderAdminErrorKind::Ambiguous => "上游执行结果未知,请刷新状态后再决定是否重试",
|
||||
ProviderAdminErrorKind::Unavailable | ProviderAdminErrorKind::CredentialRefreshRequired => {
|
||||
"服务商暂不可用"
|
||||
}
|
||||
ProviderAdminErrorKind::Unavailable => "服务商暂不可用",
|
||||
ProviderAdminErrorKind::CredentialRefreshRequired => "账号凭据需要刷新",
|
||||
ProviderAdminErrorKind::Internal => "服务内部错误",
|
||||
};
|
||||
AdminError::new(kind, error.public_message().unwrap_or(message)).with_source(OperationFailure {
|
||||
|
||||
@@ -225,7 +225,7 @@ impl KeyUsageService for DefaultKeyUsageService {
|
||||
.list_usage_records(UsageQuery {
|
||||
status: match query.kind {
|
||||
KeyUsageRecordKind::Success => UsageRecordStatus::Success,
|
||||
KeyUsageRecordKind::Error => UsageRecordStatus::Error,
|
||||
KeyUsageRecordKind::Error => UsageRecordStatus::Error(None),
|
||||
KeyUsageRecordKind::All => UsageRecordStatus::All,
|
||||
},
|
||||
range: query.usage.range,
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
//! 从已保存的错误事实投影管理端来源,不参与执行错误分类或重试
|
||||
|
||||
use crate::model::observability::{UsageErrorOrigin, UsageListRecord, UsageRecord};
|
||||
use crate::model::observability::{
|
||||
UsageErrorEvidence, UsageErrorOrigin, UsageListRecord, UsageRecord,
|
||||
};
|
||||
|
||||
impl UsageListRecord {
|
||||
#[must_use]
|
||||
@@ -47,107 +49,124 @@ fn error_origin(
|
||||
continuation_reason: Option<&str>,
|
||||
connection_exit_reason: Option<&str>,
|
||||
) -> UsageErrorOrigin {
|
||||
if has_upstream_error
|
||||
|| upstream_code.is_some_and(|code| !code.is_empty())
|
||||
|| websocket_close_code.is_some()
|
||||
|| upstream_status_code.is_some_and(|status| status >= 400)
|
||||
|| matches!(
|
||||
continuation_reason,
|
||||
Some("upstream_rejected" | "quota_exhausted")
|
||||
)
|
||||
|| matches!(
|
||||
error_kind,
|
||||
// 这里匹配内部 errorKind;message_too_big 只由收到的上游 close 1009 产生
|
||||
Some("permission_denied" | "message_too_big" | "upstream_capacity_unavailable")
|
||||
)
|
||||
{
|
||||
return UsageErrorOrigin::Upstream;
|
||||
}
|
||||
if matches!(
|
||||
continuation_reason,
|
||||
Some(
|
||||
"scope_unavailable"
|
||||
| "pool_unavailable"
|
||||
| "fresh_connection_required"
|
||||
| "connection_busy"
|
||||
| "latest_response_mismatch"
|
||||
| "reused_connection_lost"
|
||||
| "transport_payload_too_large"
|
||||
)
|
||||
) || matches!(
|
||||
error_kind,
|
||||
Some(
|
||||
"process_interrupted"
|
||||
| "process_terminated"
|
||||
| "internal_error"
|
||||
| "model_not_found"
|
||||
| "no_available_provider"
|
||||
| "no_eligible_account"
|
||||
| "account_capacity_unavailable"
|
||||
| "middleware_capacity_unavailable"
|
||||
| "concurrency_queue_full"
|
||||
| "concurrency_queue_timeout"
|
||||
| "provider_infrastructure_unavailable"
|
||||
| "session_policy_restricted"
|
||||
| "request_policy_denied"
|
||||
| "policy_denied"
|
||||
| "budget_exhausted"
|
||||
)
|
||||
) || matches!(
|
||||
connection_exit_reason,
|
||||
Some(
|
||||
"command_channel_closed"
|
||||
| "local_close"
|
||||
| "message_receiver_closed"
|
||||
| "client_cancelled"
|
||||
| "downstream_send_failed"
|
||||
| "downstream_connection_closed"
|
||||
| "downstream_rebound"
|
||||
| "pool_shutdown"
|
||||
| "max_age_expired"
|
||||
| "idle_timeout_expired"
|
||||
| "proxy_capacity_evicted"
|
||||
| "search_activation_required"
|
||||
)
|
||||
) {
|
||||
return UsageErrorOrigin::Proxy;
|
||||
}
|
||||
if matches!(
|
||||
connection_exit_reason,
|
||||
Some(
|
||||
"upstream_close"
|
||||
| "normal_close"
|
||||
| "unexpected_binary_event"
|
||||
| "incomplete_response"
|
||||
| "failed_response"
|
||||
)
|
||||
) {
|
||||
return UsageErrorOrigin::Upstream;
|
||||
}
|
||||
if error_kind == Some("transport")
|
||||
|| matches!(
|
||||
connection_exit_reason,
|
||||
Some(
|
||||
"eof"
|
||||
| "unexpected_eof"
|
||||
| "transport_error"
|
||||
| "connection_reset"
|
||||
| "connection_aborted"
|
||||
| "broken_pipe"
|
||||
| "tcp_reset"
|
||||
| "reset_without_closing_handshake"
|
||||
| "transport_timeout"
|
||||
| "outbound_transport_error"
|
||||
| "receive_idle_timeout"
|
||||
| "pong_timeout"
|
||||
| "ping_write_timeout"
|
||||
| "pong_write_timeout"
|
||||
)
|
||||
)
|
||||
{
|
||||
return UsageErrorOrigin::Connection;
|
||||
}
|
||||
// invalid_request、protocol、rate_limited 等分类同时用于本地和上游失败
|
||||
// 发送状态与笼统的连接退出摘要也不能替代缺失的来源事实
|
||||
UsageErrorOrigin::Unknown
|
||||
use UsageErrorEvidence::*;
|
||||
UsageErrorOrigin::RULES
|
||||
.iter()
|
||||
.find(|(evidence, _)| match evidence {
|
||||
ProviderErrorCode => upstream_code.is_some_and(|code| !code.is_empty()),
|
||||
UpstreamStatusAtLeast(min) => upstream_status_code.is_some_and(|status| status >= *min),
|
||||
SavedUpstreamErrorOrClose => has_upstream_error || websocket_close_code.is_some(),
|
||||
ErrorKind(values) => error_kind.is_some_and(|value| values.contains(&value)),
|
||||
Continuation(values) => {
|
||||
continuation_reason.is_some_and(|value| values.contains(&value))
|
||||
}
|
||||
ConnectionExit(values) => {
|
||||
connection_exit_reason.is_some_and(|value| values.contains(&value))
|
||||
}
|
||||
})
|
||||
.map_or(UsageErrorOrigin::Unknown, |(_, origin)| *origin)
|
||||
}
|
||||
|
||||
impl UsageErrorOrigin {
|
||||
/// 第一条命中的规则决定来源;含义不唯一的分类在缺少其他证据时保留 unknown
|
||||
pub const RULES: &'static [(UsageErrorEvidence, Self)] = {
|
||||
use UsageErrorEvidence::*;
|
||||
&[
|
||||
(ProviderErrorCode, Self::Upstream),
|
||||
(UpstreamStatusAtLeast(400), Self::Upstream),
|
||||
(
|
||||
Continuation(&["upstream_rejected", "quota_exhausted"]),
|
||||
Self::Upstream,
|
||||
),
|
||||
// message_too_big 只由收到的上游 close 1009 产生
|
||||
(
|
||||
ErrorKind(&[
|
||||
"permission_denied",
|
||||
"message_too_big",
|
||||
"upstream_capacity_unavailable",
|
||||
]),
|
||||
Self::Upstream,
|
||||
),
|
||||
(SavedUpstreamErrorOrClose, Self::Upstream),
|
||||
(
|
||||
Continuation(&[
|
||||
"scope_unavailable",
|
||||
"pool_unavailable",
|
||||
"fresh_connection_required",
|
||||
"connection_busy",
|
||||
"latest_response_mismatch",
|
||||
"reused_connection_lost",
|
||||
"transport_payload_too_large",
|
||||
]),
|
||||
Self::Proxy,
|
||||
),
|
||||
(
|
||||
ErrorKind(&[
|
||||
"process_interrupted",
|
||||
"process_terminated",
|
||||
"internal_error",
|
||||
"model_not_found",
|
||||
"no_available_provider",
|
||||
"no_eligible_account",
|
||||
"account_capacity_unavailable",
|
||||
"middleware_capacity_unavailable",
|
||||
"concurrency_queue_full",
|
||||
"concurrency_queue_timeout",
|
||||
"provider_infrastructure_unavailable",
|
||||
"session_policy_restricted",
|
||||
"request_policy_denied",
|
||||
"policy_denied",
|
||||
"budget_exhausted",
|
||||
]),
|
||||
Self::Proxy,
|
||||
),
|
||||
(
|
||||
ConnectionExit(&[
|
||||
"command_channel_closed",
|
||||
"local_close",
|
||||
"message_receiver_closed",
|
||||
"client_cancelled",
|
||||
"downstream_send_failed",
|
||||
"downstream_connection_closed",
|
||||
"downstream_rebound",
|
||||
"pool_shutdown",
|
||||
"max_age_expired",
|
||||
"idle_timeout_expired",
|
||||
"proxy_capacity_evicted",
|
||||
"search_activation_required",
|
||||
]),
|
||||
Self::Proxy,
|
||||
),
|
||||
(
|
||||
ConnectionExit(&[
|
||||
"upstream_close",
|
||||
"normal_close",
|
||||
"unexpected_binary_event",
|
||||
"incomplete_response",
|
||||
"failed_response",
|
||||
]),
|
||||
Self::Upstream,
|
||||
),
|
||||
(ErrorKind(&["transport"]), Self::Connection),
|
||||
(
|
||||
ConnectionExit(&[
|
||||
"eof",
|
||||
"unexpected_eof",
|
||||
"transport_error",
|
||||
"connection_reset",
|
||||
"connection_aborted",
|
||||
"broken_pipe",
|
||||
"tcp_reset",
|
||||
"reset_without_closing_handshake",
|
||||
"transport_timeout",
|
||||
"outbound_transport_error",
|
||||
"receive_idle_timeout",
|
||||
"pong_timeout",
|
||||
"ping_write_timeout",
|
||||
"pong_write_timeout",
|
||||
]),
|
||||
Self::Connection,
|
||||
),
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
@@ -60,6 +60,19 @@ pub trait ObservabilityService: Send + Sync {
|
||||
-> Result<Trend, AdminError>;
|
||||
async fn usage_records(&self, query: UsageQuery) -> Result<UsagePage, AdminError>;
|
||||
async fn usage_record_detail(&self, request_id: &str) -> Result<UsageDetail, AdminError>;
|
||||
async fn client_key_statistics(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
query: crate::model::observability::KeyUsageMetric,
|
||||
) -> Result<crate::model::observability::KeyStatisticsOverview, AdminError>;
|
||||
|
||||
async fn client_key_models(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
) -> Result<Vec<crate::model::observability::KeyModelRow>, AdminError>;
|
||||
|
||||
async fn usage_summary(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
@@ -287,6 +300,29 @@ impl ObservabilityService for DefaultObservabilityService {
|
||||
Ok(detail)
|
||||
}
|
||||
|
||||
async fn client_key_statistics(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
query: crate::model::observability::KeyUsageMetric,
|
||||
) -> Result<crate::model::observability::KeyStatisticsOverview, AdminError> {
|
||||
self.store
|
||||
.client_key_statistics(range, id, query)
|
||||
.await
|
||||
.map_err(|error| map_store_error(error, "client key statistics"))
|
||||
}
|
||||
|
||||
async fn client_key_models(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
) -> Result<Vec<crate::model::observability::KeyModelRow>, AdminError> {
|
||||
self.store
|
||||
.client_key_models(range, id)
|
||||
.await
|
||||
.map_err(|error| map_store_error(error, "client key statistics"))
|
||||
}
|
||||
|
||||
async fn usage_summary(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
|
||||
@@ -226,6 +226,23 @@ impl ObservabilityStore for CachedObservabilityStore {
|
||||
) -> AdminStoreResult<Vec<RequestMetricPoint>> {
|
||||
self.inner.dashboard_trend(range, granularity).await
|
||||
}
|
||||
async fn client_key_statistics(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
query: crate::model::observability::KeyUsageMetric,
|
||||
) -> AdminStoreResult<crate::model::observability::KeyStatisticsOverview> {
|
||||
self.inner.client_key_statistics(range, id, query).await
|
||||
}
|
||||
|
||||
async fn client_key_models(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
) -> AdminStoreResult<Vec<crate::model::observability::KeyModelRow>> {
|
||||
self.inner.client_key_models(range, id).await
|
||||
}
|
||||
|
||||
async fn usage_trend(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
|
||||
@@ -53,6 +53,8 @@ impl PluginResourceAccess for DefaultPluginResourceAccess {
|
||||
resource_key,
|
||||
NewAccountGroup {
|
||||
allow_image_generation: command.allow_image_generation,
|
||||
allow_subagents: command.allow_subagents,
|
||||
model_access: command.model_access,
|
||||
id,
|
||||
name: command.name,
|
||||
description: command.description,
|
||||
|
||||
@@ -116,10 +116,10 @@ fn validate_settings(command: &ReplaceRuntimeSettings) -> Result<(), AdminError>
|
||||
.map(|(requested, upstream)| (requested.as_str(), upstream.as_str())),
|
||||
)
|
||||
.map_err(|_| AdminError::invalid("运行时设置不满足约束"))?;
|
||||
command
|
||||
.values
|
||||
.validate()
|
||||
.map_err(|_| AdminError::invalid("运行时设置不满足约束"))
|
||||
command.values.validate().map_err(|field| match field {
|
||||
"openai_request_country" => AdminError::invalid("请求国家必须是两位大写 ASCII 字母"),
|
||||
_ => AdminError::invalid("运行时设置不满足约束"),
|
||||
})
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
@@ -267,7 +267,7 @@ impl SettingsService for DefaultSettingsService {
|
||||
)
|
||||
})
|
||||
{
|
||||
return Err(AdminError::invalid("xAI 不支持此价格档位"));
|
||||
return Err(AdminError::invalid("当前平台不支持此价格档位"));
|
||||
}
|
||||
if !pricing.bands.contains_key("standard")
|
||||
&& command
|
||||
|
||||
@@ -335,3 +335,42 @@ fn pending_mutation_v1_should_keep_existing_storage_fields_and_reject_invalid_do
|
||||
document["schema_version"] = json!(2);
|
||||
assert!(PendingAuthorizationMutation::from_storage_v1(document).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn manual_key_alignment_only_accepts_the_current_account_wide_day_or_week() {
|
||||
use chrono::{Duration, Utc};
|
||||
use gateway_admin::model::{
|
||||
client_keys::{ClientKeyBudgetReference, ClientKeyBudgetWindowPeriod},
|
||||
upstream_info::UpstreamQuotaWindow,
|
||||
};
|
||||
let now = Utc::now();
|
||||
for (seconds, period) in [
|
||||
(86_400, ClientKeyBudgetWindowPeriod::Daily),
|
||||
(604_800, ClientKeyBudgetWindowPeriod::Weekly),
|
||||
] {
|
||||
let mut window = usage_window("account", "quota", seconds);
|
||||
let end = now + Duration::hours(2);
|
||||
window.reset_at = Some(end);
|
||||
let reference = ClientKeyBudgetReference::from_current_window(window.clone(), now).unwrap();
|
||||
let selected = reference.period;
|
||||
let bounds = reference.bounds;
|
||||
assert_eq!(selected, period);
|
||||
assert_eq!(bounds.starts_at, end - Duration::seconds(seconds as i64));
|
||||
assert_eq!(bounds.ends_at, end);
|
||||
assert!(bounds.resume_calendar);
|
||||
assert!(ClientKeyBudgetReference::from_current_window(window.clone(), end).is_none());
|
||||
window.upstream = Some(UpstreamQuotaWindow {
|
||||
account_id: None,
|
||||
observed_at: Some(now),
|
||||
stale: true,
|
||||
amount: None,
|
||||
});
|
||||
assert!(ClientKeyBudgetReference::from_current_window(window.clone(), now).is_none());
|
||||
window.upstream = None;
|
||||
window.local_usage_attribution = QuotaLocalUsageAttribution::Unavailable;
|
||||
assert!(ClientKeyBudgetReference::from_current_window(window.clone(), now).is_none());
|
||||
}
|
||||
let mut short = usage_window("primary", "short", 18_000);
|
||||
short.reset_at = Some(now + Duration::hours(1));
|
||||
assert!(ClientKeyBudgetReference::from_current_window(short, now).is_none());
|
||||
}
|
||||
|
||||
@@ -344,6 +344,8 @@ fn group_record() -> AccountGroupRecord {
|
||||
let now = Utc::now();
|
||||
AccountGroupRecord {
|
||||
allow_image_generation: true,
|
||||
allow_subagents: true,
|
||||
model_access: Default::default(),
|
||||
fast_mode: FastMode::Default,
|
||||
id: group_id(),
|
||||
name: "Primary".to_owned(),
|
||||
@@ -420,6 +422,8 @@ async fn invalid_group_fields_are_rejected_before_store_mutations() {
|
||||
&context,
|
||||
CreateAccountGroup {
|
||||
allow_image_generation: true,
|
||||
allow_subagents: true,
|
||||
model_access: Default::default(),
|
||||
fast_mode: FastMode::Default,
|
||||
name: " invalid".to_owned(),
|
||||
description: None,
|
||||
@@ -434,6 +438,8 @@ async fn invalid_group_fields_are_rejected_before_store_mutations() {
|
||||
&context,
|
||||
UpdateAccountGroup {
|
||||
allow_image_generation: None,
|
||||
allow_subagents: None,
|
||||
model_access: None,
|
||||
fast_mode: None,
|
||||
id: record.id,
|
||||
name: "valid".to_owned(),
|
||||
|
||||
@@ -266,6 +266,7 @@ impl FakeProviderAdmin {
|
||||
plan_type: account.plan_type.clone(),
|
||||
preserve_profile: false,
|
||||
preserve_credential_state: false,
|
||||
preserve_access_token_expiry: false,
|
||||
provider_material: document(),
|
||||
has_refresh_token: account.has_refresh_token,
|
||||
access_token_expires_at: account
|
||||
@@ -1173,6 +1174,8 @@ impl SettingsStore for StaticSettingsStore {
|
||||
rotation_strategy: RotationStrategy::Smart,
|
||||
updated_at: Utc::now(),
|
||||
values: gateway_admin::model::settings::RuntimeSettingsValues {
|
||||
openai_oauth_guardian_review_model: false,
|
||||
openai_request_country: Default::default(),
|
||||
codex_privacy_policy: Default::default(),
|
||||
request_location_enabled: false,
|
||||
request_location: Default::default(),
|
||||
@@ -3513,6 +3516,7 @@ fn plugin_rotation_facts(account: &AccountRecord) -> PreparedCredentialRotationF
|
||||
plan_type: account.plan_type.clone(),
|
||||
preserve_profile: true,
|
||||
preserve_credential_state: false,
|
||||
preserve_access_token_expiry: false,
|
||||
provider_material: document(),
|
||||
has_refresh_token: account.has_refresh_token,
|
||||
access_token_expires_at: account.access_token_expires_at,
|
||||
|
||||
@@ -35,6 +35,26 @@ struct TestClientKeyStore {
|
||||
|
||||
#[async_trait]
|
||||
impl ClientKeyStore for TestClientKeyStore {
|
||||
async fn budget_reference_accounts(
|
||||
&self,
|
||||
_: &gateway_core::policy::ClientApiKeyId,
|
||||
_: &str,
|
||||
_: &str,
|
||||
) -> gateway_admin::ports::store::AdminStoreResult<
|
||||
gateway_admin::model::client_keys::BudgetReferenceAccountPage,
|
||||
> {
|
||||
Err(unused())
|
||||
}
|
||||
|
||||
async fn batch_update_client_keys(
|
||||
&self,
|
||||
_: gateway_admin::model::client_keys::BatchUpdateClientKeys,
|
||||
_: gateway_admin::ports::store::BatchClientKeyProfileMerger,
|
||||
_: &MutationContext,
|
||||
) -> AdminStoreResult<Revision> {
|
||||
Err(unused())
|
||||
}
|
||||
|
||||
async fn client_key_budget_window(
|
||||
&self,
|
||||
_: &gateway_core::policy::ClientApiKeyId,
|
||||
@@ -80,6 +100,15 @@ impl ClientKeyStore for TestClientKeyStore {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn client_key_budget(
|
||||
&self,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
) -> AdminStoreResult<Option<gateway_core::engine::budget::ClientBudgetStatus>> {
|
||||
self.get_client_key(id)
|
||||
.await
|
||||
.map(|key| key.map(|row| row.budget))
|
||||
}
|
||||
|
||||
async fn get_client_key(
|
||||
&self,
|
||||
_: &ClientApiKeyId,
|
||||
@@ -172,6 +201,34 @@ impl ClientKeyStore for TestClientKeyStore {
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn batch_keys_require_a_change_and_unique_targets() {
|
||||
use gateway_admin::model::client_keys::BatchUpdateClientKeys;
|
||||
let services = super::AdminHarness::new()
|
||||
.client_keys(Arc::new(TestClientKeyStore::default()))
|
||||
.build()
|
||||
.await;
|
||||
let id = ClientApiKeyId::new("key_a").unwrap();
|
||||
for patch in [
|
||||
BatchUpdateClientKeys {
|
||||
ids: vec![id.clone()],
|
||||
..Default::default()
|
||||
},
|
||||
BatchUpdateClientKeys {
|
||||
ids: vec![id.clone(), id],
|
||||
enabled: Some(false),
|
||||
..Default::default()
|
||||
},
|
||||
] {
|
||||
let error = services
|
||||
.client_keys()
|
||||
.batch_update(&mutation_context(), patch)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert_eq!(error.kind(), AdminErrorKind::Invalid);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn reset_budget_forwards_scope_and_returns_only_key_identity() {
|
||||
use gateway_admin::model::client_keys::{ClientKeyBudgetPeriod, ResetClientKeyBudget};
|
||||
|
||||
@@ -107,8 +107,7 @@ async fn openai_import_should_expose_only_explicit_public_errors_without_committ
|
||||
] {
|
||||
let events = events();
|
||||
let provider = FakeProviderAdmin::new("openai", events.clone());
|
||||
provider
|
||||
.fail_next_with_public_message(provider_kind, "Codex PAT 验证失败,请检查令牌后重试");
|
||||
provider.fail_next_with_public_message(provider_kind, "PAT 验证失败,请检查令牌");
|
||||
let store = FakeAccountStore::new("openai", events.clone());
|
||||
let services = service(provider, store.clone()).await;
|
||||
let error = services
|
||||
@@ -124,7 +123,7 @@ async fn openai_import_should_expose_only_explicit_public_errors_without_committ
|
||||
.await
|
||||
.expect_err("validation must fail before committing accounts");
|
||||
assert_eq!(error.kind(), admin_kind);
|
||||
assert_eq!(error.to_string(), "Codex PAT 验证失败,请检查令牌后重试");
|
||||
assert_eq!(error.to_string(), "PAT 验证失败,请检查令牌");
|
||||
assert!(!format!("{error:?}").contains("secret token"));
|
||||
assert_eq!(recorded(&events), ["provider.prepare_import"]);
|
||||
assert!(store.audit_requests().is_empty());
|
||||
|
||||
@@ -33,6 +33,8 @@ fn runtime_settings(enabled: bool, probe_enabled: bool) -> RuntimeSettings {
|
||||
rotation_strategy: gateway_admin::model::settings::RotationStrategy::Smart,
|
||||
updated_at: Utc::now(),
|
||||
values: gateway_admin::model::settings::RuntimeSettingsValues {
|
||||
openai_oauth_guardian_review_model: false,
|
||||
openai_request_country: Default::default(),
|
||||
codex_privacy_policy: Default::default(),
|
||||
request_location_enabled: false,
|
||||
request_location: Default::default(),
|
||||
|
||||
@@ -719,6 +719,26 @@ impl AccountRuntimeStore for UnavailableStore {
|
||||
|
||||
#[async_trait]
|
||||
impl ClientKeyStore for UnavailableStore {
|
||||
async fn budget_reference_accounts(
|
||||
&self,
|
||||
_: &gateway_core::policy::ClientApiKeyId,
|
||||
_: &str,
|
||||
_: &str,
|
||||
) -> gateway_admin::ports::store::AdminStoreResult<
|
||||
gateway_admin::model::client_keys::BudgetReferenceAccountPage,
|
||||
> {
|
||||
Err(unavailable("reference accounts"))
|
||||
}
|
||||
|
||||
async fn batch_update_client_keys(
|
||||
&self,
|
||||
_: gateway_admin::model::client_keys::BatchUpdateClientKeys,
|
||||
_: gateway_admin::ports::store::BatchClientKeyProfileMerger,
|
||||
_: &MutationContext,
|
||||
) -> AdminStoreResult<Revision> {
|
||||
Err(unavailable("client key batch update"))
|
||||
}
|
||||
|
||||
async fn client_key_budget_window(
|
||||
&self,
|
||||
_: &gateway_core::policy::ClientApiKeyId,
|
||||
@@ -763,6 +783,15 @@ impl ClientKeyStore for UnavailableStore {
|
||||
Err(unavailable("client key budget reset"))
|
||||
}
|
||||
|
||||
async fn client_key_budget(
|
||||
&self,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
) -> AdminStoreResult<Option<gateway_core::engine::budget::ClientBudgetStatus>> {
|
||||
self.get_client_key(id)
|
||||
.await
|
||||
.map(|key| key.map(|row| row.budget))
|
||||
}
|
||||
|
||||
async fn get_client_key(
|
||||
&self,
|
||||
_: &ClientApiKeyId,
|
||||
@@ -844,6 +873,23 @@ impl ObservabilityStore for UnavailableStore {
|
||||
Err(unavailable("dashboard trend"))
|
||||
}
|
||||
|
||||
async fn client_key_statistics(
|
||||
&self,
|
||||
_: TimeRange,
|
||||
_: &gateway_core::policy::ClientApiKeyId,
|
||||
_: gateway_admin::model::observability::KeyUsageMetric,
|
||||
) -> AdminStoreResult<gateway_admin::model::observability::KeyStatisticsOverview> {
|
||||
Ok(Default::default())
|
||||
}
|
||||
|
||||
async fn client_key_models(
|
||||
&self,
|
||||
_: TimeRange,
|
||||
_: &gateway_core::policy::ClientApiKeyId,
|
||||
) -> AdminStoreResult<Vec<gateway_admin::model::observability::KeyModelRow>> {
|
||||
Ok(Default::default())
|
||||
}
|
||||
|
||||
async fn usage_trend(
|
||||
&self,
|
||||
_: TimeRange,
|
||||
|
||||
@@ -1266,6 +1266,23 @@ impl ObservabilityStore for FixtureObservabilityStore {
|
||||
Ok(self.trend.lock().expect("trend").clone())
|
||||
}
|
||||
|
||||
async fn client_key_statistics(
|
||||
&self,
|
||||
_: TimeRange,
|
||||
_: &gateway_core::policy::ClientApiKeyId,
|
||||
_: gateway_admin::model::observability::KeyUsageMetric,
|
||||
) -> AdminStoreResult<gateway_admin::model::observability::KeyStatisticsOverview> {
|
||||
Ok(Default::default())
|
||||
}
|
||||
|
||||
async fn client_key_models(
|
||||
&self,
|
||||
_: TimeRange,
|
||||
_: &gateway_core::policy::ClientApiKeyId,
|
||||
) -> AdminStoreResult<Vec<gateway_admin::model::observability::KeyModelRow>> {
|
||||
Ok(Default::default())
|
||||
}
|
||||
|
||||
async fn usage_trend(
|
||||
&self,
|
||||
_: TimeRange,
|
||||
@@ -1414,6 +1431,8 @@ impl SettingsStore for FixtureSettingsStore {
|
||||
rotation_strategy: RotationStrategy::Smart,
|
||||
updated_at: Utc::now(),
|
||||
values: gateway_admin::model::settings::RuntimeSettingsValues {
|
||||
openai_oauth_guardian_review_model: false,
|
||||
openai_request_country: Default::default(),
|
||||
codex_privacy_policy: Default::default(),
|
||||
request_location_enabled: false,
|
||||
request_location: Default::default(),
|
||||
|
||||
@@ -254,7 +254,7 @@ fn sdk_settings_contract_matches_host_declarations() {
|
||||
),
|
||||
(
|
||||
"../gateway-core/src/account/location.rs",
|
||||
&["RequestLocation"][..],
|
||||
&["RequestLocation", "OpenAiRequestCountry"][..],
|
||||
),
|
||||
(
|
||||
"../gateway-core/src/account/scheduling.rs",
|
||||
|
||||
@@ -60,7 +60,7 @@ async fn explicit_service_calls_freeze_empty_plan_and_cancel_before_entering_ter
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
// 内部 pricing 不再单独进入插件链;显式调用只解析一次发布集合
|
||||
// 每次显式调价只解析一次插件发布集合
|
||||
assert_eq!(resolutions.load(Ordering::SeqCst), expected);
|
||||
}
|
||||
store.updated.lock().unwrap().take();
|
||||
@@ -325,6 +325,8 @@ async fn settings_should_reject_zero_refresh_margin_before_store_call() {
|
||||
model_mappings: Default::default(),
|
||||
rotation_strategy: RotationStrategy::Smart,
|
||||
values: gateway_admin::model::settings::RuntimeSettingsValues {
|
||||
openai_oauth_guardian_review_model: true,
|
||||
openai_request_country: Default::default(),
|
||||
codex_privacy_policy: Default::default(),
|
||||
request_location_enabled: false,
|
||||
request_location: Default::default(),
|
||||
|
||||
@@ -21,12 +21,15 @@ use gateway_admin::model::{
|
||||
UpdateAccountGroup,
|
||||
},
|
||||
};
|
||||
use gateway_core::{account::FastMode, routing::AccountGroupId};
|
||||
use gateway_core::{
|
||||
account::{AccountModelAccess, FastMode},
|
||||
routing::AccountGroupId,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::{
|
||||
AdminAuth, AdminEnvelope, AdminError, AdminJson, AdminQuery, AdminResponse, PageMeta,
|
||||
WireValidationError, wire::map_admin_service_error,
|
||||
WireValidationError, accounts::AccountModelView, wire::map_admin_service_error,
|
||||
};
|
||||
|
||||
/// 账号、代理与 Client Key 共用的分组引用
|
||||
@@ -51,7 +54,6 @@ impl From<gateway_admin::model::account_groups::AccountGroupRef> for AccountGrou
|
||||
}
|
||||
|
||||
const DEFAULT_PAGE_SIZE: u32 = 50;
|
||||
const MAX_PAGE_SIZE: u32 = 200;
|
||||
|
||||
#[derive(Debug, Clone, Default, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
@@ -66,7 +68,7 @@ impl ListAccountGroupsQuery {
|
||||
fn into_command(self) -> Result<AccountGroupListQuery, WireValidationError> {
|
||||
let page = self.page.unwrap_or(1);
|
||||
let page_size = self.page_size.unwrap_or(DEFAULT_PAGE_SIZE);
|
||||
if page == 0 || page_size == 0 || page_size > MAX_PAGE_SIZE {
|
||||
if page == 0 {
|
||||
return Err(WireValidationError::new("page"));
|
||||
}
|
||||
let search = self
|
||||
@@ -91,15 +93,19 @@ impl ListAccountGroupsQuery {
|
||||
}
|
||||
}
|
||||
|
||||
const fn default_allow_image_generation() -> bool {
|
||||
const fn default_allowed() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
struct CreateAccountGroupRequest {
|
||||
#[serde(default = "default_allow_image_generation")]
|
||||
#[serde(default = "default_allowed")]
|
||||
allow_image_generation: bool,
|
||||
#[serde(default = "default_allowed")]
|
||||
allow_subagents: bool,
|
||||
#[serde(default)]
|
||||
model_access: AccountModelAccess,
|
||||
#[serde(default)]
|
||||
fast_mode: FastMode,
|
||||
name: String,
|
||||
@@ -111,6 +117,8 @@ struct CreateAccountGroupRequest {
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
struct UpdateAccountGroupRequest {
|
||||
allow_image_generation: Option<bool>,
|
||||
allow_subagents: Option<bool>,
|
||||
model_access: Option<AccountModelAccess>,
|
||||
fast_mode: Option<FastMode>,
|
||||
id: String,
|
||||
name: String,
|
||||
@@ -128,6 +136,8 @@ struct AccountGroupIdRequest {
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct AccountGroupView {
|
||||
allow_image_generation: bool,
|
||||
allow_subagents: bool,
|
||||
model_access: AccountModelAccess,
|
||||
fast_mode: FastMode,
|
||||
id: String,
|
||||
name: String,
|
||||
@@ -176,6 +186,8 @@ impl From<(AccountGroupRecord, crate::time::TimePresenter)> for AccountGroupView
|
||||
description: record.description,
|
||||
fast_mode: record.fast_mode,
|
||||
allow_image_generation: record.allow_image_generation,
|
||||
allow_subagents: record.allow_subagents,
|
||||
model_access: record.model_access,
|
||||
color: record.color.as_str().to_owned(),
|
||||
enabled: record.enabled,
|
||||
member_count: record.member_count,
|
||||
@@ -305,6 +317,7 @@ where
|
||||
{
|
||||
Router::new()
|
||||
.route("/api/admin/account-groups", get(list::<S>))
|
||||
.route("/api/admin/account-groups/models", get(models::<S>))
|
||||
.route("/api/admin/account-groups/options", get(list_options::<S>))
|
||||
.route("/api/admin/account-groups/create", post(create::<S>))
|
||||
.route("/api/admin/account-groups/update", post(update::<S>))
|
||||
@@ -333,6 +346,32 @@ where
|
||||
))
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct AccountGroupModelsData {
|
||||
models: Vec<AccountModelView>,
|
||||
}
|
||||
|
||||
async fn models<S>(
|
||||
_auth: AdminAuth,
|
||||
State(state): State<S>,
|
||||
AdminQuery(query): AdminQuery<AccountGroupIdRequest>,
|
||||
) -> Result<impl IntoResponse, AdminError>
|
||||
where
|
||||
S: SessionState + Send + Sync,
|
||||
{
|
||||
let models = state
|
||||
.admin_services()
|
||||
.account_groups()
|
||||
.models(&group_id(query.id)?)
|
||||
.map_err(map_service_error)?;
|
||||
Ok(AdminResponse::new(
|
||||
StatusCode::OK,
|
||||
AdminEnvelope::ok(AccountGroupModelsData {
|
||||
models: models.into_iter().map(AccountModelView::from).collect(),
|
||||
}),
|
||||
))
|
||||
}
|
||||
|
||||
async fn list<S>(
|
||||
_auth: AdminAuth,
|
||||
State(state): State<S>,
|
||||
@@ -372,6 +411,8 @@ where
|
||||
&auth.context().mutation_context(),
|
||||
CreateAccountGroup {
|
||||
allow_image_generation: request.allow_image_generation,
|
||||
allow_subagents: request.allow_subagents,
|
||||
model_access: request.model_access,
|
||||
name: request.name,
|
||||
description: request.description,
|
||||
fast_mode: request.fast_mode,
|
||||
@@ -401,6 +442,8 @@ where
|
||||
&auth.context().mutation_context(),
|
||||
UpdateAccountGroup {
|
||||
allow_image_generation: request.allow_image_generation,
|
||||
allow_subagents: request.allow_subagents,
|
||||
model_access: request.model_access,
|
||||
id: group_id(request.id)?,
|
||||
name: request.name,
|
||||
description: request.description,
|
||||
@@ -507,11 +550,16 @@ fn group_color(value: &str) -> Result<AccountGroupColor, AdminError> {
|
||||
|
||||
fn validate_group_fields(name: &str, description: Option<&str>) -> Result<(), AdminError> {
|
||||
gateway_admin::model::account_groups::validate_group_fields(name, description)
|
||||
.map_err(|_| AdminError::bad_request("账号组请求不合法"))
|
||||
.map_err(map_admin_service_error)
|
||||
}
|
||||
|
||||
fn map_wire_error(_: WireValidationError) -> AdminError {
|
||||
AdminError::bad_request("账号组查询参数不合法")
|
||||
fn map_wire_error(error: WireValidationError) -> AdminError {
|
||||
match error.field() {
|
||||
"page" => AdminError::bad_request("页码须大于 0"),
|
||||
"pageSize" => AdminError::bad_request(format!("每页数量须为 1 至 {}", PageSize::MAX)),
|
||||
"search" => AdminError::bad_request("搜索内容最多 256 字节,不能包含控制字符"),
|
||||
field => AdminError::bad_request(format!("{field} 字段不合法")),
|
||||
}
|
||||
}
|
||||
|
||||
fn map_service_error(error: gateway_admin::model::AdminError) -> AdminError {
|
||||
|
||||
@@ -118,7 +118,6 @@ pub struct StartAccountAuthorizationRequest {
|
||||
pub provider: String,
|
||||
pub name: String,
|
||||
pub account_id: Option<String>,
|
||||
pub outbound_proxy_url: Option<super::wire::AccountProxyUpdate>,
|
||||
}
|
||||
|
||||
impl StartAccountAuthorizationRequest {
|
||||
@@ -148,10 +147,7 @@ impl StartAccountAuthorizationRequest {
|
||||
context,
|
||||
name: self.name.trim().to_owned(),
|
||||
reauthorization,
|
||||
outbound_proxy: super::wire::proxy_selection(
|
||||
self.outbound_proxy_id,
|
||||
self.outbound_proxy_url,
|
||||
)?,
|
||||
outbound_proxy: super::wire::proxy_selection(self.outbound_proxy_id)?,
|
||||
},
|
||||
))
|
||||
}
|
||||
@@ -219,7 +215,6 @@ fn validate_authorization_flow(
|
||||
pub struct UpdateAccountRequest {
|
||||
pub connection: Option<AccountConnectionUpdateRequest>,
|
||||
pub outbound_proxy_id: Option<String>,
|
||||
pub outbound_proxy_url: Option<super::wire::AccountProxyUpdate>,
|
||||
pub account_id: String,
|
||||
pub name: Option<String>,
|
||||
pub notes: Option<String>,
|
||||
@@ -255,10 +250,7 @@ impl UpdateAccountRequest {
|
||||
.connection
|
||||
.map(AccountConnectionUpdateRequest::into_document);
|
||||
let settings = UpdateAccount {
|
||||
outbound_proxy: super::wire::proxy_selection(
|
||||
self.outbound_proxy_id,
|
||||
self.outbound_proxy_url,
|
||||
)?,
|
||||
outbound_proxy: super::wire::proxy_selection(self.outbound_proxy_id)?,
|
||||
account_id: self.account_id,
|
||||
name: self.name.map(|name| name.trim().to_owned()),
|
||||
notes: self.notes,
|
||||
|
||||
@@ -53,13 +53,7 @@ pub(super) fn account_models_data(
|
||||
models: result
|
||||
.models
|
||||
.into_iter()
|
||||
.map(|model| {
|
||||
let id = model.id.to_string();
|
||||
AccountModelView {
|
||||
label: id.clone(),
|
||||
id,
|
||||
}
|
||||
})
|
||||
.map(|model| AccountModelView::from(model.id))
|
||||
.collect(),
|
||||
observed_at: observed_at.map(|value| time.rfc3339(&value)),
|
||||
observed_at_display: observed_at
|
||||
|
||||
@@ -2,46 +2,22 @@
|
||||
|
||||
use super::*;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AccountProxyUpdate(pub Option<gateway_core::account::OutboundProxy>);
|
||||
|
||||
impl<'de> Deserialize<'de> for AccountProxyUpdate {
|
||||
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
let value = String::deserialize(deserializer)?;
|
||||
if value.is_empty() {
|
||||
return Ok(Self(None));
|
||||
}
|
||||
gateway_core::account::OutboundProxy::parse(&value)
|
||||
.map(|proxy| Self(Some(proxy)))
|
||||
.map_err(serde::de::Error::custom)
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn proxy_selection(
|
||||
id: Option<String>,
|
||||
url: Option<AccountProxyUpdate>,
|
||||
) -> Result<Option<gateway_admin::model::proxies::AccountProxySelection>, WireValidationError> {
|
||||
use gateway_admin::model::proxies::AccountProxySelection;
|
||||
match (id, url) {
|
||||
(Some(_), Some(_)) => Err(WireValidationError::new("outboundProxyId")),
|
||||
(Some(id), None) if id.is_empty() => Ok(Some(AccountProxySelection::Direct)),
|
||||
(Some(id), None) => {
|
||||
if id.len() > 128
|
||||
|| !id
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-'))
|
||||
{
|
||||
return Err(WireValidationError::new("outboundProxyId"));
|
||||
}
|
||||
Ok(Some(AccountProxySelection::Saved(id)))
|
||||
}
|
||||
(None, Some(value)) => Ok(Some(
|
||||
value
|
||||
.0
|
||||
.map_or(AccountProxySelection::Direct, AccountProxySelection::Url),
|
||||
)),
|
||||
(None, None) => Ok(None),
|
||||
let Some(id) = id else { return Ok(None) };
|
||||
if id.is_empty() {
|
||||
return Ok(Some(AccountProxySelection::Direct));
|
||||
}
|
||||
if id.len() > 128
|
||||
|| !id
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-'))
|
||||
{
|
||||
return Err(WireValidationError::new("outboundProxyId"));
|
||||
}
|
||||
Ok(Some(AccountProxySelection::Saved(id)))
|
||||
}
|
||||
|
||||
/// 账号列表查询参数
|
||||
@@ -64,7 +40,6 @@ pub struct BatchUpdateAccountsRequest {
|
||||
pub quota_settings: Option<gateway_core::account::AccountQuotaSettings>,
|
||||
pub auto_use_quota_credits: Option<bool>,
|
||||
pub outbound_proxy_id: Option<String>,
|
||||
pub outbound_proxy_url: Option<AccountProxyUpdate>,
|
||||
pub account_ids: Vec<String>,
|
||||
pub enabled: Option<bool>,
|
||||
#[serde(default, deserialize_with = "deserialize_optional_nullable_limit")]
|
||||
@@ -127,7 +102,6 @@ impl BatchUpdateAccountsRequest {
|
||||
&& self.group_ids.is_none()
|
||||
&& self.model_access.is_none()
|
||||
&& self.outbound_proxy_id.is_none()
|
||||
&& self.outbound_proxy_url.is_none()
|
||||
{
|
||||
return Err(WireValidationError::new("accountSettings"));
|
||||
}
|
||||
@@ -139,7 +113,7 @@ impl BatchUpdateAccountsRequest {
|
||||
Ok(BatchUpdateAccounts {
|
||||
quota_settings: self.quota_settings,
|
||||
auto_use_quota_credits: self.auto_use_quota_credits,
|
||||
outbound_proxy: proxy_selection(self.outbound_proxy_id, self.outbound_proxy_url)?,
|
||||
outbound_proxy: proxy_selection(self.outbound_proxy_id)?,
|
||||
account_ids: self.account_ids,
|
||||
enabled: self.enabled,
|
||||
concurrency_limit: self
|
||||
@@ -743,6 +717,16 @@ pub struct AccountModelView {
|
||||
pub label: String,
|
||||
}
|
||||
|
||||
impl From<UpstreamModelId> for AccountModelView {
|
||||
fn from(model: UpstreamModelId) -> Self {
|
||||
let id = model.to_string();
|
||||
Self {
|
||||
label: id.clone(),
|
||||
id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AccountModelsData {
|
||||
|
||||
@@ -0,0 +1,291 @@
|
||||
//! 管理员查看单个密钥的模型统计与手动对齐本期额度的接口
|
||||
|
||||
use axum::{Router, extract::State, http::StatusCode, response::IntoResponse, routing::get};
|
||||
use chrono::{DateTime, Utc};
|
||||
use gateway_admin::model::observability::{KeyStatisticsOverview, KeyUsageMetric, TimeRange};
|
||||
use gateway_admin::{
|
||||
AdminServices,
|
||||
model::client_keys::{
|
||||
ClientKeyBudgetMutationOrigin, ClientKeyBudgetReference, ClientKeyBudgetWindowPeriod,
|
||||
SetClientKeyBudgetWindow,
|
||||
},
|
||||
};
|
||||
use gateway_core::{account::ProviderAccountId, policy::ClientApiKeyId};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::{
|
||||
AdminAuth, AdminEnvelope, AdminError, AdminJson, AdminQuery, AdminResponse,
|
||||
client_keys::ClientBudgetView, wire::map_admin_service_error,
|
||||
};
|
||||
use crate::{auth::SessionState, time::TimePresenter};
|
||||
|
||||
pub(super) fn router<S: SessionState + Clone + Send + Sync + 'static>() -> Router<S> {
|
||||
Router::new()
|
||||
.route(
|
||||
"/api/admin/client-keys/budget-reference-accounts",
|
||||
get(accounts::<S>),
|
||||
)
|
||||
.route("/api/admin/client-keys/statistics", get(statistics::<S>))
|
||||
.route("/api/admin/client-keys/statistics/models", get(models::<S>))
|
||||
.route(
|
||||
"/api/admin/client-keys/budget-alignment",
|
||||
get(preview::<S>).post(align::<S>),
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
struct StatisticsQuery {
|
||||
id: String,
|
||||
period: Option<String>,
|
||||
#[serde(default)]
|
||||
metric: KeyUsageMetric,
|
||||
as_of: Option<i64>,
|
||||
}
|
||||
|
||||
fn key_id(value: String) -> Result<ClientApiKeyId, AdminError> {
|
||||
ClientApiKeyId::new(value).map_err(|_| AdminError::bad_request("API Key ID 无效"))
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Statistics {
|
||||
budget: ClientBudgetView,
|
||||
dates: Vec<chrono::NaiveDate>,
|
||||
#[serde(rename = "asOf")]
|
||||
as_of: i64,
|
||||
#[serde(flatten)]
|
||||
overview: KeyStatisticsOverview,
|
||||
}
|
||||
|
||||
async fn statistics<S: SessionState>(
|
||||
_auth: AdminAuth,
|
||||
State(state): State<S>,
|
||||
AdminQuery(query): AdminQuery<StatisticsQuery>,
|
||||
) -> Result<impl IntoResponse, AdminError> {
|
||||
let services = state.admin_services();
|
||||
let id = key_id(query.id.clone())?;
|
||||
let tz = services.timezone();
|
||||
let range = statistics_range(&query, tz)?;
|
||||
let budget = services
|
||||
.client_keys()
|
||||
.budget(&id)
|
||||
.await
|
||||
.map_err(map_admin_service_error)?;
|
||||
let overview = services
|
||||
.observability()
|
||||
.client_key_statistics(range, &id, query.metric)
|
||||
.await
|
||||
.map_err(map_admin_service_error)?;
|
||||
let end = tz.local(range.end).date_naive();
|
||||
let dates = tz
|
||||
.local(range.start)
|
||||
.date_naive()
|
||||
.iter_days()
|
||||
.take_while(|date| *date <= end)
|
||||
.collect();
|
||||
Ok(AdminResponse::new(
|
||||
StatusCode::OK,
|
||||
AdminEnvelope::ok(Statistics {
|
||||
budget: ClientBudgetView::from((budget, TimePresenter::new(tz))),
|
||||
dates,
|
||||
as_of: range.end.timestamp_millis(),
|
||||
overview,
|
||||
}),
|
||||
))
|
||||
}
|
||||
|
||||
fn statistics_range(
|
||||
query: &StatisticsQuery,
|
||||
tz: gateway_core::time::DeploymentTimeZone,
|
||||
) -> Result<TimeRange, AdminError> {
|
||||
let period = query.period.as_deref().unwrap_or("7d");
|
||||
if !matches!(period, "7d" | "30d") {
|
||||
return Err(AdminError::bad_request("统计周期须为 7 天或 30 天"));
|
||||
}
|
||||
crate::time::query_range(
|
||||
None,
|
||||
None,
|
||||
Some(period),
|
||||
Some(query.as_of.unwrap_or_else(|| Utc::now().timestamp_millis())),
|
||||
tz,
|
||||
"7d",
|
||||
)
|
||||
.map_err(|_| AdminError::bad_request("统计时间范围无效"))
|
||||
}
|
||||
|
||||
async fn models<S: SessionState>(
|
||||
_auth: AdminAuth,
|
||||
State(state): State<S>,
|
||||
AdminQuery(query): AdminQuery<StatisticsQuery>,
|
||||
) -> Result<impl IntoResponse, AdminError> {
|
||||
let services = state.admin_services();
|
||||
let range = statistics_range(&query, services.timezone())?;
|
||||
let id = key_id(query.id)?;
|
||||
services
|
||||
.client_keys()
|
||||
.budget(&id)
|
||||
.await
|
||||
.map_err(map_admin_service_error)?;
|
||||
let items = services
|
||||
.observability()
|
||||
.client_key_models(range, &id)
|
||||
.await
|
||||
.map_err(map_admin_service_error)?;
|
||||
Ok(AdminResponse::new(StatusCode::OK, AdminEnvelope::ok(items)))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct AccountSearch {
|
||||
id: String,
|
||||
#[serde(default)]
|
||||
search: String,
|
||||
#[serde(default)]
|
||||
cursor: String,
|
||||
}
|
||||
|
||||
async fn accounts<S: SessionState>(
|
||||
_auth: AdminAuth,
|
||||
State(state): State<S>,
|
||||
AdminQuery(query): AdminQuery<AccountSearch>,
|
||||
) -> Result<impl IntoResponse, AdminError> {
|
||||
let page = state
|
||||
.admin_services()
|
||||
.client_keys()
|
||||
.budget_reference_accounts(&key_id(query.id)?, &query.search, &query.cursor)
|
||||
.await
|
||||
.map_err(map_admin_service_error)?;
|
||||
Ok(AdminResponse::new(StatusCode::OK, AdminEnvelope::ok(page)))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
struct ReferenceQuery {
|
||||
id: String,
|
||||
account_id: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct ReferenceWindow {
|
||||
key: String,
|
||||
label: String,
|
||||
period: &'static str,
|
||||
starts_at: DateTime<Utc>,
|
||||
ends_at: DateTime<Utc>,
|
||||
starts_at_display: String,
|
||||
ends_at_display: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Preview {
|
||||
revision: u64,
|
||||
windows: Vec<ReferenceWindow>,
|
||||
}
|
||||
|
||||
async fn reference_windows(
|
||||
services: &AdminServices,
|
||||
id: &ClientApiKeyId,
|
||||
account: String,
|
||||
) -> Result<Vec<ClientKeyBudgetReference>, AdminError> {
|
||||
let account_id =
|
||||
ProviderAccountId::new(account).map_err(|_| AdminError::bad_request("参考账号 ID 无效"))?;
|
||||
let key = services
|
||||
.client_keys()
|
||||
.get(id)
|
||||
.await
|
||||
.map_err(map_admin_service_error)?;
|
||||
services
|
||||
.accounts()
|
||||
.key_budget_references(&account_id, &key.groups)
|
||||
.await
|
||||
.map_err(map_admin_service_error)
|
||||
}
|
||||
|
||||
async fn preview<S: SessionState>(
|
||||
_auth: AdminAuth,
|
||||
State(state): State<S>,
|
||||
AdminQuery(query): AdminQuery<ReferenceQuery>,
|
||||
) -> Result<impl IntoResponse, AdminError> {
|
||||
let services = state.admin_services();
|
||||
let id = key_id(query.id)?;
|
||||
let time = TimePresenter::new(services.timezone());
|
||||
let windows = reference_windows(services, &id, query.account_id)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(
|
||||
|ClientKeyBudgetReference {
|
||||
key,
|
||||
label,
|
||||
period,
|
||||
bounds,
|
||||
}| ReferenceWindow {
|
||||
key,
|
||||
label,
|
||||
period: period.as_str(),
|
||||
starts_at: bounds.starts_at,
|
||||
ends_at: bounds.ends_at,
|
||||
starts_at_display: time.datetime(&bounds.starts_at),
|
||||
ends_at_display: time.datetime(&bounds.ends_at),
|
||||
},
|
||||
)
|
||||
.collect();
|
||||
// 日周窗口共享配置版本,普通请求的消费累计不会让预览失效
|
||||
let revision = services
|
||||
.client_keys()
|
||||
.budget_window(&id, ClientKeyBudgetWindowPeriod::Weekly)
|
||||
.await
|
||||
.map_err(map_admin_service_error)?
|
||||
.revision;
|
||||
Ok(AdminResponse::new(
|
||||
StatusCode::OK,
|
||||
AdminEnvelope::ok(Preview { revision, windows }),
|
||||
))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
struct AlignRequest {
|
||||
id: String,
|
||||
account_id: String,
|
||||
window_key: String,
|
||||
starts_at: DateTime<Utc>,
|
||||
ends_at: DateTime<Utc>,
|
||||
revision: u64,
|
||||
}
|
||||
|
||||
async fn align<S: SessionState>(
|
||||
auth: AdminAuth,
|
||||
State(state): State<S>,
|
||||
AdminJson(input): AdminJson<AlignRequest>,
|
||||
) -> Result<impl IntoResponse, AdminError> {
|
||||
let services = state.admin_services();
|
||||
let id = key_id(input.id)?;
|
||||
let reference = reference_windows(services, &id, input.account_id)
|
||||
.await?
|
||||
.into_iter()
|
||||
.find(|reference| {
|
||||
reference.key == input.window_key
|
||||
&& reference.bounds.starts_at == input.starts_at
|
||||
&& reference.bounds.ends_at == input.ends_at
|
||||
})
|
||||
.ok_or_else(|| AdminError::conflict("参考窗口已变化,请重新预览"))?;
|
||||
let revision = services
|
||||
.client_keys()
|
||||
.set_budget_window(
|
||||
&auth.context().mutation_context(),
|
||||
SetClientKeyBudgetWindow {
|
||||
id,
|
||||
period: reference.period,
|
||||
expected_revision: input.revision,
|
||||
window: Some(reference.bounds),
|
||||
},
|
||||
ClientKeyBudgetMutationOrigin::Admin,
|
||||
)
|
||||
.await
|
||||
.map_err(map_admin_service_error)?;
|
||||
Ok(AdminResponse::new(
|
||||
StatusCode::OK,
|
||||
AdminEnvelope::ok(revision),
|
||||
))
|
||||
}
|
||||
@@ -7,11 +7,12 @@ use std::{collections::BTreeMap, fmt};
|
||||
use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};
|
||||
use chrono::{DateTime, Utc};
|
||||
use gateway_admin::model::client_keys::{
|
||||
ClientKeyBudgetPeriod, ClientKeyCursor, ClientKeyCursorValue as DomainCursorValue,
|
||||
ClientKeyListQuery, ClientKeyMutation, ClientKeyPage, ClientKeyPageSize, ClientKeyRecord,
|
||||
ClientKeySecret, ClientKeySort as DomainSort, ClientKeySortField as DomainSortField,
|
||||
CreateClientKey, CreatedClientKey, DeleteClientKey, ResetClientKeyBudget, SetClientKeyEnabled,
|
||||
SortDirection, UpdateClientKey,
|
||||
BatchUpdateClientKeys, ClientKeyBudgetPeriod, ClientKeyCursor,
|
||||
ClientKeyCursorValue as DomainCursorValue, ClientKeyListQuery, ClientKeyMutation,
|
||||
ClientKeyPage, ClientKeyPageSize, ClientKeyRecord, ClientKeySecret,
|
||||
ClientKeySort as DomainSort, ClientKeySortField as DomainSortField, CreateClientKey,
|
||||
CreatedClientKey, DeleteClientKey, ResetClientKeyBudget, SetClientKeyEnabled, SortDirection,
|
||||
UpdateClientKey,
|
||||
};
|
||||
use gateway_core::{
|
||||
engine::budget::ClientBudgetLimits,
|
||||
@@ -156,8 +157,6 @@ pub struct CreateClientKeyRequest {
|
||||
peer_sharing: gateway_core::policy::ClientPeerSharing,
|
||||
#[serde(default)]
|
||||
provider_request_profile_overrides: ProviderRequestProfileOverrides,
|
||||
openai_client_profile_override: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
xai_client_profile_override: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
custom_key: Option<String>,
|
||||
name: String,
|
||||
label: Option<String>,
|
||||
@@ -186,11 +185,8 @@ impl CreateClientKeyRequest {
|
||||
let group_ids = validate_group_ids(self.group_ids)?;
|
||||
validate_limit(self.max_concurrency, "maxConcurrency")?;
|
||||
validate_limit(self.requests_per_minute, "requestsPerMinute")?;
|
||||
let request_profile_overrides = normalize_request_profile_overrides(
|
||||
self.provider_request_profile_overrides,
|
||||
self.openai_client_profile_override,
|
||||
self.xai_client_profile_override,
|
||||
)?;
|
||||
let request_profile_overrides =
|
||||
normalize_request_profile_overrides(self.provider_request_profile_overrides)?;
|
||||
Ok(CreateClientKey {
|
||||
peer_sharing: self.peer_sharing,
|
||||
request_profile_overrides,
|
||||
@@ -223,10 +219,6 @@ pub struct UpdateClientKeyRequest {
|
||||
peer_sharing: Option<gateway_core::policy::ClientPeerSharing>,
|
||||
#[serde(default)]
|
||||
provider_request_profile_overrides: ProviderRequestProfileOverrideUpdates,
|
||||
#[serde(default, deserialize_with = "deserialize_profile_override")]
|
||||
openai_client_profile_override: Option<Option<serde_json::Map<String, serde_json::Value>>>,
|
||||
#[serde(default, deserialize_with = "deserialize_profile_override")]
|
||||
xai_client_profile_override: Option<Option<serde_json::Map<String, serde_json::Value>>>,
|
||||
id: String,
|
||||
name: String,
|
||||
label: Option<String>,
|
||||
@@ -246,11 +238,8 @@ impl UpdateClientKeyRequest {
|
||||
let group_ids = validate_group_ids(self.group_ids)?;
|
||||
validate_limit(self.max_concurrency, "maxConcurrency")?;
|
||||
validate_limit(self.requests_per_minute, "requestsPerMinute")?;
|
||||
let request_profile_override_updates = normalize_request_profile_override_updates(
|
||||
self.provider_request_profile_overrides,
|
||||
self.openai_client_profile_override,
|
||||
self.xai_client_profile_override,
|
||||
)?;
|
||||
let request_profile_override_updates =
|
||||
normalize_request_profile_override_updates(self.provider_request_profile_overrides)?;
|
||||
Ok(UpdateClientKey {
|
||||
peer_sharing: self.peer_sharing,
|
||||
request_profile_override_updates,
|
||||
@@ -335,16 +324,54 @@ impl ClientKeyMutationRequest {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(super) struct ClientBudgetView {
|
||||
daily_limit_usd: String,
|
||||
weekly_limit_usd: String,
|
||||
daily_used_usd: String,
|
||||
weekly_used_usd: String,
|
||||
daily_resets_at: Option<DateTime<Utc>>,
|
||||
daily_resets_at_display: Option<String>,
|
||||
weekly_resets_at: Option<DateTime<Utc>>,
|
||||
weekly_resets_at_display: Option<String>,
|
||||
}
|
||||
|
||||
impl
|
||||
From<(
|
||||
gateway_core::engine::budget::ClientBudgetStatus,
|
||||
crate::time::TimePresenter,
|
||||
)> for ClientBudgetView
|
||||
{
|
||||
fn from(
|
||||
(budget, time): (
|
||||
gateway_core::engine::budget::ClientBudgetStatus,
|
||||
crate::time::TimePresenter,
|
||||
),
|
||||
) -> Self {
|
||||
Self {
|
||||
daily_limit_usd: budget.limits.daily_usd.canonical(),
|
||||
weekly_limit_usd: budget.limits.weekly_usd.canonical(),
|
||||
daily_used_usd: budget.daily_used_usd.canonical(),
|
||||
weekly_used_usd: budget.weekly_used_usd.canonical(),
|
||||
daily_resets_at_display: budget
|
||||
.daily_resets_at()
|
||||
.map(|value| time.datetime(&value.into())),
|
||||
daily_resets_at: budget.daily_resets_at().map(DateTime::from),
|
||||
weekly_resets_at_display: budget
|
||||
.weekly_resets_at()
|
||||
.map(|value| time.datetime(&value.into())),
|
||||
weekly_resets_at: budget.weekly_resets_at().map(DateTime::from),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 不含完整 Key 的管理端安全视图
|
||||
#[derive(Debug, Clone, PartialEq, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ClientKeyView {
|
||||
peer_sharing: gateway_core::policy::ClientPeerSharing,
|
||||
provider_request_profile_overrides: ProviderRequestProfileOverrides,
|
||||
/// 固定兼容字段;值始终从 provider_request_profile_overrides 派生
|
||||
openai_client_profile_override: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
/// 固定兼容字段;值始终从 provider_request_profile_overrides 派生
|
||||
xai_client_profile_override: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
id: String,
|
||||
name: String,
|
||||
label: Option<String>,
|
||||
@@ -355,14 +382,8 @@ pub struct ClientKeyView {
|
||||
enabled: bool,
|
||||
max_concurrency: u64,
|
||||
requests_per_minute: u64,
|
||||
daily_limit_usd: String,
|
||||
weekly_limit_usd: String,
|
||||
daily_used_usd: String,
|
||||
weekly_used_usd: String,
|
||||
daily_resets_at: Option<DateTime<Utc>>,
|
||||
daily_resets_at_display: Option<String>,
|
||||
weekly_resets_at: Option<DateTime<Utc>>,
|
||||
weekly_resets_at_display: Option<String>,
|
||||
#[serde(flatten)]
|
||||
budget: ClientBudgetView,
|
||||
created_at: DateTime<Utc>,
|
||||
created_at_display: String,
|
||||
updated_at: DateTime<Utc>,
|
||||
@@ -385,10 +406,6 @@ impl From<(ClientKeyRecord, crate::time::TimePresenter)> for ClientKeyView {
|
||||
.map(|(provider, profile)| (provider.as_str().to_owned(), profile.into_inner()))
|
||||
.collect::<ProviderRequestProfileOverrides>();
|
||||
Self {
|
||||
openai_client_profile_override: provider_request_profile_overrides
|
||||
.get("openai")
|
||||
.cloned(),
|
||||
xai_client_profile_override: provider_request_profile_overrides.get("xai").cloned(),
|
||||
provider_request_profile_overrides,
|
||||
peer_sharing: record.peer_sharing,
|
||||
id: record.id.to_string(),
|
||||
@@ -409,20 +426,7 @@ impl From<(ClientKeyRecord, crate::time::TimePresenter)> for ClientKeyView {
|
||||
enabled: record.enabled,
|
||||
max_concurrency: record.limits.max_concurrency,
|
||||
requests_per_minute: record.limits.requests_per_minute,
|
||||
daily_limit_usd: record.budget.limits.daily_usd.canonical(),
|
||||
weekly_limit_usd: record.budget.limits.weekly_usd.canonical(),
|
||||
daily_used_usd: record.budget.daily_used_usd.canonical(),
|
||||
weekly_used_usd: record.budget.weekly_used_usd.canonical(),
|
||||
daily_resets_at_display: record
|
||||
.budget
|
||||
.daily_resets_at()
|
||||
.map(|value| time.datetime(&value.into())),
|
||||
daily_resets_at: record.budget.daily_resets_at().map(DateTime::from),
|
||||
weekly_resets_at_display: record
|
||||
.budget
|
||||
.weekly_resets_at()
|
||||
.map(|value| time.datetime(&value.into())),
|
||||
weekly_resets_at: record.budget.weekly_resets_at().map(DateTime::from),
|
||||
budget: ClientBudgetView::from((record.budget, time)),
|
||||
created_at_display: time.datetime(&record.created_at),
|
||||
created_at: record.created_at,
|
||||
updated_at_display: time.datetime(&record.updated_at),
|
||||
@@ -739,11 +743,9 @@ fn validate_limit(value: u64, field: &'static str) -> Result<(), WireValidationE
|
||||
|
||||
fn normalize_request_profile_overrides(
|
||||
profiles: ProviderRequestProfileOverrides,
|
||||
openai: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
xai: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
) -> Result<gateway_admin::model::client_keys::ProviderRequestProfileOverrides, WireValidationError>
|
||||
{
|
||||
let mut normalized = profiles
|
||||
profiles
|
||||
.into_iter()
|
||||
.map(|(provider, profile)| {
|
||||
if !matches!(provider.as_str(), "openai" | "xai") {
|
||||
@@ -756,35 +758,16 @@ fn normalize_request_profile_overrides(
|
||||
gateway_core::account::OpaqueProviderData::new(profile),
|
||||
))
|
||||
})
|
||||
.collect::<Result<gateway_admin::model::client_keys::ProviderRequestProfileOverrides, _>>(
|
||||
)?;
|
||||
for (provider, profile) in [("openai", openai), ("xai", xai)] {
|
||||
let Some(profile) = profile else {
|
||||
continue;
|
||||
};
|
||||
validate_request_profile_size(&profile)?;
|
||||
let provider = ProviderKind::new(provider).expect("static Provider kind is valid");
|
||||
let profile = gateway_core::account::OpaqueProviderData::new(profile);
|
||||
if normalized
|
||||
.get(&provider)
|
||||
.is_some_and(|current| current != &profile)
|
||||
{
|
||||
return Err(WireValidationError::new("providerRequestProfileOverrides"));
|
||||
}
|
||||
normalized.insert(provider, profile);
|
||||
}
|
||||
Ok(normalized)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn normalize_request_profile_override_updates(
|
||||
profiles: ProviderRequestProfileOverrideUpdates,
|
||||
openai: Option<Option<serde_json::Map<String, serde_json::Value>>>,
|
||||
xai: Option<Option<serde_json::Map<String, serde_json::Value>>>,
|
||||
) -> Result<
|
||||
gateway_admin::model::client_keys::ProviderRequestProfileOverrideUpdates,
|
||||
WireValidationError,
|
||||
> {
|
||||
let mut normalized = profiles
|
||||
profiles
|
||||
.into_iter()
|
||||
.map(|(provider, profile)| {
|
||||
if !matches!(provider.as_str(), "openai" | "xai") {
|
||||
@@ -794,34 +777,12 @@ fn normalize_request_profile_override_updates(
|
||||
validate_request_profile_size(profile)?;
|
||||
}
|
||||
Ok((
|
||||
ProviderKind::new(provider).map_err(|_| {
|
||||
WireValidationError::new("providerRequestProfileOverrides")
|
||||
})?,
|
||||
ProviderKind::new(provider)
|
||||
.map_err(|_| WireValidationError::new("providerRequestProfileOverrides"))?,
|
||||
profile.map(gateway_core::account::OpaqueProviderData::new),
|
||||
))
|
||||
})
|
||||
.collect::<Result<
|
||||
gateway_admin::model::client_keys::ProviderRequestProfileOverrideUpdates,
|
||||
_,
|
||||
>>()?;
|
||||
for (provider, profile) in [("openai", openai), ("xai", xai)] {
|
||||
let Some(profile) = profile else {
|
||||
continue;
|
||||
};
|
||||
if let Some(profile) = profile.as_ref() {
|
||||
validate_request_profile_size(profile)?;
|
||||
}
|
||||
let provider = ProviderKind::new(provider).expect("static Provider kind is valid");
|
||||
let profile = profile.map(gateway_core::account::OpaqueProviderData::new);
|
||||
if normalized
|
||||
.get(&provider)
|
||||
.is_some_and(|current| current != &profile)
|
||||
{
|
||||
return Err(WireValidationError::new("providerRequestProfileOverrides"));
|
||||
}
|
||||
normalized.insert(provider, profile);
|
||||
}
|
||||
Ok(normalized)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn validate_request_profile_size(
|
||||
@@ -866,7 +827,12 @@ where
|
||||
S: SessionState + Clone + Send + Sync + 'static,
|
||||
{
|
||||
Router::new()
|
||||
.merge(super::client_key_usage::router::<S>())
|
||||
.route("/api/admin/client-keys", get(list_client_keys::<S>))
|
||||
.route(
|
||||
"/api/admin/client-keys/batch-update",
|
||||
post(batch_update_client_keys::<S>),
|
||||
)
|
||||
.route(
|
||||
"/api/admin/client-keys/create",
|
||||
post(create_client_key::<S>),
|
||||
@@ -913,6 +879,94 @@ where
|
||||
Ok(AdminResponse::new(StatusCode::OK, AdminEnvelope::ok(data)))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct BatchUpdateClientKeysRequest {
|
||||
ids: Vec<String>,
|
||||
group_ids: Option<Vec<String>>,
|
||||
enabled: Option<bool>,
|
||||
max_concurrency: Option<u64>,
|
||||
requests_per_minute: Option<u64>,
|
||||
daily_limit_usd: Option<String>,
|
||||
weekly_limit_usd: Option<String>,
|
||||
peer_sharing: Option<gateway_admin::model::client_keys::ClientPeerSharingPatch>,
|
||||
#[serde(default)]
|
||||
provider_request_profile_overrides: ProviderRequestProfileOverrideUpdates,
|
||||
installation_seed: Option<String>,
|
||||
}
|
||||
|
||||
impl BatchUpdateClientKeysRequest {
|
||||
/// 复用单把密钥的字段校验,缺省和显式零值分别保留
|
||||
pub fn into_command(self) -> Result<BatchUpdateClientKeys, WireValidationError> {
|
||||
if let Some(limit) = self.max_concurrency {
|
||||
validate_limit(limit, "maxConcurrency")?;
|
||||
}
|
||||
if let Some(limit) = self.requests_per_minute {
|
||||
validate_limit(limit, "requestsPerMinute")?;
|
||||
}
|
||||
let installation_seed = self
|
||||
.installation_seed
|
||||
.map(|value| {
|
||||
if value.len() != uuid::fmt::Hyphenated::LENGTH {
|
||||
return Err(WireValidationError::new("installationSeed"));
|
||||
}
|
||||
uuid::Uuid::try_parse(&value)
|
||||
.map_err(|_| WireValidationError::new("installationSeed"))
|
||||
})
|
||||
.transpose()?;
|
||||
Ok(BatchUpdateClientKeys {
|
||||
ids: self
|
||||
.ids
|
||||
.into_iter()
|
||||
.map(|id| client_key_id(id, "id"))
|
||||
.collect::<Result<_, _>>()?,
|
||||
group_ids: self.group_ids.map(validate_group_ids).transpose()?,
|
||||
enabled: self.enabled,
|
||||
max_concurrency: self.max_concurrency,
|
||||
requests_per_minute: self.requests_per_minute,
|
||||
daily_limit_usd: parse_budget(self.daily_limit_usd, "dailyLimitUsd")?,
|
||||
weekly_limit_usd: parse_budget(self.weekly_limit_usd, "weeklyLimitUsd")?,
|
||||
peer_sharing: self.peer_sharing.unwrap_or_default(),
|
||||
request_profile_override_updates: normalize_request_profile_override_updates(
|
||||
self.provider_request_profile_overrides,
|
||||
)?,
|
||||
installation_seed,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct BatchUpdatedClientKeysData {
|
||||
config_revision: gateway_admin::model::Revision,
|
||||
updated: usize,
|
||||
}
|
||||
|
||||
async fn batch_update_client_keys<S>(
|
||||
auth: AdminAuth,
|
||||
State(state): State<S>,
|
||||
AdminJson(request): AdminJson<BatchUpdateClientKeysRequest>,
|
||||
) -> Result<impl IntoResponse, AdminError>
|
||||
where
|
||||
S: SessionState + Send + Sync,
|
||||
{
|
||||
let command = request.into_command().map_err(map_wire_error)?;
|
||||
let updated = command.ids.len();
|
||||
let config_revision = state
|
||||
.admin_services()
|
||||
.client_keys()
|
||||
.batch_update(&auth.context().mutation_context(), command)
|
||||
.await
|
||||
.map_err(map_service_error)?;
|
||||
Ok(AdminResponse::new(
|
||||
StatusCode::OK,
|
||||
AdminEnvelope::ok(BatchUpdatedClientKeysData {
|
||||
config_revision,
|
||||
updated,
|
||||
}),
|
||||
))
|
||||
}
|
||||
|
||||
async fn create_client_key<S>(
|
||||
auth: AdminAuth,
|
||||
State(state): State<S>,
|
||||
@@ -1087,12 +1141,13 @@ fn map_wire_error(error: WireValidationError) -> AdminError {
|
||||
"requestsPerMinute" => "每分钟请求上限超出支持范围",
|
||||
"dailyLimitUsd" => "每日美元限额须非负,整数和小数各最多 10 位",
|
||||
"weeklyLimitUsd" => "每周美元限额须非负,整数和小数各最多 10 位",
|
||||
"providerRequestProfileOverrides" => "API Key 客户端身份配置无效或字段冲突",
|
||||
"providerRequestProfileOverrides" => "API Key 客户端身份配置无效",
|
||||
"installationSeed" => "安装身份种子必须是带连字符的 UUID",
|
||||
"limit" => "每页数量须为 1 至 65535",
|
||||
"search" => "搜索内容过长或包含控制字符",
|
||||
"sortBy" => "排序字段不受支持",
|
||||
"sortDirection" => "排序方向必须为 asc(升序)或 desc(降序)",
|
||||
_ => "API Key 请求参数不合法",
|
||||
field => return AdminError::bad_request(format!("{field} 字段不合法")),
|
||||
};
|
||||
AdminError::bad_request(message)
|
||||
}
|
||||
@@ -1100,10 +1155,3 @@ fn map_wire_error(error: WireValidationError) -> AdminError {
|
||||
fn map_service_error(error: gateway_admin::model::AdminError) -> AdminError {
|
||||
map_admin_service_error(error)
|
||||
}
|
||||
|
||||
// 省略表示不修改,null 表示恢复跟随通用设置
|
||||
fn deserialize_profile_override<'de, D: serde::Deserializer<'de>>(
|
||||
deserializer: D,
|
||||
) -> Result<Option<Option<serde_json::Map<String, serde_json::Value>>>, D::Error> {
|
||||
Option::<serde_json::Map<String, serde_json::Value>>::deserialize(deserializer).map(Some)
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ pub mod account_groups;
|
||||
pub mod accounts;
|
||||
pub mod auth;
|
||||
pub mod backups;
|
||||
mod client_key_usage;
|
||||
pub mod client_keys;
|
||||
mod compression;
|
||||
pub(crate) mod diagnostics;
|
||||
|
||||
@@ -925,25 +925,9 @@ pub(crate) fn dashboard_view(
|
||||
.filter(|window| window.used_percent.is_none())
|
||||
.and_then(|window| window.local_usage.as_ref())
|
||||
.map(|usage| usage.request_count);
|
||||
let (metric, metric_value_raw, metric_label, metric_value) = match local_requests {
|
||||
Some(count) => (
|
||||
DashboardAccountMetric::Requests,
|
||||
Some(count),
|
||||
"次数".to_owned(),
|
||||
if count > 0 {
|
||||
format_compact_number(count)
|
||||
} else {
|
||||
"—".to_owned()
|
||||
},
|
||||
),
|
||||
None => (
|
||||
DashboardAccountMetric::Tokens,
|
||||
credential.total_tokens,
|
||||
"今日 Token".to_owned(),
|
||||
credential
|
||||
.total_tokens
|
||||
.map_or_else(|| "—".to_owned(), format_compact_number),
|
||||
),
|
||||
let (metric, metric_value_raw) = match local_requests {
|
||||
Some(count) => (DashboardAccountMetric::Requests, Some(count)),
|
||||
None => (DashboardAccountMetric::Tokens, credential.total_tokens),
|
||||
};
|
||||
account_usage_views.push(DashboardAccountUsageView {
|
||||
upstream: credential.upstream.map(|info| {
|
||||
@@ -978,13 +962,10 @@ pub(crate) fn dashboard_view(
|
||||
.map(|window| crate::admin::accounts::quota_window_view(window, time)),
|
||||
metric,
|
||||
metric_value_raw,
|
||||
metric_label,
|
||||
metric_value,
|
||||
last_used_at: credential.last_used_at,
|
||||
last_used_relative: credential
|
||||
.last_used_at
|
||||
.map(|value| time.relative_view(value, range.end)),
|
||||
last_used: time.relative_optional(credential.last_used_at, range.end),
|
||||
});
|
||||
}
|
||||
let unavailable_accounts = provider_accounts
|
||||
@@ -1410,7 +1391,14 @@ pub(crate) fn map_wire_error(error: WireValidationError) -> AdminError {
|
||||
"dimension" => "诊断维度不合法",
|
||||
"id" => "用量记录 ID 不能为空",
|
||||
"cursor" => "观测数据游标不合法",
|
||||
_ => "观测查询参数不合法",
|
||||
"currentPage" => "页码须大于 0",
|
||||
"pageSize" => {
|
||||
return AdminError::bad_request(format!("每页数量须为 1 至 {}", DomainPageSize::MAX));
|
||||
}
|
||||
"status" => "请求状态须为 success、error 或 all",
|
||||
"errorOrigin" => "错误来源须为 proxy、upstream、connection 或 unknown",
|
||||
"errorOriginStatus" => "仅错误请求支持按错误来源筛选",
|
||||
field => return AdminError::bad_request(format!("{field} 字段不合法")),
|
||||
};
|
||||
AdminError::bad_request(message)
|
||||
}
|
||||
|
||||
@@ -32,8 +32,9 @@ impl DashboardQuery {
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct UsageQuery {
|
||||
pub client_api_key_name: Option<String>,
|
||||
pub account_name: Option<String>,
|
||||
pub account_search: Option<String>,
|
||||
pub status: Option<String>,
|
||||
pub error_origin: Option<String>,
|
||||
pub current_page: Option<u32>,
|
||||
pub page_size: Option<u16>,
|
||||
pub provider: Option<String>,
|
||||
@@ -339,7 +340,7 @@ pub(crate) fn dashboard_today_range(
|
||||
pub(crate) fn usage_filter(query: &UsageQuery) -> domain::UsageFilter {
|
||||
domain::UsageFilter {
|
||||
client_api_key_name: non_empty(query.client_api_key_name.clone()),
|
||||
provider_account_name: non_empty(query.account_name.clone()),
|
||||
provider_account_search: non_empty(query.account_search.clone()),
|
||||
provider_kind: non_empty(query.provider.clone()),
|
||||
model_contains: non_empty(query.model.clone()),
|
||||
..Default::default()
|
||||
@@ -354,11 +355,21 @@ pub(crate) fn usage_command(
|
||||
let page_size_value =
|
||||
DomainPageSize::new(page_size).map_err(|_| WireValidationError::new("pageSize"))?;
|
||||
Ok(domain::UsageQuery {
|
||||
status: match query.status.as_deref() {
|
||||
None | Some("success") => domain::UsageRecordStatus::Success,
|
||||
Some("error") => domain::UsageRecordStatus::Error,
|
||||
Some("all") => domain::UsageRecordStatus::All,
|
||||
Some(_) => return Err(WireValidationError::new("status")),
|
||||
status: match (query.status.as_deref(), query.error_origin.as_deref()) {
|
||||
(None | Some("success"), None) => domain::UsageRecordStatus::Success,
|
||||
(Some("all"), None) => domain::UsageRecordStatus::All,
|
||||
(Some("error"), origin) => domain::UsageRecordStatus::Error(match origin {
|
||||
None => None,
|
||||
Some("proxy") => Some(domain::UsageErrorOrigin::Proxy),
|
||||
Some("upstream") => Some(domain::UsageErrorOrigin::Upstream),
|
||||
Some("connection") => Some(domain::UsageErrorOrigin::Connection),
|
||||
Some("unknown") => Some(domain::UsageErrorOrigin::Unknown),
|
||||
Some(_) => return Err(WireValidationError::new("errorOrigin")),
|
||||
}),
|
||||
(None | Some("success" | "all"), Some(_)) => {
|
||||
return Err(WireValidationError::new("errorOriginStatus"));
|
||||
}
|
||||
(Some(_), _) => return Err(WireValidationError::new("status")),
|
||||
},
|
||||
range: usage_range(
|
||||
query.start_time.as_deref(),
|
||||
|
||||
@@ -447,11 +447,8 @@ pub struct DashboardAccountUsageView {
|
||||
pub usage_window: Option<crate::admin::accounts::AccountQuotaWindowView>,
|
||||
pub metric: DashboardAccountMetric,
|
||||
pub metric_value_raw: Option<u64>,
|
||||
pub metric_label: String,
|
||||
pub metric_value: String,
|
||||
pub last_used_at: Option<DateTime<Utc>>,
|
||||
pub last_used_relative: Option<crate::time::RelativeTimeView>,
|
||||
pub last_used: String,
|
||||
}
|
||||
|
||||
/// Provider 账号池的持久事实汇总
|
||||
|
||||
@@ -20,9 +20,24 @@ use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::{
|
||||
AdminAuth, AdminEnvelope, AdminError, AdminJson, AdminQuery, AdminResponse, PageMeta,
|
||||
accounts::{AccountGroupRefView, AccountProxyUpdate},
|
||||
accounts::AccountGroupRefView,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
struct ProxyUrl(Option<gateway_core::account::OutboundProxy>);
|
||||
|
||||
impl<'de> Deserialize<'de> for ProxyUrl {
|
||||
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
let value = String::deserialize(deserializer)?;
|
||||
if value.is_empty() {
|
||||
return Ok(Self(None));
|
||||
}
|
||||
gateway_core::account::OutboundProxy::parse(&value)
|
||||
.map(|proxy| Self(Some(proxy)))
|
||||
.map_err(serde::de::Error::custom)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
struct ListQuery {
|
||||
@@ -56,7 +71,7 @@ struct CreateRequest {
|
||||
auto_location: bool,
|
||||
location: Option<gateway_core::account::RequestLocation>,
|
||||
name: String,
|
||||
proxy_url: AccountProxyUpdate,
|
||||
proxy_url: ProxyUrl,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
@@ -70,7 +85,7 @@ struct UpdateRequest {
|
||||
id: String,
|
||||
revision: u64,
|
||||
name: String,
|
||||
proxy_url: Option<AccountProxyUpdate>,
|
||||
proxy_url: Option<ProxyUrl>,
|
||||
}
|
||||
|
||||
fn deserialize_location_update<'de, D>(
|
||||
@@ -105,7 +120,7 @@ struct ProbeRequest {
|
||||
use_deployment_proxy: bool,
|
||||
#[serde(default)]
|
||||
detect_location: bool,
|
||||
proxy_url: AccountProxyUpdate,
|
||||
proxy_url: ProxyUrl,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! Runtime settings、旧设置页聚合投影与明文 Admin API Key wire
|
||||
//! 管理端运行设置、模型定价与 Admin API Key 的 HTTP 接口
|
||||
|
||||
use crate::auth::SessionState;
|
||||
|
||||
@@ -36,6 +36,8 @@ pub type ProviderRequestProfileUpdates =
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct RuntimeSettingsFields {
|
||||
pub openai_oauth_guardian_review_model: bool,
|
||||
pub openai_request_country: gateway_core::account::OpenAiRequestCountry,
|
||||
pub codex_privacy_policy: gateway_core::settings::privacy::CodexPrivacyPolicy,
|
||||
pub request_location_enabled: bool,
|
||||
pub request_location: gateway_core::account::RequestLocation,
|
||||
@@ -64,7 +66,6 @@ pub struct RuntimeSettingsFields {
|
||||
pub account_warmup_enabled: bool,
|
||||
pub account_warmup_cron: String,
|
||||
pub account_warmup_model: Option<String>,
|
||||
#[serde(default)]
|
||||
pub reset_credit_on_low_weekly_quota: bool,
|
||||
pub reset_credit_lead_minutes: u32,
|
||||
pub reset_credit_max_weekly_remaining_percent: Option<u32>,
|
||||
@@ -80,10 +81,6 @@ pub struct RuntimeSettingsView {
|
||||
pub smart_scheduling_defaults: gateway_core::account::SmartSchedulingConfig,
|
||||
pub quota_reset_window_weights_defaults: gateway_core::account::ResetWindowWeights,
|
||||
pub provider_request_profiles: ProviderRequestProfiles,
|
||||
/// 固定兼容字段;值始终从 provider_request_profiles 派生
|
||||
pub openai_client_profile: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
/// 固定兼容字段;值始终从 provider_request_profiles 派生
|
||||
pub xai_client_profile: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
pub model_mappings: ModelMappings,
|
||||
pub rotation_strategy: String,
|
||||
pub updated_at: DateTime<Utc>,
|
||||
@@ -99,12 +96,6 @@ pub struct UpdateRuntimeSettingsRequest {
|
||||
pub config_revision: u64,
|
||||
#[serde(default)]
|
||||
pub provider_request_profiles: ProviderRequestProfileUpdates,
|
||||
/// 兼容既有 wire;与泛化字段冲突时拒绝整个请求
|
||||
#[serde(default, deserialize_with = "deserialize_profile_update")]
|
||||
pub openai_client_profile: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
/// 兼容既有 wire;与泛化字段冲突时拒绝整个请求
|
||||
#[serde(default, deserialize_with = "deserialize_profile_update")]
|
||||
pub xai_client_profile: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
pub model_mappings: ModelMappings,
|
||||
pub rotation_strategy: String,
|
||||
}
|
||||
@@ -121,11 +112,8 @@ impl UpdateRuntimeSettingsRequest {
|
||||
|
||||
fn into_command(self) -> Result<ReplaceRuntimeSettings, WireValidationError> {
|
||||
validate_model_mappings(&self.model_mappings)?;
|
||||
let request_profile_updates = normalize_request_profile_updates(
|
||||
self.provider_request_profiles,
|
||||
self.openai_client_profile,
|
||||
self.xai_client_profile,
|
||||
)?;
|
||||
let request_profile_updates =
|
||||
normalize_request_profile_updates(self.provider_request_profiles)?;
|
||||
Ok(ReplaceRuntimeSettings {
|
||||
expected_revision: gateway_admin::model::Revision::new(self.config_revision)
|
||||
.map_err(|_| WireValidationError::new("configRevision"))?,
|
||||
@@ -147,8 +135,6 @@ impl From<(RuntimeSettings, crate::time::TimePresenter)> for RuntimeSettingsView
|
||||
.collect::<ProviderRequestProfiles>();
|
||||
Self {
|
||||
config_revision: settings.config_revision.get(),
|
||||
openai_client_profile: provider_request_profiles.get("openai").cloned(),
|
||||
xai_client_profile: provider_request_profiles.get("xai").cloned(),
|
||||
provider_request_profiles,
|
||||
model_mappings: wire_model_mappings(settings.model_mappings),
|
||||
smart_scheduling_defaults: gateway_core::account::SmartSchedulingConfig::default(),
|
||||
@@ -595,8 +581,9 @@ fn map_wire_error(error: WireValidationError) -> AdminError {
|
||||
"settingsUsageRetentionOverflow" => "用量记录保留天数不合法".to_owned(),
|
||||
"settingsOpsRetentionOverflow" => "运维事件保留天数不合法".to_owned(),
|
||||
"settingsAuditRetentionOverflow" => "审计记录保留天数不合法".to_owned(),
|
||||
"openaiRequestCountry" => "自定义请求国家必须是两位大写 ASCII 字母".to_owned(),
|
||||
"requestLocation" => "请求位置的国家代码、地区或城市不合法".to_owned(),
|
||||
"providerRequestProfiles" => "服务商客户端身份配置无效或字段冲突".to_owned(),
|
||||
"providerRequestProfiles" => "服务商客户端身份配置无效".to_owned(),
|
||||
"minCodexDesktopVersion" => "Codex Desktop 最低版本格式不合法".to_owned(),
|
||||
"minCodexCliVersion" => "Codex CLI 最低版本格式不合法".to_owned(),
|
||||
field => format!("{field} 字段不合法"),
|
||||
@@ -610,10 +597,8 @@ fn map_service_error(error: gateway_admin::model::AdminError) -> AdminError {
|
||||
|
||||
fn normalize_request_profile_updates(
|
||||
profiles: ProviderRequestProfileUpdates,
|
||||
openai: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
xai: Option<serde_json::Map<String, serde_json::Value>>,
|
||||
) -> Result<gateway_admin::model::settings::ProviderRequestProfileUpdates, WireValidationError> {
|
||||
let mut normalized = profiles
|
||||
profiles
|
||||
.into_iter()
|
||||
.map(|(provider, profile)| {
|
||||
if !matches!(provider.as_str(), "openai" | "xai") {
|
||||
@@ -626,30 +611,7 @@ fn normalize_request_profile_updates(
|
||||
profile.map(gateway_core::account::OpaqueProviderData::new),
|
||||
))
|
||||
})
|
||||
.collect::<Result<gateway_admin::model::settings::ProviderRequestProfileUpdates, _>>()?;
|
||||
for (provider, profile) in [("openai", openai), ("xai", xai)] {
|
||||
let Some(profile) = profile else {
|
||||
continue;
|
||||
};
|
||||
let provider = gateway_core::routing::ProviderKind::new(provider)
|
||||
.expect("static Provider kind is valid");
|
||||
let profile = gateway_core::account::OpaqueProviderData::new(profile);
|
||||
if normalized
|
||||
.get(&provider)
|
||||
.is_some_and(|current| current.as_ref() != Some(&profile))
|
||||
{
|
||||
return Err(WireValidationError::new("providerRequestProfiles"));
|
||||
}
|
||||
normalized.insert(provider, Some(profile));
|
||||
}
|
||||
Ok(normalized)
|
||||
}
|
||||
|
||||
// 字段省略时保留已有配置;显式 null 不能清空唯一的通用默认
|
||||
fn deserialize_profile_update<'de, D: serde::Deserializer<'de>>(
|
||||
deserializer: D,
|
||||
) -> Result<Option<serde_json::Map<String, serde_json::Value>>, D::Error> {
|
||||
serde_json::Map::<String, serde_json::Value>::deserialize(deserializer).map(Some)
|
||||
.collect()
|
||||
}
|
||||
|
||||
async fn detect_location<S>(
|
||||
@@ -746,6 +708,8 @@ impl RuntimeSettingsFields {
|
||||
self,
|
||||
) -> Result<gateway_admin::model::settings::RuntimeSettingsValues, WireValidationError> {
|
||||
let values = gateway_admin::model::settings::RuntimeSettingsValues {
|
||||
openai_oauth_guardian_review_model: self.openai_oauth_guardian_review_model,
|
||||
openai_request_country: self.openai_request_country,
|
||||
codex_privacy_policy: self.codex_privacy_policy,
|
||||
request_location_enabled: self.request_location_enabled,
|
||||
request_location: self
|
||||
@@ -795,6 +759,8 @@ impl RuntimeSettingsFields {
|
||||
impl From<gateway_admin::model::settings::RuntimeSettingsValues> for RuntimeSettingsFields {
|
||||
fn from(settings: gateway_admin::model::settings::RuntimeSettingsValues) -> Self {
|
||||
crate::admin::settings::RuntimeSettingsFields {
|
||||
openai_oauth_guardian_review_model: settings.openai_oauth_guardian_review_model,
|
||||
openai_request_country: settings.openai_request_country,
|
||||
codex_privacy_policy: settings.codex_privacy_policy,
|
||||
request_location_enabled: settings.request_location_enabled,
|
||||
request_location: settings.request_location,
|
||||
@@ -848,6 +814,7 @@ fn settings_validation_error(field: &'static str) -> WireValidationError {
|
||||
"max_waiting_per_key" => "maxWaitingPerKey",
|
||||
"min_codex_cli_version" => "minCodexCliVersion",
|
||||
"min_codex_desktop_version" => "minCodexDesktopVersion",
|
||||
"openai_request_country" => "openaiRequestCountry",
|
||||
"openai_account_affinity" => "openaiAccountAffinity",
|
||||
"openai_guardian_reserved_concurrency" => "openaiGuardianReservedConcurrency",
|
||||
"openai_session_affinity_ttl_hours" => "openaiSessionAffinityTtlHours",
|
||||
|
||||
@@ -215,12 +215,12 @@ const METHOD_NOT_ALLOWED: AdminErrorSpec = AdminErrorSpec::new(
|
||||
const TOO_MANY_LOGIN_ATTEMPTS: AdminErrorSpec = AdminErrorSpec::new(
|
||||
StatusCode::TOO_MANY_REQUESTS,
|
||||
AdminErrorCode::TOO_MANY_LOGIN_ATTEMPTS,
|
||||
"登录尝试过多,请稍后重试",
|
||||
"登录尝试过多",
|
||||
);
|
||||
const RATE_LIMITED: AdminErrorSpec = AdminErrorSpec::new(
|
||||
StatusCode::TOO_MANY_REQUESTS,
|
||||
AdminErrorCode::RATE_LIMITED,
|
||||
"请求过于频繁,请稍后重试",
|
||||
"请求过于频繁",
|
||||
);
|
||||
const INTERNAL: AdminErrorSpec = AdminErrorSpec::new(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
|
||||
@@ -178,7 +178,7 @@ pub(crate) fn client_access_error_response(error: ClientAccessError) -> Response
|
||||
response
|
||||
}
|
||||
|
||||
/// Desktop 优先于内嵌 CLI/Core;未知客户端及未提供应用版本的手机远程客户端返回 `None`
|
||||
/// 识别 Desktop 应用或 CLI/Core;未知客户端及未提供应用版本的手机远程客户端返回 `None`
|
||||
#[must_use]
|
||||
pub fn identify_codex_client(headers: &HeaderMap) -> Option<IdentifiedCodexClient> {
|
||||
const MAXIMUM_HEADER_LENGTH: usize = 4096;
|
||||
@@ -196,25 +196,35 @@ pub fn identify_codex_client(headers: &HeaderMap) -> Option<IdentifiedCodexClien
|
||||
"codex_mcp_server",
|
||||
];
|
||||
|
||||
let originator = bounded_ascii_header(headers, "originator", MAXIMUM_HEADER_LENGTH);
|
||||
let user_agent = bounded_ascii_header(headers, "user-agent", MAXIMUM_HEADER_LENGTH);
|
||||
let is_desktop = originator.is_some_and(|value| value.eq_ignore_ascii_case("Codex Desktop"))
|
||||
let ascii_header = |name: &'static str| headers.get(name).and_then(|value| value.to_str().ok());
|
||||
let raw_user_agent = ascii_header("user-agent");
|
||||
// HeaderValue::to_str 只接受 ASCII,可以按字节截取搜索范围
|
||||
let user_agent = raw_user_agent.map(|ua| &ua[..ua.len().min(MAXIMUM_HEADER_LENGTH)]);
|
||||
let is_desktop = ascii_header("originator")
|
||||
.is_some_and(|value| value.eq_ignore_ascii_case("Codex Desktop"))
|
||||
|| user_agent.is_some_and(|value| contains_ascii_case_insensitive(value, "Codex Desktop"));
|
||||
if is_desktop {
|
||||
let explicit_version = bounded_ascii_header(headers, "version", MAXIMUM_HEADER_LENGTH);
|
||||
let version_header = headers.get("version");
|
||||
let user_agent_version = user_agent.and_then(desktop_version_from_user_agent);
|
||||
// 手机远程初始化会覆盖进程级 UA 后缀,保留的 Desktop/Core 前缀不代表应用版本
|
||||
// 仅缺失版本信息时免于门禁;已提供的版本即使非法,也继续按 Desktop 校验
|
||||
if !headers.contains_key("version")
|
||||
&& user_agent_version.is_none()
|
||||
&& headers
|
||||
.get("user-agent")
|
||||
.is_some_and(|value| value.as_bytes().len() <= MAXIMUM_HEADER_LENGTH)
|
||||
&& user_agent.is_some_and(has_chatgpt_remote_user_agent_suffix)
|
||||
if user_agent_version.is_none()
|
||||
&& let Some(ua) = raw_user_agent.filter(|ua| ua.len() <= MAXIMUM_HEADER_LENGTH)
|
||||
&& let Some(client) = user_agent_client_name(ua)
|
||||
{
|
||||
return None;
|
||||
// 共享 App Server 可保留 Desktop 进程名,TUI 请求的 version 头也可能重复 Core 版本
|
||||
if client.eq_ignore_ascii_case("codex-tui")
|
||||
&& let Some(core) = desktop_core_version(ua)
|
||||
&& version_header.is_none_or(|value| value.as_bytes() == core.as_bytes())
|
||||
{
|
||||
return Some(IdentifiedCodexClient {
|
||||
kind: CodexClientKind::Cli,
|
||||
version: CodexClientVersion::parse(core).ok(),
|
||||
});
|
||||
}
|
||||
if version_header.is_none() && is_chatgpt_remote_client(client) {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
let version = match explicit_version {
|
||||
let version = match version_header.and_then(|value| value.to_str().ok()) {
|
||||
Some(value) => CodexClientVersion::parse(value).ok(),
|
||||
None => user_agent_version.and_then(|value| CodexClientVersion::parse(value).ok()),
|
||||
};
|
||||
@@ -244,18 +254,6 @@ pub fn identify_codex_client(headers: &HeaderMap) -> Option<IdentifiedCodexClien
|
||||
None
|
||||
}
|
||||
|
||||
fn bounded_ascii_header<'a>(
|
||||
headers: &'a HeaderMap,
|
||||
name: &'static str,
|
||||
maximum_length: usize,
|
||||
) -> Option<&'a str> {
|
||||
let value = headers.get(name)?.to_str().ok()?;
|
||||
if !value.is_ascii() {
|
||||
return None;
|
||||
}
|
||||
Some(&value[..value.len().min(maximum_length)])
|
||||
}
|
||||
|
||||
fn desktop_version_from_user_agent(user_agent: &str) -> Option<&str> {
|
||||
let marker = "Codex Desktop;";
|
||||
let start = find_ascii_case_insensitive(user_agent, marker)? + marker.len();
|
||||
@@ -266,27 +264,29 @@ fn desktop_version_from_user_agent(user_agent: &str) -> Option<&str> {
|
||||
.next()
|
||||
}
|
||||
|
||||
fn has_chatgpt_remote_user_agent_suffix(user_agent: &str) -> bool {
|
||||
let Some((_, suffix)) = user_agent
|
||||
.strip_suffix(')')
|
||||
.and_then(|value| value.rsplit_once(" ("))
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
let Some((name, version)) = suffix.split_once("; ") else {
|
||||
return false;
|
||||
};
|
||||
let Some(platform) = name
|
||||
.strip_prefix("codex_chatgpt_")
|
||||
.and_then(|value| value.strip_suffix("_remote"))
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
[platform, version].into_iter().all(|value| {
|
||||
fn desktop_core_version(user_agent: &str) -> Option<&str> {
|
||||
let (product, rest) = user_agent.split_once('/')?;
|
||||
let (version, _) = rest.split_once(' ')?;
|
||||
product
|
||||
.eq_ignore_ascii_case("Codex Desktop")
|
||||
.then_some(version)
|
||||
}
|
||||
|
||||
fn user_agent_client_name(user_agent: &str) -> Option<&str> {
|
||||
let (_, suffix) = user_agent.strip_suffix(')')?.rsplit_once(" (")?;
|
||||
let (name, version) = suffix.split_once("; ")?;
|
||||
let valid_token = |value: &str| {
|
||||
!value.is_empty()
|
||||
&& !value.contains(['(', ')', ';'])
|
||||
&& !value.bytes().any(|byte| byte.is_ascii_whitespace())
|
||||
})
|
||||
};
|
||||
(valid_token(name) && valid_token(version)).then_some(name)
|
||||
}
|
||||
|
||||
fn is_chatgpt_remote_client(name: &str) -> bool {
|
||||
name.strip_prefix("codex_chatgpt_")
|
||||
.and_then(|value| value.strip_suffix("_remote"))
|
||||
.is_some_and(|platform| !platform.is_empty())
|
||||
}
|
||||
|
||||
fn contains_ascii_case_insensitive(haystack: &str, needle: &str) -> bool {
|
||||
|
||||
@@ -115,9 +115,14 @@ async fn models_response(
|
||||
}
|
||||
})
|
||||
.collect::<Result<Vec<_>, ()>>();
|
||||
let Ok(models) = models else {
|
||||
let Ok(mut models) = models else {
|
||||
return catalog_unavailable_response();
|
||||
};
|
||||
if !client.policy().account_scope().allows_subagents() {
|
||||
for model in &mut models {
|
||||
model["multi_agent_version"] = json!("disabled");
|
||||
}
|
||||
}
|
||||
return (
|
||||
StatusCode::OK,
|
||||
// 目录经过账号选择/别名/多 Provider 聚合,不能借用上游 ETag 表示改写后的正文
|
||||
|
||||
@@ -19,7 +19,6 @@ use crate::openai::generation::{
|
||||
};
|
||||
|
||||
const OPENAI_SUBAGENT_KEY: &str = "x-openai-subagent";
|
||||
const CODEX_TURN_METADATA_KEY: &str = "x-codex-turn-metadata";
|
||||
const PASSTHROUGH_HEADERS_CONTEXT_KEY: &str = "opaque_request_headers";
|
||||
const DOWNSTREAM_WEBSOCKET_CONNECTION_ID_CONTEXT_KEY: &str = "downstream_websocket_connection_id";
|
||||
|
||||
@@ -34,11 +33,11 @@ pub(super) enum RequestDecodeSource {
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct OpenAiRequestHeaders {
|
||||
guardian: Option<String>,
|
||||
ambiguous_guardian_headers: bool,
|
||||
turn_state: Option<String>,
|
||||
turn_metadata: Option<String>,
|
||||
beta_features: Option<String>,
|
||||
version: Option<String>,
|
||||
include_timing_metrics: Option<String>,
|
||||
codex_window_id: Option<String>,
|
||||
parent_thread_id: Option<String>,
|
||||
downstream_websocket_connection_id: Option<String>,
|
||||
@@ -60,11 +59,20 @@ impl OpenAiRequestHeaders {
|
||||
#[must_use]
|
||||
pub fn from_headers(headers: &HeaderMap) -> Self {
|
||||
Self {
|
||||
guardian: header_string(headers, "x-codex-guardian"),
|
||||
ambiguous_guardian_headers: [
|
||||
"x-codex-guardian",
|
||||
OPENAI_SUBAGENT_KEY,
|
||||
"x-codex-turn-metadata",
|
||||
]
|
||||
.into_iter()
|
||||
.any(|name| {
|
||||
let mut values = headers.get_all(name).iter();
|
||||
values.next().is_some_and(|v| v.to_str().is_err()) || values.next().is_some()
|
||||
}),
|
||||
turn_state: header_string(headers, "x-codex-turn-state"),
|
||||
turn_metadata: header_string(headers, "x-codex-turn-metadata"),
|
||||
beta_features: header_string(headers, "x-codex-beta-features"),
|
||||
version: header_string(headers, "version"),
|
||||
include_timing_metrics: header_string(headers, "x-responsesapi-include-timing-metrics"),
|
||||
codex_window_id: header_string(headers, "x-codex-window-id"),
|
||||
parent_thread_id: header_string(headers, "x-codex-parent-thread-id"),
|
||||
downstream_websocket_connection_id: None,
|
||||
@@ -92,7 +100,46 @@ impl OpenAiRequestHeaders {
|
||||
self
|
||||
}
|
||||
|
||||
fn apply_subagent(&self, body: &mut Map<String, Value>) {
|
||||
fn apply_subagent(
|
||||
&self,
|
||||
body: &mut Map<String, Value>,
|
||||
context: &mut Map<String, Value>,
|
||||
source: RequestDecodeSource,
|
||||
) {
|
||||
if matches!(source, RequestDecodeSource::WebSocketFrame)
|
||||
&& let Some(frame_agent) = body
|
||||
.get("client_metadata")
|
||||
.and_then(|m| m.get(OPENAI_SUBAGENT_KEY))
|
||||
.and_then(Value::as_str)
|
||||
&& (frame_agent == "guardian"
|
||||
|| self.subagent.as_deref() == Some("guardian")
|
||||
|| self.guardian.is_some())
|
||||
{
|
||||
// 帧内声明优先;旧 opening 的不透明副本也不能在 Provider 再度覆盖
|
||||
let stale_subagent = self
|
||||
.subagent
|
||||
.as_deref()
|
||||
.is_some_and(|opening| opening != frame_agent);
|
||||
let stale_guardian = self.guardian.as_deref().is_some_and(|opening| {
|
||||
frame_agent != "guardian"
|
||||
|| gateway_protocol::openai::codex_guardian_role(body, context)
|
||||
.is_some_and(|role| role.header() != Some(opening))
|
||||
});
|
||||
if stale_subagent {
|
||||
context.insert("subagent".into(), Value::from(frame_agent));
|
||||
}
|
||||
if (stale_subagent || stale_guardian)
|
||||
&& let Some(Value::Array(headers)) =
|
||||
context.get_mut(PASSTHROUGH_HEADERS_CONTEXT_KEY)
|
||||
{
|
||||
headers.retain(|pair| match pair.get(0).and_then(Value::as_str) {
|
||||
Some(OPENAI_SUBAGENT_KEY) => !stale_subagent,
|
||||
Some("x-codex-guardian") => !stale_guardian,
|
||||
_ => true,
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
if let Some(subagent) = self.subagent.as_deref() {
|
||||
inject_subagent_metadata(body, subagent);
|
||||
}
|
||||
@@ -120,22 +167,19 @@ impl OpenAiRequestHeaders {
|
||||
fn protocol_context(
|
||||
&self,
|
||||
use_websocket: Option<bool>,
|
||||
frame_turn_metadata: Option<&String>,
|
||||
source: RequestDecodeSource,
|
||||
) -> Map<String, Value> {
|
||||
let mut context = Map::new();
|
||||
insert_protocol_context(&mut context, "turn_state", self.turn_state.as_ref());
|
||||
insert_protocol_context(
|
||||
&mut context,
|
||||
"turn_metadata",
|
||||
frame_turn_metadata.or(self.turn_metadata.as_ref()),
|
||||
);
|
||||
insert_protocol_context(&mut context, "beta_features", self.beta_features.as_ref());
|
||||
insert_protocol_context(&mut context, "turn_metadata", self.turn_metadata.as_ref());
|
||||
insert_protocol_context(&mut context, "subagent", self.subagent.as_ref());
|
||||
if matches!(source, RequestDecodeSource::WebSocketFrame) {
|
||||
context.insert(
|
||||
gateway_protocol::openai::WEBSOCKET_FRAME_CONTEXT_KEY.to_owned(),
|
||||
Value::Bool(true),
|
||||
);
|
||||
}
|
||||
insert_protocol_context(&mut context, "version", self.version.as_ref());
|
||||
insert_protocol_context(
|
||||
&mut context,
|
||||
"include_timing_metrics",
|
||||
self.include_timing_metrics.as_ref(),
|
||||
);
|
||||
insert_protocol_context(
|
||||
&mut context,
|
||||
"codex_window_id",
|
||||
@@ -337,12 +381,14 @@ pub(super) fn decode_request_object(
|
||||
.map(|response_id| ContinuationIntent::PreviousResponseId(response_id.to_owned()))
|
||||
.unwrap_or(ContinuationIntent::None);
|
||||
|
||||
request_headers.apply_subagent(&mut object);
|
||||
let frame_turn_metadata = match source {
|
||||
RequestDecodeSource::Http => None,
|
||||
RequestDecodeSource::WebSocketFrame => frame_turn_metadata(&object),
|
||||
};
|
||||
let protocol_context = request_headers.protocol_context(use_websocket, frame_turn_metadata);
|
||||
let mut protocol_context = request_headers.protocol_context(use_websocket, source);
|
||||
gateway_protocol::openai::capture_codex_guardian_identity(
|
||||
&object,
|
||||
&mut protocol_context,
|
||||
request_headers.guardian.as_deref(),
|
||||
request_headers.ambiguous_guardian_headers,
|
||||
);
|
||||
request_headers.apply_subagent(&mut object, &mut protocol_context, source);
|
||||
|
||||
let semantics =
|
||||
gateway_protocol::openai::codex_responses_request_semantics(&object, &protocol_context);
|
||||
@@ -381,16 +427,6 @@ pub(super) fn decode_request_object(
|
||||
})
|
||||
}
|
||||
|
||||
fn frame_turn_metadata(body: &Map<String, Value>) -> Option<&String> {
|
||||
body.get("client_metadata")
|
||||
.and_then(Value::as_object)
|
||||
.and_then(|metadata| metadata.get(CODEX_TURN_METADATA_KEY))
|
||||
.and_then(|value| match value {
|
||||
Value::String(value) if !value.trim().is_empty() => Some(value),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
fn inject_subagent_metadata(body: &mut Map<String, Value>, subagent: &str) {
|
||||
let metadata = body
|
||||
.entry("client_metadata".to_owned())
|
||||
|
||||
@@ -74,13 +74,11 @@ pub(crate) async fn responses_websocket(
|
||||
&headers,
|
||||
connect_info.map(|Extension(ConnectInfo(address))| address),
|
||||
);
|
||||
let request_headers = OpenAiRequestHeaders::from_headers(&headers);
|
||||
ResponsesWebSocketAdapter::new(service).upgrade_with_client_context(
|
||||
websocket,
|
||||
client,
|
||||
client_ip,
|
||||
user_agent,
|
||||
request_headers,
|
||||
headers,
|
||||
middleware.map(|Extension(plan)| plan),
|
||||
lifetime.map(|Extension(lifetime)| lifetime),
|
||||
@@ -107,7 +105,6 @@ impl ResponsesWebSocketAdapter {
|
||||
client: AuthenticatedClient,
|
||||
client_ip: Option<IpAddr>,
|
||||
user_agent: Option<String>,
|
||||
request_headers: OpenAiRequestHeaders,
|
||||
raw_headers: HeaderMap,
|
||||
middleware: Option<FrozenMiddlewarePlan>,
|
||||
lifetime: Option<Arc<crate::middleware::CallLifetime>>,
|
||||
@@ -130,7 +127,6 @@ impl ResponsesWebSocketAdapter {
|
||||
connection_id,
|
||||
client_ip,
|
||||
user_agent,
|
||||
request_headers,
|
||||
raw_headers,
|
||||
lifecycle: self.service.lifecycle(),
|
||||
connection_guard,
|
||||
@@ -150,7 +146,6 @@ struct ResponsesWebSocketSession {
|
||||
connection_id: String,
|
||||
client_ip: Option<IpAddr>,
|
||||
user_agent: Option<String>,
|
||||
request_headers: OpenAiRequestHeaders,
|
||||
raw_headers: HeaderMap,
|
||||
lifecycle: Arc<dyn ConnectionLifecycle>,
|
||||
connection_guard: Box<dyn ConnectionGuard>,
|
||||
@@ -164,8 +159,7 @@ async fn serve_responses_websocket(socket: Socket, session: ResponsesWebSocketSe
|
||||
connection_id,
|
||||
client_ip,
|
||||
user_agent,
|
||||
request_headers,
|
||||
raw_headers,
|
||||
mut raw_headers,
|
||||
lifecycle,
|
||||
connection_guard,
|
||||
middleware,
|
||||
@@ -178,8 +172,6 @@ async fn serve_responses_websocket(socket: Socket, session: ResponsesWebSocketSe
|
||||
let cancellation = lifecycle.cancellation().child_token();
|
||||
let _connection_cancellation =
|
||||
crate::middleware::websocket::CancelOnDrop(Some(cancellation.clone()));
|
||||
let request_headers =
|
||||
request_headers.with_downstream_websocket_connection_id(connection_id.clone());
|
||||
// 帧中间件可能改写终态,此时不能据原始响应关闭下游连接
|
||||
let reconnect_for_replay = !middleware
|
||||
.as_ref()
|
||||
@@ -241,31 +233,32 @@ async fn serve_responses_websocket(socket: Socket, session: ResponsesWebSocketSe
|
||||
continue;
|
||||
}
|
||||
request_count = request_count.saturating_add(1);
|
||||
// 初步解码只保留路由事实,避免整份正文跨越准入等待和响应交付
|
||||
let model_hint = match decode_response_create_with_context(&payload, &request_headers) {
|
||||
Ok(decoded) => Some(decoded.metadata().requested_model().to_owned()),
|
||||
Err(error) => {
|
||||
trace_rejected_request(
|
||||
&correlation_id,
|
||||
connection.id(),
|
||||
&payload,
|
||||
"decode",
|
||||
&error.to_string(),
|
||||
);
|
||||
if send_protocol_error(
|
||||
&mut connection,
|
||||
StatusCode::BAD_REQUEST,
|
||||
error.protocol_body(),
|
||||
&correlation_id,
|
||||
)
|
||||
.await
|
||||
== ForwardOutcome::Disconnect
|
||||
{
|
||||
break;
|
||||
// 初步解码只取帧中的模型,实际执行使用该帧当前生效的请求头
|
||||
let model_hint =
|
||||
match decode_response_create_with_context(&payload, &OpenAiRequestHeaders::default()) {
|
||||
Ok(decoded) => Some(decoded.metadata().requested_model().to_owned()),
|
||||
Err(error) => {
|
||||
trace_rejected_request(
|
||||
&correlation_id,
|
||||
connection.id(),
|
||||
&payload,
|
||||
"decode",
|
||||
&error.to_string(),
|
||||
);
|
||||
if send_protocol_error(
|
||||
&mut connection,
|
||||
StatusCode::BAD_REQUEST,
|
||||
error.protocol_body(),
|
||||
&correlation_id,
|
||||
)
|
||||
.await
|
||||
== ForwardOutcome::Disconnect
|
||||
{
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
};
|
||||
};
|
||||
// deadline 与 Text 可能同时就绪;在任何上游执行开始前再次封住该竞争窗口
|
||||
if connection.is_expired() {
|
||||
trace_rejected_request(
|
||||
@@ -336,6 +329,16 @@ async fn serve_responses_websocket(socket: Socket, session: ResponsesWebSocketSe
|
||||
headers: encode_headers(&raw_headers),
|
||||
body: Bytes::from(payload),
|
||||
};
|
||||
// 握手的轮次头只属于首次执行,复用连接时由各 response.create 携带本轮数据
|
||||
raw_headers.remove("x-codex-turn-state");
|
||||
raw_headers.remove("x-codex-turn-metadata");
|
||||
raw_headers.remove("x-codex-guardian");
|
||||
if raw_headers
|
||||
.get("x-openai-subagent")
|
||||
.is_some_and(|value| value == "guardian")
|
||||
{
|
||||
raw_headers.remove("x-openai-subagent");
|
||||
}
|
||||
let service_for_terminal = service.clone();
|
||||
let replay_for_terminal = replay.clone();
|
||||
let connection_id_for_terminal = connection.id().to_owned();
|
||||
|
||||
@@ -19,8 +19,8 @@ use super::{
|
||||
|
||||
use crate::ApiState;
|
||||
|
||||
// 公开协议路径由 API 层统一定义;Core 使用操作类型,不根据这些路径分派业务
|
||||
pub(super) const RESPONSES_PATH: &str = "/v1/responses";
|
||||
// Core 使用操作类型,不根据这些协议路径分派业务
|
||||
pub(super) use gateway_protocol::openai::RESPONSES_PATH;
|
||||
pub(super) const MESSAGES_PATH: &str = "/v1/messages";
|
||||
pub(super) const MESSAGE_TOKENS_PATH: &str = "/v1/messages/count_tokens";
|
||||
pub(super) const CHAT_COMPLETIONS_PATH: &str = "/v1/chat/completions";
|
||||
|
||||
@@ -78,6 +78,58 @@ async fn options_route_should_return_only_lightweight_group_fields() {
|
||||
assert_eq!(value["data"]["configRevision"], 7);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn models_route_reads_the_published_group_catalog() {
|
||||
use gateway_core::{
|
||||
account::ProviderAccountId,
|
||||
routing::{
|
||||
ConfigRevision, ModelCapabilities, ProviderKind, ProviderModel, RuntimeAccount,
|
||||
RuntimeAccountDirectory, RuntimeSnapshot, UpstreamModelId,
|
||||
},
|
||||
};
|
||||
use std::{
|
||||
collections::{BTreeMap, BTreeSet},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
let fixture = authenticated_fixture().await;
|
||||
let uri = format!("/api/admin/account-groups/models?id={PRIMARY_GROUP_ID}");
|
||||
let unavailable = request(router(&fixture), Method::GET, &uri, None, true).await;
|
||||
assert_eq!(unavailable.status(), StatusCode::SERVICE_UNAVAILABLE);
|
||||
let provider = ProviderKind::new("openai").unwrap();
|
||||
let snapshot = RuntimeSnapshot::new(
|
||||
ConfigRevision::new(7).unwrap(),
|
||||
gateway_core::settings::SettingsValues::new(1, "smart", Default::default(), None, None),
|
||||
vec![provider.clone()],
|
||||
vec![ProviderModel::new(
|
||||
provider.clone(),
|
||||
UpstreamModelId::new("model-a").unwrap(),
|
||||
ModelCapabilities::new(
|
||||
BTreeSet::from([gateway_core::operation::OperationKind::Generate]),
|
||||
None,
|
||||
),
|
||||
)],
|
||||
vec![],
|
||||
)
|
||||
.unwrap()
|
||||
.with_account_directory(Arc::new(RuntimeAccountDirectory::new(BTreeMap::from([(
|
||||
ProviderAccountId::new("acct_member").unwrap(),
|
||||
RuntimeAccount::new(
|
||||
provider,
|
||||
BTreeSet::from([super::group_id(PRIMARY_GROUP_ID)]),
|
||||
),
|
||||
)]))));
|
||||
fixture.published_snapshot.publish(snapshot);
|
||||
let response = request(router(&fixture), Method::GET, &uri, None, true).await;
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
assert_eq!(
|
||||
response_json(response).await["data"],
|
||||
json!({"models":[{"id":"model-a", "label":"model-a"}]})
|
||||
);
|
||||
let unauthorized = request(router(&fixture), Method::GET, &uri, None, false).await;
|
||||
assert_eq!(unauthorized.status(), StatusCode::UNAUTHORIZED);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn create_route_should_create_an_empty_group() {
|
||||
let fixture = authenticated_fixture().await;
|
||||
@@ -106,6 +158,11 @@ async fn create_route_should_create_an_empty_group() {
|
||||
assert_eq!(value["data"]["record"]["color"], "#A855F780");
|
||||
assert_eq!(value["data"]["record"]["fastMode"], "default");
|
||||
assert_eq!(value["data"]["record"]["allowImageGeneration"], true);
|
||||
assert_eq!(value["data"]["record"]["allowSubagents"], true);
|
||||
assert_eq!(
|
||||
value["data"]["record"]["modelAccess"],
|
||||
json!({"mode":"all","models":[]})
|
||||
);
|
||||
assert_eq!(value["data"]["configRevision"], 8);
|
||||
}
|
||||
|
||||
@@ -189,6 +246,8 @@ async fn delete_route_should_return_deleted_id_without_a_live_record() {
|
||||
async fn list_route_should_reject_unknown_and_invalid_pagination_fields() {
|
||||
let fixture = authenticated_fixture().await;
|
||||
for uri in [
|
||||
"/api/admin/account-groups/models",
|
||||
"/api/admin/account-groups/models?id=bad",
|
||||
"/api/admin/account-groups?other=true",
|
||||
"/api/admin/account-groups?page=0",
|
||||
"/api/admin/account-groups?pageSize=0",
|
||||
@@ -215,7 +274,7 @@ async fn mutation_route_should_reject_unknown_json_fields() {
|
||||
"name": "Unknown field",
|
||||
"description": null,
|
||||
"color": "#2563EBFF",
|
||||
"providerKind": "openai"
|
||||
"unknownField": true
|
||||
})),
|
||||
true,
|
||||
)
|
||||
@@ -232,7 +291,7 @@ async fn create_route_should_reject_six_digit_color_without_alpha() {
|
||||
Method::POST,
|
||||
"/api/admin/account-groups/create",
|
||||
Some(json!({
|
||||
"name": "Opaque legacy color",
|
||||
"name": "Color without alpha",
|
||||
"description": null,
|
||||
"color": "#2563EB"
|
||||
})),
|
||||
@@ -345,6 +404,8 @@ async fn fast_mode_group_updates_preserve_omitted_and_null_values() {
|
||||
for (value, expected) in [
|
||||
(Some(json!("disabled")), "disabled"),
|
||||
(None, "disabled"),
|
||||
(Some(json!("ultrafast")), "ultrafast"),
|
||||
(None, "ultrafast"),
|
||||
(Some(json!("enabled")), "enabled"),
|
||||
(None, "enabled"),
|
||||
(Some(Value::Null), "enabled"),
|
||||
@@ -376,6 +437,7 @@ async fn fast_mode_rejects_invalid_values_and_creates_groups_in_each_mode() {
|
||||
for (mode, expected) in [
|
||||
(json!("enabled"), StatusCode::CREATED),
|
||||
(json!("disabled"), StatusCode::CREATED),
|
||||
(json!("ultrafast"), StatusCode::CREATED),
|
||||
(json!("default"), StatusCode::CREATED),
|
||||
(json!("force"), StatusCode::UNPROCESSABLE_ENTITY),
|
||||
(json!(true), StatusCode::BAD_REQUEST),
|
||||
@@ -430,3 +492,53 @@ async fn image_generation_flag_round_trips_and_omitted_updates_preserve_it() {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn subagent_and_model_policies_round_trip_independently() {
|
||||
let fixture = authenticated_fixture().await;
|
||||
let policy = json!({"mode":"allowlist", "models":["model-a"]});
|
||||
let created = request(
|
||||
router(&fixture),
|
||||
Method::POST,
|
||||
"/api/admin/account-groups/create",
|
||||
Some(json!({
|
||||
"name":"Model policy", "color":"#2563EBFF", "modelAccess":policy, "allowSubagents":false
|
||||
})),
|
||||
true,
|
||||
)
|
||||
.await;
|
||||
assert_eq!(created.status(), StatusCode::CREATED);
|
||||
let created = response_json(created).await;
|
||||
assert_eq!(created["data"]["record"]["modelAccess"], policy);
|
||||
assert_eq!(created["data"]["record"]["allowSubagents"], false);
|
||||
let mut update = json!({"id":created["data"]["id"], "name":"Model policy", "color":"#2563EBFF", "allowSubagents":true});
|
||||
let send = |body| {
|
||||
request(
|
||||
router(&fixture),
|
||||
Method::POST,
|
||||
"/api/admin/account-groups/update",
|
||||
Some(body),
|
||||
true,
|
||||
)
|
||||
};
|
||||
let changed = send(update.clone()).await;
|
||||
assert_eq!(changed.status(), StatusCode::OK);
|
||||
let changed = response_json(changed).await;
|
||||
assert_eq!(changed["data"]["record"]["modelAccess"], policy);
|
||||
assert_eq!(changed["data"]["record"]["allowSubagents"], true);
|
||||
update.as_object_mut().unwrap().remove("allowSubagents");
|
||||
update["modelAccess"] = json!({"mode":"denylist","models":["model-b"]});
|
||||
let changed = send(update.clone()).await;
|
||||
assert_eq!(changed.status(), StatusCode::OK);
|
||||
let changed = response_json(changed).await;
|
||||
assert_eq!(
|
||||
changed["data"]["record"]["modelAccess"],
|
||||
update["modelAccess"]
|
||||
);
|
||||
assert_eq!(changed["data"]["record"]["allowSubagents"], true);
|
||||
update["modelAccess"] = json!({"mode":"allowlist","models":[]});
|
||||
assert_eq!(
|
||||
send(update).await.status(),
|
||||
StatusCode::UNPROCESSABLE_ENTITY
|
||||
);
|
||||
}
|
||||
|
||||
@@ -341,7 +341,7 @@ async fn account_quota_refresh_time_uses_server_clock_and_deployment_timezone()
|
||||
));
|
||||
let future = now + Duration::days(1);
|
||||
let future_display = zone.local(future).format("%Y-%m-%d %H:%M:%S").to_string();
|
||||
for (observed_at, expected, legacy) in [
|
||||
for (observed_at, expected, display) in [
|
||||
(None, json!(null), "—"),
|
||||
(Some(now), json!({ "kind": "just_now" }), "刚刚"),
|
||||
(
|
||||
@@ -389,7 +389,7 @@ async fn account_quota_refresh_time_uses_server_clock_and_deployment_timezone()
|
||||
let value: serde_json::Value = serde_json::from_slice(&body).unwrap();
|
||||
let quota = &value["data"]["items"][0]["quota"];
|
||||
assert_eq!(quota["refreshedAtRelative"], expected);
|
||||
assert_eq!(quota["refreshedAtDisplay"], legacy);
|
||||
assert_eq!(quota["refreshedAtDisplay"], display);
|
||||
let refreshed_at = quota["refreshedAt"]
|
||||
.as_str()
|
||||
.map(|value| DateTime::parse_from_rfc3339(value).unwrap().to_utc());
|
||||
|
||||
@@ -12,6 +12,63 @@ use serde_json::json;
|
||||
|
||||
use super::{AdminTestFixture, AdminTestState};
|
||||
|
||||
#[test]
|
||||
fn batch_key_form_preserves_omissions_and_accepts_explicit_zero_false_and_inheritance() {
|
||||
use gateway_api::admin::client_keys::BatchUpdateClientKeysRequest;
|
||||
use gateway_core::routing::ProviderKind;
|
||||
let parse = |value| {
|
||||
serde_json::from_value::<BatchUpdateClientKeysRequest>(value)
|
||||
.unwrap()
|
||||
.into_command()
|
||||
};
|
||||
let patch = parse(json!({
|
||||
"ids":["key_a","key_b"], "enabled":false,
|
||||
"maxConcurrency":0, "dailyLimitUsd":"0", "weeklyLimitUsd":"1.75",
|
||||
"peerSharing":{"shareQuota":false}, "providerRequestProfileOverrides":{"xai":null}
|
||||
}))
|
||||
.unwrap();
|
||||
assert_eq!(patch.enabled, Some(false));
|
||||
assert_eq!(patch.max_concurrency, Some(0));
|
||||
assert_eq!(patch.daily_limit_usd.unwrap().canonical(), "0");
|
||||
assert_eq!(patch.weekly_limit_usd.unwrap().canonical(), "1.75");
|
||||
assert_eq!(patch.peer_sharing.share_quota, Some(false));
|
||||
assert_eq!(patch.peer_sharing.enabled, None);
|
||||
assert_eq!(patch.requests_per_minute, None);
|
||||
assert_eq!(patch.group_ids, None);
|
||||
assert_eq!(patch.installation_seed, None);
|
||||
assert_eq!(
|
||||
patch
|
||||
.request_profile_override_updates
|
||||
.get(&ProviderKind::new("xai").unwrap()),
|
||||
Some(&None)
|
||||
);
|
||||
|
||||
for (field, value) in [
|
||||
("dailyLimitUsd", json!("-0.01")),
|
||||
("weeklyLimitUsd", json!("1.00000000001")),
|
||||
("maxConcurrency", json!(u64::MAX)),
|
||||
("requestsPerMinute", json!(u64::MAX)),
|
||||
(
|
||||
"installationSeed",
|
||||
json!("A3A4E0BDF84447068DF8BFF10DE95B3B"),
|
||||
),
|
||||
("providerRequestProfileOverrides", json!({"unknown": null})),
|
||||
] {
|
||||
let mut payload = json!({ "ids":["key_a"] });
|
||||
payload[field] = value;
|
||||
assert_eq!(parse(payload).unwrap_err().field(), field);
|
||||
}
|
||||
let seed = "A3A4E0BD-F844-4706-8DF8-BFF10DE95B3B";
|
||||
assert_eq!(
|
||||
parse(json!({"ids":["key_a"], "installationSeed":seed}))
|
||||
.unwrap()
|
||||
.installation_seed
|
||||
.unwrap()
|
||||
.to_string(),
|
||||
seed.to_lowercase()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reset_budget_requires_an_explicit_supported_period_and_valid_key() {
|
||||
use gateway_api::admin::client_keys::ResetClientKeyBudgetRequest;
|
||||
@@ -572,45 +629,26 @@ async fn list_route_should_accept_the_full_nonzero_u16_page_size() {
|
||||
|
||||
#[test]
|
||||
fn profile_override_distinguishes_omission_from_explicit_inheritance() {
|
||||
let mut payload = json!({"id":"key_profile", "name":"profile", "groupIds":[], "maxConcurrency":0, "requestsPerMinute":0});
|
||||
let decode = |body| {
|
||||
let mut command = serde_json::from_value::<UpdateClientKeyRequest>(body)
|
||||
.unwrap()
|
||||
.into_command()
|
||||
.unwrap();
|
||||
command
|
||||
.request_profile_override_updates
|
||||
.remove(&gateway_core::routing::ProviderKind::new("openai").expect("Provider kind"))
|
||||
};
|
||||
assert_eq!(decode(payload.clone()), None);
|
||||
payload["openaiClientProfileOverride"] = json!(null);
|
||||
assert_eq!(decode(payload.clone()), Some(None));
|
||||
payload["openaiClientProfileOverride"] =
|
||||
json!({"client":"cli", "platform":"linux", "versionMode":"latest"});
|
||||
assert!(decode(payload).unwrap().is_some());
|
||||
for provider in ["openai", "xai"] {
|
||||
let decode = |body| {
|
||||
serde_json::from_value::<UpdateClientKeyRequest>(body)
|
||||
.unwrap()
|
||||
.into_command()
|
||||
.unwrap()
|
||||
.request_profile_override_updates
|
||||
.remove(&gateway_core::routing::ProviderKind::new(provider).unwrap())
|
||||
};
|
||||
let mut payload = json!({"id":"key_profile", "name":"profile", "groupIds":[], "maxConcurrency":0, "requestsPerMinute":0});
|
||||
assert_eq!(decode(payload.clone()), None);
|
||||
payload["providerRequestProfileOverrides"] = json!({(provider):null});
|
||||
assert_eq!(decode(payload.clone()), Some(None));
|
||||
payload["providerRequestProfileOverrides"][provider] = json!({"versionMode":"latest"});
|
||||
assert!(decode(payload).unwrap().is_some());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xai_profile_override_distinguishes_omission_from_explicit_inheritance() {
|
||||
let mut payload = json!({"id":"key_profile", "name":"profile", "groupIds":[], "maxConcurrency":0, "requestsPerMinute":0});
|
||||
let decode = |body| {
|
||||
let mut command = serde_json::from_value::<UpdateClientKeyRequest>(body)
|
||||
.unwrap()
|
||||
.into_command()
|
||||
.unwrap();
|
||||
command
|
||||
.request_profile_override_updates
|
||||
.remove(&gateway_core::routing::ProviderKind::new("xai").expect("Provider kind"))
|
||||
};
|
||||
assert_eq!(decode(payload.clone()), None);
|
||||
payload["xaiClientProfileOverride"] = json!(null);
|
||||
assert_eq!(decode(payload.clone()), Some(None));
|
||||
payload["xaiClientProfileOverride"] = json!({"versionMode":"latest"});
|
||||
assert!(decode(payload).unwrap().is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generic_profile_overrides_accept_native_providers_and_reject_legacy_conflicts() {
|
||||
fn profile_overrides_accept_native_providers_and_reject_unknown_providers() {
|
||||
let profile = json!({"preset":"desktop"});
|
||||
let xai = json!({"preset":"managed"});
|
||||
let request: CreateClientKeyRequest = serde_json::from_value(json!({
|
||||
@@ -622,7 +660,6 @@ fn generic_profile_overrides_accept_native_providers_and_reject_legacy_conflicts
|
||||
"openai": profile,
|
||||
"xai": xai,
|
||||
},
|
||||
"openaiClientProfileOverride": {"preset":"desktop"},
|
||||
}))
|
||||
.unwrap();
|
||||
let command = request.into_command().unwrap();
|
||||
@@ -633,20 +670,6 @@ fn generic_profile_overrides_accept_native_providers_and_reject_legacy_conflicts
|
||||
.contains_key(&gateway_core::routing::ProviderKind::new("xai").expect("Provider kind"))
|
||||
);
|
||||
|
||||
let conflict: CreateClientKeyRequest = serde_json::from_value(json!({
|
||||
"name":"profile",
|
||||
"groupIds":[],
|
||||
"maxConcurrency":0,
|
||||
"requestsPerMinute":0,
|
||||
"providerRequestProfileOverrides": {"openai":{"preset":"desktop"}},
|
||||
"openaiClientProfileOverride": {"preset":"cli"},
|
||||
}))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
conflict.into_command().unwrap_err().field(),
|
||||
"providerRequestProfileOverrides"
|
||||
);
|
||||
|
||||
let unknown: CreateClientKeyRequest = serde_json::from_value(json!({
|
||||
"name":"profile",
|
||||
"groupIds":[],
|
||||
@@ -675,20 +698,80 @@ fn generic_profile_overrides_accept_native_providers_and_reject_legacy_conflicts
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generic_profile_override_clear_rejects_conflicting_legacy_update() {
|
||||
let request: UpdateClientKeyRequest = serde_json::from_value(json!({
|
||||
"id":"key_profile",
|
||||
"name":"profile",
|
||||
"groupIds":[],
|
||||
"maxConcurrency":0,
|
||||
"requestsPerMinute":0,
|
||||
"providerRequestProfileOverrides": {"xai":null},
|
||||
"xaiClientProfileOverride": {"preset":"desktop"},
|
||||
}))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
request.into_command().unwrap_err().field(),
|
||||
"providerRequestProfileOverrides"
|
||||
);
|
||||
#[tokio::test]
|
||||
async fn key_statistics_route_returns_calendar_days_and_current_budget_only_to_admin() {
|
||||
use crate::support::{cookie_request, key_fixture, response_json};
|
||||
use axum::http::{Method, StatusCode};
|
||||
use gateway_core::policy::ClientApiKeyId;
|
||||
use tower::ServiceExt as _;
|
||||
|
||||
let fixture = key_fixture().await;
|
||||
fixture.auth.insert_session("stats-admin");
|
||||
let record = fixture
|
||||
.services
|
||||
.client_keys()
|
||||
.reveal(&ClientApiKeyId::new("key-42").unwrap())
|
||||
.await
|
||||
.unwrap()
|
||||
.record;
|
||||
*fixture.client_key.lock().unwrap() = Some(record);
|
||||
let app = client_keys::router::<AdminTestState>().with_state(fixture.state());
|
||||
for (query, cookie, status, days) in [
|
||||
("id=key-42", "", StatusCode::UNAUTHORIZED, 0),
|
||||
(
|
||||
"id=missing",
|
||||
"cpr_session=stats-admin",
|
||||
StatusCode::NOT_FOUND,
|
||||
0,
|
||||
),
|
||||
(
|
||||
"id=key-42&period=today",
|
||||
"cpr_session=stats-admin",
|
||||
StatusCode::BAD_REQUEST,
|
||||
0,
|
||||
),
|
||||
("id=key-42", "cpr_session=stats-admin", StatusCode::OK, 7),
|
||||
(
|
||||
"id=key-42&period=30d",
|
||||
"cpr_session=stats-admin",
|
||||
StatusCode::OK,
|
||||
30,
|
||||
),
|
||||
] {
|
||||
let mut request = cookie_request(
|
||||
Method::GET,
|
||||
&format!("/api/admin/client-keys/statistics?{query}"),
|
||||
cookie,
|
||||
);
|
||||
request
|
||||
.headers_mut()
|
||||
.insert("x-request-id", "req_stats".parse().unwrap());
|
||||
let response = app.clone().oneshot(request).await.unwrap();
|
||||
assert_eq!(response.status(), status);
|
||||
if days > 0 {
|
||||
let body = response_json(response).await;
|
||||
assert_eq!(body["data"]["dates"].as_array().unwrap().len(), days);
|
||||
assert!(body["data"]["budget"]["dailyUsedUsd"].is_string());
|
||||
assert!(body["data"]["series"].as_array().unwrap().is_empty());
|
||||
assert_eq!(body["data"]["modelCount"], 0);
|
||||
}
|
||||
}
|
||||
for (cookie, status) in [
|
||||
("", StatusCode::UNAUTHORIZED),
|
||||
("cpr_session=stats-admin", StatusCode::OK),
|
||||
] {
|
||||
let mut request = cookie_request(
|
||||
Method::GET,
|
||||
"/api/admin/client-keys/statistics/models?id=key-42",
|
||||
cookie,
|
||||
);
|
||||
request
|
||||
.headers_mut()
|
||||
.insert("x-request-id", "req_models".parse().unwrap());
|
||||
let response = app.clone().oneshot(request).await.unwrap();
|
||||
assert_eq!(response.status(), status);
|
||||
if status == StatusCode::OK {
|
||||
assert_eq!(response_json(response).await["data"], serde_json::json!([]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -442,29 +442,14 @@ mod provider {
|
||||
*fixture.account.lock().unwrap() = Some(account(provider));
|
||||
for (kind, status, code, message) in [
|
||||
(Kind::Invalid, 400, 40001, "刷新令牌已被使用,请重新授权"),
|
||||
(
|
||||
Kind::Conflict,
|
||||
409,
|
||||
40901,
|
||||
"令牌刷新繁忙,请等待当前刷新完成后重试",
|
||||
),
|
||||
(
|
||||
Kind::BadGateway,
|
||||
502,
|
||||
50201,
|
||||
"OpenAI 返回的 Codex PAT 身份资料不完整或格式无效,请稍后重试",
|
||||
),
|
||||
(
|
||||
Kind::Unavailable,
|
||||
503,
|
||||
50301,
|
||||
"暂时无法向 OpenAI 验证 Codex PAT,请稍后重试",
|
||||
),
|
||||
(Kind::Conflict, 409, 40901, "令牌刷新繁忙,请稍后重试"),
|
||||
(Kind::BadGateway, 502, 50201, "PAT 身份信息无效"),
|
||||
(Kind::Unavailable, 503, 50301, "PAT 验证服务暂不可用"),
|
||||
(
|
||||
Kind::Ambiguous,
|
||||
502,
|
||||
50202,
|
||||
"令牌刷新结果未知,请先核对账号状态,不要立即重复刷新",
|
||||
"刷新结果未知,核对账号状态前请勿重试",
|
||||
),
|
||||
] {
|
||||
*fixture.provider_error.lock().unwrap() = Some(
|
||||
@@ -513,6 +498,12 @@ mod provider {
|
||||
(Kind::Unsupported, 400, 40001, "该服务商不支持此操作"),
|
||||
(Kind::BadGateway, 502, 50201, "上游服务请求失败"),
|
||||
(Kind::Unavailable, 503, 50301, "服务商暂不可用"),
|
||||
(
|
||||
Kind::CredentialRefreshRequired,
|
||||
503,
|
||||
50301,
|
||||
"账号凭据需要刷新",
|
||||
),
|
||||
(
|
||||
Kind::Ambiguous,
|
||||
502,
|
||||
|
||||
@@ -641,43 +641,7 @@ impl SettingsStore for MemorySettingsStore {
|
||||
model_mappings: command.model_mappings,
|
||||
rotation_strategy: command.rotation_strategy,
|
||||
updated_at: Utc::now(),
|
||||
values: gateway_admin::model::settings::RuntimeSettingsValues {
|
||||
codex_privacy_policy: command.values.codex_privacy_policy,
|
||||
request_location_enabled: command.values.request_location_enabled,
|
||||
request_location: command.values.request_location,
|
||||
refresh_margin_seconds: command.values.refresh_margin_seconds,
|
||||
refresh_concurrency: command.values.refresh_concurrency,
|
||||
max_concurrent_per_account: command.values.max_concurrent_per_account,
|
||||
max_waiting_per_key: command.values.max_waiting_per_key,
|
||||
max_waiting_per_account: command.values.max_waiting_per_account,
|
||||
key_queue_timeout_seconds: command.values.key_queue_timeout_seconds,
|
||||
account_queue_timeout_seconds: command.values.account_queue_timeout_seconds,
|
||||
switch_account_on_queue_failure: command.values.switch_account_on_queue_failure,
|
||||
max_retry_after_seconds: command.values.max_retry_after_seconds,
|
||||
openai_guardian_reserved_concurrency: command
|
||||
.values
|
||||
.openai_guardian_reserved_concurrency,
|
||||
openai_account_affinity: command.values.openai_account_affinity,
|
||||
max_account_rotations: command.values.max_account_rotations,
|
||||
openai_session_affinity_ttl_hours: command.values.openai_session_affinity_ttl_hours,
|
||||
responses_max_decompressed_body_bytes: command
|
||||
.values
|
||||
.responses_max_decompressed_body_bytes,
|
||||
smart_scheduling: command.values.smart_scheduling,
|
||||
quota_reset_window_weights: command.values.quota_reset_window_weights,
|
||||
min_codex_desktop_version: command.values.min_codex_desktop_version,
|
||||
min_codex_cli_version: command.values.min_codex_cli_version,
|
||||
usage_retention_days: command.values.usage_retention_days,
|
||||
ops_event_retention_days: command.values.ops_event_retention_days,
|
||||
audit_retention_days: command.values.audit_retention_days,
|
||||
account_error_protection: command.values.account_error_protection,
|
||||
account_warmup_enabled: false,
|
||||
account_warmup_cron: "0 8 * * *".to_owned(),
|
||||
account_warmup_model: None,
|
||||
reset_credit_lead_minutes: 10,
|
||||
reset_credit_on_low_weekly_quota: false,
|
||||
reset_credit_max_weekly_remaining_percent: None,
|
||||
},
|
||||
values: command.values,
|
||||
};
|
||||
*settings = updated.clone();
|
||||
Ok(updated)
|
||||
@@ -745,6 +709,8 @@ impl MemoryAccountGroupStore {
|
||||
primary_id.clone(),
|
||||
AccountGroupRecord {
|
||||
allow_image_generation: true,
|
||||
allow_subagents: true,
|
||||
model_access: Default::default(),
|
||||
fast_mode: FastMode::Default,
|
||||
id: primary_id,
|
||||
name: "Alpha routing".to_owned(),
|
||||
@@ -768,6 +734,8 @@ impl MemoryAccountGroupStore {
|
||||
secondary_id.clone(),
|
||||
AccountGroupRecord {
|
||||
allow_image_generation: true,
|
||||
allow_subagents: true,
|
||||
model_access: Default::default(),
|
||||
fast_mode: FastMode::Default,
|
||||
id: secondary_id,
|
||||
name: "Beta routing".to_owned(),
|
||||
@@ -931,6 +899,8 @@ impl AccountGroupStore for MemoryAccountGroupStore {
|
||||
let now = Utc::now();
|
||||
let record = AccountGroupRecord {
|
||||
allow_image_generation: command.allow_image_generation,
|
||||
allow_subagents: command.allow_subagents,
|
||||
model_access: command.model_access,
|
||||
fast_mode: command.fast_mode,
|
||||
id: command.id.clone(),
|
||||
name: command.name,
|
||||
@@ -966,6 +936,12 @@ impl AccountGroupStore for MemoryAccountGroupStore {
|
||||
if let Some(fast_mode) = command.fast_mode {
|
||||
record.fast_mode = fast_mode;
|
||||
}
|
||||
if let Some(allowed) = command.allow_subagents {
|
||||
record.allow_subagents = allowed;
|
||||
}
|
||||
if let Some(model_access) = command.model_access {
|
||||
record.model_access = model_access;
|
||||
}
|
||||
if let Some(allowed) = command.allow_image_generation {
|
||||
record.allow_image_generation = allowed;
|
||||
}
|
||||
@@ -1020,6 +996,30 @@ fn mutation(
|
||||
|
||||
#[async_trait]
|
||||
impl ClientKeyStore for MemoryClientKeyStore {
|
||||
async fn budget_reference_accounts(
|
||||
&self,
|
||||
_: &gateway_core::policy::ClientApiKeyId,
|
||||
_: &str,
|
||||
_: &str,
|
||||
) -> gateway_admin::ports::store::AdminStoreResult<
|
||||
gateway_admin::model::client_keys::BudgetReferenceAccountPage,
|
||||
> {
|
||||
Err(unavailable("reference accounts"))
|
||||
}
|
||||
|
||||
async fn batch_update_client_keys(
|
||||
&self,
|
||||
_: gateway_admin::model::client_keys::BatchUpdateClientKeys,
|
||||
_: gateway_admin::ports::store::BatchClientKeyProfileMerger,
|
||||
_: &MutationContext,
|
||||
) -> AdminStoreResult<Revision> {
|
||||
Err(AdminStoreError::new(
|
||||
AdminStoreErrorKind::Unavailable,
|
||||
"client key",
|
||||
"unused batch update",
|
||||
))
|
||||
}
|
||||
|
||||
async fn client_key_budget_window(
|
||||
&self,
|
||||
_: &gateway_core::policy::ClientApiKeyId,
|
||||
@@ -1088,6 +1088,15 @@ impl ClientKeyStore for MemoryClientKeyStore {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn client_key_budget(
|
||||
&self,
|
||||
id: &gateway_core::policy::ClientApiKeyId,
|
||||
) -> AdminStoreResult<Option<gateway_core::engine::budget::ClientBudgetStatus>> {
|
||||
self.get_client_key(id)
|
||||
.await
|
||||
.map(|key| key.map(|row| row.budget))
|
||||
}
|
||||
|
||||
async fn get_client_key(
|
||||
&self,
|
||||
id: &ClientApiKeyId,
|
||||
@@ -1543,6 +1552,23 @@ impl ObservabilityStore for UnusedStore {
|
||||
Err(unavailable("dashboard trend"))
|
||||
}
|
||||
|
||||
async fn client_key_statistics(
|
||||
&self,
|
||||
_: TimeRange,
|
||||
_: &gateway_core::policy::ClientApiKeyId,
|
||||
_: gateway_admin::model::observability::KeyUsageMetric,
|
||||
) -> AdminStoreResult<gateway_admin::model::observability::KeyStatisticsOverview> {
|
||||
Ok(Default::default())
|
||||
}
|
||||
|
||||
async fn client_key_models(
|
||||
&self,
|
||||
_: TimeRange,
|
||||
_: &gateway_core::policy::ClientApiKeyId,
|
||||
) -> AdminStoreResult<Vec<gateway_admin::model::observability::KeyModelRow>> {
|
||||
Ok(Default::default())
|
||||
}
|
||||
|
||||
async fn usage_trend(
|
||||
&self,
|
||||
range: TimeRange,
|
||||
@@ -1934,6 +1960,8 @@ fn test_runtime_settings() -> RuntimeSettings {
|
||||
rotation_strategy: RotationStrategy::Smart,
|
||||
updated_at: Utc::now(),
|
||||
values: gateway_admin::model::settings::RuntimeSettingsValues {
|
||||
openai_oauth_guardian_review_model: true,
|
||||
openai_request_country: Default::default(),
|
||||
codex_privacy_policy: Default::default(),
|
||||
request_location_enabled: false,
|
||||
request_location: Default::default(),
|
||||
|
||||
@@ -917,7 +917,7 @@ pub(super) fn usage_record_with_account(
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn usage_route_should_forward_name_and_model_contains_filters() {
|
||||
async fn usage_route_should_forward_record_text_filters() {
|
||||
use axum::{
|
||||
body::Body,
|
||||
http::{Request, StatusCode, header},
|
||||
@@ -933,7 +933,7 @@ async fn usage_route_should_forward_name_and_model_contains_filters() {
|
||||
.with_state(fixture.state())
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri("/api/admin/usage/records?model=%20coding%20&clientApiKeyName=%20Key%20%25%20&accountName=%20Account%20Name%20")
|
||||
.uri("/api/admin/usage/records?model=%20coding%20&clientApiKeyName=%20Key%20%25%20&accountSearch=%20alice%40%20")
|
||||
.header(header::COOKIE, "cpr_session=valid-session")
|
||||
.header("x-request-id", "req_usage_other_outcome")
|
||||
.body(Body::empty())
|
||||
@@ -947,10 +947,7 @@ async fn usage_route_should_forward_name_and_model_contains_filters() {
|
||||
let filter = &observations.records.last().expect("usage query").filter;
|
||||
assert_eq!(filter.client_api_key_name.as_deref(), Some("Key %"));
|
||||
assert_eq!(filter.model_contains.as_deref(), Some("coding"));
|
||||
assert_eq!(
|
||||
filter.provider_account_name.as_deref(),
|
||||
Some("Account Name")
|
||||
);
|
||||
assert_eq!(filter.provider_account_search.as_deref(), Some("alice@"));
|
||||
assert!(filter.client_api_key_ref.is_none());
|
||||
assert!(filter.provider_account_ref.is_none());
|
||||
}
|
||||
@@ -1729,6 +1726,8 @@ async fn observability_queries_reject_group_filters_and_analytics_search() {
|
||||
"routingGroupRefs=group-a",
|
||||
),
|
||||
("/api/admin/usage/records/summary", "search=request-prefix"),
|
||||
("/api/admin/usage/records/summary", "errorOrigin=proxy"),
|
||||
("/api/admin/usage/insights/overview", "errorOrigin=proxy"),
|
||||
(
|
||||
"/api/admin/usage/insights/overview",
|
||||
"search=request-prefix",
|
||||
@@ -1823,27 +1822,50 @@ pub(super) fn ops_error_record() -> gateway_admin::model::observability::OpsErro
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn usage_status_query_accepts_three_modes_and_rejects_unknown_values() {
|
||||
async fn usage_record_query_accepts_error_sources_only_for_error_status() {
|
||||
use crate::admin::{AdminTestFixture, AdminTestState};
|
||||
use axum::{
|
||||
body::{Body, to_bytes},
|
||||
http::{Request, StatusCode, header},
|
||||
};
|
||||
use gateway_admin::model::observability::{UsageErrorOrigin, UsageRecordStatus};
|
||||
use gateway_api::admin::observability;
|
||||
use tower::ServiceExt as _;
|
||||
let fixture = AdminTestFixture::new().await;
|
||||
fixture.auth.insert_session("valid-session");
|
||||
for (status, expected) in [
|
||||
("success", StatusCode::OK),
|
||||
("error", StatusCode::OK),
|
||||
("all", StatusCode::OK),
|
||||
("failed", StatusCode::BAD_REQUEST),
|
||||
for (query, expected) in [
|
||||
("", Some(UsageRecordStatus::Success)),
|
||||
("status=success", Some(UsageRecordStatus::Success)),
|
||||
("status=error", Some(UsageRecordStatus::Error(None))),
|
||||
("status=all", Some(UsageRecordStatus::All)),
|
||||
(
|
||||
"status=error&errorOrigin=proxy",
|
||||
Some(UsageRecordStatus::Error(Some(UsageErrorOrigin::Proxy))),
|
||||
),
|
||||
(
|
||||
"status=error&errorOrigin=upstream",
|
||||
Some(UsageRecordStatus::Error(Some(UsageErrorOrigin::Upstream))),
|
||||
),
|
||||
(
|
||||
"status=error&errorOrigin=connection",
|
||||
Some(UsageRecordStatus::Error(Some(UsageErrorOrigin::Connection))),
|
||||
),
|
||||
(
|
||||
"status=error&errorOrigin=unknown",
|
||||
Some(UsageRecordStatus::Error(Some(UsageErrorOrigin::Unknown))),
|
||||
),
|
||||
("status=failed", None),
|
||||
("errorOrigin=proxy", None),
|
||||
("status=success&errorOrigin=proxy", None),
|
||||
("status=all&errorOrigin=unknown", None),
|
||||
("status=error&errorOrigin=other", None),
|
||||
("status=error&errorOrigin=", None),
|
||||
] {
|
||||
let response = observability::router::<AdminTestState>()
|
||||
.with_state(fixture.state())
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri(format!("/api/admin/usage/records?status={status}"))
|
||||
.uri(format!("/api/admin/usage/records?{query}"))
|
||||
.header(header::COOKIE, "cpr_session=valid-session")
|
||||
.header("x-request-id", "usage_status_fixture")
|
||||
.body(Body::empty())
|
||||
@@ -1855,17 +1877,15 @@ async fn usage_status_query_accepts_three_modes_and_rejects_unknown_values() {
|
||||
let body = to_bytes(response.into_body(), 1024 * 1024).await.unwrap();
|
||||
assert_eq!(
|
||||
actual,
|
||||
expected,
|
||||
"{status}: {}",
|
||||
if expected.is_some() {
|
||||
StatusCode::OK
|
||||
} else {
|
||||
StatusCode::BAD_REQUEST
|
||||
},
|
||||
"{query}: {}",
|
||||
String::from_utf8_lossy(&body)
|
||||
);
|
||||
if expected == StatusCode::OK {
|
||||
use gateway_admin::model::observability::UsageRecordStatus;
|
||||
let status_value = match status {
|
||||
"success" => UsageRecordStatus::Success,
|
||||
"error" => UsageRecordStatus::Error,
|
||||
_ => UsageRecordStatus::All,
|
||||
};
|
||||
if let Some(status_value) = expected {
|
||||
assert_eq!(
|
||||
fixture
|
||||
.observations
|
||||
@@ -2019,16 +2039,15 @@ async fn dashboard_summary_identifies_trend_metrics_and_account_usage_values() {
|
||||
assert_eq!(status, StatusCode::OK, "{kind}: {value}");
|
||||
let accounts = value["data"]["accountUsage"].as_array().unwrap();
|
||||
assert_eq!(accounts.len(), 5);
|
||||
for (account, (metric, raw, display, last_used_at)) in accounts.iter().zip([
|
||||
("tokens", Some(2_000), "2K", Some(now)),
|
||||
("tokens", Some(0), "0", None),
|
||||
("tokens", None, "—", None),
|
||||
("requests", Some(9), "9", Some(now)),
|
||||
("requests", Some(0), "—", None),
|
||||
for (account, (metric, raw, last_used_at)) in accounts.iter().zip([
|
||||
("tokens", Some(2_000), Some(now)),
|
||||
("tokens", Some(0), None),
|
||||
("tokens", None, None),
|
||||
("requests", Some(9), Some(now)),
|
||||
("requests", Some(0), None),
|
||||
]) {
|
||||
assert_eq!(account["metric"], metric);
|
||||
assert_eq!(account["metricValueRaw"], json!(raw));
|
||||
assert_eq!(account["metricValue"], display);
|
||||
assert_eq!(account["lastUsedAt"], json!(last_used_at));
|
||||
}
|
||||
let summary = value["data"]["trend"]["summary"].as_array().unwrap();
|
||||
@@ -2062,50 +2081,42 @@ async fn dashboard_relative_time_preserves_server_anchor_boundaries_and_deployme
|
||||
("Asia/Kathmandu", "2026-06-15 05:45:01"),
|
||||
("America/New_York", "2026-06-14 20:00:01"),
|
||||
] {
|
||||
for (elapsed_ms, expected, legacy) in [
|
||||
(None, json!(null), "—"),
|
||||
for (elapsed_ms, expected) in [
|
||||
(None, json!(null)),
|
||||
(
|
||||
Some(-1_000),
|
||||
json!({ "kind": "absolute", "value": future_display }),
|
||||
future_display,
|
||||
),
|
||||
(Some(-1), json!({ "kind": "just_now" }), "刚刚"),
|
||||
(Some(0), json!({ "kind": "just_now" }), "刚刚"),
|
||||
(Some(59_999), json!({ "kind": "just_now" }), "刚刚"),
|
||||
(Some(-1), json!({ "kind": "just_now" })),
|
||||
(Some(0), json!({ "kind": "just_now" })),
|
||||
(Some(59_999), json!({ "kind": "just_now" })),
|
||||
(
|
||||
Some(60_000),
|
||||
json!({ "kind": "elapsed", "value": 1, "unit": "minute" }),
|
||||
"1 分钟前",
|
||||
),
|
||||
(
|
||||
Some(119_999),
|
||||
json!({ "kind": "elapsed", "value": 1, "unit": "minute" }),
|
||||
"1 分钟前",
|
||||
),
|
||||
(
|
||||
Some(3_599_999),
|
||||
json!({ "kind": "elapsed", "value": 59, "unit": "minute" }),
|
||||
"59 分钟前",
|
||||
),
|
||||
(
|
||||
Some(3_600_000),
|
||||
json!({ "kind": "elapsed", "value": 1, "unit": "hour" }),
|
||||
"1 小时前",
|
||||
),
|
||||
(
|
||||
Some(86_399_999),
|
||||
json!({ "kind": "elapsed", "value": 23, "unit": "hour" }),
|
||||
"23 小时前",
|
||||
),
|
||||
(
|
||||
Some(86_400_000),
|
||||
json!({ "kind": "elapsed", "value": 1, "unit": "day" }),
|
||||
"1 天前",
|
||||
),
|
||||
(
|
||||
Some(34_560_000_000),
|
||||
json!({ "kind": "elapsed", "value": 400, "unit": "day" }),
|
||||
"400 天前",
|
||||
),
|
||||
] {
|
||||
// 每个边界使用独立缓存,避免相同锚点复用上一轮观测数据
|
||||
@@ -2177,7 +2188,6 @@ async fn dashboard_relative_time_preserves_server_anchor_boundaries_and_deployme
|
||||
account["lastUsedRelative"], expected,
|
||||
"{zone}: {elapsed_ms:?}"
|
||||
);
|
||||
assert_eq!(account["lastUsed"], legacy, "{zone}: {elapsed_ms:?}");
|
||||
assert_eq!(account["lastUsedAt"], json!(last_used_at));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +40,8 @@ async fn response_json(response: axum::response::Response) -> Value {
|
||||
fn update_body() -> Value {
|
||||
json!({
|
||||
"configRevision": 7,
|
||||
"openaiOauthGuardianReviewModel": true,
|
||||
"openaiRequestCountry": {"mode": "follow_location"},
|
||||
"codexPrivacyPolicy": {"enabled":false,"onError":"skip_rule","rules":[]},
|
||||
"requestLocationEnabled": false,
|
||||
"requestLocation": {"country":"US", "region":"Ohio", "city":"Piketon", "timezone":"America/New_York"},
|
||||
@@ -73,6 +75,7 @@ fn update_body() -> Value {
|
||||
"accountWarmupEnabled": false,
|
||||
"accountWarmupCron": "0 8 * * *",
|
||||
"accountWarmupModel": null,
|
||||
"resetCreditOnLowWeeklyQuota": false,
|
||||
"resetCreditLeadMinutes": 10,
|
||||
"resetCreditMaxWeeklyRemainingPercent": null
|
||||
})
|
||||
@@ -431,7 +434,7 @@ fn settings_request_accepts_unlimited_default_account_concurrency() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn settings_request_validates_cron_and_allows_disabled_empty_schedule() {
|
||||
fn warmup_cron_is_required_and_may_be_empty_only_when_disabled() {
|
||||
for (enabled, cron, valid) in [
|
||||
(false, "", true),
|
||||
(true, "", false),
|
||||
@@ -461,10 +464,9 @@ fn settings_request_validates_cron_and_allows_disabled_empty_schedule() {
|
||||
let request: UpdateRuntimeSettingsRequest = serde_json::from_value(oversized).unwrap();
|
||||
assert_eq!(request.validate().unwrap_err().field(), "accountWarmupCron");
|
||||
|
||||
let mut legacy = update_body();
|
||||
legacy.as_object_mut().unwrap().remove("accountWarmupCron");
|
||||
legacy["accountWarmupScheduleTime"] = json!("08:00");
|
||||
assert!(serde_json::from_value::<UpdateRuntimeSettingsRequest>(legacy).is_err());
|
||||
let mut missing = update_body();
|
||||
missing.as_object_mut().unwrap().remove("accountWarmupCron");
|
||||
assert!(serde_json::from_value::<UpdateRuntimeSettingsRequest>(missing).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -537,6 +539,17 @@ fn settings_service_contract_flattens_values_and_rejects_unknown_fields() {
|
||||
serde_json::from_value::<ReplaceRuntimeSettings>(wire.clone()).unwrap(),
|
||||
command
|
||||
);
|
||||
for field in [
|
||||
"openai_oauth_guardian_review_model",
|
||||
"openai_request_country",
|
||||
"reset_credit_on_low_weekly_quota",
|
||||
] {
|
||||
let mut incomplete = wire.clone();
|
||||
incomplete.as_object_mut().unwrap().remove(field);
|
||||
assert!(serde_json::from_value::<ReplaceRuntimeSettings>(incomplete.clone()).is_err());
|
||||
incomplete[field] = Value::Null;
|
||||
assert!(serde_json::from_value::<ReplaceRuntimeSettings>(incomplete).is_err());
|
||||
}
|
||||
wire["unexpected_setting"] = json!(true);
|
||||
assert!(serde_json::from_value::<ReplaceRuntimeSettings>(wire).is_err());
|
||||
}
|
||||
@@ -705,8 +718,6 @@ async fn admin_auth_should_accept_a_configured_request_id_header_name() {
|
||||
|
||||
let fixture = AdminTestFixture::new().await;
|
||||
fixture.auth.insert_session("valid-session");
|
||||
// 部署把 api.request_id_header 改名后,注入的 header 不再叫 x-request-id;
|
||||
// 管理请求仍须拿到请求上下文,而不是退化为 500
|
||||
let custom = HeaderName::from_static("x-trace-id");
|
||||
let app = app(fixture.state()).layer(SetRequestIdLayer::new(custom, MakeRequestUuid));
|
||||
let unlabelled = Request::builder()
|
||||
@@ -905,33 +916,19 @@ async fn settings_update_rejects_unknown_fields_without_writing() {
|
||||
assert_eq!(*fixture.settings.settings.lock().unwrap(), initial);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn global_profile_can_be_omitted_but_cannot_be_cleared() {
|
||||
for field in ["openaiClientProfile", "xaiClientProfile"] {
|
||||
let mut body = update_body();
|
||||
body.as_object_mut().unwrap().remove(field);
|
||||
assert!(serde_json::from_value::<UpdateRuntimeSettingsRequest>(body.clone()).is_ok());
|
||||
body[field] = json!(null);
|
||||
assert!(serde_json::from_value::<UpdateRuntimeSettingsRequest>(body.clone()).is_err());
|
||||
body[field] = json!({"versionMode":"latest"});
|
||||
assert!(serde_json::from_value::<UpdateRuntimeSettingsRequest>(body).is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn generic_global_profiles_decode_native_providers_and_reject_legacy_conflicts() {
|
||||
let mut body = update_body();
|
||||
body["providerRequestProfiles"] = json!({
|
||||
"openai":{"preset":"desktop"},
|
||||
"xai":{"preset":"managed"},
|
||||
});
|
||||
body["openaiClientProfile"] = json!({"preset":"desktop"});
|
||||
let decoded = serde_json::from_value::<UpdateRuntimeSettingsRequest>(body.clone()).unwrap();
|
||||
assert!(decoded.provider_request_profiles.contains_key("xai"));
|
||||
|
||||
body["openaiClientProfile"] = json!({"preset":"cli"});
|
||||
async fn global_profiles_map_only_updates_submitted_providers() {
|
||||
let fixture = AdminTestFixture::new().await;
|
||||
fixture.auth.insert_session("valid-session");
|
||||
{
|
||||
let mut settings = fixture.settings.settings.lock().unwrap();
|
||||
settings.request_profiles = serde_json::from_value(json!({
|
||||
"openai":{"preset":"desktop"}, "xai":{"versionMode":"latest"}
|
||||
}))
|
||||
.unwrap();
|
||||
}
|
||||
let mut body = update_body();
|
||||
body["providerRequestProfiles"] = json!({"xai":null});
|
||||
let response = app(fixture.state())
|
||||
.oneshot(request(
|
||||
Method::POST,
|
||||
@@ -940,7 +937,12 @@ async fn generic_global_profiles_decode_native_providers_and_reject_legacy_confl
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let saved = response_json(response).await;
|
||||
assert_eq!(
|
||||
saved["data"]["providerRequestProfiles"],
|
||||
json!({"openai":{"preset":"desktop"}})
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -1419,6 +1421,99 @@ async fn settings_update_rejects_a_stale_version_without_replacing_the_saved_val
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn runtime_settings_reject_incomplete_and_stale_replacements() {
|
||||
let fixture = AdminTestFixture::new().await;
|
||||
fixture.auth.insert_session("valid-session");
|
||||
let read = || app(fixture.state()).oneshot(request(Method::GET, "/api/admin/settings", None));
|
||||
let save = |body| {
|
||||
app(fixture.state()).oneshot(request(
|
||||
Method::POST,
|
||||
"/api/admin/settings/update",
|
||||
Some(body),
|
||||
))
|
||||
};
|
||||
let initial = response_json(read().await.unwrap()).await["data"].clone();
|
||||
assert_eq!(initial["openaiOauthGuardianReviewModel"], true);
|
||||
assert_eq!(
|
||||
initial["openaiRequestCountry"],
|
||||
json!({"mode":"follow_location"})
|
||||
);
|
||||
let mut body = update_body();
|
||||
body["configRevision"] = initial["configRevision"].clone();
|
||||
body["openaiOauthGuardianReviewModel"] = json!(false);
|
||||
body["openaiRequestCountry"] = json!({"mode":"custom","country":"CA"});
|
||||
body["resetCreditOnLowWeeklyQuota"] = json!(true);
|
||||
let response = save(body.clone()).await.unwrap();
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let saved = response_json(response).await["data"].clone();
|
||||
assert_eq!(saved["resetCreditOnLowWeeklyQuota"], true);
|
||||
assert_eq!(saved["openaiOauthGuardianReviewModel"], false);
|
||||
assert_eq!(saved["openaiRequestCountry"], body["openaiRequestCountry"]);
|
||||
|
||||
for field in [
|
||||
"openaiOauthGuardianReviewModel",
|
||||
"openaiRequestCountry",
|
||||
"resetCreditOnLowWeeklyQuota",
|
||||
] {
|
||||
let mut incomplete = update_body();
|
||||
incomplete["configRevision"] = saved["configRevision"].clone();
|
||||
incomplete.as_object_mut().unwrap().remove(field);
|
||||
assert_eq!(
|
||||
save(incomplete.clone()).await.unwrap().status(),
|
||||
StatusCode::UNPROCESSABLE_ENTITY
|
||||
);
|
||||
incomplete[field] = Value::Null;
|
||||
assert_eq!(
|
||||
save(incomplete).await.unwrap().status(),
|
||||
StatusCode::UNPROCESSABLE_ENTITY
|
||||
);
|
||||
}
|
||||
body["openaiOauthGuardianReviewModel"] = json!(true);
|
||||
body["openaiRequestCountry"] = json!({"mode":"passthrough"});
|
||||
assert_eq!(
|
||||
save(body.clone()).await.unwrap().status(),
|
||||
StatusCode::CONFLICT
|
||||
);
|
||||
let current = response_json(read().await.unwrap()).await["data"].clone();
|
||||
assert_eq!(current, saved);
|
||||
body["configRevision"] = current["configRevision"].clone();
|
||||
let response = save(body).await.unwrap();
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let changed = response_json(response).await["data"].clone();
|
||||
assert_eq!(changed["openaiOauthGuardianReviewModel"], true);
|
||||
assert_eq!(
|
||||
changed["openaiRequestCountry"],
|
||||
json!({"mode":"passthrough"})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn openai_request_settings_validate_modes_and_country_codes() {
|
||||
for invalid in [
|
||||
json!(null),
|
||||
json!({"mode":"custom"}),
|
||||
json!({"mode":"custom","country":"ca"}),
|
||||
json!({"mode":"custom","country":"USA"}),
|
||||
json!({"mode":"custom","country":"国"}),
|
||||
json!({"mode":"follow_location","country":"CA"}),
|
||||
json!({"mode":"passthrough","country":"CA"}),
|
||||
json!({"mode":"default","country":"CA"}),
|
||||
json!({"mode":"unknown"}),
|
||||
] {
|
||||
let mut body = update_body();
|
||||
body["openaiRequestCountry"] = invalid.clone();
|
||||
if let Ok(request) = serde_json::from_value::<UpdateRuntimeSettingsRequest>(body) {
|
||||
assert!(request.validate().is_err(), "{invalid}");
|
||||
}
|
||||
}
|
||||
for invalid in [json!(null), json!("true"), json!(1)] {
|
||||
let mut body = update_body();
|
||||
body["openaiOauthGuardianReviewModel"] = invalid;
|
||||
assert!(serde_json::from_value::<UpdateRuntimeSettingsRequest>(body).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn guardian_reservation_round_trips_and_rejects_invalid_values() {
|
||||
let fixture = AdminTestFixture::new().await;
|
||||
|
||||
@@ -198,7 +198,7 @@ mod response {
|
||||
response_json(generic).await,
|
||||
json!({
|
||||
"code": 42902,
|
||||
"message": "请求过于频繁,请稍后重试",
|
||||
"message": "请求过于频繁",
|
||||
"data": null
|
||||
})
|
||||
);
|
||||
@@ -212,7 +212,7 @@ mod response {
|
||||
response_json(login).await,
|
||||
json!({
|
||||
"code": 42901,
|
||||
"message": "登录尝试过多,请稍后重试",
|
||||
"message": "登录尝试过多",
|
||||
"data": null
|
||||
})
|
||||
);
|
||||
|
||||
@@ -345,7 +345,7 @@ async fn records_keep_pagination_and_hide_admin_and_upstream_data() {
|
||||
assert_eq!(data.records.len(), 3);
|
||||
for (query, status) in data.records.iter().zip([
|
||||
UsageRecordStatus::Success,
|
||||
UsageRecordStatus::Error,
|
||||
UsageRecordStatus::Error(None),
|
||||
UsageRecordStatus::All,
|
||||
]) {
|
||||
assert_eq!(query.status, status);
|
||||
|
||||
@@ -228,6 +228,46 @@ fn desktop_headers_should_take_precedence_over_embedded_cli_marker() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn desktop_hosted_tui_should_use_core_unless_a_desktop_version_is_supplied() {
|
||||
for (core, version_header, kind, version) in [
|
||||
("0.170.0", None, CodexClientKind::Cli, Some("0.170.0")),
|
||||
(
|
||||
"0.170.0",
|
||||
Some("0.170.0"),
|
||||
CodexClientKind::Cli,
|
||||
Some("0.170.0"),
|
||||
),
|
||||
("invalid", None, CodexClientKind::Cli, None),
|
||||
(
|
||||
"0.170.0",
|
||||
Some("26.1023.51421"),
|
||||
CodexClientKind::Desktop,
|
||||
Some("26.1023.51421"),
|
||||
),
|
||||
("0.170.0", Some("invalid"), CodexClientKind::Desktop, None),
|
||||
] {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert("originator", HeaderValue::from_static("Codex Desktop"));
|
||||
headers.insert(
|
||||
"user-agent",
|
||||
HeaderValue::from_str(&format!(
|
||||
"Codex Desktop/{core} (Ubuntu 26.4.0; aarch64) xterm-256color (codex-tui; 0.169.0)"
|
||||
))
|
||||
.unwrap(),
|
||||
);
|
||||
if let Some(value) = version_header {
|
||||
headers.insert("version", HeaderValue::from_static(value));
|
||||
}
|
||||
let client = identify_codex_client(&headers).unwrap();
|
||||
assert_eq!(client.kind(), kind);
|
||||
assert_eq!(
|
||||
client.version().map(ToString::to_string).as_deref(),
|
||||
version
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn official_core_clients_should_use_the_core_version_instead_of_the_app_suffix() {
|
||||
for product in [
|
||||
@@ -322,7 +362,7 @@ fn chatgpt_remote_desktop_without_app_version_should_not_use_a_version_gate() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chatgpt_remote_marker_should_require_a_complete_exact_client_suffix() {
|
||||
fn desktop_should_require_a_complete_exact_client_suffix() {
|
||||
for suffix in [
|
||||
"(codex_chatgpt_android_remote_extra; dev)",
|
||||
"(unofficial_codex_chatgpt_ios_remote; dev)",
|
||||
@@ -340,6 +380,9 @@ fn chatgpt_remote_marker_should_require_a_complete_exact_client_suffix() {
|
||||
"(Codex Desktop; ) (codex_chatgpt_android_remote; dev)",
|
||||
"(Codex Desktop; invalid) (codex_chatgpt_ios_remote; dev)",
|
||||
"(Codex Desktop)",
|
||||
"(codex-tui-custom; 0.169.0)",
|
||||
"(codex-tui; 0.169.0) trailing",
|
||||
"(codex-tui; )",
|
||||
] {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
@@ -395,19 +438,23 @@ fn chatgpt_remote_marker_should_not_override_an_existing_desktop_version_suffix(
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chatgpt_remote_suffix_created_by_header_truncation_should_not_skip_the_gate() {
|
||||
let suffix = " (codex_chatgpt_android_remote; dev)";
|
||||
let mut user_agent = "Codex Desktop/0.154.0-alpha.6.2 ".to_owned();
|
||||
user_agent.push_str(&"x".repeat(4096 - user_agent.len() - suffix.len()));
|
||||
user_agent.push_str(suffix);
|
||||
user_agent.push_str(" (another_client; dev)");
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert("user-agent", HeaderValue::from_str(&user_agent).unwrap());
|
||||
fn truncated_client_suffix_should_not_change_the_desktop_gate() {
|
||||
for suffix in [
|
||||
" (codex_chatgpt_android_remote; dev)",
|
||||
" (codex-tui; 0.169.0)",
|
||||
] {
|
||||
let mut user_agent = "Codex Desktop/0.154.0-alpha.6.2 ".to_owned();
|
||||
user_agent.push_str(&"x".repeat(4096 - user_agent.len() - suffix.len()));
|
||||
user_agent.push_str(suffix);
|
||||
user_agent.push_str(" (another_client; dev)");
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert("user-agent", HeaderValue::from_str(&user_agent).unwrap());
|
||||
|
||||
let client =
|
||||
identify_codex_client(&headers).expect("incomplete headers still require a version");
|
||||
assert_eq!(client.kind(), CodexClientKind::Desktop);
|
||||
assert!(client.version().is_none());
|
||||
let client =
|
||||
identify_codex_client(&headers).expect("incomplete headers still require a version");
|
||||
assert_eq!(client.kind(), CodexClientKind::Desktop);
|
||||
assert!(client.version().is_none());
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
+61
-15
@@ -1,4 +1,4 @@
|
||||
//! 真实 HTTP / WebSocket 路由与 Core 执行入口共同拒绝生图,不触发准入或上游
|
||||
//! 真实 HTTP / WebSocket 路由与 Core 执行入口共同执行分组功能授权,不触发准入或上游
|
||||
|
||||
use super::*;
|
||||
use axum::{
|
||||
@@ -11,14 +11,15 @@ use std::{sync::Arc, time::Duration};
|
||||
use tokio_tungstenite::tungstenite::{Message, client::IntoClientRequest};
|
||||
use tower::ServiceExt;
|
||||
|
||||
fn execution() -> Arc<DefaultExecutionService> {
|
||||
pub(super) fn execution() -> Arc<DefaultExecutionService> {
|
||||
Arc::new(DefaultExecutionService::new(
|
||||
RuntimeSnapshotHandle::new(snapshot_with_image_policy(
|
||||
"sk_image_policy",
|
||||
RuntimeSnapshotHandle::new(snapshot_with_group_permissions(
|
||||
"sk_group_permissions",
|
||||
"openai",
|
||||
"key_image_policy",
|
||||
"key_group_permissions",
|
||||
&["acct_api_test"],
|
||||
false,
|
||||
false,
|
||||
)),
|
||||
Arc::new(UnusedExecutionStore),
|
||||
ProviderRegistry::default(),
|
||||
@@ -54,7 +55,7 @@ async fn denied_image_requests_return_403_before_admission_or_upstream() {
|
||||
.clone()
|
||||
.oneshot(
|
||||
Request::post(path)
|
||||
.header(AUTHORIZATION, "Bearer sk_image_policy")
|
||||
.header(AUTHORIZATION, "Bearer sk_group_permissions")
|
||||
.header("content-type", "application/json")
|
||||
.body(Body::from(body.to_string()))
|
||||
.unwrap(),
|
||||
@@ -71,7 +72,7 @@ async fn denied_image_requests_return_403_before_admission_or_upstream() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn each_websocket_create_enforces_image_policy() {
|
||||
async fn each_websocket_create_enforces_group_permissions() {
|
||||
let app = api_router(execution()).await;
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
@@ -81,18 +82,33 @@ async fn each_websocket_create_enforces_image_policy() {
|
||||
let mut request = format!("ws://{address}/v1/responses")
|
||||
.into_client_request()
|
||||
.unwrap();
|
||||
request
|
||||
.headers_mut()
|
||||
.insert(AUTHORIZATION, "Bearer sk_image_policy".parse().unwrap());
|
||||
request.headers_mut().insert(
|
||||
AUTHORIZATION,
|
||||
"Bearer sk_group_permissions".parse().unwrap(),
|
||||
);
|
||||
let (mut socket, _) = tokio_tungstenite::connect_async(request).await.unwrap();
|
||||
for image_fields in [
|
||||
json!({"tools":[{"type":"image_generation"}]}),
|
||||
json!({"tool_choice":{"type":"image_generation"}}),
|
||||
for (fields, code) in [
|
||||
(
|
||||
json!({"tools":[{"type":"image_generation"}]}),
|
||||
"image_generation_disabled",
|
||||
),
|
||||
(
|
||||
json!({"client_metadata":{"x-codex-turn-metadata":r#"{"subagent_kind":"thread_spawn"}"#}}),
|
||||
"subagents_disabled",
|
||||
),
|
||||
(
|
||||
json!({"tool_choice":{"type":"image_generation"}}),
|
||||
"image_generation_disabled",
|
||||
),
|
||||
(
|
||||
json!({"client_metadata":{"x-openai-subagent":"collab_spawn"}}),
|
||||
"subagents_disabled",
|
||||
),
|
||||
] {
|
||||
let mut body = json!({"type":"response.create","model":"model-a","input":"test"});
|
||||
body.as_object_mut()
|
||||
.unwrap()
|
||||
.extend(image_fields.as_object().unwrap().clone());
|
||||
.extend(fields.as_object().unwrap().clone());
|
||||
socket
|
||||
.send(Message::Text(body.to_string().into()))
|
||||
.await
|
||||
@@ -105,8 +121,38 @@ async fn each_websocket_create_enforces_image_policy() {
|
||||
let body: Value = serde_json::from_str(frame.to_text().unwrap()).unwrap();
|
||||
assert_eq!(body["type"], "error", "{body}");
|
||||
assert_eq!(body["status"], 403);
|
||||
assert_eq!(body["error"]["code"], "image_generation_disabled");
|
||||
assert_eq!(body["error"]["code"], code);
|
||||
}
|
||||
socket.close(None).await.unwrap();
|
||||
server.abort();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn collaboration_requests_are_denied_before_admission_or_upstream() {
|
||||
let app = api_router(execution()).await;
|
||||
for (header, value) in [
|
||||
("x-openai-subagent", "collab_spawn"),
|
||||
(
|
||||
"x-codex-turn-metadata",
|
||||
r#"{"subagent_kind":"thread_spawn"}"#,
|
||||
),
|
||||
] {
|
||||
let response = app
|
||||
.clone()
|
||||
.oneshot(
|
||||
Request::post("/v1/responses")
|
||||
.header(AUTHORIZATION, "Bearer sk_group_permissions")
|
||||
.header("content-type", "application/json")
|
||||
.header(header, value)
|
||||
.body(Body::from(r#"{"model":"model-a","input":"test"}"#))
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(response.status(), StatusCode::FORBIDDEN);
|
||||
let body: Value =
|
||||
serde_json::from_slice(&to_bytes(response.into_body(), usize::MAX).await.unwrap())
|
||||
.unwrap();
|
||||
assert_eq!(body["error"]["code"], "subagents_disabled");
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ mod auth;
|
||||
mod chat_completions;
|
||||
mod endpoint;
|
||||
mod error;
|
||||
mod image_policy;
|
||||
mod group_permissions;
|
||||
mod images;
|
||||
mod live;
|
||||
mod messages;
|
||||
@@ -300,15 +300,23 @@ fn snapshot_with_accounts(
|
||||
client_key_id: &str,
|
||||
accounts: &[&str],
|
||||
) -> RuntimeSnapshot {
|
||||
snapshot_with_image_policy(plaintext, provider_name, client_key_id, accounts, true)
|
||||
snapshot_with_group_permissions(
|
||||
plaintext,
|
||||
provider_name,
|
||||
client_key_id,
|
||||
accounts,
|
||||
true,
|
||||
true,
|
||||
)
|
||||
}
|
||||
|
||||
fn snapshot_with_image_policy(
|
||||
fn snapshot_with_group_permissions(
|
||||
plaintext: &str,
|
||||
provider_name: &str,
|
||||
client_key_id: &str,
|
||||
accounts: &[&str],
|
||||
allow_image_generation: bool,
|
||||
allow_subagents: bool,
|
||||
) -> RuntimeSnapshot {
|
||||
let provider = ProviderKind::new(provider_name).expect("provider");
|
||||
let account_directory = Arc::new(RuntimeAccountDirectory::new(BTreeMap::from_iter(
|
||||
@@ -345,7 +353,8 @@ fn snapshot_with_image_policy(
|
||||
Arc::clone(&account_directory),
|
||||
ClientRoutingScope::all_accounts(),
|
||||
)
|
||||
.with_image_generation(allow_image_generation),
|
||||
.with_image_generation(allow_image_generation)
|
||||
.with_subagents(allow_subagents),
|
||||
),
|
||||
true,
|
||||
RateLimits::unlimited(),
|
||||
|
||||
@@ -406,13 +406,13 @@ async fn model_query_middleware_short_circuit_sets_json_content_type_without_cal
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn native_catalog_preserves_complete_objects_and_only_rewrites_alias_slug() {
|
||||
async fn native_catalog_preserves_upstream_fields_except_alias_and_group_subagent_policy() {
|
||||
let original = serde_json::json!({
|
||||
"slug": "gpt-native", "display_name": "Official name", "priority": 47,
|
||||
"base_instructions": "Official instructions\nDo not replace.",
|
||||
"model_messages": {"token_budget": "upstream template", "future_section": [null, {"a": true}]},
|
||||
"service_tiers": [{"id": "priority", "name": "Fast", "description": "Official speed tier"}],
|
||||
"tool_mode": "code_mode_only", "use_responses_lite": true,
|
||||
"tool_mode": "code_mode_only", "use_responses_lite": true, "multi_agent_version": "v2",
|
||||
"supports_reasoning_summary_parameter": true, "default_reasoning_summary": "detailed",
|
||||
"future_field": {"nested": [1, null, "value"]}, "explicit_null": null
|
||||
});
|
||||
@@ -424,35 +424,49 @@ async fn native_catalog_preserves_complete_objects_and_only_rewrites_alias_slug(
|
||||
)
|
||||
.expect("payload"),
|
||||
};
|
||||
let execution = Arc::new(ModelsExecution {
|
||||
client: authenticated_client("sk_models_test"),
|
||||
presentation: None,
|
||||
native: Some(Ok(vec![entry("gpt-native"), entry("my-alias")])),
|
||||
requested_versions: Mutex::default(),
|
||||
middleware: None,
|
||||
});
|
||||
let response = api_router(execution.clone())
|
||||
.await
|
||||
.oneshot(authorized_request(
|
||||
"/v1/models?client_version=0.154.0-alpha.3",
|
||||
))
|
||||
.await
|
||||
.expect("response");
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
assert_eq!(response.headers()["cache-control"], "private, no-store");
|
||||
assert!(response.headers().get("etag").is_none());
|
||||
let body = to_bytes(response.into_body(), usize::MAX)
|
||||
.await
|
||||
.expect("body");
|
||||
let value: serde_json::Value = serde_json::from_slice(&body).expect("JSON");
|
||||
assert_eq!(value["models"][0], original);
|
||||
let mut alias = original;
|
||||
alias["slug"] = serde_json::json!("my-alias");
|
||||
assert_eq!(value["models"][1], alias);
|
||||
assert_eq!(
|
||||
*execution.requested_versions.lock().expect("versions"),
|
||||
["0.154.0-alpha.3"]
|
||||
);
|
||||
for (allowed, client) in [
|
||||
(true, authenticated_client("sk_models_test")),
|
||||
(
|
||||
false,
|
||||
super::group_permissions::execution()
|
||||
.authenticate("sk_group_permissions")
|
||||
.unwrap(),
|
||||
),
|
||||
] {
|
||||
let execution = Arc::new(ModelsExecution {
|
||||
client,
|
||||
presentation: None,
|
||||
native: Some(Ok(vec![entry("gpt-native"), entry("my-alias")])),
|
||||
requested_versions: Mutex::default(),
|
||||
middleware: None,
|
||||
});
|
||||
let response = api_router(execution.clone())
|
||||
.await
|
||||
.oneshot(authorized_request(
|
||||
"/v1/models?client_version=0.154.0-alpha.3",
|
||||
))
|
||||
.await
|
||||
.expect("response");
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
assert_eq!(response.headers()["cache-control"], "private, no-store");
|
||||
assert!(response.headers().get("etag").is_none());
|
||||
let body = to_bytes(response.into_body(), usize::MAX)
|
||||
.await
|
||||
.expect("body");
|
||||
let value: serde_json::Value = serde_json::from_slice(&body).expect("JSON");
|
||||
let mut expected = original.clone();
|
||||
if !allowed {
|
||||
expected["multi_agent_version"] = serde_json::json!("disabled");
|
||||
}
|
||||
assert_eq!(value["models"][0], expected);
|
||||
let mut alias = expected;
|
||||
alias["slug"] = serde_json::json!("my-alias");
|
||||
assert_eq!(value["models"][1], alias);
|
||||
assert_eq!(
|
||||
*execution.requested_versions.lock().expect("versions"),
|
||||
["0.154.0-alpha.3"]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
@@ -743,7 +743,7 @@ async fn request_context_should_resolve_forwarded_precedence_and_peer_fallback()
|
||||
operation_kind: OperationKind::Generate,
|
||||
input: Some(json!("hello")),
|
||||
client_metadata: None,
|
||||
// 客户端 User-Agent 仅用于本地展示,不再透传给上游指纹上下文
|
||||
// 客户端 User-Agent 仅用于本地展示
|
||||
protocol_context: Some(json!({"opaque_request_headers": [
|
||||
["cf-connecting-ip", STANDARD.encode(b"198.51.100.1")],
|
||||
["x-real-ip", STANDARD.encode(b"198.51.100.2")],
|
||||
@@ -1378,27 +1378,40 @@ async fn streaming_error_after_initial_keepalive_should_be_a_terminal_sse_failur
|
||||
}
|
||||
|
||||
#[tokio::test(start_paused = true)]
|
||||
async fn streaming_late_upstream_error_should_preserve_extended_error_fields() {
|
||||
async fn streaming_bad_request_preserves_extended_error_fields_before_and_after_keepalive() {
|
||||
let mut error = json!({
|
||||
"type": "invalid_request_error", "code": "misalignment_policy_violation",
|
||||
"message": "synthetic policy error", "misalignment": {"category": "synthetic"},
|
||||
"extension": {"opaque": true}
|
||||
});
|
||||
let failure = ProviderError::new(ProviderErrorKind::InvalidRequest, UpstreamSendState::Sent)
|
||||
.with_client_visible_upstream_response(
|
||||
ClientVisibleUpstreamResponse::new(
|
||||
400,
|
||||
Some(b"application/json".to_vec()),
|
||||
Bytes::from(json!({"error": error}).to_string()),
|
||||
let failure = || {
|
||||
ProviderError::new(ProviderErrorKind::InvalidRequest, UpstreamSendState::Sent)
|
||||
.with_client_visible_upstream_response(
|
||||
ClientVisibleUpstreamResponse::new(
|
||||
400,
|
||||
Some(b"application/json".to_vec()),
|
||||
Bytes::from(json!({"error": error}).to_string()),
|
||||
)
|
||||
.with_headers(headers(&[("retry-after", "7")])),
|
||||
)
|
||||
.with_headers(headers(&[("retry-after", "7")])),
|
||||
);
|
||||
error["headers"] = json!({"retry-after": "7"});
|
||||
};
|
||||
let immediate = FakeSession::streaming(
|
||||
Arc::new(Trace::default()),
|
||||
vec![NextStep::Error(EngineError::Provider(failure()))],
|
||||
);
|
||||
let response = stream_execution_response(Box::new(immediate), None).await;
|
||||
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
|
||||
assert_eq!(response.headers()[CONTENT_TYPE], "application/json");
|
||||
let bytes = to_bytes(response.into_body(), usize::MAX).await.unwrap();
|
||||
assert_eq!(
|
||||
serde_json::from_slice::<Value>(&bytes).unwrap(),
|
||||
json!({"error": error})
|
||||
);
|
||||
let session = FakeSession::streaming(
|
||||
Arc::new(Trace::default()),
|
||||
vec![NextStep::DelayedError(
|
||||
Duration::from_secs(30),
|
||||
EngineError::Provider(failure),
|
||||
EngineError::Provider(failure()),
|
||||
)],
|
||||
);
|
||||
let response = stream_execution_response(Box::new(session), None).await;
|
||||
@@ -1410,6 +1423,7 @@ async fn streaming_late_upstream_error_should_preserve_extended_error_fields() {
|
||||
.find(|event| event.event.as_deref() == Some("response.failed"))
|
||||
.unwrap();
|
||||
let payload: Value = serde_json::from_str(&failed.data).unwrap();
|
||||
error["headers"] = json!({"retry-after": "7"});
|
||||
assert_eq!(payload["response"]["error"], error);
|
||||
assert!(bytes.ends_with(b"data: [DONE]\n\n"));
|
||||
}
|
||||
@@ -2954,20 +2968,21 @@ struct SessionExecution {
|
||||
#[tokio::test]
|
||||
async fn official_core_clients_should_be_version_checked_before_starting_responses() {
|
||||
let admin = crate::admin::AdminTestFixture::new().await.services;
|
||||
for product in [
|
||||
"codex-tui",
|
||||
"codex_exec",
|
||||
"codex_vscode",
|
||||
"codex_sdk_ts",
|
||||
"codex_python_sdk",
|
||||
for (product, client) in [
|
||||
("codex-tui", "codex-tui"),
|
||||
("codex_exec", "codex_exec"),
|
||||
("codex_vscode", "codex_vscode"),
|
||||
("codex_sdk_ts", "codex_sdk_ts"),
|
||||
("codex_python_sdk", "codex_python_sdk"),
|
||||
("Codex Desktop", "codex-tui"),
|
||||
] {
|
||||
for (version, allowed) in [("0.158.0", false), ("0.162.0", true)] {
|
||||
for (version, allowed) in [("0.168.0", false), ("0.170.0", true)] {
|
||||
let trace = Arc::new(Trace::default());
|
||||
let execution = Arc::new(SessionExecution {
|
||||
client: authenticated_client_with_min_versions(
|
||||
"sk_version_test",
|
||||
None,
|
||||
Some("0.162.0"),
|
||||
Some("26.1023.51421"),
|
||||
Some("0.170.0"),
|
||||
),
|
||||
session: Mutex::new(Some(Box::new(FakeSession::streaming(
|
||||
Arc::clone(&trace),
|
||||
@@ -2985,7 +3000,7 @@ async fn official_core_clients_should_be_version_checked_before_starting_respons
|
||||
let request = Request::post("/v1/responses")
|
||||
.header(AUTHORIZATION, "Bearer sk_version_test")
|
||||
.header("user-agent", format!(
|
||||
"{product}/{version} (Mac OS 26.6.2; arm64) Apple_Terminal ({product}; 0.165.0)"
|
||||
"{product}/{version} (Ubuntu 26.4.0; aarch64) xterm-256color ({client}; 0.169.0)"
|
||||
))
|
||||
.body(Body::from(json!({"model": "model-a", "input": "synthetic", "stream": true}).to_string()))
|
||||
.unwrap();
|
||||
@@ -3006,8 +3021,9 @@ async fn official_core_clients_should_be_version_checked_before_starting_respons
|
||||
assert_eq!(status, StatusCode::BAD_REQUEST, "{product}/{version}");
|
||||
let value: Value = serde_json::from_slice(&body).unwrap();
|
||||
assert_eq!(value["error"]["code"], "client_version_too_old");
|
||||
assert_eq!(value["error"]["client"], "codex_cli");
|
||||
assert_eq!(value["error"]["current_version"], version);
|
||||
assert_eq!(value["error"]["min_version"], "0.162.0");
|
||||
assert_eq!(value["error"]["min_version"], "0.170.0");
|
||||
assert!(execution.session.lock().unwrap().is_some());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,215 @@ fn generate_request(body: Value) -> DecodedGenerationRequest {
|
||||
decode_request(body.to_string().as_bytes()).expect("test request should decode")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn guardian_roles_should_use_complete_current_request_identity_before_header_projection() {
|
||||
use gateway_protocol::openai::{CodexGuardianRole::*, codex_guardian_role};
|
||||
|
||||
let review = r#"{"thread_source":"guardian_review"}"#;
|
||||
let classification =
|
||||
r#"{"thread_source":"guardian_classifier","turn_trigger":"guardian_classifier"}"#;
|
||||
let housekeeping = r#"{"turn_trigger":"guardian_housekeeper"}"#;
|
||||
for (websocket, body_agent, body_turn, header_agent, header_turn, explicit, expected) in [
|
||||
(
|
||||
false,
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Some(Reviewer),
|
||||
),
|
||||
(
|
||||
false,
|
||||
None,
|
||||
None,
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
None,
|
||||
Some(Reviewer),
|
||||
),
|
||||
(
|
||||
false,
|
||||
None,
|
||||
Some(review),
|
||||
Some("guardian"),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
),
|
||||
(
|
||||
false,
|
||||
Some("other"),
|
||||
Some(review),
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
None,
|
||||
None,
|
||||
),
|
||||
(
|
||||
false,
|
||||
Some("guardian"),
|
||||
Some(classification),
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
None,
|
||||
None,
|
||||
),
|
||||
(
|
||||
false,
|
||||
Some("guardian"),
|
||||
Some("invalid"),
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
None,
|
||||
None,
|
||||
),
|
||||
(
|
||||
false,
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
None,
|
||||
None,
|
||||
Some("classifier"),
|
||||
None,
|
||||
),
|
||||
(false, None, None, None, None, Some("reviewer"), None),
|
||||
(
|
||||
false,
|
||||
Some("guardian"),
|
||||
Some(classification),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Some(Classifier),
|
||||
),
|
||||
(
|
||||
false,
|
||||
Some("guardian"),
|
||||
Some(housekeeping),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Some(Housekeeper),
|
||||
),
|
||||
(
|
||||
true,
|
||||
None,
|
||||
None,
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
Some("reviewer"),
|
||||
None,
|
||||
),
|
||||
(
|
||||
true,
|
||||
None,
|
||||
Some(review),
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
None,
|
||||
None,
|
||||
),
|
||||
(
|
||||
true,
|
||||
Some("guardian"),
|
||||
Some(classification),
|
||||
Some("stale_agent"),
|
||||
Some(review),
|
||||
Some("reviewer"),
|
||||
Some(Classifier),
|
||||
),
|
||||
(
|
||||
true,
|
||||
Some("guardian"),
|
||||
Some(review),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Some(Reviewer),
|
||||
),
|
||||
] {
|
||||
let mut body =
|
||||
json!({"model":"gpt-5.6-luna","input":[],"generate":false,"client_metadata":{}});
|
||||
if let Some(agent) = body_agent {
|
||||
body["client_metadata"]["x-openai-subagent"] = json!(agent);
|
||||
}
|
||||
if let Some(turn) = body_turn {
|
||||
body["client_metadata"]["x-codex-turn-metadata"] = json!(turn);
|
||||
}
|
||||
let mut headers = HeaderMap::new();
|
||||
for (name, value) in [
|
||||
("x-openai-subagent", header_agent),
|
||||
("x-codex-turn-metadata", header_turn),
|
||||
("x-codex-guardian", explicit),
|
||||
] {
|
||||
if let Some(value) = value {
|
||||
headers.insert(name, HeaderValue::from_str(value).unwrap());
|
||||
}
|
||||
}
|
||||
let decoded = if websocket {
|
||||
body["type"] = json!("response.create");
|
||||
decode_response_create_with_context(
|
||||
&body.to_string(),
|
||||
&OpenAiRequestHeaders::from_headers(&headers),
|
||||
)
|
||||
.unwrap()
|
||||
} else {
|
||||
decode_request_with_headers(body.to_string().as_bytes(), &headers, 64 * 1024).unwrap()
|
||||
};
|
||||
let payload = generate_operation(&decoded).protocol_payload();
|
||||
if !websocket && matches!(expected, Some(Classifier | Housekeeper)) {
|
||||
assert!(!payload.context().contains_key("use_websocket"));
|
||||
assert!(
|
||||
!payload
|
||||
.context()
|
||||
.contains_key(gateway_protocol::openai::WEBSOCKET_FRAME_CONTEXT_KEY)
|
||||
);
|
||||
}
|
||||
if websocket && header_agent == Some("stale_agent") {
|
||||
assert_eq!(
|
||||
payload.body()["client_metadata"]["x-openai-subagent"],
|
||||
"guardian"
|
||||
);
|
||||
let opaque = payload.context()["opaque_request_headers"]
|
||||
.as_array()
|
||||
.unwrap();
|
||||
assert!(!opaque.iter().any(|pair| matches!(
|
||||
pair[0].as_str(),
|
||||
Some("x-codex-guardian" | "x-openai-subagent")
|
||||
)));
|
||||
}
|
||||
assert_eq!(
|
||||
codex_guardian_role(payload.body(), payload.context()),
|
||||
expected,
|
||||
"body={body}, headers={headers:?}"
|
||||
);
|
||||
}
|
||||
let mut repeated = HeaderMap::new();
|
||||
repeated.append("x-openai-subagent", HeaderValue::from_static("guardian"));
|
||||
repeated.append("x-openai-subagent", HeaderValue::from_static("other"));
|
||||
repeated.insert("x-codex-turn-metadata", HeaderValue::from_static(review));
|
||||
let decoded = decode_request_with_headers(
|
||||
br#"{"model":"gpt-5.6-luna","input":[]}"#,
|
||||
&repeated,
|
||||
64 * 1024,
|
||||
)
|
||||
.unwrap();
|
||||
let payload = generate_operation(&decoded).protocol_payload();
|
||||
assert_eq!(codex_guardian_role(payload.body(), payload.context()), None);
|
||||
for malformed in [
|
||||
r#"{"thread_source":"guardian_classifier","thread_source":"guardian_review"}"#,
|
||||
r#"{"thread_source":null}"#,
|
||||
r#"["guardian_review"]"#,
|
||||
] {
|
||||
let mut body = json!({"model":"gpt-5.6-luna","input":[],"client_metadata":{"x-openai-subagent":"guardian"}});
|
||||
body["client_metadata"]["x-codex-turn-metadata"] = json!(malformed);
|
||||
let decoded = generate_request(body);
|
||||
let payload = generate_operation(&decoded).protocol_payload();
|
||||
assert_eq!(codex_guardian_role(payload.body(), payload.context()), None);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decoder_should_attach_only_identified_codex_inference_turns() {
|
||||
let body = json!({"model":"smart-code", "input":"hello", "client_metadata": {
|
||||
@@ -492,7 +701,7 @@ fn decoder_should_strip_http_transport_but_leave_source_headers_for_provider() {
|
||||
HeaderValue::from_static(r#"{"scheme":"https"}"#),
|
||||
);
|
||||
headers.insert("accept-encoding", HeaderValue::from_static("br, gzip"));
|
||||
// 正文未压缩;入口现在消费 Content-Encoding,但仍不得向上游透传
|
||||
// identity 与未压缩的正文匹配,Content-Encoding 由入口消费
|
||||
headers.insert("content-encoding", HeaderValue::from_static("identity"));
|
||||
headers.insert("x-openai-future-mode", HeaderValue::from_static("keep"));
|
||||
|
||||
|
||||
+1
-1
@@ -358,7 +358,7 @@ async fn rewrite_upgrade_and_control_active_response_keep_http_context_until_con
|
||||
);
|
||||
let (trace, mut socket, server) =
|
||||
start_active_response_with(Some(frozen), "/custom/websocket", "codex-tui/0.166.0").await;
|
||||
// 升级后的默认会话仍持有入口上下文;101 正文结束不再提前取消它
|
||||
// 101 响应完成后,WebSocket 会话继续持有入口上下文
|
||||
assert!(
|
||||
!plan
|
||||
.http_cancellation
|
||||
|
||||
@@ -255,7 +255,7 @@ impl Stream for TestSocket {
|
||||
|
||||
fn poll_next(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let event = self.incoming.poll_recv(context);
|
||||
// 模拟 Axum transport 的自动 Pong;pump 不再重复实现协议层应答
|
||||
// 模拟 Axum 传输层收到 Ping 时自动回复 Pong
|
||||
if let Poll::Ready(Some(Ok(Message::Ping(payload)))) = &event {
|
||||
self.written
|
||||
.send(Message::Pong(payload.clone()))
|
||||
|
||||
@@ -45,6 +45,14 @@ impl Drop for TestServer {
|
||||
async fn connect(
|
||||
trace: Arc<AtomicFailureTrace>,
|
||||
response_headers: Vec<ProviderResponseHeader>,
|
||||
) -> (TestSocket, TestServer) {
|
||||
connect_with_headers(trace, response_headers, axum::http::HeaderMap::new()).await
|
||||
}
|
||||
|
||||
async fn connect_with_headers(
|
||||
trace: Arc<AtomicFailureTrace>,
|
||||
response_headers: Vec<ProviderResponseHeader>,
|
||||
opening_headers: axum::http::HeaderMap,
|
||||
) -> (TestSocket, TestServer) {
|
||||
let execution = Arc::new(AtomicFailureExecution {
|
||||
client: authenticated_client("sk_ws_atomic"),
|
||||
@@ -66,6 +74,7 @@ async fn connect(
|
||||
request
|
||||
.headers_mut()
|
||||
.insert(AUTHORIZATION, "Bearer sk_ws_atomic".parse().unwrap());
|
||||
request.headers_mut().extend(opening_headers);
|
||||
let (socket, response) = connect_async(request).await.expect("upgrade WebSocket");
|
||||
assert_eq!(response.status(), StatusCode::SWITCHING_PROTOCOLS);
|
||||
(socket, server)
|
||||
@@ -518,13 +527,42 @@ async fn errors_on_a_reused_connection_keep_their_own_upstream_ids() {
|
||||
initial_errors: Mutex::new(VecDeque::from(errors)),
|
||||
..AtomicFailureTrace::default()
|
||||
});
|
||||
let (mut socket, _server) = connect(trace, Vec::new()).await;
|
||||
let mut opening = axum::http::HeaderMap::new();
|
||||
opening.insert(
|
||||
"x-codex-turn-state",
|
||||
axum::http::HeaderValue::from_static("opening"),
|
||||
);
|
||||
opening.insert(
|
||||
"x-codex-turn-metadata",
|
||||
axum::http::HeaderValue::from_static(r#"{"request_kind":"turn"}"#),
|
||||
);
|
||||
opening.insert(
|
||||
"x-openai-subagent",
|
||||
axum::http::HeaderValue::from_static("guardian"),
|
||||
);
|
||||
opening.insert(
|
||||
"x-codex-guardian",
|
||||
axum::http::HeaderValue::from_static("reviewer"),
|
||||
);
|
||||
let (mut socket, _server) = connect_with_headers(Arc::clone(&trace), Vec::new(), opening).await;
|
||||
for request_id in ["req_first", "req_second"] {
|
||||
send_request(&mut socket).await;
|
||||
let error = next_error(&mut socket).await;
|
||||
assert_eq!(error["headers"]["x-request-id"], request_id);
|
||||
assert_eq!(error["headers"]["x-gateway-request-id"], "req_ws_atomic");
|
||||
}
|
||||
assert_eq!(
|
||||
*trace.request_turn_headers.lock().unwrap(),
|
||||
[
|
||||
[
|
||||
Some("opening".to_owned()),
|
||||
Some(r#"{"request_kind":"turn"}"#.to_owned()),
|
||||
Some("guardian".to_owned()),
|
||||
Some("incomplete".to_owned()),
|
||||
],
|
||||
[None, None, None, None]
|
||||
]
|
||||
);
|
||||
assert_no_duplicate_failure(&mut socket).await;
|
||||
socket.close(None).await.unwrap();
|
||||
}
|
||||
|
||||
@@ -168,21 +168,31 @@ fn response_create_should_preserve_compaction_trigger_for_openai() {
|
||||
|
||||
#[test]
|
||||
fn response_create_should_prefer_frame_turn_metadata_over_opening_headers() {
|
||||
let frame = r#"{"request_kind":"turn","tool_namespaces_info":{"workspace":{}}}"#;
|
||||
let opening = r#"{"request_kind":"compaction"}"#;
|
||||
let decoded = decode_response_create_with_turn_header(
|
||||
json!({
|
||||
"type": "response.create",
|
||||
"model": "smart-code",
|
||||
"input": "hello",
|
||||
"client_metadata": {
|
||||
"x-codex-turn-metadata": r#"{"request_kind":"turn"}"#
|
||||
"x-codex-turn-metadata": frame
|
||||
}
|
||||
}),
|
||||
r#"{"request_kind":"compaction"}"#,
|
||||
opening,
|
||||
);
|
||||
let Operation::Generate(request) = decoded.operation() else {
|
||||
panic!("Responses must map to Generate");
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
request.protocol_payload().context()["turn_metadata"],
|
||||
opening
|
||||
);
|
||||
assert_eq!(
|
||||
request.protocol_payload().body()["client_metadata"]["x-codex-turn-metadata"],
|
||||
frame
|
||||
);
|
||||
let semantics = codex_responses_request_semantics(
|
||||
request.protocol_payload().body(),
|
||||
request.protocol_payload().context(),
|
||||
@@ -308,6 +318,7 @@ struct AtomicFailureTrace {
|
||||
committed: AtomicBool,
|
||||
finalized: AtomicBool,
|
||||
downstream_websocket_connection_ids: Mutex<Vec<String>>,
|
||||
request_turn_headers: Mutex<Vec<[Option<String>; 4]>>,
|
||||
initial_errors: Mutex<std::collections::VecDeque<EngineError>>,
|
||||
first_batch: Mutex<Option<CoordinatedEvent>>,
|
||||
session_drops: Arc<AtomicUsize>,
|
||||
@@ -437,6 +448,16 @@ impl ExecutionService for AtomicFailureExecution {
|
||||
.get("downstream_websocket_connection_id")
|
||||
.and_then(Value::as_str)
|
||||
{
|
||||
let context = generate.protocol_payload().context();
|
||||
self.trace.request_turn_headers.lock().unwrap().push(
|
||||
[
|
||||
"turn_state",
|
||||
"turn_metadata",
|
||||
"subagent",
|
||||
"codex_guardian_identity",
|
||||
]
|
||||
.map(|key| context.get(key).and_then(Value::as_str).map(str::to_owned)),
|
||||
);
|
||||
self.trace
|
||||
.downstream_websocket_connection_ids
|
||||
.lock()
|
||||
@@ -1211,12 +1232,16 @@ fn upgrade_request(authorization: &str) -> Request<Body> {
|
||||
|
||||
#[tokio::test]
|
||||
async fn official_core_version_gate_should_run_before_websocket_upgrade() {
|
||||
let app = api_router(ModelsExecution::with_cli_min("0.162.0")).await;
|
||||
let app = api_router(ModelsExecution::with_cli_min("0.170.0")).await;
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
|
||||
|
||||
for version in ["0.158.0", "0.162.0"] {
|
||||
for (product, version) in [
|
||||
("codex-tui", "0.158.0"),
|
||||
("Codex Desktop", "0.168.0"),
|
||||
("Codex Desktop", "0.170.0"),
|
||||
] {
|
||||
let mut request = format!("ws://{address}/v1/responses")
|
||||
.into_client_request()
|
||||
.unwrap();
|
||||
@@ -1227,12 +1252,12 @@ async fn official_core_version_gate_should_run_before_websocket_upgrade() {
|
||||
request.headers_mut().insert(
|
||||
"user-agent",
|
||||
HeaderValue::from_str(&format!(
|
||||
"codex-tui/{version} (Mac OS 26.6.2; arm64) Apple_Terminal (codex-tui; 0.165.0)"
|
||||
"{product}/{version} (Ubuntu 26.4.0; aarch64) xterm-256color (codex-tui; 0.169.0)"
|
||||
))
|
||||
.unwrap(),
|
||||
);
|
||||
let result = connect_async(request).await;
|
||||
if version == "0.162.0" {
|
||||
if version == "0.170.0" {
|
||||
let (mut socket, response) = result.expect("current Core upgrades successfully");
|
||||
assert_eq!(response.status(), StatusCode::SWITCHING_PROTOCOLS);
|
||||
socket.close(None).await.unwrap();
|
||||
@@ -1245,8 +1270,9 @@ async fn official_core_version_gate_should_run_before_websocket_upgrade() {
|
||||
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
|
||||
let value: Value = serde_json::from_slice(response.body().as_deref().unwrap()).unwrap();
|
||||
assert_eq!(value["error"]["code"], "client_version_too_old");
|
||||
assert_eq!(value["error"]["current_version"], "0.158.0");
|
||||
assert_eq!(value["error"]["min_version"], "0.162.0");
|
||||
assert_eq!(value["error"]["client"], "codex_cli");
|
||||
assert_eq!(value["error"]["current_version"], version);
|
||||
assert_eq!(value["error"]["min_version"], "0.170.0");
|
||||
}
|
||||
}
|
||||
server.abort();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! 账号分组的 Fast 三态策略与多分组优先级
|
||||
//! 账号分组的快速模式策略与多分组优先级
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -10,6 +10,7 @@ pub enum FastMode {
|
||||
Default,
|
||||
Enabled,
|
||||
Disabled,
|
||||
Ultrafast,
|
||||
}
|
||||
|
||||
impl FastMode {
|
||||
@@ -18,6 +19,7 @@ impl FastMode {
|
||||
match self {
|
||||
Self::Default => "default",
|
||||
Self::Enabled => "enabled",
|
||||
Self::Ultrafast => "ultrafast",
|
||||
Self::Disabled => "disabled",
|
||||
}
|
||||
}
|
||||
@@ -27,17 +29,19 @@ impl FastMode {
|
||||
match value {
|
||||
"default" => Some(Self::Default),
|
||||
"enabled" => Some(Self::Enabled),
|
||||
"ultrafast" => Some(Self::Ultrafast),
|
||||
"disabled" => Some(Self::Disabled),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// 多分组冲突时关闭优先于开启,默认不覆盖其他分组
|
||||
/// 多分组冲突时禁用优先;同时强制两种加速档位时选用较低的 Fast
|
||||
#[must_use]
|
||||
pub const fn merge(self, other: Self) -> Self {
|
||||
match (self, other) {
|
||||
(Self::Disabled, _) | (_, Self::Disabled) => Self::Disabled,
|
||||
(Self::Enabled, _) | (_, Self::Enabled) => Self::Enabled,
|
||||
(Self::Ultrafast, _) | (_, Self::Ultrafast) => Self::Ultrafast,
|
||||
_ => Self::Default,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,31 @@ pub struct RequestLocation {
|
||||
pub timezone: chrono_tz::Tz,
|
||||
}
|
||||
|
||||
/// 独立选择 OpenAI 请求的国家字段,仅自定义模式需要国家代码
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
|
||||
#[serde(tag = "mode", rename_all = "snake_case", deny_unknown_fields)]
|
||||
pub enum OpenAiRequestCountry {
|
||||
Default {},
|
||||
Passthrough {},
|
||||
FollowLocation {},
|
||||
Custom { country: String },
|
||||
}
|
||||
|
||||
impl Default for OpenAiRequestCountry {
|
||||
fn default() -> Self {
|
||||
Self::FollowLocation {}
|
||||
}
|
||||
}
|
||||
|
||||
impl OpenAiRequestCountry {
|
||||
pub fn validate(&self) -> Result<(), InvalidRequestLocation> {
|
||||
if let Self::Custom { country } = self {
|
||||
validate_country(country)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum InvalidRequestLocation {
|
||||
#[error("country must contain two uppercase ASCII letters")]
|
||||
@@ -34,9 +59,7 @@ impl Default for RequestLocation {
|
||||
|
||||
impl RequestLocation {
|
||||
pub fn validate(&self) -> Result<(), InvalidRequestLocation> {
|
||||
if self.country.len() != 2 || !self.country.bytes().all(|byte| byte.is_ascii_uppercase()) {
|
||||
return Err(InvalidRequestLocation::Country);
|
||||
}
|
||||
validate_country(&self.country)?;
|
||||
for (value, error) in [
|
||||
(&self.region, InvalidRequestLocation::Region),
|
||||
(&self.city, InvalidRequestLocation::City),
|
||||
@@ -59,3 +82,11 @@ impl RequestLocation {
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_country(country: &str) -> Result<(), InvalidRequestLocation> {
|
||||
if country.len() == 2 && country.bytes().all(|byte| byte.is_ascii_uppercase()) {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(InvalidRequestLocation::Country)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ pub use error_protection::*;
|
||||
mod fast_mode;
|
||||
pub use fast_mode::FastMode;
|
||||
mod location;
|
||||
pub use location::{InvalidRequestLocation, RequestLocation};
|
||||
pub use location::{InvalidRequestLocation, OpenAiRequestCountry, RequestLocation};
|
||||
mod model;
|
||||
mod model_access;
|
||||
pub use model_access::{
|
||||
|
||||
@@ -1292,6 +1292,7 @@ pub struct CredentialCasUpdateParts {
|
||||
pub expected_revision: CredentialRevision,
|
||||
pub profile: ProviderAccountUpdate,
|
||||
pub preserve_profile: bool,
|
||||
pub preserve_access_token_expiry: bool,
|
||||
pub credential: PlaintextCredential,
|
||||
pub has_refresh_token: bool,
|
||||
pub access_token_expires_at: Option<SystemTime>,
|
||||
@@ -1306,6 +1307,7 @@ pub struct CredentialCasUpdate {
|
||||
expected_revision: CredentialRevision,
|
||||
profile: ProviderAccountUpdate,
|
||||
preserve_profile: bool,
|
||||
preserve_access_token_expiry: bool,
|
||||
credential: PlaintextCredential,
|
||||
has_refresh_token: bool,
|
||||
access_token_expires_at: Option<SystemTime>,
|
||||
@@ -1321,6 +1323,10 @@ impl fmt::Debug for CredentialCasUpdate {
|
||||
.field("expected_revision", &self.expected_revision)
|
||||
.field("profile", &self.profile)
|
||||
.field("preserve_profile", &self.preserve_profile)
|
||||
.field(
|
||||
"preserve_access_token_expiry",
|
||||
&self.preserve_access_token_expiry,
|
||||
)
|
||||
.field("credential", &self.credential)
|
||||
.field("has_refresh_token", &self.has_refresh_token)
|
||||
.field("access_token_expires_at", &self.access_token_expires_at)
|
||||
@@ -1356,6 +1362,7 @@ impl CredentialCasUpdate {
|
||||
expected_revision,
|
||||
profile,
|
||||
preserve_profile: false,
|
||||
preserve_access_token_expiry: false,
|
||||
credential,
|
||||
has_refresh_token,
|
||||
access_token_expires_at,
|
||||
@@ -1371,6 +1378,13 @@ impl CredentialCasUpdate {
|
||||
self
|
||||
}
|
||||
|
||||
/// 沿用同一访问令牌时,保留提交时的有效期,避免覆盖在途收到的撤销事实
|
||||
#[must_use]
|
||||
pub const fn preserving_access_token_expiry(mut self) -> Self {
|
||||
self.preserve_access_token_expiry = true;
|
||||
self
|
||||
}
|
||||
|
||||
/// 将刷新调度与账号错误事实放入同一个 revision CAS
|
||||
#[must_use]
|
||||
pub fn with_account_state(
|
||||
@@ -1437,6 +1451,7 @@ impl CredentialCasUpdate {
|
||||
expected_revision: self.expected_revision,
|
||||
profile: self.profile,
|
||||
preserve_profile: self.preserve_profile,
|
||||
preserve_access_token_expiry: self.preserve_access_token_expiry,
|
||||
credential: self.credential,
|
||||
has_refresh_token: self.has_refresh_token,
|
||||
access_token_expires_at: self.access_token_expires_at,
|
||||
|
||||
@@ -88,6 +88,12 @@ impl AccountModelAccess {
|
||||
AccountModelAccessMode::Denylist => !self.models.contains(upstream_model),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn allows_optional_model(&self, upstream_model: Option<&str>) -> bool {
|
||||
upstream_model.map_or(self.mode == AccountModelAccessMode::All, |model| {
|
||||
self.allows(model)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! 请求认证时冻结的账号范围与目录,不依赖路由选择器
|
||||
|
||||
use super::{FastMode, ProviderAccountId};
|
||||
use super::{AccountModelAccess, FastMode, ProviderAccountId};
|
||||
use crate::identity::ProviderKind;
|
||||
use crate::validation::{IdentifierError, RoutingError};
|
||||
use std::{
|
||||
@@ -107,6 +107,7 @@ pub struct RuntimeAccountDirectory {
|
||||
accounts: BTreeMap<ProviderAccountId, RuntimeAccount>,
|
||||
providers_with_accounts: BTreeSet<ProviderKind>,
|
||||
providers_by_group: BTreeMap<AccountGroupId, BTreeSet<ProviderKind>>,
|
||||
group_model_access: BTreeMap<AccountGroupId, AccountModelAccess>,
|
||||
}
|
||||
|
||||
impl RuntimeAccountDirectory {
|
||||
@@ -127,9 +128,28 @@ impl RuntimeAccountDirectory {
|
||||
accounts,
|
||||
providers_with_accounts,
|
||||
providers_by_group,
|
||||
group_model_access: BTreeMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn with_group_model_access(
|
||||
mut self,
|
||||
policies: BTreeMap<AccountGroupId, AccountModelAccess>,
|
||||
) -> Self {
|
||||
self.group_model_access = policies;
|
||||
self
|
||||
}
|
||||
|
||||
pub(crate) fn accounts_in_group(
|
||||
&self,
|
||||
group: &AccountGroupId,
|
||||
) -> impl Iterator<Item = (&ProviderAccountId, &RuntimeAccount)> {
|
||||
self.accounts
|
||||
.iter()
|
||||
.filter(move |(_, account)| account.group_ids.contains(group))
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn account(&self, account_id: &ProviderAccountId) -> Option<&RuntimeAccount> {
|
||||
self.accounts.get(account_id)
|
||||
@@ -267,6 +287,7 @@ impl ClientRoutingScope {
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct FrozenAccountScope {
|
||||
allow_image_generation: bool,
|
||||
allow_subagents: bool,
|
||||
fast_mode: FastMode,
|
||||
request_profiles: BTreeMap<ProviderKind, super::OpaqueProviderData>,
|
||||
directory: Arc<RuntimeAccountDirectory>,
|
||||
@@ -303,6 +324,11 @@ impl FrozenAccountScope {
|
||||
self.allow_image_generation
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn allows_subagents(&self) -> bool {
|
||||
self.allow_subagents
|
||||
}
|
||||
|
||||
/// 与 Key 授权范围一同冻结;具体字段只由对应 Provider 解释
|
||||
#[must_use]
|
||||
pub fn with_request_profiles(
|
||||
@@ -323,6 +349,12 @@ impl FrozenAccountScope {
|
||||
&self.request_profiles
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn with_subagents(mut self, allowed: bool) -> Self {
|
||||
self.allow_subagents = allowed;
|
||||
self
|
||||
}
|
||||
|
||||
/// Key 绑定分组的冻结 Fast 策略,与账号成员资格无关
|
||||
#[must_use]
|
||||
pub const fn with_fast_mode(mut self, fast_mode: FastMode) -> Self {
|
||||
@@ -343,6 +375,7 @@ impl FrozenAccountScope {
|
||||
};
|
||||
Self {
|
||||
allow_image_generation: true,
|
||||
allow_subagents: true,
|
||||
fast_mode: FastMode::Default,
|
||||
request_profiles: BTreeMap::new(),
|
||||
directory,
|
||||
@@ -399,6 +432,14 @@ impl FrozenAccountScope {
|
||||
|
||||
#[must_use]
|
||||
pub fn allows(&self, account_id: &ProviderAccountId) -> bool {
|
||||
self.allows_with_policy(account_id, |_| true)
|
||||
}
|
||||
|
||||
fn allows_with_policy(
|
||||
&self,
|
||||
account_id: &ProviderAccountId,
|
||||
accepts: impl Fn(&AccountModelAccess) -> bool,
|
||||
) -> bool {
|
||||
let Some(account) = self.directory.account(account_id) else {
|
||||
return false;
|
||||
};
|
||||
@@ -408,6 +449,7 @@ impl FrozenAccountScope {
|
||||
.allowed_account_ids
|
||||
.as_ref()
|
||||
.is_some_and(|accounts| !accounts.contains(account_id))
|
||||
|| !accepts(account.model_access())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -417,18 +459,43 @@ impl FrozenAccountScope {
|
||||
enabled_group_ids, ..
|
||||
} => account
|
||||
.group_ids()
|
||||
.iter()
|
||||
.any(|group_id| enabled_group_ids.contains(group_id)),
|
||||
.intersection(enabled_group_ids)
|
||||
.any(|id| {
|
||||
self.directory
|
||||
.group_model_access
|
||||
.get(id)
|
||||
.is_none_or(&accepts)
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn allows_model(&self, account_id: &ProviderAccountId, upstream_model: &str) -> bool {
|
||||
self.allows(account_id)
|
||||
&& self
|
||||
.directory
|
||||
.account(account_id)
|
||||
.is_some_and(|account| account.model_access.allows(upstream_model))
|
||||
self.allows_with_policy(account_id, |access| access.allows(upstream_model))
|
||||
}
|
||||
|
||||
/// 不猜测原生端点的默认模型,账号与授权它的分组均须允许任意模型
|
||||
#[must_use]
|
||||
pub fn allows_unidentified_model(&self, account_id: &ProviderAccountId) -> bool {
|
||||
self.allows_with_policy(account_id, |access| access.allows_optional_model(None))
|
||||
}
|
||||
|
||||
/// 只报告所有已启用绑定分组均明确拒绝模型的情况,空池沿用原路由错误
|
||||
#[must_use]
|
||||
pub fn groups_deny_model(&self, upstream_model: Option<&str>) -> bool {
|
||||
let ClientRoutingScope::Restricted {
|
||||
enabled_group_ids, ..
|
||||
} = &self.client_scope
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
!enabled_group_ids.is_empty()
|
||||
&& enabled_group_ids.iter().all(|id| {
|
||||
self.directory
|
||||
.group_model_access
|
||||
.get(id)
|
||||
.is_some_and(|access| !access.allows_optional_model(upstream_model))
|
||||
})
|
||||
}
|
||||
|
||||
/// 目录按整个授权账号池过滤,不能只看用于获取元数据的账号政策
|
||||
|
||||
@@ -173,6 +173,15 @@ pub trait ProviderAccountStore: Send + Sync {
|
||||
change: QuotaAccessChange,
|
||||
) -> Result<QuotaWriteOutcome, StoreError>;
|
||||
|
||||
/// 按请求使用的凭据版本缩短访问令牌有效期,保留在途续期的版本和重试计划
|
||||
async fn expire_access_token(
|
||||
&self,
|
||||
_account: &ProviderAccount,
|
||||
_observed_at: std::time::SystemTime,
|
||||
) -> Result<bool, StoreError> {
|
||||
Err(StoreError::new(crate::error::StoreErrorKind::InvalidData))
|
||||
}
|
||||
|
||||
async fn apply_state_change(&self, change: AccountStateChange) -> Result<(), StoreError>;
|
||||
|
||||
async fn update_account(&self, update: ProviderAccountUpdate) -> Result<(), StoreError>;
|
||||
|
||||
@@ -37,7 +37,7 @@ use crate::event::{
|
||||
use crate::lifecycle::{CancellationToken, Deadline, LeaseGuard};
|
||||
use crate::metering::Decimal;
|
||||
use crate::operation::{Operation, ProviderSessionState};
|
||||
use crate::routing::RoutingPlan;
|
||||
use crate::routing::{ProviderCandidate, RoutingPlan};
|
||||
use futures::future::{BoxFuture, Fuse};
|
||||
use futures::{FutureExt, StreamExt, pin_mut, select_biased};
|
||||
use futures_timer::Delay;
|
||||
@@ -1171,6 +1171,7 @@ where
|
||||
.with_fast_mode(self.plan.fast_mode())
|
||||
.with_pricing(self.plan.pricing())
|
||||
.with_request_location(self.plan.request_location().cloned())
|
||||
.with_openai_request_country(self.plan.openai_request_country().clone())
|
||||
.with_privacy(self.plan.privacy())
|
||||
.with_request_body_limit(self.plan.request_body_limit())
|
||||
.with_concurrency_wait_budget(self.concurrency_wait_budget.clone())
|
||||
@@ -1239,142 +1240,11 @@ where
|
||||
}
|
||||
ProviderBoundary::Result(result) => match *result {
|
||||
Ok(stream) => stream,
|
||||
Err(mut error) => {
|
||||
attempt_trace.record_provider_failure(&error);
|
||||
if self.continuation_attempt == ContinuationAttempt::RotateForClientReplay
|
||||
&& error.continuation_recovery_disposition()
|
||||
== Some(ContinuationRecoveryDisposition::ClientReplayRequired)
|
||||
&& let Some(original) = self.last_retryable_failure.take()
|
||||
{
|
||||
// 迁移只改变客户端恢复指令;失败分类、发送事实与运维正文仍属于原请求
|
||||
error = original.with_client_recovery_from(error);
|
||||
self.last_retryable_failure_events.clear();
|
||||
}
|
||||
let connection_fallback = self.connection_rotation.is_some()
|
||||
&& self
|
||||
.last_retryable_failure
|
||||
.as_ref()
|
||||
.and_then(ProviderError::unreachable_proxy)
|
||||
.is_none()
|
||||
&& !self.connection_budget.exhausted()
|
||||
&& !error.retry_is_prohibited()
|
||||
&& error.send_state() == UpstreamSendState::NotSent
|
||||
&& matches!(
|
||||
error.kind(),
|
||||
ProviderErrorKind::NoEligibleAccount
|
||||
| ProviderErrorKind::AccountCapacityUnavailable
|
||||
| ProviderErrorKind::QuotaExhausted
|
||||
| ProviderErrorKind::ConcurrencyQueueFull
|
||||
| ProviderErrorKind::ConcurrencyQueueTimeout
|
||||
);
|
||||
let continuation_retry = !error.retry_is_prohibited()
|
||||
&& self.prepare_unavailable_native_continuation_replay(&error);
|
||||
let candidate_retry = !error.retry_is_prohibited() && !connection_fallback && !continuation_retry
|
||||
&& matches!(
|
||||
error.kind(),
|
||||
ProviderErrorKind::AccountCapacityUnavailable
|
||||
| ProviderErrorKind::NoEligibleAccount
|
||||
| ProviderErrorKind::QuotaExhausted
|
||||
| ProviderErrorKind::ProviderInfrastructureUnavailable
|
||||
| ProviderErrorKind::ConcurrencyQueueFull
|
||||
| ProviderErrorKind::ConcurrencyQueueTimeout
|
||||
)
|
||||
&& error.send_state() == UpstreamSendState::NotSent
|
||||
&& self.current_send_state() == UpstreamSendState::NotSent
|
||||
&& matches!(
|
||||
self.continuation_attempt,
|
||||
ContinuationAttempt::None | ContinuationAttempt::ReplayAny
|
||||
)
|
||||
// 跨 Provider 候选推进必然换号(账号行按 Provider 隔离),
|
||||
// 预算耗尽后不再推进,交回容量类失败的原有终态语义。
|
||||
&& self.has_account_rotation_budget()
|
||||
&& self.advance_provider_candidate();
|
||||
let retryable = self
|
||||
.apply_retry_policy(super::policy::RetryFacts {
|
||||
attempt_index: next_attempt,
|
||||
provider: candidate.provider().clone(),
|
||||
model: candidate
|
||||
.upstream_model()
|
||||
.map(|model| model.as_str().to_owned()),
|
||||
error_kind: error.kind(),
|
||||
upstream_status: error.upstream_status(),
|
||||
send_state: error.send_state(),
|
||||
remaining_routing_attempts: self
|
||||
.plan
|
||||
.max_attempts()
|
||||
.get()
|
||||
.saturating_sub(self.routing_attempts),
|
||||
remaining_deadline: Duration::ZERO,
|
||||
retry_allowed: connection_fallback
|
||||
|| continuation_retry
|
||||
|| candidate_retry,
|
||||
})
|
||||
.await?;
|
||||
attempt_trace.record("retry.decided", json!({
|
||||
"phase": "prepare", "retryable": retryable,
|
||||
"continuationRetry": continuation_retry, "candidateRetry": candidate_retry,
|
||||
"connectionFallback": connection_fallback,
|
||||
"retryProhibited": error.retry_is_prohibited(),
|
||||
"sendState": error.send_state().as_str(),
|
||||
"requestSendState": self.current_send_state().as_str(),
|
||||
"continuation": format!("{:?}", self.continuation_attempt),
|
||||
"accountRotations": self.account_rotations,
|
||||
"maxAccountRotations": self.plan.account_selection_policy().max_account_rotations(),
|
||||
"remainingRoutingAttempts": self.plan.max_attempts().get().saturating_sub(self.routing_attempts),
|
||||
}));
|
||||
if retryable {
|
||||
if connection_fallback {
|
||||
let recovery = self
|
||||
.connection_rotation
|
||||
.take()
|
||||
.expect("pending connection rotation");
|
||||
self.excluded_accounts.remove(&recovery.account);
|
||||
self.pending_retry = Some(recovery);
|
||||
} else if candidate_retry {
|
||||
// 原账号只属于原 Provider,跨候选后不能拿旧钉选作恢复兜底
|
||||
self.connection_rotation = None;
|
||||
}
|
||||
return Ok(Some(PullOutcome::AttemptDiscarded));
|
||||
}
|
||||
if matches!(
|
||||
error.kind(),
|
||||
ProviderErrorKind::AccountCapacityUnavailable
|
||||
| ProviderErrorKind::NoEligibleAccount
|
||||
| ProviderErrorKind::SessionPolicyRestricted
|
||||
| ProviderErrorKind::QuotaExhausted
|
||||
| ProviderErrorKind::ConcurrencyQueueFull
|
||||
| ProviderErrorKind::ConcurrencyQueueTimeout
|
||||
) && let Some(last_failure) = self.last_retryable_failure.take()
|
||||
{
|
||||
let send_state = self.current_send_state();
|
||||
let mut events = std::mem::take(&mut self.last_retryable_failure_events);
|
||||
if !events.is_empty() {
|
||||
self.observe_atomic_terminal_events(&mut events);
|
||||
self.budget_attempt_already_counted = true;
|
||||
return Ok(Some(PullOutcome::TerminalFailure {
|
||||
events,
|
||||
error: last_failure,
|
||||
send_state,
|
||||
}));
|
||||
}
|
||||
self.finish_provider_error(&last_failure).await?;
|
||||
return Err(provider_engine_error(last_failure));
|
||||
}
|
||||
if !(matches!(
|
||||
error.kind(),
|
||||
ProviderErrorKind::AccountCapacityUnavailable
|
||||
| ProviderErrorKind::NoEligibleAccount
|
||||
| ProviderErrorKind::SessionPolicyRestricted
|
||||
| ProviderErrorKind::QuotaExhausted
|
||||
| ProviderErrorKind::ProviderInfrastructureUnavailable
|
||||
| ProviderErrorKind::ConcurrencyQueueFull
|
||||
| ProviderErrorKind::ConcurrencyQueueTimeout
|
||||
) && error.send_state() == UpstreamSendState::NotSent)
|
||||
{
|
||||
self.record_provider_failure(candidate.provider().clone());
|
||||
}
|
||||
self.finish_provider_error(&error).await?;
|
||||
return Err(provider_engine_error(error));
|
||||
Err(error) => {
|
||||
return self
|
||||
.handle_prepare_failure(error, &candidate, next_attempt, &attempt_trace)
|
||||
.await
|
||||
.map(Some);
|
||||
}
|
||||
},
|
||||
};
|
||||
@@ -1560,6 +1430,131 @@ where
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
async fn handle_prepare_failure(
|
||||
&mut self,
|
||||
mut error: ProviderError,
|
||||
candidate: &ProviderCandidate,
|
||||
next_attempt: NonZeroU32,
|
||||
attempt_trace: &TraceContext,
|
||||
) -> Result<PullOutcome, EngineError> {
|
||||
attempt_trace.record_provider_failure(&error);
|
||||
if self.continuation_attempt == ContinuationAttempt::RotateForClientReplay
|
||||
&& error.continuation_recovery_disposition()
|
||||
== Some(ContinuationRecoveryDisposition::ClientReplayRequired)
|
||||
&& let Some(original) = self.last_retryable_failure.take()
|
||||
{
|
||||
// 迁移只改变客户端恢复指令;失败分类、发送事实与运维正文仍属于原请求
|
||||
error = original.with_client_recovery_from(error);
|
||||
self.last_retryable_failure_events.clear();
|
||||
}
|
||||
let kind = error.kind();
|
||||
let account_unavailable = matches!(
|
||||
kind,
|
||||
ProviderErrorKind::NoEligibleAccount
|
||||
| ProviderErrorKind::AccountCapacityUnavailable
|
||||
| ProviderErrorKind::QuotaExhausted
|
||||
| ProviderErrorKind::ConcurrencyQueueFull
|
||||
| ProviderErrorKind::ConcurrencyQueueTimeout
|
||||
);
|
||||
let infrastructure_unavailable =
|
||||
kind == ProviderErrorKind::ProviderInfrastructureUnavailable;
|
||||
let selection_failed =
|
||||
account_unavailable || kind == ProviderErrorKind::SessionPolicyRestricted;
|
||||
let not_sent = error.send_state() == UpstreamSendState::NotSent;
|
||||
let retry_allowed = !error.retry_is_prohibited();
|
||||
let connection_fallback = self.connection_rotation.is_some()
|
||||
&& self
|
||||
.last_retryable_failure
|
||||
.as_ref()
|
||||
.and_then(ProviderError::unreachable_proxy)
|
||||
.is_none()
|
||||
&& !self.connection_budget.exhausted()
|
||||
&& retry_allowed
|
||||
&& not_sent
|
||||
&& account_unavailable;
|
||||
let continuation_retry =
|
||||
retry_allowed && self.prepare_unavailable_native_continuation_replay(&error);
|
||||
let candidate_retry = retry_allowed
|
||||
&& !connection_fallback
|
||||
&& !continuation_retry
|
||||
&& (account_unavailable || infrastructure_unavailable)
|
||||
&& not_sent
|
||||
&& self.current_send_state() == UpstreamSendState::NotSent
|
||||
&& matches!(
|
||||
self.continuation_attempt,
|
||||
ContinuationAttempt::None | ContinuationAttempt::ReplayAny
|
||||
)
|
||||
// 跨 Provider 候选推进必然换号(账号行按 Provider 隔离),
|
||||
// 预算耗尽后不再推进,交回容量类失败的原有终态语义
|
||||
&& self.has_account_rotation_budget()
|
||||
&& self.advance_provider_candidate();
|
||||
let retryable = self
|
||||
.apply_retry_policy(super::policy::RetryFacts {
|
||||
attempt_index: next_attempt,
|
||||
provider: candidate.provider().clone(),
|
||||
model: candidate
|
||||
.upstream_model()
|
||||
.map(|model| model.as_str().to_owned()),
|
||||
error_kind: kind,
|
||||
upstream_status: error.upstream_status(),
|
||||
send_state: error.send_state(),
|
||||
remaining_routing_attempts: self
|
||||
.plan
|
||||
.max_attempts()
|
||||
.get()
|
||||
.saturating_sub(self.routing_attempts),
|
||||
remaining_deadline: Duration::ZERO,
|
||||
retry_allowed: connection_fallback || continuation_retry || candidate_retry,
|
||||
})
|
||||
.await?;
|
||||
attempt_trace.record("retry.decided", json!({
|
||||
"phase": "prepare", "retryable": retryable,
|
||||
"continuationRetry": continuation_retry, "candidateRetry": candidate_retry,
|
||||
"connectionFallback": connection_fallback,
|
||||
"retryProhibited": error.retry_is_prohibited(),
|
||||
"sendState": error.send_state().as_str(),
|
||||
"requestSendState": self.current_send_state().as_str(),
|
||||
"continuation": format!("{:?}", self.continuation_attempt),
|
||||
"accountRotations": self.account_rotations,
|
||||
"maxAccountRotations": self.plan.account_selection_policy().max_account_rotations(),
|
||||
"remainingRoutingAttempts": self.plan.max_attempts().get().saturating_sub(self.routing_attempts),
|
||||
}));
|
||||
if retryable {
|
||||
if connection_fallback {
|
||||
let recovery = self
|
||||
.connection_rotation
|
||||
.take()
|
||||
.expect("pending connection rotation");
|
||||
self.excluded_accounts.remove(&recovery.account);
|
||||
self.pending_retry = Some(recovery);
|
||||
} else if candidate_retry {
|
||||
// 原账号只属于原 Provider,跨候选后不能拿旧钉选作恢复兜底
|
||||
self.connection_rotation = None;
|
||||
}
|
||||
return Ok(PullOutcome::AttemptDiscarded);
|
||||
}
|
||||
if selection_failed && let Some(last_failure) = self.last_retryable_failure.take() {
|
||||
let send_state = self.current_send_state();
|
||||
let mut events = std::mem::take(&mut self.last_retryable_failure_events);
|
||||
if !events.is_empty() {
|
||||
self.observe_atomic_terminal_events(&mut events);
|
||||
self.budget_attempt_already_counted = true;
|
||||
return Ok(PullOutcome::TerminalFailure {
|
||||
events,
|
||||
error: last_failure,
|
||||
send_state,
|
||||
});
|
||||
}
|
||||
self.finish_provider_error(&last_failure).await?;
|
||||
return Err(provider_engine_error(last_failure));
|
||||
}
|
||||
if !(not_sent && (selection_failed || infrastructure_unavailable)) {
|
||||
self.record_provider_failure(candidate.provider().clone());
|
||||
}
|
||||
self.finish_provider_error(&error).await?;
|
||||
Err(provider_engine_error(error))
|
||||
}
|
||||
|
||||
fn advance_provider_candidate(&mut self) -> bool {
|
||||
let Some(next) = self.candidate_index.checked_add(1) else {
|
||||
return false;
|
||||
@@ -1947,7 +1942,7 @@ where
|
||||
let store = Arc::clone(self.engine.store());
|
||||
let request_id = self.request_id.clone();
|
||||
let persisted = self.request_persisted;
|
||||
// 错误、账号 lease 与决策 future 都跨 next_event 的取消保留
|
||||
// 错误、尝试记录与决策 future 都跨 next_event 的取消保留
|
||||
// 在持久化及策略回调完成前,不能再次拉流或建立新的 attempt
|
||||
self.current = Some(current);
|
||||
self.pending_stream_failure = Some(PendingStreamFailure {
|
||||
@@ -2063,7 +2058,11 @@ where
|
||||
.pending_terminal_failure
|
||||
.as_ref()
|
||||
.ok_or(EngineError::NoActiveAttempt)?;
|
||||
if pending.events.is_empty() {
|
||||
// 在保活提交前冻结 HTTP 错误,否则必须交付原流内事件
|
||||
if pending.events.is_empty()
|
||||
|| (pending.error.client_visible_upstream_response().is_some()
|
||||
&& self.response_commit.finish())
|
||||
{
|
||||
return Err(provider_engine_error(
|
||||
self.finalize_pending_terminal_failure().await?,
|
||||
));
|
||||
@@ -2440,6 +2439,9 @@ where
|
||||
}
|
||||
|
||||
async fn finish_interruption(&mut self, error: &EngineError) -> Result<(), EngineError> {
|
||||
if let Some(current) = self.current.as_mut() {
|
||||
current.stream.abort();
|
||||
}
|
||||
// 结算 I/O 可能继续等待,先冻结交付事实以保持终态与实际 HTTP 状态一致
|
||||
self.response_commit.finish();
|
||||
let (outcome, gateway_error) = match error {
|
||||
|
||||
@@ -42,9 +42,12 @@ impl ResponseCommit {
|
||||
self.committed_at().is_some()
|
||||
}
|
||||
|
||||
pub(crate) fn finish(&self) {
|
||||
// 执行已结束且未提交时保留原 HTTP 错误,不能因外层插件迟迟返回再改成 200
|
||||
let _ = self.first.set(TransportState::Finished);
|
||||
/// 冻结交付状态,返回是否仍可发送独立的 HTTP 错误响应
|
||||
pub(crate) fn finish(&self) -> bool {
|
||||
matches!(
|
||||
self.first.get_or_init(|| TransportState::Finished),
|
||||
TransportState::Finished
|
||||
)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
|
||||
@@ -24,6 +24,11 @@ pub(super) fn authentication_gateway_error(error: ClientAuthenticationError) ->
|
||||
|
||||
pub(super) fn map_routing_error(error: crate::validation::RoutingError) -> GatewayError {
|
||||
match error {
|
||||
crate::validation::RoutingError::GroupModelDenied => GatewayError::new(
|
||||
GatewayErrorKind::PolicyDenied,
|
||||
crate::validation::GROUP_MODEL_DENIED_MESSAGE,
|
||||
)
|
||||
.with_client_code(crate::validation::GROUP_MODEL_DENIED_CODE),
|
||||
crate::validation::RoutingError::ModelNotFound {
|
||||
model,
|
||||
mapped_model,
|
||||
|
||||
@@ -21,7 +21,7 @@ use crate::{
|
||||
concurrency::{CapacityWait, ConcurrencyWaitBudget, ConcurrencyWaitQueue},
|
||||
engine::{
|
||||
AttemptCoordinator, EngineError, ExecutionStore, GatewayEngine, ModelRequestId,
|
||||
NewModelRequest, ProbeFailure, ProviderAccountId, UpstreamSendState,
|
||||
ModelRouteScope, NewModelRequest, ProbeFailure, ProviderAccountId, UpstreamSendState,
|
||||
admission::{
|
||||
ClientAdmissionDecision, ClientAdmissionPort, ClientAdmissionRejection,
|
||||
ClientAdmissionRequest,
|
||||
@@ -62,7 +62,7 @@ use crate::{
|
||||
policy::{ClientApiKeyId, ClientPolicy},
|
||||
provider_ports::{ProviderSessionAffinityPort, ProviderStoreErrorKind},
|
||||
routing::{
|
||||
FrozenAccountScope, ProviderCatalogUnavailable, ProviderModelContent,
|
||||
DirectAccountModel, FrozenAccountScope, ProviderCatalogUnavailable, ProviderModelContent,
|
||||
PublicModelDescriptor, PublicModelId, RoutingContext, RuntimeSnapshot, UpstreamModelId,
|
||||
request_settings::RequestSettings,
|
||||
},
|
||||
@@ -478,9 +478,10 @@ impl DefaultExecutionService {
|
||||
let budget_key_id = request.client.policy.key_id().clone();
|
||||
let mut entered_execution = false;
|
||||
let result = async {
|
||||
crate::engine::image_generation::check_image_generation(
|
||||
authorization.account_scope.allows_image_generation(),
|
||||
crate::engine::group_permissions::check_group_permissions(
|
||||
&authorization.account_scope,
|
||||
&request.operation,
|
||||
&[],
|
||||
)?;
|
||||
if authorization.cancellation.is_cancelled() {
|
||||
return Err(GatewayError::new(
|
||||
@@ -507,6 +508,48 @@ impl DefaultExecutionService {
|
||||
}
|
||||
routing_context.required_provider = Some(provider);
|
||||
}
|
||||
let continuation = match request.metadata.previous_response_id.as_ref() {
|
||||
Some(previous) => {
|
||||
let resolve = self
|
||||
.continuation
|
||||
.resolve(request.client.policy.key_id(), previous)
|
||||
.fuse();
|
||||
let timeout = Delay::new(COORDINATION_TIMEOUT).fuse();
|
||||
pin_mut!(resolve, timeout);
|
||||
let pin = select_biased! {
|
||||
result = resolve => match result {
|
||||
Ok(pin) => pin,
|
||||
Err(error)
|
||||
if error.kind() == NativeContinuationStoreErrorKind::Unavailable =>
|
||||
{
|
||||
tracing::debug!(request_id = request_id.as_str(), %error, "Continuation affinity 查询失败,退化为外部续接");
|
||||
None
|
||||
}
|
||||
Err(error)
|
||||
if error.kind() == NativeContinuationStoreErrorKind::OwnershipMismatch =>
|
||||
{
|
||||
return Err(GatewayError::new(GatewayErrorKind::PolicyDenied, "Continuation does not belong to this client API key."));
|
||||
}
|
||||
Err(error) => {
|
||||
tracing::warn!(request_id = request_id.as_str(), %error, "Continuation affinity 记录无效,已拒绝续接");
|
||||
return Err(GatewayError::new(GatewayErrorKind::Internal, "Continuation state is invalid."));
|
||||
}
|
||||
},
|
||||
_ = timeout => None,
|
||||
};
|
||||
Some(match pin {
|
||||
Some(pin) if !pin.matches_client(request.client.policy.key_id()) => {
|
||||
return Err(GatewayError::new(GatewayErrorKind::PolicyDenied, "Continuation does not belong to this client API key."));
|
||||
}
|
||||
Some(pin) if !authorization.account_scope.allows(pin.account()) => {
|
||||
return Err(GatewayError::new(GatewayErrorKind::PolicyDenied, "Continuation account is outside the client account scope."));
|
||||
}
|
||||
Some(pin) => ContinuationBinding::Pinned(pin),
|
||||
None => ContinuationBinding::External(previous.clone()),
|
||||
})
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
let account_scope = Arc::clone(&authorization.account_scope);
|
||||
let plan = match &request.target {
|
||||
ExecutionTarget::ProviderEndpoint {
|
||||
@@ -573,65 +616,66 @@ impl DefaultExecutionService {
|
||||
}
|
||||
}
|
||||
}
|
||||
request.client.snapshot.plan(
|
||||
&target_model,
|
||||
&request.operation,
|
||||
account_scope,
|
||||
&target_context,
|
||||
)
|
||||
let pin = continuation.as_ref().and_then(ContinuationBinding::pinned);
|
||||
let extension_route = ModelRouteScope {
|
||||
client_api_key: request.client.policy.key_id(),
|
||||
account_groups: &account_group_ids,
|
||||
extensions: &authorization.extension_scope,
|
||||
model: public_model.as_str(),
|
||||
};
|
||||
let direct = request.client.snapshot
|
||||
.available_provider_kinds(&account_scope, &target_context)
|
||||
.find_map(|kind| {
|
||||
let provider = self.providers.get(kind)?;
|
||||
let previous = pin.filter(|pin| pin.provider() == kind)
|
||||
.and_then(NativeContinuationPin::session_state);
|
||||
let model = provider.direct_account_model(
|
||||
&target_model,
|
||||
&request.operation,
|
||||
previous,
|
||||
request.client.snapshot.settings(),
|
||||
)?;
|
||||
middleware.as_ref()
|
||||
.is_none_or(|plan| plan.preserves_model_bindings(extension_route, kind, &model))
|
||||
.then(|| (kind.clone(), model))
|
||||
});
|
||||
if let Some((kind, model)) = direct {
|
||||
let account_scope = match authorization.required_account.as_ref() {
|
||||
Some(id) => Arc::new(account_scope.restricted_to(&Default::default(), &[id.clone()].into())),
|
||||
None => account_scope,
|
||||
};
|
||||
request.client.snapshot.plan_with_direct_account_model(
|
||||
&target_model,
|
||||
&request.operation,
|
||||
account_scope,
|
||||
&target_context,
|
||||
DirectAccountModel {
|
||||
provider: &kind,
|
||||
model: &model,
|
||||
continuation_owner: pin.map(NativeContinuationPin::account),
|
||||
},
|
||||
|account| upstream_adapters.as_ref().is_none_or(|plan| {
|
||||
plan.uses_native_upstream(extension_route, &kind, account)
|
||||
}),
|
||||
)
|
||||
} else {
|
||||
request.client.snapshot.plan(
|
||||
&target_model,
|
||||
&request.operation,
|
||||
account_scope,
|
||||
&target_context,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
.map_err(map_routing_error)?;
|
||||
let continuation = match request.metadata.previous_response_id.as_ref() {
|
||||
Some(previous) => {
|
||||
let resolve = self
|
||||
.continuation
|
||||
.resolve(request.client.policy.key_id(), previous)
|
||||
.fuse();
|
||||
let timeout = Delay::new(COORDINATION_TIMEOUT).fuse();
|
||||
pin_mut!(resolve, timeout);
|
||||
let pin = select_biased! {
|
||||
result = resolve => match result {
|
||||
Ok(pin) => pin,
|
||||
Err(error)
|
||||
if error.kind() == NativeContinuationStoreErrorKind::Unavailable =>
|
||||
{
|
||||
tracing::debug!(request_id = request_id.as_str(), %error, "Continuation affinity 查询失败,退化为外部续接");
|
||||
None
|
||||
}
|
||||
Err(error)
|
||||
if error.kind() == NativeContinuationStoreErrorKind::OwnershipMismatch =>
|
||||
{
|
||||
return Err(GatewayError::new(GatewayErrorKind::PolicyDenied, "Continuation does not belong to this client API key."));
|
||||
}
|
||||
Err(error) => {
|
||||
tracing::warn!(request_id = request_id.as_str(), %error, "Continuation affinity 记录无效,已拒绝续接");
|
||||
return Err(GatewayError::new(GatewayErrorKind::Internal, "Continuation state is invalid."));
|
||||
}
|
||||
},
|
||||
_ = timeout => None,
|
||||
};
|
||||
Some(match pin {
|
||||
Some(pin) if !pin.matches_client(request.client.policy.key_id()) => {
|
||||
return Err(GatewayError::new(GatewayErrorKind::PolicyDenied, "Continuation does not belong to this client API key."));
|
||||
}
|
||||
Some(pin) if !authorization.account_scope.allows(pin.account()) => {
|
||||
return Err(GatewayError::new(GatewayErrorKind::PolicyDenied, "Continuation account is outside the client account scope."));
|
||||
}
|
||||
Some(pin)
|
||||
if !plan.candidates().iter().any(|candidate| candidate.provider() == pin.provider()) =>
|
||||
{
|
||||
return Err(GatewayError::new(GatewayErrorKind::NoAvailableProvider, "Continuation provider is not available."));
|
||||
}
|
||||
Some(pin) => {
|
||||
attach_continuation_session_state(&mut request.operation, &pin);
|
||||
ContinuationBinding::Pinned(pin)
|
||||
}
|
||||
None => ContinuationBinding::External(previous.clone()),
|
||||
})
|
||||
if let Some(pin) = continuation.as_ref().and_then(ContinuationBinding::pinned) {
|
||||
if !plan.candidates().iter().any(|candidate| candidate.provider() == pin.provider()
|
||||
&& candidate.account_scope().allows(pin.account())) {
|
||||
return Err(GatewayError::new(GatewayErrorKind::NoAvailableProvider, "Continuation provider is not available."));
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
attach_continuation_session_state(&mut request.operation, pin);
|
||||
}
|
||||
if start_guard.is_none() {
|
||||
start_guard = Some(
|
||||
self.prepare_execution_start(&request, &request_id, &mut authorization)
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
//! Key 绑定分组的功能权限在请求入口与上游发送前生效
|
||||
|
||||
use crate::{
|
||||
account::scope::FrozenAccountScope, engine::middleware::MiddlewareHeader,
|
||||
error::DeniedGroupFeature, operation::Operation,
|
||||
};
|
||||
|
||||
pub(super) fn check_group_permissions(
|
||||
scope: &FrozenAccountScope,
|
||||
operation: &Operation,
|
||||
headers: &[MiddlewareHeader],
|
||||
) -> Result<(), DeniedGroupFeature> {
|
||||
if !scope.allows_image_generation() && operation.image_generation_requested() {
|
||||
return Err(DeniedGroupFeature::ImageGeneration);
|
||||
}
|
||||
if !scope.allows_subagents()
|
||||
&& let Operation::Generate(request) = operation
|
||||
&& request.protocol_payload().protocol() == gateway_protocol::openai::OPENAI_PROTOCOL
|
||||
{
|
||||
let payload = request.protocol_payload();
|
||||
let header = |name: &str| {
|
||||
headers
|
||||
.iter()
|
||||
.find(|header| header.name().eq_ignore_ascii_case(name))
|
||||
.and_then(|header| std::str::from_utf8(header.value()).ok())
|
||||
};
|
||||
if gateway_protocol::openai::codex::collaboration_subagent_requested(
|
||||
payload.body(),
|
||||
payload.context(),
|
||||
header,
|
||||
) {
|
||||
return Err(DeniedGroupFeature::CollaborativeSubagents);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
//! 冻结分组生图权限的请求与发送前门禁
|
||||
|
||||
use crate::{
|
||||
error::{GatewayError, GatewayErrorKind},
|
||||
operation::Operation,
|
||||
};
|
||||
|
||||
pub(super) fn check_image_generation(
|
||||
allowed: bool,
|
||||
operation: &Operation,
|
||||
) -> Result<(), GatewayError> {
|
||||
if !allowed && operation.image_generation_requested() {
|
||||
return Err(GatewayError::new(
|
||||
GatewayErrorKind::PolicyDenied,
|
||||
"Image generation is not allowed by the API key's account groups.",
|
||||
)
|
||||
.with_client_code("image_generation_disabled"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -9,6 +9,16 @@ use std::{fmt, sync::Arc};
|
||||
|
||||
/// 一个发布代次冻结的统一数据面中间件计划
|
||||
pub trait MiddlewarePlan: Send + Sync + fmt::Debug {
|
||||
/// 只在能证明模型变更保留所有已绑定处理器时允许原生模型替换。
|
||||
fn preserves_model_bindings(
|
||||
&self,
|
||||
_route: crate::engine::ModelRouteScope<'_>,
|
||||
_provider: &crate::routing::ProviderKind,
|
||||
_target: &crate::routing::UpstreamModelId,
|
||||
) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn has_outbound(&self, _context: &MiddlewareContext) -> bool {
|
||||
false
|
||||
}
|
||||
@@ -85,6 +95,16 @@ pub struct FrozenMiddlewarePlan {
|
||||
}
|
||||
|
||||
impl FrozenMiddlewarePlan {
|
||||
#[must_use]
|
||||
pub fn preserves_model_bindings(
|
||||
&self,
|
||||
route: crate::engine::ModelRouteScope<'_>,
|
||||
provider: &crate::routing::ProviderKind,
|
||||
target: &crate::routing::UpstreamModelId,
|
||||
) -> bool {
|
||||
self.plan.preserves_model_bindings(route, provider, target)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn has_outbound(&self, context: &MiddlewareContext) -> bool {
|
||||
self.plan.has_outbound(context)
|
||||
|
||||
@@ -10,7 +10,7 @@ pub mod continuation;
|
||||
pub mod coordinator;
|
||||
pub mod execution;
|
||||
pub mod extensions;
|
||||
mod image_generation;
|
||||
mod group_permissions;
|
||||
pub mod middleware;
|
||||
pub mod nested;
|
||||
pub mod observation;
|
||||
@@ -285,6 +285,31 @@ impl ContinuationAttempt {
|
||||
}
|
||||
}
|
||||
|
||||
/// 插件模型绑定使用同一组 Key、分组、调用链与模型事实。
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct ModelRouteScope<'a> {
|
||||
pub client_api_key: &'a crate::policy::ClientApiKeyId,
|
||||
pub account_groups: &'a [crate::routing::AccountGroupId],
|
||||
pub extensions: &'a extensions::ExtensionCallScope,
|
||||
pub model: &'a str,
|
||||
}
|
||||
|
||||
impl<'a> ModelRouteScope<'a> {
|
||||
fn for_attempt(
|
||||
context: &'a AttemptContext,
|
||||
upstream: &'a crate::routing::UpstreamModelId,
|
||||
) -> Self {
|
||||
Self {
|
||||
client_api_key: context.client_api_key_ref(),
|
||||
account_groups: context.account_group_ids(),
|
||||
extensions: context.extension_scope(),
|
||||
model: context
|
||||
.requested_model()
|
||||
.map_or(upstream.as_str(), |model| model.as_str()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Provider 每次执行可见的 request-local context
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RequestAttemptContext {
|
||||
@@ -295,6 +320,7 @@ pub struct RequestAttemptContext {
|
||||
pricing: Arc<crate::metering::PricingOverrides>,
|
||||
request_profile: Option<crate::account::OpaqueProviderData>,
|
||||
fast_mode: FastMode,
|
||||
openai_request_country: crate::account::OpenAiRequestCountry,
|
||||
request_location: Option<crate::account::RequestLocation>,
|
||||
privacy: Option<Arc<dyn crate::settings::privacy::CompiledPrivacyPolicy>>,
|
||||
request_id: ModelRequestId,
|
||||
@@ -392,6 +418,15 @@ impl RequestAttemptContext {
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn with_openai_request_country(
|
||||
mut self,
|
||||
country: crate::account::OpenAiRequestCountry,
|
||||
) -> Self {
|
||||
self.openai_request_country = country;
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn with_request_location(
|
||||
mut self,
|
||||
@@ -413,6 +448,7 @@ impl RequestAttemptContext {
|
||||
request_profile: None,
|
||||
pricing: Arc::default(),
|
||||
fast_mode: FastMode::Default,
|
||||
openai_request_country: Default::default(),
|
||||
request_location: None,
|
||||
privacy: None,
|
||||
timing_started_at: Instant::now(),
|
||||
@@ -605,7 +641,7 @@ impl AttemptContext {
|
||||
});
|
||||
self.request.upstream_adapters.as_ref().map_or(
|
||||
Ok(upstream_adapter::UpstreamAdapterSelection::Native),
|
||||
|plan| plan.select(self, provider, model, account),
|
||||
|plan| plan.select(ModelRouteScope::for_attempt(self, model), provider, account),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -620,7 +656,11 @@ impl AttemptContext {
|
||||
.upstream_adapters
|
||||
.as_ref()
|
||||
.map_or(Ok(None), |plan| {
|
||||
match plan.select(self, provider, model, Some(account))? {
|
||||
match plan.select(
|
||||
ModelRouteScope::for_attempt(self, model),
|
||||
provider,
|
||||
Some(account),
|
||||
)? {
|
||||
upstream_adapter::UpstreamAdapterSelection::Native => Ok(None),
|
||||
upstream_adapter::UpstreamAdapterSelection::Adapter(adapter) => {
|
||||
Ok(Some(adapter))
|
||||
@@ -643,6 +683,11 @@ impl AttemptContext {
|
||||
self.request.client_transport
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn endpoint(&self) -> &str {
|
||||
self.request.endpoint()
|
||||
}
|
||||
|
||||
/// 原生响应可交付可替换前导事件;中间件可能改变其输出语义
|
||||
#[must_use]
|
||||
pub fn allows_replayable_prelude(&self) -> bool {
|
||||
@@ -680,6 +725,11 @@ impl AttemptContext {
|
||||
self.request.request_location.as_ref()
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn openai_request_country(&self) -> &crate::account::OpenAiRequestCountry {
|
||||
&self.request.openai_request_country
|
||||
}
|
||||
|
||||
/// 当前 attempt 的诊断关联;克隆后可传给后台 transport 任务
|
||||
#[must_use]
|
||||
pub fn trace(&self) -> crate::diagnostics::TraceContext {
|
||||
@@ -772,19 +822,15 @@ impl AttemptContext {
|
||||
terminal: provider::ProviderMiddlewareTerminal,
|
||||
) -> Result<provider::ProviderStream, ProviderError> {
|
||||
// 入口已校验原请求,此处复查中间件最终提交的 operation,每次重试均生效
|
||||
let terminal: provider::ProviderMiddlewareTerminal = if self
|
||||
let terminal: provider::ProviderMiddlewareTerminal = if let Some(scope) = self
|
||||
.account_scope()
|
||||
.is_some_and(|scope| !scope.allows_image_generation())
|
||||
.filter(|scope| !scope.allows_image_generation() || !scope.allows_subagents())
|
||||
{
|
||||
let scope = Arc::clone(scope);
|
||||
Box::new(move |operation, headers| {
|
||||
Box::pin(async move {
|
||||
image_generation::check_image_generation(false, &operation).map_err(|_| {
|
||||
ProviderError::new(
|
||||
ProviderErrorKind::RequestPolicyDenied,
|
||||
UpstreamSendState::NotSent,
|
||||
)
|
||||
.with_retry_prohibited()
|
||||
})?;
|
||||
group_permissions::check_group_permissions(&scope, &operation, &headers)
|
||||
.map_err(ProviderError::group_feature_denied)?;
|
||||
terminal(operation, headers).await
|
||||
})
|
||||
})
|
||||
|
||||
@@ -250,8 +250,7 @@ const MAX_NATIVE_RESPONSE_EVENTS_PER_INPUT: usize = 64;
|
||||
/// 这样 Coordinator 可以先持久化 attempt,再越过发送屏障
|
||||
pub struct ProviderStream {
|
||||
metadata: ProviderCallMetadata,
|
||||
events: EventStream,
|
||||
_lease: Box<dyn ResourceLease>,
|
||||
source: Option<ProviderStreamSource>,
|
||||
native_response_translator: Option<Box<dyn NativeResponseTranslator>>,
|
||||
account_feedback: Option<ProviderStreamAccountFeedback>,
|
||||
account_protection: Option<super::account_protection::AttemptProtection>,
|
||||
@@ -259,7 +258,12 @@ pub struct ProviderStream {
|
||||
Option<futures::future::BoxFuture<'static, Option<Result<ProviderEvent, ProviderError>>>>,
|
||||
validator: Result<EventSequenceValidator, EventSequenceError>,
|
||||
strict_canonical_seen: bool,
|
||||
terminated: bool,
|
||||
}
|
||||
|
||||
// 事件流内部也可能持有同一租约,结束读取时须一并丢弃
|
||||
struct ProviderStreamSource {
|
||||
events: EventStream,
|
||||
_lease: Box<dyn ResourceLease>,
|
||||
}
|
||||
|
||||
struct ProviderStreamAccountFeedback {
|
||||
@@ -360,18 +364,25 @@ impl ProviderStream {
|
||||
{
|
||||
Self {
|
||||
metadata,
|
||||
events: Box::pin(events),
|
||||
_lease: Box::new(lease),
|
||||
source: Some(ProviderStreamSource {
|
||||
events: Box::pin(events),
|
||||
_lease: Box::new(lease),
|
||||
}),
|
||||
native_response_translator: None,
|
||||
account_feedback: None,
|
||||
account_protection: None,
|
||||
protection_feedback: None,
|
||||
validator: Ok(EventSequenceValidator::new()),
|
||||
strict_canonical_seen: false,
|
||||
terminated: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn abort(&mut self) {
|
||||
self.source.take();
|
||||
self.account_protection.take();
|
||||
self.protection_feedback.take();
|
||||
}
|
||||
|
||||
/// 在第一次发送前捕获反馈栅栏,诊断调用由协调器排除
|
||||
#[must_use]
|
||||
pub async fn with_account_error_protection(
|
||||
@@ -425,6 +436,15 @@ impl ProviderStream {
|
||||
/// source/target 格式不一致、单事件展开越界、终态被丢弃或 Provider 转换失败时
|
||||
/// 返回错误
|
||||
pub fn translate_native_response(
|
||||
&mut self,
|
||||
event: ProviderEvent,
|
||||
terminal: bool,
|
||||
) -> Result<Vec<ProviderEvent>, ProviderError> {
|
||||
self.translate_event(event, terminal)
|
||||
.inspect_err(|_| self.abort())
|
||||
}
|
||||
|
||||
fn translate_event(
|
||||
&mut self,
|
||||
mut event: ProviderEvent,
|
||||
terminal: bool,
|
||||
@@ -979,8 +999,12 @@ impl Stream for ProviderStream {
|
||||
};
|
||||
}
|
||||
let result = this.poll_event(context);
|
||||
let upstream_finished = matches!(&result, Poll::Ready(Some(Err(_)) | None));
|
||||
if upstream_finished {
|
||||
this.source.take();
|
||||
}
|
||||
// 客户端可在成功终态后停止拉取,不能依赖之后的 EOF 才清除保护证据
|
||||
let protection_terminal = matches!(&result, Poll::Ready(Some(Err(_)) | None))
|
||||
let protection_terminal = upstream_finished
|
||||
|| (matches!(&result, Poll::Ready(Some(Ok(_))))
|
||||
&& this
|
||||
.account_protection
|
||||
@@ -1003,14 +1027,14 @@ impl ProviderStream {
|
||||
context: &mut Context<'_>,
|
||||
) -> Poll<Option<Result<ProviderEvent, ProviderError>>> {
|
||||
let this = self;
|
||||
if this.terminated {
|
||||
let Some(source) = this.source.as_mut() else {
|
||||
return Poll::Ready(None);
|
||||
}
|
||||
};
|
||||
if let Some(feedback) = this.account_feedback.as_mut() {
|
||||
feedback.start();
|
||||
}
|
||||
|
||||
match this.events.as_mut().poll_next(context) {
|
||||
match source.events.as_mut().poll_next(context) {
|
||||
Poll::Pending => Poll::Pending,
|
||||
Poll::Ready(Some(Ok(event))) => {
|
||||
// 纯 wire 流的 facts 仍只是旁路观测;一旦交付 canonical-only
|
||||
@@ -1025,7 +1049,6 @@ impl ProviderStream {
|
||||
}
|
||||
}
|
||||
if this.strict_canonical_seen && this.validator.is_err() {
|
||||
this.terminated = true;
|
||||
let error =
|
||||
ProviderError::new(ProviderErrorKind::Protocol, UpstreamSendState::Sent);
|
||||
if let Some(feedback) = this.account_feedback.as_mut() {
|
||||
@@ -1042,14 +1065,12 @@ impl ProviderStream {
|
||||
Poll::Ready(Some(Ok(event)))
|
||||
}
|
||||
Poll::Ready(Some(Err(error))) => {
|
||||
this.terminated = true;
|
||||
if let Some(feedback) = this.account_feedback.as_mut() {
|
||||
feedback.report_failure(&error);
|
||||
}
|
||||
Poll::Ready(Some(Err(error)))
|
||||
}
|
||||
Poll::Ready(None) => {
|
||||
this.terminated = true;
|
||||
let validation = if this.strict_canonical_seen {
|
||||
this.validator
|
||||
.as_ref()
|
||||
@@ -1153,6 +1174,17 @@ pub trait Provider: Send + Sync {
|
||||
false
|
||||
}
|
||||
|
||||
/// 请求身份明确时,Provider 可以为自身直连账号声明实际模型。Core 按该模型鉴权并冻结重试范围。
|
||||
fn direct_account_model(
|
||||
&self,
|
||||
_model: &PublicModelId,
|
||||
_operation: &Operation,
|
||||
_previous: Option<&crate::operation::ProviderSessionState>,
|
||||
_settings: &crate::settings::SettingsValues,
|
||||
) -> Option<UpstreamModelId> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Provider 解释自己的隐私规则,Core 仅冻结编译结果
|
||||
fn compile_privacy_policy(
|
||||
&self,
|
||||
|
||||
@@ -14,12 +14,11 @@ use crate::{
|
||||
identity::ProviderKind,
|
||||
metering::{CalculatedCost, Usage},
|
||||
operation::Operation,
|
||||
routing::UpstreamModelId,
|
||||
routing::extensions::ExtensionSetReference,
|
||||
};
|
||||
|
||||
use super::{
|
||||
AttemptContext,
|
||||
AttemptContext, ModelRouteScope,
|
||||
middleware::MiddlewareHeader,
|
||||
provider::{EventStream, ProviderCallMetadata},
|
||||
};
|
||||
@@ -28,9 +27,8 @@ use super::{
|
||||
pub trait UpstreamAdapterPlan: fmt::Debug + Send + Sync {
|
||||
fn select(
|
||||
&self,
|
||||
context: &AttemptContext,
|
||||
context: ModelRouteScope<'_>,
|
||||
provider: &ProviderKind,
|
||||
model: &UpstreamModelId,
|
||||
account: Option<&ProviderAccountId>,
|
||||
) -> Result<UpstreamAdapterSelection, ProviderError>;
|
||||
}
|
||||
@@ -62,14 +60,27 @@ impl FrozenUpstreamAdapterPlan {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn select(
|
||||
/// 只有已确认为原生执行的账号可用于原生模型替换;命中但不可用的适配器保留其拒绝语义。
|
||||
#[must_use]
|
||||
pub fn uses_native_upstream(
|
||||
&self,
|
||||
context: &AttemptContext,
|
||||
route: ModelRouteScope<'_>,
|
||||
provider: &ProviderKind,
|
||||
account: &ProviderAccountId,
|
||||
) -> bool {
|
||||
matches!(
|
||||
self.select(route, provider, Some(account)),
|
||||
Ok(UpstreamAdapterSelection::Native)
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn select(
|
||||
&self,
|
||||
context: ModelRouteScope<'_>,
|
||||
provider: &ProviderKind,
|
||||
model: &UpstreamModelId,
|
||||
account: Option<&ProviderAccountId>,
|
||||
) -> Result<UpstreamAdapterSelection, ProviderError> {
|
||||
self.plan.select(context, provider, model, account)
|
||||
self.plan.select(context, provider, account)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -425,7 +425,7 @@ impl ProviderConnectionObservation {
|
||||
|
||||
/// 只供当前客户端请求使用的 HTTP 失败响应
|
||||
///
|
||||
/// 通常保留原始上游响应;Provider 也可为明确的恢复流程准备客户端投影
|
||||
/// Provider 可透传上游 HTTP 响应,也可按客户端协议构造错误正文
|
||||
/// 投影不能覆盖 [`ProviderError`] 中用于诊断和记账的真实上游事实
|
||||
///
|
||||
/// 该值不属于稳定诊断事实,不能进入日志或持久化
|
||||
@@ -508,6 +508,7 @@ impl fmt::Debug for ClientVisibleUpstreamResponse {
|
||||
/// Core 会在任何持久化或日志记录前取走
|
||||
/// 这些事件,不能把它们当作诊断上下文使用
|
||||
pub struct ProviderError {
|
||||
denied_group_feature: Option<DeniedGroupFeature>,
|
||||
kind: ProviderErrorKind,
|
||||
send_state: UpstreamSendState,
|
||||
upstream_status: Option<u16>,
|
||||
@@ -556,6 +557,7 @@ impl ProviderError {
|
||||
Self {
|
||||
kind,
|
||||
send_state,
|
||||
denied_group_feature: None,
|
||||
upstream_status: None,
|
||||
upstream_values: None,
|
||||
retry_after: None,
|
||||
@@ -579,6 +581,16 @@ impl ProviderError {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn group_feature_denied(feature: DeniedGroupFeature) -> Self {
|
||||
let mut error = Self::new(
|
||||
ProviderErrorKind::RequestPolicyDenied,
|
||||
UpstreamSendState::NotSent,
|
||||
)
|
||||
.with_retry_prohibited();
|
||||
error.denied_group_feature = Some(feature);
|
||||
error
|
||||
}
|
||||
|
||||
/// 附加合法上游状态码
|
||||
#[must_use]
|
||||
pub fn with_status(mut self, status: u16) -> Self {
|
||||
@@ -833,7 +845,7 @@ impl ProviderError {
|
||||
self
|
||||
}
|
||||
|
||||
/// 附加仅可返回给当前请求方的原始上游 HTTP 失败响应
|
||||
/// 附加仅可返回给当前请求方的 HTTP 失败响应
|
||||
#[must_use]
|
||||
pub fn with_client_visible_upstream_response(
|
||||
mut self,
|
||||
@@ -1018,7 +1030,7 @@ impl ProviderError {
|
||||
self.client_visible_upstream_error.as_deref()
|
||||
}
|
||||
|
||||
/// 返回只供当前协议响应使用的原始上游 HTTP 失败响应
|
||||
/// 返回仅供当前请求方使用的 HTTP 失败响应
|
||||
#[must_use]
|
||||
pub fn client_visible_upstream_response(&self) -> Option<&ClientVisibleUpstreamResponse> {
|
||||
self.client_visible_upstream_response.as_deref()
|
||||
@@ -1026,6 +1038,9 @@ impl ProviderError {
|
||||
|
||||
/// 绑定迁移完成后仅替换客户端恢复投影,保留原始失败的诊断与计费事实
|
||||
pub(crate) fn with_client_recovery_from(mut self, recovery: Self) -> Self {
|
||||
if let Some(reason) = recovery.continuation_unavailable_reason() {
|
||||
self = self.with_continuation_unavailable_reason(reason);
|
||||
}
|
||||
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;
|
||||
@@ -1057,6 +1072,7 @@ impl ProviderError {
|
||||
pub fn stable_snapshot(&self) -> Self {
|
||||
Self {
|
||||
kind: self.kind,
|
||||
denied_group_feature: self.denied_group_feature,
|
||||
send_state: self.send_state,
|
||||
upstream_status: self.upstream_status,
|
||||
upstream_values: self.upstream_values.clone(),
|
||||
@@ -1241,6 +1257,28 @@ impl GatewayErrorKind {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub(crate) enum DeniedGroupFeature {
|
||||
ImageGeneration,
|
||||
CollaborativeSubagents,
|
||||
}
|
||||
|
||||
impl From<DeniedGroupFeature> for GatewayError {
|
||||
fn from(feature: DeniedGroupFeature) -> Self {
|
||||
let (code, message) = match feature {
|
||||
DeniedGroupFeature::ImageGeneration => (
|
||||
"image_generation_disabled",
|
||||
"Image generation is not allowed by the API key's account groups.",
|
||||
),
|
||||
DeniedGroupFeature::CollaborativeSubagents => (
|
||||
"subagents_disabled",
|
||||
"Collaborative subagents are not allowed by the API key's account groups.",
|
||||
),
|
||||
};
|
||||
Self::new(GatewayErrorKind::PolicyDenied, message).with_client_code(code)
|
||||
}
|
||||
}
|
||||
|
||||
/// 协议无关、可安全暴露的网关错误
|
||||
#[derive(Clone, Error)]
|
||||
#[error("{message}")]
|
||||
@@ -1342,9 +1380,10 @@ impl GatewayError {
|
||||
GatewayErrorKind::UpstreamUnavailable,
|
||||
"The upstream account cannot access the service.",
|
||||
),
|
||||
ProviderErrorKind::RequestPolicyDenied => {
|
||||
Self::new(GatewayErrorKind::PolicyDenied, "Request blocked by policy.")
|
||||
}
|
||||
ProviderErrorKind::RequestPolicyDenied => error.denied_group_feature.map_or_else(
|
||||
|| Self::new(GatewayErrorKind::PolicyDenied, "Request blocked by policy."),
|
||||
Self::from,
|
||||
),
|
||||
ProviderErrorKind::RateLimited | ProviderErrorKind::QuotaExhausted => Self::new(
|
||||
GatewayErrorKind::RateLimited,
|
||||
"Upstream capacity is temporarily unavailable.",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! 下游 Client API Key 的准入策略
|
||||
//!
|
||||
//! Client API Key 冻结账号分组权限;模型名称不参与权限判断
|
||||
//! Client API Key 在请求开始时冻结分组成员范围及其模型策略
|
||||
|
||||
mod client_version;
|
||||
mod peer_sharing;
|
||||
|
||||
@@ -532,21 +532,28 @@ pub struct RoutingContext {
|
||||
pub blocked_providers: BTreeSet<ProviderKind>,
|
||||
}
|
||||
|
||||
/// 直连模型及首次原生续接必须使用的账号
|
||||
pub struct DirectAccountModel<'a> {
|
||||
pub provider: &'a ProviderKind,
|
||||
pub model: &'a UpstreamModelId,
|
||||
pub continuation_owner: Option<&'a crate::account::ProviderAccountId>,
|
||||
}
|
||||
|
||||
/// 已绑定 Provider 的请求候选;模型端点携带真实上游模型,原生端点不虚构模型
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ProviderCandidate {
|
||||
provider: ProviderKind,
|
||||
requires_direct_authentication: bool,
|
||||
upstream_model: Option<UpstreamModelId>,
|
||||
model_presentation: Option<ModelPresentation>,
|
||||
emulated_features: BTreeSet<Feature>,
|
||||
account_scope: Arc<FrozenAccountScope>,
|
||||
}
|
||||
|
||||
impl ProviderCandidate {
|
||||
/// 与路由计划冻结的上游模型目录,重试时不读取新的目录代次
|
||||
/// 模型按直连账号身份选择,Provider 必须在发送前核对实际凭据。
|
||||
#[must_use]
|
||||
pub const fn model_presentation(&self) -> Option<&ModelPresentation> {
|
||||
self.model_presentation.as_ref()
|
||||
pub const fn requires_direct_authentication(&self) -> bool {
|
||||
self.requires_direct_authentication
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
@@ -578,6 +585,7 @@ pub struct RoutingPlan {
|
||||
request_body_limit: usize,
|
||||
max_retry_after: std::time::Duration,
|
||||
pricing: Arc<crate::metering::PricingOverrides>,
|
||||
openai_request_country: crate::account::OpenAiRequestCountry,
|
||||
request_location: Option<crate::account::RequestLocation>,
|
||||
privacy: Option<Arc<dyn crate::settings::privacy::CompiledPrivacyPolicy>>,
|
||||
config_revision: ConfigRevision,
|
||||
@@ -632,6 +640,11 @@ impl RoutingPlan {
|
||||
self.request_location.as_ref()
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn openai_request_country(&self) -> &crate::account::OpenAiRequestCountry {
|
||||
&self.openai_request_country
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn config_revision(&self) -> ConfigRevision {
|
||||
self.config_revision
|
||||
|
||||
@@ -17,10 +17,10 @@ use crate::settings::{SettingsValues, compiled::CompiledSettings};
|
||||
use crate::validation::RoutingError;
|
||||
|
||||
use super::{
|
||||
AccountGroupId, ClientRoutingScope, ConfigRevision, FrozenAccountScope, ModelCapabilities,
|
||||
ProviderCandidate, ProviderCatalogGeneration, ProviderCatalogPort, ProviderKind, ProviderModel,
|
||||
PublicModelId, RoutingContext, RoutingGroupSnapshot, RoutingPlan, RuntimeAccount,
|
||||
RuntimeAccountDirectory, UpstreamModelId,
|
||||
AccountGroupId, ClientRoutingScope, ConfigRevision, DirectAccountModel, FrozenAccountScope,
|
||||
ModelCapabilities, ProviderCandidate, ProviderCatalogGeneration, ProviderCatalogPort,
|
||||
ProviderKind, ProviderModel, PublicModelId, RoutingContext, RoutingGroupSnapshot, RoutingPlan,
|
||||
RuntimeAccount, RuntimeAccountDirectory, UpstreamModelId,
|
||||
};
|
||||
|
||||
const MAXIMUM_SNAPSHOT_STABILITY_ATTEMPTS: usize = 4;
|
||||
@@ -83,7 +83,9 @@ impl SnapshotClientPolicyFacts {
|
||||
/// Store 读取到的账号分组事实
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct SnapshotAccountGroupFacts {
|
||||
model_access: crate::account::AccountModelAccess,
|
||||
allow_image_generation: bool,
|
||||
allow_subagents: bool,
|
||||
fast_mode: FastMode,
|
||||
id: AccountGroupId,
|
||||
name: String,
|
||||
@@ -91,12 +93,24 @@ pub struct SnapshotAccountGroupFacts {
|
||||
}
|
||||
|
||||
impl SnapshotAccountGroupFacts {
|
||||
#[must_use]
|
||||
pub fn with_model_access(mut self, model_access: crate::account::AccountModelAccess) -> Self {
|
||||
self.model_access = model_access;
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn with_image_generation(mut self, allowed: bool) -> Self {
|
||||
self.allow_image_generation = allowed;
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn with_subagents(mut self, allowed: bool) -> Self {
|
||||
self.allow_subagents = allowed;
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn with_fast_mode(mut self, fast_mode: FastMode) -> Self {
|
||||
self.fast_mode = fast_mode;
|
||||
@@ -106,10 +120,12 @@ impl SnapshotAccountGroupFacts {
|
||||
#[must_use]
|
||||
pub fn new(id: AccountGroupId, name: String, enabled: bool) -> Self {
|
||||
Self {
|
||||
model_access: crate::account::AccountModelAccess::all(),
|
||||
id,
|
||||
name,
|
||||
enabled,
|
||||
allow_image_generation: true,
|
||||
allow_subagents: true,
|
||||
fast_mode: FastMode::Default,
|
||||
}
|
||||
}
|
||||
@@ -549,12 +565,24 @@ async fn compile_runtime_snapshot(
|
||||
account.direct_authentication_kind().map(str::to_owned),
|
||||
);
|
||||
}
|
||||
let account_directory = Arc::new(RuntimeAccountDirectory::new(accounts));
|
||||
// 整份快照共享分组模型政策;默认规则无需为每个 Key 或成员复制
|
||||
let group_model_access = groups
|
||||
.values_mut()
|
||||
.filter(|group| {
|
||||
group.enabled
|
||||
&& group.model_access.mode() != crate::account::AccountModelAccessMode::All
|
||||
})
|
||||
.map(|group| (group.id.clone(), std::mem::take(&mut group.model_access)))
|
||||
.collect();
|
||||
let account_directory = Arc::new(
|
||||
RuntimeAccountDirectory::new(accounts).with_group_model_access(group_model_access),
|
||||
);
|
||||
|
||||
let mut client_policies = Vec::with_capacity(facts.client_policies.len());
|
||||
for policy in facts.client_policies {
|
||||
let mut fast_mode = FastMode::Default;
|
||||
let mut allow_image_generation = true;
|
||||
let mut allow_subagents = true;
|
||||
let account_scope = if policy.group_ids.is_empty() {
|
||||
FrozenAccountScope::new(
|
||||
Arc::clone(&account_directory),
|
||||
@@ -574,6 +602,7 @@ async fn compile_runtime_snapshot(
|
||||
// 停用只影响选号,仍绑定的分组继续贡献请求策略
|
||||
fast_mode = fast_mode.merge(group.fast_mode);
|
||||
allow_image_generation &= group.allow_image_generation;
|
||||
allow_subagents &= group.allow_subagents;
|
||||
bound_groups.push(RoutingGroupSnapshot::new(
|
||||
group.id.clone(),
|
||||
group.name.clone(),
|
||||
@@ -597,6 +626,7 @@ async fn compile_runtime_snapshot(
|
||||
Arc::new(
|
||||
account_scope
|
||||
.with_image_generation(allow_image_generation)
|
||||
.with_subagents(allow_subagents)
|
||||
.with_fast_mode(fast_mode)
|
||||
.with_request_profiles(policy.request_profiles),
|
||||
),
|
||||
@@ -967,6 +997,54 @@ impl RuntimeSnapshot {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// 分组编辑器使用成员账号的实际模型,可编辑已停用或已限制模型的分组
|
||||
#[must_use]
|
||||
pub fn upstream_models_for_group(&self, group: &AccountGroupId) -> Vec<UpstreamModelId> {
|
||||
let mut models = BTreeSet::new();
|
||||
for (account_id, account) in self.account_directory.accounts_in_group(group) {
|
||||
let provider = account.provider_kind();
|
||||
if !self.providers.contains(provider) {
|
||||
continue;
|
||||
}
|
||||
let access = account.model_access();
|
||||
if access.mode() == crate::account::AccountModelAccessMode::Allowlist {
|
||||
// 原生图片、Search 和透传模型可能只有账号白名单明确声明了 ID
|
||||
models.extend(
|
||||
access
|
||||
.models()
|
||||
.iter()
|
||||
.filter_map(|id| UpstreamModelId::new(id.clone()).ok()),
|
||||
);
|
||||
}
|
||||
let sources = self.model_catalog_accounts.get(provider);
|
||||
if let Some(catalog) = self.provider_models.get(provider) {
|
||||
models.extend(
|
||||
catalog
|
||||
.keys()
|
||||
.filter(|model| {
|
||||
access.allows(model.as_str())
|
||||
&& sources
|
||||
.and_then(|sources| sources.get(model.as_str()))
|
||||
.is_none_or(|sources| sources.contains(account_id))
|
||||
})
|
||||
.cloned(),
|
||||
);
|
||||
}
|
||||
for contribution in self.contributed_models() {
|
||||
if &contribution.provider == provider && contribution.accounts.contains(account_id)
|
||||
{
|
||||
let target = self.mapped_model(contribution.id.as_str());
|
||||
if access.allows(&target)
|
||||
&& let Ok(model) = UpstreamModelId::new(target)
|
||||
{
|
||||
models.insert(model);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
models.into_iter().collect()
|
||||
}
|
||||
|
||||
/// 合并冻结账号范围内实际存在 Provider 的公开模型
|
||||
#[must_use]
|
||||
pub fn public_models_for_scope(&self, scope: &FrozenAccountScope) -> Vec<PublicModelId> {
|
||||
@@ -1210,21 +1288,26 @@ impl RuntimeSnapshot {
|
||||
account_scope: &FrozenAccountScope,
|
||||
context: &RoutingContext,
|
||||
) -> BTreeSet<ProviderKind> {
|
||||
account_scope
|
||||
.provider_kinds()
|
||||
.iter()
|
||||
.filter(|provider| {
|
||||
self.providers.contains(*provider)
|
||||
&& context
|
||||
.required_provider
|
||||
.as_ref()
|
||||
.is_none_or(|required| required == *provider)
|
||||
&& !context.blocked_providers.contains(*provider)
|
||||
})
|
||||
self.available_provider_kinds(account_scope, context)
|
||||
.cloned()
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub(crate) fn available_provider_kinds<'a>(
|
||||
&'a self,
|
||||
account_scope: &'a FrozenAccountScope,
|
||||
context: &'a RoutingContext,
|
||||
) -> impl Iterator<Item = &'a ProviderKind> {
|
||||
account_scope.provider_kinds().iter().filter(|provider| {
|
||||
self.providers.contains(*provider)
|
||||
&& context
|
||||
.required_provider
|
||||
.as_ref()
|
||||
.is_none_or(|required| required == *provider)
|
||||
&& !context.blocked_providers.contains(*provider)
|
||||
})
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn min_codex_client_versions(&self) -> &CodexClientMinVersions {
|
||||
&self.settings.min_codex_client_versions
|
||||
@@ -1237,7 +1320,67 @@ impl RuntimeSnapshot {
|
||||
account_scope: Arc<FrozenAccountScope>,
|
||||
context: &RoutingContext,
|
||||
) -> Result<RoutingPlan, RoutingError> {
|
||||
self.plan_inner(public_model, operation, account_scope, context, true)
|
||||
self.plan_inner(public_model, operation, account_scope, context, true, None)
|
||||
}
|
||||
|
||||
/// 按实际目标分别校验目录、分组和账号。选中直连或常规路径后,重试沿用同一份计划。
|
||||
pub fn plan_with_direct_account_model(
|
||||
&self,
|
||||
public_model: &PublicModelId,
|
||||
operation: &Operation,
|
||||
scope: Arc<FrozenAccountScope>,
|
||||
context: &RoutingContext,
|
||||
direct: DirectAccountModel<'_>,
|
||||
uses_native_upstream: impl Fn(&ProviderAccountId) -> bool,
|
||||
) -> Result<RoutingPlan, RoutingError> {
|
||||
let DirectAccountModel {
|
||||
provider,
|
||||
model: target,
|
||||
continuation_owner,
|
||||
} = direct;
|
||||
if self.model_alias(public_model.as_str()).is_some()
|
||||
|| self
|
||||
.settings
|
||||
.values
|
||||
.model_mappings
|
||||
.contains_key(public_model.as_str())
|
||||
{
|
||||
return self.plan(public_model, operation, scope, context);
|
||||
}
|
||||
// 自动替换必须有原生目录依据;未替换的账号仍使用普通请求的路由合同。
|
||||
let sources = self
|
||||
.model_catalog_accounts
|
||||
.get(provider)
|
||||
.and_then(|models| models.get(target.as_str()));
|
||||
let direct_accounts = scope
|
||||
.allowed_account_ids()
|
||||
.filter(|id| {
|
||||
scope.directory().account(id).is_some_and(|account| {
|
||||
account.provider_kind() == provider && account.is_direct_account()
|
||||
}) && scope.allows_model(id, target.as_str())
|
||||
&& sources.is_none_or(|accounts| accounts.contains(*id))
|
||||
&& uses_native_upstream(id)
|
||||
})
|
||||
.cloned()
|
||||
.collect::<BTreeSet<_>>();
|
||||
// 原生续接先用原账号,容量恢复仍可在同模型的授权账号间换号
|
||||
if !direct_accounts.is_empty()
|
||||
&& continuation_owner.is_none_or(|owner| direct_accounts.contains(owner))
|
||||
&& let Ok(plan) = self.plan_inner(
|
||||
public_model,
|
||||
operation,
|
||||
Arc::new(
|
||||
scope.restricted_to(&BTreeSet::from([provider.clone()]), &direct_accounts),
|
||||
),
|
||||
context,
|
||||
true,
|
||||
Some(target),
|
||||
)
|
||||
{
|
||||
return Ok(plan);
|
||||
}
|
||||
// 只有尚未开始任何 attempt 的请求才可保留原模型;两个模型从不进入同一重试计划。
|
||||
self.plan(public_model, operation, scope, context)
|
||||
}
|
||||
|
||||
/// 管理探测已固定目标账号,不使用数据面 Key 的账号模型政策裁决探测资格
|
||||
@@ -1253,7 +1396,14 @@ impl RuntimeSnapshot {
|
||||
.as_ref()
|
||||
.clone()
|
||||
.with_request_profiles(self.settings().request_profiles().clone());
|
||||
self.plan_inner(public_model, operation, Arc::new(scope), context, false)
|
||||
self.plan_inner(
|
||||
public_model,
|
||||
operation,
|
||||
Arc::new(scope),
|
||||
context,
|
||||
false,
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
fn plan_inner(
|
||||
@@ -1263,6 +1413,7 @@ impl RuntimeSnapshot {
|
||||
account_scope: Arc<FrozenAccountScope>,
|
||||
context: &RoutingContext,
|
||||
enforce_account_model_policy: bool,
|
||||
direct_model: Option<&UpstreamModelId>,
|
||||
) -> Result<RoutingPlan, RoutingError> {
|
||||
let requirements = operation.capability_requirements();
|
||||
let mut candidates = Vec::new();
|
||||
@@ -1272,19 +1423,28 @@ impl RuntimeSnapshot {
|
||||
}
|
||||
|
||||
let requested_model = public_model.as_str();
|
||||
let mapped_model = self.mapped_model(requested_model);
|
||||
let upstream_model = if self
|
||||
.settings
|
||||
.values
|
||||
.model_mappings
|
||||
.contains_key(requested_model)
|
||||
|| self.model_alias(requested_model).is_some()
|
||||
{
|
||||
UpstreamModelId::new(mapped_model)
|
||||
let upstream_model = if let Some(direct) = direct_model {
|
||||
direct.clone()
|
||||
} else {
|
||||
UpstreamModelId::from_client_wire(mapped_model)
|
||||
let mapped_model = self.mapped_model(requested_model);
|
||||
if self
|
||||
.settings
|
||||
.values
|
||||
.model_mappings
|
||||
.contains_key(requested_model)
|
||||
|| self.model_alias(requested_model).is_some()
|
||||
{
|
||||
UpstreamModelId::new(mapped_model)
|
||||
} else {
|
||||
UpstreamModelId::from_client_wire(mapped_model)
|
||||
}
|
||||
.map_err(|_| RoutingError::InvalidIdentifier)?
|
||||
};
|
||||
if enforce_account_model_policy
|
||||
&& account_scope.groups_deny_model(Some(upstream_model.as_str()))
|
||||
{
|
||||
return Err(RoutingError::GroupModelDenied);
|
||||
}
|
||||
.map_err(|_| RoutingError::InvalidIdentifier)?;
|
||||
// 归属先于账号范围、能力与路由约束判定,原 Provider 被排除不能变成跨平台透传
|
||||
let known_model = self.is_known_upstream_model(&upstream_model)
|
||||
|| self
|
||||
@@ -1300,9 +1460,10 @@ impl RuntimeSnapshot {
|
||||
if !self.providers.contains(provider) {
|
||||
continue;
|
||||
}
|
||||
if self
|
||||
.model_alias(public_model.as_str())
|
||||
.is_some_and(|alias| &alias.provider != provider)
|
||||
if direct_model.is_none()
|
||||
&& self
|
||||
.model_alias(public_model.as_str())
|
||||
.is_some_and(|alias| &alias.provider != provider)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -1324,10 +1485,18 @@ impl RuntimeSnapshot {
|
||||
.provider_models
|
||||
.get(provider)
|
||||
.and_then(|models| models.get(&upstream_model));
|
||||
// 直连模型需要原生能力,不能借用插件为同名模型声明的账号与能力。
|
||||
if direct_model.is_some() && capabilities.is_none() {
|
||||
continue;
|
||||
}
|
||||
let contributions: Vec<_> = self
|
||||
.contributed_models()
|
||||
.iter()
|
||||
.filter(|model| &model.provider == provider && &model.id == public_model)
|
||||
.filter(|model| {
|
||||
direct_model.is_none()
|
||||
&& &model.provider == provider
|
||||
&& &model.id == public_model
|
||||
})
|
||||
.collect();
|
||||
let mut candidate_scope = Arc::clone(&account_scope);
|
||||
let emulated_features = if contributions.is_empty() {
|
||||
@@ -1384,17 +1553,8 @@ impl RuntimeSnapshot {
|
||||
native.unwrap_or_default()
|
||||
};
|
||||
candidates.push(ProviderCandidate {
|
||||
requires_direct_authentication: direct_model.is_some(),
|
||||
provider: provider.clone(),
|
||||
model_presentation: self
|
||||
.provider_model_presentations
|
||||
.get(provider)
|
||||
.and_then(|models| models.get(&upstream_model))
|
||||
.cloned()
|
||||
.or_else(|| {
|
||||
contributions
|
||||
.first()
|
||||
.map(|model| model.presentation.clone())
|
||||
}),
|
||||
upstream_model: Some(upstream_model.clone()),
|
||||
emulated_features,
|
||||
account_scope: candidate_scope,
|
||||
@@ -1434,6 +1594,7 @@ impl RuntimeSnapshot {
|
||||
)),
|
||||
config_revision: self.revision,
|
||||
pricing: Arc::clone(&self.settings.values.pricing),
|
||||
openai_request_country: self.settings.values.openai_request_country().clone(),
|
||||
request_location: self.settings.request_location.clone(),
|
||||
privacy: self.settings.privacy.clone(),
|
||||
account_error_protection: Arc::clone(&self.settings.account_error_protection),
|
||||
@@ -1471,6 +1632,12 @@ impl RuntimeSnapshot {
|
||||
.as_ref()
|
||||
.is_none_or(|required| required == provider)
|
||||
&& !context.blocked_providers.contains(provider);
|
||||
if available
|
||||
&& upstream_model
|
||||
.is_some_and(|model| account_scope.groups_deny_model(Some(model.as_str())))
|
||||
{
|
||||
return Err(RoutingError::GroupModelDenied);
|
||||
}
|
||||
if !available
|
||||
|| upstream_model
|
||||
.is_some_and(|model| !account_scope.allows_provider_model(provider, model.as_str()))
|
||||
@@ -1505,14 +1672,8 @@ impl RuntimeSnapshot {
|
||||
});
|
||||
}
|
||||
let candidate = ProviderCandidate {
|
||||
requires_direct_authentication: false,
|
||||
provider: provider.clone(),
|
||||
model_presentation: upstream_model
|
||||
.and_then(|model| {
|
||||
self.provider_model_presentations
|
||||
.get(provider)
|
||||
.and_then(|models| models.get(model))
|
||||
})
|
||||
.cloned(),
|
||||
upstream_model: upstream_model.cloned(),
|
||||
emulated_features: BTreeSet::new(),
|
||||
account_scope: Arc::clone(&account_scope),
|
||||
@@ -1524,6 +1685,7 @@ impl RuntimeSnapshot {
|
||||
)),
|
||||
config_revision: self.revision,
|
||||
pricing: Arc::clone(&self.settings.values.pricing),
|
||||
openai_request_country: self.settings.values.openai_request_country().clone(),
|
||||
request_location: self.settings.request_location.clone(),
|
||||
privacy: self.settings.privacy.clone(),
|
||||
account_error_protection: Arc::clone(&self.settings.account_error_protection),
|
||||
|
||||
@@ -111,6 +111,10 @@ impl CompiledSettings {
|
||||
max_waiting: settings.max_waiting_per_key,
|
||||
timeout: Duration::from_secs(u64::from(settings.key_queue_timeout_seconds)),
|
||||
};
|
||||
settings
|
||||
.openai_request_country
|
||||
.validate()
|
||||
.map_err(|_| InvalidSettings)?;
|
||||
let request_location = settings
|
||||
.request_location_enabled
|
||||
.then(|| settings.request_location.clone().normalized())
|
||||
|
||||
@@ -25,6 +25,8 @@ pub struct SettingsValues {
|
||||
pub(crate) switch_account_on_queue_failure: bool,
|
||||
pub(crate) max_retry_after_seconds: u32,
|
||||
pub(crate) openai_guardian_reserved_concurrency: u32,
|
||||
pub(crate) openai_oauth_guardian_review_model: bool,
|
||||
pub(crate) openai_request_country: crate::account::OpenAiRequestCountry,
|
||||
pub(crate) openai_account_affinity: crate::account::AccountAffinity,
|
||||
pub(crate) max_account_rotations: u32,
|
||||
pub(crate) openai_session_affinity_ttl_hours: u32,
|
||||
@@ -108,6 +110,31 @@ impl SettingsValues {
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn with_openai_oauth_guardian_review_model(mut self, enabled: bool) -> Self {
|
||||
self.openai_oauth_guardian_review_model = enabled;
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn openai_oauth_guardian_review_model(&self) -> bool {
|
||||
self.openai_oauth_guardian_review_model
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn with_openai_request_country(
|
||||
mut self,
|
||||
policy: crate::account::OpenAiRequestCountry,
|
||||
) -> Self {
|
||||
self.openai_request_country = policy;
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn openai_request_country(&self) -> &crate::account::OpenAiRequestCountry {
|
||||
&self.openai_request_country
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn request_profiles(&self) -> &BTreeMap<ProviderKind, crate::account::OpaqueProviderData> {
|
||||
&self.request_profiles
|
||||
@@ -207,6 +234,8 @@ impl SettingsValues {
|
||||
switch_account_on_queue_failure: false,
|
||||
max_retry_after_seconds: 240,
|
||||
openai_guardian_reserved_concurrency: 0,
|
||||
openai_oauth_guardian_review_model: true,
|
||||
openai_request_country: Default::default(),
|
||||
openai_account_affinity: crate::account::AccountAffinity::default(),
|
||||
max_account_rotations: 3,
|
||||
openai_session_affinity_ttl_hours: 24,
|
||||
|
||||
@@ -54,6 +54,12 @@ pub enum OperationError {
|
||||
},
|
||||
}
|
||||
|
||||
/// Core 路由和 Provider 原生端点共用的分组模型拒绝码
|
||||
pub const GROUP_MODEL_DENIED_CODE: &str = "group_model_denied";
|
||||
/// 拒绝原因不回显请求模型或分组名称
|
||||
pub const GROUP_MODEL_DENIED_MESSAGE: &str =
|
||||
"Model is not allowed by the API key's account groups.";
|
||||
|
||||
/// 路由快照或 Route Plan 不满足不变量
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Error)]
|
||||
pub enum RoutingError {
|
||||
@@ -72,6 +78,9 @@ pub enum RoutingError {
|
||||
/// Key 的账号范围中当前没有任何可参与路由的账号
|
||||
#[error("account routing scope is empty")]
|
||||
EmptyAccountScope,
|
||||
/// 已启用的绑定分组均明确禁止请求模型
|
||||
#[error("{GROUP_MODEL_DENIED_MESSAGE}")]
|
||||
GroupModelDenied,
|
||||
/// 快照中存在重复实体
|
||||
#[error("duplicate {entity} `{id}`")]
|
||||
DuplicateEntity {
|
||||
|
||||
@@ -143,10 +143,10 @@ fn zero_or_absent_window_weights_disable_reset_scores_and_tolerance() {
|
||||
}]
|
||||
.into(),
|
||||
);
|
||||
let legacy = json!({"loadWeight":0,"quotaWeight":0,"healthWeight":0,"latencyWeight":0,"resetWeight":10,"queueWeight":0});
|
||||
let mut disabled = legacy.clone();
|
||||
let omitted = json!({"loadWeight":0,"quotaWeight":0,"healthWeight":0,"latencyWeight":0,"resetWeight":10,"queueWeight":0});
|
||||
let mut disabled = omitted.clone();
|
||||
disabled["resetWindowWeights"] = json!({"fiveHour":0,"weekly":0,"monthly":0});
|
||||
for wire in [legacy, disabled] {
|
||||
for wire in [omitted, disabled] {
|
||||
let config: SmartSchedulingConfig = serde_json::from_value(wire).unwrap();
|
||||
context.policy = context.policy.with_smart_scheduling(config);
|
||||
let trace = gateway_core::diagnostics::TraceContext::new("req_zero_windows");
|
||||
|
||||
@@ -299,14 +299,17 @@ fn complete_success_is_reported_once_but_unfinished_drop_is_not_success() {
|
||||
Ok(gateway_core::event::GatewayEvent::Started(response.clone()).into()),
|
||||
Ok(gateway_core::event::GatewayEvent::Completed(response).into()),
|
||||
]);
|
||||
let mut completed = ProviderStream::new(metadata(), events, ())
|
||||
let released = Arc::new(AtomicUsize::new(0));
|
||||
let mut completed = ProviderStream::new(metadata(), events, Lease(released.clone()))
|
||||
.with_account_error_protection(store.clone(), Arc::new(policy().compile()), 5)
|
||||
.await;
|
||||
assert!(completed.next().await.unwrap().is_ok());
|
||||
assert_eq!(store.successes.load(Ordering::SeqCst), 0);
|
||||
assert!(completed.next().await.unwrap().is_ok());
|
||||
assert_eq!(store.successes.load(Ordering::SeqCst), 1);
|
||||
assert_eq!(released.load(Ordering::SeqCst), 0);
|
||||
assert!(completed.next().await.is_none());
|
||||
assert_eq!(released.load(Ordering::SeqCst), 1);
|
||||
assert!(completed.next().await.is_none());
|
||||
assert_eq!(store.successes.load(Ordering::SeqCst), 1);
|
||||
});
|
||||
@@ -351,27 +354,27 @@ fn stalled_begin_and_feedback_are_bounded_and_preserve_original_failure() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cancelling_pending_feedback_releases_lease_and_does_not_report_success() {
|
||||
fn failed_stream_releases_lease_while_protection_feedback_is_pending() {
|
||||
futures::executor::block_on(async {
|
||||
let store = Arc::new(Feedback {
|
||||
stalled_feedback: true,
|
||||
..Feedback::default()
|
||||
});
|
||||
let released = Arc::new(AtomicUsize::new(0));
|
||||
let mut stream = ProviderStream::new(
|
||||
metadata(),
|
||||
stream::iter([Err(failure("invalid_prompt"))]),
|
||||
Lease(released.clone()),
|
||||
)
|
||||
.with_account_error_protection(store.clone(), Arc::new(policy().compile()), 5)
|
||||
.await;
|
||||
let lease = Arc::new(Lease(released.clone()));
|
||||
let events = stream::unfold(Arc::clone(&lease), |lease| async move {
|
||||
Some((Err(failure("invalid_prompt")), lease))
|
||||
});
|
||||
let mut stream = ProviderStream::new(metadata(), events, lease)
|
||||
.with_account_error_protection(store.clone(), Arc::new(policy().compile()), 5)
|
||||
.await;
|
||||
assert!(
|
||||
timeout(Duration::from_millis(10), stream.next())
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
drop(stream);
|
||||
assert_eq!(released.load(Ordering::SeqCst), 1);
|
||||
drop(stream);
|
||||
assert_eq!(store.successes.load(Ordering::SeqCst), 0);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -898,7 +898,7 @@ impl Provider for ScriptedProvider {
|
||||
futures::stream::iter(items.into_iter().map(canonical_provider_event))
|
||||
.chain(futures::stream::pending()),
|
||||
),
|
||||
(),
|
||||
TrackedLease(Arc::clone(&self.released_leases)),
|
||||
))
|
||||
}
|
||||
Script::ObservedStream { account_id, items } => {
|
||||
@@ -2493,11 +2493,12 @@ 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),
|
||||
for (seconds, replacement, rotations, committed_prelude, expected_attempts) in [
|
||||
(309, true, 1, false, 2),
|
||||
(317, true, 1, true, 2),
|
||||
(309, false, 1, false, 2),
|
||||
(240, true, 1, false, 1),
|
||||
(309, true, 0, false, 1),
|
||||
] {
|
||||
let Operation::Generate(generate) = generate_operation() else {
|
||||
unreachable!()
|
||||
@@ -2516,11 +2517,14 @@ fn native_capacity_above_wait_limit_rotates_before_requesting_client_replay() {
|
||||
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_body = json!({"error": {
|
||||
"code": "server_is_overloaded",
|
||||
"headers": {"retry-after": seconds.to_string(), "retry-after-ms": (seconds * 1000).to_string()}
|
||||
}}).to_string();
|
||||
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(),
|
||||
original_body.clone(),
|
||||
))
|
||||
.with_continuation_recovery_disposition(
|
||||
ContinuationRecoveryDisposition::ClientReplayRequired,
|
||||
@@ -2530,6 +2534,7 @@ fn native_capacity_above_wait_limit_rotates_before_requesting_client_replay() {
|
||||
ProviderErrorKind::ContinuationRecoveryRequired,
|
||||
UpstreamSendState::NotSent,
|
||||
)
|
||||
.with_continuation_unavailable_reason("account_rotated")
|
||||
.with_continuation_recovery_disposition(
|
||||
ContinuationRecoveryDisposition::ClientReplayRequired,
|
||||
)
|
||||
@@ -2544,10 +2549,18 @@ fn native_capacity_above_wait_limit_rotates_before_requesting_client_replay() {
|
||||
UpstreamSendState::NotSent,
|
||||
)
|
||||
};
|
||||
let mut items = Vec::new();
|
||||
if committed_prelude {
|
||||
items.push(Ok(ProviderEvent::canonical(GatewayEvent::Started(
|
||||
ResponseMeta::new("response-1", "gpt-5"),
|
||||
))
|
||||
.with_replayable_prelude()));
|
||||
}
|
||||
items.push(Err(original));
|
||||
let (coordinator, store, provider) = coordinator(vec![
|
||||
Script::Stream {
|
||||
Script::ObservedStream {
|
||||
account_id: "acct_one",
|
||||
items: vec![Err(original)],
|
||||
items,
|
||||
},
|
||||
Script::Error(recovery),
|
||||
]);
|
||||
@@ -2568,26 +2581,31 @@ fn native_capacity_above_wait_limit_rotates_before_requesting_client_replay() {
|
||||
CancellationToken::new(),
|
||||
))
|
||||
.unwrap();
|
||||
let mut collection = Box::pin(session.collect_uncommitted());
|
||||
if committed_prelude {
|
||||
block_on(session.next_event()).unwrap().unwrap();
|
||||
block_on(session.commit_downstream(Some(200))).unwrap();
|
||||
}
|
||||
let mut next = Box::pin(session.next_event());
|
||||
let std::task::Poll::Ready(Err(EngineError::Provider(error))) =
|
||||
block_on(async { futures::poll!(&mut collection) })
|
||||
block_on(async { futures::poll!(&mut next) })
|
||||
else {
|
||||
panic!("native capacity recovery must not wait on the old account");
|
||||
};
|
||||
drop(collection);
|
||||
drop(next);
|
||||
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.raw_upstream_error().unwrap().as_str(), original_body);
|
||||
assert_eq!(
|
||||
error
|
||||
.client_visible_upstream_error()
|
||||
.and_then(|detail| detail.code()),
|
||||
(replacement && expected_attempts == 2).then_some("previous_response_not_found")
|
||||
);
|
||||
assert_eq!(
|
||||
error.continuation_unavailable_reason(),
|
||||
(replacement && expected_attempts == 2).then_some("account_rotated")
|
||||
);
|
||||
let contexts = provider.contexts.lock().unwrap();
|
||||
assert_eq!(contexts.len(), expected_attempts);
|
||||
if contexts.len() == 2 {
|
||||
@@ -2602,9 +2620,16 @@ fn native_capacity_above_wait_limit_rotates_before_requesting_client_replay() {
|
||||
);
|
||||
assert!(contexts[1].required_account().is_none());
|
||||
}
|
||||
let state = store.state.lock().unwrap();
|
||||
let finalization = &state.finalizations[0];
|
||||
assert_eq!(finalization.attempt_count, 1);
|
||||
assert_eq!(
|
||||
store.state.lock().unwrap().finalizations[0].attempt_count,
|
||||
1
|
||||
finalization.outcome,
|
||||
if committed_prelude {
|
||||
ExecutionOutcome::Incomplete
|
||||
} else {
|
||||
ExecutionOutcome::Failed
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -3402,7 +3427,16 @@ fn recovered_credential_retries_the_same_account_exactly_once() {
|
||||
CancellationToken::new(),
|
||||
))
|
||||
.expect("start execution");
|
||||
block_on(session.collect_uncommitted()).expect("recovered account succeeds");
|
||||
let (release, gate) = oneshot::channel();
|
||||
*store.intermediate_gate.lock().unwrap() = Some(gate);
|
||||
block_on(async {
|
||||
let mut collect = Box::pin(session.collect_uncommitted());
|
||||
assert!(futures::poll!(collect.as_mut()).is_pending());
|
||||
assert_eq!(provider.contexts.lock().unwrap().len(), 1);
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 1);
|
||||
release.send(()).unwrap();
|
||||
collect.await.expect("recovered account succeeds");
|
||||
});
|
||||
block_on(session.commit_downstream(Some(200))).expect("commit response");
|
||||
|
||||
let contexts = provider.contexts.lock().expect("contexts lock");
|
||||
@@ -5408,7 +5442,7 @@ fn delivered_prelude_allows_retry_without_control_until_non_replayable_delivery(
|
||||
fn cancellation_before_pending_delivery_commit_reaches_terminal_state() {
|
||||
let operation = generate_operation();
|
||||
let route_plan = plan(&operation);
|
||||
let (coordinator, store, _) = coordinator(vec![Script::Stream {
|
||||
let (coordinator, store, provider) = coordinator(vec![Script::Stream {
|
||||
account_id: "acct_first",
|
||||
items: complete_stream(None),
|
||||
}]);
|
||||
@@ -5429,7 +5463,15 @@ fn cancellation_before_pending_delivery_commit_reaches_terminal_state() {
|
||||
CommitRequirement::CommitBeforeDelivery
|
||||
);
|
||||
|
||||
block_on(session.cancel_and_finalize()).expect("cancel finalization");
|
||||
let (release, gate) = oneshot::channel();
|
||||
*store.send_gate.lock().unwrap() = Some(gate);
|
||||
block_on(async {
|
||||
let mut cancel = Box::pin(session.cancel_and_finalize());
|
||||
assert!(futures::poll!(cancel.as_mut()).is_pending());
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 1);
|
||||
release.send(()).unwrap();
|
||||
cancel.await.expect("cancel finalization");
|
||||
});
|
||||
|
||||
assert!(session.is_finalized());
|
||||
let state = store.state.lock().expect("store lock");
|
||||
@@ -5946,7 +5988,7 @@ fn interrupted_stream_failure_preserves_the_original_attempt_and_retry_decision(
|
||||
let mut resumed = Box::pin(session.next_event());
|
||||
assert!(futures::poll!(resumed.as_mut()).is_pending());
|
||||
assert_eq!(provider.contexts.lock().unwrap().len(), 1);
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 0);
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 1);
|
||||
release
|
||||
.send(())
|
||||
.expect("the original write still owns its receiver");
|
||||
@@ -6144,6 +6186,7 @@ fn charged_terminal_write_resumes_without_replacing_success_failure_or_send_fact
|
||||
assert!(futures::poll!(collect.as_mut()).is_pending());
|
||||
} else {
|
||||
session.collect_uncommitted().await.unwrap();
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 1);
|
||||
let mut commit = Box::pin(session.commit_downstream(Some(200)));
|
||||
assert!(futures::poll!(commit.as_mut()).is_pending());
|
||||
}
|
||||
@@ -6187,7 +6230,7 @@ fn charged_terminal_write_resumes_without_replacing_success_failure_or_send_fact
|
||||
fn deadline_after_commit_is_persisted_as_incomplete() {
|
||||
let operation = generate_operation();
|
||||
let route_plan = plan(&operation);
|
||||
let (coordinator, store, _) = coordinator(vec![Script::HangingStream {
|
||||
let (coordinator, store, provider) = coordinator(vec![Script::HangingStream {
|
||||
account_id: "acct_first",
|
||||
items: vec![Ok(GatewayEvent::Started(ResponseMeta::new(
|
||||
"response-1",
|
||||
@@ -6215,6 +6258,7 @@ fn deadline_after_commit_is_persisted_as_incomplete() {
|
||||
let error = block_on(session.next_event()).expect_err("deadline elapses mid-stream");
|
||||
|
||||
assert!(matches!(error, EngineError::Deadline));
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 1);
|
||||
let state = store.state.lock().expect("store lock");
|
||||
assert_eq!(state.finalizations[0].outcome, ExecutionOutcome::Incomplete);
|
||||
assert!(state.finalizations[0].committed);
|
||||
@@ -6249,7 +6293,12 @@ fn deadline_before_first_event_is_persisted_as_failed_attempt() {
|
||||
|
||||
#[test]
|
||||
fn global_location_and_group_fast_policy_reach_every_account_retry() {
|
||||
for mode in [FastMode::Default, FastMode::Enabled, FastMode::Disabled] {
|
||||
for mode in [
|
||||
FastMode::Default,
|
||||
FastMode::Enabled,
|
||||
FastMode::Ultrafast,
|
||||
FastMode::Disabled,
|
||||
] {
|
||||
let operation = generate_operation();
|
||||
let location = gateway_core::account::RequestLocation {
|
||||
timezone: "Asia/Tokyo".parse().unwrap(),
|
||||
@@ -6491,8 +6540,6 @@ fn connection_recovery_rotates_only_after_retrying_the_first_account() {
|
||||
assert_eq!(context.transport(), AttemptTransport::Fallback);
|
||||
}
|
||||
assert_eq!(store.state.lock().unwrap().intermediate_failures, 2);
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 2);
|
||||
drop(session);
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 3);
|
||||
}
|
||||
|
||||
@@ -6556,8 +6603,6 @@ fn connection_recovery_keeps_one_account_and_http_transport_until_success() {
|
||||
let state = store.state.lock().unwrap();
|
||||
assert_eq!(state.intermediate_failures, 3);
|
||||
assert_eq!(state.finalizations[0].outcome, ExecutionOutcome::Succeeded);
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 3);
|
||||
drop(session);
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 4);
|
||||
},
|
||||
)));
|
||||
@@ -6654,8 +6699,6 @@ fn connection_recovery_does_not_delay_existing_account_rotation() {
|
||||
let state = store.state.lock().unwrap();
|
||||
assert_eq!(state.intermediate_failures, 2);
|
||||
assert_eq!(state.finalizations[0].outcome, ExecutionOutcome::Succeeded);
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 2);
|
||||
drop(session);
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 3);
|
||||
}
|
||||
|
||||
@@ -6749,8 +6792,6 @@ fn connection_recovery_exhaustion_preserves_transport_failure() {
|
||||
store.state.lock().unwrap().finalizations[0].attempt_count,
|
||||
4
|
||||
);
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 3);
|
||||
drop(session);
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 4);
|
||||
}
|
||||
|
||||
@@ -7425,6 +7466,80 @@ fn transport_keepalive_preserves_pre_output_recovery_and_first_business_commit()
|
||||
assert!(state.finalizations[0].committed);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn atomic_http_failure_uses_wire_only_after_downstream_data_or_keepalive() {
|
||||
for (prelude, keepalive) in [(false, false), (true, false), (false, true)] {
|
||||
let operation = generate_operation();
|
||||
let route_plan = plan(&operation);
|
||||
let detail = json!({"code":"invalid_value", "message":"Invalid input."});
|
||||
let wire = json!({"type":"error","status":400,"error":detail});
|
||||
let mut items = Vec::new();
|
||||
if prelude {
|
||||
items.push(Ok(ProviderEvent::canonical(GatewayEvent::Started(
|
||||
ResponseMeta::new("response-1", "gpt-5"),
|
||||
))
|
||||
.with_replayable_prelude()));
|
||||
}
|
||||
items.push(Err(ProviderError::new(
|
||||
ProviderErrorKind::InvalidRequest,
|
||||
UpstreamSendState::Sent,
|
||||
)
|
||||
.with_status(400)
|
||||
.with_client_visible_upstream_response(ClientVisibleUpstreamResponse::new(
|
||||
400,
|
||||
Some(b"application/json".to_vec()),
|
||||
Bytes::from(json!({"error":detail}).to_string()),
|
||||
))
|
||||
.with_atomic_client_events(vec![ProviderEvent::wire(
|
||||
ProtocolWireEvent::json("openai", Some("error".into()), wire.clone()).unwrap(),
|
||||
)])));
|
||||
let (coordinator, store, _) = coordinator(vec![Script::ObservedStream {
|
||||
account_id: "acct_first",
|
||||
items,
|
||||
}]);
|
||||
let mut session = block_on(coordinator.start(
|
||||
model_request(&operation, SystemTime::now() + Duration::from_secs(30)),
|
||||
operation,
|
||||
route_plan,
|
||||
None,
|
||||
None,
|
||||
CancellationToken::new(),
|
||||
))
|
||||
.unwrap();
|
||||
let transport = session.response_commit();
|
||||
if prelude {
|
||||
block_on(session.next_event()).unwrap().unwrap();
|
||||
block_on(session.commit_downstream(Some(200))).unwrap();
|
||||
}
|
||||
if keepalive {
|
||||
assert!(transport.record(Some(200)));
|
||||
}
|
||||
if prelude || keepalive {
|
||||
let batch = block_on(session.next_event()).unwrap().unwrap();
|
||||
let events = batch.into_provider_events();
|
||||
assert_eq!(events.len(), 1);
|
||||
assert_eq!(events[0].wire_event().unwrap().data(), &wire);
|
||||
if keepalive {
|
||||
block_on(session.commit_downstream(Some(200))).unwrap();
|
||||
}
|
||||
}
|
||||
let EngineError::Provider(error) = block_on(session.next_event()).unwrap_err() else {
|
||||
panic!("expected the upstream bad request");
|
||||
};
|
||||
assert_eq!(
|
||||
error.client_visible_upstream_response().unwrap().status(),
|
||||
400
|
||||
);
|
||||
assert!(session.is_finalized());
|
||||
assert_eq!(transport.record(Some(200)), prelude || keepalive);
|
||||
let state = store.state.lock().unwrap();
|
||||
assert_eq!(state.attempts.len(), 1);
|
||||
assert_eq!(state.finalizations.len(), 1);
|
||||
assert_eq!(state.finalizations[0].committed, prelude || keepalive);
|
||||
assert_eq!(state.finalizations[0].upstream_status_code, Some(400));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transport_keepalive_records_late_failure_as_incomplete_http_200() {
|
||||
let operation = generate_operation();
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user