
    g3fiI                    8   d Z ddlmZ ddlmZmZmZmZmZ ddl	m
Z
mZmZ ddlmZ ddlmZ ddlmZmZ ddlmZmZmZmZmZ erdd	lmZ ed
   Z	  G d dee   ee         ZddZ	 	 	 	 	 	 	 	 	 	 	 	 ddZ G d de       Z! G d deee   ef   eeef         Z"y)z&Tool call limit middleware for agents.    )annotations)TYPE_CHECKING	AnnotatedAnyGenericLiteral)	AIMessageToolCallToolMessage)UntrackedValue)ContextT)NotRequiredoverride)AgentMiddleware
AgentStatePrivateStateAttr	ResponseThook_config)Runtimecontinueerrorendc                  &    e Zd ZU dZded<   ded<   y)ToolCallLimitStatea]  State schema for `ToolCallLimitMiddleware`.

    Extends `AgentState` with tool call tracking fields.

    The count fields are dictionaries mapping tool names to execution counts. This
    allows multiple middleware instances to track different tools independently. The
    special key `'__all__'` is used for tracking all tool calls globally.
    z8NotRequired[Annotated[dict[str, int], PrivateStateAttr]]thread_tool_call_countzHNotRequired[Annotated[dict[str, int], UntrackedValue, PrivateStateAttr]]run_tool_call_countN)__name__
__module____qualname____doc____annotations__     i/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain/agents/middleware/tool_call_limit.pyr   r   #   s     UTaar$   r   c                    | rd|  dS y)a  Build the error message content for `ToolMessage` when limit is exceeded.

    This message is sent to the model, so it should not reference thread/run concepts
    that the model has no notion of.

    Args:
        tool_name: Tool name being limited (if specific tool), or `None` for all tools.

    Returns:
        A concise message instructing the model not to call the tool again.
    z'Tool call limit exceeded. Do not call 'z' again.z<Tool call limit exceeded. Do not make additional tool calls.r#   	tool_names    r%   _build_tool_message_contentr)   1   s     88LLIr$   c                    |rd| dnd}g }|| |kD  r|j                  d|  d| d       |||kD  r|j                  d| d| d       dj                  |      }| d	| d
S )a;  Build the final AI message content for `'end'` behavior.

    This message is displayed to the user, so it should include detailed information
    about which limits were exceeded.

    Args:
        thread_count: Current thread tool call count.
        run_count: Current run tool call count.
        thread_limit: Thread tool call limit (if set).
        run_limit: Run tool call limit (if set).
        tool_name: Tool name being limited (if specific tool), or `None` for all tools.

    Returns:
        A formatted message describing which limits were exceeded.
    'z' toolToolzthread limit exceeded (/z calls)zrun limit exceeded (z and z call limit reached: .)appendjoin)thread_count	run_countthread_limit	run_limitr(   	tool_descexceeded_limitslimits_texts           r%   _build_final_ai_message_contentr8   C   s    , *3!I;f%IOL<$?!8a~U\]^Y!6!5i[)GTU,,/K[-k]!<<r$   c                  >     e Zd ZdZ	 d	 	 	 	 	 	 	 	 	 	 	 d fdZ xZS )ToolCallLimitExceededErrorzException raised when tool call limits are exceeded.

    This exception is raised when the configured exit behavior is `'error'` and either
    the thread or run tool call limit has been exceeded.
    c                    || _         || _        || _        || _        || _        t        |||||      }t        |   |       y)a  Initialize the exception with call count information.

        Args:
            thread_count: Current thread tool call count.
            run_count: Current run tool call count.
            thread_limit: Thread tool call limit (if set).
            run_limit: Run tool call limit (if set).
            tool_name: Tool name being limited (if specific tool), or None for all tools.
        N)r1   r2   r3   r4   r(   r8   super__init__)selfr1   r2   r3   r4   r(   msg	__class__s          r%   r=   z#ToolCallLimitExceededError.__init__l   sN    " )"(""-)\9i
 	r$   )N)r1   intr2   rA   r3   
int | Noner4   rB   r(   
str | NonereturnNone)r   r   r    r!   r=   __classcell__r@   s   @r%   r:   r:   e   sQ     !%  !	
   
 r$   r:   c                       e Zd ZdZeZddddd	 	 	 	 	 	 	 	 	 d fdZedd       ZddZ	ddZ
	 	 	 	 	 	 	 	 dd	Z ed
