TypeBox 是一个 TypeScript 优先的 schema 库。我们用它来定义 Gateway 网关 WebSocket 协议(握手、请求/响应、服务器事件)。这些 schema 驱动 运行时验证、JSON Schema 导出,以及面向 macOS 应用的 Swift 代码生成。单一事实来源;其他一切都由它生成。 如果你想了解更高层的协议上下文,请从 Gateway 网关架构开始。Documentation Index
Fetch the complete documentation index at: https://docs2.openclaw.ai/llms.txt
Use this file to discover all available pages before exploring further.
心智模型(30 秒)
每条 Gateway 网关 WS 消息都是以下三种帧之一:- 请求:
{ type: "req", id, method, params } - 响应:
{ type: "res", id, ok, payload | error } - 事件:
{ type: "event", event, payload, seq?, stateVersion? }
connect 请求。之后,客户端可以调用
方法(例如 health、send、chat.send)并订阅事件(例如
presence、tick、agent)。
连接流程(最小):
| 类别 | 示例 | 说明 |
|---|---|---|
| 核心 | 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 | 唤醒 + cron 控制 |
| 节点 | node.list、node.invoke、node.pair.* | Gateway 网关 WS + 节点操作 |
| 事件 | tick、presence、agent、chat、health、shutdown | 服务器推送 |
src/gateway/server-methods-list.ts(listGatewayMethods、GATEWAY_EVENTS)。
schema 所在位置
- 源码:
src/gateway/protocol/schema.ts - 运行时验证器(AJV):
src/gateway/protocol/index.ts - 已公布功能/设备发现注册表:
src/gateway/server-methods-list.ts - 服务器握手 + 方法分发:
src/gateway/server.impl.ts - 节点客户端:
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 在运行时的使用方式
- 服务器端:每个入站帧都会用 AJV 验证。握手只接受
params 匹配
ConnectParams的connect请求。 - 客户端端:JS 客户端会在使用事件帧和响应帧之前验证它们。
- 功能发现:Gateway 网关会在
hello-ok中从listGatewayMethods()和GATEWAY_EVENTS发送保守的features.methods和features.events列表。 - 这个设备发现列表不是
coreGatewayHandlers中每个可调用 helper 的生成转储; 一些 helper RPC 实现在src/gateway/server-methods/*.ts中, 但并未枚举到公布的功能列表里。
示例帧
连接(第一条消息):最小客户端(Node.js)
最小可用流程:连接 + health。完整示例:端到端添加一个方法
示例:添加一个新的system.echo 请求,返回 { ok: true, text }。
- Schema(事实来源)
src/gateway/protocol/schema.ts:
ProtocolSchemas 并导出类型:
- 验证
src/gateway/protocol/index.ts 中导出一个 AJV 验证器:
- 服务器行为
src/gateway/server-methods/system.ts 中添加 handler:
src/gateway/server-methods.ts 中注册它(已经合并 systemHandlers),
然后将 "system.echo" 添加到
src/gateway/server-methods-list.ts 中的 listGatewayMethods 输入。
如果该方法可由 operator 或节点客户端调用,还要在
src/gateway/method-scopes.ts 中对它分类,以便 scope 强制执行和
hello-ok 功能公布保持一致。
- 重新生成
- 测试 + 文档
src/gateway/server.*.test.ts 中添加服务器测试,并在文档中说明该方法。
Swift 代码生成行为
Swift 生成器会输出:- 包含
req、res、event和unknowncase 的GatewayFrameenum - 强类型 payload struct/enum
ErrorCode值、GATEWAY_PROTOCOL_VERSION和GATEWAY_MIN_PROTOCOL_VERSION
版本控制 + 兼容性
PROTOCOL_VERSION位于src/gateway/protocol/version.ts。- 客户端发送
minProtocol+maxProtocol;服务器会拒绝不包含其当前协议版本的范围。 - Swift 模型会保留未知帧类型,以避免破坏较旧客户端。
Schema 模式和约定
- 大多数对象使用
additionalProperties: false来实现严格 payload。 NonEmptyString是 ID 和方法/事件名称的默认值。- 顶层
GatewayFrame在type上使用判别器。 - 带副作用的方法通常需要在 params 中提供
idempotencyKey(示例:send、poll、agent、chat.send)。 agent接受可选的internalEvents,用于运行时生成的编排上下文 (例如子智能体/cron 任务完成交接);请将其视为内部 API 表面。
实时 schema JSON
生成的 JSON Schema 位于仓库中的dist/protocol.schema.json。已发布的 raw 文件通常可在以下位置获取:
更改 schema 时
- 更新 TypeBox schema。
- 在
src/gateway/server-methods-list.ts中注册方法/事件。 - 当新的 RPC 需要 operator 或节点 scope 分类时,更新
src/gateway/method-scopes.ts。 - 运行
pnpm protocol:check。 - 提交重新生成的 schema + Swift 模型。