
    o;iҙ                       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mZ ddlmZmZmZ erddlmZ daee         ed<   d	efd
Z G d d          Z e            ZddlmZ ddlmZ ddl m!Z! ddl"m#Z#m$Z$m%Z%  ej&        e'          Z(deded	e)fdZ*de+fd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/d/d0d0d0d0d1d1d2d2d2d2d3Z.d4ee+         d	e+fd5Z/ e0            Z1e0ed6<   d7ee+         d	e)fd8Z2	 d d7ee+         d9ee+         d	d:fd;Z3i dd<d d=d%d>d?d@d(d>d+dAdBdCdDdEd.dCd/dFdGdHdIdJdKdLdMdNdOdPd2dQZ4ee+e+f         edR<   dSdTdUZ5ee+e+f         edV<    e6d%d(h          Z7e6edW<   dXdYdZd[Z8 e6h d\          Z9d d]e:dz  d	e:fd^Z;dd_l<m=Z> dXdYd`e> daZ?dbdcgiZ@daAe)edd<   dNZBdNZCdeZDdfZE e            dgz  ZFdhZGdie+d	ee+e+f         fdjZHd9e+d	e+fdkZId4e+d	ee)ee         f         fdlZJdmed	e+fdnZKddmedpe+d	e+fdqZLdred	efdsZM G dt du          ZN G dv dw          ZO G dx dy          ZP G dz d{          ZQ G d| d}          ZR G d~ d          ZS G d d          ZT G d d          ZU G d d          ZV G d d          ZW G d d          ZX G d d          ZYd9e+d	e)fdZZ	 d ded7e+de+d9e+dee+         d	efdZ[d	ee:         fdZ\d4e:d	e+fdZ]d	e+fdZ^ddde)d	ee_e+e+f                  fdZ`d	ee+         fdZad	eee         ee+         f         fdZbd de+d	eee         ee+         f         fdZcd	e+fdZddde)d	eee         ee+         f         fdZed	e+fdZfd	e+fdZgd	eee+         ee+         ee+         f         fdZhd	e+fdZiddZjd9e+d	dfdZkd	eee         ee+         f         fdZld7e+d	eee         ee+         f         fdZmd	eee         ee+         f         fdZndddddZodZpdeee+ef                  d	ee+e+f         fdZqd	ee_         fdZrdesd	e)fdZtdesd	e)fdZudesd	e)fdZvdesde+d	e)fdZwdesd	e)fdZxd4e+d	dfdZyd4e+d	e)fdZz	 	 dde+de+de+d	eee         ee+         e+f         fdZ{d deee+ef                  d	eee         ee+         f         fdZ|dd7e+de)fdZ}dee+         d4e+d	ee+         fdZ~	 	 	 	 	 	 	 	 dd4e+d7e+de)de)de+de+de+deee+ef                  de)d	eee         ee+         f         fdƄZ	 dddǜde+deee+ef                  d	eee         ee+         f         fdȄZdddǜde+deee+ef                  fdɄZdZd4ee+         d	e+fd˄Z	 d d4e+d7ee+         d	eee         ee+         f         fd̄Zd4e+d	e)fd̈́Zd	ee+         fd΄Z	 	 dddddϜd4ee+         d7ee+         d9ee+         dee+         de)d	eee+         ee         ee+         f         fdЄZd	e:fdфZded	e:fdӄZi Zee_e_f         ed<    ej                    ZdZdddddd֜d4e+de)d9ee+         dee+         dee+         deee+ef                  de)d	e_fdׄZdd؜de_dedee+         ded	df
d݄Zdddddd֜de+d7ee+         de)d9ee+         dee+         dee+         deee+ef                  de)d	eee         ee+         f         fd߄ZddZded	dfdZddZddZded	e)fdZdedee+         d	e)fdZded7ee+         dee+         d	ee+         fdZ	 	 	 	 	 	 	 dd4e+d7e+de)d9e+de+de+deee+ef                  de)d	eee         ee+         f         fdZ	 	 	 	 	 dde+d4e+d7e+d9e+de+d	ee+ee+         ee+         ee+         ee+         f         fdZdZde+d	ee+ef         fdZefde+ded	efdZde+d	ee+ef         fdZ e6h d          Zd4e+d9e+d	e)fdZded	efdZ	 	 	 	 	 	 d	d4e+d7e+dedee         dee         dee         dedee:         d9ee+         d	e:fdZd dede+d	efdZ	 d ddddddddddd
de+d4e+d7e+d9e+de+deee+ef                  dededededede:d	efdZd	e+fdZ	 d dddddddddd	de+d4e+d7e+d9e+de+dededededede:d	efdZdS (
  u  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. User's main provider + main model (used regardless of provider type —
     aggregators, direct API-key providers, native Anthropic, Codex, etc.)
  2. OpenRouter  (OPENROUTER_API_KEY)
  3. Nous Portal (~/.hermes/auth.json active provider)
  4. Custom endpoint (config.yaml model.base_url + OPENAI_API_KEY)
  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. Native Anthropic
  5. Custom endpoint (for local vision models: Qwen-VL, LLaVA, Pixtral, etc.)
  6. None

Codex OAuth (ChatGPT-account auth) is intentionally NOT in either
fallback chain: OpenAI gates this endpoint behind an undocumented,
shifting model allow-list, so "just try Codex with a hardcoded model"
rots on its own.  Codex is used only when the user's main provider *is*
openai-codex (Step 1 above) or when a caller explicitly requests it with
a model (auxiliary.<task>.provider + auxiliary.<task>.model).

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TYPE_CHECKING)urlparseparse_qs
urlunparseOpenAI_OPENAI_CLS_CACHEreturnc                  .    t           ddlm}  | a t           S )z#Import and cache ``openai.OpenAI``.Nr   r   )r   openair   )_clss    ;/home/ubuntu/.hermes/hermes-agent/agent/auxiliary_client.py_load_openai_clsr   H   s(      ))))))     c                   (    e Zd ZdZdZd Zd Zd ZdS )_OpenAIProxyzModule-level proxy that looks like the ``openai.OpenAI`` class.

    Forwards ``OpenAI(...)`` calls and ``isinstance(x, OpenAI)`` checks to the
    real SDK class, importing the SDK lazily on first use.
     c                 *     t                      |i |S N)r   )selfargskwargss      r   __call__z_OpenAIProxy.__call__Z   s    !!!426222r   c                 :    t          |t                                S r   )
isinstancer   )r   objs     r   __instancecheck__z_OpenAIProxy.__instancecheck__]   s    #/11222r   c                     dS )Nz<lazy openai.OpenAI proxy>r   r   s    r   __repr__z_OpenAIProxy.__repr__`   s    ++r   N)__name__
__module____qualname____doc__	__slots__r    r$   r'   r   r   r   r   r   Q   sR          I3 3 33 3 3, , , , ,r   r   )	load_pool)get_hermes_home)OPENROUTER_BASE_URL)base_url_host_matchesbase_url_hostnamenormalize_proxy_env_varsr#   
maybe_typec                 F    	 t          | |          S # t          $ r Y dS w xY w)zDReturn False instead of raising when a patched symbol is not a type.F)r"   	TypeError)r#   r3   s     r   _safe_isinstancer6   n   s9    #z***   uus    
  urlc                     t          |           }|j        rWt          |                    d                    }d t	          |j                                                  D             }||fS | dfS )zNExtract query params from URL, return (clean_url, default_query dict or None). )queryc                 &    i | ]\  }}||d          S r   r   ).0kvs      r   
<dictcomp>z-_extract_url_query_params.<locals>.<dictcomp>{   s"    EEEda!QqTEEEr   N)r   r:   r   _replacer   items)r7   parsedcleanparamss       r   _extract_url_query_paramsrF   v   so    c]]F| 6???4455EEhv|&<&<&B&B&D&DEEEf}9r   Fgooglegeminizgoogle-geminizgoogle-ai-studiozx-aixaizx.aigrokglmzaizz-aizz.aizhipukimizkimi-codingmoonshotzkimi-cnzkimi-coding-cnzmoonshot-cnz	gmi-cloudgmigmicloudzminimax-china
minimax-cn	anthropiccopilotcopilot-acpztencent-tokenhub)
minimax_cnclaudezclaude-codegithubzgithub-copilotzgithub-modelzgithub-modelszgithub-copilot-acpzcopilot-acp-agenttencenttokenhubztencent-cloudtencentmaasproviderc                    | pd                                                                 }|                    d          r4|                    dd          d                                          }|sdS |}|dk    rdS |dk    r?t	                      pd	                                                                 }|r|d
vr|}ndS t
                              ||          S )Nautocustom::   customcodexopenai-codexmainr9   )r^   re   r9   )striplower
startswithsplit_read_main_provider_PROVIDER_ALIASESget)r\   
normalizedsuffix	main_provs       r   _normalize_aux_providerrp      s    $f++--3355JY'' !!#q))!,2244 	8
W~V )**0b7799??AA	 	*>>>"JJ8  Z888r   OMIT_TEMPERATUREmodelc                     | pd                                                                                     dd          d         }|                    d          p|dk    S )zHTrue for any Kimi / Moonshot model that manages temperature server-side.r9   /ra   zkimi-rN   )rf   rg   rsplitrh   )rr   bares     r   _is_kimi_modelrx      sS    KR  &&((//Q77;D??7##5tv~5r   base_urlzOptional[float] | objectc                 h    t          |           r"t                              d|            t          S dS )u  Return a temperature directive for models with strict contracts.

    Returns:
        ``OMIT_TEMPERATURE`` — caller must remove the ``temperature`` key so the
            provider chooses its own default.  Used for all Kimi / Moonshot
            models whose gateway selects temperature server-side.
        ``float`` — a specific value the caller must use (reserved for future
            models with fixed-temperature contracts).
        ``None`` — no override; caller should use its own default.
    z7Omitting temperature for Kimi model %r (server-managed)N)rx   loggerdebugrq   )rr   ry   s     r   _fixed_temperature_for_modelr}      s5     e  NPUVVV4r   zgemini-3-flash-previewzglm-4.5-flashzkimi-k2-turbo-previewstepfunzstep-3.5-flashz$google/gemini-3.1-flash-lite-previewminimaxzMiniMax-M2.7minimax-oauthzMiniMax-M2.7-highspeedclaude-haiku-4-5-20251001z
ai-gatewayzgoogle/gemini-3-flashzopencode-zenzgemini-3-flashzopencode-gozglm-5kilocodezgoogle/gemini-3-flash-previewzollama-cloudznemotron-3-nano:30bzhy3-preview_API_KEY_PROVIDER_AUX_MODELSz	mimo-v2.5zglm-5v-turbo)xiaomirL   _PROVIDER_VISION_MODELS_PROVIDERS_WITHOUT_VISIONz%https://hermes-agent.nousresearch.comzHermes Agentzproductivity,cli-agent)HTTP-RefererzX-OpenRouter-TitlezX-OpenRouter-Categories>   1onyestrue	or_configc                 6   t          t                    }| 8	 ddlm}  |                                di           } n# t
          $ r i } Y nw xY wt          j                            dd                                          	                                }|r
