
    j                     n    d Z ddlmZmZmZ ddlmZ ddlmZ ddl	m
Z ddlmZ dd	lmZ  G d
 de      Zy)z
This file contains the calling OpenAI's `/v1/realtime` endpoint.

This requires websockets, and is currently only supported on LiteLLM Proxy.
    )AnyOptionalcast))REALTIME_WEBSOCKET_MAX_MESSAGE_SIZE_BYTES)RealtimeQueryParams   )Logging)RealTimeStreaming   )OpenAIChatCompletionc                   r    e Zd ZdededefdZ	 	 	 	 	 ddedededee   d	ee   d
ee   dee	   dee   fdZ
y)OpenAIRealtimeapi_basequery_paramsreturnc                     ddl m} |j                  dd      }|j                  dd      } ||      }|j                  d      }|r|j                  |	      }t	        |      S )
zd
        Construct the backend websocket URL with all query parameters (including 'model').
        r   )URLzhttps://zwss://zhttp://zws://z/v1/realtime)path)params)httpxr   replace	copy_withstr)selfr   r   r   urls        }/Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/litellm/llms/openai/realtime/handler.py_construct_urlzOpenAIRealtime._construct_url   s`     	##J9##Iw7(mmmm0--|-4C3x    Nmodel	websocketlogging_objapi_keyclienttimeoutc	                   K   dd l }	ddlm}
 |d}|t        d      |d|i}| j	                  ||      }	 |	j                  |d| ddt        	      4 d {   }t        |t        |
|      |      }|j                          d {    d d d       d {    y 7 D7 7 	# 1 d {  7  sw Y   y xY w# |	j                  j                  $ r9}|j                  |j                  t        |      
       d {  7   Y d }~y d }~wt        $ rx}	 |j                  ddt        |       
       d {  7   nD# t         $ r3}dt        |      v sdt        |      v rnt        d|       Y d }~nd }~ww xY wY d }~y Y d }~y d }~ww xY ww)Nr   )ClientConnectionzhttps://api.openai.com/z-api_key is required for OpenAI realtime callsr   zBearer zrealtime=v1)AuthorizationzOpenAI-Beta)extra_headersmax_size)codereasoni  zInternal server error: zalready completedzwebsocket.closez*Unexpected error while closing WebSocket: )
websocketswebsockets.asyncio.clientr&   
ValueErrorr   connectr   r
   r   bidirectional_forward
exceptionsInvalidStatusCodeclosestatus_coder   	ExceptionRuntimeError)r   r   r    r!   r   r"   r#   r$   r   r,   r&   r   
backend_wsrealtime_streamingeclose_errors                   r   async_realtimezOpenAIRealtime.async_realtime"   s     	>0H?LMM #U+L!!(L9	!))'.wi%8#0 C *   %6t$4jA;&" )>>@@@   A    $$66 	E//q}}SV/DDD 	oo(?Ax&H &      
&#k*::>OSVT ?  $D[MR  

 	s   4F"B9 BB9 +B$B 	B$B9 B"B9 FB9  B$"B9 $B6*B-+B62B9 5F6B9 9F
)D;C><DFF
"D<5D86D<;F<	E8)E3.F3E88F;
FF

F)NNNNN)__name__
__module____qualname__r   r   r   r   LiteLLMLoggingr   floatr;    r   r   r   r      s    s :M RU * #'!% $#'6:66 6 $	6
 3-6 #6 6 %6 236r   r   N)__doc__typingr   r   r   litellm.constantsr   litellm.types.realtimer   "litellm_core_utils.litellm_loggingr	   r?   %litellm_core_utils.realtime_streamingr
   openair   r   rA   r   r   <module>rI      s0    ' & G 6 L G )G) Gr   