o
    ùT·j÷Ú  ã                   @  s(  U d dl mZ d dlZd dlZd dlZd dl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 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mZm Z  dd
l!m"Z" ddl#m$Z$m%Z% ddl&m'Z'm(Z( ddl)m*Z*m+Z+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9m:Z:m;Z; ddl<m=Z= ddl>m?Z? ddl@mAZA ddlBmCZC ddlDmEZE e	rÛd dlFmGZH d dlImGZJ ddlKmLZLmMZM ddgZNe OeP¡ZQdeRd< G d d„ de'ƒZSG d!d„ de(ƒZTG d"d#„ d#ƒZUG d$d%„ d%ƒZVG d&d'„ d'ƒZWG d(d)„ d)ƒZXG d*d+„ d+ƒZYG d,d-„ d-eYƒZZG d.d/„ d/eYƒZ[G d0d1„ d1eYƒZ\G d2d3„ d3eYƒZ]G d4d5„ d5eYƒZ^G d6d7„ d7eYƒZ_G d8d9„ d9eYƒZ`G d:d;„ d;ƒZaG d<d=„ d=eaƒZbG d>d?„ d?eaƒZcG d@dA„ dAeaƒZdG dBdC„ dCeaƒZeG dDdE„ dEeaƒZfG dFdG„ dGeaƒZgG dHdI„ dIeaƒZhdS )Jé    )ÚannotationsN)ÚTracebackType)ÚTYPE_CHECKINGÚAnyÚUnionÚCallableÚIteratorÚOptionalÚ	AwaitableÚcast)ÚAsyncIterator)Ú	BaseModelé   )ÚOmitÚQueryÚHeadersÚomit)Úis_givenÚpath_templateÚmaybe_transformÚstrip_not_givenÚasync_maybe_transform)Úcached_property)ÚFinalRequestOptionsÚconstruct_type_unchecked)ÚSyncAPIResourceÚAsyncAPIResource)ÚOpenAIErrorÚWebSocketConnectionClosedError)Ú	SendQueue)Ú_merge_mappings)ÚEventHandlerRegistry)Ú
ErrorEvent)ÚForkClientEvent)ÚForkServerEvent)ÚReconnectingEventÚReconnectingOverridesÚis_recoverable_close)ÚForkClientEventParam)ÚWebSocketConnectionOptions)ÚForkSessionConfigParam)ÚSessionUpdateConfigParam)ÚResponseInputItemParam)ÚClientConnection)ÚOpenAIÚAsyncOpenAIÚForksÚ
AsyncForkszlogging.LoggerÚlogc                	   @  ó*   e Zd Zi i i ddddddœddd„ZdS )r0   Né   ç      à?ç       @é   ©Úextra_queryÚextra_headersÚwebsocket_connection_optionsÚon_reconnectingÚmax_retriesÚinitial_delayÚ	max_delayÚmax_queue_sizeÚ
session_idÚstrr9   r   r:   r   r;   r)   r<   úBCallable[[ReconnectingEvent], ReconnectingOverrides | None] | Noner=   Úintr>   Úfloatr?   r@   ÚreturnÚForksConnectionManagerc       	   
      C  ó   t | j||||||||	|d�
S ©zµFork a stored Live session over WebSocket.

        Send session.start with an overrides object (which may be empty), then wait for session.started. The model is inherited.
        )
Úclientr9   r:   r;   r<   r=   r>   r?   r@   rA   )rG   Ú_client©
ÚselfrA   r9   r:   r;   r<   r=   r>   r?   r@   © rN   ú^/home/dinkstrade/pdmp-scanner/venv/lib/python3.10/site-packages/openai/resources/live/forks.pyÚconnect/   ó   özForks.connect)rA   rB   r9   r   r:   r   r;   r)   r<   rC   r=   rD   r>   rE   r?   rE   r@   rD   rF   rG   ©Ú__name__Ú
__module__Ú__qualname__rP   rN   rN   rN   rO   r0   .   ó    õc                	   @  r3   )r1   Nr4   r5   r6   r7   r8   rA   rB   r9   r   r:   r   r;   r)   r<   rC   r=   rD   r>   rE   r?   r@   rF   ÚAsyncForksConnectionManagerc       	   
      C  rH   rI   )rW   rK   rL   rN   rN   rO   rP   O   rQ   zAsyncForks.connect)rA   rB   r9   r   r:   r   r;   r)   r<   rC   r=   rD   r>   rE   r?   rE   r@   rD   rF   rW   rR   rN   rN   rN   rO   r1   N   rV   c                	   @  óÚ   e Zd ZU dZded< ded< ded< ddd	d
di i ddœdQdd „ZdRd"d#„ZdSd%d&„ZdTd(d)„ZdUd,d-„Z	dVd0d1„Z
d2d3d4œdWd8d9„ZdXd;d<„ZdYd@dA„ZdZdBdC„Z	d[d\dHdI„Zd]dKdL„Z	d[d\dMdN„ZdZdOdP„ZdS )^ÚAsyncForksConnectionú7Represents a live WebSocket connection to the Forks APIÚAsyncForksSessionResourceÚsessionÚAsyncForksResponseResourceÚresponseÚAsyncWebSocketConnectionÚ_connectionNr4   r5   r6   ©Úmake_wsr<   r=   r>   r?   r9   r:   Ú
send_queueÚ
connectionrb   úFCallable[[Query, Headers], Awaitable[AsyncWebSocketConnection]] | Noner<   rC   r=   rD   r>   rE   r?   r9   r   r:   r   rc   úSendQueue | NonerF   ÚNonec          
      C  sr   || _ || _|| _|| _d| _|| _|| _|| _|| _d| _	d| _
