
    f3fi              
         d Z ddlmZ ddlZddlmZmZ er6ddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZmZ dd
lmZ ddlmZ ddlmZmZmZ  ej4                  e      Z G d d      Z G d d      Z G d d      Z G d d      Z  G d d      Z! G d d      Z" G d deee ee!e"      Z# G d de#      Z$ G d de!      Z%e&e#   e%z  dz  Z'y)$Base callback handler for LangChain.    )annotationsN)TYPE_CHECKINGAny)Sequence)UUID)RetryCallState)Self)AgentActionAgentFinish)Document)BaseMessage)ChatGenerationChunkGenerationChunk	LLMResultc                  P    e Zd ZdZdd	 	 	 	 	 	 	 	 	 ddZdd	 	 	 	 	 	 	 	 	 ddZy)RetrieverManagerMixinzMixin for Retriever callbacks.Nparent_run_idc                    y)a%  Run when Retriever errors.

        Args:
            error: The error that occurred.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            **kwargs: Additional keyword arguments.
        N selferrorrun_idr   kwargss        [/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_core/callbacks/base.pyon_retriever_errorz(RetrieverManagerMixin.on_retriever_error           c                    y)a/  Run when Retriever ends running.

        Args:
            documents: The documents retrieved.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            **kwargs: Additional keyword arguments.
        Nr   )r   	documentsr   r   r   s        r   on_retriever_endz&RetrieverManagerMixin.on_retriever_end+   r   r    
r   BaseExceptionr   r   r   UUID | Noner   r   returnr   )
r"   Sequence[Document]r   r   r   r&   r   r   r'   r   )__name__
__module____qualname____doc__r   r#   r   r    r   r   r      sy    ( &* 	
 #  
, &*% 	
 #  
r    r   c                  v    e Zd ZdZddd	 	 	 	 	 	 	 	 	 	 	 ddZdd	 	 	 	 	 	 	 	 	 d	dZdd	 	 	 	 	 	 	 	 	 d
dZy)LLMManagerMixinzMixin for LLM callbacks.N)chunkr   c                    y)a  Run on new output token. Only available when streaming is enabled.

        For both chat models and non-chat models (legacy LLMs).

        Args:
            token: The new token.
            chunk: The new generated chunk, containing content and other information.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            **kwargs: Additional keyword arguments.
        Nr   )r   tokenr/   r   r   r   s         r   on_llm_new_tokenz LLMManagerMixin.on_llm_new_token@   r   r    r   c                    y)a1  Run when LLM ends running.

        Args:
            response: The response which was generated.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            **kwargs: Additional keyword arguments.
        Nr   )r   responser   r   r   s        r   
on_llm_endzLLMManagerMixin.on_llm_endU   r   r    c                    y)a  Run when LLM errors.

        Args:
            error: The error that occurred.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            **kwargs: Additional keyword arguments.
        Nr   r   s        r   on_llm_errorzLLMManagerMixin.on_llm_errorf   r   r    )r1   strr/   ,GenerationChunk | ChatGenerationChunk | Noner   r   r   r&   r   r   r'   r   )
r4   r   r   r   r   r&   r   r   r'   r   r$   )r)   r*   r+   r,   r2   r5   r7   r   r    r   r.   r.   =   s    " ?C%) <	
  #  
4 &* 	
 #  
, &* 	
 #  
r    r.   c                      e Zd ZdZdd	 	 	 	 	 	 	 	 	 ddZdd	 	 	 	 	 	 	 	 	 d	dZdd	 	 	 	 	 	 	 	 	 d
dZdd	 	 	 	 	 	 	 	 	 ddZy)ChainManagerMixinzMixin for chain callbacks.Nr   c                    y)a*  Run when chain ends running.

        Args:
            outputs: The outputs of the chain.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            **kwargs: Additional keyword arguments.
        Nr   )r   outputsr   r   r   s        r   on_chain_endzChainManagerMixin.on_chain_end{   r   r    c                    y)a!  Run when chain errors.

        Args:
            error: The error that occurred.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            **kwargs: Additional keyword arguments.
        Nr   r   s        r   on_chain_errorz ChainManagerMixin.on_chain_error   r   r    c                    y)a  Run on agent action.

        Args:
            action: The agent action.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            **kwargs: Additional keyword arguments.
        Nr   )r   actionr   r   r   s        r   on_agent_actionz!ChainManagerMixin.on_agent_action   r   r    c                    y)a  Run on the agent end.

        Args:
            finish: The agent finish.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            **kwargs: Additional keyword arguments.
        Nr   )r   finishr   r   r   s        r   on_agent_finishz!ChainManagerMixin.on_agent_finish   r   r    )
