
    PL
ju                        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 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Z0dedee(         fdZ1dee(ef         deee(         ee(         f         fdZ2dee(ef         dee(ef         fd Z3	 ddee(ef         d!ee(         dz  deee(ef                  fd"Z4d#e(d$eee(ef                  dee(ef         fd%Z5de/fd&Z6de(fd'Z7	 dd(e(d)ee(e(f         dz  de/fd*Z8dd+d,eee(ef                  d-ee(ef         d)ee(e(f         dz  dee(         fd.Z9de(fd/Z:	 dd0e*d1ee(         d2e(dz  de(dz  fd3Z;de/fd4Z<d5e(deee(ef         e(f         fd6Z=d7edee(         fd8Z>dee(         fd9Z?dee(         fd:Z@de(fd;ZAdd<e(d=e(de/fd>ZBd?d@dAe/deee(ef                  fdBZCd
eee(ef                  deee(ef                  fdCZDdDedee(         fdEZEddFe(dGe(de(fdHZFdIddJdKedLe(dMe(dNe/dOe(dz  de(fdPZG	 	 	 dd<e(dQe(dGe(dNe/de(f
dRZHedSk    ro	  eIdT            eIdU            eIdV            ejJ         eF                      ZKeKdW         r eIdXeKdY          dZ eLeKM                    d[g                      d\            eId]eKM                    d[g                        eId^           eKd
         dd_         D ]MZNeNM                    dF          rd`eNdF          dandbZO eIdceO eNd<          ddeNde         ddf          dg           Nn eIdheKdi                      eIdj            ejJ         eHdk                    ZKeKdW         r eIdleKd<                      eIdmeKM                    dedn          ddo          dg            eIdp eLeKd5                    dq           eKM                    dr          r eIdseKdr                     n eIdheKdi                      eIdt            ejJ         eHdkdu                    ZKeKdW         rP eIdveKdw                      eIdp eLeKd5                    dq            eIdxeKd5         ddy          dg           n eIdheKdi                     dzd{d|dFd}d~dig ddZPddd|d}ddd}dddd<gddZQ ejR        dzd
ePd e<d           d ZS ejR        dd
eQeSe<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)env_var_enabled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          5/home/kuhnn/.hermes/hermes-agent/tools/skills_tool.pyload_envr3   o   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     r2   r5   r5      s        I!LKKKr@   r5   )	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    r2   set_secret_capture_callbackrF      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_utilsrI   )rG   _impls     r2   rI   rI      s'     BAAAAA5r@   r1   c                 R    | sg S t          | t                    r| g} d | D             S )Nc                 n    g | ]2}t          |                                          #t          |          3S r?   strr(   ).0items     r2   
<listcomp>z2_normalize_prerequisite_values.<locals>.<listcomp>   s3    ===$3t99??+<+<=CII===r@   )
isinstancerO   )r1   s    r2   _normalize_prerequisite_valuesrT      s<     	% ==%====r@   c                     |                      d          }|rt          |t                    sg g fS t          |                     d                    t          |                     d                    fS )Nprerequisitesr,   commands)getrS   dictrT   )rG   prereqss     r2   _collect_prerequisite_valuesr[      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`   rb   rf   )rX   rS   rY   rO   r(   listboolappend)rG   r]   	help_textnormalized_helpcollect_secrets_rawr_   rQ   r`   rb   rd   entrys              r2   _normalize_setup_metadatarn      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_variablesrm   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`   ra   rb   rc   )rs   rb   r^   rd   re   required_foroptionalT)rO   rX   r(   _ENV_VAR_NAME_REmatchrS   addri   )rm   env_name
normalizedrj   rt   requiredseenr]   s        r2   _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@   rs   r_   r`   rb   rd   r^   )rs   rb   r^   )
rn   rX   rS   rY   rg   setr   rO   r   r[   )
rG   ro   required_rawr}   rQ   r0   r`   r{   r|   r]   s
          @@@r2   #_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 rs   r?   )rP   rm   s     r2   rR   z;_capture_required_environment_variables.<locals>.<listcomp>3  s    @@@uU6]@@@r@   r   r^   rt   rs   rb   z#Secret capture callback failed for Texc_info)success	stored_as	validatedskippedr   r   )_is_gateway_surface_gateway_setup_hintrD   rX   	ExceptionloggerwarningrS   rY   rh   ri   )
r   r   r   r   remaining_namesrm   metadatacallback_resultr   r   s
             r2   '_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                  `    t          d          rdS ddlm}  t           | d                    S )NHERMES_GATEWAY_SESSIONTr   get_session_envHERMES_SESSION_PLATFORM)r   gateway.session_contextr   rh   r   s    r2   r   r   p  sC    /00 t777777 9::;;;r@   c                      t          t          j        dd                                                                                    pdS )NTERMINAL_ENVlocal)rO   osgetenvr(   lowerr?   r@   r2   _get_terminal_backend_namer   w  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 rC   )r3   rh   rX   r   r   )r   r   s     r2   _is_env_var_persistedr   {  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   rs   ru   )r~   r3   rX   r   ri   )r   r   r   r   	remainingrm   rs   s          r2   %_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    r2   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 . )r5   r=   join)r   r   r   missing_strnotes        r2   _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@   r2   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)rJ   r   )r   r   s     r2   _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_DIRrJ   r   extendr   relative_topartslen
ValueError)r   dirs_to_checkr   
skills_dirrel_pathr   s         r2   _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?   rN   rP   ts     r2   rR   z_parse_tags.<locals>.<listcomp>  s+    8881a8A888r@   []   c                     g | ]=}|                                 |                                                      d           >S )r%   )r(   r   s     r2   rR   z_parse_tags.<locals>.<listcomp>  s9    OOOqQWWYYOAGGIIOOE""OOOr@   ,)rS   rg   rO   r(   r)   endswithsplit)
tags_values    r2   _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)rJ   r   r   s    r2   _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   ra   )r   r   r   r   s    r2   _get_session_platformr     sQ    ;;;;;;899?R?   rrs    
##rs   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   rX   r   r   r   r   r   )rs   r   r   config
skills_cfgresolved_platformr   s          r2   _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 rC   )_EXCLUDED_SKILL_DIRS)rP   parts     r2   	<genexpr>z#_find_all_skills.<locals>.<genexpr>A  s(      KKD4//KKKKKKr@   r    r!   Ni  rs   descriptionra   
r#   r   ...)rs   r   categoryz Failed to read skill file %s: %sz)Skipping skill at %s: failed to parse: %sTr   )rJ   r   r   r~   r   r   r&   ri   r   anyr   parent	read_textr   rI   rX   rs   MAX_NAME_LENGTHr(   r   r)   r   MAX_DESCRIPTION_LENGTHr   rx   UnicodeDecodeErrorPermissionErrorr   debugr   )r   r   r   r   
seen_namesr   dirs_to_scanscan_dirskill_md	skill_dirr   rG   bodyrs   r   r.   r   es                     r2   _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   ra   rs   rX   )ss    r2   <lambda>z_sort_skills.<locals>.<lambda>u  s     z):):)@b!F)(L r@   )r/   )sorted)r   s    r2   _sort_skillsr  s  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   ra   r   r#   r   r   z*Failed to read category description %s: %sz)Error parsing category description %s: %sTr   )r&   r   r   rX   r(   r   r)   r   r  r  r  r   r  r   r   )r  	desc_filer   rG   r  r   r.   r  s           r2   _load_category_descriptionr  x  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                 H    	 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          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  )rP   r  r   s     r2   rR   zskills_list.<locals>.<listcomp>  s/    QQQ1553D3D3P3P!3P3P3Pr@   c                 b    h | ],}|                     d           |                     d           -S r"  r  rP   r  s     r2   	<setcomp>zskills_list.<locals>.<setcomp>  s5    HHH1aeeJ6G6GHQUU:HHHr@   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   rO   )r   r  
all_skillsr  r  s   `    r2   skills_listr-    s   31  "" 
	TD999:# "$nObOdOdnnn	  #    &''
 		:# "$F	  #     	RQQQQZQQQJ "*--
 HH
HHH
 

 z$(ZZ  	
 	
 	
 		
  1 1 1#a&&%0000000001s+   AC/ #*C/ A C/ /
D!9DD!D!T
preprocess
session_idr	  	namespacebarer/  r0  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   r7  r   c              3   D   K   | ]}|                                 v V  d S rC   )r   )rP   pr   s     r2   r   z&_serve_plugin_skill.<locals>.<genexpr>  s0      
=
=A1
=
=
=
=
=
=r@   zIPlugin skill '%s:%s' contains patterns that may indicate prompt injectionr   ra   r   r   c                      g | ]
}|k    |S r?   r?   )rP   r  r2  s     r2   rR   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r0  z'Could not preprocess plugin skill %s:%sTr   )r   rs   r   r   linked_filesr   )hermes_cli.pluginsr4  r5  r*  r+  r   r   r   rI   r5   r>   r1   r   rA   r   r   rO   rX   r   r  list_plugin_skillsr   agent.skill_preprocessingrB  r   r  r<   )r	  r1  r2  r/  r0  r4  r5  r  parsed_frontmatterr0   r   siblingssib_listbannerrendered_contentrB  r   s     `             @r2   _serve_plugin_skillrM    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                   KLMNOPQ 	 d}d| v r3ddl m}m} ddlm}m}  ||           \  N}	 |N          s t          j        ddN d|  d	d
d          S  |              |            }
|
                    |           }|]|	                                s5|

                    |            t          j        dd|  d| dd
