Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

3.9 KB
Raw Blame
"""The area picker for region appshots — an isolated PySide6 sidecar.

``python -m dodaios.appshot.picker --lang de`` dims every screen with the
marking toolbar already up, lets the user mark the screen and drag one
rectangle (in either order) and prints the result as one JSON line on
stdout, then exits. The main process never
imports PySide6 (AP-26); it talks to this process through
:mod:`dodaios.appshot.region`.

With ``--resident``, it starts in pixel-free standby. Each ``start`` command
opens a fresh selection; finishing releases its windows and pixels while
keeping Qt loaded. ``quit`` or parent EOF exits the process.

Wire format (one JSON object per line):

stdout::

    {"event": "standby"}                                # resident runtime loaded
    {"event": "ready"}                                  # overlay is up
    {"event": "marking"}                                # something is marked; the
                                                        # parent drops its global Esc
    {"event": "option", "keep_area": true}              # "adjust before taking"
                                                        # flipped; the parent saves it
    {"event": "selection", "screen": {"x":…, "y":…,     # the Qt screen,
     "w":…, "h":…, "dpr":…}, "rect": [fx, fy, fw, fh],  # fractions of it,
     "action": "done" | "copy" | "save" | "edit",       # what the user pressed,
     "markup": {"overlay": "<b64 PNG>",                 # markings (optional):
                "hides": [{"kind": "blur" | "pixelate", # transparent overlay of
                           "rect": [fx, fy, fw, fh]}]}} # the area + hide patches
    {"event": "selection", "cancelled": true}           # Esc / right-click

stdin::

    {"cmd": "start", "language": "en", "monitors": [],   # resident: fresh capture
     "windows": [], "handles": [],                      # (handles: hwnds beside the
     "keep_area": false, "shield": false}               # window rects; keep the area;
                                                        # shield: hidden from screen
                                                        # captures until "unshield")
    {"cmd": "unshield"}                                 # the parent froze the screens
    {"cmd": "quit"}                                     # release the runtime
    {"cmd": "layout", "monitors": [...],                # mss monitors and the
     "windows": [[l, t, w, h], ...], "handles": [...]}  # snap targets, top first
    {"cmd": "cancel"}                                   # e.g. a global Esc

Stdin EOF (the parent died) cancels as well.
"""

from __future__ import annotations

import json
from typing import Any

#: Exit code when no usable GUI stack exists (PySide6 missing, no display).
EXIT_NO_GUI = 3

EVENT_READY = "ready"
EVENT_STANDBY = "standby"
EVENT_MARKING = "marking"
EVENT_SELECTION = "selection"
EVENT_OPTION = "option"
#: What finishing the selection asks for besides taking the appshot.
ACTION_DONE = "done"
ACTION_COPY = "copy"
ACTION_SAVE = "save"
ACTION_EDIT = "edit"
ACTIONS = (ACTION_DONE, ACTION_COPY, ACTION_SAVE, ACTION_EDIT)
CMD_CANCEL = "cancel"
CMD_LAYOUT = "layout"
CMD_START = "start"
CMD_QUIT = "quit"
CMD_UNSHIELD = "unshield"


def encode(payload: dict[str, Any]) -> str:
    return json.dumps(payload, ensure_ascii=False) + "\n"


def decode(line: str) -> dict[str, Any] | None:
    """One parsed line, or ``None`` for blank or garbled input."""
    try:
        payload = json.loads(line.strip())
    except (ValueError, TypeError):  # a garbled picker line is skipped, not an error
        return None
    return payload if isinstance(payload, dict) else None


__all__ = [
    "ACTIONS",
    "ACTION_COPY",
    "ACTION_DONE",
    "ACTION_EDIT",
    "ACTION_SAVE",
    "CMD_CANCEL",
    "CMD_LAYOUT",
    "CMD_START",
    "CMD_QUIT",
    "CMD_UNSHIELD",
    "EVENT_MARKING",
    "EVENT_OPTION",
    "EVENT_READY",
    "EVENT_STANDBY",
    "EVENT_SELECTION",
    "EXIT_NO_GUI",
    "decode",
    "encode",
]