+
    iHàjÃ  ã                   ó‚  a€ R™ tx0 t R t^ RIHt ^ RI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Ht ^ RIHt ^ RIHtHtHt ^ RIHtHtHtHtHtHtHtHtH t H!t! ^ RI"H#t#H$t$H%t% ^ R	I&H't' ^ R
I(H)t) Rs*] ^ k Rs+] ^k R t,RšR R llt-]P\                  ! ]/4      t0RRRRRRRR/t1RRRRRRRRRRRRRR/t2R›t3Rœt4R�t5Ržt6R  R! lt7R" R# lt8/ RR$bR%R$bR&R$bR'R$bRR$bRR(bRR(bRR(bRR(bR)RbR*R(bR+R$bR,R-bR.R-bR/R0bR1R0bR2R0bR3R4R5R6/Ct9R$t:R7 R8 lt;R9 R: lt<RŸR; R< llt=R= R> lt>R? R@ lt?RA RB lt@RC RD ltARE RF ltBRG RH ltCRItDRJ]D.tERKtFRLtGRMRN.tHROtIRsJ] ^k R tKR¡tLRP RQ ltMRR RS ltNRT RU ltORV RW ltPRXtQRYtRRZR[R\R]/tS]SP©                  4        U Uu/ uF  w  rWbK	  	  upp tU]V! RZ04      tW]X;QJ d    . R^ ]Y! ]W4       4       F  NK  	  5M! R^ ]Y! ]W4       4       4      tZR_ R` lt[RaRb/Rc Rd llt\Re Rf lt]Rg Rh lt^Ri t_Rj Rk lt`R¢RaRb/Rl Rm llltaRŸRn Ro lltbRp Rq ltcRr Rs ltdR¢RaRb/Rt Ru lllteRv Rw ltfRx Ry ltgRz R{ lthR£ti]PÔ                  ! R|4      tkR} tlR~ R ltmRR€R�/R�R€R�/R‚R€Rƒ//tnR¤R„ R… llto]V! 0 R¥m4      tpR†R‡/Rˆ R‰ lltqRŠ R‹ ltr ! RŒ R�4      tsRŽ R� lttR� R‘ ltuR’ tvR†R‡R“R”R•RR–R/R— R˜ lltwR# u upp i )¦a¶  Anthropic Messages API adapter: client construction + the Messages call for Hermes's
OpenAI-style internals. Auth: Console API keys (``sk-ant-api*``/``-usr*``/``-svc*``/``-admin*``) ->
x-api-key; OAuth setup-tokens (``sk-ant-oat*``) and Claude Code credentials -> Bearer + beta header.
Endpoint predicates, payload conversion and credentials live in ``agent/anthropic_{endpoints,
message_convert,credentials}.py``; import them from there.)Úinstall_hintN)Úsuppress)ÚAnyÚDictÚListÚOptional)Únormalize_proxy_env_vars)Ú_is_oauth_tokenÚanthropic_route_is_oauthÚis_native_anthropic_host)
Ú_base_url_needs_context_1m_betaÚ_is_azure_anthropic_endpointÚ_is_kimi_coding_endpointÚ_is_minimax_anthropic_endpointÚ_is_nous_portal_endpointÚ_is_opencode_endpointÚ"_is_third_party_anthropic_endpointÚ_model_name_is_kimi_familyÚ_normalize_base_url_textÚ_requires_bearer_auth)Úconvert_messages_to_anthropicÚconvert_tools_to_anthropicÚnormalize_model_name)ÚEmptyStreamError)Úget_version_info.c                 ó¾   € \         RJ d    ^ RIHp  V ! R4        ^ RIpVs \         # \         #   \         d   pTs Rp?L%Rp?ii ; i  \         d    Rs  \         # i ; i)zMReturn the ``anthropic`` SDK module, importing lazily. None if not installed..)Úensure_importÚ	anthropicN)Ú_anthropic_sdkÚpmr   Ú	ExceptionÚ_anthropic_install_errorr   ÚImportError)r   ÚexcÚ_sdks      Ú</home/ubuntu/.hermes/hermes-agent/agent/anthropic_adapter.pyÚ_get_anthropic_sdkr&   *   si   € ô ˜Óð	+Ý(Ù˜+Ô&ð	"Û$Ø!ˆNô ÐŒ>Ðøô ô 	+Ø'*Õ$ûð	+ûô
 ô 	"Ø!‰NÜÐð	"ús#   Œ- ›A ­A¸?¿AÁAÁAc                ó0   € V ^8„  d   QhR\         R\         /# )é   ÚpurposeÚverb©Ústr)Úformats   "r%   Ú__annotate__r.   ;   s   € ÷ 	ñ 	œ#ð 	¤Sñ 	ó    c           
     ó¨   € \        4       pVf#   \        e   \        RV  R\         24      \        hVf   \        RV  RV R\        R4       24      hV# )zI``_get_anthropic_sdk()`` or ImportError naming the feature that needs it.z(The 'anthropic' package is required for z: z. r   )r&   r!   r"   r   )r)   r*   Úsdks   && r%   Ú_require_sdkr2   ;   sr   € ä
Ó
€CØ
‚{Ô/Ò;ÜÐDÀWÀIÈRÜ5Ð6ð8ó 9Ü>Vð	Wà
‚{ÜÐDÀWÀIÈRÐPTÈvÐUWÜ)¨+Ó6Ð7ð9ó :ð 	:à€Jr/   Úxhighi }  Úhighi€>  Úmediumé@  Úlowi   ÚultraÚmaxÚminimalúclaude-opus-4-5úclaude-sonnet-4-5úclaude-haiku-4-5úclaude-opus-4-6úclaude-sonnet-4-6úclaude-fablec                ó>   € V ^8„  d   QhR\         R,          R\        /# )r(   ÚmodelNÚreturn©r,   Úbool)r-   s   "r%   r.   r.   m   s   € ÷ -ñ -œC $�Jð -¬4ñ -r/   c                 ó<   € R T ;'       g    RP                  4       9   # )ÚclaudeÚ )Úlower©rB   s   &r%   Ú_is_claude_modelrK   m   s   € Ø˜Ÿ˜ ×*Ñ*Ó,Ñ,Ð,r/   c                ó0   € V ^8„  d   QhR\         R\        /# ©r(   rB   rC   rD   )r-   s   "r%   r.   r.   q   s   € ÷ +ñ +œ#ð +¬dñ +r/   c                ó˜   a€ V P                  4       o\        ;QJ d    V3R lV 4       F  '       g   K   R# 	  R# ! V3R lV 4       4      # )zDCase-insensitive substring match of ``model`` against a family list.c              3   ó,   <"  € T F	  qS9   x € K  	  R # 5i©N© )Ú.0ÚvÚms   & €r%   Ú	<genexpr>Ú!_model_matches.<locals>.<genexpr>t   s   øé € Ð*™z˜!�AŽv›zùs   ƒTF)rI   Úany)rB   Ú
substringsrT   s   &&@r%   Ú_model_matchesrY   q   s7   ø€ à�‰‹€Aß‹3Ô*™zÓ*�3Œ3Ð*Š3Ð*ˆ3Ô*™zÓ*Ó*Ð*r/   i ô zclaude-sonnet-5zclaude-opus-4-8zclaude-opus-4-7i ú  zclaude-opus-4zclaude-sonnet-4zclaude-3-7-sonnetzclaude-3-5-sonneti    zclaude-3-5-haikuzclaude-3-opusé   zclaude-3-sonnetzclaude-3-haikuÚminimaxi   Úqwen3i   c                ó0   € V ^8„  d   QhR\         R\        /# rM   )r,   Úint)r-   s   "r%   r.   r.   ‡   s   € ÷ _ñ _¤Sð _¬Sñ _r/   c                óº   a€ V P                  4       P                  RR4      o\        V3R l\         4       \        RR7      pV'       d   \        V,          # \
        # )zÿMax output tokens for ``model`` via longest substring match against
``_ANTHROPIC_OUTPUT_LIMITS`` (so date-stamped ids and ``:1m``/``:fast`` suffixes resolve, and
``claude-3-5-sonnet`` beats ``claude-3-5``). Dots normalize to hyphens (``claude-opus-4.6``).Ú.Ú-c              3   ó8   <"  € T F  qS9   g   K  Vx € K  	  R # 5irP   rQ   )rR   ÚkeyrT   s   & €r%   rU   Ú,_get_anthropic_max_output.<locals>.<genexpr>Œ   s   øé € ÐHÑ#;˜CÀa¹x—C’CÓ#;ùs   ƒ�
N)rc   Údefault)rI   Úreplacer9   Ú_ANTHROPIC_OUTPUT_LIMITSÚlenÚ_ANTHROPIC_DEFAULT_OUTPUT_LIMIT)rB   Úbest_keyrT   s   & @r%   Ú_get_anthropic_max_outputrk   ‡   sG   ø€ ð 	�‰‹×Ñ˜c 3Ó'€AÜÔHÕ#;ÓHÌcÐ[_Ô`€Hß19Ô# HÕ-Ð^Ô?^Ð^r/   c                ó:   € V ^8„  d   QhR\         \        ,          /# ©r(   rC   )r   r^   )r-   s   "r%   r.   r.   �   s   € ÷ 2ñ 2´X¼cµ]ñ 2r/   c                ó  € \        V \        4      '       g   \        V \        \        34      '       g   R#  \        P
                  ! V 4      '       g   R#  \        T 4      ^ 8”  d   \        T 4      # R#   \         d     R# i ; i)a  ``value`` floored to a positive int, or None when it is not a finite positive number.