d          S t          |N|	||          S |
                    N          }|rBt          j        dd|	 dN dNfd|D             dN dt          |           ddd          S |	rN d|	 }ddl m} g }t          	                                r|                    t                     |                     |                       |st          j        ddd
d          S dQd}ddl m} g Lt'                      Pdt(          t*                   dt*          ddfLPfd}|D ]s}|| z  }|                                r'|dz  	                                r |||dz             nF|                    d           	                                r |d|                    d                      |r||z  }|                                r'|dz  	                                r |||dz             nF|                    d           	                                r |d|                    d                       ||d          D ]#}|j        j        | k    r ||j        |           $|                    |  d           D ]}|j        dk    r |d|           ut          L          d!k    rd" LD             }t7          j        t:                                        d#| t          L          d$                    |                     t          j        dd%|  d&t          L           d'|d(d)d          S LrLd         \  Q}|r|	                                sLd* tA          tC                                dd+         D             }t          j        dd|  d,|d-dd          S 	 |"                    d./          }n6# tF          $ r)}t          j        dd0|  d&| d
d          cY d}~S d}~ww xY wd1}t          $                                g}	 |                    d2 |d!d         D                        n# tF          $ r Y nw xY w|D ]=}	 |$                                %                    |           d} n# tL          $ r Y :w xY w|'                                KtQ          Kfd3tR          D                       }|s|rtg }|r|                    d4|            |r|                    d5           t7          j        t:                                        d6| d$                    |                     i }	 tU          |          \  }} n# tF          $ r i }Y nw xY wtW          |          s-t          j        dd|  d7tX          j-        j.        d8d          S |/                    d9|j        j                  }!ta          |!          rt          j        dd|! d:d
d          S |rQrdd;l1m2}"m3}#  |#|          rt          j        dd<d=d>d          S Q|z  }$ |"|$Q          }%|%rt          j        d|%d=d>d          S |$	                                sg g g g g d?}&Q                    d@          D ]/}'|'4                                r|'j        dk    rtk          |'%                    Q                    }(|(6                    dA          r|&dB                             |(           w|(6                    dC          r|&dD                             |(           |(6                    dE          r|&dF                             |(           |(6                    dG          r|&dH                             |(           |'j7        dIv r|&dJ                             |(           1dK |&8                                D             }&t          j        ddL| dM|  d|&dNdOd          S 	 |$"                    d./          }nO# tr          $ rB t          j        d1| |dP|$j         dQ|$:                                j;         dRd1dSd          cY S w xY wt          j        d1| |||$j7        dTd          S |})g }*g }+g },g }-QrXQdBz  }.|.	                                r!QfdU|.<                    dV          D             }*QdDz  }/|/	                                r9dWD ]6}0|+                    QfdX|/                    |0          D                        7QdFz  }1|1	                                ra|1                    d@          D ]K}'|'4                                r5|,                    tk          |'%                    Q                               LQdHz  }2|2	                                r9dYD ]6}0|-                    QfdZ|2<                    |0          D                        7i }3|)/                    d[          }4t{          |4t|                    r|4/                    d\i           pi }3t          |3/                    d]          p|)/                    d]d^                    }5t          |3/                    d_          p|)/                    d_d^                    }6i }7|*r|*|7dB<   |+r|+|7dD<   |,r|,|7dF<   |-r|-|7dH<   	 tk          |%                    t                              }8nO# tL          $ rB |j        j        r,tk          |%                    |j        j                            n|j        }8Y nw xY w|)/                    d9Qs|j@        nQj                  }9t          |)          \  }:} t          |)|:          };t                      }<t                      MMfd`|;D             }=t          |9|=          }>|=rt                      Mt          |;|>Ma          Ot          O          }?Ofdb|;D             }@|@r@	 ddclHmI}A  |A|@           n-# tF          $ r  t          K                    dd|9d1e           Y nw xY w|)/                    dfg           }Bt{          |Bt                    sg }Bg }C|BrD	 ddglMmN}D  |D|B          }C|Crd1}?n-# tF          $ r  t          K                    dh|9d1e           Y nw xY w|}E|rC	 ddilOmP}F  |F|Q|j          }En-# tF          $ r  t          K                    dk|9d1e           Y nw xY wi dld1d9|9dm|)/                    dmd^          d]|5d_|6dn|Edo|8dpQrtk          Q          nddq|7r|7nddr|7rdsnddt|;dug dvOdw|Cdxg dy|?dz|>dz         d{|?rtX          jQ        j.        ntX          jR        j.        i}Gt          d| |;D             d          }H|Hr|H|Gd}<   |>d~         r|>d~         |Gd~<   |?r`d OD             d |CD             z   }It          tX          jQ        |I|H          }J|<t          v r|Jr|J d|<V                                 d}J|Jr|J|Gd<   |)/                    d          r|)d         |Gd<   t{          |4t|                    r|4|Gd[<   t          j        |Gd          S # tF          $ r(}t          tk          |          d          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
    Nr9  r   )is_valid_namespaceparse_qualified_name)discover_pluginsr5  FzInvalid namespace 'z' in 'z('. Namespaces must match [a-zA-Z0-9_-]+.r6  r  r;  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 )r9  r?   )rP   r  r1  s     r2   rR   zskill_view.<locals>.<listcomp>  s'    ,S,S,SA	-?-?A-?-?,S,S,Sr@   zThe 'z' plugin provides z
 skill(s).)r   r7  available_skillsr'  /r   zISkills directory does not exist yet. It will be created on first install.)r   sdsmdr   c                     	 |                                 }n# t          $ r |}Y nw xY w|v rd S                     |                               | |f           d S rC   )resolver   rx   ri   )rV  rW  r/   
