diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index e7a96037d7..afaa3fc953 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -1202,14 +1202,16 @@ impl ChatWidget { } } - fn flush_completed_command_activity(&mut self) { - if self - .transcript - .active_cell - .as_ref() - .and_then(|cell| cell.as_any().downcast_ref::()) - .is_some_and(|cell| !cell.is_active()) - { + fn flush_completed_tool_activity(&mut self) { + if self.transcript.active_cell.as_ref().is_some_and(|cell| { + cell.as_any() + .downcast_ref::() + .is_some_and(|cell| !cell.is_active()) + || cell + .as_any() + .downcast_ref::() + .is_some_and(|cell| !cell.is_active()) + }) { self.flush_active_cell(); } } @@ -1247,10 +1249,15 @@ impl ChatWidget { .transcript .active_cell .as_ref() - .is_some_and(|active_cell| active_cell.as_any().is::()) + .is_some_and(|active_cell| { + active_cell.as_any().is::() + || active_cell + .as_any() + .is::() + }) && !cell.transcript_lines(history_width).is_empty() { - self.flush_completed_command_activity(); + self.flush_completed_tool_activity(); } self.app_event_tx.send(AppEvent::InsertHistoryCell(cell)); } @@ -1389,6 +1396,11 @@ impl ChatWidget { exec.mark_failed(); } else if let Some(tool) = cell.as_any_mut().downcast_mut::() { tool.mark_failed(); + } else if let Some(computer) = cell + .as_any_mut() + .downcast_mut::() + { + computer.mark_failed(); } self.add_boxed_history(cell); self.request_pending_usage_output_insertion(); diff --git a/codex-rs/tui/src/chatwidget/command_lifecycle.rs b/codex-rs/tui/src/chatwidget/command_lifecycle.rs index 5b3ffda9fe..00e1ea2a6d 100644 --- a/codex-rs/tui/src/chatwidget/command_lifecycle.rs +++ b/codex-rs/tui/src/chatwidget/command_lifecycle.rs @@ -376,7 +376,12 @@ impl ChatWidget { Some(exec_cell) if exec_cell.is_active() => { ExecEndTarget::OrphanHistoryWhileActiveExec } - None if cell.as_any().is::() => { + None if cell.as_any().is::() + || cell + .as_any() + .downcast_ref::() + .is_some_and(history_cell::ComputerActivityCell::is_active) => + { ExecEndTarget::OrphanHistoryWhileActiveExec } Some(_) | None => ExecEndTarget::NewCell, diff --git a/codex-rs/tui/src/chatwidget/hook_lifecycle.rs b/codex-rs/tui/src/chatwidget/hook_lifecycle.rs index de46ff34b8..db4f428f33 100644 --- a/codex-rs/tui/src/chatwidget/hook_lifecycle.rs +++ b/codex-rs/tui/src/chatwidget/hook_lifecycle.rs @@ -79,7 +79,7 @@ impl ChatWidget { if active_cell_is_empty { self.active_hook_cell = None; } - self.flush_completed_command_activity(); + self.flush_completed_tool_activity(); self.bump_active_cell_revision(); self.app_event_tx .send(AppEvent::InsertHistoryCell(Box::new(completed_cell))); diff --git a/codex-rs/tui/src/chatwidget/tests.rs b/codex-rs/tui/src/chatwidget/tests.rs index 3b4270379a..a1d17beca0 100644 --- a/codex-rs/tui/src/chatwidget/tests.rs +++ b/codex-rs/tui/src/chatwidget/tests.rs @@ -243,6 +243,8 @@ mod compaction_tests; #[path = "tests/completion_styling_tests.rs"] mod completion_styling; mod composer_submission; +#[path = "tests/computer_activity_tests.rs"] +mod computer_activity_tests; #[path = "tests/config_errors_tests.rs"] mod config_errors; mod exec_flow; diff --git a/codex-rs/tui/src/chatwidget/tests/computer_activity_tests.rs b/codex-rs/tui/src/chatwidget/tests/computer_activity_tests.rs new file mode 100644 index 0000000000..ccb91dad0c --- /dev/null +++ b/codex-rs/tui/src/chatwidget/tests/computer_activity_tests.rs @@ -0,0 +1,99 @@ +use super::*; + +fn computer_item( + id: &str, + status: codex_app_server_protocol::McpToolCallStatus, +) -> AppServerThreadItem { + AppServerThreadItem::McpToolCall { + id: id.to_string(), + server: "cua_repl".to_string(), + tool: "js".to_string(), + status, + arguments: json!({"title": format!("Inspect page {id}"), "code": "await cua.getState()"}), + app_context: None, + mcp_app_resource_uri: None, + plugin_id: None, + read_only_hint: None, + result: Some(Box::new(codex_app_server_protocol::McpToolCallResult { + content: vec![json!({"type": "text", "text": format!("Full output for {id}")})], + structured_content: None, + meta: None, + })), + error: None, + duration_ms: Some(5), + } +} + +#[tokio::test] +async fn computer_activity_live_and_replay_group_identically() { + use codex_app_server_protocol::McpToolCallStatus; + let mut outputs = Vec::new(); + for replay in [false, true] { + let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await; + for id in ["1", "2", "3", "4"] { + let item = computer_item(id, McpToolCallStatus::Completed); + if replay { + chat.replay_thread_item(item, "turn-1".to_string(), ReplayKind::ThreadSnapshot); + } else { + chat.on_mcp_tool_call_started(computer_item(id, McpToolCallStatus::InProgress)); + chat.on_mcp_tool_call_completed(item); + } + } + assert!(drain_insert_history(&mut rx).is_empty()); + let cell = chat.transcript.active_cell.as_ref().expect("group"); + outputs.push(( + cell.display_lines(/*width*/ 80), + cell.transcript_lines(/*width*/ 100), + )); + chat.finalize_turn(); + let cells = drain_insert_history(&mut rx); + assert_eq!(cells.len(), 1); + assert_eq!(cells[0], outputs.last().unwrap().0); + } + assert_eq!(outputs[0], outputs[1]); + insta::assert_snapshot!( + "computer_activity_replayed", + lines_to_single_string(&outputs[0].0) + ); +} + +#[tokio::test] +async fn computer_activity_preserves_boundaries_and_expanded_output() { + use codex_app_server_protocol::McpToolCallStatus; + let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await; + chat.on_mcp_tool_call_completed(computer_item("1", McpToolCallStatus::Completed)); + chat.prepare_assistant_message(); + let cells = drain_insert_history(&mut rx); + assert!(lines_to_single_string(&cells[0]).contains("Used computer · 1 action")); + chat.on_mcp_tool_call_completed(computer_item("2", McpToolCallStatus::Completed)); + let mut other = computer_item("other", McpToolCallStatus::Completed); + if let AppServerThreadItem::McpToolCall { server, .. } = &mut other { + *server = "another_server".to_string(); + } + chat.on_mcp_tool_call_completed(other); + let cells = drain_insert_history(&mut rx); + assert_eq!(cells.len(), 2); + assert!(lines_to_single_string(&cells[0]).contains("Used computer · 1 action")); + assert!(lines_to_single_string(&cells[1]).contains("another_server")); + chat.on_mcp_tool_call_completed(computer_item("3", McpToolCallStatus::Completed)); + let transcript = chat.active_cell_transcript_lines(/*width*/ 100).unwrap(); + assert!(lines_to_single_string(&transcript).contains("Full output for 3")); + assert!(lines_to_single_string(&transcript).contains("await cua.getState()")); +} + +#[tokio::test] +async fn computer_activity_out_of_order_completion_and_interruption() { + use codex_app_server_protocol::McpToolCallStatus; + let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await; + chat.on_mcp_tool_call_started(computer_item("1", McpToolCallStatus::InProgress)); + chat.on_mcp_tool_call_started(computer_item("2", McpToolCallStatus::InProgress)); + chat.on_mcp_tool_call_completed(computer_item("2", McpToolCallStatus::Completed)); + assert!(active_blob(&chat).contains("Using computer")); + chat.finalize_turn(); + let cells = drain_insert_history(&mut rx); + assert_eq!(cells.len(), 1); + insta::assert_snapshot!( + "computer_activity_partial_interruption", + lines_to_single_string(&cells[0]) + ); +} diff --git a/codex-rs/tui/src/chatwidget/tests/history_replay.rs b/codex-rs/tui/src/chatwidget/tests/history_replay.rs index 3165fe86d4..676bf7f0ba 100644 --- a/codex-rs/tui/src/chatwidget/tests/history_replay.rs +++ b/codex-rs/tui/src/chatwidget/tests/history_replay.rs @@ -1271,10 +1271,15 @@ async fn failed_repl_mcp_tool_call_preserves_status_and_result() { /*replay_kind*/ None, ); + chat.flush_active_cell(); let cells = drain_insert_history(&mut rx); let [lines] = cells.as_slice() else { panic!("expected one completed MCP tool call for {server}"); }; + if server == "cua_repl" { + insta::assert_snapshot!("failed_computer_activity", lines_to_single_string(lines)); + continue; + } insta::allow_duplicates! { insta::assert_snapshot!(lines_to_single_string(lines), @r#" • Called Inspect workspace diff --git a/codex-rs/tui/src/chatwidget/tests/snapshots/codex_tui__chatwidget__tests__computer_activity_tests__computer_activity_partial_interruption.snap b/codex-rs/tui/src/chatwidget/tests/snapshots/codex_tui__chatwidget__tests__computer_activity_tests__computer_activity_partial_interruption.snap new file mode 100644 index 0000000000..98eeba903f --- /dev/null +++ b/codex-rs/tui/src/chatwidget/tests/snapshots/codex_tui__chatwidget__tests__computer_activity_tests__computer_activity_partial_interruption.snap @@ -0,0 +1,7 @@ +--- +source: tui/src/chatwidget/tests/computer_activity_tests.rs +expression: "lines_to_single_string(&cells[0])" +--- +• Used computer · 2 actions · 1 failed + ├ Failed: Inspect page 1 — interrupted + └ Inspect page 2 diff --git a/codex-rs/tui/src/chatwidget/tests/snapshots/codex_tui__chatwidget__tests__computer_activity_tests__computer_activity_replayed.snap b/codex-rs/tui/src/chatwidget/tests/snapshots/codex_tui__chatwidget__tests__computer_activity_tests__computer_activity_replayed.snap new file mode 100644 index 0000000000..91918a0f84 --- /dev/null +++ b/codex-rs/tui/src/chatwidget/tests/snapshots/codex_tui__chatwidget__tests__computer_activity_tests__computer_activity_replayed.snap @@ -0,0 +1,8 @@ +--- +source: tui/src/chatwidget/tests/computer_activity_tests.rs +expression: "lines_to_single_string(&outputs[0].0)" +--- +• Used computer · 4 actions + ├ Inspect page 3 + ├ Inspect page 4 + └ 2 more · ctrl+t diff --git a/codex-rs/tui/src/chatwidget/tests/snapshots/codex_tui__chatwidget__tests__history_replay__failed_computer_activity.snap b/codex-rs/tui/src/chatwidget/tests/snapshots/codex_tui__chatwidget__tests__history_replay__failed_computer_activity.snap new file mode 100644 index 0000000000..5f3be7af9e --- /dev/null +++ b/codex-rs/tui/src/chatwidget/tests/snapshots/codex_tui__chatwidget__tests__history_replay__failed_computer_activity.snap @@ -0,0 +1,6 @@ +--- +source: tui/src/chatwidget/tests/history_replay.rs +expression: lines_to_single_string(lines) +--- +• Used computer · 1 action · 1 failed + └ Failed: Inspect workspace — permission denied diff --git a/codex-rs/tui/src/chatwidget/tool_lifecycle.rs b/codex-rs/tui/src/chatwidget/tool_lifecycle.rs index d0c3c14d5d..393604de30 100644 --- a/codex-rs/tui/src/chatwidget/tool_lifecycle.rs +++ b/codex-rs/tui/src/chatwidget/tool_lifecycle.rs @@ -173,14 +173,26 @@ impl ChatWidget { return; }; self.flush_answer_stream_with_separator(); + let invocation = McpInvocation { + server, + tool, + arguments: Some(arguments), + }; + if invocation.is_computer_activity() { + let call = history_cell::new_active_mcp_tool_call( + id, + invocation, + self.local_settings.tui.animations, + ); + self.update_computer_activity(|cell| cell.start(call)); + self.bump_active_cell_revision(); + self.request_redraw(); + return; + } self.flush_active_cell(); self.transcript.active_cell = Some(Box::new(history_cell::new_active_mcp_tool_call( id, - McpInvocation { - server, - tool, - arguments: Some(arguments), - }, + invocation, self.local_settings.tui.animations, ))); self.bump_active_cell_revision(); @@ -224,6 +236,19 @@ impl ChatWidget { (None, None) => Err("MCP tool call completed without a result".to_string()), }; + if invocation.is_computer_activity() { + let call = history_cell::new_active_mcp_tool_call( + id, + invocation, + self.local_settings.tui.animations, + ); + self.update_computer_activity(|cell| cell.complete(call, duration, result)); + self.bump_active_cell_revision(); + self.transcript.had_work_activity = true; + self.request_redraw(); + return; + } + let extra_cell = match self .transcript .active_cell @@ -250,6 +275,24 @@ impl ChatWidget { } } + /// Reuse only adjacent computer calls; all other active cells form a transcript boundary. + fn update_computer_activity( + &mut self, + update: impl FnOnce(&mut history_cell::ComputerActivityCell), + ) { + if let Some(cell) = self.transcript.active_cell.as_mut().and_then(|cell| { + cell.as_any_mut() + .downcast_mut::() + }) { + update(cell); + } else { + self.flush_active_cell(); + let mut cell = history_cell::ComputerActivityCell::default(); + update(&mut cell); + self.transcript.active_cell = Some(Box::new(cell)); + } + } + pub(crate) fn handle_queued_item_started_now(&mut self, item: ThreadItem) { match item { item @ ThreadItem::CommandExecution { .. } => { diff --git a/codex-rs/tui/src/history_cell/computer_activity.rs b/codex-rs/tui/src/history_cell/computer_activity.rs new file mode 100644 index 0000000000..03ff57a5c0 --- /dev/null +++ b/codex-rs/tui/src/history_cell/computer_activity.rs @@ -0,0 +1,218 @@ +//! Compact adjacent computer calls without discarding their chronological transcript details. +//! +//! Only CUA calls enter this cell. Other history items and turn boundaries end the group. +//! Preview selection favors failures and images, but never changes the order of retained rows. + +use super::*; + +#[derive(Debug, Default)] +pub(crate) struct ComputerActivityCell { + calls: Vec, +} + +impl ComputerActivityCell { + pub(crate) fn start(&mut self, call: McpToolCallCell) { + if !self + .calls + .iter() + .any(|existing| existing.call_id == call.call_id) + { + self.calls.push(call); + } + } + + /// Completion-only replay follows the same path as a live start followed by completion. + pub(crate) fn complete( + &mut self, + call: McpToolCallCell, + duration: Duration, + result: Result, + ) { + let id = call.call_id.clone(); + self.start(call); + if let Some(call) = self.calls.iter_mut().find(|call| call.call_id == id) { + // Images are represented in the action preview and full MCP transcript. Inserting a + // separate image-output history cell here would prematurely break adjacent grouping. + let _ = call.complete(duration, result); + } + } + + pub(crate) fn is_active(&self) -> bool { + self.calls.iter().any(|call| call.result.is_none()) + } + + pub(crate) fn mark_failed(&mut self) { + for call in &mut self.calls { + if call.result.is_none() { + call.mark_failed(); + } + } + } +} + +fn has_image(call: &McpToolCallCell) -> bool { + matches!(&call.result, Some(Ok(result)) if result.has_image) +} + +/// Keep previews on one physical row, including grapheme clusters and narrow terminals. +fn preview(text: &str, width: usize) -> String { + let normalized = text.split_whitespace().collect::>().join(" "); + let (prefix, rest, _) = take_prefix_by_width(&normalized, width); + if rest.is_empty() { + prefix + } else if width == 0 { + String::new() + } else { + let (prefix, _, _) = take_prefix_by_width(&normalized, width.saturating_sub(1)); + format!("{prefix}…") + } +} + +fn error_preview(call: &McpToolCallCell) -> Option<&str> { + let text = match call.result.as_ref()? { + Err(error) => error.as_str(), + Ok(result) => result + .content + .iter() + .filter_map(result::McpContentBlock::text) + .find(|text| text.starts_with("Script error:")) + .or_else(|| { + result + .content + .iter() + .find_map(|block| block.text().filter(|text| !text.trim().is_empty())) + })?, + }; + // CUA errors can append entire API manuals. The first nonempty line is the diagnostic; + // preserve subsequent lines only in the full transcript, without inventing a paraphrase. + text.strip_prefix("Script error:") + .unwrap_or(text) + .lines() + .map(str::trim) + .find(|line| !line.is_empty()) +} + +impl HistoryCell for ComputerActivityCell { + fn display_lines(&self, width: u16) -> Vec> { + if width == 0 { + return Vec::new(); + } + let active = self.calls.iter().rposition(|call| call.result.is_none()); + let failures = self + .calls + .iter() + .filter(|call| call.success() == Some(false)) + .count(); + let count = self.calls.len(); + let bullet = active + .and_then(|index| { + let call = &self.calls[index]; + activity_indicator( + Some(call.start_time), + MotionMode::from_animations_enabled(call.animations_enabled), + ReducedMotionIndicator::StaticBullet, + ) + }) + .unwrap_or_else(|| "•".dim()); + let label = if active.is_some() { + "Using computer" + } else { + "Used computer" + }; + let unit = if count == 1 { "action" } else { "actions" }; + let mut header = vec![ + bullet, + " ".into(), + label.bold(), + format!(" · {count} {unit}").dim(), + ]; + if failures > 0 { + header.push(format!(" · {failures} failed").red()); + } + let mut lines = adaptive_wrap_line( + &Line::from(header), + RtOptions::new(usize::from(width).max(1)).subsequent_indent(" ".into()), + ) + .iter() + .map(line_to_static) + .collect::>(); + let mut selected = if let Some(index) = active { + vec![index] + } else { + let mut indices = (0..count).collect::>(); + indices.sort_by_key(|&index| { + let call = &self.calls[index]; + std::cmp::Reverse((call.success() == Some(false), has_image(call), index)) + }); + indices.truncate(if count > 3 { 2 } else { 3 }); + indices + }; + selected.sort_unstable(); + let hidden = count - selected.len(); + let visible = selected.len(); + for (row_index, index) in selected.into_iter().enumerate() { + let call = &self.calls[index]; + let title = call + .invocation + .arguments + .as_ref() + .and_then(|args| args.get("title")) + .and_then(serde_json::Value::as_str) + .filter(|title| !title.trim().is_empty()) + .unwrap_or("Computer action"); + let failed = call.success() == Some(false); + let summary = if failed { + match error_preview(call) { + Some(error) if width < 60 => format!("Failed: {error}"), + Some(error) => { + let title = preview(title, usize::from(width) / 3); + format!("Failed: {title} — {error}") + } + None => format!("Failed: {title}"), + } + } else if has_image(call) { + format!("Captured screenshot · {title}") + } else { + title.to_string() + }; + let summary = preview(&summary, usize::from(width).saturating_sub(4)); + let row = if failed { summary.red() } else { summary.dim() }; + let prefix = if row_index + 1 == visible && (hidden == 0 || active.is_some()) { + " └ " + } else { + " ├ " + }; + lines.push(vec![prefix.dim(), row].into()); + } + if hidden > 0 && active.is_none() { + let summary = preview( + &format!("{hidden} more · ctrl+t"), + usize::from(width).saturating_sub(4), + ); + lines.push(vec![" └ ".dim(), summary.dim()].into()); + } + lines + } + + fn transcript_lines(&self, width: u16) -> Vec> { + self.calls + .iter() + .flat_map(|call| call.transcript_lines(width)) + .collect() + } + + fn raw_lines(&self) -> Vec> { + plain_lines(self.transcript_lines(u16::MAX)) + } + + fn transcript_animation_tick(&self) -> Option { + self.calls + .iter() + .filter_map(HistoryCell::transcript_animation_tick) + .max() + } +} + +#[cfg(test)] +#[path = "computer_activity_tests.rs"] +mod tests; diff --git a/codex-rs/tui/src/history_cell/computer_activity_tests.rs b/codex-rs/tui/src/history_cell/computer_activity_tests.rs new file mode 100644 index 0000000000..3efbb5e13f --- /dev/null +++ b/codex-rs/tui/src/history_cell/computer_activity_tests.rs @@ -0,0 +1,152 @@ +use super::*; +use codex_protocol::mcp::CallToolResult; +use pretty_assertions::assert_eq; +use serde_json::json; + +fn call(id: &str, title: &str) -> McpToolCallCell { + McpToolCallCell::new( + id.to_string(), + McpInvocation { + server: "cua_repl".to_string(), + tool: "js".to_string(), + arguments: Some(json!({"title": title, "code": "await cua.getState()"})), + }, + /*animations_enabled*/ false, + ) +} + +fn result(text: &str) -> Result { + Ok(CallToolResult { + content: vec![json!({"type": "text", "text": text})], + structured_content: None, + is_error: Some(false), + meta: None, + }) +} + +fn render(cell: &ComputerActivityCell, width: u16) -> String { + cell.display_lines(width) + .iter() + .map(ToString::to_string) + .collect::>() + .join("\n") +} + +#[test] +fn computer_activity_active_and_interrupted() { + let mut cell = ComputerActivityCell::default(); + cell.complete(call("1", "Opened Chrome"), Duration::ZERO, result("ready")); + cell.start(call("2", "Load the terminal")); + cell.start(call("2", "Duplicate start")); + insta::assert_snapshot!("computer_activity_active", render(&cell, /*width*/ 80)); + cell.mark_failed(); + insta::assert_snapshot!("computer_activity_interrupted", render(&cell, /*width*/ 80)); + assert_eq!( + cell.calls + .iter() + .map(McpToolCallCell::success) + .collect::>(), + vec![Some(true), Some(false)] + ); +} + +#[test] +fn computer_activity_prioritizes_errors_and_images_without_reordering() { + let mut cell = ComputerActivityCell::default(); + cell.complete( + call("1", "Open browser"), + Duration::ZERO, + Err("No browser is available\n\n## Computer Use\nFull tool documentation".to_string()), + ); + cell.complete( + call("2", "Connect to Chrome"), + Duration::ZERO, + result("connected"), + ); + cell.complete( + call("3", "Reproduce the clear-command bug"), + Duration::ZERO, + result("reproduced"), + ); + let mut image = result("screenshot metadata").unwrap(); + image.content.push(json!({"type": "image", "mimeType": "image/png", "data": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGP4z8DwHwAFAAH/iZk9HQAAAABJRU5ErkJggg=="})); + cell.complete( + call("4", "Capture the reproduced bug"), + Duration::ZERO, + Ok(image), + ); + cell.complete( + call("5", "Check the final page"), + Duration::ZERO, + result("checked"), + ); + insta::assert_snapshot!("computer_activity_mixed", render(&cell, /*width*/ 90)); + insta::assert_snapshot!("computer_activity_narrow", render(&cell, /*width*/ 24)); + let transcript = cell + .transcript_lines(/*width*/ 100) + .iter() + .map(ToString::to_string) + .collect::>() + .join("\n"); + insta::assert_snapshot!("computer_activity_expanded", transcript); + assert!(transcript.contains("Full tool documentation")); + assert_eq!( + cell.raw_lines(), + plain_lines(cell.transcript_lines(u16::MAX)) + ); + assert!( + cell.display_lines(/*width*/ 24) + .iter() + .all(|line| line.width() <= 24) + ); +} + +#[test] +fn computer_activity_completed_and_transport_errors_keep_full_details() { + let mut cell = ComputerActivityCell::default(); + cell.complete( + call("1", "Inspect page"), + Duration::ZERO, + result("A successful result with details"), + ); + cell.complete(call("2", "Reload page"), Duration::ZERO, result("Loaded")); + insta::assert_snapshot!("computer_activity_success", render(&cell, /*width*/ 80)); + let mut failure = + result("Browser state changed\nRe-query the browser before continuing").unwrap(); + failure.is_error = Some(true); + cell.complete( + call("3", "Inspect refreshed page"), + Duration::ZERO, + Ok(failure), + ); + insta::assert_snapshot!( + "computer_activity_failed_result", + render(&cell, /*width*/ 90) + ); + cell.mark_failed(); + assert_eq!( + cell.calls + .iter() + .map(McpToolCallCell::success) + .collect::>(), + vec![Some(true), Some(true), Some(false)] + ); +} + +#[test] +fn computer_activity_preview_handles_unicode_and_long_diagnostics() { + assert_eq!(preview(" 你好\nworld ", /*width*/ 6), "你好 …"); + let mut cell = ComputerActivityCell::default(); + cell.complete( + call("1", "检查浏览器"), + Duration::ZERO, + Err("diagnostic ".repeat(200)), + ); + insta::assert_snapshot!("computer_activity_unicode", render(&cell, /*width*/ 24)); + assert!( + cell.display_lines(/*width*/ 24) + .iter() + .all(|line| line.width() <= 24) + ); + assert!(cell.transcript_lines(/*width*/ 100).len() > 20); +} diff --git a/codex-rs/tui/src/history_cell/mcp.rs b/codex-rs/tui/src/history_cell/mcp.rs index 9ff53fd05e..3f0bc2a106 100644 --- a/codex-rs/tui/src/history_cell/mcp.rs +++ b/codex-rs/tui/src/history_cell/mcp.rs @@ -7,6 +7,10 @@ use codex_protocol::mcp::is_node_repl_backed_server; #[path = "mcp_result.rs"] mod result; +#[path = "computer_activity.rs"] +mod computer_activity; +pub(crate) use computer_activity::ComputerActivityCell; + use crate::style::StatusTone; use crate::style::accent_style; use crate::style::status_style; @@ -56,6 +60,12 @@ pub(crate) struct McpInvocation { pub(crate) arguments: Option, } +impl McpInvocation { + pub(crate) fn is_computer_activity(&self) -> bool { + self.server == "cua_repl" + } +} + #[derive(Clone, Copy, Eq, PartialEq)] enum McpToolCallRenderMode { /// Compact presentation used in normal conversation history. @@ -125,7 +135,9 @@ impl McpToolCallCell { } fn result_kind(&self) -> McpResultKind { - if is_node_repl_backed_server(&self.invocation.server) && self.invocation.tool == "js" { + if self.invocation.is_computer_activity() + || (is_node_repl_backed_server(&self.invocation.server) && self.invocation.tool == "js") + { McpResultKind::NodeRepl } else { McpResultKind::Standard diff --git a/codex-rs/tui/src/history_cell/snapshots/codex_tui__history_cell__mcp__computer_activity__tests__computer_activity_active.snap b/codex-rs/tui/src/history_cell/snapshots/codex_tui__history_cell__mcp__computer_activity__tests__computer_activity_active.snap new file mode 100644 index 0000000000..5ea2b1a640 --- /dev/null +++ b/codex-rs/tui/src/history_cell/snapshots/codex_tui__history_cell__mcp__computer_activity__tests__computer_activity_active.snap @@ -0,0 +1,6 @@ +--- +source: tui/src/history_cell/computer_activity_tests.rs +expression: "render(&cell, 80)" +--- +• Using computer · 2 actions + └ Load the terminal diff --git a/codex-rs/tui/src/history_cell/snapshots/codex_tui__history_cell__mcp__computer_activity__tests__computer_activity_expanded.snap b/codex-rs/tui/src/history_cell/snapshots/codex_tui__history_cell__mcp__computer_activity__tests__computer_activity_expanded.snap new file mode 100644 index 0000000000..e28eead005 --- /dev/null +++ b/codex-rs/tui/src/history_cell/snapshots/codex_tui__history_cell__mcp__computer_activity__tests__computer_activity_expanded.snap @@ -0,0 +1,17 @@ +--- +source: tui/src/history_cell/computer_activity_tests.rs +expression: transcript +--- +• Called cua_repl.js({"title":"Open browser","code":"await cua.getState()"}) + └ Error: No browser is available + ## Computer Use + Full tool documentation +• Called cua_repl.js({"title":"Connect to Chrome","code":"await cua.getState()"}) + └ connected +• Called cua_repl.js({"title":"Reproduce the clear-command bug","code":"await cua.getState()"}) + └ reproduced +• Called cua_repl.js({"title":"Capture the reproduced bug","code":"await cua.getState()"}) + └ screenshot metadata + +• Called cua_repl.js({"title":"Check the final page","code":"await cua.getState()"}) + └ checked diff --git a/codex-rs/tui/src/history_cell/snapshots/codex_tui__history_cell__mcp__computer_activity__tests__computer_activity_failed_result.snap b/codex-rs/tui/src/history_cell/snapshots/codex_tui__history_cell__mcp__computer_activity__tests__computer_activity_failed_result.snap new file mode 100644 index 0000000000..63a71f5277 --- /dev/null +++ b/codex-rs/tui/src/history_cell/snapshots/codex_tui__history_cell__mcp__computer_activity__tests__computer_activity_failed_result.snap @@ -0,0 +1,8 @@ +--- +source: tui/src/history_cell/computer_activity_tests.rs +expression: "render(&cell, 90)" +--- +• Used computer · 3 actions · 1 failed + ├ Inspect page + ├ Reload page + └ Failed: Inspect refreshed page — Browser state changed diff --git a/codex-rs/tui/src/history_cell/snapshots/codex_tui__history_cell__mcp__computer_activity__tests__computer_activity_interrupted.snap b/codex-rs/tui/src/history_cell/snapshots/codex_tui__history_cell__mcp__computer_activity__tests__computer_activity_interrupted.snap new file mode 100644 index 0000000000..4d9393b283 --- /dev/null +++ b/codex-rs/tui/src/history_cell/snapshots/codex_tui__history_cell__mcp__computer_activity__tests__computer_activity_interrupted.snap @@ -0,0 +1,7 @@ +--- +source: tui/src/history_cell/computer_activity_tests.rs +expression: "render(&cell, 80)" +--- +• Used computer · 2 actions · 1 failed + ├ Opened Chrome + └ Failed: Load the terminal — interrupted diff --git a/codex-rs/tui/src/history_cell/snapshots/codex_tui__history_cell__mcp__computer_activity__tests__computer_activity_mixed.snap b/codex-rs/tui/src/history_cell/snapshots/codex_tui__history_cell__mcp__computer_activity__tests__computer_activity_mixed.snap new file mode 100644 index 0000000000..85a82bf55a --- /dev/null +++ b/codex-rs/tui/src/history_cell/snapshots/codex_tui__history_cell__mcp__computer_activity__tests__computer_activity_mixed.snap @@ -0,0 +1,8 @@ +--- +source: tui/src/history_cell/computer_activity_tests.rs +expression: "render(&cell, 90)" +--- +• Used computer · 5 actions · 1 failed + ├ Failed: Open browser — No browser is available + ├ Captured screenshot · Capture the reproduced bug + └ 3 more · ctrl+t diff --git a/codex-rs/tui/src/history_cell/snapshots/codex_tui__history_cell__mcp__computer_activity__tests__computer_activity_narrow.snap b/codex-rs/tui/src/history_cell/snapshots/codex_tui__history_cell__mcp__computer_activity__tests__computer_activity_narrow.snap new file mode 100644 index 0000000000..71463deb93 --- /dev/null +++ b/codex-rs/tui/src/history_cell/snapshots/codex_tui__history_cell__mcp__computer_activity__tests__computer_activity_narrow.snap @@ -0,0 +1,9 @@ +--- +source: tui/src/history_cell/computer_activity_tests.rs +expression: "render(&cell, 24)" +--- +• Used computer · 5 + actions · 1 failed + ├ Failed: No browser … + ├ Captured screenshot… + └ 3 more · ctrl+t diff --git a/codex-rs/tui/src/history_cell/snapshots/codex_tui__history_cell__mcp__computer_activity__tests__computer_activity_success.snap b/codex-rs/tui/src/history_cell/snapshots/codex_tui__history_cell__mcp__computer_activity__tests__computer_activity_success.snap new file mode 100644 index 0000000000..2cd0ea94e6 --- /dev/null +++ b/codex-rs/tui/src/history_cell/snapshots/codex_tui__history_cell__mcp__computer_activity__tests__computer_activity_success.snap @@ -0,0 +1,7 @@ +--- +source: tui/src/history_cell/computer_activity_tests.rs +expression: "render(&cell, 80)" +--- +• Used computer · 2 actions + ├ Inspect page + └ Reload page diff --git a/codex-rs/tui/src/history_cell/snapshots/codex_tui__history_cell__mcp__computer_activity__tests__computer_activity_unicode.snap b/codex-rs/tui/src/history_cell/snapshots/codex_tui__history_cell__mcp__computer_activity__tests__computer_activity_unicode.snap new file mode 100644 index 0000000000..47504740bb --- /dev/null +++ b/codex-rs/tui/src/history_cell/snapshots/codex_tui__history_cell__mcp__computer_activity__tests__computer_activity_unicode.snap @@ -0,0 +1,7 @@ +--- +source: tui/src/history_cell/computer_activity_tests.rs +expression: "render(&cell, 24)" +--- +• Used computer · 1 + action · 1 failed + └ Failed: diagnostic …