§
    ‰eîiÄ8  ã                   ó²  — U 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m	Z	 ddl
mZ ddlmZmZmZ  ej        e¦  «        Zi aeeeeef         f         ed<    ej        d¦  «        Z ej        d	¦  «        Zd d
ededz  deeeef         edz  ef         dz  fd„Zdeeef         dee         ddfd„Z 	 	 	 d!deeef         dedz  dededededz  defd„Z!deeeeef         f         fd„Z"deeeeef         f         fd„Z#dede	e         fd„Z$	 	 	 d"dedededz  dede	e         f
d„Z%	 d dee         dedz  deeee         ee         f         fd„Z&dS )#z Shared slash command helpers for skills.

Shared between CLI (cli.py) and gateway (gateway/run.py) so both surfaces
can invoke skills via /skill-name commands.
é    N)ÚPath)ÚAnyÚDictÚOptional)Údisplay_hermes_home)Úexpand_inline_shellÚload_skills_configÚsubstitute_template_varsÚ_skill_commandsz
[^a-z0-9-]z-{2,}Úskill_identifierÚtask_idÚreturnc                 óh  — | pd                      ¦   «         }|sdS 	 ddlm}m} t	          |¦  «                             ¦   «         }|                     ¦   «         rZ	 t          |                     ¦   «          	                    |                     ¦   «         ¦  «        ¦  «        }n'# t          $ r |}Y nw xY w|                     d¦  «        }t          j         |||d¬¦  «        ¦  «        }n# t          $ r Y dS w xY w|                     d¦  «        sdS t          |                     d	¦  «        p|¦  «        }t          |                     d
¦  «        pd¦  «        }	d}
|                     d¦  «        }|rt	          |¦  «        }
n-|	r+	 |t	          |	¦  «        j        z  }
n# t          $ r d}
Y nw xY w||
|fS )zOLoad a skill by name/path and return (loaded_payload, skill_dir, display_name).Ú Nr   )Ú
SKILLS_DIRÚ
skill_viewú/F)r   Ú
preprocessÚsuccessÚnameÚpathÚ	skill_dir)ÚstripÚtools.skills_toolr   r   r   Ú
expanduserÚis_absoluteÚstrÚresolveÚrelative_toÚ	ExceptionÚlstripÚjsonÚloadsÚgetÚparent)r   r   Úraw_identifierr   r   Úidentifier_pathÚ
normalizedÚloaded_skillÚ
skill_nameÚ
skill_pathr   Úabs_skill_dirs               ú6/home/div/.hermes/hermes-agent/agent/skill_commands.pyÚ_load_skill_payloadr.      s  € à&Ð,¨"×3Ò3Ñ5Ô5€NØð ØˆtðØ<Ð<Ð<Ð<Ð<Ð<Ð<Ð<å˜~Ñ.Ô.×9Ò9Ñ;Ô;ˆØ×&Ò&Ñ(Ô(ð 	4ð,Ý  ×!8Ò!8Ñ!:Ô!:×!FÒ!FÀz×GYÒGYÑG[ÔG[Ñ!\Ô!\Ñ]Ô]�
�
øÝð ,ð ,ð ,Ø+�
�
�
ð,øøøð (×.Ò.¨sÑ3Ô3ˆJå”zØˆJ�z¨7¸uÐEÑEÔEñ
ô 
ˆˆøõ ð ð ð Øˆtˆtðøøøð ×Ò˜IÑ&Ô&ð Øˆtå�\×%Ò% fÑ-Ô-Ð;°Ñ<Ô<€JÝ�\×%Ò% fÑ-Ô-Ð3°Ñ4Ô4€JØ€Ið
 !×$Ò$ [Ñ1Ô1€MØð Ý˜Ñ'Ô'ˆ	ˆ	Ø	ð ð	Ø"¥T¨*Ñ%5Ô%5Ô%<Ñ<ˆIˆIøÝð 	ð 	ð 	ØˆIˆIˆIð	øøøð ˜ JÐ.Ð.sH   œ=C) ÁAB! Â C) Â!B0Â-C) Â/B0Â08C) Ã)
C7Ã6C7ÆF ÆF,Æ+F,r)   Úpartsc                 ó  — 	 ddl m}m}m} t	          |                      d¦  «        p|                      d¦  «        pd¦  «        }|sdS  ||¦  «        \  }} ||¦  «        }|sdS  ||¦  «        }	|	sdS ddt          ¦   «         › d�g}
|	                     ¦   «         D ]3\  }}|rt	          |¦  «        nd	}|
                     d
