Ë
    òÏjù1  ã                  óŒ   — d dl mZ d dlZd dlmZ d dlmZmZmZm	Z	 d dl
mZmZmZ erd dlmZ  ed«      Z G d„ d	ee   «      Zy)
é    )ÚannotationsN)Údeque)ÚAsyncIteratorÚ	AwaitableÚCallableÚIterator)ÚTYPE_CHECKINGÚGenericÚTypeVar)Ú	StreamMuxÚTc                  ó„   — e Zd ZdZdddœdd„Zdd„Zdd„Zdd„Zdd„Zdd	„Z	dd
„Z
dd„Zdd„Zdd„Zdd„Zddd„Zddd„Zy)ÚStreamChannelu.  Single-consumer drainable queue for streaming events, with optional
    protocol auto-forwarding.

    When constructed with a `name`, the StreamMux auto-wires every
    `push()` to also inject a `ProtocolEvent` into the main event stream
    using the channel's name as the method. When constructed without a
    name, the channel is local-only â€” items are only visible to
    in-process consumers that iterate the channel directly.

    Items are popped off the front as the consumer advances â€” there is
    no retention beyond what's currently queued. A channel accepts
    exactly one subscriber; a second `__iter__` / `__aiter__` call
    raises. Use `tee(n)` / `atee(n)` for fan-out.

    Starts unbound â€” neither `__iter__` nor `__aiter__` is available
    until the StreamMux calls `_bind(is_async)`. After binding, only
    the matching iteration protocol works; the other raises `TypeError`.

    Pump wiring (set by the run stream, not by `_bind`):
        - `_request_more`: sync pump callable, returns True if a new
          event was produced.
        - `_arequest_more`: async pump coroutine factory, same contract.

    Memory is bounded by caller pace: both sync and async use caller-
    driven pumps, so each cursor advance produces at most one event.

    Lazy-subscribe: `push` appends to the local buffer only when a
    subscriber has registered. Auto-forward via `_wire_fn` always fires
    regardless of subscription state.

    Lifecycle (`close` / `fail`) is managed by the mux â€” transformers
    don't need to close their channels manually.
    N)Úmaxlenc               óÒ   — |�|dk  rt        d«      ‚|| _        t        «       | _        || _        d| _        d| _        d| _        d| _        d| _	        d| _
        d| _        d| _        y)aô  Initialize the channel.

        Args:
            name: Optional protocol channel name. When set, the
                StreamMux wires every `push()` to also inject a
                `ProtocolEvent` into the main event stream. Surfaced
                on the wire as `custom:<name>` for user-defined
                transformers, or as `<name>` for channels owned by a
                native transformer (`_native = True`). When `None`,
                the channel is local-only.
            maxlen: Accepted for forward compatibility; currently
                unused. The caller-driven pump bounds memory naturally
                for single-consumer use.

        Raises:
            ValueError: If `maxlen` is not a positive integer or `None`.
        Nr   z3StreamChannel maxlen must be a positive int or NoneF)Ú