|	p%tƒ | _tdd�| _t| ƒ| _t| ƒ| _d S )Nr   F©Úuse_lock)r`   Ú_make_wsÚ_on_reconnectingÚ_max_retriesÚ_reconnect_attemptÚ_initial_delayÚ
_max_delayÚ_extra_queryÚ_extra_headersÚ_intentionally_closedÚ_is_reconnectingr   Ú_send_queuer!   Ú_event_handler_registryr[   r\   r]   r^   ©
rM   rd   rb   r<   r=   r>   r?   r9   r:   rc   rN   rN   rO   Ú__init__v   ó   
zAsyncForksConnection.__init__úAsyncIterator[ForkServerEvent]c              
   C s�   �ddl m}m} 	 z
|  ¡ I dH V  W n2 |y   Y dS  |yF } z|  |¡I dH s<| j ¡ }|r;td|d�|‚‚ W Y d}~nd}~ww q
©zq
        An infinite-iterator that will continue to yield events until
        the connection is closed.
        r   )ÚConnectionClosedOKÚConnectionClosedErrorTNz0WebSocket connection closed with unsent messages)Úunsent_messages©Úwebsockets.exceptionsr{   r|   ÚrecvÚ
_reconnectrt   Údrainr   ©rM   r{   r|   ÚexcÚunsentrN   rN   rO   Ú	__aiter__”   s,   €
þýù€ÿûzAsyncForksConnection.__aiter__r$   c                 Ã  sb   �|   |  ¡ I dH ¡}ttt|ƒtƒrtd|ƒ d¡nt|ddƒ}t|tƒr/|r/|dkr/d| _	|S )úµ
        Receive the next message from the connection and parses it into a `ForkServerEvent` object.

        Canceling this method is safe. There's no risk of losing data.
        Núdict[str, object]ÚtypeÚerrorr   ©
Úparse_eventÚ
recv_bytesÚ
isinstancer   ÚobjectÚdictÚgetÚgetattrrB   rm   ©rM   ÚeventÚ
event_typerN   rN   rO   r€   ª   s   €ÿ
ýzAsyncForksConnection.recvÚbytesc              	   Ã  s”   �| j jdd�I dH }t dt|ƒ¡ | jrHzt |¡}W n tt	fy*   | Y S w t
|tƒr8td|ƒ d¡nd}t
|tƒrH|rH|dkrHd| _|S )	á  Receive the next message from the connection as raw bytes.

        Canceling this method is safe. There's no risk of losing data.

        If you want to parse the message into a `ForkServerEvent` object like `.recv()` does,
        then you can call `.parse_event(data)`.
        F©ÚdecodeNú$Received WebSocket message: %i bytesrˆ   r‰   rŠ   r   ©r`   r€   r2   ÚdebugÚlenrm   ÚjsonÚloadsÚ
ValueErrorÚRecursionErrorrŽ   r�   r   r‘   rB   ©rM   ÚmessageÚ
event_datar•   rN   rN   rO   r�   ¼   s   €ÿzAsyncForksConnection.recv_bytesr”   ú&ForkClientEvent | ForkClientEventParamc                 Ã  s~   �t |tƒr|jdddd�n
t t|tƒI d H ¡}| jr$| j 	|¡ d S z| j
 |¡I d H  W d S  ty>   | j 	|¡ ‚ w ©NT©Úuse_api_namesÚexclude_defaultsÚexclude_unset)rŽ   r   Úto_jsonrž   Údumpsr   r(   rs   rt   Úenqueuer`   ÚsendÚ	Exception©rM   r”   ÚdatarN   rN   rO   r®   Ñ   s   €ÿýþzAsyncForksConnection.sendr±   úbytes | strc                 Ã  sF   �| j rt|tƒr|n| d¡}| j |¡ d S | j |¡I d H  d S ©Núutf-8©rs   rŽ   rB   r™   rt   r­   r`   r®   ©rM   r±   ÚrawrN   rN   rO   Úsend_rawà   s   €zAsyncForksConnection.send_rawéè  Ú ©ÚcodeÚreasonr¼   r½   rB   c                Ã  s"   �d| _ | jj||d�I d H  d S ©NTr»   ©rr   r`   Úclose©rM   r¼   r½   rN   rN   rO   rÀ   ç   s   €zAsyncForksConnection.closeústr | bytesc                 C  ó   t ttt |¡t ttƒd�ƒS ©z”
        Converts a raw `str` or `bytes` message into a `ForkServerEvent` object.

        This is helpful if you're using `.recv_bytes()`.
        )ÚvalueÚtype_©r   r$   r   rž   rŸ   r   ©rM   r±   rN   rN   rO   rŒ   ë   ó   z AsyncForksConnection.parse_eventr„   r¯   Úboolc              	   Ã  sÒ  �ddl }| jdu s| jdu rdS ddlm} d}t||ƒr'|jdur'|jj}t|ƒs-dS d| _	t
| jd | jd ƒD ]¨}|| _t| jd|d   | jƒ}d	t ¡ d
  }|| }t|| j||| j| jd�}	z|  |	¡}
W n ty{   d| _	Y  dS w |
dur‹|
 d¡r‹d| _	 dS |
dur¡d|
v r˜|
d | _d|
v r¡|
d | _t d|| j|¡ | |¡I dH  | jr»d| _	 dS z|  | j| j¡I dH | _t d¡ d| _	|  ¡ I dH  W  dS  tyã   Y q;w d| _	dS )úÀAttempt to reconnect after a connection failure.

        Returns ``True`` if a new connection was established, ``False`` if the
        caller should re-raise the original exception.
        r   NF©r|   éî  Té   é   ç      è?ç      Ð?©ÚattemptÚmax_attemptsÚdelayÚ
close_coder9   r:   Úabortr9   r:   ú?Reconnecting to WebSocket API (attempt %d/%d) after %.1fs delayúReconnected to WebSocket API)Úasynciork   rj   r   r|   rŽ   Úrcvdr¼   r'   rs   Úrangerm   rl   Úminrn   ro   Úrandomr%   rp   rq   r¯   r‘   r2   ÚinfoÚsleeprr   r`   Ú_flush_send_queue)rM   r„   rÚ   r|   rÖ   rÓ   Ú
base_delayÚjitterrÕ   r”   ÚresultrN   rN   rO   r�   ó   sv   €ú	þ

