
    `gj                     L   d Z ddlZddlZddlZddlmZ ddlmZ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y)z&Shared SKILL.md preprocessing helpers.    N)Path)
IS_WINDOWSwindows_hide_flagsz*\$\{(HERMES_SKILL_DIR|HERMES_SESSION_ID)\}z!`([^`\n]+)`i  returnc                      	 ddl m}   |        xs i }|j                  d      }t        |t              r|S 	 i S # t
        $ r t        j                  dd       Y i S w xY w)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      L/root/.hermes/venv/lib/python3.12/site-packages/agent/skill_preprocessing.pyload_skills_configr      sj    D1m!rWWX&
j$' ( I  D3dCIDs   39  AAcontent	skill_dir
session_idc                     | s| S |rt        |      nddt        j                  dt         ffd}t        j	                  ||       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                 ~    | j                  d      }|dk(  rrS |dk(  rrt              S | j                  d      S )N   HERMES_SKILL_DIRHERMES_SESSION_IDr   )groupstr)r   tokenr   skill_dir_strs     r   _replacez*substitute_template_vars.<locals>._replace6   sD    A&&=  ''Jz?"{{1~    )r    reMatch_SKILL_TEMPLATE_REsub)r   r   r   r#   r"   s     ` @r   substitute_template_varsr)   '   sF     &/C	NTM S  !!(G44r$   commandcwdtimeoutc                    t         rdt               ini }	 t        j                  dd| gf|rt	        |      ndddt        dt        |            dt        j                  d|}|j                  xs dj                  d      }|s'|j                  r|j                  j                  d      }t        |      t         kD  r|dt          dz   }|S # t        j                  $ r d	| d
|  dcY S t        $ r Y yt        $ r+}dt	        |      v rd	| d
|  dcY d}~S d| dcY d}~S d}~wt        $ r}d| dcY d}~S d}~ww xY w)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.
    creationflagsbashz-cNTr   F)r+   capture_outputtextr,   checkstdinz[inline-shell timeout after zs: ]z$[inline-shell error: bash not found]z"live-system guard: blocked os.killz[inline-shell error:  
z...[truncated])r   r   
subprocessrunr    maxintDEVNULLTimeoutExpiredFileNotFoundErrorRuntimeErrorr   stdoutrstripstderrlen_INLINE_SHELL_MAX_OUTPUT)r*   r+   r,   _popen_kwargs	completedexcoutputs          r   run_inline_shellrH   A   s^    @J_&8&:;rM.NNT7#	
CT3w<($$	
 	
	2 $",,T2Fi&&!!((.
6{--1125EEM) $$ E-gYc'!DD 65 .
 03s8;1'#gYaHH&se1-- .&se1--.sH   AC D>*D>4D><D"D>D"D>"D>.D93D>9D>c                 x    d| vr| S dt         j                  dt        ffd}t        j	                  ||       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                 b    | j                  d      j                         }|syt        |      S )Nr   r5   )r   striprH   )r   cmdr   r,   s     r   r#   z%expand_inline_shell.<locals>._replacew   s.    kk!n""$Y88r$   )r%   r&   r    _INLINE_SHELL_REr(   )r   r   r,   r#   s    `` r   expand_inline_shellrN   j   s>     79 9S 9 '22r$   r   c                     | s| S t        |t              r|n	t               }|j                  dd      rt	        | ||      } |j                  dd      r,t        |j                  dd      xs d      }t        | ||      } | S )zBApply configured SKILL.md template and inline-shell preprocessing.template_varsTinline_shellFinline_shell_timeout
   )r   r   r   r   r)   r:   rN   )r   r   r   r   r   r,   s         r   preprocess_skill_contentrT      sx     ":t4*:L:NC
ww%*7IzJ
ww~u%cgg4b9?R@%gy'BNr$   )NN)__doc__loggingr%   r7   pathlibr   hermes_cli._subprocess_compatr   r   	getLogger__name__r   compiler'   rM   rC   r   r   r    r)   r:   rH   rN   rT    r$   r   <module>r]      s/   ,  	   H			8	$
  RZZ MN  2::o.    D 55d{5 d
5 		54&c &t &c &c &R33d{3 3 		32 ""	d{ d
 t	
 	r$   