§
    œŠtjü  ã                  ó²   — 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 )z¨Shared 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        ú`/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/langchain/agents/middleware/_retry.pyÚvalidate_retry_paramsr   %   sx   € ð" �Q‚€Ø(ˆÝ˜‰oŒoÐØ�qÒÐØ*ˆÝ˜‰oŒoÐØ�1‚}€}Ø&ˆÝ˜‰oŒoÐØ˜ÒÐØ+ˆÝ˜‰oŒoÐð Ðó    Ú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   D   s2   € õ �ÑÔð Øˆx˜‰}Œ}ÐÝ�c˜8Ñ$Ô$Ð$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(   W   sy   € ð0 ˜ÒÐØˆˆà °Ñ!=Ñ>ˆõ ��yÑ!Ô!€Eàð �%˜!’)�)Ø ™ˆØ•”  °Ñ>Ô>Ñ>ˆå�A�u‘”ˆà€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” Ð$Ô
%¨°)°¸dÐ1BÔ(CÑ
C€ðð Ð'Ô(¨8°Y°KÀÐ4DÔ+EÑE€	ððð ð ð ð>%ð %ð %ð %ð&&ð &ð &ð &ð &ð &r   