
    `gj]                         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y)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, 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                   v    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y)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      j                         dk(  | _        | j                  rt        t        j                               nd| _        t               dz  | _
        g | _        | j                  r,t        j                  j                         j                         nd| _        | j                  r?| j                  j!                  dd       t"        j%                  d|| j                         y y )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      F/root/.hermes/venv/lib/python3.12/site-packages/tools/debug_helpers.py__init__zDebugSession.__init__+   s    "yy'288:fD/3||#djjl+&(61,.BF,,8,,002<<>TV<<LLtd;LLA"DOO5     c                     | j                   S )N)r   r#   s    r$   activezDebugSession.active8   s    ||r&   	call_name	call_datac                     | j                   sy| j                  j                  t        j                  j	                         j                         |d|       y)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<   sK    ||!**..0::<"
 
 	r&   c                 f   | j                   sy	 | j                   d| j                   d}| j                  |z  }| j                  | j                  t
        j
                  j                         j                         dt        | j                        | j                  d}t        |dd      5 }t        j                  ||d	d
       ddd       t        j                  d| j                  |       y# 1 sw Y   +xY w# t        $ r+}t        j!                  d| j                  |       Y d}~y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    ||	M..)0AGH||h.H"oo"..$--113==?!%"4;;/"kkG hg6 D!		'1QUCDLL14>>8LD D  	MLL8$..!LL	Ms0   BC< ,C0)C< 0C95C< <	D0!D++D0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[   sj    || "  	  //DLLdnn-=WT__DUUZ+[[\t{{+	
 	
r&   )r
   N)__name__
__module____qualname____doc__r   r%   propertyboolr)   r   r   r/   rH   rK    r&   r$   r   r   $   sx    5# 53 54 5   # $sCx. T 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
r&   