o
    i                     @   s  d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dl
mZ d dlmZ d d	lmZ d d
lmZ d dlmZ eeZedi  deeef fddZdefddZdd Zdd Zdd Zdd Zdededeedf fddZ g dZ!dd  Z"d!d" Z#dS )#    N)Any)Union)config)check_module_path)unwrap)wrap)
get_logger)get_argument_value)GoogleAdkIntegration)extract_provider_and_model_name
google_adkreturnc                   C   s   ddiS )N
google.adkz>=1.0.0 r   r   r   ]/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/contrib/internal/google_adk/patch.py_supported_versions   s   r   c                   C   s   t tddS )N__version__ )getattradkr   r   r   r   get_version   s   r   c           	         s   t jtdd}t|dd}t|dd\}}jdjj| jf f||dddz	| i  W n tyH   jt	
      w  fdd	}| S )
z7Trace the main execution of an agent (async generator).agentNmodelinstancemodel_name_attr%s.%sTproviderr   kindsubmit_to_llmobsc                    s   g } z=z 2 z3 d H W }|  | |V  q6 W n ty'   jt    w W jd< j| dd   d= d S jd< j| dd   d= w )Nr   r   argskwargsresponse	operation)append	Exceptionset_exc_infosysexc_infor   llmobs_set_tagsfinish)response_eventseventagenr"   r   integrationr#   spanr   r   
_generator5   s(   



z+_traced_agent_run_async.<locals>._generator)r   _datadog_integrationr   r   trace	__class____name__r'   r(   r)   r*   r,   )	wrappedr   r"   r#   r   r   provider_name
model_namer3   r   r/   r   _traced_agent_run_async   s.   	r;   c           
         s  t j}t||}|d u rtd | |i |S tt|di dd\}}|p*|d }|jd|jj	| j	f ||ddd>}d }	z/z| |i |I d H }	|	W W |j
||||	dd	 W  d    S  tyn   |jt    w |j
||||	dd	 w 1 s}w   Y  d S )
NUUnable to trace google adk live tool call, could not extract agent from tool context.r   r   r   r   toolTr   r!   )r   r4   extract_agent_from_tool_contextloggerwarningr   r   r5   r6   r7   r+   r'   r(   r)   r*   )
r8   r   r"   r#   r1   r   r9   r:   r2   resultr   r   r   !_traced_functions_call_tool_asyncG   sT   


rB   c                 C  s,  t j}t||}|d u r&td | |i |}|2 z	3 d H W }|V  q6 tt|di dd\}}	|jd|jj	| j	f ||	dddI}
d }z2z| |i |}|2 z	3 d H W }|V  qQ6 W n t
yl   |
jt    w W |j|
|||dd n|j|
|||dd w W d    d S 1 sw   Y  d S )	Nr<   r   r   r   r=   Tr   r!   )r   r4   r>   r?   r@   r   r   r5   r6   r7   r'   r(   r)   r*   r+   )r8   r   r"   r#   r1   r   r0   itemr9   r:   r2   rA   r   r   r    _traced_functions_call_tool_livek   sZ   



"rD   c                 C   s   t j}t||dd}tt|dddi }t|dd\}}|jd|jj| jf ||dd	d
;}	d}
z,z| |i |}
|
W W |j|	|||
dd W  d   S  t	y\   |	j
t    w |j|	|||
dd w 1 skw   Y  dS )z0Trace the execution of code by the agent (sync).r   invocation_contextr   Nr   r   r   code_executeTr   r!   )r   r4   r	   r   r   r5   r6   r7   r+   r'   r(   r)   r*   )r8   r   r"   r#   r1   rE   r   r9   r:   r2   rA   r   r   r   "_traced_code_executor_execute_code   sH   rG   r"   r#   c                 C   s4   t | |dd}d }t|drt|jdr|jj}|S )N   tool_context_invocation_contextr   )r	   hasattrrJ   r   )r"   r#   rI   r   r   r   r   r>      s
   r>   )BuiltInCodeExecutorVertexAiCodeExecutorUnsafeLocalCodeExecutorContainerCodeExecutorc                  C   s   t tddrdS ttdd ttjd} ttd|  tddt tdd	t tdd
t tddt	 t
D ]}ttd| drItdd| dt q4dS )z+Patch the `google.adk` library for tracing._datadog_patchFNT)integration_configr4   r   zrunners.Runner.run_asynczrunners.Runner.run_livez+flows.llm_flows.functions.__call_tool_asyncz*flows.llm_flows.functions.__call_tool_livecode_executors..execute_code)r   r   setattrr
   r   r   r   r;   rB   rD   CODE_EXECUTOR_CLASSESr   rG   )r1   code_executorr   r   r   patch   s$   
rW   c                  C   s   t tdr
ttdsdS ttdd ttjjd ttjjd ttjjj	d ttjjj	d t
D ]} ttd|  d	rFtttj| d
 q2ttd dS )z!Unpatch the `google.adk` library.rP   NF	run_asyncrun_live__call_tool_async__call_tool_liverR   rS   execute_coder4   )rK   r   r   rT   r   runnersRunnerflows	llm_flows	functionsrU   r   code_executorsdelattr)rV   r   r   r   unpatch   s   rd   )$r)   typingr   r   
google.adkr   ddtracer   $ddtrace.contrib.internal.trace_utilsr   ddtrace.contrib.trace_utilsr   r   ddtrace.internal.loggerr   ddtrace.internal.utilsr	   ddtrace.llmobs._integrationsr
   )ddtrace.llmobs._integrations.google_utilsr   r7   r?   _adddictstrr   r   r;   rB   rD   rG   r>   rU   rW   rd   r   r   r   r   <module>   s0    )$& 