+
    .pÐjZ1  ã                  ó²   € R t ^ RIHt ^ RIt^ RIt^ RIt^ RIt^ RIt^ RI	H
t
 ^ RIHtHtHt ^ RIHtHt ^ RIHt ]P(                  ! R4      t ! R R	4      tR# )
uŽ  Plugin-triggered gateway turns (``PluginContext.inject_message``): the process-wide injector this
runner publishes, the thread-safe scheduler, and the dispatch through the adapter message path.

Two targets: an existing session (``session_key``; route and session id pinned from the store) or a
messaging chat (``origin``) whose session is created â€” or continued â€” through the normal session path
in the calling plugin's OWN profile. The origin path never chooses a profile: the gateway's routing
for that chat through that profile's adapter must already land in the plugin's profile, so a human
follow-up in the same chat keys into the same session.)ÚannotationsN)ÚPath)ÚAnyÚMappingÚOptional)ÚMessageEventÚMessageType)ÚSessionSourcezgateway.runc                  óž   € ] tR t^tR R ltR R ltRRRRRR/R	 R
 lltR R ltR R ltR R lt	R R lt
R R ltR R ltR R ltRtR# )ÚGatewayPluginInjectionMixinc               ó   € V ^8„  d   QhRR/# ©é   ÚreturnÚNone© )Úformats   "Ú+/opt/hermes/gateway/run_plugin_injection.pyÚ__annotate__Ú(GatewayPluginInjectionMixin.__annotate__   s   € ÷ Tñ T°$ñ Tó    c                ó6   € ^ RI Hp V! W P                  4       R# )zBPublish this live gateway's plugin message scheduler process-wide.)Úpublish_gateway_message_hostN)Úhermes_cli.pluginsr   Ú"_schedule_plugin_message_injection)Úselfr   s   & r   Ú _install_plugin_message_injectorÚ<GatewayPluginInjectionMixin._install_plugin_message_injector   s   € åCá$ T×+RÑ+RÖSr   c               ó   € V ^8„  d   QhRR/# r   r   )r   s   "r   r   r       s   € ÷ 3ñ 3°ñ 3r   c                ó"   € ^ RI Hp V! V 4       R# )z@Remove this runner's scheduler without clobbering a newer owner.)Ú$clear_published_gateway_message_hostN)r   r    )r   r    s   & r   Ú_clear_plugin_message_injectorÚ:GatewayPluginInjectionMixin._clear_plugin_message_injector    s   € åKá,¨TÖ2r   Úsession_keyNÚoriginÚplugin_homec               ó0   € V ^8„  d   QhRRRRRRRRRR	R
R/# )r   ÚcontentÚstrÚ	plugin_idr#   úOptional[str]r$   zOptional[Mapping[str, Any]]r%   úOptional[Path]r   Úboolr   )r   s   "r   r   r   &   s>   € ÷ :ñ :Øð:Ø*-ð:Ø<Ið:à+ð:àAOð:ð 
ñ:r   c               ó^  aa€ ^ RI Hp \        V RR4      p\        V RR4      '       d   Ve   VP                  4       '       d   R# VeT   V P	                  WESR7      pVf   R# RVP
                  P                   RVP                   2oV P                  W�SR	7      p	MR
V 2oV P                  W1SR7      p	 \        P                  ! 4       p
W§J dT    VP                  V	4      pT P$                  P'                  T4       TP)                  T P$                  P*                  4       M$V! W—\         R\,        P.                  R7      pVf   R# R VV3R llpVP)                  V4       R#   \         d    Rp
 L¨i ; i  \         d,    T	P                  4        \         P#                  RRR7        R# i ; i)zûSchedule a plugin-triggered turn on the live gateway loop (thread-safe).

With ``origin`` the profile, origin and adapter checks run here, synchronously, so the plugin
gets ``False`` for a chat it may not start; authorization is rechecked at dispatch.)Úsafe_schedule_threadsafeÚ_gateway_loopNÚ_runningF)r%   r)   úorigin=Ú/)Úsourcer'   r)   úsession=)r#   r'   r)   z*Plugin message injection scheduling failedT©Úexc_info)ÚloggerÚlog_messageÚ	log_levelc               ó   € V ^8„  d   QhRR/# r   r   )r   s   "r   r   ÚTGatewayPluginInjectionMixin._schedule_plugin_message_injection.<locals>.__annotate__T   s   € ÷ 		gñ 		g dñ 		gr   c                ó"  <€  V P                  4       '       d   R #  \        P                  RSS4       R #   \        P                  \        P                  P                  3 d     R # \
         d    \        P                  RSSRR7        R # i ; i)Nz-Plugin message injection failed: plugin=%s %sTr5   z5Plugin message injection was not routed: plugin=%s %s)ÚresultÚasyncioÚCancelledErrorÚ