Anthropic 400s on max_tokens that are 0, negative, fractional or non-finite; the ``max_tokens
or fallback`` idiom catches 0 but lets ``-1``/``0.5`` through. Booleans are excluded (they
subclass int).N)Ú
isinstancerE   r^   ÚfloatÚmathÚisfiniter    )Úvalues   &r%   Ú&_resolve_positive_anthropic_max_tokensrt   �   so   € ô
 �%œ×Ò¤j°¼¼e¸×&EÒ&EÙðÜ�}Š}˜U×#Ò#Ùð $ô ˜U› aœŒ3ˆu‹:Ð1¨TÐ1øô ô Úðús   ¶A2 Á2BÂ Bc                óR   € V ^8„  d   QhR\         R\        \        ,          R\        /# )r(   rB   Úcontext_lengthrC   )r,   r   r^   )r-   s   "r%   r.   r.   Ÿ   s)   € ÷ 
ñ 
¼Sð 
ÔRZÔ[^ÕR_ð 
Ôknñ 
r/   c                óx   € \        V 4      ;'       g    \        V4      pV^ 8”  d   V# \        RV: RV : R24      h)zâ``requested`` when it is a positive finite number, else the model's output ceiling. Raises
ValueError if neither is positive. The context-window clamp is the caller's job so the
positive-value contract stays endpoint-agnostic.zJAnthropic Messages adapter requires a positive max_tokens value for model z; got z and no model default resolved.)rt   rk   Ú
ValueError)Ú	requestedrB   rv   Úresolveds   &&& r%   Ú&_resolve_anthropic_messages_max_tokensr{   Ÿ   sO   € ô 6°iÓ@×dÐdÔD]Ð^cÓDd€HØ�!„|ØˆÜ
ðØ‘	˜ 	™}Ð,Kð	Móð r/   c                ó0   € V ^8„  d   QhR\         R\        /# rM   rD   )r-   s   "r%   r.   r.   ¬   s   € ÷ ñ ¤sð ¬tñ r/   c                óx   € \        V 4      ;'       g)    \        V 4      ;'       d    \        V \        4      '       * # )u  True for Claude models using adaptive thinking (4.6+): unknown Claude models default to
adaptive, the explicit legacy list stays manual, and non-Claude models return False â€” except
Kimi/Moonshot, whose Anthropic-compatible endpoints implement the adaptive contract.)r   rK   rY   Ú)_LEGACY_MANUAL_THINKING_CLAUDE_SUBSTRINGSrJ   s   &r%   Ú_supports_adaptive_thinkingr   ¬   s6   € ô & eÓ,÷ ð Ü˜Ó×hÐh¬°uÔ>gÓ(hÔ$hðr/   c                ó0   € V ^8„  d   QhR\         R\        /# rM   rD   )r-   s   "r%   r.   r.   µ   s   € ÷ iñ i¤#ð i¬$ñ ir/   c                óR   € \        V 4      ;'       d    \        V \        4      '       * # )u�   True for models accepting the 'xhigh' effort (Opus 4.7+). Opus/Sonnet 4.6 400 on it â€”
callers downgrade xhigh->max when this returns False.)r   rY   Ú_NO_XHIGH_CLAUDE_SUBSTRINGSrJ   s   &r%   Ú_supports_xhigh_effortrƒ   µ   s"   € ô ' uÓ-×hÐh´nÀUÔLgÓ6hÔ2hÐhr/   c                ó0   € V ^8„  d   QhR\         R\        /# rM   rD   )r-   s   "r%   r.   r.   »   s   € ÷ 
ñ 
¤Sð 
¬Tñ 
r/   c                óŽ   € \        V 4      ;'       d4    \        V 4      ;'       d!    \        V \        \        ,           4      '       * # )a½  True when ``model`` accepts an explicit ``thinking: {"type": "disabled"}``. Adaptive Claude
thinks by default, so "off" only works if the disable is sent; mandatory-thinking families
400 on it and keep the omit behavior. Legacy manual-thinking models are opt-in via
budget_tokens, so omission is already off. Scoped to Claude: Kimi's documented disable is
omission, and sending it a new parameter on the strength of Claude's contract is a guess.)rK   r   rY   Ú%_MANDATORY_THINKING_CLAUDE_SUBSTRINGSÚ$_BETWEEN_TOOLS_OFF_CLAUDE_SUBSTRINGSrJ   s   &r%   Ú_accepts_thinking_disablerˆ   »   sE   € ô 	˜Ó÷ 	tð 	tÜ'¨Ó.÷	tð 	tä˜uÔ&KÔNrÕ&rÓsÔsðr/   c                ó0   € V ^8„  d   QhR\         R\        /# rM   rD   )r-   s   "r%   r.   r.   È   s   € ÷ Hñ H¤#ð H¬$ñ Hr/   c                ó¤   € ^ RI Hp RV P                  4       9  d   R# V! V 4      pVRJ ;'       g!    V^ ,          R8g  ;'       g    V^,          R8  # )zeHaiku generations that never get a thinking request (through 4.5). Haiku 5.5+ uses adaptive thinking.)Úclaude_family_versionÚhaikuFN)é   r�   )Úagent.anthropic_thinking_policyr‹   rI   )rB   r‹   Úparseds   &  r%   Ú_is_pre_thinking_haikur�   È   sL   € åEà�e—k‘k“mÔ#ÙÙ" 5Ó)€FØ�Tˆ>×GÐG˜V A�Y¨'Ñ1×GÐG°V¸AµYÀÑ5GÐGr/   c                ó0   € V ^8„  d   QhR\         R\        /# rM   rD   )r-   s   "r%   r.   r.   Ò   s   € ÷ ñ ¤Cð ¬Dñ r/   c                óh   € \        V 4      ;'       d!    \        V \        \        ,           4      '       * # )u  True for models that 400 on any non-default temperature/top_p/top_k (Opus 4.7 and later;
unknown Claude defaults to forbidding). The 4.6 family and the legacy manual-thinking families
still accept them. Callers omit the fields entirely â€” the API rejects anything non-null.)rK   rY   r‚   r~   rJ   s   &r%   Ú_forbids_sampling_paramsr“   Ò   s3   € ô ˜EÓ"÷ ð ¬>ØÔ*Ô-VÕVó,ô (ð r/   c                ó0   € V ^8„  d   QhR\         R\        /# rM   rD   )r-   s   "r%   r.   r.   Û   s   € ÷ /ñ /œsð /¤tñ /r/   c                ó   € ^ RI Hp V! V 4      # )z¸True for models accepting ``speed: "fast"`` (Opus 4.8 / Opus 5 / Opus 5.5, Claude API only).
The list lives in ``agent.model_metadata`` so the wire gate and the ``/fast`` toggle agree.)Úis_anthropic_fast_mode_model)Úagent.model_metadatar–   )rB   r–   s   & r%   Ú_supports_fast_moder˜   Û   s   € õ Bá'¨Ó.Ð.r/   z&fine-grained-tool-streaming-2025-05-14zinterleaved-thinking-2025-05-14zcontext-1m-2025-08-07zfast-mode-2026-02-01zclaude-code-20250219zoauth-2025-04-20z2.1.74c                ó:   € V ^8„  d   QhR\         \        ,          /# rm   )Úlistr,   )r-   s   "r%   r.   r.     s   € ÷ ñ ¤¤c¥ñ r/   c                 ó¶  € / p \          F4  p\        P                  ! V4      pV'       g   K#  V P                  V4       K6  	  \         F…  p\          Fx  p\
        P                  P                  \
        P                  P                  V4      V4      p\
        P                  P                  V4      '       g   Kg  V P                  V4       Kz  	  K‡  	  \        V 4      # )a  Executable paths to try, deduped and filtered to files that exist.

