TypeBox as protocol source of truth
Last updated: 2026-01-10 TypeBox is a TypeScript-first schema library. We use it to define the Gateway WebSocket protocol (handshake, request/response, server events). Those schemas drive runtime validation, JSON Schema export, and Swift codegen for the macOS app. One source of truth; everything else is generated. If you want the higher-level protocol context, start with Gateway architecture.Mental model (30 seconds)
Every Gateway WS message is one of three frames:- Request:
{ type: "req", id, method, params } - Response:
{ type: "res", id, ok, payload | error } -
Event:
{ type: "event", event, payload, seq?, stateVersion? }
connect request. After that,
clients can
call methods (e.g. health, send, chat.send) and subscribe
to events (e.g. presence, tick, agent).
Connection flow (minimal):
| Category | Examples | Notes |
|---|---|---|
| Core | connect, health, status |
connect must be first |
| Messaging | send, poll, agent, agent.wait |
side-effects need idempotencyKey |
| Chat |
chat.history, chat.send, chat.abort,
chat.inject
|
WebChat uses these |
| Sessions |
sessions.list, sessions.patch, sessions.delete
|
session admin |
| Nodes | node.list, node.invoke, node.pair.* |
Gateway WS + node actions |
| Events |
tick, presence, agent, chat,
health, shutdown
|
server push |
src/gateway/server.ts
(METHODS,
EVENTS).
Where the schemas live
- Source:
src/gateway/protocol/schema.ts - Runtime validators (AJV):
src/gateway/protocol/index.ts - Server handshake + method dispatch:
src/gateway/server.ts - Node client:
src/gateway/client.ts - Generated JSON Schema:
dist/protocol.schema.json -
Generated Swift models:
apps/macos/Sources/OpenClawProtocol/GatewayModels.swift
Current pipeline
-
pnpm protocol:gen- writes JSON Schema (draft‑07) to
dist/protocol.schema.json
- writes JSON Schema (draft‑07) to
-
pnpm protocol:gen:swift- generates Swift gateway models
-
pnpm protocol:check- runs both generators and verifies the output is committed
How the schemas are used at runtime
-
Server side: every inbound frame is validated with AJV. The handshake only
accepts a
connectrequest whose params matchConnectParams. - Client side: the JS client validates event and response frames before using them.
-
Method surface: the Gateway advertises the supported
methodsandeventsinhello-ok.
Example frames
Connect (first message):Minimal client (Node.js)
Smallest useful flow: connect + health.Worked example: add a method end‑to‑end
Example: add a newsystem.echo request that returns
{ ok: true, text }.
- Schema (source of truth)
src/gateway/protocol/schema.ts:
ProtocolSchemas and export types:
- Validation
src/gateway/protocol/index.ts, export an AJV validator:
- Server behavior
src/gateway/server-methods/system.ts:
src/gateway/server-methods.ts (already merges
systemHandlers), then add "system.echo" to METHODS in
src/gateway/server.ts.
- Regenerate
- Tests + docs
src/gateway/server.*.test.ts and note the method
in docs.
Swift codegen behavior
The Swift generator emits:-
GatewayFrameenum withreq,res,event, andunknowncases - Strongly typed payload structs/enums
ErrorCodevalues andGATEWAY_PROTOCOL_VERSION
Versioning + compatibility
PROTOCOL_VERSIONlives insrc/gateway/protocol/schema.ts.-
Clients send
minProtocol+maxProtocol; the server rejects mismatches. - The Swift models keep unknown frame types to avoid breaking older clients.
Schema patterns and conventions
- Most objects use
additionalProperties: falsefor strict payloads. NonEmptyStringis the default for IDs and method/event names.-
The top-level
GatewayFrameuses a discriminator ontype. -
Methods with side effects usually require an
idempotencyKeyin params (example:send,poll,agent,chat.send).
Live schema JSON
Generated JSON Schema is in the repo atdist/protocol.schema.json. The
published raw
file is typically available at:
When you change schemas
- Update the TypeBox schemas.
- Run
pnpm protocol:check. - Commit the regenerated schema + Swift models.