Skip to content
目录

07 — ModelClient、WebSocket 预连接与重试

职责划分(crate 内文档)

codex-rs/core/src/client.rs 文件头说明:

  • ModelClient:会话生命周期内稳定存在,承载鉴权、provider 选择、conversation id、传输降级状态等。
  • ModelClientSession每 Turn 一个,用于该回合内的一次或多次 Responses 请求;懒建立 WebSocket,并保存 x-codex-turn-state粘性路由 状态。
  • WebSocket prewarm:v2 的 response.creategenerate=false,等待完成以便下一请求复用连接与 previous_response_id
1:24:codex-rs/core/src/client.rs
//! Session- and turn-scoped helpers for talking to model provider APIs.
//!
//! `ModelClient` is intended to live for the lifetime of a Codex session and holds the stable
//! configuration and state needed to talk to a provider (auth, provider selection, conversation id,
//! and transport fallback state).
//!
//! Per-turn settings (model selection, reasoning controls, telemetry context, and turn metadata)
//! are passed explicitly to streaming and unary methods so that the turn lifetime is visible at the
//! call site.
//!
//! A [`ModelClientSession`] is created per turn and is used to stream one or more Responses API
//! requests during that turn. It caches a Responses WebSocket connection (opened lazily) and stores
//! per-turn state such as the `x-codex-turn-state` token used for sticky routing.
//!
//! WebSocket prewarm is a v2-only `response.create` with `generate=false`; it waits for completion
//! so the next request can reuse the same connection and `previous_response_id`.
//!
//! Turn execution performs prewarm as a best-effort step before the first stream request so the
//! subsequent request can reuse the same connection.
//!
//! ## Retry-Budget Tradeoff
//!
//! WebSocket prewarm is treated as the first websocket connection attempt for a turn. If it
//! fails, normal stream retry/fallback logic handles recovery on the same turn.

设计原因(精简版)

  1. Turn 级 session:推理力度、verbosity、service tier 等每轮可变,若塞进全局 client 易导致 隐式状态泄漏 到下一回合。
  2. Prewarm:首包延迟(特别是 TLS + WS 握手)占体感很大比例;先 generate=false 买连接,再流式生成,是典型的 latency hiding
  3. Prewarm 失败计入重试预算:避免无限次「空握手」;失败则交给统一重试/降级,保持 可预测的资源使用

与 OpenTelemetry / 合规

同文件引用 SessionTelemetryW3cTraceContext 等,说明上游请求与 分布式追踪、企业审计 对齐(与 app-server README 中 clientInfo.name 用于合规日志一致)。

延伸阅读

MIT License.