|t          v }n|                     dd          }|s|S d	|d
<   t          j                            dd                                          }|rF|                                r1t          |          }d|cxk    rdk    rn ntt          |          |d<   na|                     dd          }t          |t          t          f          r/d|cxk    rdk    r"n nt          t          |                    |d<   |S )u  Build OpenRouter headers, optionally including response-cache headers.

    Precedence for response cache: env var > config.yaml > default (enabled).

    Environment variables:
        ``HERMES_OPENROUTER_CACHE`` — truthy (``1``/``true``/``yes``/``on``)
            enables caching; ``0``/``false``/``no``/``off`` disables.
            Overrides ``openrouter.response_cache`` in config.yaml.
        ``HERMES_OPENROUTER_CACHE_TTL`` — integer seconds (1-86400).
            Overrides ``openrouter.response_cache_ttl`` in config.yaml.

    *or_config* is the ``openrouter`` section from config.yaml.  When *None*,
    falls back to reading config from disk via ``load_config()``.
    Nr   load_config
openrouterHERMES_OPENROUTER_CACHEr9   response_cacheFr   zX-OpenRouter-CacheHERMES_OPENROUTER_CACHE_TTLra   iQ zX-OpenRouter-Cache-TTLresponse_cache_ttli,  )dict_OR_HEADERS_BASEhermes_cli.configr   rl   	Exceptionosenvironrf   rg   _TRUTHY_ENV_VALUESisdigitintstrr"   float)r   headersr   	env_cachecache_enabledenv_ttlttls          r   build_or_headersr     s    #$$G 	555555#)),;;II 	 	 	III	 
8"==CCEEKKMMI ?!%77!&6>> $*G ! jnn:B??EEGGG >?? 	=g,,CC    5     47HH01mm0#66cC<(( 	>Q#->->->->->->->->->03CHHG,-Ns   $= AA)__version__zHermesAgent/)r   zX-Title
User-Agenttagsproduct=hermes-agentauxiliary_is_nousz)https://inference-api.nousresearch.com/v1zhttps://api.anthropic.comz	auth.jsonz%https://chatgpt.com/backend-api/codexaccess_tokenc                    ddd}t          | t                    r|                                 s|S 	 ddl}|                     d          }t          |          dk     r|S |d         d	t          |d                    d
z  z  z   }t          j        |                    |                    }|	                    di           	                    d          }t          |t                    r|r||d<   n# t          $ r Y nw xY w|S )u  Headers required to avoid Cloudflare 403s on chatgpt.com/backend-api/codex.

    The Cloudflare layer in front of the Codex endpoint whitelists a small set of
    first-party originators (``codex_cli_rs``, ``codex_vscode``, ``codex_sdk_ts``,
    anything starting with ``Codex``). Requests from non-residential IPs (VPS,
    server-hosted agents) that don't advertise an allowed originator are served
    a 403 with ``cf-mitigated: challenge`` regardless of auth correctness.

    We pin ``originator: codex_cli_rs`` to match the upstream codex-rs CLI, set
    ``User-Agent`` to a codex_cli_rs-shaped string (beats SDK fingerprinting),
    and extract ``ChatGPT-Account-ID`` (canonical casing, from codex-rs
    ``auth.rs``) out of the OAuth JWT's ``chatgpt_account_id`` claim.

    Malformed tokens are tolerated — we drop the account-ID header rather than
    raise, so a bad token still surfaces as an auth error (401) instead of a
    crash at client construction.
    z!codex_cli_rs/0.0.0 (Hermes Agent)codex_cli_rs)r   
originatorr   N.   ra   =   zhttps://api.openai.com/authchatgpt_account_idzChatGPT-Account-ID)r"   r   rf   base64ri   lenjsonloadsurlsafe_b64decoderl   r   )r   r   r   partspayload_b64claimsacct_ids          r   _codex_cloudflare_headersr   h  s%   & :$ G lC(( 0B0B0D0D ""3''u::>>NAhU1X(:!;;F44[AABB**:B??CCDXYYgs## 	4 	4,3G()   Ns   -C2  BC2 2
C?>C?c                    t          | pd                                                              d          }|                    d          r9|dt	          d                    dz   }t
                              d||           |S d|v r8|                    d          r#|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.
    r9   rt   
/anthropicNz/v1u,   Auxiliary client: rewrote base URL %s → %sapi.kimi.com/codingu1   Auxiliary client: rewrote Kimi base URL %s → %s)r   rf   rstripendswithr   r{   r|   )ry   r7   	rewrittens      r   _to_openai_base_urlr     s     hn"


#
#
%
%
,
,S
1
1C
||L!! ,3|,,,,-5	CS)TTTi!8!8 %K	H#yYYYJr   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-   r   r{   r|   has_credentialsselect)r\   poolexcs      r   _select_pool_entryr     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 )Nr9   runtime_api_keyr   )getattrr   rf   )r   keys     r   _pool_runtime_api_keyr     sN    }r %*D
1
1
WWUNTV5W5WCsyb>>!!!r   r9   fallbackc                 H   | 6t          |pd                                                              d          S t          | dd           p#t          | dd           pt          | dd           p|}t          |pd                                                              d          S )Nr9   rt   runtime_base_urlinference_base_urlry   )r   rf   r   r   )r   r   r7   s      r   _pool_runtime_base_urlr     s    }8>r""((**11#666 	)400 	5.55	5*d++	 	  syb>>!!((---r   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).
    r9   typetext
input_textr   r   	image_urlr7   input_image)r   r   detailN)r   r   )r"   r   listr   rl   append)	r   	convertedpartptype
image_datar7   r   r   r   s	            r   _convert_content_for_responsesr     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?r   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_clientrr   c                 "    || _         || _        d S r   )_client_model)r   r   rr   s      r   __init__z!_CodexCompletionsAdapter.__init__  s    "r   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          pi }
t          |
t                    rn|
                     d          }t          |t                    rD|                     d          d
u rn,|                     dd          }|dk    rd}|dd|	d<   dg|	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	        dEi |	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!          |                     dFd-t&          d.t          d/t&          d0t&          fd1} t          |d$g           D ]}! | |!d          }"|"d&k    rB | |!d          pg D ]2}# | |#d          }$|$d2v r |                     | |#d3d                     3V|"d"k    rU|                    t%           | |!d4d          dt%           | |!dd           | |!d5d6          7          8                     t          |d9d           }%|%r?t%          t          |%d:d;          t          |%d<d;          t          |%d=d;          >          }n.# t(          $ r!}&t                              d?|&            d }&~&ww xY wd                    |                                          pd }t%          d'||pd @          }'t%          d;|'|sdAndBC          }(t%          |(g||D          S )GNmessagesrr   zYou are a helpful assistant.roleuserr   r9   system)r   r   F)rr   instructionsinputstore
extra_body	reasoningenabledeffortmediumminimallowr^   )r   summaryzreasoning.encrypted_contentincludetoolsfunctionnamedescription
parameters)r   r  r  r  r   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_textr   )r   r   statusr   z6Codex auxiliary: synthesized from %d deltas (%d chars)r#   r   defaultr   c                     t          | |d           }|+t          | t                    r|                     ||          }||n|S r   )r   r"   r   rl   )r#   r   r  vals       r   	_item_getz2_CodexCompletionsAdapter.create.<locals>._item_get  sG    c3--;:c4#8#8;''#w//C!oss7:r   )r  r   r   call_id	argumentsz{})r  r  )idr   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choicesrr   r  r   r   )rl   r   r"   r   r   r   r   r   	responsesstreamr   get_final_responser   r  r{   r|   r   joinr   r   r   rf   ))r   r   r   rr   r   
input_msgsmsgr   r   resp_kwargsr   reasoning_cfgr   r   r   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_typer   r   
resp_usager   r  choices)                                            r   createz_CodexCompletionsAdapter.create  sM   ::j"--

7DK00
 6+-
 		 		C7766**Dggi((.BGx*4Wc*B*BTwwG!! =gFF# #     (DV$C$C#D	'
 '
 ZZ--3
j$'' 	M&NN;77M-.. M $$Y//588
 *..xBBF **!&"(#)0 0K, /L-LK	* 

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>   S+ 1BK
>S+ 
KS+ KHS+ +
T5TTN)	r(   r)   r*   r+   r   r   r   r   r;  r   r   r   r   r     sb        > >F 3    s
# s
 s
 s
 s
 s
 s
r   r   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 r   completionsr   r>  s     r   r   z_CodexChatShim.__init__      "r   N)r(   r)   r*   r+   r   r   r   r   r   r=  r=    s6        HH# 8 # # # # # #r   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.
    r   rr   c                     || _         t          ||          }t          |          | _        |j        | _        |j        | _        d S r   )_real_clientr   r=  chatapi_keyry   )r   r   rr   r>  s       r   r   zCodexAuxiliaryClient.__init__  s@    '*;>>"7++	"*#,r   c                 8    | j                                          d S r   )rG  closer&   s    r   rK  zCodexAuxiliaryClient.close  s    !!!!!r   N)r(   r)   r*   r+   r   r   r   rK  r   r   r   rE  rE    sO         -F -3 - - - -" " " " "r   rE  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 r   _syncr   rN  s     r   r   z&_AsyncCodexCompletionsAdapter.__init__      !


r   r   c                 J   K   dd l } |j        | j        j        fi | d {V S Nr   asyncio	to_threadrQ  r;  r   r   rW  s      r   r;  z$_AsyncCodexCompletionsAdapter.create  A      &W&tz'8CCFCCCCCCCCCr   N)r(   r)   r*   r+   r   r   r   r;  r   r   r   rM  rM    s\         "%= " " " "D D D D D D Dr   rM  c                       e Zd ZdefdZdS )_AsyncCodexChatShimr>  c                     || _         d S r   r@  rB  s     r   r   z_AsyncCodexChatShim.__init__  rC  r   N)r(   r)   r*   rM  r   r   r   r   r\  r\    s0        # = # # # # # #r   r\  c                       e Zd ZdZddZdS )AsyncCodexAuxiliaryClientzHAsync-compatible wrapper matching AsyncOpenAI.chat.completions.create().sync_wrapperrE  c                     |j         j        }t          |          }t          |          | _         |j        | _        |j        | _        d S r   )rH  rA  rM  r\  rI  ry   r   r`  rN  async_adapters       r   r   z"AsyncCodexAuxiliaryClient.__init__  sA    #(45lCC'66	#+$-r   N)r`  rE  )r(   r)   r*   r+   r   r   r   r   r_  r_    s.        RR. . . . . .r   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.Fr   rr   is_oauthc                 0    || _         || _        || _        d S r   )r   r   	_is_oauth)r   r   rr   rf  s       r   r   z%_AnthropicCompletionsAdapter.__init__  s    "!r   r   c           	         ddl m} ddl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          }|                    || j
                  }t!          |j        |j        |j                  }|j        }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)get_transportr   rr   r   tool_choice
max_tokensmax_completion_tokensi  temperaturer   r9   r   r  >   r^   nonerequired)rr   r   r   rm  reasoning_configrl  rf  _forbids_sampling_paramsanthropic_messages)strip_tool_prefix)r   r  r   r  r  r  r  r  r  r   r   )agent.anthropic_adapterrj  agent.transportsrk  rl   r   r"   r   r   rg   rh  rt  r   r   r;  normalize_responser   r   r  r   r  hasattrr  r   )r   r   rj  rk  r   rr   r   rl  rm  ro  normalized_tool_choicechoice_typeanthropic_kwargsrt  response
_transport_nrassistant_messager  r  r  r  r  r:  s                           r   r;  z#_AnthropicCompletionsAdapter.create  s   BBBBBB222222::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"]#788
++ , 
 
 ,K~m
 
 

 )8W%% 	(. 	#HNNAFFK!M ' K K Pq"8>>1EEl-ZkJkL#+"3)  E !%'
 
 

 H
 
 
 	
r   NF)	r(   r)   r*   r+   r   r   boolr   r;  r   r   r   re  re    sj        FF" "C " "t " " " "
I
# I
 I
 I
 I
 I
 I
