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.

21.2 KB
Raw Blame
"""Take an appshot and put it where the conversation will see it.

One entry point for every trigger — the global shortcuts, the Appshots page's
test buttons, the live model's ``take_appshot`` tool — plus
:func:`record_turn_capture` for looks a conversation turn took itself. The
capture always runs through the shared Screen Context service, so the privacy
denylist, redaction, the pre-shutter indicator and the no-disk retention rule
apply unchanged; this module only decides what happens to the picture.
"""

from __future__ import annotations

import asyncio
import logging
import time
import uuid
from dataclasses import dataclass, replace
from typing import Any, Literal

from dodaios.appshot.store import Appshot, get_store

log = logging.getLogger(__name__)

Trigger = Literal["hotkey", "voice", "tool", "button"]
#: ``window``: the front window. ``region``: an area the user drags out first.
Scope = Literal["window", "region"]

#: Trusted framing in front of the untrusted screen evidence block.
APPSHOT_PREAMBLE = (
    "APPSHOT: a captured snapshot of the user's front window, supplied as context. "
    "It shows the window at capture time, not a live view. Use it when asked about "
    "this image. A request to take another appshot requires a fresh successful "
    "take_appshot call; this earlier image is not evidence of a new capture."
)


@dataclass(frozen=True, slots=True)
class AppshotResult:
    status: Literal["captured", "refused"]
    shot: Appshot | None = None
    message: str = ""
    reason_code: str = ""

    @property
    def ok(self) -> bool:
        return self.status == "captured" and self.shot is not None


def _load_config() -> Any:
    from dodaios.core.config import load_config  # noqa: PLC0415

    return load_config()


def shot_from_context(context: Any, *, trigger: str) -> Appshot:
    """Wrap a finished, redacted Screen Context capture as an appshot."""
    from dodaios.screen_context.turn import model_note  # noqa: PLC0415

    target = context.target
    if str(target.kind) == "window":
        label = "active window"
    elif str(target.kind) == "region":
        label = "selected area"
    else:
        label = f"monitor {target.monitor_name}" if target.monitor_name else "selected monitor"
    return Appshot(
        id=uuid.uuid4().hex,
        image=context.image,
        mime=context.mime,
        width=int(context.size[0]),
        height=int(context.size[1]),
        label=label,
        app_name=str(getattr(target.window, "app_name", "") or ""),
        note=f"{APPSHOT_PREAMBLE}\n{model_note(context)}",
        ui_text=context.ui_text,
        trigger=trigger,
        taken_at=time.time(),
        master=getattr(context, "master", None),
    )


