
    /Uik                     4   U 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 d dlmZ d dlmZmZmZmZmZmZ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!m"Z" d dl#m$Z$m%Z% d dl&m'Z' d d	l(m)Z) d d
l*m+Z+m,Z, d dl-m.Z.m/Z/m0Z0m1Z1 d dl2m3Z3m4Z4  edejj                  jl                  e jl                        Z7 edejp                  e jp                        Z9dZ: ejv                         Z<ejv                  eejj                  jl                  e jl                  f      e=d<    G d dee7e9f   e>e7ee/   f         Z? G d d      Z@deAe7   deBfdZCdeejj                  jl                  e   e jl                  e   f   deeD   fdZEeFdddeeAejj                  jl                     eAe jl                     f   deGeH   deBddfdZIdddd d!ej                  e/   d"eegeee   ef   f   d#ed$eee1      d%ee.   d&ed'ej                  e?   d(ee/eKee'   gee/   f   d)ej                  e)   dejj                  jl                  e   fd*ZLdddd+d,d!ej                  e/   d"eegeee   ef   f   d#ed$eee1      d%ee.   d&ed'ej                  e?   d(ee/eKee'   geee/      f   d)ej                  e)   d-e j                  d.eBdee jl                  e   ejj                  jl                  e   f   fd/ZNdddd+d,d0ee jl                  e   ejj                  jl                  e   f   d!ej                  e/   d"eegeee   ef   f   d#ed$eee1      d%ee.   d&ed'ej                  e?e jl                  e jp                  f      d(ee/eKee'   geee/      f   d)ej                  e)   d-e j                  d.eBddfd1ZOy)2    N)AsyncIterator	AwaitableIterableIteratorSequence)partial)AnyCallableGenericOptionalTypeVarUnioncast)	Callbacks)
CONFCONFIG_KEY_CALLCONFIG_KEY_SCRATCHPADERROR	INTERRUPTMISSING	NO_WRITESRESUMERETURN
TAG_HIDDEN)GraphBubbleUpGraphInterrupt)Call)Submit)arun_with_retryrun_with_retry)CachePolicyPregelExecutableTaskPregelScratchpadRetryPolicy)chain_futurerun_coroutine_threadsafeFE)zlanggraph/pregel/retry.pyzlanggraph/pregel/runner.pyzlanggraph/pregel/executor.pyzlanggraph/utils/runnable.pyz"langchain_core/runnables/config.pyzconcurrent/futures/thread.pyzconcurrent/futures/_base.pySKIP_RERAISE_SETc                       e Zd ZU eed<   ej                  eee	e
   gdf      ed<   eed<   ee   ed<   ej                  ed<   dedej                  eee	e
   gdf      dee   ddf fd	Zd
