
    Fj                       d Z 	 ddlZn# e$ r Y nw xY wddlZddlZddlZddlZddlZddlZ ej	        e
          ZddlZddl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mZmZmZ ddlmZ ddlmZ ddlmZ ddlmZmZm Z  ddl!m"Z" dd	l#m$Z$ dd
l%m&Z&m'Z'  e            Z( ee)          j*        dz  Z+ e$e(e+          Z,e,re,D ]Z-e.                    de-           ne.                    d           ddl/m0Z0m1Z1m2Z2m3Z3 ddl4m5Z5 ddl6m7Z8 ddl9m:Z: ddl;m<Z< ddl=m>Z> ddl?m@Z@ ddlAmBZBmCZC ddlDmEZE ddlFmGZG ddlHmIZI ddlJmKZKmLZLmMZMmNZNmOZOmPZP ddlmQZQ ddlRmSZSmTZTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z] ddl^m_Z`maZbmcZdmeZe ddlfmgZgmhZhmiZi ddljmkZlmmZm dd lnmoZompZq dd!lrmsZsmtZtmuZumvZvmwZwmxZxmyZymzZzm{Z{m|Z| dd"l}m~Z~mZmZ d#Z ej                    Zd$Zd%efd&Zddd'd(edz  d)edz  d%efd*Zd%efd+Z G d, d-e          Z G d. d/          Z	 	 	 	 	 	 	 	 	 	 	 	 dAd4ed5ed6ed)ed7ed8ed9ed:ed;ed<ed=ed>efd?Ze
d@k    rddlZ ej        e           dS dS )Bao  
AI Agent Runner with Tool Calling

This module provides a clean, standalone agent that can execute AI models
with tool calling capabilities. It handles the conversation loop, tool execution,
and response management.

Features:
- Automatic tool calling loop until completion
- Configurable model parameters
- Error handling and recovery
- Message history management
- Support for multiple model providers

Usage:
    from run_agent import AIAgent
    
    agent = AIAgent(base_url="http://localhost:30000/v1", model="claude-opus-4-20250514")
    response = agent.run_conversation("Tell me about the latest Python updates")
    N)ListDictAnyOptional)datetime)Path)get_hermes_home)OpenAI_SafeWriter_get_proxy_for_base_url)IterationBudget)load_hermes_dotenv)get_provider_request_timeoutget_provider_stale_timeoutz.env)hermes_homeproject_envz$Loaded environment variables from %sz7No .env file found. Using system environment variables.)get_tool_definitionsget_toolset_for_toolhandle_function_callcheck_toolset_requirements)
cleanup_vm)set_interrupt)cleanup_browser)sanitize_context)FailoverReason)redact_sensitive_text)estimate_request_tokens_roughis_local_endpoint)normalize_usage)ContextCompressor)jittered_backoff)DEFAULT_AGENT_IDENTITYbuild_skills_system_promptbuild_context_files_promptbuild_environment_hintsbuild_nous_subscription_promptload_soul_md)_get_proxy_from_env)_SURROGATE_RE_sanitize_surrogates_sanitize_structure_surrogates_sanitize_messages_surrogates%_escape_invalid_chars_in_json_strings_repair_tool_call_arguments_strip_non_ascii_sanitize_messages_non_ascii_sanitize_tools_non_ascii_strip_images_from_messages_sanitize_structure_non_ascii)"_derive_responses_function_call_id_deterministic_call_id_split_responses_tool_id_summarize_user_message_for_log)ToolGuardrailDecisionappend_toolguard_guidancetoolguard_synthetic_result)FILE_MUTATING_TOOL_NAMESfile_mutation_result_landed)convert_scratchpad_to_thinksave_trajectory)
_should_parallelize_tool_batch_is_destructive_command_extract_parallel_scope_path_paths_overlap_is_multimodal_tool_result_multimodal_text_summary!_append_subdir_hint_to_multimodal_extract_file_mutation_targets_extract_error_preview_trajectory_normalize_msg)atomic_json_writebase_url_host_matchesbase_url_hostname   z0.14.1returnc                      ddl m}  dd|  iS )zGReturn the User-Agent RouterMint needs to avoid Cloudflare 1010 blocks.r   )__version__
User-AgentzHermesAgent/)
hermes_clirO   )_HERMES_VERSIONs    (/usr/local/lib/hermes-agent/run_agent.py_routermint_headersrT      s.    999999 	6_66     )providerbase_urlrV   rW   c                    | dS |                                  sdS |dk    s$t          |pd                              d          rdS t          |                                           dk    S )u?  Decide whether to wait for credential-pool rotation instead of falling back.

    The existing pool-rotation path requires the pool to (1) exist and (2) have
    at least one entry not currently in exhaustion cooldown.  But rotation is
    only meaningful when the pool has more than one entry.

    With a single-credential pool (common for Gemini OAuth, Vertex service
    accounts, and any "one personal key" configuration), the primary entry
    just 429'd and there is nothing to rotate to.  Waiting for the pool
    cooldown to expire means retrying against the same exhausted quota — the
    daily-quota 429 will recur immediately, and the retry budget is burned.

    Additionally, Google CloudCode / Gemini CLI rate limits are ACCOUNT-level
    throttles — even a multi-entry pool shares the same quota window, so
    rotation won't recover.  Skip straight to the fallback for those (#13636).

    In those cases we must fall back to the configured ``fallback_model``
    instead.  Returns True only when rotation has somewhere to go.

    See issues #11314 and #13636.
    NFgoogle-gemini-cli cloudcode-pa://   )has_availablestr
startswithlenentries)poolrV   rW   s      rS   !_pool_may_recover_from_rate_limitrc      su    0 |u u &&&#hn"*=*=*H*HIZ*[*[&ut||~~""rU   c                      ddl } dt           d|                                                                  d|                                  d}|d|dd	S )
z8Return default HTTP headers required by Qwen Portal API.r   Nz	QwenCode/z (z; )enablez
qwen-oauth)rP   zX-DashScope-CacheControlzX-DashScope-UserAgentzX-DashScope-AuthType)platform_QWEN_CODE_VERSIONsystemlowermachine)_plat_uas     rS   _qwen_portal_headersrn      sf    
X(
X
XELLNN,@,@,B,B
X
Xemmoo
X
X
XC$,!$ ,	  rU   c                   f     e Zd ZdZdddddedee         dee         dee         ddf
 fd	Z xZS )
_StreamErrorEventa  Synthesized provider error surfaced from a Responses ``error`` SSE frame.

    Some Codex-style Responses backends (xAI for subscription/quota
    failures, custom relays under malformed-tool-call conditions) emit a
    standalone ``type=error`` frame instead of routing the failure
    through ``response.failed`` or returning an HTTP 4xx.  The fallback
    streaming path raises this exception so ``_summarize_api_error`` and
    ``_extract_api_error_context`` see a familiar ``.body`` /
    ``.status_code`` shape and the entitlement detector can match the
    underlying provider message ("do not have an active Grok
    subscription", etc.).
    N)codeparamstatus_codemessagerq   rr   rs   rM   c                    t                                          |           || _        || _        || _        || _        d|||ddi| _        d S )Nerror)rt   rq   rr   type)super__init__rt   rq   rr   rs   body)selfrt   rq   rr   rs   	__class__s        rS   ry   z_StreamErrorEvent.__init__  sb     	!!!	
& "	 %
			rU   )	__name__
__module____qualname____doc__r^   r   intry   __classcell__)r|   s   @rS   rp   rp      s         " ##%)
 
 

 sm	

 }
 c]
 

 
 
 
 
 
 
 
 
 
rU   rp   c                   2   e Zd ZdZdZedefd            Zej        deddfd            Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddededededede	e         dz  dede	e         dz  dede
dedee         dee         dededed ed!e
d"ed#ee         d$ee         d%ee         d&ed'ed(ed)ee         d*ed+ed,ed-ed.ed/ed0ed1ed2ed3ed4ed5ed6e
d7eeef         d8ed9eeef         d:eeeef                  d;ed<ed=ed>ed?ed@edAedBedCedDedEedFedGedHdIdJeeef         dKedLe
dMe
dNe
dOef~dPZdQ ZddRZdddddSdTdUee         dVee         dWee	         dXedYeddfdZZ	 	 	 ddWee	         dUee         dXefd[Zdd\ee
         ddfd]Zdd^Zd_ Zdd`daefdbZdefdcZdefddZdeeddfdfZdeeddfdgZdeeddfdhZdeeddfdiZ ddjZ!ddkZ"	 ddleeeeef                           deee
f         fdmZ#dndol$m%Z& e'deeef         fdp            Z(dqeeef         dreddfdsZ)e'dte*defdu            Z+dte*defdvZ,ddwdxedte*dye
dze
d{edqeeeef                  ddfd|Z-ddwdte*dye
dze
d{edqeeeef                  ddfd}Z.d~ede*ddfdZ/deeef         fdZ0ddZ1ddZ2ddedefdZ3ddedefdZ4ddedefdZ5defdZ6de7eef         fdZ8dedefdZ9ddee         dee         fdZ:defdZ;ddddddee         dee         dee         dee         de7eef         f
dZ<e'dedefd            Z=e'dddedee         defd            Z>de
de?fdZ@dedefdZAdedefdZBe'dedefd            ZCdefdZD	 ddedlee	         defdZEdededleeeef                  defdZFdee         fdZGdeddfdZHdndlImJZJmKZKmLZL e'dee         dee         dee         fd            ZM	 	 ddee         dededdfdZNddddddee         dee         dee         dee         deeef         f
dZOdlee         ddfdZPddlee         dee         fdZQdlee         ddfdZRdlee         de
fdZSddlee         dee         fdZTdlee         dee         fdZUdefdZVdleeeef                  dededeeeef                  fdZWdleeeef                  dedefdZXe'deeeef                  dee
         defd            ZYe'dteZdefd            Z[dedee         fdZ\dedefdZ]e'dteZdeeef         fd            Z^dedeeeef                  fdÄZ_ddĜdeeef         dedteeZ         dee`         fdǄZae'dedefdȄ            Zbe'dɄ             Zcddleeeef                  fdʄZdddeeddfd˄Zedd̄Zfdedefd΄Zgdee         fdτZhdedeeef         dededdf
dӄZidefdԄZje'deeeeef         f         defdք            Zkdle	de
ddfd؄ZldeddfdڄZmdreddfdۄZnd܄ Zodreddfd݄Zpde?fdބZqddle	ddfd߄Zrddle	ddfdZsdddedededle	dz  ddf
dZtddZuddZvdeeeef                  ddfdZwedefd            Zxddedeeef         fdZyddedefdZze'defd            Z{e'defd            Z| e}h d          Z~e'dleeeef                  deeeef                  fd            Ze'deeef         defd            Ze'dleeeef                  deeeef                  fd            Ze'de	de	fd            Ze'de	de	fd            Zdededz  fdZd Ze'ddedede
defd            Ze'dede7ee         ee         f         fd            Z	 dd edee         defdZdefdZdefdZdej        fdZe'dedefd            Ze'ddedefd            Zd	e?ded
edefdZe'dede
fd            Zdeded
eddfdZdedefdZdedefdZdefdZe'de?defd            Zdede?fdZdddedee?         defdZdededdfdZdededdfdZdde?dedefdZdde?defdZdSd`daedefdZdSdddaededz  defdZdefdZdefd Zdeddfd!Zdd"Zddd#dee
         d$ed%ee         deeeef                  de7eef         f
d&Zdefd'Zde?fd(Zdd)Zde?fd*Zdd+Zdeddfd,Ze'dedefd-            Zdedefd.Zd/eeef         ddfd0Zdeddfd1Zdeddfd2Zdeddfd3Zdefd4Zdd5de?defd6ZddƐd7defd8Zdefd9Zd:eZd;e
d<e
defd=Ze'dedefd>            Ze'd?ede7eee`         f         fd@            Zd?edAedefdBZdefdCZdedAedefdDZddefdEZdFe	de	fdGZdFe	de	fdHZdededefdIZdFe	defdJZdFe	defdKZdefdLZdefdMZdFe	de	fdNZdle	ddfdOZĐdFe	de?fdPZdefdQZde	e         fdRZdee         fdSZde?dz  fdTZdede?fdUZdefdVZdefdWZdefdXZdefdYZΐdZe?d[e?ddfd\ZϐdFe	de
fd]Ze'd[e?de?fd^            Ze'ddd_dle	d*ede
fd`            ZdefdaZddbdddcdle	dedde
dedeedaede7fdfZԐdgeddfdhZ֐dgedefdiZdedje?dkededef
dlZؐdgedefdmZِddle	dnedoe
ddfdpZڐdje?defdqZ	 	 ddredje?dnedee         dle	dsedefdtZe'ddvededwedefdx            Zݐddle	dnedoe
ddfdyZސddle	dnedoe
ddfdzZdle	doe
defd{Z	 	 	 	 	 ddededeeeef                  ded|ee         d}ee         deeef         fd~Zddeed|ee         defdZdddededleeeef                  dnededeeef         fdZdS (  AIAgentz
    AI Agent with tool calling capabilities.

    This class manages the conversation flow, tool execution, and response handling
    for AI models that support function calling.
    z[hermes-agent: tool call arguments were corrupted in this session and have been dropped to keep the conversation alive. See issue #15236.]rM   c                     | j         S N)	_base_urlr{   s    rS   rW   zAIAgent.base_url3  s
    ~rU   valueNc                 v    || _         |r|                                nd| _        t          |          | _        d S NrZ   )r   rj   _base_url_lowerrK   _base_url_hostnamer{   r   s     rS   rW   zAIAgent.base_url7  s8    05=u{{}}}2"3E":":rU   rZ   Z         ?Fd        
   rW   api_keyrV   api_modeacp_commandacp_argscommandargsmodelmax_iterations
tool_delayenabled_toolsetsdisabled_toolsetssave_trajectoriesverbose_logging
quiet_modeephemeral_system_promptlog_prefix_chars
log_prefixproviders_allowedproviders_ignoredproviders_orderprovider_sortprovider_require_parametersprovider_data_collectionopenrouter_min_coding_score
session_idtool_progress_callbacktool_start_callbacktool_complete_callbackthinking_callbackreasoning_callbackclarify_callbackstep_callbackstream_delta_callbackinterim_assistant_callbacktool_gen_callbackstatus_callback
max_tokensreasoning_configservice_tierrequest_overridesprefill_messagesrg   user_iduser_id_alt	user_namechat_id	chat_name	chat_type	thread_idgateway_session_keyskip_context_filesload_soul_identityskip_memoryparent_session_iditeration_budgetr   fallback_modelcheckpoints_enabledcheckpoint_max_snapshotscheckpoint_max_total_size_mbcheckpoint_max_file_size_mbpass_session_idcB                    ddl m}B  |B| f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'|%d(|&d)|'d*|(d+|)d,|*d-|+d.|,d/|-d0|.d1|/d2|0d3|1d4|2d5|3d6|4d7|5d8|6d9|7d:|8d;|9d<|:d=|;d>|<d?|=d@|>dA|?dB|@dC|A dDS )Eu2   Forwarder — see ``agent.agent_init.init_agent``.r   )
init_agentrW   r   rV   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   r   r   r   r   r   rg   r   r   r   r   r   r   r   r   r   r   r   
session_dbr   r   r   credential_poolr   r   r   r   r   N)agent.agent_initr   )Cr{   rW   r   rV   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   r   r   r   r   r   rg   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   sC                                                                      rS   ry   zAIAgent.__init__=  s   J 	0/////
C	
 C	
 C	
XC	
 GC	
 X	C	

 XC	
 $C	
 XC	
 GC	
 C	
 %C	
 *>C	
 "zC	
 .-C	
 0/C	
 0/C	
  ,O!C	
" "z#C	
$ %<$;%C	
& .-'C	
( "z)C	
* 0/+C	
, 0/-C	
. ,O/C	
0 (-1C	
2 )D(C3C	
4 &>%=5C	
6 )D(C7C	
8 "z9C	
: $:#9;C	
< !4 3=C	
> $:#9?C	
@ 0/AC	
B  21CC	
D .-EC	
F (-GC	
H #8"7IC	
J (B'AKC	
L 0/MC	
N ,OOC	
P "zQC	
R .-SC	
T &UC	
V 0/WC	
X .-YC	
Z X[C	
\ G]C	
^ $_C	
`  iaC	
b GcC	
d  ieC	
f  igC	
h  iiC	
j !4 3kC	
l  21mC	
n  21oC	
p $qC	
r "zsC	
t 0/uC	
v .-wC	
x *>yC	
z ,O{C	
| !4 3}C	
~ &>%=C	
@ *F)EAC	
B )D(CCC	
D ,OEC	
 C	
 C	
 C	
rU   c                     | j         | j         S 	 ddlm}  |            | _         | j         S # t          $ r'}t                              dd           Y d}~dS d}~ww xY w)ai  Return a SessionDB for recall, lazily creating it if an entrypoint forgot.

        Most frontends pass ``session_db`` into ``AIAgent`` explicitly, but recall
        is important enough that a missing constructor argument should degrade by
        opening the default state DB instead of making the advertised
        ``session_search`` tool unusable.
        Nr   )	SessionDBz SessionDB unavailable for recallTexc_info)_session_dbhermes_stater   	Exceptionloggerdebug)r{   r   excs      rS   _get_session_db_for_recallz"AIAgent._get_session_db_for_recall  s     '##	......(y{{D## 	 	 	LL;dLKKK44444	s   , 
AAAc           	      Z   | j         s| j        sdS 	 | j                            | j        | j        pt
          j                            dd          | j        | j	        | j
        d| j                   d| _         dS # t          $ r&}t                              d|           Y d}~dS d}~ww xY w)zDCreate session DB row on first use. Disables _session_db on failure.NHERMES_SESSION_SOURCEcli)r   sourcer   model_configsystem_promptr   r   Tz5Session DB creation failed (will retry next turn): %s)_session_db_createdr   create_sessionr   rg   osenvirongetr   _session_init_model_config_cached_system_prompt_parent_session_idr   r   warning)r{   es     rS   _ensure_db_sessionzAIAgent._ensure_db_session  s    # 	4+; 	F	++?}V
7NPU(V(Vj!<"8"&"9 ,    (,D$$$ 	 	 	 NNG        	s   A&A: :
B*B%%B*Told_session_idnew_session_idprevious_messagescarry_over_contextreset_enginer   r   r   r   r   c          
         t          | dd          }|sdS |r\|Zt          |d          rJ	 |                    ||           n2# t          $ r%}t                              d|           Y d}~nd}~ww xY w|rXt          |d          rH	 |                                 n2# t          $ r%}t                              d|           Y d}~nd}~ww xY wt          |p|dup|p|          }	|pt          | dd          pd}
|	r|
rt          |d	          r||t          | d
d          pt          j	        
                    dd          t          | dd          t          |dd          t          | dd          d}|                    |           d |                                D             }	  |j        |
fi | n2# t          $ r%}t                              d|           Y d}~nd}~ww xY w|r`|r`|
r`t          |d          rR	 |                    ||
           dS # t          $ r&}t                              d|           Y d}~dS d}~ww xY wdS dS dS dS )a  Notify the active context engine about a host session transition.

        Generic host-side lifecycle helper. The built-in compressor keeps its
        existing reset behavior; plugin engines that implement richer hooks
        (``on_session_end``, ``on_session_reset``, ``on_session_start``,
        ``carry_over_new_session_context``) can flush old-session state,
        reset runtime counters, bind to the new session, and optionally
        carry retained context forward.
        context_compressorNon_session_endz3context engine on_session_end during transition: %son_session_resetz5context engine on_session_reset during transition: %sr   rZ   on_session_startrg   r   r   r   context_length_gateway_session_key)r   r   rg   r   r   conversation_idc                 "    i | ]\  }}|d v	||S )r    ).0kvs      rS   
<dictcomp>z>AIAgent._transition_context_engine_session.<locals>.<dictcomp>%  s)    [[[daqPZGZGZQGZGZGZrU   z5context engine on_session_start during transition: %scarry_over_new_session_contextzCcontext engine carry_over_new_session_context during transition: %s)getattrhasattrr   r   r   r   r   boolr   r   r   updateitemsr   r  )r{   r   r   r   r   r   extra_contextenginer   should_starttarget_session_idstart_contexts               rS   "_transition_context_engine_sessionz*AIAgent._transition_context_engine_session  s~   & 3T:: 	F 	Y/;P`@a@a;Y%%n6GHHHH Y Y YRTWXXXXXXXXY  	[GF,>?? 	[['')))) [ [ [TVYZZZZZZZZ[   ,! 	
 
 +SgdL".M.MSQS 	[- 	['&BT2U2U 	["0&8#D*d;;mrz~~Negl?m?m w33")&2BD"I"I#*41G#N#N M   ///[[m.A.A.C.C[[[M[''(9KK]KKKK [ [ [TVYZZZZZZZZ[ 		i		i "		i  @AA			ii55nFWXXXXX i i ibdghhhhhhhhhi		i 		i 		i 		i 		i 		i 		i 		is_   A 
A1A,,A1B 
C&CC'F6 6
G% G  G%?H 
I!IIc                    d| _         d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _	        d| _
        d| _        d| _        |                     |t          | dd          ||d           dS )	ax  Reset all session-scoped token counters to 0 for a fresh session.
        
        This method encapsulates the reset logic for all session-level metrics
        including:
        - Token usage counters (input, output, total, prompt, completion)
        - Cache read/write tokens
        - API call count
        - Reasoning tokens
        - Estimated cost tracking
        - Context compressor internal counters
        
        The method safely handles optional attributes (e.g., context compressor)
        using ``hasattr`` checks.

        When ``previous_messages`` / ``old_session_id`` / ``carry_over_context``
        are provided, the active context engine is notified through the
        full transition lifecycle (``_transition_context_engine_session``)
        instead of a bare reset. Default callers pass nothing and keep the
        existing reset-only behavior.
        r   g        unknownnoner   NTr   )session_total_tokenssession_input_tokenssession_output_tokenssession_prompt_tokenssession_completion_tokenssession_cache_read_tokenssession_cache_write_tokenssession_reasoning_tokenssession_api_callssession_estimated_cost_usdsession_cost_statussession_cost_source_user_turn_countr  r  )r{   r   r   r   s       rS   reset_session_statezAIAgent.reset_session_state6  s    6 %&!$%!%&"%&")*&)*&*+'()%!"*-'#, #)  !" 	//)"4t<</1 	0 	
 	
 	
 	
 	
rU   config_context_lengthc           
      2   | j         pd                                                                dk    rdS 	 ddlm} ddlm} |t          | dd          }t          |pd|          } || j	        | j
        t          | dd          |          }|rSt          | d	d          }|B|                    | j	        || j
        t          | dd          | j         | j        
           dS dS dS # t          $ r&}t                              d|           Y d}~dS d}~ww xY w)zT
        Preload the LM Studio model with at least Hermes' minimum context.
        rZ   lmstudioNr   )MINIMUM_CONTEXT_LENGTH)ensure_lmstudio_model_loaded_config_context_lengthr   r   )r   r   rW   r   rV   r   zLM Studio preload skipped: %s)rV   striprj   agent.model_metadatar&  hermes_cli.modelsr'  r  maxr   rW   update_modelr   r   r   r   )r{   r#  r&  r'  
