TypeBox 作為通訊協定的事實來源
最後更新:2026-01-10 TypeBox 是一個以 TypeScript 為優先的結構描述函式庫。我們使用它來定義 Gateway WebSocket 通訊協定(交握、請求/回應、伺服器事件)。這些結構描述驅動 執行期驗證、JSON Schema 匯出,以及 macOS 應用程式的 Swift 程式碼產生。單一事實來源;其他一切皆由此產生。 35. 我們使用它來定義 Gateway WebSocket 通訊協定(交握、請求/回應、伺服器事件)。 36. 這些結構描述驅動執行階段驗證、JSON Schema 匯出,以及 macOS App 的 Swift 程式碼產生。 37. 單一事實來源;其餘一切皆由此產生。 如果你想了解較高層級的通訊協定背景,請從 Gateway architecture 開始。心智模型(30 秒)
每個 Gateway WS 訊息都是以下三種框架之一:- Request:
{ type: "req", id, method, params } - Response:
{ type: "res", id, ok, payload | error } - Event:
{ type: "event", event, payload, seq?, stateVersion? }
connect 請求。 39. 之後,客戶端可以呼叫方法(例如 health、send、chat.send),並訂閱事件(例如 presence、tick、agent)。
連線流程(最小):
| 40. 類別 | 範例 | 注意事項 |
|---|---|---|
| 核心 | connect、health、status | connect 必須是第一個 |
| 訊息傳遞 | send、poll、agent、agent.wait | 有副作用的操作需要 idempotencyKey |
| 聊天 | chat.history、chat.send、chat.abort、chat.inject | WebChat 使用這些 |
| Sessions | sessions.list、sessions.patch、sessions.delete | session admin |
| Nodes | node.list、node.invoke、node.pair.* | Gateway WS + 節點動作 |
| 事件 | tick、presence、agent、chat、health、shutdown | 伺服器推送 |
src/gateway/server.ts(METHODS、EVENTS)。
Where the schemas live
- 來源:
src/gateway/protocol/schema.ts - 執行期驗證器(AJV):
src/gateway/protocol/index.ts - 伺服器交握 + 方法分派:
src/gateway/server.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- runs both generators and verifies the output is committed
結構描述在執行期的使用方式
- 伺服器端:每個傳入的框架都會以 AJV 驗證。交握僅接受
其參數符合
ConnectParams的connect請求。 伺服器端:每個傳入的框架都會以 AJV 驗證。交握僅接受 其參數符合ConnectParams的connect請求。 The handshake only accepts aconnectrequest whose params matchConnectParams. - 用戶端:JS 用戶端在使用事件與回應框架之前會先進行驗證。
- 方法介面:Gateway 會在
hello-ok中公告支援的methods與events。
範例框架
連線(第一則訊息):最小用戶端(Node.js)
最小可用流程:連線 + health。實作範例:端到端新增一個方法
範例:新增一個system.echo 請求,回傳 { ok: true, text }。
- 結構描述(事實來源)
src/gateway/protocol/schema.ts:
ProtocolSchemas 並匯出型別:
- 驗證
src/gateway/protocol/index.ts 中匯出一個 AJV 驗證器:
- 伺服器行為
src/gateway/server-methods/system.ts 中新增處理器:
src/gateway/server-methods.ts 中註冊它(已合併 systemHandlers),
接著將 "system.echo" 加入 src/gateway/server.ts 中的 METHODS。
- 重新產生
- 測試 + 文件
src/gateway/server.*.test.ts 中新增伺服器測試,並在文件中註明此方法。
Swift 程式碼產生行為
Swift 產生器會輸出:GatewayFrame列舉,包含req、res、event與unknown案例- 強型別的 payload 結構/列舉
ErrorCode值與GATEWAY_PROTOCOL_VERSION
Versioning + compatibility
PROTOCOL_VERSION位於src/gateway/protocol/schema.ts。- 用戶端會送出
minProtocol+maxProtocol;伺服器會拒絕不相符者。 - Swift 模型會保留未知的框架型別,以避免破壞較舊的用戶端。
Schema patterns and conventions
- 多數物件使用
additionalProperties: false以確保 payload 嚴格。 NonEmptyString是 ID 與方法/事件名稱的預設型別。- 最上層的
GatewayFrame在type上使用 鑑別器。 - 具有副作用的方法通常需要在參數中提供
idempotencyKey(例如:send、poll、agent、chat.send)。
即時結構描述 JSON
產生的 JSON Schema 位於儲存庫中的dist/protocol.schema.json。已發布的原始檔案通常可在以下位置取得: The
published raw file is typically available at: The
published raw file is typically available at:
When you change schemas
- 更新 TypeBox 結構描述。
- 執行
pnpm protocol:check。 - 提交重新產生的結構描述與 Swift 模型。