@octavus/agent 1.0.9 → 1.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -483,9 +483,54 @@ def enumerate_elements(pid=None):
483
483
  # Screenshot capture
484
484
  # ---------------------------------------------------------------------------
485
485
 
486
+ def _screenshot_tool_cmds(output_path):
487
+ """Full-display capture commands tried in order. scrot is the primary (present
488
+ on our images); the rest are fallbacks so a missing or broken scrot degrades
489
+ gracefully instead of blinding the loop. Each writes ``output_path`` directly."""
490
+ return (
491
+ ["scrot", "-o", output_path],
492
+ ["maim", output_path],
493
+ ["import", "-window", "root", output_path],
494
+ ["gnome-screenshot", "-f", output_path],
495
+ )
496
+
497
+
498
+ def _pillow_x11_grab(output_path):
499
+ """Last-resort capture via Pillow's native X11 grab (no external tool). Returns
500
+ True on a non-empty PNG, else False."""
501
+ try:
502
+ from PIL import ImageGrab
503
+
504
+ image = ImageGrab.grab(xdisplay=os.environ.get("DISPLAY") or ":0")
505
+ image.save(output_path, "PNG")
506
+ return os.path.exists(output_path) and os.path.getsize(output_path) > 0
507
+ except Exception:
508
+ return False
509
+
510
+
486
511
  def capture_screenshot(output_path):
487
- """Take a screenshot using scrot (X11)."""
488
- _run(["scrot", "-o", output_path])
512
+ """Take a full-display screenshot (X11).
513
+
514
+ Prefers scrot; falls back to other common capture tools, then Pillow's native
515
+ X11 grab, so a missing or failing scrot (e.g. it was never installed, or crashed
516
+ with a libpng write error on a full disk) degrades gracefully instead of aborting
517
+ the run with "Required command not found: scrot".
518
+ """
519
+ for cmd in _screenshot_tool_cmds(output_path):
520
+ if shutil.which(cmd[0]) is None:
521
+ continue
522
+ try:
523
+ subprocess.run(cmd, capture_output=True, text=True, timeout=20, check=True)
524
+ except (subprocess.CalledProcessError, subprocess.TimeoutExpired, OSError):
525
+ continue
526
+ if os.path.exists(output_path) and os.path.getsize(output_path) > 0:
527
+ return
528
+ if _pillow_x11_grab(output_path):
529
+ return
530
+ exit_with_error(
531
+ "Screenshot failed: no working capture tool. Install one of scrot, maim, "
532
+ "imagemagick (import), or gnome-screenshot."
533
+ )
489
534
 
490
535
 
491
536
  def crop_screenshot(screenshot_path, region):
@@ -853,11 +898,25 @@ def _resolve_from_desktop_files(name):
853
898
  return binaries, desktop_ids
854
899
 
855
900
 
901
+ # WM_CLASS regex fragments for apps whose window title does not match the app name
902
+ # the agent asks to activate - notably Chrome `--app=<url>` windows, whose title is
903
+ # the page's, not "chrome". "chrome" matches the "google-chrome" class; "chromium"
904
+ # is listed explicitly since it does not contain "chrome".
905
+ _WINDOW_CLASS_ALIASES = {
906
+ "chrome": ["chrome", "chromium"],
907
+ "google chrome": ["chrome", "chromium"],
908
+ "google-chrome": ["chrome", "chromium"],
909
+ "chromium": ["chromium", "chrome"],
910
+ "browser": ["chrome", "chromium", "firefox"],
911
+ "firefox": ["firefox"],
912
+ }
913
+
914
+
856
915
  def activate_app(name):
857
- def find_windows(query):
916
+ def find_windows(flag, query):
858
917
  try:
859
918
  r = subprocess.run(
860
- ["xdotool", "search", "--name", query],
919
+ ["xdotool", "search", flag, query],
861
920
  capture_output=True, text=True, timeout=5,
862
921
  )
863
922
  return [wid for wid in r.stdout.strip().split("\n") if wid]
@@ -865,8 +924,14 @@ def activate_app(name):
865
924
  return []
866
925
 
867
926
  def activate_existing_window():
868
- for query in (name, name.lower()):
869
- wids = find_windows(query)
927
+ # Match an already-open window by title (--name) and by window class
928
+ # (--class). A Chrome `--app=<url>` window carries the page title, not the
929
+ # app name, so a title-only search misses it; the class aliases below focus
930
+ # the running browser window instead of launching a second, duplicate one.
931
+ attempts = [("--name", name), ("--name", name.lower())]
932
+ attempts += [("--class", cls) for cls in _WINDOW_CLASS_ALIASES.get(name.lower(), [])]
933
+ for flag, query in attempts:
934
+ wids = find_windows(flag, query)
870
935
  if wids:
871
936
  # No --sync: `windowactivate --sync` blocks until the WM confirms
872
937
  # activation, which never arrives for a borderless/maximized