
    9il             	         d Z ddlmZ ddlZddlZddlZddlmZmZ ddl	m	Z	m
Z
 ddlmZmZ ddlmZ ddlmZmZmZmZmZmZmZmZmZmZmZ dd	lmZmZmZm Z m!Z!m"Z" dd
l#m$Z$m%Z%m&Z& ddl'm(Z(m)Z) ddl'm*Z+ ddl,m-Z-m.Z.m/Z/m0Z0 ddl1m2Z2 ddl3m4Z4 ddl5m6Z6 ddl7m8Z8 ddl9m:Z:m;Z;m<Z< ddl=m>Z>m?Z? ddl@mAZAmBZB erddlmCZC ddlDmEZE ddlFmGZG  eAdeH      ZI eAdd      ZJdZKdZLdZMdZN G d d ed!"      ZOe G d# d$             ZPeePeePge!e:z  f   ge!e:z  f   ZQ	 eePeePgee!e:z     f   gee!e:z     f   ZR	  G d% d&e      ZSd<d'ZT G d( d)e.      ZUd=d*ZV	 	 	 	 	 	 d>d+ZWd?d,ZX	 	 	 	 	 	 d@d-ZYe G d. d/             ZZ G d0 d1e2      Z[	 dA	 	 	 	 	 dBd2Z\e G d3 d4e/eeJeIf                Z] G d5 d6e)      Z^ G d7 d8e)      Z_	 	 	 	 	 	 dCd9Z`	 	 	 	 	 	 dDd:ZadEd;Zby)Fa  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                      e Zd ZU dZded<   y)_ToolCallRequestOverridesz9Possible overrides for ToolCallRequest.override() method.r   	tool_callN__name__
__module____qualname____doc____annotations__     f/var/www/html/backtest/airagagent/rag_env/lib/python3.12/site-packages/langgraph/prebuilt/tool_node.pyr8   r8   x   s    CrA   r8   F)totalc                  R    e Zd ZU dZded<   ded<   ded<   ded	<   dd
Z	 	 	 	 ddZy)ToolCallRequestaf  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   r9   zBaseTool | Noner!   r   stateToolRuntimeruntimec                    ddl }t        | d      rt        | |      st        j                  | ||       y|j	                  d| dt
        d       t        j                  | ||       y)zRaise deprecation warning when setting attributes directly.

        Direct attribute assignment is deprecated. Use the `override()` method instead.
        r   N__dataclass_fields__zSetting attribute 'zt' on ToolCallRequest is deprecated. Use the override() method instead to create a new instance with modified values.   )
stacklevel)warningshasattrobject__setattr__warnDeprecationWarning)selfnamevaluerM   s       rB   rP   zToolCallRequest.__setattr__   sm    
 	 t34GD$<OtT51MM%dV ,c c"	   tT51rA   c                    t        | fi |S )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	   )rS   	overridess     rB   overridezToolCallRequest.override   s    2 t)y))rA   N)rT   strrU   r   returnNone)rW   z!Unpack[_ToolCallRequestOverrides]rZ   rE   )r;   r<   r=   r>   r?   rP   rX   r@   rA   rB   rE   rE   ~   s:    
 
J2&*<*	*rA   rE   c                  2    e Zd ZU dZded<   ded<   	 ded<   y)	ToolCallWithContexta  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   r9   z!Literal['tool_call_with_context']_ToolCallWithContext__typer   rF   Nr:   r@   rA   rB   r]   r]     s$    	 --
 J6rA   r]   c                    t        | t              s"t        | t              rt        d | D              r| S 	 t	        j
                  | d      S # t        $ r t        |       cY S w xY w)aX  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  n   K   | ]-  }t        |t              xr |j                  d       t        v  / yw)typeN)
isinstancedictgetr"   ).0xs     rB   	<genexpr>z%msg_content_output.<locals>.<genexpr>9  s5      
 q$MAEE&M5M$MM
