mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-04-30 01:41:43 +00:00
Extends the cua-driver computer-use backend to drive backgrounded macOS windows without stealing keyboard or mouse focus from the foreground app. All changes target the cua-driver MCP backend and the shared dispatcher. ## cua_backend.py **Window-aware capture**: capture() now calls list_windows + get_window_state instead of the removed capture tool. Prefers structuredContent.windows (MCP 2024-11-05+ cua-driver) for zero-parse window enumeration; falls back to regex-parsed text for older builds. Stores the selected (pid, window_id) as sticky context so subsequent action calls do not need a redundant round-trip. **Action routing**: click/scroll/type_text/key all carry the sticky pid (and window_id for element-indexed clicks). type_text routes through type_text_chars (individual key events) rather than AX attribute write -- WebKit AXTextFields reject attribute writes from backgrounded processes. **Key parsing**: _parse_key_combo splits cmd+s-style strings into (key, [modifiers]) and routes to hotkey (modifier present) or press_key (bare key) -- cua-driver actual tool names. **set_value method**: new set_value(value, element) calls the cua-driver set_value MCP tool. For AXPopUpButton / HTML select in a backgrounded Safari, AXPress opens the native macOS popup which closes immediately when the app is non-frontmost; set_value AX-presses the matching child option directly (no menu required, no focus steal). **focus_app**: reimplemented as a pure window-selector (enumerates list_windows, sets sticky pid/window_id) without ever raising the window or stealing focus. **list_apps**: fixed tool name from listApps to list_apps; handles plain-text response via regex when structured data is absent. **Structured-content extraction**: _extract_tool_result now surfaces structuredContent from MCP results, enabling the list_windows window array without text parsing. **Helpers**: _parse_windows_from_text, _parse_elements_from_tree, _split_tree_text, _parse_key_combo extracted as module-level functions. ## schema.py Added set_value to the action enum with a description explaining when to prefer it over click (select/popup elements, sliders, no focus steal). Added value field for set_value payloads. ## tool.py Routed set_value action through _dispatch to backend.set_value. Added set_value to _DESTRUCTIVE_ACTIONS (approval-gated). Fixed MIME-type detection in _capture_response: cua-driver may return JPEG; detect from base64 magic bytes (/9j/ -> image/jpeg, else image/png) rather than hardcoding image/png. ## agent/display.py + run_agent.py Guard _detect_tool_failure and result-preview logic against non-string function_result values: multimodal tool results (dicts with _multimodal=True) are not string-sliceable; treat them as successes and fall back to str() for length/preview.
191 lines
8.5 KiB
Python
191 lines
8.5 KiB
Python
"""Schema for the generic `computer_use` tool.
|
|
|
|
Model-agnostic. Any tool-calling model can drive this. Vision-capable models
|
|
should prefer `capture(mode='som')` then `click(element=N)` — much more
|
|
reliable than pixel coordinates. Pixel coordinates remain supported for
|
|
models that were trained on them (e.g. Claude's computer-use RL).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any, Dict
|
|
|
|
|
|
# One consolidated tool with an `action` discriminator. Keeps the schema
|
|
# compact and the per-turn token cost low.
|
|
COMPUTER_USE_SCHEMA: Dict[str, Any] = {
|
|
"name": "computer_use",
|
|
"description": (
|
|
"Drive the macOS desktop in the background — screenshots, mouse, "
|
|
"keyboard, scroll, drag — without stealing the user's cursor, "
|
|
"keyboard focus, or Space. Preferred workflow: call with "
|
|
"action='capture' (mode='som' gives numbered element overlays), "
|
|
"then click by `element` index for reliability. Pixel coordinates "
|
|
"are supported for models trained on them. Works on any window — "
|
|
"hidden, minimized, on another Space, or behind another app. "
|
|
"macOS only; requires cua-driver to be installed."
|
|
),
|
|
"parameters": {
|
|
"type": "object",
|
|
"properties": {
|
|
"action": {
|
|
"type": "string",
|
|
"enum": [
|
|
"capture",
|
|
"click",
|
|
"double_click",
|
|
"right_click",
|
|
"middle_click",
|
|
"drag",
|
|
"scroll",
|
|
"type",
|
|
"key",
|
|
"set_value",
|
|
"wait",
|
|
"list_apps",
|
|
"focus_app",
|
|
],
|
|
"description": (
|
|
"Which action to perform. `capture` is free (no side "
|
|
"effects). All other actions require approval unless "
|
|
"auto-approved. Use `set_value` for select/popup elements "
|
|
"and sliders — it selects the matching option directly "
|
|
"without opening the native menu (no focus steal)."
|
|
),
|
|
},
|
|
# ── capture ────────────────────────────────────────────
|
|
"mode": {
|
|
"type": "string",
|
|
"enum": ["som", "vision", "ax"],
|
|
"description": (
|
|
"Capture mode. `som` (default) is a screenshot with "
|
|
"numbered overlays on every interactable element plus "
|
|
"the AX tree — best for vision models, lets you click "
|
|
"by element index. `vision` is a plain screenshot. "
|
|
"`ax` is the accessibility tree only (no image; useful "
|
|
"for text-only models)."
|
|
),
|
|
},
|
|
"app": {
|
|
"type": "string",
|
|
"description": (
|
|
"Optional. Limit capture/action to a specific app "
|
|
"(by name, e.g. 'Safari', or bundle ID, "
|
|
"'com.apple.Safari'). If omitted, operates on the "
|
|
"frontmost app's window or the whole screen."
|
|
),
|
|
},
|
|
# ── click / drag / scroll targeting ────────────────────
|
|
"element": {
|
|
"type": "integer",
|
|
"description": (
|
|
"The 1-based SOM index returned by the last "
|
|
"`capture(mode='som')` call. Strongly preferred over "
|
|
"raw coordinates."
|
|
),
|
|
},
|
|
"coordinate": {
|
|
"type": "array",
|
|
"items": {"type": "integer"},
|
|
"minItems": 2,
|
|
"maxItems": 2,
|
|
"description": (
|
|
"Pixel coordinates [x, y] in logical screen space (as "
|
|
"returned by capture width/height). Only use this if "
|
|
"no element index is available."
|
|
),
|
|
},
|
|
"button": {
|
|
"type": "string",
|
|
"enum": ["left", "right", "middle"],
|
|
"description": "Mouse button. Defaults to left.",
|
|
},
|
|
"modifiers": {
|
|
"type": "array",
|
|
"items": {
|
|
"type": "string",
|
|
"enum": ["cmd", "shift", "option", "alt", "ctrl", "fn"],
|
|
},
|
|
"description": "Modifier keys held during the action.",
|
|
},
|
|
# ── drag ───────────────────────────────────────────────
|
|
"from_element": {"type": "integer",
|
|
"description": "Source element index (drag)."},
|
|
"to_element": {"type": "integer",
|
|
"description": "Target element index (drag)."},
|
|
"from_coordinate": {
|
|
"type": "array",
|
|
"items": {"type": "integer"},
|
|
"minItems": 2, "maxItems": 2,
|
|
"description": "Source [x,y] (drag; use when no element available).",
|
|
},
|
|
"to_coordinate": {
|
|
"type": "array",
|
|
"items": {"type": "integer"},
|
|
"minItems": 2, "maxItems": 2,
|
|
"description": "Target [x,y] (drag; use when no element available).",
|
|
},
|
|
# ── scroll ─────────────────────────────────────────────
|
|
"direction": {
|
|
"type": "string",
|
|
"enum": ["up", "down", "left", "right"],
|
|
"description": "Scroll direction.",
|
|
},
|
|
"amount": {
|
|
"type": "integer",
|
|
"description": "Scroll wheel ticks. Default 3.",
|
|
},
|
|
# ── set_value ──────────────────────────────────────────
|
|
"value": {
|
|
"type": "string",
|
|
"description": (
|
|
"For action='set_value': the value to set on the element. "
|
|
"For AXPopUpButton / select dropdowns, pass the option's "
|
|
"display label (e.g. 'Blue'). For sliders and other "
|
|
"AXValue-settable elements, pass the numeric or string value."
|
|
),
|
|
},
|
|
# ── type / key / wait ──────────────────────────────────
|
|
"text": {
|
|
"type": "string",
|
|
"description": "Text to type (respects the current layout).",
|
|
},
|
|
"keys": {
|
|
"type": "string",
|
|
"description": (
|
|
"Key combo, e.g. 'cmd+s', 'ctrl+alt+t', 'return', "
|
|
"'escape', 'tab'. Use '+' to combine."
|
|
),
|
|
},
|
|
"seconds": {
|
|
"type": "number",
|
|
"description": "Seconds to wait. Max 30.",
|
|
},
|
|
# ── focus_app ──────────────────────────────────────────
|
|
"raise_window": {
|
|
"type": "boolean",
|
|
"description": (
|
|
"Only for action='focus_app'. If true, brings the "
|
|
"window to front (DISRUPTS the user). Default false "
|
|
"— input is routed to the app without raising, "
|
|
"matching the background co-work model."
|
|
),
|
|
},
|
|
# ── return shape ───────────────────────────────────────
|
|
"capture_after": {
|
|
"type": "boolean",
|
|
"description": (
|
|
"If true, take a follow-up capture after the action "
|
|
"and include it in the response. Saves a round-trip "
|
|
"when you need to verify an action's effect."
|
|
),
|
|
},
|
|
},
|
|
"required": ["action"],
|
|
},
|
|
}
|
|
|
|
|
|
def get_computer_use_schema() -> Dict[str, Any]:
|
|
"""Return the generic OpenAI function-calling schema."""
|
|
return COMPUTER_USE_SCHEMA
|