adapter.py 163 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119
  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 json
  9. import random
  10. # Put the vendored dependencies for THIS platform on sys.path before importing
  11. # anything from chattolib. Imported relatively as part of the plugin package and
  12. # absolutely when this module is loaded standalone (e.g. by the tests).
  13. try:
  14. from .vendor_path import setup_vendor_path
  15. except ImportError: # pragma: no cover - depends on how the module is loaded
  16. from vendor_path import setup_vendor_path
  17. setup_vendor_path()
  18. import asyncio
  19. import hashlib
  20. import logging
  21. import mimetypes
  22. import os
  23. import re
  24. import tempfile
  25. from collections import deque
  26. from dataclasses import dataclass, field
  27. from datetime import UTC, datetime
  28. from difflib import SequenceMatcher
  29. from enum import StrEnum
  30. from typing import Any
  31. from urllib.parse import unquote, urlsplit
  32. import httpx
  33. logger = logging.getLogger(__name__)
  34. from gateway.config import Platform, PlatformConfig
  35. from gateway.platforms.base import (
  36. BasePlatformAdapter,
  37. MessageEvent,
  38. MessageType,
  39. ProcessingOutcome,
  40. SendResult,
  41. cache_media_bytes,
  42. get_inbound_media_max_bytes,
  43. validate_inbound_media_size,
  44. )
  45. from gateway.session import build_session_key
  46. # Chattolib imports (vendored)
  47. # Using vendored chattolib from vendor/chattolib/
  48. # See vendor_chattolib.sh for how to update the vendored copy
  49. # Absolute imports — vendor/ is on sys.path (see above) and chattolib's own
  50. # modules import each other absolutely. Mixing in relative ".vendor.chattolib"
  51. # imports would load a second, distinct copy of every module, so isinstance()
  52. # checks across the two copies would silently fail.
  53. try:
  54. from chattolib._pb.chatto.realtime.v1 import ( # noqa: F401 — importability probe
  55. realtime_pb2,
  56. )
  57. from chattolib._pb.chatto.realtime.v1.events_pb2 import (
  58. MessagePostedEvent, # type: ignore[import-untyped]
  59. )
  60. from chattolib.client import (
  61. ChattoClient,
  62. )
  63. from chattolib.exceptions import (
  64. ChattoAuthError,
  65. ChattoError,
  66. )
  67. from chattolib.realtime import (
  68. ChattoRealtimeCloseError,
  69. ChattoRealtimeError,
  70. RealtimeEvent,
  71. stream_events,
  72. )
  73. from chattolib.types import (
  74. Message,
  75. MessageAttachment,
  76. PresenceStatus,
  77. Room,
  78. RoomKind,
  79. RoomWithViewerState,
  80. User,
  81. )
  82. except ImportError as e:
  83. # Fail loudly: continuing here only defers the failure to a confusing
  84. # NameError somewhere deep in the adapter.
  85. logger.error("Chatto: failed to import vendored chattolib: %s", e)
  86. raise
  87. try:
  88. from .platform_config import (
  89. ChattoConfiguration,
  90. ChattoConstants,
  91. ConfigValue,
  92. )
  93. except ImportError: # pragma: no cover - loaded as a top-level module (tests)
  94. from platform_config import (
  95. ChattoConfiguration,
  96. ChattoConstants,
  97. ConfigValue,
  98. )
  99. # --------------------------------------------------------------------------- #
  100. # Chat types
  101. # --------------------------------------------------------------------------- #
  102. class HermesChatType(StrEnum):
  103. """The ``chat_type`` vocabulary the Hermes gateway understands.
  104. Declared in ``gateway/session.py:161`` as ``"dm", "group", "channel",
  105. "thread"`` and consumed as a bare string all over the gateway:
  106. ``SessionSource.description`` (session.py:239) and the PII-redacting
  107. description in ``build_session_context_prompt`` (session.py:537) both
  108. branch on these exact values and fall back to a nameless generic case for
  109. anything else, and ``build_session_key`` puts the value straight into the
  110. session key. Passing a chattolib ``RoomKind`` (``"ROOM_KIND_CHANNEL"``)
  111. therefore does not fail loudly — it just quietly degrades what the agent is
  112. told about where it is.
  113. A StrEnum so it stays a drop-in ``str`` at every one of those call sites.
  114. GROUP vs CHANNEL
  115. ----------------
  116. There is no strict contract between the two, and the adapters disagree in
  117. practice: Slack labels every non-DM conversation ``"group"`` (including real
  118. channels), Discord uses both, and Telegram reserves ``"channel"`` for actual
  119. broadcast channels. The intended reading is ``group`` = ordinary
  120. multi-participant chat, ``channel`` = broadcast surface.
  121. The distinction only changes behaviour in three places:
  122. 1. Authorization (``gateway/authz_mixin.py``) — the only security-relevant
  123. one. The group-scoped env allowlists apply to ``{"group", "forum"}``
  124. ONLY, never to ``"channel"``: ``{PLATFORM}_GROUP_ALLOWED_USERS`` /
  125. ``_GROUP_ALLOWED_CHATS`` (:616), the chat-id allowlist (:708) and the
  126. Telegram legacy shim (:724). The adapter-delegation paths in turn treat
  127. all three alike (:461, :649, :674, :694), where the value only picks
  128. ``group_allow_from`` over ``allow_from`` from ``config.extra``.
  129. For Chatto both choices are equivalent today: those group env maps hold
  130. Telegram and QQBot only (:535-541), and our own allowlist runs through
  131. ``CHATTO_ALLOWED_USERS``, which is chat_type-independent.
  132. 2. What the agent is told — ``SessionSource.description`` renders
  133. ``"group: Name"`` vs ``"channel: Name"`` (session.py:239-246), likewise
  134. the PII-redacted variant (session.py:537-544).
  135. 3. The session key, which embeds the literal (session.py:1192). Changing
  136. the value for a room re-buckets its existing sessions.
  137. Explicitly NOT affected: ``is_shared_multi_user_session`` (session.py:1063)
  138. only looks at ``"dm"`` and ``thread_id``, so sender prefixes, the multi-user
  139. prompt line and ``group_sessions_per_user`` treat group and channel
  140. identically.
  141. """
  142. DM = "dm"
  143. GROUP = "group"
  144. CHANNEL = "channel"
  145. # Emitted by adapters whose thread events are their own chat type (Slack,
  146. # Discord). We don't: a Chatto thread keeps its room's chat_type and is
  147. # identified by ``thread_id`` on the source instead. Listed for the record,
  148. # because build_session_key rewrites the slot to "thread" itself
  149. # (session.py:1190).
  150. THREAD = "thread"
  151. # Not declared in session.py:161 but real: Telegram forum topics travel as
  152. # "forum", and the authz group allowlists above accept it alongside "group".
  153. # Chatto has no equivalent, so we never emit it.
  154. # Chatto only distinguishes DMs from channels. UNSPECIFIED means the server
  155. # sent a kind this vendored chattolib doesn't know: map it to the generic
  156. # multi-user bucket rather than guessing "channel", and never to "dm" — that
  157. # value drives session isolation (is_shared_multi_user_session, session.py:1063)
  158. # and would silently turn a room into a private conversation.
  159. #
  160. # CHANNEL for RoomKind.CHANNEL is the descriptive choice and carries no
  161. # behavioural cost (see the GROUP vs CHANNEL note above). Switching to GROUP for
  162. # Slack parity would be this one line — plus the re-bucketing of existing
  163. # sessions that point 3 of that note describes.
  164. _ROOM_KIND_TO_CHAT_TYPE: dict[RoomKind, HermesChatType] = {
  165. RoomKind.DM: HermesChatType.DM,
  166. RoomKind.CHANNEL: HermesChatType.CHANNEL,
  167. RoomKind.UNSPECIFIED: HermesChatType.GROUP,
  168. }
  169. def chat_type_for_room_kind(kind: RoomKind | None) -> HermesChatType:
  170. """Map a chattolib RoomKind onto the gateway's chat_type vocabulary.
  171. An unknown or missing kind becomes ``GROUP`` — see ``_ROOM_KIND_TO_CHAT_TYPE``.
  172. """
  173. if kind is None:
  174. return HermesChatType.GROUP
  175. return _ROOM_KIND_TO_CHAT_TYPE.get(kind, HermesChatType.GROUP)
  176. class RoomPolicy(StrEnum):
  177. """How a channel-kind room treats an inbound message.
  178. Derived per room from ``CHATTO_REQUIRE_MENTION_ROOMS`` /
  179. ``CHATTO_OPTIONAL_MENTION_ROOMS`` — see ``_room_policy``. A StrEnum
  180. so the values log readably without a formatting dance.
  181. """
  182. # Listed for unaddressed answers: every message is dispatched, and one
  183. # aimed at a named colleague gets a 🫥 acknowledgement instead of a reply.
  184. OPEN = "open"
  185. # Listed for addressed-only participation: dispatches only messages that
  186. # mention the bot (@name or a broadcast handle); others are dropped.
  187. REQUIRE_MENTION = "require_mention"
  188. # Listed in neither config: the room is silent — not dispatched at all,
  189. # read-only like any unlisted membership.
  190. SILENT = "silent"
  191. # Blocking filesystem/network helpers. The adapter runs on the shared gateway
  192. # event loop, so file reads and HTTP downloads are pushed to a worker thread
  193. # via asyncio.to_thread — a slow disk or dead image URL must not stall every
  194. # platform's message processing.
  195. def _read_file_bytes(path: str) -> bytes:
  196. """Read a whole file synchronously (run via asyncio.to_thread)."""
  197. with open(path, "rb") as f:
  198. return f.read()
  199. def _write_file_bytes(path: str, data: bytes) -> None:
  200. """Write bytes to a file synchronously (run via asyncio.to_thread)."""
  201. with open(path, "wb") as f:
  202. f.write(data)
  203. def _normalise_outbound_text(content: str) -> str:
  204. """Normalise outgoing text for Chatto.
  205. Chatto renders Markdown natively, so there is nothing to escape or
  206. translate — the only transformations here are the ones that measurably
  207. render wrong: CRLF line endings (which show up as stray blank lines)
  208. and runs of more than two blank lines. Shared by the adapter's
  209. ``format_message`` and the standalone cron sender, so both paths render
  210. identically.
  211. """
  212. if not content:
  213. return content
  214. normalised = content.replace("\r\n", "\n").replace("\r", "\n")
  215. while "\n\n\n\n" in normalised:
  216. normalised = normalised.replace("\n\n\n\n", "\n\n\n")
  217. return normalised
  218. # --------------------------------------------------------------------------- #
  219. # Adapter
  220. # --------------------------------------------------------------------------- #
  221. # Presence as the roster block spells it. UNSPECIFIED gets no label: a server
  222. # that never tracked presence should not have the roster claim anything.
  223. _PRESENCE_LABELS = {
  224. PresenceStatus.ONLINE: "online",
  225. PresenceStatus.AWAY: "away",
  226. PresenceStatus.DO_NOT_DISTURB: "do not disturb",
  227. PresenceStatus.OFFLINE: "offline",
  228. }
  229. @dataclass
  230. class _RoomRoster:
  231. """One announced room's roster projection.
  232. A miniature of the server's room membership: which user IDs belong to
  233. the room (the users themselves live in the shared ``_user_cache``) and
  234. how many directory entries were beyond the fetch limit, rendered as
  235. ``… and N more``. Presence changes patch the cached users in place;
  236. only a membership change or reconnect discards this and refetches.
  237. """
  238. member_ids: set[str] = field(default_factory=set)
  239. unfetched: int = 0
  240. def hermes_adapter_factory(config: PlatformConfig):
  241. """Construct a ChattoAdapter from a PlatformConfig."""
  242. return ChattoAdapter(config)
  243. class ChattoAdapter(BasePlatformAdapter):
  244. """Chatto platform adapter.
  245. Receives messages via WebSocket realtime, sends via ConnectRPC.
  246. """
  247. # Read by BasePlatformAdapter.max_message_length_for_chat(), which the
  248. # gateway and the stream consumer use to chunk outgoing messages. Without
  249. # it they fall back to 4096 and split Chatto messages far earlier than
  250. # necessary — send() itself already truncates at SPLIT_THRESHOLD.
  251. MAX_MESSAGE_LENGTH = ChattoConstants.MAX_MESSAGE_LENGTH
  252. splits_long_messages = True
  253. supports_code_blocks: bool = True
  254. supports_status_text: bool = True # client.update_custom_status
  255. def __init__(self, pconfig: PlatformConfig):
  256. """Signature needs to be compatible with BasePlatformAdapter.__init__."""
  257. super().__init__(
  258. config=pconfig, platform=Platform(ChattoConstants.PLATFORM_NAME)
  259. )
  260. # "extra" has been pre-populated by Hermes from config.yaml's extra block.
  261. # --- Configuration from our configuration data class with some logic ---
  262. self.chatto_config: ChattoConfiguration = ChattoConfiguration(pconfig)
  263. # ------ State -------
  264. # Our own user, filled in by connect(). Events arriving before connect()
  265. # completes must not blow up on an undefined attribute.
  266. self.me: User | None = None
  267. # --- Runtime state ---
  268. self._room_names: dict[str, str] = {}
  269. self._room_kinds: dict[str, RoomKind] = {}
  270. # Event IDs already processed — chattolib may redeliver events across
  271. # reconnects, so every inbound event is checked against this list.
  272. # Bounded deques: appending past the cap drops the oldest ID on its own.
  273. self._seen: deque[str] = deque(maxlen=ChattoConstants.SEEN_CAP)
  274. # Message IDs this adapter has handed to the gateway (posted or
  275. # edit-re-dispatched). Edits of anything on this list never start a
  276. # fresh turn — that is the lock against re-answering settled
  277. # conversations by editing old messages.
  278. self._dispatched_ids: deque[str] = deque(maxlen=ChattoConstants.SEEN_CAP)
  279. # Rooms whose member roster has been projected from the directory —
  280. # the roster is room-scoped and rides on every channel turn (threads
  281. # hold isolated sessions, so each turn needs its own copy). Together
  282. # with _rosters this is our miniature projection of server state:
  283. # presence changes patch the cached users in place; membership events
  284. # and reconnects discard a room so its next turn refetches once.
  285. self._roster_announced: deque[str] = deque(maxlen=ChattoConstants.SEEN_CAP)
  286. # Announced rooms' membership projection (see _RoomRoster).
  287. self._rosters: dict[str, _RoomRoster] = {}
  288. # Thread roots whose earlier messages have already been handed to their
  289. # session. Thread sessions start empty, so the first turn into a thread
  290. # seeds what came before it — a cron message whose echo we filtered as
  291. # a self-event, or chatter that predates the bot joining. Seeding again
  292. # would duplicate context the session already holds, so it happens once
  293. # per thread. Unlike the roster this is deliberately NOT discarded on
  294. # reconnect: thread history is immutable, so a refetch could only
  295. # repeat what the session already has.
  296. self._seeded_threads: deque[str] = deque(maxlen=ChattoConstants.SEEN_CAP)
  297. # session_key -> message ID currently being processed there. Written
  298. # by on_processing_start, cleared by on_processing_complete; an edit
  299. # landing on the recorded ID is a mid-run correction.
  300. self._processing: dict[str, str] = {}
  301. self._joined_room_ids: list[str] = []
  302. # Rooms the server force-joined everyone into (Room.universal) — used
  303. # only for [universal] tags in the joined-rooms log line, never for
  304. # gating.
  305. self._universal_room_ids: set[str] = set()
  306. # DM room ID -> chat partner's login, resolved once via the member
  307. # directory so the joined-rooms summary can name who a DM is with.
  308. self._dm_partners: dict[str, str] = {}
  309. # One-shot guard for the unjoined-home-channel warning in _refresh_rooms.
  310. self._home_warning_logged = False
  311. self._ws_task: asyncio.Task | None = None
  312. self._presence_task: asyncio.Task | None = None
  313. # Persistent typing indicator loops per room
  314. self._typing_tasks: dict[str, asyncio.Task] = {}
  315. # Member directory cache: user_id -> user info dict
  316. self._user_cache: dict[str, User] = {}
  317. # Handle -> does a user hold it. Cached both ways; see _mentions_someone_else.
  318. self._known_handles: dict[str, bool] = {}
  319. # Chattolib client cache and lock for async access.
  320. self._chatto_client: ChattoClient | None = None
  321. self._chatto_client_lock: asyncio.Lock = asyncio.Lock()
  322. # ------------------------------------------------------------------ #
  323. # Auth
  324. # ------------------------------------------------------------------ #
  325. async def _get_chatto_client(self: ChattoAdapter) -> ChattoClient | None:
  326. """Return the shared ChattoClient, creating and logging in on first use.
  327. The default way to get a client. The fast path is a plain attribute
  328. read; first creation runs under a lock so concurrent callers log in
  329. exactly once. Returns ``None`` when no client exists yet or creation
  330. failed (bad credentials, unreachable server) — a normal state during
  331. startup, shutdown and reconnects, not an exceptional one. Callers
  332. decide what "no client" means for them, as a guard clause:
  333. client = await self._get_chatto_client()
  334. if client is None:
  335. logger.warning("Chatto: dropping X - no client available")
  336. return
  337. """
  338. if self._chatto_client is not None:
  339. return self._chatto_client
  340. async with self._chatto_client_lock:
  341. if self._chatto_client is not None:
  342. return self._chatto_client
  343. try:
  344. client = await self._open_client(
  345. base_url=self.chatto_config.base_url.value,
  346. token=self.chatto_config.token.value,
  347. )
  348. self._chatto_client = client
  349. logger.info(
  350. "Chatto: authenticated via token to '%s'",
  351. self.chatto_config.base_url.value,
  352. )
  353. return client
  354. except ChattoAuthError as e:
  355. logger.error("Chatto: authentication failed: %s", e)
  356. return None
  357. except (ChattoError, ValueError) as e:
  358. logger.error("Chatto: failed to create client: %s", e)
  359. return None
  360. async def _require_chatto_client(self) -> ChattoClient:
  361. """Return a ChattoClient or raise RuntimeError if unavailable.
  362. The exception-flavoured variant of :meth:`_get_chatto_client`, for
  363. callers whose surrounding machinery already routes exceptions.
  364. Currently that is only the realtime event loop, whose except chain
  365. turns the raise into a logged warning plus a backed-off reconnect —
  366. no special "no client" branch needed there. Everywhere else, prefer
  367. the ``is None`` guard shown in :meth:`_get_chatto_client`.
  368. """
  369. client = await self._get_chatto_client()
  370. if client is None:
  371. raise RuntimeError("Chatto client unavailable")
  372. return client
  373. # ------------------------------------------------------------------ #
  374. # Connection
  375. # ------------------------------------------------------------------ #
  376. async def _open_client(
  377. self,
  378. *,
  379. base_url: str,
  380. token: str | None = None,
  381. ) -> ChattoClient:
  382. """Return a connected ``ChattoClient`` using a bearer token.
  383. Raises ValueError when no token is configured — caught upstream as a
  384. normal creation failure, so misconfiguration reads as a logged error
  385. instead of a crash.
  386. """
  387. if not token:
  388. raise ValueError("Chatto: CHATTO_TOKEN not configured")
  389. return ChattoClient(token=token, base_url=base_url)
  390. async def connect(self, *, is_reconnect: bool = False) -> bool:
  391. """Connect to Chatto and start the realtime event stream.
  392. BasePlatformAdapter override
  393. """
  394. logger.info("Chatto: connecting...")
  395. client = await self._get_chatto_client()
  396. if client is None:
  397. self._set_fatal_error(
  398. "connect_failed", "Chatto client not available", retryable=True
  399. )
  400. return False
  401. # Get our first own user info
  402. try:
  403. self.me = await client.me()
  404. except Exception as exc:
  405. logger.error("Chatto: failed to get user info: %s", exc)
  406. self._set_fatal_error(
  407. "chatto_auth_failed",
  408. f"Chatto auth failed: {exc}",
  409. retryable=False,
  410. )
  411. try:
  412. if self._chatto_client is not None:
  413. await self._chatto_client.close()
  414. finally:
  415. self._chatto_client = None
  416. return False
  417. # Announce online presence so the bot appears online in the member list.
  418. # The server treats this as a TTL, so _presence_refresh_loop below has to
  419. # keep re-announcing it — a single call here lapses back to offline.
  420. await self._announce_online()
  421. self._closing = False
  422. # Start background realtime WS event stream loop.
  423. self._ws_task = asyncio.create_task(
  424. self._chattolib_event_loop(),
  425. name="chatto-event-stream",
  426. )
  427. self._presence_task = asyncio.create_task(
  428. self._presence_refresh_loop(),
  429. name="chatto-presence-refresh",
  430. )
  431. self._mark_connected()
  432. logger.info(
  433. "Chatto: connected to %s as '%s' (display_name: '%s' id: '%s')",
  434. self.chatto_config.base_url.value,
  435. self.me.login,
  436. self.me.display_name,
  437. self.me.id,
  438. )
  439. return True
  440. async def _announce_online(self) -> bool:
  441. """Tell the server we are online. Returns whether the call got through.
  442. Logged at warning level on failure: a silently dropped presence call is
  443. indistinguishable from a bot that is simply not running.
  444. """
  445. client = await self._get_chatto_client()
  446. if client is None:
  447. logger.warning(
  448. "Chatto: presence refresh failed, bot may appear offline: no client"
  449. )
  450. return False
  451. try:
  452. await client.set_presence(status=PresenceStatus.ONLINE)
  453. return True
  454. except Exception as exc:
  455. logger.warning(
  456. "Chatto: presence refresh failed, bot may appear offline: %s", exc
  457. )
  458. return False
  459. async def _presence_refresh_loop(self) -> None:
  460. """Re-announce ONLINE until disconnect, since presence expires server-side.
  461. Failures are not fatal — the next tick tries again, so a blip in the
  462. presence endpoint costs at most one interval of visible offline time.
  463. """
  464. while not self._closing:
  465. await self._sleep_interruptible(ChattoConstants.PRESENCE_REFRESH_INTERVAL)
  466. if self._closing:
  467. return
  468. await self._announce_online()
  469. async def disconnect(self) -> None:
  470. """Stop WebSocket, presence refresh, typing tasks, and clear state.
  471. BasePlatformAdapter override
  472. """
  473. # No explicit offline broadcast: chattolib rejects OFFLINE outright
  474. # ("stop refreshing to go offline"), so cancelling the refresh loop
  475. # below is what actually takes the bot offline.
  476. self._closing = True
  477. # Cancel all typing tasks
  478. for chat_id in list(self._typing_tasks.keys()):
  479. await self.stop_typing(chat_id)
  480. if self._ws_task and not self._ws_task.done():
  481. self._ws_task.cancel()
  482. try:
  483. await self._ws_task
  484. except (asyncio.CancelledError, Exception):
  485. logger.debug(
  486. "Chatto: websocket task ended during disconnect", exc_info=True
  487. )
  488. self._ws_task = None
  489. if self._presence_task and not self._presence_task.done():
  490. self._presence_task.cancel()
  491. try:
  492. await self._presence_task
  493. except (asyncio.CancelledError, Exception):
  494. logger.debug(
  495. "Chatto: presence task ended during disconnect", exc_info=True
  496. )
  497. self._presence_task = None
  498. if self._chatto_client:
  499. try:
  500. await self._chatto_client.close()
  501. except Exception:
  502. logger.exception("Chatto: error closing client")
  503. finally:
  504. self._chatto_client = None
  505. logger.info("Chatto: disconnected")
  506. self._mark_disconnected()
  507. async def _seed_room(self, room_id: str) -> None:
  508. """Seed high-water mark from the newest events so a restart doesn't replay history."""
  509. try:
  510. client = await self._get_chatto_client()
  511. if client is None:
  512. logger.debug(
  513. "Chatto: _seed_room aborted - no client available for %s", room_id
  514. )
  515. return
  516. timeline_page = await client.get_room_events(room_id)
  517. for ev in timeline_page.events:
  518. if ev.id:
  519. self._mark_seen(ev.id)
  520. logger.debug(
  521. "Chatto: seeded room %s with %d events",
  522. room_id,
  523. len(timeline_page.events),
  524. )
  525. except Exception as e:
  526. logger.warning("Chatto: get room events failed for %s: %s", room_id, e)
  527. # ------------------------------------------------------------------ #
  528. # Realtime Event List
  529. # ------------------------------------------------------------------ #
  530. def _mark_seen(self, event_id: str) -> None:
  531. # The deque's maxlen evicts the oldest ID — no manual trimming.
  532. self._seen.append(event_id)
  533. def _is_seen(self, event_id: str) -> bool:
  534. return event_id in self._seen
  535. # ------------------------------------------------------------------ #
  536. # WebSocket Realtime Transport
  537. # ------------------------------------------------------------------ #
  538. def _own_handles(self) -> set[str]:
  539. """The handles that address this bot, lowercased for comparison.
  540. Chatto resolves mentions case-insensitively (FDR-006), so ``@Hermes_Bot``
  541. and ``@hermes_bot`` are the same handle everywhere in our gates.
  542. """
  543. if not self.me:
  544. return set()
  545. return {
  546. handle.lower() for handle in (self.me.login, self.me.display_name) if handle
  547. }
  548. def _mention_candidates(self, body: str) -> list[str]:
  549. """Candidate @-handles in a message body, in order of appearance.
  550. Mirrors the Chatto web frontend's extraction (apps/frontend/src/lib/
  551. mentions.ts upstream): candidates come from ``ChattoConstants.
  552. MENTION_RE`` outside code regions. Mentions inside fenced code blocks
  553. and inline code spans do not resolve upstream either, so a ``@bob``
  554. quoted in a snippet must not gate our behaviour.
  555. """
  556. without_fences = re.sub(r"(?s)(```|~~~).*?(\1|$)", " ", body)
  557. without_code = re.sub(r"`[^`\n]*`", " ", without_fences)
  558. return ChattoConstants.MENTION_RE.findall(without_code)
  559. def _mentions_me(self, body: str) -> bool:
  560. """Whether the message addresses this bot.
  561. By login, by display name, or by a broadcast handle — ``@here`` speaks
  562. to everyone present and the bot is one of them, so naming a colleague
  563. alongside it does not take the bot out of the audience. Matching is
  564. case-insensitive, like every mention resolution in Chatto.
  565. """
  566. own = self._own_handles()
  567. for handle in self._mention_candidates(body):
  568. lowered = handle.lower()
  569. if lowered in own or lowered in ChattoConstants.BROADCAST_MENTIONS:
  570. return True
  571. return False
  572. async def _handle_belongs_to_a_user(self, handle: str) -> bool:
  573. """Whether ``handle`` is the login of a real Chatto user.
  574. The API carries no mention entities — ``mention_confirmation_token`` is
  575. reserved in the message descriptor — so an @-token is only a candidate
  576. until the directory confirms it. Results are cached both ways under the
  577. lowercased handle (the mention namespace is case-insensitive per
  578. FDR-006), since the same handles recur and a miss is as reusable as a
  579. hit. Only logins are looked up: matching another user's display name,
  580. as the web frontend does against its room member list, has no directory
  581. equivalent here.
  582. """
  583. cache_key = handle.lower()
  584. known = self._known_handles.get(cache_key)
  585. if known is not None:
  586. return known
  587. client = await self._get_chatto_client()
  588. if client is None:
  589. # Unresolved means "not confirmed", so the message goes through.
  590. return False
  591. try:
  592. member = await client.admin_get_member(login=handle)
  593. except Exception as exc:
  594. logger.debug("Chatto: could not resolve handle @%s: %s", handle, exc)
  595. return False
  596. # admin_get_member returns pb_to_dict(GetMemberResponse):
  597. # {"member": {"user": {...}, ...}, "roles": [...]} when found,
  598. # or {} when not found.
  599. if not member.get("member"):
  600. self._known_handles[cache_key] = False
  601. return False
  602. from chattolib.types import User
  603. user = User.parse(member["member"].get("user"))
  604. self._known_handles[cache_key] = user is not None
  605. return user is not None
  606. async def _mentions_someone_else(self, body: str) -> bool:
  607. """Whether the message @-mentions a person who is not this bot.
  608. Broadcast handles are not a person — they address everyone present,
  609. the bot included, so they do not count as someone else. A handle no
  610. user holds is not a mention at all: someone writing *about* mentioning
  611. ("per @-mention", "@nonexistent") is talking to us, and staying silent
  612. on a false positive is worse than answering one.
  613. """
  614. own = self._own_handles()
  615. for handle in self._mention_candidates(body):
  616. lowered = handle.lower()
  617. if lowered in ChattoConstants.BROADCAST_MENTIONS:
  618. continue
  619. if lowered in own:
  620. continue
  621. if await self._handle_belongs_to_a_user(handle):
  622. return True
  623. return False
  624. def _check_auth(self, user: User) -> bool:
  625. """Whether this Chatto user may talk to the agent.
  626. Deliberately our own gate instead of the gateway's authz_mixin: its
  627. group allowlists key on chat_type and per-platform env vars
  628. (``{PLATFORM}_GROUP_ALLOWED_USERS``), none of which fit Chatto's one
  629. flat member directory. ``CHATTO_ALLOWED_USERS`` matches login and id,
  630. ``CHATTO_ALLOW_ALL_USERS`` overrides both — the gate stays
  631. chat_type-independent by design.
  632. """
  633. if self._is_authorized(user.id, user.login):
  634. return True
  635. logger.warning(
  636. "Chatto: rejecting message from unauthorized user '%s' (%s)",
  637. user.login,
  638. user.id,
  639. )
  640. return False
  641. # ------------------------------------------------------------------ #
  642. # Room management over DM (/join, /leave)
  643. # ------------------------------------------------------------------ #
  644. _DM_COMMANDS = ("/join", "/leave")
  645. async def _handle_dm_command(self, room_id: str, body: str) -> bool:
  646. """Run a ``/join`` or ``/leave`` admin command sent as a direct message.
  647. Returns True when ``body`` is one of the commands — whether it
  648. succeeded or not — so the caller keeps it out of the agent pipeline.
  649. Membership lives on the Chatto server: a joined room reappears in
  650. every future ``list_rooms()`` and therefore survives restarts.
  651. """
  652. verb, _, argument = body.strip().partition(" ")
  653. if verb.lower() not in self._DM_COMMANDS:
  654. return False
  655. client = await self._get_chatto_client()
  656. if client is None:
  657. await self.send(chat_id=room_id, content="Chatto client is not connected.")
  658. return True
  659. argument = argument.strip()
  660. if not argument:
  661. await self.send(
  662. chat_id=room_id,
  663. content="Usage: /join <room-id or #name> | /leave <room-id or #name>",
  664. )
  665. return True
  666. error, target = await self._resolve_room_target(client, argument)
  667. if error or target is None:
  668. await self.send(chat_id=room_id, content=error or "Room lookup failed.")
  669. return True
  670. if verb.lower() == "/join":
  671. reply = await self._run_join(client, target)
  672. else:
  673. reply = await self._run_leave(client, target)
  674. await self.send(chat_id=room_id, content=reply)
  675. return True
  676. async def _resolve_room_target(
  677. self,
  678. client: ChattoClient,
  679. argument: str,
  680. ) -> tuple[str | None, RoomWithViewerState | None]:
  681. """Resolve a ``/join`` or ``/leave`` argument to a room.
  682. ``#name`` is looked up case-insensitively in a fresh directory scan
  683. (which also refreshes our name/kind caches); anything else is treated
  684. as a room ID and verified via GetRoom. An ambiguous name comes back as
  685. an error naming the candidates, so the admin can retry with an ID.
  686. """
  687. if not argument.startswith("#"):
  688. state = await client.get_room(argument)
  689. if state is None or state.room is None:
  690. return f"No room with ID '{argument}'.", None
  691. return None, state
  692. wanted = argument[1:].strip().casefold()
  693. # (state, room) pairs: a listed match's room is already narrowed here,
  694. # so the candidate listing below needs no fresh Optional dance.
  695. matches: list[tuple[RoomWithViewerState, Room]] = []
  696. for state in await client.list_rooms() or []:
  697. room_obj = state.room if state else None
  698. if room_obj and (room_obj.name or "").strip().casefold() == wanted:
  699. matches.append((state, room_obj))
  700. self._room_names[room_obj.id] = room_obj.name
  701. self._room_kinds[room_obj.id] = room_obj.kind
  702. if not matches:
  703. return f"No room named '{argument}'.", None
  704. if len(matches) > 1:
  705. candidates = "\n".join(f"• {room.name} ({room.id})" for _, room in matches)
  706. return (
  707. f"Several rooms are named '{argument}' — pick one by ID:\n{candidates}"
  708. ), None
  709. return None, matches[0][0]
  710. async def _run_join(self, client: ChattoClient, state: RoomWithViewerState) -> str:
  711. """Join a room via RoomService/JoinRoom and track it as joined.
  712. An account that already holds membership (invited natively in Chatto)
  713. needs no JoinRoom call — it only gets seeded and added to the list.
  714. Channel-kind rooms additionally report their mention-list status,
  715. because a channel on neither list stays silent and this reply is
  716. where users copy the room ID from (see ``_room_join_hint``).
  717. """
  718. room_obj = state.room
  719. if room_obj is None:
  720. # Unreachable via _resolve_room_target: both of its paths only
  721. # return states whose room they already inspected.
  722. return "Chatto returned an empty room record — try again."
  723. label = f"'{room_obj.name}' ({room_obj.id})"
  724. joined_room = room_obj
  725. if not state.viewer_state.is_member:
  726. try:
  727. joined_room = await client.join_room(room_obj.id) or room_obj
  728. except ChattoError as exc:
  729. logger.warning("Chatto: /join failed for %s (%s)", room_obj.id, exc)
  730. return f"Could not join {label}: {exc}"
  731. self._room_names[joined_room.id] = joined_room.name
  732. self._room_kinds[joined_room.id] = joined_room.kind
  733. if joined_room.id not in self._joined_room_ids:
  734. # Same rule as _refresh_rooms: silent rooms are joined read-only
  735. # — seeding history nothing will ever answer would be waste.
  736. if self._answers_in_room(joined_room.id):
  737. await self._seed_room(joined_room.id)
  738. else:
  739. logger.info(
  740. "Chatto: %s is on neither mention list - joined read-only",
  741. label,
  742. )
  743. self._joined_room_ids.append(joined_room.id)
  744. if room_obj.kind != RoomKind.DM:
  745. head = (
  746. f"Already a member of {label}."
  747. if state.viewer_state.is_member
  748. else f"Joined {label}."
  749. )
  750. return f"{head}\n{self._room_join_hint(room_obj.id)}"
  751. if state.viewer_state.is_member:
  752. return f"Already a member of {label} — listening there."
  753. return f"Joined {label}."
  754. async def _run_leave(self, client: ChattoClient, state: RoomWithViewerState) -> str:
  755. """Leave a room via RoomService/LeaveRoom and drop it from the joined list.
  756. Two rooms are refused: a DM conversation cannot be left, and leaving
  757. the configured home channel would silently break cron/notification
  758. delivery, which posts there through the standalone sender.
  759. """
  760. room_obj = state.room
  761. if room_obj is None:
  762. # Same invariant as _run_join: _resolve_room_target pre-inspects.
  763. return "Chatto returned an empty room record — try again."
  764. label = f"'{room_obj.name}' ({room_obj.id})"
  765. if room_obj.kind == RoomKind.DM:
  766. return "Direct messages cannot be left."
  767. home_id = (self.chatto_config.home_channel.value or "").strip()
  768. if home_id == room_obj.id:
  769. return (
  770. f"{label} is the configured home channel "
  771. "(CHATTO_HOME_CHANNEL); leaving it would break cron and "
  772. "notification delivery. Point CHATTO_HOME_CHANNEL elsewhere first."
  773. )
  774. try:
  775. left = await client.leave_room(room_obj.id)
  776. except ChattoError as exc:
  777. logger.warning("Chatto: /leave failed for %s (%s)", room_obj.id, exc)
  778. return f"Could not leave {label}: {exc}"
  779. if not left:
  780. return f"Chatto refused to leave {label}."
  781. if room_obj.id in self._joined_room_ids:
  782. self._joined_room_ids.remove(room_obj.id)
  783. # We are no longer part of this audience; keep no roster for it.
  784. self._evict_roster(room_obj.id)
  785. return f"Left {label}."
  786. def _room_join_hint(self, room_id: str) -> str:
  787. """The mention-list status appended to a channel-kind /join reply.
  788. Users do not know their room IDs by heart — this reply is where they
  789. copy them from, so a silent channel names both env vars verbatim,
  790. ready to paste into ~/.hermes/.env. Configuration resolves once at
  791. gateway startup, hence the restart note.
  792. """
  793. policy = self._room_policy(room_id)
  794. if policy == RoomPolicy.REQUIRE_MENTION:
  795. return (
  796. "This channel answers only @mentions "
  797. "(listed in CHATTO_REQUIRE_MENTION_ROOMS)."
  798. )
  799. if policy == RoomPolicy.OPEN:
  800. return (
  801. "This channel answers every message "
  802. "(listed in CHATTO_OPTIONAL_MENTION_ROOMS)."
  803. )
  804. return (
  805. "This channel stays silent until you list its ID in"
  806. " ~/.hermes/.env (then restart the gateway):\n"
  807. f" CHATTO_REQUIRE_MENTION_ROOMS={room_id} <- answer only @mentions\n"
  808. f" CHATTO_OPTIONAL_MENTION_ROOMS={room_id} <- answer every message"
  809. )
  810. # ------------------------------------------------------------------ #
  811. # Inbound attachments
  812. # ------------------------------------------------------------------ #
  813. async def _download_attachment_bytes(self, url: str) -> bytes:
  814. """Download an attachment, refusing to buffer more than the gateway cap.
  815. The Content-Length header is checked first so an oversized asset is
  816. rejected before a single chunk is read; the running total is re-checked
  817. as chunks arrive, because a missing or lying header must not smuggle an
  818. unbounded body past the cap.
  819. """
  820. max_bytes = get_inbound_media_max_bytes()
  821. chunks: list[bytes] = []
  822. total = 0
  823. async with (
  824. httpx.AsyncClient(
  825. timeout=ChattoConstants.HTTP_TIMEOUT,
  826. follow_redirects=True,
  827. ) as http,
  828. http.stream("GET", url) as resp,
  829. ):
  830. resp.raise_for_status()
  831. declared = resp.headers.get("content-length")
  832. if declared:
  833. try:
  834. declared_size = int(declared)
  835. except ValueError:
  836. logger.debug("Chatto: ignoring invalid Content-Length %r", declared)
  837. else:
  838. validate_inbound_media_size(
  839. declared_size,
  840. media_type="attachment",
  841. max_bytes=max_bytes,
  842. )
  843. async for chunk in resp.aiter_bytes():
  844. total += len(chunk)
  845. validate_inbound_media_size(
  846. total,
  847. media_type="attachment",
  848. max_bytes=max_bytes,
  849. )
  850. chunks.append(chunk)
  851. return b"".join(chunks)
  852. async def _cache_attachments(
  853. self,
  854. attachments: list[MessageAttachment],
  855. ) -> tuple[list[str], list[str], list[str]]:
  856. """Download message attachments into the gateway media cache.
  857. Returns ``(media_urls, media_types, media_kinds)`` — the paths are
  858. agent-visible cache paths, exactly what ``cache_media_bytes`` yields for
  859. every other platform. A failing attachment is logged and skipped: the
  860. message itself still reaches the agent.
  861. """
  862. media_urls: list[str] = []
  863. media_types: list[str] = []
  864. media_kinds: list[str] = []
  865. for att in attachments:
  866. url = att.asset_url.url if att.asset_url else ""
  867. filename = att.filename
  868. content_type = att.content_type
  869. if not url:
  870. # Videos are announced before transcoding finishes, so the
  871. # signed URL can legitimately be missing on arrival.
  872. logger.debug(
  873. "Chatto: attachment '%s' has no asset URL yet, skipping",
  874. filename,
  875. )
  876. continue
  877. try:
  878. data = await self._download_attachment_bytes(url)
  879. cached = cache_media_bytes(
  880. data,
  881. filename=filename,
  882. mime_type=content_type,
  883. )
  884. except Exception as e:
  885. logger.warning(
  886. "Chatto: failed to cache attachment '%s' (%s): %s",
  887. filename,
  888. content_type,
  889. e,
  890. )
  891. continue
  892. if cached is None:
  893. logger.warning(
  894. "Chatto: attachment '%s' (%s) could not be cached, skipping",
  895. filename,
  896. content_type,
  897. )
  898. continue
  899. media_urls.append(cached.path)
  900. media_types.append(cached.media_type)
  901. media_kinds.append(cached.kind)
  902. return media_urls, media_types, media_kinds
  903. @staticmethod
  904. def _message_type_for_media_kinds(media_kinds: list[str]) -> MessageType:
  905. """Pick the MessageType for a set of cached attachment kinds."""
  906. if "document" in media_kinds:
  907. return MessageType.DOCUMENT
  908. if "image" in media_kinds:
  909. return MessageType.PHOTO
  910. if "video" in media_kinds:
  911. return MessageType.VIDEO
  912. if "audio" in media_kinds:
  913. return MessageType.AUDIO
  914. return MessageType.TEXT
  915. async def _room_kind_for(
  916. self, client: ChattoClient, room_id: str
  917. ) -> RoomKind | None:
  918. """The room's kind, from cache or a fresh GetRoom lookup.
  919. Returns ``None`` when the room cannot be resolved — the caller treats
  920. that as "not dispatchable" rather than guessing a kind.
  921. """
  922. kind = self._room_kinds.get(room_id)
  923. if kind is not None:
  924. return kind
  925. room_viewer_state = await client.get_room(room_id)
  926. if room_viewer_state is None or room_viewer_state.room is None:
  927. return None
  928. kind = room_viewer_state.room.kind or RoomKind.UNSPECIFIED
  929. self._room_kinds[room_id] = kind
  930. return kind
  931. def _room_policy(self, room_id: str) -> RoomPolicy:
  932. """Which of the mention lists a channel-kind room is on.
  933. The two lists are mutually exclusive (enforced by
  934. ``hermes_validate_config``), so membership decides: optional beats
  935. require in the face of contradictory runtime config, and a room on
  936. neither list stays silent.
  937. """
  938. if room_id in self.chatto_config.optional_mention_rooms.value:
  939. return RoomPolicy.OPEN
  940. if room_id in self.chatto_config.require_mention_rooms.value:
  941. return RoomPolicy.REQUIRE_MENTION
  942. return RoomPolicy.SILENT
  943. def _answers_in_room(self, room_id: str) -> bool:
  944. """Whether inbound messages from this room reach the agent pipeline.
  945. Chatto knows only DMs and channels, so the split is ``kind == DM``:
  946. a DM always answers (``/join`` must stay reachable), every other
  947. room — channel-kind or an unknown kind, which is how servers that
  948. never set ``kind`` show up — opts in through the mention lists. A
  949. room whose policy is SILENT stays read-only (marked as read, never
  950. seeded or answered).
  951. """
  952. if self._room_kinds.get(room_id) == RoomKind.DM:
  953. return True
  954. return self._room_policy(room_id) != RoomPolicy.SILENT
  955. async def _room_roster_context(
  956. self, client: ChattoClient, room_id: str
  957. ) -> _RoomRoster | None:
  958. """Fetch the room's member roster as a fresh projection.
  959. The agent only ever sees its prompt: without this block it cannot know
  960. who else is in a channel, because unaddressed messages are dropped by
  961. mention gating long before they could teach it a name. Fetched users go
  962. into _user_cache so presence patching and mention resolution share one
  963. store. Returns ``None`` on failure or when nothing renderable remains —
  964. best-effort by design, a directory hiccup must never cost the turn.
  965. """
  966. try:
  967. # list_room_members() in 0.5.0b4 returns member IDs only;
  968. # batch_get_room_members() hydrates them into DirectoryMember.
  969. member_id_list, page = await client.list_room_members(
  970. room_id, limit=ChattoConstants.ROSTER_MEMBER_LIMIT
  971. )
  972. if not member_id_list:
  973. return None
  974. members = await client.batch_get_room_members(room_id, member_id_list)
  975. except Exception:
  976. logger.debug(
  977. "Chatto: could not list members of room %s", room_id, exc_info=True
  978. )
  979. return None
  980. logger.debug(
  981. "Chatto: roster for room %s: %d members fetched, total_count=%d",
  982. room_id,
  983. len(members),
  984. page.total_count,
  985. )
  986. member_ids: set[str] = set()
  987. users: list[User] = []
  988. for member in members:
  989. user = member.user
  990. if user is None or user.deleted:
  991. continue
  992. member_ids.add(user.id)
  993. self._user_cache[user.id] = user
  994. users.append(user)
  995. entries = [
  996. entry for entry in (self._roster_entry(user) for user in users) if entry
  997. ]
  998. if not entries:
  999. logger.debug(
  1000. "Chatto: roster for room %s empty after filtering "
  1001. "(%d fetched, all deleted/self/without user)",
  1002. room_id,
  1003. len(members),
  1004. )
  1005. return None
  1006. logger.debug("Chatto: roster for room %s: %d entries", room_id, len(entries))
  1007. unfetched = max(0, page.total_count - len(members))
  1008. return _RoomRoster(member_ids=member_ids, unfetched=unfetched)
  1009. def _roster_entry(self, user: User) -> str:
  1010. """One roster entry, e.g. ``@bob (Bob Example, online)``.
  1011. Skips deleted users and our own account (the agent knows itself);
  1012. display name and presence are optional parts of the parenthetical.
  1013. """
  1014. if user.deleted:
  1015. return ""
  1016. if self.me is not None and user.id == self.me.id:
  1017. return ""
  1018. entry = f"@{user.login}"
  1019. details = [
  1020. part
  1021. for part in (
  1022. user.display_name,
  1023. _PRESENCE_LABELS.get(user.presence_status, ""),
  1024. )
  1025. if part
  1026. ]
  1027. if details:
  1028. entry += f" ({', '.join(details)})"
  1029. return entry
  1030. def _roster_line(self, room_id: str) -> str:
  1031. """Render the room's roster line from its cached projection."""
  1032. roster = self._rosters.get(room_id)
  1033. if roster is None:
  1034. return ""
  1035. users = [self._user_cache.get(member_id) for member_id in roster.member_ids]
  1036. entries = [
  1037. entry
  1038. for entry in (
  1039. self._roster_entry(user) for user in users if user is not None
  1040. )
  1041. if entry
  1042. ]
  1043. more = f" … and {roster.unfetched} more" if roster.unfetched else ""
  1044. return ", ".join(entries) + more
  1045. def _announce_roster(self, room_id: str, roster: _RoomRoster) -> None:
  1046. """Record a freshly fetched roster as the room's projection.
  1047. Bounded like _dispatched_ids: appending past the cap drops the oldest
  1048. room's projection alongside its deque entry.
  1049. """
  1050. if room_id not in self._roster_announced:
  1051. oldest = (
  1052. self._roster_announced.popleft()
  1053. if len(self._roster_announced) == self._roster_announced.maxlen
  1054. else None
  1055. )
  1056. self._roster_announced.append(room_id)
  1057. if oldest is not None:
  1058. self._rosters.pop(oldest, None)
  1059. self._rosters[room_id] = roster
  1060. async def _thread_context_for(
  1061. self,
  1062. client: ChattoClient,
  1063. *,
  1064. room_id: str,
  1065. thread_root_event_id: str,
  1066. current_message_id: str,
  1067. source: Any,
  1068. ) -> str:
  1069. """The thread's earlier messages, delivered once per thread.
  1070. A Hermes thread session is keyed on the thread root and starts empty,
  1071. so the turn that opens a thread is answered with nothing but the reply
  1072. itself. The reported case: a cron job posts into a channel, its echo
  1073. is filtered as a self-event and never reaches a session, and a human
  1074. replies — rooting a thread whose root the agent has never seen. Without
  1075. this block the agent answers a message it cannot read.
  1076. Gated on ``thread_root_event_id``, not on the resolved ``thread_id``:
  1077. a fresh room message is anchored to itself (see _admit_and_build), and
  1078. that thread does not exist yet, so there is nothing to fetch. DMs never
  1079. thread and never seed.
  1080. Seeding is asked of the session store, not just the in-memory deque:
  1081. the marker is process-local and dies on every restart, while the
  1082. thread's session lives in state.db — so a restart would otherwise
  1083. re-seed every thread in the room and duplicate a context the session
  1084. already holds. The deque remains as a cheap timing pre-check for two
  1085. turns racing before the first session is written.
  1086. Best-effort by design, like the roster: a failed lookup leaves the
  1087. thread unseeded so the next turn retries.
  1088. """
  1089. if not thread_root_event_id:
  1090. return ""
  1091. if thread_root_event_id in self._seeded_threads:
  1092. return ""
  1093. if self._thread_session_exists(source):
  1094. # This thread already has a session, so its history is already in
  1095. # that session — recording it keeps a later turn from re-asking.
  1096. self._mark_thread_seeded(thread_root_event_id)
  1097. return ""
  1098. history = await self._thread_history(
  1099. client, room_id=room_id, thread_root_event_id=thread_root_event_id
  1100. )
  1101. if history is None:
  1102. logger.debug(
  1103. "Chatto: dispatching into thread %s without history (lookup failed)",
  1104. thread_root_event_id,
  1105. )
  1106. return ""
  1107. self._mark_thread_seeded(thread_root_event_id)
  1108. earlier = [m for m in history if m.id != current_message_id]
  1109. return self._thread_context_block(earlier)
  1110. async def _thread_history(
  1111. self, client: ChattoClient, *, room_id: str, thread_root_event_id: str
  1112. ) -> list[Message] | None:
  1113. """Fetch a thread's messages oldest-first, or ``None`` if the lookup failed.
  1114. The thread root is fetched explicitly when the timeline omits it: a
  1115. cron message is a plain room message until someone replies to it, and
  1116. whether the server lists it back as the thread's first event is not
  1117. something the client signature promises.
  1118. """
  1119. try:
  1120. page = await client.get_thread_events(
  1121. room_id,
  1122. thread_root_event_id,
  1123. limit=ChattoConstants.THREAD_CONTEXT_MESSAGE_LIMIT,
  1124. )
  1125. except Exception:
  1126. logger.debug(
  1127. "Chatto: get thread events failed for %s/%s",
  1128. room_id,
  1129. thread_root_event_id,
  1130. exc_info=True,
  1131. )
  1132. return None
  1133. messages = [ev.message for ev in page.events if ev.message is not None]
  1134. if not any(m.id == thread_root_event_id for m in messages):
  1135. messages = (
  1136. await self._thread_root_message(
  1137. client, room_id=room_id, thread_root_event_id=thread_root_event_id
  1138. )
  1139. + messages
  1140. )
  1141. # The server's ordering is not a contract we build on — sort by the
  1142. # timestamp each message carries, so the block reads like the thread.
  1143. return sorted(
  1144. messages,
  1145. key=lambda m: m.created_at or datetime.min.replace(tzinfo=UTC),
  1146. )
  1147. async def _thread_root_message(
  1148. self, client: ChattoClient, *, room_id: str, thread_root_event_id: str
  1149. ) -> list[Message]:
  1150. """The thread root as a list, so a miss concatenates cleanly.
  1151. Best-effort: an unknown root costs nothing but the missing root line.
  1152. """
  1153. try:
  1154. root = await client.get_message(
  1155. room_id=room_id, event_id=thread_root_event_id
  1156. )
  1157. except Exception:
  1158. logger.debug(
  1159. "Chatto: get thread root failed for %s/%s",
  1160. room_id,
  1161. thread_root_event_id,
  1162. exc_info=True,
  1163. )
  1164. return []
  1165. return [root] if root is not None and not root.deleted_at else []
  1166. def _mark_thread_seeded(self, thread_root_event_id: str) -> None:
  1167. """Record a thread whose history has been handed to its session."""
  1168. self._seeded_threads.append(thread_root_event_id)
  1169. def _thread_session_exists(self, source: Any) -> bool:
  1170. """Whether the gateway already has a session for this source.
  1171. The gateway's own routing index is the authority on "does this thread's
  1172. conversation already exist" — it is persistent (state.db), so unlike
  1173. the in-memory deque it answers correctly after a restart. Returns False
  1174. when no store is wired (tests, or a startup path before
  1175. ``set_session_store`` runs) so we fail open and seed.
  1176. """
  1177. store = getattr(self, "_session_store", None)
  1178. if store is None:
  1179. return False
  1180. return store.peek_session_id(self._session_key_for(source)) is not None
  1181. def _thread_context_block(self, messages: list[Message]) -> str:
  1182. """Render earlier thread messages as the agent's reference block.
  1183. Empty when there is nothing to say — a brand-new thread has no earlier
  1184. messages, and that is not worth a header. Over budget, the oldest
  1185. messages go first: the reply's immediate predecessors are what the
  1186. turn is about.
  1187. """
  1188. entries: list[str] = []
  1189. for message in messages:
  1190. entry = self._thread_message_entry(message)
  1191. if entry:
  1192. entries.append(entry)
  1193. if not entries:
  1194. return ""
  1195. dropped = 0
  1196. while (
  1197. entries
  1198. and len("\n".join(entries)) > ChattoConstants.THREAD_CONTEXT_CHAR_LIMIT
  1199. ):
  1200. entries.pop(0)
  1201. dropped += 1
  1202. if not entries:
  1203. return ""
  1204. body = "\n".join(entries)
  1205. omitted = f"… {dropped} earlier message(s) omitted\n" if dropped else ""
  1206. return f"{omitted}[Earlier in this thread]\n{body}"
  1207. def _thread_message_entry(self, message: Message) -> str:
  1208. """One rendered thread message, or ``""`` for one with no body.
  1209. Attachment-only messages render empty: the block is text context, and
  1210. inventing a placeholder for a picture the agent cannot see here would
  1211. be noise.
  1212. """
  1213. body = (message.body or "").strip()
  1214. if not body:
  1215. return ""
  1216. me = self.me
  1217. user = self._user_cache.get(message.actor_id)
  1218. if me is not None and message.actor_id == me.id:
  1219. # The agent's own output — often a cron message it never saw
  1220. # arrive. Labelling it keeps "I said this" distinguishable from a
  1221. # third party talking.
  1222. return f"{me.login} (this bot): {body}"
  1223. author = f"@{user.login}" if user else f"@{message.actor_id}"
  1224. if not self._is_authorized(message.actor_id, user.login if user else None):
  1225. # Auth gates the trigger message only; the rest of a shared thread
  1226. # is reference text from people who may not be on the allowlist,
  1227. # so it is marked as unverified rather than presented as trusted
  1228. # input. An unknown author lands here too — fail closed.
  1229. return f"{author} (unverified): {body}"
  1230. return f"{author}: {body}"
  1231. def _is_authorized(self, actor_id: str, login: str | None = None) -> bool:
  1232. """Whether a Chatto user may talk to the agent, by id or login.
  1233. The allowlist test itself, without the logging _check_auth adds: a
  1234. rendering path asks the same question about someone quoted into the
  1235. prompt, where denial is a normal sight rather than an event worth a
  1236. warning. Callers pass the login when they already hold the User;
  1237. otherwise it is read from the user cache.
  1238. """
  1239. if self.chatto_config.allow_all_users.value:
  1240. return True
  1241. allowed = self.chatto_config.allowed_users.value
  1242. if actor_id in allowed:
  1243. return True
  1244. if login is None:
  1245. user = self._user_cache.get(actor_id)
  1246. login = user.login if user else None
  1247. return login is not None and login in allowed
  1248. def _evict_roster(self, room_id: str) -> None:
  1249. """Drop all roster state for a room.
  1250. Called on membership changes (user_joined/left_room), when we leave a
  1251. room ourselves, and on reconnect — protocol v1 has no presence snapshot
  1252. on subscribe, so a discarded cache is what forces one honest refetch.
  1253. """
  1254. try:
  1255. self._roster_announced.remove(room_id)
  1256. except ValueError:
  1257. pass
  1258. self._rosters.pop(room_id, None)
  1259. async def _roster_for_room(self, client: ChattoClient, *, room_id: str) -> str:
  1260. """The room's roster line for this turn — delivered on every turn.
  1261. Threads hold isolated sessions and the agent only sees
  1262. channel_context per dispatch, so each channel turn carries the current
  1263. audience rather than deduplicating it. The projection exists to spare
  1264. the directory, not the prompt: presence patches land in _user_cache
  1265. in place, membership events or a reconnect evict the room so its next
  1266. turn refetches once. An unprojected room retries on its next turn
  1267. until a lookup succeeds.
  1268. """
  1269. if room_id not in self._roster_announced:
  1270. fetched = await self._room_roster_context(client, room_id)
  1271. if fetched is None:
  1272. return ""
  1273. self._announce_roster(room_id, fetched)
  1274. return self._roster_line(room_id)
  1275. async def _dispatch_message_posted(
  1276. self, payload: MessagePostedEvent, event_id: str
  1277. ) -> None:
  1278. # Respond-room gate first: read-only memberships must not cost a
  1279. # single API call, so this runs before fetch_message and _get_chatto_client.
  1280. if not self._answers_in_room(payload.room_id):
  1281. logger.debug(
  1282. "Chatto: message from read-only room %s ignored", payload.room_id
  1283. )
  1284. return
  1285. client = await self._get_chatto_client()
  1286. if client is None:
  1287. logger.warning("Chatto: dropping message - no client available")
  1288. return
  1289. logger.debug("Chatto WS: 'message_posted' payload:%s", payload)
  1290. message = await client.get_message(room_id=payload.room_id, event_id=event_id)
  1291. if message is None or message.deleted_at:
  1292. return
  1293. message_body = message.body or ""
  1294. logger.debug("message: %s", message)
  1295. # A message carrying only an image/PDF has an empty body — dropping it
  1296. # here is what made attachments sent to Hermes disappear silently.
  1297. if not message_body and not message.attachments:
  1298. return
  1299. event = await self._admit_and_build(
  1300. client,
  1301. room_id=payload.room_id,
  1302. message=message,
  1303. source_message_id=event_id,
  1304. thread_root_event_id=payload.thread_root_event_id or None,
  1305. )
  1306. if event is None:
  1307. return
  1308. self._remember_dispatched(event.message_id or "")
  1309. logger.info("Chatto: dispatching message to Hermes")
  1310. await self.handle_message(event)
  1311. return
  1312. def _remember_dispatched(self, message_id: str) -> None:
  1313. """Record a message ID as handed to the gateway, capped like _seen."""
  1314. if not message_id:
  1315. return
  1316. self._dispatched_ids.append(message_id)
  1317. def _edit_is_fresh(self, message: Message) -> bool:
  1318. """Whether this edit is young enough to still be processed.
  1319. Age is measured against the posting time, so an edit to an hours-old
  1320. message cannot resurrect a settled conversation even when it arrives
  1321. right now.
  1322. """
  1323. if message.created_at is None or message.updated_at is None:
  1324. return True
  1325. age_seconds = (message.updated_at - message.created_at).total_seconds()
  1326. return age_seconds <= self.chatto_config.edit_window.value
  1327. def _session_key_for(self, source) -> str:
  1328. """The gateway's own session key for this source.
  1329. Built with exactly the inputs ``handle_message`` uses, so lookups in
  1330. ``_processing`` and calls to ``cancel_session_processing`` hit the
  1331. same session the gateway is running.
  1332. """
  1333. return build_session_key(
  1334. source,
  1335. group_sessions_per_user=self.config.extra.get(
  1336. "group_sessions_per_user", True
  1337. ),
  1338. thread_sessions_per_user=self.config.extra.get(
  1339. "thread_sessions_per_user", False
  1340. ),
  1341. )
  1342. async def _dispatch_message_edited(self, payload: Any) -> None:
  1343. """Route an inbound edit according to the edit-dispatch contract.
  1344. Three outcomes for the edited message:
  1345. - currently being processed → cancel that turn and re-dispatch with
  1346. the corrected text (the cancelled turn reports 🚫 via its
  1347. CANCELLED outcome hook),
  1348. - never dispatched (e.g. a forgotten @mention added later) → re-run
  1349. the admission gates against the new body and answer for real,
  1350. - already answered → stay answered.
  1351. Edits whose text parses as a DM membership command are dropped: the
  1352. command ran when the message was posted and must not run again.
  1353. """
  1354. if not self.chatto_config.edit_dispatch.value:
  1355. return
  1356. # Read-only memberships cost no API call, mirroring the posted path.
  1357. if not self._answers_in_room(payload.room_id):
  1358. logger.debug("Chatto: edit from read-only room %s ignored", payload.room_id)
  1359. return
  1360. client = await self._get_chatto_client()
  1361. if client is None:
  1362. logger.warning("Chatto: dropping edit - no client available")
  1363. return
  1364. logger.debug("Chatto WS: 'message_edited' payload:%s", payload)
  1365. message = await client.get_message(
  1366. room_id=payload.room_id, event_id=payload.message_event_id
  1367. )
  1368. if message is None or message.deleted_at:
  1369. return
  1370. message_body = message.body or ""
  1371. if not message_body and not message.attachments:
  1372. return
  1373. if not self._edit_is_fresh(message):
  1374. logger.debug(
  1375. "Chatto: edit of msg %s outside the edit window",
  1376. payload.message_event_id,
  1377. )
  1378. return
  1379. # Cheap triage before the admission pipeline: an edit to an
  1380. # already-settled message (dispatched, neither running nor queued)
  1381. # must not cost get_room/media calls or re-fire acknowledgements.
  1382. was_dispatched = payload.message_event_id in self._dispatched_ids
  1383. if (
  1384. was_dispatched
  1385. and payload.message_event_id not in self._processing.values()
  1386. and not any(
  1387. pending.message_id == payload.message_event_id
  1388. for pending in self._pending_messages.values()
  1389. )
  1390. ):
  1391. logger.debug(
  1392. "Chatto: edit of already-answered msg %s ignored",
  1393. payload.message_event_id,
  1394. )
  1395. return
  1396. event = await self._admit_and_build(
  1397. client,
  1398. room_id=payload.room_id,
  1399. message=message,
  1400. source_message_id=payload.message_event_id,
  1401. thread_root_event_id=message.thread_root_event_id or None,
  1402. allow_dm_commands=False,
  1403. )
  1404. if event is None:
  1405. return
  1406. session_key = self._session_key_for(event.source)
  1407. if self._processing.get(session_key) == payload.message_event_id:
  1408. logger.info(
  1409. "Chatto: msg %s edited mid-run - restarting the turn",
  1410. payload.message_event_id,
  1411. )
  1412. # The cancelled task's completion hook clears _processing and
  1413. # reports 🚫 before this coroutine moves on, because cancel
  1414. # awaits the task. Queued follow-ups must survive.
  1415. await self.cancel_session_processing(
  1416. session_key, release_guard=True, discard_pending=False
  1417. )
  1418. elif not was_dispatched:
  1419. logger.info(
  1420. "Chatto: msg %s was never dispatched - edit starts a fresh turn",
  1421. payload.message_event_id,
  1422. )
  1423. else:
  1424. pending = self._pending_messages.get(session_key)
  1425. if pending is not None and pending.message_id == payload.message_event_id:
  1426. # Still queued behind the running turn: correct it in place
  1427. # instead of answering stale wording later.
  1428. pending.text = event.text
  1429. logger.info(
  1430. "Chatto: queued msg %s updated to its edited text",
  1431. payload.message_event_id,
  1432. )
  1433. return
  1434. logger.debug(
  1435. "Chatto: edit of already-answered msg %s ignored",
  1436. payload.message_event_id,
  1437. )
  1438. return
  1439. self._remember_dispatched(event.message_id or "")
  1440. logger.info("Chatto: dispatching edited message to Hermes")
  1441. await self.handle_message(event)
  1442. async def _admit_and_build(
  1443. self,
  1444. client: ChattoClient,
  1445. *,
  1446. room_id: str,
  1447. message: Message,
  1448. source_message_id: str,
  1449. thread_root_event_id: str | None,
  1450. allow_dm_commands: bool = True,
  1451. ) -> MessageEvent | None:
  1452. """Run one hydrated inbound message through the admission pipeline.
  1453. Shared by the posted and the edited path: user resolution, auth,
  1454. mention gates, thread anchoring and media caching all behave
  1455. identically for both. Returns ``None`` for anything that must not
  1456. reach the agent. DM membership commands are executed here (side
  1457. effect) unless ``allow_dm_commands`` is False — edits pass False so
  1458. a corrected command line neither runs twice nor leaks to the agent.
  1459. A dispatch that opens a channel thread additionally carries the
  1460. member roster in ``channel_context`` (side effect: once the roster
  1461. was fetched, the thread root is recorded as announced).
  1462. The caller owns dispatching: a non-None result still needs
  1463. ``handle_message()``.
  1464. """
  1465. user = self._user_cache.get(message.actor_id)
  1466. if user is None:
  1467. logger.debug("Chatto: resolving user %s", message.actor_id)
  1468. member = await client.get_room_member(
  1469. room_id=message.room_id, user_id=message.actor_id
  1470. )
  1471. if member is None or member.user is None:
  1472. logger.debug(
  1473. "Chatto: dropping message from unknown user %s",
  1474. message.actor_id,
  1475. )
  1476. return None
  1477. user = member.user
  1478. self._user_cache[user.id] = user
  1479. if not self._check_auth(user):
  1480. logger.debug(
  1481. "Chatto: dropping message from user %s (auth denied)",
  1482. message.actor_id,
  1483. )
  1484. return None
  1485. room_kind = await self._room_kind_for(client, message.room_id)
  1486. if room_kind is None:
  1487. logger.debug(
  1488. "Chatto: dropping message from room %s (room_kind lookup failed)",
  1489. message.room_id,
  1490. )
  1491. return None
  1492. message_body = message.body or ""
  1493. logger.debug("message_body: %s room_kind: %s", message_body, room_kind)
  1494. # Membership commands ride in over DMs only: they change what the bot
  1495. # listens to and must never reach the agent pipeline or the mention
  1496. # gates.
  1497. if room_kind == RoomKind.DM:
  1498. if allow_dm_commands:
  1499. if await self._handle_dm_command(room_id, message_body):
  1500. return None
  1501. elif message_body.startswith("/"):
  1502. logger.debug("Chatto: edited DM command %r not re-run", message_body)
  1503. return None
  1504. # Mention gating deliberately covers everything but DMs: in a channel
  1505. # the bot is one of many listeners and must be addressed, whereas a DM
  1506. # is already addressed at it. Chatto has no group rooms — any
  1507. # multi-participant surface is a channel, and a room whose kind the
  1508. # server never set counts as one too (see _answers_in_room). The
  1509. # shared _mentions_me gate keeps this path and the someone-else check
  1510. # below on one definition of "addressed", broadcast handles included.
  1511. if room_kind != RoomKind.DM:
  1512. policy = self._room_policy(room_id)
  1513. if policy == RoomPolicy.REQUIRE_MENTION and not self._mentions_me(
  1514. message_body
  1515. ):
  1516. logger.debug(
  1517. "Chatto: dropping unaddressed message from %s (policy %s)",
  1518. room_id,
  1519. policy.value,
  1520. )
  1521. return None
  1522. # In an open channel we see every message, including ones plainly
  1523. # aimed at a named colleague. Answering those would be barging in,
  1524. # so acknowledge that we read it and stay quiet. Checked after the
  1525. # bot-mention test above, so a message naming us *and* someone
  1526. # else still counts as ours.
  1527. if (
  1528. policy == RoomPolicy.OPEN
  1529. and not self._mentions_me(message_body)
  1530. and await self._mentions_someone_else(message_body)
  1531. ):
  1532. logger.info(
  1533. "Chatto: message addresses someone else, acknowledging only"
  1534. )
  1535. if self.chatto_config.reactions.value:
  1536. await self.add_reaction(message.room_id, message.id, "🫥")
  1537. return None
  1538. # Thread anchoring — if the incoming message is inside a Chatto thread, we
  1539. # keep that thread by default; otherwise leave thread_id unset so
  1540. # replies land at the root.
  1541. thread_id = (
  1542. thread_root_event_id or None
  1543. ) # we could also take the room id but then, we're in a thread already.
  1544. if not thread_id and room_kind != RoomKind.DM:
  1545. thread_id = message.id
  1546. logger.debug(
  1547. "Chatto: thread anchoring for msg %s: incoming thread_root_event_id=%r, "
  1548. "room_kind=%s, anchored thread_id=%r",
  1549. message.id,
  1550. message.thread_root_event_id,
  1551. room_kind.name,
  1552. thread_id,
  1553. )
  1554. # Every channel turn carries the room's roster: threads hold isolated
  1555. # sessions and the agent only sees channel_context per dispatch, so a
  1556. # repeat is not deduplication but the point (see _roster_for_room).
  1557. # The gateway prepends channel_context above the message text, so the
  1558. # roster never mingles with what the user actually wrote.
  1559. source = self.build_source(
  1560. chat_id=room_id,
  1561. chat_name=self._room_names.get(message.room_id),
  1562. chat_type=chat_type_for_room_kind(room_kind),
  1563. user_id=message.actor_id,
  1564. user_name=user.login, # use login, because display_name is changeable by anyone.
  1565. thread_id=thread_id,
  1566. message_id=source_message_id,
  1567. role_authorized=True,
  1568. )
  1569. # Channel context rides ahead of the message text as read-only
  1570. # reference: the roster names the audience, the thread block carries
  1571. # what the thread already said. DMs get neither — there is no audience
  1572. # to name and no thread to catch up on. Built after the source, because
  1573. # the thread block asks the session store whether this thread's session
  1574. # already has the history (see _thread_context_for).
  1575. context_blocks: list[str] = []
  1576. if room_kind != RoomKind.DM:
  1577. logger.debug(
  1578. "Chatto: fetching roster for room %s",
  1579. room_id,
  1580. )
  1581. roster = await self._roster_for_room(client, room_id=room_id)
  1582. if roster:
  1583. context_blocks.append(roster)
  1584. else:
  1585. logger.debug(
  1586. "Chatto: dispatching channel message without roster "
  1587. "(lookup failed or empty)"
  1588. )
  1589. thread_context = await self._thread_context_for(
  1590. client,
  1591. room_id=room_id,
  1592. thread_root_event_id=thread_root_event_id or "",
  1593. current_message_id=message.id,
  1594. source=source,
  1595. )
  1596. if thread_context:
  1597. context_blocks.append(thread_context)
  1598. # prepare a MessageEvent
  1599. message_event = MessageEvent(
  1600. text=message_body,
  1601. source=source,
  1602. message_id=message.id,
  1603. timestamp=message.created_at or datetime.now(UTC),
  1604. raw_message=message,
  1605. reply_to_message_id=message.in_reply_to,
  1606. channel_context="\n\n".join(context_blocks) or None,
  1607. )
  1608. if message_event.is_command():
  1609. message_event.message_type = MessageType.COMMAND
  1610. # Attachments — download and hand the local cache paths to the gateway,
  1611. # which runs vision enrichment / document extraction off media_urls.
  1612. (
  1613. message_event.media_urls,
  1614. message_event.media_types,
  1615. media_kinds,
  1616. ) = await self._cache_attachments(list(message.attachments))
  1617. if media_kinds:
  1618. # Same precedence as the Teams/Signal adapters: document-context
  1619. # injection gates strictly on DOCUMENT, image handling keys off the
  1620. # per-path image/* MIME regardless of message_type.
  1621. message_event.message_type = self._message_type_for_media_kinds(media_kinds)
  1622. else:
  1623. message_event.message_type = MessageType.TEXT
  1624. logger.debug("Chatto: MessageEvent: %s", message_event)
  1625. return message_event
  1626. async def _forward_reaction(
  1627. self,
  1628. event: RealtimeEvent,
  1629. payload: Any,
  1630. *,
  1631. removed: bool,
  1632. ) -> None:
  1633. """Forward a human reaction to the gateway's reaction hook surface.
  1634. The handler is registered by the gateway via ``set_reaction_handler``
  1635. and fans out as ``reaction:added`` / ``reaction:removed`` through the
  1636. HookRegistry. The dict shape mirrors the Slack adapter's — hook
  1637. consumers are written against that contract, not against a per-platform
  1638. one. Our own lifecycle reactions (👀/✅/❌) are dropped: forwarding them
  1639. would feed the agent its own markers.
  1640. """
  1641. actor_id = event.actor_id
  1642. if actor_id and self.me and actor_id == self.me.id:
  1643. return
  1644. if not payload.room_id or not payload.message_event_id or not actor_id:
  1645. return
  1646. handler = self._reaction_handler
  1647. if handler is None:
  1648. return
  1649. action = "removed" if removed else "added"
  1650. try:
  1651. await handler(
  1652. {
  1653. "platform": ChattoConstants.PLATFORM_NAME,
  1654. "event_name": f"reaction:{action}",
  1655. "reaction": payload.emoji,
  1656. "user_id": actor_id,
  1657. "item_user_id": None,
  1658. "item_type": "message",
  1659. "channel_id": payload.room_id,
  1660. "message_ts": payload.message_event_id,
  1661. "event_ts": event.id,
  1662. "raw_event": event,
  1663. },
  1664. )
  1665. except Exception: # pragma: no cover - the hook contract is non-blocking
  1666. logger.debug("Chatto: reaction hook forwarding failed", exc_info=True)
  1667. async def _handle_realtime_event(self, event: RealtimeEvent) -> None:
  1668. if self._is_seen(event.id):
  1669. return
  1670. if event.actor_id is None:
  1671. return
  1672. logger.debug("EVENT happened: '%s' from %s", event.kind, event.actor_id)
  1673. # Self-event filter — the actor_id on the envelope is authoritative.
  1674. actor_id = event.actor_id
  1675. if actor_id and self.me and actor_id == self.me.id:
  1676. return
  1677. if event.kind == "message_posted":
  1678. await self._dispatch_message_posted(event.payload, event.id)
  1679. elif event.kind == "message_edited":
  1680. await self._dispatch_message_edited(event.payload)
  1681. elif event.kind in ("reaction_added", "reaction_removed"):
  1682. await self._forward_reaction(
  1683. event,
  1684. event.payload,
  1685. removed=event.kind == "reaction_removed",
  1686. )
  1687. elif event.kind == "presence_changed":
  1688. # A participant's presence changed — patch the cached user in place.
  1689. if (user := self._user_cache.get(event.payload.user_id)) is not None:
  1690. user.presence_status = event.payload.status
  1691. logger.debug(
  1692. "Chatto: patched presence of %s to %s",
  1693. event.payload.user_id,
  1694. event.payload.status.name,
  1695. )
  1696. elif event.kind in ("user_joined_room", "user_left_room"):
  1697. # Membership moved — the room's roster projection is now wrong, so
  1698. # discard it; the next turn there refetches once and re-delivers.
  1699. if event.payload.room_id in self._roster_announced:
  1700. logger.debug(
  1701. "Chatto: %s invalidated the roster of room %s",
  1702. event.kind,
  1703. event.payload.room_id,
  1704. )
  1705. self._evict_roster(event.payload.room_id)
  1706. # confirmed:
  1707. # NOTE: "projection_event" (and "caught_up") belong to realtime protocol
  1708. # v2 on unreleased Chatto main — chattolib speaks v1 and can never
  1709. # deliver them here. Revisit when vendored chattolib gains v2 typing.
  1710. elif event.kind in (
  1711. "mention_notification",
  1712. "notification_dismissed",
  1713. "room_marked_as_read",
  1714. "user_typing",
  1715. "notification_created",
  1716. "new_direct_message_notification",
  1717. "message_retracted",
  1718. "thread_created",
  1719. "thread_follow_changed",
  1720. "room_updated",
  1721. "room_groups_updated",
  1722. ):
  1723. logger.debug(
  1724. "Chatto: '%s' event received. Not yet implemented or not needed.",
  1725. event.kind,
  1726. )
  1727. else:
  1728. logger.warning("Chatto: unknown event kind: '%s'", event.kind)
  1729. async def _chattolib_event_loop(self) -> None:
  1730. """Event loop using chattolib's stream_events.
  1731. This replaces the manual WebSocket loop with chattolib's high-level
  1732. stream_events() which provides pre-decoded RealtimeEvent objects.
  1733. """
  1734. delay = ChattoConstants.WS_RECONNECT_INITIAL_BACKOFF
  1735. while not self._closing:
  1736. try:
  1737. client = await self._require_chatto_client()
  1738. await self._refresh_rooms()
  1739. logger.info(
  1740. "Chatto: starting chattolib event stream with %d rooms",
  1741. len(self._joined_room_ids),
  1742. )
  1743. async for event in stream_events(client):
  1744. if self._closing:
  1745. return
  1746. if isinstance(event, RealtimeEvent):
  1747. await self._handle_realtime_event(event)
  1748. # Iterator exited cleanly — treat as a normal close and reconnect
  1749. # with the local backoff (no server hint available).
  1750. logger.info("Chatto: realtime stream ended, reconnecting")
  1751. except asyncio.CancelledError:
  1752. return
  1753. except ChattoRealtimeCloseError as exc:
  1754. if not exc.reconnect:
  1755. logger.error(
  1756. "Chatto: realtime closed by server (%s: %s), not reconnecting",
  1757. exc.code,
  1758. exc.message,
  1759. )
  1760. return
  1761. wait = max(
  1762. exc.retry_after_ms / 1000.0,
  1763. ChattoConstants.WS_RECONNECT_INITIAL_BACKOFF,
  1764. )
  1765. logger.warning(
  1766. "Chatto: realtime closed by server (%s), reconnecting in %.1fs",
  1767. exc.code,
  1768. wait,
  1769. )
  1770. delay = (
  1771. ChattoConstants.WS_RECONNECT_INITIAL_BACKOFF
  1772. ) # server hint supersedes local backoff
  1773. await self._sleep_interruptible(wait)
  1774. continue
  1775. except ChattoRealtimeError as exc:
  1776. if exc.fatal:
  1777. logger.error(
  1778. "Chatto: fatal realtime error (%s): %s", exc.code, exc.message
  1779. )
  1780. return
  1781. logger.warning(
  1782. "Chatto: realtime error (%s: %s), reconnecting in %.1fs",
  1783. exc.code,
  1784. exc.message,
  1785. delay,
  1786. )
  1787. except Exception as exc:
  1788. cause = exc.__cause__
  1789. if cause is not None and isinstance(cause, TypeError):
  1790. logger.warning(
  1791. "Chatto: realtime wire corruption (%s), reconnecting in %.1fs",
  1792. cause,
  1793. delay,
  1794. )
  1795. else:
  1796. logger.warning(
  1797. "Chatto: unexpected realtime error: %s, reconnecting in %.1fs",
  1798. exc,
  1799. delay,
  1800. )
  1801. if self._closing:
  1802. return
  1803. jitter = delay * 0.2 * random.random()
  1804. await self._sleep_interruptible(delay + jitter)
  1805. delay = min(delay * 2, ChattoConstants.WS_RECONNECT_MAX_BACKOFF)
  1806. async def _sleep_interruptible(self, seconds: float) -> None:
  1807. """Sleep in short slices so disconnect() cancels promptly."""
  1808. end = asyncio.get_running_loop().time() + seconds
  1809. while not self._closing:
  1810. remaining = end - asyncio.get_running_loop().time()
  1811. if remaining <= 0:
  1812. return
  1813. await asyncio.sleep(min(remaining, 0.5))
  1814. def _warn_if_home_channel_unjoined(self, member_ids: set[str]) -> None:
  1815. """Warn once when CHATTO_HOME_CHANNEL names a room the bot is not in.
  1816. Standalone cron delivery posts straight into that room with a fresh
  1817. client and no join logic of its own — without server-side membership
  1818. every proactive send fails there.
  1819. """
  1820. home_id = (self.chatto_config.home_channel.value or "").strip()
  1821. if (
  1822. not home_id
  1823. or home_id in member_ids
  1824. or home_id in self._joined_room_ids
  1825. or self._home_warning_logged
  1826. ):
  1827. return
  1828. self._home_warning_logged = True
  1829. logger.warning(
  1830. "Chatto: CHATTO_HOME_CHANNEL '%s' is not a joined room - cron and "
  1831. "notification delivery will fail until the bot joins it (invite "
  1832. "the account natively in Chatto, or DM it '/join').",
  1833. home_id,
  1834. )
  1835. async def _resolve_dm_partner(self, client: ChattoClient, room_id: str) -> None:
  1836. """Cache the chat partner's login for a DM room, best-effort.
  1837. DM rooms carry no usable name of their own, so the joined-rooms
  1838. summary names the other side instead. Resolved once per room and
  1839. session; without our own user (pre-connect) or on a directory error
  1840. the raw room name stays in place.
  1841. """
  1842. if room_id in self._dm_partners or self.me is None:
  1843. return
  1844. try:
  1845. member_ids, _page = await client.list_room_members(room_id)
  1846. if not member_ids:
  1847. return
  1848. members = await client.batch_get_room_members(room_id, member_ids)
  1849. except Exception:
  1850. logger.debug(
  1851. "Chatto: could not list members of DM %s", room_id, exc_info=True
  1852. )
  1853. return
  1854. for member in members:
  1855. user = member.user
  1856. if user and user.id != self.me.id:
  1857. self._dm_partners[room_id] = user.login or user.display_name
  1858. return
  1859. def _joined_room_label(self, room_id: str) -> str:
  1860. """One entry for the joined-rooms summary, with how-we-answer tags.
  1861. The tag names the room's participation mode at a glance: DMs answer
  1862. unconditionally, listed channels answer on mentions or every message,
  1863. and an unlisted channel is read-only. A DM is labelled with its chat
  1864. partner's login rather than its (empty) room name, and names are
  1865. quoted so empty strings and spaces stay visible.
  1866. """
  1867. name = self._room_names.get(room_id, room_id)
  1868. if self._room_kinds.get(room_id) == RoomKind.DM:
  1869. tags = "[dm]"
  1870. name = self._dm_partners.get(room_id) or name
  1871. else:
  1872. policy = self._room_policy(room_id)
  1873. if policy == RoomPolicy.REQUIRE_MENTION:
  1874. tags = "[on-mention]"
  1875. elif policy == RoomPolicy.OPEN:
  1876. tags = "[every-message]"
  1877. else:
  1878. tags = "[read-only]"
  1879. if room_id in self._universal_room_ids:
  1880. tags += " [universal]"
  1881. return f'"{name}" ({room_id}) {tags}'
  1882. def _log_joined_rooms(self) -> None:
  1883. """Log the joined rooms and how the bot answers in each of them."""
  1884. labels = [self._joined_room_label(rid) for rid in self._joined_room_ids]
  1885. logger.info(
  1886. "Chatto WS: currently joined in %d room(s): %s",
  1887. len(labels),
  1888. ", ".join(labels),
  1889. )
  1890. async def _refresh_rooms(self) -> None:
  1891. """Refresh room list via ConnectRPC, join and seed any newly discovered rooms.
  1892. Runs once per (re)connect, so this is also the bootstrap point for the
  1893. roster projection: protocol v1 sends no presence snapshot on subscribe,
  1894. meaning cached presence could only heal on the next random change.
  1895. Discarding it makes each announced room's first turn refetch fresh
  1896. data — lazily, so quiet rooms stay free of API calls.
  1897. """
  1898. client = await self._get_chatto_client()
  1899. if client is None:
  1900. logger.warning("Chatto WS: _refresh_rooms aborted - no client available")
  1901. return
  1902. for announced in list(self._roster_announced):
  1903. self._evict_roster(announced)
  1904. try:
  1905. rooms_list = await client.list_rooms()
  1906. member_ids: set[str] = set()
  1907. new_room_ids: list[str] = []
  1908. for room_with_state in rooms_list:
  1909. if not room_with_state:
  1910. continue
  1911. room_obj = room_with_state.room or None
  1912. if not room_obj:
  1913. continue
  1914. self._room_names[room_obj.id] = room_obj.name
  1915. self._room_kinds[room_obj.id] = room_obj.kind
  1916. if room_obj.kind == RoomKind.DM:
  1917. await self._resolve_dm_partner(client, room_obj.id)
  1918. if room_obj.universal:
  1919. self._universal_room_ids.add(room_obj.id)
  1920. if not room_with_state.viewer_state.is_member:
  1921. continue
  1922. member_ids.add(room_obj.id)
  1923. if room_obj.id not in self._joined_room_ids:
  1924. new_room_ids.append(room_obj.id)
  1925. # Left via /leave, kicked, deleted: rooms we no longer belong
  1926. # to drop out here — otherwise the next refresh would quietly
  1927. # re-add what /leave just removed.
  1928. stale_room_ids = [
  1929. rid for rid in self._joined_room_ids if rid not in member_ids
  1930. ]
  1931. for rid in stale_room_ids:
  1932. self._joined_room_ids.remove(rid)
  1933. self._evict_roster(rid)
  1934. if stale_room_ids:
  1935. logger.info(
  1936. "Chatto WS: no longer a member of %d room(s): %s",
  1937. len(stale_room_ids),
  1938. stale_room_ids,
  1939. )
  1940. self._warn_if_home_channel_unjoined(member_ids)
  1941. if new_room_ids:
  1942. logger.info(
  1943. "Chatto WS: discovered %d new room(s): %s",
  1944. len(new_room_ids),
  1945. new_room_ids,
  1946. )
  1947. for rid in new_room_ids:
  1948. # Membership came straight from the directory scan
  1949. # (viewer_state.is_member); natively invited rooms need no
  1950. # JoinRoom call — same rule as _run_join.
  1951. if self._answers_in_room(rid):
  1952. await self._seed_room(rid)
  1953. else:
  1954. logger.info(
  1955. "Chatto WS: %s (%s) is on neither mention list -"
  1956. " joined read-only",
  1957. self._room_names.get(rid, rid),
  1958. rid,
  1959. )
  1960. self._joined_room_ids.append(rid)
  1961. self._log_joined_rooms()
  1962. except Exception:
  1963. logger.warning("Chatto WS: _refresh_rooms failed", exc_info=True)
  1964. # ------------------------------------------------------------------ #
  1965. # Read state (best-effort, Chatto-unique)
  1966. # ------------------------------------------------------------------ #
  1967. # Deliberately outside the try block above: this sweep runs on every
  1968. # refresh — including refreshes that discovered no new rooms. The
  1969. # old early return starved it to "only when something changed",
  1970. # leaving silent rooms unmarked for whole connection lifetimes.
  1971. # Best-effort: mark all joined rooms as read (room_id may be undefined here)
  1972. for _rid in list(self._joined_room_ids):
  1973. try:
  1974. await client.mark_room_as_read(room_id=_rid)
  1975. except Exception:
  1976. logger.debug(
  1977. "Chatto: mark_room_as_read failed for %s", _rid, exc_info=True
  1978. )
  1979. # ------------------------------------------------------------------ #
  1980. # Sending (ConnectRPC — unchanged from polling version)
  1981. # ------------------------------------------------------------------ #
  1982. def _resolve_outbound_thread(
  1983. self,
  1984. chat_id: str,
  1985. reply_to: str | None,
  1986. metadata: dict[str, Any] | None,
  1987. ) -> str | None:
  1988. """The thread an outbound message belongs in, or ``None`` for the room.
  1989. One definition for every outbound path (text and attachments):
  1990. ``metadata["thread_id"]`` wins — it names the thread root to stay in;
  1991. otherwise ``reply_to`` anchors a thread under the incoming message,
  1992. but only with ``auto_thread`` enabled. DMs never thread.
  1993. Deliberately does NOT open an auto-thread of its own — only the
  1994. chunked text path in :meth:`send` does that, because only there do
  1995. further chunks follow into the freshly created thread.
  1996. """
  1997. thread_id = (metadata or {}).get("thread_id")
  1998. if reply_to and self.chatto_config.auto_thread.value and not thread_id:
  1999. thread_id = reply_to
  2000. if self._room_kinds.get(chat_id) == RoomKind.DM:
  2001. return None
  2002. return str(thread_id) if thread_id else None
  2003. async def send(
  2004. self,
  2005. chat_id: str,
  2006. content: str,
  2007. reply_to: str | None = None,
  2008. metadata: dict[str, Any] | None = None,
  2009. ) -> SendResult:
  2010. """Send a message to a Chatto room.
  2011. Long messages are split into chunks via ``truncate_message`` and
  2012. each chunk is sent as a separate CreateMessage call. The first
  2013. chunk's message ID is returned as ``message_id``.
  2014. When ``auto_thread`` is enabled and the incoming message was a
  2015. regular room message (not already in a thread), the first chunk is
  2016. sent as a room message and its ID becomes the thread root. Subsequent
  2017. chunks are sent in that thread. This mirrors Discord's auto_thread
  2018. behavior.
  2019. BasePlatformAdapter override
  2020. """
  2021. if not content:
  2022. return SendResult(success=False, error="Empty message")
  2023. formatted = _normalise_outbound_text(content)
  2024. chunks = self.truncate_message(formatted, ChattoConstants.SPLIT_THRESHOLD)
  2025. thread_id = self._resolve_outbound_thread(chat_id, reply_to, metadata)
  2026. room_kind = self._room_kinds.get(chat_id)
  2027. is_dm = room_kind == RoomKind.DM
  2028. # Auto-thread: by default, Chatto creates a thread for replies to room
  2029. # messages (not DMs, not already in a thread). This keeps conversations
  2030. # organized in the room. Can be disabled via extra.auto_thread=false.
  2031. use_auto_thread = (
  2032. self.chatto_config.auto_thread.value and not thread_id and not is_dm
  2033. )
  2034. message_ids: list[str] = []
  2035. last_error: str | None = None
  2036. retryable = False
  2037. client = await self._get_chatto_client()
  2038. if client is None:
  2039. return SendResult(
  2040. success=False, error="Chatto client not available", retryable=True
  2041. )
  2042. for i, chunk in enumerate(chunks):
  2043. try:
  2044. msg_obj = await client.post_message(
  2045. room_id=chat_id,
  2046. body=chunk,
  2047. thread_root_event_id=str(thread_id) if thread_id else "",
  2048. )
  2049. except Exception as e:
  2050. # ChattoError included: both read as "this chunk did not go
  2051. # out" and stop the batch — the SendResult carries the reason.
  2052. # Only server-side failures count as retryable; a bug in our
  2053. # own code must not read as a transient network blip.
  2054. last_error = str(e)
  2055. retryable = isinstance(e, ChattoError)
  2056. break
  2057. self._mark_seen(msg_obj.id)
  2058. message_ids.append(msg_obj.id)
  2059. # Auto-thread: first chunk becomes the thread root,
  2060. # subsequent chunks go in the thread
  2061. if use_auto_thread and i == 0 and not thread_id:
  2062. thread_id = msg_obj.id
  2063. # Nothing got through at all — report the failure instead of a phantom success.
  2064. if not message_ids:
  2065. return SendResult(
  2066. success=False,
  2067. error=last_error or "Chatto: message could not be sent",
  2068. retryable=retryable,
  2069. )
  2070. first_id = message_ids[0]
  2071. # ------------------------------------------------------------------ #
  2072. # Thread following (best-effort, Chatto-unique)
  2073. # ------------------------------------------------------------------ #
  2074. if thread_id:
  2075. try:
  2076. await client.follow_thread(chat_id, thread_id)
  2077. except Exception:
  2078. logger.debug(
  2079. "Chatto: follow_thread failed for %s/%s",
  2080. chat_id,
  2081. thread_id,
  2082. exc_info=True,
  2083. )
  2084. # A later chunk failed after earlier ones went out: partial delivery.
  2085. if last_error:
  2086. logger.warning(
  2087. "Chatto: sent %d/%d chunk(s) to %s before failing: %s",
  2088. len(message_ids),
  2089. len(chunks),
  2090. chat_id,
  2091. last_error,
  2092. )
  2093. # raw_response stays unset (dict-shaped per the SendResult contract):
  2094. # gateway consumers such as the cron scheduler call .get() on it, so a
  2095. # chattolib Message here would crash delivery bookkeeping *after* the
  2096. # send already succeeded — the job then falls back to the standalone
  2097. # path and the room sees the message twice.
  2098. return SendResult(success=True, message_id=first_id)
  2099. def format_message(self, content: str) -> str:
  2100. """Normalise outgoing text for Chatto.
  2101. The transformations live in :func:`_normalise_outbound_text`, shared
  2102. with the standalone cron sender.
  2103. BasePlatformAdapter override
  2104. """
  2105. return _normalise_outbound_text(content)
  2106. async def edit_message(
  2107. self,
  2108. chat_id: str,
  2109. message_id: str,
  2110. content: str,
  2111. *,
  2112. finalize: bool = False,
  2113. ) -> SendResult:
  2114. """Edit a message we previously sent, via MessageService/UpdateMessage.
  2115. The stream consumer drives streaming replies through this: without the
  2116. override the base class reports "Not supported" and every incremental
  2117. update arrives as a *new* message.
  2118. ``finalize`` is a no-op for Chatto — an edit is an edit here, there is
  2119. no in-progress card state to close out (hence no
  2120. ``REQUIRES_EDIT_FINALIZE``).
  2121. Content that exceeds the per-message limit is refused rather than
  2122. silently truncated, so the caller falls back to ``send()``, which
  2123. splits across messages.
  2124. BasePlatformAdapter override
  2125. """
  2126. if not message_id:
  2127. return SendResult(success=False, error="Chatto: no message id to edit")
  2128. if not content:
  2129. return SendResult(success=False, error="Empty message")
  2130. formatted = self.format_message(content)
  2131. if len(formatted) > ChattoConstants.MAX_MESSAGE_LENGTH:
  2132. # Refuse instead of truncating: the caller's fallback path splits.
  2133. return SendResult(
  2134. success=False,
  2135. error=(
  2136. f"Chatto: edit exceeds {ChattoConstants.MAX_MESSAGE_LENGTH} "
  2137. f"chars ({len(formatted)})"
  2138. ),
  2139. )
  2140. client = await self._get_chatto_client()
  2141. if client is None:
  2142. return SendResult(
  2143. success=False, error="Chatto client not available", retryable=True
  2144. )
  2145. try:
  2146. msg = await client.update_message(
  2147. room_id=str(chat_id),
  2148. event_id=str(message_id),
  2149. body=formatted,
  2150. )
  2151. except ChattoError as e:
  2152. logger.warning("Chatto: UpdateMessage failed for %s: %s", message_id, e)
  2153. return SendResult(success=False, error=str(e), retryable=True)
  2154. except Exception as e:
  2155. logger.warning("Chatto: UpdateMessage error for %s: %s", message_id, e)
  2156. return SendResult(success=False, error=str(e), retryable=False)
  2157. # Our own edit comes back as a message_edited event; mark it seen so it
  2158. # is never mistaken for inbound traffic.
  2159. edited_id = msg.id or str(message_id)
  2160. self._mark_seen(edited_id)
  2161. return SendResult(success=True, message_id=edited_id)
  2162. async def delete_message(self, chat_id: str, message_id: str) -> bool:
  2163. """Delete a message via MessageService/DeleteMessage.
  2164. Used by the stream consumer's fresh-final cleanup (removing a preview
  2165. message once the completed reply has been sent) and by the ephemeral
  2166. reply TTL.
  2167. BasePlatformAdapter override
  2168. """
  2169. if not chat_id or not message_id:
  2170. return False
  2171. client = await self._get_chatto_client()
  2172. if client is None:
  2173. logger.warning("Chatto: DeleteMessage — client unavailable")
  2174. return False
  2175. try:
  2176. return bool(
  2177. await client.delete_message(
  2178. room_id=str(chat_id),
  2179. event_id=str(message_id),
  2180. )
  2181. )
  2182. except ChattoError as e:
  2183. logger.warning("Chatto: DeleteMessage failed for %s: %s", message_id, e)
  2184. return False
  2185. except Exception as e:
  2186. logger.warning("Chatto: DeleteMessage error for %s: %s", message_id, e)
  2187. return False
  2188. async def create_handoff_thread(
  2189. self,
  2190. parent_chat_id: str,
  2191. name: str,
  2192. ) -> str | None:
  2193. """Anchor a session handoff in a fresh thread under *parent_chat_id*.
  2194. Chatto threads hang off a message, not off the room, so we post a seed
  2195. message and hand its ID back as the thread root — the same shape the
  2196. Slack adapter uses. DMs don't support threads, so they get ``None``
  2197. and the watcher keeps delivering into the DM itself.
  2198. BasePlatformAdapter override
  2199. """
  2200. if not parent_chat_id:
  2201. return None
  2202. if self._room_kinds.get(parent_chat_id) == RoomKind.DM:
  2203. logger.debug("Chatto: handoff thread skipped — %s is a DM", parent_chat_id)
  2204. return None
  2205. client = await self._get_chatto_client()
  2206. if client is None:
  2207. logger.warning("Chatto: handoff thread — client unavailable")
  2208. return None
  2209. try:
  2210. msg = await client.post_message(
  2211. room_id=str(parent_chat_id),
  2212. body=f"🧵 Hermes handoff — **{(name or 'session').strip()[:80]}**",
  2213. )
  2214. except Exception as e:
  2215. logger.warning(
  2216. "Chatto: handoff thread seed-post failed for room %s: %s",
  2217. parent_chat_id,
  2218. e,
  2219. )
  2220. return None
  2221. seed_id = msg.id
  2222. if not seed_id:
  2223. logger.warning("Chatto: handoff thread seed-post returned no message id")
  2224. return None
  2225. self._mark_seen(seed_id)
  2226. try:
  2227. await client.follow_thread(str(parent_chat_id), seed_id)
  2228. except Exception:
  2229. logger.debug(
  2230. "Chatto: follow_thread failed for handoff %s/%s",
  2231. parent_chat_id,
  2232. seed_id,
  2233. exc_info=True,
  2234. )
  2235. return seed_id
  2236. # Overridden from BaseAdapter:
  2237. async def send_typing(self, chat_id: str, metadata=None) -> None:
  2238. """Start a persistent typing indicator for a room.
  2239. Sends a typing ping every 10 seconds (Chatto's indicator likely
  2240. lasts ~8-10s). The background loop runs until ``stop_typing()``
  2241. is called or the task is cancelled.
  2242. BasePlatformAdapter override
  2243. """
  2244. if chat_id in self._typing_tasks:
  2245. return # already running
  2246. async def _typing_loop() -> None:
  2247. try:
  2248. while True:
  2249. try:
  2250. client = await self._get_chatto_client()
  2251. if client is None:
  2252. return
  2253. await client.refresh_typing_indicator(room_id=str(chat_id))
  2254. except asyncio.CancelledError:
  2255. return
  2256. except Exception:
  2257. logger.debug(
  2258. "Chatto: typing indicator refresh failed for %s",
  2259. chat_id,
  2260. exc_info=True,
  2261. )
  2262. await asyncio.sleep(10)
  2263. except asyncio.CancelledError:
  2264. pass
  2265. finally:
  2266. self._typing_tasks.pop(chat_id, None)
  2267. self._typing_tasks[chat_id] = asyncio.create_task(_typing_loop())
  2268. async def stop_typing(self, chat_id: str) -> None:
  2269. """Stop the persistent typing indicator for a room.
  2270. BasePlatformAdapter override
  2271. """
  2272. task = self._typing_tasks.pop(chat_id, None)
  2273. if task:
  2274. task.cancel()
  2275. try:
  2276. await task
  2277. except (asyncio.CancelledError, Exception):
  2278. logger.debug("Chatto: typing task ended for %s", chat_id, exc_info=True)
  2279. async def get_chat_info(self, chat_id: str) -> dict[str, Any]:
  2280. """Get information about a chat/room.
  2281. BasePlatformAdapter override
  2282. """
  2283. name = self._room_names.get(chat_id, chat_id)
  2284. kind = self._room_kinds.get(chat_id)
  2285. return {
  2286. "name": name,
  2287. "type": chat_type_for_room_kind(kind).value,
  2288. }
  2289. # ------------------------------------------------------------------ #
  2290. # Reactions
  2291. # ------------------------------------------------------------------ #
  2292. @staticmethod
  2293. def _emoji_to_shortcode(emoji: str) -> str:
  2294. """Convert a unicode emoji to a Chatto shortcode name.
  2295. If the emoji is already a shortcode (no unicode mapping found),
  2296. return it as-is.
  2297. """
  2298. shortcode = ChattoConstants.EMOJI_TO_SHORTCODE.get(emoji)
  2299. if shortcode:
  2300. return shortcode
  2301. # Already a shortcode like "thumbsup" — return as-is
  2302. return emoji
  2303. async def add_reaction(self, room_id: str, message_id: str, emoji: str) -> bool:
  2304. """Add a reaction to a message via MessageService/AddReaction.
  2305. BasePlatformAdapter override
  2306. """
  2307. shortcode = self._emoji_to_shortcode(emoji)
  2308. client = await self._get_chatto_client()
  2309. if client is None:
  2310. logger.warning("Chatto: AddReaction — client unavailable")
  2311. return False
  2312. try:
  2313. result = await client.add_reaction(
  2314. room_id=room_id,
  2315. message_event_id=message_id,
  2316. emoji=shortcode,
  2317. )
  2318. return result
  2319. except ChattoError as e:
  2320. logger.warning("Chatto: AddReaction failed: %s", e)
  2321. return False
  2322. except Exception as e:
  2323. logger.warning("Chatto: AddReaction error: %s", e)
  2324. return False
  2325. async def remove_reaction(self, chat_id: str, message_id: str, emoji: str) -> bool:
  2326. """Remove a reaction from a message via MessageService/RemoveReaction.
  2327. BasePlatformAdapter override
  2328. """
  2329. shortcode = self._emoji_to_shortcode(emoji)
  2330. client = await self._get_chatto_client()
  2331. if client is None:
  2332. logger.warning("Chatto: RemoveReaction — client unavailable")
  2333. return False
  2334. try:
  2335. result = await client.remove_reaction(
  2336. room_id=str(chat_id),
  2337. message_event_id=str(message_id),
  2338. emoji=shortcode,
  2339. )
  2340. return result
  2341. except ChattoError as e:
  2342. logger.warning("Chatto: RemoveReaction failed: %s", e)
  2343. return False
  2344. except Exception as e:
  2345. logger.warning("Chatto: RemoveReaction error: %s", e)
  2346. return False
  2347. # ------------------------------------------------------------------ #
  2348. # DM initiation (Chatto-unique)
  2349. # ------------------------------------------------------------------ #
  2350. async def start_dm(self, user_id: str) -> str | None:
  2351. """Start a direct message with a user via RoomService/StartDM.
  2352. Returns the room ID on success, or None on failure.
  2353. BasePlatformAdapter override
  2354. """
  2355. if not user_id:
  2356. return None
  2357. client = await self._get_chatto_client()
  2358. if client is None:
  2359. return None
  2360. try:
  2361. room = await client.start_dm(participant_ids=[str(user_id)])
  2362. self._room_names[room.id] = room.name
  2363. self._room_kinds[room.id] = room.kind
  2364. return room.id
  2365. except ChattoError as e:
  2366. logger.debug("Chatto: StartDM failed: %s", e)
  2367. return None
  2368. except Exception as e:
  2369. logger.debug("Chatto: StartDM error: %s", e)
  2370. return None
  2371. # ------------------------------------------------------------------ #
  2372. # Room creation (Chatto-unique)
  2373. # ------------------------------------------------------------------ #
  2374. async def create_room(
  2375. self,
  2376. name: str,
  2377. description: str = "",
  2378. group_id: str = "",
  2379. universal: bool = True,
  2380. ) -> str | None:
  2381. """Create an ad-hoc room via RoomService/CreateRoom.
  2382. Returns the room ID on success, or None on failure.
  2383. BasePlatformAdapter override
  2384. """
  2385. client = await self._get_chatto_client()
  2386. if client is None:
  2387. return None
  2388. try:
  2389. room = await client.create_room(
  2390. name=name,
  2391. group_id=group_id or "",
  2392. description=description,
  2393. universal=universal,
  2394. )
  2395. rid = str(room.id) if room else ""
  2396. if rid:
  2397. self._room_names[rid] = room.name
  2398. self._room_kinds[rid] = room.kind
  2399. return rid
  2400. logger.debug("Chatto: CreateRoom returned no room id")
  2401. return None
  2402. except ChattoError as e:
  2403. logger.debug("Chatto: CreateRoom failed: %s", e)
  2404. return None
  2405. except Exception as e:
  2406. logger.debug("Chatto: CreateRoom error: %s", e)
  2407. return None
  2408. # ------------------------------------------------------------------ #
  2409. # Processing lifecycle hooks (reactions-based, like Discord)
  2410. # ------------------------------------------------------------------ #
  2411. def _event_room_and_message_id(self, event: MessageEvent) -> tuple[str, str]:
  2412. """Extract room_id and message_id from a MessageEvent."""
  2413. message_id = event.message_id or ""
  2414. return event.source.chat_id, message_id
  2415. async def on_processing_start(self, event: MessageEvent) -> None:
  2416. """Record the turn as open, then add an 👀 (eyes) reaction.
  2417. The record is what lets an edit of this very message be recognized as
  2418. a mid-run correction. It must be maintained even when reactions are
  2419. disabled — tracking and decorating are independent concerns.
  2420. BasePlatformAdapter override
  2421. """
  2422. session_key = (
  2423. self._session_key_for(event.source) if event.source is not None else ""
  2424. )
  2425. if session_key and event.message_id:
  2426. self._processing[session_key] = str(event.message_id)
  2427. if not self.chatto_config.reactions.value:
  2428. return
  2429. chat_id, message_id = self._event_room_and_message_id(event)
  2430. if not chat_id or not message_id:
  2431. # Routine, not a fault: the gateway runs agent-initiated turns
  2432. # (heartbeat polls, goal continuations) through the same pipeline
  2433. # with message_id=None, and there is no inbound message to mark.
  2434. logger.debug(
  2435. "Chatto: nothing to react to (chat_id=%r, message_id=%r)",
  2436. chat_id,
  2437. message_id,
  2438. )
  2439. return
  2440. await self.add_reaction(chat_id, message_id, "👀")
  2441. async def on_processing_complete(
  2442. self,
  2443. event: MessageEvent,
  2444. outcome: ProcessingOutcome,
  2445. ) -> None:
  2446. """Close the turn's record, then swap 👀 for ✅/❌/🚫.
  2447. Fires for every outcome, including CANCELLED (mid-run edit
  2448. correction) — this is what re-arms `_processing` before the corrected
  2449. turn is dispatched.
  2450. BasePlatformAdapter override
  2451. """
  2452. session_key = (
  2453. self._session_key_for(event.source) if event.source is not None else ""
  2454. )
  2455. if session_key and event.message_id:
  2456. self._processing.pop(session_key, None)
  2457. if not self.chatto_config.reactions.value:
  2458. return
  2459. chat_id, message_id = self._event_room_and_message_id(event)
  2460. if not chat_id or not message_id:
  2461. return
  2462. # Remove the processing eyes reaction
  2463. await self.remove_reaction(chat_id, message_id, "👀")
  2464. # Add the outcome reaction
  2465. if outcome == ProcessingOutcome.SUCCESS:
  2466. await self.add_reaction(chat_id, message_id, "✅")
  2467. elif outcome == ProcessingOutcome.FAILURE:
  2468. await self.add_reaction(chat_id, message_id, "❌")
  2469. elif outcome == ProcessingOutcome.CANCELLED:
  2470. await self.add_reaction(chat_id, message_id, "🚫")
  2471. # ------------------------------------------------------------------ #
  2472. # Asset upload (chunked)
  2473. # ------------------------------------------------------------------ #
  2474. async def _upload_asset(self, room_id: str, file_path: str) -> str | None:
  2475. """Upload a file via the chunked AssetUploadService.
  2476. Returns the asset ID on success, or None on failure.
  2477. """
  2478. try:
  2479. file_data = await asyncio.to_thread(_read_file_bytes, file_path)
  2480. except Exception as e:
  2481. logger.error("Chatto: failed to read file %s — %s", file_path, e)
  2482. return None
  2483. if not file_data:
  2484. logger.error("Chatto: file %s is empty", file_path)
  2485. return None
  2486. file_size = len(file_data)
  2487. file_name = os.path.basename(file_path)
  2488. mime_type = mimetypes.guess_type(file_path)[0] or "application/octet-stream"
  2489. sha256_hash = hashlib.sha256(file_data).hexdigest()
  2490. client = await self._get_chatto_client()
  2491. if client is None:
  2492. logger.error("Chatto: upload aborted - no client available")
  2493. return None
  2494. try:
  2495. # Step 1: Create upload session
  2496. upload = await client.create_upload(
  2497. room_id=room_id,
  2498. filename=file_name,
  2499. size=file_size,
  2500. sha256=sha256_hash,
  2501. content_type=mime_type,
  2502. )
  2503. # AssetUpload names this upload_id, not id — reading it through an
  2504. # untyped getattr default is what let the mismatch reach production.
  2505. upload_id = upload.upload_id
  2506. if not upload_id:
  2507. logger.error("Chatto: CreateUpload returned no upload ID")
  2508. return None
  2509. # Step 2: Upload chunks
  2510. offset = 0
  2511. while offset < file_size:
  2512. chunk = file_data[offset : offset + ChattoConstants.UPLOAD_CHUNK_SIZE]
  2513. chunk_sha256 = hashlib.sha256(chunk).hexdigest()
  2514. await client.upload_chunk(
  2515. upload_id=upload_id,
  2516. offset=offset,
  2517. content=chunk,
  2518. chunk_sha256=chunk_sha256,
  2519. )
  2520. offset += len(chunk)
  2521. # Step 3: Complete upload
  2522. upload, asset = await client.complete_upload(upload_id=upload_id)
  2523. if not asset:
  2524. logger.error("Chatto: CompleteUpload returned no asset")
  2525. return None
  2526. logger.info(
  2527. "Chatto: uploaded %s as asset %s (%d bytes)",
  2528. file_name,
  2529. asset.id,
  2530. file_size,
  2531. )
  2532. return asset.id
  2533. except ChattoError as e:
  2534. logger.error("Chatto: upload failed: %s", e)
  2535. return None
  2536. except Exception as e:
  2537. logger.error("Chatto: upload error: %s", e)
  2538. return None
  2539. async def _post_attachment_message(
  2540. self,
  2541. chat_id: str,
  2542. asset_ids: list[str],
  2543. caption: str | None,
  2544. reply_to: str | None,
  2545. metadata: dict[str, Any] | None,
  2546. ) -> SendResult:
  2547. """Post one message carrying already-uploaded assets.
  2548. Threading follows the same rules as a text send
  2549. (:meth:`_resolve_outbound_thread`), so with ``auto_thread`` disabled an
  2550. attachment reply lands in the room like its text counterpart instead of
  2551. quietly opening a thread.
  2552. """
  2553. thread_id = self._resolve_outbound_thread(chat_id, reply_to, metadata)
  2554. client = await self._get_chatto_client()
  2555. if client is None:
  2556. return SendResult(
  2557. success=False, error="Chatto client not available", retryable=True
  2558. )
  2559. try:
  2560. msg = await client.post_message(
  2561. room_id=str(chat_id),
  2562. body=self.format_message(caption) if caption else "",
  2563. attachment_asset_ids=asset_ids,
  2564. thread_root_event_id=thread_id or "",
  2565. )
  2566. self._mark_seen(msg.id)
  2567. return SendResult(success=True, message_id=msg.id)
  2568. except ChattoError as e:
  2569. # Same classification as the chunked text path: server-side
  2570. # failures retry, anything else is ours and must not loop.
  2571. return SendResult(success=False, error=str(e), retryable=True)
  2572. except Exception as e:
  2573. return SendResult(success=False, error=str(e), retryable=False)
  2574. async def _send_local_file_as_attachment(
  2575. self,
  2576. chat_id: str,
  2577. file_path: str,
  2578. caption: str | None,
  2579. reply_to: str | None,
  2580. metadata: dict[str, Any] | None,
  2581. *,
  2582. kind: str,
  2583. ) -> SendResult:
  2584. """Upload a local file and post it as a native Chatto attachment.
  2585. Shared by ``send_image_file``/``send_document``/``send_video``/
  2586. ``send_voice`` — the upload mechanics are identical, only the wording of
  2587. the failure notice differs. On failure we send that notice as text and
  2588. never the host path (it leaks the Hermes home layout).
  2589. """
  2590. notice = f"⚠️ Couldn't deliver the {kind} attachment."
  2591. safe_path = self.validate_media_delivery_path(file_path)
  2592. if not safe_path:
  2593. logger.warning(
  2594. "[%s] send %s: unsafe path %s",
  2595. self.name,
  2596. kind,
  2597. file_path,
  2598. )
  2599. text = f"{caption}\n{notice}" if caption else notice
  2600. return await self.send(chat_id, text, reply_to=reply_to, metadata=metadata)
  2601. asset_id = await self._upload_asset(str(chat_id), safe_path)
  2602. if not asset_id:
  2603. logger.warning(
  2604. "[%s] send %s: upload failed for %s",
  2605. self.name,
  2606. kind,
  2607. safe_path,
  2608. )
  2609. text = f"{caption}\n{notice}" if caption else notice
  2610. return await self.send(chat_id, text, reply_to=reply_to, metadata=metadata)
  2611. return await self._post_attachment_message(
  2612. chat_id,
  2613. [asset_id],
  2614. caption,
  2615. reply_to,
  2616. metadata,
  2617. )
  2618. async def send_image_file(
  2619. self,
  2620. chat_id: str,
  2621. image_path: str,
  2622. caption: str | None = None,
  2623. reply_to: str | None = None,
  2624. metadata: dict[str, Any] | None = None,
  2625. **kwargs,
  2626. ) -> SendResult:
  2627. """Send a local image file via the chunked upload API.
  2628. The parameter is ``image_path``, not ``file_path``: every caller passes
  2629. it by keyword (``gateway/run.py:22354``, ``:22470``, and the base class's
  2630. own ``send_multiple_images`` file:// branch), so a renamed parameter
  2631. makes each of those raise TypeError and silently degrade to a text
  2632. notice.
  2633. BasePlatformAdapter override
  2634. """
  2635. return await self._send_local_file_as_attachment(
  2636. chat_id,
  2637. image_path,
  2638. caption,
  2639. reply_to,
  2640. metadata,
  2641. kind="image",
  2642. )
  2643. async def send_document(
  2644. self,
  2645. chat_id: str,
  2646. file_path: str,
  2647. caption: str | None = None,
  2648. file_name: str | None = None,
  2649. reply_to: str | None = None,
  2650. metadata: dict[str, Any] | None = None,
  2651. **kwargs,
  2652. ) -> SendResult:
  2653. """Send a local file as a native Chatto attachment.
  2654. ``file_name`` exists in the base-class signature and is accepted for
  2655. compatibility, but Chatto takes the recipient-visible filename from
  2656. the upload session (derived from the local path); failures are logged
  2657. and noticed by ``_send_local_file_as_attachment`` itself.
  2658. BasePlatformAdapter override
  2659. """
  2660. return await self._send_local_file_as_attachment(
  2661. chat_id,
  2662. file_path,
  2663. caption,
  2664. reply_to,
  2665. metadata,
  2666. kind="file",
  2667. )
  2668. async def send_video(
  2669. self,
  2670. chat_id: str,
  2671. video_path: str,
  2672. caption: str | None = None,
  2673. reply_to: str | None = None,
  2674. metadata: dict[str, Any] | None = None,
  2675. **kwargs,
  2676. ) -> SendResult:
  2677. """Send a local video as a native Chatto attachment.
  2678. Chatto transcodes and plays it inline.
  2679. BasePlatformAdapter override
  2680. """
  2681. return await self._send_local_file_as_attachment(
  2682. chat_id,
  2683. video_path,
  2684. caption,
  2685. reply_to,
  2686. metadata,
  2687. kind="video",
  2688. )
  2689. async def send_voice(
  2690. self,
  2691. chat_id: str,
  2692. audio_path: str,
  2693. caption: str | None = None,
  2694. reply_to: str | None = None,
  2695. metadata: dict[str, Any] | None = None,
  2696. **kwargs,
  2697. ) -> SendResult:
  2698. """Send a local audio file as a native Chatto attachment.
  2699. Chatto has no dedicated voice-bubble type, so this is an ordinary audio
  2700. attachment — still far better than the base class's text notice.
  2701. BasePlatformAdapter override
  2702. """
  2703. return await self._send_local_file_as_attachment(
  2704. chat_id,
  2705. audio_path,
  2706. caption,
  2707. reply_to,
  2708. metadata,
  2709. kind="audio",
  2710. )
  2711. async def send_image(
  2712. self,
  2713. chat_id: str,
  2714. image_url: str,
  2715. caption: str | None = None,
  2716. reply_to: str | None = None,
  2717. metadata: dict[str, Any] | None = None,
  2718. ) -> SendResult:
  2719. """Send an image to a Chatto room.
  2720. Materialises the URL (size-capped download, like every inbound
  2721. attachment) and uploads it as a native attachment. Falls back to the
  2722. plain URL as a link — Chatto renders link previews — when the URL
  2723. cannot be materialised or the post after a successful upload fails.
  2724. An upload failure needs no fallback on top: the text notice of
  2725. ``_send_local_file_as_attachment`` has already gone out.
  2726. BasePlatformAdapter override
  2727. """
  2728. link_text = f"{caption}\n{image_url}" if caption else image_url
  2729. path, is_temp = await self._materialise_image(image_url)
  2730. if path is not None:
  2731. try:
  2732. result = await self._send_local_file_as_attachment(
  2733. chat_id, path, caption, reply_to, metadata, kind="image"
  2734. )
  2735. finally:
  2736. if is_temp:
  2737. try:
  2738. os.unlink(path)
  2739. except OSError:
  2740. pass
  2741. if result.success:
  2742. return result
  2743. return await self.send(chat_id, link_text, reply_to=reply_to, metadata=metadata)
  2744. async def _materialise_image(self, image_url: str) -> tuple[str | None, bool]:
  2745. """Resolve one ``send_multiple_images`` entry to a local file path.
  2746. Accepts ``http(s)://`` URLs (downloaded to a temp file), ``file://``
  2747. URIs and bare paths. Returns ``(path, is_temp)`` — the caller unlinks
  2748. when ``is_temp``. ``(None, False)`` means the entry is unusable.
  2749. """
  2750. if image_url.startswith(("http://", "https://")):
  2751. ext = os.path.splitext(urlsplit(image_url).path)[1] or ".png"
  2752. tmp_fd, tmp_path = tempfile.mkstemp(suffix=ext, prefix="chatto_img_")
  2753. os.close(tmp_fd)
  2754. try:
  2755. data = await self._download_attachment_bytes(image_url)
  2756. await asyncio.to_thread(_write_file_bytes, tmp_path, data)
  2757. except Exception as e:
  2758. logger.warning("Chatto: image download failed for %s: %s", image_url, e)
  2759. try:
  2760. os.unlink(tmp_path)
  2761. except OSError:
  2762. pass
  2763. return None, False
  2764. return tmp_path, True
  2765. local = image_url
  2766. if local.startswith("file://"):
  2767. local = unquote(urlsplit(local).path)
  2768. return self.validate_media_delivery_path(local), False
  2769. async def send_multiple_images(
  2770. self,
  2771. chat_id: str,
  2772. images: list[tuple[str, str]],
  2773. metadata: dict[str, Any] | None = None,
  2774. human_delay: float = 0.0,
  2775. ) -> SendResult:
  2776. """Send a batch of images as ONE message with several attachments.
  2777. The base implementation posts each image separately; a Chatto message
  2778. carries a list of attachment assets, so a batch belongs in a single
  2779. message (and a single notification).
  2780. ``human_delay`` is ignored deliberately — there is only one outbound
  2781. call to pace. Entries that can't be fetched are dropped with a warning;
  2782. if nothing survives, we fall back to the base class so the user still
  2783. gets the links.
  2784. BasePlatformAdapter override
  2785. """
  2786. if len(images or []) < 2:
  2787. return await super().send_multiple_images(
  2788. chat_id,
  2789. images,
  2790. metadata=metadata,
  2791. human_delay=human_delay,
  2792. )
  2793. asset_ids: list[str] = []
  2794. captions: list[str] = []
  2795. for image_url, alt_text in images:
  2796. path, is_temp = await self._materialise_image(image_url)
  2797. if not path:
  2798. logger.warning("Chatto: skipping unusable image %s", image_url)
  2799. continue
  2800. try:
  2801. asset_id = await self._upload_asset(str(chat_id), path)
  2802. finally:
  2803. if is_temp:
  2804. try:
  2805. os.unlink(path)
  2806. except OSError:
  2807. pass
  2808. if not asset_id:
  2809. logger.warning("Chatto: upload failed for image %s", image_url)
  2810. continue
  2811. asset_ids.append(asset_id)
  2812. if alt_text:
  2813. captions.append(alt_text)
  2814. if not asset_ids:
  2815. logger.warning(
  2816. "Chatto: no image survived upload, falling back to per-image delivery",
  2817. )
  2818. return await super().send_multiple_images(
  2819. chat_id,
  2820. images,
  2821. metadata=metadata,
  2822. human_delay=human_delay,
  2823. )
  2824. if len(asset_ids) < len(images):
  2825. logger.warning(
  2826. "Chatto: sending %d of %d images — the rest could not be uploaded",
  2827. len(asset_ids),
  2828. len(images),
  2829. )
  2830. return await self._post_attachment_message(
  2831. chat_id,
  2832. asset_ids,
  2833. "\n".join(captions) or None,
  2834. None,
  2835. metadata,
  2836. )
  2837. # ---------------------------------------------------------------------------
  2838. # Cron / out-of-process delivery
  2839. # ---------------------------------------------------------------------------
  2840. async def hermes_standalone_sender_fn(
  2841. pconfig: PlatformConfig,
  2842. chat_id: str,
  2843. message: str,
  2844. *,
  2845. thread_id=None,
  2846. media_files=None,
  2847. force_document=False,
  2848. ) -> SendResult:
  2849. """Deliver a message to Chatto without a running gateway adapter. Do not modify signature.
  2850. Used by cron / scheduled routines that run out-of-process. Creates a
  2851. short-lived chattolib client, posts, and closes. Long messages are
  2852. normalised and split like ``send()`` does, so cron output cannot die on
  2853. the server's per-message limit.
  2854. """
  2855. chatto_config: ChattoConfiguration = ChattoConfiguration(pconfig=pconfig)
  2856. # Create a temporary client for standalone sending — needs a base URL and token.
  2857. has_token = bool(chatto_config.token.value)
  2858. if not chatto_config.base_url.value or not has_token:
  2859. return SendResult(success=False, error="Chatto: base URL or token missing")
  2860. client = ChattoClient(
  2861. base_url=chatto_config.base_url.value, token=chatto_config.token.value
  2862. )
  2863. try:
  2864. kwargs: dict[str, Any] = {}
  2865. if chatto_config.auto_thread.value and thread_id:
  2866. kwargs["thread_root_event_id"] = thread_id
  2867. if media_files and media_files.get("attachment_asset_ids"):
  2868. kwargs["attachment_asset_ids"] = list(media_files["attachment_asset_ids"])
  2869. formatted = _normalise_outbound_text(message)
  2870. chunks = BasePlatformAdapter.truncate_message(
  2871. formatted, ChattoConstants.SPLIT_THRESHOLD
  2872. )
  2873. message_ids: list[str] = []
  2874. try:
  2875. for chunk in chunks:
  2876. posted = await client.post_message(chat_id, chunk, **kwargs)
  2877. message_ids.append(posted.id)
  2878. except Exception as exc:
  2879. if message_ids:
  2880. logger.warning(
  2881. "Chatto standalone: sent %d/%d chunk(s) to %s before failing: %s",
  2882. len(message_ids),
  2883. len(chunks),
  2884. chat_id,
  2885. exc,
  2886. )
  2887. # Same classification as the adapter's send paths: only
  2888. # server-side failures read as retryable.
  2889. return SendResult(
  2890. success=False,
  2891. error=str(exc),
  2892. retryable=isinstance(exc, ChattoError),
  2893. )
  2894. return SendResult(success=True, message_id=message_ids[0])
  2895. finally:
  2896. try:
  2897. await client.close()
  2898. except Exception as exc:
  2899. logger.warning(
  2900. "Chatto standalone: error closing short-lived client (perhaps already closed): %s",
  2901. exc,
  2902. )
  2903. # Resolved Chatto configuration — populated by ``hermes_validate_config`` so
  2904. # plugin tools can access it without re-reading env vars or duplicating
  2905. # ``ChattoConfiguration`` logic.
  2906. _resolved_chatto_config: "ChattoConfiguration | None" = None
  2907. # How similar an ``extra`` key must be to a declared config_key before the
  2908. # unknown-key check reads it as a likely typo or renamed field.
  2909. _NEAR_MISS_RATIO = 0.75
  2910. def _warn_on_unknown_extra_keys(config: PlatformConfig) -> None:
  2911. """Warn about ``extra`` keys that look like misspelled config fields.
  2912. A typo'd key silently resolves to the default otherwise — the warning is
  2913. the only thing telling the user their ``require_mention_channles`` never
  2914. reached us. Only near-matches against our declared config_keys are
  2915. flagged (difflib similarity): keys that resemble nothing of ours are
  2916. either placed there by the Hermes gateway itself (the shared-key loop in
  2917. load_gateway_config() bridges reply_in_thread & co. into every platform's
  2918. extra, hardcoded inline upstream — nothing to import) or are deliberate
  2919. pass-throughs, and flagging those would just train users to ignore us.
  2920. Internal markers (``_enabled_explicit``) are skipped outright.
  2921. """
  2922. extra: dict[str, Any] = getattr(config, "extra", None) or {}
  2923. known = [field.config_key for field in ChattoConfiguration.fields()]
  2924. for key in sorted(extra):
  2925. if key.startswith("_") or key in known:
  2926. continue
  2927. best = max(known, key=lambda k: SequenceMatcher(None, key, k).ratio())
  2928. if SequenceMatcher(None, key, best).ratio() >= _NEAR_MISS_RATIO:
  2929. logger.warning(
  2930. "Chatto: 'extra' key '%s' matches no config field — did you mean '%s'?",
  2931. key,
  2932. best,
  2933. )
  2934. def hermes_validate_config(config: PlatformConfig) -> bool:
  2935. """Check whether Chatto Plugin is configured.
  2936. Function name should be the same as register argument name with "hermes_" prefix, so we
  2937. know that it is needed for plugin register(). Do not change signature.
  2938. Takes ``config``. Compare to hermes_is_connected().
  2939. """
  2940. # Store resolved config for plugin tools — avoids re-reading env/config.
  2941. global _resolved_chatto_config
  2942. _resolved_chatto_config = ChattoConfiguration(pconfig=config)
  2943. _warn_on_unknown_extra_keys(config)
  2944. if (
  2945. len(_resolved_chatto_config.allowed_users.value) > 0
  2946. and _resolved_chatto_config.allow_all_users.value
  2947. ):
  2948. logger.error(
  2949. "Chatto: Conflicting configuration. Either use 'allowed_users' or 'allow_all_users' but not both."
  2950. )
  2951. return False
  2952. require_rooms = set(_resolved_chatto_config.require_mention_rooms.value)
  2953. optional_rooms = set(_resolved_chatto_config.optional_mention_rooms.value)
  2954. overlap = sorted(require_rooms & optional_rooms)
  2955. if overlap:
  2956. logger.error(
  2957. "Chatto: Conflicting configuration. Room(s) %s are on both "
  2958. "'require_mention_rooms' and 'optional_mention_rooms' — "
  2959. "each room must appear in at most one of them.",
  2960. ", ".join(overlap),
  2961. )
  2962. return False
  2963. # base_url always resolves (it defaults to ChattoHQ), so the only real
  2964. # question is whether a token came in.
  2965. if _resolved_chatto_config.token.value is not None:
  2966. return True
  2967. logger.error("Chatto: CHATTO_TOKEN must be set.")
  2968. return False
  2969. def hermes_check_fn() -> bool:
  2970. """Report whether the vendored chattolib dependency can be imported."""
  2971. try:
  2972. from chattolib import (
  2973. client, # noqa: F401 — vendored dependency probe
  2974. )
  2975. return True
  2976. except ImportError:
  2977. return False
  2978. # ---------------------------------------------------------------------------
  2979. # is_connected probe
  2980. # ---------------------------------------------------------------------------
  2981. def hermes_is_connected(config: PlatformConfig) -> bool:
  2982. """Report whether the Chatto platform is configured and enabled.
  2983. The name is fixed by the register() contract, but despite what it
  2984. suggests this does not open a connection — it validates configuration
  2985. only (see :func:`hermes_validate_config`); the gateway probes real
  2986. connectivity through ``connect()``.
  2987. """
  2988. return bool(hermes_validate_config(config) and config.enabled)
  2989. def hermes_setup_fn() -> None:
  2990. """Interactive setup wizard for Chatto. Is called by and only works in Hermes CLI context.
  2991. Function name should be the same as register argument name with "hermes_" prefix, so we
  2992. know that it is needed for plugin register().
  2993. """
  2994. from hermes_cli.setup import (
  2995. print_success,
  2996. prompt,
  2997. prompt_yes_no,
  2998. save_env_value,
  2999. )
  3000. url = prompt(
  3001. "Chatto server URL (e.g. https://chat.example.com) or leave blank for default ChattoHQ on chat.chatto.run:"
  3002. )
  3003. if url:
  3004. save_env_value(ChattoConfiguration.base_url.env_name, url)
  3005. token = prompt("Chatto bot token (cht_BK_...):", password=True)
  3006. if token:
  3007. save_env_value(ChattoConfiguration.token.env_name, token)
  3008. home = prompt("Home room ID for notifications (or empty):")
  3009. if home:
  3010. save_env_value(ChattoConfiguration.home_channel.env_name, home)
  3011. allow_all = prompt_yes_no("Allow all users to talk? (true/false):")
  3012. if allow_all:
  3013. save_env_value(ChattoConfiguration.allow_all_users.env_name, str(allow_all))
  3014. print_success("\n✓ Chatto configured. Restart the gateway to activate.")
  3015. def hermes_env_enablement_fn() -> dict:
  3016. """Seed PlatformConfig.extra from the CHATTO_* environment variables.
  3017. Returns a dict compatible with the PlatformConfig merge hook, holding
  3018. exactly the values that are actually set via env — never fabricated
  3019. defaults. Called by the platform registry during load_gateway_config(),
  3020. which commits this dict onto the platform's ``extra`` verbatim; seeding a
  3021. value that was not configured would overwrite what the user set in
  3022. config.yaml (e.g. a YAML base_url clobbered by the ChattoHQ default).
  3023. "Nothing set" therefore stays the adapter's business: every ConfigField
  3024. falls back to its declared default (base_url to ChattoHQ) on its own.
  3025. Seed keys are the config.yaml "extra" keys (config_key), NOT the env var
  3026. names — ChattoConfiguration reads extra[config_key]. The special
  3027. 'home_channel' key is extracted by the gateway and becomes a proper
  3028. HomeChannel dataclass on the PlatformConfig; every other key is merged
  3029. into PlatformConfig.extra.
  3030. Function name should be the same as register argument name with "hermes_" prefix, so we
  3031. know that it is needed for plugin register().
  3032. """
  3033. seed: dict[str, Any] = {}
  3034. for config_field in ChattoConfiguration.fields():
  3035. env_value = os.getenv(config_field.env_name)
  3036. if env_value:
  3037. seed[config_field.config_key] = env_value.strip()
  3038. logger.debug("seed: %s", {k: v for k, v in seed.items() if k != "token"})
  3039. return seed
  3040. # ---------------------------------------------------------------------------
  3041. # Plugin registration entry point
  3042. # ---------------------------------------------------------------------------
  3043. # What each capability is called in the startup banner, keyed by the method
  3044. # that implements it. Derived from real overrides rather than hard-coded, so
  3045. # dropping a method drops its claim from the log instead of leaving a lie.
  3046. _CAPABILITY_LABELS = {
  3047. "send": "text",
  3048. "send_image_file": "images",
  3049. "send_multiple_images": "image batches (bundled into one message)",
  3050. "send_video": "video",
  3051. "send_voice": "voice messages",
  3052. "send_document": "documents",
  3053. "add_reaction": "reactions",
  3054. "edit_message": "message editing",
  3055. "delete_message": "message deletion",
  3056. "send_typing": "typing indicators",
  3057. "create_handoff_thread": "threads",
  3058. "start_dm": "direct messages",
  3059. "create_room": "room creation",
  3060. }
  3061. def _capabilities() -> list[str]:
  3062. """Name the things this adapter genuinely implements itself.
  3063. A capability counts only when ChattoAdapter overrides the base method —
  3064. inheriting BasePlatformAdapter's fallback means the feature is not
  3065. natively supported, and announcing it would mislead.
  3066. """
  3067. found = [
  3068. label
  3069. for name, label in _CAPABILITY_LABELS.items()
  3070. if getattr(ChattoAdapter, name, None)
  3071. is not getattr(BasePlatformAdapter, name, None)
  3072. ]
  3073. if ChattoAdapter.supports_code_blocks:
  3074. found.append("code blocks")
  3075. if ChattoAdapter.supports_status_text:
  3076. found.append("custom status text")
  3077. found.append("presence (refreshed while connected)")
  3078. return found
  3079. # --------------------------------------------------------------------------- #
  3080. # Plugin tools: send DM to a user, or send to a room
  3081. # --------------------------------------------------------------------------- #
  3082. CHATTO_LIST_ROOMS_SCHEMA = {
  3083. "name": "chatto_list_rooms",
  3084. "description": (
  3085. "List the Chatto rooms (channels and DM rooms) the bot is a member of, "
  3086. "with their room IDs. Use this to resolve a channel name to its room ID — "
  3087. "e.g. to find the 'platform:chat_id' address for a cron job's `deliver` "
  3088. "target. Optional `name` filters case-insensitively by substring."
  3089. ),
  3090. "parameters": {
  3091. "type": "object",
  3092. "properties": {
  3093. "name": {
  3094. "type": "string",
  3095. "description": (
  3096. "Optional: case-insensitive substring filter on the room name "
  3097. "(e.g. 'liv' matches 'Liv' and 'Liv-Dev'). Empty lists all rooms."
  3098. ),
  3099. },
  3100. "base_url": {
  3101. "type": "string",
  3102. "description": (
  3103. "Chatto server base URL (e.g. 'https://chat.oberrhein.de'). "
  3104. "If omitted, falls back to the configured server URL."
  3105. ),
  3106. },
  3107. "token": {
  3108. "type": "string",
  3109. "description": (
  3110. "Chatto bot token (cht_BK_...). "
  3111. "If omitted, falls back to the configured token."
  3112. ),
  3113. },
  3114. },
  3115. "required": [],
  3116. "additionalProperties": False,
  3117. },
  3118. }
  3119. def _chatto_tool_client(args: dict, cfg):
  3120. """Shared client construction for tool handlers. Returns (client, error_json)."""
  3121. token = args.get("token") or ""
  3122. if not token and cfg is not None:
  3123. token = cfg.token.value or ""
  3124. base_url = args.get("base_url") or ""
  3125. if not base_url and cfg is not None:
  3126. base_url = cfg.base_url.value or "https://chat.chatto.run"
  3127. if not base_url:
  3128. base_url = "https://chat.chatto.run"
  3129. if not token:
  3130. return None, json.dumps({"success": False, "error": "Chatto token not configured"})
  3131. try:
  3132. return ChattoClient(base_url=base_url, token=token), None
  3133. except Exception as exc:
  3134. return None, json.dumps({"success": False, "error": f"Failed to create Chatto client: {exc}"})
  3135. CHATTO_SEND_DM_SCHEMA = {
  3136. "name": "chatto_send_dm",
  3137. "description": (
  3138. "Send a text message as a **Direct Message** to another Chatto user. "
  3139. "Looks up the user by `user_login` or `user_id`, creates or retrieves "
  3140. "a DM room, and posts the message there. Returns success with the "
  3141. "room_id and message_id, or an error."
  3142. ),
  3143. "parameters": {
  3144. "type": "object",
  3145. "properties": {
  3146. "user_login": {
  3147. "type": "string",
  3148. "description": (
  3149. "The Chatto login name of the recipient "
  3150. "(e.g. 'nickk'). Exactly one of `user_login` or `user_id` is required."
  3151. ),
  3152. },
  3153. "user_id": {
  3154. "type": "string",
  3155. "description": (
  3156. "The Chatto user ID of the recipient "
  3157. "(e.g. 'Uxxxxxxxx'). Exactly one of `user_login` or `user_id` is required."
  3158. ),
  3159. },
  3160. "message": {
  3161. "type": "string",
  3162. "description": "The message text to send (markdown supported).",
  3163. },
  3164. "base_url": {
  3165. "type": "string",
  3166. "description": (
  3167. "Chatto server base URL (e.g. 'https://chat.oberrhein.de'). "
  3168. "If omitted, falls back to the configured server URL."
  3169. ),
  3170. },
  3171. "token": {
  3172. "type": "string",
  3173. "description": (
  3174. "Chatto bot token (cht_BK_...). "
  3175. "If omitted, falls back to the configured token."
  3176. ),
  3177. },
  3178. },
  3179. "required": ["message"],
  3180. "additionalProperties": False,
  3181. },
  3182. }
  3183. CHATTO_SEND_ROOM_SCHEMA = {
  3184. "name": "chatto_send_room",
  3185. "description": (
  3186. "Send a text message to a Chatto **room** (channel or DM room) "
  3187. "by its room ID or room name. Returns success with the message_id, "
  3188. "or an error if the room is not found or not joinable."
  3189. ),
  3190. "parameters": {
  3191. "type": "object",
  3192. "properties": {
  3193. "room_id": {
  3194. "type": "string",
  3195. "description": (
  3196. "The Chatto room ID of the destination "
  3197. "(e.g. 'RbmwiHk4vs9a3Ql'). Exactly one of `room_id` or `room_name` is required."
  3198. ),
  3199. },
  3200. "room_name": {
  3201. "type": "string",
  3202. "description": (
  3203. "The exact display name of the room "
  3204. "(case-insensitive). Exactly one of `room_id` or `room_name` is required."
  3205. ),
  3206. },
  3207. "message": {
  3208. "type": "string",
  3209. "description": "The message text to send (markdown supported).",
  3210. },
  3211. "thread_root_event_id": {
  3212. "type": "string",
  3213. "description": (
  3214. "Optional: event ID of the thread-root message. When given, "
  3215. "this message is posted INSIDE that thread. Mutually "
  3216. "exclusive with `create_thread`."
  3217. ),
  3218. },
  3219. "create_thread": {
  3220. "type": "boolean",
  3221. "description": (
  3222. "Optional: post this message as the ROOT of a new thread "
  3223. "(default false = normal message). Mutually exclusive with "
  3224. "`thread_root_event_id`."
  3225. ),
  3226. },
  3227. "base_url": {
  3228. "type": "string",
  3229. "description": (
  3230. "Chatto server base URL (e.g. 'https://chat.oberrhein.de'). "
  3231. "If omitted, falls back to the configured server URL."
  3232. ),
  3233. },
  3234. "token": {
  3235. "type": "string",
  3236. "description": (
  3237. "Chatto bot token (cht_BK_...). "
  3238. "If omitted, falls back to the configured token."
  3239. ),
  3240. },
  3241. },
  3242. "required": ["message"],
  3243. "additionalProperties": False,
  3244. },
  3245. }
  3246. def _chatto_config_for_tools() -> "ChattoConfiguration | None":
  3247. """Resolve the Chatto configuration for plugin tool handlers.
  3248. Three tiers, most specific first:
  3249. 1. ``_resolved_chatto_config`` — set by ``hermes_validate_config`` at
  3250. gateway load; the authoritative snapshot in a running gateway.
  3251. 2. ``load_gateway_config()`` — re-parse of config.yaml + .env seeds,
  3252. available in fresh cron/standalone sessions where the gateway load
  3253. hooks never ran. Mirrors how the built-in send_message tool resolves
  3254. its pconfig (tools/send_message_tool.py).
  3255. 3. ``None`` — nothing configured; the caller reports the error.
  3256. """
  3257. global _resolved_chatto_config
  3258. if _resolved_chatto_config is not None:
  3259. return _resolved_chatto_config
  3260. try:
  3261. from gateway.config import PlatformConfig
  3262. pconfig = PlatformConfig(enabled=True, extra={})
  3263. _resolved_chatto_config = ChattoConfiguration(pconfig=pconfig)
  3264. except Exception as exc:
  3265. logger.warning("Chatto tools: empty pconfig build failed: %s", exc)
  3266. return None
  3267. # ChattoConfiguration resolves env → extra → default, but in a fresh cron
  3268. # scope the dotenv residue is stripped from os.environ. build_profile_secret_scope
  3269. # re-reads the profile's own .env directly — works in gateway, cron, and CLI
  3270. # contexts alike — so missing fields get a second chance from there.
  3271. try:
  3272. from agent.secret_scope import build_profile_secret_scope
  3273. from hermes_constants import get_hermes_home
  3274. profile_secrets = dict(build_profile_secret_scope(get_hermes_home()))
  3275. except Exception as exc:
  3276. logger.warning("Chatto tools: profile secret scope build failed: %s", exc)
  3277. profile_secrets = {}
  3278. for field in ChattoConfiguration.fields():
  3279. current = _resolved_chatto_config._values.get(field.field_name)
  3280. # The profile's own .env is authoritative for secrets (token) and for
  3281. # fields that merely hold their declared default: an EXPLICIT value
  3282. # (env var or config.yaml extra) is the user's own intent and wins;
  3283. # a default placeholder yields to the profile .env. The explicit flag
  3284. # is carried by ConfigValue since the value alone cannot distinguish
  3285. # "user set the default string on purpose" from "default leaked in".
  3286. if current is not None and current.explicit:
  3287. continue
  3288. secret_value = profile_secrets.get(field.env_name)
  3289. if not secret_value:
  3290. continue
  3291. _resolved_chatto_config._values[field.field_name] = ConfigValue(
  3292. secret_value, field.field_name, field.env_name, explicit=True
  3293. )
  3294. return _resolved_chatto_config
  3295. async def _handle_chatto_send_dm(args: dict) -> dict:
  3296. """Tool handler for chatto_send_dm: resolve user, get/create DM room, send message."""
  3297. user_login = args.get("user_login") or ""
  3298. user_id = args.get("user_id") or ""
  3299. message = args.get("message") or ""
  3300. if not message:
  3301. return json.dumps({"success": False, "error": "message is required"})
  3302. if not user_login and not user_id:
  3303. return json.dumps({"success": False, "error": "Exactly one of user_login or user_id is required"})
  3304. # --- Read config: explicit args first, then fall back to resolved config ---
  3305. explicit_base_url = args.get("base_url") or ""
  3306. explicit_token = args.get("token") or ""
  3307. cfg = _chatto_config_for_tools()
  3308. if explicit_token:
  3309. token = explicit_token
  3310. elif cfg is not None:
  3311. token = cfg.token.value or ""
  3312. else:
  3313. token = ""
  3314. if explicit_base_url:
  3315. base_url = explicit_base_url
  3316. elif cfg is not None:
  3317. base_url = cfg.base_url.value or "https://chat.chatto.run"
  3318. else:
  3319. base_url = "https://chat.chatto.run"
  3320. if not token:
  3321. return json.dumps({"success": False, "error": "Chatto token not configured"})
  3322. try:
  3323. client = ChattoClient(base_url=base_url, token=token)
  3324. except Exception as exc:
  3325. return json.dumps({"success": False, "error": f"Failed to create Chatto client: {exc}"})
  3326. try:
  3327. # 1. Resolve the target user — via room member search (no admin rights needed)
  3328. if user_id:
  3329. target_user_id = user_id
  3330. target_login = user_login
  3331. else:
  3332. target_user_id = None
  3333. target_login = user_login
  3334. our_id = (await client.me()).id
  3335. for rws in await client.list_rooms():
  3336. room = rws.room
  3337. if not room:
  3338. continue
  3339. try:
  3340. member_ids, _ = await client.list_room_members(
  3341. room.id, search=user_login
  3342. )
  3343. except ChattoError:
  3344. continue
  3345. if not member_ids:
  3346. continue
  3347. members = await client.batch_get_room_members(room.id, member_ids)
  3348. for m in members:
  3349. u = getattr(m, "user", None)
  3350. login = getattr(u, "login", None) if u else None
  3351. uid = getattr(u, "id", None) if u else None
  3352. if uid and login and login.casefold() == user_login.casefold():
  3353. target_user_id = uid
  3354. target_login = login
  3355. break
  3356. if target_user_id:
  3357. break
  3358. if not target_user_id:
  3359. return json.dumps({
  3360. "success": False,
  3361. "error": f"User not found (login={user_login or user_id})",
  3362. })
  3363. # 2. Find or create the DM room between us and the target
  3364. our_id = (await client.me()).id
  3365. # Search existing rooms for a DM room with this user
  3366. existing_rooms = await client.list_rooms()
  3367. dm_room_id = None
  3368. for rws in existing_rooms:
  3369. room = rws.room
  3370. if not room or room.kind != RoomKind.DM:
  3371. continue
  3372. member_ids, _ = await client.list_room_members(room.id)
  3373. if target_user_id in member_ids and our_id in member_ids:
  3374. dm_room_id = room.id
  3375. break
  3376. if dm_room_id is None:
  3377. # Create a new DM room
  3378. room = await client.start_dm(participant_ids=[our_id, target_user_id])
  3379. dm_room_id = room.id
  3380. logger.info("Chatto DM room created with @%s: %s", target_login, dm_room_id)
  3381. # 3. Post the message
  3382. normalised = _normalise_outbound_text(message)
  3383. chunks = BasePlatformAdapter.truncate_message(
  3384. normalised, ChattoConstants.SPLIT_THRESHOLD
  3385. )
  3386. message_ids = []
  3387. for chunk in chunks:
  3388. msg = await client.post_message(dm_room_id, chunk)
  3389. message_ids.append(msg.id)
  3390. return json.dumps({
  3391. "success": True,
  3392. "dm_room_id": dm_room_id,
  3393. "recipient": target_login,
  3394. "recipient_user_id": target_user_id,
  3395. "message_ids": message_ids,
  3396. "chunks_sent": len(message_ids),
  3397. })
  3398. except ChattoAuthError as exc:
  3399. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  3400. except ChattoError as exc:
  3401. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  3402. finally:
  3403. try:
  3404. await client.close()
  3405. except Exception:
  3406. pass
  3407. async def _handle_chatto_send_room(args: dict) -> dict:
  3408. """Tool handler for chatto_send_room: resolve room, send message."""
  3409. room_id = args.get("room_id") or ""
  3410. room_name = args.get("room_name") or ""
  3411. message = args.get("message") or ""
  3412. thread_root_event_id = args.get("thread_root_event_id") or ""
  3413. create_thread = bool(args.get("create_thread", False))
  3414. if not message:
  3415. return json.dumps({"success": False, "error": "message is required"})
  3416. if not room_id and not room_name:
  3417. return json.dumps({"success": False, "error": "Exactly one of room_id or room_name is required"})
  3418. if thread_root_event_id and create_thread:
  3419. return json.dumps({
  3420. "success": False,
  3421. "error": "thread_root_event_id and create_thread are mutually exclusive",
  3422. })
  3423. # --- Read config: explicit args first, then fall back to resolved config ---
  3424. explicit_token = args.get("token") or ""
  3425. cfg = _chatto_config_for_tools()
  3426. if explicit_token:
  3427. token = explicit_token
  3428. elif cfg is not None:
  3429. token = cfg.token.value or ""
  3430. else:
  3431. token = ""
  3432. base_url = args.get("base_url") or ""
  3433. if not base_url:
  3434. base_url = cfg.base_url.value or "https://chat.chatto.run" if cfg is not None else "https://chat.chatto.run"
  3435. if not token:
  3436. return json.dumps({"success": False, "error": "Chatto token not configured"})
  3437. try:
  3438. client = ChattoClient(base_url=base_url, token=token)
  3439. except Exception as exc:
  3440. return json.dumps({"success": False, "error": f"Failed to create Chatto client: {exc}"})
  3441. try:
  3442. # 1. Resolve the target room
  3443. if not room_id and room_name:
  3444. # Look up room by name
  3445. rooms = await client.list_rooms()
  3446. found = None
  3447. for rws in rooms:
  3448. room = rws.room
  3449. if room and (room.name or "").casefold() == room_name.casefold():
  3450. found = rws
  3451. break
  3452. if not found or not found.room:
  3453. return json.dumps({"success": False, "error": f"Room not found: {room_name}"})
  3454. room_id = found.room.id
  3455. room_label = found.room.name
  3456. else:
  3457. # Verify room exists
  3458. rws = await client.get_room(room_id)
  3459. if not rws or not rws.room:
  3460. return json.dumps({"success": False, "error": f"Room not found: {room_id}"})
  3461. room_label = rws.room.name
  3462. # 2. Post the message
  3463. normalised = _normalise_outbound_text(message)
  3464. chunks = BasePlatformAdapter.truncate_message(
  3465. normalised, ChattoConstants.SPLIT_THRESHOLD
  3466. )
  3467. message_ids = []
  3468. root_message_id = ""
  3469. for idx, chunk in enumerate(chunks):
  3470. # First chunk of a new thread IS the thread root; subsequent
  3471. # chunks land inside the thread. thread_root_event_id given
  3472. # explicitly puts every chunk inside that existing thread.
  3473. effective_root = thread_root_event_id or root_message_id
  3474. msg = await client.post_message(room_id, chunk, thread_root_event_id=effective_root)
  3475. message_ids.append(msg.id)
  3476. if create_thread and not root_message_id:
  3477. root_message_id = msg.id
  3478. # 3. A newly created thread should be followed so later activity
  3479. # reaches the bot (mirrors the adapter's own thread handling).
  3480. if create_thread and root_message_id:
  3481. try:
  3482. await client.follow_thread(room_id, root_message_id)
  3483. except Exception:
  3484. # Follow is best-effort — the thread exists and the message
  3485. # was delivered; don't fail the tool over a follow hiccup.
  3486. pass
  3487. return json.dumps({
  3488. "success": True,
  3489. "room_id": room_id,
  3490. "room_name": room_label,
  3491. "message_ids": message_ids,
  3492. "chunks_sent": len(message_ids),
  3493. "thread_root_event_id": thread_root_event_id or root_message_id,
  3494. "created_thread": bool(root_message_id),
  3495. })
  3496. except ChattoAuthError as exc:
  3497. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  3498. except ChattoError as exc:
  3499. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  3500. finally:
  3501. try:
  3502. await client.close()
  3503. except Exception:
  3504. pass
  3505. async def _handle_chatto_list_rooms(args: dict) -> dict:
  3506. """Tool handler for chatto_list_rooms: list rooms the bot is a member of."""
  3507. name_filter = (args.get("name") or "").strip().casefold()
  3508. cfg = _chatto_config_for_tools()
  3509. client, err = _chatto_tool_client(args, cfg)
  3510. if err:
  3511. return err
  3512. try:
  3513. rooms_out = []
  3514. for rws in await client.list_rooms():
  3515. room = getattr(rws, "room", None)
  3516. if not room:
  3517. continue
  3518. # DM rooms have no name; label them by their ID so they stay
  3519. # distinguishable in the output.
  3520. name = room.name or ""
  3521. kind = str(getattr(room.kind, "value", getattr(room.kind, "name", room.kind)) or "")
  3522. label = name or f"(DM room {room.id})"
  3523. if name_filter:
  3524. # A filter implies named rooms: skip DMs (empty name) and
  3525. # non-matching names alike.
  3526. if not name or name_filter not in name.casefold():
  3527. continue
  3528. rooms_out.append({
  3529. "room_id": room.id,
  3530. "name": name,
  3531. "kind": kind,
  3532. "threading_mode": str(
  3533. getattr(getattr(room, "threading_mode", None), "value", "") or ""
  3534. ),
  3535. "archived": bool(getattr(room, "archived", False)),
  3536. "label": label,
  3537. })
  3538. return json.dumps({
  3539. "success": True,
  3540. "count": len(rooms_out),
  3541. "rooms": rooms_out,
  3542. "hint": (
  3543. "Use room_id in chatto_send_room, or as the chat_id part of a "
  3544. "cron deliver target like 'chatto-platform:<room_id>'."
  3545. ),
  3546. })
  3547. except ChattoAuthError as exc:
  3548. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  3549. except ChattoError as exc:
  3550. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  3551. finally:
  3552. try:
  3553. await client.close()
  3554. except Exception:
  3555. pass
  3556. def register(ctx) -> None:
  3557. """Plugin entry point — called by the Hermes plugin system."""
  3558. logger.info("Registering Chatto platform plugin on Hermes Agent")
  3559. # --- Plugin tools ---
  3560. ctx.register_tool(
  3561. name="chatto_send_dm",
  3562. toolset="chatto-platform",
  3563. schema=CHATTO_SEND_DM_SCHEMA,
  3564. handler=_handle_chatto_send_dm,
  3565. is_async=True,
  3566. description=CHATTO_SEND_DM_SCHEMA["description"],
  3567. )
  3568. ctx.register_tool(
  3569. name="chatto_send_room",
  3570. toolset="chatto-platform",
  3571. schema=CHATTO_SEND_ROOM_SCHEMA,
  3572. handler=_handle_chatto_send_room,
  3573. is_async=True,
  3574. description=CHATTO_SEND_ROOM_SCHEMA["description"],
  3575. )
  3576. ctx.register_tool(
  3577. name="chatto_list_rooms",
  3578. toolset="chatto-platform",
  3579. schema=CHATTO_LIST_ROOMS_SCHEMA,
  3580. handler=_handle_chatto_list_rooms,
  3581. is_async=True,
  3582. description=CHATTO_LIST_ROOMS_SCHEMA["description"],
  3583. )
  3584. # --- Bundled skills ---
  3585. # Ship the plugin's own usage/development knowledge with the code, so any
  3586. # agent using this plugin knows when/how to use its tools and how to
  3587. # extend it. Loaded via skill_view("chatto-platform:chatto-platform-plugin").
  3588. try:
  3589. from pathlib import Path
  3590. skills_dir = Path(__file__).parent / "skills"
  3591. for child in sorted(skills_dir.iterdir()):
  3592. skill_md = child / "SKILL.md"
  3593. if child.is_dir() and skill_md.exists():
  3594. ctx.register_skill(child.name, str(skill_md))
  3595. logger.info("Chatto plugin skill registered: %s", child.name)
  3596. except Exception as exc: # pragma: no cover — skill loading is best-effort
  3597. logger.warning("Chatto plugin skill registration failed: %s", exc)
  3598. for capability in _capabilities():
  3599. logger.info("Chatto capability: %s", capability)
  3600. ctx.register_platform(
  3601. name=ChattoConstants.PLATFORM_NAME, # this will be the config.yaml key.
  3602. label=ChattoConstants.PLATFORM_LABEL,
  3603. adapter_factory=hermes_adapter_factory,
  3604. check_fn=hermes_check_fn,
  3605. validate_config=hermes_validate_config,
  3606. is_connected=hermes_is_connected,
  3607. install_hint=ChattoConstants.INSTALL_HINT,
  3608. env_enablement_fn=hermes_env_enablement_fn,
  3609. setup_fn=hermes_setup_fn,
  3610. cron_deliver_env_var=ChattoConfiguration.home_channel.env_name,
  3611. standalone_sender_fn=hermes_standalone_sender_fn,
  3612. allowed_users_env=ChattoConfiguration.allowed_users.env_name,
  3613. allow_all_env=ChattoConfiguration.allow_all_users.env_name,
  3614. max_message_length=ChattoConstants.MAX_MESSAGE_LENGTH,
  3615. emoji="😺",
  3616. allow_update_command=True,
  3617. pii_safe=False,
  3618. platform_hint=(
  3619. "Using the 'Hermes Chatto Platform Plugin' you connect to a Chatto server. "
  3620. "Authorized admins and users will contact you and call you his Hermes Agent. They are "
  3621. "natural persons and thus responsible for what they do in terms of rights. You compute "
  3622. "on their behalf. They _may_ address you by @-mentioning your name. If configured, "
  3623. "you also react without a @-mention. Direct messages reach you without a mention."
  3624. "Keep responses conversational. Markdown is supported. "
  3625. "Include MEDIA:/absolute/path/to/file in your response to refer to our local files. Images "
  3626. "(.png, .jpg, .gif, .webp) arrive as inline pictures, videos (.mp4, .mov, .webm) as "
  3627. "video attachments, audio as a voice bubble, anything else as a downloadable document. "
  3628. "Do NOT use markdown image syntax for local files. Local files always go through MEDIA:. "
  3629. "Several images in one response are bundled into a single message. "
  3630. ),
  3631. )