跳轉到主要內容

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(例如 healthsendchat.send)並訂閱 events(例如 presencetickagent)。 連線流程(最小):
Client                    Gateway
  |---- req:connect -------->|
  |<---- res:hello-ok --------|
  |<---- event:tick ----------|
  |---- req:health ---------->|
  |<---- res:health ----------|
常見 methods + events:
類別範例備註
核心connecthealthstatusconnect 必須是第一個
訊息sendagentagent.waitsystem-eventlogs.tail副作用需要 idempotencyKey
聊天chat.historychat.sendchat.abortWebChat 使用這些
工作階段sessions.listsessions.patchsessions.delete工作階段管理
自動化wakecron.listcron.runcron.runs喚醒 + Cron 控制
Nodenode.listnode.invokenode.pair.*Gateway WS + Node 動作
事件tickpresenceagentchathealthshutdownserver push
權威宣告的 discovery 清單位於 src/gateway/server-methods-list.tslistGatewayMethodsGATEWAY_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
  • pnpm protocol:gen:swift
    • 產生 Swift gateway models
  • pnpm protocol:check
    • 執行兩個產生器並驗證輸出已提交

結構描述在執行階段如何使用

  • Server 端:每個傳入框架都會以 AJV 驗證。handshake 只接受 params 符合 ConnectParamsconnect request。
  • Client 端:JS client 會在使用前驗證 event 與 response 框架。
  • 功能 discovery:Gateway 會在 hello-ok 中,從 listGatewayMethods()GATEWAY_EVENTS 傳送保守的 features.methodsfeatures.events 清單。
  • 該 discovery 清單不是 coreGatewayHandlers 中每個可呼叫 helper 的產生式傾印; 有些 helper RPC 實作在 src/gateway/server-methods/*.ts 中,但未列舉於已宣告的 功能清單。

範例框架

Connect(第一個訊息):
{
  "type": "req",
  "id": "c1",
  "method": "connect",
  "params": {
    "minProtocol": 3,
    "maxProtocol": 3,
    "client": {
      "id": "openclaw-macos",
      "displayName": "macos",
      "version": "1.0.0",
      "platform": "macos 15.1",
      "mode": "ui",
      "instanceId": "A1B2"
    }
  }
}
Hello-ok response:
{
  "type": "res",
  "id": "c1",
  "ok": true,
  "payload": {
    "type": "hello-ok",
    "protocol": 3,
    "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 }
  }
}
Request + response:
{ "type": "req", "id": "r1", "method": "health" }
{ "type": "res", "id": "r1", "ok": true, "payload": { "ok": true } }
Event:
{ "type": "event", "event": "tick", "payload": { "ts": 1730000000 }, "seq": 12 }

最小 client(Node.js)

最小可用流程:connect + health。
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: 3,
        maxProtocol: 3,
        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();
  }
});

實作範例:端到端新增 method

範例:新增一個 system.echo request,回傳 { ok: true, text }
  1. 結構描述(單一真相來源)
新增至 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 並匯出 types:
  SystemEchoParams: SystemEchoParamsSchema,
  SystemEchoResult: SystemEchoResultSchema,
export type SystemEchoParams = Static<typeof SystemEchoParamsSchema>;
export type SystemEchoResult = Static<typeof SystemEchoResultSchema>;
  1. 驗證
src/gateway/protocol/index.ts 中,匯出 AJV validator:
export const validateSystemEchoParams = ajv.compile<SystemEchoParams>(SystemEchoParamsSchema);
  1. Server 行為
src/gateway/server-methods/system.ts 中新增 handler:
export const systemHandlers: GatewayRequestHandlers = {
  "system.echo": ({ params, respond }) => {
    const text = String(params.text ?? "");
    respond(true, { ok: true, text });
  },
};
src/gateway/server-methods.ts 中註冊它(已經合併 systemHandlers), 然後將 "system.echo" 加到 src/gateway/server-methods-list.tslistGatewayMethods 輸入。 如果該 method 可由 operator 或 Node clients 呼叫,也請在 src/gateway/method-scopes.ts 中分類它,讓 scope enforcement 與 hello-ok 功能 宣告保持一致。
  1. 重新產生
pnpm protocol:check
  1. 測試 + 文件
src/gateway/server.*.test.ts 中新增 server test,並在文件中註記該 method。

Swift 程式碼產生行為

Swift 產生器會輸出:
  • 含有 reqreseventunknown cases 的 GatewayFrame enum
  • 強型別 payload structs/enums
  • ErrorCode values 與 GATEWAY_PROTOCOL_VERSION
未知框架類型會保留為原始 payload,以支援向前相容性。

版本化 + 相容性

  • PROTOCOL_VERSION 位於 src/gateway/protocol/schema.ts
  • Clients 會傳送 minProtocol + maxProtocol;server 會拒絕不相符者。
  • Swift models 會保留未知框架類型,以避免破壞較舊的 clients。

結構描述模式與慣例

  • 多數 objects 使用 additionalProperties: false 來實作嚴格 payloads。
  • NonEmptyString 是 IDs 與 method/event 名稱的預設值。
  • 最上層的 GatewayFrametype 上使用 discriminator
  • 具有副作用的 methods 通常要求 params 中有 idempotencyKey (範例:sendpollagentchat.send)。
  • agent 接受選用的 internalEvents,用於執行階段產生的 orchestration context (例如 subagent/Cron task completion handoff);請將此視為內部 API surface。

即時 schema JSON

產生的 JSON Schema 位於 repo 的 dist/protocol.schema.json。已發布的原始檔通常可在:

變更結構描述時

  1. 更新 TypeBox schemas。
  2. src/gateway/server-methods-list.ts 中註冊 method/event。
  3. 當新的 RPC 需要 operator 或 Node scope 分類時,更新 src/gateway/method-scopes.ts
  4. 執行 pnpm protocol:check
  5. 提交重新產生的 schema + Swift models。

相關