
    i                          U d Z ddlZddlZddlmZmZ ddlZddlZddlm	Z	 ddl
mZ ddlmZmZmZmZmZmZ ddlmZmZ ddlmZ  ej        e          Z e            Zed	z  Zd
ZdZddddZ ej         d          Z! e"d          Z# e"h d          Z$da%dee&e&f         fdZ' G d de&e	          Z(g dZ)e*e+d<   ddZ,dee&ef         de-fdZ.dedee&         fdZ/dee&ef         deee&         ee&         f         fdZ0dee&ef         dee&ef         fdZ1	 ddee&ef         d ee&         dz  deee&ef                  fd!Z2d"e&d#eee&ef                  dee&ef         fd$Z3de-fd%Z4de&fd&Z5	 dd'e&d(ee&e&f         dz  de-fd)Z6dd*d+eee&ef                  d,ee&ef         d(ee&e&f         dz  dee&         fd-Z7de&fd.Z8	 dd/e(d0ee&         d1e&dz  de&dz  fd2Z9de-fd3Z:d4e&deee&ef         e&f         fd5Z;d6edee&         fd7Z<dee&         fd8Z=dee&         fd9Z>de&fd:Z?dd;e&d<e&de-fd=Z@d>d?d@e-deee&ef                  fdAZAd	eee&ef                  deee&ef                  fdBZBdCedee&         fdDZCddEe&dFe&de&fdGZDdHddIdJedKe&dLe&dMe-dNe&dz  de&fdOZE	 	 	 dd;e&dPe&dFe&dMe-de&f
dQZFedRk    ro	  eGdS            eGdT            eGdU            ejH         eD                      ZIeIdV         r eGdWeIdX          dY eJeIK                    dZg                      d[            eGd\eIK                    dZg                        eGd]           eId	         dd^         D ]MZLeLK                    dE          rd_eLdE          d`ndaZM eGdbeM eLd;          dceLdd         dde          df           Nn eGdgeIdh                      eGdi            ejH         eFdj                    ZIeIdV         r eGdkeId;                      eGdleIK                    dddm          ddn          df            eGdo eJeId4                    dp           eIK                    dq          r eGdreIdq                     n eGdgeIdh                      eGds            ejH         eFdjdt                    ZIeIdV         rP eGdueIdv                      eGdo eJeId4                    dp            eGdweId4         ddx          df           n eGdgeIdh                     dydzd{dEd|d}d~ig ddZNddd{d|dd~d|dd~dd;gddZO ejP        dyd	eNd e:d           d ZQ ejP        dd	eOeQe:d           dS )u  
Skills Tool Module

This module provides tools for listing and viewing skill documents.
Skills are organized as directories containing a SKILL.md file (the main instructions)
and optional supporting files like references, templates, and examples.

Inspired by Anthropic's Claude Skills system with progressive disclosure architecture:
- Metadata (name ≤64 chars, description ≤1024 chars) - shown in skills_list
- Full Instructions - loaded via skill_view when needed
- Linked Files (references, templates) - loaded on demand

Directory Structure:
    skills/
    ├── my-skill/
    │   ├── SKILL.md           # Main instructions (required)
    │   ├── references/        # Supporting documentation
    │   │   ├── api.md
    │   │   └── examples.md
    │   ├── templates/         # Templates for output
    │   │   └── template.md
    │   └── assets/            # Supplementary files (agentskills.io standard)
    └── category/              # Category folder for organization
        └── another-skill/
            └── SKILL.md

SKILL.md Format (YAML Frontmatter, agentskills.io compatible):
    ---
    name: skill-name              # Required, max 64 chars
    description: Brief description # Required, max 1024 chars
    version: 1.0.0                # Optional
    license: MIT                  # Optional (agentskills.io)
    platforms: [macos]            # Optional — restrict to specific OS platforms
                                  #   Valid: macos, linux, windows
                                  #   Omit to load on all platforms (default)
    prerequisites:                # Optional — legacy runtime requirements
      env_vars: [API_KEY]         #   Legacy env var names are normalized into
                                  #   required_environment_variables on load.
      commands: [curl, jq]        #   Command checks remain advisory only.
    compatibility: Requires X     # Optional (agentskills.io)
    metadata:                     # Optional, arbitrary key-value (agentskills.io)
      hermes:
        tags: [fine-tuning, llm]
        related_skills: [peft, lora]
    ---

    # Skill Title

    Full instructions and content here...

Available tools:
- skills_list: List skills with metadata (progressive disclosure tier 1)
- skill_view: Load full skill content (progressive disclosure tier 2-3)

Usage:
    from tools.skills_tool import skills_list, skill_view, check_skills_requirements

    # List all skills (returns metadata only - token efficient)
    result = skills_list()

    # View a skill's main content (loads full instructions)
    content = skill_view("axolotl")

    # View a reference file within a skill (loads linked file)
    content = skill_view("axolotl", "references/dataset-formats.md")
    N)get_hermes_homedisplay_hermes_home)Enum)Path)DictAnyListOptionalSetTuple)registry
tool_error)cfg_getskills@   i   darwinlinuxwin32)macosr   windowsz^[A-Za-z_][A-Za-z0-9_]*$)z.gitz.githubz.hubz.archive>   sshmodaldockerdaytonasingularityvercel_sandboxreturnc                     t                      dz  } i }|                                 s|S |                     d          5 }|D ]}|                                }|rn|                    d          sYd|v rU|                    d          \  }}}|                                                    d          ||                                <   	 ddd           n# 1 swxY w Y   |S )z@Load profile-scoped environment variables from HERMES_HOME/.env.z.envutf-8encoding#="'N)r   existsopenstrip
startswith	partition)env_pathenv_varsflinekey_values          6/home/ubuntu/.hermes/hermes-agent/tools/skills_tool.pyload_envr2   n   s4     6)H!H?? 		(	( CA 	C 	CD::<<D CDOOC00 CSD[[ $s 3 3Q(-(;(;E(B(B%		CC C C C C C C C C C C C C C C Os    B
CCCc                       e Zd ZdZdZdZdS )SkillReadinessStatus	availablesetup_neededunsupportedN)__name__
__module____qualname__	AVAILABLESETUP_NEEDEDUNSUPPORTED     r1   r4   r4   ~   s        I!LKKKr?   r4   )	zignore previous instructionszignore all previouszyou are nowzdisregard yourzforget your instructionsznew instructions:zsystem prompt:z<system>z]]>_INJECTION_PATTERNSc                 
    | a d S N)_secret_capture_callback)callbacks    r1   set_secret_capture_callbackrE      s    'r?   frontmatterc                 $    ddl m}  ||           S )u   Check if a skill is compatible with the current OS platform.

    Delegates to ``agent.skill_utils.skill_matches_platform`` — kept here
    as a public re-export so existing callers don't need updating.
    r   )skill_matches_platform)agent.skill_utilsrH   )rF   _impls     r1   rH   rH      s'     BAAAAA5r?   r0   c                 R    | sg S t          | t                    r| g} d | D             S )Nc                 n    g | ]2}t          |                                          #t          |          3S r>   strr'   ).0items     r1   
<listcomp>z2_normalize_prerequisite_values.<locals>.<listcomp>   s3    ===$3t99??+<+<=CII===r?   )
isinstancerN   )r0   s    r1   _normalize_prerequisite_valuesrS      s<     	% ==%====r?   c                     |                      d          }|rt          |t                    sg g fS t          |                     d                    t          |                     d                    fS )Nprerequisitesr+   commands)getrR   dictrS   )rF   prereqss     r1   _collect_prerequisite_valuesrZ      so     ooo..G *Wd33 2v&w{{:'>'>??&w{{:'>'>?? r?   c           	         |                      d          }t          |t                    sd g dS |                     d          }t          |t                    r5|                                r!t          |                                          nd }|                     d          }t          |t                    r|g}t          |t
                    sg }g }|D ]}t          |t                    st          |                     d          pd                                          }|sRt          |                     d          pd|                                           }t          |                     d	          p|                     d
          pd                                          }	||t          |                     dd                    d}
|	r|	|
d	<   |                    |
           ||dS )Nsetup)helpcollect_secretsr]   r^   env_var promptEnter value for provider_urlurlsecretT)r_   ra   re   )rW   rR   rX   rN   r'   listboolappend)rF   r\   	help_textnormalized_helpcollect_secrets_rawr^   rP   r_   ra   rc   entrys              r1   _normalize_setup_metadatarm      s   OOG$$EeT"" 5444		&!!I i%%	*3//*;*;	I   ))$566%t,, 423)400 ! ,.O# & &$%% 	dhhy))/R006688 	TXXh''G+Gg+G+GHHNNPP488N33LtxxL"MMSSUU 488Hd3344!
 !

  	1$0E.!u%%%%  *  r?   legacy_env_varsc                   	 t          |           	|                     d          }t          |t                    r|g}t          |t                    sg }g t                      dt          t          t          f         dd f	fd}|D ]E}t          |t                    r |d|i           %t          |t                    r ||           F	d         D ]_} ||                    d          |                    d          |                    d	          p	                    d
          d           `|t          |           \  }}|D ]} |d|i           S )Nrequired_environment_variablesrl   r   c                    t          |                     d          p|                     d          pd                                          }|r|v rd S t                              |          sd S |t          |                     d          pd|                                           d}|                     d          p>|                     d          p)|                     d	          p                    d          }t          |t                     r+|                                r|                                |d<   |                     d
          }t          |t                     r+|                                r|                                |d
