
    	h1                     f   d dl Z d dlmZmZ d dlmZmZ d dlmZ d dlm	Z
 d dlmZ 	 d dlmZ dZ ed	 eD                       s ed
          d dlmc mZ dZn# e$ r dZ G d d          ZY nw xY wd dlmZ  e j        e          Zer# G d de          Z G d dej                  ZdS dS )    N)datetimetimezone)Optional	TypedDict)uuid4)	run_trees)get_current_run_tree)tracing)TracingProcessorTraceSpanResponseSpanDatac              #   @   K   | ]}t          t          |          V  d S N)hasattrr
   ).0names     n/var/www/html/web-builder-api.evdpl.com/venv/lib/python3.11/site-packages/langsmith/wrappers/_openai_agents.py	<genexpr>r      s,      ;;$ww%%;;;;;;    z&The `agents` package is not installed.TFc                       e Zd ZdZd ZdS )OpenAIAgentsTracingProcessora  Tracing processor for the `OpenAI Agents SDK <https://openai.github.io/openai-agents-python/>`_.

        Traces all intermediate steps of your OpenAI Agent to LangSmith.

        Requirements: Make sure to install ``pip install -U langsmith[openai-agents]``.

        Args:
            client: An instance of langsmith.client.Client. If not provided,
                a default client is created.

        Example:
            .. code-block:: python

                from agents import (
                    Agent,
                    FileSearchTool,
                    Runner,
                    WebSearchTool,
                    function_tool,
                    set_trace_processors,
                )

                from langsmith.wrappers import OpenAIAgentsTracingProcessor

                set_trace_processors([OpenAIAgentsTracingProcessor()])


                @function_tool
                def get_weather(city: str) -> str:
                    return f"The weather in {city} is sunny"


                haiku_agent = Agent(
                    name="Haiku agent",
                    instructions="Always respond in haiku form",
                    model="o3-mini",
                    tools=[get_weather],
                )
                agent = Agent(
                    name="Assistant",
                    tools=[WebSearchTool()],
                    instructions="speak in spanish. use Haiku agent if they ask for a haiku or for the weather",
                    handoffs=[haiku_agent],
                )

                result = await Runner.run(
                    agent,
                    "write a haiku about the weather today and tell me a recent news story about new york",
                )
                print(result.final_output)
        c                      t          d          )NzeThe `agents` package is not installed. Please install it with `pip install langsmith[openai-agents]`.)ImportError)selfargskwargss      r   __init__z%OpenAIAgentsTracingProcessor.__init__P   s    Q  r   N)__name__
__module____qualname____doc__r    r   r   r   r      s.        2	 2	h	 	 	 	 	r   r   )clientc                   N    e Zd ZU eed<   eed<   eed<   eed<   ee         ed<   dS )RunDataidtrace_id
start_timedotted_orderparent_run_idN)r   r    r!   str__annotations__r   r   r#   r   r   r&   r&   ]   sL         }$$$$$r   r&   c                      e Zd ZdZ	 dddddddeej                 dee         deee	                  dee	         dee	         f
d	Z
d
ej        ddfdZd
ej        ddfdZdej        ddfdZdej        ddfdZddZddZdS )r   a  Tracing processor for the `OpenAI Agents SDK <https://openai.github.io/openai-agents-python/>`_.

        Traces all intermediate steps of your OpenAI Agent to LangSmith.

        Requirements: Make sure to install ``pip install -U langsmith[openai-agents]``.

        Args:
            client: An instance of langsmith.client.Client. If not provided,
                a default client is created.
            metadata: Metadata to associate with all traces.
            tags: Tags to associate with all traces.
            project_name: LangSmith project to trace to.
            name: Name of the root trace.

        Example:
            .. code-block:: python

                from agents import (
                    Agent,
                    FileSearchTool,
                    Runner,
                    WebSearchTool,
                    function_tool,
                    set_trace_processors,
                )

                from langsmith.wrappers import OpenAIAgentsTracingProcessor

                set_trace_processors([OpenAIAgentsTracingProcessor()])


                @function_tool
                def get_weather(city: str) -> str:
                    return f"The weather in {city} is sunny"


                haiku_agent = Agent(
                    name="Haiku agent",
                    instructions="Always respond in haiku form",
                    model="o3-mini",
                    tools=[get_weather],
                )
                agent = Agent(
                    name="Assistant",
                    tools=[WebSearchTool()],
                    instructions="speak in spanish. use Haiku agent if they ask for a haiku or for the weather",
                    handoffs=[haiku_agent],
                )

                result = await Runner.run(
                    agent,
                    "write a haiku about the weather today and tell me a recent news story about new york",
                )
                print(result.final_output)
        N)metadatatagsproject_namer   r$   r/   r0   r1   r   c                    |pt          j                    | _        || _        || _        || _        || _        i | _        i | _        i | _	        d S r   )