target_ctx
loaded_ctxccerrs           rS   _ensure_lmstudio_runtime_loadedz'AIAgent._ensure_lmstudio_runtime_loadedj  s~    MR&&((..00J>>F	?CCCCCCFFFFFF$,(/6NPT(U(U%27a9OPPJ55
DM74B+G+G J  
 T#7>>>OO"j'1!% 'i < <!%!% $       ">  	? 	? 	?LL8#>>>>>>>>>	?s   B+C& &
D0DDc                 .    ddl m}  || |||||          S )u?   Forwarder — see ``agent.agent_runtime_helpers.switch_model``.r   )switch_model)agent.agent_runtime_helpersr4  )r{   	new_modelnew_providerr   rW   r   r4  s          rS   r4  zAIAgent.switch_model  s.    <<<<<<|D)\7HhWWWrU   c                 d    	 | j         pt          } ||i | dS # t          t          f$ r Y dS w xY w)a3  Print that silently handles broken pipes / closed stdout.

        In headless environments (systemd, Docker, nohup) stdout may become
        unavailable mid-session.  A raw ``print()`` raises ``OSError`` which
        can crash cron jobs and lose completed work.

        Internally routes through ``self._print_fn`` (default: builtin
        ``print``) so callers such as the CLI can inject a renderer that
        handles ANSI escape sequences properly (e.g. prompt_toolkit's
        ``print_formatted_text(ANSI(...))``) without touching this method.
        N)	_print_fnprintOSError
ValueError)r{   r   kwargsfns       rS   _safe_printzAIAgent._safe_print  sU    	(5BB$ 	 	 	DD	s    //forcerA  c                    t          | dd          rdS |st          | dd          rdS |s|                                 r	| j        sdS  | j        |i | dS )ue  Verbose print — suppressed when actively streaming tokens.

        Pass ``force=True`` for error/warning messages that should always be
        shown even during streaming playback (TTS or display).

        During tool execution (``_executing_tools`` is True), printing is
        allowed even with stream consumers registered because no tokens
        are being streamed at that point.

        After the main response has been delivered and the remaining tool
        calls are post-response housekeeping (``_mute_post_response``),
        all non-forced output is suppressed.

        ``suppress_status_output`` is a stricter CLI automation mode used by
        parseable single-query flows such as ``hermes chat -q``. In that mode,
        all status/diagnostic prints routed through ``_vprint`` are suppressed
        so stdout stays machine-readable.
        suppress_status_outputFN_mute_post_response)r  _has_stream_consumers_executing_toolsr?  )r{   rA  r   r=  s       rS   _vprintzAIAgent._vprint  s    & 41599 	F 	'<eDD 	F 	3355 	d>S 	F$)&)))))rU   c                     | j         dS t          t          dd          }|dS 	 t          |                                          S # t
          t          t          f$ r Y dS w xY w)a  Return True when quiet-mode spinner output has a safe sink.

        In headless/stdio-protocol environments, a raw spinner with no custom
        ``_print_fn`` falls back to ``sys.stdout`` and can corrupt protocol
        streams such as ACP JSON-RPC. Allow quiet spinners only when either:
        - output is explicitly rerouted via ``_print_fn``; or
        - stdout is a real TTY.
        NTstdoutF)r9  r  sysr	  isattyAttributeErrorr<  r;  )r{   streams     rS   _should_start_quiet_spinnerz#AIAgent._should_start_quiet_spinner  sp     >%4h-->5	(((
G4 	 	 	55	s    A A! A!c                 J    | j         o| j         ot          | dd          dk    S )aZ  Return True when quiet-mode tool summaries should print directly.

        Quiet mode is used by both the interactive CLI and embedded/library
        callers. The CLI may still want compact progress hints when no callback
        owns rendering. Embedded/library callers, on the other hand, expect
        quiet mode to be truly silent.
        rg   rZ   r   )r   r   r  r   s    rS    _should_emit_quiet_tool_messagesz(AIAgent._should_emit_quiet_tool_messages  s5     O 7//7j"--6	
rU   rt   c                    	 |                      | j         | d           n# t          $ r Y nw xY w| j        rF	 |                     d|           dS # t          $ r  t                              dd           Y dS w xY wdS )u  Emit a lifecycle status message to both CLI and gateway channels.

        CLI users see the message via ``_vprint(force=True)`` so it is always
        visible regardless of verbose/quiet mode.  Gateway consumers receive
        it through ``status_callback("lifecycle", ...)``.

        This helper never raises — exceptions are swallowed so it cannot
        interrupt the retry/fallback logic.
        Tr@  	lifecyclez%status_callback error in _emit_statusr   NrG  r   r   r   r   r   r{   rt   s     rS   _emit_statuszAIAgent._emit_status  s    	LLDO6W66dLCCCC 	 	 	D	 	UU$$['::::: U U UDtTTTTTTU	U 	U    # 
00A &A=<A=c                    	 |                      | j         | d           n# t          $ r Y nw xY w| j        rF	 |                     d|           dS # t          $ r  t                              dd           Y dS w xY wdS )a+  Emit a user-visible warning through the same status plumbing.

        Unlike debug logs, these warnings are meant for degraded side paths
        such as auxiliary compression or memory flushes where the main turn can
        continue but the user needs to know something important failed.
        Tr@  warnz&status_callback error in _emit_warningr   NrS  rT  s     rS   _emit_warningzAIAgent._emit_warning  s    	LLDO6W66dLCCCC 	 	 	D	 	VV$$VW55555 V V VEPTUUUUUUV	V 	VrV  c                     	 t          | dd          }|	g }|| _        |                    d|f           dS # t          $ r Y dS w xY w)a  Buffer a retry/fallback status message.

        Stored as a (kind, text) tuple where ``kind`` is one of:
        - ``"status"``  -> replays via ``_emit_status``
        - ``"vprint"``  -> replays via ``_vprint(force=True)``
        - ``"warn"``    -> replays via ``_emit_warning``
        Used to defer noisy retry chatter until we know whether the
        turn ultimately recovered or failed.
        _retry_status_bufferNstatusr  r[  appendr   r{   rt   bufs      rS   _buffer_statuszAIAgent._buffer_status  sj    	$ 6==C{,/)JJ'*+++++ 	 	 	DD	   37 
AAc                     	 t          | dd          }|	g }|| _        |                    d|f           dS # t          $ r Y dS w xY w)z0Buffer a vprint(force=True) retry/fallback line.r[  Nvprintr]  r_  s      rS   _buffer_vprintzAIAgent._buffer_vprint"  sj    	$ 6==C{,/)JJ'*+++++ 	 	 	DD	rb  c                 |    	 t          | dd          }|r|                                 dS dS # t          $ r Y dS w xY w)u=   Drop buffered retry messages — call on successful recovery.r[  N)r  clearr   )r{   r`  s     rS   _clear_status_bufferzAIAgent._clear_status_buffer-  s^    	$ 6==C 		  	 	 	DD	s   '- 
;;c                 |   	 t          | dd          }|sdS t          |          }|                                 |D ]n\  }}	 |dk    r|                     |           n<|dk    r|                     |           n |                     | j         | d           _# t          $ r Y kw xY wdS # t          $ r Y dS w xY w)u   Emit buffered retry messages — call on terminal failure.

        Surfaces the full retry/fallback trace so the user can see what
        was tried before the turn gave up.
        r[  Nr\  rX  Tr@  )r  listrg  rU  rY  rG  r   r   )r{   r`  messageskindmsgs        rS   _flush_status_bufferzAIAgent._flush_status_buffer6  s
   	$ 6==C CyyHIIKKK% 	 		cx''))#....**3////%>%>%>dKKK    D	 	  	 	 	DD	s;   B- *B- ABB- 
B(%B- 'B((B- -
B;:B;rk  c                 D   d}d}t          |t                    r|ng }|D ]u}t          |t                    r|                    d          dk    r1|                    dd          }t          |t                    r|r|dz  }|t          |          z  }vd| _        ||dS )	u&  Disable Responses encrypted reasoning replay and strip cached state.

        Called from the conversation_loop retry path when the provider
        rejects a replayed ``codex_reasoning_items`` blob with HTTP 400
        ``invalid_encrypted_content``.  Sets ``self._codex_reasoning_replay_enabled``
        to ``False`` (consumed by ``codex_responses_adapter._chat_messages_to_responses_input``
        and ``transports/codex.py`` to drop ``reasoning.encrypted_content``
        from subsequent requests) and pops ``codex_reasoning_items`` from
        every assistant message in ``messages`` so they cannot be replayed
        again later in the session.

        Returns a small stats dict ``{"messages": int, "items": int}``
        counting what was stripped — purely for diagnostic logging.
        r   role	assistantcodex_reasoning_itemsNr\   F)rk  r  )
isinstancerj  dictr   popr`   _codex_reasoning_replay_enabled)r{   rk  stripped_messagesstripped_itemstarget_messagesrm  r  s          rS   _disable_codex_reasoning_replayz'AIAgent._disable_codex_reasoning_replayP  s    $ &04&@&@H((b" 	- 	-Cc4(( CGGFOO{,J,JGG3T::E%&& -5 -!Q&!#e**,/4,-GGGrU   r   )STREAM_DIAG_HEADERSc                  "    ddl m}   |             S )u9   Forwarder — see ``agent.stream_diag.stream_diag_init``.r   stream_diag_init)agent.stream_diagr~  r}  s    rS   _stream_diag_initzAIAgent._stream_diag_initu  s%     	766666!!!rU   diaghttp_responsec                 ,    ddl m}  || ||           dS )uE   Forwarder — see ``agent.stream_diag.stream_diag_capture_response``.r   )stream_diag_capture_responseN)r  r  )r{   r  r  r  s       rS   _stream_diag_capture_responsez%AIAgent._stream_diag_capture_response{  s2     	CBBBBB$$T4?????rU   rv   c                 $    ddl m}  ||           S )u@   Forwarder — see ``agent.stream_diag.flatten_exception_chain``.r   )flatten_exception_chain)r  r  )rv   r  s     rS   _flatten_exception_chainz AIAgent._flatten_exception_chain  s'     	>=====&&u---rU   c                    t          | dd          dk    rdS t          |t                    sdS t          |t          t          j        f          rdS t          |                                                                          }d|v S )a  Return True for malformed provider streaming data from SDK parsers.

        Some Anthropic-compatible streaming providers can send a malformed
        event-stream frame.  The Anthropic SDK surfaces that as a plain
        ``ValueError`` such as ``expected ident at line 1 column 149``.  That
        is provider wire-format trouble, not local request validation, so it
        should follow the same retry path as a truncated JSON body.
        r   Nanthropic_messagesFexpected ident at line)	r  rs  r<  UnicodeEncodeErrorjsonJSONDecodeErrorr^   r)  rj   )r{   rv   rt   s      rS   _is_provider_stream_parse_errorz'AIAgent._is_provider_stream_parse_error  s     4T**.BBB5%,, 	5e0$2FGHH 	5e**""$$**,,'722rU   )r  rl  attemptmax_attemptsmid_tool_callc          	      6    ddl m}  || ||||||           dS )u9   Forwarder — see ``agent.stream_diag.log_stream_retry``.r   )log_stream_retry)rl  rv   r  r  r  r  N)r  r  )r{   rl  rv   r  r  r  r  r  s           rS   _log_stream_retryzAIAgent._log_stream_retry  sJ     	766666t5'%]	
 	
 	
 	
 	
 	
rU   c                4    ddl m}  || |||||           dS )u9   Forwarder — see ``agent.stream_diag.emit_stream_drop``.r   )emit_stream_drop)rv   r  r  r  r  N)r  r  )r{   rv   r  r  r  r  r  s          rS   _emit_stream_dropzAIAgent._emit_stream_drop  sH     	766666w\'d	
 	
 	
 	
 	
 	
rU   taskr   c                 L   	 |                      |          }n# t          $ r t          |          }Y nw xY w|p|j        j                                        }t          |          dk    r|dd                                         dz   }|                     d| d|            dS )z4Surface a compact warning for failed auxiliary work.   N   ...u   ⚠ Auxiliary z	 failed: )	_summarize_api_errorr   r^   r|   r}   r)  r`   rstriprY  )r{   r  r   details       rS   _emit_auxiliary_failurezAIAgent._emit_auxiliary_failure  s    	..s33FF 	 	 	XXFFF	2CM299;;v;;DSD\((**U2FCDCC6CCDDDDDs    44c           	          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dS )zBReturn the live main runtime for session-scoped auxiliary routing.r   rZ   rV   rW   r   r   )r   rV   rW   r   r   )r  r   s    rS   _current_main_runtimezAIAgent._current_main_runtime  sw     T7B//52j"55;j"55;tY339rj"55;
 
 	
rU   c                 (    ddl m}  ||            dS )uY   Forwarder — see ``agent.conversation_compression.check_compression_model_feasibility``.r   )#check_compression_model_feasibilityN)agent.conversation_compressionr  )r{   r  s     rS   $_check_compression_model_feasibilityz,AIAgent._check_compression_model_feasibility  s+    VVVVVV++D11111rU   c                 (    ddl m}  ||            dS )uP   Forwarder — see ``agent.conversation_compression.replay_compression_warning``.r   )replay_compression_warningN)r  r  )r{   r  s     rS   _replay_compression_warningz#AIAgent._replay_compression_warning  s+    MMMMMM""4(((((rU   c                     |t          |          }n/t          | dd          pt          t          | dd                    }|dk    S )z8Return True when a base URL targets OpenAI's native API.Nr   rZ   r   zapi.openai.comrK   r  r{   rW   hostnames      rS   _is_direct_openai_urlzAIAgent._is_direct_openai_url  s[    (22HHt%92>> BS/44C CH +++rU   c                 x    |"t          |                                          }nt          | dd          pd}d|v S )u  Return True when a base URL targets Azure OpenAI.

        Azure OpenAI exposes an OpenAI-compatible endpoint at
        ``{resource}.openai.azure.com/openai/v1`` that accepts the
        standard ``openai`` Python client.  Unlike api.openai.com it
        does NOT support the Responses API — gpt-5.x models are served
        on the regular ``/chat/completions`` path — so routing decisions
        must treat Azure separately from direct OpenAI.
        Nr   rZ   zopenai.azure.com)r^   rj   r  )r{   rW   urls      rS   _is_azure_openai_urlzAIAgent._is_azure_openai_url  sE     h--%%''CC$ 1266<"C!S((rU   c                     |t          |          }n/t          | dd          pt          t          | dd                    }|dk    S )zKReturn True when a base URL targets GitHub Copilot's OpenAI-compatible API.Nr   rZ   r   api.githubcopilot.comr  r  s      rS   _is_github_copilot_urlzAIAgent._is_github_copilot_url  s[    (22HHt%92>> BS/44C CH 222rU   c                     t          | j        | j                  }||S t          t	          j        dd                    S )a  Resolve the effective per-call request timeout in seconds.

        Priority:
          1. ``providers.<id>.models.<model>.timeout_seconds`` (per-model override)
          2. ``providers.<id>.request_timeout_seconds`` (provider-wide)
          3. ``HERMES_API_TIMEOUT`` env var (legacy escape hatch)
          4. 1800.0s default

        Used by OpenAI-wire chat completions (streaming and non-streaming) so
        the per-provider config knob wins over the 1800s default.  Without this
        helper, the hardcoded ``HERMES_API_TIMEOUT`` fallback would always be
        passed as a per-call ``timeout=`` kwarg, overriding the client-level
        timeout the AIAgent.__init__ path configured.
        NHERMES_API_TIMEOUTg      @)r   rV   r   floatr   getenv)r{   cfgs     rS   _resolved_api_call_timeoutz"AIAgent._resolved_api_call_timeout  s<     +4=$*EE?JRY3V<<===rU   c                     t          | j        | j                  }||dfS t          j        d          }|t          |          dfS dS )au  Resolve the base non-stream stale timeout and whether it is implicit.

        Priority:
          1. ``providers.<id>.models.<model>.stale_timeout_seconds``
          2. ``providers.<id>.stale_timeout_seconds``
          3. ``HERMES_API_CALL_STALE_TIMEOUT`` env var
          4. 90.0s default (time-to-first-byte for non-streaming / Codex
             internal-streaming requests; lowered from 300s in May 2026 so
             fallback providers kick in faster when upstream providers
             stall).  The detector still scales up for large contexts in
             ``_compute_non_stream_stale_timeout``.

        Returns ``(timeout_seconds, uses_implicit_default)`` so the caller can
        preserve legacy behaviors that only apply when the user has *not*
        explicitly configured a stale timeout, such as auto-disabling the
        detector for local endpoints.
        NFHERMES_API_CALL_STALE_TIMEOUT)g     V@T)r   rV   r   r   r  r  )r{   r  env_timeouts      rS   %_resolved_api_call_stale_timeout_basez-AIAgent._resolved_api_call_stale_timeout_base  sT    $ )
CC?:i ?@@"%%u,,zrU   api_payloadc                 &   |                                  \  }}t          | dd          p| j        pd}|r |rt          |          rt	          d          S ddlm}  ||          }|dk    rt          |d          S |d	k    rt          |d
          S |S )a^  Compute the effective non-stream stale timeout for this request.

        Accepts either the full ``api_kwargs`` dict (Chat Completions or
        Responses API) or a legacy ``messages`` list.  Context-size scaling
        applies the same way to both shapes via
        :func:`agent.chat_completion_helpers.estimate_request_context_tokens`.
        r   NrZ   infr   )estimate_request_context_tokensi g      n@iP  g     b@)r  r  rW   r   r  agent.chat_completion_helpersr  r,  )r{   r  
stale_baseuses_implicit_defaultrW   r  
est_tokenss          rS   !_compute_non_stream_stale_timeoutz)AIAgent._compute_non_stream_stale_timeout.  s     -1,V,V,X,X)
)4d33Jt}J  	 X 	 2CH2M2M 	 <<QQQQQQ44[AA
z5)))z5)))rU   c                 
   | j         dk    rdS | j        dk    p)t          | dd          dk    odt          | dd          pdv }|sdS ||n| j        pd}|                                }t          j        d	|          sdS d
|dS )uz  Return an actionable hint when this request matches a known
        Codex silent-reject configuration, else ``None``.

        The ChatGPT Codex backend (``chatgpt.com/backend-api/codex``) has
        historically silently dropped certain model requests: the connection
        is accepted but no stream events are emitted and no error is raised.
        The stale-call detector ends the hang, but a generic "timed out"
        message gives the user no path forward.

        This helper substitutes an actionable hint into the stale-timeout
        warning when the request matches a known silent-reject pattern.
        Currently flagged: ``gpt-5.5`` family on the Codex backend.  See
        hermes-agent #21444 for the symptom history.  The upstream backend
        behavior has historically come and gone with ChatGPT entitlement
        changes — the heuristic stays in place as future-proofing even when
        the symptom is dormant.

        Does NOT fix the backend issue.  Only converts an opaque stale-timeout
        into actionable text so users learn the workaround in seconds rather
        than digging through logs.
        codex_responsesNopenai-codexr   rZ   chatgpt.comz/backend-api/codexr   z(?:^|[/\-_])gpt-5\.5(?:$|[\-_])z/Codex backend appears to be silently rejecting a   on chatgpt.com/backend-api/codex (no stream events, no error). This is a known backend-side pattern that has affected ChatGPT Plus accounts intermittently. Workaround: try `gpt-5.4` on the same OAuth profile, or `gpt-5.3-codex`, or switch to a different model/provider in your fallback chain. Some ChatGPT Codex accounts do not support `gpt-5.4-codex`. See hermes-agent#21444 for symptom history.)r   rV   r  r   rj   research)r{   r   is_codex_backend	eff_modelmodel_lowers        rS   _codex_silent_hang_hintzAIAgent._codex_silent_hang_hintC  s    , =---4M^+ 2B77=H Y(WT;Lb-Q-Q-WUWX	 	   	4#/UUTZFB	oo''
 y;[II 	4:i : : :		
rU   c                 ,    t          | j        d          S )z1Return True when the base URL targets OpenRouter.openrouter.airJ   r   r   s    rS   _is_openrouter_urlzAIAgent._is_openrouter_urlw  s    $T%9?KKKrU   rV   rW   r   r   c                .    ddl m}  || ||||          S )uP   Forwarder — see ``agent.agent_runtime_helpers.anthropic_prompt_cache_policy``.r   )anthropic_prompt_cache_policyr  )r5  r  )r{   rV   rW   r   r   r  s         rS   _anthropic_prompt_cache_policyz&AIAgent._anthropic_prompt_cache_policy{  s5     	NMMMMM,,THxbjrwxxxxrU   c                     |                                  }d|v r|                    dd          d         }|                    d          S )a>  Return True for models that require the Responses API path.

        GPT-5.x models are rejected on /v1/chat/completions by both
        OpenAI and OpenRouter (error: ``unsupported_api_for_model``).
        Detect these so the correct api_mode is set regardless of
        which provider is serving the model.
        /r\   zgpt-5)rj   rsplitr_   )r   ms     rS   _model_requires_responses_apiz%AIAgent._model_requires_responses_api  sD     KKMM!88a  $A||G$$$rU   rV   c                    |pd                                                                 }|dk    rdS |dk    r"	 ddlm}  ||           S # t          $ r Y nw xY wt
                              |           S )zCReturn True when this provider/model pair should use Responses API.rZ   nousFcopilotr   )!_should_use_copilot_responses_api)r)  rj   r+  r  r   r   r  )r   rV   normalized_providerr  s       rS   &_provider_model_requires_responses_apiz.AIAgent._provider_model_requires_responses_api  s      (~24466<<>> &((5)++OOOOOO88???     44U;;;s   A	 	
AAc                     |                                  s(|                                 s|                                 rd|iS d|iS )av  Return the correct max tokens kwarg for the current provider.

        OpenAI's newer models (gpt-4o, o-series, gpt-5+) require
        'max_completion_tokens'. Azure OpenAI also requires
        'max_completion_tokens' for gpt-5.x models served via the
        OpenAI-compatible endpoint. OpenRouter, local models, and older
        OpenAI models use 'max_tokens'.
        max_completion_tokensr   )r  r  r  r   s     rS   _max_tokens_paramzAIAgent._max_tokens_param  sT     %%'' 	44+D+D+F+F 	4$JeJeJgJg 	4+U33e$$rU   contentc                 v    |sdS |                      |          }t          |                                          S )a  
        Check if content has actual text after any reasoning/thinking blocks.

        This detects cases where the model only outputs reasoning but no actual
        response, which indicates an incomplete generation that should be retried.
        Must stay in sync with _strip_think_blocks() tag variants.

        Args:
            content: The assistant message content to check

        Returns:
            True if there's meaningful content after think blocks, False otherwise
        F)_strip_think_blocksr	  r)  )r{   r  cleaneds      rS   _has_content_after_think_blockz&AIAgent._has_content_after_think_block  s>      	5 **733 GMMOO$$$rU   c                 &    ddl m}  || |          S )uE   Forwarder — see ``agent.agent_runtime_helpers.strip_think_blocks``.r   )strip_think_blocks)r5  r  )r{   r  r  s      rS   r  zAIAgent._strip_think_blocks  s'    BBBBBB!!$000rU   c                     | sdS |                                  }|sdS |                    d          rdS |                    d          rdS |d         }|dv rdS t          |          dk    rdS dS )zCHeuristic: does visible assistant text look intentionally finished?Fz```T^r  u%   .!?:)"']}。！？：）】」』》^i  )r  endswithord)r  strippedlasts      rS   _has_natural_response_endingz$AIAgent._has_natural_response_ending  s      	5>>## 	5U## 	4S!! 	4|;;;4t994urU   c                     | j         pd                                }| j        pd                                }d|vr|dk    rdS d| j        v s	d| j        v rdS t	          | j        ot          | j                            S )zHDetect the narrow backend family affected by Ollama/GLM stop misreports.rZ   glmzaiFollamaz:11434T)r   rj   rV   r   r	  rW   r   )r{   r  provider_lowers      rS   _is_ollama_glm_backendzAIAgent._is_ollama_glm_backend  s    z'R..00--24466##%(?(?5t+++x4;O/O/O4DMF&7&F&FGGGrU   finish_reasonc                    |dk    s| j         dk    rdS |                                 sdS t          d |pg D                       sdS |t          |dd          rdS t          |dd          }t	          |t
                    sdS |                     |                                          }|sdS t          |          dk     st          j
        d	|          sdS |                     |           S )
