"""The schedule backend the brain's schedule tool uses during one reply (schedule.spec.md). The app keeps the schedules: every action goes to it as ``schedule_action`` for the person asking and waits for its ``schedule_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 ScheduleBridge: def __init__(self, session, loop: asyncio.AbstractEventLoop, user: str, via: str, channel: Optional[str]): self._session = session self._loop = loop # Who asked and where: the app delivers 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.schedule_action(action, {**self._origin, **kw}), self._loop) try: return fut.result(self._session.worker.chat_timeout_s + 5) except Exception as e: # loop gone or the wait broke return {"ok": False, "error": f"the app did not answer ({type(e).__name__})"}