> 原文链接:Plugins
sidebar_position: 11 sidebar_label: "Plugins" title: "Plugins" description: "Extend Hermes with custom tools, hooks, and integrations via the plugin system"
Plugins(插件)
Hermes 拥有插件系统,用于添加自定义工具、钩子和集成,无需修改核心代码。
→ 构建 Hermes 插件 — 分步指南,附带完整可运行的示例。
快速概览
将一个目录放入 ~/.hermes/plugins/,包含 plugin.yaml 和 Python 代码:
~/.hermes/plugins/my-plugin/
├── plugin.yaml # 清单文件
├── __init__.py # register() — 将 schema 连接到处理器
├── schemas.py # 工具 schema(LLM 看到的内容)
└── tools.py # 工具处理器(被调用时运行的代码)
启动 Hermes — 你的工具会与内置工具一起出现。模型可以立即调用它们。
最小可运行示例
以下是一个完整的插件,添加了 hello_world 工具,并通过钩子记录每次工具调用。
~/.hermes/plugins/hello-world/plugin.yaml
name: hello-world
version: "1.0"
description: 一个最小示例插件
~/.hermes/plugins/hello-world/__init__.py
"""最小 Hermes 插件 — 注册一个工具和一个钩子。"""
def register(ctx):
# --- 工具: hello_world ---
schema = {
"name": "hello_world",
"description": "返回给定名称的友好问候。",
"parameters": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "要问候的名称",
}
},
"required": ["name"],
},
}
def handle_hello(params):
name = params.get("name", "World")
return f"Hello, {name}! 👋 (from the hello-world plugin)"
ctx.register_tool("hello_world", schema, handle_hello)
# --- 钩子: 记录每次工具调用 ---
def on_tool_call(tool_name, params, result):
print(f"[hello-world] tool called: {tool_name}")
ctx.register_hook("post_tool_call", on_tool_call)
将两个文件放入 ~/.hermes/plugins/hello-world/,重启 Hermes,模型即可立即调用 hello_world。钩子会在每次工具调用后打印一行日志。
项目本地插件位于 ./.hermes/plugins/ 下,默认禁用。仅在受信任的仓库中启用,方法是在启动 Hermes 前设置 HERMES_ENABLE_PROJECT_PLUGINS=true。
插件能做什么
| 能力 | 方式 |
|---|---|
| 添加工具 | ctx.register_tool(name, schema, handler) |
| 添加钩子 | ctx.register_hook("post_tool_call", callback) |
| 添加斜杠命令 | ctx.register_command(name, handler, description) — 在 CLI 和 Gateway 会话中添加 /name |
| 添加 CLI 命令 | ctx.register_cli_command(name, help, setup_fn, handler_fn) — 添加 hermes <plugin> <subcommand> |
| 注入消息 | ctx.inject_message(content, role="user") — 参见注入消息 |
| 携带数据文件 | Path(__file__).parent / "data" / "file.yaml" |
| 打包技能 | ctx.register_skill(name, path) — 命名空间为 plugin:skill,通过 skill_view("plugin:skill") 加载 |
| 环境变量门控 | plugin.yaml 中的 requires_env: [API_KEY] — 在 hermes plugins install 期间提示 |
| 通过 pip 分发 | [project.entry-points."hermes_agent.plugins"] |
插件发现
| 来源 | 路径 | 用例 |
|---|---|---|
| 用户 | ~/.hermes/plugins/ | 个人插件 |
| 项目 | .hermes/plugins/ | 项目特定插件(需要 HERMES_ENABLE_PROJECT_PLUGINS=true) |
| pip | hermes_agent.plugins entry_points | 分发包 |
可用钩子
插件可以为以下生命周期事件注册回调。完整详情、回调签名和示例请参见 事件钩子页面。
| 钩子 | 触发时机 |
|---|---|
pre_tool_call | 任何工具执行之前 |
post_tool_call | 任何工具返回之后 |
pre_llm_call | 每轮一次,LLM 循环之前 — 可返回 {"context": "..."} 来向用户消息注入上下文 |
post_llm_call | 每轮一次,LLM 循环之后(仅成功轮次) |
on_session_start | 新会话创建(仅首轮) |
on_session_end | 每次 run_conversation 调用结束时 + CLI 退出处理器 |
插件类型
Hermes 有三种插件:
| 类型 | 功能 | 选择方式 | 位置 |
|---|---|---|---|
| 通用插件 | 添加工具、钩子、斜杠命令、CLI 命令 | 多选(启用/禁用) | ~/.hermes/plugins/ |
| 记忆 Provider | 替换或增强内置记忆 | 单选(一个激活) | plugins/memory/ |
| 上下文引擎 | 替换内置上下文压缩器 | 单选(一个激活) | plugins/context_engine/ |
记忆 Provider 和上下文引擎是 Provider 插件 — 每种类型同时只能激活一个。通用插件可以任意组合启用。
管理插件
hermes plugins # 统一交互式 UI
hermes plugins list # 表格视图,显示启用/禁用状态
hermes plugins install user/repo # 从 Git 安装
hermes plugins update my-plugin # 拉取最新版本
hermes plugins remove my-plugin # 卸载
hermes plugins enable my-plugin # 重新启用已禁用的插件
hermes plugins disable my-plugin # 禁用但不移除
交互式 UI
不带参数运行 hermes plugins 会打开一个复合交互式界面:
Plugins
↑↓ 导航 SPACE 切换 ENTER 配置/确认 ESC 完成
General Plugins
→ [✓] my-tool-plugin — 自定义搜索工具
[ ] webhook-notifier — 事件钩子
Provider Plugins
Memory Provider ▸ honcho
Context Engine ▸ compressor
- General Plugins 区域 — 复选框,用 SPACE 切换
- Provider Plugins 区域 — 显示当前选择。按 ENTER 进入单选选择器,选择一个活跃 Provider。
Provider 插件选择保存到 config.yaml:
memory:
provider: "honcho" # 空字符串 = 仅内置
context:
engine: "compressor" # 默认内置压缩器
禁用通用插件
禁用的插件保持安装但加载时会被跳过。禁用列表存储在 config.yaml 的 plugins.disabled 中:
plugins:
disabled:
- my-noisy-plugin
在运行中的会话中,/plugins 显示当前加载的插件。
注入消息
插件可以使用 ctx.inject_message() 向活动对话注入消息:
ctx.inject_message("新数据从 webhook 到达", role="user")
签名: ctx.inject_message(content: str, role: str = "user") -> bool
工作原理:
- 如果 Agent 空闲(等待用户输入),消息会排队作为下一个输入并开始新轮次。
- 如果 Agent 正在运行(活跃处理中),消息会中断当前操作 — 效果等同于用户输入新消息并按回车。
- 对于非
"user"角色,内容会添加[role]前缀(如[system] ...)。 - 如果消息成功排队返回
True,如果没有 CLI 引用可用(例如在 Gateway 模式下)则返回False。
这使得远程控制查看器、消息桥接或 Webhook 接收器等插件能够将消息从外部来源注入对话。
:::note 注意
inject_message 仅在 CLI 模式下可用。在 Gateway 模式下,没有 CLI 引用,该方法返回 False。
:::
完整指南请参见 构建插件指南,了解处理器契约、Schema 格式、钩子行为、错误处理和常见错误。