+
    iHàj–5  ã                  óP  € R t ^ RIHt ^ RIHt ^ RIHt ^ RIt^ RIt^ RI	H
t
HtHt ^ RIHt ^ RIHt ^ RIHt ^ R	IHtHt ^ R
IHtHt ]P2                  ! R4      tR R lt] ! R R4      4       tR R ltR R lt] ! R R4      4       tR R lt ] ! R R4      4       t!R R lt"R# )añ  The provider call for the conversation turn's retry loop: ``nous_rate_limit_guard`` (skip
the attempt while another session's Nous Portal rate limit is active), ``perform_api_call``
(streaming decision, MoA prepared-request handshake, LLM execution middleware wrapper, the
redirect ``_model_request_active`` bracket and the response-vs-redirect crossing check) and
``handle_api_interrupt`` (``InterruptedError`` mid-call). Nothing here imports
``agent.conversation_loop`` at module level (cycle).
)Úannotations)Únullcontext)Ú	dataclassN)ÚAnyÚDictÚOptional)ÚFailoverReason)Ú hidden_interrupt_placeholder_row)Úappend_message)ÚREPETITION_LOOP_INTERRUPTEDÚis_runaway_repetition)Ú	site_copyÚstamp_failurezagent.conversation_loopc               ó$   € V ^8„  d   QhRRRRRR/# )é   Úagentr   Úthinking_spinnerÚreturnÚNone© )Úformats   "Ú8/home/ubuntu/.hermes/hermes-agent/agent/turn_api_call.pyÚ__annotate__r      s!   € ÷ $ñ $ ð $¸ð $Àñ $ó    c                ó‚   € V'       d   VP                  R4       V P                  '       d   V P                  R4       R# R# )z¨Stop the spinner silently and clear the thinking callback; returns ``None`` so
callers can rebind ``thinking_spinner = stop_thinking_spinner(agent, thinking_spinner)``.Ú N)ÚstopÚthinking_callback©r   r   s   &&r   Ústop_thinking_spinnerr      s4   € ÷ Ø×Ñ˜bÔ!Ø××ÐØ×Ñ Ö#ñ r   c                  óD   € ] tR t^#t$ RtR]R&   R]R&   R]R&   R]R&   RtR	# )
ÚApiCallVerdictu«   ``action``: ``"fallthrough"`` (``response`` is ready for verification) or ``"break"``
(a redirect crossed the response â€” rebuild armed on ``_retry`` or ``interrupted``).ÚstrÚactionr   Úresponser   Úinterruptedr   N©Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú__annotations__Ú__static_attributes__r   r   r   r!   r!   #   s    ‡ ñ]ð ƒKØƒMØÓØ×r   r!   c               ó    € V ^8„  d   QhRRRR/# )r   r   r   r   Úboolr   )r   s   "r   r   r   .   s   € ÷ ñ ˜#ð  $ñ r   c                óþ  € \        V RR4      '       d   R# \        V P                  ;'       g    R4      P                  4       pVP	                  R4      '       d   R# ^ RIHp ^ RIHp V! V P                  4      p\        VRR4      pVRJ d   R# V! V P                  4      '       d	   VRJd   R# V P                  4       '       g9   V P                  R	8X  d   R# ^ R
IHp \        \        V RR4      V4      '       d   R# R# )z¦Prefer streaming for liveness even without display consumers. Process providers
must opt in; ACP transports and explicit session/provider disables stay non-streaming.Ú_disable_streamingFr   )Úget_provider_profile)Ú_is_external_process_providerÚsupports_streamingNTÚmoa)ÚMockÚclient)zacp://z
acp+tcp://)Úgetattrr"   Úbase_urlÚlowerÚ
startswithÚ	providersr2   Ú$hermes_cli.runtime_provider_backendsr3   ÚproviderÚ_has_stream_consumersÚunittest.mockr6   Ú
isinstance)r   Ú_baser2   r3   ÚprofileÚ	streamingr6   s   &      r   Ú_should_streamrE   .   sÏ   € ô ˆuÐ*¨E×2Ò2ÙÜ�—‘×$Ð$ "Ó%×+Ñ+Ó-€EØ×ÑÐ0×1Ò1ÙÝ.ÝRá" 5§>¡>Ó2€GÜ˜Ð!5°tÓ<€IØ�EÓÙÙ$ U§^¡^×4Ò4¸È$Ó9NÙØ×&Ñ&×(Ò(Ø�>‰>˜UÔ"ÙÝ&Ü”g˜e X¨tÓ4°d×;Ò;ÙÙr   c               óP   € V ^8„  d   QhRRRRRRRRRRRRRRR	RR
RRRRRRRRRRR/# )r   r   r   Ú
api_kwargsÚ_original_api_kwargsÚ_llm_middleware_traceÚ_moa_prepared_requestÚ_retryr   Úretry_countÚapi_call_countÚapi_request_idÚeffective_task_idÚturn_idr%   r   r!   r   )r   s   "r   r   r   H   sœ   € ÷ Y#ñ Y#ØðY#Ø"ðY#Ø:=ðY#ØVYðY#àðY#à(+ðY#à?BðY#àQTðY#ð ðY#ð *-ðY#ð BEðY#ð PSðY#ð ð	Y#ð
 ñY#r   c               ó8  a aaa	aaaa€ RoR VVV3R llpV V3R lo\        S 4      oVVV V	V3R lp^ RIHp \        S RR4      p\        S RR4      pVf   \	        4       MTpT;_uu_ 4        Ve   VP                  4        RRR4        T! YTY«S	S P                  ;'       g    R	S P                  ;'       g    R	S P                  S P                  S P                  S P                  V\        V4      R
7      oT;_uu_ 4        Ve   VP                  4        Ve   \        S P                  4      MS P!                  4       pRRR4       X'       d7   \#        S S4      oS P%                  RR7      '       d	   RTn        MRoT! R4      # T! R4      #   + '       g   i     EL; i  + '       g   i     Lj; i  T;_uu_ 4        Te   TP                  4        Te   \        S P                  4      MS P!                  4       pRRR4       i   + '       g   i     i ; i; i)zFIssue the request (see ``_should_stream`` for the streaming decision).Nc               ó    € V ^8„  d   QhRRRR/# )r   r#   r"   r   r!   r   )r   s   "r   r   Ú&perform_api_call.<locals>.__annotate__Q   s   € ÷ 
ñ 
˜ð 
 ñ 
r   c                ó"   <€ \        V SSSR 7      # ))r#   r$   r   r%   )r!   )r#   r%   r$   r   s   &€€€r   Ú_verdictÚ"perform_api_call.<locals>._verdictQ   s   ø€ ÜØ HÐ?OØ#ô
ð 	
r   c                 ó    <€ \        S S4      oR # ©N)r   r   s   €€r   Ú_stop_spinnerÚ'perform_api_call.<locals>._stop_spinnerW   s   ø€ ä0°Ð8HÓIÒr   c                ót  <€ SP                   R 8X  d@   SP                  4       P                  V RSP                  4       SP	                  4       R7      p S'       d   SP                  V SR7      # ^ RIHp TP                  T SP                  \        SP                  ;'       g    R4      \        SP                  ;'       g    R4      \        SP                  ;'       g    R4      RSP                   RSR	\        SR
R4      '       d   RM(\        \        SR^ 4      ;'       g    ^ 4      ^ 8”  d   RMRRS/RR7      # )Úcodex_responsesF)Úallow_streamÚis_github_responsesÚsanitize_harmony_tokens)Úon_first_delta)Ú	relay_llmr   r>   Úapi_moderN   Ú	call_roleÚis_subagentÚ	delegatedÚ_fallback_indexÚfallbackÚprimaryrL   T)Ú
session_idÚnameÚ
model_nameÚmetadataÚdefer_logical_completion)rb   Ú_get_transportÚpreflight_kwargsÚ_is_copilot_urlÚ_is_codex_backendÚ!_interruptible_streaming_api_callr   ra   ÚexecuteÚ_interruptible_api_callr"   ri   r>   Úmodelr8   Úint)Únext_api_kwargsra   rY   Ú_use_streamingr   rN   rL   s   & €€€€€r   Ú_perform_api_callÚ+perform_api_call.<locals>._perform_api_call]   s1  ø€ Ø�>‰>Ð.Ô.Ø#×2Ñ2Ó4×EÑEØ¨eÈ×I^ÑI^ÓI`Ø(-×(?Ñ(?Ó(Að Fó ˆO÷ Ø×:Ñ:Ø°ð ;ó ð õ 	$à× Ñ ØØ×)Ñ)Ü˜5×+Ñ+×1Ð1¨rÓ2Ü�U—^‘^×1Ð1 zÓ2Ü˜5Ÿ;™;×,Ð,¨"Ó-à˜EŸN™NØ  .Øä˜u m°U×;Ò;ñ  ô œ7 5Ð*;¸QÓ?×DÐDÀ1ÓEÈÔIñ $à"à˜{ðð &*ð% !ó 
ð 	
r   )Úrun_llm_execution_middlewareÚ_model_request_activeÚ_pending_redirect_lockr   )Úoriginal_requestÚtask_idrP   rN   ri   Úplatformru   r>   r9   rb   rM   Úmiddleware_traceT©Úpreserve_redirectÚbreakÚfallthrough)rE   Úhermes_cli.middlewarer{   r8   r   Úsetri   r€   ru   r>   r9   rb   ÚlistÚclearr/   Ú_pending_redirectÚ_has_pending_redirectr   Úclear_interruptÚ restart_with_redirected_messages)r   rG   rH   rI   rJ   rK   r   rL   rM   rN   rO   rP   r%   rU   ry   r{   r|   Ú_redirect_lockÚ_bracketÚ_redirect_crossed_responserY   rx   r$   s   f$$$$$dd$d$$d       @@@r   Úperform_api_callr‘   H   sÈ  ÿ€ ð €H÷
ò 
öJô $ EÓ*€N÷
ñ 
õB Cô $ EÐ+BÀDÓIÐÜ˜UÐ$<¸dÓC€NØ .Ò 6Œ{Œ}¸N€Hß	ŠØ Ò,Ø!×%Ñ%Ô'÷ 
ðÙ/ØÐ<PØ%À~Ø×'Ñ'×-Ð-¨2¸¿¹×8LÐ8LÈ"ÐTY×T_ÑT_Ø—^‘^¨e¯n©nÀuÇ~Á~Ø)¼DÐAVÓ<Wô
ˆ÷ ŠXØ$Ò0Ø%×+Ñ+Ô-à1?Ò1K”�U×,Ñ,Ô-Ø×0Ñ0Ó2ð '÷ ÷ "ô 1°Ð8HÓIÐØ× Ñ °4Ð ×8Ò8Ø6:ˆFÕ3àˆKÙ˜Ó Ð Ù�MÓ"Ð"÷9 
�ˆú÷ �Xû�XŠXØ$Ò0Ø%×+Ñ+Ô-à1?Ò1K”�U×,Ñ,Ô-Ø×0Ñ0Ó2ð '÷ �X�XˆXýsH   Á,FÂ
F4 Â#F4 Â8?F4 Ä>F!ÆF	Æ!F1	Æ4HÆ?>H	Ç=	HÈHÈHc                  óD   € ] tR t^¤t$ RtR]R&   R]R&   R]R&   R]R&   RtR	# )
ÚApiInterruptVerdictz¢Always ``action == "break"`` (leave the retry loop): either a redirect restart was
armed on ``_retry`` or the turn is ``interrupted`` with ``final_response`` set.r"   r#   r   r   r%   Úfinal_responser   Nr&   r   r   r   r“   r“   ¤   s!   ‡ ñWð ƒKØÓØÓØ×r   r“   c               ó<   € V ^8„  d   QhRRRRRRRRRRRRRRR	RR
R/	# )r   r   r   rK   r   ÚmessagesÚconversation_historyÚapi_start_timer%   r”   r   r“   r   )r   s   "r   r   r   ¯   sa   € ÷ (Wñ (WØð(WØð(WØ25ð(WØADð(WØ\_ð(Wàð(Wà&)ð(Wà;>ð(Wð ñ(Wr   c          
     ó   € ^ RI Hp \        W4      pV P                  4       '       d-   V P	                  RR7      '       d   RVn        \        RW&V4      # \        P                  ! 4       V,
          p	V P                  V P                   R2RR7       RpV P                  V4       T P                  \        V RR4      ;'       g    R4      P                  4       p
V
'       d-   \        V
4      '       d   \        V\!        4       4       \"        pM'V
'       d   \        VR	R
RV
RRR//4       T
pMV V	R R2pV P%                  W44       \        RW&V4      # )zÜ``InterruptedError`` during the provider call: a pending redirect keeps its correction
queued for the outer-loop rebuild; otherwise keep any streamed partial text so the next
turn has a record of the half-finished reply.)Ú"INTERRUPT_WAITING_FOR_MODEL_PREFIXTr‚   r„   u    âš¡ Interrupted during API call.)ÚforceÚ _current_streamed_assistant_textr   ÚroleÚ	assistantÚcontentÚdisplay_metadatar%   z.1fzs elapsed).)Úagent.conversation_looprš   r   r‹   rŒ   r�   r“   ÚtimeÚ_vprintÚ
log_prefixÚ)_drop_trailing_empty_response_scaffoldingÚ_strip_think_blocksr8   Ústripr   r
   r	   r   Ú_persist_session)r   rK   r   r–   r—   r˜   r%   r”   rš   Úapi_elapsedÚ_partials   &$$$$$$$   r   Úhandle_api_interruptr«   ¯   sH  € õ Kä,¨UÓEÐð ×"Ñ"×$Ò$¨×)>Ñ)>ÐQUÐ)>×)VÒ)VØ26ˆÔ/Ü" 7Ð,<È>ÓZÐZÜ—)’)“+ Õ.€KØ	‡M�M�U×%Ñ%Ð&Ð&FÐGÈt€MÔTØ€Kð 
