Skip to content

Hermes Agent - No.20260603

核心定位

一个自我改进的 AI Agent 框架 —— 通过 LLM + 工具调用循环完成任务,具备技能自我进化、会话记忆、多平台接入、子任务委派等能力。

项目信息

字段内容
出品方Nous Research
GitHubhttps://github.com/NousResearch/hermes-agent
协议MIT
核心语言Python (95%+)
当前版本v0.15.1
代码规模~12k LOC (run_agent) + ~11k LOC (cli) + ~5k LOC (conversation_loop) + 70+ 工具 + 15+ 平台适配器

核心能力

  • 对话工具循环 (Agent Loop) — 核心引擎:LLM 生成 → 工具调用 → 结果反馈 → 继续,直到模型给出最终回答
  • 工具注册表 (Tool Registry) — 声明式工具发现:每个工具文件自注册 schema + handler,框架自动扫描装载
  • 工具集系统 (Toolsets) — 按场景(CLI/Telegram/Discord)组合不同工具子集,运行时动态过滤
  • 消息网关 (Gateway) — 单进程多平台:Telegram、Discord、Slack、WhatsApp、Signal 等 15+ 平台适配器并行运行
  • 子任务委派 (Delegation) — 隔离环境下 spawn 子 Agent 并发执行,支持单任务和批量任务
  • 技能系统 (Skills) — 程序化记忆:Agent 完成复杂任务后自动创建技能,下次复用
  • 上下文压缩 (Compression) — 会话过长时自动摘要压缩早期对话,维持上下文窗口
  • 配置文件缓存 (Prompt Caching) — 系统提示词持久化到 SQLite,保持 Anthropic prefix cache 命中
  • 定时任务 (Cron) — 内置调度器,自然语言定义,支持跨平台投递结果
  • 看板协作 (Kanban) — 多 Agent 协同工作队列,SQLite 持久化,支持多 profile 并发

架构概览

系统分为 5 层:

层级职责关键模块
前端入口层用户交互界面hermes_cli/main.py, cli.py, ui-tui/, gateway/run.py
Agent 核心层对话循环 + 策略run_agent.py, agent/conversation_loop.py, agent/agent_init.py
工具编排层工具发现 + 调度 + schema 管理model_tools.py, toolsets.py, tools/registry.py
工具实现层具体工具逻辑tools/*.py (70+ 工具文件), tools/environments/
基础设施层状态 + 配置 + 日志hermes_constants.py, hermes_state.py, hermes_logging.py, hermes_cli/config.py

技术栈

领域选型
语言Python 3.11+ (核心), TypeScript (TUI/Dashboard)
LLM SDKOpenAI Python SDK (openai==2.24.0) 作为统一 transport
CLI 交互prompt_toolkit + Rich
TUIInk (React for CLI) + JSON-RPC stdio
Web DashboardFastAPI + React (xterm.js 嵌套 TUI)
数据存储SQLite (会话/看板/定时任务)
异步asyncio (Gateway), 同步 (Agent Loop)
包管理uv + setuptools
测试pytest + subprocess 隔离 + xdist 并行
消息平台python-telegram-bot, discord.py, slack-bolt 等

设计哲学

  1. 注册表驱动 — 工具、平台、Provider 全部自注册,框架只做发现和分发,新增不改核心
  2. 同步 Agent Loop — 对话循环全同步(无 await),简化推理和调试,异步仅在 Gateway I/O 层
  3. Prompt Cache 不能断 — 会话期间不改变系统提示/工具集,保证 Anthropic 缓存命中率
  4. Profile 隔离 — 通过 HERMES_HOME 环境变量实现多实例完全隔离(配置/密钥/记忆/会话)
  5. 供应链安全 — 所有依赖精确 pin 版本(==),GitHub Actions pin SHA,应对 PyPI 投毒
  6. 延迟安装 — 非核心依赖(各 Provider SDK)通过 tools/lazy_deps.py 按需安装,减小攻击面

模块依赖链

从底到顶的核心调用关系:

┌─────────────────────────────────────────────────────────────────────┐
│                        前端入口层                                      │
│  hermes_cli/main.py ─── cli.py ─── gateway/run.py ─── ui-tui/      │
│         │                  │              │                          │
│         │      实例化 AIAgent + 运行对话                               │
│         ▼                  ▼              ▼                          │
├─────────────────────────────────────────────────────────────────────┤
│                       Agent 核心层                                     │
│  run_agent.py::AIAgent                                              │
│         │                                                           │
│         ├── __init__ → agent/agent_init.py (60+ 参数初始化)           │
│         ├── run_conversation → agent/conversation_loop.py           │
│         │         │                                                 │
│         │         ├── 构建/恢复 system prompt                        │
│         │         ├── while 循环: LLM call → tool_calls → dispatch  │
│         │         ├── 中断检测 / budget 消耗                         │
│         │         └── 上下文压缩 / 重试 / fallback                   │
│         │                                                           │
│         └── _build_system_prompt → agent/system_prompt.py           │
├─────────────────────────────────────────────────────────────────────┤
│                       工具编排层                                       │
│  model_tools.py                                                     │
│         │                                                           │
│         ├── get_tool_definitions()  ← toolsets.py (toolset 定义)    │
│         │         │                                                 │
│         │         └── registry.get_definitions(tool_names)          │
│         │                                                           │
│         └── handle_function_call() → registry.dispatch()            │
│                                                                     │
│  toolsets.py::TOOLSETS + _HERMES_CORE_TOOLS                         │
│         │                                                           │
│         └── resolve_toolset() 递归展开 includes                      │
├─────────────────────────────────────────────────────────────────────┤
│                       工具注册层                                       │
│  tools/registry.py::ToolRegistry (单例)                              │
│         │                                                           │
│         ├── discover_builtin_tools(): AST 扫描 → import → 触发注册  │
│         ├── register(): 存储 ToolEntry (schema + handler + check)   │
│         ├── get_definitions(): 过滤 check_fn → 返回 OpenAI 格式     │
│         └── dispatch(): name → handler(args) → JSON string          │
├─────────────────────────────────────────────────────────────────────┤
│                       工具实现层                                       │
│  tools/*.py (70+ 文件, 每个 import 时调用 registry.register())       │
│         │                                                           │
│         ├── terminal_tool.py    — 终端命令执行                       │
│         ├── file_tools.py       — 文件读写搜索                       │
│         ├── browser_tool.py     — 浏览器自动化 (Playwright)          │
│         ├── web_tools.py        — 网络搜索/内容提取                   │
│         ├── delegate_tool.py    — 子 Agent 委派                      │
│         ├── code_execution_tool — Python 沙盒执行                    │
│         ├── mcp_tool.py         — MCP 服务器桥接                     │
│         └── ...                                                     │
├─────────────────────────────────────────────────────────────────────┤
│                       基础设施层                                       │
│  hermes_constants.py  — get_hermes_home() / profile 路径管理         │
│  hermes_state.py      — SessionDB (SQLite + FTS5 全文搜索)          │
│  hermes_logging.py    — agent.log / errors.log / gateway.log        │
│  hermes_cli/config.py — DEFAULT_CONFIG + YAML 配置合并               │
└─────────────────────────────────────────────────────────────────────┘