跳到主要内容

插件

Hermes 拥有一套插件系统,可在不修改核心代码的情况下添加自定义工具、钩子和集成。

如果你想为自己、团队或某个项目创建自定义工具,这通常是正确的路径。开发指南中的 添加工具 页面适用于存放在 tools/toolsets.py 中的 Hermes 核心内置工具。

构建 Hermes 插件 — 包含完整工作示例的逐步指南。

快速概览

将一个包含 plugin.yaml 和 Python 代码的目录放入 ~/.hermes/plugins/

~/.hermes/plugins/my-plugin/
├── plugin.yaml # 清单文件
├── __init__.py # register() — 将 schema 与 handler 绑定
├── schemas.py # 工具 schema(LLM 所见的内容)
└── tools.py # 工具 handler(调用时运行的代码)

启动 Hermes,你的工具将与内置工具并排出现。模型可以立即调用它们。

最小工作示例

以下是一个完整的插件,它添加了一个 hello_world 工具,并通过钩子记录每次工具调用。

~/.hermes/plugins/hello-world/plugin.yaml

name: hello-world
version: "1.0"
description: A minimal example plugin

~/.hermes/plugins/hello-world/__init__.py

"""Minimal Hermes plugin — registers a tool and a hook."""

import json


def register(ctx):
# --- Tool: hello_world ---
schema = {
"name": "hello_world",
"description": "Returns a friendly greeting for the given name.",
"parameters": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Name to greet",
}
},
"required": ["name"],
},
}

def handle_hello(params, **kwargs):
del kwargs
name = params.get("name", "World")
return json.dumps({"success": True, "greeting": f"Hello, {name}!"})

ctx.register_tool(
name="hello_world",
toolset="hello_world",
schema=schema,
handler=handle_hello,
description="Return a friendly greeting for the given name.",
)

# --- Hook: log every tool call ---
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.* API 均可在插件的 register(ctx) 函数中使用。

功能方式
添加工具ctx.register_tool(name=..., toolset=..., schema=..., handler=...)
添加钩子ctx.register_hook("post_tool_call", callback)
添加斜杠命令ctx.register_command(name, handler, description) — 在 CLI 和网关会话中添加 /name
从命令分发工具ctx.dispatch_tool(name, args) — 自动注入父 Agent 上下文来调用已注册的工具
添加 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"]
注册网关平台(Discord、Telegram、IRC 等)ctx.register_platform(name, label, adapter_factory, check_fn, ...) — 参见添加平台适配器
注册图像生成后端ctx.register_image_gen_provider(provider) — 参见图像生成提供商插件
注册视频生成后端ctx.register_video_gen_provider(provider) — 参见视频生成提供商插件
注册上下文压缩引擎ctx.register_context_engine(engine) — 参见上下文引擎插件
注册记忆后端plugins/memory/<name>/__init__.py 中继承 MemoryProvider — 参见记忆提供商插件(使用独立发现系统)
运行宿主拥有的 LLM 调用ctx.llm.complete(...) / ctx.llm.complete_structured(...) — 借用用户的活跃模型 + 认证进行一次补全,支持可选 JSON schema 验证。参见插件 LLM 访问
注册推理后端(LLM 提供商)plugins/model-providers/<name>/__init__.py 中调用 register_provider(ProviderProfile(...)) — 参见模型提供商插件(使用独立发现系统)

插件发现