concurrentÚfuturesÚ	Exceptionr7   Úwarning)Ú	completedr)   Útargets   &€€r   Ú_log_resultÚSGatewayPluginInjectionMixin._schedule_plugin_message_injection.<locals>._log_resultT   s‚   ø€ ðØ×#Ñ#×%Ò%Ùð &ô �N‰NÐRÐT]Ð_eÖføô ×*Ñ*¬J×,>Ñ,>×,MÑ,MÐNô ÚÜô Ü—‘ÐNÐPYÐ[aÐlp�ÔqÚðús   ƒ5 µ/BÁ'BÁ0BÂB)Úgateway.runr.   ÚgetattrÚ	is_closedÚ_plugin_origin_sourceÚplatformÚvalueÚchat_idÚ!_dispatch_plugin_origin_injectionÚ"_dispatch_plugin_message_injectionr>   Úget_running_loopÚRuntimeErrorÚcreate_taskrB   Úcloser7   rC   Ú_background_tasksÚaddÚadd_done_callbackÚdiscardÚloggingÚWARNING)r   r'   r)   r#   r$   r%   r.   Úloopr3   ÚcoroÚcurrent_loopÚfuturerF   rE   s   &$d$$$       @r   r   Ú>GatewayPluginInjectionMixin._schedule_plugin_message_injection&   s¡  ù€ õ 	9Ü�t˜_¨dÓ3ˆÜ�t˜Z¨×/Ò/°4²<À4Ç>Á>×CSÒCSÙàÒØ×/Ñ/°Ð[dÐ/ÓeˆFØŠ~ÙØ˜vŸ™×4Ñ4Ð5°Q°v·~±~Ð6FÐGˆFØ×9Ñ9ÀÐdmÐ9Ón‰Dà ˜}Ð-ˆFØ×:Ñ:Ø'ÀIð ;ó ˆDð	 Ü"×3Ò3Ó5ˆLð ÓðØ×)Ñ)¨$Ó/�ð
 ×"Ñ"×&Ñ& vÔ.Ø×$Ñ$ T×%;Ñ%;×%CÑ%CÕDá-Ø¤6Ð7cÜ!Ÿ/™/ôˆFð Š~Ù÷		gñ 		gð 	× Ñ  Ô-ÙøôA ô 	 ØŠLð	 ûô ô Ø—
‘
”Ü—‘ÐKÐVZ�Ô[Úðús$   Â5E# ÃE6 Å#E3Å2E3Å62F,Æ+F,c               ó   € V ^8„  d   QhRR/# )r   r   r,   r   )r   s   "r   r   r   b   s   € ÷ Zñ Z¨Tñ Zr   c                	óP   € \        V R R4      ;'       d    \        V RR4      '       * # )r0   FÚ	_draining)rI   )r   s   &r   Ú_plugin_injection_acceptingÚ7GatewayPluginInjectionMixin._plugin_injection_acceptingb   s&   € Ü�t˜Z¨Ó/×YÐY¼ÀÀkÐSXÓ8YÔ4YÐYr   c               ó   € V ^8„  d   QhRR/# )r   r3   r	   r   )r   s   "r   r   r   e   s   € ÷ [ñ [¨mñ [r   c                óž   € \        \        V RR4      RR4      RJd   \        P                  ! 4       # ^ RIHp V! V P                  V4      4      # )u:  Multiplex: enter the source's profile runtime scope around ``handle_message`` so the
background task it spawns â€” the turn AND the reply send after the handler returns â€” inherits
that profile's home and secrets (this coroutine runs from the default loop context, not from
the profile adapter's own intake task).ÚconfigNÚmultiplex_profilesFT)Ú_async_profile_runtime_scope)rI   Ú
contextlibÚnullcontextrH   ri   Ú _resolve_profile_home_for_source)r   r3   ri   s   && r   Ú_plugin_injection_scopeÚ3GatewayPluginInjectionMixin._plugin_injection_scopee   sI   € ô
 ”7˜4 ¨4Ó0Ð2FÈÓNÐVZÓZÜ×)Ò)Ó+Ð+Ý<Ù+¨D×,QÑ,QÐRXÓ,YÓZÐZr   c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   r3   r	   r)   r(   rE   r   r,   r   )r   s   "r   r   r   o   s+   € ÷ ñ °=ð ÐPSð Ð]`ð Ðeiñ r   c               ó¾   €  V P                  VRR7      pT'       g   \        P                  RY#4       T#   \         d    \        P                  RY#RR7        R# i ; i)a  Current gateway authorization for the route the injected turn will run as. Host-side
