乐于分享
好东西不私藏

Hermes Agent 源码-整体架构

Hermes Agent 源码-整体架构

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-supplied system_message plus context files
(AGENTS.md / .cursorrules / etc.) discovered under
TERMINAL_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 from run_agent.AIAgent.

This is the biggest single chunk pulled out of
run_agent.py: the
roughly 3,900-line :func:
run_conversation 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 parent
AIAgent instance as its first
argument (
agent) and accesses its state via attribute lookup.
_ra().AIAgent.run_conversation is now a thin forwarder."""


对话循环流程:



  1. build_turn_context() — 每轮设置(system prompt、消息清理、todo 注入、记忆预取)

  2. while 循环(max_iterations 限制,默认 90 次)

  3. API 调用 → 解析 tool calls → 执行 tools → 追加结果 → 重复

  4. 处理:中断 / 预算耗尽 / 上下文压缩 / 截断响应





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, external
memory provider block, timestamp line.

Joined into a single string by :func:
build_system_prompt and
cached on
agent._cached_system_prompt for 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."""







层级内容重建频率
StableAgent 身份、工具指导、技能提示、环境信息对话生命周期内不变
ContextAGENTS.md、.cursorrules 等每次新会话
Volatile记忆快照、用户画像、时间戳每轮重建




4. 工具注册系统 (tools/registry.py, 589 行)



📄 tools/registry.py (第 1-15 行)


"""Central registry for all hermes-agent tools.

Each tool file calls
registry.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.py16,292网关运行器
cli.py13,973交互式 CLI
hermes_cli/main.py12,227CLI 入口
hermes_cli/config.py6,530配置系统
run_agent.py5,405Agent 核心
hermes_state.py4,777会话管理
gateway/platforms/base.py4,884平台适配器基类
hermes_cli/plugins.py2,046插件系统
trajectory_compressor.py1,579上下文压缩
toolsets.py912工具集定义
model_tools.py1,229工具分发
tools/registry.py589工具注册




六、设计模式总结












模式在哪里为什么
转发器AIAgentagent/ 子模块解耦核心循环与实现细节
Mixin 组合HermesCLI, GatewayRunner横切关注点分离
自注册工具模块插件无需修改核心
三层 PromptSystem 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