
    >'hh-                        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c mZ dZd dlmZ  e j,                  e      Zer! G d de      Z G d d
ej4                        Zyy# e$ r dZ G d	 d
      ZY Ow xY w)    N)datetimetimezone)Optional	TypedDict)uuid4)	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      h/home/kushmeetdev/Regenta/Chatbot/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                   F    e Zd ZU eed<   eed<   eed<   eed<   ee   ed<   y)RunDataidtrace_id
start_timedotted_orderparent_run_idN)r   r   r   str__annotations__r   r   r   r   r   r   r   S   s"    }$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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 | _        i | _        i | _	        y N)
rtget_cached_clientr   	_metadata_tags_project_name_name_first_response_inputs_last_response_outputs_runs)r   r   r$   r%   r&   r'   s         r   r   z%OpenAIAgentsTracingProcessor.__init__   sM     !:B$8$8$:DK%DNDJ!-DDJ02D'02D'-/DJr   tracereturnc                    | j                   r| j                   }n|j                  r|j                  }nd}t        t                     }t	        j
                  t        j                        }t        j                  ||      }t        ||||d       | j                  |j                  <   d| j                  xs i i}|j                         xs i }|j                  d      |d   |d   d<   	 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 workflow)r   run_idr   r   r   r   r    r$   group_id	thread_idchain)r'   inputsrun_typer   r   r   r   revision_idextrar%   r&   zError creating trace run: r   )r/   r'   r!   r   r   nowr   utcagent_utilsensure_dotted_orderr   r2   r   r,   exportgetdictr-   r.   r   
create_run	Exceptionlogger	exception)
r   r3   run_nametrace_run_idr   r   	run_extra
trace_dictrun_dataes
             r   on_trace_startz+OpenAIAgentsTracingProcessor.on_trace_start   sI   zz:: ::+uw<L!hll3J&::%#L *1%%)"*DJJu~~& $T^^%9r:I-2J~~j)55?
5K	*%k2C!%!$#)!-) $#!%!3!3" '&&22 C  #=aS!ABBCs   ,AD3 3	E<EEc           
         | j                   j                  |j                  d       }|j                         xs i }i |j	                  d      xs i | j
                  xs i }|rv	 | j                  j                  |d   |d   |d   | j                  j                  |j                  i       | j                  j                  |j                  i       d|i       y y # t        $ r"}t        j                  d|        Y d }~y d }~ww xY w)Nr$   r   r   r   )r6   r   r   r;   outputsr>   zError updating trace run: )r2   popr   rC   rD   r,   r   
update_runr0   r1   rG   rH   rI   )r   r3   runrM   r$   rO   s         r   on_trace_endz)OpenAIAgentsTracingProcessor.on_trace_end   s    **..6C-2JW:>>*5;WAUSUWH
GKK**"4y!$Z%(%8#::>>u~~rR $ ; ; ? ?PR S)84 +   ! G$$'A!%EFFGs   %A4C 	D$DDspanc                    |j                   r%| j                  j                  |j                         n$| j                  j                  |j                        }|#t        j                  d|j                          y |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   ||	j                  di             }
|j                  r"t        j                  |j                        |
d	<    | j,                  j.                  di |
 y # t0        $ r"}t        j3                  d
|        Y d }~y d }~ww xY w)Nz(No trace info found for span, skipping: r   r   )r   r6   parent_dotted_orderr   r7   r;   )r'   r<   r   r   r    r   r;   r   zError creating span run: r   )	parent_idr2   rD   r   rH   warningspan_idr!   r   
started_atr   fromisoformatr?   r   r@   rA   rB   r   get_run_nameget_run_typeextract_span_datarE   r   rF   rG   rI   )r   rW   
parent_runr   span_run_idspan_start_timer   rJ   r<   	extractedrN   rO   s               r   on_span_startz*OpenAIAgentsTracingProcessor.on_span_start   s    >> 

t~~.ZZ^^DMM2  !>t||nM !*-Heg,K ?? &&t7\\(,,/ 
 '::*"BLJ~$>RVL
 (/!*)(.(DJJt||$ #//5H"//5H#55d;IB!%!%"%",T"2!-$==26" ??-5-C-CDOO-TH\*&&&22 B  #<QC!@AABs   +A/G 	H$HHc           
      `   | j                   j                  |j                  d       }|rt        j                  |      }|j                  di       }|j                  |d<   |j                  |d<   |j                  |d<   ||d<   |j                  di       }|j                  di       }t        |d   |d   |d	   |d
   |j                  rt        |j                        nd |||      }|j                  r"t        j                  |j                        |d<   t        |j                  t         j"                        rY| j$                  j                  |j                        xs || j$                  |j                  <   || j&                  |j                  <    | j(                  j*                  di | y y )Nr$   openai_parent_idopenai_trace_idopenai_span_idrR   r;   r   r   r   r    )r6   r   r   r    errorrR   r;   r>   end_timer   )r2   rS   r\   rA   ra   rD   rZ   r   rE   rk   r!   ended_atr   r^   
isinstance	span_datar	   ResponseSpanDatar0   r1   r   rT   )r   rW   rU   re   r$   rR   r;   rN   s           r   on_span_endz(OpenAIAgentsTracingProcessor.on_span_end  ss   **..t4C'99$?	$==R8/3~~+,.2mm*+-1\\)*(0	*%#--	26"x4!%t9 _!$^!4"%o"6-1ZZ#djj/T#!#	" ==+3+A+A$--+PHZ(dnng.F.FG3377FP& //> BID//>&&&229 r   c                 8    | j                   j                          y r)   r   flushr   s    r   shutdownz%OpenAIAgentsTracingProcessor.shutdown>      KKr   c                 8    | j                   j                          y r)   rs   ru   s    r   force_flushz(OpenAIAgentsTracingProcessor.force_flushA  rw   r   r)   )r4   N)r   r   r   r   r   	ls_clientClientrE   listr!   r   r	   TracerP   rV   Spanrf   rq   rv   ry   r   r   r   r   r   Z   s    6	t 26	0 (,(,*."&	0Y--.	0 tn		0
 49%	0 #3-	0 3-	0&.	C .	C$ .	C`	Ggmm 	G 	G$4	Bgll 4	Bt 4	Bl	3GLL 	3T 	3@	 	 r   )loggingr   r   typingr   r   uuidr   	langsmithr   r*   agentsr	   langsmith.wrappers._agent_utilswrappers_agent_utilsrA   HAVE_AGENTSr   r   r   rz   	getLoggerr   rH   r   TracingProcessorr   r   r   <module>r      s     ' &  %B99K@ *			8	$%) %h w'?'? h  G  <K9 9<s   A/ /BB