ValueErrorÚnamer   Ú_itemsÚ_maxlenÚ_closedÚ_errorÚ	_is_asyncÚ_subscribedÚ_request_moreÚ_arequest_moreÚ_wire_fnÚ_mux)Úselfr   r   s      ú\/var/www/html/gaurav-v2/venv/lib/python3.12/site-packages/langgraph/stream/stream_channel.pyÚ__init__zStreamChannel.__init__1   sn   € ð$ Ð &¨A¢+ÜÐRÓSÐSØˆŒ	Ü,1«GˆŒØ#)ˆŒØˆŒØ,0ˆŒà&*ˆŒà ˆÔà8<ˆÔØDHˆÔà48ˆŒØ&*ˆ�	ó    c                ó   — || _         y ©N)r   )r   Úmuxs     r   Ú	_bind_muxzStreamChannel._bind_muxY   s	   € Øˆ�	r!   c               ó@   — | j                   �t        d«      ‚|| _         y)aS  Bind this channel to sync or async mode.

        Called by the StreamMux after transformer registration. Must be
        called exactly once before any iteration.

        Args:
            is_async: True to enable async iteration, False for sync.

        Raises:
            RuntimeError: If the channel has already been bound.
        NzStreamChannel is already bound)r   ÚRuntimeError)r   Úis_asyncs     r   Ú_bindzStreamChannel._bind\   s!   € ð �>‰>Ð%ÜÐ?Ó@Ð@Ø!ˆ�r!   c                ó   — || _         y)z8Install the auto-forward callback (called by StreamMux).N)r   )r   Úfns     r   Ú_wirezStreamChannel._wirep   s	   € àˆ�r!   c                ó  — | j                   r\| j                  rt        d«      ‚| j                  �| j                  j	                  «       nd}| j
                  j                  ||f«       | j                  �| j                  |«       yy)a$  Append an item. Auto-forwards if wired.

        The local buffer append is a no-op when no subscriber is
        registered, but auto-forwarding always fires so wired events
        reach the main event log regardless of subscription state.

        Items are stored as `(stamp, item)` tuples where stamp is a
        monotonic counter from the owning mux. Stamps are stripped by
        the default cursors; raw stamped tuples are visible on `_items`.

        Raises:
            RuntimeError: If the channel is closed (and subscribed).
        z%Cannot push to a closed StreamChannelNr   )r   r   r'   r   Ú_next_push_seqr   Úappendr   )r   ÚitemÚstamps      r   ÚpushzStreamChannel.pushx   sn   € ð ×ÒØ�|Š|Ü"Ð#JÓKÐKØ26·)±)Ð2G�D—I‘I×,Ñ,Ô.ÈQˆEØ�K‰K×Ñ  t˜}Ô-Ø�=‰=Ð$Ø�M‰M˜$Õð %r!   c                ó   — d| _         y)zMark the channel as complete.TN)r   ©r   s    r   ÚclosezStreamChannel.closeŽ   s	   € àˆ�r!   c                ó    — || _         d| _        y)zqMark the channel as errored.

        Args:
            err: The exception to surface to the subscriber.
        TN)r   r   )r   Úerrs     r   ÚfailzStreamChannel.fail’   s   € ð ˆŒØˆ�r!   c                óº   — | j                   €t        d«      ‚| j                   rt        d«      ‚| j                  rt        d«      ‚d| _        | j	                  «       S )zâSubscribe and return a sync cursor. Can be called only once.

        Raises:
            TypeError: If the channel is unbound or bound to async mode.
            RuntimeError: If the channel already has a subscriber.
        úVStreamChannel has not been bound yet. Register the transformer with a StreamMux first.uF   This StreamChannel is bound to async mode â€” use 'async for' instead.z@StreamChannel already has a subscriber; use .tee(n) for fan-out.T)r   Ú	TypeErrorr   r'   Ú_sync_cursorr4   s    r   Ú__iter__zStreamChannel.__iter__Ÿ   sn   € ð �>‰>Ð!ÜðCóð ð �>Š>ÜØXóð ð ×ÒÜØRóð ð  ˆÔØ× Ñ Ó"Ð"r!   c              #  ó   K  — 	 | j                   r"| j                   j                  «       \  }}|–— n[| j                  r| j                  �| j                  ‚y | j                  �)| j	                  «       s| j                   s| j                  sy y ŒŠ­wr#   )r   Úpopleftr   r   r   ©r   Ú_stampr0   s      r   r<   zStreamChannel._sync_cursor¶   sy   è ø€ ØØ�{Š{Ø#Ÿ{™{×2Ñ2Ó4‘�˜Ø“
Ø—’Ø—;‘;Ð*ØŸ+™+Ð%ØØ×#Ñ#Ð/Ø×)Ñ)Ô+ØŸ;š;¨t¯|ª|Øàð ùs   ‚BBc                óº   — | j                   €t        d«      ‚| j                   st        d«      ‚| j                  rt        d«      ‚d| _        | j	                  «       S )zãSubscribe and return an async cursor. Can be called only once.

        Raises:
            TypeError: If the channel is unbound or bound to sync mode.
            RuntimeError: If the channel already has a subscriber.
        r:   u?   This StreamChannel is bound to sync mode â€” use 'for' instead.zAStreamChannel already has a subscriber; use .atee(n) for fan-out.T)r   r;   r   r'   Ú_async_cursorr4   s    r   Ú	__aiter__zStreamChannel.__aiter__Ê   sn   € ð �>‰>Ð!ÜðCóð ð �~Š~ÜØQóð ð ×ÒÜØSóð ð  ˆÔØ×!Ñ!Ó#Ð#r!   c               ó6  K  — 	 | j                   r#| j                   j                  «       \  }}|­–— nc| j                  r| j                  �| j                  ‚y | j                  �1| j	                  «       ƒ d {  –—† s| j                   s| j                  sy y Œ“7 Œ­wr#   )r   r?   r   r   r   r@   s      r   rC   zStreamChannel._async_cursorá   s„   è ø€ ØØ�{Š{Ø#Ÿ{™{×2Ñ2Ó4‘�˜Ø”