zIDetect conservative stop->length misreports for Ollama-hosted GLM models.stopchat_completionsFc              3   r   K   | ]2}t          |t                    o|                    d           dk    V  3dS )rp  toolN)rs  rt  r   )r  rm  s     rS   	<genexpr>z:AIAgent._should_treat_stop_as_truncated.<locals>.<genexpr>  sT       
 
 sD!!?cggfoo&?
 
 
 
 
 
rU   N
tool_callsr  r   z\s)r   r  anyr  rs  r^   r  r)  r`   r  r  r  )r{   r  assistant_messagerk  r  visible_texts         rS   _should_treat_stop_as_truncatedz'AIAgent._should_treat_stop_as_truncated  s&    F""dm7I&I&I5**,, 	5 
 
 B
 
 
 
 
 	 5$0A<QU(V(V$5+Y=='3'' 	5//88>>@@ 	5|r!!5,)G)G!544\BBBBrU   user_messageassistant_contentc                 *    ddl m}  || |||          S )uT   Forwarder — see ``agent.agent_runtime_helpers.looks_like_codex_intermediate_ack``.r   )!looks_like_codex_intermediate_ack)r5  r  )r{   r  r  rk  r  s        rS   "_looks_like_codex_intermediate_ackz*AIAgent._looks_like_codex_intermediate_ack  s0     	RQQQQQ00|EVX`aaarU   c                 &    ddl m}  || |          S )uD   Forwarder — see ``agent.agent_runtime_helpers.extract_reasoning``.r   )extract_reasoning)r5  r  )r{   r  r  s      rS   _extract_reasoningzAIAgent._extract_reasoning  s(    AAAAAA  '8999rU   task_idc                 &    ddl m}  || |          S )uK   Forwarder — see ``agent.chat_completion_helpers.cleanup_task_resources``.r   )cleanup_task_resources)r  r  )r{   r  r  s      rS   _cleanup_task_resourceszAIAgent._cleanup_task_resources!  s'    HHHHHH%%dG444rU   )_MEMORY_REVIEW_PROMPT_SKILL_REVIEW_PROMPT_COMBINED_REVIEW_PROMPTreview_messagesprior_snapshotc                 &    ddl m}  || |          S )uR   Forwarder — see ``agent.background_review.summarize_background_review_actions``.r   )#summarize_background_review_actions)agent.background_reviewr  )r  r  r  s      rS   $_summarize_background_review_actionsz,AIAgent._summarize_background_review_actions/  s*     	POOOOO22?NSSSrU   messages_snapshotreview_memoryreview_skillsc                     ddl m}  || |||          \  }}t          j        |dd          }|                                 dS )u^  Spawn the background memory/skill review thread.

        Thin wrapper — the heavy lifting lives in
        ``agent.background_review.spawn_background_review_thread`` which
        returns the thread target.  ``threading.Thread`` is constructed
        here so existing tests that patch ``run_agent.threading.Thread``
        keep working.
        r   )spawn_background_review_thread)r!  r"  Tz	bg-review)targetdaemonnameN)r  r$  	threadingThreadstart)r{   r   r!  r"  r$  r%  _promptts           rS   _spawn_background_reviewz AIAgent._spawn_background_review8  sh     	KJJJJJ88''	
 
 
 F4kJJJ						rU   write_originexecution_contextr  tool_call_idr/  r0  r1  c                .    ddl m}  || ||||          S )uJ   Forwarder — see ``agent.background_review.build_memory_write_metadata``.r   )build_memory_write_metadatar.  )r  r3  )r{   r/  r0  r  r1  r3  s         rS   _build_memory_write_metadataz$AIAgent._build_memory_write_metadataP  s@     	HGGGGG**%/%
 
 
 	
rU   c                    t          | dd          }t          | dd          }||dS d|cxk    rt          |          k     rAn dS ||         }t          |t                    r"|                    d          dk    r||d<   dS dS dS dS )a  Rewrite the current-turn user message before persistence/return.

        Some call paths need an API-only user-message variant without letting
        that synthetic text leak into persisted transcripts or resumed session
        history. When an override is configured for the active turn, mutate the
        in-memory messages list in place so both persistence and returned
        history stay clean.
        _persist_user_message_idxN_persist_user_message_overrider   rp  userr  )r  r`   rs  rt  r   )r{   rk  idxoverriderm  s        rS   $_apply_persist_user_message_overridez,AIAgent._apply_persist_user_message_overrideb  s     d7>>4!A4HHs{F####c(mm######3-C#t$$ *F)B)B!)I $#* *)B)BrU   conversation_historyc                     |                      |           |                     |           || _        |                     |           |                     ||           dS )zSave session state to both JSON log and SQLite on any exit path.

        Ensures conversations are never lost, even on errors or early returns.
        N))_drop_trailing_empty_response_scaffoldingr;  _session_messages_save_session_log_flush_messages_to_session_db)r{   rk  r<  s      rS   _persist_sessionzAIAgent._persist_sessiont  se    
 	66x@@@11(;;;!)x(((**85IJJJJJrU   c                    d}|rt          |d         t                    r|d                             d          s|d                             d          ri|                                 d}|rQt          |d         t                    r6|d                             d          N|d                             d          i|sdS |rt          |d         t                    ro|d                             d          dk    rP|                                 |r:t          |d         t                    r|d                             d          dk    P|rkt          |d         t                    rR|d                             d          d	k    r5|d                             d
          r|                                 dS dS dS dS dS )u  Remove private empty-response retry/failure scaffolding from transcript tails.

        Also rewinds past any trailing tool-result / assistant(tool_calls) pair
        that the failed iteration left hanging. Without this, the tail ends at
        a raw ``tool`` message and the next user turn lands as
        ``...tool, user, user`` — a protocol-invalid sequence that most
        providers silently reject (returns empty content), causing the
        empty-retry loop to fire forever. See #<TBD>.
        Fr  _empty_recovery_synthetic_empty_terminal_sentinelTNrp  r  rq  r  )rs  rt  r   ru  )r{   rk  dropped_scaffoldings      rS   r>  z1AIAgent._drop_trailing_empty_response_scaffolding  s    $		'8B<..		'   !<==			'
 B<##$>??		' LLNNN"& 		'8B<..		'   !<==			'
 B<##$>??		'" # 	F 	8B<..	   ((F22LLNNN	 	8B<..	   ((F22 	8B<..	   ((K77  .. 8 LLNNNNN	 	 	 	 8777rU   c                 &    ddl m}  || |          S )uJ   Forwarder — see ``agent.agent_runtime_helpers.repair_message_sequence``.r   )repair_message_sequence)r5  rH  )r{   rk  rH  s      rS   _repair_message_sequencez AIAgent._repair_message_sequence  s'    GGGGGG&&tX666rU   c                    | j         sdS |                     |           	 | j        s|                                  |rt	          |          nd}t          || j                  }||d         D ]}|                    dd          }|                    d          }t          |          rt          |          }nt          |t                    rg }|D ]}	t          |	t                    rP|	                    d          dk    r7|                    t          |	                    dd                               gt          |	t                    r,|	                    d          d	v r|                    d
           |rd                    |          nd}d}
t!          |d          r3t          |j        t                    r|j        rd |j        D             }
n0t          |                    d          t                    r|d         }
| j                             | j        |||                    d          |
|                    d          |                    d          |dk    r|                    d          nd|dk    r|                    d          nd|dk    r|                    d          nd|dk    r|                    d          nd|dk    r|                    d          nd           t	          |          | _        dS # t(          $ r&}t*                              d|           Y d}~dS d}~ww xY w)u%  Persist any un-flushed messages to the SQLite session store.

        Uses _last_flushed_db_idx to track which messages have already been
        written, so repeated calls (from multiple exit paths) only write
        truly new messages — preventing the duplicate-write bug (#860).
        Nr   rp  r  r  rw   textrZ   >   image	image_urlinput_imagez[screenshot]
r  c                 @    g | ]}|j         j        |j         j        d S ))r'  	arguments)functionr'  rQ  r  tcs     rS   
<listcomp>z9AIAgent._flush_messages_to_session_db.<locals>.<listcomp>  s:     ' ' ' "$!1@UVV' ' 'rU   	tool_namer1  r  rq  	reasoningreasoning_contentreasoning_detailsrr  codex_message_items)r   rp  r  rV  r  r1  r  rW  rX  rY  rr  rZ  z$Session DB append_message failed: %s)r   r;  r   r   r`   r,  _last_flushed_db_idxr   rC   rD   rs  rj  rt  r^  r^   joinr  r  append_messager   r   r   r   )r{   rk  r<  	start_idx
flush_fromrm  rp  r  _txtptool_calls_datar   s               rS   rA  z%AIAgent._flush_messages_to_session_db  s~     	F11(;;;/	F+ *'')))5IP0111qIY(ABBJ
, & &wwvy11''),, .g66 
@6w??GG.. @D$ 8 8%a.. 8155==F3J3J KKAEE&",=,=(>(>????'400 8QUU6]]Fk5k5k KK77715?diiooo4G"&3-- 8*S^T2R2R 8WZWe 8' '"%.' ' 'OO   5 5t<< 8&),&7O //##!ggk22.!$!8!8"%''/":":6:k6I6Icggk222tFJkFYFYcgg.A&B&B&B_cFJkFYFYcgg.A&B&B&B_cNRVaNaNa#''2I*J*J*JgkJNR]J]J]0E(F(F(Fcg 0     ),HD%%% 	F 	F 	FNNA1EEEEEEEEE	Fs   K+L 
L=L88L=c                     |sg S d}t          t          |          dz
  dd          D ]%}||                             d          dk    r|} n&||                                S |d|         S )a  
        Get messages up to (but not including) the last assistant turn.
        
        This is used when we need to "roll back" to the last successful point
        in the conversation, typically when the final assistant message is
        incomplete or malformed.
        
        Args:
            messages: Full message list
            
        Returns:
            Messages up to the last complete assistant turn (ending with user/tool message)
        Nr\   r  rp  rq  )ranger`   r   copy)r{   rk  last_assistant_idxis       rS   "_get_messages_up_to_last_assistantz*AIAgent._get_messages_up_to_last_assistant  s      	I "s8}}q("b11 	 	A{v&&+55%&" 6 %==??" +++,,rU   c                 $    ddl m}  ||           S )uJ   Forwarder — see ``agent.system_prompt.format_tools_for_system_message``.r   )format_tools_for_system_message)agent.system_promptrj  )r{   rj  s     rS    _format_tools_for_system_messagez(AIAgent._format_tools_for_system_message  s%    GGGGGG..t444rU   
user_query	completedc                 *    ddl m}  || |||          S )uO   Forwarder — see ``agent.agent_runtime_helpers.convert_to_trajectory_format``.r   )convert_to_trajectory_format)r5  rp  )r{   rk  rm  rn  rp  s        rS   _convert_to_trajectory_formatz%AIAgent._convert_to_trajectory_format  s+    LLLLLL++D(J	RRRrU   c                 r    | j         sdS |                     |||          }t          || j        |           dS )a  
        Save conversation trajectory to JSONL file.
        
        Args:
            messages (List[Dict]): Complete message history
            user_query (str): Original user query
            completed (bool): Whether the conversation completed successfully
        N)r   rq  _save_trajectory_to_filer   )r{   rk  rm  rn  
trajectorys        rS   _save_trajectoryzAIAgent._save_trajectory  sE     % 	F77*iXX
 TZCCCCCrU   error_contextrs   c                    |dvrdS t          | t                    sdS t          |                     d          pd                                          }t          |                     d          pd                                          }t          |                     d          pd                                          }t          |                     d          pd                                          }| d| d| d| }|                                sdS d	|v rdS d
|v rdS d|v rdS d|v rd|v rdS d|v rd|v rdS dS )uM  Detect subscription/entitlement 403s that masquerade as auth failures.

        Returned True only when the body text matches a known entitlement
        shape AND the status is 401/403.  Refreshing an OAuth token cannot
        fix an unsubscribed account, so callers should surface the error
        instead of looping the credential pool.

        Current matches:
          * xAI OAuth: "do not have an active Grok subscription" /
            "out of available resources" / "does not have permission" + "grok"

        Disambiguator for xAI (#29344): the same ``code`` text ("The caller
        does not have permission to execute the specified operation") is
        returned for BOTH an unsubscribed account AND a stale OAuth access
        token.  xAI ships an explicit signal in the ``error`` field that
        tells the two apart: a ``[WKE=unauthenticated:...]`` suffix (and/or
        the ``OAuth2 access token could not be validated`` phrasing) means
        the credentials failed validation — that's recoverable by refreshing
        the token, NOT by surfacing an entitlement message.  When either
        signal is present we return False eagerly so the credential-pool
        refresh path runs, letting long-running TUI sessions recover from
        stale tokens without an exit/reopen cycle.

        Extend here for new providers as we discover them (Anthropic's
        Claude Max OAuth entitlement errors look distinct enough today that
        the existing 1M-context-beta branch handles them; revisit if other
        subscription tiers start producing the same loop signature).
        >   N    Frt   rZ   reasonrq   rv    z[wke=unauthenticated:z*oauth2 access token could not be validatedz'do not have an active grok subscriptionTzout of available resourcesgrokzdoes not have permission)rs  rt  r^   r   rj   r)  )rv  rs   rt   rz  rq   r1  haystacks          rS   _is_entitlement_failurezAIAgent._is_entitlement_failure,  s   B ...5-.. 	5 m''	228b99??AA]&&x006B77==??=$$V,,23399;;-##G,,23399;;55555555~~ 	5 #h..578CC54@@4'833(8J8J4%11f6H6H4urU   c                 X   t          |           }t          | t                    r#d|                                v rd|dd          S d|v sd|v rt	          j        d|t          j                  }|r'|                    d                                          nd	}t	          j        d
|          }|r'|                    d                                          nd}t          | dd          }g }|r|
                    d|            |
                    |           |r|
                    d|            d                    |          S t          | dd          }t          |t                    rt          |                    d          t                    r)|                    di                               d          n|                    d          }	|	r)t          | dd          }|rd| dnd}
|
 |	dd          S t          | dd          }|rd| dnd}
|
 |dd          S )zExtract a human-readable one-liner from an API error.

        Handles Cloudflare HTML error pages (502, 503, etc.) by pulling the
        <title> tag instead of dumping raw HTML.  Falls back to a truncated
        str(error) for everything else.
        r  z'Malformed provider streaming response: Ni,  z	<!DOCTYPE<htmlz<title[^>]*>([^<]+)</title>r\   z!HTML error page (title not found)z2Cloudflare Ray ID:\s*<strong[^>]*>([^<]+)</strong>rs   zHTTP zRay u    — rz   rv   rt   : rZ   r   )r^   rs  r<  rj   r  r  
IGNORECASEgroupr)  r  r^  r\  rt  r   )rv   rawr  titlerayray_idrs   partsrz   rm  prefixs              rS   r  zAIAgent._summarize_api_errorp  s^    %jj uj))	I(CIIKK77HS#YHHH #C	8#r}MMA*+TAGGAJJ$$&&&1TE)QSVWWC-0:SYYq\\'')))dF!%==KE 42[22333LL ._F__---<<&&& ufd++dD!! 	.:DTXXgEVEVX\:]:]v$((7B''++I666cgckcklucvcvC .%e]DAA4?G00000R -#dsd)--- e]D99,7?(((((R%#dsd)%%%rU   keyc                     t          |          rt          |t                    sdS |sd S t          |          dk    rdS |d d          d|dd           S )Nz<entra-id-bearer>   z***rL   r  )callablers  r^   r`   )r{   r  s     rS   _mask_api_key_for_logszAIAgent._mask_api_key_for_logs  sn     C== 	'C!5!5 	'&& 	4s88r>>5bqb'((c"##h(((rU   	error_msgc                     |sdS |                                                     d          sd|v rdS d                    |                                          }t	          |          dk    r|dd         dz   }|S )	a  
        Clean up error messages for user display, removing HTML content and truncating.
        
        Args:
            error_msg: Raw error message from API or exception
            
        Returns:
            Clean, user-friendly error message
        zUnknown errorz<!DOCTYPE htmlr  z:Service temporarily unavailable (HTML error page returned)r{     Nr  )r)  r_   r\  splitr`   )r{   r  r  s      rS   _clean_error_messagezAIAgent._clean_error_message  s      	#"? ??''(899 	PW	=Q=QOO ((9??,,-- w<<#dsdme+GrU   c                 $    ddl m}  ||           S )uL   Forwarder — see ``agent.agent_runtime_helpers.extract_api_error_context``.r   )extract_api_error_context)r5  r  )rv   r  s     rS   _extract_api_error_contextz"AIAgent._extract_api_error_context  s(     	JIIIII((///rU   responsec                     |dS t          |dd          }|sdS ddlm} t          || j        | j                  } ||          }|                    dd           |j        |d<   |j        |d<   |S )	zLToken buckets for ``post_api_request`` plugins (no raw ``response`` object).Nusager   )asdict)rV   r   	raw_usageprompt_tokenstotal_tokens)	r  dataclassesr  r   rV   r   ru  r  r  )r{   r  r  r  cusummarys         rS   #_usage_summary_for_api_request_hookz+AIAgent._usage_summary_for_api_request_hook  s    4Hgt44	 	4&&&&&&YWWW&**K&&&#%#3 "$/rU   )rv   
api_kwargsrz  c                ,    ddl m}  || |||          S )uI   Forwarder — see ``agent.agent_runtime_helpers.dump_api_request_debug``.r   )dump_api_request_debug)rz  rv   )r5  r  )r{   r  rz  rv   r  s        rS   _dump_api_request_debugzAIAgent._dump_api_request_debug  s1     	GFFFFF%%dJvUSSSSrU   c                     | s| S t          |           } t          j        dd|           } t          j        dd|           } |                                 S )zCConvert REASONING_SCRATCHPAD to think tags and clean up whitespace.z\n+(<think>)z\n\1z(</think>)\n+z\1\n)r=   r  subr)  )r  s    rS   _clean_session_contentzAIAgent._clean_session_content  sS      	N-g66&'7;;&)7G<<}}rU   c                    | | S t          | t                    rt          |           S t          | t                    rg }| D ]}t          |t                    rt	          |          }t          |                    d          t                    rt          |d                   |d<   t          |                    d          t                    rt          |d                   |d<   |                    |           |S | S )uD  Apply secret redaction to message content (str or list-of-parts).

        Handles both plain-string content and the OpenAI/Anthropic multimodal
        shape where ``content`` is a list of ``{"type": "text", "text": ...}``
        / ``{"type": "image_url", ...}`` / ``{"type": "input_text", "content": ...}``
        parts. Image / binary parts are left untouched; only text fields are
        passed through ``redact_sensitive_text``.

        Respects ``HERMES_REDACT_SECRETS`` via ``redact_sensitive_text`` —
        when disabled the helper is effectively a no-op.
        NrK  r  )rs  r^   r   rj  rt  r   r^  )r  redactedparts      rS   _redact_message_contentzAIAgent._redact_message_content  s     ?Ngs## 	2(111gt$$ 
	H & &dD)) Q::D!$((6"2"2C88 K'<T&\'J'JV!$((9"5"5s;; Q*?Y*P*PY%%%%OrU   c                 &   t          | dd          sdS |p| j        }|sdS 	 | j        d| j         dz  }n# t          $ r Y dS w xY w	 g }|D ]}|                    d          dk    rB|                    d          r-t          |          }|                     |d                   |d<   d|v r:t          |          }|                     |                    d                    |d<   |	                    |           |
                                r	 t          j        |                    d	
                    }|                    dt          |                    dg                               }|t          |          k    r%t          j        d|t          |                     dS n# t          $ r Y nw xY w| j        | j        | j        | j        | j                                        t-          j                                                    t1          | j        pd          | j        pg t          |          |d
}t7          ||dt8                     dS # t          $ r/}| j        rt          j        d|            Y d}~dS Y d}~dS d}~ww xY w)a  Optional per-session JSON snapshot writer.

        Gated by ``sessions.write_json_snapshots`` (default False).  state.db
        is the canonical message store; this writer exists only for users
        whose external tooling consumes ``~/.hermes/sessions/session_{sid}.json``
        directly.  When the flag is off this is a fast no-op.

        When enabled, rewrites the snapshot after every persistence point with
        the full message list (assistant content normalized via
        ``_clean_session_content`` to convert REASONING_SCRATCHPAD to think
        tags).  The truncation guard ("don't overwrite a larger log with
        fewer messages") is preserved so resume + branch don't clobber a
        fuller existing snapshot.
        _session_json_enabledFNsession_.jsonrp  rq  r  utf-8encodingmessage_countrk  zHSkipping session log overwrite: existing has %d messages, current has %drZ   )
r   r   rW   rg   session_startlast_updatedr   toolsr  rk     )indentdefaultzFailed to save session log: )r  r?  logs_dirr   r   r   rt  r  r  r^  existsr  loads	read_textr`   loggingr   r   rW   rg   r  	isoformatr   nowr   r   r  rI   r^   r   r   )	r{   rk  log_filer  rm  existingexisting_countentryr   s	            rS   r@  zAIAgent._save_session_log  s    t4e<< 	F5t5 	F
	}'H$/'H'H'HHHH 	 	 	FF	6	DG $ $776??k11cggi6H6H1s))C%)%@%@Y%P%PC	N ##s))C%)%A%A#'')BTBT%U%UC	Ns####
    
