网关协议(WebSocket)
Gateway WS 协议是 OpenClaw 的单一控制平面 + node 传输。 所有客户端(CLI、web UI、macOS app、iOS/Android nodes、headless nodes)都通过 WebSocket 连接,并在握手时声明其role + scope。传输
- WebSocket,文本帧承载 JSON。
- 首帧必须是
connect请求。
握手(connect)
Gateway → Client(预连接 challenge):hello-ok 还包含:
Node 示例
帧格式
- Request:
{type:"req", id, method, params} - Response:
{type:"res", id, ok, payload|error} - Event:
{type:"event", event, payload, seq?, stateVersion?}
Roles + scopes
Roles
operator= 控制平面客户端(CLI/UI/automation)。node= 能力宿主(camera/screen/canvas/system.run)。
Scopes(operator)
常见 scopes:operator.readoperator.writeoperator.adminoperator.approvalsoperator.pairing
Caps/commands/permissions(node)
Nodes 在 connect 时声明能力:caps:高层能力分类。commands:可 invoke 的命令 allowlist。permissions:细粒度开关(如screen.record、camera.capture)。
Presence
system-presence返回按设备身份分组的条目。- Presence 条目包含
deviceId、roles与scopes,使 UI 能在设备同时以 operator 与 node 连接时显示单行。
Node helper methods
- Nodes 可调用
skills.bins以获取当前 skill 可执行文件列表,用于自动 allow 检查。
Exec approvals
- 当 exec 请求需要审批时,gateway 会广播
exec.approval.requested。 - Operator 客户端通过
exec.approval.resolve处理(需要operator.approvalsscope)。
版本
PROTOCOL_VERSION位于src/gateway/protocol/schema.ts。- 客户端发送
minProtocol+maxProtocol;服务端拒绝不匹配。 - Schemas + models 从 TypeBox 定义生成:
pnpm protocol:genpnpm protocol:gen:swiftpnpm protocol:check
Auth
- 若设置
OPENCLAW_GATEWAY_TOKEN(或--token),connect.params.auth.token必须匹配,否则 socket 会关闭。 - 配对后,Gateway 发放device token,与连接 role + scopes 绑定。它会在
hello-ok.auth.deviceToken返回,客户端应持久化以便后续连接使用。 - 可通过
device.token.rotate与device.token.revoke轮换/吊销 device tokens(需operator.pairingscope)。
设备身份 + 配对
- Nodes 应包含稳定的设备身份(
device.id),源自 keypair 指纹。 - Gateway 为每个设备 + role 发放 tokens。
- 新 device ID 需配对审批,除非启用本地自动审批。
- 本地连接包括 loopback 与 gateway 主机自身的 tailnet 地址(因此同机 tailnet bind 仍可自动审批)。
- 所有 WS 客户端在
connect时必须包含device身份(operator + node)。 仅当启用gateway.controlUi.allowInsecureAuth(或gateway.controlUi.dangerouslyDisableDeviceAuth用于紧急场景)时,Control UI 可省略。 - 非本地连接必须对服务器提供的
connect.challengenonce 进行签名。
TLS + pinning
- 支持 WS 的 TLS。
- 客户端可选固定 gateway 证书指纹(见
gateway.tls配置与gateway.remote.tlsFingerprint或 CLI--tls-fingerprint)。
作用域
该协议暴露完整 gateway API(status、channels、models、chat、agent、sessions、nodes、approvals 等)。确切面由src/gateway/protocol/schema.ts 中的 TypeBox schemas 定义。