← 返回总览

⚕️ Hermes Agent

Nous Research — The Self-Improving AI Agent

Conversation Loop Self-Improving Python 🔥 Background Review Fork 76 Tools 30+ Platforms 25 Skill Categories
🐍 1,724 Python files 🔧 76 tools 💬 30+ platforms 🧠 Self-improving memory 📚 25 skill categories 🔌 16 plugin types

一、核心架构

Hermes Agent 整体架构 — Self-Improving Conversation Loop ┌──────────────────────────────────────────────────────────────────────────┐ │ AIAgent (核心类, ~12k LOC) │ │ │ │ ┌────────────────────────────────────────────────────────────────────┐ │ │ │ run_conversation() (主循环) │ │ │ │ │ │ │ │ user_message → [system_prompt] → LLM call → tool_calls → │ │ │ │ → [tool dispatch] → results → [compression check] → │ │ │ │ → [memory nudge] → [skill nudge] → reply → │ │ │ │ → [background review fork] ──────────────────────┐ │ │ │ │ │ │ │ │ │ ←────────────── next user message ←───────────────┘ │ │ │ └────────────────────────────────────────────────────────────────────┘ │ │ │ │ ┌──────────────────┐ ┌──────────────────┐ ┌──────────────────┐ │ │ │ System Prompt │ │ Tool Registry │ │ Memory Manager │ │ │ │ Builder │ │ (auto-discover) │ │ (multi-provider) │ │ │ │ 3-tier assembly │ │ 76 tools │ │ builtin+external │ │ │ └──────────────────┘ └──────────────────┘ └──────────────────┘ │ │ │ │ ┌──────────────────┐ ┌──────────────────┐ ┌──────────────────┐ │ │ │ Context Engine │ │ Skill System │ │ Credential Pool │ │ │ │ (pluggable) │ │ (25 categories) │ │ (multi-key) │ │ │ │ compressor/LCM │ │ auto-discover │ │ rate limit guard │ │ │ └──────────────────┘ └──────────────────┘ └──────────────────┘ │ └──────────────────────────────────────────────────────────────────────────┘ ┌──────────────────────────────────────────────────────────────────────────┐ │ Background Review Fork (🔥 核心创新) │ │ │ │ 每轮对话后,fork 一个 AIAgent daemon thread: │ │ ┌────────────────────────────────────────────────────────────────────┐ │ │ │ 1. 继承父 agent 的 runtime (provider, model, credentials, cache) │ │ │ │ 2. 白名单限制: 只能调用 memory + skill_manage 工具 │ │ │ │ 3. 审查对话 → 决定是否保存记忆/更新技能 │ │ │ │ 4. 写入直接落地 (memory/skill stores) │ │ │ │ 5. 主对话的 prompt cache 完全不受影响 │ │ │ └────────────────────────────────────────────────────────────────────┘ │ │ │ │ 触发条件: │ │ • memory_nudge_interval (默认每 N 轮触发记忆审查) │ │ • skill_nudge_interval (默认每 N 轮触发技能审查) │ │ • 用户显式要求 │ └──────────────────────────────────────────────────────────────────────────┘ ┌──────────────────────────────────────────────────────────────────────────┐ │ Gateway (消息平台适配层) │ │ Telegram / Discord / WhatsApp / Slack / Signal / Matrix / ... 30+ │ │ + CLI (hermes) + TUI (Ink/React) + ACP (VS Code/Zed/JetBrains) │ └──────────────────────────────────────────────────────────────────────────┘

二、🔥 自我改进循环 (核心创新)

这是 Hermes 最核心的差异化特性,也是所有 Agent 项目中唯一的自进化设计。

Background Review 机制详解

