openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
160 lines (159 loc) • 10.3 kB
JavaScript
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { Et as _enum, Jn as string, Kt as custom, Lt as boolean, Pn as number, or as union, xn as literal } from "./schemas-Cy6abvsu.mjs";
import { a as workerProtocolObject, i as workerProtocolIdentifier, n as decodeWorkerRequest, t as WorkerGatewayNamespace } from "./protocol-record-CP9_JxHx.mjs";
import { c as NodeWorkerWorkspaceTransferInputSchema } from "./node-workspace-transfer-protocol-CTDuSXSy.mjs";
import { n as WORKSPACE_INSPECTION_MAX_BYTES, r as isWorkspaceInspectionCommand, t as WORKSPACE_INSPECTION_COMMAND } from "./workspace-inspection-protocol-Bdv-2wfk.mjs";
import path from "node:path";
//#region src/worker/node-workspace-protocol.ts
const REQUEST_MAX_BYTES = 262144;
const NODE_WORKER_WORKSPACE_STDIN_MAX_BYTES = 131072;
const OUTPUT_MAX_BYTES = 65536;
const STDERR_MAX_BYTES = 16384;
const ARGV_MAX_ITEMS = 128;
const ARG_MAX_BYTES = 131072;
const TIMEOUT_MAX_MS = 6e5;
const NODE_WORKSPACE_VALIDATE_DEVICE_FOLDER_COMMAND = "openclaw-internal-workspace-validate-device-folder";
const NODE_WORKSPACE_DRAIN_COMMAND = "openclaw-internal-workspace-drain";
const NODE_WORKSPACE_QUIESCENCE_COMMAND = "openclaw-internal-workspace-quiescence";
const QuiescenceNonce = string().regex(/^[a-f0-9]{32}$/u);
const QuiescenceTimeout = number().int().min(1).max(72e4);
const WorkspaceQuiescence = union([
workerProtocolObject({
action: literal("acquire"),
nonce: QuiescenceNonce,
timeoutMs: QuiescenceTimeout
}),
workerProtocolObject({
action: literal("renew"),
nonce: QuiescenceNonce,
timeoutMs: QuiescenceTimeout,
validationMode: _enum(["heartbeat", "final"])
}),
workerProtocolObject({
action: literal("release"),
nonce: QuiescenceNonce
})
]);
const SeedKey = string().regex(/^[a-f0-9]{64}$/u);
const WorkspaceProcessId = string().regex(/^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/u);
const WorkspaceProcess = workerProtocolObject({
action: _enum([
"start",
"status",
"stop"
]),
processId: WorkspaceProcessId
});
const SeedInput = union([workerProtocolObject({
action: literal("apply"),
key: SeedKey
}), workerProtocolObject({
action: literal("store"),
key: SeedKey,
maxAgeMs: number().int().min(0).max(Number.MAX_SAFE_INTEGER)
})]);
const WorkspaceInput = workerProtocolObject({
gatewayNamespace: WorkerGatewayNamespace,
environmentId: workerProtocolIdentifier("environmentId"),
sessionId: workerProtocolIdentifier("sessionId"),
deviceFolder: workerProtocolIdentifier("deviceFolder", 4096).optional(),
sessionKey: workerProtocolIdentifier("sessionKey", 1024).optional(),
preparationKey: custom((e) => typeof e === "string" && /^[a-f0-9]{64}$/u.test(e), { error: "INVALID_REQUEST: preparationKey must be a SHA-256 hex digest" }).optional(),
generation: custom((e) => typeof e === "number" && Number.isSafeInteger(e) && e >= 0, { error: "INVALID_REQUEST: generation must be a non-negative safe integer" }),
argv: custom((e) => Array.isArray(e) && e.length > 0 && e.length <= ARGV_MAX_ITEMS && e[0] !== "" && e.every((e) => typeof e === "string" && !e.includes("\0") && Buffer.byteLength(e, "utf8") <= ARG_MAX_BYTES), { error: "INVALID_REQUEST: argv must be a bounded non-empty string array" }).transform((e) => [...e]),
input: string({ error: "INVALID_REQUEST: workspace command input exceeds its bound" }).optional(),
timeoutMs: custom((e) => typeof e === "number" && Number.isSafeInteger(e) && e >= 1 && e <= TIMEOUT_MAX_MS, { error: "INVALID_REQUEST: workspace command timeout is invalid" }).optional(),
resetWorkspace: boolean({ error: "INVALID_REQUEST: resetWorkspace must be a boolean" }).optional(),
transfer: NodeWorkerWorkspaceTransferInputSchema.optional(),
seed: SeedInput.optional(),
process: WorkspaceProcess.optional(),
quiescence: WorkspaceQuiescence.optional(),
nativeProcessOwner: literal(true).optional()
});
/** Device folders cannot receive staged files; chat ingress and worker media preparation refuse them. */
const DEVICE_FOLDER_ATTACHMENTS_UNSUPPORTED = "Attachments are not supported in sessions that use an existing device folder. Send the message without attachments.";
/** User-owned folders only run commands; ingress parsing and direct node execution share this rule. */
function assertNodeWorkspaceDeviceFolderCommand(e) {
if (e.deviceFolder !== void 0 && (e.preparationKey !== void 0 || e.resetWorkspace !== void 0 || e.transfer || e.seed || e.quiescence)) throw new Error("INVALID_REQUEST: device folders cannot be prepared, reset, transferred, seeded, or quiesced");
if (e.argv[0] === "openclaw-internal-workspace-validate-device-folder" && (e.deviceFolder === void 0 || e.argv.length !== 1 || e.input !== void 0 || e.process || e.nativeProcessOwner)) throw new Error("INVALID_REQUEST: device folder validation owns its operation");
}
function parseNodeWorkerWorkspaceExecInput(e) {
const t = decodeWorkerRequest(e, WORKSPACE_INSPECTION_MAX_BYTES * 2, "node worker workspace");
const n = WorkspaceInput.safeParse(t);
if (!n.success) {
const e = n.error.issues[0];
if (e?.path[0] === "transfer") throw new Error("INVALID_REQUEST: workspace transfer is invalid");
if (e?.path[0] === "process") throw new Error("INVALID_REQUEST: workspace process operation is invalid");
if (e?.path[0] === "seed") {
const e = isRecord(t) && isRecord(t.seed) && SeedKey.safeParse(t.seed.key).success;
throw new Error(e ? "INVALID_REQUEST: workspace seed action or maxAgeMs is invalid" : "INVALID_REQUEST: workspace seed key must be a SHA-256 hex digest");
}
throw new Error(e?.path.length ? e.message : "INVALID_REQUEST: invalid node worker workspace request");
}
const r = n.data;
assertNodeWorkspaceDeviceFolderCommand(r);
if (r.quiescence || r.argv[0] === "openclaw-internal-workspace-quiescence") {
if (!r.quiescence || r.argv[0] !== "openclaw-internal-workspace-quiescence" || r.argv.length !== 2 || r.input !== void 0 || r.transfer || r.seed || r.process || r.resetWorkspace !== void 0 || r.nativeProcessOwner) throw new Error("INVALID_REQUEST: workspace quiescence owns its operation");
}
if (r.nativeProcessOwner && (r.process || r.seed || r.transfer)) throw new Error("INVALID_REQUEST: native process ownership requires a foreground command");
if (r.process && (r.seed || r.transfer || r.resetWorkspace !== void 0)) throw new Error("INVALID_REQUEST: workspace process owns its operation");
if (r.process && r.process.action !== "start" && (r.argv.length !== 1 || r.argv[0] !== "openclaw-internal-workspace-process" || r.input !== void 0)) throw new Error("INVALID_REQUEST: workspace process control accepts no command");
const i = isWorkspaceInspectionCommand(r.argv);
if (r.argv[0] === "openclaw-internal-workspace-drain" && (r.argv.length !== 1 || r.input !== void 0 || r.transfer !== void 0 || r.seed !== void 0 || r.process !== void 0 || r.resetWorkspace !== void 0)) throw new Error("INVALID_REQUEST: workspace drain owns its operation");
if (r.argv[0] === "openclaw-internal-workspace-inspect" && (!i || r.transfer !== void 0 || r.seed !== void 0 || r.process !== void 0 || r.resetWorkspace !== void 0)) throw new Error("INVALID_REQUEST: workspace inspection owns its operation");
if (!i && Buffer.byteLength(e ?? "", "utf8") > REQUEST_MAX_BYTES) throw new Error("INVALID_REQUEST: workspace command request exceeds its bound");
if (r.input !== void 0 && Buffer.byteLength(r.input, "utf8") > (i ? 2097152 : 131072)) throw new Error("INVALID_REQUEST: workspace command input exceeds its bound");
if (r.seed !== void 0 && (r.transfer !== void 0 || r.resetWorkspace !== void 0)) throw new Error("INVALID_REQUEST: workspace seed cannot combine with transfer or resetWorkspace");
return r;
}
const TruncatedBytes = number().int().min(0).max(Number.MAX_SAFE_INTEGER).optional();
const WorkspaceResult = workerProtocolObject({
workspaceDir: string().max(4096).refine((e) => path.posix.isAbsolute(e) || path.win32.isAbsolute(e)),
stdout: string(),
stderr: string().refine((e) => Buffer.byteLength(e, "utf8") <= STDERR_MAX_BYTES),
code: number().int().min(Number.MIN_SAFE_INTEGER).max(Number.MAX_SAFE_INTEGER).nullable(),
signal: string().min(1).max(32).nullable(),
killed: boolean(),
termination: _enum([
"exit",
"timeout",
"no-output-timeout",
"signal"
]),
stdoutTruncatedBytes: TruncatedBytes,
stderrTruncatedBytes: TruncatedBytes,
noOutputTimedOut: boolean().optional(),
outputLimitExceeded: boolean().optional(),
outputErrorStream: _enum(["stdout", "stderr"]).optional(),
process: workerProtocolObject({
processId: WorkspaceProcessId,
state: _enum(["running", "exited"])
}).optional()
});
function parseNodeWorkerWorkspaceExecResult(e, t = []) {
const n = WorkspaceResult.safeParse(e);
if (!n.success || Buffer.byteLength(n.data.stdout, "utf8") > (isWorkspaceInspectionCommand(t) ? 2097152 : OUTPUT_MAX_BYTES)) return null;
return e;
}
function projectNodeWorkerWorkspaceExecResult(e, t, n = []) {
const r = parseNodeWorkerWorkspaceExecResult({
workspaceDir: e,
stdout: t.stdout,
stderr: t.stderr,
code: t.code,
signal: t.signal,
killed: t.killed,
termination: t.termination,
...t.stdoutTruncatedBytes === void 0 ? {} : { stdoutTruncatedBytes: t.stdoutTruncatedBytes },
...t.stderrTruncatedBytes === void 0 ? {} : { stderrTruncatedBytes: t.stderrTruncatedBytes },
...t.noOutputTimedOut === void 0 ? {} : { noOutputTimedOut: t.noOutputTimedOut },
...t.outputLimitExceeded === void 0 ? {} : { outputLimitExceeded: t.outputLimitExceeded },
...t.outputErrorStream === void 0 ? {} : { outputErrorStream: t.outputErrorStream }
}, n);
if (!r) throw new Error("node worker workspace result violated its bounded contract");
return r;
}
const NODE_WORKER_WORKSPACE_STDOUT_MAX_BYTES = OUTPUT_MAX_BYTES;
const NODE_WORKER_WORKSPACE_STDERR_MAX_BYTES = STDERR_MAX_BYTES;
//#endregion
export { NODE_WORKSPACE_DRAIN_COMMAND as a, assertNodeWorkspaceDeviceFolderCommand as c, projectNodeWorkerWorkspaceExecResult as d, NODE_WORKER_WORKSPACE_STDOUT_MAX_BYTES as i, parseNodeWorkerWorkspaceExecInput as l, NODE_WORKER_WORKSPACE_STDERR_MAX_BYTES as n, NODE_WORKSPACE_QUIESCENCE_COMMAND as o, NODE_WORKER_WORKSPACE_STDIN_MAX_BYTES as r, NODE_WORKSPACE_VALIDATE_DEVICE_FOLDER_COMMAND as s, DEVICE_FOLDER_ATTACHMENTS_UNSUPPORTED as t, parseNodeWorkerWorkspaceExecResult as u };