thumbnail.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. """Thumbnail of a bot's screen: one JPEG grab of the profile's Xvnc display.
  2. Feeds the Screen hero in Hermes Desktop (the big preview at the top of a bot's pane). Read-only:
  3. it never touches the lease, so a human in control is not disturbed and the bot is not blocked.
  4. """
  5. from __future__ import annotations
  6. import base64
  7. import io
  8. import os
  9. import threading
  10. from typing import Optional
  11. from tools.bot_desktop import runtime
  12. THUMB_MAX = (960, 600)
  13. _grab_lock = threading.Lock()
  14. def thumbnail_data_url(max_size: tuple[int, int] = THUMB_MAX, quality: int = 72) -> Optional[str]:
  15. """``data:image/jpeg;base64,...`` of the running screen, or ``None`` when no screen is up."""
  16. env = runtime.published_env()
  17. display = env.get("DISPLAY")
  18. if not display:
  19. return None
  20. if runtime.sandbox_screen_running():
  21. from tools.bot_desktop import sandbox_host
  22. sandbox = runtime._owned_sandbox_env()
  23. jpeg = sandbox_host.grab_jpeg(sandbox, env, max_size, quality) if sandbox is not None else None
  24. return "data:image/jpeg;base64," + base64.b64encode(jpeg).decode("ascii") if jpeg else None
  25. if runtime._launcher_pid() is None:
  26. return None
  27. from PIL import ImageGrab # Pillow is a hard dependency; import lazily to keep status calls cheap
  28. # Xlib reads XAUTHORITY from the process env; the launcher publishes a per-profile cookie file.
  29. # The swap is process-wide, so two profiles grabbed on worker threads at once serialise here or
  30. # one would grab with the other's cookie and restore the wrong value.
  31. with _grab_lock:
  32. previous = os.environ.get("XAUTHORITY")
  33. if env.get("XAUTHORITY"):
  34. os.environ["XAUTHORITY"] = env["XAUTHORITY"]
  35. try:
  36. image = ImageGrab.grab(xdisplay=display)
  37. finally:
  38. if previous is None:
  39. os.environ.pop("XAUTHORITY", None)
  40. else:
  41. os.environ["XAUTHORITY"] = previous
  42. image.thumbnail(max_size)
  43. buf = io.BytesIO()
  44. image.convert("RGB").save(buf, "JPEG", quality=quality, optimize=True)
  45. return "data:image/jpeg;base64," + base64.b64encode(buf.getvalue()).decode("ascii")