s   35F)ensure_ascii)rb   rY   listalljsondumps	Exception)outputs    rB   msg_content_outputro   +  sc     &#64  

 

 
zz&u55 6{s   A A$#A$c                  :     e Zd ZdZ	 d	 	 	 	 	 	 	 	 	 d fdZ xZS )ToolInvocationErrorzAn error occurred while invoking a tool due to invalid arguments.

    This exception is only raised when invoking a tool using the `ToolNode`!
    c                   |kg }|D ]R  }dj                  d |j                  dd      D              }|j                  dd      }|j                  | d|        T d	j                  |      }	nt        |      }	t        j                  |||	
      | _        || _        || _        || _	        || _
        t        
| 1  | j                         y)ai  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  2   K   | ]  }t        |        y wN)rY   )re   locs     rB   rg   z/ToolInvocationError.__init__.<locals>.<genexpr>d  s     "L3s8"Ls   rv   r@   msgzUnknown errorz: 
)	tool_nametool_kwargserror)joinrd   appendrY   TOOL_INVOCATION_ERROR_TEMPLATEformatmessagery   rz   sourcefiltered_errorssuper__init__)rS   ry   r   rz   r   error_str_partsr{   loc_strrw   error_display_str	__class__s             rB   r   zToolInvocationError.__init__O  s    " & O( <(("Luyy7K"LLii7&&'"SE':;< !%		/ : #F5<<[@Q = 
 #&.&rA   ru   )
ry   rY   r   r.   rz   dict[str, Any]r   zlist[ErrorDetails] | NonerZ   r[   )r;   r<   r=   r>   r   __classcell__r   s   @rB   rq   rq   I  sG     6:#'#'  #' $	#'
 3#' 
#' #'rA   rq   c                >    t        | t              r| j                  S | )zDefault error handler for tool errors.

    If the tool is a tool invocation error, return its message.
    Otherwise, raise the error.
    )rb   rq   r   )es    rB   _default_handle_tool_errorsr   u  s     !()yy
GrA   c               "   t        |t        t        f      s t        |t              r1t	        |t
              r!t        j                  t        |             }|S t        |t              r|}|S t        |      r
 ||       }|S d| }t        |      )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.
    )r{   zVGot unexpected type of `handle_tool_error`. Expected bool, str or callable. Received: )rb   booltuplera   
issubclassrm   TOOL_CALL_ERROR_TEMPLATEr   reprrY   callable
ValueError)r   flagcontentrw   s       rB   _handle_tool_errorr     s    B $u&4:dI#>*11Q1@ N 
D#	 N 
$q' N	&&*V- 	 orA   c                T   t        j                  |       }t        |j                  j	                               }|r|d   j
                  dv rt        |      dk(  r|d   }n|d   }t        |       }|j
                  |v rt        |j                        }|t        t        fv rMt        |j                        }t        d |D              rt        |      S d|j                   d}t        |      ||j
                     }t         |j"                  v r|fS d| d}t        |      t         fS )	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   )rS   clsrK      c              3  <   K   | ]  }t        |t                y wru   )r   rm   )re   args     rB   rg   z'_infer_handled_types.<locals>.<genexpr>  s     Bcz#y1B   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	signatureri   
parametersvaluesrT   lenr   r   
annotationr   r
   r   rj   r   r   rm   __mro__)	handlersigparamsfirst_param
type_hintsoriginargsrw   exception_types	            rB   _infer_handled_typesr     s9   0 

G
$C#..'')*F!9>>_,V1A )K )K#G,
z) 6 67F%++ 6 67BTBB ;& (223:?  !o%'(8(89NN222&((
 ''z3  S/! <rA   c                L   t               }|r|j                  r)|j                  |j                  j                                |j                  r|j                  |j                         |j                  r|j                  |j                         g }| j                         D ]y  }|d   s	|d   d   |vsi |}t        |j                  d      t              r2|d   }|j                         D ci c]  \  }}||vs|| }	}}|	|d<   |j                  |       { |S c c}}w )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.
        injected_args: The _InjectedArgs structure containing all injected arguments,
            or None if there are no injected arguments.

    Returns:
        List of ErrorDetails containing only errors for LLM-controlled arguments,
        with system-injected argument values removed from the input field.
    rv   r   input)setrF   updatekeysstoreaddrH   errorsrb   rd   rc   itemsr}   )
