Concept internals
TypeBox
TypeBox एक TypeScript-first schema लाइब्रेरी है। हम इसका उपयोग Gateway WebSocket protocol (handshake, request/response, server events) को परिभाषित करने के लिए करते हैं। वे schemas runtime validation, JSON Schema export, और macOS app के लिए Swift codegen चलाते हैं। सत्य का एक स्रोत; बाकी सब generated है।
यदि आपको उच्च-स्तरीय protocol संदर्भ चाहिए, तो Gateway architecture से शुरू करें।
मानसिक मॉडल (30 सेकंड)
हर Gateway WS message इन तीन frames में से एक होता है:
- Request:
{ type: "req", id, method, params } - Response:
{ type: "res", id, ok, payload | error } - Event:
{ type: "event", event, payload, seq?, stateVersion? }
पहला frame अनिवार्य रूप से connect request होना चाहिए। उसके बाद, clients
methods (जैसे health, send, chat.send) call कर सकते हैं और events (जैसे
presence, tick, agent) subscribe कर सकते हैं।
Connection flow (न्यूनतम):
Client Gateway |---- req:connect -------->| |<---- res:hello-ok --------| |<---- event:tick ----------| |---- req:health ---------->| |<---- res:health ----------|सामान्य methods + events:
| श्रेणी | उदाहरण | नोट्स |
|---|---|---|
| Core | connect, health, status |
connect पहले होना चाहिए |
| Messaging | send, agent, agent.wait, system-event, logs.tail |
side-effects को idempotencyKey चाहिए |
| Chat | chat.history, chat.send, chat.abort |
WebChat इन्हें उपयोग करता है |
| Sessions | sessions.list, sessions.patch, sessions.delete |
session admin |
| Automation | wake, cron.list, cron.run, cron.runs |
wake + cron control |
| Nodes | node.list, node.invoke, node.pair.* |
Gateway WS + node actions |
| Events | tick, presence, agent, chat, health, shutdown |
server push |
प्रामाणिक advertised discovery inventory
src/gateway/server-methods-list.ts (listGatewayMethods, GATEWAY_EVENTS) में रहती है।
Schemas कहां रहते हैं
- Source:
packages/gateway-protocol/src/schema.ts - Runtime validators (AJV):
packages/gateway-protocol/src/index.ts - Advertised feature/discovery registry:
src/gateway/server-methods-list.ts - Server handshake + method dispatch:
src/gateway/server.impl.ts - Node client:
src/gateway/client.ts - Generated JSON Schema:
dist/protocol.schema.json - Generated Swift models:
apps/macos/Sources/OpenClawProtocol/GatewayModels.swift
मौजूदा pipeline
pnpm protocol:gen- JSON Schema (draft-07) को
dist/protocol.schema.jsonमें लिखता है
- JSON Schema (draft-07) को
pnpm protocol:gen:swift- Swift gateway models generate करता है
pnpm protocol:check- दोनों generators चलाता है और verify करता है कि output committed है
Runtime पर schemas का उपयोग कैसे होता है
- Server side: हर inbound frame AJV से validate किया जाता है। handshake केवल
वह
connectrequest accept करता है जिसके paramsConnectParamsसे match करते हैं। - Client side: JS client event और response frames को उपयोग करने से पहले validate करता है।
- Feature discovery: Gateway
listGatewayMethods()औरGATEWAY_EVENTSसेhello-okमें एक conservativefeatures.methodsऔरfeatures.eventslist भेजता है। - वह discovery list
coreGatewayHandlersमें मौजूद हर callable helper का generated dump नहीं है; कुछ helper RPCssrc/gateway/server-methods/*.tsमें implement होते हैं, बिना advertised feature list में enumerate हुए।
Example frames
Connect (पहला message):
{ "type": "req", "id": "c1", "method": "connect", "params": { "minProtocol": 3, "maxProtocol": 4, "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": 4, "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)
सबसे छोटा उपयोगी flow: connect + health।
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: 4, maxProtocol: 4, 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(); }});Worked example: एक method end-to-end जोड़ें
Example: एक नया system.echo request जोड़ें जो { ok: true, text } लौटाता है।
- Schema (सत्य का स्रोत)
packages/gateway-protocol/src/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 export करें:
SystemEchoParams: SystemEchoParamsSchema, SystemEchoResult: SystemEchoResultSchema,export type SystemEchoParams = Static<typeof SystemEchoParamsSchema>;export type SystemEchoResult = Static<typeof SystemEchoResultSchema>;- Validation
packages/gateway-protocol/src/index.ts में, एक AJV validator export करें:
export const validateSystemEchoParams = ajv.compile<SystemEchoParams>(SystemEchoParamsSchema);- Server behavior
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 में register करें (पहले से systemHandlers merge करता है),
फिर src/gateway/server-methods-list.ts में listGatewayMethods input में
"system.echo" जोड़ें।
यदि method operator या node clients द्वारा callable है, तो इसे
src/gateway/method-scopes.ts में भी classify करें ताकि scope enforcement और hello-ok feature
advertising aligned रहें।
- Regenerate
pnpm protocol:check- Tests + docs
src/gateway/server.*.test.ts में एक server test जोड़ें और docs में method note करें।
Swift codegen behavior
Swift generator emit करता है:
GatewayFrameenum, जिसमेंreq,res,event, औरunknowncases होते हैं- Strongly typed payload structs/enums
ErrorCodevalues,GATEWAY_PROTOCOL_VERSION, औरGATEWAY_MIN_PROTOCOL_VERSION
Forward compatibility के लिए unknown frame types raw payloads के रूप में preserve किए जाते हैं।
Versioning + compatibility
PROTOCOL_VERSIONpackages/gateway-protocol/src/version.tsमें रहता है।- Clients
minProtocol+maxProtocolभेजते हैं; server उन ranges को reject करता है जो उसके current protocol को include नहीं करते। - Swift models older clients को break करने से बचाने के लिए unknown frame types रखते हैं।
Schema patterns और conventions
- अधिकांश objects strict payloads के लिए
additionalProperties: falseउपयोग करते हैं। - IDs और method/event names के लिए
NonEmptyStringdefault है। - Top-level
GatewayFrametypeपर discriminator उपयोग करता है। - Side effects वाले methods को आमतौर पर params में
idempotencyKeyचाहिए होता है (example:send,poll,agent,chat.send)। agentruntime-generated orchestration context के लिए optionalinternalEventsaccept करता है (उदाहरण के लिए subagent/cron task completion handoff); इसे internal API surface मानें।
Live schema JSON
Generated JSON Schema repo में dist/protocol.schema.json पर है। Published raw file आमतौर पर यहां उपलब्ध होती है:
जब आप schemas बदलते हैं
- TypeBox schemas update करें।
- Method/event को
src/gateway/server-methods-list.tsमें register करें। - जब नए RPC को operator या node scope classification चाहिए हो, तो
src/gateway/method-scopes.tsupdate करें। pnpm protocol:checkचलाएं।- Regenerated schema + Swift models commit करें।