mirror of
https://github.com/zyycn/codex-proxy-rs.git
synced 2026-11-21 16:01:32 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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 |
@@ -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 自动打包原文件并计算摘要,宿主在安装时校验
|
||||
|
||||
## 开发
|
||||
|
||||
|
||||
@@ -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(())
|
||||
}
|
||||
|
||||
@@ -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")]
|
||||
|
||||
@@ -180,7 +180,7 @@ pub struct UsageFilter {
|
||||
pub enum UsageRecordStatus {
|
||||
#[default]
|
||||
Success,
|
||||
Error,
|
||||
Error(Option<UsageErrorOrigin>),
|
||||
All,
|
||||
}
|
||||
|
||||
@@ -814,6 +814,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 {
|
||||
|
||||
@@ -480,6 +480,7 @@ pub trait AccountGroupStore: Send + Sync {
|
||||
context: &MutationContext,
|
||||
) -> AdminStoreResult<AccountGroupMutation>;
|
||||
|
||||
/// 仍有 API Key 引用分组时返回 Conflict
|
||||
async fn delete_account_group(
|
||||
&self,
|
||||
command: DeleteAccountGroup,
|
||||
|
||||
@@ -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};
|
||||
@@ -159,8 +159,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(
|
||||
@@ -189,8 +188,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 +207,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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -314,7 +314,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"),
|
||||
}
|
||||
|
||||
@@ -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,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();
|
||||
|
||||
@@ -51,7 +51,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 +65,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
|
||||
@@ -507,11 +506,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 {
|
||||
|
||||
@@ -1092,7 +1092,7 @@ fn map_wire_error(error: WireValidationError) -> AdminError {
|
||||
"search" => "搜索内容过长或包含控制字符",
|
||||
"sortBy" => "排序字段不受支持",
|
||||
"sortDirection" => "排序方向必须为 asc(升序)或 desc(降序)",
|
||||
_ => "API Key 请求参数不合法",
|
||||
field => return AdminError::bad_request(format!("{field} 字段不合法")),
|
||||
};
|
||||
AdminError::bad_request(message)
|
||||
}
|
||||
|
||||
@@ -1410,7 +1410,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)
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ pub struct UsageQuery {
|
||||
pub client_api_key_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>,
|
||||
@@ -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(),
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -36,9 +35,7 @@ pub(super) enum RequestDecodeSource {
|
||||
pub struct OpenAiRequestHeaders {
|
||||
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>,
|
||||
@@ -62,9 +59,7 @@ impl OpenAiRequestHeaders {
|
||||
Self {
|
||||
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,
|
||||
@@ -120,22 +115,18 @@ 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());
|
||||
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",
|
||||
@@ -338,11 +329,7 @@ pub(super) fn decode_request_object(
|
||||
.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 protocol_context = request_headers.protocol_context(use_websocket, source);
|
||||
|
||||
let semantics =
|
||||
gateway_protocol::openai::codex_responses_request_semantics(&object, &protocol_context);
|
||||
@@ -381,16 +368,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,9 @@ 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");
|
||||
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";
|
||||
|
||||
@@ -215,7 +215,7 @@ async fn mutation_route_should_reject_unknown_json_fields() {
|
||||
"name": "Unknown field",
|
||||
"description": null,
|
||||
"color": "#2563EBFF",
|
||||
"providerKind": "openai"
|
||||
"unknownField": true
|
||||
})),
|
||||
true,
|
||||
)
|
||||
@@ -232,7 +232,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"
|
||||
})),
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -610,7 +610,7 @@ fn xai_profile_override_distinguishes_omission_from_explicit_inheritance() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generic_profile_overrides_accept_native_providers_and_reject_legacy_conflicts() {
|
||||
fn profile_overrides_accept_matching_native_values_and_reject_conflicts_and_unknown_providers() {
|
||||
let profile = json!({"preset":"desktop"});
|
||||
let xai = json!({"preset":"managed"});
|
||||
let request: CreateClientKeyRequest = serde_json::from_value(json!({
|
||||
@@ -676,7 +676,7 @@ fn generic_profile_overrides_accept_native_providers_and_reject_legacy_conflicts
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generic_profile_override_clear_rejects_conflicting_legacy_update() {
|
||||
fn clearing_an_xai_profile_override_conflicts_with_an_explicit_xai_value() {
|
||||
let request: UpdateClientKeyRequest = serde_json::from_value(json!({
|
||||
"id":"key_profile",
|
||||
"name":"profile",
|
||||
|
||||
@@ -513,6 +513,12 @@ mod provider {
|
||||
(Kind::Unsupported, 400, 40001, "该服务商不支持此操作"),
|
||||
(Kind::BadGateway, 502, 50201, "上游服务请求失败"),
|
||||
(Kind::Unavailable, 503, 50301, "服务商暂不可用"),
|
||||
(
|
||||
Kind::CredentialRefreshRequired,
|
||||
503,
|
||||
50301,
|
||||
"账号凭据需要刷新",
|
||||
),
|
||||
(
|
||||
Kind::Ambiguous,
|
||||
502,
|
||||
|
||||
@@ -1726,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",
|
||||
@@ -1820,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())
|
||||
@@ -1852,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
|
||||
@@ -2059,7 +2082,7 @@ 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 [
|
||||
for (elapsed_ms, expected, display) in [
|
||||
(None, json!(null), "—"),
|
||||
(
|
||||
Some(-1_000),
|
||||
@@ -2174,7 +2197,7 @@ 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["lastUsed"], display, "{zone}: {elapsed_ms:?}");
|
||||
assert_eq!(account["lastUsedAt"], json!(last_used_at));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -431,7 +431,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 +461,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]
|
||||
@@ -705,8 +704,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()
|
||||
@@ -919,7 +916,7 @@ fn global_profile_can_be_omitted_but_cannot_be_cleared() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn generic_global_profiles_decode_native_providers_and_reject_legacy_conflicts() {
|
||||
async fn global_profiles_decode_native_providers_and_reject_conflicting_openai_settings() {
|
||||
let mut body = update_body();
|
||||
body["providerRequestProfiles"] = json!({
|
||||
"openai":{"preset":"desktop"},
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -492,7 +492,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,32 @@ 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"}"#),
|
||||
);
|
||||
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())
|
||||
],
|
||||
[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>; 2]>>,
|
||||
initial_errors: Mutex<std::collections::VecDeque<EngineError>>,
|
||||
first_batch: Mutex<Option<CoordinatedEvent>>,
|
||||
session_drops: Arc<AtomicUsize>,
|
||||
@@ -437,6 +448,11 @@ 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"]
|
||||
.map(|key| context.get(key).and_then(Value::as_str).map(str::to_owned)),
|
||||
);
|
||||
self.trace
|
||||
.downstream_websocket_connection_ids
|
||||
.lock()
|
||||
@@ -1211,12 +1227,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 +1247,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 +1265,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();
|
||||
|
||||
@@ -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;
|
||||
@@ -1239,142 +1239,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 +1429,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 +1941,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 +2057,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 +2438,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]
|
||||
|
||||
@@ -643,6 +643,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 {
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -425,7 +425,7 @@ impl ProviderConnectionObservation {
|
||||
|
||||
/// 只供当前客户端请求使用的 HTTP 失败响应
|
||||
///
|
||||
/// 通常保留原始上游响应;Provider 也可为明确的恢复流程准备客户端投影
|
||||
/// Provider 可透传上游 HTTP 响应,也可按客户端协议构造错误正文
|
||||
/// 投影不能覆盖 [`ProviderError`] 中用于诊断和记账的真实上游事实
|
||||
///
|
||||
/// 该值不属于稳定诊断事实,不能进入日志或持久化
|
||||
@@ -833,7 +833,7 @@ impl ProviderError {
|
||||
self
|
||||
}
|
||||
|
||||
/// 附加仅可返回给当前请求方的原始上游 HTTP 失败响应
|
||||
/// 附加仅可返回给当前请求方的 HTTP 失败响应
|
||||
#[must_use]
|
||||
pub fn with_client_visible_upstream_response(
|
||||
mut self,
|
||||
@@ -1018,7 +1018,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()
|
||||
|
||||
@@ -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 } => {
|
||||
@@ -3402,7 +3402,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 +5417,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 +5438,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 +5963,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 +6161,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 +6205,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 +6233,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);
|
||||
@@ -6491,8 +6510,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 +6573,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 +6669,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 +6762,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 +7436,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();
|
||||
|
||||
@@ -1955,6 +1955,7 @@ impl NativeResponseTranslator for FailingNativeResponseTranslator {
|
||||
|
||||
struct FailingNativeResponseProvider {
|
||||
calls: Arc<AtomicUsize>,
|
||||
lease: Arc<()>,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
@@ -2010,7 +2011,7 @@ impl Provider for FailingNativeResponseProvider {
|
||||
GatewayEvent::Completed(response),
|
||||
)),
|
||||
]),
|
||||
(),
|
||||
Arc::clone(&self.lease),
|
||||
)
|
||||
.with_native_response_translator(FailingNativeResponseTranslator::default()))
|
||||
}
|
||||
@@ -2020,8 +2021,10 @@ impl Provider for FailingNativeResponseProvider {
|
||||
fn native_response_failure_after_downstream_commit_is_not_replayed() {
|
||||
block_on(async {
|
||||
let calls = Arc::new(AtomicUsize::new(0));
|
||||
let lease = Arc::new(());
|
||||
let provider_index = ProviderRegistry::new([Arc::new(FailingNativeResponseProvider {
|
||||
calls: Arc::clone(&calls),
|
||||
lease: Arc::clone(&lease),
|
||||
}) as Arc<dyn Provider>])
|
||||
.unwrap();
|
||||
let generation = super::extensions::reference("native-response-failure");
|
||||
@@ -2054,6 +2057,7 @@ fn native_response_failure_after_downstream_commit_is_not_replayed() {
|
||||
}));
|
||||
started.session.commit_downstream(Some(200)).await.unwrap();
|
||||
|
||||
let active_refs = Arc::strong_count(&lease);
|
||||
let error = started.session.next_event().await.unwrap_err();
|
||||
assert!(matches!(
|
||||
error,
|
||||
@@ -2061,6 +2065,7 @@ fn native_response_failure_after_downstream_commit_is_not_replayed() {
|
||||
if error.kind() == ProviderErrorKind::Protocol
|
||||
&& error.send_state() == UpstreamSendState::Sent
|
||||
));
|
||||
assert_eq!(Arc::strong_count(&lease), active_refs - 1);
|
||||
assert_eq!(calls.load(Ordering::SeqCst), 1);
|
||||
started.session.detach_finalize().await;
|
||||
});
|
||||
|
||||
@@ -69,7 +69,7 @@ fn privacy_rule_requires_steps_and_rejects_unknown_fields() {
|
||||
missing_steps.as_object_mut().unwrap().remove("steps");
|
||||
assert!(serde_json::from_value::<PrivacyRule>(missing_steps).is_err());
|
||||
let mut unknown_rule_field = value.clone();
|
||||
unknown_rule_field["scope"] = serde_json::json!("request_body");
|
||||
unknown_rule_field["unexpected"] = serde_json::json!(true);
|
||||
assert!(serde_json::from_value::<PrivacyRule>(unknown_rule_field).is_err());
|
||||
let mut unknown_step_field = value;
|
||||
unknown_step_field["steps"][0]["unexpected"] = serde_json::json!(true);
|
||||
|
||||
@@ -139,7 +139,7 @@ fn blocked_file_output_keeps_http_and_streaming_responsive_and_reserves_errors()
|
||||
);
|
||||
let (release, released) = std::sync::mpsc::channel();
|
||||
let reader = std::thread::spawn(move || {
|
||||
// 超时只用于让旧的同步实现能退出测试,接口必须在解除阻塞之前完成
|
||||
// 请求必须在读端放行前完成,超时让写入阻塞时的测试也能退出
|
||||
let _ = released.recv_timeout(Duration::from_secs(3));
|
||||
let mut body = String::new();
|
||||
fs::File::open(fifo)
|
||||
|
||||
@@ -327,7 +327,6 @@ async fn managed_http_enforces_the_callers_body_limit_before_sending() {
|
||||
.mount(&server)
|
||||
.await;
|
||||
let client = HttpClient::new().unwrap();
|
||||
// 覆盖旧固定上限之外的正文,并验证同一个客户端逐次使用调用方的预算
|
||||
let maximum = 17 * 1024 * 1024;
|
||||
for (size, limit, accepted) in [
|
||||
(maximum, maximum, true),
|
||||
|
||||
@@ -24,9 +24,9 @@ async fn invalid_update_state_should_keep_raw_details_out_of_public_error() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn update_status_should_reconcile_manual_deployment_without_reusing_legacy_success() {
|
||||
async fn manual_deployment_reports_the_running_version_and_preserves_the_last_operation() {
|
||||
let fixture = Fixture::new();
|
||||
fs::write(fixture.state(), r#"{"previousVersion":"3.14.0","currentVersion":"3.14.1","operation":{"operationId":"legacy","kind":"update","status":"succeeded","targetVersion":"3.14.1"}}"#).expect("legacy state");
|
||||
fs::write(fixture.state(), r#"{"previousVersion":"3.14.0","currentVersion":"3.14.1","operation":{"operationId":"completed-update","kind":"update","status":"succeeded","targetVersion":"3.14.1"}}"#).expect("completed update state");
|
||||
let mut config = fixture.config("https://api.github.com/repos");
|
||||
config.version = "3.15.0".to_owned();
|
||||
config.deployment_mode = "docker".to_owned();
|
||||
|
||||
@@ -645,7 +645,7 @@ async fn concurrent_calls_keep_their_wire_ids_monotonic() {
|
||||
}
|
||||
ready.wait().await;
|
||||
entered.await;
|
||||
// 旧实现会在 begin 前分配后续 ID;等其他线程进入该窗口后再释放首调用
|
||||
// 暂停首个 begin,给其他并发调用留出竞争发送顺序的时间
|
||||
tokio::time::sleep(Duration::from_millis(20)).await;
|
||||
callbacks.release();
|
||||
for call in calls {
|
||||
|
||||
@@ -542,8 +542,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
);
|
||||
if let Some(expected) = configuration.get("expected_fast_source") {
|
||||
let sources = &call.request.head.settings_sources["execution"];
|
||||
assert!(sources.get("disable_fast").is_none());
|
||||
assert!(sources["input"].get("disable_fast").is_none());
|
||||
assert_eq!(sources["input"]["fast_mode"], "default");
|
||||
assert_eq!(&sources["fast_mode"], expected);
|
||||
}
|
||||
|
||||
@@ -1,15 +1,9 @@
|
||||
# Gateway Plugin SDK
|
||||
|
||||
用于编写 Codex Proxy 插件的 Rust 合同与可选会话辅助。插件只依赖本包,不依赖宿主 Core、Admin、Store 或 Host。
|
||||
SDK 包版本为 `0.1.0`,独立于网关版本,处于实验阶段。能力是否可用取决于宿主支持、清单声明与实际注册
|
||||
Rust 插件只需依赖 `gateway-plugin-sdk`。SDK `0.1.0` 处于实验阶段,独立于网关发版。清单、进程协议和能力分别版本化,插件可用能力取决于宿主支持、清单声明与实际注册
|
||||
|
||||
清单、进程协议和各项能力分别版本化,当前版本与声明规则统一见[插件清单](docs/manifest.md)
|
||||
|
||||
开发顺序:编写[清单](docs/manifest.md) → 实现[能力处理器](docs/capabilities.md) → 用 [CLI](../../../apps/plugin-cli/README.md)
|
||||
打包 → [安装、配置与启用](../../../../docs/plugins.md)。完整可运行示例位于独立仓库
|
||||
`codex-proxy-plugins` 的 `examples/workbench`
|
||||
|
||||
使用 AI 协作开发时,可调用仓库技能 [`$cpr-plugin-dev`](../../../../.agents/skills/cpr-plugin-dev/SKILL.md),按任务定位合同、示例和验证入口
|
||||
编写[清单](docs/manifest.md) → 实现[处理器](docs/capabilities.md) → 用 [CLI 打包](../../../apps/plugin-cli/README.md) → [安装并启用](../../../../docs/plugins.md)。
|
||||
完整示例见 `codex-proxy-plugins/examples/workbench`,AI 协作入口见 [cpr-plugin-dev](../../../../.agents/skills/cpr-plugin-dev/SKILL.md)
|
||||
|
||||
## 模块与依赖
|
||||
|
||||
@@ -29,11 +23,7 @@ SDK 包版本为 `0.1.0`,独立于网关版本,处于实验阶段。能力
|
||||
| `call::management` | 管理 API、页面与资源声明、公开回调,以及 CLI 命令与待保存账号 |
|
||||
| `client` | 通过 `io` feature 开启的 `PluginBuilder`、会话、中间件与异步帧收发 |
|
||||
|
||||
`call/` 按业务能力组织数据,`message.rs` 定义消息与帧。已有请求链的请求/响应改写、协议转换和思考参数映射使用
|
||||
`call::middleware`;需要指定业务上游并解析结果时使用 `call::upstream_adapter`,作为洋葱链终端复用宿主账号与结算。
|
||||
下游 WebSocket 改写使用 `websocket` 中间件;上游发送前的请求准备使用 `outbound` 中间件,响应观察接口保留只读合同
|
||||
|
||||
插件使用的导入路径例如:
|
||||
`message.rs` 定义消息与帧。改写现有请求、响应或推理参数使用 `call::middleware`,其中 `websocket` 处理下游消息,`outbound` 处理上游发送前的请求。响应观察只读。需要自行选择业务上游并解析响应时使用 `call::upstream_adapter`
|
||||
|
||||
```rust
|
||||
use gateway_plugin_sdk::{Frame, Message, PluginFault};
|
||||
@@ -42,10 +32,8 @@ use gateway_plugin_sdk::call::{host::ModelEventBatch, model::ExecutionEvent};
|
||||
use gateway_plugin_sdk::client::{read_frame, write_frame};
|
||||
```
|
||||
|
||||
默认 feature 不引入异步运行时。`io` 除帧编解码外,还提供 `PluginBuilder`、
|
||||
`PluginSession::accept/run` 和 `PluginHandler`。组合插件从作者清单建立类型化方法,SDK 自动生成
|
||||
`plugin.register`;作者不需要复制贡献项或手写方法名、阶段和敏感载荷编解码。
|
||||
会话管理握手、有界并发、父调用关联、回调、流信用、取消、排空与关闭,容量与计时见[并发与期限](#并发与期限)
|
||||
默认 feature 不引入异步运行时。`io` 提供帧编解码、`PluginBuilder`、`PluginSession::accept/run` 和 `PluginHandler`。
|
||||
[类型化构建器](docs/capabilities.md#类型化作者入口)按清单注册处理器。会话管理握手、并发、父调用关联、回调、流信用、取消、排空与关闭,容量与计时见[并发与期限](#并发与期限)
|
||||
|
||||
`HostClient::scan_usage_records` 与 `query_usage_statistics` 提供用量明细扫描和完整分组统计,按批读取与关闭见[用量查询](docs/usage-data.md#完整扫描与分组统计)
|
||||
|
||||
@@ -149,9 +137,8 @@ session.run(handler).await?;
|
||||
|
||||
### 帧与会话
|
||||
|
||||
`CallContext.timeout_ms` 限定初始处理期限。宿主对由流或连接 owner 管理的调用设置 `resource_stream`:
|
||||
首个流式结果成功返回后,正文、会话及关联回调由资源取消与终态回收,不再沿用初始期限。
|
||||
普通调用继续使用原期限;资源模式仍保留并发容量、Credit、父调用关联及取消约束
|
||||
普通调用受 `CallContext.timeout_ms` 限制。对标记为 `resource_stream` 的调用,初始期限只到首个流式结果成功返回。
|
||||
其后的正文、会话及回调由资源终态或取消回收,仍受并发容量、Credit 和父调用约束
|
||||
|
||||
协议 v7 的帧头由大端 `u32` 元数据长度和大端 `u64` 载荷长度组成,随后依次传输 JSON 元数据和二进制载荷。
|
||||
元数据保留 64 KiB 上限;正文总量不由插件传输层额外限制。传输内部以 256 KiB 为单次 I/O 块,
|
||||
@@ -210,6 +197,4 @@ RUST_MIN_STACK=16777216 cargo +1.99.1 test --manifest-path backend/Cargo.toml -p
|
||||
RUST_MIN_STACK=16777216 RUSTDOCFLAGS='-D warnings' cargo +1.99.1 doc --manifest-path backend/Cargo.toml -p gateway-plugin-sdk --all-features --no-deps --locked
|
||||
```
|
||||
|
||||
源码中的类型和字段是当前合同;宿主接入、目录边界、内置 Provider 与 Runtime 集成验证见
|
||||
[系统架构](../../../../docs/architecture.md#31-插件扩展)。
|
||||
单元测试与文档构建不代替目标平台上的安装、配置及实际能力验证
|
||||
宿主集成见[系统架构](../../../../docs/architecture.md#31-插件扩展)。插件还须在目标平台验证安装、配置和实际功能
|
||||
|
||||
@@ -27,22 +27,15 @@ let plugin = PluginBuilder::from_json(include_bytes!("../plugin.json"))?
|
||||
session.run(plugin).await?;
|
||||
```
|
||||
|
||||
`PluginBuilder::from_json` 与 `cpr-plugin package` 使用同一个作者清单规范化入口。构建器负责:
|
||||
|
||||
- 自动响应 `plugin.register`,注册内容直接来自规范化后的清单
|
||||
- 按方法合同解码控制参数和二进制载荷,并在进入业务处理器前检查调用阶段
|
||||
- 保留 `TypedCall` 中的调用上下文、宿主客户端和取消信号
|
||||
- 按方法合同编码 `TypedReply`,流式结果继续复用有界 `ResponseStream`
|
||||
- 在 `build()` 时核对能力与必需处理器,避免清单与分派表漂移
|
||||
`PluginBuilder::from_json` 与打包 CLI 共用[作者清单入口](manifest.md),按清单响应 `plugin.register`,并在 `build()` 时核对必需处理器。
|
||||
构建器在分派前检查阶段、解码控制参数与载荷。`TypedCall` 携带上下文、宿主客户端及取消信号,结果编码为 `TypedReply`,流式结果使用有界 `ResponseStream`
|
||||
|
||||
`management`、`command_line` 和 `middleware` 是常用组合入口;其余能力使用
|
||||
`on(methods::..., handler)`。方法常量固定请求、响应、阶段与载荷位置,作者不手写 RPC 方法名。
|
||||
状态迁移不是 capability:清单任一 `state[]` 声明了 `migratesFrom` 时,注册
|
||||
`methods::STATE_MIGRATE`,构建器也会要求该处理器存在
|
||||
|
||||
类型化合同故意区分三种数据:普通控制元数据、可能敏感的 JSON 二进制载荷、原始字节。
|
||||
例如 CLI 参数、账号凭据和状态迁移批次使用二进制 JSON,管理 HTTP 正文保持原始字节。
|
||||
错误地把敏感载荷放进控制参数会在业务处理器运行前被拒绝
|
||||
控制参数只放普通元数据。CLI 参数、账号凭据和状态迁移批次使用二进制 JSON,管理 HTTP 正文使用原始字节。敏感载荷放进控制参数会在分派前被拒绝
|
||||
|
||||
## 请求上下文
|
||||
|
||||
@@ -252,9 +245,7 @@ SDK 不自动重试。超时或断连不能证明写入未提交;重试上限
|
||||
|
||||
### Key、模型与模型调用
|
||||
|
||||
模型调用方法:
|
||||
|
||||
- `host.keys.list`:只返回 `{id, name, enabled}`,不返回 Key 明文、前缀或隐藏 scope
|
||||
- `host.keys.list`:分页返回 `{id, name, enabled}`
|
||||
- `host.models.list`:以显式 `client_key_id` 查询该 Key 当前可见的模型目录
|
||||
- `host.model.execute[_stream]`:交给 Core 的路由、准入、租约、重试、账本和计费链路执行
|
||||
- `host.model.stream_read/close`:读取或关闭当前父调用创建的有界流
|
||||
@@ -266,9 +257,8 @@ SDK 不自动重试。超时或断连不能证明写入未提交;重试上限
|
||||
`invalid_input`,不启动模型执行。上游适配器的请求 `GPAQ` 也使用请求预算,响应事件仍受
|
||||
原有 8 MiB 编码上限、Runtime 缓冲预算及流控窗口限制
|
||||
|
||||
HTTP 和 request 中间件中的模型回调沿用有效请求设置;attempt、outbound、路由、调度、重试和上游适配器
|
||||
沿用该次模型请求冻结的正文预算。显式 Key 模型调用的预检与执行使用同一身份快照,
|
||||
同一父调用内复用 Key 时不受后续配置发布影响;父调用结束后重新解析身份才读取新配置,无需重新启用插件
|
||||
HTTP 和 request 中间件使用有效请求设置。attempt、outbound、路由、调度、重试及上游适配器沿用冻结的模型请求预算。
|
||||
显式 Key 的预检与执行共用父调用内的身份快照,后续发布的配置在新的父调用解析身份时生效
|
||||
|
||||
`ModelExecuteRequest.client_key_id` 可显式选择执行 Key,不与触发插件的父请求 Key 求交。省略时继承现有模型父请求身份;没有模型父请求的调用必须提供 Key。所选 Key 的启用状态、模型规则、账号组、并发和预算继续由 Core 执行
|
||||
嵌套调用与显式 Key 调用继承入口快照中的客户端 IP 和 User-Agent;没有 HTTP 入口的后台调用保持来源未知
|
||||
@@ -276,7 +266,7 @@ HTTP 和 request 中间件中的模型回调沿用有效请求设置;attempt
|
||||
模型事件使用 `call::model::ExecutionEvent` 的 `GPE2` 封套,将 canonical 事实、宿主事实和原始 wire 分段编码。`host` 包含费用及定价明细、完整上游观测、会话状态和改写来源;当前 wire 与原始 wire 都保留二进制字节;`WirePayload::WebSocket` 独立保留完整文本消息和可选 JSON 视图,不把未知文本改造成 SSE。读取或回传快照不触发第二次计费、续接提交或发送状态更新。
|
||||
非流式模型结果通过 `ModelEventBatch` 的 `HME1` 有界二进制批次返回;流式句柄只在创建它的父调用内有效。
|
||||
发起回调的插件实例在子调用中跳过,Core 在单次执行的子调用图内限制递归深度、总数和并发,避免插件调用自身形成环。
|
||||
显式 Key 的身份快照随父调用保留,每个模型执行独立计时并建立调用图;父调用结束会取消尚未完成的执行,连接空闲不消耗后续执行的期限
|
||||
每次模型执行独立计时并建立调用图。父调用结束会取消未完成的执行,连接空闲不消耗后续执行的期限
|
||||
|
||||
`host.affinity.lookup` 查询 Provider 已持久化的真实亲和键。命中只是账号偏好,
|
||||
后续执行仍重新检查当前 Key 规则、账号资格和租约
|
||||
@@ -288,16 +278,11 @@ HTTP 和 request 中间件中的模型回调沿用有效请求设置;attempt
|
||||
父调用取消或结束时宿主回收未关闭的流,SDK 不后台预读。取消一次 `read()` 等待后,再次读取会继续同一次操作,不丢弃已经返回的分块。
|
||||
账号上游使用同一正文对象,使用方式见[受管上游](upstream-adapters.md)
|
||||
|
||||
`HostClient::account_http(AccountHttpRequest { account_id, credential_revision, request }, body)` 使用指定账号的当前代理配置,
|
||||
经 `host.http.account` 返回同一 `HostHttpBody`。每次发送核对账号存在、启用和凭据版本,版本变化须由插件重新读取后决定下一步。
|
||||
请求 headers 与正文完全由插件提供;宿主不注入账号认证、不申请推理租约。无显式代理时使用部署默认出口,代理不可用时直接失败,不回退直连。
|
||||
这项能力与上游适配器的账号推理 HTTP 分开,适用于插件自行构造的账号网络请求
|
||||
`HostClient::account_http(AccountHttpRequest { account_id, credential_revision, request }, body)` 经 `host.http.account` 使用账号代理并返回 `HostHttpBody`。每次发送要求账号存在、已启用且凭据版本匹配。版本变化时须重新读取凭据。
|
||||
插件提供请求头与正文,宿主不注入账号认证或申请推理租约。无显式代理时使用部署默认出口,代理不可用时直接失败
|
||||
|
||||
`HostClient::proxy_http(ProxyHttpRequest { proxy_id, request }, body)` 经 `host.http.proxy` 使用指定代理的当前配置,
|
||||
返回同一响应头与 `HostHttpBody`,不要求账号存在或绑定代理,也不校验账号凭据版本。
|
||||
代理不存在、禁用或连接失败时返回错误,不回退直连;未测试或历史测试失败不阻止实际请求。
|
||||
每次发送重新读取代理,已发出的请求沿该次解析的出口完成,不锁定整个插件业务流程的代理配置。
|
||||
目标请求的 headers 与正文由插件提供,宿主不注入账号认证;代理连接所需认证由宿主处理
|
||||
`HostClient::proxy_http(ProxyHttpRequest { proxy_id, request }, body)` 经 `host.http.proxy` 使用指定代理,无需绑定账号,返回响应头与 `HostHttpBody`。
|
||||
宿主每次发送读取代理配置,已发出的请求沿该出口完成。代理不存在、禁用或连接失败时直接报错,历史测试结果不限制发送。插件提供目标请求头与正文,宿主仅处理代理认证
|
||||
|
||||
`HostClient::list_proxies(ProxyListRequest { page, page_size, search })` 经 `host.proxies.list` 查询代理目录。
|
||||
`page` 从 1 开始,`page_size` 为 1–200,`search` 可省略,默认空字符串,最多 256 字节且不能包含控制字符。
|
||||
@@ -387,7 +372,7 @@ let status = host.service::<SetResetCreditAutomation>((account_id, true)).await?
|
||||
|
||||
`HttpCall.request` 提供 method、URI(含 query)、HTTP version、完整多值 headers、`settings`、`timeout_ms` 和 `HttpBody`。
|
||||
调用发生在路由匹配之前,管理接口、模型接口和未知路径都可参与。`settings` 是同一请求快照中的宿主运行设置,
|
||||
插件改写后用于后续认证、版本检查、正文解压及执行;不新增设置业务的中间件入口。`timeout_ms` 使用宿主 HTTP 超时基线,
|
||||
插件改写后的设置用于认证、版本检查、正文解压及执行。`timeout_ms` 默认取宿主 HTTP 超时,
|
||||
插件可改写或用 `None` 清除;终端按有效值执行,不恢复宿主默认值。宿主快照不可用时 `settings` 为 `null`
|
||||
|
||||
`POST /v1/chat/completions` 在 HTTP 挂载中保留 Chat 请求与 Chat JSON/SSE 响应视图,
|
||||
@@ -756,7 +741,7 @@ keep、replace 或 delete;全部完成后宿主才原子提交新配置与状
|
||||
需要跳转到外部页面时,在插件页面的用户点击处理器中调用 `openExternal(url): Promise<void>`。
|
||||
地址必须是绝对 HTTPS URL,不能包含用户名或密码。宿主展示目标主机名和非默认端口,管理员可打开新标签页、复制链接或取消。
|
||||
Promise 成功只表示宿主已接受确认请求,不代表链接已打开;无用户操作、地址无效或已有链接待确认时,请求会被拒绝。
|
||||
切换页面会关闭确认框。兼容较早的 v2 宿主时,先检查 `typeof window.codexProxyPlugin.openExternal === 'function'`
|
||||
切换页面会关闭确认框。插件需要适配缺少此方法的宿主时,先检查 `typeof window.codexProxyPlugin.openExternal === 'function'`
|
||||
|
||||
普通模型请求使用标准 `Response` 接口:
|
||||
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
|
||||
[返回 SDK](../README.md) · [能力与回调](capabilities.md)
|
||||
|
||||
`Manifest` 对应包内 `plugin.json`。作者源清单由 `Manifest::from_author_slice` 读取,CLI 打包也调用同一入口;
|
||||
因此本地构建器、生成的安装清单和运行注册共享一份规范化结果
|
||||
`Manifest` 对应包内的 `plugin.json`。本地构建器与打包 CLI 都通过 `Manifest::from_author_slice` 校验并规范化作者清单
|
||||
|
||||
| 字段 | 规则 |
|
||||
| --- | --- |
|
||||
@@ -14,11 +13,10 @@
|
||||
| `configurationSchema` / `secretFields` | 普通设置的 schema / 单独保存的敏感字段 |
|
||||
| `main` / `resources` | 包内可执行文件 / 资源路径与 MIME 类型 |
|
||||
| `state` | [私有状态](capabilities.md#状态日志与迁移)的命名空间、schema 与配额 |
|
||||
| `package` | CLI 生成的协议版本、单一目标平台与文件摘要,不需作者手写 |
|
||||
| `package` | 作者清单须省略。CLI 填入协议版本、目标平台与文件摘要 |
|
||||
|
||||
作者清单不能包含 `package`。使用 `Manifest::from_author_slice()` 校验并规范化;直接反序列化的 `Manifest`
|
||||
不会执行作者清单的 ID 与固定阶段推导,声明的可选字段仍使用其默认值。打包后,`main` 和所有资源都必须进入 `package.files`,
|
||||
宿主校验包结构、目标平台与文件完整性,宿主范围和能力版本另行进行兼容检查。运行模式为 `trustedProcess`,信任边界见[完整信任](#完整信任)
|
||||
直接反序列化 `Manifest` 只应用可选字段默认值。需要推导 ID 和固定阶段时使用作者清单入口。
|
||||
打包后,`package.files` 须包含 `main` 与所有资源
|
||||
|
||||
宿主实际开放的合同由 [plugin-host-compatibility.json](../../runtime/plugin-host-compatibility.json)
|
||||
声明,SDK 的 `Capability::contract_versions()` 只表示 SDK 能描述的行为版本,不能代替宿主支持检查
|
||||
@@ -32,13 +30,9 @@
|
||||
|
||||
`capabilities` 使用扩展能力标识;Provider、模型和账号 ID 不属于这个集合
|
||||
|
||||
清单不接受 `permissions` 字段;插件按[完整信任](#完整信任)运行
|
||||
CLI 按 SDK 合同校验声明。安装包清单拒绝未知字段,并校验必需字段、类型与版本。进程通信见 [RPC 合同](../README.md#通信边界),已安装元数据的读取见[制品与发布](../../../../../docs/architecture.md#制品配置与发布)
|
||||
|
||||
RPC v7 的帧布局、编码与会话生命周期见[通信边界](../README.md#通信边界)。插件使用 SDK 构建,并由 CLI 生成包含协议版本的安装包
|
||||
|
||||
安装包清单校验与已安装元数据读取是两个边界:清单拒绝未知字段,并检查必需字段、类型和版本;
|
||||
数据库元数据的读取规则见[制品与发布](../../../../../docs/architecture.md#制品配置与发布),不改变清单或 RPC 合同。
|
||||
`engines.codex-proxy-rs` 声明插件所需的宿主范围。范围或已声明能力版本不匹配时不自动启动,管理员确认风险后可尝试本次启动;确认不跨宿主重启保留。包结构、清单格式、平台与 RPC 封装校验失败阻止加载,不能通过风险确认绕过。CLI 严格校验作者声明是否符合 SDK 支持的合同
|
||||
宿主版本超出 `engines.codex-proxy-rs` 范围或能力版本不匹配时,管理员可确认风险并尝试启动,确认仅对本次启动有效。包结构、文件完整性、清单格式、平台或 RPC 校验失败时无法加载
|
||||
|
||||
## 接口弃用
|
||||
|
||||
@@ -98,15 +92,11 @@ capability 固定并由工具生成:
|
||||
|
||||
安装并启用插件意味着信任其全部代码和行为。插件与宿主使用相同的系统身份,可以访问数据、凭据和网络;安全由安装者承担,宿主不提供插件安全沙箱
|
||||
|
||||
清单只声明处理器、配置和资源。宿主回调不需要权限声明,也不按调用阶段授予访问域;类型校验、期限、取消、实例 revision、事务及流资源生命周期仍然生效
|
||||
运行模式为 `trustedProcess`。清单不接受 `permissions`,宿主回调无需声明权限。类型、期限、取消、实例 revision、事务与流资源约束均生效
|
||||
|
||||
## 插件图标
|
||||
|
||||
`icon` 指定管理端展示的插件图标。值可以是一个包内文件路径,也可以是包含 `light`、`dark` 两个路径的对象。
|
||||
使用单个路径时,浅色和深色主题共用同一张图;使用主题对象时,管理端按当前主题选择,两项都必须填写。
|
||||
不填写 `icon` 时显示通用图标。图标路径不是远程 URL,也不接受内置图标名称
|
||||
|
||||
在 `plugin.json` 中同时设置 `icon` 和对应的 `resources` 项。以下是单图标配置片段:
|
||||
`icon` 可填包内路径(两种主题共用),或同时含 `light`、`dark` 路径的对象。每个文件均须在 `resources` 登记。省略 `icon` 时显示通用图标
|
||||
|
||||
```json
|
||||
{
|
||||
@@ -151,6 +141,4 @@ capability 固定并由工具生成:
|
||||
- SVG 使用 UTF-8 编码,根元素为 SVG 命名空间中的 `<svg>`,XML 节点不超过 16384 个;不使用 DTD 实体或 XML 处理指令
|
||||
- SVG 保留矢量、渐变、内联样式和 data URL 内嵌图片;图标不能依赖脚本、外部图片、外部样式或外部字体
|
||||
|
||||
将图标文件与清单一同交给 [插件 CLI](../../../../apps/plugin-cli/README.md) 打包。工具收集 `resources` 声明的文件,
|
||||
生成 `package.files` 摘要;宿主在安装时验证图标内容。管理端通过 [图标读取接口](../../../../../docs/api.md#12-插件管理)
|
||||
以图片方式展示,不将 SVG 源码插入页面 HTML
|
||||
[插件 CLI](../../../../apps/plugin-cli/README.md) 打包图标,宿主在安装时校验内容。管理端通过[图标接口](../../../../../docs/api.md#图标读取)以图片展示 SVG
|
||||
|
||||
@@ -2,9 +2,8 @@
|
||||
|
||||
[返回 SDK](../README.md) · [清单声明](manifest.md#扩展项简写) · [能力与回调](capabilities.md)
|
||||
|
||||
`upstream_adapter` v5 是现有生成请求洋葱链的终端,只挂到内置 `openai` / `xai`,不替换独立图像或压缩端点。
|
||||
插件定义 Base URL、业务路径、请求编码和响应解析;宿主继续持有身份、ACL、选号、租约、凭据、代理、重试和账本。
|
||||
普通请求或响应加工使用 [middleware](capabilities.md#洋葱中间件)
|
||||
`upstream_adapter` v5 处理内置 `openai` / `xai` 的生成请求,独立图像和压缩端点使用原生实现。
|
||||
插件负责 Base URL、路径、请求编码和响应解析。宿主负责身份、ACL、选号、租约、凭据、代理、重试和账本。仅需加工请求或响应时使用[中间件](capabilities.md#洋葱中间件)
|
||||
|
||||
## 声明与绑定
|
||||
|
||||
@@ -26,18 +25,13 @@
|
||||
| `models` | 公开请求模型的精确匹配,空集合沿用绑定范围 |
|
||||
| `account_ids` | 上游账号 ID 范围,必填;空数组匹配所有账号 |
|
||||
|
||||
实例绑定沿用 Key、账号组、Provider、模型范围,只允许一次 `upstream` 绑定和 `reject` 故障策略。
|
||||
多个声明命中时按发布顺序选择首个适配器,插件负责声明的业务含义。适配器未命中时仍使用原生上游,
|
||||
已命中但不可用时拒绝请求。安装、配置、启停和升级使用已有插件管理
|
||||
实例只允许一个 `upstream` 绑定,故障策略为 `reject`。Key、账号组、Provider 和模型限定请求范围,插件注册的 `account_ids` 只能进一步收窄账号范围
|
||||
|
||||
账号范围只收窄绑定,不能扩大 Key、分组或模型权限。宿主先排除适配器不可用或认证类型不兼容的账号,
|
||||
再沿用原调度、亲和和租约;取得账号后从同一发布代次解析适配器。不同账号可以为同一公开模型选择不同适配器,
|
||||
未匹配账号保持原生执行,重叠声明仍按发布顺序取首个匹配。
|
||||
前置账号范围已匹配的可用适配器不受后置兜底声明故障影响。
|
||||
xAI 原生编码不支持的请求仍可选择匹配的适配器账号;原生账号会在取得租约前被排除
|
||||
同一模型可按账号使用不同适配器。每个账号按发布顺序取首个匹配声明,后续声明的故障不影响已匹配的适配器。未匹配时使用原生上游,命中但不可用时拒绝执行
|
||||
|
||||
账号范围由插件注册结果提供,不改变管理端 binding 中 `accountGroupIds` 的请求作用域语义。
|
||||
停用或卸载实例会移除其绑定;维护时须先停用依赖该适配器的账号并排空请求,账号范围不提供永久依赖约束。
|
||||
取得租约前,宿主排除适配器不可用或认证不兼容的账号。xAI 原生编码不支持请求时,仅排除原生账号,匹配的适配器账号仍可使用。宿主按亲和与调度策略取得租约后,从同一发布代次解析适配器
|
||||
|
||||
停用或卸载实例会移除绑定。维护前先停用依赖该适配器的账号并排空请求
|
||||
|
||||
能力、清单、进程 RPC 和宿主兼容声明分别校验各自版本,见[清单](manifest.md)。
|
||||
Provider、业务协议和传输名称不能写入宿主兼容清单的 capability / RPC 版本位置
|
||||
@@ -65,9 +59,8 @@ while let Some(bytes) = body.read().await? {
|
||||
}
|
||||
```
|
||||
|
||||
正文与普通 `HostClient::http` 共用 `read`、`collect(maximum_bytes)` 和 `close`;每次读取至多 64 KiB,
|
||||
不后台预读,不向作者暴露流 ID。提前关闭及收集超限会释放正文,父调用结束或取消时宿主兜底回收。
|
||||
JSON 可有界收集,SSE 应增量解析,不能把读取块当成完整事件。HTTP 版本、字节头及 trailers 的读取见[网络](capabilities.md#网络)
|
||||
正文每次 `read` 至多 64 KiB,SDK 按需读取并管理流 ID。JSON 可用 `collect(maximum_bytes)`,SSE 须跨读取块增量解析。
|
||||
显式 `close`、收集超限或父调用结束、取消时释放正文。HTTP 版本、字节头及 trailers 见[网络](capabilities.md#网络)
|
||||
|
||||
WebSocket 使用 `call.host.upstream_websocket(request).await?`,返回
|
||||
`UpstreamWebSocketUpgrade::Connected { http_version, headers, connection }` 或 `Rejected { status, http_version, headers, body, trailers }`。
|
||||
@@ -102,9 +95,7 @@ let (message, ()) = tokio::try_join!(
|
||||
观测事件和该次 HTTP 的读流、关流不单独阻断;HTTP200 流内报错、WebSocket 拒绝及其他状态码不适用。
|
||||
Core 仍复核续接归属、外部副作用、交付与重试预算;这不保证一定换号,也不证明完整信任插件绕过宿主执行的操作可重放
|
||||
|
||||
初始 RPC 回复受插件处理预算约束,默认上限为 120 秒;持续接收与解析应放在返回的流中。
|
||||
初始流式回复后,流的寿命遵循模型请求期限与取消,受管 HTTP 正文每次读取单独计时。
|
||||
持续有数据的流不会因初始 RPC 预算耗尽而结束,单次读取超时、客户端取消和流结束仍会回收资源
|
||||
初始 RPC 回复默认限时 120 秒。持续接收与解析应放在返回的流中,遵循模型请求期限。受管 HTTP 正文每次读取单独计时,读取超时、客户端取消或流结束时回收资源
|
||||
|
||||
用量提交标准 `Usage`,费用由内置 Provider 按已冻结的上游模型与价格计算,响应回显模型不切换计价。
|
||||
`observation.service_tier` 记录上游回显的最新档位,允许上游在完成时确定实际值;OpenAI 计价沿用请求策略,回显档位仅作观测。
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
| `WriteAggregates` | `usage.aggregates.write` | 按聚合键批量覆盖或删除统计贡献 |
|
||||
| `EvaluatePricing` | `pricing.evaluate` | 根据当前价目或指定价格试算,逐项返回 `estimated` 或 `not_estimable` |
|
||||
|
||||
明细列表只列请求记录,聚合贡献参与统计但不生成请求明细。完整分组与全局总量使用下方统计读取,趋势使用既有统计查询
|
||||
请求列表只含明细。全局与分组统计包含聚合贡献,趋势由统计查询提供
|
||||
|
||||
## 完整扫描与分组统计
|
||||
|
||||
@@ -93,21 +93,21 @@ async fn read_statistics(
|
||||
|
||||
## 聚合维护
|
||||
|
||||
`UsageAggregateKey` 由 `bucket_start_ms`、`dimensions` 和调用方的 `key` 共同构成。相同完整键的 `put` 覆盖旧值,重复提交相同内容不会累计;改变时间、维度或 `key` 会形成另一个目标,需要移动时由调用方删除旧键
|
||||
`UsageAggregateKey` 由 `bucket_start_ms`、`dimensions` 和调用方的 `key` 组成。`put` 按完整键覆盖,重复提交不会累加。移动记录须写入新键并删除旧键
|
||||
|
||||
`dimensions` 支持 Provider、模型、账号、Key、操作和传输类型。`counts` 的键由 `UsageMetric` 定义,包括请求次数、各终态次数、已完成次数和各项 Token;`costs` 按币种保存十进制金额。计数与费用允许负值,供调用方表达统计修正
|
||||
|
||||
聚合按 `bucket_start_ms` 这个归属时间纳入 `[start_ms, end_ms)` 查询及趋势时间桶,宿主不自动摊分。导入日汇总时,调用方选择该日的归属时间;需要小时分布时,调用方自行提供小时数据
|
||||
贡献按 `bucket_start_ms` 计入查询区间与趋势时间桶。日汇总不会自动摊到各小时,需要小时分布时须写入小时数据
|
||||
|
||||
查询只按实际提供的维度匹配,缺少的维度不会自动补全。未提供某个维度的贡献参与总量,完整分组读取使用独立未归属分组表达,其他维度查询只计入实际提供该维度的贡献。请求 ID、响应 ID、状态码、请求结果及文字搜索等明细筛选不包含聚合贡献,聚合也不生成延迟样本、尝试次数或请求列表页数。原生统计的无符号计数与非负金额若因贡献合计超出表示范围,查询返回错误,由调用方修正输入
|
||||
贡献计入原生总量、趋势、维度统计和账号用量,维度匹配与越界处理见[分组与覆盖](#分组与覆盖)。按请求或响应 ID、状态码、请求结果或文字筛选时排除贡献。贡献不产生延迟样本、尝试次数或请求列表页数
|
||||
|
||||
聚合贡献参与原生总量、趋势、维度统计和账号用量,统计重建保留这些数据。贡献由调用方删除,不随请求历史保留任务清理;明细沿用请求历史保留策略
|
||||
统计重建保留贡献,须由写入方删除。请求历史保留策略只清理明细
|
||||
|
||||
## 费用试算与写回
|
||||
|
||||
`EvaluatePricing` 使用 Provider 的原生单请求 Token 计价公式,支持当前价目和调用方指定的价格覆盖。OpenAI 与 xAI 提供此能力;不支持的 Provider、缺少必要 Token、图像用量或无法确定的价目返回 `not_estimable`
|
||||
`EvaluatePricing` 按 OpenAI 或 xAI 的单请求公式,用当前价目或指定价格试算 Token 费用。须提供输入、输出、缓存读取、缓存写入和图像 Token,纯文本的图像 Token 填零。Provider 不支持、用量不全或价格未知时返回 `not_estimable`
|
||||
|
||||
试算输入需要明确输入、输出、缓存读取、缓存写入以及图像 Token;纯文本用量的图像 Token 填零。结果只包含 Token 费用,不自动处理图像、工具或其他附加费用,也不写入历史
|
||||
试算不含图像、工具等附加费用,也不写入请求记录
|
||||
|
||||
调用方可查询明细、选择目标、逐条试算,再通过 `WriteRecords::Update` 写回费用。补全缺失时只处理 `unavailable`;全部覆盖时自行选择整个范围。日汇总若缺少逐请求分布,长上下文档位和逐请求舍入可能无法复现,计算方法由调用方决定
|
||||
|
||||
@@ -121,4 +121,4 @@ async fn read_statistics(
|
||||
- 存储操作共享宿主观测连接与执行预算,超时、锁等待或取消会结束该次执行并释放资源
|
||||
- 错误类型包括 `invalid_input`、`not_found`、`conflict`、`capacity_exceeded`、`unavailable`,分批读取还区分 `cancelled` 与 `timeout`
|
||||
|
||||
响应丢失时,调用方按稳定 ID 或聚合键查询、重试覆盖。宿主不保存任务、来源登记、幂等回执、调整历史或撤销计划;调用方负责来源映射、业务去重、重试策略、并发写入协调和恢复。服务输入不能指定审计主体,宿主从当前调用身份生成普通变更审计
|
||||
响应丢失时,按稳定 ID 或聚合键查询后重试覆盖。调用方负责来源映射、去重、并发写入、进度、调整记录和恢复。宿主按当前调用身份记录变更审计,不提供幂等回执
|
||||
|
||||
@@ -6,6 +6,38 @@ const MULTI_AGENT_MODE_OPEN_TAG: &str = "<multi_agent_mode>";
|
||||
const MULTI_AGENT_MODE_CLOSE_TAG: &str = "</multi_agent_mode>";
|
||||
const PROACTIVE_MULTI_AGENT_MODE_PREFIX: &str = "Proactive multi-agent delegation is active.";
|
||||
|
||||
/// 标记实际入站的 Responses WS 帧,独立于出站传输偏好
|
||||
pub const WEBSOCKET_FRAME_CONTEXT_KEY: &str = "websocket_frame";
|
||||
|
||||
/// 当前帧携带的完整元数据仅用于解释正文,握手或 HTTP 头保留各自的出站值
|
||||
#[must_use]
|
||||
pub fn observed_turn_metadata<'a>(
|
||||
body: &'a Map<String, Value>,
|
||||
header: Option<&'a str>,
|
||||
websocket_frame: bool,
|
||||
) -> Option<&'a str> {
|
||||
if websocket_frame && let Some(frame) = client_metadata_string(body, "x-codex-turn-metadata") {
|
||||
return Some(frame);
|
||||
}
|
||||
header
|
||||
}
|
||||
|
||||
/// HTTP 请求使用本次请求头,WS 帧使用本次正文或首次握手头
|
||||
#[must_use]
|
||||
pub fn context_turn_metadata<'a>(
|
||||
body: &'a Map<String, Value>,
|
||||
context: &'a Map<String, Value>,
|
||||
) -> Option<&'a str> {
|
||||
observed_turn_metadata(
|
||||
body,
|
||||
context.get("turn_metadata").and_then(Value::as_str),
|
||||
context
|
||||
.get(WEBSOCKET_FRAME_CONTEXT_KEY)
|
||||
.and_then(Value::as_bool)
|
||||
== Some(true),
|
||||
)
|
||||
}
|
||||
|
||||
/// 各 Codex 端点共用的显式根会话身份
|
||||
/// 连接边界的 session_id 优先,
|
||||
/// 正文及官方 metadata 只做回退;不从 turn、window 或请求内容猜测归属
|
||||
@@ -24,9 +56,7 @@ pub fn codex_account_session_id(
|
||||
client_metadata_string(body, "session_id")
|
||||
.map(str::to_owned)
|
||||
.or_else(|| request_turn_metadata(body).and_then(turn_metadata_session_id))
|
||||
.or_else(|| {
|
||||
non_empty_string(context.get("turn_metadata")).and_then(turn_metadata_session_id)
|
||||
})
|
||||
.or_else(|| context_turn_metadata(body, context).and_then(turn_metadata_session_id))
|
||||
.or_else(|| non_empty_string(body.get("session_id")).map(str::to_owned))
|
||||
.or_else(|| codex_session_id(body, context))
|
||||
}
|
||||
@@ -40,9 +70,7 @@ pub fn codex_account_thread_id(
|
||||
client_metadata_string(body, "thread_id")
|
||||
.map(str::to_owned)
|
||||
.or_else(|| request_turn_metadata(body).and_then(turn_metadata_thread_id))
|
||||
.or_else(|| {
|
||||
non_empty_string(context.get("turn_metadata")).and_then(turn_metadata_thread_id)
|
||||
})
|
||||
.or_else(|| context_turn_metadata(body, context).and_then(turn_metadata_thread_id))
|
||||
.or_else(|| codex_thread_id(body, context))
|
||||
}
|
||||
|
||||
@@ -64,7 +92,7 @@ pub fn codex_turn_id(body: &Map<String, Value>, context: &Map<String, Value>) ->
|
||||
client_metadata_string(body, "turn_id")
|
||||
.map(str::to_owned)
|
||||
.or_else(|| request_turn_metadata(body).and_then(turn_metadata_turn_id))
|
||||
.or_else(|| non_empty_string(context.get("turn_metadata")).and_then(turn_metadata_turn_id))
|
||||
.or_else(|| context_turn_metadata(body, context).and_then(turn_metadata_turn_id))
|
||||
.or_else(|| codex_identity_field(body, context, "turn_id"))
|
||||
}
|
||||
|
||||
@@ -90,7 +118,8 @@ fn codex_identity_field(
|
||||
.or_else(|| client_metadata_string(body, field))
|
||||
.map(str::to_owned)
|
||||
.or_else(|| {
|
||||
let metadata = non_empty_string(context.get("turn_metadata"))
|
||||
let metadata = context_turn_metadata(body, context)
|
||||
.filter(|value| !value.trim().is_empty())
|
||||
.or_else(|| request_turn_metadata(body))?;
|
||||
let metadata: Value = serde_json::from_str(metadata).ok()?;
|
||||
non_empty_string(metadata.get(field)).map(str::to_owned)
|
||||
@@ -127,7 +156,7 @@ pub fn codex_responses_request_semantics(
|
||||
) -> CodexResponsesRequestSemantics {
|
||||
codex_responses_request_semantics_with_turn_metadata(
|
||||
body,
|
||||
context.get("turn_metadata").and_then(Value::as_str),
|
||||
context_turn_metadata(body, context),
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
/// OpenAI 线协议载荷与事件的协议标识
|
||||
pub const OPENAI_PROTOCOL: &str = "openai";
|
||||
|
||||
/// 原生 Responses 客户端入口
|
||||
pub const RESPONSES_PATH: &str = "/v1/responses";
|
||||
|
||||
/// Codex Responses Lite 的 HTTP 请求头
|
||||
pub const X_OPENAI_INTERNAL_CODEX_RESPONSES_LITE_HEADER: &str =
|
||||
"x-openai-internal-codex-responses-lite";
|
||||
@@ -21,9 +24,10 @@ mod headers;
|
||||
pub mod sse;
|
||||
|
||||
pub use codex::{
|
||||
CodexResponsesRequestSemantics, codex_account_session_id, codex_account_thread_id,
|
||||
codex_responses_request_semantics, codex_responses_request_semantics_with_turn_metadata,
|
||||
codex_session_id, codex_thread_id, codex_turn_id, turn_metadata_session_id,
|
||||
CodexResponsesRequestSemantics, WEBSOCKET_FRAME_CONTEXT_KEY, codex_account_session_id,
|
||||
codex_account_thread_id, codex_responses_request_semantics,
|
||||
codex_responses_request_semantics_with_turn_metadata, codex_session_id, codex_thread_id,
|
||||
codex_turn_id, context_turn_metadata, observed_turn_metadata, turn_metadata_session_id,
|
||||
turn_metadata_thread_id, turn_metadata_turn_id,
|
||||
};
|
||||
pub use headers::{
|
||||
|
||||
@@ -32,7 +32,7 @@ pub fn request_observation(
|
||||
OPENAI_PROTOCOL => {
|
||||
let semantics = codex_responses_request_semantics_with_turn_metadata(
|
||||
body,
|
||||
context.get("turn_metadata").and_then(Value::as_str),
|
||||
crate::openai::context_turn_metadata(body, context),
|
||||
previous_reasoning_effort_override,
|
||||
);
|
||||
RequestObservation {
|
||||
|
||||
@@ -454,7 +454,10 @@ impl AccountGroupStore for PgAccountGroupRepository {
|
||||
.await
|
||||
.map_err(|source| unavailable("check account group delete conflict", source))?;
|
||||
if exists {
|
||||
Err(conflict(command.id.as_str()))
|
||||
Err(conflict(
|
||||
command.id.as_str(),
|
||||
ConflictKind::InvalidTransition,
|
||||
))
|
||||
} else {
|
||||
Err(not_found_store(command.id.as_str()))
|
||||
}
|
||||
@@ -681,11 +684,15 @@ fn require_one(rows: u64, id: &str) -> StoreResult<()> {
|
||||
}
|
||||
|
||||
fn map_group_write_error(error: sqlx::Error, id: &str) -> StoreError {
|
||||
if error
|
||||
.as_database_error()
|
||||
.is_some_and(|database| database.is_unique_violation())
|
||||
if let Some(database) = error.as_database_error()
|
||||
&& database.is_unique_violation()
|
||||
{
|
||||
conflict(id)
|
||||
let kind = if database.constraint() == Some("account_groups_name_uq") {
|
||||
ConflictKind::DuplicateName
|
||||
} else {
|
||||
ConflictKind::InvalidTransition
|
||||
};
|
||||
conflict(id, kind)
|
||||
} else {
|
||||
unavailable("write account group", error)
|
||||
}
|
||||
@@ -703,12 +710,12 @@ fn not_found_store(id: &str) -> StoreError {
|
||||
}
|
||||
}
|
||||
|
||||
fn conflict(id: &str) -> StoreError {
|
||||
fn conflict(id: &str, kind: ConflictKind) -> StoreError {
|
||||
StoreError::Conflict {
|
||||
source: None,
|
||||
entity: ENTITY,
|
||||
id: id.to_owned(),
|
||||
kind: ConflictKind::InvalidTransition,
|
||||
kind,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -130,18 +130,7 @@ pub(crate) fn push_usage_filter(
|
||||
pub(crate) const USAGE_LIST_RECORD_SELECT: &str =
|
||||
"select mr.id, mr.outcome, mr.provider_error_code as error_code, mr.error_message, mr.client_status_code, mr.client_api_key_name_snapshot as client_api_key_name, mr.endpoint, mr.client_transport, mr.requested_model_id,
|
||||
mr.error_kind, mr.upstream_status_code,
|
||||
exists (
|
||||
select 1 from (values
|
||||
(saved_error.body -> 'code'),
|
||||
(saved_error.body -> 'type'),
|
||||
(saved_error.body -> 'message')
|
||||
) as field(value)
|
||||
where jsonb_typeof(field.value) = 'string' and field.value <> '\"\"'::jsonb
|
||||
) as has_upstream_error,
|
||||
case when saved_upstream.body ->> 'type' = 'websocket.close'
|
||||
and jsonb_typeof(saved_upstream.body -> 'code') = 'number'
|
||||
then saved_upstream.body ->> 'code'
|
||||
end as websocket_close_code,
|
||||
facts.has_upstream_error, facts.websocket_close_code,
|
||||
mr.continuation_unavailable_reason, mr.upstream_connection_exit_reason,
|
||||
(mr.recovery_request_id is not null and mr.recovered_at is not null) as recovered,
|
||||
mr.provider_kind, mr.provider_account_ref,
|
||||
@@ -164,17 +153,36 @@ pub(crate) const USAGE_LIST_RECORD_SELECT: &str =
|
||||
mr.reasoning_effort, mr.reasoning_preset, mr.subagent_kind, mr.compact,
|
||||
mr.started_at
|
||||
from model_request_observations mr
|
||||
cross join lateral (
|
||||
cross join lateral ";
|
||||
|
||||
// 列表与筛选共用已保存上游事实的解码,兼容早期的空值和非 JSON 错误详情
|
||||
const SAVED_ERROR_FACTS: &str = "(select exists (
|
||||
select 1 from (values
|
||||
(saved_error.body -> 'code'),
|
||||
(saved_error.body -> 'type'),
|
||||
(saved_error.body -> 'message')
|
||||
) as field(value)
|
||||
where jsonb_typeof(field.value) = 'string' and field.value <> '\"\"'::jsonb
|
||||
) as has_upstream_error,
|
||||
case when saved_upstream.body ->> 'type' = 'websocket.close'
|
||||
and jsonb_typeof(saved_upstream.body -> 'code') = 'number'
|
||||
and (saved_upstream.body ->> 'code') ~ '^[0-9]{1,5}$'
|
||||
and (saved_upstream.body -> 'code') <= '65535'::jsonb
|
||||
then saved_upstream.body ->> 'code'
|
||||
end as websocket_close_code
|
||||
from lateral (
|
||||
select case when pg_input_is_valid(mr.error_details, 'jsonb') then
|
||||
case when jsonb_typeof(mr.error_details::jsonb -> 'upstream') = 'string'
|
||||
and pg_input_is_valid(mr.error_details::jsonb ->> 'upstream', 'jsonb')
|
||||
then (mr.error_details::jsonb ->> 'upstream')::jsonb
|
||||
end
|
||||
end as body
|
||||
-- 保留子查询边界,让错误字段和关闭码复用同一行已解码的上游正文
|
||||
offset 0
|
||||
) saved_upstream
|
||||
cross join lateral (
|
||||
select coalesce(saved_upstream.body -> 'error', saved_upstream.body #> '{response,error}') as body
|
||||
) saved_error";
|
||||
) saved_error) facts";
|
||||
|
||||
pub(crate) const USAGE_RECORD_DETAIL_SELECT: &str =
|
||||
"select mr.id, mr.client_api_key_ref, mr.config_revision,
|
||||
@@ -261,7 +269,7 @@ async fn projected_usage_counts(
|
||||
query: &UsageRecordQuery,
|
||||
) -> StoreResult<Option<BTreeMap<DateTime<Utc>, u64>>> {
|
||||
use admin_observability::UsageRecordStatus;
|
||||
if query.status == UsageRecordStatus::Error {
|
||||
if matches!(query.status, UsageRecordStatus::Error(_)) {
|
||||
return Ok(None);
|
||||
}
|
||||
let Some(mut counts) = snapshot
|
||||
@@ -351,7 +359,9 @@ async fn list_usage_record_items_at<'e, E: sqlx::Executor<'e, Database = Postgre
|
||||
statement.push(" offset ");
|
||||
statement.push_bind(offset);
|
||||
statement.push(") ");
|
||||
statement.push(USAGE_LIST_RECORD_SELECT);
|
||||
statement
|
||||
.push(USAGE_LIST_RECORD_SELECT)
|
||||
.push(SAVED_ERROR_FACTS);
|
||||
statement.push(
|
||||
" join candidate_page page on page.id = mr.id order by page.started_at desc, page.id desc",
|
||||
);
|
||||
@@ -504,7 +514,7 @@ fn usage_counts_query(
|
||||
},
|
||||
);
|
||||
}
|
||||
if branch > 0 || status == UsageRecordStatus::Error {
|
||||
if branch > 0 || matches!(status, UsageRecordStatus::Error(_)) {
|
||||
push_non_success_count_filter(&mut statement, filter);
|
||||
} else {
|
||||
push_usage_filter(&mut statement, filter, "mr");
|
||||
@@ -744,8 +754,11 @@ fn push_usage_record_status(
|
||||
use admin_observability::UsageRecordStatus;
|
||||
match status {
|
||||
UsageRecordStatus::Success => push_completed_usage_fact_filter(statement, "mr"),
|
||||
UsageRecordStatus::Error => {
|
||||
UsageRecordStatus::Error(origin) => {
|
||||
statement.push(" and mr.outcome in ('failed', 'incomplete')");
|
||||
if let Some(origin) = origin {
|
||||
push_usage_error_origin(statement, origin);
|
||||
}
|
||||
}
|
||||
UsageRecordStatus::All => {
|
||||
// 成功沿用用量边界,未成功记录保留真实生命周期,不把取消与执行中标为错误
|
||||
@@ -756,3 +769,52 @@ fn push_usage_record_status(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn push_usage_error_origin(
|
||||
statement: &mut QueryBuilder<Postgres>,
|
||||
origin: admin_observability::UsageErrorOrigin,
|
||||
) {
|
||||
use admin_observability::{UsageErrorEvidence::*, UsageErrorOrigin};
|
||||
statement.push(" and case");
|
||||
for &(evidence, result) in UsageErrorOrigin::RULES {
|
||||
statement.push(" when ");
|
||||
match evidence {
|
||||
ProviderErrorCode => {
|
||||
statement.push("mr.provider_error_code <> ''");
|
||||
}
|
||||
UpstreamStatusAtLeast(min) => {
|
||||
statement
|
||||
.push("mr.upstream_status_code >= ")
|
||||
.push_bind(i32::from(min));
|
||||
}
|
||||
SavedUpstreamErrorOrClose => {
|
||||
statement.push("mr.error_details is not null and (select facts.has_upstream_error or facts.websocket_close_code is not null from lateral ")
|
||||
.push(SAVED_ERROR_FACTS).push(")");
|
||||
}
|
||||
ErrorKind(values) => {
|
||||
statement
|
||||
.push("mr.error_kind = any(")
|
||||
.push_bind(values)
|
||||
.push(")");
|
||||
}
|
||||
Continuation(values) => {
|
||||
statement
|
||||
.push("mr.continuation_unavailable_reason = any(")
|
||||
.push_bind(values)
|
||||
.push(")");
|
||||
}
|
||||
ConnectionExit(values) => {
|
||||
statement
|
||||
.push("mr.upstream_connection_exit_reason = any(")
|
||||
.push_bind(values)
|
||||
.push(")");
|
||||
}
|
||||
}
|
||||
statement.push(" then ").push_bind(result.as_str());
|
||||
}
|
||||
statement
|
||||
.push(" else ")
|
||||
.push_bind(UsageErrorOrigin::Unknown.as_str())
|
||||
.push(" end = ")
|
||||
.push_bind(origin.as_str());
|
||||
}
|
||||
|
||||
@@ -406,7 +406,7 @@ async fn health_counters_match_for_rollups_and_facts_without_counting_recovery_o
|
||||
sqlx::query("update model_requests set recovered_at=completed_at,recovery_request_id='health_success',recovery_attempt_count=1,request_observation_json=request_observation_json||jsonb_build_object('recovery',jsonb_build_object('retryDelayMs',0,'totalLatencyMs',1)) where id='health_recovered'")
|
||||
.execute(&database.pool).await.unwrap();
|
||||
drain_analytics(&database.pool).await;
|
||||
// 模拟旧桶曾把取消的 499 计入调用方错误,无需重建也应得到相同结果
|
||||
// 即使已存储的桶把 499 计入调用方错误,查询也应将其归为取消
|
||||
sqlx::raw_sql("update request_analytics_15m set counts=jsonb_set(counts,'{caller_error_count}','1') where outcome='cancelled'; update request_analytics_1h set counts=jsonb_set(counts,'{caller_error_count}','1') where outcome='cancelled'")
|
||||
.execute(&database.pool).await.unwrap();
|
||||
let repo = observability_repository(&database.pool);
|
||||
@@ -930,7 +930,7 @@ async fn interrupted_quarters_restart_changed_versions_and_publish_one_consisten
|
||||
.execute(&database.pool)
|
||||
.await
|
||||
.unwrap();
|
||||
// 独立重建请求改变版本,验证旧前缀废弃后仍可恢复,普通源变更不再重启整桶
|
||||
// 显式重建会更换版本,Worker 需要丢弃已处理的前缀并恢复构建
|
||||
sqlx::query("update request_analytics_pending_buckets set change_version=nextval('request_analytics_change_version')")
|
||||
.execute(&database.pool).await.unwrap();
|
||||
drop(worker);
|
||||
@@ -1034,7 +1034,7 @@ async fn backfill_coverage_uses_contiguous_cursor_instead_of_old_corrected_facts
|
||||
] {
|
||||
seed(&database.pool, id, now - TimeDelta::hours(age), &[]).await;
|
||||
}
|
||||
// 模拟迁移后历史队列为空,随后最旧请求发生单点修正
|
||||
// 清空待处理队列后只修正最早的请求,验证中间的时间段仍需回填
|
||||
sqlx::query("delete from request_analytics_pending_requests")
|
||||
.execute(&database.pool)
|
||||
.await
|
||||
|
||||
@@ -175,7 +175,7 @@ async fn usage_error_origins_agree_for_saved_errors_without_failure_status() {
|
||||
.list_usage_records(UsageRecordQuery {
|
||||
range,
|
||||
filter: UsageRecordFilter::default(),
|
||||
status: admin_observability::UsageRecordStatus::All,
|
||||
status: admin_observability::UsageRecordStatus::Error(Some(origin)),
|
||||
current_page: 1,
|
||||
page_size: ObservabilityPageSize::new(10).unwrap(),
|
||||
})
|
||||
@@ -282,7 +282,7 @@ async fn usage_error_origins_preserve_saved_failure_and_connection_boundaries()
|
||||
.list_usage_records(UsageRecordQuery {
|
||||
range,
|
||||
filter: UsageRecordFilter::default(),
|
||||
status: admin_observability::UsageRecordStatus::All,
|
||||
status: admin_observability::UsageRecordStatus::Error(Some(origin)),
|
||||
current_page: 1,
|
||||
page_size: ObservabilityPageSize::new(10).unwrap(),
|
||||
})
|
||||
@@ -993,10 +993,10 @@ async fn usage_account_filter_matches_only_historical_names_and_emails() {
|
||||
let repo = observability_repository(&database.pool);
|
||||
for (status, search, page, total, expected_id) in [
|
||||
(Success, r"_%\", 1, 1, Some("req_observe_success")),
|
||||
(Error, "team", 1, 1, Some("req_observe_failed")),
|
||||
(Error(None), "team", 1, 1, Some("req_observe_failed")),
|
||||
(All, "alice", 1, 2, Some("req_observe_failed")),
|
||||
(All, "alice", 2, 2, Some("req_observe_success")),
|
||||
(Error, "@example", 1, 0, None),
|
||||
(Error(None), "@example", 1, 0, None),
|
||||
(All, "ALICE", 1, 0, None),
|
||||
(All, "alice%ops", 1, 0, None),
|
||||
(All, "public-model", 1, 0, None),
|
||||
|
||||
@@ -460,7 +460,7 @@ async fn all_status_counts_keep_filters_delivery_rules_and_time_boundaries() {
|
||||
vec![],
|
||||
),
|
||||
] {
|
||||
for status in [UsageRecordStatus::All, UsageRecordStatus::Error] {
|
||||
for status in [UsageRecordStatus::All, UsageRecordStatus::Error(None)] {
|
||||
let expected = expected
|
||||
.iter()
|
||||
.copied()
|
||||
@@ -584,7 +584,7 @@ async fn request_status_pages_share_one_request_timeline() {
|
||||
.unwrap();
|
||||
for (status, expected) in [
|
||||
(UsageRecordStatus::Success, vec!["status_f", "status_a"]),
|
||||
(UsageRecordStatus::Error, vec!["status_c", "status_b"]),
|
||||
(UsageRecordStatus::Error(None), vec!["status_c", "status_b"]),
|
||||
(
|
||||
UsageRecordStatus::All,
|
||||
vec![
|
||||
@@ -619,7 +619,7 @@ async fn request_status_pages_share_one_request_timeline() {
|
||||
}
|
||||
let page = repo
|
||||
.list_usage_records(UsageRecordQuery {
|
||||
status: UsageRecordStatus::Error,
|
||||
status: UsageRecordStatus::Error(None),
|
||||
range,
|
||||
filter: UsageRecordFilter {
|
||||
search: Some("model_status_b".into()),
|
||||
@@ -647,3 +647,123 @@ async fn request_status_pages_share_one_request_timeline() {
|
||||
assert_eq!(errors.len(), 2);
|
||||
database.close().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn error_origin_filters_matching_requests_before_counting_and_paging() {
|
||||
use gateway_admin::model::observability::{UsageErrorOrigin::*, UsageRecordStatus};
|
||||
|
||||
let Some(database) = TestDatabase::create("usage_error_origin_pages").await else {
|
||||
return;
|
||||
};
|
||||
let start = Utc::now();
|
||||
let repository = observability_repository(&database.pool);
|
||||
let range = ObservabilityRange::new(start, start + TimeDelta::hours(1)).unwrap();
|
||||
let saved =
|
||||
|body: serde_json::Value| serde_json::json!({"upstream": body.to_string()}).to_string();
|
||||
for (minute, (id, outcome, kind, status, details, model)) in [
|
||||
("edge_0", "failed", "internal_error", None, None, "main"),
|
||||
(
|
||||
"edge_1",
|
||||
"failed",
|
||||
"internal_error",
|
||||
Some(503_i32),
|
||||
None,
|
||||
"main",
|
||||
),
|
||||
("edge_2", "incomplete", "transport", Some(200), None, "main"),
|
||||
(
|
||||
"edge_3",
|
||||
"incomplete",
|
||||
"invalid_request",
|
||||
None,
|
||||
Some("old plain text".into()),
|
||||
"main",
|
||||
),
|
||||
(
|
||||
"edge_4",
|
||||
"failed",
|
||||
"internal_error",
|
||||
None,
|
||||
Some(saved(
|
||||
serde_json::json!({"response":{"error":{"message":"sample"}}}),
|
||||
)),
|
||||
"main",
|
||||
),
|
||||
(
|
||||
"edge_5",
|
||||
"failed",
|
||||
"internal_error",
|
||||
None,
|
||||
Some(saved(
|
||||
serde_json::json!({"type":"websocket.close","code":1012}),
|
||||
)),
|
||||
"other",
|
||||
),
|
||||
(
|
||||
"edge_6",
|
||||
"failed",
|
||||
"internal_error",
|
||||
None,
|
||||
Some(saved(
|
||||
serde_json::json!({"type":"websocket.close","code":65536}),
|
||||
)),
|
||||
"main",
|
||||
),
|
||||
("edge_7", "cancelled", "internal_error", None, None, "main"),
|
||||
("edge_8", "succeeded", "internal_error", None, None, "main"),
|
||||
]
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
{
|
||||
seed(
|
||||
&database.pool,
|
||||
id,
|
||||
start + TimeDelta::minutes(minute as i64),
|
||||
&[],
|
||||
)
|
||||
.await;
|
||||
sqlx::query("update model_requests set outcome=$2, error_kind=$3, upstream_status_code=$4, error_details=$5, requested_model_id=$6 where id=$1")
|
||||
.bind(id).bind(outcome).bind(kind).bind(status).bind(details).bind(model)
|
||||
.execute(&database.pool).await.unwrap();
|
||||
}
|
||||
for (origin, model, ids) in [
|
||||
(Proxy, None, vec!["edge_6", "edge_0"]),
|
||||
(Upstream, None, vec!["edge_5", "edge_4", "edge_1"]),
|
||||
(Connection, None, vec!["edge_2"]),
|
||||
(Unknown, None, vec!["edge_3"]),
|
||||
(Upstream, Some("main"), vec!["edge_4", "edge_1"]),
|
||||
(Proxy, Some("other"), vec![]),
|
||||
] {
|
||||
for current_page in 1..=ids.len() + 1 {
|
||||
let page = repository
|
||||
.list_usage_records(UsageRecordQuery {
|
||||
range,
|
||||
status: UsageRecordStatus::Error(Some(origin)),
|
||||
filter: UsageRecordFilter {
|
||||
model_contains: model.map(str::to_owned),
|
||||
..Default::default()
|
||||
},
|
||||
current_page: current_page as u32,
|
||||
page_size: ObservabilityPageSize::new(1).unwrap(),
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(page.total, ids.len() as u64, "{origin:?} / {model:?}");
|
||||
let items: Vec<_> = page
|
||||
.items
|
||||
.iter()
|
||||
.map(|item| (item.id.as_str(), item.error_origin()))
|
||||
.collect();
|
||||
let expected: Vec<_> = ids
|
||||
.get(current_page - 1)
|
||||
.map(|id| (*id, origin))
|
||||
.into_iter()
|
||||
.collect();
|
||||
assert_eq!(
|
||||
items, expected,
|
||||
"{origin:?} / {model:?} / page {current_page}"
|
||||
);
|
||||
}
|
||||
}
|
||||
database.close().await;
|
||||
}
|
||||
|
||||
@@ -59,10 +59,10 @@ tokio-rustls = { version = "=0.26.4", default-features = false }
|
||||
tokio-util = { version = "0.7.18", features = ["rt"] }
|
||||
tower-service = "0.3.3"
|
||||
tower-layer = "0.3.3"
|
||||
# 官方 Codex WebSocket 需要上游尚未发布的 proxy 与 permessage-deflate 实现,固定到已审计提交。
|
||||
tokio-tungstenite = { git = "https://github.com/openai-oss-forks/tokio-tungstenite", rev = "0e5b2d73aa18dd9f0a50ee9ff199d5aef7594186", features = ["proxy", "rustls-tls-native-roots"] }
|
||||
# 官方 fork 支持代理连接和分帧消息的 permessage-deflate,两个提交须配套
|
||||
tokio-tungstenite = { git = "https://github.com/openai-oss-forks/tokio-tungstenite", rev = "7447c427513cfe685f236e1ac0513c4ea3dd9c0b", features = ["proxy", "rustls-tls-native-roots"] }
|
||||
tracing = "0.1.44"
|
||||
tungstenite = { git = "https://github.com/openai-oss-forks/tungstenite-rs", rev = "4fffad30fe373adbdcffab9545e9e9bf4f2fc19f", features = ["deflate", "proxy"] }
|
||||
tungstenite = { git = "https://github.com/openai-oss-forks/tungstenite-rs", rev = "bc589d684755a8c02da0fc7e58d0861cbfc306f1", features = ["deflate", "proxy"] }
|
||||
url = "2.5.8"
|
||||
uuid = { version = "1.23.3", features = ["v4", "v7"] }
|
||||
# 静态链接 XZ 解码器,不要求部署环境安装 liblzma。
|
||||
@@ -73,8 +73,8 @@ zstd = "=0.14.3"
|
||||
zip = { version = "9.0.4", default-features = false, features = ["deflate-flate2"] }
|
||||
|
||||
[target.'cfg(target_os = "linux")'.dependencies]
|
||||
openssl = "=0.10.80"
|
||||
openssl-sys = { version = "=0.9.116", features = ["vendored"] }
|
||||
openssl = "=0.10.82"
|
||||
openssl-sys = { version = "=0.9.118", features = ["vendored"] }
|
||||
|
||||
[dev-dependencies]
|
||||
gateway-host = { path = "../../gateway-host" }
|
||||
|
||||
@@ -265,8 +265,7 @@ impl ProviderAdmin for OpenAiAdminProvider {
|
||||
}
|
||||
|
||||
fn plan_type_display(&self, plan_type: &str) -> String {
|
||||
// Pro 系列按套餐类型区分展示,其他套餐遵循官方 KnownPlan::display_name
|
||||
// 只映射展示名称,原始套餐值与未知套餐保持不变
|
||||
// Pro 三档沿用管理端短名,其余已知套餐使用 Codex /status 的名称
|
||||
match plan_type.to_ascii_lowercase().as_str() {
|
||||
"free" => "Free",
|
||||
"go" => "Go",
|
||||
@@ -274,16 +273,17 @@ impl ProviderAdmin for OpenAiAdminProvider {
|
||||
"pro" => "Pro",
|
||||
"prolite" => "ProLite",
|
||||
"promax" => "ProMax",
|
||||
"team" => "Team",
|
||||
"self_serve_business_prolite" => "Self Serve Business ProLite",
|
||||
"self_serve_business_usage_based" => "Self Serve Business Usage Based",
|
||||
"business" => "Business",
|
||||
"ent26" | "enterprise" | "hc" => "Enterprise",
|
||||
"team" | "self_serve_business_usage_based" => "Business",
|
||||
"self_serve_business_prolite" => "Business Premium",
|
||||
"business" | "ent26" | "enterprise" | "hc" | "enterprise_cbp_usage_based" => {
|
||||
"Enterprise"
|
||||
}
|
||||
"enterprise_cbp_automation" => "Enterprise (Automation)",
|
||||
"enterprise_cbp_usage_based" => "Enterprise CBP Usage Based",
|
||||
"edu" | "education" => "Edu",
|
||||
"edu_plus" => "Edu Plus",
|
||||
"edu_pro" => "Edu Pro",
|
||||
"sci" => "Academic Researchers",
|
||||
"sci_plus" => "Academic Researchers Plus",
|
||||
_ => plan_type,
|
||||
}
|
||||
.to_owned()
|
||||
|
||||
@@ -8,7 +8,9 @@ use url::Url;
|
||||
|
||||
use crate::credential::CodexQuotaRefreshPolicy;
|
||||
use crate::transport::session::{CodexSessionIdentity, CodexSessionIdentityError};
|
||||
use crate::transport::websocket::CodexWebSocketPoolConfig;
|
||||
use crate::transport::websocket::{
|
||||
CodexWebSocketPoolConfig, DEFAULT_FAST_PATH_BUDGET_MS, WEBSOCKET_CONNECT_TIMEOUT,
|
||||
};
|
||||
use crate::{
|
||||
OFFICIAL_CODEX_BASE_URL,
|
||||
credential::token_client::{
|
||||
@@ -165,26 +167,24 @@ impl CodexApiConfig {
|
||||
|
||||
/// Codex Responses WebSocket pool 的 Provider-owned 启动设置
|
||||
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
|
||||
#[serde(default)]
|
||||
pub struct CodexWebSocketPoolSettings {
|
||||
pub enabled: bool,
|
||||
pub max_age_ms: u64,
|
||||
pub max_connecting: usize,
|
||||
#[serde(default = "default_websocket_pool_idle_timeout_ms")]
|
||||
pub fast_path_budget_ms: u64,
|
||||
pub idle_timeout_ms: u64,
|
||||
pub stream_idle_timeout_ms: u64,
|
||||
}
|
||||
|
||||
const fn default_websocket_pool_idle_timeout_ms() -> u64 {
|
||||
300_000
|
||||
}
|
||||
|
||||
impl Default for CodexWebSocketPoolSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
enabled: true,
|
||||
max_age_ms: 55 * 60 * 1000,
|
||||
max_connecting: 8,
|
||||
idle_timeout_ms: default_websocket_pool_idle_timeout_ms(),
|
||||
fast_path_budget_ms: DEFAULT_FAST_PATH_BUDGET_MS,
|
||||
idle_timeout_ms: 300_000,
|
||||
stream_idle_timeout_ms: 300_000,
|
||||
}
|
||||
}
|
||||
@@ -192,6 +192,13 @@ impl Default for CodexWebSocketPoolSettings {
|
||||
|
||||
impl CodexWebSocketPoolSettings {
|
||||
fn validate(&self) -> Result<(), OpenAiConfigError> {
|
||||
if self.fast_path_budget_ms == 0
|
||||
|| Duration::from_millis(self.fast_path_budget_ms) > WEBSOCKET_CONNECT_TIMEOUT
|
||||
{
|
||||
return Err(OpenAiConfigError::InvalidField(
|
||||
"openai.ws_pool.fast_path_budget_ms",
|
||||
));
|
||||
}
|
||||
if self.max_age_ms == 0 || self.max_connecting == 0 || self.idle_timeout_ms == 0 {
|
||||
return Err(OpenAiConfigError::InvalidField("openai.ws_pool"));
|
||||
}
|
||||
@@ -203,6 +210,7 @@ impl CodexWebSocketPoolSettings {
|
||||
enabled: self.enabled,
|
||||
max_age: Duration::from_millis(self.max_age_ms),
|
||||
max_connecting: self.max_connecting,
|
||||
fast_path_budget: Duration::from_millis(self.fast_path_budget_ms),
|
||||
idle_timeout: Duration::from_millis(self.idle_timeout_ms),
|
||||
stream_idle_timeout: (self.stream_idle_timeout_ms != 0)
|
||||
.then(|| Duration::from_millis(self.stream_idle_timeout_ms)),
|
||||
|
||||
@@ -33,7 +33,7 @@ use crate::transport::{CodexBackendClient, CodexCatalogModel, CodexRequestContex
|
||||
|
||||
const MAX_RESPONSE_ETAG_BYTES: usize = 256;
|
||||
const ACCOUNT_CATALOG_CACHE_TTL: Duration = Duration::from_secs(5 * 60);
|
||||
const CATALOG_REFRESH_AFTER: Duration = Duration::from_secs(30 * 60);
|
||||
const CATALOG_REFRESH_AFTER: Duration = Duration::from_hours(1);
|
||||
const MAX_ACCOUNT_CATALOG_MODELS: usize = 2_048;
|
||||
const MODEL_CATALOG_TIMEOUT: Duration = Duration::from_secs(15);
|
||||
|
||||
@@ -137,14 +137,31 @@ impl CatalogContent {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AccountEntry {
|
||||
observed_at: SystemTime,
|
||||
content: Arc<CatalogContent>,
|
||||
etag: Option<String>,
|
||||
// 目录返回不同版本时,触发这次查询的标记只用于去重
|
||||
trigger_etag: Option<String>,
|
||||
// 响应只确认这份目录的刷新期限,正文观测时间仍用于并发发布和持久化
|
||||
etag_confirmed_at: std::sync::Mutex<Option<tokio::time::Instant>>,
|
||||
}
|
||||
|
||||
type Entries = BTreeMap<CodexCatalogScope, AccountEntry>;
|
||||
impl AccountEntry {
|
||||
fn recognizes_etag(&self, etag: &str) -> bool {
|
||||
self.etag.as_deref() == Some(etag) || self.trigger_etag.as_deref() == Some(etag)
|
||||
}
|
||||
|
||||
fn is_fresh(&self, now: SystemTime) -> bool {
|
||||
now.duration_since(self.observed_at)
|
||||
.is_ok_and(|age| age < CATALOG_REFRESH_AFTER)
|
||||
|| self.etag_confirmed_at.lock().is_ok_and(|confirmed| {
|
||||
confirmed.is_some_and(|at| at.elapsed() < CATALOG_REFRESH_AFTER)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type Entries = BTreeMap<CodexCatalogScope, Arc<AccountEntry>>;
|
||||
|
||||
pub struct CodexCredentialCatalogSnapshot {
|
||||
models: Vec<CodexCatalogModel>,
|
||||
@@ -551,7 +568,7 @@ impl CodexCredentialCatalogService {
|
||||
revision: account.revision(),
|
||||
plan: account.plan_type().map(str::to_owned),
|
||||
observed_at,
|
||||
etag: etag.clone().or(fetched.etag),
|
||||
etag: fetched.etag.or_else(|| etag.clone()),
|
||||
};
|
||||
if !self
|
||||
.repository
|
||||
@@ -577,7 +594,9 @@ impl CodexCredentialCatalogService {
|
||||
AccountEntry {
|
||||
observed_at,
|
||||
content,
|
||||
trigger_etag: etag.filter(|tag| Some(tag) != observed.etag.as_ref()),
|
||||
etag: observed.etag,
|
||||
etag_confirmed_at: Default::default(),
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
@@ -37,11 +37,13 @@ impl CodexCredentialCatalogService {
|
||||
};
|
||||
entries.insert(
|
||||
scope,
|
||||
AccountEntry {
|
||||
Arc::new(AccountEntry {
|
||||
observed_at: a.observed_at,
|
||||
content: content.clone(),
|
||||
etag: a.etag,
|
||||
},
|
||||
trigger_etag: None,
|
||||
etag_confirmed_at: Default::default(),
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -119,15 +121,21 @@ impl CodexCredentialCatalogService {
|
||||
.cache
|
||||
.write()
|
||||
.map_err(|_| CodexCredentialCatalogError::Cache)?;
|
||||
if state.pending.get(&scope).map(String::as_str) == Some(etag)
|
||||
|| !state.pending.contains_key(&scope)
|
||||
&& state
|
||||
.snapshot
|
||||
.as_ref()
|
||||
.and_then(|s| s.entries.get(&scope))
|
||||
.and_then(|e| e.etag.as_deref())
|
||||
== Some(etag)
|
||||
let pending = state.pending.get(&scope);
|
||||
if pending.is_some_and(|tag| tag == etag) {
|
||||
return Ok(false);
|
||||
}
|
||||
if let Some(entry) = state.snapshot.as_ref().and_then(|s| s.entries.get(&scope))
|
||||
&& entry.recognizes_etag(etag)
|
||||
{
|
||||
// 已收到变更时,迟到的旧标记不能覆盖待刷新版本
|
||||
if pending.is_none() && entry.etag.as_deref() == Some(etag) {
|
||||
let mut confirmed_at = entry
|
||||
.etag_confirmed_at
|
||||
.lock()
|
||||
.map_err(|_| CodexCredentialCatalogError::Cache)?;
|
||||
*confirmed_at = Some(tokio::time::Instant::now());
|
||||
}
|
||||
return Ok(false);
|
||||
}
|
||||
state.pending.insert(scope, etag.to_owned());
|
||||
@@ -166,7 +174,7 @@ impl CodexCredentialCatalogService {
|
||||
.get(&scope)
|
||||
.is_none_or(|old| old.observed_at < entry.observed_at)
|
||||
{
|
||||
entries.insert(scope, entry);
|
||||
entries.insert(scope, Arc::new(entry));
|
||||
}
|
||||
}
|
||||
self.publish(revision, entries)?;
|
||||
@@ -205,7 +213,7 @@ impl CodexCredentialCatalogService {
|
||||
return true;
|
||||
};
|
||||
// 发布其他账号时保留通知,只有该账号更新的观测才能确认 ETag
|
||||
entry.etag.as_deref() != Some(tag)
|
||||
!entry.recognizes_etag(tag)
|
||||
|| old
|
||||
.as_ref()
|
||||
.and_then(|s| s.entries.get(scope))
|
||||
@@ -278,10 +286,7 @@ impl CodexCredentialCatalogService {
|
||||
.into_iter()
|
||||
.filter(|(scope, _)| {
|
||||
pending.contains_key(scope)
|
||||
|| !entries.get(scope).is_some_and(|e| {
|
||||
now.duration_since(e.observed_at)
|
||||
.is_ok_and(|age| age < CATALOG_REFRESH_AFTER)
|
||||
})
|
||||
|| !entries.get(scope).is_some_and(|entry| entry.is_fresh(now))
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
if due.is_empty() {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! AttemptContext 驱动的 Codex 账号选择与 Redis lease port
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{BTreeSet, HashMap};
|
||||
use std::fmt;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::{Duration, SystemTime};
|
||||
@@ -10,11 +10,11 @@ use gateway_core::account::CredentialRevision;
|
||||
use gateway_core::error::ErrorSource;
|
||||
|
||||
use gateway_core::account::{
|
||||
AccountAffinity, AccountCandidate, AccountCapacitySnapshot, AccountErrorReason,
|
||||
AccountFeedbackStats, AccountModelAccessMode, AccountQuotaSignals, AccountRuntimeSignals,
|
||||
AccountSchedulingBlocker, AccountSelectionContext, AccountSelectionPolicy, AccountSelector,
|
||||
AccountStatus, CredentialState, PreferredAccountSelection, ProviderAccount, ProviderAccountId,
|
||||
QuotaEvidence, RotationStrategy,
|
||||
AccountAffinity, AccountCandidate, AccountCapacitySnapshot, AccountCooldown,
|
||||
AccountCooldownKind, AccountErrorReason, AccountFeedbackStats, AccountModelAccessMode,
|
||||
AccountQuotaSignals, AccountRuntimeSignals, AccountSchedulingBlocker, AccountSelectionContext,
|
||||
AccountSelectionPolicy, AccountSelector, AccountStatus, CredentialState,
|
||||
PreferredAccountSelection, ProviderAccount, ProviderAccountId, QuotaEvidence, RotationStrategy,
|
||||
};
|
||||
use gateway_core::concurrency::{
|
||||
CapacityWait, ConcurrencyQueuePolicy, ConcurrencyWaitQueue, QueueRejection, WaitPriority,
|
||||
@@ -30,7 +30,7 @@ use gateway_core::provider_ports::{
|
||||
ProviderSessionAffinityPort, ProviderSessionBinding, ProviderSessionClaim,
|
||||
ProviderSessionExclusionPort, ProviderSessionExclusions, ProviderStoreError,
|
||||
};
|
||||
use gateway_core::routing::ProviderKind;
|
||||
use gateway_core::routing::{ProviderKind, UpstreamModelId};
|
||||
use secrecy::ExposeSecret;
|
||||
use thiserror::Error;
|
||||
use url::Url;
|
||||
@@ -185,48 +185,63 @@ impl AffinityEscapeReason {
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct AffinitySelection {
|
||||
bound_account: Option<ProviderAccountId>,
|
||||
preferred_account: Option<ProviderAccountId>,
|
||||
escape_reason: Option<AffinityEscapeReason>,
|
||||
enum AffinitySelection<'a> {
|
||||
#[default]
|
||||
Unbound,
|
||||
Preferred(&'a ProviderAccountId),
|
||||
Escaped(&'a ProviderAccountId, AffinityEscapeReason),
|
||||
}
|
||||
|
||||
impl AffinitySelection {
|
||||
fn preferred(account_id: ProviderAccountId) -> Self {
|
||||
Self {
|
||||
bound_account: Some(account_id.clone()),
|
||||
preferred_account: Some(account_id),
|
||||
escape_reason: None,
|
||||
impl<'a> AffinitySelection<'a> {
|
||||
fn for_binding(
|
||||
account: &'a ProviderAccountId,
|
||||
candidates: &[AccountCandidate],
|
||||
now: SystemTime,
|
||||
) -> Self {
|
||||
let status = candidates
|
||||
.iter()
|
||||
.find(|candidate| candidate.account.id() == account)
|
||||
.map(|candidate| {
|
||||
candidate
|
||||
.account
|
||||
.status_projection(now, candidate.signals.cooldown)
|
||||
.status
|
||||
});
|
||||
match status {
|
||||
Some(AccountStatus::Normal) => Self::Preferred(account),
|
||||
Some(AccountStatus::QuotaExhausted) => {
|
||||
Self::Escaped(account, AffinityEscapeReason::QuotaExhausted)
|
||||
}
|
||||
Some(AccountStatus::RateLimited | AccountStatus::QuotaPaused) => {
|
||||
Self::Escaped(account, AffinityEscapeReason::Cooldown)
|
||||
}
|
||||
Some(AccountStatus::Disabled | AccountStatus::Error) | None => {
|
||||
Self::Escaped(account, AffinityEscapeReason::HardUnavailable)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn escaped(account_id: ProviderAccountId, reason: AffinityEscapeReason) -> Self {
|
||||
Self {
|
||||
bound_account: Some(account_id),
|
||||
preferred_account: None,
|
||||
escape_reason: Some(reason),
|
||||
fn bound_account(&self) -> Option<&'a ProviderAccountId> {
|
||||
match self {
|
||||
Self::Preferred(account) | Self::Escaped(account, _) => Some(account),
|
||||
Self::Unbound => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn bound_account(&self) -> Option<&ProviderAccountId> {
|
||||
self.bound_account.as_ref()
|
||||
}
|
||||
|
||||
fn preferred_account(&self) -> Option<&ProviderAccountId> {
|
||||
self.preferred_account.as_ref()
|
||||
fn preferred_account(&self) -> Option<&'a ProviderAccountId> {
|
||||
match self {
|
||||
Self::Preferred(account) => Some(account),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn escape(&mut self, reason: AffinityEscapeReason) {
|
||||
if self.bound_account.is_some() && self.escape_reason.is_none() {
|
||||
self.escape_reason = Some(reason);
|
||||
self.preferred_account = None;
|
||||
if let Self::Preferred(account) = self {
|
||||
*self = Self::Escaped(account, reason);
|
||||
}
|
||||
}
|
||||
|
||||
fn observe_preferred_selection(&mut self, selection: PreferredAccountSelection) {
|
||||
if self.bound_account.is_none() || self.escape_reason.is_some() {
|
||||
return;
|
||||
}
|
||||
match selection {
|
||||
PreferredAccountSelection::Hit => {}
|
||||
PreferredAccountSelection::OverriddenByPolicy => {
|
||||
@@ -253,7 +268,7 @@ impl AffinitySelection {
|
||||
}
|
||||
|
||||
fn observe_lease_busy(&mut self, account_id: &ProviderAccountId) {
|
||||
if self.bound_account.as_ref() == Some(account_id) {
|
||||
if self.preferred_account() == Some(account_id) {
|
||||
self.escape(AffinityEscapeReason::LeaseSaturated);
|
||||
}
|
||||
}
|
||||
@@ -264,14 +279,15 @@ impl AffinitySelection {
|
||||
cache_preference: bool,
|
||||
) -> AffinityTelemetry {
|
||||
AffinityTelemetry {
|
||||
affinity_hit: self.bound_account.as_ref() == Some(selected_account)
|
||||
&& self.escape_reason.is_none(),
|
||||
escape_reason: self.escape_reason,
|
||||
// 仅因缓存偏好而分流时,客户端的轮次状态仍没有已知归属。
|
||||
affinity_hit: self.preferred_account() == Some(selected_account),
|
||||
escape_reason: match self {
|
||||
Self::Escaped(_, reason) => Some(*reason),
|
||||
_ => None,
|
||||
},
|
||||
// 仅因缓存偏好而分流时,客户端的轮次状态仍没有已知归属
|
||||
account_switch: !cache_preference
|
||||
&& self
|
||||
.bound_account
|
||||
.as_ref()
|
||||
.bound_account()
|
||||
.is_some_and(|bound| bound != selected_account),
|
||||
}
|
||||
}
|
||||
@@ -556,6 +572,123 @@ impl CodexCredentialSelector {
|
||||
})
|
||||
}
|
||||
|
||||
async fn load_candidate_accounts(
|
||||
&self,
|
||||
attempt: &AttemptContext,
|
||||
model: CodexSelectionModel<'_>,
|
||||
adapter_model: Option<&UpstreamModelId>,
|
||||
only_account: Option<&ProviderAccountId>,
|
||||
) -> Result<Vec<ProviderAccount>, CredentialSelectionError> {
|
||||
let diagnostic = attempt.account_eligibility().bypasses_local_eligibility();
|
||||
// 固定归属或已有完整投影时只重查目标账号,诊断允许读取停用账号
|
||||
if let Some(id) = only_account {
|
||||
let account = self
|
||||
.repository
|
||||
.store()
|
||||
.get_account(id)
|
||||
.await
|
||||
.map_err(|source| CredentialSelectionError::Store(ErrorSource::new(source)))?;
|
||||
return Ok(account
|
||||
.filter(|account| diagnostic || account.enabled())
|
||||
.into_iter()
|
||||
.collect());
|
||||
}
|
||||
if diagnostic {
|
||||
return Ok(self.repository.list_for_provider().await?);
|
||||
}
|
||||
let account_ids: Vec<_> = attempt
|
||||
.account_scope()
|
||||
.into_iter()
|
||||
.flat_map(|scope| scope.allowed_account_ids().cloned())
|
||||
.collect();
|
||||
let omissions = match (model, attempt.account_scope()) {
|
||||
(CodexSelectionModel::Requested(model), Some(scope)) => {
|
||||
self.catalog.omissions(model, &account_ids, |id| {
|
||||
scope.directory().account(id).is_some_and(|account| {
|
||||
account.model_access().mode() == AccountModelAccessMode::Allowlist
|
||||
}) || adapter_model.is_some_and(|model| {
|
||||
!matches!(
|
||||
attempt.upstream_adapter_for_account(&self.provider_kind, model, id),
|
||||
Ok(None)
|
||||
)
|
||||
})
|
||||
})
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
let Some(filter) = omissions else {
|
||||
return Ok(self.repository.list_for_accounts(&account_ids).await?);
|
||||
};
|
||||
let candidates = self
|
||||
.repository
|
||||
.store()
|
||||
.list_catalog_candidates(&self.provider_kind, &account_ids, &filter)
|
||||
.await
|
||||
.map_err(CredentialRepositoryError::from)?;
|
||||
if attempt.trace().is_enabled() {
|
||||
attempt.trace().record(
|
||||
"account.catalog_candidates",
|
||||
serde_json::json!({
|
||||
"scopedCount": account_ids.len(),
|
||||
"candidateCount": candidates.len(),
|
||||
}),
|
||||
);
|
||||
}
|
||||
Ok(candidates)
|
||||
}
|
||||
|
||||
async fn load_candidate_cooldowns(
|
||||
&self,
|
||||
accounts: &[ProviderAccount],
|
||||
account_ids: &[ProviderAccountId],
|
||||
native_accounts: &BTreeSet<ProviderAccountId>,
|
||||
model: CodexSelectionModel<'_>,
|
||||
) -> HashMap<ProviderAccountId, AccountCooldown> {
|
||||
let native = accounts
|
||||
.iter()
|
||||
.filter(|account| native_accounts.contains(account.id()));
|
||||
let model_cooldowns = async {
|
||||
let CodexSelectionModel::Requested(model) = model else {
|
||||
return Vec::new();
|
||||
};
|
||||
let Ok(model) = UpstreamModelId::new(model) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let ids: Vec<_> = native.clone().map(|account| account.id().clone()).collect();
|
||||
self.cooldowns
|
||||
.read_scoped_many(&ids, &ProviderCooldownScope::upstream_model(model))
|
||||
.await
|
||||
};
|
||||
let (account_cooldowns, model_cooldowns) =
|
||||
futures::join!(self.cooldowns.read_many(account_ids), model_cooldowns);
|
||||
let mut active: HashMap<_, _> = account_ids
|
||||
.iter()
|
||||
.zip(account_cooldowns)
|
||||
.filter_map(|(id, cooldown)| {
|
||||
let state = cooldown.ok().flatten()?.scheduling_state();
|
||||
state
|
||||
.is_active(SystemTime::now())
|
||||
.then(|| (id.clone(), state))
|
||||
})
|
||||
.collect();
|
||||
for (account, cooldown) in native.zip(model_cooldowns) {
|
||||
if let Some(cooldown) = cooldown.ok().flatten().filter(|cooldown| {
|
||||
cooldown.credential_revision() == account.revision()
|
||||
&& cooldown.until() > SystemTime::now()
|
||||
}) {
|
||||
// 两种冷却取较晚期限,保留账号级冷却的恢复探测语义
|
||||
active
|
||||
.entry(account.id().clone())
|
||||
.and_modify(|state| state.until = state.until.max(cooldown.until()))
|
||||
.or_insert(AccountCooldown {
|
||||
until: cooldown.until(),
|
||||
kind: AccountCooldownKind::RateLimit,
|
||||
});
|
||||
}
|
||||
}
|
||||
active
|
||||
}
|
||||
|
||||
async fn select_inner_loop(
|
||||
&self,
|
||||
request: &CredentialSelectionInput<'_>,
|
||||
@@ -567,7 +700,7 @@ impl CodexCredentialSelector {
|
||||
) -> Result<CodexCredentialLease, CredentialSelectionError> {
|
||||
let adapter_model = match model {
|
||||
CodexSelectionModel::Requested(model) if request.managed_upstream => Some(
|
||||
gateway_core::routing::UpstreamModelId::new(model)
|
||||
UpstreamModelId::new(model)
|
||||
.map_err(|_| CredentialSelectionError::NoEligibleCredential)?,
|
||||
),
|
||||
_ => None,
|
||||
@@ -730,64 +863,18 @@ impl CodexCredentialSelector {
|
||||
.as_ref()
|
||||
.and_then(|_| binding.as_ref().map(ProviderSessionBinding::account_id));
|
||||
let projection_started = std::time::Instant::now();
|
||||
// 固定归属或已有完整投影的会话只查询目标账号,随后实时复核其资格和租约
|
||||
// 常规调度排除停用账号,管理端诊断允许验证其真实上游状态
|
||||
let accounts = if let Some(pinned) = pinned_account
|
||||
let only_account = pinned_account
|
||||
.as_ref()
|
||||
.or(queued_account.as_ref())
|
||||
.or(projected_account)
|
||||
{
|
||||
self.repository
|
||||
.store()
|
||||
.get_account(pinned)
|
||||
.await
|
||||
.map_err(|source| CredentialSelectionError::Store(ErrorSource::new(source)))?
|
||||
.filter(|account| diagnostic || account.enabled())
|
||||
.into_iter()
|
||||
.collect()
|
||||
} else if diagnostic {
|
||||
self.repository.list_for_provider().await?
|
||||
} else {
|
||||
let account_ids = request
|
||||
.attempt
|
||||
.account_scope()
|
||||
.into_iter()
|
||||
.flat_map(|scope| scope.allowed_account_ids().cloned())
|
||||
.collect::<Vec<_>>();
|
||||
let omissions = match (model, request.attempt.account_scope()) {
|
||||
(CodexSelectionModel::Requested(model), Some(scope)) => {
|
||||
self.catalog.omissions(model, &account_ids, |id| {
|
||||
scope.directory().account(id).is_some_and(|a| {
|
||||
a.model_access().mode() == AccountModelAccessMode::Allowlist
|
||||
}) || adapter_model.as_ref().is_some_and(|m| {
|
||||
!matches!(
|
||||
request.attempt.upstream_adapter_for_account(
|
||||
&self.provider_kind,
|
||||
m,
|
||||
id
|
||||
),
|
||||
Ok(None)
|
||||
)
|
||||
})
|
||||
})
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
if let Some(filter) = omissions {
|
||||
let candidates = self
|
||||
.repository
|
||||
.store()
|
||||
.list_catalog_candidates(&self.provider_kind, &account_ids, &filter)
|
||||
.await
|
||||
.map_err(CredentialRepositoryError::from)?;
|
||||
if request.attempt.trace().is_enabled() {
|
||||
request.attempt.trace().record("account.catalog_candidates",serde_json::json!({"scopedCount":account_ids.len(),"candidateCount":candidates.len()}));
|
||||
}
|
||||
candidates
|
||||
} else {
|
||||
self.repository.list_for_accounts(&account_ids).await?
|
||||
}
|
||||
};
|
||||
.or(projected_account);
|
||||
let accounts = self
|
||||
.load_candidate_accounts(
|
||||
request.attempt,
|
||||
model,
|
||||
adapter_model.as_ref(),
|
||||
only_account,
|
||||
)
|
||||
.await?;
|
||||
let mut model_access_rejected = 0_usize;
|
||||
let accounts = accounts
|
||||
.into_iter()
|
||||
@@ -822,7 +909,7 @@ impl CodexCredentialSelector {
|
||||
_ => None,
|
||||
};
|
||||
let mut catalog_rejected = 0_usize;
|
||||
let mut native_accounts = std::collections::BTreeSet::new();
|
||||
let mut native_accounts = BTreeSet::new();
|
||||
for account in accounts {
|
||||
let adapter = if let Some(model) = adapter_model.as_ref() {
|
||||
match request.attempt.upstream_adapter_for_account(
|
||||
@@ -943,65 +1030,12 @@ impl CodexCredentialSelector {
|
||||
.iter()
|
||||
.map(|account| account.id().clone())
|
||||
.collect::<Vec<_>>();
|
||||
let mut rate_limits = HashMap::with_capacity(accounts.len());
|
||||
let model_scope = match model {
|
||||
CodexSelectionModel::Requested(model) => {
|
||||
gateway_core::routing::UpstreamModelId::new(model)
|
||||
.ok()
|
||||
.map(ProviderCooldownScope::upstream_model)
|
||||
}
|
||||
_ => None,
|
||||
let rate_limits = if diagnostic {
|
||||
HashMap::new()
|
||||
} else {
|
||||
self.load_candidate_cooldowns(&accounts, &account_ids, &native_accounts, model)
|
||||
.await
|
||||
};
|
||||
if !diagnostic {
|
||||
let model_account_ids = account_ids
|
||||
.iter()
|
||||
.filter(|id| native_accounts.contains(*id))
|
||||
.cloned()
|
||||
.collect::<Vec<_>>();
|
||||
let model_cooldowns = async {
|
||||
match model_scope.as_ref() {
|
||||
Some(scope) => {
|
||||
self.cooldowns
|
||||
.read_scoped_many(&model_account_ids, scope)
|
||||
.await
|
||||
}
|
||||
None => Vec::new(),
|
||||
}
|
||||
};
|
||||
let (account_cooldowns, model_cooldowns) =
|
||||
futures::join!(self.cooldowns.read_many(&account_ids), model_cooldowns,);
|
||||
// 存储端有界批量读取,单项失败沿用未知冷却;待探测冻结由端口事实判断
|
||||
for (id, cooldown) in account_ids.iter().zip(account_cooldowns) {
|
||||
let state = cooldown
|
||||
.ok()
|
||||
.flatten()
|
||||
.map(|value| value.scheduling_state());
|
||||
rate_limits.insert(
|
||||
id.clone(),
|
||||
state.filter(|state| state.is_active(SystemTime::now())),
|
||||
);
|
||||
}
|
||||
for (account, cooldown) in accounts
|
||||
.iter()
|
||||
.filter(|account| native_accounts.contains(account.id()))
|
||||
.zip(model_cooldowns)
|
||||
{
|
||||
if let Some(cooldown) = cooldown.ok().flatten().filter(|cooldown| {
|
||||
cooldown.credential_revision() == account.revision()
|
||||
&& cooldown.until() > SystemTime::now()
|
||||
}) {
|
||||
// 两种冷却取较晚期限,账号保护保留其恢复探测语义
|
||||
let until = rate_limits
|
||||
.entry(account.id().clone())
|
||||
.or_default()
|
||||
.get_or_insert(gateway_core::account::AccountCooldown {
|
||||
until: cooldown.until(),
|
||||
kind: gateway_core::account::AccountCooldownKind::RateLimit,
|
||||
});
|
||||
until.until = until.until.max(cooldown.until());
|
||||
}
|
||||
}
|
||||
}
|
||||
let policy = request.attempt.account_selection_policy();
|
||||
let client_key = request.attempt.client_api_key_ref();
|
||||
let use_assignment_cursor = policy.strategy() == RotationStrategy::RoundRobin
|
||||
@@ -1068,7 +1102,7 @@ impl CodexCredentialSelector {
|
||||
first_output_latency_ms: None,
|
||||
})
|
||||
.with_provider_quota(quota)
|
||||
.with_rate_limit(rate_limits.get(account.id()).copied().flatten())
|
||||
.with_rate_limit(rate_limits.get(account.id()).copied())
|
||||
.with_runtime_health(health.0, health.1);
|
||||
AccountCandidate {
|
||||
account,
|
||||
@@ -1107,8 +1141,8 @@ impl CodexCredentialSelector {
|
||||
.as_ref()
|
||||
.filter(|_| affinity_mode != AccountAffinity::Relaxed)
|
||||
.map_or_else(AffinitySelection::default, |binding| {
|
||||
affinity_selection_for_bound_account(
|
||||
binding.account_id().clone(),
|
||||
AffinitySelection::for_binding(
|
||||
binding.account_id(),
|
||||
&candidates,
|
||||
SystemTime::now(),
|
||||
)
|
||||
@@ -1410,17 +1444,13 @@ impl CodexCredentialSelector {
|
||||
};
|
||||
affinity.observe_preferred_selection(selection.preferred());
|
||||
let selected = selection.candidate();
|
||||
let account = candidates
|
||||
.iter()
|
||||
.find(|candidate| candidate.account.id() == selected.account.id())
|
||||
.map(|candidate| candidate.account.clone())
|
||||
.ok_or(CredentialSelectionError::InvalidCredential(None))?;
|
||||
let account = &selected.account;
|
||||
// 凭据、状态或额度结论的并发更新会使账号快照失效,必须重新选号并校验资格;
|
||||
// 仅额度观测时刻前进时沿用最新快照
|
||||
// 在占用租约前完成校验,避免重读被自己的异步释放挡住
|
||||
let (account, runtime) = match self
|
||||
.repository
|
||||
.reload_selected_credential(&account)
|
||||
.reload_selected_credential(account)
|
||||
.await
|
||||
{
|
||||
Ok(reloaded) => reloaded,
|
||||
@@ -1429,7 +1459,7 @@ impl CodexCredentialSelector {
|
||||
self.scheduling_projection.invalidate(key);
|
||||
}
|
||||
refresh_candidates = true;
|
||||
retry_account_snapshot(request.attempt, &account, &mut snapshot_retries)?;
|
||||
retry_account_snapshot(request.attempt, account, &mut snapshot_retries)?;
|
||||
continue 'capacity;
|
||||
}
|
||||
Err(error) => return Err(error.into()),
|
||||
@@ -1445,7 +1475,7 @@ impl CodexCredentialSelector {
|
||||
}
|
||||
let allows_account_state_mutation =
|
||||
eligibility.allows_account_state_mutation(account.enabled());
|
||||
match self
|
||||
let guard = match self
|
||||
.leases
|
||||
.try_acquire(ProviderLeaseRequest::Scheduling(
|
||||
ProviderSchedulingLeaseRequest::new(
|
||||
@@ -1474,167 +1504,117 @@ impl CodexCredentialSelector {
|
||||
}
|
||||
shortest_retry = minimum_duration(shortest_retry, retry_after);
|
||||
busy_accounts.insert(account.id().clone());
|
||||
continue;
|
||||
}
|
||||
ProviderLeaseAcquisition::Acquired(guard) => {
|
||||
let mut binding_account = account.id();
|
||||
if affinity_mode == AccountAffinity::Preferred
|
||||
&& let Some(primary) = binding.as_ref()
|
||||
&& (primary.account_id() == account.id()
|
||||
|| pinned_account.is_some()
|
||||
|| !self
|
||||
.preferred_primary_needs_migration(
|
||||
primary,
|
||||
queue_policy.timeout,
|
||||
)
|
||||
.await?)
|
||||
{
|
||||
binding_account = primary.account_id();
|
||||
}
|
||||
let claim = binding_key.map(|key| ProviderSessionClaim {
|
||||
key,
|
||||
expected: binding.as_ref(),
|
||||
account: binding_account,
|
||||
ttl: policy.openai_session_affinity_ttl(),
|
||||
});
|
||||
// 插件委托与普通内置选号共享轮询,显式选择、亲和命中及等待不消耗位置
|
||||
let admission = match assignment_cursor {
|
||||
Some(cursor)
|
||||
if !selection.is_policy_choice()
|
||||
&& selection.preferred() != PreferredAccountSelection::Hit =>
|
||||
{
|
||||
Some(ProviderAccountAdmission::RoundRobin {
|
||||
client_key,
|
||||
cursor,
|
||||
session: claim,
|
||||
})
|
||||
}
|
||||
_ => claim.map(ProviderAccountAdmission::Session),
|
||||
};
|
||||
if let Some(admission) = admission {
|
||||
let admitted = match self.admit_account(admission).await {
|
||||
Ok(false) => false,
|
||||
Err(error)
|
||||
if cache_preference
|
||||
&& affinity_mode == AccountAffinity::Preferred
|
||||
&& admission.session().is_some() =>
|
||||
{
|
||||
tracing::warn!(%error, "OpenAI cache preference update unavailable");
|
||||
// 缓存偏好允许丢失,实际的轮询分配仍须占用该位置
|
||||
match admission {
|
||||
ProviderAccountAdmission::RoundRobin {
|
||||
client_key,
|
||||
cursor,
|
||||
..
|
||||
} => {
|
||||
self.admit_account(
|
||||
ProviderAccountAdmission::RoundRobin {
|
||||
client_key,
|
||||
cursor,
|
||||
session: None,
|
||||
},
|
||||
)
|
||||
.await?
|
||||
}
|
||||
ProviderAccountAdmission::Session(_) => true,
|
||||
}
|
||||
}
|
||||
Err(error) => return Err(error),
|
||||
Ok(true) => {
|
||||
if let Some(reason) = queue_rejection
|
||||
&& let Some(previous) = binding.as_ref().filter(|binding| {
|
||||
binding.account_id() != binding_account
|
||||
})
|
||||
{
|
||||
tracing::info!(
|
||||
request_id = %request.attempt.request_id(),
|
||||
attempt_index = request.attempt.attempt_index().get(),
|
||||
reason = %reason,
|
||||
from_account_id = %previous.account_id(),
|
||||
to_account_id = %binding_account,
|
||||
"OpenAI 账号排队失败后已迁移绑定"
|
||||
);
|
||||
request.attempt.trace().record_evidence(
|
||||
"account.binding.migrated",
|
||||
serde_json::json!({
|
||||
"reason": reason.to_string(),
|
||||
"fromAccountId": previous.account_id().as_str(),
|
||||
"toAccountId": binding_account.as_str(),
|
||||
}),
|
||||
);
|
||||
}
|
||||
true
|
||||
}
|
||||
};
|
||||
if !admitted {
|
||||
guard.release().await?;
|
||||
if request.attempt.deadline().is_elapsed() {
|
||||
return Err(CredentialSelectionError::AccountSnapshotChanged);
|
||||
}
|
||||
continue 'capacity;
|
||||
}
|
||||
}
|
||||
let affinity_telemetry = affinity.telemetry(account.id(), cache_preference);
|
||||
let affinity_observation = request.session_affinity_observation;
|
||||
tracing::info!(
|
||||
request_id = %request.attempt.request_id(),
|
||||
attempt_index = request.attempt.attempt_index().get(),
|
||||
rotation_strategy = policy.strategy().as_str(),
|
||||
account_id = %account.id(),
|
||||
affinity_hit = affinity_telemetry.affinity_hit,
|
||||
escape_reason = affinity_telemetry
|
||||
.escape_reason
|
||||
.map_or("", AffinityEscapeReason::as_str),
|
||||
account_switch = affinity_telemetry.account_switch,
|
||||
affinity_key_hash = affinity_observation
|
||||
.map_or("", CodexSessionAffinity::key_hash),
|
||||
affinity_anchor_source = affinity_observation
|
||||
.map_or("", CodexSessionAffinity::anchor_source),
|
||||
affinity_anchor = affinity_observation
|
||||
.map_or("", CodexSessionAffinity::anchor),
|
||||
session_id = affinity_observation
|
||||
.and_then(CodexSessionAffinity::session_id)
|
||||
.unwrap_or(""),
|
||||
session_id_present = affinity_observation
|
||||
.is_some_and(CodexSessionAffinity::session_id_present),
|
||||
"OpenAI account selected"
|
||||
);
|
||||
let cookies = runtime
|
||||
.cookies
|
||||
.into_iter()
|
||||
.filter(|cookie| {
|
||||
cookie
|
||||
.expires_at
|
||||
.is_none_or(|expires| expires > chrono::Utc::now())
|
||||
&& self.cookie_policy.may_replay(
|
||||
request.request_url,
|
||||
&cookie.domain,
|
||||
&cookie.path,
|
||||
cookie.host_only,
|
||||
cookie.secure,
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
if !waiting.elapsed().is_zero() {
|
||||
request.attempt.trace().record(
|
||||
"account.queue.acquired",
|
||||
serde_json::json!({"waitMs": waiting.elapsed().as_millis() as u64}),
|
||||
);
|
||||
}
|
||||
return Ok(CodexCredentialLease {
|
||||
installation_id: runtime.installation_id,
|
||||
transport: runtime.transport,
|
||||
account,
|
||||
authentication: runtime.authentication,
|
||||
cookies,
|
||||
cyber_policy_scope,
|
||||
allows_account_state_mutation,
|
||||
affinity_telemetry,
|
||||
admitted_session: binding_key.filter(|_| !cache_preference).cloned(),
|
||||
capacity: capacity.map(AccountCapacitySnapshot::with_acquired_request),
|
||||
_guard: guard,
|
||||
});
|
||||
}
|
||||
ProviderLeaseAcquisition::Acquired(guard) => guard,
|
||||
};
|
||||
let mut binding_account = account.id();
|
||||
if affinity_mode == AccountAffinity::Preferred
|
||||
&& let Some(primary) = binding.as_ref()
|
||||
&& (primary.account_id() == account.id()
|
||||
|| pinned_account.is_some()
|
||||
|| !self
|
||||
.preferred_primary_needs_migration(primary, queue_policy.timeout)
|
||||
.await?)
|
||||
{
|
||||
binding_account = primary.account_id();
|
||||
}
|
||||
let claim = binding_key.map(|key| ProviderSessionClaim {
|
||||
key,
|
||||
expected: binding.as_ref(),
|
||||
account: binding_account,
|
||||
ttl: policy.openai_session_affinity_ttl(),
|
||||
});
|
||||
// 插件委托与普通内置选号共享轮询,显式选择、亲和命中及等待不消耗位置
|
||||
let admission = match assignment_cursor {
|
||||
Some(cursor)
|
||||
if !selection.is_policy_choice()
|
||||
&& selection.preferred() != PreferredAccountSelection::Hit =>
|
||||
{
|
||||
Some(ProviderAccountAdmission::RoundRobin {
|
||||
client_key,
|
||||
cursor,
|
||||
session: claim,
|
||||
})
|
||||
}
|
||||
_ => claim.map(ProviderAccountAdmission::Session),
|
||||
};
|
||||
if let Some(admission) = admission
|
||||
&& !self
|
||||
.admit_selected_account(
|
||||
admission,
|
||||
cache_preference && affinity_mode == AccountAffinity::Preferred,
|
||||
request.attempt,
|
||||
queue_rejection,
|
||||
)
|
||||
.await?
|
||||
{
|
||||
guard.release().await?;
|
||||
if request.attempt.deadline().is_elapsed() {
|
||||
return Err(CredentialSelectionError::AccountSnapshotChanged);
|
||||
}
|
||||
continue 'capacity;
|
||||
}
|
||||
let affinity_telemetry = affinity.telemetry(account.id(), cache_preference);
|
||||
let affinity_observation = request.session_affinity_observation;
|
||||
tracing::info!(
|
||||
request_id = %request.attempt.request_id(),
|
||||
attempt_index = request.attempt.attempt_index().get(),
|
||||
rotation_strategy = policy.strategy().as_str(),
|
||||
account_id = %account.id(),
|
||||
affinity_hit = affinity_telemetry.affinity_hit,
|
||||
escape_reason = affinity_telemetry
|
||||
.escape_reason
|
||||
.map_or("", AffinityEscapeReason::as_str),
|
||||
account_switch = affinity_telemetry.account_switch,
|
||||
affinity_key_hash = affinity_observation
|
||||
.map_or("", CodexSessionAffinity::key_hash),
|
||||
affinity_anchor_source = affinity_observation
|
||||
.map_or("", CodexSessionAffinity::anchor_source),
|
||||
affinity_anchor = affinity_observation
|
||||
.map_or("", CodexSessionAffinity::anchor),
|
||||
session_id = affinity_observation
|
||||
.and_then(CodexSessionAffinity::session_id)
|
||||
.unwrap_or(""),
|
||||
session_id_present = affinity_observation
|
||||
.is_some_and(CodexSessionAffinity::session_id_present),
|
||||
"OpenAI account selected"
|
||||
);
|
||||
let cookies = runtime
|
||||
.cookies
|
||||
.into_iter()
|
||||
.filter(|cookie| {
|
||||
cookie
|
||||
.expires_at
|
||||
.is_none_or(|expires| expires > chrono::Utc::now())
|
||||
&& self.cookie_policy.may_replay(
|
||||
request.request_url,
|
||||
&cookie.domain,
|
||||
&cookie.path,
|
||||
cookie.host_only,
|
||||
cookie.secure,
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
if !waiting.elapsed().is_zero() {
|
||||
request.attempt.trace().record(
|
||||
"account.queue.acquired",
|
||||
serde_json::json!({"waitMs": waiting.elapsed().as_millis() as u64}),
|
||||
);
|
||||
}
|
||||
return Ok(CodexCredentialLease {
|
||||
installation_id: runtime.installation_id,
|
||||
transport: runtime.transport,
|
||||
account,
|
||||
authentication: runtime.authentication,
|
||||
cookies,
|
||||
cyber_policy_scope,
|
||||
allows_account_state_mutation,
|
||||
affinity_telemetry,
|
||||
admitted_session: binding_key.filter(|_| !cache_preference).cloned(),
|
||||
capacity: capacity.map(AccountCapacitySnapshot::with_acquired_request),
|
||||
_guard: guard,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1717,6 +1697,60 @@ impl CodexCredentialSelector {
|
||||
.map_err(binding_store_error)
|
||||
}
|
||||
|
||||
async fn admit_selected_account(
|
||||
&self,
|
||||
admission: ProviderAccountAdmission<'_>,
|
||||
optional_cache_binding: bool,
|
||||
attempt: &AttemptContext,
|
||||
queue_rejection: Option<QueueRejection>,
|
||||
) -> Result<bool, CredentialSelectionError> {
|
||||
let claim = admission.session();
|
||||
match self.admit_account(admission).await {
|
||||
Ok(false) => return Ok(false),
|
||||
Err(error) if optional_cache_binding && claim.is_some() => {
|
||||
tracing::warn!(%error, "OpenAI cache preference update unavailable");
|
||||
// 缓存偏好允许丢失,实际的轮询分配仍须占用该位置
|
||||
return match admission {
|
||||
ProviderAccountAdmission::RoundRobin {
|
||||
client_key, cursor, ..
|
||||
} => {
|
||||
self.admit_account(ProviderAccountAdmission::RoundRobin {
|
||||
client_key,
|
||||
cursor,
|
||||
session: None,
|
||||
})
|
||||
.await
|
||||
}
|
||||
ProviderAccountAdmission::Session(_) => Ok(true),
|
||||
};
|
||||
}
|
||||
Err(error) => return Err(error),
|
||||
Ok(true) => {}
|
||||
}
|
||||
if let (Some(reason), Some(claim)) = (queue_rejection, claim)
|
||||
&& let Some(previous) = claim.expected
|
||||
&& previous.account_id() != claim.account
|
||||
{
|
||||
tracing::info!(
|
||||
request_id = %attempt.request_id(),
|
||||
attempt_index = attempt.attempt_index().get(),
|
||||
reason = %reason,
|
||||
from_account_id = %previous.account_id(),
|
||||
to_account_id = %claim.account,
|
||||
"OpenAI 账号排队失败后已迁移绑定"
|
||||
);
|
||||
attempt.trace().record_evidence(
|
||||
"account.binding.migrated",
|
||||
serde_json::json!({
|
||||
"reason": reason.to_string(),
|
||||
"fromAccountId": previous.account_id().as_str(),
|
||||
"toAccountId": claim.account.as_str(),
|
||||
}),
|
||||
);
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
async fn admit_account(
|
||||
&self,
|
||||
admission: ProviderAccountAdmission<'_>,
|
||||
@@ -2194,49 +2228,6 @@ fn binding_store_error(
|
||||
)))
|
||||
}
|
||||
|
||||
fn affinity_selection_for_bound_account(
|
||||
account_id: ProviderAccountId,
|
||||
candidates: &[AccountCandidate],
|
||||
now: SystemTime,
|
||||
) -> AffinitySelection {
|
||||
let Some(candidate) = candidates
|
||||
.iter()
|
||||
.find(|candidate| candidate.account.id() == &account_id)
|
||||
else {
|
||||
return AffinitySelection::escaped(account_id, AffinityEscapeReason::HardUnavailable);
|
||||
};
|
||||
match candidate
|
||||
.account
|
||||
.status_projection(now, candidate.signals.cooldown)
|
||||
.status
|
||||
{
|
||||
AccountStatus::Normal => AffinitySelection::preferred(account_id),
|
||||
AccountStatus::QuotaPaused
|
||||
| AccountStatus::QuotaExhausted
|
||||
| AccountStatus::RateLimited
|
||||
| AccountStatus::Disabled
|
||||
| AccountStatus::Error => {
|
||||
AffinitySelection::escaped(account_id, affinity_unavailable_reason(candidate, now))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn affinity_unavailable_reason(
|
||||
candidate: &AccountCandidate,
|
||||
now: SystemTime,
|
||||
) -> AffinityEscapeReason {
|
||||
match candidate
|
||||
.account
|
||||
.status_projection(now, candidate.signals.cooldown)
|
||||
.status
|
||||
{
|
||||
AccountStatus::QuotaExhausted => AffinityEscapeReason::QuotaExhausted,
|
||||
AccountStatus::RateLimited | AccountStatus::QuotaPaused => AffinityEscapeReason::Cooldown,
|
||||
AccountStatus::Normal => AffinityEscapeReason::SelectionInvariant,
|
||||
AccountStatus::Disabled | AccountStatus::Error => AffinityEscapeReason::HardUnavailable,
|
||||
}
|
||||
}
|
||||
|
||||
fn active_risk_recovery<'a>(
|
||||
recovery: &'a mut HashMap<String, RiskRecoveryState>,
|
||||
account_id: &str,
|
||||
|
||||
@@ -434,10 +434,6 @@ pub(super) struct OpenAiSessionState {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub(super) oauth_identity: Option<String>,
|
||||
pub(super) conversation_id: Option<String>,
|
||||
#[serde(default)]
|
||||
pub(super) turn_state: Option<String>,
|
||||
#[serde(default)]
|
||||
pub(super) client_turn_id: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub(super) reasoning_effort_override: Option<String>,
|
||||
pub(super) continuation_scope: OpenAiContinuationScope,
|
||||
@@ -456,20 +452,12 @@ pub(super) struct OpenAiSessionCapture {
|
||||
pub(super) credential_revision: Option<u64>,
|
||||
pub(super) oauth_identity: Option<String>,
|
||||
pub(super) conversation_id: Option<String>,
|
||||
pub(super) turn_state: Option<String>,
|
||||
pub(super) client_turn_id: Option<String>,
|
||||
pub(super) reasoning_effort_override: Option<String>,
|
||||
pub(super) response_store: bool,
|
||||
pub(super) oversized_http: bool,
|
||||
pub(super) continuation_scope: Option<OpenAiContinuationScope>,
|
||||
}
|
||||
|
||||
pub(super) fn same_client_turn(previous: Option<&str>, current: Option<&str>) -> bool {
|
||||
previous
|
||||
.zip(current)
|
||||
.is_some_and(|(previous, current)| !previous.is_empty() && previous == current)
|
||||
}
|
||||
|
||||
pub(super) fn decode_openai_session_state(request: &GenerateRequest) -> Option<OpenAiSessionState> {
|
||||
request
|
||||
.provider_session_state(PROVIDER_NAME)
|
||||
@@ -505,8 +493,6 @@ fn encode_openai_session_capture(
|
||||
credential_revision: capture.credential_revision,
|
||||
oauth_identity: capture.oauth_identity.clone(),
|
||||
conversation_id: capture.conversation_id.clone(),
|
||||
turn_state: capture.turn_state.clone(),
|
||||
client_turn_id: capture.client_turn_id.clone(),
|
||||
reasoning_effort_override: capture.reasoning_effort_override.clone(),
|
||||
continuation_scope,
|
||||
})
|
||||
@@ -938,7 +924,6 @@ pub(super) fn cold_response_stream(response: ColdResponse) -> EventStream {
|
||||
} else {
|
||||
OpenAiContinuationScope::ReplayRequired
|
||||
});
|
||||
capture.turn_state = response.turn_state.clone().or(capture.turn_state.clone());
|
||||
}
|
||||
let mut observation_state = OpenAiResponseObservationState::from_backend_response(
|
||||
&response,
|
||||
@@ -1070,7 +1055,6 @@ pub(super) fn cold_response_stream(response: ColdResponse) -> EventStream {
|
||||
};
|
||||
let metadata_merge = merge_response_metadata_updates(
|
||||
response_metadata_updates.as_ref(),
|
||||
&mut session_capture,
|
||||
&mut observation_state,
|
||||
)
|
||||
.await;
|
||||
@@ -1148,7 +1132,6 @@ pub(super) fn cold_response_stream(response: ColdResponse) -> EventStream {
|
||||
};
|
||||
let metadata_merge = merge_response_metadata_updates(
|
||||
response_metadata_updates.as_ref(),
|
||||
&mut session_capture,
|
||||
&mut observation_state,
|
||||
)
|
||||
.await;
|
||||
@@ -1267,9 +1250,7 @@ pub(super) fn cold_response_stream(response: ColdResponse) -> EventStream {
|
||||
yield event;
|
||||
}
|
||||
} else if atomic_upstream_failure {
|
||||
failure.error = failure
|
||||
.error
|
||||
.with_atomic_client_events(events);
|
||||
failure.error = with_atomic_failure_events(failure.error, events, &context);
|
||||
}
|
||||
Err(continuation_recovery_error(
|
||||
failure.error,
|
||||
@@ -1354,7 +1335,6 @@ pub(super) fn cold_response_stream(response: ColdResponse) -> EventStream {
|
||||
}
|
||||
let metadata_changed = merge_response_metadata_updates(
|
||||
response_metadata_updates.as_ref(),
|
||||
&mut session_capture,
|
||||
&mut observation_state,
|
||||
)
|
||||
.await
|
||||
@@ -1417,12 +1397,8 @@ pub(super) fn cold_response_stream(response: ColdResponse) -> EventStream {
|
||||
|
||||
async fn merge_response_metadata_updates(
|
||||
updates: Option<&CodexResponseMetadataUpdates>,
|
||||
session_capture: &mut Option<OpenAiSessionCapture>,
|
||||
observation_state: &mut OpenAiResponseObservationState,
|
||||
) -> Option<bool> {
|
||||
let turn_state = updates?.lock().await.turn_state.take()?;
|
||||
if let Some(capture) = session_capture.as_mut() {
|
||||
capture.turn_state = Some(turn_state.clone());
|
||||
}
|
||||
Some(observation_state.merge_client_header("x-codex-turn-state", &turn_state))
|
||||
}
|
||||
|
||||
@@ -135,6 +135,32 @@ fn is_replaceable_prelude(event: &ProviderEvent) -> bool {
|
||||
})
|
||||
}
|
||||
|
||||
/// WS 拒绝请求时也保留原始 error 对象,供原生 Responses 在 HTTP 首字节前交付
|
||||
pub(super) fn with_atomic_failure_events(
|
||||
mut error: ProviderError,
|
||||
events: Vec<ProviderEvent>,
|
||||
context: &AttemptContext,
|
||||
) -> ProviderError {
|
||||
if error.kind() == ProviderErrorKind::InvalidRequest
|
||||
&& error.upstream_status() == Some(400)
|
||||
&& error.retry_is_prohibited()
|
||||
&& context.client_transport() == gateway_core::engine::execution::ClientTransport::HttpSse
|
||||
&& context.endpoint() == gateway_protocol::openai::RESPONSES_PATH
|
||||
&& let [event] = events.as_slice()
|
||||
&& let Some(wire) = event.wire_event()
|
||||
&& wire.raw_websocket_message().is_some()
|
||||
&& wire.event_type() == Some("error")
|
||||
&& let Some(detail) = wire.data().get("error").and_then(Value::as_object)
|
||||
{
|
||||
error = error.with_client_visible_upstream_response(ClientVisibleUpstreamResponse::new(
|
||||
400,
|
||||
Some(b"application/json".to_vec()),
|
||||
Bytes::from(json!({ "error": detail }).to_string()),
|
||||
));
|
||||
}
|
||||
error.with_atomic_client_events(events)
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub(super) enum ReplayBoundary {
|
||||
BeforeSemanticOutput,
|
||||
@@ -1500,15 +1526,12 @@ pub(super) fn map_upstream_failure(
|
||||
.status
|
||||
.is_some_and(|status| status.is_client_error())
|
||||
&& is_cyber_policy_code(failure.code.as_deref());
|
||||
let continuation_failure = failure
|
||||
.upstream_code()
|
||||
.filter(|code| is_history_failure_code(code))
|
||||
.map(|_| ContinuationFailure::HistoryUnavailable);
|
||||
let continuation_summary = failure.upstream_code().and_then(history_failure_summary);
|
||||
let send_state = upstream_send_state(failure.send_phase);
|
||||
let error_kind = if continuation_failure.is_some() {
|
||||
ProviderErrorKind::ContinuationRecoveryRequired
|
||||
let (error_kind, summary) = if let Some(summary) = continuation_summary {
|
||||
(ProviderErrorKind::ContinuationRecoveryRequired, summary)
|
||||
} else {
|
||||
provider_error_kind(category)
|
||||
upstream_failure_summary(category, failure.upstream_code())
|
||||
};
|
||||
let mut error = provider_error(error_kind, send_state)
|
||||
.with_upstream_failure_fields(failure.upstream_fields.clone());
|
||||
@@ -1517,7 +1540,7 @@ pub(super) fn map_upstream_failure(
|
||||
let upgrade_required = failure.send_phase == CodexUpstreamSendPhase::BeforePayload
|
||||
&& failure.status == Some(reqwest::StatusCode::UPGRADE_REQUIRED);
|
||||
if category == CodexFailureCategory::FlexUnavailable
|
||||
|| (!replay_safe && continuation_failure.is_none() && !upgrade_required)
|
||||
|| (!replay_safe && continuation_summary.is_none() && !upgrade_required)
|
||||
{
|
||||
error = error.with_retry_prohibited();
|
||||
}
|
||||
@@ -1552,9 +1575,9 @@ pub(super) fn map_upstream_failure(
|
||||
if replay_boundary.permits_provider_proof() && replay_safe {
|
||||
error = error.with_replay_safe();
|
||||
}
|
||||
if let Some(continuation_failure) = continuation_failure {
|
||||
if continuation_summary.is_some() {
|
||||
error = error
|
||||
.with_continuation_failure(continuation_failure)
|
||||
.with_continuation_failure(ContinuationFailure::HistoryUnavailable)
|
||||
.with_continuation_recovery_disposition(
|
||||
ContinuationRecoveryDisposition::ClientReplayRequired,
|
||||
)
|
||||
@@ -1595,16 +1618,13 @@ pub(super) fn map_upstream_failure(
|
||||
if let Some(request_id) = failure.request_id.as_deref() {
|
||||
error = error.with_upstream_request_id(OpaqueUpstreamValue::new(request_id.to_owned()));
|
||||
}
|
||||
let status = error
|
||||
.upstream_status()
|
||||
.map_or_else(|| "none".to_owned(), |status| status.to_string());
|
||||
let kind = error.kind().as_str();
|
||||
let summary = if cyber_policy_failure {
|
||||
"请求被上游安全策略拦截"
|
||||
} else {
|
||||
summary
|
||||
};
|
||||
error = error.with_diagnostic(
|
||||
ProviderDiagnostic::new(format!(
|
||||
"OpenAI upstream failure: kind={}, status={status}",
|
||||
kind
|
||||
))
|
||||
.with_classification("upstream", "upstream_rejected"),
|
||||
ProviderDiagnostic::new(summary).with_classification("upstream", "upstream_rejected"),
|
||||
);
|
||||
MappedProviderFailure {
|
||||
error,
|
||||
@@ -1633,38 +1653,80 @@ pub(super) fn is_cyber_policy_code(code: Option<&str>) -> bool {
|
||||
code.is_some_and(|code| code.trim().eq_ignore_ascii_case("cyber_policy"))
|
||||
}
|
||||
|
||||
pub(super) fn is_history_failure_code(code: &str) -> bool {
|
||||
[
|
||||
"previous_response_not_found",
|
||||
"invalid_encrypted_content",
|
||||
"missing_tool_output",
|
||||
"no_tool_output",
|
||||
]
|
||||
.iter()
|
||||
.any(|known| code.trim().eq_ignore_ascii_case(known))
|
||||
fn history_failure_summary(code: &str) -> Option<&'static str> {
|
||||
let code = code.trim();
|
||||
if code.eq_ignore_ascii_case("previous_response_not_found") {
|
||||
Some("上游找不到先前的响应")
|
||||
} else if code.eq_ignore_ascii_case("invalid_encrypted_content") {
|
||||
Some("上游拒绝了加密的历史内容")
|
||||
} else if code.eq_ignore_ascii_case("missing_tool_output")
|
||||
|| code.eq_ignore_ascii_case("no_tool_output")
|
||||
{
|
||||
Some("对话历史缺少工具调用结果")
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) const fn provider_error_kind(category: CodexFailureCategory) -> ProviderErrorKind {
|
||||
fn upstream_failure_summary(
|
||||
category: CodexFailureCategory,
|
||||
code: Option<&str>,
|
||||
) -> (ProviderErrorKind, &'static str) {
|
||||
match category {
|
||||
CodexFailureCategory::ModelUnsupported => ProviderErrorKind::Unsupported,
|
||||
CodexFailureCategory::CredentialExpired => ProviderErrorKind::Unauthorized,
|
||||
CodexFailureCategory::IdentityVerificationRequired | CodexFailureCategory::Banned => {
|
||||
ProviderErrorKind::PermissionDenied
|
||||
CodexFailureCategory::ModelUnsupported => {
|
||||
(ProviderErrorKind::Unsupported, "上游账号不支持此模型")
|
||||
}
|
||||
CodexFailureCategory::UsageLimitExhausted => ProviderErrorKind::QuotaExhausted,
|
||||
CodexFailureCategory::RateLimited | CodexFailureCategory::TurnRateLimited => {
|
||||
ProviderErrorKind::RateLimited
|
||||
CodexFailureCategory::CredentialExpired => {
|
||||
(ProviderErrorKind::Unauthorized, "上游账号凭据无效")
|
||||
}
|
||||
CodexFailureCategory::QuotaExhausted => ProviderErrorKind::QuotaExhausted,
|
||||
CodexFailureCategory::CloudflareChallenge
|
||||
| CodexFailureCategory::CloudflarePathBlocked
|
||||
| CodexFailureCategory::FlexUnavailable
|
||||
| CodexFailureCategory::Unavailable => ProviderErrorKind::Unavailable,
|
||||
CodexFailureCategory::CapacityUnavailable => ProviderErrorKind::UpstreamCapacityUnavailable,
|
||||
CodexFailureCategory::InvalidRequest => ProviderErrorKind::InvalidRequest,
|
||||
CodexFailureCategory::PermissionDenied => ProviderErrorKind::PermissionDenied,
|
||||
CodexFailureCategory::Timeout => ProviderErrorKind::Timeout,
|
||||
CodexFailureCategory::Transport => ProviderErrorKind::Transport,
|
||||
CodexFailureCategory::IdentityVerificationRequired => {
|
||||
(ProviderErrorKind::PermissionDenied, "上游账号需要验证身份")
|
||||
}
|
||||
CodexFailureCategory::Banned => (ProviderErrorKind::PermissionDenied, "上游账号已被停用"),
|
||||
CodexFailureCategory::UsageLimitExhausted => (
|
||||
ProviderErrorKind::QuotaExhausted,
|
||||
"当前用量窗口的额度已用完",
|
||||
),
|
||||
CodexFailureCategory::QuotaExhausted => {
|
||||
(ProviderErrorKind::QuotaExhausted, "上游账号额度已用完")
|
||||
}
|
||||
CodexFailureCategory::RateLimited => (ProviderErrorKind::RateLimited, "上游请求频率受限"),
|
||||
CodexFailureCategory::TurnRateLimited => {
|
||||
(ProviderErrorKind::RateLimited, "当前对话轮次受到限流")
|
||||
}
|
||||
CodexFailureCategory::CloudflareChallenge => {
|
||||
(ProviderErrorKind::Unavailable, "Cloudflare 要求验证请求")
|
||||
}
|
||||
CodexFailureCategory::CloudflarePathBlocked => {
|
||||
(ProviderErrorKind::Unavailable, "上游接口返回空的 404 响应")
|
||||
}
|
||||
CodexFailureCategory::FlexUnavailable => {
|
||||
(ProviderErrorKind::Unavailable, "Flex 服务容量不足")
|
||||
}
|
||||
CodexFailureCategory::CapacityUnavailable => (
|
||||
ProviderErrorKind::UpstreamCapacityUnavailable,
|
||||
"上游模型容量不足",
|
||||
),
|
||||
CodexFailureCategory::InvalidRequest => {
|
||||
let summary = match code.map(str::trim) {
|
||||
Some(code) if code.eq_ignore_ascii_case("unsupported_value") => {
|
||||
"上游不支持该参数值"
|
||||
}
|
||||
Some(code) if code.eq_ignore_ascii_case("invalid_prompt") => "上游拒绝了提示词",
|
||||
Some(code) if code.eq_ignore_ascii_case("context_length_exceeded") => {
|
||||
"输入超出模型上下文长度"
|
||||
}
|
||||
_ => "上游拒绝了请求参数",
|
||||
};
|
||||
(ProviderErrorKind::InvalidRequest, summary)
|
||||
}
|
||||
CodexFailureCategory::PermissionDenied => (
|
||||
ProviderErrorKind::PermissionDenied,
|
||||
"上游账号无权执行此请求",
|
||||
),
|
||||
CodexFailureCategory::Timeout => (ProviderErrorKind::Timeout, "上游响应超时"),
|
||||
CodexFailureCategory::Unavailable => (ProviderErrorKind::Unavailable, "上游服务暂不可用"),
|
||||
CodexFailureCategory::Transport => (ProviderErrorKind::Transport, "上游返回了非预期响应"),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -86,7 +86,7 @@ use crate::transport::protocol::responses::{
|
||||
};
|
||||
use crate::transport::request::{
|
||||
CodexRequestEncodeError, RequestAccountScope, align_structured_location_fields,
|
||||
clear_request_turn_state, encode_generate_request, scope_request_to_account,
|
||||
encode_generate_request, scope_request_to_account,
|
||||
};
|
||||
use crate::transport::session::CodexSessionIdentity;
|
||||
use crate::transport::usage::normalize_service_tier;
|
||||
@@ -240,19 +240,6 @@ impl CodexProvider {
|
||||
if let Some(identity) = &self.session_identity {
|
||||
identity.prepare_local_conversation(&mut upstream);
|
||||
}
|
||||
if let Some(previous_session) = previous_session.as_ref() {
|
||||
if same_client_turn(
|
||||
previous_session.client_turn_id.as_deref(),
|
||||
upstream.client_turn_id.as_deref(),
|
||||
) {
|
||||
upstream.turn_state = upstream
|
||||
.turn_state
|
||||
.take()
|
||||
.or_else(|| previous_session.turn_state.clone());
|
||||
} else {
|
||||
clear_request_turn_state(&mut upstream);
|
||||
}
|
||||
}
|
||||
let session_affinity =
|
||||
derive_codex_session_affinity(&upstream, context.client_api_key_ref());
|
||||
let cyber_policy_session_key =
|
||||
@@ -1144,8 +1131,6 @@ impl CodexProvider {
|
||||
)
|
||||
.then_some(lease.account().revision().get()),
|
||||
conversation_id: upstream_request.local_conversation_id.clone(),
|
||||
turn_state: upstream_request.turn_state.clone(),
|
||||
client_turn_id: upstream_request.client_turn_id.clone(),
|
||||
reasoning_effort_override: upstream_request
|
||||
.semantics(
|
||||
previous_session
|
||||
|
||||
@@ -701,8 +701,6 @@ pub(super) fn codex_request_context<'a>(
|
||||
request_id,
|
||||
turn_state: request.turn_state.as_deref(),
|
||||
turn_metadata: request.turn_metadata.as_deref(),
|
||||
beta_features: request.beta_features.as_deref(),
|
||||
include_timing_metrics: request.include_timing_metrics.as_deref(),
|
||||
version: request.version.as_deref(),
|
||||
codex_window_id: request.codex_window_id.as_deref(),
|
||||
parent_thread_id: request.parent_thread_id.as_deref(),
|
||||
@@ -711,7 +709,6 @@ pub(super) fn codex_request_context<'a>(
|
||||
session_id: request.client_session_id.as_deref(),
|
||||
thread_id: request.client_thread_id.as_deref(),
|
||||
client_request_id: request.client_request_id.as_deref(),
|
||||
turn_id: request.client_turn_id.as_deref(),
|
||||
account_selection,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ pub(crate) fn follows_session_with_headers(
|
||||
}
|
||||
gateway_protocol::openai::codex_responses_request_semantics_with_turn_metadata(
|
||||
body,
|
||||
metadata.or_else(|| context.get("turn_metadata").and_then(Value::as_str)),
|
||||
metadata.or_else(|| gateway_protocol::openai::context_turn_metadata(body, context)),
|
||||
None,
|
||||
)
|
||||
.subagent_kind
|
||||
|
||||
@@ -472,10 +472,6 @@ pub struct CodexRequestContext<'a> {
|
||||
pub turn_state: Option<&'a str>,
|
||||
/// 客户端 turn metadata;其中 installation ID 已按当前账号处理
|
||||
pub turn_metadata: Option<&'a str>,
|
||||
/// x-codex-beta-features 头值
|
||||
pub beta_features: Option<&'a str>,
|
||||
/// x-responsesapi-include-timing-metrics 头值
|
||||
pub include_timing_metrics: Option<&'a str>,
|
||||
/// 下游 `version` 扩展头;存在时上游值由 Desktop 版本画像统一生成
|
||||
pub version: Option<&'a str>,
|
||||
/// 客户端 window ID
|
||||
@@ -492,8 +488,6 @@ pub struct CodexRequestContext<'a> {
|
||||
pub thread_id: Option<&'a str>,
|
||||
/// 客户端逻辑 request ID;缺失时使用代理请求 ID
|
||||
pub client_request_id: Option<&'a str>,
|
||||
/// 客户端 turn ID
|
||||
pub turn_id: Option<&'a str>,
|
||||
/// 账号亲和选择结果;仅用于结构化遥测,不参与 transport 决策
|
||||
pub account_selection: CodexAccountSelectionTelemetry<'a>,
|
||||
}
|
||||
@@ -514,8 +508,6 @@ impl<'a> CodexRequestContext<'a> {
|
||||
request_id,
|
||||
turn_state: None,
|
||||
turn_metadata: None,
|
||||
beta_features: None,
|
||||
include_timing_metrics: None,
|
||||
version: None,
|
||||
codex_window_id: None,
|
||||
parent_thread_id: None,
|
||||
@@ -524,7 +516,6 @@ impl<'a> CodexRequestContext<'a> {
|
||||
session_id: None,
|
||||
thread_id: None,
|
||||
client_request_id: None,
|
||||
turn_id: None,
|
||||
account_selection: CodexAccountSelectionTelemetry::NONE,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,10 @@
|
||||
|
||||
use gateway_core::diagnostics::{StreamCapture, StreamFormat, TraceContext, diagnostic_json};
|
||||
|
||||
use std::{sync::Arc, time::Instant};
|
||||
use std::{
|
||||
sync::Arc,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
use futures::{StreamExt, TryStreamExt};
|
||||
use gateway_protocol::openai::{
|
||||
@@ -32,9 +35,10 @@ use crate::transport::{
|
||||
},
|
||||
response_meta,
|
||||
websocket::{
|
||||
CodexWebSocketConnection, CodexWebSocketExchangeError, CodexWebSocketPoolKey,
|
||||
DEFAULT_STREAM_IDLE_TIMEOUT, PreviousResponseUnavailableReason, WEBSOCKET_FAST_PATH_BUDGET,
|
||||
WebSocketFastPath, execute_prepared_response_create_request_stream, post_send_ambiguous,
|
||||
CodexWebSocketConnection, CodexWebSocketExchangeError, CodexWebSocketPool,
|
||||
CodexWebSocketPoolKey, DEFAULT_FAST_PATH_BUDGET_MS, DEFAULT_STREAM_IDLE_TIMEOUT,
|
||||
PreviousResponseUnavailableReason, WebSocketFastPath,
|
||||
execute_prepared_response_create_request_stream, post_send_ambiguous,
|
||||
prepare_response_create_request_with_pool, websocket_audit_dir,
|
||||
write_websocket_audit_artifact_from_env,
|
||||
},
|
||||
@@ -375,7 +379,10 @@ impl CodexBackendClient {
|
||||
.map(|pool| (pool, pool_key, downstream_cancellation));
|
||||
let fast_path_budget = match requirement {
|
||||
TransportRequirement::PersistedContinuation | TransportRequirement::NewChain => {
|
||||
Some(WEBSOCKET_FAST_PATH_BUDGET)
|
||||
Some(self.websocket_pool.as_deref().map_or(
|
||||
Duration::from_millis(DEFAULT_FAST_PATH_BUDGET_MS),
|
||||
CodexWebSocketPool::fast_path_budget,
|
||||
))
|
||||
}
|
||||
TransportRequirement::ExplicitWebSocketWarmup
|
||||
| TransportRequirement::WebSocketNewChain
|
||||
|
||||
@@ -211,11 +211,6 @@ impl CodexBackendClient {
|
||||
("x-codex-window-id", context.codex_window_id),
|
||||
("x-codex-turn-state", context.turn_state),
|
||||
("x-codex-turn-metadata", context.turn_metadata),
|
||||
("x-codex-beta-features", context.beta_features),
|
||||
(
|
||||
"x-responsesapi-include-timing-metrics",
|
||||
context.include_timing_metrics,
|
||||
),
|
||||
("x-codex-parent-thread-id", context.parent_thread_id),
|
||||
(
|
||||
X_OPENAI_MEMGEN_REQUEST_HEADER,
|
||||
|
||||
@@ -56,10 +56,10 @@ pub(super) struct DesktopArtifact {
|
||||
pub etag: &'static str,
|
||||
}
|
||||
|
||||
// 官方 npm 稳定标签及六个平台依赖;核验于 2026-11-14T02:09:52Z
|
||||
// 官方 npm 稳定版本及六个平台依赖,核验于 2026-11-19T00:51:11Z
|
||||
pub(super) const CLI_RELEASE: CoreRelease = CoreRelease {
|
||||
version: "0.169.0",
|
||||
verified_at_millis: 1_794_622_192_000,
|
||||
version: "0.170.0",
|
||||
verified_at_millis: 1_795_049_471_000,
|
||||
};
|
||||
|
||||
// macOS arm64 ZIP;核验于 2026-11-14T02:09:52Z
|
||||
|
||||
@@ -50,8 +50,6 @@ pub struct CodexResponsesRequest {
|
||||
pub client_thread_id: Option<String>,
|
||||
/// 客户端 request ID,仅保留在受控本地上下文
|
||||
pub client_request_id: Option<String>,
|
||||
/// 客户端 turn ID,仅保留在受控本地上下文
|
||||
pub client_turn_id: Option<String>,
|
||||
/// 连接池和 affinity 使用的本地会话身份,不发送上游
|
||||
pub local_conversation_id: Option<String>,
|
||||
/// 变体身份键
|
||||
@@ -70,12 +68,10 @@ pub struct CodexResponsesRequest {
|
||||
pub turn_state: Option<String>,
|
||||
/// turn metadata 透传头
|
||||
pub turn_metadata: Option<String>,
|
||||
/// beta features 透传头
|
||||
pub beta_features: Option<String>,
|
||||
/// 实际入站为 WS 帧时,观测优先使用本帧的轮次元数据
|
||||
pub(crate) websocket_frame: bool,
|
||||
/// 下游 `version` 扩展头;存在时上游值由 Desktop 版本画像统一生成
|
||||
pub version: Option<String>,
|
||||
/// timing metrics 透传头
|
||||
pub include_timing_metrics: Option<String>,
|
||||
/// Responses Lite 请求语义;HTTP 使用 header,WebSocket 使用 client metadata 投影
|
||||
pub responses_lite: Option<String>,
|
||||
/// Memory consolidation 请求语义;HTTP 与 WebSocket opening 均使用 header
|
||||
@@ -605,7 +601,6 @@ impl CodexResponsesRequest {
|
||||
client_account_follow_only: false,
|
||||
client_thread_id: None,
|
||||
client_request_id: None,
|
||||
client_turn_id: None,
|
||||
local_conversation_id: None,
|
||||
variant_identity: None,
|
||||
client_ip: None,
|
||||
@@ -615,9 +610,8 @@ impl CodexResponsesRequest {
|
||||
force_http_sse: false,
|
||||
turn_state: None,
|
||||
turn_metadata: None,
|
||||
beta_features: None,
|
||||
websocket_frame: false,
|
||||
version: None,
|
||||
include_timing_metrics: None,
|
||||
responses_lite: None,
|
||||
memgen_request: None,
|
||||
codex_window_id: None,
|
||||
@@ -774,7 +768,11 @@ impl CodexResponsesRequest {
|
||||
) -> CodexRequestSemantics {
|
||||
codex_responses_request_semantics_with_turn_metadata(
|
||||
self.body(),
|
||||
self.turn_metadata.as_deref(),
|
||||
gateway_protocol::openai::observed_turn_metadata(
|
||||
self.body(),
|
||||
self.turn_metadata.as_deref(),
|
||||
self.websocket_frame,
|
||||
),
|
||||
previous_reasoning_effort_override,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -198,7 +198,7 @@ pub fn websocket_response_create_payload(request: &CodexResponsesRequest) -> Val
|
||||
Value::Object(payload)
|
||||
}
|
||||
|
||||
/// 借用原始 body 序列化 `response.create` 帧,不复制字段
|
||||
/// 借用原始 body 序列化 `response.create` 消息,不复制字段
|
||||
///
|
||||
/// 输出与序列化 [`websocket_response_create_payload`] 的合并 Map 逐字节一致:
|
||||
/// body 自带 `type` 键时以 body 值置于首位,其余字段按插入顺序原样透传
|
||||
@@ -224,7 +224,7 @@ impl serde::Serialize for ResponseCreateFrame<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// 生成 Responses WebSocket `response.create` 文本帧内容
|
||||
/// 生成 Responses WebSocket `response.create` 消息的完整文本
|
||||
pub fn websocket_response_create_payload_text(
|
||||
request: &CodexResponsesRequest,
|
||||
) -> Result<String, serde_json::Error> {
|
||||
|
||||
@@ -22,7 +22,6 @@ use crate::transport::protocol::responses::{
|
||||
};
|
||||
|
||||
pub(crate) const PASSTHROUGH_HEADERS_CONTEXT_KEY: &str = "opaque_request_headers";
|
||||
const TURN_ID_CLIENT_METADATA_KEY: &str = "turn_id";
|
||||
const THREAD_SPAWN_SUBAGENT_KIND: &str = "thread_spawn";
|
||||
const THREAD_SPAWN_CONVERSATION_PREFIX: &str = "thread-spawn:";
|
||||
const ENVIRONMENT_CONTEXT_CONTENT_KIND: &str = "environments.environment_context";
|
||||
@@ -256,14 +255,11 @@ fn extract_request_context(request: &mut CodexResponsesRequest) {
|
||||
let context = ExtractedRequestContext::from_body(request.body());
|
||||
request.turn_state = context.turn_state;
|
||||
request.turn_metadata = context.turn_metadata;
|
||||
request.beta_features = context.beta_features;
|
||||
request.version = context.version;
|
||||
request.include_timing_metrics = context.include_timing_metrics;
|
||||
request.codex_window_id = context.codex_window_id;
|
||||
request.parent_thread_id = context.parent_thread_id;
|
||||
request.client_conversation_id = context.conversation_id;
|
||||
request.client_request_id = context.client_request_id;
|
||||
request.client_turn_id = context.turn_id;
|
||||
request.responses_lite = context.responses_lite;
|
||||
request.memgen_request = context.memgen_request;
|
||||
}
|
||||
@@ -271,14 +267,11 @@ fn extract_request_context(request: &mut CodexResponsesRequest) {
|
||||
struct ExtractedRequestContext {
|
||||
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>,
|
||||
conversation_id: Option<String>,
|
||||
client_request_id: Option<String>,
|
||||
turn_id: Option<String>,
|
||||
responses_lite: Option<String>,
|
||||
memgen_request: Option<String>,
|
||||
}
|
||||
@@ -287,26 +280,18 @@ impl ExtractedRequestContext {
|
||||
fn from_body(body: &Map<String, Value>) -> Self {
|
||||
let client_metadata = body.get("client_metadata").and_then(Value::as_object);
|
||||
Self {
|
||||
// 官方 downstream WebSocket 无法逐帧更新 HTTP header,因此把
|
||||
// response.metadata 返回的 turn state 放回下一帧 client_metadata
|
||||
// Provider 仍会在账号/turn 归属确定后决定是否允许复用该状态
|
||||
// 下游 WS 在每个 response.create 的 client_metadata 回传本轮路由状态
|
||||
turn_state: body_string(body, "turnState").or_else(|| {
|
||||
client_metadata.and_then(|metadata| {
|
||||
string_value(metadata.get(X_CODEX_TURN_STATE_CLIENT_METADATA_KEY))
|
||||
})
|
||||
}),
|
||||
turn_metadata: body_string(body, "turnMetadata"),
|
||||
beta_features: body_string(body, "betaFeatures"),
|
||||
version: body_string(body, "version"),
|
||||
include_timing_metrics: body_string(body, "includeTimingMetrics"),
|
||||
codex_window_id: body_string(body, "codexWindowId"),
|
||||
parent_thread_id: body_string(body, "parentThreadId"),
|
||||
conversation_id: body_string(body, "conversation_id"),
|
||||
client_request_id: body_string(body, "x-client-request-id"),
|
||||
turn_id: body_string(body, "turn_id").or_else(|| {
|
||||
client_metadata
|
||||
.and_then(|metadata| string_value(metadata.get(TURN_ID_CLIENT_METADATA_KEY)))
|
||||
}),
|
||||
// Responses Lite 在 WebSocket body 中的这个键是官方 header 投影,
|
||||
// 不是普通上下文字段的 metadata 回退
|
||||
responses_lite: client_metadata.and_then(|metadata| {
|
||||
@@ -452,8 +437,8 @@ fn normalize_conversation_anchor_text(text: &str) -> String {
|
||||
rest.to_owned()
|
||||
}
|
||||
|
||||
/// 已确认换轮或换号时,各 wire 入口同步清理原有路由 token
|
||||
pub(crate) fn clear_request_turn_state(request: &mut CodexResponsesRequest) {
|
||||
/// 明确换号时,同步清除请求头与正文中的原账号路由 token
|
||||
fn clear_request_turn_state(request: &mut CodexResponsesRequest) {
|
||||
request.turn_state = None;
|
||||
request.passthrough_headers.remove("x-codex-turn-state");
|
||||
for key in TURN_STATE_KEYS {
|
||||
@@ -745,20 +730,24 @@ fn replace_metadata_field(metadata: &mut Map<String, Value>, key: &str, value: O
|
||||
// 请求头的场景)
|
||||
fn apply_protocol_context(request: &mut CodexResponsesRequest, context: &Map<String, Value>) {
|
||||
request.passthrough_headers = decode_passthrough_headers(context);
|
||||
request.turn_state =
|
||||
context_string(context, "turn_state").or_else(|| request.turn_state.take());
|
||||
request.websocket_frame = context
|
||||
.get(gateway_protocol::openai::WEBSOCKET_FRAME_CONTEXT_KEY)
|
||||
.and_then(Value::as_bool)
|
||||
== Some(true);
|
||||
request.turn_metadata =
|
||||
context_string(context, "turn_metadata").or_else(|| request.turn_metadata.take());
|
||||
request.beta_features =
|
||||
context_string(context, "beta_features").or_else(|| request.beta_features.take());
|
||||
request.version = context_string(context, "version").or_else(|| request.version.take());
|
||||
request.include_timing_metrics = context_string(context, "include_timing_metrics")
|
||||
.or_else(|| request.include_timing_metrics.take());
|
||||
request.codex_window_id =
|
||||
context_string(context, "codex_window_id").or_else(|| request.codex_window_id.take());
|
||||
request.downstream_websocket_connection_id =
|
||||
context_string(context, "downstream_websocket_connection_id")
|
||||
.or_else(|| request.downstream_websocket_connection_id.take());
|
||||
if request.downstream_websocket_connection_id.is_some() && request.turn_state.is_some() {
|
||||
// 当前帧的路由状态优先于握手值,原始握手头也不能覆盖出站投影
|
||||
request.passthrough_headers.remove("x-codex-turn-state");
|
||||
} else if let Some(state) = context_string(context, "turn_state") {
|
||||
request.turn_state = Some(state);
|
||||
}
|
||||
request.parent_thread_id =
|
||||
context_string(context, "parent_thread_id").or_else(|| request.parent_thread_id.take());
|
||||
let prompt_cache_key = request.prompt_cache_key().map(ToOwned::to_owned);
|
||||
@@ -773,8 +762,6 @@ fn apply_protocol_context(request: &mut CodexResponsesRequest, context: &Map<Str
|
||||
request.client_thread_id = gateway_protocol::openai::codex_thread_id(request.body(), context);
|
||||
request.client_request_id =
|
||||
context_string(context, "client_request_id").or_else(|| request.client_request_id.take());
|
||||
request.client_turn_id =
|
||||
context_string(context, "turn_id").or_else(|| request.client_turn_id.take());
|
||||
request.responses_lite =
|
||||
context_string(context, "responses_lite").or_else(|| request.responses_lite.take());
|
||||
request.memgen_request =
|
||||
|
||||
@@ -32,8 +32,6 @@ use super::{
|
||||
pump::{PumpLogContext, PumpedWebSocket, transport_metric_reason},
|
||||
};
|
||||
|
||||
pub(crate) const WEBSOCKET_FAST_PATH_BUDGET: Duration = Duration::from_millis(800);
|
||||
|
||||
/// WebSocket 快路径的控制流结果;预算未命中不代表连接失败
|
||||
pub(crate) enum WebSocketFastPath<T> {
|
||||
/// 在前台预算内获得结果
|
||||
@@ -575,7 +573,7 @@ async fn wait_for_shared_connect(
|
||||
wait_for_fast_path(waiter.started_at(), fast_path_budget, waiter.wait()).await
|
||||
}
|
||||
|
||||
// 同一次 opening 的所有等待者共享截止时间;后来的请求不会重置 800ms 预算
|
||||
// 同一次 opening 的所有等待者共享截止时间
|
||||
async fn wait_for_fast_path<F: Future>(
|
||||
started_at: TokioInstant,
|
||||
budget: Option<Duration>,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! Responses WebSocket endpoint、opening handshake 与首帧发送
|
||||
//! Responses WebSocket endpoint、opening handshake 与请求发送
|
||||
|
||||
use std::{error::Error as _, io, time::Duration};
|
||||
|
||||
@@ -35,7 +35,7 @@ use super::{
|
||||
use super::control::WEBSOCKET_SEND_TIMEOUT;
|
||||
|
||||
const WEBSOCKET_EXTENSIONS: &str = "permessage-deflate; client_max_window_bits";
|
||||
const WEBSOCKET_CONNECT_TIMEOUT: Duration = Duration::from_secs(15);
|
||||
pub(crate) const WEBSOCKET_CONNECT_TIMEOUT: Duration = Duration::from_secs(15);
|
||||
|
||||
impl CodexWebSocketConnection {
|
||||
/// 复用传输库的握手编码检查,不能在拨号后才发现插件输出不可表示
|
||||
@@ -95,7 +95,7 @@ impl CodexWebSocketConnection {
|
||||
}
|
||||
}
|
||||
|
||||
/// 构造 Responses WebSocket opening 与首个 `response.create` 文本帧
|
||||
/// 构造 Responses WebSocket opening 与首条 `response.create` 消息
|
||||
pub fn responses_create_request(
|
||||
base_url: &str,
|
||||
websocket_key: &str,
|
||||
|
||||
@@ -39,11 +39,11 @@ pub use self::{
|
||||
};
|
||||
pub(crate) use self::{
|
||||
coordinator::{
|
||||
PreparedWebSocket, WEBSOCKET_FAST_PATH_BUDGET, WebSocketFastPath,
|
||||
execute_prepared_response_create_request_stream, post_send_ambiguous,
|
||||
prepare_response_create_request_with_pool,
|
||||
PreparedWebSocket, WebSocketFastPath, execute_prepared_response_create_request_stream,
|
||||
post_send_ambiguous, prepare_response_create_request_with_pool,
|
||||
},
|
||||
error::WEBSOCKET_CLOSE_MESSAGE_TOO_BIG,
|
||||
handshake::WEBSOCKET_CONNECT_TIMEOUT,
|
||||
live::{connect_live_sideband, into_live_relay},
|
||||
pool::DEFAULT_STREAM_IDLE_TIMEOUT,
|
||||
pool::{DEFAULT_FAST_PATH_BUDGET_MS, DEFAULT_STREAM_IDLE_TIMEOUT},
|
||||
};
|
||||
|
||||
@@ -147,7 +147,7 @@ impl CodexWebSocketRequest {
|
||||
&self.connection
|
||||
}
|
||||
|
||||
/// 返回将要发送的首个文本帧
|
||||
/// 返回将要发送的完整 `response.create` 文本
|
||||
pub fn payload_text(&self) -> &str {
|
||||
&self.payload_text
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ pub(crate) use self::{
|
||||
};
|
||||
|
||||
const DEFAULT_MAX_CONNECTING: usize = 16;
|
||||
pub(crate) const DEFAULT_FAST_PATH_BUDGET_MS: u64 = 800;
|
||||
const DEFAULT_MAX_AGE: Duration = Duration::from_mins(55);
|
||||
const DEFAULT_MAINTENANCE_INTERVAL: Duration = Duration::from_secs(5);
|
||||
const DEFAULT_IDLE_TIMEOUT: Duration = Duration::from_secs(300);
|
||||
@@ -70,6 +71,8 @@ pub struct CodexWebSocketPoolConfig {
|
||||
pub max_age: Duration,
|
||||
/// 所有账号合计允许并发执行的 opening 数
|
||||
pub max_connecting: usize,
|
||||
/// 允许回退 HTTP 的请求在前台等待 WebSocket 建连的时长
|
||||
pub fast_path_budget: Duration,
|
||||
/// 后台维护间隔;`None` 表示不启动后台任务
|
||||
pub maintenance_interval: Option<Duration>,
|
||||
/// 未绑定下游 WS 的连接归池后可空闲的时间,不由 Ping/Pong 刷新
|
||||
@@ -84,6 +87,7 @@ impl Default for CodexWebSocketPoolConfig {
|
||||
enabled: true,
|
||||
max_age: DEFAULT_MAX_AGE,
|
||||
max_connecting: DEFAULT_MAX_CONNECTING,
|
||||
fast_path_budget: Duration::from_millis(DEFAULT_FAST_PATH_BUDGET_MS),
|
||||
maintenance_interval: Some(DEFAULT_MAINTENANCE_INTERVAL),
|
||||
idle_timeout: DEFAULT_IDLE_TIMEOUT,
|
||||
stream_idle_timeout: Some(DEFAULT_STREAM_IDLE_TIMEOUT),
|
||||
@@ -117,6 +121,10 @@ impl CodexWebSocketPool {
|
||||
pool
|
||||
}
|
||||
|
||||
pub(crate) fn fast_path_budget(&self) -> Duration {
|
||||
self.config.fast_path_budget
|
||||
}
|
||||
|
||||
/// 等待下一条上游消息的空闲超时;`None` 或零值使用默认 300 秒
|
||||
pub(crate) fn stream_idle_timeout(&self) -> Option<Duration> {
|
||||
self.config.stream_idle_timeout
|
||||
|
||||
@@ -11,9 +11,13 @@ use std::sync::{
|
||||
};
|
||||
use std::time::Duration;
|
||||
|
||||
use bytes::Bytes;
|
||||
use futures::{Sink, SinkExt, Stream, StreamExt};
|
||||
use tokio::{sync::mpsc, task::JoinHandle, time::Instant};
|
||||
use tokio_tungstenite::tungstenite::Message;
|
||||
use tungstenite::{
|
||||
Message,
|
||||
protocol::frame::{Fragment, Frame, coding::Data},
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
@@ -311,7 +315,7 @@ impl PumpedWebSocket {
|
||||
}
|
||||
}
|
||||
|
||||
/// 通过 pump 发送一帧,区分未开始发送与底层传输失败
|
||||
/// 通过 pump 发送一条消息,区分未开始发送与底层传输失败
|
||||
pub(crate) async fn send(&self, message: Message) -> Result<(), CodexWebSocketExchangeError> {
|
||||
if self.is_closed() {
|
||||
return Err(CodexWebSocketExchangeError::SendNotStarted);
|
||||
@@ -613,7 +617,7 @@ async fn handle_command(
|
||||
return None;
|
||||
}
|
||||
let is_close = matches!(message, Message::Close(_));
|
||||
let result = inner.send(message).await;
|
||||
let result = write_message(inner, message).await;
|
||||
let transport_error = result
|
||||
.as_ref()
|
||||
.err()
|
||||
@@ -629,6 +633,31 @@ async fn handle_command(
|
||||
}
|
||||
}
|
||||
|
||||
/// 同一命令内逐帧写完大文本,保持一条消息的压缩上下文和发送回执
|
||||
async fn write_message(
|
||||
inner: &mut RawWsStream,
|
||||
message: Message,
|
||||
) -> Result<(), tungstenite::Error> {
|
||||
const FRAGMENT_BYTES: usize = 1024 * 1024;
|
||||
let mut remaining = match message {
|
||||
Message::Text(text) if text.len() > FRAGMENT_BYTES => Bytes::from(text),
|
||||
other => return inner.send(other).await,
|
||||
};
|
||||
let mut opcode = Data::Text;
|
||||
while !remaining.is_empty() {
|
||||
let payload = remaining.split_to(remaining.len().min(FRAGMENT_BYTES));
|
||||
let fragment = if remaining.is_empty() {
|
||||
Fragment::Final
|
||||
} else {
|
||||
Fragment::More
|
||||
};
|
||||
let frame = Frame::message_fragment(payload, opcode, fragment);
|
||||
inner.send(Message::Frame(frame)).await?;
|
||||
opcode = Data::Continue;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn record_activity(lifecycle: &Mutex<PumpLifecycleState>, observed_at: Instant) {
|
||||
lifecycle
|
||||
.lock()
|
||||
|
||||
@@ -69,7 +69,7 @@ const COMPLETED_SESSION_SSE: &str = concat!(
|
||||
);
|
||||
|
||||
#[tokio::test]
|
||||
async fn plan_display_should_distinguish_pro_tiers_and_preserve_other_official_names() {
|
||||
async fn plan_display_uses_pro_short_names_and_codex_status_labels_for_other_known_plans() {
|
||||
let config = valid_config();
|
||||
let bundle = provider_openai::initialize(
|
||||
config.config.clone(),
|
||||
@@ -86,25 +86,25 @@ async fn plan_display_should_distinguish_pro_tiers_and_preserve_other_official_n
|
||||
("free", "Free"),
|
||||
("go", "Go"),
|
||||
("plus", "Plus"),
|
||||
("team", "Team"),
|
||||
("self_serve_business_prolite", "Self Serve Business ProLite"),
|
||||
(
|
||||
"self_serve_business_usage_based",
|
||||
"Self Serve Business Usage Based",
|
||||
),
|
||||
("business", "Business"),
|
||||
("team", "Business"),
|
||||
("self_serve_business_prolite", "Business Premium"),
|
||||
("self_serve_business_usage_based", "Business"),
|
||||
("business", "Enterprise"),
|
||||
("ent26", "Enterprise"),
|
||||
("enterprise", "Enterprise"),
|
||||
("hc", "Enterprise"),
|
||||
("enterprise_cbp_automation", "Enterprise (Automation)"),
|
||||
("enterprise_cbp_usage_based", "Enterprise CBP Usage Based"),
|
||||
("enterprise_cbp_usage_based", "Enterprise"),
|
||||
("edu", "Edu"),
|
||||
("education", "Edu"),
|
||||
("edu_plus", "Edu Plus"),
|
||||
("edu_pro", "Edu Pro"),
|
||||
("future_plan", "future_plan"),
|
||||
("sci", "Academic Researchers"),
|
||||
("sci_plus", "Academic Researchers Plus"),
|
||||
("SCI_PLUS", "Academic Researchers Plus"),
|
||||
("Future_Plan", "Future_Plan"),
|
||||
] {
|
||||
assert_eq!(admin.plan_type_display(raw), display);
|
||||
assert_eq!(admin.plan_type_display(raw), display, "{raw}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -811,12 +811,6 @@ async fn openai_admin_provider_persists_the_full_pending_envelope_and_binds_owne
|
||||
.get("mutation")
|
||||
.and_then(Value::as_object)
|
||||
.expect("pending mutation");
|
||||
assert!(
|
||||
payload
|
||||
.expose_to_provider()
|
||||
.get("reauthorization_credential_revision")
|
||||
.is_none()
|
||||
);
|
||||
assert!(
|
||||
payload
|
||||
.expose_to_provider()
|
||||
@@ -829,13 +823,8 @@ async fn openai_admin_provider_persists_the_full_pending_envelope_and_binds_owne
|
||||
mutation.get("schema_version").and_then(Value::as_u64),
|
||||
Some(3)
|
||||
);
|
||||
assert!(mutation.get("expected_config_revision").is_none());
|
||||
assert!(
|
||||
mutation
|
||||
.get("target")
|
||||
.and_then(Value::as_object)
|
||||
.is_some_and(|target| target.get("expected_credential_revision").is_none())
|
||||
);
|
||||
assert_eq!(mutation["target"]["kind"], "create");
|
||||
assert_eq!(mutation["target"]["name"], "OAuth account");
|
||||
assert_eq!(
|
||||
mutation.get("started_request_id").and_then(Value::as_str),
|
||||
Some("request-start")
|
||||
@@ -1037,11 +1026,6 @@ async fn openai_reauthorization_pending_payload_reuses_the_account_installation_
|
||||
document.get("installation_id").and_then(Value::as_str),
|
||||
Some(expected_installation_id.as_str())
|
||||
);
|
||||
assert!(
|
||||
document
|
||||
.get("reauthorization_credential_revision")
|
||||
.is_none()
|
||||
);
|
||||
assert_eq!(
|
||||
target.get("kind").and_then(Value::as_str),
|
||||
Some("reauthorize")
|
||||
@@ -1050,7 +1034,6 @@ async fn openai_reauthorization_pending_payload_reuses_the_account_installation_
|
||||
target.get("account_id").and_then(Value::as_str),
|
||||
Some("acct_pending_reauth")
|
||||
);
|
||||
assert!(target.get("expected_credential_revision").is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -2942,7 +2925,6 @@ mod errors {
|
||||
.prepare_refresh(command())
|
||||
.await
|
||||
.unwrap_err();
|
||||
// 既有 transport 策略未认定此错误为安全重试,本次不能因展示更详细而放宽重试边界
|
||||
assert_eq!(error.kind(), Kind::Ambiguous);
|
||||
assert_eq!(
|
||||
error.public_message(),
|
||||
|
||||
@@ -96,26 +96,53 @@ fn openai_stream_retry_budget_uses_the_official_hard_cap() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn websocket_pool_idle_timeout_accepts_existing_configs_and_explicit_overrides() {
|
||||
for (idle_timeout_ms, expected) in [(None, 300_000), (Some(600_000), 600_000)] {
|
||||
let mut document = serde_json::json!({
|
||||
"ws_pool": {
|
||||
fn websocket_pool_timeouts_accept_defaults_and_individual_overrides() {
|
||||
for (settings, idle_ms, fast_ms) in [
|
||||
(
|
||||
serde_json::json!({
|
||||
"enabled": true,
|
||||
"max_age_ms": 3_300_000,
|
||||
"max_connecting": 8,
|
||||
"stream_idle_timeout_ms": 300_000
|
||||
}
|
||||
});
|
||||
if let Some(idle_timeout_ms) = idle_timeout_ms {
|
||||
document["ws_pool"]["idle_timeout_ms"] = serde_json::json!(idle_timeout_ms);
|
||||
}
|
||||
let mut config: OpenAiConfig = serde_json::from_value(document).unwrap();
|
||||
}),
|
||||
300_000,
|
||||
800,
|
||||
),
|
||||
(
|
||||
serde_json::json!({"idle_timeout_ms": 600_000}),
|
||||
600_000,
|
||||
800,
|
||||
),
|
||||
(serde_json::json!({"fast_path_budget_ms": 1}), 300_000, 1),
|
||||
(
|
||||
serde_json::json!({"fast_path_budget_ms": 15_000}),
|
||||
300_000,
|
||||
15_000,
|
||||
),
|
||||
] {
|
||||
let mut config: OpenAiConfig =
|
||||
serde_json::from_value(serde_json::json!({"ws_pool": settings})).unwrap();
|
||||
config
|
||||
.resolve_and_validate(Path::new("/srv/gateway"))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
config.websocket_pool_config().idle_timeout,
|
||||
Duration::from_millis(expected)
|
||||
let pool = config.websocket_pool_config();
|
||||
assert_eq!(pool.idle_timeout, Duration::from_millis(idle_ms));
|
||||
assert_eq!(pool.fast_path_budget, Duration::from_millis(fast_ms));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn websocket_fast_path_budget_must_fit_the_opening_timeout() {
|
||||
for budget_ms in [0, 15_001] {
|
||||
let mut config = valid_config();
|
||||
config.ws_pool.fast_path_budget_ms = budget_ms;
|
||||
let error = config
|
||||
.resolve_and_validate(Path::new("/srv/gateway"))
|
||||
.unwrap_err();
|
||||
assert!(
|
||||
error
|
||||
.to_string()
|
||||
.contains("openai.ws_pool.fast_path_budget_ms")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -738,6 +738,108 @@ async fn invalidation_queues_background_refresh_without_an_upstream_etag() {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn matching_etag_renews_only_its_account_until_the_next_change_or_expiry() {
|
||||
use gateway_core::account::AccountCatalogObservation;
|
||||
use std::time::{Duration, SystemTime};
|
||||
|
||||
let store = Arc::new(MemoryAccountStore::default());
|
||||
let active = seed_account(&store, "acct_active").await;
|
||||
let idle = seed_account(&store, "acct_idle").await;
|
||||
let document = [&[0_u8][..], OFFICIAL_FIXTURE].concat();
|
||||
let old_tag = r#""v1""#;
|
||||
let new_tag = r#""v2""#;
|
||||
let observed_at = SystemTime::now() - Duration::from_mins(61);
|
||||
for account in [&active, &idle] {
|
||||
assert!(
|
||||
store
|
||||
.save_account_catalog(
|
||||
&AccountCatalogObservation {
|
||||
account_id: account.id().clone(),
|
||||
revision: account.revision(),
|
||||
plan: account.plan_type().map(str::to_owned),
|
||||
observed_at,
|
||||
etag: Some(old_tag.to_owned()),
|
||||
},
|
||||
&document
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
let server = MockServer::start().await;
|
||||
for account in [&active, &idle] {
|
||||
Mock::given(method("GET"))
|
||||
.and(path("/codex/models"))
|
||||
.and(header(
|
||||
"chatgpt-account-id",
|
||||
format!("chatgpt-{}", account.id()),
|
||||
))
|
||||
.respond_with(
|
||||
ResponseTemplate::new(200)
|
||||
.insert_header("etag", old_tag)
|
||||
.set_body_raw(OFFICIAL_FIXTURE, "application/json"),
|
||||
)
|
||||
.expect(1)
|
||||
.mount(&server)
|
||||
.await;
|
||||
}
|
||||
let service = service_with_catalog_cache(&store, server.uri(), catalog_cache());
|
||||
service.restore().await.unwrap();
|
||||
assert!(!service.observe_response_etag(&active, old_tag).unwrap());
|
||||
service.refresh_catalogs().await.unwrap();
|
||||
let requests = server.received_requests().await.unwrap();
|
||||
assert_eq!(requests.len(), 1);
|
||||
assert_eq!(
|
||||
requests[0].headers["chatgpt-account-id"],
|
||||
"chatgpt-acct_idle"
|
||||
);
|
||||
|
||||
tokio::time::pause();
|
||||
tokio::time::advance(Duration::from_mins(59)).await;
|
||||
service.refresh_catalogs().await.unwrap();
|
||||
assert_eq!(server.received_requests().await.unwrap().len(), 1);
|
||||
tokio::time::advance(Duration::from_mins(1)).await;
|
||||
tokio::time::resume();
|
||||
service.refresh_catalogs().await.unwrap();
|
||||
server.verify().await;
|
||||
|
||||
server.reset().await;
|
||||
Mock::given(method("GET"))
|
||||
.and(path("/codex/models"))
|
||||
.and(header("chatgpt-account-id", "chatgpt-acct_active"))
|
||||
.respond_with(
|
||||
ResponseTemplate::new(200)
|
||||
.insert_header("etag", new_tag)
|
||||
.set_body_json(
|
||||
serde_json::json!({"models":[{"slug":"active-only","display_name":"Active"}]}),
|
||||
),
|
||||
)
|
||||
.expect(1)
|
||||
.mount(&server)
|
||||
.await;
|
||||
assert!(!service.observe_response_etag(&active, old_tag).unwrap());
|
||||
assert!(
|
||||
service
|
||||
.observe_response_etag(&active, "change-hint")
|
||||
.unwrap()
|
||||
);
|
||||
assert!(!service.observe_response_etag(&active, old_tag).unwrap());
|
||||
service.refresh_catalogs().await.unwrap();
|
||||
assert!(
|
||||
!service
|
||||
.observe_response_etag(&active, "change-hint")
|
||||
.unwrap()
|
||||
);
|
||||
assert!(!service.observe_response_etag(&active, new_tag).unwrap());
|
||||
service.refresh_catalogs().await.unwrap();
|
||||
assert_eq!(
|
||||
service.cached_account_models(&active).unwrap().unwrap(),
|
||||
["active-only"]
|
||||
);
|
||||
server.verify().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn simultaneous_periodic_refreshes_share_the_completed_scan() {
|
||||
let store = Arc::new(MemoryAccountStore::default());
|
||||
|
||||
@@ -232,7 +232,6 @@ async fn warmup_uses_global_profile_and_lowest_reasoning_effort() {
|
||||
zstd::stream::decode_all(requests[0].body.as_slice()).expect("zstd body should decode");
|
||||
let body: serde_json::Value =
|
||||
serde_json::from_slice(&decoded).expect("warmup body should be valid JSON");
|
||||
// 上游按目录拒绝目录外的档位,none 曾让全部定时预热只留下 error 终态
|
||||
assert_eq!(body["reasoning"]["effort"], "low");
|
||||
*policy.0.lock().unwrap() = OpaqueProviderData::new(
|
||||
json!({"mode":"custom","userAgent":"invalid"})
|
||||
|
||||
@@ -48,13 +48,15 @@ fn isolation_payload() -> ProtocolPayload {
|
||||
turn_metadata.insert("parent_response_id".to_owned(), json!("client-correlation"));
|
||||
turn_metadata.insert("workspace".to_owned(), json!("/工作区"));
|
||||
turn_metadata.insert("future".to_owned(), json!({"traceparent":"business-value"}));
|
||||
let mut frame_metadata = turn_metadata.clone();
|
||||
frame_metadata.insert("tool_namespaces_info".into(), json!({"workspace":{}}));
|
||||
let mut body = json!({
|
||||
"model": "gpt-5.4",
|
||||
"input": isolation_history(),
|
||||
"client_metadata": {
|
||||
"parent_response_id": "resp_parent_old",
|
||||
"guardian_credits_requested": "true",
|
||||
"x-codex-turn-metadata": Value::Object(turn_metadata.clone()).to_string(),
|
||||
"x-codex-turn-metadata": Value::Object(frame_metadata).to_string(),
|
||||
"turnMetadata": turn_metadata,
|
||||
"mcp_attribution": r#"{"status":"complete","sources":[]}"#
|
||||
}
|
||||
@@ -86,6 +88,10 @@ fn isolation_payload() -> ProtocolPayload {
|
||||
ProtocolPayload::json_object("openai", body.as_object().unwrap().clone())
|
||||
.unwrap()
|
||||
.with_context(Map::from_iter([
|
||||
(
|
||||
gateway_protocol::openai::WEBSOCKET_FRAME_CONTEXT_KEY.into(),
|
||||
json!(true),
|
||||
),
|
||||
("opaque_request_headers".to_owned(), json!(headers)),
|
||||
(
|
||||
"turn_metadata".to_owned(),
|
||||
@@ -155,7 +161,6 @@ fn assert_isolated_request(
|
||||
&body["turnMetadata"],
|
||||
&body["turn_metadata"],
|
||||
&body["client_metadata"]["turnMetadata"],
|
||||
&body["client_metadata"]["x-codex-turn-metadata"],
|
||||
] {
|
||||
let actual = match metadata {
|
||||
Value::String(raw) => serde_json::from_str(raw).unwrap(),
|
||||
@@ -163,6 +168,15 @@ fn assert_isolated_request(
|
||||
};
|
||||
assert_eq!(actual, Value::Object(expected_turn_metadata.clone()));
|
||||
}
|
||||
let mut expected_frame_metadata = expected_turn_metadata.clone();
|
||||
expected_frame_metadata.insert("tool_namespaces_info".into(), json!({"workspace":{}}));
|
||||
let frame = body["client_metadata"]["x-codex-turn-metadata"]
|
||||
.as_str()
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(frame).unwrap(),
|
||||
Value::Object(expected_frame_metadata)
|
||||
);
|
||||
let header = headers["x-codex-turn-metadata"].to_str().unwrap();
|
||||
assert!(header.is_ascii());
|
||||
assert_eq!(
|
||||
|
||||
@@ -128,6 +128,9 @@ async fn upstream_codes_survive_http_and_websocket_opening_without_normalization
|
||||
body,
|
||||
);
|
||||
let diagnostic = error.diagnostic().expect("safe diagnostic");
|
||||
if expected_kind == ProviderErrorKind::ContinuationRecoveryRequired {
|
||||
assert_eq!(diagnostic.as_str(), "上游找不到先前的响应");
|
||||
}
|
||||
assert!(!diagnostic.as_str().contains(original_code));
|
||||
assert!(!diagnostic.as_str().contains("PRIVATE_UPSTREAM_DETAIL"));
|
||||
assert!(!format!("{error:?} {error}").contains(original_code));
|
||||
|
||||
@@ -2053,8 +2053,6 @@ async fn provider_should_override_unclassified_time_contexts_over_websocket() {
|
||||
}
|
||||
|
||||
async fn capture_turn_state_request(
|
||||
request_id: &str,
|
||||
previous_turn_id: Option<&str>,
|
||||
current_turn_id: Option<&str>,
|
||||
client_turn_state: Option<&str>,
|
||||
) -> wiremock::Request {
|
||||
@@ -2072,18 +2070,19 @@ async fn capture_turn_state_request(
|
||||
.expect(1)
|
||||
.mount(&server)
|
||||
.await;
|
||||
let mut session_state = Map::from_iter([
|
||||
let session_state = Map::from_iter([
|
||||
("account_id".to_owned(), json!(account_id)),
|
||||
("conversation_id".to_owned(), json!("conversation")),
|
||||
("turn_state".to_owned(), json!("previous-turn-state")),
|
||||
("client_turn_id".to_owned(), json!("turn-same")),
|
||||
("continuation_scope".to_owned(), json!("persisted")),
|
||||
]);
|
||||
if let Some(previous_turn_id) = previous_turn_id {
|
||||
session_state.insert("client_turn_id".to_owned(), json!(previous_turn_id));
|
||||
}
|
||||
let mut protocol_context = Map::from_iter([("use_websocket".to_owned(), json!(false))]);
|
||||
if let Some(current_turn_id) = current_turn_id {
|
||||
protocol_context.insert("turn_id".to_owned(), json!(current_turn_id));
|
||||
protocol_context.insert(
|
||||
"turn_metadata".to_owned(),
|
||||
Value::String(json!({"turn_id": current_turn_id}).to_string()),
|
||||
);
|
||||
}
|
||||
if let Some(client_turn_state) = client_turn_state {
|
||||
protocol_context.insert("turn_state".to_owned(), json!(client_turn_state));
|
||||
@@ -2095,6 +2094,7 @@ async fn capture_turn_state_request(
|
||||
let mut body = Map::from_iter([
|
||||
("model".to_owned(), json!("gpt-5.4")),
|
||||
("input".to_owned(), json!("current input")),
|
||||
("previous_response_id".to_owned(), json!("resp_previous")),
|
||||
("client_metadata".to_owned(), json!({"custom":"preserved"})),
|
||||
]);
|
||||
if let Some(state) = client_turn_state {
|
||||
@@ -2119,7 +2119,7 @@ async fn capture_turn_state_request(
|
||||
let mut stream = provider_with_base_url(&store, server.uri())
|
||||
.execute(
|
||||
planned_request("openai", operation),
|
||||
context(request_id, CancellationToken::new()),
|
||||
context("req_client_turn_state", CancellationToken::new()),
|
||||
)
|
||||
.await
|
||||
.expect("prepare turn-state provider stream");
|
||||
@@ -3559,7 +3559,7 @@ async fn assert_cross_endpoint_affinity(thread_id: Option<&str>) {
|
||||
}
|
||||
drop(same);
|
||||
|
||||
// 仍由旧租约流程报告繁忙并换号,亲和不绕过并发限制
|
||||
// 亲和账号的并发租约已满时,请求应切换账号
|
||||
leases
|
||||
.busy_accounts
|
||||
.lock()
|
||||
@@ -7093,78 +7093,29 @@ async fn missing_affinity_full_request_should_preserve_client_turn_state() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn matching_turn_id_should_restore_previous_turn_state() {
|
||||
let request = capture_turn_state_request(
|
||||
"req_restore_same_turn_state",
|
||||
Some("turn-same"),
|
||||
Some("turn-same"),
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
|
||||
assert_eq!(
|
||||
captured_header_values(&request, "x-codex-turn-state"),
|
||||
vec![b"previous-turn-state".to_vec()]
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn matching_turn_id_should_prefer_an_explicit_client_echo_over_saved_provider_state() {
|
||||
let request = capture_turn_state_request(
|
||||
"req_use_client_turn_state",
|
||||
Some("turn-same"),
|
||||
Some("turn-same"),
|
||||
Some("client-turn-state"),
|
||||
)
|
||||
.await;
|
||||
|
||||
assert_eq!(
|
||||
captured_header_values(&request, "x-codex-turn-state"),
|
||||
vec![b"client-turn-state".to_vec()]
|
||||
);
|
||||
let body = captured_request_body(&request);
|
||||
for key in ["turnState", "turn_state", "x-codex-turn-state"] {
|
||||
assert_eq!(body[key], "client-turn-state");
|
||||
assert_eq!(body["client_metadata"][key], "client-turn-state");
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn new_or_unidentified_turn_should_not_restore_previous_turn_state() {
|
||||
for (request_id, previous_turn_id, current_turn_id, client_turn_state) in [
|
||||
(
|
||||
"req_new_turn_state",
|
||||
Some("turn-old"),
|
||||
Some("turn-new"),
|
||||
Some("stale-client-turn-state"),
|
||||
),
|
||||
(
|
||||
"req_unidentified_turn_state",
|
||||
None,
|
||||
Some("turn-new"),
|
||||
Some("stale-client-turn-state"),
|
||||
),
|
||||
(
|
||||
"req_missing_current_turn_state",
|
||||
Some("turn-old"),
|
||||
None,
|
||||
Some("stale-client-turn-state"),
|
||||
),
|
||||
async fn same_account_continuation_forwards_only_the_current_request_turn_state() {
|
||||
// 旧版会话中可能仍有保存的轮次和 token
|
||||
for (turn, supplied) in [
|
||||
(Some("turn-same"), None),
|
||||
(Some("turn-same"), Some("client-turn-state")),
|
||||
(Some("turn-new"), Some("client-turn-state")),
|
||||
(None, Some("client-turn-state")),
|
||||
] {
|
||||
let request = capture_turn_state_request(
|
||||
request_id,
|
||||
previous_turn_id,
|
||||
current_turn_id,
|
||||
client_turn_state,
|
||||
)
|
||||
.await;
|
||||
assert!(captured_header_values(&request, "x-codex-turn-state").is_empty());
|
||||
let request = capture_turn_state_request(turn, supplied).await;
|
||||
let headers = captured_header_values(&request, "x-codex-turn-state");
|
||||
assert_eq!(
|
||||
headers,
|
||||
supplied
|
||||
.map(|value| value.as_bytes())
|
||||
.into_iter()
|
||||
.collect::<Vec<_>>()
|
||||
);
|
||||
let body = captured_request_body(&request);
|
||||
for key in ["turnState", "turn_state", "x-codex-turn-state"] {
|
||||
assert!(body.get(key).is_none(), "stale body state: {key}");
|
||||
assert!(
|
||||
body["client_metadata"].get(key).is_none(),
|
||||
"stale metadata state: {key}"
|
||||
assert_eq!(body.get(key).and_then(Value::as_str), supplied);
|
||||
assert_eq!(
|
||||
body["client_metadata"].get(key).and_then(Value::as_str),
|
||||
supplied
|
||||
);
|
||||
}
|
||||
assert_eq!(body["input"][0]["content"][0]["text"], "current input");
|
||||
@@ -8924,6 +8875,7 @@ async fn capacity_http_and_websocket_opening_rejections_use_bounded_business_ret
|
||||
}
|
||||
};
|
||||
assert_eq!(error.kind(), ProviderErrorKind::UpstreamCapacityUnavailable);
|
||||
assert_eq!(error.diagnostic().unwrap().as_str(), "上游模型容量不足");
|
||||
assert_eq!(error.upstream_status(), Some(status));
|
||||
assert!(error.replay_is_safe());
|
||||
assert_eq!(error.retry_after(), Some(Duration::from_secs(129600)));
|
||||
|
||||
@@ -2,6 +2,45 @@
|
||||
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn websocket_body_fields_stay_in_the_body_when_using_http_upstream() {
|
||||
let frame = r#"{"request_kind":"turn","tool_namespaces_info":{"workspace":{}}}"#;
|
||||
let body = json!({
|
||||
"model": "gpt-5.4", "input": "hello",
|
||||
"client_metadata": {"x-codex-turn-metadata": frame},
|
||||
"betaFeatures": "only-in-body", "includeTimingMetrics": "true"
|
||||
});
|
||||
let sent = capture_scoped_http_request(
|
||||
"req_frame_only",
|
||||
"acct_scope_same",
|
||||
"acct_scope_same",
|
||||
body.as_object().unwrap().clone(),
|
||||
Map::from_iter([(
|
||||
gateway_protocol::openai::WEBSOCKET_FRAME_CONTEXT_KEY.into(),
|
||||
json!(true),
|
||||
)]),
|
||||
)
|
||||
.await;
|
||||
for header in [
|
||||
"x-codex-turn-metadata",
|
||||
"x-codex-beta-features",
|
||||
"x-responsesapi-include-timing-metrics",
|
||||
] {
|
||||
assert!(!sent.headers.contains_key(header), "{header}");
|
||||
}
|
||||
let sent_body = captured_request_body(&sent);
|
||||
let sent_frame = sent_body["client_metadata"]["x-codex-turn-metadata"]
|
||||
.as_str()
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(sent_frame).unwrap(),
|
||||
serde_json::from_str::<Value>(frame).unwrap()
|
||||
);
|
||||
for key in ["betaFeatures", "includeTimingMetrics"] {
|
||||
assert_eq!(sent_body[key], body[key]);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn responses_options_follow_the_selected_upstream_on_both_transports() {
|
||||
let oauth_omissions = json!({
|
||||
|
||||
@@ -70,6 +70,78 @@ pub(super) async fn stream_failure(
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn bare_websocket_bad_request_offers_http_error_only_for_native_responses_sse() {
|
||||
use ClientTransport::{HttpJson, HttpSse, WebSocket};
|
||||
use gateway_protocol::openai::RESPONSES_PATH;
|
||||
|
||||
let detail = json!({
|
||||
"type":"invalid_request_error", "code":"invalid_value", "message":"Invalid input.",
|
||||
"param":"input[2].id", "extension":{"reason":"duplicate"}
|
||||
});
|
||||
let bad_request = json!({"type":"error", "status":400, "error":detail, "sequence_number":7});
|
||||
let no_status = json!({"type":"error", "error":detail});
|
||||
let response_failed = json!({"type":"response.failed", "status":400, "response":{"id":"resp_failed","error":detail}});
|
||||
let capacity = json!({"status":400, "type":"error", "error":overload()["error"]});
|
||||
for (endpoint, transport, frame, expects_http) in [
|
||||
(RESPONSES_PATH, HttpSse, &bad_request, true),
|
||||
(RESPONSES_PATH, WebSocket, &bad_request, false),
|
||||
(RESPONSES_PATH, HttpJson, &bad_request, false),
|
||||
("/v1/chat/completions", HttpSse, &bad_request, false),
|
||||
(RESPONSES_PATH, HttpSse, &no_status, false),
|
||||
(RESPONSES_PATH, HttpSse, &response_failed, false),
|
||||
(RESPONSES_PATH, HttpSse, &capacity, false),
|
||||
] {
|
||||
let store = Arc::new(MemoryAccountStore::default());
|
||||
create_account(&store, "acct_provider_contract").await;
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let base_url = format!("http://{}", listener.local_addr().unwrap());
|
||||
let raw = frame.to_string();
|
||||
let server = tokio::spawn(async move {
|
||||
let (socket, _) = listener.accept().await.unwrap();
|
||||
let mut ws = accept_codex_test_websocket(socket).await;
|
||||
ws.next().await.unwrap().unwrap();
|
||||
ws.send(Message::Text(raw.into())).await.unwrap();
|
||||
});
|
||||
let request = RequestAttemptContext::new(
|
||||
ModelRequestId::new("req_ws_bad_input").unwrap(),
|
||||
ClientApiKeyId::new("key_openai_contract").unwrap(),
|
||||
)
|
||||
.with_middleware(None, Arc::from([]), endpoint.to_owned(), transport)
|
||||
.with_request_location(Some(global_request_location()));
|
||||
let mut stream = provider_with_base_url(&store, base_url)
|
||||
.execute(
|
||||
planned_request("openai", generate_operation()),
|
||||
context_for_request(request, CancellationToken::new()),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let (events, mut error) = stream_failure(&mut stream).await;
|
||||
assert!(events.iter().all(|event| !event.has_client_event()));
|
||||
let response = error.client_visible_upstream_response();
|
||||
assert_eq!(
|
||||
response.is_some(),
|
||||
expects_http,
|
||||
"{endpoint} {transport:?} {frame}"
|
||||
);
|
||||
if let Some(response) = response {
|
||||
assert_eq!(response.status(), 400);
|
||||
assert_eq!(
|
||||
response.content_type(),
|
||||
Some(b"application/json".as_slice())
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::from_slice::<Value>(response.body()).unwrap(),
|
||||
json!({"error":detail})
|
||||
);
|
||||
}
|
||||
let events = error.take_atomic_client_events();
|
||||
assert_eq!(events.len(), 1);
|
||||
assert_eq!(events[0].wire_event().unwrap().data(), frame);
|
||||
server.await.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn large_preludes_are_forwarded_before_failure_on_http_and_websocket() {
|
||||
for websocket in [false, true] {
|
||||
|
||||
@@ -431,8 +431,6 @@ fn context() -> CodexRequestContext<'static> {
|
||||
request_id: "req_catalog",
|
||||
turn_state: None,
|
||||
turn_metadata: None,
|
||||
beta_features: None,
|
||||
include_timing_metrics: None,
|
||||
version: None,
|
||||
codex_window_id: None,
|
||||
parent_thread_id: None,
|
||||
@@ -441,7 +439,6 @@ fn context() -> CodexRequestContext<'static> {
|
||||
session_id: None,
|
||||
thread_id: None,
|
||||
client_request_id: None,
|
||||
turn_id: None,
|
||||
account_selection: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,8 +34,6 @@ fn request_context_debug_should_redact_all_identity_material() {
|
||||
request_id: "req_safe",
|
||||
turn_state: Some("turn-secret-marker"),
|
||||
turn_metadata: Some("metadata-secret-marker"),
|
||||
beta_features: None,
|
||||
include_timing_metrics: None,
|
||||
version: None,
|
||||
codex_window_id: None,
|
||||
parent_thread_id: None,
|
||||
@@ -44,7 +42,6 @@ fn request_context_debug_should_redact_all_identity_material() {
|
||||
session_id: None,
|
||||
thread_id: None,
|
||||
client_request_id: None,
|
||||
turn_id: None,
|
||||
account_selection: Default::default(),
|
||||
};
|
||||
let debug = format!("{context:?}");
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user