validation_errorinjected_argsinjected_arg_namesr   r{   
error_copy
input_dictkv
input_copys
             rB   _filter_validation_errorsr     s)   2 $'5%%m&9&9&>&>&@A""=#6#67  ""=#8#89*,O!((* / <E%LO3EE)2EJ *..148'0
%/%5%5%7!Q1DV;VAqD
  '1
7# "":.!/$ s   1D >D c                  0    e Zd ZU dZded<   ded<   ded<   y)_InjectedArgsaS  Internal structure for tracking injected arguments for a tool.

    This data structure is built once during ToolNode initialization by analyzing
    the tool's signature and args schema, then reused during execution for efficient
    injection without repeated reflection.

    The structure maps from tool parameter names to their injection sources, enabling
    the ToolNode to know exactly which arguments need to be injected and where to
    get their values from.

    Attributes:
        state: Mapping from tool parameter names to state field names for injection.
            Keys are tool parameter names, values are either:
            - str: Name of the state field to extract and inject
            - None: Inject the entire state object
            Empty dict if no state injection is needed.
        store: Name of the tool parameter where the store should be injected,
            or None if no store injection is needed.
        runtime: Name of the tool parameter where the runtime should be injected,
            or None if no runtime injection is needed.

    Example:
        For a tool with signature:
        ```python
        def my_tool(
            x: int,
            messages: Annotated[list, InjectedState("messages")],
            full_state: Annotated[dict, InjectedState()],
            store: Annotated[BaseStore, InjectedStore()],
            runtime: ToolRuntime,
        ) -> str:
            ...
        ```

        The resulting `_InjectedArgs` would be:
        ```python
        _InjectedArgs(
            state={
                "messages": "messages",  # Extract state["messages"]
                "full_state": None,      # Inject entire state
            },
            store="store",               # Inject into "store" parameter
            runtime="runtime",           # Inject into "runtime" parameter
        )
        ```
    zdict[str, str | None]rF   