|› d|› �¦  «         Œ4|
                     d¦  «         | 	                    |
¦  «         dS # t          $ r Y dS w xY w)ae  Resolve and inject skill-declared config values into the message parts.

    If the loaded skill's frontmatter declares ``metadata.hermes.config``
    entries, their current values (from config.yaml or defaults) are appended
    as a ``[Skill config: ...]`` block so the agent knows the configured values
    without needing to read config.yaml itself.
    r   )Úextract_skill_config_varsÚparse_frontmatterÚresolve_skill_config_valuesÚraw_contentÚcontentr   Nz[Skill config (from z/config.yaml):z	(not set)z  z = Ú])Úagent.skill_utilsr1   r2   r3   r   r$   r   ÚitemsÚappendÚextendr    )r)   r/   r1   r2   r3   r4   ÚfrontmatterÚ_Úconfig_varsÚresolvedÚlinesÚkeyÚvalueÚdisplay_vals                 r-   Ú_inject_skill_configrC   I   sŠ  € ðð	
ð 	
ð 	
ð 	
ð 	
ð 	
ð 	
ð 	
ð 	
ð 	
õ ˜,×*Ò*¨=Ñ9Ô9Ð^¸\×=MÒ=MÈiÑ=XÔ=XÐ^Ð\^Ñ_Ô_ˆØð 	ØˆFà*Ð*¨;Ñ7Ô7‰ˆ�QØ/Ð/°Ñ<Ô<ˆØð 	ØˆFà.Ð.¨{Ñ;Ô;ˆØð 	ØˆFàÐQÕ,?Ñ,AÔ,AÐQÐQÐQÐRˆØ"Ÿ.š.Ñ*Ô*ð 	5ð 	5‰JˆC�Ø(-Ð>�#˜e™*œ*˜*°;ˆKØ�LŠLÐ3˜cÐ3Ð3 kÐ3Ð3Ñ4Ô4Ð4Ð4Ø�Š�SÑÔÐØ�Š�UÑÔÐÐÐøÝð ð ð Øˆˆðøøøs%   ‚AC= Á	C= Á&C= Á5BC= Ã=
DÄ
Dr   r   Úactivation_noteÚuser_instructionÚruntime_noteÚ
session_idc                 óÆ  — ddl m} t          |                      d¦  «        pd¦  «        }t	          ¦   «         }|                     dd¦  «        rt          |||¦  «        }|                     dd¦  «        r6t          |                     d	d
¦  «        pd
¦  «        }	t          |||	¦  «        }|d|                     ¦   «         g}
|rC|
 	                    d¦  «         |
 	                    d|› d�¦  «         |
 	                    d¦  «         t          | |
¦  «         |                      d¦  «        r|
                     ddg¦  «         n‚|                      d¦  «        r"|
                     dd| d         › d�g¦  «         nK|                      d¦  «        r6|                      d¦  «        r!|
                     dd| d         › d�g¦  «         g }|                      d¦  «        pi }|                     ¦   «         D ],}t          |t          ¦  «        r|                     |¦  «         Œ-|s¤|r¢dD ]Ÿ}||z  }|                     ¦   «         r„t!          |                     d¦  «        ¦  «        D ]a}|                     ¦   «         rK|                     ¦   «         s7t          |                     |¦  «        ¦  «        }| 	                    |¦  «         ŒbŒ |r¦|r¤	 t          |                     |¦  «        ¦  «        }n# t*          $ r
 |j        }Y nw xY w|
 	                    d¦  «         |
 	                    d¦  «         |D ] }|
 	                    d|› d||z  › �¦  «         Œ!|
 	                    d|› d|› d�¦  «         |r-|
 	                    d¦  «         |
 	                    d|› �¦  «         |r.|
 	                    d¦  «         |
 	                    d|› d�¦  «         d                     |
