"""Backends the brain's schedule, personalNotes, screenView and pcControl tools use during one reply (schedule.spec.md, personal_notes.spec.md, screen_view.spec.md, pc_control.spec.md). The app keeps the schedules and the notes, captures the screen and operates programs: every action goes to it as ``_action`` for the person asking and waits for its ``_action_result``. Built per reply, on the event loop; ``act`` is called from the brain's thread. """ from __future__ import annotations import asyncio from typing import Any, Optional class AppBridge: def __init__(self, session, loop: asyncio.AbstractEventLoop, kind: str, user: str, via: str, channel: Optional[str], turn: int = 0, state: Optional[dict] = None): self._session = session self._loop = loop self._kind = kind # "schedule", "note", "screen" or "pc" # pc only: the asker's reply number and the state the tool keeps between their replies. self.turn = turn self.state = state if state is not None else {} # Who asked and where: the app delivers schedules there (voice, or the chat channel asked in). self._origin = {"user": user, "via": via, **({"channel": channel} if channel else {})} def act(self, action: str, **kw: Any) -> dict: fut = asyncio.run_coroutine_threadsafe( self._session.app_action(self._kind, action, {**self._origin, **kw}), self._loop) try: return fut.result(self._session.worker.action_timeout_s(self._kind) + 5) except Exception as e: # loop gone or the wait broke return {"ok": False, "error": f"the app did not answer ({type(e).__name__})"}