Ë
    ãß²jõ  ã                  ó>  — d Z ddlmZ ddlZddlZddlmZ ddlmZm	Z	m
Z
mZmZmZ ddlmZ ddlmZ ddlmZmZ d	Zd
ZdZdZdZdZdZdZe	e
eef   gee   f   Ze G d„ d«      «       Zdd„Z ddœ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Z!ddœ	 	 	 	 	 	 	 	 	 	 	 dd„Z"dd„Z#dd„Z$y)a3  The one lifecycle every ``manage_connections`` operation follows, whatever the target kind.

``run_operation`` mints the op, registers it in ``live``, lets the kind prepare its targets (a
managed mint, an MCP catalog check), emits the card through the session callback, then loops:
sleep on ``op.wake`` for at most one tick, run the kind's ``observe`` hook, settle when every
target is resolved or the deadline passes. ``connection.respond`` reaches the loop by transitioning
the op through ``live`` and setting ``wake``; ``/stop`` sets the thread interrupt flag.é    )ÚannotationsN)Ú	dataclass)ÚAnyÚCallableÚDictÚListÚOptionalÚSequence)Úlive)ÚSettleReason)ÚConnectionOperationÚTargetÚunknown_targetÚmixed_kindsÚlink_still_validÚsettledÚnot_allowedÚrefusedg      @g      Ð?c                  ó0   — e Zd ZU dZded<   ded<   ded<   y)ÚKindz¸Per-kind hooks. ``prepare`` runs once before the card; ``observe`` runs every tick and may
    transition targets; ``note`` is the model-facing guidance appended to the settled result.z%Callable[[ConnectionOperation], None]ÚprepareÚobserveÚstrÚnoteN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__© ó    ú9/home/ubuntu/.hermes/hermes-agent/tools/connectors/run.pyr   r   "   s   … ñað 3Ó2Ø2Ó2Ø
„Ir!   r   c                ó¨  ‡
‡— ddl m}mŠ
mŠ |D �cg c]  }| j	                  |«      ‘Œ }}t        d„ |D «       «      rt        S t        |D �ch c]  }|j                  ’Œ c}«      dk7  rt        S |D �cg c]3  }|j                  ‰
j                  ‰
j                  fv sŒ(|j                  ‘Œ5 }}t        |«      t        |«      k7  rt        S | j                  rt         S t        ˆ
ˆfd„|D «       «      rt"        S |d   j                  dk(  r%ddlm}m}  | |«       | |d|j*                  ¬	«       y dd
lm}	  |	| |«      rt0        S d S c c}w c c}w c c}w )Nr   )ÚActorÚTargetStateÚallowedc              3  ó$   K  — | ]  }|d u –— Œ
 y ­w©Nr    )Ú.0Útargets     r"   ú	<genexpr>zreissue.<locals>.<genexpr>0   s   è ø€ Ò
0˜fˆ6�TŒ>Ñ
0ùs   ‚é   c              3  ór   •K  — | ].  } ‰|j                   |j                  ‰j                  «      d u –— Œ0 y ­wr(   )ÚkindÚstateÚ	initiated)r)   r*   r%   r&   s     €€r"   r+   zreissue.<locals>.<genexpr>9   s.   øè ø€ Ò
cÐQW‰7�6—;‘; §¡¨k×.CÑ.CÓDÈÔLÑ
cùs   ƒ47Ú	connector)Úmanaged_clientÚmintT)Ú
reinitiateÚactor)Úretry)Útools.connectors.contractr$   r%   r&   r*   ÚanyÚUNKNOWN_TARGETÚlenr.   ÚMIXED_KINDSr/   ÚfailedÚexpiredÚnameÚLINK_STILL_VALIDr   ÚSETTLEDÚNOT_ALLOWEDÚtools.connectors.managedr2   r3   ÚuserÚtools.connectors.mcpr6   ÚREFUSED)Ú	operationÚnamesr$   r>   Útargetsr*   Ústaler2   r3   r6   r%   r&   s             @@r"   ÚreissuerJ   ,   s  ù€ ßEÑEà27Ö8¨$ˆy×Ñ Õ%Ð8€GÐ8Ü
Ñ
0¨Ô
0Ô0ÜÐÜ
 gÖ.˜FˆF�K‹KÒ.Ó/°1Ò4ÜÐØ'.Öl˜V°&·,±,À;×CUÑCUÐWb×WjÑWjÐBkÒ2kˆV�[‹[Ðl€EÐlÜ
ˆ5ƒz”S˜“\Ò!ÜÐØ×ÒÜˆÜ
Ô
cÐ[bÔ
cÔcÜÐØˆq�z‡�˜+Ò%ßAá‰^Ó˜y¨%¸DÈÏ
É
ÕSØÝ*á˜I uÔ-Œ7Ð7°4Ð7ùò' 9ùò /ùâls   ‘EÁE
Á3)EÂE)Útick_secondsc               óö   — t        | ||¬«      }	 t        j                  |«       t        |||||¬«      S # t        j                  $ r0}ddlm}	  |	d|j                  j                  › d�«      cY d}~S d}~ww xY w)zQBlock the tool thread until the operation settles; return the tool's JSON string.)Úsession_keyÚtool_call_idr   )Ú
tool_errorz8a connection operation is already open in this session (zj); it settles when the user finishes with the card, on Continue, or at its deadline. Do not start another.N)Úconnection_callbackrK   Úwith_urls_in_result)	r   r   ÚopenÚOperationAlreadyOpenÚtools.registryrO   ÚexistingÚop_idÚdrive_operation)
rH   r.   rM   rN   rP   rK   rQ   rF   ÚexcrO   s
             r"   Úrun_operationrY   E   s‰   € ô $ G¸ÐS_Ô`€Ið