async def take_appshot(
    *,
    trigger: Trigger,
    bus: Any | None = None,
    deliver: bool = True,
    trace_id: uuid.UUID | None = None,
    scope: Scope = "window",
) -> AppshotResult:
    """Capture once and deliver it per ``[appshot].target``.

    ``scope="window"`` takes the front window; ``scope="region"`` first lets
    the user drag out an area and mark it up in place
    (:mod:`dodaios.appshot.region`) and takes exactly that, with the markings
    burnt in — a cancelled selection is a refusal with
    ``reason_code="cancelled"``. Finishing the area with Copy, Save or Edit
    also copies, saves or opens the finished appshot.
    ``deliver=False`` is for a caller that consumes the picture itself (the
    live model's tool). Never raises; a refusal carries the user-facing reason.
    """
    from dodaios.screen_context.models import IntentVerdict, VisualIntent  # noqa: PLC0415
    from dodaios.screen_context.turn import get_service  # noqa: PLC0415

    selection = None
    try:
        config = await asyncio.to_thread(_load_config)
        if not config.screen_context.enabled:
            return AppshotResult(
                status="refused",
                reason_code="disabled",
                message="Appshots are switched off. Turn them on under Settings > Appshots.",
            )
        service = get_service(bus=bus)
        capture_trace_id = trace_id or uuid.uuid4()
        if scope == "region":
            # Refuse BEFORE the screens freeze when asking cannot help (a
            # Wayland portal, say): the frozen frame would be unusable and the
            # refusal would come only after the user had marked up a fake
            # screen. A missing Screen Recording grant is not refused here: the
            # picker asks macOS for it just in time before it starts.
            check = getattr(service, "capture_permission_issue", None)
            issue = await check() if check is not None else None
            if issue is not None and issue[0] != "capture_permission":
                return AppshotResult(status="refused", reason_code=issue[0], message=issue[1])
            # The picture is the screen at the press, as the picker shows it
            # frozen, not whatever a video has played on to by the time the
            # user finishes selecting. Without a live grant a frozen frame would
            # be wallpaper only, so the area is then grabbed live after the
            # picker has asked macOS just in time.
            frozen = None if issue is not None else await service.freeze_screens()
            picked = await _pick_area(
                service,
                _language(config),
                keep_area=bool(getattr(config.appshot, "region_keep_area", False)),
                trace_id=capture_trace_id,
            )
            if isinstance(picked, AppshotResult):
                return picked
            bbox, selection = picked
            from dodaios.appshot.effect import shutter_markup  # noqa: PLC0415

            # The corner card's thumbnail is cut at the shutter: give it the
            # markings too, so it shows what the assistant gets.
            token = shutter_markup.set(selection.markup)
            try:
                outcome = await service.capture(
                    verdict=IntentVerdict(intent=VisualIntent.SCREEN, evidence=("appshot-region",)),
                    trace_id=capture_trace_id,
                    region=bbox,
                    master=True,
                    frozen=frozen,
                )
            finally:
                shutter_markup.reset(token)
        else:
            outcome = await service.capture(
                verdict=IntentVerdict(intent=VisualIntent.WINDOW, evidence=("appshot",)),
                trace_id=capture_trace_id,
                master=True,
            )
        if outcome.status != "captured" or outcome.context is None:
            return AppshotResult(
                status="refused",
                reason_code=outcome.reason_code or outcome.reason_kind or "refused",
                message=outcome.message or "The appshot could not be taken.",
            )
        if outcome.handle_id:
            service.consume(outcome.handle_id)
        shot = shot_from_context(outcome.context, trigger=trigger)
        if selection is not None and selection.markup is not None:
            shot = await _with_markup(shot, selection.markup)
    except Exception:  # noqa: BLE001 - a shortcut press must never crash the app
        log.error("appshot: capture failed", exc_info=True)
        return AppshotResult(
            status="refused",
            reason_code="failure",
            message="The appshot failed. Nothing was captured.",
        )

    store = get_store()
    store.remember(shot, keep_s=float(config.screen_context.deck_preview_s))
    # The lossless copy encodes in the background: the model's picture and the
    # card must not wait a second for a 4K HDR PNG. It then goes to the library.
    _start_master(shot, selection.markup if selection is not None else None, config)
    await _attach_to_card(shot, config)
    delivered_to = "turn"
    if deliver:
        delivered_to = await _deliver(
            shot,
            target=str(config.appshot.target),
            ttl_s=float(config.screen_context.ttl_s),
        )
    store.mark_delivered(shot.id, delivered_to)
    await _publish(bus, shot, delivered_to)
    log.info(
        "appshot: %s %dx%d via %s -> %s",
        shot.label,
        shot.width,
        shot.height,
        trigger,
        delivered_to,
    )
    _start_clipboard_copy(
        shot, trigger, config, selection.action if selection is not None else "done"
    )
    if selection is not None and selection.action != "done":
        await _finish_action(selection.action, await finished(shot), bus)
    return AppshotResult(
        status="captured", shot=replace(shot, delivered_to=delivered_to, master=None)
    )


def _language(config: Any) -> str:
    return str(getattr(getattr(config, "ui", None), "language", "") or "en").lower()[:2]


