
    o;ih                        U d Z ddlZddlZddlZddlZddlZddl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Zddlm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 m!Z!m"Z" ddl#m#Z# ddl$m%Z% ddl&m'Z' da(ee)         e*d	<   d
e)fdZ+ G d d          Z, e,            Z-ddl.m/Z/ ddl0m1Z1m2Z2  e'            Z3 e%e4          j5        dz  Z6 e/e3e6          Z7e7re7D ]Z8e9                    de8           ne9                    d           ddl:m;Z;m<Z<m=Z=m>Z> ddl?m@Z@mAZAmBZB ddl?mCZDmEZFmGZGmHZH ddlImJZJmKZK ddlLmMZN ddlOmPZP ddlQmRZRmSZSmTZT ddlUmVZV ddlWmXZXmYZY ddlZm[Z[m\Z\m]Z]m^Z^m_Z_m`Z`maZambZb ddlcmdZdmeZemfZfmgZgmhZhmiZimjZjmkZkmlZlmmZm ddlnmoZo dd lpmqZq dd!lrmsZs dd"lZmtZtmuZumvZvmwZwmxZxmyZymzZzm{Z{ dd#l|m}Z}m~Z~ dd$lmZmZmZmZ dd%lmZmZmZmZmZ dd&lmZmZmZmZmZ dd'lmZmZmZ dd(lmZmZmZmZmZ dd)lmZ  G d* d+          Zd
ee         fd,Zd-ee         d
ee         fd.Zdid/Z G d0 d1          Z ed2h          Z eh d3          Z eh d4          Zd5Z ej                    Z ej        d6ej                  Z ej        d7          Zd8ed
efd9Zd
efd:Zd;ed<ed
e%dz  fd=Zd>e%d?e%d
efd@Z ej        dA          ZdBed
efdCZdDed
efdEZdFed
efdGZdHed
efdIZdjdKed;ed
efdLZdBed
efdMZdFed
efdNZdOed
efdPZdDed
efdQZdRZd
efdSZd
efdTZd
efdUZ G dV dW          Z	 	 	 	 	 	 	 	 	 	 	 	 dkd\ed]ed^ed-ed_ed`edaedbedceddedeedfefdgZedhk    rddlZ ej        e˦           dS dS )lao  
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)SimpleNamespace)ListDictAnyOptional)urlparseparse_qs
urlunparse)datetime)Path)get_hermes_home_OPENAI_CLS_CACHEreturnc                  .    t           ddlm}  | a t           S )z#Import and cache ``openai.OpenAI``.Nr   )OpenAI)r   openair   )_clss    ./home/ubuntu/.hermes/hermes-agent/run_agent.py_load_openai_clsr   B   s(      ))))))     c                   (    e Zd ZdZdZd Zd Zd ZdS )_OpenAIProxyzHModule-level proxy that looks like ``openai.OpenAI`` but imports lazily. c                 *     t                      |i |S N)r   )selfargskwargss      r   __call__z_OpenAIProxy.__call__P   s    !!!426222r   c                 :    t          |t                                S r   )
isinstancer   )r   objs     r   __instancecheck__z_OpenAIProxy.__instancecheck__S   s    #/11222r   c                     dS )Nz<lazy openai.OpenAI proxy>r   r   s    r   __repr__z_OpenAIProxy.__repr__V   s    ++r   N)__name__
__module____qualname____doc__	__slots__r   r#   r&   r   r   r   r   r   K   sL        RRI3 3 33 3 3, , , , ,r   r   )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get_active_envis_persistent_env)set_approval_callbackset_sudo_password_callback_get_approval_callback_get_sudo_password_callback)maybe_persist_tool_resultenforce_turn_budget)set_interrupt)cleanup_browser)StreamingContextScrubberbuild_memory_context_blocksanitize_context)jittered_backoff)classify_api_errorFailoverReason)DEFAULT_AGENT_IDENTITYPLATFORM_HINTSMEMORY_GUIDANCESESSION_SEARCH_GUIDANCESKILLS_GUIDANCEHERMES_AGENT_HELP_GUIDANCEKANBAN_GUIDANCEbuild_nous_subscription_prompt)
fetch_model_metadataestimate_tokens_roughestimate_messages_tokens_roughestimate_request_tokens_roughget_next_probe_tierparse_context_limit_from_error(parse_available_output_tokens_from_errorsave_context_lengthis_local_endpointquery_ollama_num_ctx)ContextCompressor)SubdirectoryHintTracker)apply_anthropic_cache_control)build_skills_system_promptbuild_context_files_promptbuild_environment_hintsload_soul_mdTOOL_USE_ENFORCEMENT_GUIDANCETOOL_USE_ENFORCEMENT_MODELS!GOOGLE_MODEL_OPERATIONAL_GUIDANCEOPENAI_MODEL_EXECUTION_GUIDANCE)estimate_usage_costnormalize_usage)"_derive_responses_function_call_id_deterministic_call_id_split_responses_tool_id_summarize_user_message_for_log)KawaiiSpinnerbuild_tool_previewget_cute_tool_message_detect_tool_failureget_tool_emoji)ToolCallGuardrailConfigToolCallGuardrailControllerToolGuardrailDecisionappend_toolguard_guidancetoolguard_synthetic_result)convert_scratchpad_to_thinkhas_incomplete_scratchpadsave_trajectory)atomic_json_writebase_url_host_matchesbase_url_hostnameenv_var_enablednormalize_proxy_url)cfg_getc                   :    e Zd ZdZdZd Zd Zd Zd Zd Z	d Z
d	S )
_SafeWriterup  Transparent stdio wrapper that catches OSError/ValueError from broken pipes.

    When hermes-agent runs as a systemd service, Docker container, or headless
    daemon, the stdout/stderr pipe can become unavailable (idle timeout, buffer
    exhaustion, socket reset). Any print() call then raises
    ``OSError: [Errno 5] Input/output error``, which can crash agent setup or
    run_conversation() — especially via double-fault when an except handler
    also tries to print.

    Additionally, when subagents run in ThreadPoolExecutor threads, the shared
    stdout handle can close between thread teardown and cleanup, raising
    ``ValueError: I/O operation on closed file`` instead of OSError.

    This wrapper delegates all writes to the underlying stream and silently
    catches both OSError and ValueError. It is transparent when the wrapped
    stream is healthy.
    )_innerc                 >    t                               | d|           d S )Nr~   )object__setattr__)r   inners     r   __init__z_SafeWriter.__init__   s     4511111r   c                     	 | j                             |          S # t          t          f$ r) t	          |t
                    rt          |          ndcY S w xY wNr   )r~   writeOSError
ValueErrorr!   strlen)r   datas     r   r   z_SafeWriter.write   sb    	=;$$T***$ 	= 	= 	= *4 5 5<3t9991<<<	=s    7AAc                 j    	 | j                                          d S # t          t          f$ r Y d S w xY wr   )r~   flushr   r   r%   s    r   r   z_SafeWriter.flush   sH    	K$ 	 	 	DD	s    22c                 4    | j                                         S r   )r~   filenor%   s    r   r   z_SafeWriter.fileno   s    {!!###r   c                 f    	 | j                                         S # t          t          f$ r Y dS w xY w)NF)r~   isattyr   r   r%   s    r   r   z_SafeWriter.isatty   sB    	;%%'''$ 	 	 	55	s    00c                 ,    t          | j        |          S r   )getattrr~   )r   names     r   __getattr__z_SafeWriter.__getattr__   s    t{D)))r   N)r'   r(   r)   r*   r+   r   r   r   r   r   r   r   r   r   r}   r}      s         $ I2 2 2= = =  $ $ $  * * * * *r   r}   c                      dD ]G} t           j                            | d                                          }|rt	          |          c S HdS )zRead proxy URL from environment variables.

    Checks HTTPS_PROXY, HTTP_PROXY, ALL_PROXY (and lowercase variants) in order.
    Returns the first valid proxy URL found, or None if no proxy is configured.
    )HTTPS_PROXY
HTTP_PROXY	ALL_PROXYhttps_proxy
http_proxy	all_proxy N)osenvirongetstriprz   )keyvalues     r   _get_proxy_from_envr      sZ    : . .
sB''--// 	.&u-----	.4r   base_urlc                     t                      }|r| s|S t          |           }|s|S 	 t          j                            |          rdS n# t
          $ r Y nw xY w|S )zFReturn an env-configured proxy unless NO_PROXY excludes this base URL.N)r   rx   urllibrequestproxy_bypass_environment	Exception)r   proxyhosts      r   _get_proxy_for_base_urlr      s    !!E  X&&D >22488 	4	    Ls   A 
AAc                      dD ]R} t          t          | d          }|8t          |t                    s#t	          t          | t          |                     SdS )zHWrap stdout/stderr so best-effort console output cannot crash the agent.)stdoutstderrN)r   sysr!   r}   setattr)stream_namestreams     r   _install_safe_stdior     s\    + ; ;k400j&E&ECk&&9&9:::; ;r   c                   j    e Zd ZdZdefdZdefdZd
dZe	defd            Z
e	defd	            ZdS )IterationBudgetua  Thread-safe iteration counter for an agent.

    Each agent (parent or subagent) gets its own ``IterationBudget``.
    The parent's budget is capped at ``max_iterations`` (default 90).
    Each subagent gets an independent budget capped at
    ``delegation.max_iterations`` (default 50) — this means total
    iterations across parent + subagents can exceed the parent's cap.
    Users control the per-subagent limit via ``delegation.max_iterations``
    in config.yaml.

    ``execute_code`` (programmatic tool calling) iterations are refunded via
    :meth:`refund` so they don't eat into the budget.
    	max_totalc                 R    || _         d| _        t          j                    | _        d S r   )r   _used	threadingLock_lock)r   r   s     r   r   zIterationBudget.__init__  s#    "
^%%


r   r   c                     | j         5  | j        | j        k    r	 ddd           dS | xj        dz  c_        	 ddd           dS # 1 swxY w Y   dS )z7Try to consume one iteration.  Returns True if allowed.NF   T)r   r   r   r%   s    r   consumezIterationBudget.consume#  s    Z 	 	zT^++	 	 	 	 	 	 	 	 JJ!OJJ		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   AAA	A	Nc                 |    | j         5  | j        dk    r| xj        dz  c_        ddd           dS # 1 swxY w Y   dS )z6Give back one iteration (e.g. for execute_code turns).r   r   N)r   r   r%   s    r   refundzIterationBudget.refund+  s    Z 	  	 zA~~

a

	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 s   155c                     | j         S r   )r   r%   s    r   usedzIterationBudget.used1  s
    zr   c                 ~    | j         5  t          d| j        | j        z
            cd d d            S # 1 swxY w Y   d S r   )r   maxr   r   r%   s    r   	remainingzIterationBudget.remaining5  s    Z 	7 	7q$.4:566	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7s   266r   N)r'   r(   r)   r*   intr   boolr   r   propertyr   r   r   r   r   r   r     s         &# & & & &
            c    X 73 7 7 7 X7 7 7r   r   clarify>   	read_file
skill_view
web_searchskills_listweb_extractha_get_statesearch_filessession_searchvision_analyzeha_list_entitiesha_list_services>   patchr   
write_file   z(?:^|\s|&&|\|\||;|`)(?:
        rm\s|rmdir\s|
        cp\s|install\s|
        mv\s|
        sed\s+-i|
        truncate\s|
        dd\s|
        shred\s|
        git\s+(?:reset|clean|checkout)\s
    )z[^>]>[^>]|^>[^>]cmdc                 ~    | sdS t                               |           rdS t                              |           rdS dS )zJHeuristic: does this terminal command look like it modifies/deletes files?FT)_DESTRUCTIVE_PATTERNSsearch_REDIRECT_OVERWRITE)r   s    r   _is_destructive_commandr   l  sJ     u##C(( t!!#&& t5r   c           	         t          |           dk    rdS d | D             }t          d |D                       rdS g }| D ]}|j        j        }	 t	          j        |j        j                  }n:# t          $ r- t          j	        d||j        j        dd                    Y  dS w xY wt          |t                    s+t          j	        d|t          |          j                    dS |t          v rIt          ||           dS t          fd	|D                       r dS |                               |t"          vr dS 	d
S )z?Return True when a tool-call batch is safe to run concurrently.r   Fc                 &    g | ]}|j         j        S r   functionr   .0tcs     r   
<listcomp>z2_should_parallelize_tool_batch.<locals>.<listcomp>|  s    888r"+"888r   c              3   (   K   | ]}|t           v V  d S r   )_NEVER_PARALLEL_TOOLS)r   r   s     r   	<genexpr>z1_should_parallelize_tool_batch.<locals>.<genexpr>}  s(      
@
@T4((
@
@
@
@
@
@r   u@   Could not parse args for %s — defaulting to sequential; raw=%sN   u6   Non-dict args for %s (%s) — defaulting to sequentialc              3   8   K   | ]}t          |          V  d S r   )_paths_overlap)r   existingscoped_paths     r   r   z1_should_parallelize_tool_batch.<locals>.<genexpr>  s-      XXX>+x88XXXXXXr   T)r   anyr   r   jsonloads	argumentsr   loggingdebugr!   dicttyper'   _PATH_SCOPED_TOOLS_extract_parallel_scope_pathappend_PARALLEL_SAFE_TOOLS)
tool_calls
tool_namesreserved_paths	tool_call	tool_namefunction_argsr   s         @r   _should_parallelize_tool_batchr  w  s   
:!u88Z888J

@
@Z
@
@
@@@ u!#N  	&+		 Jy'9'CDDMM 	 	 	MR",TcT2  
 555	 -.. 	MH]##,  
 55***6y-PPK"uuXXXXXXXXX uu!!+...00055 1 4s   A112B('B(r  r  c                    | t           vrdS |                    d          }t          |t                    r|                                sdS t          |                                          }|                                r9t          t          j	        
                    t          |                              S t          t          j	        
                    t          t          j                    |z                                S )z8Return the normalized file target for path-scoped tools.Npath)r   r   r!   r   r   r   
expanduseris_absoluter   r  abspathcwd)r  r  raw_pathexpandeds       r   r   r     s    ***t  ((Hh$$ HNN,<,< tH~~((**H 4BGOOCMM22333 DHJJ$9 : :;;<<<r   leftrightc                    | j         }|j         }|r|s/t          |          t          |          k    ot          |          S t          t          |          t          |                    }|d|         |d|         k    S )z9Return True when two paths may refer to the same subtree.N)partsr   minr   )r  r  
left_partsright_parts
common_lens        r   r   r     s    J+K J[ JJ4#4#44Ij9I9IIS__c+&6&677Jkzk"k+:+&>>>r   z[\ud800-\udfff]textc                 p    t                               |           rt                               d|           S | S )zReplace lone surrogate code points with U+FFFD (replacement character).

    Surrogates are invalid in UTF-8 and will crash ``json.dumps()`` inside the
    OpenAI SDK.  This is a fast no-op when the text contains no surrogates.
       �)_SURROGATE_REr   subr  s    r   _sanitize_surrogatesr    s5     D!! 1  4000Kr   payloadc                 0    dfd |            S )uu  Replace surrogate code points in nested dict/list payloads in-place.

    Mirror of ``_sanitize_structure_non_ascii`` but for surrogate recovery.
    Used to scrub nested structured fields (e.g. ``reasoning_details`` — an
    array of dicts with ``summary``/``text`` strings) that flat per-field
    checks don't reach.  Returns True if any surrogates were replaced.
    Fc                    t          | t                    r|                                 D ]|\  }}t          |t                    r;t                              |          r t                              d|          | |<   dUt          |t          t          f          r |           }d S t          | t                    rt          |           D ]~\  }}t          |t                    r;t                              |          r t                              d|          | |<   dUt          |t          t          f          r |           }d S d S )Nr  T)	r!   r   itemsr   r  r   r  list	enumerate)noder   r   idx_walkfounds       r   r#  z-_sanitize_structure_surrogates.<locals>._walk  sQ   dD!! 	!"jjll ! !
UeS)) !$++E22 %$1$5$5h$F$FS	 $d|44 !E%LLL! ! d## 	!'oo ! !
UeS)) !$++E22 %$1$5$5h$F$FS	 $d|44 !E%LLL	! 	!! !r   r   r  r#  r$  s    @@r   _sanitize_structure_surrogatesr&    s=     E! ! ! ! ! !& 
E'NNNLr   messagesc                 p   d}| D ]}t          |t                    s|                    d          }t          |t                    r;t                              |          r!t                              d|          |d<   d}nt          |t                    r~|D ]{}t          |t                    rd|                    d          }t          |t                    r:t                              |          r t                              d|          |d<   d}||                    d          }t          |t                    r:t                              |          r t                              d|          |d<   d}|                    d          }t          |t                    rs|D ]o}t          |t                    s|                    d          }	t          |	t                    r:t                              |	          r t                              d|	          |d<   d}|                    d	          }
t          |
t                    r|
                    d          }t          |t                    r:t                              |          r t                              d|          |
d<   d}|
                    d
          }t          |t                    r:t                              |          r t                              d|          |
d
<   d}q|                                D ]\  }}|dv r
t          |t                    r;t                              |          r t                              d|          ||<   d}Zt          |t          t          f          rt          |          rd}|S )a  Sanitize surrogate characters from all string content in a messages list.

    Walks message dicts in-place. Returns True if any surrogates were found
    and replaced, False otherwise. Covers content/text, name, tool call
    metadata/arguments, AND any additional string or nested structured fields
    (``reasoning``, ``reasoning_content``, ``reasoning_details``, etc.) so
    retries don't fail on a non-content field.  Byte-level reasoning models
    (xiaomi/mimo, kimi, glm) can emit lone surrogates in reasoning output
    that flow through to ``api_messages["reasoning_content"]`` on the next
    turn and crash json.dumps inside the OpenAI SDK.
    Fcontentr  Tr  r   r   idr   r   >   r   roler)  r   )
r!   r   r   r   r  r   r  r  r  r&  )r'  r$  msgr)  partr  r   r   r   tc_idfnfn_namefn_argsr   r   s                  r   _sanitize_messages_surrogatesr2    sw    E 3! 3!#t$$ 	'')$$gs## 		%(<(<W(E(E 		%*..xAAC	NEE&& 	% % %dD)) %88F++D!$,, %1E1Ed1K1K %'4'8'84'H'HV $wwvdC   	]%9%9$%?%? 	'++Hd;;CKEWW\**
j$'' 	%  % %!"d++ teS)) !m.B.B5.I.I !,005AABtH EVVJ''b$'' % ffVnnG!'3// %M4H4H4Q4Q %%2%6%6x%I%I6
 $ ff[11G!'3// %M4H4H4Q4Q %*7*;*;Hg*N*N; $ ))++ 		! 		!JC???%%% ! ''.. !,005AACH EED$<00 !1%88 ! E		! Lr   rawc                 2   g }d}d}t          |           }||k     r| |         }|r|dk    rB|dz   |k     r9|                    |           |                    | |dz                       |dz  }X|dk    rd}|                    |           nmt          |          dk     r'|                    dt          |          d	           n3|                    |           n|dk    rd
}|                    |           |dz  }||k     d                    |          S )ug  Escape unescaped control chars inside JSON string values.

    Walks the raw JSON character-by-character, tracking whether we are
    inside a double-quoted string. Inside strings, replaces literal
    control characters (0x00-0x1F) that aren't already part of an escape
    sequence with their ``\uXXXX`` equivalents. Pass-through for everything
    else.

    Ported from #12093 — complements the other repair passes in
    ``_repair_tool_call_arguments`` when ``json.loads(strict=False)`` is
    not enough (e.g. llama.cpp backends that emit literal apostrophes or
    tabs alongside other malformations).
    Fr   \r      "    z\u04xTr   )r   r   ordjoin)r3  out	in_stringinchs         r   %_escape_invalid_chars_in_json_stringsrA  9  s+    CI	ACA
a%%V 	Tzza!eaii

2

3q1u:&&&QSyy!	

2R4

.R...////

2Syy 	JJrNNN	Q) a%%* 773<<r   ?raw_argsc                 z   t          | t                    r|                                 nd}|st                              d|           dS |dk    rt                              d|           dS 	 t          j        |d          }t          j        |d	          }||k    rt                              d
|           |S # t
          j        t          t          f$ r Y nw xY w|}t          j        dd|          }|                    d          |                    d          z
  }|                    d          |                    d          z
  }|dk    r|d|z  z  }|dk    r|d|z  z  }t          d          D ]}	 t          j        |            n# t
          j        $ r |                    d          r7|                    d          |                    d          k    r|dd         }nO|                    d          r7|                    d          |                    d          k    r|dd         }nY  nY w xY w	 t          j        |           t                              d||dd         |dd                    |S # t
          j        $ r Y nw xY w	 t!          |          }	|	|k    rCt          j        |	           t                              d||dd         |	dd                    |	S n"# t
          j        t          t          f$ r Y nw xY wt                              d||dd                    dS )a  Attempt to repair malformed tool_call argument JSON.

    Models like GLM-5.1 via Ollama can produce truncated JSON, trailing
    commas, Python ``None``, etc.  The API proxy rejects these with HTTP 400
    "invalid tool call arguments".  This function applies common repairs;
    if all fail it returns ``"{}"`` so the request succeeds (better than
    crashing the session).  All repairs are logged at WARNING level.
    r   z*Sanitized empty tool_call arguments for %s{}Nonez0Sanitized Python-None tool_call arguments for %sF)strict,:)
separatorsz>Repaired unescaped control chars in tool_call arguments for %sz,\s*([}\]])z\1{}[]r   2   Nu8   Repaired malformed tool_call arguments for %s: %s → %sP   uA   Repaired control-char-laced tool_call arguments for %s: %s → %suP   Unrepairable tool_call arguments for %s — replaced with empty object (was: %s))r!   r   r   loggerwarningr   r   dumpsJSONDecodeError	TypeErrorr   rer  countrangeendswithrA  )
rC  r  raw_strippedparsedreserialisedfixed
open_curlyopen_bracket_escapeds
             r   _repair_tool_call_argumentsrd  c  s    (2(C'@'@H8>>###bL  CYOOOt vI9UUUt
L777z&Z@@@<''NNP    )Z8    EF>5%00ES!!EKK$4$44J;;s##ekk#&6&66LA~~z!!a|##2YY 
 
		JuE# 	 	 	~~c"" u{{3'7'7%++c:J:J'J'Jcrc
$$ S)9)9EKK<L<L)L)Lcrc
	
5F|CRC(%*	
 	
 	
    
7>>eJwNNS<,gcrcl   N   )Z8   
 NN	/<$  
 4sL   /AB> >CC<FB'H?>H?AJ JJAK5 5LLc                 V    |                      dd                              d          S )zRemove non-ASCII characters, replacing with closest ASCII equivalent or removing.

    Used as a last resort when the system encoding is ASCII and can't handle
    any non-ASCII characters (e.g. LANG=C on Chromebooks).
    asciiignore)errors)encodedecoder  s    r   _strip_non_asciirk    s(     ;;wx;0077@@@r   c                 R   d}| D ] }t          |t                    s|                    d          }t          |t                    rt	          |          }||k    r||d<   d}nut          |t
                    r`|D ]]}t          |t                    rF|                    d          }t          |t                    rt	          |          }||k    r||d<   d}^|                    d          }t          |t                    rt	          |          }||k    r||d<   d}|                    d          }t          |t
                    r|D ]}	t          |	t                    rq|	                    di           }
t          |
t                    rF|
                    d          }t          |t                    rt	          |          }||k    r||
d<   d}|                                D ];\  }}|d	v r
t          |t                    rt	          |          }||k    r|||<   d}<"|S )
a  Strip non-ASCII characters from all string content in a messages list.

    This is a last-resort recovery for systems with ASCII-only encoding
    (LANG=C, Chromebooks, minimal containers).  Returns True if any
    non-ASCII content was found and sanitized.
    Fr)  Tr  r   r   r   r   >   r   r+  r)  r   )r!   r   r   r   rk  r  r  )r'  r$  r,  r)  	sanitizedr-  r  r   r   r   r/  r1  r   r   s                 r   _sanitize_messages_non_asciirn    s]    E /! /!#t$$ 	'')$$gs## 	)(11IG##!*I&& 	) ) )dD)) )88F++D!$,, )$4T$:$:	$,,+4DL$(EwwvdC   	(..ID  'FWW\**
j$'' 
	-  	- 	-b$'' -
B//B!"d++ -"$&&"5"5%gs33 -(8(A(AI(G332;;(,))++ 	! 	!JC???%%% !,U33	%%(CH E	! Lr   toolsc                      t          |           S )z7Strip non-ASCII characters from tool payloads in-place.)_sanitize_structure_non_asciiro  s    r   _sanitize_tools_non_asciirs    s    (///r   c                 0    dfd |            S )zCStrip non-ASCII characters from nested dict/list payloads in-place.Fc                 &   t          | t                    ru|                                 D ]^\  }}t          |t                    rt	          |          }||k    r|| |<   d7t          |t          t
          f          r |           _d S t          | t
                    rnt          |           D ]`\  }}t          |t                    rt	          |          }||k    r|| |<   d7t          |t          t
          f          r |           _d S d S )NT)r!   r   r  r   rk  r  r   )r!  r   r   rm  r"  r#  r$  s        r   r#  z,_sanitize_structure_non_ascii.<locals>._walk  s5   dD!! 	!"jjll ! !
UeS)) ! 0 7 7I E))$-S	 $d|44 !E%LLL! ! d## 	!'oo ! !
UeS)) ! 0 7 7I E))$-S	 $d|44 !E%LLL	! 	!! !r   r   r%  s    @@r   rq  rq    s;    E! ! ! ! ! !* 
E'NNNLr   z0.14.1c                      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_clirw  )_HERMES_VERSIONs    r   _routermint_headersr{  :  s.    999999 	6_66 r   c                     | dS |                                  sdS t          |                                           dk    S )uH  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.

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

    See issue #11314.
    NFr   )has_availabler   entries)pools    r   !_pool_may_recover_from_rate_limitr  C  sB    $ |u ut||~~""r   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; )enablez
qwen-oauth)rx  zX-DashScope-CacheControlzX-DashScope-UserAgentzX-DashScope-AuthType)platform_QWEN_CODE_VERSIONsystemlowermachine)_plat_uas     r   _qwen_portal_headersr  \  sf    
X(
X
XELLNN,@,@,B,B
X
Xemmoo
X
X
XC$,!$ ,	  r   c            w       @   e Zd ZdZdZedefd            Zej        deddfd            Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dLd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d(ed)ed*ed+ed,ed-ed.ed/ed0ed1ed2ed3e
d4eeef         d5ed6eeef         d7eeeef                  d8ed9ed:ed;ed<ed=ed>ed?ed@edAedBedCedDdEdFeeef         dGedHe
dIefvdJZdMdKZdL ZdNdMee
         ddfdNZdOdOZdP ZddQdRefdSZdefdTZdefdUZdVeddfdWZdVeddfdXZdYedZeddfd[Zdeeef         fd\ZdMd]Z dMd^Z!dNdedefd_Z"dNdedefd`Z#defdaZ$de%eef         fdbZ&dce	e'eef                  defddZ(defdeZ)dddddfdee         dee         dee         dee         de%eef         f
dgZ*e+dedefdh            Z,e+ddidedee         defdj            Z-de
de'fdkZ.dledefdmZ/dledefdnZ0e+dledefdo            Z1defdpZ2	 dNdqedcee	         defdrZ3dsedtedceeeef                  defduZ4dee         fdvZ5dweddfdxZ6dyZ7dzZ8d{Z9e+d|ee         d}ee         dee         fd~            Z:	 	 dPdee         dededdfdZ;ddddddee         dee         dwee         dee         deeef         f
dZ<dcee         ddfdZ=dNdcee         dee         fdZ>dNdcee         dee         fdZ?dcee         dee         fdZ@defdZAdceeeef                  dededeeeef                  fdZBdceeeef                  dedefdZCe+deDdefd            ZEdee         dee         fdZFdedefdZGe+deDdeeef         fd            ZHdedeeeef                  fdZIdddeeef         dedeeD         deeJ         fdZKe+dledefd            ZLdNdceeeef                  fdZMdNdVeddfdZNdMdZOdedefdZPdee         fdZQdce	de
ddfdZRdeddfdZSdeddfdZTd ZUdeddfdZVde'fdZWdNdce	ddfdZXdNdce	ddfdZYdedededdfdZZdMdZ[dMdZ\deeeef                  ddfdZ]edefd            Z^dNdedefdZ_e+defd            Z` eah d          Zbe+dceeeef                  deeeef                  fd            Zce+deeef         defd            Zde+dceeeef                  deeeef                  fd            Zee+de	de	fdÄ            Zfe+de	de	fdĄ            Zgdededz  fdƄZhdǄ Zie+dQdedede
defd̄            Zje+dede%ee         ee         f         fd΄            Zk	 dNdedee         defdфZldefd҄ZmdefdӄZndeojp        fdԄZqe+dedefdք            Zre+dRdedefdׄ            Zsde'dededefdڄZte+dede
fdۄ            Zudedededdfd܄Zvdedefd݄ZwdedefdބZxdefd߄Zye+de'defd            Zzdede'fdZ{dddedee'         defdZ|dededdfdZ}dSde'dedefdZ~dNde'defdZddQdRedefdZddQdRedefdZdefdZdefdZdeddfdZdMdZddddee
         dedee         deeeef                  de%eef         f
dZde'fdZdMdZde'fdZdMdZdeddfdZe+dedefd            ZdledefdZdeeef         ddfdZdeddfdZdeddfd ZdeddfdZdefdZddde'defdZdNdddefdZdefdZ eah d          Zd	eDd
e
de
defdZe+dledefd            Ze+dede%eeeJ         f         fd            ZdededefdZdefdZdlededefdZdNdefdZde	de	fdZde	de	fdZde	defdZdefdZdefdZde	de	fdZdce	ddfdZde	de'fdZdefdZde	e         fdZdee         fd Zde'dz  fd!Zdqede'fd"Zdefd#Zdefd$Zdefd%Zd&e'd'e'ddfd(Ze+d'e'de'fd)            Ze+ddd*dce	d'ede
fd+            Zdefd,Zdd-dd.dce	ded/e
dwed0ede%fd1Zd2eddfd3Zd2edefd4Zded5e'd6ed7edef
d8Zd2edefd9ZdQdce	d:ed;e
ddfd<Zd5e'defd=Z	 	 dTd>ed5e'd:edee         dce	d?edefd@Ze+dUdBededCedefdD            ZdQdce	d:ed;e
ddfdEZdQdce	d:ed;e
ddfdFZdce	d;e
defdGZ	 	 	 	 	 dVdsededeeeef                  dwedHee         dIee         deeef         fdJZdNdVedHee         defdKZdS (W  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.]r   c                     | j         S r   )	_base_urlr%   s    r   r   zAIAgent.base_urlv  s
    ~r   r   Nc                 v    || _         |r|                                nd| _        t          |          | _        d S Nr   )r  r  _base_url_lowerrx   _base_url_hostnamer   r   s     r   r   zAIAgent.base_urlz  s8    05=u{{}}}2"3E":":r   r   Z         ?Fd   rP  r   api_keyproviderapi_modeacp_commandacp_argscommandr   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
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_messagesr  user_id	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pass_session_idc>                 L   t                       |	| _        |
| _        |8pt          |
          | _        || _        || _        || _        || _        || _	        |+| _
        |,| _        |-| _        |.| _        |/| _        |0| _        |1| _        |2| _        d| _        d| _        |3| _        |4| _        |=| _        |:| _        || _        |r| dnd| _        |pd| _        t7          |t8                    r:|                                r&|                                                                nd}>|>pd| _        |p|| _         tC          |p|pg           | _"        |dv r	|| _#        n| j        dk    rd| _#        n| j        dk    rd| _#        n|>#| j$        dk    rd	| j%        v rd| _#        d| _        n|>| j$        d
k    rd| _#        d| _        n| j        dk    s|>| j$        dk    rd| _#        d| _        n~| j%        &                    d          '                    d          rd| _#        nI| j        dk    s/| j$        (                    d          rtS          | j%        d          rd| _#        nd| _#        	 | *                                 n# tV          $ r Y nw xY w	 ddl,m-}?m.}@ | j        |?vr |@| j        | j                  | _        n# tV          $ r Y nw xY w|| j#        dk    r| j        dk    rt9          | j        pd                                          (                    d          st9          | j        pd                                          (                    d          sy| /                                se| 0                                s!| 1                    | j        | j                  r0d| _#        te          | d          r| j3        4                                 | j        dk    s| 5                                r`tl          7                                sGtl          8                                 ts          j:        tv          dd          <                                 || _=        || _>        || _?        d | _@        || _A        || _B        | | _C        |!| _D        |"| _E        |#| _F        |%| _G        |$| _H        d | _I        t                      | _K        d| _L        d | _M        d| _N        d| _O        d | _P        ts          jQ                    | _R        d| _S        ts          jT                    | _U        tq                      | _V        ts          jT                    | _W        d| _X        g | _Y        ts          jT                    | _Z        || _[        || _\        || _]        || _^        || __        || _`        || _a        || _b        |&| _c        |'| _d        |(| _e        t          |)pi           | _g        |*pg | _h        d | _i        | j                                \  | _k        | _l        d!| _m        	 dd"lnmo}A  |A            p                    d#i           pi }B|Bp                    d$d!          }C|Cd%v r|C| _m        n# tV          $ r Y nw xY wd | _q        d | _r        t          js                    | _t        d&| _u        d| _v        d| _w        d| _x        d| _y        dd'lzm{}Dm|}E  |Dt          (           | j        r% |E             t                              d)           n?| j        r8d*D ]5}Ft          j        |F                              t           j                   6d| _        d | _        t                      | _        d| _        d| _        d| _        i | _        d| _        d | _        t          | j        | j                  }G| j#        dk    rdd+lm}Hm}I | j        dk    }J|Jrdd,lm}K t'          j        d-|pd          }L|Lr|L                    d.          nd/}M|M| _         |K|M          | _        d0| _        || _        d | _        d0| _        d| _        i | _        | j        st9          d1| j         d2|M d3           n| j        dk    }N|Nr|p |I            pdn|pd}O|O| _        |O| _        || _        dd4lm}P |Nr |P|O          nd | _         |H|O||G5          | _        d| _        i | _        | j        sUt9          d1| j         d6           |Or:t=          |O          d7k    r&t9          d8|Odd9          d:|Od;d                     n&| j#        dk    rVt'          j        d-|pd          }L|Lr|L                    d.          nd/| _        d| _        	 dd"lnmo}Q  |Q            p                    di           p                    d<i           }R|Rp                    d=          ru|Rp                    d>          r`|Rd=         |Rd>         d?| _        |Rp                    d@          r|Rd@         | j        dA<   |Rp                    dB          r|RdB         | j        dB<   n# tV          $ r Y nw xY wd| _        i | _        | j        s.| j        rdCnd}St9          d1| j         dD| j         |S d3           n|rt|rqtA          |          }T|Tj        r\tE          |T                    dE                    }UdF tI          |Tj                                                  D             }V||U|VdG}Wn||dH}W|G|G|WdI<   | j        dk    r| j         |WdJ<   | j"        |WdK<   |}XtS          |XdL          rddMlm}Y  |Y            |WdN<   ntS          |XdO          rtQ                      |WdN<   ntS          |XdP          rddQlm}Z  |Z            |WdN<   ntS          |XdR          r	dSdTi|WdN<   ntS          |XdU          rtW                      |WdN<   ntS          |Xd          rddVlm}[  |[|          |WdN<   nnddWlm}\  |\| j        pdX| j        dY          \  }]}^|]S|]j        t9          |]j                  dH}W|G|G|WdI<   te          |]dZ          r|]j        rt          |]j                  |WdN<   n| j        pd                                                                }_|_r|_d[vr|_                                 d\}`	 dd]lm}a |ap                    |_          }b|br|bj        r|bj        d         }`n# tV          $ r Y nw xY wg }ct7          |9tB                    rd^ |9D             }cnBt7          |9t                    r-|9p                    d_          r|9p                    d`          r|9g}cd }d|cD ]}e |\|ed_         |ed`         d|ep                    da          |ep                    db          c          \  }f}g|fx|ed_         | _        |gp|ed`         | _        d| _        |fj        t9          |fj                  dH}W|G|G|WdI<   te          |fdZ          r|fj        rt          |fj                  |WdN<   d}d n|dsti          dd|_ de|` df          tk          | dgd           sti          dh          |W| _        t9          |Wp                    dad                                                    }htS          |hdL          rgdi| j        pd                                v rJ|Wp                    dN          pi }i|ip                    djd          }jdk}k|k|jvr|jr|j dl|k |idj<   n|k|idj<   |i|WdN<   |Wp                    dbd          | _        |Wp                    da| j                  | _        	 |                     |Wdmdn          | _        | j        st9          d1| j                    |rt9          do|            |Wp                    dbdp          }l|lrA|ldqk    r;t=          |l          d7k    r't9          dr|ldd9          d:|ld;d                     n t9          ds|lr
|lddt         ndp du           n%# tV          $ r}mti          dv|m           d}m~mww xY wt7          |9tB                    rdw |9D             | _        nOt7          |9t                    r3|9p                    d_          r|9p                    d`          r	|9g| _        ng | _        d| _        tk          | dgd           | _        | j        r| j        d         nd| _        | j        r| j        st=          | j                  d.k    r1| j        d         }nt9          dx|nd`          dy|nd_          d3           nLt9          dzt=          | j                   d{d|                    d} | j        D                       z              tw          ||| j        ~          | _        tq                      | _        | j        rd | j        D             | _        t}          | j                  }o| j        st9          dt=          | j                   dd                    |o                      |r&t9          dd                    |                      |r&t9          dd                    |                      n| j        st9          d           | j        rI| j        sBt                      }pd |p                                D             }q|qrt9          d|q            | j        r| j        st9          d           | j	        rM| j        sFt=          | j	                  dk    r| j	        dd         d:z   n| j	        }rt9          d|r d           | jk        rD| j        s=| jl        r| j        dk    rd}sn| jl        rd}snd}st9          d|s d| jm         d           t          j                    | _        |r|| _        nG| j                            d          }tt          j                    j        dd         }u|t d|u | _        t                      }v|vdz  | _        | j                            dd           | j        d| j         dz  | _        g | _        d| _        d| _        d| _        ddlm}w  |w|;|<          | _        |6| _        |7| _        d| _        d | _        | j        |'|&d| _        ddlm}x  |x            | _        	 dd"lnmo}y  |y            }zn# tV          $ r i }zY nw xY w	 t          t          j        |zp                    di                               | _K        n2# tV          $ r%}{t                              d|{           Y d}{~{nd}{~{ww xY wd| _        d| _        d | _        d | _        d| _        d| _        d| _        |5s	 |zp                    di           }|||p                    dd           | _        ||p                    dd           | _        t          ||p                    dd                    | _        | j        s| j        rYddlm}}  |}||p                    dd          ||p                    dd                    | _        | j                                         n# tV          $ r Y nw xY wd| _        |5s	 ||r||p                    d_d          nd}~|~rddlm} ddlm}  |            | _         ||~          }|r.|                                r| j                            |           | j        j        r9| j        |+pdt9          t                                dd}| j        r8	 | j                            | j                  }|r||d<   n# tV          $ r Y nw xY w| j        r
| j        |d<   | j        r
| j        |d<   | j        r
| j        |d<   | j        r
| j        |d<   | j        r
| j        |d<   | j        r
| j        |d<   | j        r
| j        |d<   	 ddlm}  |            }||d<   d|d<   n# tV          $ r Y nw xY w | j        j        d	i | t                              d|~           n"t                              d|~           d| _        n9# tV          $ r,}t                              d|           d| _        Y d}~nd}~ww xY w| j        r| j        d | j        D             }| j                                        D ]o}|p                    dd          }|r||v rd|d}| j                            |           |r/| j                            |           |                    |           pd| _        	 |zp                    di           }t          |p                    dd                    | _        n# tV          $ r Y nw xY w|zp                    di           }t7          |t                    si }|p                    ddX          | _        	 |p                    ddȦ          }t          |          }|d.k     rd.}n# t          t          f$ r d}Y nw xY w|| _        |zp                    di           }t7          |t                    si }t          |p                    dd˦                    }t9          |p                    dd                                                    dv }t          |p                    ddϦ                    }t          |p                    ddt                    }	 t          |zddi Ҧ          }n# tV          $ r i }Y nw xY wt7          |t                    r|p                    dӦ          }nd}|-	 t          |          }n# t          t          f$ r d}Y nw xY w|| _        |zp                    d`i           }t7          |t                    r|p                    dӦ          }nd}|j	 t          |          }nX# t          t          f$ rB t                              d|           t9          d|d֝t           j        צ           d}Y nw xY w	 ddlnm}  ||z          }n<# tV          $ r/ |zp                    d٦          }t7          |tB                    sg }Y nw xY w||r	 ddlnm}  || j        | j        |ۦ          }|rt          |          }n# tV          $ r d}Y nw xY w|j| j        r| j        &                    d          nd}|D ]C}t7          |t                    s|p                    da          pd&                    d          }|r||k    r|p                    di           }t7          |t                    r|p                    | j        i           }t7          |t                    r|p                    dӦ          }|	 t          |          }|dk    rt          nd# t          t          f$ rN t                              d| j        |           t9          d| j        d|dt           j        צ           Y nw xY w nE|| _        |                     |           d}d}	 t7          |zt                    r|zp                    di           ni }|p                    dd          pd}n# tV          $ r Y nw xY w|dk    r	 ddlm}  ||          }n2# tV          $ r%}t                              d|           Y d}~nd}~ww xY w|4	 ddlm	}  |            }|r|j
        |k    r|}n# tV          $ r Y nw xY w|t                              d|           ||| _        ddlm}  || j        | j        tk          | dbd          || j        |          }| j                            | j        || j        tk          | dbd          | j                   | j        s!t                              d|j
                   nKt          | j        |d||d| j        | j        tk          | dbd          || j        | j#                  | _        || _        ddlm} tk          | j        dd          }|r-||k     r't          d| j         d|dld|dld|dz   d	          tq                      | _        te          | d          r| j        r| j        d | j        D             }| j                                        D ]}|p                    dd          }|r||v rd|d}| j                            |           |rJ| j                            |           | j                            |           |                    |           te          | d          r| j        r	 | j                            | j        t9          t                                | j
        pd| j        tk          | j        dd                     n2# tV          $ r%}t                              d|           Y d}~nd}~ww xY wt+          t-          j        d          pd          | _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _         d| _!        d| _"        d| _#        d| _$        dp| _%        d| _&        d}t7          |t                    r|p                    d          }|L	 t          |          | _&        n4# t          t          f$ r t                              d|           Y nw xY w| j&        | j        r}tO          | j                  rh	 tQ          | j        | j        | j        pd          }|r|dk    r|| _&        n2# tV          $ r%}t                              d|           Y d}~nd}~ww xY w| j&        r;|r9|7| j&        |k    r+t                              d | j&        |           || _&        | j&        r)| j        s"t                              d| j&                   | j        so|rJt9          d| j        j)        dldt          |dz             d| j        j*        dld3           n#t9          d| j        j)        dld           d| _+        | ,                                 | j        }| j        | j        | j        | j#        tk          | dbd          t          | j                  | jk        | jl        tk          |d`| j                  tk          |da| j                  tk          |dbd          tk          |d_| j                  |j)        |j*        d| _-        | j#        dk    r2| j-        .                    | j        | j        | j        d           dS dS (
  ax  
        Initialize the AI Agent.

        Args:
            base_url (str): Base URL for the model API (optional)
            api_key (str): API key for authentication (optional, uses env var if not provided)
            provider (str): Provider identifier (optional; used for telemetry/routing hints)
            api_mode (str): API mode override: "chat_completions" or "codex_responses"
            model (str): Model name to use (default: "anthropic/claude-opus-4.6")
            max_iterations (int): Maximum number of tool calling iterations (default: 90)
            tool_delay (float): Delay between tool calls in seconds (default: 1.0)
            enabled_toolsets (List[str]): Only enable tools from these toolsets (optional)
            disabled_toolsets (List[str]): Disable tools from these toolsets (optional)
            save_trajectories (bool): Whether to save conversation trajectories to JSONL files (default: False)
            verbose_logging (bool): Enable verbose logging for debugging (default: False)
            quiet_mode (bool): Suppress progress output for clean CLI experience (default: False)
            ephemeral_system_prompt (str): System prompt used during agent execution but NOT saved to trajectories (optional)
            log_prefix_chars (int): Number of characters to show in log previews for tool calls/responses (default: 100)
            log_prefix (str): Prefix to add to all log messages for identification in parallel processing (default: "")
            providers_allowed (List[str]): OpenRouter providers to allow (optional)
            providers_ignored (List[str]): OpenRouter providers to ignore (optional)
            providers_order (List[str]): OpenRouter providers to try in order (optional)
            provider_sort (str): Sort providers by price/throughput/latency (optional)
            session_id (str): Pre-generated session ID for logging (optional, auto-generated if not provided)
            tool_progress_callback (callable): Callback function(tool_name, args_preview) for progress notifications
            clarify_callback (callable): Callback function(question, choices) -> str for interactive user questions.
                Provided by the platform layer (CLI or gateway). If None, the clarify tool returns an error.
            max_tokens (int): Maximum tokens for model responses (optional, uses model default if not set)
            reasoning_config (Dict): OpenRouter reasoning configuration override (e.g. {"effort": "none"} to disable thinking).
                If None, defaults to {"enabled": True, "effort": "medium"} for OpenRouter. Set to disable/customize reasoning.
            prefill_messages (List[Dict]): Messages to prepend to conversation history as prefilled context.
                Useful for injecting a few-shot example or priming the model's response style.
                Example: [{"role": "user", "content": "Hi!"}, {"role": "assistant", "content": "Hello!"}]
                NOTE: Anthropic Sonnet 4.6+ and Opus 4.6+ reject a conversation that ends on an
                assistant-role message (400 error).  For those models use structured outputs or
                output_config.format instead of a trailing-assistant prefill.
            platform (str): The interface platform the user is on (e.g. "cli", "telegram", "discord", "whatsapp").
                Used to inject platform-specific formatting hints into the system prompt.
            skip_context_files (bool): If True, skip auto-injection of SOUL.md, AGENTS.md, and .cursorrules
                into the system prompt. Use this for batch processing and data generation to avoid
                polluting trajectories with user-specific persona or project instructions.
            load_soul_identity (bool): If True, still use ~/.hermes/SOUL.md as the primary
                identity even when skip_context_files=True. Project context files from the cwd
                remain skipped.
        N r   >   codex_responsesbedrock_conversechat_completionsanthropic_messagesopenai-codexr  xaichatgpt.com/backend-api/codexapi.x.ai	anthropicapi.anthropic.comr  /
/anthropicbedrockbedrock-runtime.amazonaws.comr  r  r   )_AGGREGATOR_PROVIDERSnormalize_model_for_providercopilot-acpacp://copilot
acp+tcp://r  _transport_cache
openrouterTzopenrouter-prewarmtargetdaemonr   F5m)load_configprompt_caching	cache_ttl)r  1hinitializing)setup_loggingsetup_verbose_logging)r/   z=Verbose logging enabled (third-party library logs suppressed))ro  	run_agenttrajectory_compressorcronry  )build_anthropic_clientresolve_anthropic_tokenbuild_anthropic_bedrock_clientzbedrock-runtime\.([a-z0-9-]+)\.r   	us-east-1zaws-sdku&   🤖 AI Agent initialized with model: z& (AWS Bedrock + AnthropicBedrock SDK, r  _is_oauth_tokentimeoutz (Anthropic native)   u   🔑 Using token: r   ...	guardrailguardrail_identifierguardrail_version)guardrailIdentifierguardrailVersionstream_processing_modestreamProcessingModetracez + Guardrailsz (AWS Bedrock, )queryc                 &    i | ]\  }}||d          S r   r   r   kvs      r   
<dictcomp>z$AIAgent.__init__.<locals>.<dictcomp>  s/     % % %$(Aq1Q4% % %r   )r  r   default_queryr  r   r  r  r   openrouter.ai)build_or_headersdefault_headersapi.routermint.comapi.githubcopilot.comcopilot_default_headersapi.kimi.comrx  claude-code/0.1.0portal.qwen.ai_codex_cloudflare_headersresolve_provider_clientauto)r  	raw_codex_default_headers)r$  r  custom_API_KEY)PROVIDER_REGISTRYc                     g | ]C}t          |t                    |                    d           ,|                    d          A|DS r  r  r!   r   r   r   fs     r   r   z$AIAgent.__init__.<locals>.<listcomp>  sc     + + +&'#-a#6#6+;<55;L;L+QRQVQVW^Q_Q_+ !+ + +r   r  r  r   r  r  r%  explicit_base_urlexplicit_api_keyz
Provider 'z:' is set in config.yaml but no API key was found. Set the zM environment variable, or switch to a different provider with `hermes model`._fallback_activatedzxNo LLM provider configured. Run `hermes model` to select a provider, or run `hermes setup` for first-time configuration.claudezx-anthropic-betaz&fine-grained-tool-streaming-2025-05-14rI  
agent_initreasonsharedu   🔗 Using custom base URL: nonez	dummy-keyu   🔑 Using API key: u;   ⚠️  Warning: API key appears invalid or missing (got: '   z...')z$Failed to initialize OpenAI client: c                     g | ]C}t          |t                    |                    d           ,|                    d          A|DS r+  r,  r-  s     r   r   z$AIAgent.__init__.<locals>.<listcomp>  s^     $ $ $a&&$+,55+<+<$ABw$$ $ $r   u   🔄 Fallback model: r  u   🔄 Fallback chain (z providers):     → c              3   >   K   | ]}|d           d|d          dV  dS )r  r  r  r  Nr   r-  s     r   r   z#AIAgent.__init__.<locals>.<genexpr>-  s;      "c"caj#D#DAjM#D#D#D"c"c"c"c"c"cr   )r  r  r  c                 *    h | ]}|d          d         S r   r   )r   tools     r   	<setcomp>z#AIAgent.__init__.<locals>.<setcomp>9  s"    $U$U$U$T*%5f%=$U$U$Ur   u   🛠️  Loaded z tools: , u      ✅ Enabled toolsets: u      ❌ Disabled toolsets: u@   🛠️  No tools loaded (all tools filtered out or unavailable)c                     g | ]	\  }}||
S r   r   )r   r   	availables      r   r   z$AIAgent.__init__.<locals>.<listcomp>I  s"    ^^^_T9T]^D^^^r   u=   ⚠️  Some tools may not work due to missing requirements: u   📝 Trajectory saving enabled<   u   🔒 Ephemeral system prompt: 'z' (not saved to trajectories)znative AnthropiczAnthropic-compatible endpointzClaude via OpenRouteru   💾 Prompt caching: ENABLED (z TTL)%Y%m%d_%H%M%S   rb  sessions)parentsexist_oksession_.jsonassistant_tool
foreground)CheckpointManager)enabledmax_snapshots)r  r  r  )	TodoStoretool_loop_guardrailsz&Tool loop guardrail config ignored: %s
   memorymemory_enableduser_profile_enablednudge_interval)MemoryStorememory_char_limiti  user_char_limiti_  )rX  rY  )MemoryManager)load_memory_providercliprimary)r  r  r/   agent_contextsession_titler  r  r  r  r  r  r  )get_active_profile_nameagent_identityhermesagent_workspacezMemory provider '%s' activatedz/Memory provider '%s' not found or not availablez&Memory provider plugin init failed: %sc                     h | ]@}t          |t                    |                    d i                               d          AS r   r,  r   ts     r   r?  z#AIAgent.__init__.<locals>.<setcomp>  T     $ $ $a&&$j"%%))&11$ $ $r   r   r   )r   r   skillscreation_nudge_intervalagenttool_use_enforcementapi_max_retries   compression	thresholdg      ?rN  )true1yestarget_ratio皙?protect_last_n	auxiliarydefaultcontext_lengthu   Invalid model.context_length in config.yaml: %r — must be a plain integer (e.g. 256000, not '256K'). Falling back to auto-detection.u2   
⚠ Invalid model.context_length in config.yaml: zf
  Must be a plain integer (e.g. 256000, not '256K').
  Falling back to auto-detected context window.
)file)get_compatible_custom_providerscustom_providers)"get_custom_provider_context_length)r  r   r|  modelsu   Invalid context_length for model %r in custom_providers: %r — must be a positive integer (e.g. 256000, not '256K'). Falling back to auto-detection.u&   
⚠ Invalid context_length for model z in custom_providers: zi
  Must be a positive integer (e.g. 256000, not '256K').
  Falling back to auto-detected context window.

compressorcontextengine)load_context_enginez4Context engine load from plugins/context_engine/: %s)get_plugin_context_engineuE   Context engine '%s' not found — falling back to built-in compressorget_model_context_length)r   r  config_context_lengthr  r|  r  ry  r   r  r  zUsing context engine: %s)r  threshold_percentprotect_first_nru  summary_target_ratiosummary_model_overrider  r   r  r  r  r  MINIMUM_CONTEXT_LENGTHzModel  has a context window of $ tokens, which is below the minimum z9 required by Hermes Agent.  Choose a model with at least   zBK context, or set model.context_length in config.yaml to override.context_compressorc                     h | ]@}t          |t                    |                    d i                               d          AS r   r,  re  s     r   r?  z#AIAgent.__init__.<locals>.<setcomp>  rg  r   )r/   r  r  ry  z#Context engine on_session_start: %sTERMINAL_CWD)working_dir        unknownollama_num_ctxz'Invalid ollama_num_ctx config value: %r)r  z#Ollama num_ctx detection failed: %sz?Ollama num_ctx capped: %d -> %d (model.context_length override)zAOllama num_ctx: will request %d tokens (model max from /api/show)u   📊 Context limit: z tokens (compress at r  z% = z# tokens (auto-compression disabled)r  r  r   r  r  client_kwargsuse_prompt_cachinguse_native_cache_layoutcompressor_modelcompressor_base_urlcompressor_api_keycompressor_providercompressor_context_lengthcompressor_threshold_tokensanthropic_api_keyanthropic_base_urlis_anthropic_oauthr   (/  r   r  r  r   r  r  r  r  r  r  r  _user_id
_user_name_chat_id
_chat_name
_chat_type
_thread_id_gateway_session_key	_print_fnbackground_review_callbackr  r  r  _credential_poolr  r  r   r!   r   r   r  r  r  r  r  r  r  r  rstripr[  
startswithrw   _get_transportr   hermes_cli.model_normalizer  r  _is_azure_openai_url_is_direct_openai_url&_provider_model_requires_responses_apihasattrr  clear_is_openrouter_url_openrouter_prewarm_doneis_setsetr   ThreadrN   startr  r  r  suppress_status_outputr  r  r  r  r  r  r  r  _executing_toolsro   _tool_guardrails_tool_guardrail_halt_decision_interrupt_requested_interrupt_message_execution_thread_id _interrupt_thread_signal_pendingRLock_client_lock_pending_steerr   _pending_steer_lock_tool_worker_threads_tool_worker_threads_lock_delegate_depth_active_children_active_children_lockr  r  r  r  r  r  r  r  r  r  r  r   r  r  _force_ascii_payload_anthropic_prompt_cache_policy_use_prompt_caching_use_native_cache_layout
_cache_ttlhermes_cli.configr  r   _budget_exhausted_injected_budget_grace_calltime_last_activity_ts_last_activity_desc_current_tool_api_call_count_rate_limit_state_or_cache_hitshermes_loggingr  r  _hermes_homerS  infor   	getLoggersetLevelERROR_stream_callback_stream_needs_breakr@   _stream_context_scrubber _current_streamed_assistant_text_persist_user_message_idx_persist_user_message_override_anthropic_image_fallback_cache_anthropic_client_is_anthropic_oauthr-   agent.anthropic_adapterr  r  r  rX  r   group_bedrock_region_anthropic_api_key_anthropic_base_urlr  client_client_kwargsprintr  r   _bedrock_guardrail_configr   r  r
   _replacer	   r  agent.auxiliary_clientr  r{  hermes_cli.modelsr  r  r!  r#  r&  upperhermes_cli.authr)  api_key_env_varsr2  RuntimeErrorr   _create_openai_client_fallback_chain_fallback_index_fallback_modelr;  r1   ro  valid_tool_namessortedr4   r   nowsession_startr  strftimeuuiduuid4hexr   logs_dirmkdirsession_log_file_session_messages_memory_write_origin_memory_write_context_cached_system_prompttools.checkpoint_managerrM  _checkpoint_mgr_session_db_parent_session_id_last_flushed_db_idx_session_db_created_session_init_model_configtools.todo_toolrP  _todo_storern   from_mappingrT  &_aux_compression_context_length_config_memory_store_memory_enabled_user_profile_enabled_memory_nudge_interval_turns_since_memory_iters_since_skillr   tools.memory_toolrW  load_from_disk_memory_manageragent.memory_managerrZ  plugins.memoryr[  is_availableadd_provider	providersget_session_titlehermes_cli.profilesr`  initialize_allr   get_all_tool_schemasr   add_skill_nudge_interval_tool_use_enforcementrW  r   _api_max_retriesfloatr{   r   r   r{  r}  _config_context_length_ensure_lmstudio_runtime_loadedplugins.context_enginer  hermes_cli.pluginsr  r   r  agent.model_metadatar  update_modelrX   compression_enabledr  _context_engine_tool_namesget_tool_schemason_session_startrY   r   getenv_subdirectory_hints_user_turn_countsession_prompt_tokenssession_completion_tokenssession_total_tokenssession_api_callssession_input_tokenssession_output_tokenssession_cache_read_tokenssession_cache_write_tokenssession_reasoning_tokenssession_estimated_cost_usdsession_cost_statussession_cost_source_ollama_num_ctxrV   rW   ry  threshold_tokens_compression_warning$_check_compression_model_feasibility_primary_runtimeupdate)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  r  r  r  r  r  r  r  r  r  r  r  r  r  r  
session_dbr  r  r  credential_poolr  r  r  provider_namer  r  _load_pc_cfg_pc_cfg_ttlr  r  quiet_logger_provider_timeoutr  r  _is_bedrock_anthropicr  _region_match
_br_region_is_native_anthropiceffective_key_is_oat_load_br_cfg_gr	_gr_label_parsed_url
_clean_url_query_paramsr  effective_baser  r  r!  r#  _routed_clientrb  	_explicit	_env_hintr)  _pcfg_fb_entries_fb_resolved_fb
_fb_client	_fb_model_effective_baseheadersexisting_beta_FINE_GRAINEDkey_usedefbr   requirementsmissing_reqsprompt_previewsourcetimestamp_str
short_uuidr/   rM  rP  _load_agent_config
_agent_cfg_tlg_err
mem_configrW  _mem_provider_name_MemoryManager	_load_mem_mp_init_kwargs_str`  _profile_mpe_existing_tool_names_schema_tname_wrappedskills_config_agent_section_raw_api_retries_api_retries_compression_cfgcompression_thresholdr9  compression_target_ratiocompression_protect_last_aux_cfg_aux_context_config
_model_cfgr3  r{  _custom_providersr}  _cp_ctx_resolved_target	_cp_entry_cp_url
_cp_models_cp_model_cfg_cp_ctx_parsed_selected_engine_engine_name_ctx_cfgr  _ce_load_errr  
_candidater  _plugin_ctx_lenr  _ctx_ce_err_ollama_num_ctx_override	_detectedexc_ccs                                                                                                                                                                                      r   r   zAIAgent.__init__  s]/   Z 	
, !1 SON4S4S$!2.$'>$ ####$7!
 *.'"4"4. / 0.8@Z****b B4>x4M4MlRZR`R`RbRbl((..000hl%+&1'X3344hhh$DMM]n,,-DMM]e##-DMM##}44$(<<<-DM*DMM#)@J)N)N-DM!DMM]k))m.CH_cvHvHv0DM'DMM!((--66|DD 	/ 1DMM]i''#../ABB (%d&:OLL ( /DMM.DM	!!!! 	 	 	D			       
 }$99999$*dmTT
 	 	 	D	  !333..+,,2244??PP /+,,2244??MM / --// /
 **,, / >>J!] ?   / .DM t/00 .%++--- M\))T-D-D-F-F),3355 *$((***+)   eggg&<##6 &<#&+#!2"4 0*%:"*D'.!2
 !& ; = =KO* %*!"&04!05-%O-- .2#,>#3#3  /2ee!)2)9)9&  ! "%.^%5%5" "3!2.*+F((@% !1!2 % 0(!%&7&=2!>!> 0 6B$)! //11 	@ $"? 	EEEEEE"lnn(()92>>D"G;;{D11D|##"& 	 	 	D	 +0'"' )-	(6 )-$% >B $%
 	HGGGGGGG,//// 	L!!###KKWXXXX L
% L LL %l33<<W]KKKK !% $)  )A(B(B%
 13-
 *.&.2+
 @B, "&#(  9
SS=000________ %)MY$>!$ '_RRRRRR "	*LhnZ\ ] ]7DU]00333+
'1$)G)G
)S)S&*3'+3(+0(("&(# D  C4:  C  Cu  C  C  C  D  D  D
 (,}'C$Pd y!K,C,C,E,E!Kkrkxvx,*7'+3( ONNNNNEY+d77=+A+A+A_d()?)?xar)s)s)s&"&(# _b4:bbbccc$ _]););b)@)@]=!3D]]WYWZWZI[]]^^^]000 I&H(.VXYYM=J#[=#6#6q#9#9#9P[D -1D*IIIIII"lnn((B77;;KLL77122 Osww?R7S7S O/23I/J,/0C,D6 6D2 ww788 oQTUmQn67MNwww'' OBEg,6w?   DK"$D? ~/3/MUOOSU	|tz||Z^Zn|py|||}}} u8 u 'x00$ O!+K,@,@r,@,J,J!K!KJ% %,4[5F,G,G,M,M,O,O% % %M $+$.)6% %MM 18X$N$NM$0/@M),=M11/3/?M),,0MM&)!)(II ZGGGGGG7G7G7I7IM"344*>;OPP Z7J7L7LM"344*>;RSS ZIIIIII7N7N7P7PM"344*>>JJ Z$&98M"344 +>;KLL Z7K7M7MM"344*>=II ZPPPPPP7P7PQX7Y7YM"34 KJJJJJ$;$;M+V4:%O %O %O!!-#1#9$'(?$@$@% %M )43Di0~/ABB a~Gf a;?@_;`;`&78
 "&!4" ; ; = = C C E EI  /Y6V%V%V (1'8'8$B$B$B	!IIIIII$5$9$9)$D$DE$ F)? F,1,B1,E	( ! ! ! D! ')%nd;; ;+ ++9+ + +KK (== ;.BTBTU_B`B` ;esewewx  fA  fA ;+9*:K',#. & &C4K4K #Js7|t25''*2E2E141C1C5 5 51J	
  *503J-6-F#g,
;? 8/9/A03J4G0H0H1" 1" $5#@?PM)$<#*:7I#J#J !izOj !iGKJLgGhGhM2C$D/3 %  6  , ".!dY !d !d6?!d !d !d# # 
 #4)>FF *-   #0D "-"3"3J"C"CDDJJLLO$_oFF 	?8X\XbXhfhWoWoWqWqKqKq'++,=>>D" ',> C C H 55$ D9F6X6X6X6X 2336C 237>M"34(,,Y;;DL)--j$-HHDMO"88|dh8ii 	JO4:OOPPP IGXGGHHH,00FCCH JH$;$;HPR@R@RUXbqb\UUhrssmUUVVVV  Imu  ]B\dehfheh\i\i  |B  I  I  I  J  J  J O O O"#M!#M#MNNNO nd++ 	&$ $)$ $ $D   -- 	&.2D2DZ2P2P 	&UcUgUghoUpUp 	&$2#3D  #%D  #*41F#N#N :>:NXt3A66TX 	e 	e4'((A--)!,NbkNNR
^NNNOOOOVc$2F.G.GVVVll"c"cdNb"c"c"cccd e e e *-/
 
 

 !$: 	V$U$U$*$U$U$UD! 566J? WYTZYY$))JBWBWYYZZZ $ USdii@P6Q6QSSTTT$ WUtyyAR7S7SUUVVV 	VTUUU : 	fdo 	f577L^^8J8J8L8L^^^L fdVbddeee ! 	4$/ 	42333 ' 	c 	cJMdNjJkJknpJpJpT9#2#>FFvz  wSNaNaaabbb # 	UDO 	U, 1+1M1M+. 180S6SST_SSSTTT &\^^ 	>(DOO !.77HHM)"1"-J!.====DO &''#j0D4888 $0Q4?0Q0Q0Q Q 8:$4!%1" 59" 	?>>>>>00'2 
  
  
 &"3$%!#( "1 0$+
 +
' 	.-----$9;;	KKKKKK++--JJ 	 	 	JJJ		O$?'4NN#92>> % %D!!
  	O 	O 	ONNCXNNNNNNNN	O
 7;3 "$%*"&(##$ "# 	'^^Hb99
'1~~6F'N'N$-7^^<RTY-Z-Z*.1*..AQSU2V2V.W.W+' 84+E 8======)4*4..9Ld*S*S(27H$(O(O* * *D& &55777     $ :	,9,GQ%YZ^^J%C%C%CWY"% 34TTTTTTPPPPPP+9>+;+;D(#)$677C ?s//11 ?,99#>>>+5 ,4*./(0(9E+./@/@+A+A-6	( (  + %%&*&6&H&H&Y&Y#& !HDGL$A#, % % % $%  = D6:mL3? H8<L5= D6:mL3? H8<L5? H8<L5? H8<L54 \BFB[L)>?!SSSSSS'>'>'@'@H=EL)9:>FL):;;( ! ! ! D!;,;KKlKKK$DFXYYYY%VXjkkk/3, , , ,GNNN'+$$$$$$,  	5DJ$:$ $$ $ $ 
  /DDFF 5 5 VR00 f(<<<$.GDD
!!(+++ 5)--f555(,,V444 &("	&NN8R88M),]->->?XZ\-]-])^)^D&& 	 	 	D	
 $44.$// 	 N%3%7%78NPV%W%W"	-112CQGG/00La :& 	 	 	LLL	 ,
 &>>-<<*D11 	"! %&6&:&:;&M&M N N!"2"6"6y$"G"GHHNNPPThh#()9)=)=nd)S)S#T#T #&'7';';<Lb'Q'Q#R#R 
	z;rRRRHH 	 	 	HHH	h%% 	'"*,,/?"@"@"&*+&)*=&>&>##z* + + +&*###+6I3  ^^GR00
j$'' 	*%/^^4D%E%E""%)"!-.),-C)D)D&&z* . . .6 +	   IJ` I I I 	    *.&&&."	'IIIIII ? ?
 K K 	' 	' 	' */A B B/66 '$&!	' ").?)
(PPPPPP#E#E*!]%6$ $ $ 
 $ C-01A-B-B* ( ( (#'   (
 &-7;}L$-..s333"!2  I%i66 ! (}}Z88>BFFsKKG 7g#5#5%.]]8R%@%@
%j$77 *,6NN4:r,J,JM)->> **7*;*;<L*M*M#*#6%*25g,,+2a<<2<,< ,8,5z+B %* %* %*(.-N -1J)* )* )* ).-aVZV` -a -a  {B -a -a -a 25	)* )* )* )* )* )*%*  '=#,,-CDDD  #	8B:t8T8T\z~~i444Z\H#<<,??O<LL 	 	 	D	 <''cFFFFFF#6#6|#D#D   c c cSUabbbbbbbbc  'LLLLLL!:!:!<!<J! 6jo&E&E+5(    D  '[    '&6D#EEEEEE66
i44&<!2  O #00j.i44 1    ? O68H8MNNN&7j"7 !7%='+?i44&<' ' 'D# $7  	@?????t.0@!DD 	D111D D DdO D D.DRD D *T1D D D    03uu'4-.. 	543J 	5tzOe$ $$ $ $ 
  2CCEE 	5 	5 VR00 f(<<<$.GDD
!!(+++ 5)--f555377???(,,V444 4-.. 
	M43J 
	M	M'88O #O$5$5 6 6!]3e*#*4+BDTVW#X#X 9      M M MBGLLLLLLLLM $;	.119T$
 $
 $
  !" &'")*&$%!!"$%!%&")*&*+'()%*-'#, #)  ,0#' j$'' 	H'1~~6F'G'G$#/b'*+C'D'D$$z* b b bFH`aaaaab'DM'>OPTP]>^>^'I0T]TXT`Tfdfggg	 5Q+4D( I I IBCHHHHHHHHI  
	:&
	: )0$'===KKQ$&<   $:D  	 	KKS$  
  	|" |  ~T-D-S  ~  ~  ~lo  qF  GJ  qJ  mK  mK  ~  ~  QU  Qh  Qy  ~  ~  ~  ~        zT-D-Szzzz{{{
 %)!11333 %ZtY33!$"566"&":'+'D !(Wdj A A#*3
DM#J#J")#y""="=#*3
DM#J#J),);+.+?#!
 !
& =000!((%)%<&*&>&*&>* *      10s  +J   
JJ,J> >
K
K
AY 
YY+Ch. .
h;:h;?1s1 1
s>=s>C
A@" @"
AA@,A@?@?AAS!AS2 S2ATT ATT;AU U
AU0UAU+U+AU0V'CAZ Z
AZZAZZB/Aa; ]&A]6 ]5Aa; ]6
A^^ Aa; ^A^^A:Aa; _>A` `Aa; `
A`&`#Aa; `%A`&`&AAa; a;
Ab1b"Ab,b,Ab1e$?Af$ f$
Af1f0Af1g=.Ah, h,AiiAilAl4 l4AmmAmm6An nAnnAno/Ap  p AAqqAqqAq, q,6Ar%r$Ar%r/1As! s!As0s/As0wAw8w8AAyyAyzAA{	 {	
A{{A{{ A{4 {4
A|#{>A||A|#|)"A} }
A}}A}G	A"BH, H,
BIH6BIIBIL*BM M.BM2M1BM2N4BO O
BO>OBO9O9BO>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_SOURCEr\  )r  rz  r  model_configsystem_promptr  r  Tz5Session DB creation failed (will retry next turn): %s)r  r  create_sessionr  r  r   r   r   r  r  r  r  r   rS  rT  )r   ru  s     r   _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*c                 $   d| _         d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _	        d| _
        d| _        d| _        t          | d          r"| j        r| j                                         dS dS dS )a  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.
        
        This keeps the counter reset logic DRY and maintainable in one place
        rather than scattering it across multiple methods.
        r   r  r  r8  r  N)rB  rD  rE  r@  rA  rF  rG  rH  rC  rI  rJ  rK  r?  r  r  on_session_resetr%   s    r   reset_session_statezAIAgent.reset_session_state  s    & %&!$%!%&"%&")*&)*&*+'()%!"*-'#, #)  !" 4-.. 	743J 	7#4466666	7 	7 	7 	7r   r  c           
      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.
        r   lmstudioNr   r  )ensure_lmstudio_model_loadedr3  r  r  r  ry  r   r  r  r  zLM Studio preload skipped: %s)r  r   r  r7  r  r  r  r   r   r  r   r8  r  r   rS  r   )r   r  r  r  
target_ctx
loaded_ctxccerrs           r   r4  z'AIAgent._ensure_lmstudio_runtime_loaded  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 |||          }|dk    r1|dv r-t          |t                    r|rt	          j        dd|          }| j        }| j        }|| _        || _        |p| j        | _        || _	        t          | d          r| j                                         |r|| _        |dk    rddlm}	m}
m} |d	k    }|r|p| j        p |
            pdn
|p| j        pd}|| _        || _        |pt'          | d
d          | _         |	|| j        t+          | j        | j                            | _        |r ||          nd| _        d| _        i | _        nq|p| j        }|p| j        }||d| _        t+          | j        | j                  }|
|| j        d<   |                     t7          | j                  dd          | _        |                     || j        ||          \  | _        | _        |                                  t          | d          r| j         rddl!m"} d}	 ddl#m$}m%}  |            } ||          }n# tL          $ r d}Y nw xY w || j        | j        | j        | j        t'          | dd          |          }| j         '                    | j        || j        t'          | dd          | j        | j	                   d| _(        t          | d          r| j         r| j         nd}| j        | j        | j        | j	        t'          | dd          t7          | j                  | j        | j        |rt'          |d| j                  n| j        |rt'          |d| j                  n| j        |rt'          |dd          nd|rt'          |d| j                  n| j        |r|j)        nd|r|j*        ndd| _+        |dk    r-| j+        ,                    | j        | j        | j        d           d| _-        d| _.        |pd/                                0                                |pd/                                0                                tc          t'          | d g           pg           }rrk    rfd!|D             }|| _2        |r|d         nd| _3        ti          j5        d"||||           dS )#a]  Switch the model/provider in-place for a live agent.

        Called by the /model command handlers (CLI and gateway) after
        ``model_switch.switch_model()`` has resolved credentials and
        validated the model.  This method performs the actual runtime
        swap: rebuilding clients, updating caching flags, and refreshing
        the context compressor.

        The implementation mirrors ``_try_activate_fallback()`` for the
        client-swap logic but also updates ``_primary_runtime`` so the
        change persists across turns (unlike fallback which is
        turn-scoped).
        r   )determine_api_moder  )opencode-zenopencode-goz/v1/?$r   r  r  r  r  r  r  Nr   Fr  r  switch_modelTr5  r  r   r  r  r  r  )r  r{  r3  )r   r  r  r  r|  r  r  r  r   r  r  r  r  c                     g | ]C}|                     d           pd                                                                hvA|DS )r  r   )r   r   r  )r   entrynew_normold_norms     r   r   z(AIAgent.switch_model.<locals>.<listcomp>	  s]       IIj))/R6688>>@@S[H\\\ \\\r   z+Model switched in-place: %s (%s) -> %s (%s))6hermes_cli.providersr  r!   r   rX  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  r4  r  r7  r  r  r  r{  r   r8  r  ry  rM  rP  rQ  r2  r   r   r  r  r  r  r   r  )r   	new_modelnew_providerr  r   r  r  	old_modelold_providerr  r  r  r]  r^  rf  _sm_timeoutr  _sm_custom_providersr  r{  _sm_cfgnew_context_lengthr  fallback_chainr  r  s                           @@r   r  zAIAgent.switch_model  sk    	<;;;;;  	B)),AAH ,,, ???8S)) @ @ viX66HJ	} 
$ 1DM 4+,, 	*!''))) 	#"DL +++          $0;#> \p  VWWW8O8O8Q8QWUWw~  xU  CG  CO  xU  SUM(DL&3D#'/']74AVX\3]3]D$%;%;t74T]DJOO& & &D" J^'h}'E'E'EchD$DK"$D#3t|M%6N(*# #D 7t}djQQK&1<#I.44T())% 5  DK //%!	 0   	@ $"? 	,,... 4-.. 	43J 	EEEEEE $( ,ZZZZZZZZ%+--'F'Fw'O'O$$ , , ,'+$$$,!9!9
&-d4Ld&S&S!5" " " #00j1i44 1    &*" *17K)L)LrQUQhrd%%nrZtY33!$"566"&":'+'DEH XWdj A A AdjNQ#d73
DM#J#J#JW[WdAD"L'#y""="="="NQ#d73
DM#J#J#JW[Wd?B)I););CF+M3+?+?A!
 !
  +++!((%)%<&*&>&*&>* *    $)   !&B--//5577 &B--//5577gd,=rBBHbII 	 	X%9%9    #1  N  .4BL~a009|Y	
 	
 	
 	
 	
s   2I II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)r  r  r   r   )r   r   r   r/  s       r   _safe_printzAIAgent._safe_print	  sU    	(5BB$ 	 	 	DD	s    //forcer  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.
        r  FN_mute_post_response)r   _has_stream_consumersr  r  )r   r  r   r   s       r   _vprintzAIAgent._vprint	  s    & 41599 	F 	'<eDD 	F 	3355 	d>S 	F$)&)))))r   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.
        NTr   F)r  r   r   r   r   AttributeErrorr   r   )r   r   s     r   _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.
        r  r   r\  )r  r  r   r%   s    r    _should_emit_quiet_tool_messagesz(AIAgent._should_emit_quiet_tool_messages	  s5     O 7//7j"--6	
r   messagec                    	 |                      | 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_statusexc_infoNr  r  r   r  rS  r   r   r  s     r   _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  Nr  r  s     r   _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r  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   r  u   ⚠ Auxiliary z	 failed: )	_summarize_api_errorr   r   	__class__r'   r   r   r  r  )r   r  r  details       r   _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  r   r  r   r  r  )r  r  r   r  r  )r   r%   s    r   _current_main_runtimezAIAgent._current_main_runtime
  sw     T7B//52j"55;j"55;tY339rj"55;
 
 	
r   c                 2   | j         sdS 	 ddlm}m} ddlm}m}  |d|                                           \  }}	  |d          \  }}}}}n# t          $ r d}Y nw xY w||s:d}	|	| _	        | 
                    |	           t                              d	           dS t          t          |d
d                    }
t          t          |dd                    } |||
|t          | dd          t          | dd                    }|r'||k     r!t          d| d|dd|dd|dz   d	          | j        j        }||k     r|}|}|| j        _        | j        j        }|r||z  | j        _        |rt)          ||z  dz            nd}t          | dd          pd}t          | dd          pd}|r|dk    r|nd}|s.	 ddlm}  ||
          j        p|
}n# t          $ r |
pd}Y nw xY w|r| d| dn|}| d| d}d| d|dd | d!|dd"|dd#|dd$|d%}	|	| _	        | 
                    |	           t                              d&||||           dS dS # t          $ r  t          $ r&}t                              d'|           Y d}~dS d}~ww xY w)(a  Warn at session start if the auxiliary compression model's context
        window is smaller than the main model's compression threshold.

        When the auxiliary model cannot fit the content that needs summarising,
        compression will either fail outright (the LLM call errors) or produce
        a severely truncated summary.

        Called during ``__init__`` so CLI users see the warning immediately
        (via ``_vprint``).  The gateway sets ``status_callback`` *after*
        construction, so ``_replay_compression_warning()`` re-sends the
        stored warning through the callback on the first
        ``run_conversation()`` call.
        Nr   )_resolve_task_provider_modelget_text_auxiliary_client)r  r  rn  )main_runtimer   u   ⚠ No auxiliary LLM provider configured — context compression will drop middle turns without a summary. Run `hermes setup` or set OPENROUTER_API_KEY.uL   No auxiliary LLM provider for compression — summaries will be unavailable.r   r  r  r  )r   r  r  r  zAuxiliary compression model r  rI  r  zE required by Hermes Agent.  Choose a compression model with at least r  zK context (set auxiliary.compression.model in config.yaml), or set auxiliary.compression.context_length to override the detected value if it is wrong.r  rP  r  rB  r$  )r   r  r  u   ⚠ Compression model z context is z tokens, but the main model z's compression threshold was z2 tokens. Auto-lowered this session's threshold to u    tokens so compression can run.
  To make this permanent, edit config.yaml — either:
  1. Use a larger compression model:
       auxiliary:
         compression:
           model: <model-with-z[+-context>
  2. Lower the compression threshold:
       compression:
         threshold: 0.02du   Auxiliary compression model %s has %d token context, below the main model's compression threshold of %d tokens — auto-lowered session threshold to %d to keep compression working.z4Compression feasibility check failed (non-fatal): %s)r9  r  r  r  r7  r  r  r  r   rN  r  rS  rT  r   r   r   r  rM  ry  r  r   urllib.parser   hostnamer   )r   r  r  r  r  r  	aux_model_aux_cfg_providerrb  r,  aux_base_urlaux_api_keyaux_contextro  old_thresholdnew_thresholdmain_ctxsafe_pct_main_model_main_provider_aux_provider_labelr   _main_label
_aux_labelr  s                            r   rO  z,AIAgent._check_compression_model_feasibility)
  s    ' 	FU	              
 !: 9!7799! ! !FI'0L0L]0[0[-!1aAA ' ' '$&!!!'~Y~D 
 -0)!!#&&&5   wvz2>>??Lgfi<<==K22%#&-d4\^b&c&c z266  K  
{-CCC 69 6 6!,L6 65M6 6 .5	6 6 6	 	 	 /@IY&& !* +;H'8  2A %0 += CKR3h 6#=>>>PR &dGR88?C!(z2!>!>!D" )->&-H-H &% $
 + EE999999$H\22;K| ,+ % E E E.:.Df+++E &%{77n7777$ 
 !*CC-@CCC
<Z < <"B< <"< < %1< <
 %I< < 6CR< < .6;< <   -0)!!#&&&0 !!	 	 	 	 	E '&X  	 	 	  	 	 	LLF        	sf   2I A I AI A?I  D I G I G+(I *G++A-I J0JJc                     t          | dd          }|r1| j        r,	 |                     d|           dS # t          $ r Y dS w xY wdS dS )u  Re-send the compression warning through ``status_callback``.

        During ``__init__`` the gateway's ``status_callback`` is not yet
        wired, so ``_emit_status`` only reaches ``_vprint`` (CLI).  This
        method is called once at the start of the first
        ``run_conversation()`` — by then the gateway has set the callback,
        so every platform (Telegram, Discord, Slack, etc.) receives the
        warning.
        rN  Nr  )r   r  r   )r   r,  s     r   _replay_compression_warningz#AIAgent._replay_compression_warning
  s     d2D99 	4' 	$$[#66666   	 	 	 	s   4 
AA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  r   r  zapi.openai.com)rx   r   )r   r   r  s      r   r  zAIAgent._is_direct_openai_url
  s[    (22HHt%92>> BS/44C CH +++r   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  r   zopenai.azure.com)r   r  r   )r   r   urls      r   r  zAIAgent._is_azure_openai_url
  sE     h--%%''CC$ 1266<"C!S((r   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_TIMEOUT      @)r-   r  r  r2  r   r=  )r   cfgs     r   _resolved_api_call_timeoutz"AIAgent._resolved_api_call_timeout
  s<     +4=$*EE?JRY3V<<===r   c                     t          | j        | j                  }||dfS t          j        d          }|t          |          dfS dS )a9  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. 300.0s default

        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)     r@T)r.   r  r  r   r=  r2  )r   r  env_timeouts      r   %_resolved_api_call_stale_timeout_basez-AIAgent._resolved_api_call_stale_timeout_base  sT     )
CC?:i ?@@"%%u,,{r   r'  c                 <   |                                  \  }}t          | dd          p| j        pd}|r |rt          |          rt	          d          S t          d |D                       dz  }|dk    rt          |d          S |d	k    rt          |d
          S |S )z@Compute the effective non-stream stale timeout for this request.r  Nr   infc              3   N   K   | ] }t          t          |                    V  !d S r   r   r   r   r  s     r   r   z<AIAgent._compute_non_stream_stale_timeout.<locals>.<genexpr>.  s.      77SVV777777r      順 g     @P  g      |@)r  r   r   rV   r2  sumr   )r   r'  
stale_baseuses_implicit_defaultr   
est_tokenss         r   !_compute_non_stream_stale_timeoutz)AIAgent._compute_non_stream_stale_timeout'  s    ,0,V,V,X,X)
)4d33Jt}J  	 X 	 2CH2M2M 	 <<77h777771<
z5)))z5)))r   c                 ,    t          | j        d          S )z1Return True when the base URL targets OpenRouter.r  rw   r  r%   s    r   r  zAIAgent._is_openrouter_url5  s    $T%9?KKKr   r  c                   ||n| j         pd}||n| j        pd}||n| j        pd}||n| j        pd}|                                }	|                                }
d|	v }t          |d          }|dk    }|o|dk    pt          |          dk    }|rdS |r|rd	S |r|rdS |r*|
d
v }t          |d          pt          |d          }|s|rdS d|	v }|
dv }|r|rd	S dS )u  Decide whether to apply Anthropic prompt caching and which layout to use.

        Returns ``(should_cache, use_native_layout)``:
          * ``should_cache`` — inject ``cache_control`` breakpoints for this
            request (applies to OpenRouter Claude, native Anthropic, and
            third-party gateways that speak the native Anthropic protocol).
          * ``use_native_layout`` — place markers on the *inner* content
            blocks (native Anthropic accepts and requires this layout);
            when False markers go on the message envelope (OpenRouter and
            OpenAI-wire proxies expect the looser layout).

        Third-party providers using the native Anthropic transport
        (``api_mode == 'anthropic_messages'`` + Claude-named model) get
        caching with the native layout so they benefit from the same
        cost reduction as direct Anthropic callers, provided their
        gateway implements the Anthropic cache_control contract
        (MiniMax, Zhipu GLM, LiteLLM's Anthropic proxy mode all do).

        Qwen / Alibaba-family models on OpenCode, OpenCode Go, and direct
        Alibaba (DashScope) also honour Anthropic-style ``cache_control``
        markers on OpenAI-wire chat completions. Upstream pi-mono #3392 /
        pi #3393 documented this for opencode-go Qwen. Without markers
        these providers serve zero cache hits, re-billing the full prompt
        on every turn.
        Nr   r3  r  r  r  r  )TTTF>   minimax
minimax-cnzapi.minimax.iozapi.minimaxi.comqwen>   alibabaopencoder  r  FF)r  r   r  r  r  rw   rx   )r   r  r   r  r  eff_providereff_base_urleff_api_mode	eff_modelmodel_lowerprovider_lower	is_claudeis_openrouteris_anthropic_wireis_native_anthropicis_minimax_provideris_minimax_hostmodel_is_qwenprovider_is_alibaba_familys                      r   r  z&AIAgent._anthropic_prompt_cache_policy9  s   B %-$8dmRPR#+#7xxdm>Qr#+#7xxdm>Qr#/UUTZFB	oo''%++--+	-lOLL(,@@ h,f0A,0O0OSf0f 	
  	: 	Y 	; 	 	:  	""04M"M%l4DEE K(7IJJ  # "o "!z +-%3 8
 &
" & 	- 	 ;|r   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  r   rQ  zgpt-5)r  rsplitr  )r  ms     r   _model_requires_responses_apiz%AIAgent._model_requires_responses_api  sD     KKMM!88a  $A||G$$$r   r  c                    |pd                                                                 }|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.r   copilotr   )!_should_use_copilot_responses_api)r   r  r  rE  r   r  rB  )r  r  normalized_providerrE  s       r   r  z.AIAgent._provider_model_requires_responses_api  s      (~24466<<>>)++OOOOOO88???     44U;;;s   A 
AAc                 b    |                                  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  s     r   _max_tokens_paramzAIAgent._max_tokens_param  sA     %%'' 	44+D+D+F+F 	4+U33e$$r   r)  c                 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      r   _has_content_after_think_blockz&AIAgent._has_content_after_think_block  s>      	5 **733 GMMOO$$$r   c                    |sdS t          j        dd|t           j        t           j        z            }t          j        dd|t           j        t           j        z            }t          j        dd|t           j        t           j        z            }t          j        dd|t           j        t           j        z            }t          j        dd|t           j        t           j        z            }dD ]8}t          j        d	| d
| dd|t           j        t           j        z            }9t          j        dd|t           j        t           j        z            }t          j        dd|t           j        t           j        z            }t          j        dd|t           j                  }t          j        dd|t           j                  }|S )u  Remove reasoning/thinking blocks from content, returning only visible text.

        Handles four cases:
          1. Closed tag pairs (``<think>…</think>``) — the common path when
             the provider emits complete reasoning blocks.
          2. Unterminated open tag at a block boundary (start of text or
             after a newline) — e.g. MiniMax M2.7 / NIM endpoints where the
             closing tag is dropped.  Everything from the open tag to end
             of string is stripped.  The block-boundary check mirrors
             ``gateway/stream_consumer.py``'s filter so models that mention
             ``<think>`` in prose aren't over-stripped.
          3. Stray orphan open/close tags that slip through.
          4. Tag variants: ``<think>``, ``<thinking>``, ``<reasoning>``,
             ``<REASONING_SCRATCHPAD>``, ``<thought>`` (Gemma 4), all
             case-insensitive.

        Additionally strips standalone tool-call XML blocks that some open
        models (notably Gemma variants on OpenRouter) emit inside assistant
        content instead of via the structured ``tool_calls`` field:
          * ``<tool_call>…</tool_call>``
          * ``<tool_calls>…</tool_calls>``
          * ``<tool_result>…</tool_result>``
          * ``<function_call>…</function_call>``
          * ``<function_calls>…</function_calls>``
          * ``<function name="…">…</function>`` (Gemma style)
        Ported from openclaw/openclaw#67318. The ``<function>`` variant is
        boundary-gated (only strips when the tag sits at start-of-line or
        after punctuation and carries a ``name="..."`` attribute) so prose
        mentions like "Use <function> in JavaScript" are preserved.
        r   z<think>.*?</think>flagsz<thinking>.*?</thinking>z<reasoning>.*?</reasoning>z0<REASONING_SCRATCHPAD>.*?</REASONING_SCRATCHPAD>z<thought>.*?</thought>)r   r   tool_resultfunction_callfunction_calls<z\b[^>]*>.*?</>zd(?:(?<=^)|(?<=[\n\r.!?:]))[ \t]*<function\b[^>]*\bname\s*=[^>]*>(?:(?:(?!</function>).)*)</function>zS(?:^|\n)[ \t]*<(?:think|thinking|reasoning|thought|REASONING_SCRATCHPAD)\b[^>]*>.*$z@</?(?:think|thinking|reasoning|thought|REASONING_SCRATCHPAD)>\s*zP</(?:tool_call|tool_calls|tool_result|function_call|function_calls|function)>\s*)rX  r  DOTALL
IGNORECASE)r   r)  _tc_names      r   rK  zAIAgent._strip_think_blocks  s   >  	2 &.G29r}C\]]]&4b'UWUbIbccc&6G29WYWdKdeee&LbRYacajmomzaz{{{&2BrySUS`G`aaa= 	 	Hf7X77H777i"-/	  GG &4 )bm+
 
 
 &b)bm+	
 
 
 &O-	
 
 
 &_-	
 
 
 r   c                 |    | sdS |                                  }|sdS |                    d          rdS |d         dv S )zCHeuristic: does visible assistant text look intentionally finished?Fz```TrQ  u$   .!?:)"']}。！？：）】」』》)r  r[  )r)  strippeds     r   _has_natural_response_endingz$AIAgent._has_natural_response_ending7  sX      	5>>## 	5U## 	4|FFFr   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.r   glmzaiFollamaz:11434T)r  r  r  r  r   r   rV   )r   r5  r6  s      r   _is_ollama_glm_backendzAIAgent._is_ollama_glm_backendC  s    z'R..00--24466##%(?(?5t+++x4;O/O/O4DMF&7&F&FGGGr   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.stopr  Fc              3   r   K   | ]2}t          |t                    o|                    d           dk    V  3dS r+  r>  Nr,  r   r,  s     r   r   z:AIAgent._should_treat_stop_as_truncated.<locals>.<genexpr>X  sT       
 
 sD!!?cggfoo&?
 
 
 
 
 
r   Nr   r)  r9  z\s)r  r`  r   r   r!   r   rK  r   r   rX  r   r[  )r   ra  assistant_messager'  r)  visible_texts         r   _should_treat_stop_as_truncatedz'AIAgent._should_treat_stop_as_truncatedM  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Br   user_messageassistant_contentc                 F  
 t          d |D                       rdS |                     |pd                                                                          

sdS t	          
          dk    rdS t          t          j        d
                    }|sdS d}d}|pd                                                                t          fd|D                       pd	v pd
v }t          
fd|D                       }t          
fd|D                       }	|s|	o|S )zNDetect a planning/ack message that should continue instead of ending the turn.c              3   r   K   | ]2}t          |t                    o|                    d           dk    V  3dS re  r,  rf  s     r   r   z=AIAgent._looks_like_codex_intermediate_ack.<locals>.<genexpr>s  s@      WWsz#t$$BF)BWWWWWWr   Fr   i  u@   \b(i['’]ll|i will|let me|i can do that|i can help with that)\b)z	look intozlook atinspectscancheckanalyzreviewexplorereadopenruntestfixr   r   findwalkthroughzreport back	summarize)	directoryzcurrent directoryzcurrent dirr	  repo
repositorycodebaseprojectfolder
filesystemz	file treefilesr  c              3       K   | ]}|v V  	d S r   r   )r   marker	user_texts     r   r   z=AIAgent._looks_like_codex_intermediate_ack.<locals>.<genexpr>  s(      DD)#DDDDDDr   z~/r  c              3       K   | ]}|v V  	d S r   r   r   r  assistant_texts     r   r   z=AIAgent._looks_like_codex_intermediate_ack.<locals>.<genexpr>  s(      '^'^V.(@'^'^'^'^'^'^r   c              3       K   | ]}|v V  	d S r   r   r  s     r   r   z=AIAgent._looks_like_codex_intermediate_ack.<locals>.<genexpr>  s9       *
 *
)/Fn$*
 *
 *
 *
 *
 *
r   )r   rK  r   r  r   r   rX  r   )r   rj  rk  r'  has_future_ackaction_markersworkspace_markersuser_targets_workspaceassistant_mentions_actionassistant_targets_workspacer  r  s             @@r   "_looks_like_codex_intermediate_ackz*AIAgent._looks_like_codex_intermediate_ackl  s    WWhWWWWW 	5112C2IrJJPPRRXXZZ 	5~%%5IY[ijj
 
  	5
*
  "'R..006688	DDDD2CDDDDD  y  i 	
 %('^'^'^'^~'^'^'^$^$^!&) *
 *
 *
 *
3D*
 *
 *
 '
 '
# 'E*EdKddr   c                 ~   g }t          |d          r!|j        r|                    |j                   t          |d          r*|j        r#|j        |vr|                    |j                   t          |d          r|j        r|j        D ]}t          |t                    ro|                    d          p>|                    d          p)|                    d          p|                    d          }|r||vr|                    |           t          |dd          }|st          |t                    rk|rid	}|D ]d}t          j        t          j        z  }t          j        |||
          D ]1}	|	                                }
|
r|
|vr|                    |
           2e|rd                    |          S dS )ay  
        Extract reasoning/thinking content from an assistant message.
        
        OpenRouter and various providers can return reasoning in multiple formats:
        1. message.reasoning - Direct reasoning field (DeepSeek, Qwen, etc.)
        2. message.reasoning_content - Alternative field (Moonshot AI, Novita, etc.)
        3. message.reasoning_details - Array of {type, summary, ...} objects (OpenRouter unified)
        
        Args:
            assistant_message: The assistant message object from the API response
            
        Returns:
            Combined reasoning text, or None if no reasoning found
        	reasoningreasoning_contentreasoning_detailssummarythinkingr)  r  N)<think>(.*?)</think>z<thinking>(.*?)</thinking>z<thought>(.*?)</thought>z<reasoning>(.*?)</reasoning>z2<REASONING_SCRATCHPAD>(.*?)</REASONING_SCRATCHPAD>rO  

)r  r  r   r  r  r!   r   r   r   r   rX  rV  rW  findallr   r;  )r   rg  reasoning_partsr  r  r)  inline_patternspatternrP  blockrL  s              r   _extract_reasoningzAIAgent._extract_reasoning  s"     $k22 	@7H7R 	@""#4#>??? $&9:: 	L?P?b 	L 2/II&&'8'JKKK $&9:: 	8?P?b 	8+= 
8 
8fd++ 	8 

9-- .!::j11.!::i00. "::f--	   87/#A#A'..w777
 +Y== 	8:gs#;#; 	8 	8O + 8 8	BM1ZFFF 8 8E#kkmmG 87/#A#A'..w7778  	0;;///tr   task_idc                    	 t          |          r | j        rt          j        d| d           nt	          |           n8# t
          $ r+}| j        rt          j        d| d|            Y d}~nd}~ww xY w	 t          |           dS # t
          $ r2}| j        r t          j        d| d|            Y d}~dS Y d}~dS d}~ww xY w)aH  Clean up VM and browser resources for a given task.

        Skips ``cleanup_vm`` when the active terminal environment is marked
        persistent (``persistent_filesystem=True``) so that long-lived sandbox
        containers survive between turns. The idle reaper in
        ``terminal_tool._cleanup_inactive_envs`` still tears them down once
        ``terminal.lifetime_seconds`` is exceeded. Non-persistent backends are
        torn down per-turn as before to prevent resource leakage (the original
        intent of this hook for the Morph backend, see commit fbd3a2fd).
        z0Skipping per-turn cleanup_vm for persistent env z; idle reaper will handle it.zFailed to cleanup VM for task : Nz#Failed to cleanup browser for task )r7   r  r   r   r5   r   rT  r?   )r   r  ru  s      r   _cleanup_task_resourceszAIAgent._cleanup_task_resources  sU   	Q )) $' M77 7 7 7  
 7### 	Q 	Q 	Q# Q O O OA O OPPP	Q	VG$$$$$ 	V 	V 	V# V Tg T TQR T TUUUUUUUUUV V V V V V	Vs/   >A 
A6!A11A6:B 
C!CCu  Review the conversation above and consider saving to memory if appropriate.

Focus on:
1. Has the user revealed things about themselves — their persona, desires, preferences, or personal details worth remembering?
2. Has the user expressed expectations about how you should behave, their work style, or ways they want you to operate?

If something stands out, save it using the memory tool. If nothing is worth saving, just say 'Nothing to save.' and stop.u  Review the conversation above and update the skill library. Be ACTIVE — most sessions produce at least one skill update, even if small. A pass that does nothing is a missed learning opportunity, not a neutral outcome.

Target shape of the library: CLASS-LEVEL skills, each with a rich SKILL.md and a `references/` directory for session-specific detail. Not a long flat list of narrow one-session-one-skill entries. This shapes HOW you update, not WHETHER you update.

Signals to look for (any one of these warrants action):
  • User corrected your style, tone, format, legibility, or verbosity. Frustration signals like 'stop doing X', 'this is too verbose', 'don't format like this', 'why are you explaining', 'just give me the answer', 'you always do Y and I hate it', or an explicit 'remember this' are FIRST-CLASS skill signals, not just memory signals. Update the relevant skill(s) to embed the preference so the next session starts already knowing.
  • User corrected your workflow, approach, or sequence of steps. Encode the correction as a pitfall or explicit step in the skill that governs that class of task.
  • Non-trivial technique, fix, workaround, debugging path, or tool-usage pattern emerged that a future session would benefit from. Capture it.
  • A skill that got loaded or consulted this session turned out to be wrong, missing a step, or outdated. Patch it NOW.

Preference order — prefer the earliest action that fits, but do pick one when a signal above fired:
  1. UPDATE A CURRENTLY-LOADED SKILL. Look back through the conversation for skills the user loaded via /skill-name or you read via skill_view. If any of them covers the territory of the new learning, PATCH that one first. It is the skill that was in play, so it's the right one to extend.
  2. UPDATE AN EXISTING UMBRELLA (via skills_list + skill_view). If no loaded skill fits but an existing class-level skill does, patch it. Add a subsection, a pitfall, or broaden a trigger.
  3. ADD A SUPPORT FILE under an existing umbrella. Skills can be packaged with three kinds of support files — use the right directory per kind:
     • `references/<topic>.md` — session-specific detail (error transcripts, reproduction recipes, provider quirks) AND condensed knowledge banks: quoted research, API docs, external authoritative excerpts, or domain notes you found while working on the problem. Write it concise and for the value of the task, not as a full mirror of upstream docs.
     • `templates/<name>.<ext>` — starter files meant to be copied and modified (boilerplate configs, scaffolding, a known-good example the agent can `reproduce with modifications`).
     • `scripts/<name>.<ext>` — statically re-runnable actions the skill can invoke directly (verification scripts, fixture generators, deterministic probes, anything the agent should run rather than hand-type each time).
     Add support files via skill_manage action=write_file with file_path starting 'references/', 'templates/', or 'scripts/'. The umbrella's SKILL.md should gain a one-line pointer to any new support file so future agents know it exists.
  4. CREATE A NEW CLASS-LEVEL UMBRELLA SKILL when no existing skill covers the class. The name MUST be at the class level. The name MUST NOT be a specific PR number, error string, feature codename, library-alone name, or 'fix-X / debug-Y / audit-Z-today' session artifact. If the proposed name only makes sense for today's task, it's wrong — fall back to (1), (2), or (3).

User-preference embedding (important): when the user expressed a style/format/workflow preference, the update belongs in the SKILL.md body, not just in memory. Memory captures 'who the user is and what the current situation and state of your operations are'; skills capture 'how to do this class of task for this user'. When they complain about how you handled a task, the skill that governs that task needs to carry the lesson.

If you notice two existing skills that overlap, note it in your reply — the background curator handles consolidation at scale.

'Nothing to save.' is a real option but should NOT be the default. If the session ran smoothly with no corrections and produced no new technique, just say 'Nothing to save.' and stop. Otherwise, act.u  Review the conversation above and update two things:

**Memory**: who the user is. Did the user reveal persona, desires, preferences, personal details, or expectations about how you should behave? Save facts about the user and durable preferences with the memory tool.

**Skills**: how to do this class of task. Be ACTIVE — most sessions produce at least one skill update. A pass that does nothing is a missed learning opportunity, not a neutral outcome.

Target shape of the skill library: CLASS-LEVEL skills with a rich SKILL.md and a `references/` directory for session-specific detail. Not a long flat list of narrow one-session-one-skill entries.

Signals that warrant a skill update (any one is enough):
  • User corrected your style, tone, format, legibility, verbosity, or approach. Frustration is a FIRST-CLASS skill signal, not just a memory signal. 'stop doing X', 'don't format like this', 'I hate when you Y' — embed the lesson in the skill that governs that task so the next session starts fixed.
  • Non-trivial technique, fix, workaround, or debugging path emerged.
  • A skill that was loaded or consulted turned out wrong, missing, or outdated — patch it now.

Preference order for skills — pick the earliest that fits:
  1. UPDATE A CURRENTLY-LOADED SKILL. Check what skills were loaded via /skill-name or skill_view in the conversation. If one of them covers the learning, PATCH it first. It was in play; it's the right place.
  2. UPDATE AN EXISTING UMBRELLA (skills_list + skill_view to find the right one). Patch it.
  3. ADD A SUPPORT FILE under an existing umbrella via skill_manage action=write_file. Three kinds: `references/<topic>.md` for session-specific detail OR condensed knowledge banks (quoted research, API docs excerpts, domain notes) written concise and task-focused; `templates/<name>.<ext>` for starter files meant to be copied and modified; `scripts/<name>.<ext>` for statically re-runnable actions (verification, fixture generators, probes). Add a one-line pointer in SKILL.md so future agents find them.
  4. CREATE A NEW CLASS-LEVEL UMBRELLA when nothing exists. Name at the class level — NOT a PR number, error string, codename, library-alone name, or 'fix-X / debug-Y' session artifact. If the name only fits today's task, fall back to (1), (2), or (3).

User-preference embedding: when the user complains about how you handled a task, update the skill that governs that task — memory alone isn't enough. Memory says 'who the user is and what the current situation and state of your operations are'; skills say 'how to do this class of task for this user'. Both should carry user-preference lessons when relevant.

If you notice overlapping existing skills, mention it — the background curator handles consolidation.

Act on whichever of the two dimensions has real signal. If genuinely nothing stands out on either, say 'Nothing to save.' and stop — but don't reach for that conclusion as a default.review_messagesprior_snapshotc                 >   t                      }t                      }|pg D ]}t          |t                    r|                    d          dk    r1|                    d          }|r|                    |           ^|                    d          }t          |t
                    r|                    |           g }| pg D ]V}t          |t                    r|                    d          dk    r2|                    d          }|r||v rN|s/|                    d          }	t          |	t
                    r|	|v r	 t          j        |                    dd                    }
n# t          j        t          f$ r Y w xY wt          |
t                    r|
                    d          s|
                    dd          }|
                    d	d          }d
|
                                v r|                    |           Id|
                                v r|                    |           vd|
                                v s|rBd|
                                v r,|dk    rdn	|dk    rdn|}|                    | d           d|v r,|dk    rdn	|dk    rdn|}|                    | d            d|
                                v sd|
                                v r*|dk    rdn	|dk    rdn|}|                    | d           X|S )aS  Build the human-facing action summary for a background review pass.

        Walks the review agent's session messages and collects "successful tool
        action" descriptions to surface to the user (e.g. "Memory updated").
        Tool messages already present in ``prior_snapshot`` are skipped so we
        don't re-surface stale results from the prior conversation that the
        review agent inherited via ``conversation_history`` (issue #14944).

        Matching is by ``tool_call_id`` when available, with a content-equality
        fallback for tool messages that lack one.
        r+  r>  tool_call_idr)  rE  successr  r   r  createdupdatedaddedr.  rS  MemoryuserzUser profilez updatedzEntry addedremovedreplaced)r  r!   r   r   r.  r   r   r   rV  rW  r  r   )r  r  existing_tool_call_idsexisting_tool_contentspriortcidr)  actionsr,  content_strr   r  r  labels                 r   $_summarize_background_review_actionsz,AIAgent._summarize_background_review_actions  sk     "%!$#)r 		8 		8EeT** eii.?.?6.I.I99^,,D 8&**40000))I..gs++ 8*..w777"(b 	3 	3Cc4(( CGGFOOv,E,E77>**D  666 !ggi00k3// KCY4Y4Yz#'')T":":;;()4   dD)) )1D1D hhy"--GXXh++FGMMOO++w''''gmmoo--w''''GMMOO+++5GMMOO;S;S$*h$6$6fX^N^N^NNdj%1112222'))$*h$6$6fX^N^N^NNdj%1112222gmmoo--w}}1N1N$*h$6$6fX^N^N^NNdj%111222s   (E..FFmessages_snapshotreview_memoryreview_skillsc                      ddl }|r
|r j        n|r j        n j         fd} |j        |dd          }|                                 dS )a  Spawn a background thread to review the conversation for memory/skill saves.

        Creates a full AIAgent fork with the same model, tools, and context as the
        main session. The review prompt is appended as the next user turn in the
        forked conversation. Writes directly to the shared memory/skill stores.
        Never modifies the main conversation history or produces user-visible output.
        r   Nc                     dd l } d }	 t          |           n# t          $ r Y nw xY wd }	 t          t          j        d          5 }|                     |          5  |                     |          5                                  }t          j
        ddj        j        |                    d          pd |                    d          pd |                    d          pd t          d	d           j        d
dg          }d|_        d|_        j        |_        j        |_        j        |_        d|_        d|_        |                    
	           d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y                       t          |dg           	          }|rnd                    t4                              |                    }                    d|            j        }|r 	  |d|            n# t          $ r Y nw xY wnH# t          $ r;}t<                              d|                                d|           Y d }~nd }~ww xY w|L	 |!                                 n# t          $ r Y nw xY w	 |"                                 n# t          $ r Y nw xY w	 t          d            d S # t          $ r Y d S w xY w# |L	 |!                                 n# t          $ r Y nw xY w	 |"                                 n# t          $ r Y nw xY w	 t          d            w # t          $ r Y w w xY wxY w)Nr   c                 >    t                               d| |           dS )Nz8Background review auto-denied dangerous command: %s (%s)deny)rS  rT  )r  descriptionr   s      r   _bg_review_auto_denyzSAIAgent._spawn_background_review.<locals>._run_review.<locals>._bg_review_auto_deny  s'    N[   vr   wr   Tr  r   r  r  rS  rh  )r  r  r  r  r  r  r   r  rS  r  r  background_review)rj  conversation_historyr  u    · u      💾 Self-improvement review: u   💾 Self-improvement review: z)Background memory/skill review failed: %szbackground review)#
contextlib_set_approval_callbackr   ru  r   devnullredirect_stdoutredirect_stderrr  r  r  r  r  r   r   r  r  r  r  r  r  r  r/  run_conversationr  r;  r   fromkeysr  r  rS  rT  r  shutdown_memory_providerclose)r  r  review_agent_devnull_parent_runtimer  r  _bg_cbru  r  promptr   s            r   _run_reviewz5AIAgent._spawn_background_review.<locals>._run_review  s9     &';<<<<   LZ"*c** $h//99$ $//99$ $ '+&@&@&B&BO#*"j'(#'!%!%!0!4!4Z!@!@!HD!0!4!4Z!@!@!HD / 3 3I > > F$(/6H$(O(O*./*2H)=$ $ $L 9LL59LL6151CL.373GL09=9SL6:;L79:L6 11%+-> 2   C$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $X CCL*=rBB% 
  !$kk$--*@*@AAG$$D7DD   "<F !!"F J J J     ) ! ! ! D!  E E EJANNN,,-@!DDDDDDDDE  +$==????$   $**,,,,$   *400000    DD  +$==????$   $**,,,,$   *40000    Dso   
''H8 FF 3C*E)F )E--F 0E-1F 4F F	FF	FH8 FH8 FA8H8 H' &H8 '
H41H8 3H44H8 7K1 8
I=1I83K1 8I==K1 J 
J%$J%)J> >
K
KK   
K.-K.1M!5L
	M!

LM!LM!L0/M!0
L=:M!<L==M!MM!
MM!MM!Tz	bg-reviewr  )r   _COMBINED_REVIEW_PROMPT_MEMORY_REVIEW_PROMPT_SKILL_REVIEW_PROMPTr  r  )r   r  r  r  r   r  rf  r  s   ``     @r   _spawn_background_reviewz AIAgent._spawn_background_review  s     	  	/] 	/1FF 	//FF.Fl	 l	 l	 l	 l	 l	 l	\ IK;OOO						r   )write_originexecution_contextr  r  r  r  r  c                   |pt          | dd          |pt          | dd          | j        pd| j        pd| j        pt          j                            dd          dd	}|r||d
<   |r||d<   d |                                D             S )z?Build provenance metadata for external memory-provider mirrors.r  rK  r  rL  r   r  r\  rS  )r  r  r  r  r  r  r  r  c                 "    i | ]\  }}|d v	||S )r  r   r  s      r   r  z8AIAgent._build_memory_write_metadata.<locals>.<dictcomp>  s(    IIIAQj5H5H15H5H5Hr   )r   r  r  r  r   r   r   r  )r   r  r  r  r  metadatas         r   _build_memory_write_metadataz$AIAgent._build_memory_write_metadatan  s     )cGD:PRb,c,c! H4!8,GG//R!%!8!>BW8OQV)W)W!
$
 
$
  	*")HY 	4'3H^$II!1!1IIIIr   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.
        r  Nr  r   r+  r  r)  )r   r   r!   r   r   )r   r'  r"  overrider,  s        r   $_apply_persist_user_message_overridez,AIAgent._apply_persist_user_message_override  s     d7>>4!A4HHs{F####c(mm######3-C#t$$ *F)B)B!)I $#* *)B)Br   r  c                     |                      |           || _        |                     |           |                     ||           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)r  r  _save_session_log_flush_messages_to_session_db)r   r'  r  s      r   _persist_sessionzAIAgent._persist_session  sQ    
 	11(;;;!)x(((**85IJJJJJr   c                    | j         sdS |                     |           	 | j        s|                                  |rt	          |          nd}t          || j                  }||d         D ]}|                    dd          }|                    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   r+  r  r)  r   c                 @    g | ]}|j         j        |j         j        d S )r   r   )r   r   r   r   s     r   r   z9AIAgent._flush_messages_to_session_db.<locals>.<listcomp>  s:     ' ' ' "$!1@UVV' ' 'r   r  r  ra  	assistantr  r  r  codex_reasoning_itemscodex_message_items)r  r+  r)  r  r   r  ra  r  r  r  r  r  z$Session DB append_message failed: %s)r  r  r  r  r   r   r  r   r  r!   r   r  append_messager  r   rS  rT  )
r   r'  r  	start_idx
flush_fromr,  r+  r)  tool_calls_dataru  s
             r   r  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''),,"&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   G1H 
IH>>I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   rQ  r+  r  )rZ  r   r   copy)r   r'  last_assistant_idxr>  s       r   "_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 %==??" +++,,r   c                     | j         sdS g }| j         D ]T}|d         }|d         |                    dd          |                    di           dd}|                    |           Ut          j        |d	
          S )z
        Format tool definitions for the system message in the trajectory format.
        
        Returns:
            str: JSON string representation of tool definitions
        z[]r   r   r  r   
parametersN)r   r  r  requiredFensure_ascii)ro  r   r   r   rU  )r   formatted_toolsr>  funcformatted_tools        r    _format_tools_for_system_messagez(AIAgent._format_tools_for_system_message  s     z 	4 J 	3 	3D
#DV#xxr::"hh|R88 	 N "">2222z/>>>>r   
user_query	completedc                    g }d|                                   d}|                    d|d           |                    d|d           d}|t          |          k     r||         }|d         dk    rd	|v r-|d	         r$d
}|                    d          r&|d                                         rd|d          d}|                    d          r5|d                                         r|t          |d                   dz   z  }|d	         D ]}	|	rt          |	t                    s	 t          |	d         d         t                    r t          j
        |	d         d                   n|	d         d         }
nB# t          j        $ r0 t          j        d|	d         d         dd                     i }
Y nw xY w|	d         d         |
d}|dt          j        |d           dz  }d|vrd|z   }|                    d|                                d           g }|dz   }|t          |          k     r8||         d         dk    r%||         }d}|d         }	 |                                                    d           rt          j
        |          }n# t          j        t"          f$ r Y nw xY wt          |          }|t          |d	                   k     r|d	         |         d         d         nd!}|t          j        |                    d"d
          ||d#d          z  }|d$z  }|                    |           |dz  }|t          |          k     r||         d         dk    %|r0|                    dd                    |          d           |dz
  }nd
}|                    d          r&|d                                         rd|d          d}|d         pd
}|t          |          z  }d|vrd|z   }|                    d|                                d           n*|d         d%k    r|                    d|d         d           |dz  }|t          |          k     |S )&ay  
        Convert internal message format to trajectory format for saving.
        
        Args:
            messages (List[Dict]): Internal message history
            user_query (str): Original user query
            completed (bool): Whether the conversation completed successfully
            
        Returns:
            List[Dict]: Messages in trajectory format
        a  You are a function calling AI model. You are provided with function signatures within <tools> </tools> XML tags. You may call one or more functions to assist with the user query. If available tools are not relevant in assisting with user query, just respond in natural conversational language. Don't make assumptions about what values to plug into functions. After calling & executing the functions, you will be provided with function results within <tool_response> </tool_response> XML tags. Here are the available tools:
<tools>
a  
</tools>
For each function call return a JSON object, with the following pydantic model json schema for each:
{'title': 'FunctionCall', 'type': 'object', 'properties': {'name': {'title': 'Name', 'type': 'string'}, 'arguments': {'title': 'Arguments', 'type': 'object'}}, 'required': ['name', 'arguments']}
Each function call should be enclosed within <tool_call> </tool_call> XML tags.
Example:
<tool_call>
{'name': <function-name>,'arguments': <args-dict>}
</tool_call>r  )fromr   humanr   r+  r  r   r   r  z<think>
z

</think>
r)  
r   r   z2Unexpected invalid JSON in trajectory conversion: Nr  r   r  z<tool_call>
Fr  z
</tool_call>
<think>z<think>
</think>
gptr>  z<tool_response>
)rL  rN  r  r  )r  r   r)  z
</tool_response>r  )r  r   r   r   r   rs   r!   r   r   r   r   rV  r   rT  rU  r  r  r  r;  )r   r'  r  r  
trajectory
system_msgr>  r,  r)  r   r   tool_call_jsontool_responsesjtool_msgtool_responsetool_content
tool_indexr  raw_contents                       r   _convert_to_trajectory_formatz%AIAgent._convert_to_trajectory_format  s!    

f
 ==??
f 
f 
f 	 	
 
 	 	 	 	
 
 	 	 	 #h--1+C6{k))3&&3|+<& !G ww{++ MK0@0F0F0H0H M"Lc+.>"L"L"Lwwy)) Vc)n.B.B.D.D V  #>s9~#N#NQU#UU &)%6 t t	(U
9d0K0KUX+Zdenoyez  |G  fH  JM  [N  [N  )v
9Z3H3U(V(V(V  T]  ^h  Ti  ju  TvII#3 + + + $O  -Lajkuav  xC  bD  EI  FI  EI  bJ  -L  -L  M  M  M(*III	+ %.j$9&$A)2* *  #s4:n[`3a3a3a#s#s#ss !//"7'"A%% %!(!1!1' '    &(NAAc(mm++F0Cv0M0M#+A;(; (0	':!+1133>>zJJ H/3z,/G/G $ 4nE ! ! ! D! &)%8%8
  *CL0A,B,BBB  -j9*EfMM!* "
 &,4LL,L,L$-'35 5 ).	*/ */ */ /
 &)==&--m<<<Q5 c(mm++F0Cv0M0M: & ""))$*%)YY~%>%>+ +    E
 !G ww{++ MK0@0F0F0H0H M"Lc+.>"L"L"L #&i."6BK:;GGGG !//"7'"A%% %!(' '    
 V&&!!# ^# #   
 FAi #h--l s%   .AE>><F=<F=#;J J87J8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  r  _save_trajectory_to_filer  )r   r'  r  r  r  s        r   _save_trajectoryzAIAgent._save_trajectory  sE     % 	F77*iXX
 TZCCCCCr   errorc                    t          |           }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.
        z	<!DOCTYPE<htmlz<title[^>]*>([^<]+)</title>r   z!HTML error page (title not found)z2Cloudflare Ray ID:\s*<strong[^>]*>([^<]+)</strong>Nstatus_codezHTTP zRay u    — bodyr  r  r  r   ,    )r   rX  r   rW  r  r   r   r   r;  r!   r   r   )r  r3  rA  titlerayray_idr  r  r  r,  prefixs              r   r  zAIAgent._summarize_api_error  s     %jj #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)%%%r   r   c                 b    |sd S t          |          dk    rdS |d d          d|dd           S )Nr  z***r   r  r  r   )r   r   s     r   _mask_api_key_for_logszAIAgent._mask_api_key_for_logs  sG     	4s88r>>5bqb'((c"##h(((r   	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  rL  s      r   _clean_error_messagezAIAgent._clean_error_message  s      	#"? ??''(899 	PW	=Q=QOO ((9??,,-- w<<#dsdme+Gr   c                    i }t          | dd          }d}t          |t                    r?t          |                    d          t                    r|                    d          n|}t          |t                    rU|                    d          p|                    d          }t          |t                    r+|                                r|                                |d<   |                    d          p|                    d          }t          |t                    r+|                                r|                                |d<   dD ]"}|                    |          }|d	vr||d
<    n#|                    d          }|d	vrCd
|vr?	 t          j                    t          |          z   |d
<   n# t          t          f$ r Y nw xY wt          | dd          }	t          |	dd          }
|
r|
                    d          p|
                    d          }|rCd
|vr?	 t          j                    t          |          z   |d
<   n# t          t          f$ r Y nw xY w|
                    d          }|r	d
|vr||d
<   d|vr0t	          |                                           }|r|dd         |d<   d
|vr|                    d          pd}t          |t                    rt          j        d|t          j                  }|rnt          |                    d                    }|                    d                                          dk    r|dz  n|}t          j                    |z   |d
<   n[t          j        d|t          j                  }|r9t          j                    t          |                    d                    z   |d
<   |S )z;Extract structured rate-limit details from provider errors.r  Nr  coder6  r  error_description)	resets_atreset_atr  r(  retry_afterresponserq  retry-afterRetry-Afterzx-ratelimit-resetr  r   z/quotaResetDelay[:\s\"]+(\\d+(?:\\.\\d+)?)(ms|s)r   r6  msg     @@z?retry\s+(?:after\s+)?(\d+(?:\.\d+)?)\s*(?:sec|secs|seconds|s\b))r   r!   r   r   r   r   r  r2  rW  r   rX  r   rW  r  r  )r  r  r  r  r6  r  r   r   r)  r*  rq  ratelimit_resetraw_messagedelay_matchseconds	sec_matchs                   r   _extract_api_error_contextz"AIAgent._extract_api_error_context  s    #%ufd++dD!! 	Y+5dhhw6G6G+N+NXdhhw'''TXGgt$$ 	[[((@GKK,@,@F&#&& 36<<>> 3$*LLNN!kk),,P<O0P0PG'3'' 5GMMOO 5%,]]__	"0  C((
***/GJ'E + "++m44K*,,71J1J*.)++k8J8J*JGJ''!:.   D 5*d33(It44 		6!++m44RM8R8RK z88*.)++k8J8J*JGJ''!:.   D%kk*=>>O 6:W#<#<&5
#G##e****,,K 7%0#%6	"W$$kk),,2G'3'' V i(Z\cegerss V!+"3"3A"6"677E0;0A0A!0D0D0J0J0L0LPT0T0TefnnZ_G*.)++*?GJ'' "	Z! !I
 ! V.2ikkE)//RSBTBT<U<U.U
+s$   &F> >GG*&I I%$I%r*  c                     |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r  r  	raw_usageprompt_tokenstotal_tokens)	r   dataclassesr6  rd   r  r  popr9  r:  )r   r*  r8  r6  cur  s         r   #_usage_summary_for_api_request_hookz+AIAgent._usage_summary_for_api_request_hookF  s    4Hgt44	 	4&&&&&&YWWW&**K&&&#%#3 "$/r   )r  
api_kwargsr6  c          	         	 t          j        |          }|                    dd           d |                                D             }d}	 t	          | j        dd          }n2# t          $ r%}t                              d|           Y d}~nd}~ww xY wt          j
                                                    | j        |d| j                            d           | j        dk    rd	nd
 d|                     |           dd|dd}|t#          |          j        t'          |          d}dD ]}	t	          ||	d          }
|
|
||	<   t	          |dd          }|||d<   t	          |dd          }|R	 t	          |dd          |d<   |j        |d<   n2# t          $ r%}t                              d|           Y d}~nd}~ww xY w||d<   t          j
                                        d          }| j        d| j         d| dz  }|                    t1          j        |ddt&                    d !           |                     | j         d"|            t9          d#          r*t;          t1          j        |ddt&                               |S # t          $ r)}| j        rt?          j         d$|            Y d}~dS d}~ww xY w)%a  
        Dump a debug-friendly HTTP request record for the active inference API.

        Captures the request body from api_kwargs (excluding transport-only keys
        like timeout). Intended for debugging provider-side 4xx failures where
        retries are not useful.
        r  Nc                     i | ]
\  }}|||S r   r   r  s      r   r  z3AIAgent._dump_api_request_debug.<locals>.<dictcomp>g  s    CCCTQQ]Aq]]]r   r  z,Could not extract API key for debug dump: %sPOSTr  r  z
/responsesz/chat/completionszBearer zapplication/json)AuthorizationzContent-Type)methodr  rq  r  )	timestampr  r6  r   )r   r  )r  
request_idr%  paramr   r  r*  r  response_statusresponse_textz,Could not extract error response details: %sr  z%Y%m%d_%H%M%S_%frequest_dump_rb  rJ  Fr6  )r  indentrx  utf-8encodingu$   🧾 Request debug dump written to: HERMES_DUMP_REQUEST_STDOUTz*Failed to dump API request debug payload: )!r  deepcopyr<  r  r   r  r   rS  r   r   r  	isoformatr  r   r  r  r  r   r'   r   r  r  r
  
write_textr   rU  r  r  ry   r  r  r   rT  )r   r?  r6  r  r  r  ru  dump_payload
error_info	attr_name
attr_value	body_attrresponse_objrE  	dump_file
dump_errors                   r   _dump_api_request_debugzAIAgent._dump_api_request_debugV  s   B	=,,DHHY%%%CCTZZ\\CCCDGP!$+y$?? P P PKQOOOOOOOOP &\^^5577"o $"m22377  GZkIkIk  rE  G  G)Y43N3Nw3W3W)Y)Y(:    ! 	, ,L   KK0"5zz. .
 "X ; ;I!(	4!@!@J!-0:
9-#E6488	()2Jv&&uj$??+X8?m]a8b8b
#456B6G
?33$ X X X%SUVWWWWWWWWX )3W% //0BCCI(Z(Z(Z)(Z(Z(ZZI  
<eAsSSS  !   
 LLDO\\QZ\\]]];<< [djE!UXYYYZZZ 	 	 	# [ YZ Y YZZZ44444	sn   A
J A$ #J $
B.B	J BC'J ;F J 
G	$G?J G		CJ 
K#KK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)rs   rX  r  r   )r)  s    r   _clean_session_contentzAIAgent._clean_session_content  sS      	N-g66&'7;;&)7G<<}}r   c                 <   |p| j         }|sdS 	 g }|D ]r}|                    d          dk    rB|                    d          r-t          |          }|                     |d                   |d<   |                    |           s| j                                        r	 t          j        | 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                                                    | j        pd
| j        pg t          |          |d
}t1          | j        |dt2                     dS # t          $ r/}| j        rt          j        d|            Y d}~dS Y d}~dS d}~ww xY w)a  
        Save the full raw session to a JSON file.

        Stores every message exactly as the agent sees it: user messages,
        assistant messages (with reasoning, finish_reason, tool_calls),
        tool responses (with tool_call_id, tool_name), and injected system
        messages (compression summaries, todo snapshots, etc.).

        REASONING_SCRATCHPAD tags are converted to <think> blocks for consistency.
        Overwritten after each turn so it always reflects the latest state.
        Nr+  r  r)  rL  rM  message_countr'  zHSkipping session log overwrite: existing has %d messages, current has %dr   )
r  r  r   r  r  last_updatedr  ro  r_  r'  r6  )rK  rx  zFailed to save session log: )r  r   r   r]  r   r  existsr   r   	read_textr   r   r   r   r  r  r   r  r  rQ  r   r  r  ro  rv   r   r  rT  )r   r'  rL  r,  r   existing_countr  ru  s           r   r  zAIAgent._save_session_log  s    5t5 	F0	DG $ $776??k11cggi6H6H1s))C%)%@%@Y%P%PC	Ns#### $++-- 
#z$*?*I*ISZ*I*[*[\\H%-\\/3x||T^`bGcGcCdCd%e%eN%G44f*CLL    5 !   D #o M M!%!3!=!=!?!? ( 8 8 : :!%!;!Ar)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s>   BG"  BD= <G" =
E
G" 	E

BG" "
H,HH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)
        TNFr  r  z0Failed to propagate interrupt to child agent: %su   
⚡ Interrupt requested(   z: 'z...''r   )r  r  r  _set_interruptr  r   r  r   r  r  	interruptrS  r   r  r  r   )	r   r  _tracker_tracker_lock_worker_tids_wtidchildren_copychildru  s	            r   rh  zAIAgent.interrupt  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  r  )	r  r  r  r  rg  r   r  r   r  )r   ri  rj  rk  rl  _steer_locks         r   clear_interruptzAIAgent.clear_interrupt8  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r  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  r   T)r   r   r  )r   r  rL  r   r   s        r   steerzAIAgent.steerY  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   r  s      r   _drain_pending_steerzAIAgent._drain_pending_steer}  s     3T::=4!1488D"&DK 	' 	'&D"&D	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' s   AAAnum_tool_msgsc                 "   |dk    s|sdS |                                  }|sdS d}t          t          |          dz
  t          t          |          |z
  dz
  d          d          D ]<}||         }t	          |t
                    r|                    d          dk    r|} n=|st          | dd          }|<|5  | j        r| j        dz   |z   | _        n|| _        ddd           n# 1 swxY w Y   n"t          | d	d          }|r|dz   |z   n|| _        dS d
| }	||                             dd          }
t	          |
t                    si	 |
rt          |
          ng }|                    d|	                                d           |||         d<   n-# t          $ r |
 |	 ||         d<   Y nw xY w|
|	z   ||         d<   t                              dt          |          |dd         t          |          dk    rdndz              dS )uy  Append any pending /steer text to the last tool result in this turn.

        Called at the end of a tool-call batch, before the next API call.
        The steer is appended to the last ``role:"tool"`` message's content
        with a clear marker so the model understands it came from the user
        and NOT from the tool itself. Role alternation is preserved —
        nothing new is inserted, we only modify existing content.

        Args:
            messages: The running messages list.
            num_tool_msgs: Number of tool results appended in this batch;
                used to locate the tail slice safely.
        r   Nr   rQ  r+  r>  r  r   r  

User guidance: r)  r   r  r   r  z9Delivered /steer to agent after tool batch (%d chars): %sx   r  )ru  rZ  r   r   r!   r   r   r   r  r   r  r   lstripr   rS  r  )r   r'  rv  
steer_text
target_idxr  r,  r   r   r  existing_contentblockss               r   $_apply_pending_steer_to_tool_resultsz,AIAgent._apply_pending_steer_to_tool_results  s    AXF..00
 	F 
s8}}q(#c(mmm.Ka.OQS*T*TVXYY 	 	A1+C#t$$ F)B)B
 D"7>>E  9 9* 9.2.AD.H:.U++.8+	9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 #4)94@@HP&`x$'C'CV`#F3z33#J/33IrBB*C00 	HP3CK.///vv}}GGHHH28$Y// P P P5E2Ov2O2O$Y///P /?.GHZ +G
OOttZ3)>)>BG	
 	
 	
 	
 	
s%   ;"C))C-0C-AF F54F5descc                 D    t          j                     | _        || _        dS )zAUpdate the last-activity timestamp and description (thread-safe).N)r  r  r  )r   r  s     r   _touch_activityzAIAgent._touch_activity  s    !%#'   r   http_responsec                     |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``.
        Nrq  r   )parse_rate_limit_headersr  )r   agent.rate_limit_trackerr  r  r  r   )r   r  rq  r  states        r   _capture_rate_limitszAIAgent._capture_rate_limits  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    r   get_rate_limit_statezAIAgent.get_rate_limit_state  s    %%r   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.
        Nrq  zx-openrouter-cache-statusHITr   z)OpenRouter response cache HIT (total: %d)zOpenRouter response cache %s)r   r   r  r  rS  r  r   r   )r   r  rq  statuss       r   _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  roundr  r  r  r  r   r   )r   elapseds     r   get_activity_summaryzAIAgent.get_activity_summary  sc     )++ 66 $ 6"&":&+GQ&7&7 ."2"105/9	
 	
 		
r   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  r   N)r$  on_session_endr   shutdown_allr  r  r  r   r'  s     r   r  z 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                 t    | j         sdS 	 | j                             |pg            dS # t          $ r Y dS w xY w)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.N)r$  r  r   r  s     r   commit_memory_sessionzAIAgent.commit_memory_session+  s\    
 # 	F	 //B????? 	 	 	DD	s   ) 
77original_user_messagefinal_responseinterruptedc                    |rdS | j         r|r|sdS 	 | j                             ||| j        pd           | 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   )r  )r$  sync_allr  queue_prefetch_allr   )r   r  r  r  s       r   _sync_external_memory_for_turnz&AIAgent._sync_external_memory_for_turn7  s    B  	F$ 	 	<Q 	F
	 ))%~?0b *     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).
        Nr  cache_evictTr5  )
r  r  r  r  release_clientsr   r  r   _close_openai_clientr  )r   childrenrn  r  s       r   r  zAIAgent.release_clientsh  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  Nr   r   )process_registryr  r  agent_closeTr5  )r   tools.process_registryr  kill_allr   r5   r?   r  r  r  r  r  r  r  )r   r  r  r  rn  r  s         r   r  zAIAgent.close  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.
        Nr+  r>  r)  r   z"todos"todosF)mergeu   📋 Restored z todo item(s) from history)reversedr   r   r   r!   r  rV  rW  r  r   r  r  r  r   rg  )r   r  last_todo_responser,  r)  r   s         r   _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    r   is_interruptedzAIAgent.is_interrupted  s     ((r   system_messagec                     d} j         s j        st                      }|r|g}d}|st          g}|                    t
                     g }d j        v r|                    t                     d j        v r|                    t                     d j        v r|                    t                     d j        v r|                    t                     |r(|                    d                    |                     t           j                  }|r|                    |            j        ro j        }d}|du s+t          |t                    r|                                dv rd}n|du s+t          |t                    r|                                d	v rd}nt          |t"                    r7 j        pd
                                t'          fd|D                       }n; j        pd
                                t'          fdt(          D                       }|ry|                    t*                      j        pd
                                }	d|	v sd|	v r|                    t,                     d|	v sd|	v r|                    t.                     ||                    |            j        rp j        r1 j                            d          }
|
r|                    |
            j        r1 j                            d          }|r|                    |            j        rB	  j                                        }|r|                    |           n# t<          $ r Y nw xY wt'           fddD                       }|r2d d  j        D             D             }t?           j        |          }nd
}|r|                    |            j        s>tA          j!        d          pd}tE          ||          }|r|                    |           ddl#m$}  |            }d|%                    d           } j&        r j'        r|d j'         z  } j        r|d j         z  } j(        r|d  j(         z  }|                    |            j(        d!k    rQd" j        v r  j        )                    d"          d#         n j        }|                    d$| d% j         d&           tU                      }|r|                    |            j+        pd
                                ,                                }|tZ          v r!|                    tZ          |                    nR|rP	 dd'l.m/} |0                    |          }|r!|j1        r|                    |j1                   n# t<          $ r Y nw xY wd(                    d) |D                       S )*a;  
        Assemble the full system prompt from all layers.
        
        Called once per session (cached on self._cached_system_prompt) and only
        rebuilt after context compression events. This ensures the system prompt
        is stable across all turns in a session, maximizing prefix cache hits.
        FTrS  r   skill_managekanban_showr  )rp  alwaysrr  on)falsenevernooffr   c              3   n   K   | ]/}t          |t                    |                                v V  0d S r   )r!   r   r  r   pr5  s     r   r   z/AIAgent._build_system_prompt.<locals>.<genexpr>C  sA      __1JWXZ]L^L^_aggii;6______r   c              3       K   | ]}|v V  	d S r   r   r  s     r   r   z/AIAgent._build_system_prompt.<locals>.<genexpr>G  s(      TT1a;.TTTTTTr   geminigemmar  codexNr  c              3   *   K   | ]}|j         v V  d S r   )r  )r   r   r   s     r   r   z/AIAgent._build_system_prompt.<locals>.<genexpr>o  s+      wwtt'<<wwwwwwr   )r   r   r  c                     h | ]}||S r   r   )r   toolsets     r   r?  z/AIAgent._build_system_prompt.<locals>.<setcomp>q  s0          r   c              3   4   K   | ]}t          |          V  d S r   )r2   )r   r  s     r   r   z/AIAgent._build_system_prompt.<locals>.<genexpr>s  s<          8A(33           r   )available_toolsavailable_toolsetsr  )r	  	skip_soulr   )r  zConversation started: z%A, %B %d, %Y %I:%M %pz
Session ID: z
Model: z
Provider: r.  r  rQ  z#You are powered by the model named z. The exact model ID is zt. When asked what model you are, always answer based on this information, not on any model name returned by the API.)platform_registryr  c              3   f   K   | ],}|                                 |                                 V  -d S r   r   r   r  s     r   r   z/AIAgent._build_system_prompt.<locals>.<genexpr>  s7      HHaggiiH17799HHHHHHr   )2r  r  r^   rF   r   rK   r  rH   rI   rJ   rL   r;  rM   r0  r!   r   r  r  r  r   r`   r_   ra   rb   r  r  format_for_system_promptr  r$  build_system_promptr   r[   r   r=  r\   hermes_timer  r  r  r  r  r"  r]   r  r   rG   gateway.platform_registryr  r   platform_hint)r   r  _soul_loaded_soul_contentprompt_partstool_guidancenous_subscription_prompt_enforce_inject_model_lower	mem_block
user_block_ext_mem_blockhas_skills_toolsavail_toolsetsskills_prompt_context_cwdcontext_files_prompt_hermes_nowr  timestamp_line_model_short
_env_hintsplatform_keyr  _entryr5  s   `                         @r   _build_system_promptzAIAgent._build_system_prompt  s5   ( " 	$$*A 	$(NNM $ -# 	423L 	6777 t,,,  111t444  !8999T222  111
 D111  111 	9 7 7888#A$BW#X#X # 	: 8999   	I1HG4Jx$=$=(..BRBRVuBuBuU""z(C'@'@"X^^EUEUYxExExHd++ U#z/R6688_________  $z/R6688TTTT8STTTTT 
I##$ABBB $
 0b7799 |++w,/F/F ''(IJJJ L((G|,C,C ''(GHHH %/// 		4# 3 .GGQQ	 3 ''	222) 4!/HHPP
 4 ''
333  	!%!5!I!I!K!K! 8 ''777    wwwwIvwwwww 	    EIEZ       N 7 $ 5#1  MM
 M 	/...& 		:
 9^44<L#= L$: $: $: # :##$8999222222kmmZ#,,?W2X2XZZ 	ADO 	A@t@@@N: 	76$*666N= 	=<T]<<<NN+++
 =I%%8;tz8I8I4:++C0044tzL>l > >)-> > >   -..
 	,
++++2244::<<>))| <==== 	GGGGGG*..|<< >f2 > ''(<===    {{HHlHHHHHHs$   <0M- -
M:9M:>W 
WW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_idr   r*  )r!   r   r   r   )r   s    r   _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Hr   >   r>  r  r  r   r  	developerc                 H  
 g }| D ]V}|                     d          }|t          j        vrt                              d|           A|                    |           W|} t                      }| D ]f}|                     d          dk    rK|                     d          pg D ]3}t                              |          }|r|                    |           4gt                      }| D ]G}|                     d          dk    r,|                     d          }|r|                    |           H||z
  

r6
fd| D             } t                              dt          
                     ||z
  }|rg }	| D ]}|	                    |           |                     d          dk    rQ|                     d          pg D ]9}t                              |          }||v r|	                    dd	|d
           :|	} t                              dt          |                     | S )u	  Fix orphaned tool_call / tool_result pairs before every LLM call.

        Runs unconditionally — not gated on whether the context compressor
        is present — so orphans from session loading or manual message
        manipulation are always caught.
        r+  z9Pre-call sanitizer: dropping message with invalid role %rr  r   r>  r  c                 t    g | ]4}|                     d           dk    r|                     d          v 2|5S r+  r>  r  r   )r   rA  orphaned_resultss     r   r   z2AIAgent._sanitize_api_messages.<locals>.<listcomp>  sO       f//AEE.4I4IM]4]4] 4]4]4]r   z6Pre-call sanitizer: removed %d orphaned tool result(s)u2   [Result unavailable — see context summary above]r+  r)  r  z0Pre-call sanitizer: added %d stub tool result(s))
r   r  _VALID_API_ROLESrS  r   r   r  r  r.  r   )r'  filteredr,  r+  surviving_call_idsr   cidresult_call_idsmissing_resultspatchedr  s             @r   _sanitize_api_messageszAIAgent._sanitize_api_messages  s     	! 	!C776??D7333O   OOC    "%%% 	4 	4Cwwv+--'',//52 4 4B!::2>>C 4*..s333"uu 	- 	-Cwwv&((ggn-- -#'',,, +-?? 	   #  H LLH$%%   -> 	,.G 
 
s###776??k11!ggl339r  %>>rBB/11#NN(.+_03, ,   
 HLLBO$$   r   r,  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.
        r+  r  Fr   r)  r   )r  redacted_thinkingr  r   Nr  r  Tr  )r!   r   r   r   r   r  )r,  r)  r  btyper  r  rds          r   _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ur   c                    | s| S d | D             }t          |           t          |          z
  }|dk    r| S g }d}|D ]}|r|d         nd}||                    d          dk    r|                    d          dk    r|                    dd          }|                    dd          }t          |          }	t          |t                    r*t          |t                    r|r|rd	nd}
||
z   |z   |	d<   nt          |t
                    r8t          |t
                    r#t          |          t          |          z   |	d<   nt          |t
                    rDt          |t                    r/|rt          |          d
|dgz   |	d<   nt          |          |	d<   nxt          |t                    rLt          |t
                    r7g }|r|                    d
|d           |                    |           ||	d<   n|                    |           |	|d<   |dz  }|                    |           t          	                    d||           |S )a  Drop thinking-only assistant turns; merge any adjacent user messages left behind.

        Runs on the per-call ``api_messages`` copy only. The stored
        conversation history (``self.messages``) is never mutated, so the
        user still sees the thinking block in the CLI/gateway transcript and
        session persistence keeps the full trace. Only the wire copy sent to
        the provider is cleaned.

        Why drop-and-merge rather than inject stub text:
        - Fabricating ``"."`` / ``"(continued)"`` text lies in the history
          and makes future turns see model output the model didn't emit.
        - Dropping the turn preserves honesty; merging adjacent user messages
          preserves the provider's role-alternation invariant.
        - This is the pattern used by Claude Code's ``normalizeMessagesForAPI``
          (filterOrphanedThinkingOnlyMessages + mergeAdjacentUserMessages).
        c                 F    g | ]}t                               |          |S r   )r  r  r   rA  s     r   r   z?AIAgent._drop_thinking_only_and_merge_users.<locals>.<listcomp>Y  s+    RRRa7+N+Nq+Q+QRRRRr   r   rQ  Nr+  r  r)  r   r  r  ry  r   zbPre-call sanitizer: dropped %d thinking-only assistant turn(s), merged %d adjacent user message(s))
r   r   r   r!   r   r  r   extendrS  r   )r'  keptdroppedmergedmergesrA  prevprev_contentcur_content	prev_copysep
new_blockss               r   #_drop_thinking_only_and_merge_usersz+AIAgent._drop_thinking_only_and_merge_usersA  s   (  	O SR8RRRh--#d))+a<<O (* +	! +	!A!'16"::TD HHV$$..EE&MMV++#xx	266eeIr22
 !JJ	 lC00 ZS5Q5Q $0H[H&&bC+7#+=+KIi((d33 
;PT8U8U +/+=+=[@Q@Q+QIi((d33 
;PS8T8T " B/3L/A/A%+[AAE 0	),, 04L/A/A	),,c22 
z+t7T7T 
79J# R"))6<*P*PQQQ%%k222+5Ii(( MM!$$$&r
!a    1		
 	
 	
 r   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   Nr   r   s     r   r   z3AIAgent._cap_delegate_task_calls.<locals>.<genexpr>  s1      [[2r{7G?7Z7ZQ7Z7Z7Z7Z[[r   r  r   zUTruncated %d excess delegate_task call(s) to enforce max_concurrent_children=%d limit)tools.delegate_toolr  r"  r   r   r   rS  rT  )r   r  max_childrendelegate_countkept_delegates	truncatedr   s          r   _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  $$$$/\)<	
 	
 	

 r   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)	r  r   r   r   r.  r   rS  rT  r   )r   seenuniquer   r   s        r   _deduplicate_tool_callszAIAgent._deduplicate_tool_calls  s     EE 	T 	TB;#R[%:;C$b!!!!@"+BRSSSSVs:66vvJFr   r  c                    ddl ddlm} |sdS dt          dt          fd}dt          dt          ffd}dt          dt          dz  fd}|                                }|| j        v r|S  ||          }|| j        v r|S ||| ||          h}t          d	          D ]x}	t                      }
|D ]`} ||          }|rQ|
                    |           |
                     ||                     |
                     ||                     a||
z  }y|D ]}|r|| j        v r|c S  ||| j        d
d          }|r|d         S dS )a  Attempt to repair a mismatched tool name before aborting.

        Models sometimes emit variants of a tool name that differ only
        in casing, separators, or class-like suffixes. Normalize
        aggressively before falling back to fuzzy match:

        1. Lowercase direct match.
        2. Lowercase + hyphens/spaces -> underscores.
        3. CamelCase -> snake_case (TodoTool -> todo_tool).
        4. Strip trailing ``_tool`` / ``-tool`` / ``tool`` suffix that
           Claude-style models sometimes tack on (TodoTool_tool ->
           TodoTool -> Todo -> todo). Applied twice so double-tacked
           suffixes like ``TodoTool_tool`` reduce all the way.
        5. Fuzzy match (difflib, cutoff=0.7).

        See #14784 for the original reports (TodoTool_tool, Patch_tool,
        BrowserClick_tool were all returning "Unknown tool" before).

        Returns the repaired name if found in valid_tool_names, else None.
        r   N)get_close_matchessr   c                 z    |                                                      dd                              dd          S )N-rb  r  )r  replace)r+  s    r   _normz(AIAgent._repair_tool_call.<locals>._norm  s0    7799$$S#..66sC@@@r   c                 L     j         dd|                                           S )Nz(?<!^)(?=[A-Z])rb  )r  r  )r+  rX  s    r   _camel_snakez/AIAgent._repair_tool_call.<locals>._camel_snake  s&    26,c155;;===r   c                     |                                  }dD ]D}|                    |          r-| d t          |                                        d          c S Ed S )N)_toolz-toolr>  z_-)r  r[  r   r  )r+  lcsuffixs      r   _strip_tool_suffixz5AIAgent._repair_tool_call.<locals>._strip_tool_suffix  sg    B4 : :;;v&& :^F|^,33D99999:4r   r6  r   gffffff?)r?  cutoff)	rX  difflibr*  r   r  r  rZ  r  r.  )r   r  r*  r/  r1  r6  lowered
normalizedcandsrb  extracrZ  matchesrX  s                 @r   _repair_tool_callzAIAgent._repair_tool_call  s   * 				------ 	4	AS 	AS 	A 	A 	A 	A	>C 	>C 	> 	> 	> 	> 	> 	>	# 	#* 	 	 	 	 //##d+++NU9%%
... %gz<<	;R;RSq 	 	A!eeE 6 6--a00 6IIh'''IIeeHoo...IIll844555UNEE 	 	A Q$/// $#GT-BaPSTTT 	1:tr   c                 X    d| _         | j        r| j                                         dS dS )z
        Invalidate the cached system prompt, forcing a rebuild on the next turn.
        
        Called after context compression events. Also reloads memory from disk
        so the rebuilt prompt captures any writes from this session.
        N)r  r  r#  r%   s    r   _invalidate_system_promptz!AIAgent._invalidate_system_prompt  s<     &*" 	0--/////	0 	0r   r   r0  r   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)r0  r   rB  s      r   rf   zAIAgent._deterministic_call_id  s     ,GYFFFr   raw_idc                      t          |           S )z8Split a stored tool id into (call_id, response_item_id).)_codex_split_responses_tool_id)rE  s    r   rg   z AIAgent._split_responses_tool_id$  s     .f555r   r  response_item_idc                 "    t          ||          S )zCBuild a valid Responses `function_call.id` (must start with `fc_`).)(_codex_derive_responses_function_call_id)r   r  rH  s      r   re   z*AIAgent._derive_responses_function_call_id)  s     8AQRRRr   c                 J    t          j                    }|j         d|j         S )NrJ  )r   current_threadr   ident)r   threads     r   _thread_identityzAIAgent._thread_identity1  s(    )+++.....r   c                     t          | dd          }t          | dd          }t          | dd          }d|                                  d| d| d| S )	Nr  r  r   r  zthread=
 provider=z
 base_url=z model=)r   rO  )r   r  r   r  s       r   _client_log_contextzAIAgent._client_log_context5  s}    4Y774Y77gy111d++-- 1 1 1 1 1 1).1 1	
r   c                 `    t          | dd           }|t          j                    }|| _        |S )Nr  )r   r   r  r  )r   locks     r   _openai_client_lockzAIAgent._openai_client_lock>  s2    t^T22<?$$D $Dr   r  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.mockrX  r!   r   callabler   )r  rX  is_closed_attrhttp_clients       r   _is_openai_client_closedz AIAgent._is_openai_client_closedE  s     	'&&&&&fd## 	5 d;;%'' !>##  4 n%% tfi66"[%@@AAAur   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   rR  rm  TCP_KEEPALIVE)socket_options)	transportr   )httpxsocket
SOL_SOCKETSO_KEEPALIVEr  r   IPPROTO_TCPra  TCP_KEEPINTVLTCP_KEEPCNTrc  r   ClientHTTPTransportr   )r   _httpx_socket
_sock_opts_proxys        r   _build_keepalive_http_clientz$AIAgent._build_keepalive_http_clientc  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r  r7  c                   ddl m}m} t          |          } |              ||                    d                     | j        dk    s6t          |                    dd                                        d          r?ddlm	}  |di |}t                              d|||                                            |S | j        d	k    s6t          |                    dd                                        d
          r]ddlm} d |                                D             }	 |di |	}t                              d|||                                            |S | j        dk    rddlm}
m} t          |                    dd          pd          } ||          rwd |                                D             }	d|	vr|                     |          }|||	d<    |
di |	}t                              d|||                                            |S d|vr0|                     |                    dd                    }|||d<   t)          di |}t                              d|||                                            |S )Nr   )_validate_base_url_validate_proxy_env_urlsr   r  r   r  )CopilotACPClientz-Copilot ACP client created (%s, shared=%s) %szgoogle-gemini-clizcloudcode-pa://)GeminiCloudCodeClientc                 "    i | ]\  }}|d v 	||S )>   r  r  r   
project_idr  r   r  s      r   r  z1AIAgent._create_openai_client.<locals>.<dictcomp>  s4       A[[[ 1[[[r   z:Gemini Cloud Code Assist client created (%s, shared=%s) %sr  )GeminiNativeClientis_native_gemini_base_urlc                 "    i | ]\  }}|d v 	||S )>   r  r  r   r^  r  r   r  s      r   r  z1AIAgent._create_openai_client.<locals>.<dictcomp>  s4       !Q``` q```r   r^  z/Gemini native client created (%s, shared=%s) %sz(OpenAI client created (%s, shared=%s) %sr   )r  ru  rv  r   r   r  r   r  agent.copilot_acp_clientrw  rS  r  rR  agent.gemini_cloudcode_adapterrx  r  agent.gemini_native_adapterr{  r|  rs  r   )r   r  r6  r7  ru  rv  rw  r  rx  safe_kwargsr{  r|  r   keepalive_https                 r   r  zAIAgent._create_openai_client|  s<   WWWWWWWW ]++  """=,,Z88999=M))S1B1B:r1R1R-S-S-^-^_n-o-o)AAAAAA%%6666FKK?((**	   M=///3}7H7HUW7X7X3Y3Y3d3dev3w3w/LLLLLL !.!4!4!6!6  K +*99[99FKKL((**	   M=H$$aaaaaaaa=,,Z<<BCCH((22  %2%8%8%:%:   !33%)%F%Fx%P%PN%15CM2++::k::E,,..	   $ --!>>}?P?PQ[]_?`?`aaN)/=m, ((-((6$$&&		
 	
 	
 r   c                    ddl }d}	 t          | dd          }|dS t          |dd          }|dS t          |dd          }|dS t          |dd          pt          |dd          pg }t          |          D ]}t          |dd          pt          |dd          }|'t          |d	d          }	|	$t          |d
d          }	|	t          |	d	d          }	|	a	 |	                    |j                   n# t
          $ r Y nw xY w	 |	                                 n# t
          $ r Y nw xY w|dz  }n2# t          $ r%}
t          	                    d|
           Y d}
~
nd}
~
ww xY w|S )a  Force-close underlying TCP sockets to prevent CLOSE-WAIT accumulation.

        When a provider drops a connection mid-stream, httpx's ``client.close()``
        performs a graceful shutdown which leaves sockets in CLOSE-WAIT until the
        OS times them out (often minutes).  This method walks the httpx transport
        pool and issues ``socket.shutdown(SHUT_RDWR)`` + ``socket.close()`` to
        force an immediate TCP RST, freeing the file descriptors.

        Returns the number of sockets force-closed.
        r   NrZ  
_transport_pool_connections_network_stream_stream_sockr   r   z'Force-close TCP sockets sweep error: %s)
rg  r   r  shutdown	SHUT_RDWRr   r  r   rS  r   )r  rp  closedr^  re  r  connectionsconnr   sockr  s              r   _force_close_tcp_socketsz AIAgent._force_close_tcp_sockets  s    	!   )	I!&)T::K"q\4@@I q9gt44D|q nd33 4$// 
 [))  D"3T:: 6tY55  >vw55<"68T::D'&tWd;;<MM'"34444   DJJLLLL   D!-.  	I 	I 	ILLBCHHHHHHHH	Isp   D4 D4 D4 BD4 C76D4 7
DD4 DD4 DD4 
D*'D4 )D**	D4 4
E#>EE#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  r  rS  r  rR  r   r   )r   r  r6  r7  force_closedr  s         r   r  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 )Nr  Tr5  z7Failed to rebuild shared OpenAI client (%s) %s error=%sFzreplace:)
rU  r   r  r  r   rS  rT  rR  r  r  )r   r6  
old_client
new_clientr  s        r   _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 )Nr  zCDetected closed shared OpenAI client; recreating before use (%s) %szrecreate_closed:r6  z'Failed to recreate closed OpenAI client)	rU  r   r_  rS  rT  rR  r  r  r  )r   r6  r  s      r   _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                 ,   t          | dd          }|dS 	 t          |dd          }|dS t          |dd          }|dS t          |dd          }|dS t          |dd          pt          |dd          pg }d}t          |          D ]}t          |d	d          pt          |d
d          }|(t          |dd          }	|	$t          |dd          }	|	t          |	dd          }	|	bddl}
	 |	                    d           |	                    d|
j        |
j        z            }|dk    r|dz  }n # t          $ r Y nt          $ r |dz  }Y nw xY w	 |	                    d           # t          $ r Y w xY w# 	 |	                    d           w # t          $ r Y w w xY wxY w|dk    r3t          
                    d|           |                     d           dS n2# t          $ r%}t                              d|           Y d}~nd}~ww xY wdS )aY  Detect and clean up dead TCP connections on the primary client.

        Inspects the httpx connection pool for sockets in unhealthy states
        (CLOSE-WAIT, errors).  If any are found, force-closes all sockets
        and rebuilds the primary client from scratch.

        Returns True if dead connections were found and cleaned up.
        r  NFrZ  r  r  r  r   r  r  r  r   r   r   Tu@   Found %d dead connection(s) in client pool — rebuilding clientdead_connection_cleanupr  zDead connection check error: %s)r   r  rg  setblockingrecvMSG_PEEKMSG_DONTWAITBlockingIOErrorr   rS  rT  r  r   r   )r   r  r^  re  r  r  
dead_countr  r   r  rp  r   r  s                r   _cleanup_dead_connectionsz!AIAgent._cleanup_dead_connectionsM  s    x..>57	A!&)T::K"u\4@@I u9gt44D|und33 4$// 
 J[))   D"3T:: 6tY55  >vw55<"68T::D'&tWd;;<(((($$U+++99Q(87;O(OPPDs{{"a
&   D $ $ $!OJJJ$((...."   ((...."   A~~V   33;T3UUUt   	A 	A 	ALL:C@@@@@@@@	Aus   G" G" G" BG" 2AD65E=6
E E=EE=EE=E-,G" -
E:7G" 9E::G" =F%?FF%
F"	F%!F"	"F%%:G" "
H,HH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.Fr'  inputr   r   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 )Nr   >   	image_urlinput_imageTc              3   .   K   | ]} |          V  d S r   r   r   r  _contains_images     r   r   zPAIAgent._api_kwargs_have_image_parts.<locals>._contains_image.<locals>.<genexpr>  s-      FF!??1--FFFFFFr   c              3   .   K   | ]} |          V  d S r   r   r  s     r   r   zPAIAgent._api_kwargs_have_image_parts.<locals>._contains_image.<locals>.<genexpr>  s-      ==!??1--======r   F)r!   r   r   r   valuesr  )r   ptyper  s     r   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======5r   c              3   .   K   | ]} |          V  d S r   r   )r   itemr  s     r   r   z7AIAgent._api_kwargs_have_image_parts.<locals>.<genexpr>  s-      @@T??4((@@@@@@r   )r!   r   r   r  r  r   r   r   )r?  
candidatesr'  response_inputr  s       @r   _api_kwargs_have_image_partsz$AIAgent._api_kwargs_have_image_parts  s     *d++ 	5
>>*--h%% 	(h''' $00nd++ 	.n---	3 	4 	 	 	 	 	 	 @@@@Z@@@@@@r   	is_visionc                (    ddl m}  |d|          S )Nr   )copilot_request_headersT)is_agent_turnr  )hermes_cli.copilot_authr  )r   r  r  s      r   _copilot_headers_for_requestz$AIAgent._copilot_headers_for_request  s*    CCCCCC&&TYOOOOr   )r?  c                   ddl m} |                     |          }t          ||          r|S |                                 5  t          | j                  }d d d            n# 1 swxY w Y   t          t          |	                    dd                    d          r0| 
                    |pi           r|                     d          |d	<   |                     ||d
          S )Nr   rW  r  r   r   r  T)r  r  Fr5  )r[  rX  r  r!   rU  r   r  rw   r   r   r  r  r  )r   r6  r?  rX  primary_clientrequest_kwargss         r   _create_request_openai_clientz%AIAgent._create_request_openai_client  s>   &&&&&&;;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 )NFr5  )r  )r   r  r6  s      r   _close_request_openai_clientz$AIAgent._close_request_openai_client  s#    !!&!FFFFFr   on_first_deltac                 	   ddl }|p|                     d          }d}d}d}g | _        t          |dz             D ]N}	| j        rt          d          g }
	  |j        j        d(i |5 }|D ]}|                     d           | j        r nzt          |d	d
          }d|v s|dk    rit          |dd
          }|r| j        
                    |           |r9|s7|s d}|r	  |             n# t          $ r Y nw xY w|                     |           d|v rd}d|v r-d|v r)t          |dd
          }|r|                     |           |dk    r*t          |dd          }||

                    |           |dv rt          |dd          }|rt          |dd          nd}|rt          |dd          nd}t                              d|||t!          d | j        D                       |                                            |                                }t          |dd          }t'          |t(                    r|s|
r=t)          |
          |_        t                              dt/          |
                     n| j        r|s~d
                    | j                  }t3          dddt3          d|          g           g|_        t                              d!t/          | j                  t/          |                     |cddd           c S # 1 swxY w Y   # |j        |j        |j        t:          f$ r}|	|k     r<t                              d"|	dz   |dz   |                                 |           Y d}~Kt                              d#|                                 |           |                     ||$          cY d}~c S d}~wt>          $ r}tA          |          }d%|v }|rA|	|k     r;t                              d&|	dz   |dz   |                                            Y d}~|rKt                              d'|                                            |                     ||$          cY d}~c S  d}~ww xY wdS ))zJExecute one streaming Responses API request and return the final response.r   Ncodex_stream_directr  r   Fz+Agent interrupted before Codex stream retryreceiving stream responser   r   zoutput_text.deltaresponse.output_text.deltadeltaTrR  r  response.output_item.doner  )response.incompleteresponse.failedr*  r  incomplete_detailszkCodex Responses stream received terminal event %s (status=%s, incomplete_details=%s, streamed_chars=%d). %sc              3   4   K   | ]}t          |          V  d S r   r  r  s     r   r   z,AIAgent._run_codex_stream.<locals>.<genexpr>  s(      #T#TqCFF#T#T#T#T#T#Tr   outputz;Codex stream: backfilled %d output items from stream eventsr  r  r  output_textry  r   r+  r  r)  z?Codex stream: synthesized output from %d text deltas (%d chars)zNCodex Responses stream transport failed (attempt %s/%s); retrying. %s error=%szYCodex Responses stream transport failed; falling back to create(stream=True). %s error=%s)r  response.completedzGResponses stream closed before completion (attempt %s/%s); retrying. %szYResponses stream did not emit response.completed; falling back to create(stream=True). %sr   )!rf  r  _codex_streamed_text_partsrZ  r  InterruptedError	responsesr   r  r   r   r   _fire_stream_delta_fire_reasoning_deltarS  rT  r"  rR  get_final_responser!   r  r  r   r   r;  r   RemoteProtocolErrorReadTimeoutConnectErrorConnectionError!_run_codex_create_stream_fallbackr  r   )r   r?  r  r  ro  active_clientmax_stream_retrieshas_tool_callsfirst_delta_firedattemptcollected_output_itemsr   event
event_type
delta_textreasoning_text	done_itemresp_objr  r  r  _out	assembledr  err_textmissing_completeds                             r   _run_codex_streamzAIAgent._run_codex_stream  sN   b$"D"DLa"D"b"b! 13'/!344 o	 o	G( V&'TUUU+-"k3],3AAjAA H*V!' . .,,-HIII4 "!E%,UFB%?%?
.*<<
Nj@j@j)0)D)DJ) S $ ? F Fz R R R) D. D'8 !18<$5'5 %1)1,:N,<,<,<,</8 )1 )1 )1,0D)1 $ 7 7
 C C C,
::-1NN(J667j;P;P-4UGR-H-HN- K $ : :> J J J
 (+FFF(/vt(D(DI(4 6 = =i H H H'+UUU'.uj$'G'GHJR%\WXx%F%F%FX\Fbj1tCWY]1^1^1^pt."NN!\ *F4F ##T#TD4S#T#T#T T T $ 8 8 : :   &,%>%>%@%@N #>8TBBD!$-- d 1 489O4P4PN1"LL ] #$: ; ;    "< ^ (*0O(P(PI5D%.%0'2)8mR[)\)\)\(]	6 6 6 5N1 #LL a #D$C D Dc)nn   *QH* H* H* H* H* H* H* H* H* H* H* H* H* H* H* H* H* H* H*R .0BFDWYhi ` ` `///LLh!*Q.0022   HHHHo,,..  
 ==jQ^=__________   s88$8H$D!$ 3E)E)ELLa!*Q.0022	   HHHH$ dLLs0022    AA*UbAcccccccccc#}o	 o	s   L(*A7L"
C-,L-
C:	7L9C:	:HLL(L	L("L	#L((R	<OAOR	R	 AR6AR;R	RR	c           
      *   |p|                      d          }t          |          }d|d<   |                                                     |d          } |j        j        di |}t          |d          r|S t          |d          s|S d}g }g }	 |D ]l}	|                     d	           t          |	d
d          }
|
s*t          |	t                    r|	
                    d
          }
|
dk    rUt          |	dd          }|*t          |	t                    r|	
                    d          }||                    |           nY|
dv rUt          |	dd          }|s+t          |	t                    r|	
                    dd          }|r|                    |           |
dvrt          |	dd          }|*t          |	t                    r|	
                    d          }|t          |dd          }t          |t                    r|s|r=t          |          |_        t                              dt!          |                     nv|rtd                    |          }t%          dddt%          d|          g          g|_        t                              dt!          |          t!          |                     |c t          |dd          }t'          |          r	  |             S # t(          $ r Y S w xY wS n	 t          |dd          }t'          |          r	  |             nR# t(          $ r Y nFw xY wnA# t          |dd          }t'          |          r	  |             w # t(          $ r Y w w xY ww xY w||S t+          d          )zQFallback path for stream completion edge cases on Codex-style Responses backends.codex_create_stream_fallbackr  Tr   allow_streamr  __iter__Nr  r   r  r  )r  r  r   >   r  r  r  r*  z1Codex fallback stream: backfilled %d output itemsr  r  r  r  ry  r  z<Codex fallback stream: synthesized from %d deltas (%d chars)r  zHResponses create(stream=True) fallback did not emit a terminal response.r   )r  r   r  preflight_kwargsr  creater  r  r   r!   r   r   r  r  rS  r   r   r;  r   r\  r   r  )r   r?  r  r  fallback_kwargsstream_or_responseterminal_responser  collected_text_deltasr  r  r  r  r  r  close_fns                   r   r  z)AIAgent._run_codex_create_stream_fallbackB  sl   k$"D"DLj"D"k"kz**$(!--//@@_c@dd;]4;NNoNN %x00 	&%%):66 	&%% ')&(7	+ /- /-$$%@AAA$UFD99
! 3j&=&= 3!&6!2!2J !<<< 'vt < <I (Zt-D-D($)IIf$5$5	 ,.55i@@@#BBB#E7B77E  7Zt%<%< 7 %		'2 6 6 <-44U;;;%eee$+E:t$D$D!$,E41H1H,(-		*(=(=%$0"#4hEED!$-- d 1 7;<R7S7S-4"LL S #$: ; ;    3 
(*0E(F(FI8G%.['2)8mR[)\)\)\(]9 9 9 8-4
 #LL ^ #$9 : :C	NN   -,,17DAAH!! HJJJJ    D1 15/-b 17DAAH!! HJJJJ    D 17DAAH!! HJJJJ    D ($$efffs[   H1M %
K00
K=<K=M %
L0 0
L=<L=!M?#
M.-M?.
M;8M?:M;;M?Tc                   | j         dk    s| 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|                    d          }|                    d	          }t          |t                    r|
                                sdS t          |t                    r|
                                sdS |
                                | _        |
                                                    d
          | _        | j        | j        d<   | j        | j        d	<   |                     d          sdS dS )Nr  r  Fr   )!resolve_codex_runtime_credentials)force_refreshz#Codex credential refresh failed: %sr  r   r  codex_credential_refreshr  T)r  r  r  r  r   rS  r   r   r!   r   r   r  r  r   r  r  )r   r  r  credsr  r  r   s          r   %_try_refresh_codex_client_credentialsz-AIAgent._try_refresh_codex_client_credentials  sp   =---.1P1P5	IIIIII55EJJJEE 	 	 	LL>DDD55555	 ))I&&99Z(('3'' 	w}} 	5(C(( 	0@0@ 	5}} ((//44)-I&*.-J'22:T2UU 	5ts   - 
AAAc                   | j         dk    s| j        dk    rdS 	 ddlm}  |t	          dt          t          j        dd                              t          t          j        d	d
                    |          }n3# t          $ r&}t                              d|           Y d }~dS d }~ww xY w|                    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  nousFr   ) resolve_nous_runtime_credentialsrC  HERMES_NOUS_MIN_KEY_TTL_SECONDS1800HERMES_NOUS_TIMEOUT_SECONDS15)min_key_ttl_secondstimeout_seconds
force_mintz"Nous credential refresh failed: %sr  r   r  r  nous_credential_refreshr  T)r  r  r  r  r   r   r   r=  r2  r   rS  r   r   r!   r   r   r  r  r   r  r<  r  )r   r  r  r  r  r  r   s          r   $_try_refresh_nous_client_credentialsz,AIAgent._try_refresh_nous_client_credentials  s   =...$-62I2I5
	HHHHHH44$'C	:[]c0d0d,e,e$f$f %bi0Mt&T&T U U   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"A= =
B-B((B-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.
        rD  Fr   )resolve_copilot_tokenz%Copilot credential refresh failed: %sNr  r   r   copilot_credential_refreshr  z%Copilot credentials refreshed from %sT)r  r  r	  r   rS  r   r!   r   r   r  r  r   "_apply_client_headers_for_base_urlr  r  )r   r	  	new_tokentoken_sourcer  s        r   '_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  r  Fr  r  r   z	azure.comr   )r  r  z'Anthropic credential refresh failed: %sr   z?Failed to rebuild Anthropic client after credential refresh: %sr  T)r  r  r  r   r  r  r  r   rS  r   r!   r   r   r  r  r  r-   r  rT  r  r  )r   _baser  r  r  r  r  s          r   )_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t          |          | 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 | j                            dd            d S )Nr   )_AI_GATEWAY_HEADERSr  r  r  ai-gateway.vercel.shr  r  r  r  rx  r  r  r  r   r  r   )r  r  r  rw   r  r   r{  r  r  r  r!  r   r<  )r   r   r  r  r  r!  s         r   r  z*AIAgent._apply_client_headers_for_base_url&  s   PPPPPPPP ?;; 	=5E5E5G5GD 1222"8-CDD 	=59:M5N5ND 1222"8-ABB 	=5H5J5JD 1222"8-DEE 	=AAAAAA5L5L5N5ND 1222"8^<< 
	=6BDW5XD 1222"8-=>> 	=5I5K5KD 1222"8];; 	=HHHHHH5N5N#''	2666 6D 1222 ##$5t<<<<<r   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_tokenr   runtime_base_urlr   r  r   )r  r  r   r  Fr  r  credential_rotationr  )r   r   r  r  r  r  r  r  r   r  r  r-   r  r  r  r  r!   r   r  r  r  r  )r   r  runtime_keyruntime_baser  r  s         r   _swap_credentialzAIAgent._swap_credential?  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_reasonerror_contextr  has_retried_429r  r  c          	         | j         }|d|fS |}|6|dk    rt          j        }n#|dk    rt          j        }n|dv rt          j        }|t          j        k    re||nd}|                    ||          }|Bt                              d|t          |dd	                     | 	                    |           d
S d|fS |t          j        k    rg|sdS ||nd}|                    ||          }|Bt                              d|t          |dd	                     | 	                    |           d
S dS |t          j        k    r|
                                }	|	Et                              dt          |	dd	                      | 	                    |	           d|fS ||nd}|                    ||          }|Bt                              d|t          |dd	                     | 	                    |           d
S d|fS )a  Attempt credential recovery via pool rotation.

        Returns (recovered, has_retried_429).
        On rate limits: first occurrence retries same credential (sets flag True).
                        second consecutive failure rotates to next credential.
        On billing exhaustion: immediately rotates.
        On auth failures: attempts token refresh before rotating.

        `classified_reason` lets the recovery path honor the structured error
        classifier instead of relying only on raw HTTP codes. This matters for
        providers that surface billing/rate-limit/auth conditions under a
        different status code, such as Anthropic returning HTTP 400 for
        "out of extra usage".
        NFi    )  i  )r  r  u4   Credential %s (billing) — rotated to pool entry %sr*  rB  r*  )FTu7   Credential %s (rate limit) — rotated to pool entry %su1   Credential auth failure — refreshed pool entry Tr"  u@   Credential %s (auth refresh failed) — rotated to pool entry %s)r  rE   billing
rate_limitauthmark_exhausted_and_rotaterS  r  r   r  try_refresh_current)
r   r  r  r  r  r  effective_reasonrotate_status
next_entry	refresheds
             r   _recover_with_credential_poolz%AIAgent._recover_with_credential_pool]  sx   , $</)),#c!!#1#9  ###1#<  
**#1#6 ~555+6+BKKM77Man7ooJ%J!Jc22  
 %%j111"{/))~888" #"{+6+BKKM77Man7ooJ%M!Jc22  
 %%j111"{;~2220022I$oPWXacgilPmPmooppp%%i000_,, ,7+BKKM77Man7ooJ%V!Jc22  
 %%j111"{o%%r   c                 n    | j         dk    r|                                   | j        j        j        di |S )Nr  r   )r  r  r  r'  r  )r   r?  s     r   _anthropic_messages_createz"AIAgent._anthropic_messages_create  sA    =000::<<<5t%.5CC
CCCr   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_disabledFr  Nr  r   r  r  r  r  r  )r  drop_context_1m_beta)
r   r   r  r  r  r  r  r-   r  r  )r   _drop_1mr  regionr  s        r   _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"""r   c           	          dddddi fd}                                          dg                     }t          j                    }                     d           t	          j        |d          }|                                 d	}|                                r|                    d
           |dz  }|dz  d	k    r<t          j                    |z
  }                     dt          |           d           t          j                    |z
  }||k    rt          d                     dg           D                       dz  }t                              d||                    dd          |d                                dt          |           d                    dd           d           	  j        dk    r. j                                                                           n.                    d          }	|	                     |	d           n# t&          $ r Y nw xY w                     dt          |           d           |                    d           d         ;d          3t)          d!t          |           d"t          |           d#          d<   n j        r	  j        dk    r. j                                                                           n.                    d          }
|
                     |
d$           n# t&          $ r Y nw xY wt-          d%          |                                d         d         d          S )&ae  
        Run the API call in a background thread so the main conversation loop
        can detect interrupts without waiting for the full HTTP round-trip.

        Each worker thread gets its own OpenAI client instance. Interrupts only
        close that worker-local client, so retries and other requests never
        inherit a closed transport.

        Includes a stale-call detector: if no response arrives within the
        configured timeout, the connection is killed and an error raised so
        the main retry loop can try again with backoff / credential rotation /
        provider fallback.
        Nr*  r  r  c            	         	 j         dk    rK                    d
          d<                       
d         t          dd                     d<   nj         dk    r                    
          d<   nωj         d	k    rd
dlm} m}m}m	} 

                    dd          }

                    dd             | |          }	  |j        di 
}n)# t          $ r} ||          r ||            d }~ww xY w ||          d<   n:                    d
          d<    d         j        j        j        di 
d<   n# t          $ r}|d<   Y d }~nd }~ww xY w                    d          }	|	                    |	d           d S d S #                     d          }	|	                    |	d           w w xY w)Nr  codex_stream_requestr6  r?  r  _codex_on_first_delta)r  r  r*  r  r  r   )_get_bedrock_runtime_clientinvalidate_runtime_clientis_stale_connection_errornormalize_converse_response__bedrock_region__r  __bedrock_converse__chat_completion_requestr  request_completer  r   )r  r  r  r   r.  agent.bedrock_adapterr<  r=  r>  r?  r<  converser   chatcompletionsr  r   r  )r<  r=  r>  r?  r4  r  raw_response_bedrock_excru  request_clientr?  request_client_holderresultr   s             r   _callz.AIAgent._interruptible_api_call.<locals>._call  s   /a=$5556:6X6X5#- 7Y 7 7)(3 *.)?)?"4X>'.t5Ld'S'S *@ * *F:&&
 ]&:::)-)H)H)T)TF:&&]&888
            (^^,@+NNFNN#94@@@88@@F'6v'D'D'D'D$    54\BB >55f=== *E)D\)R)RF:&&6:6X6X8#- 7Y 7 7)(3 *a)>x)H)M)Y)`)n)ncm)n)nF:& $ $ $"#w$ "7!:!:8!D!D!-55nM_5````` .- "7!:!:8!D!D!-55nM_5```` .sU   CE C E 
D $C;;D  AE F 
E&E!F !E&&F 1Gr'  z&waiting for non-streaming API responseTr  r  r   333333?r   r   r  z$waiting for non-streaming response (z
s elapsed)c              3   N   K   | ] }t          t          |                    V  !d S r   r  r  s     r   r   z2AIAgent._interruptible_api_call.<locals>.<genexpr>3  .      SSqs3q66{{SSSSSSr   r  zjNon-streaming API call stale for %.0fs (threshold %.0fs). model=%s context=~%s tokens. Killing connection.r  r  rI  %   ⚠️ No response from provider for zs (non-streaming, model: z). Aborting call.r  stale_call_killr  z&stale non-streaming call killed after r+         @r  r*  z'Non-streaming API call timed out after zs with no response (threshold: s)interrupt_abortz!Agent interrupted during API call)r&  r   r  r  r   r  r  is_aliver;  r   r"  rS  rT  r  r  r  r  r5  r  r   TimeoutErrorr  r  )r   r?  rM  _stale_timeout_call_startrf  _poll_count_elapsed_est_ctxrcrJ  rK  rL  s   ``         @@r   _interruptible_api_callzAIAgent._interruptible_api_call  s.    #T22!)4 00	a 0	a 0	a 0	a 0	a 0	a 0	a 0	ap ??NN:r**
 
 ikkEFFFE$777				jjll @	LFF3F1K S A%%9;;4$$T3x==TTT   y{{[0H.((SSJNN:r4R4RSSSSSWXXGnNN7I668	   !!&CMM & &.8nnWi.P.P& & &  
	}(<<<.44666668888266x@@> ==bIZ=[[[    D$$MS]]MMM   s###'?*vj/A/I&2P#h-- P P8;N8K8KP P P' 'F7O ( L	}(<<<.44666668888)>)B)B8)L)L)5 ==nUf=ggg    D&'JKKKA jjll @	LB '?&/!j!!s&   A'H/ /
H<;H<A'L/ /
L<;L<c                    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.r  Nc                     g | ]}||S r   r   r   cbs     r   r   z;AIAgent._reset_stream_delivery_tracking.<locals>.<listcomp>s  s    pppBacaoRaoaoaor   r   )r   r   r  r  r   _record_streamed_assistant_textr  )r   scrubbertail	callbacksrc  s        r   _reset_stream_delivery_trackingz'AIAgent._reset_stream_delivery_trackingj  s    
 4!;TBB>>##D ;pp4+EtG\*]ppp	#  B4$   44T:::02---s   A
A A c                 n    t          |t                    r|rt          | dd          |z   | _        dS dS dS )zCAccumulate visible assistant text emitted through stream callbacks.r  r   N)r!   r   r   r  )r   r  s     r   rd  z'AIAgent._record_streamed_assistant_text|  sR    dC   	T 	@"EEL 111	 	 	 	r   c                     t          | t                    sdS t          j        dd|                                           S )Nr   z\s+r  )r!   r   rX  r  r   r  s    r   _normalize_interim_visible_textz'AIAgent._normalize_interim_visible_text  s9    $$$ 	2vfc4((..000r   c           	          |                      |                     |pd                    }|sdS |                      |                     t          | dd          pd                    }t          |          o||k    S )Nr   Fr  )rk  rK  r   r   )r   r)  visible_contentstreameds       r   _interim_content_was_streamedz%AIAgent._interim_content_was_streamed  s    >>$$W]33
 
  	577$$WT3UWY%Z%Z%`^`aa
 
 H~~=(o"==r   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)  r   (empty))already_streamedz interim_assistant_callback errorTr  )
r   r!   r   r   rK  r   ro  r   rS  r   )r   rp  rc  r)  visiblers  s         r   _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                 B   t          | dd          r%|r#|                                rd| _        d|z   }d}nd}t          |t                    rw|                     |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).r  Fr  Tr   r  Nr  r   c                     g | ]}||S r   r   rb  s     r   r   z.AIAgent._fire_stream_delta.<locals>.<listcomp>  s    hhhBY[YgRYgYgYgr   )r   r   r  r!   r   rK  feedrB   r{  r  r  r   rd  )r   r  prepended_breakre  rg  	deliveredrc  s          r   r  zAIAgent._fire_stream_delta  s    4.66 	$4 	$DJJLL 	$',D$D=D"OO#OdC   	)
 ++DJB77D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   (C66
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   r  rc  s      r   r  zAIAgent._fire_reasoning_delta  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   r  rc  s      r   _fire_tool_gen_startedzAIAgent._fire_tool_gen_started  sP     #>9    >r|  c                 :    | j         dupt          | dd          duS )z4Return True if any streaming consumer is registered.Nr  )r  r   r%   s    r   r  zAIAgent._has_stream_consumers  s/     &d2 Ct/66dB	
r   r  c          	      2     j         rt          d           j        dk    r0 _        	                                d _        S # d _        w xY w j        dk    rdddddiddifd	 fd
}t          j        |d          }|                                 |                                r@|	                    d            j         rt          d          |                                @d         d         d         S ddg dddiddiddidt          j
                    ifd fd fd fd}t          t          j        dd                    }|dk    rK j        rDt           j                  r0t          d          }t                               d j                   n`t%          d                     dg           D                       dz  }|d k    rt)          |d!          }n|d"k    rt)          |d#          }n|}t          j        |d          }|                                 t          j
                    }	d$}
|                                ru|	                    d           t          j
                    }||	z
  |
k    r3|}	t+          |d         z
            }                     d%| d&           t          j
                    d         z
  }||k    rIt%          d'                     dg           D                       dz  }t                               d(||                    d)d*          |d+                                d,t+          |           d-                    d)d*           d.|d+d/           	                     d          }|                     |d01           n# t4          $ r Y nw xY w	                      d21           n# t4          $ r Y nw xY wt          j
                    d<                        d3t+          |           d4            j         r	  j        d5k    r. j                                                                           n.                    d          }|                     |d61           n# t4          $ r Y nw xY wt          d7          |                                ud         d         rtt?           d8d9          pd9                                 pd}tC                              d:          pg           }|rd;	                    |dd<                   }tE          |          d<k    r|d=tE          |          d<z
   d>z  }d?| d@}|pd9|z   }	  #                    |           n# t4          $ r Y nw xY wt                               dA|tE          |pd9          d                    n1t                               dBtE          |pd9          d                    tI          dC|ddD          }tI          dEt?           d)d*          tI          dF|dGH          gdI          S d         d         S )Ju  Streaming variant of _interruptible_api_call for real-time token delivery.

        Handles all three api_modes:
        - chat_completions: stream=True on OpenAI-compatible endpoints
        - anthropic_messages: client.messages.stream() via Anthropic SDK
        - codex_responses: delegates to _run_codex_stream (already streaming)

        Fires stream_delta_callback and _stream_callback for each text token.
        Tool-call turns suppress the callback — only text-only final responses
        stream to the consumer.  Returns a SimpleNamespace that mimics the
        non-streaming response shape so the rest of the agent loop is unchanged.

        Falls back to _interruptible_api_call on provider errors indicating
        streaming is not supported.
        z+Agent interrupted before streaming API callr  Nr  r7  doneFrr  c                  f     d         s%r%d d<   	               d S # t           $ r Y d S w xY wd S d S Nr  Tr   r  r  s   r   _fire_firstz>AIAgent._interruptible_streaming_api_call.<locals>._fire_first  sq    (0 ^ 04%f-&((((($   	      
 
,,c                     	 ddl m} m}m}m}                     dd          }                    dd             | |          }	  |j        di }n)# t          $ r} ||          r ||            d }~ww xY wfd}fd}	fd}
 ||                                r|nd |	j	        sj
        r|
nd fd	
          d<   d S # t          $ r}|d<   Y d }~d S d }~ww xY w)Nr   )r<  r=  r>  stream_converse_with_callbacksr@  r  rA  c                 P                                       |            dd<   d S )NTrr  )r  )r  r  deltas_were_sentr   s    r   _on_textzRAIAgent._interruptible_streaming_api_call.<locals>._bedrock_call.<locals>._on_text/  s3    #//55526(///r   c                 F                                       |            d S r   )r~  )r   r  r   s    r   _on_toolzRAIAgent._interruptible_streaming_api_call.<locals>._bedrock_call.<locals>._on_tool4  s(    #33D99999r   c                 F                                       |            d S r   )r  )r  r  r   s    r   _on_reasoningzWAIAgent._interruptible_streaming_api_call.<locals>._bedrock_call.<locals>._on_reasoning8  s(    #22488888r   c                       j         S r   r  r%   s   r   <lambda>zRAIAgent._interruptible_streaming_api_call.<locals>._bedrock_call.<locals>.<lambda>A  s
    43L r   )on_text_deltaon_tool_starton_reasoning_deltaon_interrupt_checkr*  r  r   )rD  r<  r=  r>  r  r<  converse_streamr   r  r  r  )r<  r=  r>  r  r4  r  rH  rI  r  r  r  ru  r  r?  r  rL  r   s               r   _bedrock_callz@AIAgent._interruptible_streaming_api_call.<locals>._bedrock_call  s   ((            (^^,@+NNFNN#94@@@88@@F'=v'='K'K
'K'K$    54\BB >55f===7 7 7 7 7 7 7
: : : : : :9 9 9 9 9 9 *H)G$262L2L2N2N&XhhTX&.<@<S+{W[Wq+{==w{+L+L+L+L* * *F:&&& ! ( ( (&'F7OOOOOOO(s=   AC A C 
A;A66A;;AC 
C)C$$C)TrN  rO  r   z)Agent interrupted during Bedrock API callr  r*  )r*  r  partial_tool_namesr  rf  c                  f     d         s%r%d d<   	               d S # t           $ r Y d S w xY wd S d S r  r  r  s   r   _fire_first_deltazDAIAgent._interruptible_streaming_api_call.<locals>._fire_first_deltaY  sq    $V,  ,0!&)"N$$$$$    DD	   r  c                     ddl } t          ,j        ,j                  }||n!t	          t          j        dd                    }||}nft	          t          j        dd                    }|dk    r>,j        r7t          ,j                  r#|}t          
                    d,j        |           i 'dd	di|                     d
||d
          d},                    d|          *d<   t          j                    )d<   ,                    d            *d         j        j        j        d3i |},                    t'          |dd                     ,                    t'          |dd                     g }i }t+                      }i }	i }
d}d}d}g }d}|D ]}t          j                    )d<   ,                    d           ,j        r nq|j        s=t1          |d          r|j        r|j        }t1          |d          r|j        r|j        }||j        d         j        }t1          |d          r|j        r|j        }t'          |dd          pt'          |dd          }|r4|                    |            &             ,                    |           |r|j        r|                    |j                   |s* &             ,                    |j                   d(d<   nM,j        rF	 ,                    |j                   ,                     |j                   n# tB          $ r Y nw xY w|r|j"        r|j"        D ]}|j#        |j#        nd}|j$        pd}||
vr||
|<   |r)||	v r%||	|         k    rtK          |d          dz   }||
|<   |r||	|<   |
|         }||vr|j$        pddddddd ||<   ||         }|j$        r
|j$        |d!<   |j&        rM|j&        j'        r|j&        j'        |d         d"<   |j&        j(        r |d         d#xx         |j&        j(        z  cc<   t'          |d$d          }|,t1          |d%          r|j)        pi *                    d$          }|)t1          |d&          r|+                                }||d$<   |d         d"         }|rS||vrO|,                    |            &             ,-                    |           +d'                             |           |j        d         j.        r|j        d         j.        }t1          |d          r|j        r|j        }d/                    |          pd}d}d(}|rg }ta          |          D ]}||         }|d         d#         }|d         d"         pd)} |rZ|1                                rF	 te          j3        |           n0# td          j4        $ r tk          ||           }!|!d*k    r|!}nd}Y nw xY w|                    tm          |d!         |d+         |*                    d$          tm          |d         d"         |          ,                     |pd-}"|rd.}"d/                    |          pd}#tm          ||||#/          }$tm          d|$|"0          }%tm          d1to          tq          j9                              z   ||%g|2          S )4z#Stream a chat completions response.r   Nr  r  HERMES_STREAM_READ_TIMEOUT      ^@uD   Local provider detected (%s) — stream read timeout raised to %.0fsTinclude_usage      >@)connectrt  r   r  )r   stream_optionsr  chat_completion_stream_requestr:  r  rf  z)waiting for provider response (streaming)r*  r  r  r  r5  r  r  rr  r   rQ  rw  r   r   r  )r*  r   r   extra_contentr*  r   r   r  model_extra
model_dumpr  FrB  rE  r   )r*  r   r  r   rc  lengthr+  r)  r   r  rB  r  ra  zstream-r*  r  choicesr5  r   ):rf  r-   r  r  r2  r   r=  r   rV   rS  r   Timeoutr  r  r  rF  rG  r  r  r   r  r  r  r  r  r5  r  r   r  r)  r  r  rd  r   r   rB  r*  r   r   r   r   r  r   r  r.  r~  ra  r;  r  r   r   r   rV  rd  r   r   r  r  )-ro  _provider_timeout_cfg_base_timeout_stream_read_timeoutstream_kwargsr   content_partstool_calls_acctool_gen_notified_last_id_at_idx_active_slot_by_idxra  
model_namer+  r  	usage_objchunkr  r  tc_deltaraw_idxdelta_idnew_slotr"  r  r<  r   full_contentmock_tool_callshas_truncated_tool_argsr   r   r  repairedeffective_finish_reasonfull_reasoningmock_messagemock_choicer  r?  r  last_chunk_timerK  rL  r   s-                                         r   _call_chat_completionszIAIAgent._interruptible_streaming_api_call.<locals>._call_chat_completionsa  s   """" %APTPZ$[$[! )4 &%29%96BBCC  %0'<$$',RY7SUZ-[-['\'\$
 (500T]0GXY]YfGgGg0+8(LL^';  

#2D"9!>> -'	 *  	
 
 
M /3.P.P7( /Q / /!(+ $(9;;OC   !LMMML*849EL]]}]]F
 %%gfj$&G&GHHH //
D0Q0QRRR"$M#%N%(UU
 %'O(* MJD$&OI {, {,'+y{{$$$%@AAA, E} ug.. 15; 1%*[
ug.. 05; 0$)K	a(.5'** -u{ -!&J ")0CT!J!J!ogV[]hjnNoNo! ?#**>:::%%'''..~>>>  %U] %!((777) %))+++//>>>26(//  5 %% $ : :5= I I I $ D DU] S S S S#, % % % $%  ?FU- ?F$)$4 >F >F4<N4N(..TU#+;#4" #*===;B/8$D '? : : (OG,D D D'*>2'F'F'F'JH;C/8# @7?OG41':n44&.k&7R(257b,I,I15	3 3N3/ !/s 3#; 6*2+E$K#, ^'05 	S =E<M<Rj 1& 9'0: ^ %j 1+ > > >(BSB] ] > > > '/4 H H =WX}-M-M=%-%9%?R$D$D_$U$UE ,&ul;; ;(-(8(8(:(:5:E/2$Z08 FC/@$@$@-11#666--/// 77=== ##78??EEE=#1 C$)M!$4$BM 5'** ,u{ , %I 77=119TL"O&+#  "$!.11  C',B ":{ ;I ":v 6 =#I  ?Y__%6%6 ?? Jy1111#3 ? ? ? (C9i'X'XH'4//,4		 ;? 7? $**?d8Z&(ff_&=&=!0!#J!7&/" " "	, , ,     '4&=v#& 3*2'WW_55=N*$*"0	  L *$5  K
 #s4:<<000 $	   s$   ;4L00
L=<L=6W*W87W8c                  d   d} t          j                     d<    j        j        j        di 5 }|D ]O}t          j                     d<                       d           j        r nt          |dd          }|dk    r]t          |dd          }|rIt          |dd          dk    r4d	} t          |d
d          }|r 
                                 |           |dk    rt          |dd          }|rt          |dd          }|dk    r;t          |dd          }|r&| s$ 
                                 |           d	d<   |dk    r2t          |dd          }	|	r 
             	                    |	           Q|
                                cddd           S # 1 swxY w Y   dS )a8  Stream an Anthropic Messages API response.

            Fires delta callbacks for real-time token delivery, but returns
            the native Anthropic Message object from get_final_message() so
            the rest of the agent loop (validation, tool extraction, etc.)
            works unchanged.
            Frf  r  r   Ncontent_block_startcontent_blocktool_useTr   content_block_deltar  
text_deltar  r   rr  thinking_deltar  r   )r  r  r'  r   r  r  r   r~  r  r  get_final_message)has_tool_user   r  r  r  r  r  
delta_typer  thinking_textr  r?  r  r  r   s             r   _call_anthropiczBAIAgent._interruptible_streaming_api_call.<locals>._call_anthropica  sc    !L $(9;;OC 7'07EE*EE *2# &N &NE ,09;;OC((()DEEE0 !(!=!=J!%::: ' E E  GWUFD%A%AZ%O%O+/L(/vt(D(DI( G 1 1 3 3 3 $ ; ;I F F F#'<<< 'w = =  N)0)E)EJ)\99'.ufb'A'A#' !C !C$5$5$7$7$7$($;$;D$A$A$A>B$4U$;!+/?!?!?07z20N0N#0 !N$5$5$7$7$7$($>$>}$M$M$M //11U*2 *2 *2 *2 *2 *2 *2 *2 *2 *2 *2 *2 *2 *2 *2 *2 *2 *2s   E'F%%F),F)c                  `   dd l } t          t          j        dd                    }	 t	          |dz             D ]}j        rt          d          	 j        dk    r"                                              d<   n             d<    	                    d          }|
                    |d	
           d S d S # t          $ r)}t          || j        | j        | j        f          }t          || j        | j        t$          f          }d         rkt'          	                    d                    }d}|sg|seddlm}	 t          ||	          rOt-          |dd           s>t/          |                                          d}
t3          fd|
D                       }|p|p|}|o|o||k     }|sWt4                              d|           |d<   Y d }~ 	                    d          }|
                    |d	
           d S d S t4                              d|dz   |dz   t;          |          j        |           	                     d           n# t          $ r Y nw xY w	                                   n# t          $ r Y nw xY wg d<   dd<   dd<   !                    dt;          |          j         d|dz    d|dz    d           "                    d|dz    d|dz    dt;          |          j                    	                    d          }|
                    |d
           d d<   	 #                    d
           n# t          $ r Y nw xY w!                    d           Y d }~Jd}|sg|seddlm}	 t          ||	          rOt-          |dd           s>t/          |                                          d}t3          fd |D                       }|s|s|r\||k     rt4                              d!|dz   |dz   t;          |          j        |           !                    d"t;          |          j         d|dz    d|dz    d           "                    d|dz    d|dz    d#t;          |          j                    	                    d          }|
                    |d$
           d d<   	 #                    d%
           n# t          $ r Y nw xY w!                    d           Y d }~܉!                    d&|dz    d'           t4                              d(|dz   |           nbt/          |                                          }d)|v od*|v }|rd+_$        %                    d,           t4                              d-|           |d<   Y d }~ 	                    d          }|
                    |d	
           d S d S d }~ww xY wnM# t          $ r@}|d<   Y d }~	                    d          }|
                    |d	
           d S d S d }~ww xY w	 	                    d          }|
                    |d	
           d S d S # 	                    d          }|
                    |d	
           w w xY w).Nr   HERMES_STREAM_RETRIESr6  r   z%Agent interrupted before stream retryr  r*  r  stream_request_completer  rr  r  F)APIErrorr  )
connection lostconnection resetconnection closedzconnection terminatednetwork errornetwork connection
terminatedzpeer closedzbroken pipezupstream connect errorc              3       K   | ]}|v V  	d S r   r   )r   phrase_err_lower_previews     r   r   zKAIAgent._interruptible_streaming_api_call.<locals>._call.<locals>.<genexpr>  sE       C& C&,2 )/2D(DC& C& C& C& C& C&r   z9Streaming failed after partial delivery, not retrying: %sr  zStreaming attempt %s/%s died mid tool-call (%s: %s) after user-visible text; retrying silently to avoid losing the action. Preamble will re-stream.u9   

⚠ Connection dropped mid tool-call; reconnecting…

r  u)   ⚠️ Connection dropped mid tool-call (u   ). Reconnecting… (attempt r  r  zstream retry z mid tool-call after stream_mid_tool_retry_cleanup"stream_mid_tool_retry_pool_cleanupu    🔄 Reconnected — resuming…c              3       K   | ]}|v V  	d S r   r   )r   r  _err_lower_sses     r   r   zKAIAgent._interruptible_streaming_api_call.<locals>._call.<locals>.<genexpr>F  s<       7" 7"(. %+n$<7" 7" 7" 7" 7" 7"r   zJStreaming attempt %s/%s failed (%s: %s), retrying with fresh connection...u'   ⚠️ Connection to provider dropped (z after stream_retry_cleanupstream_retry_pool_cleanupu(   ❌ Connection to provider failed after uM    attempts. The provider may be experiencing issues — try again in a moment.z5Streaming exhausted %s retries on transient error: %sr   znot supportedTu   
⚠  Streaming is not supported for this model/provider. Switching to non-streaming.
   To avoid this delay, set display.streaming: false in config.yaml
z$Streaming failed before delivery: %s)&rf  r   r   r=  rZ  r  r  r  r  r   r  r   r!   r  ConnectTimeoutPoolTimeoutr  r  r  r   r   r  r   r   r  r   rS  rT  r  r   r'   r  rh  r  r  r  _disable_streamingr  )ro  _max_stream_retries_stream_attemptrJ  ru  _is_timeout_is_conn_err_partial_tool_in_flight_is_sse_conn_err_preview	_APIError_SSE_PREVIEW_PHRASES_is_transient_can_silent_retrystale_is_sse_conn_err_SSE_CONN_PHRASES
_err_lower_is_stream_unsupportedr  r  r  r  r  r  rK  rL  r   s                     @@r   rM  z8AIAgent._interruptible_streaming_api_call.<locals>._call  s6
   """""%bi0G&K&K"L"L@h',-@1-D'E'E u uO 0 X./VWWWl=,@@@ JJLLL1@1B1BF:..1G1G1I1IF:.\ "7!:!:8!D!D!-55nMf5ggggg .-] % e e e&0 2F4I6K]^' ' (2 3V5OQ`a( (  ,E2 g%6: &

+? @ @7 73 8=4#. &| & H H H H H H#-a#;#; !&GA}^bDcDc !&9<Q$6<&$8 @C C& C& C& C&6JC& C& C& @& @&$<
 !, W| W?W * !8 !J$1!J$36I$I .
 $5 ' !'$_ab!" !" !" 34w &Z "7!:!:8!D!D!-55nMf5ggggg .-Q #KK!; !0! 3 3a 7 $Q 0 !	 	 	% $ 7 7%:!" !" !" !" $- % % % $%% $ D D F F F F#, % % % $%
 <>F#786;,U38=-f5 --!]$(GG$4!] !],;a,?!] !]BUXYBY!] !] !]  
 !00!J!0C !J !JFY\]F] !J !J7;Aww7G!J !J   %:$=$=h$G$GE$0 $ A A$)2Q !B !" !" !" CG 5h ?% $ C C+O !D !" !" !" !" $- % % % $% --.PQQQ$HHHH ,1(* "< "DDDDDD)!Y77 "=Z^@_@_ "14Q5" 1 47 7" 7" 7" 7"2C7" 7" 7" 4" 4" 0
 ' B, B:J B  /1DDD &%H$3a$7$7!$;$(GG$4$%!" !" !" !% 1 1%a(,Q(8%a %a0?!0C%a %aFY\]F]%a %a %a!" !" !"
 !% 4 4%@Oa4G %@ %@J]`aJa %@ %@-1!WW-=%@ %@!" !" !"
 )>(A(A((K(K#(#4$($E$E(-6L %F %& %& %& GK$9($C!)$($G$G/J %H %& %& %& %& (1 !) !) !)$(D!) $ 1 12T U U U ( --!9#6#:!9 !9 !9   #NN W 3a 7 !    *-QJ (J 6 !B$3z$A 3  6 ":> 7 $ 0 0%7!" !" !" #KK F !   +,w "7!:!:8!D!D!-55nMf5ggggg .-]e!ul $    #$w!6!:!:8!D!D!-55nMf5ggggg .-muz "7!:!:8!D!D!-55nMf5ggggg .- "7!:!:8!D!D!-55nMf5gggg .s  +W< :CW< W8DW3W< 6W3	IW3
I,)W3+I,,W30JW3
JW3JB2W3MW3
M)&W3(M))W3W< EW3S.-W3.
S;8W3:S;;W3W< B#W3<W< 3W88W< ;Y< <
YYY< YY< <1Z-HERMES_STREAM_STALE_TIMEOUTg     f@r  u>   Local provider detected (%s) — stale stream timeout disabledc              3   N   K   | ] }t          t          |                    V  !d S r   r  r  s     r   r   z<AIAgent._interruptible_streaming_api_call.<locals>.<genexpr>  s.      RRac#a&&kkRRRRRRr   r'  r  r   r  r!  g      n@r  zwaiting for stream response (zs, no chunks yet)c              3   N   K   | ] }t          t          |                    V  !d S r   r  r  s     r   r   z<AIAgent._interruptible_streaming_api_call.<locals>.<genexpr>  rQ  r   uq   Stream stale for %.0fs (threshold %.0fs) — no chunks received. model=%s context=~%s tokens. Killing connection.r  r  rI  rR  z
s (model: z, context: ~z tokens). Reconnecting...stale_stream_killr  stale_stream_pool_cleanupzstale stream detected after zs, reconnectingr  stream_interrupt_abortz+Agent interrupted during streaming API callr  r   r  r@  rm  z, +z moreu$   

⚠ Stream stalled mid tool-call (zH); the action was not executed. Ask me to retry if you want to continue.z[Partial stream dropped tool call(s) %s after %s chars of text; surfaced warning to user: %szPartial stream delivered before error; returning stub response with %s chars of recovered content to prevent duplicate messages: %sr  r  zpartial-stream-stubr   rc  r  r  )%r  r  r  r;  r_  r   r  r  rW  r;  r  r2  r   r=  r   rV   rS  r   r"  r   r   r   r  rT  r  r  r   r  r  r  r5  r   r   r  r   r  r   )r   r?  r  r  rf  rM  _stream_stale_timeout_base_stream_stale_timeout_est_tokens_last_heartbeat_HEARTBEAT_INTERVAL_hb_now_waiting_secs_stale_elapsedr]  r^  rJ  _partial_text_partial_names	_name_str_warn	_stub_msgr  r  r  r  r  r  r  rK  rL  s   ```                   @@@@@@@@@r   !_interruptible_streaming_api_callz)AIAgent._interruptible_streaming_api_call  s   $ $ 	R"#PQQQ=---
 *8D&233J??-1**T*1111 =..."&66F!' %u~     )( )( )( )( )( )( )( )( )(V  dCCCAGGIII**,, Xs###, X*+VWWW **,, X g*Wo%*%%"TLL!)4 0#UO!5> 	,	 	 	 	 	 	~	 ~	 ~	 ~	 ~	 ~	 ~	 ~	 ~	 ~	 ~	@7	2 7	2 7	2 7	2 7	2 7	2 7	2 7	2 7	2rE	h E	h E	h E	h E	h E	h E	h E	h E	h E	h E	hN &+295RTY+Z+Z%[%[" &..4=.EVW[WdEeEe.$)%LL!LLY[_[hiiii RR:>>*b3Q3QRRRRRVWWKW$$(+,F(N(N%%v%%(+,F(N(N%%(B%E$777				)++"jjll C	VFF3F ikkG(,???") #Goc.B$B C C$$TMTTT   "Y[[?3+??N 555SSJNN:r4R4RSSSSSWXXG"$9NN7I668	   !!'C<O<O ' ')~~gyAA' '!)7' ' '  .228<<B~99"EX9YYY    D77?Z7[[[[    D (,y{{$$$W3~3F3FWWW   ( V	}(<<<.44666668888)>)B)B8)L)L)5 ==nUm=nnn    D&'TUUUG jjll C	VH '?&& > D"DbIIOR%''!"!  "&fjj1E&F&F&L"!M!M!  $		.!*< = =I>**Q..!%I3~+>+>+B%I%I%II	D%D D D 
 &3%8bE$AM//6666$   NN@&M,?R(@(@&/    NN1 M/R00w   ,$m&*  	 ',!$;;,&        /!j!!sT   A 	A<.O+ +
O87O8<P 
P P 'A'S 
SS9W 
WWzFailoverReason | Nonec                    |t           j        t           j        fv rt          t	          | dd                    }t	          | dd          pd                                                                }| j        pi                     d          pd                                                                }|r|r!||k    rt          j
                    dz   | _        | j        t          | j                  k    rdS | j        | j                 }| xj        dz  c_        |                    d          pd                                                                }|                    d          pd                                }|r|s|                                 S 	 dd	lm} |                    d
          pd                                pd}	|                    d          pd                                pd}
|
sT|                    d          pd                                }|r)t%          j        |d                                          pd}
|	r(t)          |	d          r|
st%          j        d          pd}
 |||d|	|
          \  }}|)t+          j        d|           |                                 S 	 ddlm}  |||          }n# t2          $ r Y nw xY wd}t5          |j                  }|                     |          }|dk    rd}n|dk    s:|                    d                                                              d          rd}nq|rd}nl|                     |          rd}nT|                      ||          rd}n:|dk    s2tC          |          "                    d          rt)          |d          rd}| j#        }|| _#        || _$        || _        || _%        tM          | d           r| j'        (                                 d| _)        tU          ||          }|dk    rdd!l+m,}m-}m.} |dk    r|j/        p |            pdn|j/        pd}|| _/        || _0        || _1         ||| j1        |"          | _2        |dk    r ||          nd| _3        d| _4        i | _5        n}|j/        | _/        || _4        t	          |d#d          }|st	          |d$d          }|j/        |d%|rd$tm          |          ini | _5        | || j5        d&<   | 7                    d'(           | 8                    ||||)          \  | _9        | _:        | ;                                 tM          | d*          r}| j<        rvdd+l=m>}  || j#        | j        | j/        | j$        t	          | d,d          -          }| j<        ?                    | j#        || j        t	          | dd          | j$        .           | @                    d/| d0|            t+          jA        d1|||           dS # t2          $ r4}t+          jB        d2||           |                                 cY d}~S d}~ww xY w)3u  Switch to the next fallback model/provider in the chain.

        Called when the current model is failing after retries.  Swaps the
        OpenAI client, model slug, and provider in-place so the retry loop
        can continue with the new backend.  Advances through the chain on
        each call; returns False when exhausted.

        Uses the centralized provider router (resolve_provider_client) for
        auth resolution and client construction — no duplicated provider→key
        mappings.
        r2  Fr  r   rC  r   r  r   r"  r   Nr  key_envz
ollama.comOLLAMA_API_KEYTr/  z.Fallback to %s failed: provider not configured)r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   _custom_headersr  r  r  fallback_timeout_applyr  r  r  r  r3  )r   r  r  r  r  u5   🔄 Primary model failed — switching to fallback: z via u"   Fallback activated: %s → %s (%s)z"Failed to activate fallback %s: %s)CrE   r$  r#  r   r   r   r  rP  r   r  	monotonic_rate_limited_untilr   r   r  _try_activate_fallbackr  r#  r   r=  rw   r   rT  r  r  r   r   r   r  r  r[  r  r  rx   r  r  r  r  r  r  r  r2  r-   r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r4  r  r7  r  r8  r  r  r  )r   r6  fallback_already_activecurrent_providerprimary_providerrv  fb_providerfb_modelr#  fb_base_url_hintfb_api_key_hint
fb_key_env	fb_client_resolved_fb_modelr  fb_api_modefb_base_url_fb_is_azurer  _fb_timeoutr  r  r  r^  
fb_headersr  fb_context_lengthru  s                               r   r  zAIAgent._try_activate_fallbackC  s    n/1GHHH '+749NPU+V+V&W&W# 'j" = = CJJLLRRTT!%!6!<" A A* M M SQSZZ\\bbdd+ A1A AFVZjFjFj+/>+;+;b+@(3t';#<#<<<5!$"67!vvj))/R6688>>@@FF7OO)r0022 	1( 	1..000
`	1FFFFFF !#z 2 2 8b??AAIT!vvi006B==??G4O" P ffY//52<<>>
 P&(i
B&?&?&E&E&G&G&O4O   F$9:JL$Y$Y Fbq F"$),<"="="E,C,C8t"2!0-2 -2 -2)I)  D! ! ! 22444SSSSSS77+NN    -Ki011K44[AALn,,/++{/A/A#/F/F/L/L/N/N/W/WXd/e/e+2 1 1++K88 1/<<$ =   1 0	))!+..99:LMM *)+GG * 1
I!DJ'DM'DM'DMt/00 .%++---'+D$
 7{HMMK222ttttttttttZeitZtZt!2!U6M6M6O6O!USU  |E  |M  |S  QS,*7'+6()?)?!4#;[* * *& NY\gMgMg??=+I+I+Imr("&(##  )0' %Y0A4HH
! M!(4Et!L!LJ(0 +' ' AKR)4
+;+;<<PR'#
 *5@D'	2 77?W7XXX 33((("	 4   DD$d&C 00222 t122 t7N IIIIII$<$<J L4=*1$8PRV*W*W% % %!
 '44*#4!]#D)R88!] 5    00 0"-0 0   L48[   4 	1 	1 	1M>!LLL..00000000	1sD   DX 0K X 
KX KM
X 
Y&)YYYc                 z   | j         sdS t          | dd          t          j                    k    rdS | j        }	 |d         | _        |d         | _        |d         | _        |d         | _        t          | d          r| j
                                         |d	         | _        t          |d
                   | _        |d         | _        |                    d| j        dk    o
| j        dk              | _        | j        dk    rlddlm} |d         | _        |d         | _         ||d         |d         t-          | j        | j                            | _        |d         | _        d| _        n0|                     t          |d
                   dd          | _        | j        }|                    |d         |d         |d         |d         |d                    d| _         d| _        t=          j        d| j        | j                   dS # t@          $ r }t=          j!        d|           Y d}~dS d}~ww xY w) a  Restore the primary runtime at the start of a new turn.

        In long-lived CLI sessions a single AIAgent instance spans multiple
        turns.  Without restoration, one transient failure pins the session
        to the fallback provider for every subsequent turn.  Calling this at
        the top of ``run_conversation()`` makes fallback turn-scoped.

        The gateway caches agents across messages (``_agent_cache`` in
        ``gateway/run.py``), so this restoration IS needed there too.
        Fr  r   r  r  r   r  r  r  r  r  r  r  r  r1  r  r  r   r  Nrestore_primaryTr5  r  r  r  r  r  r  z.Primary runtime restored for new turn: %s (%s)z%Failed to restore primary runtime: %s)"r2  r   r  r  rP  r  r  r   r  r  r  r  r  r   r  r  r   r  r  r  r  r  r-   r  r  r  r  r  r8  r   r   r  r   rT  )r   rtr  r  ru  s        r   _restore_primary_runtimez AIAgent._restore_primary_runtime	  s    ' 	54.22T^5E5EEE5"9	GDJzNDMzNDMzNDMt/00 .%++---i=DL"&r/':";";D')*>'?D$ -/FF)!55V$-;:V- -D) } 444JJJJJJ*,-@*A'+-.B+C()?)?*+R0D-E8
SS* * *& ,..B+C(""88O,--, 9   (BOO+,!"=>12/012     (-D$#$D L@
DM   4 	 	 	OCQGGG55555	s   GH 
H:H55H:>   r  r  r  r  APITimeoutErrorAPIConnectionErrorr  	api_errorretry_countmax_retriesc          
         | j         rdS t          |          j        }|| j        vrdS |                                 rdS | j        pd                                                                }|dv rdS 	 t          | dd          /	 | 	                    | j
        dd           n# t          $ r Y nw xY w| j        }t          |d	                   | _        |d
         | _        |d         | _        |d         | _        |d         | _        t%          | d          r| j                                         |d         | _        | j        dk    rlddlm} |d         | _        |d         | _         ||d         |d         t5          | j        | j                            | _        |d         | _        d| _
        n0|                     t          |d	                   dd          | _
        t=          d|z   d          }|                     | j          d| d| j         d| dd           tC          j"        |           dS # t          $ r }	tG          j$        d|	           Y d}	~	dS d}	~	ww xY w)u  Attempt one extra primary-provider recovery cycle for transient transport failures.

        After ``max_retries`` exhaust, rebuild the primary client (clearing
        stale connection pools) and give it one more attempt before falling
        back.  This is most useful for direct endpoints (custom, Z.AI,
        Anthropic, OpenAI, local models) where a TCP-level hiccup does not
        mean the provider is down.

        Skipped for proxy/aggregator providers (OpenRouter, Nous) which
        already manage connection pools and retries server-side — if our
        retries through them are exhausted, one more rebuilt client won't help.
        Fr   )r  znous-researchr  Nprimary_recoveryTr5  r  r  r  r   r  r  r  r  r   r1  r  r  r   r  rm  r   u   🔁 Transient z on u    — rebuilt client, waiting z"s before one last primary attempt.r  z%Primary transport recovery failed: %s)%r2  r   r'   _TRANSIENT_TRANSPORT_ERRORSr  r  r   r  r   r  r  r   rP  r   r  r  r   r  r  r  r  r  r  r  r  r  r-   r  r  r  r  r  r  r  sleepr   rT  )
r   r+  r,  r-  
error_typer6  r'  r  	wait_timeru  s
             r   _try_recover_primary_transportz&AIAgent._try_recover_primary_transport^  s    # 	5 )__-
T===5 ""$$ 	5--24466<<>>66650	tXt,,8--,>t .     !   D &B"&r/':";";DGDJzNDMzNDMzNDMt/00 .%++---i=DL} 444JJJJJJ*,-@*A'+-.B+C()?)?*+R0D-E8
SS* * *& ,..B+C(""88O,--- 9   AOQ//ILL? Y Y: Y Y4= Y Y+4Y Y Y    
 Jy!!!4 	 	 	OCQGGG55555	s=   3H; B# "H; #
B0-H; /B00F	H; ;
I%I  I%c                     t          | t                    sdS | D ]1}t          |t                    r|                    d          dv r dS 2dS )NFr   >   r  r  T)r!   r  r   r   )r)  r-  s     r   _content_has_image_partsz AIAgent._content_has_image_parts  s^    '4(( 	5 	 	D$%% $((6*:*:>Z*Z*Zttur   r  c                 f   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   t          |j                  }t          |          |fS )Nr   rI  
image/jpegdata:;r   r   image/.png.gif.webp.jpg)	image/png	image/gif
image/webpr8  	image/jpganthropic_image_Fr  r5  delete)r   	partitionr  r   r"  r   r   tempfileNamedTemporaryFiler   base64	b64decoder   r   )	r  headerrb  r   mime	mime_partr5  tmpr  s	            r    _materialize_data_url_for_visionz(AIAgent._materialize_data_url_for_vision  sq   io2..88==4W%% 	!s7||}}-33C;;A>DDFFI##H-- ! ! 
 
 #dF

 	 )1CF[`aaa 	. 	.IIf&t,,---	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.CH~~4yy$s   (DD
Dr+  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 )Nr   rL  r  ztool result)r  r>  r  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.r9  r   )vision_analyze_tool)r  user_promptanalysiszImage analysis failed: zImage analysis failed.z[The z- attached an image. Here's what it contains:
rO  z@
[If you need a closer look, use vision_analyze with image_url: )hashlibsha256r   ri  	hexdigestr  r   r  rP  tools.vision_toolsrR  asynciorv  r!   r   r   r   r   ra  unlinkr   )r   r  r+  	cache_keycached
role_labelanalysis_promptvision_sourcecleanup_pathr  rR  result_jsonrL  ru  notes                  r   &_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} t          | dd          pd                                }t          | dd          pd                                }|r|sdS  |||          }|dS t	          |j                  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).
        r   )get_model_capabilitiesr  r   r  F)agent.models_devre  r   r   r   supports_visionr   )r   re  r  r  capss        r   _model_supports_visionzAIAgent._model_supports_vision  s    	??????j"55;BBDDHT7B//52<<>>E 5 u))(E::D|u,--- 	 	 	55	s   AA< A< (A< <
B
	B
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 )Nr   >   r  
input_textr  r   >   r  r  r  r  z:[An image was attached but no image source was available.]r  c              3      K   | ]}||V  	d S r   r   )r   rb  s     r   r   z8AIAgent._preprocess_anthropic_content.<locals>.<genexpr>7  s'      BBdTBTBBBBBBr   r   c              3      K   | ]}||V  	d S r   r   )r   r  s     r   r   z8AIAgent._preprocess_anthropic_content.<locals>.<genexpr>8  s'      ??D$?4??????r   zI[A multimodal message was converted to text for Anthropic compatibility.])	r6  r!   r   r   r   r   r   rc  r;  )r   r)  r+  
text_partsimage_notesr-  r  r  
image_datar  r  r5  s               r   _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Zr   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.
        r  Nr   )get_transport)r  r   r  r   agent.transportsrs  )r   r  modecacherf  rs  s         r   r  zAIAgent._get_transportA  sw     (4=0$77=E$)D!IIdOO9666666d##AE$Kr   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r!   r   r6  r   r   r,  r   s     r   r   z>AIAgent._prepare_anthropic_messages_for_api.<locals>.<genexpr>U  `       
 
 sD!!Wd&C&CCGGIDVDV&W&W
 
 
 
 
 
r   r)  r+  r  	r   ri  r  rP  r!   r   rq  r   r   r   rw  transformedr,  s   `   r   #_prepare_anthropic_messages_for_apiz+AIAgent._prepare_anthropic_messages_for_apiS  s     
 
 
 
#
 
 
 
 
 	    &&(( 	  mL11 	 	Cc4(( !??	""CGGFF++5v66 C	NN r   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 rz  r{  r|  s     r   r   zAAIAgent._prepare_messages_for_non_vision_model.<locals>.<genexpr>x  r}  r   r)  r+  r  r~  r  s   `   r   &_prepare_messages_for_non_vision_modelz.AIAgent._prepare_messages_for_non_vision_modelo  s      
 
 
 
#
 
 
 
 
 	   &&(( 	 mL11 
	 
	Cc4(( 
 "??	""CGGFF++5v66 C	NN r   c                    |sdS 	 ddl m n3# t          $ r&}t                              d|           Y d}~dS d}~ww xY wdd}dt
          dt          t
                   ffd	}|D ]}t          |t                    s|	                    d
          }t          |t                    sC|D ]}t          |t                    s|	                    d          }|dvr2|	                    d          }	t          |	t                    r.|		                    dd          }
 ||
          }|r
||	d<   |dz  }t          |	t
                    r ||	          }|r
||d<   |dz  }|rt                              d|dz             |dk    S )u  Re-encode all native image parts at a smaller size to recover from
        image-too-large errors (Anthropic 5 MB, unknown other providers).

        Mutates ``api_messages`` in place. Returns True if any image part was
        actually replaced, False if there were no image parts to shrink or
        Pillow couldn't help (caller should surface the original error).

        Strategy: look for ``image_url`` / ``input_image`` parts carrying a
        ``data:image/...;base64,...`` payload.  For each one whose encoded
        size exceeds 4 MB (a safe target that slides under Anthropic's 5 MB
        ceiling with header overhead), write the base64 to a tempfile, call
        ``vision_tools._resize_image_for_vision`` to produce a smaller data
        URL, and substitute it in place.

        Non-data-URL images (http/https URLs) are not touched — the provider
        fetches those itself and the size limit is different.
        Fr   )_resize_image_for_visionu6   image-shrink recovery: vision_tools unavailable — %sNi  @ r  r   c                 l   t          | t                    r|                     d          sdS t          |           k    rdS 	 |                     d          \  }}}d}|                    d          rZ|t          d          d                             dd          d                                         }|                    d          r|}ddl}|                    |          }d	d
ddddd	                    |d          }t          j        d|d          }		 |	                    |           |	                                  t          |	j                  |          }
	 t          |	j                                      d           nN# t"          $ r Y nBw xY w# 	 t          |	j                                      d           w # t"          $ r Y w w xY wxY w|
r t          |
          t          |           k    rdS |
S # t"          $ r&}t$                              d|           Y d}~dS d}~ww xY w)z8Return a smaller data URL, or None if shrink can't help.r9  NrI  r8  r:  r   r   r;  r<  r=  r>  r?  z.bmp)r@  rA  rB  r8  rC  z	image/bmphermes_shrink_FrE  )	mime_typemax_base64_bytesT)
missing_oku.   image-shrink recovery: re-encode failed — %s)r!   r   r  r   rG  r"  r   rJ  rK  r   rH  rI  r   r  r   r   rZ  r   rS  rT  )r  rL  rb  r   rM  rN  _b64r3  r5  rO  resizedr  r  target_bytess               r   _shrink_data_urlzEAIAgent._try_shrink_image_parts_in_messages.<locals>._shrink_data_url  sz   c3'' s~~g/F/F t3xx<''t#"%--"4"44#$$W-- ) &s7||}} 5 ; ;C C CA F L L N NI ++H55 )(%%%%nnT**!'fG"(vF  #dF##  1+F5  IIcNNNIIKKK66SX"&)5  GSX--->>>>$   SX--->>>>$     #g,,#c((":":4   OQTUUUtttttsz   CH A	F &(F H 
FH FH G!(G
	G

GGGG%H H 
H3H..H3r)  r   >   r  r  r  r   r   zGimage-shrink recovery: re-encoded %d image part(s) to fit under %.0f MBi   )rX  r  r   rS  rT  r   r   r!   r   r   r  r  )r   rw  r  changed_countr  r,  r)  r-  r  image_valuer  r  r  r  s               @@r   #_try_shrink_image_parts_in_messagesz+AIAgent._try_shrink_image_parts_in_messages  s$   $  	5	CCCCCCC 	 	 	NNSUXYYY55555	 '*	# *	(3- *	 *	 *	 *	 *	 *	 *	X   	+ 	+Cc4(( ggi((Ggt,,  + +!$-- (( <<<"hh{33 k400 
+%//%44C..s33G +-4E*%*S11 +..{;;G +,3[)%*'+*  	KKY|{;   q  s    
?:?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 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).
        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.r  r   >   r^  r.  r  r+  r,  r  r  Tr   	dashscopealiyuncsr+  zopencode.ai/zen/zbigmodel.cnr  )r   r  )r   bases     r   _anthropic_preserve_dotsz AIAgent._anthropic_preserve_dots  s     D*b))/R6688 =
 
 

 4j"--3::<<4 *T!*D * "T)* $	* "T)		
r   c                 ,    t          | j        d          S )z2Return True when the base URL targets Qwen Portal.r  r(  r%   s    r   _is_qwen_portalzAIAgent._is_qwen_portal   s    $T%9;KLLLr   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)  r  ry  r+  r  rQ  r   	ephemeralcache_control)r  rP  r!   r   r   r   r  r   )r   rw  preparedr,  r)  normalized_partsr-  s          r   _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r   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)  r  ry  r+  r  rQ  r   r  r  )r!   r   r   r   r  r   )r   r'  r,  r)  r  r-  s         r   #_qwen_prepare_chat_messages_inplacez+AIAgent._qwen_prepare_chat_messages_inplaceC   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	 	r   c                    | j         dk    r|                                 }|                     |          }t          | dd          }|r|j        nd}t          | dd          }|d| _        |                    | j        || j        ||n| j	        | j
        | j        |                                 |t          | dd          | j        pi                     d          dk    t          t          | dd	                    
          S | j         dk    rd|                                 }t          | dd          pd}t          | dd          }|                    | j        || j        | j	        pd||          S | j         dk    r|                                 }	t!          | j        d          pt!          | j        d          }
| j        dk    p| j        dk    od| j        v }| j        dk    p
| j        dk    }|                     |          }|	                    | j        || j        | j
        t          | dd          | j	        | j        |
|||
r|                                 nd          S |                                 }	|                                 }|                                 }t!          | j        d          pt!          | j        d          }d| j        v }d| j        v }t!          | j        d          p)t!          | j        d          pt!          | j        d          }t!          | j        d           }| j        pd!                                                                d"k    }	 d#d$lm}m}  || j        | j                  }||u }|s|nd}n# t<          $ r d	}d}Y nw xY wi }| j        r
| j        |d%<   | j         r
| j         |d&<   | j!        r
| j!        |d'<   | j"        r
| j"        |d(<   | j#        rd)|d*<   | j$        r
| j$        |d+<   d}|s|rEd,| j        pd!                                v r(	 d#d-l%m&}  || j                  }n# t<          $ r Y nw xY wd}|r*| j'        pd.tQ          tS          j*                              d/}t          | dd          }|d| _        |                     |          }  |	j        dQi d0| j        d1| d2| j        d3| j        d4| +                                d5| j	        d6|d7| j,        d8| j
        d9| j        dt          | dd          d:| j        pd!                                d;|d<|d=|d>|d?|d@|dA|dB|dC| j        dDk    dE| j-        dF|pddG|r| j.        nddH|r| j/        nddI|dJ|dK|dL| 0                                dM|r|                                 nddN|r| 1                                nddO|dP| j        S )Rz9Build the keyword arguments dict for the active API mode.r  r  N_ephemeral_max_output_tokensr  speedfastr0  F)r  r'  ro  r  r  is_oauthpreserve_dotsry  r   	fast_moder2  r  r  r  r     )r  r'  ro  r  r4  guardrail_configr  models.github.air  r  r  r  r  r  r  )r  r'  ro  r  r  r  r  is_github_responsesis_codex_backendis_xai_responsesgithub_reasoning_extranousresearchzintegrate.api.nvidia.comr  moonshot.aimoonshot.cnztokenhub.tencentmaas.comr   r  r   _fixed_temperature_for_modelOMIT_TEMPERATUREonlyrg  ordersortTrequire_parametersdata_collectionr3  )_get_anthropic_max_outputrb  )	sessionIdpromptIdr  r'  ro  r   r  r  ephemeral_max_output_tokensmax_tokens_param_fnr  r  r5  r8  is_nousis_qwen_portalis_github_modelsis_nvidia_nimis_kimiis_tokenhubis_lmstudiois_custom_providerr'  r  provider_preferencesqwen_prepare_fnqwen_prepare_inplace_fnqwen_session_metadatafixed_temperatureomit_temperaturesupports_reasoningr  lmstudio_reasoning_optionsanthropic_max_outputrT  r   )2r  r  r  r   ry  r  build_kwargsr  ro  r  r  r  r  r  r   r   rw   r   r  r  r  r  #_github_models_reasoning_extra_bodyr  r  r   r  r  r  r  r   r  r  r  r  r  r  r  r  r  r   r  r  r  rI  rL  r  r  _supports_reasoning_extra_body"_lmstudio_reasoning_options_cached)!r   rw  r  r  ctx_lenephemeral_out_btr4  r  _ctr  r  r  _msgs_for_codex_is_qwen_is_or_is_gh_is_nous
_is_nvidia_is_kimi_is_tokenhub_is_lmstudior  r  _ft
_omit_temp_fixed_temp_prefs_ant_maxr  
_qwen_meta_ephemeral_out_msgs_for_chats!                                    r   _build_api_kwargszAIAgent._build_api_kwargs_   s   =000,,..J!%!I!I,!W!Wd$8$??G07Ag,,TG#D*H$OOM(481**j+j,9,E==4?!%!61";;==& '<dCC17R<<WEEO%)'$8QSX*Y*Y%Z%Z +     =...%%''CT#4d;;J{F&A4HHI##j%j?2d!* $    =---%%''C%dm5GHH Q(8OPP  
 / +}< E,0DD	   $}5^9PT^9^"II,WWO##j(j!%!6"4t<<?"&"8$7!1!1Uh'rt'O'O'Q'Q'Qnr $    !!## ''))((**!$"68JKK T$T%9;RSS 	 "T%99/43GG
!$-@@ C$T]MBBC$T]MBB 	
 -T-AC]^^+2244::<<
J	]]]]]]]]..tz4=IIC 00J%/9##TKK 	 	 	JKKK	
 "$! 	4!3F6N! 	6#5F8 	3"2F7O 	0!/F6N+ 	0+/F'(( 	F(,(EF$%  	h 	H1Ar0H0H0J0J$J$JMMMMMM44TZ@@    
 	!_8
-- J !'EtLL%04D- DD\RRs "
 "
 "
**"
#^"
 **"
 ]]	"

 33555"
 "
 )7"
 !% 6 6"
 "22"
 #44"
 t\4888"
 )r00222"
 !&"
 H"
 $8"
  $V!"
" %*#"
$ H%"
& %'"
( %)"
*  $}88+"
,  //-"
. "(4/"
0 AIRD<<d1"
2 QY$bD$L$L^b3"
4 #-*5"
6 *k7"
8 (Z9"
:  $BBDDD;"
< RX#a4#K#K#M#M#M]a="
> Ua'jt'N'N'P'P'Pfj?"
@ "*A"
B --C"
 "	
s$   !(N
 
NN%P< <
Q	Q	c                 v   t          | j        d          rdS 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.comTr  r  r  r   github_model_reasoning_effortsFr   r  c              3   &   K   | ]}|o|d k    V  dS )r  Nr   )r   opts     r   r   z9AIAgent._supports_reasoning_extra_body.<locals>.<genexpr>'!  s+      <<s+se|<<<<<<r   r  zapi.mistral.ai)z	deepseek/z
anthropic/zopenai/zx-ai/zgoogle/gemini-2z
qwen/qwen3ztencent/hy3-previewc              3   B   K   | ]}                     |          V  d S r   )r  )r   r  r  s     r   r   z9AIAgent._supports_reasoning_extra_body.<locals>.<genexpr>7!  s1      SS5##F++SSSSSSr   )rw   r  r  r  r   r  r   r  r   r  r  r   )r   r  optsreasoning_model_prefixesr  s       @r   r  z&AIAgent._supports_reasoning_extra_body!  s    !!57IJJ 	4 !57MNN 	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> >
BB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_cacherC  ) lmstudio_model_reasoning_optionsr  r   )
r  r   r  r  r   r   r  r  r  r   )r   _timerv  r   r\  r  tsr  s           r   r  z*AIAgent._lmstudio_reasoning_options_cached9!  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     r   *_resolve_lmstudio_summary_reasoning_effortz2AIAgent._resolve_lmstudio_summary_reasoning_effort[!  sA     	EDDDDD&&!3355
 
 	
r   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  NrN  Feffortmediumxhighhighminimallow)r  r  r   r  r  r!   r   r   r   r   r  )r   r  supported_effortsrequested_efforts       r   r  z+AIAgent._github_models_reasoning_extra_bodyh!  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                 P
   t          |dd          }|                     |          }t          |          }|sO|j        pd}t	          j        d|t          j                  }|r#d                    d |D                       }|pd}|r.| j        r't          j
        dt          |           d	|            |r<| j        r5| j        s.| j        s'	 |                     |           n# t          $ r Y nw xY w|j        pd}	t!          |	          }
|rt!          |          }t#          |
t$                    r)|
r'|                     |
                                          }
d
|
||d}t          |dd          }|Dt+          |d          r4t          |dd          pi }t#          |t,                    rd|v r|d         }|t!          |          |d<   n|r|                                 r|pd|d<   d|vr|r||d<   t+          |d          r|j        r|j        }g }|D ]}t#          |t,                    r|                    |           -t+          |d          r|                    |j                   Xt+          |d          r'|                    |                                           |r||d<   t          |dd          }|r||d<   t          |dd          }|r||d<   |rCg }|D ]8}t          |dd          }t          |dd          }t#          |t$                    r|                                s|                     |          \  }}|}t#          |t$                    r|                                st#          |t$                    r)|                                r|                                }n_t          |dd          }|rt          |dd          nd}|rt          |dd          nd}|                     ||t          |                    }|                                }t          |dd          }t#          |t$                    r|                                s|                     |          \  }}|}|                     |t#          |t$                    r|nd          }||||j        |j         j!        |j         j"        dd}t          |dd          }|)t+          |d          r|                                }||d<   |                    |           :||d<   |S )zBuild a normalized assistant message dict from an API response message.

        Handles reasoning extraction, reasoning_details, and optional tool_calls
        so both the tool-call path and the final-response path share one builder.
        r   Nr   r  rO  r  c              3   f   K   | ],}|                                 |                                 V  -d S r   r  )r   bs     r   r   z3AIAgent._build_assistant_message.<locals>.<genexpr>!  s7      &T&TQ!''))&Tqwwyy&T&T&T&T&T&Tr   zCaptured reasoning (	 chars): r  )r+  r)  r  ra  r  r  r  r  __dict__r  r  r  r*  r  r   r   r   rE  rH  r  )r*  r  rH  r   r   r  )#r   r  r   r)  rX  r  rV  r;  r  r   r   r   r  r  r  r   r  r!   r   rK  r   r  r   _needs_thinking_reasoning_padr  r   r		  r  rg   rf   re   r   r   r   r   ) r   rg  ra  assistant_tool_callsr  _from_structuredr)  think_blockscombined_raw_content_san_contentr,  raw_reasoning_contentr  raw_details	preserveddcodex_itemsr  r   r   rE  r  embedded_call_idrb  _fn_fn_name_fn_argsrH  embedded_response_item_idtc_dictr<  s                                    r   _build_assistant_messagez AIAgent._build_assistant_message!  s8     ''8,MM001BCC//
  	2'/52G:&=wbiXXXL 2!;;&T&T,&T&T&TTT!)!1T 	ad2 	aM_^1D1D__~__``` 	d5 	 - d6K ++N;;;;    D
 )06B+L99 	B1.AAN lC(( 	J\ 	J33LAAGGIIL  #'*	
 
 !((9;NPT U U (W5F-V-V(!"3]DIIORK+t,, I1D1S1S(34G(H% ,';<Q'R'RC#$$! 	=d&H&H&J&J 	= (6'<C#$0 c))n)'5C#$$&9:: 	5?P?b 	5
 ,=KI  5 5a&& 5$$Q''''Q
++ 5$$QZ0000Q-- 5$$Q\\^^444 5+4'( /1H$OO 	7+6C'(
 &&79NPTUU 	=)<C%& /	+J1 ,+ ,+	 D$77!)Y==!'3// /w}} /*.*G*G*O*O'$a.G!'3// cw}} c!&#.. c6<<>> c"(,,..%iTBB?B#J73#;#;#;FI#S73T#B#B#Bt"&"="=hRUV`RaRa"b"b!--//#*96H$#O#O !"2C88 A@P@V@V@X@X A373P3PQW3X3X0A0'@$#'#J#J(23CS(I(IS$$t$ $  "&(8%N ) 2 7%.%7%A! !	 	  	?DAA$ul33 3 % 0 0 2 2/4GO,!!'**** *C
s   C% %
C21C2c                 R    |                                  p|                                 S )a	  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).
        )_needs_deepseek_tool_reasoning_needs_kimi_tool_reasoningr%   s    r   r
	  z%AIAgent._needs_thinking_reasoning_padI"  s,     //11 1..00	
r   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.
        >   kimi-codingkimi-coding-cnr  r  r  )r  rw   r   r%   s    r   r	  z"AIAgent._needs_kimi_tool_reasoningU"  sU     M>> C$T]NCCC$T]MBBC %T]MBB		
r   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).
        r   deepseekzapi.deepseek.com)r  r  r  rw   r   )r   r  r  s      r   r	  z&AIAgent._needs_deepseek_tool_reasoningc"  se     M'R..00!r((**
" HU"H$T]4FGG	
r   
source_msgapi_msgc                    |                     d          dk    rdS |                     d          }t          |t                    r'|dk    r|                                 rd|d<   n||d<   dS |                                 }|                     d          }|r3|                     d          rt          |t                    r	|rd|d<   dS t          |t                    r	|r||d<   dS |rd|d<   dS |                    dd           dS )	zACopy provider-facing reasoning fields onto an API replay message.r+  r  Nr  r   r  r  r   )r   r!   r   r
	  r<  )r   r%	  r&	  r   needs_thinking_padnormalized_reasonings         r   _copy_reasoning_content_for_apiz'AIAgent._copy_reasoning_content_for_apir"  sR   >>&!![00F >>"566h$$ 	2~~$"D"D"F"F~/2+,,/7+,F!??AA  *~~k::	|,,	 /55	 %		 ,/G'(F *C00 	5I 	+?G'(F  	+.G'(F 	'.....r   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   r  rH  c                 ~    g | ]9}t          |t                    r fd |                                D             n|:S )c                 $    i | ]\  }}|v	||S r   r   )r   r  r  _STRIP_KEYSs      r   r  zJAIAgent._sanitize_tool_calls_for_strict_api.<locals>.<listcomp>.<dictcomp>"  s)    AAAdaA[,@,@Q,@,@,@r   )r!   r   r  )r   r   r.	  s     r   r   z?AIAgent._sanitize_tool_calls_for_strict_api.<locals>.<listcomp>"  s_     !
 !
 !
  "d##,AAAAbhhjjAAAA)+!
 !
 !
r   )r   r!   r  )r&	  r   r.	  s     @r   #_sanitize_tool_calls_for_strict_apiz+AIAgent._sanitize_tool_calls_for_strict_api"  sn    " [[..
*d++ 	N "45!
 !
 !
 !
 !!
 !
 !

 r   rS  r  c          
         |pt          j        t                    }t          | t                    sdS d}t
          j        dt          ddffd}d}|t          |           k     rv| |         }t          |t                    r|	                    d          dk    r|dz  }P|	                    d	          }t          |t                    r|s|dz  }|dz   }	|D ]}
t          |
t                    s|
	                    d
          }t          |t                    sD|	                    d          }||dk    rd|d<   gt          |t                    r|                                sd|d<   t          |t                    s	 t          j        |           # t          j        $ r |
	                    d          }|	                    dd          }|dd         }|                    d|pd||pd||           d|d<   d}|dz   }|t          |           k     rk| |         }t          |t                    r|	                    d          dk    rn4|	                    d          |k    r|}n|dz  }|t          |           k     k| |                     |	d|d           |	dz  }	n ||           |dz  }Y w xY w|dz  }|t          |           k     v|S )z<Repair corrupted assistant tool-call argument JSON in-place.r   r  r   Nc                 >   |                      d          }t          |t                    r)|s| d<   n|                              s
 d| | d<   d S || d<   d S 	 t	          j        |          }n# t          $ r t          |          }Y nw xY w d| | d<   d S )Nr)  r   )r   r!   r   r  r   rU  rW  )r  r   existing_textr  s      r   _prepend_markerz>AIAgent._sanitize_tool_call_arguments.<locals>._prepend_marker"  s    ||I..H(C((  B*0HY''!,,V44 B-3*A*Ax*A*AHY'&,#. $
8 4 4 . . . #H.%+">">}">">HYs   A4 4BBr+  r  r   r   r   r   r   rE  r*  r   rB  rR  z~Corrupted tool_call arguments repaired before request (session=%s, message_index=%s, tool_call_id=%s, function=%s, preview=%r)r-  r>  r  r+  r  r)  )r   r  r'   r!   r  r  &_TOOL_CALL_ARGUMENTS_CORRUPTION_MARKERr   r   r   r   r   r   r   rV  rT  insert)r'  rS  r  logr  r4	  message_indexr,  r   	insert_atr   r   r   r  function_namepreviewexisting_tool_msg
scan_index	candidater  s                      @r   _sanitize_tool_call_argumentsz%AIAgent._sanitize_tool_call_arguments"  s    3)(33(D)) 	1?	?d 	?t 	? 	? 	? 	? 	? 	?" c(mm++=)Cc4(( CGGFOO{,J,J"..Jj$// z "%)I' :" :"	!)T22 $==44!(D11 $LL55	$	R,0H[)i-- ioo6G6G ,0H[)!)S11 )"Jy))))+ '" '" '"#,==#6#6L$,LL$=$=M'nGKKc")c%$+%   -1H[)(,%!.!2J$s8}}44$,Z$8	))T:: "immF>S>SW]>]>]!$==88LHH09-!"a
 %s8}}44 )0 %(.0<+1    "Q		'(9:::MHHHO'"R QMQ c(mm++T s   	FDK K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    r   _should_sanitize_tool_callsz#AIAgent._should_sanitize_tool_callsA#  s     } 111r   rx  )approx_tokensr  focus_topicrC	  rD	  c                Z   t          |          }t                              d| j        pd||r|dnd| j        |           | j        r,	 | j                            |           n# t          $ r Y nw xY w	 | j        	                    |||          }n,# t          $ r | j        	                    ||          }Y nw xY wt          | j        dd          }|r6t          | d	d          |k    r || _        |                     d
| d           nlt          | j        dd          }	t          | j        dd          }
|	r>|	|
f}t          | dd          |k    r%|| _        |                     d|	 d|
pd d           | j                                        }|r|                    d|d           |                                  |                     |          }|| _        | j        r	 | j                            | j                  }|                     |           | j                            | j        d           | j        }t3          j                                        d           dt9          j                    j        dd          | _        | j        d| j         dz  | _         d| _!        | j        "                    | j        | j#        ptH          j%        &                    dd          | j        | j'        |           d| _!        |ru	 | j        (                    |          }| j        )                    | j        |           n9# tT          t          f$ r%}t          +                    d |           Y d}~nd}~ww xY w| j        ,                    | j        |           d!| _-        n2# t          $ r%}t          .                    d"|           Y d}~nd}~ww xY w	 t_                      &                    d#          }|r9ta          | j        d$          r$| j        1                    | j        pd%d|&           n2# t          $ r%}t          +                    d'|           Y d}~nd}~ww xY w	 t_                      &                    d#          }|r,| j        r%| j        2                    | j        pd%|dd(           n2# t          $ r%}t          +                    d)|           Y d}~nd}~ww xY w| j        j3        }|d*k    r"| 4                    | j5         d+| d,d-           tm          ||pd%| j7        pd.          }|| j        _8        d!| j        _9        	 d!d/l:m;}  ||           n# t          $ r Y nw xY wt                              d0| j        pd|t          |          |d           ||fS )1u  Compress conversation context and split the session in SQLite.

        Args:
            focus_topic: Optional focus string for guided compression — the
                summariser will prioritise preserving information related to
                this topic.  Inspired by Claude Code's ``/compact <focus>``.

        Returns:
            (compressed_messages, new_system_prompt) tuple
        zPcontext compression started: session=%s messages=%d tokens=~%s model=%s focus=%rr8  rI  r  )current_tokensrD	  )rF	  _last_summary_errorN!_last_compression_summary_warningu    ⚠ Compression summary failed: z%. Inserted a fallback context marker._last_aux_model_failure_model_last_aux_model_failure_error_last_aux_fallback_warning_keyu"   ℹ Configured compression model 'z
' failed (zunknown erroruS   ). Recovered using main model — check auxiliary.compression.model in config.yaml.r  r+  r)  rn  rD  rb  rE  rI  rJ  Fr  r\  )r  rz  r  r  r  Tz,Could not propagate title on compression: %sr   uK   Session DB compression split failed — new session will NOT be indexed: %sold_session_idr<  r   )boundary_reasonrM	  z1context engine on_session_start (compression): %s)r  resetr6  z2memory manager on_session_switch (compression): %sr6  u   ⚠️  Session compressed u>    times — accuracy may degrade. Consider /new to start fresh.r  r  ro  )reset_file_dedupz?context compression done: session=%s messages=%d->%d tokens=~%s)<r   rS  r  r  r  r$  on_pre_compressr   r  compressrW  r   rH	  r  rK	  r  format_for_injectionr   rA  r  r  r  r*  r  end_sessionr   r  r  r  r  r	  r
  r  r  r  r  r   r   r   r  get_next_title_in_lineageset_session_titler   r   update_system_promptr  rT  localsr  r<  on_session_switchcompression_countr  r  rQ   ro  last_prompt_tokenslast_completion_tokenstools.file_toolsrQ	  )r   r'  r  rC	  r  rD	  _pre_msg_count
compressedsummary_error_aux_fail_model_aux_fail_err_aux_keytodo_snapshotnew_system_prompt	old_titlerM	  	new_titleru  _old_sidr  _me_errr  _compressed_estrQ	  s                           r   _compress_contextzAIAgent._compress_contextN#  s+    X^O%v~$1@}   y$*		
 	
 	
  	$44X>>>>   	b099(S`ny9zzJJ 	b 	b 	b 099(S`9aaJJJ	b
   79NPTUU 	t@$GG=XX9F6"":} : : :   &d&=?^`deeO#D$;=\^bccM 	+];4!A4HHHTT:BD7&&L_ L L)<_L L L   (==?? 	Jv-HHIII&&((( 55nEE%6" 	qq ,>>tOO	**8444 ,,T_mLLL!%%-\^^%<%<_%M%M"f"fPTPZP\P\P`acbcacPd"f"f(,8Y4?8Y8Y8Y(Y%+0( //#=ZBJNN;RTY,Z,Z*!%!@&4 0    ,0( XX$($4$N$Ny$Y$Y	(::4?IVVVV&	2 X X X%SUVWWWWWWWWX 55doGXYYY,-)) q q qlnoppppppppq		Wxx||$455H GD$;=OPP '88O)r$1#+ 9   
  	W 	W 	WLLLgVVVVVVVV	W
	Xxx||$455H D0 $66O)r&.(	 7     	X 	X 	XLLMwWWWWWWWW	X %7!88LL? G Gs G G G     8+1r*$
 
 

 6E29:6
	999999W%%%% 	 	 	D	 	MO%v~s:""	
 	
 	

 ,,,s   	A$ $
A10A15B &B<;B<%D$N& 
:M N& M;M61N& 6M;;*N& &
O0OOAP6 6
Q% Q  Q%)AR9 9
S(S##S(U% %
U21U2decisionc                 8    |j         r| j        || _        dS dS dS )z?Record the first guardrail decision that should stop this turn.N)should_haltr  r   rm	  s     r   _set_tool_guardrail_haltz AIAgent._set_tool_guardrail_halt#  s3     	:D$F$N19D...	: 	:$N$Nr   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.)r  r%  rY  )r   rm	  r>  s      r   #_toolguard_controlled_halt_responsez+AIAgent._toolguard_controlled_halt_response#  sP    !-XE$ E EE E'/~E E E	
r   r  function_resultfailedc                    | j                             ||||          }|j        dv rt          ||          }|j        r|                     |           |S )Nru	  >   haltr  )r  
after_callactionrq   ro	  rq	  )r   r  r  rt	  ru	  rm	  s         r   _append_guardrail_observationz%AIAgent._append_guardrail_observation$  sq     (33	 4 
 
 ?...7RRO 	4))(333r   c                 J    |                      |           t          |          S r   )rq	  rr   rp	  s     r   _guardrail_block_resultzAIAgent._guardrail_block_result$  s$    %%h///)(333r   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   r  r  _execute_tool_calls_sequential_execute_tool_calls_concurrent)r   rg  r'  r~	  r  r   s         r   _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  goalr  toolsetstasksr  r  r  r+  )	r	  r  r	  r	  r  r  r  r+  parent_agent)r  r  r   )r   r  _delegate_tasks      r   _dispatch_delegate_taskzAIAgent._dispatch_delegate_task7$  s     	HGGGGG~""6**!%%i00"&&z22##G,,(,,-=>>%))-88"&&z22""6**

 

 

 
	
r   r;	  pre_tool_block_checkedc           
         d}|s(	 ddl m}  ||||pd          }n# t          $ r Y nw xY w|t          j        d|id          S |d	k    r@dd
lm}	  |	|                    d          |                    dd          | j                  S |dk    ry| j	        st          j        ddd          S ddl
m}
  |
|                    dd          |                    d          |                    dd          | j	        | j                  S |dk    r|                    dd          }ddlm}  ||                    d          ||                    d          |                    d          | j                  }| j        r|                    d          dv rm	 | j                            |                    dd          ||                    dd          |                     ||                      n# t          $ r Y nw xY w|S | j        r5| j                            |          r| j                            ||          S |d!k    r@dd"lm}  ||                    d#d          |                    d$          | j        %          S |d&k    r|                     |          S t3          ||||| j        pd| j        rt7          | j                  ndd'(          S ))a@  Invoke a single tool and return the result string. No display logic.

        Handles both agent-level tools (todo, memory, etc.) and registry-dispatched
        tools. Used by the concurrent execution path; the sequential path retains
        its own inline invocation for backward-compatible display handling.
        Nr   get_pre_tool_call_block_messager   r  r  Fr  todo	todo_toolr  r  r  r  storer   Session database not available.r  r  r   r  role_filterlimitrm  r  r	  r	  dbcurrent_session_idrS  r  memory_toolrz	  r)  old_textrz	  r  r)  r	  r	  r.  r.  r  r  r  r   clarify_toolquestionr  r	  r  callbackr  Tr  r  enabled_toolsskip_pre_tool_call_hook)r6  r	  r   r   rU  r  r	  r   r  r  tools.session_search_toolr   r  r"  r	  r  r$  on_memory_writer  has_toolhandle_tool_calltools.clarify_toolr	  r  r	  r3   r  r  )r   r;	  r  r~	  r  r'  r	  block_messager	  
_todo_tool_session_searchr  _memory_toolrL  _clarify_tools                  r   _invoke_toolzAIAgent._invoke_toolJ$  s    (,% 	NNNNNN ? ?!=:K:Qr! ! !    $:w6UKKKKF""??????:#''00#''77&   
 ...# bze>_"`"`aaaSSSSSS"?#''44)--m<<#''33##'?    h&&"&&x::FEEEEEE!\$((22%)))44&**:66(  F # (9(9((C(CGY(Y(Y(88%))(B77%)))R88!%!B!B$5)5 "C " "	 9     !   DM! 	d&:&C&CM&R&R 	'88VVVi''HHHHHH =&**:r::%)))44.   
 o--//>>>'}.?)?0b=A=R\d4#8999X\(,   s    
**AG7 7
HH     r  rK  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)rz     re  r   TF)widthbreak_long_wordsbreak_on_hyphens)shutiltextwrapget_terminal_sizecolumnsr   r   r"  r   wrapr  r;  )r  r  rK  _shutil_twcols
wrap_width	out_linesraw_linewrappedr  s              r   _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..'%''''r   c                    789:; |j         }t          |          } j        rMt           j         d| d           |D ].}                    dd|j        j         d|j        d           /dS g :|D ]0}|j        j        }	|	dk    rd	 _	        n|	d
k    rd	 _
        	 t          j        |j        j                  }
n# t          j        $ r i }
Y nw xY wt          |
t                     si }
|	dv rn j        j        rb	 |
                    dd          }|r8 j                            |          } j                            |d|	            n# t,          $ r Y nw xY w|	dk    r j        j        r	 |
                    dd          }t/          |          ra|
                    d          p%t1          j        dt1          j                              } j                            |d|dd                     n# t,          $ r Y nw xY wd}d}	 d	dlm}  ||	|
pd          }n# t,          $ r d}Y nw xY w|t          j        d|id          }n9 j                            |	|
          }|j         s !                    |          }d}:                    ||	|
||f           2d"                    d :D                       } j#        s't          d| d|            tI          :d          D ] \  }\  }}}}}t          j        |d          } j%        rqt          d | d!| d"tM          |'                                           d#           t           (                    d$t          j        |d%d&                               t          |           j)        k    r|d j)                 d'z   n|}t          d | d!| d"tM          |'                                           d(|            :D ]j\  }}}}}| j*        rX	 tW          ||          } *                    d)|||           <# t,          $ r!}tY          j-        d*|            Y d}~bd}~ww xY wk:D ]^\  }}}}}| j.        rL	  .                    |j        ||           0# t,          $ r!}tY          j-        d+|            Y d}~Vd}~ww xY w_dg|z  ;tI          :          D ]\  }\  }}}}}||||d,ddf;|<   | _/         0                    d-| d.|            tc                      7te                      878; fd/}d} 3                                rj 4                                rVtk          j6        to          j8                              }to          | d0| d1d2 j9        3          }|:                                 	 d4 tI          :          D             }g 9|rtw          t          |          tx                    } tz          j>        ?                    | 5          5 }!|D ]N\  }}}}t          jA                    }"|!B                    |"jC        |||||          }#9                    |#           Ot          jD                    }$d}%	 tz          j>        E                    9d67          \  }&}'|'sn j        rn|%s1d}% F                     j         d8t          |'           d9d:           |'D ]}#|#G                                 tz          j>        E                    |'d;7           nt          t          jD                    |$z
            }(|(d	k    r_|(d<z  d=k     rV9:fd>|'D             }) 0                    d?|( d@t          |'           dAd"                    |)ddB                    d#           &ddd           n# 1 swxY w Y   |rRt          dC ;D                       }*t          dD ;D                       }+|J                    dE|* dF| dG|+dHdI           nZ# |rSt          dC ;D                       }*t          dD ;D                       }+|J                    dE|* dF| dG|+dHdI           w w xY wtI          :          D ]\  }\  }}}}};|         },d}-|, j        rd| d}.ndJ| dK}.d,}/n |,\  }	}
}.}/}0}-|-s K                    |	|
|.|0L          }.|0r<t          |.          dMk    r
|.ddM         n|.}1t          M                    dN|	|/|1           |-sR j*        rK	  *                    dO|	dd|/|0P           n.# t,          $ r!}tY          j-        d*|            Y d}~nd}~ww xY w j%        rCtY          j-        dQ|	 dR|/dSdT           tY          j-        dUt          |.           dV|.             3                                r,t          |||/|.W          }2 O                    dX|2            n j#        s j%        r>t          dY|dz    dR|/dSdT           t           (                    dZ|.                     nHt          |.           j)        k    r|.d j)                 d'z   n|.}3t          dY|dz    dR|/dSd[|3            d _/         0                    d\| d]|/dHd^           |-sT jP        rM	  P                    |j        |||.           n.# t,          $ r!}tY          j-        d_|            Y d}~nd}~ww xY wt          |.||j        t                    `          }. jS        T                    ||          }4|4r|.|4z  }.d|.|j        d}5                    |5            U                    d           t          :          }|d	k    r)| d         }6t          |6t                    a           |d	k    r U                    |           dS dS )bzExecute multiple tool calls concurrently using a thread pool.

        Results are collected in the original tool-call order and appended to
        messages so the API sees them in the expected sequence.
           ⚡ Interrupt: skipping  tool call(s)r>     [Tool execution cancelled — # was skipped due to user interrupt]r  NrS  r   r  r   r   r  r   before terminalr  workdirr  before terminal: rC  Fr	  r  r  r  Tr@  c              3   &   K   | ]\  }}}}}|V  d S r   r   )r   rb  r   s      r   r   z9AIAgent._execute_tool_calls_concurrent.<locals>.<genexpr>	%  s-      "M"M,<AtQ14"M"M"M"M"M"Mr   u     ⚡ Concurrent: u    tool calls — r        📞 Tool r  (r  Args: r6  rK  r  r  ) - tool.startedTool progress callback error: Tool start callback error: r  z
executing z tools concurrently: c                    t          j                    j        }j        5  j                            |           ddd           n# 1 swxY w Y   j        r"	 t          d|           n# t          $ r Y nw xY w	 ddl	m
}  |j                   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t          j                    }	                     |||j        d          }n=# t          $ r0}d| d| }t"                              d||d	           Y d}~nd}~ww xY wt          j                    |z
  }	t'          ||          \  }
}|
r&t"                              d
||	|dd                    n*t"                              d||	t+          |                     ||||	|
df| <   j        5  j                            |           ddd           n# 1 swxY w Y   	 t          d|           n# t          $ r Y nw xY w	 t          d           t          d           dS # t          $ r Y dS w xY w)z%Worker function executed in a thread.NTr   set_activity_callback)r'  r	  Error executing tool '': z_invoke_tool raised for %s: %sr  ztool %s failed (%.2fs): %sr   #tool %s completed (%.2fs, %d chars)F)r   rL  rM  r  r  r.  r  rg  r   tools.environments.baser	  r  r  _set_sudo_password_callbackr  r	  r*  rS  r  rl   r  r   discard)rB  r   r;	  r  _worker_tidr	  r  rL  
tool_errordurationis_errorrb  _parent_approval_cb_parent_sudo_cbr~	  r'  resultsr   s               r   	_run_toolz9AIAgent._execute_tool_calls_concurrent.<locals>._run_tool<%  s   
 $244:K/ ; ;)--k:::; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ( "45555    DIIIIII%%d&:;;;;    #.*+>????    D*/@@@@    DIKKEi**!!%L%+/ +    i i iP-PPJPP=}jcghhhhhhhhi y{{U*H.}fEEKHa i8-SYZ^[^Z^S_````A=RZ\_`f\g\ghhh+]FHhX]^GEN / ? ?)11+>>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ?uk2222   &t,,,+D11111   s   AAAA, ,
A98A9=B 
B! B!'B7 7
CC
C 
C'&C'> D 
E)&EE)HHHH- -
H:9H:>I 
I,+I,u    ⚡ running z tools concurrentlydotsspinner_typeprint_fnc                 0    g | ]\  }\  }}}}}|||||fS r   r   )r   r>  r   r   r   block_resultblocked_by_guardrails          r   r   z:AIAgent._execute_tool_calls_concurrent.<locals>.<listcomp>%  sA       KAKD$6J' Bd#'''r   )max_workers      @r   u   ⚡ Interrupt: cancelling z pending concurrent tool(s)r  g      @rb  rE  c                 Z    g | ]'}|v                      |                   d          (S )r   )rB  )r   r.  futuresparsed_callss     r   r   z:AIAgent._execute_tool_calls_concurrent.<locals>.<listcomp>%  s@     . . .$%#$<< !-W]]1-=-= >q A#/<<r   zconcurrent tools running (zs, z remaining: rm  c              3      K   | ]}|dV  	d S )Nr   r   r   rs     r   r   z9AIAgent._execute_tool_calls_concurrent.<locals>.<genexpr>%  s"      DDaammmmmDDr   c              3   *   K   | ]}||d         V  d S )Nrm  r   r	  s     r   r   z9AIAgent._execute_tool_calls_concurrent.<locals>.<genexpr>%  s&      GG!GGr   u   ⚡ r  z tools completed in .1fzs totalr	  z!': thread did not return a resultrw	  r   "Tool %s returned error (%.2fs): %stool.completedr	  r	  Tool  completed in .2fr+  Tool result (r	  rL         ✅ Tool Result: s - tool completed: r  rU  Tool complete callback error: r)  r  tool_use_idenvr
  )Wr   r   r  r  r  r   r   r   r*  r   r!  r   r   r   rV  r!   r   r  rN  r   get_working_dir_for_pathensure_checkpointr   r   r   r=  getcwdr6  r	  rU  r  before_callallows_executionr}	  r;  r  r   r  r  keysr	  r  r  _build_tool_previewr   r   r  r  r  r:   r;   r  r  randomchoiceri   get_waiting_facesr  r  r  _MAX_TOOL_WORKERS
concurrentr	  ThreadPoolExecutorcontextvarscopy_contextsubmitrv  r  waitr  cancelr   r"  rc  r{	  rS  rT  _get_cute_tool_message_implr  r  r<   r6   r>  check_tool_callr  r=   )<r   rg  r'  r~	  r  r   	num_toolsr   r   r;	  r  	file_pathwork_dirr   r	  r	  r	  r	  r	  guardrail_decisiontool_names_strr>  r   r   args_strargs_previewr<	  cb_errr	  spinnerfacerunnable_callsr	  executorctxr.  _conc_start_interrupt_loggedr  not_done_conc_elapsed_still_runningr  	total_durr	  blockedrt	  tool_durationr	  result_previewcute_msgresponse_previewsubdir_hintsr  turn_tool_msgsr	  r	  r	  r	  r	  s<   ` ``                                                   @@@@@r   r	  z&AIAgent._execute_tool_calls_concurrent$  s    '1

OO	 $ 	T_VViVVVWWW   "u@Puuu$&E! !    
 F # 8	o 8	oI%.3M ((+,((.00*+'# $
9+=+G H H' # # # "#mT22 # "  777D<P<X7 - 1 1&" = =I  d#'#7#P#PQZ#[#[,>>xIbS`IbIbccc    D 
**t/C/K*'++Ir::C.s33 +//	::dbiXZXaXcXc>d>d,>>!?S"X!?!?   !   D  L#( %NNNNNN ? ?!=:K:Qr! ! !  % % % $% (#z7M*BQVWWW%)%:%F%F}Vc%d%d"): 0#'#?#?@R#S#SL+/(M=,Xl mnnnn "M"M"M"M"MMM 		\RyRR.RRSSSKTUacdKeKe \ \GGBdL2F:d???' \IIIdIIT$))++5F5FIIIJJJ$,,Xtz$q_d7e7e7effggggORS[}}_c_tOtOt8,BT-B,B#Ce#K#K  {CLZZZdZZT$))++5F5FZZLZZ[[[[BN 	M 	M>BdL*>'* MM1$==G//gtTTTT  M M MM"K6"K"KLLLLLLLLM	M CO 	J 	J>BdL*>'' JJ,,RUD$????  J J JM"H"H"HIIIIIIIIJJ &9$GPQ]G^G^ 	I 	ICACD$.B'"D,T4H
 ,Z)ZZ.ZZ[[[ 566577I	 I	 I	 I	 I	 I	 I	 I	 I	 I	X 0022 	t7W7W7Y7Y 	=!@!B!BCCD#t$X$X)$X$X$Xgmx|  yG  H  H  HGMMOOOC	g OXYeOfOf  N
 G 6!#n"5"57HII'::{:SS 4W_-; * *)2tT)688$OOCGY2tTRRq)))) #')++K(-%&)3);)@)@#S *A * *h  ( "!  4 "#4 "48 1 $'+ %R %R'*8}}%R %R %R*. !- !" !" !"
 &. + + !



 '.33Hc3JJJ!(+DIKK+,E(F(F(1,,1Ca1G1G. . . . .)1. . .N
 !00!_] !_ !_#&x==!_ !_>BiiWYXYWYHZ>[>[!_ !_ !_  G&4 4 4 4 4 4 4 4 4 4 4 4 4 4 4l  gDD7DDDDD	GGgGGGGG	eIee	eeyeeeefff	  gDD7DDDDD	GGgGGGGG	eIee	eeyeeeeffff	g HQQ]G^G^ P	C P	CCACD$.B
AGy, g&pt&p&p&pOO&ft&f&f&fO #bc_}o}hX_ &*&H&H%%''	 'I ' 'O  w>A/>R>RUX>X>X_TcT%:%:^mNNN#GXeguvvv Q4#> QQ33,mT4%2X 4     % Q Q Q&Ov&O&OPPPPPPPPQ ' dM"[-"["[}"["["["[\\\M"b#o2F2F"b"bQ`"b"bccc 4466 	f6tT=Yhiii  h1111_ f' fO!OO=OOOOPPP$,,ZIIJJJJZ]^mZnZnqu  rG  [G  [G7M8M7M'NQV'V'V  M\$d!dd=dddRbddeee!%D  !QD!Q!QM!Q!Q!Q!QRRR Mt: MM//tT?SSSS  M M MM"K6"K"KLLLLLLLLM 8'E"#455	  O  3CCD$OOL 0</ * " H
 OOH%%%
 55hBBBB %%	q==%yjkk2NNCT4U4UVVVV q==55h	JJJJJ =s   "CCC AE
EE4BG;;
HHH''H65H6(P55
Q ?QQ 9R
S R<<S<A ` F^6*` 6^::` =^:>` Aa-d77
e"ee"1k
k:k55k:c                 t*   t          |j        d          D ]\  }}| j        r{|j        |dz
  d         }|r/|                     | j         dt          |           dd           |D ]2}|j        j        }	dd|	 d	|j        d
}
|	                    |
           3 n;|j        j        }	 t          j        |j        j                  }n5# t          j        $ r#}t          j        d|            i }Y d}~nd}~ww xY wt!          |t"                    si }d}	 ddlm}  ||||pd          }n# t(          $ r Y nw xY wd}|$| j                            ||          }|j        s|}|dup|du}|rn|dk    rd| _        n|dk    rd| _        | j        st          j        |d          }| j        rqt;          d| d| dt=          |                                           d           t;          |                      dt          j        |dd                               nft          |          | j!        k    r|d| j!                 dz   n|}t;          d| d| dt=          |                                           d|            |s|| _"        | #                    d|            |s(	 ddl$m%}  || j#                   n# t(          $ r Y nw xY w|s_| j&        rX	 tO          ||          }| &                    d|||           n.# t(          $ r!}t          j(        d |            Y d}~nd}~ww xY w|sS| j)        rL	 | )                    |j        ||           n.# t(          $ r!}t          j(        d!|            Y d}~nd}~ww xY w|sr|d"v rn| j*        j+        rb	 |,                    d#d          }|r8| j*        -                    |          }| j*        .                    |d$|            n# t(          $ r Y nw xY w|s|d%k    r| j*        j+        r	 |,                    d&d          }t_          |          ra|,                    d'          p%ta          j1        d(ta          j2                              }| j*        .                    |d)|dd*                     n# t(          $ r Y nw xY wtg          j3                    }|t          j        d+|id          }d,}n9|| 4                    |          }d,}n|d-k    rdd.l5m6}  ||,                    d/          |,                    d0d          | j7        1          }tg          j3                    |z
  }| 8                                r)|                     d2ts          d-|||3                      n|d4k    r| j:        st          j        dd5d6          }n[dd7l;m<}   | |,                    d8d          |,                    d9          |,                    d:d;          | j:        | j=        <          }tg          j3                    |z
  }| 8                                r)|                     d2ts          d4|||3                      
n|dk    rY|,                    d=d          }!dd>l>m?}"  |"|,                    d?          |!|,                    d@          |,                    dA          | j@        B          }| jA        r|,                    d?          dCv r|	 | jA        B                    |,                    d?d          |!|,                    d@d          | C                    |t          |dDd          E          F           n# t(          $ r Y nw xY wtg          j3                    |z
  }| 8                                r)|                     d2ts          d|||3                      	nN|dGk    rddHlEmF}#  |#|,                    dId          |,                    dJ          | jG        K          }tg          j3                    |z
  }| 8                                r)|                     d2ts          dG|||3                      n|dLk    r|,                    dM          }$|$r)t!          |$t<                    rdNt          |$           dO}%n(|,                    dP          pdddQ         }&|&rdR|& ndS}%d}'| 8                                ri| H                                rUt          jJ        t          jL                              }(t          |( dT|% dU| jM        V          }'|'N                                 |'| _O        d})	 | P                    |          }|})d| _O        tg          j3                    |z
  }ts          dL|||)3          }*|'r|'Q                    |*           n[| 8                                r|                     d2|*            n-# d| _O        tg          j3                    |z
  }ts          dL|||)3          }*|'r|'Q                    |*           w | 8                                r|                     d2|*            w w xY w| jR        r|| jR        v rd}'| 8                                ryt          jJ        t          jL                              }(t          |          }+tO          ||          p|}t          |( dT|+ dT| dU| jM        V          }'|'N                                 d},	 | jT        U                    |||W          }|},nQ# t(          $ rD}-t          j        d+dX| dY|- i          }t          W                    dZ||-d[           Y d}-~-nd}-~-ww xY wtg          j3                    |z
  }ts          ||||,3          }*|'r|'Q                    |*           n\| 8                                r|                     d2|*            n.# tg          j3                    |z
  }ts          ||||,3          }*|'r|'Q                    |*           w | 8                                r|                     d2|*            w w xY w| jA        r| jA        X                    |          rd}'| 8                                r| H                                ryt          jJ        t          jL                              }(t          |          }+tO          ||          p|}t          |( dT|+ dT| dU| jM        V          }'|'N                                 d}.	 | jA        U                    ||          }|}.nQ# t(          $ rD}-t          j        d+d\| dY|- i          }t          W                    d]||-d[           Y d}-~-nd}-~-ww xY wtg          j3                    |z
  }ts          ||||.3          }*|'r|'Q                    |*           nA| 8                                r|                     d2|*            n# tg          j3                    |z
  }ts          ||||.3          }*|'r|'Q                    |*           w | 8                                r|                     d2|*            w w xY w| j        rd}'| 8                                r| H                                ryt          jJ        t          jL                              }(t          |          }+tO          ||          p|}t          |( dT|+ dT| dU| jM        V          }'|'N                                 d}/	 t          ||||j        | j=        pd| jZ        rt=          | jZ                  ndd^          }|}/n=# t(          $ r0}-d_| d`|- }t          W                    da||-d[           Y d}-~-nd}-~-ww xY wtg          j3                    |z
  }ts          ||||/3          }*|'r|'Q                    |*           n3| 8                                r|                     d2|*            n# tg          j3                    |z
  }ts          ||||/3          }*|'r|'Q                    |*           w | 8                                r|                     d2|*            w w xY w	 t          ||||j        | j=        pd| jZ        rt=          | jZ                  ndd^          }n=# t(          $ r0}-d_| d`|- }t          W                    da||-d[           Y d}-~-nd}-~-ww xY wtg          j3                    |z
  }| j        r|nt          |          dbk    r
|ddb         n|}0t          ||          \  }1}2|sA| \                    ||||1c          }| j        r|nt          |          dbk    r
|ddb         n|}0|1rt                              dd|||0           n*t          ]                    de||t          |                     |sR| j&        rK	 | &                    df|dd||1g           n.# t(          $ r!}t          j(        d |            Y d}~nd}~ww xY wd| _"        | #                    dh| di|djdk           | j        rCt          j(        dl| dm|dndo           t          j(        dpt          |           dq|            |sT| j^        rM	 | ^                    |j        |||           n.# t(          $ r!}t          j(        dr|            Y d}~nd}~ww xY wt          |||j        t          |          s          }| ja        b                    ||          }3|3r||3z  }d||j        d
}4|	                    |4           | c                    |d           | j        s| j        r;t;          dt| dm|dndo           t;          |                      du|                     nEt          |          | j!        k    r|d| j!                 dz   n|}5t;          dt| dm|dndv|5            | j        r|t          |j                  k     r}t          |j                  |z
  }6|                     | j         d|6 dwd           |j        |d         D ]2}|j        j        }	ddx|	 dy|j        d
}
|	                    |
           3 n>| jd        dk    r1|t          |j                  k     rtg          je        | jd                   t          |j                  }7|7dk    r't          ||7 d         t          |          z           |7dk    r| c                    ||7           dS dS ){z`Execute tool calls sequentially (original behavior). Used for single calls or interactive tools.r   Nr	  r	  Tr  r>  r	  r	  r  z(Unexpected JSON error after validation: r   r	  r   r  rS  r  Fr  r	  r  r	  r  r	  r6  r	  r  r	  zexecuting tool: r	  r	  r	  r	  r	  r  r	  r	  r  r	  r  r	  rC  r  r  r	  r	  r  r  r	  r
  r
  r   r	  r	  r	  r  r	  r	  rm  r	  r  r	  rz	  r)  r	  r	  r	  r*  r	  r	  r   r	  r	  r  r	  r  r	  u   🔀 delegating z tasksr	  rb  u   🔀 u   🔀 delegatingr  r	  r	  )r'  zContext engine tool 'z
' failed: z1context_engine.handle_tool_call raised for %s: %sr  zMemory tool 'z1memory_manager.handle_tool_call raised for %s: %sr	  r	  r	  z&handle_function_call raised for %s: %sr   rw	  r	  r	  r	  r	  r	
  r  r	  rU  r 
  r
  r
  r+  r
  r	  r

  r
  r
  r
  r
  z remaining tool call(s)u   [Tool execution skipped — z* was not started. User sent a new message]r
  )gr   r   r  r  r  r   r   r   r*  r   r   r   r   rV  r   rT  r!   r   r6  r	  r   r  r
  r
  r   r!  r  rU  r  r  r  r
  r	  r  r  r  r	  r	  r  r
  r   r  r  rN  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
  ri   r
  r  r  _delegate_spinnerr	  rc  r:  _get_tool_emojir  r	  rS  r  r	  r3   r  rl   r{	  r  r  r<   r6   r>  r"
  r  r  r1  r=   )8r   rg  r'  r~	  r  r>  r   remaining_calls
skipped_tcskipped_nameskip_msgr;	  r  ru  
_block_msgr	  _guardrail_block_decisionr&
  _execution_blockedr(
  r)
  r	  r<	  r*
  r$
  r%
  r   r	  tool_start_timert	  r7
  r	  r	  r  r	  r	  	tasks_argspinner_labelgoal_previewr+
  r,
  _delegate_resultr9
  emoji
_ce_resultr	  _mem_result_spinner_resultr8
  _is_error_resultrb  r;
  r  r:
  r   num_tools_seqs8                                                           r   r	  z&AIAgent._execute_tool_calls_sequential3&  s   %&7&BAFF K	, K	,LAy ( "3">qstt"D" ~LLDO!p!pSQ`MaMa!p!p!px|L}}}"1 . .J#-#6#;L &#uL#u#u#u(2   H
 OOH----%.3M# $
9+=+G H H' # # # N1 N NOOO "# mT22 # " )-JNNNNNN<<!=:K:Qr  

     GK%!%)%:%F%F}Vc%d%d"): C0B-!+4!7!`;T\`;`! 	0  !H,,/0D,,"n44./D+? n:m%HHH' n[[[m[[d=CUCUCWCW>X>X[[[\\\$,,Xtz-XYhm7n7n7nooppppORS[}}_c_tOtOt8,BT-B,B#Ce#K#K  {CLlllmlld=CUCUCWCW>X>Xll^jllmmm% I%2"$$%G%G%GHHH
 & MMMMMM))$*>????    D & M$*E MM1-OOG//wXeffff  M M MM"K6"K"KLLLLLLLLM & J$*B JJ,,Y\=-XXXX  J J JM"H"H"HIIIIIIIIJ & 	-;R*R*RW[WkWs*R - 1 1&" = =I  #'#7#P#PQZ#[#[,>>$&?&?&?   !   D & 	-:*E*E$J^Jf*E'++Ir::C.s33 +//	::dbiXZXaXcXc>d>d,>>!?S"X!?!?   !   D #ikkO%"&*gz-BQV"W"W"W #*6 #'">">?X"Y"Y #&((CCCCCC",*'++G44'++GU;;*# # #
 !%	o =88:: CLL  "B&A&-Yfo~&&&  "B  "B  C  C  C"222' 
&*jUMn1o1o&p&pOO[[[[[[&5o+//<<$1$5$5m$D$D+//;;++/?' ' 'O !%	o =88:: MLL  "L&ABRTacp  zI  'J  'J  'J  "L  "L  M  M  M(**&**8X>>IIIIII".,(,,X66!)--i88*..z::,# # # ' M,=,=h,G,GK],],],<<)--h;;")--i<<%)%F%F(9-4Yd-K-K &G & &	 =     %    $	o =88:: ELL  "D&A(M[h  rA  'B  'B  'B  "D  "D  E  E  E)++LLLLLL"/-*..z2>>)--i88!2# # #
 !%	o =88:: FLL  "E&A)]\i  sB  'C  'C  'C  "E  "E  F  F  F/11)--g66	 bIt!<!< b$Ms9~~$M$M$MMM$1$5$5f$=$=$CSbS#IL>J$a$:L$:$:$:PaM88:: $t?_?_?a?a $!=)H)J)JKKD+t,E,Em,E,ETZeiestttGMMOOO)0&#' 
6&*&B&B=&Q&QO'6$-1D*$(IKK/$AM:?M[h  rB   C   C   CH 6X....>>@@ 6_(__555 .2D*$(IKK/$AM:?M[h  rB   C   C   CH 6X....>>@@ 6_(__555560 W>]dFe5e5e88:: $!=)H)J)JKKD+M::E1-OO`S`G+t,G,Ge,G,Gg,G,GV\gkguvvvGMMOOO!
6&*&=&N&N}^kv~&N&&O!0JJ  @ @ @&*j';xS`;x;xlv;x;x1y&z&zOLL!TVceoz~L@ %)IKK/$AM:=-YfoyzzzH 6X....>>@@ 6_(__555 %)IKK/$AM:=-YfoyzzzH 6X....>>@@ 6_(__55556% @>$*>*G*G*V*V @> 88:: $t?_?_?a?a $!=)H)J)JKKD+M::E1-OO`S`G+t,G,Ge,G,Gg,G,GV\gkguvvvGMMOOO"6&*&:&K&KM[h&i&iO"1KK  @ @ @&*j';p=;p;pdn;p;p1q&r&rOLL!TVceoz~L@ %)IKK/$AM:=-Yfoz{{{H 6X....>>@@ 6_(__555 %)IKK/$AM:=-Yfoz{{{H 6X....>>@@ 6_(__55556 (>88:: $t?_?_?a?a $!=)H)J)JKKD+M::E1-OO`S`G+t,G,Ge,G,Gg,G,GV\gkguvvvGMMOOO"&6&:%}6G%.\#'?#8bEIEZ&dd4+@&A&A&A`d04' ' 'O '6OO  u u u&]}&]&]Q[&]&]OLL!I=ZdosLttttttttu %)IKK/$AM:=-Yfo~H 6X....>>@@ 6_(__555 %)IKK/$AM:=-Yfo~H 6X....>>@@ 6_(__55556
u&:%}6G%.\#'?#8bEIEZ&dd4+@&A&A&A`d04' ' 'OO ! u u u&]}&]&]Q[&]&]OLL!I=ZdosLttttttttu !%	o =040D __),_)=)=)C)C%%  #7}o"V"Va% 	"&"D"D!!#+	 #E # # 594H "-0-A-AC-G-GODSD))_    wC]TacqrrrrA=R_adetauauvvv% M$*E MM//(-t!.9I 0     ! M M MM"K6"K"KLLLLLLLLM "&D  !ZM!Z!Z]!Z!Z!Z!Z[[[# `WmWW=WWWWXXX^c/.B.B^^_^^___% M$*E MM//	m]\kllll  M M MM"K6"K"KLLLLLLLLM 8''%L"#455	  O  3CCMS`aaL 0</ * ) H
 OOH%%% 55hBBB? d' dMMMMMMMNNN$,,ZIIJJJJZ]^mZnZnqu  rG  [G  [G7M8M7M'NQV'V'V  M\$bbbbbbP`bbccc( Q5F5Q1R1R-R-R 1 <==A	kkkkkswxxx"3">qrr"B . .J#-#6#;L &#z,#z#z#z(2   H
 OOH----""q3/@/K+L+L'L'L
4?+++ -8991- 9~N_?`?`aaaa
 155hNNNNN s  +C

C<C77C<D00
D=<D=-K
KK(L
L2L--L2?M
N&NNAO..
O;:O;BR
R'&R'6A*]!!
].-].g''A7il%$o&%
m3/:m.)o&.m33o&&A0q"u ?x 
v
:v	x	vxA0y1"?}"!A@"
~,&~A@~A@@A0AA?B=ACC
AC;C&AC6C6AC;G&AHH
AH-HAH(H(AH-J(AKK
AK1KAK,K,AK1c           
      D   t          d| j         d           d}|                    d|d           	 |                                 }g }|D ]s}|                                }|                     ||           dD ]}|                    |d           |r|                     |           |                    |           t| j        pd}	| j	        r|	d	z   | j	        z   
                                }	|	r	d
|	dg|z   }| j        rK|	rdnd}
t          | j                  D ]0\  }}|                    |
|z   |                                           1|                     |          }|                     |          }i }	 ddlm}m} n# t&          $ r d}d}Y nw xY w| || j        | j                  nd}||u }|rdn|}d| j        v }| j        pd
                                                                dk    o|                                 }|r|                                 nd}|s.|                                 r| j        | j        |d<   nddd|d<   |rdg|d<   | j        dk    r|                     |          }|                    dd           |                     |          }|                                 }|                     |          }|j!        pd
                                }n| j        |d}|||d<   | j"        -|#                    | $                    | j"                             |||d<   i }| j%        r
| j%        |d<   | j&        r
| j&        |d<   | j'        r
| j'        |d<   | j(        r
| j(        |d<   |r||d<   |r||d <   | j        d!k    r|                                 }|)                    | j        |d| j"        | j        | j*        | +                                "          }| ,                    |          }|                     || j*        #          }|j!        pd
                                }nm | -                    d$%          j.        j/        j0        d0i |}|                                                      |          }|j!        pd
                                }|rXd&|v r4tc          j2        d'd|tb          j3        (          
                                }|r|                    d)|d           n]d*}nY| j        dk    r|                     |          }|                    dd           |                     |          } |                                 }!|!                     |           }"|"j!        pd
                                }nn| j        d!k    r|                                 }#|#)                    | j        |d| j*        | j"        | j        | +                                +          }$| ,                    |$          } |#                     | | j*        #          }%|%j!        pd
                                }n| j        |d}|||d<   | j"        -|#                    | $                    | j"                             |||d<   |r||d <    | -                    d,%          j.        j/        j0        d0i |}|                                                      |          }%|%j!        pd
                                }|rVd&|v r4tc          j2        d'd|tb          j3        (          
                                }|r|                    d)|d           nd*}nd*}nH# t&          $ r;}&ti          j5        d-|&            d.| j         d/tm          |&           }Y d}&~&nd}&~&ww xY w|S )1zSRequest a summary when max iterations are reached. Returns the final response text.u$   ⚠️  Reached maximum iterations (z). Requesting summary...zYou've reached the maximum number of tool-calling iterations allowed. Please provide a final response summarizing what you've found and accomplished so far, without calling any more tools.r  rL	  )r  ra  _thinking_prefillNr   r  r  r   r   r  r  r  r  Tr  )rN  r  zproduct=hermes-agenttagsr  ro  )r  r'  temperaturereasoning_effortr  rg  r  r  r  
extra_bodyr  )r  r'  ro  r  r  r  r  strip_tool_prefixiteration_limit_summaryr  r  z<think>.*?</think>\s*rO  r  z>I reached the iteration limit and couldn't generate a summary.)r  r'  ro  r  r  r  r  iteration_limit_summary_retryz Failed to get summary response: z"I reached the maximum iterations (z!) but couldn't summarize. Error: r   )7r  r  r   rB	  r  r*	  r<  r/	  r  r  r   r  r   r7	  r  r  r  r  r  r   r  r   r  r  r  r  r  r  r  r  r  r  normalize_responser)  r  rQ  rI  r  r  r  r  r  r  r  r.  r  rF  rG  r  rX  r  rV  r   rT  r   )'r   r'  r  summary_request_needs_sanitizerw  r,  r&	  internal_fieldeffective_system
sys_offsetr"  pfmsummary_extra_bodyr  
_OMIT_TEMP_raw_summary_temp_omit_summary_temperature_summary_temperaturer  _is_lmstudio_summary_lm_reasoning_effortcodex_kwargssummary_response_ct_sum_cnr_sumr  summary_kwargsr  _tsum_ant_kw_summary_resultretry_response	_ct_retry
_cnr_retry_tretry_ant_kw2_retry_resultru  s'                                          r   _handle_max_iterationszAIAgent._handle_max_iterations'  s	   bT5Hbbbccc. 	
 	ODDEEEp	A #>>@@OL - -((**44S'BBB&Y 6 6NKK5555" F<<WEEE##G,,,,#9?R+ f$4v$=@\$\#c#c#e#e  `)1>N O OPS__$ F"29QQ
 )$*? @ @ F FHC ''
S(8#((**EEEE  66|DDL  CCLQQL!#"ooooooooo " " "/3,!


"
 0; -,TZGGG 
 ):Z(G%+D#[44J[ %)==H $"++--3355C :7799 ! (2??AAA-1 ! ( D,O,O,Q,Q (46:6K&{33 $("*7 7&{3  F.D-E"6*} 111#55lCC  $///#'#9#9,#G#G --//"556FGG"*"2"8b!?!?!A!A "Z ," " (34HN=1?."))$*@*@*Q*QRRR'39MN#56 (*$) J373I(0) L595K(2' I484H(1% F373E(0' J5I&z2% F3EN<0=$888 //11E#00tzL`d.2oPTPe,0,D151N1N1P1P 1 R RG (,'F'Fw'O'O$&+&>&>?Ocgc{&>&|&|O&5&=&C%J%J%L%LNN (Dt'I'IQj'I'k'k'p'|  (D  (V  (V  GU  (V  (V$&*&9&9&;&;&N&NO_&`&`O&5&=&C%J%J%L%LN 3f..%'V,Db.`b`i%j%j%j%p%p%r%rN! fOO[^$T$TUUUU%eNN =$555#'#9#9,#G#GL $$Wd333%)%;%;L%I%IN $ 3 3 5 5I!*!=!=n!M!MJ&0&8&>B%E%E%G%GNN]&:::"1133G&33$*|cg-1-E/3QUQf262O2O2Q2Q  4  S  SH &*%D%DX%N%NN$+$>$>~aeay$>$z$zM&3&;&Ar%H%H%J%JNN "&$0& &N ,78L}52&--d.D.DT_.U.UVVV+7=Q'9:) J7I|4 (Jt'I'IQp'I'q'q'v  (C  (J  (\  (\  M[  (\  (\$$($7$7$9$9$L$LM]$^$^M&3&;&Ar%H%H%J%JN! f N22)+0H"ndfdm)n)n)n)t)t)v)v% j (X(XYYYY)i%eN 	A 	A 	AOBqBBCCC A$BU  A  Ax{|}x~x~  A  ANNNNNN	A s=   E] 9F ] F] FW] 
^"1^^stream_callbackpersist_user_messagec                     -./012 t                                                         ddlm}  | j                                                     t          |t                    rt          |          }t          |t                    rt          |          }| _	        d _
        | _        |pt          t          j                              }| _        d _        d _        d _        d _        d _        d _        d _        d _        d _        d _        d _         j                                         d _         j        dk    r;	                                  r                     d           n# t@          $ r Y nw xY w j!        r "                                 d _!        tG           j$                   _%        tM          |          }	tO          |	          dk    r|	dd         dz   n|	}
|
(                    d	d
          }
tR          *                    d j        pd j+         j,        pd j-        pdtO          |pg           |
           |rt]          |          ng }|r. j/        0                                s 1                    |            xj2        dz  c_2        tg           dd          }||4                                 ||n|}d} j5        dk    r9d j6        v r0 j7        r) xj8        dz  c_8         j8         j5        k    r	d}d _8        d|d}|9                    |           tO          |          dz
  }| _
         j:        sGtM          |          } ;                    d|dd          tO          |          dk    rdnd d            j<        =d}|rQ j=        rJ	  j=        >                     j                  }|r|?                    d          pd}n# t@          $ r Y nw xY w|r| _<        nމ @                    |           _<        	 ddlAmB}  |d j         j+        tg           dd          pd           n2# t@          $ r%}tR          C                    d|           Y d}~nd}~ww xY w j=        rY	  j=        D                     j         j<                   n2# t@          $ r%}tR          E                    d|           Y d}~nd}~ww xY w j<        } jF        rjtO          |           jG        jH         jG        jI        z   dz   k    r<t          ||pd jK        pd          }| jG        jL        k    rtR          *                    d |d! jG        jL        d! j+         jG        jM        d!            j:        s( ;                    d"|d!d# jG        jL        d!d$           t          d%          D ]}tO          |          } O                    ||||&          \  }}tO          |          |k    r nSd}d _        d _        d _        d _        d _        t          ||pd jK        pd          }| jG        jL        k     r nd}	 ddlAmB}  |d' j        |t]          |          t          |            j+        tg           dd          pdtg           d(d          pd)          }g }|D ]}t          |t                    r>|?                    d*          r)|9                    t          |d*                              Ut          |t                    r)|R                                r|9                    |           |rd+S                    |          }n2# t@          $ r%}tR          C                    d,|           Y d}~nd}~ww xY wd}d} d}!d}"d}#d}$d}%d}&d}'t          jU                    jV         _W        t          d jW                    jY        rt          d jW                   d _Z        nd _[        d _Z         j\        rK	 t          |t                    r|nd}( j\        ]                     j2        |(           n# t@          $ r Y nw xY wd}) j\        rG	 t          |t                    r|nd}* j\        ^                    |*          pd})n# t@          $ r Y nw xY w| j$        k     r j%        j_        dk    s j`        \r6 ja        b                                  jY        r"d}!d-}' j:        s ;                    d.           [n|dz  }| _c         d                    d/|             j`        rd _`        nT j%        e                                s;d0}' j:        s0 ;                    d1 j%        jf         d2 j%        jg         d3           [nm jh        "	 g }+t          t          |                    D ]\  },}-|-?                    d4          d5k    r|-?                    d6          rtO          |          |,z
  }.i .||.d         D ]N}/|/?                    d4          d7k    r n2|/?                    d8          }0|0r|/?                    d9d          .|0<   O.fd:|-d6         D             }+ n h                    ||+           n3# t@          $ r&}1tR          E                    d;||1           Y d}1~1nd}1~1ww xY w jk        dk    rd< j6        v r xjl        dz  c_l         m                                }2|2rid}3t          tO          |          dz
  d=d=          D ]}4||4         }5t          |5t                    r|5?                    d4          d7k    rd>|2 }6|5?                    d9d          }7t          |7t                    r	|7|6z   |5d9<   nB	 |7rt]          |7          ng }8|89                    d?|6d@           |8|5d9<   n# t@          $ r Y nw xY wd}3tR          E                    dA|4            n|3sqtg           dBd          }9|9<|95   jn        r jn        d	z   |2z    _n        n|2 _n        ddd           n# 1 swxY w Y   n"tg           dCd          }7|7r|7d	z   |2z   n|2 _n        tg           dDd          pt          jp        t                    }: r                    ||: j        E          };|;dk    r|:*                    dF|; j        pdG           g }<t          |          D ]a\  }=}>|>s                                }?|=|k    r|>?                    d4          dk    rg }@|)r&t          |)          }A|Ar|@9                    |A           |r|@9                    |           |@rI|??                    d9d          }Bt          |Bt                    r|Bd+z   d+S                    |@          z   |?d9<    u                    |>|?           dH|?v r|?v                    dH           dI|?v r|?v                    dI           |?v                    dJd            w                                r x                    |?           |<9                    |?           c|pd}C jy        r|Cd+z    jy        z   R                                }C|Cr	dK|Cdg|<z   }< jz        rK|Crdnd}Dt           jz                  D ]0\  }=}E|<{                    |D|=z   |Es                                           1 j|        rt          |< j~         j        L          }<                     |<          }<                     |<          }<|<D ]G}Ft          |F?                    d9          t                    r|Fd9         R                                |Fd9<   H|<D ]}F|F?                    d6          }G|Gsg }H|GD ]ؐ1t          1t                    rdM1v r	 t          j        1dM         dN                   }Ii 1dMi 1dM         dNt          j        |IdOdP          ii1nS# t@          $ rF t          1dM         dN         1dM         ?                    dQdR                    1dM         dN<   Y nw xY w|H9                    1           |H|Fd6<   t          |<           t          dS |<D                       }Jt          |<          }Kd2 j:        s                     d	 j         dT| d2 j$         d                                 j         dUtO          |<           dV|Kd!dW|Jd!dX                                 j         dY jK        rtO           jK                  nd            nt          j        t          j                              }Lt          j        t          j                              }M j        r                     |L d
|M d           ns                                 s_                                 rKt          j        g dZ          }Nt          |L d
|M d|N j        [          22                                  j        rt          jE        d\ j+         d]tO          |           d^ jK        rtO           jK                  nd            t          jE        d_|r|d=         d4         nd            t          jE        d`|Kd!da           t-          j                    }Od}P j        }Qd}Rd%}Sd}Td}Ud}Vd}Wd}Xd}Yd}Zd}[d}\d}]db}^d}_d}`|P|Qk     6rp j,        dck    r	 dddlm}am}b  |a            }c|c|cdk    rde |b|c           df}d                      j         dg|d dhdi                                dg|d                                             rd}Pd}&d}R                     ||           dg|d dj||dd|ddkS n# t:          $ r Y nt@          $ r Y nw xY w	                                                        |<          }` j        rtC          |`            j        dlk    r)                                                     |`dm          }`	 ddlAmB}  |dn| j        pd j-        pd j+         j,         j         j        |tO          |<          tO           jK        pg           |K|J j        o           n# t@          $ r Y nw xY wtM          dp          r                     |`dqr            2fds}ed}ftg           dtd          rd}fn j,        duk    svt           j        pd                                                              dv          s;t           j        pd                                                              dw          rd}fn;                                 s'ddxlm}g t          tg           dyd          |g          rd}f|fr                     |`|ez          }_n                     |`          }_t-          j                    |Oz
  }h2r2                    d           d2 j        r                     d            j:        s!                      j         d{|hd|d}            j        rG|_rtg          |_d~d          nd}it          jE        d|i dt_          |_d          r|_j        nd            d}jg }k j        dlk    r                                 }l|l                    |_          s|_d}j|k9                    d           n(t          tg          |_dd          pd          R                                                                }m|mdv rtg          |_dd          }nt          |nt                    r|n?                    d          n|nrt          |n          nd|m d}ot          jC        d|m|o                                            d}j|k9                    d|m d|o            nFtg          |_dd          }pt          |pt                    r|pR                                nd}q|qr*tR          E                    dtO          |q                     ntg          |_dd          }rtg          |_dd          }stR          C                    d|r|stg          |_d~d          d j         d j,                    d}j|k9                    d           nf j        dk    r[                                 }t|t                    |_          s0d}j|_|k9                    d           n|k9                    d           n  j        dk    rY                                 }u|u                    |_          s/d}j|_|k9                    d           n|k9                    d           n                                 }v|v                    |_          ssd}j|_|k9                    d           nYt_          |_d          s|k9                    d           n2|_j        |k9                    d           n|k9                    d           |jry2r2                    d           d2 j        r                     d           |Pdz  }P j        tO           j                  k     r                     d                                            rd}Pd}&d}Rd/d}w|_rpt_          |_d          r_|_j        rXt          |_j                  /t_          |_j        d          r,|_j        j        r |_j        j        ?                    dd          }wn/|_r-t_          |_d          r|_j        rt          |_j                  /|wdk    r$|_r"t_          |_d~          r|_j+        r
d|_j+         }w|wdk    rL|_rJd ts          |_                                          D             }x j        rt          jE        d|x            d}y|_rt_          |_d          r|_j        rytg          |_j        dd          }z|z4t          |_j        t                    r|_j        ?                    d          }z|z+	 tw          |z          }yn# tx          tz          f$ r Y nw xY w|ydk    rd|hdd}{na|ydk    rd|hdd}{nS|ydk    rd}{nJ|ydv rd|y d|hdd}{n;|ydv rd|y d}{n0|yd|y d|hdd}{n#|hdk     rd|hdd}{n|hdk    rd|hdd}{nd|hdd}}{                      j         d|P d2|Q ddS                    |k           di                                 j         d|w di                                /          }|                      j         d|| di                                 j         d|{ di           |P|Qk    r                     d|Q d                                            rd}Pd}&d}R
Չ                     d|Q d           t          j         j         d|Q dÝ                                ||           |d|d|Q d|{ ddŜS t          |PddǬȦ          }}                      j         d|}dd|{ d˝di           t          jC        d|P d2|Q ddS                    |k           d|w            t-          j                    |}z   }~d}t-          j                    |~k     r߉ jY        r[                      j         dΝdi                                ||                                             d|{ d|P d2|Q dѝ||dddҜS t-          j        dӦ           |dz  }|dz  dk    rB d                    d|P d2|Q d֐tw          |~t-          j                    z
             dם           t-          j                    |~k     ߐ j        dlk    rptg          |_dd          }tg          |_dd          }d}t          |t                    r|?                    dئ          }ntg          |dd          }|dk    r|dv rd}^ndb}^n j        dk    r/                                 }|                    |_j                  }^n j        dk    r1                                 }|                    |_          }|j        }^nj                                 }|                    |_          }|j        }^|}                     |^||          r!                      j         dܝdi           d}^|^dk    r                      j         dݝdi           d}                                 } j        dk    r|                    |_ j        ަ          }n|                    |_          }|}|rtg          |d9d          nd}|rt          tg          |d6d                    nd}t          |o!t          j        d|t          j                            }| o|o|dur                     |           p|du }|rWd}                      j         ddi           d}                     |                                ||           |||dd|dS  j        dv r|}| |s|#dz  }#                     ||^          }|9                    |           |j        r
|%|j        z  }%|#d%k     rZ                      j         d|# d           ddd}|9                    |           | _                             |           d}]&n\                     |%          R                                }                     |                                ||           |pd||ddddS  j        dv r|}||r|$dk     r&|$dz  }$                      j         ddi                                 j         ddi                                |                                ||           d||ddddS tO          |          dk    rf                      j         d                                |          }                     |                                ||           d||ddddS                       j         ddi                                ||           d||ddddkS t_          |_d          r|_j        rt          |_j         j,         j                  }|j        }|j        }|j        }|||d} jG                            |           tg           jG        dd          r~ jG        jM        }tg           jG        dd          rDt           j+         j        |            ;                     j         d|d!d j+                    d jG        _        d jG        _         xj        |z  c_         xj        |z  c_         xj        |z  c_         xj        dz  c_         xj        |j        z  c_         xj        |j        z  c_         xj        |j        z  c_         xj        |j        z  c_         xj        |j        z  c_        d}|j        r|rd|j         d2| dd|j        z  |z  dd}tR          *                    d j         j+         j,        pd||||h|	  	         t           j+        | j,         j        tg           dd                    }|j        # xj        t          |j                  z  c_        |j         _        |j         _         j=        r j        r	  j=                             j        |j        |j        |j        |j        |j        |j        t          |j                  nd|j        |j         j,         j        |j        dk    rdnd j+        d           n# t@          $ r Y nw xY w j        r7t          jE        d|d          d!d|d         d!d|d         d!           |j        }|j        }|d          }|s|rH j:        sA|dk    r||z  dz  nd}                      j         d|d!d2|d!d|dd|d!d
           d}[ j,        dck    r#	 dd	lm}  |             n# t@          $ r Y nw xY w d                    d
| d            n# t          $ r 2r2                    d           d2 j        r                     d           t-          j                    |Oz
  }                      j         ddi                                ||           d}!d|dd} Y  nt@          $ r}2r2                    d           d2 j        r                     d           t          |t                    r/tg           dd          dk     rt          |                                          }d|v pd|v }d|v pd|v o| }t          |          }t          |<t\                    rt          |<          rd}t          |`t                    rt          |`          rd}t          tg           dd          t\                    rt           jz                  rd}|s|rY xj        dz  c_        |r!                      j         ddi           n                       j         ddi           Y d}~|rd _        t          |          }t          |<t\                    rt          |<           t          |`t                    rtC          |`           d}t          tg           dd          t\                    rt           jz                  }d}t          tg           dd          t\                    rt           jK                  }d}t          |t                    r!t          |          }||k    r|}| _<        d}t          tg           dd          t                    r)t           jy                  }| jy        k    r	| _y        d}d}t          tg           dd          t                    r j        ?                    d          nd}t          |t                    rtC          |          }d}tg           dd          pd}|rt          |          }||k    r| _        t          tg           dd          t                    r
| j        d<   tg           dyd          "t_           j        d          r| j        _        d}                      j         ddi            xj        dz  c_        |p	|p|p|p|p|}|r!                      j         ddi           n                       j         ddi           Y d}~tg          |d d          }                     |          }tg           d!d          }|rtg          |d"d#          nd#}t          |tg           d$d          pdtg           d~d          pd|K||<rtO          |<          nd%          }tR          E                    d&|j        j        |j        |j         |j        |j        |j                                        ||[|j        |'          \  }}[|rY d}~|j        t
          j        k    r[|YsYd}Y                     |<          r&                      j         d(di           Y d}~^tR          *                    d)           |j        t
          j        k    r j        dk    r j        r|Zsd}Ztg           d*d          spd _	        	  j
                                         n# t@          $ r Y nw xY w                                                        j         d+di           Y d}~( j        dlk    rR j,        d,k    rF|d-k    r?|Ts=d}T                     di          r$                      j         d.           Y d}~ j        d/k    r j,        dck    rx|d-k    rp|Vsmd}V                     di          rt           j         d0           Y d}~dd1lm}  |            }d}	 tg          |d2d          ptg          |d3d          }|t          |          dd4         }n# t@          $ r Y nw xY wt           j         d5           |rt           j         d6|            t           j         d7           t           j         d8           t           j         d9           t           j         d:           t           j         d;| d<           t           j         d=            j,        d>k    rD|d-k    r=|Ws;d}W                                 r$                      j         d?           Y d}~f j        dk    r|d-k    rt_           d@          r|Usd}UddAlm}                                  rt           j         dB           Y d}~ω j        } ||          rdCndD}t           j         dE           t           j         dF|            t          |r+tO          |          dGk    r j         dH|ddG          dn
 j         dI           t           j         d8           dd1lm}  |            }t           j         dJ| dK           t           j         dL| dM           t           j         dN           t           j         dO           t           j         dP           t           j         dQ           |j        t
          j        k    r|Xsd}X|D ].}-t          |-t                    r|-v                    dRd           /                      j         dSdi           t          jC        dT j        tO          |                     Y d}~!|Pdz  }Pt-          j                    |Oz
  } d                    dU|P d2|Q d           t1          |          jq        }t          |                                          /                     |          }tR          C                    dV|P|Q|É                                 |Ħ           tg           d$d          }tg           dWd          }Btg           d~d          }|rdX| dYnd}ǉ                      j         dZ|P d2|Q d|Û |Ǜ di                                 j         d[|ś d\|ƛ di                                 j         d]|B di                                 j         d^|ě di           |rY|d_k     rRtg          |d2d          }|rt          |Ȧ          dd`         nd}|r"                      j         da|ɛ di                                 j         db|d|dctO          |<           dd|Kd!da                                            rde/v r                      j         df|ƛ dgdi            j        r@                      j         dhdi                                 j         didi                                 j         dj|ƛ di            jY        r                      j         dkdi                                ||                                             dl|Û d                     t          |                     dѝ||dddҜcY d}~S tg          |d d          }|j        t
          j        k    r)d#}ʉ jG        }|jM        }||k    r|ː                     j+        |ʉ j        tg           dd           j,        m           t_          |d          rd|_        d|_                              j         dn|d!do|d!dadi           |&dz  }&|&|Sk    rtO          |          }͉ O                    |||K|&          \  }}d}tO          |          |k     s||k    r?                     dp|d!dq|d!dr           t-          j        d           d}\Y d}~n|j        t
          j        t
          j        fv }|rr j        tO           j                  k     rUtA           j!                  }|s=                     ds                                |j        r          rd}Pd}&d}RY d}~' |r j,        dck    r|j        t
          j        k    r|sd}	 ddtlm"}ѐm#} tg          |d3d          }|rtg          |Ӑdud          nd} ||ԉ j$        v          }|r |||w           nt          j*        dx           n# t@          $ r Y nw xY w|r|Q}PY d}~'|j        t
          j%        k    }|Րr|&dz  }&|&|Sk    r                      j         dy|S dzdi                                 j         d{di           t          j         j         d||S d}                                ||           |d|d~|S dddddcY d}~S                      d|& d2|S d           tO          |          }͉ O                    |||K|&          \  }}d}tO          |          |k     rJ                     d|͛ dotO          |           d           t-          j        d           d}\Y d}~ni                      j         ddi                                 j         d{di           t          j         j         d                                ||           |d|dddddcY d}~S |j        t
          j&        k    }|֐rƉ jG        }|jM        }̐tO          /          }|ׁtQ          d|אdz
            }|؉ _)                              j         d|d!d|d!d|d!ddi           |&dz  }&|&|Sk    r                      j         dy|S ddi                                 j         d{di           t          j         j         d|S d}                                ||           |d|d|S dddddcY d}~S d}\Y d}~ntU          /          }tg           d$d          pd                                }tg           dWd          pd+                    d2                                          }|ڐdv p|۠                    d          }|o	|du od/v }|r2||k     r,|}މ                      j         d|d!dq|d!ddi           n:|r(|}މ                      j         d|d!ddi           ntY          |̦          }|r||k     r|ː                     j+        |މ j        tg           dd           j,        m           t_          |d          r!d|_        t          |o||k              |_                              j         d|d!do|d!dadi           n                       j         ddi           |&dz  }&|&|Sk    r                      j         dy|S ddi                                 j         d{di           t          j         j         d|S d}                                ||           |d|d|S dddddcY d}~S                      d|Kd!d|& d2|S d˝           tO          |          }͉ O                    |||K|&          \  }}d}tO          |          |k     s|rc||k     r]tO          |          |k     r,                     d|͛ dotO          |           d           t-          j        d           d}\Y d}~n                      j         ddi                                 j         ddi           t          j         j         d|Kd!d                                ||           |d|d|Kd!dddddcY d}~S t          |tz          tx          f          oAt          |t          t          j-        f           ot          |t\          j/                   }|pu|j          ol|j         oc|j        t
          j        t
          j        t
          j0        t
          j&        t
          j%        t
          j        t
          j        fvo| }|r                     d| d                                            rd}Pd}&d}RY d}~/|`                     |`d|                                d| d                     |                                            j         d| ddi                                 j         d[|ś d\|ƛ di                                 j         d]|B di           |j1        s|j        t
          j        k    r2|Őd,k    r|d-k    r                      j         ddi                                 j         ddi                                 j         ddi                                 j         ddi           nÉ                      j         ddi                                 j         ddi                                 j         d|ƛ dRdi           te          t          |B          d          r                       j         ddi           n                       j         ddi           t          j         j         d|            |dk    r;|Kdk    stO          |<          dk    r!                      j         ddi           n                     ||           d||ddt          |          dkcY d}~S |P|Qk    r|Rs$ 3                    ||P|Q          r
d}Rd}PY d}~2t                     d|Q d                                            rd}Pd}&d}RY d}~2                     |          }|r                     d|Q d|            n                     d|Q d|                                  j         d| di           tg          |d d           oti          /fddD                       }|r@                      j         ddi                                 j         ddi           t          j        d j        |Q|||tO          |<          |Kd!           |`                     |`d|                                ||           d|Q d| }|r|dz  }|||dd|dkcY d}~S d}|rtg          tg          |d3d          dud          }|r{t_          |d          ri|?                    d          p|?                    d¦          }|r;	 tk          tw          |          dæ          }n# tx          tz          f$ r Y nw xY w|r|nt          |PdĐdŬȦ          }}|r&                     d|}dd|Pdz    d2|Q d˝           n!                     d|}dd|P d2|Q d˝           tR          C                    d|}|P|Q                                 |           t-          j                    |}z   }~d}t-          j                    |~k     r jY        r^                      j         dΝdi                                ||                                             d|P d2|Q dѝ||dddҜcY d}~S t-          j        dӦ           |dz  }|dz  dk    rC d                    d|P d2|Q d֐tw          |~t-          j                    z
             dם           t-          j                    |~k     Y d}~nd}~ww xY w|P|Qk     6p|!rd}'n|\r(|dz  } j%        6                                 |Pdz  }Pd}\@|]r3 j        r j        nd}||#dz   z  }tk          |dͦ           _)        A |_4d}'t           j         dϝ                                ||           n	                                  }i } j        dk    r j        |d<    |j        |_fi |}|}|j        }^|j        t          |j        t                    st|j        }t          |t                    rH|?                    d?d          p*|?                    d9d          pt          j        |          |_        nt          |t\                    rg }|D ]}t          |t                    r|9                    |           -t          |t                    rD|?                    dѦ          d?k    r*|9                    |?                    d?d                     t          |t                    r,d?|v r(|9                    t          |d?                              d	S                    |          |_        nt          |          |_        	 ddlAmB} tg          |d6d          pg }|j        pd} |	 d[| j        pd j-        pd j+         j,         j         j        ||h|^tO          |<          tg          |_d~d           7                    |_          tO          |          tO          |          dӜ n# t@          $ r Y nw xY w|j        r} j:        sv j        r&                      j         d|j                    nI                      j         d|j        dd          tO          |j                  dk    rdnd            |j        r j8        r|j        R                                }t          j9        dd|          R                                }|r$|:                    d	          d         dd         nd}|r@tg           dd          dk    r*	  8                    d|           nH# t@          $ r Y n<w xY w|r6	  8                    dؐd|dd_         d           n# t@          $ r Y nw xY wtw          |j        pd          rԉ xj        dz  c_                              j         dٝ            j        dk    r)                      j         dډ j         d۝           E݉                      j         dܝdi           d _                             |          }                     |                                ||           d||ddddS d _         j        dlk    r|^dk    r xj        dz  c_                             ||^          }t          |?                    d9          pdR                                          }t          |?                    dH          t                    r5t          |?                    dHd          R                                          nd}t          |?                    dަ                    }t          |?                    dߦ                    }|s|s|s|rr|r|d=         nd}t          |t                    r|?                    dަ          nd}|?                    dަ          }t          |t                    r|?                    dߦ          nd}|?                    dߦ          }t          |t                    o|?                    d4          d5k    o|?                    dI          dk    ok|?                    d9          pd|?                    d9          pdk    o;|?                    dH          pd|?                    dH          pdk    o||k    o||k    }|s+|9                    |            <                    |            j        d%k     rL j:        s'                      j         d j         d           | _                             |           I;d _                             ||           d||ddddS t_           d          rd _        |j=        	rv j:        s5                      j         dtO          |j=                   d            j        rL|j=        D ]C1t          jE        d1j>        j?         d1j>        j@        dd4          d           D|j=        D ]z11j>        j?         j6        vra A                    1j>        j?                  }|r<t           j         d1j>        j?         d| d           |1j>        _?        { fd|j=        D             }|ry xj        dz  c_        dS                    t           j6                            }|d         } tO          |           dk    r| dd         dz   n| }                      j         d| d j         d            j        d%k    rK                      j         ddi           d _                             ||           d||ddd| dS                      ||^          }|9                    |           |j=        D ]X11j>        j?         j6        vrd1j>        j?         d| }nd}|9                    d71jC        |d           YL'd _        g 0|j=        D ]+11j>        j@        }t          |t          t\          f          r$t          j        |          1j>        _@        U|Ct          |t                    s-t          |          1j>        _@        1j>        j@        }|r|R                                sd1j>        _@        	 t          j        |           # t          j-        $ r=}09                    1j>        j?        t          |          f           Y d}~%d}~ww xY w0rАti          0fd|j=        D                       }|r_                      j         d|^ddi           d _                             |                                ||           d||ddddS  xj        dz  c_        0d         \  }/                      j         d| d/             j        d%k     r(                      j         dډ j         d           Nh                      j         d           d _                             ||^          }|9                    |           d 0D             }|j=        D ]f11j>        j?        |v r+t          1fd0D                       }	d|	 d}
nd}
|9                    d71jC        |
d           gO:d _         E                    |j=                  |_=         F                    |j=                  |_=                             ||^          }|j        pd}|rى                     |          rÐ| _        t          h d          -t          -fd |j=        D                       }| _        |r                                 rd _        n[ I                                rF                     |          R                                }|r                     d|            d}|rt          |d=         t                    rj|d=         ?                    dJ          rO|v                                 d}|r6t          |d=         t                    r|d=         ?                    dJ          O|rd _        d _        d _        |9                    |            <                    |            jJ        r(	  J                    d           n# t@          $ r Y nw xY w K                    ||||            j        f j        }d}' L                    |          }                      d|jM         d|jN                    |9                    d5| d           
nRd}$d _O        d |j=        D             }|dhk    r j%        6                                  jG        }|jP        dk    r
|jP        }nt          | jK        pd          } jF        rV|                    |          r? ;                    d            O                    || jG        jP        |&          \  }}d}| _                             |           R|j        pd} d _                             |           shtg           dd          pd}                     |          ryd	}'                     |          R                                }tR          *                    d
tO          |                                          d           |} d _Q        ntg           dd          }|rtg           dd          r{d}'tR          *                    d                                d           d _        d _        d _                             |          R                                } d _Q        nti          d |dd         D                       }|rtg           dd          sd _        d _        d _        tR          *                    d                                d                                ||^          }d|d9<   |9                    |           |9                    ddd           U6t          tg          |dHd          p#tg          |dd          ptg          |dRd                    }|r j        dk     r xj        dz  c_        tR          *                    d j                                        d j         d                                |d٦          }d|dJ<   |9                    |           | _                             |           V'                     |           R                                 }|o j        dk    }|rj|r|rd j        d%k     rY xj        dz  c_        tR          C                    d j         j+                                        d j         d           V͐|r j        rtR          C                    d j         j+         j,                                        d                                            rWd _                             d  j+         d j,         d           tR          *                    d! j+         j,                   Wd"}' R                    |          }                     ||^          }d|d9<   |9                    |           |r\tO          |          d_k    r|dd_         dz   n|}tR          C                    d#|                                d$           nQtR          C                    d% j         j+         j,                                        d& j        rd'nd(z              d} nd _        d _         j        dlk    r j6        r|"dk     r S                    || |)          r|"dz  }"                     |d٦          }|9                    |            <                    |           dd*d}|9                    |           | _                             |           YHd}"|%r	|%| z   } d}%d}#                     |           R                                }                      ||^          }|rt          |d=         t                    rg|d=         ?                    dJ          rL|v                                 |r6t          |d=         t                    r|d=         ?                    dJ          L|9                    |           d+|^ d}' j:        s ;                    d,| d-           n# t@          $ r}d.| dt          |           /	 t          d//            n4# t          tz          f$ r tR                              /           Y nw xY wtR          E                    d0|d1           t          tO          |          dz
  d=d=          D ]}=||=         }>t          |>t                    s n|>?                    d4          d7k    r;|>?                    d4          d5k    r|>?                    d6          rwd2 ||=dz   d         D             }|>d6         D ]U11rt          1t                    s1d3         |vr+d71d3         d4/ d}|9                    |           V | j$        dz
  k    r4d5/dd          d}'d6/ } |9                    d5| d           Y d}~n1Y d}~nd}~ww xY w| j$        k     r j%        j_        dk    \. j`        \6| | j$        k    s j%        j_        dk    rrd7| d2 j$         d}'                     d8| d2 j$         d9            j:        s" ;                    d1| d2 j$         d:            U                    ||          } | duo
| j$        k     } V                    |tM          |          |                                |                                ||           |r|d=         ?                    d4          nd} d}!| d7k    rt          |          D ]}-|-?                    d4          d5k    rp|-?                    d6          r[|-d6         }"|"rMt          |"d         t                    r1|"d=         ?                    dMi           ?                    dQ          }! nt          d; |D                       }#| rtO          |           nd}$ j%        r j%        jf        nd}% j%        r j%        jg        nd}&d<}'|' j+        | j$        |%|&|#| |$ j        pdf
}(| d7k    r"|!s tS          jC        d=|'z   d>z   g|(|!R   ntS          j*        |'g|(R   | ry|!sw	 ddlAmB}  |d? j        || t]          |           j+        tg           dd          pd@           n3# t@          $ r&}tR          C                    dA|           Y d}~nd}~ww xY wd})t          |          D ];}>|>?                    d4          d5k    r |>?                    dH          r|>dH         }) n<i dB| dC|)dD|dE|dF|dG|'dHddI|!dJtg           dKd          d~ j+        d$ j,        dW j        dL j        dM j        dN j        dO j        dP j         j         j         j        tg           jG        dQd          pd j         j         j        dR}* j         j        W                                |*dS<    m                                }+|+r|+|*dT<   d _Q        |!r j[        r j[        |*dU<                                     d _	        d}, jk        dk    r# jl         jk        k    rd< j6        v r
d},d _l         X                    || |!V           | rA|!s?|s|,r:	  Y                    t]          |          ||,W           n# t@          $ r Y nw xY w	 ddlAmB}  |dX j        ||! j+        tg           dd          pdY           n3# t@          $ r&}tR          C                    dZ|           Y d}~nd}~ww xY w|*S (\  a[  
        Run a complete conversation with tool calling until completion.

        Args:
            user_message (str): The user's message/question
            system_message (str): Custom system message (optional, overrides ephemeral_system_prompt if provided)
            conversation_history (List[Dict]): Previous conversation messages (optional)
            task_id (str): Unique identifier for this task to isolate VMs between concurrent tasks (optional, auto-generated if not provided)
            stream_callback: Optional callback invoked with each text delta during streaming.
                Used by the TTS pipeline to start audio generation before the full response.
                When None (default), API calls use the standard non-streaming path.
            persist_user_message: Optional clean user message to store in
                transcripts/history when user_message contains API-only
                synthetic prefixes.
                    or queuing follow-up prefetch work.

        Returns:
            Dict: Complete conversation result with final response and message history
        r   )set_session_contextNFr  u~   🔌 Detected stale connections from a previous provider issue — cleaned up automatically. Proceeding with fresh connection.rR  r  r   r  zPconversation turn: session=%s model=%s provider=%s platform=%s history=%d msg=%rr8  r  r   r  rS  Tr  rL	  u   💬 Starting conversation: 'rC  r   rf  r  )invoke_hookr<  r  )r  r  r  z on_session_start hook failed: %sz*Session DB update_system_prompt failed: %srP	  zDPreflight compression: ~%s tokens >= %s threshold (model %s, ctx %s)rI  u   📦 Preflight compression: ~z tokens >= z
 thresholdrm  )rC	  r  pre_llm_callr  )r  rj  r  is_first_turnr  r  	sender_idr  r  zpre_llm_call hook failed: %sinterrupted_by_useru2   
⚡ Breaking out of tool loop due to interrupt...zstarting API call #budget_exhaustedu%   
⚠️  Iteration budget exhausted (r  z iterations used)r+  r  r   r>  r  r)  c                     g | ]h}t          |t                    |d          d                             |                    d                    |d                              d          diS )r   r   r*  r   )r   rL  r   r,  )r   r   _results_by_ids     r   r   z,AIAgent.run_conversation.<locals>.<listcomp>)*  s~     * * * %'#-b$#7#7*,.zN6,B.<.@.@.N.N13J1C1CK1P1P!" !"* * *r   z&step_callback error (iteration %s): %sr  rQ  rx  r  ry  z<Pre-API-call steer drain: injected into tool msg at index %dr  r  rS  r0	  zFSanitized %s corrupted tool_call arguments before request (session=%s)r-  r  ra  rS
  r  )r  native_anthropicr   r   rH  )rK  	sort_keysr   rB  c              3   N   K   | ] }t          t          |                    V  !d S r   r  rf  s     r   r   z+AIAgent.run_conversation.<locals>.<genexpr>	+  s.      DDc#c((mmDDDDDDr   u   🔄 Making API call #u      📊 Request size: z messages, ~z
 tokens (~z chars)u      🔧 Available tools: )brainsparklepulsemoonstarr	  zAPI Request - Model: z, Messages: z	, Tools: zLast message role: zTotal message size: ~z tokensrc  r  )nous_rate_limit_remainingformat_remainingu,   Nous Portal rate limit active — resets in .u   ⏳ z Trying fallback...r  zg

No fallback provider available. Try again after the reset, or add a fallback provider in config.yaml.)r  r'  	api_callsr  ru	  r  r  r  pre_api_request)r  r  r  r  r  r   r  r  r_  
tool_countapprox_input_tokensrequest_char_countr  HERMES_DUMP_REQUESTS	preflightr  c                  v    r                     d           d  j        r                     d           d S d S r  )rc  r  )r   thinking_spinners   r   _stop_spinnerz/AIAgent.run_conversation.<locals>._stop_spinner+  sT    + 4,11"555/3,1 7 222666667 7r   r  r  r  r  rW  r  r  u   ⏱️  API call completed in r
  r+  r  zN/AzAPI Response received - Model: z	, Usage: r5  zresponse is Noner  >   ru	  	cancelledr  r  zResponses API returned status 'z>Codex response status='%s' (error=%s). Routing to fallback. %szresponse.status=r  r  zaCodex response.output is empty but output_text is present (%d chars); deferring to normalization.r  zeCodex response.output is empty after stream backfill (status=%s, incomplete_details=%s, model=%s). %sz	api_mode=rQ  zresponse.output is emptyz/response.content invalid (not a non-empty list)r  z/Bedrock response invalid (no output or choices)r  z#response has no 'choices' attributezresponse.choices is Nonezresponse.choices is emptyu   (´;ω;`) oops, retrying...u<   ⚠️ Empty/malformed response — switching to fallback...Unknownr  rT  zmodel=c                 n    i | ]2\  }}|                     d           |t          |          dd         3S )rb  Nr  )r  r   r  s      r   r  z,AIAgent.run_conversation.<locals>.<dictcomp>I,  sA    )v)v)vdadedpdpqtdudu)v!SVVDSD\)v)v)vr   z*Response attributes for invalid response: r%  i  z-upstream provider timed out (Cloudflare 524, z.0frU  i  zupstream gateway timeout (504, r!  z'rate limited by upstream provider (429))r  i  zupstream server error (r@  )i  i  zupstream provider overloaded (r  zupstream error (code rR  zfast response (r	  u   s) — likely rate limitedzslow response (u   s) — likely upstream timeoutzresponse time u&   ⚠️  Invalid API response (attempt z): u      🏢 Provider: u      📝 Provider message: u      ⏱️  u   ⚠️ Max retries (u.   ) for invalid responses — trying fallback...u   ❌ Max retries (z,) exceeded for invalid responses. Giving up.zInvalid API response after z	 retries.z
 retries: )r'  r  r
  r  ru	  r	  r  )
base_delay	max_delayu   ⏳ Retrying in zs (z)...zInvalid API response (retry z | Provider: u3   ⚡ Interrupt detected during retry wait, aborting.z$Operation interrupted during retry (z
, attempt z).)r  r'  r
  r  r  rt  r!  zretry backoff (z), zs remainingr6  
incomplete>   r  max_output_tokensr  uA   ⚠️  Treating suspicious Ollama/GLM stop response as truncateduQ   ⚠️  Response truncated (finish_reason='length') - model hit max output tokensrX
  z8<(?:think|thinking|reasoning|REASONING_SCRATCHPAD)[^>]*>zModel used all output tokens on reasoning with none left for the response. Try lowering reasoning effort or increasing max_tokens.uV   💭 Reasoning exhausted the output token budget — no visible response was produced.u  ⚠️ **Thinking Budget Exhausted**

The model used all its output tokens on reasoning and had none left for the actual response.

To fix this:
→ Lower reasoning effort: `/thinkon low` or `/thinkon minimal`
→ Or switch to a larger/non-reasoning model with `/model`)r  r'  r
  r  partialr  )r  r  r  u   ↻ Requesting continuation (z/3)...z[System: Your previous response was truncated by the output length limit. Continue exactly where you left off. Do not restart or repeat prior text. Finish the answer directly.]z9Response remained truncated after 3 continuation attemptsu=   ⚠️  Truncated tool call detected — retrying API call...uf   ⚠️  Truncated tool call response detected again — refusing to execute incomplete tool arguments.z-Response truncated due to output length limitu3      ⏪ Rolling back to last complete assistant turnu-   ❌ First response truncated - cannot recoverz3First response truncated due to output length limitr7  )r9  completion_tokensr:  _context_probed_context_probe_persistableu   💾 Cached context length: z tokens for z cache=r  r  z%)zHAPI call #%d: model=%s provider=%s in=%d out=%d total=%d latency=%.1fs%sr  )r  r   r  includedsubscription_included)input_tokensoutput_tokenscache_read_tokenscache_write_tokensreasoning_tokensestimated_cost_usdcost_statuscost_sourcebilling_providerbilling_base_urlbilling_moder  r  zToken usage: prompt=r9  z, completion=r
  z, total=r:  u      💾 Cache: z	 tokens (z% hit, z	 written))clear_nous_rate_limitz
API call #z
 completedu    ⚡ Interrupted during API call.z3Operation interrupted: waiting for model response (zs elapsed).u   (╥_╥) error, retrying..._unicode_sanitization_passesr6  z'ascii'rf  	surrogatez'utf-8'r  uH   ⚠️  Stripped invalid surrogate characters from messages. Retrying...uP   ⚠️  Surrogate encoding error — retrying after full-payload sanitization...ro  r  r  r  u   ⚠️  API key contained non-ASCII characters (bad copy-paste?) — stripped them. If auth fails, re-copy the key from your provider's dashboard.ud   ⚠️  System encoding is ASCII — stripped non-ASCII characters from request payload. Retrying...uT   ⚠️  System encoding is ASCII — enabling full-payload sanitization for retry...r  r  ry  i@ r  )r  r  rC	  ry  num_messageszTError classified: reason=%s status=%s retryable=%s compress=%s rotate=%s fallback=%s)r  r  r  r  uE   📐 Image(s) exceeded provider size limit — shrank and retrying...zlimage-shrink recovery: no data-URL image parts found or shrink didn't reduce size; surfacing original error.r0  ui   🔕 OAuth subscription doesn't support the 1M-context beta — disabled for this session and retrying...r  r"  u8   🔐 Codex auth refreshed after 401. Retrying request...r  u<   🔐 Nous agent key refreshed after 401. Retrying request...)display_hermes_homer  r*  r   u/   🔐 Nous 401 — Portal authentication failed.z   Response: zS   Most likely: Portal OAuth expired, account out of credits, or agent key revoked.z   Troubleshooting:u6        • Re-authenticate: hermes login --provider nousuA        • Check credits / billing: https://portal.nousresearch.comu$        • Verify stored credentials: z
/auth.jsonuK        • Switch providers temporarily: /model <model> --provider openrouterrD  uA   🔐 Copilot credentials refreshed after 401. Retrying request...r  r  uC   🔐 Anthropic credentials refreshed after 401. Retrying request...zBearer (OAuth/setup-token)zx-api-key (API key)u-   🔐 Anthropic 401 — authentication failed.z   Auth method: r  z   Token prefix: z   Token: (empty or short)u"        • Check ANTHROPIC_TOKEN in z+/.env for Hermes-managed OAuth/setup tokensu$        • Check ANTHROPIC_API_KEY in z)/.env for API keys or legacy token valuesuJ        • For API keys: verify at https://platform.claude.com/settings/keysuD        • For Claude Code: run 'claude /login' to refresh, then retryu=        • Legacy cleanup: hermes config set ANTHROPIC_TOKEN ""uA        • Clear stale keys: hermes config set ANTHROPIC_API_KEY ""r  uV   ⚠️  Thinking block signature invalid — stripped all thinking blocks, retrying...zP%sThinking block signature recovery: stripped reasoning_details from %d messageszAPI error recovery (attempt z;API call failed (attempt %s/%s) error_type=%s %s summary=%sr   z [HTTP rO  u!   ⚠️  API call failed (attempt u      🔌 Provider: z	  Model: u      🌐 Endpoint: u      📝 Error: r  r  u      📋 Details: u      ⏱️  Elapsed: zs  Context: z msgs, ~zsupport tool useu$      💡 No OpenRouter providers for z1 support tool calling with your current settings.zU      Your provider_routing.only restriction is filtering out tool-capable providers.zY      Try removing the restriction or adding providers that support tools for this model.zH      Check which providers support tools: https://openrouter.ai/models/u?   ⚡ Interrupt detected during error handling, aborting retries.z+Operation interrupted: handling API error (r  uO   ⚠️  Anthropic long-context tier requires extra usage — reducing context: r;  u   🗜️ Context reduced to z tokens (was z), retrying...u9   ⚠️ Rate limited — switching to fallback provider...)is_genuine_nous_rate_limitrecord_nous_rate_limitrq  )rq  last_known_state)rq  r  zNous 429 looks like upstream capacity (no exhausted bucket in headers or last-known state) -- not tripping cross-session breaker.u   ❌ Max compression attempts (z&) reached for payload-too-large error.uR      💡 Try /new to start a fresh conversation, or /compress to retry compression.z413 compression failed after z
 attempts.z5Request payload too large: max compression attempts (z
) reached.)r'  r  r
  r  r
  ru	  compression_exhaustedu@   ⚠️  Request payload too large (413) — compression attempt u   🗜️ Compressed z messages, retrying...u2   ❌ Payload too large and cannot compress further.z/413 payload too large. Cannot compress further.z9Request payload too large (413). Cannot compress further.@   uM   ⚠️  Output cap too large for current prompt — retrying with max_tokens=z (available_tokens=z; context_length unchanged at z!Context compression failed after z3Context length exceeded: max compression attempts (>   r+  r,  )z https://api.minimax.io/anthropicz"https://api.minimaxi.com/anthropiczcontext window exceeds limit (z!Context limit detected from API: zBProvider reported overflow amount only; keeping context_length at z tokens and compressing.u3   ⚠️  Context length exceeded — stepping down: uM   ⚠️  Context length exceeded at minimum tier — attempting compression...u   🗜️ Context too large (~u    tokens) — compressing (u8   ❌ Context length exceeded and cannot compress further.u      💡 The conversation has accumulated too much content. Try /new to start fresh, or /compress to manually trigger compression.zContext length exceeded: z! tokens. Cannot compress further.zContext length exceeded (z" tokens). Cannot compress further.u!   ⚠️ Non-retryable error (HTTP u   ) — trying fallback...non_retryable_client_error)r6  r  u   ❌ Non-retryable error (HTTP u%   ❌ Non-retryable client error (HTTP z). Aborting.uK      💡 Codex OAuth token was rejected (HTTP 401). Your token may have beenz?      refreshed by another client (Codex CLI, VS Code). To fix:z?      1. Run `codex` in your terminal to generate fresh tokens.z3      2. Then run `hermes auth` to re-authenticate.u9      💡 Your API key was rejected by the provider. Check:u-         • Is the key valid? Run: hermes setupu+         • Does your account have access to r  u?         • Check credits: https://openrouter.ai/settings/creditsu6      💡 This type of error won't be fixed by retrying.zNon-retryable client error: i  r!  uU   ⚠️  Skipping session persistence for large failed session to prevent growth loop.)r,  r-  u"   ) exhausted — trying fallback...u   ❌ Rate limited after u    retries — u   ❌ API failed after u      💀 Final error: c              3       K   | ]}|v V  	d S r   r   )r   r  r  s     r   r   z+AIAgent.run_conversation.<locals>.<genexpr>1  s7       $ $qAN $ $ $ $ $ $r   )r  r  r  r  r  r  u      💡 The provider's stream connection keeps dropping. This often happens when the model tries to write a very large file in a single tool call.z~      Try asking the model to use execute_code with Python's open() for large files, or to write the file in smaller sections.zP%sAPI call failed after %s retries. %s | provider=%s model=%s msgs=%s tokens=~%smax_retries_exhaustedzAPI call failed after u  

The provider's stream connection keeps dropping — this often happens when generating very large tool call responses (e.g. write_file with long content). Try asking me to use execute_code with Python's open() for large files, or to write in smaller sections.r   r+  r,  rz  rT  g      N@u   ⏱️ Rate limited. Waiting zs (attempt z4Retrying API call in %ss (attempt %s/%s) %s error=%sz<Operation interrupted: retrying API call after error (retry zerror retry backoff (interrupted_during_api_callr  i   !all_retries_exhausted_no_responseu:   ❌ All API retries exhausted with no successful response.rY
  r   post_api_request)r  r  r  r  r  r   r  r  api_durationra  r_  response_modelr5  assistant_content_charsassistant_tool_call_countu   🤖 Assistant: z,</?(?:REASONING_SCRATCHPAD|think|reasoning)>r  	_thinkingzreasoning.availableuL   ⚠️  Incomplete <REASONING_SCRATCHPAD> detected (opened but never closed)u   🔄 Retrying API call (z/2)...uA   ❌ Max retries (2) for incomplete scratchpad. Saving as partial.z/Incomplete REASONING_SCRATCHPAD after 2 retriesr  r  u0   ↻ Codex response incomplete; continuing turn (z/3)z@Codex response remained incomplete after 3 continuation attempts_codex_incomplete_retriesu   🔧 Processing z tool call(s)...zTool call: z with args: u   🔧 Auto-repaired tool name: 'z' -> 'c                 N    g | ]!}|j         j        j        v|j         j        "S r   )r   r   r  )r   r   r   s     r   r   z,AIAgent.run_conversation.<locals>.<listcomp>2  s;     * * *-/;+43HHH (HHHr   u   ⚠️  Unknown tool 'u2   ' — sending error to model for self-correction (uI   ❌ Max retries (3) for invalid tool calls exceeded. Stopping as partial.z#Model generated invalid tool call: zTool 'z#' does not exist. Available tools: zZSkipped: another tool call in this turn used an invalid name. Please retry this tool call.r5	  rE  c              3      K   | ]P}|j         j        d  D             v |j         j        pd                                                    d           V  QdS )c                     h | ]\  }}|S r   r   )r   r?  rb  s      r   r?  z5AIAgent.run_conversation.<locals>.<genexpr>.<setcomp><3  s    3T3T3T$!QA3T3T3Tr   r   )rM  rO  N)r   r   r   r  r[  )r   r   invalid_json_argss     r   r   z+AIAgent.run_conversation.<locals>.<genexpr>93  sw       ) ) "!{/3T3TBS3T3T3TTT "$!6!<" D D F F O OPZ [ [[TTTT) )r   u>   ⚠️  Truncated tool call arguments detected (finish_reason=u   ) — refusing to execute.u1   ⚠️  Invalid JSON in tool call arguments for 'r	  u;   ⚠️  Injecting recovery tool results for invalid JSON...c                     h | ]\  }}|S r   r   )r   r   rb  s      r   r?  z+AIAgent.run_conversation.<locals>.<setcomp>e3  s    ,S,S,SgdAT,S,S,Sr   c              3   B   K   | ]\  }}|j         j        k    |V  d S r   r   )r   r?  ru  r   s      r   r   z+AIAgent.run_conversation.<locals>.<genexpr>h3  s9      .h.hTQRSWYWbWgRgRgqRgRgRgRg.h.hr   zError: Invalid JSON arguments. z_. For tools with no required parameters, use an empty object: {}. Please retry with valid JSON.z;Skipped: other tool call in this response had invalid JSON.>   r	  rS  r  r   c              3   4   K   | ]}|j         j        v V  d S r   r   )r   r   _HOUSEKEEPING_TOOLSs     r   r   z+AIAgent.run_conversation.<locals>.<genexpr>3  sB       0 0 " K,0CC0 0 0 0 0 0r   u     ┊ 💬 guardrail_haltu   ⚠️ Tool guardrail halted c                 &    h | ]}|j         j        S r   r   r   s     r   r?  z+AIAgent.run_conversation.<locals>.<setcomp>3  s     Y Y Yb!1 Y Y Yr   execute_coderr  u     ⟳ compacting context…r  partial_stream_recoveryuG   Partial stream content delivered (%d chars) — using as final responseuD   ↻ Stream interrupted — using delivered content as final response_last_content_with_tools$_last_content_tools_all_housekeepingfallback_prior_turn_contentuO   Empty follow-up after tool calls — using prior turn content as final responseuM   ↻ Empty response after tool calls — using earlier content as final answerc              3   H   K   | ]}|                     d           dk    V  dS re  r  r  s     r   r   z+AIAgent.run_conversation.<locals>.<genexpr>f4  sE       . . ! EE&MMV3. . . . . .r   _post_tool_empty_retrieduH   Empty response after tool calls — nudging model to continue processinguD   ⚠️ Model returned empty after tool calls — nudging to continuerr  z~You just executed tool calls but returned an empty response. Please process the tool results above and continue with the task.r  uM   Thinking-only response (no visible content) — prefilling to continue (%d/2)u7   ↻ Thinking-only response — prefilling to continue (z/2)uB   Empty response (no content or reasoning) — retry %d/3 (model=%s)u/   ⚠️ Empty response from model — retrying (uO   Empty response after %d retries — attempting fallback (model=%s, provider=%s)uL   ⚠️ Model returning empty responses — switching to fallback provider...u   ↻ Switched to fallback: z<Fallback activated after empty responses: now using %s on %sempty_response_exhaustedzaReasoning-only response (no visible content) after exhausting retries and fallback. Reasoning: %su[   ⚠️ Model produced reasoning but no visible response after all retries. Returning empty.zfEmpty response (no content or reasoning) after %d retries. No fallback available. model=%s provider=%su/   ❌ Model returned no content after all retriesz and fallback attempts.z#. No fallback providers configured.)rj  rk  r'  zr[System: Continue now. Execute the required tool calls and only send your final answer after completing the task.]ztext_response(finish_reason=u"   🎉 Conversation completed after z OpenAI-compatible API call(s)z)Error during OpenAI-compatible API call #u   ❌ z Outer loop error in API call #%dr  c                 z    h | ]8}t          |t                    r!|                    d           dk    0|d         9S r  r,  r  s     r   r?  z+AIAgent.run_conversation.<locals>.<setcomp>\5  sP     ( ( ( !)!T22( 89uuV}}7N7N n-7N7N7Nr   r*  zError executing tool: zerror_near_max_iterations(z0I apologize, but I encountered repeated errors: zmax_iterations_reached(u#   ⚠️ Iteration budget exhausted (u   ) — asking model to summariseu   ) — requesting summary...c              3      K   | ]I}t          |t                    r2|                    d           dk    0|                    d          EdV  JdS )r+  r  r   r   Nr,  r  s     r   r   z+AIAgent.run_conversation.<locals>.<genexpr>5  sf       
 
!T""
'(uuV}}'C'ClH[H['C 'C'C'C'C
 
r   zuTurn ended: reason=%s model=%s api_calls=%d/%d budget=%d/%d tool_turns=%d last_msg_role=%s response_len=%d session=%sz>Turn ended with pending tool result (agent may appear stuck). z last_tool=%spost_llm_call)r  rj  assistant_responser  r  r  zpost_llm_call hook failed: %sr  last_reasoningr'  r
  r  turn_exit_reasonr
  r  response_previewed_response_was_previewedr
  r
  r
  r
  r
  r\	  )r9  r
  r:  r\	  r
  r
  r
  r  pending_steerinterrupt_message)r  r  r  )r  r  r  r  )r  r  r  r  r  zon_session_end hook failed: %s)r
  (Z  r   r  r  r|
  r  r(  r!   r   r  r  r  r  r  r  _current_task_id_invalid_tool_retries_invalid_json_retries_empty_content_retries_incomplete_scratchpad_retriesr
  _thinking_prefill_retriesr
  r
  r
  r  r
  r  reset_for_turnr  r  r  r  r   rN  r  r   r  r  rh   r   r.  rS  r  r  r  r  r  r  	has_itemsr  r?  r   rO	  r  r  r  r   r   r  r  r  r  get_sessionr   r  r6  r}
  rT  rX	  r   r9  r  r  ru  rQ   ro  rM  ry  rZ  rl	  r   r   r   r;  r   rL  rM  r  rg  r  r  r  r$  on_turn_startprefetch_allr   r  r  new_turnr  r  r   r   r   r  r   r  r/  r!  ru  r  r   r  r'   r@	  r  rA   r*	  r<  rB	  r/	  r  r  r7	  r  rZ   r  r  r  r  r   r   rU  rd  r2  r"  rP   r  r  r
  r
  ri   get_thinking_facesget_thinking_verbsr  r  r  r  r  r  r  r1  agent.nous_rate_guardr
  r
  r  r  ImportErrorrh  r  r  rq  r  r  r   r  ry   r[  r  r  r[  rX  r  r_  rc  r  r5  validate_responserR  r  r   r  r  r  r  varsr  r   rW  r   r#  rC   rq  r1  map_finish_reasonstop_reasonr\
  ra  ri  r  rX  r   rW  rM  r  r	  r)  r  r  rK  r  rd   r9  r
  r:  update_from_responserU   r
  r
  r@  rA  rB  rC  rD  r
  rE  rF  r
  rG  r
  rH  r
  rc   
amount_usdrI  r2  r  rJ  rz  rK  update_token_countsr
  r  UnicodeEncodeErrorr&  rn  rs  rk  r  r  r  r3  rD   r6  r   r  	retryableshould_compressshould_rotate_credentialshould_fallbackr,  rE   image_too_larger  !oauth_long_context_beta_forbiddenr0  r  r  r5  r  r  r  hermes_constantsr
  r  r  r  r  r  thinking_signaturer   r  r  r  long_context_tierr8  r$  r#  r  r  r
  r
  r  payload_too_largecontext_overflowrT   r   r  rS   r  rR   rV  sslSSLError
overloadedis_authrw   r4  r   r  r   r>  r  r  r"  rt   ru  r   r   r   r   r?  r  r*  nextr$  r(  	frozensetallr  r  r	  rs	  r  r%  r  r\	  r
  r  r  r   rx
  r  to_metadatar  r  (3  r   rj  r  r  r  ry
  rz
  r|
  r~	  _preview_text_msg_previewr'  re  r  _should_review_memoryuser_msgcurrent_turn_user_idx_print_previewstored_promptsession_row_invoke_hookr  ru  active_system_prompt_preflight_tokens_pass	_orig_len_plugin_user_context_pre_results
_ctx_partsr	  r  r  r  codex_ack_continuationslength_continue_retriestruncated_tool_call_retriestruncated_response_prefixcompression_attempts_turn_exit_reason	_turn_msg_ext_prefetch_cache_query
prev_tools_idx_m
_fwd_start_tm_tcid	_step_err_pre_api_steer	_injected_si_smr  r   r  r   request_loggerrepaired_tool_callsrw  r"  r,  r&	  _injections_fencedr  r`
  ra
  rb
  amtcsnew_tcsargs_objtotal_charsrC	  r,
  verbr	  api_start_timer,  r-  primary_recovery_attemptedmax_compression_attemptscodex_auth_retry_attemptedanthropic_auth_retry_attemptednous_auth_retry_attemptedcopilot_auth_retry_attemptedthinking_sig_retry_attemptedimage_shrink_retry_attemptedoauth_1m_beta_retry_attemptedr   restart_with_compressed_messages restart_with_length_continuationra  r*  r?  r
  _fmt_nous_remaining_nous_remaining	_nous_msgr
  _use_streamingrX  r
  
resp_modelresponse_invaliderror_details_ct_v_codex_resp_status_codex_error_obj_codex_error_msg	_out_text_out_text_stripped_resp_status_resp_incomplete_tv_btv_ctvrT  
resp_attrs_resp_error_code	_code_raw_failure_hintcleaned_provider_errorr3  	sleep_end_backoff_touch_counterr  r  incomplete_reason_tfr_bt_fr_bedrock_result_cc_fr_finish_resultrg  
_trunc_msg_trunc_transport_trunc_result_trunc_content_trunc_has_tool_calls_has_think_tags_thinking_exhausted_exhaust_error_exhaust_responseinterim_msgcontinue_msgpartial_responserolled_back_messagescanonical_usager9  r
  r:  
usage_dictr/
  
_cache_pctcost_resultr\  writtenr  hit_pctr
  api_elapsedr+  _err_str_is_ascii_codec_is_surrogate_error_surrogates_found_messages_sanitized_prefill_sanitized_tools_sanitized_system_sanitized_sanitized_system_sanitized_ephemeral_headers_sanitizedr&  _credential_sanitized_raw_key
_clean_key_any_sanitizedr  r  _compressor_ctx_len
classifiedrecovered_with_pool_dhh_fn_dhh
_body_text_bodyr  r   auth_methodelapsed_timer2  _error_summary	_provider_model_status_code_str	_err_body_err_body_str_reduced_ctxr  old_ctxoriginal_lenis_rate_limitedpool_may_recover_genuine_nous_rate_limitr
  r
  	_err_resp	_err_hdrsis_payload_too_largeis_context_length_erroravailable_outsafe_outparsed_limit_provider_lower_base_lowerr;  minimax_delta_only_overflownew_ctxis_local_validation_erroris_client_error_final_summary_is_stream_drop_final_response_retry_after_resp_headers_ra_raw_boost_base_boostr  _normalize_kwargsr:  r3  r  r-  _assistant_tool_calls_assistant_text_think_text
first_lineinterim_has_contentinterim_has_reasoninginterim_has_codex_reasoninginterim_has_codex_message_itemslast_msglast_codex_itemsinterim_codex_itemslast_codex_message_itemsinterim_codex_message_itemsduplicate_interimr  invalid_tool_callsrB  invalid_nameinvalid_previewrp  r)  r   
_truncatedr  recovery_assistantinvalid_namesr  rQ  turn_content_all_housekeepingclean_had_prefillrm	  	_tc_names_real_tokens_partial_streamed
_recoveredfallback_prior_was_tool
_nudge_msg_has_structured_truly_empty_prefill_exhaustedr  reasoning_preview	final_msganswered_idserr_msgr  _last_msg_role_last_tool_name_tcs_turn_tool_count	_resp_len_budget_used_budget_max	_diag_msg
_diag_argsr
  rL  _leftover_steer_should_review_skillsr
  r
  r  r
  r   r
  s3  `                                                                                                                                                                                                                                                                                                            @@@@@@r   r  zAIAgent.run_conversation(  s{   < 	!!! 	766666DO,,,
 	%%'''
 lC(( 	>/==L*C00 	N#78L#M#M  !0)-&.B+#8s4:<<'8'8
 !2 &'"%&"&'#./+)*&)*&(-%(,%491#( ,-),,...-1*
 =0001133 %%&  
     $ 	-,,...(,D%
 !00C D D 8EE7:=7I7IB7N7Ncrc*U22Ta#++D#66^O%vtz4=3MIM&Y,@,FB(G(G		
 	
 	
 2FM4,---2
   	;(8(B(B(D(D 	;$$%9::: 	"
 4!;TBBNN 9M8X 4 4^j
 !&'!++ 555& 6$$)$$'4+FFF(,%+,( #|<<!!! #H 1)>& 	@<\JJN~^CRC=P~[^_m[n[nqs[s[sRWRWy{~~~ %- M# (8 "&"2">">t"O"OK" Q(3(H(H(PD    D  V .;** .2-F-F~-V-V*
	LNNNNNN L*#'?"j!(z4!@!@!FB	     ! L L LNN#EsKKKKKKKKL # VV(==dotOijjjj$ V V V%QSTUUUUUUUUV  $9 $;	H 7 G"&"9"H!IKL!M M M
 !>28bj(D! ! ! !D$;$LLLZ(,,.?CCJ.=AA    $$U8IU U U"5FTU U U   #1XX  E #HI595K5K .@Q 1 6L 6 62H2 8}}	11 ,0( 34D/56D248D1@ED=/4D,(E &:&@b"j0D) ) )%
 )4+B+SSS T  "	@FFFFFF'<?2%)(^^#'(<#=#==j z488>B!$
D99?R	 	 	L %'J! ) )a&& )155+;+; )%%c!I,&7&788883'' )AGGII )%%a((( ?'-{{:'>'>$ 	@ 	@ 	@NN93????????	@ "#"#&'#$&! %
 %.$<$>$>$D!
 	ud7888$ 	:4!:;;;49D11&*D#49D1
  	5?@UWZ5[5[c11ac	$2243H)TTTT    ! 	2<=RTW2X2X`..^`&*&:&G&G&O&O&USU##     3338M8WZ[8[8[`d`w8[ ))+++ ( "$9! \$$%Z[[[aN#1D   !G~!G!GHHH
 & */''*2244 $6! _$$  &^dNcNh  &^  &^ko  lA  lK  &^  &^  &^  _  _  _ !-f!#J$-hx.@.@$A$A " "b66&>>[88RVVL=Q=Q8),X)=J-/N'/
'< S S#&776??f#<#<$)E(+(?(?#( !S<?GGIr<R<RN5$9* * * * +-\*:* * *J "E&&~zBBBB  f f fLL!I>[deeeeeeeef
 *Q..&$*???''1,'' "6688N #q!	 X!2B;;  C"3-C!#t,, F1J1J!G~!G!G#&779b#9#9%h44 	%-5->C	NN%;C)Kh &vv.N.N O O O17I#, % % % $%$(	Z     q $D*?FFE(" E E#2 E6:6ID6PSa6a 3 36D 3	E E E E E E E E E E E E E E E $+41A4#H#HT\.px$/O/Obp+ %T8T::Yg>OPX>Y>YN"&"D"D%? #E # #
 #Q&&##\'O*s   L%h// *- *-S((** ///CGGFOOv4M4M"$K* 8"<=P"Q"Q" 8'..w777+ A#**+?@@@" [ 'Ir : :%eS11 [16&++kBZBZ1ZGI. 44S'BBB '))KK,,,"g--KK000/666
 3355 F<<WEEE ##G,,,,  49r+ f$4v$=@\$\#c#c#e#e 
   `)1>N O OPS__ $ F"29QQ
 )$*? @ @ F FHC ''
S(8#((**EEEE ' < "o%)%B       66|DDL  CCLQQL # : :bffY//55 :$&yM$7$7$9$9ByM" + +ff\**  ' 'B!"d++ 
b0@0@'+z"Z.2M'N'NH"B "
 5"$Z.5 +TZ$,.2." ." ."5 5 " "BB  )   :U ":{ ; ": 2 263 ? ?; ;BzN;777
 NN2&&&&#*<   *,777 DD|DDDDDK:<HHM  $? -r$/rrrrZ^Zmrrrsss  X  Xs<GXGX  X  Xfs  X  X  X  BM  X  X  X  X  Y  Y  Ypp]a]gJn#dj///mnppqqqq }]%E%G%GHH}]%E%G%GHH) 	- **d+>+>T+>+>+>????3355 -$:Z:Z:\:\ - $*=1^1^1^#_#_L'45H5Ht5H5H5HWcnrn|'}'}'}$$**,,, # P  Mdj  M  McRZmm  M  My}  zD  gKfijnjtfufufu  JK  M  M  N  N  NbH4`HRL4H4HZ`bbcccNmNNNNOOO!Y[[NK/K).&'($',&+0*&+%).(+0(+0(,1)#O/4,/4,"MHJ++ =F**'        +D*C*E*E*6?Q;N;N!U-@-@-Q-Q!U !U !U & !LL#'? V V	 V V V&* )    !--.@Y.@.@AAA#::<< )./78 4=B : ( 11(<PQQQ%H9 %H %H %H
 -5-;-2*.)2$ $  '   $   c88:::!%!7!7!E!EJ0 B5jAAA}(999%)%8%8%:%:%K%KJej%K%k%k
RRRRRR$-$5'+'<"%)]%8b"&*%)]%)]%)]+9*-l*;*;'*4:+;'<'<0=/:'+      %    ''=>> U44Z4TTT7 7 7 7 7 7 &*N t%95AA 3). 66t}23399;;FFWW 7t}23399;;FF|TT 7 */!7799 3 766666%gdHd&C&CTJJ 3-2N% L#'#I#I&} $J $ $ $(#?#?
#K#K#'9;;#?L ( 0(--b111+/(- 3..r222? l%j%jWc%j%j%j%jkkk+ XJR%]WXw%F%F%FX]
  'W
  'W  'Wovw  BI  pJ  pJ  ^U]e]k]k  PU  'W  'W  X  X  X (-$$&M}(999 $ 3 3 5 5$66x@@ -Y'/37 0 - 4 45G H H H H
 698UW9X9X9^\^5_5_5e5e5g5g5m5m5o5o 2#59P#P#P7>xRV7W7W$4KUVfhlKmKm )e(8(<(<Y(G(G(GFV .eS1A-B-B-B-dOa-d-d-d %5
 %,O(h(:<L(,(@(@(B(B%& %& %&
 8<$4$1$8$89tL^9t9tbr9t9t$u$u$u$u 18-QU0V0VINXYbdgNhNh9p9J9J9Jnp$6'9 %Y(.-V,/0B,C,C)* )* )* )* 8?xSW7X7X;B8Macg;h;h(8(.-_,8:J,3Hgt,L,L,`,`,`QUQ^,`,`)* )* )* <@(8(5(<(<=W(X(X(X*>>>"1133"44X>> h/3,'/ - 4 45G H H H H - 4 45f g g g*<<<#2244#55h?? h/3,'/ - 4 45G H H H H - 4 45f g g g#2244#55h?? 	R/3,'/ - 4 45G H H H H%,Xy%A%A R - 4 45Z [ [ [ [!)!1!9 - 4 45O P P P P - 4 45P Q Q Q' D!+ 4,112OPPP/3,1 7 222666 $q(
  /#d6J2K2KKK --.lmmm6688 %*+K3409>6$ %.	(1# >'(B(B >x~ >(+HN(;(;I&x~zBB hx~G^ h080G0K0KO]f0g0g% >'(I*F*F >8K[ >(+H,<(=(=I )I55(5wxY`GaGa5fnft5,EX^,E,EM )I55(5)v)vd8nnFZFZF\F\)v)v)vJ#3 i '.g[e.g.g h h h ,0(# )'(B(B )x~ )(/(M(MI(0ZPT5U5U0,4N,>,>v,F,F	(4!)7:9~~$4$4(1:'> !) !) !)$(D!)
 ,s22,p\h,p,p,p,pMM-44,bl,b,b,b,bMM-44,VMM-;;,nFV,n,nZf,n,n,n,nMM-;;,`M],`,`,`MM-9,lDT,l,lXd,l,l,l,lMM)B..,jl,j,j,j,jMM)B..,nl,n,n,n,nMM,P\,P,P,P,PM  &X  &X_j  &X  &Xmx  &X  &X  ~B  ~G  ~G  HU  ~V  ~V  &X  &X  `d  e  e  e%Z%Z=%Z%Zbfggg151J1J91U1U.%k%kSi%k%kswxxx%S%SM%S%S[_```&+55 --  /A[  /A  /A  /A  B  B  B#::<< )./78 4=B : ( --.{+.{.{.{|||#MT_*o*oYd*o*o*oppp 11(<PQQQ,4-2-;)m{)m)m^k)m)m*.$ $  %5[S\a$b$b$b	%n%n%n%n%n[h%n%n%nvz{{{  )\{  )\  )\U`  )\  )\eieneno|e}e}  )\  )\  MZ  )\  )\  ]  ]  ] %)IKK)$;	12."ikkI55#8 
" $-t-t-t  }A  !B  !B  !B $ 5 5h@T U U U $ 4 4 6 6 6 7T]j  7T  7T  wB  7T  7T  EP  7T  7T  7T081?1637(" (" !" !JsOOO 3a725;q@@ $ 4 4%Qk %Q %QK %Q %Q'*9ty{{+B'C'C%Q %Q %Q!" !" !"# #ikkI55* ! }(999!(8T!B!B-4X?SUY-Z-Z*,0)%&8$?? \0B0F0Fx0P0P--078JHVZ0[0[-!\116GKj6j6j,4MM,2MM*>>>#2244(,(>(>x?S(T(T*<<<!%!4!4!6!6*0*C*CH*M*M(7(E!%!4!4!6!6)/)B)B8)L)L(6(D,:)??)-$  	5
 !LL#'? u u u&* )    -5M$00  &K  &K  &K  SW  X  X  X &*
+/+>+>+@+@(=,@@@,<,O,O (D<T -P - -MM -=,O,OPX,Y,YM%2
Q[)eY)M)M)Maeak0vWZW[5\5\0]0]0]qv- +/* ry [ . "0 0+ + !65  / "0t!;!wDDgDghvDwDw@w !:#1T#9 , / !9 +
 !LL#'? !E !E !E&* )   !^ . !889JKKK 11(<PQQQ2C,4-;-2+/)7$ $   =,jjj0:-0<EZ< 71 < 7.2.K.KL]_l.m.m ( < < <#4#< ![$=ARAZ$Z$=#:Q#>#>$(LL+/? )L )L,C)L )L )L%& %& %&
 17-i4& 4&L %-OOL$A$A$A=ED$:$($:$:8$D$D$DGK$D$)373K3KLe3f3f3l3l3n3n 0 $ < <=N O O O $ 5 5h@T U U U6F6N$081?16/3-h(" (" !"  =,jjj0:-0<AV<#>#B#B$?1$D$?$(LL+/?(y(y(y.2 %1 %& %& %& %- $'+  %_  %_  %_*. !- !" !" !" !% < <=N O O O $ 5 5h@T U U U6:081?16/3-\(" (" !" x==1,, LLDO)p)p)pqqq373Z3Z[c3d3d0 889JKKK 11(<PQQQ 37,@-;-2+/)X$ $  !LLDO)j)j)jrvLwww 11(<PQQQ26,4-;-2*.)^$ $  x11 qhn q*9$N%)]%)]+ + +
 )8(E,;,I)'6'C-:1B,8& &

 /DDZPPP
 #4#:<MuUU W"&"9"HC&t'>@\^cdd B 3DJs S S S $ 0 0DO  2A  2Aad  2A  2A  2Atxt~  2A  2A  !B  !B  !BFKD3CQVD3N22mC2266:KK6611\A11..!3..11_5QQ1122o6SS2266/:[[6677?;]]77559YY55 &(
*< d d *d?3T  *d  *dWd  *d  *dhkl{  mN  iN  O\  i\  *d  *d  *d  *dJf 2DJ@ZQZ)+<l(*	   ': J+%)]%)]$+D)R$@$@' ' ' '1= ;;u[E[?\?\\;;3>3E03>3E0  + % %% $ 0 D D$(O1@1M2A2O6E6W7F7Y5D5U'2'='I 8=[=S7T7T7TOS0;0B0;0B59]59]'2'9Z'G'G 2I1HMQ*.*34! !E !" !" !" !"$ $- % % % $%  / x#M  +wOA\  +w  +w  +wmw  yL  nM  +w  +w  +w  Yc  dr  Ys  +w  +w  +w  x  x  x "1!B"1"D!+O!<" g t AG!v'<'<QRG LL#'? !M !M#) @!M !M.4 @!M !M$+ L!M !M8? L!M !M !M   ',O }..!SSSSSS113333( ! ! ! D!(()Pn)P)P)PQQQ'   ' 0(--b111+/(- 3..r222"&)++">KLLDO!U!U!U]aLbbb))(4HIII"&K%w[f%w%w%w%wNE  A A A' 0(--.LMMM+/(- 3..r222 ")-?@@ W%WTSqstEuEuxyEyEy#&y>>#7#7#9#9*3x*?*V7hCV (83 O )X 5 Mo:M , -J(,S,S)%lD99 9<\JJ 948 1%j$77 9=jII 948 1%gd4F&M&MtTT 9<T=RSS 948 1 - %0C % ==B==0 	" $'+  %A  %A  %A*. !- !" !" !" !"
 !%'+  %I  %I  %I*. !- !" !" !" %HHHH* f%8<D5 3Ox2X2X/),== K <\ J J J
  **d;; J =j I I I16.)'$8JD*Q*QSWXX i5QRVRg5h5h 2/4,)'$*F*FMM Y3LTZ3X3X 005-)*>DD =4DEY4Z4Z 1#48L#L#L;L$8ARD$>8<$5)'$8QSW*X*XZ]^^ =7GHd7e7e 4#74;W#W#WCWD$@8<$516. $.gd<Ld.S.SUY#Z#Z!* 3 7 78I J J J%) -
  **:DAA e5RSc5d5d 2 5:1'.tY'E'E'KH' &-=h-G-G
#-#9#93=DL'1'$@PRV2W2WY]'^'^ %TIS(;I(F (/tXt'D'D'PU\]a]hjsUtUt'P>H(;<@$9$(LL+/? )[ )[ )[ /3	 %1 %& %& %& !==B== 3 !9#5!9#3!9 $5!9 $6	!9
 $9 +  . 	" $'+  %]  %]  %]*. !- !" !" !" !"
 !%'+  %M  %M  %M*. !- !" !" !" %HHHH"))]D"I"IK$($C$CI$N$NM #*$0Dd"K"KKQ\hw{4DfMMMbhH!3!!(z2!>!>!D"%dGR88>B&3'/:F%MS%6%6%6A" " "J LLn")/1G",j.H";Z=W	   <@;];]$/(7*4*;&3	 <^ < <8' + !  #)^-KKK < L 8<4CCLQQ  LL#'? !: !: !:&* )   
 %HHHH"KK!Z   #)^-]]] M-AAA 4 B = B 9=5&t-FNN %;?D8% $ 6 < < > > > >#, % % % $% ::<<< LL#'? !e !e !e&* )   
 %HHHH )::: M^;;'3.. : / 6:2EEDEQQ % LLDO)u)u)uvvv$HHHH);;; MV33'3.. 9 / 591DD4DPP %!T_"r"r"rsss$HHHH TSSSSS&wyy%'
!$+Ivt$D$D$lPY[egkHlHlE$0-0ZZ-=
( ! ! ! D!aaabbb% Q!T_"O"O:"O"OPPP  F  F  F  G  G  GEEEFFFhhhiiissstttffVZfffggg}}}~~~22'3.. < / 8<4GGII % LLDO)~)~)~$HHHH)==='3..#D*>?? / > / :>6KKKKKKIIKK %!T_"y"y"yzzz$HHHH"5FUoVYFZFZ&u&B&B`u___```OO+OOPPPTW  ^\_`c\d\dgi\i\iPP3ss8PPPPrv  sB  p^  p^  p^  _  _  _EEEFFFSSSSSS&wyy  F  FTX  F  F  F  G  G  G  F  FVZ  F  F  F  G  G  G|||}}}vvvwwwqqqrrruuuvvv #)^-NNN < O 8<4"* B BB)"d33 B "':D A A A# I I I"& %   
  A OS]]  
 !1$K#'9;;#?L((S{SS[SSS   "&i!9J #I 4 4 6 6I%)%>%>y%I%INNNU##"0022&   !(j) D DI#D*i@@E$T7I>>FCN'V'?'?'?'?'?TV$LLDO  "S  "SVa  "S  "Sdo  "S  "St~  "S  AQ  "S  "S  [_L  `  `  `LLDO!c!cy!c!c[a!c!ckoLpppLLDO!N!Nu!N!NVZL[[[LLDO!T!TN!T!T\`Laaa" k{S'8'8$+Ivt$D$D	@I(SItt(<(<t( k LLDO)])]m)])]eiLjjjLLDO  "[  "[  "[  "[  "[gjkwgxgx  "[  "[  CP  "[  "[  "[  "[  \  \  \ //11.);;#  N  NTZ  N  N  N"& %     1  LL#'?  !J  !J  !J&* )    !LL#'?  !N  !N  !N&* )    #  A  Ax~  A  A"& %    0 
%x%x%x  AE  F  F  F--h8LMMM,,... /X\f  /X  /Xjn  kD  kD  EH  IR  ES  ES  kT  kT  /X  /X  /X(0)7).+/          #*)]D"I"IK "(N,LLL'-%)%<
",";"\11&33&*j/;)-(/i(D(D)- 4     'z3DEE N=A
 :
 IN
 E LL#'? !K !K#* J!K !K3? J!K !K !K '+	 )    -1,/3KKK+.x==L=A=S=S (..;(9 >T > >:H&: 480"8}}|;;w?U?U $ 1 1%F,$Z %F %F,3$E%F %F %F!" !" !" !%
1CG @ % '1&7&1&.< 'O ' )4+?#dFZB[B[+[+[ ,M 1, ,(  0 ) --.ijjj#::*BS:TT )./78 4=B : (. ((% MV33&-1JJJ 3 K 490!        )0	:t(L(LI $-!7	9d C C C26 & 8R7Q(1151G8 8 84  8 " 6 6,52?!" !" !" !" !"
 !(%=!" !" !"  ) ! ! ! D!3 % +6K$HHHH #)^-MM ) , .,1,/2JJJ LLDO  *\  *\[s  *\  *\  *\  dhL  i  i  i LLDO  *P  *P  *P  X\L  ]  ]  ]#MT_**[s***  A  A  A 11(<PQQQ,4-2-; *Fay  *F  *F  *F+/*.9=$ $       ))  +b  nB  +b  +b  E]  +b  +b  +b  c  c  c'*8}}9=9O9O$nM$5 :P : :6"6 04,x==<77 --.|L.|.|WZ[cWdWd.|.|.|}}} JqMMM?C<!EEEE LLDO)o)o)ow{L||| LLDO  *P  *P  *P  X\L  ]  ]  ]#MT_*m*m*mnnn 11(<PQQQ,4-2-;)d+/*.9=$ $       #)^-LL , / U%)%<
","; )QQZ([([(4 (+1mb.@'A'AH@HD= LL#'? !q !q<D I!q !q5B p!q !qdk p!q !q !q '+	 )    1A5036NNN $  .D  .D_w  .D  .D  .D  LP  !Q  !Q  !Q $  .T  .T  .T  \`  !a  !a  !a '  /H  /Hc{  /H  /H  /H  !I  !I  !I $ 5 5h@T U U U08161? .Hc{  .H  .H  .H/3.2=A(" (" !" !" !" !" !" !" @D<!EEEE (Fi'P'P+24R+H+H+NB*U*U*W*W'.tZ'D'D'J&R&RSV&W&W&]&]&_&_+/HH *55 7     , 0 N , 4N @I M 4
 ( CL7,B,B&2G LLDO  *C  *C^e  *C  *C  *Cv}  *C  *C  *C  *C  KOL  P  P  P  P8 	C&-G LL#'? !a !a=D `!a !a !a&* )     ':'&B&BG" Xw'8'8&33&*j/6)-(/i(D(D)- 4     'z3DEE 	"=A
 : IM$0$L\W5LI" I"
 E !LLDO  *S  *Spw  *S  *S  *S  AH  *S  *S  *S  *S  [_L  `  `  `  ` LLDO  *K  *K  *K  SWL  X  X  X,1,/2JJJ LLDO))[s)))  HLL  M  M  M LLDO  *P  *P  *P  X\L  ]  ]  ]#MT_  +D  +D_w  +D  +D  +D  E  E  E 11(<PQQQ,4-2-; *D_w  *D  *D  *D+/*.9=$ $       ))  +j  +j  +j  +j  uI  +j  +j  Ld  +j  +j  +j  k  k  k'*8}}9=9O9O$nM$5 :P : :6"6 04,x==<7777wQXGXGX"8}}|;; $ 1 1  3A  3A  3A[^_g[h[h  3A  3A  3A  !B  !B  !B JqMMM?C<!EEEE !LLDO)u)u)u  ~BL  C  C  C LLDO  *  *  *  GKL  L  L  L#MT_  +J  +JWd  +J  +J  +J  +J  K  K  K 11(<PQQQ,4-2-;)x])x)x)x)x+/*.9=$ $      , #9z9.EFF D *%(:D<P'Q! ! D !+9cl C CC . 2  * 44 
$.$> >
 * 1 . 9 . 6 . 9 . ? . @ . @ . A: !'6 65 $" ' 8 ))*sk*s*s*sttt6688 %*+K3409>6$HHHH%1 88 *3OW` 9    ))F[ F F#88CCF F   %w%w^i%w%w%w  @D  E  E  E%g%g9%g%g_e%g%gosttt%R%R5%R%RZ^___%- A1BnF\1\1\(N::{c?Q?Q $  .M  .M  .M  UY  !Z  !Z  !Z $  .A  .A  .A  IM  !N  !N  !N $  .A  .A  .A  IM  !N  !N  !N $-t-t-t  }A  !B  !B  !B  !B $-z-z-z  CG  !H  !H  !H $-n-n-nvz { { { $-u-ulr-u-u-u  ~B  !C  !C  !C#8U_#U#U !R$(LLDO  2E  2E  2E  MQL  %R  %R  %R LLDO)s)s)s{L  A  A  A&a&aV_&a&abbb '#--=53H3HCP\L]L]`bLbLb LL#'? !T !T !T&* )     !11(<PQQQ.2(0)7).&*%(^^          #k11
  : %d>a>a%;K ?b ? ? % :>6*+K$HHHH))*p*p*p*pqqq6688 %*+K3409>6$HHHH)-)B)B9)M)M* r --.r.r.rbp.r.rssss --.pk.p.p`n.p.pqqq%^%^n%^%^fjkkk !(	=$ G GG  # $ $ $ $ = $ $ $ ! ! ( +  LL#'? !? !? !? '+ )    !LL#'? !- !- !- '+ )     n O[.%vs</@/@]BVBV  
 &1 88 *3JR[ 9    --h8LMMM*j;*j*jZh*j*j* +!JO />(0)7).&*%3          $(L& )(/	:t0T0TV_ae(f(f( )W]E-J-J )&3&7&7&F&F&j-J[J[\iJjJjG& )!)36s7||S3I3ILL(1:'> !) !) !)$(D!)0< BRS^knz~BBBI& x))  +H)  +H  +H  +Hdorsds  +H  +H  wB  +H  +H  +H  I  I  I  I))*vY*v*v*vWb*v*vep*v*v*vwwwNNN!##0022!   !%	i 7I-.*)++	114 
 LLDO)p)p)px|L}}} 11(<PQQQ 00222 3Oq|  3O  3O  @K  3O  3O  3O,4-;-2/3$ $       
3 /!3.1C71<< 00!M !M !Mk !M !M#&y49;;'>#?#?!M !M !M  # )++	11[Ae ++l6  $A!/ !#%,,... q 380/  26Jdood$(?!(CD474F4F1
 $G!dddeee%%h0DEEEG!0022
$&!=$888=A=U%&9::Z:8YYGXYY
$.! * 8
 %,8L]LegjAkAk8+3C!#t,, =47GGFB4G4G4t377S\^`KaKa4teieopsetet)11#C.. = "$' @ @D)$44 @ %T 2 2 2 2!+D$!7!7 @DHHV<L<LPV<V<V %TXXfb-A-A B B B B!+D$!7!7 @FdNN %Sf->-> ? ? ?48IIe4D4D)1147HH)1NNNNNN,34E|UY,Z,Z,`^`)&7&?&E2O L* 1#'?#8b!%!4""j!%!%!%'5%1&3&),&7&7'.x$'G'G"FFxPP03O0D0D256K2L2L!    $ !   D %, cT_ c+ c%d%dIZIb%d%deeee  &b  &bIZIbcgdgcgIh  &bsv  xI  xQ  tR  tR  UX  tX  tXjojo  ^`  &b  &b  c  c  c &- !$2M !"3";"A"A"C"CK"$&G[# #egg  
 EP!W!2!24!8!8!;CRC!@!@UWJ! 	!gd4Eq&I&IA&M&M! 77ZPPPP( ! ! ! D!$ !! 778M{\ghlilhl\mostttt( ! ! ! D!
 -->-F-L"MM 771<77LLDO  "B  "B  "B  C  C  C:a??%|%|QUQt%|%|%|}}}  %z%z%z  CG  H  H  H>?;/3/V/VW_/`/`,445FGGG--h8LMMM /3(<)7).'+%V     783=$555-<:W:W22a722"&"?"?@QS`"a"aK*.	0J0J0Pb/W/W/Y/Y*Z*Z'^hitixix  zE  jF  jF  HK  _L  _L  -WDb1Q1Q1W1W1Y1Y,Z,Z,Z  RW)26{G^7_7_2`2`/6:;??K`;a;a6b6b3 ,N0N 7N ;	N 4<#E8B<< U__gimTnTn+x8<<8O+P+P+Ptx(.9oo>U.V.V+ZdemosZtZt3~8<<@U3V3V3Vz~06AooF[6\6\3&x66 X (V 4 4 CX (_ = = MX "*i!8!8!>BKOOT]D^D^Ddbd eX "*k!:!:!@bkooVaFbFbFhfh i	X
 !14G GX !9<W W *  1 N$OOK888 @@MMM599# R LLDO  *Q  *Qmq  nL  *Q  *Q  *Q  R  R  R19...x888 56D2))(4HIII*.$,%3%*#'!c   T#>?? 756D2 %/ d	? ~%|%|M^MiIjIj%|%|%|}}}+ x"3"> x xB#M*v8H*v*vVXVaVklpmplpVq*v*v*vwwww 0: < <;+43HHH'+'='=bk>N'O'OH' < %&|&|Y[YdYi&|&|qy&|&|&| } } }3; 0* * * *3D3O* * *& * #!22a722 %)IIfT5J.K.K$L$L	'9!'<GJ<GXGX[]G]G],ss*;e*C*Cco  &r  &r  &r  &r  SW  Sm  &r  &r  &r  s  s  s5:: LLDO  *G  *G  *G  OSL  T  T  T9:D6 11(<PQQQ26,4-;-2+/)`)`)`$ $  )-(E(EFWYf(g(g 666"3"> 	 	B!{/t7LLL*s2;3C*s*shq*s*s +G$OO(.02+2- -    
 !12D. )+%/: Q Q!{4%dT4L99 %48Jt4D4DBK1$+JtS4I4I+47IIBK1#%;#8D# %4::<< %48BK1$Q Jt,,,,#3 Q Q Q-44bk6FA5OPPPPPPPPQ ) C% &) ) ) ) )&7&B) ) ) & &

 &  LL#'? !^ !^2?!^ !^ !^&* )   
 :;D6 889JKKK 11(<PQQQ26,4-;-2+/)X$ $  22a722/@/C,	9  &D  &Djs  &D  &D  yB  &D  &D  E  E  E599 LLDO)w)wUYUo)w)w)wxxx$ !LLDO)x)x)xyyy9:D6 261N1NO`bo1p1p.$OO,>??? -T,SAR,S,S,SM&7&B # ##%;#3}#D#D*..h.h.h.h=N.h.h.h*h*hC)I# )I )I )I %0K 3pK (,246E/:1" 1" !# !# !# !#
 % 23D. 483P3P)44 4%0 483O3O)44 4%0 %)$A$ABSUb$c$cM $5#<#BL# D(K(KL(Y(Y D8D5 /8 9 9 9 / /+ -0 0 0 0 0&7&B0 0 0 - -) EVA, D1K1K1M1M D7;D44!BBDD D$($<$<\$J$J$P$P$R$RE$ D $-B5-B-B C C C $)L ,&x|T::, %RL,,-@AA,
 !'+ !,&x|T::, %RL,,-@AA, $ 89:6673 5:D1OOM22288GGG 1 !! 66t<<<<( ! ! ! D! ,,->J[]klll9E#'#E,<))-)Q)QRZ)[)[))aH<NaaRZR_aa   !(X(XYYY
 34/ 04D,
 !Z Y<M<X Y Y YI ^$444-44666  #'"9K"599 (3'E (E$DJ,>$( ( ( / 
4K4O4OP\4]4] 
4(()FGGG9=9O9O$n*.*A*T$5 :P : :6"6 04, .6D***8444  &7%>%D"N 05D,  >>~NN e $D*LbQQWUW *  >>?PQQ "0I-)-)A)ABS)T)T)Z)Z)\)\J"KK!> #J  
 !--!4   .8N;?D8! $+41KT#R#R# "6\^c(d(d "0M-"KK(yzzz --.}~~~<@D9HMDE:;D7
 .2-E-Eh-O-O-U-U-W-WN;?D8! +. . .%-bcc]. . . + +
 ,!%$+D2Le$T$T!% =AD9 =AD9HMDE"KK!9   !--!6   *.)F)FGXZg)h)hJ4=Jy1$OOJ777$OO(.%P- -    % +/#$5{DII U&'8:MtTTU&'8:MtTT+ +
 + %t/MPQ/Q/Q ::a?::"KK!@ $ >  
 !--!H$($B!H !H !H   +/*G*G 1<+ +K @DK(;<$OOK8885=D2 228<<<$ ,0+C+C*, ,%''(" , D $ >! C + ( % %DV %\`\wz{\{\{ 771<77"NN!8 $ ;TZ  
 !--!E$($?!E !E !E   % ( )D,@ )"NN!N $ ;TZ $	   !--!D    $::<< )>? ; $ 1 1%9 %9 %9(,%9 %9 %9!" !" !" !'%9$(J!" !" !"
 !)
 -G))-)@)@AR)S)S(,(E(EFWYf(g(g3<i0 666) PSTbPcPcfiPiPitt0Du0L0Lo}-"NN!01B  
 !--!O   
 #NN!7 !% ;TZ $   !-- Q@D@T $I#<#<#H!J   *3 34D/56D2 )::: 1 ;3a77 CC)5.<%- D   8 014/&*&C&CDUWc&d&d 444<<[III %+!U( ( !55519...x888 ./+0 4)B^)S46123/%)%=%=n%M%M%S%S%U%UN $ = =>OQ^ _ _I !'&x|T::' %RL,,-@AA'
 !	 !'&x|T::' %RL,,-@AA' OOI...(W}(W(W(W%? ~(()|n)|)|)|}}} 0 0 0bbbZ]^_Z`Z`bb	,,,,----, , , ,LL+++++, ?Z^___
 !X!2B;;  C"3-C%c400 wwv&00 wwv+55#'',:O:O5( (%-cAghh%7( ( (
 #&l"3 9 9B#%KZD-A-AK8!$x|;;,246tH/S	/S/S+" +"
 !) 8 8 8 "T%81%<<<(VYsPRs^(V(V(V%%cXa%c%cN OO[^$T$TUUUEEEE =<<<<U0SZ  3338M8WZ[8[8[`d`w8[v[ !d111$.!33
 !b. a a4K^ a a a0n 0 0tGZ 0 0 0   ?   0^ 0 0dNa 0 0 0   "88>RRN #$.W>DDW3W	 	h(G(U(UW`aaa 	$$%6777 	h(<=== 6>G"))&1114V##x((  66&>>[00RVVL5I5I0l+D S
47D 9 9 S*.r(,,z2*F*F*J*J6*R*RE 
 

 
 
 
 
 ,:@C'''q	595JQt,11PQ9=9NUd+55TUH 	
 tz>4;N+niO%v	

 V##K#NP-.  -     K	/J////  	E+ 	EEJJJJJJ##!6'5)-h*$T:t<<B      E E E>DDDDDDDDE H%% 	 	Cwwv+--#''+2F2F-!$[!1
n
n
 
 	

 
  1
 u
 ;
 !'$0I5"Q"Q
 TZ
 
 
 D5
 T7
  !?
  !$"A!
"  =#
$ "7!%!? 5")$*ACWYZ"["["`_`"&"A331
 
 
4 -9"&"D"P"P"R"RF; 3355 	6&5F?#',$  	B42 	B*.*AF&' 	 !% !&&**+t/III"d&;;;$(!&'D# 	++"7)# 	, 	
 	
 	
  	+ 	3H 	La 	--&*8nn"7"7 .    
    	BFFFFFFL ?#'j z488>B      	B 	B 	BNN;SAAAAAAAA	B s  )F 
FF98O2 2
O?>O?'0Q 
R"RR%R8 8
S'S""S'&D]6 6
^% ^  ^%&9a   
a-,a-:5b0 0
b=<b=5C-j# #
k-kk&0o
o$#o$"qqq6AA@@AAAAAALA=AN8 N"AN8 N8AOO	AOOAOOA4Bc Q
A'AR2 R1Bc R2
AR?R<Bc R>AR?R?XBc kFBc qAq" q!Bc q"Aq8q5Bc q7Aq8q8EBc wABc x4C>Bc |3A=Bc ~2J9Bc I,B0Bc LABc M;9Bc N6ABc P	A8Bc R=Bc S JBc ]BB_ _Bc _
B_#_ Bc _"B_#_#B1Bc bBb' b&Bc b'
Bb4b1Bc b3Bb4b4Bc cBDe0e2
De0e<FDe+lK"De+w;D	De+|
ADe+}!A"De+B De+ 
B-*De+,B--8De+@+ADe+BADe+C$De+C:>CD9D8De+D9
CEEDe+ECEEDDe+I)A#De+KG2De+S
M	De+`De0`EDe+e,BDe+h(De+h,A-CjjDe+j
Cj'j$De+j&Cj'j'De+j4B0De+m$De0m*B!De+pA>De+rDe0rDDe+v1De0v7De+v?IDe+@De0@CDe+C&B	De+E/De0E5DDe+JJ6De+UDe0U
'De+U74De+V1E$De+\De0\A(De+^ D^%^$De+^%D^;^8De+^:D^;^;D'De+c"De0c(A>De+e+De0hGFL o2B'Dr rFL r
Dr'r$FL r&Dr'r'DFL wDw wFL w
Dw(w%FL w'Dw(w(FL w.$Dx xFL x
Dx xFL xDx x A;FL zA0FL |K+FL G;&FL H"H4FL QBFL S'CFL V>EWWFL WEX#W&2EXXFL XEX#X#BFL Z1A5FL \(CFL _:HFL hEh1 h0FL h1
Eh>h;FL h=Eh>h>BFL kC1FL n:CFL rB!FL t'CFL w6C/FL {'B$FL ~B6FL ADFL EB>FL HDFL LFS2L#FS-L;FMMFS-M.FNM?FS-NFNNE!FS-S-FS2^AF_ _
F`_ F``F`g*(Fh h
Fh hFh h$5Fi i
Fj
i$FjjFj
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
        )ry
  r  )r  )r   r  ry
  rL  s       r   rF  zAIAgent.chatD6  s(     &&w&PP&''r   )=NNNNNNNNr   r  r  NNFFFNr  r   NNNNFNNNNNNNNNNNNNNNNNNNNNNNNNNFFFNNNNNFrP  Fr   r   )r   r   r   r0  r  )r   )NN)NNF)r	  )NNNNN)r'   r(   r)   r*   r6	  r   r   r   setterr  r   r2  r   r   r\  r   r   r   r  r  r   r4  r  r  r  r  r  r  r  BaseExceptionr  r  rO  r  r  r  r  tupler  r   r&  r  r  staticmethodrB  r  rI  rM  rK  r[  r`  ri  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r#  r3  r>  r   r[  r]  r  rh  rq  rs  ru  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r$  r(  r?  rA  rf   rg   re   rO  rR  r   r  rU  r_  rs  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rE   r,  r.  r5  r_  rh  rd  rk  ro  ru  r  r  r~  r  r  r  r(  r0  r4  r6  rP  rc  ri  rq  r  r  r  r  r  r  r  r  r  r  r  r  r  r	  r
	  r	  r	  r*	  r/	  r@	  rB	  rl	  rp   rq	  rs	  r{	  r}	  r	  r	  r	  r	  r	  r	  rx
  r  rF  r   r   r   r  r  i  s!        	O +
 #    X _;c ;d ; ; ; _; %)!% &*'+"' % '+ #'+'+%)!,1(,+/(,+/&*'+%)"&*./3&*$(+/ ,015#'#(#(!!%.2)-$)(* %}J JJ J 	J
 J J s)d"J J 3i$J J J J s)J  9J  J  !J" #J$ "%%J& 'J( )J*  9+J,  9-J. c/J0 1J2 &*3J4 #&5J6 7J8 !)9J: &;J< !)=J> $?J@ %AJB #CJD  EJF  (GJH %-IJJ $KJL "MJN OJP sCx.QJR SJT  S>UJV tCH~.WJX YJZ [J\ ]J^ _J` aJb cJd eJf !gJh !iJj !kJl mJp qJr ,sJt S#XuJx "yJz #&{J| }J J J JX(   ,%7 %7 %7N? ?Xc] ?^b ? ? ? ?Bu
 u
 u
 u
n  $ ,1 * * *D * * * *6T    &
$ 
 
 
 
UC UD U U U U(VS VT V V V V"	EC 	Em 	E 	E 	E 	E 	E
tCH~ 
 
 
 
e e e eN   ", ,c ,T , , , ,) )S )D ) ) ) ) >E > > > >(uUD[7I    0$tCH~:N SX    LD L L L L #'"&"&#Y Y Y 3-Y 3-	Y
 3-Y }Y 
tTz	Y Y Y Yv %S %T % % % \%  #'< < << 3-< 
	< < < \<"%s %t % % % %%c %d % % % %.\3 \3 \ \ \ \| 	Gc 	Gd 	G 	G 	G \	GH H H H H $(	C CC 4.	C
 
C C C C>EeEe Ee tCH~&	Ee
 
Ee Ee Ee EePAx} A A A AFVs Vt V V V VD	L H	 Z4	I p <d<T
< 
c< < < \<B $#	F F:F F 	F
 
F F F FV '++/!%&*J J J smJ $C=	J
 #J smJ 
c3hJ J J J4*T$Z *D * * * *$K Kd K4PT: K K K K+F +Fd4j +FX\]aXb +F +F +F +FZ-4: -$t* - - - ->?# ? ? ? ?0cd4S>6J cX[ chl cquvz{~  AD  |D  wE  rF c c c cJDd38n)= D3 D[_ D D D D %&I %&# %& %& %& \%&N)(3- )HSM ) ) ) )c c    4 >) >S#X > > > \>@C HTRUWZRZ^D\    * &*P P PcNP 	P
 	"P 
$P P P Pd      \@D @D$tCH~*> @D @D @D @DDBV BV BV BV BV BV BVH+ + + +B"# "$ " " " "Hhsm     =
T =
RU =
Z^ =
 =
 =
 =
~(C (D ( ( ( (
# $    && & &C D    ,
d 
 
 
 
$      6
 
d 
d 
 
 
 
/  #/ 	/
 / 
/ / / /b- - - -^6 6 6 6p4S#X+? D    @ ) ) ) ) X)yI yI3 yI# yI yI yI yI~ I I I I \I !y!a!a!abbCd38n)= C$tCQTH~BV C C C \CJ 3c3h 3D 3 3 3 \3j RtCH~&R	d38n	R R R \Rh T d    \< GD GT G G G \G"F3 F3: F F F FP	0 	0 	0 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[4 [C [QU [Z] [ [ [ [z 8 8 8 8 8 \8t3 3  QU    0     "s s     D4 D D D DL 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G3 G3 G4 G G G G{ {D {# {V^ { { { {zLg LgD Lg# Lg Lg Lg Lg\ FJ   d d    : EI ! ! !T !T ! ! ! !F! ! ! ! !F14 1 1 1 1f=3 =4 = = = =2J J J JF 7;26R& R& R& c]R& 	R&
 $N3R&  S#X/R& 
tTz	R& R& R& R&hDT D D D D
   4V"$ V" V" V" V"t3 3 3 3$C D     1c 1c 1 1 1 \1
	>S 	>T 	> 	> 	> 	>LT#s(^ LPT L L L L(7s (7t (7 (7 (7 (7T# $         
t 
 
 
 
 ?CS" S" S"S"3;S" S" S" S"nB1 B1-D B1PT B1 B1 B1 B1LK$ K K K K^ #,) - - - # #N"N47NFIN	N N N Nd # $    \ C E#xPT~BU<V    \(1 13 1SV 1 1 1 1f    (*[S *[ *[ *[ *[ *[ *[X s    $     84 D    @n! n! n! n! n! n!`
$ 
 
 
 
>M M M M M     BD T    8n
d n
t n
 n
 n
 n
`(T (T (T (T (TT DI        D
HSM 
 
 
 
,TD[ , , , ,@ QU    B

t 

 

 

 


D 
 
 
 

 
 
 
 
D/$ D/ D/RV D/ D/ D/ D/L T d    \6  	j j jj 	j
 
j j j \jX2T 2 2 2 2 ^br{  QU j- j- j-$ j- j-WZ j-lo j-  KN j-  Z_ j- j- j- j-X:1F :4 : : : :

<Q 
VY 
 
 
 
  	  
   (40E 4# 4 4 4 4* *t *X[ *mp *y} * * * *.
T 
c 
 
 
 
( KO49S S# Sd SWZ S#+C=SCGS-1S>AS S S Sj ( (S ( (S (s ( ( ( \(0yK yK$ yKcf yKx{ yK  EI yK yK yK yKvXO XO$ XOcf XOx{ XO  EI XO XO XO XOx}t }S }S } } } }D #59.2.2t6 t6t6 t6 #4S>2	t6
 t6 "(+t6 'smt6 
c3ht6 t6 t6 t6lm( (C ((82D (PS ( ( ( ( ( (r   r  r   rR  Fr9  r  r  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(----------------------------------------)webr	  visioncreativer  )researchdevelopmentrT  content_creation
full_stacku   
📌 Basic Toolsets:resolved_toolsr@  r8  u     • r  z - r  z    Tools: u5   
📂 Composite Toolsets (built from other toolsets):includesz    Includes: z    Total tools: r
  u!   
🎭 Scenario-Specific Toolsets:20u3   
📦 Legacy Toolsets (for backward compatibility):rB  u   ✅u   ❌r
  r  r  z    Requirements: rw  u   
🔧 Individual Tools (z available):u     📌 z (from r  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  re  s     r   r   zmain.<locals>.<listcomp>6  s      P P Pq P P Pr   rI  u   🎯 Enabled toolsets: c                 6    g | ]}|                                 S r   r  re  s     r   r   zmain.<locals>.<listcomp>6  s     !R!R!R!''))!R!R!Rr   u   🚫 Disabled toolsets: u   💾 Trajectory saving: ENABLEDu:      - Successful conversations → trajectory_samples.jsonlu7      - Failed conversations → failed_trajectories.jsonl)	r   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: r  u   📞 API Calls: r
  u   💬 Messages: r'  r  u   
🎯 FINAL RESPONSE:z------------------------------r   sample_rJ  )conversationsrE  r  r  r  r  rL  rM  Fr6  )r  rK  u"   
💾 Sample trajectory saved to: u   
⚠️ Failed to save sample: u    
👋 Agent execution completed!)r  model_toolsr  r  r	  r  r  r  r   r;  r   r  r2   r"  r  r  r  r   r  r  r  r   r  rQ  ru  r   r   rU  r   )(r  r  r  r   r  r  r  r  r  r  r  r  r  r  r  r  all_toolsetsbasic_toolsetscomposite_toolsetsscenario_toolsetsr   r  r  r  	tools_strincludes_strlegacy_toolsetsr  	all_toolsr  enabled_toolsets_listdisabled_toolsets_listrj  ru  r  rL  	sample_idsample_filenamer  r.  s(                                           r   mainr  S6  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__r   )rB  )Nr   Nr   rR  NNFFFFr9  )r*   rY  rJ  concurrent.futuresr
  r
  r  rU  r   r   r  r'   rS  r   r
  rX  r  r   rH  r  r   typesr   urllib.requestr   r  typingr   r   r   r   r  r   r	   r
   r   pathlibr   r  r   r   r   __annotations__r   r   r   hermes_cli.env_loaderr,   hermes_cli.timeoutsr-   r.   r  __file__parent_project_env_loaded_env_paths	_env_pathr  r  r1   r2   r3   r4   tools.terminal_toolr5   r6   r7   r8   r  r9   r	  r:   r;   tools.tool_result_storager<   r=   tools.interruptr>   rg  tools.browser_toolr?   r%  r@   rA   rB   agent.retry_utilsrC   agent.error_classifierrD   rE   agent.prompt_builderrF   rG   rH   rI   rJ   rK   rL   rM   r7  rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   agent.context_compressorrX   agent.subdirectory_hintsrY   agent.prompt_cachingrZ   r[   r\   r]   r^   r_   r`   ra   rb   agent.usage_pricingrc   rd   agent.codex_responses_adapterre   rJ  rf   rD  rg   rG  rh   agent.displayri   rj   r
  rk   r!
  rl   rm   r?
  agent.tool_guardrailsrn   ro   rp   rq   rr   agent.trajectoryrs   rt   ru   r  utilsrv   rw   rx   ry   rz   r  r{   r}   r   r   r   r   r   r  r   r   r   r
  Eventr  compileVERBOSEr   r   r   r   r  r   r   r   r  r  r&  r  r2  rA  rd  rk  rn  rs  rq  r  r{  r  r  r  r   r  fireFirer   r   r   <module>r6     sf    *              		8	$	$ 				  				 



 



       ! ! ! ! ! !      , , , , , , , , , , , , 7 7 7 7 7 7 7 7 7 7             , , , , , , %) 8D> ( ( ($    , , , , , , , , 
 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            N M M M M M M M M M            U T T T T T T T ; ; ; ; ; ; . . . . . . h g g g g g g g g g . . . . . . E E E E E E E E                                           7 6 6 6 6 6 < < < < < < > > > > > >                                        D D D D D D D D                                               t s s s s s s s s s s s s s % % % % % %.* .* .* .* .* .* .* .*bXc]    hsm     &; ; ; ;)7 )7 )7 )7 )7 )7 )7 )7\ "	9+..  !y " " "    YCCCDD    +9?,,  #
		 J   !bj!455      )$ ) ) ) )X=C = =PT = = = ="? ?d ?t ? ? ? ? 
-..
s s    C D    BAD AT A A A AH's 's ' ' ' 'T^ ^# ^# ^ ^ ^ ^ ^BA3 A3 A A A A84 8D 8 8 8 8v0T 0d 0 0 0 0
3 4    T  T    #t # # # #2
d 
 
 
 
gK( gK( gK( gK( gK( gK( gK( gK(VW  !#S/ S/S/S/ S/ 	S/
 S/ S/ S/ S/ S/ S/ S/ S/ S/ S/ S/l zKKKDIdOOOOO r   