Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions changelog/unreleased/routing-attempt-and-cooldown.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### English

- Reserve an account and update rotation together. Full accounts do not consume an upstream attempt, and failover cannot leave the provider or region allowed by the request and API key.
- Use the selected account and concrete model for dispatch, cooldowns, and stream feedback, including native Responses and older workers. API-key concurrency is checked before any aggregation classifier can send an upstream request.
- Priority aggregations rotate among available targets in the best tier. `max_attempts` now caps dispatches of the requested model across all tiers, not the attempts per tier.
- Named dispatch groups preserve member order when a Responses request can use either protocol, and honor explicit no-failover errors. Early stream heartbeats do not announce a provisional account.
- Keep nested group listings and previews in sync with configured members. Quota checks use the resolved model, and late successes do not restore models that were explicitly rejected.
- Keep the public model list consistent with API-key grants and routing, including namespaced models and group names that shadow account aliases. Canceled streams no longer cool down healthy accounts, and stale session affinity cannot block a healthy fallback.

### 中文

- 账号选择、占槽和轮转一起提交。满载账号不消耗上游尝试次数,失败回退不会越过请求和 API Key 允许的提供商或区域。
- 派发、冷却和流式反馈使用实际选中的账号和模型,覆盖原生 Responses 和旧 worker。聚合分类请求发往上游前也会先取得 API Key 并发槽。
- 优先级聚合在最高可用档内轮转。`max_attempts` 现在限制请求模型在所有档位的总派发次数,而不是每个档位各自计数。
- 兼容两种协议的 Responses 请求按调度组顺序派发,并遵守明确禁止切换的错误。流式早期心跳不再公布尚未确定的账号。
- 嵌套调度组列表和预览使用当前成员配置;额度按实际模型检查,旧请求晚到的成功不会恢复已明确拒绝的模型。
- 公开模型列表与 API Key 授权和实际派发保持一致,正确处理带命名空间的模型及被调度组占用的账号别名。取消流式请求不会误冷却健康账号,失效的会话粘性也不会挡住健康备选。
31 changes: 18 additions & 13 deletions docs/NATIVE_PROTOCOL_REQUESTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ title: 原生协议请求的现状与后续
scope: [gateway, executor, translate, providers]
status: current
read-when: 理解三个公共入口与上游协议的关系、改 Responses 原生链路或评估 ChatRequest 边界时
summary: 三个公共入口的当前转发方式。Responses 在 adapter 声明原生能力时保留原文;Chat 与 Messages 仍先译成 ChatRequest。Codex 两条路径共用同一套上游约束。
summary: 三个公共入口的当前转发方式。Responses 保留原文;可兼容转换的命名聚合组先按组策略选择成员,再决定是否原生派发。Chat 与 Messages 仍先译成 ChatRequest。
related: [AGENTS.md, docs/ARCHITECTURE_SUMMARY.md, docs/REFACTORING.md, docs/DEVELOPMENT.md]
last-updated: 2026-09-25
last-updated: 2026-12-02
---

# 原生协议请求的现状与后续
Expand All @@ -17,7 +17,7 @@ last-updated: 2026-09-25