ü
ÿzAsyncForksConnection._reconnectc                 ƒ  sH   �d	‡ fdd„}zˆ j  |¡I dH  W dS  ty#   t d¡ Y dS w )
ú5Send all queued messages over the current connection.r±   rB   rF   rg   c                 “  s   �ˆ j  | ¡I d H  d S ©N©r`   r®   ©r±   ©rM   rN   rO   Ú_sendC  s   €z5AsyncForksConnection._flush_send_queue.<locals>._sendNú*Failed to flush send queue after reconnect)r±   rB   rF   rg   )rt   Úflush_asyncr¯   r2   Úwarning)rM   rê   rN   ré   rO   rá   @  s   €ÿz&AsyncForksConnection._flush_send_queuer•   ÚhandlerúCallable[..., Any] | NoneúOUnion[AsyncForksConnection, Callable[[Callable[..., Any]], Callable[..., Any]]]c                   ó.   |durˆj  ˆ |¡ ˆS d‡ ‡fdd„}|S )a-  Adds the handler to the end of the handlers list for the given event type.

        No checks are made to see if the handler has already been added. Multiple calls
        passing the same combination of event type and handler will result in the handler
        being added, and called, multiple times.

        Can be used as a method (returns ``self`` for chaining)::

            connection.on("session.started", my_handler)

        Or as a decorator::

            @connection.on("session.started")
            async def my_handler(event): ...
        NÚfnúCallable[..., Any]rF   c                   ó   ˆj  ˆ | ¡ | S ræ   ©ru   Úadd©rò   ©r•   rM   rN   rO   Ú	decoratora  ó   z*AsyncForksConnection.on.<locals>.decorator©rò   ró   rF   ró   rõ   ©rM   r•   rî   rù   rN   rø   rO   ÚonK  ó
   zAsyncForksConnection.onró   c                 C  ó   | j  ||¡ | S ©z-Remove a previously registered event handler.©ru   Úremove©rM   r•   rî   rN   rN   rO   Úoffg  ó   zAsyncForksConnection.offc                   ó2   |durˆj jˆ |dd� ˆS d	‡ ‡fdd„}|S )
úbRegister a one-time event handler.

        Automatically removed after first invocation.
        NT©Úoncerò   ró   rF   c                   ó   ˆj jˆ | dd� | S ©NTr  rõ   r÷   rø   rN   rO   rù   w  ó   z,AsyncForksConnection.once.<locals>.decoratorrû   rõ   rü   rN   rø   rO   r	  l  ó
   zAsyncForksConnection.oncec                 Ã  s´   �ddl }| 2 zO3 dH W }|j}| j |¡}| j d¡}|dkr0|s0|s0t|tƒr0td|› �ƒ‚|D ]}||ƒ}| |¡rB|I dH  q2|D ]}||ƒ}| |¡rU|I dH  qEq6 dS )al  Run the event loop, dispatching received events to registered handlers.

        Blocks until the connection is closed. This is the push-based
        alternative to iterating with ``async for event in connection``.

        If an ``"error"`` event arrives and no handler is registered for
        ``"error"`` or ``"event"``, an ``OpenAIError`` is raised.
        r   Nr”   rŠ   úWebSocket error: )rÚ   r‰   ru   Úget_handlersrŽ   r"   r   Úiscoroutine)rM   rÚ   r”   r•   ÚspecificÚgenericrî   rä   rN   rN   rO   Údispatch_events}  s*   €	


€

€ýòz$AsyncForksConnection.dispatch_events)rd   r_   rb   re   r<   rC   r=   rD   r>   rE   r?   rE   r9   r   r:   r   rc   rf   rF   rg   )rF   ry   ©rF   r$   ©rF   r–   ©r”   r¥   rF   rg   ©r±   r²   rF   rg   ©r¼   rD   r½   rB   rF   rg   ©r±   rÂ   rF   r$   ©r„   r¯   rF   rÊ   ©rF   rg   ræ   )r•   rB   rî   rï   rF   rð   )r•   rB   rî   ró   rF   rY   )rS   rT   rU   Ú__doc__Ú__annotations__rw   r†   r€   r�   r®   r¸   rÀ   rŒ   r�   rá   rý   r  r	  r  rN   rN   rN   rO   rY   n   s:   
 õ







Mÿ
ÿrY   c                   @  ó„   e Zd ZdZddddddœd>dd„Zd?d d!„Z	d@dAd&d'„ZdBd)d*„Z	d@dAd+d,„ZdCd.d/„Z	e	Z
dDd1d2„ZdEd4d5„ZdFd<d=„ZdS )GrW   a*  
    Context manager over a `AsyncForksConnection` that is returned by `live.forks.connect()`

    This context manager ensures that the connection will be closed when it exits.

    ---

    Note that if your application doesn't work well with the context manager approach then you
    can call the `.enter()` method directly to initiate a connection.

    **Warning**: You must remember to close the connection with `.close()`.

    ```py
    connection = await client.live.forks.connect(...).enter()
    # ...
    await connection.close()
    ```
    Nr4   r5   r6   r7   ©r<   r=   r>   r?   r@   rJ   r/   rA   rB   r9   r   r:   r   r;   r)   r<   rC   r=   rD   r>   rE   r?   r@   rF   rg   c       
         C  óX   || _ || _d | _|| _|| _|| _|| _|| _|| _|	| _	t
|
d�| _tdd�| _d S )N©Ú	max_bytesFrh   )Ú$_AsyncForksConnectionManager__clientÚ(_AsyncForksConnectionManager__session_idÚ(_AsyncForksConnectionManager__connectionÚ)_AsyncForksConnectionManager__extra_queryÚ+_AsyncForksConnectionManager__extra_headersÚ:_AsyncForksConnectionManager__websocket_connection_optionsÚ-_AsyncForksConnectionManager__on_reconnectingÚ)_AsyncForksConnectionManager__max_retriesÚ+_AsyncForksConnectionManager__initial_delayÚ'_AsyncForksConnectionManager__max_delayr   Ú(_AsyncForksConnectionManager__send_queuer!   Ú4_AsyncForksConnectionManager__event_handler_registry©rM   rJ   rA   r9   r:   r;   r<   r=   r>   r?   r@   rN   rN   rO   rw   °  ó   z$AsyncForksConnectionManager.__init__r”   r¥   c                 C  ó:   t |tƒr|jdddd�nt t|tƒ¡}| j |¡ dS ©zÜQueue a message to be sent when the connection is established.

        This can be called before entering the context manager. Queued messages
        are automatically sent once the WebSocket connection opens.
        Tr§   N)	rŽ   r   r«   rž   r¬   r   r(   r-  r­   r°   rN   rN   rO   r®   Ë  ó
   ÿýz AsyncForksConnectionManager.sendr•   rî   rï   úVUnion[AsyncForksConnectionManager, Callable[[Callable[..., Any]], Callable[..., Any]]]c                   rñ   )zÛRegister an event handler before the connection is established.

        Handlers are transferred to the connection on enter. Supports the
        same method and decorator forms as ``AsyncForksConnection.on``.
        Nrò   ró   rF   c                   rô   ræ   ©r.  rö   r÷   rø   rN   rO   rù   ä  rú   z1AsyncForksConnectionManager.on.<locals>.decoratorrû   r5  rü   rN   rø   rO   rý   Ø  ó
   zAsyncForksConnectionManager.onró   c                 C  rÿ   r   )r.  r  r  rN   rN   rO   r  ê  r  zAsyncForksConnectionManager.offc                   r  )
úGRegister a one-time event handler before the connection is established.NTr  rò   ró   rF   c                   r
  r  r5  r÷   rø   rN   rO   rù   ÷  r  z3AsyncForksConnectionManager.once.<locals>.decoratorrû   r5  rü   rN   rø   rO   r	  ï  ó
   z AsyncForksConnectionManager.oncerY   c                 Ã  sx   �|   | j| j¡I dH }t|| jdur| j nd| j| j| j| j| j| j| jd�	| _	| j
 | j	j¡ | j	 ¡ I dH  | j	S )a  
        If your application doesn't work well with the context manager approach then you
        can call this method directly to initiate a connection.

        **Warning**: You must remember to close the connection with `.close()`.

        ```py
        connection = await client.live.forks.connect(...).enter()
        # ...
        await connection.close()
        ```
        Nra   )Ú_connect_wsr&  r'  rY   r)  r*  r+  r,  r-  r%  r.  Ú
merge_intoru   rá   ©rM   ÚwsrN   rN   rO   Ú
__aenter__ý  s    €÷z&AsyncForksConnectionManager.__aenter__r_   c              
   Ã  sZ  �zddl m} W n ty } ztdƒ|‚d }~ww |  ¡ ji | jjj¥|¥d�}|jdddœ 	|j
|j
¡d�}| j tjd	t|ƒt|ƒd
did�¡I d H }| j |j¡ | jj tt|jƒ¡¡}|jdddœ 	|j
|j
¡d�}dd„ | jji | jjt|jƒrƒ|jni fD ƒ}t d¡ | jr•t d¡ |t|ƒfd tt| ¡ ƒi ƒdœ| j¤ŽI d H S )Nr   )Ú_WebSocketConnectú9You need to install `openai[realtime]` to use this method©Úparamsr<  Úwss©ÚhttpÚhttps©Úschemer‘   Úbearer_authT©ÚmethodÚurlÚheadersÚsecurityc                 S  ó,   i | ]}|  ¡ D ]\}}| ¡ ||f“qqS rN   ©ÚitemsÚlower©Ú.0Ú
header_setÚkeyrÅ   rN   rN   rO   Ú
<dictcomp>8  ó    øùÿz;AsyncForksConnectionManager._connect_ws.<locals>.<dictcomp>úConnecting to WebSocket APIú,Custom WebSocket connection options provided©Úuser_agent_headerÚadditional_headers)Úlib._websocketr>  ÚImportErrorr   Ú_prepare_urlÚ	copy_withr#  Úbase_urlrA  r‘   rG  Ú_prepare_optionsr   Ú	constructrB   r�   rK  Úcopy_merge_paramsÚqsÚ	stringifyr   r   Úauth_headersÚdefault_headersr   rL  r2   rœ   r(  r    Úvalues©rM   r9   r:   rP   r„   rK  ÚoptionsrL  rN   rN   rO   r9    sZ   €
€ÿÿþÿü
ÿÿüþ


ÿýüz'AsyncForksConnectionManager._connect_wsú
httpx2.URLc                 C  ór   | j jd urt | j j¡}n| j jj}|dkrdnd}| j jj|d�}|j d¡t	d| j
d� d¡ }|j|d	�S ©
NrD  r<  rB  rF  ó   /z /live/sessions/{session_id}/fork)rA   r´   )Úraw_path)r#  Úwebsocket_base_urlÚhttpx2ÚURLÚ	_base_urlrG  r`  rp  Úrstripr   r$  Úencode©rM   ra  rG  Ú	ws_schemeÚmerge_raw_pathrN   rN   rO   r_  M  ó   
ÿþz(AsyncForksConnectionManager._prepare_urlÚexc_typeútype[BaseException] | Noner„   úBaseException | NoneÚexc_tbúTracebackType | Nonec                 Ã  s$   �| j d ur| j  ¡ I d H  d S d S ræ   )r%  rÀ   ©rM   r{  r„   r~  rN   rN   rO   Ú	__aexit__Z  s   €
ÿz%AsyncForksConnectionManager.__aexit__)rJ   r/   rA   rB   r9   r   r:   r   r;   r)   r<   rC   r=   rD   r>   rE   r?   rE   r@   rD   rF   rg   r  ræ   )r•   rB   rî   rï   rF   r4  )r•   rB   rî   ró   rF   rW   )rF   rY   )r9   r   r:   r   rF   r_   ©rF   rl  ©r{  r|  r„   r}  r~  r  rF   rg   )rS   rT   rU   r  rw   r®   rý   r  r	  r=  Úenterr9  r_  r�  rN   rN   rN   rO   rW   œ  ó&    ô
ÿ
ÿ
 

