Skip to main content
TypeBox is a TypeScript-first schema library. OpenClaw uses it to define the Gateway WebSocket protocol (handshake, request/response, server events). Those schemas drive runtime validation (AJV), JSON Schema export, and Swift codegen for the macOS app. One source of truth; everything else is generated. For 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? }
The first frame must be a connect request. After that, clients call methods (e.g. health, send, chat.send) and subscribe to events (e.g. presence, tick, agent). Connection flow (minimal):
Common methods and events: The authoritative advertised discovery inventory lives in src/gateway/server-methods-list.ts (listGatewayMethods, GATEWAY_EVENTS).

Where the schemas live

  • Source barrels: packages/gateway-protocol/src/schema-modules.ts owns the canonical domain-module list, while the public schema.ts wrapper also exposes ProtocolSchemas.
  • Generator registry: ordered protocol-schema-fragment-*.ts files map stable names to the canonical TypeBox objects from their owner modules. protocol-schemas.ts composes those fragments in a fixed order and rejects duplicate keys.
  • Runtime validators (AJV): packages/gateway-protocol/src/index.ts
  • Advertised feature/discovery registry: src/gateway/server-methods-list.ts
  • Server handshake and method dispatch: src/gateway/server-core-runtime.ts
  • Node client: src/gateway/client.ts
  • Generated JSON Schema: dist/protocol.schema.json (build output, not committed)
  • Generated Swift models: apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift

Current pipeline

  • pnpm protocol:gen writes JSON Schema (draft-07) to dist/protocol.schema.json.
  • pnpm protocol:gen:swift generates the Swift gateway models.
  • pnpm protocol:check:swift verifies the committed Swift models without rewriting them.
  • pnpm protocol:gen:kotlin generates the Android protocol models and constants.
  • pnpm protocol:check checks the registry structure, runs all three generators, and verifies the committed Swift and Kotlin output (the JSON Schema output is a gitignored build artifact).
When a gateway schema affects native clients, run pnpm protocol:gen:swift, review the generated diff, then run pnpm protocol:check:swift. Commit the schema and GatewayModels.swift update together. Stable decoding behavior belongs in the focused GatewayModelsCompatibilityTests.swift regressions rather than in handwritten model copies.

How the schemas are used at runtime

  • Server side: every inbound frame is validated with AJV. The handshake only accepts a connect request whose params match ConnectParams.
  • Client side: the JS client validates event and response frames before using them.
  • Feature discovery: the Gateway sends a conservative features.methods and features.events list in hello-ok, from listGatewayMethods() and GATEWAY_EVENTS.
  • That discovery list is not a generated dump of every callable helper in coreGatewayHandlers; some helper RPCs are implemented in src/gateway/server-methods/*.ts without being enumerated in the advertised feature list.

Example frames

Connect (first message):
Hello-ok response:
Request and response:
Event:

Minimal client (Node.js)

Smallest useful flow: connect + health.

Worked example: add a method end-to-end

Example: add a new system.echo request that returns { ok: true, text }.
  1. Schema (source of truth)
Add to packages/gateway-protocol/src/schema/system-info.ts (or the closest matching feature module):
Add both entries to the closest semantic packages/gateway-protocol/src/schema/protocol-schema-fragment-*.ts file. Import the owner module as a namespace when that fragment does not already use it, then map the stable registry names to the canonical schema objects:
Do not sort fragment keys or move existing entries: native code generation follows registry insertion order. protocol-schemas.ts owns the deliberate fragment order and should change only when introducing a new semantic fragment.
  1. Validation
In packages/gateway-protocol/src/index.ts, export an AJV validator:
  1. Server behavior
Add a handler in src/gateway/server-methods/system.ts:
Register it in src/gateway/server-methods.ts (already merges systemHandlers), then add "system.echo" to the listGatewayMethods input in src/gateway/server-methods-list.ts. If the method is callable by operator or node clients, also classify it in src/gateway/method-scopes.ts so scope enforcement and hello-ok feature advertising stay aligned.
  1. Regenerate
  1. Tests and docs
Add a server test in src/gateway/server.*.test.ts and note the method in docs.

Swift codegen behavior

The Swift generator emits:
  • a GatewayFrame enum with req, res, event, and unknown cases
  • strongly typed payload structs/enums
  • ErrorCode values, GATEWAY_PROTOCOL_VERSION, and GATEWAY_MIN_PROTOCOL_VERSION
Unknown frame types are preserved as raw payloads for forward compatibility.

Versioning and compatibility

  • PROTOCOL_VERSION lives in packages/gateway-protocol/src/version.ts (current value: 4).
  • Clients send minProtocol and maxProtocol; the server rejects ranges that do not include its current protocol.
  • The Swift models keep unknown frame types to avoid breaking older clients.

Schema patterns and conventions

  • Most objects use additionalProperties: false for strict payloads.
  • NonEmptyString (Type.String({ minLength: 1 })) is the default for IDs and method/event names.
  • The top-level GatewayFrame uses a discriminator on type.
  • Methods with side effects usually require an idempotencyKey in params (example: send, poll, agent, chat.send).
  • agent accepts optional internalEvents for runtime-generated orchestration context (for example subagent/cron task completion handoff); treat this as internal API surface.

Live schema JSON

Generated JSON Schema is a build artifact, not committed to the repo. During the package rollout, the current beta schema is available at:

When you change schemas

  1. Update the TypeBox schemas in the owning packages/gateway-protocol/src/schema/*.ts module and register them in the closest protocol-schema-fragment-*.ts file without reordering existing keys.
  2. Register the method/event in src/gateway/server-methods-list.ts.
  3. Update src/gateway/method-scopes.ts when the new RPC needs operator or node scope classification.
  4. Run pnpm protocol:check.
  5. Commit the regenerated Swift models.