Ë
    uPêj  ã                   ó€   — d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ d	efd
efd„Z G d„ de	«      Zy)zE
Module containing "timeout" decorator for sync and async callables.
é    N)Úfutures)Úwraps)Úiscoroutinefunction)ÚThread)ÚFinal)ÚTimeoutg        Útimeout_durationc                 ó   ‡ ‡— ˆˆ fd„}|S )aL  
    Wraps a function to raise the specified exception if execution time
    is greater than the specified timeout.

    Works with both synchronous and asynchronous callables, but with synchronous ones will introduce
    some overhead due to the backend use of threads and asyncio.

        :param float timeout_duration: Timeout duration in seconds. If none callable won't time out.
        :param OpenAIError exception_to_raise: Exception to raise when the callable times out.
            Defaults to TimeoutError.
        :return: The decorated function.
        :rtype: callable
    c                 óx   •‡ — t        ‰ «      ˆˆ ˆfd„«       }t        ‰ «      ˆˆ ˆfd„«       }t        ‰ «      r|S |S )Nc                  ó¸  •‡ ‡— ˆ ˆ	ˆfd„}t        «       }|j                  «        t        j                   |«       |j                  «      }‰
}d‰v r‰d   �‰d   }nd‰v r
‰d   �‰d   }	 |j                  |¬«      }|j                  «        |S # t        j                  $ r8 |j                  «        t        ‰ «      dkD  r‰ d   n‰d   } ‰d|› d�|d	¬
«      ‚w xY w)Nc               “   ó   •K  —  ‰‰ i ‰¤ŽS ­w©N© )ÚargsÚfuncÚkwargss   €€€úT/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/timeout.pyÚ
async_funcz?timeout.<locals>.decorator.<locals>.wrapper.<locals>.async_func*   s   øè ø€ Ù˜TÐ, VÑ,Ð,ùs   ƒ	Úforce_timeoutÚrequest_timeout©Útimeoutr   Úmodelú=A timeout error occurred. The function call took longer than ú second(s).Úopenai©r   Úllm_provider)
Ú_LoopWrapperÚstartÚasyncioÚrun_coroutine_threadsafeÚloopÚresultr   ÚTimeoutErrorÚ	stop_loopÚlen)r   r   r   ÚthreadÚfutureÚlocal_timeout_durationr$   r   Úexception_to_raiser   r	   s   ``      €€€r   Úwrapperz+timeout.<locals>.decorator.<locals>.wrapper(   sû   ú€ ö-ô )›NˆFØ�L‰LŒNÜ#×<Ñ<¹Z»\È6Ï;É;ÓWˆFØ%5Ð"Ø &Ñ(¨V°OÑ-DÐ-PØ)/°Ñ)@Ñ&Ø" fÑ,°Ð8IÑ1JÐ1VØ)/Ð0AÑ)BÐ&ð	Ø &§¡Ð6L Ó M�ð ×ÑÔØˆMøô ×'Ñ'ò Ø× Ñ Ô"Ü*-¨d«)°aª-˜t Ašw¸VÀG¹_�Ù(ØSÐTjÐSkÐkvÐwØØ!)ôð ðús   Á*B ÂACc               Ÿ   ó  •K  — ‰}d|v r|d   }nd|v r
|d   �|d   }	 t        j                   ‰| i |¤Ž‰¬«      ƒ d {  –—† }|S 7 Œ# t         j                  $ r( t        | «      dkD  r| d   n|d   } ‰d|› d�|d¬	«      ‚w xY w­w)
Nr   r   r   r   r   r   r   r   r   )r!   Úwait_forr%   r'   )r   r   r*   Úvaluer   r+   r   r	   s        €€€r   Úasync_wrapperz1timeout.<locals>.decorator.<locals>.async_wrapperB   s¾   øè ø€ à%5Ð"Ø &Ñ(Ø)/°Ñ)@Ñ&Ø" fÑ,°Ð8IÑ1JÐ1VØ)/Ð0AÑ)BÐ&ð	Ü%,×%5Ñ%5±d¸DÐ6KÀFÑ6KÐUeÔ%f×f�Ø�ð  gùä×'Ñ'ò Ü*-¨d«)°aª-˜t Ašw¸VÀG¹_�Ù(ØSÐTjÐSkÐkvÐwØØ!)ôð ðüs-   ƒBŸ A ¿AÁ A ÁBÁA Á;BÂB)r   r   )r   r,   r0   r+   r	   s   `  €€r   Ú	decoratorztimeout.<locals>.decorator'   sF   ù€ Ü	ˆt‹õ	ó 
ð	ô2 
ˆt‹õ	ó 
ð	ô" ˜tÔ$Ø Ð Øˆó    r   )r	   r+   r1   s   `` r   r   r      s   ù€ õ/ðb Ðr2   c                   ó0   ‡ — e Zd ZdZˆ fd„Zdd„Zd„ Zˆ xZS )r   a  Daemon thread that owns a dedicated asyncio event loop.

    Used by the sync branch of :func:`timeout` to run a coroutine on a
    background event loop so the calling thread can wait on it with a
    timeout via :func:`asyncio.run_coroutine_threadsafe`.
    c                 óX   •— t         ‰| �  d¬«       t        j                  «       | _        y )NT)Údaemon)ÚsuperÚ__init__r!   Únew_event_loopr#   )ÚselfÚ	__class__s    €r   r7   z_LoopWrapper.__init__c   s#   ø€ Ü‰Ñ ÐÔ%Ü×*Ñ*Ó,ˆ�	r2   c                 ó|  — 	 | j                   j                  «        | j                   j                  | j                   j                  «       | j                   j                  «        t        j                  d «       y # t        $ r Y Œ;w xY w# | j                   j                  «        t        j                  d «       w xY wr   )r#   Úrun_foreverÚcall_soon_threadsafeÚcloseÚ	Exceptionr!   Úset_event_loop)r9   s    r   Úrunz_LoopWrapper.rung   s‚   € ð	)Ø�I‰I×!Ñ!Ô#Ø�I‰I×*Ñ*¨4¯9©9¯?©?Ô;ð
 �I‰I�O‰OÔÜ×"Ñ" 4Õ(øô ò 	áð	ûð �I‰I�O‰OÔÜ×"Ñ" 4Õ(ús$   ‚A	A; Á;	BÂB
 ÂBÂB
 Â
1B;c                 óÊ   — t        j                  | j                  «      D ]  }|j                  «        Œ | j                  j	                  | j                  j
                  «       y r   )r!   Ú	all_tasksr#   Úcancelr=   Ústop)r9   Útasks     r   r&   z_LoopWrapper.stop_loopr   sC   € Ü×%Ñ% d§i¡iÓ0ò 	ˆDØ�K‰K�Mð	à�	‰	×&Ñ& t§y¡y§~¡~Õ6r2   )ÚreturnN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r7   rA   r&   Ú__classcell__)r:   s   @r   r   r   [   s   ø„ ñô-ó	)ö7r2   r   )rK   r!   Ú
concurrentr   Ú	functoolsr   Úinspectr   Ú	threadingr   Útypingr   Úlitellm.exceptionsr   Úfloatr   r   r   r2   r   ú<module>rT      sC   ðñó Ý Ý Ý 'Ý Ý å &ð '*¸gñ @˜eó @ôF7�6õ 7r2   