mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
reuse encoded Responses request bodies (#28327)
## Why Responses HTTP requests were converted from `ResponsesApiRequest` into a full `serde_json::Value`. `EndpointSession` then deep-cloned that value for each retry, and the transport serialized and compressed it again before every send. Large histories make those copies expensive. Retry attempts should reuse the same immutable request bytes. ## What - Serialize standard Responses requests directly into a ref-counted `EncodedJsonBody`. - Preserve the Azure path that attaches item IDs before encoding. - Prepare JSON, compression, and derived content headers once before the retry loop. - Clone the prepared request per attempt so body clones only bump the `Bytes` reference count. - Keep auth inside the retry loop. Signing auth sees the exact final headers and body bytes that the transport sends. - Preserve request-body TRACE output. With TRACE plus compression, retain the original JSON bytes for logging; normal requests keep only the final wire bytes. - Leave non-Responses endpoint bodies on the existing `Value` path. ## Performance A temporary release-mode measurement used a 10 MiB JSON body and 10 retry preparations: - old `Value` clone + serialize path: 30 ms total - prepared shared-byte path: less than 1 ms total That is about 3 ms avoided per retry for this payload on the test machine. Each retry also stops allocating another request-sized JSON tree and serialized buffer. Without TRACE, compressed requests retain only the final compressed wire bytes. ## Validation - `just test -p codex-client` — 28 passed - `just test -p codex-api` — 125 passed - `just fix -p codex-client` - `just fix -p codex-api`
This commit is contained in:
@@ -11,6 +11,7 @@ use crate::requests::headers::insert_header;
|
||||
use crate::requests::headers::subagent_header;
|
||||
use crate::sse::spawn_response_stream;
|
||||
use crate::telemetry::SseTelemetry;
|
||||
use codex_client::EncodedJsonBody;
|
||||
use codex_client::HttpTransport;
|
||||
use codex_client::RequestCompression;
|
||||
use codex_client::RequestTelemetry;
|
||||
@@ -81,11 +82,16 @@ impl<T: HttpTransport> ResponsesClient<T> {
|
||||
turn_state,
|
||||
} = options;
|
||||
|
||||
let mut body = serde_json::to_value(&request)
|
||||
.map_err(|e| ApiError::Stream(format!("failed to encode responses request: {e}")))?;
|
||||
if request.store && self.session.provider().is_azure_responses_endpoint() {
|
||||
let body = if request.store && self.session.provider().is_azure_responses_endpoint() {
|
||||
let mut body = serde_json::to_value(&request).map_err(|e| {
|
||||
ApiError::Stream(format!("failed to encode responses request: {e}"))
|
||||
})?;
|
||||
attach_item_ids(&mut body, &request.input);
|
||||
EncodedJsonBody::encode(&body)
|
||||
} else {
|
||||
EncodedJsonBody::encode(&request)
|
||||
}
|
||||
.map_err(|e| ApiError::Stream(format!("failed to encode responses request: {e}")))?;
|
||||
|
||||
let mut headers = extra_headers;
|
||||
if let Some(ref thread_id) = thread_id {
|
||||
@@ -96,7 +102,8 @@ impl<T: HttpTransport> ResponsesClient<T> {
|
||||
insert_header(&mut headers, "x-openai-subagent", &subagent);
|
||||
}
|
||||
|
||||
self.stream(body, headers, compression, turn_state).await
|
||||
self.stream_encoded(body, headers, compression, turn_state)
|
||||
.await
|
||||
}
|
||||
|
||||
fn path() -> &'static str {
|
||||
@@ -120,6 +127,19 @@ impl<T: HttpTransport> ResponsesClient<T> {
|
||||
extra_headers: HeaderMap,
|
||||
compression: Compression,
|
||||
turn_state: Option<Arc<OnceLock<String>>>,
|
||||
) -> Result<ResponseStream, ApiError> {
|
||||
let body = EncodedJsonBody::encode(&body)
|
||||
.map_err(|e| ApiError::Stream(format!("failed to encode responses request: {e}")))?;
|
||||
self.stream_encoded(body, extra_headers, compression, turn_state)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn stream_encoded(
|
||||
&self,
|
||||
body: EncodedJsonBody,
|
||||
extra_headers: HeaderMap,
|
||||
compression: Compression,
|
||||
turn_state: Option<Arc<OnceLock<String>>>,
|
||||
) -> Result<ResponseStream, ApiError> {
|
||||
let request_compression = match compression {
|
||||
Compression::None => RequestCompression::None,
|
||||
@@ -128,7 +148,7 @@ impl<T: HttpTransport> ResponsesClient<T> {
|
||||
|
||||
let stream_response = self
|
||||
.session
|
||||
.stream_with(
|
||||
.stream_encoded_json_with(
|
||||
Method::POST,
|
||||
Self::path(),
|
||||
extra_headers,
|
||||
|
||||
@@ -2,6 +2,7 @@ use crate::auth::SharedAuthProvider;
|
||||
use crate::error::ApiError;
|
||||
use crate::provider::Provider;
|
||||
use crate::telemetry::run_with_request_telemetry;
|
||||
use codex_client::EncodedJsonBody;
|
||||
use codex_client::HttpTransport;
|
||||
use codex_client::Request;
|
||||
use codex_client::RequestBody;
|
||||
@@ -49,12 +50,12 @@ impl<T: HttpTransport> EndpointSession<T> {
|
||||
method: &Method,
|
||||
path: &str,
|
||||
extra_headers: &HeaderMap,
|
||||
body: Option<&Value>,
|
||||
body: Option<&RequestBody>,
|
||||
) -> Request {
|
||||
let mut req = self.provider.build_request(method.clone(), path);
|
||||
req.headers.extend(extra_headers.clone());
|
||||
if let Some(body) = body {
|
||||
req.body = Some(RequestBody::Json(body.clone()));
|
||||
req.body = Some(body.clone());
|
||||
}
|
||||
req
|
||||
}
|
||||
@@ -87,6 +88,7 @@ impl<T: HttpTransport> EndpointSession<T> {
|
||||
where
|
||||
C: Fn(&mut Request),
|
||||
{
|
||||
let body = body.map(RequestBody::Json);
|
||||
let make_request = || {
|
||||
let mut req = self.make_request(&method, path, &extra_headers, body.as_ref());
|
||||
configure(&mut req);
|
||||
@@ -112,27 +114,27 @@ impl<T: HttpTransport> EndpointSession<T> {
|
||||
}
|
||||
|
||||
#[instrument(
|
||||
name = "endpoint_session.stream_with",
|
||||
name = "endpoint_session.stream_encoded_json_with",
|
||||
level = "info",
|
||||
skip_all,
|
||||
fields(http.method = %method, api.path = path)
|
||||
)]
|
||||
pub(crate) async fn stream_with<C>(
|
||||
pub(crate) async fn stream_encoded_json_with<C>(
|
||||
&self,
|
||||
method: Method,
|
||||
path: &str,
|
||||
extra_headers: HeaderMap,
|
||||
body: Option<Value>,
|
||||
body: Option<EncodedJsonBody>,
|
||||
configure: C,
|
||||
) -> Result<StreamResponse, ApiError>
|
||||
where
|
||||
C: Fn(&mut Request),
|
||||
{
|
||||
let make_request = || {
|
||||
let mut req = self.make_request(&method, path, &extra_headers, body.as_ref());
|
||||
configure(&mut req);
|
||||
req
|
||||
};
|
||||
let body = body.map(RequestBody::EncodedJson);
|
||||
let mut request = self.make_request(&method, path, &extra_headers, body.as_ref());
|
||||
configure(&mut request);
|
||||
let request = request.into_prepared().map_err(TransportError::Build)?;
|
||||
let make_request = || request.clone();
|
||||
|
||||
let stream = run_with_request_telemetry(
|
||||
self.provider.retry.to_policy(),
|
||||
|
||||
Reference in New Issue
Block a user