Ë
    uPêjL*  ã                   óJ   — d Z ddlZddlmZ ddlmZ ddlmZ  G d„ de«      Zy)a_  
Vertex AI Realtime (BidiGenerateContent) config.

Extends GeminiRealtimeConfig but adapts the WSS URL and auth header for the
Vertex AI endpoint instead of Google AI Studio.

URL pattern:
  wss://{location}-aiplatform.googleapis.com/ws/
      google.cloud.aiplatform.v1.LlmBidiService/BidiGenerateContent

Auth: OAuth2 Bearer token (not an API key).
é    N)ÚFinal)Úverbose_logger)ÚGeminiRealtimeConfigc            
       óü   ‡ — e Zd ZdZdedededdfˆ fd„Zdefd„Z	 dd	edz  d
ededz  defd„Z	 dde	d
ededz  de	fd„Z
ddedefd„Zd
edefd„Zd
edefd„Zd
ede	de	fd„Z	 dded
ededz  dee   fˆ fd„Zˆ xZS )ÚVertexAIRealtimeConfigzÇ
    Realtime config for Vertex AI (BidiGenerateContent).

    ``access_token`` and ``project`` must be pre-resolved by the caller
    (they require async I/O) and injected at construction time.
    Úaccess_tokenÚprojectÚlocationÚreturnNc                 óL   •— t         ‰| �  «        || _        || _        || _        y ©N)ÚsuperÚ__init__Ú_access_tokenÚ_projectÚ	_location)Úselfr   r	   r
   Ú	__class__s       €ús/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/llms/vertex_ai/realtime/transformation.pyr   zVertexAIRealtimeConfig.__init__   s$   ø€ Ü‰ÑÔØ)ˆÔØˆŒØ!ˆ�ó    c                  ó   — y)NF© )r   s    r   Ú_include_function_response_idz4VertexAIRealtimeConfig._include_function_response_id#   s   € Ør   Úapi_baseÚmodelÚapi_keyc                 ó´   — |r8|j                  d«      }|j                  dd«      j                  dd«      }|› d�S | j                  }|dk(  rd}n|› d	�}d|› d�S )
zØ
        Build the Vertex AI Live WSS endpoint URL.

        If *api_base* is provided it overrides the default aiplatform host,
        allowing enterprise / VPC-SC deployments to point at a custom gateway.
        ú/zhttps://zwss://zhttp://zws://zA/ws/google.cloud.aiplatform.v1.LlmBidiService/BidiGenerateContentÚglobalzaiplatform.googleapis.comz-aiplatform.googleapis.com)ÚrstripÚreplacer   )r   r   r   r   Úbaser
   Úhosts          r   Úget_complete_urlz'VertexAIRealtimeConfig.get_complete_url*   ss   € ñ à—?‘? 3Ó'ˆDØ—<‘< 
¨HÓ5×=Ñ=¸iÈÓQˆDØ�VÐ\Ð]Ð]àŸ.™.ˆØ�xÒØ.‰Dà�ZÐ9Ð:ˆDà˜�vÐ^Ð_Ð_r   Úheadersc                 óv   — t        |«      }d| j                  › �|d<   | j                  r| j                  |d<   |S )uÞ   
        Return headers with a Bearer token for Vertex AI.

        ``api_key`` is intentionally ignored â€” Vertex AI uses OAuth2 tokens,
        not API keys.  The token was resolved at config-construction time.
        zBearer ÚAuthorizationzx-goog-user-project)Údictr   r   )r   r%   r   r   s       r   Úvalidate_environmentz+VertexAIRealtimeConfig.validate_environmentH   s@   € ô �w“-ˆØ%,¨T×-?Ñ-?Ð,@Ð#Aˆ�Ñ Ø�=Š=Ø-1¯]©]ˆGÐ)Ñ*Øˆr   Úinput_audio_formatc                 ó2   — ddddœ}|j                  |d«      S )Nzaudio/pcm;rate=24000z
