Ë
    áß²j�  ã                   ó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)zmShared SKILL.md preprocessing helpers: ``${HERMES_*}`` template tokens and
inline ``!`cmd``` shell expansion.é    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_readonlyÚskillszCould not read skills configT)Úexc_info)Úhermes_cli.configr   ÚgetÚ
isinstanceÚdictÚ	ExceptionÚloggerÚdebug)r   Ú
skills_cfgs     ú>/home/ubuntu/.hermes/hermes-agent/agent/skill_preprocessing.pyÚload_skills_configr      sh   € ðDÝ:Ù*Ó,Ò2°×7Ñ7¸ÓAˆ
Ü�j¤$Ô'ØÐð (ð €Iøô ò DÜ�‰Ð3¸dˆÕCØ€IðDús   ‚17 · AÁAÚcontentÚ	skill_dirÚ
session_idc                 ó|   ‡— | s| S |rt        |«      nd|rt        |«      nddœŠt        j                  ˆfd„| «      S )zYReplace ${HERMES_SKILL_DIR} / ${HERMES_SESSION_ID}; tokens without a value stay in place.N)ÚHERMES_SKILL_DIRÚHERMES_SESSION_IDc                 óR   •— ‰| j                  d«         xs | j                  d«      S )Né   r   )Úgroup)ÚmÚvaluess    €r   ú<lambda>z*substitute_template_vars.<locals>.<lambda>*   s!   ø€ ¨F°1·7±7¸1³:Ñ,>Ò,LÀ!Ç'Á'È!Ã*€ ó    )ÚstrÚ_SKILL_TEMPLATE_REÚsub)r   r   r   r   s      @r   Úsubstitute_template_varsr%   "   s>   ø€ áØˆá.7œC 	œN¸TÙ0:œS œ_Àñ€Fô ×!Ñ!Ó"LÈgÓVÐVr!   ÚcommandÚcwdÚtimeoutc                 óò  — t         rdt        «       ini }ddlm} 	 d}t         rddlm}  |«       }t        j                  |d| gf|rt        |«      ndddd	d
t        dt        |«      «      dt        j                   |«       dœ	|¤Ž}|j"                  xs dj%                  d«      xs |j&                  xs dj%                  d«      }	|j(                  dk7  r|	sd|j(                  › d| › d�S t+        |	«      t,        kD  r|	dt,         dz   }	|	S # t        j                  $ r d|› d| › d�cY S t        $ r Y yt        $ r;}t        |t         «      rdt        |«      v rd|› d| › d�cY d}~S d|› d�cY d}~S d}~ww xY w)zßRun one inline-shell snippet and return its stdout (trimmed; stderr when
    stdout is empty). Failures return an ``[inline-shell ...]`` marker instead
    of raising, so one bad snippet can't wreck the whole skill message.Úcreationflagsr   )Údelegated_child_subprocess_envÚbash)Ú
_find_bashz-cNTzutf-8Úreplacer   F)	r'   Úcapture_outputÚtextÚencodingÚerrorsr(   ÚcheckÚstdinÚenvz[inline-shell timeout after zs: ú]z$[inline-shell error: bash not found]z"live-system guard: blocked os.killz[inline-shell error: Ú ú
z[inline-shell exit z with no output: z...[truncated])r   r   Úagent.delegation_contextr+   Útools.environments.localr-   Ú
subprocessÚrunr"   ÚmaxÚintÚDEVNULLÚTimeoutExpiredÚFileNotFoundErrorr   r   ÚRuntimeErrorÚstdoutÚrstripÚstderrÚ
returncodeÚlenÚ_INLINE_SHELL_MAX_OUTPUT)
r&   r'   r(   Ú_popen_kwargsr+   r,   r-   Ú	completedÚexcÚoutputs
             r   Úrun_inline_shellrM   -   sŸ  € õ @J�_Ô&8Ó&:Ñ;Èr€MÝGð.ØˆÝõ <Ù“<ˆDÜ—N‘NØ�4˜Ð!ð

