
    i              
       "   d 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	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.argskwargsreturnc                 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_threadpool	functoolspartial)r   r   r   s      m/Users/bowang/.openclaw/workspace/ChatDev/.venv/lib/python3.12/site-packages/fastmcp/utilities/async_utils.py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_at8   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   &   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   collections.abcr   r   typingr   r   r   r   r(   anyio.to_threadr	   r   r
   r   listr"   r   boolr       r   <module>r4      s   "  / 2 2  >CLPcP#&P25PP 
"1"t}" 
!m
" 
" 
 ),1u~ 
!W 
 $1 
!WtA%&&r3   