#z(*<*<g*<*N*NOOH%-\\/3x||T^`bGcGcCdCd%e%eN%G44f*CLL    5 !   D #o M M!%!3!=!=!?!? ( 8 8 : :!6t7Q7WUW!X!X)r!$W# E 	       	D 	D 	D# D Bq B BCCCCCCCCCD D D D D D	DsN   6 
AAC	I BF* )I *
F74I 6F77BI 
J!JJc                    d| _         || _        | j        t          d| j                   d| _        nd| _        t          | dd          }t          | dd          }|Q|O|5  t          |          }ddd           n# 1 swxY w Y   |D ]#}	 t          d|           # t          $ r Y  w xY w| j        5  t          | j	                  }ddd           n# 1 swxY w Y   |D ]J}	 |
                    |           # t          $ r%}t                              d|           Y d}~Cd}~ww xY w| j        s?t          d|r!t          |          dk    rd	|dd          d
n	|rd	| dndz              dS dS )a  
        Request the agent to interrupt its current tool-calling loop.
        
        Call this from another thread (e.g., input handler, message receiver)
        to gracefully stop the agent and process a new message.
        
        Also signals long-running tool executions (e.g. terminal commands)
        to terminate early, so the agent can respond immediately.
        
        Args:
            message: Optional new message that triggered the interrupt.
                     If provided, the agent will include this in its response context.
        
        Example (CLI):
            # In a separate input thread:
            if user_typed_something:
                agent.interrupt(user_input)
        
        Example (Messaging):
            # When new message arrives for active session:
            if session_has_running_agent:
                running_agent.interrupt(new_message.text)
        TNF_tool_worker_threads_tool_worker_threads_lockz0Failed to propagate interrupt to child agent: %su   
⚡ Interrupt requested(   z: 'z...''rZ   )_interrupt_requested_interrupt_message_execution_thread_id_set_interrupt _interrupt_thread_signal_pendingr  rj  r   _active_children_lock_active_children	interruptr   r   r   r:  r`   )	r{   rt   _tracker_tracker_lock_worker_tids_wtidchildren_copychildr   s	            rS   r  zAIAgent.interruptb  s   0 %)!") $04!:;;;49D11 59D1 4!7>>&A4HHM$= . .#H~~. . . . . . . . . . . . . . .%  "4////    D ' 	8 	8 !677M	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8" 	T 	TET(((( T T TOQRSSSSSSSST 	V-W  2TY\]dYeYehjYjYj1Iwss|1I1I1I1I  EL  qT  qAv}  qA  qA  qA  qA  RT  U  V  V  V  V  V	V 	VsN   "A>>BBB
B,+B,6CCC'C==
D,D''D,c                    d| _         d| _        d| _        | j        t	          d| j                   t          | dd          }t          | dd          }|Q|O|5  t          |          }ddd           n# 1 swxY w Y   |D ]#}	 t	          d|           # t          $ r Y  w xY wt          | dd          }|$|5  d| _        ddd           dS # 1 swxY w Y   dS dS )zMClear any pending interrupt request and the per-thread tool interrupt signal.FNr  r  _pending_steer_lock)	r  r  r  r  r  r  rj  r   _pending_steer)r{   r  r  r  r  _steer_locks         rS   clear_interruptzAIAgent.clear_interrupt  s   $)!"&05-$05$";<<< 4!7>>&A4HHM$= . .#H~~. . . . . . . . . . . . . . .%  "5%0000    D d$94@@" + +&*#+ + + + + + + + + + + + + + + + + + #"s6   A66A:=A:B
B$#B$<CCCrK  c                 B   |r|                                 sdS |                                 }t          | dd          }|$t          | dd          }|r|dz   |z   n|| _        dS |5  | j        r| j        dz   |z   | _        n|| _        ddd           n# 1 swxY w Y   dS )a  
        Inject a user message into the next tool result without interrupting.

        Unlike interrupt(), this does NOT stop the current tool call. The
        text is stashed and the agent loop appends it to the LAST tool
        result's content once the current tool batch finishes. The model
        sees the steer as part of the tool output on its next iteration.

        Thread-safe: callable from gateway/CLI/TUI threads. Multiple calls
        before the drain point concatenate with newlines.

        Args:
            text: The user text to inject. Empty strings are ignored.

        Returns:
            True if the steer was accepted, False if the text was empty.
        Fr  Nr  rO  T)r)  r  r  )r{   rK  r  _lockr  s        rS   steerzAIAgent.steer  s   $  	4::<< 	5**,,3T::= t%5t<<HAI"V8d?W#<#<wD4 	. 	." .&*&9D&@7&J##&-#		. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.
 ts   &"BBBc                     t          | dd          }|t          | dd          }d| _        |S |5  | j        }d| _        ddd           n# 1 swxY w Y   |S )zReturn the pending steer text (if any) and clear the slot.

        Safe to call from the agent execution thread after appending tool
        results. Returns None when no steer is pending.
        r  Nr  )r  r  )r{   r  rK  s      rS   _drain_pending_steerzAIAgent._drain_pending_steer  s     3T::=4!1488D"&DK 	' 	'&D"&D	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' s   AAArV  resultis_errorc                    |t           vrdS t          | dd          }|dS t          ||          }|sdS t          ||          }|r$|s"t	          |          }|D ]}	|	|vr||d||	<   dS |D ]}	|                    |	d           dS )a  Record a ``write_file`` / ``patch`` outcome for the turn-end verifier.

        On failure, store ``{path: {error_preview, tool}}`` entries.  On
        success, remove any prior failure entries for the same paths (the
        model recovered within the turn).  Silently no-ops if the per-turn
        state dict hasn't been initialised yet (e.g. a tool dispatched
        outside ``run_conversation``).
        N_turn_failed_file_mutations)r  error_preview)_FILE_MUTATING_TOOLSr  rF   r<   rG   ru  )
r{   rV  r   r  r  statetargetslandedpreviewpaths
             rS   _record_file_mutation_resultz$AIAgent._record_file_mutation_result  s     000F;TBB=F0DAA 	F,Y?? 	&F 	&,V44G   u$$ ))0# #E$K    & &		$%%%%& &rU   c                    	 ddl }|j                            d          }|(|                                                                dvS 	 ddlm}  |            pi }n# t          $ r i }Y nw xY wt          |t                    r|                    d          nd}t          |t                    r&d|v r"t          |                    d                    S n# t          $ r Y nw xY wdS )	ao  Check whether the per-turn file-mutation verifier footer is on.

        Config path: ``display.file_mutation_verifier`` (bool, default True).
        ``HERMES_FILE_MUTATION_VERIFIER`` env var overrides config.  Exposed
        as a method so tests can patch a single seam without reaching into
        the private ``_turn_failed_file_mutations`` state dict.
        r   NHERMES_FILE_MUTATION_VERIFIER>   0noofffalseload_configdisplayfile_mutation_verifierT)r   r   r   r)  rj   hermes_cli.configr  r   rs  rt  r	  )r{   _osenv_load_config_cfg_displays         rS   _file_mutation_verifier_enabledz'AIAgent._file_mutation_verifier_enabled"  s   	+//"ABBCyy{{((**2MMMIIIIII#|~~+   .8t.D.DNtxx	***$H(D)) D.F(.R.RHLL)ABBCCC 	 	 	D	ts7   AC A C A-*C ,A--A)C 
C%$C%failedc           	      
   | sdS dt          |            dg}d}|                                 D ]\  }}|dk    r n|                    d          pd                                }|                    d          pd}|r|                    d	| d
| d|            n|                    d	| d
| d           |dz  }t          |           |z
  }|dk    r|                    d| d           d                    |          S )a   Render the per-turn failed-mutation dict as a user-facing footer.

        Displays up to 10 paths with their first error preview, then a
        count of any additional failures.  Returns an empty string when
        the dict is empty so callers can concatenate unconditionally.
        rZ   u   ⚠️ File-mutation verifier: z file(s) were NOT modified this turn despite any wording above that may suggest otherwise. Run `git status` or `read_file` to confirm.r   r   r  r  patch     • u    — [z] z] failedr\   u     • … and z morerO  )r`   r  r   r)  r^  r\  )r  linesshownr  infor  r  	remainings           rS   $_format_file_mutation_failure_footerz,AIAgent._format_file_mutation_failure_footer=  sN     	2&6{{& & &
  ,,.. 		 		JD${{xx006B==??G88F##.wD BCdCC$CC'CCDDDD@d@@$@@@AAAQJEEKK%'	q==LL:):::;;;yyrU   num_tool_msgsc                 (    ddl m}  || ||          S )uV   Forwarder — see ``agent.agent_runtime_helpers.apply_pending_steer_to_tool_results``.r   )#apply_pending_steer_to_tool_results)r5  r  )r{   rk  r
  r  s       rS   $_apply_pending_steer_to_tool_resultsz,AIAgent._apply_pending_steer_to_tool_results]  s)    SSSSSS224=QQQrU   descc                     t          j                     | _        || _        t          j                            d          r$	 ddlm}  |             dS # t          $ r Y dS w xY wdS )u  Update the last-activity timestamp and description (thread-safe).

        Also bridges to the kanban board's heartbeat fields when this
        process is a dispatcher-spawned worker (HERMES_KANBAN_TASK set),
        so the dispatcher watchdog doesn't reclaim an actively-running
        worker as stale (#31752). Bridge is rate-limited (60s) and
        best-effort — it never raises into the agent loop.
        HERMES_KANBAN_TASKr   )!heartbeat_current_worker_from_envN)	time_last_activity_ts_last_activity_descr   r   r   tools.kanban_toolsr  r   )r{   r  r  s      rS   _touch_activityzAIAgent._touch_activityb  s     "&#' :>>.// 		PPPPPP1133333   
 			 		s    A 
A A c                     |dS t          |dd          }|sdS 	 ddlm}  ||| j                  }|	|| _        dS dS # t
          $ r Y dS w xY w)zParse x-ratelimit-* headers from an HTTP response and cache the state.

        Called after each streaming API call.  The httpx Response object is
        available on the OpenAI SDK Stream via ``stream.response``.
        Nheadersr   )parse_rate_limit_headersr  )r  agent.rate_limit_trackerr  rV   _rate_limit_stater   )r{   r  r  r  r  s        rS   _capture_rate_limitszAIAgent._capture_rate_limitsx  s      F-D99 	F	IIIIII,,Wt}MMME ).&&& !  	 	 	DD	s   !A   
AAc                     | j         S )z1Return the last captured RateLimitState, or None.)r  r   s    rS   get_rate_limit_statezAIAgent.get_rate_limit_state  s    %%rU   c                 |   |dS t          |dd          }|sdS 	 |                    d          }|sdS |                                dk    r2| xj        dz  c_        t                              d| j                   dS t                              d|                                           dS # t          $ r Y dS w xY w)zRead X-OpenRouter-Cache-Status from response headers and log it.

        Increments ``_or_cache_hits`` on HIT so callers can report savings.
        Nr  zx-openrouter-cache-statusHITr\   z)OpenRouter response cache HIT (total: %d)zOpenRouter response cache %s)r  r   upper_or_cache_hitsr   r  r   r   )r{   r  r  r\  s       rS   _check_openrouter_cache_statusz&AIAgent._check_openrouter_cache_status  s    
  F-D99 	F
	[[!<==F ||~~&&##q(##GI\]]]]];V\\^^LLLLL 	 	 	DD	s   B- AB- >-B- -
B;:B;c           	          t          j                     | j        z
  }| j        | j        t          |d          | j        | j        | j        | j        j        | j        j	        dS )zReturn a snapshot of the agent's current activity for diagnostics.

        Called by the gateway timeout handler to report what the agent was doing
        when it was killed, and by the periodic "still working" notifications.
        r\   )last_activity_tslast_activity_descseconds_since_activitycurrent_toolapi_call_countr   budget_used
budget_max)
r  r  r  round_current_tool_api_call_countr   r   used	max_total)r{   elapseds     rS   get_activity_summaryzAIAgent.get_activity_summary  sc     )++ 66 $ 6"&":&+GQ&7&7 ."2"105/9	
 	
 		
rU   c                 h   | j         rY	 | j                             |pg            n# t          $ r Y nw xY w	 | j                                          n# t          $ r Y nw xY wt	          | d          r?| j        r:	 | j                            | j        pd|pg            dS # t          $ r Y dS w xY wdS dS )uC  Shut down the memory provider and context engine — call at actual session boundaries.

        This calls on_session_end() then shutdown_all() on the memory
        manager, and on_session_end() on the context engine.
        NOT called per-turn — only at CLI exit, /reset, gateway
        session expiry, etc.
        r   rZ   N)_memory_managerr   r   shutdown_allr  r   r   r{   rk  s     rS   shutdown_memory_providerz AIAgent.shutdown_memory_provider  s!     	$33HNCCCC   $113333    4-.. 	43J 	'66O)rN        	 	 	 	s/   & 
33A 
AA9$B 
B-,B-c                    | j         r.	 | j                             |pg            n# t          $ r Y nw xY wt          | d          r?| j        r:	 | j                            | j        pd|pg            dS # t          $ r Y dS w xY wdS dS )u  Trigger end-of-session extraction without tearing providers down.
        Called when session_id rotates (e.g. /new, context compression);
        providers keep their state and continue running under the old
        session_id — they just flush pending extraction now.r   rZ   N)r4  r   r   r  r   r   r6  s     rS   commit_memory_sessionzAIAgent.commit_memory_session  s    
  	$33HNCCCC    4-.. 	43J 	'66O)rN        	 	 	 	s   & 
33$A4 4
BB)rk  original_user_messagefinal_responseinterruptedc                    |rdS | j         r|r|sdS 	 d| j        pdi}|||d<    | j         j        ||fi | | j                             || j        pd           dS # t          $ r Y dS w xY w)us  Mirror a completed turn into external memory providers.

        Called at the end of ``run_conversation`` with the cleaned user
        message (``original_user_message``) and the finalised assistant
        response.  The external memory backend gets both ``sync_all`` (to
        persist the exchange) and ``queue_prefetch_all`` (to start
        warming context for the next turn) in one shot.

        Uses ``original_user_message`` rather than ``user_message``
        because the latter may carry injected skill content that bloats
        or breaks provider queries.

        Interrupted turns are skipped entirely (#15218).  A partial
        assistant output, an aborted tool chain, or a mid-stream reset
        is not durable conversational truth — mirroring it into an
        external memory backend pollutes future recall with state the
        user never saw completed.  The prefetch is gated on the same
        flag: the user's next message is almost certainly a retry of
        the same intent, and a prefetch keyed on the interrupted turn
        would fire against stale context.

        Normal completed turns still sync as before.  The whole body is
        wrapped in ``try/except Exception`` because external memory
        providers are strictly best-effort — a misconfigured or offline
        backend must not block the user from seeing their response.
        Nr   rZ   rk  )r   )r4  r   sync_allqueue_prefetch_allr   )r{   r:  r;  r<  rk  sync_kwargss         rS   _sync_external_memory_for_turnz&AIAgent._sync_external_memory_for_turn  s    D  	F$ 	 	<Q 	F	')>B?K#*2J')D )%    
  33%?0b 4       	 	 	DD	s   A	A 
A,+A,c                    	 | j         5  t          | j                  }| j                                         ddd           n# 1 swxY w Y   |D ]M}	 |                                 # t
          $ r) 	 |                                 n# t
          $ r Y nw xY wY Jw xY wn# t
          $ r Y nw xY w	 t          | dd          }|!|                     |dd           d| _	        dS dS # t
          $ r Y dS w xY w)u  Release LLM client resources WITHOUT tearing down session tool state.

        Used by the gateway when evicting this agent from _agent_cache for
        memory-management reasons (LRU cap or idle TTL) — the session may
        resume at any time with a freshly-built AIAgent that reuses the
        same task_id / session_id, so we must NOT kill:
          - process_registry entries for task_id (user's bg shells)
          - terminal sandbox for task_id (cwd, env, shell state)
          - browser daemon for task_id (open tabs, cookies)
          - memory provider (has its own lifecycle; keeps running)

        We DO close:
          - OpenAI/httpx client pool (big chunk of held memory + sockets;
            the rebuilt agent gets a fresh client anyway)
          - Active child subagents (per-turn artefacts; safe to drop)

        Safe to call multiple times.  Distinct from close() — which is the
        hard teardown for actual session boundaries (/new, /reset, session
        expiry).
        Nclientcache_evictTrz  shared)
r  rj  r  rg  release_clientsr   closer  _close_openai_clientrC  )r{   childrenr  rC  s       rS   rG  zAIAgent.release_clients!	  s   ,	+ . . 566%++---. . . . . . . . . . . . . . . "  ))++++    $   	  	 	 	D		T8T22F!))&t)TTT" "!  	 	 	DD	s   B .AB AB 
AB A('B (
B3BB
BBBBB BB 
B,+B,02C& &
C43C4c                    t          | dd          pd}	 ddlm} |                    |           n# t          $ r Y nw xY w	 t          |           n# t          $ r Y nw xY w	 t          |           n# t          $ r Y nw xY w	 | j        5  t          | j	                  }| j	        
                                 ddd           n# 1 swxY w Y   |D ]'}	 |                                 # t          $ r Y $w xY wn# t          $ r Y nw xY w	 t          | dd          }|!|                     |dd	
           d| _        dS dS # t          $ r Y dS w xY w)a   Release all resources held by this agent instance.

        Cleans up subprocess resources that would otherwise become orphans:
        - Background processes tracked in ProcessRegistry
        - Terminal sandbox environments
        - Browser daemon sessions
        - Active child agents (subagent delegation)
        - OpenAI/httpx client connections

        Safe to call multiple times (idempotent).  Each cleanup step is
        independently guarded so a failure in one does not prevent the rest.
        r   NrZ   r   )process_registry)r  rC  agent_closeTrE  )r  tools.process_registryrL  kill_allr   r   r   r  rj  r  rg  rH  rI  rC  )r{   r  rL  rJ  r  rC  s         rS   rH  zAIAgent.closeP	  s9    $d339r	??????%%g%6666 	 	 	D		w 	 	 	D		G$$$$ 	 	 	D	
	+ . . 566%++---. . . . . . . . . . . . . . . "  KKMMMM    D
  	 	 	D		T8T22F!))&t)TTT" "!  	 	 	DD	s   2 
??A 
A A $A4 4
B BC< .C:C< C

C< C
C< C+*C< +
C85C< 7C88C< <
D	D	2E 
EEhistoryc                    d}t          |          D ]}|                    d          dk    r|                    dd          }d|vr7	 t          j        |          }d|v r%t	          |d         t
                    r
|d         } nv# t          j        t          f$ r Y w xY w|rP| j        	                    |d	           | j
        s-|                     | j         d
t          |           d           t          d           dS )a  
        Recover todo state from conversation history.
        
        The gateway creates a fresh AIAgent per message, so the in-memory
        TodoStore is empty. We scan the history for the most recent todo
        tool response and replay it to reconstruct the state.
        Nrp  r  r  rZ   z"todos"todosF)mergeu   📋 Restored z todo item(s) from history)reversedr   r  r  rs  rj  r  	TypeError_todo_storewriter   rG  r   r`   r  )r{   rP  last_todo_responserm  r  datas         rS   _hydrate_todo_storezAIAgent._hydrate_todo_store	  s6    "G$$ 	 	Cwwv&((ggi,,G''z'**d??z$w-'F'F?)-g&E()4     	t""#5U"CCC? trrsCU?V?Vrrrsssus   
;BB! B!c                     | j         S )z)Check if an interrupt has been requested.)r  r   s    rS   is_interruptedzAIAgent.is_interrupted	  s     ((rU   system_messagec                 (    ddl m}  || |          S )uD   Forwarder — see ``agent.system_prompt.build_system_prompt_parts``.r   )build_system_prompt_partsr]  )rk  r_  )r{   r]  r_  s      rS   _build_system_prompt_partsz"AIAgent._build_system_prompt_parts	  s*    AAAAAA((nMMMMrU   c                 (    ddl m}  || |          S )u>   Forwarder — see ``agent.system_prompt.build_system_prompt``.r   )build_system_promptr`  )rk  rc  )r{   r]  rc  s      rS   _build_system_promptzAIAgent._build_system_prompt	  s*    ;;;;;;""4GGGGrU   c                     t          | t                    r.|                     dd          p|                     dd          pdS t          | dd          pt          | dd          pdS )z8Extract call ID from a tool_call entry (dict or object).call_idrZ   idrs  rt  r   r  )rT  s    rS   _get_tool_call_id_staticz AIAgent._get_tool_call_id_static	  sk     b$ 	C66)R((BBFF4,<,<BBr9b))HWRr-B-BHbHrU   c                     t          | t                    rD|                     d          }t          |t                    r|                    dd          pdS dS t          | dd          }t          |dd          pdS )ab  Extract function name from a tool_call entry (dict or object).

        Gemini's OpenAI-compatibility endpoint requires every `role: tool`
        message to carry the matching function name. OpenAI/Anthropic/ollama
        tolerate its absence, so the field is best-effort: callers fall back
        to "" and the message still works elsewhere.
        rR  r'  rZ   Nrh  )rT  r>  s     rS   _get_tool_call_name_staticz"AIAgent._get_tool_call_name_static	  s     b$ 	
##B"d## 0vvfb))/R/2RT**r62&&,",rU   >   r  r8  ri   rR  rq  	developerc                 $    ddl m}  ||           S )uH   Forwarder — see ``agent.agent_runtime_helpers.sanitize_api_messages``.r   )sanitize_api_messages)r5  rn  )rk  rn  s     rS   _sanitize_api_messageszAIAgent._sanitize_api_messages	  s(     	FEEEEE$$X...rU   rm  c                 f   t          | t                    r|                     d          dk    rdS |                     d          rdS |                     d          }t          |t                    r|                                rdS nt          |t
                    r|D ]}t          |t                    s|r dS |                    d          }|dv r7|dk    rC|                    dd	          }t          |t                    r|                                r dS  dS n
