Ë
    ãß²j‹  ã                   ó�   — 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defd„Z G d„ d«      Zy)	z’Shared per-tool debug session: records tool calls to a JSON log when a
tool-specific env var (e.g. WEB_TOOLS_DEBUG=true) is set; no-ops otherwise.é    N)ÚAnyÚDict)Úget_hermes_homeÚreturnc                  óZ   — t         j                   j                  «       j                  «       S ©N)ÚdatetimeÚnowÚ	isoformat© ó    ú8/home/ubuntu/.hermes/hermes-agent/tools/debug_helpers.pyÚ_nowr      s    € Ü×Ñ× Ñ Ó"×,Ñ,Ó.Ð.r   c                   ó`   — 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y)ÚDebugSessionzBPer-tool debug session that records tool calls to a JSON log file.Ú	tool_nameÚenv_varr   Nc                óÌ  — || _         t        j                  |d«      j                  «       dk(  | _        | j                  rt        t        j                  «       «      nd| _        t        «       dz  | _
        g | _        | j                  r
t        «       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Ú_callsr   Ú_start_timeÚmkdirÚloggerÚdebug)Úselfr   r   s      r   Ú__init__zDebugSession.__init__   s£   € Ø"ˆŒÜ—y‘y ¨'Ó2×8Ñ8Ó:¸fÑDˆŒØ/3¯|ª|œ#œdŸj™j›lÔ+ÀˆŒÜ&Ó(¨6Ñ1ˆŒØ,.ˆŒØ%)§\¢\œ4œ6°rˆÔØ�<Š<Ø�L‰L×Ñ t°dÐÔ;Ü�L‰LÐAÀ9ÈdÏoÉoÕ^ð r   c                 ó   — | j                   S r   )r   )r)   s    r   ÚactivezDebugSession.active"   s   € à�|‰|Ðr   Ú	call_nameÚ	call_datac                 ón   — | j                   r)| j                  j                  t        «       |dœ|¥«       yy)z.Append a tool-call entry to the in-memory log.)Ú	timestampr   N)r   r$   Úappendr   )r)   r-   r.   s      r   Úlog_callzDebugSession.log_call&   s,   € à�<Š<Ø�K‰K×Ñ¬T«VÀ)ÑYÈyÐYÕZð r   c                 ó  — | j                   sy	 | j                  | j                  › d| j                  › d�z  }| j                  | j                  t        «       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_z.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   Úlenr$   ÚopenÚjsonÚdumpr'   r(   Ú	ExceptionÚerror)r)   ÚfilepathÚpayloadÚfÚes        r   ÚsavezDebugSession.save+   sç   € à�|Š|Øð		MØ—|‘|¨¯©Ð(8¸ÀÇÁÐ?PÐPUÐ&VÑVˆHà"Ÿo™o¸T×=MÑ=MÔ[_Ó[aØ!%´c¸$¿+¹+Ó6FÐVZ×VaÑVañcˆGô �h ¨gÔ6ð D¸!Ü—	‘	˜' 1¨Q¸UÕC÷Dä�L‰LÐ1°4·>±>À8ÕL÷Dð Dûô ò 	MÜ�L‰LÐ8¸$¿.¹.È!×LÑLûð	Mús0   �A9C ÂCÂ")C ÃCÃC Ã	DÃ!!DÄD)r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r*   ÚpropertyÚboolr,   r   r   r2   rI   r   r   r   r   r      sj   „ ÙLð	_ #ð 	_°3ð 	_¸4ó 	_ð ð˜ò ó ðð[ #ð [°$°s¸C°x±.ð [ÀTó [ô
Mr   r   )rM   r	   rA   Úloggingr   r    Útypingr   r   Úhermes_constantsr   Ú	getLoggerrJ   r'   r   r   r   r   r   r   ú<module>rT      sL   ðñOó Û Û Û 	Û ß å ,à	ˆ×	Ñ	˜8Ó	$€ð/ˆcó /÷$Mò $Mr   