
    iz                        % S r SSKrSSKrSSKJrJr  SSKJrJrJr  SSK	J
r
  SSKJr  \(       a
  SSKJrJrJr  \R"                  " \5      r0 q\\\\\4   4   \S	'   S
SS\\   SSSS4S jrS
SS\\   SSSS4S jrSS jrg)zHook-based tool tracing for Claude Agent SDK.

This module provides hook handlers that traces tool calls by intercepting
PreToolUse and PostToolUse events.
    N)datetimetimezone)TYPE_CHECKINGAnyOptional)get_current_run_tree   )get_parent_run_tree)HookContext	HookInputHookJSONOutput_active_tool_runs
input_datar   tool_use_idcontextr   returnr   c           
        #    U(       d  [         R                  S5        0 $ [        U R                  SS5      5      nU R                  S0 5      n [	        5       =(       d
    [        5       nU(       d  [         R                  SU 35        0 $ [        R                  " 5       nUR                  USU(       a  SU0O0 [        R                  " U[        R                  S9S	9n UR                  5         Xv4[        U'   [         R                  SU SU S35        0 $ ! [         a%  n[         R                  S
U SU 35         SnANRSnAff = f! [         a%  n[         R                  SU SU 3SS9   SnA0 $ SnAff = f7f)a#  Trace tool execution before it starts.

Args:
    input_data: Contains tool_name, tool_input, session_id
    tool_use_id: Unique identifier for this tool invocation
    context: Hook context (currently contains only signal)

Returns:
    Hook output (empty dict allows execution to proceed)
z:PreToolUse hook called without tool_use_id, skipping trace	tool_nameunknown_tool
tool_inputz"No parent run tree found for tool toolinput)tz)namerun_typeinputs
start_timezFailed to post tool run for : NzStarted tool trace for  (id=)zError in PreToolUse hook for Texc_info)loggerdebugstrgetr
   r   timecreate_childr   fromtimestampr   utcpost	Exceptionwarningr   )	r   r   r   r   r   parentr   tool_runes	            h/home/james-whalen/.local/lib/python3.13/site-packages/langsmith/integrations/claude_agent_sdk/_hooks.pypre_tool_use_hookr2      sc     QR	^DEIb1JX$&@*>*@LL=i[IJIYY[
&&,6GZ(B--jX\\J	 ' 
	LMMO +3)?+&.yk{m1MN
 I  	LNN9)BqcJKK	L  X6ykA3GRVWIXsg   AE;;E	 
E;AE	 D /&E	 E;
E!E<E	 EE	 	
E8E3-E;3E88E;c           	        #    U(       d  [         R                  S5        0 $ [        U R                  SS5      5      nU R                  S5      n [        R                  US5      nU(       d  [         R                  SU SU S35        0 $ Uu  pg[        U[        5      (       a  UnO0[        U[        5      (       a  S	U0nOU(       a  S
[        U5      0O0 nSn	[        U[        5      (       a  UR                  SS5      n	UR                  UU	(       a  UR                  S
5      OSS9   UR                  5         [        R                  " 5       U-
  S-  n[         R                  SU SU SUS S35        0 $ ! [         a%  n
[         R                  SU SU
 35         Sn
A
NgSn
A
ff = f! [         a%  n
[         R                  SU SU
 3SS9   Sn
A
0 $ Sn
A
ff = f7f)a)  Trace tool execution after it completes.

Args:
    input_data: Contains tool_name, tool_input, tool_response, session_id, etc.
    tool_use_id: Unique identifier for this tool invocation
    context: Hook context (currently contains only signal)

Returns:
    Hook output (empty dict by default)
z;PostToolUse hook called without tool_use_id, skipping tracer   r   tool_responseNz!No matching PreToolUse found for r   r    contentoutputFis_error)outputserrorzFailed to patch tool run for r   i  zCompleted tool trace for z, duration=z.2fzms)zError in PostToolUse hook for Tr!   )r#   r$   r%   r&   r   pop
isinstancedictlistendpatchr,   r-   r'   )r   r   r   r   r4   run_infor/   r   r8   r7   r0   duration_mss               r1   post_tool_use_hookrB   Q   s     RS	^DEINN?3M'Y$((d;LL3I;eK=PQR I'mT**#Gt,, -0G8Ex]!342G mT**$((U;H+3'++h' 	 	

	MNN yy{Z/47'	{ 3-{;s*;3@	
 I  	MNN:9+RsKLL	M  Y7	{"QCHSWXIYsg   AG:F& G	BF& 'E4 7;F& 2G4
F#>FF& F##F& &
G0G
GGGc                     [         R                  5        H(  u  n u  p UR                  SS9  UR                  5         M*     [         R                  5         g! [         a&  n[
        R                  SU  SU 35         SnAMl  SnAff = f)zClear all active tool runs.

This should be called when a conversation ends to avoid memory leaks
and to clean up any orphaned tool runs.
z+Tool run not completed (conversation ended))r9   z%Failed to clean up orphaned tool run r   N)r   itemsr>   r?   r,   r#   r$   clear)r   r/   _r0   s       r1   clear_active_tool_runsrG      s~     '8&=&=&?"]h	ULLLLMNN '@   	ULL@RPQsSTT	Us   A
B BB)r   N)__doc__loggingr'   r   r   typingr   r   r   langsmith.run_helpersr   _toolsr
   claude_agent_sdkr   r   r   	getLogger__name__r#   r   r<   r%   tuplefloat__annotations__r2   rB   rG        r1   <module>rU      s      ' / / 6 '  
		8	$ 35 4U3:../ 400#0 0 	0f??#? ? 	?DrT   