ede	e   ddf fdZdededdfdZ xZS )FuturesDicteventNcallbackcounterdonelockfuture_typereturnc                     t         |           t        j                         | _        || _        || _        d| _        t               | _	        y )Nr   )
super__init__	threadingLockr0   r,   r-   r.   setr/   )selfr,   r-   r1   	__class__s       s/home/jay/workspace/tools/ai-image-gen/jaaz-app/server/venv/lib/python3.12/site-packages/langgraph/pregel/runner.pyr5   zFuturesDict.__init__I   s<     	NN$	
 E	    keyvaluec                    t         |   ||       |j| j                  5  | j                  j	                          | xj
                  dz  c_        d d d        |j                  t        | j                  |             y y # 1 sw Y   0xY w)N   )	r4   __setitem__r0   r,   clearr.   add_done_callbackr   on_done)r9   r=   r>   r:   s      r;   rA   zFuturesDict.__setitem__Y   st    
 	C' "

  "!" !!'$,,">?	 " "s   0A??Btaskfutc                    	  | j                         |t        |             | j                  5  | j                  j	                  |       | xj
                  dz  c_        | j
                  dk(  st        | j                        r| j                  j                          d d d        y # 1 sw Y   y xY w# | j                  5  | j                  j	                  |       | xj
                  dz  c_        | j
                  dk(  st        | j                        r| j                  j                          d d d        w # 1 sw Y   w xY wxY w)Nr@   r   )	r-   
_exceptionr0   r/   addr.   _should_stop_othersr,   r8   )r9   rE   rF   s      r;   rD   zFuturesDict.on_donee   s    
	%DMMOD*S/2 %		c"!<<1$(;DII(FJJNN$	% % % %		c"!<<1$(;DII(FJJNN$	% % %s0    B2 A/B&&B/2E?A/D7.	E7E <E)__name__
__module____qualname__r(   __annotations__weakrefrefr
   r"   r   BaseExceptionintr8   r'   r6   r7   typer5   rA   rD   __classcell__)r:   s   @r;   r+   r+   @   s    Hkk&(?@$FG  L
a&L
.."" ++*H],CDdJK
" !W" 
" 
@
@ ,-
@ 
	
@%"% % 
	%r<   r+   c                      e Zd ZdZddddej
                  e   dej
                  eee	e
eef      gdf      dedeeegdf      d	df
d
Zddddddee   dedee   dee	e      deeg ej(                  j*                  d   f      deeeee   gee   f   d	ed   fdZddddddee   dedee   dee	e      deeg ej*                  d   f      deeeee   geee      f   d	ed   fdZdedee   d	dfdZy)PregelRunnerzResponsible for executing a set of Pregel tasks concurrently, committing
    their writes, yielding control to caller when there is output to emit, and
    interrupting other tasks if appropriate.FN)use_astreamnode_finishedsubmit
put_writesrW   rX   r2   c                <    || _         || _        || _        || _        y N)rY   rZ   rW   rX   )r9   rY   rZ   rW   rX   s        r;   r5   zPregelRunner.__init__y   s"     $&*r<   T)reraisetimeoutretry_policy
get_waitertasksr]   r^   r_   r`   schedule_taskc             #     K   t        |      }t        t        j                  | j                        t        j                         t        j                  j                        }d  t        |      dk(  ry t        |      dk(  rx|v|t|d   }	 t        ||t        t        t        t        j                  |      |t        j                  |      || j                         i       | j	                  |d        |sy d}|
d | |       <   |D ]o  } | j!                         t        ||t        t        t        t        j                  |      |t        j                  |      || j                         i|      }
|||
<   q |r|t3        j4                         z   nd }t        |      |dndkD  rt        j                  j7                  |t        j                  j8                  |r!t;        d|t3        j4                         z
        nd 	      \  }}|snP|D ]&  }
|j=                  |
      }||s|d | |       <   ( ~
~t?        |      rnd  t        |      |dndkD  r|j@                  j7                  |r!t;        d|t3        j4                         z
        nd 
       d  	 tC        |j&                  jE                  d |jG                         D              |       y # t"        $ r}	| j	                  ||	       |rM|rKt        j                  j                         }
|
j%                  |	       |j&                  j)                  |
       nm|rk|	j*                  xr\j,                  It/        fdt0        D              r1j,                  j,                  t/        fdt0        D              r1|	_         Y d }	~	d }	~	ww xY w# t"        $ rp}	|	j*                  xr\j,                  It/        fdt0        D              r1j,                  j,                  t/        fdt0        D              r1|	_         d }	~	ww xY ww)Nr-   r,   r1   r   r@   )retryfuturesrb   rY   )configurablec              3   |   K   | ]3  }j                   j                  j                  j                  |       5 y wr\   tb_framef_codeco_filenameendswith.0nametbs     r;   	<genexpr>z$PregelRunner.tick.<locals>.<genexpr>   4      = $ KK..::CCDI=   9< )rg   __reraise_on_exit__return_whenr^   r^   c              3   ,   K   | ]  \  }}|	|  y wr\   ru   ro   fts      r;   rr   z$PregelRunner.tick.<locals>.<genexpr>        "RAAM1"R   
)panicc              3   |   K   | ]3  }j                   j                  j                  j                  |       5 y wr\   ri   rn   s     r;   rr   z$PregelRunner.tick.<locals>.<genexpr>  4      5 KK&&22;;DA5rt   )$tupler+   rO   
WeakMethodcommitr6   Event
concurrentrf   Futurelenr    r   r   _callrP   rY   	Exceptionset_exceptionr/   rI   __traceback__tb_nextanyEXCLUDED_FRAME_FNAMEStime	monotonicwaitFIRST_COMPLETEDmaxpoprJ   r,   _panic_or_proceedunionitems)r9   ra   r]   r^   r_   r`   rb   rf   r}   excrF   end_timer/   inflightrE   rq   s                  @r;   tickzPregelRunner.tick   s     e''4//#"**11
 	u:?Z1_Z5GaA '!#KKN".$+KK$8*7#';;*	" At$" !$(GJL! 	A$++-#WA* 'G 4&3#{{&	 %,C  GCL#	* 297T^^--d'l:#9aqA'//44&..>>@HQ4>>+; ;<d 5 ND(
  {{3'<J$:04
- "4(+ 'l:#9aqA. 	<DSHt~~'778$ 	 	
 		"""R"RRY  As#w5?5G5G5N5N5PC%%c*LL$$S) ...r. jj4 =(== : "$B	 !jj4 =(== :
 -/)`  	&&&r&jj, 5 55 2 B	 jj, 5 55 2
 %'!	s}   BP>A)K 8D-P>&P>)P>,0P>AP>!;O P>	N?&CN:,N:4P>:N??P>	P;A"P6.P66P;;P>c               	  K   t        j                         }t        |      }t        t	        j
                  | j                        t        j                         t         j                        }d  t        |      dk(  ry t        |      dk(  r|||d   }		 t        |	|| j                  t        t        t        t	        j                  |	      | j                  |t	        j                  |      || j                   |      i       d {    | j                  |	d        |sy d}|
d | |       <   |D ]  }	t5        t         j                   | j!                         t        |	|| j                  t        t        t        t	        j                  |	      || j                  t	        j                  |      || j                   |      i|	j6                  d	|
            }|	||<    |r||j9                         z   nd }t        |      |dndkD  rt        j:                  |t         j<                  |rt?        d||j9                         z
        nd        d {   \  }}|snQ|D ]&  }|jA                  |      }||s|d | |       <   ( ~~tC        |      rnd  t        |      |dndkD  rt        jD                  |jF                  j;                         |rt?        d||j9                         z
        nd        d {    d  |D ]  }|jI                           	 tK        |j(                  jM                  d |jO                         D              t         jP                  |       y 7 b# t"        $ r}
| j                  |	|
       |r?|r=|j%                         }|j'                  |
       |j(                  j+                  |       nm|rk|
j,                  xr\j.                  It1        fdt2        D              r1j.                  j.                  t1        fdt2        D              r1|
_         Y d }
~
d }
~
ww xY w7 7 J# t"        $ rp}
|
j,                  xr\j.                  It1        fdt2        D              r1j.                  j.                  t1        fdt2        D              r1|
_         d }
~
ww xY ww)Nrd   r   r@   )streamre   rf   rb   rY   loop)r   rg   c              3   |   K   | ]3  }j                   j                  j                  j                  |       5 y wr\   ri   rn   s     r;   rr   z%PregelRunner.atick.<locals>.<genexpr>A  rs   rt   ru   )re   r   rf   rb   rY   r   T)r   rg   rK   __cancel_on_exit__rv   rw   ry   c              3   ,   K   | ]  \  }}|	|  y wr\   ru   r{   s      r;   rr   z%PregelRunner.atick.<locals>.<genexpr>  r~   r   timeout_exc_clsr   c              3   |   K   | ]3  }j                   j                  j                  j                  |       5 y wr\   ri   rn   s     r;   rr   z%PregelRunner.atick.<locals>.<genexpr>  r   rt   ))asyncioget_event_loopr   r+   rO   r   r   r   r   r   r   rW   r   r   _acallrP   rY   r   create_futurer   r/   rI   r   r   r   r   r   rp   r   r   r   r   r   rJ   wait_forr,   cancelr   r   r   TimeoutError)r9   ra   r]   r^   r_   r`   rb   r   rf   r}   r   rF   r   r/   r   rE   rq   s                   @r;   atickzPregelRunner.atick
  s]     %%'e''4--/
 	u:?Z1_!3aA"% ++'"#KKN#'#3#3".$+KK$8*7#';;!%	*"	  " At$" !$(GJL! 	A# ++'"#KKN".#'#3#3$+KK$8*7#';;!%	*" VV'+(/'C0 GCL3	: -47TYY[('l:#9aqA#*<<#33;CQ499; 67$ ND(
  {{3'<J$:04
- "4(+ 'l:#9aqA. MM 7?SHtyy{23T
 	
 	

 	 	CJJL		"""R"RR ' 4 4U$  As#w*.*<*<*>C%%c*LL$$S) ...r. jj4 =(== : "$B	 !jj4 =(== :
 -/)n,	
   	&&&r&jj, 5 55 2 B	 jj, 5 55 2
 %'!	s   BS	A1M0 M-M0 "D1S	Q"S	7S	:S	=1S	/AS	 Q
 S	"A
Q ,S	-M0 0	Q9B7P?1P?9S	?QS	
S		SA"S9SSS	rE   	exceptionc                    t        |t        j                        rM|j                  j	                  t
        |f        | j                         |j                  |j                         y |rt        |t              r|j                  d   rt        |j                  d   fg}|j                  D cg c]  }|d   t        k(  s| c}x}r|j                  |        | j                         |j                  |       y t        |t              r||j                  j	                  t
        |f        | j                         |j                  |j                         y y | j                  rI|j                  "t         |j                  j#                  dg       vr| j                  |j$                         |j                  s!|j                  j	                  t&        d f        | j                         |j                  |j                         y c c}w )Nr   tags)