r   re  c                       e Zd ZdefdZdS )_AnthropicChatShimr>  c                     || _         d S r   r@  rB  s     r   r   z_AnthropicChatShim.__init__O  rC  r   N)r(   r)   r*   re  r   r   r   r   r  r  N  s0        # < # # # # # #r   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.Fr   rr   rI  ry   rf  c                 |    || _         t          |||          }t          |          | _        || _        || _        d S )Nrf  )rG  re  r  rH  rI  ry   )r   r   rr   rI  ry   rf  r>  s          r   r   z!AnthropicAuxiliaryClient.__init__V  s?    '.{EHUUU&w//	 r   c                 h    t          | j        dd           }t          |          r |             d S d S )NrK  )r   rG  callable)r   close_fns     r   rK  zAnthropicAuxiliaryClient.close]  s?    4,gt<<H 	HJJJJJ	 	r   Nr  )	r(   r)   r*   r+   r   r   r  r   rK  r   r   r   r  r  S  sf        JJ! !C ! !c !S !\` ! ! ! !    r   r  c                   &    e Zd ZdefdZdefdZdS )!_AsyncAnthropicCompletionsAdapterrN  c                     || _         d S r   rP  rR  s     r   r   z*_AsyncAnthropicCompletionsAdapter.__init__d  rS  r   r   c                 J   K   dd l } |j        | j        j        fi | d {V S rU  rV  rY  s      r   r;  z(_AsyncAnthropicCompletionsAdapter.createg  rZ  r   N)r(   r)   r*   re  r   r   r;  r   r   r   r  r  c  sR        "%A " " " "D D D D D D Dr   r  c                       e Zd ZdefdZdS )_AsyncAnthropicChatShimr>  c                     || _         d S r   r@  rB  s     r   r   z _AsyncAnthropicChatShim.__init__m  rC  r   N)r(   r)   r*   r  r   r   r   r   r  r  l  s0        # A # # # # # #r   r  c                       e Zd ZddZdS )AsyncAnthropicAuxiliaryClientr`  r  c                     |j         j        }t          |          }t          |          | _         |j        | _        |j        | _        d S r   )rH  rA  r  r  rI  ry   rb  s       r   r   z&AsyncAnthropicAuxiliaryClient.__init__r  sA    #(49,GG+M::	#+$-r   N)r`  r  )r(   r)   r*   r   r   r   r   r  r  q  s(        . . . . . .r   r  c                     | pd                                                                                     d          }|sdS |                    d          rdS t	          |          }|dk    rdS |dk    rd|v rdS dS )	u  True if the endpoint at ``base_url`` speaks the Anthropic Messages
    protocol instead of OpenAI chat.completions.

    Mirrors ``hermes_cli.runtime_provider._detect_api_mode_for_url`` so the
    auxiliary client and the main agent stay in sync on transport selection.
    Covers:

    - Any URL ending in ``/anthropic`` (MiniMax, Zhipu GLM, LiteLLM proxies,
      Anthropic-compatible gateways).
    - ``api.kimi.com/coding`` (Kimi Coding Plan — the /coding route only
      speaks Claude-Code's native Anthropic shape; ``chat.completions``
      returns 404 on Anthropic-only model aliases like ``kimi-for-coding``).
    - ``api.anthropic.com`` (native Anthropic).
    r9   rt   Fr   Tzapi.anthropic.comr   r   )rf   rg   r   r   r1   )ry   rm   hostnames      r   #_endpoint_speaks_anthropic_messagesr  z  s     .b''))//1188==J u<(( t ,,H&&&t>!!i:&=&=t5r   
client_objrI  api_modec                    t          | t                    r| S t          | t                    r| S 	 ddlm} t          | |          r| S n# t
          $ r Y nw xY w	 ddlm} t          | |          r| S n# t
          $ r Y nw xY w|r|dk    r| S |dk    pt          |          }|s| S 	 ddl	m
} n-# t
          $ r  t                              d|           | cY S w xY w	  |||          }	n5# t          $ r(}
t                              d||
           | cY d}
~
S d}
~
ww xY wt                              d	||r
|dd
         nd|pd           t          |	|||d          S )a  Rewrap a plain OpenAI client in ``AnthropicAuxiliaryClient`` when
    the endpoint actually speaks Anthropic Messages.

    This is the single chokepoint for aux-client transport correction.
    Runs at the end of every ``resolve_provider_client`` branch so that
    api_key providers (Kimi Coding Plan), the ``custom`` endpoint, and
    future /anthropic gateways all land on the right wire format
    regardless of which branch built the client.

    Returns ``client_obj`` unchanged when:

    - It's already an Anthropic/Codex/Gemini/CopilotACP wrapper.
    - The endpoint is an OpenAI-wire endpoint.
    - ``api_mode`` is explicitly set to a non-Anthropic transport.
    - The ``anthropic`` SDK is not installed (falls back to OpenAI wire).
    r   )GeminiNativeClientCopilotACPClientru  build_anthropic_clientu   Endpoint %s speaks Anthropic Messages but the anthropic SDK is not installed — falling back to OpenAI-wire (will likely 404).uT   Failed to build Anthropic client for %s (%s) — falling back to OpenAI-wire client.NzeAuxiliary transport: wrapping client in AnthropicAuxiliaryClient (model=%s, base_url=%s, api_mode=%s)<   r9   auto-detectedFr  )r6   r  rE  agent.gemini_native_adapterr  ImportErroragent.copilot_acp_clientr  r  rw  r  r{   warningr   r|   )r  rr   rI  ry   r  r  r  should_wrapr  r   r   s              r   _maybe_wrap_anthropicr    sZ   0 
$<== 
$899 BBBBBBJ(:;; 		   ======J(899 		     H 444 	(( 	9.x88   BBBBBBB   O	
 	
 	

 ,,Wh??   "#+S	
 	
 	
  LL	/0x}}b(2Mo  
 $UGX   sS   A	 	
AAA3 3
B ?B 'B. .'CCC) )
D3DDD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.
    nousNr   r9   refresh_token	agent_keyportal_base_url	client_idscope
token_typeBearerr   )	r   r  r  r   r  r  r  r  sourceactive_provider	providerszCould not read Nous auth: %s)r   r   r   _NOUS_DEFAULT_BASE_URL_AUTH_JSON_PATHis_filer   r   	read_textrl   r   r{   r|   )pool_presentr   datar\   r   s        r   _read_nous_authr    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  r   r9   rl   r\   s    r   _nous_api_keyr    s'    <<$$H^R(H(HHr   c                  6    t          j        dt                    S )z8Resolve the Nous inference base URL from env or default.NOUS_INFERENCE_BASE_URL)r   getenvr  r   r   r   _nous_base_urlr    s    9.0FGGGr   force_refreshr  c                 B   	 ddl m}  |t          dt          t	          j        dd                              t          t	          j        dd                    |           }n3# t          $ r&}t          	                    d	|           Y d
}~d
S d
}~ww xY wt          |                    d          pd                                          }t          |                    d          pd                                                              d          }|r|sd
S ||fS )a2  Return fresh Nous runtime credentials when available.

    This mirrors the main agent's 401 recovery path and keeps auxiliary
    clients aligned with the singleton auth store + mint flow instead of
    relying only on whatever raw tokens happen to be sitting in auth.json
    or the credential pool.
    r    resolve_nous_runtime_credentialsr  HERMES_NOUS_MIN_KEY_TTL_SECONDS1800HERMES_NOUS_TIMEOUT_SECONDS15min_key_ttl_secondstimeout_seconds
force_mintz7Auxiliary Nous runtime credential resolution failed: %sNrI  r9   ry   rt   )hermes_cli.authr  maxr   r   r  r   r   r{   r|   r   rl   rf   r   )r  r  credsr   rI  ry   s         r   _resolve_nous_runtime_apir    s7   
DDDDDD00 #BBI6WY_,`,`(a(a b b!"),I4"P"PQQ$
 
 

    NPSTTTttttt %))I&&,"--3355G599Z((.B//5577>>sCCH ( tHs   A"A% %
B/BB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.
    rd   r   )_read_codex_tokenstokensr   Nr   ra   r   r   expz-Codex access token expired (exp=%s), skippingz2Could not read Codex auth for auxiliary client: %s)r   r   r  r  rl   r"   r   rf   r   ri   r   r   r   r   timer{   r|   r   )r  r   tokenr  r  r  r   r   payloadr   r  r   s               r   _read_codex_access_tokenr  3  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                  J   	 ddl m} m} n+# t          $ r t                              d           Y dS w xY w|                                 D ]\  }}|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          |          \  }}|r	t          |          }|sst          ||j                  p|j        }t          |          }	t                              |          }
|
t                              d	|j        |
           |d
k    r$ddlm}m}  ||	          r |||	          |
fc S i }t+          |	d          rddi|d<   n#t+          |	d          rddlm}  |            |d<   t1          d||	d|}t3          ||
||          }||
fc S  ||          }t5          |                    dd                                                    }|st5          |                    dd                                                                        d          p|j        }t          |          }	t                              |          }
|
*t                              d|j        |
           |d
k    r$ddlm}m}  ||	          r |||	          |
fc S i }t+          |	d          rddi|d<   n#t+          |	d          rddlm}  |            |d<   t1          d||	d|}t3          ||
||          }||
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NNrI  rS   )!is_provider_explicitly_configuredNz'Auxiliary text client: %s (%s) via poolrH   r  is_native_gemini_base_urlrI  ry   r   r   claude-code/0.1.0default_headersapi.githubcopilot.com)copilot_default_headersr9   ry   rt   zAuxiliary text client: %s (%s)r   )r  r  r  r  r{   r|   rB   	auth_typer  _try_anthropicr   r   r   r   r   r   rl   r  r  r  r  r0   hermes_cli.modelsr  r   r  r   rf   r   )r  r  provider_idpconfigr  r  r   rI  raw_base_urlry   rr   r  r  extrar  r   r  s                    r   _resolve_api_key_providerr  ^  sF   [[[[[[[[[   NOOOzz !2 7 7 9 9 D DW	))+%%MMMMMM88EE    !#####0==e 	"+E22G 1%9STTrX_XrL*<88H044[AAE}LLBGLRWXXXh&&eeeeeeee,,X66 Y--gQQQSXXXXXE$X~>> E,8:M+N'((&x1HII EEEEEEE+B+B+D+D'(IWxII5IIG+GUG\RRGE>!!!44[AAeii	2..//5577 	599Z4455;;==DDSIIgWMg&|44,00===5w|UKKK(""aaaaaaaa((22 U))'HMMMuTTTT >:: 	A(46I'JE#$$"8-DEE 	AAAAAAA'>'>'@'@E#$E8EEuEE'NN~:s    $33#A66
BBexplicit_api_keyc                    t          d          \  }}|rp| pt          |          }|sdS t          |t                    pt          }t                              d           t          ||t                                t          fS | pt          j
        d          }|sdS t                              d           t          |t          t                                t          fS )Nr   r  z%Auxiliary client: OpenRouter via poolrI  ry   r  OPENROUTER_API_KEYzAuxiliary client: OpenRouter)r   r   r   r/   r{   r|   r   r   _OPENROUTER_MODELr   r  )r  r  r   or_keyry   s        r   _try_openrouterr    s    ,\::L% N!A%:5%A%A 	:)%1DEE\I\<===fx'7'9'9; ; ;<MN 	N @+?!@!@F z