g      e	 	 	 	 	 	 dd              Z ed
g      	 	 	 	 	 	 dd       Z xZS )ToolCallLimitMiddlewarea  Track tool call counts and enforces limits during agent execution.

    This middleware monitors the number of tool calls made and can terminate or
    restrict execution when limits are exceeded. It supports both thread-level
    (persistent across runs) and run-level (per invocation) call counting.

    Configuration:
        - `exit_behavior`: How to handle when limits are exceeded
            - `'continue'`: Block exceeded tools, let execution continue (default)
            - `'error'`: Raise an exception
            - `'end'`: Stop immediately with a `ToolMessage` + AI message for the single
                tool call that exceeded the limit (raises `NotImplementedError` if there
                are other pending tool calls (due to parallel tool calling).

    Examples:
        !!! example "Continue execution with blocked tools (default)"

            ```python
            from langchain.agents.middleware.tool_call_limit import ToolCallLimitMiddleware
            from langchain.agents import create_agent

            # Block exceeded tools but let other tools and model continue
            limiter = ToolCallLimitMiddleware(
                thread_limit=20,
                run_limit=10,
                exit_behavior="continue",  # default
            )

            agent = create_agent("openai:gpt-4o", middleware=[limiter])
            ```

        !!! example "Stop immediately when limit exceeded"

            ```python
            # End execution immediately with an AI message
            limiter = ToolCallLimitMiddleware(run_limit=5, exit_behavior="end")

            agent = create_agent("openai:gpt-4o", middleware=[limiter])
            ```

        !!! example "Raise exception on limit"

            ```python
            # Strict limit with exception handling
            limiter = ToolCallLimitMiddleware(
                tool_name="search", thread_limit=5, exit_behavior="error"
            )

            agent = create_agent("openai:gpt-4o", middleware=[limiter])

            try:
                result = await agent.invoke({"messages": [HumanMessage("Task")]})
            except ToolCallLimitExceededError as e:
                print(f"Search limit exceeded: {e}")
            ```

    Nr   )r(   r3   r4   exit_behaviorc                   t         |           ||d}t        |      d}||vrd|d| }t        |      ||||kD  rd| d| d}t        |      || _        || _        || _        || _        y)	a  Initialize the tool call limit middleware.

        Args:
            tool_name: Name of the specific tool to limit. If `None`, limits apply
                to all tools.
            thread_limit: Maximum number of tool calls allowed per thread.
                `None` means no limit.
            run_limit: Maximum number of tool calls allowed per run.
                `None` means no limit.
            exit_behavior: How to handle when limits are exceeded.

                - `'continue'`: Block exceeded tools with error messages, let other
                    tools continue. Model decides when to end.
                - `'error'`: Raise a `ToolCallLimitExceededError` exception
                - `'end'`: Stop execution immediately with a `ToolMessage` + AI message
                    for the single tool call that exceeded the limit. Raises
                    `NotImplementedError` if there are multiple parallel tool
                    calls to other tools or multiple pending tool calls.

        Raises:
            ValueError: If both limits are `None`, if `exit_behavior` is invalid,
                or if `run_limit` exceeds `thread_limit`.
        Nz@At least one limit must be specified (thread_limit or run_limit)r   zInvalid exit_behavior: z. Must be one of zrun_limit (z) cannot exceed thread_limit (zB). The run limit should be less than or equal to the thread limit.)r<   r=   
ValueErrorr(   r3   r4   rJ   )r>   r(   r3   r4   rJ   r?   valid_behaviorsr@   s          r%   r=   z ToolCallLimitMiddleware.__init__   s    > 	I$5TCS/!6/+M+<<MoM^_CS/!#	(=)lBZi[(F|n UR R  S/!"("*r$   c                n    | j                   j                  }| j                  r| d| j                   dS |S )zThe name of the middleware instance.

        Includes the tool name if specified to allow multiple instances
        of this middleware with different tool names.
        [])r@   r   r(   )r>   	base_names     r%   namezToolCallLimitMiddleware.name   s8     NN++	>>[$..!133r$   c                    | j                   duxr |dz   | j                   kD  xs" | j                  duxr |dz   | j                  kD  S )a  Check if incrementing the counts would exceed any configured limit.

        Args:
            thread_count: Current thread call count.
            run_count: Current run call count.

        Returns:
            True if either limit would be exceeded by one more call.
        N   )r3   r4   )r>   r1   r2   s      r%   _would_exceed_limitz+ToolCallLimitMiddleware._would_exceed_limit  sO     !!-V,2BTEVEV2V 
NN$&I9q=4>>+I	
r$   c                F    | j                   du xs |d   | j                   k(  S )zCheck if a tool call matches this middleware's tool filter.

        Args:
            tool_call: The tool call to check.

        Returns:
            True if this middleware should track this tool call.
        NrR   r'   )r>   	tool_calls     r%   _matches_tool_filterz,ToolCallLimitMiddleware._matches_tool_filter  s&     ~~%L6):dnn)LLr$   c                    g }g }|}|}|D ]S  }| j                  |      s| j                  ||      r|j                  |       9|j                  |       |dz  }|dz  }U ||||fS )ao  Separate tool calls into allowed and blocked based on limits.

        Args:
            tool_calls: List of tool calls to evaluate.
            thread_count: Current thread call count.
            run_count: Current run call count.

        Returns:
            Tuple of `(allowed_calls, blocked_calls, final_thread_count,
                final_run_count)`.
        rT   )rX   rU   r/   )	r>   
tool_callsr1   r2   allowed_callsblocked_callstemp_thread_counttemp_run_countrW   s	            r%   _separate_tool_callsz,ToolCallLimitMiddleware._separate_tool_calls$  s     )+(*("# 		$I,,Y7''(9>J$$Y/$$Y/!Q&!!#		$ m->NNr$   r   )can_jump_toc           
        |j                  dg       }|syd}t        |      D ]  }t        |t              s|} n |r|j                  sy| j
                  xs d}|j                  di       j                         }|j                  di       j                         }|j                  |d      }	|j                  |d      }
