Skip to content

Gateway Core

源码版本v2026.7.20

Responsibility

The gateway is the bridge between the agent and the outside world: it receives messages from each platform (Telegram/Discord/Slack/Signal/WeChat...) and converts them into a unified MessageEvent fed to the agent; it turns the agent's streaming output into per-platform send actions. gateway/run.py is the main process of this layer (GatewayRunner), coordinating adapters, sessions, stream consumption, the delivery ledger, hooks, the cron heartbeat, and other subsystems.

Key Files

Data Flow

  1. main() (gateway/run.py:22965) starts GatewayRunner.
  2. _start_one_profile_adapter:9389 brings up each platform adapter (see Platform Adapters).
  3. Adapters receive raw platform events and convert them to unified MessageEvents (see gateway/platforms/base.py:1759).
  4. _handle_message (gateway/run.py:9947) dispatches: authz check, session resolution, slash command routing, or handoff to _handle_message_with_agent (gateway/run.py:11956).
  5. The latter calls the agent's run_conversation:588 and wires the streaming callback to GatewayStreamConsumer:83.
  6. Streaming output progressively edits the platform message via the consumer; the final state is recorded by delivery_ledger to prevent loss of the final reply on crash.
  7. Housekeeping and the cron heartbeat run in the background on a 60s cycle (gateway/run.py:22246 / gateway/run.py:22335).

Summary

The gateway core is an "event bus + streaming bridge": the unified MessageEvent abstraction hides platform differences; GatewayStreamConsumer bridges the synchronous agent callback to asynchronous platform delivery; delivery_ledger ensures the final state isn't lost. It is decoupled from the agent — the agent only runs the loop; all delivery details live in the gateway.

Unofficial community learning site. Content based on the MIT-licensed NousResearch/hermes-agent source.