candidatesseen_mds      r2   _recordzskill_view.<locals>._record  sy    kkmm   g~~KKr3i(((((s    ''r   .mdr   c                 2    g | ]\  }}t          |          S r?   )rO   )rP   r0   rW  s      r2   rR   zskill_view.<locals>.<listcomp>  s"    777&!SSXX777r@   u3   Skill name collision for '%s': %d candidates — %sz; zAmbiguous skill name 'r:  u    skills match across your local skills dir and external_dirs. Refusing to guess — load one explicitly by its categorized path.zPass the full relative path instead of the bare name (e.g., 'category/skill-name'), or rename one of the colliding skills so each name is unique.)r   r7  matchesr'  c                     g | ]
}|d          S r   r?   r$  s     r2   rR   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!   r8  Tc              3   >   K   | ]}|                                 V  d S rC   )rY  )rP   ds     r2   r   zskill_view.<locals>.<genexpr>+  s*       C C C C C C C Cr@   c              3       K   | ]}|v V  	d S rC   r?   )rP   r?  _content_lowers     r2   r   zskill_view.<locals>.<genexpr>9  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': %sr<  r=  rs   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   r7  r'  )
references	templatesassetsscriptsother*zreferences/rh  z
templates/ri  zassets/rj  zscripts/rk  >   .py.sh.tex.yml.json.yamlr]  rl  c                     i | ]
\  }}|||S r?   r?   )rP   kvs      r2   
<dictcomp>zskill_view.<locals>.<dictcomp>  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   r7  available_filesr'  z[Binary file: z, size: z bytes])r   rs   filer   	is_binary)r   rs   ry  r   	file_typec                 T    g | ]$}t          |                                        %S r?   rO   r   rP   r-   r
  s     r2   rR   zskill_view.<locals>.<listcomp>  s:     # # #67Ci0011# # #r@   *.md)r  *.pyz*.yamlz*.ymlz*.jsonz*.tex*.shc                 T    g | ]$}t          |                                        %S r?   r}  r~  s     r2   rR   zskill_view.<locals>.<listcomp>  s=        !  i 8 899  r@   )r  r  z*.bashz*.jsz*.tsz*.rbc                 T    g | ]$}t          |                                        %S r?   r}  r~  s     r2   rR   zskill_view.<locals>.<listcomp>  s-    VVV1Q]]95566VVVr@   r   hermestagsra   related_skillsc                 j    g | ]/}|                     d           t          |d                   -|0S )ru   rs   )rX   r   )rP   r  r   s     r2   rR   zskill_view.<locals>.<listcomp>  sS     %
 %
 %