isinstancer   CancelledErrorwritesappendr   rZ   idr   argsr   r   extendr   rX   configr   getrp   r   )r9   rE   r   r   wresumess         r;   r   zPregelRunner.commit  s   
 i!7!78 KKy12DOOdggt{{3)^4>>!$()..*;<=F.2kk"LQqTV^1"LLwLg.%DOO%dggv6I}5 ""E9#56!!$''4;;7 % !!#zQS9T'T""499-;;""It#45DOOdggt{{3% #Ms   -H	?H	) rK   rL   rM   __doc__rO   rP   r   r
   strr   r   r	   boolr   r5   r   r"   floatr$   r   rf   r   rR   r   r   r   r   r   r   r   rQ   r   ru   r<   r;   rV   rV   t   s   0 "9=+ F#+ KK#xc3h/H)I4)O PQ	+
 +  # 56+ 
+" #'8<NRB,-B 	B
 %B x45B Xb**<*<*C*CD*I&IJKB  !37)*,
B 
$BP #'8<CGS,-S 	S
 %S x45S Xb'..*>&>?@S  !37h3457
S 
t	Sj!4"!4 M*!4 
	!4r<   rV   r/   r2   c                     | D ]A  }|j                         r|j                         x}s't        |t              r8|t        vsA y y)ziCheck if any task failed, if so, cancel all other tasks.
    GraphInterrupts are not considered failures.TF)	cancelledr   r   r   r)   )r/   rF   r   s      r;   rJ   rJ     sJ    
  ==?MMO#S#c=1cAQ6Q r<   rF   c                     | j                         rLt        | t        j                        rt        j                         S t
        j                  j	                         S | j                         S )zCReturn the exception from a future, without raising CancelledError.)r   r   r   r   r   r   rf   r   )rF   s    r;   rH   rH     sK     }}c7>>*))++%%4466}}r<   Tr   futsr   r   c                @   t               }t               }| D ]F  }|j                         r|j                         r|j                  |       6|j                  |       H g }|rq|j	                         }t        |      x}rQ|r!|j	                         j                          |r!|r,t        |t              r|j                  |       n
