
    g3fi                        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y)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                    | dk  rd}t        |      |dk  rd}t        |      |dk  rd}t        |      |dk  rd}t        |      y)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        `/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain/agents/middleware/_retry.pyvalidate_retry_paramsr   #   sc    " Q(oq*o1}&o+o     c                @    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)excretry_ons     r   should_retry_exceptionr   B   s#     }c8$$r   c                   |dk(  r|}n||| z  z  }t        ||      }|r0|dkD  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)retry_numberr   r   r   jitterdelayjitter_amounts          r   calculate_delayr!   U   sg    0 !=> y!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   