audio/pcmuz
audio/pcma)Úpcm16Ú	g711_ulawÚ	g711_alawzapplication/octet-stream)Úget)r   r*   Ú
mime_typess      r   Úget_audio_mime_typez*VertexAIRealtimeConfig.get_audio_mime_type^   s)   € ð ,Ø%Ø%ñ	
ˆ
ð �~‰~Ð0Ð2LÓMÐMr   c                 óª   — ddl m} ddlm} dg}d| j                  › d| j
                  › d|› �}|d|id	d
ddœii i dœ}t        j                  d|i«      S )ad  
        Return the JSON setup message for Vertex AI Live.

        Vertex AI requires the fully-qualified model path:
        ``projects/{project}/locations/{location}/publishers/google/models/{model}``

        Also enables automatic activity detection (server VAD) and output
        audio transcription so the proxy forwards transcript events.
        r   )ÚBidiGenerateContentSetup)ÚGeminiResponseModalitiesÚAUDIOú	projects/ú/locations/ú/publishers/google/models/ÚresponseModalitiesÚautomaticActivityDetectionFé   )ÚdisabledÚsilenceDurationMs)r   ÚgenerationConfigÚrealtimeInputConfigÚinputAudioTranscriptionÚoutputAudioTranscriptionÚsetup)Úlitellm.types.llms.geminir3   Úlitellm.types.llms.vertex_air4   r   r   ÚjsonÚdumps)r   r   r3   r4   Úresponse_modalitiesÚfull_model_pathÚsetup_configs          r   Úsession_configuration_requestz4VertexAIRealtimeConfig.session_configuration_requestk   s}   € õ 	GÝIàFMÀYÐØ#,¨T¯]©]¨O¸;ÀtÇ~Á~ÐFVÐVpÐqvÐpwÐ!xˆà$Ø!5Ð7JÐ Kð -Ø %Ø),ñ/ð$ð (*à(*ñ9
ˆô �z‰z˜7 LÐ1Ó2Ð2r   c                 ó@   — d| j                   › d| j                  › d|› �S )z9Return the fully-qualified Vertex AI model resource path.r6   r7   r8   )r   r   )r   r   s     r   Ú_vertex_model_pathz)VertexAIRealtimeConfig._vertex_model_path�   s'   € à˜4Ÿ=™=˜/¨°T·^±^Ð4DÐD^Ð_dÐ^eÐfÐfr   Úsession_paramsc                 ó  — | j                  |«      }| j                  i |¬«      }| j                  |«      |d<   |j                  di «      }|j                  ddg«       |j	                  d«      }t        |t        «      xr |j	                  d«      du }|j                  d	i «      }|j                  d
i «      }|sd|d<   |j                  dd«       |j                  di «       |j                  di «       | j                  ||«      S )zHBuild Vertex AI setup configuration with proper model path and defaults.)Úoptional_paramsÚnon_default_paramsr   r>   r9   r5   Úturn_detectionÚcreate_responseFr?   r:   r<   r=   r;   r@   rA   )Ú&_normalize_session_payload_for_mappingÚmap_openai_paramsrL   Ú
setdefaultr/   Ú
isinstancer(   Ú_finalize_gemini_live_setup)	r   r   rM   rI   Úgeneration_configÚclient_turn_detectionÚclient_disabled_auto_responseÚrealtime_input_configÚautomatic_detections	            r   Ú_build_vertex_ai_setup_configz4VertexAIRealtimeConfig._build_vertex_ai_setup_config“   s&  € ð ×DÑDÀ^ÓTˆØ"×4Ñ4ÀRÐ\jÐ4Ókˆð !%× 7Ñ 7¸Ó >ˆ�WÑð $0×#:Ñ#:Ð;MÈrÓ#RÐØ×$Ñ$Ð%9¸G¸9ÔEð (6×'9Ñ'9Ð:JÓ'KÐäÐ,¬dÓ3ÒmÐ8M×8QÑ8QÐRcÓ8dÐhmÐ8mð 	&ð (4×'>Ñ'>Ð?TÐVXÓ'YÐØ%:×%EÑ%EÐFbÐdfÓ%gÐÙ,Ø.3Ð 
Ñ+Ø×&Ñ&Ð':¸CÔ@à×ÑÐ 9¸2Ô>Ø×ÑÐ :¸BÔ?à×/Ñ/°°|ÓDÐDr   ÚmessagerJ   c                 ó  •— t        j                  |«      }|j                  d«      }|dk(  rÏ|€T| j                  ||j                  d«      xs i «      }t        j                  d|i«      }t        j                  d«       |gS t        j                  |j                  d«      xs i «      }t        |t        «      r*|j                  d«      du rt        j                  d«       g S t        j                  d	«       g S t        ‰	| �5  |||«      S )
aÉ  
        Translate OpenAI realtime client messages to Vertex AI format.

        On the first ``session.update`` (when no setup has been sent yet) the
        full ``BidiGenerateContentSetup`` is built with Vertex AI's model path
        and forwarded. Any later ``session.update`` is dropped: Vertex AI
        documents ``setup`` as the first-and-only client message, and a second
        ``setup`` closes the connection with a 1007 policy error.
        Útypezsession.updateÚsessionrB   z?Vertex AI Realtime: Sending initial setup with tools to backendrR   Fuù   Vertex AI Realtime: Dropping subsequent session.update (turn_detection.create_response=False) â€” Vertex Live rejects a second setup message. Audio-transcription guardrails cannot suppress the model's auto-response on Vertex AI in non-deferred mode.z@Vertex AI Realtime: Ignoring session.update (setup already sent))rE   Úloadsr/   r]   rF   r   Údebugr   Ú_extract_turn_detectionrV   r(   Úwarningr   Útransform_realtime_request)