¦  «        S ) z9Format a loaded skill into a user/system message payload.r   )r   r5   r   Útemplate_varsTÚinline_shellFÚinline_shell_timeouté
   z[Skill directory: r6   z¯Resolve any relative paths in this skill (e.g. `scripts/foo.js`, `templates/config.yaml`) against that directory, then run them with the terminal tool using the absolute path.Úsetup_skippedz‹[Skill setup note: Required environment setup was skipped. Continue loading the skill and explain any reduced functionality if it matters.]Úgateway_setup_hintz[Skill setup note: Úsetup_neededÚ
setup_noteÚlinked_files)Ú
referencesÚ	templatesÚscriptsÚassetsÚ*z"[This skill has supporting files:]z- z  ->  z)
Load any of these with skill_view(name="zM", file_path="<path>"), or run scripts directly by absolute path (e.g. `node z/scripts/foo.js`).zPThe user has provided the following instruction alongside the skill invocation: z[Runtime note: Ú
)r   r   r   r$   Ú_load_skills_configÚ_substitute_template_varsÚintÚ_expand_inline_shellr   r9   rC   r:   ÚvaluesÚ
isinstanceÚlistÚexistsÚsortedÚrglobÚis_fileÚ
is_symlinkr   Ú
ValueErrorr   Újoin)r)   r   rD   rE   rF   rG   r   r5   Ú
skills_cfgÚtimeoutr/   Ú
supportingrQ   ÚentriesÚsubdirÚsubdir_pathÚfÚrelÚskill_view_targetÚsfs                       r-   Ú_build_skill_messagerp   p   s¥  € ð -Ð,Ð,Ð,Ð,Ð,å�,×"Ò" 9Ñ-Ô-Ð3°Ñ4Ô4€Gõ
 %Ñ&Ô&€JØ‡~‚~�o tÑ,Ô,ð LÝ+¨G°YÀ
ÑKÔKˆØ‡~‚~�n eÑ,Ô,ð DÝ�j—n’nÐ%;¸RÑ@Ô@ÐFÀBÑGÔGˆÝ& w°	¸7ÑCÔCˆà˜b '§-¢-¡/¤/Ð2€Eð ð 
Ø�Š�RÑÔÐØ�ŠÐ6¨)Ð6Ð6Ð6Ñ7Ô7Ð7Ø�Šð>ñ	
ô 	
ð 	
õ ˜ uÑ-Ô-Ð-à×Ò˜Ñ(Ô(ð 
Ø�Šàð ^ðñ	
ô 	
ð 	
ð 	
ð 
×	Ò	Ð.Ñ	/Ô	/ð 
Ø�ŠàØK lÐ3GÔ&HÐKÐKÐKðñ	
ô 	
ð 	
ð 	
ð 
×	Ò	˜.Ñ	)Ô	)ð 
¨l×.>Ò.>¸|Ñ.LÔ.Lð 
Ø�ŠàØC l°<Ô&@ÐCÐCÐCðñ	
ô 	
ð 	
ð €JØ×#Ò# NÑ3Ô3Ð9°r€LØ×&Ò&Ñ(Ô(ð 'ð 'ˆÝ�g�tÑ$Ô$ð 	'Ø×Ò˜gÑ&Ô&Ð&øàð /˜)ð /ØFð 	/ð 	/ˆFØ# fÑ,ˆKØ×!Ò!Ñ#Ô#ð /Ý × 1Ò 1°#Ñ 6Ô 6Ñ7Ô7ð /ð /�AØ—y’y‘{”{ð /¨1¯<ª<©>¬>ð /Ý! !§-¢-°	Ñ":Ô":Ñ;Ô;˜Ø"×)Ò)¨#Ñ.Ô.Ð.øøàð 
�ið 
ð	/Ý # I×$9Ò$9¸*Ñ$EÔ$EÑ FÔ FÐÐøÝð 	/ð 	/ð 	/à )¤ÐÐÐð	/øøøð 	�Š�RÑÔÐØ�ŠÐ9Ñ:Ô:Ð:Øð 	:ð 	:ˆBØ�LŠLÐ8˜bÐ8Ð8¨	°B©Ð8Ð8Ñ9Ô9Ð9Ð9Ø�Šð9Ð9Jð 9ð 9à$ð9ð 9ð 9ñ	
ô 	
ð 	
ð ð |Ø�Š�RÑÔÐØ�ŠÐzÐhxÐzÐzÑ{Ô{Ð{àð 8Ø�Š�RÑÔÐØ�ŠÐ6 |Ð6Ð6Ð6Ñ7Ô7Ð7à�9Š9�UÑÔÐs   Ë"K/ Ë/LÌLc                  ó  — i a 	 ddlm} m}m}m} ddlm}m}  |¦   «         }t          ¦   «         }g }|  
                    ¦   «         r|                     | ¦  «         |                      |¦   «         ¦  «         |D �]í}	 ||	d¦  «        D �]Ü}