plugin code is trusted to ask, never to widen access: a stored or supplied route must pass
today's allowlists / pairing / allow-all, and adapter-time grants (Discord roles) do not count.F)Úallow_adapter_delegationzAPlugin message injection authorization check failed: plugin=%s %sTr5   zNPlugin message injection denied by current gateway authorization: plugin=%s %s)Ú_is_user_authorized_for_sourcerB   r7   rC   )r   r3   r)   rE   Ú
authorizeds   &&$$ r   Ú_plugin_injection_authorizedÚ8GatewayPluginInjectionMixin._plugin_injection_authorizedo   sg   € ð	Ø×<Ñ<¸VÐ^cÐ<ÓdˆJ÷
 Ü�N‰NÐkØ$ô.àÐøô ô 	Ü�N‰NÐ^Ø$°tð ô =âð	ús   ‚5 µ#AÁAc               ó(   € V ^8„  d   QhRRRRRRRR/# )r   r#   r(   r'   r)   r   r,   r   )r   s   "r   r   r   ~   s,   € ÷ !ñ !Ø!ð!Ø,/ð!Ø<?ð!à	ñ!r   c             ƒ  ó   "  € V P                  4       '       g   R# V P                  P                  V4      G Rj  x€L
 pVe%   VP                  e   V P                  4       '       g   R# ^ RIHp V! V P                  V4      4      pV P                  WcRV 2R7      '       g   R# V P                  V4      pVf   R# V P                  V4      ;_uu_4       GRj  x€L
  VP                  \        V\        P                  VRRRVRRR	VR
VP                  RR/R7      4      G Rj  x€L
  RRR4      GRj  x€L
  \        P!                  RW1VP                  4       R#  EL  L„ L< L.  + GRj  x€L 
 '       g   i     LE; i5i)zARoute a plugin-triggered turn through the session's live adapter.FN)Úreplace_sourcer4   ©r)   rE   TÚhermes_plugin_idÚhermes_plugin_injectionÚgateway_session_keyÚgateway_session_idÚgateway_session_strict©ÚtextÚmessage_typer3   ÚinternalÚallow_gateway_controlÚmetadatazGPlugin message injection dispatched: plugin=%s session=%s session_id=%s)rc   Úasync_session_storeÚlookup_by_session_keyr$   Úgateway.session_identityrx   Ú_restored_sourcert   Ú_delivery_adapter_forrm   Úhandle_messager   r   ÚTEXTÚ
session_idr7   Úinfo)r   r#   r'   r)   Úentryrx   r3   Úadapters   &$$$    r   rP   Ú>GatewayPluginInjectionMixin._dispatch_plugin_message_injection~   sU  é € ð ×/Ñ/×1Ò1ÙØ×.Ñ.×DÑDÀ[ÓQ×QˆØŠ=˜EŸL™LÒ0¸×8XÑ8X×8ZÒ8ZÙå;Ù × 5Ñ 5°eÓ <Ó=ˆØ×0Ñ0°ÐW_Ð`kÐ_lÐUmÐ0×nÒnÙà×,Ñ,¨VÓ4ˆØŠ?Ùà×/Ñ/°×7×7Õ7Ø×(Ñ(¬Ø¬;×+;Ñ+;ÀFÐUYØ&+à&¨	Ð3LÈdØ)¨;Ð8LÈe×N^ÑN^Ø,¨dðô*ó ÷ ð ÷ 8×7ô 	�‰ØUØ E×$4Ñ$4ô	
ñ ò7 Rñ 8ñ÷ 8×7×7Ò7üsw   ‚7E>¹Eº+E>Á&A1E>ÃEÃE>ÃAE"Ä!EÄ"E"Ä&E>Ä1E Ä2(E>ÅE>ÅE"Å E>Å"E;	Å(E+Å)
E;	Å4E;	Å6E>c               ó    € V ^8„  d   QhRRRR/# )r   Úhomer+   r   r*   r   )r   s   "r   r   r   ¡   s   € ÷ [ñ [¨^ð [Àñ [r   c                ó’  aa€ Vf   R# ^ RI HpHo S! V4      o\        \        V RR4      RR4      '       d1   ^ RIHp  V! V P                  4      p\        TT3R
 lT 4       R4      # S! V! 4       4      S8w  d   R# \        V RR4      ;'       g    V P                  4       #   \         d    \        P                  RRR	7        R# i ; i)z‡Name of the profile this gateway serves at *home* (a plugin manager's immutable home), or
