adapter.py 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892
  1. """Chatto Platform Adapter for Hermes Agent.
  2. A plugin-based gateway adapter that connects to a Chatto server
  3. (self-hosted team chat) and relays messages to/from the Hermes agent.
  4. The adapter uses the chattolib library for all Chatto API interactions,
  5. including both outbound messaging and realtime WebSocket connections.
  6. """
  7. from __future__ import annotations
  8. import random
  9. # Put the vendored dependencies for THIS platform on sys.path before importing
  10. # anything from chattolib. Imported relatively as part of the plugin package and
  11. # absolutely when this module is loaded standalone (e.g. by the tests).
  12. try:
  13. from .vendor_path import setup_vendor_path
  14. except ImportError: # pragma: no cover - depends on how the module is loaded
  15. from vendor_path import setup_vendor_path
  16. setup_vendor_path()
  17. import asyncio
  18. import hashlib
  19. import logging
  20. import mimetypes
  21. import os
  22. import ssl
  23. import urllib.request as _urllib_request
  24. from datetime import UTC, datetime
  25. from enum import StrEnum
  26. from typing import Any, cast
  27. from urllib.parse import urlsplit
  28. logger = logging.getLogger(__name__)
  29. from gateway.config import Platform, PlatformConfig
  30. from gateway.platforms.base import (
  31. BasePlatformAdapter,
  32. MessageEvent,
  33. MessageType,
  34. ProcessingOutcome,
  35. SendResult,
  36. cache_media_bytes,
  37. get_inbound_media_max_bytes,
  38. validate_inbound_media_size,
  39. )
  40. from gateway.session import build_session_key
  41. # Chattolib imports (vendored)
  42. # Using vendored chattolib from vendor/chattolib/
  43. # See vendor_chattolib.sh for how to update the vendored copy
  44. # Absolute imports — vendor/ is on sys.path (see above) and chattolib's own
  45. # modules import each other absolutely. Mixing in relative ".vendor.chattolib"
  46. # imports would load a second, distinct copy of every module, so isinstance()
  47. # checks across the two copies would silently fail.
  48. try:
  49. from chattolib.client import (
  50. ChattoClient,
  51. )
  52. from chattolib.exceptions import (
  53. ChattoAuthError,
  54. ChattoError,
  55. )
  56. from chattolib.realtime import (
  57. ChattoRealtimeCloseError,
  58. ChattoRealtimeError,
  59. RealtimeEvent,
  60. stream_events,
  61. )
  62. from chattolib.realtime_types import (
  63. MessageEditedPayload,
  64. MessagePostedPayload,
  65. ReactionPayload,
  66. )
  67. from chattolib.types import (
  68. Message,
  69. PresenceStatus,
  70. Room,
  71. RoomKind,
  72. RoomWithViewerState,
  73. User,
  74. )
  75. except ImportError as e:
  76. # Fail loudly: continuing here only defers the failure to a confusing
  77. # NameError somewhere deep in the adapter.
  78. logger.error("Chatto: failed to import vendored chattolib: %s", e)
  79. raise
  80. try:
  81. from .platform_config import (
  82. ChattoConfiguration,
  83. ChattoConstants,
  84. )
  85. except ImportError: # pragma: no cover - loaded as a top-level module (tests)
  86. from platform_config import (
  87. ChattoConfiguration,
  88. ChattoConstants,
  89. )
  90. # --------------------------------------------------------------------------- #
  91. # Chat types
  92. # --------------------------------------------------------------------------- #
  93. class HermesChatType(StrEnum):
  94. """The ``chat_type`` vocabulary the Hermes gateway understands.
  95. Declared in ``gateway/session.py:161`` as ``"dm", "group", "channel",
  96. "thread"`` and consumed as a bare string all over the gateway:
  97. ``SessionSource.description`` (session.py:239) and the PII-redacting
  98. description in ``build_session_context_prompt`` (session.py:537) both
  99. branch on these exact values and fall back to a nameless generic case for
  100. anything else, and ``build_session_key`` puts the value straight into the
  101. session key. Passing a chattolib ``RoomKind`` (``"ROOM_KIND_CHANNEL"``)
  102. therefore does not fail loudly — it just quietly degrades what the agent is
  103. told about where it is.
  104. A StrEnum so it stays a drop-in ``str`` at every one of those call sites.
  105. GROUP vs CHANNEL
  106. ----------------
  107. There is no strict contract between the two, and the adapters disagree in
  108. practice: Slack labels every non-DM conversation ``"group"`` (including real
  109. channels), Discord uses both, and Telegram reserves ``"channel"`` for actual
  110. broadcast channels. The intended reading is ``group`` = ordinary
  111. multi-participant chat, ``channel`` = broadcast surface.
  112. The distinction only changes behaviour in three places:
  113. 1. Authorization (``gateway/authz_mixin.py``) — the only security-relevant
  114. one. The group-scoped env allowlists apply to ``{"group", "forum"}``
  115. ONLY, never to ``"channel"``: ``{PLATFORM}_GROUP_ALLOWED_USERS`` /
  116. ``_GROUP_ALLOWED_CHATS`` (:616), the chat-id allowlist (:708) and the
  117. Telegram legacy shim (:724). The adapter-delegation paths in turn treat
  118. all three alike (:461, :649, :674, :694), where the value only picks
  119. ``group_allow_from`` over ``allow_from`` from ``config.extra``.
  120. For Chatto both choices are equivalent today: those group env maps hold
  121. Telegram and QQBot only (:535-541), and our own allowlist runs through
  122. ``CHATTO_ALLOWED_USERS``, which is chat_type-independent.
  123. 2. What the agent is told — ``SessionSource.description`` renders
  124. ``"group: Name"`` vs ``"channel: Name"`` (session.py:239-246), likewise
  125. the PII-redacted variant (session.py:537-544).
  126. 3. The session key, which embeds the literal (session.py:1192). Changing
  127. the value for a room re-buckets its existing sessions.
  128. Explicitly NOT affected: ``is_shared_multi_user_session`` (session.py:1063)
  129. only looks at ``"dm"`` and ``thread_id``, so sender prefixes, the multi-user
  130. prompt line and ``group_sessions_per_user`` treat group and channel
  131. identically.
  132. """
  133. DM = "dm"
  134. GROUP = "group"
  135. CHANNEL = "channel"
  136. # Emitted by adapters whose thread events are their own chat type (Slack,
  137. # Discord). We don't: a Chatto thread keeps its room's chat_type and is
  138. # identified by ``thread_id`` on the source instead. Listed for the record,
  139. # because build_session_key rewrites the slot to "thread" itself
  140. # (session.py:1190).
  141. THREAD = "thread"
  142. # Not declared in session.py:161 but real: Telegram forum topics travel as
  143. # "forum", and the authz group allowlists above accept it alongside "group".
  144. # Chatto has no equivalent, so we never emit it.
  145. # Chatto only distinguishes DMs from channels. UNSPECIFIED means the server
  146. # sent a kind this vendored chattolib doesn't know: map it to the generic
  147. # multi-user bucket rather than guessing "channel", and never to "dm" — that
  148. # value drives session isolation (is_shared_multi_user_session, session.py:1063)
  149. # and would silently turn a room into a private conversation.
  150. #
  151. # CHANNEL for RoomKind.CHANNEL is the descriptive choice and carries no
  152. # behavioural cost (see the GROUP vs CHANNEL note above). Switching to GROUP for
  153. # Slack parity would be this one line — plus the re-bucketing of existing
  154. # sessions that point 3 of that note describes.
  155. _ROOM_KIND_TO_CHAT_TYPE: dict[RoomKind, HermesChatType] = {
  156. RoomKind.DM: HermesChatType.DM,
  157. RoomKind.CHANNEL: HermesChatType.CHANNEL,
  158. RoomKind.UNSPECIFIED: HermesChatType.GROUP,
  159. }
  160. def chat_type_for_room_kind(kind: RoomKind | None) -> HermesChatType:
  161. """Map a chattolib RoomKind onto the gateway's chat_type vocabulary.
  162. An unknown or missing kind becomes ``GROUP`` — see ``_ROOM_KIND_TO_CHAT_TYPE``.
  163. """
  164. if kind is None:
  165. return HermesChatType.GROUP
  166. return _ROOM_KIND_TO_CHAT_TYPE.get(kind, HermesChatType.GROUP)
  167. # Blocking filesystem/network helpers. The adapter runs on the shared gateway
  168. # event loop, so file reads and HTTP downloads are pushed to a worker thread
  169. # via asyncio.to_thread — a slow disk or dead image URL must not stall every
  170. # platform's message processing.
  171. def _read_file_bytes(path: str) -> bytes:
  172. """Read a whole file synchronously (run via asyncio.to_thread)."""
  173. with open(path, "rb") as f:
  174. return f.read()
  175. def _write_file_bytes(path: str, data: bytes) -> None:
  176. """Write bytes to a file synchronously (run via asyncio.to_thread)."""
  177. with open(path, "wb") as f:
  178. f.write(data)
  179. def _download_to_file(url: str, path: str, timeout: float) -> None:
  180. """Download ``url`` to ``path`` synchronously (run via asyncio.to_thread)."""
  181. req = _urllib_request.Request(url, headers={"User-Agent": "Hermes/1.0"})
  182. try:
  183. ctx = ssl.create_default_context()
  184. except Exception:
  185. ctx = None
  186. with _urllib_request.urlopen(req, timeout=timeout, context=ctx) as resp:
  187. _write_file_bytes(path, resp.read())
  188. # --------------------------------------------------------------------------- #
  189. # Adapter
  190. # --------------------------------------------------------------------------- #
  191. def hermes_adapter_factory(config: PlatformConfig):
  192. """Construct a ChattoAdapter from a PlatformConfig."""
  193. return ChattoAdapter(config)
  194. class ChattoAdapter(BasePlatformAdapter):
  195. """Chatto platform adapter.
  196. Receives messages via WebSocket realtime, sends via ConnectRPC.
  197. """
  198. _SPLIT_THRESHOLD = 9900
  199. # Read by BasePlatformAdapter.max_message_length_for_chat(), which the
  200. # gateway and the stream consumer use to chunk outgoing messages. Without
  201. # it they fall back to 4096 and split Chatto messages far earlier than
  202. # necessary — send() itself already truncates at 10000.
  203. MAX_MESSAGE_LENGTH = ChattoConstants.MAX_MESSAGE_LENGTH
  204. splits_long_messages = True
  205. supports_code_blocks: bool = True
  206. supports_status_text: bool = True # client.update_custom_status
  207. def __init__(self, pconfig: PlatformConfig):
  208. """Signature needs to be compatible with BasePlatformAdapter.__init__."""
  209. super().__init__(
  210. config=pconfig, platform=Platform(ChattoConstants.PLATFORM_NAME)
  211. )
  212. # "extra" has been pre-processed by Hermes-Framework to be a dict of extra config values insode PlatformConfig.
  213. # --- Configuration from our configuration data class with some logic ---
  214. self.chatto_config: ChattoConfiguration = ChattoConfiguration(pconfig)
  215. # ------ State -------
  216. # Our own user, filled in by connect(). Events arriving before connect()
  217. # completes must not blow up on an undefined attribute.
  218. self.me: User | None = None
  219. # --- Runtime state ---
  220. self._room_names: dict[str, str] = {}
  221. self._room_kinds: dict[str, RoomKind] = {}
  222. self._our_thread_roots: set = set() # thread root event IDs we created
  223. self._our_message_ids: set = (
  224. set()
  225. ) # message IDs we sent (for thread root detection)
  226. # Event IDs already processed — chattolib may redeliver events across
  227. # reconnects, so every inbound event is checked against this list.
  228. self._seen: list[str] = []
  229. # Message IDs this adapter has handed to the gateway (posted or
  230. # edit-re-dispatched). Edits of anything on this list never start a
  231. # fresh turn — that is the lock against re-answering settled
  232. # conversations by editing old messages.
  233. self._dispatched_ids: list[str] = []
  234. # session_key -> message ID currently being processed there. Written
  235. # by on_processing_start, cleared by on_processing_complete; an edit
  236. # landing on the recorded ID is a mid-run correction.
  237. self._processing: dict[str, str] = {}
  238. self._joined_room_ids: list[str] = []
  239. # Rooms the server force-joined everyone into (Room.universal) — used
  240. # only for [universal] tags in the joined-rooms log line, never for
  241. # gating.
  242. self._universal_room_ids: set[str] = set()
  243. # One-shot guard for the unjoined-home-channel warning in _refresh_rooms.
  244. self._home_warning_logged = False
  245. self._ws_task: asyncio.Task | None = None
  246. self._presence_task: asyncio.Task | None = None
  247. # Persistent typing indicator loops per room
  248. self._typing_tasks: dict[str, asyncio.Task] = {}
  249. # Member directory cache: user_id -> user info dict
  250. self._user_cache: dict[str, User] = {}
  251. # Handle -> does a user hold it. Cached both ways; see _mentions_someone_else.
  252. self._known_handles: dict[str, bool] = {}
  253. # Chattolib client cache and lock for async access.
  254. self._chatto_client: ChattoClient | None = None
  255. self._chatto_client_lock: asyncio.Lock = asyncio.Lock()
  256. # ------------------------------------------------------------------ #
  257. # Auth
  258. # ------------------------------------------------------------------ #
  259. async def _get_chatto_client(self: ChattoAdapter) -> ChattoClient | None:
  260. """Return the shared ChattoClient, creating and logging in on first use.
  261. The default way to get a client. The fast path is a plain attribute
  262. read; first creation runs under a lock so concurrent callers log in
  263. exactly once. Returns ``None`` when no client exists yet or creation
  264. failed (bad credentials, unreachable server) — a normal state during
  265. startup, shutdown and reconnects, not an exceptional one. Callers
  266. decide what "no client" means for them, as a guard clause:
  267. client = await self._get_chatto_client()
  268. if client is None:
  269. logger.warning("Chatto: dropping X - no client available")
  270. return
  271. """
  272. if self._chatto_client is not None:
  273. return self._chatto_client
  274. async with self._chatto_client_lock:
  275. if self._chatto_client is not None:
  276. return self._chatto_client
  277. try:
  278. client = await self._open_client(
  279. base_url=self.chatto_config.base_url.value,
  280. login=self.chatto_config.login.value,
  281. password=self.chatto_config.password.value,
  282. token=self.chatto_config.token.value,
  283. )
  284. self._chatto_client = client
  285. logger.info(
  286. "Chatto: logged in as '%s' via chattolib",
  287. self.chatto_config.login.value,
  288. )
  289. return client
  290. except ChattoAuthError as e:
  291. logger.error("Chatto: authentication failed: %s", e)
  292. return None
  293. except (ChattoError, ValueError) as e:
  294. logger.error("Chatto: failed to create client: %s", e)
  295. return None
  296. async def _require_chatto_client(self) -> ChattoClient:
  297. """Return a ChattoClient or raise RuntimeError if unavailable.
  298. The exception-flavoured variant of :meth:`_get_chatto_client`, for
  299. callers whose surrounding machinery already routes exceptions.
  300. Currently that is only the realtime event loop, whose except chain
  301. turns the raise into a logged warning plus a backed-off reconnect —
  302. no special "no client" branch needed there. Everywhere else, prefer
  303. the ``is None`` guard shown in :meth:`_get_chatto_client`.
  304. """
  305. client = await self._get_chatto_client()
  306. if client is None:
  307. raise RuntimeError("Chatto client unavailable")
  308. return client
  309. async def _ensure_token(self) -> bool:
  310. """Ensure we have a logged-in Chatto client and token."""
  311. if self.chatto_config.token.value and isinstance(
  312. self._chatto_client, ChattoClient
  313. ):
  314. return True
  315. client = await self._get_chatto_client()
  316. return client is not None
  317. # ------------------------------------------------------------------ #
  318. # Connection
  319. # ------------------------------------------------------------------ #
  320. async def _open_client(
  321. self,
  322. *,
  323. base_url: str,
  324. login: str,
  325. password: str,
  326. token: str | None = None,
  327. ) -> ChattoClient:
  328. """Return a connected ``ChattoClient`` using token or login/password.
  329. Token wins when both are configured. Raises ValueError when neither a
  330. token nor login+password is set — caught upstream as a normal
  331. creation failure, so misconfiguration reads as a logged error instead
  332. of a crash.
  333. """
  334. if token:
  335. return ChattoClient(token=token, base_url=base_url)
  336. if not login or not password:
  337. raise ValueError("Chatto: neither token nor login/password configured")
  338. return await ChattoClient.login(login, password, base_url=base_url)
  339. async def connect(self, *, is_reconnect: bool = False) -> bool:
  340. """Connect to Chatto and start the realtime event stream.
  341. BasePlatformAdapter override
  342. """
  343. logger.info("Chatto: connecting...")
  344. if not await self._ensure_token():
  345. return False
  346. client = await self._get_chatto_client()
  347. if client is None:
  348. self._set_fatal_error(
  349. "connect_failed", "Chatto client not available", retryable=True
  350. )
  351. return False
  352. # Get our first own user info
  353. try:
  354. self.me = await client.me()
  355. except Exception as exc:
  356. logger.error("Chatto: failed to get user info: %s", exc)
  357. self._set_fatal_error(
  358. "chatto_auth_failed",
  359. f"Chatto auth failed: {exc}",
  360. retryable=False,
  361. )
  362. try:
  363. assert self._chatto_client
  364. await self._chatto_client.close()
  365. finally:
  366. self._chatto_client = None
  367. return False
  368. # Announce online presence so the bot appears online in the member list.
  369. # The server treats this as a TTL, so _presence_refresh_loop below has to
  370. # keep re-announcing it — a single call here lapses back to offline.
  371. await self._announce_online()
  372. self._closing = False
  373. # Start background realtime WS event stream loop.
  374. self._ws_task = asyncio.create_task(
  375. self._chattolib_event_loop(),
  376. name="chatto-event-stream",
  377. )
  378. self._presence_task = asyncio.create_task(
  379. self._presence_refresh_loop(),
  380. name="chatto-presence-refresh",
  381. )
  382. self._mark_connected()
  383. logger.info(
  384. "Chatto: connected and authentiated to %s using login:'%s' (display_name:'%s' id: '%s')",
  385. self.chatto_config.base_url.value,
  386. self.me.login,
  387. self.me.display_name,
  388. self.me.id,
  389. )
  390. return True
  391. async def _announce_online(self) -> bool:
  392. """Tell the server we are online. Returns whether the call got through.
  393. Logged at warning level on failure: a silently dropped presence call is
  394. indistinguishable from a bot that is simply not running.
  395. """
  396. client = await self._get_chatto_client()
  397. if client is None:
  398. logger.warning(
  399. "Chatto: presence refresh failed, bot may appear offline: no client"
  400. )
  401. return False
  402. try:
  403. await client.update_presence(status=PresenceStatus.ONLINE)
  404. return True
  405. except Exception as exc:
  406. logger.warning(
  407. "Chatto: presence refresh failed, bot may appear offline: %s", exc
  408. )
  409. return False
  410. async def _presence_refresh_loop(self) -> None:
  411. """Re-announce ONLINE until disconnect, since presence expires server-side.
  412. Failures are not fatal — the next tick tries again, so a blip in the
  413. presence endpoint costs at most one interval of visible offline time.
  414. """
  415. while not self._closing:
  416. await self._sleep_interruptible(ChattoConstants.PRESENCE_REFRESH_INTERVAL)
  417. if self._closing:
  418. return
  419. await self._announce_online()
  420. async def disconnect(self) -> None:
  421. """Stop WebSocket, presence refresh, typing tasks, and clear state.
  422. BasePlatformAdapter override
  423. """
  424. # No explicit offline broadcast: chattolib rejects OFFLINE outright
  425. # ("stop refreshing to go offline"), so cancelling the refresh loop
  426. # below is what actually takes the bot offline.
  427. self._closing = True
  428. # Cancel all typing tasks
  429. for chat_id in list(self._typing_tasks.keys()):
  430. await self.stop_typing(chat_id)
  431. if self._ws_task and not self._ws_task.done():
  432. self._ws_task.cancel()
  433. try:
  434. await self._ws_task
  435. except (asyncio.CancelledError, Exception):
  436. logger.debug(
  437. "Chatto: websocket task ended during disconnect", exc_info=True
  438. )
  439. self._ws_task = None
  440. if self._presence_task and not self._presence_task.done():
  441. self._presence_task.cancel()
  442. try:
  443. await self._presence_task
  444. except (asyncio.CancelledError, Exception):
  445. logger.debug(
  446. "Chatto: presence task ended during disconnect", exc_info=True
  447. )
  448. self._presence_task = None
  449. if self._chatto_client:
  450. try:
  451. await self._chatto_client.close()
  452. except Exception:
  453. logger.exception("Chatto: error closing client")
  454. finally:
  455. self._chatto_client = None
  456. logger.info("Chatto: disconnected")
  457. self._mark_disconnected()
  458. async def _seed_room(self, room_id: str) -> None:
  459. """Seed high-water mark from the newest events so a restart doesn't replay history."""
  460. try:
  461. client = await self._get_chatto_client()
  462. if client is None:
  463. logger.debug(
  464. "Chatto: _seed_room aborted - no client available for %s", room_id
  465. )
  466. return
  467. timeline_page = await client.get_room_events(room_id)
  468. for ev in timeline_page.events:
  469. if ev.id:
  470. self._mark_seen(ev.id)
  471. logger.debug(
  472. "Chatto: seeded room %s with %d events",
  473. room_id,
  474. len(timeline_page.events),
  475. )
  476. except Exception as e:
  477. logger.warning("Chatto: get room events failed for %s: %s", room_id, e)
  478. # ------------------------------------------------------------------ #
  479. # Realtime Event List
  480. # ------------------------------------------------------------------ #
  481. def _mark_seen(self, event_id: str) -> None:
  482. self._seen.append(event_id)
  483. while len(self._seen) > ChattoConstants.SEEN_CAP:
  484. self._seen.remove(self._seen[0]) # fastest removal of first item in a list.
  485. def _is_seen(self, event_id: str) -> bool:
  486. return event_id in self._seen
  487. # ------------------------------------------------------------------ #
  488. # WebSocket Realtime Transport
  489. # ------------------------------------------------------------------ #
  490. def _mentions_me(self, body: str) -> bool:
  491. """Whether the message addresses this bot.
  492. By login, by display name, or by a broadcast handle — ``@here`` speaks
  493. to everyone present and the bot is one of them, so naming a colleague
  494. alongside it does not take the bot out of the audience.
  495. """
  496. if not self.me:
  497. return False
  498. for handle in (self.me.login, self.me.display_name):
  499. if handle and f"@{handle}" in body:
  500. return True
  501. return any(
  502. handle.lower() in ChattoConstants.BROADCAST_MENTIONS
  503. for handle in ChattoConstants.MENTION_RE.findall(body)
  504. )
  505. async def _handle_belongs_to_a_user(self, handle: str) -> bool:
  506. """Whether ``handle`` is the login of a real Chatto user.
  507. The API carries no mention entities — ``mention_confirmation_token`` is
  508. reserved in the message descriptor — so an @-token is only a candidate
  509. until the directory confirms it. Results are cached both ways, since
  510. the same handles recur and a miss is as reusable as a hit.
  511. """
  512. known = self._known_handles.get(handle)
  513. if known is not None:
  514. return known
  515. client = await self._get_chatto_client()
  516. if client is None:
  517. # Unresolved means "not confirmed", so the message goes through.
  518. return False
  519. try:
  520. member = await client.get_user(login=handle)
  521. except Exception as exc:
  522. logger.debug("Chatto: could not resolve handle @%s: %s", handle, exc)
  523. return False
  524. exists = member is not None and member.user is not None
  525. self._known_handles[handle] = exists
  526. return exists
  527. async def _mentions_someone_else(self, body: str) -> bool:
  528. """Whether the message @-mentions a person who is not this bot.
  529. Broadcast handles are not a person — they address everyone present,
  530. the bot included, so they do not count as someone else. A handle no
  531. user holds is not a mention at all: someone writing *about* mentioning
  532. ("per @-mention", "@nonexistent") is talking to us, and staying silent
  533. on a false positive is worse than answering one.
  534. """
  535. for handle in ChattoConstants.MENTION_RE.findall(body):
  536. if handle.lower() in ChattoConstants.BROADCAST_MENTIONS:
  537. continue
  538. if self.me and handle in (self.me.login, self.me.display_name):
  539. continue
  540. if await self._handle_belongs_to_a_user(handle):
  541. return True
  542. return False
  543. def _check_auth(self, user: User) -> bool:
  544. """We roll our own auth-systen on the against the chatto_config'ured allowed_users etc.
  545. because.. Hermes authz_mixin.py IS NOT SANE.
  546. """
  547. if self.chatto_config.allow_all_users.value:
  548. return True
  549. if user.login in self.chatto_config.allowed_users.value:
  550. return True
  551. if user.id in self.chatto_config.allowed_users.value:
  552. return True
  553. logger.warning(
  554. "Chatto: rejecting message from unauthorized user '%s' (%s)",
  555. user.login,
  556. user.id,
  557. )
  558. return False
  559. # ------------------------------------------------------------------ #
  560. # Room management over DM (/join, /leave)
  561. # ------------------------------------------------------------------ #
  562. _DM_COMMANDS = ("/join", "/leave")
  563. async def _handle_dm_command(self, room_id: str, body: str) -> bool:
  564. """Run a ``/join`` or ``/leave`` admin command sent as a direct message.
  565. Returns True when ``body`` is one of the commands — whether it
  566. succeeded or not — so the caller keeps it out of the agent pipeline.
  567. Membership lives on the Chatto server: a joined room reappears in
  568. every future ``list_rooms()`` and therefore survives restarts.
  569. """
  570. verb, _, argument = body.strip().partition(" ")
  571. if verb.lower() not in self._DM_COMMANDS:
  572. return False
  573. client = await self._get_chatto_client()
  574. if client is None:
  575. await self.send(chat_id=room_id, content="Chatto client is not connected.")
  576. return True
  577. argument = argument.strip()
  578. if not argument:
  579. await self.send(
  580. chat_id=room_id,
  581. content="Usage: /join <room-id or #name> | /leave <room-id or #name>",
  582. )
  583. return True
  584. error, target = await self._resolve_room_target(client, argument)
  585. if error or target is None:
  586. await self.send(chat_id=room_id, content=error or "Room lookup failed.")
  587. return True
  588. if verb.lower() == "/join":
  589. reply = await self._run_join(client, target)
  590. else:
  591. reply = await self._run_leave(client, target)
  592. await self.send(chat_id=room_id, content=reply)
  593. return True
  594. async def _resolve_room_target(
  595. self,
  596. client: ChattoClient,
  597. argument: str,
  598. ) -> tuple[str | None, RoomWithViewerState | None]:
  599. """Resolve a /join//leave argument to a room.
  600. ``#name`` is looked up case-insensitively in a fresh directory scan
  601. (which also refreshes our name/kind caches); anything else is treated
  602. as a room ID and verified via GetRoom. An ambiguous name comes back as
  603. an error naming the candidates, so the admin can retry with an ID.
  604. """
  605. if not argument.startswith("#"):
  606. state = await client.get_room(argument)
  607. if state is None or state.room is None:
  608. return f"No room with ID '{argument}'.", None
  609. return None, state
  610. wanted = argument[1:].strip().casefold()
  611. # (state, room) pairs: a listed match's room is already narrowed here,
  612. # so the candidate listing below needs no fresh Optional dance.
  613. matches: list[tuple[RoomWithViewerState, Room]] = []
  614. for state in await client.list_rooms() or []:
  615. room_obj = state.room if state else None
  616. if room_obj and (room_obj.name or "").strip().casefold() == wanted:
  617. matches.append((state, room_obj))
  618. self._room_names[room_obj.id] = room_obj.name
  619. self._room_kinds[room_obj.id] = room_obj.kind
  620. if not matches:
  621. return f"No room named '{argument}'.", None
  622. if len(matches) > 1:
  623. candidates = "\n".join(f"• {room.name} ({room.id})" for _, room in matches)
  624. return (
  625. f"Several rooms are named '{argument}' — pick one by ID:\n{candidates}"
  626. ), None
  627. return None, matches[0][0]
  628. async def _run_join(self, client: ChattoClient, state: RoomWithViewerState) -> str:
  629. """Join a room via RoomService/JoinRoom and track it as joined.
  630. An account that already holds membership (invited natively in Chatto)
  631. needs no JoinRoom call — it only gets seeded and added to the list.
  632. """
  633. room_obj = state.room
  634. if room_obj is None:
  635. # Unreachable via _resolve_room_target: both of its paths only
  636. # return states whose room they already inspected.
  637. return "Chatto returned an empty room record — try again."
  638. label = f"'{room_obj.name}' ({room_obj.id})"
  639. joined_room = room_obj
  640. if not state.viewer_state.is_member:
  641. try:
  642. joined_room = await client.join_room(room_obj.id) or room_obj
  643. except ChattoError as exc:
  644. logger.warning("Chatto: /join failed for %s (%s)", room_obj.id, exc)
  645. return f"Could not join {label}: {exc}"
  646. self._room_names[joined_room.id] = joined_room.name
  647. self._room_kinds[joined_room.id] = joined_room.kind
  648. if joined_room.id not in self._joined_room_ids:
  649. await self._seed_room(joined_room.id)
  650. self._joined_room_ids.append(joined_room.id)
  651. if state.viewer_state.is_member:
  652. return f"Already a member of {label} — listening there."
  653. return f"Joined {label}."
  654. async def _run_leave(self, client: ChattoClient, state: RoomWithViewerState) -> str:
  655. """Leave a room via RoomService/LeaveRoom and drop it from the joined list.
  656. Two rooms are refused: a DM conversation cannot be left, and leaving
  657. the configured home channel would silently break cron/notification
  658. delivery, which posts there through the standalone sender.
  659. """
  660. room_obj = state.room
  661. if room_obj is None:
  662. # Same invariant as _run_join: _resolve_room_target pre-inspects.
  663. return "Chatto returned an empty room record — try again."
  664. label = f"'{room_obj.name}' ({room_obj.id})"
  665. if room_obj.kind == RoomKind.DM:
  666. return "Direct messages cannot be left."
  667. home_id = (self.chatto_config.home_channel.value or "").strip()
  668. if home_id == room_obj.id:
  669. return (
  670. f"{label} is the configured home channel "
  671. "(CHATTO_HOME_CHANNEL); leaving it would break cron and "
  672. "notification delivery. Point CHATTO_HOME_CHANNEL elsewhere first."
  673. )
  674. try:
  675. left = await client.leave_room(room_obj.id)
  676. except ChattoError as exc:
  677. logger.warning("Chatto: /leave failed for %s (%s)", room_obj.id, exc)
  678. return f"Could not leave {label}: {exc}"
  679. if not left:
  680. return f"Chatto refused to leave {label}."
  681. if room_obj.id in self._joined_room_ids:
  682. self._joined_room_ids.remove(room_obj.id)
  683. return f"Left {label}."
  684. # ------------------------------------------------------------------ #
  685. # Inbound attachments
  686. # ------------------------------------------------------------------ #
  687. async def _download_attachment_bytes(self, url: str) -> bytes:
  688. """Download an attachment, refusing to buffer more than the gateway cap.
  689. The Content-Length header is checked first so an oversized asset is
  690. rejected before a single chunk is read; the running total is re-checked
  691. as chunks arrive, because a missing or lying header must not smuggle an
  692. unbounded body past the cap.
  693. """
  694. import httpx
  695. max_bytes = get_inbound_media_max_bytes()
  696. chunks: list[bytes] = []
  697. total = 0
  698. async with (
  699. httpx.AsyncClient(
  700. timeout=ChattoConstants.HTTP_TIMEOUT,
  701. follow_redirects=True,
  702. ) as http,
  703. http.stream("GET", url) as resp,
  704. ):
  705. resp.raise_for_status()
  706. declared = resp.headers.get("content-length")
  707. if declared:
  708. try:
  709. declared_size = int(declared)
  710. except ValueError:
  711. logger.debug("Chatto: ignoring invalid Content-Length %r", declared)
  712. else:
  713. validate_inbound_media_size(
  714. declared_size,
  715. media_type="attachment",
  716. max_bytes=max_bytes,
  717. )
  718. async for chunk in resp.aiter_bytes():
  719. total += len(chunk)
  720. validate_inbound_media_size(
  721. total,
  722. media_type="attachment",
  723. max_bytes=max_bytes,
  724. )
  725. chunks.append(chunk)
  726. return b"".join(chunks)
  727. async def _cache_attachments(
  728. self,
  729. room_id: str,
  730. attachments: list[Any],
  731. ) -> tuple[list[str], list[str], list[str]]:
  732. """Download message attachments into the gateway media cache.
  733. Returns ``(media_urls, media_types, media_kinds)`` — the paths are
  734. agent-visible cache paths, exactly what ``cache_media_bytes`` yields for
  735. every other platform. A failing attachment is logged and skipped: the
  736. message itself still reaches the agent.
  737. """
  738. media_urls: list[str] = []
  739. media_types: list[str] = []
  740. media_kinds: list[str] = []
  741. for att in attachments or []:
  742. asset_url = getattr(att, "asset_url", None)
  743. url = getattr(asset_url, "url", "") if asset_url else ""
  744. filename = getattr(att, "filename", "") or ""
  745. content_type = getattr(att, "content_type", "") or ""
  746. if not url:
  747. # Videos are announced before transcoding finishes, so the
  748. # signed URL can legitimately be missing on arrival.
  749. logger.debug(
  750. "Chatto: attachment '%s' has no asset URL yet, skipping",
  751. filename,
  752. )
  753. continue
  754. try:
  755. data = await self._download_attachment_bytes(url)
  756. cached = cache_media_bytes(
  757. data,
  758. filename=filename,
  759. mime_type=content_type,
  760. )
  761. except Exception as e:
  762. logger.warning(
  763. "Chatto: failed to cache attachment '%s' (%s): %s",
  764. filename,
  765. content_type,
  766. e,
  767. )
  768. continue
  769. if cached is None:
  770. logger.warning(
  771. "Chatto: attachment '%s' (%s) could not be cached, skipping",
  772. filename,
  773. content_type,
  774. )
  775. continue
  776. media_urls.append(cached.path)
  777. media_types.append(cached.media_type)
  778. media_kinds.append(cached.kind)
  779. return media_urls, media_types, media_kinds
  780. @staticmethod
  781. def _message_type_for_media_kinds(media_kinds: list[str]) -> MessageType:
  782. """Pick the MessageType for a set of cached attachment kinds."""
  783. if "document" in media_kinds:
  784. return MessageType.DOCUMENT
  785. if "image" in media_kinds:
  786. return MessageType.PHOTO
  787. if "video" in media_kinds:
  788. return MessageType.VIDEO
  789. if "audio" in media_kinds:
  790. return MessageType.AUDIO
  791. return MessageType.TEXT
  792. def _is_respond_room(self, room_id: str) -> bool:
  793. """Whether inbound messages from this room reach the agent pipeline.
  794. With ``CHATTO_RESPOND_ROOMS`` set, every non-DM room is gated against
  795. that positive list; rooms outside it stay read-only (marked as read,
  796. never seeded or answered). DMs are always respond rooms so ``/join``
  797. stays reachable, and unknown room kinds fail closed.
  798. """
  799. respond_rooms = self.chatto_config.respond_rooms.value
  800. if not respond_rooms:
  801. return True
  802. if self._room_kinds.get(room_id) == RoomKind.DM:
  803. return True
  804. return room_id in respond_rooms
  805. async def _dispatch_message_posted(self, payload: MessagePostedPayload) -> None:
  806. # Respond-room gate first: read-only memberships must not cost a
  807. # single API call, so this runs before fetch_message and _get_chatto_client.
  808. if not self._is_respond_room(payload.room_id):
  809. logger.debug(
  810. "Chatto: message from read-only room %s ignored", payload.room_id
  811. )
  812. return
  813. client = await self._get_chatto_client()
  814. if client is None:
  815. logger.warning("Chatto: dropping message - no client available")
  816. return
  817. logger.debug("Chatto WS: 'message_posted' payload:%s", payload)
  818. message = await payload.fetch_message(client=client)
  819. if message is None or message.deleted_at:
  820. return
  821. message_body = message.body or ""
  822. logger.debug("message: %s", message)
  823. # A message carrying only an image/PDF has an empty body — dropping it
  824. # here is what made attachments sent to Hermes disappear silently.
  825. attachments = list(message.attachments or [])
  826. if not message_body and not attachments:
  827. return
  828. event = await self._admit_and_build(
  829. client,
  830. room_id=payload.room_id,
  831. message=message,
  832. source_message_id=payload.message_event_id,
  833. thread_root_event_id=payload.thread_root_event_id or None,
  834. )
  835. if event is None:
  836. return
  837. self._remember_dispatched(event.message_id or "")
  838. logger.info("Chatto: dispatching message to Hermes")
  839. await self.handle_message(event)
  840. return
  841. def _remember_dispatched(self, message_id: str) -> None:
  842. """Record a message ID as handed to the gateway, capped like _seen."""
  843. if not message_id:
  844. return
  845. self._dispatched_ids.append(message_id)
  846. while len(self._dispatched_ids) > ChattoConstants.SEEN_CAP:
  847. self._dispatched_ids.remove(self._dispatched_ids[0])
  848. def _edit_is_fresh(self, message: Message) -> bool:
  849. """Whether this edit is young enough to still be processed.
  850. Age is measured against the posting time, so an edit to an hours-old
  851. message cannot resurrect a settled conversation even when it arrives
  852. right now.
  853. """
  854. if message.created_at is None or message.updated_at is None:
  855. return True
  856. age_seconds = (message.updated_at - message.created_at).total_seconds()
  857. return age_seconds <= self.chatto_config.edit_window.value
  858. def _session_key_for(self, source) -> str:
  859. """The gateway's own session key for this source.
  860. Built with exactly the inputs ``handle_message`` uses, so lookups in
  861. ``_processing`` and calls to ``cancel_session_processing`` hit the
  862. same session the gateway is running.
  863. """
  864. return build_session_key(
  865. source,
  866. group_sessions_per_user=self.config.extra.get(
  867. "group_sessions_per_user", True
  868. ),
  869. thread_sessions_per_user=self.config.extra.get(
  870. "thread_sessions_per_user", False
  871. ),
  872. )
  873. async def _dispatch_message_edited(self, payload: MessageEditedPayload) -> None:
  874. """Route an inbound edit according to the edit-dispatch contract.
  875. Three outcomes for the edited message:
  876. - currently being processed → cancel that turn and re-dispatch with
  877. the corrected text (the cancelled turn reports 🚫 via its
  878. CANCELLED outcome hook),
  879. - never dispatched (e.g. a forgotten @mention added later) → re-run
  880. the admission gates against the new body and answer for real,
  881. - already answered → stay answered.
  882. Edits whose text parses as a DM membership command are dropped: the
  883. command ran when the message was posted and must not run again.
  884. """
  885. if not self.chatto_config.edit_dispatch.value:
  886. return
  887. # Read-only memberships cost no API call, mirroring the posted path.
  888. if not self._is_respond_room(payload.room_id):
  889. logger.debug("Chatto: edit from read-only room %s ignored", payload.room_id)
  890. return
  891. client = await self._get_chatto_client()
  892. if client is None:
  893. logger.warning("Chatto: dropping edit - no client available")
  894. return
  895. logger.debug("Chatto WS: 'message_edited' payload:%s", payload)
  896. message = await payload.fetch_message(client=client)
  897. if message is None or message.deleted_at:
  898. return
  899. message_body = message.body or ""
  900. if not message_body and not message.attachments:
  901. return
  902. if not self._edit_is_fresh(message):
  903. logger.debug(
  904. "Chatto: edit of msg %s outside the edit window",
  905. payload.message_event_id,
  906. )
  907. return
  908. # Cheap triage before the admission pipeline: an edit to an
  909. # already-settled message (dispatched, neither running nor queued)
  910. # must not cost get_room/media calls or re-fire acknowledgements.
  911. was_dispatched = payload.message_event_id in self._dispatched_ids
  912. if (
  913. was_dispatched
  914. and payload.message_event_id not in self._processing.values()
  915. and not any(
  916. getattr(pending, "message_id", None) == payload.message_event_id
  917. for pending in self._pending_messages.values()
  918. )
  919. ):
  920. logger.debug(
  921. "Chatto: edit of already-answered msg %s ignored",
  922. payload.message_event_id,
  923. )
  924. return
  925. event = await self._admit_and_build(
  926. client,
  927. room_id=payload.room_id,
  928. message=message,
  929. source_message_id=payload.message_event_id,
  930. thread_root_event_id=message.thread_root_event_id or None,
  931. allow_dm_commands=False,
  932. )
  933. if event is None:
  934. return
  935. session_key = self._session_key_for(event.source)
  936. if self._processing.get(session_key) == payload.message_event_id:
  937. logger.info(
  938. "Chatto: msg %s edited mid-run - restarting the turn",
  939. payload.message_event_id,
  940. )
  941. # The cancelled task's completion hook clears _processing and
  942. # reports 🚫 before this coroutine moves on, because cancel
  943. # awaits the task. Queued follow-ups must survive.
  944. await self.cancel_session_processing(
  945. session_key, release_guard=True, discard_pending=False
  946. )
  947. elif payload.message_event_id not in self._dispatched_ids:
  948. logger.info(
  949. "Chatto: msg %s was never dispatched - edit starts a fresh turn",
  950. payload.message_event_id,
  951. )
  952. else:
  953. pending = self._pending_messages.get(session_key)
  954. if pending is not None and pending.message_id == payload.message_event_id:
  955. # Still queued behind the running turn: correct it in place
  956. # instead of answering stale wording later.
  957. pending.text = event.text
  958. logger.info(
  959. "Chatto: queued msg %s updated to its edited text",
  960. payload.message_event_id,
  961. )
  962. return
  963. logger.debug(
  964. "Chatto: edit of already-answered msg %s ignored",
  965. payload.message_event_id,
  966. )
  967. return
  968. self._remember_dispatched(event.message_id or "")
  969. logger.info("Chatto: dispatching edited message to Hermes")
  970. await self.handle_message(event)
  971. async def _admit_and_build(
  972. self,
  973. client: ChattoClient,
  974. *,
  975. room_id: str,
  976. message: Message,
  977. source_message_id: str,
  978. thread_root_event_id: str | None,
  979. allow_dm_commands: bool = True,
  980. ) -> MessageEvent | None:
  981. """Run one hydrated inbound message through the admission pipeline.
  982. Shared by the posted and the edited path: user resolution, auth,
  983. mention gates, thread anchoring and media caching all behave
  984. identically for both. Returns ``None`` for anything that must not
  985. reach the agent. DM membership commands are executed here (side
  986. effect) unless ``allow_dm_commands`` is False — edits pass False so
  987. a corrected command line neither runs twice nor leaks to the agent.
  988. The caller owns dispatching: a non-None result still needs
  989. ``handle_message()``.
  990. """
  991. if message.actor_id in self._user_cache:
  992. # try the user cache.
  993. user = self._user_cache.get(message.actor_id)
  994. else:
  995. # get the user and update cache.
  996. directory_member = await client.get_user(user_id=message.actor_id)
  997. if directory_member is None:
  998. return None
  999. user = directory_member.user
  1000. if user is None:
  1001. return None
  1002. self._user_cache[user.id] = user
  1003. if user is None:
  1004. return None
  1005. if not self._check_auth(user):
  1006. return None
  1007. # Todo: use a function that either reads from cache or gets room kind again.
  1008. if self._room_kinds.get(message.room_id) is None:
  1009. room_viewer_state = await client.get_room(message.room_id)
  1010. if room_viewer_state is None:
  1011. return None
  1012. if room_viewer_state.room is None:
  1013. return None
  1014. self._room_kinds[message.room_id] = (
  1015. room_viewer_state.room.kind or RoomKind.UNSPECIFIED
  1016. )
  1017. room_kind = self._room_kinds.get(message.room_id)
  1018. message_body = message.body or ""
  1019. logger.debug("message_body: %s room_kind: %s", message_body, room_kind)
  1020. # Membership commands ride in over DMs only: they change what the bot
  1021. # listens to and must never reach the agent pipeline or the mention
  1022. # gates.
  1023. if room_kind == RoomKind.DM:
  1024. if allow_dm_commands:
  1025. if await self._handle_dm_command(room_id, message_body):
  1026. return None
  1027. elif message_body.startswith("/"):
  1028. logger.debug("Chatto: edited DM command %r not re-run", message_body)
  1029. return None
  1030. # require_mention deliberately gates channels only: in a channel the bot
  1031. # is one of many listeners and must be addressed, whereas a DM is already
  1032. # addressed at it — so DMs are always answered, mention or not.
  1033. mentioned = False
  1034. if (
  1035. room_kind == RoomKind.CHANNEL
  1036. and self.chatto_config.require_mention.value
  1037. and self.me
  1038. ):
  1039. if self.me.login and not mentioned:
  1040. mentioned = bool(f"@{self.me.login}" in message_body)
  1041. if self.me.display_name and not mentioned:
  1042. mentioned = bool(f"@{self.me.display_name}" in message_body)
  1043. if mentioned is False:
  1044. logger.debug(
  1045. "Discarding message. Bot was not mentionend but require_mention is '%s'.",
  1046. self.chatto_config.require_mention.value,
  1047. )
  1048. return None
  1049. logger.debug("mentioned: %s", mentioned)
  1050. # With require_mention off we see every message in the channel, including
  1051. # ones plainly aimed at a named colleague. Answering those would be
  1052. # barging in, so acknowledge that we read it and stay quiet. Checked
  1053. # after the bot-mention test above, so a message naming us *and* someone
  1054. # else still counts as ours.
  1055. if (
  1056. room_kind == RoomKind.CHANNEL
  1057. and not self.chatto_config.require_mention.value
  1058. and not self._mentions_me(message_body)
  1059. and await self._mentions_someone_else(message_body)
  1060. ):
  1061. logger.info("Chatto: message addresses someone else, acknowledging only")
  1062. if self.chatto_config.reactions.value:
  1063. await self.add_reaction(message.room_id, message.id, "🫥")
  1064. return None
  1065. # Thread anchoring — if the incoming message is inside a Chatto thread, we
  1066. # keep that thread by default; otherwise leave thread_id unset so
  1067. # replies land at the root.
  1068. thread_id = (
  1069. thread_root_event_id or None
  1070. ) # we could also take the room id but then, we're in a thread already.
  1071. if not thread_id and room_kind != RoomKind.DM:
  1072. thread_id = message.id
  1073. source = self.build_source(
  1074. chat_id=room_id,
  1075. chat_name=self._room_names.get(message.room_id),
  1076. chat_type=chat_type_for_room_kind(room_kind),
  1077. user_id=message.actor_id,
  1078. user_name=user.login, # use login, because display_name is changeable by anyone.
  1079. thread_id=thread_id,
  1080. message_id=source_message_id,
  1081. role_authorized=True,
  1082. )
  1083. # prepare a MessageEvent
  1084. message_event = MessageEvent(
  1085. text=message_body,
  1086. source=source,
  1087. message_id=message.id,
  1088. timestamp=message.created_at or datetime.now(UTC),
  1089. raw_message=message,
  1090. reply_to_message_id=message.in_reply_to,
  1091. )
  1092. if message_event.is_command():
  1093. message_event.message_type = MessageType.COMMAND
  1094. # Attachments — download and hand the local cache paths to the gateway,
  1095. # which runs vision enrichment / document extraction off media_urls.
  1096. attachments = list(message.attachments or [])
  1097. (
  1098. message_event.media_urls,
  1099. message_event.media_types,
  1100. media_kinds,
  1101. ) = await self._cache_attachments(
  1102. room_id,
  1103. attachments,
  1104. )
  1105. if media_kinds:
  1106. # Same precedence as the Teams/Signal adapters: document-context
  1107. # injection gates strictly on DOCUMENT, image handling keys off the
  1108. # per-path image/* MIME regardless of message_type.
  1109. message_event.message_type = self._message_type_for_media_kinds(media_kinds)
  1110. else:
  1111. message_event.message_type = MessageType.TEXT
  1112. logger.debug("Chatto: MessageEvent: %s", message_event)
  1113. return message_event
  1114. async def _forward_reaction(
  1115. self,
  1116. event: RealtimeEvent,
  1117. payload: ReactionPayload,
  1118. *,
  1119. removed: bool,
  1120. ) -> None:
  1121. """Forward a human reaction to the gateway's reaction hook surface.
  1122. The handler is registered by the gateway via ``set_reaction_handler``
  1123. and fans out as ``reaction:added`` / ``reaction:removed`` through the
  1124. HookRegistry. The dict shape mirrors the Slack adapter's — hook
  1125. consumers are written against that contract, not against a per-platform
  1126. one. Our own lifecycle reactions (👀/✅/❌) are dropped: forwarding them
  1127. would feed the agent its own markers.
  1128. """
  1129. actor_id = event.actor_id
  1130. if actor_id and self.me and actor_id == self.me.id:
  1131. return
  1132. if not payload.room_id or not payload.message_event_id or not actor_id:
  1133. return
  1134. handler = getattr(self, "_reaction_handler", None)
  1135. if handler is None:
  1136. return
  1137. action = "removed" if removed else "added"
  1138. try:
  1139. await handler(
  1140. {
  1141. "platform": ChattoConstants.PLATFORM_NAME,
  1142. "event_name": f"reaction:{action}",
  1143. "reaction": payload.emoji,
  1144. "user_id": actor_id,
  1145. "item_user_id": None,
  1146. "item_type": "message",
  1147. "channel_id": payload.room_id,
  1148. "message_ts": payload.message_event_id,
  1149. "event_ts": event.id,
  1150. "raw_event": event,
  1151. },
  1152. )
  1153. except Exception: # pragma: no cover - the hook contract is non-blocking
  1154. logger.debug("Chatto: reaction hook forwarding failed", exc_info=True)
  1155. async def _handle_realtime_event(self, event: RealtimeEvent) -> None:
  1156. if self._is_seen(event.id):
  1157. return
  1158. if event.actor_id is None:
  1159. return
  1160. logger.debug("EVENT happened: '%s' from %s", event.kind, event.actor_id)
  1161. if (event_payload := event.get("message_posted")) is not None:
  1162. # Self-event filter — the actor_id on the envelope is authoritative
  1163. # (chattolib does NOT filter this itself; see chatto-bridge notes).
  1164. actor_id = event.actor_id
  1165. if actor_id and self.me and actor_id == self.me.id:
  1166. return
  1167. await self._dispatch_message_posted(event_payload)
  1168. elif (edited_payload := event.get("message_edited")) is not None:
  1169. # Same self-event filter: our own streaming edits echo back here.
  1170. # Redeliveries of edits we made are also caught by _mark_seen in
  1171. # edit_message().
  1172. actor_id = event.actor_id
  1173. if actor_id and self.me and actor_id == self.me.id:
  1174. return
  1175. await self._dispatch_message_edited(
  1176. cast(MessageEditedPayload, edited_payload)
  1177. )
  1178. elif event.kind in ("reaction_added", "reaction_removed"):
  1179. reaction_payload = event.get(event.kind)
  1180. if reaction_payload is not None:
  1181. await self._forward_reaction(
  1182. event,
  1183. cast(ReactionPayload, reaction_payload),
  1184. removed=event.kind == "reaction_removed",
  1185. )
  1186. # confirmed:
  1187. elif event.kind in (
  1188. "mention_notification",
  1189. "projection_event",
  1190. "presence_changed",
  1191. "notification_dismissed",
  1192. "room_marked_as_read",
  1193. "user_typing",
  1194. "notification_created",
  1195. "new_direct_message_notification",
  1196. "message_retracted",
  1197. ):
  1198. logger.debug(
  1199. "Chatto: '%s' event received. Not yet implemented or not needed.",
  1200. event.kind,
  1201. )
  1202. else:
  1203. logger.warning("Chatto: unknown event kind: '%s'", event.kind)
  1204. async def _chattolib_event_loop(self) -> None:
  1205. """Event loop using chattolib's stream_events.
  1206. This replaces the manual WebSocket loop with chattolib's high-level
  1207. stream_events() which provides pre-decoded RealtimeEvent objects.
  1208. """
  1209. delay = ChattoConstants.WS_RECONNECT_INITIAL_BACKOFF
  1210. while not self._closing:
  1211. try:
  1212. client = await self._require_chatto_client()
  1213. await self._refresh_rooms()
  1214. logger.info(
  1215. "Chatto: starting chattolib event stream with %d rooms",
  1216. len(self._joined_room_ids),
  1217. )
  1218. async for event in stream_events(client):
  1219. if self._closing:
  1220. return
  1221. await self._handle_realtime_event(event)
  1222. # Iterator exited cleanly — treat as a normal close and reconnect
  1223. # with the local backoff (no server hint available).
  1224. logger.info("Chatto: realtime stream ended, reconnecting")
  1225. except asyncio.CancelledError:
  1226. return
  1227. except ChattoRealtimeCloseError as exc:
  1228. if not exc.reconnect:
  1229. logger.error(
  1230. "Chatto: realtime closed by server (%s: %s), not reconnecting",
  1231. exc.code,
  1232. exc.message,
  1233. )
  1234. return
  1235. wait = max(
  1236. exc.retry_after_ms / 1000.0,
  1237. ChattoConstants.WS_RECONNECT_INITIAL_BACKOFF,
  1238. )
  1239. logger.warning(
  1240. "Chatto: realtime closed by server (%s), reconnecting in %.1fs",
  1241. exc.code,
  1242. wait,
  1243. )
  1244. delay = (
  1245. ChattoConstants.WS_RECONNECT_INITIAL_BACKOFF
  1246. ) # server hint supersedes local backoff
  1247. await self._sleep_interruptible(wait)
  1248. continue
  1249. except ChattoRealtimeError as exc:
  1250. if getattr(exc, "fatal", False):
  1251. logger.error(
  1252. "Chatto: fatal realtime error (%s): %s", exc.code, exc.message
  1253. )
  1254. return
  1255. logger.warning(
  1256. "Chatto: realtime error (%s: %s), reconnecting in %.1fs",
  1257. exc.code,
  1258. exc.message,
  1259. delay,
  1260. )
  1261. except Exception as exc:
  1262. logger.warning(
  1263. "Chatto: unexpected realtime error: %s, reconnecting in %.1fs",
  1264. exc,
  1265. delay,
  1266. )
  1267. if self._closing:
  1268. return
  1269. jitter = delay * 0.2 * random.random()
  1270. await self._sleep_interruptible(delay + jitter)
  1271. delay = min(delay * 2, ChattoConstants.WS_RECONNECT_MAX_BACKOFF)
  1272. async def _sleep_interruptible(self, seconds: float) -> None:
  1273. """Sleep in short slices so disconnect() cancels promptly."""
  1274. end = asyncio.get_running_loop().time() + seconds
  1275. while not self._closing:
  1276. remaining = end - asyncio.get_running_loop().time()
  1277. if remaining <= 0:
  1278. return
  1279. await asyncio.sleep(min(remaining, 0.5))
  1280. def _warn_if_home_channel_unjoined(self, member_ids: set[str]) -> None:
  1281. """Warn once when CHATTO_HOME_CHANNEL names a room the bot is not in.
  1282. Standalone cron delivery posts straight into that room with a fresh
  1283. client and no join logic of its own — without server-side membership
  1284. every proactive send fails there.
  1285. """
  1286. home_id = (self.chatto_config.home_channel.value or "").strip()
  1287. if (
  1288. not home_id
  1289. or home_id in member_ids
  1290. or home_id in self._joined_room_ids
  1291. or self._home_warning_logged
  1292. ):
  1293. return
  1294. self._home_warning_logged = True
  1295. logger.warning(
  1296. "Chatto: CHATTO_HOME_CHANNEL '%s' is not a joined room - cron and "
  1297. "notification delivery will fail until the bot joins it (invite "
  1298. "the account natively in Chatto, or DM it '/join').",
  1299. home_id,
  1300. )
  1301. async def _refresh_rooms(self) -> None:
  1302. """Refresh room list via ConnectRPC, join and seed any newly discovered rooms."""
  1303. client = await self._get_chatto_client()
  1304. if client is None:
  1305. logger.warning("Chatto WS: _refresh_rooms aborted - no client available")
  1306. return
  1307. try:
  1308. rooms_list = await client.list_rooms()
  1309. respond_rooms = self.chatto_config.respond_rooms.value
  1310. member_ids: set[str] = set()
  1311. new_room_ids: list[str] = []
  1312. for room_with_state in rooms_list:
  1313. if not room_with_state:
  1314. continue
  1315. room_obj = room_with_state.room or None
  1316. if not room_obj:
  1317. continue
  1318. self._room_names[room_obj.id] = room_obj.name
  1319. self._room_kinds[room_obj.id] = room_obj.kind
  1320. if room_obj.universal:
  1321. self._universal_room_ids.add(room_obj.id)
  1322. if not room_with_state.viewer_state.is_member:
  1323. continue
  1324. member_ids.add(room_obj.id)
  1325. if room_obj.id not in self._joined_room_ids:
  1326. new_room_ids.append(room_obj.id)
  1327. # Left via /leave, kicked, deleted: rooms we no longer belong
  1328. # to drop out here — otherwise the next refresh would quietly
  1329. # re-add what /leave just removed.
  1330. stale_room_ids = [
  1331. rid for rid in self._joined_room_ids if rid not in member_ids
  1332. ]
  1333. for rid in stale_room_ids:
  1334. self._joined_room_ids.remove(rid)
  1335. if stale_room_ids:
  1336. logger.info(
  1337. "Chatto WS: no longer a member of %d room(s): %s",
  1338. len(stale_room_ids),
  1339. stale_room_ids,
  1340. )
  1341. unjoined_listed = [rid for rid in respond_rooms if rid not in member_ids]
  1342. if unjoined_listed:
  1343. logger.warning(
  1344. "Chatto WS: CHATTO_RESPOND_ROOMS lists room(s) we are not a"
  1345. " member of: %s",
  1346. unjoined_listed,
  1347. )
  1348. self._warn_if_home_channel_unjoined(member_ids)
  1349. if not new_room_ids:
  1350. return
  1351. logger.info(
  1352. "Chatto WS: discovered %d new room(s): %s",
  1353. len(new_room_ids),
  1354. new_room_ids,
  1355. )
  1356. for rid in new_room_ids:
  1357. if self._room_kinds.get(rid) != RoomKind.DM:
  1358. await client.join_room(
  1359. rid
  1360. ) # but list_rooms() would not return any if we were not already joined?!
  1361. if self._is_respond_room(rid):
  1362. await self._seed_room(rid)
  1363. else:
  1364. logger.info(
  1365. "Chatto WS: %s (%s) is outside CHATTO_RESPOND_ROOMS -"
  1366. " joined read-only",
  1367. self._room_names.get(rid, rid),
  1368. rid,
  1369. )
  1370. self._joined_room_ids.append(rid)
  1371. joined_room_names: list[str] = []
  1372. for rid in self._joined_room_ids:
  1373. name = self._room_names[rid]
  1374. if not self._is_respond_room(rid):
  1375. name += " [read-only]"
  1376. if rid in self._universal_room_ids:
  1377. name += " [universal]"
  1378. joined_room_names.append(name + " (" + rid + ")")
  1379. logger.info(
  1380. "Chatto WS: currently joined in %d room(s): %s",
  1381. len(self._joined_room_ids),
  1382. ", ".join(joined_room_names),
  1383. )
  1384. except Exception:
  1385. logger.warning("Chatto WS: _refresh_rooms failed", exc_info=True)
  1386. # ------------------------------------------------------------------ #
  1387. # Read state & notification dismissal (best-effort, Chatto-unique)
  1388. # ------------------------------------------------------------------ #
  1389. # Best-effort: mark all joined rooms as read (room_id may be undefined here)
  1390. for _rid in list(self._joined_room_ids):
  1391. try:
  1392. await client.mark_room_as_read(room_id=_rid)
  1393. await client.dismiss_all_notifications()
  1394. except Exception:
  1395. logger.debug(
  1396. "Chatto: mark_room_as_read failed for %s", _rid, exc_info=True
  1397. )
  1398. # ------------------------------------------------------------------ #
  1399. # Sending (ConnectRPC — unchanged from polling version)
  1400. # ------------------------------------------------------------------ #
  1401. async def send(
  1402. self,
  1403. chat_id: str,
  1404. content: str,
  1405. reply_to: str | None = None,
  1406. metadata: dict[str, Any] | None = None,
  1407. ) -> SendResult:
  1408. """Send a message to a Chatto room.
  1409. Long messages are split into chunks via ``truncate_message`` and
  1410. each chunk is sent as a separate CreateMessage call. The first
  1411. chunk's message ID is returned as ``message_id``.
  1412. When ``auto_thread`` is enabled and the incoming message was a
  1413. regular room message (not already in a thread), the first chunk is
  1414. sent as a room message and its ID becomes the thread root. Subsequent
  1415. chunks are sent in that thread. This mirrors Discord's auto_thread
  1416. behavior.
  1417. BasePlatformAdapter override
  1418. """
  1419. if not content:
  1420. return SendResult(success=False, error="Empty message")
  1421. formatted = self.format_message(content)
  1422. chunks = self.truncate_message(formatted, ChattoConstants.MAX_MESSAGE_LENGTH)
  1423. # Thread support — resolve thread_id once
  1424. # DM rooms don't support threads, so skip threading for DMs
  1425. thread_id = (metadata or {}).get("thread_id")
  1426. # Only use reply_to as thread_id if auto_thread is enabled.
  1427. # When auto_thread=false, responses go directly in the room
  1428. # without threading under the incoming message.
  1429. # If we already have thread_id from metadata, keep it (it's the
  1430. # thread root); only use reply_to when we don't already have one.
  1431. if reply_to and self.chatto_config.auto_thread.value and not thread_id:
  1432. thread_id = reply_to
  1433. # Check if this is a DM room — DMs don't support threads
  1434. room_kind = self._room_kinds.get(chat_id)
  1435. is_dm = room_kind == RoomKind.DM
  1436. if is_dm:
  1437. thread_id = None
  1438. # Auto-thread: by default, Chatto creates a thread for replies to room
  1439. # messages (not DMs, not already in a thread). This keeps conversations
  1440. # organized in the room. Can be disabled via extra.auto_thread=false.
  1441. use_auto_thread = (
  1442. self.chatto_config.auto_thread.value and not thread_id and not is_dm
  1443. )
  1444. message_ids: list[str] = []
  1445. last_error: str | None = None
  1446. retryable = False
  1447. client = await self._get_chatto_client()
  1448. if client is None:
  1449. return SendResult(
  1450. success=False, error="Chatto client not available", retryable=True
  1451. )
  1452. for i, chunk in enumerate(chunks):
  1453. try:
  1454. msg_obj = await client.post_message(
  1455. room_id=chat_id,
  1456. body=chunk,
  1457. thread_root_event_id=str(thread_id) if thread_id else "",
  1458. )
  1459. except ChattoError as e:
  1460. last_error = str(e)
  1461. retryable = True
  1462. break
  1463. except Exception as e:
  1464. last_error = str(e)
  1465. retryable = True
  1466. break
  1467. self._mark_seen(msg_obj.id)
  1468. message_ids.append(msg_obj.id)
  1469. self._our_message_ids.add(msg_obj.id)
  1470. # If we sent a message WITHOUT a thread_id, this message could
  1471. # become a thread root if someone replies to it
  1472. if not thread_id:
  1473. self._our_thread_roots.add(msg_obj.id)
  1474. # Auto-thread: first chunk becomes the thread root,
  1475. # subsequent chunks go in the thread
  1476. if use_auto_thread and i == 0 and not thread_id:
  1477. thread_id = msg_obj.id
  1478. # Nothing got through at all — report the failure instead of a phantom success.
  1479. if not message_ids:
  1480. return SendResult(
  1481. success=False,
  1482. error=last_error or "Chatto: message could not be sent",
  1483. retryable=retryable,
  1484. )
  1485. first_id = message_ids[0]
  1486. # ------------------------------------------------------------------ #
  1487. # Thread following (best-effort, Chatto-unique)
  1488. # ------------------------------------------------------------------ #
  1489. if thread_id:
  1490. try:
  1491. await client.follow_thread(chat_id, thread_id)
  1492. except Exception:
  1493. logger.debug(
  1494. "Chatto: follow_thread failed for %s/%s",
  1495. chat_id,
  1496. thread_id,
  1497. exc_info=True,
  1498. )
  1499. # A later chunk failed after earlier ones went out: partial delivery.
  1500. if last_error:
  1501. logger.warning(
  1502. "Chatto: sent %d/%d chunk(s) to %s before failing: %s",
  1503. len(message_ids),
  1504. len(chunks),
  1505. chat_id,
  1506. last_error,
  1507. )
  1508. # raw_response stays unset (dict-shaped per the SendResult contract):
  1509. # gateway consumers such as the cron scheduler call .get() on it, so a
  1510. # chattolib Message here would crash delivery bookkeeping *after* the
  1511. # send already succeeded — the job then falls back to the standalone
  1512. # path and the room sees the message twice.
  1513. return SendResult(success=True, message_id=first_id)
  1514. def format_message(self, content: str) -> str:
  1515. """Normalise outgoing text for Chatto.
  1516. Chatto renders Markdown natively, so there is nothing to escape or
  1517. translate — the only transformations here are the ones that measurably
  1518. render wrong: CRLF line endings (which show up as stray blank lines)
  1519. and runs of more than two blank lines.
  1520. BasePlatformAdapter override
  1521. """
  1522. if not content:
  1523. return content
  1524. normalised = content.replace("\r\n", "\n").replace("\r", "\n")
  1525. while "\n\n\n\n" in normalised:
  1526. normalised = normalised.replace("\n\n\n\n", "\n\n\n")
  1527. return normalised
  1528. async def edit_message(
  1529. self,
  1530. chat_id: str,
  1531. message_id: str,
  1532. content: str,
  1533. *,
  1534. finalize: bool = False,
  1535. ) -> SendResult:
  1536. """Edit a message we previously sent, via MessageService/UpdateMessage.
  1537. The stream consumer drives streaming replies through this: without the
  1538. override the base class reports "Not supported" and every incremental
  1539. update arrives as a *new* message.
  1540. ``finalize`` is a no-op for Chatto — an edit is an edit here, there is
  1541. no in-progress card state to close out (hence no
  1542. ``REQUIRES_EDIT_FINALIZE``).
  1543. Content that exceeds the per-message limit is refused rather than
  1544. silently truncated, so the caller falls back to ``send()``, which
  1545. splits across messages.
  1546. BasePlatformAdapter override
  1547. """
  1548. if not message_id:
  1549. return SendResult(success=False, error="Chatto: no message id to edit")
  1550. if not content:
  1551. return SendResult(success=False, error="Empty message")
  1552. formatted = self.format_message(content)
  1553. if len(formatted) > ChattoConstants.MAX_MESSAGE_LENGTH:
  1554. # Refuse instead of truncating: the caller's fallback path splits.
  1555. return SendResult(
  1556. success=False,
  1557. error=(
  1558. f"Chatto: edit exceeds {ChattoConstants.MAX_MESSAGE_LENGTH} "
  1559. f"chars ({len(formatted)})"
  1560. ),
  1561. )
  1562. client = await self._get_chatto_client()
  1563. if client is None:
  1564. return SendResult(
  1565. success=False, error="Chatto client not available", retryable=True
  1566. )
  1567. try:
  1568. msg = await client.update_message(
  1569. room_id=str(chat_id),
  1570. event_id=str(message_id),
  1571. body=formatted,
  1572. )
  1573. except ChattoError as e:
  1574. logger.warning("Chatto: UpdateMessage failed for %s: %s", message_id, e)
  1575. return SendResult(success=False, error=str(e), retryable=True)
  1576. except Exception as e:
  1577. logger.warning("Chatto: UpdateMessage error for %s: %s", message_id, e)
  1578. return SendResult(success=False, error=str(e), retryable=False)
  1579. # Our own edit comes back as a message_edited event; mark it seen so it
  1580. # is never mistaken for inbound traffic.
  1581. edited_id = getattr(msg, "id", "") or str(message_id)
  1582. self._mark_seen(edited_id)
  1583. return SendResult(success=True, message_id=edited_id)
  1584. async def delete_message(self, chat_id: str, message_id: str) -> bool:
  1585. """Delete a message via MessageService/DeleteMessage.
  1586. Used by the stream consumer's fresh-final cleanup (removing a preview
  1587. message once the completed reply has been sent) and by the ephemeral
  1588. reply TTL.
  1589. BasePlatformAdapter override
  1590. """
  1591. if not chat_id or not message_id:
  1592. return False
  1593. client = await self._get_chatto_client()
  1594. if client is None:
  1595. logger.warning("Chatto: DeleteMessage — client unavailable")
  1596. return False
  1597. try:
  1598. return bool(
  1599. await client.delete_message(
  1600. room_id=str(chat_id),
  1601. event_id=str(message_id),
  1602. )
  1603. )
  1604. except ChattoError as e:
  1605. logger.warning("Chatto: DeleteMessage failed for %s: %s", message_id, e)
  1606. return False
  1607. except Exception as e:
  1608. logger.warning("Chatto: DeleteMessage error for %s: %s", message_id, e)
  1609. return False
  1610. async def create_handoff_thread(
  1611. self,
  1612. parent_chat_id: str,
  1613. name: str,
  1614. ) -> str | None:
  1615. """Anchor a session handoff in a fresh thread under *parent_chat_id*.
  1616. Chatto threads hang off a message, not off the room, so we post a seed
  1617. message and hand its ID back as the thread root — the same shape the
  1618. Slack adapter uses. DMs don't support threads, so they get ``None``
  1619. and the watcher keeps delivering into the DM itself.
  1620. BasePlatformAdapter override
  1621. """
  1622. if not parent_chat_id:
  1623. return None
  1624. if self._room_kinds.get(parent_chat_id) == RoomKind.DM:
  1625. logger.debug("Chatto: handoff thread skipped — %s is a DM", parent_chat_id)
  1626. return None
  1627. client = await self._get_chatto_client()
  1628. if client is None:
  1629. logger.warning("Chatto: handoff thread — client unavailable")
  1630. return None
  1631. seed_text = f"🧵 Hermes handoff — **{(name or 'session').strip()[:80]}**"
  1632. try:
  1633. msg = await client.post_message(room_id=str(parent_chat_id), body=seed_text)
  1634. except Exception as e:
  1635. logger.warning(
  1636. "Chatto: handoff thread seed-post failed for room %s: %s",
  1637. parent_chat_id,
  1638. e,
  1639. )
  1640. return None
  1641. seed_id = getattr(msg, "id", "") or ""
  1642. if not seed_id:
  1643. logger.warning("Chatto: handoff thread seed-post returned no message id")
  1644. return None
  1645. self._mark_seen(seed_id)
  1646. self._our_message_ids.add(seed_id)
  1647. self._our_thread_roots.add(seed_id)
  1648. try:
  1649. await client.follow_thread(str(parent_chat_id), seed_id)
  1650. except Exception:
  1651. logger.debug(
  1652. "Chatto: follow_thread failed for handoff %s/%s",
  1653. parent_chat_id,
  1654. seed_id,
  1655. exc_info=True,
  1656. )
  1657. return seed_id
  1658. # Overridden from BaseAdapter:
  1659. async def send_typing(self, chat_id: str, metadata=None) -> None:
  1660. """Start a persistent typing indicator for a room.
  1661. Sends a typing ping every 10 seconds (Chatto's indicator likely
  1662. lasts ~8-10s). The background loop runs until ``stop_typing()``
  1663. is called or the task is cancelled.
  1664. BasePlatformAdapter override
  1665. """
  1666. if chat_id in self._typing_tasks:
  1667. return # already running
  1668. async def _typing_loop() -> None:
  1669. try:
  1670. while True:
  1671. try:
  1672. client = await self._get_chatto_client()
  1673. if client is None:
  1674. return
  1675. await client.update_typing_indicator(room_id=str(chat_id))
  1676. except asyncio.CancelledError:
  1677. return
  1678. except Exception:
  1679. logger.debug(
  1680. "Chatto: typing indicator refresh failed for %s",
  1681. chat_id,
  1682. exc_info=True,
  1683. )
  1684. await asyncio.sleep(10)
  1685. except asyncio.CancelledError:
  1686. pass
  1687. finally:
  1688. self._typing_tasks.pop(chat_id, None)
  1689. self._typing_tasks[chat_id] = asyncio.create_task(_typing_loop())
  1690. async def stop_typing(self, chat_id: str) -> None:
  1691. """Stop the persistent typing indicator for a room.
  1692. BasePlatformAdapter override
  1693. """
  1694. task = self._typing_tasks.pop(chat_id, None)
  1695. if task:
  1696. task.cancel()
  1697. try:
  1698. await task
  1699. except (asyncio.CancelledError, Exception):
  1700. logger.debug("Chatto: typing task ended for %s", chat_id, exc_info=True)
  1701. async def get_chat_info(self, chat_id: str) -> dict[str, Any]:
  1702. """Get information about a chat/room.
  1703. BasePlatformAdapter override
  1704. """
  1705. name = self._room_names.get(chat_id, chat_id)
  1706. kind = self._room_kinds.get(chat_id)
  1707. return {
  1708. "name": name,
  1709. "type": chat_type_for_room_kind(kind).value,
  1710. }
  1711. # ------------------------------------------------------------------ #
  1712. # Reactions
  1713. # ------------------------------------------------------------------ #
  1714. @staticmethod
  1715. def _emoji_to_shortcode(emoji: str) -> str:
  1716. """Convert a unicode emoji to a Chatto shortcode name.
  1717. If the emoji is already a shortcode (no unicode mapping found),
  1718. return it as-is.
  1719. """
  1720. shortcode = ChattoConstants.EMOJI_TO_SHORTCODE.get(emoji)
  1721. if shortcode:
  1722. return shortcode
  1723. # Already a shortcode like "thumbsup" — return as-is
  1724. return emoji
  1725. async def add_reaction(self, room_id: str, message_id: str, emoji: str) -> bool:
  1726. """Add a reaction to a message via MessageService/AddReaction."""
  1727. shortcode = self._emoji_to_shortcode(emoji)
  1728. client = await self._get_chatto_client()
  1729. if client is None:
  1730. logger.warning("Chatto: AddReaction — client unavailable")
  1731. return False
  1732. try:
  1733. result = await client.add_reaction(
  1734. room_id=room_id,
  1735. message_event_id=message_id,
  1736. emoji=shortcode,
  1737. )
  1738. return result
  1739. except ChattoError as e:
  1740. logger.warning("Chatto: AddReaction failed: %s", e)
  1741. return False
  1742. except Exception as e:
  1743. logger.warning("Chatto: AddReaction error: %s", e)
  1744. return False
  1745. async def remove_reaction(self, chat_id: str, message_id: str, emoji: str) -> bool:
  1746. """Remove a reaction from a message via MessageService/RemoveReaction."""
  1747. shortcode = self._emoji_to_shortcode(emoji)
  1748. client = await self._get_chatto_client()
  1749. if client is None:
  1750. logger.warning("Chatto: RemoveReaction — client unavailable")
  1751. return False
  1752. try:
  1753. result = await client.remove_reaction(
  1754. room_id=str(chat_id),
  1755. message_event_id=str(message_id),
  1756. emoji=shortcode,
  1757. )
  1758. return result
  1759. except ChattoError as e:
  1760. logger.warning("Chatto: RemoveReaction failed: %s", e)
  1761. return False
  1762. except Exception as e:
  1763. logger.warning("Chatto: RemoveReaction error: %s", e)
  1764. return False
  1765. # ------------------------------------------------------------------ #
  1766. # DM initiation (Chatto-unique)
  1767. # ------------------------------------------------------------------ #
  1768. async def start_dm(self, user_id: str) -> str | None:
  1769. """Start a direct message with a user via RoomService/StartDM.
  1770. Returns the room ID on success, or None on failure.
  1771. """
  1772. if not user_id:
  1773. return None
  1774. client = await self._get_chatto_client()
  1775. if client is None:
  1776. return None
  1777. try:
  1778. room = await client.start_dm(participant_ids=[str(user_id)])
  1779. self._room_names[room.id] = room.name
  1780. self._room_kinds[room.id] = room.kind
  1781. return room.id
  1782. except ChattoError as e:
  1783. logger.debug("Chatto: StartDM failed: %s", e)
  1784. return None
  1785. except Exception as e:
  1786. logger.debug("Chatto: StartDM error: %s", e)
  1787. return None
  1788. # ------------------------------------------------------------------ #
  1789. # Room creation (Chatto-unique)
  1790. # ------------------------------------------------------------------ #
  1791. async def create_room(
  1792. self,
  1793. name: str,
  1794. description: str = "",
  1795. group_id: str = "",
  1796. universal: bool = True,
  1797. ) -> str | None:
  1798. """Create an ad-hoc room via RoomService/CreateRoom.
  1799. Returns the room ID on success, or None on failure.
  1800. """
  1801. client = await self._get_chatto_client()
  1802. if client is None:
  1803. return None
  1804. try:
  1805. room = await client.create_room(
  1806. name=name,
  1807. group_id=group_id or "",
  1808. description=description,
  1809. universal=universal,
  1810. )
  1811. rid = str(room.id) if room else ""
  1812. if rid:
  1813. self._room_names[rid] = room.name
  1814. self._room_kinds[rid] = room.kind
  1815. return rid
  1816. logger.debug("Chatto: CreateRoom returned no room id")
  1817. return None
  1818. except ChattoError as e:
  1819. logger.debug("Chatto: CreateRoom failed: %s", e)
  1820. return None
  1821. except Exception as e:
  1822. logger.debug("Chatto: CreateRoom error: %s", e)
  1823. return None
  1824. # ------------------------------------------------------------------ #
  1825. # Processing lifecycle hooks (reactions-based, like Discord)
  1826. # ------------------------------------------------------------------ #
  1827. def _event_room_and_message_id(self, event: MessageEvent) -> tuple[str, str]:
  1828. """Extract room_id and message_id from a MessageEvent."""
  1829. message_id = event.message_id or ""
  1830. chat_id = event.source.chat_id
  1831. return chat_id, message_id
  1832. async def on_processing_start(self, event: MessageEvent) -> None:
  1833. """Record the turn as open, then add an 👀 (eyes) reaction.
  1834. The record is what lets an edit of this very message be recognized as
  1835. a mid-run correction. It must be maintained even when reactions are
  1836. disabled — tracking and decorating are independent concerns.
  1837. BasePlatformAdapter override
  1838. """
  1839. session_key = (
  1840. self._session_key_for(event.source) if event.source is not None else ""
  1841. )
  1842. if session_key and event.message_id:
  1843. self._processing[session_key] = str(event.message_id)
  1844. if not self.chatto_config.reactions.value:
  1845. return
  1846. chat_id, message_id = self._event_room_and_message_id(event)
  1847. if not chat_id or not message_id:
  1848. # Routine, not a fault: the gateway runs agent-initiated turns
  1849. # (heartbeat polls, goal continuations) through the same pipeline
  1850. # with message_id=None, and there is no inbound message to mark.
  1851. logger.debug(
  1852. "Chatto: nothing to react to (chat_id=%r, message_id=%r)",
  1853. chat_id,
  1854. message_id,
  1855. )
  1856. return
  1857. await self.add_reaction(chat_id, message_id, "👀")
  1858. async def on_processing_complete(
  1859. self,
  1860. event: MessageEvent,
  1861. outcome: ProcessingOutcome,
  1862. ) -> None:
  1863. """Close the turn's record, then swap 👀 for ✅/❌/🚫.
  1864. Fires for every outcome, including CANCELLED (mid-run edit
  1865. correction) — this is what re-arms `_processing` before the corrected
  1866. turn is dispatched.
  1867. BasePlatformAdapter override
  1868. """
  1869. session_key = (
  1870. self._session_key_for(event.source) if event.source is not None else ""
  1871. )
  1872. if session_key and event.message_id:
  1873. self._processing.pop(session_key, None)
  1874. if not self.chatto_config.reactions.value:
  1875. return
  1876. chat_id, message_id = self._event_room_and_message_id(event)
  1877. if not chat_id or not message_id:
  1878. return
  1879. # Remove the processing eyes reaction
  1880. await self.remove_reaction(chat_id, message_id, "👀")
  1881. # Add the outcome reaction
  1882. if outcome == ProcessingOutcome.SUCCESS:
  1883. await self.add_reaction(chat_id, message_id, "✅")
  1884. elif outcome == ProcessingOutcome.FAILURE:
  1885. await self.add_reaction(chat_id, message_id, "❌")
  1886. elif outcome == ProcessingOutcome.CANCELLED:
  1887. await self.add_reaction(chat_id, message_id, "🚫")
  1888. # ------------------------------------------------------------------ #
  1889. # Asset upload (chunked)
  1890. # ------------------------------------------------------------------ #
  1891. async def _upload_asset(self, room_id: str, file_path: str) -> str | None:
  1892. """Upload a file via the chunked AssetUploadService.
  1893. Returns the asset ID on success, or None on failure.
  1894. """
  1895. try:
  1896. file_data = await asyncio.to_thread(_read_file_bytes, file_path)
  1897. except Exception as e:
  1898. logger.error("Chatto: failed to read file %s — %s", file_path, e)
  1899. return None
  1900. if not file_data:
  1901. logger.error("Chatto: file %s is empty", file_path)
  1902. return None
  1903. file_size = len(file_data)
  1904. file_name = os.path.basename(file_path)
  1905. mime_type = mimetypes.guess_type(file_path)[0] or "application/octet-stream"
  1906. sha256_hash = hashlib.sha256(file_data).hexdigest()
  1907. client = await self._get_chatto_client()
  1908. if client is None:
  1909. logger.error("Chatto: upload aborted - no client available")
  1910. return None
  1911. try:
  1912. # Step 1: Create upload session
  1913. upload = await client.create_upload(
  1914. room_id=room_id,
  1915. filename=file_name,
  1916. size=file_size,
  1917. sha256=sha256_hash,
  1918. content_type=mime_type,
  1919. )
  1920. # AssetUpload names this upload_id, not id — reading it through an
  1921. # untyped getattr default is what let the mismatch reach production.
  1922. upload_id = upload.upload_id
  1923. if not upload_id:
  1924. logger.error("Chatto: CreateUpload returned no upload ID")
  1925. return None
  1926. # Step 2: Upload chunks
  1927. offset = 0
  1928. while offset < file_size:
  1929. chunk = file_data[offset : offset + ChattoConstants.UPLOAD_CHUNK_SIZE]
  1930. chunk_sha256 = hashlib.sha256(chunk).hexdigest()
  1931. await client.upload_chunk(
  1932. upload_id=upload_id,
  1933. offset=offset,
  1934. content=chunk,
  1935. chunk_sha256=chunk_sha256,
  1936. )
  1937. offset += len(chunk)
  1938. # Step 3: Complete upload
  1939. upload, asset = await client.complete_upload(upload_id=upload_id)
  1940. if not asset:
  1941. logger.error("Chatto: CompleteUpload returned no asset")
  1942. return None
  1943. asset_id = str(getattr(cast(Any, asset), "id", ""))
  1944. logger.info(
  1945. "Chatto: uploaded %s as asset %s (%d bytes)",
  1946. file_name,
  1947. asset_id,
  1948. file_size,
  1949. )
  1950. return asset_id
  1951. except ChattoError as e:
  1952. logger.error("Chatto: upload failed: %s", e)
  1953. return None
  1954. except Exception as e:
  1955. logger.error("Chatto: upload error: %s", e)
  1956. return None
  1957. async def _post_attachment_message(
  1958. self,
  1959. chat_id: str,
  1960. asset_ids: list[str],
  1961. caption: str | None,
  1962. reply_to: str | None,
  1963. metadata: dict[str, Any] | None,
  1964. ) -> SendResult:
  1965. """Post one message carrying already-uploaded assets."""
  1966. thread_id = (metadata or {}).get("thread_id")
  1967. if reply_to:
  1968. thread_id = reply_to
  1969. client = await self._get_chatto_client()
  1970. if client is None:
  1971. return SendResult(
  1972. success=False, error="Chatto client not available", retryable=True
  1973. )
  1974. try:
  1975. msg = await client.post_message(
  1976. room_id=str(chat_id),
  1977. body=self.format_message(caption) if caption else "",
  1978. attachment_asset_ids=asset_ids,
  1979. thread_root_event_id=str(thread_id) if thread_id else "",
  1980. )
  1981. self._mark_seen(msg.id)
  1982. self._our_message_ids.add(msg.id)
  1983. return SendResult(success=True, message_id=msg.id)
  1984. except ChattoError as e:
  1985. return SendResult(success=False, error=str(e), retryable=True)
  1986. except Exception as e:
  1987. return SendResult(success=False, error=str(e), retryable=False)
  1988. async def _send_local_attachment(
  1989. self,
  1990. chat_id: str,
  1991. file_path: str,
  1992. caption: str | None,
  1993. reply_to: str | None,
  1994. metadata: dict[str, Any] | None,
  1995. *,
  1996. kind: str,
  1997. ) -> SendResult:
  1998. """Upload a local file and post it as a native Chatto attachment.
  1999. Shared by ``send_image_file``/``send_document``/``send_video``/
  2000. ``send_voice`` — the upload mechanics are identical, only the wording of
  2001. the failure notice differs. On failure we send that notice as text and
  2002. never the host path (it leaks the Hermes home layout).
  2003. """
  2004. notice = f"⚠️ Couldn't deliver the {kind} attachment."
  2005. safe_path = self.validate_media_delivery_path(file_path)
  2006. if not safe_path:
  2007. logger.warning(
  2008. "[%s] send %s: unsafe path %s",
  2009. self.name,
  2010. kind,
  2011. file_path,
  2012. )
  2013. text = f"{caption}\n{notice}" if caption else notice
  2014. return await self.send(chat_id, text, reply_to=reply_to, metadata=metadata)
  2015. asset_id = await self._upload_asset(str(chat_id), safe_path)
  2016. if not asset_id:
  2017. logger.warning(
  2018. "[%s] send %s: upload failed for %s",
  2019. self.name,
  2020. kind,
  2021. safe_path,
  2022. )
  2023. text = f"{caption}\n{notice}" if caption else notice
  2024. return await self.send(chat_id, text, reply_to=reply_to, metadata=metadata)
  2025. return await self._post_attachment_message(
  2026. chat_id,
  2027. [asset_id],
  2028. caption,
  2029. reply_to,
  2030. metadata,
  2031. )
  2032. async def send_image_file(
  2033. self,
  2034. chat_id: str,
  2035. image_path: str,
  2036. caption: str | None = None,
  2037. reply_to: str | None = None,
  2038. metadata: dict[str, Any] | None = None,
  2039. **kwargs,
  2040. ) -> SendResult:
  2041. """Send a local image file via the chunked upload API.
  2042. The parameter is ``image_path``, not ``file_path``: every caller passes
  2043. it by keyword (``gateway/run.py:22354``, ``:22470``, and the base class's
  2044. own ``send_multiple_images`` file:// branch), so a renamed parameter
  2045. makes each of those raise TypeError and silently degrade to a text
  2046. notice.
  2047. BasePlatformAdapter override
  2048. """
  2049. return await self._send_local_attachment(
  2050. chat_id,
  2051. image_path,
  2052. caption,
  2053. reply_to,
  2054. metadata,
  2055. kind="image",
  2056. )
  2057. async def send_document(
  2058. self,
  2059. chat_id: str,
  2060. file_path: str,
  2061. caption: str | None = None,
  2062. file_name: str | None = None,
  2063. reply_to: str | None = None,
  2064. metadata: dict[str, Any] | None = None,
  2065. **kwargs,
  2066. ) -> SendResult:
  2067. """Send a local file as a native Chatto attachment.
  2068. ``file_name`` is the user-facing name the agent chose; Chatto takes the
  2069. filename from the upload session, so it only matters for the failure
  2070. notice.
  2071. BasePlatformAdapter override
  2072. """
  2073. result = await self._send_local_attachment(
  2074. chat_id,
  2075. file_path,
  2076. caption,
  2077. reply_to,
  2078. metadata,
  2079. kind="file",
  2080. )
  2081. if not result.success and file_name:
  2082. logger.debug("Chatto: document delivery failed for %s", file_name)
  2083. return result
  2084. async def send_video(
  2085. self,
  2086. chat_id: str,
  2087. video_path: str,
  2088. caption: str | None = None,
  2089. reply_to: str | None = None,
  2090. metadata: dict[str, Any] | None = None,
  2091. **kwargs,
  2092. ) -> SendResult:
  2093. """Send a local video as a native Chatto attachment.
  2094. Chatto transcodes and plays it inline.
  2095. BasePlatformAdapter override
  2096. """
  2097. return await self._send_local_attachment(
  2098. chat_id,
  2099. video_path,
  2100. caption,
  2101. reply_to,
  2102. metadata,
  2103. kind="video",
  2104. )
  2105. async def send_voice(
  2106. self,
  2107. chat_id: str,
  2108. audio_path: str,
  2109. caption: str | None = None,
  2110. reply_to: str | None = None,
  2111. metadata: dict[str, Any] | None = None,
  2112. **kwargs,
  2113. ) -> SendResult:
  2114. """Send a local audio file as a native Chatto attachment.
  2115. Chatto has no dedicated voice-bubble type, so this is an ordinary audio
  2116. attachment — still far better than the base class's text notice.
  2117. BasePlatformAdapter override
  2118. """
  2119. return await self._send_local_attachment(
  2120. chat_id,
  2121. audio_path,
  2122. caption,
  2123. reply_to,
  2124. metadata,
  2125. kind="audio",
  2126. )
  2127. async def send_image(
  2128. self,
  2129. chat_id: str,
  2130. image_url: str,
  2131. caption: str | None = None,
  2132. reply_to: str | None = None,
  2133. metadata: dict[str, Any] | None = None,
  2134. ) -> SendResult:
  2135. """Send an image to a Chatto room.
  2136. Tries to download the image from the URL and upload it as a native
  2137. attachment. Falls back to sending the URL as a link (Chatto renders
  2138. link previews) if the download fails.
  2139. BasePlatformAdapter override
  2140. """
  2141. # Try downloading and uploading as attachment
  2142. try:
  2143. import tempfile
  2144. # Download to a temp file
  2145. parsed = urlsplit(image_url)
  2146. url_path = parsed.path
  2147. ext = os.path.splitext(url_path)[1] or ".png"
  2148. tmp_fd, tmp_path = tempfile.mkstemp(suffix=ext, prefix="chatto_img_")
  2149. try:
  2150. os.close(tmp_fd)
  2151. await asyncio.to_thread(
  2152. _download_to_file,
  2153. image_url,
  2154. tmp_path,
  2155. ChattoConstants.HTTP_TIMEOUT,
  2156. )
  2157. # Upload as attachment
  2158. result = await self.send_image_file(
  2159. chat_id,
  2160. tmp_path,
  2161. caption=caption,
  2162. reply_to=reply_to,
  2163. metadata=metadata,
  2164. )
  2165. if result.success:
  2166. return result
  2167. finally:
  2168. try:
  2169. os.unlink(tmp_path)
  2170. except OSError:
  2171. pass
  2172. except Exception as e:
  2173. logger.debug(
  2174. "Chatto: send_image download/upload failed, falling back to link: %s", e
  2175. )
  2176. # Fallback: send as link (Chatto renders link previews)
  2177. text = image_url
  2178. if caption:
  2179. text = f"{caption}\n{image_url}"
  2180. return await self.send(chat_id, text, reply_to=reply_to, metadata=metadata)
  2181. async def _materialise_image(self, image_url: str) -> tuple[str | None, bool]:
  2182. """Resolve one ``send_multiple_images`` entry to a local file path.
  2183. Accepts ``http(s)://`` URLs (downloaded to a temp file), ``file://``
  2184. URIs and bare paths. Returns ``(path, is_temp)`` — the caller unlinks
  2185. when ``is_temp``. ``(None, False)`` means the entry is unusable.
  2186. """
  2187. import tempfile
  2188. from urllib.parse import unquote as _unquote
  2189. if image_url.startswith(("http://", "https://")):
  2190. parsed = urlsplit(image_url)
  2191. ext = os.path.splitext(parsed.path)[1] or ".png"
  2192. tmp_fd, tmp_path = tempfile.mkstemp(suffix=ext, prefix="chatto_img_")
  2193. os.close(tmp_fd)
  2194. try:
  2195. data = await self._download_attachment_bytes(image_url)
  2196. await asyncio.to_thread(_write_file_bytes, tmp_path, data)
  2197. except Exception as e:
  2198. logger.warning("Chatto: image download failed for %s: %s", image_url, e)
  2199. try:
  2200. os.unlink(tmp_path)
  2201. except OSError:
  2202. pass
  2203. return None, False
  2204. return tmp_path, True
  2205. local = image_url
  2206. if local.startswith("file://"):
  2207. local = _unquote(urlsplit(local).path)
  2208. return self.validate_media_delivery_path(local), False
  2209. async def send_multiple_images(
  2210. self,
  2211. chat_id: str,
  2212. images: list[tuple[str, str]],
  2213. metadata: dict[str, Any] | None = None,
  2214. human_delay: float = 0.0,
  2215. ) -> None:
  2216. """Send a batch of images as ONE message with several attachments.
  2217. The base implementation posts each image separately; a Chatto message
  2218. carries a list of attachment assets, so a batch belongs in a single
  2219. message (and a single notification).
  2220. ``human_delay`` is ignored deliberately — there is only one outbound
  2221. call to pace. Entries that can't be fetched are dropped with a warning;
  2222. if nothing survives, we fall back to the base class so the user still
  2223. gets the links.
  2224. BasePlatformAdapter override
  2225. """
  2226. if len(images or []) < 2:
  2227. await super().send_multiple_images(
  2228. chat_id,
  2229. images,
  2230. metadata=metadata,
  2231. human_delay=human_delay,
  2232. )
  2233. return
  2234. asset_ids: list[str] = []
  2235. captions: list[str] = []
  2236. for image_url, alt_text in images:
  2237. path, is_temp = await self._materialise_image(image_url)
  2238. if not path:
  2239. logger.warning("Chatto: skipping unusable image %s", image_url)
  2240. continue
  2241. try:
  2242. asset_id = await self._upload_asset(str(chat_id), path)
  2243. finally:
  2244. if is_temp:
  2245. try:
  2246. os.unlink(path)
  2247. except OSError:
  2248. pass
  2249. if not asset_id:
  2250. logger.warning("Chatto: upload failed for image %s", image_url)
  2251. continue
  2252. asset_ids.append(asset_id)
  2253. if alt_text:
  2254. captions.append(alt_text)
  2255. if not asset_ids:
  2256. logger.warning(
  2257. "Chatto: no image survived upload, falling back to per-image delivery",
  2258. )
  2259. await super().send_multiple_images(
  2260. chat_id,
  2261. images,
  2262. metadata=metadata,
  2263. human_delay=human_delay,
  2264. )
  2265. return
  2266. if len(asset_ids) < len(images):
  2267. logger.warning(
  2268. "Chatto: sending %d of %d images — the rest could not be uploaded",
  2269. len(asset_ids),
  2270. len(images),
  2271. )
  2272. await self._post_attachment_message(
  2273. chat_id,
  2274. asset_ids,
  2275. "\n".join(captions) or None,
  2276. None,
  2277. metadata,
  2278. )
  2279. # ---------------------------------------------------------------------------
  2280. # Cron / out-of-process delivery
  2281. # ---------------------------------------------------------------------------
  2282. async def hermes_standalone_sender_fn(
  2283. pconfig: PlatformConfig,
  2284. chat_id: str,
  2285. message: str,
  2286. *,
  2287. thread_id=None,
  2288. media_files=None,
  2289. force_document=False,
  2290. ) -> SendResult:
  2291. """Deliver a message to Chatto without a running gateway adapter. Do not modify signature.
  2292. Used by cron / scheduled routines that run out-of-process. Creates a
  2293. short-lived chattolib client, posts, and closes.
  2294. """
  2295. chatto_config: ChattoConfiguration = ChattoConfiguration(pconfig=pconfig)
  2296. # Create a temporary client for standalone sending — we need a base URL plus
  2297. # either a token or a full login/password pair.
  2298. has_credentials = bool(
  2299. chatto_config.token.value
  2300. or (chatto_config.login.value and chatto_config.password.value),
  2301. )
  2302. if not chatto_config.base_url.value or not has_credentials:
  2303. return SendResult(
  2304. success=False, error="Chatto: base URL or credentials missing"
  2305. )
  2306. client: ChattoClient
  2307. try:
  2308. if chatto_config.token.value:
  2309. client = ChattoClient(
  2310. base_url=chatto_config.base_url.value, token=chatto_config.token.value
  2311. )
  2312. else:
  2313. client = await ChattoClient.login(
  2314. base_url=chatto_config.base_url.value,
  2315. login=chatto_config.login.value,
  2316. password=chatto_config.password.value,
  2317. )
  2318. except Exception as exc:
  2319. return SendResult(success=False, error=f"Chatto login failed: {exc}")
  2320. try:
  2321. kwargs: dict[str, Any] = {}
  2322. if chatto_config.auto_thread.value and thread_id:
  2323. kwargs["thread_root_event_id"] = thread_id
  2324. if media_files and media_files.get("attachment_asset_ids"):
  2325. kwargs["attachment_asset_ids"] = list(media_files["attachment_asset_ids"])
  2326. try:
  2327. posted = await client.post_message(chat_id, message, **kwargs)
  2328. except Exception as exc:
  2329. return SendResult(success=False, error=str(exc))
  2330. return SendResult(success=True, message_id=posted.id)
  2331. finally:
  2332. try:
  2333. await client.close()
  2334. except Exception as exc:
  2335. logger.warning(
  2336. "Chatto standalone: error closing short-lived client (perhaps already closed): %s",
  2337. exc,
  2338. )
  2339. def hermes_validate_config(config: PlatformConfig) -> bool:
  2340. """Check whether Chatto Plugin is configured.
  2341. Function name should be the same as register argument name with "hermes_" prefix, so we
  2342. know that it is needed for plugin register(). Do not change signature.
  2343. Takes ``config``. Compare to hermes_is_connected().
  2344. """
  2345. chatto_config = ChattoConfiguration(pconfig=config)
  2346. if (
  2347. len(chatto_config.allowed_users.value) > 0
  2348. and chatto_config.allow_all_users.value
  2349. ):
  2350. logger.error(
  2351. "Chatto: Conflicting configuration. Either use 'allowed_users' or 'allow_all_users' but not both."
  2352. )
  2353. return False
  2354. if chatto_config.base_url.value:
  2355. if (chatto_config.token.value is not None) or (
  2356. chatto_config.login.value and chatto_config.password.value
  2357. ):
  2358. return True
  2359. else:
  2360. logger.error(
  2361. "Chatto: Minimally, either token or login/password must be set."
  2362. )
  2363. else:
  2364. logger.error("Chatto: base_url must be set.")
  2365. return False
  2366. def hermes_check_fn() -> bool:
  2367. """Report whether the vendored chattolib dependency can be imported."""
  2368. try:
  2369. from chattolib import (
  2370. client, # noqa: F401 — vendored dependency probe
  2371. )
  2372. return True
  2373. except ImportError:
  2374. return False
  2375. # ---------------------------------------------------------------------------
  2376. # is_connected probe
  2377. # ---------------------------------------------------------------------------
  2378. def hermes_is_connected(config: PlatformConfig) -> bool:
  2379. """Check whether Chatto Plugin is connected. But where to: the Hermes Agent or the Chatto server.
  2380. The Hermes Agent plugin docs suck and it seems there are many functions to do the same.
  2381. """
  2382. return bool(hermes_validate_config(config) and config.enabled)
  2383. def hermes_setup_fn() -> None:
  2384. """Interactive setup wizard for Chatto. Is called by and only works in Hermes CLI context.
  2385. Function name should be the same as register argument name with "hermes_" prefix, so we
  2386. know that it is needed for plugin register().
  2387. """
  2388. from hermes_cli.setup import (
  2389. print_success,
  2390. prompt,
  2391. prompt_yes_no,
  2392. save_env_value,
  2393. )
  2394. url = prompt(
  2395. "Chatto server URL (e.g. https://chat.example.com) or leave blank for default ChattoHQ on chat.chatto.run:"
  2396. )
  2397. if url:
  2398. save_env_value(ChattoConfiguration.base_url.env_name, url)
  2399. login = prompt("Chatto login (username):")
  2400. if login:
  2401. save_env_value(ChattoConfiguration.login.env_name, login)
  2402. password = prompt("Chatto password:", password=True)
  2403. if password:
  2404. save_env_value(ChattoConfiguration.password.env_name, password)
  2405. home = prompt("Home room ID for notifications (or empty):")
  2406. if home:
  2407. save_env_value(ChattoConfiguration.home_channel.env_name, home)
  2408. allow_all = prompt_yes_no("Allow all users to talk? (true/false):")
  2409. if allow_all:
  2410. save_env_value(ChattoConfiguration.allow_all_users.env_name, str(allow_all))
  2411. print_success("\n✓ Chatto configured. Restart the gateway to activate.")
  2412. def hermes_env_enablement_fn() -> dict | None:
  2413. """Seed PlatformConfig.extra from env vars.
  2414. Returns a dict compatible with the PlatformConfig merge hook (or None
  2415. when no env-provided values are present).
  2416. Called by the platform registry during load_gateway_config().
  2417. Return None when the platform isn't minimally configured — the
  2418. caller then skips auto-enabling. Return a dict to seed extras.
  2419. The special 'home_channel' key is extracted and becomes a proper
  2420. HomeChannel dataclass on the PlatformConfig; every other key is
  2421. merged into PlatformConfig.extra.
  2422. Function name should be the same as register argument name with "hermes_" prefix, so we
  2423. know that it is needed for plugin register().
  2424. """
  2425. # Seed keys must be the config.yaml "extra" keys (config_key), NOT the env
  2426. # var names — ChattoConfiguration reads extra[config_key].
  2427. seed: dict[str, Any] = {
  2428. ChattoConfiguration.base_url.field_name: (
  2429. os.getenv(ChattoConfiguration.base_url.env_name)
  2430. or ChattoClient.DEFAULT_BASE_URL
  2431. ).strip(),
  2432. }
  2433. for field in ChattoConfiguration.fields():
  2434. if field.field_name == ChattoConfiguration.base_url.field_name:
  2435. continue
  2436. env_value = os.getenv(field.env_name)
  2437. if env_value:
  2438. seed[field.config_key] = env_value.strip()
  2439. logger.debug(
  2440. "seed: %s", {k: v for k, v in seed.items() if k not in ("token", "password")}
  2441. )
  2442. return seed
  2443. # ---------------------------------------------------------------------------
  2444. # Plugin registration entry point
  2445. # ---------------------------------------------------------------------------
  2446. # What each capability is called in the startup banner, keyed by the method
  2447. # that implements it. Derived from real overrides rather than hard-coded, so
  2448. # dropping a method drops its claim from the log instead of leaving a lie.
  2449. _CAPABILITY_LABELS = {
  2450. "send": "text",
  2451. "send_image_file": "images",
  2452. "send_multiple_images": "image batches (bundled into one message)",
  2453. "send_video": "video",
  2454. "send_voice": "voice messages",
  2455. "send_document": "documents",
  2456. "add_reaction": "reactions",
  2457. "edit_message": "message editing",
  2458. "delete_message": "message deletion",
  2459. "send_typing": "typing indicators",
  2460. "create_handoff_thread": "threads",
  2461. "start_dm": "direct messages",
  2462. "create_room": "room creation",
  2463. }
  2464. def _capabilities() -> list[str]:
  2465. """Name the things this adapter genuinely implements itself.
  2466. A capability counts only when ChattoAdapter overrides the base method —
  2467. inheriting BasePlatformAdapter's fallback means the feature is not
  2468. natively supported, and announcing it would mislead.
  2469. """
  2470. found = [
  2471. label
  2472. for name, label in _CAPABILITY_LABELS.items()
  2473. if getattr(ChattoAdapter, name, None)
  2474. is not getattr(BasePlatformAdapter, name, None)
  2475. ]
  2476. if ChattoAdapter.supports_code_blocks:
  2477. found.append("code blocks")
  2478. if ChattoAdapter.supports_status_text:
  2479. found.append("custom status text")
  2480. found.append("presence (refreshed while connected)")
  2481. return found
  2482. def register(ctx) -> None:
  2483. """Plugin entry point — called by the Hermes plugin system."""
  2484. logger.info("Registering Chatto platform plugin on Hermes Agent")
  2485. for capability in _capabilities():
  2486. logger.info("Chatto capability: %s", capability)
  2487. logger.info(
  2488. "ChattoConfiguration.allowed_users.env_name: %s",
  2489. ChattoConfiguration.allowed_users.env_name,
  2490. )
  2491. ctx.register_platform(
  2492. name=ChattoConstants.PLATFORM_NAME, # this will be the config.yaml key.
  2493. label=ChattoConstants.PLATFORM_LABEL,
  2494. adapter_factory=hermes_adapter_factory,
  2495. check_fn=hermes_check_fn,
  2496. validate_config=hermes_validate_config,
  2497. is_connected=hermes_is_connected,
  2498. install_hint=ChattoConstants.INSTALL_HINT,
  2499. env_enablement_fn=hermes_env_enablement_fn,
  2500. setup_fn=hermes_setup_fn,
  2501. cron_deliver_env_var=ChattoConfiguration.home_channel.env_name,
  2502. standalone_sender_fn=hermes_standalone_sender_fn,
  2503. allowed_users_env=ChattoConfiguration.allowed_users.env_name,
  2504. allow_all_env=ChattoConfiguration.allow_all_users.env_name,
  2505. max_message_length=ChattoConstants.MAX_MESSAGE_LENGTH,
  2506. emoji="😺",
  2507. allow_update_command=True,
  2508. pii_safe=False,
  2509. platform_hint=(
  2510. "Using the 'Hermes Chatto Platform Plugin' you connect to a Chatto server. "
  2511. "Authorized admins and users will contact you and call you his Hermes Agent. They are "
  2512. "natural persons and thus responsible for what they do in terms of rights. You compute "
  2513. "on their behalf. They _may_ address you by @-mentioning your name. If configured, "
  2514. "you also react without a @-mention. Direct messages reach you without a mention."
  2515. "Keep responses conversational. Markdown is supported. "
  2516. "Include MEDIA:/absolute/path/to/file in your response to refer to our local files. Images "
  2517. "(.png, .jpg, .gif, .webp) arrive as inline pictures, videos (.mp4, .mov, .webm) as "
  2518. "video attachments, audio as a voice bubble, anything else as a downloadable document. "
  2519. "Do NOT use markdown image syntax for local files. Local files always go through MEDIA:. "
  2520. "Several images in one response are bundled into a single message. "
  2521. ),
  2522. )