r=   dict[str, Any]r   r   r   r&   r   r   r'   r   r$   )
rB   r   r   r   r   r&   r   r   r'   r   )
rE   r   r   r   r   r&   r   r   r'   r   )r)   r*   r+   r,   r>   r@   rC   rF   r   r    r   r;   r;   x   s    $ &* 	
 #  
, &* 	
 #  
, &* 	
 #  
, &* 	
 #  
r    r;   c                  P    e Zd ZdZdd	 	 	 	 	 	 	 	 	 ddZdd	 	 	 	 	 	 	 	 	 ddZy)ToolManagerMixinzMixin for tool callbacks.Nr   c                    y)a*  Run when the tool ends running.

        Args:
            output: The output of the tool.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            **kwargs: Additional keyword arguments.
        Nr   )r   outputr   r   r   s        r   on_tool_endzToolManagerMixin.on_tool_end   r   r    c                    y)a   Run when tool errors.

        Args:
            error: The error that occurred.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            **kwargs: Additional keyword arguments.
        Nr   r   s        r   on_tool_errorzToolManagerMixin.on_tool_error   r   r    )
rK   r   r   r   r   r&   r   r   r'   r   r$   )r)   r*   r+   r,   rL   rN   r   r    r   rI   rI      sy    # &* 	
 #  
, &* 	
 #  
r    rI   c                     e Zd ZdZdddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d
dZdddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZdddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZdddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd	Zy)CallbackManagerMixinzMixin for callback manager.Nr   tagsmetadatac                    y)a]  Run when LLM starts running.

        !!! warning
            This method is called for non-chat models (regular LLMs). If you're
            implementing a handler for a chat model, you should use
            `on_chat_model_start` instead.

        Args:
            serialized: The serialized LLM.
            prompts: The prompts.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            tags: The tags.
            metadata: The metadata.
            **kwargs: Additional keyword arguments.
        Nr   r   
