# macOS Computer-Use Agent: Multi-Layered Desktop Automation An intelligent agentic framework that controls native macOS desktop applications. Powered by `cua-driver` accessibility primitives and an LLM-driven orchestration loop, it implements a cost-efficient **5-layer cascade** to solve tasks with minimal API token usage and fallback to vision-only control when needed. --- ## 📺 Demo Video Click the preview below to watch the agent operating live with the **agent-cursor overlay** enabled as it performs the notes task: [![YouTube Demo Video](https://img.youtube.com/vi/2tvCjHqpUJw/0.jpg)](https://www.youtube.com/watch?v=2tvCjHqpUJw) --- ## 🧱 Architecture: The 5-Layer Cascade The agent does not blindly use expensive Multimodal Vision Models. Instead, it processes goals through a 5-layer execution cascade, stopping at the first layer that can successfully complete the task: ``` [ User Subgoal ] │ ┌────────────────┴────────────────┐ ▼ ▼ [ Mutation Verb? ] [ Read-Only Verb? ] │ │ │ No │ Yes ▼ ▼ ┌───────────────┐ ┌───────────────┐ │ Layer 1 │ │ Layer 1 │ │ (Bypassed) │ │ ax_extract │ ──► Success? ──► [Done] └───────┬───────┘ └───────┬───────┘ │ │ No └────────────────┬────────────────┘ ▼ ┌───────────────┐ │ Layer 2a │ │ deterministic │ ──► Success? ──► [Done] └───────┬───────┘ │ No ▼ [ Electron App? ] ┌───────┴───────────────┐ No │ │ Yes ┌────────┴─────────────────────────┐ ▼ ▼ ┌───────────────┐ ┌───────────────┐ │ Layer 2b │ │ Layer 2c │ │ ax_llm │ │ electron/CDP │ └───────┬───────┘ └───────┬───────┘ │ │ └────────┬───────────────┬──────────────┘ ▼ (Failure / AX-Empty / Canvas) ┌───────────────┐ │ Layer 3 │ │ vision (VLM) │ ──► Success? ──► [Done] └───────────────┘ ``` ### 1. Layer 1: Accessibility Extract (`ax_extract`) * **Mechanism:** Reads the native Accessibility (AX) tree state directly from macOS. * **Cost:** **Zero LLM tokens / Zero Vision cost**. * **Use Case:** Triggered only for pure read-only queries (e.g., *"What is the text in Notes?"* or *"List window titles"*). If the goal contains mutation actions (e.g., click, type, drag), this layer is automatically bypassed. ### 2. Layer 2a: Deterministic Sequences (`deterministic`) * **Mechanism:** Executes pre-defined keystrokes, shortcuts, or element-index clicks directly. * **Cost:** **Zero LLM tokens / Zero Vision cost**. * **Use Case:** Standard bootstrap actions (e.g., sending `Cmd+N` to open a new file) or hardcoded UI workflows where indices are resolved programmatically. ### 3. Layer 2b: Accessibility + Text LLM (`ax_llm`) * **Mechanism:** Parses the AX tree into structured Markdown, tagging interactive elements with turn-scoped `[element_index N]`. A text LLM (Gemini Flash-Lite) reasons over the tree and outputs JSON actions referencing indices. * **Cost:** Cheap text-only LLM input/output. * **Use Case:** The core workhorse for native apps. Re-scans the tree after every step to handle shifting indices due to UI reflow. ### 4. Layer 2c: Electron DOM Control (`electron`) * **Mechanism:** Bypasses macOS accessibility tree limitations for Electron apps (e.g., VS Code, Notion, Slack) by launching them with an active `electron_debugging_port` and using Chrome DevTools Protocol (CDP) to drive the DOM. * **Cost:** Cheap text-only LLM reasoning over page HTML. * **Use Case:** Complex web-tech desktop apps. Also uses system clipboard integration (`pbcopy` + `Cmd+V`) for fast, robust document writing. ### 5. Layer 3: Multimodal Vision fallback (`vision`) * **Mechanism:** Takes a screenshot, applies a Set-of-Marks (SoM) bounding-box overlay using AX coordinates, and feeds the image to a Vision-Language Model (Gemini Flash). The VLM outputs window-relative pixel coordinates `(x, y)` for clicks or drags. * **Cost:** **Highest cost (~10x Layer 2b)**. * **Use Case:** Canvas elements (e.g., drawing in paint/graphics tools), games, or applications that block accessibility tree inspection. --- ## 🏃‍♂️ The Three Tasks ### Task A: Notes * **Goal:** Open notes and create a new note with title and body [session artifacts here](./agent/others/s8-317576df) ### Task B: Describe the image in Inkscape canvas * **Goal:** To describe the image opened in Inkscape canvas [session artifacts here](./agent/others/s8-753b320c) ### Task C: Electron App * **Goal:** Open notion and type hello [session artifacts here](./agent/others/s8-71b5b818) --- ## 🛠️ Getting Started ### 1. Prerequisites Start the `cua-driver` daemon on your Mac and grant accessibility + screen recording permissions: ```bash # Start the daemon open -n -g -a CuaDriver --args serve # Grant TCC permissions cua-driver permissions grant ``` ### 2. Running Tasks Ensure you have the V9 Gateway running locally on port `8109`, then execute the task scripts: ```bash # Run Task A (Calculator) python -m computer.tasks.task_calculator # Run Task B (Notes) python -m computer.tasks.task_notes # Run Task C (VS Code - Electron path) python -m computer.tasks.task_vscode --electron # Run Task C (VS Code - Vision path) python -m computer.tasks.task_vscode --vision ```