메인 콘텐츠로 건너뛰기

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? }
첫 frame은 반드시 connect request여야 합니다. 그 뒤에 client는 health, send, chat.send 같은 method를 호출하고, presence, tick, agent 같은 event를 구독할 수 있습니다. 최소 connection flow:
주요 method + event: authoritative list는 src/gateway/server.tsMETHODS, 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에 기록
  • 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에 맞는 connect request만 허용
  • Client side: JS client는 event와 response frame을 사용하기 전에 검증
  • Method surface: Gateway는 hello-ok에서 지원하는 methodsevents를 광고

Example frames

Connect (첫 message):
Hello-ok response:
Request + response:
Event:

Minimal client (Node.js)

가장 작은 유용한 흐름은 connect + health입니다.

Worked example: add a method end-to-end

예시: { ok: true, text }를 반환하는 새 system.echo request를 추가합니다.
  1. Schema (source of truth)
src/gateway/protocol/schema.ts에 추가:
ProtocolSchemas에 둘 다 추가하고 type export:
  1. Validation
src/gateway/protocol/index.ts에서 AJV validator export:
  1. Server behavior
src/gateway/server-methods/system.ts에 handler 추가:
src/gateway/server-methods.ts에 등록하고, src/gateway/server.tsMETHODS"system.echo"를 추가합니다.
  1. Regenerate
  1. Tests + docs
src/gateway/server.*.test.ts에 server test를 추가하고 문서에도 method를 기록합니다.

Swift codegen behavior

Swift generator는 다음을 생성합니다.
  • GatewayFrame enum (req, res, event, unknown)
  • strongly typed payload struct/enum
  • ErrorCode 값과 GATEWAY_PROTOCOL_VERSION
알 수 없는 frame type은 forward compatibility를 위해 raw payload로 보존됩니다.

Versioning + compatibility

  • PROTOCOL_VERSIONsrc/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 GatewayFrametype discriminator를 사용
  • side effect가 있는 method는 보통 params에 idempotencyKey가 필요 (예: send, poll, agent, chat.send)
  • agent는 runtime-generated orchestration context를 위한 optional internalEvents를 받을 수 있으며 (예: 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

  1. TypeBox schema를 수정합니다
  2. pnpm protocol:check를 실행합니다
  3. regenerated schema + Swift model을 commit합니다