
    f3fi3                        U d Z ddlZddlZddlmZmZ ddlmZmZmZ ddl	m
Z
 ddlmZ ddlmZ er
dd	lmZmZmZ  ej&                  e      Zi aeeeeef   f   ed
<   i aeeef   ed<   dddee   ddddfdZdddee   ddddfdZddZy)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)RunTree   )get_parent_run_tree)HookContext	HookInputHookJSONOutput_active_tool_runs_client_managed_runs
input_datar   tool_use_idcontextr   returnr   c           	        K   |st         j                  d       i S |t        v ri S t        | j	                  dd            }| j	                  di       }	 t               xs
 t               }|st         j                  d|        i S t        j                         }|j                  |d|rd|ini t        j                  |t        j                        	      }	 |j                          ||ft         |<   t         j                  d| d| d       i S # t        $ r%}t         j                  d
| d|        Y d}~Rd}~ww xY w# t        $ r(}t         j                  d| d| d       Y d}~i S d}~ww xY ww)aE  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debugr   strgetr   r   timecreate_childr   fromtimestampr   utcpost	Exceptionwarningr   )	r   r   r   r   r   parentr   tool_runes	            l/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langsmith/integrations/claude_agent_sdk/_hooks.pypre_tool_use_hookr4   #   sy     QR	 **	^DEIb1JX$&@*>*@LL=i[IJIYY[
&&,6GZ(B--jX\\J	 ' 
	LMMO +3J)?+&.yk{m1MN
 I  	LNN9)BqcJKK	L  X6ykA3GRVWWIXsg   AE71E E7AE D )'E E7	E D;6E ;E  E 	E4E/)E7/E44E7c           	      0  K   |st         j                  d       i S t        | j                  dd            }| j                  d      }t        j                  |d      }|r	 t        |t              r|}n&t        |t              rd|i}n|rdt        |      ini }d}t        |t              r|j                  d	d      }|j                  ||r|j                  d      nd
       |j                          i S 	 t        j                  |d      }	|	st         j                  d| d| d       i S |	\  }
}t        |t              r|}n&t        |t              rd|i}n|rdt        |      ini }d}t        |t              r|j                  d	d      }|
j                  ||r|j                  d      nd
       	 |
j                          t        j                         |z
  dz  }t         j                  d| d| d|dd       i S # t        $ r#}t         j                  d|        Y d}~i S d}~ww xY w# t        $ r%}t         j                  d| d|        Y d}~d}~ww xY w# t        $ r(}t         j                  d| d| d       Y d}~i S d}~ww xY ww)aO  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_responseNcontentoutputFis_error)outputserrorz%Failed to update client-managed run: z!No matching PreToolUse found for r!   r"   z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   r   r6   run_treer:   r9   r2   run_infor1   r   duration_mss                r3   post_tool_use_hookrE   Y   s     RS	^DEINN?3M $''T:H	H-.'M40$m4<I8S%78rH-.(,,Z?LL/7gkk(+T   NN 	'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[  	HNNB1#FGG		HB  	MNN:9+RsKLL	M  Y7	{"QCHSWXXIYs   AJ!BH 4J75I" ,J-BI" 6H1 :I"  J	H.H)#J)H..J1	I:II" II" "	J+JJJJc                     t         j                         D ](  \  } }	 |j                  d       |j                          * t        j                         D ]+  \  } \  }}	 |j                  d       |j                          - t        j                          t         j                          y# t        $ r%}t
        j                  d|  d|        Y d}~d}~ww xY w# t        $ r%}t
        j                  d|  d|        Y d}~d}~ww xY w)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.
    z5Client-managed run not completed (conversation ended))r;   z/Failed to clean up orphaned client-managed run r    Nz+Tool run not completed (conversation ended)z%Failed to clean up orphaned tool run )	r   itemsr@   rA   r.   r%   r&   r   clear)r   rB   r2   r1   _s        r3   clear_active_tool_runsrJ      s    "6!;!;!= X	LLVLWNN '8&=&=&? U"]h	ULLLLMNNU    	LLA+bQRPST 	  	ULL@RPQsSTT	Us/   "B+"C+	C4CC	D
%DD
)r   N) __doc__loggingr)   r   r   typingr   r   r   langsmith.run_helpersr   langsmith.run_treesr	   _toolsr   claude_agent_sdkr   r   r   	getLogger__name__r%   r   r>   r'   tuplefloat__annotations__r   r4   rE   rJ        r3   <module>rY      s      ' / / 6 ' '  
		8	$ 35 4U3:../ 4 ,. d3<( -33#3 3 	3lXX#X X 	Xv!rX   