o
    iD                     @   s  d Z ddlmZmZmZmZmZ erddlmZ ddl	m
Z
 ddlmZ e r-ddlmZ dedefdd	Z	
	
	
		
d9dddedededee ded dee dedee dd
fddZ		
	
	
	
	
		
d:dddedededee dee dee dee ded dedee dd
fdd Z	!	
	
	
	
	
	
	
	
	
d;d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eef  d*eeeef  dee dd
fd+d,Z	
	
	
	
	
	
	
	
	
	
d<dddedededee dee d-ee ded d%eee  d.ee dee d/ee d0ee d1ee dd
fd2d3Z	
	
	
	
	
	
	
	
d=dddededed4eeeef  dee d/ee d5ee d6eee  d%eee  d.ee d1ee dd
fd7d8Zd
S )>a  Functions for adding attributes to OpenTelemetry spans.

This module provides specialized functions for adding service-specific
attributes to OpenTelemetry spans, following standard semantic conventions
where applicable and Pipecat-specific conventions for additional context.
    )TYPE_CHECKINGAnyDictListOptional)Span)ServiceSettings)is_tracing_availableservice_namereturnc                 C   sL   ddddddd}| |v r||  S |  dr | dd	  }|S |  }|S )
a  Extract the standardized gen_ai.system value from a service class name.

    Source:
    https://opentelemetry.io/docs/specs/semconv/attributes-registry/gen-ai/#gen-ai-system

    Uses standard OTel names where possible, with special case mappings for
    service names that don't follow the pattern.

    Args:
        service_name: The service class name to extract system name from.

    Returns:
        The standardized gen_ai.system value.
    zaws.bedrockzaz.ai.openai
gcp.geminizgcp.vertex_aixai)AWSBedrockLLMServiceAzureLLMServiceGoogleLLMServiceGoogleLLMOpenAIBetaServiceGoogleVertexLLMServiceGrokLLMService
LLMServiceNi)endswithlower)r
   SPECIAL_CASE_MAPPINGSprovider r   \/home/ubuntu/.local/lib/python3.10/site-packages/pipecat/utils/tracing/service_attributes.py$_get_gen_ai_system_from_service_name   s   
r   Nttsspanr   modelvoice_idtextsettingsr   character_countoperation_namettfbc	                 K   s   |  d|dd  |  d| |  d| |  dd |  d| |r,|  d	| |d
ur6|  d| |d
ur@|  d| |r_|  D ]\}
}t|tttt	fr^|  d|
 | qH|	 D ]\}
}t|tttt	frv|  |
| qcd
S )a#  Add TTS-specific attributes to a span.

    Args:
        span: The span to add attributes to.
        service_name: Name of the TTS service (e.g., "cartesia").
        model: Model name/identifier.
        voice_id: Voice identifier.
        text: The text being synthesized.
        settings: Service configuration settings.
        character_count: Number of characters in the text.
        operation_name: Name of the operation (default: "tts").
        ttfb: Time to first byte in seconds.
        **kwargs: Additional attributes to add.
    gen_ai.system
TTSService gen_ai.request.modelgen_ai.operation.namegen_ai.output.typespeechr   r    Nzmetrics.character_countmetrics.ttfb	settings.
set_attributereplacer   given_fieldsitems
isinstancestrintfloatbool)r   r
   r   r   r    r!   r"   r#   r$   kwargskeyvaluer   r   r   add_tts_span_attributesC   s*   r;   sttF
transcriptis_finallanguageuser_idvad_enabledc                 K   s  |  d|dd  |  d| |  d| |  d|	 |r&|  d| |dur0|  d	| |r8|  d
| |r@|  d| |
durJ|  d|
 |ri|  D ]\}}t|tttt	frh|  d| | qR| D ]\}}t|tttt	fr|  || qmdS )a  Add STT-specific attributes to a span.

    Args:
        span: The span to add attributes to.
        service_name: Name of the STT service (e.g., "deepgram").
        model: Model name/identifier.
        operation_name: Name of the operation (default: "stt").
        transcript: The transcribed text.
        is_final: Whether this is a final transcript.
        language: Detected or configured language.
        user_id: User ID associated with the audio being transcribed.
        settings: Service configuration settings.
        vad_enabled: Whether voice activity detection is enabled.
        ttfb: Time to first byte in seconds.
        **kwargs: Additional attributes to add.
    r%   
STTServicer'   r(   r)   rA   r=   Nr>   r?   r@   r,   r-   r.   )r   r
   r   r#   r=   r>   r?   r@   r!   rA   r$   r8   r9   r:   r   r   r   add_stt_span_attributesz   s0   rC   Tstreammessagesoutputtools
tool_counttool_choicesystem
parametersextra_parametersc                 K   s|  |  dt| |  d| |  dd |  dd |  d| |r(|  d| |r0|  d	| |r8|  d
| |durB|  d| |rJ|  d| |	rR|  d|	 |dur\|  d| |
r|
 D ]$\}}t|ttttfr|dv r}|  d| | qb|  d| | qb|r| D ]\}}t|ttttfr|  d| | q| D ]\}}t|ttttfr|  || qdS )a  Add LLM-specific attributes to a span.

    Args:
        span: The span to add attributes to.
        service_name: Name of the LLM service (e.g., "openai").
        model: Model name/identifier.
        stream: Whether streaming is enabled.
        messages: JSON-serialized messages.
        output: Aggregated output text from the LLM.
        tools: JSON-serialized tools configuration.
        tool_count: Number of tools available.
        tool_choice: Tool selection configuration.
        system: System message.
        parameters: Service parameters.
        extra_parameters: Additional parameters.
        ttfb: Time to first byte in seconds.
        **kwargs: Additional attributes to add.
    r%   r(   r)   chatr*   r    rD   inputrF   rG   NrH   rI   rJ   r,   )temperature
