|
| 1 | +// Canonical published Gateway protocol schema document. The generator that |
| 2 | +// writes protocol.schema.json and the verifier that inspects the packed tarball |
| 3 | +// both build the envelope here; a second copy is how the shipped machine-readable |
| 4 | +// contract drifts without any lane noticing. |
| 5 | + |
| 6 | +export type ProtocolMethodMetadata = { |
| 7 | + name: string; |
| 8 | + scope: string; |
| 9 | + since?: string; |
| 10 | +}; |
| 11 | + |
| 12 | +export type ProtocolSchemaDocument = { |
| 13 | + $id: string; |
| 14 | + $schema: string; |
| 15 | + definitions: Record<string, unknown>; |
| 16 | + description: string; |
| 17 | + discriminator: { |
| 18 | + mapping: Record<string, string>; |
| 19 | + propertyName: string; |
| 20 | + }; |
| 21 | + methods: Record<string, { scope: string; since?: string }>; |
| 22 | + oneOf: { $ref: string }[]; |
| 23 | + title: string; |
| 24 | +}; |
| 25 | + |
| 26 | +/** Frame definitions every consumer of the published schema resolves by name. */ |
| 27 | +export const REQUIRED_PROTOCOL_DEFINITIONS = [ |
| 28 | + "ConnectParams", |
| 29 | + "RequestFrame", |
| 30 | + "ResponseFrame", |
| 31 | + "EventFrame", |
| 32 | +] as const; |
| 33 | + |
| 34 | +// Frame order is part of the published contract: generated clients select the |
| 35 | +// oneOf branch positionally before reading the discriminator, so req/res/event |
| 36 | +// must stay in this order across regenerations. |
| 37 | +const FRAME_DEFINITIONS_BY_TYPE = [ |
| 38 | + ["req", "RequestFrame"], |
| 39 | + ["res", "ResponseFrame"], |
| 40 | + ["event", "EventFrame"], |
| 41 | +] as const; |
| 42 | + |
| 43 | +const definitionRef = (definition: string) => `#/definitions/${definition}`; |
| 44 | +const FRAME_REFS = FRAME_DEFINITIONS_BY_TYPE.map(([, definition]) => definitionRef(definition)); |
| 45 | +const FRAME_DISCRIMINATOR_MAPPING = Object.fromEntries( |
| 46 | + FRAME_DEFINITIONS_BY_TYPE.map(([frameType, definition]) => [ |
| 47 | + frameType, |
| 48 | + definitionRef(definition), |
| 49 | + ]), |
| 50 | +); |
| 51 | + |
| 52 | +/** Builds the JSON document published as protocol.schema.json. */ |
| 53 | +export function buildProtocolSchemaDocument(params: { |
| 54 | + methods: readonly ProtocolMethodMetadata[]; |
| 55 | + schemas: Record<string, unknown>; |
| 56 | +}): ProtocolSchemaDocument { |
| 57 | + const document = { |
| 58 | + $schema: "http://json-schema.org/draft-07/schema#", |
| 59 | + $id: "https://openclaw.ai/protocol.schema.json", |
| 60 | + title: "OpenClaw Gateway Protocol", |
| 61 | + description: "Handshake, request/response, and event frames for the Gateway WebSocket.", |
| 62 | + oneOf: FRAME_REFS.map((ref) => ({ $ref: ref })), |
| 63 | + discriminator: { |
| 64 | + propertyName: "type", |
| 65 | + mapping: FRAME_DISCRIMINATOR_MAPPING, |
| 66 | + }, |
| 67 | + methods: Object.fromEntries( |
| 68 | + // Omit an absent `since` instead of carrying undefined so the document |
| 69 | + // equals the JSON the artifact ships. |
| 70 | + params.methods.map(({ name, scope, since }) => [ |
| 71 | + name, |
| 72 | + since === undefined ? { scope } : { since, scope }, |
| 73 | + ]), |
| 74 | + ), |
| 75 | + definitions: params.schemas, |
| 76 | + }; |
| 77 | + // TypeBox schemas carry symbol keys that never reach JSON; cloning them away |
| 78 | + // lets the producer and the published-artifact verifier compare one document. |
| 79 | + return structuredClone(document) as ProtocolSchemaDocument; |
| 80 | +} |
| 81 | + |
| 82 | +/** Rejects a published document that lost the frame contract clients decode by. */ |
| 83 | +export function assertProtocolSchemaDocument(document: ProtocolSchemaDocument): void { |
| 84 | + const problems = REQUIRED_PROTOCOL_DEFINITIONS.filter( |
| 85 | + (definition) => !Object.hasOwn(document.definitions, definition), |
| 86 | + ).map((definition) => `definition ${definition} is missing`); |
| 87 | + if (JSON.stringify(document.oneOf.map((entry) => entry.$ref)) !== JSON.stringify(FRAME_REFS)) { |
| 88 | + problems.push(`frame oneOf must list ${FRAME_REFS.join(", ")}`); |
| 89 | + } |
| 90 | + if ( |
| 91 | + JSON.stringify(document.discriminator.mapping) !== JSON.stringify(FRAME_DISCRIMINATOR_MAPPING) |
| 92 | + ) { |
| 93 | + problems.push(`type discriminator must map ${JSON.stringify(FRAME_DISCRIMINATOR_MAPPING)}`); |
| 94 | + } |
| 95 | + if (Object.keys(document.methods).length === 0) { |
| 96 | + problems.push("method metadata is empty"); |
| 97 | + } |
| 98 | + if (problems.length > 0) { |
| 99 | + throw new Error(`published protocol schema contract violated: ${problems.join("; ")}`); |
| 100 | + } |
| 101 | +} |
0 commit comments