str | Noner   rH   Nr:   r@   rA   rB   r   r   ,  s    -^ ! rA   r   c                  l    e Zd ZU dZdZded<   ddedddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fdZedd	       Z		 	 	 	 	 	 	 	 dd
Z
	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZ	 	 	 	 ddZddZ	 	 	 	 ddZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 d dZ xZS )!ToolNodeu"  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
        - `Command` 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.
        tags: Optional metadata tags to associate with the node for filtering
            and organization.
        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.

            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)
        ```
    toolsrY   rT   Nmessages)rT   tagshandle_tool_errorsmessages_keywrap_tool_callawrap_tool_callc               ~   t         
|   | j                  | j                  ||d       i | _        i | _        || _        || _        || _        || _	        |D ]e  }t        |t              st        t        d|            }	n|}	|	| j                  |	j                  <   t        |	      | j
                  |	j                  <   g y)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)rT   r   traceztype[BaseTool]N)r   r   _func_afunc_tools_by_name_injected_args_handle_tool_errors_messages_key_wrap_tool_call_awrap_tool_callrb   r   create_toolr   rT   _get_all_injected_args)rS   r   rT   r   r   r   r   r   r!   tool_r   s             rB   r   zToolNode.__init__  s    : 	T[[t$eT358:#5 )- / 	LDdH-#D)94$@A.3D

+.DU.KD

+	LrA   c                    | j                   S )z,Mapping from tool name to BaseTool instance.)r   )rS   s    rB   tools_by_namezToolNode.tools_by_name   s     """rA   c           	        | j                  |      \  }}t        |t        |            }g }t        ||d      D ]Y  \  }}	| j	                  |      }
t        |
|d   |	|j                  |j                  |j                        }|j                  |       [ |gt        |      z  }t        |      5 }t        |j                  | j                  |||            }d d d        | j                  |      S # 1 sw Y   xY wNF)strictid)rF   tool_call_idconfigcontextr   stream_writer)_parse_inputr   r   zip_extract_staterG   r   r   r   r}   r   ri   map_run_one_combine_tool_outputs)rS   r   r   rH   
tool_calls
input_typeconfig_listtool_runtimescallcfgrF   tool_runtimeinput_typesexecutoroutputss                  rB   r   zToolNode._func  s     "&!2!25!9
J%fc*o> ZUC 
	/ID#''.E&!$Zmm%33L   .
	/ "lS_4$V, 	T]]J]SG	
 ))':>>	 	s   0(C22C;c           	       K   | j                  |      \  }}t        |t        |            }g }t        ||d      D ]Y  \  }}	| j	                  |      }
t        |
|d   |	|j                  |j                  |j                        }|j                  |       [ g }t        ||d      D ]'  \  }}|j                  | j                  |||             ) t        j                  |  d {   }| j                  ||      S 7 wr   )r   r   r   r   r   rG   r   r   r   r}   	_arun_oneasynciogatherr   )rS   r   r   rH   r   r   r   r   r   r   rF   r   corosr   s                 rB   r   zToolNode._afunc%  s     "&!2!25!9
J%fc*o> ZUC 
	/ID#''.E&!$Zmm%33L   .
	/ "%j-"N 	ID,LLj,GH	I..))':>> /s   C%D 'C>(D c                r   t        d |D              s|dk(  r|S | j                  |iS g }d }|D ]  }t        |t              r|j                  t        j
                  u rt        |j                  t              rst        d |j                  D              rW|r/t        |t        d|j                        |j                  z         }t        t        j
                  |j                        }|j                  |       |j                  |dk(  r|gn| j                  |gi        |r|j                  |       |S )Nc              3  <   K   | ]  }t        |t                y wru   )rb   r*   )re   rn   s     rB   rg   z1ToolNode._combine_tool_outputs.<locals>.<genexpr>K  s     E6:fg.Er   ri   c              3  <   K   | ]  }t        |t                y wru   )rb   r+   )re   sends     rB   rg   z1ToolNode._combine_tool_outputs.<locals>.<genexpr>\  s     KtJtT2Kr   z
list[Send])goto)graphr   )anyr   rb   r*   r   PARENTr   ri   rj   r	   r   r}   )rS   r   r   combined_outputsparent_commandrn   s         rB   r   zToolNode._combine_tool_outputsD  s    EWEE(F27U9K9KW8UU  	
 *. 	F&'*LLGNN2"6;;5Kv{{KK%)0*!%lN4G4G!H6;;!V*
 *1w~~FKK)X$++F3 '' *f 4VH4;M;MPVx:X#	* ##N3rA   c                   |j                   }|j                  }|)| j                  |      x}r|S d|d    d}t        |      | j	                  ||j
                        }i |ddi}		 	 |j                  |	|      }
	 t        |
t.              r| j1                  |
|j                   |      S t        |
t,              r&t3        dt5        |
j6                              |
_        |
S d|d    dt!        |
       }t        |      # t        $ rD}| j                  j                  |d         }t        ||      }t        |d   ||d   |      |d}~ww xY w# t        $ r  t        $ r}t        | j                  t               r(t#        | j                  t              r| j                  f}nst        | j                  t$              r| j                  }nLt'        | j                        r0t        | j                  t               st)        | j                        }nt        f}| j                  rt        ||      s t+        || j                        }t-        ||d   |d	   d
      cY d}~S d}~ww xY w)aU  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 rT     is not registered with ToolNodera   r9   r   r   r   r{   r   rT   r   status
str | list returned unexpected type: )r9   r!   _validate_tool_call	TypeError_inject_tool_argsrH   invoker.   r   rd   r   rq   r'   rm   rb   r   ra   r   r   r   r   r   r   r*   _validate_tool_commandr   ro   r   rS   requestr   r   r   r!   invalid_tool_messagerw   injected_call	call_argsresponseexcinjectedr   r   handled_typesr   s                    rB   _execute_tool_synczToolNode._execute_tool_syncp  s`   &   || <'+'?'?'EE#E++$v,'GHCC.  ..tW__E:}:fk:	3		;;y&9h h(..x9J9JJWWh,#L2DXEUEU2VWHOd6l^#>tH~>NOnw # ..224<@";C"J)L#tF|_$  	 	 $22D9j(()? "&!9!9 ;D44e< $ 8 8$223J(($= !5T5M5M N "+ ++:a3O )1I1IJG&\!$Z	 /	s7   *C? ?	E?EEE I&"C9I!I&!I&c                   	  j                   j                  |d         }t        |||j                  |      }|j                  	 j
                   j                  |	      S d		 fd}	  j                  ||      S # t        $ rW} j                  s t        | j                        }t        ||j                  d   |j                  d   d      cY d}~S d}~ww xY w)