来源路径使用场景
内置<repo>/plugins/随 Hermes 一起发布——参见内置插件
用户~/.hermes/plugins/个人插件
项目.hermes/plugins/项目特定插件(需要 HERMES_ENABLE_PROJECT_PLUGINS=true
piphermes_agent.plugins entry_points分发包
Nixservices.hermes-agent.extraPlugins / extraPythonPackagesNixOS 声明式安装——参见 Nix 设置

后来的来源在名称冲突时会覆盖前面的,因此与内置插件同名的用户插件会替换它。

插件子分类

在每个来源内,Hermes 还识别将插件路由到专门发现系统的子分类目录:

子目录包含内容发现系统
plugins/(根目录)通用插件——工具、钩子、斜杠命令、CLI 命令、捆绑技能PluginManager(kind: standalonebackend
plugins/platforms/<name>/网关频道适配器(ctx.register_platform()PluginManager(kind: platform,深一层)
plugins/image_gen/<name>/图像生成后端(ctx.register_image_gen_provider()PluginManager(kind: backend,深一层)
plugins/memory/<name>/记忆提供商(继承 MemoryProviderplugins/memory/__init__.py 中的专属加载器(kind: exclusive——同时只有一个活跃)
plugins/context_engine/<name>/上下文压缩引擎(ctx.register_context_engine()plugins/context_engine/__init__.py 中的专属加载器(同时只有一个活跃)
plugins/model-providers/<name>/LLM 提供商配置(register_provider(ProviderProfile(...))providers/__init__.py 中的专属加载器(在首次 get_provider_profile() 调用时懒扫描)

~/.hermes/plugins/model-providers/<name>/~/.hermes/plugins/memory/<name>/ 下的用户插件会覆盖同名内置插件——在 register_provider() / register_memory_provider() 中后写者胜出。放入目录,即替换内置版本,无需修改仓库。

插件是可选启用的(少数例外)

通用插件和用户安装的后端默认禁用——发现后它们会显示在 hermes plugins/plugins 中,但在你将插件名称添加到 ~/.hermes/config.yamlplugins.enabled 之前,不会加载任何带钩子或工具的内容。这阻止了第三方代码在未经明确同意的情况下运行。

plugins:
enabled:
- my-tool-plugin
- disk-cleanup
disabled: # 可选拒绝列表——如果名称同时出现在两者中,始终胜出
- noisy-plugin

三种切换状态的方式:

hermes plugins # 交互式切换(空格选中/取消选中)
hermes plugins enable <name> # 添加到允许列表
hermes plugins disable <name> # 从允许列表移除 + 添加到禁用列表

hermes plugins install owner/repo 后,会询问 Enable 'name' now? [y/N]——默认为否。为脚本化安装跳过提示,使用 --enable--no-enable

允许列表不门控的内容

几类插件绕过 plugins.enabled——它们是 Hermes 内置功能的一部分,如果默认被门控会导致基本功能失效:

插件类型替代激活方式
内置平台插件(IRC、Teams 等,在 plugins/platforms/ 下)自动加载,使所有已发布的网关频道可用。实际频道通过 config.yaml 中的 gateway.platforms.<name>.enabled 开启。
内置后端plugins/image_gen/ 等下的图像生成提供商)自动加载,使默认后端"开箱即用"。通过 config.yaml 中的 <category>.provider 选择(例如 image_gen.provider: openai)。
记忆提供商plugins/memory/全部发现;只有一个活跃,由 config.yaml 中的 memory.provider 选择。
上下文引擎plugins/context_engine/全部发现;一个活跃,由 config.yaml 中的 context.engine 选择。
模型提供商plugins/model-providers/plugins/model-providers/ 下的全部捆绑提供商在首次 get_provider_profile() 调用时发现并注册。用户通过 --providerconfig.yaml 一次选择一个。
pip 安装的 backend 插件通过 plugins.enabled 可选启用(与通用插件相同)。
用户安装的平台(在 ~/.hermes/plugins/platforms/ 下)通过 plugins.enabled 可选启用——第三方网关适配器需要明确同意。

简而言之:内置"始终可用"的基础设施自动加载;第三方通用插件是可选启用的。 plugins.enabled 允许列表专门用于用户放入 ~/.hermes/plugins/ 的任意代码。

现有用户的迁移

当你升级到有可选启用插件的 Hermes 版本(config schema v21+)时,已安装在 ~/.hermes/plugins/ 下且不在 plugins.disabled 中的用户插件会自动纳入 plugins.enabled。你的现有设置继续工作。内置独立插件自动纳入——即使是现有用户也必须明确选择启用。(内置平台/后端插件从不需要自动纳入,因为它们从未被门控。)

可用钩子

插件可以为这些生命周期事件注册回调。完整细节、回调签名和示例参见**事件钩子页面**。

钩子触发时机
pre_tool_call任意工具执行前
post_tool_call任意工具返回后
pre_llm_call每轮一次,LLM 循环前——可返回 {"context": "..."}将上下文注入用户消息
post_llm_call每轮一次,LLM 循环后(仅成功轮次)
on_session_start新会话创建(仅第一轮)
on_session_end每次 run_conversation 调用结束 + CLI 退出处理器
on_session_finalizeCLI/网关拆除活跃会话时(/new、GC、CLI 退出)
on_session_reset网关换入新会话键时(/new/reset/clear、空闲轮换)
subagent_stopdelegate_task 完成后每个子 Agent 触发一次
pre_gateway_dispatch网关接收到用户消息,在认证 + 分发之前。返回 {"action": "skip" | "rewrite" | "allow", ...} 以影响流程。

插件类型

Hermes 有四种插件:

类型功能选择方式位置
通用插件添加工具、钩子、斜杠命令、CLI 命令多选(启用/禁用)~/.hermes/plugins/
记忆提供商替换或增强内置记忆单选(一个活跃)plugins/memory/
上下文引擎替换内置上下文压缩器单选(一个活跃)plugins/context_engine/
模型提供商声明推理后端(OpenRouter、Anthropic 等)多注册,通过 --provider / config.yaml 选择plugins/model-providers/

记忆提供商和上下文引擎是提供商插件——每种类型同时只能有一个活跃。模型提供商也是插件,但可以同时加载多个;用户通过 --providerconfig.yaml 一次选择一个。通用插件可以任意组合启用。

可插拔接口——每种情况去哪里

上表展示了四个插件类别,但在"通用插件"内,PluginContext 暴露了几个不同的扩展点——Hermes 还接受 Python 插件系统之外的扩展(配置驱动的后端、shell 钩子命令、外部服务器等)。使用此表找到你想构建内容的正确文档:

想要添加…方式编写指南
LLM 可调用的工具Python 插件——ctx.register_tool()构建 Hermes 插件 · 添加工具
生命周期钩子(LLM 前/后、会话开始/结束、工具过滤器)Python 插件——ctx.register_hook()钩子参考 · 构建 Hermes 插件
CLI / 网关的斜杠命令Python 插件——ctx.register_command()构建 Hermes 插件 · 扩展 CLI
hermes <thing>子命令Python 插件——ctx.register_cli_command()扩展 CLI
插件携带的捆绑技能Python 插件——ctx.register_skill()创建技能
推理后端(LLM 提供商:OpenAI 兼容、Codex、Anthropic-Messages、Bedrock)提供商插件——在 plugins/model-providers/<name>/ 中调用 register_provider(ProviderProfile(...))模型提供商插件 · 添加提供商
网关频道(Discord / Telegram / IRC / Teams 等)平台插件——在 plugins/platforms/<name>/ 中调用 ctx.register_platform()添加平台适配器
记忆后端(Honcho、Mem0、Supermemory 等)记忆插件——在 plugins/memory/<name>/ 中继承 MemoryProvider记忆提供商插件
上下文压缩策略上下文引擎插件——ctx.register_context_engine()上下文引擎插件
图像生成后端(DALL·E、SDXL 等)后端插件——ctx.register_image_gen_provider()图像生成提供商插件
TTS 后端(任意 CLI——Piper、VoxCPM、Kokoro、xtts、声音克隆脚本等)配置驱动——在 config.yamltts.providers.<name> 下声明 type: commandTTS 设置
STT 后端(自定义 whisper 二进制、本地 ASR CLI)配置驱动——将 HERMES_LOCAL_STT_COMMAND 环境变量设置为 shell 模板语音消息转录(STT)
通过 MCP 的外部工具(文件系统、GitHub、Linear、Notion、任意 MCP 服务器)配置驱动——在 config.yaml 中用 command: / url: 声明 mcp_servers.<name>。Hermes 自动发现服务器的工具并与内置工具一起注册。MCP
额外技能来源(自定义 GitHub 仓库、私有技能索引)CLI——hermes skills tap add <repo>技能中心 · 发布自定义 tap
网关事件钩子(在 gateway:startupsession:startagent:endcommand:* 上触发)HOOK.yaml + handler.py 放入 ~/.hermes/hooks/<name>/事件钩子
Shell 钩子(在事件上运行 shell 命令——通知、审计日志、桌面提醒)配置驱动——在 config.yamlhooks: 下声明Shell 钩子
备注

并非所有内容都是 Python 插件。某些扩展接口有意使用配置驱动的 shell 命令(TTS、STT、shell 钩子),这样你已有的任何 CLI 无需编写 Python 即可成为插件。其他是 Agent 连接并自动注册工具的外部服务器(MCP)。还有一些是具有自己清单格式的直接放入目录(网关钩子)。为你的集成风格选择合适的接口;上表中的编写指南均涵盖占位符、发现和示例。

NixOS 声明式插件

在 NixOS 上,可以通过模块选项声明式安装插件——无需 hermes plugins install。完整细节参见 Nix 设置指南

services.hermes-agent = {
# 目录插件(带 plugin.yaml 的源树)
extraPlugins = [ (pkgs.fetchFromGitHub { ... }) ];
# 入口点插件(pip 包)
extraPythonPackages = [ (pkgs.python312Packages.buildPythonPackage { ... }) ];
# 在配置中启用
settings.plugins.enabled = [ "my-plugin" ];
};

声明式插件以 nix-managed- 前缀符号链接——与手动安装的插件共存,从 Nix 配置中移除时自动清理。

管理插件

hermes plugins # 统一交互式 UI
hermes plugins list # 表格:已启用 / 已禁用 / 未启用
hermes plugins install user/repo # 从 Git 安装,然后提示 Enable? [y/N]
hermes plugins install user/repo --enable # 安装并启用(无提示)
hermes plugins install user/repo --no-enable # 安装但保持禁用(无提示)
hermes plugins update my-plugin # 拉取最新版本
hermes plugins remove my-plugin # 卸载
hermes plugins enable my-plugin # 添加到允许列表
hermes plugins disable my-plugin # 从允许列表移除 + 添加到禁用列表

交互式 UI

无参数运行 hermes plugins 会打开一个复合交互式界面:

Plugins
↑↓ navigate SPACE toggle ENTER configure/confirm ESC done

General Plugins
→ [✓] my-tool-plugin — Custom search tool
[ ] webhook-notifier — Event hooks
[ ] disk-cleanup — Auto-cleanup of ephemeral files [bundled]

Provider Plugins
Memory Provider ▸ honcho
Context Engine ▸ compressor
  • 通用插件区域 — 复选框,用 SPACE 切换。选中 = 在 plugins.enabled 中,未选中 = 在 plugins.disabled 中(明确关闭)。
  • 提供商插件区域 — 显示当前选择。按 ENTER 进入单选选择器,从中选择一个活跃的提供商。
  • 内置插件在同一列表中显示,带有 [bundled] 标签。

提供商插件选择保存到 config.yaml

memory:
provider: "honcho" # 空字符串 = 仅内置

context:
engine: "compressor" # 默认内置压缩器

已启用 vs. 已禁用 vs. 两者都不是

插件占据三种状态之一:

状态含义plugins.enabled 中?plugins.disabled 中?
enabled下次会话时加载
disabled明确关闭——即使在 enabled 中也不会加载(无关)
not enabled已发现但从未选择启用

新安装或内置插件的默认状态为 not enabledhermes plugins list 显示所有三种不同状态,便于区分明确关闭的和只是等待启用的插件。

在运行中的会话里,/plugins 显示当前已加载的插件。

注入消息

插件可以使用 ctx.inject_message() 向活跃对话注入消息:

ctx.inject_message("New data arrived from the webhook", role="user")

签名: ctx.inject_message(content: str, role: str = "user") -> bool

工作原理:

  • 如果 Agent 空闲(等待用户输入),消息被排队为下一个输入并开始新一轮。
  • 如果 Agent 处于轮次中(正在运行),消息会中断当前操作——与用户输入新消息并按 Enter 相同。
  • 对于非 "user" 角色,内容前缀为 [role](例如 [system] ...)。
  • 如果消息成功排队返回 True,如果没有 CLI 引用则返回 False(例如在网关模式下)。

这使得远程控制查看器、消息桥接或 webhook 接收器等插件能够从外部来源向对话注入消息。

备注

inject_message 仅在 CLI 模式下可用。在网关模式下,没有 CLI 引用,该方法返回 False

完整的 handler 契约、schema 格式、钩子行为、错误处理和常见错误参见**完整指南**。