t          d„ |
j        D ¦   «         ¦  «        rŒ"	 |
                     d¬¦  «        } ||¦  «        \  }} ||¦  «        sŒS|                     d|
j        j        ¦  «        }||v rŒx||v rŒ}|                     d	d
¦  «        }|sa|                     ¦   «                              d¦  «        D ]9}|                     ¦   «         }|r!|                     d¦  «        s|dd…         } nŒ:|                     |¦  «         |                     ¦   «                              dd¦  «                             dd¦  «        }t2                               d
|¦  «        }t6                               d|¦  «                             d¦  «        }|s�Œ”||pd|› d�t9          |
¦  «        t9          |
j        ¦  «        dœt           d|› �<   �ŒÍ# t:          $ r Y �ŒÚw xY w�Œïn# t:          $ r Y nw xY wt           S )z®Scan ~/.hermes/skills/ and return a mapping of /command -> skill info.

    Returns:
        Dict mapping "/skill-name" to {name, description, skill_md_path, skill_dir}.
    r   )r   Ú_parse_frontmatterÚskill_matches_platformÚ_get_disabled_skill_names)Úget_external_skills_dirsÚiter_skill_index_fileszSKILL.mdc              3   ó   K  — | ]}|d v V — Œ	dS ))z.gitz.githubz.hubN© )Ú.0Úparts     r-   ú	<genexpr>z&scan_skill_commands.<locals>.<genexpr>í   s(   è è € ÐVÐV¸t�tÐ:Ð:ÐVÐVÐVÐVÐVÐVó    zutf-8)Úencodingr   Údescriptionr   rW   ú#NéP   Ú Ú-r<   zInvoke the z skill)r   r~   Úskill_md_pathr   r   )r   r   r   rr   rs   rt   r7   ru   rv   Úsetr_   r9   r:   Úanyr/   Ú	read_textr$   r%   r   r   ÚsplitÚ