每轮对话后: 主对话 ──→ run_conversation() ──→ 回复用户 │ └──→ spawn_background_review_thread() │ └──→ daemon thread (不阻塞主对话) │ └──→ fork AIAgent (继承父 runtime) │ ├─ Memory Review: │ "用户是否揭示了个人偏好、工作风格、期望?" │ → memory tool 写入 MEMORY.md │ ├─ Skill Review: │ "是否有值得更新的技能?" │ 1. UPDATE 当前加载的 skill │ 2. UPDATE 现有 umbrella skill │ 3. ADD support file (references/templates/scripts) │ 4. CREATE 新 class-level umbrella │ └─ 结果摘要 → "💾 Self-improvement review: Memory updated" 关键安全措施: • 工具白名单: 只允许 memory + skill_manage • 自动拒绝危险命令: _bg_review_auto_deny • skip_memory=True: 不触碰外部 memory provider • 继承父 cached system prompt: 命中 prefix cache (~26% 成本降低) • suppress_status_output=True: 不泄露内部状态给用户

Skill Review Prompt 的精妙设计

这不是简单的"保存对话",而是一套精心设计的知识管理哲学:

三、系统提示词架构

三层组装 + 前缀缓存复用

build_system_prompt_parts() 返回三个层级: ┌──────────────────────────────────────────────┐ │ STABLE (跨 turn 不变, 命中 prefix cache) │ │ │ │ • SOUL.md / DEFAULT_AGENT_IDENTITY │ │ • Tool guidance (per-model) │ │ • Computer-use guidance │ │ • Subscription block │ │ • Tool-use enforcement guidance │ │ • Skills prompt (skill index) │ │ • Environment hints │ │ • Platform hints │ │ • Alibaba model-name workaround │ ├──────────────────────────────────────────────┤ │ CONTEXT (用户定义内容) │ │ │ │ • caller system_message │ │ • Context files (AGENTS.md, .cursorrules...) │ │ • Context threat scanning (注入检测!) │ ├──────────────────────────────────────────────┤ │ VOLATILE (每 turn 重建) │ │ │ │ • Memory snapshot │ │ • USER.md profile │ │ • External memory provider block │ │ • Timestamp / session / model / provider │ └──────────────────────────────────────────────┘ 🔥 Context Threat Scanning — 上下文注入检测: 扫描 AGENTS.md / .cursorrules 等用户文件中的: • "ignore previous instructions" (prompt injection) • "do not tell the user" (deception) • hidden unicode characters (不可见字符注入) • curl exfiltration patterns (数据外泄) • 不可见 Unicode 字符检测 (零宽字符等)

四、76 个工具全景

类别工具数量
🌐 Webweb_search, web_extract2
💻 Terminalterminal, process2
📁 Filesread_file, write_file, patch, search_files4
👁️ Visionvision_analyze, image_generate2
📚 Skillsskills_list, skill_view, skill_manage3
🖱️ Browsernavigate, snapshot, click, type, scroll, back, press, get_images, vision, console, cdp, dialog12
🔊 TTStext_to_speech1
🧠 Memorymemory, todo2
🔍 Sessionsession_search1
❓ Clarifyclarify1
⚡ Code/Delegateexecute_code, delegate_task2
⏰ Croncronjob1
📨 Messagingsend_message1
🏠 Smart Homeha_list_entities, ha_get_state, ha_list_services, ha_call_service4
📋 Kanbanshow, list, complete, block, heartbeat, comment, create, link, unblock9
🖥️ Computer Usecomputer_use1
🎨 Image/Video Genimage_generation, video_generation2
💬 Discorddiscord_tool1
📄 Feishu Docsfeishu_doc, feishu_drive2
🔍 XAI Searchx_search1
🧪 Vision Transcriptiontranscription, voice_mode2

五、记忆系统

MemoryManager — 多 Provider 架构

