Files
codex/codex-rs/app-server/src/dynamic_tools.rs
T
Brandon Zhang ff146c344d 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
2026-11-24 23:38:37 +00:00

109 lines
3.8 KiB
Rust

use codex_app_server_protocol::DynamicToolCallOutputContentItem;
use codex_app_server_protocol::DynamicToolCallResult;
use codex_core::CodexThread;
use codex_protocol::dynamic_tools::DynamicToolCallResult as CoreDynamicToolCallResult;
use codex_protocol::protocol::Op;
use std::sync::Arc;
use tokio::sync::oneshot;
use tracing::error;
use crate::image_url::REMOTE_IMAGE_URL_ERROR;
use crate::image_url::is_remote_image_url;
use crate::outgoing_message::ClientRequestResult;
use crate::server_request_error::is_turn_transition_server_request_error;
const INVALID_AUDIO_URL_ERROR: &str = "audio URLs must use an inline data URL";
pub(crate) async fn on_call_response(
call_id: String,
receiver: oneshot::Receiver<ClientRequestResult>,
conversation: Arc<CodexThread>,
) {
let response = receiver.await;
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)) => {
error!("request failed with client error: {err:?}");
fallback_response("dynamic tool request failed")
}
Err(err) => {
error!("request failed: {err:?}");
fallback_response("dynamic tool request failed")
}
};
if let Err(err) = conversation
.submit(Op::DynamicToolResult {
id: call_id.clone(),
response,
})
.await
{
error!("failed to submit DynamicToolResponse: {err}");
}
}
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,
DynamicToolCallOutputContentItem::InputImage { image_url }
if is_remote_image_url(image_url)
)
}) =>
{
error!(
message = REMOTE_IMAGE_URL_ERROR,
"dynamic tool response was invalid"
);
fallback_response(REMOTE_IMAGE_URL_ERROR)
}
Ok(DynamicToolCallResult::ContentItems(response))
if response.content_items.iter().any(|item| {
matches!(
item,
DynamicToolCallOutputContentItem::InputAudio { audio_url }
if !audio_url
.get(.."data:".len())
.is_some_and(|prefix| prefix.eq_ignore_ascii_case("data:"))
)
}) =>
{
error!(
message = INVALID_AUDIO_URL_ERROR,
"dynamic tool response was invalid"
);
fallback_response(INVALID_AUDIO_URL_ERROR)
}
Ok(response) => response.into(),
Err(err) => {
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) -> 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,
})
}