adapter.py 338 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459
  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. import time
  26. from collections import deque
  27. from collections.abc import AsyncIterator, Callable, Iterable
  28. from contextlib import asynccontextmanager
  29. from dataclasses import dataclass, field
  30. from datetime import UTC, datetime
  31. from difflib import SequenceMatcher
  32. from enum import StrEnum
  33. from pathlib import Path
  34. from typing import Any, Final, NamedTuple, TypeVar
  35. from urllib.parse import unquote, urlsplit
  36. import httpx
  37. logger = logging.getLogger(__name__)
  38. from gateway.config import Platform, PlatformConfig
  39. from gateway.platforms.base import (
  40. BasePlatformAdapter,
  41. ExecApprovalPrompt,
  42. MessageEvent,
  43. MessageType,
  44. ProcessingOutcome,
  45. SendResult,
  46. cache_media_bytes,
  47. get_inbound_media_max_bytes,
  48. validate_inbound_media_size,
  49. validate_media_delivery_path,
  50. )
  51. from gateway.session import SessionSource
  52. # Chattolib imports (vendored)
  53. # Using vendored chattolib from vendor/chattolib/
  54. # See vendor_chattolib.sh for how to update the vendored copy
  55. # Absolute imports — vendor/ is on sys.path (see above) and chattolib's own
  56. # modules import each other absolutely. Mixing in relative ".vendor.chattolib"
  57. # imports would load a second, distinct copy of every module, so isinstance()
  58. # checks across the two copies would silently fail.
  59. try:
  60. from chattolib._pb.chatto.api.v1.presence_pb2 import (
  61. PresenceStatus as ProtoPresenceStatus, # type: ignore[import-untyped]
  62. )
  63. from chattolib._pb.chatto.realtime.v1 import ( # noqa: F401 — importability probe
  64. realtime_pb2,
  65. )
  66. from chattolib._pb.chatto.realtime.v1.events_pb2 import (
  67. MessageMention, # type: ignore[import-untyped]
  68. MessagePostedEvent, # type: ignore[import-untyped]
  69. )
  70. from chattolib.client import (
  71. ChattoClient,
  72. )
  73. from chattolib.exceptions import (
  74. ChattoAuthError,
  75. ChattoConnectError,
  76. ChattoError,
  77. )
  78. from chattolib.realtime import (
  79. ChattoRealtimeCloseError,
  80. ChattoRealtimeError,
  81. RealtimeEvent,
  82. stream_events,
  83. )
  84. from chattolib.types import (
  85. DirectoryMember,
  86. Message,
  87. MessageAttachment,
  88. MessageSearchGroupBy,
  89. MessageSearchOrder,
  90. MessageSearchResult,
  91. MessageSearchScope,
  92. MessageSearchState,
  93. PresenceStatus,
  94. Room,
  95. RoomKind,
  96. RoomWithViewerState,
  97. User,
  98. format_datetime,
  99. )
  100. except ImportError as e:
  101. # Fail loudly: continuing here only defers the failure to a confusing
  102. # NameError somewhere deep in the adapter.
  103. logger.error("Chatto: failed to import vendored chattolib: %s", e)
  104. raise
  105. try:
  106. from .platform_config import (
  107. ChattoConfiguration,
  108. ChattoConstants,
  109. )
  110. except ImportError: # pragma: no cover - loaded as a top-level module (tests)
  111. from platform_config import (
  112. ChattoConfiguration,
  113. ChattoConstants,
  114. )
  115. # --------------------------------------------------------------------------- #
  116. # Chat types
  117. # --------------------------------------------------------------------------- #
  118. class HermesChatType(StrEnum):
  119. """The ``chat_type`` vocabulary the Hermes gateway understands.
  120. Declared in ``gateway/session.py:161`` as ``"dm", "group", "channel",
  121. "thread"`` and consumed as a bare string all over the gateway:
  122. ``SessionSource.description`` (session.py:239) and the PII-redacting
  123. description in ``build_session_context_prompt`` (session.py:537) both
  124. branch on these exact values and fall back to a nameless generic case for
  125. anything else, and ``build_session_key`` puts the value straight into the
  126. session key. Passing a chattolib ``RoomKind`` (``"ROOM_KIND_CHANNEL"``)
  127. therefore does not fail loudly — it just quietly degrades what the agent is
  128. told about where it is.
  129. A StrEnum so it stays a drop-in ``str`` at every one of those call sites.
  130. GROUP vs CHANNEL
  131. ----------------
  132. There is no strict contract between the two, and the adapters disagree in
  133. practice: Slack labels every non-DM conversation ``"group"`` (including real
  134. channels), Discord uses both, and Telegram reserves ``"channel"`` for actual
  135. broadcast channels. The intended reading is ``group`` = ordinary
  136. multi-participant chat, ``channel`` = broadcast surface.
  137. The distinction only changes behaviour in three places:
  138. 1. Authorization (``gateway/authz_mixin.py``) — the only security-relevant
  139. one. The group-scoped env allowlists apply to ``{"group", "forum"}``
  140. ONLY, never to ``"channel"``: ``{PLATFORM}_GROUP_ALLOWED_USERS`` /
  141. ``_GROUP_ALLOWED_CHATS`` (:616), the chat-id allowlist (:708) and the
  142. Telegram legacy shim (:724). The adapter-delegation paths in turn treat
  143. all three alike (:461, :649, :674, :694), where the value only picks
  144. ``group_allow_from`` over ``allow_from`` from ``config.extra``.
  145. For Chatto both choices are equivalent today: those group env maps hold
  146. Telegram and QQBot only (:535-541), and our own allowlist runs through
  147. ``CHATTO_ALLOWED_USERS``, which is chat_type-independent.
  148. 2. What the agent is told — ``SessionSource.description`` renders
  149. ``"group: Name"`` vs ``"channel: Name"`` (session.py:239-246), likewise
  150. the PII-redacted variant (session.py:537-544).
  151. 3. The session key, which embeds the literal (session.py:1192). Changing
  152. the value for a room re-buckets its existing sessions.
  153. Explicitly NOT affected: ``is_shared_multi_user_session`` (session.py:1063)
  154. only looks at ``"dm"`` and ``thread_id``, so sender prefixes, the multi-user
  155. prompt line and ``group_sessions_per_user`` treat group and channel
  156. identically.
  157. """
  158. DM = "dm"
  159. GROUP = "group"
  160. CHANNEL = "channel"
  161. # Emitted by adapters whose thread events are their own chat type (Slack,
  162. # Discord). We don't: a Chatto thread keeps its room's chat_type and is
  163. # identified by ``thread_id`` on the source instead. Listed for the record,
  164. # because build_session_key rewrites the slot to "thread" itself
  165. # (session.py:1190).
  166. THREAD = "thread"
  167. # Not declared in session.py:161 but real: Telegram forum topics travel as
  168. # "forum", and the authz group allowlists above accept it alongside "group".
  169. # Chatto has no equivalent, so we never emit it.
  170. # Chatto only distinguishes DMs from channels. UNSPECIFIED means the server
  171. # sent a kind this vendored chattolib doesn't know: map it to the generic
  172. # multi-user bucket rather than guessing "channel", and never to "dm" — that
  173. # value drives session isolation (is_shared_multi_user_session, session.py:1063)
  174. # and would silently turn a room into a private conversation.
  175. #
  176. # CHANNEL for RoomKind.CHANNEL is the descriptive choice and carries no
  177. # behavioural cost (see the GROUP vs CHANNEL note above). Switching to GROUP for
  178. # Slack parity would be this one line — plus the re-bucketing of existing
  179. # sessions that point 3 of that note describes.
  180. _ROOM_KIND_TO_CHAT_TYPE: dict[RoomKind, HermesChatType] = {
  181. RoomKind.DM: HermesChatType.DM,
  182. RoomKind.CHANNEL: HermesChatType.CHANNEL,
  183. RoomKind.UNSPECIFIED: HermesChatType.GROUP,
  184. }
  185. def chat_type_for_room_kind(kind: RoomKind | None) -> HermesChatType:
  186. """Map a chattolib RoomKind onto the gateway's chat_type vocabulary.
  187. An unknown or missing kind becomes ``GROUP`` — see ``_ROOM_KIND_TO_CHAT_TYPE``.
  188. """
  189. if kind is None:
  190. return HermesChatType.GROUP
  191. return _ROOM_KIND_TO_CHAT_TYPE.get(kind, HermesChatType.GROUP)
  192. class RoomPolicy(StrEnum):
  193. """How a channel-kind room treats an inbound message.
  194. Derived per room from ``CHATTO_REQUIRE_MENTION_ROOMS`` /
  195. ``CHATTO_OPTIONAL_MENTION_ROOMS`` — see ``_room_policy``. A StrEnum
  196. so the values log readably without a formatting dance.
  197. """
  198. # Listed for unaddressed answers: every message is dispatched, and one
  199. # aimed at a named colleague gets a 🫥 acknowledgement instead of a reply.
  200. OPEN = "open"
  201. # Listed for addressed-only participation: dispatches only messages that
  202. # mention the bot (@name or a broadcast handle); others are dropped.
  203. REQUIRE_MENTION = "require_mention"
  204. # Listed in neither config: the room is silent — not dispatched at all,
  205. # read-only like any unlisted membership.
  206. SILENT = "silent"
  207. # Blocking filesystem/network helpers. The adapter runs on the shared gateway
  208. # event loop, so file reads and HTTP downloads are pushed to a worker thread
  209. # via asyncio.to_thread — a slow disk or dead image URL must not stall every
  210. # platform's message processing.
  211. def _read_file_bytes(path: str) -> bytes:
  212. """Read a whole file synchronously (run via asyncio.to_thread)."""
  213. with open(path, "rb") as f:
  214. return f.read()
  215. def _write_file_bytes(path: str, data: bytes) -> None:
  216. """Write bytes to a file synchronously (run via asyncio.to_thread)."""
  217. with open(path, "wb") as f:
  218. f.write(data)
  219. def _normalise_outbound_text(content: str) -> str:
  220. """Normalise outgoing text for Chatto.
  221. Chatto renders Markdown natively, so there is nothing to escape or
  222. translate — the only transformations here are the ones that measurably
  223. render wrong: CRLF line endings (which show up as stray blank lines)
  224. and runs of more than two blank lines. Shared by the adapter's
  225. ``format_message`` and the standalone cron sender, so both paths render
  226. identically.
  227. """
  228. if not content:
  229. return content
  230. normalised = content.replace("\r\n", "\n").replace("\r", "\n")
  231. while "\n\n\n\n" in normalised:
  232. normalised = normalised.replace("\n\n\n\n", "\n\n\n")
  233. return normalised
  234. def _oldest_first(messages: list[Message]) -> list[Message]:
  235. """Sort messages by the timestamp they carry, oldest first.
  236. The server's ordering is not a contract anything here builds on — it is a
  237. page, and a page's order depends on which end it was fetched from — so
  238. every timeline is read the way a conversation is spoken. A message with no
  239. timestamp sorts first: it is the one whose position we know least about,
  240. and putting it at the end would claim an order the data does not support.
  241. Shared by the thread seeding and the read tools so the block an agent reads
  242. in a session and the block it gets from ``chatto_get_history`` are ordered
  243. by one rule.
  244. """
  245. return sorted(
  246. messages, key=lambda m: m.created_at or datetime.min.replace(tzinfo=UTC)
  247. )
  248. # --------------------------------------------------------------------------- #
  249. # Adapter
  250. # --------------------------------------------------------------------------- #
  251. # Presence as the roster block spells it. UNSPECIFIED gets no label: a server
  252. # that never tracked presence should not have the roster claim anything.
  253. _PRESENCE_LABELS = {
  254. PresenceStatus.ONLINE: "online",
  255. PresenceStatus.AWAY: "away",
  256. PresenceStatus.DO_NOT_DISTURB: "do not disturb",
  257. PresenceStatus.OFFLINE: "offline",
  258. }
  259. # Proto enum name (``PRESENCE_STATUS_ONLINE``) → chattolib member, built once
  260. # from the enum's own values. The realtime wire carries the presence status as
  261. # a plain int (protobuf runtime without native enum wrappers), so every
  262. # presence change needs this lookup — a linear scan per event would be O(n) on
  263. # a hot path for a five-member enum.
  264. _PRESENCE_BY_PROTO_NAME = {member.value: member for member in PresenceStatus}
  265. @dataclass(frozen=True)
  266. class _Mentions:
  267. """Who one inbound message addresses.
  268. Both admission gates read this and nothing else, so "addressed" has a
  269. single definition. What fills it differs by path, not what counts:
  270. - :meth:`from_payload` reads the targets the server resolved when the
  271. message was posted. It is authoritative and the only source the posted
  272. path uses.
  273. - ``ChattoAdapter._mentions_from_body`` re-derives them from the text,
  274. because an edit event carries no targets — see
  275. ``_dispatch_message_edited``.
  276. Broadcast and role targets arrive unexpanded (one ``all``/``here``/``role``
  277. entry, never one per recipient), which is why the payload path needs no
  278. room roster to answer "was this meant for me".
  279. """
  280. me: bool = False
  281. someone_else: bool = False
  282. @classmethod
  283. def from_payload(
  284. cls, mentions: Iterable[MessageMention], my_user_id: str
  285. ) -> _Mentions:
  286. """Read the server-resolved targets of a ``message_posted`` event.
  287. ``includes_viewer`` is the server's own answer to "did this target
  288. include me", decided when the message was posted and unaffected by
  289. membership since. A direct target naming somebody else is the only
  290. kind that means "not for us": broadcasts and roles address the room,
  291. and we are in it.
  292. """
  293. me = False
  294. someone_else = False
  295. for mention in mentions:
  296. if mention.includes_viewer:
  297. me = True
  298. elif mention.WhichOneof("cause") == "direct" and (
  299. mention.direct.user_id != my_user_id
  300. ):
  301. someone_else = True
  302. return cls(me=me, someone_else=someone_else)
  303. @dataclass
  304. class _RoomRoster:
  305. """One announced room's roster projection.
  306. A miniature of the server's room membership: which user IDs belong to
  307. the room (the users themselves live in the shared ``_user_cache``) and
  308. how many directory entries were beyond the fetch limit, rendered as
  309. ``… and N more``. Presence changes patch the cached users in place;
  310. only a membership change or reconnect discards this and refetches.
  311. """
  312. member_ids: set[str] = field(default_factory=set)
  313. unfetched: int = 0
  314. @dataclass
  315. class _ClarifyPrompt:
  316. """State for a reaction-driven clarify prompt posted to a Chatto room."""
  317. clarify_id: str
  318. choices: list[str]
  319. session_key: str
  320. chat_id: str
  321. message_id: str
  322. resolved: bool = False
  323. awaiting_text: bool = False
  324. resolved_by: str | None = None
  325. resolved_choice: str | None = None
  326. expires_at: float = 0.0
  327. @dataclass
  328. class _ApprovalPrompt:
  329. """State for a reaction-driven exec-approval prompt posted to a Chatto room."""
  330. session_key: str
  331. chat_id: str
  332. message_id: str
  333. choices_map: dict[str, str] # emoji -> choice
  334. resolved: bool = False
  335. resolved_by: str | None = None
  336. resolved_choice: str | None = None
  337. expires_at: float = 0.0
  338. INTERACTIVE_PROMPT_TIMEOUT_SECONDS = 300
  339. # Check-mark variants that Chatto clients send for "the green check". The
  340. # prompts seed ✅ (U+2705), but emoji pickers offer near-identical glyphs with
  341. # a different codepoint — ✔️ adds a variation selector, ✓ is the plain text
  342. # check — and a client may also append or strip U+FE0F in transit. Matching
  343. # only the seeded form rejected the very reaction the prompt asks for.
  344. _CHECK_EMOJI_ALIASES: Final[frozenset[str]] = frozenset(
  345. {"\u2705", "\u2714", "\u2713", "\u2611", "\u221a"}
  346. )
  347. def _normalize_reaction_emoji(emoji: str) -> str:
  348. """The comparison key for a reaction emoji.
  349. Callers pass a decoded display emoji — shortcode names are translated by
  350. ``_SHORTCODE_TO_PROMPT_EMOJI`` first (see ``_try_handle_interactive_reaction``).
  351. Strips variation selectors (U+FE0E/U+FE0F) so ✔️ and ✔ collapse, then
  352. folds check-mark variants onto ✅. Keycap sequences (1️⃣ = digit + VS16 +
  353. combining keycap) lose only the VS16 — digit and U+20E3 survive, and seed
  354. and incoming reaction normalize to the same key.
  355. """
  356. stripped = emoji.replace("\ufe0e", "").replace("\ufe0f", "")
  357. return "✅" if stripped in _CHECK_EMOJI_ALIASES else stripped
  358. # Shortcodes the Chatto server accepts for AddReaction — its API takes gemoji
  359. # names ("white_check_mark"), never Unicode, and rejects anything else with
  360. # `invalid_argument: unsupported emoji`. The realtime events echo the same
  361. # names back, so incoming reactions arrive as shortcodes too. Reactions sent
  362. # as Unicode were silently dropped server-side; the cards then showed no
  363. # reaction anchors and every matching reaction answered "not a valid option".
  364. _PROMPT_SHORTCODES: Final[dict[str, str]] = {
  365. "1️⃣": "one",
  366. "2️⃣": "two",
  367. "3️⃣": "three",
  368. "4️⃣": "four",
  369. "5️⃣": "five",
  370. "6️⃣": "six",
  371. "7️⃣": "seven",
  372. "8️⃣": "eight",
  373. "9️⃣": "nine",
  374. "✏️": "pencil2",
  375. "✏": "pencil2",
  376. "✅": "white_check_mark",
  377. "🌀": "cyclone",
  378. "♾️": "infinity",
  379. "♾": "infinity",
  380. "❌": "x",
  381. }
  382. def _shortcode_for_reaction_emoji(emoji: str) -> str:
  383. """The shortcode name a Chatto reaction must be sent as.
  384. Prompt emojis map explicitly; anything else falls through to the adapter's
  385. general shortcode table. An unknown emoji is sent as-is — the server
  386. rejects it with `unsupported emoji`, which the caller logs.
  387. """
  388. return _PROMPT_SHORTCODES.get(emoji) or ChattoConstants.EMOJI_TO_SHORTCODE.get(
  389. emoji, emoji
  390. )
  391. # Inverse of _PROMPT_SHORTCODES plus the check-mark aliases: the server's
  392. # realtime events carry shortcode names, and reaction matching compares the
  393. # display emojis the cards seed — so "white_check_mark" must read as ✅.
  394. _SHORTCODE_TO_PROMPT_EMOJI: Final[dict[str, str]] = {
  395. name: emoji for emoji, name in _PROMPT_SHORTCODES.items()
  396. }
  397. _SHORTCODE_TO_PROMPT_EMOJI["heavy_check_mark"] = "✅"
  398. _SHORTCODE_TO_PROMPT_EMOJI["ballot_box_with_check"] = "✅"
  399. # Reaction glyphs for the interactive prompt cards. The display forms keep
  400. # their variation selectors — they are what the card text and the seed
  401. # reactions show, and that is what should render as colour emoji. Reaction
  402. # *matching* never uses them directly: incoming emojis are normalized first
  403. # (_normalize_reaction_emoji), so every comparison happens against the
  404. # normalized *_KEY forms below.
  405. _PROMPT_NUMBER_EMOJIS: Final[tuple[str, ...]] = (
  406. "1️⃣",
  407. "2️⃣",
  408. "3️⃣",
  409. "4️⃣",
  410. "5️⃣",
  411. "6️⃣",
  412. "7️⃣",
  413. "8️⃣",
  414. "9️⃣",
  415. )
  416. _PROMPT_OTHER_EMOJI: Final = "✏️"
  417. _PROMPT_APPROVAL_EMOJIS: Final[dict[str, str]] = {
  418. "once": "✅",
  419. "session": "🌀",
  420. "always": "♾️",
  421. "deny": "❌",
  422. }
  423. _NUMBER_EMOJI_KEYS: Final[tuple[str, ...]] = tuple(
  424. _normalize_reaction_emoji(emoji) for emoji in _PROMPT_NUMBER_EMOJIS
  425. )
  426. _OTHER_EMOJI_KEY: Final = _normalize_reaction_emoji(_PROMPT_OTHER_EMOJI)
  427. _UNSEEDED: Final = object()
  428. """Sentinel: nothing is known yet about how far this thread's session reaches.
  429. The in-memory map starts every thread here. The session's own copy starts here
  430. too — a session carrying no cursor is one whose frontier cannot be established
  431. (see _thread_context_for), which is not the same as a thread this process has
  432. not seeded.
  433. """
  434. _CURSOR_META_KEY: Final = "chatto.thread_seed_cursor"
  435. """Session-metadata key holding the last message ID a thread's session reached.
  436. One opaque ID per thread, on the session the platform already owns: nothing of
  437. the thread's content is mirrored anywhere, and the value dies with the session.
  438. """
  439. _T = TypeVar("_T")
  440. def hermes_adapter_factory(config: PlatformConfig):
  441. """Construct a ChattoAdapter from a PlatformConfig."""
  442. return ChattoAdapter(config)
  443. class ChattoAdapter(BasePlatformAdapter):
  444. """Chatto platform adapter.
  445. Receives messages via WebSocket realtime, sends via ConnectRPC.
  446. """
  447. # Read by BasePlatformAdapter.max_message_length_for_chat(), which the
  448. # gateway and the stream consumer use to chunk outgoing messages. Without
  449. # it they fall back to 4096 and split Chatto messages far earlier than
  450. # necessary — send() itself already truncates at SPLIT_THRESHOLD.
  451. MAX_MESSAGE_LENGTH = ChattoConstants.MAX_MESSAGE_LENGTH
  452. splits_long_messages = True
  453. supports_code_blocks: bool = True
  454. supports_status_text: bool = True # client.update_custom_status
  455. def __init__(self, pconfig: PlatformConfig):
  456. """Signature needs to be compatible with BasePlatformAdapter.__init__."""
  457. super().__init__(
  458. config=pconfig, platform=Platform(ChattoConstants.PLATFORM_NAME)
  459. )
  460. # "extra" has been pre-populated by Hermes from config.yaml's extra block.
  461. # --- Configuration from our configuration data class with some logic ---
  462. self.chatto_config: ChattoConfiguration = ChattoConfiguration(pconfig)
  463. # ------ State -------
  464. # Our own user, filled in by connect(). Events arriving before connect()
  465. # completes must not blow up on an undefined attribute.
  466. self.me: User | None = None
  467. # --- Runtime state ---
  468. self._room_names: dict[str, str] = {}
  469. self._room_kinds: dict[str, RoomKind] = {}
  470. # Last realtime cursor whose event was fully applied. Passed back to
  471. # stream_events on reconnect so the server resumes after it instead of
  472. # starting at the current boundary and silently skipping everything
  473. # that arrived while we were disconnected. None until the first event
  474. # carries one (chattolib leaves it unset for unreplayable events).
  475. self._realtime_cursor: str | None = None
  476. # Event IDs already processed — chattolib may redeliver events across
  477. # reconnects, so every inbound event is checked against this list.
  478. # Bounded deques: appending past the cap drops the oldest ID on its own.
  479. self._seen: deque[str] = deque(maxlen=ChattoConstants.SEEN_CAP)
  480. # Message IDs this adapter has handed to the gateway (posted or
  481. # edit-re-dispatched). Edits of anything on this list never start a
  482. # fresh turn — that is the lock against re-answering settled
  483. # conversations by editing old messages.
  484. self._dispatched_ids: deque[str] = deque(maxlen=ChattoConstants.SEEN_CAP)
  485. # Rooms whose member roster has been projected from the directory —
  486. # the roster is room-scoped and rides on every channel turn (threads
  487. # hold isolated sessions, so each turn needs its own copy). Together
  488. # with _rosters this is our miniature projection of server state:
  489. # presence changes patch the cached users in place; membership events
  490. # and reconnects discard a room so its next turn refetches once.
  491. self._roster_announced: deque[str] = deque(maxlen=ChattoConstants.SEEN_CAP)
  492. # Announced rooms' membership projection (see _RoomRoster).
  493. self._rosters: dict[str, _RoomRoster] = {}
  494. # Thread roots whose history has been handed to their session, mapped to
  495. # the last message ID that was delivered. Thread sessions start empty, so
  496. # the first turn into a thread seeds what came before it — a cron message
  497. # whose echo we filtered as a self-event, or chatter that predates the
  498. # bot joining.
  499. #
  500. # The cursor is what makes later turns safe to seed again: the agent's
  501. # own messages never reach a session (their echo is filtered as a
  502. # self-event), so a reply it posts into a live thread via
  503. # chatto_send_room is invisible to that thread forever unless the tail
  504. # after the cursor is delivered. Re-delivering everything instead would
  505. # duplicate context on every turn. Insertion-ordered and capped like the
  506. # seen-ring; unlike the roster this is deliberately NOT discarded on
  507. # reconnect, since thread history is immutable.
  508. #
  509. # Keyed by thread root, which assumes one session per thread. With
  510. # thread_sessions_per_user turned on, each participant gets their own
  511. # session while this map stays shared, so one participant's turn moves
  512. # the cursor past the other's messages and that session is seeded once
  513. # and never again. The flag is off by default and this is the price of
  514. # switching it on; keying the map by session key would fix it.
  515. self._seeded_threads: dict[str, str] = {}
  516. # Threads whose cursor is on its way to the session but has not landed:
  517. # a thread's first turn has no session yet, and the second turn retries.
  518. self._unpersisted_threads: set[str] = set()
  519. # session_key -> message ID currently being processed there. Written
  520. # by on_processing_start, cleared by on_processing_complete; an edit
  521. # landing on the recorded ID is a mid-run correction.
  522. self._processing: dict[str, str] = {}
  523. self._joined_room_ids: list[str] = []
  524. # Rooms the server force-joined everyone into (Room.universal) — used
  525. # only for [universal] tags in the joined-rooms log line, never for
  526. # gating.
  527. self._universal_room_ids: set[str] = set()
  528. # DM room ID -> chat partner's login, resolved once via the member
  529. # directory so the joined-rooms summary can name who a DM is with.
  530. self._dm_partners: dict[str, str] = {}
  531. # One-shot guard for the unjoined-home-channel warning in _refresh_rooms.
  532. self._home_warning_logged = False
  533. self._ws_task: asyncio.Task | None = None
  534. self._presence_task: asyncio.Task | None = None
  535. # Persistent typing indicator loops per room
  536. self._typing_tasks: dict[str, asyncio.Task] = {}
  537. # Member directory cache: user_id -> user info dict
  538. self._user_cache: dict[str, User] = {}
  539. # Interactive prompt registries (clarify + exec-approval), keyed by
  540. # message_id. Reverse lookup _clarify_id_to_message supports
  541. # retire_clarify_card.
  542. self._clarify_prompts: dict[str, _ClarifyPrompt] = {}
  543. self._approval_prompts: dict[str, _ApprovalPrompt] = {}
  544. self._clarify_id_to_message: dict[str, str] = {}
  545. # Chattolib client cache and lock for async access.
  546. self._chatto_client: ChattoClient | None = None
  547. self._chatto_client_lock: asyncio.Lock = asyncio.Lock()
  548. # ------------------------------------------------------------------ #
  549. # Auth
  550. # ------------------------------------------------------------------ #
  551. async def _get_chatto_client(self: ChattoAdapter) -> ChattoClient | None:
  552. """Return the shared ChattoClient, creating and logging in on first use.
  553. The default way to get a client. The fast path is a plain attribute
  554. read; first creation runs under a lock so concurrent callers log in
  555. exactly once. Returns ``None`` when no client exists yet or creation
  556. failed (bad credentials, unreachable server) — a normal state during
  557. startup, shutdown and reconnects, not an exceptional one. Callers
  558. decide what "no client" means for them, as a guard clause:
  559. client = await self._get_chatto_client()
  560. if client is None:
  561. logger.warning("Chatto: dropping X - no client available")
  562. return
  563. """
  564. if self._chatto_client is not None:
  565. return self._chatto_client
  566. async with self._chatto_client_lock:
  567. if self._chatto_client is not None:
  568. return self._chatto_client
  569. try:
  570. client = await self._open_client(
  571. base_url=self.chatto_config.base_url.value,
  572. token=self.chatto_config.token.value,
  573. )
  574. self._chatto_client = client
  575. logger.info(
  576. "Chatto: authenticated via token to '%s'",
  577. self.chatto_config.base_url.value,
  578. )
  579. return client
  580. except ChattoAuthError as e:
  581. logger.error("Chatto: authentication failed: %s", e)
  582. return None
  583. except (ChattoError, ValueError) as e:
  584. logger.error("Chatto: failed to create client: %s", e)
  585. return None
  586. async def _require_chatto_client(self) -> ChattoClient:
  587. """Return a ChattoClient or raise RuntimeError if unavailable.
  588. The exception-flavoured variant of :meth:`_get_chatto_client`, for
  589. callers whose surrounding machinery already routes exceptions.
  590. Currently that is only the realtime event loop, whose except chain
  591. turns the raise into a logged warning plus a backed-off reconnect —
  592. no special "no client" branch needed there. Everywhere else, prefer
  593. the ``is None`` guard shown in :meth:`_get_chatto_client`.
  594. """
  595. client = await self._get_chatto_client()
  596. if client is None:
  597. raise RuntimeError("Chatto client unavailable")
  598. return client
  599. # ------------------------------------------------------------------ #
  600. # Connection
  601. # ------------------------------------------------------------------ #
  602. async def _open_client(
  603. self,
  604. *,
  605. base_url: str,
  606. token: str | None = None,
  607. ) -> ChattoClient:
  608. """Return a connected ``ChattoClient`` using a bearer token.
  609. Raises ValueError when no token is configured — caught upstream as a
  610. normal creation failure, so misconfiguration reads as a logged error
  611. instead of a crash.
  612. """
  613. if not token:
  614. raise ValueError("Chatto: CHATTO_TOKEN not configured")
  615. return ChattoClient(token=token, base_url=base_url)
  616. async def connect(self, *, is_reconnect: bool = False) -> bool:
  617. """Connect to Chatto and start the realtime event stream.
  618. BasePlatformAdapter override
  619. """
  620. logger.info("Chatto: connecting...")
  621. client = await self._get_chatto_client()
  622. if client is None:
  623. self._set_fatal_error(
  624. "connect_failed", "Chatto client not available", retryable=True
  625. )
  626. return False
  627. # Get our first own user info
  628. try:
  629. self.me = await client.me()
  630. except Exception as exc:
  631. logger.error("Chatto: failed to get user info: %s", exc)
  632. self._set_fatal_error(
  633. "chatto_auth_failed",
  634. f"Chatto auth failed: {exc}",
  635. retryable=False,
  636. )
  637. try:
  638. if self._chatto_client is not None:
  639. await self._chatto_client.close()
  640. finally:
  641. self._chatto_client = None
  642. return False
  643. # Announce online presence so the bot appears online in the member list.
  644. # The server treats this as a TTL, so _presence_refresh_loop below has to
  645. # keep re-announcing it — a single call here lapses back to offline.
  646. await self._announce_online()
  647. self._closing = False
  648. # A cursor persisted by a clean shutdown lets this process pick up where
  649. # the previous one stopped, so a restart inside the window answers what
  650. # arrived while it was down instead of starting at the live boundary.
  651. self._realtime_cursor = _restore_realtime_cursor(self.chatto_config)
  652. if self._realtime_cursor:
  653. logger.info("Chatto: resuming the realtime stream from the stored cursor")
  654. # Start background realtime WS event stream loop.
  655. self._ws_task = asyncio.create_task(
  656. self._chattolib_event_loop(),
  657. name="chatto-event-stream",
  658. )
  659. self._presence_task = asyncio.create_task(
  660. self._presence_refresh_loop(),
  661. name="chatto-presence-refresh",
  662. )
  663. self._mark_connected()
  664. logger.info(
  665. "Chatto: connected to %s as '%s' (display_name: '%s' id: '%s')",
  666. self.chatto_config.base_url.value,
  667. self.me.login,
  668. self.me.display_name,
  669. self.me.id,
  670. )
  671. return True
  672. async def _announce_online(self) -> bool:
  673. """Tell the server we are online. Returns whether the call got through.
  674. Logged at warning level on failure: a silently dropped presence call is
  675. indistinguishable from a bot that is simply not running.
  676. """
  677. client = await self._get_chatto_client()
  678. if client is None:
  679. logger.warning(
  680. "Chatto: presence refresh failed, bot may appear offline: no client"
  681. )
  682. return False
  683. try:
  684. await client.set_presence(status=PresenceStatus.ONLINE)
  685. return True
  686. except Exception as exc:
  687. logger.warning(
  688. "Chatto: presence refresh failed, bot may appear offline: %s", exc
  689. )
  690. return False
  691. async def _presence_refresh_loop(self) -> None:
  692. """Re-announce ONLINE until disconnect, since presence expires server-side.
  693. Failures are not fatal — the next tick tries again, so a blip in the
  694. presence endpoint costs at most one interval of visible offline time.
  695. """
  696. while not self._closing:
  697. await self._sleep_interruptible(ChattoConstants.PRESENCE_REFRESH_INTERVAL)
  698. if self._closing:
  699. return
  700. await self._announce_online()
  701. async def disconnect(self) -> None:
  702. """Stop WebSocket, presence refresh, typing tasks, and clear state.
  703. BasePlatformAdapter override
  704. """
  705. # No explicit offline broadcast: chattolib rejects OFFLINE outright
  706. # ("stop refreshing to go offline"), so cancelling the refresh loop
  707. # below is what actually takes the bot offline.
  708. self._closing = True
  709. # Store the stream position before anything is torn down: this is the
  710. # one moment where a restart can resume instead of starting at the live
  711. # boundary, and it costs one small write.
  712. _persist_realtime_cursor(self.chatto_config, self._realtime_cursor)
  713. # Cancel all typing tasks
  714. for chat_id in list(self._typing_tasks.keys()):
  715. await self.stop_typing(chat_id)
  716. if self._ws_task and not self._ws_task.done():
  717. self._ws_task.cancel()
  718. try:
  719. await self._ws_task
  720. except (asyncio.CancelledError, Exception):
  721. logger.debug(
  722. "Chatto: websocket task ended during disconnect", exc_info=True
  723. )
  724. self._ws_task = None
  725. if self._presence_task and not self._presence_task.done():
  726. self._presence_task.cancel()
  727. try:
  728. await self._presence_task
  729. except (asyncio.CancelledError, Exception):
  730. logger.debug(
  731. "Chatto: presence task ended during disconnect", exc_info=True
  732. )
  733. self._presence_task = None
  734. if self._chatto_client:
  735. try:
  736. await self._chatto_client.close()
  737. except Exception:
  738. logger.exception("Chatto: error closing client")
  739. finally:
  740. self._chatto_client = None
  741. logger.info("Chatto: disconnected")
  742. self._mark_disconnected()
  743. async def _seed_room(self, room_id: str) -> None:
  744. """Seed high-water mark from the newest events so a restart doesn't replay history."""
  745. try:
  746. client = await self._get_chatto_client()
  747. if client is None:
  748. logger.debug(
  749. "Chatto: _seed_room aborted - no client available for %s", room_id
  750. )
  751. return
  752. timeline_page = await client.get_room_events(room_id)
  753. for ev in timeline_page.events:
  754. if ev.id:
  755. self._mark_seen(ev.id)
  756. logger.debug(
  757. "Chatto: seeded room %s with %d events",
  758. room_id,
  759. len(timeline_page.events),
  760. )
  761. except Exception as e:
  762. logger.warning("Chatto: get room events failed for %s: %s", room_id, e)
  763. # ------------------------------------------------------------------ #
  764. # Realtime Event List
  765. # ------------------------------------------------------------------ #
  766. def _mark_seen(self, event_id: str) -> None:
  767. # The deque's maxlen evicts the oldest ID — no manual trimming.
  768. self._seen.append(event_id)
  769. def _is_seen(self, event_id: str) -> bool:
  770. return event_id in self._seen
  771. # ------------------------------------------------------------------ #
  772. # WebSocket Realtime Transport
  773. # ------------------------------------------------------------------ #
  774. def _own_handles(self) -> set[str]:
  775. """The handles that address this bot, lowercased for comparison.
  776. Chatto resolves mentions case-insensitively (FDR-006), so ``@Hermes_Bot``
  777. and ``@hermes_bot`` are the same handle everywhere in our gates.
  778. """
  779. if not self.me:
  780. return set()
  781. return {
  782. handle.lower() for handle in (self.me.login, self.me.display_name) if handle
  783. }
  784. def _mention_candidates(self, body: str) -> list[str]:
  785. """Candidate @-handles in a message body, in order of appearance.
  786. Mirrors the Chatto web frontend's extraction (apps/frontend/src/lib/
  787. mentions.ts upstream): candidates come from ``ChattoConstants.
  788. MENTION_RE`` outside code regions. Mentions inside fenced code blocks
  789. and inline code spans do not resolve upstream either, so a ``@bob``
  790. quoted in a snippet must not gate our behaviour.
  791. """
  792. without_fences = re.sub(r"(?s)(```|~~~).*?(\1|$)", " ", body)
  793. without_code = re.sub(r"`[^`\n]*`", " ", without_fences)
  794. return ChattoConstants.MENTION_RE.findall(without_code)
  795. def _mentions_from_body(self, body: str) -> _Mentions:
  796. """Re-derive the addressed parties from message text.
  797. Needed only for edited messages: an edit event carries no resolved
  798. targets, and re-gating on the new text is a promise we make on purpose
  799. (a mention added after posting still reaches the agent) — Chatto itself
  800. would not re-resolve, but it also has no turn for an edit to open. So
  801. this is the best available reading, not the authoritative one, and it
  802. inherits the regex's blind spots: a ``@bob`` inside a URL or blockquote
  803. reads as a mention here. The posted path does not have this problem —
  804. it reads the server's own resolution.
  805. Handles no user holds are not mentions: someone writing *about*
  806. mentioning ("per @-mention", "@nonexistent") is talking to us, and
  807. staying silent on a false positive is worse than answering one.
  808. """
  809. own = self._own_handles()
  810. me = False
  811. someone_else = False
  812. for handle in self._mention_candidates(body):
  813. lowered = handle.lower()
  814. if lowered in ChattoConstants.BROADCAST_MENTIONS or lowered in own:
  815. me = True
  816. elif self._handle_belongs_to_a_user(lowered):
  817. someone_else = True
  818. return _Mentions(me=me, someone_else=someone_else)
  819. def _handle_belongs_to_a_user(self, lowered_handle: str) -> bool:
  820. """Whether ``lowered_handle`` is the login of a real Chatto user.
  821. Resolved strictly from local knowledge — the member cache the adapter
  822. already keeps — never from the admin directory: ``admin_get_member``
  823. needs admin permissions a bot account must not be assumed to hold,
  824. and in the field its ``permission_denied`` failed every lookup, which
  825. fail-open let the bot answer every @colleague message. The cache is
  826. filled by the admission pipeline itself (room rosters are fetched for
  827. every channel turn), presence patching and the edited-message path.
  828. """
  829. return any(
  830. user is not None and (user.login or "").lower() == lowered_handle
  831. for user in self._user_cache.values()
  832. )
  833. def _check_auth(self, user: User) -> bool:
  834. """Whether this Chatto user may talk to the agent.
  835. Deliberately our own gate instead of the gateway's authz_mixin: its
  836. group allowlists key on chat_type and per-platform env vars
  837. (``{PLATFORM}_GROUP_ALLOWED_USERS``), none of which fit Chatto's one
  838. flat member directory. ``CHATTO_ALLOWED_USERS`` matches user IDs,
  839. ``CHATTO_ALLOW_ALL_USERS`` overrides it — the gate stays
  840. chat_type-independent by design.
  841. """
  842. if self._is_authorized(user.id):
  843. return True
  844. logger.warning(
  845. "Chatto: rejecting message from unauthorized user '%s' (%s)",
  846. user.login,
  847. user.id,
  848. )
  849. return False
  850. # ------------------------------------------------------------------ #
  851. # Room management over DM (/join, /leave)
  852. # ------------------------------------------------------------------ #
  853. _DM_COMMANDS = ("/join", "/leave")
  854. async def _handle_dm_command(self, room_id: str, body: str) -> bool:
  855. """Run a ``/join`` or ``/leave`` admin command sent as a direct message.
  856. Returns True when ``body`` is one of the commands — whether it
  857. succeeded or not — so the caller keeps it out of the agent pipeline.
  858. Membership lives on the Chatto server: a joined room reappears in
  859. every future ``list_rooms()`` and therefore survives restarts.
  860. """
  861. verb, _, argument = body.strip().partition(" ")
  862. if verb.lower() not in self._DM_COMMANDS:
  863. return False
  864. client = await self._get_chatto_client()
  865. if client is None:
  866. await self.send(chat_id=room_id, content="Chatto client is not connected.")
  867. return True
  868. argument = argument.strip()
  869. if not argument:
  870. await self.send(
  871. chat_id=room_id,
  872. content="Usage: /join <room-id or #name> | /leave <room-id or #name>",
  873. )
  874. return True
  875. error, target = await self._resolve_room_target(client, argument)
  876. if error or target is None:
  877. await self.send(chat_id=room_id, content=error or "Room lookup failed.")
  878. return True
  879. if verb.lower() == "/join":
  880. reply = await self._run_join(client, target)
  881. else:
  882. reply = await self._run_leave(client, target)
  883. await self.send(chat_id=room_id, content=reply)
  884. return True
  885. async def _resolve_room_target(
  886. self,
  887. client: ChattoClient,
  888. argument: str,
  889. ) -> tuple[str | None, RoomWithViewerState | None]:
  890. """Resolve a ``/join`` or ``/leave`` argument to a room.
  891. ``#name`` is looked up case-insensitively in a fresh directory scan
  892. (which also refreshes our name/kind caches); anything else is treated
  893. as a room ID and verified via GetRoom. An ambiguous name comes back as
  894. an error naming the candidates, so the admin can retry with an ID.
  895. """
  896. if not argument.startswith("#"):
  897. state = await client.get_room(argument)
  898. if state is None or state.room is None:
  899. return f"No room with ID '{argument}'.", None
  900. return None, state
  901. wanted = argument[1:].strip().casefold()
  902. # (state, room) pairs: a listed match's room is already narrowed here,
  903. # so the candidate listing below needs no fresh Optional dance.
  904. matches: list[tuple[RoomWithViewerState, Room]] = []
  905. for state in await client.list_rooms() or []:
  906. room_obj = state.room if state else None
  907. if room_obj and (room_obj.name or "").strip().casefold() == wanted:
  908. matches.append((state, room_obj))
  909. self._room_names[room_obj.id] = room_obj.name
  910. self._room_kinds[room_obj.id] = room_obj.kind
  911. if not matches:
  912. return f"No room named '{argument}'.", None
  913. if len(matches) > 1:
  914. candidates = "\n".join(f"• {room.name} ({room.id})" for _, room in matches)
  915. return (
  916. f"Several rooms are named '{argument}' — pick one by ID:\n{candidates}"
  917. ), None
  918. return None, matches[0][0]
  919. async def _run_join(self, client: ChattoClient, state: RoomWithViewerState) -> str:
  920. """Join a room via RoomService/JoinRoom and track it as joined.
  921. An account that already holds membership (invited natively in Chatto)
  922. needs no JoinRoom call — it only gets seeded and added to the list.
  923. Channel-kind rooms additionally report their mention-list status,
  924. because a channel on neither list stays silent and this reply is
  925. where users copy the room ID from (see ``_room_join_hint``).
  926. """
  927. room_obj = state.room
  928. if room_obj is None:
  929. # Unreachable via _resolve_room_target: both of its paths only
  930. # return states whose room they already inspected.
  931. return "Chatto returned an empty room record — try again."
  932. label = f"'{room_obj.name}' ({room_obj.id})"
  933. joined_room = room_obj
  934. if not state.viewer_state.is_member:
  935. try:
  936. joined_room = await client.join_room(room_obj.id) or room_obj
  937. except ChattoError as exc:
  938. logger.warning("Chatto: /join failed for %s (%s)", room_obj.id, exc)
  939. return f"Could not join {label}: {exc}"
  940. self._room_names[joined_room.id] = joined_room.name
  941. self._room_kinds[joined_room.id] = joined_room.kind
  942. if joined_room.id not in self._joined_room_ids:
  943. # Same rule as _refresh_rooms: silent rooms are joined read-only
  944. # — seeding history nothing will ever answer would be waste.
  945. if self._answers_in_room(joined_room.id):
  946. await self._seed_room(joined_room.id)
  947. else:
  948. logger.info(
  949. "Chatto: %s is on neither mention list - joined read-only",
  950. label,
  951. )
  952. self._joined_room_ids.append(joined_room.id)
  953. if room_obj.kind != RoomKind.DM:
  954. head = (
  955. f"Already a member of {label}."
  956. if state.viewer_state.is_member
  957. else f"Joined {label}."
  958. )
  959. return f"{head}\n{self._room_join_hint(room_obj.id)}"
  960. if state.viewer_state.is_member:
  961. return f"Already a member of {label} — listening there."
  962. return f"Joined {label}."
  963. async def _run_leave(self, client: ChattoClient, state: RoomWithViewerState) -> str:
  964. """Leave a room via RoomService/LeaveRoom and drop it from the joined list.
  965. Two rooms are refused: a DM conversation cannot be left, and leaving
  966. the configured home channel would silently break cron/notification
  967. delivery, which posts there through the standalone sender.
  968. """
  969. room_obj = state.room
  970. if room_obj is None:
  971. # Same invariant as _run_join: _resolve_room_target pre-inspects.
  972. return "Chatto returned an empty room record — try again."
  973. label = f"'{room_obj.name}' ({room_obj.id})"
  974. if room_obj.kind == RoomKind.DM:
  975. return "Direct messages cannot be left."
  976. home_id = (self.chatto_config.home_channel.value or "").strip()
  977. if home_id == room_obj.id:
  978. return (
  979. f"{label} is the configured home channel "
  980. "(CHATTO_HOME_CHANNEL); leaving it would break cron and "
  981. "notification delivery. Point CHATTO_HOME_CHANNEL elsewhere first."
  982. )
  983. try:
  984. left = await client.leave_room(room_obj.id)
  985. except ChattoError as exc:
  986. logger.warning("Chatto: /leave failed for %s (%s)", room_obj.id, exc)
  987. return f"Could not leave {label}: {exc}"
  988. if not left:
  989. return f"Chatto refused to leave {label}."
  990. if room_obj.id in self._joined_room_ids:
  991. self._joined_room_ids.remove(room_obj.id)
  992. # We are no longer part of this audience; keep no roster for it.
  993. self._evict_roster(room_obj.id)
  994. return f"Left {label}."
  995. def _room_join_hint(self, room_id: str) -> str:
  996. """The mention-list status appended to a channel-kind /join reply.
  997. Users do not know their room IDs by heart — this reply is where they
  998. copy them from, so a silent channel names both env vars verbatim,
  999. ready to paste into ~/.hermes/.env. Configuration resolves once at
  1000. gateway startup, hence the restart note.
  1001. """
  1002. policy = self._room_policy(room_id)
  1003. if policy == RoomPolicy.REQUIRE_MENTION:
  1004. return (
  1005. "This channel answers only @mentions "
  1006. "(listed in CHATTO_REQUIRE_MENTION_ROOMS)."
  1007. )
  1008. if policy == RoomPolicy.OPEN:
  1009. return (
  1010. "This channel answers every message "
  1011. "(listed in CHATTO_OPTIONAL_MENTION_ROOMS)."
  1012. )
  1013. return (
  1014. "This channel stays silent until you list its ID in"
  1015. " ~/.hermes/.env (then restart the gateway):\n"
  1016. f" CHATTO_REQUIRE_MENTION_ROOMS={room_id} <- answer only @mentions\n"
  1017. f" CHATTO_OPTIONAL_MENTION_ROOMS={room_id} <- answer every message"
  1018. )
  1019. # ------------------------------------------------------------------ #
  1020. # Inbound attachments
  1021. # ------------------------------------------------------------------ #
  1022. async def _download_attachment_bytes(self, url: str) -> bytes:
  1023. """Download an attachment, refusing to buffer more than the gateway cap.
  1024. The Content-Length header is checked first so an oversized asset is
  1025. rejected before a single chunk is read; the running total is re-checked
  1026. as chunks arrive, because a missing or lying header must not smuggle an
  1027. unbounded body past the cap.
  1028. """
  1029. max_bytes = get_inbound_media_max_bytes()
  1030. chunks: list[bytes] = []
  1031. total = 0
  1032. async with (
  1033. httpx.AsyncClient(
  1034. timeout=ChattoConstants.HTTP_TIMEOUT,
  1035. follow_redirects=True,
  1036. ) as http,
  1037. http.stream("GET", url) as resp,
  1038. ):
  1039. resp.raise_for_status()
  1040. declared = resp.headers.get("content-length")
  1041. if declared:
  1042. try:
  1043. declared_size = int(declared)
  1044. except ValueError:
  1045. logger.debug("Chatto: ignoring invalid Content-Length %r", declared)
  1046. else:
  1047. validate_inbound_media_size(
  1048. declared_size,
  1049. media_type="attachment",
  1050. max_bytes=max_bytes,
  1051. )
  1052. async for chunk in resp.aiter_bytes():
  1053. total += len(chunk)
  1054. validate_inbound_media_size(
  1055. total,
  1056. media_type="attachment",
  1057. max_bytes=max_bytes,
  1058. )
  1059. chunks.append(chunk)
  1060. return b"".join(chunks)
  1061. async def _cache_attachments(
  1062. self,
  1063. attachments: list[MessageAttachment],
  1064. ) -> tuple[list[str], list[str], list[str]]:
  1065. """Download message attachments into the gateway media cache.
  1066. Returns ``(media_urls, media_types, media_kinds)`` — the paths are
  1067. agent-visible cache paths, exactly what ``cache_media_bytes`` yields for
  1068. every other platform. A failing attachment is logged and skipped: the
  1069. message itself still reaches the agent.
  1070. """
  1071. media_urls: list[str] = []
  1072. media_types: list[str] = []
  1073. media_kinds: list[str] = []
  1074. for att in attachments:
  1075. url = att.asset_url.url if att.asset_url else ""
  1076. filename = att.filename
  1077. content_type = att.content_type
  1078. if not url:
  1079. # Videos are announced before transcoding finishes, so the
  1080. # signed URL can legitimately be missing on arrival.
  1081. logger.debug(
  1082. "Chatto: attachment '%s' has no asset URL yet, skipping",
  1083. filename,
  1084. )
  1085. continue
  1086. try:
  1087. data = await self._download_attachment_bytes(url)
  1088. cached = cache_media_bytes(
  1089. data,
  1090. filename=filename,
  1091. mime_type=content_type,
  1092. )
  1093. except Exception as e:
  1094. logger.warning(
  1095. "Chatto: failed to cache attachment '%s' (%s): %s",
  1096. filename,
  1097. content_type,
  1098. e,
  1099. )
  1100. continue
  1101. if cached is None:
  1102. logger.warning(
  1103. "Chatto: attachment '%s' (%s) could not be cached, skipping",
  1104. filename,
  1105. content_type,
  1106. )
  1107. continue
  1108. media_urls.append(cached.path)
  1109. media_types.append(cached.media_type)
  1110. media_kinds.append(cached.kind)
  1111. return media_urls, media_types, media_kinds
  1112. @staticmethod
  1113. def _message_type_for_media_kinds(media_kinds: list[str]) -> MessageType:
  1114. """Pick the MessageType for a set of cached attachment kinds."""
  1115. if "document" in media_kinds:
  1116. return MessageType.DOCUMENT
  1117. if "image" in media_kinds:
  1118. return MessageType.PHOTO
  1119. if "video" in media_kinds:
  1120. return MessageType.VIDEO
  1121. if "audio" in media_kinds:
  1122. return MessageType.AUDIO
  1123. return MessageType.TEXT
  1124. async def _room_kind_for(
  1125. self, client: ChattoClient, room_id: str
  1126. ) -> RoomKind | None:
  1127. """The room's kind, from cache or a fresh GetRoom lookup.
  1128. Returns ``None`` when the room cannot be resolved — the caller treats
  1129. that as "not dispatchable" rather than guessing a kind.
  1130. """
  1131. kind = self._room_kinds.get(room_id)
  1132. if kind is not None:
  1133. return kind
  1134. room_viewer_state = await client.get_room(room_id)
  1135. if room_viewer_state is None or room_viewer_state.room is None:
  1136. return None
  1137. kind = room_viewer_state.room.kind or RoomKind.UNSPECIFIED
  1138. self._room_kinds[room_id] = kind
  1139. return kind
  1140. def _room_policy(self, room_id: str) -> RoomPolicy:
  1141. """Which of the mention lists a channel-kind room is on.
  1142. The two lists are mutually exclusive (enforced by
  1143. ``hermes_validate_config``), so membership decides: optional beats
  1144. require in the face of contradictory runtime config, and a room on
  1145. neither list stays silent. Callers that only need yes/no ask
  1146. ``_answers_in_room``, which delegates to the shared ``_room_answers_in``
  1147. the send tools use.
  1148. """
  1149. if room_id in self.chatto_config.optional_mention_rooms.value:
  1150. return RoomPolicy.OPEN
  1151. if room_id in self.chatto_config.require_mention_rooms.value:
  1152. return RoomPolicy.REQUIRE_MENTION
  1153. return RoomPolicy.SILENT
  1154. def _answers_in_room(self, room_id: str) -> bool:
  1155. """Whether inbound messages from this room reach the agent pipeline.
  1156. Delegates to the shared ``_room_answers_in``, which the send tools gate
  1157. on too — see there for the rule and why it exists only once. A room
  1158. that answers is never read-only (its policy is not SILENT), and an
  1159. unknown kind falls through to the mention lists rather than being
  1160. mistaken for a DM.
  1161. """
  1162. return _room_answers_in(
  1163. self.chatto_config, room_id, self._room_kinds.get(room_id)
  1164. )
  1165. def toolsets_for_source(self, source: SessionSource) -> list[str] | None:
  1166. """Restrict which toolsets a turn in a listed room may use.
  1167. ``CHATTO_ROOM_TOOLSETS`` holds ``room_id:toolset`` entries. A room with
  1168. at least one entry *replaces* the platform's toolset list for turns in
  1169. that room — Hermes' contract for this hook — so the entries have to name
  1170. every toolset the room should keep. A room without an entry returns
  1171. None, leaving ``platform_toolsets`` in charge. DM rooms are looked up
  1172. like any other room, and a thread turn carries its room's ID, so a room
  1173. entry covers its threads too.
  1174. BasePlatformAdapter override
  1175. """
  1176. room_id = str(getattr(source, "chat_id", "") or "")
  1177. if not room_id:
  1178. return None
  1179. by_room, _malformed = _parse_room_toolsets(
  1180. self.chatto_config.room_toolsets.value
  1181. )
  1182. return by_room.get(room_id) or None
  1183. async def _room_roster_context(
  1184. self, client: ChattoClient, room_id: str
  1185. ) -> _RoomRoster | None:
  1186. """Fetch the room's member roster as a fresh projection.
  1187. The agent only ever sees its prompt: without this block it cannot know
  1188. who else is in a channel, because unaddressed messages are dropped by
  1189. mention gating long before they could teach it a name. Fetched users go
  1190. into _user_cache so presence patching and mention resolution share one
  1191. store. Returns ``None`` on failure or when nothing renderable remains —
  1192. best-effort by design, a directory hiccup must never cost the turn.
  1193. """
  1194. try:
  1195. # list_room_members() in 0.5.0b4 returns member IDs only;
  1196. # batch_get_room_members() hydrates them into DirectoryMember.
  1197. member_id_list, page = await client.list_room_members(
  1198. room_id, limit=ChattoConstants.ROSTER_MEMBER_LIMIT
  1199. )
  1200. if not member_id_list:
  1201. return None
  1202. members = await client.batch_get_room_members(room_id, member_id_list)
  1203. except Exception:
  1204. logger.debug(
  1205. "Chatto: could not list members of room %s", room_id, exc_info=True
  1206. )
  1207. return None
  1208. logger.debug(
  1209. "Chatto: roster for room %s: %d members fetched, total_count=%d",
  1210. room_id,
  1211. len(members),
  1212. page.total_count,
  1213. )
  1214. member_ids: set[str] = set()
  1215. users: list[User] = []
  1216. for member in members:
  1217. user = member.user
  1218. if user is None or user.deleted:
  1219. continue
  1220. member_ids.add(user.id)
  1221. self._user_cache[user.id] = user
  1222. users.append(user)
  1223. entries = [
  1224. entry for entry in (self._roster_entry(user) for user in users) if entry
  1225. ]
  1226. if not entries:
  1227. logger.debug(
  1228. "Chatto: roster for room %s empty after filtering "
  1229. "(%d fetched, all deleted/self/without user)",
  1230. room_id,
  1231. len(members),
  1232. )
  1233. return None
  1234. logger.debug("Chatto: roster for room %s: %d entries", room_id, len(entries))
  1235. unfetched = max(0, page.total_count - len(members))
  1236. return _RoomRoster(member_ids=member_ids, unfetched=unfetched)
  1237. def _roster_entry(self, user: User) -> str:
  1238. """One roster entry, e.g. ``@bob (Bob Example, online)``.
  1239. Skips deleted users and our own account (the agent knows itself);
  1240. display name and presence are optional parts of the parenthetical.
  1241. """
  1242. if user.deleted:
  1243. return ""
  1244. if self.me is not None and user.id == self.me.id:
  1245. return ""
  1246. entry = f"@{user.login}"
  1247. details = [
  1248. part
  1249. for part in (
  1250. user.display_name,
  1251. _PRESENCE_LABELS.get(user.presence_status, ""),
  1252. )
  1253. if part
  1254. ]
  1255. if details:
  1256. entry += f" ({', '.join(details)})"
  1257. return entry
  1258. def _roster_line(self, room_id: str) -> str:
  1259. """Render the room's roster line from its cached projection."""
  1260. roster = self._rosters.get(room_id)
  1261. if roster is None:
  1262. return ""
  1263. users = [self._user_cache.get(member_id) for member_id in roster.member_ids]
  1264. entries = [
  1265. entry
  1266. for entry in (
  1267. self._roster_entry(user) for user in users if user is not None
  1268. )
  1269. if entry
  1270. ]
  1271. more = f" … and {roster.unfetched} more" if roster.unfetched else ""
  1272. return ", ".join(entries) + more
  1273. def _announce_roster(self, room_id: str, roster: _RoomRoster) -> None:
  1274. """Record a freshly fetched roster as the room's projection.
  1275. Bounded like _dispatched_ids: appending past the cap drops the oldest
  1276. room's projection alongside its deque entry.
  1277. """
  1278. if room_id not in self._roster_announced:
  1279. oldest = (
  1280. self._roster_announced.popleft()
  1281. if len(self._roster_announced) == self._roster_announced.maxlen
  1282. else None
  1283. )
  1284. self._roster_announced.append(room_id)
  1285. if oldest is not None:
  1286. self._rosters.pop(oldest, None)
  1287. self._rosters[room_id] = roster
  1288. async def _thread_context_for(
  1289. self,
  1290. client: ChattoClient,
  1291. *,
  1292. room_id: str,
  1293. thread_root_event_id: str,
  1294. current_message_id: str,
  1295. source: Any,
  1296. ) -> str:
  1297. """The thread's earlier messages, delivered once per thread.
  1298. A Hermes thread session is keyed on the thread root and starts empty,
  1299. so the turn that opens a thread is answered with nothing but the reply
  1300. itself. The reported case: a cron job posts into a channel, its echo
  1301. is filtered as a self-event and never reaches a session, and a human
  1302. replies — rooting a thread whose root the agent has never seen. Without
  1303. this block the agent answers a message it cannot read.
  1304. Gated on ``thread_root_event_id``, not on the resolved ``thread_id``:
  1305. a fresh room message is anchored to itself (see _admit_and_build), and
  1306. that thread does not exist yet, so there is nothing to fetch. DMs never
  1307. thread and never seed.
  1308. The cursor is kept twice: in memory for this process, and on the session
  1309. for the next one. Only the second one survives a restart, and it is not
  1310. a redundant copy — a session's frontier is *not* derivable from the
  1311. thread's messages, because the bot's own messages belong to no session
  1312. while the previous turn's trigger looks exactly like one the session
  1313. holds. Rebuilding a partition from the fetch therefore lands on the
  1314. wrong message and skips the very output this exists to deliver.
  1315. Best-effort by design, like the roster: a failed lookup leaves the
  1316. thread unseeded so the next turn retries.
  1317. """
  1318. if not thread_root_event_id:
  1319. return ""
  1320. session_key = self._session_key_for(source)
  1321. # The cursor is resolved *before* the fetch, because it anchors it. That
  1322. # is what makes a long thread work: an anchored fetch returns the oldest
  1323. # messages past the anchor rather than the newest ones in the thread, so
  1324. # a thread that grew past the fetch limit still answers with what the
  1325. # session is missing instead of leaving the cursor outside the window.
  1326. cursor = self._seeded_threads.get(thread_root_event_id, _UNSEEDED)
  1327. if cursor is _UNSEEDED:
  1328. resumed = self._persisted_cursor(session_key, _UNSEEDED)
  1329. if isinstance(resumed, str):
  1330. # A cursor an earlier process left behind: it outlived that
  1331. # process with the session, so the thread goes on delivering its
  1332. # tail.
  1333. self._record_thread_seeded(thread_root_event_id, resumed)
  1334. cursor = resumed
  1335. anchored = cursor if isinstance(cursor, str) else ""
  1336. history = await self._thread_history(
  1337. client,
  1338. room_id=room_id,
  1339. thread_root_event_id=thread_root_event_id,
  1340. after=anchored,
  1341. )
  1342. if history is None and anchored:
  1343. # The anchored fetch failed, most plausibly because the cursor
  1344. # message has gone away (deleted, retracted). Fail open as a session
  1345. # without a cursor — the blind pass below re-delivers own messages
  1346. # and writes a fresh cursor — rather than re-reading a cursor that
  1347. # will fail every turn from now on.
  1348. logger.debug(
  1349. "Chatto: anchored thread history for %s failed at cursor %s; "
  1350. "refetching unanchored",
  1351. thread_root_event_id,
  1352. anchored,
  1353. )
  1354. self._seeded_threads.pop(thread_root_event_id, None)
  1355. anchored = ""
  1356. history = await self._thread_history(
  1357. client,
  1358. room_id=room_id,
  1359. thread_root_event_id=thread_root_event_id,
  1360. after="",
  1361. )
  1362. if history is None:
  1363. logger.debug(
  1364. "Chatto: dispatching into thread %s without history (lookup failed)",
  1365. thread_root_event_id,
  1366. )
  1367. return ""
  1368. if anchored:
  1369. # Everything the anchored page holds landed after the cursor, so it is
  1370. # the tail — no cutting point to find inside the window.
  1371. return self._seed_thread_tail(
  1372. thread_root_event_id,
  1373. history,
  1374. current_message_id,
  1375. session_key=session_key,
  1376. )
  1377. if self._thread_session_exists(source):
  1378. # A session that survived a restart but carries no cursor: a thread
  1379. # whose first turn had nothing to seed wrote no cursor (there was no
  1380. # session to write it to) and never got a second turn to retry on.
  1381. #
  1382. # Its frontier is not derivable from the message list, so this
  1383. # delivers the one thing that is provably at risk — the bot's own
  1384. # messages, which belong to no session — and leaves the people's
  1385. # messages alone, since those became turns. Duplicating the former
  1386. # is possible (a first seed that did carry them, same lost second
  1387. # turn) and is bounded to that one block; losing it is the common
  1388. # case and permanent. The cursor is written either way, so the thread
  1389. # is blind once, not for the rest of its life.
  1390. own = [m for m in history if m.id != current_message_id and self._is_own(m)]
  1391. logger.debug(
  1392. "Chatto: thread %s has a session without a seed cursor; "
  1393. "re-delivering %d own message(s)",
  1394. thread_root_event_id,
  1395. len(own),
  1396. )
  1397. established = self._seeded_through(history, own, current_message_id)
  1398. if established is not None:
  1399. self._record_thread_seeded(thread_root_event_id, established)
  1400. self._persist_thread_cursor(
  1401. session_key, thread_root_event_id, established
  1402. )
  1403. return self._thread_context_block(own)
  1404. # First turn into this thread: the session starts empty, so hand it the
  1405. # whole thread before the reply.
  1406. earlier = [m for m in history if m.id != current_message_id]
  1407. self._remember_thread_cursor(
  1408. session_key,
  1409. thread_root_event_id,
  1410. self._seeded_through(history, earlier, current_message_id),
  1411. delivered=earlier,
  1412. )
  1413. return self._thread_context_block(earlier)
  1414. @staticmethod
  1415. def _seeded_through(
  1416. history: list[Message], delivered: list[Message], current_message_id: str
  1417. ) -> str:
  1418. """How far this turn's session now reaches.
  1419. The trigger is excluded from the *block* — it is the message text — but
  1420. the session holds it, so the cursor passes it too. Leaving it behind
  1421. would hand the next turn the previous question a second time, as an
  1422. `[Earlier in this thread]` line beside the conversation that already
  1423. contains it.
  1424. The trigger is also the fallback when the fetched window does not list
  1425. it: the session holds this turn's own message either way. That is safe
  1426. because the fetch is anchored *at* the cursor rather than sliced around
  1427. it — a cursor names a boundary, not a position inside the window, so one
  1428. the window does not contain is still a usable cursor. That is what lets
  1429. this be a plain ``str`` instead of "maybe unknowable".
  1430. """
  1431. if any(m.id == current_message_id for m in history):
  1432. return current_message_id
  1433. return delivered[-1].id if delivered else current_message_id
  1434. def _seed_thread_tail(
  1435. self,
  1436. thread_root_event_id: str,
  1437. history: list[Message],
  1438. current_message_id: str,
  1439. *,
  1440. session_key: str,
  1441. ) -> str:
  1442. """Deliver what landed in an already-seeded thread since the cursor.
  1443. This is the agent's own output the session never saw: a message it posted
  1444. into the thread through a tool, whose realtime echo is filtered as a
  1445. self-event. Without it the next human message in that thread is answered
  1446. by an agent that does not know what it said.
  1447. ``history`` is anchored at the cursor, so every message in it is by
  1448. definition one the session does not hold — there is no cut point to find
  1449. inside the window, and no case where the window cannot answer.
  1450. """
  1451. unseen = [m for m in history if m.id != current_message_id]
  1452. # The cursor moves even when the trigger was all that arrived: it is in
  1453. # the session, and a cursor left behind it would hand the next turn the
  1454. # previous question a second time.
  1455. self._remember_thread_cursor(
  1456. session_key,
  1457. thread_root_event_id,
  1458. self._seeded_through(history, unseen, current_message_id),
  1459. delivered=unseen,
  1460. )
  1461. return self._thread_context_block(unseen)
  1462. async def _thread_history(
  1463. self,
  1464. client: ChattoClient,
  1465. *,
  1466. room_id: str,
  1467. thread_root_event_id: str,
  1468. after: str = "",
  1469. ) -> list[Message] | None:
  1470. """Fetch a thread's messages oldest-first, or ``None`` if the lookup failed.
  1471. ``after`` anchors the page at a cursor instead of at the thread's start,
  1472. and that is the whole point: the anchored fetch returns the *oldest*
  1473. messages past that cursor rather than the newest ones in the thread. A
  1474. thread that has grown past the fetch limit therefore still answers with
  1475. exactly what the session is missing, where an unanchored page would have
  1476. left the cursor outside the window and the turn with nothing to deliver.
  1477. The anchor is a ``GetThreadEventsAround`` call, not ``GetThreadEvents``
  1478. with ``after=``: the server's ``after``/``before`` take opaque cursor
  1479. tokens it seals and hands out with a page — an event ID, which is what
  1480. our cursor is, is rejected with ``invalid cursor format``. Around takes
  1481. an event ID directly; the request asks for nearly twice the delivery
  1482. limit because the window is centred on the anchor, so the half after it
  1483. holds up to ``THREAD_CONTEXT_MESSAGE_LIMIT`` messages.
  1484. Two things follow from the anchor, both from the server's own handler,
  1485. either way: the root is not part of an anchored page, and it is not
  1486. needed — a cursor means everything up to and including it is already in
  1487. the session. So the extra root fetch is skipped, which is one RPC fewer
  1488. per turn than the unanchored path.
  1489. When the gap is larger than the limit the page holds only its oldest
  1490. slice and the cursor stops at the last message delivered; the next turn
  1491. continues from there. A backlog drains over turns rather than in one, and
  1492. nothing is skipped either way.
  1493. A failed lookup returns ``None``; the caller treats a failure at an
  1494. anchored cursor as a stale cursor and falls back to the unanchored path.
  1495. """
  1496. window_limit = 2 * ChattoConstants.THREAD_CONTEXT_MESSAGE_LIMIT + 1
  1497. try:
  1498. if after:
  1499. page, target_index = await client.get_thread_events_around(
  1500. room_id,
  1501. thread_root_event_id,
  1502. after,
  1503. limit=window_limit,
  1504. )
  1505. # target_index is the anchor's position + 1 (0 when the anchor
  1506. # is the root), so everything from there on is strictly past
  1507. # the cursor. The root-as-anchor case can return more than the
  1508. # delivery limit holds, so slice like the anchored-after shape
  1509. # does: no more than THREAD_CONTEXT_MESSAGE_LIMIT per turn.
  1510. events = page.events[
  1511. target_index : target_index
  1512. + ChattoConstants.THREAD_CONTEXT_MESSAGE_LIMIT
  1513. ]
  1514. else:
  1515. page = await client.get_thread_events(
  1516. room_id,
  1517. thread_root_event_id,
  1518. limit=ChattoConstants.THREAD_CONTEXT_MESSAGE_LIMIT,
  1519. )
  1520. events = page.events
  1521. except Exception:
  1522. logger.debug(
  1523. "Chatto: get thread events failed for %s/%s",
  1524. room_id,
  1525. thread_root_event_id,
  1526. exc_info=True,
  1527. )
  1528. return None
  1529. messages = [ev.message for ev in events if ev.message is not None]
  1530. if not after and not any(m.id == thread_root_event_id for m in messages):
  1531. # The thread root is fetched explicitly when the unanchored timeline
  1532. # omits it: a cron message is a plain room message until someone
  1533. # replies to it, and whether the server lists it back as the thread's
  1534. # first event is not something the client signature promises.
  1535. messages = (
  1536. await self._thread_root_message(
  1537. client, room_id=room_id, thread_root_event_id=thread_root_event_id
  1538. )
  1539. + messages
  1540. )
  1541. return _oldest_first(messages)
  1542. async def _thread_root_message(
  1543. self, client: ChattoClient, *, room_id: str, thread_root_event_id: str
  1544. ) -> list[Message]:
  1545. """The thread root as a list, so a miss concatenates cleanly.
  1546. Best-effort: an unknown root costs nothing but the missing root line.
  1547. """
  1548. try:
  1549. root = await client.get_message(
  1550. room_id=room_id, event_id=thread_root_event_id
  1551. )
  1552. except Exception:
  1553. logger.debug(
  1554. "Chatto: get thread root failed for %s/%s",
  1555. room_id,
  1556. thread_root_event_id,
  1557. exc_info=True,
  1558. )
  1559. return []
  1560. return [root] if root is not None and not root.deleted_at else []
  1561. def _remember_thread_cursor(
  1562. self,
  1563. session_key: str,
  1564. thread_root_event_id: str,
  1565. cursor: str,
  1566. *,
  1567. delivered: list[Message],
  1568. ) -> None:
  1569. """Record how far a thread's session reaches, and mirror it if it must.
  1570. The in-memory map is authoritative while this process lives. The session
  1571. copy exists for one thing — a restart, where the map is empty and the
  1572. session survived — and it is written when either of two things holds:
  1573. - the turn put the bot's own output into the session. That is the one
  1574. thing a later turn cannot recover from the message list, so it is the
  1575. one cursor that has to outlive us. Every other turn's frontier is the
  1576. newest message that is not ours, and a restart that loses such a cursor
  1577. delivers nothing, which is right because that turn delivered nothing
  1578. worth losing.
  1579. - an earlier turn's cursor is still waiting for a session to land on. A
  1580. thread's first turn has none — and its seed is exactly the case above,
  1581. a cron message delivered to a session that did not exist yet. By the
  1582. second turn the session exists.
  1583. Writing unconditionally would cost a full routing-table rewrite per turn
  1584. (see _persist_thread_cursor) to save a case that costs nothing.
  1585. """
  1586. self._record_thread_seeded(thread_root_event_id, cursor)
  1587. unrecoverable = any(self._is_own(message) for message in delivered)
  1588. if unrecoverable or thread_root_event_id in self._unpersisted_threads:
  1589. self._persist_thread_cursor(session_key, thread_root_event_id, cursor)
  1590. def _persist_thread_cursor(
  1591. self, session_key: str, thread_root_event_id: str, cursor: str
  1592. ) -> None:
  1593. """Mirror the cursor onto the session, remembering a refusal.
  1594. ``set_session_metadata`` rewrites the whole routing index on every call,
  1595. which is why the caller restricts this to the turns that need it. A
  1596. refusal costs nothing here — the in-memory cursor carries the thread
  1597. until the next process — so it is remembered and retried on the next turn
  1598. rather than logged away, because a cursor that never lands is exactly the
  1599. cursor a restart needs.
  1600. """
  1601. store = getattr(self, "_session_store", None)
  1602. writer = getattr(store, "set_session_metadata", None)
  1603. if not callable(writer):
  1604. return
  1605. try:
  1606. landed = bool(writer(session_key, _CURSOR_META_KEY, cursor))
  1607. except Exception:
  1608. logger.debug(
  1609. "Chatto: persisting the seed cursor for %s failed",
  1610. session_key,
  1611. exc_info=True,
  1612. )
  1613. return
  1614. if landed:
  1615. self._unpersisted_threads.discard(thread_root_event_id)
  1616. else:
  1617. # No session under that key yet, which is the expected shape of a
  1618. # thread's first turn.
  1619. self._unpersisted_threads.add(thread_root_event_id)
  1620. def _persisted_cursor(self, session_key: str, default: _T) -> str | _T:
  1621. """The cursor a previous process left on this session, or ``default``.
  1622. Best-effort bookkeeping, so every failure answers ``default``: a missing
  1623. store, a store without metadata support, a rejected read. That degrades
  1624. to "this thread's frontier is unknown", never to a failed turn.
  1625. """
  1626. store = getattr(self, "_session_store", None)
  1627. reader = getattr(store, "get_session_metadata", None)
  1628. if not callable(reader):
  1629. return default
  1630. try:
  1631. # Duck-typed: the store is optional and may not carry metadata at
  1632. # all, so its value comes back as whatever it is.
  1633. value: Any = reader(session_key, _CURSOR_META_KEY, None)
  1634. except Exception:
  1635. logger.debug(
  1636. "Chatto: reading the seed cursor for %s failed",
  1637. session_key,
  1638. exc_info=True,
  1639. )
  1640. return default
  1641. return value if value is not None else default
  1642. async def _reply_target(
  1643. self, client: ChattoClient, room_id: str, in_reply_to: str
  1644. ) -> Message | None:
  1645. """The message a reply points at, or ``None`` when it cannot be read.
  1646. Best-effort on purpose: a deleted or unreadable target costs the
  1647. context line, not the turn — an unanswered reply is worse than one
  1648. answered without its quote.
  1649. """
  1650. try:
  1651. target = await client.get_message(room_id=room_id, event_id=in_reply_to)
  1652. except Exception:
  1653. logger.debug(
  1654. "Chatto: reply target %s/%s could not be read",
  1655. room_id,
  1656. in_reply_to,
  1657. exc_info=True,
  1658. )
  1659. return None
  1660. if target is None or target.deleted_at:
  1661. return None
  1662. return target
  1663. def _is_own(self, message: Message) -> bool:
  1664. """Whether this message is one the bot itself posted."""
  1665. return self.me is not None and message.actor_id == self.me.id
  1666. def _record_thread_seeded(
  1667. self, thread_root_event_id: str, last_delivered_id: str
  1668. ) -> None:
  1669. """Note that this thread's session holds its history up to that message.
  1670. Re-inserting keeps the map insertion-ordered, which is what the cap below
  1671. evicts from: the least recently touched thread.
  1672. """
  1673. self._seeded_threads.pop(thread_root_event_id, None)
  1674. self._seeded_threads[thread_root_event_id] = last_delivered_id
  1675. while len(self._seeded_threads) > ChattoConstants.SEEN_CAP:
  1676. self._seeded_threads.pop(next(iter(self._seeded_threads)))
  1677. def _thread_session_exists(self, source: Any) -> bool:
  1678. """Whether the gateway already has a session for this source.
  1679. Kept as the thread path's own name: it answers the same question as
  1680. ``_session_exists`` and delegates to it, so the two paths cannot
  1681. disagree about what "a session exists" means.
  1682. """
  1683. return self._session_exists(source)
  1684. async def _room_context_for(
  1685. self,
  1686. client: ChattoClient,
  1687. *,
  1688. room_id: str,
  1689. trigger: Message,
  1690. source: Any,
  1691. ) -> str:
  1692. """The room's earlier messages, delivered when a room session starts.
  1693. A fresh room message roots its own session (see _admit_and_build), so
  1694. the first turn into a room is answered with nothing but that message —
  1695. even when the room has been talking for hours and even when the bot
  1696. itself posted there. The reported case: a human replies to the bot's
  1697. message without opening a thread, and the agent answers a conversation
  1698. it has never seen.
  1699. The block is bounded by the same caps as the thread seed, and it is
  1700. skipped as soon as the session holds a turn: the goal is to open a room
  1701. conversation with its recent context, not to re-deliver the room's
  1702. history on every message.
  1703. Seeding is anchored on the trigger, not on a per-room cursor: every
  1704. fresh room message opens a new session, so a cursor written for one
  1705. session's frontier would hand the next message's session a block
  1706. written for the first one's. The trigger-anchored fetch makes the
  1707. block "what the room said before this message", which is the question
  1708. a session start actually asks.
  1709. """
  1710. if self._session_exists(source):
  1711. return ""
  1712. try:
  1713. page, _anchor_index = await client.get_room_events_around(
  1714. room_id, trigger.id, limit=ChattoConstants.THREAD_CONTEXT_MESSAGE_LIMIT
  1715. )
  1716. except Exception:
  1717. logger.debug(
  1718. "Chatto: room context lookup failed for %s/%s",
  1719. room_id,
  1720. trigger.id,
  1721. exc_info=True,
  1722. )
  1723. return ""
  1724. messages = [ev.message for ev in page.events if ev.message is not None]
  1725. # The page carries its own user directory, which answers the author
  1726. # names in the block without a member-list call — the same hydration
  1727. # the read tools use for a fetched timeline.
  1728. for user_id, user in page.users_by_id.items():
  1729. self._user_cache.setdefault(user_id, user)
  1730. # ``around`` serves both directions of the trigger, and a session
  1731. # start is answered with the past, not the future: newer messages are
  1732. # not "earlier", and including one would hand the agent a message the
  1733. # room may still be typing around. Age is compared by time rather
  1734. # than by position, because the page's order is display order, not a
  1735. # promise about either side of the anchor.
  1736. trigger_time = trigger.created_at or datetime.min.replace(tzinfo=UTC)
  1737. earlier = [
  1738. m
  1739. for m in _oldest_first(messages)
  1740. if m.id != trigger.id
  1741. and not m.deleted_at
  1742. and (m.created_at or datetime.min.replace(tzinfo=UTC)) <= trigger_time
  1743. ]
  1744. return self._earlier_context_block(earlier, "[Earlier in this room]")
  1745. def _session_exists(self, source: Any) -> bool:
  1746. """Whether the gateway already has a session for this source.
  1747. The gateway's own routing index is the authority on "does this
  1748. conversation already exist" — it is persistent (state.db), so unlike
  1749. the in-memory dedup state it answers correctly after a restart.
  1750. Returns False when no store is wired (tests, or a startup path before
  1751. ``set_session_store`` runs) so we fail open and seed.
  1752. """
  1753. store = getattr(self, "_session_store", None)
  1754. if store is None:
  1755. return False
  1756. return store.peek_session_id(self._session_key_for(source)) is not None
  1757. def _thread_context_block(self, messages: list[Message]) -> str:
  1758. """Render earlier thread messages as the agent's reference block.
  1759. Empty when there is nothing to say — a brand-new thread has no earlier
  1760. messages, and that is not worth a header. Over budget, the oldest
  1761. messages go first: the reply's immediate predecessors are what the
  1762. turn is about.
  1763. """
  1764. return self._earlier_context_block(messages, "[Earlier in this thread]")
  1765. def _earlier_context_block(self, messages: list[Message], header: str) -> str:
  1766. """Render earlier messages as one reference block under ``header``.
  1767. Shared by the thread and the room seed: both answer the same question
  1768. ("what did this conversation say before the trigger"), so both get the
  1769. same rendering, the same caps and the same own/verified labelling — two
  1770. copies of one renderer would drift until a thread and a room disagreed
  1771. about what a quoted message looks like.
  1772. """
  1773. entries: list[str] = []
  1774. for message in messages:
  1775. entry = self._thread_message_entry(message)
  1776. if entry:
  1777. entries.append(entry)
  1778. if not entries:
  1779. return ""
  1780. dropped = 0
  1781. while (
  1782. entries
  1783. and len("\n".join(entries)) > ChattoConstants.THREAD_CONTEXT_CHAR_LIMIT
  1784. ):
  1785. entries.pop(0)
  1786. dropped += 1
  1787. if not entries:
  1788. return ""
  1789. body = "\n".join(entries)
  1790. omitted = f"… {dropped} earlier message(s) omitted\n" if dropped else ""
  1791. return f"{omitted}{header}\n{body}"
  1792. def _thread_message_entry(self, message: Message) -> str:
  1793. """One rendered message for an earlier-context block, or ``""``.
  1794. Attachment-only messages render empty: the block is text context, and
  1795. inventing a placeholder for a picture the agent cannot see here would
  1796. be noise.
  1797. """
  1798. body = (message.body or "").strip()
  1799. if not body:
  1800. return ""
  1801. me = self.me
  1802. user = self._user_cache.get(message.actor_id)
  1803. if me is not None and message.actor_id == me.id:
  1804. # The agent's own output — often a cron message it never saw
  1805. # arrive. Labelling it keeps "I said this" distinguishable from a
  1806. # third party talking.
  1807. return f"{me.login} (this bot): {body}"
  1808. author = f"@{user.login}" if user else f"@{message.actor_id}"
  1809. if not self._is_authorized(message.actor_id):
  1810. # Auth gates the trigger message only; the rest of a shared thread
  1811. # is reference text from people who may not be on the allowlist,
  1812. # so it is marked as unverified rather than presented as trusted
  1813. # input. An unknown author lands here too — fail closed.
  1814. return f"{author} (unverified): {body}"
  1815. return f"{author}: {body}"
  1816. def _is_authorized(self, actor_id: str) -> bool:
  1817. """Whether a Chatto user may talk to the agent, by user ID.
  1818. The allowlist test itself, without the logging _check_auth adds: a
  1819. rendering path asks the same question about someone quoted into the
  1820. prompt, where denial is a normal sight rather than an event worth a
  1821. warning. IDs only — a login entry would pass this intake but fail the
  1822. gateway's fresh-role recheck, which matches raw IDs, and the message
  1823. would be silently dropped between the two gates.
  1824. """
  1825. if self.chatto_config.allow_all_users.value:
  1826. return True
  1827. return actor_id in self.chatto_config.allowed_users.value
  1828. def _interactive_prompt_expired(self, prompt: Any) -> bool:
  1829. """Whether an interactive prompt has passed its expiry deadline."""
  1830. try:
  1831. return time.monotonic() > float(prompt.expires_at)
  1832. except (TypeError, ValueError, AttributeError):
  1833. return False
  1834. async def _try_handle_interactive_reaction(
  1835. self,
  1836. room_id: Any,
  1837. message_id: Any,
  1838. emoji: Any,
  1839. actor_id: Any,
  1840. ) -> bool:
  1841. """Route a reaction on a tracked interactive prompt.
  1842. Returns True when the message is a tracked prompt (consumed), False
  1843. otherwise so the caller forwards normally.
  1844. """
  1845. if not room_id or not message_id or not emoji or not actor_id:
  1846. return False
  1847. room_id = str(room_id)
  1848. message_id = str(message_id)
  1849. # Keep the raw form for the log: the server delivers shortcode names
  1850. # ("heavy_check_mark"), and a decoded-only log would hide exactly the
  1851. # name a client actually picked.
  1852. raw_emoji = str(emoji)
  1853. decoded = _SHORTCODE_TO_PROMPT_EMOJI.get(raw_emoji, raw_emoji)
  1854. emoji = _normalize_reaction_emoji(decoded)
  1855. actor_id = str(actor_id)
  1856. is_clarify = message_id in self._clarify_prompts
  1857. is_approval = message_id in self._approval_prompts
  1858. if not is_clarify and not is_approval:
  1859. return False
  1860. logger.debug(
  1861. "Chatto: reaction on prompt card %s: %s (raw %r)",
  1862. message_id,
  1863. emoji,
  1864. raw_emoji,
  1865. )
  1866. if is_clarify:
  1867. prompt = self._clarify_prompts.get(message_id)
  1868. else:
  1869. prompt = self._approval_prompts.get(message_id)
  1870. if prompt is None:
  1871. return False
  1872. # Room must match.
  1873. if str(getattr(prompt, "chat_id", "")) != room_id:
  1874. logger.debug(
  1875. "Chatto: reaction on prompt %s ignored - room %s, card lives in %s",
  1876. message_id,
  1877. room_id,
  1878. getattr(prompt, "chat_id", ""),
  1879. )
  1880. return True
  1881. # Expiry handling.
  1882. if self._interactive_prompt_expired(prompt):
  1883. try:
  1884. await self.edit_message(
  1885. room_id, message_id, "⏰ This prompt has expired."
  1886. )
  1887. except Exception:
  1888. logger.debug("Chatto: expired prompt edit failed", exc_info=True)
  1889. if is_clarify:
  1890. self._clarify_prompts.pop(message_id, None)
  1891. cid = getattr(prompt, "clarify_id", None)
  1892. if cid is not None:
  1893. self._clarify_id_to_message.pop(cid, None)
  1894. else:
  1895. self._approval_prompts.pop(message_id, None)
  1896. return True
  1897. # Authorization (fail-closed).
  1898. if not self._is_authorized(actor_id):
  1899. logger.debug(
  1900. "Chatto: reaction on prompt %s by unauthorized actor %s ignored",
  1901. message_id,
  1902. actor_id,
  1903. )
  1904. return True
  1905. # Double-click guard.
  1906. if getattr(prompt, "resolved", False):
  1907. logger.debug(
  1908. "Chatto: prompt %s already resolved - reaction ignored", message_id
  1909. )
  1910. return True
  1911. if is_clarify and getattr(prompt, "awaiting_text", False):
  1912. return True
  1913. user = self._user_cache.get(actor_id)
  1914. user_label = (
  1915. getattr(user, "name", None) or getattr(user, "login", None) or actor_id
  1916. )
  1917. if is_clarify:
  1918. from tools import clarify_gateway # type: ignore[import-not-found]
  1919. choices = list(getattr(prompt, "choices", []) or [])
  1920. logger.debug(
  1921. "Chatto: clarify prompt %s accepts: %s",
  1922. message_id,
  1923. [*choices, "Other"],
  1924. )
  1925. if emoji == _OTHER_EMOJI_KEY:
  1926. prompt.awaiting_text = True # type: ignore[assignment]
  1927. try:
  1928. clarify_gateway.mark_awaiting_text(prompt.clarify_id) # type: ignore[attr-defined]
  1929. except Exception:
  1930. logger.debug("Chatto: mark_awaiting_text failed", exc_info=True)
  1931. try:
  1932. await self.edit_message(
  1933. room_id, message_id, f"awaiting text from {user_label}"
  1934. )
  1935. except Exception:
  1936. logger.debug("Chatto: prompt edit failed", exc_info=True)
  1937. return True
  1938. if emoji in _NUMBER_EMOJI_KEYS:
  1939. idx = _NUMBER_EMOJI_KEYS.index(emoji)
  1940. if 0 <= idx < len(choices):
  1941. choice = choices[idx]
  1942. prompt.resolved = True
  1943. self._clarify_prompts.pop(message_id, None)
  1944. cid = getattr(prompt, "clarify_id", None) # type: ignore[arg-type]
  1945. if cid is not None:
  1946. self._clarify_id_to_message.pop(cid, None)
  1947. try:
  1948. clarify_gateway.resolve_gateway_clarify(str(cid), choice)
  1949. except Exception:
  1950. logger.exception("Chatto: resolve_gateway_clarify failed")
  1951. try:
  1952. await self.edit_message(
  1953. room_id,
  1954. message_id,
  1955. f"✅ {user_label} chose: {choice}",
  1956. )
  1957. except Exception:
  1958. logger.debug("Chatto: prompt edit failed", exc_info=True)
  1959. return True
  1960. # Invalid emoji feedback — name the options, so a rejected
  1961. # reaction teaches the valid ones instead of just refusing.
  1962. offered = [
  1963. _PROMPT_NUMBER_EMOJIS[i]
  1964. for i in range(min(len(choices), len(_PROMPT_NUMBER_EMOJIS)))
  1965. ]
  1966. try:
  1967. await self.send(
  1968. room_id,
  1969. "That reaction isn't a valid option for this prompt. "
  1970. f"React with {', '.join([*offered, _PROMPT_OTHER_EMOJI])}.",
  1971. )
  1972. except Exception:
  1973. logger.debug("Chatto: invalid reaction feedback failed", exc_info=True)
  1974. return True
  1975. # Approval prompt.
  1976. choices_map = getattr(prompt, "choices_map", {}) or {}
  1977. logger.debug(
  1978. "Chatto: approval prompt %s accepts: %s",
  1979. message_id,
  1980. [*choices_map],
  1981. )
  1982. if emoji in choices_map:
  1983. choice = choices_map[emoji]
  1984. prompt.resolved = True
  1985. self._approval_prompts.pop(message_id, None)
  1986. try:
  1987. import tools.approval as approval_mod
  1988. approval_mod.resolve_gateway_approval(
  1989. str(getattr(prompt, "session_key", "")), choice
  1990. )
  1991. except Exception:
  1992. logger.exception("Chatto: resolve_gateway_approval failed")
  1993. try:
  1994. await self.edit_message(
  1995. room_id, message_id, f"✅ {user_label}: {choice}"
  1996. )
  1997. except Exception:
  1998. logger.debug("Chatto: prompt edit failed", exc_info=True)
  1999. return True
  2000. # Invalid emoji feedback — the card's legend is the shortest correct
  2001. # list of what *is* valid here, so repeat it verbatim.
  2002. legend = ", ".join(f"{key} {choice}" for key, choice in choices_map.items())
  2003. try:
  2004. await self.send(
  2005. room_id,
  2006. "That reaction isn't a valid option for this prompt. "
  2007. f"Valid on this card: {legend}.",
  2008. )
  2009. except Exception:
  2010. logger.debug("Chatto: invalid reaction feedback failed", exc_info=True)
  2011. return True
  2012. def _evict_roster(self, room_id: str) -> None:
  2013. """Drop all roster state for a room.
  2014. Called on membership changes (user_joined/left_room), when we leave a
  2015. room ourselves, and on reconnect. The subscribe snapshot carries the
  2016. room and user directory but no presence, and the adapter does not
  2017. request it — so a discarded cache is what forces one honest refetch.
  2018. """
  2019. try:
  2020. self._roster_announced.remove(room_id)
  2021. except ValueError:
  2022. pass
  2023. self._rosters.pop(room_id, None)
  2024. async def _roster_for_room(self, client: ChattoClient, *, room_id: str) -> str:
  2025. """The room's roster line for this turn — delivered on every turn.
  2026. Threads hold isolated sessions and the agent only sees
  2027. channel_context per dispatch, so each channel turn carries the current
  2028. audience rather than deduplicating it. The projection exists to spare
  2029. the directory, not the prompt: presence patches land in _user_cache
  2030. in place, membership events or a reconnect evict the room so its next
  2031. turn refetches once. An unprojected room retries on its next turn
  2032. until a lookup succeeds.
  2033. """
  2034. if room_id not in self._roster_announced:
  2035. fetched = await self._room_roster_context(client, room_id)
  2036. if fetched is None:
  2037. return ""
  2038. self._announce_roster(room_id, fetched)
  2039. return self._roster_line(room_id)
  2040. async def _dispatch_message_posted(
  2041. self, payload: MessagePostedEvent, event_id: str
  2042. ) -> None:
  2043. # Respond-room gate first: read-only memberships must not cost a
  2044. # single API call, so this runs before fetch_message and _get_chatto_client.
  2045. if not self._answers_in_room(payload.room_id):
  2046. logger.debug(
  2047. "Chatto: message from read-only room %s ignored", payload.room_id
  2048. )
  2049. return
  2050. client = await self._get_chatto_client()
  2051. if client is None:
  2052. logger.warning("Chatto: dropping message - no client available")
  2053. return
  2054. logger.debug("Chatto WS: 'message_posted' payload:%s", payload)
  2055. message = await client.get_message(room_id=payload.room_id, event_id=event_id)
  2056. if message is None or message.deleted_at:
  2057. return
  2058. message_body = message.body or ""
  2059. logger.debug("message: %s", message)
  2060. # A message carrying only an image/PDF has an empty body — dropping it
  2061. # here is what made attachments sent to Hermes disappear silently.
  2062. if not message_body and not message.attachments:
  2063. return
  2064. event = await self._admit_and_build(
  2065. client,
  2066. room_id=payload.room_id,
  2067. message=message,
  2068. source_message_id=event_id,
  2069. thread_root_event_id=payload.thread_root_event_id or None,
  2070. mentions=_Mentions.from_payload(
  2071. payload.mentions, self.me.id if self.me else ""
  2072. ),
  2073. )
  2074. if event is None:
  2075. return
  2076. self._remember_dispatched(event.message_id or "")
  2077. logger.info("Chatto: dispatching message to Hermes")
  2078. await self.handle_message(event)
  2079. return
  2080. def _remember_dispatched(self, message_id: str) -> None:
  2081. """Record a message ID as handed to the gateway, capped like _seen."""
  2082. if not message_id:
  2083. return
  2084. self._dispatched_ids.append(message_id)
  2085. def _edit_is_fresh(self, message: Message) -> bool:
  2086. """Whether this edit is young enough to still be processed.
  2087. Age is measured against the posting time, so an edit to an hours-old
  2088. message cannot resurrect a settled conversation even when it arrives
  2089. right now.
  2090. """
  2091. if message.created_at is None or message.updated_at is None:
  2092. return True
  2093. age_seconds = (message.updated_at - message.created_at).total_seconds()
  2094. return age_seconds <= self.chatto_config.edit_window.value
  2095. def _session_key_for(self, source) -> str:
  2096. """The gateway's own session key for this source.
  2097. Delegates to the base class rather than calling ``build_session_key``
  2098. itself. A hand-built key has to be right about two things this one was
  2099. not: the routing identity has to be pinned first (``_canonicalize``), and
  2100. the key has to carry the serving profile's namespace. Without the second,
  2101. a multiplexed gateway keys every bot into ``agent:main:`` and the derived
  2102. key names a session nobody is running — which made
  2103. ``_thread_session_exists`` answer False for a live thread session, and
  2104. would make ``cancel_session_processing`` a no-op on an edited message.
  2105. """
  2106. return self._source_session_key(source)
  2107. async def _dispatch_message_edited(self, payload: Any) -> None:
  2108. """Route an inbound edit according to the edit-dispatch contract.
  2109. Three outcomes for the edited message:
  2110. - currently being processed → cancel that turn and re-dispatch with
  2111. the corrected text (the cancelled turn reports 🚫 via its
  2112. CANCELLED outcome hook),
  2113. - never dispatched (e.g. a forgotten @mention added later) → re-run
  2114. the admission gates against the new body and answer for real,
  2115. - already answered → stay answered.
  2116. Edits whose text parses as a DM membership command are dropped: the
  2117. command ran when the message was posted and must not run again.
  2118. """
  2119. if not self.chatto_config.edit_dispatch.value:
  2120. return
  2121. # Read-only memberships cost no API call, mirroring the posted path.
  2122. if not self._answers_in_room(payload.room_id):
  2123. logger.debug("Chatto: edit from read-only room %s ignored", payload.room_id)
  2124. return
  2125. client = await self._get_chatto_client()
  2126. if client is None:
  2127. logger.warning("Chatto: dropping edit - no client available")
  2128. return
  2129. logger.debug("Chatto WS: 'message_edited' payload:%s", payload)
  2130. message = await client.get_message(
  2131. room_id=payload.room_id, event_id=payload.message_event_id
  2132. )
  2133. if message is None or message.deleted_at:
  2134. return
  2135. message_body = message.body or ""
  2136. if not message_body and not message.attachments:
  2137. return
  2138. if not self._edit_is_fresh(message):
  2139. logger.debug(
  2140. "Chatto: edit of msg %s outside the edit window",
  2141. payload.message_event_id,
  2142. )
  2143. return
  2144. # Cheap triage before the admission pipeline: an edit to an
  2145. # already-settled message (dispatched, neither running nor queued)
  2146. # must not cost get_room/media calls or re-fire acknowledgements.
  2147. was_dispatched = payload.message_event_id in self._dispatched_ids
  2148. if (
  2149. was_dispatched
  2150. and payload.message_event_id not in self._processing.values()
  2151. and not any(
  2152. pending.message_id == payload.message_event_id
  2153. for pending in self._pending_messages.values()
  2154. )
  2155. ):
  2156. logger.debug(
  2157. "Chatto: edit of already-answered msg %s ignored",
  2158. payload.message_event_id,
  2159. )
  2160. return
  2161. event = await self._admit_and_build(
  2162. client,
  2163. room_id=payload.room_id,
  2164. message=message,
  2165. source_message_id=payload.message_event_id,
  2166. thread_root_event_id=message.thread_root_event_id or None,
  2167. allow_dm_commands=False,
  2168. )
  2169. if event is None:
  2170. return
  2171. session_key = self._session_key_for(event.source)
  2172. if self._processing.get(session_key) == payload.message_event_id:
  2173. logger.info(
  2174. "Chatto: msg %s edited mid-run - restarting the turn",
  2175. payload.message_event_id,
  2176. )
  2177. # The cancelled task's completion hook clears _processing and
  2178. # reports 🚫 before this coroutine moves on, because cancel
  2179. # awaits the task. Queued follow-ups must survive.
  2180. await self.cancel_session_processing(
  2181. session_key, release_guard=True, discard_pending=False
  2182. )
  2183. elif not was_dispatched:
  2184. logger.info(
  2185. "Chatto: msg %s was never dispatched - edit starts a fresh turn",
  2186. payload.message_event_id,
  2187. )
  2188. else:
  2189. pending = self._pending_messages.get(session_key)
  2190. if pending is not None and pending.message_id == payload.message_event_id:
  2191. # Still queued behind the running turn: correct it in place
  2192. # instead of answering stale wording later.
  2193. pending.text = event.text
  2194. logger.info(
  2195. "Chatto: queued msg %s updated to its edited text",
  2196. payload.message_event_id,
  2197. )
  2198. return
  2199. logger.debug(
  2200. "Chatto: edit of already-answered msg %s ignored",
  2201. payload.message_event_id,
  2202. )
  2203. return
  2204. self._remember_dispatched(event.message_id or "")
  2205. logger.info("Chatto: dispatching edited message to Hermes")
  2206. await self.handle_message(event)
  2207. async def _admit_and_build(
  2208. self,
  2209. client: ChattoClient,
  2210. *,
  2211. room_id: str,
  2212. message: Message,
  2213. source_message_id: str,
  2214. thread_root_event_id: str | None,
  2215. mentions: _Mentions | None = None,
  2216. allow_dm_commands: bool = True,
  2217. ) -> MessageEvent | None:
  2218. """Run one hydrated inbound message through the admission pipeline.
  2219. Shared by the posted and the edited path: user resolution, auth,
  2220. mention gates, thread anchoring and media caching all behave
  2221. identically for both. Returns ``None`` for anything that must not
  2222. reach the agent. DM membership commands are executed here (side
  2223. effect) unless ``allow_dm_commands`` is False — edits pass False so
  2224. a corrected command line neither runs twice nor leaks to the agent.
  2225. A dispatch that opens a channel thread additionally carries the
  2226. member roster in ``channel_context`` (side effect: once the roster
  2227. was fetched, the thread root is recorded as announced).
  2228. ``mentions`` is the already-resolved answer to "who does this
  2229. address"; passing None derives it from the message body instead, which
  2230. is what an edit does because its event carries no targets. The
  2231. derivation happens down here rather than at the call site because
  2232. resolving a handle needs the member cache, and the roster fetch below
  2233. is what fills it — read any earlier, every handle looks unknown.
  2234. The caller owns dispatching: a non-None result still needs
  2235. ``handle_message()``.
  2236. """
  2237. user = self._user_cache.get(message.actor_id)
  2238. if user is None:
  2239. logger.debug("Chatto: resolving user %s", message.actor_id)
  2240. member = await client.get_room_member(
  2241. room_id=message.room_id, user_id=message.actor_id
  2242. )
  2243. if member is None or member.user is None:
  2244. logger.debug(
  2245. "Chatto: dropping message from unknown user %s",
  2246. message.actor_id,
  2247. )
  2248. return None
  2249. user = member.user
  2250. self._user_cache[user.id] = user
  2251. if not self._check_auth(user):
  2252. logger.debug(
  2253. "Chatto: dropping message from user %s (auth denied)",
  2254. message.actor_id,
  2255. )
  2256. return None
  2257. room_kind = await self._room_kind_for(client, message.room_id)
  2258. if room_kind is None:
  2259. logger.debug(
  2260. "Chatto: dropping message from room %s (room_kind lookup failed)",
  2261. message.room_id,
  2262. )
  2263. return None
  2264. message_body = message.body or ""
  2265. logger.debug("message_body: %s room_kind: %s", message_body, room_kind)
  2266. # Membership commands ride in over DMs only: they change what the bot
  2267. # listens to and must never reach the agent pipeline or the mention
  2268. # gates.
  2269. if room_kind == RoomKind.DM:
  2270. if allow_dm_commands:
  2271. if await self._handle_dm_command(room_id, message_body):
  2272. return None
  2273. elif message_body.startswith("/"):
  2274. logger.debug("Chatto: edited DM command %r not re-run", message_body)
  2275. return None
  2276. # Mention gating deliberately covers everything but DMs: in a channel
  2277. # the bot is one of many listeners and must be addressed, whereas a DM
  2278. # is already addressed at it. Chatto has no group rooms — any
  2279. # multi-participant surface is a channel, and a room whose kind the
  2280. # server never set counts as one too (see _answers_in_room). Both gates
  2281. # read one resolved `mentions`, so "addressed" is defined once and
  2282. # broadcast/role targets count for us on either path.
  2283. if room_kind != RoomKind.DM:
  2284. policy = self._room_policy(room_id)
  2285. # Handle resolution on the body-derived path reads the member
  2286. # cache, which the roster projection fills. Fetch it before deriving
  2287. # anything, or every handle looks unknown — the same projection is
  2288. # then reused for this turn's channel_context further down, one
  2289. # fetch per room.
  2290. if policy == RoomPolicy.OPEN:
  2291. await self._roster_for_room(client, room_id=room_id)
  2292. if mentions is None:
  2293. mentions = self._mentions_from_body(message_body)
  2294. if room_kind != RoomKind.DM:
  2295. policy = self._room_policy(room_id)
  2296. if policy == RoomPolicy.REQUIRE_MENTION and not mentions.me:
  2297. logger.debug(
  2298. "Chatto: dropping unaddressed message from %s (policy %s)",
  2299. room_id,
  2300. policy.value,
  2301. )
  2302. return None
  2303. # In an open channel we see every message, including ones plainly
  2304. # aimed at a named colleague. Answering those would be barging in,
  2305. # so acknowledge that we read it and stay quiet. Checked after the
  2306. # bot-mention test above, so a message naming us *and* someone
  2307. # else still counts as ours.
  2308. if policy == RoomPolicy.OPEN and not mentions.me and mentions.someone_else:
  2309. logger.info(
  2310. "Chatto: message addresses someone else, acknowledging only"
  2311. )
  2312. if self.chatto_config.reactions.value:
  2313. await self.add_reaction(message.room_id, message.id, "🫥")
  2314. return None
  2315. # Thread anchoring — if the incoming message is inside a Chatto thread, we
  2316. # keep that thread by default; otherwise leave thread_id unset so
  2317. # replies land at the root.
  2318. thread_id = (
  2319. thread_root_event_id or None
  2320. ) # we could also take the room id but then, we're in a thread already.
  2321. if not thread_id and room_kind != RoomKind.DM:
  2322. thread_id = message.id
  2323. logger.debug(
  2324. "Chatto: thread anchoring for msg %s: incoming thread_root_event_id=%r, "
  2325. "room_kind=%s, anchored thread_id=%r",
  2326. message.id,
  2327. message.thread_root_event_id,
  2328. room_kind.name,
  2329. thread_id,
  2330. )
  2331. # Every channel turn carries the room's roster: threads hold isolated
  2332. # sessions and the agent only sees channel_context per dispatch, so a
  2333. # repeat is not deduplication but the point (see _roster_for_room).
  2334. # The gateway prepends channel_context above the message text, so the
  2335. # roster never mingles with what the user actually wrote.
  2336. source = self.build_source(
  2337. chat_id=room_id,
  2338. chat_name=self._room_names.get(message.room_id),
  2339. chat_type=chat_type_for_room_kind(room_kind),
  2340. user_id=message.actor_id,
  2341. user_name=user.login, # use login, because display_name is changeable by anyone.
  2342. thread_id=thread_id,
  2343. message_id=source_message_id,
  2344. role_authorized=True,
  2345. )
  2346. # Channel context rides ahead of the message text as read-only
  2347. # reference: the roster names the audience, the thread block carries
  2348. # what the thread already said. DMs get neither — there is no audience
  2349. # to name and no thread to catch up on. Built after the source, because
  2350. # the thread block asks the session store whether this thread's session
  2351. # already has the history (see _thread_context_for).
  2352. context_blocks: list[str] = []
  2353. if room_kind != RoomKind.DM:
  2354. logger.debug(
  2355. "Chatto: fetching roster for room %s",
  2356. room_id,
  2357. )
  2358. roster = await self._roster_for_room(client, room_id=room_id)
  2359. if roster:
  2360. context_blocks.append(roster)
  2361. else:
  2362. logger.debug(
  2363. "Chatto: dispatching channel message without roster "
  2364. "(lookup failed or empty)"
  2365. )
  2366. thread_context = await self._thread_context_for(
  2367. client,
  2368. room_id=room_id,
  2369. thread_root_event_id=thread_root_event_id or "",
  2370. current_message_id=message.id,
  2371. source=source,
  2372. )
  2373. if thread_context:
  2374. context_blocks.append(thread_context)
  2375. # A fresh room message roots its own session (the anchoring above),
  2376. # so it starts empty even when the room talked for hours. The reply
  2377. # pointer above carries what this message answers; this block adds
  2378. # what the room said before it — including the bot's own output,
  2379. # whose echo is filtered as a self-event and reaches no session
  2380. # any other way.
  2381. if not thread_root_event_id:
  2382. room_context = await self._room_context_for(
  2383. client,
  2384. room_id=room_id,
  2385. trigger=message,
  2386. source=source,
  2387. )
  2388. if room_context:
  2389. context_blocks.append(room_context)
  2390. # A reply must not read as a fresh conversation: the gateway renders
  2391. # ``reply_to_text`` into the turn as "[Replying to …]", but only when the
  2392. # adapter resolved it. Signal does this for quotes; a Chatto reply's
  2393. # target is one get_message away, so a session that starts here (every
  2394. # fresh room message roots its own — see the anchoring above) at least
  2395. # knows what the reply answers. DMs get the same treatment: a reply is
  2396. # the one pointer a session start can carry there too.
  2397. reply_to_text: str | None = None
  2398. reply_to_is_own = False
  2399. if message.in_reply_to:
  2400. target = await self._reply_target(client, room_id, message.in_reply_to)
  2401. if target is not None:
  2402. reply_to_text = target.body or ""
  2403. reply_to_is_own = self._is_own(target)
  2404. # prepare a MessageEvent
  2405. message_event = MessageEvent(
  2406. text=message_body,
  2407. source=source,
  2408. message_id=message.id,
  2409. timestamp=message.created_at or datetime.now(UTC),
  2410. raw_message=message,
  2411. reply_to_message_id=message.in_reply_to,
  2412. reply_to_text=reply_to_text,
  2413. reply_to_is_own_message=reply_to_is_own,
  2414. channel_context="\n\n".join(context_blocks) or None,
  2415. )
  2416. if message_event.is_command():
  2417. message_event.message_type = MessageType.COMMAND
  2418. # Attachments — download and hand the local cache paths to the gateway,
  2419. # which runs vision enrichment / document extraction off media_urls.
  2420. (
  2421. message_event.media_urls,
  2422. message_event.media_types,
  2423. media_kinds,
  2424. ) = await self._cache_attachments(list(message.attachments))
  2425. if media_kinds:
  2426. # Same precedence as the Teams/Signal adapters: document-context
  2427. # injection gates strictly on DOCUMENT, image handling keys off the
  2428. # per-path image/* MIME regardless of message_type.
  2429. message_event.message_type = self._message_type_for_media_kinds(media_kinds)
  2430. else:
  2431. message_event.message_type = MessageType.TEXT
  2432. logger.debug("Chatto: MessageEvent: %s", message_event)
  2433. return message_event
  2434. async def _forward_reaction(
  2435. self,
  2436. event: RealtimeEvent,
  2437. payload: Any,
  2438. *,
  2439. removed: bool,
  2440. ) -> None:
  2441. """Forward a human reaction to the gateway's reaction hook surface.
  2442. The handler is registered by the gateway via ``set_reaction_handler``
  2443. and fans out as ``reaction:added`` / ``reaction:removed`` through the
  2444. HookRegistry. The dict shape mirrors the Slack adapter's — hook
  2445. consumers are written against that contract, not against a per-platform
  2446. one. Our own lifecycle reactions (👀/✅/❌) are dropped: forwarding them
  2447. would feed the agent its own markers.
  2448. """
  2449. actor_id = event.actor_id
  2450. if actor_id and self.me and actor_id == self.me.id:
  2451. return
  2452. if not payload.room_id or not payload.message_event_id or not actor_id:
  2453. return
  2454. handler = self._reaction_handler
  2455. if handler is None:
  2456. return
  2457. # The server echoes the shortcode name back in the event ("thumbsup",
  2458. # "white_check_mark") — which is exactly the shape the mirrored Slack
  2459. # contract uses (Slack reaction events carry names too), so it is
  2460. # forwarded as-is rather than decoded.
  2461. action = "removed" if removed else "added"
  2462. try:
  2463. await handler(
  2464. {
  2465. "platform": ChattoConstants.PLATFORM_NAME,
  2466. "event_name": f"reaction:{action}",
  2467. "reaction": payload.emoji,
  2468. "user_id": actor_id,
  2469. "item_user_id": None,
  2470. "item_type": "message",
  2471. "channel_id": payload.room_id,
  2472. "message_ts": payload.message_event_id,
  2473. "event_ts": event.id,
  2474. "raw_event": event,
  2475. },
  2476. )
  2477. except Exception: # pragma: no cover - the hook contract is non-blocking
  2478. logger.debug("Chatto: reaction hook forwarding failed", exc_info=True)
  2479. async def _handle_realtime_event(self, event: RealtimeEvent) -> None:
  2480. if self._is_seen(event.id):
  2481. return
  2482. if event.actor_id is None:
  2483. return
  2484. logger.debug("EVENT happened: '%s' from %s", event.kind, event.actor_id)
  2485. # Self-event filter — the actor_id on the envelope is authoritative.
  2486. actor_id = event.actor_id
  2487. if actor_id and self.me and actor_id == self.me.id:
  2488. return
  2489. if event.kind == "message_posted":
  2490. await self._dispatch_message_posted(event.payload, event.id)
  2491. elif event.kind == "message_edited":
  2492. await self._dispatch_message_edited(event.payload)
  2493. elif event.kind in ("reaction_added", "reaction_removed"):
  2494. # Interactive prompts (clarify / exec-approval) are driven by
  2495. # reactions. Route a reaction on a tracked prompt before
  2496. # forwarding — the prompt is consumed either way, but the reaction
  2497. # still reaches the gateway hook surface afterward.
  2498. if event.kind == "reaction_added":
  2499. try:
  2500. await self._try_handle_interactive_reaction(
  2501. getattr(event.payload, "room_id", None),
  2502. getattr(event.payload, "message_event_id", None),
  2503. getattr(event.payload, "emoji", None),
  2504. actor_id,
  2505. )
  2506. except Exception:
  2507. logger.debug(
  2508. "Chatto: interactive reaction routing failed", exc_info=True
  2509. )
  2510. await self._forward_reaction(
  2511. event,
  2512. event.payload,
  2513. removed=event.kind == "reaction_removed",
  2514. )
  2515. elif event.kind == "presence_changed":
  2516. # A participant's presence changed — patch the cached user in place.
  2517. # The identified user is the event's own actor: the realtime
  2518. # PresenceChangedEvent carries only a status field, and a
  2519. # payload.user_id read here raised AttributeError on every
  2520. # presence change, which the event loop mistook for a broken
  2521. # stream and answered with a reconnect backoff — dropping
  2522. # inbound messages that arrived inside the gap.
  2523. changed_user_id = event.actor_id
  2524. if (
  2525. changed_user_id
  2526. and (user := self._user_cache.get(changed_user_id)) is not None
  2527. ):
  2528. # The realtime payload carries the proto enum as a plain int
  2529. # (protobuf runtime without native enum wrappers); chattolib's
  2530. # own bot.py coerces the same field the same way. Read the
  2531. # wire shape, never ``.name`` directly, which raised
  2532. # AttributeError on every presence change and dropped inbound
  2533. # messages inside the reconnect gaps that followed.
  2534. raw_status = event.payload.status
  2535. if isinstance(raw_status, PresenceStatus):
  2536. status = raw_status
  2537. else:
  2538. proto_name = ProtoPresenceStatus.Name(int(raw_status))
  2539. status = _PRESENCE_BY_PROTO_NAME.get(
  2540. proto_name, PresenceStatus.UNSPECIFIED
  2541. )
  2542. user.presence_status = status
  2543. logger.debug(
  2544. "Chatto: patched presence of %s to %s",
  2545. changed_user_id,
  2546. status.name,
  2547. )
  2548. elif event.kind in ("user_joined_room", "user_left_room"):
  2549. # Membership moved — the room's roster projection is now wrong, so
  2550. # discard it; the next turn there refetches once and re-delivers.
  2551. if event.payload.room_id in self._roster_announced:
  2552. logger.debug(
  2553. "Chatto: %s invalidated the roster of room %s",
  2554. event.kind,
  2555. event.payload.room_id,
  2556. )
  2557. self._evict_roster(event.payload.room_id)
  2558. elif event.kind == "room_updated":
  2559. # Room metadata changed (name, description) — patch the cache in
  2560. # place so the next turn sees the fresh values instead of stale
  2561. # names from a long-lived _room_names dict.
  2562. payload = event.payload
  2563. room_id = getattr(payload, "room_id", None)
  2564. if room_id and room_id in self._joined_room_ids:
  2565. name = getattr(payload, "name", None)
  2566. if name is not None:
  2567. self._room_names[room_id] = name
  2568. logger.debug(
  2569. "Chatto: room %s updated: name=%s",
  2570. room_id,
  2571. name,
  2572. )
  2573. elif event.kind == "room_groups_updated":
  2574. # Room-group membership moved — discard the cached projection so
  2575. # the next turn that needs room groups refetches once.
  2576. logger.debug("Chatto: room_groups_updated — discarding cached group state")
  2577. elif event.kind == "message_retracted":
  2578. # A message was retracted — no-op for now. Interactive prompts
  2579. # are keyed by their own message IDs (not the original event_id),
  2580. # and the prompt timeout handles stale cards anyway.
  2581. payload = event.payload
  2582. logger.debug(
  2583. "Chatto: message retracted: %s in %s",
  2584. getattr(payload, "message_event_id", None),
  2585. getattr(payload, "room_id", None),
  2586. )
  2587. elif event.kind == "thread_created":
  2588. # A human created a thread — no adapter action needed. The thread
  2589. # will be seeded on first message because _seeded_threads starts
  2590. # empty and seeds on first turn into an unknown root.
  2591. payload = event.payload
  2592. logger.debug(
  2593. "Chatto: thread created: room=%s root=%s",
  2594. getattr(payload, "room_id", None),
  2595. getattr(payload, "thread_root_event_id", None),
  2596. )
  2597. elif event.kind == "user_typing":
  2598. # Presence-like ephemeral signal: a user is typing in a room.
  2599. # The adapter never surfaces this to the agent — it is only useful
  2600. # for a client UI cursor, which is not what a bot does.
  2601. logger.debug("Chatto: user_typing event received")
  2602. # confirmed:
  2603. # NOTE: "projection_event" (and "caught_up") belong to realtime protocol
  2604. # v2 on unreleased Chatto main — chattolib speaks v1 and can never
  2605. # deliver them here. Revisit when vendored chattolib gains v2 typing.
  2606. # The notification_* / viewer-state kinds are the server's per-viewer
  2607. # bookkeeping: they fire on every read-state change in every followed
  2608. # room, need no adapter action, and warning on each one buried real
  2609. # signals in noise.
  2610. elif event.kind in (
  2611. "mention_notification",
  2612. "notification_dismissed",
  2613. "notification_occurrences_changed",
  2614. "notification_unread_state_changed",
  2615. "thread_viewer_state_changed",
  2616. "room_marked_as_read",
  2617. "user_typing",
  2618. "notification_created",
  2619. "new_direct_message_notification",
  2620. "message_retracted",
  2621. "thread_created",
  2622. "thread_follow_changed",
  2623. ):
  2624. logger.debug(
  2625. "Chatto: '%s' event received. Not yet implemented or not needed.",
  2626. event.kind,
  2627. )
  2628. else:
  2629. logger.warning("Chatto: unknown event kind: '%s'", event.kind)
  2630. async def _chattolib_event_loop(self) -> None:
  2631. """Event loop using chattolib's stream_events.
  2632. This replaces the manual WebSocket loop with chattolib's high-level
  2633. stream_events() which provides pre-decoded RealtimeEvent objects.
  2634. """
  2635. delay = ChattoConstants.WS_RECONNECT_INITIAL_BACKOFF
  2636. while not self._closing:
  2637. try:
  2638. client = await self._require_chatto_client()
  2639. await self._refresh_rooms()
  2640. logger.info(
  2641. "Chatto: starting chattolib event stream with %d rooms",
  2642. len(self._joined_room_ids),
  2643. )
  2644. async for event in stream_events(
  2645. client, resume_cursor=self._realtime_cursor
  2646. ):
  2647. if self._closing:
  2648. return
  2649. if isinstance(event, RealtimeEvent):
  2650. try:
  2651. await self._handle_realtime_event(event)
  2652. except (AttributeError, TypeError) as exc:
  2653. # A handler reading a payload field that does not
  2654. # exist or is not the type it assumed is a handler
  2655. # bug, not wire damage: it failed one event, so it
  2656. # must not cost the stream. Reconnecting here is
  2657. # what turned the presence crashes into 30s
  2658. # backoff gaps that silently dropped every
  2659. # message_posted arriving inside them (see
  2660. # AGENTS.md, "A handler bug is not a broken
  2661. # stream"). The cursor is NOT advanced: an event
  2662. # the adapter never applied is not safe to resume
  2663. # past.
  2664. logger.warning(
  2665. "Chatto: handler for '%s' failed (%s); skipping event %s",
  2666. event.kind,
  2667. exc,
  2668. event.id,
  2669. )
  2670. except Exception:
  2671. # Not a wire-shape bug — loud traceback, same skip
  2672. # semantics: one event failed, the stream stays up.
  2673. logger.exception(
  2674. "Chatto: handler for '%s' crashed on event %s; skipping it",
  2675. event.kind,
  2676. event.id,
  2677. )
  2678. else:
  2679. # chattolib marks a cursor as safe only once its event
  2680. # has been applied, and leaves it None when the event
  2681. # cannot be replayed. Keeping the last good one is what
  2682. # turns a reconnect into a resume rather than a gap.
  2683. if event.cursor:
  2684. self._realtime_cursor = event.cursor
  2685. # Iterator exited cleanly — treat as a normal close and reconnect
  2686. # with the local backoff (no server hint available).
  2687. logger.info("Chatto: realtime stream ended, reconnecting")
  2688. except asyncio.CancelledError:
  2689. return
  2690. except ChattoRealtimeCloseError as exc:
  2691. if not exc.reconnect:
  2692. logger.error(
  2693. "Chatto: realtime closed by server (%s: %s), not reconnecting",
  2694. exc.code,
  2695. exc.message,
  2696. )
  2697. return
  2698. wait = max(
  2699. exc.retry_after_ms / 1000.0,
  2700. ChattoConstants.WS_RECONNECT_INITIAL_BACKOFF,
  2701. )
  2702. logger.warning(
  2703. "Chatto: realtime closed by server (%s), reconnecting in %.1fs",
  2704. exc.code,
  2705. wait,
  2706. )
  2707. delay = (
  2708. ChattoConstants.WS_RECONNECT_INITIAL_BACKOFF
  2709. ) # server hint supersedes local backoff
  2710. await self._sleep_interruptible(wait)
  2711. continue
  2712. except ChattoRealtimeError as exc:
  2713. if exc.fatal:
  2714. logger.error(
  2715. "Chatto: fatal realtime error (%s): %s", exc.code, exc.message
  2716. )
  2717. return
  2718. logger.warning(
  2719. "Chatto: realtime error (%s: %s), reconnecting in %.1fs",
  2720. exc.code,
  2721. exc.message,
  2722. delay,
  2723. )
  2724. except Exception as exc:
  2725. cause = exc.__cause__
  2726. if cause is not None and isinstance(cause, TypeError):
  2727. logger.warning(
  2728. "Chatto: realtime wire corruption (%s), reconnecting in %.1fs",
  2729. cause,
  2730. delay,
  2731. )
  2732. else:
  2733. logger.warning(
  2734. "Chatto: unexpected realtime error: %s, reconnecting in %.1fs",
  2735. exc,
  2736. delay,
  2737. )
  2738. if self._closing:
  2739. return
  2740. jitter = delay * 0.2 * random.random()
  2741. await self._sleep_interruptible(delay + jitter)
  2742. delay = min(delay * 2, ChattoConstants.WS_RECONNECT_MAX_BACKOFF)
  2743. async def _sleep_interruptible(self, seconds: float) -> None:
  2744. """Sleep in short slices so disconnect() cancels promptly."""
  2745. end = asyncio.get_running_loop().time() + seconds
  2746. while not self._closing:
  2747. remaining = end - asyncio.get_running_loop().time()
  2748. if remaining <= 0:
  2749. return
  2750. await asyncio.sleep(min(remaining, 0.5))
  2751. def _warn_if_home_channel_unjoined(self, member_ids: set[str]) -> None:
  2752. """Warn once when CHATTO_HOME_CHANNEL names a room the bot is not in.
  2753. Standalone cron delivery posts straight into that room with a fresh
  2754. client and no join logic of its own — without server-side membership
  2755. every proactive send fails there.
  2756. """
  2757. home_id = (self.chatto_config.home_channel.value or "").strip()
  2758. if (
  2759. not home_id
  2760. or home_id in member_ids
  2761. or home_id in self._joined_room_ids
  2762. or self._home_warning_logged
  2763. ):
  2764. return
  2765. self._home_warning_logged = True
  2766. logger.warning(
  2767. "Chatto: CHATTO_HOME_CHANNEL '%s' is not a joined room - cron and "
  2768. "notification delivery will fail until the bot joins it (invite "
  2769. "the account natively in Chatto, or DM it '/join').",
  2770. home_id,
  2771. )
  2772. async def _resolve_dm_partner(self, client: ChattoClient, room_id: str) -> None:
  2773. """Cache the chat partner's login for a DM room, best-effort.
  2774. DM rooms carry no usable name of their own, so the joined-rooms
  2775. summary names the other side instead. Resolved once per room and
  2776. session; without our own user (pre-connect) or on a directory error
  2777. the raw room name stays in place.
  2778. """
  2779. if room_id in self._dm_partners or self.me is None:
  2780. return
  2781. try:
  2782. member_ids, _page = await client.list_room_members(room_id)
  2783. if not member_ids:
  2784. return
  2785. members = await client.batch_get_room_members(room_id, member_ids)
  2786. except Exception:
  2787. logger.debug(
  2788. "Chatto: could not list members of DM %s", room_id, exc_info=True
  2789. )
  2790. return
  2791. for member in members:
  2792. user = member.user
  2793. if user and user.id != self.me.id:
  2794. self._dm_partners[room_id] = user.login or user.display_name
  2795. return
  2796. def _joined_room_label(self, room_id: str) -> str:
  2797. """One entry for the joined-rooms summary, with how-we-answer tags.
  2798. The tag names the room's participation mode at a glance: DMs answer
  2799. unconditionally, listed channels answer on mentions or every message,
  2800. and an unlisted channel is read-only. A DM is labelled with its chat
  2801. partner's login rather than its (empty) room name, and names are
  2802. quoted so empty strings and spaces stay visible.
  2803. """
  2804. name = self._room_names.get(room_id, room_id)
  2805. if self._room_kinds.get(room_id) == RoomKind.DM:
  2806. tags = "[dm]"
  2807. name = self._dm_partners.get(room_id) or name
  2808. else:
  2809. policy = self._room_policy(room_id)
  2810. if policy == RoomPolicy.REQUIRE_MENTION:
  2811. tags = "[on-mention]"
  2812. elif policy == RoomPolicy.OPEN:
  2813. tags = "[every-message]"
  2814. else:
  2815. tags = "[read-only]"
  2816. if room_id in self._universal_room_ids:
  2817. tags += " [universal]"
  2818. return f'"{name}" ({room_id}) {tags}'
  2819. def _log_joined_rooms(self) -> None:
  2820. """Log the joined rooms and how the bot answers in each of them."""
  2821. labels = [self._joined_room_label(rid) for rid in self._joined_room_ids]
  2822. logger.info(
  2823. "Chatto WS: currently joined in %d room(s): %s",
  2824. len(labels),
  2825. ", ".join(labels),
  2826. )
  2827. async def _refresh_rooms(self) -> None:
  2828. """Refresh room list via ConnectRPC, join and seed any newly discovered rooms.
  2829. Runs once per (re)connect, so this is also the bootstrap point for the
  2830. roster projection: nothing on the wire reports presence at connect
  2831. time (the subscribe snapshot has no presence field; presence arrives
  2832. as its own events), so cached presence could only heal on the next
  2833. random change. Discarding it makes each announced room's first turn
  2834. refetch fresh data — lazily, so quiet rooms stay free of API calls.
  2835. """
  2836. client = await self._get_chatto_client()
  2837. if client is None:
  2838. logger.warning("Chatto WS: _refresh_rooms aborted - no client available")
  2839. return
  2840. for announced in list(self._roster_announced):
  2841. self._evict_roster(announced)
  2842. try:
  2843. rooms_list = await client.list_rooms()
  2844. member_ids: set[str] = set()
  2845. new_room_ids: list[str] = []
  2846. for room_with_state in rooms_list:
  2847. if not room_with_state:
  2848. continue
  2849. room_obj = room_with_state.room or None
  2850. if not room_obj:
  2851. continue
  2852. self._room_names[room_obj.id] = room_obj.name
  2853. self._room_kinds[room_obj.id] = room_obj.kind
  2854. if room_obj.kind == RoomKind.DM:
  2855. await self._resolve_dm_partner(client, room_obj.id)
  2856. if room_obj.universal:
  2857. self._universal_room_ids.add(room_obj.id)
  2858. if not room_with_state.viewer_state.is_member:
  2859. continue
  2860. member_ids.add(room_obj.id)
  2861. if room_obj.id not in self._joined_room_ids:
  2862. new_room_ids.append(room_obj.id)
  2863. # Left via /leave, kicked, deleted: rooms we no longer belong
  2864. # to drop out here — otherwise the next refresh would quietly
  2865. # re-add what /leave just removed.
  2866. stale_room_ids = [
  2867. rid for rid in self._joined_room_ids if rid not in member_ids
  2868. ]
  2869. for rid in stale_room_ids:
  2870. self._joined_room_ids.remove(rid)
  2871. self._evict_roster(rid)
  2872. if stale_room_ids:
  2873. logger.info(
  2874. "Chatto WS: no longer a member of %d room(s): %s",
  2875. len(stale_room_ids),
  2876. stale_room_ids,
  2877. )
  2878. self._warn_if_home_channel_unjoined(member_ids)
  2879. if new_room_ids:
  2880. logger.info(
  2881. "Chatto WS: discovered %d new room(s): %s",
  2882. len(new_room_ids),
  2883. new_room_ids,
  2884. )
  2885. for rid in new_room_ids:
  2886. # Membership came straight from the directory scan
  2887. # (viewer_state.is_member); natively invited rooms need no
  2888. # JoinRoom call — same rule as _run_join.
  2889. if self._answers_in_room(rid):
  2890. await self._seed_room(rid)
  2891. else:
  2892. logger.info(
  2893. "Chatto WS: %s (%s) is on neither mention list -"
  2894. " joined read-only",
  2895. self._room_names.get(rid, rid),
  2896. rid,
  2897. )
  2898. self._joined_room_ids.append(rid)
  2899. self._log_joined_rooms()
  2900. except Exception:
  2901. logger.warning("Chatto WS: _refresh_rooms failed", exc_info=True)
  2902. # ------------------------------------------------------------------ #
  2903. # Read state (best-effort, Chatto-unique)
  2904. # ------------------------------------------------------------------ #
  2905. # Deliberately outside the try block above: this sweep runs on every
  2906. # refresh — including refreshes that discovered no new rooms. The
  2907. # old early return starved it to "only when something changed",
  2908. # leaving silent rooms unmarked for whole connection lifetimes.
  2909. # Best-effort: mark all joined rooms as read (room_id may be undefined here)
  2910. for _rid in list(self._joined_room_ids):
  2911. try:
  2912. await client.mark_room_as_read(room_id=_rid)
  2913. except Exception:
  2914. logger.debug(
  2915. "Chatto: mark_room_as_read failed for %s", _rid, exc_info=True
  2916. )
  2917. # ------------------------------------------------------------------ #
  2918. # Sending (ConnectRPC — unchanged from polling version)
  2919. # ------------------------------------------------------------------ #
  2920. def _resolve_outbound_thread(
  2921. self,
  2922. chat_id: str,
  2923. reply_to: str | None,
  2924. metadata: dict[str, Any] | None,
  2925. ) -> str | None:
  2926. """The thread an outbound message belongs in, or ``None`` for the room.
  2927. One definition for every outbound path (text and attachments):
  2928. ``metadata["thread_id"]`` wins — it names the thread root to stay in;
  2929. otherwise ``reply_to`` anchors a thread under the incoming message,
  2930. but only with ``auto_thread`` enabled. DMs never thread.
  2931. Deliberately does NOT open an auto-thread of its own — only the
  2932. chunked text path in :meth:`send` does that, because only there do
  2933. further chunks follow into the freshly created thread.
  2934. """
  2935. thread_id = (metadata or {}).get("thread_id")
  2936. if reply_to and self.chatto_config.auto_thread.value and not thread_id:
  2937. thread_id = reply_to
  2938. if self._room_kinds.get(chat_id) == RoomKind.DM:
  2939. return None
  2940. return str(thread_id) if thread_id else None
  2941. async def send(
  2942. self,
  2943. chat_id: str,
  2944. content: str,
  2945. reply_to: str | None = None,
  2946. metadata: dict[str, Any] | None = None,
  2947. ) -> SendResult:
  2948. """Send a message to a Chatto room.
  2949. Long messages are split into chunks via ``truncate_message`` and
  2950. each chunk is sent as a separate CreateMessage call. The first
  2951. chunk's message ID is returned as ``message_id``.
  2952. When ``auto_thread`` is enabled and the incoming message was a
  2953. regular room message (not already in a thread), the first chunk is
  2954. sent as a room message and its ID becomes the thread root. Subsequent
  2955. chunks are sent in that thread. This mirrors Discord's auto_thread
  2956. behavior.
  2957. BasePlatformAdapter override
  2958. """
  2959. if not content:
  2960. return SendResult(success=False, error="Empty message")
  2961. thread_id = self._resolve_outbound_thread(chat_id, reply_to, metadata)
  2962. room_kind = self._room_kinds.get(chat_id)
  2963. is_dm = room_kind == RoomKind.DM
  2964. # Auto-thread: by default, Chatto creates a thread for replies to room
  2965. # messages (not DMs, not already in a thread). This keeps conversations
  2966. # organized in the room. Can be disabled via extra.auto_thread=false.
  2967. use_auto_thread = (
  2968. self.chatto_config.auto_thread.value and not thread_id and not is_dm
  2969. )
  2970. client = await self._get_chatto_client()
  2971. if client is None:
  2972. return SendResult(
  2973. success=False, error="Chatto client not available", retryable=True
  2974. )
  2975. # The split, the routing and the stop-at-first-failure all live in
  2976. # _deliver_chunks, shared with the plugin tools: a long answer must
  2977. # reach the room the same way whoever sent it.
  2978. delivery = await _deliver_chunks(
  2979. client,
  2980. chat_id,
  2981. content,
  2982. thread_root_event_id=str(thread_id) if thread_id else "",
  2983. create_thread=use_auto_thread,
  2984. on_posted=self._mark_seen,
  2985. )
  2986. message_ids = delivery.message_ids
  2987. if use_auto_thread and message_ids and not thread_id:
  2988. thread_id = delivery.root_event_id
  2989. # Nothing got through at all — report the failure instead of a phantom success.
  2990. if not message_ids:
  2991. # Only server-side failures count as retryable; a bug in our own
  2992. # code must not read as a transient network blip.
  2993. return SendResult(
  2994. success=False,
  2995. error=(
  2996. str(delivery.error)
  2997. if delivery.error
  2998. else "Chatto: message could not be sent"
  2999. ),
  3000. retryable=isinstance(delivery.error, ChattoError),
  3001. )
  3002. first_id = message_ids[0]
  3003. # ------------------------------------------------------------------ #
  3004. # Thread following (best-effort, Chatto-unique)
  3005. # ------------------------------------------------------------------ #
  3006. if thread_id:
  3007. try:
  3008. await client.follow_thread(chat_id, thread_id)
  3009. except Exception:
  3010. logger.debug(
  3011. "Chatto: follow_thread failed for %s/%s",
  3012. chat_id,
  3013. thread_id,
  3014. exc_info=True,
  3015. )
  3016. # A later chunk failed after earlier ones went out: partial delivery.
  3017. if delivery.error is not None:
  3018. logger.warning(
  3019. "Chatto: sent %d/%d chunk(s) to %s before failing: %s",
  3020. len(message_ids),
  3021. delivery.chunks_total,
  3022. chat_id,
  3023. delivery.error,
  3024. )
  3025. # raw_response stays unset (dict-shaped per the SendResult contract):
  3026. # gateway consumers such as the cron scheduler call .get() on it, so a
  3027. # chattolib Message here would crash delivery bookkeeping *after* the
  3028. # send already succeeded — the job then falls back to the standalone
  3029. # path and the room sees the message twice.
  3030. return SendResult(success=True, message_id=first_id)
  3031. def format_message(self, content: str) -> str:
  3032. """Normalise outgoing text for Chatto.
  3033. The transformations live in :func:`_normalise_outbound_text`, shared
  3034. with the standalone cron sender.
  3035. BasePlatformAdapter override
  3036. """
  3037. return _normalise_outbound_text(content)
  3038. async def send_clarify(
  3039. self,
  3040. chat_id: str,
  3041. question: str,
  3042. choices: list[str] | None,
  3043. clarify_id: str,
  3044. session_key: str,
  3045. metadata: dict[str, Any] | None = None,
  3046. ) -> SendResult:
  3047. """Post a reaction-driven clarify prompt.
  3048. When ``choices`` is empty/None or exceeds 9 entries (the number emojis
  3049. only cover 1-9), delegate to the base-class text fallback. Otherwise
  3050. post a numbered message, seed one reaction per option plus ``✏️``
  3051. (Other), register the prompt in both registries, and return.
  3052. BasePlatformAdapter override
  3053. """
  3054. if not choices or len(choices) > 9:
  3055. return await super().send_clarify(
  3056. chat_id, question, choices, clarify_id, session_key, metadata=metadata
  3057. )
  3058. client = await self._get_chatto_client()
  3059. if client is None:
  3060. return SendResult(
  3061. success=False, error="Chatto client not available", retryable=True
  3062. )
  3063. number_emojis = _PROMPT_NUMBER_EMOJIS
  3064. emojis = [number_emojis[i] for i in range(len(choices))]
  3065. other_emoji = _PROMPT_OTHER_EMOJI
  3066. lines = [f"❓ {question}", ""]
  3067. for i, choice in enumerate(choices):
  3068. lines.append(f"{emojis[i]} {choice}")
  3069. lines.append(f"{other_emoji} Other (type your answer)")
  3070. content = "\n".join(lines)
  3071. # The card must carry the conversation's thread: an answer to it is
  3072. # routed by session key, and a card outside the thread keys to a
  3073. # different session than the one waiting for the answer.
  3074. thread_id = self._resolve_outbound_thread(chat_id, None, metadata)
  3075. try:
  3076. msg = await client.post_message(
  3077. room_id=str(chat_id),
  3078. body=content,
  3079. thread_root_event_id=str(thread_id) if thread_id else "",
  3080. )
  3081. except ChattoError as e:
  3082. logger.warning("Chatto: send_clarify post failed: %s", e)
  3083. return SendResult(success=False, error=str(e), retryable=True)
  3084. except Exception as e:
  3085. logger.warning("Chatto: send_clarify post error: %s", e)
  3086. return SendResult(success=False, error=str(e), retryable=False)
  3087. message_id = str(msg.id)
  3088. if message_id:
  3089. self._mark_seen(message_id)
  3090. for emoji in [*emojis, other_emoji]:
  3091. try:
  3092. await client.add_reaction(
  3093. room_id=str(chat_id),
  3094. message_event_id=message_id,
  3095. emoji=_shortcode_for_reaction_emoji(emoji),
  3096. )
  3097. except Exception:
  3098. logger.debug("Chatto: seed reaction %s failed", emoji, exc_info=True)
  3099. prompt = _ClarifyPrompt(
  3100. clarify_id=clarify_id,
  3101. choices=list(choices),
  3102. session_key=session_key,
  3103. chat_id=str(chat_id),
  3104. message_id=message_id,
  3105. expires_at=time.monotonic() + INTERACTIVE_PROMPT_TIMEOUT_SECONDS,
  3106. )
  3107. self._clarify_prompts[message_id] = prompt
  3108. self._clarify_id_to_message[clarify_id] = message_id
  3109. if thread_id:
  3110. try:
  3111. await client.follow_thread(chat_id, thread_id)
  3112. except Exception:
  3113. logger.debug(
  3114. "Chatto: clarify follow_thread failed for %s/%s",
  3115. chat_id,
  3116. thread_id,
  3117. exc_info=True,
  3118. )
  3119. return SendResult(success=True, message_id=message_id)
  3120. async def retire_clarify_card(self, clarify_id: str, notice: str) -> None:
  3121. """Rewrite a still-live clarify card into a terminal, button-less state.
  3122. The gateway calls this whenever it ends a clarify without a click — the
  3123. wait timed out, the session was reset, or unmatched free prose superseded
  3124. the prompt — so the card stops advertising an answer path the released
  3125. clarify can no longer accept. Keyed by ``clarify_id``, so a late call
  3126. cannot touch a newer prompt; no-op once resolved.
  3127. BasePlatformAdapter override
  3128. """
  3129. message_id = self._clarify_id_to_message.pop(clarify_id, None)
  3130. if message_id is None:
  3131. return
  3132. prompt = self._clarify_prompts.get(message_id)
  3133. if prompt is None:
  3134. return
  3135. prompt.resolved = True
  3136. terminal = f"❌ {notice}"
  3137. try:
  3138. await self.edit_message(prompt.chat_id, message_id, terminal)
  3139. except Exception:
  3140. logger.debug("Chatto: retire_clarify_card edit failed", exc_info=True)
  3141. self._clarify_prompts.pop(message_id, None)
  3142. async def _send_exec_approval_prompt(
  3143. self, prompt: ExecApprovalPrompt
  3144. ) -> SendResult:
  3145. """Post a reaction-driven exec-approval prompt.
  3146. BasePlatformAdapter override
  3147. """
  3148. choices = list(prompt.choices)
  3149. choices_map: dict[str, str] = {}
  3150. emojis: list[str] = []
  3151. for i, choice in enumerate(choices):
  3152. emoji = _PROMPT_APPROVAL_EMOJIS.get(choice)
  3153. if emoji is None:
  3154. emoji = (
  3155. _PROMPT_NUMBER_EMOJIS[i] if i < len(_PROMPT_NUMBER_EMOJIS) else "🔢"
  3156. )
  3157. emojis.append(emoji)
  3158. # Keys in the normalized form: reaction matching looks the
  3159. # incoming emoji up here after _normalize_reaction_emoji.
  3160. choices_map[_normalize_reaction_emoji(emoji)] = choice
  3161. client = await self._get_chatto_client()
  3162. if client is None:
  3163. return SendResult(
  3164. success=False, error="Chatto client not available", retryable=True
  3165. )
  3166. smart_denied = bool(prompt.smart_denied)
  3167. tier = "once"
  3168. if not smart_denied:
  3169. if "always" in choices:
  3170. tier = "always"
  3171. elif "session" in choices:
  3172. tier = "session"
  3173. typed_hints = {
  3174. "once": "React with ✅ to approve once.",
  3175. "session": "React with 🌀 to approve for this session.",
  3176. "always": "React with ♾️ to approve always.",
  3177. }
  3178. typed_hint = typed_hints.get(tier, "")
  3179. lines = [str(prompt.text), ""]
  3180. if typed_hint:
  3181. lines.append(typed_hint)
  3182. lines.append("")
  3183. lines.append("Legend:")
  3184. for emoji, choice in choices_map.items():
  3185. lines.append(f"{emoji} {choice}")
  3186. content = "\n".join(lines)
  3187. try:
  3188. # Same thread logic as send_clarify: the card must land in the
  3189. # conversation's thread, or a text answer to it keys to a session
  3190. # that is not the one waiting on the approval.
  3191. thread_id = self._resolve_outbound_thread(
  3192. str(prompt.chat_id), None, prompt.metadata
  3193. )
  3194. msg = await client.post_message(
  3195. room_id=str(prompt.chat_id),
  3196. body=content,
  3197. thread_root_event_id=str(thread_id) if thread_id else "",
  3198. )
  3199. except ChattoError as e:
  3200. logger.warning("Chatto: send_exec_approval post failed: %s", e)
  3201. return SendResult(success=False, error=str(e), retryable=True)
  3202. except Exception as e:
  3203. logger.warning("Chatto: send_exec_approval post error: %s", e)
  3204. return SendResult(success=False, error=str(e), retryable=False)
  3205. message_id = str(msg.id)
  3206. if message_id:
  3207. self._mark_seen(message_id)
  3208. for emoji in emojis:
  3209. try:
  3210. await client.add_reaction(
  3211. room_id=str(prompt.chat_id),
  3212. message_event_id=message_id,
  3213. emoji=_shortcode_for_reaction_emoji(emoji),
  3214. )
  3215. except Exception:
  3216. logger.debug("Chatto: seed reaction %s failed", emoji, exc_info=True)
  3217. approval_prompt = _ApprovalPrompt(
  3218. session_key=str(prompt.session_key),
  3219. chat_id=str(prompt.chat_id),
  3220. message_id=message_id,
  3221. choices_map=choices_map,
  3222. expires_at=time.monotonic() + INTERACTIVE_PROMPT_TIMEOUT_SECONDS,
  3223. )
  3224. self._approval_prompts[message_id] = approval_prompt
  3225. if thread_id:
  3226. try:
  3227. await client.follow_thread(str(prompt.chat_id), thread_id)
  3228. except Exception:
  3229. logger.debug(
  3230. "Chatto: approval follow_thread failed for %s/%s",
  3231. prompt.chat_id,
  3232. thread_id,
  3233. exc_info=True,
  3234. )
  3235. return SendResult(success=True, message_id=message_id)
  3236. async def edit_message(
  3237. self,
  3238. chat_id: str,
  3239. message_id: str,
  3240. content: str,
  3241. *,
  3242. finalize: bool = False,
  3243. ) -> SendResult:
  3244. """Edit a message we previously sent, via MessageService/UpdateMessage.
  3245. The stream consumer drives streaming replies through this: without the
  3246. override the base class reports "Not supported" and every incremental
  3247. update arrives as a *new* message.
  3248. ``finalize`` is a no-op for Chatto — an edit is an edit here, there is
  3249. no in-progress card state to close out (hence no
  3250. ``REQUIRES_EDIT_FINALIZE``).
  3251. Content that exceeds the per-message limit is refused rather than
  3252. silently truncated, so the caller falls back to ``send()``, which
  3253. splits across messages.
  3254. BasePlatformAdapter override
  3255. """
  3256. if not message_id:
  3257. return SendResult(success=False, error="Chatto: no message id to edit")
  3258. if not content:
  3259. return SendResult(success=False, error="Empty message")
  3260. formatted = self.format_message(content)
  3261. if len(formatted) > ChattoConstants.MAX_MESSAGE_LENGTH:
  3262. # Refuse instead of truncating: the caller's fallback path splits.
  3263. return SendResult(
  3264. success=False,
  3265. error=(
  3266. f"Chatto: edit exceeds {ChattoConstants.MAX_MESSAGE_LENGTH} "
  3267. f"chars ({len(formatted)})"
  3268. ),
  3269. )
  3270. client = await self._get_chatto_client()
  3271. if client is None:
  3272. return SendResult(
  3273. success=False, error="Chatto client not available", retryable=True
  3274. )
  3275. try:
  3276. msg = await client.update_message(
  3277. room_id=str(chat_id),
  3278. event_id=str(message_id),
  3279. body=formatted,
  3280. )
  3281. except ChattoError as e:
  3282. logger.warning("Chatto: UpdateMessage failed for %s: %s", message_id, e)
  3283. return SendResult(success=False, error=str(e), retryable=True)
  3284. except Exception as e:
  3285. logger.warning("Chatto: UpdateMessage error for %s: %s", message_id, e)
  3286. return SendResult(success=False, error=str(e), retryable=False)
  3287. # Our own edit comes back as a message_edited event; mark it seen so it
  3288. # is never mistaken for inbound traffic.
  3289. edited_id = msg.id or str(message_id)
  3290. self._mark_seen(edited_id)
  3291. return SendResult(success=True, message_id=edited_id)
  3292. async def delete_message(self, chat_id: str, message_id: str) -> bool:
  3293. """Delete a message via MessageService/DeleteMessage.
  3294. Used by the stream consumer's fresh-final cleanup (removing a preview
  3295. message once the completed reply has been sent) and by the ephemeral
  3296. reply TTL.
  3297. BasePlatformAdapter override
  3298. """
  3299. if not chat_id or not message_id:
  3300. return False
  3301. client = await self._get_chatto_client()
  3302. if client is None:
  3303. logger.warning("Chatto: DeleteMessage — client unavailable")
  3304. return False
  3305. try:
  3306. return bool(
  3307. await client.delete_message(
  3308. room_id=str(chat_id),
  3309. event_id=str(message_id),
  3310. )
  3311. )
  3312. except ChattoError as e:
  3313. logger.warning("Chatto: DeleteMessage failed for %s: %s", message_id, e)
  3314. return False
  3315. except Exception as e:
  3316. logger.warning("Chatto: DeleteMessage error for %s: %s", message_id, e)
  3317. return False
  3318. async def create_handoff_thread(
  3319. self,
  3320. parent_chat_id: str,
  3321. name: str,
  3322. ) -> str | None:
  3323. """Anchor a session handoff in a fresh thread under *parent_chat_id*.
  3324. Chatto threads hang off a message, not off the room, so we post a seed
  3325. message and hand its ID back as the thread root — the same shape the
  3326. Slack adapter uses. DMs don't support threads, so they get ``None``
  3327. and the watcher keeps delivering into the DM itself.
  3328. BasePlatformAdapter override
  3329. """
  3330. if not parent_chat_id:
  3331. return None
  3332. if self._room_kinds.get(parent_chat_id) == RoomKind.DM:
  3333. logger.debug("Chatto: handoff thread skipped — %s is a DM", parent_chat_id)
  3334. return None
  3335. client = await self._get_chatto_client()
  3336. if client is None:
  3337. logger.warning("Chatto: handoff thread — client unavailable")
  3338. return None
  3339. try:
  3340. msg = await client.post_message(
  3341. room_id=str(parent_chat_id),
  3342. body=f"🧵 Hermes handoff — **{(name or 'session').strip()[:80]}**",
  3343. )
  3344. except Exception as e:
  3345. logger.warning(
  3346. "Chatto: handoff thread seed-post failed for room %s: %s",
  3347. parent_chat_id,
  3348. e,
  3349. )
  3350. return None
  3351. seed_id = msg.id
  3352. if not seed_id:
  3353. logger.warning("Chatto: handoff thread seed-post returned no message id")
  3354. return None
  3355. self._mark_seen(seed_id)
  3356. try:
  3357. await client.follow_thread(str(parent_chat_id), seed_id)
  3358. except Exception:
  3359. logger.debug(
  3360. "Chatto: follow_thread failed for handoff %s/%s",
  3361. parent_chat_id,
  3362. seed_id,
  3363. exc_info=True,
  3364. )
  3365. return seed_id
  3366. # Overridden from BaseAdapter:
  3367. async def send_typing(self, chat_id: str, metadata=None) -> None:
  3368. """Start a persistent typing indicator for a room.
  3369. Sends a typing ping every 10 seconds (Chatto's indicator likely
  3370. lasts ~8-10s). The background loop runs until ``stop_typing()``
  3371. is called or the task is cancelled.
  3372. BasePlatformAdapter override
  3373. """
  3374. if chat_id in self._typing_tasks:
  3375. return # already running
  3376. async def _typing_loop() -> None:
  3377. try:
  3378. while True:
  3379. try:
  3380. client = await self._get_chatto_client()
  3381. if client is None:
  3382. return
  3383. await client.refresh_typing_indicator(room_id=str(chat_id))
  3384. except asyncio.CancelledError:
  3385. return
  3386. except Exception:
  3387. logger.debug(
  3388. "Chatto: typing indicator refresh failed for %s",
  3389. chat_id,
  3390. exc_info=True,
  3391. )
  3392. await asyncio.sleep(10)
  3393. except asyncio.CancelledError:
  3394. pass
  3395. finally:
  3396. self._typing_tasks.pop(chat_id, None)
  3397. self._typing_tasks[chat_id] = asyncio.create_task(_typing_loop())
  3398. async def stop_typing(self, chat_id: str) -> None:
  3399. """Stop the persistent typing indicator for a room.
  3400. BasePlatformAdapter override
  3401. """
  3402. task = self._typing_tasks.pop(chat_id, None)
  3403. if task:
  3404. task.cancel()
  3405. try:
  3406. await task
  3407. except (asyncio.CancelledError, Exception):
  3408. logger.debug("Chatto: typing task ended for %s", chat_id, exc_info=True)
  3409. async def get_chat_info(self, chat_id: str) -> dict[str, Any]:
  3410. """Get information about a chat/room.
  3411. BasePlatformAdapter override
  3412. """
  3413. name = self._room_names.get(chat_id, chat_id)
  3414. kind = self._room_kinds.get(chat_id)
  3415. return {
  3416. "name": name,
  3417. "type": chat_type_for_room_kind(kind).value,
  3418. }
  3419. # ------------------------------------------------------------------ #
  3420. # Reactions
  3421. # ------------------------------------------------------------------ #
  3422. async def add_reaction(self, room_id: str, message_id: str, emoji: str) -> bool:
  3423. """Add a reaction to a message via MessageService/AddReaction.
  3424. BasePlatformAdapter override
  3425. """
  3426. shortcode = _shortcode_for_reaction_emoji(emoji)
  3427. client = await self._get_chatto_client()
  3428. if client is None:
  3429. logger.warning("Chatto: AddReaction — client unavailable")
  3430. return False
  3431. try:
  3432. result = await client.add_reaction(
  3433. room_id=room_id,
  3434. message_event_id=message_id,
  3435. emoji=shortcode,
  3436. )
  3437. return result
  3438. except ChattoError as e:
  3439. logger.warning("Chatto: AddReaction failed: %s", e)
  3440. return False
  3441. except Exception as e:
  3442. logger.warning("Chatto: AddReaction error: %s", e)
  3443. return False
  3444. async def remove_reaction(self, chat_id: str, message_id: str, emoji: str) -> bool:
  3445. """Remove a reaction from a message via MessageService/RemoveReaction.
  3446. BasePlatformAdapter override
  3447. """
  3448. shortcode = _shortcode_for_reaction_emoji(emoji)
  3449. client = await self._get_chatto_client()
  3450. if client is None:
  3451. logger.warning("Chatto: RemoveReaction — client unavailable")
  3452. return False
  3453. try:
  3454. result = await client.remove_reaction(
  3455. room_id=str(chat_id),
  3456. message_event_id=str(message_id),
  3457. emoji=shortcode,
  3458. )
  3459. return result
  3460. except ChattoError as e:
  3461. logger.warning("Chatto: RemoveReaction failed: %s", e)
  3462. return False
  3463. except Exception as e:
  3464. logger.warning("Chatto: RemoveReaction error: %s", e)
  3465. return False
  3466. # NOTE: there is deliberately no start_dm(). On Chatto only a person can
  3467. # start a DM ("Only people can start a DM... A bot cannot start a DM, even
  3468. # with message.post", dev-docs/guides/operations/permissions), and nothing
  3469. # in Hermes calls such a method — BasePlatformAdapter does not even declare
  3470. # one. An earlier version had it and advertised "DM initiation" in the
  3471. # capability banner, but it could only ever fail: the vendored chattolib's
  3472. # start_dm() calls a service stub that does not exist, so the AttributeError
  3473. # was swallowed into a silent None. DMs become usable the moment the other
  3474. # person writes to the bot; /join and the chatto_send_dm tool both say so.
  3475. # ------------------------------------------------------------------ #
  3476. # Room creation (Chatto-unique)
  3477. # ------------------------------------------------------------------ #
  3478. async def create_room(
  3479. self,
  3480. name: str,
  3481. description: str = "",
  3482. group_id: str = "",
  3483. universal: bool = True,
  3484. ) -> str | None:
  3485. """Create an ad-hoc room via RoomService/CreateRoom.
  3486. Returns the room ID on success, or None on failure.
  3487. BasePlatformAdapter override
  3488. """
  3489. client = await self._get_chatto_client()
  3490. if client is None:
  3491. return None
  3492. try:
  3493. room = await client.create_room(
  3494. name=name,
  3495. group_id=group_id or "",
  3496. description=description,
  3497. universal=universal,
  3498. )
  3499. rid = str(room.id) if room else ""
  3500. if rid:
  3501. self._room_names[rid] = room.name
  3502. self._room_kinds[rid] = room.kind
  3503. return rid
  3504. logger.debug("Chatto: CreateRoom returned no room id")
  3505. return None
  3506. except ChattoError as e:
  3507. logger.debug("Chatto: CreateRoom failed: %s", e)
  3508. return None
  3509. except Exception as e:
  3510. logger.debug("Chatto: CreateRoom error: %s", e)
  3511. return None
  3512. # ------------------------------------------------------------------ #
  3513. # Processing lifecycle hooks (reactions-based, like Discord)
  3514. # ------------------------------------------------------------------ #
  3515. def _event_room_and_message_id(self, event: MessageEvent) -> tuple[str, str]:
  3516. """Extract room_id and message_id from a MessageEvent."""
  3517. message_id = event.message_id or ""
  3518. return event.source.chat_id, message_id
  3519. async def on_processing_start(self, event: MessageEvent) -> None:
  3520. """Record the turn as open, then add an 👀 (eyes) reaction.
  3521. The record is what lets an edit of this very message be recognized as
  3522. a mid-run correction. It must be maintained even when reactions are
  3523. disabled — tracking and decorating are independent concerns.
  3524. BasePlatformAdapter override
  3525. """
  3526. session_key = (
  3527. self._session_key_for(event.source) if event.source is not None else ""
  3528. )
  3529. if session_key and event.message_id:
  3530. self._processing[session_key] = str(event.message_id)
  3531. if not self.chatto_config.reactions.value:
  3532. return
  3533. chat_id, message_id = self._event_room_and_message_id(event)
  3534. if not chat_id or not message_id:
  3535. # Routine, not a fault: the gateway runs agent-initiated turns
  3536. # (heartbeat polls, goal continuations) through the same pipeline
  3537. # with message_id=None, and there is no inbound message to mark.
  3538. logger.debug(
  3539. "Chatto: nothing to react to (chat_id=%r, message_id=%r)",
  3540. chat_id,
  3541. message_id,
  3542. )
  3543. return
  3544. await self.add_reaction(chat_id, message_id, "👀")
  3545. async def on_processing_complete(
  3546. self,
  3547. event: MessageEvent,
  3548. outcome: ProcessingOutcome,
  3549. ) -> None:
  3550. """Close the turn's record, then swap 👀 for ✅/❌/🚫.
  3551. Fires for every outcome, including CANCELLED (mid-run edit
  3552. correction) — this is what re-arms `_processing` before the corrected
  3553. turn is dispatched.
  3554. BasePlatformAdapter override
  3555. """
  3556. session_key = (
  3557. self._session_key_for(event.source) if event.source is not None else ""
  3558. )
  3559. if session_key and event.message_id:
  3560. self._processing.pop(session_key, None)
  3561. if not self.chatto_config.reactions.value:
  3562. return
  3563. chat_id, message_id = self._event_room_and_message_id(event)
  3564. if not chat_id or not message_id:
  3565. return
  3566. # Remove the processing eyes reaction
  3567. await self.remove_reaction(chat_id, message_id, "👀")
  3568. # Add the outcome reaction
  3569. if outcome == ProcessingOutcome.SUCCESS:
  3570. await self.add_reaction(chat_id, message_id, "✅")
  3571. elif outcome == ProcessingOutcome.FAILURE:
  3572. await self.add_reaction(chat_id, message_id, "❌")
  3573. elif outcome == ProcessingOutcome.CANCELLED:
  3574. await self.add_reaction(chat_id, message_id, "🚫")
  3575. # ------------------------------------------------------------------ #
  3576. # Asset upload (chunked)
  3577. # ------------------------------------------------------------------ #
  3578. async def _upload_asset(self, room_id: str, file_path: str) -> str | None:
  3579. """Upload a file via the chunked AssetUploadService.
  3580. Returns the asset ID on success, or None on failure.
  3581. """
  3582. try:
  3583. file_data = await asyncio.to_thread(_read_file_bytes, file_path)
  3584. except Exception as e:
  3585. logger.error("Chatto: failed to read file %s — %s", file_path, e)
  3586. return None
  3587. if not file_data:
  3588. logger.error("Chatto: file %s is empty", file_path)
  3589. return None
  3590. file_size = len(file_data)
  3591. file_name = os.path.basename(file_path)
  3592. mime_type = mimetypes.guess_type(file_path)[0] or "application/octet-stream"
  3593. sha256_hash = hashlib.sha256(file_data).hexdigest()
  3594. client = await self._get_chatto_client()
  3595. if client is None:
  3596. logger.error("Chatto: upload aborted - no client available")
  3597. return None
  3598. try:
  3599. # Step 1: Create upload session
  3600. upload = await client.create_upload(
  3601. room_id=room_id,
  3602. filename=file_name,
  3603. size=file_size,
  3604. sha256=sha256_hash,
  3605. content_type=mime_type,
  3606. )
  3607. # AssetUpload names this upload_id, not id — reading it through an
  3608. # untyped getattr default is what let the mismatch reach production.
  3609. upload_id = upload.upload_id
  3610. if not upload_id:
  3611. logger.error("Chatto: CreateUpload returned no upload ID")
  3612. return None
  3613. # Step 2: Upload chunks
  3614. offset = 0
  3615. while offset < file_size:
  3616. chunk = file_data[offset : offset + ChattoConstants.UPLOAD_CHUNK_SIZE]
  3617. chunk_sha256 = hashlib.sha256(chunk).hexdigest()
  3618. await client.upload_chunk(
  3619. upload_id=upload_id,
  3620. offset=offset,
  3621. content=chunk,
  3622. chunk_sha256=chunk_sha256,
  3623. )
  3624. offset += len(chunk)
  3625. # Step 3: Complete upload
  3626. upload, asset = await client.complete_upload(upload_id=upload_id)
  3627. if not asset:
  3628. logger.error("Chatto: CompleteUpload returned no asset")
  3629. return None
  3630. logger.info(
  3631. "Chatto: uploaded %s as asset %s (%d bytes)",
  3632. file_name,
  3633. asset.id,
  3634. file_size,
  3635. )
  3636. return asset.id
  3637. except ChattoError as e:
  3638. logger.error("Chatto: upload failed: %s", e)
  3639. return None
  3640. except Exception as e:
  3641. logger.error("Chatto: upload error: %s", e)
  3642. return None
  3643. async def _post_attachment_message(
  3644. self,
  3645. chat_id: str,
  3646. asset_ids: list[str],
  3647. caption: str | None,
  3648. reply_to: str | None,
  3649. metadata: dict[str, Any] | None,
  3650. ) -> SendResult:
  3651. """Post one message carrying already-uploaded assets.
  3652. Threading follows the same rules as a text send
  3653. (:meth:`_resolve_outbound_thread`), so with ``auto_thread`` disabled an
  3654. attachment reply lands in the room like its text counterpart instead of
  3655. quietly opening a thread.
  3656. """
  3657. thread_id = self._resolve_outbound_thread(chat_id, reply_to, metadata)
  3658. client = await self._get_chatto_client()
  3659. if client is None:
  3660. return SendResult(
  3661. success=False, error="Chatto client not available", retryable=True
  3662. )
  3663. try:
  3664. msg = await client.post_message(
  3665. room_id=str(chat_id),
  3666. body=self.format_message(caption) if caption else "",
  3667. attachment_asset_ids=asset_ids,
  3668. thread_root_event_id=thread_id or "",
  3669. )
  3670. self._mark_seen(msg.id)
  3671. return SendResult(success=True, message_id=msg.id)
  3672. except ChattoError as e:
  3673. # Same classification as the chunked text path: server-side
  3674. # failures retry, anything else is ours and must not loop.
  3675. return SendResult(success=False, error=str(e), retryable=True)
  3676. except Exception as e:
  3677. return SendResult(success=False, error=str(e), retryable=False)
  3678. async def _send_local_file_as_attachment(
  3679. self,
  3680. chat_id: str,
  3681. file_path: str,
  3682. caption: str | None,
  3683. reply_to: str | None,
  3684. metadata: dict[str, Any] | None,
  3685. *,
  3686. kind: str,
  3687. ) -> SendResult:
  3688. """Upload a local file and post it as a native Chatto attachment.
  3689. Shared by ``send_image_file``/``send_document``/``send_video``/
  3690. ``send_voice`` — the upload mechanics are identical, only the wording of
  3691. the failure notice differs. On failure we send that notice as text and
  3692. never the host path (it leaks the Hermes home layout).
  3693. """
  3694. notice = f"⚠️ Couldn't deliver the {kind} attachment."
  3695. safe_path = self.validate_media_delivery_path(file_path)
  3696. if not safe_path:
  3697. logger.warning(
  3698. "[%s] send %s: unsafe path %s",
  3699. self.name,
  3700. kind,
  3701. file_path,
  3702. )
  3703. text = f"{caption}\n{notice}" if caption else notice
  3704. return await self.send(chat_id, text, reply_to=reply_to, metadata=metadata)
  3705. asset_id = await self._upload_asset(str(chat_id), safe_path)
  3706. if not asset_id:
  3707. logger.warning(
  3708. "[%s] send %s: upload failed for %s",
  3709. self.name,
  3710. kind,
  3711. safe_path,
  3712. )
  3713. text = f"{caption}\n{notice}" if caption else notice
  3714. return await self.send(chat_id, text, reply_to=reply_to, metadata=metadata)
  3715. return await self._post_attachment_message(
  3716. chat_id,
  3717. [asset_id],
  3718. caption,
  3719. reply_to,
  3720. metadata,
  3721. )
  3722. async def send_image_file(
  3723. self,
  3724. chat_id: str,
  3725. image_path: str,
  3726. caption: str | None = None,
  3727. reply_to: str | None = None,
  3728. metadata: dict[str, Any] | None = None,
  3729. **kwargs,
  3730. ) -> SendResult:
  3731. """Send a local image file via the chunked upload API.
  3732. The parameter is ``image_path``, not ``file_path``: every caller passes
  3733. it by keyword (``gateway/run.py:22354``, ``:22470``, and the base class's
  3734. own ``send_multiple_images`` file:// branch), so a renamed parameter
  3735. makes each of those raise TypeError and silently degrade to a text
  3736. notice.
  3737. BasePlatformAdapter override
  3738. """
  3739. return await self._send_local_file_as_attachment(
  3740. chat_id,
  3741. image_path,
  3742. caption,
  3743. reply_to,
  3744. metadata,
  3745. kind="image",
  3746. )
  3747. async def send_document(
  3748. self,
  3749. chat_id: str,
  3750. file_path: str,
  3751. caption: str | None = None,
  3752. file_name: str | None = None,
  3753. reply_to: str | None = None,
  3754. metadata: dict[str, Any] | None = None,
  3755. **kwargs,
  3756. ) -> SendResult:
  3757. """Send a local file as a native Chatto attachment.
  3758. ``file_name`` exists in the base-class signature and is accepted for
  3759. compatibility, but Chatto takes the recipient-visible filename from
  3760. the upload session (derived from the local path); failures are logged
  3761. and noticed by ``_send_local_file_as_attachment`` itself.
  3762. BasePlatformAdapter override
  3763. """
  3764. return await self._send_local_file_as_attachment(
  3765. chat_id,
  3766. file_path,
  3767. caption,
  3768. reply_to,
  3769. metadata,
  3770. kind="file",
  3771. )
  3772. async def send_video(
  3773. self,
  3774. chat_id: str,
  3775. video_path: str,
  3776. caption: str | None = None,
  3777. reply_to: str | None = None,
  3778. metadata: dict[str, Any] | None = None,
  3779. **kwargs,
  3780. ) -> SendResult:
  3781. """Send a local video as a native Chatto attachment.
  3782. Chatto transcodes and plays it inline.
  3783. BasePlatformAdapter override
  3784. """
  3785. return await self._send_local_file_as_attachment(
  3786. chat_id,
  3787. video_path,
  3788. caption,
  3789. reply_to,
  3790. metadata,
  3791. kind="video",
  3792. )
  3793. async def send_voice(
  3794. self,
  3795. chat_id: str,
  3796. audio_path: str,
  3797. caption: str | None = None,
  3798. reply_to: str | None = None,
  3799. metadata: dict[str, Any] | None = None,
  3800. **kwargs,
  3801. ) -> SendResult:
  3802. """Send a local audio file as a native Chatto attachment.
  3803. Chatto has no dedicated voice-bubble type, so this is an ordinary audio
  3804. attachment — still far better than the base class's text notice.
  3805. BasePlatformAdapter override
  3806. """
  3807. return await self._send_local_file_as_attachment(
  3808. chat_id,
  3809. audio_path,
  3810. caption,
  3811. reply_to,
  3812. metadata,
  3813. kind="audio",
  3814. )
  3815. async def send_image(
  3816. self,
  3817. chat_id: str,
  3818. image_url: str,
  3819. caption: str | None = None,
  3820. reply_to: str | None = None,
  3821. metadata: dict[str, Any] | None = None,
  3822. ) -> SendResult:
  3823. """Send an image to a Chatto room.
  3824. Materialises the URL (size-capped download, like every inbound
  3825. attachment) and uploads it as a native attachment. Falls back to the
  3826. plain URL as a link — Chatto renders link previews — when the URL
  3827. cannot be materialised or the post after a successful upload fails.
  3828. An upload failure needs no fallback on top: the text notice of
  3829. ``_send_local_file_as_attachment`` has already gone out.
  3830. BasePlatformAdapter override
  3831. """
  3832. link_text = f"{caption}\n{image_url}" if caption else image_url
  3833. path, is_temp = await self._materialise_image(image_url)
  3834. if path is not None:
  3835. try:
  3836. result = await self._send_local_file_as_attachment(
  3837. chat_id, path, caption, reply_to, metadata, kind="image"
  3838. )
  3839. finally:
  3840. if is_temp:
  3841. try:
  3842. os.unlink(path)
  3843. except OSError:
  3844. pass
  3845. if result.success:
  3846. return result
  3847. return await self.send(chat_id, link_text, reply_to=reply_to, metadata=metadata)
  3848. async def _materialise_image(self, image_url: str) -> tuple[str | None, bool]:
  3849. """Resolve one ``send_multiple_images`` entry to a local file path.
  3850. Accepts ``http(s)://`` URLs (downloaded to a temp file), ``file://``
  3851. URIs and bare paths. Returns ``(path, is_temp)`` — the caller unlinks
  3852. when ``is_temp``. ``(None, False)`` means the entry is unusable.
  3853. """
  3854. if image_url.startswith(("http://", "https://")):
  3855. ext = os.path.splitext(urlsplit(image_url).path)[1] or ".png"
  3856. tmp_fd, tmp_path = tempfile.mkstemp(suffix=ext, prefix="chatto_img_")
  3857. os.close(tmp_fd)
  3858. try:
  3859. data = await self._download_attachment_bytes(image_url)
  3860. await asyncio.to_thread(_write_file_bytes, tmp_path, data)
  3861. except Exception as e:
  3862. logger.warning("Chatto: image download failed for %s: %s", image_url, e)
  3863. try:
  3864. os.unlink(tmp_path)
  3865. except OSError:
  3866. pass
  3867. return None, False
  3868. return tmp_path, True
  3869. local = image_url
  3870. if local.startswith("file://"):
  3871. local = unquote(urlsplit(local).path)
  3872. return self.validate_media_delivery_path(local), False
  3873. async def send_multiple_images(
  3874. self,
  3875. chat_id: str,
  3876. images: list[tuple[str, str]],
  3877. metadata: dict[str, Any] | None = None,
  3878. human_delay: float = 0.0,
  3879. ) -> SendResult:
  3880. """Send a batch of images as ONE message with several attachments.
  3881. The base implementation posts each image separately; a Chatto message
  3882. carries a list of attachment assets, so a batch belongs in a single
  3883. message (and a single notification).
  3884. ``human_delay`` is ignored deliberately — there is only one outbound
  3885. call to pace. Entries that can't be fetched are dropped with a warning;
  3886. if nothing survives, we fall back to the base class so the user still
  3887. gets the links.
  3888. BasePlatformAdapter override
  3889. """
  3890. if len(images or []) < 2:
  3891. return await super().send_multiple_images(
  3892. chat_id,
  3893. images,
  3894. metadata=metadata,
  3895. human_delay=human_delay,
  3896. )
  3897. asset_ids: list[str] = []
  3898. captions: list[str] = []
  3899. for image_url, alt_text in images:
  3900. path, is_temp = await self._materialise_image(image_url)
  3901. if not path:
  3902. logger.warning("Chatto: skipping unusable image %s", image_url)
  3903. continue
  3904. try:
  3905. asset_id = await self._upload_asset(str(chat_id), path)
  3906. finally:
  3907. if is_temp:
  3908. try:
  3909. os.unlink(path)
  3910. except OSError:
  3911. pass
  3912. if not asset_id:
  3913. logger.warning("Chatto: upload failed for image %s", image_url)
  3914. continue
  3915. asset_ids.append(asset_id)
  3916. if alt_text:
  3917. captions.append(alt_text)
  3918. if not asset_ids:
  3919. logger.warning(
  3920. "Chatto: no image survived upload, falling back to per-image delivery",
  3921. )
  3922. return await super().send_multiple_images(
  3923. chat_id,
  3924. images,
  3925. metadata=metadata,
  3926. human_delay=human_delay,
  3927. )
  3928. if len(asset_ids) < len(images):
  3929. logger.warning(
  3930. "Chatto: sending %d of %d images — the rest could not be uploaded",
  3931. len(asset_ids),
  3932. len(images),
  3933. )
  3934. return await self._post_attachment_message(
  3935. chat_id,
  3936. asset_ids,
  3937. "\n".join(captions) or None,
  3938. None,
  3939. metadata,
  3940. )
  3941. # ---------------------------------------------------------------------------
  3942. # Cron / out-of-process delivery
  3943. # ---------------------------------------------------------------------------
  3944. async def hermes_standalone_sender_fn(
  3945. pconfig: PlatformConfig,
  3946. chat_id: str,
  3947. message: str,
  3948. *,
  3949. thread_id=None,
  3950. media_files=None,
  3951. force_document=False,
  3952. ) -> SendResult:
  3953. """Deliver a message to Chatto without a running gateway adapter. Do not modify signature.
  3954. Used by cron / scheduled routines that run out-of-process. Creates a
  3955. short-lived chattolib client, posts, and closes. Long messages are
  3956. normalised and split like ``send()`` does, so cron output cannot die on
  3957. the server's per-message limit.
  3958. """
  3959. chatto_config: ChattoConfiguration = ChattoConfiguration(pconfig=pconfig)
  3960. # Create a temporary client for standalone sending — needs a base URL and token.
  3961. has_token = bool(chatto_config.token.value)
  3962. if not chatto_config.base_url.value or not has_token:
  3963. return SendResult(success=False, error="Chatto: base URL or token missing")
  3964. client = ChattoClient(
  3965. base_url=chatto_config.base_url.value, token=chatto_config.token.value
  3966. )
  3967. try:
  3968. kwargs: dict[str, Any] = {}
  3969. if chatto_config.auto_thread.value and thread_id:
  3970. kwargs["thread_root_event_id"] = thread_id
  3971. if media_files and media_files.get("attachment_asset_ids"):
  3972. kwargs["attachment_asset_ids"] = list(media_files["attachment_asset_ids"])
  3973. formatted = _normalise_outbound_text(message)
  3974. chunks = BasePlatformAdapter.truncate_message(
  3975. formatted, ChattoConstants.SPLIT_THRESHOLD
  3976. )
  3977. message_ids: list[str] = []
  3978. try:
  3979. for chunk in chunks:
  3980. posted = await client.post_message(chat_id, chunk, **kwargs)
  3981. message_ids.append(posted.id)
  3982. except Exception as exc:
  3983. if message_ids:
  3984. logger.warning(
  3985. "Chatto standalone: sent %d/%d chunk(s) to %s before failing: %s",
  3986. len(message_ids),
  3987. len(chunks),
  3988. chat_id,
  3989. exc,
  3990. )
  3991. # Same classification as the adapter's send paths: only
  3992. # server-side failures read as retryable.
  3993. return SendResult(
  3994. success=False,
  3995. error=str(exc),
  3996. retryable=isinstance(exc, ChattoError),
  3997. )
  3998. return SendResult(success=True, message_id=message_ids[0])
  3999. finally:
  4000. try:
  4001. await client.close()
  4002. except Exception as exc:
  4003. logger.warning(
  4004. "Chatto standalone: error closing short-lived client (perhaps already closed): %s",
  4005. exc,
  4006. )
  4007. # Resolved Chatto configuration — populated by ``hermes_validate_config`` so
  4008. # plugin tools can access it without re-reading env vars or duplicating
  4009. # ``ChattoConfiguration`` logic.
  4010. #
  4011. # Keyed by the profile the snapshot was validated for, not stored in one slot:
  4012. # Hermes multiplexes several profiles through a single gateway process and runs
  4013. # every profile's own validate_config (run_adapters.py, one _create_adapter per
  4014. # profile), so a single global would be whichever profile validated last — and a
  4015. # routed cron fire or turn for another profile would then send with the wrong
  4016. # profile's bot token. The key is the same home key the platform registry scopes
  4017. # its own entries by, so both agree on what "the serving profile" means.
  4018. _resolved_chatto_configs: dict[str, ChattoConfiguration] = {}
  4019. def _profile_scope_key() -> str:
  4020. """Registry key of the profile being served right now.
  4021. Falls back to a constant when ``hermes_constants`` is unavailable (a bare
  4022. standalone sender process): one profile there, so one key loses nothing.
  4023. """
  4024. try:
  4025. from hermes_constants import hermes_home_key
  4026. return hermes_home_key()
  4027. except Exception:
  4028. logger.debug("Chatto: no profile scope key available", exc_info=True)
  4029. return ""
  4030. def _realtime_cursor_file() -> Path | None:
  4031. """Where a restart-resumable realtime cursor lives, or None without Hermes.
  4032. Under the profile's own home so two profiles never read each other's
  4033. position; ``None`` (a bare process with no ``hermes_constants``) simply
  4034. means nothing is persisted, which is the same behaviour as no config.
  4035. """
  4036. try:
  4037. from hermes_constants import get_hermes_home
  4038. except ImportError:
  4039. return None
  4040. return Path(get_hermes_home()) / "state" / "chatto_realtime_cursor.json"
  4041. def _restore_realtime_cursor(cfg: ChattoConfiguration) -> str | None:
  4042. """The stored realtime cursor, if it is this profile's and still fresh.
  4043. Three ways a stored value is refused instead of trusted: a different
  4044. profile wrote it, it carries a shape this build does not understand, or it
  4045. is older than ``resume_window``. In every case the stream starts at the
  4046. live boundary, which is what the plugin did before the cursor was stored at
  4047. all — so a bad file costs the gap it was meant to close, never a crash.
  4048. """
  4049. window = cfg.resume_window.value
  4050. if window <= 0:
  4051. return None
  4052. path = _realtime_cursor_file()
  4053. if path is None:
  4054. return None
  4055. try:
  4056. payload = json.loads(path.read_text(encoding="utf-8"))
  4057. except (OSError, ValueError):
  4058. return None
  4059. if not isinstance(payload, dict):
  4060. return None
  4061. if payload.get("scope") != _profile_scope_key():
  4062. return None
  4063. cursor = payload.get("cursor")
  4064. saved_at = payload.get("saved_at")
  4065. if not isinstance(cursor, str) or not cursor or not isinstance(saved_at, str):
  4066. return None
  4067. try:
  4068. saved = datetime.fromisoformat(saved_at)
  4069. except ValueError:
  4070. return None
  4071. if saved.tzinfo is None:
  4072. saved = saved.replace(tzinfo=UTC)
  4073. if (datetime.now(UTC) - saved).total_seconds() > window:
  4074. return None
  4075. return cursor
  4076. def _persist_realtime_cursor(cfg: ChattoConfiguration, cursor: str | None) -> None:
  4077. """Store the cursor of the last applied event, best-effort.
  4078. Written only on a clean stop, not per event: a deploy or restart should not
  4079. lose the gap, while a crash is allowed to — the alternative is a file write
  4080. on every presence change. A failed write is logged and otherwise ignored,
  4081. because losing the resume point is not a reason to fail a shutdown.
  4082. """
  4083. if not cursor or cfg.resume_window.value <= 0:
  4084. return
  4085. path = _realtime_cursor_file()
  4086. if path is None:
  4087. return
  4088. payload = {
  4089. "scope": _profile_scope_key(),
  4090. "cursor": cursor,
  4091. "saved_at": datetime.now(UTC).isoformat(),
  4092. }
  4093. try:
  4094. path.parent.mkdir(parents=True, exist_ok=True)
  4095. path.write_text(json.dumps(payload), encoding="utf-8")
  4096. except OSError as exc:
  4097. logger.debug("Chatto: realtime cursor not persisted: %s", exc)
  4098. # How similar an ``extra`` key must be to a declared config_key before the
  4099. # unknown-key check reads it as a likely typo or renamed field.
  4100. _NEAR_MISS_RATIO = 0.75
  4101. def _warn_on_unknown_extra_keys(config: PlatformConfig) -> None:
  4102. """Warn about ``extra`` keys that look like misspelled config fields.
  4103. A typo'd key silently resolves to the default otherwise — the warning is
  4104. the only thing telling the user their ``require_mention_channles`` never
  4105. reached us. Only near-matches against our declared config_keys are
  4106. flagged (difflib similarity): keys that resemble nothing of ours are
  4107. either placed there by the Hermes gateway itself (the shared-key loop in
  4108. load_gateway_config() bridges reply_in_thread & co. into every platform's
  4109. extra, hardcoded inline upstream — nothing to import) or are deliberate
  4110. pass-throughs, and flagging those would just train users to ignore us.
  4111. Internal markers (``_enabled_explicit``) are skipped outright.
  4112. """
  4113. extra: dict[str, Any] = getattr(config, "extra", None) or {}
  4114. known = [field.config_key for field in ChattoConfiguration.fields()]
  4115. for key in sorted(extra):
  4116. if key.startswith("_") or key in known:
  4117. continue
  4118. best = max(known, key=lambda k: SequenceMatcher(None, key, k).ratio())
  4119. if SequenceMatcher(None, key, best).ratio() >= _NEAR_MISS_RATIO:
  4120. logger.warning(
  4121. "Chatto: 'extra' key '%s' matches no config field — did you mean '%s'?",
  4122. key,
  4123. best,
  4124. )
  4125. def hermes_validate_config(config: PlatformConfig) -> bool:
  4126. """Check whether Chatto Plugin is configured.
  4127. Function name should be the same as register argument name with "hermes_" prefix, so we
  4128. know that it is needed for plugin register(). Do not change signature.
  4129. Takes ``config``. Compare to hermes_is_connected().
  4130. """
  4131. resolved = ChattoConfiguration(pconfig=config)
  4132. _warn_on_unknown_extra_keys(config)
  4133. if len(resolved.allowed_users.value) > 0 and resolved.allow_all_users.value:
  4134. logger.error(
  4135. "Chatto: Conflicting configuration. Either use 'allowed_users' or 'allow_all_users' but not both."
  4136. )
  4137. return False
  4138. require_rooms = set(resolved.require_mention_rooms.value)
  4139. optional_rooms = set(resolved.optional_mention_rooms.value)
  4140. overlap = sorted(require_rooms & optional_rooms)
  4141. if overlap:
  4142. logger.error(
  4143. "Chatto: Conflicting configuration. Room(s) %s are on both "
  4144. "'require_mention_rooms' and 'optional_mention_rooms' — "
  4145. "each room must appear in at most one of them.",
  4146. ", ".join(overlap),
  4147. )
  4148. return False
  4149. if resolved.resume_window.value < 0:
  4150. logger.error(
  4151. "Chatto: 'resume_window' must be 0 (disabled) or a positive number "
  4152. "of seconds, not %s.",
  4153. resolved.resume_window.value,
  4154. )
  4155. return False
  4156. _by_room, malformed_toolsets = _parse_room_toolsets(resolved.room_toolsets.value)
  4157. if malformed_toolsets:
  4158. logger.error(
  4159. "Chatto: 'room_toolsets' entries must look like 'room_id:toolset' — "
  4160. "these could not be read: %s",
  4161. ", ".join(repr(entry) for entry in malformed_toolsets),
  4162. )
  4163. return False
  4164. # base_url always resolves (it defaults to ChattoHQ), so the only real
  4165. # question is whether a token came in.
  4166. if resolved.token.value is None:
  4167. logger.error("Chatto: CHATTO_TOKEN must be set.")
  4168. return False
  4169. # Publish for the plugin tools only now: a config we just rejected is not
  4170. # one they should send with, and a rejected call must not overwrite a
  4171. # snapshot an earlier, accepted call left behind.
  4172. _resolved_chatto_configs[_profile_scope_key()] = resolved
  4173. return True
  4174. def hermes_check_fn() -> bool:
  4175. """Report whether the vendored chattolib dependency can be imported."""
  4176. try:
  4177. from chattolib import (
  4178. client, # noqa: F401 — vendored dependency probe
  4179. )
  4180. return True
  4181. except ImportError:
  4182. return False
  4183. # ---------------------------------------------------------------------------
  4184. # is_connected probe
  4185. # ---------------------------------------------------------------------------
  4186. def hermes_is_connected(config: PlatformConfig) -> bool:
  4187. """Report whether the Chatto platform is configured and enabled.
  4188. The name is fixed by the register() contract, but despite what it
  4189. suggests this does not open a connection — it validates configuration
  4190. only (see :func:`hermes_validate_config`); the gateway probes real
  4191. connectivity through ``connect()``.
  4192. """
  4193. return bool(hermes_validate_config(config) and config.enabled)
  4194. def hermes_setup_fn() -> None:
  4195. """Interactive setup wizard for Chatto. Is called by and only works in Hermes CLI context.
  4196. Function name should be the same as register argument name with "hermes_" prefix, so we
  4197. know that it is needed for plugin register().
  4198. """
  4199. from hermes_cli.setup import (
  4200. print_success,
  4201. prompt,
  4202. prompt_yes_no,
  4203. save_env_value,
  4204. )
  4205. url = prompt(
  4206. "Chatto server URL (e.g. https://chat.example.com) or leave blank for default ChattoHQ on chat.chatto.run:"
  4207. )
  4208. if url:
  4209. save_env_value(ChattoConfiguration.base_url.env_name, url)
  4210. token = prompt("Chatto bot token (cht_BK_...):", password=True)
  4211. if token:
  4212. save_env_value(ChattoConfiguration.token.env_name, token)
  4213. home = prompt("Home room ID for notifications (or empty):")
  4214. if home:
  4215. save_env_value(ChattoConfiguration.home_channel.env_name, home)
  4216. allow_all = prompt_yes_no("Allow all users to talk? (true/false):")
  4217. if allow_all:
  4218. save_env_value(ChattoConfiguration.allow_all_users.env_name, str(allow_all))
  4219. print_success("\n✓ Chatto configured. Restart the gateway to activate.")
  4220. def hermes_env_enablement_fn() -> dict:
  4221. """Seed PlatformConfig.extra from the CHATTO_* environment variables.
  4222. Returns a dict compatible with the PlatformConfig merge hook, holding
  4223. exactly the values that are actually set via env — never fabricated
  4224. defaults. Called by the platform registry during load_gateway_config(),
  4225. which commits this dict onto the platform's ``extra`` verbatim; seeding a
  4226. value that was not configured would overwrite what the user set in
  4227. config.yaml (e.g. a YAML base_url clobbered by the ChattoHQ default).
  4228. "Nothing set" therefore stays the adapter's business: every ConfigField
  4229. falls back to its declared default (base_url to ChattoHQ) on its own.
  4230. Seed keys are the config.yaml "extra" keys (config_key), NOT the env var
  4231. names — ChattoConfiguration reads extra[config_key]. The special
  4232. 'home_channel' key is extracted by the gateway and becomes a proper
  4233. HomeChannel dataclass on the PlatformConfig; every other key is merged
  4234. into PlatformConfig.extra.
  4235. Function name should be the same as register argument name with "hermes_" prefix, so we
  4236. know that it is needed for plugin register().
  4237. """
  4238. seed: dict[str, Any] = {}
  4239. for config_field in ChattoConfiguration.fields():
  4240. env_value = os.getenv(config_field.env_name)
  4241. if env_value:
  4242. seed[config_field.config_key] = env_value.strip()
  4243. logger.debug("seed: %s", {k: v for k, v in seed.items() if k != "token"})
  4244. return seed
  4245. # ---------------------------------------------------------------------------
  4246. # Plugin registration entry point
  4247. # ---------------------------------------------------------------------------
  4248. # What each capability is called in the startup banner, keyed by the method
  4249. # that implements it. Derived from real overrides rather than hard-coded, so
  4250. # dropping a method drops its claim from the log instead of leaving a lie.
  4251. _CAPABILITY_LABELS = {
  4252. "send": "text",
  4253. "send_image_file": "images",
  4254. "send_multiple_images": "image batches (bundled into one message)",
  4255. "send_video": "video",
  4256. "send_voice": "voice messages",
  4257. "send_document": "documents",
  4258. "add_reaction": "reactions",
  4259. "edit_message": "message editing",
  4260. "delete_message": "message deletion",
  4261. "send_typing": "typing indicators",
  4262. "create_handoff_thread": "threads",
  4263. "create_room": "room creation",
  4264. "toolsets_for_source": "per-room toolsets (CHATTO_ROOM_TOOLSETS)",
  4265. }
  4266. def _capabilities() -> list[str]:
  4267. """Name the things this adapter genuinely implements itself.
  4268. A capability counts only when ChattoAdapter overrides the base method —
  4269. inheriting BasePlatformAdapter's fallback means the feature is not
  4270. natively supported, and announcing it would mislead.
  4271. """
  4272. found = [
  4273. label
  4274. for name, label in _CAPABILITY_LABELS.items()
  4275. if getattr(ChattoAdapter, name, None)
  4276. is not getattr(BasePlatformAdapter, name, None)
  4277. ]
  4278. if ChattoAdapter.supports_code_blocks:
  4279. found.append("code blocks")
  4280. if ChattoAdapter.supports_status_text:
  4281. found.append("custom status text")
  4282. found.append("presence (refreshed while connected)")
  4283. return found
  4284. # --------------------------------------------------------------------------- #
  4285. # Plugin tools: send DM to a user, or send to a room
  4286. # --------------------------------------------------------------------------- #
  4287. class _ToolRefusal(Exception):
  4288. """A tool step that ends the call with a result for the model.
  4289. The rendered JSON travels on the exception, so every early exit reads the
  4290. same to Hermes and none of them escapes as an unhandled error.
  4291. """
  4292. def __init__(self, result_json: str) -> None:
  4293. super().__init__(result_json)
  4294. self.result_json = result_json
  4295. def _refuse(error: str, **extra: Any) -> _ToolRefusal:
  4296. """Build a refusal result, so every early exit agrees on the shape."""
  4297. return _ToolRefusal(json.dumps({"success": False, "error": error, **extra}))
  4298. def _int_arg(args: dict, key: str, default: int, *, low: int, high: int) -> int:
  4299. """An integer tool argument, validated — a refusal rather than a guess.
  4300. The value comes from a model, so "twenty" and a float are both reachable,
  4301. and an unvalidated ``int()`` would raise out of the handler instead of
  4302. answering. An absent argument takes the default; a *wrong* one is refused,
  4303. which also keeps a falsy ``0`` from silently becoming the default and
  4304. quietly widening the answer.
  4305. """
  4306. raw = args.get(key)
  4307. if raw is None or raw == "":
  4308. return default
  4309. not_a_number = f"{key} must be a whole number between {low} and {high}"
  4310. try:
  4311. value = int(raw)
  4312. except (TypeError, ValueError) as exc:
  4313. raise _refuse(not_a_number) from exc
  4314. if isinstance(raw, float) and not raw.is_integer():
  4315. # int() would truncate 3.5 to 3, answering a question nobody asked.
  4316. raise _refuse(not_a_number)
  4317. if value < low or value > high:
  4318. raise _refuse(f"{key} must be between {low} and {high}")
  4319. return value
  4320. def _timestamp_arg(args: dict, key: str) -> datetime | None:
  4321. """An ISO-8601 tool argument as a datetime, or a refusal.
  4322. Search bounds are the first date arguments these tools take, so they get
  4323. the same treatment as numbers: an absent value means "no bound", and a
  4324. wrong one is refused rather than silently dropped — a caller who mistyped
  4325. a date is better served by hearing it than by getting an unbounded search.
  4326. A date without a time is midnight, and a value without an offset is read as
  4327. UTC, matching how everything else here treats a bare timestamp.
  4328. """
  4329. raw = args.get(key)
  4330. if raw is None or raw == "":
  4331. return None
  4332. if isinstance(raw, datetime):
  4333. return raw if raw.tzinfo is not None else raw.replace(tzinfo=UTC)
  4334. try:
  4335. parsed = datetime.fromisoformat(str(raw).strip())
  4336. except ValueError as exc:
  4337. raise _refuse(
  4338. f"{key} must be an ISO-8601 date or timestamp, e.g. '2026-01-31' "
  4339. "or '2026-01-31T12:00:00Z'"
  4340. ) from exc
  4341. return parsed if parsed.tzinfo is not None else parsed.replace(tzinfo=UTC)
  4342. def _enum_arg(args: dict, key: str, mapping: dict[str, Any], default: Any) -> Any:
  4343. """A named tool argument resolved through ``mapping``, or a refusal.
  4344. The choices are spelled out in the schema's enum, so an unknown name means
  4345. the caller invented one; picking a default instead would answer a
  4346. different question than the one asked.
  4347. """
  4348. raw = args.get(key)
  4349. if raw is None or raw == "":
  4350. return default
  4351. value = mapping.get(str(raw).strip().lower())
  4352. if value is None:
  4353. raise _refuse(f"{key} must be one of: {', '.join(sorted(mapping))}")
  4354. return value
  4355. @asynccontextmanager
  4356. async def _tool_client(cfg: ChattoConfiguration | None) -> AsyncIterator[ChattoClient]:
  4357. """A short-lived client for one tool call, closed however the call ends.
  4358. Credentials come from the resolved configuration and from nowhere else. A
  4359. tool argument carrying ``base_url`` would send the configured bot token to
  4360. whatever host the caller named, and every message the bot reads is
  4361. something a caller could talk it into repeating.
  4362. """
  4363. if cfg is None:
  4364. raise _refuse("Chatto configuration unavailable")
  4365. token = cfg.token.value or ""
  4366. if not token:
  4367. raise _refuse("Chatto token not configured")
  4368. try:
  4369. client = ChattoClient(
  4370. base_url=cfg.base_url.value or ChattoClient.DEFAULT_BASE_URL, token=token
  4371. )
  4372. except Exception as exc:
  4373. raise _refuse(f"Failed to create Chatto client: {exc}") from exc
  4374. try:
  4375. yield client
  4376. finally:
  4377. try:
  4378. await client.close()
  4379. except Exception as exc: # pragma: no cover — close is best-effort
  4380. logger.debug("Chatto tools: client close failed: %s", exc)
  4381. # Read-tool bounds, named so the schemas and the validation cannot disagree.
  4382. # A chain is walked hop by hop, so its ceiling is a cost bound: each hop is a
  4383. # round trip, and a caller asking for the whole conversation has
  4384. # chatto_get_history to page it. The page size is bounded for the opposite
  4385. # reason — one read must stay inside a model context.
  4386. FOLLOW_REPLY_CHAIN_MAX = 10
  4387. HISTORY_PAGE_SIZE = 20
  4388. HISTORY_PAGE_MAX = 100
  4389. # Read-tool bounds for pinned messages and attachments — one call per page,
  4390. # kept small enough that a model can digest the output without choking.
  4391. PINNED_PAGE_SIZE = 20
  4392. ATTACHMENT_PAGE_SIZE = 40
  4393. # Search pages are bounded the same way, and for the same reason: a hit is
  4394. # something to read, not a dump. The server's own ceiling is 100, and a page
  4395. # may come back short while `next_cursor` is set — stale index hits are dropped
  4396. # after the provider matches them, which is not an error.
  4397. SEARCH_PAGE_SIZE = 20
  4398. SEARCH_PAGE_MAX = 100
  4399. # The model names a search order the way a person would; the proto spells it
  4400. # out. One table maps the two, so the schema's enum and the lookup cannot
  4401. # disagree about what "newest" means.
  4402. _SEARCH_ORDERS = {
  4403. "relevance": MessageSearchOrder.RELEVANCE,
  4404. "newest": MessageSearchOrder.NEWEST,
  4405. "thread_activity": MessageSearchOrder.THREAD_ACTIVITY,
  4406. "thread_created": MessageSearchOrder.THREAD_CREATED,
  4407. }
  4408. _SEARCH_SCOPES = {"followed_threads": MessageSearchScope.FOLLOWED_THREADS}
  4409. _SEARCH_GROUPS = {"thread": MessageSearchGroupBy.THREAD}
  4410. # Why search cannot answer right now, in terms someone can act on. The server
  4411. # returns FAILED_PRECONDITION for "the operator turned this off" and
  4412. # UNAVAILABLE for "the provider is not ready yet" — the second one resolves
  4413. # itself, the first one never does, and the caller should not have to guess
  4414. # which it hit.
  4415. _SEARCH_STATE_HINTS = {
  4416. MessageSearchState.DISABLED: (
  4417. "message search is turned off on this Chatto server; the operator has "
  4418. "to enable it (`search.enabled = true`) and restart"
  4419. ),
  4420. MessageSearchState.STARTING: (
  4421. "the search provider is starting up; try again shortly"
  4422. ),
  4423. MessageSearchState.INDEXING: (
  4424. "the search provider is still building its index from the message "
  4425. "history, so recent results may be missing; try again later"
  4426. ),
  4427. MessageSearchState.DEGRADED: (
  4428. "the search provider is degraded and answering only partially"
  4429. ),
  4430. MessageSearchState.UNAVAILABLE: ("no search provider is reachable for this server"),
  4431. }
  4432. # A self-deleting notice: long enough to read, short enough not to linger.
  4433. # The floor keeps a TTL from deleting a message before anyone sees it; the
  4434. # ceiling keeps a "transient" notice from becoming a day-long fixture.
  4435. EPHEMERAL_TTL_DEFAULT = 60
  4436. EPHEMERAL_TTL_MIN = 5
  4437. EPHEMERAL_TTL_MAX = 3600
  4438. CHATTO_LIST_ROOMS_SCHEMA = {
  4439. "name": "chatto_list_rooms",
  4440. "description": (
  4441. "List the Chatto rooms visible to the bot (channels and DM rooms), with "
  4442. "their room IDs and whether the bot is a member. Check `is_member` before "
  4443. "posting: the bot can see rooms it has not joined, and cannot post in "
  4444. "them. Use this to resolve a channel name to its room ID — e.g. to find "
  4445. "the 'platform:chat_id' address for a cron job's `deliver` target. "
  4446. "Optional `name` filters case-insensitively by substring."
  4447. ),
  4448. "parameters": {
  4449. "type": "object",
  4450. "properties": {
  4451. "name": {
  4452. "type": "string",
  4453. "description": (
  4454. "Optional: case-insensitive substring filter on the room name "
  4455. "(e.g. 'liv' matches 'Liv' and 'Liv-Dev'). Empty lists all rooms."
  4456. ),
  4457. },
  4458. },
  4459. "required": [],
  4460. "additionalProperties": False,
  4461. },
  4462. }
  4463. CHATTO_LIST_ROOM_ATTACHMENTS_SCHEMA = {
  4464. "name": "chatto_list_room_attachments",
  4465. "description": (
  4466. "List the file attachments in a Chatto room (images, documents, etc.). "
  4467. "Returns each attachment's ID, filename, MIME type, size and an optional "
  4468. "thumbnail URL. Optional ``limit`` (default 40) and ``offset`` allow "
  4469. "paginating through large attachment lists."
  4470. ),
  4471. "parameters": {
  4472. "type": "object",
  4473. "properties": {
  4474. "room_id": {
  4475. "type": "string",
  4476. "description": "The Chatto room ID to list attachments in.",
  4477. },
  4478. "room_name": {
  4479. "type": "string",
  4480. "description": (
  4481. "Optional: human-readable room name — one of ``room_id`` or "
  4482. "``room_name`` is required."
  4483. ),
  4484. },
  4485. "limit": {
  4486. "type": "integer",
  4487. "description": (
  4488. f"Maximum attachments to return (default {ATTACHMENT_PAGE_SIZE})."
  4489. ),
  4490. },
  4491. "offset": {
  4492. "type": "integer",
  4493. "description": "Skip this many attachments (for pagination).",
  4494. },
  4495. },
  4496. "required": [],
  4497. "additionalProperties": False,
  4498. },
  4499. }
  4500. CHATTO_LIST_PINNED_MESSAGES_SCHEMA = {
  4501. "name": "chatto_list_pinned_messages",
  4502. "description": (
  4503. "List pinned messages in a Chatto room. Returns each message's event ID, "
  4504. "body, author (user ID and login), and the pin timestamp. Optional "
  4505. "``limit`` (default 20) and ``offset`` allow paginating through long "
  4506. "pin lists."
  4507. ),
  4508. "parameters": {
  4509. "type": "object",
  4510. "properties": {
  4511. "room_id": {
  4512. "type": "string",
  4513. "description": "The Chatto room ID to list pinned messages in.",
  4514. },
  4515. "room_name": {
  4516. "type": "string",
  4517. "description": (
  4518. "Optional: human-readable room name — one of ``room_id`` or "
  4519. "``room_name`` is required."
  4520. ),
  4521. },
  4522. "limit": {
  4523. "type": "integer",
  4524. "description": f"Maximum pinned messages to return (default {PINNED_PAGE_SIZE}).",
  4525. },
  4526. "offset": {
  4527. "type": "integer",
  4528. "description": "Skip this many pinned messages (for pagination).",
  4529. },
  4530. },
  4531. "required": [],
  4532. "additionalProperties": False,
  4533. },
  4534. }
  4535. CHATTO_PIN_MESSAGE_SCHEMA = {
  4536. "name": "chatto_pin_message",
  4537. "description": (
  4538. "Pin or unpin a message in a Chatto room. Use ``pin=true`` to pin a "
  4539. "message (it becomes visible via ``chatto_list_pinned_messages``), or "
  4540. "``pin=false`` to remove an existing pin. Requires the ``event_id`` of "
  4541. "the message to pin."
  4542. ),
  4543. "parameters": {
  4544. "type": "object",
  4545. "properties": {
  4546. "room_id": {
  4547. "type": "string",
  4548. "description": "The Chatto room ID.",
  4549. },
  4550. "room_name": {
  4551. "type": "string",
  4552. "description": (
  4553. "Optional: human-readable room name — one of ``room_id`` or "
  4554. "``room_name`` is required."
  4555. ),
  4556. },
  4557. "event_id": {
  4558. "type": "string",
  4559. "description": "The message event ID to pin or unpin.",
  4560. },
  4561. "pin": {
  4562. "type": "boolean",
  4563. "description": ("True to pin, false to unpin the message."),
  4564. },
  4565. },
  4566. "required": ["event_id", "pin"],
  4567. "additionalProperties": False,
  4568. },
  4569. }
  4570. CHATTO_DELETE_MESSAGE_SCHEMA = {
  4571. "name": "chatto_delete_message",
  4572. "description": (
  4573. "Delete one message in a Chatto room by event ID. Intended for "
  4574. "cleaning up the bot's own messages (e.g. a summary posted into the "
  4575. "wrong room); the server enforces who may delete what — a bot "
  4576. "account can usually delete only its own messages, and the server's "
  4577. "refusal is reported as an error. Deleted messages are gone from the "
  4578. "room for everyone; there is no un-delete."
  4579. ),
  4580. "parameters": {
  4581. "type": "object",
  4582. "properties": {
  4583. "room_id": {
  4584. "type": "string",
  4585. "description": "The Chatto room ID the message lives in.",
  4586. },
  4587. "room_name": {
  4588. "type": "string",
  4589. "description": (
  4590. "Optional: human-readable room name — one of ``room_id`` or "
  4591. "``room_name`` is required."
  4592. ),
  4593. },
  4594. "event_id": {
  4595. "type": "string",
  4596. "description": "The message event ID to delete.",
  4597. },
  4598. },
  4599. "required": ["event_id"],
  4600. "additionalProperties": False,
  4601. },
  4602. }
  4603. CHATTO_EDIT_MESSAGE_SCHEMA = {
  4604. "name": "chatto_edit_message",
  4605. "description": (
  4606. "Edit the text of an already-posted message in a Chatto room, by its "
  4607. "event ID. Use it to correct the bot's own earlier messages — editing "
  4608. "another user's message is refused by the server. The full new text "
  4609. "replaces the old body; content over the per-message limit is refused "
  4610. "rather than truncated."
  4611. ),
  4612. "parameters": {
  4613. "type": "object",
  4614. "properties": {
  4615. "room_id": {
  4616. "type": "string",
  4617. "description": "The Chatto room ID the message lives in.",
  4618. },
  4619. "room_name": {
  4620. "type": "string",
  4621. "description": (
  4622. "Optional: human-readable room name — one of ``room_id`` or "
  4623. "``room_name`` is required."
  4624. ),
  4625. },
  4626. "event_id": {
  4627. "type": "string",
  4628. "description": "The event ID of the message to edit.",
  4629. },
  4630. "message": {
  4631. "type": "string",
  4632. "description": "The full new message text (markdown supported).",
  4633. },
  4634. },
  4635. "required": ["event_id", "message"],
  4636. "additionalProperties": False,
  4637. },
  4638. }
  4639. CHATTO_UPLOAD_FILE_SCHEMA = {
  4640. "name": "chatto_upload_file",
  4641. "description": (
  4642. "Upload a local file (image, video, audio or document) from this "
  4643. "machine and post it into a Chatto room as an attachment, with an "
  4644. "optional caption. Returns the asset ID and the message ID."
  4645. ),
  4646. "parameters": {
  4647. "type": "object",
  4648. "properties": {
  4649. "room_id": {
  4650. "type": "string",
  4651. "description": "The Chatto room ID of the destination.",
  4652. },
  4653. "room_name": {
  4654. "type": "string",
  4655. "description": (
  4656. "Optional: human-readable room name — one of ``room_id`` or "
  4657. "``room_name`` is required."
  4658. ),
  4659. },
  4660. "file_path": {
  4661. "type": "string",
  4662. "description": (
  4663. "Absolute path of the local file to upload (e.g. a file "
  4664. "this session produced earlier)."
  4665. ),
  4666. },
  4667. "caption": {
  4668. "type": "string",
  4669. "description": "Optional text posted alongside the attachment (markdown).",
  4670. },
  4671. },
  4672. "required": ["file_path"],
  4673. "additionalProperties": False,
  4674. },
  4675. }
  4676. CHATTO_ADD_REACTION_SCHEMA = {
  4677. "name": "chatto_add_reaction",
  4678. "description": (
  4679. "Add an emoji reaction to a message in a Chatto room. The emoji is "
  4680. "given as its gemoji shortcode name (e.g. 'white_check_mark', 'tada', "
  4681. "'rocket'); a Unicode emoji is translated when a shortcode is known "
  4682. "for it. Use reactions to acknowledge or rate messages."
  4683. ),
  4684. "parameters": {
  4685. "type": "object",
  4686. "properties": {
  4687. "room_id": {
  4688. "type": "string",
  4689. "description": "The Chatto room ID the message lives in.",
  4690. },
  4691. "room_name": {
  4692. "type": "string",
  4693. "description": (
  4694. "Optional: human-readable room name — one of ``room_id`` or "
  4695. "``room_name`` is required."
  4696. ),
  4697. },
  4698. "event_id": {
  4699. "type": "string",
  4700. "description": "The event ID of the message to react to.",
  4701. },
  4702. "emoji": {
  4703. "type": "string",
  4704. "description": (
  4705. "The reaction as a gemoji shortcode (e.g. 'white_check_mark', "
  4706. "'tada', 'rocket') or a known display emoji."
  4707. ),
  4708. },
  4709. },
  4710. "required": ["event_id", "emoji"],
  4711. "additionalProperties": False,
  4712. },
  4713. }
  4714. CHATTO_REMOVE_REACTION_SCHEMA = {
  4715. "name": "chatto_remove_reaction",
  4716. "description": (
  4717. "Remove the bot's emoji reaction from a message in a Chatto room. The "
  4718. "emoji is given as its gemoji shortcode name (see chatto_add_reaction)."
  4719. ),
  4720. "parameters": {
  4721. "type": "object",
  4722. "properties": {
  4723. "room_id": {
  4724. "type": "string",
  4725. "description": "The Chatto room ID the message lives in.",
  4726. },
  4727. "room_name": {
  4728. "type": "string",
  4729. "description": (
  4730. "Optional: human-readable room name — one of ``room_id`` or "
  4731. "``room_name`` is required."
  4732. ),
  4733. },
  4734. "event_id": {
  4735. "type": "string",
  4736. "description": "The event ID of the message the reaction is on.",
  4737. },
  4738. "emoji": {
  4739. "type": "string",
  4740. "description": "The reaction to remove, as a gemoji shortcode name.",
  4741. },
  4742. },
  4743. "required": ["event_id", "emoji"],
  4744. "additionalProperties": False,
  4745. },
  4746. }
  4747. CHATTO_MARK_ROOM_AS_READ_SCHEMA = {
  4748. "name": "chatto_mark_room_as_read",
  4749. "description": (
  4750. "Mark a Chatto room as read for the bot. Updates the server's read "
  4751. "marker so the room stops counting as unread. Useful after the bot "
  4752. "has fetched a room's context in bulk. Use chatto_mark_thread_as_read "
  4753. "for a single thread instead."
  4754. ),
  4755. "parameters": {
  4756. "type": "object",
  4757. "properties": {
  4758. "room_id": {
  4759. "type": "string",
  4760. "description": "The Chatto room ID to mark read.",
  4761. },
  4762. "room_name": {
  4763. "type": "string",
  4764. "description": (
  4765. "Optional: human-readable room name — one of ``room_id`` or "
  4766. "``room_name`` is required."
  4767. ),
  4768. },
  4769. "up_to_event_id": {
  4770. "type": "string",
  4771. "description": (
  4772. "Optional: mark read only up to this event ID instead of "
  4773. "the room's newest message."
  4774. ),
  4775. },
  4776. },
  4777. "required": [],
  4778. "additionalProperties": False,
  4779. },
  4780. }
  4781. CHATTO_CREATE_ROOM_SCHEMA = {
  4782. "name": "chatto_create_room",
  4783. "description": (
  4784. "Create a new Chatto channel in a room group. Returns the new room's "
  4785. "ID. Room creation is a server-side privileged action — the bot "
  4786. "account needs the room.create permission, and the refusal is "
  4787. "reported when it does not."
  4788. ),
  4789. "parameters": {
  4790. "type": "object",
  4791. "properties": {
  4792. "name": {
  4793. "type": "string",
  4794. "description": "The new room's display name.",
  4795. },
  4796. "group_id": {
  4797. "type": "string",
  4798. "description": (
  4799. "The room group to create the room in "
  4800. "(see chatto_list_room_groups)."
  4801. ),
  4802. },
  4803. "description": {
  4804. "type": "string",
  4805. "description": "Optional room description (markdown).",
  4806. },
  4807. },
  4808. "required": ["name", "group_id"],
  4809. "additionalProperties": False,
  4810. },
  4811. }
  4812. CHATTO_LIST_ROOM_MEMBERS_SCHEMA = {
  4813. "name": "chatto_list_room_members",
  4814. "description": (
  4815. "List a Chatto room's members with their login, display name and "
  4816. "presence, in pages. Use it to find who is in a room before mentioning "
  4817. "them or to resolve a user ID to a login."
  4818. ),
  4819. "parameters": {
  4820. "type": "object",
  4821. "properties": {
  4822. "room_id": {
  4823. "type": "string",
  4824. "description": "The Chatto room ID whose members to list.",
  4825. },
  4826. "room_name": {
  4827. "type": "string",
  4828. "description": (
  4829. "Optional: human-readable room name — one of ``room_id`` or "
  4830. "``room_name`` is required."
  4831. ),
  4832. },
  4833. "limit": {
  4834. "type": "integer",
  4835. "description": "Maximum members per page, 1-100 (default 50).",
  4836. },
  4837. "offset": {
  4838. "type": "integer",
  4839. "description": "Offset into the member list, for the next page.",
  4840. },
  4841. },
  4842. "required": [],
  4843. "additionalProperties": False,
  4844. },
  4845. }
  4846. CHATTO_GET_USER_SCHEMA = {
  4847. "name": "chatto_get_user",
  4848. "description": (
  4849. "Look up a Chatto user in the member directory: display name, login, "
  4850. "presence and roles. Exactly one of ``user_id`` or ``user_login`` is "
  4851. "required."
  4852. ),
  4853. "parameters": {
  4854. "type": "object",
  4855. "properties": {
  4856. "user_id": {
  4857. "type": "string",
  4858. "description": (
  4859. "The Chatto user ID (e.g. 'Uxxxxxxxx'). Exactly one of "
  4860. "`user_id` or `user_login` is required."
  4861. ),
  4862. },
  4863. "user_login": {
  4864. "type": "string",
  4865. "description": (
  4866. "The Chatto login name (e.g. 'nickk'). Exactly one of "
  4867. "`user_id` or `user_login` is required."
  4868. ),
  4869. },
  4870. },
  4871. "required": [],
  4872. "additionalProperties": False,
  4873. },
  4874. }
  4875. CHATTO_SET_PRESENCE_SCHEMA = {
  4876. "name": "chatto_set_presence",
  4877. "description": (
  4878. "Set the bot's presence status in Chatto: 'online', 'away' or "
  4879. "'do_not_disturb'. The status expires server-side after about a "
  4880. "minute without refresh; while the gateway runs it is re-announced "
  4881. "automatically, so an explicit 'away' lasts until the next refresh "
  4882. "cycle unless the server supports preference persistence. 'offline' "
  4883. "cannot be set — presence lapses when the gateway disconnects."
  4884. ),
  4885. "parameters": {
  4886. "type": "object",
  4887. "properties": {
  4888. "status": {
  4889. "type": "string",
  4890. "enum": ["online", "away", "do_not_disturb"],
  4891. "description": "The presence status to announce.",
  4892. },
  4893. },
  4894. "required": ["status"],
  4895. "additionalProperties": False,
  4896. },
  4897. }
  4898. CHATTO_SET_CUSTOM_STATUS_SCHEMA = {
  4899. "name": "chatto_set_custom_status",
  4900. "description": (
  4901. "Set the bot's custom status in Chatto. Shows an emoji and text next "
  4902. "to the bot name in the client (e.g. 🛠️ processing, ⏳ awaiting review). "
  4903. "Optional ``expires_in_seconds`` auto-clears the status; omit or 0 for "
  4904. "manual clearing."
  4905. ),
  4906. "parameters": {
  4907. "type": "object",
  4908. "properties": {
  4909. "emoji": {
  4910. "type": "string",
  4911. "description": (
  4912. "A single emoji to show next to the status text (e.g. 🛠️, ⏳, 🔴)."
  4913. ),
  4914. },
  4915. "text": {
  4916. "type": "string",
  4917. "description": (
  4918. "The status text to display (e.g. 'processing', 'awaiting review')."
  4919. ),
  4920. },
  4921. "expires_in_seconds": {
  4922. "type": "integer",
  4923. "description": (
  4924. "Optional: auto-clear the status after this many seconds. "
  4925. "Omit or 0 to keep the status until cleared."
  4926. ),
  4927. },
  4928. },
  4929. "required": ["emoji", "text"],
  4930. "additionalProperties": False,
  4931. },
  4932. }
  4933. CHATTO_DELETE_CUSTOM_STATUS_SCHEMA = {
  4934. "name": "chatto_delete_custom_status",
  4935. "description": ("Clear the bot's custom status, reverting to no status display."),
  4936. "parameters": {
  4937. "type": "object",
  4938. "properties": {},
  4939. "required": [],
  4940. "additionalProperties": False,
  4941. },
  4942. }
  4943. CHATTO_SEND_DM_SCHEMA = {
  4944. "name": "chatto_send_dm",
  4945. "description": (
  4946. "Send a text message as a **Direct Message** to another Chatto user. "
  4947. "Looks up the user by `user_login` or `user_id` and posts into the DM "
  4948. "room you already have with them. Returns success with the room_id "
  4949. "and message_ids, or an error."
  4950. ),
  4951. "parameters": {
  4952. "type": "object",
  4953. "properties": {
  4954. "user_login": {
  4955. "type": "string",
  4956. "description": (
  4957. "The Chatto login name of the recipient "
  4958. "(e.g. 'nickk'). Exactly one of `user_login` or `user_id` is required."
  4959. ),
  4960. },
  4961. "user_id": {
  4962. "type": "string",
  4963. "description": (
  4964. "The Chatto user ID of the recipient "
  4965. "(e.g. 'Uxxxxxxxx'). Exactly one of `user_login` or `user_id` is required."
  4966. ),
  4967. },
  4968. "message": {
  4969. "type": "string",
  4970. "description": "The message text to send (markdown supported).",
  4971. },
  4972. },
  4973. "required": ["message"],
  4974. "additionalProperties": False,
  4975. },
  4976. }
  4977. CHATTO_FETCH_LINK_PREVIEW_SCHEMA = {
  4978. "name": "chatto_fetch_link_preview",
  4979. "description": (
  4980. "Fetch an OpenGraph link preview for a URL. Returns title, description, "
  4981. "image URL and other metadata that Chatto's server would show when "
  4982. "rendering the link in a message. Passes through whatever metadata the "
  4983. "target site provides."
  4984. ),
  4985. "parameters": {
  4986. "type": "object",
  4987. "properties": {
  4988. "url": {
  4989. "type": "string",
  4990. "description": (
  4991. "The URL to fetch a preview for (e.g. 'https://example.com')."
  4992. ),
  4993. },
  4994. },
  4995. "required": ["url"],
  4996. "additionalProperties": False,
  4997. },
  4998. }
  4999. CHATTO_SEND_ROOM_SCHEMA = {
  5000. "name": "chatto_send_room",
  5001. "description": (
  5002. "Send a text message to a Chatto **room** (channel or DM room) "
  5003. "by its room ID or room name. Channels the bot is configured to "
  5004. "answer in only — a room on neither mention list stays read-only. "
  5005. "Returns success with the message_ids, or an error."
  5006. ),
  5007. "parameters": {
  5008. "type": "object",
  5009. "properties": {
  5010. "room_id": {
  5011. "type": "string",
  5012. "description": (
  5013. "The Chatto room ID of the destination "
  5014. "(e.g. 'RbmwiHk4vs9a3Ql'). Exactly one of `room_id` or `room_name` is required."
  5015. ),
  5016. },
  5017. "room_name": {
  5018. "type": "string",
  5019. "description": (
  5020. "The display name of the room (case-insensitive). Exactly one "
  5021. "of `room_id` or `room_name` is required. If several rooms "
  5022. "share the name, the call returns their IDs instead of guessing."
  5023. ),
  5024. },
  5025. "message": {
  5026. "type": "string",
  5027. "description": "The message text to send (markdown supported).",
  5028. },
  5029. "thread_root_event_id": {
  5030. "type": "string",
  5031. "description": (
  5032. "Optional: event ID of the thread-root message. When given, "
  5033. "this message is posted INSIDE that thread, which has to be "
  5034. "a thread root of the destination room. Mutually exclusive "
  5035. "with `create_thread`."
  5036. ),
  5037. },
  5038. "create_thread": {
  5039. "type": "boolean",
  5040. "description": (
  5041. "Optional: post this message as the ROOT of a new thread "
  5042. "(default false = normal message). Only for channel rooms. "
  5043. "Mutually exclusive with `thread_root_event_id`."
  5044. ),
  5045. },
  5046. },
  5047. "required": ["message"],
  5048. "additionalProperties": False,
  5049. },
  5050. }
  5051. CHATTO_SEND_EPHEMERAL_SCHEMA = {
  5052. "name": "chatto_send_ephemeral",
  5053. "description": (
  5054. "Send a text notice to a Chatto room that deletes itself after a TTL. "
  5055. "Use it for progress or status updates that should not stay in the "
  5056. "timeline. Same room rules as chatto_send_room: a channel the bot is "
  5057. "configured to answer in only. Returns the message_ids and the TTL, or "
  5058. "an error. Deletion is best-effort: a gateway restart before the TTL "
  5059. "expires leaves the notice in the room."
  5060. ),
  5061. "parameters": {
  5062. "type": "object",
  5063. "properties": {
  5064. "room_id": {
  5065. "type": "string",
  5066. "description": (
  5067. "The Chatto room ID of the destination. Exactly one of "
  5068. "`room_id` or `room_name` is required."
  5069. ),
  5070. },
  5071. "room_name": {
  5072. "type": "string",
  5073. "description": (
  5074. "The display name of the room (case-insensitive). Exactly one "
  5075. "of `room_id` or `room_name` is required. If several rooms "
  5076. "share the name, the call returns their IDs instead of guessing."
  5077. ),
  5078. },
  5079. "message": {
  5080. "type": "string",
  5081. "description": "The notice text (markdown supported).",
  5082. },
  5083. "ttl_seconds": {
  5084. "type": "integer",
  5085. "description": (
  5086. f"Seconds until the notice is deleted (default "
  5087. f"{EPHEMERAL_TTL_DEFAULT}, min {EPHEMERAL_TTL_MIN}, "
  5088. f"max {EPHEMERAL_TTL_MAX})."
  5089. ),
  5090. },
  5091. },
  5092. "required": ["message"],
  5093. "additionalProperties": False,
  5094. },
  5095. }
  5096. CHATTO_MARK_THREAD_AS_READ_SCHEMA = {
  5097. "name": "chatto_mark_thread_as_read",
  5098. "description": (
  5099. "Mark a thread as read for the bot. This updates the server's read "
  5100. "marker so Chatto clients show the thread as read. Useful after the bot "
  5101. "has processed a thread's full context."
  5102. ),
  5103. "parameters": {
  5104. "type": "object",
  5105. "properties": {
  5106. "room_id": {
  5107. "type": "string",
  5108. "description": "The Chatto room ID of the thread.",
  5109. },
  5110. "room_name": {
  5111. "type": "string",
  5112. "description": (
  5113. "Optional: room name — one of ``room_id`` or ``room_name`` is required."
  5114. ),
  5115. },
  5116. "thread_root_event_id": {
  5117. "type": "string",
  5118. "description": ("The event ID of the thread root message."),
  5119. },
  5120. },
  5121. "required": ["thread_root_event_id"],
  5122. "additionalProperties": False,
  5123. },
  5124. }
  5125. CHATTO_GET_MESSAGE_SCHEMA = {
  5126. "name": "chatto_get_message",
  5127. "description": (
  5128. "Read one or more Chatto messages by event ID, with all their data: "
  5129. "body, author (user ID + login), timestamps, reply/thread references "
  5130. "(in_reply_to, thread_root_event_id), attachments, reactions, pinned "
  5131. "and deleted status. Use it to resolve a `reply_to=`/`in_reply_to` "
  5132. "reference or any other event ID mentioned in a message. "
  5133. "`follow_reply_chain` (0-10) additionally walks the in_reply_to chain "
  5134. "backwards and returns the ancestor messages oldest-first; it applies to "
  5135. "a single `event_id` and is refused with `event_ids`. `event_ids` "
  5136. "fetches several messages in one call, answered in the order asked for "
  5137. "(then `event_id` is refused)."
  5138. ),
  5139. "parameters": {
  5140. "type": "object",
  5141. "properties": {
  5142. "room_id": {
  5143. "type": "string",
  5144. "description": (
  5145. "The Chatto room ID the message lives in "
  5146. "(e.g. 'RbmwiHk4vs9a3Ql'). Exactly one of `room_id` or "
  5147. "`room_name` is required."
  5148. ),
  5149. },
  5150. "room_name": {
  5151. "type": "string",
  5152. "description": (
  5153. "The room's display name (case-insensitive). Exactly one of "
  5154. "`room_id` or `room_name` is required; ambiguous names are "
  5155. "refused with the candidate IDs."
  5156. ),
  5157. },
  5158. "event_id": {
  5159. "type": "string",
  5160. "description": (
  5161. "The event ID of the message to read (e.g. 'EM0D7tFdvd14EXS'). "
  5162. "Exactly one of `event_id` or `event_ids` is required."
  5163. ),
  5164. },
  5165. "event_ids": {
  5166. "type": "array",
  5167. "items": {"type": "string"},
  5168. "description": (
  5169. "Several event IDs to read in one call. Mutually exclusive "
  5170. "with `event_id`."
  5171. ),
  5172. },
  5173. "follow_reply_chain": {
  5174. "type": "integer",
  5175. "description": (
  5176. "How many in_reply_to ancestors to resolve, 0-10 "
  5177. "(default 0 = do not follow). Single `event_id` only."
  5178. ),
  5179. },
  5180. },
  5181. "required": [],
  5182. "additionalProperties": False,
  5183. },
  5184. }
  5185. CHATTO_LIST_ROOM_GROUPS_SCHEMA = {
  5186. "name": "chatto_list_room_groups",
  5187. "description": (
  5188. "List room groups as configured on the Chatto server. Each group has an "
  5189. "ID, name, description, list of rooms, and optional sidebar links. "
  5190. "Useful for browsing the server's room organisation."
  5191. ),
  5192. "parameters": {
  5193. "type": "object",
  5194. "properties": {},
  5195. "required": [],
  5196. "additionalProperties": False,
  5197. },
  5198. }
  5199. CHATTO_GET_HISTORY_SCHEMA = {
  5200. "name": "chatto_get_history",
  5201. "description": (
  5202. "Read the most recent messages of a Chatto room, or of one thread in "
  5203. "it, oldest-first. Use it for context: what was said before a message "
  5204. "the bot is about to answer or reference. With `thread_root_event_id` "
  5205. "the thread's events are read instead of the room timeline; the root "
  5206. "has to belong to the room and BE a root, like in chatto_send_room. "
  5207. "`limit` (1-100, default 20) bounds the page; `has_older` says whether "
  5208. "older history exists. To read further back, pass the previous "
  5209. "result's `start_cursor` as `before` — one call reads one page."
  5210. ),
  5211. "parameters": {
  5212. "type": "object",
  5213. "properties": {
  5214. "room_id": {
  5215. "type": "string",
  5216. "description": (
  5217. "The Chatto room ID to read (e.g. 'RbmwiHk4vs9a3Ql'). "
  5218. "Exactly one of `room_id` or `room_name` is required."
  5219. ),
  5220. },
  5221. "room_name": {
  5222. "type": "string",
  5223. "description": (
  5224. "The room's display name (case-insensitive). Exactly one of "
  5225. "`room_id` or `room_name` is required; ambiguous names are "
  5226. "refused with the candidate IDs."
  5227. ),
  5228. },
  5229. "thread_root_event_id": {
  5230. "type": "string",
  5231. "description": (
  5232. "Optional: read this thread's events instead of the room "
  5233. "timeline. The ID must be a thread ROOT in this room."
  5234. ),
  5235. },
  5236. "limit": {
  5237. "type": "integer",
  5238. "description": (
  5239. "Maximum number of messages to return, 1-100 (default 20)."
  5240. ),
  5241. },
  5242. "before": {
  5243. "type": "string",
  5244. "description": (
  5245. "Optional: page backwards. Pass the `start_cursor` of the "
  5246. "previous result — cursors are opaque positions, not event "
  5247. "IDs. Mutually exclusive with `after`."
  5248. ),
  5249. },
  5250. "after": {
  5251. "type": "string",
  5252. "description": (
  5253. "Optional: page forwards. Pass the `end_cursor` of the "
  5254. "previous result. Mutually exclusive with `before`."
  5255. ),
  5256. },
  5257. },
  5258. "required": [],
  5259. "additionalProperties": False,
  5260. },
  5261. }
  5262. CHATTO_SEARCH_MESSAGES_SCHEMA = {
  5263. "name": "chatto_search_messages",
  5264. "description": (
  5265. "Full-text search over Chatto messages the bot can read. Use it to find "
  5266. "something said earlier when you do not know which room, thread or day: "
  5267. "`chatto_get_history` reads a window, this finds a needle. Search is a "
  5268. "server feature the operator has to enable; when it is off or still "
  5269. "building its index, the answer says so instead of reporting no "
  5270. 'matches. Query syntax: words are required terms, "quoted text" is an '
  5271. "exact phrase, `AND` separates terms, and `in:`, `from:`, `before:`, "
  5272. "`after:`, `has:attachment` are filters. Scope it with `room_id`/"
  5273. "`room_name`, `author_id` or the time bounds; `group_by: thread` "
  5274. "answers one row per matching thread instead of per message."
  5275. ),
  5276. "parameters": {
  5277. "type": "object",
  5278. "properties": {
  5279. "query": {
  5280. "type": "string",
  5281. "description": (
  5282. "Required search expression. Words are required terms "
  5283. '(all must match), "a quoted phrase" matches exactly, and '
  5284. "`AND` may join terms. Filters: `in:room-name`, "
  5285. "`from:user-login`, `before:2026-03-01`, "
  5286. "`after:2026-01-01`, `has:attachment`. A filter may be used "
  5287. "without a word or phrase."
  5288. ),
  5289. },
  5290. "room_id": {
  5291. "type": "string",
  5292. "description": (
  5293. "Optional: search only this room (e.g. 'RbmwiHk4vs9a3Ql'). "
  5294. "Mutually exclusive with `room_name`. Without either, the "
  5295. "search covers every room the bot is a member of."
  5296. ),
  5297. },
  5298. "room_name": {
  5299. "type": "string",
  5300. "description": (
  5301. "Optional: search only the room with this display name "
  5302. "(case-insensitive). Mutually exclusive with `room_id`; "
  5303. "ambiguous names are refused with the candidate IDs."
  5304. ),
  5305. },
  5306. "author_id": {
  5307. "type": "string",
  5308. "description": (
  5309. "Optional: only messages posted by this Chatto user ID. To "
  5310. "filter by login instead, put `from:<login>` inside "
  5311. "`query`."
  5312. ),
  5313. },
  5314. "created_after": {
  5315. "type": "string",
  5316. "description": (
  5317. "Optional ISO-8601 lower bound on posting time, exclusive "
  5318. "(e.g. '2026-01-31' or '2026-01-31T12:00:00Z'). When "
  5319. "`query` also carries `after:`, the stricter bound wins."
  5320. ),
  5321. },
  5322. "created_before": {
  5323. "type": "string",
  5324. "description": (
  5325. "Optional ISO-8601 upper bound on posting time, exclusive. "
  5326. "When `query` also carries `before:`, the stricter bound "
  5327. "wins."
  5328. ),
  5329. },
  5330. "has_attachments": {
  5331. "type": "boolean",
  5332. "description": (
  5333. "Optional: true returns only messages that currently have "
  5334. "attachments."
  5335. ),
  5336. },
  5337. "order": {
  5338. "type": "string",
  5339. "enum": [
  5340. "relevance",
  5341. "newest",
  5342. "thread_activity",
  5343. "thread_created",
  5344. ],
  5345. "description": (
  5346. "Result order (default relevance). `thread_activity` and "
  5347. "`thread_created` require `group_by: thread`."
  5348. ),
  5349. },
  5350. "group_by": {
  5351. "type": "string",
  5352. "enum": ["thread"],
  5353. "description": (
  5354. "Optional: with `thread`, return one row per matching "
  5355. "thread instead of one per message, with the thread's root, "
  5356. "reply count and latest reply."
  5357. ),
  5358. },
  5359. "scope": {
  5360. "type": "string",
  5361. "enum": ["followed_threads"],
  5362. "description": (
  5363. "Optional: with `followed_threads`, search only threads the "
  5364. "bot follows. Combine with `group_by: thread` for a thread "
  5365. "list."
  5366. ),
  5367. },
  5368. "limit": {
  5369. "type": "integer",
  5370. "description": (
  5371. "Maximum results per page, 1-100 (default 20). The page is "
  5372. "counted in messages, or in threads when `group_by` is "
  5373. "`thread`."
  5374. ),
  5375. },
  5376. "cursor": {
  5377. "type": "string",
  5378. "description": (
  5379. "Opaque `next_cursor` from a previous call, for the next "
  5380. "page. Bound to this caller and to every other query field, "
  5381. "so repeat the same query with it. Results come from a live "
  5382. "index: a page may repeat or omit a hit that moved while "
  5383. "paging."
  5384. ),
  5385. },
  5386. },
  5387. "required": ["query"],
  5388. "additionalProperties": False,
  5389. },
  5390. }
  5391. def _chatto_config_for_tools() -> ChattoConfiguration | None:
  5392. """Resolve the Chatto configuration for plugin tool handlers.
  5393. Two tiers, most specific first:
  5394. 1. The snapshot ``hermes_validate_config`` published for *this* profile — the
  5395. authoritative one in a running gateway.
  5396. 2. A fresh read of config.yaml plus the serving profile's own secrets, for
  5397. the cron and standalone sessions where the gateway load hooks never ran
  5398. and the dotenv residue has been stripped from ``os.environ``. Mirrors how
  5399. the built-in send_message tool resolves its pconfig
  5400. (tools/send_message_tool.py).
  5401. Both tiers are keyed by the serving profile, so a multiplexed gateway hands
  5402. each profile its own credentials rather than whichever one was served last.
  5403. Returns ``None`` when neither tier yields a configuration; the caller
  5404. reports that rather than guessing at credentials.
  5405. """
  5406. scope_key = _profile_scope_key()
  5407. snapshot = _resolved_chatto_configs.get(scope_key)
  5408. if snapshot is not None:
  5409. return snapshot
  5410. # config.yaml's own ``extra`` block, so a deployment configured there — and
  5411. # nowhere in .env — is honoured outside the gateway process too.
  5412. try:
  5413. from gateway.config import Platform as GatewayPlatform
  5414. from gateway.config import load_gateway_config
  5415. pconfig = load_gateway_config().platforms.get(
  5416. GatewayPlatform(ChattoConstants.PLATFORM_NAME)
  5417. )
  5418. resolved = ChattoConfiguration(pconfig=_pconfig_or_empty(pconfig))
  5419. except Exception as exc:
  5420. logger.warning("Chatto tools: could not read gateway config: %s", exc)
  5421. return None
  5422. # In a fresh cron scope the dotenv residue is stripped from os.environ, so
  5423. # ChattoConfiguration's env layer sees nothing. The secrets then come from
  5424. # the scope Hermes installed for the profile being served when there is one —
  5425. # that mapping is that profile's authoritative secret set, and rebuilding
  5426. # one from the launch home would answer with another profile's credentials.
  5427. # With no scope installed (CLI, bare standalone sender) the profile's own
  5428. # .env is the right source, and build_profile_secret_scope reads it
  5429. # directly in gateway, cron and CLI contexts alike.
  5430. try:
  5431. from agent.secret_scope import current_secret_scope
  5432. profile_secrets = current_secret_scope()
  5433. if profile_secrets is None:
  5434. from agent.secret_scope import build_profile_secret_scope
  5435. from hermes_constants import get_hermes_home
  5436. profile_secrets = dict(build_profile_secret_scope(get_hermes_home()))
  5437. except Exception as exc:
  5438. logger.warning("Chatto tools: profile secret scope build failed: %s", exc)
  5439. profile_secrets = {}
  5440. resolved.overlay_profile_secrets(profile_secrets)
  5441. # Cache only a configuration that can actually send. Caching a tokenless one
  5442. # would pin this failure for the life of the process: the next call in a
  5443. # properly scoped context would be served the degraded config and keep
  5444. # reporting "token not configured" long after the scope came back.
  5445. if resolved.token.value:
  5446. _resolved_chatto_configs[scope_key] = resolved
  5447. return resolved
  5448. def _parse_room_toolsets(
  5449. entries: Iterable[str],
  5450. ) -> tuple[dict[str, list[str]], list[str]]:
  5451. """Split ``room_id:toolset`` entries into per-room toolset lists.
  5452. Returns the mapping plus the entries that could not be read. Several
  5453. entries may name the same room; they accumulate in the order given, which
  5454. is what lets a room keep more than one toolset without inventing a nested
  5455. separator inside a comma-separated list. Malformed entries are reported
  5456. rather than raised: startup validation refuses the config, and a running
  5457. gateway must not die on a typo it can ignore.
  5458. """
  5459. by_room: dict[str, list[str]] = {}
  5460. malformed: list[str] = []
  5461. for raw in entries:
  5462. entry = str(raw).strip()
  5463. room_id, _, toolset = entry.partition(":")
  5464. room_id, toolset = room_id.strip(), toolset.strip()
  5465. if not room_id or not toolset:
  5466. malformed.append(entry)
  5467. continue
  5468. toolsets = by_room.setdefault(room_id, [])
  5469. if toolset not in toolsets:
  5470. toolsets.append(toolset)
  5471. return by_room, malformed
  5472. def _room_answers_in(
  5473. cfg: ChattoConfiguration | None, room_id: str, kind: RoomKind | None
  5474. ) -> bool:
  5475. """Whether the bot takes part in this room: answers there, and may post.
  5476. The single definition of that question, on purpose. It answers for two
  5477. callers whose disagreement would be a bug the user cannot see:
  5478. - ``ChattoAdapter._answers_in_room``, the *inbound* gate — whether a
  5479. message here reaches the agent at all
  5480. - ``_room_is_writable``, the *outbound* gate on the send tools, so a tool
  5481. cannot post into a room that would never have answered
  5482. Chatto knows only DMs and channels, so the split is ``kind == DM``: a DM
  5483. always answers (``/join`` must stay reachable), and every other kind —
  5484. channel, an unknown kind (which is how servers that never set ``kind`` show
  5485. up), or a room the adapter has not resolved yet, so ``kind`` is None —
  5486. opts in through one of the two mention lists. The lists are mutually
  5487. exclusive by validation; should contradictory config reach a running adapter
  5488. anyway, optional beats require, because answering a room is safer than
  5489. muting it. A ``cfg`` of None means no configuration could be resolved at
  5490. all, and then nothing but a DM qualifies.
  5491. """
  5492. if kind == RoomKind.DM:
  5493. return True
  5494. if cfg is None:
  5495. return False
  5496. if room_id in cfg.optional_mention_rooms.value:
  5497. return True
  5498. return room_id in cfg.require_mention_rooms.value
  5499. def _pconfig_or_empty(pconfig: PlatformConfig | None) -> PlatformConfig:
  5500. """The Chatto PlatformConfig from config.yaml, or an empty stand-in."""
  5501. if pconfig is not None:
  5502. return pconfig
  5503. from gateway.config import PlatformConfig as GatewayPlatformConfig
  5504. return GatewayPlatformConfig(enabled=True, extra={})
  5505. def _room_is_writable(cfg: ChattoConfiguration | None, room: Room) -> bool:
  5506. """Whether the bot may post into this room on its own initiative.
  5507. Delegates to ``_room_answers_in`` so the outbound gate is the *same*
  5508. function as the inbound one: a room that would never be answered must not
  5509. be postable, and two copies of that rule would drift apart until the send
  5510. tool became a way around the read-only promise.
  5511. """
  5512. return _room_answers_in(cfg, room.id, room.kind)
  5513. def _not_joined_refusal(room: Room, action: str) -> _ToolRefusal:
  5514. """The refusal for a room the bot can see but has not joined.
  5515. ``action`` names what the caller wanted to do there — "post there", "read
  5516. its history there" — because the remedy is identical for both while the
  5517. symptom is not, and a read that reports a sending problem sends the reader
  5518. looking in the wrong place. The room kinds differ in the remedy: giving a
  5519. channel the DM remedy sends the user down a path that cannot work, since a
  5520. direct message to the bot is not how you get it into a channel.
  5521. The test is on ``DM`` rather than on ``CHANNEL`` on purpose: a server that
  5522. never sets ``kind`` reports ``UNSPECIFIED``, and this plugin counts an unknown
  5523. kind as a channel everywhere else (``_room_answers_in``, ``_refresh_rooms``).
  5524. Keying on ``CHANNEL`` would hand exactly the advice this function exists to
  5525. avoid to every self-hosted server on an older or looser build.
  5526. """
  5527. remedy = (
  5528. "Have someone start a DM with the bot account"
  5529. if room.kind == RoomKind.DM
  5530. else f"Invite the bot account in Chatto, or DM it `/join {room.id}`"
  5531. )
  5532. return _refuse(
  5533. f"Room '{room.name or room.id}' ({room.id}) is not one the bot has "
  5534. f"joined, so it cannot {action}. {remedy}, then try again."
  5535. )
  5536. def _read_only_room_refusal(room_label: str) -> _ToolRefusal:
  5537. """The refusal a read-only room earns, naming where to opt it in.
  5538. This reply is where an operator copies the room ID from, so it spells both
  5539. env vars out verbatim.
  5540. """
  5541. return _refuse(
  5542. f"Room '{room_label}' is configured read-only: the bot neither answers "
  5543. "there nor posts to it on its own initiative. To allow that, add the "
  5544. f"room ID to {ChattoConfiguration.require_mention_rooms.env_name} (only "
  5545. f"answers when addressed) or {ChattoConfiguration.optional_mention_rooms.env_name} "
  5546. "(answers everything) in ~/.hermes/.env, and restart the gateway."
  5547. )
  5548. class _ChunkDelivery(NamedTuple):
  5549. """What a split send actually put into a room.
  5550. ``error`` is the exception that stopped the batch, if any: the chunks in
  5551. ``message_ids`` did go out and must not be resent. Each caller shapes this
  5552. into its own result contract — a ``SendResult`` for the adapter, a refusal
  5553. for the tools — because those are genuinely different contracts; the
  5554. *sending* is not. ``chunks_total`` counts what the split produced, so a
  5555. caller can report "got 1 of 3 out" without re-deriving the split.
  5556. """
  5557. message_ids: list[str]
  5558. root_event_id: str
  5559. error: Exception | None
  5560. chunks_total: int
  5561. async def _deliver_chunks(
  5562. client: ChattoClient,
  5563. room_id: str,
  5564. message: str,
  5565. *,
  5566. thread_root_event_id: str = "",
  5567. create_thread: bool = False,
  5568. on_posted: Callable[[str], None] | None = None,
  5569. ) -> _ChunkDelivery:
  5570. """Post a message split at the server limit, stopping at the first failure.
  5571. One implementation for both send paths, because the decisions are the same
  5572. and a copy of each drifts: split at ``SPLIT_THRESHOLD`` like every other
  5573. send, and route a split answer as *one* answer — chunk 1 opens a thread
  5574. (``create_thread``) or joins the one passed in, and every later chunk
  5575. carries that root, so a long reply does not scatter across the timeline.
  5576. ``on_posted`` is the adapter's ``_mark_seen``: the tools have no dedup state
  5577. to keep, the adapter must not treat its own output as unread input. The
  5578. first failure stops the batch and is returned rather than raised — the
  5579. caller knows better than this function what a partial send means to it, and
  5580. what to call the exception (``ChattoError`` is a server failure, anything
  5581. else is a bug here).
  5582. """
  5583. normalised = _normalise_outbound_text(message)
  5584. chunks = BasePlatformAdapter.truncate_message(
  5585. normalised, ChattoConstants.SPLIT_THRESHOLD
  5586. )
  5587. message_ids: list[str] = []
  5588. root_id = thread_root_event_id
  5589. for chunk in chunks:
  5590. try:
  5591. posted = await client.post_message(
  5592. room_id=room_id, body=chunk, thread_root_event_id=root_id
  5593. )
  5594. except Exception as exc:
  5595. return _ChunkDelivery(message_ids, root_id, exc, len(chunks))
  5596. message_ids.append(posted.id)
  5597. if on_posted is not None:
  5598. on_posted(posted.id)
  5599. if create_thread and not root_id:
  5600. root_id = posted.id
  5601. return _ChunkDelivery(message_ids, root_id, None, len(chunks))
  5602. async def _post_chunks(
  5603. client: ChattoClient,
  5604. room_id: str,
  5605. message: str,
  5606. *,
  5607. thread_root_event_id: str = "",
  5608. create_thread: bool = False,
  5609. ) -> tuple[list[str], str]:
  5610. """Post a message through ``_deliver_chunks`` as a tool result. ``(ids, root)``.
  5611. A chunk that fails leaves the earlier ones delivered, so this refuses with
  5612. exactly what went out: calling a half-delivered send a plain failure
  5613. invites an agent to retry and duplicate what the room already has.
  5614. """
  5615. delivery = await _deliver_chunks(
  5616. client,
  5617. room_id,
  5618. message,
  5619. thread_root_event_id=thread_root_event_id,
  5620. create_thread=create_thread,
  5621. )
  5622. if delivery.error is not None:
  5623. # Blame the server only for a server error. chattolib maps every
  5624. # transport failure to ChattoError, so anything else is a bug on our
  5625. # side — calling that "Chatto API error" would send an operator hunting
  5626. # the wrong component.
  5627. cause = (
  5628. "Chatto API error"
  5629. if isinstance(delivery.error, ChattoError)
  5630. else "Internal plugin error"
  5631. )
  5632. raise _refuse(
  5633. f"{cause} on chunk {len(delivery.message_ids) + 1}/"
  5634. f"{delivery.chunks_total}: {delivery.error}",
  5635. # partial marks a half-delivered send; the caller must not
  5636. # resend it from the top without knowing about these chunks.
  5637. partial=bool(delivery.message_ids),
  5638. room_id=room_id,
  5639. message_ids=delivery.message_ids,
  5640. chunks_sent=len(delivery.message_ids),
  5641. chunks_total=delivery.chunks_total,
  5642. ) from delivery.error
  5643. return delivery.message_ids, delivery.root_event_id
  5644. async def _find_user_by_login(client: ChattoClient, user_login: str) -> tuple[str, str]:
  5645. """Find a user among the members of our DM rooms, ``(user_id, login)``.
  5646. Both are empty when no room yields the login. Only a person can start a DM,
  5647. so a DM room is the only place a deliverable recipient can be — scanning
  5648. the channels the bot sits in would cost one member RPC per room and could
  5649. resolve a login whose DM does not exist yet. A bot cannot read the
  5650. server-wide user directory either: that needs admin rights.
  5651. Every DM room is searched, including one whose membership the directory
  5652. left unreported — that field is optional, and skipping such a room would
  5653. make its login unfindable while the DM room sits right there.
  5654. """
  5655. for rws in await client.list_rooms():
  5656. room = rws.room
  5657. if room is None or room.kind != RoomKind.DM:
  5658. continue
  5659. try:
  5660. member_ids, _ = await client.list_room_members(room.id, search=user_login)
  5661. except ChattoError as exc:
  5662. # A room we cannot enumerate must not abort the search over the
  5663. # remaining ones.
  5664. logger.debug("Chatto tools: member search in %s failed: %s", room.id, exc)
  5665. continue
  5666. if not member_ids:
  5667. continue
  5668. for member in await client.batch_get_room_members(room.id, member_ids):
  5669. user = member.user
  5670. if (
  5671. user is not None
  5672. and user.login
  5673. and user.login.casefold() == user_login.casefold()
  5674. ):
  5675. return user.id, user.login
  5676. return "", ""
  5677. async def _find_dm_room(
  5678. client: ChattoClient, our_id: str, other_id: str
  5679. ) -> Room | None:
  5680. """The DM room between two users, or None when there is none yet.
  5681. Membership is read from the room object the directory already returned, and
  5682. only falls back to a ListMembers RPC when that came back empty:
  5683. ``member_user_ids`` is not among the fields the ListRooms documentation
  5684. lists for ``RoomWithViewerState``, so a server is free to leave it
  5685. unpopulated — and a DM room we can see plainly exists, whatever the
  5686. directory said about who is in it. The fallback costs one RPC per DM room
  5687. and only on servers that need it.
  5688. """
  5689. for rws in await client.list_rooms():
  5690. room = rws.room
  5691. if room is None or room.kind != RoomKind.DM:
  5692. continue
  5693. if rws.member_user_ids:
  5694. if other_id in rws.member_user_ids and our_id in rws.member_user_ids:
  5695. return room
  5696. continue
  5697. try:
  5698. member_ids, _page = await client.list_room_members(room.id)
  5699. except ChattoError as exc:
  5700. # An unreadable room cannot be the one we want, but it must not
  5701. # abort the search over the remaining ones.
  5702. logger.debug("Chatto tools: members of DM %s unreadable: %s", room.id, exc)
  5703. continue
  5704. if other_id in member_ids and our_id in member_ids:
  5705. return room
  5706. return None
  5707. class _ResolvedRoom(NamedTuple):
  5708. """A room the send path resolved, plus whether the bot belongs to it.
  5709. Membership travels with the room because the directory answers a broader
  5710. question than the send path can use: a room the bot can *see* is not one it
  5711. can *post in* (ListRooms returns visible channels, membership or room.list
  5712. permission). Splitting that into two lookups would be a second chance to
  5713. disagree with the state the first one already carried.
  5714. """
  5715. room: Room
  5716. is_member: bool
  5717. async def _resolve_room(
  5718. client: ChattoClient, room_id: str, room_name: str
  5719. ) -> _ResolvedRoom:
  5720. """The destination room, by ID or by name.
  5721. A name is not unique on Chatto — two room groups may both hold an "Ops" —
  5722. and delivering to whichever the directory listed first is not a choice
  5723. anyone made, so an ambiguous name asks for the ID instead.
  5724. """
  5725. if room_id:
  5726. room_viewer_state = await client.get_room(room_id)
  5727. if room_viewer_state is None or room_viewer_state.room is None:
  5728. raise _refuse(
  5729. f"Room {room_id} was not found, or the bot cannot see it. "
  5730. "Use chatto_list_rooms to look up a room ID."
  5731. )
  5732. return _ResolvedRoom(
  5733. room_viewer_state.room, room_viewer_state.viewer_state.is_member
  5734. )
  5735. matches = [
  5736. _ResolvedRoom(rws.room, rws.viewer_state.is_member)
  5737. for rws in await client.list_rooms()
  5738. if rws.room is not None
  5739. and (rws.room.name or "").casefold() == room_name.casefold()
  5740. ]
  5741. if not matches:
  5742. raise _refuse(
  5743. f"Room not found: {room_name}. Use chatto_list_rooms to see the available names."
  5744. )
  5745. if len(matches) > 1:
  5746. candidates = ", ".join(
  5747. f"{match.room.id} ('{match.room.name}')" for match in matches
  5748. )
  5749. raise _refuse(
  5750. f"Room name '{room_name}' is ambiguous — {len(matches)} rooms carry it: "
  5751. f"{candidates}. Pass the room_id instead."
  5752. )
  5753. return matches[0]
  5754. async def _resolve_thread_root(
  5755. client: ChattoClient, room: Room, event_id: str
  5756. ) -> Message:
  5757. """The thread root a tool was pointed at, or a refusal saying why not.
  5758. Both tools that take a ``thread_root_event_id`` — posting into one and
  5759. reading one — resolve it here, because two copies of "is this a root?" is
  5760. one too many: they drift, and the drift shows up as two tools disagreeing
  5761. about the same event ID.
  5762. Two things have to hold. The message has to exist in this room (a root from
  5763. another room would attach a thread to the wrong conversation), and it has
  5764. to BE a root (a reply's ID names no thread, so following it lands nowhere).
  5765. """
  5766. root = await client.get_message(room.id, event_id)
  5767. if root is None:
  5768. raise _refuse(
  5769. f"No message {event_id} in room {room.id}. It has to be an event ID "
  5770. f"from this room — chatto_get_history lists them."
  5771. )
  5772. if root.thread_root_event_id:
  5773. raise _refuse(
  5774. f"Message {event_id} is a reply inside thread "
  5775. f"{root.thread_root_event_id}, not a thread root. Pass that "
  5776. "thread's root ID instead."
  5777. )
  5778. return root
  5779. async def _send_dm(args: dict) -> str:
  5780. """Resolve the recipient, find the DM room, post. See _handle_chatto_send_dm."""
  5781. user_login = args.get("user_login") or ""
  5782. user_id = args.get("user_id") or ""
  5783. message = args.get("message") or ""
  5784. if not message:
  5785. raise _refuse("message is required")
  5786. if not user_login and not user_id:
  5787. raise _refuse("Exactly one of user_login or user_id is required")
  5788. async with _tool_client(_chatto_config_for_tools()) as client:
  5789. # A bot cannot read the server-wide user directory (that needs admin
  5790. # rights), so a login is resolved among the members of shared rooms.
  5791. target_user_id = user_id
  5792. target_login = user_login
  5793. if not target_user_id:
  5794. target_user_id, target_login = await _find_user_by_login(client, user_login)
  5795. if not target_user_id:
  5796. raise _refuse(f"User not found: {user_login} shares no DM with the bot")
  5797. # Only people may start a DM (Chatto permissions guide), so a recipient
  5798. # who has never written to the bot has no room to post into — say so
  5799. # instead of guessing at a server rejection later.
  5800. dm_room = await _find_dm_room(client, (await client.me()).id, target_user_id)
  5801. if dm_room is None:
  5802. raise _refuse(
  5803. f"No DM with {user_login or user_id} exists yet: on Chatto only a "
  5804. "person can start a DM, so they have to send the bot a message "
  5805. "once before it can message them"
  5806. )
  5807. message_ids, _root_id = await _post_chunks(client, dm_room.id, message)
  5808. return json.dumps(
  5809. {
  5810. "success": True,
  5811. "dm_room_id": dm_room.id,
  5812. "recipient": target_login,
  5813. "recipient_user_id": target_user_id,
  5814. "message_ids": message_ids,
  5815. "chunks_sent": len(message_ids),
  5816. }
  5817. )
  5818. async def _send_room(args: dict) -> str:
  5819. """Resolve the room, check the gates, post. See _handle_chatto_send_room."""
  5820. room_id = args.get("room_id") or ""
  5821. room_name = args.get("room_name") or ""
  5822. message = args.get("message") or ""
  5823. thread_root_event_id = args.get("thread_root_event_id") or ""
  5824. create_thread = bool(args.get("create_thread", False))
  5825. if not message:
  5826. raise _refuse("message is required")
  5827. if not room_id and not room_name:
  5828. raise _refuse("Exactly one of room_id or room_name is required")
  5829. if thread_root_event_id and create_thread:
  5830. raise _refuse("thread_root_event_id and create_thread are mutually exclusive")
  5831. cfg = _chatto_config_for_tools()
  5832. async with _tool_client(cfg) as client:
  5833. resolved = await _resolve_room(client, room_id, room_name)
  5834. room = resolved.room
  5835. # Posting into a room the plugin treats as read-only would break a
  5836. # promise the README makes about unlisted rooms staying silent.
  5837. if not _room_is_writable(cfg, room):
  5838. raise _read_only_room_refusal(room.name or room.id)
  5839. if create_thread and room.kind == RoomKind.DM:
  5840. raise _refuse(
  5841. "A DM has no threads: drop create_thread and send it as a normal message."
  5842. )
  5843. # The directory lists rooms the bot can see but has not joined, and
  5844. # posting into one fails server-side with an error that names neither
  5845. # the room nor the fix.
  5846. if not resolved.is_member:
  5847. raise _not_joined_refusal(room, "post there")
  5848. # A thread root has to belong to this room and has to BE a root, or
  5849. # the message attaches to a thread somewhere else entirely — or to
  5850. # none that can be followed. One shared check, so this tool and
  5851. # chatto_get_history cannot answer differently about the same ID.
  5852. if thread_root_event_id:
  5853. await _resolve_thread_root(client, room, thread_root_event_id)
  5854. message_ids, root_event_id = await _post_chunks(
  5855. client,
  5856. room.id,
  5857. message,
  5858. thread_root_event_id=thread_root_event_id,
  5859. create_thread=create_thread,
  5860. )
  5861. # Any thread the bot posts into is followed, not only one it opened:
  5862. # Chatto delivers a thread's replies to an account that follows it (or
  5863. # was mentioned in it directly), so replying into someone else's thread
  5864. # without following it can end the conversation the agent just joined.
  5865. # Mirrors the adapter's own send(), which follows every thread it uses.
  5866. if root_event_id:
  5867. try:
  5868. await client.follow_thread(room.id, root_event_id)
  5869. except Exception as exc:
  5870. # Follow is best-effort: the thread exists and the message was
  5871. # delivered, so a hiccup here is not a failed send.
  5872. logger.debug(
  5873. "Chatto tools: follow_thread(%s) failed: %s", root_event_id, exc
  5874. )
  5875. return json.dumps(
  5876. {
  5877. "success": True,
  5878. "room_id": room.id,
  5879. "room_name": room.name,
  5880. "message_ids": message_ids,
  5881. "chunks_sent": len(message_ids),
  5882. "thread_root_event_id": root_event_id,
  5883. "created_thread": bool(create_thread),
  5884. }
  5885. )
  5886. _EPHEMERAL_DELETION_TASKS: set[asyncio.Task[None]] = set()
  5887. async def _send_ephemeral(args: dict) -> str:
  5888. """Post a notice that deletes itself. See _handle_chatto_send_ephemeral."""
  5889. room_id = args.get("room_id") or ""
  5890. room_name = args.get("room_name") or ""
  5891. message = args.get("message") or ""
  5892. ttl_seconds = _int_arg(
  5893. args,
  5894. "ttl_seconds",
  5895. EPHEMERAL_TTL_DEFAULT,
  5896. low=EPHEMERAL_TTL_MIN,
  5897. high=EPHEMERAL_TTL_MAX,
  5898. )
  5899. if not message:
  5900. raise _refuse("message is required")
  5901. if not room_id and not room_name:
  5902. raise _refuse("Exactly one of room_id or room_name is required")
  5903. cfg = _chatto_config_for_tools()
  5904. if cfg is None:
  5905. raise _refuse("Chatto configuration unavailable")
  5906. async with _tool_client(cfg) as client:
  5907. resolved = await _resolve_room(client, room_id, room_name)
  5908. room = resolved.room
  5909. # Same gates as chatto_send_room: this tool posts, so it must not be a
  5910. # way into a room the plugin keeps read-only.
  5911. if not _room_is_writable(cfg, room):
  5912. raise _read_only_room_refusal(room.name or room.id)
  5913. if not resolved.is_member:
  5914. raise _not_joined_refusal(room, "post there")
  5915. message_ids, _root = await _post_chunks(client, room.id, message)
  5916. _schedule_ephemeral_deletion(cfg, room.id, message_ids, ttl_seconds)
  5917. return json.dumps(
  5918. {
  5919. "success": True,
  5920. "room_id": room.id,
  5921. "message_ids": message_ids,
  5922. "chunks_sent": len(message_ids),
  5923. "expires_in_seconds": ttl_seconds,
  5924. }
  5925. )
  5926. def _schedule_ephemeral_deletion(
  5927. cfg: ChattoConfiguration,
  5928. room_id: str,
  5929. event_ids: list[str],
  5930. ttl_seconds: int,
  5931. ) -> None:
  5932. """Delete a delivered notice once its TTL has passed, best-effort.
  5933. The task is held in a module-level set because asyncio keeps only a weak
  5934. reference: an unreferenced task can be collected before it ever runs.
  5935. Failure to delete is logged, never raised — the message was delivered, and
  5936. a process restart simply leaves it behind.
  5937. """
  5938. task = asyncio.create_task(_delete_after_ttl(cfg, room_id, event_ids, ttl_seconds))
  5939. _EPHEMERAL_DELETION_TASKS.add(task)
  5940. task.add_done_callback(_EPHEMERAL_DELETION_TASKS.discard)
  5941. async def _delete_after_ttl(
  5942. cfg: ChattoConfiguration,
  5943. room_id: str,
  5944. event_ids: list[str],
  5945. ttl_seconds: int,
  5946. ) -> None:
  5947. """Sleep out the TTL, then delete each chunk it covered."""
  5948. await asyncio.sleep(ttl_seconds)
  5949. try:
  5950. async with _tool_client(cfg) as client:
  5951. for event_id in event_ids:
  5952. try:
  5953. await client.delete_message(room_id, event_id)
  5954. except ChattoError as exc:
  5955. logger.debug(
  5956. "Chatto tools: ephemeral message %s in %s not deleted: %s",
  5957. event_id,
  5958. room_id,
  5959. exc,
  5960. )
  5961. except Exception as exc:
  5962. # No caller to raise to: the notice was delivered and this task exists
  5963. # only to make it disappear again. A transport oddity that chattolib
  5964. # did not map to ChattoError must not surface as an unretrieved task
  5965. # exception, so it is logged like any other cleanup failure.
  5966. logger.debug(
  5967. "Chatto tools: ephemeral cleanup of %s in %s failed: %s",
  5968. event_ids,
  5969. room_id,
  5970. exc,
  5971. exc_info=True,
  5972. )
  5973. async def _list_rooms(args: dict) -> str:
  5974. """List the rooms visible to the bot. See _handle_chatto_list_rooms."""
  5975. name_filter = (args.get("name") or "").strip().casefold()
  5976. async with _tool_client(_chatto_config_for_tools()) as client:
  5977. rooms_out = []
  5978. for rws in await client.list_rooms():
  5979. room = rws.room
  5980. if room is None:
  5981. continue
  5982. # A filter implies named rooms: skip DMs (empty name) and
  5983. # non-matching names alike.
  5984. if name_filter and not (room.name and name_filter in room.name.casefold()):
  5985. continue
  5986. rooms_out.append(
  5987. {
  5988. "room_id": room.id,
  5989. "name": room.name or "",
  5990. "kind": room.kind.value,
  5991. "threading_mode": room.threading_mode.value,
  5992. "archived": room.archived,
  5993. "is_member": rws.viewer_state.is_member,
  5994. # DM rooms carry no name, so they are labelled by their ID
  5995. # instead of leaving the model with a blank to match on.
  5996. "label": room.name or f"(DM room {room.id})",
  5997. }
  5998. )
  5999. return json.dumps(
  6000. {
  6001. "success": True,
  6002. "count": len(rooms_out),
  6003. "rooms": rooms_out,
  6004. "hint": (
  6005. "Use room_id in chatto_send_room, or as the chat_id part of a "
  6006. "cron deliver target like 'chatto-platform:<room_id>'."
  6007. ),
  6008. }
  6009. )
  6010. async def _list_room_attachments(args: dict) -> str:
  6011. """List file attachments in a Chatto room. See _handle_chatto_list_room_attachments."""
  6012. room_id = args.get("room_id") or ""
  6013. room_name = args.get("room_name") or ""
  6014. limit = _int_arg(
  6015. args, "limit", ATTACHMENT_PAGE_SIZE, low=1, high=ATTACHMENT_PAGE_SIZE * 5
  6016. )
  6017. offset = _int_arg(args, "offset", 0, low=0, high=100_000)
  6018. if not room_id and not room_name:
  6019. raise _refuse("Exactly one of room_id or room_name is required")
  6020. async with _tool_client(_chatto_config_for_tools()) as client:
  6021. resolved = await _resolve_room(client, room_id, room_name)
  6022. room = resolved.room
  6023. attachments, page = await client.list_room_attachments(
  6024. room.id, limit=limit, offset=offset
  6025. )
  6026. return json.dumps(
  6027. {
  6028. "success": True,
  6029. "room_id": room.id,
  6030. "room_name": room.name,
  6031. "count": len(attachments),
  6032. "offset": offset,
  6033. "has_more": page.has_more,
  6034. "total_count": page.total_count,
  6035. "attachments": attachments,
  6036. }
  6037. )
  6038. async def _list_pinned_messages(args: dict) -> str:
  6039. """List pinned messages in a Chatto room. See _handle_chatto_list_pinned_messages."""
  6040. room_id = args.get("room_id") or ""
  6041. room_name = args.get("room_name") or ""
  6042. limit = _int_arg(args, "limit", PINNED_PAGE_SIZE, low=1, high=PINNED_PAGE_SIZE * 5)
  6043. offset = _int_arg(args, "offset", 0, low=0, high=100_000)
  6044. if not room_id and not room_name:
  6045. raise _refuse("Exactly one of room_id or room_name is required")
  6046. async with _tool_client(_chatto_config_for_tools()) as client:
  6047. resolved = await _resolve_room(client, room_id, room_name)
  6048. room = resolved.room
  6049. page = await client.list_pinned_messages(room.id, limit=limit, offset=offset)
  6050. messages = [pm.message for pm in page.pinned_messages if pm.message is not None]
  6051. author_logins = await _batch_author_logins(
  6052. client, room.id, [msg.actor_id for msg in messages if msg.actor_id]
  6053. )
  6054. pinned_out = [
  6055. {
  6056. "event_id": msg.id,
  6057. "body": msg.body,
  6058. "author_id": msg.actor_id,
  6059. "author_login": author_logins.get(msg.actor_id) or "",
  6060. "posted_at": _iso_or_none(msg.created_at),
  6061. }
  6062. for msg in messages
  6063. ]
  6064. return json.dumps(
  6065. {
  6066. "success": True,
  6067. "room_id": room.id,
  6068. "room_name": room.name,
  6069. "count": len(pinned_out),
  6070. "has_more": page.page.has_more,
  6071. "total_count": page.page.total_count,
  6072. "pinned_messages": pinned_out,
  6073. "latest_pin_marker": page.latest_pin_marker,
  6074. }
  6075. )
  6076. async def _pin_message(args: dict) -> str:
  6077. """Pin or unpin a message in a Chatto room. See _handle_chatto_pin_message."""
  6078. room_id = args.get("room_id") or ""
  6079. room_name = args.get("room_name") or ""
  6080. event_id = args.get("event_id") or ""
  6081. pin = args.get("pin")
  6082. if not room_id and not room_name:
  6083. raise _refuse("Exactly one of room_id or room_name is required")
  6084. if not event_id:
  6085. raise _refuse("event_id is required")
  6086. if pin is None:
  6087. raise _refuse("pin (true/false) is required")
  6088. cfg = _chatto_config_for_tools()
  6089. async with _tool_client(cfg) as client:
  6090. resolved = await _resolve_room(client, room_id, room_name)
  6091. room = resolved.room
  6092. # Pinning changes what the room shows everyone, so it is a write:
  6093. # a room on neither mention list stays read-only, exactly as it does
  6094. # for chatto_send_room.
  6095. if not _room_is_writable(cfg, room):
  6096. raise _read_only_room_refusal(room.name or room.id)
  6097. if not resolved.is_member:
  6098. raise _not_joined_refusal(room, "pin a message there")
  6099. if pin:
  6100. await client.pin_message(room.id, event_id)
  6101. return json.dumps(
  6102. {
  6103. "success": True,
  6104. "action": "pinned",
  6105. "room_id": room.id,
  6106. "event_id": event_id,
  6107. }
  6108. )
  6109. unpinned = await client.unpin_message(room.id, event_id)
  6110. return json.dumps(
  6111. {
  6112. "success": True,
  6113. "action": "unpinned",
  6114. "room_id": room.id,
  6115. "event_id": event_id,
  6116. "unpinned": unpinned,
  6117. }
  6118. )
  6119. def _iso_or_none(moment: datetime | None) -> str | None:
  6120. """A Message timestamp as ISO-8601, or None when the server sent none.
  6121. chattolib's own formatter does the work: it pins a naive timestamp to UTC
  6122. instead of letting it print without an offset, so two timestamps from
  6123. different sources compare the way a reader expects.
  6124. """
  6125. if moment is None:
  6126. return None
  6127. return format_datetime(moment)
  6128. async def _batch_author_logins(
  6129. client: ChattoClient, room_id: str, actor_ids: list[str]
  6130. ) -> dict[str, str]:
  6131. """Map actor IDs to logins, using the room directory the bot may read.
  6132. ``batch_get_room_members`` needs no admin rights and answers member
  6133. profiles for the caller's own rooms, so it is the one sanctioned way to
  6134. turn an ``actor_id`` into a name here. Members who left (or were never
  6135. members) come back missing — their IDs stay as their only label, which
  6136. is still unambiguous for the caller. A directory that refuses the call
  6137. costs the names, not the messages: the logins are enrichment, and an
  6138. unreadable roster must not make a readable history unreadable.
  6139. """
  6140. unique_ids = list({actor_id for actor_id in actor_ids if actor_id})
  6141. if not unique_ids:
  6142. return {}
  6143. try:
  6144. members = await client.batch_get_room_members(room_id, unique_ids)
  6145. except ChattoError as exc:
  6146. logger.debug("Chatto tools: members of room %s unreadable: %s", room_id, exc)
  6147. return {}
  6148. logins: dict[str, str] = {}
  6149. for member in members:
  6150. if member.user is not None and member.user.id:
  6151. logins[member.user.id] = member.user.login
  6152. return logins
  6153. def _attachment_out(attachment: MessageAttachment) -> dict[str, Any]:
  6154. """One attachment as the tool's JSON shape."""
  6155. out: dict[str, Any] = {
  6156. "id": attachment.id,
  6157. "filename": attachment.filename,
  6158. "content_type": attachment.content_type,
  6159. "width": attachment.width,
  6160. "height": attachment.height,
  6161. "description": attachment.description,
  6162. }
  6163. if attachment.asset_url is not None:
  6164. out["asset_url"] = attachment.asset_url.url
  6165. if attachment.thumbnail_asset_url is not None:
  6166. out["thumbnail_asset_url"] = attachment.thumbnail_asset_url.url
  6167. if attachment.video_processing is not None:
  6168. # A video carries its own dimensions, duration and transcoded
  6169. # variants, and its own URL only once the server has produced one —
  6170. # without this block the tool reports a video as an attachment with no
  6171. # size, no length and nothing to fetch.
  6172. video = attachment.video_processing
  6173. out["video"] = {
  6174. "duration_ms": video.duration_ms,
  6175. "status": video.status.name,
  6176. "thumbnail_url": (
  6177. video.thumbnail_asset_url.url
  6178. if video.thumbnail_asset_url is not None
  6179. else None
  6180. ),
  6181. "variants": [
  6182. {
  6183. "quality": variant.quality,
  6184. "width": variant.width,
  6185. "height": variant.height,
  6186. "size": variant.size,
  6187. "url": (
  6188. variant.asset_url.url if variant.asset_url is not None else None
  6189. ),
  6190. }
  6191. for variant in video.variants
  6192. ],
  6193. }
  6194. return out
  6195. def _message_out(message: Message, author_logins: dict[str, str]) -> dict[str, Any]:
  6196. """A Message as the tool's JSON shape — every field the caller can act on."""
  6197. return {
  6198. "event_id": message.id,
  6199. "room_id": message.room_id,
  6200. "author_user_id": message.actor_id,
  6201. "author_login": author_logins.get(message.actor_id),
  6202. "created_at": _iso_or_none(message.created_at),
  6203. "updated_at": _iso_or_none(message.updated_at),
  6204. "deleted_at": _iso_or_none(message.deleted_at),
  6205. "body": message.body,
  6206. "in_reply_to": message.in_reply_to or None,
  6207. "thread_root_event_id": message.thread_root_event_id or None,
  6208. "pinned": message.pinned,
  6209. "attachments": [
  6210. _attachment_out(attachment) for attachment in message.attachments
  6211. ],
  6212. "reactions": [
  6213. {
  6214. "emoji": reaction.emoji,
  6215. "count": reaction.count,
  6216. "has_reacted": reaction.has_reacted,
  6217. }
  6218. for reaction in message.reactions
  6219. ],
  6220. "link_preview": (
  6221. {"url": message.link_preview.url, "title": message.link_preview.title}
  6222. if message.link_preview is not None
  6223. else None
  6224. ),
  6225. "thread_summary": (
  6226. {
  6227. "thread_root_event_id": message.thread.thread_root_event_id,
  6228. "reply_count": message.thread.reply_count,
  6229. "participant_count": message.thread.participant_count,
  6230. }
  6231. if message.thread is not None
  6232. else None
  6233. ),
  6234. }
  6235. async def _get_message(args: dict) -> str:
  6236. """Resolve the room, fetch the message(s). See _handle_chatto_get_message."""
  6237. room_id = args.get("room_id") or ""
  6238. room_name = args.get("room_name") or ""
  6239. event_id = args.get("event_id") or ""
  6240. event_ids = [e for e in (args.get("event_ids") or []) if e]
  6241. follow_reply_chain = _int_arg(
  6242. args, "follow_reply_chain", 0, low=0, high=FOLLOW_REPLY_CHAIN_MAX
  6243. )
  6244. if not room_id and not room_name:
  6245. raise _refuse("Exactly one of room_id or room_name is required")
  6246. if not event_id and not event_ids:
  6247. raise _refuse("Exactly one of event_id or event_ids is required")
  6248. if event_id and event_ids:
  6249. raise _refuse("event_id and event_ids are mutually exclusive")
  6250. if event_ids and follow_reply_chain:
  6251. # A chain belongs to one message. With several requested there is no
  6252. # single answer, and picking one would hand back a conversation the
  6253. # caller did not ask for.
  6254. raise _refuse(
  6255. "follow_reply_chain applies to a single event_id — read one message "
  6256. "per call to follow its chain"
  6257. )
  6258. async with _tool_client(_chatto_config_for_tools()) as client:
  6259. resolved = await _resolve_room(client, room_id, room_name)
  6260. room = resolved.room
  6261. # Every read RPC the plugin uses documents room membership as a
  6262. # precondition (GetRoomEvents, GetMessage, BatchGetMessages), so an
  6263. # unjoined room is unreadable whatever the mention lists say. Saying so
  6264. # here beats a PERMISSION_DENIED that names neither the room nor the fix.
  6265. if not resolved.is_member:
  6266. raise _not_joined_refusal(room, "read it there")
  6267. if event_id:
  6268. # One message is one lookup: the single-message RPC is the
  6269. # authoritative one, so there is no batch to fall back from.
  6270. single = await client.get_message(room.id, event_id)
  6271. found = {single.id: single} if single is not None else {}
  6272. missing = [] if single is not None else [event_id]
  6273. else:
  6274. fetched = await client.batch_get_messages(room.id, event_ids)
  6275. by_id = {m.id: m for m in fetched if m.id}
  6276. # Reported in the order they were asked for, not the order the
  6277. # server happened to answer in.
  6278. found = {eid: by_id[eid] for eid in event_ids if eid in by_id}
  6279. missing = [eid for eid in event_ids if eid not in found]
  6280. if not found:
  6281. raise _refuse(
  6282. f"No message {', '.join(missing)} found in room {room.id}. "
  6283. "Check the event ID and the room."
  6284. )
  6285. # The reply chain walks in_reply_to backwards, oldest ancestor first,
  6286. # one hop per call — a chain is a path, so it cannot be batched. A hop
  6287. # the server cannot resolve ends the chain: the ancestors above it are
  6288. # unreachable, and stopping beats reporting a hole as the top.
  6289. chain: list[Message] = []
  6290. if follow_reply_chain > 0:
  6291. frontier_id = next(iter(found.values())).in_reply_to
  6292. hops = follow_reply_chain
  6293. while frontier_id and hops > 0:
  6294. ancestor = await client.get_message(room.id, frontier_id)
  6295. if ancestor is None:
  6296. break
  6297. chain.append(ancestor)
  6298. frontier_id = ancestor.in_reply_to
  6299. hops -= 1
  6300. chain.reverse()
  6301. author_ids = [m.actor_id for m in found.values()] + [m.actor_id for m in chain]
  6302. author_logins = await _batch_author_logins(client, room.id, author_ids)
  6303. return json.dumps(
  6304. {
  6305. "success": True,
  6306. "room_id": room.id,
  6307. "room_name": room.name,
  6308. "messages": [_message_out(m, author_logins) for m in found.values()],
  6309. "missing_event_ids": missing,
  6310. "reply_chain": [_message_out(m, author_logins) for m in chain],
  6311. }
  6312. )
  6313. async def _get_history(args: dict) -> str:
  6314. """Resolve the room, read the timeline. See _handle_chatto_get_history."""
  6315. room_id = args.get("room_id") or ""
  6316. room_name = args.get("room_name") or ""
  6317. thread_root_event_id = args.get("thread_root_event_id") or ""
  6318. limit = _int_arg(args, "limit", HISTORY_PAGE_SIZE, low=1, high=HISTORY_PAGE_MAX)
  6319. before = args.get("before") or ""
  6320. after = args.get("after") or ""
  6321. if not room_id and not room_name:
  6322. raise _refuse("Exactly one of room_id or room_name is required")
  6323. if before and after:
  6324. # The client sends `before` OR `after` and drops the second without a
  6325. # word, so both would answer a question nobody asked.
  6326. raise _refuse(
  6327. "before and after are mutually exclusive — pick the direction you "
  6328. "want to page in"
  6329. )
  6330. async with _tool_client(_chatto_config_for_tools()) as client:
  6331. resolved = await _resolve_room(client, room_id, room_name)
  6332. room = resolved.room
  6333. # A read of an unjoined room fails server-side whatever the mention
  6334. # lists say; saying so here names the room and the way in.
  6335. if not resolved.is_member:
  6336. raise _not_joined_refusal(room, "read its history there")
  6337. root: Message | None = None
  6338. if thread_root_event_id:
  6339. root = await _resolve_thread_root(client, room, thread_root_event_id)
  6340. page = await client.get_thread_events(
  6341. room.id,
  6342. thread_root_event_id,
  6343. limit=limit,
  6344. before=before or None,
  6345. after=after or None,
  6346. )
  6347. else:
  6348. page = await client.get_room_events(
  6349. room.id, limit=limit, before=before or None, after=after or None
  6350. )
  6351. messages = [ev.message for ev in page.events if ev.message is not None]
  6352. if root is not None and all(m.id != root.id for m in messages):
  6353. # Whether the server lists the root back as the thread's first
  6354. # event is not something the client signature promises, and a
  6355. # thread read without its root is a conversation whose opening
  6356. # line is missing. The root is already in hand, so putting it back
  6357. # costs nothing.
  6358. messages.insert(0, root)
  6359. messages = _oldest_first(messages)
  6360. # The timeline page carries its own user directory (``users_by_id``),
  6361. # which answers author names without a member-list call. Fall back to
  6362. # the room directory for actors the page did not include.
  6363. author_logins = {
  6364. user_id: user.login
  6365. for user_id, user in page.users_by_id.items()
  6366. if user.login
  6367. }
  6368. unresolved = [
  6369. m.actor_id
  6370. for m in messages
  6371. if m.actor_id and m.actor_id not in author_logins
  6372. ]
  6373. if unresolved:
  6374. author_logins.update(
  6375. await _batch_author_logins(client, room.id, unresolved)
  6376. )
  6377. return json.dumps(
  6378. {
  6379. "success": True,
  6380. "room_id": room.id,
  6381. "room_name": room.name,
  6382. "thread_root_event_id": thread_root_event_id or None,
  6383. "count": len(messages),
  6384. "has_older": page.has_older,
  6385. "has_newer": page.has_newer,
  6386. # Paging is the caller's next call, so this answer has to carry
  6387. # the positions that next call takes as its arguments —
  6388. # otherwise the model would have to guess which message to
  6389. # anchor on.
  6390. "start_cursor": page.start_cursor,
  6391. "end_cursor": page.end_cursor,
  6392. "messages": [_message_out(m, author_logins) for m in messages],
  6393. }
  6394. )
  6395. def _search_result_out(
  6396. result: MessageSearchResult, author_logins: dict[str, str]
  6397. ) -> dict[str, Any]:
  6398. """One search hit as the tool's JSON shape.
  6399. A hit is a pointer, not a record: ``chatto_get_message`` is what reads one
  6400. in full, so this carries what decides *which* hit to read — where it is,
  6401. who said it, when, what it says — and leaves the reaction and attachment
  6402. detail to that call.
  6403. """
  6404. message = result.message
  6405. out: dict[str, Any] = {"relevance": round(result.relevance_score, 4)}
  6406. if message is not None:
  6407. out.update(
  6408. {
  6409. "event_id": message.id,
  6410. "room_id": message.room_id,
  6411. "author_user_id": message.actor_id,
  6412. "author_login": author_logins.get(message.actor_id),
  6413. "created_at": _iso_or_none(message.created_at),
  6414. "body": message.body,
  6415. "in_reply_to": message.in_reply_to or None,
  6416. "thread_root_event_id": message.thread_root_event_id or None,
  6417. }
  6418. )
  6419. context = result.thread_context
  6420. if context is not None:
  6421. # Only THREAD grouping asks for this: the row stands for a whole
  6422. # conversation, so the caller needs its opening line and how far it got,
  6423. # not just the message the provider matched.
  6424. root = context.root_message
  6425. latest = context.latest_reply
  6426. thread = context.thread
  6427. out["thread"] = {
  6428. "root_event_id": (
  6429. thread.thread_root_event_id
  6430. if thread is not None
  6431. else (root.id if root is not None else None)
  6432. ),
  6433. "root_body": root.body if root is not None else None,
  6434. "room_id": context.room.id if context.room is not None else None,
  6435. "room_name": context.room.name if context.room is not None else None,
  6436. "reply_count": thread.reply_count if thread is not None else None,
  6437. "participant_count": (
  6438. thread.participant_count if thread is not None else None
  6439. ),
  6440. "last_reply_at": (
  6441. _iso_or_none(thread.last_reply_at) if thread is not None else None
  6442. ),
  6443. "latest_reply_event_id": latest.id if latest is not None else None,
  6444. "latest_reply_body": latest.body if latest is not None else None,
  6445. }
  6446. return out
  6447. async def _search_unavailable_reason(
  6448. client: ChattoClient, exc: ChattoConnectError
  6449. ) -> str:
  6450. """Why search cannot answer, in terms the caller can act on.
  6451. The server reports one of two codes for two very different situations:
  6452. the operator turned search off, or the provider is not ready yet. The first
  6453. never resolves on its own, the second does — saying which one it is changes
  6454. what the caller should do next. ``GetStatus`` stays available while search
  6455. is disabled, so it is the one call that can tell them apart.
  6456. """
  6457. try:
  6458. status = await client.get_search_status()
  6459. except ChattoError as status_exc:
  6460. logger.debug("Chatto tools: search status unreadable: %s", status_exc)
  6461. return f"Message search is unavailable on this Chatto server ({exc.code})"
  6462. hint = _SEARCH_STATE_HINTS.get(status.state)
  6463. if hint is None:
  6464. return f"Message search is unavailable on this Chatto server ({exc.code})"
  6465. reason = f"Message search is unavailable: {hint}"
  6466. if status.retry_after_seconds:
  6467. reason += f" (retry in about {status.retry_after_seconds}s)"
  6468. return reason
  6469. async def _search_messages(args: dict) -> str:
  6470. """Search message bodies. See _handle_chatto_search_messages."""
  6471. query = str(args.get("query") or "").strip()
  6472. room_id = args.get("room_id") or ""
  6473. room_name = args.get("room_name") or ""
  6474. author_id = args.get("author_id") or ""
  6475. cursor = args.get("cursor") or ""
  6476. limit = _int_arg(args, "limit", SEARCH_PAGE_SIZE, low=1, high=SEARCH_PAGE_MAX)
  6477. created_after = _timestamp_arg(args, "created_after")
  6478. created_before = _timestamp_arg(args, "created_before")
  6479. order = _enum_arg(args, "order", _SEARCH_ORDERS, MessageSearchOrder.RELEVANCE)
  6480. group_by = _enum_arg(
  6481. args, "group_by", _SEARCH_GROUPS, MessageSearchGroupBy.UNSPECIFIED
  6482. )
  6483. scope = _enum_arg(args, "scope", _SEARCH_SCOPES, MessageSearchScope.UNSPECIFIED)
  6484. has_attachments = args.get("has_attachments")
  6485. if not query:
  6486. raise _refuse("query is required and must not be empty")
  6487. if room_id and room_name:
  6488. raise _refuse("room_id and room_name are mutually exclusive")
  6489. if has_attachments is not None and not isinstance(has_attachments, bool):
  6490. raise _refuse("has_attachments must be true or false")
  6491. if order is MessageSearchOrder.THREAD_ACTIVITY and (
  6492. group_by is not MessageSearchGroupBy.THREAD
  6493. ):
  6494. # The server refuses this combination; saying which two arguments
  6495. # collide is more useful than relaying INVALID_ARGUMENT.
  6496. raise _refuse("order 'thread_activity' requires group_by 'thread'")
  6497. if created_after and created_before and created_after >= created_before:
  6498. raise _refuse("created_after must be earlier than created_before")
  6499. async with _tool_client(_chatto_config_for_tools()) as client:
  6500. if room_id or room_name:
  6501. room_id = (await _resolve_room(client, room_id, room_name)).room.id
  6502. try:
  6503. page = await client.search_messages(
  6504. query,
  6505. room_id=room_id or None,
  6506. author_id=author_id or None,
  6507. created_after=created_after,
  6508. created_before=created_before,
  6509. has_attachments=has_attachments,
  6510. order=order,
  6511. page_size=limit,
  6512. cursor=cursor or None,
  6513. scope=scope,
  6514. group_by=group_by,
  6515. )
  6516. except ChattoConnectError as exc:
  6517. if exc.code not in {"failed_precondition", "unavailable"}:
  6518. raise
  6519. raise _refuse(await _search_unavailable_reason(client, exc)) from exc
  6520. results = page.results
  6521. # The page carries its own user directory, which answers author names
  6522. # without a member-list call. A cross-room search can still miss an
  6523. # author, so the remaining ones are resolved per room — the directory
  6524. # call is per room, and a hit names the room it came from.
  6525. author_logins = {
  6526. user_id: user.login
  6527. for user_id, user in page.users_by_id.items()
  6528. if user.login
  6529. }
  6530. unresolved_by_room: dict[str, list[str]] = {}
  6531. for result in results:
  6532. message = result.message
  6533. if message is None or not message.actor_id:
  6534. continue
  6535. if message.actor_id in author_logins:
  6536. continue
  6537. unresolved_by_room.setdefault(message.room_id, []).append(message.actor_id)
  6538. for hit_room_id, actor_ids in unresolved_by_room.items():
  6539. if hit_room_id:
  6540. author_logins.update(
  6541. await _batch_author_logins(client, hit_room_id, actor_ids)
  6542. )
  6543. return json.dumps(
  6544. {
  6545. "success": True,
  6546. "query": query,
  6547. "room_id": room_id or None,
  6548. "count": len(results),
  6549. "grouped_by_thread": group_by is MessageSearchGroupBy.THREAD,
  6550. "thread_total_count": page.thread_total_count,
  6551. "next_cursor": page.next_cursor or None,
  6552. "results": [
  6553. _search_result_out(result, author_logins) for result in results
  6554. ],
  6555. }
  6556. )
  6557. async def _handle_chatto_search_messages(args: dict) -> str:
  6558. """Tool handler for chatto_search_messages.
  6559. Returns a JSON string: Hermes' tool contract accepts no other type, and a
  6560. dict (or an unflagged coroutine) fails at dispatch with "unsupported result
  6561. type". Every early exit arrives here as a _ToolRefusal and leaves as that
  6562. refusal's own JSON, so no path escapes as an unhandled error.
  6563. """
  6564. try:
  6565. return await _search_messages(args)
  6566. except _ToolRefusal as refusal:
  6567. return refusal.result_json
  6568. except ChattoAuthError as exc:
  6569. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  6570. except ChattoError as exc:
  6571. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  6572. async def _handle_chatto_send_dm(args: dict) -> str:
  6573. """Tool handler for chatto_send_dm.
  6574. Returns a JSON string: Hermes' tool contract accepts no other type, and a
  6575. dict (or an unflagged coroutine) fails at dispatch with "unsupported result
  6576. type". Every early exit arrives here as a _ToolRefusal and leaves as that
  6577. refusal's own JSON, so no path escapes as an unhandled error.
  6578. """
  6579. try:
  6580. return await _send_dm(args)
  6581. except _ToolRefusal as refusal:
  6582. return refusal.result_json
  6583. except ChattoAuthError as exc:
  6584. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  6585. except ChattoError as exc:
  6586. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  6587. async def _handle_chatto_send_room(args: dict) -> str:
  6588. """Tool handler for chatto_send_room.
  6589. Returns a JSON string: Hermes' tool contract accepts no other type, and a
  6590. dict (or an unflagged coroutine) fails at dispatch with "unsupported result
  6591. type". Every early exit arrives here as a _ToolRefusal and leaves as that
  6592. refusal's own JSON, so no path escapes as an unhandled error.
  6593. """
  6594. try:
  6595. return await _send_room(args)
  6596. except _ToolRefusal as refusal:
  6597. return refusal.result_json
  6598. except ChattoAuthError as exc:
  6599. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  6600. except ChattoError as exc:
  6601. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  6602. async def _handle_chatto_send_ephemeral(args: dict) -> str:
  6603. """Tool handler for chatto_send_ephemeral.
  6604. Returns a JSON string: Hermes' tool contract accepts no other type, and a
  6605. dict (or an unflagged coroutine) fails at dispatch with "unsupported result
  6606. type". Every early exit arrives here as a _ToolRefusal and leaves as that
  6607. refusal's own JSON, so no path escapes as an unhandled error.
  6608. """
  6609. try:
  6610. return await _send_ephemeral(args)
  6611. except _ToolRefusal as refusal:
  6612. return refusal.result_json
  6613. except ChattoAuthError as exc:
  6614. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  6615. except ChattoError as exc:
  6616. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  6617. async def _handle_chatto_list_rooms(args: dict) -> str:
  6618. """Tool handler for chatto_list_rooms.
  6619. Returns a JSON string: Hermes' tool contract accepts no other type, and a
  6620. dict (or an unflagged coroutine) fails at dispatch with "unsupported result
  6621. type".
  6622. """
  6623. try:
  6624. return await _list_rooms(args)
  6625. except _ToolRefusal as refusal:
  6626. return refusal.result_json
  6627. except ChattoAuthError as exc:
  6628. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  6629. except ChattoError as exc:
  6630. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  6631. async def _handle_chatto_set_custom_status(args: dict) -> str:
  6632. """Tool handler for chatto_set_custom_status.
  6633. Returns a JSON string: Hermes' tool contract accepts no other type, and a
  6634. dict (or an unflagged coroutine) fails at dispatch with "unsupported result
  6635. type". Every early exit arrives here as a _ToolRefusal and leaves as that
  6636. refusal's own JSON, so no path escapes as an unhandled error.
  6637. """
  6638. try:
  6639. return await _set_custom_status(args)
  6640. except _ToolRefusal as refusal:
  6641. return refusal.result_json
  6642. except ChattoAuthError as exc:
  6643. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  6644. except ChattoError as exc:
  6645. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  6646. async def _handle_chatto_delete_custom_status(args: dict) -> str:
  6647. """Tool handler for chatto_delete_custom_status.
  6648. Returns a JSON string: Hermes' tool contract accepts no other type, and a
  6649. dict (or an unflagged coroutine) fails at dispatch with "unsupported result
  6650. type". Every early exit arrives here as a _ToolRefusal and leaves as that
  6651. refusal's own JSON, so no path escapes as an unhandled error.
  6652. """
  6653. try:
  6654. return await _delete_custom_status(args)
  6655. except _ToolRefusal as refusal:
  6656. return refusal.result_json
  6657. except ChattoAuthError as exc:
  6658. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  6659. except ChattoError as exc:
  6660. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  6661. async def _set_custom_status(args: dict) -> str:
  6662. """Set the bot's custom status. See _handle_chatto_set_custom_status."""
  6663. emoji = (args.get("emoji") or "").strip()
  6664. text = (args.get("text") or "").strip()
  6665. expires_in = args.get("expires_in_seconds")
  6666. if expires_in is not None:
  6667. expires_in = int(expires_in)
  6668. if expires_in < 0:
  6669. raise _refuse("expires_in_seconds must be non-negative")
  6670. if not emoji or not text:
  6671. raise _refuse("emoji and text are required")
  6672. async with _tool_client(_chatto_config_for_tools()) as client:
  6673. expires_at = None
  6674. if expires_in and expires_in > 0:
  6675. from datetime import UTC, datetime, timedelta
  6676. expires_at = datetime.now(UTC) + timedelta(seconds=expires_in)
  6677. await client.set_custom_status(emoji, text, expires_at=expires_at)
  6678. return json.dumps(
  6679. {
  6680. "success": True,
  6681. "emoji": emoji,
  6682. "text": text,
  6683. "expires_at": (_iso_or_none(expires_at) if expires_at else None),
  6684. }
  6685. )
  6686. async def _delete_custom_status(args: dict) -> str:
  6687. """Clear the bot's custom status. See _handle_chatto_delete_custom_status."""
  6688. async with _tool_client(_chatto_config_for_tools()) as client:
  6689. await client.delete_custom_status()
  6690. return json.dumps({"success": True, "message": "Custom status cleared"})
  6691. async def _fetch_link_preview(args: dict) -> str:
  6692. """Fetch an OpenGraph link preview. See _handle_chatto_fetch_link_preview."""
  6693. url = (args.get("url") or "").strip()
  6694. if not url:
  6695. raise _refuse("url is required")
  6696. async with _tool_client(_chatto_config_for_tools()) as client:
  6697. preview, _ = await client.fetch_link_preview(url)
  6698. return json.dumps(
  6699. {
  6700. "success": True,
  6701. "url": url,
  6702. "title": preview.title if preview else None,
  6703. "description": preview.description if preview else None,
  6704. "image_url": preview.image_url if preview else None,
  6705. "site_name": preview.site_name if preview else None,
  6706. "embed_type": preview.embed_type if preview else None,
  6707. }
  6708. )
  6709. async def _mark_thread_as_read(args: dict) -> str:
  6710. """Mark a thread as read. See _handle_chatto_mark_thread_as_read."""
  6711. room_id = args.get("room_id") or ""
  6712. room_name = args.get("room_name") or ""
  6713. thread_root_event_id = args.get("thread_root_event_id") or ""
  6714. if not room_id and not room_name:
  6715. raise _refuse("Exactly one of room_id or room_name is required")
  6716. if not thread_root_event_id:
  6717. raise _refuse("thread_root_event_id is required")
  6718. async with _tool_client(_chatto_config_for_tools()) as client:
  6719. if not room_id:
  6720. resolved = await _resolve_room(client, room_id, room_name)
  6721. room_id = resolved.room.id
  6722. prev_last_read = await client.mark_thread_as_read(room_id, thread_root_event_id)
  6723. return json.dumps(
  6724. {
  6725. "success": True,
  6726. "room_id": room_id,
  6727. "thread_root_event_id": thread_root_event_id,
  6728. "previous_last_read_at": (
  6729. _iso_or_none(prev_last_read) if prev_last_read else None
  6730. ),
  6731. }
  6732. )
  6733. async def _list_room_groups(args: dict) -> str:
  6734. """List room groups. See _handle_chatto_list_room_groups."""
  6735. async with _tool_client(_chatto_config_for_tools()) as client:
  6736. groups = await client.list_room_groups()
  6737. results: list[dict] = []
  6738. for g in groups:
  6739. results.append(
  6740. {
  6741. "id": g.id,
  6742. "name": g.name,
  6743. "description": g.description or "",
  6744. "room_count": len(g.rooms),
  6745. "room_ids": [rws.room.id for rws in g.rooms if rws.room],
  6746. "sidebar_links": [
  6747. {"id": l.id, "label": l.label, "url": l.url}
  6748. for l in g.sidebar_links
  6749. ],
  6750. }
  6751. )
  6752. return json.dumps({"success": True, "groups": results})
  6753. async def _handle_chatto_fetch_link_preview(args: dict) -> str:
  6754. """Tool handler for chatto_fetch_link_preview.
  6755. Returns a JSON string: Hermes' tool contract accepts no other type, and a
  6756. dict (or an unflagged coroutine) fails at dispatch with "unsupported result
  6757. type". Every early exit arrives here as a _ToolRefusal and leaves as that
  6758. refusal's own JSON, so no path escapes as an unhandled error.
  6759. """
  6760. try:
  6761. return await _fetch_link_preview(args)
  6762. except _ToolRefusal as refusal:
  6763. return refusal.result_json
  6764. except ChattoAuthError as exc:
  6765. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  6766. except ChattoError as exc:
  6767. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  6768. async def _handle_chatto_mark_thread_as_read(args: dict) -> str:
  6769. """Tool handler for chatto_mark_thread_as_read.
  6770. Returns a JSON string: Hermes' tool contract accepts no other type, and a
  6771. dict (or an unflagged coroutine) fails at dispatch with "unsupported result
  6772. type". Every early exit arrives here as a _ToolRefusal and leaves as that
  6773. refusal's own JSON, so no path escapes as an unhandled error.
  6774. """
  6775. try:
  6776. return await _mark_thread_as_read(args)
  6777. except _ToolRefusal as refusal:
  6778. return refusal.result_json
  6779. except ChattoAuthError as exc:
  6780. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  6781. except ChattoError as exc:
  6782. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  6783. async def _handle_chatto_get_message(args: dict) -> str:
  6784. """Tool handler for chatto_get_message.
  6785. Returns a JSON string: Hermes' tool contract accepts no other type, and a
  6786. dict (or an unflagged coroutine) fails at dispatch with "unsupported result
  6787. type". Every early exit arrives here as a _ToolRefusal and leaves as that
  6788. refusal's own JSON, so no path escapes as an unhandled error.
  6789. """
  6790. try:
  6791. return await _get_message(args)
  6792. except _ToolRefusal as refusal:
  6793. return refusal.result_json
  6794. except ChattoAuthError as exc:
  6795. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  6796. except ChattoError as exc:
  6797. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  6798. async def _handle_chatto_get_history(args: dict) -> str:
  6799. """Tool handler for chatto_get_history.
  6800. Returns a JSON string: Hermes' tool contract accepts no other type, and a
  6801. dict (or an unflagged coroutine) fails at dispatch with "unsupported result
  6802. type". Every early exit arrives here as a _ToolRefusal and leaves as that
  6803. refusal's own JSON, so no path escapes as an unhandled error.
  6804. """
  6805. try:
  6806. return await _get_history(args)
  6807. except _ToolRefusal as refusal:
  6808. return refusal.result_json
  6809. except ChattoAuthError as exc:
  6810. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  6811. except ChattoError as exc:
  6812. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  6813. async def _handle_chatto_list_room_groups(args: dict) -> str:
  6814. """Tool handler for chatto_list_room_groups.
  6815. Returns a JSON string: Hermes' tool contract accepts no other type, and a
  6816. dict (or an unflagged coroutine) fails at dispatch with "unsupported result
  6817. type". Every early exit arrives here as a _ToolRefusal and leaves as that
  6818. refusal's own JSON, so no path escapes as an unhandled error.
  6819. """
  6820. try:
  6821. return await _list_room_groups(args)
  6822. except _ToolRefusal as refusal:
  6823. return refusal.result_json
  6824. except ChattoAuthError as exc:
  6825. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  6826. except ChattoError as exc:
  6827. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  6828. async def _handle_chatto_list_room_attachments(args: dict) -> str:
  6829. """Tool handler for chatto_list_room_attachments.
  6830. Returns a JSON string: Hermes' tool contract accepts no other type, and a
  6831. dict (or an unflagged coroutine) fails at dispatch with "unsupported result
  6832. type". Every early exit arrives here as a _ToolRefusal and leaves as that
  6833. refusal's own JSON, so no path escapes as an unhandled error.
  6834. """
  6835. try:
  6836. return await _list_room_attachments(args)
  6837. except _ToolRefusal as refusal:
  6838. return refusal.result_json
  6839. except ChattoAuthError as exc:
  6840. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  6841. except ChattoError as exc:
  6842. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  6843. async def _handle_chatto_list_pinned_messages(args: dict) -> str:
  6844. """Tool handler for chatto_list_pinned_messages.
  6845. Returns a JSON string: Hermes' tool contract accepts no other type, and a
  6846. dict (or an unflagged coroutine) fails at dispatch with "unsupported result
  6847. type". Every early exit arrives here as a _ToolRefusal and leaves as that
  6848. refusal's own JSON, so no path escapes as an unhandled error.
  6849. """
  6850. try:
  6851. return await _list_pinned_messages(args)
  6852. except _ToolRefusal as refusal:
  6853. return refusal.result_json
  6854. except ChattoAuthError as exc:
  6855. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  6856. except ChattoError as exc:
  6857. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  6858. async def _handle_chatto_pin_message(args: dict) -> str:
  6859. """Tool handler for chatto_pin_message.
  6860. Returns a JSON string: Hermes' tool contract accepts no other type, and a
  6861. dict (or an unflagged coroutine) fails at dispatch with "unsupported result
  6862. type". Every early exit arrives here as a _ToolRefusal and leaves as that
  6863. refusal's own JSON, so no path escapes as an unhandled error.
  6864. """
  6865. try:
  6866. return await _pin_message(args)
  6867. except _ToolRefusal as refusal:
  6868. return refusal.result_json
  6869. except ChattoAuthError as exc:
  6870. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  6871. except ChattoError as exc:
  6872. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  6873. async def _handle_chatto_delete_message(args: dict) -> str:
  6874. """Tool handler for chatto_delete_message.
  6875. Returns a JSON string: Hermes' tool contract accepts no other type, and a
  6876. dict (or an unflagged coroutine) fails at dispatch with "unsupported result
  6877. type". Every early exit arrives here as a _ToolRefusal and leaves as that
  6878. refusal's own JSON, so no path escapes as an unhandled error.
  6879. """
  6880. try:
  6881. return await _delete_message(args)
  6882. except _ToolRefusal as refusal:
  6883. return refusal.result_json
  6884. except ChattoAuthError as exc:
  6885. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  6886. except ChattoError as exc:
  6887. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  6888. async def _handle_chatto_edit_message(args: dict) -> str:
  6889. """Tool handler for chatto_edit_message. See _handle_chatto_delete_message."""
  6890. try:
  6891. return await _edit_message(args)
  6892. except _ToolRefusal as refusal:
  6893. return refusal.result_json
  6894. except ChattoAuthError as exc:
  6895. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  6896. except ChattoError as exc:
  6897. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  6898. async def _handle_chatto_add_reaction(args: dict) -> str:
  6899. """Tool handler for chatto_add_reaction. See _handle_chatto_delete_message."""
  6900. try:
  6901. return await _add_reaction(args)
  6902. except _ToolRefusal as refusal:
  6903. return refusal.result_json
  6904. except ChattoAuthError as exc:
  6905. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  6906. except ChattoError as exc:
  6907. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  6908. async def _handle_chatto_remove_reaction(args: dict) -> str:
  6909. """Tool handler for chatto_remove_reaction. See _handle_chatto_delete_message."""
  6910. try:
  6911. return await _remove_reaction(args)
  6912. except _ToolRefusal as refusal:
  6913. return refusal.result_json
  6914. except ChattoAuthError as exc:
  6915. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  6916. except ChattoError as exc:
  6917. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  6918. async def _handle_chatto_mark_room_as_read(args: dict) -> str:
  6919. """Tool handler for chatto_mark_room_as_read. See _handle_chatto_delete_message."""
  6920. try:
  6921. return await _mark_room_as_read(args)
  6922. except _ToolRefusal as refusal:
  6923. return refusal.result_json
  6924. except ChattoAuthError as exc:
  6925. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  6926. except ChattoError as exc:
  6927. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  6928. async def _handle_chatto_create_room(args: dict) -> str:
  6929. """Tool handler for chatto_create_room. See _handle_chatto_delete_message."""
  6930. try:
  6931. return await _create_room(args)
  6932. except _ToolRefusal as refusal:
  6933. return refusal.result_json
  6934. except ChattoAuthError as exc:
  6935. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  6936. except ChattoError as exc:
  6937. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  6938. async def _handle_chatto_list_room_members(args: dict) -> str:
  6939. """Tool handler for chatto_list_room_members. See _handle_chatto_delete_message."""
  6940. try:
  6941. return await _list_room_members(args)
  6942. except _ToolRefusal as refusal:
  6943. return refusal.result_json
  6944. except ChattoAuthError as exc:
  6945. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  6946. except ChattoError as exc:
  6947. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  6948. async def _handle_chatto_get_user(args: dict) -> str:
  6949. """Tool handler for chatto_get_user. See _handle_chatto_delete_message."""
  6950. try:
  6951. return await _get_user(args)
  6952. except _ToolRefusal as refusal:
  6953. return refusal.result_json
  6954. except ChattoAuthError as exc:
  6955. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  6956. except ChattoError as exc:
  6957. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  6958. async def _handle_chatto_set_presence(args: dict) -> str:
  6959. """Tool handler for chatto_set_presence. See _handle_chatto_delete_message."""
  6960. try:
  6961. return await _set_presence(args)
  6962. except _ToolRefusal as refusal:
  6963. return refusal.result_json
  6964. except ChattoAuthError as exc:
  6965. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  6966. except ChattoError as exc:
  6967. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  6968. except ValueError as exc:
  6969. # chattolib refuses UNSPECIFIED/OFFLINE outright with a ValueError.
  6970. return json.dumps({"success": False, "error": str(exc)})
  6971. async def _handle_chatto_upload_file(args: dict) -> str:
  6972. """Tool handler for chatto_upload_file. See _handle_chatto_delete_message."""
  6973. try:
  6974. return await _upload_file(args)
  6975. except _ToolRefusal as refusal:
  6976. return refusal.result_json
  6977. except ChattoAuthError as exc:
  6978. return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
  6979. except ChattoError as exc:
  6980. return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
  6981. async def _delete_message(args: dict) -> str:
  6982. """Delete one message in a Chatto room. See _handle_chatto_delete_message."""
  6983. room_id = args.get("room_id") or ""
  6984. room_name = args.get("room_name") or ""
  6985. event_id = args.get("event_id") or ""
  6986. if not room_id and not room_name:
  6987. raise _refuse("Exactly one of room_id or room_name is required")
  6988. if not event_id:
  6989. raise _refuse("event_id is required")
  6990. cfg = _chatto_config_for_tools()
  6991. async with _tool_client(cfg) as client:
  6992. resolved = await _resolve_room(client, room_id, room_name)
  6993. room = resolved.room
  6994. # Deleting changes what the room shows everyone, so it is a write:
  6995. # a room on neither mention list stays read-only, exactly as it does
  6996. # for chatto_send_room and chatto_pin_message.
  6997. if not _room_is_writable(cfg, room):
  6998. raise _read_only_room_refusal(room.name or room.id)
  6999. if not resolved.is_member:
  7000. raise _not_joined_refusal(room, "delete a message there")
  7001. deleted = await client.delete_message(room.id, event_id)
  7002. return json.dumps(
  7003. {
  7004. "success": True,
  7005. "deleted": bool(deleted),
  7006. "room_id": room.id,
  7007. "event_id": event_id,
  7008. }
  7009. )
  7010. async def _edit_message(args: dict) -> str:
  7011. """Edit one message's text. See _handle_chatto_edit_message."""
  7012. room_id = args.get("room_id") or ""
  7013. room_name = args.get("room_name") or ""
  7014. event_id = args.get("event_id") or ""
  7015. message = args.get("message") or ""
  7016. if not room_id and not room_name:
  7017. raise _refuse("Exactly one of room_id or room_name is required")
  7018. if not event_id:
  7019. raise _refuse("event_id is required")
  7020. if not message:
  7021. raise _refuse("message (the full new text) is required")
  7022. cfg = _chatto_config_for_tools()
  7023. async with _tool_client(cfg) as client:
  7024. resolved = await _resolve_room(client, room_id, room_name)
  7025. room = resolved.room
  7026. # An edit is a write like any other: the read-only promise holds.
  7027. if not _room_is_writable(cfg, room):
  7028. raise _read_only_room_refusal(room.name or room.id)
  7029. if not resolved.is_member:
  7030. raise _not_joined_refusal(room, "edit a message there")
  7031. formatted = _normalise_outbound_text(message)
  7032. if len(formatted) > ChattoConstants.MAX_MESSAGE_LENGTH:
  7033. raise _refuse(
  7034. f"message exceeds the {ChattoConstants.MAX_MESSAGE_LENGTH}-char "
  7035. f"per-message limit ({len(formatted)}); it was not sent"
  7036. )
  7037. await client.update_message(room.id, event_id, body=formatted)
  7038. return json.dumps(
  7039. {
  7040. "success": True,
  7041. "room_id": room.id,
  7042. "event_id": event_id,
  7043. }
  7044. )
  7045. def _emoji_shortcode(emoji: str) -> str:
  7046. """The shortcode name a reaction must be sent as (see _shortcode_for_reaction_emoji)."""
  7047. return _shortcode_for_reaction_emoji(emoji)
  7048. def _reaction_result(action: str, room: Room, event_id: str, emoji: str, done: bool) -> str:
  7049. """One reaction tool's result shape, shared by add and remove."""
  7050. result: dict[str, Any] = {
  7051. "success": True,
  7052. "action": action,
  7053. "room_id": room.id,
  7054. "event_id": event_id,
  7055. "emoji": emoji,
  7056. }
  7057. if action == "add":
  7058. result["added"] = bool(done)
  7059. else:
  7060. result["removed"] = bool(done)
  7061. return json.dumps(result)
  7062. async def _add_reaction(args: dict) -> str:
  7063. """Add an emoji reaction. See _handle_chatto_add_reaction."""
  7064. room_id = args.get("room_id") or ""
  7065. room_name = args.get("room_name") or ""
  7066. event_id = args.get("event_id") or ""
  7067. emoji = (args.get("emoji") or "").strip()
  7068. if not room_id and not room_name:
  7069. raise _refuse("Exactly one of room_id or room_name is required")
  7070. if not event_id:
  7071. raise _refuse("event_id is required")
  7072. if not emoji:
  7073. raise _refuse("emoji is required")
  7074. cfg = _chatto_config_for_tools()
  7075. async with _tool_client(cfg) as client:
  7076. resolved = await _resolve_room(client, room_id, room_name)
  7077. room = resolved.room
  7078. # A reaction is visible to everyone — a write like a pin.
  7079. if not _room_is_writable(cfg, room):
  7080. raise _read_only_room_refusal(room.name or room.id)
  7081. if not resolved.is_member:
  7082. raise _not_joined_refusal(room, "react there")
  7083. shortcode = _emoji_shortcode(emoji)
  7084. added = await client.add_reaction(room.id, event_id, shortcode)
  7085. return _reaction_result("add", room, event_id, shortcode, added)
  7086. async def _remove_reaction(args: dict) -> str:
  7087. """Remove an emoji reaction. See _handle_chatto_remove_reaction."""
  7088. room_id = args.get("room_id") or ""
  7089. room_name = args.get("room_name") or ""
  7090. event_id = args.get("event_id") or ""
  7091. emoji = (args.get("emoji") or "").strip()
  7092. if not room_id and not room_name:
  7093. raise _refuse("Exactly one of room_id or room_name is required")
  7094. if not event_id:
  7095. raise _refuse("event_id is required")
  7096. if not emoji:
  7097. raise _refuse("emoji is required")
  7098. cfg = _chatto_config_for_tools()
  7099. async with _tool_client(cfg) as client:
  7100. resolved = await _resolve_room(client, room_id, room_name)
  7101. room = resolved.room
  7102. if not _room_is_writable(cfg, room):
  7103. raise _read_only_room_refusal(room.name or room.id)
  7104. if not resolved.is_member:
  7105. raise _not_joined_refusal(room, "react there")
  7106. shortcode = _emoji_shortcode(emoji)
  7107. removed = await client.remove_reaction(room.id, event_id, shortcode)
  7108. return _reaction_result("remove", room, event_id, shortcode, removed)
  7109. async def _mark_room_as_read(args: dict) -> str:
  7110. """Mark a room read for the bot. See _handle_chatto_mark_room_as_read."""
  7111. room_id = args.get("room_id") or ""
  7112. room_name = args.get("room_name") or ""
  7113. up_to_event_id = args.get("up_to_event_id") or ""
  7114. if not room_id and not room_name:
  7115. raise _refuse("Exactly one of room_id or room_name is required")
  7116. async with _tool_client(_chatto_config_for_tools()) as client:
  7117. resolved = await _resolve_room(client, room_id, room_name)
  7118. room = resolved.room
  7119. if not resolved.is_member:
  7120. # A read marker is private state, but the server scopes it to
  7121. # membership — same remedy as every other room-scoped call.
  7122. raise _not_joined_refusal(room, "mark it read")
  7123. last_read, previous = await client.mark_room_as_read(room.id, up_to_event_id)
  7124. return json.dumps(
  7125. {
  7126. "success": True,
  7127. "room_id": room.id,
  7128. "last_read_at": _iso_or_none(last_read),
  7129. "previous_last_read_at": _iso_or_none(previous),
  7130. }
  7131. )
  7132. async def _create_room(args: dict) -> str:
  7133. """Create a channel in a room group. See _handle_chatto_create_room."""
  7134. name = (args.get("name") or "").strip()
  7135. group_id = (args.get("group_id") or "").strip()
  7136. description = args.get("description") or ""
  7137. if not name:
  7138. raise _refuse("name is required")
  7139. if not group_id:
  7140. raise _refuse(
  7141. "group_id is required — chatto_list_room_groups lists the groups"
  7142. )
  7143. async with _tool_client(_chatto_config_for_tools()) as client:
  7144. room = await client.create_room(
  7145. name=name, group_id=group_id, description=description
  7146. )
  7147. return json.dumps(
  7148. {
  7149. "success": True,
  7150. "room_id": room.id,
  7151. "name": room.name,
  7152. "kind": room.kind.value,
  7153. "hint": (
  7154. "The bot is not a member of the new room yet. Invite it in "
  7155. "Chatto, or DM it `/join "
  7156. f"{room.id}`"
  7157. ),
  7158. }
  7159. )
  7160. def _member_out(member: DirectoryMember) -> dict[str, Any]:
  7161. """One directory member as the tools' JSON shape."""
  7162. user = member.user
  7163. out: dict[str, Any] = {
  7164. "roles": list(member.roles or []),
  7165. }
  7166. if user is not None:
  7167. out.update(
  7168. {
  7169. "user_id": user.id,
  7170. "login": user.login,
  7171. "display_name": user.display_name,
  7172. "presence": _PRESENCE_LABELS.get(user.presence_status, ""),
  7173. "deleted": user.deleted,
  7174. }
  7175. )
  7176. return out
  7177. async def _list_room_members(args: dict) -> str:
  7178. """List a room's members. See _handle_chatto_list_room_members."""
  7179. room_id = args.get("room_id") or ""
  7180. room_name = args.get("room_name") or ""
  7181. limit = _int_arg(args, "limit", 50, low=1, high=100)
  7182. offset = _int_arg(args, "offset", 0, low=0, high=100_000)
  7183. if not room_id and not room_name:
  7184. raise _refuse("Exactly one of room_id or room_name is required")
  7185. async with _tool_client(_chatto_config_for_tools()) as client:
  7186. resolved = await _resolve_room(client, room_id, room_name)
  7187. room = resolved.room
  7188. # The server enforces room membership for the member directory too,
  7189. # so an unjoined room is refused with the usual remedy.
  7190. if not resolved.is_member:
  7191. raise _not_joined_refusal(room, "list its members")
  7192. member_ids, page = await client.list_room_members(
  7193. room.id, limit=limit, offset=offset
  7194. )
  7195. members = await client.batch_get_room_members(room.id, member_ids)
  7196. return json.dumps(
  7197. {
  7198. "success": True,
  7199. "room_id": room.id,
  7200. "count": len(members),
  7201. "offset": offset,
  7202. "has_more": page.has_more,
  7203. "total_count": page.total_count,
  7204. "members": [_member_out(member) for member in members],
  7205. }
  7206. )
  7207. async def _get_user(args: dict) -> str:
  7208. """Look up one user. See _handle_chatto_get_user."""
  7209. user_id = (args.get("user_id") or "").strip()
  7210. user_login = (args.get("user_login") or "").strip()
  7211. if bool(user_id) == bool(user_login):
  7212. raise _refuse("Exactly one of user_id or user_login is required")
  7213. async with _tool_client(_chatto_config_for_tools()) as client:
  7214. if user_id:
  7215. matches = await client.batch_get_users([user_id])
  7216. member = matches[0] if matches else None
  7217. else:
  7218. # A login needs the server-wide directory, which batch_get_users
  7219. # cannot search; list_users' search does.
  7220. members, _page = await client.list_users(search=user_login, limit=25)
  7221. member = next(
  7222. (
  7223. m
  7224. for m in members
  7225. if m.user is not None
  7226. and m.user.login.casefold() == user_login.casefold()
  7227. ),
  7228. None,
  7229. )
  7230. if member is None or member.user is None:
  7231. raise _refuse(
  7232. f"No user found for "
  7233. f"{'user_id ' + user_id if user_id else 'login ' + user_login}. "
  7234. "The directory search may be scoped to members of rooms the "
  7235. "bot can see."
  7236. )
  7237. return json.dumps({"success": True, **_member_out(member)})
  7238. _PRESENCE_BY_NAME = {
  7239. "online": PresenceStatus.ONLINE,
  7240. "away": PresenceStatus.AWAY,
  7241. "do_not_disturb": PresenceStatus.DO_NOT_DISTURB,
  7242. }
  7243. async def _set_presence(args: dict) -> str:
  7244. """Set the bot's presence status. See _handle_chatto_set_presence."""
  7245. status_name = (args.get("status") or "").strip()
  7246. status = _PRESENCE_BY_NAME.get(status_name)
  7247. if status is None:
  7248. raise _refuse(
  7249. "status must be one of: online, away, do_not_disturb"
  7250. )
  7251. async with _tool_client(_chatto_config_for_tools()) as client:
  7252. set_status = await client.set_presence(status)
  7253. return json.dumps(
  7254. {
  7255. "success": True,
  7256. "status": _PRESENCE_LABELS.get(set_status, set_status.value),
  7257. "note": (
  7258. "Presence expires server-side without refresh; the running "
  7259. "gateway re-announces it periodically."
  7260. ),
  7261. }
  7262. )
  7263. async def _upload_file(args: dict) -> str:
  7264. """Upload a local file and post it. See _handle_chatto_upload_file."""
  7265. room_id = args.get("room_id") or ""
  7266. room_name = args.get("room_name") or ""
  7267. file_path = (args.get("file_path") or "").strip()
  7268. caption = args.get("caption") or ""
  7269. if not room_id and not room_name:
  7270. raise _refuse("Exactly one of room_id or room_name is required")
  7271. if not file_path:
  7272. raise _refuse("file_path is required")
  7273. # Same path gate as the adapter's own sends: a model-named path must not
  7274. # become a channel for reading host secrets into the chat.
  7275. safe_path = validate_media_delivery_path(file_path)
  7276. if not safe_path:
  7277. raise _refuse(
  7278. f"file_path '{file_path}' is not a deliverable file on this host "
  7279. "(missing, or outside the paths allowed for media delivery)"
  7280. )
  7281. cfg = _chatto_config_for_tools()
  7282. async with _tool_client(cfg) as client:
  7283. resolved = await _resolve_room(client, room_id, room_name)
  7284. room = resolved.room
  7285. if not _room_is_writable(cfg, room):
  7286. raise _read_only_room_refusal(room.name or room.id)
  7287. if not resolved.is_member:
  7288. raise _not_joined_refusal(room, "post an attachment there")
  7289. asset = await client.upload_attachment(
  7290. room.id, safe_path, filename=Path(safe_path).name
  7291. )
  7292. message = await client.post_message(
  7293. room.id,
  7294. _normalise_outbound_text(caption),
  7295. attachment_asset_ids=[asset.id],
  7296. )
  7297. return json.dumps(
  7298. {
  7299. "success": True,
  7300. "room_id": room.id,
  7301. "message_id": message.id,
  7302. "asset_id": asset.id,
  7303. "filename": asset.filename,
  7304. "size": asset.size,
  7305. }
  7306. )
  7307. def _register_bundled_skills(ctx) -> None:
  7308. """Register the skills shipped in ``skills/``, each with its own description.
  7309. ``register_skill`` stores the description verbatim and never reads the
  7310. SKILL.md frontmatter, so passing none leaves the skill listed in
  7311. ``<available_skills>`` with nothing explaining when to load it — which is
  7312. the only place an agent learns it exists.
  7313. """
  7314. from pathlib import Path
  7315. from agent.skill_utils import parse_frontmatter
  7316. skills_dir = Path(__file__).parent / "skills"
  7317. if not skills_dir.is_dir():
  7318. logger.debug("Chatto: no bundled skills directory at %s", skills_dir)
  7319. return
  7320. for child in sorted(skills_dir.iterdir()):
  7321. skill_md = child / "SKILL.md"
  7322. if not (child.is_dir() and skill_md.exists()):
  7323. continue
  7324. frontmatter, _ = parse_frontmatter(skill_md.read_text(encoding="utf-8"))
  7325. ctx.register_skill(
  7326. child.name,
  7327. str(skill_md),
  7328. description=str(frontmatter.get("description") or ""),
  7329. frontmatter=frontmatter,
  7330. )
  7331. logger.info("Chatto plugin skill registered: %s", child.name)
  7332. # The plugin's outbound tools: (tool name, JSON schema, handler). One list, so
  7333. # the two registration entry points below cannot disagree about what exists.
  7334. _PLUGIN_TOOLS = (
  7335. ("chatto_send_dm", CHATTO_SEND_DM_SCHEMA, _handle_chatto_send_dm),
  7336. ("chatto_send_room", CHATTO_SEND_ROOM_SCHEMA, _handle_chatto_send_room),
  7337. (
  7338. "chatto_send_ephemeral",
  7339. CHATTO_SEND_EPHEMERAL_SCHEMA,
  7340. _handle_chatto_send_ephemeral,
  7341. ),
  7342. ("chatto_list_rooms", CHATTO_LIST_ROOMS_SCHEMA, _handle_chatto_list_rooms),
  7343. ("chatto_get_message", CHATTO_GET_MESSAGE_SCHEMA, _handle_chatto_get_message),
  7344. ("chatto_get_history", CHATTO_GET_HISTORY_SCHEMA, _handle_chatto_get_history),
  7345. (
  7346. "chatto_search_messages",
  7347. CHATTO_SEARCH_MESSAGES_SCHEMA,
  7348. _handle_chatto_search_messages,
  7349. ),
  7350. (
  7351. "chatto_list_room_attachments",
  7352. CHATTO_LIST_ROOM_ATTACHMENTS_SCHEMA,
  7353. _handle_chatto_list_room_attachments,
  7354. ),
  7355. (
  7356. "chatto_list_pinned_messages",
  7357. CHATTO_LIST_PINNED_MESSAGES_SCHEMA,
  7358. _handle_chatto_list_pinned_messages,
  7359. ),
  7360. (
  7361. "chatto_pin_message",
  7362. CHATTO_PIN_MESSAGE_SCHEMA,
  7363. _handle_chatto_pin_message,
  7364. ),
  7365. (
  7366. "chatto_delete_message",
  7367. CHATTO_DELETE_MESSAGE_SCHEMA,
  7368. _handle_chatto_delete_message,
  7369. ),
  7370. (
  7371. "chatto_edit_message",
  7372. CHATTO_EDIT_MESSAGE_SCHEMA,
  7373. _handle_chatto_edit_message,
  7374. ),
  7375. (
  7376. "chatto_upload_file",
  7377. CHATTO_UPLOAD_FILE_SCHEMA,
  7378. _handle_chatto_upload_file,
  7379. ),
  7380. (
  7381. "chatto_add_reaction",
  7382. CHATTO_ADD_REACTION_SCHEMA,
  7383. _handle_chatto_add_reaction,
  7384. ),
  7385. (
  7386. "chatto_remove_reaction",
  7387. CHATTO_REMOVE_REACTION_SCHEMA,
  7388. _handle_chatto_remove_reaction,
  7389. ),
  7390. (
  7391. "chatto_mark_room_as_read",
  7392. CHATTO_MARK_ROOM_AS_READ_SCHEMA,
  7393. _handle_chatto_mark_room_as_read,
  7394. ),
  7395. (
  7396. "chatto_create_room",
  7397. CHATTO_CREATE_ROOM_SCHEMA,
  7398. _handle_chatto_create_room,
  7399. ),
  7400. (
  7401. "chatto_list_room_members",
  7402. CHATTO_LIST_ROOM_MEMBERS_SCHEMA,
  7403. _handle_chatto_list_room_members,
  7404. ),
  7405. (
  7406. "chatto_get_user",
  7407. CHATTO_GET_USER_SCHEMA,
  7408. _handle_chatto_get_user,
  7409. ),
  7410. (
  7411. "chatto_set_presence",
  7412. CHATTO_SET_PRESENCE_SCHEMA,
  7413. _handle_chatto_set_presence,
  7414. ),
  7415. (
  7416. "chatto_set_custom_status",
  7417. CHATTO_SET_CUSTOM_STATUS_SCHEMA,
  7418. _handle_chatto_set_custom_status,
  7419. ),
  7420. (
  7421. "chatto_delete_custom_status",
  7422. CHATTO_DELETE_CUSTOM_STATUS_SCHEMA,
  7423. _handle_chatto_delete_custom_status,
  7424. ),
  7425. (
  7426. "chatto_fetch_link_preview",
  7427. CHATTO_FETCH_LINK_PREVIEW_SCHEMA,
  7428. _handle_chatto_fetch_link_preview,
  7429. ),
  7430. (
  7431. "chatto_mark_thread_as_read",
  7432. CHATTO_MARK_THREAD_AS_READ_SCHEMA,
  7433. _handle_chatto_mark_thread_as_read,
  7434. ),
  7435. (
  7436. "chatto_list_room_groups",
  7437. CHATTO_LIST_ROOM_GROUPS_SCHEMA,
  7438. _handle_chatto_list_room_groups,
  7439. ),
  7440. )
  7441. def register_plugin_tools(ctx) -> None:
  7442. """Register the plugin's outbound tools in the ``chatto-platform`` toolset.
  7443. Reached through two doors: ``register()`` in a gateway/web process, which
  7444. materializes the platform, and ``tools.register_tools`` in a CLI/TUI one,
  7445. which never does. A tool list written out twice would mean tools that exist
  7446. in only half the sessions, so both doors call this.
  7447. """
  7448. for name, schema, handler in _PLUGIN_TOOLS:
  7449. ctx.register_tool(
  7450. name=name,
  7451. toolset="chatto-platform",
  7452. schema=schema,
  7453. handler=handler,
  7454. is_async=True,
  7455. description=schema["description"],
  7456. )
  7457. def register(ctx) -> None:
  7458. """Plugin entry point — called by the Hermes plugin system."""
  7459. logger.info("Registering Chatto platform plugin on Hermes Agent")
  7460. # --- Plugin tools ---
  7461. register_plugin_tools(ctx)
  7462. # --- Bundled skills ---
  7463. # Ship the plugin's own usage/development knowledge with the code, so any
  7464. # agent using this plugin knows when/how to use its tools and how to
  7465. # extend it. Loaded via skill_view("chatto-platform:chatto-platform-plugin").
  7466. _register_bundled_skills(ctx)
  7467. for capability in _capabilities():
  7468. logger.info("Chatto capability: %s", capability)
  7469. ctx.register_platform(
  7470. name=ChattoConstants.PLATFORM_NAME, # this will be the config.yaml key.
  7471. label=ChattoConstants.PLATFORM_LABEL,
  7472. adapter_factory=hermes_adapter_factory,
  7473. check_fn=hermes_check_fn,
  7474. validate_config=hermes_validate_config,
  7475. is_connected=hermes_is_connected,
  7476. install_hint=ChattoConstants.INSTALL_HINT,
  7477. env_enablement_fn=hermes_env_enablement_fn,
  7478. setup_fn=hermes_setup_fn,
  7479. cron_deliver_env_var=ChattoConfiguration.home_channel.env_name,
  7480. standalone_sender_fn=hermes_standalone_sender_fn,
  7481. allowed_users_env=ChattoConfiguration.allowed_users.env_name,
  7482. allow_all_env=ChattoConfiguration.allow_all_users.env_name,
  7483. max_message_length=ChattoConstants.MAX_MESSAGE_LENGTH,
  7484. emoji="😺",
  7485. allow_update_command=True,
  7486. pii_safe=False,
  7487. platform_hint=(
  7488. "Using the 'Hermes Chatto Platform Plugin' you connect to a Chatto server. "
  7489. "Authorized admins and users will contact you and call you his Hermes Agent. They are "
  7490. "natural persons and thus responsible for what they do in terms of rights. You compute "
  7491. "on their behalf. They _may_ address you by @-mentioning your name. If configured, "
  7492. "you also react without a @-mention. Direct messages reach you without a mention."
  7493. "Keep responses conversational. Markdown is supported. "
  7494. "Include MEDIA:/absolute/path/to/file in your response to refer to our local files. Images "
  7495. "(.png, .jpg, .gif, .webp) arrive as inline pictures, videos (.mp4, .mov, .webm) as "
  7496. "video attachments, audio as a voice bubble, anything else as a downloadable document. "
  7497. "Do NOT use markdown image syntax for local files. Local files always go through MEDIA:. "
  7498. "Several images in one response are bundled into a single message. "
  7499. "You can also talk to Chatto unprompted: the plugin tools chatto_list_rooms, "
  7500. "chatto_send_room and chatto_send_dm post to a channel or a person's DM on your "
  7501. "behalf. Only send into a channel that the bot answers in, and reach a person by DM "
  7502. "only once they have written to the bot themselves. "
  7503. "chatto_get_history and chatto_get_message read instead of posting: use them to "
  7504. "look up what was said in a room or thread, and to resolve an event ID you were given. "
  7505. "chatto_search_messages finds messages by their words when you do not know the room, "
  7506. "thread or day — it only answers when the server has search enabled, and says so when "
  7507. "it has not. "
  7508. ),
  7509. )