||d	k    rdS |                     d          p|                     d          }t          |t                    r|                                rdS |                     d          }t          |t
                    r|rdS dS )um  Return True if ``msg`` is an assistant turn whose only payload is reasoning.

        "Thinking-only" means the model emitted reasoning (``reasoning`` or
        ``reasoning_content``) but no visible text and no tool_calls. When sent
        back to providers that convert reasoning into thinking blocks (native
        Anthropic, OpenRouter Anthropic, third-party Anthropic-compatible
        gateways), the resulting message has only thinking blocks — which
        Anthropic rejects with HTTP 400 "The final block in an assistant
        message cannot be `thinking`."

        Symmetric with Claude Code's ``filterOrphanedThinkingOnlyMessages``
        (src/utils/messages.ts). We drop the whole turn from the API copy
        rather than fabricating stub text — the message log (UI transcript)
        keeps the reasoning block; only the wire copy is cleaned.
        rp  rq  Fr  r  rw   >   thinkingredacted_thinkingrK  rZ   NrX  rW  TrY  )rs  rt  r   r^   r)  rj  )rm  r  blockbtyperK  rW  rds          rS   _is_thinking_only_assistantz#AIAgent._is_thinking_only_assistant	  s   " #t$$ 	;(F(F577<   	5'')$$gs## 	}} u&& 	   !%..  %$uu		&))===F?? 99VR00D!$,, % %$uuuu  W]]5GG/00HCGGK4H4H	i%% 	)//*;*; 	4WW())b$ 	B 	4urU   c                 $    ddl m}  ||           S )uU   Forwarder — see ``agent.agent_runtime_helpers.drop_thinking_only_and_merge_users``.r   )"drop_thinking_only_and_merge_users)r5  rx  )rk  rx  s     rS   #_drop_thinking_only_and_merge_usersz+AIAgent._drop_thinking_only_and_merge_users
  s(    
 	SRRRRR11(;;;rU   r  c                 D   ddl m}  |            }t          d | D                       }||k    r| S d}g }| D ]H}|j        j        dk    r!||k     r|                    |           |dz  }3|                    |           It                              d||z
  |           |S )aj  Truncate excess delegate_task calls to max_concurrent_children.

        The delegate_tool caps the task list inside a single call, but the
        model can emit multiple separate delegate_task tool_calls in one
        turn.  This truncates the excess, preserving all non-delegate calls.

        Returns the original list if no truncation was needed.
        r   )_get_max_concurrent_childrenc              3   :   K   | ]}|j         j        d k    dV  dS )delegate_taskr\   N)rR  r'  rS  s     rS   r  z3AIAgent._cap_delegate_task_calls.<locals>.<genexpr>*
  s1      [[2r{7G?7Z7ZQ7Z7Z7Z7Z[[rU   r}  r\   zUTruncated %d excess delegate_task call(s) to enforce max_concurrent_children=%d limit)tools.delegate_toolr{  sumrR  r'  r^  r   r   )r  r{  max_childrendelegate_countkept_delegates	truncatedrT  s          rS   _cap_delegate_task_callsz AIAgent._cap_delegate_task_calls
  s     	EDDDDD3355[[[[[[[\))	 	% 	%B{?22!L00$$R((("a'N  $$$$/\)<	
 	
 	

 rU   c                 L   t                      }g }| D ]n}|j        j        |j        j        f}||vr+|                    |           |                    |           It                              d|j        j                   ot          |          t          |           k     r|n| S )zRemove duplicate (tool_name, arguments) pairs within a single turn.

        Only the first occurrence of each unique pair is kept.
        Returns the original list if no duplicates were found.
        zRemoved duplicate tool call: %s)	setrR  r'  rQ  addr^  r   r   r`   )r  seenuniquerT  r  s        rS   _deduplicate_tool_callszAIAgent._deduplicate_tool_calls=
  s     EE 	T 	TB;#R[%:;C$b!!!!@"+BRSSSSVs:66vvJFrU   c                 &    ddl m}  || |          S )uC   Forwarder — see ``agent.agent_runtime_helpers.repair_tool_call``.r   )repair_tool_call)r5  r  )r{   rV  r  s      rS   _repair_tool_callzAIAgent._repair_tool_callO
  s'    @@@@@@i000rU   c                 (    ddl m}  ||            dS )uC   Forwarder — see ``agent.system_prompt.invalidate_system_prompt``.r   )invalidate_system_promptN)rk  r  )r{   r  s     rS   _invalidate_system_promptz!AIAgent._invalidate_system_promptT
  s+    @@@@@@  &&&&&rU   fn_namerQ  indexc                 $    t          | ||          S )u  Generate a deterministic call_id from tool call content.

        Used as a fallback when the API doesn't provide a call_id.
        Deterministic IDs prevent cache invalidation — random UUIDs would
        make every API call's prefix unique, breaking OpenAI's prompt cache.
        )_codex_deterministic_call_id)r  rQ  r  s      rS   r5   zAIAgent._deterministic_call_idY
  s     ,GYFFFrU   raw_idc                      t          |           S )z8Split a stored tool id into (call_id, response_item_id).)_codex_split_responses_tool_id)r  s    rS   r6   z AIAgent._split_responses_tool_idc
  s     .f555rU   rf  response_item_idc                 "    t          ||          S )zCBuild a valid Responses `function_call.id` (must start with `fc_`).)(_codex_derive_responses_function_call_id)r{   rf  r  s      rS   r4   z*AIAgent._derive_responses_function_call_idh
  s     8AQRRRrU   c                 J    t          j                    }|j         d|j         S )N:)r(  current_threadr'  ident)r{   threads     rS   _thread_identityzAIAgent._thread_identityp
  s(    )+++.....rU   c                     t          | dd          }t          | dd          }t          | dd          }d|                                  d| d| d| S )	NrV   r  rW   r   zthread=z
 provider=z
 base_url=z model=)r  r  )r{   rV   rW   r   s       rS   _client_log_contextzAIAgent._client_log_contextt
  s}    4Y774Y77gy111d++-- 1 1 1 1 1 1).1 1	
rU   c                 `    t          | dd           }|t          j                    }|| _        |S )N_client_lock)r  r(  RLockr  )r{   locks     rS   _openai_client_lockzAIAgent._openai_client_lock}
  s2    t^T22<?$$D $DrU   rC  c                    ddl m} t          | |          rdS t          | dd          }|-t	          |          r |            rdS nt          |          rdS t          | dd          }|t          t          |dd                    S dS )a  Check if an OpenAI client is closed.

        Handles both property and method forms of is_closed:
        - httpx.Client.is_closed is a bool property
        - openai.OpenAI.is_closed is a method returning bool

        Prior bug: getattr(client, "is_closed", False) returned the bound method,
        which is always truthy, causing unnecessary client recreation on every call.
        r   MockF	is_closedNT_client)unittest.mockr  rs  r  r  r	  )rC  r  is_closed_attrhttp_clients       rS   _is_openai_client_closedz AIAgent._is_openai_client_closed
  s     	'&&&&&fd## 	5 d;;%'' !>##  4 n%% tfi66"[%@@AAAurU   c                    	 dd l }dd l}|j        |j        dfg}t	          |d          rg|                    |j        |j        df           |                    |j        |j        df           |                    |j        |j	        df           n2t	          |d          r"|                    |j        |j
        df           t          |           }|                    |                    |          |	          S # t          $ r Y d S w xY w)
Nr   r\   TCP_KEEPIDLE   r      TCP_KEEPALIVE)socket_options)	transportproxy)httpxsocket
SOL_SOCKETSO_KEEPALIVEr  r^  IPPROTO_TCPr  TCP_KEEPINTVLTCP_KEEPCNTr  r   ClientHTTPTransportr   )rW   _httpx_socket
_sock_opts_proxys        rS   _build_keepalive_http_clientz$AIAgent._build_keepalive_http_client
  sF   	""""$$$$"-w/CQGHJw// T!!7#68Lb"QRRR!!7#68Mr"RSSS!!7#68KQ"OPPPP/22 T!!7#68Mr"RSSS
 -X66F== ..j.II !     	 	 	44	s   C:C= =
D
Dclient_kwargsrF  c                ,    ddl m}  || |||          S )uG   Forwarder — see ``agent.agent_runtime_helpers.create_openai_client``.r   )create_openai_clientrE  )r5  r  )r{   r  rz  rF  r  s        rS   _create_openai_clientzAIAgent._create_openai_client
  s.    DDDDDD##D-vVVVVrU   c                 $    ddl m}  ||           S )uJ   Forwarder — see ``agent.agent_runtime_helpers.force_close_tcp_sockets``.r   )force_close_tcp_sockets)r5  r  )rC  r  s     rS   _force_close_tcp_socketsz AIAgent._force_close_tcp_sockets
  s(     	HGGGGG&&v...rU   c                R   |d S |                      |          }	 |                                 t                              d||||                                            d S # t
          $ r;}t                              d|||                                 |           Y d }~d S d }~ww xY w)Nz<OpenAI client closed (%s, shared=%s, tcp_force_closed=%d) %sz6OpenAI client close failed (%s, shared=%s) %s error=%s)r  rH  r   r  r  r   r   )r{   rC  rz  rF  force_closedr   s         rS   rI  zAIAgent._close_openai_client
  s    >F 44V<<	LLNNNKKN((**      	 	 	LLH((**        	s   AA! !
B&+0B!!B&c                   |                                  5  t          | dd           }	 |                     | j        |d          }nR# t          $ rE}t
                              d||                                 |           Y d }~d d d            dS d }~ww xY w|| _        d d d            n# 1 swxY w Y   | 	                    |d| d           dS )NrC  TrE  z7Failed to rebuild shared OpenAI client (%s) %s error=%sFzreplace:)
r  r  r  _client_kwargsr   r   r   r  rC  rI  )r{   rz  
old_client
new_clientr   s        rS   _replace_primary_openai_clientz&AIAgent._replace_primary_openai_client
  sU   %%'' 	% 	% x66J	!778KTZcg7hh

   M,,..	   uuu	% 	% 	% 	% 	% 	% 	% 	% %DK	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	!!*5H5H5HQU!VVVts:   B+AB+
B/B?B+B
B++B/2B/c                   |                                  5  t          | dd           }|#|                     |          s|cd d d            S d d d            n# 1 swxY w Y   t                              d||                                            |                     d|           st          d          |                                  5  | j        cd d d            S # 1 swxY w Y   d S )NrC  zCDetected closed shared OpenAI client; recreating before use (%s) %szrecreate_closed:rz  z'Failed to recreate closed OpenAI client)	r  r  r  r   r   r  r  RuntimeErrorrC  )r{   rz  rC  s      rS   _ensure_primary_openai_clientz%AIAgent._ensure_primary_openai_client
  s   %%'' 	 	T8T22F!$*G*G*O*O!	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	
 	Q$$&&	
 	
 	

 22:UV:U:U2VV 	JHIII%%'' 	 	;	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s#   *AAAC!!C%(C%c                 $    ddl m}  ||           S )uK   Forwarder — see ``agent.agent_runtime_helpers.cleanup_dead_connections``.r   )cleanup_dead_connections)r5  r  )r{   r  s     rS   _cleanup_dead_connectionsz!AIAgent._cleanup_dead_connections
  s%    HHHHHH''---rU   c                    t          | t                    sdS g }|                     d          }t          |t                    r|                    |           |                     d          }t          |t                    r|                    |           dt
          dt          ffdt          fd|D                       S )zHReturn True when the outbound request still contains native image parts.Frk  inputr   rM   c                 "   t          | t                    rH|                     d          }|dv rdS t          fd|                                 D                       S t          | t
                    rt          fd| D                       S dS )Nrw   >   rM  rN  Tc              3   .   K   | ]} |          V  d S r   r  r  r  _contains_images     rS   r  zPAIAgent._api_kwargs_have_image_parts.<locals>._contains_image.<locals>.<genexpr>  s-      FF!??1--FFFFFFrU   c              3   .   K   | ]} |          V  d S r   r  r  s     rS   r  zPAIAgent._api_kwargs_have_image_parts.<locals>._contains_image.<locals>.<genexpr>  s-      ==!??1--======rU   F)rs  rt  r   r  valuesrj  )r   ptyper  s     rS   r  z=AIAgent._api_kwargs_have_image_parts.<locals>._contains_image  s    %&& G		&))8884FFFFu||~~FFFFFF%&& >====u======5rU   c              3   .   K   | ]} |          V  d S r   r  )r  itemr  s     rS   r  z7AIAgent._api_kwargs_have_image_parts.<locals>.<genexpr>  s-      @@T??4((@@@@@@rU   )rs  rt  r   rj  extendr   r	  r  )r  
candidatesrk  response_inputr  s       @rS   _api_kwargs_have_image_partsz$AIAgent._api_kwargs_have_image_parts  s     *d++ 	5
>>*--h%% 	(h''' $00nd++ 	.n---	3 	4 	 	 	 	 	 	 @@@@Z@@@@@@rU   	is_visionc                (    ddl m}  |d|          S )Nr   )copilot_request_headersT)is_agent_turnr  )hermes_cli.copilot_authr  )r{   r  r  s      rS   _copilot_headers_for_requestz$AIAgent._copilot_headers_for_request  s*    CCCCCC&&TYOOOOrU   )r  c                   ddl m} |                     |          }t          ||          r|S |                                 5  t          | j                  }d d d            n# 1 swxY w Y   d|d<   t          t          |	                    dd                    d          r0| 
                    |pi           r|                     d	          |d
<   |                     ||d          S )Nr   r  r  max_retriesrW   rZ   r  T)r  default_headersFrE  )r  r  r  rs  r  rt  r  rJ   r^   r   r  r  r  )r{   rz  r  r  primary_clientrequest_kwargss         rS   _create_request_openai_clientz%AIAgent._create_request_openai_client$  sH   &&&&&&;;6;JJnd++ 	"!!%%'' 	7 	7!$"566N	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 )*}%!#n&8&8R&H&H"I"IKbcc	b11*2BCC	b 150Q0Q\`0Q0a0aN,-)).PU)VVVs   A$$A(+A(c                6    |                      ||d           d S )NFrE  )rI  )r{   rC  rz  s      rS   _close_request_openai_clientz$AIAgent._close_request_openai_client>  s#    !!&!FFFFFrU   c                &   |dS 	 |                      |          }t                              d|||                                            dS # t          $ r:}t                              d||                                 |           Y d}~dS d}~ww xY w)u  Cross-thread abort: shut sockets down without releasing FDs.

        Companion to :meth:`_close_request_openai_client` for stranger-thread
        callers (interrupt-check loop, stale-call detector). Calling
        ``client.close()`` from a thread that does not own the active httpx
        connection raced the still-live SSL BIO and corrupted unrelated file
        descriptors when the kernel recycled the just-freed TCP FD (#29507).

        Here we only ``shutdown(SHUT_RDWR)`` the pool's sockets. That unblocks
        the owning worker thread's pending ``recv``/``send`` with an EOF or
        ``EPIPE`` so it can unwind and close ``client`` from its own context
        — which is where the FD release belongs.
        Nz`OpenAI client aborted (%s, shared=False, tcp_force_closed=%d, deferred_close=stranger_thread) %sz9OpenAI client abort failed (%s, shared=False) %s error=%s)r  r   r  r  r   r   )r{   rC  rz  shutdown_countr   s        rS   _abort_request_openai_clientz$AIAgent._abort_request_openai_clientA  s     >F	!::6BBNKK5((**      	 	 	LLK((**	        	s   AA 
B/BBon_first_deltac                 *    ddl m}  || |||          S )u;   Forwarder — see ``agent.codex_runtime.run_codex_stream``.r   )run_codex_stream)agent.codex_runtimer  )r{   r  rC  r  r  s        rS   _run_codex_streamzAIAgent._run_codex_streamb  s+    888888j&.IIIrU   c                 (    ddl m}  || ||          S )uK   Forwarder — see ``agent.codex_runtime.run_codex_create_stream_fallback``.r   ) run_codex_create_stream_fallback)r  r  )r{   r  rC  r  s       rS   !_run_codex_create_stream_fallbackz)AIAgent._run_codex_create_stream_fallbackg  s)    HHHHHH//j&IIIrU   c                   | j         dk    s	| j        dvrdS 	 | j        dk    rddlm}  |d          }nddlm}  |d          }n9# t
          $ r,}t                              d	| j        |           Y d }~dS d }~ww xY wt          |	                    d
          pd          
                                }t          | j        pd          
                                }|r*|r(||k    r"t                              d| j                   dS 	 | j        dk    rddlm}  ||          }nddlm}  ||          }n9# t
          $ r,}t                              d| j        |           Y d }~dS d }~ww xY w|	                    d
          }	|	                    d          }
t          |	t                    r|	
                                sdS t          |
t                    r|

                                sdS |	
                                | _        |

                                                    d          | _        | j        | j        d
<   | j        | j        d<   |                     | j         d          sdS dS )Nr  >   	xai-oauthr  Fr  r   )!resolve_codex_runtime_credentials)refresh_if_expiring)%resolve_xai_oauth_runtime_credentialsz%s singleton read failed: %sr   rZ   z%s singleton tokens differ from the active api_key; skipping singleton force-refresh to avoid silent account swap. Reactive credential rotation should go through the pool.)force_refreshz %s credential refresh failed: %srW   r  _credential_refreshr  T)r   rV   hermes_cli.authr  r  r   r   r   r^   r   r)  r   rs  r  rW   r  r  )r{   rA  r  singleton_nowr  r   singleton_key
active_keycredsr   rW   s              rS   %_try_refresh_codex_client_credentialsz-AIAgent._try_refresh_codex_client_credentialsl  s   =---Fc1c1c5	}..MMMMMM A A(-! ! ! RQQQQQ E E(-! ! !  	 	 	LL7LLL55555	 M--i88>B??EEGG+,,2244
 	Z 	MZ,G,GLLK 	   5	}..MMMMMM99NNNQQQQQQ==ERRR 	 	 	LL;T]CPPP55555	 ))I&&99Z(('3'' 	w}} 	5(C(( 	0@0@ 	5}} ((//44)-I&*.-J'22T]:_:_:_2`` 	5ts/   0A	 	
A?!A::A?0D> >
E4!E//E4)rA  inference_auth_moder  c                   | j         dk    s| j        dk    rdS 	 ddlm}m}m} |p|r|n|} |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|                    d          }	|                    d          }
t          |	t                     r|	                                sdS t          |
t                     r|
                                sdS |	                                | _        |
                                                    d          | _        | j        | j        d<   | j        | j        d<   | j                            dd            |                     d          sdS dS )Nr  r  Fr   )NOUS_INFERENCE_AUTH_MODE_AUTONOUS_INFERENCE_AUTH_MODE_LEGACY resolve_nous_runtime_credentials<   HERMES_NOUS_MIN_KEY_TTL_SECONDS1800HERMES_NOUS_TIMEOUT_SECONDS15)min_key_ttl_secondstimeout_secondsr  z"Nous credential refresh failed: %sr   rW   r  r  nous_credential_refreshr  T)r   rV   r  r  r  r  r,  r   r   r  r  r   r   r   r   rs  r^   r)  r   r  rW   r  ru  r  )r{   rA  r  r  r  r  selected_auth_moder  r   r   rW   s              rS   $_try_refresh_nous_client_credentialsz,AIAgent._try_refresh_nous_client_credentials  s    =...$-62I2I5	          "5 "3//2 
 54$'C	:[]c0d0d,e,e$f$f %bi0Mt&T&T U U$6  EE
  	 	 	LL=sCCC55555	 ))I&&99Z(('3'' 	w}} 	5(C(( 	0@0@ 	5}} ((//44)-I&*.-J' 1488822:S2TT 	5ts   A.B	 	
B9B44B9c                 0   | j         dk    rdS 	 ddlm}  |            \  }}n3# t          $ r&}t                              d|           Y d}~dS d}~ww xY wt          |t                    r|                                sdS |                                }|| _	        | j	        | j
        d<   | j        | j
        d<   |                     t          | j        pd	                     |                     d
          sdS t                              d|           dS )a]  Refresh Copilot credentials and rebuild the shared OpenAI client.

        Copilot tokens may remain the same string across refreshes (`gh auth token`
        returns a stable OAuth token in many setups). We still rebuild the client
        on 401 so retries recover from stale auth/client state without requiring
        a session restart.
        r  Fr   )resolve_copilot_tokenz%Copilot credential refresh failed: %sNr   rW   rZ   copilot_credential_refreshr  z%Copilot credentials refreshed from %sT)rV   r  r!  r   r   r   rs  r^   r)  r   r  rW   "_apply_client_headers_for_base_urlr  r  )r{   r!  	new_tokentoken_sourcer   s        rS   '_try_refresh_copilot_client_credentialsz/AIAgent._try_refresh_copilot_client_credentials  s=    =I%%5	EEEEEE&;&;&=&=#I|| 	 	 	LL@#FFF55555	 )S)) 	1B1B 	5OO%%	 )-I&*.-J'//DM4GR0H0HIII22:V2WW 	5;\JJJts   # 
AAAc                 :   | j         dk    st          | d          sdS | j        dk    rdS t          | dd          pd}d|v rdS 	 dd	lm}m}  |            }n3# t          $ r&}t          	                    d
|           Y d }~dS d }~ww xY wt          |t                    r|                                sdS |                                }|| j        k    rdS 	 | j                                         n# t          $ r Y nw xY w	  ||t          | dd           t!          | j        | j                            | _        n3# t          $ r&}t                              d|           Y d }~dS d }~ww xY w|| _        ddlm} | j        dk    r ||          nd| _        dS )Nr  _anthropic_api_keyF	anthropic_anthropic_base_urlrZ   z	azure.comr   )resolve_anthropic_tokenbuild_anthropic_clientz'Anthropic credential refresh failed: %stimeoutz?Failed to rebuild Anthropic client after credential refresh: %s)_is_oauth_tokenT)r   r  rV   r  agent.anthropic_adapterr+  r,  r   r   r   rs  r^   r)  r(  _anthropic_clientrH  r   r   r   r/  _is_anthropic_oauth)r{   _baser+  r,  r$  r   r/  s          rS   )_try_refresh_anthropic_client_credentialsz1AIAgent._try_refresh_anthropic_client_credentials
  s+   =000FZ8[8[05 =K''5 3R88>B%5	________//11II 	 	 	LLBCHHH55555	 )S)) 	1B1B 	5OO%%	///5	"((**** 	 	 	D		%;%;3T::4T]DJOO& & &D""
  	 	 	NN\^abbb55555	 #,
 	<;;;;;AER]A]A]??9#=#=#=ch tsB   A 
