Skip to main content

mermaid_cli/providers/tool/computer_use/
scroll.rs

1//! `scroll` — wheel scroll in "up" or "down" direction. Amount is
2//! clamped to `[1, MAX_SCROLL_AMOUNT]` so a runaway model can't
3//! request a million ticks (would blow ARG_MAX building xdotool's
4//! argv on the X11 path).
5
6use std::sync::Arc;
7use std::time::Instant;
8
9use async_trait::async_trait;
10use serde_json::Value;
11
12use crate::constants::MAX_SCROLL_AMOUNT;
13use crate::domain::{ToolDefinition, ToolOutcome};
14use crate::providers::ctx::ExecContext;
15
16use super::super::ToolExecutor;
17use super::computer_use_success;
18use super::driver::ComputerUseDriver;
19
20pub struct ScrollTool {
21    driver: Arc<ComputerUseDriver>,
22}
23
24impl ScrollTool {
25    pub fn new(driver: Arc<ComputerUseDriver>) -> Self {
26        Self { driver }
27    }
28}
29
30#[async_trait]
31impl ToolExecutor for ScrollTool {
32    fn name(&self) -> &'static str {
33        "scroll"
34    }
35
36    fn schema(&self) -> ToolDefinition {
37        ToolDefinition {
38            name: "scroll".to_string(),
39            description: "Scroll the focused window. `direction` is 'up' or 'down'; `amount` is \
40                 clamped to 1..=100 wheel ticks. Scrolls in the currently-focused window; \
41                 click on the scroll target first if it isn't focused."
42                .to_string(),
43            input_schema: serde_json::json!({
44                "type": "object",
45                "properties": {
46                    "direction": { "type": "string", "enum": ["up", "down"] },
47                    "amount": { "type": "integer", "minimum": 1, "maximum": MAX_SCROLL_AMOUNT }
48                },
49                "required": ["direction", "amount"]
50            }),
51        }
52    }
53
54    async fn execute(&self, args: Value, ctx: ExecContext) -> ToolOutcome {
55        let started = Instant::now();
56        if let Err(error) = self.driver.ensure_alive() {
57            return ToolOutcome::error(error, started.elapsed().as_secs_f64());
58        }
59        if let Some(blocked) = super::super::policy_gate::gate_external(
60            &ctx,
61            "scroll",
62            crate::runtime::ToolCategory::ComputerUse,
63            "computer-use: scroll".to_string(),
64            &args,
65        ) {
66            return blocked;
67        }
68
69        let direction = args
70            .get("direction")
71            .and_then(|v| v.as_str())
72            .unwrap_or("down")
73            .to_string();
74        if direction != "up" && direction != "down" {
75            return ToolOutcome::error(
76                format!(
77                    "scroll: direction must be 'up' or 'down', got '{}'",
78                    direction
79                ),
80                started.elapsed().as_secs_f64(),
81            );
82        }
83        let requested = args
84            .get("amount")
85            .and_then(|v| v.as_i64())
86            .map(|n| n as i32)
87            .unwrap_or(3);
88        let amount = requested.clamp(1, MAX_SCROLL_AMOUNT);
89
90        let res = tokio::select! {
91            biased;
92            _ = ctx.token.cancelled() => return ToolOutcome::cancelled(),
93            r = self.driver.scroll(&direction, amount, &ctx.token) => r,
94        };
95        if let Err(e) = res {
96            return ToolOutcome::error(
97                format!("scroll failed: {}", e),
98                started.elapsed().as_secs_f64(),
99            );
100        }
101
102        let clamp_note = if requested != amount {
103            format!(" (clamped from {} to {})", requested, amount)
104        } else {
105            String::new()
106        };
107        computer_use_success(
108            "scroll",
109            args,
110            format!("Scrolled {} by {}{}", direction, amount, clamp_note),
111            started.elapsed().as_secs_f64(),
112        )
113    }
114}