55$$%
 *!F)\BB	%
%
 %
 %
r@   r   c                 4    g | ]}|d          v|d          S r   r?   )rP   r  remaining_missing_required_envss     r2   rR   zskill_view.<locals>.<listcomp>1  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 %srA  rC  z)Could not preprocess skill content for %sr   r   r   pathr
  rD  
usage_hintzzTo view linked files, call skill_view(name, file_path) where file_path is e.g. 'references/api.md' or 'assets/config.yaml'rq   required_commands&missing_required_environment_variablesmissing_credential_filesmissing_required_commandsr7   r   r   c              3   P   K   | ]!}|                     d           |d          V  "dS )r^   Nr  )rP   r  s     r2   r   zskill_view.<locals>.<genexpr>  s5      QQ155==Q1V9QQQQQQr@   r   r   c                     g | ]}d | S )zenv $r?   )rP   ry   s     r2   rR   zskill_view.<locals>.<listcomp>  s.       '/"""  r@   c                     g | ]}d | S )zfile r?   )rP   r  s     r2   rR   zskill_view.<locals>.<listcomp>  s+       #'  r@   r   zW-backed skills need these requirements available inside the remote environment as well.
setup_notecompatibilityr(  )XrJ   rP  rQ  rE  rR  r5  r*  r+  find_plugin_skillr&   remove_plugin_skillrM  rF  r   r   r   ri   r   r   r~   r
   r   is_dirwith_suffixr   rs   rgloblogging	getLoggerr9   r   r   r  r  r   r   rY  r   r   r   r   rA   r   rI   r5   r>   r1   rX   r   tools.path_securityrf  rg  is_filerO   r)   suffixitemsr  statst_sizeglobrS   rY   r   stemr[   r   r   r3   r   r   rh   tools.env_passthroughr  r   r  rg   tools.credential_filesr  rG  rB  r=   r<   nextr   _REMOTE_ENV_BACKENDSupperr   )Rrs   rN  r  r/  local_category_namerP  rQ  rR  r5  r2  pmplugin_skill_mdr6   r   all_dirsr	  r   r\  
