adapter.py 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196
  1. """
  2. Chatto Platform Adapter for Hermes Agent.
  3. A plugin-based gateway adapter that connects to a Chatto server
  4. (self-hosted team chat) and relays messages to/from the Hermes agent.
  5. The adapter uses the chattolib library for all Chatto API interactions,
  6. including both outbound messaging and realtime WebSocket connections.
  7. """
  8. from __future__ import annotations
  9. import random
  10. from gateway.platforms.helpers import MessageDeduplicator
  11. # Put the vendored dependencies for THIS platform on sys.path before importing
  12. # anything from chattolib. Imported relatively as part of the plugin package and
  13. # absolutely when this module is loaded standalone (e.g. by the tests).
  14. try:
  15. from .vendor_path import setup_vendor_path
  16. except ImportError: # pragma: no cover - depends on how the module is loaded
  17. from vendor_path import setup_vendor_path
  18. setup_vendor_path()
  19. import asyncio
  20. import hashlib
  21. import logging
  22. import mimetypes
  23. import os
  24. from datetime import datetime, timezone
  25. from enum import StrEnum
  26. from typing import Any, Dict, List, Literal, Optional, Tuple, cast
  27. from urllib.parse import urlsplit
  28. logger = logging.getLogger(__name__)
  29. from gateway.platforms.base import (
  30. BasePlatformAdapter,
  31. SendResult,
  32. MessageEvent,
  33. MessageType,
  34. ProcessingOutcome,
  35. cache_media_bytes,
  36. get_inbound_media_max_bytes,
  37. validate_inbound_media_size,
  38. )
  39. from gateway.config import Platform, PlatformConfig
  40. # Chattolib imports (vendored)
  41. # Using vendored chattolib from vendor/chattolib/
  42. # See vendor_chattolib.sh for how to update the vendored copy
  43. # Absolute imports — vendor/ is on sys.path (see above) and chattolib's own
  44. # modules import each other absolutely. Mixing in relative ".vendor.chattolib"
  45. # imports would load a second, distinct copy of every module, so isinstance()
  46. # checks across the two copies would silently fail.
  47. try:
  48. from chattolib.client import (
  49. ChattoClient,
  50. )
  51. from chattolib.exceptions import (
  52. ChattoAuthError,
  53. ChattoError,
  54. )
  55. from chattolib.realtime import (
  56. ChattoRealtimeError,
  57. ChattoRealtimeCloseError, RealtimeEvent,
  58. stream_events
  59. )
  60. from chattolib.realtime_types import (
  61. MessagePostedPayload,
  62. ReactionPayload,
  63. )
  64. from chattolib.types import (
  65. PresenceStatus, RoomKind, User
  66. )
  67. except ImportError as e:
  68. # Fail loudly: continuing here only defers the failure to a confusing
  69. # NameError somewhere deep in the adapter.
  70. logger.error("Chatto: failed to import vendored chattolib: %s", e)
  71. raise
  72. try:
  73. from .platform_config import (
  74. ChattoConfiguration, ChattoConstants,
  75. )
  76. except ImportError: # pragma: no cover - loaded as a top-level module (tests)
  77. from platform_config import (
  78. ChattoConfiguration, ChattoConstants,
  79. )
  80. # --------------------------------------------------------------------------- #
  81. # Chat types
  82. # --------------------------------------------------------------------------- #
  83. class HermesChatType(StrEnum):
  84. """The ``chat_type`` vocabulary the Hermes gateway understands.
  85. Declared in ``gateway/session.py:161`` as ``"dm", "group", "channel",
  86. "thread"`` and consumed as a bare string all over the gateway:
  87. ``SessionSource.description`` (session.py:239) and the PII-redacting
  88. description in ``build_session_context_prompt`` (session.py:537) both
  89. branch on these exact values and fall back to a nameless generic case for
  90. anything else, and ``build_session_key`` puts the value straight into the
  91. session key. Passing a chattolib ``RoomKind`` (``"ROOM_KIND_CHANNEL"``)
  92. therefore does not fail loudly — it just quietly degrades what the agent is
  93. told about where it is.
  94. A StrEnum so it stays a drop-in ``str`` at every one of those call sites.
  95. GROUP vs CHANNEL
  96. ----------------
  97. There is no strict contract between the two, and the adapters disagree in
  98. practice: Slack labels every non-DM conversation ``"group"`` (including real
  99. channels), Discord uses both, and Telegram reserves ``"channel"`` for actual
  100. broadcast channels. The intended reading is ``group`` = ordinary
  101. multi-participant chat, ``channel`` = broadcast surface.
  102. The distinction only changes behaviour in three places:
  103. 1. Authorization (``gateway/authz_mixin.py``) — the only security-relevant
  104. one. The group-scoped env allowlists apply to ``{"group", "forum"}``
  105. ONLY, never to ``"channel"``: ``{PLATFORM}_GROUP_ALLOWED_USERS`` /
  106. ``_GROUP_ALLOWED_CHATS`` (:616), the chat-id allowlist (:708) and the
  107. Telegram legacy shim (:724). The adapter-delegation paths in turn treat
  108. all three alike (:461, :649, :674, :694), where the value only picks
  109. ``group_allow_from`` over ``allow_from`` from ``config.extra``.
  110. For Chatto both choices are equivalent today: those group env maps hold
  111. Telegram and QQBot only (:535-541), and our own allowlist runs through
  112. ``CHATTO_ALLOWED_USERS``, which is chat_type-independent.
  113. 2. What the agent is told — ``SessionSource.description`` renders
  114. ``"group: Name"`` vs ``"channel: Name"`` (session.py:239-246), likewise
  115. the PII-redacted variant (session.py:537-544).
  116. 3. The session key, which embeds the literal (session.py:1192). Changing
  117. the value for a room re-buckets its existing sessions.
  118. Explicitly NOT affected: ``is_shared_multi_user_session`` (session.py:1063)
  119. only looks at ``"dm"`` and ``thread_id``, so sender prefixes, the multi-user
  120. prompt line and ``group_sessions_per_user`` treat group and channel
  121. identically.
  122. """
  123. DM = "dm"
  124. GROUP = "group"
  125. CHANNEL = "channel"
  126. # Emitted by adapters whose thread events are their own chat type (Slack,
  127. # Discord). We don't: a Chatto thread keeps its room's chat_type and is
  128. # identified by ``thread_id`` on the source instead. Listed for the record,
  129. # because build_session_key rewrites the slot to "thread" itself
  130. # (session.py:1190).
  131. THREAD = "thread"
  132. # Not declared in session.py:161 but real: Telegram forum topics travel as
  133. # "forum", and the authz group allowlists above accept it alongside "group".
  134. # Chatto has no equivalent, so we never emit it.
  135. # Chatto only distinguishes DMs from channels. UNSPECIFIED means the server
  136. # sent a kind this vendored chattolib doesn't know: map it to the generic
  137. # multi-user bucket rather than guessing "channel", and never to "dm" — that
  138. # value drives session isolation (is_shared_multi_user_session, session.py:1063)
  139. # and would silently turn a room into a private conversation.
  140. #
  141. # CHANNEL for RoomKind.CHANNEL is the descriptive choice and carries no
  142. # behavioural cost (see the GROUP vs CHANNEL note above). Switching to GROUP for
  143. # Slack parity would be this one line — plus the re-bucketing of existing
  144. # sessions that point 3 of that note describes.
  145. _ROOM_KIND_TO_CHAT_TYPE: Dict[RoomKind, HermesChatType] = {
  146. RoomKind.DM: HermesChatType.DM,
  147. RoomKind.CHANNEL: HermesChatType.CHANNEL,
  148. RoomKind.UNSPECIFIED: HermesChatType.GROUP,
  149. }
  150. def chat_type_for_room_kind(kind: Optional[RoomKind]) -> HermesChatType:
  151. """Map a chattolib RoomKind onto the gateway's chat_type vocabulary.
  152. An unknown or missing kind becomes ``GROUP`` — see ``_ROOM_KIND_TO_CHAT_TYPE``.
  153. """
  154. if kind is None:
  155. return HermesChatType.GROUP
  156. return _ROOM_KIND_TO_CHAT_TYPE.get(kind, HermesChatType.GROUP)
  157. # --------------------------------------------------------------------------- #
  158. # Adapter
  159. # --------------------------------------------------------------------------- #
  160. def hermes_adapter_factory(config: PlatformConfig):
  161. """Factory wrapper that constructs ChattoAdapter from a PlatformConfig."""
  162. return ChattoAdapter(config)
  163. class ChattoAdapter(BasePlatformAdapter):
  164. """Chatto platform adapter — receives messages via WebSocket realtime,
  165. sends via ConnectRPC."""
  166. _SPLIT_THRESHOLD = 9900
  167. # Read by BasePlatformAdapter.max_message_length_for_chat(), which the
  168. # gateway and the stream consumer use to chunk outgoing messages. Without
  169. # it they fall back to 4096 and split Chatto messages far earlier than
  170. # necessary — send() itself already truncates at 10000.
  171. MAX_MESSAGE_LENGTH = ChattoConstants.MAX_MESSAGE_LENGTH
  172. splits_long_messages = True
  173. supports_code_blocks: bool = True
  174. supports_status_text: bool = True # client.update_custom_status
  175. def __init__(self, pconfig: PlatformConfig):
  176. """Signature needs to be compatible with BasePlatformAdapter.__init__ """
  177. super().__init__(config=pconfig, platform=Platform(ChattoConstants.PLATFORM_NAME))
  178. # "extra" has been pre-processed by Hermes-Framework to be a dict of extra config values insode PlatformConfig.
  179. # --- Configuration from our configuration data class with some logic ---
  180. self.chatto_config: ChattoConfiguration = ChattoConfiguration(pconfig)
  181. # ------ State -------
  182. # SDK runtime handle (injected by Hermes); annotate for Pylance
  183. self.sdk: Any = getattr(self, "sdk", None)
  184. # Our own user, filled in by connect(). Events arriving before connect()
  185. # completes must not blow up on an undefined attribute.
  186. self.me: Optional[User] = None
  187. # --- Runtime state ---
  188. self._user_id: str = ""
  189. self._user_display: str = ""
  190. self._room_names: Dict[str, str] = {}
  191. self._room_kinds: Dict[str, RoomKind] = {}
  192. self._our_thread_roots: set = set() # thread root event IDs we created
  193. self._our_message_ids: set = set() # message IDs we sent (for thread root detection)
  194. self._seen: list[str] = [] # Plain RealtimeEvent-id list
  195. self._resume_cursor: Optional[str] = None
  196. self._watch_room_ids: List[str] = []
  197. self._ws_task: Optional[asyncio.Task] = None
  198. self._presence_task: Optional[asyncio.Task] = None
  199. self._ws_ready: Optional[asyncio.Event] = None
  200. self._ws_active = False
  201. self._ws_ref = None # reference to open websocket for dynamic resubscribe
  202. # Persistent typing indicator loops per room
  203. self._typing_tasks: Dict[str, asyncio.Task] = {}
  204. # Member directory cache: user_id -> user info dict
  205. self._user_cache: Dict[str, User] = {}
  206. # Handle -> does a user hold it. Cached both ways; see _mentions_someone_else.
  207. self._known_handles: Dict[str, bool] = {}
  208. # Chattolib client cache and lock for async access.
  209. self._chatto_client: Optional[ChattoClient] = None
  210. self._chatto_client_lock: asyncio.Lock = asyncio.Lock()
  211. # Dedup — chattolib may redeliver events across reconnects.
  212. self._dedup = MessageDeduplicator()
  213. # ------------------------------------------------------------------ #
  214. # Auth
  215. # ------------------------------------------------------------------ #
  216. async def _get_chatto_client(self: ChattoAdapter) -> Optional[ChattoClient]:
  217. """Get or create a ChattoClient instance."""
  218. if self._chatto_client is not None:
  219. return self._chatto_client
  220. async with self._chatto_client_lock:
  221. if self._chatto_client is not None:
  222. return self._chatto_client
  223. try:
  224. assert(self.chatto_config.login.value) # now we can assume, _login is available.
  225. client = await self._open_client(
  226. base_url=self.chatto_config.base_url.value,
  227. login=self.chatto_config.login.value,
  228. password=self.chatto_config.password.value,
  229. token=self.chatto_config.token.value,
  230. )
  231. self._chatto_client = client
  232. self._token = client.token
  233. logger.info("Chatto: logged in as '%s' via chattolib", self.chatto_config.login.value)
  234. return client
  235. except ChattoAuthError as e:
  236. logger.error("Chatto: authentication failed: %s", e)
  237. return None
  238. except (ChattoError, ValueError) as e:
  239. logger.error("Chatto: failed to create client: %s", e)
  240. return None
  241. async def _require_client(self) -> ChattoClient:
  242. """Return a ChattoClient or raise RuntimeError if unavailable.
  243. Use this helper when the caller expects a client to exist and
  244. wants a single canonical failure path. Methods that prefer a
  245. soft-fail can catch RuntimeError and return gracefully.
  246. """
  247. client = await self._get_chatto_client()
  248. if client is None:
  249. raise RuntimeError("Chatto client unavailable")
  250. return client
  251. async def _ensure_token(self) -> bool:
  252. """Ensure we have a logged-in Chatto client and token."""
  253. if self.chatto_config.token.value and isinstance(self._chatto_client, ChattoClient):
  254. return True
  255. client = await self._get_chatto_client()
  256. return client is not None
  257. # ------------------------------------------------------------------ #
  258. # Connection
  259. # ------------------------------------------------------------------ #
  260. async def _open_client(
  261. self,
  262. *,
  263. base_url: str,
  264. login: str,
  265. password: str,
  266. token: Optional[str] = None,
  267. ) -> ChattoClient:
  268. """Return a connected ``ChattoClient`` using token or login/password."""
  269. if token:
  270. return ChattoClient(token=token, base_url=base_url)
  271. return await ChattoClient.login(login, password, base_url=base_url)
  272. async def connect(self, *, is_reconnect: bool = False) -> bool:
  273. """Connect to Chatto and start the realtime event stream.
  274. BasePlatformAdapter override
  275. """
  276. logger.info("Chatto: connecting...")
  277. if not await self._ensure_token():
  278. return False
  279. try:
  280. client = await self._require_client()
  281. except RuntimeError:
  282. self._set_fatal_error("connect_failed", "Chatto client not available", retryable=True)
  283. return False
  284. # Get our first own user info
  285. try:
  286. self.me = await client.me()
  287. except Exception as exc:
  288. logger.error("Chatto: failed to get user info: %s", exc)
  289. self._set_fatal_error(
  290. "chatto_auth_failed",
  291. f"Chatto auth failed: {exc}",
  292. retryable=False,
  293. )
  294. try:
  295. assert(self._chatto_client)
  296. await self._chatto_client.close()
  297. finally:
  298. self._chatto_client = None
  299. return False
  300. # Announce online presence so the bot appears online in the member list.
  301. # The server treats this as a TTL, so _presence_refresh_loop below has to
  302. # keep re-announcing it — a single call here lapses back to offline.
  303. await self._announce_online()
  304. self._closing = False
  305. # Start background realtime WS event stream loop.
  306. self._ws_ready = asyncio.Event()
  307. self._ws_task = asyncio.create_task(
  308. self._chattolib_event_loop(), name="chatto-event-stream",
  309. )
  310. self._presence_task = asyncio.create_task(
  311. self._presence_refresh_loop(), name="chatto-presence-refresh",
  312. )
  313. self._mark_connected()
  314. logger.info(
  315. "Chatto: connected and authentiated to %s using login:'%s' (display_name:'%s' id: '%s')",
  316. self.chatto_config.base_url.value,
  317. self.me.login, self.me.display_name, self.me.id
  318. )
  319. return True
  320. async def _announce_online(self) -> bool:
  321. """Tell the server we are online. Returns whether the call got through.
  322. Logged at warning level on failure: a silently dropped presence call is
  323. indistinguishable from a bot that is simply not running.
  324. """
  325. try:
  326. client = await self._require_client()
  327. await client.update_presence(status=PresenceStatus.ONLINE)
  328. return True
  329. except Exception as exc:
  330. logger.warning("Chatto: presence refresh failed, bot may appear offline: %s", exc)
  331. return False
  332. async def _presence_refresh_loop(self) -> None:
  333. """Re-announce ONLINE until disconnect, since presence expires server-side.
  334. Failures are not fatal — the next tick tries again, so a blip in the
  335. presence endpoint costs at most one interval of visible offline time.
  336. """
  337. while not self._closing:
  338. await self._sleep_interruptible(ChattoConstants.PRESENCE_REFRESH_INTERVAL)
  339. if self._closing:
  340. return
  341. await self._announce_online()
  342. async def disconnect(self) -> None:
  343. """Stop WebSocket, presence refresh, typing tasks, and clear state.
  344. BasePlatformAdapter override
  345. """
  346. # No explicit offline broadcast: chattolib rejects OFFLINE outright
  347. # ("stop refreshing to go offline"), so cancelling the refresh loop
  348. # below is what actually takes the bot offline.
  349. self._ws_active = False
  350. self._closing = True
  351. # Cancel all typing tasks
  352. for chat_id in list(self._typing_tasks.keys()):
  353. await self.stop_typing(chat_id)
  354. if self._ws_task and not self._ws_task.done():
  355. self._ws_task.cancel()
  356. try:
  357. await self._ws_task
  358. except (asyncio.CancelledError, Exception):
  359. pass
  360. self._ws_task = None
  361. if self._presence_task and not self._presence_task.done():
  362. self._presence_task.cancel()
  363. try:
  364. await self._presence_task
  365. except (asyncio.CancelledError, Exception):
  366. pass
  367. self._presence_task = None
  368. if self._chatto_client:
  369. try:
  370. await self._chatto_client.close()
  371. except Exception:
  372. logger.exception("Chatto: error closing client")
  373. finally:
  374. self._chatto_client = None
  375. self._token = None
  376. logger.info("Chatto: disconnected")
  377. self._mark_disconnected()
  378. async def _seed_room(self, room_id: str) -> None:
  379. """Seed high-water mark from the newest events so a restart doesn't replay history."""
  380. try:
  381. try:
  382. client = await self._require_client()
  383. timeline_page = await client.get_room_events(room_id)
  384. except RuntimeError:
  385. logger.debug("Chatto: _seed_room aborted - no client available for %s", room_id)
  386. return
  387. for ev in timeline_page.events:
  388. if ev.id:
  389. self._mark_seen(ev.id)
  390. logger.debug("Chatto: seeded room %s with %d events", room_id, len(timeline_page.events))
  391. except Exception as e:
  392. logger.debug("Chatto: get room events failed for %s: %s", room_id, e)
  393. # ------------------------------------------------------------------ #
  394. # Realtime Event List
  395. # ------------------------------------------------------------------ #
  396. def _mark_seen(self, event_id: str) -> None:
  397. self._seen.append(event_id)
  398. while len(self._seen) > ChattoConstants.SEEN_CAP:
  399. self._seen.remove(self._seen[0]) # fastest removal of first item in a list.
  400. def _is_seen(self, event_id: str) -> bool:
  401. return event_id in self._seen
  402. # ------------------------------------------------------------------ #
  403. # WebSocket Realtime Transport
  404. # ------------------------------------------------------------------ #
  405. def _mentions_me(self, body: str) -> bool:
  406. """Whether the message @-mentions this bot, by login or display name."""
  407. if not self.me:
  408. return False
  409. for handle in (self.me.login, self.me.display_name):
  410. if handle and f"@{handle}" in body:
  411. return True
  412. return False
  413. async def _handle_belongs_to_a_user(self, handle: str) -> bool:
  414. """Whether ``handle`` is the login of a real Chatto user.
  415. The API carries no mention entities — ``mention_confirmation_token`` is
  416. reserved in the message descriptor — so an @-token is only a candidate
  417. until the directory confirms it. Results are cached both ways, since
  418. the same handles recur and a miss is as reusable as a hit.
  419. """
  420. known = self._known_handles.get(handle)
  421. if known is not None:
  422. return known
  423. try:
  424. client = await self._require_client()
  425. member = await client.get_user(login=handle)
  426. except Exception as exc:
  427. # Unresolved means "not confirmed", so the message goes through.
  428. logger.debug("Chatto: could not resolve handle @%s: %s", handle, exc)
  429. return False
  430. exists = member is not None and member.user is not None
  431. self._known_handles[handle] = exists
  432. return exists
  433. async def _mentions_someone_else(self, body: str) -> bool:
  434. """Whether the message @-mentions a person who is not this bot.
  435. Broadcast handles are not a person — they address everyone present,
  436. the bot included, so they do not count as someone else. A handle no
  437. user holds is not a mention at all: someone writing *about* mentioning
  438. ("per @-mention", "@nonexistent") is talking to us, and staying silent
  439. on a false positive is worse than answering one.
  440. """
  441. for handle in ChattoConstants.MENTION_RE.findall(body):
  442. if handle.lower() in ChattoConstants.BROADCAST_MENTIONS:
  443. continue
  444. if self.me and handle in (self.me.login, self.me.display_name):
  445. continue
  446. if await self._handle_belongs_to_a_user(handle):
  447. return True
  448. return False
  449. def _check_auth(self, user: User) -> bool:
  450. """We roll our own auth-systen on the against the chatto_config'ured allowed_users etc.
  451. because.. Hermes authz_mixin.py IS NOT SANE.
  452. """
  453. if self.chatto_config.allow_all_users.value:
  454. return True
  455. if user.login in self.chatto_config.allowed_users.value:
  456. return True
  457. if user.id in self.chatto_config.allowed_users.value:
  458. return True
  459. logger.info("Chatto: rejecting message from unauthorized user '%s' (%s)", user.login, user.id)
  460. return False
  461. # ------------------------------------------------------------------ #
  462. # Inbound attachments
  463. # ------------------------------------------------------------------ #
  464. async def _download_attachment_bytes(self, url: str) -> bytes:
  465. """Download an attachment, refusing to buffer more than the gateway cap.
  466. The Content-Length header is checked first so an oversized asset is
  467. rejected before a single chunk is read; the running total is re-checked
  468. as chunks arrive, because a missing or lying header must not smuggle an
  469. unbounded body past the cap.
  470. """
  471. import httpx
  472. max_bytes = get_inbound_media_max_bytes()
  473. chunks: List[bytes] = []
  474. total = 0
  475. async with httpx.AsyncClient(
  476. timeout=ChattoConstants.HTTP_TIMEOUT, follow_redirects=True,
  477. ) as http:
  478. async with http.stream("GET", url) as resp:
  479. resp.raise_for_status()
  480. declared = resp.headers.get("content-length")
  481. if declared:
  482. try:
  483. declared_size = int(declared)
  484. except ValueError:
  485. logger.debug("Chatto: ignoring invalid Content-Length %r", declared)
  486. else:
  487. validate_inbound_media_size(
  488. declared_size, media_type="attachment", max_bytes=max_bytes,
  489. )
  490. async for chunk in resp.aiter_bytes():
  491. total += len(chunk)
  492. validate_inbound_media_size(
  493. total, media_type="attachment", max_bytes=max_bytes,
  494. )
  495. chunks.append(chunk)
  496. return b"".join(chunks)
  497. async def _cache_attachments(
  498. self, room_id: str, attachments: List[Any],
  499. ) -> Tuple[List[str], List[str], List[str]]:
  500. """Download message attachments into the gateway media cache.
  501. Returns ``(media_urls, media_types, media_kinds)`` — the paths are
  502. agent-visible cache paths, exactly what ``cache_media_bytes`` yields for
  503. every other platform. A failing attachment is logged and skipped: the
  504. message itself still reaches the agent.
  505. """
  506. media_urls: List[str] = []
  507. media_types: List[str] = []
  508. media_kinds: List[str] = []
  509. for att in attachments or []:
  510. asset_url = getattr(att, "asset_url", None)
  511. url = getattr(asset_url, "url", "") if asset_url else ""
  512. filename = getattr(att, "filename", "") or ""
  513. content_type = getattr(att, "content_type", "") or ""
  514. if not url:
  515. # Videos are announced before transcoding finishes, so the
  516. # signed URL can legitimately be missing on arrival.
  517. logger.info(
  518. "Chatto: attachment '%s' has no asset URL yet, skipping", filename,
  519. )
  520. continue
  521. try:
  522. data = await self._download_attachment_bytes(url)
  523. cached = cache_media_bytes(
  524. data, filename=filename, mime_type=content_type,
  525. )
  526. except Exception as e:
  527. logger.warning(
  528. "Chatto: failed to cache attachment '%s' (%s): %s",
  529. filename, content_type, e,
  530. )
  531. continue
  532. if cached is None:
  533. logger.warning(
  534. "Chatto: attachment '%s' (%s) could not be cached, skipping",
  535. filename, content_type,
  536. )
  537. continue
  538. media_urls.append(cached.path)
  539. media_types.append(cached.media_type)
  540. media_kinds.append(cached.kind)
  541. return media_urls, media_types, media_kinds
  542. @staticmethod
  543. def _message_type_for_media_kinds(media_kinds: List[str]) -> MessageType:
  544. """Pick the MessageType for a set of cached attachment kinds."""
  545. if "document" in media_kinds:
  546. return MessageType.DOCUMENT
  547. if "image" in media_kinds:
  548. return MessageType.PHOTO
  549. if "video" in media_kinds:
  550. return MessageType.VIDEO
  551. if "audio" in media_kinds:
  552. return MessageType.AUDIO
  553. return MessageType.TEXT
  554. async def _dispatch_message_posted(self, payload: MessagePostedPayload) -> None:
  555. try:
  556. client = await self._require_client()
  557. except RuntimeError:
  558. logger.warning("Chatto: chattolib event loop aborted - no client available")
  559. return
  560. logger.info("Chatto WS: 'message_posted' event_payload:%s", payload)
  561. message = await payload.fetch_message(client=client)
  562. if message is None or message.deleted_at:
  563. return
  564. message_body = message.body or ""
  565. attachments = list(message.attachments or [])
  566. # A message carrying only an image/PDF has an empty body — dropping it
  567. # here is what made attachments sent to Hermes disappear silently.
  568. if not message_body and not attachments:
  569. return
  570. if message.actor_id in self._user_cache:
  571. # try the user cache.
  572. user = self._user_cache.get(message.actor_id)
  573. else:
  574. # get the user and update cache.
  575. directory_member = await client.get_user(user_id=message.actor_id)
  576. if directory_member is None:
  577. return
  578. user = directory_member.user
  579. if user is None:
  580. return
  581. self._user_cache[user.id] = user
  582. if user is None:
  583. return
  584. if not self._check_auth(user):
  585. return
  586. # Todo: use a function that either reads from cache or gets room kind again.
  587. if self._room_kinds.get(message.room_id) is None:
  588. room_viewer_state = await client.get_room(message.room_id)
  589. if room_viewer_state is None:
  590. return
  591. if room_viewer_state.room is None:
  592. return
  593. self._room_kinds[message.room_id] = room_viewer_state.room.kind or RoomKind.UNSPECIFIED
  594. room_kind = self._room_kinds.get(message.room_id)
  595. logger.info("message_body: %s room_kind: %s", message_body, room_kind)
  596. # require_mention deliberately gates channels only: in a channel the bot
  597. # is one of many listeners and must be addressed, whereas a DM is already
  598. # addressed at it — so DMs are always answered, mention or not.
  599. mentioned = False
  600. if (room_kind == RoomKind.CHANNEL and self.chatto_config.require_mention.value and self.me):
  601. if self.me.login and not mentioned:
  602. mentioned = bool(f"@{self.me.login}" in message_body)
  603. if self.me.display_name and not mentioned:
  604. mentioned = bool(f"@{self.me.display_name}" in message_body)
  605. if mentioned is False:
  606. logger.debug("Discarding message. Bot was not mentionend but require_mention is '%s'.", self.chatto_config.require_mention.value)
  607. return
  608. logger.info("mentioned: %s", mentioned)
  609. # With require_mention off we see every message in the channel, including
  610. # ones plainly aimed at a named colleague. Answering those would be
  611. # barging in, so acknowledge that we read it and stay quiet. Checked
  612. # after the bot-mention test above, so a message naming us *and* someone
  613. # else still counts as ours.
  614. if (
  615. room_kind == RoomKind.CHANNEL
  616. and not self.chatto_config.require_mention.value
  617. and not self._mentions_me(message_body)
  618. and await self._mentions_someone_else(message_body)
  619. ):
  620. logger.info("Chatto: message addresses someone else, acknowledging only")
  621. if self.chatto_config.reactions.value:
  622. await self.add_reaction(message.room_id, message.id, "🫥")
  623. return
  624. # Thread anchoring — if the incoming message is inside a thread, we
  625. # keep that thread by default; otherwise leave thread_id unset so
  626. # replies land at the root.
  627. thread_id = payload.thread_root_event_id or None
  628. if not thread_id and room_kind != RoomKind.DM and self.chatto_config.auto_thread.value:
  629. thread_id = message.id
  630. source = self.build_source(
  631. chat_id=payload.room_id,
  632. chat_name=self._room_names.get(message.room_id),
  633. chat_type=chat_type_for_room_kind(room_kind),
  634. user_id=message.actor_id,
  635. user_name=user.login, # use login, because display_name is changeable by anyone.
  636. thread_id=thread_id,
  637. message_id=payload.message_event_id,
  638. role_authorized=True,
  639. )
  640. msg_type = MessageType.COMMAND if (message_body.lstrip().startswith("/")) else MessageType.TEXT
  641. my_body = message_body.lstrip() if msg_type == MessageType.COMMAND else message_body
  642. # Attachments — download and hand the local cache paths to the gateway,
  643. # which runs vision enrichment / document extraction off media_urls.
  644. media_urls, media_types, media_kinds = await self._cache_attachments(
  645. payload.room_id, attachments,
  646. )
  647. if media_kinds and msg_type != MessageType.COMMAND:
  648. # Same precedence as the Teams/Signal adapters: document-context
  649. # injection gates strictly on DOCUMENT, image handling keys off the
  650. # per-path image/* MIME regardless of message_type.
  651. msg_type = self._message_type_for_media_kinds(media_kinds)
  652. message_event = MessageEvent(
  653. text=my_body,
  654. source=source,
  655. message_id=message.id,
  656. message_type=msg_type,
  657. media_urls=media_urls,
  658. media_types=media_types,
  659. timestamp=message.created_at or datetime.now(timezone.utc),
  660. raw_message=message,
  661. reply_to_message_id=message.id,
  662. reply_to_text=message_body,
  663. reply_to_author_id=user.id,
  664. reply_to_author_name=user.login,
  665. )
  666. logger.info("Chatto: Dispatching MessageEvent to Hermes: %s", message_event)
  667. await self.handle_message(message_event)
  668. return
  669. async def _forward_reaction(
  670. self, event: RealtimeEvent, payload: ReactionPayload, *, removed: bool,
  671. ) -> None:
  672. """Forward a human reaction to the gateway's reaction hook surface.
  673. The handler is registered by the gateway via ``set_reaction_handler``
  674. and fans out as ``reaction:added`` / ``reaction:removed`` through the
  675. HookRegistry. The dict shape mirrors the Slack adapter's — hook
  676. consumers are written against that contract, not against a per-platform
  677. one. Our own lifecycle reactions (👀/✅/❌) are dropped: forwarding them
  678. would feed the agent its own markers.
  679. """
  680. actor_id = event.actor_id
  681. if actor_id and self.me and actor_id == self.me.id:
  682. return
  683. if not payload.room_id or not payload.message_event_id or not actor_id:
  684. return
  685. handler = getattr(self, "_reaction_handler", None)
  686. if handler is None:
  687. return
  688. action = "removed" if removed else "added"
  689. try:
  690. await handler(
  691. {
  692. "platform": ChattoConstants.PLATFORM_NAME,
  693. "event_name": f"reaction:{action}",
  694. "reaction": payload.emoji,
  695. "user_id": actor_id,
  696. "item_user_id": None,
  697. "item_type": "message",
  698. "channel_id": payload.room_id,
  699. "message_ts": payload.message_event_id,
  700. "event_ts": event.id,
  701. "raw_event": event,
  702. }
  703. )
  704. except Exception: # pragma: no cover - the hook contract is non-blocking
  705. logger.debug("Chatto: reaction hook forwarding failed", exc_info=True)
  706. async def _handle_realtime_event(self, event: RealtimeEvent) -> None:
  707. if self._is_seen(event.id):
  708. return
  709. if event.actor_id is None:
  710. return
  711. logger.info("EVENT happened: '%s' from %s", event.kind, event.actor_id)
  712. if (event_payload := event.get("message_posted")) is not None:
  713. # Self-event filter — the actor_id on the envelope is authoritative
  714. # (chattolib does NOT filter this itself; see chatto-bridge notes).
  715. actor_id = event.actor_id
  716. if actor_id and self.me and actor_id == self.me.id:
  717. return
  718. await self._dispatch_message_posted(event_payload)
  719. elif event.kind in ("reaction_added", "reaction_removed"):
  720. reaction_payload = event.get(event.kind)
  721. if reaction_payload is not None:
  722. await self._forward_reaction(
  723. event,
  724. cast(ReactionPayload, reaction_payload),
  725. removed=event.kind == "reaction_removed",
  726. )
  727. # confirmed:
  728. elif event.kind in ("mention_notification", "projection_event", "presence_changed", "notification_dismissed",
  729. "room_marked_as_read", "user_typing", "notification_created", "new_direct_message_notification",
  730. "message_edited", "message_retracted"):
  731. logger.debug("Chatto: '%s' event received. Not yet implemented or not needed.", event.kind)
  732. else:
  733. logger.error("Chatto: unknown event kind: '%s'", event.kind)
  734. async def _chattolib_event_loop(self) -> None:
  735. """Event loop using chattolib's stream_events.
  736. This replaces the manual WebSocket loop with chattolib's high-level
  737. stream_events() which provides pre-decoded RealtimeEvent objects.
  738. """
  739. delay = ChattoConstants.WS_RECONNECT_INITIAL_BACKOFF
  740. while not self._closing:
  741. try:
  742. client = await self._require_client()
  743. await self._refresh_rooms()
  744. logger.info("Chatto: starting chattolib event stream with %d rooms", len(self._watch_room_ids))
  745. async for event in stream_events(client):
  746. if self._closing:
  747. return
  748. await self._handle_realtime_event(event)
  749. # Iterator exited cleanly — treat as a normal close and reconnect
  750. # with the local backoff (no server hint available).
  751. logger.info("Chatto: realtime stream ended, reconnecting")
  752. except asyncio.CancelledError:
  753. return
  754. except ChattoRealtimeCloseError as exc:
  755. if not exc.reconnect:
  756. logger.error(
  757. "Chatto: realtime closed by server (%s: %s), not reconnecting",
  758. exc.code, exc.message,
  759. )
  760. return
  761. wait = max(exc.retry_after_ms / 1000.0, ChattoConstants.WS_RECONNECT_INITIAL_BACKOFF)
  762. logger.warning(
  763. "Chatto: realtime closed by server (%s), reconnecting in %.1fs",
  764. exc.code, wait,
  765. )
  766. delay = ChattoConstants.WS_RECONNECT_INITIAL_BACKOFF # server hint supersedes local backoff
  767. await self._sleep_interruptible(wait)
  768. continue
  769. except ChattoRealtimeError as exc:
  770. if getattr(exc, "fatal", False):
  771. logger.error("Chatto: fatal realtime error (%s): %s", exc.code, exc.message)
  772. return
  773. logger.warning(
  774. "Chatto: realtime error (%s: %s), reconnecting in %.1fs",
  775. exc.code, exc.message, delay,
  776. )
  777. except Exception as exc:
  778. logger.warning(
  779. "Chatto: unexpected realtime error: %s, reconnecting in %.1fs",
  780. exc, delay,
  781. )
  782. if self._closing:
  783. return
  784. jitter = delay * 0.2 * random.random()
  785. await self._sleep_interruptible(delay + jitter)
  786. delay = min(delay * 2, ChattoConstants.WS_RECONNECT_MAX_BACKOFF)
  787. async def _sleep_interruptible(self, seconds: float) -> None:
  788. """Sleep in short slices so disconnect() cancels promptly."""
  789. end = asyncio.get_running_loop().time() + seconds
  790. while not self._closing:
  791. remaining = end - asyncio.get_running_loop().time()
  792. if remaining <= 0:
  793. return
  794. await asyncio.sleep(min(remaining, 0.5))
  795. async def _refresh_rooms(self) -> None:
  796. """Refresh room list via ConnectRPC, join and seed any newly discovered rooms."""
  797. try:
  798. client = await self._require_client()
  799. except RuntimeError:
  800. logger.warning("Chatto WS: _refresh_rooms aborted - no client available")
  801. return
  802. try:
  803. rooms_list = await client.list_rooms()
  804. new_room_ids: List[str] = []
  805. for room_with_state in rooms_list:
  806. if not room_with_state:
  807. continue
  808. room_obj = room_with_state.room or None
  809. if not room_obj:
  810. continue
  811. self._room_names[room_obj.id] = room_obj.name
  812. self._room_kinds[room_obj.id] = room_obj.kind
  813. if room_with_state.viewer_state.is_member and room_obj.id not in self._watch_room_ids:
  814. new_room_ids.append(room_obj.id)
  815. if not new_room_ids:
  816. return
  817. logger.info("Chatto WS: discovered %d new room(s): %s", len(new_room_ids), new_room_ids)
  818. for rid in new_room_ids:
  819. if self._room_kinds.get(rid) != RoomKind.DM:
  820. await client.join_room(rid) # but list_rooms() would not return any if we were not already joined?!
  821. await self._seed_room(rid)
  822. self._watch_room_ids.append(rid)
  823. watch_room_names: list[str] = []
  824. for rid in self._watch_room_ids:
  825. watch_room_names.append(self._room_names[rid] + " (" + rid + ")")
  826. logger.info("Chatto WS: Watching %d room(s): %s", len(self._watch_room_ids), ", ".join(watch_room_names))
  827. except Exception:
  828. logger.warning("Chatto WS: _refresh_rooms failed", exc_info=True)
  829. # ------------------------------------------------------------------ #
  830. # Read state & notification dismissal (best-effort, Chatto-unique)
  831. # ------------------------------------------------------------------ #
  832. # Best-effort: mark all watched rooms as read (room_id may be undefined here)
  833. for _rid in list(self._watch_room_ids):
  834. try:
  835. await client.mark_room_as_read(room_id=_rid)
  836. await client.dismiss_all_notifications()
  837. except Exception:
  838. logger.debug("Chatto: mark_room_as_read failed for %s", _rid, exc_info=True)
  839. # ------------------------------------------------------------------ #
  840. # Sending (ConnectRPC — unchanged from polling version)
  841. # ------------------------------------------------------------------ #
  842. async def send(
  843. self,
  844. chat_id: str,
  845. content: str,
  846. reply_to: Optional[str] = None,
  847. metadata: Optional[Dict[str, Any]] = None,
  848. ) -> SendResult:
  849. """Send a message to a Chatto room.
  850. Long messages are split into chunks via ``truncate_message`` and
  851. each chunk is sent as a separate CreateMessage call. The first
  852. chunk's message ID is returned as ``message_id``.
  853. When ``auto_thread`` is enabled and the incoming message was a
  854. regular room message (not already in a thread), the first chunk is
  855. sent as a room message and its ID becomes the thread root. Subsequent
  856. chunks are sent in that thread. This mirrors Discord's auto_thread
  857. behavior.
  858. BasePlatformAdapter override
  859. """
  860. if not content:
  861. return SendResult(success=False, error="Empty message")
  862. formatted = self.format_message(content)
  863. chunks = self.truncate_message(formatted, ChattoConstants.MAX_MESSAGE_LENGTH)
  864. # Thread support — resolve thread_id once
  865. # DM rooms don't support threads, so skip threading for DMs
  866. thread_id = (metadata or {}).get("thread_id")
  867. # Only use reply_to as thread_id if auto_thread is enabled.
  868. # When auto_thread=false, responses go directly in the room
  869. # without threading under the incoming message.
  870. if reply_to and self.chatto_config.auto_thread.value:
  871. # reply_to might be the incoming message ID. If we already have
  872. # thread_id from metadata, keep it (it's the thread root).
  873. # Only use reply_to as thread_id if we don't already have one.
  874. if not thread_id:
  875. thread_id = reply_to
  876. # Check if this is a DM room — DMs don't support threads
  877. room_kind = self._room_kinds.get(chat_id)
  878. is_dm = room_kind == RoomKind.DM
  879. if is_dm:
  880. thread_id = None
  881. # Auto-thread: by default, Chatto creates a thread for replies to room
  882. # messages (not DMs, not already in a thread). This keeps conversations
  883. # organized in the room. Can be disabled via extra.auto_thread=false.
  884. use_auto_thread = self.chatto_config.auto_thread.value and not thread_id and not is_dm
  885. message_ids: List[str] = []
  886. last_resp: Optional[Any] = None
  887. last_error: Optional[str] = None
  888. retryable = False
  889. try:
  890. client = await self._require_client()
  891. except RuntimeError:
  892. return SendResult(success=False, error="Chatto client not available", retryable=True)
  893. for i, chunk in enumerate(chunks):
  894. try:
  895. msg_obj = await client.post_message(
  896. room_id=chat_id,
  897. body=chunk,
  898. thread_root_event_id=str(thread_id) if thread_id else "",
  899. )
  900. except ChattoError as e:
  901. last_error = str(e)
  902. retryable = True
  903. break
  904. except Exception as e:
  905. last_error = str(e)
  906. retryable = True
  907. break
  908. last_resp = msg_obj
  909. self._mark_seen(msg_obj.id)
  910. message_ids.append(msg_obj.id)
  911. self._our_message_ids.add(msg_obj.id)
  912. # If we sent a message WITHOUT a thread_id, this message could
  913. # become a thread root if someone replies to it
  914. if not thread_id:
  915. self._our_thread_roots.add(msg_obj.id)
  916. # Auto-thread: first chunk becomes the thread root,
  917. # subsequent chunks go in the thread
  918. if use_auto_thread and i == 0 and not thread_id:
  919. thread_id = msg_obj.id
  920. # Nothing got through at all — report the failure instead of a phantom success.
  921. if not message_ids:
  922. return SendResult(
  923. success=False,
  924. error=last_error or "Chatto: message could not be sent",
  925. retryable=retryable,
  926. )
  927. first_id = message_ids[0]
  928. # ------------------------------------------------------------------ #
  929. # Thread following (best-effort, Chatto-unique)
  930. # ------------------------------------------------------------------ #
  931. if thread_id:
  932. try:
  933. await client.follow_thread(chat_id, thread_id)
  934. except Exception:
  935. logger.debug("Chatto: follow_thread failed for %s/%s", chat_id, thread_id, exc_info=True)
  936. # A later chunk failed after earlier ones went out: partial delivery.
  937. if last_error:
  938. logger.warning(
  939. "Chatto: sent %d/%d chunk(s) to %s before failing: %s",
  940. len(message_ids), len(chunks), chat_id, last_error,
  941. )
  942. return SendResult(success=True, message_id=first_id, raw_response=last_resp)
  943. def format_message(self, content: str) -> str:
  944. """Normalise outgoing text for Chatto.
  945. Chatto renders Markdown natively, so there is nothing to escape or
  946. translate — the only transformations here are the ones that measurably
  947. render wrong: CRLF line endings (which show up as stray blank lines)
  948. and runs of more than two blank lines.
  949. BasePlatformAdapter override
  950. """
  951. if not content:
  952. return content
  953. normalised = content.replace("\r\n", "\n").replace("\r", "\n")
  954. while "\n\n\n\n" in normalised:
  955. normalised = normalised.replace("\n\n\n\n", "\n\n\n")
  956. return normalised
  957. async def edit_message(
  958. self,
  959. chat_id: str,
  960. message_id: str,
  961. content: str,
  962. *,
  963. finalize: bool = False,
  964. ) -> SendResult:
  965. """Edit a message we previously sent, via MessageService/UpdateMessage.
  966. The stream consumer drives streaming replies through this: without the
  967. override the base class reports "Not supported" and every incremental
  968. update arrives as a *new* message.
  969. ``finalize`` is a no-op for Chatto — an edit is an edit here, there is
  970. no in-progress card state to close out (hence no
  971. ``REQUIRES_EDIT_FINALIZE``).
  972. Content that exceeds the per-message limit is refused rather than
  973. silently truncated, so the caller falls back to ``send()``, which
  974. splits across messages.
  975. BasePlatformAdapter override
  976. """
  977. if not message_id:
  978. return SendResult(success=False, error="Chatto: no message id to edit")
  979. if not content:
  980. return SendResult(success=False, error="Empty message")
  981. formatted = self.format_message(content)
  982. if len(formatted) > ChattoConstants.MAX_MESSAGE_LENGTH:
  983. # Refuse instead of truncating: the caller's fallback path splits.
  984. return SendResult(
  985. success=False,
  986. error=(
  987. f"Chatto: edit exceeds {ChattoConstants.MAX_MESSAGE_LENGTH} "
  988. f"chars ({len(formatted)})"
  989. ),
  990. )
  991. try:
  992. client = await self._require_client()
  993. except RuntimeError:
  994. return SendResult(success=False, error="Chatto client not available", retryable=True)
  995. try:
  996. msg = await client.update_message(
  997. room_id=str(chat_id),
  998. event_id=str(message_id),
  999. body=formatted,
  1000. )
  1001. except ChattoError as e:
  1002. logger.warning("Chatto: UpdateMessage failed for %s: %s", message_id, e)
  1003. return SendResult(success=False, error=str(e), retryable=True)
  1004. except Exception as e:
  1005. logger.warning("Chatto: UpdateMessage error for %s: %s", message_id, e)
  1006. return SendResult(success=False, error=str(e), retryable=False)
  1007. # Our own edit comes back as a message_edited event; mark it seen so it
  1008. # is never mistaken for inbound traffic.
  1009. edited_id = getattr(msg, "id", "") or str(message_id)
  1010. self._mark_seen(edited_id)
  1011. return SendResult(success=True, message_id=edited_id, raw_response=msg)
  1012. async def delete_message(self, chat_id: str, message_id: str) -> bool:
  1013. """Delete a message via MessageService/DeleteMessage.
  1014. Used by the stream consumer's fresh-final cleanup (removing a preview
  1015. message once the completed reply has been sent) and by the ephemeral
  1016. reply TTL.
  1017. BasePlatformAdapter override
  1018. """
  1019. if not chat_id or not message_id:
  1020. return False
  1021. try:
  1022. client = await self._require_client()
  1023. except RuntimeError:
  1024. logger.warning("Chatto: DeleteMessage — client unavailable")
  1025. return False
  1026. try:
  1027. return bool(await client.delete_message(
  1028. room_id=str(chat_id), event_id=str(message_id),
  1029. ))
  1030. except ChattoError as e:
  1031. logger.warning("Chatto: DeleteMessage failed for %s: %s", message_id, e)
  1032. return False
  1033. except Exception as e:
  1034. logger.warning("Chatto: DeleteMessage error for %s: %s", message_id, e)
  1035. return False
  1036. async def create_handoff_thread(
  1037. self, parent_chat_id: str, name: str,
  1038. ) -> Optional[str]:
  1039. """Anchor a session handoff in a fresh thread under *parent_chat_id*.
  1040. Chatto threads hang off a message, not off the room, so we post a seed
  1041. message and hand its ID back as the thread root — the same shape the
  1042. Slack adapter uses. DMs don't support threads, so they get ``None``
  1043. and the watcher keeps delivering into the DM itself.
  1044. BasePlatformAdapter override
  1045. """
  1046. if not parent_chat_id:
  1047. return None
  1048. if self._room_kinds.get(parent_chat_id) == RoomKind.DM:
  1049. logger.debug("Chatto: handoff thread skipped — %s is a DM", parent_chat_id)
  1050. return None
  1051. try:
  1052. client = await self._require_client()
  1053. except RuntimeError:
  1054. logger.warning("Chatto: handoff thread — client unavailable")
  1055. return None
  1056. seed_text = f"🧵 Hermes handoff — **{(name or 'session').strip()[:80]}**"
  1057. try:
  1058. msg = await client.post_message(room_id=str(parent_chat_id), body=seed_text)
  1059. except Exception as e:
  1060. logger.warning(
  1061. "Chatto: handoff thread seed-post failed for room %s: %s",
  1062. parent_chat_id, e,
  1063. )
  1064. return None
  1065. seed_id = getattr(msg, "id", "") or ""
  1066. if not seed_id:
  1067. logger.warning("Chatto: handoff thread seed-post returned no message id")
  1068. return None
  1069. self._mark_seen(seed_id)
  1070. self._our_message_ids.add(seed_id)
  1071. self._our_thread_roots.add(seed_id)
  1072. try:
  1073. await client.follow_thread(str(parent_chat_id), seed_id)
  1074. except Exception:
  1075. logger.debug(
  1076. "Chatto: follow_thread failed for handoff %s/%s",
  1077. parent_chat_id, seed_id, exc_info=True,
  1078. )
  1079. return seed_id
  1080. # Overridden from BaseAdapter:
  1081. async def send_typing(self, chat_id: str, metadata=None) -> None:
  1082. """Start a persistent typing indicator for a room.
  1083. Sends a typing ping every 10 seconds (Chatto's indicator likely
  1084. lasts ~8-10s). The background loop runs until ``stop_typing()``
  1085. is called or the task is cancelled.
  1086. BasePlatformAdapter override
  1087. """
  1088. if chat_id in self._typing_tasks:
  1089. return # already running
  1090. async def _typing_loop() -> None:
  1091. try:
  1092. while True:
  1093. try:
  1094. try:
  1095. client = await self._require_client()
  1096. except RuntimeError:
  1097. return
  1098. await client.update_typing_indicator(room_id=str(chat_id))
  1099. except asyncio.CancelledError:
  1100. return
  1101. except Exception:
  1102. pass
  1103. await asyncio.sleep(10)
  1104. except asyncio.CancelledError:
  1105. pass
  1106. finally:
  1107. self._typing_tasks.pop(chat_id, None)
  1108. self._typing_tasks[chat_id] = asyncio.create_task(_typing_loop())
  1109. async def stop_typing(self, chat_id: str) -> None:
  1110. """Stop the persistent typing indicator for a room.
  1111. BasePlatformAdapter override
  1112. """
  1113. task = self._typing_tasks.pop(chat_id, None)
  1114. if task:
  1115. task.cancel()
  1116. try:
  1117. await task
  1118. except (asyncio.CancelledError, Exception):
  1119. pass
  1120. async def get_chat_info(self, chat_id: str) -> Dict[str, Any]:
  1121. """Get information about a chat/room.
  1122. BasePlatformAdapter override
  1123. """
  1124. name = self._room_names.get(chat_id, chat_id)
  1125. kind = self._room_kinds.get(chat_id)
  1126. return {
  1127. "name": name,
  1128. "type": chat_type_for_room_kind(kind).value,
  1129. }
  1130. # ------------------------------------------------------------------ #
  1131. # Reactions
  1132. # ------------------------------------------------------------------ #
  1133. @staticmethod
  1134. def _emoji_to_shortcode(emoji: str) -> str:
  1135. """Convert a unicode emoji to a Chatto shortcode name.
  1136. If the emoji is already a shortcode (no unicode mapping found),
  1137. return it as-is.
  1138. """
  1139. shortcode = ChattoConstants.EMOJI_TO_SHORTCODE.get(emoji)
  1140. if shortcode:
  1141. return shortcode
  1142. # Already a shortcode like "thumbsup" — return as-is
  1143. return emoji
  1144. async def add_reaction(self, room_id: str, message_id: str, emoji: str) -> bool:
  1145. """Add a reaction to a message via MessageService/AddReaction."""
  1146. shortcode = self._emoji_to_shortcode(emoji)
  1147. try:
  1148. try:
  1149. client = await self._require_client()
  1150. except RuntimeError:
  1151. logger.warning("Chatto: AddReaction — client unavailable")
  1152. return False
  1153. result = await client.add_reaction(
  1154. room_id=room_id,
  1155. message_event_id=message_id,
  1156. emoji=shortcode,
  1157. )
  1158. return result
  1159. except ChattoError as e:
  1160. logger.warning("Chatto: AddReaction failed: %s", e)
  1161. return False
  1162. except Exception as e:
  1163. logger.warning("Chatto: AddReaction error: %s", e)
  1164. return False
  1165. async def remove_reaction(self, chat_id: str, message_id: str, emoji: str) -> bool:
  1166. """Remove a reaction from a message via MessageService/RemoveReaction."""
  1167. shortcode = self._emoji_to_shortcode(emoji)
  1168. try:
  1169. try:
  1170. client = await self._require_client()
  1171. except RuntimeError:
  1172. logger.warning("Chatto: RemoveReaction — client unavailable")
  1173. return False
  1174. result = await client.remove_reaction(
  1175. room_id=str(chat_id),
  1176. message_event_id=str(message_id),
  1177. emoji=shortcode,
  1178. )
  1179. return result
  1180. except ChattoError as e:
  1181. logger.warning("Chatto: RemoveReaction failed: %s", e)
  1182. return False
  1183. except Exception as e:
  1184. logger.warning("Chatto: RemoveReaction error: %s", e)
  1185. return False
  1186. # ------------------------------------------------------------------ #
  1187. # DM initiation (Chatto-unique)
  1188. # ------------------------------------------------------------------ #
  1189. async def start_dm(self, user_id: str) -> Optional[str]:
  1190. """Start a direct message with a user via RoomService/StartDM.
  1191. Returns the room ID on success, or None on failure.
  1192. """
  1193. if not user_id:
  1194. return None
  1195. try:
  1196. try:
  1197. client = await self._require_client()
  1198. except RuntimeError:
  1199. return None
  1200. room = await client.start_dm(participant_ids=[str(user_id)])
  1201. self._room_names[room.id] = room.name
  1202. self._room_kinds[room.id] = room.kind
  1203. return room.id
  1204. except ChattoError as e:
  1205. logger.debug("Chatto: StartDM failed: %s", e)
  1206. return None
  1207. except Exception as e:
  1208. logger.debug("Chatto: StartDM error: %s", e)
  1209. return None
  1210. # ------------------------------------------------------------------ #
  1211. # Room creation (Chatto-unique)
  1212. # ------------------------------------------------------------------ #
  1213. async def create_room(
  1214. self,
  1215. name: str,
  1216. description: str = "",
  1217. group_id: str = "",
  1218. universal: bool = True,
  1219. ) -> Optional[str]:
  1220. """Create an ad-hoc room via RoomService/CreateRoom.
  1221. Returns the room ID on success, or None on failure.
  1222. """
  1223. try:
  1224. try:
  1225. client = await self._require_client()
  1226. except RuntimeError:
  1227. return None
  1228. room = await client.create_room(
  1229. name=name,
  1230. group_id=group_id or "",
  1231. description=description,
  1232. universal=universal,
  1233. )
  1234. rid = str(room.id) if room else ""
  1235. if rid:
  1236. self._room_names[rid] = room.name
  1237. self._room_kinds[rid] = room.kind
  1238. return rid
  1239. logger.debug("Chatto: CreateRoom returned no room id")
  1240. return None
  1241. except ChattoError as e:
  1242. logger.debug("Chatto: CreateRoom failed: %s", e)
  1243. return None
  1244. except Exception as e:
  1245. logger.debug("Chatto: CreateRoom error: %s", e)
  1246. return None
  1247. # ------------------------------------------------------------------ #
  1248. # Processing lifecycle hooks (reactions-based, like Discord)
  1249. # ------------------------------------------------------------------ #
  1250. def _event_room_and_message_id(self, event: MessageEvent) -> Tuple[str, str]:
  1251. """Extract room_id and message_id from a MessageEvent."""
  1252. message_id = event.message_id or ""
  1253. chat_id = event.source.chat_id
  1254. return chat_id, message_id
  1255. async def on_processing_start(self, event: MessageEvent) -> None:
  1256. """Add an 👀 (eyes) reaction to the incoming message.
  1257. BasePlatformAdapter override
  1258. """
  1259. if not self.chatto_config.reactions.value:
  1260. return
  1261. chat_id, message_id = self._event_room_and_message_id(event)
  1262. if not chat_id or not message_id:
  1263. # Routine, not a fault: the gateway runs agent-initiated turns
  1264. # (heartbeat polls, goal continuations) through the same pipeline
  1265. # with message_id=None, and there is no inbound message to mark.
  1266. logger.debug(
  1267. "Chatto: nothing to react to (chat_id=%r, message_id=%r)",
  1268. chat_id, message_id,
  1269. )
  1270. return
  1271. await self.add_reaction(chat_id, message_id, "👀")
  1272. async def on_processing_complete(
  1273. self, event: MessageEvent, outcome: ProcessingOutcome
  1274. ) -> None:
  1275. """Swap the 👀 reaction for ✅ (success) or ❌ (failure).
  1276. BasePlatformAdapter override
  1277. """
  1278. if not self.chatto_config.reactions.value:
  1279. return
  1280. chat_id, message_id = self._event_room_and_message_id(event)
  1281. if not chat_id or not message_id:
  1282. return
  1283. # Remove the processing eyes reaction
  1284. await self.remove_reaction(chat_id, message_id, "👀")
  1285. # Add the outcome reaction
  1286. if outcome == ProcessingOutcome.SUCCESS:
  1287. await self.add_reaction(chat_id, message_id, "✅")
  1288. elif outcome == ProcessingOutcome.FAILURE:
  1289. await self.add_reaction(chat_id, message_id, "❌")
  1290. # ------------------------------------------------------------------ #
  1291. # Asset upload (chunked)
  1292. # ------------------------------------------------------------------ #
  1293. async def _upload_asset(self, room_id: str, file_path: str) -> Optional[str]:
  1294. """Upload a file via the chunked AssetUploadService.
  1295. Returns the asset ID on success, or None on failure.
  1296. """
  1297. try:
  1298. with open(file_path, "rb") as f:
  1299. file_data = f.read()
  1300. except Exception as e:
  1301. logger.error("Chatto: failed to read file %s — %s", file_path, e)
  1302. return None
  1303. if not file_data:
  1304. logger.error("Chatto: file %s is empty", file_path)
  1305. return None
  1306. file_size = len(file_data)
  1307. file_name = os.path.basename(file_path)
  1308. mime_type = mimetypes.guess_type(file_path)[0] or "application/octet-stream"
  1309. sha256_hash = hashlib.sha256(file_data).hexdigest()
  1310. try:
  1311. try:
  1312. client = await self._require_client()
  1313. except RuntimeError:
  1314. logger.error("Chatto: upload aborted - no client available")
  1315. return None
  1316. # Step 1: Create upload session
  1317. upload = await client.create_upload(
  1318. room_id=room_id,
  1319. filename=file_name,
  1320. size=file_size,
  1321. sha256=sha256_hash,
  1322. content_type=mime_type,
  1323. )
  1324. # AssetUpload names this upload_id, not id — reading it through an
  1325. # untyped getattr default is what let the mismatch reach production.
  1326. upload_id = upload.upload_id
  1327. if not upload_id:
  1328. logger.error("Chatto: CreateUpload returned no upload ID")
  1329. return None
  1330. # Step 2: Upload chunks
  1331. offset = 0
  1332. while offset < file_size:
  1333. chunk = file_data[offset:offset + ChattoConstants.UPLOAD_CHUNK_SIZE]
  1334. chunk_sha256 = hashlib.sha256(chunk).hexdigest()
  1335. await client.upload_chunk(
  1336. upload_id=upload_id,
  1337. offset=offset,
  1338. content=chunk,
  1339. chunk_sha256=chunk_sha256,
  1340. )
  1341. offset += len(chunk)
  1342. # Step 3: Complete upload
  1343. upload, asset = await client.complete_upload(upload_id=upload_id)
  1344. if not asset:
  1345. logger.error("Chatto: CompleteUpload returned no asset")
  1346. return None
  1347. asset_id = str(getattr(cast(Any, asset), "id", ""))
  1348. logger.info("Chatto: uploaded %s as asset %s (%d bytes)", file_name, asset_id, file_size)
  1349. return asset_id
  1350. except ChattoError as e:
  1351. logger.error("Chatto: upload failed: %s", e)
  1352. return None
  1353. except Exception as e:
  1354. logger.error("Chatto: upload error: %s", e)
  1355. return None
  1356. async def _post_attachment_message(
  1357. self,
  1358. chat_id: str,
  1359. asset_ids: List[str],
  1360. caption: Optional[str],
  1361. reply_to: Optional[str],
  1362. metadata: Optional[Dict[str, Any]],
  1363. ) -> SendResult:
  1364. """Post one message carrying already-uploaded assets."""
  1365. thread_id = (metadata or {}).get("thread_id")
  1366. if reply_to:
  1367. thread_id = reply_to
  1368. try:
  1369. try:
  1370. client = await self._require_client()
  1371. except RuntimeError:
  1372. return SendResult(success=False, error="Chatto client not available", retryable=True)
  1373. msg = await client.post_message(
  1374. room_id=str(chat_id),
  1375. body=self.format_message(caption) if caption else "",
  1376. attachment_asset_ids=asset_ids,
  1377. thread_root_event_id=str(thread_id) if thread_id else "",
  1378. )
  1379. self._mark_seen(msg.id)
  1380. self._our_message_ids.add(msg.id)
  1381. return SendResult(success=True, message_id=msg.id, raw_response=msg)
  1382. except ChattoError as e:
  1383. return SendResult(success=False, error=str(e), retryable=True)
  1384. except Exception as e:
  1385. return SendResult(success=False, error=str(e), retryable=False)
  1386. async def _send_local_attachment(
  1387. self,
  1388. chat_id: str,
  1389. file_path: str,
  1390. caption: Optional[str],
  1391. reply_to: Optional[str],
  1392. metadata: Optional[Dict[str, Any]],
  1393. *,
  1394. kind: str,
  1395. ) -> SendResult:
  1396. """Upload a local file and post it as a native Chatto attachment.
  1397. Shared by ``send_image_file``/``send_document``/``send_video``/
  1398. ``send_voice`` — the upload mechanics are identical, only the wording of
  1399. the failure notice differs. On failure we send that notice as text and
  1400. never the host path (it leaks the Hermes home layout).
  1401. """
  1402. notice = f"⚠️ Couldn't deliver the {kind} attachment."
  1403. safe_path = self.validate_media_delivery_path(file_path)
  1404. if not safe_path:
  1405. logger.warning(
  1406. "[%s] send %s: unsafe path %s", self.name, kind, file_path,
  1407. )
  1408. text = f"{caption}\n{notice}" if caption else notice
  1409. return await self.send(chat_id, text, reply_to=reply_to, metadata=metadata)
  1410. asset_id = await self._upload_asset(str(chat_id), safe_path)
  1411. if not asset_id:
  1412. logger.warning(
  1413. "[%s] send %s: upload failed for %s", self.name, kind, safe_path,
  1414. )
  1415. text = f"{caption}\n{notice}" if caption else notice
  1416. return await self.send(chat_id, text, reply_to=reply_to, metadata=metadata)
  1417. return await self._post_attachment_message(
  1418. chat_id, [asset_id], caption, reply_to, metadata,
  1419. )
  1420. async def send_image_file(
  1421. self,
  1422. chat_id: str,
  1423. image_path: str,
  1424. caption: Optional[str] = None,
  1425. reply_to: Optional[str] = None,
  1426. metadata: Optional[Dict[str, Any]] = None,
  1427. **kwargs,
  1428. ) -> SendResult:
  1429. """Send a local image file via the chunked upload API.
  1430. The parameter is ``image_path``, not ``file_path``: every caller passes
  1431. it by keyword (``gateway/run.py:22354``, ``:22470``, and the base class's
  1432. own ``send_multiple_images`` file:// branch), so a renamed parameter
  1433. makes each of those raise TypeError and silently degrade to a text
  1434. notice.
  1435. BasePlatformAdapter override
  1436. """
  1437. return await self._send_local_attachment(
  1438. chat_id, image_path, caption, reply_to, metadata, kind="image",
  1439. )
  1440. async def send_document(
  1441. self,
  1442. chat_id: str,
  1443. file_path: str,
  1444. caption: Optional[str] = None,
  1445. file_name: Optional[str] = None,
  1446. reply_to: Optional[str] = None,
  1447. metadata: Optional[Dict[str, Any]] = None,
  1448. **kwargs,
  1449. ) -> SendResult:
  1450. """Send a local file as a native Chatto attachment.
  1451. ``file_name`` is the user-facing name the agent chose; Chatto takes the
  1452. filename from the upload session, so it only matters for the failure
  1453. notice.
  1454. BasePlatformAdapter override
  1455. """
  1456. result = await self._send_local_attachment(
  1457. chat_id, file_path, caption, reply_to, metadata, kind="file",
  1458. )
  1459. if not result.success and file_name:
  1460. logger.debug("Chatto: document delivery failed for %s", file_name)
  1461. return result
  1462. async def send_video(
  1463. self,
  1464. chat_id: str,
  1465. video_path: str,
  1466. caption: Optional[str] = None,
  1467. reply_to: Optional[str] = None,
  1468. metadata: Optional[Dict[str, Any]] = None,
  1469. **kwargs,
  1470. ) -> SendResult:
  1471. """Send a local video as a native Chatto attachment (Chatto transcodes
  1472. and plays it inline).
  1473. BasePlatformAdapter override
  1474. """
  1475. return await self._send_local_attachment(
  1476. chat_id, video_path, caption, reply_to, metadata, kind="video",
  1477. )
  1478. async def send_voice(
  1479. self,
  1480. chat_id: str,
  1481. audio_path: str,
  1482. caption: Optional[str] = None,
  1483. reply_to: Optional[str] = None,
  1484. metadata: Optional[Dict[str, Any]] = None,
  1485. **kwargs,
  1486. ) -> SendResult:
  1487. """Send a local audio file as a native Chatto attachment.
  1488. Chatto has no dedicated voice-bubble type, so this is an ordinary audio
  1489. attachment — still far better than the base class's text notice.
  1490. BasePlatformAdapter override
  1491. """
  1492. return await self._send_local_attachment(
  1493. chat_id, audio_path, caption, reply_to, metadata, kind="audio",
  1494. )
  1495. async def send_image(
  1496. self,
  1497. chat_id: str,
  1498. image_url: str,
  1499. caption: Optional[str] = None,
  1500. reply_to: Optional[str] = None,
  1501. metadata: Optional[Dict[str, Any]] = None,
  1502. ) -> SendResult:
  1503. """Send an image to a Chatto room.
  1504. Tries to download the image from the URL and upload it as a native
  1505. attachment. Falls back to sending the URL as a link (Chatto renders
  1506. link previews) if the download fails.
  1507. BasePlatformAdapter override
  1508. """
  1509. # Try downloading and uploading as attachment
  1510. try:
  1511. import tempfile
  1512. import urllib.request as _urllib_request
  1513. # Download to a temp file
  1514. parsed = urlsplit(image_url)
  1515. url_path = parsed.path
  1516. ext = os.path.splitext(url_path)[1] or ".png"
  1517. tmp_fd, tmp_path = tempfile.mkstemp(suffix=ext, prefix="chatto_img_")
  1518. try:
  1519. os.close(tmp_fd)
  1520. req = _urllib_request.Request(image_url, headers={"User-Agent": "Hermes/1.0"})
  1521. try:
  1522. import ssl
  1523. ctx = ssl.create_default_context()
  1524. except Exception:
  1525. ctx = None
  1526. with _urllib_request.urlopen(req, timeout=ChattoConstants.HTTP_TIMEOUT, context=ctx) as resp:
  1527. with open(tmp_path, "wb") as f:
  1528. f.write(resp.read())
  1529. # Upload as attachment
  1530. result = await self.send_image_file(
  1531. chat_id, tmp_path, caption=caption,
  1532. reply_to=reply_to, metadata=metadata,
  1533. )
  1534. if result.success:
  1535. return result
  1536. finally:
  1537. try:
  1538. os.unlink(tmp_path)
  1539. except OSError:
  1540. pass
  1541. except Exception as e:
  1542. logger.debug("Chatto: send_image download/upload failed, falling back to link: %s", e)
  1543. # Fallback: send as link (Chatto renders link previews)
  1544. text = image_url
  1545. if caption:
  1546. text = f"{caption}\n{image_url}"
  1547. return await self.send(chat_id, text, reply_to=reply_to, metadata=metadata)
  1548. async def _materialise_image(self, image_url: str) -> Tuple[Optional[str], bool]:
  1549. """Resolve one ``send_multiple_images`` entry to a local file path.
  1550. Accepts ``http(s)://`` URLs (downloaded to a temp file), ``file://``
  1551. URIs and bare paths. Returns ``(path, is_temp)`` — the caller unlinks
  1552. when ``is_temp``. ``(None, False)`` means the entry is unusable.
  1553. """
  1554. import tempfile
  1555. from urllib.parse import unquote as _unquote
  1556. if image_url.startswith(("http://", "https://")):
  1557. parsed = urlsplit(image_url)
  1558. ext = os.path.splitext(parsed.path)[1] or ".png"
  1559. tmp_fd, tmp_path = tempfile.mkstemp(suffix=ext, prefix="chatto_img_")
  1560. os.close(tmp_fd)
  1561. try:
  1562. data = await self._download_attachment_bytes(image_url)
  1563. with open(tmp_path, "wb") as f:
  1564. f.write(data)
  1565. except Exception as e:
  1566. logger.warning("Chatto: image download failed for %s: %s", image_url, e)
  1567. try:
  1568. os.unlink(tmp_path)
  1569. except OSError:
  1570. pass
  1571. return None, False
  1572. return tmp_path, True
  1573. local = image_url
  1574. if local.startswith("file://"):
  1575. local = _unquote(urlsplit(local).path)
  1576. return self.validate_media_delivery_path(local), False
  1577. async def send_multiple_images(
  1578. self,
  1579. chat_id: str,
  1580. images: List[Tuple[str, str]],
  1581. metadata: Optional[Dict[str, Any]] = None,
  1582. human_delay: float = 0.0,
  1583. ) -> None:
  1584. """Send a batch of images as ONE message with several attachments.
  1585. The base implementation posts each image separately; a Chatto message
  1586. carries a list of attachment assets, so a batch belongs in a single
  1587. message (and a single notification).
  1588. ``human_delay`` is ignored deliberately — there is only one outbound
  1589. call to pace. Entries that can't be fetched are dropped with a warning;
  1590. if nothing survives, we fall back to the base class so the user still
  1591. gets the links.
  1592. BasePlatformAdapter override
  1593. """
  1594. if len(images or []) < 2:
  1595. await super().send_multiple_images(
  1596. chat_id, images, metadata=metadata, human_delay=human_delay,
  1597. )
  1598. return
  1599. asset_ids: List[str] = []
  1600. captions: List[str] = []
  1601. for image_url, alt_text in images:
  1602. path, is_temp = await self._materialise_image(image_url)
  1603. if not path:
  1604. logger.warning("Chatto: skipping unusable image %s", image_url)
  1605. continue
  1606. try:
  1607. asset_id = await self._upload_asset(str(chat_id), path)
  1608. finally:
  1609. if is_temp:
  1610. try:
  1611. os.unlink(path)
  1612. except OSError:
  1613. pass
  1614. if not asset_id:
  1615. logger.warning("Chatto: upload failed for image %s", image_url)
  1616. continue
  1617. asset_ids.append(asset_id)
  1618. if alt_text:
  1619. captions.append(alt_text)
  1620. if not asset_ids:
  1621. logger.warning(
  1622. "Chatto: no image survived upload, falling back to per-image delivery",
  1623. )
  1624. await super().send_multiple_images(
  1625. chat_id, images, metadata=metadata, human_delay=human_delay,
  1626. )
  1627. return
  1628. if len(asset_ids) < len(images):
  1629. logger.warning(
  1630. "Chatto: sending %d of %d images — the rest could not be uploaded",
  1631. len(asset_ids), len(images),
  1632. )
  1633. await self._post_attachment_message(
  1634. chat_id, asset_ids, "\n".join(captions) or None, None, metadata,
  1635. )
  1636. # ---------------------------------------------------------------------------
  1637. # Cron / out-of-process delivery
  1638. # ---------------------------------------------------------------------------
  1639. async def hermes_standalone_sender_fn(
  1640. pconfig,
  1641. chat_id,
  1642. message,
  1643. *,
  1644. thread_id=None,
  1645. media_files=None,
  1646. force_document=False,
  1647. ) -> SendResult:
  1648. """Deliver a message to Chatto without a running gateway adapter. Do not modify signature.
  1649. Used by cron / scheduled routines that run out-of-process. Creates a
  1650. short-lived chattolib client, posts, and closes.
  1651. """
  1652. chatto_config: ChattoConfiguration = ChattoConfiguration(pconfig=pconfig)
  1653. # Create a temporary client for standalone sending — we need a base URL plus
  1654. # either a token or a full login/password pair.
  1655. has_credentials = bool(
  1656. chatto_config.token.value
  1657. or (chatto_config.login.value and chatto_config.password.value)
  1658. )
  1659. if not chatto_config.base_url.value or not has_credentials:
  1660. return SendResult(success=False, error="Chatto: base URL or credentials missing")
  1661. client: ChattoClient
  1662. try:
  1663. if chatto_config.token.value:
  1664. client = ChattoClient(base_url=chatto_config.base_url.value, token=chatto_config.token.value)
  1665. else:
  1666. client = await ChattoClient.login(
  1667. base_url=chatto_config.base_url.value,
  1668. login=chatto_config.login.value,
  1669. password=chatto_config.password.value,
  1670. )
  1671. except Exception as exc:
  1672. return SendResult(success=False, error=f"Chatto login failed: {exc}")
  1673. try:
  1674. kwargs: Dict[str, Any] = {}
  1675. if chatto_config.auto_thread.value and thread_id:
  1676. kwargs["in_reply_to"] = thread_id
  1677. if media_files and media_files.get("attachment_asset_ids"):
  1678. kwargs["attachment_asset_ids"] = list(media_files["attachment_asset_ids"])
  1679. try:
  1680. posted = await client.post_message(chat_id, message, **kwargs)
  1681. except Exception as exc:
  1682. return SendResult(success=False, error=str(exc))
  1683. return SendResult(success=True, message_id=getattr(posted, "id", "") or None)
  1684. finally:
  1685. try:
  1686. await client.close()
  1687. except Exception as exc:
  1688. logger.error(
  1689. "Chatto standalone: error closing short-lived client (perhaps already closed): %s", exc,
  1690. )
  1691. def hermes_validate_config(config: PlatformConfig) -> bool:
  1692. """"
  1693. Function name should be the same as register argument name with "hermes_" prefix, so we
  1694. know that it is needed for plugin register(). Do not change signature.
  1695. - config
  1696. Check whether Chatto Plugin is configured. Compare to hermes_is_connected()."""
  1697. chatto_config = ChattoConfiguration(pconfig=config)
  1698. if len(chatto_config.allowed_users.value) > 0 and chatto_config.allow_all_users.value:
  1699. logger.info("Chatto: Conflicting configuration. Either use 'allowed_users' or 'allow_all_users' but not both.")
  1700. return False
  1701. if chatto_config.base_url.value:
  1702. if (chatto_config.token.value is not None) or (chatto_config.login.value and chatto_config.password.value):
  1703. return True
  1704. else:
  1705. logger.error("Chatto: Minimally, either token or login/password must be set.")
  1706. else:
  1707. logger.error("Chatto: base_url must be set.")
  1708. return False
  1709. def hermes_check_fn() -> bool:
  1710. """Check if Chatto is configured and dependencies are available.
  1711. Add real logic?! Or just .. there are no dependencies.. always return true. Really. Docs suck."""
  1712. try:
  1713. import chattolib.client # noqa: F401 — vendored dependency probe
  1714. return True
  1715. except ImportError:
  1716. return False
  1717. # ---------------------------------------------------------------------------
  1718. # is_connected probe
  1719. # ---------------------------------------------------------------------------
  1720. def hermes_is_connected(config: PlatformConfig) -> bool:
  1721. """Check whether Chatto Plugin is connected. But where to? To the Hermes Agent? To Chatto Server?
  1722. The Hermes Agent plugin docs suck and it seems there are many functions to do the same."""
  1723. return bool(hermes_validate_config(config) and config.enabled)
  1724. def hermes_setup_fn() -> None:
  1725. """Interactive setup wizard for Chatto. Is called by and only works in Hermes CLI context.
  1726. Function name should be the same as register argument name with "hermes_" prefix, so we
  1727. know that it is needed for plugin register().
  1728. """
  1729. from hermes_cli.setup import (
  1730. prompt,
  1731. prompt_yes_no,
  1732. save_env_value,
  1733. get_env_value,
  1734. print_header,
  1735. print_info,
  1736. print_warning,
  1737. print_success,
  1738. )
  1739. url = prompt(
  1740. "Chatto server URL (e.g. https://chat.example.com) or leave blank for default ChattoHQ on chat.chatto.run:")
  1741. if url:
  1742. save_env_value(ChattoConfiguration.base_url.env_name, url)
  1743. login = prompt("Chatto login (username):")
  1744. if login:
  1745. save_env_value(ChattoConfiguration.login.env_name, login)
  1746. password = prompt("Chatto password:", password=True)
  1747. if password:
  1748. save_env_value(ChattoConfiguration.password.env_name, password)
  1749. channels = prompt("Room IDs to watch (comma-separated, or empty for all):")
  1750. if channels:
  1751. save_env_value(ChattoConfiguration.channels_list.env_name, channels)
  1752. home = prompt("Home room ID for notifications (or empty):")
  1753. if home:
  1754. save_env_value(ChattoConfiguration.home_channel.env_name, home)
  1755. allow_all = prompt_yes_no("Allow all users to talk? (true/false):")
  1756. if allow_all:
  1757. save_env_value(ChattoConfiguration.allow_all_users.env_name, str(allow_all))
  1758. print_success("\n✓ Chatto configured. Restart the gateway to activate.")
  1759. def hermes_env_enablement_fn() -> Optional[dict]:
  1760. """Seed PlatformConfig.extra from env vars.
  1761. Returns a dict compatible with the PlatformConfig merge hook (or None
  1762. when no env-provided values are present).
  1763. Called by the platform registry during load_gateway_config().
  1764. Return None when the platform isn't minimally configured — the
  1765. caller then skips auto-enabling. Return a dict to seed extras.
  1766. The special 'home_channel' key is extracted and becomes a proper
  1767. HomeChannel dataclass on the PlatformConfig; every other key is
  1768. merged into PlatformConfig.extra.
  1769. Function name should be the same as register argument name with "hermes_" prefix, so we
  1770. know that it is needed for plugin register().
  1771. """
  1772. # Seed keys must be the config.yaml "extra" keys (config_key), NOT the env
  1773. # var names — ChattoConfiguration reads extra[config_key].
  1774. seed: Dict[str, Any] = {
  1775. ChattoConfiguration.base_url.config_key: (
  1776. os.getenv(ChattoConfiguration.base_url.env_name) or ChattoClient.DEFAULT_BASE_URL
  1777. ).strip(),
  1778. }
  1779. for field in ChattoConfiguration.fields():
  1780. if field.field_name == ChattoConfiguration.base_url.field_name:
  1781. continue
  1782. env_value = os.getenv(field.env_name)
  1783. if env_value:
  1784. seed[field.config_key] = env_value.strip()
  1785. logger.debug("seed: %s", {k: v for k, v in seed.items() if k not in ("token", "password")})
  1786. return seed
  1787. # ---------------------------------------------------------------------------
  1788. # Plugin registration entry point
  1789. # ---------------------------------------------------------------------------
  1790. # What each capability is called in the startup banner, keyed by the method
  1791. # that implements it. Derived from real overrides rather than hard-coded, so
  1792. # dropping a method drops its claim from the log instead of leaving a lie.
  1793. _CAPABILITY_LABELS = {
  1794. "send": "text",
  1795. "send_image_file": "images",
  1796. "send_multiple_images": "image batches (bundled into one message)",
  1797. "send_video": "video",
  1798. "send_voice": "voice messages",
  1799. "send_document": "documents",
  1800. "add_reaction": "reactions",
  1801. "edit_message": "message editing",
  1802. "delete_message": "message deletion",
  1803. "send_typing": "typing indicators",
  1804. "create_handoff_thread": "threads",
  1805. "start_dm": "direct messages",
  1806. "create_room": "room creation",
  1807. }
  1808. def _capabilities() -> List[str]:
  1809. """Name the things this adapter genuinely implements itself.
  1810. A capability counts only when ChattoAdapter overrides the base method —
  1811. inheriting BasePlatformAdapter's fallback means the feature is not
  1812. natively supported, and announcing it would mislead.
  1813. """
  1814. found = [
  1815. label
  1816. for name, label in _CAPABILITY_LABELS.items()
  1817. if getattr(ChattoAdapter, name, None) is not getattr(BasePlatformAdapter, name, None)
  1818. ]
  1819. if ChattoAdapter.supports_code_blocks:
  1820. found.append("code blocks")
  1821. if ChattoAdapter.supports_status_text:
  1822. found.append("custom status text")
  1823. found.append("presence (refreshed while connected)")
  1824. return found
  1825. def register(ctx) -> None:
  1826. """Plugin entry point — called by the Hermes plugin system."""
  1827. logger.info("Registering Chatto platform plugin on Hermes Agent")
  1828. for capability in _capabilities():
  1829. logger.info("Chatto capability: %s", capability)
  1830. logger.info("ChattoConfiguration.allowed_users.env_name: %s", ChattoConfiguration.allowed_users.env_name)
  1831. ctx.register_platform(
  1832. name=ChattoConstants.PLATFORM_NAME, # this will be the config.yaml key.
  1833. label=ChattoConstants.PLATFORM_LABEL,
  1834. adapter_factory=hermes_adapter_factory,
  1835. check_fn=hermes_check_fn,
  1836. validate_config=hermes_validate_config,
  1837. is_connected=hermes_is_connected,
  1838. install_hint=ChattoConstants.INSTALL_HINT,
  1839. env_enablement_fn=hermes_env_enablement_fn,
  1840. setup_fn=hermes_setup_fn,
  1841. cron_deliver_env_var=ChattoConfiguration.home_channel.env_name,
  1842. standalone_sender_fn=hermes_standalone_sender_fn,
  1843. allowed_users_env=ChattoConfiguration.allowed_users.env_name,
  1844. allow_all_env=ChattoConfiguration.allow_all_users.env_name,
  1845. max_message_length=ChattoConstants.MAX_MESSAGE_LENGTH,
  1846. emoji="💬",
  1847. allow_update_command=True,
  1848. pii_safe=False,
  1849. platform_hint=(
  1850. "Using the 'Hermes Chatto Platform Plugin' you connect to a Chatto server. "
  1851. "Authorized admins and users will contact you and call you his Hermes Agent. They are "
  1852. "natural persons and thus responsible for what they do in terms of rights. You compute "
  1853. "on their behalf. They _may_ address you by @-mentioning your name. If configured, "
  1854. "you also react without a @-mention. Direct messages reach you without a mention."
  1855. "Keep responses conversational. Markdown is supported. "
  1856. "Include MEDIA:/absolute/path/to/file in your response to refer to our local files. Images "
  1857. "(.png, .jpg, .gif, .webp) arrive as inline pictures, videos (.mp4, .mov, .webm) as "
  1858. "video attachments, audio as a voice bubble, anything else as a downloadable document. "
  1859. "Do NOT use markdown image syntax for local files. Local files always go through MEDIA:. "
  1860. "Several images in one response are bundled into a single message. "
  1861. ),
  1862. )