
    D_ix)                        S r SSKJr  SSKJrJrJrJrJrJ	r	  SSK
Jr  SSKJr  SSKJrJrJrJrJr  SSKJr  SSKJr  \" S	5      r\" 5       r\" S
SS9\4S\\   S\\-  S\\\-  S-     4S jj5       r " S S5      rS\\   S\\   S\\\      S\\   S\\S4   4
S jr " S S\\   5      r\r  " S S\5      r!S\"S\\   S\\\      4S jr#g)zAsynchronous iterator utilities.

Adapted from
https://github.com/maxfischer2781/asyncstdlib/blob/master/asyncstdlib/itertools.py
MIT License.
    )deque)AsyncGeneratorAsyncIterableAsyncIterator	AwaitableCallableIterator)AbstractAsyncContextManager)TracebackType)AnyGenericTypeVarcastoverload)override)
deprecatedTz1.1.2z2.0.0)sinceremovaliteratordefaultreturnNc                    ^ ^^  [        S[        T 5      R                  5      mT[
        L a  T" T 5      $ S[        [        -  4UUU 4S jjnU" 5       $ ! [         a  nT < S3n[	        U5      UeSnAff = f)a{  Pure-Python implementation of anext() for testing purposes.

Closely matches the builtin anext() C implementation.
Can be used to compare the built-in implementation of the inner
coroutines machinery to C-implementation of __anext__() and send()
or throw() on the returned generator.

Args:
    iterator: The async iterator to advance.
    default: The value to return if the iterator is exhausted.
        If not provided, a StopAsyncIteration exception is raised.

Returns:
    The next value from the iterator, or the default value
    if the iterator is exhausted.

Raises:
    TypeError: If the iterator is not an async iterator.
z*Callable[[AsyncIterator[T]], Awaitable[T]]z is not an async iteratorNr   c                  V   >#     T " T5      I S h  vN $  N! [          a    Ts $ f = f7fN)StopAsyncIteration)	__anext__r   r   s   T/home/james-whalen/.local/lib/python3.13/site-packages/langchain_core/utils/aiter.py
anext_implpy_anext.<locals>.anext_implG   s1     	 #8,,,,! 	N	s(   )  ) &)&))r   typer   AttributeError	TypeError_no_defaultr   r   )r   r   emsgr   r   s   ``   @r   py_anextr'   %   s    .$8$x.:R:R
	 +""	a#g 	 	 <%  $56n!#$s   A 
A4A//A4c                   T    \ rS rSrSrSS jrS\\   S-  S\S-  S\S-  S\	4S	 jr
S
rg)NoLockU   z@Dummy lock that provides the proper interface but no protection.r   Nc                    #    g7f)zDo nothing.N selfs    r   
__aenter__NoLock.__aenter__X   s        exc_typeexc_valexc_tbc                    #    g7f)z'Return False, exception not suppressed.Fr,   r.   r2   r3   r4   s       r   	__aexit__NoLock.__aexit__[   s
      r1   r,   r   N)__name__
__module____qualname____firstlineno____doc__r/   r!   BaseExceptionr   boolr7   __static_attributes__r,       r   r)   r)   U   sI    J}%, % $	
 
rB   r)   bufferpeerslockc           	       #      U(       dc  U ISh  vN   U(       a   SSS5      ISh  vN   M-   [        U 5      I Sh  vN nU H  nUR                  U5        M     SSS5      ISh  vN   UR                  5       7v   M   Nq N[ NG! [         a     SSS5      ISh  vN    O f = f N@! , ISh  vN  (       d  f       NU= fU ISh  vN    [	        U5       H  u  peXQL d  M  UR                  U5          O   U(       d*  [        U S5      (       a  U R                  5       I Sh  vN    SSS5      ISh  vN    g! , ISh  vN  (       d  f       g= f! U ISh  vN    [	        U5       H  u  peXQL d  M  UR                  U5          O   U(       d*  [        U S5      (       a  U R                  5       I Sh  vN    SSS5      ISh  vN    f ! , ISh  vN  (       d  f       f = f= f7f)a  An individual iterator of a `tee`.