async def _with_markup(shot: Appshot, markup: Any) -> Appshot:
    """Burn the markings the user drew in the picker into the finished appshot."""
    from dodaios.appshot.markup import apply_to_bytes  # noqa: PLC0415

    try:
        image = await asyncio.to_thread(apply_to_bytes, shot.image, shot.mime, markup)
        width, height = await asyncio.to_thread(_image_size, image)
    except Exception:  # noqa: BLE001 - the plain appshot is still worth sending
        log.warning("appshot: markings could not be applied; sent unmarked", exc_info=True)
        return shot
    # A background frame makes the picture larger than the area.
    return replace(
        shot, image=image, width=width, height=height, note=f"{shot.note}\n\n{EDIT_NOTE}"
    )


def _image_size(image: bytes) -> tuple[int, int]:
    import io  # noqa: PLC0415

    from PIL import Image  # noqa: PLC0415

    with Image.open(io.BytesIO(image)) as picture:
        return picture.size


#: Lossless copies still encoding, by appshot id.
_MASTERS: dict[str, asyncio.Task[Appshot]] = {}
#: Appshots the user took on purpose; a look the assistant or a spoken
#: "what do you see?" took never replaces what the user copied.
_CLIPBOARD_TRIGGERS = frozenset({"hotkey", "button"})
_COPIES: set[asyncio.Task[None]] = set()


def _start_master(shot: Appshot, markup: Any, config: Any) -> None:
    """Encode the lossless copy, swap it into the store, then keep it in the library."""
    if shot.master is None:
        task = asyncio.create_task(_keep_without_master(shot, config))
    else:
        task = asyncio.create_task(_encode_master(shot, markup, config))
    _MASTERS[shot.id] = task
    task.add_done_callback(lambda _done, shot_id=shot.id: _MASTERS.pop(shot_id, None))


async def _keep_without_master(shot: Appshot, config: Any) -> Appshot:
    await _keep_in_library(shot, config)
    return shot


async def _encode_master(shot: Appshot, markup: Any, config: Any) -> Appshot:
    from dodaios.appshot.master import encode_master  # noqa: PLC0415

    try:
        files = await asyncio.to_thread(encode_master, shot.master, markup)
        shot = replace(
            shot, original_png=files.png, hdr_png=files.hdr_png or b"", master=None
        )
    except Exception:  # noqa: BLE001 - the model's picture is still kept and shown
        log.warning("appshot: the lossless copy could not be made", exc_info=True)
        shot = replace(shot, master=None)
    store = get_store()
    store.attach_originals(shot)
    # A picture edited meanwhile keeps its edit; the library gets what is held.
    shot = store.get(shot.id) or shot
    await _keep_in_library(shot, config)
    return shot


async def finished(shot: Appshot) -> Appshot:
    """``shot`` with its lossless copy, waiting for it when it is still encoding."""
    task = _MASTERS.get(shot.id)
    if task is not None:
        try:
            return await asyncio.shield(task)
        except Exception:  # noqa: BLE001 - fall back to the picture we have
            log.debug("appshot: lossless copy unavailable", exc_info=True)
    held = get_store().get(shot.id)
    return held if held is not None else replace(shot, master=None)


def _start_clipboard_copy(shot: Appshot, trigger: str, config: Any, action: str) -> None:
    """Put a shortcut or button appshot on the clipboard, ready for Ctrl/Cmd+V.

    Read from ``[appshot].copy_to_clipboard``. The picker's own Copy already
    copies, so it is not done twice. Runs in the background: the full-quality
    picture may still be encoding, and the delivery must not wait for it.
    """
    if trigger not in _CLIPBOARD_TRIGGERS or action == "copy":
        return
    if not bool(getattr(config.appshot, "copy_to_clipboard", True)):
        return
    task = asyncio.create_task(_copy_to_clipboard(shot), name="appshot-clipboard")
    _COPIES.add(task)
    task.add_done_callback(_COPIES.discard)


async def _copy_to_clipboard(shot: Appshot) -> None:
    try:
        from dodaios.appshot.card_actions import as_png  # noqa: PLC0415
        from dodaios.platform.clipboard_image import copy_image  # noqa: PLC0415

        shot = await finished(shot)
        png = shot.original_png or await asyncio.to_thread(as_png, shot.image)
        result = await asyncio.to_thread(copy_image, png)
        if not result.ok:
            log.info("appshot: not copied to the clipboard (%s)", result.reason)
    except Exception:  # noqa: BLE001 - the appshot itself is taken and delivered
        log.warning("appshot: the clipboard copy failed", exc_info=True)


