§
    ÷žyjµ  ã                   óB  — 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dS )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}   | ¦   «         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_readonlyÚskillszCould not read skills configT)Úexc_info)Úhermes_cli.configr   ÚgetÚ
isinstanceÚdictÚ	ExceptionÚloggerÚdebug)r   ÚcfgÚ
skills_cfgs      ú?/home/ragecks/.hermes/hermes-agent/agent/skill_preprocessing.pyÚload_skills_configr      sœ   € ðDØ:Ð:Ð:Ð:Ð:Ð:à"Ð"Ñ$Ô$Ð*¨ˆØ—W’W˜XÑ&Ô&ˆ
Ý�j¥$Ñ'Ô'ð 	ØÐð	øåð Dð Dð DÝ�ŠÐ3¸dˆÑ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 )zÖReplace ${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>._replace6   sV   ø€ Ø—’˜A‘”ˆØÐ&Ò&Ð&¨=Ð&Ø Ð ØÐ'Ò'Ð'¨JÐ'Ý�z‘?”?Ð"Ø�{Š{˜1‰~Œ~Ðó    )r    ÚreÚMatchÚ_SKILL_TEMPLATE_REÚsub)r   r   r   r#   r"   s     ` @r   Úsubstitute_template_varsr)   '   sr   øø€ ð ð Øˆà&/Ð9•C˜	‘N”N�N°T€Mð�œð ¥Sð ð ð ð ð ð ð õ ×!Ò! (¨GÑ4Ô4Ð4r$   ÚcommandÚcwdÚtimeoutc                 ó¨  — t           rdt          ¦   «         ini }	 t          j        dd| gf|rt	          |¦  «        ndddddt          dt          |¦  «        ¦  «        d	t          j        d
œ|¤Ž}ny# t          j        $ r d|› d| › d�cY S t          $ r Y dS 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|j        pd                     d¦  «        }|s!|j        r|j                             d¦  «        }t          |¦  «        t           k    r|dt           …         dz   }|S )zÝExecute 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-cNTzutf-8Úreplacer   F)r+   Úcapture_outputÚtextÚencodingÚerrorsr,   Ú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_shellrK   A   s  € õ @JÐQ�_Õ&8Ñ&:Ô&:Ð;Ð;Èr€Mð.Ý”NØ�T˜7Ð#ð	
àÐ)•�C‘”� TØØ °	Ý˜�3˜w™<œ<Ñ(Ô(ØÝÔ$ð	
ð 	
ð ð	
ð 	
ˆ	ˆ	øõ Ô$ð Eð Eð EØD¨gÐDÐD¸'ÐDÐDÐDÐDÐDÐDÝð 6ð 6ð 6Ø5Ð5Ð5Ýð .ð .ð .ð
 0µ3°s±8´8Ð;Ð;ØH°'ÐHÐH¸gÐHÐHÐHÐHÐHÐHÐHÐHÐHØ- sÐ-Ð-Ð-Ð-Ð-Ð-Ð-Ð-Ð-øøøøÝð .ð .ð .Ø- sÐ-Ð-Ð-Ð-Ð-Ð-Ð-Ð-Ð-øøøøð.øøøð ÔÐ$ "×,Ò,¨TÑ2Ô2€FØð /�iÔ&ð /ØÔ!×(Ò(¨Ñ.Ô.ˆÝ
ˆ6�{„{Õ-Ò-Ð-ØÐ1Õ1Ð1Ô2Ð5EÑEˆØ€MsH   ›AA0 Á0C&Â
	C&Â	C&ÂC	Â8C&Â>C	ÃC&Ã	C&ÃC!ÃC&Ã!C&c                 ó|   ‡‡— d| vr| S dt           j        dt          fˆˆfd„}t                               || ¦  «        S )z¾Replace 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   r8   )r   ÚstriprK   )r   Úcmdr   r,   s     €€r   r#   z%expand_inline_shell.<locals>._replacew   s=   ø€ Ø�kŠk˜!‰nŒn×"Ò"Ñ$Ô$ˆØð 	Ø�2Ý  Y°Ñ8Ô8Ð8r$   )r%   r&   r    Ú_INLINE_SHELL_REr(   )r   r   r,   r#   s    `` r   Úexpand_inline_shellrQ   j   sa   øø€ ð �7ÐÐØˆð9�œð 9¥Sð 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)   r=   rQ   )r   r   r   r   r   r,   s         r   Úpreprocess_skill_contentrW   €   s¦   € ð ð Øˆå" :­tÑ4Ô4Ð
Nˆ*ˆ*Õ:LÑ:NÔ:N€CØ
‡w‚wˆ Ñ%Ô%ð KÝ*¨7°I¸zÑJÔJˆØ
‡w‚wˆ~˜uÑ%Ô%ð CÝ�c—g’gÐ4°bÑ9Ô9Ð?¸RÑ@Ô@ˆÝ% g¨y¸'ÑBÔBˆØ€Nr$   )NN)Ú__doc__Úloggingr%   r:   Úpathlibr   Úhermes_cli._subprocess_compatr   r   Ú	getLoggerÚ__name__r   Úcompiler'   rP   rF   r   r   r    r)   r=   rK   rQ   rW   © r$   r   ú<module>r`      sÇ  ðØ ,Ð ,à €€€Ø 	€	€	€	Ø Ð Ð Ð Ø Ð Ð Ð Ð Ð à HÐ HÐ HÐ HÐ HÐ HÐ HÐ Hà	ˆÔ	˜8Ñ	$Ô	$€ð
  �R”ZÐ MÑNÔNÐ ð �2”:˜oÑ.Ô.Ð ð  Ð ð˜Dð ð ð ð ð5Øð5à�d‰{ð5ð �d‘
ð5ð 	ð	5ð 5ð 5ð 5ð4&˜cð &¨¨t©ð &¸cð &Àcð &ð &ð &ð &ðR3Øð3à�d‰{ð3ð ð3ð 	ð	3ð 3ð 3ð 3ð2 "Ø"ð	ð Øðà�d‰{ðð �d‘
ðð �t‘ð	ð
 	ðð ð ð ð ð r$   