From 7c46a41bbfffe4396e8e33591ef09e01b21346d1 Mon Sep 17 00:00:00 2001 From: Liang-Ting Jiang Date: Sat, 25 Apr 2026 09:09:30 -0700 Subject: [PATCH] Read file download materialization from result meta --- codex-rs/core/src/codex_apps_file_download.rs | 37 +++++++++++++++---- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/codex-rs/core/src/codex_apps_file_download.rs b/codex-rs/core/src/codex_apps_file_download.rs index 164a79a107..09b019446c 100644 --- a/codex-rs/core/src/codex_apps_file_download.rs +++ b/codex-rs/core/src/codex_apps_file_download.rs @@ -36,21 +36,28 @@ fn codex_apps_download_base_url(turn_context: &TurnContext) -> &str { fn should_materialize_codex_apps_file_download( server: &str, codex_apps_meta: Option<&JsonMap>, + result_meta: Option<&JsonMap>, ) -> bool { if server != codex_mcp::CODEX_APPS_MCP_SERVER_NAME { return false; } - let Some(codex_apps_meta) = codex_apps_meta else { - return false; - }; - - codex_apps_meta - .get(CODEX_APPS_META_MATERIALIZE_FILE_DOWNLOAD_KEY) - .and_then(JsonValue::as_bool) + codex_apps_materialize_file_download_flag(result_meta) + .or_else(|| codex_apps_materialize_file_download_flag(codex_apps_meta)) == Some(true) } +fn codex_apps_materialize_file_download_flag( + codex_apps_meta: Option<&JsonMap>, +) -> Option { + codex_apps_meta + .and_then(|codex_apps_meta| { + codex_apps_meta + .get(CODEX_APPS_META_MATERIALIZE_FILE_DOWNLOAD_KEY) + .and_then(JsonValue::as_bool) + }) +} + pub(crate) async fn maybe_materialize_codex_apps_file_download_result( sess: &Session, turn_context: &TurnContext, @@ -58,7 +65,8 @@ pub(crate) async fn maybe_materialize_codex_apps_file_download_result( codex_apps_meta: Option<&JsonMap>, mut result: CallToolResult, ) -> CallToolResult { - if !should_materialize_codex_apps_file_download(server, codex_apps_meta) + let result_meta = result.meta.as_ref().and_then(JsonValue::as_object); + if !should_materialize_codex_apps_file_download(server, codex_apps_meta, result_meta) || result.is_error == Some(true) { return result; @@ -248,6 +256,19 @@ mod tests { use wiremock::matchers::method; use wiremock::matchers::path; + #[test] + fn should_materialize_codex_apps_file_download_reads_result_meta() { + let result_meta = serde_json::json!({ + CODEX_APPS_META_MATERIALIZE_FILE_DOWNLOAD_KEY: true, + }); + + assert!(should_materialize_codex_apps_file_download( + codex_mcp::CODEX_APPS_MCP_SERVER_NAME, + /*codex_apps_meta*/ None, + result_meta.as_object(), + )); + } + #[tokio::test] async fn codex_apps_file_download_materialization_adds_local_path_for_marked_tools() { let server = MockServer::start().await;