
    @hSA                    6   S SK Jr  S SKrS SKrS SKrS SKJrJr  S SKJ	r	J
r
JrJr  S SKJr  S SKJrJrJr  S SKJrJrJrJrJr  S SKJr  S S	KJr  S S
KJr  S SKJ r   S SK!J"r"  S SK#J$r$  S SK%J&r&  \	(       a  S SK'J(r(J)r)  \RT                  " \+5      r, " S S5      r-g)    )annotationsN)AsyncIteratorIterator)TYPE_CHECKINGAnyOptionalUnion)UUID)AgentActionAgentFinish	AgentStep)AsyncCallbackManagerAsyncCallbackManagerForChainRunCallbackManagerCallbackManagerForChainRun	Callbacks)dumpd)RunInfo)AddableDict)BaseTool)get_color_mapping)RUN_KEY)asyncio_timeout)AgentExecutorNextStepOutputc                     \ rS rSr% Sr S(SSSSSSS.                 S)S jjjrS\S'   S\S'   S	\S'   S\S
'   S\S'   S\S'   S\S'   S\S'   \S*S j5       r\R                  S+S j5       r\S,S j5       r
\
R                  S-S j5       r
\S.S j5       r\S*S j5       rS/S jrS/S jr      S0S jrS1S jrS2S  jr      S3S! jr      S4S" jrS5S# jrS6S$ jr      S7S% jr      S8S& jrS'rg)9AgentExecutorIterator*   zIterator for AgentExecutor.NF)tagsmetadatarun_namerun_idinclude_run_infoyield_actions	callbacksr   r   Optional[list[str]]r    Optional[dict[str, Any]]r!   Optional[str]r"   Optional[UUID]r#   boolr$   c                   Xl         X l        X0l        X@l        XPl        X`l        Xpl        Xl        Xl        U R                  5         g)a  
Initialize the AgentExecutorIterator with the given AgentExecutor,
inputs, and optional callbacks.

Args:
    agent_executor (AgentExecutor): The AgentExecutor to iterate over.
    inputs (Any): The inputs to the AgentExecutor.
    callbacks (Callbacks, optional): The callbacks to use during iteration.
        Defaults to None.
    tags (Optional[list[str]], optional): The tags to use during iteration.
        Defaults to None.
    metadata (Optional[Dict[str, Any]], optional): The metadata to use
        during iteration. Defaults to None.
    run_name (Optional[str], optional): The name of the run. Defaults to None.
    run_id (Optional[UUID], optional): The ID of the run. Defaults to None.
    include_run_info (bool, optional): Whether to include run info
        in the output. Defaults to False.
    yield_actions (bool, optional): Whether to yield actions as they
        are generated. Defaults to False.
N)
_agent_executorinputsr%   r   r    r!   r"   r#   r$   reset)
selfagent_executorr-   r%   r   r    r!   r"   r#   r$   s
             W/var/www/html/shao/venv/lib/python3.13/site-packages/langchain/agents/agent_iterator.py__init__AgentExecutorIterator.__init__-   s>    B  ."	   0*

    dict[str, str]_inputsc                    U R                   $ )z The inputs to the AgentExecutor.)r6   r/   s    r1   r-   AgentExecutorIterator.inputsb   s     ||r4   c                D    U R                   R                  U5      U l        g N)r0   prep_inputsr6   )r/   r-   s     r1   r-   r9   g   s    **66v>r4   c                    U R                   $ )z"The AgentExecutor to iterate over.)r,   r8   s    r1   r0   $AgentExecutorIterator.agent_executork   s     ###r4   c                2    Xl         U R                  U l        g r;   )r,   r-   )r/   r0   s     r1   r0   r>   p   s    -kkr4   c                l    U R                   R                   Vs0 sH  oR                  U_M     sn$ s  snf )z!A mapping of tool names to tools.)r0   toolsnamer/   tools     r1   name_to_tool_map&AgentExecutorIterator.name_to_tool_mapv   s0     -1,?,?,E,EF,ED		4,EFFFs   1c                ~    [        U R                  R                   Vs/ sH  oR                  PM     snSS/S9$ s  snf )z"A mapping of tool names to colors.greenred)excluded_colors)r   r0   rA   rB   rC   s     r1   color_mapping#AgentExecutorIterator.color_mapping{   s>     !#'#6#6#<#<=#<4YY#<=$e,
 	