async def _finish_action(action: str, shot: Appshot, bus: Any | None) -> None:
    """Copy, save or open the appshot, as the picker's toolbar asked."""
    try:
        if action in ("copy", "save"):
            from dodaios.appshot.card_actions import (  # noqa: PLC0415
                as_png,
                save_shot_to_downloads,
            )

            if action == "copy":
                from dodaios.platform.clipboard_image import write_png  # noqa: PLC0415

                png = shot.original_png or await asyncio.to_thread(as_png, shot.image)
                if not await asyncio.to_thread(write_png, png):
                    log.warning("appshot: the area could not be copied to the clipboard")
            else:
                path = await asyncio.to_thread(save_shot_to_downloads, shot)
                log.info("appshot: area saved as %s", path.name)
        elif action == "edit":
            from dodaios.appshot.editor_window import open_editor_window  # noqa: PLC0415

            if await open_editor_window(shot.id) or bus is None:
                return
            from dodaios.core.events import (  # noqa: PLC0415
                AppshotEditRequested,
                ShowWindowRequested,
            )

            await bus.publish(AppshotEditRequested(source_layer="appshot", appshot_id=shot.id))
            await bus.publish(ShowWindowRequested(source_layer="appshot", source="appshot_picker"))
    except Exception:  # noqa: BLE001 - the appshot itself is taken and delivered
        log.warning("appshot: the picker's %r action failed", action, exc_info=True)


async def _pick_area(
    service: Any,
    language: str = "en",
    *,
    keep_area: bool = False,
    trace_id: uuid.UUID | None = None,
) -> tuple[Any, Any] | AppshotResult:
    """Run the area picker; ``(rectangle, selection)``, or the refusal to return."""
    from dodaios.appshot.region import (  # noqa: PLC0415
        RegionUnavailable,
        pick_region,
        selection_to_bbox,
    )
    from dodaios.platform.screen_access import ScreenCaptureRefused

    try:
        selection = await pick_region(language=language, keep_area=keep_area, trace_id=trace_id)
    except ScreenCaptureRefused as exc:
        log.info("appshot: area selection refused (%s)", exc.reason or "capture_permission")
        return AppshotResult(
            status="refused", reason_code="capture_permission", message=exc.user_detail,
        )
    except RegionUnavailable as exc:  # reported to the caller as a refused appshot
        return AppshotResult(status="refused", reason_code="region_unavailable", message=str(exc))
    if selection is None:
        return AppshotResult(
            status="refused", reason_code="cancelled", message="No area was selected."
        )
    monitors = await asyncio.to_thread(service.displays.monitors)
    bbox = selection_to_bbox(selection, monitors)
    if bbox is None:
        return AppshotResult(
            status="refused",
            reason_code="no_display",
            message="No screen could be found for the selected area.",
        )
    return bbox, selection


async def record_turn_capture(context: Any, *, bus: Any | None, trigger: Trigger) -> None:
    """A conversation turn looked at the screen itself — show it as an appshot."""
    try:
        config = await asyncio.to_thread(_load_config)
        shot = shot_from_context(context, trigger=trigger)
        store = get_store()
        store.remember(shot, keep_s=float(config.screen_context.deck_preview_s))
        await _attach_to_card(shot, config)
        store.mark_delivered(shot.id, "turn")
        await _publish(bus, shot, "turn")
    except Exception:  # noqa: BLE001 - the turn already has its picture
        log.warning("appshot: could not record the turn's capture", exc_info=True)


async def _keep_in_library(shot: Appshot, config: Any) -> None:
    """Write the appshot into the gallery's history when ``[appshot].library`` is on."""
    if not bool(getattr(config.appshot, "library", False)):
        return
    from dodaios.appshot import library, retention  # noqa: PLC0415

    if await asyncio.to_thread(library.save, shot):
        await asyncio.to_thread(retention.apply, retention.keep_newest(config))