.rW   c                	   @  rX   )^ÚForksConnectionrZ   ÚForksSessionResourcer\   ÚForksResponseResourcer^   ÚWebSocketConnectionr`   Nr4   r5   r6   ra   rd   rb   ú6Callable[[Query, Headers], WebSocketConnection] | Noner<   rC   r=   rD   r>   rE   r?   r9   r   r:   r   rc   rf   rF   rg   c          
      C  sr   || _ || _|| _|| _d| _|| _|| _|| _|| _d| _	d| _
|	p%tƒ | _tdd�| _t| ƒ| _t| ƒ| _d S )Nr   FTrh   )r`   rj   rk   rl   rm   rn   ro   rp   rq   rr   rs   r   rt   r!   ru   r‡  r\   rˆ  r^   rv   rN   rN   rO   rw   i  rx   zForksConnection.__init__úIterator[ForkServerEvent]c              
   c  s„   � ddl m}m} 	 z|  ¡ V  W n/ |y   Y dS  |y@ } z|  |¡s6| j ¡ }|r5td|d�|‚‚ W Y d}~nd}~ww q
rz   r~   rƒ   rN   rN   rO   Ú__iter__‡  s,   €

þýù€ÿûzForksConnection.__iter__r$   c                 C  sZ   |   |  ¡ ¡}ttt|ƒtƒrtd|ƒ d¡nt|ddƒ}t|tƒr+|r+|dkr+d| _	|S )r‡   rˆ   r‰   NrŠ   r   r‹   r“   rN   rN   rO   r€   �  s   ÿ
ýzForksConnection.recvr–   c              	   C  sŒ   | j jdd�}t dt|ƒ¡ | jrDzt |¡}W n tt	fy&   | Y S w t
|tƒr4td|ƒ d¡nd}t
|tƒrD|rD|dkrDd| _|S )	r—   Fr˜   rš   rˆ   r‰   NrŠ   r   r›   r¢   rN   rN   rO   r�   ¯  s   ÿzForksConnection.recv_bytesr”   r¥   c                 C  sp   t |tƒr|jdddd�nt t|tƒ¡}| jr | j 	|¡ d S z	| j
 |¡ W d S  ty7   | j 	|¡ ‚ w r¦   )rŽ   r   r«   rž   r¬   r   r(   rs   rt   r­   r`   r®   r¯   r°   rN   rN   rO   r®   Ä  s   ÿýþzForksConnection.sendr±   r²   c                 C  s>   | j rt|tƒr
|n| d¡}| j |¡ d S | j |¡ d S r³   rµ   r¶   rN   rN   rO   r¸   Ó  s
   zForksConnection.send_rawr¹   rº   r»   r¼   r½   rB   c                C  s   d| _ | jj||d� d S r¾   r¿   rÁ   rN   rN   rO   rÀ   Ú  s   zForksConnection.closerÂ   c                 C  rÃ   rÄ   rÇ   rÈ   rN   rN   rO   rŒ   Þ  rÉ   zForksConnection.parse_eventr„   r¯   rÊ   c           
   	   C  s¶  | j du s
| jdu rdS ddlm} d}t||ƒr"|jdur"|jj}t|ƒs(dS d| _t	| j
d | jd ƒD ]Ÿ}|| _
t| jd|d   | jƒ}d	t ¡ d
  }|| }t|| j||| j| jd�}z|   |¡}	W n tyv   d| _Y  dS w |	dur†|	 d¡r†d| _ dS |	durœd|	v r“|	d | _d|	v rœ|	d | _t d|| j|¡ t |¡ | jr³d| _ dS z|  | j| j¡| _t d¡ d| _|  ¡  W  dS  tyÕ   Y q6w d| _dS )rË   NFr   rÌ   rÍ   TrÎ   rÏ   rÐ   rÑ   rÒ   r×   r9   r:   rØ   rÙ   )rk   rj   r   r|   rŽ   rÛ   r¼   r'   rs   rÜ   rm   rl   rÝ   rn   ro   rÞ   r%   rp   rq   r¯   r‘   r2   rß   Útimerà   rr   r`   rá   )
