o
    ¨Ýji´  ã                   @  sÄ  U d dl m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 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 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+m,Z,m-Z-m.Z.m/Z/m0Z0 ddl1m2Z2 ddl3m4Z4 ddl5m6Z6m7Z7 ddl8m9Z9m:Z: ddl;m<Z<m=Z= ddl>m?Z? ddl@mAZA ddlBmCZC ddlDmEZE ddlFmGZG erÛd dlHmIZJ d dlKmIZL ddlMmNZNmOZO ddgZPe QeR¡ZSdeTd < G d!d„ de#ƒZUG d"d„ de$ƒZVG d#d$„ d$ƒZWG d%d&„ d&ƒZXG d'd(„ d(ƒZYG d)d*„ d*ƒZZG d+d,„ d,e7ƒZ[G d-d.„ d.ƒZ\G d/d0„ d0e6ƒZ]G d1d2„ d2ƒZ^G d3d4„ d4ƒZ_G d5d6„ d6e_ƒZ`G d7d8„ d8e_ƒZaG d9d:„ d:ƒZbG d;d<„ d<ebƒZcG d=d>„ d>ebƒZddS )?é    )ÚannotationsN)ÚTracebackType)ÚTYPE_CHECKINGÚAnyÚUnionÚCallableÚIteratorÚ	AwaitableÚcast)ÚAsyncIterator)Ú	BaseModelé   )ÚOmitÚQueryÚHeadersÚomit)Úis_givenÚmaybe_transformÚstrip_not_givenÚasync_maybe_transform)Úcached_property)ÚFinalRequestOptionsÚconstruct_type_unchecked)ÚSyncAPIResourceÚAsyncAPIResource)ÚOpenAIErrorÚWebSocketConnectionClosedError)Ú	SendQueueé   )ÚClientSecretsÚAsyncClientSecretsÚClientSecretsWithRawResponseÚ!AsyncClientSecretsWithRawResponseÚ"ClientSecretsWithStreamingResponseÚ'AsyncClientSecretsWithStreamingResponse)Ú_merge_mappings)ÚEventHandlerRegistry)ÚWebSocketConnectionBaseÚAsyncWebSocketConnectionBase)ÚReconnectingEventÚReconnectingOverrides)ÚWebSocketConnectionOptionsÚSyncWebSocketConnectionOptions)ÚRealtimeErrorEvent)ÚRealtimeTranslationClientEvent)ÚRealtimeTranslationServerEvent)Ú#RealtimeTranslationClientEventParam)Ú,RealtimeTranslationSessionUpdateRequestParam)ÚClientConnection)ÚOpenAIÚAsyncOpenAIÚTranslationsÚAsyncTranslationszlogging.LoggerÚlogc                   @  óR   e Zd Zed#dd„ƒZed$dd„ƒZed%d	d
„ƒZi i i dddddfd&d!d"„ZdS )'r5   Úreturnr   c                 C  ó
   t | jƒS ©N)r   Ú_client©Úself© r?   ún/var/www/html/chatgpt/venv/lib/python3.10/site-packages/openai/resources/realtime/translations/translations.pyÚclient_secrets6   ó   
zTranslations.client_secretsÚTranslationsWithRawResponsec                 C  ó   t | ƒS ©a  
        This property can be used as a prefix for any HTTP method call to return
        the raw response object instead of the parsed content.

        For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
        )rC   r=   r?   r?   r@   Úwith_raw_response:   ó   zTranslations.with_raw_responseÚ!TranslationsWithStreamingResponsec                 C  rD   ©zÌ
        An alternative to `.with_raw_response` that doesn't eagerly read the response body.

        For more information, see https://www.github.com/openai/openai-python#with_streaming_response
        )rH   r=   r?   r?   r@   Úwith_streaming_responseD   ó   z$Translations.with_streaming_responseNé   ç      à?ç       @é   ÚmodelÚstrÚextra_queryr   Úextra_headersr   Úwebsocket_connection_optionsr,   Úon_reconnectingúBCallable[[ReconnectingEvent], ReconnectingOverrides | None] | NoneÚmax_retriesÚintÚinitial_delayÚfloatÚ	max_delayÚmax_queue_sizeÚTranslationsConnectionManagerc
           
      C  ó   t | j|||||||||	d�
