
    `i                         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mZ ddlmZmZ 	 d d	lmZ n# e$ r  ed
          w xY wd dlmZ er
d dlmZmZmZ ddZ	 	 	 	 ddZdS )    Nwraps)SPANDATA)DidNotEnable)capture_internal_exceptionsreraise   )agent_workflow_spanend_invoke_agent_span)_capture_exception_record_exception_on_span)AgentsExceptionzOpenAI Agents not installed)TYPE_CHECKING)AnyAsyncIteratorCallableoriginal_funcCallable[..., Any]returnc                 >     t                     d fd            }|S )z
    Wraps the agents.Runner.run methods to create a root span for the agent workflow runs.

    Note agents.Runner.run_sync() is a wrapper around agents.Runner.run(),
    so it does not need to be wrapped separately.
    argsr   kwargsr   c            	      "  K   t          j                    5  | d                                         }t          |          5 }|                    d          }|r'||_        |                    t          j        |           |g| dd          R } 	  
| i | d {V }n# t          $ r}t          j                    }t                      5  t          |           t          |j        dd           }|:t          |dd           }	|	'|	j         t#          |	|           t%          ||           d d d            n# 1 swxY w Y   t'          |  Y d }~nkd }~wt(          $ r[}t          j                    }t                      5  t          |           d d d            n# 1 swxY w Y   t'          |  Y d }~nd }~ww xY wt%          |j        |           |cd d d            cd d d            S # 1 swxY w Y   	 d d d            d S # 1 swxY w Y   d S )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   )r   r   agentworkflow_spanr   
run_resultexcr&   r   invoke_agent_spanr   s             /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/sentry_sdk/integrations/openai_agents/patches/runner.pywrapperz$_create_run_wrapper.<locals>.wrapper   s      ')) ,	" ,	"GMMOOE$U++ ("}"(**->"?"?" 4CE1!** 7   )QRR))''4}d'Ef'E'E!E!E!E!E!E!EJJ& ' ' '"|~~H466 N N*3///*1#,@QSW*X*X*607 /1Et1 1-
 !2 =$5$?$G 9:KS Q Q Q 5ou M M MN N N N N N N N N N N N N N N X&&&&&&&  ' ' '"|~~H466 0 0 +3///	0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 X&&&&&&&' &j&@%HHH!Q(" (" (" (" (" (" ("	,	" ,	" ,	" ,	" ,	" ,	" ,	" ,	"(" (" (" (" (" (" (" (" ("	,	" ,	" ,	" ,	" ,	" ,	" ,	" ,	" ,	" ,	" ,	" ,	" ,	" ,	" ,	" ,	" ,	" ,	"s   *HAG+BG+
F9)!E
A"D8	,E8D<<E?D< EG+F9!F4?F	F4FF4"F#F4/G+4F99G+H+G/	/H2G/	3HHHr   r   r   r   r   r   r   r   r1   s   ` r0   _create_run_wrapperr4      s>     =/" /" /" /" /" /"b N    c                 >     t                     d fd            }|S )aP  
    Wraps the agents.Runner.run_streamed method to create a root span for streaming agent workflow runs.

    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).
    r   r   r   r   c                    	
 | d                                          |                    d          }|r|_        t                                                     |r                     t          j        |           _        g| dd          R } 	  | i |}n=# t          $ r0} j
        t          j                      t          |            d }~ww xY wdfdt          |d          r0|j        
t!          
          	 	 	 	 	 	 d
fd            }||_        t          |d          r*|j        	t!          	          d	fd            }||_        |S )Nr   r   r   r   c                      t           d          r- j        t          j                      t	           d           d S d S )N_sentry_workflow_span)hasattr__exit__r%   r&   delattr)r+   r,   s   r0   _close_workflow_spanzK_create_run_streamed_wrapper.<locals>.wrapper.<locals>._close_workflow_span   sM    u566 8&&776777778 8r5   stream_eventsstream_argsr   stream_kwargsAsyncIterator[Any]c                 n   K   	  | i |2 3 d {V }|W V  6 	               d S #               w xY wN )r?   r@   eventr=   original_stream_eventss      r0   wrapped_stream_eventszL_create_run_streamed_wrapper.<locals>.wrapper.<locals>.wrapped_stream_events   s      +'='=$((5( ( $ $ $ $ $ $ $e $( (
 )(*****((****s   ( ( 4cancelcancel_argscancel_kwargsc                  H    	  | i |              S #               w xY wrC   rD   )rI   rJ   r=   original_cancels     r0   wrapped_cancelzE_create_run_streamed_wrapper.<locals>.wrapper.<locals>.wrapped_cancel   sB    +*?KI=II((****((****s    !)r   N)r?   r   r@   r   r   rA   )rI   r   rJ   r   r   r   )r    r!   r"   r
   	__enter__r#   r   r$   r9   r*   r;   r%   r&   r   r:   r>   r   rH   )r   r   r   r-   r.   rG   rM   r=   r+   rL   rF   r,   r   s          @@@@@r0   r1   z-_create_run_streamed_wrapper.<locals>.wrapperc   s     Q !**%677 	<,;E) ,E22!!!  	U""8#BOTTT '4#!QRR!!	&777JJ 	 	 	"M"CLNN33s###			8 	8 	8 	8 	8 	8 	8
 :// 	=%/%=")**	+#	+6;	+%	+ 	+ 	+ 	+ 	+ 	+ +*	+ (=J$:x(( 
	/(/O?##+ + + + + + $#+ !/Js   B# #
C-+CCr2   r   r3   s   ` r0   _create_run_streamed_wrapperrO   T   sD     =A A A A A AF Nr5   )r   r   r   r   )r%   	functoolsr   r   sentry_sdk.constsr   sentry_sdk.integrationsr   sentry_sdk.utilsr   r   spansr
   r   utilsr   r   agents.exceptionsr   ImportErrortypingr   r   r   r   r4   rO   rD   r5   r0   <module>rY      sc   



           & & & & & & 0 0 0 0 0 0 A A A A A A A A > > > > > > > > A A A A A A A A61111111 6 6 6
,4
5
556 !           43333333333: : : :zS'SS S S S S Ss	   ; A