
    `i                         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 	 d d
lmZ dZn# e$ r dZY nw xY w	 d dlmZ d dlmZ n# e$ r  ed          w xY w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                      t           j        t                    	 	 	 	 	 	 	 	 	 	 d	f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           	        K   |j         }| j        r| j                            |          nd }d}|r#t          rt	          |j        t                    rd}t                      }|r|r}	 |                                }n3# t          $ r& t	          |j
        t                    r|j
        ni }Y nw xY wt          j                    5  t          ||||          5 }		  | |g|R i | d {V }
t          |	|
           |
cd d d            cd d d            S # t           $ r}t#          j                    }t'                      5  ddlm} t          j                                        |          }||j        rt3          |d           d d d            n# 1 swxY w Y   t5          |  Y d }~nd }~ww xY w	 d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y    | |g|R i | d {V 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_tools                  /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/sentry_sdk/integrations/pydantic_ai/patches/tools.pywrapped_call_toolz0_patch_tool_execution.<locals>.wrapped_call_tool-   s     
 ~'+z;tz~~d###t 	 	G 	
4< C C 	I "## (	+T (	+M --//		 M M M)3DIt)D)DLDII"			M
 +--  +  +&'	   +
 +'9'9  ( "( ( ( %	( ( " " " " " " 1v>>>%+ + + + + + + +  +  +  +  +  +  +  +  * + + +#&<>>8:: F F      +5*?*A*A*Q*Q 5+ +K !, 7$/$L !8 !33 E E E EF F F F F F F F F F F F F F F  *******!+  +?+ + + + + + + + + + + + + + + +  +  +  +  +  +  +  +  +  +  +  +  +  +  +F ('
 
 
 
 	
 
 
 
 
 
 
 
 	
s   *A? ?-B/.B/GG$D?G
F4"!F/AF	
F/FF/FF/*G/F44G8GG	GG	GG"GN)
r   r   r   r   r   r   r   r   r   r   )r   
_call_toolr   )r8   r6   s    @r7   _patch_tool_executionr:      sy     %/
?
?
 ?
).?
:??
	?
 ?
 ?
 ?
 ?
 ?
B /K    )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   r:    r;   r7   <module>rG      sp   



       0 0 0 0 0 0     A A A A A A A A ? ? ? ? ? ? ? ? 9 9 9 9 9 9 9 9             ))))))GG   GGG45555555555555 4 4 4
,2
3
334Q/ Q/ Q/ Q/ Q/ Q/s   A AAA   A1