``None`` when this gateway does not serve it.N)Úget_routing_process_hermes_homeÚhermes_home_keyrg   rh   F)Ú_multiplex_profile_homesz8Plugin message injection: served-profile set unavailableTr5   c              3  óJ   <"  € T F  w  rS! V4      S8X  g   K  Vx € K  	  R # 5i)Nr   )Ú.0ÚnameÚserved_homer•   Úkeys   &  €€r   Ú	<genexpr>ÚGGatewayPluginInjectionMixin._served_profile_for_home.<locals>.<genexpr>¯   s$   øé € Ðe±vÑ"3 $ÁÐQ\ÓA]ÐadÑAdŸš³vùs   ƒ#™
#Ú_primary_profile_name)Úhermes_constantsr”   r•   rI   rH   r–   rg   rB   r7   rC   ÚnextÚ_active_profile_name)r   r’   r”   r–   Úservedr•   r›   s   &&   @@r   Ú_served_profile_for_homeÚ4GatewayPluginInjectionMixin._served_profile_for_home¡   sº   ù€ ð Š<ÙßUÙ˜dÓ#ˆÜ”7˜4 ¨4Ó0Ð2FÈ×NÒNÝ<ðÙ1°$·+±+Ó>�ô Õe±vÓeÐgkÓlÐlÙÑ:Ó<Ó=ÀÔDÙÜ�tÐ4°dÓ;×ZÐZ¸t×?XÑ?XÓ?ZÐZøô ô Ü—‘ÐYÐdh�ÔiÚðús   ¾B  Â "CÃCc               ó(   € V ^8„  d   QhRRRRRRRR/# )	r   r$   zMapping[str, Any]r%   r+   r)   r(   r   zOptional[SessionSource]r   )r   s   "r   r   r   ´   s,   € ÷ $ñ $Ø'ð$Ø9Gð$ØTWð$à	 ñ$r   c               óâ  a€ R V3R llpV P                  V4      pVf
   V! RV4      # \        V4      pVP                  RR4      pV'       d   Wu8w  d
   V! RWu4      #  \        P                  ! V4      pTP                  P                   RTP                   2p
T P                  T4      P                  TP                  4      pTf   T! R	TP                  P                  T4      # \        P                  ! T4      Tn        T P!                  T4      pTf
   T! R
T
4      # TP"                  T8w  d   T! RY¬P"                  T4      # T#   \
        \        \        3 d   p	T! RT	4      u Rp	?	# Rp	?	ii ; i)aA  The canonicalized source for a plugin-supplied *origin*, or ``None`` (logged) when the