|t        vr||rq|rt        t        d |D                    |r+|r!|j	                         j                          |r! |d      y)zJCancel remaining tasks if any failed, re-raise exception if panic is True.c              3   H   K   | ]  }|j                   d    D ]  }|   yw)r   N)r   )ro   r   is      r;   rr   z$_panic_or_proceed.<locals>.<genexpr>   s$     "PCHHQK"Pq1"P1"Ps    "z	Timed outN)r8   r   r/   rI   r   rH   r   r   r   r   r)   r   )r   r   r   r/   r   rF   
interruptsr   s           r;   r   r     s    MPEDPSPUH ==?XXZHHSMLL (*J
hhjS/!3!%%'  c>2%%c* 00I  U"P
"PPQQLLN!!#  k** r<   re   cache_policy	callbacksrE   funcinputre   r   r   rf   rb   rY   c                L   t        j                  |      rt        d      d }	 |        j                  t           t
           }
 | |        |
j                         t        |||||            xrRt        fd |       j                         D        d       x}	rn&j                  rt        j                  j                         }	t        d j                  D        t              }|t        ur|	j                  |       nt        d j                  D        d       x}r-|	j!                  t#        |t$              r|n
t'        |             nu|	j                  d        nc  |       t(        |t*        t-        t.        t1        j2                        |||||      idd	      }	t4        j7                  |	        |       |	<   t9        t:        t         j                  t        j                  j                  f   |	      }	t=        |	t        j                  j                               S )
Nz/In an sync context async tasks cannot be calledr   c              3   J   K   | ]  \  }}||j                   k(  r|  y wr\   r   ro   r|   r}   	next_tasks      r;   rr   z_call.<locals>.<genexpr>%  s-      Aq=Q),,%6     #c              3   :   K   | ]  \  }}|t         k(  s|  y wr\   r   ro   cvs      r;   rr   z_call.<locals>.<genexpr>2  s     Fda!v+F   c              3   :   K   | ]  \  }}|t         k(  s|  y wr\   r   r   s      r;   rr   z_call.<locals>.<genexpr>5  s     KDAqU
aKr   )rf   re   r   rb   rY   FT)rg   rv   __next_tick__)r   iscoroutinefunctionRuntimeErrorr   r   r   call_counterr   nextr   r   r   rf   r   r   
set_resultr   r   rQ   r   r    r   r   r   rO   rP   r)   rI   r   r   r%   )rE   r   r   re   r   r   rf   rb   rY   rF   
scratchpadretr   r   s                @r;   r   r   
  s    ""4(LMM/3C#'6==#67L#MJ!!T5LIV y 
 #IOO-
 
 
3 
 $$++-CFi&6&6FPC'!s#KI,<,<KTRRR!!%c=9Cy~ t$ &(#WI. '#"+&3%&
 %* #%C,   %&GIcN
uW^^Z%7%7%>%>>?
EC Z//66899r<   F)re   r   r   r   r   r   c                
   	 t        j                         d u}|rt        j                  |	      }nt        j
                  j                         }t        t        || |||||||||	|
      |	d       |S # t        $ r d}Y iw xY w)NFr   )re   r   r   rf   rb   rY   r   r   )lazy)r   current_taskr   r   r   rf   r&   _acall_impl)rE   r   r   re   r   r   rf   rb   rY   r   r   in_asyncrF   s                r;   r   r   [  s    ('')5 NN% 	   '')%'	
 	!$ J9  s   A4 4BBdestinationc                V  K   	 d } |       j                   t           t           } | |       |j                         t	        |||||             d {   xryt        fd |       j                         D        d       x}rnMj                  rt        j                  |
      }t        d j                  D        t              }|t        ur|j                  |       n]t        d j                  D        d       x}r-|j                  t        |t              r|n
