o
    i_                     @   s   d dl mZ d dl mZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dlm
Z
 d d	lmZ d d
lmZ d dlmZ d dlmZ eddG dd dZdS )    )	dataclass)replace)Any)Literal)Optional)Union)extract_template)render_chat)safe_substitute)Message)Prompt)PromptFallbackT)frozenc                   @   s  e Zd ZU dZeed< eed< ee ed< ed ed< eee	e
 f ed< dZee ed	< dZee ed
< dedeee	e
 f fddZdedefddZdefddZdeeef fddZedeeef dd fddZded dd fddZe	ddededd fddZdS )ManagedPrompta  
    An immutable prompt template retrieved from the Datadog Prompt Registry.

    STABLE API:
        - format(**vars) -> str | list[dict]
        - to_annotation_dict(**vars) -> dict[str, Any]

    INTERNAL (may change):
        - All fields (id, version, label, source, template)
    idversionlabel)registrycachefallbacksourcetemplateN_uuid_version_uuid	variablesreturnc                 K   s$   t | jtrt| j|S t| j|S )a  
        Render the template with variables.

        For text templates, returns the rendered string.
        For chat templates, returns the rendered message list.

        Uses safe substitution: missing variables are left as placeholders.

        Args:
            **variables: Template variables to substitute

        Returns:
            str (for text templates) or list[Message] (for chat templates)
        )
isinstancer   strr
   r	   )selfr    r   R/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/llmobs/_prompts/prompt.pyformat%   s   zManagedPrompt.formatc                 K   sr   | j | jd}|r||d< | jr| j|d< | jr| j|d< | jr%| j|d< t| jtr2| j|d< |S | j|d< |S )aL  
        Convert to the format expected by annotation_context.

        Args:
            **variables: Variable names and values used in the prompt template.
                         Pass the same kwargs here that you use with format().

        Example:
            prompt.to_annotation_dict(name="Alice", topic="weather")
        )r   r   r   r   prompt_uuidprompt_version_uuidr   chat_template)r   r   r   r   r   r   r   r   )r   r   resultr   r   r    to_annotation_dict8   s    




z ManagedPrompt.to_annotation_dictc              	   C   s&   d| j d| jd| jd| jd	S )NzManagedPrompt(id=z
, version=z, label=z	, source=))r   r   r   r   r   r   r   r    __repr__W   s   &zManagedPrompt.__repr__c                 C   s"   | j | j| j| j| j| j| jdS )z6Serialize to a JSON-compatible dict for cache storage.r   r   r   r   r   r   r   r*   r(   r   r   r    
_serializeZ   s   zManagedPrompt._serializedatac              
   C   s:   | |d |d |d | dd|d | d| dd	S )
z&Deserialize from a cache storage dict.r   r   r   r   r   r   r   r   r*   )get)clsr,   r   r   r    _deserializef   s   
zManagedPrompt._deserializec                 C   s   | j |kr| S t| |dS )zCCreate a copy with a different source. Used internally for caching.)r   )r   r   )r   r   r   r   r    _with_sources   s   
zManagedPrompt._with_source	prompt_idr   c                 C   sZ   d}d}|dur$t |r| n|}t|tr"t|}|dp d}n|}| ||dd|dS )a'  Create a ManagedPrompt from a fallback value.

        Args:
            prompt_id: The prompt identifier.
            fallback: A string, message list, Prompt dict, or callable returning any of those.

        Returns:
            A ManagedPrompt with source="fallback" and label=None.
         r   Nr   )r   r   r   r   r   )callabler   dictr   r-   )r.   r1   r   r   r   valuer   r   r    from_fallbacky   s   
zManagedPrompt.from_fallback)N)__name__
__module____qualname____doc__r   __annotations__r   r   r   listr   r   r   r!   r   r   r&   r)   r4   r+   classmethodr/   r0   r   r6   r   r   r   r    r      s2   
 r   N)dataclassesr   r   typingr   r   r   r   ddtrace.llmobs._prompts.utilsr   r	   r
   ddtrace.llmobs.typesr   r   r   r   r   r   r   r    <module>   s    