plugin may not start a turn there: its profile is not served, the origin is malformed or
names another profile, that profile has no live adapter for the platform, or the gateway's
own routing for the chat lands in a different profile.c               ó$   € V ^8„  d   QhRRRRRR/# )r   Úreasonr(   Úargsr   r   r   r   )r   s   "r   r   ÚGGatewayPluginInjectionMixin._plugin_origin_source.<locals>.__annotate__»   s&   € ÷ 	fñ 	f˜Cð 	f¨ð 	f°ñ 	fr   c                óH   <€ \         P                  ! R V ,           S.VO5!   R# )z,Plugin message injection refused: plugin=%s N)r7   rC   )r¨   r©   r)   s   &*€r   Ú_refuseÚBGatewayPluginInjectionMixin._plugin_origin_source.<locals>._refuse»   s   ø€ Ü�NŠNÐIÈFÕRÐT]ÐeÐ`dÖer   Nz1its profile home %s is not served by this gatewayÚprofilezRorigin names profile %r; plugins may only start sessions in their own profile (%s)zinvalid origin (%s)r2   z&no connected %s adapter for profile %sz.origin %s does not resolve to a served profilez;origin %s routes to profile %s, not the plugin's profile %s)r£   ÚdictÚpopr	   Ú	from_dictÚKeyErrorÚ	TypeErrorÚ
ValueErrorrL   rM   rN   Ú_adapters_for_profileÚgetÚweakrefÚrefÚ_transport_adapter_refÚ_canonicalizeÚruntime_profile)r   r$   r%   r)   r¬   r®   ÚfieldsÚ	requestedr3   ÚexcÚwherer�   Úidentitys   &&$d         r   rK   Ú1GatewayPluginInjectionMixin._plugin_origin_source´   sm  ø€ ÷	fð 	fð ×/Ñ/°Ó<ˆØŠ?ÙÐNÐP[Ó\Ð\Ü�f“ˆØ—J‘J˜y¨$Ó/ˆ	ß˜Ô-ÙÐoØ$ó/ð /ð	7Ü"×,Ò,¨VÓ4ˆFð —?‘?×(Ñ(Ð)¨¨6¯>©>Ð*:Ð;ˆØ×,Ñ,¨WÓ5×9Ñ9¸&¿/¹/ÓJˆØŠ?ÙÐCÀVÇ_Á_×EZÑEZÐ\cÓdÐdô )0¯ª°GÓ(<ˆÔ%Ø×%Ñ% fÓ-ˆØÒÙÐKÈUÓSÐSØ×#Ñ# wÔ.ÙÐXØ ×":Ñ":¸GóEð Eàˆøô! œ)¤ZÐ0ô 	7ÙÐ0°#Ó6Õ6ûð	7ús   ÁE ÅE.ÅE)Å#E.Å)E.c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   r3   r	   r'   r(   r)   r   r,   r   )r   s   "r   r   r   Ú   s,   € ÷ ñ Ø&ðØ14ðØADðà	ñr   c             ƒ  óŠ  "  € RVP                   P                   RVP                   2pV P                  4       '       g   R# V P	                  V4      pVf   \
        P                  RV4       R# V P                  WVR7      '       g   R# V P                  V4      pV P                  V4      ;_uu_4       GRj  x€L
  VP                  \        V\        P                  VRRRVR	RR
V/R7      4      G Rj  x€L
  RRR4      GRj  x€L
  \
        P                  RW4V4       R#  Li L/ L!  + GRj  x€L 
 '       g   i     L8; i5i)z®Start (or continue) the session at a plugin-supplied origin through the adapter's normal
message path: ``get_or_create_session`` creates it, the reply goes back to that chat.r1   r2   FNzBPlugin message injection: adapter for %s went away before dispatchry   Trz   r{   r|   r   z<Plugin message injection dispatched: plugin=%s %s session=%s)rL   rM   rN   rc   r‰   r7   rC   rt   Ú_session_key_for_sourcerm   rŠ   r   r   r‹   r�   )r   r3   r'   r)   rE   r�   r#   s   &$$$   r   rO   Ú=GatewayPluginInjectionMixin._dispatch_plugin_origin_injectionÚ   s!  é € ð
 ˜6Ÿ?™?×0Ñ0Ð1°°6·>±>Ð2BÐCˆØ×/Ñ/×1Ò1ÙØ×,Ñ,¨VÓ4ˆØŠ?Ü�N‰NÐ_ÐagÔhÙØ×0Ñ0°ÐU[Ð0×\Ò\ÙØ×2Ñ2°6Ó:ˆØ×/Ñ/°×7×7Õ7Ø×(Ñ(¬Ø¬;×+;Ñ+;ÀFÐUYØ&+ð '¨	Ð3LÈdØ)¨;ðô	*ó ÷ ð ÷ 8×7ô 	�‰ÐRÐT]ÐgrÔsÙñ 8ñ÷ 8×7×7Ò7üs`   ‚B5EÂ7D!Â8EÂ;8D'Ã3D#Ã4D'Ã8EÄD%ÄEÄ#D'Ä%EÄ'E 	Ä-D0Ä.
E 	Ä9E 	Ä;Er   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__r   r!   r   rc   rm   rt   rP   r£   rK   rO   Ú__static_attributes__r   r   r   r   r      sX   † õTõ3ð:ØLPð:à.2ð:àRV÷:õxZõ[õõ!õF[õ&$÷Lñ r   r   )Ú__doc__Ú
__future__r   r>   Úconcurrent.futuresr@   rj   rY   r·   Úpathlibr   Útypingr   r   r   Úgateway.platforms.eventr   r   Úgateway.sessionr	   Ú	getLoggerr7   r   r   r   r   Ú<module>rÓ      sI   ðñ:õ #ã Û Û Û Û Ý ß )Ñ )ç =Ý )à	×	Ò	˜=Ó	)€÷[ó [r   