×3Ñ3°HÔ=Ø×(Ñ(Ü�Ð9¸2Ó>×DÐDÀ"óç�eƒgð ÷ Ô)¨(×3Ò3ô 	�xÔ!AÓ!CÔDÜ4‰ß	Ü�xØ�K ¨HÐ6HÈ=ÐZ^ÐJ_ð"
ô 	ð "‰à>Ð?ÀÈCÐ?PÐP[Ð\ˆØ	×Ñ˜8Ô:Ü˜wÐ(8À~ÓVÐVr   c                  óR   € ] tR t^Út$ RtR]R&   R]R&   R]R&   R]R&   RtR	]R
&   RtR# )ÚNousRateGuardVerdictu¢   ``action``: ``"fallthrough"`` (no active limit â€” make the call), ``"break"``
(fallback armed on ``_retry``) or ``"return"`` (``result``: no fallback available).r"   r#   r   Úactive_system_promptrL   Úcompression_attemptsNúOptional[dict[str, Any]]Úresultr   )r'   r(   r)   r*   r+   r,   r±   r-   r   r   r   r­   r­   Ú   s,   ‡ ñ[ð ƒKØÓØÓØÓØ'+€FÐ$×+r   r­   c               ó@   € V ^8„  d   QhRRRRRRRRRRRRRRR	RR
RRR/
# )r   r   r   rK   Úapi_messagesr–   r—   r®   rL   r¯   rM   r   r­   r   )r   s   "r   r   r   æ   s^   € ÷ ?#ñ ?#Øð?#Øð?#Ø.1ð?#Ø=@ð?#ØX[ð?#àð?#à,/ð?#àGJð?#à\_ð?#ð ñ?#r   c               óÀ  aaa€ ^ RI Hp	 RR VVV3R lllp
V P                  R8X  Ed�    ^ RIHp V! V 4        ^ RIHpHp ^ RI	H
p VP                  V 4      pV! VR7      pVEeP   V^ 8”  EdH   V! V4      pV'       d-   VP                  P                  VP                  V4      R	7      pMR
V R2pV P                  RV R24       V P!                  RV 24       V P#                  4       '       d   V	! WSV4      o^ o^ oV
! R4      # V P%                  4        V P'                  W44       T
! R\)        RV'       d   RV 2MRV R\+        R4       2RVRVRRRRRV/V'       d2   RRRRVP,                  P                  VP                  V4      R	7      //M/ C\.        P0                  P2                  R4      4      # V
! R4      #   \
         d     EL“i ; i  \
         d     L'i ; i) z«Skip the call if another session recorded a Nous Portal rate limit: every attempt (incl.
SDK retries) counts against RPH. Never lets the guard itself break the agent loop.)Ú_arm_fallback_restartc               ó$   € V ^8„  d   QhRRRRRR/# )r   r#   r"   r±   r°   r   r­   r   )r   s   "r   r   Ú+nous_rate_limit_guard.<locals>.__annotate__î   s#   € ÷ 
ñ 
˜ð 
Ð&>ð 
ÐJ^ñ 
r   c                ó$   <€ \        V SSSVR 7      # ))r#   r®   rL   r¯   r±   )r­   )r#   r±   r®   r¯   rL   s   &&€€€r   rU   Ú'nous_rate_limit_guard.<locals>._verdictî   s   ø€ Ü#ØÐ0DÐR]Ø!5¸fô
ð 	
r   Únous)Úapply_model_switch)Únous_rate_limit_remainingÚformat_remaining)Ú	anon_auth)Ú	anonymous)Úresetz7Your Nous account has hit its rate limit; it resets in Ú.u   â�³ z Trying fallback...r„   r   r”   z

