深入理解Hermes Agent Skill 機制
發(fā)布時間:2026-07-16 09:57:36 作者:金烏意_
我要評論
本文主要介紹了深入理解Hermes Agent Skill 機制,包含發(fā)現(xiàn)索引→觸發(fā)加載→預處理→Prompt注入→LLM響應五個階段,下面就詳細了解這五種階段,感興趣的可以了解一下
Summary
Hermes Agent 的 Skill 系統(tǒng)是一個完整的知識注入機制,包含發(fā)現(xiàn)索引→觸發(fā)加載→預處理→Prompt注入→LLM響應五個階段。Skill 以 SKILL.md 文件形式存儲,通過 YAML frontmatter 聲明元數(shù)據(jù),支持條件激活、平臺過濾、安全檢測等特性。
1. Skill 目錄結(jié)構(gòu)
~/.hermes/skills/ ├── category-a/ │ └── skill-name/ │ ├── SKILL.md # 必需:主文件 │ ├── references/ # 可選:參考資料 │ ├── templates/ # 可選:模板文件 │ ├── scripts/ # 可選:腳本文件 │ └── assets/ # 可選:資源文件 ├── category-b/ │ └── another-skill/ │ └── SKILL.md └── ...
關鍵特性:
- 支持任意深度的子目錄嵌套(通過
os.walk遞歸掃描) - 子目錄僅用于分類組織,不影響索引
- 排除目錄:
.git,.github,.hub,.archive
2. 完整生命周期(5階段)
Phase 1: 發(fā)現(xiàn)與索引(啟動時)
get_all_skills_dirs()
├── ~/.hermes/skills/ # 本地目錄(始終在前)
└── skills.external_dirs # 外部目錄(config.yaml配置)
↓
iter_skill_index_files() # 遞歸掃描所有 SKILL.md
↓
parse_frontmatter() # 解析 YAML 元數(shù)據(jù)
↓
過濾規(guī)則:
├── skill_matches_platform() # 平臺兼容性檢查
├── get_disabled_skill_names() # 禁用列表過濾
└── _skill_should_show() # 條件激活規(guī)則
↓
build_skills_system_prompt() # 生成分類索引 Prompt
兩級緩存機制
| 層級 | 類型 | 容量 | Key |
|---|---|---|---|
| L1 | 進程內(nèi) LRU 緩存 | 8 entries | (skills_dir, external_dirs, tools, toolsets, platform, disabled) |
| L2 | 磁盤快照 | 持久化 | .skills_prompt_snapshot.json(mtime/size驗證) |
生成的 System Prompt 格式
## Skills (mandatory)
Before replying, scan the skills below. If a skill matches or is even partially relevant
to your task, you MUST load it with skill_view(name) and follow its instructions.
<available_skills>
category-a:
- skill-name: brief description
- another-skill: another description
</available_skills>
Only proceed without loading a skill if genuinely none are relevant to the task.
Phase 2: 觸發(fā)(3種方式)
方式 A: LLM 自主調(diào)用(主要路徑)
System Prompt 引導:
"Before replying, scan the skills below... MUST load with skill_view(name)"
↓
LLM 判斷任務與 skill 相關
↓
tool_call: skill_view(name="skill-name")
觸發(fā)條件:
- Skill 名稱或描述與用戶請求匹配
- Skill 的
description字段包含關鍵詞 - LLM 自主判斷"部分相關"就應加載
方式 B: Slash 命令(用戶顯式調(diào)用)
用戶輸入: /skill-name some instruction
↓
scan_skill_commands() # 掃描所有 SKILL.md 生成命令映射
↓
匹配 /skill-name
↓
build_skill_invocation_message() # 構(gòu)建觸發(fā)消息
Skill 命令命名規(guī)則:
- Skill name 規(guī)范化:小寫,空格/下劃線→連字符,去除非法字符
- 示例:
My Skill→/my-skill
方式 C: CLI 預加載(Session 啟動時)
hermes --skills skill-name
build_preloaded_skills_prompt()
↓
注入到 session prompt 開頭
Phase 3: 加載與預處理
skill_view() 執(zhí)行流程
skill_view(name)
├── 解析 qualified name # plugin:skill → 路由到 plugin 系統(tǒng)
├── 目錄搜索(first match wins) # local → external
│ ├── 直接路徑: search_dir/name/SKILL.md
│ ├── 分類路徑: search_dir/category/name/SKILL.md
│ └── 名稱匹配: 遍歷所有 SKILL.md 比對目錄名
├── 安全檢查
│ ├── 路徑穿越檢測(防止 .. 攻擊)
│ └── prompt injection 模式掃描
├── 平臺/禁用檢查
├── 前置條件檢查
│ ├── 環(huán)境變量(prerequisites.env)
│ └── 憑證文件(prerequisites.credential_files)
├── preprocess_skill_content()
│ ├── 模板變量替換: ${HERMES_SKILL_DIR} → 絕對路徑
│ └── inline shell 展開: !`cmd` → 執(zhí)行結(jié)果
├── 收集 linked_files
│ ├── references/*.md
│ ├── templates/*.yaml
│ ├── scripts/*.py
│ └── assets/*
└── 返回 JSON:
{
"success": true,
"name": "skill-name",
"content": "<SKILL.md 完整內(nèi)容>",
"linked_files": {"references": [...], "templates": [...]},
"skill_dir": "/path/to/skill",
"setup_needed": false,
"readiness_status": "available"
}
預處理機制
模板變量替換:
${HERMES_SKILL_DIR} → skill 的絕對路徑
${HERMES_SESSION_ID} → 當前會話 ID
Inline Shell 展開(需配置啟用):
!`ls -la ${HERMES_SKILL_DIR}/scripts/`
→ 替換為命令執(zhí)行結(jié)果
Phase 4: Prompt 注入
方式 A: LLM tool call 返回
tool 返回 JSON
↓
作為 tool_result 注入 conversation history
↓
LLM 在下一輪看到完整 skill 內(nèi)容
方式 B: Slash 命令消息構(gòu)建
_build_skill_message()
├── [IMPORTANT: The user has invoked the "xxx" skill...]
├── <SKILL.md 完整內(nèi)容(已預處理)>
├── [Skill directory: /path/to/skill]
├── [Skill config: key = value] # 配置變量注入
├── [Skill setup note: ...] # 安裝提示
├── [This skill has supporting files:] # 支持文件列表
│ ├── references/api.md → /path/to/references/api.md
│ └── scripts/run.py → /path/to/scripts/run.py
└── The user has provided the following instruction: ...
↓
作為 user message 注入 → LLM 響應
方式 C: CLI 預加載
[IMPORTANT: The user launched this CLI session with "xxx" ...]
<SKILL.md 完整內(nèi)容>
↓
追加到 system prompt 的 prompt_parts
Phase 5: LLM 響應與執(zhí)行
LLM 接收包含 skill 內(nèi)容的 message ├── 遵循 skill 中的 instructions ├── 可通過 skill_view(name, file_path) 加載 supporting files ├── 執(zhí)行 skill 中的 scripts(通過 skill_dir 絕對路徑) ├── 使用 skill 聲明的配置變量 └── 任務完成后可選:skill_manage(action='patch') 更新 skill
3. Skill 元數(shù)據(jù)(YAML Frontmatter)
---
name: skill-name
description: "簡短描述(≤1024字符)"
version: 1.0.0
author: Author Name
license: MIT
platforms: [macos, linux] # 可選:平臺限制
metadata:
hermes:
tags: [tag1, tag2]
related_skills: [skill-a, skill-b]
fallback_for_toolsets: [web] # 當 web toolset 可用時隱藏
fallback_for_tools: [web_fetch] # 當 web_fetch tool 可用時隱藏
requires_toolsets: [browser] # 當 browser toolset 不可用時隱藏
requires_tools: [browser_click] # 當 browser_click tool 不可用時隱藏
config: # 配置變量聲明
- key: api_endpoint
description: API endpoint URL
default: "https://api.example.com"
prompt: Enter API endpoint
prerequisites:
env:
- name: API_KEY
description: API key for authentication
credential_files:
- path: ~/.config/skill/credentials.json
description: Credentials file
---
4. 條件激活規(guī)則
| 字段 | 含義 | 示例 |
|---|---|---|
| fallback_for_toolsets | 當指定 toolset 可用時隱藏此 skill | 有 web toolset 時不需要 web-fallback skill |
| fallback_for_tools | 當指定 tool 可用時隱藏此 skill | 有 web_fetch tool 時不需要備用方案 |
| requires_toolsets | 當指定 toolset 不可用時隱藏 | 沒有 browser toolset 時瀏覽器 skill 不可用 |
| requires_tools | 當指定 tool 不可用時隱藏 | 沒有 git tool 時 git 相關 skill 不可用 |
5. 安全機制
Prompt Injection 檢測
檢測模式:
ignore previous instructionsdisregard your instructionsyou are nowsystem prompt:<system>
處理方式:
- Context 文件注入:拒絕加載并警告
- Skill 內(nèi)容注入:記錄警告但仍加載(skill 是可信來源)
路徑安全
- 防止路徑穿越攻擊(
..) - 驗證解析路徑仍在 skill 目錄內(nèi)
- 禁止訪問 skill 目錄外的文件
6. 關鍵文件職責
| 文件 | 職責 |
|---|---|
| hermes_constants.py | get_skills_dir() → ~/.hermes/skills/ 路徑解析 |
| agent/skill_utils.py | 核心工具:frontmatter 解析、平臺匹配、禁用列表、外部目錄、條件提取、文件迭代 |
| agent/prompt_builder.py | build_skills_system_prompt() 生成分類索引,兩級緩存,_skill_should_show() 條件過濾 |
| agent/skill_commands.py | Slash 命令掃描、消息構(gòu)建、CLI 預加載 |
| agent/skill_preprocessing.py | 模板變量替換、inline shell 展開 |
| tools/skills_tool.py | Tool 注冊(skills_list, skill_view)、skill 加載、安全檢查、linked_files 發(fā)現(xiàn) |
| model_tools.py | Tool 注冊觸發(fā)、toolset 過濾 |
| tools/registry.py | Tool 注冊機制,registry.register() 統(tǒng)一注冊 schema + handler |
| run_agent.py | Agent 循環(huán)中調(diào)用 build_skills_system_prompt(),注入 system prompt |
7. 優(yōu)化與最佳實踐
Skill 設計原則
- 描述清晰:
description字段是 LLM 判斷是否加載的主要依據(jù),應包含關鍵詞 - 觸發(fā)詞明確:在 description 中列出典型觸發(fā)場景
- 條件激活合理使用:避免與內(nèi)置 tool 沖突,使用
fallback_for_*聲明備選關系 - 支持文件組織:將參考資料、模板、腳本放在對應子目錄中
- 配置變量聲明:需要用戶配置的參數(shù)應在 frontmatter 中聲明
性能優(yōu)化
- 兩級緩存:避免重復掃描文件系統(tǒng)
- 條件過濾前置:在索引階段就過濾掉不兼容的 skill
- 延遲加載:只加載與當前任務相關的 skill 內(nèi)容
到此這篇關于深入理解Hermes Agent Skill 機制的文章就介紹到這了,更多相關Hermes Agent Skill 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持腳本之家!
相關文章

一文全解Hermes Agent 的 Skills、Plugins、Gateway
本文給大家深度解析Hermes Agent 的 Skills、Plugins、Gateway的相關知識,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2026-05-09