serializedpromptsr   r   rR   rS   r   s           r   on_llm_startz!CallbackManagerMixin.on_llm_start   r   r    c               J    | j                   j                   d}t        |      aM  Run when a chat model starts running.

        !!! warning
            This method is called for chat models. If you're implementing a handler for
            a non-chat model, you should use `on_llm_start` instead.

        Args:
            serialized: The serialized chat model.
            messages: The messages.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            tags: The tags.
            metadata: The metadata.
            **kwargs: Additional keyword arguments.
        z) does not implement `on_chat_model_start`	__class__r)   NotImplementedError	r   rV   messagesr   r   rR   rS   r   msgs	            r   on_chat_model_startz(CallbackManagerMixin.on_chat_model_start  s'    8 (())RS!#&&r    c                    y)a  Run when the Retriever starts running.

        Args:
            serialized: The serialized Retriever.
            query: The query.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            tags: The tags.
            metadata: The metadata.
            **kwargs: Additional keyword arguments.
        Nr   r   rV   queryr   r   rR   rS   r   s           r   on_retriever_startz'CallbackManagerMixin.on_retriever_start$  r   r    c                    ya  Run when a chain starts running.

        Args:
            serialized: The serialized chain.
            inputs: The inputs.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            tags: The tags.
            metadata: The metadata.
            **kwargs: Additional keyword arguments.
        Nr   r   rV   inputsr   r   rR   rS   r   s           r   on_chain_startz#CallbackManagerMixin.on_chain_start;  r   r    r   rR   rS   ri   c                    y)a  Run when the tool starts running.

        Args:
            serialized: The serialized chain.
            input_str: The input string.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            tags: The tags.
            metadata: The metadata.
            inputs: The inputs.
            **kwargs: Additional keyword arguments.
        Nr   	r   rV   	input_strr   r   rR   rS   ri   r   s	            r   on_tool_startz"CallbackManagerMixin.on_tool_startR  r   r    )rV   rG   rW   	list[str]r   r   r   r&   rR   list[str] | NonerS   dict[str, Any] | Noner   r   r'   r   rV   rG   r_   zlist[list[BaseMessage]]r   r   r   r&   rR   rq   rS   rr   r   r   r'   r   )rV   rG   rd   r8   r   r   r   r&   rR   rq   rS   rr   r   r   r'   r   )rV   rG   ri   rG   r   r   r   r&   rR   rq   rS   rr   r   r   r'   r   )rV   rG   rn   r8   r   r   r   r&   rR   rq   rS   rr   ri   rr   r   r   r'   r   )	r)   r*   r+   r,   rX   ra   re   rj   ro   r   r    r   rP   rP      s   % &*!%*." 
  #  (  
D &*!%*.'"' *'
 ' #' ' (' ' 
'J &*!%*." 
  #  (  
: &*!%*." 
  #  (  
: &*!%*.(," 
  #  ( &  
r    rP   c                  z    e Zd ZdZdd	 	 	 	 	 	 	 	 	 ddZdd	 	 	 	 	 	 	 	 	 d	dZddd	 	 	 	 	 	 	 	 	 	 	 	 	 d
dZy)RunManagerMixinzMixin for run manager.Nr   c                    y)a  Run on an arbitrary text.

        Args:
            text: The text.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            **kwargs: Additional keyword arguments.
        Nr   )r   textr   r   r   s        r   on_textzRunManagerMixin.on_texto  r   r    c                    ya  Run on a retry event.

        Args:
            retry_state: The retry state.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            **kwargs: Additional keyword arguments.
        Nr   r   retry_stater   r   r   s        r   on_retryzRunManagerMixin.on_retry  r   r    rR   rS   c                    y)a  Override to define a handler for a custom event.

        Args:
            name: The name of the custom event.
            data: The data for the custom event. Format will match
                the format specified by the user.
            run_id: The ID of the run.
            tags: The tags associated with the custom event
                (includes inherited tags).
            metadata: The metadata associated with the custom event
                (includes inherited metadata).
        Nr   r   namedatar   rR   rS   r   s          r   on_custom_eventzRunManagerMixin.on_custom_event  r   r    )
rw   r8   r   r   r   r&   r   r   r'   r   
r|   r	   r   r   r   r&   r   r   r'   r   )r   r8   r   r   r   r   rR   rq   rS   rr   r   r   r'   r   )r)   r*   r+   r,   rx   r}   r   r   r    r   ru   ru   l  s      &* 	
 #  
, &*# 	
 #  
. "&*. 
   (  
r    ru   c                      e Zd ZU dZdZded<   	 dZded<   	 edd       Zedd       Z	edd       Z
edd	       Zedd
       Zedd       Zedd       Zy)BaseCallbackHandlerr   Fboolraise_error
