openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
171 lines (170 loc) • 6.92 kB
JavaScript
import { readFileSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
//#region gateway-shutdown-budget.mjs
const GATEWAY_SHUTDOWN_RESERVE_MS = 1e4;
const RESPAWN_SIGNAL_FORCE_KILL_GRACE_MS = 1e3;
//#endregion
//#region cli-parent-lifetime.mjs
const parentKey = Symbol.for("openclaw.cliRespawnParent");
const CLI_RESPAWN_PARENT_ENV = "OPENCLAW_CLI_RESPAWN_PARENT";
const inheritedParent = process.env[CLI_RESPAWN_PARENT_ENV];
delete process.env[CLI_RESPAWN_PARENT_ENV];
if (inheritedParent === String(process.ppid)) globalThis[parentKey] = { pid: process.ppid };
const CLI_PARENT_BOUND_PRELOAD = `data:text/javascript,${encodeURIComponent(`
globalThis[Symbol.for("openclaw.cliRespawnParent")] = { pid: process.ppid };
process.execArgv = process.execArgv.filter((arg, index, args) =>
arg !== import.meta.url && !(arg === "--import" && args[index + 1] === import.meta.url));
`)}`;
/** Only the respawn producer opts in; detached update drivers keep independent custody. */
function getCliParentPid() {
return globalThis[parentKey]?.pid;
}
let terminating = false;
function requestCliParentTermination(e = () => {
if (!process.emit("SIGTERM")) process.kill(process.pid, "SIGTERM");
}) {
if (terminating) return;
terminating = true;
setTimeout(() => process.kill(process.pid, "SIGKILL"), GATEWAY_SHUTDOWN_RESERVE_MS).unref();
e();
}
//#endregion
//#region cli-invocation.mjs
/** Packaging owns hashes; launch planning consumes only the artifact identity. */
function readStandaloneRuntimeManifest(e = process.execPath) {
return readStandaloneManifest({ executable: e });
}
function projectStandaloneBuildInfo(e) {
const nonBlank = (e) => typeof e === "string" && e.trim().length > 0;
const stringList = (e) => Array.isArray(e) && e.every(nonBlank);
const t = {
mode: e.mode,
openclaw: {
version: e.openclaw.version,
...nonBlank(e.openclaw.commit) ? { commit: e.openclaw.commit } : {}
},
bun: { version: e.bun.version }
};
for (const n of [
"revision",
"commit",
"executableSha256"
]) if (nonBlank(e.bun[n])) t.bun[n] = e.bun[n];
if (e.build !== void 0) {
const { id: n, flavor: r, pluginSetSha256: i, moduleGraphSha256: a } = e.build;
if (!nonBlank(n) || !["standard", "support"].includes(r) || !/^[a-f0-9]{64}$/.test(i) || !/^[a-f0-9]{64}$/.test(a)) throw new Error("Invalid standalone build provenance");
t.build = {
id: n,
flavor: r,
pluginSetSha256: i,
moduleGraphSha256: a
};
if (e.build.debugEntry !== void 0) {
const n = e.build.debugEntry;
if (typeof n !== "string" || !n.startsWith("debug/") || n.includes("\\") || n.split("/").some((e) => !e || e === "." || e === "..")) throw new Error("Invalid standalone debugger entry");
t.build.debugEntry = n;
}
}
if (e.target !== void 0) {
if (!nonBlank(e.target.platform) || !nonBlank(e.target.arch)) throw new Error("Invalid standalone build target");
t.target = {
platform: e.target.platform,
arch: e.target.arch
};
}
if (e.plugins !== void 0) {
if (!stringList(e.plugins.embedded) || !stringList(e.plugins.omitted)) throw new Error("Invalid standalone plugin inventory");
t.plugins = {
embedded: [...e.plugins.embedded],
omitted: [...e.plugins.omitted]
};
}
if (e.nativeModules !== void 0) {
if (!Array.isArray(e.nativeModules) || e.nativeModules.some((e) => !e || !nonBlank(e.name) || !nonBlank(e.version) || !stringList(e.artifacts) || e.qualification !== "unverified" || !nonBlank(e.backend))) throw new Error("Invalid standalone native inventory");
t.nativeModules = e.nativeModules.map(({ name: e, version: t, artifacts: n, qualification: r, backend: i }) => ({
name: e,
version: t,
artifacts: [...n],
qualification: r,
backend: i
}));
}
return t;
}
function readStandaloneManifest({ executable: e, runtimeRoot: t, includeBuild: n = false }) {
const r = e ?? t;
if (!path.isAbsolute(r)) return;
const i = path.dirname(r);
const a = path.join(i, "manifest.json");
let o;
try {
o = readFileSync(a, "utf8");
} catch (e) {
if (e?.code === "ENOENT" || e?.code === "ENOTDIR") return;
throw e;
}
let s;
try {
s = JSON.parse(o);
} catch (e) {
throw new Error(`Invalid standalone runtime manifest: ${a}`, { cause: e });
}
const nonBlank = (e) => typeof e === "string" && e.trim().length > 0;
if (s?.schemaVersion !== 1 || ![
"jit",
"bytecode",
"aot"
].includes(s.mode) || !nonBlank(s.executable?.path) || !nonBlank(s.runtime?.path) || !nonBlank(s.openclaw?.version) || !nonBlank(s.bun?.version)) throw new Error(`Invalid standalone runtime manifest: ${a}`);
const c = path.relative(i, path.resolve(i, s.executable.path));
const l = path.relative(i, path.resolve(i, s.runtime.path));
if (path.isAbsolute(s.executable.path) || !c || c === ".." || c.startsWith(`..${path.sep}`) || e && path.resolve(i, s.executable.path) !== path.resolve(e) || t && path.resolve(i, s.runtime.path) !== path.resolve(t) || path.isAbsolute(s.runtime.path) || !l || l === ".." || l.startsWith(`..${path.sep}`)) throw new Error(`Invalid standalone runtime manifest: ${a}`);
return {
schemaVersion: 1,
mode: s.mode,
executable: {
path: s.executable.path,
...nonBlank(s.executable.sha256) ? { sha256: s.executable.sha256 } : {}
},
runtime: {
path: s.runtime.path,
...nonBlank(s.runtime.sha256) ? { sha256: s.runtime.sha256 } : {}
},
openclaw: {
version: s.openclaw.version,
...nonBlank(s.openclaw.commit) ? { commit: s.openclaw.commit } : {}
},
bun: { version: s.bun.version },
...n ? projectStandaloneBuildInfo(s) : {}
};
}
/** The compiler publishes this fact before any embedded module is loaded. */
function getEmbeddedStandaloneRuntimeRoot() {
const e = globalThis.__openclawStandaloneRootUrl;
return typeof e === "string" ? fileURLToPath(e) : void 0;
}
/** Image membership comes from the compiler's module inventory, never disk contents. */
function isEmbeddedStandaloneModule(e) {
const t = e.startsWith("file:") ? fileURLToPath(e) : e;
return globalThis.__openclawStandaloneIsEmbeddedModule?.(t) === true;
}
function resolveStandaloneSidecarRoot(e = process.execPath) {
if (e === process.execPath) {
const e = getEmbeddedStandaloneRuntimeRoot();
if (e !== void 0) return e;
}
const t = readStandaloneRuntimeManifest(e);
return t ? path.resolve(path.dirname(e), t.runtime.path) : void 0;
}
function isStandaloneRuntimeExecutable(e = process.execPath) {
return resolveStandaloneSidecarRoot(e) !== void 0;
}
/** A compiled Bun executable needs this mode to execute a disk program as Bun. */
function resolveRuntimeScriptEnv(e) {
return isStandaloneRuntimeExecutable() ? {
...e,
BUN_BE_BUN: "1"
} : { ...e };
}
//#endregion
export { requestCliParentTermination as a, getCliParentPid as i, resolveRuntimeScriptEnv as n, RESPAWN_SIGNAL_FORCE_KILL_GRACE_MS as o, resolveStandaloneSidecarRoot as r, isEmbeddedStandaloneModule as t };