+
    jHàjI   ã                  ó  a € 0 t $ R t^ RIHt ^ RIt^ RIt^ RIt^ RIHtH	t	 ^ RI
Ht ]P                  ! R4      tRt/ tR]R&   R	tR
 R lt. R!OtRRR.R3RRR.R3RR]R3RR]R3.tR R ltR"R R lltR R ltR R  ltR# )#a  Kimi Code and Z.AI endpoint auto-detection, LM Studio base-URL normalization.

Re-exported from ``hermes_cli/auth.py`` (patch targets unchanged); origin helpers are imported
lazily per function so ``hermes_cli.auth.<helper>`` patches still intercept and no cycle forms.
)ÚannotationsN)ÚDictÚOptional)Úhttpxzhermes_cli.authi,  zdict[str, float]Ú_zai_probe_failed_untilzhttps://api.kimi.com/codingc               ó(   € V ^8„  d   QhRRRRRRRR/# ©é   Úapi_keyÚstrÚdefault_urlÚenv_overrideÚreturn© )Úformats   "Ú=/home/ubuntu/.hermes/hermes-agent/hermes_cli/auth_zai_kimi.pyÚ__annotate__r      s)   € ÷ ñ  Cð °cð Èð ÐQTñ ó    c                ód   € V'       d   V# V '       d   V P                  R4      '       d   \        # V# )zIKimi base URL from the key prefix; an explicit KIMI_BASE_URL always wins.zsk-kimi-)Ú
startswithÚKIMI_CODE_BASE_URL)r
   r   r   s   &&&r   Ú_resolve_kimi_base_urlr      s*   € çØÐß�7×%Ñ% j×1Ò1Ü!Ð!ØÐr   Úglobalzhttps://api.z.ai/api/paas/v4zglm-5ÚGlobalÚcnz$https://open.bigmodel.cn/api/paas/v4ÚChinazcoding-globalz#https://api.z.ai/api/coding/paas/v4zGlobal (Coding Plan)z	coding-cnz+https://open.bigmodel.cn/api/coding/paas/v4zChina (Coding Plan)c               ó(   € V ^8„  d   QhRRRRRRRR/# )	r	   r
   r   ÚendpointÚtupleÚtimeoutÚfloatr   úOptional[dict[str, str]]r   )r   s   "r   r   r   1   s)   € ÷ ñ ¨ð °uð Àuð ÐQiñ r   c                ó�  € Vw  r4rVV FŠ  p \         P                  ! V R2RRV  2RR/RVRRR	^R
RRRR/./VR7      pVP                  ^È8X  d$   \        P	                  RW4V4       RVRVRVRV/u # \        P	                  RW7VP                  4       KŒ  	  R#   \
         d#   p	\        P	                  RY7T	4        Rp	?	K¸  Rp	?	ii ; i)zWProbe one Z.AI endpoint, trying its candidate models in order; None when none succeeds.z/chat/completionsÚAuthorizationzBearer zContent-Typezapplication/jsonÚmodelÚstreamFÚ