<   |                     d          rd|d<                       |                               |           d S )Nnamer_   r`   ra   rb   )rr   ra   r]   rc   rd   required_foroptionalT)rN   rW   r'   _ENV_VAR_NAME_REmatchrR   addrh   )rl   env_name
normalizedri   rs   requiredseenr\   s        r1   _append_requiredz=_get_required_environment_variables.<locals>._append_required   s   uyy((FEIIi,@,@FBGGMMOO 	8t++F%%h// 	F %))H--N1NH1N1NOOUUWW&
 &

 IIf !yy((!yy! yy  	 	 i%% 	3)//*;*; 	3!*!2!2Jvyy00lC(( 	>\-?-?-A-A 	>)5););)=)=J~&99Z   	*%)Jz"
#####r?   rr   r^   r_   ra   rc   r]   )rr   ra   r]   )
rm   rW   rR   rX   rf   setr   rN   r   rZ   )
rF   rn   required_rawr|   rP   r/   r_   rz   r{   r\   s
          @@@r1   #_get_required_environment_variablesr      s    &k22E??#CDDL,%% &$~lD)) %'HUUD$S#X $4 $ $ $ $ $ $ $ $>  # #dC   	fd^,,,dD!! 	#T"""'( 
 
++((8,,00EEIIf4E4E 	
 	
 	
 	
 9+FF" , ,&'*++++Or?   
skill_namemissing_entriesc                    |sg dd dS d |D             }t                      r|dt                      dS t          |dd dS d}g }|D ]9}d| i}|                    d          r|d         |d<   |                    d          r|d         |d<   	 t          |d         |d         |          }nB# t          $ r5 t
                              d	|d          d
           d|d         dd
d}Y nw xY wt          |t                    o!t          |                    d                    }t          |t                    o!t          |                    d                    }	|r|	sd
}|
                    |d                    ;||d dS )NF)missing_namessetup_skippedgateway_setup_hintc                     g | ]
}|d          S rr   r>   )rO   rl   s     r1   rQ   z;_capture_required_environment_variables.<locals>.<listcomp>2  s    @@@uU6]@@@r?   r   r]   rs   rr   ra   z#Secret capture callback failed for Texc_info)success	stored_as	validatedskippedr   r   )_is_gateway_surface_gateway_setup_hintrC   rW   	ExceptionloggerwarningrR   rX   rg   rh   )
r   r   r   r   remaining_namesrl   metadatacallback_resultr   r   s
             r1   '_capture_required_environment_variablesr   '  s7     
""&
 
 	
 A@@@@M 
*""5"7"7
 
 	
  '*""&
 
 	
 M!#O  ". ". *-99V 	-$V}HV99^$$ 	=',^'<H^$	6fh OO
  		 		 		NNEeFmEEPT     !"6]"	 OOO			 _d33 
	**9
 9
 _d33 
	**9
 9
  	7 	uV}---- )&"  s   B..<C-,C-c                  j    t          j        d          rdS ddlm}  t	           | d                    S )NHERMES_GATEWAY_SESSIONTr   get_session_envHERMES_SESSION_PLATFORM)osgetenvgateway.session_contextr   rg   r   s    r1   r   r   o  sE    	y)** t777777 9::;;;r?   c                      t          t          j        dd                                                                                    pdS )NTERMINAL_ENVlocal)rN   r   r   r'   lowerr>   r?   r1   _get_terminal_backend_namer   v  s9    ry112288::@@BBMgMr?   var_nameenv_snapshotc                     |t                      }| |v r"t          |                    |                     S t          t          j        |                     S rB   )r2   rg   rW   r   r   )r   r   s     r1   _is_env_var_persistedr   z  sR     zz<L$$X..///	(##$$$r?   r   required_env_varscapture_resultc                    t          |d                   }|t                      }g }| D ]I}|d         }|                    d          r ||v st          ||          s|                    |           J|S )Nr   rr   rt   )r}   r2   rW   r   rh   )r   r   r   r   	remainingrl   rr   s          r1   %_remaining_required_environment_namesr     s     788MzzI" # #V}99Z   	=  (=dL(Q(Q T"""r?   c                  X    	 ddl m}  | S # t          $ r dt                       dcY S w xY w)Nr   *GATEWAY_SECRET_CAPTURE_UNSUPPORTED_MESSAGEziSecure secret entry is not available. Load this skill in the local CLI to be prompted, or add the key to z/.env manually.)gateway.platforms.baser   r   r   r   s    r1   r   r     s~    bUUUUUU99 b b b b  |O  |Q  |Q  b  b  b  	b  	b  	bbs   
 ))readiness_statusmissing
setup_helpc                 z    | t           j        k    r*|rd                    |          nd}d| d}|r| d| S |S d S )N, zrequired prerequisitesz.Setup needed before using this skill: missing . )r4   r<   join)r   r   r   missing_strnotes        r1   _build_setup_noter     se    
 /<<<,3Qdii(((9QNNNN 	*))Z)))4r?   c                      dS )zOSkills are always available -- the directory is created on first use if needed.Tr>   r>   r?   r1   check_skills_requirementsr     s    4r?   contentc                 $    ddl m}  ||           S )u   Parse YAML frontmatter from markdown content.

    Delegates to ``agent.skill_utils.parse_frontmatter`` — kept here
    as a public re-export so existing callers don't need updating.
    r   )parse_frontmatter)rI   r   )r   r   s     r1   _parse_frontmatterr     s'     433333W%%%r?   
skill_pathc                    t           g}	 ddlm} |                     |                       n# t          $ r Y nw xY w|D ]L}	 |                     |          }|j        }t          |          dk    r
|d         c S =# t          $ r Y Iw xY wdS )z
    Extract category from skill path based on directory structure.

    For paths like: ~/.hermes/skills/mlops/axolotl/SKILL.md -> "mlops"
    Also works for external skill dirs configured via skills.external_dirs.
    r   get_external_skills_dirs   N)	
SKILLS_DIRrI   r   extendr   relative_topartslen
ValueError)r   dirs_to_checkr   
skills_dirrel_pathr   s         r1   _get_category_from_pathr     s      LM>>>>>>55778888   #  
	!--j99HNE5zzQQx  	 	 	H	4s   #. 
;;6A==
B
	B
c                 4   | sg S t          | t                    rd | D             S t          |                                           } |                     d          r|                     d          r
| dd         } d |                     d          D             S )uB  
    Parse tags from frontmatter value.

    Handles:
    - Already-parsed list (from yaml.safe_load): [tag1, tag2]
    - String with brackets: "[tag1, tag2]"
    - Comma-separated string: "tag1, tag2"

    Args:
        tags_value: Raw tags value — may be a list or string

    Returns:
        List of tag strings
    c                 T    g | ]%}|t          |                                          &S r>   rM   rO   ts     r1   rQ   z_parse_tags.<locals>.<listcomp>  s+    8881a8A888r?   []   c                     g | ]=}|                                 |                                                      d           >S )r$   )r'   r   s     r1   rQ   z_parse_tags.<locals>.<listcomp>  s9    OOOqQWWYYOAGGIIOOE""OOOr?   ,)rR   rf   rN   r'   r(   endswithsplit)
tags_values    r1   _parse_tagsr     s      	 *d## 988
8888 Z&&((JS!! &j&9&9#&>&> &"%
OOJ,<,<S,A,AOOOOr?   c                  "    ddl m}   |             S )u   Load disabled skill names from config.

    Delegates to ``agent.skill_utils.get_disabled_skill_names`` — kept here
    as a public re-export so existing callers don't need updating.
    r   get_disabled_skill_names)rI   r   r   s    r1   _get_disabled_skill_namesr     s%     ;:::::##%%%r?   c                  L    	 ddl m}   | d          pdS # t          $ r Y dS w xY w)zResolve the current platform from gateway session context.

    Mirrors the platform-resolution logic in
    ``agent.skill_utils.get_disabled_skill_names`` so that
    ``_is_skill_disabled`` respects ``HERMES_SESSION_PLATFORM``.
    r   r   r   r`   )r   r   r   r   s    r1   _get_session_platformr     sQ    ;;;;;;899?R?   rrs    