Two passes: every PATH hit first (what the user's shell would run), then the
well-known install prefixes. A single nested loop would probe a stale prefix
``claude`` before a current PATH ``claude-code``.
)Ú_CLAUDE_CODE_NAMESÚshutilÚwhichÚ
setdefaultÚ_CLAUDE_CODE_PREFIXESÚosÚpathÚjoinÚ
expanduserÚisfilerš   )ÚseenÚnameÚhitÚprefixr¢   s        r%   Ú_claude_code_candidatesrª     s–   € ð €Dß"ˆÜ�lŠl˜4Ó ˆß‰3Ø�O‰O˜CÖ ñ #÷ (ˆß&ˆDÜ—7‘7—<‘<¤§¡× 2Ñ 2°6Ó :¸DÓAˆDÜ�w‰w�~‰~˜d×#Ô#Ø—‘ Ö%ó 'ñ (ô
 �‹:Ðr/   c                óF   € V ^8„  d   QhR\         R\        \         ,          /# )r(   ÚcommandrC   )r,   r   )r-   s   "r%   r.   r.     s   € ÷ 
'ñ 
'¤#ð 
'¬(´3­-ñ 
'r/   c                ó~   € V \         9  d   R# ^ RIHp V! V \        R7      P                  pV'       d
   V^ ,          # R# )a#  Path of a bare Claude Code command name on PATH, else in an install prefix; None for any other command.

Core's own presence checks (external-process providers, ``claude setup-token``) ask this so they
agree with version detection about whether the CLI is installed under a GUI/service PATH.N)Úlocate_command)Ú
known_dirs)rœ   Úhermes_platform.resolverr®   r    r¬   )r¬   r®   Úfounds   &  r%   Úfind_claude_code_clir²     s7   € ð
 Ô(Ô(ÙÝ7á˜7Ô/DÔE×MÑM€Eßˆ5��8Ð& $Ð&r/   c                ó$   € V ^8„  d   QhR\         /# rm   r+   )r-   s   "r%   r.   r.   &  s   € ÷ )ñ )¤Sñ )r/   c                 ó  € \        4        Fä  p \        \        4      ;_uu_ 4        \        P                  ! V R.\        P
                  RRRR^RR7      pVP                  ^ 8X  dƒ   VP                  P                  4       '       dc   VP                  P                  4       P                  4       ^ ,          pV'       d,   V^ ,          P                  4       '       d   VuuRRR4       u # RRR4       Kæ  	  \        #   + '       g   i     Kÿ  ; i)zOInstalled Claude Code version (``claude --version``), else the static fallback.z	--versionTzutf-8rf   F)ÚstdinÚcapture_outputÚtextÚencodingÚerrorsÚtimeoutÚcheckN)rª   r   r    Ú
subprocessÚrunÚDEVNULLÚ
returncodeÚstdoutÚstripÚsplitÚisdigitÚ_CLAUDE_CODE_VERSION_FALLBACK)ÚcmdÚresultÚversions      r%   Ú_detect_claude_code_versionrÈ   &  sÂ   € ä&Ö(ˆÜ”i× Õ Ü—^’^Ø�kÐ"Ü ×(Ñ(¸ÀDÐSZÐclÐvwØôˆFð
 × Ñ  AÔ%¨&¯-©-×*=Ñ*=×*?Ò*?Ø Ÿ-™-×-Ñ-Ó/×5Ñ5Ó7¸Õ:�ß˜w q�z×1Ñ1×3Ò3Ø"÷ !× Ö ñ )ô )Ð(÷ !× Ð ús   ¥AC9Â6C9Â<C9ÃC9Ã9D
c                ó$   € V ^8„  d   QhR\         /# rm   r+   )r-   s   "r%   r.   r.   6  s   € ÷ &ñ &¤#ñ &r/   c                 ó2   € \         f   \        4       s \         # )zEDetect lazily (only OAuth headers need it) and cache for the process.)Ú_claude_code_version_cacherÈ   rQ   r/   r%   Ú_get_claude_code_versionrÌ   6  s   € ô "Ò)Ü%@Ó%BÐ"Ü%Ð%r/   z9You are Claude Code, Anthropic's official CLI for Claude.Úmcp__Úsession_searchÚchat_history_lookupÚmemoryÚcontext_notesc              #   ó˜   "  € T F@  p\         P                  ! R \         P                  ! V4       R 24      \        V,          3x € KB  	  R# 5i)z\bN)ÚreÚcompileÚescapeÚ_OAUTH_TOOL_NAME_ALIASES)rR   r§   s   & r%   rU   rU   Q  s=   é € ð $á0ˆô ‡Z‚Z�2”b—i’i “oÐ& bÐ)Ó*Ô,DÀTÕ,JÕKÛ0ùs   ‚AA
c                ó0   € V ^8„  d   QhR\         R\         /# )r(   r·   rC   r+   )r-   s   "r%   r.   r.   W  s   € ÷ ñ ¤Sð ¬Sñ r/   c                óF   € \          F  w  rVP                  W 4      p K  	  V # )z@Rewrite prose-safe tool-name tokens to their OAuth wire aliases.)Ú_OAUTH_PROSE_ALIAS_PATTERNSÚsub)r·   ÚpatternÚ	wire_names   &  r%   Ú_apply_oauth_prose_aliasesrÝ   W  s"   € ç9ÑˆØ�{‰{˜9Ó+Šñ :à€Kr/   Údrop_context_1m_betaFc                ó`   € V ^8„  d   QhR\         R,          R\        R\        \         ,          /# )r(   Úbase_urlNrÞ   rC   )r,   rE   rš   )r-   s   "r%   r.   r.   ^  s,   € ÷ ñ ¬¨t­ð Ìdð Ô_cÔdgÕ_hñ r/   c               óô   € \        \        4      p\        V 4      '       d   V'       g   VP                  \        4       \        V 4      '       d%   V Uu. uF  q3\        \        39  g   K  VNK  	  up# V# u upi )uß  Beta headers safe for the configured endpoint. MiniMax (Bearer-auth) rejects both the
fine-grained-tool-streaming beta (every tool-use message errors) and the 1M-context beta.
Azure AI Foundry also uses Bearer auth but keeps both â€” it needs the 1M beta for 1M context,
which native Anthropic does not get by default (some subscriptions reject it; Bedrock opts in
via its own client helper). ``drop_context_1m_beta`` strips the 1M beta after a
subscription/endpoint rejected it.)rš   Ú_COMMON_BETASr   ÚappendÚ_CONTEXT_1M_BETAr   Ú_TOOL_STREAMING_BETA)rà   rÞ   ÚbetasÚbs   &$  r%   Ú_common_betas_for_base_urlrè   ^  sb   € ô ”Ó€EÜ& x×0Ò0×9MØ�‰Ô%Ô&Ü% h×/Ò/Ù ÓV™5�aÔ.BÔDTÐ-UÑ$U—�™5ÑVÐVØ€Lùò Ws   ÁA5Á*A5c                óR   € V ^8„  d   QhR\         R\        \        \        3,          /# )r(   ræ   rC   )rš   Údictr,   )r-   s   "r%   r.   r.   m  s&   € ÷ @ñ @œð @¤¤c¬3 h¥ñ @r/   c                ó<   € V '       d   RRP                  V 4      /# / # )z>``{"anthropic-beta": ...}`` when there are betas, else ``{}``.zanthropic-betaÚ,)r£   )ræ   s   &r%   Ú_beta_headerrí   m  s   € ç27Ð˜cŸh™h u›oÐ.Ð?¸RÐ?r/   c                óF   € V ^8„  d   QhR\         \        \        3,          /# rm   ©rê   r,   )r-   s   "r%   r.   r.   r  s   € ÷ ñ œd¤3¬ 8�nñ r/   c                 ó<   € RRRRRR\        4       P                   2/# )zOSame client-attribution set sent to OpenRouter / Vercel AI Gateway / Fireworks.zHTTP-Refererz%https://hermes-agent.nousresearch.comzX-TitleúHermes Agentz
User-AgentzHermesAgent/)r   Úbase_versionrQ   r/   r%   Ú_attribution_headersró   r  s/   € ð 	Ð?ÀÈNØ˜Ô%5Ó%7×%DÑ%DÐ$EÐFðð r/   c                ó‚   € ^ RI Hp \        V \        \        34      '       d
   V ^ 8”  d   T MRpV! \	        V4      RR7      # )zNhttpx.Timeout with the caller's read timeout (default 900s) and a 10s connect.)ÚTimeoutg      Œ@g      $@)rº   Úconnect)Úhttpxrõ   ro   r^   rp   )rº   rõ   Úreads   &  r%   Ú_client_timeoutrù   z  s3   € åÜ! '¬C´¨<×8Ò8¸WÀq¼[‰7Èu€DÙœ5 ›;°Ô5Ð5r/   c                óh   € V ^8„  d   QhR\         \        \        \        \        3,          3,          /# rm   )Útupler,   rê   r   )r-   s   "r%   r.   r.   �  s%   € ÷ ñ ¬e´C¼¼cÄ3¸h½Ð4GÕ.Hñ r/   c                óÜ   € R\        V4      R^ /p\        P                  ! RR\        V 4      P	                  R4      4      pV'       d$   W2R&   \        V4      '       d   RV9  d   RR/VR	&   W23# )
