Skip to main content

agentmux_srv/server/
browser_scroll.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! `POST /api/v1/ui/browser/scroll` - backs Browser's `scroll`: the wheel over a pane's page, or an element of the last
5//! snapshot brought into view. The host does it; this carries the call and the answer (where the page is now).
6
7use axum::extract::State;
8use axum::http::StatusCode;
9use axum::response::IntoResponse;
10use axum::Json;
11use serde_json::json;
12
13use crate::server::ui_handlers::{err_response, get_host_ipc, proxy_to_host_timeout, target_block_id};
14use crate::server::AppState;
15
16/// A wheel scroll the page animates takes a second or so; the host gives up on one that does not settle.
17const HOST_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(15);
18
19pub(crate) async fn handle_ui_browser_scroll(
20    State(state): State<AppState>,
21    caller: Option<axum::Extension<crate::server::caller::Caller>>,
22    Json(req): Json<agentmux_common::browser_control_types::UiBrowserScrollRequest>,
23) -> impl IntoResponse {
24    let block_id = match target_block_id(&state, caller.as_deref(), &req.auth, req.pane.as_deref()) {
25        Ok(b) => b,
26        Err((code, e)) => return err_response(code, e),
27    };
28    let host = match get_host_ipc(&state).await {
29        Ok(h) => h,
30        Err(e) => return err_response(StatusCode::SERVICE_UNAVAILABLE, e),
31    };
32    let body = json!({ "block_id": block_id, "ref": req.ref_, "dx": req.dx, "dy": req.dy });
33    let host_resp = match proxy_to_host_timeout(&state, &host, "scroll", body, Some(HOST_TIMEOUT)).await {
34        Ok(v) => v,
35        Err(e) => return err_response(StatusCode::BAD_GATEWAY, e),
36    };
37    if host_resp.get("ok").and_then(|v| v.as_bool()) != Some(true) {
38        let err = host_resp.get("error").and_then(|v| v.as_str()).unwrap_or("unknown host error").to_string();
39        return err_response(StatusCode::BAD_REQUEST, err);
40    }
41    tracing::info!(agent_id = %req.auth.agent_id, block_id = %block_id, "[ui-automation] browser scroll");
42    (StatusCode::OK, Json(json!({ "ok": true, "data": host_resp.get("data").cloned().unwrap_or_default() }))).into_response()
43}