LL/000&+>#3#5#57 7 78IJ Jr   c                      t          d          \  } }| r|dS t          |          sdS t          t          j        d          pd                                          sdS dS )	z>Return a more precise OpenRouter auth failure reason for logs.r   NzOOpenRouter credential pool has no usable entries (credentials may be exhausted)z=OpenRouter credential pool entry is missing a runtime API keyr  r9   zOPENROUTER_API_KEY not setz&no usable OpenRouter credentials found)r   r   r   r   r  rf   )r  r   s     r    _describe_openrouter_unavailabler    sw    ,\::L% S=dd$U++ 	SRRry-..4"55;;== ,++33r   visionc                 "   	 ddl m}  |            }|#|dk    rt                              d|           dS n# t          $ r Y nw xY wt                      }t          d          }||sdS dat                              d           t          }	 dd	l	m
}  || 
          }|r#|}t                              d| rdnd|           n t                              d| rdnd|           n8# t          $ r+}t                              d| rdnd||           Y d }~nd }~ww xY w||\  }	}
nVt          |pi           }	t          |pi                     d          pt                                                    d          }
t!          |	|
          |fS )Nr   )nous_rate_limit_remainingz?Auxiliary: skipping Nous Portal (rate-limited, resets in %.0fs)r  Fr  TzAuxiliary client: Nous Portal)get_nous_recommended_aux_modelr  z/Auxiliary/%s: using Portal-recommended model %sr  r   z:Auxiliary/%s: no Portal recommendation, falling back to %szGAuxiliary/%s: recommended-models lookup failed (%s); falling back to %sr   rt   r  )agent.nous_rate_guardr  r{   r|   r   r  r  r   _NOUS_MODELr  r  r  r   rl   r  r   r   )r  r  
_remainingr  runtimerr   r  recommendedr   rI  ry   s              r   	_try_nousr    s<   
CCCCCC..00
!j1nnLLQ   :    D'e<<<Gtz
LL0111 E
DDDDDD44FCCC 
	ELLA".   
 LLL".    
 
 
!*HHFC	
 	
 	
 	
 	
 	
 	
 	

 #
++
''(<==QAQAQRRYYZ]^^	
 	
 	
 	 s(   38 
AAAC( (
D2!DD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   r   rr   r  r9   )r   r   rl   r"   r   rf   r   r   )r   cfg	model_cfgr  s       r   _read_main_modelr
    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   r   rr   r\   r9   )	r   r   rl   r"   r   r   rf   rg   r   )r   r  r	  r\   s       r   rj   rj   .  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	          }t          |d          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_providerrb   )	requestedz6Auxiliary client: custom runtime resolution failed: %sNOPENAI_BASE_URLr9   rt   OPENAI_API_KEYNNN)ry   rI  ry   rI  r  openrouter.aino-key-required)hermes_cli.runtime_providerr  r   r{   r|   r"   r   r   r  rf   r   rl   r   r0   )r  r  r   openai_base
openai_keycustom_base
custom_keycustom_modes           r   _resolve_custom_runtimer  A  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 )Nr9   )r  )r  _s     r   _current_custom_base_urlr  s  s    /11KA"r   c            	      D   ddl m}  t                       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   r   )HTTPS_PROXY
HTTP_PROXY	ALL_PROXYhttps_proxy
http_proxy	all_proxyr9   z%Malformed proxy environment variable r   z1. Fix or unset your proxy settings and try again.N)urllib.parser   r2   r   r   r   rl   rf   schemeport
ValueErrorRuntimeError)r   r   valuerC   r  r   s         r   _validate_proxy_env_urlsr,  x  s     &%%%%%:  BJNN3''-2..4466 		Xe__F}  K 	 	 	B B Be B B B  	 s   A77
B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  r9   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   r   rf   rh   r'  r(  r)  r*  )ry   r   	candidaterC   r  r   s         r   _validate_base_urlr1    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           t          |          \  }}|rd|ini }|dk    r!t          d||d|}t          ||          |fS |d	k    re	 d
dlm}	  |	||          }n;# t          $ r. t                              d           t          d||d||fcY S w xY wt          ||||d          |fS t          d||d|}
t!          |
||||          }
|
|fS )Nr   r  gpt-4o-miniz3Auxiliary client: custom endpoint (%s, api_mode=%s)chat_completionsdefault_querycodex_responsesr  ru  r   r  u|   Custom endpoint declares api_mode=anthropic_messages but the anthropic SDK is not installed — falling back to OpenAI-wire.Fr  r   )r  r   rg   rh   _CODEX_AUX_BASE_URLr
  r{   r|   rF   r   rE  rw  r  r  r  r  r  )r  r  r  r  rr   _clean_base_dq_extrar   r  _fallback_clients              r   _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0==K'*2os##F'''PZ+PPPP#K77>>***	UFFFFFF00[IIKK 	U 	U 	UNNR   M*{MMfMMuTTTT	U %[%[[`aaa
 	
 Qj;QQ&QQ,%[+  U""s   ,C? ?5D76D7c                    | st                               d           dS t          d          \  }}|rHt          |          }|rt	          |t
                    pt
          }n3t                      }|sdS t
          }nt                      }|sdS t
          }t                               d|            t          ||t          |                    }t          ||           | fS )a  Build a CodexAuxiliaryClient for an explicitly-requested model.

    There is no auto-selection of the Codex model: the ChatGPT-account
    Codex endpoint's accepted model list is an undocumented, drifting
    allow-list, so any hardcoded default we pick goes stale.  The caller
    is responsible for passing the model (e.g. from the user's own
    ``model.model`` or ``auxiliary.<task>.model`` config).

    Returns (None, None) when no Codex OAuth token is available.
    zxAuxiliary client: openai-codex requested without a model; pass model explicitly (auxiliary.<task>.model in config.yaml).r  rd   z4Auxiliary client: Codex OAuth (%s via Responses API)r  )r{   r  r   r   r   r7  r  r|   r   r   rE  )rr   r  r   codex_tokenry   r   s         r   _build_codex_clientr?    s     M	
 	
 	
 z,^<<L% '+E22 	+-e5HII`M`HH244K "!z*HH.00 	:&
LLGOOO1+>>  K
  U33U::r   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   )r  resolve_anthropic_tokenr  rS   r   rr   r\   r9   ry   rt   )_is_oauth_tokenr   z8Auxiliary client: Anthropic native (%s) at %s (oauth=%s)r  )rw  r  rA  r  r   r   r   _ANTHROPIC_DEFAULT_BASE_URLr   r   rl   r"   r   r   rf   rg   r   r   rB  r   r{   r|   r  )r  rA  r  r   r  ry   r   r  r	  cfg_providercfg_base_urlrB  rf  rr   r   s                  r   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r   r  local/customapi-keyr  r  r<  r  )r\   rr   ry   rI  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\   )r"   r   _MAIN_RUNTIME_FIELDSrl   r   rf   rg   )rI  rm   fieldr+  r\   s        r   _normalize_main_runtimerM  6  s    lD)) 	!#J% . .  ''eS!! 	.ekkmm 	. %Ju~~j))H 2!)!1!1
:r   c                  F    dt           fdt          fdt          fdt          fgS )aB  Return the ordered provider detection chain.

    Built at call time (not module level) so that test patches
    on the ``_try_*`` functions are picked up correctly.

    NOTE: ``openai-codex`` is deliberately NOT in this chain.  The
    ChatGPT-account Codex endpoint only accepts a shifting, undocumented
    allow-list of model IDs, so falling back to it with a guessed model
    fails more often than not.  Codex is used only when the user's main
    provider *is* openai-codex (see Step 1 of ``_resolve_auto``) or when
    a caller explicitly requests it with a model.
    r   r  rF  rG  rH  r   r   r   _get_provider_chainrO  E  s0     
'		-.	-.	 r   r   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)rR  i  Nc              3       K   | ]}|v V  	d S r   r   r=   kw	err_lowers     r   	<genexpr>z$_is_payment_error.<locals>.<genexpr>g  s?       @ @2rY @ @ @ @ @ @r   )creditszinsufficient fundszcan only affordbillingzpayment requiredF)r   r   rg   anyr   r  rV  s     @r   _is_payment_errorr\  Z  s     S-..F}}tC  I !!! @ @ @ @ *? @ @ @ @ @ 	 45r   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 r   r   )r=   rU  err_types     r   rW  z'_is_connection_error.<locals>.<genexpr>|  s'      
L
Lb2>
L
L
L
L
L
Lr   )
ConnectionTimeoutDNSSSLc              3       K   | ]}|v V  	d S r   r   rT  s     r   rW  z'_is_connection_error.<locals>.<genexpr>  s7        r2?      r   )zconnection refusedzname or service not knownzno route to hostznetwork is unreachablez	timed outzconnection resetF)	r   r^  r_  r"   r   r(   rZ  r   rg   )r   r^  r_  rV  ra  s      @@r   _is_connection_errorrg  n  s     ;:::::::#*O<== tCyy!H

L
L
L
L$K
L
L
LLL tC  I
     &      
 t5r   c                     t          | dd          }|dk    rdS t          |                                           }d|v p'dt          |           j                                        v S )zCDetect auth failures that should trigger provider-specific refresh.rQ  Ni  Tzerror code: 401authenticationerror)r   r   rg   r   r(   r[  s      r   _is_auth_errorrj    sa    S-..F}}tC  I	)`-Bd3iiFXF^F^F`F`-``r   paramc                     |pd                                 }|sdS t          |                                            |vrdS t          fddD                       S )a/  Detect provider 400s for an unsupported request parameter.

    Different OpenAI-compatible endpoints phrase the same class of error a few
    ways: ``Unsupported parameter: X``, ``unsupported_parameter`` with a
    ``param`` field, ``X is not supported``, ``unknown parameter: X``,
    ``unrecognized request argument: X``.  We match on both the parameter
    name and a generic "unsupported/unknown/unrecognized parameter" marker so
    call sites can reactively retry without the offending key instead of
    surfacing a noisy auxiliary failure.

    Generalizes the temperature-specific detector that originally shipped
    with PR #15621 so the same retry strategy can cover ``max_tokens``,
    ``seed``, ``top_p``, and any future quirk. Credit @nicholasrae (PR #15416)
    for the generalization pattern.
    r9   Fc              3       K   | ]}|v V  	d S r   r   )r=   markerrV  s     r   rW  z2_is_unsupported_parameter_error.<locals>.<genexpr>  s8       	 	vv" 	 	 	 	 	 	r   )zunsupported parameterunsupported_parameterznot supportedzdoes not supportzunknown parameterzunrecognized request argumentzunrecognized parameterzinvalid parameter)rg   r   rZ  )r   rk  param_lowerrV  s      @r   _is_unsupported_parameter_errorrq    s      ;B%%''K uC  I)##u 	 	 	 	 	2 	 	 	 	 	 	r   c                 "    t          | d          S )zBack-compat wrapper: detect API errors where the model rejects ``temperature``.

    Delegates to :func:`_is_unsupported_parameter_error`; kept as a separate
    public symbol because existing tests and call sites import it by name.
    ro  )rq  )r   s    r   !_is_unsupported_temperature_errorrs    s     +3>>>r   c                    t          |           t          5  fdt          D             }|D ]}t                              |d          d         }|Kt	          |           	 t          |dd          }t          |          r
 |             n# t          $ r Y nw xY wt                              |d           	 ddd           dS # 1 swxY w Y   dS )zEDrop cached auxiliary clients for a provider so fresh creds are used.c                 `    g | ]*}t          t          |d                              k    (|+S r<   )rp   r   )r=   r   rm   s     r   
