
    
i                        d Z ddlmZ ddlZddlmZ ddlmZ ee	e
         df         ee
gef         z  Z	 ed         ee
gef         z  Z	 ddZddZddZdS )zShared retry utilities for agent middleware.

This module contains common constants, utilities, and logic used by both
model and tool retry middleware implementations.
    )annotationsN)Callable)Literal.)errorcontinuemax_retriesintinitial_delayfloat	max_delaybackoff_factorreturnNonec                    | dk     rd}t          |          |dk     rd}t          |          |dk     rd}t          |          |dk     rd}t          |          dS )an  Validate retry parameters.

    Args:
        max_retries: Maximum number of retry attempts.
        initial_delay: Initial delay in seconds before first retry.
        max_delay: Maximum delay in seconds between retries.
        backoff_factor: Multiplier for exponential backoff.

    Raises:
        ValueError: If any parameter is invalid (negative values).
    r   zmax_retries must be >= 0zinitial_delay must be >= 0zmax_delay must be >= 0zbackoff_factor must be >= 0N)
ValueError)r   r
   r   r   msgs        C:\Users\Dell Inspiron 16\Desktop\tws\AgrotaPowerBi\back-agrota-powerbi\mcp-client-agrota\venv\Lib\site-packages\langchain/agents/middleware/_retry.pyvalidate_retry_paramsr   #   sx    " Q(ooq*oo1}}&oo+oo     exc	Exceptionretry_onRetryOnboolc                V    t          |          r ||           S t          | |          S )aZ  Check if an exception should trigger a retry.

    Args:
        exc: The exception that occurred.
        retry_on: Either a tuple of exception types to retry on, or a callable
            that takes an exception and returns `True` if it should be retried.

    Returns:
        `True` if the exception should be retried, `False` otherwise.
    )callable
isinstance)r   r   s     r   should_retry_exceptionr   B   s2      x}}c8$$$r   retry_numberjitterc                   |dk    r|}n||| z  z  }t          ||          }|r4|dk    r.|dz  }|t          j        | |          z  }t          d|          }|S )a@  Calculate delay for a retry attempt with exponential backoff and optional jitter.

    Args:
        retry_number: The retry attempt number (0-indexed).
        backoff_factor: Multiplier for exponential backoff.

            Set to `0.0` for constant delay.
        initial_delay: Initial delay in seconds before first retry.
        max_delay: Maximum delay in seconds between retries.

            Caps exponential backoff growth.
        jitter: Whether to add random jitter to delay to avoid thundering herd.

    Returns:
        Delay in seconds before next retry.
    g        r   g      ?)minrandomuniformmax)r   r   r
   r   r    delayjitter_amounts          r   calculate_delayr(   U   sy    0 !=> y!!E %!))>>>AuLr   )
r   r	   r
   r   r   r   r   r   r   r   )r   r   r   r   r   r   )r   r	   r   r   r
   r   r   r   r    r   r   r   )__doc__
__future__r   r#   collections.abcr   typingr   tupletyper   r   r   str	OnFailurer   r   r(    r   r   <module>r2      s     # " " " " "  $ $ $ $ $ $       Y$
%)d1B(C
C '(8YK4D+EE		   >% % % %&& & & & & &r   