a  Execute 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.
        rT   r9   r!   rF   rH   Nc                *    j                  |       S )>Execute tool with given request. Can be called multiple times.r  reqr   r   rS   s    rB   executez"ToolNode._run_one.<locals>.execute      **3
FCCrA   r  r   r{   r  r!  rE   rZ   ToolMessage | Command)r   rd   rE   rF   r   r   r  rm   r   r   r   r9   )
rS   r   r   r   r!   tool_requestr"  r   r   r   s
   ` `      @rB   r   zToolNode._run_one  s    $ !!%%d6l3 '$$ 	
 $$'**<VLL	D
	''g>> 	++(1I1IJG!++F3)33D9	 	s   /B 	C!
ACC!C!c                  K   |j                   }|j                  }|)| j                  |      x}r|S d|d    d}t        |      | j	                  ||j
                        }i |ddi}		 	 |j                  |	|       d{   }
	 t        |
t.              r| j1                  |
|j                   |      S t        |
t,              r&t3        dt5        |
j6                              |
_        |
S d|d    dt!        |
       }t        |      7 # t        $ rD}| j                  j                  |d         }t        ||      }t        |d   ||d   |      |d}~ww xY w# t        $ r  t        $ r}t        | j                  t               r(t#        | j                  t              r| j                  f}nst        | j                  t$              r| j                  }nLt'        | j                        r0t        | j                  t               st)        | j                        }nt        f}| j                  rt        ||      s t+        || j                        }t-        ||d   |d	   d
      cY d}~S d}~ww xY ww)ad  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  rT   r  ra   r9   r   r  r   r{   r  r
  r  )r9   r!   r  r  r  rH   ainvoker.   r   rd   r   rq   r'   rm   rb   r   ra   r   r   r   r   r   r   r*   r  r   ro   r   r  s                    rB   _execute_tool_asynczToolNode._execute_tool_async	  sj    &   || <'+'?'?'EE#E++$v,'GHCC.  ..tW__E:}:fk:	3		!%i!@@h h(..x9J9JJWWh,#L2DXEUEU2VWHOd6l^#>tH~>NOny A" ..224<@";C"J)L#tF|_$  	 	 $22D9j(()? "&!9!9 ;D44e< $ 8 8$223J(($= !5T5M5M N "+ ++:a3O )1I1IJG&\!$Z	 /	sb   A(I5,D D	D BI5	D 	E?EEE I2.C9I-'I2(I5-I22I5c                   
K    j                   j                  |d         }t        |||j                  |      }|j                  
 j
                  ' j                   j                  |
       d{   S d
 fd}d
 fd}	  j
                   j                  ||       d{   S t        d j                         _         j                  ||      S 7 h7 3# t        $ rW} j                  s t        | j                        }	t        |	|j                  d   |j                  d   d	
      cY d}~S d}~ww xY ww)a  Execute 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.
        rT   r  Nc                F   K   j                  |        d{   S 7 w)r  N)r)  r   s    rB   r"  z#ToolNode._arun_one.<locals>.execute  s"     11#z6JJJJs   !!c                *    j                  |       S )z'Sync execute fallback for sync wrapper.r  r   s    rB   _sync_executez)ToolNode._arun_one.<locals>._sync_execute  r#  rA   ToolCallWrapperr  r   r{   r  r$  )r   rd   rE   rF   r   r   r   r)  r   rm   r   r   r   r9   )rS   r   r   r   r!   r&  r"  r-  r   r   r   s   ` `       @rB   r   zToolNode._arun_onek  sD    $ !!%%d6l3 '$$ 	
 $$  (T-A-A-I11,
FSSS	K	D
	$$0!22<III#'(94;O;O#PD ''mDD# T J  	++(1I1IJG!++F3)33D9	 	sa   A2E7C 8E!C$ .C"/C$ 2E3,C$ E"C$ $	E-AD?9E:E?EEc                j   t        |t              rAt        |d   t              r)|d   j                  d      dk(  rd}t	        d|      }||fS d}|}nt        |t              r*|j                  d      dk(  rt	        d	|      }d}|d   g|fS t        |t              r!|j                  | j
                  g       x}rd
}n)t        || j
                  g       x}rd
}nd}t        |      	 t        d t        |      D              }t        |j                        }||fS # t        $ r d}t        |      w xY w)Nra   r9   r   zlist[ToolCall]ri   __typetool_call_with_contextr]   rc   zNo message found in inputc              3  B   K   | ]  }t        |t              s|  y wru   )rb   r   )re   ms     rB   rg   z(ToolNode._parse_input.<locals>.<genexpr>  s      %Ay1I%s   zNo AIMessage found in input)rb   ri   rc   rd   r   r   getattrr   nextreversedStopIterationr   )rS   r   r   r   r   input_with_ctxrw   latest_ai_messages           rB   r   zToolNode._parse_input  sR   
 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   'D D2c                    |d   }|| j                   vr[t        | j                   j                               }t        j	                  |dj                  |            }t        |||d   d      S y )NrT   , )requested_toolavailable_toolsr   r{   )rT   r   r	  )r   ri   r    INVALID_TOOL_NAME_ERROR_TEMPLATEr   r|   r   )rS   r   r=  all_tool_namesr   s        rB   r  zToolNode._validate_tool_call  sw    f!3!33!$"4"4"9"9";<N6==- $		. 9 > G n4:g  rA   c                X    t        |t              r|j                  d      dk(  r|d   S |S )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.
        r1  r2  rF   )rb   rc   rd   )rS   r   s     rB   r   zToolNode._extract_state  s.     eT"uyy':>V'V>!rA   c                   |d   | j                   vr|S | j                  j                  |d         }|s|S t        |      }i }|j                  rF|j                  }t        |t              rt        |j                  j                               }t        |      dk(  r|d   | j                  k(  s|d   | j                  |i}n_d|d    d}t        d |j                  j                         D              r!dj                  d |D              }	|d	|	 d
