
    qiD                        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Z	 	 	 	 	 d/d
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Z	 	 	 	 	 	 	 	 d0d
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Z	 	 	 	 	 	 	 	 	 	 d1d
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$Z	 	 	 	 	 	 	 	 	 	 d2d
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   d(ee   d)ee   dd	fd*Z	 	 	 	 	 	 	 	 d3d
ddededed+eeeef      dee   d'ee   d,ee   d-eee      deee      d&ee   d)ee   dd	fd.Zy	)4a  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                     ddddddd}| |v r||    S | j                  d      r| dd	 j                         }|S | j                         }|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providers      Z/opt/pipecat/venv/lib/python3.12/site-packages/pipecat/utils/tracing/service_attributes.py$_get_gen_ai_system_from_service_namer      ss    " !.)(&2"1 ,,$\22\*%++- O  %%'O    Nspanr   modelvoice_idtextsettingsr	   character_countoperation_namettfbc	                    | j                  d|j                  dd      j                                | j                  d|       | j                  d|       | j                  dd       | j                  d|       |r| j                  d	|       || j                  d|       || j                  d|       |r\|j                         j	                         D ];  \  }
}t        |t        t        t        t        f      s'| j                  d|
 |       = |	j	                         D ]8  \  }
}t        |t        t        t        t        f      s'| j                  |
|       : y
)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values               r   add_tts_span_attributesr=   C   sA   6 	(<(<\2(N(T(T(VW-u5.?+X6z8, 64("4oF>40 "//1779 	=JC%#sE4!89""Yse#4e<	=
 lln +
Uec3t45sE*+r   
transcriptis_finallanguageuser_idvad_enabledc                    | j                  d|j                  dd      j                                | j                  d|       | j                  d|       | j                  d|	       |r| j                  d|       || j                  d	|       |r| j                  d
|       |r| j                  d|       |
| j                  d|
       |r\|j                         j	                         D ];  \  }}t        |t        t        t        t        f      s'| j                  d| |       = |j	                         D ]8  \  }}t        |t        t        t        t        f      s'| j                  ||       : y)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+   rB   r>   Nr?   r@   rA   r.   r/   r0   )r   r   r   r$   r>   r?   r@   rA   r"   rB   r%   r:   r;   r<   s                 r   add_stt_span_attributesrE   z   sU   > 	(<(<\2(N(T(T(VW-u5.?}k2 <4:x0:x09g.>40 "//1779 	=JC%#sE4!89""Yse#4e<	=
 lln +
Uec3t45sE*+r   streammessagesoutputtools
tool_counttool_choicesystem
parametersextra_parametersc                    | j                  dt        |             | j                  d|       | j                  dd       | j                  dd       | j                  d|       |r| j                  d|       |r| j                  d	|       |r| j                  d
|       || j                  d|       |r| j                  d|       |	r| j                  d|	       || j                  d|       |
rh|
j                         D ]U  \  }}t        |t        t
        t        t        f      s'|dv r| j                  d| |       A| j                  d| |       W |rN|j                         D ];  \  }}t        |t        t
        t        t        f      s'| j                  d| |       = |j                         D ]8  \  }}t        |t        t
        t        t        f      s'| j                  ||       : y)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!   rF   inputrH   rI   NrJ   rK   rL   r.   )temperature