<listcomp>z)_evict_cached_clients.<locals>.<listcomp>  s@     
 
 
&s3q6{{33zAA AAAr   r  r   NrK  )	rp   _client_cache_lock_client_cacherl   _force_close_async_httpxr   r  r   pop)r\   
stale_keysr   clientr  rm   s        @r   _evict_cached_clientsr}    sR   (22J	 ) )
 
 
 
(
 
 

  
	) 
	)C"&&s,>??BF!(000&vw==H)) # 


    Dc4((((
	)) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) )s6   A
C#*BC
BCBCCCc                     t          |           }	 |dk    r\ddlm}  |d          }t          |                    dd          pd                                          sdS t          |           dS |d	k    rdd
lm}  |t          dt          t          j        dd                              t          t          j        dd                    d          }t          |                    dd          pd                                          sdS t          |           dS |dk    rddlm}m}m}  |            }t#          |t$                    r |                    d          r ||          nd}t          |pd                                          s
 |            }t          |pd                                          sdS t          |           dS n4# t&          $ r'}	t(                              d||	           Y d}	~	dS d}	~	ww xY wdS )zERefresh short-lived credentials for OAuth-backed auxiliary providers.rd   r   )!resolve_codex_runtime_credentialsTr  rI  r9   Fr  r  r  r  r  r  r  r  rS   )read_claude_code_credentials_refresh_oauth_tokenrA  refreshTokenNz7Auxiliary provider credential refresh failed for %s: %s)rp   r  r  r   rl   rf   r}  r  r  r   r   r  r   rw  r  r  rA  r"   r   r   r{   r|   )
r\   rm   r  r  r  r  r  rA  r  r   s
             r   _refresh_provider_credentialsr    sn   (22J"''IIIIII55DIIIEuyyB//5266<<>> u!*---4HHHHHH44$'C	:[]c0d0d,e,e$f$f %bi0Mt&T&T U U  E
 uyyB//5266<<>> u!*---4$${{{{{{{{{{0022E3=eT3J3JruyyYgOhOhr((///nrEu{##))++ 2//11u{##))++ u!*---4 %    NPZ\_```uuuuu 5s8   AG "G 3BG G %B!G G 
H$HH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.
    r   r  rd   rF  )r   r  rd   rc   rb   rF  c                 <    h | ]}                     ||          S r   r  )r=   s_alias_to_labels     r   	<setcomp>z(_try_payment_fallback.<locals>.<setcomp>  s)    HHHq,,Q22HHHr   Nu2   Auxiliary %s: %s on %s — falling back to %s (%s)callr  z<Auxiliary %s: %s on %s and no fallback available (tried: %s), )NNr9   )
rg   rf   rj   addrO  r{   infor   r  r%  )r  r  r  skipmain_providerskip_labelsskip_chain_labelstriedlabeltry_fnr|  rr   r  s               @r   _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   >r   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                      }
|	rp|
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. User's main provider + main model, regardless of provider type.
         This means auxiliary tasks (compression, vision, web extraction,
         session search, etc.) use the same model the user configured for
         chat.  Users on OpenRouter/Nous get their chosen chat model; users
         on DeepSeek/ZAI/Alibaba get theirs; etc.  Running aux tasks on the
         user's picked model keeps behavior predictable — no surprise
         switches to a cheap fallback model for side tasks.
      2. OpenRouter → Nous → custom → Codex → API-key providers (fallback
         chain, only used when the main provider has no working client).
    Fr\   r9   rr   ry   rI  r  r  rb   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^   r9   N)explicit_base_urlr  r  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.r  )r   rM  rl   _stale_base_url_warnedr   r  rf   rj   rh   r{   r  r
  resolve_provider_clientr  rO  r%  r   )rI  r  runtime_providerruntime_modelr   r   runtime_api_mode	_env_base_cfg_providerr  
main_modelresolved_providerr  r  r|  resolvedr  r  r  rr   s                       r   _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##% % % :r   	is_visionc                    ddl m} t          | t                    rt	          |           |fS t          | t
                    rt          |           |fS 	 ddlm}m	} t          | |          r ||           |fS n# t          $ r Y nw xY w	 ddlm} t          | |          r| |fS n# t          $ r Y nw xY w| j        t          | j                  d}t          | j                  }t!          |d          rt#                      |d<   n>t!          |d          rdd	lm}	  |	d
|          |d<   nt!          |d          rddi|d<    |di ||fS )aa  Convert a sync client to its async counterpart, preserving Codex routing.

    When ``is_vision=True`` and the underlying base URL is Copilot, the
    resulting async client carries the ``Copilot-Vision-Request: true``
    header so the request is routed to Copilot's vision-capable
    infrastructure (otherwise vision payloads silently time out).
    r   )AsyncOpenAI)r  AsyncGeminiNativeClientr  r  r  r  r  copilot_request_headersTis_agent_turnr  r   r   r  r   )r   r  r"   rE  r_  r  r  r  r  r  r  r  r  rI  r   ry   r0   r   hermes_cli.copilot_authr  )
sync_clientrr   r  r  r  r  r  async_kwargssync_base_urlr  s
             r   _to_async_clientr    s    #"""""+344 =(55u<<+788 A,[995@@[[[[[[[[k#566 	?**;77>>	?   ======k#344 	&%%	&    &,-- L ,--M]O<< 	N*:*<*<&''	}.E	F	F NCCCCCC*A*A)+
 +
 +
