o
    ٰi                     @   s   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 zd d	lmZ W n eyE   ed
w d dlmZ erXd dlmZmZmZ dddZ				d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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.
    argsr   kwargsr   c            
         s  t   | d  }t|}|d}|r"||_|tj| |g| dd  R } z | i |I d H }W n t	y } zIt
 }t 0 t| t|jdd }|d urpt|dd }	|	d urp|	jd u rpt|	| t|| W d    n1 szw   Y  t|  W Y d }~n5d }~w ty } z%t
 }t  t| W d    n1 sw   Y  t|  W Y d }~nd }~ww t|j| |W  d    W  d    S 1 sw   Y  W d    d S 1 sw   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_spanr    h/home/ubuntu/.local/lib/python3.10/site-packages/sentry_sdk/integrations/openai_agents/patches/runner.pywrapper!   sX   






	"z$_create_run_wrapper.<locals>.wrapperNr   r   r   r   r   r   r   r   r1   r/   r.   r0   _create_run_wrapper   s   
1r4   c                    r   )	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).
    r   r   r   r   c               
      s  | d   |d}|r|_t  |r!tj| _g| dd  R } z	| i |}W n t	yO } zj
t   t|  d }~ww dfdd t|dru|jt				
				d fdd}||_t|dr|jtd fdd}||_|S )Nr   r   r   r   c                      s*   t  drjt   t d d S d S )N_sentry_workflow_span)hasattr__exit__r#   r$   delattrr/   )r)   r*   r/   r0   _close_workflow_span   s   
zK_create_run_streamed_wrapper.<locals>.wrapper.<locals>._close_workflow_spanstream_eventsstream_argsr   stream_kwargsAsyncIterator[Any]c                    s<   z| i |2 z	3 d H W }|V  q	6 W    d S    w Nr/   )r;   r<   event)r9   original_stream_eventsr/   r0   wrapped_stream_events   s   zL_create_run_streamed_wrapper.<locals>.wrapper.<locals>.wrapped_stream_eventscancelcancel_argscancel_kwargsc                     s    z| i |W    S    w r>   r/   )rC   rD   )r9   original_cancelr/   r0   wrapped_cancel   s   zE_create_run_streamed_wrapper.<locals>.wrapper.<locals>.wrapped_cancel)r   N)r;   r   r<   r   r   r=   )rC   r   rD   r   r   r   )r   r   r    r	   	__enter__r!   r   r"   r5   r(   r7   r#   r$   r   r6   r:   r   rB   )r   r   r   r+   r,   rA   rF   r.   )r9   r)   rE   r@   r*   r0   r1   g   sH   


z-_create_run_streamed_wrapper.<locals>.wrapperNr2   r   r3   r/   r.   r0   _create_run_streamed_wrapperV   s   CrH   )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   rH   r/   r/   r/   r0   <module>   s,    
?