Skip to main content

agentmux_srv/server/
browser_locate.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! `POST /api/v1/ui/browser/locate` — backs Browser's `locate`, and the first half of `click_at`: what a
5//! click at a point of a screenshot would land on, as a reference that `click`, `fill`, `select` and
6//! `check` take (the visual fallback, docs/specs/SPEC_BROWSER_PANE_FORMS_IN_FRAMES_AND_VISUAL_FALLBACK_2026_10_16.md
7//! §4.6). The host does the hit test and keeps the reference; srv checks that the pane is the caller's and
8//! turns the screenshot's pixels into the page's.
9
10use axum::extract::State;
11use axum::http::StatusCode;
12use axum::response::IntoResponse;
13use axum::Json;
14use serde_json::json;
15
16use agentmux_common::api_types::UiBrowserLocateRequest;
17
18use crate::server::ui_handlers::{err_response, proxy_data, target_block_id};
19use crate::server::AppState;
20
21/// The point and the screenshot's width, checked: the host turns them into the page's pixels, as it alone knows
22/// the page's width.
23fn host_body(block_id: &str, req: &UiBrowserLocateRequest) -> Result<serde_json::Value, String> {
24    if !req.x.is_finite() || !req.y.is_finite() || req.x < 0.0 || req.y < 0.0 {
25        return Err("x and y are positions in the screenshot, counted from its top left corner".to_string());
26    }
27    if req.image_width.is_some_and(|w| !w.is_finite() || w < 1.0) {
28        return Err("image_width is the screenshot's width in pixels".to_string());
29    }
30    Ok(json!({ "block_id": block_id, "x": req.x, "y": req.y, "image_width": req.image_width }))
31}
32
33pub(crate) async fn handle_ui_browser_locate(
34    State(state): State<AppState>,
35    caller: Option<axum::Extension<crate::server::caller::Caller>>,
36    Json(req): Json<UiBrowserLocateRequest>,
37) -> impl IntoResponse {
38    // The same checks as a snapshot: the caller's identity, that the pane is theirs, and that it isn't
39    // waiting for the user.
40    let block_id = match target_block_id(&state, caller.as_deref(), &req.auth, req.pane.as_deref()) {
41        Ok(b) => b,
42        Err((code, e)) => return err_response(code, e),
43    };
44    let body = match host_body(&block_id, &req) {
45        Ok(b) => b,
46        Err(e) => return err_response(StatusCode::BAD_REQUEST, e),
47    };
48    tracing::info!(agent_id = %req.auth.agent_id, block_id = %block_id, "[ui-automation] browser locate");
49    proxy_data(&state, "locate", body, std::time::Duration::from_secs(30)).await
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    fn req(x: f64, y: f64, image_width: Option<f64>) -> UiBrowserLocateRequest {
57        serde_json::from_value(json!({ "agent_id": "a", "ts_secs": 1, "sig": "s", "x": x, "y": y, "image_width": image_width })).unwrap()
58    }
59
60    #[test]
61    fn the_host_gets_the_pane_the_point_and_the_images_width() {
62        let body = host_body("b1", &req(200.0, 100.0, Some(1920.0))).unwrap();
63        assert_eq!(body, json!({ "block_id": "b1", "x": 200.0, "y": 100.0, "image_width": 1920.0 }));
64        let plain = host_body("b1", &req(10.0, 20.0, None)).unwrap();
65        assert_eq!(plain["image_width"], serde_json::Value::Null);
66    }
67
68    #[test]
69    fn a_point_or_width_that_cannot_be_in_an_image_is_refused() {
70        assert!(host_body("b", &req(-1.0, 1.0, None)).unwrap_err().starts_with("x and y are"));
71        assert!(host_body("b", &req(1.0, -0.5, Some(800.0))).is_err());
72        for bad in [0.0, -5.0, 0.5] {
73            assert!(host_body("b", &req(1.0, 1.0, Some(bad))).unwrap_err().starts_with("image_width is"));
74        }
75    }
76
77    #[test]
78    fn the_request_reads_its_point_beside_the_signature() {
79        let r: UiBrowserLocateRequest =
80            serde_json::from_value(json!({ "agent_id": "a", "ts_secs": 1, "sig": "s", "pane": "p", "x": 3, "y": 4 })).unwrap();
81        assert_eq!((r.x, r.y, r.image_width, r.pane.as_deref()), (3.0, 4.0, None, Some("p")));
82    }
83}