&'' 
}n	=	= N+79L*M&';&&&&--s$   $A: :
BBB& &
B32B3
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  r   )r  r\   r  s      r   _normalize_resolved_modelr    sa     KKKKKK++JAAA   s    ''
async_mode	raw_codexr  c	                 ~  A t                       | pd                                                                }	t          |           } dt          dt          dt
          ffdA	 	 d`dt          dt          dt          fA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    rht          |          \  }}|)t                              dt                                 dS t          |p||           }|rt          |||          n||fS | dk    r|t                                          v p+|pd                                                                dk    }t!          |          \  }}|t                              d           dS t          |p||           }|rt          |||          n||fS | dk    r|st                              d           dS rdt#                      }|st                              d           dS t          ||           }t%          |t&          t)          |                    }||fS t+          |          \  }}|t                              d           dS t          |p||           }|rt          |||          n||fS | dk    r|r7t-          |                                          }|pd                                p(t/          j        dd                                          pd}|st                              d           dS t          |p|r|                    d          ndpd |           }i }t5          |          \  }}|r||d!<   t7          |d"          rd#d$i|d%<   n&t7          |d&          rd'd(lm}  |d)|*          |d%<   t%          da||d+|} |
||||          }|rt          |||          n||fS t<          t>          fD ]} |            \  }}|zt          |p||           }t	          tA          |d,d          pd          }t	          tA          |d-d          pd          } |
||||          }|rt          |||          n||fc S t                              d.           dS 	 d'd/l!m"} d}|	r|	| k    r ||	          }| ||           }|r|                    d,d                                          }|                    d-d                                          }|                    d0d                                          }|s)|r't/          j        |d                                          }|pd}p|                    d1          pd                                }|rt          |p=|                    d          p(|r|                    d          ndptG                      pd |           }|d2k    r|}|} nt-          |          }|} t5          |          \  }!}"|"rd!|"ini }#t                              d3| ||pd4           |d2k    r	 d'd5l$m%}$  |$||          }%n{# tL          $ rn t                              d6|            t-          |          }&t5          |&          \  }'}(|(rd!|(ini })t%          da||'d+|)}|rt          |||          n||fcY S w xY wtO          |%|||d78          }*|rtQ          |*          |fS |*|fS t%          da||!d+|#}|d9k    r&tS          |tT                    stU          ||          }n |
||| |          }|rt          |||          n||fS t                              d:|            dS n# tL          $ r Y nw xY w	 d'd;l+m,}+m-},m.}- n,# tL          $ r t                              d<|            Y dS w xY w|+                    |           }.|.t                              d=|            dS |.j/        d-k    r1| d>k    rYta                      \  }}/|t                              d?           dS t          |p|/|           }|rt          |||          n||fS  |,|           }0t	          |0                    d-d                                                    }1|r|                                p|1}1|1s`tc          |.j2                  }2| d@k    r|23                    dA           t                              dB| dC4                    |2                     dS t	          |0                    d,d                                                    5                    d          p|.j6        }3t-          |3          }4|r4t-          |                                5                    d                    }4tn                              | d          }/t          |p|/|           }| dDk    rTd'dEl8m9}5m:}6  |6|4          rA |5|1|4+          }t                              dF| |           |rt          |||          n||fS i }7t7          |4d"          rd$|7d#<   n6t7          |4d&          r&d'd(lm} |7;                     |d)|*                     t%          da|1|4d+|7rd%|7ini }| d@k    rR|rPsN	 d'dGl<m=}8  |8|          r+t                              dH|           tU          ||          }n# tL          $ r Y nw xY w |
|||3|1          }t                              dF| |           |rt          |||          n||fS |.j/        dIk    r |-|           }0t          |p&|r|                    d          ndptG                      |           }| dJk    rLt	          |0                    d-d                                                    }1t	          |0                    d,d                                                    }4t	          |0                    dKd                                                    pd}9tc          |0                    dL          pg           }:|st                              dM           dS |1r|4st                              dN           dS d'dOl>m?};  |;|1|4|9|:P          }t                              dF| |           |rt          |||          n||fS t                              dQ|            dS |.j/        dRk    r
	 d'dSl@mA}<mB}= d'dTl$mC}> n+# tL          $ r t                              dU           Y dS w xY w |<            st                              dV           dS  |=            }?dW}/t          |p|/|           }	  |>|?          }%n3# tL          $ r&}@t                              dX|@           Y d}@~@dS d}@~@ww xY wtO          |%|dYdZ|? d[+          }t                              d\||?           |rt          |||          n||fS |.j/        d]v rK| dk    rt          d||          S | dk    rt          d||          S t                              d^|            dS t                              d_|.j/        |            dS )bu  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.
    r9   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 t          |          dk    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.
        Fr6  Tapi.openai.comr9   rc   )r"   rE  r1   rg   )r  r  r  model_lowerr  r  s       r   _needs_codex_wrapz2resolve_provider_client.<locals>._needs_codex_wrap  s     j"677 	5 	5(((4 	$5555\**.>>>$?1133K+%%tur   final_model_strapi_key_strc                      | ||          r;t                               dpd||r
|dd         nd           t          | |          S t          | |||          S )a@  Wrap a plain OpenAI client in the correct transport adapter.

        Handles two cases:
        - ``CodexAuxiliaryClient`` when the endpoint needs the Responses API
          (explicit ``api_mode=codex_responses`` or api.openai.com + codex
          model name).
        - ``AnthropicAuxiliaryClient`` when the endpoint speaks Anthropic
          Messages (explicit ``api_mode=anthropic_messages``, any ``/anthropic``
          suffix, ``api.kimi.com/coding``, or ``api.anthropic.com``).

        Clients that are already specialized wrappers pass through unchanged.
        zeresolve_provider_client: wrapping client in CodexAuxiliaryClient (api_mode=%s, model=%s, base_url=%s)r  Nr  r9   )r{   r|   rE  r  )r  r  r  r  r  r  s       r   _wrap_if_neededz0resolve_provider_client.<locals>._wrap_if_needed  s     ZGG 	ELL7+O_%19SbS!!r	; ; ;
 (
ODDD %lH
 
 	
r   r^   rI  Nr  rt   z\Dropping OpenRouter-format model %r for non-OpenRouter auxiliary provider (using %r instead)r  r   )r  z4resolve_provider_client: openrouter requested but %sr  zmimo-v2-omnir   zYresolve_provider_client: nous requested but Nous Portal not configured (run: hermes auth)rd   zresolve_provider_client: openai-codex requested without a model; pass model explicitly (e.g. model.model in config.yaml or auxiliary.<task>.model for per-task aux routing).zbresolve_provider_client: openai-codex requested but no Codex OAuth token found (run: hermes model)r  rb   r  r  zQresolve_provider_client: explicit custom endpoint requested but base_url is emptyrr   r3  r5  r   r   r  r  r  r   r  Tr  r  ry   rI  zPresolve_provider_client: custom/main requested but no endpoint credentials found)_get_named_custom_providerkey_envr  ru  zCresolve_provider_client: named custom provider %r (%s, api_mode=%s)r4  r  u   Named custom provider %r declares api_mode=anthropic_messages but the anthropic SDK is not installed — falling back to OpenAI-wire.Fr  r6  zAresolve_provider_client: named custom provider %r has no base_url)r  r  -resolve_external_process_provider_credentialsz-hermes_cli.auth not available for provider %sz,resolve_provider_client: unknown provider %rrS   zOresolve_provider_client: anthropic requested but no Anthropic credentials foundrT   zgh auth tokenzJresolve_provider_client: provider %s has no API key configured (tried: %s)r  rH   r  z resolve_provider_client: %s (%s))!_should_use_copilot_responses_apiud   resolve_provider_client: copilot model %s needs Responses API — wrapping with CodexAuxiliaryClientexternal_processrU   commandr   zVresolve_provider_client: copilot-acp requested but no model was provided or configuredz^resolve_provider_client: copilot-acp requested but external process credentials are incompleter  )rI  ry   r  r   zLresolve_provider_client: external-process provider %s not directly supportedaws_sdk)has_aws_credentialsresolve_bedrock_region)build_anthropic_bedrock_clientzSresolve_provider_client: bedrock requested but boto3 or anthropic SDK not installedzGresolve_provider_client: bedrock requested but no AWS credentials foundz(anthropic.claude-haiku-4-5-20251001-v1:0z9resolve_provider_client: cannot create Bedrock client: %szaws-sdkzhttps://bedrock-runtime.z.amazonaws.comz)resolve_provider_client: bedrock (%s, %s))oauth_device_codeoauth_externalzMresolve_provider_client: OAuth provider %s not directly supported, try 'auto'z6resolve_provider_client: unhandled auth_type %s for %s)r9   r9   r   )Er,  rf   rg   rp   r   r  r  r{   r|   r  r  r  r  r  r   valuesr  r  r   r7  r   r?  r   r   r  rl   rF   r0   r  r  r<  r  r   r  r  r
  rw  r  r  r  r  r"   rE  r  r  r  r  r  r  r   api_key_env_varsr   r%  r   r   r   r  r  r  updater  r  r  r  agent.bedrock_adapterr  r  r  r  )Br\   rr   r  r  r  r  r  rI  r  original_providerr  r|  r  final_modelr  
_is_visionr>  
raw_clientr  r  r  r8  r9  r  r  _cbase_ckeyr  custom_entrycustom_key_enventry_api_moder  raw_base_for_wrap_clean_base2_dq2_extra2r  r   _fallback_base	_fb_clean_fb_dq	_fb_extrasync_anthropicr  r  r  r  default_modelr  rI  tried_sourcesr  ry   r  r  r   r  r  r   r  r  r  r  regionr   r  sB      `  `                                                          @r   r  r    s   R  "R..006688&x00HC C D       , OQ+-
 
S 
 
%(
 
 
 
 
 
 
8 6(lCCC>:
  	SE\\h\3h3F3FLL89>J J J E'xNX + 	JJJJk*	, <);KLLL>NNF022   :/0@(KKNX + 	JJJJk*	, 6 ,33555 ?""$$**,,> 	 $:666>NN O P P P:/0@(KKNX + 	JJJJk*	, >!! 	NNG  
 : 	- 344K "  T U U U!z3E8DDK#, 9+ F F  J
 ,,-e44>NN P Q Q Q:/0@(KKNX + 	JJJJk*	, 8 	0-.?@@FFHHK!'R..00 %9-r2288::%$ 
  ",   "z3_|M,**7333_R_ K E8EEK -),o&$[.AA ,8:M+N'((&{4KLL KKKKKK+B+B"&), , ,'( NJNNNNF$_V[+zRRFR\ /$V[INNNN +.0 ,-FG 	4 	4F$fhhOFG!78H(SSWVZ<<BCCGFIr::@bAA(feLLV` 3(	RRRR$k24 4 4 " 	 ; 	< 	< 	<z^JJJJJJ  	I!2h!>!>556GHHL55h??L N	&**:r::@@BBK%)))R88>>@@J)--i<<BBDDN C. CY~r::@@BB
#8'8J 'L,*:*::*F*FL"SSUUN @47 %#''00%5AK((111t% ())% %  "%999"-K(3%%"5k"B"BK(3%%>{%K%K"d59A?D11rYk>+O=OQ Q Q "%999<RRRRRR&<&<Z&U&U& < < <I %	   *=[)I)I,En,U,U)	6AG$O_f$=$=R	!'!\
Y!\!\R[!\!\^h !; 0PY Z Z Z Z&,k%:< < << &>#[*kTY& & &N " Z<^LLkYY);66U
\UUWUU
 "%666z0@ @6 2&+FFFF,_V[BSU_``FV` 3(	RRRR$k24NNS   :]N	^    	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	

    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UKR\w$V[INNNNcikvbwx44X>>eii	2..//5577
  	:&,,..9'G 	 !9::M9$$$$_555LL 6!499]#;#;= = = :599Z4455;;==DDSIIgWMg&|44  	R*+<+B+B+D+D+K+KC+P+PQQH4882FF/0FQQxaaaaaaaa((22 4++GhOOO?;WWWV` 3(	RRRR$k24  >:: 	$7GL!!"8-DEE 	GGGGGGNN22"i       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    !lGLL7;OOONX + 	JJJJk*	, ...==hGG/ "-9C  )))t"!!	
 
 }$$%))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SR\ /$V[INNNN +.0 ,-5	7 	7 	7z		i	'	'	YYYYYYYYNNNNNNN 	 	 	NN B C C C::	
 #"$$ 	LL 4 5 5 5:''))B/0FQQ	88@@KK 	 	 	NN (),. . .:::::	 *iFFFF
 
 
 	@+vVVVNX + 	JJJJk*	, 
	E	E	Ev*65*EEE~%%*>5*MMM 89A	C 	C 	Cz
NNK$h0 0 0:s   F)^; Z ^; A5\^; \)^; 8^; <A ^; ^; ;
__
_ %` ?` %<l" "
l/.l/?u $u65u6>w
 

w:w55w: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)rr   r  r  r  rI  _resolve_task_provider_modelr  r  rI  r\   rr   ry   rI  r  s          r   get_text_auxiliary_clientr  	  sM     4PPTP\X\3]3]0HeXw"" !   r   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)rr   r  r  r  r  rI  r  r  s          r   get_async_text_auxiliary_clientr  	  sP     4PPTP\X\3]3]0HeXw"" !   r   )r   r  c                      t          |           S r   )rp   r  s    r   _normalize_vision_providerr  
  s    "8,,,r   c                 (   t          |           } | dk    rt          d|d          S | dk    rt                      S | dk    rt          d          S | dk    rt          d|d          S | dk    rt	                      S | d	k    rt                      S d
