Documentation Index
Fetch the complete documentation index at: https://docs.openclaw.ai/llms.txt
Use this file to discover all available pages before exploring further.
TypeBox 作為協定的單一真相來源
最後更新:2026-01-10 TypeBox 是 TypeScript 優先的結構描述函式庫。我們用它定義 Gateway WebSocket 協定(handshake、request/response、server events)。這些結構描述 驅動執行階段驗證、JSON Schema 匯出,以及 macOS 應用程式的 Swift 程式碼產生。 一個單一真相來源;其餘全部產生而來。 如果你想了解較高層次的協定脈絡,請從 Gateway 架構開始。心智模型(30 秒)
每個 Gateway WS 訊息都是下列三種框架之一:- Request:
{ type: "req", id, method, params } - Response:
{ type: "res", id, ok, payload | error } - Event:
{ type: "event", event, payload, seq?, stateVersion? }
connect request。之後,clients 可以呼叫
methods(例如 health、send、chat.send)並訂閱 events(例如
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 | node.list、node.invoke、node.pair.* | Gateway WS + Node 動作 |
| 事件 | tick、presence、agent、chat、health、shutdown | server push |
src/gateway/server-methods-list.ts(listGatewayMethods、GATEWAY_EVENTS)。
結構描述所在位置
- 來源:
src/gateway/protocol/schema.ts - 執行階段驗證器(AJV):
src/gateway/protocol/index.ts - 已宣告的功能/discovery 登錄檔:
src/gateway/server-methods-list.ts - Server handshake + method dispatch:
src/gateway/server.impl.ts - Node client:
src/gateway/client.ts - 產生的 JSON Schema:
dist/protocol.schema.json - 產生的 Swift models:
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 models
pnpm protocol:check- 執行兩個產生器並驗證輸出已提交
結構描述在執行階段如何使用
- Server 端:每個傳入框架都會以 AJV 驗證。handshake 只接受 params 符合
ConnectParams的connectrequest。 - Client 端:JS client 會在使用前驗證 event 與 response 框架。
- 功能 discovery:Gateway 會在
hello-ok中,從listGatewayMethods()和GATEWAY_EVENTS傳送保守的features.methods與features.events清單。 - 該 discovery 清單不是
coreGatewayHandlers中每個可呼叫 helper 的產生式傾印; 有些 helper RPC 實作在src/gateway/server-methods/*.ts中,但未列舉於已宣告的 功能清單。
範例框架
Connect(第一個訊息):最小 client(Node.js)
最小可用流程:connect + health。實作範例:端到端新增 method
範例:新增一個system.echo request,回傳 { ok: true, text }。
- 結構描述(單一真相來源)
src/gateway/protocol/schema.ts:
ProtocolSchemas 並匯出 types:
- 驗證
src/gateway/protocol/index.ts 中,匯出 AJV validator:
- Server 行為
src/gateway/server-methods/system.ts 中新增 handler:
src/gateway/server-methods.ts 中註冊它(已經合併 systemHandlers),
然後將 "system.echo" 加到
src/gateway/server-methods-list.ts 的 listGatewayMethods 輸入。
如果該 method 可由 operator 或 Node clients 呼叫,也請在
src/gateway/method-scopes.ts 中分類它,讓 scope enforcement 與 hello-ok 功能
宣告保持一致。
- 重新產生
- 測試 + 文件
src/gateway/server.*.test.ts 中新增 server test,並在文件中註記該 method。
Swift 程式碼產生行為
Swift 產生器會輸出:- 含有
req、res、event和unknowncases 的GatewayFrameenum - 強型別 payload structs/enums
ErrorCodevalues 與GATEWAY_PROTOCOL_VERSION
版本化 + 相容性
PROTOCOL_VERSION位於src/gateway/protocol/schema.ts。- Clients 會傳送
minProtocol+maxProtocol;server 會拒絕不相符者。 - Swift models 會保留未知框架類型,以避免破壞較舊的 clients。
結構描述模式與慣例
- 多數 objects 使用
additionalProperties: false來實作嚴格 payloads。 NonEmptyString是 IDs 與 method/event 名稱的預設值。- 最上層的
GatewayFrame在type上使用 discriminator。 - 具有副作用的 methods 通常要求 params 中有
idempotencyKey(範例:send、poll、agent、chat.send)。 agent接受選用的internalEvents,用於執行階段產生的 orchestration context (例如 subagent/Cron task completion handoff);請將此視為內部 API surface。
即時 schema JSON
產生的 JSON Schema 位於 repo 的dist/protocol.schema.json。已發布的原始檔通常可在:
變更結構描述時
- 更新 TypeBox schemas。
- 在
src/gateway/server-methods-list.ts中註冊 method/event。 - 當新的 RPC 需要 operator 或 Node scope 分類時,更新
src/gateway/method-scopes.ts。 - 執行
pnpm protocol:check。 - 提交重新產生的 schema + Swift models。