This function is a generator that yields items from the shared iterator
`iterator`. It buffers items until the least advanced iterator has
yielded them as well. The buffer is shared with all other peers.

Args:
    iterator: The shared iterator.
    buffer: The buffer for this peer.
    peers: The buffers of all peers.
    lock: The lock to synchronise access to the shared buffers.

Yields:
    The next item from the shared iterator.
Naclose)anextappendr   popleft	enumeratepophasattrrG   )r   rC   rD   rE   itempeer_bufferidxs          r   tee_peerrQ   e   sS    .(4  	  44
5%*8_4 ,1K'..t4 ,1  4" ..""'   5-   44  444& 4$-e$4 (IIcN %5
 WXx88oo''' 4444444$-e$4 (IIcN %5
 WXx88oo''' 44444sU  G3E BE 	B-E BE B
 BB
B-E *B++E E B


B(B-E  B#!E 'B((B-+E -C3B64C E G3CG3E)A E)D,*E/G3:D=;G3EE
EG3G0"E%
#G0'G>A G>G
?GG0GG0G-GG-)G00G3c            	       >   \ rS rSrSr SSS.S\\   S\S\\	   S-  4S jjjr
S	\4S
 jr\S\S	\\   4S j5       r\S\S	\\\   S4   4S j5       rS\\-  S	\\   \\\   S4   -  4S jrS	\\\      4S jrSS jrS\\   S-  S\S-  S\S-  S	\4S jrSS jrSrg)Tee   a  Create `n` separate asynchronous iterators over `iterable`.

This splits a single `iterable` into multiple iterators, each providing
the same items in the same order.
All child iterators may advance separately but share the same items
from `iterable` -- when the most advanced iterator retrieves an item,
it is buffered until the least advanced iterator has yielded it as well.
A `tee` works lazily and can handle an infinite `iterable`, provided
that all iterators advance.

```python
async def derivative(sensor_data):
    previous, current = a.tee(sensor_data, n=2)
    await a.anext(previous)  # advance one iterator
    return a.map(operator.sub, previous, current)
```

Unlike `itertools.tee`, `.tee` returns a custom type instead
of a :py`tuple`. Like a tuple, it can be indexed, iterated and unpacked
to get the child iterators. In addition, its `.tee.aclose` method
immediately closes all children, and it can be used in an `async with` context
for the same effect.

If `iterable` is an iterator and read elsewhere, `tee` will *not*
provide these items. Also, `tee` must internally buffer each item until the
last iterator has yielded it; if the most and least advanced iterator differ
by most data, using a :py`list` is more efficient (but not lazy).

If the underlying iterable is concurrency safe (`anext` may be awaited
concurrently) the resulting iterators are concurrency safe as well. Otherwise,
the iterators are safe if there is only ever one single "most advanced" iterator.
To enforce sequential use of `anext`, provide a `lock`
- e.g. an :py`asyncio.Lock` instance in an :py:mod:`asyncio` application -
and access is automatically synchronised.

N)rE   iterablenrE   c                   ^ ^ UR                  5       T l        [        U5       Vs/ s H  n[        5       PM     snT l        [        UU 4S jT R                   5       5      T l        gs  snf )zCreate a `tee`.

Args:
    iterable: The iterable to split.
    n: The number of iterators to create.
    lock: The lock to synchronise access to the shared buffers.

c              3   ~   >#    U  H2  n[        TR                  UTR                  Tb  TO	[        5       S9v   M4     g 7f)N)r   rC   rD   rE   )rQ   	_iterator_buffersr)   ).0rC   rE   r.   s     r   	<genexpr>Tee.__init__.<locals>.<genexpr>   s>      
 ( mm!-T68	 (s   :=N)	__aiter__rY   ranger   rZ   tuple	_children)r.   rU   rV   rE   _s   `  ` r   __init__Tee.__init__   sV     "++-:?((C(Q((C 
 --
 
 )Ds   A'r   c                 ,    [        U R                  5      $ )z%Return the number of child iterators.)lenra   r-   s    r   __len__Tee.__len__   s    4>>""rB   rN   c                     g r   r,   r.   rN   s     r   __getitem__Tee.__getitem__   s    :=rB   .c                     g r   r,   rj   s     r   rk   rl      s    HKrB   c                      U R                   U   $ )z:Return the child iterator(s) for the given index or slice.ra   rj   s     r   rk   rl      s     ~~d##rB   c              #   8   #    U R                    Sh  vN   g N7f)zDIterate over the child iterators.

Yields:
    The child iterators.
Nro   r-   s    r   __iter__Tee.__iter__   s      >>!!s   c                    #    U $ 7f)zReturn the tee instance.r,   r-   s    r   r/   Tee.__aenter__   s
     s   r2   r3   r4   c                 @   #    U R                  5       I Sh  vN   g N7f)zKClose all child iterators.

Returns:
    False, exceptions not suppressed.
NF)rG   r6   s       r   r7   Tee.__aexit__   s      kkm 	s   c                 f   #    U R                    H  nUR                  5       I Sh  vN   M     g N	7f)z Async close all child iterators.N)ra   rG   )r.   childs     r   rG   
Tee.aclose  s%     ^^E,,.   $ s   #1/
1)rZ   ra   rY   )   )r   zTee[T]r9   )r:   r;   r<   r=   r>   r   r   intr
   r   rc   rg   r   rk   slicer`   r	   rq   r/   r!   r?   r   r@   r7   rG   rA   r,   rB   r   rS   rS      s*   #P 

 9=
"
 

 *#.5
6# # ==a(8= =KK%a0@#0E*FK K$%K$	q	E-"2C"78	8$"(=#34 "}%, % $	
 
!rB   rS   c            	           \ rS rSrSrS\\\4   \\   -  SS4S jr\	S\\\4   \\   -  4S j5       r
\	S\\   S-  S	\S-  S
\S-  SS4S j5       rSrg)aclosingi  a1  Async context manager to wrap an AsyncGenerator that has a `aclose()` method.

Code like this:

```python
async with aclosing(<module>.fetch(<arguments>)) as agen:
    <block>
