Support structured dynamic tool results in Code Mode (#63766)

## Why

Dynamic tool clients could return JSON as text in `contentItems`, so Code Mode scripts had to parse that text to access fields in the result.

## What changed

App server dynamic tool requests advertise `supportedResultFormat: "structuredContent"`. Clients can reply with `structuredContent` and a `success` flag, report a failure with `errorMessage` and `success: false`, or continue using `contentItems`.

Code Mode exposes structured replies as MCP-style tool results, with the original JSON under `structuredContent` and failure status in `isError`. Completed tool items include a text representation of the reply. Matching post-tool-use hooks can receive the structured result for successful calls.

## Testing

Add protocol tests for the wire formats and rejection of ambiguous replies, plus an app server integration test that exercises structured results, content items, and errors through Code Mode.

GitOrigin-RevId: d63152633b43bd5bd236f6e0dcb41ee7e98e7947
This commit is contained in:
Brandon Zhang
2026-11-24 23:38:37 +00:00
committed by copyberry
parent 0adee0df26
commit ff146c344d
37 changed files with 1104 additions and 169 deletions
@@ -1135,6 +1135,9 @@ pub(crate) async fn apply_bespoke_event_handling(
};
let dynamic_tool_call_params = match &event.item {
CoreTurnItem::DynamicToolCall(item) => Some(DynamicToolCallParams {
supported_result_format: Some(
codex_app_server_protocol::DynamicToolResultFormat::StructuredContent,
),
thread_id: conversation_id.to_string(),
turn_id: event.turn_id.clone(),
call_id: item.id.clone(),
@@ -3500,6 +3503,9 @@ mod tests {
assert_eq!(
params,
DynamicToolCallParams {
supported_result_format: Some(
codex_app_server_protocol::DynamicToolResultFormat::StructuredContent
),
thread_id: conversation_id.to_string(),
turn_id: "turn-1".to_string(),
call_id: "dynamic-1".to_string(),
+30 -33
View File
@@ -1,8 +1,7 @@
use codex_app_server_protocol::DynamicToolCallOutputContentItem;
use codex_app_server_protocol::DynamicToolCallResponse;
use codex_app_server_protocol::DynamicToolCallResult;
use codex_core::CodexThread;
use codex_protocol::dynamic_tools::DynamicToolCallOutputContentItem as CoreDynamicToolCallOutputContentItem;
use codex_protocol::dynamic_tools::DynamicToolResponse as CoreDynamicToolResponse;
use codex_protocol::dynamic_tools::DynamicToolCallResult as CoreDynamicToolCallResult;
use codex_protocol::protocol::Op;
use std::sync::Arc;
use tokio::sync::oneshot;
@@ -21,7 +20,7 @@ pub(crate) async fn on_call_response(
conversation: Arc<CodexThread>,
) {
let response = receiver.await;
let (response, _error) = match response {
let response = match response {
Ok(Ok(value)) => decode_response(value),
Ok(Err(err)) if is_turn_transition_server_request_error(&err) => return,
Ok(Err(err)) => {
@@ -34,21 +33,10 @@ pub(crate) async fn on_call_response(
}
};
let DynamicToolCallResponse {
content_items,
success,
} = response.clone();
let core_response = CoreDynamicToolResponse {
content_items: content_items
.into_iter()
.map(CoreDynamicToolCallOutputContentItem::from)
.collect(),
success,
};
if let Err(err) = conversation
.submit(Op::DynamicToolResponse {
.submit(Op::DynamicToolResult {
id: call_id.clone(),
response: core_response,
response,
})
.await
{
@@ -56,9 +44,11 @@ pub(crate) async fn on_call_response(
}
}
fn decode_response(value: serde_json::Value) -> (DynamicToolCallResponse, Option<String>) {
match serde_json::from_value::<DynamicToolCallResponse>(value) {
Ok(response)
fn decode_response(value: serde_json::Value) -> CoreDynamicToolCallResult {
let structured =
value.get("structuredContent").is_some() || value.get("errorMessage").is_some();
match serde_json::from_value::<DynamicToolCallResult>(value) {
Ok(DynamicToolCallResult::ContentItems(response))
if response.content_items.iter().any(|item| {
matches!(
item,
@@ -73,7 +63,7 @@ fn decode_response(value: serde_json::Value) -> (DynamicToolCallResponse, Option
);
fallback_response(REMOTE_IMAGE_URL_ERROR)
}
Ok(response)
Ok(DynamicToolCallResult::ContentItems(response))
if response.content_items.iter().any(|item| {
matches!(
item,
@@ -90,22 +80,29 @@ fn decode_response(value: serde_json::Value) -> (DynamicToolCallResponse, Option
);
fallback_response(INVALID_AUDIO_URL_ERROR)
}
Ok(response) => (response, None),
Ok(response) => response.into(),
Err(err) => {
error!("failed to deserialize DynamicToolCallResponse: {err}");
fallback_response("dynamic tool response was invalid")
error!("failed to deserialize DynamicToolCallResult: {err}");
let message = "dynamic tool response was invalid";
if structured {
CoreDynamicToolCallResult::Error {
error_message: message.to_owned(),
}
} else {
fallback_response(message)
}
}
}
}
fn fallback_response(message: &str) -> (DynamicToolCallResponse, Option<String>) {
(
DynamicToolCallResponse {
content_items: vec![DynamicToolCallOutputContentItem::InputText {
fn fallback_response(message: &str) -> CoreDynamicToolCallResult {
// Advertising support does not mean the client selected structured results.
CoreDynamicToolCallResult::ContentItems(codex_protocol::dynamic_tools::DynamicToolResponse {
content_items: vec![
codex_protocol::dynamic_tools::DynamicToolCallOutputContentItem::InputText {
text: message.to_string(),
}],
success: false,
},
Some(message.to_string()),
)
},
],
success: false,
})
}
@@ -1910,6 +1910,9 @@ mod tests {
let (dynamic_tool_request_id, _dynamic_tool_waiter) = thread_outgoing
.send_request(ServerRequestPayload::DynamicToolCall(
DynamicToolCallParams {
supported_result_format: Some(
codex_app_server_protocol::DynamicToolResultFormat::StructuredContent,
),
thread_id: thread_id.to_string(),
turn_id: "turn-1".to_string(),
call_id: "call-0".to_string(),
@@ -1974,6 +1977,9 @@ mod tests {
let (_dynamic_tool_request_id, dynamic_tool_waiter) = thread_outgoing
.send_request(ServerRequestPayload::DynamicToolCall(
DynamicToolCallParams {
supported_result_format: Some(
codex_app_server_protocol::DynamicToolResultFormat::StructuredContent,
),
thread_id: thread_id.to_string(),
turn_id: "turn-1".to_string(),
call_id: "call-0".to_string(),
@@ -60,6 +60,9 @@ async fn stalled_replay_can_be_replaced_or_unsubscribed() -> Result<()> {
.send_request_to_connections(
Some(&[]),
ServerRequestPayload::DynamicToolCall(DynamicToolCallParams {
supported_result_format: Some(
codex_app_server_protocol::DynamicToolResultFormat::StructuredContent,
),
thread_id: thread_id.to_string(),
turn_id: "turn".into(),
call_id: index.to_string(),
@@ -16,16 +16,19 @@ use codex_app_server_protocol::DynamicToolNamespaceTool;
use codex_app_server_protocol::DynamicToolSpec;
use codex_app_server_protocol::ItemCompletedNotification;
use codex_app_server_protocol::ItemStartedNotification;
use codex_app_server_protocol::JSONRPCErrorError;
use codex_app_server_protocol::JSONRPCNotification;
use codex_app_server_protocol::JSONRPCResponse;
use codex_app_server_protocol::RequestId;
use codex_app_server_protocol::ServerRequest;
use codex_app_server_protocol::ThreadItemPayload;
use codex_app_server_protocol::ThreadReadResponse;
use codex_app_server_protocol::ThreadStartParams;
use codex_app_server_protocol::ThreadStartResponse;
use codex_app_server_protocol::TurnStartParams;
use codex_app_server_protocol::TurnStartResponse;
use codex_app_server_protocol::UserInput as V2UserInput;
use codex_features::Feature;
use codex_protocol::models::DEFAULT_IMAGE_DETAIL;
use codex_protocol::models::FunctionCallOutputBody;
use codex_protocol::models::FunctionCallOutputContentItem;
@@ -482,6 +485,9 @@ async fn dynamic_tool_call_round_trip_sends_text_content_items_to_model() -> Res
};
let expected = DynamicToolCallParams {
supported_result_format: Some(
codex_app_server_protocol::DynamicToolResultFormat::StructuredContent,
),
thread_id: thread_id.clone(),
turn_id: turn_id.clone(),
call_id: call_id.to_string(),
@@ -672,6 +678,9 @@ async fn start_function_dynamic_tool_call(call_id: &str) -> Result<PendingDynami
};
let params = DynamicToolCallParams {
supported_result_format: Some(
codex_app_server_protocol::DynamicToolResultFormat::StructuredContent,
),
thread_id,
turn_id,
call_id: call_id.to_string(),
@@ -689,6 +698,207 @@ async fn start_function_dynamic_tool_call(call_id: &str) -> Result<PendingDynami
})
}
#[test_case::test_case(false; "dedup_disabled")]
#[test_case::test_case(true; "dedup_enabled")]
#[tokio::test]
async fn dynamic_tool_code_mode_round_trip_decodes_wire_results_and_errors(
dedup: bool,
) -> Result<()> {
let server = responses::start_mock_server().await;
let script = r#"
const keys = ["native", "content-items", "tool-error", "request-error", "malformed"];
const results = await Promise.all(keys.map(key => tools.lookup({ key })));
text(results[0]);
text(results[2]);
text(JSON.stringify(results));
"#;
responses::mount_sse_once(
&server,
responses::sse(vec![
responses::ev_response_created("resp-1"),
responses::ev_custom_tool_call("cell", "exec", script),
responses::ev_completed("resp-1"),
]),
)
.await;
let follow_up = responses::mount_sse_once(
&server,
create_final_assistant_message_sse_response("Done")?,
)
.await;
let home = TempDir::new()?;
let config = MockResponsesConfig::new(&server.uri()).enable_feature(Feature::CodeModeOnly);
if dedup {
config.enable_feature(Feature::CodeModeMcpResultDedup)
} else {
config.disable_feature(Feature::CodeModeMcpResultDedup)
}
.write(home.path())?;
let mut app = TestAppServer::builder()
.with_codex_home(home.path())
.build_initialized()
.await?;
let thread = app
.start_thread(ThreadStartParams {
dynamic_tools: Some(vec![DynamicToolSpec::Function(DynamicToolFunctionSpec {
name: "lookup".to_owned(),
description: "Look up a value.".to_owned(),
input_schema: json!({
"type": "object",
"properties": {"key": {"type": "string"}},
"required": ["key"],
"additionalProperties": false
}),
defer_loading: false,
})]),
..Default::default()
})
.await?
.thread;
let start_id = app
.send_turn_start_request(TurnStartParams {
thread_id: thread.id.clone(),
input: vec![V2UserInput::Text {
text: "Run the lookups.".to_owned(),
text_elements: Vec::new(),
}],
..Default::default()
})
.await?;
let _: TurnStartResponse = app.read_response(start_id).await?;
let structured = json!({"threads": [{"id":"🌿", "attributes":[{}, [], "\n\""]}]});
let content_items_text = r#"{"threads":[]}"#;
let mut native_call_id = None;
for _ in 0..5 {
let ServerRequest::DynamicToolCall { request_id, params } = timeout(
DEFAULT_READ_TIMEOUT,
app.read_stream_until_request_message(),
)
.await??
else {
anyhow::bail!("expected a dynamic tool request");
};
let result = match params.arguments["key"].as_str() {
Some("native") => {
native_call_id = Some(params.call_id);
json!({"success":true, "structuredContent": structured})
}
Some("content-items") => json!({
"success":true, "contentItems":[{"type": "inputText", "text": content_items_text}],
"_meta": {"client":"extension"}
}),
Some("tool-error") => json!({"success":false, "errorMessage": "History unavailable"}),
Some("request-error") => {
app.send_error(
request_id,
JSONRPCErrorError {
code: -32000,
message: "Client disconnected".to_owned(),
data: None,
},
)
.await?;
continue;
}
Some("malformed") => {
json!({"success":true, "contentItems":[], "structuredContent": {}})
}
key => anyhow::bail!("unexpected lookup key {key:?}"),
};
app.send_response(request_id, result).await?;
}
timeout(
DEFAULT_READ_TIMEOUT,
app.read_stream_until_notification_message("turn/completed"),
)
.await??;
let body = follow_up.single_request().body_json();
let output = body["input"]
.as_array()
.context("expected request input")?
.iter()
.find(|item| item["type"] == "custom_tool_call_output" && item["call_id"] == "cell")
.context("expected executed Code Mode cell")?;
let printed = output["output"]
.as_array()
.context("expected Code Mode output content")?
.iter()
.filter_map(|part| part["text"].as_str())
.filter_map(|text| serde_json::from_str::<Value>(text).ok())
.collect::<Vec<_>>();
let error = |message: &str| {
json!({
"content": [{"type": "text", "text": message}], "isError": true,
})
};
let native = json!({
"content": [{"type": "text", "text": structured.to_string()}],
"structuredContent": structured, "isError": false,
});
let printed_native = if dedup {
json!({"structuredContent": structured, "isError": false})
} else {
native.clone()
};
assert_eq!(
printed,
vec![
printed_native,
error("History unavailable"),
json!([
native,
content_items_text,
error("History unavailable"),
"dynamic tool request failed",
error("dynamic tool response was invalid"),
]),
],
);
let read_id = app
.send_request(
"thread/read",
Some(json!({
"threadId": thread.id, "includeTurns": true
})),
)
.await?;
let saved: ThreadReadResponse = app.read_response(read_id).await?;
let native_call_id = native_call_id.context("expected native tool request")?;
let saved_output = saved
.thread
.turns
.into_iter()
.flat_map(|turn| turn.items)
.find_map(|item| {
if let ThreadItemPayload::DynamicToolCall {
id,
success,
content_items,
..
} = item.payload
&& id == native_call_id
{
return Some((success, content_items));
}
None
})
.context("expected saved dynamic tool call")?;
assert_eq!(
saved_output,
(
Some(true),
Some(vec![DynamicToolCallOutputContentItem::InputText {
text: structured.to_string()
}])
)
);
Ok(())
}
/// Ensures dynamic tool call responses can include structured content items.
#[tokio::test]
async fn dynamic_tool_call_round_trip_handles_content_items() -> Result<()> {
@@ -347,6 +347,9 @@ async fn thread_unsubscribe_during_turn_keeps_turn_running(
assert_eq!(
params,
DynamicToolCallParams {
supported_result_format: Some(
codex_app_server_protocol::DynamicToolResultFormat::StructuredContent
),
thread_id: thread_id.clone(),
turn_id: started.turn_id,
call_id: call_id.to_string(),