startswithÚaddÚlowerÚreplaceÚ_SKILL_INVALID_CHARSÚsubÚ_SKILL_MULTI_HYPHENr   r    )r   rr   rs   rt   ru   rv   ÚdisabledÚ
seen_namesÚdirs_to_scanÚscan_dirÚskill_mdr5   r;   Úbodyr   r~   ÚlineÚcmd_names                     r-   Úscan_skill_commandsr—   ×   s  € ð €Oð5ØwÐwÐwÐwÐwÐwÐwÐwÐwÐwÐwÐwØVÐVÐVÐVÐVÐVÐVÐVØ,Ð,Ñ.Ô.ˆÝ™%œ%ˆ
ð ˆØ×ÒÑÔð 	,Ø×Ò 
Ñ+Ô+Ð+Ø×ÒÐ4Ð4Ñ6Ô6Ñ7Ô7Ð7à$ð '	ñ '	ˆHØ2Ð2°8¸ZÑHÔHð &ñ &�ÝÐVÐVÀxÄ~ÐVÑVÔVÑVÔVð Øð#Ø&×0Ò0¸'Ð0ÑBÔB�GØ(:Ð(:¸7Ñ(CÔ(CÑ%�K à1Ð1°+Ñ>Ô>ð !Ø Ø&Ÿ?š?¨6°8´?Ô3GÑHÔH�DØ˜zÐ)Ð)Ø à˜xÐ'Ð'Ø Ø"-§/¢/°-ÀÑ"DÔ"D�KØ&ð &Ø$(§J¢J¡L¤L×$6Ò$6°tÑ$<Ô$<ð &ð &˜DØ#'§:¢:¡<¤<˜DØ#ð &¨D¯OªO¸CÑ,@Ô,@ð &Ø.2°3°B°3¬i Ø % øØ—N’N 4Ñ(Ô(Ð(ð  $Ÿzšz™|œ|×3Ò3°C¸Ñ=Ô=×EÒEÀcÈ3ÑOÔO�HÝ3×7Ò7¸¸HÑEÔE�HÝ2×6Ò6°s¸HÑEÔE×KÒKÈCÑPÔP�HØ#ð !Ù à $Ø'2Ð'PÐ6PÀDÐ6PÐ6PÐ6PÝ),¨X©¬Ý%(¨¬Ñ%9Ô%9ð	7ð 7•O N¨ N NÑ3Ñ3øõ !ð ð ð Ø‘HðøøøñK&ð'	øõP ð ð ð ØˆðøøøåÐsg   „B'I* Â,/IÃI* Ã$IÄ I* ÄIÄI* ÄDIÈI* È7IÉI* É
I$É I* É#I$É$I* É*
I7É6I7c                  ó:   — t           st          ¦   «          t           S )z@Return the current skill commands mapping (scan first if empty).)r   r—   rx   r|   r-   Úget_skill_commandsr™     s   € åð ÝÑÔÐÝÐr|   Úcommandc                 ód   — | sdS d|                       dd¦  «        › �}|t          ¦   «         v r|ndS )uV  Resolve a user-typed /command to its canonical skill_cmds key.

    Skills are always stored with hyphens â€” ``scan_skill_commands`` normalizes
    spaces and underscores to hyphens when building the key. Hyphens and
    underscores are treated interchangeably in user input: this matches
    ``_check_unavailable_skill`` and accommodates Telegram bot-command names
    (which disallow hyphens, so ``/claude-code`` is registered as
    ``/claude_code`` and comes back in the underscored form).

    Returns the matching ``/slug`` key from ``get_skill_commands()`` or
    ``None`` if no match.
    Nr   r<   r‚   )r‹   r™   )rš   Úcmd_keys     r-   Úresolve_skill_command_keyr�     sG   € ð ð ØˆtØ-�'—/’/ # sÑ+Ô+Ð-Ð-€GØÕ!3Ñ!5Ô!5Ð5Ð5ˆ7ˆ7¸4Ð?r|   rœ   c                 óÜ   — t          ¦   «         }|                     | ¦  «        }|sdS t          |d         |¬¦  «        }|sd|d         › d�S |\  }}}	d|	› d�}
t          |||
|||¬	¦  «        S )
aE  Build the user message content for a skill slash command invocation.

    Args:
        cmd_key: The command key including leading slash (e.g., "/gif-search").
        user_instruction: Optional text the user typed after the command.

    Returns:
        The formatted message string, or None if the skill wasn't found.
    Nr   ©r   z[Failed to load skill: r   r6   z&[IMPORTANT: The user has invoked the "zf" skill, indicating they want you to follow its instructions. The full skill content is loaded below.])rE   rF   rG   )r™   r$   r.   rp   )rœ   rE   r   rF   ÚcommandsÚ
