
    i                     2   d Z ddlZddlZddlZddlmZ  ej        e          Z ej	        d          Z
 ej	        d          ZdZdefdZd	ed
edz  dedz  defdZdededz  dedefdZd	ed
edz  dedefdZ	 	 dd	ed
edz  dedz  dedz  def
dZdS )z&Shared SKILL.md preprocessing helpers.    N)Pathz*\$\{(HERMES_SKILL_DIR|HERMES_SESSION_ID)\}z!`([^`\n]+)`i  returnc                      	 ddl m}   |             pi }|                    d          }t          |t                    r|S n,# t
          $ r t                              dd           Y nw xY wi S )z9Load the ``skills`` section of config.yaml (best-effort).r   )load_configskillszCould not read skills configT)exc_info)hermes_cli.configr   get
isinstancedict	Exceptionloggerdebug)r   cfg
skills_cfgs      >/home/ubuntu/.hermes/hermes-agent/agent/skill_preprocessing.pyload_skills_configr      s    D111111kmm!rWWX&&
j$'' 		 D D D3dCCCCCDIs   =A &A*)A*content	skill_dir
session_idc                     | s| S |rt          |          nddt          j        dt           ffd}t                              ||           S )zReplace ${HERMES_SKILL_DIR} / ${HERMES_SESSION_ID} in skill content.

    Only substitutes tokens for which a concrete value is available --
    unresolved tokens are left in place so the author can spot them.
    Nmatchr   c                     |                      d          }|dk    rrS |dk    rrt                    S |                      d          S )N   HERMES_SKILL_DIRHERMES_SESSION_IDr   )groupstr)r   tokenr   skill_dir_strs     r   _replacez*substitute_template_vars.<locals>._replace4   sV    A&&&=&  '''J'z??"{{1~~    )r   reMatch_SKILL_TEMPLATE_REsub)r   r   r   r!   r    s     ` @r   substitute_template_varsr'   %   sr      &/9C	NNNTM S        !!(G444r"   commandcwdtimeoutc                    	 t          j        dd| g|rt          |          ndddt          dt	          |                    d          }nB# t           j        $ r d| d	|  d
cY S t          $ r Y dS t          $ r}d| d
cY d}~S d}~ww xY w|j        pd	                    d          }|s!|j
        r|j
        	                    d          }t          |          t          k    r|dt                   dz   }|S )zExecute a single inline-shell snippet and return its stdout (trimmed).

    Failures return a short ``[inline-shell error: ...]`` marker instead of
    raising, so one bad snippet can't wreck the whole skill message.
    bashz-cNTr   F)r)   capture_outputtextr*   checkz[inline-shell timeout after zs: ]z$[inline-shell error: bash not found]z[inline-shell error:  
z...[truncated])
subprocessrunr   maxintTimeoutExpiredFileNotFoundErrorr   stdoutrstripstderrlen_INLINE_SHELL_MAX_OUTPUT)r(   r)   r*   	completedexcoutputs         r   run_inline_shellrA   ?   s]   .NT7#)CT3w<<((
 
 
		 $ E E EDgDD'DDDDDD 6 6 6555 . . .-s---------. $",,T22F /i& /!((..
6{{---11125EEMs*   A	A B&	B2	B;B BBc                 |    d| vr| S dt           j        dt          ffd}t                              ||           S )zReplace every !`cmd` snippet in ``content`` with its stdout.

    Runs each snippet with the skill directory as CWD so relative paths in
    the snippet work the way the author expects.
    z!`r   r   c                 |    |                      d                                          }|sdS t          |          S )Nr   r1   )r   striprA   )r   cmdr   r*   s     r   r!   z%expand_inline_shell.<locals>._replacej   s=    kk!nn""$$ 	2Y888r"   )r#   r$   r   _INLINE_SHELL_REr&   )r   r   r*   r!   s    `` r   expand_inline_shellrG   ]   sa     79 9S 9 9 9 9 9 9 9 '222r"   r   c                 >   | s| S t          |t                    r|nt                      }|                    dd          rt	          | ||          } |                    dd          r6t          |                    dd          pd          }t          | ||          } | S )zBApply configured SKILL.md template and inline-shell preprocessing.template_varsTinline_shellFinline_shell_timeout
   )r   r   r   r
   r'   r6   rG   )r   r   r   r   r   r*   s         r   preprocess_skill_contentrM   s   s      ":t44
N**:L:N:NC
ww%% K*7IzJJ
ww~u%% Ccgg4b99?R@@%gy'BBNr"   )NN)__doc__loggingr#   r3   pathlibr   	getLogger__name__r   compiler%   rF   r=   r   r   r   r'   r6   rA   rG   rM    r"   r   <module>rU      s   , ,  				          		8	$	$
  RZ MNN  2:o..    D    55d{5 d
5 		5 5 5 54c t c c    <33d{3 3 		3 3 3 32 ""	 d{ d
 t	
 	     r"   