Hermes Agent 源码解析 · 第 1 讲
Hermes Agent 源码解析 第 1 讲:整体架构概览 基于 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: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 │ │ Sche…