TypeBox
TypeBox 是一个以 TypeScript 为首的 Schema 库。我们使用它来定义 Gateway(网关) WebSocket 协议(握手、请求/响应、服务器事件)。这些 Schema 驱动 运行时验证、JSON Schema 导出以及 macOS 应用程序的 Swift 代码生成。单一事实来源;其他所有内容均由此生成。
如果您需要更高级别的协议上下文,请从 Gateway(网关) 架构 开始。
心智模型(30 秒)
Section titled “心智模型(30 秒)”每条 Gateway(网关) WS 消息都是以下三种帧之一:
- 请求(Request):
{ type: "req", id, method, params } - 响应(Response):
{ type: "res", id, ok, payload | error } - 事件(Event):
{ type: "event", event, payload, seq?, stateVersion? }
第一帧必须是一个 connect 请求。之后,客户端可以调用
方法(例如 health、send、chat.send)并订阅事件(例如
presence、tick、agent)。
连接流程(最简版):
Client Gateway |---- req:connect -------->| |<---- res:hello-ok --------| |<---- event:tick ----------| |---- req:health ---------->| |<---- res:health ----------|常用方法 + 事件:
| 类别 | 示例 | 说明 |
|---|---|---|
| 核心 | connect,health,status | connect 必须位于首位 |
| 消息传递 | send,agent,agent.wait,system-event,logs.tail | 副作用需要 idempotencyKey |
| 聊天 | chat.history,chat.send,chat.abort | WebChat 使用这些 |
| 会话 | sessions.list,sessions.patch,sessions.delete | 会话管理 |
| 自动化 | wake,cron.list,cron.run,cron.runs | 唤醒 + 定时任务控制 |
| 节点 | node.list,node.invoke,node.pair.* | Gateway(网关) WS + 节点操作 |
| 事件 | tick、presence、agent、chat、health、shutdown | 服务器推送 |
权威的通告式 discovery(发现) 清单位于
src/gateway/server-methods-list.ts (listGatewayMethods、GATEWAY_EVENTS)。
Schema 的存放位置
Section titled “Schema 的存放位置”- 源码:
src/gateway/protocol/schema.ts - 运行时验证器 (AJV):
src/gateway/protocol/index.ts - 通告式功能/发现注册表:
src/gateway/server-methods-list.ts - 服务器握手 + 方法分发:
src/gateway/server.impl.ts - Node 客户端:
src/gateway/client.ts - 生成的 JSON Schema:
dist/protocol.schema.json - 生成的 Swift 模型:
apps/macos/Sources/OpenClawProtocol/GatewayModels.swift
pnpm protocol:gen- 将 JSON Schema (draft-07) 写入
dist/protocol.schema.json
- 将 JSON Schema (draft-07) 写入
pnpm protocol:gen:swift- 生成 Swift gateway 模型
pnpm protocol:check- 运行两个生成器并验证输出已提交
Schema 在运行时的使用方式
Section titled “Schema 在运行时的使用方式”- 服务器端:每个入站帧都通过 AJV 进行验证。握手仅
接受参数匹配
ConnectParams的connect请求。 - 客户端:JS 客户端在使用事件和响应帧之前会对其进行验证。
- 功能发现:Gateway(网关) 在
hello-ok中发送来自listGatewayMethods()和GATEWAY_EVENTS的保守features.methods和features.events列表。 - 该发现列表并非
coreGatewayHandlers中每个可调用助手的生成转储; 某些辅助 RPC 在src/gateway/server-methods/*.ts中实现,而未在通告的 功能列表中枚举。
连接(第一条消息):
{ "type": "req", "id": "c1", "method": "connect", "params": { "minProtocol": 3, "maxProtocol": 4, "client": { "id": "openclaw-macos", "displayName": "macos", "version": "1.0.0", "platform": "macos 15.1", "mode": "ui", "instanceId": "A1B2" } }}Hello-ok 响应:
{ "type": "res", "id": "c1", "ok": true, "payload": { "type": "hello-ok", "protocol": 4, "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 } }}请求 + 响应:
{ "type": "req", "id": "r1", "method": "health" }{ "type": "res", "id": "r1", "ok": true, "payload": { "ok": true } }事件:
{ "type": "event", "event": "tick", "payload": { "ts": 1730000000 }, "seq": 12 }最小客户端 (Node.js)
Section titled “最小客户端 (Node.js)”最小可用流程:连接 + 健康检查。
import { 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: 4, maxProtocol: 4, 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(); }});实战示例:端到端添加一个方法
Section titled “实战示例:端到端添加一个方法”示例:添加一个新的 system.echo 请求,返回 { ok: true, text }。
- Schema(单一事实来源)
添加到 src/gateway/protocol/schema.ts:
export const SystemEchoParamsSchema = Type.Object({ text: NonEmptyString }, { additionalProperties: false });
export const SystemEchoResultSchema = Type.Object({ ok: Type.Boolean(), text: NonEmptyString }, { additionalProperties: false });将两者都添加到 ProtocolSchemas 并导出类型:
SystemEchoParams: SystemEchoParamsSchema, SystemEchoResult: SystemEchoResultSchema,export type SystemEchoParams = Static<typeof SystemEchoParamsSchema>;export type SystemEchoResult = Static<typeof SystemEchoResultSchema>;- 验证
在 src/gateway/protocol/index.ts 中,导出一个 AJV 验证器:
export const validateSystemEchoParams = ajv.compile<SystemEchoParams>(SystemEchoParamsSchema);- 服务器行为
在 src/gateway/server-methods/system.ts 中添加一个处理程序:
export const systemHandlers: GatewayRequestHandlers = { "system.echo": ({ params, respond }) => { const text = String(params.text ?? ""); respond(true, { ok: true, text }); },};在 src/gateway/server-methods.ts 中注册它(已合并 systemHandlers),
然后在 src/gateway/server-methods-list.ts 中的 listGatewayMethods 输入里添加
"system.echo"。
如果该方法可由操作员或节点客户端调用,请在 src/gateway/method-scopes.ts 中对其进行分类,以便作用域强制执行和 hello-ok 功能通告保持一致。
- 重新生成
pnpm protocol:check- 测试 + 文档
在 src/gateway/server.*.test.ts 中添加服务器测试并在文档中注明该方法。
Swift 代码生成行为
Section titled “Swift 代码生成行为”Swift 生成器发出:
- 带有
req、res、event和unknown情况的GatewayFrame枚举 - 强类型载荷结构体/枚举
ErrorCode值、GATEWAY_PROTOCOL_VERSION和GATEWAY_MIN_PROTOCOL_VERSION
未知的帧类型将作为原始载荷保留,以确保向前兼容性。
版本控制 + 兼容性
Section titled “版本控制 + 兼容性”PROTOCOL_VERSION位于src/gateway/protocol/version.ts中。- 客户端发送
minProtocol+maxProtocol;服务器会拒绝不包含其当前协议的范围。 - Swift 模型保留未知的帧类型,以避免破坏较旧的客户端。
Schema 模式和约定
Section titled “Schema 模式和约定”- 大多数对象使用
additionalProperties: false来定义严格的有效载荷。 NonEmptyString是 ID 和方法/事件名称的默认值。- 顶层
GatewayFrame在type上使用了 discriminator(区分符)。 - 具有副作用的方法通常在参数中需要一个
idempotencyKey(例如:send、poll、agent、chat.send)。 agent接受可选的internalEvents,用于运行时生成的编排上下文 (例如子代理/cron 任务完成交接);请将其视为内部 API 接口。
实时架构 JSON
Section titled “实时架构 JSON”生成的 JSON Schema 位于仓库中的 dist/protocol.schema.json。
发布的原始文件通常可在此处获取:
- 更新 TypeBox 架构。
- 在
src/gateway/server-methods-list.ts中注册该方法/事件。 - 当新的 RPC 需要操作员或节点范围分类时,更新
src/gateway/method-scopes.ts。 - 运行
pnpm protocol:check。 - 提交重新生成的架构 + Swift 模型。