a½  Shared SDK constructor kwargs -> ``(normalized_base_url, kwargs)``. Retry is delegated to
hermes's outer loop (``max_retries=0``): the SDK default of 2 uses its own backoff that ignores
Retry-After and double-retries inside our loop. Any trailing ``/v1`` is stripped because the
SDK appends ``/v1/messages``. Azure's ``api-version`` goes through ``default_query`` so the
base_url is not corrupted into ``/anthropic?api-version=.../v1/messages``.rº   Úmax_retriesz/v1/?$rH   Ú/rà   zapi-versionz
2025-04-15Údefault_query)rù   rÓ   rÚ   r   Úrstripr   )rà   rº   ÚkwargsÚ
normalizeds   &&  r%   Ú_base_client_kwargsr  �  sq   € ð (¬¸Ó)AÀ=ÐRSÐT€FÜ—’˜	 2Ô'?ÀÓ'I×'PÑ'PÐQTÓ'UÓV€JßØ'ˆzÑÜ'¨
×3Ò3¸ÈZÔ8WØ'4°lÐ&CˆF�?Ñ#ØÐÐr/   c                óX   € V ^8„  d   QhR\         R,          R\        R,          R\        /# ©r(   rà   Nrº   rÞ   ©r,   rp   rE   )r-   s   "r%   r.   r.   �  s1   € ÷ Añ AÜ! D�jðAÜ:?À$½,ðAÜhlñAr/   c               ót  € \        RRR7      p\        4        ^ RIHp \	        W4      w  rgT! YR,          \        V4      '       d   R MRR7      VR	&   R
VR&   \        WcR7      p\        W4      '       d.   \        V\        ,           4      p	R\        4        R2V	R&   RV	R&   M\        V4      p	\        WGW‘R7      # )aË  Anthropic-on-Foundry Entra ID variant of :func:`build_anthropic_client`. The SDK stores
``api_key``/``auth_token`` as static strings, so per-request bearer refresh (Microsoft's
documented Foundry pattern) uses a custom ``httpx.Client`` whose request hook mints a fresh JWT
and rewrites ``Authorization``; the SDK skips its own auth when ``http_client`` is given. The
placeholder ``auth_token`` is still required at construction and makes any leak diagnosable.z:Azure Foundry Anthropic-style endpoints with Entra ID authzInstall with)r*   )Úbuild_bearer_http_clientrº   c                 ó"   € \        V 4      '       * # rP   )r	   )Útokens   &r%   Ú<lambda>Ú:_build_anthropic_client_with_bearer_hook.<locals>.<lambda>¢  s   € ¬O¸EÓ,BÕ(Br/   N)rº   Úsends_as_api_keyÚhttp_clientzentra-id-bearer-via-http-hookÚ
auth_token©rÞ   úclaude-code/ú (external, cli)ú
user-agentÚcliúx-app©Úroute)r2   r   Úagent.azure_identity_adapterr  r  r   rè   r
   rí   Ú_OAUTH_ONLY_BETASrÌ   Ú_new_sdk_client)
