o
    h                     @  s   d dl mZ d dlZ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 G d
d dejZG dd deZe ZG dd deZdS )    )annotationsN)Any   )logger   )util)TracingProcessor)Scopec                   @  s   e Zd ZdZejdddZejdd Zejddd
dZejddddZ	e
ejdddZe
ejdddZejdddZdS )Tracea:  A complete end-to-end workflow containing related spans and metadata.

    A trace represents a logical workflow or operation (e.g., "Customer Service Query"
    or "Code Generation") and contains all the spans (individual operations) that occur
    during that workflow.

    Example:
        ```python
        # Basic trace usage
        with trace("Order Processing") as t:
            validation_result = await Runner.run(validator, order_data)
            if validation_result.approved:
                await Runner.run(processor, order_data)

        # Trace with metadata and grouping
        with trace(
            "Customer Service",
            group_id="chat_123",
            metadata={"customer": "user_456"}
        ) as t:
            result = await Runner.run(support_agent, query)
        ```

    Notes:
        - Use descriptive workflow names
        - Group related traces with consistent group_ids
        - Add relevant metadata for filtering/analysis
        - Use context managers for reliable cleanup
        - Consider privacy when adding trace data
    returnc                 C     d S N selfr   r   V/var/www/html/openai_agents/venv/lib/python3.10/site-packages/agents/tracing/traces.py	__enter__-      zTrace.__enter__c                 C  r   r   r   r   exc_typeexc_valexc_tbr   r   r   __exit__1   r   zTrace.__exit__Fmark_as_currentboolc                 C     dS )a  Start the trace and optionally mark it as the current trace.

        Args:
            mark_as_current: If true, marks this trace as the current trace
                in the execution context.

        Notes:
            - Must be called before any spans can be added
            - Only one trace can be current at a time
            - Thread-safe when using mark_as_current
        Nr   r   r   r   r   r   start5      zTrace.startreset_currentc                 C  r   )aj  Finish the trace and optionally reset the current trace.

        Args:
            reset_current: If true, resets the current trace to the previous
                trace in the execution context.

        Notes:
            - Must be called to complete the trace
            - Finalizes all open spans
            - Thread-safe when using reset_current
        Nr   r   r   r   r   r   finishD   r   zTrace.finishstrc                 C  r   )a=  Get the unique identifier for this trace.

        Returns:
            str: The trace's unique ID in the format 'trace_<32_alphanumeric>'

        Notes:
            - IDs are globally unique
            - Used to link spans to their parent trace
            - Can be used to look up traces in the dashboard
        Nr   r   r   r   r   trace_idS   r   zTrace.trace_idc                 C  r   )aW  Get the human-readable name of this workflow trace.

        Returns:
            str: The workflow name (e.g., "Customer Service", "Data Processing")

        Notes:
            - Should be descriptive and meaningful
            - Used for grouping and filtering in the dashboard
            - Helps identify the purpose of the trace
        Nr   r   r   r   r   nameb   r   z
Trace.namedict[str, Any] | Nonec                 C  r   )aK  Export the trace data as a serializable dictionary.

        Returns:
            dict | None: Dictionary containing trace data, or None if tracing is disabled.

        Notes:
            - Includes all spans and their data
            - Used for sending traces to backends
            - May include metadata and group ID
        Nr   r   r   r   r   exportq   s   zTrace.exportNr   r
   Fr   r   r   r   r   r"   r   r%   )__name__
__module____qualname____doc__abcabstractmethodr   r   r   r!   propertyr#   r$   r&   r   r   r   r   r
      s$    
r
   c                   @  sh   e Zd ZdZdd ZdddZdd	 ZddddZddddZe	dddZ
e	dddZd ddZdS )!	NoOpTracea  A no-op implementation of Trace that doesn't record any data.

    Used when tracing is disabled but trace operations still need to work.
    Maintains proper context management but doesn't store or export any data.

    Example:
        ```python
        # When tracing is disabled, traces become NoOpTrace
        with trace("Disabled Workflow") as t:
            # Operations still work but nothing is recorded
            await Runner.run(agent, "query")
        ```
    c                 C  s   d| _ d | _d S NF)_started_prev_context_tokenr   r   r   r   __init__   s   
