
    <iq             	         S r 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
  SSKJrJr  SSKJr  SSKJrJrJrJrJrJrJrJrJrJrJr  SS	KJrJrJrJ r J!r!J"r"  SS
K#J$r$J%r%J&r&  SSK'J(r(J)r)  SSK'J*r+  SSK,J-r-J.r.J/r/J0r0  SSK1J2r2  SSK3J4r4  SSK5J6r6  SSK7J8r8  SSK9J:r:J;r;J<r<  SSK=J>r>J?r?  SSK@JArAJBrB  \(       a  SSKJCrC  SSKDJErE  SSKFJGrG  \A" S\HS9rI\A" SSS9rJSrKSrLSrMSrN " S S \S!S"9rO\ " S# S$5      5       rP\\P\\P/\!\:-  4   /\!\:-  4   rQ \\P\\P/\\!\:-     4   /\\!\:-     4   rR  " S% S&\5      rSS;S' jrT " S( S)\.5      rUS<S* jrV      S=S+ jrWS>S, jrX          S?S- jrY " S. S/\25      rZ S@     SAS0 jjr[\ " S1 S2\/\\J\I4   5      5       r\ " S3 S4\)5      r] " S5 S6\)5      r^      SBS7 jr_SCS8 jr`SDS9 jraSDS: jrbg)Ea  Tool execution node for LangGraph workflows.

This module provides prebuilt functionality for executing tools in LangGraph.

Tools are functions that models can call to interact with external systems,
APIs, databases, or perform computations.

The module implements design patterns for:
- Parallel execution of multiple tool calls for efficiency
- Robust error handling with customizable error messages
- State injection for tools that need access to graph state
- Store injection for tools that need persistent storage
- Command-based state updates for advanced control flow

Key Components:
    `ToolNode`: Main class for executing tools in LangGraph workflows
    `InjectedState`: Annotation for injecting graph state into tools
    `InjectedStore`: Annotation for injecting persistent store into tools
    `ToolRuntime`: Runtime information for tools, bundling together state, context, config, stream_writer, tool_call_id, and store
    `tools_condition`: Utility function for conditional routing based on tool calls

Typical Usage:
    ```python
    from langchain_core.tools import tool
    from langchain.tools import ToolNode


    @tool
    def my_tool(x: int) -> str:
        return f"Result: {x}"


    tool_node = ToolNode([my_tool])
    ```
    )annotationsN)	AwaitableCallable)copydeepcopy)	dataclassreplace)	UnionType)TYPE_CHECKING	AnnotatedAnyGenericLiteral	TypedDictUnioncastget_args
get_originget_type_hints)	AIMessage
AnyMessageRemoveMessageToolCallToolMessageconvert_to_messages)RunnableConfigget_config_listget_executor_for_config)BaseToolInjectedToolArg)tool)TOOL_MESSAGE_BLOCK_TYPESToolException_DirectlyInjectedToolArgget_all_basemodel_annotations)RunnableCallable)GraphBubbleUp)REMOVE_ALL_MESSAGES)	BaseStore)CommandSendStreamWriter)	BaseModelValidationError)TypeVarUnpack)Sequence)Runtime)ErrorDetailsStateT)defaultContextTzLError: {requested_tool} is not a valid tool, try one of [{available_tools}].z)Error: {error}
 Please fix your mistakes.zvError executing tool '{tool_name}' with kwargs {tool_kwargs} with error:
 {error}
 Please fix the error and try again.zuError invoking tool '{tool_name}' with kwargs {tool_kwargs} with error:
 {error}
 Please fix the error and try again.c                  $    \ rS rSr% SrS\S'   Srg)_ToolCallRequestOverridesu   z9Possible overrides for ToolCallRequest.override() method.r   	tool_call N__name__
__module____qualname____firstlineno____doc____annotations____static_attributes__r;       l/home/dmtnaga/Documents/work/airagagent/rag_env/lib/python3.13/site-packages/langgraph/prebuilt/tool_node.pyr8   r8   u   s    CrD   r8   F)totalc                  T    \ rS rSr% SrS\S'   S\S'   S\S'   S	\S
'       SS jrSrg)ToolCallRequest{   aB  Tool execution request passed to tool call interceptors.

Attributes:
    tool_call: Tool call dict with name, args, and id from model output.
    tool: BaseTool instance to be invoked, or None if tool is not
        registered with the `ToolNode`. When tool is `None`, interceptors can
        handle the request without validation. If the interceptor calls `execute()`,
        validation will occur and raise an error for unregistered tools.
    state: Agent state (`dict`, `list`, or `BaseModel`).
    runtime: LangGraph runtime context (optional, `None` if outside graph).
r   r:   zBaseTool | Noner!   r   stateToolRuntimeruntimec                    [        U 40 UD6$ )a  Replace the request with a new request with the given overrides.

Returns a new `ToolCallRequest` instance with the specified attributes replaced.
This follows an immutable pattern, leaving the original request unchanged.

Args:
    **overrides: Keyword arguments for attributes to override. Supported keys:
        - tool_call: Tool call dict with name, args, and id

Returns:
    New ToolCallRequest instance with specified overrides applied.

Examples:
    ```python
    # Modify tool call arguments without mutating original
    modified_call = {**request.tool_call, "args": {"value": 10}}
    new_request = request.override(tool_call=modified_call)

    # Override multiple attributes
    new_request = request.override(tool_call=modified_call, state=new_state)
    ```
)r	   )self	overridess     rE   overrideToolCallRequest.override   s    2 t)y))rD   r;   N)rO   z!Unpack[_ToolCallRequestOverrides]returnrH   )r=   r>   r?   r@   rA   rB   rP   rC   r;   rD   rE   rH   rH   {   s5    
 
J*<*	*rD   rH   c                  :    \ rS rSr% SrS\S'   S\S'    S\S'   S	rg
)ToolCallWithContext   a}  ToolCall with additional context for graph state.

This is an internal data structure meant to help the `ToolNode` accept
tool calls with additional context (e.g. state) when dispatched using the
Send API.

