Skip to content

Computer use CLI for AI agents

refactor: address code review findings

Lahfire154cc0cc0c5
diff --git a/crates/macos/src/actions/ax_helpers.rs b/crates/macos/src/actions/ax_helpers.rs
index b7a3b45d8..78f7a1ab5 100644
--- a/crates/macos/src/actions/ax_helpers.rs
+++ b/crates/macos/src/actions/ax_helpers.rs
@@ -50,16 +50,6 @@ mod imp {
err == kAXErrorSuccess
}
- #[allow(dead_code)]
- pub fn set_ax_string(el: &AXElement, attr: &str, value: &str) -> bool {
- let cf_attr = CFString::new(attr);
- let cf_val = CFString::new(value);
- let err = unsafe {
- AXUIElementSetAttributeValue(el.0, cf_attr.as_concrete_TypeRef(), cf_val.as_CFTypeRef())
- };
- err == kAXErrorSuccess
- }
-
pub fn set_ax_string_or_err(
el: &AXElement,
attr: &str,
@@ -185,9 +175,6 @@ mod imp {
pub fn set_ax_bool(_el: &AXElement, _attr: &str, _value: bool) -> bool {
false
}
- pub fn set_ax_string(_el: &AXElement, _attr: &str, _value: &str) -> bool {
- false
- }
pub fn set_ax_string_or_err(
_el: &AXElement,
_attr: &str,
diff --git a/crates/macos/src/actions/chain.rs b/crates/macos/src/actions/chain.rs
index 843f29b7b..baa8d5db3 100644
--- a/crates/macos/src/actions/chain.rs
+++ b/crates/macos/src/actions/chain.rs
@@ -1,53 +1,55 @@
+use agent_desktop_core::action::MouseButton;
use agent_desktop_core::error::{AdapterError, ErrorCode};
+use crate::actions::discovery::ElementCaps;
+use crate::tree::AXElement;
+
+#[allow(dead_code)]
+pub enum ChainStep {
+ Action(&'static str),
+ SetBool {
+ attr: &'static str,
+ value: bool,
+ },
+ SetDynamic {
+ attr: &'static str,
+ },
+ FocusThenAction(&'static str),
+ FocusThenConfirmOrPress,
+ ChildActions {
+ actions: &'static [&'static str],
+ limit: usize,
+ },
+ AncestorActions {
+ actions: &'static [&'static str],
+ limit: usize,
+ },
+ Custom {
+ label: &'static str,
+ func: fn(&AXElement, &ElementCaps) -> bool,
+ },
+ CGClick {
+ button: MouseButton,
+ count: u32,
+ },
+}
+
+pub struct ChainDef {
+ pub pre_scroll: bool,
+ pub steps: &'static [ChainStep],
+ pub suggestion: &'static str,
+}
+
+pub struct ChainContext<'a> {
+ pub dynamic_value: Option<&'a str>,
+}
+
#[cfg(target_os = "macos")]
mod imp {
use super::*;
- use crate::actions::{ax_helpers, discovery::ElementCaps};
- use crate::tree::AXElement;
- use agent_desktop_core::action::MouseButton;
+ use crate::actions::ax_helpers;
use std::time::{Duration, Instant};
- #[allow(dead_code)]
- pub enum ChainStep {
- Action(&'static str),
- SetBool {
- attr: &'static str,
- value: bool,
- },
- SetDynamic {
- attr: &'static str,
- },
- FocusThenAction(&'static str),
- FocusThenConfirmOrPress,
- ChildActions {
- actions: &'static [&'static str],
- limit: usize,
- },
- AncestorActions {
- actions: &'static [&'static str],
- limit: usize,
- },
- Custom {
- label: &'static str,
- func: fn(&AXElement, &ElementCaps) -> bool,
- },
- CGClick {
- button: MouseButton,
- count: u32,
- },
- }
-
- pub struct ChainDef {
- pub pre_scroll: bool,
- pub steps: &'static [ChainStep],
- pub suggestion: &'static str,
- }
-
- pub struct ChainContext<'a> {
- pub dynamic_value: Option<&'a str>,
- }
-
const CHAIN_TIMEOUT: Duration = Duration::from_secs(10);
pub fn execute_chain(
@@ -173,49 +175,6 @@ mod imp {
#[cfg(not(target_os = "macos"))]
mod imp {
use super::*;
- use crate::actions::discovery::ElementCaps;
- use crate::tree::AXElement;
- use agent_desktop_core::action::MouseButton;
-
- #[allow(dead_code)]
- pub enum ChainStep {
- Action(&'static str),
- SetBool {
- attr: &'static str,
- value: bool,
- },
- SetDynamic {
- attr: &'static str,
- },
- FocusThenAction(&'static str),
- FocusThenConfirmOrPress,
- ChildActions {
- actions: &'static [&'static str],
- limit: usize,
- },
- AncestorActions {
- actions: &'static [&'static str],
- limit: usize,
- },
- Custom {
- label: &'static str,
- func: fn(&AXElement, &ElementCaps) -> bool,
- },
- CGClick {
- button: MouseButton,
- count: u32,
- },
- }
-
- pub struct ChainDef {
- pub pre_scroll: bool,
- pub steps: &'static [ChainStep],
- pub suggestion: &'static str,
- }
-
- pub struct ChainContext<'a> {
- pub dynamic_value: Option<&'a str>,
- }
pub fn execute_chain(
_el: &AXElement,
@@ -231,5 +190,4 @@ mod imp {
}
}
-#[allow(unused_imports)]
-pub(crate) use imp::{execute_chain, ChainContext, ChainDef, ChainStep};
+pub(crate) use imp::execute_chain;
diff --git a/crates/macos/src/actions/discovery.rs b/crates/macos/src/actions/discovery.rs
index 7cb37c2a1..d574662c6 100644
--- a/crates/macos/src/actions/discovery.rs
+++ b/crates/macos/src/actions/discovery.rs
@@ -1,86 +1,26 @@
-#[cfg(target_os = "macos")]
-#[allow(dead_code)]
-mod imp {
- use crate::actions::ax_helpers;
- use crate::tree::AXElement;
-
- pub struct ElementCaps {
- pub actions: Vec<String>,
- pub settable_value: bool,
- pub settable_focus: bool,
- pub settable_selected: bool,
- pub settable_disclosing: bool,
- pub role: Option<String>,
- pub has_children: bool,
- pub pid: Option<i32>,
- }
+use crate::tree::AXElement;
- pub fn discover(el: &AXElement) -> ElementCaps {
- let actions = ax_helpers::list_ax_actions(el);
- let settable_value = ax_helpers::is_attr_settable(el, "AXValue");
- let settable_focus = ax_helpers::is_attr_settable(el, "AXFocused");
- let settable_selected = ax_helpers::is_attr_settable(el, "AXSelected");
- let settable_disclosing = ax_helpers::is_attr_settable(el, "AXDisclosing");
- let role = ax_helpers::element_role(el);
- let has_children = crate::tree::copy_ax_array(el, "AXChildren")
- .map(|c| !c.is_empty())
- .unwrap_or(false);
- let pid = crate::system::app_ops::pid_from_element(el);
-
- ElementCaps {
- actions,
- settable_value,
- settable_focus,
- settable_selected,
- settable_disclosing,
- role,
- has_children,
- pid,
- }
- }
+pub struct ElementCaps {
+ pub settable_focus: bool,
+ pub settable_selected: bool,
+ pub settable_disclosing: bool,
+}
- impl ElementCaps {
- pub fn has_action(&self, name: &str) -> bool {
- self.actions.iter().any(|a| a == name)
- }
+#[cfg(target_os = "macos")]
+pub fn discover(el: &AXElement) -> ElementCaps {
+ use crate::actions::ax_helpers;
+ ElementCaps {
+ settable_focus: ax_helpers::is_attr_settable(el, "AXFocused"),
+ settable_selected: ax_helpers::is_attr_settable(el, "AXSelected"),
+ settable_disclosing: ax_helpers::is_attr_settable(el, "AXDisclosing"),
}
}
#[cfg(not(target_os = "macos"))]
-#[allow(dead_code)]
-mod imp {
- use crate::tree::AXElement;
-
- pub struct ElementCaps {
- pub actions: Vec<String>,
- pub settable_value: bool,
- pub settable_focus: bool,
- pub settable_selected: bool,
- pub settable_disclosing: bool,
- pub role: Option<String>,
- pub has_children: bool,
- pub pid: Option<i32>,
- }
-
- pub fn discover(_el: &AXElement) -> ElementCaps {
- ElementCaps {
- actions: Vec::new(),
- settable_value: false,
- settable_focus: false,
- settable_selected: false,
- settable_disclosing: false,
- role: None,
- has_children: false,
- pid: None,
- }
- }
-
- impl ElementCaps {
- pub fn has_action(&self, _name: &str) -> bool {
- false
- }
+pub fn discover(_el: &AXElement) -> ElementCaps {
+ ElementCaps {
+ settable_focus: false,
+ settable_selected: false,
+ settable_disclosing: false,
}
}
-
-#[allow(unused_imports)]
-pub(crate) use imp::{discover, ElementCaps};
diff --git a/src/cli_args.rs b/src/cli_args.rs
index e5d011f0c..2048a7351 100644
--- a/src/cli_args.rs
+++ b/src/cli_args.rs
@@ -12,6 +12,21 @@ pub enum Surface {
Alert,
}
+impl Surface {
+ pub fn to_core(&self) -> agent_desktop_core::adapter::SnapshotSurface {
+ use agent_desktop_core::adapter::SnapshotSurface;
+ match self {
+ Self::Window => SnapshotSurface::Window,
+ Self::Focused => SnapshotSurface::Focused,
+ Self::Menu => SnapshotSurface::Menu,
+ Self::Menubar => SnapshotSurface::Menubar,
+ Self::Sheet => SnapshotSurface::Sheet,
+ Self::Popover => SnapshotSurface::Popover,
+ Self::Alert => SnapshotSurface::Alert,
+ }
+ }
+}
+
#[derive(Parser, Debug)]
pub struct SnapshotArgs {
#[arg(long, help = "Filter to application by name")]
diff --git a/src/dispatch.rs b/src/dispatch.rs
index 49d922286..c04cd5d4f 100644
--- a/src/dispatch.rs
+++ b/src/dispatch.rs
@@ -16,7 +16,7 @@ use serde_json::Value;
use crate::cli::Commands;
pub fn dispatch(cmd: Commands, adapter: &dyn PlatformAdapter) -> Result<Value, AppError> {
- tracing::debug!("dispatch: {}", command_name(&cmd));
+ tracing::debug!("dispatch: {}", cmd.name());
match cmd {
Commands::Snapshot(a) => snapshot::execute(
snapshot::SnapshotArgs {
@@ -26,7 +26,7 @@ pub fn dispatch(cmd: Commands, adapter: &dyn PlatformAdapter) -> Result<Value, A
include_bounds: a.include_bounds,
interactive_only: a.interactive_only,
compact: a.compact,
- surface: cli_surface_to_core(&a.surface),
+ surface: a.surface.to_core(),
},
adapter,
),
@@ -390,71 +390,3 @@ fn parse_xy_opt(s: Option<&str>) -> Result<Option<(f64, f64)>, AppError> {
None => Ok(None),
}
}
-
-fn command_name(cmd: &Commands) -> &'static str {
- match cmd {
- Commands::Snapshot(_) => "snapshot",
- Commands::Find(_) => "find",
- Commands::Screenshot(_) => "screenshot",
- Commands::Get(_) => "get",
- Commands::Is(_) => "is",
- Commands::Click(_) => "click",
- Commands::DoubleClick(_) => "double-click",
- Commands::TripleClick(_) => "triple-click",
- Commands::RightClick(_) => "right-click",
- Commands::Type(_) => "type",
- Commands::SetValue(_) => "set-value",
- Commands::Clear(_) => "clear",
- Commands::Focus(_) => "focus",
- Commands::Toggle(_) => "toggle",
- Commands::Check(_) => "check",
- Commands::Uncheck(_) => "uncheck",
- Commands::Expand(_) => "expand",
- Commands::Collapse(_) => "collapse",
- Commands::Select(_) => "select",
- Commands::Scroll(_) => "scroll",
- Commands::ScrollTo(_) => "scroll-to",
- Commands::Press(_) => "press",
- Commands::KeyDown(_) => "key-down",
- Commands::KeyUp(_) => "key-up",
- Commands::Hover(_) => "hover",
- Commands::Drag(_) => "drag",
- Commands::MouseMove(_) => "mouse-move",
- Commands::MouseClick(_) => "mouse-click",
- Commands::MouseDown(_) => "mouse-down",
- Commands::MouseUp(_) => "mouse-up",
- Commands::Launch(_) => "launch",
- Commands::CloseApp(_) => "close-app",
- Commands::ListWindows(_) => "list-windows",
- Commands::ListApps => "list-apps",
- Commands::ListSurfaces(_) => "list-surfaces",
- Commands::FocusWindow(_) => "focus-window",
- Commands::ResizeWindow(_) => "resize-window",
- Commands::MoveWindow(_) => "move-window",
- Commands::Minimize(_) => "minimize",
- Commands::Maximize(_) => "maximize",
- Commands::Restore(_) => "restore",
- Commands::ClipboardGet => "clipboard-get",
- Commands::ClipboardSet(_) => "clipboard-set",
- Commands::ClipboardClear => "clipboard-clear",
- Commands::Wait(_) => "wait",
- Commands::Status => "status",
- Commands::Permissions(_) => "permissions",
- Commands::Version(_) => "version",
- Commands::Batch(_) => "batch",
- }
-}
-
-fn cli_surface_to_core(s: &crate::cli::Surface) -> agent_desktop_core::adapter::SnapshotSurface {
- use crate::cli::Surface;
- use agent_desktop_core::adapter::SnapshotSurface;
- match s {
- Surface::Window => SnapshotSurface::Window,
- Surface::Focused => SnapshotSurface::Focused,
- Surface::Menu => SnapshotSurface::Menu,
- Surface::Menubar => SnapshotSurface::Menubar,
- Surface::Sheet => SnapshotSurface::Sheet,
- Surface::Popover => SnapshotSurface::Popover,
- Surface::Alert => SnapshotSurface::Alert,
- }
-}