Ü�	‰	�)Ôô ØØØ/Ø!Ø/ôð øô ×$Ñ$ò 
Ý-áØFÀsÇ|Á|×GYÑGYÐFZð [kð kó
õ 	
ûð
ús   �5 µA8Á%A3Á-A8Á3A8c               ó€  — 	 |j                  | «       | j                  «        |�"| j                  s || j                  «       «       t	        | ||«       t        j                  | «       | j                  |¬«      }d|d<   |j                  |d<   t        j                  |d¬«      S # t        j                  | «       w xY w)N)Ú	with_urlsr   Ústatusr   F)Úensure_ascii)r   Úsettle_if_all_resolvedr   Úrequest_payloadÚ_watchr   ÚcloseÚresultr   ÚjsonÚdumps)rF   r.   rP   rK   rQ   Úpayloads         r"   rW   rW   c   s¢   € ðØ�‰�YÔØ×(Ñ(Ô*ØÐ*°9×3DÒ3DÙ 	× 9Ñ 9Ó ;Ô<Üˆy˜$ Ô-ä�
‰
�9ÔØ×ÑÐ)<ÐÓ=€GØ!€GˆHÑØ—i‘i€GˆF�OÜ�:‰:�g¨EÔ2Ð2øô	 	�
‰
�9Õús   ‚AB& Â&B=c                óœ  — ddl m} |€t        n|}| j                  s° |«       r | j	                  t
        j                  «       y t        j                  «       | j                  k\  r | j	                  t
        j                  «       y |j                  | «       | j                  s| j                  «       ry t        | |«       | j                  sŒ¯y y )Nr   ©Úis_interrupted)Útools.interruptrh   ÚWATCH_INTERVAL_SECONDSr   Úsettler   Ú	interruptÚtimeÚdeadline_atÚdeadliner   r^   Ú_sleep_until_wake)rF   r.   rK   rh   Úticks        r"   r`   r`   y   s›   € Ý.à%1Ð%9Õ!¸|€DØ×ÒÙÔØ×Ñœ\×3Ñ3Ô4ØÜ�9‰9‹;˜)×/Ñ/Ò/Ø×Ñœ\×2Ñ2Ô3ØØ�‰�YÔà×Ò 	× @Ñ @Ô BØÜ˜) TÔ*ð ×Õr!   c                óv  — ddl m} t        t        j                  «       |z   | j                  «      }| j
                  sb |«       s[|t        j                  «       z
  }|dk  rn>| j                  j                  t        t        |«      «      rn| j
                  s |«       sŒ[| j                  j                  «        y)zUSleep up to one tick, leaving early on ``wake``, the deadline, or the interrupt flag.r   rg   N)
ri   rh   Úminrm   rn   r   ÚwakeÚwaitÚ_WAKE_SLICE_SECONDSÚclear)rF   rq   rh   ÚuntilÚ	remainings        r"   rp   rp   ‹   s‰   € å.ä”—	‘	“˜dÑ" I×$9Ñ$9Ó:€EØ×Ò©Ô(8ØœDŸI™I›KÑ'ˆ	Ø˜Š>ØØ�>‰>×ÑœsÔ#6¸	ÓBÔCØð ×Ò©Õ(8ð ‡N�N×ÑÕr!   )rF   r   rG   zSequence[str]ÚreturnúOptional[str])rH   zList[Target]r.   r   rM   r   rN   r{   rP   úOptional[Callback]rK   úOptional[float]rQ   Úboolrz   r   )rF   r   r.   r   rP   r|   rK   r}   rQ   r~   rz   r   )rF   r   r.   r   rK   r}   rz   ÚNone)rF   r   rq   Úfloatrz   r   )%r   Ú
__future__r   rc   rm   Údataclassesr   Útypingr   r   r   r   r	   r
   Útools.connectorsr   r7   r   Útools.connectors.operationr   r   r9   r;   r?   r@   rA   rE   rj   rv   r   ÚCallbackr   rJ   rY   rW   r`   rp   r    r!   r"   ú<module>r‡      s3  ðñ[õ #ã Û Ý !ß @× @å !Ý 2ß Bà!€Ø€Ø%Ð Ø
€Ø€Ø
€àÐ àÐ à�T˜#˜s˜(‘^Ð$ h¨s¡mÐ3Ñ4€ð ÷ð ó ðó8ð@ %)ñØðà
ðð ð	ð
  ðð ,ðð "ðð ðð 	óðF %)ñ3Ø"ð3à
ð3ð ,ð	3ð
 "ð3ð ð3ð 	ó3ó,+ô$r!   