B"BBC2 2
C?>C?:D> >
E.E))E.c                 @   ddl m}m} t          |d          r |            | j        d<   d S t          |d          r ||          | j        d<   d S t          |d          rt                      | j        d<   d S t          |d          rddlm}  |            | j        d<   d S t          |d	          rd
di| j        d<   d S t          |d          rt                      | j        d<   d S t          |d          r4ddl m	}  || j        
                    dd                    | j        d<   d S d }	 ddlm}  || j                  }|r|j        rt          |j                  }n# t           $ r Y nw xY w|r|| j        d<   d S | j                            dd            d S )Nr   )build_nvidia_nim_headersbuild_or_headersr  r  zintegrate.api.nvidia.comzapi.routermint.comr  )copilot_default_headersapi.kimi.comrP   zclaude-code/0.1.0portal.qwen.air  )_codex_cloudflare_headersr   rZ   )get_provider_profile)agent.auxiliary_clientr6  r7  rJ   r  rT   r+  r8  rn   r;  r   	providersr<  rV   r  rt  r   ru  )	r{   rW   r6  r7  r8  r;  _ph_headers_gpf2_ph2s	            rS   r#  z*AIAgent._apply_client_headers_for_base_url=  sf   	
 	
 	
 	
 	
 	
 	
 	

 !?;;  	A5E5E5G5GD 1222"8-GHH 	A5M5Mh5W5WD 1222"8-ABB 	A5H5J5JD 1222"8-DEE 	AAAAAAA5L5L5N5ND 1222"8^<< 	A6BDW5XD 1222"8-=>> 	A5I5K5KD 1222"8];; 	AHHHHHH5N5N#''	2666 6D 1222
 KCCCCCCuT]++ =D0 ="&t';"<"<K    A9D#$5666#''(94@@@@@s   13E% %
E21E2c                    t          |dd           pt          |dd          }t          |dd           pt          |dd           p| j        }| j        dk    rddlm}m} 	 | j                                         n# t          $ r Y nw xY w|| _	        || _
         |||t          | j        | j                  	          | _        | j        d
k    r ||          nd| _        || _        || _        d S || _        t!          |t"                    r|                    d          n|| _        | j        | j        d<   | j        | j        d<   |                     | j                   |                     d           d S )Nruntime_api_keyaccess_tokenrZ   runtime_base_urlrW   r  r   )r,  r/  r-  r)  Fr  r   credential_rotationr  )r  rW   r   r0  r,  r/  r1  rH  r   r(  r*  r   rV   r   r2  r   rs  r^   r  r  r#  r  )r{   r  runtime_keyruntime_baser,  r/  s         rS   _swap_credentialzAIAgent._swap_credentiale  s   e%6==cP^`bAcAcu&8$??t75R\^bCcCctgkgt=000WWWWWWWW&,,....    '2D#'3D$%;%;\4T]DJOO& & &D" HL}XcGcGc{'C'C'CinD$&DL(DMF"4>|S4Q4Qc++C000Wc)-I&*.-J'//>>>++3H+IIIIIs    A: :
BB)classified_reasonrv  has_retried_429rJ  c                .    ddl m}  || ||||          S )uO   Forwarder — see ``agent.agent_runtime_helpers.recover_with_credential_pool``.r   )recover_with_credential_pool)rs   rK  rJ  rv  )r5  rM  )r{   rs   rK  rJ  rv  rM  s         rS   _recover_with_credential_poolz%AIAgent._recover_with_credential_pool  sJ     	MLLLLL++Dk[j  P  `m  n  n  n  	nrU   c                     | j         }|dS | j        dk    s1t          t          | dd                                        d          rdS |                                S )zEWhether a rate-limit retry should wait for same-provider credentials.NFrY   rW   rZ   r[   )_credential_poolrV   r^   r  r_   r]   )r{   rb   s     rS   '_credential_pool_may_recover_rate_limitz/AIAgent._credential_pool_may_recover_rate_limit  sf    $<5M00074R0011<<=NOO 1 5!!###rU   c                 n    | j         dk    r|                                   | j        j        j        di |S )Nr  r  )r   r4  r1  rk  create)r{   r  s     rS   _anthropic_messages_createz"AIAgent._anthropic_messages_create  sA    =000::<<<5t%.5CC
CCCrU   c                 N   t          t          | dd                    }t          | dd          dk    r+ddlm} t          | dd	          pd	} ||          | _        dS dd
lm}  || j        t          | dd          t          | j        | j	                  |          | _        dS )u)  Rebuild the Anthropic client after an interrupt or stale call.

        Handles both direct Anthropic and Bedrock-hosted Anthropic models
        correctly — rebuilding with the Bedrock SDK when provider is bedrock,
        rather than always falling back to build_anthropic_client() which
        requires a direct Anthropic API key.

        Honors ``self._oauth_1m_beta_disabled`` (set by the reactive recovery
        path when an OAuth subscription rejects the 1M-context beta) so the
        rebuilt client carries the reduced beta set.
        _oauth_1m_beta_disabledFrV   Nbedrockr   )build_anthropic_bedrock_client_bedrock_regionz	us-east-1)r,  r*  )r.  drop_context_1m_beta)
r	  r  r0  rX  r1  r,  r(  r   rV   r   )r{   _drop_1mrX  regionr,  s        rS   _rebuild_anthropic_clientz!AIAgent._rebuild_anthropic_client  s     &?GGHH4T**i77NNNNNNT#4kBBQkF%C%CF%K%KD"""FFFFFF%;%;'3T::4T]DJOO%-	& & &D"""rU   c                 &    ddl m}  || |          S )uK   Forwarder — see ``agent.chat_completion_helpers.interruptible_api_call``.r   )interruptible_api_call)r  r_  )r{   r  r_  s      rS   _interruptible_api_callzAIAgent._interruptible_api_call  s'    HHHHHH%%dJ777rU   c                 D   t          | dd          }||                                }|rxt          | dd          }||                    |          }|rNd | j        | j        fD             }|D ]}	  ||           # t
          $ r Y w xY w|                     |           t          | dd          }|d|                                }|rNd | j        | j        fD             }|D ]}	  ||           # t
          $ r Y w xY w|                     |           d| _        dS )zDReset tracking for text delivered during the current model response._stream_think_scrubberN_stream_context_scrubberc                     g | ]}||S r   r  r  cbs     rS   rU  z;AIAgent._reset_stream_delivery_tracking.<locals>.<listcomp>  s     t t tegesesesesrU   c                     g | ]}||S r   r  re  s     rS   rU  z;AIAgent._reset_stream_delivery_tracking.<locals>.<listcomp>  s    pppBacaoRaoaoaorU   rZ   )r  flushfeedr   _stream_callbackr   _record_streamed_assistant_text _current_streamed_assistant_text)r{   think_scrubber
think_tailctx_scrubber	callbacksrf  scrubbertails           rS   _reset_stream_delivery_trackingz'AIAgent._reset_stream_delivery_tracking  s    !'?FF%'--//J E  't-GNN+!-!2!2:!>!>J E t tt/I4K`.a t t tI' ! !!BzNNNN( ! ! ! D!88DDD 4!;TBB>>##D ;pp4+EtG\*]ppp	#  B4$   44T:::02---s$   1A==
B
	B
(C44
D Dc                 n    t          |t                    r|rt          | dd          |z   | _        dS dS dS )zCAccumulate visible assistant text emitted through stream callbacks.rl  rZ   N)rs  r^   r  rl  )r{   rK  s     rS   rk  z'AIAgent._record_streamed_assistant_text  sR    dC   	T 	@"EEL 111	 	 	 	rU   c                     t          | t                    sdS t          j        dd|                                           S )NrZ   z\s+r{  )rs  r^   r  r  r)  )rK  s    rS   _normalize_interim_visible_textz'AIAgent._normalize_interim_visible_text  s9    $$$ 	2vfc4((..000rU   c           	          |                      |                     |pd                    }|sdS |                      |                     t          | dd          pd                    }t          |          o||k    S )NrZ   Frl  )rv  r  r  r	  )r{   r  visible_contentstreameds       rS   _interim_content_was_streamedz%AIAgent._interim_content_was_streamed  s    >>$$W]33
 
  	577$$WT3UWY%Z%Z%`^`aa
 
 H~~=(o"==rU   assistant_msgc                    t          | dd          }|t          |t                    sdS |                    d          }|                     |pd                                          }|r|dk    rdS |                     |          }	  |||           dS # t          $ r  t          	                    dd	           Y dS w xY w)
zESurface a real mid-turn assistant commentary message to the UI layer.r   Nr  rZ   z(empty))already_streamedz interim_assistant_callback errorTr   )
r  rs  rt  r   r  r)  rz  r   r   r   )r{   r{  rf  r  visibler}  s         rS   _emit_interim_assistant_messagez'AIAgent._emit_interim_assistant_message  s    T7>>:Zt<<:F##I..**7=b99??AA 	'Y..F==gFF	LBw)9:::::: 	L 	L 	LLL;dLKKKKKK	Ls   	B &CCc                    t          | dd          r%|r#|                                rd| _        d|z   }d}nd}t          |t                    rt          | dd          }||                    |pd          }n|                     |pd          }t          | dd          }||                    |          }nt          |          }|s&t          | d	d          s|                    d
          }|sdS d | j	        | j
        fD             }d}|D ] }	  ||           d}# t          $ r Y w xY w|r|                     |           dS dS )z;Fire all registered stream delta callbacks (display + TTS)._stream_needs_breakF

Trb  NrZ   rc  rl  rO  c                     g | ]}||S r   r  re  s     rS   rU  z.AIAgent._fire_stream_delta.<locals>.<listcomp>>  s    hhhBY[YgRYgYgYgrU   )r  r)  r  rs  r^   ri  r  r   lstripr   rj  r   rk  )r{   rK  prepended_breakrm  rq  rp  	deliveredrf  s           rS   _fire_stream_deltazAIAgent._fire_stream_delta  s    4.66 	$4 	$DJJLL 	$',D$D=D"OO#OdC   	) %T+CTJJN)%**4:266 //
;; t%?FFH#}}T** (--" )78", , ) {{4(( 	Fhh4#=t?T"Uhhh		 	 	B4 		    	70066666	7 	7s   D!!
D.-D.c                 V    | j         }|	  ||           dS # t          $ r Y dS w xY wdS )z&Fire reasoning callback if registered.N)r   r   )r{   rK  rf  s      rS   _fire_reasoning_deltazAIAgent._fire_reasoning_deltaI  sN    $>4    >    
&&c                 V    | j         }|	  ||           dS # t          $ r Y dS w xY wdS )a  Notify display layer that the model is generating tool call arguments.

        Fires once per tool name when the streaming response begins producing
        tool_call / tool_use tokens.  Gives the TUI a chance to show a spinner
        or status line so the user isn't staring at a frozen screen while a
        large tool payload (e.g. a 45 KB write_file) is being generated.
        N)r   r   )r{   rV  rf  s      rS   _fire_tool_gen_startedzAIAgent._fire_tool_gen_startedR  sP     #>9    >r  c                 :    | j         dupt          | dd          duS )z4Return True if any streaming consumer is registered.Nrj  )r   r  r   s    rS   rE  zAIAgent._has_stream_consumersa  s/     &d2 Ct/66dB	
rU   r  c                *    ddl m}  || ||          S )uU   Forwarder — see ``agent.chat_completion_helpers.interruptible_streaming_api_call``.r   ) interruptible_streaming_api_callr  )r  r  )r{   r  r  r  s       rS   !_interruptible_streaming_api_callz)AIAgent._interruptible_streaming_api_callh  s0     	SRRRRR//jQ_````rU   zFailoverReason | Nonec                 &    ddl m}  || |          S )uJ   Forwarder — see ``agent.chat_completion_helpers.try_activate_fallback``.r   )try_activate_fallback)r  r  )r{   rz  r  s      rS   _try_activate_fallbackzAIAgent._try_activate_fallbacko  s'    GGGGGG$$T6222rU   c                 $    ddl m}  ||           S )uJ   Forwarder — see ``agent.agent_runtime_helpers.restore_primary_runtime``.r   )restore_primary_runtime)r5  r  )r{   r  s     rS   _restore_primary_runtimez AIAgent._restore_primary_runtimev  s%    GGGGGG&&t,,,rU   	api_errorretry_countr  c                ,    ddl m}  || |||          S )uP   Forwarder — see ``agent.agent_runtime_helpers.try_recover_primary_transport``.r   )try_recover_primary_transport)r  r  )r5  r  )r{   r  r  r  r  s        rS   _try_recover_primary_transportz&AIAgent._try_recover_primary_transport{  s2     	NMMMMM,,T9+cnoooorU   c                     t          | t                    sdS | D ]1}t          |t                    r|                    d          dv r dS 2dS )NFrw   >   rM  rN  T)rs  rj  rt  r   )r  r  s     rS   _content_has_image_partsz AIAgent._content_has_image_parts  s^    '4(( 	5 	 	D$%% $((6*:*:>Z*Z*ZtturU   rM  c                    t          | pd                              d          \  }}}d}|                    d          rZ|t          d          d                              dd          d                                         }|                    d          r|}d	d
dddd                    |d          }t          j        d|d          }	 |5  |	                    t          j        |                     d d d            n# 1 swxY w Y   n:# t          $ r- 	 t          j        |j                   n# t           $ r Y nw xY w w xY wt#          |j                  }t          |          |fS )NrZ   ,
image/jpegdata:;r\   r   zimage/z.pngz.gifz.webpz.jpg)z	image/pngz	image/gifz
image/webpr  z	image/jpganthropic_image_F)r  suffixdelete)r^   	partitionr_   r`   r  r)  r   tempfileNamedTemporaryFilerW  base64	b64decoder   r   unlinkr'  r;  r   )	rM  header_rY  mime	mime_partr  tmpr  s	            rS    _materialize_data_url_for_visionz(AIAgent._materialize_data_url_for_vision  s   io2..88==4W%% 	!s7||}}-33C;;A>DDFFI##H-- ! ! 
 
 #dF

 	 )1CF[`aaa
	 2 2		&*4001112 2 2 2 2 2 2 2 2 2 2 2 2 2 2 	 	 		#(####   	 CH~~4yy$sT   D (D8D DD DD 
ED54E5
E?EEErp  c                 D   t          j        t          |pd                              d                                                    }| j                            |          }|r|S ddd                    |d          }d}t          |pd          }d }|                    d          r|                     |          \  }}d}		 d	d
l	m
}
 t          j         |
||                    }t          |t                    rt          j        |          ni }|                    d          pd                                }	n# t"          $ r}d| }	Y d }~nd }~ww xY w|r:|                                r&	 |                                 nS# t(          $ r Y nGw xY wnB# |r:|                                r'	 |                                 w # t(          $ r Y w w xY ww w xY w|	sd}	d| d|	 d}|r-t          |pd                              d          s	|d| dz  }|| j        |<   |S )NrZ   r  rq  ztool result)rq  r  r8  zDescribe everything visible in this image in thorough detail. Include any text, code, UI, data, objects, people, layout, colors, and any other notable visual information.r  r   )vision_analyze_tool)rM  user_promptanalysiszImage analysis failed: zImage analysis failed.z[The z- attached an image. Here's what it contains:
]z@
[If you need a closer look, use vision_analyze with image_url: )hashlibsha256r^   encode	hexdigest_anthropic_image_fallback_cacher   r_   r  tools.vision_toolsr  asynciorunrs  r  r  r)  r   r  r  r;  )r{   rM  rp  	cache_keycached
role_labelanalysis_promptvision_sourcecleanup_pathdescriptionr  result_jsonr  r   notes                  rS   &_describe_image_for_anthropic_fallbackz.AIAgent._describe_image_for_anthropic_fallback  s   N3yB#7#7#>#>w#G#GHHRRTT	599)DD 	M %!
 
 #dF

 	
8 	 IO,,'+##G,, 	_*.*O*OP]*^*^'M<	>>>>>>!+##mYYY K 1;;0L0LTTZ,,,RTF!::j117R>>@@KK 	8 	8 	87A77KKKKKK	8   3 3 5 5  ''))))   D   3 3 5 5  ''))))   D   	32K_z__Q\___ 	Y_"!5!5!@!@!I!I 	dTadddD ;?,Y7sg   A9D? >F ?
E	EF EF 2F 
FFG0GG
GGGGc                    	 ddl m} ddlm}  |            }t	          | dd          pd                                }t	          | dd          pd                                } ||||          du S # t          $ r Y dS w xY w)	a  Return True if the active provider+model reports native vision.

        Used to decide whether to strip image content parts from API-bound
        messages (for non-vision models) or let the provider adapter handle
        them natively (for vision-capable models).

        Resolution order (see ``agent.image_routing._supports_vision_override``):
          1. ``model.supports_vision`` (top-level, single-model shortcut)
          2. ``providers.<provider>.models.<model>.supports_vision``
          3. models.dev capability lookup
        Custom/local models absent from models.dev would otherwise be
        misclassified as non-vision and have their images stripped.
        r   r  )_lookup_supports_visionrV   rZ   r   TF)r  r  agent.image_routingr  r  r)  r   )r{   r  r  r  rV   r   s         rS   _model_supports_visionzAIAgent._model_supports_vision  s    	555555CCCCCC+--Cj"55;BBDDHT7B//52<<>>E**8UC@@DHH 	 	 	55	s   A.A1 1
A?>A?c                    |                      |          s|S g }g }|D ]}t          |t                    r<|                                r'|                    |                                           Tt          |t
                    sj|                    d          }|dv rOt          |                    dd          pd                                          }|r|                    |           |dv r|                    di           }t          |t
                    r|                    dd          nt          |pd          }	|	r*|                    |                     |	|                     n|                    d           kt          |                    dd          pd                                          }|r|                    |           d	                    d
 |D                                                       }
d                    d |D                                                       }|
r	|r|
 d	| S |
r|
S |r|S dS )Nrw   >   rK  
input_textrK  rZ   >   rM  rN  rM  r  z:[An image was attached but no image source was available.]r  c              3      K   | ]}||V  	d S r   r  )r  r  s     rS   r  z8AIAgent._preprocess_anthropic_content.<locals>.<genexpr>  s'      BBdTBTBBBBBBrU   rO  c              3      K   | ]}||V  	d S r   r  )r  rK  s     rS   r  z8AIAgent._preprocess_anthropic_content.<locals>.<genexpr>  s'      ??D$?4??????rU   zI[A multimodal message was converted to text for Anthropic compatibility.])	r  rs  r^   r)  r^  rt  r   r  r\  )r{   r  rp  
text_partsimage_notesr  r  rK  
image_datarM  r  r  s               rS   _preprocess_anthropic_contentz%AIAgent._preprocess_anthropic_content  sy   ,,W55 	N "
!# 	( 	(D$$$ ::<< 4%%djjll333dD)) HHV$$E...488FB//5266<<>> ,%%d+++444!XXk266
9CJPT9U9UpJNN5"555[^_i_omo[p[p	 e&&t'R'RS\^b'c'cdddd&&'cdddtxx++1r2288::D (!!$'''BBkBBBBBHHJJ??J?????EEGG 	+f 	+**&*** 	M 	MZZrU   c                     |p| j         }t          | dd          }|	i }|| _        |                    |          }|ddlm}  ||          }|||<   |S )zReturn the cached transport for the given (or current) api_mode.

        Lazy-initializes on first call per api_mode. Returns None if no
        transport is registered for the mode.
        _transport_cacheNr   )get_transport)r   r  r  r   agent.transportsr  )r{   r   modecacher,  r  s         rS   _get_transportzAIAgent._get_transport   sw     (4=0$77=E$)D!IIdOO9666666d##AE$KrU   api_messagesc           
      j    t           fd|D                       s|S                                  r|S t          j        |          }|D ]g}t	          |t
                    s                     |                    d          t          |                    dd          pd                    |d<   h|S )Nc              3      K   | ]A}t          |t                    o'                    |                    d                     V  BdS r  Nrs  rt  r  r   r  rm  r{   s     rS   r  z>AIAgent._prepare_anthropic_messages_for_api.<locals>.<genexpr>4  `       
 
 sD!!Wd&C&CCGGIDVDV&W&W
 
 
 
 
 
rU   r  rp  r8  	r  r  re  deepcopyrs  rt  r  r   r^   r{   r  transformedrm  s   `   rS   #_prepare_anthropic_messages_for_apiz+AIAgent._prepare_anthropic_messages_for_api2  s     
 
 
 
#
 
 
 
 
 	    &&(( 	  mL11 	 	Cc4(( !??	""CGGFF++5v66 C	NN rU   c           
      j    t           fd|D                       s|S                                  r|S t          j        |          }|D ]g}t	          |t
                    s                     |                    d          t          |                    dd          pd                    |d<   h|S )a  Strip native image parts when the active model lacks vision.

        Runs on the chat.completions / codex_responses paths. Vision-capable
        models pass through unchanged (provider and any downstream translator
        handle the image parts natively). Non-vision models get each image
        replaced by a cached vision_analyze text description so the turn
        doesn't fail with "model does not support image input".
        c              3      K   | ]A}t          |t                    o'                    |                    d                     V  BdS r  r  r  s     rS   r  zAAIAgent._prepare_messages_for_non_vision_model.<locals>.<genexpr>W  r  rU   r  rp  r8  r  r  s   `   rS   &_prepare_messages_for_non_vision_modelz.AIAgent._prepare_messages_for_non_vision_modelN  s      
 
 
 
#
 
 
 
 
 	   &&(( 	 mL11 
	 
	Cc4(( 
 "??	""CGGFF++5v66 C	NN rU   c                    t          |          s|S |                    d          pg }|                     |          s|S |                                 rt	          | dd          pd                                                                t	          | dd          pd                                f}t	          | dd          }|r<||v r8t                              d||d         |d	                    t          |          S |S t          |          }|d
k    rt          j        d|d          S t                              d|| j        | j                   |S )a  Return the tool message content that is safe for the active model.

        Multimodal tool results normally unwrap to OpenAI-style content parts so
        vision-capable models can inspect screenshots.  Text-only providers must
        not receive those image parts, because a rejected tool result becomes
        part of the canonical history and can make the next user turn fail before
        the agent has a chance to recover.
        r  rV   rZ   r   _no_list_tool_content_modelsNua   Tool %s: model %s/%s known to reject list-type tool content this session — sending text summaryr   r\   computer_usezcomputer_use returned screenshot/image content, but the active model/provider does not support image input. Switch to a vision-capable model for desktop computer use, or use browser tools for browser tasks.)rv   text_summaryzWTool %s returned image content for non-vision model %s/%s; falling back to text summary)rC   r   r  r  r  r)  rj   r   r   rD   r  dumpsr   rV   r   )r{   rV  r  r  r  no_listr  s          rS   %_tool_result_content_for_active_modelz-AIAgent._tool_result_content_for_active_modeln  s    *&11 	M**Y''-2,,W55 	N&&(( 	 z2..4";;==CCEEw++1r88::C d$BDIIG 83'>>Ds1vs1v  
 0777N*622&&:/
 !(     	+MJ	
 	
 	
 rU   c                 $    ddl m}  ||          S )uX   Forwarder — see ``agent.conversation_compression.try_shrink_image_parts_in_messages``.r   )"try_shrink_image_parts_in_messages)r  r  )r{   r  r  s      rS   #_try_shrink_image_parts_in_messagesz+AIAgent._try_shrink_image_parts_in_messages  s%    UUUUUU11,???rU   c                 P   t          |t                    sdS t          | dd          pd                                                                t          | dd          pd                                f}t          | d          st                      | _        |d         r| j                            |           d}|D ]g}t          |t                    r|
                    d          dk    r2|
                    d	          }t          |t                    s]g }d}|D ]}t          |t                    sQt          |t                    r;|                                r'|                    |                                           h|
                    d
          }	|	dk    s|	dk    rd}|	dv rMt          |
                    d          pd                                          }
