Gateway Core
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
class GatewayRunner:3029— gateway main class, mixes in authz/Kanban/Slash capabilities_handle_message:9947— inbound message dispatch entry_handle_message_with_agent:11956— hands the message to the agent for a turn_start_one_profile_adapter / _start_secondary:9389-9947— start each profile's platform adapters_start_stream_consumer:21218— start the stream consumer_start_gateway_housekeeping:22246— periodic housekeeping (60s)_start_cron_ticker:22335— cron heartbeat (60s)def main:22965— gateway process entryexceptions & redaction:276-340—_gateway_loop_exception_handler/_redact_gateway_user_facing_secretsPlatformEntry:39-162— platform registration dataclassclass PlatformRegistry:162-260— registry (register / register_deferred / unregister)
Data Flow
main()(gateway/run.py:22965) startsGatewayRunner._start_one_profile_adapter:9389brings up each platform adapter (see Platform Adapters).- Adapters receive raw platform events and convert them to unified
MessageEvents (seegateway/platforms/base.py:1759). _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).- The latter calls the agent's
run_conversation:588and wires the streaming callback toGatewayStreamConsumer:83. - Streaming output progressively edits the platform message via the consumer; the final state is recorded by
delivery_ledgerto prevent loss of the final reply on crash. - 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.