S ©N)
ÚclientrP   rR   rS   rT   rU   rW   rY   r[   r\   )r]   r<   ©
r>   rP   rR   rS   rT   rU   rW   rY   r[   r\   r?   r?   r@   ÚconnectM   ó   özTranslations.connect)r9   r   )r9   rC   )r9   rH   )rP   rQ   rR   r   rS   r   rT   r,   rU   rV   rW   rX   rY   rZ   r[   rZ   r\   rX   r9   r]   ©Ú__name__Ú
__module__Ú__qualname__r   rA   rF   rJ   rb   r?   r?   r?   r@   r5   5   ó     	öc                   @  r8   )'r6   r9   r    c                 C  r:   r;   )r    r<   r=   r?   r?   r@   rA   i   rB   z AsyncTranslations.client_secretsÚ AsyncTranslationsWithRawResponsec                 C  rD   rE   )ri   r=   r?   r?   r@   rF   m   rG   z#AsyncTranslations.with_raw_responseÚ&AsyncTranslationsWithStreamingResponsec                 C  rD   rI   )rj   r=   r?   r?   r@   rJ   w   rK   z)AsyncTranslations.with_streaming_responseNrL   rM   rN   rO   rP   rQ   rR   r   rS   r   rT   r+   rU   rV   rW   rX   rY   rZ   r[   r\   Ú"AsyncTranslationsConnectionManagerc
           
      C  r^   r_   )rk   r<   ra   r?   r?   r@   rb   €   rc   zAsyncTranslations.connect)r9   r    )r9   ri   )r9   rj   )rP   rQ   rR   r   rS   r   rT   r+   rU   rV   rW   rX   rY   rZ   r[   rZ   r\   rX   r9   rk   rd   r?   r?   r?   r@   r6   h   rh   c                   @  ó$   e Zd Zddd„Zeddd	„ƒZd
S )rC   Útranslationsr5   r9   ÚNonec                 C  ó
   || _ d S r;   ©Ú_translations©r>   rm   r?   r?   r@   Ú__init__œ   ó   
z$TranslationsWithRawResponse.__init__r!   c                 C  ó   t | jjƒS r;   )r!   rq   rA   r=   r?   r?   r@   rA   Ÿ   ó   z*TranslationsWithRawResponse.client_secretsN©rm   r5   r9   rn   )r9   r!   ©re   rf   rg   rs   r   rA   r?   r?   r?   r@   rC   ›   ó    
rC   c                   @  rl   )ri   rm   r6   r9   rn   c                 C  ro   r;   rp   rr   r?   r?   r@   rs   ¥   rt   z)AsyncTranslationsWithRawResponse.__init__r"   c                 C  ru   r;   )r"   rq   rA   r=   r?   r?   r@   rA   ¨   rv   z/AsyncTranslationsWithRawResponse.client_secretsN©rm   r6   r9   rn   )r9   r"   rx   r?   r?   r?   r@   ri   ¤   ry   ri   c                   @  rl   )rH   rm   r5   r9   rn   c                 C  ro   r;   rp   rr   r?   r?   r@   rs   ®   rt   z*TranslationsWithStreamingResponse.__init__r#   c                 C  ru   r;   )r#   rq   rA   r=   r?   r?   r@   rA   ±   rv   z0TranslationsWithStreamingResponse.client_secretsNrw   )r9   r#   rx   r?   r?   r?   r@   rH   ­   ry   rH   c                   @  rl   )rj   rm   r6   r9   rn   c                 C  ro   r;   rp   rr   r?   r?   r@   rs   ·   rt   z/AsyncTranslationsWithStreamingResponse.__init__r$   c                 C  ru   r;   )r$   rq   rA   r=   r?   r?   r@   rA   º   rv   z5AsyncTranslationsWithStreamingResponse.client_secretsNrz   )r9   r$   rx   r?   r?   r?   r@   rj   ¶   ry   rj   c                	      óª   e Zd ZU dZded< ded< ddddd	i i dd
œd>‡ fdd„Zd?d d!„Zd@d#d$„ZdAd&d'„ZdBd*d+„Z	dCd.d/„Z
	dDdEd5d6„ZdFd8d9„Z	dDdEd:d;„ZdGd<d=„Z‡  ZS )HÚAsyncTranslationsConnectionú>Represents a live WebSocket connection to the Translations APIÚ AsyncTranslationsSessionResourceÚsessionÚAsyncWebSocketConnectionÚ_connectionNrL   rM   rN   ©Úmake_wsrU   rW   rY   r[   rR   rS   Ú
send_queueÚ
connectionrƒ   úFCallable[[Query, Headers], Awaitable[AsyncWebSocketConnection]] | NonerU   rV   rW   rX   rY   rZ   r[   rR   r   rS   r   r„   úSendQueue | Noner9   rn   c          
        ó:   t ƒ j|||||||||	td�
 tdd�| _t| ƒ| _d S )N©	rƒ   rU   rW   rY   r[   rR   rS   r„   ÚloggerF©Zuse_lock)Úsuperrs   r7   r&   Ú_event_handler_registryr~   r   ©
r>   r…   rƒ   rU   rW   rY   r[   rR   rS   r„   ©Ú	__class__r?   r@   rs   Æ   ó   öz$AsyncTranslationsConnection.__init__ú-AsyncIterator[RealtimeTranslationServerEvent]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)Zunsent_messages©Zwebsockets.exceptionsr”   r•   ÚrecvZ
_reconnectÚ_send_queueZdrainr   ©r>   r”   r•   ÚexcZunsentr?   r?   r@   Ú	__aiter__ã   s,   €
þýù€ÿûz%AsyncTranslationsConnection.__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 `RealtimeTranslationServerEvent` 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ÚgetattrrQ   Ú_reconnect_attempt©r>   ÚeventÚ
event_typer?   r?   r@   r—   ù   s   €ÿ
ýz AsyncTranslationsConnection.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 `RealtimeTranslationServerEvent` object like `.recv()` does,
        then you can call `.parse_event(data)`.
        F)ÚdecodeNú$Received WebSocket message: %i bytesr�   rž   rŸ   r   )r�   r—   r7   ÚdebugÚlenr¨   ÚjsonÚloadsÚ
