Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

附录 A:核心类与模块索引

本索引按五层架构组织,覆盖 Hermes Agent 的核心文件和关键类/函数。行号基于 v0.8.0。

入口层

文件类/函数用途
hermes_cli/main.pymain()CLI 命令入口,解析子命令并路由
cli.pyrun_cli()交互式 TUI 主循环,prompt_toolkit 驱动
cli.py_handle_slash_command()Slash command 解析与分发
gateway/run.pyGatewayRunner461Gateway 主类,管理多平台连接
gateway/run.pyhandle_message()接收平台消息并路由到 AIAgent
batch_runner.pyBatchRunner批量轨迹生成,用于 RL 训练
cron/scheduler.pyCronSchedulercroniter 驱动的定时任务调度

编排层

文件类/函数用途
run_agent.pyAIAgent416核心编排器,9431 行,全系统心脏
run_agent.pyAIAgent.__init__()43350+ 参数,七阶段初始化决策链
run_agent.pyAIAgent.run_conversation()主循环:构建消息 → 调用模型 → 执行工具 → 循环
run_agent.pyIterationBudget168线程安全的迭代预算,防止 agent 失控
run_agent.py_SafeWriter111Broken pipe 保护,包装 stdout/stderr
run_agent.py_spawn_background_review()1718后台线程启动 review agent,自动审查对话
run_agent.py_build_system_prompt()拼装 identity + memory + skills + context
model_tools.py_discover_tools()132导入所有工具模块,触发自注册
model_tools.pyget_tool_definitions()234按 enabled/disabled toolset 过滤工具 schema
model_tools.pyhandle_function_call()459路由工具调用到具体 handler
model_tools.py_run_async()81async→sync 桥接

编排支撑(agent/)

文件类/函数用途
agent/prompt_builder.pybuild_system_prompt()多块拼装 system prompt(983 行)
agent/prompt_builder.pybuild_skills_system_prompt()529扫描技能目录,生成索引列表
agent/prompt_builder.pySKILLS_GUIDANCE164引导模型自主创建和改进技能的指令
agent/prompt_caching.pyPrompt cache 优化策略(72 行)
agent/memory_manager.pyMemoryManager外部 memory provider 编排器;抽象目标包含 builtin provider(367 行)
agent/memory_provider.pyMemoryProvider记忆后端 ABC,17 个方法(231 行)
agent/builtin_memory_provider.pyBuiltinMemoryProvider内置记忆(MEMORY.md + USER.md)(114 行)
agent/context_compressor.pyContextCompressor上下文压缩,保护首尾消息(696 行)
agent/model_metadata.pyModelMetadata模型能力探测与缓存(1001 行)
agent/usage_pricing.pyUsagePricingToken 成本追踪(656 行)
agent/retry_utils.pyretry_with_backoff()指数退避重试策略(57 行)
agent/skill_utils.pyparse_frontmatter()52解析技能文件的 YAML frontmatter
agent/skill_utils.pyget_all_skills_dirs()226返回技能搜索路径列表
agent/skill_utils.pyskill_matches_platform()92平台条件过滤
agent/smart_model_routing.py智能模型路由选择(194 行)
agent/anthropic_adapter.pybuild_anthropic_client()Anthropic API 客户端构造(1373 行)
agent/credential_pool.pyCredentialPoolAPI key 池管理与轮换(1207 行)
agent/context_references.py上下文引用追踪
agent/redact.py敏感信息脱敏
agent/trajectory.py对话轨迹格式化(56 行)
agent/title_generator.py会话标题自动生成(125 行)

能力层

工具注册与调度

文件类/函数用途
tools/registry.pyToolRegistry48工具注册表单例(335 行)
tools/registry.pyToolEntry24工具元数据:名称、handler、toolset、schema
toolsets.py工具分组定义与可用性控制(637 行)

核心工具实现

文件类/函数用途
tools/terminal_tool.pyexecute_command()命令执行,6 种后端(1627 行)
tools/file_tools.pyread_file() / write_file()文件读写操作(835 行)
tools/browser_tool.pybrowse()浏览器自动化(2178 行)
tools/delegate_tool.pydelegate()子代理委托(978 行)
tools/mcp_tool.pyMCP 外部能力接入(2186 行)
tools/skills_tool.pyskill_view()787技能检索(1376 行)
tools/skill_manager_tool.pyskill_manage()569技能创建/改进/删除(742 行)
tools/code_execution_tool.pyexecute_code()Python/JS 代码执行(1347 行)
tools/session_search_tool.pysession_search()跨会话 FTS5 搜索(504 行)
tools/memory_tool.pyMemoryStore内置记忆存储(560 行)
tools/process_registry.pyProcessRegistry后台进程追踪(990 行)

状态层

文件类/函数用途
hermes_state.pySessionDB115SQLite 持久化:sessions + messages + FTS5(1304 行)
hermes_state.pyWAL 并发安全164BEGIN IMMEDIATE + 15 次 jitter retry

平台层

Gateway 核心

文件类/函数用途
gateway/platforms/base.pyBasePlatformAdapter470平台适配器 ABC(1696 行)
gateway/session.pySessionStore503会话管理(1081 行)
gateway/config.pyGateway 配置解析(957 行)

Gateway 平台适配器与支撑文件

文件平台
gateway/platforms/telegram.pyTelegram(webhook + polling)
gateway/platforms/discord.pyDiscord(多 guild、线程)
gateway/platforms/slack.pySlack(多 workspace、线程)
gateway/platforms/whatsapp.pyWhatsApp(Twilio 桥接)
gateway/platforms/signal.pySignal(signal-cli 桥接)
gateway/platforms/matrix.pyMatrix(E2E、spaces)
gateway/platforms/email.pyEmail(IMAP/SMTP)
gateway/platforms/feishu.py飞书
gateway/platforms/wecom.py企业微信
gateway/platforms/dingtalk.py钉钉
gateway/platforms/homeassistant.pyHome Assistant IoT
gateway/platforms/mattermost.pyMattermost
gateway/platforms/sms.pySMS(Twilio)
gateway/platforms/webhook.py自定义 Webhook
gateway/platforms/api_server.pyREST API 端点

ACP Adapter

文件类/函数用途
acp_adapter/entry.pyACP 入口模块(85 行)
acp_adapter/server.pyACP HTTP 服务器(726 行)
acp_adapter/session.pyACP 会话管理(475 行)
acp_adapter/tools.py工具能力暴露(214 行)
acp_adapter/events.pySSE 事件流(175 行)
acp_adapter/auth.py认证(24 行)
acp_adapter/permissions.py权限控制(77 行)

支撑文件

文件用途
hermes_constants.py全局常量(105 行)
hermes_logging.py日志配置
hermes_time.py时间工具函数
utils.py通用工具函数
mcp_serve.pyMCP 服务器模式(Hermes 作为 MCP server)
batch_runner.py批量轨迹生成(1287 行)
trajectory_compressor.py轨迹压缩(训练数据处理)
rl_cli.pyRL 训练 CLI