S )NrT   Tr  r   r  r   rd   rS   rb   r  )r  r  r  r  r  r<  )r\   rr   s     r   _resolve_strict_vision_backendr  	
  s     *(33H9&y%4HHHH<   6%%%%>!! '~uMMMM;8#%%%:r   c                 0    t          |           d         d uS rU  )r  r  s    r    _strict_vision_backend_availabler   
  s    )(33A6dBBr   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  )rj   _VISION_AUTO_PROVIDER_ORDERr  r   r  r
  )	availabler  r|  r  ps        r   get_available_vision_backendsr  $
  s     I'))M 0l::777/>> 0  ////?O?Q?QRRIFA!  ///(    I"B1"E"EQr   )ry   rI  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    r@t                      }t                      }|r|dvrt          	                    ||          }|dk    rCt          ||          \  }}|-t                              d||pp|            |	|||          S nj|t          v rt                              d|           nEt          |||d          \  }}|-t                              d||p|            |	|||p|          S t          D ],}||k    r	t          |          \  }}| |	|||          c S -t                              d           dS |t          v r t          |          \  }} |	|||          S t!          ||d          \  }
}|
|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.
    r  r  r  r  c                 \    || d d fS p|}rt          ||d          \  }}| ||fS | ||fS )NTr  )r  )r  r  r  r  async_clientasync_modelr  resolved_models         r   	_finalizez1resolve_vision_provider_client.<locals>._finalizeQ
  s]    $dD00$5 	@(8k]a(b(b(b%L+$lK?? +{::r   rb   )rr   r  r  r  r  N)rb   NNr^   r  r  z/Vision auto-detect: using main provider %s (%s)ui   Vision auto-detect: skipping main provider %s (no vision support) — falling through to aggregator chainT)r  r  z'Auxiliary vision client: none availabler  )r  r  r   r   r   r  rj   r
  r   rl   r  r{   r  r   r|   r  _get_cached_client)r\   rr   ry   rI  r  r  resolved_base_urlresolved_api_keyresolved_api_moder  r|  r  r  r  vision_modelr  r  
rpc_client	rpc_modelr0  r  s       `               @r   resolve_vision_provider_clientr  =
  s&    Yu(E8WY YUI~02BDU +955I;S ;s ;8TW= ; ; ; ; ; ; ;  -5 !/-&
 
 
 >'',,F ,--%''
 $	N],>>266}jQQL&&-K!<. .*] *KKI%}'T'T*   %9]KOOO + ";;; N!    )@!<.")$ )$ )$%
I )KKI%y'@L   %9%z93LN N N
 5 	H 	HIM)))G	)R)R&K& yKGGGGG ' 	>???///%C~&
 &
"] yK???,Y
6G7;= = =FK ~$$$fk))r   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   r   NOUS_EXTRA_BODYr   r   r   get_auxiliary_extra_bodyr  
  s     %6=4   2=r   r+  c                     t                      }t          j        d          }|s%t                      t	          |          dk    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.
    r  Nr  rn  rm  )r  r   r  r  r1   )r+  r  r  s      r   auxiliary_max_tokens_paramr  
  s]     +,,KY+,,F 0!!)!+..2BBB'//%  r   rx  @   )ry   rI  r  rI  r  c                    t          |          | dk    r t          fdt          D                       nd}| ||pd|pd|pd||fS )Nr^   c              3   D   K   | ]}                     |d           V  dS )r9   Nr  )r=   rL  r  s     r   rW  z$_client_cache_key.<locals>.<genexpr>
  s1      QQ5E2..QQQQQQr   r   r9   )rM  tuplerK  )	r\   r  ry   rI  r  rI  r  runtime_keyr  s	           @r   _client_cache_keyr  
  si     &l33GU]agUgUg%QQQQ<PQQQQQQmoKj(.b'-RRQ\^ghhr   
bound_loop	cache_keyr|  r  r  c                Z   t           5  t                              |           }|a|d         |urWt          |d                    	 t	          |d         dd           }t          |          r
 |             n# t          $ r Y nw xY w|||ft          | <   d d d            d S # 1 swxY w Y   d S )Nr   rK  )rw  rx  rl   ry  r   r  r   )r  r|  r  r  	old_entryr  s         r   _store_cached_clientr  
  s    	 
G 
G!%%i00	 Yq\%?%?$Yq\222"9Q<$??H%% HJJJ   $*M:#Fi 
G 
G 
G 
G 
G 
G 
G 
G 
G 
G 
G 
G 
G 
G 
G 
G 
G 
Gs5   <B 0A65B 6
B B BB  B$'B$cache_providerc           	      H   t          d          }|d|fS |\  }	}
t          |	|
          }|}d}|rB	 ddl}|                                }n# t          $ r Y nw xY wt          ||pd|          \  }}n|}t          | ||||||          }t          ||||	           ||fS )
zLRefresh Nous runtime creds, rebuild the client, and replace the cache entry.Tr  Nr  r   r9   r  r  ry   rI  r  rI  r  r  )r  r   rW  get_event_loopr*  r  r  r  )r  rr   r  ry   rI  r  rI  r  r  	fresh_keyfresh_base_urlr  r  current_loop_aior|  r  s                    r   _refresh_nous_auxiliary_clientr&    s    (d;;;GU{ 'I~^DDDKKL 	""""..00LL 	 	 	D	.{K<M2Ybccc!!  I FKLQQQQ;s   A 
AA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 r   r   r&   s    r   <lambda>z(neuter_async_httpx_del.<locals>.<lambda>L  s    t r   N)openai._base_clientr)  __del__r  AttributeErrorr(  s    r   neuter_async_httpx_delr/  0  sR    4??????*;*;'''(   s    ''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   )ClientStater   N	is_closedT)httpx._clientr1  r   CLOSED_stater   )r|  r1  inners      r   ry  ry  Q  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   NrK  )
inspectrw  r   rx  rB   ry  r   iscoroutinefunctionr   clear)r8  r   r   r|  r  s        r   shutdown_cached_clientsr;  e  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)rw  rx  rB   r2  ry  r   )r{  r   r   r|  _defaultcached_loops         r   cleanup_stale_async_clientsr?    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 ]5}|r1t          t          t          |dd          pd          d          r dS 6dS )Nr   r|  ry   r9   r  TF)r   r0   r   )r|  r#   s     r   _is_openrouter_clientrA    sr    	488'&(TX:Y:YZ   	(WS*b-I-I-OR)P)PRabb 	445r   cached_defaultc                 J    t          |           rdS t          |od|v           S )zFBest-effort check for cached clients that accept ``vendor/model`` IDs.Trt   )rA  r  )r|  rB  s     r   #_cached_client_accepts_slash_modelsrD    s/    V$$ t83.#8999r   c                 :    |rd|v rt          | |          s|S |p|S )zKeep slash-bearing model IDs only for cached clients that support them.

    Mirrors the guard in resolve_provider_client() which is skipped on cache hits.
    rt   )rD  )r|  rr   rB  s      r   _compat_modelrF    s5    
  &I&R`&a&a"N"r   c           
          d}|r*	 ddl }	|	                                }n# t          $ r Y nw xY wt          |          }