|
r|                    |
           |sE|rd                    |          |d	<   nd|d	<   d}i|S )u  Downgrade list-type tool messages to text summaries in-place.

        Recovery path for providers that reject list-type tool message content
        (e.g. Xiaomi MiMo's 400 "text is not set"; see issue #27344).  Walks
        ``api_messages`` for any ``role: "tool"`` message whose ``content`` is
        a list containing image parts, replaces the content with the existing
        text part(s) (or a minimal placeholder if none survive), and records
        the active (provider, model) in ``self._no_list_tool_content_models``
        so subsequent ``_tool_result_content_for_active_model`` calls in this
        session preemptively downgrade screenshots without a round-trip.

        Returns True when at least one tool message was downgraded — the
        caller (the 400 recovery branch in ``agent.conversation_loop``) uses
        this to decide whether to retry the API call with the modified
        history or surface the original error.
        FrV   rZ   r   r  r\   rp  r  r  rw   rM  rN  T>   rK  r  rK  r  uS   [image content removed — provider does not accept list-type tool message content])rs  rj  r  r)  rj   r  r  r  r  rt  r   r^   r^  r\  )r{   r  r  changedrm  r  r  	had_imager  r  rK  s              rS   )_try_strip_image_parts_from_tool_messagesz1AIAgent._try_strip_image_parts_from_tool_messages  sM   " ,-- 	5 T:r**0b7799??AAT7B''-24466
 t;<< 	603D-q6 	7-11#666 '	 '	Cc4(( CGGFOOv,E,Eggi((Ggt,,  %'JI 0 0!$-- !$,, 8 8"))$**,,777((K''5M+A+A $I222txx//5266<<>>D 0"))$///   !'Z!8!8I6 I GGrU   c                     t          | dd          pd                                dv rdS t          | dd          pd                                }d|v pd|v pd|v pd	|v pd
|v pd|v pd|v S )a  True when using an anthropic-compatible endpoint that preserves dots in model names.
        Alibaba/DashScope keeps dots (e.g. qwen3.5-plus).
        MiniMax keeps dots (e.g. MiniMax-M2.7).
        Xiaomi MiMo keeps dots (e.g. mimo-v2.5, mimo-v2.5-pro).
        OpenCode Go/Zen keeps dots for non-Claude models (e.g. minimax-m2.5-free).
        ZAI/Zhipu keeps dots (e.g. glm-4.7, glm-5.1).
        AWS Bedrock uses dotted inference-profile IDs
        (e.g. ``global.anthropic.claude-opus-4-7``,
        ``us.anthropic.claude-sonnet-4-5-20250929-v1:0``) and rejects
        the hyphenated form with
        ``HTTP 400 The provided model identifier is invalid``.
        Regression for #11976; mirrors the opencode-go fix for #5211
        (commit f77be22c), which extended this same allowlist.rV   rZ   >   
minimax-cnopencode-goopencode-zenr  xiaomialibabarW  minimaxTrW   	dashscopealiyuncsr   zopencode.ai/zen/zbigmodel.cnxiaomimimo.comzbedrock-runtime.)r  rj   )r{   bases     rS   _anthropic_preserve_dotsz AIAgent._anthropic_preserve_dots  s     D*b))/R6688 =
 
 
 4j"--3::<<4 *T!*D * "T)* $	*
  4'* "T)
	
rU   c                 ,    t          | j        d          S )z2Return True when the base URL targets Qwen Portal.r:  r  r   s    rS   _is_qwen_portalzAIAgent._is_qwen_portal  s    $T%9;KLLLrU   c                    t          j        |          }|s|S |D ]}t          |t                    s|                    d          }t          |t
                    r
d|dg|d<   Lt          |t                    rfg }|D ]Z}t          |t
                    r|                    d|d           0t          |t                    r|                    |           [|r||d<   |D ]}t          |t                    ro|                    d          dk    rV|                    d          }t          |t                    r*|r(t          |d         t                    rddi|d         d	<    n|S )
Nr  rK  rw   rK  rp  ri   r  rw   	ephemeralcache_control)re  r  rs  rt  r   r^   rj  r^  )r{   r  preparedrm  r  normalized_partsr  s          rS   _qwen_prepare_chat_messagesz#AIAgent._qwen_prepare_chat_messages  s   =.. 	O 	6 	6Cc4(( ggi((G'3'' 6+17"C"C!DIGT** 
6 $& # 6 6D!$,, 6(//0N0NOOOO#D$// 6(//555# 6%5C	N  	 	C#t$$ H)D)D''),,gt,, I IZPRUY=Z=Z I4:K3HGBK0rU   c                    |sdS |D ]}t          |t                    s|                    d          }t          |t                    r
d|dg|d<   Lt          |t                    rfg }|D ]Z}t          |t                    r|                    d|d           0t          |t                    r|                    |           [|r||d<   |D ]}t          |t                    rp|                    d          dk    rW|                    d          }t          |t                    r*|r(t          |d         t                    rdd	i|d         d
<    dS dS )u<   In-place variant — mutates an already-copied message list.Nr  rK  r	  rp  ri   r  rw   r
  r  )rs  rt  r   r^   rj  r^  )r{   rk  rm  r  r  r  s         rS   #_qwen_prepare_chat_messages_inplacez+AIAgent._qwen_prepare_chat_messages_inplace?  s    	F 	6 	6Cc4(( ggi((G'3'' 
6+17"C"C!DIGT** 6#% # 6 6D!$,, 6(//0N0NOOOO#D$// 6(//555# 6%5C	N 	 	C#t$$ H)D)D''),,gt,, I IZPRUY=Z=Z I4:K3HGBK0	 	rU   c                 &    ddl m}  || |          S )uE   Forwarder — see ``agent.chat_completion_helpers.build_api_kwargs``.r   )build_api_kwargs)r  r  )r{   r  r  s      rS   _build_api_kwargszAIAgent._build_api_kwargs[  s'    BBBBBBl333rU   c                 H   t          | j        d          rdS t          | j        d          st          | j        d          r5	 ddlm} t	           || j                            S # t          $ r Y dS w xY w| j        pd                                	                                d	k    r-| 
                                }t          d
 |D                       S d| j        vrdS d| j        v rdS | j        pd	                                d}t          fd|D                       S )a3  Return True when reasoning extra_body is safe to send for this route/model.

        OpenRouter forwards unknown extra_body fields to upstream providers.
        Some providers/routes reject `reasoning` with 400s, so gate it to
        known reasoning-capable model families and direct Nous Portal.
        znousresearch.comTzmodels.github.air  r   github_model_reasoning_effortsFrZ   r%  c              3   &   K   | ]}|o|d k    V  dS )r  Nr  )r  opts     rS   r  z9AIAgent._supports_reasoning_extra_body.<locals>.<genexpr>v  s+      <<s+se|<<<<<<rU   
openrouterzapi.mistral.ai)	z	deepseek/z
anthropic/zopenai/zx-ai/zgoogle/gemini-2zgoogle/gemma-4z
qwen/qwen3ztencent/hy3-previewzxiaomi/c              3   B   K   | ]}                     |          V  d S r   )r_   )r  r  r   s     rS   r  z9AIAgent._supports_reasoning_extra_body.<locals>.<genexpr>  s1      SS5##F++SSSSSSrU   )rJ   r   r+  r  r	  r   r   rV   r)  rj   "_lmstudio_reasoning_options_cachedr  )r{   r  optsreasoning_model_prefixesr   s       @rS   _supports_reasoning_extra_bodyz&AIAgent._supports_reasoning_extra_body`  se    !!57IJJ 	4!$"68JKK		$T%9;RSS		LLLLLL::4:FFGGG   uuMR&&((..00J>>::<<D<<t<<<<<<t3335t3335!r((**
$
  SSSS:RSSSSSSs   "A' '
A54A5c           	         ddl }t          | dd          }|	i x}| _        | j        | j        f}|                    |          }|$|\  }}|s|                                |z
  dk     r|S 	 ddlm}  || j        | j        t          | dd                    }n# t          $ r g }Y nw xY w||                                f||<   |S )a  Probe LM Studio's published reasoning ``allowed_options`` once per
        (model, base_url). The list (e.g. ``["off","on"]`` or
        ``["off","minimal","low"]``) is needed both for the supports-reasoning
        gate and for clamping the emitted ``reasoning_effort`` so toggle-style
        models don't 400 on ``high``. Cache is keyed on (model, base_url) so
        ``/model`` swaps and base-URL changes don't reuse a stale list.
        Non-empty results are cached permanently (model capabilities don't
        change). Empty results (transient probe failure OR genuinely
        non-reasoning model) are cached with a 60-second TTL to avoid an
        HTTP round-trip on every turn while still retrying reasonably soon.
        r   N_lm_reasoning_opts_cacher  ) lmstudio_model_reasoning_optionsr   rZ   )
r  r  r   r   rW   r   	monotonicr+  r!  r   )r{   _timer  r  r  r  tsr!  s           rS   r  z*AIAgent._lmstudio_reasoning_options_cached  s    	8$??=466ED1z4=)3HD" ))B."44	JJJJJJ33
DM74B+G+G DD  	 	 	DDD	EOO--.c
s   +,B B'&B'c                 T    ddl m}  || j        |                                           S )a  Resolve a safe top-level ``reasoning_effort`` for LM Studio.

        The iteration-limit summary path calls ``chat.completions.create()``
        directly, bypassing the transport. Share the helper so the two paths
        can't drift on effort resolution and clamping.
        r   )resolve_lmstudio_effort)agent.lmstudio_reasoningr&  r   r  )r{   r&  s     rS   *_resolve_lmstudio_summary_reasoning_effortz2AIAgent._resolve_lmstudio_summary_reasoning_effort  sA     	EDDDDD&&!3355
 
 	
rU   c                    	 ddl m} n# t          $ r Y dS w xY w || j                  }|sdS | j        rt          | j        t                    rk| j                            d          du rdS t          | j                            dd                    	                                
                                }nd}|dk    rd	|v rd	}n ||vr|d
k    rd|v rd}nd|v rd}n|d         }d|iS )zDFormat reasoning payload for GitHub Models/OpenAI-compatible routes.r   r  NenabledFeffortmediumxhighhighminimallow)r+  r  r   r   r   rs  rt  r   r^   r)  rj   )r{   r  supported_effortsrequested_efforts       rS   #_github_models_reasoning_extra_bodyz+AIAgent._github_models_reasoning_extra_body  sZ   	HHHHHHH 	 	 	44	 ;:4:FF  	4  	(Z0Et%L%L 	($((33u<<t"%))(H==   eggeegg   (w&&65F+F+F%%6669,,:K1K1K#(  ...#+  #4Q#7 *++s   	 
c                 (    ddl m}  || ||          S )uL   Forwarder — see ``agent.chat_completion_helpers.build_assistant_message``.r   )build_assistant_message)r  r5  )r{   r  r  r5  s       rS   _build_assistant_messagez AIAgent._build_assistant_message  s*    IIIIII&&t->NNNrU   c                 $   | j         | j        t          | d| j                  f}t          | dd          }||d         |k    r|d         S |                                 p'|                                 p|                                 }||f| _        |S )u  Return True when the active provider enforces reasoning_content echo-back.

        DeepSeek v4 thinking and Kimi / Moonshot thinking both reject replays
        of assistant tool-call messages that omit ``reasoning_content`` (refs
        #15250, #17400). Xiaomi MiMo thinking mode has the same requirement.

        Result cached on the AIAgent instance keyed by (provider, model,
        base_url); invalidated whenever ``switch_model()`` /
        ``_try_activate_fallback()`` mutate any of those. This is hot — the
        agent loop hits ~16 invocations per turn, each of which would
        otherwise re-run ~5 ``base_url_host_matches`` (and therefore
        ``urlparse``) calls under it. Caching drops the per-turn cost from
        ~5us × 16 = ~80us to <1us.
        r   _thinking_pad_cacheNr   r\   )rV   r   r  rW   _needs_deepseek_tool_reasoning_needs_kimi_tool_reasoning_needs_mimo_tool_reasoningr8  )r{   r  r  r  s       rS   _needs_thinking_reasoning_padz%AIAgent._needs_thinking_reasoning_pad  s     }dj'$8I4=*Y*YZ4d;;&)s"2"2!9//11 1..001..00 	
 %(= rU   c                     | j         dv p>t          | j        d          p)t          | j        d          pt          | j        d          S )a  Return True when the current provider is Kimi / Moonshot thinking mode.

        Kimi ``/coding`` and Moonshot thinking mode both require
        ``reasoning_content`` on every assistant tool-call message; omitting
        it causes the next replay to fail with HTTP 400.

        Detection is host-driven, not model-name-driven: aggregators like
        OpenRouter that re-export Kimi/Moonshot models speak their own
        protocol and reject ``reasoning_content`` echoes. We only enable the
        kimi-reasoning replay when the request actually targets a
        kimi/moonshot endpoint or the dedicated kimi-coding provider.
        >   kimi-codingkimi-coding-cnr9  zmoonshot.aizmoonshot.cn)rV   rJ   rW   r   s    rS   r:  z"AIAgent._needs_kimi_tool_reasoning  sU     M>> C$T]NCCC$T]MBBC %T]MBB		
rU   c                     | j         pd                                }| j        pd                                }|dk    pd|v pt          | j        d          S )a  Return True when the current provider is DeepSeek thinking mode.

        DeepSeek V4 thinking mode requires ``reasoning_content`` on every
        assistant tool-call turn; omitting it causes HTTP 400 when the
        message is replayed in a subsequent API request (#15250).
        rZ   deepseekzapi.deepseek.comrV   rj   r   rJ   rW   r{   rV   r   s      rS   r9  z&AIAgent._needs_deepseek_tool_reasoning  se     M'R..00!r((**
" HU"H$T]4FGG	
rU   c                     | j         pd                                }| j        pd                                }|dk    p-d|v p)t          | j        d          pt          | j        d          S )aM  Return True when the current provider is Xiaomi MiMo thinking mode.

        MiMo thinking mode requires ``reasoning_content`` on every assistant
        tool-call message when replaying history; omitting it causes HTTP 400.
        Refs: https://platform.xiaomimimo.com/docs/zh-CN/usage-guide/passing-back-reasoning_content
        rZ   r  mimozapi.xiaomimimo.comr  rB  rC  s      rS   r;  z"AIAgent._needs_mimo_tool_reasoning  s|     M'R..00!r((**  FF$T]4HIIF %T]4DEE		
rU   
source_msgapi_msgc                 (    ddl m}  || ||          S )uQ   Forwarder — see ``agent.agent_runtime_helpers.copy_reasoning_content_for_api``.r   )copy_reasoning_content_for_api)r5  rI  )r{   rF  rG  rI  s       rS   _copy_reasoning_content_for_apiz'AIAgent._copy_reasoning_content_for_api,  s)    NNNNNN--dJHHHrU   c                 &    ddl m}  || |          S )uV   Forwarder — see ``agent.agent_runtime_helpers.reapply_reasoning_echo_for_provider``.r   )#reapply_reasoning_echo_for_provider)r5  rL  )r{   r  rL  s      rS   $_reapply_reasoning_echo_for_providerz,AIAgent._reapply_reasoning_echo_for_provider1  s'    SSSSSS224FFFrU   c                     |                      d          }t          |t                    s| S ddhfd|D             | d<   | S )u  Strip Codex Responses API fields from tool_calls for strict providers.

        Providers like Mistral, Fireworks, and other strict OpenAI-compatible APIs
        validate the Chat Completions schema and reject unknown fields (call_id,
        response_item_id) with 400 or 422 errors. These fields are preserved in
        the internal message history — this method only modifies the outgoing
        API copy.

        Creates new tool_call dicts rather than mutating in-place, so the
        original messages list retains call_id/response_item_id for Codex
        Responses API compatibility (e.g. if the session falls back to a
        Codex provider later).

        Fields stripped: call_id, response_item_id
        r  rf  r  c                 ~    g | ]9}t          |t                    r fd |                                D             n|:S )c                 $    i | ]\  }}|v	||S r  r  )r  r  r  _STRIP_KEYSs      rS   r  zJAIAgent._sanitize_tool_calls_for_strict_api.<locals>.<listcomp>.<dictcomp>L  s)    AAAdaA[,@,@Q,@,@,@rU   )rs  rt  r  )r  rT  rQ  s     rS   rU  z?AIAgent._sanitize_tool_calls_for_strict_api.<locals>.<listcomp>K  s_     !
 !
 !
  "d##,AAAAbhhjjAAAA)+!
 !
 !
