mod core;
mod linux;
mod macos;
mod windows;
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
mod stub;
use self::core::{
AppInfo, Backend, Capture, ElementAct, MouseButton, Region, ScrollDirection, TargetSpec,
};
use crate::util::{TOOL_OUTPUT_BUDGET_BYTES, UnwrapPoison};
use crate::{Tool, Workspace};
use anyhow::Context;
use async_trait::async_trait;
use serde::Deserialize;
use serde::Serialize;
use serde_json::{Value, json};
use std::collections::HashMap;
use std::fmt::Write as _;
use std::path::PathBuf;
use std::sync::LazyLock;
use std::time::{Duration, Instant};
use strum::IntoEnumIterator;
use tracing::debug;
#[derive(Debug, Clone, Deserialize, Serialize, strum::EnumIter)]
#[serde(rename_all = "snake_case")]
enum ComputerAction {
Observe {
target: Option<String>,
#[serde(rename = "ref")]
reference: Option<String>,
depth: Option<usize>,
kind: Option<String>,
changed: Option<bool>,
},
Screenshot {
target: Option<String>,
},
Zoom {
target: Option<String>,
region: Region,
},
Apps {},
Windows {
app: Option<String>,
},
Click {
target: Option<String>,
#[serde(rename = "ref")]
reference: Option<String>,
x: Option<f64>,
y: Option<f64>,
button: Option<MouseButton>,
double: Option<bool>,
modifiers: Option<Vec<core::Modifier>>,
},
Type {
text: String,
target: Option<String>,
#[serde(rename = "ref")]
reference: Option<String>,
},
Press {
keys: String,
},
Scroll {
direction: ScrollDirection,
amount: Option<u32>,
target: Option<String>,
x: Option<f64>,
y: Option<f64>,
},
Drag {
from: (f64, f64),
to: (f64, f64),
target: Option<String>,
},
Cursor {},
Wait {
seconds: f64,
},
}
static ACTION_WIRE_NAMES: LazyLock<Vec<String>> = LazyLock::new(|| {
<ComputerAction as IntoEnumIterator>::iter()
.map(|action| wire_name(&action))
.collect()
});
fn wire_name(action: &ComputerAction) -> String {
let v = serde_json::to_value(action).expect("ComputerAction serializes");
match v {
Value::String(s) => s,
Value::Object(obj) => {
assert_eq!(obj.len(), 1, "ComputerAction must serialize to one tag");
obj.keys().next().expect("one tag").clone()
}
other => panic!("unexpected ComputerAction serialization: {other}"),
}
}
const EXPECTED_ACTION_SHAPE: &str = "one of: {\"observe\":{\"target\":\"a1\"}}, \
{\"observe\":{\"changed\":true}}, {\"observe\":{\"ref\":\"e3\",\"kind\":\"button\"}}, \
{\"screenshot\":{\"target\":\"screen\"}}, \
{\"zoom\":{\"target\":\"a1\",\"region\":[x0,y0,x1,y1]}}, {\"apps\":{}}, \
{\"windows\":{\"app\":\"Safari\"}}, \
{\"click\":{\"ref\":\"e3\"} or {\"x\":<0-1000>,\"y\":<0-1000>}}, \
{\"type\":{\"text\":\"...\"}}, {\"press\":{\"keys\":\"cmd+shift+t\"}}, \
{\"scroll\":{\"direction\":\"down\",\"amount\":3}}, \
{\"drag\":{\"from\":[x,y],\"to\":[x,y]}}, {\"cursor\":{}}, {\"wait\":{\"seconds\":2}}";
const DEFAULT_SCROLL_AMOUNT: u32 = 3;
const MAX_WAIT_SECS: f64 = 10.0;
const MODIFIERS: [&str; 4] = ["cmd", "ctrl", "alt", "shift"];
static ACTION_ENTRIES: LazyLock<Vec<Value>> = LazyLock::new(|| {
vec![
super::action_entry_schema(
"observe",
"Read a surface tree.",
&[],
&json!({
"target": {"type": "string"},
"ref": {"type": "string"},
"depth": {"type": "integer", "minimum": 1, "maximum": core::MAX_TREE_DEPTH},
"kind": {"type": "string"},
"changed": {"type": "boolean"}
}),
),
super::action_entry_schema(
"screenshot",
"Capture a surface as an image.",
&[],
&json!({
"target": {"type": "string"}
}),
),
super::action_entry_schema(
"zoom",
"Capture a crop; [x0, y0, x1, y1] normalized.",
&["region"],
&json!({
"target": {"type": "string"},
"region": {"type": "array", "minItems": 4, "maxItems": 4, "items": {"type": "number"}}
}),
),
super::action_entry_schema("apps", "List applications.", &[], &json!({})),
super::action_entry_schema(
"windows",
"List windows (all or one app).",
&[],
&json!({
"app": {"type": "string"}
}),
),
super::action_entry_schema(
"click",
"Click a ref or point.",
&[],
&json!({
"target": {"type": "string"},
"ref": {"type": "string"},
"x": {"type": "number"},
"y": {"type": "number"},
"button": {"type": "string", "enum": ["left", "right", "middle"]},
"double": {"type": "boolean"},
"modifiers": {"type": "array", "maxItems": MODIFIERS.len(), "items": {"enum": MODIFIERS}}
}),
),
super::action_entry_schema(
"type",
"Set value or type into focus.",
&["text"],
&json!({
"text": {"type": "string"},
"target": {"type": "string"},
"ref": {"type": "string"}
}),
),
super::action_entry_schema(
"press",
"Press a key chord.",
&["keys"],
&json!({
"keys": {"type": "string"}
}),
),
super::action_entry_schema(
"scroll",
"Scroll, optionally at a point.",
&["direction"],
&json!({
"direction": {"type": "string", "enum": ["up", "down", "left", "right"]},
"amount": {"type": "integer", "minimum": 1, "maximum": core::MAX_SCROLL_AMOUNT},
"target": {"type": "string"},
"x": {"type": "number"},
"y": {"type": "number"}
}),
),
super::action_entry_schema(
"drag",
"Drag between points.",
&["from", "to"],
&json!({
"from": {"type": "array", "minItems": 2, "maxItems": 2, "items": {"type": "number"}},
"to": {"type": "array", "minItems": 2, "maxItems": 2, "items": {"type": "number"}},
"target": {"type": "string"}
}),
),
super::action_entry_schema("cursor", "Report pointer position.", &[], &json!({})),
super::action_entry_schema(
"wait",
"Pause (seconds).",
&["seconds"],
&json!({
"seconds": {"type": "number", "exclusiveMinimum": 0, "maximum": MAX_WAIT_SECS}
}),
),
]
});
fn check_action_fields(action_value: &Value) -> anyhow::Result<()> {
let Some((name, payload)) = action_value.as_object().and_then(|o| o.iter().next()) else {
return Ok(());
};
let Some(payload) = payload.as_object() else {
return Ok(());
};
let Some(entry) = ACTION_ENTRIES
.iter()
.find(|entry| entry["properties"].get(name.as_str()).is_some())
else {
return Ok(());
};
let accepted = entry["properties"][name.as_str()]["properties"]
.as_object()
.map(|props| props.keys().map(String::as_str).collect::<Vec<_>>())
.unwrap_or_default();
let Some(extra) = payload.keys().find(|key| !accepted.contains(&key.as_str())) else {
return Ok(());
};
anyhow::bail!(
"{name} does not take '{extra}' — its arguments are: {}",
if accepted.is_empty() {
"none".to_string()
} else {
accepted.join(", ")
}
)
}
fn validate_action(action: &ComputerAction) -> anyhow::Result<()> {
match action {
ComputerAction::Observe {
target,
reference,
depth,
kind,
changed,
} => {
check_opt_arg("target", target.as_deref())?;
check_observation(reference.as_deref(), *depth, kind.as_deref(), *changed)?;
}
ComputerAction::Screenshot { target } => check_opt_arg("target", target.as_deref())?,
ComputerAction::Zoom { target, region } => {
check_opt_arg("target", target.as_deref())?;
core::check_region(region)?;
}
ComputerAction::Apps {} | ComputerAction::Cursor {} => {}
ComputerAction::Windows { app } => check_opt_arg("app", app.as_deref())?,
ComputerAction::Click {
target,
reference,
x,
y,
button,
double,
modifiers,
} => check_click(
target.as_deref(),
reference.as_deref(),
*x,
*y,
*button,
*double,
modifiers.as_deref(),
)?,
ComputerAction::Type {
text,
target,
reference,
} => {
core::check_arg_len("text", text)?;
check_opt_arg("target", target.as_deref())?;
check_opt_arg("ref", reference.as_deref())?;
}
ComputerAction::Press { keys } => core::check_arg_len("keys", keys)?,
ComputerAction::Scroll {
amount,
target,
x,
y,
..
} => {
core::check_scroll_amount(amount.unwrap_or(DEFAULT_SCROLL_AMOUNT))?;
check_opt_arg("target", target.as_deref())?;
if coord_pair(*x, *y, "scroll anchor")?.is_none() && target.is_some() {
anyhow::bail!(
"scroll without x and y does not address a surface — the wheel goes to \
whatever is under the pointer; pass x and y to scroll the surface you \
name, or drop target"
);
}
}
ComputerAction::Drag { from, to, target } => {
core::check_point("drag start", from.0, from.1)?;
core::check_point("drag end", to.0, to.1)?;
check_opt_arg("target", target.as_deref())?;
}
ComputerAction::Wait { seconds } => {
if !seconds.is_finite() || *seconds <= 0.0 || *seconds > MAX_WAIT_SECS {
anyhow::bail!("wait seconds must be in (0, {MAX_WAIT_SECS}] — got {seconds}");
}
}
}
Ok(())
}
fn check_observation(
reference: Option<&str>,
depth: Option<usize>,
kind: Option<&str>,
changed: Option<bool>,
) -> anyhow::Result<()> {
check_opt_arg("ref", reference)?;
check_opt_arg("kind", kind)?;
if let Some(depth) = depth
&& !(1..=core::MAX_TREE_DEPTH).contains(&depth)
{
let max = core::MAX_TREE_DEPTH;
anyhow::bail!("observe depth must be 1-{max} levels below the surface — got {depth}");
}
if changed.unwrap_or(false) && (reference.is_some() || depth.is_some() || kind.is_some()) {
anyhow::bail!(
"observe takes changed on its own: it answers what differs from the recorded look, \
which a ref, depth or kind would narrow — drop them, or drop changed to look at \
part of the tree"
);
}
Ok(())
}
fn check_click(
target: Option<&str>,
reference: Option<&str>,
x: Option<f64>,
y: Option<f64>,
button: Option<MouseButton>,
double: Option<bool>,
modifiers: Option<&[core::Modifier]>,
) -> anyhow::Result<()> {
check_opt_arg("target", target)?;
check_opt_arg("ref", reference)?;
match (reference, x, y) {
(Some(reference), None, None) => {
if modifiers.is_some_and(|m| !m.is_empty())
|| button.is_some_and(|b| b != MouseButton::Left)
|| double.is_some_and(|d| d)
{
anyhow::bail!(
"click on element {reference} takes no button, double or modifiers beyond the \
plain left click — those belong to coordinate clicks; drop them, or drop ref \
and give x and y to click a point"
);
}
}
(Some(_), _, _) => anyhow::bail!(
"click takes a ref or x/y, never both — drop x and y to click the element, or drop ref \
to click the point"
),
(None, Some(x), Some(y)) => {
core::check_point("click point", x, y)?;
check_modifiers(modifiers)?;
}
(None, x, y) => anyhow::bail!(
"click needs a ref or both x and y — got {}",
match (x, y) {
(Some(_), None) => "x alone",
(None, Some(_)) => "y alone",
_ => "neither",
}
),
}
Ok(())
}
fn coord_pair(x: Option<f64>, y: Option<f64>, what: &str) -> anyhow::Result<Option<(f64, f64)>> {
match (x, y) {
(Some(x), Some(y)) => {
core::check_point(what, x, y)?;
Ok(Some((x, y)))
}
(None, None) => Ok(None),
(Some(_), None) | (None, Some(_)) => {
anyhow::bail!("{what} needs both x and y, or neither")
}
}
}
fn check_opt_arg(field: &str, value: Option<&str>) -> anyhow::Result<()> {
value.map_or(Ok(()), |value| core::check_arg_len(field, value))
}
fn check_modifiers(modifiers: Option<&[core::Modifier]>) -> anyhow::Result<()> {
let Some(modifiers) = modifiers else {
return Ok(());
};
if modifiers.len() > MODIFIERS.len() {
anyhow::bail!(
"click modifiers take at most {} entries, one for each of {} — got {}",
MODIFIERS.len(),
MODIFIERS.join(", "),
modifiers.len()
);
}
Ok(())
}
fn corrective_action_error(received: &Value) -> String {
format!(
"Invalid computer action arguments. Expected action to be {EXPECTED_ACTION_SHAPE}. \
Received: {received}"
)
}
fn normalize_action(action: Value, args: &Value) -> Result<(Value, Option<String>), String> {
match action {
Value::Object(map) if map.len() == 1 => {
let (name, inner) = map.into_iter().next().expect("len == 1");
if !ACTION_WIRE_NAMES.iter().any(|n| n == &name) {
return Err(corrective_action_error(&json!({name: inner})));
}
if let Some(extra) = args
.as_object()
.and_then(|o| o.keys().find(|k| k.as_str() != "action"))
{
return Err(format!(
"unexpected argument '{extra}' beside the action — send the action and \
nothing else at the top level"
));
}
match inner {
Value::Object(o) => Ok((json!({name: o}), None)),
_ => Err(corrective_action_error(&json!({name: inner}))),
}
}
Value::String(s) => {
let s = s.trim();
if s.starts_with('{') {
let parsed: Value = serde_json::from_str(s)
.map_err(|_| corrective_action_error(&Value::String(s.to_string())))?;
return normalize_action(parsed, args);
}
if ACTION_WIRE_NAMES.iter().any(|n| n == s) {
return build_action_from_siblings(s, args);
}
Err(corrective_action_error(&Value::String(s.to_string())))
}
other => Err(corrective_action_error(&other)),
}
}
fn build_action_from_siblings(name: &str, args: &Value) -> Result<(Value, Option<String>), String> {
let Some(obj) = args.as_object() else {
return Err(corrective_action_error(args));
};
let mut siblings = serde_json::Map::new();
for (k, v) in obj {
if k == "action" {
continue;
}
siblings.insert(k.clone(), v.clone());
}
if let Some(payload) = siblings.remove(name) {
let note = format!("sibling '{name}' object used as action payload");
return match payload {
Value::Object(o) => {
if let Some(extra) = siblings.keys().next() {
return Err(format!(
"unexpected argument '{extra}' beside the '{name}' payload — send the \
payload or the flattened fields, not both"
));
}
Ok((json!({name: o}), Some(note)))
}
_ => Err(corrective_action_error(&Value::Object(siblings))),
};
}
Ok((
json!({name: siblings}),
Some(format!("flattened fields wrapped into {name} action")),
))
}
fn backend() -> &'static dyn Backend {
#[cfg(target_os = "linux")]
{
&linux::LINUX_BACKEND
}
#[cfg(target_os = "macos")]
{
&macos::MACOS_BACKEND
}
#[cfg(target_os = "windows")]
{
&windows::WINDOWS_BACKEND
}
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
{
&stub::STUB_BACKEND
}
}
pub(crate) fn is_advertised() -> bool {
backend().is_supported()
}
type CaptureProbe = (Instant, bool);
static CAPTURE_CACHE: LazyLock<std::sync::Mutex<Option<CaptureProbe>>> =
LazyLock::new(|| std::sync::Mutex::new(None));
const CAPTURE_PROBE_TTL: Duration = Duration::from_secs(60);
pub async fn warm_capture_probe() {
let ok = backend().capture_available().await;
set_capture_cache(ok);
}
fn set_capture_cache(ok: bool) {
let mut guard = CAPTURE_CACHE.lock().unwrap_poison();
*guard = Some((Instant::now(), ok));
}
pub(crate) fn cached_capture_available() -> Option<bool> {
let guard = CAPTURE_CACHE.lock().unwrap_poison();
let (ts, ok) = guard.as_ref()?;
if ts.elapsed() > CAPTURE_PROBE_TTL {
return None;
}
Some(*ok)
}
pub(crate) fn capture_known_available() -> bool {
cached_capture_available() == Some(true)
}
static ACTION_LOCK: LazyLock<tokio::sync::Mutex<()>> =
LazyLock::new(|| tokio::sync::Mutex::new(()));
static CAPTURES: LazyLock<std::sync::Mutex<HashMap<String, PathBuf>>> =
LazyLock::new(|| std::sync::Mutex::new(HashMap::new()));
pub(crate) fn cleanup_turn_state(agent_key: &str) {
CAPTURES.lock().unwrap_poison().remove(agent_key);
core::cleanup_turn(agent_key);
}
pub(crate) fn end_session(agent_key: &str) {
core::end_session(agent_key);
#[cfg(target_os = "macos")]
macos::forget_agent_windows(agent_key);
}
const PLATFORM_DESCRIPTION: Option<&str> = if cfg!(target_os = "linux") {
Some("tool/computer_linux.md")
} else if cfg!(target_os = "macos") {
Some("tool/computer_macos.md")
} else if cfg!(target_os = "windows") {
Some("tool/computer_windows.md")
} else {
None
};
#[derive(Default)]
pub(crate) struct ComputerTool;
#[async_trait]
impl Tool for ComputerTool {
fn name(&self) -> &'static str {
"computer"
}
fn description(&self) -> String {
let mut sections = vec![crate::prompt::load_prompt("tool/computer.md")];
if let Some(asset) = PLATFORM_DESCRIPTION {
sections.push(crate::prompt::load_prompt(asset));
}
crate::tools::tool_reference::standing(
self.name(),
§ions
.iter()
.map(|s| s.trim_end())
.collect::<Vec<_>>()
.join("\n\n"),
)
}
fn is_advertised(&self) -> bool {
self::is_advertised()
}
fn parameters_schema(&self) -> Value {
json!({
"type": "object",
"properties": {
"action": {
"description": "One action per call.",
"oneOf": Value::Array(ACTION_ENTRIES.clone()),
}
},
"required": ["action"],
})
}
async fn execute(&self, _ws: &Workspace, args: Value) -> anyhow::Result<String> {
let action_value = args
.get("action")
.cloned()
.ok_or_else(|| anyhow::anyhow!(corrective_action_error(&args)))?;
let (action_value, normalized_note) =
normalize_action(action_value, &args).map_err(anyhow::Error::msg)?;
if let Some(note) = normalized_note {
super::log_normalization_note("computer", ¬e);
}
check_action_fields(&action_value)?;
let action: ComputerAction = serde_json::from_value(action_value).map_err(|e| {
anyhow::anyhow!(
"Invalid computer action arguments. Expected action to be {EXPECTED_ACTION_SHAPE}. \
Serde error: {e}"
)
})?;
validate_action(&action)?;
debug!(action = ?action, "computer action");
if needs_desktop(&action) {
ensure_interactive_desktop().await?;
}
let result = {
let _lock = ACTION_LOCK.lock().await;
dispatch(&action).await
};
if let Err(err) = &result {
forget_confirmed_gone(&action, err);
}
let delivered = result.map_err(|e| framework_next_step(&action, e))?;
let answer = answer(delivered).await;
Ok(answer)
}
async fn execute_with_payloads(
&self,
ws: &crate::Workspace,
args: serde_json::Value,
) -> anyhow::Result<crate::tools::ToolOutput> {
let text = self.execute(ws, args.clone()).await?;
let (text, image_payloads) = match captured_payload(&args).await {
Ok(Some(payload)) => (text, vec![payload]),
Ok(None) => (text, Vec::new()),
Err(e) => (
format!("{text}\nno image could be attached to this result: {e}"),
Vec::new(),
),
};
Ok(crate::tools::ToolOutput {
text,
image_payloads,
text_is_content: true,
preserve_text: false,
})
}
}
async fn captured_payload(
args: &serde_json::Value,
) -> anyhow::Result<Option<crate::tools::ImagePayload>> {
let Some(action_value) = args.get("action").cloned() else {
return Ok(None);
};
let Ok((action_value, _)) = normalize_action(action_value, args) else {
return Ok(None);
};
let Ok(action) = serde_json::from_value::<ComputerAction>(action_value) else {
return Ok(None);
};
if !matches!(
action,
ComputerAction::Screenshot { .. } | ComputerAction::Zoom { .. }
) {
return Ok(None);
}
let Some(path) = CAPTURES
.lock()
.unwrap_poison()
.get(&core::agent_key())
.cloned()
else {
return Ok(None);
};
let meta = crate::util::local_image_to_compressed_data_uri_with_meta(&path).await?;
Ok(Some(crate::tools::ImagePayload::from_compressed_meta(
&path,
meta,
None,
crate::tools::ImagePayloadSource::Computer,
)))
}
fn needs_desktop(action: &ComputerAction) -> bool {
!matches!(
action,
ComputerAction::Apps {} | ComputerAction::Windows { .. } | ComputerAction::Wait { .. }
)
}
async fn ensure_interactive_desktop() -> anyhow::Result<()> {
let Some(reason) = backend().desktop_unavailable().await else {
return Ok(());
};
Err(core::taxonomy_error(
core::ERR_DESKTOP_UNAVAILABLE,
format!(
"{reason} — this call needs an interactive desktop, so nothing was delivered or read; \
tell the owner and run it again only once the session is back. Listing apps and \
windows, and wait, still work"
),
))
}
fn framework_next_step(action: &ComputerAction, err: anyhow::Error) -> anyhow::Error {
if !core::is_framework_failed(&err) {
return err;
}
let next = match action {
ComputerAction::Apps {} | ComputerAction::Windows { .. } | ComputerAction::Cursor {} => {
"retry it once, and report it if it repeats"
}
ComputerAction::Screenshot { .. } | ComputerAction::Zoom { .. } => {
"re-observe the target if it is a window, then take another route or report it"
}
_ => {
if capture_known_available() {
"re-observe or screenshot to check the target's state, then take another route \
or report it"
} else {
"re-observe to check the target's state, then take another route or report it"
}
}
};
anyhow::anyhow!("{err} — the call was abandoned, so {next}")
}
async fn dispatch(action: &ComputerAction) -> anyhow::Result<Delivered> {
match action {
ComputerAction::Apps {} => Ok(Delivered::Read(apps().await?)),
ComputerAction::Windows { app } => Ok(Delivered::Read(windows(app.as_deref()).await?)),
ComputerAction::Observe {
target,
reference,
depth,
kind,
changed,
} => {
observe(
target.as_deref(),
reference.as_deref(),
*depth,
kind.clone(),
changed.unwrap_or(false),
)
.await
}
ComputerAction::Screenshot { target } => {
Ok(Delivered::Read(capture(target.as_deref(), None).await?))
}
ComputerAction::Zoom { target, region } => Ok(Delivered::Read(
capture(target.as_deref(), Some(region)).await?,
)),
ComputerAction::Click {
target,
reference,
x,
y,
button,
double,
modifiers,
} => {
click(
target.as_deref(),
reference.as_deref(),
*x,
*y,
*button,
*double,
modifiers.as_deref(),
)
.await
}
ComputerAction::Type {
text,
target,
reference,
} => type_text(text, target.as_deref(), reference.as_deref()).await,
ComputerAction::Press { keys } => press(keys).await,
ComputerAction::Scroll {
direction,
amount,
target,
x,
y,
} => scroll(*direction, *amount, target.as_deref(), *x, *y).await,
ComputerAction::Drag { from, to, target } => drag(*from, *to, target.as_deref()).await,
ComputerAction::Cursor {} => Ok(Delivered::Read(cursor().await?)),
ComputerAction::Wait { seconds } => {
tokio::time::sleep(Duration::from_secs_f64(*seconds)).await;
Ok(Delivered::Read(format!("waited {seconds}s")))
}
}
}
async fn press(keys: &str) -> anyhow::Result<Delivered> {
let actuation = backend()
.raw_input(
&TargetSpec::Focused,
core::RawInput::KeyChord {
chord: keys.to_string(),
},
)
.await?;
Ok(Delivered::Global(core::impact_text(
format!("pressed chord: {keys} — a chord goes to the focused application"),
actuation.impact,
)))
}
async fn drag(from: (f64, f64), to: (f64, f64), target: Option<&str>) -> anyhow::Result<Delivered> {
let surface = resolve_in_place(target).await?;
let gesture = core::RawInput::Drag { from, to };
let actuation = backend().raw_input(&surface, gesture.clone()).await?;
let mut text = match &actuation.placed {
Some(placed) => format!(
"dragged {}{} — input {} to {}",
placed.describe(),
on_surface(&surface, Some(placed)),
point_text(from),
point_text(to)
),
None => format!(
"dragged{}{UNREAD_POSITION} — input {} to {}",
on_surface(&surface, None),
point_text(from),
point_text(to)
),
};
text.push_str(unpinned_clause(&surface, &gesture));
Ok(Delivered::OnSurface {
text: core::impact_text(text, actuation.impact),
surface,
})
}
enum Delivered {
Read(String),
OnSurface { text: String, surface: TargetSpec },
Global(String),
}
fn point_text(point: (f64, f64)) -> String {
format!(
"({}, {}) of 0-{:.0}",
point.0,
point.1,
core::NORMALIZED_MAX
)
}
fn on_surface(target: &TargetSpec, placed: Option<&core::Placement>) -> String {
match target {
TargetSpec::Focused => placed.map_or_else(String::new, |placed| {
let geo = placed.geometry;
format!(" on the focused window at ({}, {})", geo.x, geo.y)
}),
other => format!(" on {}", other.describe()),
}
}
fn unpinned_clause(target: &TargetSpec, input: &core::RawInput) -> &'static str {
if core::pinned_input(target, input) {
""
} else {
" — the input goes to whatever is under its coordinates"
}
}
async fn answer(delivered: Delivered) -> String {
match delivered {
Delivered::Read(text) => text,
Delivered::Global(text) => {
let focus = focus_text(None).await;
format!("{text}\n[After: no surface addressed — {focus}]")
}
Delivered::OnSurface { text, surface } => {
let focus = focus_text(Some(&surface)).await;
let state = surface_state(&surface).await;
format!("{text}\n[After: {} — {focus}; {state}]", surface.describe())
}
}
}
async fn focus_text(surface: Option<&TargetSpec>) -> String {
match backend().focused_element(surface).await {
Some(element) => format!("focus: {}", element.describe()),
None => "focus: could not be read this time".to_string(),
}
}
fn no_look() -> String {
format!(
"the tool keeps the {} most recently observed surfaces and holds no remembered look of \
this one",
core::MAX_LOOKS
)
}
async fn surface_state(surface: &TargetSpec) -> String {
let Some((remembered, taken)) = core::remembered_geometry(surface) else {
return format!("nothing to compare against — {}", no_look());
};
match backend().surface_geometry(surface).await {
Ok(now) => match core::geometry_change(&remembered, &now) {
Some(change) => format!(
"the surface {change} since the observation taken {}",
core::look_age(taken)
),
None => format!(
"the surface sits where the observation taken {} found it",
core::look_age(taken)
),
},
Err(_) => "whether the surface moved could not be read".to_string(),
}
}
fn resolve_action_target(target: Option<&str>) -> anyhow::Result<TargetSpec> {
if let Some(t) = target {
return core::resolve_target(Some(t));
}
Ok(core::last_surface().unwrap_or(TargetSpec::Focused))
}
async fn resolve_surface(target: Option<&str>) -> anyhow::Result<TargetSpec> {
let surface = resolve_in_place(target).await?;
if !matches!(surface, TargetSpec::Focused) {
return Ok(surface);
}
let win = backend().surface_window(&surface).await?;
Ok(TargetSpec::from_window(&win))
}
async fn resolve_in_place(target: Option<&str>) -> anyhow::Result<TargetSpec> {
match resolve_action_target(target)? {
app @ TargetSpec::App { .. } => {
let win = backend().surface_window(&app).await?;
Ok(TargetSpec::from_window(&win))
}
other => Ok(other),
}
}
fn forget_confirmed_gone(action: &ComputerAction, err: &anyhow::Error) {
if !core::is_surface_gone(err) {
return;
}
let named = match action {
ComputerAction::Windows { app } => app.as_deref(),
ComputerAction::Observe { target, .. }
| ComputerAction::Screenshot { target }
| ComputerAction::Zoom { target, .. }
| ComputerAction::Click { target, .. }
| ComputerAction::Type { target, .. }
| ComputerAction::Scroll { target, .. }
| ComputerAction::Drag { target, .. } => target.as_deref(),
ComputerAction::Apps {}
| ComputerAction::Press { .. }
| ComputerAction::Cursor {}
| ComputerAction::Wait { .. } => None,
};
let by_ref = match action {
ComputerAction::Click {
reference: Some(reference),
..
}
| ComputerAction::Type {
reference: Some(reference),
..
} => core::resolve_ref(reference).ok().map(|(_, target)| target),
_ => None,
};
if let Some(surface) = by_ref.or_else(|| named.and_then(|arg| core::get_target(arg.trim()))) {
core::forget_look(&surface);
} else if let Some(arg) = named {
core::forget_look_of_app_argument(arg);
} else if let Some(surface) = core::last_surface() {
core::forget_look(&surface);
}
if let Some(id) = named {
core::forget_target(id.trim());
}
}
async fn apps() -> anyhow::Result<String> {
let listing = backend().list_apps().await?;
let surfaces = listing
.apps
.iter()
.map(|app| TargetSpec::App {
app_pid: app.pid,
app_name: app.name.clone(),
})
.collect::<Vec<_>>();
let ids = core::store_app_targets(&surfaces);
let mut lines = Vec::with_capacity(listing.apps.len());
for (app, id) in listing.apps.iter().zip(&ids) {
let pid = app.pid.map_or_else(|| "?".to_string(), |p| p.to_string());
lines.push(format!("[{id}] {} (pid {pid})", app.name));
}
if lines.is_empty() {
lines.push("no apps found".to_string());
}
let names = listing
.apps
.iter()
.map(|app| app.name.as_str())
.collect::<Vec<_>>();
lines.push(frontmost_line(listing.frontmost.as_deref(), &names));
Ok(lines.join("\n"))
}
fn frontmost_line(frontmost: Option<&str>, listed: &[&str]) -> String {
match frontmost {
None => "frontmost application: unknown — the application in front could not be named"
.to_string(),
Some(name) => {
if listed.iter().any(|app| app.eq_ignore_ascii_case(name)) {
format!("frontmost application: {name}")
} else {
format!("frontmost application: {name} (not in the list above)")
}
}
}
}
async fn windows(app: Option<&str>) -> anyhow::Result<String> {
let app_info = match app {
Some(arg) => {
let info = resolve_app(arg).await?;
if backend().app_running(&info).await == Some(false) {
return Err(core::app_absent(&info.name, core::AppAbsence::Quit));
}
Some(info)
}
None => None,
};
let listing = backend().list_windows(app_info.as_ref()).await?;
let surfaces = listing
.windows
.iter()
.map(TargetSpec::from_window)
.collect::<Vec<_>>();
let ids = core::store_window_targets(&surfaces);
Ok(render_windows(&listing, &ids, app_info.as_ref()))
}
fn render_windows(
listing: &core::WindowListing,
ids: &[String],
requested: Option<&AppInfo>,
) -> String {
let mut lines = listing
.windows
.iter()
.zip(ids)
.map(|(win, id)| {
let mut line = format!("[{id}] ");
if let Some(rank) = win.rank {
let _ = write!(line, "#{rank} ");
}
let _ = write!(
line,
"{} — \"{}\" {}x{} @ ({}, {})",
win.app_name,
win.title,
win.surface.width,
win.surface.height,
win.surface.x,
win.surface.y
);
if win.focused {
line.push_str(" [focused]");
}
line
})
.collect::<Vec<_>>();
if lines.is_empty() {
lines.push(
if listing.unread.is_empty() {
"no windows found"
} else {
"no windows could be read"
}
.to_string(),
);
}
if !listing.windows.is_empty() {
match listing.order {
core::WindowOrder::All => {}
core::WindowOrder::Unknown => lines.push(
"front-to-back order: unknown — the platform could not attribute one to the \
windows listed"
.to_string(),
),
core::WindowOrder::Unread => lines.push(
"front-to-back order: unknown — the list it is attributed from could not be read \
this time"
.to_string(),
),
core::WindowOrder::Partial => {
let total = listing.windows.len();
let unplaced = listing
.windows
.iter()
.filter(|window| window.rank.is_none())
.count();
let ranked = total - unplaced;
lines.push(if ranked == 0 {
"front-to-back order: unknown — no listed window has an on-screen entry to be \
ranked by"
.to_string()
} else {
format!(
"front-to-back order: given for {ranked} of the {total} listed windows (#1 \
is in front); {unplaced} {} no on-screen entry",
if unplaced == 1 { "has" } else { "have" }
)
});
}
}
}
lines.push(if let Some(app) = requested {
match listing.frontmost.as_deref() {
None => format!(
"whether {} is frontmost: unknown — the application in front could not be named",
app.name
),
Some(front) if front.eq_ignore_ascii_case(&app.name) => {
format!("{} is frontmost", app.name)
}
Some(front) => format!("{} is not frontmost — {front} is", app.name),
}
} else {
let names = listing
.windows
.iter()
.map(|win| win.app_name.as_str())
.collect::<Vec<_>>();
frontmost_line(listing.frontmost.as_deref(), &names)
});
lines.extend(listing.unread.iter().map(|unread| {
format!(
"windows not listed for {}: {}",
unread.app_name, unread.reason
)
}));
lines.join("\n")
}
async fn resolve_app(arg: &str) -> anyhow::Result<AppInfo> {
let trimmed = arg.trim();
if trimmed == "screen" {
anyhow::bail!("'screen' is a surface, not an app — use the 'apps' action to list apps");
}
if let Some((pid, name)) = core::get_target(trimmed)
.and_then(|target| target.app().map(|(pid, name)| (pid, name.to_string())))
{
return Ok(AppInfo { pid, name });
}
if core::is_identifier(trimmed) {
return Err(core::unknown_identifier(trimmed));
}
let apps = backend().list_apps().await?.apps;
if let Ok(pid) = trimmed.parse::<u32>() {
return apps
.iter()
.find(|a| a.pid == Some(pid))
.cloned()
.ok_or_else(|| no_app_error(trimmed, &apps));
}
if let Some(app) = apps.iter().find(|a| a.name.eq_ignore_ascii_case(trimmed)) {
return Ok(app.clone());
}
Err(no_app_error(trimmed, &apps))
}
fn no_app_error(name: &str, apps: &[AppInfo]) -> anyhow::Error {
if apps.is_empty() {
anyhow::anyhow!(
"no running applications found — start '{name}' or list apps with the 'apps' action"
)
} else {
const SHOWN: usize = 3;
let candidates = apps
.iter()
.take(SHOWN)
.map(|a| a.name.as_str())
.collect::<Vec<_>>()
.join(", ");
let more = apps.len().saturating_sub(SHOWN);
let rest = if more == 0 {
String::new()
} else {
format!(" and {more} more")
};
anyhow::anyhow!(
"no running application matches '{name}' — running apps: {candidates}{rest}; use the \
'apps' action for exact ids"
)
}
}
fn ensure_ref_target_match(
reference: &str,
target: Option<&str>,
cache_target: &TargetSpec,
) -> anyhow::Result<()> {
let Some(t) = target else {
return Ok(());
};
let given = core::resolve_target(Some(t))?;
let same_app =
matches!(given, TargetSpec::App { .. }) && core::same_application(&given, cache_target);
if given != *cache_target && !same_app {
anyhow::bail!(
"ref {reference} belongs to the surface observed for {} — drop the target argument \
or re-observe the target you want",
cache_target.describe()
);
}
Ok(())
}
async fn observe(
target: Option<&str>,
reference: Option<&str>,
depth: Option<usize>,
kind: Option<String>,
changed: bool,
) -> anyhow::Result<Delivered> {
let (surface, at) = match reference {
Some(reference) => {
let (locator, cache_target) = core::resolve_ref(reference)?;
ensure_ref_target_match(reference, target, &cache_target)?;
(cache_target, Some(locator))
}
None => (resolve_surface(target).await?, None),
};
let narrow = core::Narrow { at, depth, kind };
if changed {
return Ok(Delivered::Read(observe_changed(&surface).await?));
}
if narrow.is_narrow() {
let refs_valid = core::refs_belong_to(&surface);
let Some(look) = core::with_look(&surface) else {
return Ok(Delivered::Read(spill(&format!(
"nothing to narrow — {}; run observe on it, or drop ref, depth and kind to read \
it now",
no_look()
))));
};
let answer = core::render_narrow(&look, &narrow, refs_valid)?;
return Ok(Delivered::Read(spill(&answer)));
}
let obs = backend().observe(&surface).await?;
let counts = core::counts(&obs.root);
let full = render_full(&obs, counts);
record_observation(&surface, obs, counts);
Ok(Delivered::Read(spill(&full)))
}
async fn observe_changed(surface: &TargetSpec) -> anyhow::Result<String> {
let obs = backend().observe(surface).await?;
let counts = core::counts(&obs.root);
if let Some(look) = core::with_look(surface) {
return Ok(spill(&core::render_changes(&look, &obs, counts)));
}
let mut output = render_full(&obs, counts);
let _ = writeln!(
output,
"nothing to compare against — {}; it is now recorded and its refs are the ones to act on",
no_look()
);
record_observation(surface, obs, counts);
Ok(spill(&output))
}
fn record_observation(surface: &TargetSpec, obs: core::Observation, counts: core::Counts) {
core::set_last_surface(surface.clone());
core::store_observation(surface.clone(), &obs);
core::remember_look(surface, obs, counts);
}
fn render_full(obs: &core::Observation, counts: core::Counts) -> String {
let mut output = core::render_tree(obs, counts);
if counts.interactive < core::AX_THIN_INTERACTIVE_ELEMENTS {
let _ = writeln!(
output,
"surface exposes few actionable elements — {} interactive of {} nodes read: {}",
counts.interactive,
counts.nodes,
match cached_capture_available() {
Some(true) => "use screenshot to see the rest",
Some(false) => {
"stills are unavailable here, so act on the references it lists, or use \
apps/windows for another surface"
}
None => {
"act on the references it lists, or use apps/windows for another surface"
}
}
);
}
output
}
fn spill(output: &str) -> String {
crate::tools::shell::try_spill_to_file(output.to_string(), TOOL_OUTPUT_BUDGET_BYTES)
}
async fn click(
target: Option<&str>,
reference: Option<&str>,
x: Option<f64>,
y: Option<f64>,
button: Option<MouseButton>,
double: Option<bool>,
modifiers: Option<&[core::Modifier]>,
) -> anyhow::Result<Delivered> {
if let Some(reference) = reference {
let (locator, cache_target) = core::resolve_ref(reference)?;
ensure_ref_target_match(reference, target, &cache_target)?;
let impact = backend()
.act_on_element(&cache_target, &locator, ElementAct::Press)
.await?;
return Ok(Delivered::OnSurface {
text: core::impact_text(
format!(
"clicked element {reference}{}",
on_surface(&cache_target, None)
),
impact,
),
surface: cache_target,
});
}
let Some((x, y)) = coord_pair(x, y, "click point")? else {
anyhow::bail!("click requires an element ref (preferred) or normalized x/y coordinates");
};
let target = resolve_surface(target).await?;
let input = core::RawInput::Click {
point: (x, y),
button: button.unwrap_or(MouseButton::Left),
double: double.unwrap_or(false),
modifiers: modifiers.unwrap_or_default().to_vec(),
};
let actuation = backend().raw_input(&target, input.clone()).await?;
let mut text = match &actuation.placed {
Some(placed) => format!(
"clicked at {}{}",
placed.describe(),
on_surface(&target, Some(placed))
),
None => format!("clicked{}{UNREAD_POSITION}", on_surface(&target, None)),
};
let _ = write!(text, " — input {}", point_text((x, y)));
Ok(Delivered::OnSurface {
text: core::impact_text(text, actuation.impact),
surface: target,
})
}
const UNREAD_POSITION: &str = " — its position could not be read";
async fn type_text(
text: &str,
target: Option<&str>,
reference: Option<&str>,
) -> anyhow::Result<Delivered> {
if let Some(reference) = reference {
let (locator, cache_target) = core::resolve_ref(reference)?;
ensure_ref_target_match(reference, target, &cache_target)?;
let impact = backend()
.act_on_element(
&cache_target,
&locator,
ElementAct::SetValue {
text: text.to_string(),
},
)
.await?;
return Ok(Delivered::OnSurface {
text: core::impact_text(
format!(
"set value on element {reference} in {}",
cache_target.describe()
),
impact,
),
surface: cache_target,
});
}
let surface = resolve_in_place(target).await?;
let input = core::RawInput::TypeText {
text: text.to_string(),
};
let actuation = backend().raw_input(&surface, input.clone()).await?;
Ok(Delivered::OnSurface {
text: core::impact_text(
format!(
"typed text ({} chars){}",
text.chars().count(),
if core::pinned_input(&surface, &input) {
on_surface(&surface, None)
} else {
" — the text goes to the focused element".to_string()
}
),
actuation.impact,
),
surface,
})
}
async fn scroll(
direction: ScrollDirection,
amount: Option<u32>,
target: Option<&str>,
x: Option<f64>,
y: Option<f64>,
) -> anyhow::Result<Delivered> {
let amount = amount.unwrap_or(DEFAULT_SCROLL_AMOUNT);
let Some(point) = coord_pair(x, y, "scroll anchor")? else {
let actuation = backend()
.raw_input(
&TargetSpec::Focused,
core::RawInput::Scroll {
point: None,
direction,
amount,
},
)
.await?;
return Ok(Delivered::Global(core::impact_text(
format!(
"scrolled {amount} ticks {direction:?} — a point-less wheel goes to whatever is \
under the pointer"
),
actuation.impact,
)));
};
let surface = resolve_in_place(target).await?;
let input = core::RawInput::Scroll {
point: Some(point),
direction,
amount,
};
let actuation = backend().raw_input(&surface, input.clone()).await?;
let mut text = match &actuation.placed {
Some(placed) => format!(
"scrolled {amount} ticks {direction:?} at {}{}",
placed.describe(),
on_surface(&surface, Some(placed))
),
None => format!(
"scrolled {amount} ticks {direction:?}{}{UNREAD_POSITION}",
on_surface(&surface, None)
),
};
let _ = write!(text, " — input {}", point_text(point));
text.push_str(unpinned_clause(&surface, &input));
Ok(Delivered::OnSurface {
text: core::impact_text(text, actuation.impact),
surface,
})
}
async fn cursor_surface() -> anyhow::Result<(TargetSpec, core::SurfaceGeometry)> {
let target = resolve_surface(None).await?;
let geo = backend().surface_geometry(&target).await?;
Ok((target, geo))
}
async fn cursor() -> anyhow::Result<String> {
let (ax, ay) = backend().cursor_position().await?;
let (target, geo) = match cursor_surface().await {
Ok(resolved) => resolved,
Err(e) if !core::is_permission_denied(&e) => {
return Ok(format!(
"cursor at absolute ({ax:.1}, {ay:.1}) — no surface to normalize against: {e}"
));
}
Err(e) => return Err(e),
};
if core::contains_point(&geo, ax, ay) {
let (nx, ny) = core::surface_to_normalized(ax, ay, &geo)?;
Ok(format!(
"cursor at normalized ({nx:.1}, {ny:.1}){} — absolute ({ax:.1}, {ay:.1})",
on_surface(&target, None)
))
} else {
Ok(format!(
"cursor outside {surface} ({ax:.1}, {ay:.1}) — surface bounds x:[{:.1}, {:.1}] \
y:[{:.1}, {:.1}]",
geo.x,
geo.x + geo.width,
geo.y,
geo.y + geo.height,
surface = target.describe()
))
}
}
async fn capture(target: Option<&str>, region: Option<&Region>) -> anyhow::Result<String> {
let target = resolve_surface(target).await?;
let budget = core::CaptureBudget::default();
let capture_result = backend().capture(&target, &budget).await;
let cap = match capture_result {
Ok(cap) => {
set_capture_cache(true);
cap
}
Err(e) => {
if cached_capture_available().is_none() {
let ok = backend().capture_available().await;
set_capture_cache(ok);
}
if cached_capture_available() == Some(false) {
return Err(backend().capture_unavailable_error());
}
return Err(e);
}
};
let (cropped, rect_opt) = match region {
Some(region) => {
let rect = core::region_to_pixels(region, cap.width, cap.height)?;
(
core::crop_rgba(&cap, rect, &budget, "the zoom crop")?,
Some(rect),
)
}
None => (cap, None),
};
let (cw, ch) = (cropped.width, cropped.height);
let path = encode_png(cropped)?;
core::set_last_surface(target.clone());
CAPTURES
.lock()
.unwrap_poison()
.insert(core::agent_key(), path.clone());
let kind = if rect_opt.is_some() {
"zoom"
} else {
"screenshot"
};
let rect_note = rect_opt.map_or_else(String::new, |(x, y, w, h)| {
format!(" (cropped rect {x},{y},{w},{h})")
});
let summary = format!(
"{kind} of {}: {cw}x{ch} — {}{rect_note}",
target.describe(),
path.display()
);
Ok(summary)
}
fn encode_png(cap: Capture) -> anyhow::Result<PathBuf> {
let (width, height, rgba, _claim) = cap.into_parts();
crate::util::with_block_in_place(move || {
let img = image::RgbaImage::from_raw(width, height, rgba)
.ok_or_else(|| anyhow::anyhow!("capture dimensions mismatch"))?;
let mut buf = Vec::new();
img.write_to(&mut std::io::Cursor::new(&mut buf), image::ImageFormat::Png)
.map_err(|e| anyhow::anyhow!("failed to encode screen capture as PNG: {e}"))?;
let dir = crate::tools::shell::agent_temp_dir()
.ok_or_else(|| anyhow::anyhow!("no agent temp dir available for screenshots"))?
.join("computer");
std::fs::create_dir_all(&dir)
.with_context(|| format!("failed to create screenshot dir {}", dir.display()))?;
let nonce = rand::random::<u64>();
let path = dir.join(format!("screenshot_{nonce:016x}.png"));
crate::tools::shell::write_owned_spill(&path, &buf)
.with_context(|| format!("failed to write screenshot {}", path.display()))?;
Ok::<PathBuf, anyhow::Error>(path)
})
}
#[cfg(test)]
mod tests {
use super::*;
fn unread_explorer() -> core::UnreadWindows {
core::UnreadWindows {
app_name: "Explorer".to_string(),
reason: "one read timed out".to_string(),
}
}
fn window_info(title: &str, focused: bool, rank: Option<usize>) -> core::WindowInfo {
core::WindowInfo {
app_pid: Some(10),
app_name: "Chrome".to_string(),
title: title.to_string(),
key: core::WindowKey::new(format!("k{title}")),
surface: core::SurfaceGeometry {
x: 0.0,
y: 0.0,
width: 800.0,
height: 600.0,
},
focused,
rank,
}
}
fn app(name: &str) -> AppInfo {
AppInfo {
pid: Some(10),
name: name.to_string(),
}
}
fn action_payload(name: &str) -> Value {
match name {
"observe" | "screenshot" | "windows" | "apps" | "cursor" => json!({}),
"zoom" => json!({"region": [0, 0, 1, 1]}),
"click" => json!({"x": 1, "y": 1}),
"type" => json!({"text": "x"}),
"press" => json!({"keys": "a"}),
"scroll" => json!({"direction": "down"}),
"drag" => json!({"from": [0, 0], "to": [1, 1]}),
"wait" => json!({"seconds": 1}),
other => panic!("no lockstep payload for {other}"),
}
}
struct ListingCase {
name: &'static str,
windows: &'static [(&'static str, bool, Option<usize>)],
unread: bool,
frontmost: Option<&'static str>,
order: core::WindowOrder,
refs: &'static [&'static str],
app: Option<&'static str>,
exact: Option<&'static str>,
present: &'static [&'static str],
}
#[expect(clippy::too_many_lines)]
#[test]
fn window_listing_cases() {
let cases = [
ListingCase {
name: "an_unread_note_has_its_own_line",
windows: &[("Start", false, Some(2))],
unread: true,
frontmost: Some("Chrome"),
order: core::WindowOrder::All,
refs: &["w1"],
app: None,
exact: Some(
"[w1] #2 Chrome — \"Start\" 800x600 @ (0, 0)\n\
frontmost application: Chrome\n\
windows not listed for Explorer: one read timed out",
),
present: &[],
},
ListingCase {
name: "a_listing_where_nothing_could_be_read",
windows: &[],
unread: true,
frontmost: None,
order: core::WindowOrder::Unknown,
refs: &[],
app: None,
exact: Some(
"no windows could be read\n\
frontmost application: unknown — the application in front could not be named\n\
windows not listed for Explorer: one read timed out",
),
present: &[],
},
ListingCase {
name: "an_unordered_listing_says_so_and_names_the_application_in_front",
windows: &[("Inbox", false, None)],
unread: false,
frontmost: Some("Notes"),
order: core::WindowOrder::Unknown,
refs: &["w1"],
app: Some("Chrome"),
exact: Some(
"[w1] Chrome — \"Inbox\" 800x600 @ (0, 0)\n\
front-to-back order: unknown — the platform could not attribute one to the windows \
listed\n\
Chrome is not frontmost — Notes is",
),
present: &[],
},
ListingCase {
name: "the_frontmost_applications_own_listing_marks_its_focused_window",
windows: &[("Inbox", true, Some(1))],
unread: false,
frontmost: Some("Chrome"),
order: core::WindowOrder::All,
refs: &["w1"],
app: Some("Chrome"),
exact: Some(
"[w1] #1 Chrome — \"Inbox\" 800x600 @ (0, 0) [focused]\nChrome is frontmost",
),
present: &[],
},
ListingCase {
name: "a_partial_order_counts_what_it_covers",
windows: &[
("Inbox", false, Some(1)),
("Sent", false, None),
("Drafts", false, None),
],
unread: false,
frontmost: Some("Chrome"),
order: core::WindowOrder::Partial,
refs: &["w1", "w2", "w3"],
app: None,
exact: None,
present: &[
"front-to-back order: given for 1 of the 3 listed windows (#1 is in front); 2 \
have no on-screen entry",
],
},
ListingCase {
name: "a_partial_order_with_no_ranked_window_says_so",
windows: &[
("Inbox", false, None),
("Sent", false, None),
("Drafts", false, None),
],
unread: false,
frontmost: Some("Chrome"),
order: core::WindowOrder::Partial,
refs: &["w1", "w2", "w3"],
app: None,
exact: None,
present: &[
"front-to-back order: unknown — no listed window has an on-screen entry to be \
ranked by",
],
},
ListingCase {
name: "a_frontmost_application_the_listing_could_not_show",
windows: &[("Inbox", false, Some(1))],
unread: false,
frontmost: Some("Dock"),
order: core::WindowOrder::All,
refs: &["w1"],
app: None,
exact: None,
present: &["frontmost application: Dock (not in the list above)"],
},
ListingCase {
name: "an_unread_list_is_not_a_fact_about_the_windows",
windows: &[("Inbox", false, None)],
unread: false,
frontmost: Some("Chrome"),
order: core::WindowOrder::Unread,
refs: &["w1"],
app: None,
exact: None,
present: &[
"front-to-back order: unknown — the list it is attributed from could not be read \
this time",
],
},
];
for case in cases {
let listing = core::WindowListing {
windows: case
.windows
.iter()
.map(|(title, focused, rank)| window_info(title, *focused, *rank))
.collect(),
unread: if case.unread {
vec![unread_explorer()]
} else {
Vec::new()
},
frontmost: case.frontmost.map(str::to_string),
order: case.order,
};
let refs: Vec<String> = case.refs.iter().map(ToString::to_string).collect();
let app = case.app.map(app);
let answer = render_windows(&listing, &refs, app.as_ref());
if let Some(exact) = case.exact {
assert_eq!(answer, exact, "{}: the rendered listing", case.name);
}
for expected in case.present {
assert!(
answer.contains(expected),
"{}: the listing does not say {expected:?}: {answer}",
case.name
);
}
}
}
#[expect(clippy::needless_pass_by_value)] fn parsed_action(name: &str, payload: Value) -> ComputerAction {
let args = json!({"action": {name: payload}});
let (normalized, _) = normalize_action(args["action"].clone(), &args)
.unwrap_or_else(|e| panic!("'{name}' should normalize: {e}"));
serde_json::from_value(normalized).unwrap_or_else(|e| panic!("'{name}' should parse: {e}"))
}
fn refusal(action: &ComputerAction) -> String {
validate_action(action)
.expect_err("the action should be refused")
.to_string()
}
#[test]
fn an_argument_outside_its_range_is_refused_with_the_range() {
let err = refusal(&parsed_action(
"scroll",
json!({"direction": "down", "amount": core::MAX_SCROLL_AMOUNT + 1}),
));
assert!(
err.contains(&format!("1-{}", core::MAX_SCROLL_AMOUNT)),
"{err}"
);
assert!(
validate_action(&parsed_action(
"scroll",
json!({"direction": "down", "amount": 0})
))
.is_err()
);
assert!(validate_action(&parsed_action("scroll", json!({"direction": "down"}))).is_ok());
let err = refusal(&parsed_action(
"type",
json!({"text": "Z".repeat(core::MAX_ARG_CHARS + 1)}),
));
assert!(
err.contains(&format!("max {}", core::MAX_ARG_CHARS)),
"{err}"
);
assert!(
!err.contains('Z'),
"the refused text is counted, never echoed: {err}"
);
assert!(validate_action(&parsed_action("type", json!({"text": "ok"}))).is_ok());
for seconds in [json!(0), json!(-1), json!(MAX_WAIT_SECS + 1.0)] {
assert!(
validate_action(&parsed_action("wait", json!({ "seconds": seconds }))).is_err()
);
}
assert!(validate_action(&parsed_action("wait", json!({"seconds": 1}))).is_ok());
assert!(
validate_action(&parsed_action(
"drag",
json!({"from": [0, core::NORMALIZED_MAX + 1.0], "to": [1, 1]})
))
.is_err()
);
assert!(
validate_action(&parsed_action(
"zoom",
json!({"region": [0, 0, core::NORMALIZED_MAX + 1.0, 10]})
))
.is_err()
);
assert!(
validate_action(&parsed_action(
"zoom",
json!({"region": [10, 10, 1000, 10]})
))
.is_err()
);
assert!(
validate_action(&parsed_action(
"zoom",
json!({"region": [1000, 1000, 0, 0]})
))
.is_ok()
);
assert!(
validate_action(&parsed_action(
"click",
json!({"x": 1, "y": 1, "modifiers": ["cmd", "cmd", "ctrl", "alt", "shift"]})
))
.is_err()
);
assert!(
validate_action(&parsed_action(
"click",
json!({"x": 1, "y": 1, "modifiers": ["cmd", "shift"]})
))
.is_ok()
);
}
#[test]
fn an_argument_the_action_cannot_honour_is_refused() {
let err = {
let args = json!({"action": {"press": {"keys": "cmd+t", "target": "screen"}}});
let (normalized, _) = normalize_action(args["action"].clone(), &args).unwrap();
check_action_fields(&normalized).unwrap_err().to_string()
};
assert!(
err.contains("does not take 'target'") && err.contains("keys"),
"{err}"
);
let err = refusal(&parsed_action(
"click",
json!({"ref": "e3", "x": 10, "y": 10}),
));
assert!(err.contains("never both"), "{err}");
assert!(
validate_action(&parsed_action(
"click",
json!({"ref": "e3", "double": true})
))
.is_err()
);
assert!(
validate_action(&parsed_action(
"click",
json!({"ref": "e3", "button": "left", "double": false, "modifiers": []})
))
.is_ok()
);
assert!(validate_action(&parsed_action("click", json!({"x": 5}))).is_err());
assert!(
validate_action(&parsed_action(
"scroll",
json!({"direction": "down", "x": 5})
))
.is_err()
);
assert!(
validate_action(&parsed_action(
"scroll",
json!({"direction": "down", "target": "a1"})
))
.is_err()
);
assert!(
validate_action(&parsed_action("observe", json!({"depth": 0}))).is_err(),
"a depth of 0 shows nothing"
);
assert!(
validate_action(&parsed_action(
"observe",
json!({"depth": core::MAX_TREE_DEPTH + 1})
))
.is_err()
);
let err = refusal(&parsed_action(
"observe",
json!({"changed": true, "kind": "button"}),
));
assert!(
err.contains("changed on its own"),
"the two questions are refused together: {err}"
);
assert!(validate_action(&parsed_action("observe", json!({"changed": true}))).is_ok());
assert!(
validate_action(&parsed_action(
"observe",
json!({"ref": "e3", "depth": 2, "kind": "button"})
))
.is_ok()
);
let flattened = json!({"action": "click", "x": 1, "y": 1, "text": "hi"});
let (normalized, note) = normalize_action(flattened["action"].clone(), &flattened).unwrap();
assert!(
note.is_some(),
"the flattened form is still tolerated with its note"
);
assert!(check_action_fields(&normalized).is_err());
let tagged = json!({"action": {"click": {"x": 1, "y": 1}}, "foo": 2});
assert!(normalize_action(tagged["action"].clone(), &tagged).is_err());
}
#[test]
fn parameters_schema_enum_lockstep() {
let schema = ComputerTool.parameters_schema();
let action_schemas = schema["properties"]["action"]["oneOf"]
.as_array()
.expect("oneOf should be an array");
let enum_names: Vec<&str> = ACTION_WIRE_NAMES.iter().map(String::as_str).collect();
let one_of_names: Vec<&str> = action_schemas
.iter()
.filter_map(|s| {
s.get("properties")
.and_then(|p| p.as_object())
.and_then(|props| props.keys().next())
.map(String::as_str)
})
.collect();
assert_eq!(
enum_names.len(),
one_of_names.len(),
"oneOf count differs from the enum variant count"
);
for name in &enum_names {
assert!(
one_of_names.contains(name),
"oneOf entry is missing '{name}'"
);
}
for name in &one_of_names {
assert!(
enum_names.contains(name),
"oneOf has an entry '{name}' that is not an enum variant"
);
}
for action_schema in action_schemas {
let Some((name, inner)) = action_schema["properties"]
.as_object()
.and_then(|props| props.iter().next())
else {
panic!("every schema entry names its action");
};
let declared = inner["properties"]
.as_object()
.expect("every action entry declares its arguments")
.keys()
.cloned()
.collect::<std::collections::BTreeSet<_>>();
let payload = action_payload(name);
let wrapped = json!({name.as_str(): payload});
let action: ComputerAction =
serde_json::from_value(wrapped).expect("the schema payload deserializes");
let carried = match serde_json::to_value(&action).expect("a parsed action serializes") {
Value::Object(mut map) => map
.remove(name.as_str())
.and_then(|inner| {
inner
.as_object()
.map(|fields| fields.keys().cloned().collect())
})
.unwrap_or_default(),
other => panic!("an action serializes to one tag, not {other}"),
};
assert_eq!(
declared, carried,
"'{name}': the schema declares {declared:?} but the variant carries {carried:?}"
);
}
for name in &enum_names {
let payload = action_payload(name);
let wrapped_args = json!({"action": {*name: payload.clone()}});
let (wrapped_norm, _) = normalize_action(wrapped_args["action"].clone(), &wrapped_args)
.unwrap_or_else(|_| panic!("wrapped '{name}' should normalize"));
let mut bare_args = json!({"action": *name});
if let Value::Object(payload_obj) = &payload {
bare_args
.as_object_mut()
.expect("bare args is an object")
.extend(payload_obj.clone());
}
let (bare_norm, _) = normalize_action(bare_args["action"].clone(), &bare_args)
.unwrap_or_else(|_| panic!("bare '{name}' should normalize"));
for normalized in [&wrapped_norm, &bare_norm] {
let action: ComputerAction = serde_json::from_value(normalized.clone())
.unwrap_or_else(|e| {
panic!("normalized payload does not deserialize: {normalized} — {e}")
});
assert_eq!(
wire_name(&action),
*name,
"deserialized variant wire name does not match '{name}'"
);
}
}
}
}