
    3fi*B                    $   d dl mZ d dlZd dlZd dlZd dlmZmZ d dlm	Z	m
Z
 d dlmZ d dlmZmZmZ d dlmZmZmZmZmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlm Z  d dl!m"Z" d dl#m$Z$ e	rd dl%m&Z&m'Z'  ejP                  e)      Z* G d d      Z+y)    )annotationsN)AsyncIteratorIterator)TYPE_CHECKINGAny)UUID)AgentActionAgentFinish	AgentStep)AsyncCallbackManagerAsyncCallbackManagerForChainRunCallbackManagerCallbackManagerForChainRun	Callbacks)dumpd)RunInfo)AddableDict)BaseTool)get_color_mapping)RUN_KEY)asyncio_timeout)AgentExecutorNextStepOutputc                     e Zd ZU dZ	 d&ddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d'dZded<   ded<   ded<   d
ed	<   ded<   ded<   ded<   ded<   ed(d       Zej                  d)d       Zed*d       Z	e	j                  d+d       Z	ed,d       Z
ed(d       Zd-dZd-dZ	 	 	 	 	 	 d.dZd/dZd0dZ	 	 	 	 	 	 d1d Z	 	 	 	 	 	 d2d!Zd3d"Zd4d#Z	 	 	 	 	 	 d5d$Z	 	 	 	 	 	 d6d%Zy)7AgentExecutorIteratorzIterator for AgentExecutor.NF)tagsmetadatarun_namerun_idinclude_run_infoyield_actions	callbacksr   r   list[str] | Noner   dict[str, Any] | Noner   
str | Noner   UUID | Noner    boolr!   c                   || _         || _        || _        || _        || _        || _        || _        || _        |	| _        | j                          y)a  Initialize the `AgentExecutorIterator`.

        Initialize the `AgentExecutorIterator` with the given `AgentExecutor`,
        inputs, and optional callbacks.

        Args:
            agent_executor: The `AgentExecutor` to iterate over.
            inputs: The inputs to the `AgentExecutor`.
            callbacks: The callbacks to use during iteration.
            tags: The tags to use during iteration.
            metadata: The metadata to use during iteration.
            run_name: The name of the run.
            run_id: The ID of the run.
            include_run_info: Whether to include run info in the output.
            yield_actions: Whether to yield actions as they are generated.
        N)
_agent_executorinputsr"   r   r   r   r   r    r!   reset)
selfagent_executorr*   r"   r   r   r   r   r    r!   s
             e/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_classic/agents/agent_iterator.py__init__zAgentExecutorIterator.__init__+   sO    :  ."	   0*

    dict[str, str]_inputsc                    | j                   S )z"The inputs to the `AgentExecutor`.)r2   r,   s    r.   r*   zAgentExecutorIterator.inputs\   s     ||r0   c                D    | j                   j                  |      | _        y N)r-   prep_inputsr2   )r,   r*   s     r.   r*   zAgentExecutorIterator.inputsa   s    **66v>r0   c                    | j                   S )z$The `AgentExecutor` to iterate over.)r)   r4   s    r.   r-   z$AgentExecutorIterator.agent_executore   s     ###r0   c                4    || _         | j                  | _        y r6   )r)   r*   )r,   r-   s     r.   r-   z$AgentExecutorIterator.agent_executorj   s    -kkr0   c                j    | j                   j                  D ci c]  }|j                  | c}S c c}w )z!A mapping of tool names to tools.)r-   toolsnamer,   tools     r.   name_to_tool_mapz&AgentExecutorIterator.name_to_tool_mapp   s,     -1,?,?,E,EFD		4FFFs   0c                    t        | j                  j                  D cg c]  }|j                   c}ddg      S c c}w )z"A mapping of tool names to colors.greenred)excluded_colors)r   r-   r;   r<   r=   s     r.   color_mappingz#AgentExecutorIterator.color_mappingu   s:     !#'#6#6#<#<=4TYY=$e,
 	
=s   <c                    t         j                  d       g | _        d| _        d| _        t        j
                         | _        y)zReset the iterator to its initial state.

        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_timer4   s    r.   r+   zAgentExecutorIterator.reset}   s5     	GHAC))+r0   c                    | xj                   dz  c_         t        j                         | j                  z
  | _        t        j                  d| j                   | j                         y)z?Increment the number of iterations and update the time elapsed.   z$Agent Iterations: %s (%.2fs elapsed)N)rI   rK   rL   rJ   rF   rG   r4   s    r.   update_iterationsz'AgentExecutorIterator.update_iterations   sF    1 IIK$//92OO	