á”�C” TØØ °	Ü˜œ3˜w›<Ó(ØÜ×$Ñ$Ù.Ó0ñ

ð ñ

ˆ	ð* ×ÑÒ$ "×,Ñ,¨TÓ2Ò[°y×7GÑ7GÒ7MÈ2×6UÑ6UÐVZÓ6[€FØ×Ñ˜qÒ ©ð % Y×%9Ñ%9Ð$:Ð:KÈGÈ9ÐTUÐVÐVÜ
ˆ6ƒ{Ô-Ò-ØÐ1Ô1Ð2Ð5EÑEˆØ€Møô# ×$Ñ$ò EØ-¨g¨Y°c¸'¸À!ÐDÒDÜò 6Ù5Üò .ô �cœ<Ô(Ð-QÔUXÐY\ÓU]Ñ-]Ø1°'°¸#¸g¸YÀaÐHÕHØ& s e¨1Ð-Õ-ûð.ús6   œA+D ÄE6Ä)E6Ä3E6Ä;%E1Å E6Å&E1Å+E6Å1E6c                 óx   ‡‡— d| vr| S dt         j                  dt        fˆˆfd„}t        j	                  || «      S )zLReplace every !`cmd` snippet with its stdout, run with the skill dir as CWD.z!`Úmatchr   c                 ód   •— | j                  d«      j                  «       }|rt        |‰‰«      S dS )Nr   r7   )r   ÚstriprM   )rO   Úcmdr   r(   s     €€r   Ú_replacez%expand_inline_shell.<locals>._replace]   s0   ø€ Ø�k‰k˜!‹n×"Ñ"Ó$ˆÙ<?Ô  Y°Ó8ÐGÀRÐGr!   )ÚreÚMatchr"   Ú_INLINE_SHELL_REr$   )r   r   r(   rS   s    `` r   Úexpand_inline_shellrW   Y   s?   ù€ à�7ÑØˆðHœŸ™ð H¤Sö Hô ×Ñ ¨'Ó2Ð2r!   r   c           
      óü   — | s| S t        |t        «      r|n	t        «       }|j                  dd«      rt	        | ||«      } |j                  dd«      r*t        | |t        |j                  dd«      xs d«      «      } | S )zBApply configured SKILL.md template and inline-shell preprocessing.Útemplate_varsTÚinline_shellFÚinline_shell_timeouté
   )r   r   r   r   r%   rW   r>   )r   r   r   r   Úcfgs        r   Úpreprocess_skill_contentr^   c   su   € ñ ØˆÜ" :¬tÔ4‰*Ô:LÓ:N€CØ
‡w�wˆ Ô%Ü*¨7°I¸zÓJˆØ
‡w�wˆ~˜uÔ%Ü% g¨y¼#¸c¿g¹gÐF\Ð^`Ó>aÒ>gÐegÓ:hÓiˆØ€Nr!   )NN)Ú__doc__ÚloggingrT   r;   Úpathlibr   Úhermes_cli._subprocess_compatr   r   Ú	getLoggerÚ__name__r   Úcompiler#   rV   rH   r   r   r"   r%   r>   rM   rW   r^   © r!   r   ú<module>rg      s&  ðñ&ó Û 	Û Ý ç Hà	ˆ×	Ñ	˜8Ó	$€ð  �R—Z‘ZÐ MÓNÐ à�2—:‘:˜oÓ.Ð àÐ ð	˜Dó 	ðW cð W°d¸T±kð WÈsÐUYÉzð WÐ^aó Wð)˜cð )¨¨t©ð )¸cð )Àcó )ðX3 ð 3°¸±ð 3Àsð 3Èsó 3ð "Ø"ñ	Øðà�d‰{ðð �d‘
ðð �t‘ð	ð
 	ôr!   