mirror of
https://github.com/openai/codex.git
synced 2026-11-20 22:40:37 +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:
@@ -3,6 +3,7 @@ use codex_protocol::ResponseUsageMetadata;
|
||||
use codex_protocol::config_types::ReasoningSummary as ReasoningSummaryConfig;
|
||||
use codex_protocol::config_types::Verbosity as VerbosityConfig;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use codex_protocol::models::ToolResultBody;
|
||||
use codex_protocol::openai_models::ReasoningEffort as ReasoningEffortConfig;
|
||||
use codex_protocol::protocol::ModelVerification;
|
||||
use codex_protocol::protocol::RateLimitSnapshot;
|
||||
@@ -28,6 +29,74 @@ use tokio::sync::oneshot;
|
||||
pub const WS_REQUEST_HEADER_TRACEPARENT_CLIENT_METADATA_KEY: &str = "ws_request_header_traceparent";
|
||||
pub const WS_REQUEST_HEADER_TRACESTATE_CLIENT_METADATA_KEY: &str = "ws_request_header_tracestate";
|
||||
|
||||
/// Maximum outgoing Responses message size for attaching optional tool observations.
|
||||
pub const MAX_RESPONSE_MESSAGE_BYTES: usize = 15 * 1024 * 1024;
|
||||
|
||||
/// Adds authorized connector results to the selected rows without re-encoding the ordinary request.
|
||||
/// Borrowed raw JSON leaves routing field order, tool schemas, and unrelated inputs intact.
|
||||
pub(crate) fn with_tool_results(
|
||||
json: &str,
|
||||
results: &[(usize, usize, Arc<ToolResultBody>)],
|
||||
) -> Option<String> {
|
||||
#[derive(Deserialize)]
|
||||
struct Input<'a> {
|
||||
#[serde(borrow)]
|
||||
input: Vec<Item<'a>>,
|
||||
}
|
||||
#[derive(Deserialize)]
|
||||
struct Item<'a> {
|
||||
#[serde(borrow)]
|
||||
internal_chat_message_metadata_passthrough: Option<Metadata<'a>>,
|
||||
}
|
||||
#[derive(Deserialize)]
|
||||
struct Metadata<'a> {
|
||||
#[serde(borrow)]
|
||||
executed_tool_calls: Option<Vec<&'a RawValue>>,
|
||||
}
|
||||
|
||||
let mut remaining = MAX_RESPONSE_MESSAGE_BYTES.checked_sub(json.len())?;
|
||||
if results.is_empty() || remaining == 0 {
|
||||
return None;
|
||||
}
|
||||
let input: Input<'_> = serde_json::from_str(json).ok()?;
|
||||
let mut enriched = String::new();
|
||||
let mut cursor = 0;
|
||||
for (item, call, body) in results {
|
||||
let row = input
|
||||
.input
|
||||
.get(*item)?
|
||||
.internal_chat_message_metadata_passthrough
|
||||
.as_ref()?
|
||||
.executed_tool_calls
|
||||
.as_ref()?
|
||||
.get(*call)?
|
||||
.get();
|
||||
// Native ExecutedToolCall rows always contain name and arguments.
|
||||
let row_fields = row.strip_prefix('{')?.strip_suffix('}')?;
|
||||
if row_fields.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let close = row.as_ptr() as usize - json.as_ptr() as usize + row.len() - 1;
|
||||
if close <= cursor {
|
||||
return None;
|
||||
}
|
||||
const FIELD: &str = r#","tool_result":"#;
|
||||
let Some(next_remaining) = remaining.checked_sub(FIELD.len() + body.compact_bytes()) else {
|
||||
continue;
|
||||
};
|
||||
enriched.push_str(&json[cursor..close]);
|
||||
enriched.push_str(FIELD);
|
||||
enriched.push_str(body.as_json());
|
||||
cursor = close;
|
||||
remaining = next_remaining;
|
||||
}
|
||||
if enriched.is_empty() {
|
||||
return None;
|
||||
}
|
||||
enriched.push_str(&json[cursor..]);
|
||||
Some(enriched)
|
||||
}
|
||||
|
||||
/// Explicit per-request access selection using the Responses API wire values.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
|
||||
pub struct AccessPrograms {
|
||||
@@ -359,6 +428,7 @@ impl<'a> From<&'a ResponsesApiRequest> for ResponseCreateWsRequest<'a> {
|
||||
model: &request.model,
|
||||
previous_response_id: None,
|
||||
input: &request.input,
|
||||
tool_results: Vec::new(),
|
||||
tools: request.tools.as_ref().map(ResponsesApiTools::as_raw_value),
|
||||
tool_choice: &request.tool_choice,
|
||||
parallel_tool_calls: request.parallel_tool_calls,
|
||||
@@ -390,6 +460,9 @@ pub struct ResponseCreateWsRequest<'a> {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub previous_response_id: Option<String>,
|
||||
pub input: &'a [ResponseItem],
|
||||
/// Observations for this wire frame only. Canonical request serialization omits them.
|
||||
#[serde(skip)]
|
||||
pub tool_results: Vec<(usize, usize, Arc<ToolResultBody>)>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub tools: Option<&'a RawValue>,
|
||||
pub tool_choice: &'a str,
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -35,6 +35,7 @@ pub use crate::auth::AuthRefreshFuture;
|
||||
pub use crate::auth::SharedAuthProvider;
|
||||
pub use crate::auth::auth_header_telemetry;
|
||||
pub use crate::common::AccessPrograms;
|
||||
pub use crate::common::MAX_RESPONSE_MESSAGE_BYTES;
|
||||
pub use crate::common::MemorySummarizeInput;
|
||||
pub use crate::common::MemorySummarizeOutput;
|
||||
pub use crate::common::OpenAiVerbosity;
|
||||
|
||||
Reference in New Issue
Block a user