ValueErrorÚRecursionErrorr£   r¥   r
   r¦   rQ   )r>   ÚmessageÚ
event_datar«   r?   r?   r@   r¢     s   €ÿz&AsyncTranslationsConnection.recv_bytesrª   úDRealtimeTranslationClientEvent | RealtimeTranslationClientEventParamc                 Ã  s\   �t |tƒr|jddd�n
t t|tƒI d H ¡}| jr#| j 	|¡ d S | j
 |¡I d H  d S ©NT©Zuse_api_namesZexclude_unset)r£   r   Úto_jsonr²   Údumpsr   r0   Ú_is_reconnectingr˜   Úenqueuer�   Úsend©r>   rª   Údatar?   r?   r@   r¿      s   €ÿýz AsyncTranslationsConnection.sendrÁ   ústr | bytesc                 C  ó   t ttt |¡t ttƒd�ƒS ©z£
        Converts a raw `str` or `bytes` message into a `RealtimeTranslationServerEvent` object.

        This is helpful if you're using `.recv_bytes()`.
        )ÚvalueÚtype_©r
   r/   r   r²   r³   r   ©r>   rÁ   r?   r?   r@   r¡   +  ó   þz'AsyncTranslationsConnection.parse_eventr«   rQ   ÚhandlerúCallable[..., Any] | NoneúVUnion[AsyncTranslationsConnection, 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.created", my_handler)

        Or as a decorator::

            @connection.on("session.created")
            async def my_handler(event): ...
        NÚfnúCallable[..., Any]r9   c                   ó   ˆj  ˆ | ¡ | S r;   ©r�   Úadd©rÎ   ©r«   r>   r?   r@   Ú	decoratorL  ó   z1AsyncTranslationsConnection.on.<locals>.decorator©rÎ   rÏ   r9   rÏ   rÑ   ©r>   r«   rÊ   rÕ   r?   rÔ   r@   Úon6  ó
   zAsyncTranslationsConnection.onrÏ   c                 C  ó   | j  ||¡ | S ©z-Remove a previously registered event handler.©r�   Úremove©r>   r«   rÊ   r?   r?   r@   ÚoffR  ó   zAsyncTranslationsConnection.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Ï   r9   c                   ó   ˆj jˆ | dd� | S ©NTrä   rÑ   rÓ   rÔ   r?   r@   rÕ   b  ó   z3AsyncTranslationsConnection.once.<locals>.decoratorr×   rÑ   rØ   r?   rÔ   r@   rå   W  ó
   z AsyncTranslationsConnection.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: )Úasynciorž   r�   Úget_handlersr£   r-   r   Úiscoroutine)r>   rë   rª   r«   ÚspecificÚgenericrÊ   Úresultr?   r?   r@   Údispatch_eventsh  s*   €	


€

