
    iu                         d Z ddlZddlZddlZddlZddlZddlmZmZ ddl	m
Z
  ej        e          Z G d d          ZdS )a  Shared debug session infrastructure for Hermes tools.

Replaces the identical DEBUG_MODE / _log_debug_call / _save_debug_log /
get_debug_session_info boilerplate previously duplicated across web_tools,
vision_tools, mixture_of_agents_tool, and image_generation_tool.

Usage in a tool module:

    from tools.debug_helpers import DebugSession

    _debug = DebugSession("web_tools", env_var="WEB_TOOLS_DEBUG")

    # Log a call (no-op when debug mode is off)
    _debug.log_call("web_search", {"query": q, "results": len(r)})

    # Save the debug log (no-op when debug mode is off)
    _debug.save()

    # Expose debug info to external callers
    def get_debug_session_info():
        return _debug.get_session_info()
    N)AnyDict)get_hermes_homec                       e Zd ZdZdededdfdZedefd            Zded	e	ee
f         ddfd
ZddZde	ee
f         fdZdS )DebugSessionzPer-tool debug session that records tool calls to a JSON log file.

    Activated by a tool-specific environment variable (e.g. WEB_TOOLS_DEBUG=true).
    When disabled, all methods are cheap no-ops.
    	tool_nameenv_varreturnNc                   || _         t          j        |d                                          dk    | _        | j        r t          t          j                              nd| _        t                      dz  | _
        g | _        | j        r0t          j                                                                        nd| _        | j        r?| j
                            dd           t"                              d|| j                   d S d S )Nfalsetrue logsT)parentsexist_okz&%s debug mode enabled - Session ID: %s)r   osgetenvlowerenabledstruuiduuid4
session_idr   log_dir_callsdatetimenow	isoformat_start_timemkdirloggerdebug)selfr   r	   s      8/home/ubuntu/.hermes/hermes-agent/tools/debug_helpers.py__init__zDebugSession.__init__+   s    "y'2288::fD/3|C#djll+++&((61,.BF,V8,0022<<>>>TV< 	5Ltd;;;LLA"DO5 5 5 5 5	5 	5    c                     | j         S )N)r   r#   s    r$   activezDebugSession.active8   s
    |r&   	call_name	call_datac                     | j         sdS | j                            t          j                                                                        |d|           dS )z.Append a tool-call entry to the in-memory log.N)	timestampr   )r   r   appendr   r   r   )r#   r*   r+   s      r$   log_callzDebugSession.log_call<   sj    | 	F!*..00::<<"
 
 
 	 	 	 	 	r&   c                 :   | j         sdS 	 | j         d| j         d}| j        |z  }| j        | j        t
          j                                                                        dt          | j	                  | j	        d}t          |dd          5 }t          j        ||d	d
           ddd           n# 1 swxY w Y   t                              d| j        |           dS # t          $ r,}t                              d| j        |           Y d}~dS d}~ww xY w)z=Flush the in-memory log to a JSON file in the logs directory.N_debug_.jsonT)r   
start_timeend_timedebug_enabledtotal_calls
tool_callswzutf-8)encoding   F)indentensure_asciiz%s debug log saved: %szError saving %s debug log: %s)r   r   r   r   r   r   r   r   lenr   openjsondumpr!   r"   	Exceptionerror)r#   filenamefilepathpayloadfes         r$   savezDebugSession.saveF   s   | 	F	M.GGGGGH|h.H"o".$-1133==??!%"4;//"k G hg666 D!	'1QUCCCCD D D D D D D D D D D D D D DLL14>8LLLLL 	M 	M 	MLL8$.!LLLLLLLLL	Ms<   BC$ B6*C$ 6B::C$ =B:>$C$ $
D.!DDc           	          | j         sdddddS d| j        t          | j        | j         d| j         dz            t          | j                  dS )zKReturn a summary dict suitable for returning from get_debug_session_info().FNr   )r   r   log_pathr6   Tr1   r2   )r   r   r   r   r   r=   r   r(   s    r$   get_session_infozDebugSession.get_session_info[   st    | 	 "  	   /DLdn+[+[T_+[+[+[[\\t{++	
 
 	
r&   )r
   N)__name__
__module____qualname____doc__r   r%   propertyboolr)   r   r   r/   rH   rK    r&   r$   r   r   $   s         5# 53 54 5 5 5 5     X# $sCx. T    M M M M*
$sCx. 
 
 
 
 
 
r&   r   )rO   r   r?   loggingr   r   typingr   r   hermes_constantsr   	getLoggerrL   r!   r   rR   r&   r$   <module>rW      s    .    				          , , , , , ,		8	$	$E
 E
 E
 E
 E
 E
 E
 E
 E
 E
r&   