rtget_cached_clientr$   	_metadata_tags_project_name_name_first_response_inputs_last_response_outputs_runs)r   r$   r/   r0   r1   r   s         r   r   z%OpenAIAgentsTracingProcessor.__init__   sQ     !:B$8$:$:DK%DNDJ!-DDJ02D'02D'-/DJJJr   tracereturnc                    t                      }| j        r| j        }n|j        r|j        }nd}t          t	                                }t          j        t          j                  }|0t          |j	                  }t          |j
                  }|j        }n|}d }d }t          j        |||          }	t          ||||	|          | j        |j	        <   d| j        pi i}
|                                pi }|                    d          |d         |
d         d<   	 t'          |i d||||	|d |
| j        | j                  } | j        j        d
i | d S # t0          $ r(}t2                              d	|            Y d }~d S d }~ww xY w)NzAgent workflowr)   run_idparent_dotted_orderr'   r(   r)   r*   r+   r/   group_id	thread_idchain)r   inputsrun_typer'   r(   r+   r*   r)   revision_idextrar0   r1   zError creating trace run: r#   )r	   r8   r   r,   r   r   nowr   utcr(   r'   r*   agent_utilsensure_dotted_orderr&   r;   r5   exportgetdictr6   r7   r$   
create_run	Exceptionlogger	exception)r   r<   current_run_treerun_nametrace_run_idr)   r(   r+   rA   r*   	run_extra
trace_dictrun_dataes                 r   on_trace_startz+OpenAIAgentsTracingProcessor.on_trace_start   s   355z ,: , :+uww<<L!hl33J  +/899 #$4$7 8 8&6&C##' $&*#&:%#$7  L
 *1!%)+* * *DJu~& $T^%9r:I-2J~~j))55?
5K	*%k2C!%!$#%"/!-) $#!%!3" " " '&2222222 C C C  !Aa!A!ABBBBBBBBBCs    7E 
F#FFc                 $   | j                             |j        d           }|                                pi }i |                    d          pi | j        pi }|r	 | j                            |d         |d         |d         |d         | j                            |j        i           | j	                            |j        i           d|i| j
                   d S # t          $ r(}t                              d|            Y d }~d S d }~ww xY wd S )Nr/   r'   r(   r+   r*   )r@   r(   r+   r*   rF   outputsrI   r1   zError updating trace run: )r;   popr(   rN   rO   r5   r$   
update_runr9   r:   r7   rR   rS   rT   )r   r<   runrY   r/   r[   s         r   on_trace_endz)OpenAIAgentsTracingProcessor.on_trace_end   sH   *..66C-2JW:>>*55;WAUSUWH GGK**"4y!$Z&)/&:%(%8#:>>u~rRR $ ; ? ?PR S S)84%)%7 + 	 	 	 	 	 ! G G G$$%E!%E%EFFFFFFFFFGG Gs   A=C 
D%DDspanc                    |j         r| j                            |j                   n| j                            |j                  }|$t                              d|j                    d S |d         }t          t                                }|j	        rt          j        |j	                  nt          j        t          j                  }t          j        |||r|d         nd           }t#          |||||d                   | j        |j        <   t          j        |          }t          j        |          }t          j        |          }		 t+          |||||d         ||	                    di           | j                  }
|j	        rt          j        |j	                  |
d	<    | j        j        di |
 d S # t2          $ r(}t                              d