z  }t        |      t        |t              r/|j                  j                         D ]  \  }
}|r||   n|||
<    n5|j                  j                         D ]  \  }
}|rt        ||      n|||
<    |j                   r2|j                   d}t        |      |j                   ||j                   <   |j"                  r|||j"                  <   i |d   ||d<   |S )a+  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`.
        rT   r   r   z Invalid input to ToolNode. Tool z$ requires graph state dict as input.c              3      K   | ]  }|  y wru   r@   )re   state_fields     rB   rg   z-ToolNode._inject_tool_args.<locals>.<genexpr>/  s     R;;Rs   r<  c              3  &   K   | ]	  }|s|  y wru   r@   )re   fs     rB   rg   z-ToolNode._inject_tool_args.<locals>.<genexpr>0  s     7XaVW7Xs   z State should contain fields rs   zgCannot inject store into tools with InjectedStore annotations - please compile your graph with a store.r   )r   r   rd   r   rF   rb   ri   r   r   r   r   r|   r   rc   r   r5  r   rH   )rS   r9   r   r  tool_call_copyr   rF   required_fieldserr_msgrequired_fields_strtool_argrD  rw   s                rB   r  zToolNode._inject_tool_args  s.   F VD$6$66&&**9V+<=#'	? >> &&E%&"&x~~'<'<'>"?(A-'*d.@.@@$Q'/!//7E ;9V;L:M N5 6  R(..:O:O:QRR.2ii7X?7X.X+;<O;PPQR %W-- %&-5^^-A-A-C )Hk.9k*u "(+
 .6^^-A-A-C )Hk7B{3 "(+ >>!!)>  !o%,8,>,>M(..) .:M(**+!LN6$:!Lm!LvrA   c                   t        |j                  t              rt|dvr.d| j                   d|j                   d|d    d}t	        |      t        |      }t        d|j                        xs i }|j                  | j                  g       }nZt        |j                  t              r>|dk7  r!d	|j                   d|d    d}t	        |      t        |      }|j                  }n|S t        |      }|t        t        
      gk(  r|S d}|D ]2  }	t        |	t              s|	j                  |d   k(  s'|d   |	_        d}4 |j                  %|s#|dk(  rdnd}
d|d    d| d|
 d}t	        |      |S )N)rc   r   zFTools can provide a dict in Command.update only when using dict with 'z' key as ToolNode input, got: z for tool 'rT   'r   ri   zpTools can provide a list of messages in Command.update only when using list of messages as ToolNode input, got: )r   Fr   Trc   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 rs   )rb   r   rc   r   r   r   r   rd   ri   r   r   r(   r   r   rT   r   )rS   commandr   r   rw   updated_commandstate_updatemessages_updatehas_matching_tool_messager   example_updates              rB   r  zToolNode._validate_tool_commandS  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""$)!& 	1Gg{3##tDz1#F|,0)	1   (1J 'SR	 !&\N(?2C DB CQAQQR	T  S/!rA   )r   zSequence[BaseTool | Callable]rT   rY   r   zlist[str] | Noner   Obool | str | Callable[..., str] | type[Exception] | tuple[type[Exception], ...]r   rY   r   zToolCallWrapper | Noner   zAsyncToolCallWrapper | NonerZ   r[   )rZ   zdict[str, BaseTool])r   -list[AnyMessage] | dict[str, Any] | BaseModelr   r   rH   r2   rZ   r   )r   zlist[ToolMessage | Command]r   %Literal['list', 'dict', 'tool_calls']rZ   z@list[Command | list[ToolMessage] | dict[str, list[ToolMessage]]])r  rE   r   rV  r   r   rZ   r%  )r   r   r   rV  r   rG   rZ   r%  )r   rU  rZ   z<tuple[list[ToolCall], Literal['list', 'dict', 'tool_calls']])r   r   rZ   zToolMessage | None)r   rU  rZ   rU  )r9   r   r   rG   rZ   r   )rN  r*   r   r   r   rV  rZ   r*   )r;   r<   r=   r>   rT   r?   r   r   propertyr   r   r   r   r  r   r)  r   r   r  r   r  r  r   r   s   @rB   r   r   b  s[   l\ D# !%
 )D&157;+L,+L 	+L
 +L&+L +L /+L 5+L 
+LZ # #?<? ? 	?
 
?@?<? ? 	?
 
?>* ,*  :*  
J	* X` ` :` 	`
 
`D55 :5 "	5
 
