Skip to content

Commit dbe58cc

Browse files
committed
feat: add PyAutoGUI backend, MIT license, fix README consistency
- Add PyAutoGUIBackend as default backend (fallback when Hermes computer_use unavailable) - Update _discover() to auto-register PyAutoGUIBackend if pyautogui is installed - Update _backend_error message to mention both Hermes and pyautogui options - Add MIT LICENSE file - Update README desktop control note to reflect pluggable backend architecture - Update test assertion to match new error message
1 parent a937ddc commit dbe58cc

4 files changed

Lines changed: 227 additions & 15 deletions

File tree

‎LICENSE‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2026 nextjohnp7
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Controls any desktop application through screenshots, mouse clicks, keyboard inp
6565

6666
Risky actions (`click`, `type`, `key`, `drag`) require confirmation (`y/n`) unless `--confirm` is passed. All desktop actions are recorded in the activity log.
6767

68-
**Note:** Desktop control requires the Hermes agent environment (the `computer_use` tool is only available through the Hermes agent). Running `deskbridge.py desktop ...` outside the Hermes agent will return an error explaining this requirement.
68+
**Note:** Desktop control works with any registered backend. By default, DeskBridge tries the Hermes `computer_use` backend (when running inside the Hermes agent) and falls back to the PyAutoGUI backend (if `pip install pyautogui` has been done). If no backend is available, commands return a clear error explaining how to install one.
6969

7070
## Pluggable desktop backends
7171

‎core/desktop_control.py‎

Lines changed: 195 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,190 @@ def focus_app(self, app: str, raise_window: bool = False) -> dict:
351351
}
352352

353353

354+
class PyAutoGUIBackend(DesktopBackend):
355+
"""Backend that wraps the PyAutoGUI Python package.
356+
357+
Available cross-platform without requiring the Hermes agent context.
358+
Requires: pip install pyautogui
359+
"""
360+
361+
name = "pyautogui"
362+
363+
def _require_pyautogui(self) -> dict:
364+
try:
365+
import pyautogui # noqa: F401
366+
except ImportError:
367+
return {
368+
"ok": False,
369+
"error": "pyautogui not installed",
370+
"message": "Install pyautogui to use the PyAutoGUI backend",
371+
}
372+
return {"ok": True}
373+
374+
def capture(self, app: str | None = None, mode: str = "som") -> dict:
375+
check = self._require_pyautogui()
376+
if not check["ok"]:
377+
return check
378+
try:
379+
import pyautogui
380+
import tempfile
381+
screenshot = pyautogui.screenshot()
382+
tmp = tempfile.NamedTemporaryFile(suffix=".png", delete=False, dir=os.getcwd())
383+
screenshot.save(tmp.name)
384+
return {
385+
"ok": True,
386+
"message": f"Captured screenshot: {tmp.name}",
387+
"screenshot_path": tmp.name,
388+
"element_count": None,
389+
"app": app,
390+
}
391+
except Exception as exc:
392+
return {"ok": False, "error": str(exc), "app": app}
393+
394+
def click(
395+
self,
396+
element: int | None = None,
397+
coordinate: list[int] | None = None,
398+
app: str | None = None,
399+
button: str = "left",
400+
modifiers: list[str] | None = None,
401+
delivery_mode: str = "background",
402+
) -> dict:
403+
check = self._require_pyautogui()
404+
if not check["ok"]:
405+
return check
406+
try:
407+
import pyautogui
408+
if coordinate:
409+
x, y = coordinate
410+
pyautogui.click(x=x, y=y, button=button)
411+
return {"ok": True, "message": f"Clicked at {coordinate}", "app": app}
412+
return {
413+
"ok": False,
414+
"error": "PyAutoGUI backend requires --coordinate, not --element",
415+
"app": app,
416+
}
417+
except Exception as exc:
418+
return {"ok": False, "error": str(exc), "app": app}
419+
420+
def type_text(
421+
self, text: str, app: str | None = None, delivery_mode: str = "background"
422+
) -> dict:
423+
check = self._require_pyautogui()
424+
if not check["ok"]:
425+
return check
426+
try:
427+
import pyautogui
428+
pyautogui.write(text)
429+
return {"ok": True, "message": "Typed text", "app": app}
430+
except Exception as exc:
431+
return {"ok": False, "error": str(exc), "app": app}
432+
433+
def press_keys(
434+
self, keys: str, app: str | None = None, delivery_mode: str = "background"
435+
) -> dict:
436+
check = self._require_pyautogui()
437+
if not check["ok"]:
438+
return check
439+
try:
440+
import pyautogui
441+
key_list = keys.replace("cmd", "command").split("+")
442+
pyautogui.hotkey(*key_list)
443+
return {"ok": True, "message": f"Pressed keys: {keys}", "app": app}
444+
except Exception as exc:
445+
return {"ok": False, "error": str(exc), "app": app}
446+
447+
def scroll(
448+
self,
449+
direction: str,
450+
amount: int = 3,
451+
app: str | None = None,
452+
delivery_mode: str = "background",
453+
) -> dict:
454+
check = self._require_pyautogui()
455+
if not check["ok"]:
456+
return check
457+
try:
458+
import pyautogui
459+
scroll_amount = amount if direction == "up" else -amount
460+
pyautogui.scroll(scroll_amount)
461+
return {
462+
"ok": True,
463+
"message": f"Scrolled {direction} ({amount} ticks)",
464+
"app": app,
465+
}
466+
except Exception as exc:
467+
return {"ok": False, "error": str(exc), "app": app}
468+
469+
def drag(
470+
self,
471+
from_element: int | None = None,
472+
from_coordinate: list[int] | None = None,
473+
to_element: int | None = None,
474+
to_coordinate: list[int] | None = None,
475+
app: str | None = None,
476+
delivery_mode: str = "background",
477+
) -> dict:
478+
check = self._require_pyautogui()
479+
if not check["ok"]:
480+
return check
481+
try:
482+
import pyautogui
483+
if from_coordinate and to_coordinate:
484+
x1, y1 = from_coordinate
485+
x2, y2 = to_coordinate
486+
pyautogui.moveTo(x1, y1)
487+
pyautogui.dragTo(x2, y2, button="left")
488+
return {"ok": True, "message": "Dragged", "app": app}
489+
return {
490+
"ok": False,
491+
"error": "PyAutoGUI backend requires --from-coordinate and --to-coordinate",
492+
"app": app,
493+
}
494+
except Exception as exc:
495+
return {"ok": False, "error": str(exc), "app": app}
496+
497+
def list_apps(self) -> dict:
498+
check = self._require_pyautogui()
499+
if not check["ok"]:
500+
return check
501+
try:
502+
import pyautogui
503+
active = pyautogui.getActiveWindowTitle()
504+
return {
505+
"ok": True,
506+
"message": f"Active window: {active or 'unknown'}",
507+
"apps": [active] if active else [],
508+
}
509+
except Exception as exc:
510+
return {"ok": False, "error": str(exc)}
511+
512+
def list_windows(self) -> dict:
513+
check = self._require_pyautogui()
514+
if not check["ok"]:
515+
return check
516+
try:
517+
import pyautogui
518+
active = pyautogui.getActiveWindowTitle()
519+
return {
520+
"ok": True,
521+
"message": f"Active window: {active or 'unknown'}",
522+
"windows": [active] if active else [],
523+
}
524+
except Exception as exc:
525+
return {"ok": False, "error": str(exc)}
526+
527+
def focus_app(self, app: str, raise_window: bool = False) -> dict:
528+
check = self._require_pyautogui()
529+
if not check["ok"]:
530+
return check
531+
return {
532+
"ok": False,
533+
"error": "focus_app not supported by PyAutoGUI backend",
534+
"app": app,
535+
}
536+
537+
354538
class DesktopController:
355539
"""Manages desktop backends and routes calls to the first available one."""
356540

