Concepts
Gateway
WebSocket JSON-RPC gateway for client connections, session management, execution engine, and multi-interface support.
The gateway module is Aleph's WebSocket JSON-RPC gateway. It handles client connections, session management, the execution engine, and interfaces with multiple messaging platforms (Discord, Telegram, CLI, TUI, etc.).
Design Philosophy
- Interface-agnostic core — The same agent brain serves every interface
- Session isolation — Each conversation is an independent session with its own context
- Streaming responses — Real-time token streaming to all connected clients
Architecture
┌─────────────────────────────────────────────────────────────┐
│ Gateway │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ WebSocket │ │ Session │ │ Execution │ │
│ │ Handler │ │ Manager │ │ Engine │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
│ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Discord │ │ Telegram │ │ CLI │ │
│ │ Interface │ │ Interface │ │ Interface │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘Core Components
WebSocket Handler
Accepts JSON-RPC 2.0 connections:
pub struct WebSocketHandler;
impl WebSocketHandler {
pub async fn handle(
&self,
socket: WebSocket,
) -> Result<()> { /* ... */ }
}Methods:
session.create— Create a new sessionsession.send— Send a messagesession.history— Get conversation historysession.clear— Clear session contexttool.call— Invoke a tool directly
SessionManager
Manages active sessions:
pub struct SessionManager {
sessions: HashMap<SessionId, Session>,
}
impl SessionManager {
pub async fn create(
&self,
config: SessionConfig,
) -> Result<SessionId> { /* ... */ }
pub async fn get(
&self,
id: SessionId,
) -> Option<Session> { /* ... */ }
}Session types:
Owner— Full access to all featuresGuest— Restricted access, no persistent historyPaired— Linked to a specific channel (Telegram, Discord)
ExecutionEngine
The agent loop that processes messages:
pub struct ExecutionEngine {
thinker: Thinker,
tool_service: ToolService,
provider: Arc<dyn LlmProvider>,
}
impl ExecutionEngine {
pub async fn run(
&self,
request: RunRequest,
) -> Result<RunOutput> { /* ... */ }
}Flow:
- Receive
RunRequest(message + attachments + context) - Process attachments via
MediaProcessor - Build system prompt via
Thinker - Call LLM provider
- Parse response (text + tool calls)
- Execute tools
- Stream results back to client
Interfaces
Platform-specific adapters:
Discord:
- Bot integration via
serenity - Slash commands
- DM and guild channel support
Telegram:
- Bot integration via
teloxide - Inline queries
- Group chat support
CLI:
- Interactive terminal interface
- Command history
- Tab completion
TUI:
- Terminal UI with
ratatui - Multi-pane layout
- Real-time streaming
Routing
Messages are routed to the appropriate handler:
pub struct InboundRouter {
handlers: HashMap<InterfaceType, Box<dyn MessageHandler>>,
}Routing rules:
- WebSocket → SessionManager → ExecutionEngine
- Discord → ChannelManager → SessionManager
- Telegram → BotHandler → SessionManager
- CLI → DirectExecution
Rate Limiting
pub struct RateLimiter {
buckets: HashMap<String, TokenBucket>,
}Per-session and per-user rate limits prevent abuse.
Safety Properties
- UTF-8 safe — All string operations use
char_indices()or.find() - Lock recovery —
unwrap_or_else(|e| e.into_inner()) - No unwrap in production — All hot paths return
Result - Saturating arithmetic —
saturating_subfor index calculations
Code Location
src/gateway/— Module rootsrc/gateway/handlers/— WebSocket and HTTP handlerssrc/gateway/session/— Session managementsrc/gateway/execution_engine/— Agent execution loopsrc/gateway/interfaces/— Platform adapters (Discord, Telegram, CLI, TUI)src/gateway/routing/— Message routing
See Also
- Session Management — Session lifecycle
- Event System — Event distribution
- Thinker — Prompt construction