t        |             n|j                  d         |       |<   nt!        t        j                    |	       t"        ||t$        t'        t(        t+        j,                        ||||	|
      ij.                  ddd		            }t0        j3                  |        |       |<   |t5        ||        y | j                  t7        d
             y 7 # t        $ r}| j                  |       Y d }~y d }~ww xY ww)Nr   c              3   J   K   | ]  \  }}||j                   k(  r|  y wr\   r   r   s      r;   rr   z_acall_impl.<locals>.<genexpr>  s-      1}ill): r   r   c              3   :   K   | ]  \  }}|t         k(  s|  y wr\   r   r   s      r;   rr   z_acall_impl.<locals>.<genexpr>  s     J$!Qa6kAJr   c              3   :   K   | ]  \  }}|t         k(  s|  y wr\   r   r   s      r;   rr   z_acall_impl.<locals>.<genexpr>  s     !O1AJ!!Or   )r   rf   rb   rY   r   TF)r   rg   rK   r   rv   r   zTask not scheduled)r   r   r   r   r   r   r   r   r   r   r   r   r   r   rQ   r   r   r   r   r   r   rO   rP   rp   r)   rI   r%   r   )r   rE   r   r   re   r   r   rf   rb   rY   r   r   rF   r   r   r   r   s                   @r;   r   r     s	    &I'(,'+v}}T':;P'Q