##rr   platformc                    	 ddl m}  |            }|                    di           }|p!t          j        d          pt                      }|rt          |d|          }|| |v S | |                    dg           v S # t          $ r Y dS w xY w)	a  Check if a skill is disabled in config.

    Resolves the active platform from (in order of precedence):
    1. Explicit ``platform`` argument
    2. ``HERMES_PLATFORM`` environment variable
    3. ``HERMES_SESSION_PLATFORM`` from gateway session context
    r   )load_configr   HERMES_PLATFORMplatform_disabledNdisabledF)hermes_cli.configr   rW   r   r   r   r   r   )rr   r   r   config
skills_cfgresolved_platformr   s          r1   _is_skill_disabledr     s    111111ZZ"--
$_	2C(D(D_H]H_H_ 	1 '
4GIZ [ [ ,000z~~j"5555   uus   A"A= %A= =
B
BF)skip_disabledr   c           	         ddl m}m} g }t                      }| rt                      nt	                      }g }t
                                          r|                    t
                     |                     |                       |D ]} ||d          D ]}t          d |j
        D                       r"|j        }		 |                    d          dd         }
t          |
          \  }}t          |          sj|                    d	|	j                  dt"                   }||v r||v r|                    d
d          }|sY|                                                    d          D ]1}|                                }|r|                    d          s|} n2t+          |          t,          k    r|dt,          dz
           dz   }t/          |          }|                    |           |                    |||d           y# t2          t4          f$ r'}t6                              d||           Y d}~d}~wt:          $ r)}t6                              d||d           Y d}~d}~ww xY w|S )a^  Recursively find all skills in ~/.hermes/skills/ and external dirs.

    Args:
        skip_disabled: If True, return ALL skills regardless of disabled
            state (used by ``hermes skills`` config UI). Default False
            filters out disabled skills.

    Returns:
        List of skill metadata dicts (name, description, category).
    r   )r   iter_skill_index_filesSKILL.mdc              3   (   K   | ]}|t           v V  d S rB   )_EXCLUDED_SKILL_DIRS)rO   parts     r1   	<genexpr>z#_find_all_skills.<locals>.<genexpr>@  s(      KKD4//KKKKKKr?   r   r    Ni  rr   descriptionr`   