€ýòz+AsyncTranslationsConnection.dispatch_events)r…   r€   rƒ   r†   rU   rV   rW   rX   rY   rZ   r[   rZ   rR   r   rS   r   r„   r‡   r9   rn   )r9   r’   ©r9   r/   ©r9   r¬   ©rª   r¸   r9   rn   ©rÁ   rÂ   r9   r/   r;   )r«   rQ   rÊ   rË   r9   rÌ   )r«   rQ   rÊ   rÏ   r9   r|   ©r9   rn   )re   rf   rg   Ú__doc__Ú__annotations__rs   r›   r—   r¢   r¿   r¡   rÙ   rà   rå   rñ   Ú__classcell__r?   r?   r�   r@   r|   ¿   s0   
 õ




ÿ
ÿr|   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 )Grk   aG  
    Context manager over a `AsyncTranslationsConnection` that is returned by `realtime.translations.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.realtime.translations.connect(...).enter()
    # ...
    await connection.close()
    ```
    NrL   rM   rN   rO   ©rU   rW   rY   r[   r\   r`   r4   rP   rQ   rR   r   rS   r   rT   r+   rU   rV   rW   rX   rY   rZ   r[   r\   r9   rn   c       
         C  ó`   || _ d | _|| _|| _|| _dd i|¥| _|| _|| _|| _|	| _	t
|
d�| _tdd�| _d S )NÚmax_size©Z	max_bytesFr‹   )Ú+_AsyncTranslationsConnectionManager__clientÚ/_AsyncTranslationsConnectionManager__connectionÚ*_AsyncTranslationsConnectionManager__modelÚ0_AsyncTranslationsConnectionManager__extra_queryÚ2_AsyncTranslationsConnectionManager__extra_headersÚA_AsyncTranslationsConnectionManager__websocket_connection_optionsÚ4_AsyncTranslationsConnectionManager__on_reconnectingÚ0_AsyncTranslationsConnectionManager__max_retriesÚ2_AsyncTranslationsConnectionManager__initial_delayÚ._AsyncTranslationsConnectionManager__max_delayr   Ú/_AsyncTranslationsConnectionManager__send_queuer&   Ú;_AsyncTranslationsConnectionManager__event_handler_registry©r>   r`   rP   rR   rS   rT   rU   rW   rY   r[   r\   r?   r?   r@   rs   ›  ó   ÿþz+AsyncTranslationsConnectionManager.__init__rª   r¸   c                 C  ó8   t |tƒr|j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   r0   r	  r¾   rÀ   r?   r?   r@   r¿   ¹  ó
   ÿýz'AsyncTranslationsConnectionManager.sendr«   rÊ   rË   ú]Union[AsyncTranslationsConnectionManager, 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 ``AsyncTranslationsConnection.on``.
        NrÎ   rÏ   r9   c                   rÐ   r;   ©r
  rÒ   rÓ   rÔ   r?   r@   rÕ   Ò  rÖ   z8AsyncTranslationsConnectionManager.on.<locals>.decoratorr×   r  rØ   r?   rÔ   r@   rÙ   Æ  ó
   z%AsyncTranslationsConnectionManager.onrÏ   c                 C  rÛ   rÜ   )r
  rÞ   rß   r?   r?   r@   rà   Ø  rá   z&AsyncTranslationsConnectionManager.offc                   râ   )
úGRegister a one-time event handler before the connection is established.NTrä   rÎ   rÏ   r9   c                   ræ   rç   r  rÓ   rÔ   r?   r@   rÕ   å  rè   z:AsyncTranslationsConnectionManager.once.<locals>.decoratorr×   r  rØ   r?   rÔ   r@   rå   Ý  ó
   z'AsyncTranslationsConnectionManager.oncer|   c                 Ã  s¶   �|   | j| j¡I dH }z1t|| jdur| j nd| j| j| j| j| j| j| jd�	| _	| j
 | j	j¡ | j	 ¡ I dH  W | j	S  tyZ   d| _	z	| ¡ I dH  W ‚  tyY   Y ‚ w w )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.realtime.translations.connect(...).enter()
        # ...
        await connection.close()
        ```
        Nr‚   )Ú_connect_wsr  r  r|   r  r  r  r  r	  r   r
  Ú
merge_intor�   Ú_flush_send_queueÚBaseExceptionÚclose©r>   Úwsr?   r?   r@   Ú
__aenter__ë  s6   €÷
÷ýýüz-AsyncTranslationsConnectionManager.__aenter__r€   c           
   
   Ã  sÎ  �zddl m} W n ty } ztdƒ|‚d }~ww |  ¡ }ddlm} |jt||ƒr.|n| j	 
