mirror of
https://github.com/openai/codex.git
synced 2026-11-19 22:30:42 +00:00
Support optional Apps tool results in Responses requests (#62388)
## What changed Allow callers to attach eligible Apps tool results as `tool_result` on selected executed tool calls in Responses HTTP and WebSocket requests. Keep text content and optional object-valued `structuredContent`, with a 1 MiB per-result limit and a 15 MiB budget for the enriched message. Attach results after request observers receive the original JSON. Keep result IDs local and suppress WebSocket payload tracing for enriched frames. Preserve the enriched HTTP body on retries to the original endpoint and retry with the original JSON on HTTP 307 and 308 redirects. Codex's default request construction supplies no result bodies. ## Testing Add coverage for result eligibility and size limits, HTTP retry and redirect behavior, and TLS fallback without forwarding the enriched body to a redirect destination. GitOrigin-RevId: c3f2f86f31349e40fcb699d48637c3cbbbe38837
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use crate::auth::SharedAuthProvider;
|
||||
use crate::common::ResponseStream;
|
||||
use crate::common::ResponsesApiRequest;
|
||||
use crate::common::with_tool_results;
|
||||
use crate::endpoint::session::EndpointSession;
|
||||
use crate::error::ApiError;
|
||||
use crate::provider::Provider;
|
||||
@@ -14,10 +15,13 @@ use codex_client::EncodedJsonBody;
|
||||
use codex_client::HttpTransport;
|
||||
use codex_client::RequestCompression;
|
||||
use codex_client::RequestTelemetry;
|
||||
use codex_client::TransportError;
|
||||
use codex_protocol::models::ToolResultBody;
|
||||
use codex_protocol::protocol::SessionSource;
|
||||
use http::HeaderMap;
|
||||
use http::HeaderValue;
|
||||
use http::Method;
|
||||
use http::StatusCode;
|
||||
use serde_json::Value;
|
||||
use std::sync::Arc;
|
||||
use std::sync::OnceLock;
|
||||
@@ -35,6 +39,7 @@ pub struct ResponsesOptions {
|
||||
pub session_source: Option<SessionSource>,
|
||||
pub extra_headers: HeaderMap,
|
||||
pub compression: Compression,
|
||||
pub tool_results: Vec<(usize, usize, Arc<ToolResultBody>)>,
|
||||
pub turn_state: Option<Arc<OnceLock<String>>>,
|
||||
}
|
||||
|
||||
@@ -66,7 +71,7 @@ impl<T: HttpTransport> ResponsesClient<T> {
|
||||
.await
|
||||
}
|
||||
|
||||
/// Gives the observer a shared copy of the exact JSON before transport dispatch.
|
||||
/// Observes the encoded request before private connector results are attached.
|
||||
#[instrument(
|
||||
name = "responses.stream_request",
|
||||
level = "info",
|
||||
@@ -89,6 +94,7 @@ impl<T: HttpTransport> ResponsesClient<T> {
|
||||
session_source,
|
||||
extra_headers,
|
||||
compression,
|
||||
tool_results,
|
||||
turn_state,
|
||||
} = options;
|
||||
let body = EncodedJsonBody::encode(&request)
|
||||
@@ -104,6 +110,30 @@ impl<T: HttpTransport> ResponsesClient<T> {
|
||||
insert_header(&mut headers, "x-openai-subagent", &subagent);
|
||||
}
|
||||
|
||||
if !tool_results.is_empty()
|
||||
&& let Ok(json) = std::str::from_utf8(body.as_bytes())
|
||||
&& let Some(json) = with_tool_results(json, &tool_results)
|
||||
{
|
||||
let stream = self
|
||||
.stream_encoded(
|
||||
body.with_private_json(json),
|
||||
headers.clone(),
|
||||
compression,
|
||||
turn_state.clone(),
|
||||
)
|
||||
.await;
|
||||
// A redirect that preserves the request body must start over without private results.
|
||||
if !matches!(
|
||||
stream,
|
||||
Err(ApiError::Transport(TransportError::Http {
|
||||
status: StatusCode::TEMPORARY_REDIRECT | StatusCode::PERMANENT_REDIRECT,
|
||||
headers: Some(ref headers),
|
||||
..
|
||||
})) if headers.get(http::header::LOCATION).is_some_and(|location| !location.is_empty())
|
||||
) {
|
||||
return stream;
|
||||
}
|
||||
}
|
||||
self.stream_encoded(body, headers, compression, turn_state)
|
||||
.await
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ use crate::common::ResponseStream;
|
||||
use crate::common::ResponsesWsRequest;
|
||||
use crate::common::SafetyBufferingTreatment;
|
||||
use crate::common::WS_REQUEST_HEADER_TRACEPARENT_CLIENT_METADATA_KEY;
|
||||
use crate::common::with_tool_results;
|
||||
use crate::endpoint::responses_websocket_probe::ResponsesWebsocketClose;
|
||||
use crate::endpoint::responses_websocket_probe::ResponsesWebsocketProbe;
|
||||
use crate::endpoint::responses_websocket_probe::ResponsesWebsocketProbeError;
|
||||
@@ -56,6 +57,7 @@ use tracing::debug;
|
||||
use tracing::error;
|
||||
use tracing::info;
|
||||
use tracing::instrument;
|
||||
use tracing::instrument::WithSubscriber;
|
||||
use tungstenite::extensions::ExtensionsConfig;
|
||||
use tungstenite::extensions::compression::deflate::DeflateConfig;
|
||||
use tungstenite::protocol::WebSocketConfig;
|
||||
@@ -69,9 +71,14 @@ struct WsStream {
|
||||
pump_task: tokio::task::JoinHandle<()>,
|
||||
}
|
||||
|
||||
struct WsRequestText {
|
||||
text: Utf8Bytes,
|
||||
private: bool,
|
||||
}
|
||||
|
||||
enum WsCommand {
|
||||
Send {
|
||||
message: Message,
|
||||
request: WsRequestText,
|
||||
tx_result: oneshot::Sender<Result<(), WsError>>,
|
||||
},
|
||||
}
|
||||
@@ -121,8 +128,14 @@ impl WsStream {
|
||||
break;
|
||||
};
|
||||
match command {
|
||||
WsCommand::Send { message, tx_result } => {
|
||||
let result = send_message(&mut inner, message).await;
|
||||
WsCommand::Send { request, tx_result } => {
|
||||
let send = send_message(&mut inner, Message::Text(request.text));
|
||||
// Tungstenite traces frame payloads from inside this future.
|
||||
let result = if request.private {
|
||||
send.with_subscriber(tracing::Dispatch::none()).await
|
||||
} else {
|
||||
send.await
|
||||
};
|
||||
let should_break = result.is_err();
|
||||
let _ = tx_result.send(result);
|
||||
if should_break {
|
||||
@@ -190,8 +203,8 @@ impl WsStream {
|
||||
Err(WsError::ConnectionClosed)
|
||||
}
|
||||
|
||||
async fn send(&mut self, message: Message) -> Result<(), WsError> {
|
||||
self.request(|tx_result| WsCommand::Send { message, tx_result })
|
||||
async fn send(&mut self, request: WsRequestText) -> Result<(), WsError> {
|
||||
self.request(|tx_result| WsCommand::Send { request, tx_result })
|
||||
.await
|
||||
}
|
||||
|
||||
@@ -294,7 +307,7 @@ impl ResponsesWebsocketConnection {
|
||||
.await
|
||||
}
|
||||
|
||||
/// Gives the observer a shared copy of the exact JSON before transport dispatch.
|
||||
/// Gives the observer the serialized request before private tool results are attached.
|
||||
#[instrument(
|
||||
name = "responses_websocket.stream_request",
|
||||
level = "info",
|
||||
@@ -343,6 +356,11 @@ impl ResponsesWebsocketConnection {
|
||||
};
|
||||
let request_text = Utf8Bytes::from(serialize_websocket_request(&request)?);
|
||||
on_encoded(request_text.as_ref());
|
||||
let enriched = with_tool_results(request_text.as_str(), &ws_request.tool_results);
|
||||
let request_text = WsRequestText {
|
||||
private: enriched.is_some(),
|
||||
text: enriched.map(Utf8Bytes::from).unwrap_or(request_text),
|
||||
};
|
||||
|
||||
let current_span = Span::current();
|
||||
tokio::spawn(
|
||||
@@ -841,7 +859,7 @@ fn is_websocket_receive_failure(
|
||||
async fn run_websocket_response_stream(
|
||||
ws_stream: &mut WsStream,
|
||||
tx_event: mpsc::Sender<std::result::Result<ResponseEvent, ApiError>>,
|
||||
request_text: Utf8Bytes,
|
||||
request_text: WsRequestText,
|
||||
idle_timeout: Duration,
|
||||
telemetry: Option<Arc<dyn WebsocketTelemetry>>,
|
||||
turn_state: Option<&OnceLock<String>>,
|
||||
@@ -883,11 +901,14 @@ async fn run_websocket_response_stream(
|
||||
interruption = Some(request);
|
||||
send_websocket_request(
|
||||
ws_stream,
|
||||
serde_json::json!({
|
||||
"type": "response.interrupt",
|
||||
"response_id": response_id,
|
||||
"mode": "discard_partial_items",
|
||||
}).to_string().into(),
|
||||
WsRequestText {
|
||||
text: serde_json::json!({
|
||||
"type": "response.interrupt",
|
||||
"response_id": response_id,
|
||||
"mode": "discard_partial_items",
|
||||
}).to_string().into(),
|
||||
private: false,
|
||||
},
|
||||
idle_timeout,
|
||||
/*telemetry*/ None,
|
||||
timing_log_context.connection_reused,
|
||||
@@ -1118,19 +1139,19 @@ fn safety_buffering_for_event(
|
||||
|
||||
async fn send_websocket_request(
|
||||
ws_stream: &mut WsStream,
|
||||
request_text: Utf8Bytes,
|
||||
request_text: WsRequestText,
|
||||
idle_timeout: Duration,
|
||||
telemetry: Option<&Arc<dyn WebsocketTelemetry>>,
|
||||
connection_reused: bool,
|
||||
) -> Result<(), ApiError> {
|
||||
if ws_stream
|
||||
.max_message_bytes
|
||||
.is_some_and(|limit| request_text.len() > limit)
|
||||
.is_some_and(|limit| request_text.text.len() > limit)
|
||||
{
|
||||
return Err(ApiError::WebsocketMessageTooLarge);
|
||||
}
|
||||
let request_start = Instant::now();
|
||||
let result = tokio::time::timeout(idle_timeout, ws_stream.send(Message::Text(request_text)))
|
||||
let result = tokio::time::timeout(idle_timeout, ws_stream.send(request_text))
|
||||
.await
|
||||
.map_err(|_| ApiError::Stream("idle timeout sending websocket request".into()))
|
||||
.and_then(|result| result.map_err(map_ws_stream_error));
|
||||
|
||||
Reference in New Issue
Block a user