rM   r„   r|   rÖ   rÓ   râ   rã   rÕ   r”   rä   rN   rN   rO   r�   æ  sr   ú	þ

ü

ÿzForksConnection._reconnectc                   s:   zˆ j  ‡ fdd„¡ W dS  ty   t d¡ Y dS w )rå   c                   s   ˆ j  | ¡S ræ   rç   rè   ré   rN   rO   Ú<lambda>4  s    z3ForksConnection._flush_send_queue.<locals>.<lambda>rë   N)rt   Ú
flush_syncr¯   r2   rí   ré   rN   ré   rO   rá   1  s
   ÿz!ForksConnection._flush_send_queuer•   rî   rï   úJUnion[ForksConnection, Callable[[Callable[..., Any]], Callable[..., Any]]]c                   rñ   )a'  Adds the handler to the end of the handlers list for the given event type.

        No checks are made to see if the handler has already been added. Multiple calls
        passing the same combination of event type and handler will result in the handler
        being added, and called, multiple times.

        Can be used as a method (returns ``self`` for chaining)::

            connection.on("session.started", my_handler)

        Or as a decorator::

            @connection.on("session.started")
            def my_handler(event): ...
        Nrò   ró   rF   c                   rô   ræ   rõ   r÷   rø   rN   rO   rù   N  rú   z%ForksConnection.on.<locals>.decoratorrû   rõ   rü   rN   rø   rO   rý   8  rþ   zForksConnection.onró   c                 C  rÿ   r   r  r  rN   rN   rO   r  T  r  zForksConnection.offc                   r  )
r  NTr  rò   ró   rF   c                   r
  r  rõ   r÷   rø   rN   rO   rù   d  r  z'ForksConnection.once.<locals>.decoratorrû   rõ   rü   rN   rø   rO   r	  Y  r  zForksConnection.oncec                 C  sx   | D ]7}|j }| j |¡}| j d¡}|dkr'|s'|s't|tƒr'td|› �ƒ‚|D ]}||ƒ q)|D ]}||ƒ q2qdS )ay  Run the event loop, dispatching received events to registered handlers.

        Blocks the current thread until the connection is closed. This is the push-based
        alternative to iterating with ``for event in connection``.

        If an ``"error"`` event arrives and no handler is registered for
        ``"error"`` or ``"event"``, an ``OpenAIError`` is raised.
        r”   rŠ   r  N)r‰   ru   r  rŽ   r"   r   )rM   r”   r•   r  r  rî   rN   rN   rO   r  j  s   	


ÿôzForksConnection.dispatch_events)rd   r‰  rb   rŠ  r<   rC   r=   rD   r>   rE   r?   rE   r9   r   r:   r   rc   rf   rF   rg   )rF   r‹  r  r  r  r  r  r  r  r  ræ   )r•   rB   rî   rï   rF   r�  )r•   rB   rî   ró   rF   r†  )rS   rT   rU   r  r  rw   rŒ  r€   r�   r®   r¸   rÀ   rŒ   r�   rá   rý   r  r	  r  rN   rN   rN   rO   r†  a  s:   
 õ







