+
    iHàjo  ã                  ó¾   € R t ^ RIHt ^ RIt^ RIt^ RIt^ RIt^ RI	H
t
 ^ RIHtHt ^ RIHt ]P                   ! ]4      tR R ltR R	 ltR
 R ltR R ltR R ltR# )a  Chat completions against llama-server (sibling of ``chat_completion_helpers``).

Which requests go to llama-server, the turn-start health check that lets a recovered server try
again past the stale breaker, and the live load / prompt-reading notice shown before the first
token.
)ÚannotationsN)ÚOptional)ÚurlparseÚurlsplit©Úenv_intc               ó   € V ^8„  d   QhRR/# ©é   ÚreturnÚbool© )Úformats   "Ú@/home/ubuntu/.hermes/hermes-agent/agent/chat_completion_local.pyÚ__annotate__r      s   € ÷ 	uñ 	u ñ 	uó    c           
     óÀ   € \        V RR4      R9  d   R# ^ RIHp T! \        \        V RR4      ;'       g    R4      \        \        V RR4      ;'       g    R4      4      # )aX  True when this agent's chat requests go to llama-server: the managed server, or a
``llamacpp`` provider endpoint. These requests always stream and ask for prompt progress
(unless the user turned streaming off in config). A non-streamed request can't be cancelled
through the router, so it would keep the server busy after Hermes gives up on it.Úapi_modeÚ F)Úis_managed_providerÚproviderÚbase_url)r   Úchat_completions)ÚgetattrÚ%hermes_cli.local_runtime.capabilitiesr   Ústr)Úagentr   s   & r   Ú_streams_llamacppr      sY   € ô
 ˆu�j "Ó%Ð-EÔEÙÝIáœs¤7¨5°*¸bÓ#A×#GÐ#GÀRÓHÌ#ÌgÐV[Ð]gÐikÓNl×NrÐNrÐprÓJsÓtÐtr   c               ó   € V ^8„  d   QhRR/# )r
   r   Úintr   )r   s   "r   r   r   "   s   € ÷ 4ñ 4�cñ 4r   c                 ó   € \        R^4      # )zSConsecutive stale attempts after which calls fail at once; 0 turns the breaker off.ÚHERMES_STREAM_STALE_GIVEUPr   r   r   r   Ústale_giveupr"   "   s   € äÐ/°Ó3Ð3r   c               ó   € V ^8„  d   QhRR/# r	   r   )r   s   "r   r   r   '   s   € ÷ ñ  Dñ r   c           	     ó¢  € \        \        \        V RR4      ;'       g    R4      4      pVP                  '       d   VP                  '       g   R# VP                  R8X  d   VP
                  P                  RR^4      MVP
                  p\        P                  P                  \        P                  P                  / 4      4      p VP                  VP                   RV R2^R7      ;_uu_ 4       pVP                  ^È8H  uuR	R	R	4       #   + '       g   i     R	# ; i  \        \        \        P                   P"                  3 d     R# i ; i)
zSTrue when the llama-server behind *agent* answers ``/health`` within a few seconds.r   r   FÚ	localhostz	127.0.0.1z://z/health)ÚtimeoutN)r   r   r   ÚschemeÚhostnameÚnetlocÚreplaceÚurllibÚrequestÚbuild_openerÚProxyHandlerÚopenÚstatusÚOSErrorÚ
ValueErrorÚhttpÚclientÚHTTPException)r   Úpartsr)   ÚopenerÚresponses   &    r   Ú_local_server_answersr9   '   sï   € ä”Sœ ¨
°BÓ7×=Ð=¸2Ó>Ó?€EØ�<�<ˆ<˜uŸ~Ÿ~˜~ÙàBGÇ.Á.ÐT_ÔB_ˆU�\‰\×!Ñ! +¨{¸AÔ>Ðej×eqÑeq€FÜ�^‰^×(Ñ(¬¯©×)DÑ)DÀRÓ)HÓI€FðØ�[‰[˜EŸL™L˜>¨¨V¨H°GÐ<Àaˆ[×HÔHÈHØ—?‘? cÑ)÷ I×H×HÓHûä”Z¤§¡×!:Ñ!:Ð;ô Úðús0   Ã+D  Ã2DÄ
D  ÄD	ÄD  ÄD  Ä *EÅEc               ó   € V ^8„  d   QhRR/# r	   r   )r   s   "r   r   r   6   s   € ÷ ñ ¨4ñ r   c                óÞ   € ^ RI Hp \        4       V! V 4      r2V^ 8:  g)   W28  g#   \        V 4      '       d   \	        V 4      '       g   R# \
        P                  RV4       V^,
          V n        R# )a»  Turn start: when the stale breaker is open, the agent talks to llama-server, and the
server answers its health check, let one attempt through. Within a turn the breaker stops
endless retries; across turns, a local server that crashed or fell behind and has since
recovered gets the user's next message instead of an instant error. The router answers
``/health`` even while its model is stuck, so one more stale attempt shuts the breaker again.)Ú_stale_streakFzVLocal model server answers its health check after %d stale attempts; trying once more.T)Úagent.chat_completion_helpersr<   r"   r   r9   ÚloggerÚinfoÚ_consecutive_stale_streams)r   r<   ÚgiveupÚstreaks   &   r   Úretry_recovered_local_serverrC   6   sY   € õ <ä!“^¡]°5Ó%9ˆFØ�„{�f”oÔ->¸u×-EÒ-EÔMbÐch×MiÒMiÙÜ
‡K�KÐhÐjpÔqØ'-°¥z€EÔ$Ùr   c               ó    € V ^8„  d   QhRRRR/# )r
   Ú