max_tokensÚmessagesÚroleÚuserÚcontentÚping)ÚheadersÚjsonr   z(Z.AI endpoint probe: %s (%s) model=%s OKÚidÚbase_urlÚlabelz,Z.AI endpoint probe: %s model=%s returned %sz+Z.AI endpoint probe: %s model=%s failed: %sN)r   ÚpostÚstatus_codeÚloggerÚdebugÚ	Exception)
r
   r   r   Úep_idr/   Úprobe_modelsr0   r$   ÚrespÚexcs
   &&&       r   Ú_probe_single_zai_endpointr:   1   sð   € à+3Ñ(€E�\Ûˆð	[Ü—:’:Ø�*Ð-Ð.Ø(¨G°G°9Ð*=¸~ÐOaÐbØ˜u h°°|ÀQÈ
ÐV\Ð^dÐfoÐqwÐUxÐTyÐzØô	ˆDð ×Ñ 3Ô&Ü—‘ÐGÈÐZ_Ô`Ø˜e Z°¸7ÀEÈ7ÐTYÐZÒZÜ�L‰LÐGÈÐW[×WgÑWgÖhñ ñ øô ô 	[Ü�L‰LÐFÈÐVY×ZÒZûð	[ús   ŒA"BÁ1!BÂCÂ#C Ã Cc               ó$   € V ^8„  d   QhRRRRRR/# )r	   r
   r   r   r    r   r!   r   )r   s   "r   r   r   E   s"   € ÷  "ñ  " ð  "¨uð  "Ð?Wñ  "r   c           	     óF  aa€ ^ RI HpHp V! \        \        4      R7      p \         Uu/ uF!  qTP                  \        WV4      V^ ,          bK#  	  ppVP                  4        UUu/ uF  w  rxW‡bK	  	  uppo/ oR VV3R llp	V! V4       FF  p
 V
P                  4       pVe   VSWj,          &   T	! RR7      pTf   K2  Tu TP                  RR7       # 	  V	! RR7      VP                  RR7       # u upi u uppi   \         d     LZi ; i  TP                  RR7       i ; i)	z]Probe z.ai endpoints in parallel; first working one in ZAI_ENDPOINTS priority order, or None.)ÚThreadPoolExecutorÚas_completed)Úmax_workersc               ó    € V ^8„  d   QhRRRR/# )r	   Úrequire_doneÚboolr   r!   r   )r   s   "r   r   Ú)detect_zai_endpoint.<locals>.__annotate__O   s   € ÷ 	ñ 	 tð 	Ð0Hñ 	r   c                óÀ   <€ \          FR  pV '       d(   SV^ ,          ,          P                  4       '       g    R# V^ ,          S9   g   KB  SV^ ,          ,          u # 	  R# )é    N)ÚZAI_ENDPOINTSÚdone)rA   ÚepÚby_idÚresultss   & €€r   Ú_first_readyÚ)detect_zai_endpoint.<locals>._first_readyO   sJ   ø€ ÷ $�ß¨¨b°­e­×(9Ñ(9×(;Ò(;ÚØ�a•5˜GÖ#Ø" 2 a¥5�>Ò)ñ	 $ñ
 r   T)rA   F)Úwait)Úconcurrent.futuresr=   r>   ÚlenrF   Úsubmitr:   ÚitemsÚresultr5   Úshutdown)r
   r   r=   r>   ÚpoolrH   ÚfuturesÚfr6   rK   ÚfuturerR   ÚwinnerrI   rJ   s   &&           @@r   Údetect_zai_endpointrY   E   s  ù€ çCá¬#¬mÓ*<Ô=€Dð"ÝboÓpÑboÐ\^—;‘;Ô9¸7ÈÓPÐRTÐUVÕRWÒWÑboˆÐpØ*1¯-©-¬/Ô:©/™h˜a�’©/Ò:ˆØ-/ˆ÷	ñ 	ñ # 7Ö+ˆFðØŸ™›�ØÒ%Ø/5�G˜G�OÑ,ñ "¨tÔ4ˆFØÔ!Ø‘ð 	�‰˜5ˆÕ!ñ ,ñ ¨Ô/à�‰˜5ˆÕ!ùò5 qùÛ:øô$ ô Ùðûð 	�‰˜5ˆÕ!úsX   ¢D ª'C0ÁD Á&C5Á3D ÂC;Â.
D Â=D Ã
D Ã0D Ã;D	ÄD ÄD	Ä	D ÄD c               ó(   € V ^8„  d   QhRRRRRRRR/# r   r   )r   s   "r   r   r   h   s)   € ÷ 0 ñ 0  3ð 0 °Sð 0 Èð 0 ÐPSñ 0 r   c                ó  € ^ RI HpHpHpHpHpHp V'       d   V# V '       g   V# \        P                  ! V P                  4       4      P                  4       R,          p	V! V! 4       R4      ;'       g    / p
V
P                  R4      p\        V\        4      '       dU   VP                  R4      '       d>   VP                  RR4      V	8X  d'   \        P                  RVR,          4       VR,          # \         P                  V	R	4      \"        P"                  ! 4       8”  d   V# V! V 4      pV'       d   VP                  R4      '       g@   \        P                  R
V4       \"        P"                  ! 4       \$        ,           \         V	&   V# RVR,          RVP                  RR4      RVP                  RR4      RVP                  RR4      RV	/p V! 4       ;_uu_ 4        V! 4       pV! VR4      ;'       g    / pWßR&   V! VRVRR7       V! V4       RRR4       \        P+                  RTR,          TR,          4       TR,          #   + '       g   i     L>; i  \&         d!   p\        P)                  RT4        Rp?LgRp?ii ; i)zÍZ.AI base URL by probing endpoints; an explicit GLM_BASE_URL always wins.

The detected endpoint is cached in provider state (auth.json) keyed on a hash of the API key so
subsequent starts skip the probe.
)Ú_auth_store_lockÚ_load_auth_storeÚ_load_provider_stateÚ_save_auth_storeÚ_store_provider_staterY   :Né   NÚzaiÚdetected_endpointr/   Úkey_hashÚ zZ.AI: using cached endpoint %sg        z.Z.AI: probe failed, falling back to default %sÚendpoint_idr.   r$   r0   F)Ú
set_activeNzHZ.AI: could not persist detected endpoint (%s); will re-probe next startz$Z.AI: auto-detected endpoint %s (%s))Úhermes_cli.authr\   r]   r^   r_   r`   rY   ÚhashlibÚsha256ÚencodeÚ	hexdigestÚgetÚ
isinstanceÚdictr3   r4   r   ÚtimeÚ_ZAI_PROBE_FAILURE_TTL_SECONDSr5   ÚwarningÚinfo)r
   r   r   r\   r]   r^   r_   r`   rY   rd   ÚstateÚcachedÚdetectedrc   Ú
auth_storeÚstate_under_lockr9   s   &&&              r   Ú_resolve_zai_base_urlry   h   s  € ÷ W÷  WßØÐçØÐä�~Š~˜gŸn™nÓ.Ó/×9Ñ9Ó;¸CÕ@€HÙ Ñ!1Ó!3°UÓ;×AÐA¸r€EØ�Y‰YÐ*Ó+€FÜ�&œ$×Ò F§J¡J¨z×$:Ò$:¸v¿z¹zÈ*ÐVXÓ?YÐ]eÔ?eÜ�‰Ð5°v¸jÕ7IÔJØ�jÕ!Ð!ô ×"Ñ" 8¨SÓ1´D·I²I³KÔ?ØÐñ # 7Ó+€Hß˜Ÿ™ j×1Ò1Ü�‰ÐEÀ{ÔSÜ,0¯IªI«KÔ:XÕ,XÔ Ñ)ØÐð 	�H˜ZÕ(¨-¸¿¹ÀdÈBÓ9OØ�—‘˜g rÓ*¨G°X·\±\À'È2Ó5NØ�HðÐð	hÙ×ÕÙ)Ó+ˆJÙ3°JÀÓF×LÐLÈ"ÐØ4EÐ0Ñ1á! *¨eÐ5EÐRWÕXÙ˜ZÔ(÷  ô ‡K�KÐ6¸ÀÕ8IÈ8ÐT^ÕK_Ô`Ø�JÕÐ÷  ×ûô ô hÜ�‰ÐaÐcf×gÑgûðhús6   ÇI Ç3IÈI ÉI	ÉI ÉI ÉJÉ!I<É<Jc               ó    € V ^8„  d   QhRRRR/# )r	   r/   r   r   r   )r   s   "r   r   r   ›   s   € ÷ 5ñ 5°3ð 5¸3ñ 5r   c                ó  € \        T ;'       g    R4      P                  4       P                  R4      pR F:  pVP                  V4      '       g   K  VR\	        V4      )  P                  R4      p M	  T;'       g    RR,           # )a  Return the OpenAI-compatible LM Studio runtime base URL.

LM Studio's native management API lives under ``/api/v1`` while its OpenAI-compatible chat
endpoint lives under ``/v1``; users paste either form, so normalize before the SDK appends
``/chat/completions``.
re   Ú/ú/v1Nzhttp://127.0.0.1:1234)z/api/v1z/apir}   )r   ÚstripÚrstripÚendswithrO   )r/   ÚrootÚsuffixs   &  r   Ú$_normalize_lmstudio_runtime_base_urlrƒ   ›   sw   € ô ˆx�~ˆ~˜2Ó×$Ñ$Ó&×-Ñ-¨cÓ2€DÛ,ˆØ�=‰=˜× Ô Ø˜œ3˜v›;˜,Ð'×.Ñ.¨sÓ3ˆDÙñ -ð ×+Ð+Ð+¨uÕ4Ð4r   )zglm-5.3zglm-5.3-flashzglm-5.2zglm-5.1zglm-5v-turbozglm-4.7)g       @)Ú__conditional_annotations__Ú__doc__Ú
__future__r   Úloggingri   rp   Útypingr   r   Úhermes_cli.auth_constantsr   Ú	getLoggerr3   rq   r   Ú__annotations__r   r   Ú_ZAI_CODING_PROBE_MODELSrF   r:   rY   ry   rƒ   )r„   s   @r   Ú<module>r�      sµ   øðôõ #ã Û Û ß !Ý +à	×	Ò	Ð,Ó	-€ð "%Ð Ø,.Ð Ð)Ó .ð 3Ð õò iÐ ð Ð4¸g¸YÈ(ÐSØ	Ð<¸w¸iÈ7ÐSØÐ;Ð>VÐXnÐoØÐCÐE]Ð_tÐuð€õ÷( "õF0 ÷f5r   