max_tokensmax_completion_tokenstop_ptop_kfrequency_penaltypresence_penaltyseedzgen_ai.request.zparam.zextra.)r1   r   r4   r5   r6   r7   r8   r9   )r   r   r   rF   rG   rH   rI   rJ   rK   rL   rM   rN   r%   r:   r;   r<   s                   r   add_llm_span_attributesrZ      s   F 	(L\(Z[-u5.7+V4x( 7H-8V,7E*<4=+68V,>40 $**, 	>JC%#sE4!89 	 	 &&'>F&&u~u=	>" *002 	:JC%#sE4!89""VC5>59	:
 lln +
Uec3t45sE*+r   
modalitiestools_serializedis_inputtext_outputaudio_data_sizec                    | j                  dd       | j                  d|       | j                  d|       | j                  d|       |r| j                  d|       |r| j                  d|       |r| j                  d|       |
r&| j                  d	|
       || j                  d|       |r| j                  d|       || j                  d|       |r| j                  dt        |             | j                  dd       g }|D ]Y  }t        |t              rd|v r|j	                  |d          ,t        |d      s9t        |dd
      }|I|j	                  |       [ |r!| j                  ddj                  |             |	r| j                  d|	       |r|j                         j                         D ]  \  }}t        |t        t        t        t        f      r| j                  d| |       <|dk(  sB|sEt        |d      r(|j                  | j                  d|j                         t        |d      r2|j                  r&| j                  d|j                  j                          t        |d      s|j"                  s| j                  d|j"                  j                           |j                         D ]8  \  }}t        |t        t        t        t        f      s'| j                  ||       : y
)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@   r[   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)r1   lenr5   dictappendhasattrgetattrjoinr3   r4   r6   r7   r8   r9   rk   rl   r<   rm   )r   r   r   r$   r    r@   r[   r"   rI   r\   r>   r]   r^   r_   r:   
tool_namestool	tool_namer;   r<   s                       r   add_gemini_live_span_attributesrw     s   J 	5-u5.?*N; :x0:x0<4<44h?=+6"2OD=#e*5,d3 
 	1D$%&D.!!$v,/v&#D&$7	(%%i0	1 }chhz.BC.0@A "//1779 	dJC%#sE4!89""Yse#4e<%5*-%..2L&&'>O5"565;R;R&&8%:Q:Q:W:W 5"349N9N&&'EuG\G\GbGbc	d lln +
Uec3t45sE*+r   session_propertiescontext_messagesfunction_callsc           	      z   | j                  dd       | j                  d|       | j                  d|       | j                  d|       |r&| j                  d|       || j                  d|       |r| j                  d	|       || j                  d
|       |	r| j                  dt        |	             | j                  dd       g }|	D ]  }t        |t              rd|v r|j	                  |d          ,t        |d      r|j	                  |j                         Tt        |t              sed|v sjd|d   v sr|j	                  |d   d           |r!| j                  ddj                  |             |
r| j                  d|
       |rt| j                  dt        |             |rW|d   }t        |d      r| j                  d|j                         n)t        |t              rd|v r| j                  d|d          |r|j                         D ]  \  }}t        |t        t        t        t        f      r| j                  d| |       <|dk(  sB|Et        |t              r| j                  d|       ht        |t              sy| j                  dd       |j                         D ];  \  }}t        |t        t        t        t        f      s'| j                  d| |       =  |j                         D ]8  \  }}t        |t        t        t        t        f      s'| j                  ||       : y)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+   ra   r>   Nrb   rQ   rc   rd   re   Trf   functionrg   rh   ri   zfunction_calls.countr   zfunction_calls.first_namezsession.turn_detectionzsession.turn_detection.enabledzsession.turn_detection.)r1   rn   r5   ro   rp   rq   rf   rs   r4   r6   r7   r8   r9   )r   r   r   r$   rx   r>   r]   ry   rz   rI   r\   r_   r:   rt   ru   callr;   r<   td_keytd_values                       r   #add_openai_realtime_span_attributesr     s   B 	1-u5.?*N; <44h?7$45"2OD=#e*5,d3 
 	<D$%&D.!!$v,/v&!!$)),D$'J$,>6TR\M]C]!!$z"26":;	< }chhz.BC.0@A13~3FG!!$DtV$""#>		JD$'FdN""#>VM ,224 
	]JC%#sE4!89""XcU#3U;((U->eT*&&'GOt,&&'GN,1KKM ](%hc5$0GH ..1H/QS[\]
	] lln +
Uec3t45sE*+r   )NNNttsN)sttNNNNNFN)
TNNNNNNNNN)
NNNNNNNNNN)NNNNNNNN)__doc__typingr   r   r   r   r   opentelemetry.tracer   pipecat.services.settingsr	   pipecat.utils.tracing.setupr
   r6   r   r7   r8   r=   r9   rE   rZ   rw   r    r   r   <module>r      s<   < ; (9 <($s $s $X ,0%) 4+
4+4+ 4+ 	4+
 3-4+ ()4+ c]4+ 4+ 5/4+ 
4+v   $#"!,0 =+
=+=+ =+ 	=+
 =+ tn=+ sm=+ c]=+ ()=+ =+ 5/=+ 
=+H "  $!% +/15 Z+
Z+Z+ Z+ 	Z+
 smZ+ SMZ+ C=Z+ Z+ #Z+ SMZ+ c3h(Z+ tCH~.Z+ 5/Z+ 
Z+D #" $,0"&&* $#!%%)f+
f+f+ f+ 	f+
 smf+ smf+ f+ ()f+ DJf+ smf+ f+ tnf+ #f+ c]f+  
!f+\ 48 $#&*+/"&&*%)`+
`+`+ `+ 	`+
 !c3h0`+ `+ tn`+ sm`+ T$Z(`+ DJ`+ sm`+ c]`+ 
`+r   