api_kwargsÚdictr   zOptional[str]r   )r   s   "r   r   r   F   s   € ÷  ñ  °$ð  ¸=ñ  r   c                ó^  €  \        \        V RR4      ;'       g    R4      pV'       g   R# ^ RIHp ^ RIHpHp ^ RIHp \        P                  ! V! 4       P                  RR7      4      p\        \        VP                  RR4      4      4      P                  P                  4       pV'       d)   \        V4      P                  P                  4       V8w  d   R# \        VP                  R	R4      4      p	V! 4       P                  V	4      p
V
e   R
V	 RV
R,           R2# V! V	4      pVf   R# \!        VR,          4      pV! V4      pV'       d7   WÜ8¼  d1   R\#        ^ \%        ^d\'        WÍ,          ^d,          4      4      4       R2# R#   \(         d    \*        P-                  RRR7        R# i ; i)uå   Live phase notice ("â�³ loading <model> into memory â€” N%" / "âš™ processing
prompt â€” P%") while the managed local server works before the first token;
None when neither applies. Otherwise a cold load reads as a generic stall.r   r   N)Úestimate_request_context_tokens)Úget_loading_progressÚget_prefill_progress)Ú
state_pathz	utf-8-sig)ÚencodingÚmodelu   â�³ loading u    into memory â€” Úpercentz,% (responses start once the model is loaded)Ú	processedu   âš™ processing prompt â€” Ú%u   âš™ processing promptzlocal load notice unavailableT)Úexc_info)r   r   r=   rH   Ú&hermes_cli.local_runtime.load_progressrI   rJ   Ú#hermes_cli.local_runtime.supervisorrK   ÚjsonÚloadsÚ	read_textr   Úgetr)   Úlowerr   ÚmaxÚminÚroundÚ	Exceptionr>   Údebug)r   rE   ÚbaserH   rI   rJ   rK   ÚstateÚmanagedrM   ÚprogressÚprefillrO   Útotals   &&            r   Ú_managed_local_load_noticerd   F   sp  € ðÜ”7˜5 *¨bÓ1×7Ð7°RÓ8ˆßÙÝQßeÝBÜ—
’
™:›<×1Ñ1¸;Ð1ÓGÓHˆÜœ3˜uŸy™y¨°RÓ8Ó9Ó:×AÑA×GÑGÓIˆßœ( 4›.×/Ñ/×5Ñ5Ó7¸7ÔBÙÜ�J—N‘N 7¨BÓ/Ó0ˆÙ'Ó)×-Ñ-¨eÓ4ˆØÒØ" 5 'Ð):¸8ÀIÕ;NÐ:Oð P=ð =ð >á& uÓ-ˆØŠ?ÙÜ˜ Õ,Ó-ˆ	Ù/°
Ó;ˆß�UÔ'Ø/´°A´s¸3ÄÀiÕFWÐZ]ÕF]Ó@^Ó7_Ó0`Ð/aÐabÐcÐcá&øÜô ä�‰Ð4¸tˆÔDÚðús0   ‚F ›F ¬B+F ÃAF ÄF Ä,AF Æ"F,Æ+F,)Ú__doc__Ú
__future__r   Úhttp.clientr3   rT   ÚloggingÚurllib.requestr+   Útypingr   Úurllib.parser   r   Úutilsr   Ú	getLoggerÚ__name__r>   r   r"   r9   rC   rd   r   r   r   Ú<module>ro      sN   ðñõ #ã Û Û Û Ý ß +å à	×	Ò	˜8Ó	$€õ	uõ4õ
õ÷  r   