diff --git a/docs/computer-use.md b/docs/computer-use.md index 11e4ef4e8..a142c5d03 100644 --- a/docs/computer-use.md +++ b/docs/computer-use.md @@ -44,8 +44,11 @@ Chrome runs headless with a separate random profile for each thread below `/computer-use/`; it never attaches to an everyday browser. Browser state stays within that profile. Profiles are removed when the browser exits normally; crashes may leave a directory that can be deleted when RustCode is -stopped. Screenshots are returned in memory as PNG data, not saved to disk by -this feature. Typed values are redacted from the engine's approval preview; +stopped. External harnesses receive a screenshot as an MCP image. On the +built-in harness the tool result is a short note, and the image is saved with +the thread's attachments and shown to the model after the tool batch, like a +pasted image (a model without image input gets it described by the configured +`vision_model`). Typed values are redacted from the engine's approval preview; the harness may retain its own tool arguments in its transcript. This first slice supports one page per thread, standard macOS/Linux Chrome diff --git a/rustcode/engine/src/computer_use.rs b/rustcode/engine/src/computer_use.rs index d677c2583..8491784b3 100644 --- a/rustcode/engine/src/computer_use.rs +++ b/rustcode/engine/src/computer_use.rs @@ -562,6 +562,88 @@ pub async fn run_approved( result.map(|value| value.to_string()) } +/// Turn a native `browser_screenshot` result into a short text result and +/// the saved image, which the turn attaches after the tool batch. +pub(crate) fn attach_screenshot( + content: &str, + save_image: impl FnOnce(&[u8], &str) -> std::io::Result, +) -> (rustcode_tools::ToolExecutionOutput, Option) { + use rustcode_tools::ToolExecutionOutput; + let decoded = serde_json::from_str::(content) + .map_err(|error| format!("the browser answered with malformed JSON: {error}")) + .and_then(|value| crate::preview::decode_screenshot(&value)); + let (bytes, extension) = match decoded { + Ok(decoded) => decoded, + Err(error) => { + return ( + ToolExecutionOutput::failure(format!("error: browser_screenshot failed: {error}")), + None, + ); + } + }; + match save_image(&bytes, extension) { + Ok(path) => ( + ToolExecutionOutput::success(format!( + "Captured the browser viewport ({} KB {}); the image is attached after this tool result.", + bytes.len().div_ceil(1024), + extension.to_ascii_uppercase() + )), + Some(path), + ), + Err(error) => ( + ToolExecutionOutput::failure(format!( + "error: could not save the browser screenshot: {error}" + )), + None, + ), + } +} + +#[cfg(test)] +mod screenshot_tests { + use super::attach_screenshot; + use base64::{Engine as _, engine::general_purpose::STANDARD}; + use std::path::PathBuf; + + #[test] + fn screenshot_becomes_an_attachment_not_base64_text() { + let png = b"\x89PNG\r\n\x1a\nrest-of-image".to_vec(); + let content = serde_json::json!({ "data": STANDARD.encode(&png) }).to_string(); + let mut saved = None; + let (output, path) = attach_screenshot(&content, |bytes, extension| { + saved = Some((bytes.to_vec(), extension.to_owned())); + Ok(PathBuf::from("/tmp/browser.png")) + }); + assert!(output.success, "{}", output.content); + assert!(output.content.len() < 1024); + assert!(!output.content.contains(&STANDARD.encode(&png))); + assert_eq!(path, Some(PathBuf::from("/tmp/browser.png"))); + assert_eq!(saved, Some((png, "png".to_owned()))); + } + + #[test] + fn unusable_screenshots_are_errors() { + for content in [ + "not json".to_owned(), + serde_json::json!({}).to_string(), + serde_json::json!({ "data": "!!!" }).to_string(), + serde_json::json!({ "data": STANDARD.encode(b"GIF89a") }).to_string(), + ] { + let (output, path) = + attach_screenshot(&content, |_, _| Ok(PathBuf::from("/tmp/unused.png"))); + assert!(!output.success, "{content}"); + assert!(path.is_none()); + } + let content = + serde_json::json!({ "data": STANDARD.encode(b"\x89PNG\r\n\x1a\n") }).to_string(); + let (output, path) = + attach_screenshot(&content, |_, _| Err(std::io::Error::other("disk full"))); + assert!(!output.success); + assert!(output.content.contains("disk full")); + assert!(path.is_none()); + } +} + #[cfg(test)] mod settings_tests { #[test] diff --git a/rustcode/engine/src/network/tool_exec.rs b/rustcode/engine/src/network/tool_exec.rs index 2aa751474..9cd592ddb 100644 --- a/rustcode/engine/src/network/tool_exec.rs +++ b/rustcode/engine/src/network/tool_exec.rs @@ -1571,6 +1571,18 @@ async fn execute_tool_batch_validated( ) .await }; + let execution = if name_clone == "browser_screenshot" && execution.success { + let (output, image) = crate::computer_use::attach_screenshot( + &execution.content, + crate::controller::save_image_attachment, + ); + if let Some(path) = image { + state_clone.lock().await.pending_preview_images.push(path); + } + output + } else { + execution + }; { let mut s = state_clone.lock().await;