=s   :c                    [         R                  S5        / U l        SU l        SU l        [
        R
                  " 5       U l        g)ze
Reset the iterator to its initial state, clearing intermediate steps,
iterations, and time elapsed.
z0(Re)setting AgentExecutorIterator to fresh stater   g        N)loggerdebugintermediate_steps
iterationstime_elapsedtime
start_timer8   s    r1   r.   AgentExecutorIterator.reset   s5    
 	GHAC))+r4   c                    U =R                   S-  sl         [        R                  " 5       U R                  -
  U l        [        R                  SU R                   U R                  5        g)zA
Increment the number of iterations and update the time elapsed.
   z$Agent Iterations: %s (%.2fs elapsed)N)rQ   rS   rT   rR   rN   rO   r8   s    r1   update_iterations'AgentExecutorIterator.update_iterations   sH     	1 IIK$//92OO	
r4   c                    [        U R                  R                  U R                  USS95      nU R                  (       a  [        UR                  S9U[        '   U$ )NT)return_only_outputs)r"   )r   r0   prep_outputsr-   r#   r   r"   r   )r/   outputsrun_managerprepared_outputss       r1   make_final_outputs(AgentExecutorIterator.make_final_outputs   s\     ',,$( - 
   (/{7I7I(JW%r4   c           	   #    #    [         R                  S5        U R                  5         [        R                  " U R
                  U R                  R
                  U R                  R                  U R                  U R                  R                  U R                  U R                  R                  5      nUR                  [        U R                  5      U R                  U R                  U R                  S9n U R                  R                  U R                   U R"                  5      (       Gam  / nU R                  R%                  U R&                  U R(                  U R                  U R*                  U5       H  nUR-                  U5        U R.                  (       d  M'  [1        U[2        5      (       a  [5        U/UR6                  S9v   MU  [1        U[8        5      (       d  Ml  [5        U/UR6                  S9v   M     U R                  R;                  U5      nU R=                  5         U R?                  XR5      nSU;  nU R.                  (       a  U(       a  Uv   U(       a  g U R                  R                  U R                   U R"                  5      (       a  GMm  U RE                  U5      v   g ! [@         a  nURC                  U5        e S nAff = f7f)Nz"Initialising AgentExecutorIteratorrB   actionsmessagesstepsrf   intermediate_step)#rN   rO   r.   r   	configurer%   r0   verboser   r    on_chain_startr   r-   r"   r!   _should_continuerQ   rR   _iter_next_steprE   rK   rP   appendr$   
isinstancer   r   rf   r   _consume_next_steprX   _process_next_step_outputBaseExceptionon_chain_error_stop	r/   callback_managerr^   next_step_seqchunk	next_stepoutputis_finales	            r1   __iter__AgentExecutorIterator.__iter__   s@    9:

*44NN))''II$$MM((
 '55$%%&KKKK	 6 
(	%%66!!  13!00@@))&&KK++E "((/ )))%e[99"-ug"WW'y99"-UGenn"UU" !//BB=Q	&&(77	O.f< ))X LI %%66!! T jj%%  	&&q)	sE   C3K6B"J- AJ- !A?J-  K!5J- K-
K7K		KKc           	    P  #    [         R                  S5        U R                  5         [        R                  " U R
                  U R                  R
                  U R                  R                  U R                  U R                  R                  U R                  U R                  R                  5      nUR                  [        U R                  5      U R                  U R                  U R                  S9I Sh  vN n [        U R                  R                   5       ISh  vN   U R                  R#                  U R$                  U R&                  5      (       a  / nU R                  R)                  U R*                  U R,                  U R                  U R.                  U5        Sh  vN nUR1                  U5        U R2                  (       d  M-  [5        U[6        5      (       a  [9        U/UR:                  S97v   M\  [5        U[<        5      (       d  Ms  [9        U/UR:                  S97v   M  SSS5      ISh  vN   U RI                  U5      I Sh  vN 7v   g GNf GN@ N
 U R                  R?                  U5      nU RA                  5         U RC                  UU5      I Sh  vN  nSU;  nU R2                  (       a  U(       a  U7v   U(       a   SSS5      ISh  vN    gU R                  R#                  U R$                  U R&                  5      (       a  GM  N N! , ISh  vN  (       d  f       N= f! [D        [F        RD                  4 a     U RI                  U5      I Sh  vN  7v    g[J         a   nURM                  U5      I Sh  vN    e SnAff = f GNN7f)z
N.B. __aiter__ must be a normal method, so need to initialize async run manager
on first __anext__ call where we can await it
z*Initialising AgentExecutorIterator (async)rc   Nrd   rg   ri   )'rN   rO   r.   r   rj   r%   r0   rk   r   r    rl   r   r-   r"   r!   r   max_execution_timerm   rQ   rR   _aiter_next_steprE   rK   rP   ro   r$   rp   r   r   rf   r   rq   rX   _aprocess_next_step_outputTimeoutErrorasyncio_astoprs   rt   rv   s	            r1   	__aiter__AgentExecutorIterator.__aiter__   s    
 	AB

/99NN))''II$$MM((
 -;;$%%&KKKK	 < 
 
5	&t':':'M'MNN))::OO%%  57M'+':':'K'K--**//#( "e &,,U3  ---)%==&1-2G-2^^'" !" ",E9!=!=&1+0'-2^^'" !"3 ONn KK,,,}
 O" (. !% 3 3 F F} UI**,#'#B#B!#$  F  3&@H  --$] ONN))::OO%%  ONNN^ g223 	K0000 	,,Q///	
 -s  C6N&8I9N&>#L> !I!"L> %A?L$$I&(I$)I&,!L$AL$L$1L> <L"=L> N&N#	N&!L> $I&&AL$'J*(/L$L> "K%#L> (N&)5L$"L> $L;*L-+L;7L> :N&;L> >.N ,M/-N 5N&7	N  NNNN  N&c                   [         R                  S5        [        U[        5      (       a$  [         R                  S5        U R	                  XS9$ U R
                  R                  U5        [         R                  S5        [        U5      S:X  a2  US   nU R                  R                  U5      nUb  U R	                  XBS9$ [        US9$ )zR
Process the output of the next step,
handling AgentFinish and tool return cases.
z$Processing output of Agent loop stepzBHit AgentFinish: _return -> on_chain_end -> run final output logicr^   +Updated intermediate_steps with step outputrW   r   ri   )rN   rO   rp   r   _returnrP   extendlenr0   _get_tool_returnr   r/   next_step_outputr^   next_step_actiontool_returns        r1   rr   /AgentExecutorIterator._process_next_step_output=  s     	;<&44LLT << 0<JJ&&'78BC  A%/2-->>?OPK&||K|II-=>>r4   c                  #    [         R                  S5        [        U[        5      (       a,  [         R                  S5        U R	                  XS9I Sh  vN $ U R
                  R                  U5        [         R                  S5        [        U5      S:X  a:  US   nU R                  R                  U5      nUb  U R	                  XBS9I Sh  vN $ [        US9$  N N7f)	zX
Process the output of the next async step,
handling AgentFinish and tool return cases.
z*Processing output of async Agent loop stepzCHit AgentFinish: _areturn -> on_chain_end -> run final output logicr   Nr   rW   r   r   )rN   rO   rp   r   _areturnrP   r   r   r0   r   r   r   s        r1   r   0AgentExecutorIterator._aprocess_next_step_outputY  s      	AB&44LLU '7QQQ&&'78BC  A%/2-->>?OPK&!]];]PPP-=>> R Qs%   ACCA8CCCCc                    [         R                  S5        U R                  R                  R                  " U R                  R
                  U R                  40 U R                  D6nU R                  X!S9$ )zR
Stop the iterator and raise a StopIteration exception with the stopped response.
;Stopping agent prematurely due to triggering stop conditionr   )	rN   warningr0   _action_agentreturn_stopped_responseearly_stopping_methodrP   r-   r   r/   r^   r{   s      r1   ru   AgentExecutorIterator._stopu  sf     	TU$$22JJ55##
 kk

 ||F|<<r4   c                  #    [         R                  S5        U R                  R                  R                  " U R                  R
                  U R                  40 U R                  D6nU R                  X!S9I Sh  vN $  N7f)z]
Stop the async iterator and raise a StopAsyncIteration exception with
the stopped response.
r   r   N)	rN   r   r0   r   r   r   rP   r-   r   r   s      r1   r   AgentExecutorIterator._astop  so     
 	TU$$22JJ55##
 kk

 ]]6]CCCCs   A8B:A?;Bc                    U R                   R                  UU R                  US9nUR                  US'   UR	                  U5        U R                  X25      $ )z*
Return the final output of the iterator.
r   rf   )r0   r   rP   rf   on_chain_endr`   r/   r{   r^   returned_outputs       r1   r   AgentExecutorIterator._return  s]     --55### 6 

 '-oo
#  1&&DDr4   c                   #    U R                   R                  UU R                  US9I Sh  vN nUR                  US'   UR	                  U5      I Sh  vN   U R                  X25      $  N= N7f)z0
Return the final output of the async iterator.
r   Nrf   )r0   r   rP   rf   r   r`   r   s       r1   r   AgentExecutorIterator._areturn  su      !% 3 3 < <### != !
 

 '-oo
#&&777&&DD
 	8s!   )A-A)'A-A+A-+A-)r,   r6   r%   r#   r-   rP   rQ   r    r"   r!   rT   r   rR   r$   r;   )r0   r   r-   r   r%   r   r   r&   r    r'   r!   r(   r"   r)   r#   r*   r$   r*   )returnr5   )r-   r   r   None)r   r   )r0   r   r   r   )r   zdict[str, BaseTool])r   r   )r]   zdict[str, Any]r^   zBUnion[CallbackManagerForChainRun, AsyncCallbackManagerForChainRun]r   r   )r/   r   r   zIterator[AddableDict])r   zAsyncIterator[AddableDict])r   1Union[AgentFinish, list[tuple[AgentAction, str]]]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   )__name__
__module____qualname____firstlineno____doc__r2   __annotations__propertyr-   setterr0   rE   rK   r.   rX   r`   r~   r   rr   r   ru   r   r   r   __static_attributes__ r4   r1   r   r   *   s   %  $	* %)-1"&!%!&#*%* * 	* "* +*  * * * *X 
&&  ]]? ? $ $ " "
 G G 
 

&

   X  
	 &=&~N-`?K? 0? 
	?8?K? 5? 
	?8=DEE 0E 
	E"EE 5E 
	Er4   r   ).
__future__r   r   loggingrS   collections.abcr   r   typingr   r   r   r	   uuidr
   langchain_core.agentsr   r   r   langchain_core.callbacksr   r   r   r   r   langchain_core.load.dumpr   langchain_core.outputsr   langchain_core.runnables.utilsr   langchain_core.toolsr   langchain_core.utils.inputr   langchain.schemar   langchain.utilities.asyncior   langchain.agents.agentr   r   	getLoggerr   rN   r   r   r4   r1   <module>r      sr    "    3   
  + * 6 ) 8 $ 7D			8	$EE EEr4   