UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

145 lines (144 loc) • 7.04 kB
import "./src-H3RMXyFL.mjs"; import { n as safeParseJsonRecord } from "./json-coercion-CaJLaav-.mjs"; import { t as stableStringify } from "./stable-stringify-vqde_3tT.mjs"; import { ni as NEVER } from "./json-schema-processors-CIhP-F53.mjs"; import { Et as _enum, Jn as string, Kt as custom, or as union, tr as transform, xn as literal } from "./schemas-Cy6abvsu.mjs"; import { n as sha256Hex } from "./node-crypto-B6yuTjmm.mjs"; import { a as workerProtocolObject, i as workerProtocolIdentifier, t as WorkerGatewayNamespace } from "./protocol-record-CP9_JxHx.mjs"; import { i as parseWorkerLaunchPlan } from "./launch-descriptor-BUjz-ttq.mjs"; import { i as decodeNodeInvokeParams } from "./invoke-payload-Csjq7TlY.mjs"; //#region src/worker/node-supervisor-protocol.ts const NODE_WORKER_SUPERVISOR_CONTROL_REQUEST_MAX_BYTES = 4096; const NODE_WORKER_STATUS_WAIT_MAX_MS = 2e4; const NODE_WORKER_RESULT_JSON_MAX_BYTES = 65536; const NODE_WORKER_ERROR_TEXT_MAX_BYTES = 4096; const NODE_WORKER_CONNECTION_FAILURE_CAUSE_MAX_BYTES = 65536; const NODE_WORKER_CONNECTION_FAILURE_MESSAGE_TYPE = "openclaw-worker-connection-failure-v1"; function isNonNegativeInteger(e) { return typeof e === "number" && Number.isSafeInteger(e) && e >= 0; } function isPlanHash(e) { return typeof e === "string" && /^[a-f0-9]{64}$/u.test(e); } const nonNegativeInteger = (e) => custom(isNonNegativeInteger, { error: `INVALID_REQUEST: ${e} must be a non-negative safe integer` }); const IdentityShape = { launchId: workerProtocolIdentifier("launchId"), planHash: custom(isPlanHash, { error: "INVALID_REQUEST: planHash must be 64 lowercase hexadecimal characters" }), environmentId: workerProtocolIdentifier("environmentId"), sessionId: workerProtocolIdentifier("sessionId"), ownerEpoch: nonNegativeInteger("ownerEpoch"), placementGeneration: nonNegativeInteger("placementGeneration"), runId: workerProtocolIdentifier("runId") }; const Identity = workerProtocolObject(IdentityShape); const EnvironmentStop = workerProtocolObject({ gatewayNamespace: WorkerGatewayNamespace, environmentId: IdentityShape.environmentId, sessionId: IdentityShape.sessionId, ownerEpoch: IdentityShape.ownerEpoch }); const LaunchInput = workerProtocolObject({ environmentSession: literal(1, { error: "INVALID_REQUEST: node worker environment lifetime support required" }), sessionKey: workerProtocolIdentifier("sessionKey", 1024).optional(), idleRetention: literal(true, { error: "INVALID_REQUEST: idleRetention must be true" }).optional(), launchId: IdentityShape.launchId, gatewayNamespace: WorkerGatewayNamespace, expectedBundleHash: custom(isPlanHash, { error: "INVALID_REQUEST: expectedBundleHash must be 64 lowercase hexadecimal characters" }), descriptor: transform((e, t) => { try { return parseWorkerLaunchPlan(e); } catch { t.addIssue({ code: "custom", message: "INVALID_REQUEST: invalid worker launch descriptor" }); return NEVER; } }), placementGeneration: IdentityShape.placementGeneration }); const Lookup = workerProtocolObject({ launchId: IdentityShape.launchId, waitMs: custom((e) => isNonNegativeInteger(e) && e >= 1 && e <= 2e4, { error: "INVALID_REQUEST: waitMs must be an integer between 1 and 20000" }).optional() }); const Receipt = union([ workerProtocolObject({ ...IdentityShape, state: _enum(["pending", "running"]) }), workerProtocolObject({ ...IdentityShape, state: literal("completed"), resultJson: custom(isBoundedResultJson) }), workerProtocolObject({ ...IdentityShape, state: _enum([ "failed", "interrupted", "cancelled" ]), errorText: custom(isBoundedErrorText) }) ]); const ConnectionFailure = workerProtocolObject({ type: literal(NODE_WORKER_CONNECTION_FAILURE_MESSAGE_TYPE), cause: string().min(1).refine((e) => Buffer.byteLength(e, "utf8") <= NODE_WORKER_CONNECTION_FAILURE_CAUSE_MAX_BYTES).nullable() }); function parseRequest(e, t, n) { const r = e.safeParse(t); if (!r.success) { const e = r.error.issues[0]; throw new Error(e?.path.length ? e.message : `INVALID_REQUEST: invalid node worker ${n} request`); } return r.data; } function parseNodeWorkerLaunchInput(e) { return validateNodeWorkerLaunchInput(decodeNodeInvokeParams(e)); } function validateNodeWorkerLaunchInput(e) { const t = parseRequest(LaunchInput, e, "launch"); if (t.descriptor.assignment.turnId !== t.launchId) throw new Error("INVALID_REQUEST: launchId must match descriptor assignment turnId"); if (t.descriptor.admission.handshake.bundleHash !== t.expectedBundleHash) throw new Error("INVALID_REQUEST: descriptor bundle hash does not match expectedBundleHash"); if (t.sessionKey === void 0) delete t.sessionKey; return t; } function parseNodeWorkerLookupInput(e) { return parseRequest(Lookup, decodeNodeInvokeParams(e), "lookup"); } function parseNodeWorkerCancelInput(e) { if (!e || Buffer.byteLength(e, "utf8") > NODE_WORKER_SUPERVISOR_CONTROL_REQUEST_MAX_BYTES) throw new Error("INVALID_REQUEST: invalid node worker cancel request"); return parseRequest(Identity, decodeNodeInvokeParams(e), "cancel"); } function parseNodeWorkerEnvironmentStopInput(e) { if (!e || Buffer.byteLength(e, "utf8") > NODE_WORKER_SUPERVISOR_CONTROL_REQUEST_MAX_BYTES) throw new Error("INVALID_REQUEST: invalid node worker environment stop request"); return parseRequest(EnvironmentStop, decodeNodeInvokeParams(e), "environment stop"); } function nodeWorkerPlanHash(e) { return sha256Hex(stableStringify({ expectedBundleHash: e.expectedBundleHash, descriptor: e.descriptor, gatewayNamespace: e.gatewayNamespace, placementGeneration: e.placementGeneration, ...e.sessionKey === void 0 ? {} : { sessionKey: e.sessionKey }, ...e.idleRetention ? { idleRetention: true } : {} })); } function isBoundedResultJson(e) { if (typeof e !== "string" || e.length === 0 || Buffer.byteLength(e, "utf8") > NODE_WORKER_RESULT_JSON_MAX_BYTES) return false; return safeParseJsonRecord(e) !== void 0; } function isBoundedErrorText(e) { return typeof e === "string" && e.length > 0 && Buffer.byteLength(e, "utf8") <= NODE_WORKER_ERROR_TEXT_MAX_BYTES && !/[\r\n]/u.test(e); } function parseNodeWorkerConnectionFailureMessage(e) { return ConnectionFailure.safeParse(e).data ?? null; } function parseNodeWorkerSupervisorReceipt(e) { return Receipt.safeParse(e).data ?? null; } function nodeWorkerTurnMatchesIdentity(e, t) { return e.launchId === t.launchId && e.planHash === t.planHash && e.environmentId === t.environmentId && e.sessionId === t.sessionId && e.ownerEpoch === t.ownerEpoch && e.placementGeneration === t.placementGeneration && e.runId === t.runId; } //#endregion export { parseNodeWorkerCancelInput as a, parseNodeWorkerLaunchInput as c, validateNodeWorkerLaunchInput as d, nodeWorkerTurnMatchesIdentity as i, parseNodeWorkerLookupInput as l, NODE_WORKER_STATUS_WAIT_MAX_MS as n, parseNodeWorkerConnectionFailureMessage as o, nodeWorkerPlanHash as r, parseNodeWorkerEnvironmentStopInput as s, NODE_WORKER_CONNECTION_FAILURE_MESSAGE_TYPE as t, parseNodeWorkerSupervisorReceipt as u };