Útoken_providerrà   rº   rÞ   r1   r  Únormalized_base_urlr  ræ   Úheaderss
   &&&$      r%   Ú(_build_anthropic_client_with_bearer_hookr  �  sÂ   € ô ÐSÐZhÔ
i€CÜÔÝEÜ"5°hÓ"HÑÐñ
 5Ø yÕ 1ÜG_Ð`h×GiÒGiÒBÐosô€Fˆ=Ñð ;€Fˆ<ÑÜ&Ð':Ôf€EÜ ×9Ò9ô ˜uÔ'8Õ8Ó9ˆØ".Ô/GÓ/IÐ.JÐJZÐ [ˆ�ÑØ ˆ�Òä˜uÓ%ˆÜ˜3¨Ô@Ð@r/   c                óŽ   € V ^8„  d   QhR\         \        \        3,          R\         \        \        3,          R\        R,          /# )r(   r  r  r  N©rê   r,   r   )r-   s   "r%   r.   r.   ²  s8   € ÷ #ñ #¤¤c¬3 h¥ð #¼$¼sÄC¸x½.ð #ÔQTÐW[ÕQ[ñ #r/   c                ó@  € \        V4      pRV9   d   RV9  d   V P                  4       VR&   M!RV9   d   RV9  d   V P                  4       VR&   TP                  \        T;'       g    VP	                  R4      4      4       V'       d   WAR&   V P
                  ! R/ VB # )uD  ``sdk.Anthropic(**kwargs)`` with ``headers`` attached, sending exactly ONE credential.
``route`` is the caller's un-normalized base_url (the ``/v1`` form ``custom_providers`` entries are
keyed by; ``kwargs["base_url"]`` has it stripped) for the per-provider ``extra_headers`` lookup.

The SDK fills whichever of ``api_key`` / ``auth_token`` we left unset from ANTHROPIC_API_KEY /
ANTHROPIC_AUTH_TOKEN in the environment (both loaded from ~/.hermes/.env) and then sends dual
auth â€” x-api-key *and* Authorization: Bearer â€” shipping a foreign credential to Portal / MiniMax
/ OAuth / Entra / third-party endpoints (#26970, #105774). An ``Omit()`` default header is the
SDK-sanctioned way to drop the other header, and unlike an attribute clear it survives
``with_options()``, which re-runs the constructor and re-reads the environment.Úapi_keyr  ÚAuthorizationz	X-Api-Keyrà   Údefault_headersrQ   )rê   ÚOmitÚupdateÚ_custom_provider_extra_headersÚgetÚ	Anthropic)r1   r  r  r  Úmergeds   &&&& r%   r  r  ²  sŽ   € ô �'‹]€FØ�FÔ˜|°6Ô9Ø"%§(¡(£*ˆˆÒØ	˜Ô	 I°VÔ$;Ø!Ÿh™h›jˆˆ{Ñð ‡M�MÔ0°×1PÐ1P¸&¿*¹*ÀZÓ:PÓQÔRßØ$*Ð Ñ!Ø�=Š=Ñ"˜6Ñ"Ð"r/   c                óF   € V ^8„  d   QhR\         \        \        3,          /# rm   rï   )r-   s   "r%   r.   r.   Ë  s   € ÷ 
ñ 
´´S¼#°Xµñ 
r/   c                óš   € V '       g   / #  ^ RI Hp V! \        V 4      4      #   \         d    \        P                  RRR7       / u # i ; i)u²   ``extra_headers`` of the ``custom_providers`` entry routed at *base_url*, else ``{}``.
SECURITY: values routinely carry credentials (Cloudflare Access tokens) â€” never log them.)Ú!get_custom_provider_extra_headersz:custom-provider extra_headers skipped for Anthropic clientT©Úexc_info)Úhermes_cli.configr-  r,   r    ÚloggerÚdebug)rà   r-  s   & r%   r'  r'  Ë  sI   € ÷ Øˆ	ðÝGÙ0´°X³Ó?Ð?øÜô Ü�‰ÐQÐ\`ˆÔaØŠ	ðús   Œ# £$A
Á	A
c                ó$   € V ^8„  d   QhR\         /# rm   r+   )r-   s   "r%   r.   r.   Ø  s   € ÷ ñ ¼3ñ r/   c                óž   € \        V4      '       d   R# \        V4      '       d   R# \        V4      '       d   R# \        V 4      '       d   R# R# )u1  Order-sensitive endpoint/key classification for :func:`build_anthropic_client`. ``kimi``:
Kimi's /coding endpoint 403s without a User-Agent (the Kimi team asked for proper attribution).
``bearer``: MiniMax & co. want Authorization: Bearer â€” checked before the OAuth shape test
because their secrets lack the sk-ant-api prefix and would be misread as OAuth/setup tokens.
``api_key``: third-party proxies use their own x-api-key keys (skip OAuth detection). ``oauth``:
Bearer auth + Claude Code identity (Anthropic routes OAuth by user-agent; without it, 500s).ÚkimiÚbearerr"  Úoauth)r   r   r   r	   )r"  rà   r  s   &&&r%   Ú_auth_styler8  Ø  s@   € ô   ×)Ò)ÙÜÐ0×1Ò1ÙÜ)¨(×3Ò3ÙÜ�w×ÒÙÙr/   c                óX   € V ^8„  d   QhR\         R,          R\        R,          R\        /# r  r  )r-   s   "r%   r.   r.   ê  s0   € ÷ Añ A¬c°D­jð AÌ%ÐRVÍ,ð AÔx|ñ Ar/   c               ór  € \        R4      p\        V 4      '       d$   \        V \        4      '       g   \	        WW#R7      # \        4        \        W4      w  rVRV9   d   VP                  R4      VR&   \        WSR7      p\        WV4      pYVR9   d   RMR&   \        VR8X  d   V\        ,           MT4      p	VR	8X  d   / \        4       CV	Cp	MVR8X  d   R
\        4        R2V	R&   RV	R&   \        V4      '       d3   \        4       P                  4        F  w  r«V	P!                  W«4       K  	  \#        WFW‘R7      # )u  Create an Anthropic client, auto-detecting setup-tokens vs API keys. ``api_key`` is a static
``str`` or a ``Callable[[], str]`` Entra ID bearer provider (routed through
:func:`_build_anthropic_client_with_bearer_hook`). ``timeout`` overrides the 900s read timeout
(connect stays 10s). ``drop_context_1m_beta`` strips ``context-1m-2025-08-07`` from the
client-level beta header â€” the reactive OAuth retry in run_agent uses it after a subscription
rejects it; fresh clients keep the default so 1M-capable subscriptions keep the capability.zthe Anthropic providerr  rÿ   rþ   rà   r7  r  r"  r5  r  r  r  r  r  r  )r6  r7  )r2   Úcallablero   r,   r  r   r  r   rè   r8  rí   r  ró   rÌ   r   ÚitemsrŸ   r  )r"  rà   rº   rÞ   r1   r  r  Úcommon_betasÚstyler  ÚkrS   s   &&&$        r%   Úbuild_anthropic_clientr@  ê  s4  € ô Ð/Ó
0€CÜ�×Ò¤¨G´S×!9Ò!9Ü7Ø˜wô
ð 	
ô ÔÜ"5°hÓ"HÑÐØ˜&Ô Ø0×7Ñ7¸Ó<ˆˆzÑÜ-Ð.AÔm€LÜ˜Ð+>Ó?€EØJQ˜5Ð$7Ô7‰<¸YÑGÜ¸uÈÔ?O˜<Ô*;Ö;ÐUaÓb€GØ�„Ø7Ô)Ó+Ð7¨wÐ7‰Ø	�'Ô	Ø".Ô/GÓ/IÐ.JÐJZÐ [ˆ�ÑØ ˆ�ÑÜ˜X×&Ò&ô )Ó*×0Ñ0Ö2‰DˆAØ×Ñ˜qÖ$ñ 3ä˜3¨Ô@Ð@r/   c                ó$   € V ^8„  d   QhR\         /# )r(   Úregionr+   )r-   s   "r%   r.   r.     s   € ÷ ñ ¬3ñ r/   c                óæ  € ^ RI HpHp \        R4      p\	        VR4      '       g   \        R4      hV! 4       pRVP                  R4      RVP                  R4      R	VP                  R	4      R
VP                  R4      /pVP                  ! RRV R\        R4      /VP                  4        UUu/ uF  w  rgV'       g   K  WgbK  	  uppBR^ R/ \        . \        O\        N4      CV! 4       C/B # u uppi )añ  AnthropicBedrock client for Bedrock Claude models (boto3 default credential chain). The
SDK's native Bedrock adapter gives full Claude feature parity (prompt caching, thinking
budgets, adaptive thinking, fast mode) that Converse lacks. The common betas plus
``context-1m-2025-08-07`` are attached: without the latter Bedrock caps Opus 4.6/4.7 at 200K.
A configured ``bedrock.guardrail`` rides as InvokeModel headers so every client built here
(primary, auxiliary, per-request rebuild) enforces it.)Úbedrock_guardrail_headersÚscoped_aws_session_kwargszthe Bedrock providerÚAnthropicBedrockz?anthropic.AnthropicBedrock not available. Run: hermes pm repairÚaws_access_keyÚaws_access_key_idÚaws_secret_keyÚaws_secret_access_keyÚaws_session_tokenÚaws_profileÚprofile_nameÚ
aws_regionrº   Nrý   r$  rQ   )Úagent.bedrock_adapterrD  rE  r2   Úhasattrr"   r(  rF  rù   r<  rí   râ   rä   )rB  rD  rE  r1   ÚscopedÚ
aws_kwargsr?  rS   s   &       r%   Úbuild_anthropic_bedrock_clientrS    s  € ÷ [Ü
Ð-Ó
.€CÜ�3Ð*×+Ò+ÜÐ[Ó\Ð\ñ 'Ó(€FØ" F§J¡JÐ/BÓ$CÐEUÐW]×WaÑWaÐbyÓWzØ% v§z¡zÐ2EÓ'FÈÐW]×WaÑWaÐbpÓWqðs€Jà×Òñ ØðÜ#2°4Ó#8ðØNX×N^ÑN^ÔN`Ô<fÑN`ÁdÀaÔde¼T¸QºTÑN`Ò<fñàðð lœ<Ð(J¬-Ð(JÔ9IÐ(JÓKÐkÑOhÓOjÐkòð ùÛ<fs   Â,C-Â=C-c                ó0   € V ^8„  d   QhR\         R\         /# ©r(   r§   rC   r+   )r-   s   "r%   r.   r.   "  s   € ÷ 8ñ 8¤ð 8¬ñ 8r/   c                ól   € V P                  R4      '       d   V # \        V P                  R4      ,           # )aÅ  OAuth wire form of a tool name (no aliasing): ``mcp__<...>``. Anthropic's OAuth billing
classifier treats a single-underscore ``mcp_`` tool name as a third-party-app fingerprint
(HTTP 400 "Third-party apps now draw from extra usage"); ``mcp__foo`` is accepted. Both bare
Hermes tools (``read_file``) and native MCP tools registered as ``mcp_<server>_<tool>`` must
land on the double-underscore form. normalize_response reverses both via registry lookup.rÍ   Úmcp_)Ú
startswithÚ_MCP_TOOL_PREFIXÚremoveprefix)r§   s   &r%   Ú_normalize_to_mcp_wirer[  "  s.   € ð ‡��w×ÒØˆÜ˜d×/Ñ/°Ó7Õ7Ð7r/   c                ó\   € V ^8„  d   QhR\         \        \        \        3,          ,          /# )r(   Úanthropic_tools)rš   rê   r,   r   )r-   s   "r%   r.   r.   -  s    € ÷ ñ ¤t¬D´´c°­NÕ';ñ r/   c                óò   a€ T ;'       g    .  Uu0 uFP  p\        VP                  R4      \        4      '       g   K*  VR,          \        9  g   K>  \	        VR,          4      kKR  	  upoR V3R llpV# u upi )aW  Return ``name -> OAuth wire name`` for this request's tool set. An alias must never collide
with a wire name owned by a non-alias tool: two identical tool names in one request is a hard
400, strictly worse than the bug being fixed. Mirrors normalize_response's "registered tool
wins" so outbound and inbound agree on who owns a contested name.r§   c                ó0   € V ^8„  d   QhR\         R\         /# rU  r+   )r-   s   "r%   r.   Ú'_oauth_wire_namer.<locals>.__annotate__8  s   € ÷ ,ñ ,”cð ,œcñ ,r/   c                 ó|   <€ \         P                  V 4      pV'       d   \        V,           S9  d   Tp \        V 4      # rP   )rÖ   r(  rY  r[  )r§   ÚaliasedÚclaimeds   & €r%   Úto_wireÚ"_oauth_wire_namer.<locals>.to_wire8  s3   ø€ Ü*×.Ñ.¨tÓ4ˆßÔ'¨'Õ1¸Ô@ØˆDÜ% dÓ+Ð+r/   )ro   r(  r,   rÖ   r[  )r]  Útoolrd  rc  s   &  @r%   Ú_oauth_wire_namerrg  -  st   ø€ ð %×*Ð*¨Ð*óá*ˆDÜ�d—h‘h˜vÓ&¬×,ô 	-à15°fµÔE]Ñ1]ô 	-Ô˜t F�|Ö,Ù*ñ€G÷,ð ,ð €Nùòs   �'A4¼A4ÁA4z-(?<![\w./:@'"`-])hermes-agent(?![\w/@-]|\.\w)c                ó¼  € RRR\         /p\        V \        4      '       d   V '       d   RRRV /.p V.\        V \        4      '       d   T M. ,           p V  F‰  p\        V\        4      '       g   K  VP                  R4      R8X  g   K3  VP                  RR4      p\         F  w  rxVP                  Wx4      pK  	  \        P                  RV4      p\        V4      VR&   K‹  	  T;'       g    .  FX  p	RV	9   d   V! V	R,          4      V	R&   \        V	P                  R4      \        4      '       g   KC  \        V	R,          4      V	R&   KZ  	  V F�  p
\        V
P                  R4      \        4      '       d   V
P                  R4      M.  FN  p\        V\        4      '       g   K  VP                  R4      R8X  g   K3  RV9   g   K<  V! VR,          4      VR&   KP  	  K�  	  V # )	a   OAuth transforms: Claude Code system prefix, product-name sanitizing (avoids server-side
content filters), tool/description aliasing, and the same tool renames on replayed tool_use
blocks so history matches ``tools[]``. Returns the new ``system``; tools and messages are
mutated in place.Útyper·   rH   úclaude-coder§   ÚdescriptionÚcontentÚtool_use)Ú_CLAUDE_CODE_SYSTEM_PREFIXro   r,   rš   rê   r(  Ú_OAUTH_SYSTEM_REPLACEMENTSrf   Ú_OAUTH_SLUG_PATTERNrÚ   rÝ   )Úsystemr]  Úanthropic_messagesrd  Úcc_blockÚblockr·   ÚoldÚnewrf  Úmsgs   &&&&       r%   Ú_apply_claude_code_identityrx  L  s�  € ð
 ˜ Ô(BÐC€HÜ�&œ#×Ò§6Ø˜6 6¨6Ð2Ð3ˆØˆZ¤Z°¼×%=Ò%=™6À2ÕF€FÛˆÜ�eœT×"Ô" u§y¡y°Ó'8¸FÖ'BØ—9‘9˜V RÓ(ˆDß6‘�Ø—|‘| CÓ-’ñ 7ä&×*Ñ*¨=¸$Ó?ˆDÜ6°tÓ<ˆE�&‹Mñ ð  ×%Ð% 2Ò%ˆØ�TŒ>Ù" 4¨¥<Ó0ˆD�‰LÜ�d—h‘h˜}Ó-¬s×3Ô3Ü"<¸TÀ-Õ=PÓ"QˆD�Óñ	 &ó
 "ˆÜ+5°c·g±g¸iÓ6HÌ$×+OÒ+O�S—W‘W˜YÔ'ÐUWÒWˆEÜ˜%¤×&Ô&¨5¯9©9°VÓ+<À
Ö+JÈvÐY^ÎÙ '¨¨f­Ó 6��f“ó Xñ "ð €Mr/   c          
      óŒ   € V ^8„  d   QhR\         \        \        3,          R\        R\        R\         \        \        3,          /# )r(   Úreasoning_configrB   Úeffective_max_tokensrC   )rê   r,   r   r^   )r-   s   "r%   r.   r.   h  s>   € ÷ ñ ¤t¬C´¨H¥~ð ¼cð ÔY\ð ÔaeÔfiÔknÐfnÕaoñ r/   c           
     ó  € V P                  R4      RJ d6   \        V\        4      '       d   RRR//# \        V4      '       d   RRR//# / # \	        V4      '       d   / # \        V P                  RR4      4      P                  4       p\        V4      '       d=   \        P                  VR4      pVR	8X  d   \        V4      '       g   R
pRRRRR/RRV//# \        P                  VR4      pRRRRV/R^R\        W%R,           4      /# )aª  Map ``reasoning_config`` to Anthropic thinking kwargs. Adaptive models (Claude 4.6+,
Kimi/Moonshot) get ``thinking.type=adaptive`` + ``output_config.effort``; older models and
manual-only compat endpoints (MiniMax) get budget_tokens. Haiku through 4.5 gets none. On
4.7+ ``thinking.display`` defaults to "omitted", hiding the reasoning Hermes shows in its CLI,
so "summarized" is requested to keep the activity feed populated.ÚenabledFÚthinkingri  Úbetween_toolsÚdisabledÚeffortr5   r3   r9   ÚadaptiveÚdisplayÚ
summarizedÚoutput_configr6   Úbudget_tokensÚtemperatureÚ
max_tokensrZ   )r(  rY   r‡   rˆ   r�   r,   rI   r   ÚADAPTIVE_EFFORT_MAPrƒ   ÚTHINKING_BUDGETr9   )rz  rB   r{  r�  Úadaptive_effortÚbudgets   &&&   r%   Ú_thinking_kwargsr�  h  s  € ð ×Ñ˜IÓ&¨%Ó/ô ˜%Ô!E×FÒFØ ¨Ð 9Ð:Ð:Ü5NÈu×5UÒ5U�
˜V ZÐ0Ð1Ð]Ð[]Ð]Ü˜e×$Ò$Øˆ	ÜÐ!×%Ñ% h°Ó9Ó:×@Ñ@ÓB€FÜ" 5×)Ò)Ü-×1Ñ1°&¸(ÓCˆØ˜gÔ%Ô.DÀU×.KÒ.KØ#ˆOØ˜V Z°¸LÐIÈ?Ð]eÐgvÐ\wÐxÐxÜ× Ñ  ¨Ó.€Fà�V˜Y¨¸Ð@Ø�qØ”cÐ.¸µÓ>ðð r/   ri  ÚautoÚrequiredrW   c                ó   € V ^8„  d   QhR\         R\        \        ,          R\        \        \        ,          ,          R\        \        ,          R\        \        \         \
        3,          ,          R\        \         ,          R\        R\        R	\        \        ,          R