run_inlinec                     y)z Whether to ignore LLM callbacks.Fr   r   s    r   
ignore_llmzBaseCallbackHandler.ignore_llm       r    c                     y)z"Whether to ignore retry callbacks.Fr   r   s    r   ignore_retryz BaseCallbackHandler.ignore_retry  r   r    c                     y)z"Whether to ignore chain callbacks.Fr   r   s    r   ignore_chainz BaseCallbackHandler.ignore_chain  r   r    c                     y)z"Whether to ignore agent callbacks.Fr   r   s    r   ignore_agentz BaseCallbackHandler.ignore_agent  r   r    c                     y)z&Whether to ignore retriever callbacks.Fr   r   s    r   ignore_retrieverz$BaseCallbackHandler.ignore_retriever  r   r    c                     y)z'Whether to ignore chat model callbacks.Fr   r   s    r   ignore_chat_modelz%BaseCallbackHandler.ignore_chat_model  r   r    c                     y)zIgnore custom event.Fr   r   s    r   ignore_custom_eventz'BaseCallbackHandler.ignore_custom_event  r   r    Nr'   r   )r)   r*   r+   r,   r   __annotations__r   propertyr   r   r   r   r   r   r   r   r    r   r   r     s     /K;J-             r    r   c                     e Zd ZdZdddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZdddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZdddd	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddd	 	 	 	 	 	 	 	 	 	 	 dd	Zddd	 	 	 	 	 	 	 	 	 	 	 d d
Zdddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d!dZ	ddd	 	 	 	 	 	 	 	 	 	 	 d"dZ
ddd	 	 	 	 	 	 	 	 	 	 	 d dZddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d#dZddd	 	 	 	 	 	 	 	 	 	 	 d$dZddd	 	 	 	 	 	 	 	 	 	 	 d dZddd	 	 	 	 	 	 	 	 	 	 	 d%dZdd	 	 	 	 	 	 	 	 	 d&dZddd	 	 	 	 	 	 	 	 	 	 	 d'dZddd	 	 	 	 	 	 	 	 	 	 	 d(dZdddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d)dZddd	 	 	 	 	 	 	 	 	 	 	 d*dZddd	 	 	 	 	 	 	 	 	 	 	 d dZddd	 	 	 	 	 	 	 	 	 	 	 	 	 d+dZy),AsyncCallbackHandlerz%Async callback handler for LangChain.NrQ   c                  K   yw)ac  Run when the model starts running.

        !!! warning
            This method is called for non-chat models (regular LLMs). If you're
            implementing a handler for a chat model, you should use
            `on_chat_model_start` instead.

        Args:
            serialized: The serialized LLM.
            prompts: The prompts.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            tags: The tags.
            metadata: The metadata.
            **kwargs: Additional keyword arguments.
        Nr   rU   s           r   rX   z!AsyncCallbackHandler.on_llm_start          c               R   K   | j                   j                   d}t        |      wrZ   r[   r^   s	            r   ra   z(AsyncCallbackHandler.on_chat_model_start  s+     8 (())RS!#&&s   %')r/   r   rR   c                  K   yw)a  Run on new output token. Only available when streaming is enabled.

        For both chat models and non-chat models (legacy LLMs).

        Args:
            token: The new token.
            chunk: The new generated chunk, containing content and other information.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            tags: The tags.
            **kwargs: Additional keyword arguments.
        Nr   )r   r1   r/   r   r   rR   r   s          r   r2   z%AsyncCallbackHandler.on_llm_new_token  r   r   )r   rR   c                  K   yw)aS  Run when the model ends running.

        Args:
            response: The response which was generated.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            tags: The tags.
            **kwargs: Additional keyword arguments.
        Nr   )r   r4   r   r   rR   r   s         r   r5   zAsyncCallbackHandler.on_llm_end2  r   r   c                  K   yw)a  Run when LLM errors.

        Args:
            error: The error that occurred.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            tags: The tags.
            **kwargs: Additional keyword arguments.
                - response (LLMResult): The response which was generated before
                    the error occurred.
        Nr   r   r   r   r   rR   r   s         r   r7   z!AsyncCallbackHandler.on_llm_errorE  r   r   c                  K   ywrg   r   rh   s           r   rj   z#AsyncCallbackHandler.on_chain_startZ  r   r   c                  K   yw)aH  Run when a chain ends running.

        Args:
            outputs: The outputs of the chain.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            tags: The tags.
            **kwargs: Additional keyword arguments.
        Nr   )r   r=   r   r   rR   r   s         r   r>   z!AsyncCallbackHandler.on_chain_endq  r   r   c                  K   yw)a=  Run when chain errors.

        Args:
            error: The error that occurred.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            tags: The tags.
            **kwargs: Additional keyword arguments.
        Nr   r   s         r   r@   z#AsyncCallbackHandler.on_chain_error  r   r   rk   c                  K   yw)a  Run when the tool starts running.

        Args:
            serialized: The serialized tool.
            input_str: The input string.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            tags: The tags.
            metadata: The metadata.
            inputs: The inputs.
            **kwargs: Additional keyword arguments.
        Nr   rm   s	            r   ro   z"AsyncCallbackHandler.on_tool_start  r   r   c                  K   yw)aF  Run when the tool ends running.

        Args:
            output: The output of the tool.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            tags: The tags.
            **kwargs: Additional keyword arguments.
        Nr   )r   rK   r   r   rR   r   s         r   rL   z AsyncCallbackHandler.on_tool_end  r   r   c                  K   yw)a<  Run when tool errors.

        Args:
            error: The error that occurred.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            tags: The tags.
            **kwargs: Additional keyword arguments.
        Nr   r   s         r   rN   z"AsyncCallbackHandler.on_tool_error  r   r   c                  K   yw)a0  Run on an arbitrary text.

        Args:
            text: The text.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            tags: The tags.
            **kwargs: Additional keyword arguments.
        Nr   )r   rw   r   r   rR   r   s         r   rx   zAsyncCallbackHandler.on_text  r   r   r   c                  K   ywrz   r   r{   s        r   r}   zAsyncCallbackHandler.on_retry  r   r   c                  K   yw)a5  Run on agent action.

        Args:
            action: The agent action.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            tags: The tags.
            **kwargs: Additional keyword arguments.
        Nr   )r   rB   r   r   rR   r   s         r   rC   z$AsyncCallbackHandler.on_agent_action  r   r   c                  K   yw)a6  Run on the agent end.

        Args:
            finish: The agent finish.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            tags: The tags.
            **kwargs: Additional keyword arguments.
        Nr   )r   rE   r   r   rR   r   s         r   rF   z$AsyncCallbackHandler.on_agent_finish  r   r   c                  K   yw)a  Run on the retriever start.

        Args:
            serialized: The serialized retriever.
            query: The query.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            tags: The tags.
            metadata: The metadata.
            **kwargs: Additional keyword arguments.
        Nr   rc   s           r   re   z'AsyncCallbackHandler.on_retriever_start   r   r   c                  K   yw)aD  Run on the retriever end.

        Args:
            documents: The documents retrieved.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            tags: The tags.
            **kwargs: Additional keyword arguments.
        Nr   )r   r"   r   r   rR   r   s         r   r#   z%AsyncCallbackHandler.on_retriever_end7  r   r   c                  K   yw)a>  Run on retriever error.

        Args:
            error: The error that occurred.
            run_id: The run ID. This is the ID of the current run.
            parent_run_id: The parent run ID. This is the ID of the parent run.
            tags: The tags.
            **kwargs: Additional keyword arguments.
        Nr   r   s         r   r   z'AsyncCallbackHandler.on_retriever_errorJ  r   r   r~   c                  K   yw)a  Override to define a handler for custom events.

        Args:
            name: The name of the custom event.
            data: The data for the custom event. Format will match
                the format specified by the user.
            run_id: The ID of the run.
            tags: The tags associated with the custom event
                (includes inherited tags).
            metadata: The metadata associated with the custom event
                (includes inherited metadata).
        Nr   r   s          r   r   z$AsyncCallbackHandler.on_custom_event]  r   r   )rV   rG   rW   rp   r   r   r   r&   rR   rq   rS   rr   r   r   r'   Noners   )r1   r8   r/   r9   r   r   r   r&   rR   rq   r   r   r'   r   )r4   r   r   r   r   r&   rR   rq   r   r   r'   r   )r   r%   r   r   r   r&   rR   rq   r   r   r'   r   )rV   rG   ri   rG   r   r   r   r&   rR   rq   rS   rr   r   r   r'   r   )r=   rG   r   r   r   r&   rR   rq   r   r   r'   r   )rV   rG   rn   r8   r   r   r   r&   rR   rq   rS   rr   ri   rr   r   r   r'   r   )rK   r   r   r   r   r&   rR   rq   r   r   r'   r   )rw   r8   r   r   r   r&   rR   rq   r   r   r'   r   r   )rB   r   r   r   r   r&   rR   rq   r   r   r'   r   )rE   r   r   r   r   r&   rR   rq   r   r   r'   r   )rV   rG   rd   r8   r   r   r   r&   rR   rq   rS   rr   r   r   r'   r   )r"   r(   r   r   r   r&   rR   rq   r   r   r'   r   )r   r8   r   r   r   r   rR   rq   rS   rr   r   r   r'   r   )r)   r*   r+   r,   rX   ra   r2   r5   r7   rj   r>   r@   ro   rL   rN   rx   r}   rC   rF   re   r#   r   r   r   r    r   r   r     s   / &*!%*." 
  #  (  
