
    iv                       U d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	 ddl
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 dd	lmZ  ej        e          Zd
ai ddddddddddddddddddddddddddddddd dd!d"d#d"iZd$ee         d%efd&Zd'd(d)d)d*d*d+d,d-d.d/d0d1Zeeef         e d2<   d3d4d5Z!eeef         e d6<   d7d8d9d:Z"d;d<giZ#d
a$e%e d=<   d/Z&d/Z'd>Z(d?Z)d@Z*dAZ+ e            dBz  Z,dCZ-dDZ.dEed%efdFZ/d$ed%ee%ee         f         fdGZ0dHed%efdIZ1ddHedKed%efdLZ2dMed%efdNZ3 G dO dP          Z4 G dQ dR          Z5 G dS dT          Z6 G dU dV          Z7 G dW dX          Z8 G dY dZ          Z9 G d[ d\          Z: G d] d^          Z; G d_ d`          Z< G da db          Z= G dc dd          Z> G de df          Z?d%ee@         fdgZAd$e@d%efdhZBd%efdiZCd%ee         fdjZDd%eee         ee         f         fdkZEd%eee         ee         f         fdlZFddme%d%eee         ee         f         fdnZGd%efdoZHd%efdpZId%eee         ee         ee         f         fdqZJd%efdrZKddsZLdEed%dfdtZMd%eee         ee         f         fduZNd%eee         ee         f         fdvZOd%eee         ee         f         fdwZPdxdydzd{d|d}ZQ eRdxdyh          ZSd~ZTdeeeef                  d%eeef         fdZUd%eeV         fdZWdeXd%e%fdZYdeXd%e%fdZZ	 	 ddededed%eee         ee         ef         fdZ[ddeeeef                  d%eee         ee         f         fdZ\defdZ]dee         d$ed%ee         fdZ^	 	 	 	 	 	 	 dd$edede%de%dedededeeeef                  d%eee         ee         f         fdZ_	 ddddedeeeef                  d%eee         ee         f         fdZ`ddddedeeeef                  fdZadZbd$ee         d%efdZcd$ed%eee         ee         f         fdZdd$ed%e%fdZed%ee         fdZf	 	 dddd
dd$ee         dee         dEee         dee         de%d%eee         ee         ee         f         fdZgd%e@fdZhdeid%e@fdZji ZkeeVeVf         e d<    ejl                    ZmdZnddZoded%dfdZpddZqddZrded%e%fdZsdedee         dee         d%ee         fdZt	 	 	 	 	 	 dd$edede%dEedededeeeef                  d%eee         ee         f         fdZu	 	 	 	 	 dded$ededEeded%eeee         ee         ee         ee         f         fdZvdZwewfdedexd%exfdZy eRddh          Zzd$edEed%e%fdZ{de|d%e|fdZ}	 	 	 	 	 	 dd$edede|deex         deei         dee|         dexdee@         dEee         d%e@fdZ~ddeded%efdZ	 dddddddddddd
ded$ededEededeeeef                  de|dexdeide|dexde@d%efdZd%efdZ	 ddddddddddd	ded$ededEedede|dexdeide|dexde@d%efdZdS )a,  Shared auxiliary client router for side tasks.

Provides a single resolution chain so every consumer (context compression,
session search, web extraction, vision analysis, browser vision) picks up
the best available backend without duplicating fallback logic.

Resolution order for text tasks (auto mode):
  1. OpenRouter  (OPENROUTER_API_KEY)
  2. Nous Portal (~/.hermes/auth.json active provider)
  3. Custom endpoint (config.yaml model.base_url + OPENAI_API_KEY)
  4. Codex OAuth (Responses API via chatgpt.com with gpt-5.3-codex,
     wrapped to look like a chat.completions client)
  5. Native Anthropic
  6. Direct API-key providers (z.ai/GLM, Kimi/Moonshot, MiniMax, MiniMax-CN)
  7. None

Resolution order for vision/multimodal tasks (auto mode):
  1. Selected main provider, if it is one of the supported vision backends below
  2. OpenRouter
  3. Nous Portal
  4. Codex OAuth (gpt-5.3-codex supports vision via Responses API)
  5. Native Anthropic
  6. Custom endpoint (for local vision models: Qwen-VL, LLaVA, Pixtral, etc.)
  7. None

Per-task overrides are configured in config.yaml under the ``auxiliary:`` section
(e.g. ``auxiliary.vision.provider``, ``auxiliary.compression.model``).
Default "auto" follows the chains above.

Payment / credit exhaustion fallback:
  When a resolved provider returns HTTP 402 or a credit-related error,
  call_llm() automatically retries with the next available provider in the
  auto-detection chain.  This handles the common case where a user depletes
  their OpenRouter balance but has Codex OAuth or another provider available.
    N)Path)SimpleNamespace)AnyDictListOptionalTuple)OpenAI)	load_pool)get_hermes_home)OPENROUTER_BASE_URLFgooglegeminizgoogle-geminizgoogle-ai-studiozx-aixaizx.aigrokglmzaizz-aizz.aizhipukimikimi-codingmoonshotzkimi-cnkimi-coding-cnzmoonshot-cnzminimax-china
minimax-cn
minimax_cnclaude	anthropiczclaude-codeproviderreturnc                 f   | pd                                                                 }|                    d          r4|                    dd          d                                          }|sdS |}|dk    rdS |dk    rt	                      }|r|d	vr|S dS t
                              ||          S )
Nautocustom::   customcodexopenai-codexmain)r    r'    )striplower
startswithsplit_read_main_provider_PROVIDER_ALIASESget)r   
normalizedsuffix	main_provs       >/home/agentuser/.hermes/hermes-agent/agent/auxiliary_client.py_normalize_aux_providerr4   O   s    $f++--3355JY'' !!#q))!,2244 	8
W~V ())	 	*>>>x  Z888    zgemini-3-flash-previewzglm-4.5-flashzkimi-k2-turbo-previewzMiniMax-M2.7claude-haiku-4-5-20251001zgoogle/gemini-3-flashgemini-3-flashzglm-5zgoogle/gemini-3-flash-previewznemotron-3-nano:30b)r   r   r   r   minimaxr   r   z
ai-gatewayzopencode-zenzopencode-gokilocodezollama-cloud_API_KEY_PROVIDER_AUX_MODELSzmimo-v2-omnizglm-5v-turbo)xiaomir   _PROVIDER_VISION_MODELSz%https://hermes-agent.nousresearch.comzHermes Agentzproductivity,cli-agent)zHTTP-RefererzX-OpenRouter-TitlezX-OpenRouter-Categoriestagsproduct=hermes-agentauxiliary_is_nouszxiaomi/mimo-v2-omnizxiaomi/mimo-v2-proz)https://inference-api.nousresearch.com/v1zhttps://api.anthropic.comz	auth.jsonzgpt-5.2-codexz%https://chatgpt.com/backend-api/codexbase_urlc                    t          | pd                                                              d          }|                    d          r9|dt	          d                    dz   }t
                              d||           |S |S )u  Normalize an Anthropic-style base URL to OpenAI-compatible format.

    Some providers (MiniMax, MiniMax-CN) expose an ``/anthropic`` endpoint for
    the Anthropic Messages API and a separate ``/v1`` endpoint for OpenAI chat
    completions.  The auxiliary client uses the OpenAI SDK, so it must hit the
    ``/v1`` surface.  Passing the raw ``inference_base_url`` causes requests to
    land on ``/anthropic/chat/completions`` — a 404.
    r(   /
/anthropicNz/v1u,   Auxiliary client: rewrote base URL %s → %s)strr)   rstripendswithlenloggerdebug)r@   url	rewrittens      r3   _to_openai_base_urlrL      s     hn"


#
#
%
%
,
,S
1
1C
||L!! ,3|,,,,-5	CS)TTTJr5   c                 R   	 t          |           }n4# t          $ r'}t                              d| |           Y d}~dS d}~ww xY w|r|                                sdS 	 d|                                fS # t          $ r'}t                              d| |           Y d}~dS d}~ww xY w)z2Return (pool_exists_for_provider, selected_entry).z0Auxiliary client: could not load pool for %s: %sN)FNTz8Auxiliary client: could not select pool entry for %s: %s)TN)r   	ExceptionrH   rI   has_credentialsselect)r   poolexcs      r3   _select_pool_entryrS      s    ""   GSVWWW{{{{{  t++-- {T[[]]""   OQY[^___zzzzzs*    
A>AA5 5
B&?B!!B&entryc                     | dS t          | dd           pt          | dd          }t          |pd                                          S )Nr(   runtime_api_keyaccess_token)getattrrD   r)   )rT   keys     r3   _pool_runtime_api_keyrZ      sN    }r %*D
1
1
WWUNTV5W5WCsyb>>!!!r5   r(   fallbackc                 H   | 6t          |pd                                                              d          S t          | dd           p#t          | dd           pt          | dd           p|}t          |pd                                                              d          S )Nr(   rB   runtime_base_urlinference_base_urlr@   )rD   r)   rE   rX   )rT   r[   rJ   s      r3   _pool_runtime_base_urlr_      s    }8>r""((**11#666 	)400 	5.55	5*d++	 	  syb>>!!((---r5   contentc                 8   t          | t                    r| S t          | t                    s| rt          |           ndS g }| D ]R}t          |t                    s|                    dd          }|dk    r-|                    d|                    dd          d           b|dk    r|                    di           }t          |t                    r|                    dd          nt          |          }d|d	}t          |t                    r|                    d
          nd}|r||d