@@ -363,7 +547,7 @@ def _discover(self) -> None:
363547
"""Auto-detect available backends.
364548
365549
Order matters: prefer Hermes computer_use when running inside the
366-
Hermes agent; fall back to other backends as they are implemented.
550+
Hermes agent; fall back to PyAutoGUI when installed.
367551
"""
368552
try:
369553
from hermes_tools import tool_call # noqa: F401 (availability check)
@@ -372,6 +556,13 @@ def _discover(self) -> None:
372556
except ImportError:
373557
pass
374558

559+
try:
560+
import pyautogui # noqa: F401 (availability check)
561+
562+
self.backends.append(PyAutoGUIBackend())
563+
except ImportError:
564+
pass
565+
375566
if self.backends:
376567
self.active = self.backends[0]
377568

@@ -396,9 +587,9 @@ def _backend_error(
396587
"ok": False,
397588
"error": "No desktop backend available",
398589
"message": (
399-
"Desktop control requires the Hermes agent environment or "
400-
"a registered desktop backend. Install Hermes Agent or add "
401-
"a DesktopBackend subclass (e.g. MCP backend)."
590+
"No desktop backend is available. Install Hermes Agent (for the "
591+
"computer_use backend) or install pyautogui (for the PyAutoGUI "
592+
"backend), or register a custom DesktopBackend subclass."
402593
),
403594
"app": app_name,
404595
}

‎tests/test_discovery.py‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,41 +94,41 @@ def test_desktop_control_requires_hermes_agent():
9494
# All functions should return ok=False with a clear error message
9595
result = capture()
9696
assert result["ok"] is False
97-
assert "Hermes agent environment" in result.get("message", "")
97+
assert "No desktop backend is available" in result.get("message", "")
9898

9999
result = click(element=1)
100100
assert result["ok"] is False
101-
assert "Hermes agent environment" in result.get("message", "")
101+
assert "No desktop backend is available" in result.get("message", "")
102102

103103
result = type_text("hello")
104104
assert result["ok"] is False
105-
assert "Hermes agent environment" in result.get("message", "")
105+
assert "No desktop backend is available" in result.get("message", "")
106106

107107
result = press_keys("return")
108108
assert result["ok"] is False
109-
assert "Hermes agent environment" in result.get("message", "")
109+
assert "No desktop backend is available" in result.get("message", "")
110110

111111
result = scroll("down")
112112
assert result["ok"] is False
113-
assert "Hermes agent environment" in result.get("message", "")
113+
assert "No desktop backend is available" in result.get("message", "")
114114

115115
result = drag(from_element=1, to_element=2)
116116
assert result["ok"] is False
117-
assert "Hermes agent environment" in result.get("message", "")
117+
assert "No desktop backend is available" in result.get("message", "")
118118

119119
result = list_apps()
120120
assert result["ok"] is False
121-
assert "Hermes agent environment" in result.get("message", "")
121+
assert "No desktop backend is available" in result.get("message", "")
122122

123123
result = list_windows()
124124
assert result["ok"] is False
125-
assert "Hermes agent environment" in result.get("message", "")
125+
assert "No desktop backend is available" in result.get("message", "")
126126

127127
result = focus_app("TestApp")
128128
assert result["ok"] is False
129-
assert "Hermes agent environment" in result.get("message", "")
129+
assert "No desktop backend is available" in result.get("message", "")
130130

131-
print("Desktop control correctly requires Hermes agent environment")
131+
print("Desktop control correctly reports no backend available")
132132

133133

134134
if __name__ == "__main__":

0 commit comments

Comments
 (0)