\         R,          R\        R\        R\        \         \
        3,          /# )r(   rB   ÚmessagesÚtoolsrˆ  rz  Útool_choiceÚis_oauthÚpreserve_dotsrv   rà   NÚ	fast_moderÞ   rC   )r,   rš   rê   r   r^   r   rE   )r-   s   "r%   r.   r.   ‰  sÉ   € ÷ Añ AÜðAÜœt�*ðAÜ-5´d¼4µjÕ-AðAÜOWÔX[Í}ðAäœt¤C¬ H�~Õ.ðAä=EÄc½]ðAô ðAô ,0ðAô JRÔRUÍðAô �D�jð	Aô -1ð	Aô QUð	Aô
 
Œ#Œsˆ(…^ñAr/   c                ó  € \        WV R7      w  rÍV'       d   \        V4      M. p\        V	4      '       g   \        WR7      p \	        W0VR7      pV'       d   Wø8”  d   \        V^,
          ^4      pV'       d   \        V4      MRpV'       d   \        WÎVV4      pRV RVRV/pV'       d   VVR&   V'       dp   VVR	&   VR
8X  d   VP                  R	R4       MPVe   \        V\        4      '       d7   \        P                  V4      ;'       g    RRRV'       d	   V! V4      MT/VR&   V'       d2   \        V\        4      '       d   VP                  \        W@V4      4       \!        V 4      '       d   R F  pVP                  VR4       K  	  V
'       ds   \#        V	4      '       gb   \%        V 4      '       dQ   RVP'                  R/ 4      R&   \)        W›R7      p\+        TV'       d   \,        M. ,           \.        .,           4      VR&   V# )uæ  Build kwargs for anthropic.messages.create(). ``max_tokens`` is the OUTPUT cap for one
response; ``context_length`` is the TOTAL window (input + output). ``max_tokens=None`` uses the
model's native output ceiling; if that exceeds ``context_length`` (small local endpoints) it is
clamped to ``context_length - 1``. The clamp ignores prompt size â€” callers must catch
"max_tokens too large given prompt" and retry smaller (parse_available_output_tokens_from_error).
``is_oauth`` applies Claude Code compatibility transforms; ``preserve_dots`` keeps model-name
dots (DashScope: qwen3.5-plus); a third-party ``base_url`` strips thinking signatures;
``fast_mode`` adds ``extra_body.speed="fast"`` plus the fast-mode beta on native Anthropic only.)rà   rB   )r•  )rv   NrB   r‘  rˆ  rq  r’  Únoneri  rf  r§   r“  ÚfastÚ
extra_bodyÚspeedr  Úextra_headers)r‡  Útop_pÚtop_k)r   r   r   r   r{   r9   rg  rx  Úpopro   r,   Ú_TOOL_CHOICE_MAPr(  rê   r&  r�  r“   r   r˜   rŸ   rè   rí   r  Ú_FAST_MODE_BETA)rB   r‘  r’  rˆ  rz  r“  r”  r•  rv   rà   r–  rÞ   rq  rr  r]  r{  rd  r  rc   ræ   s   &&&&&&&&&&&&        r%   Úbuild_anthropic_kwargsr¢  ‰  sÅ  € ô "?¸xÐbgÔ!hÑ€Fß;@Ô0°Ô7Àb€Oô $ H×-Ò-Ü$ UÔHˆäAÀ*ÐdrÔsÐßÐ.Ô?Ü" >°AÕ#5°qÓ9Ðß4<Ô Ô0À$€GßÜ,¨VÐFXÐZaÓbˆØ% u¨jÐ:LÈlÐ\pÐq€FßØ!ˆˆxÑßØ)ˆˆw‰Ø˜&Ô Ø�J‰J�w Õ%ØÒ ¤J¨{¼C×$@Ò$@ô %5×$8Ñ$8¸Ó$E÷ %ð %Ø˜ Ç©°Ô(<È[ðJˆF�=Ñ!÷ œJÐ'7¼×>Ò>Ø�‰Ô&Ð'7Ð@TÓUÔVô   ×&Ò&Û4ˆCØ�J‰J�s˜DÖ!ñ 5÷
 Ô;¸H×EÒEÔJ]Ð^c×JdÒJdØ7=ˆ×Ñ˜,¨Ó+¨GÑ4Ü*¨8Ô_ˆÜ".¨uÏXÕ8IÐ[]Õ/^ÔbqÐarÕ/rÓ"sˆˆÑØ€Mr/   Ú