search_dirdirect_pathcategorized_pathfound_skill_mdfound_mdpathsr   r  _outside_skills_dir_trusted_dirs_td_injection_detected	_warningsrH  r0   resolved_namerf  rg  target_filetraversal_errorrx  r-   relrG   reference_filestemplate_filesasset_filesscript_filesreferences_dirtemplates_dirext
assets_dirscripts_dirhermes_metar   r  r  rD  r   r   ro   r   backendmissing_required_env_varsr   r7   available_env_namesr  required_cred_files_rawmissing_cred_filesr  rL  rB  resultr   missing_itemsr  re  rZ  r   r1  r  r[  r
  sR                                                                              @@@@@@@r2   
skill_viewr  R  sb   *x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^^ _ _ _	  "'     <)2&;&;T&;&;#>>>>>>  	(OOJ'''0022333 	:$h  #    	 	=<<<<<8:
uu	) 	)T 	)d 	) 	) 	) 	) 	) 	) 	) # 	, 	,J %t+K!!## >z)A(I(I(K(K >[:%=>>>>((//6688 >k55e<<===
 # G#-0C#C #**,, G2BZ2O1W1W1Y1Y GG,.>.KLLLL%11%88??AA GGD"2">">u"E"EFFF #9"8Z"P"P C C!(-55GN1>BBB ',,\\\:: , ,=J..GD(+++, z??Q77J777Eh''//Ec*ootyy'7'7   :$] ] ]#j// ] ] ]  %C  #   $  	0",Q-Ix 
	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| A3| | !A| :A;| 6I| A.| 6R | 