Ø—’Ø—;‘;Ð*ØŸ+™+Ð%ØØ×$Ñ$Ð0Ø!×0Ñ0Ó2×2Ð2ØŸ;š;¨t¯|ª|Øàð ð 3ús   ‚A5BÁ7BÁ8 Bc                óæ   ‡‡‡‡— |dk  rt        d«      ‚| j                  «       Št        |«      D �cg c]  }t        «       ‘Œ c}ŠdgŠdˆˆˆfd„Št	        ˆfd„t        |«      D «       «      S c c}w )a{  Subscribe and return `n` independent sync iterators.

        Each branch has its own buffer; items pulled from the
        underlying cursor are copied into every branch. Branches are
        naturally bounded by caller pace since the sync pump is
        caller-driven.

        Args:
            n: Number of branches to create. Must be >= 1.

        Returns:
            A tuple of `n` iterators over the same underlying stream.

        Raises:
            TypeError: If the channel is unbound or bound to async mode.
            RuntimeError: If the channel already has a subscriber.
            ValueError: If `n` < 1.
        é   ztee() requires n >= 1Fc              3  óÀ   •K  — ‰|    }	 |r|j                  «       –— n*‰d   ry 	 t        ‰«      }‰D ]  }|j                  |«       Œ Œ@# t        $ r d‰d<   Y y w xY w­w©NTr   )r?   ÚnextÚStopIterationr/   )ÚiÚbufr0   ÚbÚbuffersÚ	exhaustedÚsources       €€€r   Úbranchz!StreamChannel.tee.<locals>.branch  sr   øè ø€ Ø˜!‘*ˆCØÙØŸ+™+›-Ó'Ø˜q’\ØðÜ# F›|˜ó %˜ØŸ™ �ð %ð øô )ò Ø'+˜	 !™Ùðüs'   ƒ"A¦A
 ±AÁ