log_prefixrH   c                ó<   € V ^8„  d   QhR\         R\        R\         /# )r(   Ú
api_kwargsr£  rC   )r   r,   )r-   s   "r%   r.   r.   Ò  s!   € ÷ ñ ¬#ð ¼cð Ì3ñ r/   c               óÆ   € \        V \        4      '       d   \        P                  V 4      MRpV'       d,   V F  pW K  	  \        P                  RV\        V4      4       V # )a¥  Drop Responses-API-only keys before an Anthropic Messages SDK call. Boundary guard for
api_mode-flip races (a concurrent auxiliary call mutating a shared agent between kwargs build
and dispatch): a Responses-shaped payload reaching ``messages.stream()`` dies with a
non-retryable TypeError that takes the whole turn and fallback chain with it. Mutates and
returns ``api_kwargs``; logs a WARNING so the race stays visible.uò   %sStripped Responses-only kwarg(s) %s from an Anthropic Messages call (api_mode flip race â€” see #31673). The call will proceed; this breadcrumb means a kwargs build ran under a Responses api_mode while dispatch ran under anthropic_messages.rQ   )ro   rê   Ú_RESPONSES_ONLY_KWARGSÚintersectionr1  ÚwarningÚsorted)r¥  r£  Úleakedrc   s   &$  r%   Úsanitize_anthropic_kwargsr¬  Ò  s_   € ô AKÈ:ÔW[×@\Ò@\Ô#×0Ñ0°Ô<Ðbd€FßÛˆCØ’ñ ä�‰ðDð Ü�6‹Nô	
ð Ðr/   c                ód   € V ^8„  d   QhR\         \        \        3,          R\        R,          RR/# )r(   r¥  rà   NrC   r   )r-   s   "r%   r.   r.   ç  s.   € ÷ .ñ .¬D´´c°­Nð .ÄcÈDÅjð .ÐUYñ .r/   c                ó€   € \         \        V4      9  d   R# V P                  R4      ;'       g    R F  pRVR&   K
  	  R# )aÆ  Retry knob for a malformed fine-grained tool-JSON stream (#107830): the beta streams tool
args unvalidated, so a model that emits ``{"names": cronjob_manage}`` breaks the SDK parser
and an identical retry breaks identically. ``eager_input_streaming: false`` per tool restores
Anthropic's buffered, validated args for the rest of this turn (the flag lives on the turn's
kwargs, so a later retry of the same turn keeps it; the changed ``tools`` block costs one
prompt-cache miss, cheaper than a dead turn). Off the happy path on purpose:
buffering a large payload is a zero-event gap the stale-stream detector kills. No-op on
endpoints that never get the beta (MiniMax) rather than sending them an unknown field.Nr’  FÚeager_input_streamingrQ   )rå   rè   r(  )r¥  rà   rf  s   && r%   Úbuffer_anthropic_tool_inputr°  ç  s>   € ô Ô#=¸hÓ#GÔGÙØ—‘˜wÓ'×-Ð-¨2Ò-ˆØ(-ˆÐ$Ó%ó .r/   c                   ó6   a € ] tR tRt o RtR tR tR tRtV t	R# )Ú_UsageNormalizingStreamiö  zYRaw SSE iterator proxy that fills ``usage: null`` before the SDK accumulates it (#60683).c                ó   € Wn         R # rP   ©Ú_inner)ÚselfÚinners   &&r%   Ú__init__Ú _UsageNormalizingStream.__init__ù  s   € ØŽr/   c                ó.   € \        V P                  V4      # rP   )Úgetattrrµ  )r¶  r§   s   &&r%   Ú__getattr__Ú#_UsageNormalizingStream.__getattr__ü  s   € Ü�t—{‘{ DÓ)Ð)r/   c              #  ó\  "  € ^ RI HpHp ^ pV P                   F�  p\	        VRR4      pVR8X  dO   \	        VRR4      p\	        VRR4      pVe   Vf   V! ^ ^ R7      Vn        M@Ve   \	        VR^ 4      ;'       g    ^ pM$VR	8X  d   \	        VRR4      f   V! VR
7      Vn        Vx € K�  	  R# 5i)é    )ÚMessageDeltaUsageÚUsageri  NÚmessage_startÚmessageÚusage)Úinput_tokensÚoutput_tokensrÆ  Úmessage_delta)rÆ  )Úanthropic.typesrÀ  rÁ  rµ  r»  rÄ  )r¶  rÀ  rÁ  rÆ  ÚeventÚetyperÃ  rÄ  s   &       r%   Ú__iter__Ú _UsageNormalizingStream.__iter__ÿ  s«   é € ç<ØˆØ—[”[ˆEÜ˜E 6¨4Ó0ˆEØ˜Ô'Ü! %¨°DÓ9�Ü ¨°$Ó7�ØÒ&¨5ª=Ù$)°qÈÔ$J�G•MØÒ&Ü$+¨E°?ÀAÓ$F×$KÐ$KÈ!�MøØ˜/Ô)¬g°e¸WÀdÓ.KÒ.Sñ 0¸mÔL�”ØŒKó !ùs   ‚A7B,Á:2B,r´  N)
Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r¸  r¼  rË  Ú__static_attributes__Ú__classdictcell__)Ú__classdict__s   @r%   r²  r²  ö  s   ø‡ € Ùcòò*÷ð r/   r²  c                ó0   € V ^8„  d   QhR\         R\         /# )r(   Úmessage_streamrC   )r   )r-   s   "r%   r.   r.     s   € ÷ 
ñ 
¬3ð 
´3ñ 
r/   c                óp   € \        V RR4      pVe   \        VR4      '       g   V # \        V4      V n        V # )aO  Patch ``usage: null`` on raw SSE events before the SDK accumulates them.