D &*!%*.'"' *'
 ' #' ' (' ' 
'F ?C%)!% <	
  #   
8 &*!% 	
 #   
0 &*!% 	
 #   
6 &*!%*." 
  #  (  
8 &*!% 	
 #   
0 &*!% 	
 #   
2 &*!%*.(," 
  #  ( &  
< &*!% 	
 #   
0 &*!% 	
 #   
0 &*!% 	
 #   
0 &*# 	
 #  
, &*!% 	
 #   
0 &*!% 	
 #   
2 &*!%*." 
  #  (  
8 &*!%% 	
 #   
0 &*!% 	
 #   
2 "&*. 
   (  
r    r   c                      e Zd ZdZ	 	 dddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZddZedd       Z	 d	 	 	 	 	 ddZ	dd	Z
	 d	 	 	 	 	 dd
Z	 d	 	 	 	 	 ddZ	 d	 	 	 	 	 ddZddZ	 d	 	 	 	 	 ddZddZy)BaseCallbackManagerz$Base callback manager for LangChain.N)rR   inheritable_tagsrS   inheritable_metadatac                   || _         |xs g | _        || _        |xs g | _        |xs g | _        |xs i | _        |xs i | _        y)ar  Initialize callback manager.

        Args:
            handlers: The handlers.
            inheritable_handlers: The inheritable handlers.
            parent_run_id: The parent run ID.
            tags: The tags.
            inheritable_tags: The inheritable tags.
            metadata: The metadata.
            inheritable_metadata: The inheritable metadata.
        Nhandlersinheritable_handlersr   rR   r   rS   r   )r   r   r   r   rR   r   rS   r   s           r   __init__zBaseCallbackManager.__init__x  sU    , 4< &B 	! +8JB	 0 6B B$8$>B!r    c           
     f   | j                  | j                  j                         | j                  j                         | j                  | j
                  j                         | j                  j                         | j                  j                         | j                  j                               S )z&Return a copy of the callback manager.r   )	r\   r   copyr   r   rR   r   rS   r   r   s    r   r   zBaseCallbackManager.copy  s    ~~]]'')!%!:!:!?!?!A,,!!22779]]'')!%!:!:!?!?!A  
 	
