Skip to main content

agentmux_srv/server/
browser_press.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Enter and Space on a granted site: as the click they are
5//! (docs/specs/SPEC_BROWSER_WEB_TERMINAL_TYPING_2026_10_23.md ยง5).
6//!
7//! Enter and Space skip the classification that keeps an Auto-approve grant's
8//! promises, so `key` refuses them on a pane an agent drives. A grant that
9//! carries the Enter and Space power lifts that refusal for the sites it names,
10//! and then the key is never sent: the host says what it would do (a form's
11//! default button, a button, a link, a checkbox) and makes that click through
12//! `act`'s check. A payment, a form that sends to another site and a click the
13//! page changed after approval ask or stop exactly as for `click`. A key whose
14//! click the host can't name is refused.
15
16use axum::response::Response;
17use serde_json::{json, Value};
18
19use super::browser_actions::Note;
20use super::browser_bypass as bypass;
21use super::browser_powers::Power;
22use super::ui_handlers::{banner_for, proxy_value};
23use super::AppState;
24
25/// What became of a `key` call with Enter or Space.
26pub(crate) enum Pressed {
27    /// No grant gives this agent the power on this page: refuse as before.
28    NotGranted,
29    /// Answered: the click was made, or refused.
30    Done(Response),
31}
32
33/// The grant that gives `agent` the power `power` on `pane`, which also covers the page.
34/// The person's standing grant for the agent on the page's site makes one when
35/// the pane has none.
36pub(crate) fn grant(state: &AppState, pane: &str, agent: &str, page: &str, power: Power) -> Option<bypass::Grant> {
37    let has = |g: &bypass::Grant| g.allows(power) && g.covers(page);
38    bypass::active_for(pane, agent)
39        .filter(has)
40        .or_else(|| super::browser_standing::activate_at(state, pane, agent, page).filter(has))
41}
42
43/// `key` (`Enter`, `NumpadEnter` or `Space`) on `block_id`, if the agent holds the
44/// power there. `selector` is focused first, as `key` does for any key.
45pub(crate) async fn press(state: &AppState, block_id: &str, agent: &str, selector: Option<&str>, key: &str, log: &impl Fn() -> Note) -> Pressed {
46    let pane = banner_for(block_id).0;
47    let Ok(page) = super::browser_bypass_routes::page_url(state, block_id).await else { return Pressed::NotGranted };
48    let Some(g) = grant(state, &pane, agent, &page, Power::EnterSpace) else { return Pressed::NotGranted };
49    let host_key = if key == "Space" { "Space" } else { "Enter" };
50    let timeout = std::time::Duration::from_secs(20);
51    if let Err((code, e)) = super::browser_actions::not_paused(block_id) {
52        return Pressed::Done(log().fail(state, code, e));
53    }
54    if let Some(selector) = selector {
55        if let Err((code, e)) = proxy_value(state, "focus_element", json!({ "block_id": block_id, "selector": selector }), timeout).await {
56            return Pressed::Done(log().fail(state, code, e));
57        }
58    }
59    // `approved` carries what the person was shown: the host makes the click only if the page still shows it.
60    let body = |approved: Option<&Value>| {
61        json!({ "block_id": block_id, "key": host_key, "allowed_origins": g.origins, "approved": approved.is_some(), "approved_as": approved })
62    };
63    let first = match proxy_value(state, "press", body(None), timeout).await {
64        Ok(d) => d,
65        Err((code, e)) => return Pressed::Done(log().fail(state, code, e)),
66    };
67    let Some(ask) = first.pointer("/after/needs_approval").cloned() else {
68        return Pressed::Done(log().outcome(state, Ok(first)));
69    };
70    if let Some(resp) = super::browser_bypass_routes::bypassed_click(state, block_id, agent, &ask, body(Some(&ask)), "press", log()).await {
71        return Pressed::Done(resp);
72    }
73    Pressed::Done(super::browser_click_approval::ask_then_click(state, block_id, agent, "press", &ask, body(Some(&ask)), log).await)
74}
75
76/// `key` with Enter or Space (or `text` with a line break) on a pane an agent drives: taken as the
77/// click it is when a grant gives the power, otherwise refused with the reason and the way
78/// forward. `None`: carry on with the key (not a driven pane).
79pub(crate) async fn press_or_refuse(
80    state: &AppState,
81    block_id: &str,
82    req: &agentmux_common::api_types::UiBrowserDispatchKeyRequest,
83    log: impl Fn() -> Note,
84) -> Option<Response> {
85    if let (None, Some(key)) = (req.text.as_deref(), req.key.as_deref()) {
86        match press(state, block_id, &req.auth.agent_id, req.selector.as_deref(), key, &log).await {
87            Pressed::Done(resp) => return Some(resp),
88            Pressed::NotGranted => {}
89        }
90    }
91    super::ui_handlers::refuse_on_driven_pane(
92        state,
93        log(),
94        block_id,
95        "Browser's key action with Enter or Space",
96        "Browser's click action on the button (it asks the user when the click submits something), or ask for the enter_space power with request_auto_approve",
97    )
98    .err()
99}