Hermes Agent 源码解析
第 1 讲:整体架构概览
基于 Hermes Agent v0.16.0 源码 · 2026-06-14
一、Hermes 是什么
Hermes Agent 是一个开源 AI Agent 框架,核心特点:
- 同一个 Agent 内核运行在 CLI、消息网关(Telegram/Discord/微信等 20+ 平台)、TUI 桌面端
- 跨会话记忆(memory + skills),越用越聪明
- 插件化扩展,核心保持精简,能力长在边缘
- 多模型提供商,20+ provider 随时切换
📦 源码仓库
https://github.com/NousResearch/hermes-agent
本地源码位置:~/.hermes/hermes-agent/
总代码量:约 45 万行 Python
二、核心设计哲学
设计原则 1:核心是窄腰,能力长在边缘
每条设计决策都围绕两个不变量:
不变量 1:Prompt 缓存神圣不可侵犯
长对话每轮复用缓存的前缀。任何在对话中途修改上下文、切换工具集或重建 system prompt 的行为都会使缓存失效,成本倍增。
📄 agent/system_prompt.py (第 1-22 行)
"""System-prompt assembly for :class:AIAgent.
The agent's system prompt is built once per session and reused across all
turns — only context compression triggers a rebuild. This keeps the
upstream prefix cache warm.
Three tiers are joined with \\n\\n:
- `
stable— identity, tool guidance, skills prompt,- environment hints, platform hints, model-family guidance.
context— caller-suppliedsystem_messageplus context files
(AGENTS.md / .cursorrules / etc.) discovered underTERMINAL_CWD.volatile— memory snapshot, USER.md profile, external memory
provider block, timestamp/session/model/provider line.
Pure helpers that read the agent's state. AIAgent keeps thin forwarders."""
关键设计:system prompt 分为三层,在对话生命周期内字节级稳定。Hermes 的注释明确写了:"Hermes never re-renders parts of this string mid-session — that's the only way to keep upstream prompt caches warm across turns."
不变量 2:核心精简,扩展在外
每个新增的 model tool 都会发送到每次 API 调用,所以新 tool 的门槛很高。优先顺序:
扩展现有 tool → CLI 命令 + skill → 服务门控 tool → 插件 → MCP 服务器 → 新核心 tool(最后手段)
三、整体架构
┌─────────────────────────────────────────────────────┐
│ Hermes Agent v0.16.0 │
├─────────────────────────────────────────────────────┤
│ │
│ ┌──────────┐ ┌──────────┐ ┌──────────────┐ │
│ │ CLI / │ │ Gateway │ │ Cron │ │
│ │ TUI │ │ Runner │ │ Scheduler │ │
│ └────┬─────┘ └────┬─────┘ └──────┬───────┘ │
│ │ │ │ │
│ └───────────────┼─────────────────┘ │
│ │ │
│ ┌────────▼────────┐ │
│ │ AIAgent │ ◄── Agent 核心 │
│ │ (run_agent.py) │ │
│ └────────┬────────┘ │
│ │ │
│ ┌───────────────┼───────────────┐ │
│ │ │ │ │
│ ┌────▼────┐ ┌──────▼──────┐ ┌────▼────┐ │
│ │ Tool │ │ System │ │ Session │ │
│ │ Registry│ │ Prompt │ │ DB │ │
│ │ │ │ Builder │ │ │ │
│ └─────────┘ └─────────────┘ └─────────┘ │
│ │ │ │ │
│ ┌────▼────┐ ┌──────▼──────┐ ┌────▼────┐ │
│ │Toolsets │ │ 3-tier │ │ SQLite │ │
│ │ │ │ Prompt │ │ + FTS5 │ │
│ └─────────┘ └─────────────┘ └─────────┘ │
│ │
│ ┌──────────────────────────────────────────┐ │
│ │ Plugin System │ │
│ │ Platforms(20+) │ Providers(20+) │ Hooks │ │
│ └──────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────┘
四、核心模块深度解析
1. run_agent.py — Agent 核心 (5,405 行)
这是 Hermes 的心脏。让我们看看它的结构:
📄 run_agent.py (第 1-21 行)
#!/usr/bin/env python3
"""
AI Agent Runner with Tool Calling
This module provides a clean, standalone agent that can execute AI models
with tool calling capabilities. It handles the conversation loop, tool execution,
and response management.
Features:
- Automatic tool calling loop until completion
- Configurable model parameters
- Error handling and recovery
- Message history management
- Support for multiple model providers
- Usage:
- from run_agent import AIAgent
-
- agent = AIAgent(base_url="http://localhost:30000/v1", model="claude-opus-4-20250514")
- response = agent.run_conversation("Tell me about the latest Python updates")
- """
关键设计模式:转发器模式
5400 行的文件大部分是 thin forwarders,真正的逻辑委托给 agent/ 子模块:
| 方法 | 委托到 |
|---|---|
__init__() | agent.agent_init.init_agent() |
run_conversation() | agent.conversation_loop.run_conversation() |
_build_system_prompt() | agent.system_prompt |
2. 对话循环 (agent/conversation_loop.py, 4,252 行)
这是 Hermes 最核心的循环。让我们看源码:
📄 agent/conversation_loop.py (第 1-15 行)
"""The agent conversation loop — extracted fromrun_agent.AIAgent.
This is the biggest single chunk pulled out ofrun_agent.py: therun_conversation
roughly 3,900-line :func:body that drives one user
turn through the agent (model call, tool dispatch, retries, fallbacks,
compression, post-turn hooks, background memory/skill review nudges).
The function takes the parentAIAgentinstance as its first
argument (agent) and accesses its state via attribute lookup._ra().AIAgent.run_conversationis now a thin forwarder."""
对话循环流程:
build_turn_context()— 每轮设置(system prompt、消息清理、todo 注入、记忆预取)while循环(max_iterations限制,默认 90 次)- API 调用 → 解析 tool calls → 执行 tools → 追加结果 → 重复
- 处理:中断 / 预算耗尽 / 上下文压缩 / 截断响应
3. 三层 System Prompt 架构 (agent/system_prompt.py)
这是 Hermes 最精妙的设计之一。看源码:
📄 agent/system_prompt.py (第 62-79 行)
def build_system_prompt_parts(agent: Any, system_message: Optional[str] = None) -> Dict[str, str]:
"""Assemble the system prompt as three ordered parts.
Returns a dict with three keys:
*stable— identity, tool guidance, skills prompt,
environment hints, platform hints, model-family operational
guidance.
*context— context files (AGENTS.md, .cursorrules, etc.)
and caller-supplied system_message.
*volatile— memory snapshot, user profile, externalbuild_system_prompt
memory provider block, timestamp line.
Joined into a single string by :func:and
cached onagent._cached_system_promptfor the lifetime of the
AIAgent. Hermes never re-renders parts of this string mid-
session — that's the only way to keep upstream prompt caches
warm across turns."""
| 层级 | 内容 | 重建频率 |
|---|---|---|
| Stable | Agent 身份、工具指导、技能提示、环境信息 | 对话生命周期内不变 |
| Context | AGENTS.md、.cursorrules 等 | 每次新会话 |
| Volatile | 记忆快照、用户画像、时间戳 | 每轮重建 |
4. 工具注册系统 (tools/registry.py, 589 行)
📄 tools/registry.py (第 1-15 行)
"""Central registry for all hermes-agent tools.
Each tool file callsregistry.register()at module level to declare its
schema, handler, toolset membership, and availability check.model_tools.py`
queries the registry instead of maintaining its own parallel data structures.
Import chain (circular-import safe):
tools/registry.py (no imports from model_tools or tool files)
^
tools/*.py (import from tools.registry at module level)
^
model_tools.py (imports tools.registry + all tool modules)
^
run_agent.py, cli.py, batch_runner.py, etc."""
自注册机制:每个工具模块在导入时调用 registry.register() 自动注册。导入链设计为无循环依赖。
5. 工具集定义 (toolsets.py, 912 行)
📄 toolsets.py (第 31-76 行)
# Shared tool list for CLI and all messaging platform toolsets.
Edit this once to update all platforms simultaneously.
_HERMES_CORE_TOOLS = [
# Web
"web_search", "web_extract",
# Terminal + process management
"terminal", "process",
# File manipulation
"read_file", "write_file", "patch", "search_files",
# Vision + image generation
"vision_analyze", "image_generate",
# Skills
"skills_list", "skill_view", "skill_manage",
# Browser automation
"browser_navigate", "browser_snapshot", "browser_click",
"browser_type", "browser_scroll", "browser_back",
# ... 更多工具
# Kanban multi-agent coordination
"kanban_show", "kanban_list",
"kanban_complete", "kanban_block", "kanban_heartbeat",
# Computer use (macOS)
"computer_use",
]6. 会话管理 (hermes_state.py, 4,777 行)
📄 hermes_state.py (第 1-15 行)
#!/usr/bin/env python3
"""
SQLite State Store for Hermes Agent.
Provides persistent session storage with FTS5 full-text search, replacing
the per-session JSONL file approach. Stores session metadata, full message
history, and model configuration for CLI and gateway sessions.
Key design decisions:
- WAL mode for concurrent readers + one writer (gateway multi-platform)
- FTS5 virtual table for fast text search across all session messages
- Compression-triggered session splitting via parent_session_id chains
- Batch runner and RL trajectories are NOT stored here (separate systems)
- Session source tagging ('cli', 'telegram', 'discord', etc.) for filtering
- """
关键特性:
- WAL 模式(自动检测 NFS/SMB 回退到 DELETE journal)
- FTS5 全文搜索(不可用时回退到 LIKE 查询)
- 线程安全:Python 锁 +
BEGIN IMMEDIATE+ 抖动重试 - Schema 版本 v16,自动迁移
五、关键文件清单
| 文件 | 行数 | 职责 |
|---|---|---|
gateway/run.py | 16,292 | 网关运行器 |
cli.py | 13,973 | 交互式 CLI |
hermes_cli/main.py | 12,227 | CLI 入口 |
hermes_cli/config.py | 6,530 | 配置系统 |
run_agent.py | 5,405 | Agent 核心 |
hermes_state.py | 4,777 | 会话管理 |
gateway/platforms/base.py | 4,884 | 平台适配器基类 |
hermes_cli/plugins.py | 2,046 | 插件系统 |
trajectory_compressor.py | 1,579 | 上下文压缩 |
toolsets.py | 912 | 工具集定义 |
model_tools.py | 1,229 | 工具分发 |
tools/registry.py | 589 | 工具注册 |
六、设计模式总结
| 模式 | 在哪里 | 为什么 |
|---|---|---|
| 转发器 | AIAgent → agent/ 子模块 | 解耦核心循环与实现细节 |
| Mixin 组合 | HermesCLI, GatewayRunner | 横切关注点分离 |
| 自注册 | 工具模块 | 插件无需修改核心 |
| 三层 Prompt | System prompt | 保护 prompt 缓存 |
| LRU 缓存 | 配置、工具定义、Agent 实例 | 性能优化 |
| 懒初始化 | SessionDB、OpenAI client | 启动速度 |
| 优雅降级 | FTS5→LIKE、WAL→DELETE | 兼容性 |
| 线程安全 | RLock、WAL SQLite | 并发安全 |
📌 预告
下一讲深入 Agent 对话循环——从用户输入到 LLM 响应的完整链路,包括 tool call 的分发、上下文压缩触发、中断处理等核心机制。
基于 Hermes Agent v0.16.0 (commit 45f9099e) 源码分析
https://github.com/NousResearch/hermes-agent
夜雨聆风