
    g3fix)                        d Z ddlmZ ddl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mZmZmZmZ ddlmZ ddlmZ  ed	      Z e       Z ed
d      efdee   deez  deeez  dz     fd       Z G d d      Zdee   dee   deee      dee   deedf   f
dZ G d dee         ZeZ  G d de      Z!de"dee   deee      fdZ#y)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                      	 t        dt               j                        t
        u r        S dt        t        z  f fd} |       S # t        $ r} d}t	        |      |d}~ww xY w)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                  T   K   	          d {   S 7 # t         $ r cY S w xY wwN)StopAsyncIteration)	__anext__r   r   s   X/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_core/utils/aiter.py
anext_implzpy_anext.<locals>.anext_implG   s2     	 #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 	A/A**A/c                   H    e Zd ZdZd	dZdee   dz  dedz  dedz  defdZ	y)
NoLockz@Dummy lock that provides the proper interface but no protection.r   Nc                    K   yw)zDo nothing.N selfs    r   
__aenter__zNoLock.__aenter__X   s        exc_typeexc_valexc_tbc                    K   yw)z'Return False, exception not suppressed.Fr*   r,   r/   r0   r1   s       r   	__aexit__zNoLock.__aexit__[   s      r.   r   N)
__name__
__module____qualname____doc__r-   r    BaseExceptionr   boolr4   r*       r   r(   r(   U   sI    J}%, % $	
 
r<   r(   bufferpeerslockc           	       K   	 	 |s\|4 d{    |r	 ddd      d{    "	 t        |        d{   }|D ]  }|j                  |        	 ddd      d{    |j                          r7 i7 X7 E# t        $ r Y ddd      d{  7   nw xY w7 ># 1 d{  7  sw Y   NxY w	 |4 d{  7   t	        |      D ]  \  }}||u s|j                  |        n |s%t        | d      r| j                          d{  7   ddd      d{  7   y# 1 d{  7  sw Y   yxY w# |4 d{  7   t	        |      D ]  \  }}||u s|j                  |        n |s%t        | d      r| j                          d{  7   ddd      d{  7   w # 1 d{  7  sw Y   w xY wxY ww)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hasattrrA   )r   r=   r>   r?   itempeer_bufferidxs          r   tee_peerrK   e   s    .( 5 5  	5 5 5
5%*8_4 ,1 5K'..t455 5" ..""' 5 5  5- 5 5 55 5 5 5 5&  	( 	($-e$4  [&(IIcN
 WXx8oo'''	( 	( 	( 	( 	(4 	( 	($-e$4  [&(IIcN
 WXx8oo'''	( 	( 	( 	( 	(sP  G	D9 A7D9 BD9 A9D9 A=A;A=BD9 BD9 9D9 ;A==	BBD9 BD9 BBD9 B1%B(&B1-D9 5G;B><G D$5D$DD$GD G$D6*D-+D62G9F> E
F>F)5F)F
F)F>"F%#F>)F;/F20F;7F>>Gc            	           e Zd ZdZ	 ddddee   dedee   dz  fdZ	defd	Z
ed
edee   fd       Zed
edeee   df   fd       Zd
eez  dee   eee   df   z  fdZdeee      fdZddZdee   dz  dedz  dedz  defdZddZy)Teea  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)r?   iterablenr?   c                     |j                          _        t        |      D cg c]  }t                c} _        t         fd j                  D               _        yc c}w )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   |   K   | ]3  }t        j                  |j                  n	t                      5 y w)N)r   r=   r>   r?   )rK   	_iterator_buffersr(   ).0r=   r?   r,   s     r   	<genexpr>zTee.__init__.<locals>.<genexpr>   sB      
  mm!-T68	 
s   9<N)	__aiter__rR   ranger   rS   tuple	_children)r,   rN   rO   r?   _s   `  ` r   __init__zTee.__init__   sR     "++-:?((CQ(C 
 --
 
 )Ds   A#r   c                 ,    t        | j                        S )z%Return the number of child iterators.)lenrY   r+   s    r   __len__zTee.__len__   s    4>>""r<   rH   c                      y r   r*   r,   rH   s     r   __getitem__zTee.__getitem__   s    :=r<   .c                      y r   r*   r`   s     r   ra   zTee.__getitem__   s    HKr<   c                      | j                   |   S )z:Return the child iterator(s) for the given index or slice.rY   r`   s     r   ra   zTee.__getitem__   s     ~~d##r<   c              #   8   K   | j                   E d{    y7 w)z\Iterate over the child iterators.

        Yields:
            The child iterators.
        Nrd   r+   s    r   __iter__zTee.__iter__   s      >>!!s   c                    K   | S w)zReturn the tee instance.r*   r+   s    r   r-   zTee.__aenter__   s     s   r/   r0   r1   c                 @   K   | j                          d{    y7 w)zcClose all child iterators.

        Returns:
            False, exceptions not suppressed.
        NF)rA   r3   s       r   r4   zTee.__aexit__   s      kkm 	s   c                 b   K   | j                   D ]  }|j                          d{     y7 w)z Async close all child iterators.N)rY   rA   )r,   childs     r   rA   z
Tee.aclose  s+     ^^ 	!E,,.  	! s   #/-/)   )r   zTee[T]r5   )r6   r7   r8   r9   r   r   intr
   r   r[   r^   r   ra   slicerX   r	   rf   r-   r    r:   r   r;   r4   rA   r*   r<   r   rM   rM      s*   #P 

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

 *#.5
6# # ==a(8= =KK%a0@#0E*FK K$%K$	q	E-"2C"78	8$"(=#34 "}%, % $	
 
!r<   rM   c            	           e Zd ZdZdeeef   ee   z  ddfdZedeeef   ee   z  fd       Z	ede
e   dz  dedz  d	edz  ddfd
       Zy)aclosingai  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                     || _         y)z\Create the context manager.

        Args:
            thing: The resource to wrap.
        Nrp   )r,   rp   s     r   r[   zaclosing.__init__'  s     
r<   c                 "   K   | j                   S wr   rr   r+   s    r   r-   zaclosing.__aenter__/  s     zzs   r/   	exc_value	tracebackc                    K   t        | j                  d      r#| j                  j                          d {    y y 7 w)NrA   )rG   rp   rA   )r,   r/   rt   ru   s       r   r4   zaclosing.__aexit__3  s4      4::x(**##%%% )%s   4?=?)r6   r7   r8   r9   r   r   r   r[   r   r-   r    r:   r   r4   r*   r<   r   ro   ro     s    *nS#X6s9KK PT  .c":]3=O"O   &}%,& !4'& !4'	&
 
& &r<   ro   sizerN   c                   K   g }|2 3 d{   }t        |      | k  r|j                  |       t        |      | k\  s7| g }?7 :6 |r| yyw)zUtility batching function for async iterables.

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

    Yields:
        The batches.
    N)r]   rC   )rw   rN   batchelements       r   abatch_iterater{   >  sa      E!  gu:LL!u:KE  s*   AAAA.AAA
A)$r9   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(   listrK   rM   ateero   rl   r{   r*   r<   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r<   