zNoOpTrace.__init__r   r
   c                 C  s0   | j r| jstd | S d| _ | jdd | S Nz.Trace already started but no context token setT)r   r6   r7   r   errorr   r   r   r   r   r      s   
zNoOpTrace.__enter__c                 C  s   | j dd d S )NTr   )r!   r   r   r   r   r      s   zNoOpTrace.__exit__Fr   r   c                 C  s   |r
t | | _d S d S r   )r	   set_current_tracer7   r   r   r   r   r      s   zNoOpTrace.startr   c                 C  s,   |r| j d urt| j  d | _ d S d S d S r   )r7   r	   reset_current_tracer    r   r   r   r!      s   
zNoOpTrace.finishr"   c                 C  r   )zfThe trace's unique identifier.

        Returns:
            str: A unique ID for this trace.
        no-opr   r   r   r   r   r#         zNoOpTrace.trace_idc                 C  r   )z{The workflow name for this trace.

        Returns:
            str: Human-readable name describing this workflow.
        r?   r   r   r   r   r   r$      r@   zNoOpTrace.namer%   c                 C  r   )zExport the trace data as a dictionary.

        Returns:
            dict | None: Trace data in exportable format, or None if no data.
        Nr   r   r   r   r   r&      s   zNoOpTrace.exportNr'   r(   r)   r*   r+   r,   )r-   r.   r/   r0   r8   r   r   r   r!   r3   r#   r$   r&   r   r   r   r   r4      s    
r4   c                   @  sn   e Zd ZdZdZd#ddZed$ddZed$ddZd%d&ddZ	d%d'ddZ
d(ddZdd Zd)d d!Zd"S )*	TraceImplz?
    A trace that will be recorded by the tracing library.
    )_name	_trace_idgroup_idmetadatar7   
_processorr6   r$   r"   r#   
str | NonerD   rE   r%   	processorr   c                 C  s6   || _ |pt | _|| _|| _d | _|| _d| _d S r5   )	rB   r   gen_trace_idrC   rD   rE   r7   rF   r6   )r   r$   r#   rD   rE   rH   r   r   r   r8      s   
zTraceImpl.__init__r   c                 C     | j S r   )rC   r   r   r   r   r#         zTraceImpl.trace_idc                 C  rJ   r   )rB   r   r   r   r   r$      rK   zTraceImpl.nameFr   r   c                 C  s4   | j rd S d| _ | j|  |rt| | _d S d S )NT)r6   rF   on_trace_startr	   r=   r7   r   r   r   r   r      s   zTraceImpl.startr   c                 C  sB   | j sd S | j|  |r| jd urt| j d | _d S d S d S r   )r6   rF   on_trace_endr7   r	   r>   r    r   r   r   r!      s   
zTraceImpl.finishr
   c                 C  s*   | j r| jstd | S | jdd | S r9   r:   r   r   r   r   r     s   
zTraceImpl.__enter__c                 C  s   | j |tud d S )Nr<   )r!   GeneratorExitr   r   r   r   r     s   zTraceImpl.__exit__c                 C  s   d| j | j| j| jdS )Ntrace)objectidworkflow_namerD   rE   )r#   r$   rD   rE   r   r   r   r   r&     s   zTraceImpl.exportN)
r$   r"   r#   rG   rD   rG   rE   r%   rH   r   r+   r(   r)   r*   r'   r,   )r-   r.   r/   r0   	__slots__r8   r3   r#   r$   r   r!   r   r   r&   r   r   r   r   rA      s    




	rA   )
__future__r   r1   contextvarstypingr   r    r   processor_interfacer   scoper	   ABCr
   r4   NO_OP_TRACErA   r   r   r   r   <module>   s    sE