S R;5S 6| ;S  |  'T | 
T| T| )U| 
U| UB1| X | X)&| (X))>| (A| 42| ',| F"| 8c | A	d| d#| ?H7| 7'n | A	o+(| *o++C| 0s | 's,)| +s,,4| !t7 6| 7'u!|  u!!| )u> =| >'v(%| 'v((E6| 
})}}}__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] ra   u     • z: r   <   r   zError: r7  u   
📖 Viewing skill 'axolotl':axolotlzName: zDescription: zN/Ad   zContent length: z charsrD  zLinked files: uE   
📄 Viewing reference file 'axolotl/references/dataset-formats.md':zreferences/dataset-formats.mdzFile: ry  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  
propertiesr{   )rs   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.)rs   rN  c                 p    t          |                     d          |                    d                    S )Nr   r  )r   r  )r-  rX   )argskws     r2   r  r    s2    {*%%rvvi/@/@      r@   u   📚)rs   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.rs   ra   rN  r  )rN  r  r   r   )bump_use	bump_view)rX   r  r*  loadsrS   rY   tools.skill_usager  r  rO   r   )r  r  rs   r  parsedresolvedr  r  s           r2   _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   NrC   )NN)NNT)T__doc__r*  r  hermes_constantsr   r   r   reenumr   pathlibr   typingr   r   r	   r
   r   r   tools.registryr   r   r   r   utilsr   r  r9   r   HERMES_HOMEr   r  r  _PLATFORM_MAPcompilerv   	frozensetr   r  rD   rO   r3   r5   rA   rg   __annotations__rF   rh   rI   rT   r[   rn   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r-  rM  r  printr  r  r   rX   skillcatSKILLS_LIST_SCHEMASKILL_VIEW_SCHEMAregisterr  r?   r@   r2   <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 	M	1 M	1
M	1M	1 M	1 	M	1
 	M	1 M	1 M	1 M	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@   