<   |                    |           |dv r|                    |           "|                    dd          }|r|                    d|d           T|pdS )a  Convert chat.completions content to Responses API format.

    chat.completions uses:
      {"type": "text", "text": "..."}
      {"type": "image_url", "image_url": {"url": "data:image/png;base64,..."}}

    Responses API uses:
      {"type": "input_text", "text": "..."}
      {"type": "input_image", "image_url": "data:image/png;base64,..."}

    If content is a plain string, it's returned as-is (the Responses API
    accepts strings directly for text-only messages).
    r(   typetext
input_textrb   rc   	image_urlrJ   input_image)rb   rf   detailN)rd   rg   )
isinstancerD   listdictr/   append)	r`   	convertedpartptype
image_datarJ   rT   rh   rc   s	            r3   _convert_content_for_responsesrq      s    '3 gt$$ /&.s7|||B.&(I G G$%% 	$$F??lDHHVR<P<PQQRRRRk!!+r22J/9*d/K/K`*..+++QTU_Q`Q`C-:$M$ME1;J1M1MWZ^^H---SWF )"(hU####333T"""" 88FB''D G  ,!E!EFFF?r5   c                   .    e Zd ZdZdedefdZdefdZdS )_CodexCompletionsAdapterzyDrop-in shim that accepts chat.completions.create() kwargs and
    routes them through the Codex Responses streaming API.real_clientmodelc                 "    || _         || _        d S N)_client_model)selfrt   ru   s      r3   __init__z!_CodexCompletionsAdapter.__init__  s    "r5   r   c                 	   |                     dg           }|                     d| j                  }d}g }|D ]}|                     dd          }|                     d          pd}|dk    r't          |t                    r|nt          |          }\|                    |t          |          d	           |||pddd	gd
d}	|                     d          }
|
rg }|
D ]}t          |t                    r|                     di           ni }|                     d          }|sG|                    d||                     dd          |                     di           d           |r||	d<   g }g }d }	 g }g }d
} | j        j        j	        d:i |	5 }|D ]u}t          |dd          }|dk    r)t          |dd           }||                    |           Bd|v r)t          |dd          }|r|                    |           od|v rd}v|                                }d d d            n# 1 swxY w Y   t          |dd           }t          |t                    r|s|r=t          |          |_        t                              dt!          |                     nx|rv|std                    |          }t%          dddt%          d|          g           g|_        t                              d!t!          |          t!          |                     d;d"t&          d#t          d$t&          d%t&          fd&}t          |dg           D ]} ||d          }|dk    rB ||d          pg D ]2}  || d          }!|!d'v r |                     || d(d                     3V|dk    rU|                    t%           ||d)d          dt%           ||dd           ||d*d+          ,          -                     t          |d.d           }"|"r?t%          t          |"d/d0          t          |"d1d0          t          |"d2d0          3          }n.# t(          $ r!}#t                              d4|#            d }#~#ww xY wd                    |                                          pd }t%          d||pd 5          }$t%          d0|$|sd6nd78          }%t%          |%g||9          S )<Nmessagesru   zYou are a helpful assistant.roleuserr`   r(   system)r~   r`   F)ru   instructionsinputstoretoolsfunctionnamedescription
parameters)rb   r   r   r   rb   zresponse.output_item.doneitemzoutput_text.deltadeltafunction_callToutputz>Codex auxiliary: backfilled %d output items from stream eventsmessage	assistant	completedoutput_textre   )rb   r~   statusr`   z6Codex auxiliary: synthesized from %d deltas (%d chars)objrY   defaultr   c                     t          | |d           }|+t          | t                    r|                     ||          }||n|S rw   )rX   ri   rk   r/   )r   rY   r   vals       r3   	_item_getz2_CodexCompletionsAdapter.create.<locals>._item_getv  sG    c3--;:c4#8#8;''#w//C!oss7:r5   )r   rc   rc   call_id	argumentsz{})r   r   )idrb   r   usageinput_tokensr   output_tokenstotal_tokensprompt_tokenscompletion_tokensr   z-Codex auxiliary Responses API call failed: %s)r~   r`   
tool_callsstopr   indexr   finish_reasonchoicesru   r    rw   )r/   ry   ri   rD   rl   rq   rk   rx   	responsesstreamrX   get_final_responserj   r   rH   rI   rG   joinr   r   rN   r)   )&rz   kwargsr}   ru   r   
input_msgsmsgr~   r`   resp_kwargsr   rm   tfnr   
text_partstool_calls_rawr   collected_output_itemscollected_text_deltashas_function_callsr   _event_etype_done_deltafinal_output	assembledr   r   	item_typern   ro   
resp_usagerR   r   choices&                                         r3   createz_CodexCompletionsAdapter.create  s   ::j"--

7DK00
 6+-
 		 		C7766**Dggi((.BGx*4Wc*B*BTwwG!! =gFF# #     (DV$C$C#D	'
 '
 

7## 	1I 
 
.8D.A.AIQUU:r***rvvf~~   & #%66-#<#<"$&&r":":	" "      1'0G$ !#
$&P	 13"/1!!&.'.==== 4$ 2 2F$VVR88F!<<< ' = = ,299%@@@,66!("!=!=! A188@@@(F22-1*11334 4 4 4 4 4 4 4 4 4 4 4 4 4 4  eXt44G'4((  ) #'(>#?#?ELLLX233    + 3E  !#(= > >I$3&[!0m)!T!T!T U% % % $EL LLP122C	NN  ; ;s ; ;s ;c ; ; ; ;  x44  %IdF33		))!*4!;!;!Ar K K )	$ 7 7 $;;;&--iifb.I.IJJJK /11"))/$9T9b99'!0!*4!<!<&/ik4&H&H" " "+ + +    !66J '")*na"H"H&-j/1&M&M!(^Q!G!G  
  	 	 	LLH#NNN	 ''*%%++--5 "%-
 
 

 !(6H&&L
 
 

 H
 
 
 	
s>   :Q BH0$Q 0H44Q 7H48HQ 
Q<Q77Q<N)	__name__
__module____qualname____doc__r
   rD   r{   r   r   r   r5   r3   rs   rs   	  sb        > >F 3    X
# X
 X
 X
 X
 X
 X
r5   rs   c                       e Zd ZdZdefdZdS )_CodexChatShimz>Wraps the adapter to provide client.chat.completions.create().adapterc                     || _         d S rw   completionsrz   r   s     r3   r{   z_CodexChatShim.__init__      "r5   N)r   r   r   r   rs   r{   r   r5   r3   r   r     s6        HH# 8 # # # # # #r5   r   c                   (    e Zd ZdZdedefdZd ZdS )CodexAuxiliaryClientzOpenAI-client-compatible wrapper that routes through Codex Responses API.

    Consumers can call client.chat.completions.create(**kwargs) as normal.
    Also exposes .api_key and .base_url for introspection by async wrappers.
    rt   ru   c                     || _         t          ||          }t          |          | _        |j        | _        |j        | _        d S rw   )_real_clientrs   r   chatapi_keyr@   )rz   rt   ru   r   s       r3   r{   zCodexAuxiliaryClient.__init__  s@    '*;>>"7++	"*#,r5   c                 8    | j                                          d S rw   )r   closerz   s    r3   r   zCodexAuxiliaryClient.close  s    !!!!!r5   N)r   r   r   r   r
   rD   r{   r   r   r5   r3   r   r     sO         -F -3 - - - -" " " " "r5   r   c                   *    e Zd ZdZdefdZdefdZdS )_AsyncCodexCompletionsAdapterzAsync version of the Codex Responses adapter.

    Wraps the sync adapter via asyncio.to_thread() so async consumers
    (web_tools, session_search) can await it as normal.
    sync_adapterc                     || _         d S rw   _syncrz   r   s     r3   r{   z&_AsyncCodexCompletionsAdapter.__init__      !


r5   r   c                 J   K   dd l } |j        | j        j        fi | d {V S Nr   asyncio	to_threadr   r   rz   r   r   s      r3   r   z$_AsyncCodexCompletionsAdapter.create  A      &W&tz'8CCFCCCCCCCCCr5   N)r   r   r   r   rs   r{   r   r   r   r5   r3   r   r     s\         "%= " " " "D D D D D D Dr5   r   c                       e Zd ZdefdZdS )_AsyncCodexChatShimr   c                     || _         d S rw   r   r   s     r3   r{   z_AsyncCodexChatShim.__init__  r   r5   N)r   r   r   r   r{   r   r5   r3   r   r     s0        # = # # # # # #r5   r   c                       e Zd ZdZddZdS )AsyncCodexAuxiliaryClientzHAsync-compatible wrapper matching AsyncOpenAI.chat.completions.create().sync_wrapperr   c                     |j         j        }t          |          }t          |          | _         |j        | _        |j        | _        d S rw   )r   r   r   r   r   r@   rz   r   r   async_adapters       r3   r{   z"AsyncCodexAuxiliaryClient.__init__  sA    #(45lCC'66	#+$-r5   N)r   r   )r   r   r   r   r{   r   r5   r3   r   r     s.        RR. . . . . .r5   r   c                   4    e Zd ZdZd
dededefdZdefdZd	S )_AnthropicCompletionsAdapterz<OpenAI-client-compatible adapter for Anthropic Messages API.Frt   ru   is_oauthc                 0    || _         || _        || _        d S rw   )rx   ry   	_is_oauth)rz   rt   ru   r   s       r3   r{   z%_AnthropicCompletionsAdapter.__init__  s    "!r5   r   c           	      \   ddl m}m} |                    dg           }|                    d| j                  }|                    d          }|                    d          }|                    d          p|                    d          pd	}|                    d
          }	d }
t          |t                    r|}
nt          |t                    rkt          |                    dd                                                    }|dk    r*|                    di                               d          }
n|dv r|}
 |||||d |
| j	                  }|	ddl m
}  ||          s|	|d
<    | j        j        j        di |} ||          \  }}d }t          |d          rd|j        r]t!          |j        dd          pd}t!          |j        dd          pd}t!          |j        dd          p||z   }t#          |||          }t#          d||          }t#          |g||          S )Nr   )build_anthropic_kwargsnormalize_anthropic_responser}   ru   r   tool_choice
max_tokensmax_completion_tokensi  temperaturerb   r(   r   r   >   r    nonerequired)ru   r}   r   r   reasoning_configr   r   _forbids_sampling_paramsr   r   r   r   r   r   r   r   )agent.anthropic_adapterr   r   r/   ry   ri   rD   rk   r*   r   r   rx   r}   r   hasattrr   rX   r   )rz   r   r   r   r}   ru   r   r   r   r   normalized_tool_choicechoice_typeanthropic_kwargsr   responseassistant_messager   r   r   r   r   r   s                         r3   r   z#_AnthropicCompletionsAdapter.create  s   ````````::j"--

7DK00

7##jj//ZZ--\<S1T1T\X\
jj//!%k3'' 	5%0""T** 	5koofb99::@@BBKj(()4R)H)H)L)LV)T)T&& <<<)4&11!!.^
 
 
 "HHHHHH++E22 >2= //4<(/CC2BCC+G+G+Q+Q(=8W%% 	(. 	#HNNAFFK!M ' K K Pq"8>>1EEl-ZkJkL#+"3)  E !%'
 
 

 H
 
 
 	
r5   NF)	r   r   r   r   r   rD   boolr{   r   r   r5   r3   r   r     sc        FF" "C " "t " " " "
<
# <
 <
 <
 <
 <
 <
r5   r   c                       e Zd ZdefdZdS )_AnthropicChatShimr   c                     || _         d S rw   r   r   s     r3   r{   z_AnthropicChatShim.__init__,  r   r5   N)r   r   r   r   r{   r   r5   r3   r  r  +  s0        # < # # # # # #r5   r  c                   6    e Zd ZdZddededededef
dZd	 Zd
S )AnthropicAuxiliaryClientz@OpenAI-client-compatible wrapper over a native Anthropic client.Frt   ru   r   r@   r   c                 |    || _         t          |||          }t          |          | _        || _        || _        d S )Nr   )r   r   r  r   r   r@   )rz   rt   ru   r   r@   r   r   s          r3   r{   z!AnthropicAuxiliaryClient.__init__3  s?    '.{EHUUU&w//	 r5   c                 h    t          | j        dd           }t          |          r |             d S d S )Nr   )rX   r   callable)rz   close_fns     r3   r   zAnthropicAuxiliaryClient.close:  s?    4,gt<<H 	HJJJJJ	 	r5   Nr  )	r   r   r   r   r   rD   r	  r{   r   r   r5   r3   r  r  0  sf        JJ! !C ! !c !S !\` ! ! ! !    r5   r  c                   &    e Zd ZdefdZdefdZdS )!_AsyncAnthropicCompletionsAdapterr   c                     || _         d S rw   r   r   s     r3   r{   z*_AsyncAnthropicCompletionsAdapter.__init__A  r   r5   r   c                 J   K   dd l } |j        | j        j        fi | d {V S r   r   r   s      r3   r   z(_AsyncAnthropicCompletionsAdapter.createD  r   r5   N)r   r   r   r   r{   r   r   r   r5   r3   r  r  @  sR        "%A " " " "D D D D D D Dr5   r  c                       e Zd ZdefdZdS )_AsyncAnthropicChatShimr   c                     || _         d S rw   r   r   s     r3   r{   z _AsyncAnthropicChatShim.__init__J  r   r5   N)r   r   r   r  r{   r   r5   r3   r  r  I  s0        # A # # # # # #r5   r  c                       e Zd ZddZdS )AsyncAnthropicAuxiliaryClientr   r  c                     |j         j        }t          |          }t          |          | _         |j        | _        |j        | _        d S rw   )r   r   r  r  r   r@   r   s       r3   r{   z&AsyncAnthropicAuxiliaryClient.__init__O  sA    #(49,GG+M::	#+$-r5   N)r   r  )r   r   r   r{   r   r5   r3   r  r  N  s(        . . . . . .r5   r  c                     t          d          \  } }| r|dS t          |dd          t          |dd          t          |dd          t          |t                    t          |dd          t          |dd          t          |d	d          t          |d
d          dd	S 	 t                                          sdS t          j        t                                                    }|	                    d          dk    rdS |	                    di           	                    di           }|	                    d          s|	                    d          sdS |S # t          $ r&}t                              d|           Y d}~dS d}~ww xY w)zRead and validate ~/.hermes/auth.json for an active Nous provider.

    Returns the provider state dict if Nous is active with tokens,
    otherwise None.
    nousNrW   r(   refresh_token	agent_keyportal_base_url	client_idscope
token_typeBearerrQ   )	rW   r   r!  r^   r"  r#  r$  r%  sourceactive_provider	providerszCould not read Nous auth: %s)rS   rX   r_   _NOUS_DEFAULT_BASE_URL_AUTH_JSON_PATHis_filejsonloads	read_textr/   rN   rH   rI   )pool_presentrT   datar   rR   s        r3   _read_nous_authr2  W  s    -V44L% 
=4#E>2>>$UOTBB T::"8@V"W"W&u.?FF T::UGT22!%x@@

 

 
	
&&(( 	4z/33556688%&&&00488K,,00<<||K(( 	n1M1M 	4   3S999ttttts,   "E =AE AE E 
F%FFc                 X    |                      d          p|                      dd          S )z9Extract the best API key from a Nous provider state dict.r!  rW   r(   r/   r   s    r3   _nous_api_keyr6  }  s'    <<$$H^R(H(HHr5   c                  6    t          j        dt                    S )z8Resolve the Nous inference base URL from env or default.NOUS_INFERENCE_BASE_URL)osgetenvr*  r   r5   r3   _nous_base_urlr;    s    9.0FGGGr5   c                  &   t          d          \  } }| rt          |          }|r|S 	 ddlm}  |            }|                    di           }|                    d          }t          |t                    r|                                sdS 	 ddl}|	                    d          d         }|d	t          |           d
z  z  z  }t          j        |                    |                    }	|	                    dd          }
|
r4t          j                    |
k    rt                              d|
           dS n# t"          $ r Y nw xY w|                                S # t"          $ r&}t                              d|           Y d}~dS d}~ww xY w)a  Read a valid, non-expired Codex OAuth access token from Hermes auth store.

    If a credential pool exists but currently has no selectable runtime entry
    (for example all pool slots are marked exhausted), fall back to the
    profile's auth.json token instead of hard-failing. This keeps explicit
    fallback-to-Codex working when the pool state is stale but the stored OAuth
    token is still valid.
    r&   r   )_read_codex_tokenstokensrW   N.r#   =   expz-Codex access token expired (exp=%s), skippingz2Could not read Codex auth for auxiliary client: %s)rS   rZ   hermes_cli.authr=  r/   ri   rD   r)   base64r,   rG   r-  r.  urlsafe_b64decodetimerH   rI   rN   )r0  rT   tokenr=  r1  r>  rW   rD  payloadclaimsrB  rR   s               r3   _read_codex_access_tokenrJ    s    -^<<L% %e,, 	L666666!!##(B''zz.11,,, 	L4F4F4H4H 	4
	MMM"((--a0Gss7||ma/00GZ 8 8 A ABBF**UA&&C ty{{S((LcRRRt 	 	 	D	 !!###   I3OOOttttts=   A$E  B)D< ;E  <
E	E  E		E   
F*FFc            	      x   	 ddl m} m} n+# t          $ r t                              d           Y dS w xY w|                                 D ]n\  }}|j        dk    r|dk    r4	 ddl m}  |d          s+n# t          $ r Y nw xY wt                      c S t          |          \  }}|rt          |          }|srt          t          ||j                  p|j                  }t                              |          }	|	t                              d	|j        |	           i }
d
|                                v rddi|
d<   n)d|                                v rddlm}  |            |
d<   t+          d||d|
|	fc S  ||          }t-          |                    dd                                                    }|syt          t-          |                    dd                                                                        d          p|j                  }t                              |          }	|	t                              d|j        |	           i }
d
|                                v rddi|
d<   n)d|                                v rddlm}  |            |
d<   t+          d||d|
|	fc S dS )zTry each API-key provider in PROVIDER_REGISTRY order.

    Returns (client, model) for the first provider with usable runtime
    credentials, or (None, None) if none are configured.
    r   )PROVIDER_REGISTRY$resolve_api_key_provider_credentialsz7Could not import PROVIDER_REGISTRY for API-key fallbackNNr   r   )!is_provider_explicitly_configuredNz'Auxiliary text client: %s (%s) via poolapi.kimi.com
User-AgentKimiCLI/1.30.0default_headersapi.githubcopilot.comcopilot_default_headersr   r@   r(   r@   rB   zAuxiliary text client: %s (%s)r   )rC  rL  rM  ImportErrorrH   rI   items	auth_typerO  _try_anthropicrS   rZ   rL   r_   r^   r:   r/   r   r*   hermes_cli.modelsrV  r
   rD   r)   rE   )rL  rM  provider_idpconfigrO  r0  rT   r   r@   ru   extrarV  credss                r3   _resolve_api_key_providerra    sq   [[[[[[[[[   NOOOzz !2 7 7 9 9 8J 8JW	))+%%MMMMMM88EE    !#####0==e 	N+E22G *&ug.HIIgWMg H 144[AAE}LLBGLRWXXXE!1!111,8:J+K'(((HNN,<,<<<EEEEEE+B+B+D+D'(F'HFFFFMMMM44[AAeii	2..//5577 	&		*b))**002299#>>\'B\
 
 -00===5w|UKKKX^^----(46F'GE#$$$(8(888AAAAAA'>'>'@'@E#$BgBBEBBEIIII:s    $33#A66
BBc                     t          d          \  } }| rgt          |          }|sdS t          |t                    pt          }t                              d           t          ||t                    t          fS t          j
        d          }|sdS t                              d           t          |t          t                    t          fS )N
openrouterrN  z%Auxiliary client: OpenRouter via pool)r   r@   rS  OPENROUTER_API_KEYzAuxiliary client: OpenRouter)rS   rZ   r_   r   rH   rI   r
   _OR_HEADERS_OPENROUTER_MODELr9  r:  )r0  rT   or_keyr@   s       r3   _try_openrouterrh    s    ,\::L% G&u-- 	:)%1DEE\I\<===fx'24 4 45FG 	G Y+,,F z
LL/000&+>#.0 0 01BC Cr5   visionc                    	 ddl m}  |            }|#|dk    rt                              d|           dS n# t          $ r Y nw xY wt                      }|sdS dat                              d           |                    d          dk    rd	}nt          }	 dd
l	m
}  |            r0| rt          nt          }t                              d|| rdnd           n# t          $ r Y nw xY wt          t          |          t          |                    d          pt!                                                    d                    |fS )Nr   )nous_rate_limit_remainingz?Auxiliary: skipping Nous Portal (rate-limited, resets in %.0fs)rN  TzAuxiliary client: Nous Portalr'  rQ   r7   )check_nous_free_tieru4   Free-tier Nous account — using %s for auxiliary/%sri  rc   r^   rB   rW  )agent.nous_rate_guardrk  rH   rI   rN   r2  r?   r/   _NOUS_MODELr\  rl  _NOUS_FREE_TIER_VISION_MODEL_NOUS_FREE_TIER_AUX_MODELr
   r6  rD   r;  rE   )ri  rk  
_remainingr  ru   rl  s         r3   	_try_nousrr    s   
CCCCCC..00
!j1nnLLQ   :    D z
LL0111xxV## ::::::!! 	@4:Y00@YELLOF >@ @ @    	!$''"677K>;K;KLLSSTWXX	
 	
 	
 	 s"   38 
AAA C 
C('C(c                     	 ddl m}   |             }|                    di           }t          |t                    r(|                                r|                                S t          |t                    rS|                    dd          }t          |t                    r(|                                r|                                S n# t          $ r Y nw xY wdS )zRead the user's configured main model from config.yaml.

    config.yaml model.default is the single source of truth for the active
    model. Environment variables are no longer consulted.
    r   load_configru   r   r(   )hermes_cli.configru  r/   ri   rD   r)   rk   rN   )ru  cfg	model_cfgr   s       r3   _read_main_modelry  ?  s    111111kmmGGGR((	i%% 	%)//*;*; 	%??$$$i&& 	'mmIr22G'3'' 'GMMOO '}}&   2s   A"C %A'C 
CCc                  j   	 ddl m}   |             }|                    di           }t          |t                    re|                    dd          }t          |t
                    r:|                                r&|                                                                S n# t          $ r Y nw xY wdS )zRead the user's configured main provider from config.yaml.

    Returns the lowercase provider id (e.g. "alibaba", "openrouter") or ""
    if not configured.
    r   rt  ru   r   r(   )	rv  ru  r/   ri   rk   rD   r)   r*   rN   )ru  rw  rx  r   s       r3   r-   r-   T  s    	111111kmmGGGR((	i&& 	0 }}Z44H(C(( 0X^^-=-= 0~~''--///   2s   BB# #
B0/B0c                     	 ddl m}   | d          }n4# t          $ r'}t                              d|           d}Y d}~nd}~ww xY wt          |t                    sjt          j        dd          	                                
                    d	          }t          j        d
d          	                                }|sdS ||d}|                    d          }|                    d          }|                    d          }t          |t                    r|	                                sdS |	                                
                    d	          }d|                                v rdS t          |t                    r|	                                sd}t          |t                    r|	                                sd}||	                                |fS )zResolve the active custom/main endpoint the same way the main CLI does.

    This covers both env-driven OPENAI_BASE_URL setups and config-saved custom
    endpoints where the base URL lives in config.yaml instead of the live
    environment.
    r   )resolve_runtime_providerr$   )	requestedz6Auxiliary client: custom runtime resolution failed: %sNOPENAI_BASE_URLr(   rB   OPENAI_API_KEYNNN)r@   r   r@   r   api_modezopenrouter.aino-key-required)hermes_cli.runtime_providerr|  rN   rH   rI   ri   rk   r9  r:  r)   rE   r/   rD   r*   )r|  runtimerR   openai_base
openai_keycustom_base
custom_keycustom_modes           r3   _resolve_custom_runtimer  g  s   HHHHHH**X>>>   MsSSS gt$$ 
i 1266<<>>EEcJJY/44::<<
 	$###!
 

 ++j))KY''J++j))Kk3''  {/@/@/B/B  ##%%,,S11K+++----   j#&& 'j.>.>.@.@ '&
k3'' {/@/@/B/B 
((**K77s    
AAAc                  .    t                      \  } }}| pdS )Nr(   )r  )r  _s     r3   _current_custom_base_urlr    s    /11KA"r5   c            	      (   ddl m}  dD ]}t          t          j                            |          pd                                          }|sE	  | |          }|j        r|j        }`# t          $ r}t          d| d|d          |d}~ww xY wdS )	a  Fail fast with a clear error when proxy env vars have malformed URLs.

    Common cause: shell config (e.g. .zshrc) with a typo like
    ``export HTTP_PROXY=http://127.0.0.1:6153export NEXT_VAR=...``
    which concatenates 'export' into the port number.  Without this
    check the OpenAI/httpx client raises a cryptic ``Invalid port``
    error that doesn't name the offending env var.
    r   urlparse)HTTPS_PROXY
HTTP_PROXY	ALL_PROXYhttps_proxy
http_proxy	all_proxyr(   z%Malformed proxy environment variable r@  z1. Fix or unset your proxy settings and try again.N)urllib.parser  rD   r9  environr/   r)   schemeport
ValueErrorRuntimeError)r  rY   valueparsedr  rR   s         r3   _validate_proxy_env_urlsr    s     &%%%%%:  BJNN3''-2..4466 		Xe__F}  K 	 	 	B B Be B B B  	 s   A))
B3B

Bc                    ddl m} t          | pd                                          }|r|                    d          rdS 	  ||          }|j        dv r	|j        }dS dS # t          $ r}t          d|d          |d}~ww xY w)	zEReject obviously broken custom endpoint URLs before they reach httpx.r   r  r(   zacp://N>   httphttpszMalformed custom endpoint URL: zJ. Run `hermes setup` or `hermes model` and enter a valid http(s) base URL.)	r  r  rD   r)   r+   r  r  r  r  )r@   r  	candidater  r  rR   s         r3   _validate_base_urlr    s    %%%%%%HN##))++I 	,,X66 )$$=---AAA .-   Wi W W W
 
 	s   A# #
B-BBc                     t                      } t          |           dk    r| \  }}d }n| \  }}}|r|sdS |                                                    t                                                    rdS t                      pd}t                              d||pd           |dk    r#t          ||          }t          ||          |fS t          ||          |fS )N   rN  gpt-4o-miniz3Auxiliary client: custom endpoint (%s, api_mode=%s)chat_completionscodex_responsesrW  )
r  rG   r*   r+   _CODEX_AUX_BASE_URLry  rH   rI   r
   r   )r  r  r  r  ru   rt   s         r3   _try_custom_endpointr    s    %''G
7||q")Z/6,Z j z%%&9&?&?&A&ABB z/-E
LLF{Op^pqqq'''Z+FFF#K77>>*{;;;UBBr5   c                     t          d          \  } }| rHt          |          }|rt          |t                    pt          }n3t	                      }|sdS t          }nt	                      }|sdS t          }t
                              dt                     t          ||          }t          |t                    t          fS )Nr&   rN  z4Auxiliary client: Codex OAuth (%s via Responses API)rW  )
rS   rZ   r_   r  rJ  rH   rI   _CODEX_AUX_MODELr
   r   )r0  rT   codex_tokenr@   rt   s        r3   
_try_codexr    s    ,^<<L% '+E22 	+-e5HII`M`HH244K "!z*HH.00 	:&
LLGIYZZZx@@@K-=>>@PPPr5   c                     	 ddl m} m} n# t          $ r Y dS w xY wt	          d          \  }}|r|dS t          |          }nd } |            }|sdS |rt          |t                    nt          }	 ddlm	}  |            }|
                    d          }t          |t                    rt          |
                    d          pd                                                                          }	|	dk    r@|
                    d	          pd                                                    d
          }
|
r|
}n# t"          $ r Y nw xY wddl m}  ||          }t&          
                    dd          }t(                              d|||           	  | ||          }n# t          $ r Y dS w xY wt-          |||||          |fS )Nr   )build_anthropic_clientresolve_anthropic_tokenrN  r   rt  ru   r   r(   r@   rB   )_is_oauth_tokenr6   z8Auxiliary client: Anthropic native (%s) at %s (oauth=%s)r  )r  r  r  rX  rS   rZ   r_   _ANTHROPIC_DEFAULT_BASE_URLrv  ru  r/   ri   rk   rD   r)   r*   rE   rN   r  r:   rH   rI   r  )r  r  r0  rT   rG  r@   ru  rw  rx  cfg_providercfg_base_urlr  r   ru   rt   s                  r3   r[  r[    sQ   [[[[[[[[[   zz -[99L% *=:%e,,'')) z
 NZz%e-HIII_zH111111kmmGGG$$	i&& 	,y}}Z88>B??EEGGMMOOL{** )j 9 9 ?RFFHHOOPSTT ,+H    877777u%%H(,,[:UVVE
LLKUT\^fggg,,UH==    zz	
 $KxRZ[[[]bbbs1    
3CD< <
E	E	F# #
F10F1rc  r  local/customr&   api-keyrh  rr  r  r  ra  )r   ru   r@   r   r  main_runtimec                 R   t          | t                    si S i }t          D ]W}|                     |          }t          |t                    r+|                                r|                                ||<   X|                    d          }|r|                                |d<   |S )z8Return a sanitized copy of a live main-runtime override.r   )ri   rk   _MAIN_RUNTIME_FIELDSr/   rD   r)   r*   )r  r0   fieldr  r   s        r3   _normalize_main_runtimer  0  s    lD)) 	!#J% . .  ''eS!! 	.ekkmm 	. %Ju~~j))H 2!)!1!1
:r5   c                  V    dt           fdt          fdt          fdt          fdt          fgS )zReturn the ordered provider detection chain.

    Built at call time (not module level) so that test patches
    on the ``_try_*`` functions are picked up correctly.
    rc  r  r  r&   r  r  r   r5   r3   _get_provider_chainr  ?  s8     
'		-.	$	-. r5   rR   c                     t          | dd          }|dk    rdS t          |                                           |dv rt          fddD                       rdS dS )	zDetect payment/credit/quota exhaustion errors.

    Returns True for HTTP 402 (Payment Required) and for 429/other errors
    whose message indicates billing exhaustion rather than rate limiting.
    status_codeN  T)r  i  Nc              3       K   | ]}|v V  	d S rw   r   .0kw	err_lowers     r3   	<genexpr>z$_is_payment_error.<locals>.<genexpr>[  s?       @ @2rY @ @ @ @ @ @r5   )creditszinsufficient fundszcan only affordbillingzpayment requiredF)rX   rD   r*   any)rR   r   r  s     @r3   _is_payment_errorr  N  s     S-..F}}tC  I !!! @ @ @ @ *? @ @ @ @ @ 	 45r5   c                     ddl m}m} t          | ||f          rdS t	          |           j        t          fddD                       rdS t          |                                           t          fddD                       rdS dS )	aE  Detect connection/network errors that warrant provider fallback.

    Returns True for errors indicating the provider endpoint is unreachable
    (DNS failure, connection refused, TLS errors, timeouts).  These are
    distinct from API errors (4xx/5xx) which indicate the provider IS
    reachable but returned an error.
    r   )APIConnectionErrorAPITimeoutErrorTc              3       K   | ]}|v V  	d S rw   r   )r  r  err_types     r3   r  z'_is_connection_error.<locals>.<genexpr>p  s'      
L
Lb2>
L
L
L
L
L
Lr5   )
ConnectionTimeoutDNSSSLc              3       K   | ]}|v V  	d S rw   r   r  s     r3   r  z'_is_connection_error.<locals>.<genexpr>s  s7        r2?      r5   )zconnection refusedzname or service not knownzno route to hostznetwork is unreachablez	timed outzconnection resetF)	openair  r  ri   rb   r   r  rD   r*   )rR   r  r  r  r  s      @@r3   _is_connection_errorr  b  s     ;:::::::#*O<== tCyy!H

L
L
L
L$K
L
L
LLL tC  I
     &      
 t5r5   payment errorfailed_providertaskreasonc           	      Z   |                                                                  }t                      }|h}|r=|                                 |v r'|                    |                                            dddddddfd|D             }g }t	                      D ]X\  }}	||v r
 |	            \  }
}|
*t
                              d|pd	|| ||pd
           |
||fc S |                    |           Yt
                              d|pd	|| d	                    |                     dS )zTry alternative providers after a payment/credit or connection error.

    Iterates the standard auto-detection chain, skipping the provider that
    failed.

    Returns:
        (client, model, provider_label) or (None, None, "") if no fallback.
    rc  r  r&   r  )rc  r  r&   r%   r$   r  c                 <    h | ]}                     ||          S r   r4  )r  s_alias_to_labels     r3   	<setcomp>z(_try_payment_fallback.<locals>.<setcomp>  s)    HHHq,,Q22HHHr5   Nu2   Auxiliary %s: %s on %s — falling back to %s (%s)callr   z<Auxiliary %s: %s on %s and no fallback available (tried: %s), )NNr(   )
r*   r)   r-   addr  rH   inforl   warningr   )r  r  r  skipmain_providerskip_labelsskip_chain_labelstriedlabeltry_fnclientru   r  s               @r3   _try_payment_fallbackr  |  s~      ""((**D ())M&K /,,..$66++--...%16'5!/Q QO IHHHKHHHE,.. 
 
v%%%KKD@R   5%''''U
NNF51A1A   >r5   c           	         da t          |           }|                    dd          }|                    dd          }|                    dd          }|                    dd          }|                    dd          }t          stt	          j        dd                                          }|pt                      }|r;|r9|d	k    r3|                    d
          st          
                    d||           da|pt                      }	|pt                      }
|	ry|
rw|	t          vrn|	dvrj|	}d}d}|r#|	d	k    s|	                    d
          rd	}|}|pd}t          ||
|||pd          \  }}|$t                              d|	|p|
           ||p|
fS g }t                      D ]\  }} |            \  }}|Y|r3t                              d||pdd                    |                     nt                              d||pd           ||fc S |                    |           t          
                    dd                    |                     dS )u  Full auto-detection chain.

    Priority:
      1. If the user's main provider is NOT an aggregator (OpenRouter / Nous),
         use their main provider + main model directly.  This ensures users on
         Alibaba, DeepSeek, ZAI, etc. get auxiliary tasks handled by the same
         provider they already have credentials for — no OpenRouter key needed.
      2. OpenRouter → Nous → custom → Codex → API-key providers (original chain).
    Fr   r(   ru   r@   r   r  r~  r$   r!   zOPENAI_BASE_URL is set (%s) but model.provider is '%s'. Auxiliary clients may route to the wrong endpoint. Run: hermes model to reconfigure, or remove OPENAI_BASE_URL from ~/.hermes/.envTr    r(   N)explicit_base_urlexplicit_api_keyr  z2Auxiliary auto-detect: using main provider %s (%s)u4   Auxiliary auto-detect: using %s (%s) — skipped: %sr   r  z$Auxiliary auto-detect: using %s (%s)zAuxiliary auto-detect: no provider available (tried: %s). Compression, summarization, and memory flush will not work. Set OPENROUTER_API_KEY or configure a local model in config.yaml.rN  )r?   r  r/   _stale_base_url_warnedr9  r:  r)   r-   r+   rH   r  ry  _AGGREGATOR_PROVIDERSresolve_provider_clientr  r  r   rl   )r  r  runtime_providerruntime_modelr]   rV   runtime_api_mode	_env_base_cfg_providerr  
main_modelresolved_providerr  r  r  resolvedr  r  r  ru   s                       r3   _resolve_autor     s    %l33G{{:r22KK,,M{{:r22kk)R00O{{:r22 " *I/44::<<	(A,?,A,A 
	*- 
	*!X--%00;; .NN6 =   &*" %=(;(=(=M4"2"4"4J 2* 2%:::\11)  	7(!:!:m>V>VW`>a>a!: ( 0.6$2/-%-
 
 
 KKL%x'=:? ? ?81z11 E,.. 	 	v _R!5#5Ityy7G7GI I I I BE5K]T]^^^5=   U
NN W 99U##% % % :r5   ru   c                    ddl m} t          | t                    rt	          |           |fS t          | t
                    rt          |           |fS 	 ddlm} t          | |          r| |fS n# t          $ r Y nw xY w| j
        t          | j                  d}t          | j                                                  }d|v rt          t                    |d<   n#d|v rddlm}  |            |d<   nd	|v rd
di|d<    |di ||fS )zIConvert a sync client to its async counterpart, preserving Codex routing.r   )AsyncOpenAICopilotACPClientrW  rc  rS  rT  rU  rP  rQ  rR  r   )r  r  ri   r   r   r  r  agent.copilot_acp_clientr  rX  r   rD   r@   r*   rk   re  r\  rV  )sync_clientru   r  r  async_kwargs
base_lowerrV  s          r3   _to_async_clientr	    s   """"""+344 =(55u<<+788 A,[995@@======k#344 	&%%	&    &,-- L [)**0022Jz!!*.{*;*;&''	 J	.	.======*A*A*C*C&''	:	%	%+79I*J&';&&&&--s   A/ /
A<;A<
model_namec                 T    | s| S 	 ddl m}  || |          S # t          $ r | cY S w xY w)zANormalize a resolved model for the provider that will receive it.r   )normalize_model_for_provider)hermes_cli.model_normalizer  rN   )r
  r   r  s      r3   _normalize_resolved_modelr  '  sa     KKKKKK++JAAA   s    ''
async_mode	raw_codexr  r  r  c                   & t                       t          |           } dt          dt          dt          ffd&dAdt          dt          f&fd}| dk    r[t	          |	          \  }	}
|	dS |r(d|v r$|
r"d|
vrt
                              d||
           d
}|p|
}|rt          |	|          n|	|fS | dk    rWt                      \  }	}|	t
          	                    d           dS t          |p||           }|rt          |	|          n|	|fS | dk    rWt                      \  }	}|	t
          	                    d           dS t          |p||           }|rt          |	|          n|	|fS | dk    rr]t                      }|st
          	                    d           dS t          |pt          |           }t          |t                    }||fS t!                      \  }	}|	t
          	                    d           dS t          |p||           }|rt          |	|          n|	|fS | dk    r|r|                                }|pd                                p(t%          j        dd                                          pd}|st
          	                    d           dS t          |pt)                      pd|           }i }d|                                v rddi|d<   n)d|                                v rdd lm}  |            |d<   t          dB||d|}	 ||	||          }	|rt          |	|          n|	|fS t0          t           t2          fD ]h} |            \  }	}|	Wt          |p||           }t          t5          |	d!d          pd          } ||	||          }	|rt          |	|          n|	|fc S it
          	                    d"           dS 	 dd#lm}  ||           }|rK|                    d!d                                          }|                    d$d                                          }|                    d%d                                          }|s)|r't%          j        |d                                          }|pd}|rt          |p$|                    d&          pt)                      pd|           }t          ||          }	 ||	||          }	t
                              d'| |           |rt          |	|          n|	|fS t
          	                    d(|            dS n# t<          $ r Y nw xY w	 dd)lm }m!}m"} n,# t<          $ r t
                              d*|            Y dS w xY w|                    |           }|t
          	                    d+|            dS |j#        d$k    r| d,k    rWtI                      \  }	}|	t
          	                    d-           dS t          |p||           }|rt          |	|          n|	|fS  ||           }t          |                    d$d                                                    }|s`tK          |j&                  }| d.k    r|'                    d/           t
                              d0| d1(                    |                     dS tS          t          |                    d!d                                                    *                    d          p|j+                  } tX                              | d          }t          |p||           }i }!d|                                 v rd|!d<   n9d|                                 v r#dd lm} |!-                     |                       t          dB|| d|!rd|!ini }	| d.k    rR|rPsN	 dd2lm.}"  |"|          r+t
                              d3|           t_          |	|          }	n# t<          $ r Y nw xY w ||	||           }	t
                              d4| |           |rt          |	|          n|	|fS |j#        d5k    r ||           }t          |pt)                      |           }| d6k    rJt          |                    d$d                                                    }t          |                    d!d                                                    } t          |                    d7d                                                    pd
}#tK          |                    d8          pg           }$|st
          	                    d9           dS |r| st
          	                    d:           dS dd;l0m1}%  |%|| |#|$<          }	t
                              d4| |           |rt          |	|          n|	|fS t
          	                    d=|            dS |j#        d>v rK| dk    rte          d||          S | dk    rte          d||          S t
          	                    d?|            dS t
          	                    d@|j#        |            dS )Cu  Central router: given a provider name and optional model, return a
    configured client with the correct auth, base URL, and API format.

    The returned client always exposes ``.chat.completions.create()`` — for
    Codex/Responses API providers, an adapter handles the translation
    transparently.

    Args:
        provider: Provider identifier.  One of:
            "openrouter", "nous", "openai-codex" (or "codex"),
            "zai", "kimi-coding", "minimax", "minimax-cn",
            "custom" (OPENAI_BASE_URL + OPENAI_API_KEY),
            "auto" (full auto-detection chain).
        model: Model slug override.  If None, uses the provider's default
               auxiliary model.
        async_mode: If True, return an async-compatible client.
        raw_codex: If True, return a raw OpenAI client for Codex providers
            instead of wrapping in CodexAuxiliaryClient.  Use this when
            the caller needs direct access to responses.stream() (e.g.,
            the main agent loop).
        explicit_base_url: Optional direct OpenAI-compatible endpoint.
        explicit_api_key: Optional API key paired with explicit_base_url.
        api_mode: API mode override.  One of "chat_completions",
            "codex_responses", or None (auto-detect).  When set to
            "codex_responses", the client is wrapped in
            CodexAuxiliaryClient to route through the Responses API.

    Returns:
        (client, resolved_model) or (None, None) if auth is unavailable.
    base_url_str	model_strr   c                     t          | t                    rdS rdS dk    rdS rdk    rdS |pd                                                                }d|v r d|vr|pd                                }d|v rdS dS )a(  Decide if a plain OpenAI client should be wrapped for Responses API.

        Returns True when api_mode is explicitly "codex_responses", or when
        auto-detection (api.openai.com + codex-family model) suggests it.
        Already-wrapped clients (CodexAuxiliaryClient) are skipped.
        Fr  Tr(   api.openai.comrc  r%   )ri   r   r)   r*   )
client_objr  r  normalized_basemodel_lowerr  r  s        r3   _needs_codex_wrapz2resolve_provider_client.<locals>._needs_codex_wrap_  s     j"677 	5 	5(((4 	$5555'-24466<<>>..<3V3V$?1133K+%%tur5   r(   final_model_strc                      | ||          r;t                               dpd||r
|dd         nd           t          | |          S | S )zNWrap a plain OpenAI client in CodexAuxiliaryClient if Responses API is needed.zeresolve_provider_client: wrapping client in CodexAuxiliaryClient (api_mode=%s, model=%s, base_url=%s)zauto-detectedN<   r(   )rH   rI   r   )r  r  r  r  r  s      r3   _wrap_if_neededz0resolve_provider_client.<locals>._wrap_if_neededv  sn    ZGG 	ELL7+O_%19SbS!!r	; ; ;
 (
ODDDr5   r    r  NrN  rB   z\Dropping OpenRouter-format model %r for non-OpenRouter auxiliary provider (using %r instead)rc  zLresolve_provider_client: openrouter requested but OPENROUTER_API_KEY not setr  zYresolve_provider_client: nous requested but Nous Portal not configured (run: hermes auth)r&   zbresolve_provider_client: openai-codex requested but no Codex OAuth token found (run: hermes model)rW  r$   r  r  zQresolve_provider_client: explicit custom endpoint requested but base_url is emptyr  rP  rQ  rR  rS  rT  r   rU  r@   zPresolve_provider_client: custom/main requested but no endpoint credentials found)_get_named_custom_providerr   key_envru   z6resolve_provider_client: named custom provider %r (%s)zAresolve_provider_client: named custom provider %r has no base_url)rL  rM  -resolve_external_process_provider_credentialsz-hermes_cli.auth not available for provider %sz,resolve_provider_client: unknown provider %rr   zOresolve_provider_client: anthropic requested but no Anthropic credentials foundcopilotzgh auth tokenzJresolve_provider_client: provider %s has no API key configured (tried: %s)r  )!_should_use_copilot_responses_apiud   resolve_provider_client: copilot model %s needs Responses API — wrapping with CodexAuxiliaryClientz resolve_provider_client: %s (%s)external_processzcopilot-acpcommandargszVresolve_provider_client: copilot-acp requested but no model was provided or configuredz^resolve_provider_client: copilot-acp requested but external process credentials are incompleter  )r   r@   r%  r&  zLresolve_provider_client: external-process provider %s not directly supported)oauth_device_codeoauth_externalzMresolve_provider_client: OAuth provider %s not directly supported, try 'auto'z6resolve_provider_client: unhandled auth_type %s for %sr(   r   )3r  r4   rD   r	  r   rH   rI   r	  rh  r  r  rr  rJ  r  r
   r  r  r)   r9  r:  ry  r*   r\  rV  r  ra  rX   r  r  r/   rX  rC  rL  rM  r!  rZ  r[  rj   api_key_env_varsrl   r   rL   rE   r^   r:   updater#  r   r  r  r  )'r   ru   r  r  r  r  r  r  r  r  r  final_modelr   r  
raw_clientr  r  r_  rV  r  _cbaser  custom_entrycustom_key_envrL  rM  r!  r^  default_modelr`  r   tried_sourcesr@   headersr#  r%  r&  r  r  s'      `  `                               @r3   r  r  3  sN   P &x00HC C D       .	 	S 	 	 	 	 	 	 	 	 6(lCCC>:
  	SE\\h\3h3F3FLL89>J J J E'x9C + 555k*	, <)++>NN < = = =:/0@(KK9C + 555k*	, 6#++>NN O P P P:/0@(KK9C + 555k*	, >!! 
	- 344K "  T U U U!z3E4M=MxXXK>QRRRJ,,$,,>NN P Q Q Q:/0@(KK9C + 555k*	, 8 	0+1133K!'R..00 %9-r2288::%$ 
  ",   "z3<)++<} K E!2!2!4!444,8:J+K'(((K,=,=,?,???EEEEEE+B+B+D+D'(NJNNNNF$_V[+FFF=G /$V[999 +.0 ,Z02 	4 	4F$fhhOFG!78H(SSWVZ<<BCC(fEEAK 3(===$k24 4 4	 " 	 ; 	< 	< 	<zJJJJJJ11(;; 	&**:r::@@BBK%)))R88>>@@J)--i<<BBDDN C. CY~r::@@BB
#8'8J 47]\--g66]:J:L:L]P]   
[III(kJJLk+ + + BL 3(===$k24NNS   :-	.    	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	

    DhOOOzz  ##H--GExPPPzI%%{""$2$4$4!FM~pqqq!z3E4J]HUUK=Gb$V[999fVaMbc44X>>eii	2..//5577 	 !9::M9$$$$_555LL 6!499]#;#;= = = :&		*b))**002299#>>\'B\
 
 5882FF/0FQQ X^^----$4GL!!$(8(888AAAAAANN2244555 M( M M;BJ-w77M M y  [  	OOOOOO44[AA GLLO#% % % 2&+FFF    !h??7;OOO9C + 555k*	, ...==hGG/0K9I9K9KXVV}$$%))Ir223399;;G599Z4455;;==H%))Ir223399;;CtG		&))/R00D "1   "z "( "9   "zAAAAAA%%!	  F LL;X{SSS=G /$V[999 +.0 ,-5	7 	7 	7z		E	E	Ev*65*EEE~%%*>5*MMM 89A	C 	C 	Cz
NNK$h0 0 0:s=   ET: T: :
UU
U %U?>U?<_> >
`
`r  c                Z    t          | pd          \  }}}}}t          ||||||          S )aR  Return (client, default_model_slug) for text-only auxiliary tasks.

    Args:
        task: Optional task name ("compression", "web_extract") to check
              for a task-specific provider override.

    Callers may override the returned model via config.yaml
    (e.g. auxiliary.compression.model, auxiliary.web_extract.model).
    N)ru   r  r  r  r  _resolve_task_provider_modelr  r  r  r   ru   r@   r   r  s          r3   get_text_auxiliary_clientr8    sM     4PPTP\X\3]3]0HeXw"" !   r5   c          	      \    t          | pd          \  }}}}}t          ||d||||          S )a  Return (async_client, model_slug) for async consumers.

    For standard providers returns (AsyncOpenAI, model). For Codex returns
    (AsyncCodexAuxiliaryClient, model) which wraps the Responses API.
    Returns (None, None) when no provider is available.
    NT)ru   r  r  r  r  r  r5  r7  s          r3   get_async_text_auxiliary_clientr:    sP     4PPTP\X\3]3]0HeXw"" !   r5   )rc  r  c                      t          |           S rw   )r4   r5  s    r3   _normalize_vision_providerr<    s    "8,,,r5   c                     t          |           } | dk    rt                      S | dk    rt          d          S | dk    rt                      S | dk    rt	                      S | dk    rt                      S dS )	Nrc  r  T)ri  r&   r   r$   rN  )r<  rh  rr  r  r[  r  r5  s    r3   _resolve_strict_vision_backendr>    s    )(33H<   6%%%%>!!||;8#%%%:r5   c                 0    t          |           d         d uS r   )r>  r5  s    r3    _strict_vision_backend_availabler@    s    )(33A6dBBr5   c                  ^   g } t                      }|rh|dvrd|t          v r%t          |          r|                     |           n6t	          |t                                \  }}||                     |           t          D ]*}|| vr$t          |          r|                     |           +| S )u   Return the currently available vision backends in auto-selection order.

    Order: active provider → OpenRouter → Nous → stop.  This is the single
    source of truth for setup, tool gating, and runtime auto-routing of
    vision tasks.
    r  )r-   _VISION_AUTO_PROVIDER_ORDERr@  rl   r  ry  )	availabler  r  r  ps        r3   get_available_vision_backendsrE    s     I'))M 0l::777/>> 0  ////?O?Q?QRRIFA!  ///(    I"B1"E"EQr5   )r@   r   r  r   c                   t          d| |||          \  }}}}t          |          }dt          dt          dt          t                   ffd}	|r!t          d|||          \  }
}|
d	S d|
|fS |d
k    rt                      }t                      }|r|dvr|t          v r"t          |          \  }}| |	|||          S n_t                              ||          }t          |||          \  }}|-t                              d||p|            |	|||p|          S t          D ],}||k    r	t          |          \  }}| |	|||          c S -t                              d           dS |t          v rt          |          \  }} |	|||          S t          ||          \  }
}|
|ddfS ||
|fS )ar  Resolve the client actually used for vision tasks.

    Direct endpoint overrides take precedence over provider selection. Explicit
    provider overrides still use the generic provider router for non-standard
    backends, so users can intentionally force experimental providers. Auto mode
    stays conservative and only tries vision backends known to work today.
    ri  r  r  r1  c                 X    || d d fS p|}rt          ||          \  }}| ||fS | ||fS rw   )r	  )r  r  r1  r,  async_clientasync_modelr  resolved_models         r3   	_finalizez1resolve_vision_provider_client.<locals>._finalize  sW    $dD00$5 	@(8k(R(R%L+$lK?? +{::r5   r$   )ru   r  r  r  r  N)r$   NNr    r  )r  z1Vision auto-detect: using active provider %s (%s)z'Auxiliary vision client: none availabler  )r6  r<  rD   r   r   r  r-   ry  rB  r>  r<   r/   rH   r  rI   _get_cached_client)r   ru   r@   r   r  r}  resolved_base_urlresolved_api_keyresolved_api_moderK  r  r,  r  r  r  r1  vision_model
rpc_client	rpc_modelr  rJ  s       `               @r3   resolve_vision_provider_clientrS    s    Yu(E8WY YUI~02BDU +955I;S ;s ;8TW= ; ; ; ; ; ; ;  -5 !/-&
 
 
 >'',,F ,--%''
 	N],>> ;;;-KM-Z-Z*]*$9]KOOO +
  7::=*UU(?!<.)0 )0 )0%
I )KKK%y'@L   %9%z93LN N N 5 	H 	HIM)))G	)R)R&K& yKGGGGG ' 	>???///%CI%N%N"]yK???,Y
6GI I IFK~$$$fk))r5   c                  <    t           rt          t                    ni S )zReturn extra_body kwargs for auxiliary API calls.
    
    Includes Nous Portal product tags when the auxiliary client is backed
    by Nous Portal. Returns empty dict otherwise.
    )r?   rk   NOUS_EXTRA_BODYr   r5   r3   get_auxiliary_extra_bodyrV  M  s     %6=4   2=r5   r  c                     t                      }t          j        d          }|s(t                      d|                                v rd| iS d| iS )aF  Return the correct max tokens kwarg for the auxiliary client's provider.
    
    OpenRouter and local models use 'max_tokens'. Direct OpenAI with newer
    models (gpt-4o, o-series, gpt-5+) requires 'max_completion_tokens'.
    The Codex adapter translates max_tokens internally, so we use max_tokens
    for it as well.
    rd  Nr  r   r   )r  r9  r:  r2  r*   )r  r  rg  s      r3   auxiliary_max_tokens_paramrX  V  s`     +,,KY+,,F 0!!) K$5$5$7$777'//%  r5   _client_cache@   c                  T    	 ddl m}  d | _        dS # t          t          f$ r Y dS w xY w)a  Monkey-patch ``AsyncHttpxClientWrapper.__del__`` to be a no-op.

    The OpenAI SDK's ``AsyncHttpxClientWrapper.__del__`` schedules
    ``self.aclose()`` via ``asyncio.get_running_loop().create_task()``.
    When an ``AsyncOpenAI`` client is garbage-collected while
    prompt_toolkit's event loop is running (the common CLI idle state),
    the ``aclose()`` task runs on prompt_toolkit's loop but the
    underlying TCP transport is bound to a *different* loop (the worker
    thread's loop that the client was originally created on).  If that
    loop is closed or its thread is dead, the transport's
    ``self._loop.call_soon()`` raises ``RuntimeError("Event loop is
    closed")``, which prompt_toolkit surfaces as "Unhandled exception
    in event loop ... Press ENTER to continue...".

    Neutering ``__del__`` is safe because:
    - Cached clients are explicitly cleaned via ``_force_close_async_httpx``
      on stale-loop detection and ``shutdown_cached_clients`` on exit.
    - Uncached clients' TCP connections are cleaned up by the OS when the
      process exits.
    - The OpenAI SDK itself marks this as a TODO (``# TODO(someday):
      support non asyncio runtimes here``).

    Call this once at CLI startup, before any ``AsyncOpenAI`` clients are
    created.
    r   AsyncHttpxClientWrapperc                     d S rw   r   r   s    r3   <lambda>z(neuter_async_httpx_del.<locals>.<lambda>  s    t r5   N)openai._base_clientr]  __del__rX  AttributeErrorr\  s    r3   neuter_async_httpx_delrc    sR    4??????*;*;'''(   s    ''r  c                     	 ddl m} t          | dd          }|t          |dd          s|j        |_        dS dS dS # t
          $ r Y dS w xY w)u  Mark the httpx AsyncClient inside an AsyncOpenAI client as closed.

    This prevents ``AsyncHttpxClientWrapper.__del__`` from scheduling
    ``aclose()`` on a (potentially closed) event loop, which causes
    ``RuntimeError: Event loop is closed`` → prompt_toolkit's
    "Press ENTER to continue..." handler.

    We intentionally do NOT run the full async close path — the
    connections will be dropped by the OS when the process exits.
    r   )ClientStaterx   N	is_closedT)httpx._clientre  rX   CLOSED_staterN   )r  re  inners      r3   _force_close_async_httpxrk    s    ------	400WUK%F%F&-ELLL    s   6> 
AAc                     ddl } t          5  t          t                                                    D ]b\  }}|d         }|t          |           	 t          |dd          }|r|                     |          s
 |             S# t          $ r Y _w xY wt          	                                 ddd           dS # 1 swxY w Y   dS )zClose all cached clients (sync and async) to prevent event-loop errors.

    Call this during CLI shutdown, *before* the event loop is closed, to
    avoid ``AsyncHttpxClientWrapper.__del__`` raising on a dead loop.
    r   Nr   )
inspect_client_cache_lockrj   rY  rY  rk  rX   iscoroutinefunctionrN   clear)rm  rY   rT   r  r  s        r3   shutdown_cached_clientsrq    s0    NNN	  }224455 	 	JC1XF~ %V,,,"67D99 G$?$?$I$I HJJJ   !                 s6   AB<2BB<
BB<BB<<C C c                  $   t           5  g } t                                          D ]E\  }}|\  }}}|8|                                r$t	          |           |                     |           F| D ]
}t          |= 	 ddd           dS # 1 swxY w Y   dS )uK  Force-close cached async clients whose event loop is closed.

    Call this after each agent turn to proactively clean up stale clients
    before GC can trigger ``AsyncHttpxClientWrapper.__del__`` on them.
    This is defense-in-depth — the primary fix is ``neuter_async_httpx_del``
    which disables ``__del__`` entirely.
    N)rn  rY  rY  rf  rk  rl   )
stale_keysrY   rT   r  _defaultcached_loops         r3   cleanup_stale_async_clientsrv    s     
 # #
'--// 	' 	'JC,1)FHk&;+@+@+B+B&(000!!#&&& 	# 	#Cc""	## # # # # # # # # # # # # # # # # #s   A/BB	B	c           	          | t          | dd           t          | dd           fD ];}|r7dt          t          |dd          pd                                          v r dS <dS )Nrx   r  rc  r@   r(   TF)rX   rD   r*   )r  r   s     r3   _is_openrouter_clientrx    su    	488'&(TX:Y:YZ   	<3wsJ'C'C'Ir#J#J#P#P#R#RRR445r5   cached_defaultc                 8    |rd|v rt          |           s|S |p|S )zDrop OpenRouter-format model slugs (with '/') for non-OpenRouter clients.

    Mirrors the guard in resolve_provider_client() which is skipped on cache hits.
    rB   )rx  )r  ru   ry  s      r3   _compat_modelr{    s2    
  &;F&C&C"N"r5   c           	      @   d}|r*	 ddl }|                                }n# t          $ r Y nw xY wt          |          | dk    r t	          fdt
          D                       nd}	| ||pd|pd|pd|	f}
t          5  |
t          v rt          |
         \  }}}|rX|duo||u o|                                 }|r!t          |||          }||fcddd           S t          |           t          |
= n!t          |||          }||fcddd           S ddd           n# 1 swxY w Y   t          | |||||          \  }}||}t          5  |
t          vrt          t                    t          k    rpt          t          t                                                              \  }}t          |d                    t          |= t          t                    t          k    p|||ft          |
<   nt          |
         \  }}}ddd           n# 1 swxY w Y   ||p|fS )a  Get or create a cached client for the given provider.

    Async clients (AsyncOpenAI) use httpx.AsyncClient internally, which
    binds to the event loop that was current when the client was created.
    Using such a client on a *different* loop causes deadlocks or
    RuntimeError.  To prevent cross-loop issues, the cache validates on
    every async hit that the cached loop is the *current, open* loop.
    If the loop changed (e.g. a new gateway worker-thread loop), the stale
    entry is replaced in-place rather than creating an additional entry.

    This keeps cache size bounded to one entry per unique provider config,
    preventing the fd-exhaustion that previously occurred in long-running
    gateways where recycled worker threads created unbounded entries (#10200).
    Nr   r    c              3   D   K   | ]}                     |d           V  dS )r(   Nr4  )r  r  r  s     r3   r  z%_get_cached_client.<locals>.<genexpr>  s1      QQ5E2..QQQQQQr5   r   r(   )r  r  r  r  )r   get_event_loopr  r  tupler  rn  rY  rf  r{  rk  r  rG   _CLIENT_CACHE_MAX_SIZEnextiterrY  )r   ru   r  r@   r   r  r  current_loop_aioruntime_key	cache_keycached_clientry  ru  loop_ok	effectiver  r1  
bound_loop	evict_keyevict_entryr  r  s                         @r3   rL  rL    sk   : L 	""""..00LL 	 	 	D	%l33GU]agUgUg%QQQQ<PQQQQQQmoK:x~2w}"hnRTVabI	 0 0%%9Fy9Q6M>; 0
  t+ 4#|34'11333 
  4 -mUN S SI()30 0 0 0 0 0 0 0  )777!),,)-OO	$i/)0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0, 4"   FM  "
 
	D 
	D-- -((,BBB-1$}7J7J7L7L2M2M-N-N*I{,[^<<<%i0 -((,BBB -3M:+Ni((+8+C(q
	D 
	D 
	D 
	D 
	D 
	D 
	D 
	D 
	D 
	D 
	D 
	D 
	D 
	D 
	D 5)M))s8     
--<AD,DD"%D"B6HHHc                    i }d}d}d}d}	d}
| r	 ddl m}  |            }n# t          $ r i }Y nw xY wt          |t                    r|                    di           ni }t          |t                    r|                    | i           ni }t          |t                    si }t          |                    dd                                                    pd}t          |                    dd                                                    pd}t          |                    dd                                                    pd}t          |                    d	d                                                    pd}	t          |                    d
d                                                    pd}
|p|}|
}|rd||||fS |r|||||fS | r|rd|||	|fS |r|dk    r||dd|fS d|dd|fS d|dd|fS )a  Determine provider + model for a call.

    Priority:
      1. Explicit provider/model/base_url/api_key args (always win)
      2. Config file (auxiliary.{task}.provider/model/base_url)
      3. "auto" (full auto-detection chain)

    Returns (provider, model, base_url, api_key, api_mode) where model may
    be None (use provider default). When base_url is set, provider is forced
    to "custom" and the task uses that direct endpoint. api_mode is one of
    "chat_completions", "codex_responses", or None (auto-detect).
    Nr   rt  	auxiliaryr   r(   ru   r@   r   r  r$   r    )rv  ru  rX  ri   rk   r/   rD   r)   )r  r   ru   r@   r   configr  	cfg_modelr  cfg_api_keycfg_api_moderu  auxtask_configrJ  rO  s                   r3   r6  r6  K  si   & FLILKL L	555555 []]FF 	 	 	FFF	 .8-E-EMfjjb)))2+5c4+@+@HcggdB'''b+t,, 	K;??:r::;;AACCKt4455;;==E	;??:r::;;AACCKt+//)R8899??AAIT;??:r::;;AACCKt'iN$ N7<MMM N7<MMM E 	Z^\;HYYY 	OLF22t=NNN~tT3DDD>4/@@@s   " 11      >@r   c                    | s|S 	 ddl m}  |            }n# t          $ r |cY S w xY wt          |t                    r|                    di           ni }t          |t                    r|                    | i           ni }|                    d          }|'	 t          |          S # t          t          f$ r Y nw xY w|S )zPRead timeout from auxiliary.{task}.timeout in config, falling back to *default*.r   rt  r  timeout)	rv  ru  rX  ri   rk   r/   floatr  	TypeError)r  r   ru  r  r  r  raws          r3   _get_task_timeoutr    s     111111   )3FD)A)A
I&**["
%
%
%rC'1#t'<'<D#''$###"K
//)
$
$C
	::I& 	 	 	D	Ns    &&B* *B>=B>r8   c                 L    | t           v rdS |pd                                }d|v S )zDetect if an endpoint expects Anthropic-format content blocks.

    Returns True for known Anthropic-compatible providers (MiniMax) and
    any endpoint whose URL contains ``/anthropic`` in the path.
    Tr(   rC   )_ANTHROPIC_COMPAT_PROVIDERSr*   )r   r@   	url_lowers      r3   _is_anthropic_compat_endpointr    s5     ...tR&&((I9$$r5   r}   c           	         g }| D ]o}|                     d          }t          |t                    s|                    |           Cg }d}|D ]}|                     d          dk    r|                     d          pi                      dd          }|                    d          rv|                    d          \  }}	}
d	}d
|v r:d|v r6|                    d
d          d                             dd          d         }|                    dd||
dd           n|                    dd|dd           d}|                    |           |                    |ri |d|in|           q|S )zConvert OpenAI ``image_url`` content blocks to Anthropic ``image`` blocks.

    Only touches messages that have list-type content with ``image_url`` blocks;
    plain text messages pass through unchanged.
    r`   Frb   rf   rJ   r(   zdata:,z	image/pngr"   ;r#   r   imagerD  )rb   
media_typer1  )rb   r'  )rb   rJ   T)r/   ri   rj   rl   r+   	partitionr,   )r}   rm   r   r`   new_contentchangedblockimage_url_valheaderr  b64datar  s               r3   #_convert_openai_images_to_anthropicr    s    I $N $N'')$$'4(( 	S!!! 	* 	*Eyy  K//!&;!7!7!=2 B B5" M M ++G44 )6)@)@)E)E&FAw!,Jf}}%+\\#q%9%9!%<%B%B3%J%J1%M
&& '$,*4$+# #( (      && '$)#0# #( (    ""5))))GL8C8K888MMMMr5   r   r   r   r  
extra_bodyc	                 t   |||d}	|ddl m}
  |
|          rd}|||	d<   |=| dk    r2|pt                      }d|                                v r||	d<   n||	d	<   n||	d	<   |r||	d
<   t	          |pi           }| dk    st
          r*|                    dg                               dg           |r||	d<   |	S )zLBuild kwargs for .chat.completions.create() with model/provider adjustments.)ru   r}   r  Nr   r   r   r$   r  r   r   r   r  r=   r>   r  )r  r   r  r*   rk   r?   
setdefaultextend)r   ru   r}   r   r   r   r  r  r@   r   r   r  merged_extras                r3   _build_call_kwargsr    s0     F DDDDDD##E** 	K +} x"@&>&@&@K;#4#4#6#6662<.//'1|$$#-F<   w 
(b))L6.++224J3KLLL ,+|Mr5   r  c           
      `   | t          d|pd d          	 | j        }|rt          |d         d          st          d          nf# t          t          t
          f$ rL}t          |           j        }t          |           dd         }t          d|pd d	| d
|d          |d}~ww xY w| S )a3  Validate that an LLM response has the expected .choices[0].message shape.

    Fails fast with a clear error instead of letting malformed payloads
    propagate to downstream consumers where they crash with misleading
    AttributeError (e.g. "'str' object has no attribute 'choices'").

    See #7264.
    Nz
Auxiliary r  z: LLM returned None responser   r   zmissing choices[0].messagex   z&: LLM returned invalid response (type=z): ug   . Expected object with .choices[0].message — check provider adapter or custom endpoint compatibility.)	r  r   r  rb  r  
IndexErrorrb   r   rD   )r  r  r   rR   response_typeresponse_previews         r3   _validate_llm_responser  	  s    EEEE
 
 	

" 	?ggaj)<< 	? !=>>>	?Iz2   X/x==#.9 9 9"9 9'79 9 9
 

 	 Os   .A B+AB&&B+)
r   ru   r@   r   r  r   r   r   r  r  c                :   t          | ||||          \  }}}}}| dk    r~t          |dk    r|n||p||p||p|d          \  }}}|9|dk    r3|s1t                              d|           t          d|d          \  }}}|t	          d|  d	| d
          |p|}nt          ||||||          \  }}||pd                                                                }|r,|dvr(t	          d| d|                                 d          |s2t          	                    d| pd|           t          d|          \  }}|t	          d|  d	| d
          |
|
nt          |           }t          t          |d|          pd          }| r-t          	                    d| |pd|pd|r	d|vrd| nd           t          ||||||	|||	  	        }t          t          |dd          pd          }t          ||          rt          |d                   |d<   	 t!           |j        j        j        d$i ||           S # t(          $ rM}t          |          }d|v sd|v r~|                    dd           ||d<   	 t!           |j        j        j        d$i ||           cY d}~S # t(          $ r+}t-          |          st/          |          s |}Y d}~nd}~ww xY wt-          |          pt/          |          }|dv }|r|rt-          |          rdnd }t          	                    d!| pd|||           t1          || |"          \  }} }!|At          |!| ||||	||#          }"t!           |j        j        j        d$i |"|           cY d}~S  d}~ww xY w)%a  Centralized synchronous LLM call.

    Resolves provider + model (from task config, explicit args, or auto-detect),
    handles auth, request formatting, and model-specific arg adjustments.

    Args:
        task: Auxiliary task name ("compression", "vision", "web_extract",
              "session_search", "skills_hub", "mcp", "flush_memories").
              Reads provider:model from config/env. Ignored if provider is set.
        provider: Explicit provider override.
        model: Explicit model override.
        messages: Chat messages list.
        temperature: Sampling temperature (None = provider default).
        max_tokens: Max output tokens (handles max_tokens vs max_completion_tokens).
        tools: Tool definitions (for function calling).
        timeout: Request timeout in seconds (None = read from auxiliary.{task}.timeout config).
        extra_body: Additional request body fields.

    Returns:
        Response object with .choices[0].message.content

    Raises:
        RuntimeError: If no provider is configured.
    ri  r    Fr   ru   r@   r   r  NDVision provider %s unavailable, falling back to auto vision backendsr   ru   r  $No LLM provider configured for task=
 provider=. Run: hermes setup)r@   r   r  r  r(   r    rc  r$   
Provider ':' is set in config.yaml but no API key was found. Set the U_API_KEY environment variable, or switch to a different provider with `hermes model`.BAuxiliary %s: provider %s unavailable, trying auto-detection chainr  r  r@   zAuxiliary %s: using %s (%s)%sr   rc  z at r   r   r   r  r  r@   r}   r   unsupported_parameterr   r    r(   Nr  connection errorz,Auxiliary %s: %s on %s (%s), trying fallbackr  r   r   r   r  r  r   )r6  rS  rH   r  r  rL  r)   r*   upperr  r  rD   rX   r  r  r  r  r   r   r   rN   popr  r  r  )#r  r   ru   r@   r   r  r}   r   r   r   r  r  r  rJ  rM  rN  rO  effective_providerr  r,  	_expliciteffective_timeout
_base_infor   _client_base	first_errerr_str	retry_errshould_fallbackis_autor  	fb_clientfb_modelfb_label	fb_kwargss#                                      r3   call_llmr  <	  s   N a}hxa2 a2]~'8:JL] x2P*;v*E*E&&8 )E&2($/3
 3
 3
/FK >/699BS9NNV!   7U$ 7 7 73
 >%t % %GX % % %   /C2C0&$&%
 
 
 > +0b7799??AAI Y.NNN"X X X*3//*;*;X X X   % \` NF,=? ? ?&8l&[&[&[#>%t % %GX % % %& & & $+#6<Md<S<S WVZ1BCCIrJJJ c3,68Py,6a<z;Y;Y(J(((_a	c 	c 	c  ;J.:"	$ $ $F wvz266<"==L$%6EE U@
ASTTz1%*FK#*44V44d< < 	< . . .i..7""&=&H&HJJ|T***.8F*+&-2FK+2<<V<<dD D D D D D D D & & & *)44 8LY8W8W %						&& ,I66Y:Ny:Y:Y $'99 	Jw 	J(9)(D(D\__J\FKKF0A9N N N,A!4-8 -8 -8)Ix$.h +
):)	+ + +	
 .5IN.5BB	BBDJ J J J J J J J].sO   $I N2N $J*$N*
K4!KNKB/NNNNc                    ddl }| j        d         j        }|j        pd                                }|r<|                    dd||j        |j        z                                            }|r|S g }dD ]i}t          ||d          }|rTt          |t                    r?|                                r+||vr'|                    |                                           jt          |dd          }|rt          |t                    r|D ]}	t          |	t                    r|	                    d          p)|	                    d	          p|	                    d
          }
|
rO|
|vrK|                    t          |
t                    r|
                                nt          |
                     |rd                    |          S dS )u  Extract content from an LLM response, falling back to reasoning fields.

    Mirrors the main agent loop's behavior when a reasoning model (DeepSeek-R1,
    Qwen-QwQ, etc.) returns ``content=None`` with reasoning in structured fields.

    Resolution order:
      1. ``message.content`` — strip inline think/reasoning blocks, check for
         remaining non-whitespace text.
      2. ``message.reasoning`` / ``message.reasoning_content`` — direct
         structured reasoning fields (DeepSeek, Moonshot, Novita, etc.).
      3. ``message.reasoning_details`` — OpenRouter unified array format.

    Returns the best available text, or ``""`` if nothing found.
    r   Nr(   zz<(?:think|thinking|reasoning|thought|REASONING_SCRATCHPAD)>.*?</(?:think|thinking|reasoning|thought|REASONING_SCRATCHPAD)>)flags)	reasoningreasoning_contentreasoning_detailssummaryr`   rc   z

)rer   r   r`   r)   subDOTALL
IGNORECASErX   ri   rD   rl   rj   rk   r/   r   )r  r  r   r`   cleanedreasoning_partsr  r   detailsrh   r  s              r3   extract_content_or_reasoningr  	  s    III

1

%C{ b''))G 	&&L ry2=8	  
 

 %'' 	  	N "$O3 0 0c5$'' 	0:c3'' 	0CIIKK 	0C<V<V""399;;///c.55G 	j:gt,, 	j 	j 	jF&$'' jJJy)) *zz),,*zz&)) 
  jwo==#**jRU>V>V+h7==???\_`g\h\hiii ,{{?+++2r5   )	r   ru   r@   r   r   r   r   r  r  c       
         .  K   t          | ||||          \  }}}}}| dk    r~t          |dk    r|n||p||p||p|d          \  }}}|9|dk    r3|s1t                              d|           t          d|d          \  }}}|t	          d|  d	| d
          |p|}nt          ||d|||          \  }}||pd                                                                }|r,|dvr(t	          d| d|                                 d          |s2t          	                    d| pd|           t          dd          \  }}|t	          d|  d	| d
          |	|	nt          |           }t          ||||||||
|	  	        }t          t          |dd          pd          }t          ||          rt          |d                   |d<   	 t!           |j        j        j        d!i | d{V |           S # t(          $ r}t          |          }d|v sd|v r|                    dd           ||d<   	 t!           |j        j        j        d!i | d{V |           cY d}~S # t(          $ r+}t-          |          st/          |          s |}Y d}~nd}~ww xY wt-          |          pt/          |          }|dv }|r|rt-          |          rdnd}t          	                    d| pd|||           t1          || |          \  }}}||t          ||||||||
          } t3          ||pd          \  }!}"|"r|"|                     d           k    r|"| d <   t!           |!j        j        j        d!i |  d{V |           cY d}~S  d}~ww xY w)"zqCentralized asynchronous LLM call.

    Same as call_llm() but async. See call_llm() for full documentation.
    ri  r    Tr  Nr  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  z4Auxiliary %s (async): %s on %s (%s), trying fallbackr  r  ru   r   )r6  rS  rH   r  r  rL  r)   r*   r  r  r  r  rD   rX   r  r  r  r   r   r   rN   r  r  r  r  r	  r/   )#r  r   ru   r@   r   r}   r   r   r   r  r  r  rJ  rM  rN  rO  r  r  r,  r  r  r   r  r  r  r  r  r  r  r  r  r  r  async_fbasync_fb_models#                                      r3   async_call_llmr  !
  s     $ a}hxa2 a2]~'8:JL] x2P*;v*E*E&&8 )E&2($/3
 3
 3
/FK >/699BS9NNV!   7U$7 7 73
 >%t % %GX % % %   /C2C0&$&
 
 
 >*0b7799??AAI Y.NNN"X X X*3//*;*;X X X  
 % R` NF,=? ? ?&8D&Q&Q&Q#>%t % %GX % % %& & & $+#6<Md<S<S;J.:"	$ $ $F wvz266<"==L$%6EE U@
ASTTz'%0&+)0::6::::::::DB B 	B $ $ $i..7""&=&H&HJJ|T***.8F*+&-8&+18BB6BBBBBBBBDJ J J J J J J J & & & *)44 8LY8W8W %						& ,I66Y:Ny:Y:Y#'99 	Ow 	O(9)(D(D\__J\FKKN0A9N N N,A!4-8 -8 -8)Ix$.h +
):)	+ + +	 ,<Ix~SU+V+V(.! 8n	g8N8N&N&N)7Ig&-:(-3:GGYGGGGGGGGO O O O O O O OI$sO   *G; ;N2N9*I)#N)
J3!JNJC*NNNNr)  r  )r   N)Nr  rw   )NFFNNNNrN  )NFNNNN)NNNNN)NNNr  NN)r   r-  loggingr9  	threadingrF  pathlibr   typesr   typingr   r   r   r   r	   r  r
   agent.credential_poolr   rv  r   hermes_constantsr   	getLoggerr   rH   r  r.   rD   r4   r:   __annotations__r<   re  rU  r?   r	  rf  rn  ro  rp  r*  r  r+  r  r  rL   rS   rZ   r_   rq   rs   r   r   r   r   r   r   r  r  r  r  r  rk   r2  r6  r;  rJ  ra  rh  rr  ry  r-   r  r  r  r  r  r  r[  _AUTO_PROVIDER_LABELS	frozensetr  r  r  r  r  rN   r  r  r  r   r	  r  r  r8  r:  rB  r<  r>  r@  rE  rS  rV  intrX  rY  Lockrn  r  rc  rk  rq  rv  rx  r{  rL  r6  _DEFAULT_AUX_TIMEOUTr  r  r  r  rj   r  r  r  r  r  r  r   r5   r3   <module>r     so  " " "H   				            ! ! ! ! ! ! 3 3 3 3 3 3 3 3 3 3 3 3 3 3       + + + + + + - - - - - - 0 0 0 0 0 0		8	$	$  hX  E	
 E E 
5 E E U M   # \  ,!" k#$ ;%  ,9hsm 9 9 9 9 9( '*- ,)$/)0 0 d38n   ( + + c3h    <(7  234   4    4 -4 0 D 9 !/##k1 # = # #    " tXc]/B)C     " " " " " ". .# . .c . . . .(-C -C - - - -``
 `
 `
 `
 `
 `
 `
 `
F# # # # # # # #" " " " " " " "$D D D D D D D D# # # # # # # #
. . . . . . . .D
 D
 D
 D
 D
 D
 D
 D
N# # # # # # # #
        D D D D D D D D# # # # # # # #
. . . . . . . .#$ # # # #LID IS I I I I
H H H H H
((3- ( ( ( (VF5&)98C=)H#I F F F FZCx/#>? C C C C&* *d *uXf-=x}-L'M * * * *Z#    *S    &/8x}hsmXc]'R!S /8 /8 /8 /8d#    
   6     $CeHV$4hsm$CD C C C C&QE(3-#67 Q Q Q Q*-chsmXc]:; -c -c -c -cb $* !*   "	<"899 O (4S>*B tCQTH~    T%[    9     (i D    8 !, ,,
, , 8C=(3-,-	, , , ,^O Oc3h 8 OE(SYJZ\deh\iJiDj O O O Oz. . . . .>	(3- 	3 	8TW= 	 	 	 	 ! -1V VVV V 	V
 V V V 4S>*V 8C=(3-'(V V V Vx
  .2  
 4S>* 8FXc]*+	   2`d   # XdSVX[S[nE]    & -# -3 - - - -S U8C=(SV-;W5X    Cs Ct C C C CtCy    4 #Z* #!Z* Z* Z*smZ*C=Z* sm	Z*
 c]Z* Z* 8C=(3-#67Z* Z* Z* Z*z>$ > > > >!c !d ! ! ! !H %'tE5L! & & &#Y^%%     BS T    (   6# # # #&# $    ## #hsm #Xc] #W_`cWd # # # # -1U* U*U*U* U* 	U*
 U* U* 4S>*U* 8C=(3-'(U* U* U* U*r <A <A
<A<A <A 	<A
 <A 3x}hsmXc]JK<A <A <A <A~   3G  C % 5    4 (iL(ABB 	%C 	%3 	%4 	% 	% 	% 	%,$ ,4 , , , ,h $( $ !%"4 444 4 %	4
 4 D>4 4 4 sm4 
4 4 4 4n S  s    @ j -1j j j
j j 	j
 j j 4S>*j j j j j j j 	j j j jZ5c 5 5 5 5r { { { {
{ { 	{
 { { { { { { { { 	{ { { { { {r5   