|            Y d }~d S d }~ww xY w)Nz(No trace info found for span, skipping: r(   r*   r?   r'   rB   rF   )r   rG   r'   r(   r+   r*   rF   r1   r)   zError creating span run: r#   )	parent_idr;   rO   r(   rS   warningspan_idr,   r   
started_atr   fromisoformatrJ   r   rK   rL   rM   r&   get_run_nameget_run_typeextract_span_datarP   r7   r$   rQ   rR   rT   )r   rc   
parent_runr(   span_run_idspan_start_timer*   rV   rG   	extractedrZ   r[   s               r   on_span_startz*OpenAIAgentsTracingProcessor.on_span_start  s.    >3
t~...Z^^DM22  !Mt|MM   !*-Hegg,,K ?0&t777\(,// 
 ':*"BL$VJ~$>$>RV  L
 (/!*)(.( ( (DJt|$ #/55H"/55H#5d;;IB!%!%"%",T"2!-$==266!%!3	" 	" 	" ? U-5-CDO-T-TH\*&&2222222 B B B  !@Q!@!@AAAAAAAAABs   A+F? ?
G1	G,,G1c                    | j                             |j        d           }|r\t          j        |          }|                    di           }|j        |d<   |j        |d<   |j        |d<   ||d<   |                    di           }|                    di           }t          |d         |d         |d	         |d
         |j	        rt          |j	                  nd |||| j        	  	        }|j        rt          j        |j                  |d<   t          |j        t"          j                  r=| j                            |j                  p|| j        |j        <   || j        |j        <    | j        j        di | d S d S )Nr/   openai_parent_idopenai_trace_idopenai_span_idr^   rF   r'   r(   r*   r+   )	r@   r(   r*   r+   errorr^   rF   rI   r1   end_timer#   )r;   r_   rg   rL   rl   rO   re   r(   rP   rv   r,   r7   ended_atr   ri   
isinstance	span_datar
   r   r9   r:   r$   r`   )r   rc   ra   rp   r/   r^   rF   rZ   s           r   on_span_endz(OpenAIAgentsTracingProcessor.on_span_end9  s   *..t44C 3'9$??	$==R88/3~+,.2m*+-1\)*(0	*%#--	266"x44!%t9 _!$^!4"%o"6-1ZA#dj///T#!#!%!3
" 
" 
" = Q+3+A$-+P+PHZ(dng.FGG I377FFP& /> BID/>&&2222222;3 3r   c                 8    | j                                          d S r   r$   flushr   s    r   shutdownz%OpenAIAgentsTracingProcessor.shutdownZ      Kr   c                 8    | j                                          d S r   r}   r   s    r   force_flushz(OpenAIAgentsTracingProcessor.force_flush]  r   r   r   )r=   N)r   r    r!   r"   r   	ls_clientClientrP   listr,   r   r
   r   r\   rb   r   rq   r{   r   r   r#   r   r   r   r   d   so       6	 6	t 26	0 (,(,*."&	0 	0 	0Y-.	0 tn		0
 49%	0 #3-	0 3-	0 	0 	0 	0&<	C <	C$ <	C <	C <	C <	C|	Ggm 	G 	G 	G 	G 	G(5	Bgl 5	Bt 5	B 5	B 5	B 5	Bn	3GL 	3T 	3 	3 	3 	3B	  	  	  	 	  	  	  	  	  	 r   )loggingr   r   typingr   r   uuidr   	langsmithr   r3   langsmith.run_helpersr	   agentsr
   requiredallr   langsmith.wrappers._agent_utilswrappers_agent_utilsrL   HAVE_AGENTSr   r$   r   	getLoggerr   rS   r&   r   r#   r   r   <module>r      s    ' ' ' ' ' ' ' ' & & & & & & & &       % % % % % % 6 6 6 6 6 6KH 3;;(;;;;; DkBCCC999999999KK < < <K9 9 9 9 9 9 9 9 9 9<~ * ) ) ) ) )		8	$	$ C % % % % %) % % %z  z  z  z  z w'? z  z  z  z  z C  C s   3A A32A3