max_tokensmax_completion_tokenstop_ptop_kfrequency_penaltypresence_penaltyseedzgen_ai.request.zparam.zextra.)r/   r   r2   r3   r4   r5   r6   r7   )r   r
   r   rD   rE   rF   rG   rH   rI   rJ   rK   rL   r$   r8   r9   r:   r   r   r   add_llm_span_attributes   sH   #
rW   
modalitiestools_serializedis_inputtext_outputaudio_data_sizec                 K   s*  |  dd |  d| |  d| |  d| |r |  d| |r(|  d| |r0|  d| |
rB|  d	|
 |d
urB|  d| |rJ|  d| |d
urT|  d| |r|  dt| |  dd g }|D ]'}t|tr{d|v r{||d  qht|drt|dd
}|d
ur|| qh|r|  dd| |	r|  d|	 |r| 	 D ]N\}}t|t
tttfr|  d| | q|dkr|rt|dr|jd
ur|  d|j t|dr|jr|  d|jj t|dr|jr|  d|jj q|	 D ]\}}t|t
tttfr|  || qd
S )ad  Add Gemini Live specific attributes to a span.

    Args:
        span: The span to add attributes to.
        service_name: Name of the service.
        model: Model name/identifier.
        operation_name: Name of the operation (setup, model_turn, tool_call, etc.).
        voice_id: Voice identifier used for output.
        language: Language code for the session.
        modalities: Supported modalities (e.g., "AUDIO", "TEXT").
        settings: Service configuration settings.
        tools: Available tools/functions list.
        tools_serialized: JSON-serialized tools for detailed inspection.
        transcript: Transcription text.
        is_input: Whether transcript is input (True) or output (False).
        text_output: Text output from model.
        audio_data_size: Size of audio data in bytes.
        **kwargs: Additional attributes to add.
    r%   r   r(   r)   service.operationr   r?   rX   r=   Ntranscript.is_inputr[   audio.data_size_bytestools.counttools.availableTnametools.names,tools.definitionsr-   vaddisabledzsettings.vad.disabledstart_sensitivityzsettings.vad.start_sensitivityend_sensitivityzsettings.vad.end_sensitivity)r/   lenr3   dictappendhasattrgetattrjoinr1   r2   r4   r5   r6   r7   rg   rh   r:   ri   )r   r
   r   r#   r   r?   rX   r!   rG   rY   r=   rZ   r[   r\   r8   
tool_namestool	tool_namer9   r:   r   r   r   add_gemini_live_span_attributes  sj   %

rs   session_propertiescontext_messagesfunction_callsc                 K   s  |  dd |  d| |  d| |  d| |r*|  d| |dur*|  d| |r2|  d	| |dur<|  d
| |	r|  dt|	 |  dd g }|	D ]7}t|trcd|v rc||d  qPt|dro||j qPt|trd|v rd|d v r||d d  qP|r|  dd| |
r|  d|
 |r|  dt| |r|d }t|dr|  d|j nt|trd|v r|  d|d  |r'| D ]V\}}t|t	t
ttfr|  d| | q|dkr&|dur&t|tr|  d| qt|tr&|  dd | D ]\}}t|t	t
ttfr$|  d| | qq| D ]\}}t|t	t
ttfr?|  || q+dS )a  Add OpenAI Realtime specific attributes to a span.

    Args:
        span: The span to add attributes to.
        service_name: Name of the service.
        model: Model name/identifier.
        operation_name: Name of the operation (setup, transcription, response, etc.).
        session_properties: Session configuration properties.
        transcript: Transcription text.
        is_input: Whether transcript is input (True) or output (False).
        context_messages: JSON-serialized context messages.
        function_calls: Function calls being made.
        tools: Available tools/functions list.
        tools_serialized: JSON-serialized tools for detailed inspection.
        audio_data_size: Size of audio data in bytes.
        **kwargs: Additional attributes to add.
    r%   openair(   r)   r]   r=   Nr^   rN   r_   r`   ra   Trb   functionrc   rd   re   zfunction_calls.countr   zfunction_calls.first_namezsession.turn_detectionzsession.turn_detection.enabledzsession.turn_detection.)r/   rj   r3   rk   rl   rm   rb   ro   r2   r4   r5   r6   r7   )r   r
   r   r#   rt   r=   rZ   ru   rv   rG   rY   r\   r8   rp   rq   callr9   r:   td_keytd_valuer   r   r   #add_openai_realtime_span_attributes  sl   !


r}   )NNNr   N)r<   NNNNNFN)
TNNNNNNNNN)
NNNNNNNNNN)NNNNNNNN)__doc__typingr   r   r   r   r   opentelemetry.tracer   pipecat.services.settingsr   pipecat.utils.tracing.setupr	   r4   r   r5   r6   r;   r7   rC   rW   rs   r}   r   r   r   r   <module>   sn  ,	
;	

D	

b
	

n
	

