Architecture
High-Level Design
Key Components
API Gateway (Hono)
Entry point for all client requests. Handles cross-cutting concerns:- Rate Limiting — Sliding window per user/IP
- Idempotency — Safe retries with
Idempotency-Keyheader - i18n — Locale detection + translation
- Logging — Request/response tracking
Application Layer
Use cases implementing business logic. Each use case:- Has single responsibility
- Depends on domain interfaces (ports), not implementations
- Returns pure domain objects
Domain Layer
Pure TypeScript with no external dependencies:- Entities — Objects with identity (User, Session, Message)
- Value Objects — Immutable objects (Email, UserId)
- Domain Events — State changes (UserCreated, SessionStarted)
Infrastructure Layer
Adapters implementing application ports:- PostgreSQL — Persistence via Drizzle ORM (Better Auth manages auth schema)
- BunRedis — Cache with in-memory fallback
- File System — i18n JSON files
Design Principles
| Principle | Application |
|---|---|
| Hexagonal | Core business logic isolated from infrastructure |
| DRY | Shared code in packages/ |
| Fail-Fast | Early validation in domain layer |
| Explicit over Implicit | No magic — clear data flow |
Data Flow
Key Architectural Decisions
1. Monolith Over Microservices
The Bun Core Platform is a monolith (single deployable unit). This is intentional:- Simplicity — One server to deploy, monitor, debug
- Co-location — No network latency between modules
- Transactions — ACID across modules without distributed coordination
- MVP speed — Faster to build and iterate
2. ACP WebSocket as Client
The gateway module connects to OpenClaw as a client, not a server:- Single WebSocket per agent (multiplexed sessions)
- JSON-RPC 2.0 message framing over WebSocket
- Async iterable for streaming responses
3. BunSQLite for Sessions (Zero Infra)
Session storage uses BunSQLite instead of MongoDB:- Zero infrastructure — embedded SQLite file
- Fast for session workloads (key-value + history)
- TTL-based expiration for old sessions
- Falls back to Redis if configured for distributed setups
Gateway Prompt Flow (SSE)
Performance Considerations
Cold Start
- Bun: ~10ms cold start
- Hono: < 1ms route matching
- MongoDB connection pool: lazy initialization
Request Latency
Security
Middleware Stack
- Rate Limiting — Per user/IP, sliding window
- Idempotency — Prevents duplicate submissions
- Authentication — (Upstream proxy handles, attaches userId to context)
- i18n — No user input in error messages
- Logging — Correlation IDs for audit trails
WebSocket Security
- Token validation on connect
- Per-session authorization via
_meta.sessionKey - No sensitive data in WebSocket URLs