MemoryManager (单外部 Provider 限制) ┌──────────────────────────────────────┐ │ Built-in Memory │ │ MEMORY.md + USER.md (文件系统) │ │ • memory(action="add/search/...") │ │ • memory_nudge_interval 自动触发 │ │ • background review 写入 │ ├──────────────────────────────────────┤ │ External Provider (选一) │ │ • Honcho │ │ • Hindsight │ │ • Mem0 │ │ • Supermemory │ │ • 自定义 (plugins/memory/) │ ├──────────────────────────────────────┤ │ Lifecycle Hooks │ │ • initialize() — 连接+预热 │ │ • system_prompt_block() — 注入 │ │ • prefetch(query) — 背景检索 │ │ • sync_turn() — 异步写入 │ │ • on_session_end() — 会话结束提取 │ │ • on_pre_compress() — 压缩前提取 │ │ • on_delegation() — 子代理观察 │ └──────────────────────────────────────┘ Context Fencing (安全机制): • sanitize_context() — 剥离注入的 memory-context 标签 • "[System note: recalled memory, NOT new user input]" • 防止 memory provider 输出伪装成用户输入

六、30+ 消息平台

Gateway Platforms (运行时发现)

类别平台
即时通讯Telegram, WhatsApp, Signal, Discord, Slack, Matrix, Mattermost, iMessage (BlueBubbles)
中国企业飞书, 企业微信, 微信, 钉钉, QQ Bot, 腾讯元宝
邮件/短信Email, SMS
智能家居Home Assistant
API/WebhookAPI Server, Webhook
微软MS Graph Webhook
IDE 集成ACP (VS Code / Zed / JetBrains)
TUIInk (React) 终端 UI

七、上下文压缩

ContextEngine — 可插拔压缩引擎

ContextEngine ABC (可插拔): ┌──────────────────────┐ │ ContextCompressor │ ← 默认,用 LLM 生成摘要 │ (built-in) │ │ • should_compress() │ │ • compress() │ │ • protect_first_n │ ← 保留前 N 条非系统消息 └──────────────────────┘ ┌──────────────────────┐ │ LCM Engine │ ← 第三方,DAG 构建上下文 │ (plugins/context/) │ │ • 可暴露额外工具 │ 如 lcm_grep └──────────────────────┘ ┌──────────────────────┐ │ 自定义引擎 │ ← 放到 plugins/context_engine// │ config: context.engine └──────────────────────┘ 生命周期: on_session_start() → update_from_response() × N → should_compress()? → compress() → on_session_end()

八、25 个技能分类

分类用途
software-development代码开发、调试、部署
research网络研究、信息收集
devopsCI/CD、容器管理、监控
data-science数据分析、可视化、ML
creative写作、设计、内容生成
productivity任务管理、日程、笔记
email邮件撰写、整理
githubPR、Issue、代码审查
smart-homeHome Assistant 控制
applemacOS/iOS 集成
diagramming图表、流程图生成
gaming游戏辅助
domain垂直领域专家
red-teaming安全测试、对抗
mlops模型训练、部署、监控
social-media社交平台操作
note-taking笔记管理
media音视频处理
gifsGIF 生成/搜索
autonomous-ai-agents自主代理协作
mcpMCP 协议工具
inference-sh推理服务集成
dogfood自用/测试技能
yuanbao腾讯元宝集成
🎯 Hermes 对你构建 Agent 的启示:

九、关键源文件

文件作用行数
run_agent.pyAIAgent 核心类 + 主循环~12,000
agent/conversation_loop.py对话循环 (从 run_agent 提取)~3,900
agent/background_review.py🔥 自我改进循环~570
agent/system_prompt.py系统提示词三层组装~200
agent/prompt_builder.py身份、平台、技能、上下文文件~300
agent/context_engine.py可插拔上下文压缩引擎~150
agent/memory_manager.py多 Provider 记忆管理器~200
agent/memory_provider.py记忆 Provider ABC~100
agent/tool_executor.py工具执行 (串行+并行)~300
agent/tool_guardrails.py工具安全防护~200
model_tools.py工具发现 + handle_function_call~500
toolsets.py工具集定义~200
cli.pyCLI 交互 (~11k LOC)~11,000