Anthropic-compatible providers (MiniMax) send ``usage: null`` on message_start and/or
message_delta; the SDK's accumulate_event() then dies on ``usage.output_tokens`` mid-
iteration (#60683). Wraps ``MessageStream._raw_stream`` in place; no-op for other shapes.Ú_raw_streamNrË  )r»  rP  r²  rØ  )rÖ  Úraws   & r%   Únormalize_stream_usagerÚ    s<   € ô �. -°Ó
6€CØ
‚{œ' # z×2Ò2ØÐÜ!8¸Ó!=€NÔØÐr/   c                ó0   € V ^8„  d   QhR\         R\        /# )r(   r#   rC   )r    rE   )r-   s   "r%   r.   r.      s   € ÷ 
1ñ 
1¤ið 
1´Dñ 
1r/   c                ó”   € \        V 4      P                  4       pRV9   d
   RV9   d   R# RV9   d   R# RV9  d   R# ^ RIHp V! V 4      # )z@True when an Anthropic stream call should fall back to create().Ústreamznot supportedTzunexpected event orderÚinvokemodelwithresponsestreamF)Ú is_streaming_access_denied_error)r,   rI   rO  rß  )r#   Ú	err_lowerrß  s   &  r%   Ú_is_stream_unavailable_errorrá     sH   € ä�C“—‘Ó €IØ�9Ô °IÔ!=ÙØ 9Ô,ÙØ&¨iÔ7ÙÝFÙ+¨CÓ0Ð0r/   c           
     ó  € T ! R/ VP                  4        UUu/ uF  w  rVVR8w  g   K  WVbK  	  uppB ;_uu_ 4       p\        V4      p\        V4      '       d    V! \        VRR4      4       RpRp	V Fc  p
\        V
RR4      R	8X  d   Rp	\        V
RR4      R
8X  d   \        \        V
RR4      RR4      pVe   Tp\        V4      '       g   KZ   V! V
4       Ke  	  V	'       g   \        R4      hVP                  4       pVe   WŒn
        VuuRRR4       # u uppi   \         d    \
        P                  RTRR7        LÑi ; i  \         d    h \         d    \
        P                  RTRR7        EK  i ; i  + '       g   i     R# ; i)zJ``messages.stream()`` -> final Message, ticking the best-effort callbacks.rÝ  ÚresponseNz%son_response callback failedTr.  Fri  Úmessage_stoprÇ  ÚdeltaÚstop_detailsz!%son_stream_event callback failedzTAnthropic Messages stream ended before message_stop (possible upstream stream drop).rQ   )r<  rÚ  r;  r»  r    r1  r2  ÚTimeoutErrorr   Úget_final_messageræ  )Ú	stream_fnr¥  r£  Úon_stream_eventÚon_responser?  rS   rÝ  ræ  Úsaw_message_stoprÉ  ÚdetailsrÃ  s   &&&&&        r%   Ú_stream_final_messagerî  -  sŽ  € á	Ñ	L z×'7Ñ'7Ô'9ÔKÑ'9™t˜q¸QÀ(¹]”d�a’dÑ'9ÒK×	LÒ	LÐPVÜ'¨Ó/ˆÜ�K× Ò ðYÙœG F¨J¸Ó=Ô>ð ˆØ ÐÛˆEÜ�u˜f dÓ+¨~Ô=Ø#'Ð Ü�u˜f dÓ+¨Ô>Ü!¤'¨%°¸$Ó"?ÀÐQUÓV�ØÒ&Ø#*�LÜ˜O×,Ò,Ùð]Ù Ö&ñ ÷$  Ü"Øfóð ð ×*Ñ*Ó,ˆØÒ#Ø#/Ô Ø÷Q 
MÒ	LùÓKøô
 ô YÜ—‘Ð<¸jÐSW�ÖXðYûô*  ô ð Üô ]Ü—‘Ð@À*ÐW[�×\Ð\ð]ú÷? 
M×	LÐ	Lúsj   ˜D¨D¼E8ÁDÁ-A E8ÃEÃE8Ã"&E8Ä#D?Ä<E8Ä>D?Ä?E8ÅE5	ÅE5	Å0E8Å4E5	Å5E8Å8F		Úprefer_streamTrê  rë  c          
      óT   € V ^8„  d   QhR\         R\        R\        R\        R\         /# )r(   Úclientr¥  r£  rï  rC   )r   rê   r,   rE   )r-   s   "r%   r.   r.   Z  s=   € ÷ Yñ YÜðYÜ!ðYÜ25ðYÜKOðYô 	ñYr/   c          
     ó¸  € \        WR7       \        V RR4      p\        VRR4      pV'       d    \        V4      '       d    \        WqW$V4      # VP                  ! R/ VP                  4        U	U
u/ uF  w  ršV	R8w  g   K  WšbK  	  up
p	B #   \         d    h \
         d3   p\        T4      '       g   h \        P                  RY(4        Rp?L€Rp?ii ; iu up
p	i )a¨  Create an Anthropic message, aggregating via stream when available. Some Anthropic-compatible
gateways are SSE-only and answer ``create()`` with ``text/event-stream``, which the SDK surfaces
as raw text (callers then crash on ``.content``), so prefer ``messages.stream()`` like the main
turn path and fall back to ``create()`` only for providers that explicitly don't support
streaming (restricted Bedrock roles). Both callbacks are best-effort and fire only on the
streaming path: ``on_stream_event(event)`` lets liveness watchdogs see forward progress;
``on_response(httpx_response)`` exposes headers the parsed Message drops (Nous Portal's
``x-nous-credits-*`` balance family).)r£  r‘  NrÝ  zN%sAnthropic Messages stream unavailable; falling back to messages.create(): %srQ   )r¬  r»  r;  rî  rç  r    rá  r1  r2  Úcreater<  )rñ  r¥  r£  rï  rê  rë  Úmessages_apiré  r#   r?  rS   s   &&$$$$     r%   Úcreate_anthropic_messagerõ  Z  sÍ   € ô ˜jÕ@Ü˜6 :¨tÓ4€LÜ˜ h°Ó5€Ißœ )×,Ò,ð		Ü(¨À
Ð]hÓiÐið ×ÒÑX°:×3CÑ3CÔ3EÔ!WÑ3E©4¨1ÈÈhÉ¤$ !¢$Ñ3EÒ!WÑXÐXøô ô 	ØÜô 	Ü/°×4Ò4ØÜ�L‰LØ`Ðbl÷ñ ûð	üó "Xs*   ÁB Á/CÁ?CÂCÂ CÂ!(CÃCc                ó¼   € V ^8„  d   Qh/ ^ \         9   d
   \        ;R&   ^\         9   d   \        \        ,          ;R&   ^\         9   d   \        \        ,          ;R&   # )r(   r   r!   rË   )Ú__conditional_annotations__r   r   r    r,   )r-   s   "r%   r.   r.      sQ   € ÷ >ñ >÷F Ò ”Ñ ñG>÷L 5Ò 4œ(¤9Õ-Ñ 4ñM>÷f 1Ò 0œH¤S�MÑ 0òg>r/   )zInstall it with)zclaude-3zclaude-opus-4-0zclaude-opus-4.0zclaude-opus-4-1zclaude-opus-4.1zclaude-sonnet-4-0zclaude-sonnet-4.0zclaude-opus-4-2025zclaude-sonnet-4-2025r;   zclaude-opus-4.5r<   zclaude-sonnet-4.5r=   zclaude-haiku-4.5)r>   zclaude-opus-4.6r?   zclaude-sonnet-4.6)r@   zclaude-opus-5-5zclaude-opus-5.5)zclaude-sonnet-5-5zclaude-sonnet-5.5rP   )z~/.local/binz~/.claude/localz~/binz~/.npm-global/binz
~/.bun/binz~/.volta/binz/opt/homebrew/binz/usr/local/bin)rG   rj  )NN))rñ   úClaude Code)zHermes agentrø  )zNous Researchr)  )NFFNNFF>   ÚinputÚstoreÚinstructionsÚparallel_tool_calls)yr÷  rÑ  r   r   Úloggingrq   r¡   rÓ   r�   r¼   Ú
contextlibr   Útypingr   r   r   r   Úutilsr   Úagent.anthropic_credentialsr	   r
   r   Úagent.anthropic_endpointsr   r   r   r   r   r   r   r   r   r   Úagent.anthropic_message_convertr   r   r   Úagent.errorsr   Úhermes_cli.version_infor   r   r!   r&   r2   Ú	getLoggerrÍ  r1  rŠ  r‰  r~   r‚   r†   r‡   rK   rY   rg   ri   rk   rt   r{   r   rƒ   rˆ   r�   r“   r˜   rå   râ   rä   r¡  r  rÄ   rË   r    rœ   rª   r²   rÈ   rÌ   rn  rY  rÖ   r<  Ú _OAUTH_TOOL_NAME_REVERSE_ALIASESÚ	frozensetÚ_OAUTH_PROSE_ALIAS_NAMESrû   rª  rÙ   rÝ   rè   rí   ró   rù   r  r  r  r'  r8  r@  rS  r[  rg  ro  rÔ   rp  rx  r�  r   r¢  r§  r¬  r°  r²  rÚ  rá  rî  rõ  r.   )r§   rÜ   r÷  s   00@r%   Ú<module>r
     s/  øðö>õ Û Û Û 	Û 	Û Û Ý ß ,Ó ,å *ç kÑ k÷÷ ÷ ÷ñ õ *å 4ð
 €Ó ð 15Ð Ó 4ò÷"	ð 
×	Ò	˜8Ó	$€à˜E 6¨5°(¸DÀ%ÈÐN€ð
 ˆU�E˜5 '¨7°F¸FÀHÈhÐX]Ð_dØˆuðÐ ð-Ð )ð oÐ ð )_Ð %ð (RÐ $õ-õ+ðØ�GðØ.°ðØ9JÈGðà�wðà 1°7ðà<OÐQWðð �vðð  3°Fðð =OÐPVðð �Vð	ð /°ð	ð 9LÈWð	ð
 ˜ðð
 !3°Eðð
 <KÈEðð �uðð /°ðð 8AÀ'È7ÐTZñÐ ð #*Ð õ_õ2÷
õõiõ
õHõõ/ð @Ð Ø2Ð4HÐI€Ø*Ð Ø(€à+Ð-?Ð@Ð ð
 !)Ð Ø,0Ð Ó 0ðÐ ð /Ð õõ(
'õ)õ &ð YÐ ØÐ ð -Ð.CÀXÈÐ_Ð ØKc×KiÑKiÔKkÔ#lÑKk¹¸ I¢OÑKkÒ#lÐ  ñ %Ð&6Ð%7Ó8Ð ÷ $˜eñ $áÐ/Ô0ó$Ÿe™eñ $áÐ/Ô0ó$ó Ð õðÐUZ÷ õ@õ
ò6õñAØot÷Að A÷D#õ2
õñ$Að  @E÷ Að AõBõ.8õð(Ð ð —j’jÐ!UÓVÐ òõ8ð< ˜6 6Ð*¨F°V¸VÐ4DÀjÐSYÐ[`ÐRaÐbÐ ÷AñL #Ò#\Ó]Ð ðÀB÷ õ*.÷ñ õ:
õ
1ò*ðZYØ8:ðYØRVðYàðYà&*÷Yñ Yùóg $ms   ÆJ;