| j                  |j                  |	|
      \  }}}}|||<   |t        |      z   ||<   |s|r||dS y||   }||   }| j                  dk(  r<|t        |      z   }t        ||| j                  | j                  | j
                  	      t        | j
                        }|D cg c]#  }t        ||d
   |j                  d      d      % }}| j                  dk(  r|j                  D cg c]"  }| j
                  |d   | j
                  k7  r|$ }}|r3dj                  |D ch c]  }|d   	 c}      }d| d}t!        |      |t        |      z   }t#        ||| j                  | j                  | j
                        }|j%                  t        |             ||d|dS |||dS c c}w c c}w c c}w )a  Increment tool call counts after a model call and check limits.

        Args:
            state: The current agent state.
            runtime: The langgraph runtime.

        Returns:
            State updates with incremented tool call counts. If limits are exceeded
                and exit_behavior is `'end'`, also includes a jump to end with a
                `ToolMessage` and AI message for the single exceeded tool call.

        Raises:
            ToolCallLimitExceededError: If limits are exceeded and `exit_behavior`
                is `'error'`.
            NotImplementedError: If limits are exceeded, `exit_behavior` is `'end'`,
                and there are multiple tool calls.
        messagesN__all__r   r   r   )r   r   r   )r1   r2   r3   r4   r(   idrR   )contenttool_call_idrR   statusr   z, zDCannot end execution with other tool calls pending. Found calls to: z-. Use 'continue' or 'error' behavior instead.)re   )r   r   jump_torb   )r   r   rb   )getreversed
isinstancer	   rZ   r(   copyr_   lenrJ   r:   r3   r4   r)   r   r0   NotImplementedErrorr8   r/   )r>   stateruntimerb   last_ai_messagemessage	count_keythread_counts
run_countscurrent_thread_countcurrent_run_countr[   r\   new_thread_countnew_run_countfinal_thread_countfinal_run_counthypothetical_thread_counttool_msg_contentrW   artificial_messagestcother_tools
tool_namesr?   final_msg_contents                             r%   after_modelz#ToolCallLimitMiddleware.after_modelD  s   2 99Z, ) 	G'9-")	
 o&@&@ NN/i	 		":B?DDFYY4b9>>@
,00A>&NN9a8 IMHaHa&&(<>OI
E}&6 $4i  -M0B B
9 .;+5   +95$Y/ ((:S=O(O%,6)!......  7t~~F +>
  (&t_]]6*	>
 >
 & *44>>-"V*2N K  !YY['Ir6
'IJ
''1l2_a  *#..
 );S=O(O% ?)!!!  &&y9J'KL +8'1 /	  '4#-+
 	
_>
 (Js   /(I76'I<0Jc                .   K   | j                  ||      S w)a  Async increment tool call counts after a model call and check limits.

        Args:
            state: The current agent state.
            runtime: The langgraph runtime.

        Returns:
            State updates with incremented tool call counts. If limits are exceeded
                and exit_behavior is `'end'`, also includes a jump to end with a
                `ToolMessage` and AI message for the single exceeded tool call.

        Raises:
            ToolCallLimitExceededError: If limits are exceeded and `exit_behavior`
                is `'error'`.
            NotImplementedError: If limits are exceeded, `exit_behavior` is `'end'`,
                and there are multiple tool calls.
        )r   )r>   ro   rp   s      r%   aafter_modelz$ToolCallLimitMiddleware.aafter_model  s     . w//s   )
r(   rC   r3   rB   r4   rB   rJ   ExitBehaviorrD   rE   )rD   str)r1   rA   r2   rA   rD   bool)rW   r
   rD   r   )rZ   zlist[ToolCall]r1   rA   r2   rA   rD   z/tuple[list[ToolCall], list[ToolCall], int, int])ro   zToolCallLimitState[ResponseT]rp   zRuntime[ContextT]rD   zdict[str, Any] | None)r   r   r    r!   r   state_schemar=   propertyrR   rU   rX   r_   r   r   r   r   rF   rG   s   @r%   rI   rI      s   8t &L
 !%#' $&04+ 4+ !	4+
 4+ $4+ 
4+l 	 	
	MO(O8;OHKO	8O@ eW%I
,I
 #I
 
	I
  &I
V eW%0,0 #0 
	0 &0r$   rI   N)r(   rC   rD   r   )r1   rA   r2   rA   r3   rB   r4   rB   r(   rC   rD   r   )#r!   
__future__r   typingr   r   r   r   r   langchain_core.messagesr	   r
   r   "langgraph.channels.untracked_valuer   langgraph.typingr   typing_extensionsr   r   !langchain.agents.middleware.typesr   r   r   r   r   langgraph.runtimer   r   r   r)   r8   	Exceptionr:   rI   r#   r$   r%   <module>r      s    , " B B D D = % 3  )12bI.	0B bJ$=== = 	=
 = 	=D! !H_0&y18;<Ix _0r$   