
    kKj	              
       :   d Z ddlZddlZddlmZmZ ddlmZmZm	Z	m
Z
 ddlZddlmZ  e	d      Zdedefd	Zded
ef   dededefdZe
dee   ded   deeez     fd       Ze
d
ddee   ded   dee   fd       Zdddee   dedee   eeez     z  fdZy)zAsync utilities for FastMCP.    N)	AwaitableCallable)AnyLiteralTypeVaroverload)run_syncTfnreturnc                     t        | t        j                        r'| j                  } t        | t        j                        r't	        j
                  |       S )a  Check if a callable is a coroutine function, unwrapping functools.partial.

    ``inspect.iscoroutinefunction`` returns ``False`` for
    ``functools.partial`` objects wrapping an async function on Python < 3.12.
    This helper unwraps any layers of ``partial`` before checking.
    )
isinstance	functoolspartialfuncinspectiscoroutinefunction)r   s    n/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/fastmcp/utilities/async_utils.pyis_coroutine_functionr      s@     R**
+WW R**
+&&r**    .argskwargsc                 b   K   t        t        j                  | g|i |       d{   S 7 w)zCall a sync function in a threadpool to avoid blocking the event loop.

    Uses anyio.to_thread.run_sync which properly propagates contextvars,
    making this safe for functions that depend on context (like dependency injection).
    N)run_sync_in_threadpoolr   r   )r   r   r   s      r   call_sync_fn_in_threadpoolr      s.      (	(9(9"(Nt(Nv(NOOOOs   &/-/
awaitablesreturn_exceptionsTc                    K   y wN r   r   s     r   gatherr"   %   s      "   )r   Fc                    K   y wr   r    r!   s     r   r"   r"   ,   s      r#   c                 B   K   dgt        |      z  dt        dt        t           ddf fd}t	        j
                         4 d{   }t        |      D ]  \  }}|j                  |||        ddd      d{    S 7 <7 # 1 d{  7  sw Y   S xY ww)a  Run awaitables concurrently and return results in order.

    Uses anyio TaskGroup for structured concurrency.

    Args:
        *awaitables: Awaitables to run concurrently
        return_exceptions: If True, exceptions are returned in results.
                          If False, first exception cancels all and raises.

    Returns:
        List of results in the same order as input awaitables.
    Niawr   c                 l   K   	 | d {   | <   y 7 # t         $ r}r|| <   n Y d }~y d }~ww xY wwr   )BaseException)r&   r'   eresultsr   s      r   run_atzgather.<locals>.run_atE   s<     	!GAJ 	 
 	s,   4  4 	1	,414)lenintr   r
   anyiocreate_task_group	enumerate
start_soon)r   r   r,   tgr&   r'   r+   s   `     @r   r"   r"   3   s       )-vJ'?G 1 $  &&((Bz*EArMM&!R( + )( N	 )(((( NsH   ABB	B'B	3B>B?BB	BBBB)__doc__r   r   collections.abcr   r   typingr   r   r   r   r/   anyio.to_threadr	   r   r
   boolr   r   listr)   r"   r    r   r   <module>r:      s&   "   / 2 2  >CL	+c 	+d 	+PcP#&P25PP 
"1"t}" 
!m
" 
" 
 ),1u~ 
!W 
 $1 
!WtA%&&r   