The Send API is used in create_agent to distribute tool calls in parallel
and support human-in-the-loop workflows where graph execution may be paused
for an indefinite time.
r   r:   z!Literal['tool_call_with_context']_ToolCallWithContext__typer   rJ   r;   Nr<   r;   rD   rE   rT   rT      s$    	 --
 J6rD   rT   c                    [        U [        5      (       d,  [        U [        5      (       a  [        S U  5       5      (       a  U $  [        R
                  " U SS9$ ! [         a    [        U 5      s $ f = f)a<  Convert tool output to `ToolMessage` content format.

Handles `str`, `list[dict]` (content blocks), and arbitrary objects by attempting
JSON serialization with fallback to str().

Args:
    output: Tool execution output of any type.

Returns:
    String or list of content blocks suitable for `ToolMessage.content`.
c              3     #    U  H4  n[        U[        5      =(       a    UR                  S 5      [        ;   v   M6     g7f)typeN)
isinstancedictgetr"   ).0xs     rE   	<genexpr>%msg_content_output.<locals>.<genexpr>"  s4      
 q$MAEE&M5M$MMs   <>F)ensure_ascii)rZ   strlistalljsondumps	Exception)outputs    rE   msg_content_outputri     sn     &#64   

 
 

 
zz&u55 6{s   A A21A2c                  H   ^  \ rS rSrSr S         SU 4S jjjrSrU =r$ )ToolInvocationErrori2  zAn error occurred while invoking a tool due to invalid arguments.

This exception is only raised when invoking a tool using the `ToolNode`!
c                  > Ubm  / nU HS  nSR                  S UR                  SS5       5       5      nUR                  SS5      nUR                  U SU 35        MU     S	R                  U5      n	O[        U5      n	[        R                  XU	S
9U l        Xl        X0l        X l	        X@l
        [        T
U ]1  U R                  5        g)a1  Initialize the ToolInvocationError.

Args:
    tool_name: The name of the tool that failed.
    source: The exception that occurred.
    tool_kwargs: The keyword arguments that were passed to the tool.
    filtered_errors: Optional list of filtered validation errors excluding
        injected arguments.
N.c              3  8   #    U  H  n[        U5      v   M     g 7fN)rb   )r]   locs     rE   r_   /ToolInvocationError.__init__.<locals>.<genexpr>M  s     "L7K3s887Ks   rp   r;   msgzUnknown errorz: 
)	tool_nametool_kwargserror)joinr\   appendrb   TOOL_INVOCATION_ERROR_TEMPLATEformatmessagert   ru   sourcefiltered_errorssuper__init__)rN   rt   r|   ru   r}   error_str_partsrv   loc_strrr   error_display_str	__class__s             rE   r   ToolInvocationError.__init__8  s    " & O((("Luyy7K"LLii7&&'"SE':; ) !%		/ : #F5<<@Q = 
 #&.&rD   )r}   r{   r|   ru   rt   ro   )
rt   rb   r|   r.   ru   dict[str, Any]r}   zlist[ErrorDetails] | NonerR   None)r=   r>   r?   r@   rA   r   rC   __classcell__r   s   @rE   rk   rk   2  sG     6:#'#'  #' $	#'
 3#' 
#' #'rD   rk   c                H    [        U [        5      (       a  U R                  $ U e)zDefault error handler for tool errors.

If the tool is a tool invocation error, return its message.
Otherwise, raise the error.
)rZ   rk   r{   )es    rE   _default_handle_tool_errorsr   ^  s!     !())yy
GrD   c               N   [        U[        [        45      (       d*  [        U[        5      (       a3  [	        U[
        5      (       a  [        R                  [        U 5      S9nU$ [        U[        5      (       a  UnU$ [        U5      (       a
  U" U 5      nU$ SU 3n[        U5      e)a|  Generate error message content based on exception handling configuration.

This function centralizes error message generation logic, supporting different
error handling strategies configured via the `ToolNode`'s `handle_tool_errors`
parameter.

Args:
    e: The exception that occurred during tool execution.
    flag: Configuration for how to handle the error. Can be:
        - bool: If `True`, use default error template
        - str: Use this string as the error message
        - Callable: Call this function with the exception to get error message
        - tuple: Not used in this context (handled by caller)

Returns:
    A string containing the error message to include in the `ToolMessage`.

Raises:
    ValueError: If flag is not one of the supported types.

!!! note
    The tuple case is handled by the caller through exception type checking,
    not by this function directly.
)rv   zVGot unexpected type of `handle_tool_error`. Expected bool, str or callable. Received: )rZ   booltuplerY   
issubclassrg   TOOL_CALL_ERROR_TEMPLATErz   reprrb   callable
ValueError)r   flagcontentrr   s       rE   _handle_tool_errorr   i  s    B $u&&4:dI#>#>*11Q1@ N 
D#		 N 
$q' N	&&*V- 	 orD   c                z   [         R                  " U 5      n[        UR                  R	                  5       5      nU(       a  US   R
                  S;   a  [        U5      S:X  a  US   nOUS   n[        U 5      nUR
                  U;   a  [        UR                  5      nU[        [        4;   aR  [        UR                  5      n[        S U 5       5      (       a  [        U5      $ SUR                   S3n[        U5      eXCR
                     n[         UR"                  ;   a  U4$ SU S3n[        U5      e[         4$ )	a  Infer exception types handled by a custom error handler function.

This function analyzes the type annotations of a custom error handler to determine
which exception types it's designed to handle. This enables type-safe error handling
where only specific exceptions are caught and processed by the handler.

Args:
    handler: A callable that takes an exception and returns an error message string.
            The first parameter (after self/cls if present) should be type-annotated
            with the exception type(s) to handle.

Returns:
    A tuple of exception types that the handler can process. Returns (Exception,)
    if no specific type information is available for backward compatibility.

Raises:
    ValueError: If the handler's annotation contains non-Exception types or
        if Union types contain non-Exception types.

!!! note
    This function supports both single exception types and Union types for
    handlers that need to handle multiple exception types differently.
r   )rN   cls      c              3  B   #    U  H  n[        U[        5      v   M     g 7fro   )r   rg   )r]   args     rE   r_   '_infer_handled_types.<locals>.<genexpr>  s     BTcz#y11T   zAll types in the error handler error annotation must be Exception types. For example, `def custom_handler(e: Union[ValueError, TypeError])`. Got 'z
' instead.a  Arbitrary types are not supported in the error handler signature. Please annotate the error with either a specific Exception type or a union of Exception types. For example, `def custom_handler(e: ValueError)` or `def custom_handler(e: Union[ValueError, TypeError])`. Got ')inspect	signaturerc   
parametersvaluesnamelenr   r   
annotationr   r
   r   rd   r   r   rg   __mro__)	handlersigparamsfirst_param
type_hintsoriginargsrr   exception_types	            rE   _infer_handled_typesr     s:   0 

G
$C#..'')*F!9>>_,V1A )K )K#G,
z) 6 67F%++ 6 67BTBBB ;& (223:?  !o%'(8(89NN222&((
 ''z3  S/! <rD   c                   [        UR                  5       5      nU(       a  UR                  U5        U(       a  UR                  U5        / nU R                  5        H  nUS   (       d  M  US   S   U;  d  M  0 UEn[	        UR                  S5      [        5      (       a4  US   nUR                  5        V	V
s0 s H  u  pX;  d  M  X_M     nn	n
XS'   UR                  U5        M     U$ s  sn
n	f )a  Filter validation errors to only include LLM-controlled arguments.

When a tool invocation fails validation, only errors for arguments that the LLM
controls should be included in error messages. This ensures the LLM receives
focused, actionable feedback about parameters it can actually fix. System-injected
arguments (state, store, runtime) are filtered out since the LLM has no control
over them.

This function also removes injected argument values from the `input` field in error
details, ensuring that only LLM-provided arguments appear in error messages.

Args:
    validation_error: The Pydantic ValidationError raised during tool invocation.
    tool_to_state_args: Mapping of state argument names to state field names.
    tool_to_store_arg: Name of the store argument, if any.
    tool_to_runtime_arg: Name of the runtime argument, if any.

Returns:
    List of ErrorDetails containing only errors for LLM-controlled arguments,
    with system-injected argument values removed from the input field.
rp   r   input)	setkeysadderrorsrZ   r\   r[   itemsrx   )validation_errortool_to_state_argstool_to_store_argtool_to_runtime_arginjected_argsr}   rv   
error_copy
input_dictkv
input_copys               rE   _filter_validation_errorsr     s    6 *//12M+,-.*,O!((* <<E%LO=@)2EJ *..1488'0
%/%5%5%7%7TQ1;QDAD%7   '17# "":.! +$ s   9C,C,c                    ^  \ rS rSr% SrSrS\S'   SS\SSSS.               SU 4S	 jjjr\	SS
 j5       r
        SS jr        SS jr      SS jr        SS jr        S S jr        SS jr        S S jr    S!S jrS"S jr    S#S jr      S$S jrS%S jr      S&S jr      S&S jr        S'S jrSrU =r$ )(ToolNodei  u'  A node for executing tools in LangGraph workflows.

Handles tool execution patterns including function calls, state injection,
persistent storage, and control flow. Manages parallel execution,
error handling.

Input Formats:
    1. Graph state with `messages` key that has a list of messages:
        - Common representation for agentic workflows
        - Supports custom messages key via `messages_key` parameter

    2. **Message List**: `[AIMessage(..., tool_calls=[...])]`
        - List of messages with tool calls in the last AIMessage

    3. **Direct Tool Calls**: `[{"name": "tool", "args": {...}, "id": "1", "type": "tool_call"}]`
        - Bypasses message parsing for direct tool execution
        - For programmatic tool invocation and testing

Output Formats:
    Output format depends on input type and tool behavior:

    **For Regular tools**:
    - Dict input → `{"messages": [ToolMessage(...)]}`
    - List input → `[ToolMessage(...)]`

    **For Command tools**:
    - Returns `[Command(...)]` or mixed list with regular tool outputs
    - Commands can update state, trigger navigation, or send messages

Args:
    tools: A sequence of tools that can be invoked by this node. Supports:
        - **BaseTool instances**: Tools with schemas and metadata
        - **Plain functions**: Automatically converted to tools with inferred schemas
    name: The name identifier for this node in the graph. Used for debugging
        and visualization. Defaults to "tools".
    tags: Optional metadata tags to associate with the node for filtering
        and organization. Defaults to `None`.
    handle_tool_errors: Configuration for error handling during tool execution.
        Supports multiple strategies:

        - **True**: Catch all errors and return a ToolMessage with the default
            error template containing the exception details.
        - **str**: Catch all errors and return a ToolMessage with this custom
            error message string.
        - **type[Exception]**: Only catch exceptions with the specified type and
            return the default error message for it.
        - **tuple[type[Exception], ...]**: Only catch exceptions with the specified
            types and return default error messages for them.
        - **Callable[..., str]**: Catch exceptions matching the callable's signature
            and return the string result of calling it with the exception.
        - **False**: Disable error handling entirely, allowing exceptions to
            propagate.

        Defaults to a callable that:
            - catches tool invocation errors (due to invalid arguments provided by the model) and returns a descriptive error message
            - ignores tool execution errors (they will be re-raised)

    messages_key: The key in the state dictionary that contains the message list.
        This same key will be used for the output `ToolMessage` objects.
        Defaults to "messages".
        Allows custom state schemas with different message field names.

Examples:
    Basic usage:

    ```python
    from langchain.tools import ToolNode
    from langchain_core.tools import tool

    @tool
    def calculator(a: int, b: int) -> int:
        """Add two numbers."""
        return a + b

    tool_node = ToolNode([calculator])
    ```

    State injection:

    ```python
    from typing_extensions import Annotated
    from langchain.tools import InjectedState

    @tool
    def context_tool(query: str, state: Annotated[dict, InjectedState]) -> str:
        """Some tool that uses state."""
        return f"Query: {query}, Messages: {len(state['messages'])}"

    tool_node = ToolNode([context_tool])
    ```

    Error handling:

    ```python
    def handle_errors(e: ValueError) -> str:
        return "Invalid input provided"


    tool_node = ToolNode([my_tool], handle_tool_errors=handle_errors)
    ```
toolsrb   r   Nmessages)r   tagshandle_tool_errorsmessages_keywrap_tool_callawrap_tool_callc                 > [         T
U ]  U R                  U R                  X#SS9  0 U l        0 U l        0 U l        0 U l        X@l        XPl	        X`l
        Xpl        U H  n[        U[        5      (       d  [        [        SU5      5      n	OUn	XR                  U	R                   '   [#        U	5      U R
                  U	R                   '   [%        U	5      U R                  U	R                   '   ['        U	5      U R                  U	R                   '   M     g)a  Initialize `ToolNode` with tools and configuration.

Args:
    tools: Sequence of tools to make available for execution.
    name: Node name for graph identification.
    tags: Optional metadata tags.
    handle_tool_errors: Error handling configuration.
    messages_key: State key containing messages.
    wrap_tool_call: Sync wrapper function to intercept tool execution. Receives
        ToolCallRequest and execute callable, returns ToolMessage or Command.
        Enables retries, caching, request modification, and control flow.
    awrap_tool_call: Async wrapper function to intercept tool execution.
        If not provided, falls back to wrap_tool_call for async execution.
F)r   r   traceztype[BaseTool]N)r~   r   _func_afunc_tools_by_name_tool_to_state_args_tool_to_store_arg_tool_to_runtime_arg_handle_tool_errors_messages_key_wrap_tool_call_awrap_tool_callrZ   r   create_toolr   r   _get_state_args_get_store_arg_get_runtime_arg)rN   r   r   r   r   r   r   r   r!   tool_r   s             rE   r   ToolNode.__init__}  s    : 	T[[teT35EG 9;;=!#5 )- /DdH--#D)94$@A.3

+3B53ID$$UZZ02@2GD##EJJ/4DU4KD%%ejj1 rD   c                    U R                   $ )z,Mapping from tool name to BaseTool instance.)r   )rN   s    rE   tools_by_nameToolNode.tools_by_name  s     """rD   c           
        U R                  U5      u  pE[        U[        U5      5      n/ n[        XFSS9 HV  u  pU R	                  U5      n
[        U
US   U	UR                  UR                  UR                  S9nUR                  U5        MX     U/[        U5      -  n[        U5       n[        UR                  U R                  XLU5      5      nS S S 5        U R                  WU5      $ ! , (       d  f       N = fNF)strictid)rJ   tool_call_idconfigcontextstorestream_writer)_parse_inputr   r   zip_extract_staterK   r   r   r   rx   r   rc   map_run_one_combine_tool_outputs)rN   r   r   rL   
tool_calls
input_typeconfig_listtool_runtimescallcfgrJ   tool_runtimeinput_typesexecutoroutputss                  rE   r   ToolNode._func  s     "&!2!25!9
%fc*o> ZUCID''.E&!$Zmm%33L   . D "lS_4$V,T]]J]SG -
 ))':>> -,s   )'C**
C8c           
       #    U R                  U5      u  pE[        U[        U5      5      n/ n[        XFSS9 HV  u  pU R	                  U5      n
[        U
US   U	UR                  UR                  UR                  S9nUR                  U5        MX     / n[        XGSS9 H&  u  pUR                  U R                  XU5      5        M(     [        R                  " U6 I S h  vN nU R                  X5      $  N7fr   )r   r   r   r   r   rK   r   r   r   rx   	_arun_oneasynciogatherr   )rN   r   r   rL   r   r   r   r   r   r   rJ   r   corosr   s                 rE   r   ToolNode._afunc  s      "&!2!25!9
%fc*o> ZUCID''.E&!$Zmm%33L   . D "%j"NDLL,GH #O..))'>> /s   CC5C3C5c                   [        S U 5       5      (       d  US:X  a  U$ U R                  U0$ / nS nU GH  n[        U[        5      (       a  UR                  [        R
                  L a  [        UR                  [        5      (       ay  [        S UR                   5       5      (       aX  U(       a-  [        U[        SUR                  5      UR                  -   S9nM  [        [        R
                  UR                  S9nM  UR                  U5        M  UR                  US:X  a  U/OU R                  U/05        GM     U(       a  UR                  U5        U$ )Nc              3  B   #    U  H  n[        U[        5      v   M     g 7fro   )rZ   r*   )r]   rh   s     rE   r_   1ToolNode._combine_tool_outputs.<locals>.<genexpr>  s     EW6:fg..Wr   rc   c              3  B   #    U  H  n[        U[        5      v   M     g 7fro   )rZ   r+   )r]   sends     rE   r_   r  	  s     K{tJtT22{r   z
list[Send])goto)graphr  )anyr   rZ   r*   r  PARENTr  rc   rd   r	   r   rx   )rN   r   r   combined_outputsparent_commandrh   s         rE   r   ToolNode._combine_tool_outputs  s     EWEEE(F27U9K9KW8UU  	
 *.F&'**LLGNN2"6;;55Kv{{KKK%)0*!%lN4G4G!H6;;!V*
 *1w~~FKK)X$++F3 '' *f 4VH4;M;MPVx:X# * ##N3rD   c           
        UR                   nUR                  nUc.  U R                  U5      =n(       a  U$ SUS    S3n[        U5      eU R	                  XAR
                  5      n0 UESS0En	  UR                  X5      n
[!        U
[2        5      (       a  U R5                  XR                   U5      $ [!        U
[0        5      (       a&  [7        S[9        U
R:                  5      5      U
l        U
$ SUS    S[%        U
5       3n[        U5      e! [         a|  n[        UU R                  R                  US   0 5      U R                  R                  US   5      U R                  R                  US   5      5      n[        US   XS   U5      UeSnAff = f! [         a    e [         Ga  n[!        U R"                  [$        5      (       a-  ['        U R"                  [        5      (       a  U R"                  4nO[!        U R"                  [(        5      (       a  U R"                  nOV[+        U R"                  5      (       a5  [!        U R"                  [$        5      (       d  [-        U R"                  5      nO[        4nU R"                  (       a  [!        X5      (       d  e [/        XR"                  S9n[1        UUS   US	   S
S9s SnA$ SnAff = f)a  Execute tool call with configured error handling.

Args:
    request: Tool execution request.
    input_type: Input format.
    config: Runnable configuration.

Returns:
    ToolMessage or Command.

Raises:
    Exception: If tool fails and handle_tool_errors is False.
NTool r     is not registered with ToolNoderY   r:   r   r   r   rv   r   r   r   status
str | list returned unexpected type: )r:   r!   _validate_tool_call	TypeError_inject_tool_argsrL   invoker.   r   r   r\   r   r   rk   r'   rg   rZ   r   rY   r   r   r   r   r   r   r*   _validate_tool_commandr   ri   r   rN   requestr   r   r   r!   invalid_tool_messagerr   injected_call	call_argsresponseexcr}   r   handled_typesr   s                   rE   _execute_tool_syncToolNode._execute_tool_sync  s   &   || <'+'?'?'EE#E++$v,'GHCC.  ..t__E:}:fk:	7	;;y9p h((..x9J9JJWWh,,#L2DXEUEU2VWHOd6l^#>tH~>NOn # ";,,00frB++//V=--11$v,?	# *L#F|_,  	 	 $22D99j(()? ? "&!9!9 ;D44e<< $ 8 8$2233J(($= = !5T5M5M N "+ ++:a3O3O )1I1IJG&\!$Z	 /	s8   /D 
FA7FFF K)DK=KKc                  ^ ^^	 T R                   R                  US   5      n[        UUUR                  US9nUR                  m	T R
                  c  T R                  UTT	5      $ S	U	UU 4S jjn T R                  XV5      $ ! [         aV  nT R                  (       d  e [        UT R                  S9n[        UUR                  S   UR                  S   SS9s SnA$ SnAff = f)
zExecute single tool call with wrap_tool_call wrapper if configured.

Args:
    call: Tool call dict.
    input_type: Input format.
    tool_runtime: Tool runtime.

Returns:
    ToolMessage or Command.
r   r:   r!   rJ   rL   Nc                *   > TR                  U TT5      $ )>Execute tool with given request. Can be called multiple times.r!  reqr   r   rN   s    rE   execute"ToolNode._run_one.<locals>.execute      **3
FCCrD   r  r   rv   r  r)  rH   rR   ToolMessage | Command)r   r\   rH   rJ   r   r   r!  rg   r   r   r   r:   )
rN   r   r   r   r!   tool_requestr*  r   r   r   s
   ` `      @rE   r   ToolNode._run_one  s    $ !!%%d6l3 '$$ 	
 $$'**<VLL	D 	D
	''>> 	++(1I1IJG!++F3)33D9	 	s   /B   
C 
ACC C c           
       #    UR                   nUR                  nUc.  U R                  U5      =n(       a  U$ SUS    S3n[        U5      eU R	                  XAR
                  5      n0 UESS0En	  UR                  X5      I Sh  vN n
[!        U
[2        5      (       a  U R5                  XR                   U5      $ [!        U
[0        5      (       a&  [7        S[9        U
R:                  5      5      U
l        U
$ SUS    S[%        U
5       3n[        U5      e N! [         a|  n[        UU R                  R                  US   0 5      U R                  R                  US   5      U R                  R                  US   5      5      n[        US   XS   U5      UeSnAff = f! [         a    e [         Ga  n[!        U R"                  [$        5      (       a-  ['        U R"                  [        5      (       a  U R"                  4nO[!        U R"                  [(        5      (       a  U R"                  nOV[+        U R"                  5      (       a5  [!        U R"                  [$        5      (       d  [-        U R"                  5      nO[        4nU R"                  (       a  [!        X5      (       d  e [/        XR"                  S9n[1        UUS   US	   S
S9s SnA$ SnAff = f7f)a  Execute tool call asynchronously with configured error handling.

Args:
    request: Tool execution request.
    input_type: Input format.
    config: Runnable configuration.

Returns:
    ToolMessage or Command.

Raises:
    Exception: If tool fails and handle_tool_errors is False.
Nr  r   r  rY   r:   r   r  r   rv   r  r  r  )r:   r!   r  r  r  rL   ainvoker.   r   r   r\   r   r   rk   r'   rg   rZ   r   rY   r   r   r   r   r   r   r*   r  r   ri   r   r  s                   rE   _execute_tool_asyncToolNode._execute_tool_async  s    &   || <'+'?'?'EE#E++$v,'GHCC.  ..t__E:}:fk:	7	!%i!@@p h((..x9J9JJWWh,,#L2DXEUEU2VWHOd6l^#>tH~>NOnA A" ";,,00frB++//V=--11$v,?	# *L#F|_,  	 	 $22D99j(()? ? "&!9!9 ;D44e<< $ 8 8$2233J(($= = !5T5M5M N "+ ++:a3O3O )1I1IJG&\!$Z	 /	sc   A-K1D DD 
BKD 
F!A7FFF   K5DK	K
KKKc                  ^ ^^
#    T R                   R                  US   5      n[        UUUR                  US9nUR                  m
T R
                  c(  T R                  c  T R                  UTT
5      I Sh  vN $ SU
UU 4S jjnSU
UU 4S jjn T R
                  b  T R                  XV5      I Sh  vN $ [        ST R                  5      T l        T R                  XW5      $  Nk N2! [         aV  nT R                  (       d  e [        UT R                  S9n	[        U	UR                  S   UR                  S   S	S
9s SnA$ SnAff = f7f)zExecute single tool call asynchronously with awrap_tool_call wrapper if configured.

Args:
    call: Tool call dict.
    input_type: Input format.
    tool_runtime: Tool runtime.

Returns:
    ToolMessage or Command.
r   r$  Nc                F   >#    TR                  U TT5      I Sh  vN $  N7f)r&  N)r3  r(  s    rE   r*  #ToolNode._arun_one.<locals>.executeC  s!     11#z6JJJJs   !!c                *   > TR                  U TT5      $ )z'Sync execute fallback for sync wrapper.r'  r(  s    rE   _sync_execute)ToolNode._arun_one.<locals>._sync_executeG  r,  rD   ToolCallWrapperr  r   rv   r  r-  )r   r\   rH   rJ   r   r   r   r3  r   rg   r   r   r   r:   )rN   r   r   r   r!   r/  r*  r9  r   r   r   s   ` `       @rE   r   ToolNode._arun_one   sJ    $ !!%%d6l3 '$$ 	
 $$  (T-A-A-I11,
FSSS	K 	K	D 	D
	$$0!22<III#'(94;O;O#PD ''DD# T J  	++(1I1IJG!++F3)33D9	 	sa   A1E	6C"7E	!C& 1C$2C& 5E	6+C& !E	$C& &
E0AE;E<E	EE	c                   [        U[        5      (       aF  [        US   [        5      (       a)  US   R                  S5      S:X  a  Sn[	        SU5      nX24$ SnUnO[        U[        5      (       a+  UR                  S5      S:X  a  [	        S	U5      nSnUS   /U4$ [        U[        5      (       a&  UR                  U R
                  / 5      =n(       a  S
nO-[        XR
                  / 5      =n(       a  S
nOSn[        U5      e [        S [        U5       5       5      n[        UR                  5      nX24$ ! [         a    Sn[        U5      ef = f)NrY   r:   r   zlist[ToolCall]rc   __typetool_call_with_contextrT   r[   zNo message found in inputc              3  T   #    U  H  n[        U[        5      (       d  M  Uv   M      g 7fro   )rZ   r   )r]   ms     rE   r_   (ToolNode._parse_input.<locals>.<genexpr>  s      %-aAy1I-s   (	(zNo AIMessage found in input)rZ   rc   r[   r\   r   r   getattrr   nextreversedStopIterationr   )rN   r   r   r   r   input_with_ctxrr   latest_ai_messages           rE   r   ToolNode._parse_input_  sX   
 eT""%)T**uRy}}V/D/S)
!"2E:
!--JHud##		((;?W(W ""7?N%J";/0*<<t$$		$"4"4b99H9J (:(:B??X?J-CS/!	" $ %#H-% ! +667
%%  	"/CS/!	"s   D8 8Ec                    US   nX R                   ;  aT  [        U R                   R                  5       5      n[        R	                  USR                  U5      S9n[        XBUS   SS9$ g )Nr   , )requested_toolavailable_toolsr   rv   )r   r   r  )r   rc   r    INVALID_TOOL_NAME_ERROR_TEMPLATErz   rw   r   )rN   r   rM  all_tool_namesr   s        rE   r  ToolNode._validate_tool_call  ss    f!3!33!$"4"4"9"9";<N6==- $		. 9 > G 4:g  rD   c                d    [        U[        5      (       a  UR                  S5      S:X  a  US   $ U$ )zExtract state from input, handling ToolCallWithContext if present.

Args:
    input: The input which may be raw state or ToolCallWithContext.

Returns:
    The actual state to pass to wrap_tool_call wrappers.
r?  r@  rJ   )rZ   r[   r\   )rN   r   s     rE   r   ToolNode._extract_state  s1     eT""uyy':>V'V>!rD   c           	        U R                   US      nU(       a  [        U[        5      (       a  [        UR                  5       5      n[	        U5      S:X  a  US   U R
                  :X  d  US   c  U R
                  U0nOZSUS    S3n[        S UR                  5        5       5      (       a!  SR                  S U 5       5      nUS	U S
3-  n[        U5      e[        U[        5      (       a0  UR                  5        VVs0 s H  u  pxXx(       a  X(   OU_M     n	nnO6UR                  5        VVs0 s H  u  pxXx(       a  [        X(5      OU_M     n	nn0 US   EU	EUS'   U$ s  snnf s  snnf )Nr   r   r   z Invalid input to ToolNode. Tool z$ requires graph state dict as input.c              3  $   #    U  H  ov   M     g 7fro   r;   )r]   state_fields     rE   r_   )ToolNode._inject_state.<locals>.<genexpr>  s     J6I{{6Is   rL  c              3  6   #    U  H  o(       d  M  Uv   M     g 7fro   r;   )r]   fs     rE   r_   rW    s     3T!RSAAs   
	z State should contain fields rm   r   )r   rZ   rc   r   r   r   r  rw   r   r[   r   rD  )
rN   r:   rJ   
state_argsrequired_fieldserr_msgrequired_fields_strtool_argrV  tool_state_argss
             rE   _inject_stateToolNode._inject_state  s   
 --i.?@
*UD11":#4#4#67OO$)oa.@DDVDV.V #+++U3 7y7H6I J1 2  Jj6G6G6IJJJ*.))3T3T*T'!>?R>SSTUUG ))eT"" .8-=-=-?-?)H %,F-?  O .8-=-=-?-?)H '%5%O-?  



	& 
s   E"1 E(c                v    U R                   US      nU(       d  U$ Uc  Sn[        U5      e0 US   EX20EUS'   U$ )Nr   zgCannot inject store into tools with InjectedStore annotations - please compile your graph with a store.r   )r   r   )rN   r:   r   	store_argrr   s        rE   _inject_storeToolNode._inject_store  sd    ++If,=>	=:  S/!


	& rD   c                n    U R                   R                  US   5      nU(       d  U$ 0 US   EX20EUS'   U$ )zInject ToolRuntime into tool call arguments.

Args:
    tool_call: The tool call to inject runtime into.
    tool_runtime: The ToolRuntime instance to inject.

Returns:
    The tool call with runtime injected if needed.
r   r   )r   r\   )rN   r:   r   runtime_args       rE   _inject_runtimeToolNode._inject_runtime  sQ     //33If4EF


	& rD   c                    US   U R                   ;  a  U$ [        U5      nU R                  X2R                  5      nU R	                  XBR
                  5      nU R                  XR5      $ )as  Inject graph state, store, and runtime into tool call arguments.

This is an internal method that enables tools to access graph context that
should not be controlled by the model. Tools can declare dependencies on graph
state, persistent storage, or runtime context using InjectedState, InjectedStore,
and ToolRuntime annotations. This method automatically identifies these
dependencies and injects the appropriate values.

The injection process preserves the original tool call structure while adding
the necessary context arguments. This allows tools to be both model-callable
and context-aware without exposing internal state management to the model.

Args:
    tool_call: The tool call dictionary to augment with injected arguments.
        Must contain 'name', 'args', 'id', and 'type' fields.
    tool_runtime: The ToolRuntime instance containing all runtime context
        (state, config, store, context, stream_writer) to inject into tools.

Returns:
    A new ToolCall dictionary with the same structure as the input but with
    additional arguments injected based on the tool's annotation requirements.

Raises:
    ValueError: If a tool requires store injection but no store is provided,
        or if state injection requirements cannot be satisfied.

!!! note
    This method is called automatically during tool execution. It should not
    be called from outside the `ToolNode`.
r   )r   r   r`  rJ   rd  r   rh  )rN   r:   r   tool_call_copytool_call_with_statetool_call_with_stores         rE   r  ToolNode._inject_tool_args  sl    F VD$6$66#'	?#11.BTBTU#11 "4"4 
 ##$8GGrD   c                .   [        UR                  [        5      (       a{  US;  a.  SU R                   SUR                   SUS    S3n[	        U5      e[        U5      n[        SUR                  5      =(       d    0 nUR                  U R                  / 5      nO`[        UR                  [        5      (       a?  US:w  a!  S	UR                   SUS    S3n[	        U5      e[        U5      nUR                  nOU$ [        U5      nU[        [        S
9/:X  a  U$ SnU H;  n	[        U	[        5      (       d  M  U	R                  US   :X  d  M/  US   U	l        SnM=     UR                  c+  U(       d$  US:X  a  SOSn
SUS    SU SU
 S3n[	        U5      eU$ )N)r[   r   zFTools can provide a dict in Command.update only when using dict with 'z' key as ToolNode input, got: z for tool 'r   'r   rc   zpTools can provide a list of messages in Command.update only when using list of messages as ToolNode input, got: )r   Fr   Tr[   z]`Command(update={"messages": [ToolMessage("Success", tool_call_id=tool_call_id), ...]}, ...)`zO`Command(update=[ToolMessage("Success", tool_call_id=tool_call_id), ...], ...)`zDExpected to have a matching ToolMessage in Command.update for tool 'z', got: z. Every tool call (LLM requesting to call a tool) in the message history MUST have a corresponding ToolMessage. You can fix it by modifying the tool to return rm   )rZ   updater[   r   r   r   r   r\   rc   r   r   r(   r   r   r   r  )rN   commandr   r   rr   updated_commandstate_updatemessages_updatehas_matching_tool_messager{   example_updates              rE   r  ToolNode._validate_tool_command$  s    gnnd++ !77!//0 1#NN+;tF|nAG 
 !o%&w/O 0/2H2HIORL*..t/A/A2FO-- V##NN+;tF|nAG 
 !o%&w/O-44ON .o> }0CDEE""$)!&Gg{33##tDz1#F|,0) '   (1J 'SR	 !&\N(?2C DB CQAQQR	T  S/!rD   )r   r   r   r   r   r   r   r   )r   zSequence[BaseTool | Callable]r   rb   r   zlist[str] | Noner   Obool | str | Callable[..., str] | type[Exception] | tuple[type[Exception], ...]r   rb   r   zToolCallWrapper | Noner   zAsyncToolCallWrapper | NonerR   r   )rR   zdict[str, BaseTool])r   -list[AnyMessage] | dict[str, Any] | BaseModelr   r   rL   r2   rR   r   )r   zlist[ToolMessage | Command]r   %Literal['list', 'dict', 'tool_calls']rR   z@list[Command | list[ToolMessage] | dict[str, list[ToolMessage]]])r  rH   r   r{  r   r   rR   r.  )r   r   r   r{  r   rK   rR   r.  )r   rz  rR   z<tuple[list[ToolCall], Literal['list', 'dict', 'tool_calls']])r   r   rR   zToolMessage | None)r   rz  rR   rz  )r:   r   rJ   rz  rR   r   )r:   r   r   BaseStore | NonerR   r   )r:   r   r   rK   rR   r   )rr  r*   r   r   r   r{  rR   r*   )r=   r>   r?   r@   rA   r   rB   r   r   propertyr   r   r   r   r!  r   r3  r   r   r  r   r`  rd  rh  r  r  rC   r   r   s   @rE   r   r     s   dL D# !%
 )D&157;.L,.L 	.L
 .L&.L .L /.L 5.L 
.L .L` # #?<? ? 	?
 
?@?<? ? 	?
 
?>* ,*  :*  
J	* Xd d :d 	d
 
dL55 :5 "	5
 
5nd d :d 	d
 
dL== := "	=
 
=~*&<*& 
F*&XB	6&& =& 
	&P$!1<	,+H+H "+H 
	+HZFF F :	F
 
F FrD   r   c                >   [        U [        5      (       a  U S   nOW[        U [        5      (       a  U R                  U/ 5      =n(       d  [	        X/ 5      =n(       a  US   nOSU  3n[        U5      e[        US5      (       a  [        UR                  5      S:  a  gg)a  Conditional routing function for tool-calling workflows.

This utility function implements the standard conditional logic for ReAct-style
agents: if the last AI message contains tool calls, route to the tool execution
node; otherwise, end the workflow. This pattern is fundamental to most tool-calling
agent architectures.

The function handles multiple state formats commonly used in LangGraph applications,
making it flexible for different graph designs while maintaining consistent behavior.

Args:
    state: The current graph state to examine for tool calls. Supported formats:
        - Dictionary containing a messages key (for StateGraph)
        - BaseModel instance with a messages attribute
    messages_key: The key or attribute name containing the message list in the state.
        This allows customization for graphs using different state schemas.
        Defaults to "messages".

Returns:
    Either "tools" if tool calls are present in the last AI message, or "__end__"
    to terminate the workflow. These are the standard routing destinations for
    tool-calling conditional edges.

Raises:
    ValueError: If no messages can be found in the provided state format.

Example:
    Basic usage in a ReAct agent:

    ```python
    from langgraph.graph import StateGraph
    from langchain.tools import ToolNode
    from langchain.tools.tool_node import tools_condition
    from typing_extensions import TypedDict


    class State(TypedDict):
        messages: list


    graph = StateGraph(State)
    graph.add_node("llm", call_model)
    graph.add_node("tools", ToolNode([my_tool]))
    graph.add_conditional_edges(
        "llm",
        tools_condition,  # Routes to "tools" or "__end__"
        {"tools": "tools", "__end__": "__end__"},
    )
    ```

    Custom messages key:

    ```python
    def custom_condition(state):
        return tools_condition(state, messages_key="chat_history")
    ```

!!! note
    This function is designed to work seamlessly with `ToolNode` and standard
    LangGraph patterns. It expects the last message to be an `AIMessage` when
    tool calls are present, which is the standard output format for tool-calling
    language models.
r>  z/No messages found in input state to tool_edge: r   r   r   __end__)	rZ   rc   r[   r\   rD  r   hasattrr   r   )rJ   r   
ai_messager   rr   s        rE   tools_conditionr  m  s    F %2Y

UD
!
!599\23N'Nx'NE444b\
?wGoz<((S1F1F-G!-KrD   c                  V    \ rS rSr% SrS\S'   S\S'   S\S'   S	\S
'   S\S'   S\S'   Srg)rK   i  a*  Runtime context automatically injected into tools.

When a tool function has a parameter named `tool_runtime` with type hint
`ToolRuntime`, the tool execution system will automatically inject an instance
containing:

- `state`: The current graph state
- `tool_call_id`: The ID of the current tool call
- `config`: `RunnableConfig` for the current execution
- `context`: Runtime context (from langgraph `Runtime`)
- `store`: `BaseStore` instance for persistent storage (from langgraph `Runtime`)
- `stream_writer`: `StreamWriter` for streaming output (from langgraph `Runtime`)

No `Annotated` wrapper is needed - just use `runtime: ToolRuntime`
as a parameter.

Example:
    ```python
    from langchain_core.tools import tool
    from langchain.tools import ToolRuntime

    @tool
    def my_tool(x: int, runtime: ToolRuntime) -> str:
        """Tool that accesses runtime context."""
        # Access state
        messages = tool_runtime.state["messages"]

        # Access tool_call_id
        print(f"Tool call ID: {tool_runtime.tool_call_id}")

        # Access config
        print(f"Run ID: {tool_runtime.config.get('run_id')}")

        # Access runtime context
        user_id = tool_runtime.context.get("user_id")

        # Access store
        tool_runtime.store.put(("metrics",), "count", 1)

        # Stream output
        tool_runtime.stream_writer.write("Processing...")

        return f"Processed {x}"
    ```

!!! note
    This is a marker class used for type checking and detection.
    The actual runtime object will be constructed during tool execution.
r4   rJ   r6   r   r   r   r,   r   
str | Noner   r|  r   r;   Nr<   r;   rD   rE   rK   rK     s-    0d MrD   rK   c                  &    \ rS rSrSrSSS jjrSrg)InjectedStatei  a%	  Annotation for injecting graph state into tool arguments.

This annotation enables tools to access graph state without exposing state
management details to the language model. Tools annotated with `InjectedState`
receive state data automatically during execution while remaining invisible
to the model's tool-calling interface.

Args:
    field: Optional key to extract from the state dictionary. If `None`, the entire
        state is injected. If specified, only that field's value is injected.
        This allows tools to request specific state components rather than
        processing the full state structure.

Example:
    ```python
    from typing import List
    from typing_extensions import Annotated, TypedDict

    from langchain_core.messages import BaseMessage, AIMessage
    from langchain.tools import InjectedState, ToolNode, tool


    class AgentState(TypedDict):
        messages: List[BaseMessage]
        foo: str


    @tool
    def state_tool(x: int, state: Annotated[dict, InjectedState]) -> str:
        '''Do something with state.'''
        if len(state["messages"]) > 2:
            return state["foo"] + str(x)
        else:
            return "not enough messages"


    @tool
    def foo_tool(x: int, foo: Annotated[str, InjectedState("foo")]) -> str:
        '''Do something else with state.'''
        return foo + str(x + 1)


    node = ToolNode([state_tool, foo_tool])

    tool_call1 = {"name": "state_tool", "args": {"x": 1}, "id": "1", "type": "tool_call"}
    tool_call2 = {"name": "foo_tool", "args": {"x": 1}, "id": "2", "type": "tool_call"}
    state = {
        "messages": [AIMessage("", tool_calls=[tool_call1, tool_call2])],
        "foo": "bar",
    }
    node.invoke(state)
    ```

    ```python
    [
        ToolMessage(content="not enough messages", name="state_tool", tool_call_id="1"),
        ToolMessage(content="bar2", name="foo_tool", tool_call_id="2"),
    ]
    ```

!!! note
    - `InjectedState` arguments are automatically excluded from tool schemas
        presented to language models
    - `ToolNode` handles the injection process during execution
    - Tools can mix regular arguments (controlled by the model) with injected
        arguments (controlled by the system)
    - State injection occurs after the model generates tool calls but before
        tool execution
Nc                    Xl         g)z*Initialize the `InjectedState` annotation.Nfield)rN   r  s     rE   r   InjectedState.__init__A  s    
rD   r  ro   )r  r  rR   r   )r=   r>   r?   r@   rA   r   rC   r;   rD   rE   r  r    s    DL rD   r  c                      \ rS rSrSrSrg)InjectedStoreiF  a	  Annotation for injecting persistent store into tool arguments.

This annotation enables tools to access LangGraph's persistent storage system
without exposing storage details to the language model. Tools annotated with
InjectedStore receive the store instance automatically during execution while
remaining invisible to the model's tool-calling interface.

The store provides persistent, cross-session data storage that tools can use
for maintaining context, user preferences, or any other data that needs to
persist beyond individual workflow executions.

!!! warning
    `InjectedStore` annotation requires `langchain-core >= 0.3.8`

Example:
    ```python
    from typing_extensions import Annotated
    from langgraph.store.memory import InMemoryStore
    from langchain.tools import InjectedStore, ToolNode, tool

    @tool
    def save_preference(
        key: str,
        value: str,
        store: Annotated[Any, InjectedStore()]
    ) -> str:
        """Save user preference to persistent storage."""
        store.put(("preferences",), key, value)
        return f"Saved {key} = {value}"

    @tool
    def get_preference(
        key: str,
        store: Annotated[Any, InjectedStore()]
    ) -> str:
        """Retrieve user preference from persistent storage."""
        result = store.get(("preferences",), key)
        return result.value if result else "Not found"
    ```

    Usage with `ToolNode` and graph compilation:

    ```python
    from langgraph.graph import StateGraph
    from langgraph.store.memory import InMemoryStore

    store = InMemoryStore()
    tool_node = ToolNode([save_preference, get_preference])

    graph = StateGraph(State)
    graph.add_node("tools", tool_node)
    compiled_graph = graph.compile(store=store)  # Store is injected automatically
    ```

    Cross-session persistence:

    ```python
    # First session
    result1 = graph.invoke({"messages": [HumanMessage("Save my favorite color as blue")]})

    # Later session - data persists
    result2 = graph.invoke({"messages": [HumanMessage("What's my favorite color?")]})
    ```

!!! note
    - `InjectedStore` arguments are automatically excluded from tool schemas
        presented to language models
    - The store instance is automatically injected by `ToolNode` during execution
    - Tools can access namespaced storage using the store's get/put methods
    - Store injection requires the graph to be compiled with a store instance
    - Multiple tools can share the same store instance for data consistency
r;   N)r=   r>   r?   r@   rA   rC   r;   rD   rE   r  r  F  s    GrD   r  c                   ^ [        U T5      (       d&  [        U [        5      (       a  [        U T5      (       a  g[        U 5      nU[        L d	  U[
        L a  [        U4S j[        U 5       5       5      $ g)a  Check if a type argument represents an injection annotation.

This utility function determines whether a type annotation indicates that
an argument should be injected with state or store data. It handles both
direct annotations and nested annotations within Union or Annotated types.

Args:
    type_arg: The type argument to check for injection annotations.
    injection_type: The injection type to look for (InjectedState or InjectedStore).

Returns:
    True if the type argument contains the specified injection annotation.
Tc              3  <   >#    U  H  n[        UT5      v   M     g 7fro   )_is_injection)r]   tainjection_types     rE   r_    _is_injection.<locals>.<genexpr>  s     R?Q=^44?Qs   F)rZ   rY   r   r   r   r   r  r   )type_argr  origin_s    ` rE   r  r    s`    " (N++8T""z(N'K'K"G%7i/Rx?QRRRrD   c                   U R                  5       n0 n[        U5      R                  5        H  u  p4[        U5       Vs/ s H  n[	        U[
        5      (       d  M  UPM     nn[        U5      S:  a  SU SU S3n[        U5      e[        U5      S:X  aA  US   n[        U[
        5      (       a!  UR                  (       a  UR                  X#'   M  SX#'   M  M     U$ s  snf )a  Extract state injection mappings from tool annotations.

This function analyzes a tool's input schema to identify arguments that should
be injected with graph state. It processes InjectedState annotations to build
a mapping of tool argument names to state field names.

Args:
    tool: The tool to analyze for state injection requirements.

Returns:
    A dictionary mapping tool argument names to state field names. If a field
    name is None, the entire state should be injected for that argument.
r   zXA tool argument should not be annotated with InjectedState more than once. Received arg  with annotations rm   r   N)
get_input_schemar%   r   r   r  r  r   r   rZ   r  )	r!   full_schematool_args_to_state_fieldsr   type_r  
injectionsrr   	injections	            rE   r   r     s     '')K&(4[AGGI %UO
+X}5 + 	 

 z?Q&&*V+=j\L  S/!z?a"1I)]33	2;//)/26)/' J( %$'
s   CCc                B   U R                  5       n[        U5      R                  5        Hm  u  p#[        U5       Vs/ s H  n[	        U[
        5      (       d  M  UPM     nn[        U5      S:  a  SU SU S3n[        U5      e[        U5      S:X  d  Mk  Us  $    gs  snf )a  Extract store injection argument from tool annotations.

This function analyzes a tool's input schema to identify the argument that
should be injected with the graph store. Only one store argument is supported
per tool.

Args:
    tool: The tool to analyze for store injection requirements.

Returns:
    The name of the argument that should receive the store injection, or None
    if no store injection is required.

Raises:
    ValueError: If a tool argument has multiple InjectedStore annotations.
r   zXA tool argument should not be annotated with InjectedStore more than once. Received arg r  rm   N)r  r%   r   r   r  r  r   r   r!   r  r   r  r  r  rr   s          rE   r   r     s    " '')K4[AGGI %UO
+X}5 + 	 

 z?Q&&*V+=j\L  S/!z?aK J 
s   BBc                   U R                  5       n[        U5      R                  5        H  u  p#US:X  a  Us  $ [        U[        5      (       a  Us  $ [        U5       Vs/ s H  n[        U[        5      (       d  M  UPM     nn[        U5      S:  a  SU SU S3n[        U5      e[        U5      S:X  d  M  Us  $    gs  snf )a  Extract runtime injection argument from tool annotations.

This function analyzes a tool's input schema to identify the argument that
should be injected with the ToolRuntime instance. Only one runtime argument
is supported per tool.

Args:
    tool: The tool to analyze for runtime injection requirements.

Returns:
    The name of the argument that should receive the runtime injection, or None
    if no runtime injection is required.

Raises:
    ValueError: If a tool argument has multiple ToolRuntime annotations.
rL   r   zVA tool argument should not be annotated with ToolRuntime more than once. Received arg r  rm   N)r  r%   r   r  rK   r   r   r   r  s          rE   r   r     s    " '')K4[AGGI9K,,K %UO
+X{3 + 	 

 z?Q&&*V+=j\L  S/!z?aK) J, 
s   B?<B?)rh   r   rR   zstr | list[dict])r   rg   rR   rb   )r   rg   r   ry  rR   rb   )r   zCallable[..., str]rR   ztuple[type[Exception], ...])
r   r.   r   dict[str, str | None]r   r  r   r  rR   zlist[ErrorDetails])r   )rJ   rz  r   rb   rR   zLiteral['tools', '__end__'])r  r   r  z1type[InjectedState | InjectedStore | ToolRuntime]rR   r   )r!   r   rR   r  )r!   r   rR   r  )crA   
__future__r   r   r   re   collections.abcr   r   r   r   dataclassesr   r	   typesr
   typingr   r   r   r   r   r   r   r   r   r   r   langchain_core.messagesr   r   r   r   r   r   langchain_core.runnables.configr   r   r   langchain_core.toolsr   r    r!   r   langchain_core.tools.baser"   r#   r$   r%   langgraph._internal._runnabler&   langgraph.errorsr'   langgraph.graph.messager(   langgraph.store.baser)   langgraph.typesr*   r+   r,   pydanticr-   r.   typing_extensionsr/   r0   r1   langgraph.runtimer2   pydantic_corer3   r[   r4   r6   rO  r   TOOL_EXECUTION_ERROR_TEMPLATEry   r8   rH   r;  AsyncToolCallWrapperrT   ri   rk   r   r   r   r   r   r  rK   r  r  r  r   r   r   r;   rD   rE   <module>r     s  "H #    /  *      
 ; 4  ; * 7 * 7 7 / -()*
 
4	(:t, S ! H + + 	  +* +* +*\ h0+2GGHI'FP  h0)K'<Q2RRSTkG#$&  E7) 7.<)'- )'X//"/ 	/d?D4%4-4 "4 $	4
 4nV Vv #N8NN !Nb 8*GHf4D,E 8 8vIO IXHO HVE 
6%%P!H(rD   