+F##%e5|y
 
 
9 
  '	 1
  s  !!nn$/J)*:*:JGTg%NN3' !O	0@0@!OQUVVSV%%)#}=9S> NN4(!*	# NNFH'!%+W & 'I 6'-(/.;'-%).
& "++/,1 '++8 !$$S)!*	#?k*%%l3G&HIE
F  '!!#&&'sN   H)AH G?FH #H)$H >H)?H 	H&H!H)!H&&H))Pr   concurrent.futuresr   r6   r   rO   collections.abcr   r   r   r   r   	functoolsr   typingr	   r
   r   r   r   r   r   langchain_core.callbacksr   langgraph.constantsr   r   r   r   r   r   r   r   r   r   langgraph.errorsr   r   langgraph.pregel.algor   langgraph.pregel.executorr   langgraph.pregel.retryr   r    langgraph.typesr!   r"   r#   r$   langgraph.utils.futurer%   r&   rf   r   r'   r   r(   r   WeakSetr)   rN   dictr+   rV   r8   r   rJ   rQ   rH   r   rS   r   r   rP   rR   r   AbstractEventLoopr   r   ru   r<   r;   <module>r      s        R R    /   ; & , B  JC##**GNN;C'--0  GOO '//%
(:(:(A(A7>>(Q"RS 
1%'!Q$-a2F)G&G!H 1%hL4 L4^

a&	
	z!!((-w~~c/BB	C
m
  (4	(+
J&&--.GNN0CC
D(+ )_(+ 	(+
 
(+` .2*.N:
++*
+N:
C5%	# 344
5N: N:
 H[)*N: ;'N: N: [[%N: 	sHTN3X>R5SSN: KKN: s#N:l .2*. !2
++*
+2
C5%	# 344
52 2
 H[)*2 ;'2 2 [[%2 	sHTN3(/01	32 KK2 
#
#2  !2" 7>>#
 2 2 9 9# >>?#2v .2*. #\'w~~c*J,>,>,E,Ec,JJK\'
++*
+\' C5%	# 344
5\' 	\' H[)*\' ;'\' \' [[W^^W]]%BCD\' 	sHTN3(/01	3\' KK\'  
#
#!\'" #\'$ 
%\'r<   