5n` ` :` 	`
 
`D== := "	=
 
=~*&<*& 
F*&XB	6__ "_ 
	_BFF F :	F
 
FrA   r   c                   t        | t              r| d   }nIt        | t              r| j                  |g       x}st	        | |g       x}r|d   }nd|  }t        |      t        |d      rt        |j                        dkD  ryy)a=	  Conditional routing function for tool-calling workflows.

    This utility function implements the standard conditional logic for ReAct-style
    agents: if the last `AIMessage` 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.

    Returns:
        Either `'tools'` if tool calls are present in the last `AIMessage`, 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.
    r0  z/No messages found in input state to tool_edge: r   r   r   __end__)	rb   ri   rc   rd   r5  r   rN   r   r   )rF   r   
ai_messager   rw   s        rB   tools_conditionr[    s    D %2Y

UD
!599\23N'Nx'NE<444b\
?wGoz<(S1F1F-G!-KrA   c                  N    e Zd ZU dZded<   ded<   ded<   ded	<   d
ed<   ded<   y)rG   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   rF   r6   r   r   r   r,   r   r   r   zBaseStore | Noner   Nr:   r@   rA   rB   rG   rG     s-    0d MrA   rG   c                      e Zd ZdZdddZy)InjectedStatea	  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                    || _         y)z*Initialize the `InjectedState` annotation.N)field)rS   r`  s     rB   r   zInjectedState.__init__o  s	    
rA   ru   )r`  r   rZ   r[   )r;   r<   r=   r>   r   r@   rA   rB   r^  r^  (  s    DLrA   r^  c                      e Zd ZdZy)InjectedStorea
  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
    N)r;   r<   r=   r>   r@   rA   rB   rb  rb  t  s    GrA   rb  c                    t        |       st        | t              rt        |       ryt        |       }|t        u s|t
        u rt        fdt        |       D              S y)a5  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  6   K   | ]  }t        |        y wru   )_is_injection)re   tainjection_types     rB   rg   z _is_injection.<locals>.<genexpr>  s     R=^4Rs   F)rb   ra   r   r   r   r   r   r   )type_argrg  origin_s    ` rB   re  re    sW    " (N+8T"z(N'K"G%7i/Rx?QRRRrA   c                    t        |       }|D cg c]  }t        ||      s| }}t        |      dkD  rd|j                   d| }t	        |      t        |      dk(  r|d   S t        | |      ryyc c}w )a  Extract injection instance from a type annotation.

    Args:
        type_: The type annotation to check.
        injection_type: The injection type to look for.

    Returns:
        The injection instance if found, True if injection marker found without instance, None otherwise.
    r   z-A tool argument should not be annotated with z more than once. Found: r   TN)r   re  r   r;   r   )type_rg  	type_argsr   matchesrw   s         rB   _get_injection_from_typern    s     I'Ns=n+MsNGN