skill_infoÚloadedr)   r   r*   rD   s              r-   Úbuild_skill_invocation_messager£   2  s¸   € õ "Ñ#Ô#€HØ—’˜gÑ&Ô&€JØð Øˆtå  ¨KÔ!8À'ÐJÑJÔJ€FØð ?Ø>¨°FÔ);Ð>Ð>Ð>Ð>à*0Ñ'€L�)˜Zð	S°ð 	Sð 	Sð 	Sð õ  ØØØØ)Ø!Øðñ ô ð r|   Úskill_identifiersc           	      ó¨  — g }g }g }t          ¦   «         }| D ]¤}|pd                     ¦   «         }|r||v rŒ|                     |¦  «         t          ||¬¦  «        }|s|                     |¦  «         Œ]|\  }	}
}d|› d�}|                     t          |	|
||¬¦  «        ¦  «         |                     |¦  «         Œ¥d                     |¦  «        ||fS )z‚Load one or more skills for session-wide CLI preloading.

    Returns (prompt_text, loaded_skill_names, missing_identifiers).
    r   rŸ   z9[IMPORTANT: The user launched this CLI session with the "z~" skill preloaded. Treat its instructions as active guidance for the duration of this session unless the user overrides them.])rG   z

)r„   r   r‰   r.   r9   rp   re   )r¤   r   Úprompt_partsÚloaded_namesÚmissingÚseenr&   Ú
identifierr¢   r)   r   r*   rD   s                r-   Úbuild_preloaded_skills_promptr«   Y  s(  € ð !€LØ €LØ€Gå‘U”U€DØ+ð (ð (ˆØ$Ð*¨×1Ò1Ñ3Ô3ˆ
Øð 	˜Z¨4Ð/Ð/ØØ�Š�ÑÔÐå$ Z¸ÐAÑAÔAˆØð 	Ø�NŠN˜:Ñ&Ô&Ð&Øà.4Ñ+ˆ�i ð7È
ð 7ð 7ð 7ð 	ð
 	×ÒÝ ØØØØ"ð	ñ ô ñ	
ô 	
ð 	
ð 	×Ò˜JÑ'Ô'Ð'Ð'à�;Š;�|Ñ$Ô$ l°GÐ;Ð;r|   )N)r   r   N)r   Nr   )'Ú__doc__r"   ÚloggingÚreÚpathlibr   Útypingr   r   r   Úhermes_constantsr   Úagent.skill_preprocessingr   r[   r	   rX   r
   rY   Ú	getLoggerÚ__name__Úloggerr   r   Ú__annotations__ÚcompilerŒ   rŽ   ÚtupleÚdictr.   r^   rC   rp   r—   r™   r�   r£   r«   rx   r|   r-   ú<module>rº      sR  ððð ð ð €€€Ø €€€Ø 	€	€	€	Ø Ð Ð Ð Ð Ð Ø &Ð &Ð &Ð &Ð &Ð &Ð &Ð &Ð &Ð &à 0Ð 0Ð 0Ð 0Ð 0Ð 0ðð ð ð ð ð ð ð ð ð ð 
ˆÔ	˜8Ñ	$Ô	$€à-/€��c˜4  S œ>Ð)Ô*Ð /Ð /Ñ /à!�r”z -Ñ0Ô0Ð Ø �b”j Ñ*Ô*Ð ð+/ð +/¨#ð +/¸¸d¹
ð +/ÈeÐTXÐY\Ð^aÐYaÔTbÐdhÐkoÑdoÐqtÐTtÔNuÐx|ÑN|ð +/ð +/ð +/ð +/ð\$ t¨C°¨H¤~ð $¸dÀ3¼ið $ÈDð $ð $ð $ð $ðV ØØ!ðdð dØ�s˜C�x”.ðdà�d‰{ðdð ðdð ð	dð
 ðdð �d‘
ðdð 	ðdð dð dð dðN>˜T # t¨C°¨H¤~Ð"5Ô6ð >ð >ð >ð >ðB˜D  d¨3°¨8¤nÐ!4Ô5ð ð ð ð ð@ sð @¨x¸¬}ð @ð @ð @ð @ð* ØØð	$ð $Øð$àð$ð �4‰Zð$ð ð	$ð
 ˆc„]ð$ð $ð $ð $ðR ð(<ð (<Ø˜C”yð(<à�4‰Zð(<ð ˆ3��S”	˜4 œ9Ð$Ô%ð(<ð (<ð (<ð (<ð (<ð (<r|   