Únous_rate_limitr–   Ú	api_callsÚ	completedFÚfailedTÚerrorÚ	free_tierÚkindÚrate_limitedÚmessager…   rX   )r¡   rµ   r>   Úhermes_cli.anon_authr»   Ú	ExceptionÚagent.nous_rate_guardr¼   r½   Ú
hermes_clir¾   Úis_anonymous_agentÚFREE_TIER_RATE_LIMIT_CHATr   Úfriendly_waitÚ_buffer_vprintÚ_buffer_diagnostic_statusÚ_try_activate_fallbackÚ_flush_status_bufferr¨   r   r   ÚFREE_TIER_RATE_LIMIT_CARDr   Ú
rate_limitÚvalue)r   rK   r³   r–   r—   r®   rL   r¯   rM   rµ   rU   r»   r¼   Ú_fmt_nous_remainingr¾   Ú
_anonymousÚ_nous_remainingrÀ   Ú	_nous_msgs   &$$$$ddd$          r   Únous_rate_limit_guardrÝ   æ   s&  ú€ õ >÷
ô 
ð ‡~�~˜Õð	Ý?Ù˜uÔ%ð(	÷õ -Ø"×5Ñ5°eÓ<ˆJÙ7À*ÔMˆOØÓ*¨ÀÕ/BÙ+¨OÓ<�ßØ )× CÑ C× JÑ JØ'×5Ñ5°oÓFð !Kó !H‘Ið #ZÐZ_ÐY`Ð`aÐ b�IØ×$Ñ$ t¨I¨;Ð6IÐ%JÔKØ×/Ñ/°$°y°kÐ0BÔCØ×/Ñ/×1Ò1Ù+@ØÐ-AÀ6ó,KÐ(à"#�KØ+,Ð(Ù# GÓ,Ð,à×*Ñ*Ô,Ø×&Ñ& xÔFñ   ¬-Ø$¿Z¨¨i¨[Ñ'9Ø.2°9°+¸TÄ)ÐL]ÓB^ÐA_Ð,`Ø Ø Ø Ø˜dØ˜Yð9÷ NXð $ f¨n¸iÈ×IlÑIl×IsÑIsØ'×5Ñ5°oÓFð Jtó JHð &Iñ JØ]_ð9ô "×,Ñ,×2Ñ2°Dó+:ó ;ð ;ñ �MÓ"Ð"øôW ô 	Úð	ûôR ô 	Ùð	ús>   ªF= ¹AG Á>A0G Ã/G ÄAG ÅAG Æ=GÇGÇGÇG)#r+   Ú
__future__r   Ú
contextlibr   Údataclassesr   Úloggingr¢   Útypingr   r   r   Úagent.error_classifierr   Ú(agent.agent_runtime_helpers_placeholdersr	   Úagent.message_metadatar
   Úagent.repetition_guardr   r   Úagent.turn_failure_copyr   r   Ú	getLoggerÚloggerr   r!   rE   r‘   r“   r«   r­   rÝ   r   r   r   Ú<module>rê      s©   ðñõ #å "Ý !Û Û ß &Ñ &å 1Ý UÝ 1ß Uß <à	×	Ò	Ð4Ó	5€õ$ð ÷ð ó ðõõ4Y#ðx ÷ð ó ðõ(WðV ÷,ð ,ó ð,÷?#r   