r0   c                    t        | j                  j                  | j                  |d            }| j                  rt        |j                        |t        <   |S )zMake final outputs for the iterator.

        Args:
            outputs: The outputs from the agent executor.
            run_manager: The run manager to use for callbacks.
        T)return_only_outputs)r   )r   r-   prep_outputsr*   r    r   r   r   )r,   outputsrun_managerprepared_outputss       r.   make_final_outputsz(AgentExecutorIterator.make_final_outputs   s\     ',,$( - 
   (/{7I7I(JW%r0   c           	   #  X  K   t         j                  d       | j                          t        j                  | j
                  | j                  j
                  | j                  j                  | j                  | j                  j                  | j                  | j                  j                        }|j                  t        | j                        | j                  | j                  | j                        }	 | j                  j                  | j                   | j"                        rJg }| j                  j%                  | j&                  | j(                  | j                  | j*                  |      D ]v  }|j-                  |       | j.                  s!t1        |t2              rt5        |g|j6                         Lt1        |t8              s]t5        |g|j6                         x | j                  j;                  |      }| j=                          | j?                  ||      }d|v}| j.                  r|r| |ry| j                  j                  | j                   | j"                        rJ| jE                  |       y# t@        $ r}|jC                  |        d}~ww xY ww)z1Create an async iterator for the `AgentExecutor`.z"Initialising AgentExecutorIteratorr<   actionsmessagesstepsr[   intermediate_stepN)#rF   rG   r+   r   	configurer"   r-   verboser   r   on_chain_startr   r*   r   r   _should_continuerI   rJ   _iter_next_stepr?   rD   rH   appendr!   
isinstancer	   r   r[   r   _consume_next_steprO   _process_next_step_outputBaseExceptionon_chain_error_stop	r,   callback_managerrT   next_step_seqchunk	next_stepoutputis_finales	            r.   __iter__zAgentExecutorIterator.__iter__   s7    9:

*44NN))''II$$MM((
 '55$%%&KKKK	 6 
(	%%66!! 13!00@@))&&KK++ VE "((/ ))%e[9"-ug"WW'y9"-UGenn"UUV" !//BB=Q	&&(77	;O.f< ))X LI %%66!!T jj%%  	&&q)	sD   C5J*8BJ ;J A1J  J*0J 3J*	J'J""J''J*c           	      K   t         j                  d       | j                          t        j                  | j
                  | j                  j
                  | j                  j                  | j                  | j                  j                  | j                  | j                  j                        }|j                  t        | j                        | j                  | j                  | j                         d{   }	 t        | j                  j                         4 d{    | j                  j#                  | j$                  | j&                        rg }| j                  j)                  | j*                  | j,                  | j                  | j.                  |      2 3 d{   }|j1                  |       | j2                  s't5        |t6              rt9        |g|j:                         St5        |t<              sdt9        |g|j:                         ddd      d{    | jI                  |       d{    y7 T7 .7 6 | j                  j?                  |      }| jA                          | jC                  ||       d{  7  }d|v}| j2                  r|r| |r	 ddd      d{  7   y| j                  j#                  | j$                  | j&                        r7 # 1 d{  7  sw Y   xY w# tD        tF        jD                  f$ r  | jI                  |       d{  7   Y ytJ        $ r }|jM                  |       d{  7    d}~ww xY w7 1w)zCreate an async iterator for the `AgentExecutor`.

        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)rX   NrY   r\   r^   )'rF   rG   r+   r   r_   r"   r-   r`   r   r   ra   r   r*   r   r   r   max_execution_timerb   rI   rJ   _aiter_next_stepr?   rD   rH   rd   r!   re   r	   r   r[   r   rf   rO   _aprocess_next_step_outputTimeoutErrorasyncio_astoprh   ri   rk   s	            r.   	__aiter__zAgentExecutorIterator.__aiter__   s     	AB

