o
    ưi.                     @   sh   d Z ddlZddlZddlmZ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 deZdS )z
This file contains the handler for AWS Bedrock Nova Sonic realtime API.

This uses aws_sdk_bedrock_runtime for bidirectional streaming with Nova Sonic.
    N)AnyOptional)verbose_proxy_logger)Logging   )
BaseAWSLLM   )BedrockRealtimeConfigc                $       s  e Zd ZdZ fddZ														d dedededee d	ee d
ee	 dee dee dee dee dee dee dee dee dee dee dee f"ddZ
dededededef
ddZdedededededefddZ  ZS )!BedrockRealtimez=Handler for Bedrock Nova Sonic realtime speech-to-speech API.c                    s   t    d S )N)super__init__)self	__class__ \/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/bedrock/realtime/handler.pyr      s   zBedrockRealtime.__init__Nmodel	websocketlogging_objapi_baseapi_keytimeoutaws_region_nameaws_access_key_idaws_secret_access_keyaws_session_tokenaws_role_nameaws_session_nameaws_profile_nameaws_web_identity_tokenaws_sts_endpointaws_bedrock_runtime_endpointaws_external_idc           !         s  zddl m}m} ddlm} ddlm} W n ty"   tdw |du r1d|i}| ||}|dur8|}n|dur?|}nd| d	}t	
d
| d|  |||| d}||d}t }zA|||dI dH }t	
d dddddddd}t| |||||}t| ||||||}tj||ddI dH  W dS  ty }  z#t	d|   z|jddt|  dI dH  W   ty   Y  w d} ~ ww )a`  
        Establish bidirectional streaming connection with Bedrock Nova Sonic.

        Args:
            model: Model ID (e.g., 'amazon.nova-sonic-v1:0')
            websocket: Client WebSocket connection
            logging_obj: LiteLLM logging object
            aws_region_name: AWS region
            Various AWS authentication parameters
        r   )BedrockRuntimeClient0InvokeModelWithBidirectionalStreamOperationInput)Config)EnvironmentCredentialsResolverzRMissing aws_sdk_bedrock_runtime. Install with: pip install aws-sdk-bedrock-runtimeNr   zhttps://bedrock-runtime.z.amazonaws.comz Bedrock Realtime: Connecting to z with model )endpoint_uriregionZ!aws_credentials_identity_resolver)config)Zmodel_idz2Bedrock Realtime: Bidirectional stream establishedcurrent_output_item_idcurrent_response_idcurrent_conversation_idcurrent_delta_chunkscurrent_item_chunkscurrent_delta_typesession_configuration_requestT)return_exceptionsz)Error in BedrockRealtime.async_realtime: i  zInternal error: )codereason)Zaws_sdk_bedrock_runtime.clientr#   r$   Zaws_sdk_bedrock_runtime.configr%   Z$smithy_aws_core.identity.environmentr&   ImportErrorZ_get_aws_region_namer   debugr	   Z&invoke_model_with_bidirectional_streamasynciocreate_task_forward_client_to_bedrock_forward_bedrock_to_clientgather	Exception	exceptionclosestr)!r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   kwargsr#   r$   r%   r&   Zoptional_paramsr'   r)   Zbedrock_clienttransformation_configbedrock_streamsession_stateZclient_to_bedrock_taskZbedrock_to_client_tasker   r   r   async_realtime   s   


 zBedrockRealtime.async_realtime	client_wsrB   rA   rC   c                    s  zMddl m}m} 	 | I dH }td|dd   |j|||dd}	|	D ]"}
|||
d	d
d}|j	
|I dH  td|
dd   q+q ty } z,tjd| dd z
|j	 I dH  W n	 tys   Y nw W Y d}~dS W Y d}~dS d}~ww )z9Forward messages from client WebSocket to Bedrock stream.r   )BidirectionalInputPayloadPart,InvokeModelWithBidirectionalStreamInputChunkTNz(Bedrock Realtime: Received from client:    r1   )messager   r1   utf-8)bytes_)valuez#Bedrock Realtime: Sent to Bedrock: z$Client to Bedrock forwarding ended: exc_info)Zaws_sdk_bedrock_runtime.modelsrG   rH   Zreceive_textr   r6   Ztransform_realtime_requestgetencodeZinput_streamsendr<   r>   )r   rF   rB   rA   r   rC   rG   rH   rJ   Ztransformed_messagesZbedrock_messageeventrD   r   r   r   r9      sN   		
z*BedrockRealtime._forward_client_to_bedrockc                    s  z	 |  I dH }|d  I dH }|jr|jjr|jjd}	td|	dd   ddlm}
 |	d	|	d
|	d|	d|	d|	d|	dd}|j
|	|||d}||	d	|	d
|	d|	d|	d|	d|	dd |	dg }|D ]}t|}||I dH  td|dd   qq ty } z+tjd| dd z	| I dH  W n	 ty   Y nw W Y d}~dS W Y d}~dS d}~ww )z9Forward messages from Bedrock stream to client WebSocket.TNr   rK   z)Bedrock Realtime: Received from Bedrock: rI   r   )RealtimeResponseTransformInputr+   r,   r-   r.   r/   r0   r1   r*   )rJ   r   r   !realtime_response_transform_inputresponsez"Bedrock Realtime: Sent to client: z$Bedrock to client forwarding ended: rN   )Zawait_outputZreceiverM   rL   decoder   r6   Zlitellm.types.realtimerT   rP   Ztransform_realtime_responseupdatejsondumpsZ	send_textr<   r>   )r   rB   rF   rA   r   r   rC   outputresultZbedrock_responserT   rU   Ztransformed_responseZopenai_messagesZopenai_messageZmessage_jsonrD   r   r   r   r:      s   


M
z*BedrockRealtime._forward_bedrock_to_client)NNNNNNNNNNNNNN)__name__
__module____qualname____doc__r   r?   r   LiteLLMLoggingr   floatrE   r	   dictr9   r:   __classcell__r   r   r   r   r
      s    	

 
5r
   )r`   r7   rY   typingr   r   Zlitellm._loggingr   Z*litellm.litellm_core_utils.litellm_loggingr   ra   Zbase_aws_llmr   Ztransformationr	   r
   r   r   r   r   <module>   s    