|t|tjƒrCt d| ji¡ |¡nd| ji|¥¡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¡ | j# $¡ }	|	 %dd ¡ |	 d¡d u rÐ|	 %dd ¡ |t|ƒfd t&t| '¡ ƒi ƒdœ|	¤ŽI d H S )Nr   )Ú_WebSocketConnectú9You need to install `openai[realtime]` to use this method©Ú_PreparedTranslationQueryrP   ©Úparamsr  Úwss©ÚhttpÚhttps©Úschemer¦   Úbearer_authT©ÚmethodÚurlÚheadersÚsecurityc                 S  ó,   i | ]}|  ¡ D ]\}}| ¡ ||f“qqS r?   ©ÚitemsÚlower©Ú.0Z
header_setÚkeyrÅ   r?   r?   r@   Ú
<dictcomp>6  ó    øùÿzBAsyncTranslationsConnectionManager._connect_ws.<locals>.<dictcomp>úConnecting to WebSocket APIú,Custom WebSocket connection options providedrý   Úcompression©Zuser_agent_headerZadditional_headers)(Úlib._websocketr  ÚImportErrorr   Ú_prepare_urlÚlib.realtime_translationr   Ú	copy_withr£   rÿ   Ú_websocket_query_paramsÚhttpx2ÚQueryParamsr  Úmerger¦   r(  Ú_prepare_optionsr   Ú	constructrQ   r¥   r,  Úcopy_merge_paramsÚqsÚ	stringifyr
   r   r"  Úauth_headersÚdefault_headersr   r-  r7   r°   r  ÚcopyÚ
setdefaultr%   Úvalues©
r>   rR   rS   rb   rš   r,  r   Úoptionsr-  Zwebsocket_optionsr?   r?   r@   r    sl   €
€ÿÿ
ÿüý
ü
ÿÿüþ



ÿýüz.AsyncTranslationsConnectionManager._connect_wsú
httpx2.URLc                 C  óz   | j jd urt | j j¡}n| j jj}|dkrdnd}| j jj|d�}|j d¡\}}}| 	d¡d | | }|j|d�S ©	Nr%  r  r#  r'  ó   ?ó   /s   /realtime/translations)Úraw_path)
rÿ   Úwebsocket_base_urlrB  ÚURLÚ	_base_urlr(  r@  rV  Ú	partitionÚrstrip©r>   Úbase_urlr(  Z	ws_schemeÚpathÚ	separatorÚqueryZmerge_raw_pathr?   r?   r@   r>  P  ó   
z/AsyncTranslationsConnectionManager._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  ©r>   rb  rš   re  r?   r?   r@   Ú	__aexit__\  s   €
ÿz,AsyncTranslationsConnectionManager.__aexit__)r`   r4   rP   rQ   rR   r   rS   r   rT   r+   rU   rV   rW   rX   rY   rZ   r[   rZ   r\   rX   r9   rn   rô   r;   )r«   rQ   rÊ   rË   r9   r  )r«   rQ   rÊ   rÏ   r9   rk   )r9   r|   )rR   r   rS   r   r9   r€   ©r9   rQ  ©rb  rc  rš   rd  re  rf  r9   rn   )re   rf   rg   r÷   rs   r¿   rÙ   rà   rå   r  Úenterr  r>  rh  r?   r?   r?   r@   rk   ‡  ó&    ô
ÿ
ÿ
)

:rk   c                	      r{   )HÚTranslationsConnectionr}   ÚTranslationsSessionResourcer   ÚWebSocketConnectionr�   NrL   rM   rN   r‚   r…   rƒ   ú6Callable[[Query, Headers], WebSocketConnection] | NonerU   rV   rW   rX   rY   rZ   r[   rR   r   rS   r   r„   r‡   r9   rn   c          
        rˆ   )Nr‰   Tr‹   )rŒ   rs   r7   r&   r�   rn  r   rŽ   r�   r?   r@   rs   j  r‘   zTranslationsConnection.__init__ú(Iterator[RealtimeTranslationServerEvent]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
r“   r–   r™   r?   r?   r@   Ú__iter__‡  s,   €

þýù€ÿûzTranslationsConnection.__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©   r?   r?   r@   r—   �  s   ÿ
ýzTranslationsConnection.recvr¬   c              	   C  s”   ddl m} || jƒ}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 )	r­   r   )Ú_recv_bytesr¯   r�   rž   NrŸ   r   )r<  rs  r�   r7   r°   r±   r¨   r²   r³   r´   rµ   r£   r¥   r
   r¦   rQ   )r>   rs  r¶   r·   r«   r?   r?   r@   r¢   ¯  s   
ÿz!TranslationsConnection.recv_bytesrª   r¸   c                 C  sN   t |tƒr|jddd�nt t|tƒ¡}| jr| j 	|¡ d S | j
 |¡ d S r¹   )r£   r   r»   r²   r¼   r   r0   r½   r˜   r¾   r�   r¿   rÀ   r?   r?   r@   r¿   Æ  s   ÿýzTranslationsConnection.sendrÁ   rÂ   c                 C  rÃ   rÄ   rÇ   rÈ   r?   r?   r@   r¡   Ñ  rÉ   z"TranslationsConnection.parse_eventr«   rQ   rÊ   rË   úQUnion[TranslationsConnection, 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.created", my_handler)

        Or as a decorator::

            @connection.on("session.created")
            def my_handler(event): ...
        NrÎ   rÏ   r9   c                   rÐ   r;   rÑ   rÓ   rÔ   r?   r@   rÕ   ò  rÖ   z,TranslationsConnection.on.<locals>.decoratorr×   rÑ   rØ   r?   rÔ   r@   rÙ   Ü  rÚ   zTranslationsConnection.onrÏ   c                 C  rÛ   rÜ   rÝ   rß   r?   r?   r@   rà   ø  rá   zTranslationsConnection.offc                   râ   )
rã   NTrä   rÎ   rÏ   r9   c                   ræ   rç   rÑ   rÓ   rÔ   r?   r@   rÕ     rè   z.TranslationsConnection.once.<locals>.decoratorr×   rÑ   rØ   r?   rÔ   r@   rå   ý  ré   zTranslationsConnection.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ž   r�   rì   r£   r-   r   )r>   rª   r«   rî   rï   rÊ   r?   r?   r@   rñ     s   	


ÿôz&TranslationsConnection.dispatch_events)r…   ro  rƒ   rp  rU   rV   rW   rX   rY   rZ   r[   rZ   rR   r   rS   r   r„   r‡   r9   rn   )r9   rq  rò   ró   rô   rõ   r;   )r«   rQ   rÊ   rË   r9   rt  )r«   rQ   rÊ   rÏ   r9   rm  rö   )re   rf   rg   r÷   rø   rs   rr  r—   r¢   r¿   r¡   rÙ   rà   rå   rñ   rù   r?   r?   r�   r@   rm  c  s0   
 õ




ÿ
ÿrm  c                   @  rú   )Gr]   a6  
    Context manager over a `TranslationsConnection` that is returned by `realtime.translations.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.realtime.translations.connect(...).enter()
    # ...
    connection.close()
    ```
    NrL   rM   rN   rO   rû   r`   r3   rP   rQ   rR   r   rS   r   rT   r,   rU   rV   rW   rX   rY   rZ   r[   r\   r9   rn   c       
         C  rü   )Nrý   rþ   Tr‹   )Ú&_TranslationsConnectionManager__clientÚ*_TranslationsConnectionManager__connectionÚ%_TranslationsConnectionManager__modelÚ+_TranslationsConnectionManager__extra_queryÚ-_TranslationsConnectionManager__extra_headersÚ<_TranslationsConnectionManager__websocket_connection_optionsÚ/_TranslationsConnectionManager__on_reconnectingÚ+_TranslationsConnectionManager__max_retriesÚ-_TranslationsConnectionManager__initial_delayÚ)_TranslationsConnectionManager__max_delayr   Ú*_TranslationsConnectionManager__send_queuer&   Ú6_TranslationsConnectionManager__event_handler_registryr  r?   r?   r@   rs   ;  r  z&TranslationsConnectionManager.__init__rª   r¸   c                 C  r  r  )	r£   r   r»   r²   r¼   r   r0   r  r¾   rÀ   r?   r?   r@   r¿   Y  r  z"TranslationsConnectionManager.sendr«   rÊ   rË   úXUnion[TranslationsConnectionManager, 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 ``TranslationsConnection.on``.
        NrÎ   rÏ   r9   c                   rÐ   r;   ©r€  rÒ   rÓ   rÔ   r?   r@   rÕ   r  rÖ   z3TranslationsConnectionManager.on.<locals>.decoratorr×   r‚  rØ   r?   rÔ   r@   rÙ   f  r  z TranslationsConnectionManager.onrÏ   c                 C  rÛ   rÜ   )r€  rÞ   rß   r?   r?   r@   rà   x  rá   z!TranslationsConnectionManager.offc                   râ   )
