o
    i*                     @   s   U d dl 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eee	f  Ze jddd	Ze je ed
< G dd deZdS )    N)Optional)Union)Context)DefaultContextProvider)Span)	SpanTypesdatadog_llmobs_contextvar)default_DD_LLMOBS_CONTEXTVARc                       sj   e Zd ZdZd fddZdefddZdedee fd	d
Z	de
ddf fddZde
fddZ  ZS )LLMObsContextProviderzContext provider that retrieves contexts from a context variable.
    It is suitable for synchronous programming and for asynchronous executors
    that support contextvars.
    returnNc                    s   t t|   td  d S )N)superr   __init__r
   set)self	__class__ K/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/llmobs/_context.pyr      s   zLLMObsContextProvider.__init__c                 C   s   t  }|duS )zDReturns whether there is an active context in the current execution.N)r
   getr   ctxr   r   r   _has_active_context   s   z)LLMObsContextProvider._has_active_contextspanc                 C   sL   |j s|S |j}|r|j s|jtjkr| | |S |j}|s
| d dS )zUpdates the active LLMObs span.
        The active span is updated to be the span's closest unfinished LLMObs ancestor span.
        N)finished_parent	span_typer   LLMactivate)r   r   
new_activer   r   r   _update_active#   s   

z$LLMObsContextProvider._update_activer   c                    s   t | tt| | dS )z8Makes the given context active in the current execution.N)r
   r   r   r   r   r   r   r   r   r   2   s   
zLLMObsContextProvider.activatec                 C   s    t  }t|tr| |S |S )z=Returns the active span or context for the current execution.)r
   r   
isinstancer   r    )r   itemr   r   r   active7   s   

zLLMObsContextProvider.active)r   N)__name__
__module____qualname____doc__r   boolr   r   r   r    ContextTypeValuer   r#   __classcell__r   r   r   r   r      s    r   )contextvarstypingr   r   ddtrace._trace.contextr   ddtrace._trace.providerr   ddtrace._trace.spanr   ddtrace.extr   r)   
ContextVarr
   __annotations__r   r   r   r   r   <module>   s   
 