r    c                6   t        t        | j                        t        |j                        z        }t        t        | j                        t        |j                        z        }| j	                  | j
                  xs |j
                  ||t        t        | j                  |j                  z               t        t        | j                  |j                  z               i | j                  |j                  i | j                  |j                        S )a  Merge the callback manager with another callback manager.

        May be overwritten in subclasses. Primarily used internally
        within merge_configs.

        Returns:
            The merged callback manager of the same type as the current object.

        Example: Merging two callback managers.

            ```python
            from langchain_core.callbacks.manager import (
                CallbackManager,
                trace_as_chain_group,
            )
            from langchain_core.callbacks.stdout import StdOutCallbackHandler

            manager = CallbackManager(handlers=[StdOutCallbackHandler()], tags=["tag2"])
            with trace_as_chain_group("My Group Name", tags=["tag1"]) as group_manager:
                merged_manager = group_manager.merge(manager)
                print(merged_manager.handlers)
                # [
                #    <langchain_core.callbacks.stdout.StdOutCallbackHandler object at ...>,
                #    <langchain_core.callbacks.streaming_stdout.StreamingStdOutCallbackHandler object at ...>,
                # ]

                print(merged_manager.tags)
                #    ['tag2', 'tag1']
            ```
        )r   r   r   rR   r   rS   r   )
listsetr   r   r\   r   rR   r   rS   r   )r   othercombined_handlerscombined_inheritables       r   mergezBaseCallbackManager.merge  s    B !T]]!3c%..6I!IJ#))*S1K1K-LL 
 ~~,,C0C0C&!5c$))ejj012!#d&;&;e>T>T&T"UV--.."++",,"  
 	
