词元之母TOK.MOM - 平台充值汇率 1:1 即 1 人民币充值到账 1 美元,支持一个 Key 调用近 600+ 海内外模型,限时特价模型低至 1 折,欢迎上岸!
| 文件 | 用途 |
|---|---|
gateway/run.py | GatewayRunner — 主循环、斜杠命令、消息分发(大文件;请查看 git 获取当前行数) |
gateway/session.py | SessionStore — 会话持久化与会话键构造 |
gateway/delivery.py | 向目标平台/频道投递出站消息 |
gateway/pairing.py | 用于用户授权的 DM 配对流程 |
gateway/channel_directory.py | 将聊天 ID 映射为可读名称,用于 cron 投递 |
gateway/hooks.py | Hook(钩子)发现、加载与生命周期事件分发 |
gateway/mirror.py | 为 send_message 提供跨会话消息镜像 |
gateway/status.py | 面向 profile 范围的 gateway 实例的 token 锁管理 |
gateway/builtin_hooks/ | 始终注册的 hook 扩展点(当前未内置任何 hook) |
gateway/platforms/ | 平台适配器(每个消息平台一个) |
┌─────────────────────────────────────────────────┐
│ GatewayRunner │
│ │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ Telegram │ │ Discord │ │ Slack │ │
│ │ Adapter │ │ Adapter │ │ Adapter │ │
│ └────┬─────┘ └────┬─────┘ └────┬─────┘ │
│ │ │ │ │
│ └─────────────┼─────────────┘ │
│ ▼ │
│ _handle_message() │
│ │ │
│ ┌───────────┼───────────┐ │
│ ▼ ▼ ▼ │
│ Slash command AIAgent Queue/BG │
│ dispatch creation sessions │
│ │ │
│ ▼ │
│ SessionStore │
│ (SQLite persistence) │
└───────┴─────────────┴─────────────┴─────────────┘MessageEvent/approve、/deny、/stop → 绕过守卫(内联分发)_session_key_for_source() 解析会话键(格式:agent:main:{platform}:{chat_type}:{chat_id})/stop、/status 等命令AIAgent 实例并运行对话agent:main:{platform}:{chat_type}:{chat_id}agent:main:telegram:private:123456789gateway/session.py 中的 build_session_key()。gateway/platforms/base.py):检查 _active_sessions。若会话处于活跃状态,将消息加入 _pending_messages 队列并设置中断事件。此级在消息到达 gateway runner 之前进行拦截。gateway/run.py):检查 _running_agents。拦截特定命令(/stop、/new、/queue、/status、/approve、/deny)并进行相应路由。其余所有消息触发 running_agent.interrupt()。/approve)通过 await self._message_handler(event) 内联分发 — 绕过后台任务系统以避免竞态条件。TELEGRAM_ALLOW_ALL_USERS)— 若设置,该平台所有用户均被授权TELEGRAM_ALLOWED_USERS)— 逗号分隔的用户 IDGATEWAY_ALLOW_ALL_USERS)— 若设置,所有平台的所有用户均被授权Admin: /pair
Gateway: "Pairing code: ABC123. Share with the user."
New user: ABC123
Gateway: "Paired! You're now authorized."gateway/pairing.py,重启后仍然有效。hermes_cli/commands.py 中的 resolve_command() 将输入映射为规范名称(处理别名、前缀匹配)GATEWAY_KNOWN_COMMANDS 进行比对_handle_message() 中的处理器根据规范名称进行分发CommandDef 上的 gateway_config_gate)/stop、/new、/approve、/deny、/queue、/status)具有特殊处理逻辑。| 来源 | 提供内容 |
|---|---|
~/.hermes/.env | API 密钥、bot token、平台凭据 |
~/.hermes/config.yaml | 模型设置、工具配置、显示选项 |
| 环境变量 | 覆盖上述任意配置 |
load_cli_config())不同,gateway 通过 YAML 加载器直接读取 config.yaml。这意味着存在于 CLI 默认值字典但不在用户配置文件中的配置键,在 CLI 和 gateway 之间可能表现不同。gateway/platforms/ 下均有对应适配器:gateway/platforms/
├── base.py # BaseAdapter — 所有平台的共享逻辑
├── telegram.py # Telegram Bot API(长轮询或 webhook)
├── discord.py # Discord bot(通过 discord.py)
├── slack.py # Slack Socket Mode
├── whatsapp.py # WhatsApp Business Cloud API
├── signal.py # Signal(通过 signal-cli REST API)
├── matrix.py # Matrix(通过 mautrix,可选 E2EE)
├── mattermost.py # Mattermost WebSocket API
├── email.py # 电子邮件(通过 IMAP/SMTP)
├── sms.py # 短信(通过 Twilio)
├── dingtalk.py # 钉钉 WebSocket
├── feishu.py # 飞书/Lark WebSocket 或 webhook
├── wecom.py # 企业微信(WeCom)回调
├── weixin.py # 微信(个人版,通过 iLink Bot API)
├── bluebubbles.py # Apple iMessage(通过 BlueBubbles macOS 服务端)
├── qqbot/ # QQ Bot(腾讯 QQ,通过官方 API v2,子包:adapter.py、crypto.py、keyboards.py 等)
├── yuanbao.py # 元宝(腾讯)私信/群组适配器
├── feishu_comment.py # 飞书文档/云盘评论回复处理器
├── msgraph_webhook.py # Microsoft Graph 变更通知 webhook(Teams、Outlook 等)
├── webhook.py # 入站/出站 webhook 适配器
├── api_server.py # REST API 服务器适配器
└── homeassistant.py # Home Assistant 对话集成connect() / disconnect() — 生命周期管理send_message() — 出站消息投递on_message() — 入站消息规范化 → MessageEventconnect() 中调用 acquire_scoped_lock(),在 disconnect() 中调用 release_scoped_lock()。这可防止两个 profile 同时使用同一 bot token。gateway/delivery.py)处理以下场景:send_message 工具指定 telegram:-1001234567890,或通过 hermes send CLI 封装同一工具供 shell 脚本使用| 事件 | 触发时机 |
|---|---|
gateway:startup | Gateway 进程启动时 |
session:start | 新对话会话开始时 |
session:end | 会话完成或超时时 |
session:reset | 用户通过 /new 重置会话时 |
agent:start | Agent 开始处理消息时 |
agent:step | Agent 完成一次工具调用迭代时 |
agent:end | Agent 完成并返回响应时 |
command:* | 任意斜杠命令被执行时 |
gateway/builtin_hooks/(扩展点 — 当前发行版中为空;_register_builtin_hooks() 是一个空操作存根)和 ~/.hermes/hooks/(用户安装)中发现。每个 hook 是一个包含 HOOK.yaml 清单和 handler.py 的目录。AIAgentMemoryManager 使用会话上下文初始化提供者honcho_profile、viking_search)通过以下路径路由:AIAgent._invoke_tool()
→ self._memory_manager.handle_tool_call(name, args)
→ provider.handle_tool_call(name, args)on_session_end() 触发以进行清理和最终数据刷写on_session_end() hook 触发AIAgent 运行仅含内存的对话轮次hermes gateway start / hermes gateway stop — 手动控制systemctl(Linux)或 launchctl(macOS)— 服务管理~/.hermes/gateway.pid — 面向 profile 的进程追踪start_gateway() 使用 profile 范围的 PID 文件。hermes gateway stop 仅停止当前 profile 的 gateway。hermes gateway stop --all 使用全局 ps aux 扫描来终止所有 gateway 进程(用于更新时)。