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
9 changes: 9 additions & 0 deletions changelog/unreleased/provider-tool-profiles.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### English

- Give every adapter the canonical tool profile from `internal/toolcall`, so declared tools, tool choice and parallel calls reach the upstream payload unchanged.
- Replay a transient failure inside the adapter and report the internal retry count, keep an upstream `Retry-After` instead of a generic backoff, and stop a stream that ends without a terminal frame.

### 中文

- 各适配器统一使用 `internal/toolcall` 的规范工具档案,声明的工具、工具选择与并行调用原样进入上游请求体。
- 瞬时失败由适配器就地重放并回报内部重试次数,保留上游 `Retry-After` 而非通用退避,并对没有终态帧的流立即结束。
9 changes: 9 additions & 0 deletions changelog/unreleased/request-attempt-state.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### English

- Record per-attempt request state (`kind`, `state`, whether the request went upstream, and the error code) so a failed round explains where it stopped.
- Compile the aggregation pipeline with the classifier in one place, so a logical-model dispatch keeps its own target list and grant scope.

### 中文

- 每次尝试记录请求状态(类型、状态、是否已发往上游、错误码),失败的一轮能说明停在哪一步。
- 聚合管线与分类器在同一处编译,逻辑模型分发的目标列表与授权范围保持一致。
27 changes: 27 additions & 0 deletions changelog/unreleased/runtime-route-toolcall-delivery.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
### English

- Route through a single dispatch plan: reserve the account and update rotation together, keep the provider and region allowed by the request and API key, and skip an incapable member before any upstream attempt.
- Use the selected account and concrete model for cooldowns and stream feedback, including native Responses, and check API-key concurrency before an aggregation classifier can send a request.
- Deliver tool calls from the canonical contract: reject malformed, truncated, or undeclared calls before a success frame or buffered message is written, and keep one call identity from the upstream event through the final frame.
- Record the upstream terminal state and the downstream terminal write separately, propagate flush failures, and classify connect, write, read and first-frame timeouts by the transport stage that actually failed.
- Return only generic or redacted text on public HTTP and SSE errors, sanitize native failure frames, and preserve the upstream `Retry-After` through CORS.
- Add a gateway quality model that separates TTFT, head wait, full and decode throughput, TPOT, and a delivery-completeness verdict, and extend request statistics with TTFT percentiles, an incompletion rate, and average throughput.
- Bound and reclaim the per-request attempt buffer, assign a stable attempt sequence, and log entry rejections with the request ID.
- Priority aggregations rotate among the best-tier targets in member order, `max_attempts` caps dispatches of the requested model across tiers, and previews stay in sync with configured members.
- Keep the public model list consistent with API-key grants and routing, including namespaced models and group names that shadow account aliases, and stop a canceled stream from cooling a healthy account.
- Run bulk account operations fully concurrent, push real runtime state to the console, and show provider, model and cooldown detail for a failed quota refresh.
- Remove the superseded tool-argument helpers in `internal/translate` now that `internal/toolcall` owns parsing, repair and validation.

### 中文

- 路由收敛到单一调度计划:账号预留与轮转一起更新,只使用请求与密钥允许的供应商和地区,能力不满足的成员在上游请求之前就被跳过。
- 冷却与流反馈按最终选中的账号和具体模型记录(含原生 Responses),API 密钥并发在聚合分类器发请求之前就校验。
- 工具调用按规范契约交付:损坏、截断或未声明的调用在成功帧或缓冲消息写出之前就拒绝,同一次调用从上游事件到最终帧保持同一身份。
- 上游终态与下游终态写出分开记录,冲刷失败向上传播,连接、写、读、首帧超时按真实失败的传输阶段归类。
- 公开 HTTP 与 SSE 错误只返回通用或脱敏文案,原生失败帧同样脱敏,上游 `Retry-After` 经 CORS 透出。
- 新增网关质量模型,把首字节、等待响应头、整体与解码吞吐、TPOT 和交付完整性分开衡量;请求统计补充首字节分位、未完成率与平均吞吐。
- 每次请求的尝试缓冲加上限并回收,尝试序号稳定,入口拒绝会带上请求 ID。
- 优先级聚合在最优档位内按成员顺序轮转,`max_attempts` 限制的是所请求模型跨档位的派发次数,预览与配置成员保持一致。
- 公开模型列表与密钥授权、路由保持一致,包含带命名空间的模型和遮蔽账号别名的组名;被取消的流不再冷却健康账号。
- 批量账号操作完全并发执行,控制台收到真实运行状态,配额刷新失败时会指出供应商、模型与冷却情况。
- `internal/toolcall` 接管解析、修复与校验之后,删除 `internal/translate` 中已被取代的工具参数辅助函数。
10 changes: 10 additions & 0 deletions changelog/unreleased/toolcall-contract-package.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
### English

- Add `internal/toolcall`: one canonical model of tool declarations, tool choice, argument repair, native call identity and rendering, so protocols stop each re-implementing the contract.
- Validate `tools`, `tool_choice` and `parallel_tool_calls` through that parser in the shared chat-request validation every protocol entry runs, so an orphan `auto` degrades to unspecified while `required` or a named choice fails instead of silently dropping the tool request.

### 中文

- 新增 `internal/toolcall`:工具声明、工具选择、参数修复、原生调用身份与渲染只有一份规范模型,协议层不再各自实现这套契约。
- Responses 入口与各协议的共用请求校验都通过该解析器校验 `tools`、`tool_choice`、`parallel_tool_calls`:孤立的 `auto` 降级为未指定,`required` 或指定工具则校验失败,不再静默丢弃工具请求。

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