r    c                     y)z&Whether the callback manager is async.Fr   r   s    r   is_asynczBaseCallbackManager.is_async  r   r    c                    || j                   vr| j                   j                  |       |r+|| j                  vr| j                  j                  |       yyy)zAdd a handler to the callback manager.

        Args:
            handler: The handler to add.
            inherit: Whether to inherit the handler.
        N)r   appendr   r   handlerinherits      r   add_handlerzBaseCallbackManager.add_handler  sN     $--'MM  )wd&?&??%%,,W5 @7r    c                    || j                   v r| j                   j                  |       || j                  v r| j                  j                  |       yy)zoRemove a handler from the callback manager.

        Args:
            handler: The handler to remove.
        N)r   remover   )r   r   s     r   remove_handlerz"BaseCallbackManager.remove_handler  sI     dmm#MM  )d///%%,,W5 0r    c                T    g | _         g | _        |D ]  }| j                  ||        y)zSet handlers as the only handlers on the callback manager.

        Args:
            handlers: The handlers to set.
            inherit: Whether to inherit the handlers.
        r   N)r   r   r   )r   r   r   r   s       r   set_handlersz BaseCallbackManager.set_handlers  s5     $&! 	7GWg6	7r    c                ,    | j                  |g|       y)zSet handler as the only handler on the callback manager.

        Args:
            handler: The handler to set.
            inherit: Whether to inherit the handler.
        r   N)r   r   s      r   set_handlerzBaseCallbackManager.set_handler
  s     	7)W5r    c                    |D ]#  }|| j                   v s| j                  |g       % | j                   j                  |       |r| j                  j                  |       yy)zAdd tags to the callback manager.

        Args:
            tags: The tags to add.
            inherit: Whether to inherit the tags.
        N)rR   remove_tagsextendr   )r   rR   r   tags       r   add_tagszBaseCallbackManager.add_tags  s_      	(Cdii  #'	( 			!!((. r    c                    |D ]U  }|| j                   v r| j                   j                  |       || j                  v s;| j                  j                  |       W y)zdRemove tags from the callback manager.

        Args:
            tags: The tags to remove.
        N)rR   r   r   )r   rR   r   s      r   r   zBaseCallbackManager.remove_tags)  sS      	2Cdii		  %d+++%%,,S1		2r    c                v    | j                   j                  |       |r| j                  j                  |       yy)zAdd metadata to the callback manager.

        Args:
            metadata: The metadata to add.
            inherit: Whether to inherit the metadata.
        N)rS   updater   )r   rS   r   s      r   add_metadataz BaseCallbackManager.add_metadata5  s2     	X&%%,,X6 r    c                    |D ]:  }| j                   j                  |d       | j                  j                  |d       < y)zhRemove metadata from the callback manager.

        Args:
            keys: The keys to remove.
        N)rS   popr   )r   keyskeys      r   remove_metadataz#BaseCallbackManager.remove_metadataD  s=      	5CMMc4(%%))#t4	5r    )NN)r   list[BaseCallbackHandler]r   z list[BaseCallbackHandler] | Noner   r&   rR   rq   r   rq   rS   rr   r   rr   r'   r   )r'   r
   )r   r   r'   r
   r   )T)r   r   r   r   r'   r   )r   r   r'   r   )r   r   r   r   r'   r   )rR   rp   r   r   r'   r   )rR   rp   r'   r   )rS   rG   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    r   r   r   u  sb   .
 BF%)	? "&-1*.6:?+? ?? #	? ? +? (? 4? 
?@

4
l   6$6 6 
	6 	6 7+7 7 
	7& 6$6 6 
	6  // / 
	/$
2 7 7 7 
	75r    r   )(r,   
__future__r   loggingtypingr   r   collections.abcr   uuidr   tenacityr	   typing_extensionsr
   langchain_core.agentsr   r   langchain_core.documentsr   langchain_core.messagesr   langchain_core.outputsr   r   r   	getLoggerr)   _LOGGERr   r.   r;   rI   rP   ru   r   r   r   r   	Callbacksr   r    r   <module>r      s    * "  %('&>13VV
'

H
%# #L8 8vE EP# #LC CL: :z11hU. UpW5. W5t $%(;;dB	r    