
    _j                    @    S SK Jr  S SKrS SKrSSKJr   " S S5      rg)    )annotationsN   )WebSocketQueueFullErrorc                  b    \ rS rSrSrSSS jjrSS jrSS jrSS jrSS jr	SS jr
SS	 jrS
rg)	SendQueue   zBounded byte-size queue for outgoing WebSocket messages.

Messages are stored as pre-serialized strings. The queue enforces a
maximum byte budget so that unbounded buffering cannot occur during
reconnection windows.
c                `    / U l         SU l        Xl        [        R                  " 5       U l        g Nr   )_queue_bytes
_max_bytes	threadingLock_lock)self	max_bytess     G/home/rurouni/.local/lib/python3.13/site-packages/openai/_send_queue.py__init__SendQueue.__init__   s#    -/#^^%
    c                6   [        UR                  S5      5      nU R                     U R                  U-   U R                  :  a  [        S5      eU R                  R                  X45        U =R                  U-  sl        SSS5        g! , (       d  f       g= f)zvAppend *data* to the queue.

Raises :class:`WebSocketQueueFullError` if the message would
exceed the byte-size limit.
zutf-8z%send queue is full, message discardedN)lenencoder   r   r   r   r   append)r   databyte_lengths      r   enqueueSendQueue.enqueue   sl     $++g./ZZ{{[(4??:-.UVVKK23KK;&K	 ZZs   AB


Bc                   U R                      [        U R                  5      nU R                  R                  5         SU l        SSS5        [        W5       H  u  nu  pE U" U5        M     g! , (       d  f       N/= f! [         a`    U R                      X#S nX`R                  -   U l        [        S U R                   5       5      U l        SSS5        e ! , (       d  f       e = ff = f)zSend every queued message via *send*.

If *send* raises, the failing message and all subsequent messages
are re-queued and the error is re-raised.
r   Nc              3  *   #    U  H	  u  pUv   M     g 7fN .0_bls      r   	<genexpr>'SendQueue.flush_sync.<locals>.<genexpr>8        %BkUQbk   r   listr   clearr   	enumerate	Exceptionsumr   sendpendingir   _byte_length	remainings          r   
flush_syncSendQueue.flush_sync&   s     ZZ4;;'GKKDK 
 (1'9#A#T
 (: Z  ZZ 'I"+kk"9DK"%%Bdkk%B"BDK   	  Z s/   7A- A>-
A;>C(9C	C(
C$ C(c                  #    U R                      [        U R                  5      nU R                  R                  5         SU l        SSS5        [        W5       H  u  nu  pE U" U5      I Sh  vN   M     g! , (       d  f       N7= f N! [         a`    U R                      X#S nX`R                  -   U l        [        S U R                   5       5      U l        SSS5        e ! , (       d  f       e = ff = f7f)z$Async variant of :meth:`flush_sync`.r   Nc              3  *   #    U  H	  u  pUv   M     g 7fr!   r"   r#   s      r   r'   (SendQueue.flush_async.<locals>.<genexpr>I   r)   r*   r+   r1   s          r   flush_asyncSendQueue.flush_async;   s     ZZ4;;'GKKDK 
 (1'9#A#4j   (: Z ! ZZ 'I"+kk"9DK"%%Bdkk%B"BDK   	  Z s^   C77A7C7"B
-B.B
2C77
BC7B

C4 9C"	C4"
C0,C44C7c                    U R                      U R                   VVs/ s H  u  pUPM	     nnnU R                  R                  5         SU l        UsSSS5        $ s  snnf ! , (       d  f       g= f)z&Remove and return all queued messages.r   N)r   r   r-   r   )r   r   r%   itemss       r   drainSendQueue.drainL   sO    ZZ)-5gdTE5KKDK	 Z5 Zs   A A%A A  
A.c                z    U R                      [        U R                  5      sS S S 5        $ ! , (       d  f       g = fr!   r   r   r   r   s    r   __len__SendQueue.__len__T   s    ZZt{{# ZZs   ,
:c                    U R                      [        U R                  5      S:  sS S S 5        $ ! , (       d  f       g = fr
   rC   rD   s    r   __bool__SendQueue.__bool__X   s$    ZZt{{#a' ZZs   /
=)r   r   r   r   N)i   )r   intreturnNone)r   strrK   rL   )r2   ztyping.Callable[[str], object]rK   rL   )r2   z0typing.Callable[[str], typing.Awaitable[object]]rK   rL   )rK   z	list[str])rK   rJ   )rK   bool)__name__
__module____qualname____firstlineno____doc__r   r   r7   r<   r@   rE   rH   __static_attributes__r"   r   r   r   r      s*    &'*"$(r   r   )
__future__r   typingr   _exceptionsr   r   r"   r   r   <module>rX      s    #   0O( O(r   