r   r^   r   rJ   Újson_messageÚmsg_typerI   Úgemini_setup_msgrY   r   s
            €r   rf   z1VertexAIRealtimeConfig.transform_realtime_request¾   s  ø€ ô #Ÿj™j¨Ó1ˆØ&×*Ñ*¨6Ó2ˆàÐ'Ò'Ø,Ð4Ø&*×&HÑ&HÈÐP\×P`ÑP`ÐajÓPkÒPqÐoqÓ&r�Ü*.¯*©*°g¸|Ð5LÓ*MÐ ä×$Ñ$Ð%fÔgØ(Ð)Ð)ô %9×$PÑ$PÐQ]×QaÑQaÐbkÓQlÒQrÐprÓ$sÐ!ÜÐ/´Ô6Ð;P×;TÑ;TÐUfÓ;gÐkpÑ;pÜ×&Ñ&ð6ôð ˆIô ×$Ñ$Ð%gÔhØˆIä‰wÑ1°'¸5ÐB_Ó`Ð`r   r   )r,   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústrr   Úboolr   r$   r(   r)   r1   rJ   rL   r]   Úlistrf   Ú__classcell__)r   s   @r   r   r      sJ  ø„ ñð" Sð "°3ð "À#ð "È$õ "ð¨tó ð #ñ	`à˜‘*ð`ð ð`ð �t‘ð	`ð
 
ó`ðD #ñ	àðð ðð �t‘ð	ð
 
óñ,N°cð NÈó Nð3°3ð 3¸3ó 3ðHg¨ð g°ó gð)E°3ð )EÈð )EÐQUó )Eð^ 59ñ	,aàð,að ð,að (+¨T¡zð	,að
 
ˆc‰÷,añ ,ar   r   )	rm   rE   Útypingr   Úlitellmr   Ú+litellm.llms.gemini.realtime.transformationr   r   r   r   r   ú<module>ru      s)   ðñó Ý å "Ý LôUaÐ1õ Uar   