Kÿ
ÿr†  c                   @  r  )GrG   a  
    Context manager over a `ForksConnection` that is returned by `live.forks.connect()`

    This context manager ensures that the connection will be closed when it exits.

    ---

    Note that if your application doesn't work well with the context manager approach then you
    can call the `.enter()` method directly to initiate a connection.

    **Warning**: You must remember to close the connection with `.close()`.

    ```py
    connection = client.live.forks.connect(...).enter()
    # ...
    connection.close()
    ```
    Nr4   r5   r6   r7   r  rJ   r.   rA   rB   r9   r   r:   r   r;   r)   r<   rC   r=   rD   r>   rE   r?   r@   rF   rg   c       
         C  r   )Nr!  Trh   )Ú_ForksConnectionManager__clientÚ#_ForksConnectionManager__session_idÚ#_ForksConnectionManager__connectionÚ$_ForksConnectionManager__extra_queryÚ&_ForksConnectionManager__extra_headersÚ5_ForksConnectionManager__websocket_connection_optionsÚ(_ForksConnectionManager__on_reconnectingÚ$_ForksConnectionManager__max_retriesÚ&_ForksConnectionManager__initial_delayÚ"_ForksConnectionManager__max_delayr   Ú#_ForksConnectionManager__send_queuer!   Ú/_ForksConnectionManager__event_handler_registryr/  rN   rN   rO   rw   —  r0  zForksConnectionManager.__init__r”   r¥   c                 C  r1  r2  )	rŽ   r   r«   rž   r¬   r   r(   r›  r­   r°   rN   rN   rO   r®   ²  r3  zForksConnectionManager.sendr•   rî   rï   úQUnion[ForksConnectionManager, Callable[[Callable[..., Any]], Callable[..., Any]]]c                   rñ   )zÖRegister an event handler before the connection is established.

        Handlers are transferred to the connection on enter. Supports the
        same method and decorator forms as ``ForksConnection.on``.
        Nrò   ró   rF   c                   rô   ræ   ©rœ  rö   r÷   rø   rN   rO   rù   Ë  rú   z,ForksConnectionManager.on.<locals>.decoratorrû   rž  rü   rN   rø   rO   rý   ¿  r6  zForksConnectionManager.onró   c                 C  rÿ   r   )rœ  r  r  rN   rN   rO   r  Ñ  r  zForksConnectionManager.offc                   r  )
r7  NTr  rò   ró   rF   c                   r
  r  rž  r÷   rø   rN   rO   rù   Þ  r  z.ForksConnectionManager.once.<locals>.decoratorrû   rž  rü   rN   rø   rO   r	  Ö  r8  zForksConnectionManager.oncer†  c                 C  sj   |   | j| j¡}t|| jdur| j nd| j| j| j| j| j| j| jd�	| _	| j
 | j	j¡ | j	 ¡  | j	S )as  
        If your application doesn't work well with the context manager approach then you
        can call this method directly to initiate a connection.

        **Warning**: You must remember to close the connection with `.close()`.

        ```py
        connection = client.live.forks.connect(...).enter()
        # ...
        connection.close()
        ```
        Nra   )r9  r”  r•  r†  r—  r˜  r™  rš  r›  r“  rœ  r:  ru   rá   r;  rN   rN   rO   Ú	__enter__ä  s   ÷
z ForksConnectionManager.__enter__r‰  c              
   C  sL  zddl m} W n ty } ztdƒ|‚d }~ww |  ¡ ji | jjj¥|¥d�}|jdddœ 	|j
|j
¡d�}| j tjd	t|ƒt|ƒd
did�¡}| j |j¡ | jj tt|jƒ¡¡}|jdddœ 	|j
|j
¡d�}dd„ | jji | jjt|jƒr|jni fD ƒ}t d¡ | jr‘t d¡ |t|ƒfd tt| ¡ ƒi ƒdœ| j¤ŽS )Nr   )rP   r?  r@  r<  rB  rC  rF  r‘   rH  TrI  c                 S  rN  rN   rO  rR  rN   rN   rO   rV    rW  z6ForksConnectionManager._connect_ws.<locals>.<dictcomp>rX  rY  rZ  )Úwebsockets.sync.clientrP   r^  r   r_  r`  r‘  ra  rA  r‘   rG  rb  r   rc  rB   r�   rK  rd  re  rf  r   r   rg  rh  r   rL  r2   rœ   r–  r    ri  rj  rN   rN   rO   r9    sX   
€ÿÿþÿüÿÿüþ


ÿýüz"ForksConnectionManager._connect_wsrl  c                 C  rm  rn  )r‘  rq  rr  rs  rt  rG  r`  rp  ru  r   r’  rv  rw  rN   rN   rO   r_  4  rz  z#ForksConnectionManager._prepare_urlr{  r|  r„   r}  r~  r  c                 C  s   | j d ur| j  ¡  d S d S ræ   )r“  rÀ   r€  rN   rN   rO   Ú__exit__A  s   
ÿzForksConnectionManager.__exit__)rJ   r.   rA   rB   r9   r   r:   r   r;   r)   r<   rC   r=   rD   r>   rE   r?   rE   r@   rD   rF   rg   r  ræ   )r•   rB   rî   rï   rF   r�  )r•   rB   rî   ró   rF   rG   )rF   r†  )r9   r   r:   r   rF   r‰  r‚  rƒ  )rS   rT   rU   r  rw   r®   rý   r  r	  rŸ  r„  r9  r_  r¡  rN   rN   rN   rO   rG   ƒ  r…  rG   c                   @  ó   e Zd Zddd„ZdS )	ÚBaseForksConnectionResourcerd   r†  rF   rg   c                 C  ó
   || _ d S ræ   ©r`   ©rM   rd   rN   rN   rO   rw   I  ó   
z$BaseForksConnectionResource.__init__N)rd   r†  rF   rg   ©rS   rT   rU   rw   rN   rN   rN   rO   r£  H  ó    r£  c                   @  ót   e Zd Zeddd„ƒZeddd„ƒZedd	d
„ƒZeddd„ƒZedœd dd„Z	edœd!dd„Z
edœd"dd„ZdS )#r‡  rF   ÚForksSessionCommentaryResourcec                 C  ó
   t | jƒS ræ   )r«  r`   ré   rN   rN   rO   Ú
