
    OKi                        S r SSKJr  SSKrSSKJr  SSKJr  \\	\
   S4   \\
/\4   -  r \S   \\
/\4   -  r           SS jr      SS	 jr            SS
 jrg)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continuec                    U S:  a  Sn[        U5      eUS:  a  Sn[        U5      eUS:  a  Sn[        U5      eUS:  a  Sn[        U5      eg)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)max_retriesinitial_delay	max_delaybackoff_factormsgs        d/var/www/html/dynamic-report/venv/lib/python3.13/site-packages/langchain/agents/middleware/_retry.pyvalidate_retry_paramsr   #   sc    " Q(oq*o1}&o+o     c                H    [        U5      (       a  U" U 5      $ [        X5      $ )a>  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)excretry_ons     r   should_retry_exceptionr   B   s$     }c$$r   c                   US:X  a  UnOX!U -  -  n[        XS5      nU(       a2  US:  a,  US-  nU[        R                  " U* U5      -  n[        SU5      nU$ )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)retry_numberr   r   r   jitterdelayjitter_amounts          r   calculate_delayr!   U   sc    0 !=> !E%!)>>AuLr   )
r
   intr   floatr   r#   r   r#   returnNone)r   	Exceptionr   RetryOnr$   bool)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   