website/docs/developer-guide/gateway-internals.md
The messaging gateway is the long-running process that connects Hermes to 20+ external messaging platforms through a unified architecture.
| File | Purpose |
|---|---|
gateway/run.py | GatewayRunner — main loop, slash commands, message dispatch (large file; check git for current LOC) |
gateway/session.py | SessionStore — conversation persistence and session key construction |
gateway/delivery.py | Outbound message delivery to target platforms/channels |
gateway/pairing.py | DM pairing flow for user authorization |
gateway/channel_directory.py | Maps chat IDs to human-readable names for cron delivery |
gateway/hooks.py | Hook discovery, loading, and lifecycle event dispatch |
gateway/mirror.py | Cross-session message mirroring for send_message |
gateway/status.py | Token lock management for profile-scoped gateway instances |
gateway/builtin_hooks/ | Extension point for always-registered hooks (none shipped) |
gateway/platforms/ | Platform adapters (one per messaging platform) |
┌─────────────────────────────────────────────────┐
│ GatewayRunner │
│ │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ Telegram │ │ Discord │ │ Slack │ │
│ │ Adapter │ │ Adapter │ │ Adapter │ │
│ └────┬─────┘ └────┬─────┘ └────┬─────┘ │
│ │ │ │ │
│ └─────────────┼─────────────┘ │
│ ▼ │
│ _handle_message() │
│ │ │
│ ┌───────────┼───────────┐ │
│ ▼ ▼ ▼ │
│ Slash command AIAgent Queue/BG │
│ dispatch creation sessions │
│ │ │
│ ▼ │
│ SessionStore │
│ (SQLite persistence) │
└───────┴─────────────┴─────────────┴─────────────┘
When a message arrives from any platform:
MessageEvent/approve, /deny, /stop → bypass guard (dispatched inline)_session_key_for_source() (format: agent:main:{platform}:{chat_type}:{chat_id})/stop, /statusAIAgent instance and run conversationSession keys encode the full routing context:
agent:main:{platform}:{chat_type}:{chat_id}
For example: agent:main:telegram:private:123456789
Thread-aware platforms (Telegram forum topics, Discord threads, Slack threads) may include thread IDs in the chat_id portion. Never construct session keys manually — always use build_session_key() from gateway/session.py.
When an agent is actively running, incoming messages pass through two sequential guards:
Level 1 — Base adapter (gateway/platforms/base.py): Checks _active_sessions. If the session is active, queues the message in _pending_messages and sets an interrupt event. This catches messages before they reach the gateway runner.
Level 2 — Gateway runner (gateway/run.py): Checks _running_agents. Intercepts specific commands (/stop, /new, /queue, /status, /approve, /deny) and routes them appropriately. Everything else triggers running_agent.interrupt().
Commands that must reach the runner while the agent is blocked (like /approve) are dispatched inline via await self._message_handler(event) — they bypass the background task system to avoid race conditions.
The gateway uses a multi-layer authorization check, evaluated in order:
TELEGRAM_ALLOW_ALL_USERS) — if set, all users on that platform are authorizedTELEGRAM_ALLOWED_USERS) — comma-separated user IDsGATEWAY_ALLOW_ALL_USERS) — if set, all users across all platforms are authorizedAdmin: /pair
Gateway: "Pairing code: ABC123. Share with the user."
New user: ABC123
Gateway: "Paired! You're now authorized."
Pairing state is persisted in gateway/pairing.py and survives restarts.
All slash commands in the gateway flow through the same resolution pipeline:
resolve_command() from hermes_cli/commands.py maps input to canonical name (handles aliases, prefix matching)GATEWAY_KNOWN_COMMANDS_handle_message() dispatches based on canonical namegateway_config_gate on CommandDef)Commands that must NOT execute while the agent is processing are rejected early:
if _quick_key in self._running_agents:
if canonical == "model":
return "⏳ Agent is running — wait for it to finish or /stop first."
Bypass commands (/stop, /new, /approve, /deny, /queue, /status) have special handling.
The gateway reads configuration from multiple sources:
| Source | What it provides |
|---|---|
~/.hermes/.env | API keys, bot tokens, platform credentials |
~/.hermes/config.yaml | Model settings, tool configuration, display options |
| Environment variables | Override any of the above |
Unlike the CLI (which uses load_cli_config() with hardcoded defaults), the gateway reads config.yaml directly via YAML loader. This means config keys that exist in the CLI's defaults dict but not in the user's config file may behave differently between CLI and gateway.
Each messaging platform has an adapter in gateway/platforms/:
gateway/platforms/
├── base.py # BaseAdapter — shared logic for all platforms
├── telegram.py # Telegram Bot API (long polling or webhook)
├── discord.py # Discord bot via discord.py
├── slack.py # Slack Socket Mode
├── whatsapp.py # WhatsApp Business Cloud API
├── signal.py # Signal via signal-cli REST API
├── matrix.py # Matrix via mautrix (optional E2EE)
├── mattermost.py # Mattermost WebSocket API
├── email.py # Email via IMAP/SMTP
├── sms.py # SMS via Twilio
├── dingtalk.py # DingTalk WebSocket
├── feishu.py # Feishu/Lark WebSocket or webhook
├── wecom.py # WeCom (WeChat Work) callback
├── weixin.py # Weixin (personal WeChat) via iLink Bot API
├── bluebubbles.py # Apple iMessage via BlueBubbles macOS server
├── qqbot/ # QQ Bot (Tencent QQ) via Official API v2 (sub-package: adapter.py, crypto.py, keyboards.py, …)
├── yuanbao.py # Yuanbao (Tencent) DM/group adapter
├── feishu_comment.py # Feishu document/drive comment-reply handler
├── msgraph_webhook.py # Microsoft Graph change-notification webhook (Teams, Outlook, etc.)
├── webhook.py # Inbound/outbound webhook adapter
├── api_server.py # REST API server adapter
└── homeassistant.py # Home Assistant conversation integration
Adapters implement a common interface:
connect() / disconnect() — lifecycle managementsend_message() — outbound message deliveryon_message() — inbound message normalization → MessageEventAdapters that connect with unique credentials call acquire_scoped_lock() in connect() and release_scoped_lock() in disconnect(). This prevents two profiles from using the same bot token simultaneously.
Outgoing deliveries (gateway/delivery.py) handle:
send_message tool specifying telegram:-1001234567890, or the hermes send CLI wrapping the same tool for shell scriptsCron job deliveries are NOT mirrored into gateway session history — they live in their own cron session only. This is a deliberate design choice to avoid message alternation violations.
Gateway hooks are Python modules that respond to lifecycle events:
| Event | When fired |
|---|---|
gateway:startup | Gateway process starts |
session:start | New conversation session begins |
session:end | Session completes or times out |
session:reset | User resets session with /new |
agent:start | Agent begins processing a message |
agent:step | Agent completes one tool-calling iteration |
agent:end | Agent finishes and returns response |
command:* | Any slash command is executed |
Hooks are discovered from gateway/builtin_hooks/ (an extension point — currently empty in the shipped distribution; _register_builtin_hooks() is a no-op stub) and ~/.hermes/hooks/ (user-installed). Each hook is a directory with a HOOK.yaml manifest and handler.py.
When a memory provider plugin (e.g., Honcho) is enabled:
AIAgent per message with the session IDMemoryManager initializes the provider with the session contexthoncho_profile, viking_search) are routed through:AIAgent._invoke_tool()
→ self._memory_manager.handle_tool_call(name, args)
→ provider.handle_tool_call(name, args)
on_session_end() fires for cleanup and final data flushWhen a session is reset, resumed, or expires:
on_session_end() hook firesAIAgent runs a memory-only conversation turnThe gateway runs periodic maintenance alongside message handling:
The gateway runs as a long-lived process, managed via:
hermes gateway start / hermes gateway stop — manual controlsystemctl (Linux) or launchctl (macOS) — service management~/.hermes/gateway.pid — profile-scoped process trackingProfile-scoped vs global: start_gateway() uses profile-scoped PID files. hermes gateway stop stops only the current profile's gateway. hermes gateway stop --all uses global ps aux scanning to kill all gateway processes (used during updates).