r  NTrä   rÎ   rÏ   r9   c                   ræ   rç   r‚  rÓ   rÔ   r?   r@   rÕ   …  rè   z5TranslationsConnectionManager.once.<locals>.decoratorr×   r‚  rØ   r?   rÔ   r@   rå   }  r  z"TranslationsConnectionManager.oncerm  c                 C  s¢   |   | j| j¡}z.t|| jdur| j nd| j| j| j| j| j| j| jd�	| _	| j
 | j	j¡ | j	 ¡  W | j	S  tyP   d| _	z| ¡  W ‚  tyO   Y ‚ w w )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 = client.realtime.translations.connect(...).enter()
        # ...
        connection.close()
        ```
        Nr‚   )r  rx  ry  rm  r{  r|  r}  r~  r  rv  r€  r  r�   r  r  r  r  r?   r?   r@   Ú	__enter__‹  s4   ÷
÷
ýýüz'TranslationsConnectionManager.__enter__ro  c           
   
   C  sÀ  zddl m} W n ty } ztdƒ|‚d }~ww |  ¡ }ddlm} |jt||ƒr-|n| j	 
|t|tjƒrBt d| ji¡ |¡nd| ji|¥¡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¡ | j# $¡ }	|	 %dd ¡ |	 d¡d u rÌ|	 %dd ¡ |t|ƒfd t&t| '¡ ƒi ƒdœ|	¤ŽS )Nr   )rb   r  r   r  rP   r!  r  r#  r$  r'  r¦   r)  Tr*  c                 S  r/  r?   r0  r3  r?   r?   r@   r6  Ö  r7  z=TranslationsConnectionManager._connect_ws.<locals>.<dictcomp>r8  r9  rý   r:  r;  )(Úwebsockets.sync.clientrb   r=  r   r>  r?  r   r@  r£   ru  rA  rB  rC  rw  rD  r¦   r(  rE  r   rF  rQ   r¥   r,  rG  rH  rI  r
   r   r"  rJ  rK  r   r-  r7   r°   rz  rL  rM  r%   rN  rO  r?   r?   r@   r  ¶  sj   
€ÿÿ
ÿüý
üÿÿüþ



ÿýüz)TranslationsConnectionManager._connect_wsrQ  c                 C  rR  rS  )
ru  rW  rB  rX  rY  r(  r@  rV  rZ  r[  r\  r?   r?   r@   r>  ð  ra  z*TranslationsConnectionManager._prepare_urlrb  rc  rš   rd  re  rf  c                 C  s   | j d ur| j  ¡  d S d S r;   )rv  r  rg  r?   r?   r@   Ú__exit__ü  s   
ÿz&TranslationsConnectionManager.__exit__)r`   r3   rP   rQ   rR   r   rS   r   rT   r,   rU   rV   rW   rX   rY   rZ   r[   rZ   r\   rX   r9   rn   rô   r;   )r«   rQ   rÊ   rË   r9   r�  )r«   rQ   rÊ   rÏ   r9   r]   )r9   rm  )rR   r   rS   r   r9   ro  ri  rj  )re   rf   rg   r÷   rs   r¿   rÙ   rà   rå   rƒ  rk  r  r>  r…  r?   r?   r?   r@   r]   '  rl  r]   c                   @  ó   e Zd Zddd„ZdS )	Ú"BaseTranslationsConnectionResourcer…   rm  r9   rn   c                 C  ro   r;   ©r�   ©r>   r…   r?   r?   r@   rs     rt   z+BaseTranslationsConnectionResource.__init__N)r…   rm  r9   rn   ©re   rf   rg   rs   r?   r?   r?   r@   r‡    ó    r‡  c                   @  ó:   e Zd Zeddd„ƒZedœdd	d
„Zedœddd„ZdS )rn  r9   Ú+TranslationsSessionInputAudioBufferResourcec                 C  r:   r;   )r�  r�   r=   r?   r?   r@   Úinput_audio_buffer	  rB   z.TranslationsSessionResource.input_audio_buffer©Úevent_idr�  ú
str | Omitrn   c                C  s    | j  tttd|dœƒƒ¡ dS ©zÁGracefully close the realtime translation session.

        The server flushes pending
        input audio and emits any remaining translated output before closing the
        session.
        zsession.close)rž   r�  N©r�   r¿   r
   r0   r   ©r>   r�  r?   r?   r@   r    s   ÿz!TranslationsSessionResource.closer   r1   c             	   C  ó"   | j  tttd||dœƒƒ¡ dS ©zãSend this event to update the translation session configuration.

        Translation
        sessions support updates to `audio.output.language`, `audio.input.transcription`,
        and `audio.input.noise_reduction`.
        zsession.update)rž   r   r�  Nr“  ©r>   r   r�  r?   r?   r@   Úupdate  s   þÿz"TranslationsSessionResource.updateN)r9   r�  ©r�  r‘  r9   rn   ©r   r1   r�  r‘  r9   rn   ©re   rf   rg   r   rŽ  r   r  r˜  r?   r?   r?   r@   rn    s
    rn  c                   @  ó   e Zd Zedœddd	„Zd
