o
    cBg                     @   s<  d dl Z d dl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mZmZmZmZmZmZmZ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mZ edZedZ G dd deee f Z!G dd deZ"G dd deZ#de j$dedde f de fddZ%deee f dej&dej'de fddZ(dS )    N)	ExitStack)copy_context)TracebackType)	AsyncContextManager	AwaitableCallableContextManager	CoroutineOptionalProtocolTypeVarcast)RunnableConfig)get_executor_for_config)	ParamSpec)GraphBubbleUp)CONTEXT_NOT_SUPPORTEDrun_coroutine_threadsafePTc                   @   sZ   e Zd Zddddddeeef dejdee de	d	e	d
e	dej
dejje fddZdS )SubmitNFT__name____cancel_on_exit____reraise_on_exit____next_tick__fnargsr   r   r   r   kwargsreturnc                O   s   d S N )selfr   r   r   r   r   r   r   r!   r!   `/var/www/html/development/chatbot/venv/lib/python3.10/site-packages/langgraph/pregel/executor.py__call__   s   	zSubmit.__call__)r   
__module____qualname__r   r   r   r   r
   strboolr   
concurrentfuturesFuturer$   r!   r!   r!   r#   r      s,    

	r   c                   @   s   e Zd ZdZdeddfddZddddd	d
eeef dej	de
e dedededejdejje fddZdejjddfddZdefddZde
ee  de
e de
e de
e fddZdS )BackgroundExecutoraI  A context manager that runs sync tasks in the background.
    Uses a thread pool executor to delegate tasks to separate threads.
    On exit,
    - cancels any (not yet started) tasks with `__cancel_on_exit__=True`
    - waits for all tasks to finish
    - re-raises the first exception from tasks with `__reraise_on_exit__=True`configr   Nc                 C   s$   t  | _| jt|| _i | _d S r    )r   stackenter_contextr   executortasks)r"   r-   r!   r!   r#   __init__3   s   
zBackgroundExecutor.__init__FTr   r   r   r   r   r   r   r   c          
      O   sv   t  }|rttjjt | jjt|j	|g|R i |}	n| jj|j	|g|R i |}	||f| j
|	< |	| j |	S r    )r   r   r)   r*   r+   r   r0   submit	next_tickrunr1   add_done_callbackdone)
r"   r   r   r   r   r   r   r   ctxtaskr!   r!   r#   r3   9   s   

zBackgroundExecutor.submitr9   c                 C   sP   z|   W n ty   | j| Y dS  ty   Y dS w | j| dS )z3Remove the task from the tasks dict when it's done.N)resultr   r1   popBaseException)r"   r9   r!   r!   r#   r7   P   s   zBackgroundExecutor.donec                 C   s   | j S r    r3   r"   r!   r!   r#   	__enter__]   s   zBackgroundExecutor.__enter__exc_type	exc_value	tracebackc           
   	   C   s   | j  }| D ]\}\}}|r|  q	dd |D  }r%tj| | j||| |d u rP| D ]\}\}}	|	s>q5z|	  W q5 tjj
yO   Y q5w d S d S )Nc                 S   s   h | ]}|  s|qS r!   )r7   ).0tr!   r!   r#   	<setcomp>m   s    z.BackgroundExecutor.__exit__.<locals>.<setcomp>)r1   copyitemscancelr)   r*   waitr.   __exit__r:   CancelledError)
r"   r@   rA   rB   r1   r9   rH   _pendingreraiser!   r!   r#   rJ   `   s&   
zBackgroundExecutor.__exit__)r   r%   r&   __doc__r   r2   r   r   r   r   r
   r'   r(   r   r)   r*   r+   r3   r7   r   r?   typer<   r   rJ   r!   r!   r!   r#   r,   +   sF    


	

r,   c                   @   s   e Zd ZdZdeddfddZddddd	d
eeee	 f dej
dee dedededejdeje	 fddZdejddfddZdefddZdeee  dee dee ddfddZdS )AsyncBackgroundExecutoraS  A context manager that runs async tasks in the background.
    Uses the current event loop to delegate tasks to asyncio tasks.
    On exit,
    - cancels any tasks with `__cancel_on_exit__=True`
    - waits for all tasks to finish
    - re-raises the first exception from tasks with `__reraise_on_exit__=True`
      ignoring CancelledErrorr-   r   Nc                 C   s@   i | _ t | _t | _|d }rt|| _d S d | _d S )Nmax_concurrency)	r1   objectsentinelasyncioget_running_looploopget	Semaphore	semaphore)r"   r-   rR   r!   r!   r#   r2      s   


z AsyncBackgroundExecutor.__init__FTr   r   r   r   r   r   r   r   c          
      O   s|   t td d tf ||i |}| jrt| j|}tr$t|| j||d}	nt|| j|t |d}	||f| j	|	< |	
| j |	S )N)namelazy)r[   contextr\   )r   r	   r   rZ   gatedr   r   rW   r   r1   r6   r7   )
r"   r   r   r   r   r   r   r   coror9   r!   r!   r#   r3      s"   

zAsyncBackgroundExecutor.submitr9   c                 C   sd   z |   }rt|tr| j| W d S W d S | j| W d S  tjy1   | j| Y d S w r    )	exception
isinstancer   r1   r;   rU   rK   )r"   r9   excr!   r!   r#   r7      s   
zAsyncBackgroundExecutor.donec                    s   | j S r    r=   r>   r!   r!   r#   
__aenter__   s   z"AsyncBackgroundExecutor.__aenter__r@   rA   rB   c           
   	      s   | j  }| D ]\}\}}|r|| j q
|r#t|I d H  |d u rI| D ]\}\}}|s4q+z
|  }	r=|	W q+ tjyH   Y q+w d S d S r    )	r1   rF   rG   rH   rT   rU   rI   r`   rK   )
r"   r@   rA   rB   r1   r9   rH   rL   rN   rb   r!   r!   r#   	__aexit__   s*   
z!AsyncBackgroundExecutor.__aexit__)r   r%   r&   rO   r   r2   r   r   r   r   r   r
   r'   r(   r   rU   r+   r3   r7   r   rc   rP   r<   r   rd   r!   r!   r!   r#   rQ   }   sF    	

rQ   rZ   r_   r   c              	      sH   | 4 I dH  |I dH W  d  I dH  S 1 I dH sw   Y  dS )zHA coroutine that waits for a semaphore before running another coroutine.Nr!   )rZ   r_   r!   r!   r#   r^      s   0r^   r   r   r   c                 O   s   t d | |i |S )zPA function that yields control to other threads before running another function.r   )timesleep)r   r   r   r!   r!   r#   r4      s   
r4   ))rU   concurrent.futuresr)   re   
contextlibr   contextvarsr   typesr   typingr   r   r   r   r	   r
   r   r   r   langchain_core.runnablesr   langchain_core.runnables.configr   typing_extensionsr   langgraph.errorsr   langgraph.utils.futurer   r   r   r   r   r,   rQ   rY   r^   r   r   r4   r!   r!   r!   r#   <module>   s&    ,R"\*