- `translate.ChatRequest` 仍是兼容表示,不再是所有请求的强制中间表示。
- 客户端入口决定返回协议,adapter 决定怎么访问上游。两边互不绑定。
- `/v1/responses` 先保留原文。候选账号的 adapter 实现了 `NativeResponsesStreamer` 时走原生路径;否则才在兼容转换能成功时走 `ChatRequest`。
- `/v1/responses` 先保留原文。普通请求优先走实现了 `NativeResponsesStreamer` 的候选账号;可兼容转换的命名聚合组先按组策略选成员,再按成员的 adapter 能力派发。
- `/v1/chat/completions` 和 `/v1/messages` 没有原生路径。它们仍先变成 `ChatRequest`。选中 Codex 时,adapter 再把它建成 Responses 请求。
- 路由、权限、冷却、重试和日志仍在 executor。没有第二套直通执行器。
- 原生能力看接口,不看 provider 名字。gateway 里没有 `if provider == "codex"`。
Expand All @@ -29,9 +29,9 @@ last-updated: 2026-09-25
```text
/v1/messages Messages → ChatRequest ─────────────┐
/v1/chat/completions ChatRequest ────────────────────────┼─→ 选号
/v1/responses 保留原文 ──→ 有原生 adapter? │
├─ 是:原文副本 + 本次执行参数 ──┤
└─ 否:能转 Chat 才继续 ────────┘
/v1/responses 保留原文,尝试 Chat 兼容转换 ─────────┘
普通请求:有原生候选时只选原生
命名聚合组:转换成功时不按协议重排成员
│
选中的 adapter 决定上游协议
Codex:两条路径都打 Responses,并套同一套上游约束
Expand All @@ -46,14 +46,18 @@ last-updated: 2026-09-25

`HandleResponses` 有上限地读完 body,解析为 `translate.NativeResponsesRequest`。非法 JSON、非对象、尾随值、缺 `model` 或 `input`、以及 `previous_response_id` / `conversation` 直接拒绝。

兼容转换 `Compat()` 会试一次,但失败不再挡住请求。随后:
网关先尝试兼容转换 `Compat()`;转换失败只限制候选协议,不直接挡住请求。随后:

1. 有实现 `NativeResponsesStreamer` 的候选账号时,只在这些账号里选。每次尝试从原文取一份副本,带上 `providers.RequestOptions`(本次上游模型、是否去掉 system/developer)。流式转发上游 SSE;`stream:false` 仍向上游要 SSE,再由 `translate.CollectResponses` 取出终态 `response` 对象。失败不降级到 Chat 重建。
2. 没有原生账号,且原文能转成 `ChatRequest` 时,走原来的 `ChatStream` / `ChatNonStream`,网关再把结果拼回 Responses。
3. 两边都不行,返回 `unsupported_request`。
1. **普通请求**:有原生候选时只选原生账号,暂时满载或冷却并不会让它降级到兼容路径。没有原生候选且转换成功时,使用 `ChatStream` / `ChatNonStream`,由网关把结果编码回 Responses。
2. **命名聚合组且转换成功**:与 Chat 使用同一组选成员规则,不为了协议优先级重排。每次尝试由选中成员决定:支持 `NativeResponsesStreamer` 就用原文,否则用兼容 `ChatRequest`。组允许回退时,两种路径共用同一个 `max_attempts` 派发预算。
3. **转换失败**:无论是否命名聚合组,都只允许原生成员;若没有符合条件的原生候选,返回 `unsupported_request`,不丢字段重建请求。

每次原生尝试用原文副本和 `providers.RequestOptions`(本次上游模型、是否去掉 system/developer)。流式请求转发 SSE;`stream:false` 仍向上游要 SSE,再由 `translate.CollectResponses` 取出终态 `response` 对象。

现在只有 Codex 实现了这个接口。

**English summary:** Ordinary Responses requests remain native-only when the route has a native-capable candidate. A named aggregation whose request can be translated to Chat selects members using its configured strategy, then chooses native or Chat dispatch per member. Both share the group’s attempt budget. Requests that cannot be translated to Chat still require a native member.

### 2.2 `/v1/chat/completions` 和 `/v1/messages`

这两条没有原文保留,也没有原生分派。
Expand All @@ -70,7 +74,7 @@ last-updated: 2026-09-25
| 内容 | 现在的位置 | 约束 |
| --- | --- | --- |
| Responses 原文 | `translate.NativeResponsesRequest` | 自有 body。`Fields()` 每次返回副本,failover 不能改到下一次 |
| 兼容表示 | `translate.ChatRequest` | Chat/Messages 的唯一内容;Responses 仅在没有原生账号且转换成功时使用 |
| 兼容表示 | `translate.ChatRequest` | Chat/Messages 的唯一内容;Responses 转换成功且本次路由使用兼容 adapter 时使用 |
| 本次执行决定 | `providers.RequestOptions` | 显式参数,不放进 context。目前是上游模型 id 和 `DropSystemPrompt` |
| 原生能力 | `NativeResponsesStreamer.ResponsesStream` | 入参是原文指针和 `RequestOptions`,返回上游 Responses SSE |
| 事件判定 | `translate.ParseResponsesEvent` | relay 和 collector 共用。`response.failed` 是终态错误,不再补第二条错误帧 |
Expand Down Expand Up @@ -115,9 +119,10 @@ Chat/Messages 翻译路径还会先做消息到 item 的重建(tool 结果变
- 原生流式转发上游事件,不压成 chat delta。namespace 工具名在响应里还原。
- 原生非流式只取 `response.completed` 或 `response.incomplete` 上的完整 `response` 对象,不经 `ChatOutcome`。
- `response.failed` 记为错误。事件已经转给客户端时,不再追加第二条错误帧。
- 原生候选耗尽后不自动改走有损的 Chat 重建。没有原生候选且兼容转换也失败时,请求直接失败。
- 普通请求的原生候选耗尽后不自动走兼容路径。只有转换成功的命名聚合组可以在同一派发预算内按配置回退到兼容成员,仍遵守上游明确禁止回退的错误。
- 协议不兼容不给账号加冷却。上游的限流、认证和可用性错误仍走原来的分类。
- 响应已经开始写给客户端之后不换上游再拼一段。
- 流式请求在接受首个有效上游事件后不换上游再拼一段。Chat 的早期 heartbeat 注释不是上游事件,只保活、不公布尚未确定的账号。
- 如果客户端已取消,且流只给出 EOF 或无法确定来源的读取错误,不将其计作账号故障;已确认的终态成功或明确的上游错误保留原结果。
- 原文、加密内容和凭证不进默认日志。

fake 测试证明字段按上面的清单改写、其余成员留下来、失败终态不会被当成成功。它不证明某个真实 ChatGPT 账号接受全部保留字段,也不证明加密 reasoning 可以跨账号回放。
Expand Down
Loading
Loading