S )r�  r�  ÚaudiorQ   r�  r‘  r9   rn   c             	   C  r•  ©a˜  
        Send this event to append audio bytes to the translation session input audio buffer.

        WebSocket translation sessions accept base64-encoded 24 kHz PCM16 mono
        little-endian raw audio bytes. Unsupported websocket audio formats return a
        validation error because lower-quality audio materially degrades translation
        quality.

        Translation consumes 200 ms engine frames. For best realtime behavior, append
        audio in 200 ms chunks. If a chunk is shorter, the server buffers it until it
        has enough audio for one frame. If a chunk is longer, the server splits it into
        200 ms frames and enqueues them back-to-back.

        Keep appending silence while the session is active. If a client stops sending
        audio and later resumes, model time treats the resumed audio as contiguous with
        the previous audio rather than as a real-world pause.
        z!session.input_audio_buffer.append)rž   r�  r�  Nr“  ©r>   r�  r�  r?   r?   r@   Úappend(  s   þÿz2TranslationsSessionInputAudioBufferResource.appendN©r�  rQ   r�  r‘  r9   rn   ©re   rf   rg   r   r   r?   r?   r?   r@   r�  '  ó    r�  c                   @  r†  )	Ú'BaseAsyncTranslationsConnectionResourcer…   r|   r9   rn   c                 C  ro   r;   rˆ  r‰  r?   r?   r@   rs   C  rt   z0BaseAsyncTranslationsConnectionResource.__init__N)r…   r|   r9   rn   rŠ  r?   r?   r?   r@   r¤  B  r‹  r¤  c                   @  rŒ  )r~   r9   Ú0AsyncTranslationsSessionInputAudioBufferResourcec                 C  r:   r;   )r¥  r�   r=   r?   r?   r@   rŽ  H  rB   z3AsyncTranslationsSessionResource.input_audio_bufferr�  r�  r‘  rn   c                Ã  s(   �| j  tttd|dœƒƒ¡I dH  dS r’  r“  r”  r?   r?   r@   r  L  s   €ÿz&AsyncTranslationsSessionResource.closer   r1   c             	   Ã  ó*   �| j  tttd||dœƒƒ¡I dH  dS r–  r“  r—  r?   r?   r@   r˜  W  s   €	þÿz'AsyncTranslationsSessionResource.updateN)r9   r¥  r™  rš  r›  r?   r?   r?   r@   r~   G  s    ÿr~   c                   @  rœ  )r¥  r�  r�  rQ   r�  r‘  r9   rn   c             	   Ã  r¦  rž  r“  rŸ  r?   r?   r@   r   i  s   €þÿz7AsyncTranslationsSessionInputAudioBufferResource.appendNr¡  r¢  r?   r?   r?   r@   r¥  h  r£  r¥  )eÚ
__future__r   r²   ÚloggingÚtypesr   Útypingr   r   r   r   r   r	   r
   Ztyping_extensionsr   rB  Zpydanticr   Ú_typesr   r   r   r   Z_utilsr   r   r   r   Z_compatr   Z_modelsr   r   Z	_resourcer   r   Ú_exceptionsr   r   r˜   r   rA   r   r    r!   r"   r#   r$   Z_base_clientr%   Z_event_handlerr&   Z_websocket_connectionr'   r(   Ztypes.websocket_reconnectionr)   r*   Z"types.websocket_connection_optionsr+   r,   Z#types.realtime.realtime_error_eventr-   Z0types.realtime.realtime_translation_client_eventr.   Z0types.realtime.realtime_translation_server_eventr/   Z6types.realtime.realtime_translation_client_event_paramr0   Z@types.realtime.realtime_translation_session_update_request_paramr1   r„  r2   ro  Zwebsockets.asyncio.clientr€   r<   r3   r4   Ú__all__Ú	getLoggerre   r7   rø   r5   r6   rC   ri   rH   rj   r|   rk   rm  r]   r‡  rn  r�  r¤  r~   r¥  r?   r?   r?   r@   Ú<module>   sh   $ 33				 I ] E ]!