Ë
    ³Œj‘  ã                  ó†   — d Z ddlmZ ddlZddlmZ ddlmZ ddlmZm	Z	 ddl
mZmZ ddlmZ dd	„Ze G d
„ d«      «       Zy)u‹  ``TranslatedSpan`` â€” the mutable draft handlers rewrite before export.

Split out of :mod:`langsmith._internal.voice.base_span_processor` so the base
processor stays focused on span lifecycle and export wiring. The framework
subclasses (Pipecat, LiveKit) rewrite one of these per span while translating
``lk.*`` / ``pipecat.*`` data into LangSmith's ``gen_ai.*`` / ``langsmith.*``
namespaces.
é    )ÚannotationsN)Úcopy)Ú	dataclass)ÚAnyÚOptional)ÚEventÚReadableSpan)Úrebuild_readable_spanc           	     óž   — | si S | j                  «       D ��ci c](  \  }}t        |t        t        f«      sŒ|t        |«      “Œ* c}}S c c}}w )zFKeep only the int-valued detail keys, dropping ``None`` / non-numeric.)ÚitemsÚ
isinstanceÚintÚfloat)ÚdetailsÚkÚvs      ús/var/www/html/Fitness-lenito-AI-main/venv/lib/python3.12/site-packages/langsmith/_internal/voice/translated_span.pyÚ_clean_token_detailsr      sA   € áØˆ	Ø")§-¡-¤/ÔQ¡/™$˜!˜Q´ZÀÄCÌÀ<Õ5PˆAŒs�1‹v‰I /ÒQÐQùÓQs
   ˜A	·A	c                  óè   — e Zd ZU dZded<   ded<   ded<   edd„«       Zdd	„Zdd
„Zdd„Z	dd„Z
dd„Zd d„Zd!d„Zd"d„Zd#d„Zd$d„Zddddddœ	 	 	 	 	 	 	 	 	 	 	 d%d„Zdddœ	 	 	 	 	 d&d„Zd'd„Zd(d„Zy))ÚTranslatedSpanuÑ  A span being translated into LangSmith's namespaces before export.

    Wraps a shallow copy of the original read-only OTel ``ReadableSpan`` with
    mutable ``attributes`` and ``events`` seeded from it. Handlers rewrite the
    draft while translating; :meth:`finalize` builds a fresh ``ReadableSpan``
    from it â€” so the original span is never mutated.

    Created per span in :meth:`BaseLangSmithSpanProcessor.on_end` and threaded
    through dispatch â€” no global state. A processor that defers a span (see
    :meth:`BaseLangSmithSpanProcessor._dispatch`) simply holds the
    ``TranslatedSpan`` itself until it exports it later, so the in-progress
    translation outlives the originating ``on_end`` call.
    r	   Úspanzdict[str, Any]Ú
attributeszlist[Event]Úeventsc                ó„   —  | t        |«      t        |j                  xs i «      t        |j                  xs g «      «      S )z>Seed a draft from a span's own fields, attributes, and events.)r   Údictr   Úlistr   )Úclsr   s     r   ÚofzTranslatedSpan.of1   s:   € ñ Ü�‹JÜ�—‘Ò& BÓ'Ü�—‘Ò" Ó#ó
ð 	
ó    c                óZ   — t        | j                  | j                  | j                  ¬«      S )zDBuild the export span from the draft and its rewritten attrs/events.)r   r   )r
   r   r   r   ©Úselfs    r   ÚfinalizezTranslatedSpan.finalize:   s"   € ä$Ø�I‰I $§/¡/¸$¿+¹+ô
