
    %Gg\"                         d dl Z d dlZd dlZd dlmZmZ d dlmZ 	 d dl	m
Z
 d dlmc mZ dZd d	lmZ  ej(                  e      Zer G d
 de
j.                        Zyy# e$ r dZ G d d      ZY Dw xY w)    N)DictOptional)	run_trees)tracingTFc                       e Zd ZdZd Zy)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      ~/home/kushmeetdev/apache_webroot/langgraph_flaskproject/venv/lib/python3.12/site-packages/langsmith/wrappers/_openai_agents.py__init__z%OpenAIAgentsTracingProcessor.__init__F   s    Q     N)__name__
__module____qualname____doc__r    r   r   r   r      s    2	h	r   r   )clientc                      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y)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_namenamer   r   r   r   r   c                    |xs t        j                         | _        || _        || _        || _        || _        i | _        y N)rtget_cached_clientr   	_metadata_tags_project_name_name_runs)r   r   r   r   r   r   s         r   r   z%OpenAIAgentsTracingProcessor.__init__   s=     !:B$8$8$:DK%DNDJ!-DDJ)+DJr   tracereturnc           
         | j                   r| j                   }n|j                  r|j                  }nd}t        t        j                               }|| j
                  |j                  <   | j                  rd| j                  ini }	 t        |i d|d || j                  | j                        } | j                  j                  di | y # t        $ r"}t        j                  d|        Y d }~y d }~ww xY w)NzAgent workflowr   chain)r   inputsrun_typeidrevision_idextrar   r   zError creating trace run: r   )r#   r   struuiduuid4r$   trace_idr    dictr!   r"   r   
create_run	Exceptionlogger	exception)r   r%   run_nametrace_run_id	run_extrarun_dataes          r   on_trace_startz+OpenAIAgentsTracingProcessor.on_trace_start   s    zz:: ::+tzz|,L)5DJJu~~&8<T^^4BIC!%!$# $#!%!3!3	" '&&22 C  #=aS!ABBCs   AC 	C7C22C7c                 h   | j                   j                  |j                  d       }|j                         xs i }i |j	                  d      xs i | j
                  xs i }|r!	 | j                  j                  |d|i       y y # t        $ r"}t        j                  d|        Y d }~y d }~ww xY w)Nr   )run_idr-   zError updating trace run: )r$   popr1   exportgetr    r   
update_runr4   r5   r6   )r   r%   r>   
trace_dictr   r;   s         r   on_trace_endz)OpenAIAgentsTracingProcessor.on_trace_end   s    ZZ^^ENND9F-2JW:>>*5;WAUSUWHGKK**&X@V*W  ! G$$'A!%EFFGs   %B 	B1B,,B1spanc           
         | j                   j                  |j                  xs |j                        }t	        t        j                               }|| j                   |j                  <   t        j                  |      }t        j                  |      }t        j                  |      }	 t        |||||j                  di             }|j                  r,t        j                  j                  |j                        |d<    | j                   j"                  di | y # t$        $ r"}t&        j)                  d|        Y d }~y d }~ww xY w)Nr)   )r   r*   r+   parent_run_idr)   
start_timezError creating span run: r   )r$   rA   	parent_idr1   r.   r/   r0   span_idagent_utilsget_run_nameget_run_typeextract_span_datar2   
started_atdatetimefromisoformatr   r3   r4   r5   r6   )	r   rE   rG   span_run_idr7   r*   	extractedr:   r;   s	            r   on_span_startz*OpenAIAgentsTracingProcessor.on_span_start   s    JJNN4>>+JT]]KMdjjl+K'2DJJt||$"//5H"//5H#55d;IB!%!%""/$==26" ??-5->->-L-L.H\* '&&22 B  #<QC!@AABs   *A4D 	E
(EE
c           	      R   | j                   j                  |j                  d       }|rt        j                  |      }|j                  di       }|j                  |d<   |j                  |d<   |j                  |d<   ||d<   t        ||j                  rt        |j                        nd |j                  di       |j                  di       |      }|j                  r,t        j                  j                  |j                        |d<    | j                  j                  d	i | y y )
Nr   openai_parent_idopenai_trace_idopenai_span_idoutputsr)   )r>   errorrY   r)   r-   end_timer   )r$   r?   rJ   rK   rN   rA   rI   r1   r2   rZ   r.   ended_atrP   rQ   r   rB   )r   rE   r>   rS   r   r:   s         r   on_span_endz(OpenAIAgentsTracingProcessor.on_span_end   s    ZZ^^DLL$7F'99$?	$==R8/3~~+,.2mm*+-1\\)*(0	*%!%!-1ZZ#djj/T%MM)R8$==26#" ==+3+<+<+J+J,HZ( '&&22% r   c                 8    | j                   j                          y r   r   flushr   s    r   shutdownz%OpenAIAgentsTracingProcessor.shutdown       KKr   c                 8    | j                   j                          y r   r_   ra   s    r   force_flushz(OpenAIAgentsTracingProcessor.force_flush   rc   r   r   )r&   N)r   r   r   r   r   	ls_clientClientr2   listr.   r   r   Tracer<   rD   SpanrT   r]   rb   re   r   r   r   r   r   S   s    6	t 26	, (,(,*."&	,Y--.	, tn		,
 49%	, #3-	, 3-	,"	C 	C$ 	C4	Ggmm 	G 	G	Bgll 	Bt 	B2	3GLL 	3T 	3,	 	 r   )rP   loggingr/   typingr   r   	langsmithr   r   agentsr   langsmith.wrappers._agent_utilswrappers_agent_utilsrK   HAVE_AGENTSr
   r   r   rf   	getLoggerr   r5   TracingProcessorr   r   r   <module>ru      s}       ! %B99K@ *			8	$a w'?'? a  G  <K9 9<s   A A21A2