o
    ٰi                     @   s   d dl Z d dlmZ d dlmZ d dl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 er:d d	lmZ z
d d
lmZ dZW n eyO   dZY nw zd dlmZ d dlmZ W n eyi   edw dddZdS )    N)wraps)DidNotEnable)capture_internal_exceptionsreraise   )execute_tool_spanupdate_execute_tool_span)_capture_exceptionget_current_agent)TYPE_CHECKING)Any)	MCPServerTF)ToolManager)ToolRetryErrorzpydantic-ai not installedreturnc                     s:   t j t 										d
 fdd} | t _d	S )a  
    Patch ToolManager._call_tool to create execute_tool spans.

    This is the single point where ALL tool calls flow through in pydantic_ai,
    regardless of toolset type (function, MCP, combined, wrapper, etc.).

    By patching here, we avoid:
    - Patching multiple toolset classes
    - Dealing with signature mismatches from instrumented MCP servers
    - Complex nested toolset handling
    selfr   callargskwargsr   c                    s  |j }| jr| j|nd }d}|rtrt|jtrd}t }|r|rz| }W n t	y>   t|j
tr:|j
ni }Y nw t  t||||du}	z% | |g|R i |I d H }
t|	|
 |
W W  d    W  d    S  ty } z;t }t " ddlm} t |}|d ur|jrt|dd W d    n1 sw   Y  t|  W Y d }~nd }~ww W d    n1 sw   Y  W d    n1 sw   Y   | |g|R i |I d H S )Nfunctionmcp)	tool_typer   )PydanticAIIntegrationT)handled)	tool_nametoolsgetHAS_MCP
isinstancetoolsetr   r
   args_as_dict	Exceptionr   dict
sentry_sdkisolation_scoper   r   r   sysexc_infor   #sentry_sdk.integrations.pydantic_air   
get_clientget_integrationhandled_tool_call_exceptionsr	   r   )r   r   r   r   nametoolr   agent	args_dictspanresultexcr&   r   integrationoriginal_call_tool e/home/ubuntu/.local/lib/python3.10/site-packages/sentry_sdk/integrations/pydantic_ai/patches/tools.pywrapped_call_tool-   s|   

#z0_patch_tool_execution.<locals>.wrapped_call_toolN)
r   r   r   r   r   r   r   r   r   r   )r   
_call_toolr   )r7   r5   r3   r6   _patch_tool_execution   s   
Ar9   )r   N)r%   	functoolsr   sentry_sdk.integrationsr   r#   sentry_sdk.utilsr   r   spansr   r   utilsr	   r
   typingr   r   pydantic_ai.mcpr   r   ImportErrorpydantic_ai._tool_managerr   pydantic_ai.exceptionsr   r9   r5   r5   r5   r6   <module>   s.    