
    \j                         d dl Z d dlmZ d dlZd dlmZ d dlmZ d dlm	Z	m
Z
 d dlmZ ddlmZmZ dd	lmZ 	 d d
lmZ d dlmZ er
d dlmZmZmZ ddZ	 	 	 	 ddZy# e$ r	  ed      w xY w)    Nwraps)SPANDATA)DidNotEnable)capture_internal_exceptionsreraise)set_span_errored   )agent_workflow_spanend_invoke_agent_span)_capture_exception)AgentsExceptionzOpenAI Agents not installed)TYPE_CHECKING)AnyAsyncIteratorCallablec                 0     t               d fd       }|S )aA  
    Wraps the agents.Runner.run methods to
    - create and manage a root span for the agent workflow runs.
    - end the agent invocation span if an `AgentsException` is raised in `run()`.

    Note agents.Runner.run_sync() is a wrapper around agents.Runner.run(),
    so it does not need to be wrapped separately.
    c            	        K   t        j                         5  | d   j                         }t        |      5 }|j	                  d      }|r'||_        |j                  t        j                  |       |g| dd  } 	  
| i | d {   }t%        j*                  |       |cd d d        cd d d        S 7 .# t        $ r}t        j                         }t               5  t        |       t        |j                  dd       }|2t        |dd       }	|	#|	j                   t#        |	       t%        ||       d d d        n# 1 sw Y   nxY wt'        |  Y d }~d }~wt(        $ rR}t        j                         }t               5  t        |       d d d        n# 1 sw Y   nxY wt'        |  Y d }~%d }~ww xY w# 1 sw Y   nxY w	 d d d        y # 1 sw Y   y xY ww)Nr   conversation_id   context_wrapper_sentry_agent_span)
sentry_sdkisolation_scopecloner   get_sentry_conversation_idset_datar   GEN_AI_CONVERSATION_IDr   sysexc_infor   r   getattrrun_data	timestampr	   r   r   	Exceptionr   )argskwargsagentworkflow_spanr   
run_resultexcr!   r   invoke_agent_spanoriginal_funcs             ^/root/env/lib/python3.12/site-packages/sentry_sdk/integrations/openai_agents/patches/runner.pywrapperz$_create_run_wrapper.<locals>.wrapper"   s     '') ,	"GMMOE$U+ ("}"(**->"?"4CE1!** 77 )QR)''4d'Ef'E!EJ8 &j&@&@%H!Q(" ("	,	" ,	" "F& '"||~H46 N*3/*1#,,@QSW*X*607 /1Et1-
 !2 =$5$?$?$G 01B C 5ou MN N N X&  '"||~H46 0 +3/	0 0 0
 X&'=(" (" ("	,	" ,	" ,	"s   GGAF9<B8B6B8F9#	G,
G6B88	F6EAD?	6	E?EEF9F6$F1F		F1F F1+F91F66F99G	>G	GGGr&   r   r'   r   returnr   r   r-   r/   s   ` r.   _create_run_wrapperr3      s$     =/" /"b N    c                 0     t               d fd       }|S )a  
    Wraps the agents.Runner.run_streamed method to
    - create a root span for streaming agent workflow runs.
    - end the workflow span if and only if the response stream is consumed or cancelled.

    Unlike run(), run_streamed() returns immediately with a RunResultStreaming object
    while execution continues in a background task. The workflow span must stay open
    throughout the streaming operation and close when streaming completes or is abandoned.

    Note: We don't use isolation_scope() here because it uses context variables that
    cannot span async boundaries (the __enter__ and __exit__ would be called from
    different async contexts, causing ValueError).
    c                  ~  	
 | d   j                         |j                  d      }|r|_        t              j	                          |r j                  t        j                  |       _        g| dd  } 	  | i |}d	fdt        |d      r.|j                  
t!        
      	 	 	 	 	 	 d

fd       }||_        t        |d      r(|j"                  	t!        	      d	fd       }||_        |S # t        $ r2} j                  t        j                           t        |        d }~ww xY w)Nr   r   r   c                  z    t         d      r. j                  t        j                           t	         d       y y )N_sentry_workflow_span)hasattr__exit__r    r!   delattr)r(   r)   s   r.   _close_workflow_spanzK_create_run_streamed_wrapper.<locals>.wrapper.<locals>._close_workflow_span   s4    u56&&&767 7r4   stream_eventsc                 l   K   	  | i |2 3 d {   }| 7 
6 	          y #          w xY wwN )stream_argsstream_kwargseventr<   original_stream_eventss      r.   wrapped_stream_eventszL_create_run_streamed_wrapper.<locals>.wrapper.<locals>.wrapped_stream_events   sL     +'=$((5( $ $e $$ (
 )*(*s,   4( ( ( 4	14cancelc                  <    	  | i |         S #          w xY wr?   r@   )cancel_argscancel_kwargsr<   original_cancels     r.   wrapped_cancelzE_create_run_streamed_wrapper.<locals>.wrapper.<locals>.wrapped_cancel   s#    +*KI=I(*(*s    	)r1   N)rA   r   rB   r   r1   zAsyncIterator[Any])rH   r   rI   r   r1   r   )r   r   r   r   	__enter__r   r   r   r8   r%   r:   r    r!   r   r9   r=   r   rF   )r&   r'   r   r*   r+   rE   rK   r<   r(   rJ   rD   r)   r-   s          @@@@@r.   r/   z-_create_run_streamed_wrapper.<locals>.wrapperh   s[    Q !**%67,;E) ,E2! ""8#B#BOT '4#!QR!	&77J	8
 :/%/%=%=")*	+#	+6;	+%	+ +	+ (=J$:x((//O?#+ $+ !/JQ  	"M""CLLN3s#		s   D 	D<
-D77D<r0   r   r2   s   ` r.   _create_run_streamed_wrapperrM   W   s&    " =A AF Nr4   )r-   Callable[..., Any]r1   rN   )r    	functoolsr   r   sentry_sdk.constsr   sentry_sdk.integrationsr   sentry_sdk.utilsr   r   sentry_sdk.tracing_utilsr	   spansr   r   utilsr   agents.exceptionsr   ImportErrortypingr   r   r   r   r3   rM   r@   r4   r.   <module>rY      sr    
   & 0 A 5 > &61 !33<~U'UUQ  6
4
556s   A A+