adapter.py 190 KB

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