AÁAÁAÁAc              3  ó.   •K  — | ]  } ‰|«      –— Œ y ­wr#   © ©Ú.0rL   rR   s     €r   Ú	<genexpr>z$StreamChannel.tee.<locals>.<genexpr>  ó   øè ø€ Ð1© 1‘V˜A—Y©ùó   ƒ)rL   ÚintÚreturnúIterator[T])r   r=   Úranger   Útuple)r   ÚnÚ_rR   rO   rP   rQ   s      @@@@r   ÚteezStreamChannel.teeõ   se   û€ ð& ˆqŠ5ÜÐ4Ó5Ð5Ø—‘“ˆÜ49¸!´HÓ"=±H¨q¤5¥7°HÑ"=ˆØ�Gˆ	÷	'ô  Ó1¬¨a¬Ó1Ó1Ð1ùò' #>s   ²A.c                ó  ‡‡‡‡‡‡— |dk  rt        d«      ‚| j                  «       Št        |«      D �cg c]  }t        «       ‘Œ c}ŠdgŠdgŠt	        j
                  «       Šdˆˆˆˆˆfd„Št        ˆfd„t        |«      D «       «      S c c}w )až  Subscribe and return `n` independent async iterators.

        Caller-driven fan-out: each branch's `__anext__` either pops
        from its own buffer or, under a shared `asyncio.Lock`, pulls
        one item from the underlying cursor and distributes it to
        every branch's buffer.

        Args:
            n: Number of branches to create. Must be >= 1.

        Returns:
            A tuple of `n` async iterators over the same underlying
            stream.

        Raises:
            TypeError: If the channel is unbound or bound to sync mode.
            RuntimeError: If the channel already has a subscriber.
            ValueError: If `n` < 1.
        rG   zatee() requires n >= 1FNc               ó  •K  — ‰|    }	 |r|j                  «       ­–— Œ‰d   r‰d   �‰d   ‚y ‰4 ƒd {  –—†  |s‰d   r	 d d d «      ƒd {  –—†  ŒK	 ‰	j                  «       ƒ d {  –—† }‰D ]  }|j	                  |«       Œ d d d «      ƒd {  –—†  ŒŒ7 Œ_7 ŒI7 Œ1# t        $ r d‰d<   Y d d d «      ƒd {  –—†7   Œ´t        $ r%}|‰d<   d‰d<   Y d }~d d d «      ƒd {  –—†7   ŒÜd }~ww xY w7 Œ\# 1 ƒd {  –—†7  sw Y   ŒlxY w­wrI   )r?   Ú	__anext__ÚStopAsyncIterationÚ	Exceptionr/   )
rL   rM   r0   ÚerN   rO   ÚerrorrP   ÚlockrQ   s
        €€€€€r   rR   z"StreamChannel.atee.<locals>.branch<  sü   øè ø€ Ø˜!‘*ˆCØÙØŸ+™+›-Ó'ØØ˜Q’<Ø˜Q‘xÐ+Ø# A™h˜Øßš4Ù˜i¨šlØ ÷  Ÿ4˜4ð!Ø%+×%5Ñ%5Ó%7×7˜ó %˜ØŸ™ �ð %÷  Ÿ4ð ð  ø˜4øð  8ùÜ-ò !Ø'+˜	 !™Ø ÷  Ÿ4™4ô %ò !Ø#$˜˜a™Ø'+˜	 !™Û ÷  Ÿ4™4ûð!úð  øŸ4Ÿ4™4üsÈ   ƒ3D¶B·Dº	C/ÁDÁBÁDÁBÁ(BÁ)BÁ-C/ÂDÂC-ÂDÂDÂBÂC*Â*C/Â+DÂ6B9Â7DÂ=C*Ã
C%ÃC/ÃDÃC!ÃDÃ%C*Ã*C/Ã-DÃ/DÃ5C8Ã6DÃ=Dc              3  ó.   •K  — | ]  } ‰|«      –— Œ y ­wr#   rT   rU   s     €r   rW   z%StreamChannel.atee.<locals>.<genexpr>U  rX   rY   )rL   rZ   r[   úAsyncIterator[T])r   rD   r]   r   ÚasyncioÚLockr^   )	r   r_   r`   rR   rO   rh   rP   ri   rQ   s	      @@@@@@r   ÚateezStreamChannel.atee   s}   ý€ ð( ˆqŠ5ÜÐ5Ó6Ð6Ø—‘Ó!ˆÜ49¸!´HÓ"=±H¨q¤5¥7°HÑ"=ˆØ�Gˆ	Ø-1¨FˆÜ�|‰|‹~ˆ÷	'ñ 	'ô2 Ó1¬¨a¬Ó1Ó1Ð1ùò= #>s   ´B	r#   )r   z
str | Noner   z
int | Noner[   ÚNone)r$   r   r[   ro   )r(   Úboolr[   ro   )r+   zCallable[[T], None]r[   ro   )r0   r   r[   ro   )r[   ro   )r7   ÚBaseExceptionr[   ro   )r[   r\   )r[   rk   )é   )r_   rZ   r[   ztuple[Iterator[T], ...])r_   rZ   r[   ztuple[AsyncIterator[T], ...])Ú__name__Ú
__module__Ú__qualname__Ú__doc__r    r%   r)   r,   r2   r5   r8   r=   r<   rD   rC   ra   rn   rT   r!   r   r   r      sR   „ ñ ðD"+Èõ "+óPó"ó(ó ó,óó#ó.ó($ó.ô()2õV52r!   r   )Ú
__future__r   rl   Úcollectionsr   Úcollections.abcr   r   r   r   Útypingr	   r
   r   Úlanggraph.stream._muxr   r   r   rT   r!   r   Ú<module>r|      s;   ðÝ "ã Ý ß HÓ Hß 2Ñ 2áÝ/áˆCƒL€ôG2�G˜A‘Jõ G2r!   