TypeBox as protocol source of truth
Last updated: 2026-01-10 TypeBox는 TypeScript-first schema library입니다. OpenClaw는 이를 사용해 Gateway WebSocket protocol (handshake, request/response, server event)을 정의합니다. 이 schema는 runtime validation, JSON Schema export, macOS app용 Swift codegen을 구동합니다. source of truth는 하나이고, 나머지는 모두 생성물입니다. 더 높은 수준의 protocol 맥락은 Gateway architecture에서 확인하세요.Mental model (30 seconds)
모든 Gateway WS message는 세 가지 frame 중 하나입니다.- Request:
{ type: "req", id, method, params } - Response:
{ type: "res", id, ok, payload | error } - Event:
{ type: "event", event, payload, seq?, stateVersion? }
connect request여야 합니다. 그 뒤에 client는
health, send, chat.send 같은 method를 호출하고,
presence, tick, agent 같은 event를 구독할 수 있습니다.
최소 connection flow:
| 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 validator
(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 model:
apps/macos/Sources/OpenClawProtocol/GatewayModels.swift
Current pipeline
pnpm protocol:gen- JSON Schema
(draft-07)를
dist/protocol.schema.json에 기록
- JSON Schema
(draft-07)를
pnpm protocol:gen:swift- Swift gateway model 생성
pnpm protocol:check- 두 generator를 모두 실행하고 output이 commit돼 있는지 검증
How the schemas are used at runtime
- Server side: 들어오는 모든 frame을 AJV로 검증.
handshake는
ConnectParams에 맞는connectrequest만 허용 - Client side: JS client는 event와 response frame을 사용하기 전에 검증
- Method surface: Gateway는
hello-ok에서 지원하는methods와events를 광고
Example frames
Connect (첫 message):Minimal client (Node.js)
가장 작은 유용한 흐름은 connect + health입니다.Worked example: add a method end-to-end
예시:{ ok: true, text }를 반환하는 새 system.echo request를 추가합니다.
- Schema (source of truth)
src/gateway/protocol/schema.ts에 추가:
ProtocolSchemas에 둘 다 추가하고 type export:
- Validation
src/gateway/protocol/index.ts에서 AJV validator export:
- Server behavior
src/gateway/server-methods/system.ts에 handler 추가:
src/gateway/server-methods.ts에 등록하고,
src/gateway/server.ts의 METHODS에 "system.echo"를 추가합니다.
- Regenerate
- Tests + docs
src/gateway/server.*.test.ts에 server test를 추가하고 문서에도 method를 기록합니다.
Swift codegen behavior
Swift generator는 다음을 생성합니다.GatewayFrameenum (req,res,event,unknown)- strongly typed payload struct/enum
ErrorCode값과GATEWAY_PROTOCOL_VERSION
Versioning + compatibility
PROTOCOL_VERSION은src/gateway/protocol/schema.ts에 있습니다- client는
minProtocol+maxProtocol을 보내고, server는 mismatch를 reject합니다 - Swift model은 오래된 client가 깨지지 않도록 unknown frame type을 유지합니다
Schema patterns and conventions
- 대부분 object는 엄격한 payload를 위해
additionalProperties: false를 사용 - ID와 method/event name에는 기본적으로
NonEmptyString사용 - top-level
GatewayFrame은typediscriminator를 사용 - side effect가 있는 method는 보통 params에
idempotencyKey가 필요 (예:send,poll,agent,chat.send) agent는 runtime-generated orchestration context를 위한 optionalinternalEvents를 받을 수 있으며 (예: subagent/cron task completion handoff), 이는 internal API surface로 다뤄야 합니다
Live schema JSON
생성된 JSON Schema는 repo의dist/protocol.schema.json에 있습니다.
published raw file은 보통 다음 주소에서 확인할 수 있습니다.
When you change schemas
- TypeBox schema를 수정합니다
pnpm protocol:check를 실행합니다- regenerated schema + Swift model을 commit합니다