rU   )r   rs  rj  )rG  r  rQ  s     @rS   #_sanitize_tool_calls_for_strict_apiz+AIAgent._sanitize_tool_calls_for_strict_api6  sn    " [[..
*d++ 	N "45!
 !
 !
 !
 !!
 !
 !

 rU   r   r   c                *    ddl m}  || ||          S )uO   Forwarder — see ``agent.agent_runtime_helpers.sanitize_tool_call_arguments``.r   )sanitize_tool_call_argumentsrS  )r5  rU  )rk  r   r   rU  s       rS   _sanitize_tool_call_argumentsz%AIAgent._sanitize_tool_call_argumentsR  s0     	MLLLLL++HVPZ[[[[rU   c                     | j         dk    S )a  Determine if tool_calls need sanitization for strict APIs.

        Codex Responses API uses fields like call_id and response_item_id
        that are not part of the standard Chat Completions schema. These
        fields must be stripped when calling any other API to avoid
        validation errors (400 Bad Request).

        Returns:
            bool: True if sanitization is needed (non-Codex API), False otherwise.
        r  )r   r   s    rS   _should_sanitize_tool_callsz#AIAgent._should_sanitize_tool_calls]  s     } 111rU   r  approx_tokensr  focus_topicrA  rZ  r[  c          	      2    ddl m}  || ||||||          S )u:  Forwarder — see ``agent.conversation_compression.compress_context``.

        ``force=True`` is passed by the manual ``/compress`` slash command
        so users can bypass the summary-failure cooldown after an
        auto-compress abort.  Auto-compress callers use the default
        ``force=False``.
        r   )compress_contextrY  )r  r]  )r{   rk  r]  rZ  r  r[  rA  r]  s           rS   _compress_contextzAIAgent._compress_contextj  sB     	DCCCCC(N'k
 
 
 	
rU   decisionc                 8    |j         r| j        || _        dS dS dS )z?Record the first guardrail decision that should stop this turn.N)should_halt_tool_guardrail_halt_decisionr{   r_  s     rS   _set_tool_guardrail_haltz AIAgent._set_tool_guardrail_halty  s3     	:D$F$N19D...	: 	:$N$NrU   c                 @    |j         pd}d| d|j         d|j         dS )Nza toolzI stopped retrying z) because it hit the tool-call guardrail (z) after z repeated non-progressing attempts. The last tool result explains the blocker; the next step is to change strategy instead of repeating the same call.)rV  rq   count)r{   r_  r  s      rS   #_toolguard_controlled_halt_responsez+AIAgent._toolguard_controlled_halt_response~  sP    !-XE$ E EE E'/~E E E	
rU   function_argsfunction_resultc                    | j                             ||||          }|j        dv rt          ||          }|j        r|                     |           |S )N)r  >   haltrX  )_tool_guardrails
after_callactionr9   ra  rd  )r{   rV  rh  ri  r  r_  s         rS   _append_guardrail_observationz%AIAgent._append_guardrail_observation  sq     (33	 4 
 
 ?...7RRO 	4))(333rU   c                 J    |                      |           t          |          S r   )rd  r:   rc  s     rS   _guardrail_block_resultzAIAgent._guardrail_block_result  s$    %%h///)(333rU   effective_task_idr)  c                     |j         }d| _        	 t          |          s|                     ||||          d| _        S |                     ||||          d| _        S # d| _        w xY w)a=  Execute tool calls from the assistant message and append results to messages.

        Dispatches to concurrent execution only for batches that look
        independent: read-only tools may always share the parallel path, while
        file reads/writes may do so only when their target paths do not overlap.
        TF)r  rF  r?   _execute_tool_calls_sequential_execute_tool_calls_concurrent)r{   r  rk  rr  r)  r  s         rS   _execute_tool_callszAIAgent._execute_tool_calls  s     '1
 !%
	*1*== ::%x1BN  %*D!!	 66!8->  %*D!!ED!))))s   &A A 	A&c                 f   ddl m}  ||                    d          |                    d          |                    d          |                    d          |                    d          |                    d          |                    d	          |                    d
          | 	  	        S )zSingle call site for delegate_task dispatch.

        New DELEGATE_TASK_SCHEMA fields only need to be added here to reach all
        invocation paths (concurrent, sequential, inline).
        r   )r}  goalcontexttoolsetstasksr   r   r   rp  )	rx  ry  rz  r{  r   r   r   rp  parent_agent)r~  r}  r   )r{   rh  _delegate_tasks      rS   _dispatch_delegate_taskzAIAgent._dispatch_delegate_task  s     	HGGGGG~""6**!%%i00"&&z22##G,,(,,-=>>%))-88"&&z22""6**

 

 

 
	
rU   function_namepre_tool_block_checkedc           	      0    ddl m}  || ||||||          S )u>   Forwarder — see ``agent.agent_runtime_helpers.invoke_tool``.r   )invoke_tool)r5  r  )r{   r  rh  rr  r1  rk  r  r  s           rS   _invoke_toolzAIAgent._invoke_tool  sC     	<;;;;;{4?PR^`h  kA  B  B  	BrU        labelr  c                    ddl }ddl}|                    d          j        }t	          d|t          |          z
            }g }|                    d          D ]\}t          |          |k    r|                    |           +|                    ||dd          }	|	                    |	p|g           ]d|z   
                    |          }
| |  |
 S )	aF  Word-wrap verbose tool output to fit the terminal width.

        Splits *text* on existing newlines and wraps each line individually,
        preserving intentional line breaks (e.g. pretty-printed JSON).
        Returns a ready-to-print string with *label* on the first line and
        continuation lines indented.
        r   N)x      r  rO  TF)widthbreak_long_wordsbreak_on_hyphens)shutiltextwrapget_terminal_sizecolumnsr,  r`   r  r^  wrapr  r\  )r  rK  r  _shutil_twcols
wrap_width	out_linesraw_linewrappedrz   s              rS   _wrap_verbosezAIAgent._wrap_verbose  s    	!   ((33;TCKK/00
!	

4(( 	8 	8H8}}
**  ****((8:4849 # ; ;   !6XJ7777v##I..'%''''rU   c                 ,    ddl m}  || ||||          S )uH   Forwarder — see ``agent.tool_executor.execute_tool_calls_concurrent``.r   )execute_tool_calls_concurrent)agent.tool_executorr  )r{   r  rk  rr  r)  r  s         rS   ru  z&AIAgent._execute_tool_calls_concurrent  0    EEEEEE,,T3DhPacqrrrrU   c                 ,    ddl m}  || ||||          S )uH   Forwarder — see ``agent.tool_executor.execute_tool_calls_sequential``.r   )execute_tool_calls_sequential)r  r  )r{   r  rk  rr  r)  r  s         rS   rt  z&AIAgent._execute_tool_calls_sequential  r  rU   c                 (    ddl m}  || ||          S )uJ   Forwarder — see ``agent.chat_completion_helpers.handle_max_iterations``.r   )handle_max_iterations)r  r  )r{   rk  r)  r  s       rS   _handle_max_iterationszAIAgent._handle_max_iterations  s)    GGGGGG$$T8^DDDrU   stream_callbackpersist_user_messagec           	      0    ddl m}  || ||||||          S )u?   Forwarder — see ``agent.conversation_loop.run_conversation``.r   run_conversation)agent.conversation_loopr  )r{   r  r]  r<  r  r  r  r  s           rS   r  zAIAgent.run_conversation  sD     	=<<<<<lNDXZacr  uI  J  J  	JrU   c                 @    |                      ||          }|d         S )a  
        Simple chat interface that returns just the final response.

        Args:
            message (str): User message
            stream_callback: Optional callback invoked with each text delta during streaming.

        Returns:
            str: Final assistant response
        )r  r;  r  )r{   rt   r  r  s       rS   chatzAIAgent.chat  s(     &&w&PP&''rU   )should_review_memoryr  c                0    ddl m}  || |||||          S )uD   Forwarder — see ``agent.codex_runtime.run_codex_app_server_turn``.r   )run_codex_app_server_turn)r  r:  rk  rr  r  )r  r  )r{   r  r:  rk  rr  r  r  s          rS   _run_codex_app_server_turnz"AIAgent._run_codex_app_server_turn  sQ     	BAAAAA((L`u  AI  ]n  EY  Z  Z  Z  	ZrU   )ANNNNNNNNrZ   r   r   NNFFFNr   rZ   NNNNFNNNNNNNNNNNNNNNNNNNNNNNNNNNNFFFNNNNNFr   r   r   F)rM   N)NNFr   )rZ   rZ   rZ   )FF)r   )rZ   )NN)r  )NNNNN)r}   r~   r   r   &_TOOL_CALL_ARGUMENTS_CORRUPTION_MARKERpropertyr^   rW   setterrj  r   r  r   r	  r   r  r   r   ry   r   r   r  r"  r2  r4  r?  rG  rN  rP  rU  rY  ra  re  rh  rn  rz  r  r{  _STREAM_DIAG_HEADERSstaticmethodr  r  BaseExceptionr  r  r  r  r  r  r  r  r  r  r  r  tupler  r  r  r  r  r  r  rt  r  r  r  r  r  r
  r  r  r  r  r  r  r  r  r-  r4  r;  rB  r>  rI  rA  rh  rl  rq  ru  r~  r   r  r  r  r  r  r   r  r  r  r@  r  r  r  r  r  r   r	  r  r  r  r  r#  r2  r7  r9  rA  rG  rH  rZ  r\  ra  rd  ri  rk  	frozenset_VALID_API_ROLESro  rv  ry  r  r  r  r  r5   r6   r4   r  r  r(  r  r  r  r  r  r  rI  r  r  r  r  r  r  r  r  r   r  r  r  r&  r4  r#  rI  r   rN  rQ  rT  r]  r`  rs  rk  rv  rz  r  r  r  r  rE  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r(  r3  r6  r<  r:  r9  r;  rJ  rM  rR  rV  rX  r^  r8   rd  rg  ro  rq  rv  r~  r  r  ru  rt  r  r  r  r  r  rU   rS   r   r   &  s$(        	O +
 #    X _;c ;d ; ; ; _; %)!% &*'+"' % '+ #'+'+%)!,1(,7;+/(,+/&*'+%)"&*./3&*$(+/ ,015#'#(#(!!%.2)-$)(*,/+- %EI
 I
I
 I
 	I

 I
 I
 s)d"I
 I
 3i$I
 I
 I
 I
 s)I
  9I
  I
  !I
" #I
$ "%%I
& 'I
( )I
*  9+I
,  9-I
. c/I
0 1I
2 &*3I
4 #&5I
6 &.e_7I
8 9I
: !);I
< &=I
> !)?I
@ $AI
B %CI
D #EI
F  GI
H  (II
J %-KI
L $MI
N "OI
P QI
R sCx.SI
T UI
V  S>WI
X tCH~.YI
Z [I
\ ]I
^ _I
` aI
b cI
d eI
f gI
h iI
j !kI
l !mI
n !oI
p qI
t uI
v ,wI
x S#XyI
| "}I
~ #&I
@ '*AI
B &)CI
D EI
 I
 I
 I
V  &   2 )-(,,0#(!Ci Ci Ci !Ci !	Ci
 $D>Ci !Ci Ci 
Ci Ci Ci CiN -1(,#(	2
 2
#D>2
 !2
 !	2
 2
 2
 2
h? ?Xc] ?^b ? ? ? ?BX X X X
  $ ,1 * * *D * * * *6T    &
$ 
 
 
 
UC UD U U U U(VS VT V V V V8c d    (	c 	d 	 	 	 	      8 48H H4S#X/0H 
c3hH H H HF NMMMMM"tCH~ " " " \"
@cN@36@	@ @ @ @ . .# . . . \.
3] 3t 3 3 3 34 *.
 
 
 
 	

 
 
 
 tCH~&
 

 
 
 
0 *.
 
 
 
 	

 
 
 tCH~&
 

 
 
 
 	EC 	Em 	E 	E 	E 	E 	E
tCH~ 
 
 
 
2 2 2 2
) ) ) )
, ,c ,T , , , ,) )S )D ) ) ) ) 3 3s 3d 3 3 3 3>E > > > >(uUD[7I    8S U    *2
 2
Xc] 2
hsm 2
 2
 2
 2
hLD L L L L #'"&"&#
y 
y 
y 3-
y 3-	
y
 3-
y }
y 
tTz	
y 
y 
y 
y %S %T % % % \%  #'< < << 3-< 
	< < < \<*%s %t % % % %%c %d % % % %.13 13 1 1 1 1
 c d    \&H H H H H $(	C CC 4.	C
 
C C C C>bb b tCH~&	b
 
b b b b:x} : : : :
5s 5t 5 5 5 5          TdTT
T 
cT T T \T $#	 :  	
 
   6 '++/!%&*
 
 
 sm
 $C=	

 #
 sm
 
c3h
 
 
 
$*T$Z *D * * * *$	K 	Kd 	K4PT: 	K 	K 	K 	K3$t* 3QU 3 3 3 3j7d 7 7 7 7 7
9F 9Fd4j 9FX\]aXb 9F 9F 9F 9Fv-4: -$t* - - - ->5# 5 5 5 5
Sd4S>6J SX[ Shl Squvz{~  AD  |D  wE  rF S S S S
Dd38n)= D3 D[_ D D D D AS#X/Ac]A 
A A A \AF +&I +&# +& +& +& \+&Z	)# 	)(3- 	) 	) 	) 	)c c    4 0) 0S#X 0 0 0 \0
C HTRUWZRZ^D\    * &*	T 	T 	TcN	T 		T
 	"	T 
$	T 	T 	T 	T      \   \:SD SD$tCH~*> SD SD SD SDlBV BV BV BV BV BV BVH+ + + +B"# "$ " " " "Hhsm     %&%& 38n%& 	%&
 %& 
%& %& %& %&N    6  T#tCH~:M5N  SV       \ >RT RRU RZ^ R R R R
C D    ,# $    && & &C D    ,
d 
 
 
 
$      6 d d    > !%4 4 4  #4 	4
 4 +4 
4 4 4 4l- - - -^6 6 6 6p4S#X+? D    @ ) ) ) ) X)N N NSRUX N N N N
H H3 H# H H H H
 I I I I \I -# - - - \-  !y!a!a!abb/d38n)= /$tCQTH~BV / / / \/
 3c3h 3D 3 3 3 \3j <tCH~&<	d38n	< < < \< T d    \< GD GT G G G \G"13 13: 1 1 1 1
' ' '
 G G G GC GPS G G G \G 6 6x}hsm7S1T 6 6 6 \6 +/S SS #3-S 
	S S S S/# / / / /
S 
 
 
 
Y_          \:  s C    \0W4 WC WQU WZ] W W W W
 / / / / / \/
3 3  QU    0     "s s     .4 . . . .
 A A$ A A A \A4P P$ P P P P
 Z^ W W Ws WQU Wbe W W W W4G3 G3 G4 G G G G3 3 4    BJ JD J# JV^ J J J J
J JD J# J J J J
 FJ H H Hd Hd H H H HZ *.	/ / / / !4Z	/
 
/ / / /b! ! ! ! !F14 1 1 1 1f&A3 &A4 &A &A &A &APJ J J JF 7;26
n 
n 
n c]
n 	
n
 $N3
n  S#X/
n 
tTz	
n 
n 
n 
n$ $ $ $ $DT D D D D
   48$ 8 8 8 8'3 '3 '3 '3RC D     1c 1c 1 1 1 \1
	>S 	>T 	> 	> 	> 	>LT#s(^ LPT L L L L37s 37t 37 37 37 37j# $         
t 
 
 
 
 ?Ca a aa3;a a a a3 3-D 3PT 3 3 3 3-$ - - - -
p"p47pFIp	p p p p # $    \ C E#xPT~BU<V    \:1 13 1SV 1 1 1 1f    0*[S *[ *[ *[ *[ *[ *[X s    $     84 D    @9s 9C 9TW 9 9 9 9v@ @ @ @ @ @
Hd Ht H H H HT 
$  
  
  
  
DM M M M M     BD T    84d 4t 4 4 4 4
(T (T (T (T (TT DI        D
HSM 
 
 
 
,TD[ , , , ,@O OQU O O O O
t    6
D 
 
 
 
(
 
 
 
 

D 
 
 
 
 I$ I IRV I I I I
G G# G G G G
 T d    \6  	\ \ \\ 	\
 
\ \ \ \\2T 2 2 2 2 ^br{  QU  ej 
 
 
$ 
 
WZ 
lo 
  KN 
  ^b 
  ot 
 
 
 
:1F :4 : : : :

<Q 
VY 
 
 
 
  	  
   (40E 4# 4 4 4 4* *t *X[ *mp *y} * * * *.
T 
c 
 
 
 
( KO49B B# Bd BWZ B#+C=BCGB-1B>AB B B B ( (S ( (S (s ( ( ( \(0s s$ scf sx{ s  EI s s s s
s s$ scf sx{ s  EI s s s s
Et ES ES E E E E #59.2.2J JJ J #4S>2	J
 J "(+J 'smJ 
c3hJ J J J( (C ((82D (PS ( ( ( (* &+Z Z Z Z  #	Z
 tCH~&Z Z #Z 
c3hZ Z Z Z Z ZrU   r   rZ   r   Fr   queryr   r   	max_turnsr   r   
list_toolsr   save_sampleverboser   c                    t          d           t          d           |rddlm}m} ddlm}m} t          d           t          d           t          d           t          d	            |            }g }g }g }|                                D ]_\  }} ||          }|rM||f}|d
v r|                    |           0|dv r|                    |           J|                    |           `t          d           |D ]X\  }}|d         rd	                    |d                   nd}t          d|dd|d                     t          d|            Yt          d           |D ]p\  }}|d         rd	                    |d                   nd}t          d|dd|d                     t          d|            t          d|d                     qt          d           |D ]9\  }}t          d|dd|d                     t          d|d                     :t          d            |            }|                                D ]b\  }}|d         rdnd}t          d | d!| d"|d                     |d         s+t          d#d	                    |d$                               c |            }t          d%t          |           d&           t          |          D ]'}t          |          }t          d'| d(| d)           (t          d*           t          d+           t          d,           t          d-           t          d.           t          d            t          d/           t          d0           t          d            t          d1           t          d2           t          d            t          d3           t          d4           d5S d5}d5}|r1d6 |                    d7          D             }t          d8|            |r1d9 |                    d7          D             }t          d:|            |r-t          d;           t          d<           t          d=           	 t          ||||||||
|>	  	        } n*# t          $ r}!t          d?|!            Y d5}!~!d5S d5}!~!ww xY w| d@}"n| }"t          dA|"            t          dB           |                     |"          }#t          dB           t          dC           t          d           t          dD|#dE                     t          dF|#dG                     t          dHt          |#dI                               |#dJ         r3t          dK           t          dL           t          |#dJ                    |	rt#          t%          j                              d5dM         }$dN|$ dO}%|                     |#dI         |"|#dE                   }&|&t+          j                                                    ||#dE         |"dP}	 t1          |%dQdRS          5 }'|'                    t5          j        |dTdUV                     d5d5d5           n# 1 swxY w Y   t          dW|%            n)# t8          $ r}!t          dX|!            Y d5}!~!nd5}!~!ww xY wt          dY           d5S )Za  
    Main function for running the agent directly.

    Args:
        query (str): Natural language query for the agent. Defaults to Python 3.13 example.
        model (str): Model name to use (OpenRouter format: provider/model). Defaults to anthropic/claude-sonnet-4.6.
        api_key (str): API key for authentication. Uses OPENROUTER_API_KEY env var if not provided.
        base_url (str): Base URL for the model API. Defaults to https://openrouter.ai/api/v1
        max_turns (int): Maximum number of API call iterations. Defaults to 10.
        enabled_toolsets (str): Comma-separated list of toolsets to enable. Supports predefined
                              toolsets (e.g., "research", "development", "safe").
                              Multiple toolsets can be combined: "web,vision"
        disabled_toolsets (str): Comma-separated list of toolsets to disable (e.g., "terminal")
        list_tools (bool): Just list available tools and exit
        save_trajectories (bool): Save conversation trajectories to JSONL files (appends to trajectory_samples.jsonl). Defaults to False.
        save_sample (bool): Save a single trajectory sample to a UUID-named JSONL file for inspection. Defaults to False.
        verbose (bool): Enable verbose logging for debugging. Defaults to False.
        log_prefix_chars (int): Number of characters to show in log previews for tool calls/responses. Defaults to 20.

    Toolset Examples:
        - "research": Web search, extract, crawl + vision tools
    u   🤖 AI Agent with Tool Callingz2==================================================r   )get_all_tool_namesget_available_toolsets)get_all_toolsetsget_toolset_infou    📋 Available Tools & Toolsets:z2--------------------------------------------------u'   
🎯 Predefined Toolsets (New System):z(---------------------------------------->   webvisioncreativeterminalrW  >   r  research
full_stackdevelopmentcontent_creationu   
📌 Basic Toolsets:resolved_toolsz, r  r  r  z - r  z    Tools: u5   
📂 Composite Toolsets (built from other toolsets):includesz    Includes: z    Total tools: 
tool_countu!   
🎭 Scenario-Specific Toolsets:20u3   
📦 Legacy Toolsets (for backward compatibility):	availableu   ✅u   ❌z  r{  r  z    Requirements: requirementsu   
🔧 Individual Tools (z available):u     📌 z (from re   u   
💡 Usage Examples:z  # Use predefined toolsetszR  python run_agent.py --enabled_toolsets=research --query='search for Python news'zN  python run_agent.py --enabled_toolsets=development --query='debug this code'zP  python run_agent.py --enabled_toolsets=safe --query='analyze without terminal'z  # Combine multiple toolsetszM  python run_agent.py --enabled_toolsets=web,vision --query='analyze website'z  # Disable toolsetszQ  python run_agent.py --disabled_toolsets=terminal --query='no command execution'z&  # Run with trajectory saving enabledzF  python run_agent.py --save_trajectories --query='your question here'Nc                 6    g | ]}|                                 S r  r)  r  r,  s     rS   rU  zmain.<locals>.<listcomp>  s      P P Pq P P PrU   r  u   🎯 Enabled toolsets: c                 6    g | ]}|                                 S r  r  r  s     rS   rU  zmain.<locals>.<listcomp>  s     !R!R!R!''))!R!R!RrU   u   🚫 Disabled toolsets: u   💾 Trajectory saving: ENABLEDu:      - Successful conversations → trajectory_samples.jsonlu7      - Failed conversations → failed_trajectories.jsonl)	rW   r   r   r   r   r   r   r   r   u    ❌ Failed to initialize agent: zTell me about the latest developments in Python 3.13 and what new features developers should know about. Please search for current information and try it out.u   
📝 User Query: z3
==================================================u   📋 CONVERSATION SUMMARYu   ✅ Completed: rn  u   📞 API Calls: 	api_callsu   💬 Messages: rk  r;  u   
🎯 FINAL RESPONSE:z------------------------------rL   sample_r  )conversations	timestampr   rn  r  wr  r  Fr  )ensure_asciir  u"   
💾 Sample trajectory saved to: u   
⚠️ Failed to save sample: u    
👋 Agent execution completed!)r:  model_toolsr  r  rz  r  r  r  r^  r\  r`   sortedr   r  r   r  r  r^   uuiduuid4rq  r   r  r  openrW  r  r  r   )(r  r   r   rW   r  r   r   r  r   r  r  r   r  r  r  r  all_toolsetsbasic_toolsetscomposite_toolsetsscenario_toolsetsr'  toolsetr  r  	tools_strincludes_strlegacy_toolsetsr\  	all_toolsrV  enabled_toolsets_listdisabled_toolsets_listagentr   rm  r  	sample_idsample_filenamert  fs(                                           rS   mainr     s   H 

+,,,	(OOO  PJJJJJJJJ????????0111h 	8999h'')) )//11 		4 		4MD'##D))D 4tQQQ"))%0000fff&--e4444%,,U333 	&'''( 	- 	-JD$=ABR=S_		$'7"8999Y_I<4<<<tM':<<===+	++,,,, 	FGGG, 	< 	<JD$:>z:JV499T*%5666PVL<4<<<tM':<<===1<11222:d<&8::;;;; 	2333+ 	< 	<JD$<4<<<tM':<<===:d<&8::;;;; 	DEEE0022)//11 	N 	NJD$";/:UUUF=v====](;==>>>$ NL499T.5I+J+JLLMMM '&((	F#i..FFFGGG	** 	: 	:I*955G8I88g8889999&'''+,,,bccc^___`aaad-...]^^^d$%%%abbbd6777VWWW !! A P P4D4J4J34O4O P P P?(=??@@@ C!R!R5F5L5LS5Q5Q!R!R!RA)?AABBB I/000JKKKGHHH$24/#-

 

 

    444555
 }b 	

 
	
,

,
,---	/ ##J//F	/	
%&&&	(OOO	
1F;/
1
1222	
2VK0
2
2333	
5Cz 233
5
5666 (&'''hf%&'''  :
%%bqb)	4I444 88:;
 

 (!1133,
 
	:osW=== I
5uQGGGHHHI I I I I I I I I I I I I I I IIIJJJJ 	: 	: 	:8Q8899999999	: 

-.....sT   R! !
S+SSZ2 +ZZ2 ZZ2 ZZ2 2
[<[[__main__)NrZ   NrZ   r   NNFFFFr   )r   hermes_bootstrapModuleNotFoundErrorr  r  re  r  r  r  	getLoggerr}   r   r   r  rJ  r  r  r(  r  typingr   r   r   r   r   pathlibr   hermes_constantsr	   agent.process_bootstrapr
   r   r   agent.iteration_budgetr   hermes_cli.env_loaderr   hermes_cli.timeoutsr   r   _hermes_home__file__parent_project_env_loaded_env_paths	_env_pathr  r  r   r   r   r   tools.terminal_toolr   tools.interruptr   r  tools.browser_toolr   agent.memory_managerr   agent.error_classifierr   agent.redactr   r*  r   r   agent.usage_pricingr   agent.context_compressorr    agent.retry_utilsr!   agent.prompt_builderr"   r#   r$   r%   r&   r'   r(   agent.message_sanitizationr)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   agent.codex_responses_adapterr4   r  r5   r  r6   r  r7   agent.tool_guardrailsr8   r9   r:    agent.tool_result_classificationr;   r  r<   agent.trajectoryr=   r>   rs  agent.tool_dispatch_helpersr?   r@   rA   rB   rC   rD   rE   rF   rG   rH   utilsrI   rJ   rK   _MAX_TOOL_WORKERSEvent_openrouter_prewarm_donerh   rt  rT   r^   r	  rc   rn   r   rp   r   r   r  fireFirer  rU   rS   <module>r     sx   .	 	 	 	
 	D	      		8	$	$ 				 				 



        , , , , , , , , , , , ,             , , , , , ,         
 3 2 2 2 2 2 5 4 4 4 4 4       
   tH~~$v-&&<\ZZZ  K& G G	:IFFFFG KKIJJJ            + * * * * * ; ; ; ; ; ; . . . . . . 2 1 1 1 1 1 1 1 1 1 1 1 . . . . . .        0 / / / / / 6 6 6 6 6 6 . . . . . .                8 7 7 7 7 7                                             
                                      N M M M M M M M M M   +9?,,   T     %) #  #  #Tz #47$J #	 #  #  #  #F
d 
 
 
 
$
 $
 $
 $
 $
	 $
 $
 $
Nx?Z x?Z x?Z x?Z x?Z x?Z x?Z x?Zv  !#S/ S/S/S/ S/ 	S/
 S/ S/ S/ S/ S/ S/ S/ S/ S/ S/ S/l zKKKDIdOOOOO s   	 