/99NN))''II$$MM((
 -;;$%%&KKKK	 < 
 
5	&t':':'M'MN . .))::OO%% 57M'+':':'K'K--**//#( " "e &,,U3  --)%=&1-2G-2^^'" !" ",E9!=&1+0'-2^^'" !"3. .n KK,,,}
." (. !% 3 3 F F} UI**,#'#B#B!#$  F  3&@H  --$]. . .))::OO%%. . . .^ g223 	K0000 	,,Q///	
 -s
  C8M9:I;M9 #L #I$L 'A:K>!I%I&I)K><K>K>!L ,K<-L 1M9M6	M9L IAK>JK>7L KL M9	0K><L >LLLL -M3 MM3	M9M3M.'M*(M..M33M9c                   t         j                  d       t        |t              r(t         j                  d       | j	                  ||      S | j
                  j                  |       t         j                  d       t        |      dk(  r5|d   }| j                  j                  |      }|| j	                  ||      S t        |      S )zProcess the output of the next step.

        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rT   +Updated intermediate_steps with step outputrN   r   r^   )rF   rG   re   r
   _returnrH   extendlenr-   _get_tool_returnr   r,   next_step_outputrT   next_step_actiontool_returns        r.   rg   z/AgentExecutorIterator._process_next_step_output>  s     	;<&4LLT << 0k<JJ&&'78BC  A%/2-->>?OPK&||K[|II-=>>r0   c                  K   t         j                  d       t        |t              r0t         j                  d       | j	                  ||       d{   S | j
                  j                  |       t         j                  d       t        |      dk(  r=|d   }| j                  j                  |      }|| j	                  ||       d{   S t        |      S 7 7 w)	zProcess the output of the next async step.

        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~   rN   r   r   )rF   rG   re   r
   _areturnrH   r   r   r-   r   r   r   s        r.   rw   z0AgentExecutorIterator._aprocess_next_step_output[  s      	AB&4LLU '7[QQQ&&'78BC  A%/2-->>?OPK&!]];K]PPP-=>> R Qs%   AC#CA:C#C!C#!C#c                    t         j                  d        | j                  j                  j                  | j                  j
                  | j                  fi | j                  }| j                  ||      S )zuStop the iterator.

        Stop the iterator and raise a StopIteration exception with the stopped response.
        ;Stopping agent prematurely due to triggering stop conditionr}   )	rF   warningr-   _action_agentreturn_stopped_responseearly_stopping_methodrH   r*   r   r,   rT   rp   s      r.   rj   zAgentExecutorIterator._stopx  sj    
 	TUJ$$22JJ55##
 kk

 ||F|<<r0   c                  K   t         j                  d        | j                  j                  j                  | j                  j
                  | j                  fi | j                  }| j                  ||       d{   S 7 w)zStop the async iterator.

        Stop the async iterator and raise a StopAsyncIteration exception with
        the stopped response.
        r   r}   N)	rF   r   r-   r   r   r   rH   r*   r   r   s      r.   rz   zAgentExecutorIterator._astop  st      	TUJ$$22JJ55##
 kk

 ]]6{]CCCCs   A<B>B?Bc                    | j                   j                  || j                  |      }|j                  |d<   |j	                  |       | j                  ||      S )z(Return the final output of the iterator.r}   r[   )r-   r   rH   r[   on_chain_endrV   r,   rp   rT   returned_outputs       r.   r   zAgentExecutorIterator._return  s_     --55### 6 

 '-oo
#  1&&DDr0   c                   K   | j                   j                  || j                  |       d{   }|j                  |d<   |j	                  |       d{    | j                  ||      S 7 >7 w)z.Return the final output of the async iterator.r}   Nr[   )r-   r   rH   r[   r   rV   r   s       r.   r   zAgentExecutorIterator._areturn  sy      !% 3 3 < <### != !
 

 '-oo
#&&777&&DD
 	8s!   ,A1A-'A1A/A1/A1r6   )r-   r   r*   r   r"   r   r   r#   r   r$   r   r%   r   r&   r    r'   r!   r'   )returnr1   )r*   r   r   None)r   r   )r-   r   r   r   )r   zdict[str, BaseTool])r   r   )rS   zdict[str, Any]rT   z<CallbackManagerForChainRun | AsyncCallbackManagerForChainRunr   r   )r,   r   r   zIterator[AddableDict])r   zAsyncIterator[AddableDict])r   +AgentFinish | list[tuple[AgentAction, str]]rT   r   r   r   )r   r   rT   r   r   r   )rT   r   r   r   )rT   r   r   r   )rp   r
   rT   r   r   r   )rp   r
   rT   r   r   r   )__name__
__module____qualname____doc__r/   __annotations__propertyr*   setterr-   r?   rD   r+   rO   rV   rs   r{   rg   rw   rj   rz   r   r    r0   r.   r   r   (   s   %  $	& "&*.#"!&#&%& & 	& & (& & & & &P 
##  ]]? ? $ $ " "
 G G 
 
&
   R  
	 2>&@O-b?E? 0? 
	?:?E? 5? 
	?:=DEE 0E 
	EEE 5E 
	Er0   r   ),
__future__r   ry   loggingrK   collections.abcr   r   typingr   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_classic.schemar   #langchain_classic.utilities.asyncior   langchain_classic.agents.agentr   r   	getLoggerr   rF   r   r   r0   r.   <module>r      so    "    3  
  + * 6 ) 8 , ?L			8	$HE HEr0   