commentaryN  ó   
zForksSessionResource.commentaryÚForksSessionInputAudioResourcec                 C  r¬  ræ   )r¯  r`   ré   rN   rN   rO   Úinput_audioR  r®  z ForksSessionResource.input_audioÚ ForksSessionInstructionsResourcec                 C  r¬  ræ   )r±  r`   ré   rN   rN   rO   ÚinstructionsV  r®  z!ForksSessionResource.instructionsÚForksSessionThinkingResourcec                 C  r¬  ræ   )r³  r`   ré   rN   rN   rO   ÚthinkingZ  r®  zForksSessionResource.thinking©Úevent_idr\   r*   r¶  úOptional[str] | Omitrg   c             	   C  ó"   | j  tttd||dœƒƒ¡ dS ©u¨   
        Start a Live session after connecting to a stored sessionâ€™s fork WebSocket.

        Send an empty `session` object to use the stored configuration.
        zsession.start©r‰   r\   r¶  N©r`   r®   r   r(   r   ©rM   r\   r¶  rN   rN   rO   Ústart^  s   þÿzForksSessionResource.startc                C  ó    | j  tttd|dœƒƒ¡ dS ©z…Request that the Live session close.

        The terminal `session.closed` event contains the close reason and final usage.
        zsession.close©r‰   r¶  Nr»  ©rM   r¶  rN   rN   rO   rÀ   k  ó   ÿzForksSessionResource.closer+   c             	   C  r¸  ©zŒUpdate the delegation settings of an active Live session.

        The server acknowledges accepted changes with `session.updated`.
        zsession.updaterº  Nr»  r¼  rN   rN   rO   Úupdatet  ó   þÿzForksSessionResource.updateN)rF   r«  )rF   r¯  )rF   r±  )rF   r³  ©r\   r*   r¶  r·  rF   rg   ©r¶  r·  rF   rg   ©r\   r+   r¶  r·  rF   rg   ©rS   rT   rU   r   r­  r°  r²  r´  r   r½  rÀ   rÄ  rN   rN   rN   rO   r‡  M  ó    	r‡  c                   @  ó   e Zd Zedœdd
d„ZdS )r«  rµ  ÚcontentrB   Údelegation_idúOptional[str]r¶  r·  rF   rg   c             
   C  ó$   | j  tttd|||dœƒƒ¡ dS ©z{
        Provide context the Live model can communicate to the user, optionally for an existing client delegation.
        zsession.commentary.append©r‰   rÌ  rÍ  r¶  Nr»  ©rM   rÌ  rÍ  r¶  rN   rN   rO   Úappend‚  ó   üÿþÿz%ForksSessionCommentaryResource.appendN©rÌ  rB   rÍ  rÎ  r¶  r·  rF   rg   ©rS   rT   rU   r   rÓ  rN   rN   rN   rO   r«  �  ó    r«  c                   @  ó<   e Zd Zedœddd	„Zedœdd
d„Zedœddd„ZdS )r¯  rµ  ÚaudiorB   r¶  r·  rF   rg   c             	   C  r¸  ©z‰Send audio to a Live session over its primary WebSocket.

        WebRTC and SIP sessions send audio over their media transport.
        zsession.input_audio.append)r‰   rÙ  r¶  Nr»  ©rM   rÙ  r¶  rN   rN   rO   rÓ  –  rÅ  z%ForksSessionInputAudioResource.appendc                C  r¾  ©z‹Mute audio input to the Live model without closing the session.

        The server acknowledges with `session.input_audio.muted`.
        zsession.input_audio.muterÀ  Nr»  rÁ  rN   rN   rO   Úmute¢  rÂ  z#ForksSessionInputAudioResource.mutec                C  r¾  ©z�Resume audio input to a Live model after muting it.

        The server acknowledges with `session.input_audio.unmuted`.
        zsession.input_audio.unmuterÀ  Nr»  rÁ  rN   rN   rO   Úunmute«  rÂ  z%ForksSessionInputAudioResource.unmuteN©rÙ  rB   r¶  r·  rF   rg   rÇ  ©rS   rT   rU   r   rÓ  rÝ  rß  rN   rN   rN   rO   r¯  •  ó    	r¯  c                   @  rË  )r±  rµ  rÌ  rB   rÍ  rÎ  r¶  r·  rF   rg   c             
   C  rÏ  ©z“
        Append instructions to the Live conversation while it is running, optionally associating them with an existing client delegation.
        zsession.instructions.appendrÑ  Nr»  rÒ  rN   rN   rO   rÓ  ¶  rÔ  z'ForksSessionInstructionsResource.appendNrÕ  rÖ  rN   rN   rN   rO   r±  µ  r×  r±  c                   @  rË  )r³  rµ  rÌ  rB   rÍ  rÎ  r¶  r·  rF   rg   c             
   C  rÏ  ©z
        Provide silent reasoning or progress context to the Live model, optionally for an existing client delegation.
        zsession.thinking.appendrÑ  Nr»  rÒ  rN   rN   rO   rÓ  Ê  rÔ  z#ForksSessionThinkingResource.appendNrÕ  rÖ  rN   rN   rN   rO   r³  É  r×  r³  c                   @  ó*   e Zd Zeddd„ƒZedœdd	d