7|a;N<S<S;T U&&-Y0 	 o
7|qqz	un	- Os
   A4A4c                   | j                         }t        |      }t        | dd      xs t        | dd      }|rt        |d      ni }i ||}i }d}d}|j	                         D ]t  \  }	}
|	dk(  r|	}t        |
t              x}r1t        |t              r|j                  r|j                  ||	<   nd||	<   t        |
t              r|	}t        |
t              ss|	}v t        |||      S )ay  Extract all injected arguments from tool in a single pass.

    This function analyzes both the tool's input schema and function signature
    to identify all arguments that should be injected (state, store, runtime).

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

    Returns:
        _InjectedArgs structure containing all detected injections.
    funcN	coroutineT)include_extrasrH   )rF   r   rH   )get_input_schemar%   r5  r   r   rn  r^  rb   r`  rb  rG   r   )r!   full_schemaschema_annotationsrp  func_annotationsall_annotations
state_args	store_argruntime_argrT   rk  	state_injs               rB   r   r     s    '')K6{C4&J'$T*JDDH~d4@b A)@-?@O )+J I"K&,,. e9K 1FF9F)]3	#,??
4 #'
4  $E=9I $E;7K%(  rA   )rn   r   rZ   zstr | list[dict])r   rm   rZ   rY   )r   rm   r   rT  rZ   rY   )r   zCallable[..., str]rZ   ztuple[type[Exception], ...])r   r.   r   z_InjectedArgs | NonerZ   zlist[ErrorDetails])r   )rF   rU  r   rY   rZ   zLiteral['tools', '__end__'])rh  r   rg  1type[InjectedState | InjectedStore | ToolRuntime]rZ   r   )rk  r   rg  r|  rZ   z
Any | None)r!   r   rZ   r   )cr>   
__future__r   r   r   rk   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   rc   r4   r6   r?  r   TOOL_EXECUTION_ERROR_TEMPLATEr~   r8   rE   r.  AsyncToolCallWrapperr]   ro   rq   r   r   r   r   r   r   r[  rG   r^  rb  re  rn  r   r@   rA   rB   <module>r     s  %N #    /  *      
 ; 4  ; * 7 * 7 7 / -()*
 
4	(:t, S ! H + + 	  >* >* >*B h0+2GGHI'GR  h0)K'<Q2RRSTkG#$&  E7) 7.<)'- )'X//"/ 	/d?D5%5'5 5p 2 2 2jw wx #M8MM !M` 8*GHf4D,E 8 8vIO IXHO HVE 
6 Q<4rA   