```

is equivalent to this:

```python
agen = <module>.fetch(<arguments>)
try:
    <block>
finally:
    await agen.aclose()

```
thingr   Nc                     Xl         g)zDCreate the context manager.

Args:
    thing: The resource to wrap.
Nr   )r.   r   s     r   rc   aclosing.__init__'  s	     
rB   c                 "   #    U R                   $ 7fr   r   r-   s    r   r/   aclosing.__aenter__/  s     zzs   r2   	exc_value	tracebackc                    #    [        U R                  S5      (       a#  U R                  R                  5       I S h  vN   g g  N7f)NrG   )rM   r   rG   )r.   r2   r   r   s       r   r7   aclosing.__aexit__3  s6      4::x((**##%%% )%s   9AAAr   )r:   r;   r<   r=   r>   r   r   r   rc   r   r/   r!   r?   r   r7   rA   r,   rB   r   r~   r~     s    *nS#X6s9KK PT  .c":]3=O"O   &}%,& !4'& !4'	&
 
& &rB   r~   sizerU   c                   #    / nU  Sh  vN n[        U5      U :  a  UR                  U5        [        U5      U :  d  M:  U7v   / nMC   N>
 U(       a  U7v   gg7f)zUtility batching function for async iterables.

Args:
    size: The size of the batch.
    iterable: The async iterable to batch.

Yields:
    The batches.
N)rf   rI   )r   rU   batchelements       r   abatch_iterater   >  sZ      E! gu:LL!u:KE  s+   AAA
A.A	A
AA)$r>   collectionsr   collections.abcr   r   r   r   r   r	   
contextlibr
   typesr   typingr   r   r   r   r   typing_extensionsr   langchain_core._api.deprecationr   r   objectr$   r'   r)   listrQ   rS   ateer~   r{   r   r,   rB   r   <module>r      sB     3   ' 6CLh '7+3>,A,)*S,q3w~, ,,^  5(A5( !H5(
 a>5( &c
*5( AtG5(pn!'!* n!b *&* *&Z
&q)47rB   