r"   r   ...)rr   r   categoryz Failed to read skill file %s: %sz)Skipping skill at %s: failed to parse: %sTr   )rI   r   r   r}   r   r   r%   rh   r   anyr   parent	read_textr   rH   rW   rr   MAX_NAME_LENGTHr'   r   r(   r   MAX_DESCRIPTION_LENGTHr   rw   UnicodeDecodeErrorPermissionErrorr   debugr   )r   r   r   r   
seen_namesr   dirs_to_scanscan_dirskill_md	skill_dirr   rF   bodyrr   r   r-   r   es                     r1   _find_all_skillsr  %  s    SRRRRRRRFeeJ &Fsuuu+D+F+FH L (J'''0022333  / /..xDD .	 .	HKKHNKKKKK  I(",,g,>>uuE$6w$?$?!T-k:: "vy~>>?O?OP:%%8##)oomR@@" " $

 2 24 8 8 " "#zz|| "(<(< "*.K!E{##&<<<"-.I/E/I.I"JU"RK28<<t$$$ #. (      '8   ?1MMM   ?1W[     	U.	` Ms7   ?H,H1H6CHI<$II<I77I<c                 &    t          | d           S )z3Keep every skill listing path ordered the same way.c                 @    |                      d          pd| d         fS )Nr   r`   rr   rW   )ss    r1   <lambda>z_sort_skills.<locals>.<lambda>t  s     z):):)@b!F)(L r?   )r.   )sorted)r   s    r1   _sort_skillsr  r  s    &LLMMMMr?   category_dirc                    | dz  }|                                 sdS 	 |                    d          }t          |          \  }}|                    dd          }|sY|                                                    d          D ]1}|                                }|r|                    d          s|} n2t          |          t          k    r|dt          d	z
           d
z   }|r|ndS # t          t          f$ r'}t                              d||           Y d}~dS d}~wt          $ r)}t                              d||d           Y d}~dS d}~ww xY w)z
    Load category description from DESCRIPTION.md if it exists.

    Args:
        category_dir: Path to the category directory

    Returns:
        Description string or None if not found
    zDESCRIPTION.mdNr   r    r   r`   r   r"   r   r   z*Failed to read category description %s: %sz)Error parsing category description %s: %sTr   )r%   r   r   rW   r'   r   r(   r   r  r  r  r   r  r   r   )r  	desc_filer   rF   r
  r   r-   r  s           r1   _load_category_descriptionr  w  s    //I t%%w%77.w77T "oomR88 	

**400  zz||  4 4 "&KE {444%&B(>(B&BCeKK)3{{t30   A9aPPPttttt   7APT 	 	
 	
 	
 ttttt	s$   CC) )E:DE)EEr   task_idc                 b    	 t                                           sGt                               dd           t          j        dg g dt                       ddd          S t                      }|st          j        dg g ddd          S  r fd	|D             }t          |          }t          t          d
 |D                                 }t          j        d||t          |          ddd          S # t          $ r(}t          t          |          d          cY d}~S d}~ww xY w)a  
    List all available skills (progressive disclosure tier 1 - minimal metadata).

    Returns only name + description to minimize token usage. Use skill_view() to
    load full content, tags, related files, etc.

    Args:
        category: Optional category filter (e.g., "mlops")
        task_id: Optional task identifier used to probe the active backend

    Returns:
        JSON string with minimal skill info: name, description, category
    T)parentsexist_okz-No skills found. Skills directory created at z/skills/)r   r   
categoriesmessageFensure_asciiz%No skills found in skills/ directory.c                 F    g | ]}|                     d           k    |S )r   r  )rO   r  r   s     r1   rQ   zskills_list.<locals>.<listcomp>  s/    QQQ1553D3D3P3P!3P3P3Pr?   c              3   j   K   | ].}|                     d           |                     d           V  /dS )r   Nr  rO   r  s     r1   r   zskills_list.<locals>.<genexpr>  s?      KKaz9J9JKj!!KKKKKKr?   z@Use skill_view(name) to see full content, tags, and linked files)r   r   r  counthintr   N)r   r%   mkdirjsondumpsr   r  r  r  r}   r   r   r   rN   )r   r  
all_skillsr  r  s   `    r1   skills_listr*    s   31  "" 
	TD999:# "$nObOdOdnnn	  #    &''
 		:# "$F	  #     	RQQQQZQQQJ "*--
 KK:KKKKK
 

 z$(ZZ  	
 	
 	
 		
  1 1 1#a&&%0000000001s+   AC< #*C< A-C< <
D.D)#D.)D.T
preprocess
session_idr  	namespacebarer,  r-  c          
         ddl m}m} | |            v rt          j        dd| d| dd          S 	 |                     d	          n9# t          $ r,}t          j        dd
| d d| dd          cY d}~S d}~ww xY wi }	 t                    \  }}	n# t          $ r Y nw xY wt          |          s0t          j        dd| d dt          j
        j        dd          S t          fdt          D                       rt                              d|           t!          |                    dd                    }
t%          |
          t&          k    r|
dt&          dz
           dz   }
	 fd |                                |          D             }|r+d                    |          }d| d| d| d|d          d	}nd| d}n# t          $ r d}Y nw xY w}|rI	 ddlm}  || j        |          }n.# t          $ r! t                              d |d!"           Y nw xY wt          j        d!| d |r| | n||
dt          j        j        d#d          S )$z8Read a plugin-provided skill, apply guards, return JSON.r   )_get_disabled_pluginsget_plugin_managerFzPlugin 'z5' is disabled. Re-enable with: hermes plugins enable r   errorr  r   r    Failed to read skill ':': NSkill '$' is not supported on this platform.r   r4  r   c              3   D   K   | ]}|                                 v V  d S rB   )r   )rO   pr   s     r1   r   z&_serve_plugin_skill.<locals>.<genexpr>  s0      
=
=A1
=
=
=
=
=
=r?   zIPlugin skill '%s:%s' contains patterns that may indicate prompt injectionr   r`   r   r   c                      g | ]
}|k    |S r>   r>   )rO   r  r/  s     r1   rQ   z'_serve_plugin_skill.<locals>.<listcomp>%  s*     
 
 
Dyy yyr?   r   z,[Bundle context: This skill is part of the 'z' plugin.
Sibling skills: z..
Use qualified form to invoke siblings (e.g. z).]

z' plugin.]

preprocess_skill_contentr-  z'Could not preprocess plugin skill %s:%sTr   )r   rr   r   r   linked_filesr   )hermes_cli.pluginsr1  r2  r'  r(  r   r   r   rH   r4   r=   r0   r   r@   r   r   rN   rW   r   r  list_plugin_skillsr   agent.skill_preprocessingr?  r   r  r;   )r  r.  r/  r,  r-  r1  r2  r  parsed_frontmatterr/   r   siblingssib_listbannerrendered_contentr?  r   s     `             @r1   _serve_plugin_skillrJ    s7    MLLLLLLL))++++z Iy I I=FI I  	
 	
 	
 		

$$g$66 
 
 
z(Y(Y(YT(Y(YVW(Y(YZZ
 
 
 	
 	
 	
 	
 	
 	

 *, 27 ; ;AA    ""455 
z Y9YYtYYY$8$D$J 
 
 
 
 	
 
=
=
=
=)<
=
=
=== 
Wt	
 	
 	

 (,,]B??@@K
;000!">$:Q$>">?%G
 
 
 
))++>>yII
 
 
  	^yy**H`y ` `#+` `?H` `KSTU;` ` ` F ^I]]]F     	JJJJJJ77%     
  	 	 	LL99dUY      	
 : ))4))8>T&4"2444DT&  4 > D	
 	
 
 
 
 
sT   A 
B!A?9B?B
B 
B*)B*9AG G%$G%-H (H21H2	file_pathc                   GHIJK 	 d| v r*ddl m}m} ddlm}m}  ||           \  I} |I          s t          j        ddI d|  dd	d
          S  |              |            }	|	                    |           }
|
]|
	                                s5|	
                    |            t          j        dd|  d|
 dd	d
          S t          |
I|||          S |	                    I          }|rBt          j        dd| dI dIfd|D             dI dt          |           ddd
          S ddl m} g }t          	                                r|                    t                     |                     |                       |st          j        ddd	d
          S dKd}|D ]y}|| z  }|                                r |dz  	                                r	|K|dz  } n?|                    d          	                                r|                    d          } nz|s9|D ]6}ddl m}  ||d          D ]}|j        j        | k    r|j        K|} n|r n7|s3|D ]0}|                    |  d          D ]}|j        dk    r|} n|r n1|r|	                                sLd t1          t3                                dd         D             }t          j        dd|  d|ddd
          S 	 |                    d !          }n6# t6          $ r)}t          j        dd"|  d#| d	d
          cY d}~S d}~ww xY wd$}t                                          g}	 |                    d% |d&d         D                        n# t6          $ r Y nw xY w|D ]=}	 |                                                    |           d} n# t<          $ r Y :w xY w|                                GtA          Gfd'tB          D                       }|s|rtg }|r|                    d(|            |r|                    d)           tE          j#        tH                    %                    d*| d+&                    |                     i }	 tO          |          \  }}n# t6          $ r i }Y nw xY wtQ          |          s-t          j        dd|  d,tR          j*        j+        d-d
          S |,                    d.|j        j                  }t[          |          rt          j        dd| d/d	d
          S |rKrdd0l.m/}m0}  ||          rt          j        dd1d2d3d
          S K|z  }  || K          }!|!rt          j        d|!d2d3d
          S | 	                                sg g g g g d4}"K                    d5          D ]/}#|#1                                r|#j        dk    rte          |#                    K                    }$|$3                    d6          r|"d7                             |$           w|$3                    d8          r|"d9                             |$           |$3                    d:          r|"d;                             |$           |$3                    d<          r|"d=                             |$           |#j4        d>v r|"d?                             |$           1d@ |"5                                D             }"t          j        ddA| dB|  d|"dCdDd
          S 	 |                     d !          }nO# tl          $ rB t          j        d$| |dE| j         dF| 7                                j8         dGd$dHd
          cY S w xY wt          j        d$| ||| j4        dId
          S |}%g }&g }'g }(g })KrXKd7z  }*|*	                                r!KfdJ|*9                    dK          D             }&Kd9z  }+|+	                                r9dLD ]6},|'                    KfdM|+                    |,          D                        7Kd;z  }-|-	                                ra|-                    d5          D ]K}#|#1                                r5|(                    te          |#                    K                               LKd=z  }.|.	                                r9dND ]6},|)                    KfdO|.9                    |,          D                        7i }/|%,                    dP          }0tu          |0tv                    r|0,                    dQi           pi }/ty          |/,                    dR          p|%,                    dRdS                    }1ty          |/,                    dT          p|%,                    dTdS                    }2i }3|&r|&|3d7<   |'r|'|3d9<   |(r|(|3d;<   |)r|)|3d=<   	 te          |                    t                              }4nO# t<          $ rB |j        j        r,te          |                    |j        j                            n|j        }4Y nw xY w|%,                    d.Ks|j=        nKj                  }5t}          |%          \  }6}t          |%|6          }7t                      }8t                      HHfdU|7D             }9t          |5|9          }:|9rt                      Ht          |7|:HV          Jt          J          };JfdW|7D             }<|<r@	 ddXlEmF}=  |=|<           n-# t6          $ r  t          H                    dY|5d$Z           Y nw xY w|%,                    d[g           }>tu          |>t                    sg }>g }?|>rD	 dd\lJmK}@  |@|>          }?|?rd$};n-# t6          $ r  t          H                    d]|5d$Z           Y nw xY w|}A|rC	 dd^lLmM}B  |B|K|_          }An-# t6          $ r  t          H                    d`|5d$Z           Y nw xY wi dad$d.|5db|%,                    dbdS          dR|1dT|2dc|Add|4deKrte          K          nddf|3r|3nddg|3rdhnddi|7djg dkJdl|?dmg dn|;do|:do         dp|;rtR          jN        j+        ntR          jO        j+        i}Ct          dq |7D             d          }D|Dr|D|Cdr<   |:ds         r|:ds         |Cds<   |;r`dt JD             du |?D             z   }Et          tR          jN        |E|D          }F|8t          v r|Fr|F dv|8S                                 dw}F|Fr|F|Cdx<   |%,                    dy          r|%dy         |Cdy<   tu          |0tv                    r|0|CdP<   t          j        |Cd
          S # t6          $ r(}t          te          |          dz          cY d}~S d}~ww xY w){a  
    View the content of a skill or a specific file within a skill directory.

    Args:
        name: Name or path of the skill (e.g., "axolotl" or "03-fine-tuning/axolotl").
            Qualified names like "plugin:skill" resolve to plugin-provided skills.
        file_path: Optional path to a specific file within the skill (e.g., "references/api.md")
        task_id: Optional task identifier used to probe the active backend
        preprocess: Apply configured SKILL.md template and inline shell rendering
            to main skill content. Internal slash/preload callers disable this
            because they render the skill message themselves.

    Returns:
        JSON string with skill content or error message
    r6  r   )is_valid_namespaceparse_qualified_name)discover_pluginsr2  FzInvalid namespace 'z' in 'z('. Namespaces must match [a-zA-Z0-9_-]+.r3  r  Nr8  z' file no longer exists at uT   . The registry entry has been cleaned up — try again after the plugin is reloaded.r+  z' not found in plugin 'z'.c                     g | ]	} d | 
S )r6  r>   )rO   r  r.  s     r1   rQ   zskill_view.<locals>.<listcomp>  s'    ,S,S,SA	-?-?A-?-?,S,S,Sr?   zThe 'z' plugin provides z
 skill(s).)r   r4  available_skillsr$  r   zISkills directory does not exist yet. It will be created on first install.r   .md)r   c                     g | ]
}|d          S r   r>   r"  s     r1   rQ   zskill_view.<locals>.<listcomp>  s    RRRq6RRRr?      z' not found.z+Use skills_list to see all available skillsr   r    r5  r7  Tc              3   >   K   | ]}|                                 V  d S rB   )resolve)rO   ds     r1   r   zskill_view.<locals>.<genexpr>  s*       C C C C C C C Cr?   r   c              3       K   | ]}|v V  	d S rB   r>   )rO   r<  _content_lowers     r1   r   zskill_view.<locals>.<genexpr>  s(      !S!S!!~"5!S!S!S!S!S!Sr?   zHskill file is outside the trusted skills directory (~/.hermes/skills/): zBskill content contains patterns that may indicate prompt injectionz#Skill security warning for '%s': %sz; r9  r:  rr   zT' is disabled. Enable it with `hermes skills` or inspect the files directly on disk.)validate_within_dirhas_traversal_componentz%Path traversal ('..') is not allowed.z.Use a relative path within the skill directory)r   r4  r$  )
references	templatesassetsscriptsother*zreferences/r\  z
templates/r]  zassets/r^  zscripts/r_  )rR  z.pyz.yamlz.ymlz.jsonz.texz.shr`  c                     i | ]
\  }}|||S r>   r>   )rO   kvs      r1   
<dictcomp>zskill_view.<locals>.<dictcomp>n  s#    "Q"Q"QDAqq"Q1a"Q"Q"Qr?   zFile 'z' not found in skill 'z0Use one of the available file paths listed above)r   r4  available_filesr$  z[Binary file: z, size: z bytes])r   rr   filer   	is_binary)r   rr   rg  r   	file_typec                 T    g | ]$}t          |                                        %S r>   rN   r   rO   r,   r	  s     r1   rQ   zskill_view.<locals>.<listcomp>  s:     # # #67Ci0011# # #r?   *.md)rm  *.pyz*.yamlz*.ymlz*.jsonz*.tex*.shc                 T    g | ]$}t          |                                        %S r>   rk  rl  s     r1   rQ   zskill_view.<locals>.<listcomp>  s=        !  i 8 899  r?   )rn  ro  z*.bashz*.jsz*.tsz*.rbc                 T    g | ]$}t          |                                        %S r>   rk  rl  s     r1   rQ   zskill_view.<locals>.<listcomp>  s-    VVV1Q]]95566VVVr?   r   hermestagsr`   related_skillsc                 j    g | ]/}|                     d           t          |d                   -|0S )rt   rr   )rW   r   )rO   r  r   s     r1   rQ   zskill_view.<locals>.<listcomp>  sS     %
 %
 %
55$$%
 *!F)\BB	%
%
 %
 %
r?   r   c                 4    g | ]}|d          v|d          S r   r>   )rO   r  remaining_missing_required_envss     r1   rQ   zskill_view.<locals>.<listcomp>   s7     
 
 
y ??? fI???r?   )register_env_passthroughz/Could not register env passthrough for skill %sr   required_credential_files)register_credential_filesz0Could not register credential files for skill %sr>  r@  z)Could not preprocess skill content for %sr   r   r   pathr	  rA  
usage_hintzzTo view linked files, call skill_view(name, file_path) where file_path is e.g. 'references/api.md' or 'assets/config.yaml'rp   required_commands&missing_required_environment_variablesmissing_credential_filesmissing_required_commandsr6   r   r   c              3   P   K   | ]!}|                     d           |d          V  "dS )r]   Nr  )rO   r  s     r1   r   zskill_view.<locals>.<genexpr>N  s5      QQ155==Q1V9QQQQQQr?   r   r   c                     g | ]}d | S )zenv $r>   )rO   rx   s     r1   rQ   zskill_view.<locals>.<listcomp>V  s.       '/"""  r?   c                     g | ]}d | S )zfile r>   )rO   r{  s     r1   rQ   zskill_view.<locals>.<listcomp>X  s+       #'  r?   r   zW-backed skills need these requirements available inside the remote environment as well.
setup_notecompatibilityr%  )UrI   rM  rN  rB  rO  r2  r'  r(  find_plugin_skillr%   remove_plugin_skillrJ  rC  r   r   r   rh   r   is_dirwith_suffixr   r   rr   rglobr  r  r   r   rV  r   r   r   r   r@   logging	getLoggerr8   r   r   r   rH   r4   r=   r0   rW   r   tools.path_securityrZ  r[  is_filerN   r(   suffixitemsr  statst_sizeglobrR   rX   r   stemrZ   r   r   r2   r   r   rg   tools.env_passthroughrx  r   r  rf   tools.credential_filesrz  rD  r?  r<   r;   nextr   _REMOTE_ENV_BACKENDSupperr   )Lrr   rK  r  r,  rM  rN  rO  r2  r/  pmplugin_skill_mdr5   r   all_dirsr  
search_dirdirect_pathr   found_skill_mdfound_mdr   r  _outside_skills_dir_trusted_dirs_td_injection_detected	_warningsrE  r/   resolved_namerZ  r[  target_filetraversal_errorrf  r,   relrF   reference_filestemplate_filesasset_filesscript_filesreferences_dirtemplates_dirext
assets_dirscripts_dirhermes_metar   rs  rt  rA  r   r   rn   r   backendmissing_required_env_varsr   r6   available_env_namesrx  required_cred_files_rawmissing_cred_filesrz  rI  r?  resultr   missing_itemsr  rY  r   r.  rw  r	  sL                                                                          @@@@@r1   
skill_viewr  Q  s   *H1 $;;RRRRRRRROOOOOOOO22488OIt%%i00 
z#(E) E E4 E E E  "'	 	 	 	 ##%%B 22488O*&--// **4000:',!7$ !7 !7#2!7 !7 !7  &+    +#)&    --i88I 	z#(!U4!U!U	!U!U!U,S,S,S,S,S,S,S _	 _ _S^^ _ _ _	  "'    	?>>>>>  	(OOJ'''0022333 	:$h  #    	 # 		 		J$t+K!!## z)A(I(I(K(K '	&3((//6688 &22599
  
	& 	 	
DDDDDD&<&<Z&T&T  N%,1T99$2$9	#1 :  E  	&  
 * 0 0D > >  H}
