mermaid_cli/providers/tool/computer_use/
mod.rs1pub mod click;
26pub mod driver;
27pub mod list_windows;
28pub mod mouse_move;
29pub mod press_key;
30pub mod screenshot;
31pub mod scroll;
32pub mod type_text;
33
34use mermaid_domain::ProgressEvent;
35use std::process::Command;
36
37use serde_json::Value;
38
39use crate::providers::ctx::ExecContext;
40use mermaid_domain::{ToolMetadata, ToolOutcome, ToolRunMetadata};
41
42pub use click::ClickTool;
43pub use driver::ComputerUseDriver;
44pub use list_windows::ListWindowsTool;
45pub use mouse_move::MouseMoveTool;
46pub use press_key::PressKeyTool;
47pub use screenshot::ScreenshotTool;
48pub use scroll::ScrollTool;
49pub use type_text::TypeTextTool;
50
51#[derive(Debug, Clone, Copy, PartialEq, Eq)]
53pub enum Backend {
54 X11,
55 Wayland,
56 MacOS,
57 Windows,
58 Unsupported,
59}
60
61impl Backend {
62 #[must_use]
64 pub fn is_usable(self) -> bool {
65 !matches!(self, Self::Unsupported)
66 }
67
68 #[must_use]
74 pub fn supports_input_injection(self) -> bool {
75 matches!(self, Self::X11 | Self::Wayland)
76 }
77
78 #[must_use]
82 pub fn supports_window_listing(self) -> bool {
83 matches!(self, Self::X11)
84 }
85}
86
87#[must_use]
92pub fn probe() -> Backend {
93 if cfg!(target_os = "macos") {
94 if has_command("screencapture") {
95 return Backend::MacOS;
96 }
97 return Backend::Unsupported;
98 }
99 if cfg!(target_os = "windows") {
100 return Backend::Unsupported;
103 }
104
105 if std::env::var("WAYLAND_DISPLAY").is_ok()
107 && has_command("grim")
108 && (has_command("ydotool") || has_command("wtype"))
109 {
110 return Backend::Wayland;
111 }
112
113 if std::env::var("DISPLAY").is_ok()
117 && has_command("scrot")
118 && has_command("xdotool")
119 && xdpyinfo_alive()
120 {
121 return Backend::X11;
122 }
123
124 Backend::Unsupported
125}
126
127#[must_use]
131pub fn display_is_reachable(backend: Backend) -> bool {
132 match backend {
133 Backend::X11 => xdpyinfo_alive(),
134 Backend::Wayland => std::env::var("WAYLAND_DISPLAY").is_ok(),
135 Backend::MacOS | Backend::Windows => true,
136 Backend::Unsupported => false,
137 }
138}
139
140pub(super) fn has_command(name: &str) -> bool {
141 Command::new("which")
145 .arg(name)
146 .output()
147 .map(|o| o.status.success() && !o.stdout.is_empty())
148 .unwrap_or(false)
149}
150
151fn xdpyinfo_alive() -> bool {
155 if !has_command("xdpyinfo") {
156 return Command::new("xdotool")
160 .arg("getactivewindow")
161 .output()
162 .map(|o| o.status.success())
163 .unwrap_or(false);
164 }
165 match Command::new("timeout").arg("0.2").arg("xdpyinfo").output() {
167 Ok(o) => o.status.success(),
168 Err(_) => {
169 Command::new("xdpyinfo")
172 .output()
173 .map(|o| o.status.success())
174 .unwrap_or(false)
175 },
176 }
177}
178
179pub(super) fn computer_use_success(
180 action: &'static str,
181 params: Value,
182 output: String,
183 duration_secs: f64,
184) -> ToolOutcome {
185 ToolOutcome::success(output, format!("{action} completed"), duration_secs).with_metadata(
186 ToolRunMetadata {
187 detail: ToolMetadata::ComputerUse {
188 action: action.to_string(),
189 params,
190 },
191 ..ToolRunMetadata::default()
192 },
193 )
194}
195
196pub(super) async fn emit_auto_screenshot(
205 driver: &ComputerUseDriver,
206 ctx: &ExecContext,
207 caption: &'static str,
208) -> Option<(String, String)> {
209 if !ctx.config.computer_use.auto_screenshot {
210 return None;
211 }
212 let (summary, base64_png) = driver.capture_focused_for_autoshot(&ctx.token).await?;
213 if let Ok(bytes) =
214 base64::Engine::decode(&base64::engine::general_purpose::STANDARD, &base64_png)
215 {
216 let _ = ctx
217 .progress
218 .send(ProgressEvent::Artifact {
219 mime: "image/png".to_string(),
220 data: bytes,
221 caption: Some(caption.to_string()),
222 })
223 .await;
224 }
225 Some((summary, base64_png))
226}
227
228#[cfg(test)]
229mod tests {
230 use super::*;
231
232 #[test]
233 fn backend_unsupported_is_not_usable() {
234 assert!(!Backend::Unsupported.is_usable());
235 assert!(Backend::X11.is_usable());
236 assert!(Backend::Wayland.is_usable());
237 assert!(Backend::MacOS.is_usable());
238 }
239
240 #[test]
241 fn input_injection_only_on_linux_backends() {
242 assert!(Backend::X11.supports_input_injection());
243 assert!(Backend::Wayland.supports_input_injection());
244 assert!(!Backend::MacOS.supports_input_injection());
245 assert!(!Backend::Windows.supports_input_injection());
246 assert!(!Backend::Unsupported.supports_input_injection());
247 }
248
249 #[test]
250 fn window_listing_only_on_x11() {
251 assert!(Backend::X11.supports_window_listing());
252 assert!(!Backend::Wayland.supports_window_listing());
253 assert!(!Backend::MacOS.supports_window_listing());
254 }
255
256 #[test]
257 fn probe_does_not_panic_on_headless() {
258 let _ = probe();
263 }
264
265 #[tokio::test]
266 async fn auto_screenshot_is_noop_when_disabled() {
267 use mermaid_domain::{ToolCallId, TurnId};
268 let mut cfg = mermaid_domain::Config::default();
269 cfg.computer_use.auto_screenshot = false;
270 let (tx, mut rx) = tokio::sync::mpsc::channel::<ProgressEvent>(8);
271 let ctx = ExecContext::new(
272 tokio_util::sync::CancellationToken::new(),
273 tx,
274 ToolCallId(1),
275 TurnId(1),
276 std::path::PathBuf::from("/tmp"),
277 std::sync::Arc::new(cfg),
278 String::new(),
279 None,
280 None,
281 None,
282 mermaid_runtime::SafetyMode::FullAccess,
283 None,
284 None,
285 None,
286 None,
287 None,
288 );
289 let driver = ComputerUseDriver::new(Backend::Unsupported);
291 assert!(emit_auto_screenshot(&driver, &ctx, "test").await.is_none());
292 assert!(rx.try_recv().is_err(), "no artifact emitted when disabled");
293 }
294}