Caricamento in corso...
Caricamento in corso...
Last synced: Today, 22:00
Technical reference for the OpenClaw framework. Real-time synchronization with the official documentation engine.
Use this file to discover all available pages before exploring further.
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.
Every Gateway WS message is one of three frames:
{ type: "req", id, method, params }{ type: "res", id, ok, payload | error }{ type: "event", event, payload, seq?, stateVersion? }The first frame must be a
connecthealthsendchat.sendpresencetickagentConnection flow (minimal):
textClient Gateway |---- req:connect -------->| |<---- res:hello-ok --------| |<---- event:tick ----------| |---- req:health ---------->| |<---- res:health ----------|
Common methods + events:
| Category | Examples | Notes |
|---|---|---|
| Core | text connecttext healthtext status | text connect |
| Messaging | text sendtext agenttext agent.waittext system-eventtext logs.tail | side-effects need text idempotencyKey |
| Chat | text chat.historytext chat.sendtext chat.abort | WebChat uses these |
| Sessions | text sessions.listtext sessions.patchtext sessions.delete | session admin |
| Automation | text waketext cron.listtext cron.runtext cron.runs | wake + cron control |
| Nodes | text node.listtext node.invoketext node.pair.* | Gateway WS + node actions |
| Events | text ticktext presencetext agenttext chattext healthtext shutdown | server push |
Authoritative advertised discovery inventory lives in
src/gateway/server-methods-list.tslistGatewayMethodsGATEWAY_EVENTSsrc/gateway/protocol/schema.tssrc/gateway/protocol/index.tssrc/gateway/server-methods-list.tssrc/gateway/server.impl.tssrc/gateway/client.tsdist/protocol.schema.jsonapps/macos/Sources/OpenClawProtocol/GatewayModels.swiftpnpm protocol:gendist/protocol.schema.jsonpnpm protocol:gen:swiftpnpm protocol:checkconnectConnectParamsfeatures.methodsfeatures.eventshello-oklistGatewayMethods()GATEWAY_EVENTScoreGatewayHandlerssrc/gateway/server-methods/*.tsConnect (first message):
json{ "type": "req", "id": "c1", "method": "connect", "params": { "minProtocol": 3, "maxProtocol": 3, "client": { "id": "openclaw-macos", "displayName": "macos", "version": "1.0.0", "platform": "macos 15.1", "mode": "ui", "instanceId": "A1B2" } } }
Hello-ok response:
json{ "type": "res", "id": "c1", "ok": true, "payload": { "type": "hello-ok", "protocol": 3, "server": { "version": "dev", "connId": "ws-1" }, "features": { "methods": ["health"], "events": ["tick"] }, "snapshot": { "presence": [], "health": {}, "stateVersion": { "presence": 0, "health": 0 }, "uptimeMs": 0 }, "policy": { "maxPayload": 1048576, "maxBufferedBytes": 1048576, "tickIntervalMs": 30000 } } }
Request + response:
json{ "type": "req", "id": "r1", "method": "health" }
json{ "type": "res", "id": "r1", "ok": true, "payload": { "ok": true } }
Event:
json{ "type": "event", "event": "tick", "payload": { "ts": 1730000000 }, "seq": 12 }
Smallest useful flow: connect + health.
tsimport { WebSocket } from "ws"; const ws = new WebSocket("ws://127.0.0.1:18789"); ws.on("open", () => { ws.send( JSON.stringify({ type: "req", id: "c1", method: "connect", params: { minProtocol: 3, maxProtocol: 3, client: { id: "cli", displayName: "example", version: "dev", platform: "node", mode: "cli", }, }, }), ); }); ws.on("message", (data) => { const msg = JSON.parse(String(data)); if (msg.type === "res" && msg.id === "c1" && msg.ok) { ws.send(JSON.stringify({ type: "req", id: "h1", method: "health" })); } if (msg.type === "res" && msg.id === "h1") { console.log("health:", msg.payload); ws.close(); } });
Example: add a new
system.echo{ ok: true, text }Add to
src/gateway/protocol/schema.tstsexport const SystemEchoParamsSchema = Type.Object( { text: NonEmptyString }, { additionalProperties: false }, ); export const SystemEchoResultSchema = Type.Object( { ok: Type.Boolean(), text: NonEmptyString }, { additionalProperties: false }, );
Add both to
ProtocolSchemastsSystemEchoParams: SystemEchoParamsSchema, SystemEchoResult: SystemEchoResultSchema,
tsexport type SystemEchoParams = Static<typeof SystemEchoParamsSchema>; export type SystemEchoResult = Static<typeof SystemEchoResultSchema>;
In
src/gateway/protocol/index.tstsexport const validateSystemEchoParams = ajv.compile<SystemEchoParams>(SystemEchoParamsSchema);
Add a handler in
src/gateway/server-methods/system.tstsexport const systemHandlers: GatewayRequestHandlers = { "system.echo": ({ params, respond }) => { const text = String(params.text ?? ""); respond(true, { ok: true, text }); }, };
Register it in
src/gateway/server-methods.tssystemHandlers"system.echo"listGatewayMethodssrc/gateway/server-methods-list.tsIf the method is callable by operator or node clients, also classify it in
src/gateway/method-scopes.tshello-okbashpnpm protocol:check
Add a server test in
src/gateway/server.*.test.tsThe Swift generator emits:
GatewayFramereqreseventunknownErrorCodeGATEWAY_PROTOCOL_VERSIONUnknown frame types are preserved as raw payloads for forward compatibility.
PROTOCOL_VERSIONsrc/gateway/protocol/schema.tsminProtocolmaxProtocoladditionalProperties: falseNonEmptyStringGatewayFrametypeidempotencyKeysendpollagentchat.sendagentinternalEventsGenerated JSON Schema is in the repo at
dist/protocol.schema.jsonsrc/gateway/server-methods-list.tssrc/gateway/method-scopes.tspnpm protocol:check© 2024 TaskFlow Mirror
Powered by TaskFlow Sync Engine