ð 	
r   c                ó&   — || j                   _        y)z6Set the exported span's name (the LangSmith run name).N)r   Ú_name)r"   Únames     r   Úset_namezTranslatedSpan.set_name@   s   € àˆ�	‰	�r   c                ó&   — || j                   _        y)uÒ   Set the exported span's end time (epoch ns).

        Lets a span merged from two sources report a duration that runs past its
        own end â€” e.g. a tool span spanning from the call to the result.
        N)r   Ú	_end_time)r"   Úend_time_nss     r   Úset_end_timezTranslatedSpan.set_end_timeD   s   € ð *ˆ�	‰	Õr   c                ó"   — || j                   d<   y)uC   Set ``langsmith.span.kind`` (``llm`` / ``chain`` / ``tool`` / â€¦).zlangsmith.span.kindN©r   )r"   Úkinds     r   Úset_kindzTranslatedSpan.set_kindL   s   € à15ˆ�‰Ð-Ò.r   c                ó"   — || j                   d<   y)zBSet ``langsmith.metadata.thread_id`` (the conversation/thread id).zlangsmith.metadata.thread_idNr-   )r"   Ú	thread_ids     r   Úset_thread_idzTranslatedSpan.set_thread_idP   s   € à:Cˆ�‰Ð6Ò7r   c                óF   — |r|| j                   d<   || j                   d<   yy)uS  Set ``provider`` on both gen_ai.* provider keys (no-op if empty).

        LangSmith maps either ``gen_ai.provider.name`` (newer) or the legacy
        ``gen_ai.system`` to ``ls_provider``; writing both is version-agnostic.
        The caller passes an already-resolved slug â€” normalization is the
        integration's concern.
        zgen_ai.provider.namezgen_ai.systemNr-   )r"   Úproviders     r   Úset_providerzTranslatedSpan.set_providerT   s)   € ñ Ø6>ˆD�O‰OÐ2Ñ3Ø/7ˆD�O‰O˜OÒ,ð r   c                ój   — |r1t        |«      | j                  d<   t        |«      | j                  d<   yy)zJSet the request model on ``gen_ai.request.model`` and its metadata mirror.zgen_ai.request.modelzlangsmith.metadata.model_nameN)Ústrr   )r"   Úmodels     r   Ú	set_modelzTranslatedSpan.set_model`   s0   € áÜ69¸%³jˆD�O‰OÐ2Ñ3Ü?BÀ5»zˆD�O‰OÐ;Ò<ð r   c                ó"   — d| j                   d<   y)u@  Drop this span from the conversation Messages view (still in the tree).

        That view reconstructs the chat from ``llm``/``tool`` runs. STT/TTS spans
        are tagged ``llm``-kind for the tree but would otherwise add fake turns
        (raw transcripts, "Generated audio for: â€¦"), so they opt out here.
        Tz*langsmith.metadata.ls_message_view_excludeNr-   r!   s    r   Úexclude_from_message_viewz(TranslatedSpan.exclude_from_message_viewf   s   € ð IMˆ�‰ÐDÒEr   c                ó"   — || j                   d<   y)z:Mark the span as the trace root (``langsmith.root_span``).zlangsmith.root_spanNr-   )r"   Úis_roots     r   Úset_root_spanzTranslatedSpan.set_root_spano   s   € à18ˆ�‰Ð-Ò.r   c                ó(   — || j                   d|› �<   y)u[  Set ``langsmith.metadata.<key>`` to the given value.

        LangSmith surfaces everything under ``langsmith.metadata.*`` as run
        metadata. Note ``langsmith.root_span`` and ``langsmith.span.kind`` are NOT
        metadata â€” they live in the top-level ``langsmith.*`` namespace and have
        their own setters / direct writes.
        zlangsmith.metadata.Nr-   )r"   ÚkeyÚvalues      r   Úset_metadatazTranslatedSpan.set_metadatas   s   € ð 8=ˆ�‰Ð-¨c¨UÐ3Ò4r   N)Úinput_tokensÚoutput_tokensÚtotal_tokensÚinput_token_detailsÚoutput_token_detailsc               óú   — i }|�t        |«      |d<   |�t        |«      |d<   |�t        |«      |d<   t        |«      x}r||d<   t        |«      x}r||d<   |r#t        j                  |«      | j                  d<   yy)uÑ   Write the given token usage as ``langsmith.usage_metadata`` (JSON).

        Callers pass the complete usage â€” it replaces the flat ``gen_ai.usage.*``
        at ingest. No-op when nothing is given.
        NrC   rD   rE   rF   rG   zlangsmith.usage_metadata)r   r   ÚjsonÚdumpsr   )r"   rC   rD   rE   rF   rG   Úusager   s           r   Ú	set_usagezTranslatedSpan.set_usage}   s    € ð !#ˆØÐ#Ü$'¨Ó$5ˆE�.Ñ!ØÐ$Ü%(¨Ó%7ˆE�/Ñ"ØÐ#Ü$'¨Ó$5ˆE�.Ñ!Ü*Ð+>Ó?Ð?ˆ7Ð?Ø+2ˆEÐ'Ñ(Ü*Ð+?Ó@Ð@ˆ7Ð@Ø,3ˆEÐ(Ñ)ÙÜ:>¿*¹*ÀUÓ:KˆD�O‰OÐ6Ò7ð r   )ÚpromptÚ
completionc               óž   — |�$t        j                  d|i«      | j                  d<   |�%t        j                  d|i«      | j                  d<   yy)zIWrite ``gen_ai.prompt``/``gen_ai.completion`` as ``{"messages": [...]}``.NÚmessagesúgen_ai.promptúgen_ai.completion)rI   rJ   r   )r"   rM   rN   s      r   Úset_messageszTranslatedSpan.set_messages™   sN   € ð ÐÜ/3¯z©z¸:ÀvÐ:NÓ/OˆD�O‰O˜OÑ,ØÐ!Ü37·:±:¸zÈ:Ð>VÓ3WˆD�O‰OÐ/Ò0ð "r   c                ól   — t        |t        «      r|nt        j                  |«      | j                  d<   y)z¢Write a tool run's input to ``gen_ai.prompt`` as raw I/O (not messages).

        A ``str`` is passed through unchanged; any other value is JSON-encoded.
        rQ   N©r   r7   rI   rJ   r   )r"   Ú
tool_inputs     r   Úset_tool_inputzTranslatedSpan.set_tool_input¥   s)   € ô % Z´Ô5‰J¼4¿:¹:ÀjÓ;Qð 	�‰˜Ò(r   c                ól   — t        |t        «      r|nt        j                  |«      | j                  d<   y)z§Write a tool run's output to ``gen_ai.completion`` as raw I/O (not messages).

        A ``str`` is passed through unchanged; any other value is JSON-encoded.
        rR   NrU   )r"   Útool_outputs     r   Úset_tool_outputzTranslatedSpan.set_tool_output®   s*   € ô & k´3Ô7‰K¼T¿Z¹ZÈÓ=Tð 	�‰Ð+Ò,r   )r   r	   Úreturnr   )r[   r	   )r&   r7   r[   ÚNone)r*   r   r[   r\   )r.   r7   r[   r\   )r1   r7   r[   r\   )r4   úOptional[str]r[   r\   )r8   r]   r[   r\   )r[   r\   )r=   Úboolr[   r\   )r@   r7   rA   r   r[   r\   )rC   úOptional[int]rD   r_   rE   r_   rF   úOptional[dict[str, int]]rG   r`   r[   r\   )rM   úOptional[list[dict]]rN   ra   r[   r\   )rV   r   r[   r\   )rY   r   r[   r\   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__Úclassmethodr   r#   r'   r+   r/   r2   r5   r9   r;   r>   rB   rL   rS   rW   rZ   © r   r   r   r      sô   … ñð ÓØÓØÓàò
ó ð
ó
óó*ó6óDó
8óJóMó9ó=ð '+Ø'+Ø&*Ø8<Ø9=ñLð $ðLð %ð	Lð
 $ðLð 6ðLð 7ðLð 
óLð> (,Ø+/ñ	
Xð %ð
Xð )ð	
Xð
 
ó
Xó
ô
r   r   )r   zOptional[dict[str, Any]]r[   zdict[str, int])re   Ú
__future__r   rI   r   Údataclassesr   Útypingr   r   Úopentelemetry.sdk.tracer   r	   Ú$langsmith._internal.otel._span_utilsr
   r   r   rh   r   r   Ú<module>rn      sC   ðñõ #ã Ý Ý !ß  ç 7å FóRð ÷W
ð W
ó ñW
r   