async def keep_edit_in_library(shot: Appshot) -> None:
    """Keep a saved edit beside its original, when the library is on."""
    config = await asyncio.to_thread(_load_config)
    if not bool(getattr(config.appshot, "library", False)):
        return
    from dodaios.appshot import library  # noqa: PLC0415

    await asyncio.to_thread(library.save_edit, shot)


async def _attach_to_card(shot: Appshot, config: Any) -> None:
    """The corner card may hand this picture out by drag — if it may be kept.

    ``deck_preview_s = 0`` means the user wants no picture kept around, so the
    card then only opens the editor and shares nothing.
    """
    if float(config.screen_context.deck_preview_s) <= 0:
        return
    from dodaios.appshot.effect import attach_card_image  # noqa: PLC0415

    await attach_card_image(shot.image, shot.id)


async def _deliver(shot: Appshot, *, target: str, ttl_s: float) -> str:
    from dodaios.appshot.delivery import deliver_to_live  # noqa: PLC0415

    if target in ("auto", "voice") and await deliver_to_live(shot.image, shot.mime, shot.note):
        return "voice"
    if target == "voice":
        return "none"
    get_store().park(shot, ttl_s=ttl_s)
    return "message"


async def _publish(bus: Any | None, shot: Appshot, delivered_to: str) -> None:
    if bus is None:
        return
    try:
        from dodaios.core.events import AppshotTaken  # noqa: PLC0415

        await bus.publish(
            AppshotTaken(
                source_layer="appshot",
                appshot_id=shot.id,
                trigger=shot.trigger,
                delivered_to=delivered_to,
                target_label=shot.label,
                width=shot.width,
                height=shot.height,
            )
        )
    except Exception:  # noqa: BLE001 - a lost receipt cannot undo the appshot
        log.warning("appshot: receipt publication failed", exc_info=True)


#: Rides with an edited appshot, after its original evidence note.
EDIT_NOTE = (
    "EDITED BY THE USER: this is the same appshot with the user's own markings. "
    "Arrows, boxes, circles, numbers, highlights and text are theirs and point "
    "at what they mean; blurred or pixelated parts were hidden on purpose. Read "
    "the markings first."
)


async def deliver_edit(shot: Appshot) -> str:
    """Hand the user's edited appshot to the assistant; where it went.

    The edit replaces the picture wherever the original still waits (the
    store already swapped it). Then: a running voice call gets the edited
    picture at once when ``[appshot].target`` allows it — including a call
    that already saw the original — and otherwise the next message carries
    it, even when the original has been sent already. ``none`` only when the
    target is voice-only and no call runs.
    """
    config = await asyncio.to_thread(_load_config)
    target = str(config.appshot.target)
    note = shot.note
    if EDIT_NOTE not in note:
        note = "\n\n".join(part for part in (note, EDIT_NOTE) if part)
    edited = replace(shot, note=note)
    store = get_store()
    from dodaios.appshot.delivery import deliver_to_live  # noqa: PLC0415

    delivered_to = "none"
    if target in ("auto", "voice") and await deliver_to_live(edited.image, edited.mime, note):
        # The call has it now; the waiting original must not follow later.
        store.take_pending(shot.id)
        delivered_to = "voice"
    elif target != "voice":
        store.park(edited, ttl_s=float(config.screen_context.ttl_s))
        delivered_to = "message"
    store.mark_delivered(shot.id, delivered_to)
    log.info("appshot: edited %s -> %s", shot.label, delivered_to)
    return delivered_to


def take_pending_for_turn() -> Appshot | None:
    """The appshot waiting for the next message, removed on the way out."""
    return get_store().take_pending()


__all__ = [
    "APPSHOT_PREAMBLE",
    "EDIT_NOTE",
    "AppshotResult",
    "Scope",
    "deliver_edit",
    "keep_edit_in_library",
    "record_turn_capture",
    "shot_from_context",
    "take_appshot",
    "take_pending_for_turn",
]