o
    cBgD                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZm	Z	m
Z
mZmZmZmZ edZee jejjf Zejdk Zejdk Zde jde jfdd	Zd
edefddZdejjdeddfddZdede jddfddZdededdfddZdededefddZdddddee	ddef ee f de jdee deej de de j!e fd d!Z"ej#d"ee de
ddef fd#d$Z$ddd%d&e	ddef de jde dee deej de je fd'd(Z%dS ))    N)	Awaitable	Coroutine	GeneratorOptionalTypeVarUnioncastT)      )r
      futreturnc                 C   s&   z| j }W | S  ty   Y | jS w N)get_loopAttributeError_loop)r   r    r   ]/var/www/html/development/chatbot/venv/lib/python3.10/site-packages/langgraph/utils/future.py	_get_loop   s   r   excc                 C   sT   t | }|tjju rtj| j S |tjju rtj| j S |tjju r(tj| j S | S r   )type
concurrentfuturesCancelledErrorasyncioargsTimeoutErrorInvalidStateError)r   	exc_classr   r   r   _convert_future_exc   s   r    r   sourcec                 C   s`   |  sJ | r|   |  sdS | }|dur%| t| dS | }| | dS )z8Copy state from a future to a concurrent.futures.Future.N)	done	cancelledcancelset_running_or_notify_cancel	exceptionset_exceptionr    result
set_result)r   r!   r&   r(   r   r   r   _set_concurrent_future_state(   s   r*   destc                 C   sp   |  rdS |   sJ | rdS |  r|  dS |  }|dur-|t| dS |  }|| dS )zqInternal helper to copy state from another Future.

    The other Future may be a concurrent.futures.Future.
    N)r"   r#   r$   r&   r'   r    r(   r)   )r!   r+   r&   r(   r   r   r   _copy_future_state:   s   r,   destinationc                    s   t sttjjstdt s ttjjs tdt r)tndt r4tnddtdtddfdd d	tddffd
d}dtddf fdd}	| 	| dS )a  Chain two futures so that when one completes, so does the other.

    The result (or exception) of source will be copied to destination.
    If destination is cancelled, source gets cancelled too.
    Compatible with both asyncio.Future and concurrent.futures.Future.
    z(A future is required for source argumentz-A future is required for destination argumentNfutureotherr   c                 S   s&   t | rt||  d S t| | d S r   )r   isfuturer,   r*   )r.   r/   r   r   r   
_set_statea   s   
z!_chain_future.<locals>._set_stater-   c                    s8   |   rd u s u r  d S j d S d S r   )r#   r$   call_soon_threadsafe)r-   )	dest_loopr!   source_loopr   r   _call_check_cancelg   s
   z)_chain_future.<locals>._call_check_cancelr!   c                    sX     rd ur rd S d u su r |  d S  r#d S  |  d S r   )r#   	is_closedr2   )r!   )r1   r3   r-   r4   r   r   _call_set_staten   s   z&_chain_future.<locals>._call_set_state)
r   r0   
isinstancer   r   Future	TypeErrorr   	AnyFutureadd_done_callback)r!   r-   r5   r7   r   )r1   r3   r-   r!   r4   r   _chain_futureO   s   

r=   c              
   C   sj   zt | | |W S  ttfy     ty4 } zt|tjjr*| r)|	|  |	|  d }~ww r   )
r=   
SystemExitKeyboardInterruptBaseExceptionr8   r   r   r9   r%   r'   )r!   r-   r   r   r   r   chain_future|   s   


rA   T)namecontextlazycoro_or_futurelooprB   rC   rD   c                C   s   d}t | s&t| rttd d tf t| } d}ntdt	| j
 dz!tr1|j| |dW S ts5|r>|j| ||dW S t j|| ||dW S  tyU   |sT|    w )NFTz@An asyncio.Future, a coroutine or an awaitable is required. Got z	 instead.)rB   rB   rC   )r   iscoroutineinspectisawaitabler   r   r	   _wrap_awaitabler:   r   __name__CONTEXT_NOT_SUPPORTEDcreate_taskEAGER_NOT_SUPPORTEDeager_task_factoryRuntimeErrorclose)rE   rF   rB   rC   rD   called_wrap_awaitabler   r   r   _ensure_future   s2   

rT   	awaitablec                 c   s    |   E dH S )zHelper for asyncio.ensure_future().

    Wraps awaitable (an object with __await__) into a coroutine
    that will later be wrapped in a Task by ensure_future().
    N)	__await__)rU   r   r   r   rK      s   rK   rG   coroc                   sR   t  u rt |dS t jdd fdd}j| d S )	zhSubmit a coroutine object to a given event loop.

    Return a asyncio.Future to access the result.
    )rF   rB   rC   rD   )rF   r   Nc               
      sT   zt t d W d S  ttfy     ty) }  z|   d } ~ ww )N)rF   rB   rC   )rA   rT   r>   r?   r@   r'   )r   rC   rW   r.   rF   rB   r   r   callback   s   

z*run_coroutine_threadsafe.<locals>.callback)rC   )r   N)r   _get_running_looprT   r9   r2   )rW   rF   rD   rB   rC   rY   r   rX   r   run_coroutine_threadsafe   s   r[   )&r   concurrent.futuresr   contextvarsrI   systypestypingr   r   r   r   r   r   r   r	   r9   r   r;   version_inforM   rO   AbstractEventLoopr   r@   r    r*   r,   r=   rA   strContextboolTaskrT   	coroutinerK   r[   r   r   r   r   <module>   sp    $


-
$"