rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The page's cursor under a person's pointer, which a client shows over
//! the frame it draws (`rho/browser/cursor`). A headless browser keeps no
//! cursor of its own to read, so rho works it out from the page as a
//! browser would for the common cases: the CSS `cursor` of the element
//! under the pointer, `auto` as `text` over text and editable fields and
//! `default` elsewhere. Under an iframe it is the iframe's own cursor.
//!
//! It runs after each of the person's mouse inputs, while the tab takes no
//! other input. Merged, the moves reach the tab about once a frame (16.5
//! ms, see `browser/service_moves.rs`), and a read took p50 1.7 ms, p90
//! 4.9 ms, max 13 ms (n = 305 moves over the `CURSORS` page of
//! `person_live_tests.rs`, chrome-headless-shell 158, Linux). Not after
//! the wheel: the page scrolls after the browser answers it, so a read
//! then would see what the pointer was over before.

use super::tabs::OpenTab;
use rho_protocol::BrowserCursor;
use std::time::Duration;

/// The cursor a browser shows at `(x, y)` of the page, in CSS pixels of the
/// viewport: the computed `cursor` of the element there, inside open
/// shadow roots, the keyword after an image cursor's `url(...)`, and for
/// `auto`, `text` over editable fields and over the lines of selectable
/// text outside buttons and selects.
const CURSOR: &str = r##"function (x, y) {
  let at = document.elementFromPoint(x, y);
  const shadowRoots = [];
  while (at && at.shadowRoot) {
    const inner = at.shadowRoot.elementFromPoint(x, y);
    if (!inner || inner === at) break;
    shadowRoots.push(at.shadowRoot);
    at = inner;
  }
  if (!at) return "default";
  const style = getComputedStyle(at);
  const cursor = style.cursor.split(",").pop().trim();
  if (cursor !== "auto") return cursor;
  const typed = ["button", "submit", "reset", "checkbox", "radio", "range", "color", "file", "image"];
  if (at.isContentEditable || at.tagName === "TEXTAREA"
      || (at.tagName === "INPUT" && !typed.includes(at.type))) {
    return "text";
  }
  if (style.userSelect === "none" || at.closest("button, select")) return "default";
  const caret = document.caretPositionFromPoint
    ? document.caretPositionFromPoint(x, y, { shadowRoots })
    : document.caretRangeFromPoint(x, y);
  const node = caret && (caret.offsetNode || caret.startContainer);
  if (!node || node.nodeType !== Node.TEXT_NODE) return "default";
  const text = document.createRange();
  text.selectNodeContents(node);
  for (const line of text.getClientRects()) {
    if (x >= line.left && x <= line.right && y >= line.top && y <= line.bottom) return "text";
  }
  return "default";
}"##;

/// How long a read of the cursor may take before the cursor stays as it
/// was: a tripwire far past the slowest read measured, 13 ms, so a page
/// that stalls the read holds the person's next input no longer.
const READ_LIMIT: Duration = Duration::from_millis(250);

/// The cursor at `(x, y)` of `open`'s page, or none if the page could not
/// say in time, as while it navigates. A keyword rho does not know is
/// `default`.
pub(super) async fn under(open: &mut OpenTab, x: f64, y: f64) -> Option<BrowserCursor> {
    let expression = format!("({CURSOR})({x}, {y})");
    let keyword = tokio::time::timeout(READ_LIMIT, open.evaluate(&expression))
        .await
        .ok()?
        .ok()?;
    match serde_json::from_value(keyword) {
        Ok(BrowserCursor::Unknown) | Err(_) => Some(BrowserCursor::Default),
        Ok(cursor) => Some(cursor),
    }
}