Skip to main content

agentmux_mcp/tools/
browser_canvas.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! `Browser` actions `click` and `drag` on a canvas's `ref`: where in the canvas, in the pixels `UI` `screenshot` with `canvas`
5//! shows it at the size it was drawn (docs/specs/SPEC_BACKGROUND_BROWSER_SESSIONS_2026_11_28.md section 5). Each waits for
6//! the person's approval card, which shows the canvas with the point marked.
7
8use agentmux_common::browser_control_types::ActPoint;
9
10use super::*;
11
12/// A coordinate argument, or none; a value that is not a number is a mistake, not a zero.
13fn coordinate(arguments: &Value, key: &str) -> Result<Option<f64>> {
14    match arguments.get(key) {
15        None | Some(Value::Null) => Ok(None),
16        Some(v) => v.as_f64().filter(|n| n.is_finite()).map(Some).ok_or_else(|| anyhow::anyhow!("{key} is a number: pixels of the canvas as UI screenshot with canvas shows it")),
17    }
18}
19
20/// The point of a click or a drag in a canvas: none for the other actions, and for a click or a drag on an ordinary element
21/// (the host says so when the ref is not a canvas).
22pub(super) fn point(arguments: &Value, action: &str) -> Result<Option<ActPoint>> {
23    if !matches!(action, "click" | "drag") {
24        return Ok(None);
25    }
26    let (x, y, to_x, to_y) = (coordinate(arguments, "x")?, coordinate(arguments, "y")?, coordinate(arguments, "to_x")?, coordinate(arguments, "to_y")?);
27    match (action, x, y, to_x, to_y) {
28        ("click", None, None, None, None) => Ok(None),
29        ("click", Some(x), Some(y), None, None) => Ok(Some(ActPoint { x, y, to_x: None, to_y: None })),
30        ("click", ..) => anyhow::bail!("a click in a canvas takes x and y (and nothing else); a drag takes x, y, to_x and to_y"),
31        ("drag", Some(x), Some(y), Some(to_x), Some(to_y)) => Ok(Some(ActPoint { x, y, to_x: Some(to_x), to_y: Some(to_y) })),
32        _ => anyhow::bail!("a drag takes x and y (where the button goes down) and to_x and to_y (where it is let go), in canvas pixels, and the canvas's ref"),
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39    use serde_json::json;
40
41    #[test]
42    fn a_click_on_an_ordinary_element_has_no_point() {
43        assert!(point(&json!({ "ref": "e1" }), "click").unwrap().is_none());
44        assert!(point(&json!({ "ref": "e1", "x": 1, "y": 2 }), "fill").unwrap().is_none(), "only click and drag read a point");
45    }
46
47    #[test]
48    fn a_click_in_a_canvas_takes_x_and_y() {
49        let p = point(&json!({ "x": 412, "y": 188.5 }), "click").unwrap().unwrap();
50        assert_eq!((p.x, p.y, p.to_x, p.to_y), (412.0, 188.5, None, None));
51        assert!(point(&json!({ "x": 412 }), "click").is_err(), "x without y is a mistake, not a click at 0");
52        assert!(point(&json!({ "x": 1, "y": 2, "to_x": 3 }), "click").is_err());
53    }
54
55    #[test]
56    fn a_drag_takes_both_ends_and_a_number_that_is_not_one_is_refused() {
57        let p = point(&json!({ "x": 10, "y": 20, "to_x": 300, "to_y": 40 }), "drag").unwrap().unwrap();
58        assert_eq!((p.to_x, p.to_y), (Some(300.0), Some(40.0)));
59        assert!(point(&json!({ "x": 10, "y": 20 }), "drag").is_err());
60        assert!(point(&json!({}), "drag").is_err());
61        assert!(point(&json!({ "x": "left", "y": 2 }), "click").is_err());
62    }
63}