22#+ 3  E  
	x00 
	RRL9I9K9K,L,LSbS,QRRRI:$9t999(1I	  #   		(('(::GG 	 	 	:$BdBBqBB  #        	 ##++--.	   C Chqrrl C C CCCCC 	 	 	D	  	 	C  ""..s333&+#   
 !!!S!S!S!S?R!S!S!SSS 	s"5 	sI" x  !vlt!v!vwww" g  !efffh''//0UW[]a]f]fgp]q]qrrr-/	$$6w$?$?! 	$ 	$ 	$!#	$ &&899 	:$QtQQQ(<(H(N 
 #    +..vx7KLLm,, 
	:$`- ` ` `  #	 	 	 	  b	 b	XXXXXXXX '&y11 z#(!H P 
 "'    $i/K 21+yIIO z#(!0 P 
 "'    %%'' , #%!# !# # #-- A AAyy{{ Aqv';';!!--	":":;;>>-88 A+L9@@EEEE ^^L99 A+K8??DDDD ^^I66 A+H5<<SAAAA ^^J77 A+I6==cBBBBX *   ,G4;;C@@@ #R"QO4I4I4K4K"Q"Q"Qz#(!S)!S!S4!S!S!S+: R	  "'   %///AA%   z#' $ )#qK4D#q#qkN^N^N`N`Nh#q#q#q%)  "'	 	 	 	 	 	 :# %&!,!3  #	 	 	 	 )  %	&5N$$&& # # # #;I;N;Nv;V;V# # # &3M##%%   C #))   %2%8%8%=%=      #X-J  "" J#))#.. J JAyy{{ J#**3q}}Y/G/G+H+HIII#i/K!!## M  C ''VVVV@P@PQT@U@UVVV    ??:..h%% 	;",,x44:K;??622Qkoofb6Q6QRR$OO,--VAQSU1V1V
 

  	9)8L& 	7(6L% 	1%0L" 	3&2L#	v8//
;;<<HH 	v 	v 	vLTOLbus8//0FGGHHHhphuHHH	v !__FHMM	
 

 :+FF?
 
 -..zz%
 %
 %
 %
&%
 %
 %
! A%
 
 % 	&#::L*O%+
 +
 +
'
 ;<<

 
 
 
&
 
 

  
		JJJJJJ(()<====   E!       #.//2Mr"R"R1488 	)&(##%" 	LLLLLL%>%>?V%W%W"% (#'L   F!       # 	NNNNNN#;#;&$ $ $  
    ?VZ      

t
J
 ;??="==
 D	

 n
 '
 H
 9>Y$
 LBLLd
   W  W
 -.?
  
 56U
  '(:!
" (#
$ L%
& ^O<'
( !6 4 A G G%/5-
 
2 QQ.?QQQSWXX
 	.#-F< ./ 	P+9:N+OF'( 	2 3R   +=  M
 +$1 J
 ...:. *  V  VW]]__  V  V  V
 2'1|$ ???++ 	C&1/&BF?#h%% 	*!)F:z&u5555 1 1 1#a&&%0000000001s  Av% A3v% 	v% Av% 6A2v% )Ev% <L v% 
MM;M<v% Mv% &'N v% 
Nv% Nv% #)Ov% 
Ov% OB1v% R  v%  R/,v% .R//>v% .Av% :2v% -,v% F"v% >] v% A	^!v%  ^!!#v% H7v% ='h% $v% %A	i1.v% 0i11Cv% 6m v% 'm2/v% 1m224v% 'n= <v% ='o'$v% &o''v% /p v% 'p.+v% -p..E6v% %
w/www__main__u   🎯 Skills Tool Testz<============================================================u   
📋 Listing all skills:r   zFound r#  z skills in r  z categorieszCategories: z
First 10 skills:
   r   z] r`   u     • z: r   <   r   zError: r4  u   
📖 Viewing skill 'axolotl':axolotlzName: zDescription: zN/Ad   zContent length: z charsrA  zLinked files: uE   
📄 Viewing reference file 'axolotl/references/dataset-formats.md':zreferences/dataset-formats.mdzFile: rg  z	Preview:    r*  zVList available skills (name + description). Use skill_view(name) to load full content.objectstringz*Optional category filter to narrow results)typer   )r  
propertiesrz   )rr   r   
parametersr  ah  Skills allow for loading information about specific tasks and workflows, as well as scripts and templates. Load a skill's full content or access its linked files (references, templates, scripts). First call returns SKILL.md content plus a 'linked_files' dict showing available references/templates/scripts. To access those, call again with file_path parameter.zThe skill name (use skills_list to see available skills). For plugin-provided skills, use the qualified form 'plugin:skill' (e.g. 'superpowers:writing-plans').zOPTIONAL: Path to a linked file within the skill (e.g., 'references/api.md', 'templates/config.yaml', 'scripts/validate.py'). Omit to get the main SKILL.md content.)rr   rK  c                 p    t          |                     d          |                    d                    S )Nr   r  )r   r  )r*  rW   )argskws     r1   r  r    s2    {*%%rvvi/@/@      r?   u   📚)rr   toolsetschemahandlercheck_fnemojic                    |                      dd          }t          ||                      d          |                     d                    }	 t          j        |          }t	          |t
                    rf|                     d          rQ|                     d          p|}|r8ddlm}m}  |t          |                      |t          |                     n# t          $ r Y nw xY w|S )	ztInvoke skill_view, then bump view_count on success. Best-effort: a
    telemetry failure never breaks the tool call.rr   r`   rK  r  )rK  r  r   r   )bump_use	bump_view)rW   r  r'  loadsrR   rX   tools.skill_usager  r  rN   r   )r  r  rr   r  parsedresolvedr  r  s           r1   _skill_view_with_bumpr    s    88FBD--rvvi7H7H  FF##fd## 
	(

9(=(= 
	( zz&))1TH (AAAAAAAA	#h--((( X'''   Ms   BC   
C-,C-)r   NrB   )NN)NNT)R__doc__r'  r  hermes_constantsr   r   r   reenumr   pathlibr   typingr   r   r	   r
   r   r   tools.registryr   r   r   r   r  r8   r   HERMES_HOMEr   r   r  _PLATFORM_MAPcompileru   	frozensetr   r  rC   rN   r2   r4   r@   rf   __annotations__rE   rg   rH   rS   rZ   rm   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r*  rJ  r  printr  r  r   rW   skillcatSKILLS_LIST_SCHEMASKILL_VIEW_SCHEMAregisterr  r>   r?   r1   <module>r     s  A A AF   A A A A A A A A 				 				             8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 / / / / / / / / % % % % % %		8	$	$ o8#
  
  
 2:9::  y!HII  yJJJ     $sCx.              3      
 
 
 T 
 
 