t	          | ||||||          }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   )r  r  r  rI  r  )rW  r!  r*  rM  r  rw  rx  r2  rF  ry  r  r   _CLIENT_CACHE_MAX_SIZEnextiterrB   )r\   rr   r  ry   rI  r  rI  r  r$  r%  r  r  cached_clientrB  r>  loop_ok	effectiver|  r  r  	evict_keyevict_entryr  s                          r   r  r    sD   < L 	""""..00LL 	 	 	D	%l33G!!  I 
 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C=8,C==DD-B6G//G36G3c                    d}d}d}d}d}	| r"t          |           }
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\   r9   rr   ry   rI  r  rb   r^   )_get_auxiliary_task_configr   rl   rf   )r  r\   rr   ry   rI  rD  	cfg_modelrE  cfg_api_keycfg_api_modetask_configr  r  s                r   r  r    s   & LILKL L066;??: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/@@@r         >@c                 8   | si S 	 ddl m}  |            }n# t          $ r i cY S w xY wt          |t                    r|                    di           ni }t          |t                    r|                    | i           ni }t          |t                    r|ni S )zDReturn the config dict for auxiliary.<task>, or {} when unavailable.r   r   	auxiliary)r   r   r  r"   r   rl   )r  r   configauxrU  s        r   rQ  rQ  D  s     	111111   			)3FD)A)A
I&**["
%
%
%rC'1#t'<'<D#''$###"K$[$77?;;R?s    &&r  c                     | s|S t          |           }|                    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*.timeout)rQ  rl   r   r)  r5   )r  r  rU  raws       r   _get_task_timeoutr^  R  sm     ,T22K
//)
$
$C
	::I& 	 	 	D	Ns   ; AAc                     t          |           }|                    d          }t          |t                    rt          |          S i S )zFRead auxiliary.<task>.extra_body and return a shallow copy when valid.r   )rQ  rl   r"   r   )r  rU  r]  s      r   _get_task_extra_bodyr`  `  sC    ,T22K
//,
'
'C#t CyyIr   >   r   rR   r   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.
    Tr9   r   )_ANTHROPIC_COMPAT_PROVIDERSrg   )r\   ry   	url_lowers      r   _is_anthropic_compat_endpointrd  r  s5     ...tR&&((I9$$r   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   Fr   r   r7   r9   zdata:,z	image/pngr`   ;ra   r   imager   )r   
media_typer  )r   r  )r   r7   T)rl   r"   r   r   rh   	partitionri   )r   r   r'  r   new_contentchangedblockimage_url_valheaderr  b64datari  s               r   #_convert_openai_images_to_anthropicrq  ~  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r   ro  rm  r   r\  r   c	                    |||d}	t          ||          }
|
t          u rd}n|
|
}|ddlm}  ||          rd}|||	d<   |:| dk    r/|pt	                      }t          |          dk    r||	d<   n||	d	<   n||	d	<   |rt                      }g }|D ]}}|                    d
          pi                     dd          }|r"||v rt          	                    d|| |           Q|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.)rr   r   r\  Nr   rs  ro  rb   r  rn  rm  r   r  r9   zK_build_call_kwargs: duplicate tool name '%s' removed (provider=%s model=%s)r   r  r   r   r   )r}   rq   rw  rt  r  r1   setrl   r{   r  r  r   r   r   
setdefaultextend)r\   rr   r   ro  rm  r   r\  r   ry   r   fixed_temperaturert  r  _seen_deduped_t_tnamemerged_extras                     r   _build_call_kwargsr|    s     F 5UHEE,,,		&' DDDDDD##E** 	K +} x"@&>&@&@K --1AAA2<.//'1|$$#-F<  # UU 	  	 BffZ((.B33FB??F &E//-He  
  "		&!!!OOB"w 
(b))L6.++224J3KLLL ,+|Mr   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!  rz  r.  r5   
IndexErrorr   r(   r   )r~  r  r!  r   response_typeresponse_previews         r   _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\   rr   ry   rI  rI  ro  rm  r   r\  r   c                   t          | ||||          \  }}}}}t          |           }|                    |pi            | 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          ||||||	|||p|	  	        }t          t          |dd          pd          }t!          ||          rt#          |d                   |d<   	 t%           |j        j        j        d,i ||           S # t,          $ r}d|v rt/          |          rt1          |          }|                    dd           t                              d| pd           	 t%           |j        j        j        d,i ||           cY d}~S # t,          $ rS}t          |          }t5          |          s't7          |          st9          |          s	d|v sd|v s |}|}Y d}~nd}~ww xY wt          |          }|d|v sd|v st;          |d          r~|                    dd           ||d<   	 t%           |j        j        j        d,i ||           cY d}~S # t,          $ r+}t5          |          st7          |          s |}Y d}~nd}~ww xY w|d k    pt=          |d!          }t9          |          r|rt?          |pd |d||||| dk    "          \  } }!| gt                              d#| pd           |!r|!|                     d$          k    r|!|d$<   t%           | j        j        j        d,i ||           cY d}~S t9          |          r|d%vr|stC          |          rt                              d&| pd|           | dk    rt          ||d          d'd         nt          ||||||          \  }"}#|"t          ||#p|||||	|||	  	        }t          t          |"dd          pd          }$t!          ||$          rt#          |d                   |d<   t%           |"j        j        j        d,i ||           cY d}~S t5          |          pt7          |          }%|d%v }&|%r|&rt5          |          rd(nd)}'t                              d*| pd|'||           tE          || |'+          \  }(})}*|(`t          |*|)||||	||t          t          |(dd          pd          	  	        }+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", "title_generation").
              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.
    r  r^   Fr\   rr   ry   rI  r  NDVision provider %s unavailable, falling back to auto vision backendsr\   rr   r  $No LLM provider configured for task=
 provider=. Run: hermes setup)ry   rI  r  rI  r9   r^   r   rb   
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  ry   zAuxiliary %s: using %s (%s)%sr  r   z at ro  rm  r   r\  r   ry   r   ro  zEAuxiliary %s: provider rejected temperature; retrying once without itrm  ro  rn  r  inference-api.nousresearch.com)r  rr   r  ry   rI  r  rI  r  zDAuxiliary %s: refreshed Nous runtime credentials after 401, retryingrr   r^   r9   NzAAuxiliary %s: refreshed %s credentials after auth error, retryingra   r  connection errorz,Auxiliary %s: %s on %s (%s), trying fallbackr  r   )#r  r`  r  r  r{   r  r*  r  rf   rg   upperr  r^  r   r   r|  rd  rq  r  rH  rA  r;  r   rs  r   rz  r\  rg  rj  rq  r0   r&  rl   r  r  ),r  r\   rr   ry   rI  rI  r   ro  rm  r   r\  r   r  r  r  r  r  effective_extra_bodyeffective_providerr|  r  	_expliciteffective_timeout
_base_infor   _client_base	first_errretry_kwargs	retry_errretry_err_strerr_strclient_is_nousrefreshed_clientrefreshed_modelretry_clientretry_model_retry_baseshould_fallbackis_autor  	fb_clientfb_modelfb_label	fb_kwargss,                                               r   call_llmr    s.
   N a}hxa2 a2]~'8:JL]/55
 0b111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.;O00	2 2 2F wvz266<"==L$%6EE U@
ASTTzT%*FK#*44V44d< < 	< Q Q QF""'H'S'S"<<L]D111KKW  &-2FK+2BB\BBDJ J J J J J J J & & & #I &i00+I66 &i00 $}44.-??%	%&" i..!G##&'11.y,GG 2 JJ|T***.8F*+&-2FK+2<<V<<dD D D D D D D D & & & *)44 8LY8W8W %						& ' S$Z1QRR 	 )$$ 	N 	N0N0:F! *(*)8+	1 	1 	1-o  +b NF, , ," 6&**W:M:M'M'M&5F7O-<$)5<FFvFFN N N N N N N N 9%% (	T%-???& @,->?? %TWNF$5   x'' 3!2)#(   bb	  ,)&!2 0!2%1   *k   +#5)#2{ $/#-# 1#7!2
$ 
$ 
$L #&glJ&K&K&Qr"R"RK45FTT q3VWcdnWo3p3pZ01<)5<LL|LLdT T T T T T T T ,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 +
):3 J!C!C!IrJJL L L	 .5IN.5BB	BBDJ J J J J J J JcQs   $I* *Z5AZ$K5/Z5
M?A	MZMAZ$O>Z
O9!O4/Z4O99B-Z&Z,DZ.Z4CZ?ZZZ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   Nr9   zz<(?:think|thinking|reasoning|thought|REASONING_SCRATCHPAD)>.*?</(?:think|thinking|reasoning|thought|REASONING_SCRATCHPAD)>)flags)r   reasoning_contentreasoning_detailsr   r   r   z

)rer!  r  r   rf   subDOTALL
IGNORECASEr   r"   r   r   r   r   rl   r%  )r~  r  r'  r   cleanedreasoning_partsrL  r  detailsr   r   s              r   extract_content_or_reasoningr  3  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 ,{{?+++2r   )	r\   rr   ry   rI  ro  rm  r   r\  r   c       
         8  K   t          | ||||          \  }}}}}t          |           }|                    |
pi            | 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          |dd          pd          }t          |||||||||p|	  	        }t!          ||          rt#          |d                   |d<   	 t%           |j        j        j        d(i | d{V |           S # t,          $ rk}d|v rt/          |          rt1          |          }|                    dd           t                              d| pd           	 t%           |j        j        j        d(i | d{V |           cY d}~S # t,          $ rS}t          |          }t5          |          s't7          |          st9          |          s	d|v sd|v s |}|}Y d}~nd}~ww xY wt          |          }|d|v sd|v st;          |d          r|                    dd           ||d<   	 t%           |j        j        j        d(i | d{V |           cY d}~S # t,          $ r+}t5          |          st7          |          s |}Y d}~nd}~ww xY w|dk    pt=          |d          }t9          |          r|rt?          |pd|d|||| dk              \  }}|mt                              d| pd           |r||                     d           k    r||d <   t%           |j        j        j        d(i | d{V |           cY d}~S t9          |          r|d!vr|stC          |          rt                              d"| pd|           | dk    rt          ||d          \  } }!}"nt          ||d|||          \  }!}"|!t          ||"p||||||||	  	        }t          t          |!dd          pd          }#t!          ||#          rt#          |d                   |d<   t%           |!j        j        j        d(i | d{V |           cY d}~S t5          |          pt7          |          }$|d!v }%|$r|%rt5          |          rd#nd$}&t                              d%| pd|&||           tE          || |&&          \  }'}(})|'t          |)|(||||||t          t          |'dd          pd          	  	        }*tG          |'|(pd| dk    '          \  }+},|,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.
    r  r^   Tr  Nr  r  r  r  r  )r  ry   rI  r  r9   r  r  r  r  r  r  )r  ry   r  r   ro  zMAuxiliary %s (async): provider rejected temperature; retrying once without itrm  ro  rn  r  r  )r  rr   r  ry   rI  r  r  zLAuxiliary %s (async): refreshed Nous runtime credentials after 401, retryingrr   r  zIAuxiliary %s (async): refreshed %s credentials after auth error, retryingr  r  z4Auxiliary %s (async): %s on %s (%s), trying fallbackr  r  r   )$r  r`  r  r  r{   r  r*  r  rf   rg   r  r  r^  r   r   r|  rd  rq  r  rH  rA  r;  r   rs  r   rz  r\  rg  rj  rq  r0   r&  rl   r  r  r  )-r  r\   rr   ry   rI  r   ro  rm  r   r\  r   r  r  r  r  r  r  r  r|  r  r  r  r  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  async_fbasync_fb_models-                                                r   async_call_llmr  k  sg
     $ a}hxa2 a2]~'8:JL]/55
 0b111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
 wvz266<"==L;J.;O2!2	4 4 4F %%6EE U@
ASTTzF%0&+)0::6::::::::DB B 	B C C CF""'H'S'S"<<L]D111KK_  &-8&+18HH<HHHHHHHH$P P P P P P P P & & & #I%i00+I66 &i00 $}44.-??%	%& i..!G##&'11.y,GG 2 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 %						& ' U$\3STT 	 )$$ 	T 	T0N0:F!*(*8+1 1 1-o  +j NF, , ," 6&**W:M:M'M'M&5F7O-B*/;BLLVLLLLLLLLdT T T T T T T T 9%% '	Z%-???& @,->?? $Z_NF$5   8##3Q!2)#'4 4 40A|[[ 1C)&#'!2 0!21 1 1-L+  +#5)#2{ $/#-# 1#7!2
$ 
$ 
$L #&glJ&K&K&Qr"R"RK45FTT q3VWcdnWo3p3pZ01Bl/;BRR\RRRRRRRRTXZ Z Z Z Z Z Z Z ,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 +
):3 J!C!C!IrJJL L L	 ,<x~2$(:J, , ,(. " 8n	g8N8N&N&N)7Ig&-:(-3:GGYGGGGGGGGO O O O O O O OGCs   8*H# #Z.AZ*J4.Z4
L>A	LZLAZ*N	Z	
N>!N94Z9N>>B2Z0Z6DZ;ZDZZZZr   )r9   r  )r   N)Nr  )NFFNNNNFr  )NFNNNNF)NNNNN)NNNrV  NN)r+   r   loggingr   	threadingr  pathlibr   typesr   typingr   r   r   r   r	   r
   r&  r   r   r   r   r   r   r   __annotations__r   r   agent.credential_poolr-   r   r.   hermes_constantsr/   utilsr0   r1   r2   	getLoggerr(   r{   r  r6   r   rF   r  rk   rp   objectrq   rx   r}   r   r   	frozensetr   r   r   r   r   
hermes_clir   _HERMES_VERSION_AI_GATEWAY_HEADERSr  r   r  r  r  rC  r  r7  r   r   r   r   r   r   r   r=  rE  rM  r\  r_  re  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r
  rj   r  r  r,  r1  r<  r?  r  _AUTO_PROVIDER_LABELSrK  rM  rO  r   r\  rg  rj  rq  rs  r}  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  rx  Lockrw  rH  r  r  r&  r/  ry  r;  r?  rA  rD  rF  r  r  _DEFAULT_AUX_TIMEOUTrQ  r   r^  r`  rb  rd  r   rq  r|  r  r  r  r  r   r   r   <module>r     s  ( ( (T   				            ! ! ! ! ! ! B B B B B B B B B B B B B B B B 7 7 7 7 7 7 7 7 7 7  $( 8D> ( ( ($    , , , , , , , ,& 
 + + + + + + - - - - - - 0 0 0 0 0 0 T T T T T T T T T T		8	$	$# 3 4    3      hX  E	
 E E 
5 E E U M   #   !" \#$ '&!"'%=   D9hsm 9 9 9 9 92 "688 & # # #6(3- 6D 6 6 6 6 # C=sm     (0&0	?0 *0 	0
 -0 
10 ~0 -0 .0 ,0 )0 $0 70 /0 )0  !0 d38n   0 + + c3h    (1y2 ( ( 9    <(7   Y999:: 1 1t 1t 1 1 1 1j 6 5 5 5 5 5 <222   234   4    4 -D 9 !/##k1 > $C $DcN $ $ $ $N# #    0 tXc]/B)C     " " " " " ". .# . .c . . . .(-C -C - - - -`{
 {
 {
 {
 {
 {
 {
 {
|# # # # # # # #" " " " " " " "$D D D D D D D D# # # # # # # #
. . . . . . . .Q
 Q
 Q
 Q
 Q
 Q
 Q
 Q
h# # # # # # # #
        D D D D D D D D# # # # # # # #
. . . . . . . .# $    B #O OOO O 	O
 smO 	O O O Od#$ # # # #LID IS I I I I
H H H H H
 8=    %PSUXPX/AZ    6((3- ( ( ( (VR5&)98C=)H#I R R R RrJ Jc JU8F;KXVY];Z5[ J J J J&
4# 
4 
4 
4 
4? ?d ?uXf-=x}-L'M ? ? ? ?D#    *S    &/8x}hsmXc]'R!S /8 /8 /8 /8d#    
   :     $)#eHSM8C=$@A )# )# )# )#X&;s &;uXc]HSM-I'J &; &; &; &;R-chsmXc]:; -c -c -c -cb $*!*	   P (4S>*B tCQTH~    T%[    *9     (i D    4a	 ad a a a a 3 4    D?9 ? ? ? ? ?)C )D ) ) ) )*&C &D & & & &V !, ,,
, , 8C=(3-,-	, , , ,^X Xc3h 8 XE(SYJZ\deh\iJiDj X X X XL+. +. +. +. +. +. +.\	(3- 	3 	8TW= 	 	 	 	 ! -1E EEE E 	E
 E E E 4S>*E E 8C=(3-'(E E E EV  .2  
 4S>* 8FXc]*+	   2`d   # XdSVX[S[nE]    & -# -3 - - - -   C= 8C=(3-'(   .Cs Ct C C C CtCy    4 #u* #!u* u* u*smu*C=u* sm	u*
 c]u* u* 8C=(3-#67u* u* u* u*p>$ > > > >!c !d ! ! ! !H %'tE5L! & & &#Y^%%   #!"-1i i ii i sm	i
 c]i smi 4S>*i i i i i i lp G G GE G3 GxPS} Geh Gtx G G G G& #!"-1) ) )) C=) 	)
 sm) c]) sm) 4S>*) ) 8C=(3-'() ) ) )X   BS T    (   6# # # #&# $    : :Xc] :W[ : : : :## #hsm #Xc] #W_`cWd # # # # -1^* ^*^*^* ^* 	^*
 ^* ^* 4S>*^* ^* 8C=(3-'(^* ^* ^* ^*D 2A 2A
2A2A 2A 	2A
 2A 3x}hsmXc]JK2A 2A 2A 2Aj  @S @T#s(^ @ @ @ @ 3G  C % 5    s tCH~     (i(R(R(RSS 	%C 	%3 	%4 	% 	% 	% 	%,$ ,4 , , , ,h $( $ !%"M MMM M %	M
 M D>M M M smM 
M M M M` S  s    @ S -1S S S
S S 	S
 S S 4S>*S S S S S S S 	S S S Sl5c 5 5 5 5r _ _ _ _
_ _ 	_
 _ _ _ _ _ _ _ _ 	_ _ _ _ _ _r   