„ZdS )rˆ  rF   ÚForksResponseItemResourcec                 C  r¬  ræ   )ræ  r`   ré   rN   rN   rO   ÚitemÞ  r®  zForksResponseResource.itemrµ  r¶  r·  rg   c                C  r¾  ©u¼   
        Request a response from the Live sessionâ€™s Responses backend, or continue a
        delegated response waiting for tool results.

        Requires Responses delegation.
        zresponse.createrÀ  Nr»  rÁ  rN   rN   rO   Úcreateâ  s   ÿzForksResponseResource.createN)rF   ræ  rÇ  ©rS   rT   rU   r   rç  r   ré  rN   rN   rN   rO   rˆ  Ý  ó    rˆ  c                   @  ó   e Zd Zedœddd	„Zd
S )ræ  rµ  rç  r,   r¶  r·  rF   rg   c             	   C  r¸  ©uš   Add an input item to the Live sessionâ€™s Responses backend.

        Requires Responses delegation; use `response.create` to request a response.
        zresponse.item.create)r‰   rç  r¶  Nr»  ©rM   rç  r¶  rN   rN   rO   ré  ï  rÅ  z ForksResponseItemResource.createN©rç  r,   r¶  r·  rF   rg   ©rS   rT   rU   r   ré  rN   rN   rN   rO   ræ  î  r×  ræ  c                   @  r¢  )	Ú BaseAsyncForksConnectionResourcerd   rY   rF   rg   c                 C  r¤  ræ   r¥  r¦  rN   rN   rO   rw   ý  r§  z)BaseAsyncForksConnectionResource.__init__N)rd   rY   rF   rg   r¨  rN   rN   rN   rO   rñ  ü  r©  rñ  c                   @  rª  )#r[   rF   Ú#AsyncForksSessionCommentaryResourcec                 C  r¬  ræ   )rò  r`   ré   rN   rN   rO   r­    r®  z$AsyncForksSessionResource.commentaryÚ#AsyncForksSessionInputAudioResourcec                 C  r¬  ræ   )ró  r`   ré   rN   rN   rO   r°    r®  z%AsyncForksSessionResource.input_audioÚ%AsyncForksSessionInstructionsResourcec                 C  r¬  ræ   )rô  r`   ré   rN   rN   rO   r²  
  r®  z&AsyncForksSessionResource.instructionsÚ!AsyncForksSessionThinkingResourcec                 C  r¬  ræ   )rõ  r`   ré   rN   rN   rO   r´    r®  z"AsyncForksSessionResource.thinkingrµ  r\   r*   r¶  r·  rg   c             	   Ã  ó*   �| j  tttd||dœƒƒ¡I dH  dS r¹  r»  r¼  rN   rN   rO   r½    s   €þÿzAsyncForksSessionResource.startc                Ã  ó(   �| j  tttd|dœƒƒ¡I dH  dS r¿  r»  rÁ  rN   rN   rO   rÀ     ó   €ÿzAsyncForksSessionResource.closer+   c             	   Ã  rö  rÃ  r»  r¼  rN   rN   rO   rÄ  (  ó   €þÿz AsyncForksSessionResource.updateN)rF   rò  )rF   ró  )rF   rô  )rF   rõ  rÆ  rÇ  rÈ  rÉ  rN   rN   rN   rO   r[     rÊ  r[   c                   @  rË  )rò  rµ  rÌ  rB   rÍ  rÎ  r¶  r·  rF   rg   c             
   Ã  ó,   �| j  tttd|||dœƒƒ¡I dH  dS rÐ  r»  rÒ  rN   rN   rO   rÓ  6  ó   €üÿþÿz*AsyncForksSessionCommentaryResource.appendNrÕ  rÖ  rN   rN   rN   rO   rò  5  ó    ÿrò  c                   @  rØ  )ró  rµ  rÙ  rB   r¶  r·  rF   rg   c             	   Ã  rö  rÚ  r»  rÛ  rN   rN   rO   rÓ  L  rù  z*AsyncForksSessionInputAudioResource.appendc                Ã  r÷  rÜ  r»  rÁ  rN   rN   rO   rÝ  X  rø  z(AsyncForksSessionInputAudioResource.mutec                Ã  r÷  rÞ  r»  rÁ  rN   rN   rO   rß  a  rø  z*AsyncForksSessionInputAudioResource.unmuteNrà  rÇ  rá  rN   rN   rN   rO   ró  K  râ  ró  c                   @  rË  )rô  rµ  rÌ  rB   rÍ  rÎ  r¶  r·  rF   rg   c             
   Ã  rú  rã  r»  rÒ  rN   rN   rO   rÓ  l  rû  z,AsyncForksSessionInstructionsResource.appendNrÕ  rÖ  rN   rN   rN   rO   rô  k  rü  rô  c                   @  rË  )rõ  rµ  rÌ  rB   rÍ  rÎ  r¶  r·  rF   rg   c             
   Ã  rú  rä  r»  rÒ  rN   rN   rO   rÓ  ‚  rû  z(AsyncForksSessionThinkingResource.appendNrÕ  rÖ  rN   rN   rN   rO   rõ  �  rü  rõ  c                   @  rå  )r]   rF   ÚAsyncForksResponseItemResourcec                 C  r¬  ræ   )rý  r`   ré   rN   rN   rO   rç  ˜  r®  zAsyncForksResponseResource.itemrµ  r¶  r·  rg   c                Ã  r÷  rè  r»  rÁ  rN   rN   rO   ré  œ  s   €ÿz!AsyncForksResponseResource.createN)rF   rý  rÇ  rê  rN   rN   rN   rO   r]   —  rë  r]   c                   @  rì  )rý  rµ  rç  r,   r¶  r·  rF   rg   c             	   Ã  rö  rí  r»  rî  rN   rN   rO   ré  ©  rù  z%AsyncForksResponseItemResource.createNrï  rð  rN   rN   rN   rO   rý  ¨  r×  rý  )iÚ
__future__r   rž   r�  rÞ   ÚloggingÚtypesr   Útypingr   r   r   r   r   r	   r
   r   Útyping_extensionsr   rr  Úpydanticr   Ú_typesr   r   r   r   Ú_utilsr   r   r   r   r   Ú_compatr   Ú_modelsr   r   Ú	_resourcer   r   Ú_exceptionsr   r   rt   r   Ú_base_clientr    Ú_event_handlerr!   Útypes.live.error_eventr"   Útypes.live.fork_client_eventr#   Útypes.live.fork_server_eventr$   Útypes.websocket_reconnectionr%   r&   r'   Ú"types.live.fork_client_event_paramr(   Ú"types.websocket_connection_optionsr)   Ú$types.live.fork_session_config_paramr*   Ú&types.live.session_update_config_paramr+   Ú)types.responses.response_input_item_paramr,   r   r-   r‰  Úwebsockets.asyncio.clientr_   rK   r.   r/   Ú__all__Ú	getLoggerrS   r2   r  r0   r1   rY   rW   r†  rG   r£  r‡  r«  r¯  r±  r³  rˆ  ræ  rñ  r[   rò  ró  rô  rõ  r]   rý  rN   rN   rN   rO   Ú<module>   s|   (    0 F  $ F4 4 