( ( ( (
S#X 4    ># >$s) > > > >	c3h	
49d3i 	 	 	 	*4S> *d38n * * * *^ )-B Bc3hB#Y%B 
$sCx.B B B BJEE$sCx.)E 
#s(^E E E EP<T < < < <NC N N N N
 :>% %%!%c3h$!6%	% % % % +/	  DcN+cN sCx.4'	
 
#Y   (bS b b b b " *#Y d
 	4Z	   4    
& &d38nc.A(B & & & & #    4PtCy P P P P>&3s8 & & & &s     S C 4    , /4 J J Jt JT#s(^8L J J J JZNd38n- N$tCH~2F N N N N
(T (hsm ( ( ( (VA1 A1# A1s A1c A1 A1 A1 A1X !e e eee e
 e d
e 	e e e eT 	]1 ]1
]1]1 ]1 	]1
 	]1 ]1 ]1 ]1D z	E
!"""	E(OOO 
E
&'''TZ&&Fi 
+_VG___VZZb5Q5Q1R1R___	
 	
 	
 	;VZZb99;;<<<"###H%crc* 	Q 	QE/4yy/D/DL+eJ'++++"CEO3OfOO}1Ecrc1JOOOPPPP	Q 	)w))*** 
E
+,,,TZ

9--..Fi +'vf~''(((Ifjj>>ttDIIIJJJ?VI%6!7!7???@@@::n%% 	=E;6.#9;;<<<)w))*** 
E
RSSSTZ

9.MNNOOFi +'vf~''(((?VI%6!7!7???@@@6&+DSD16667777)w))*** k K 
 	 	     ~ !  A 
 !  F 	
 	
 H   &  	  '
	 	 	 	  2  	!&
     r?   