openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
238 lines (237 loc) • 9.59 kB
JavaScript
import { r as GATEWAY_SHUTDOWN_RESERVE_MS } from "./gateway-shutdown-budget-cDAYhBVI.mjs";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import path from "node:path";
//#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();
}
function watchCliRespawnParent() {
const e = globalThis[parentKey];
if (!e || e.watching || !process.channel) return;
e.watching = true;
process.once("disconnect", requestCliParentTermination);
process.channel.unref();
if (!process.connected) requestCliParentTermination();
}
//#endregion
//#region cli-invocation.mjs
/** Packaging owns hashes; launch planning consumes only the artifact identity. */
function readStandaloneRuntimeManifest(e = process.execPath) {
return readStandaloneManifest({ executable: e });
}
/** Resolve a peer package through the same manifest contract, including renamed executables. */
function readStandaloneRuntimeManifestForRoot(e) {
return readStandaloneManifest({ runtimeRoot: e });
}
/** The executing image owns its build facts, even if its sidecar is replaced. */
function getStandaloneRuntimeBuild(e = process.execPath) {
if (e === process.execPath) {
const e = globalThis.__openclawStandaloneBuildInfo;
if (e !== void 0) return e;
}
return readStandaloneManifest({
executable: e,
includeBuild: true
});
}
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;
}
/** Disk-executed package-manager programs belong to the standalone sidecar. */
function resolveStandaloneNpmCliPath() {
const e = resolveStandaloneSidecarRoot(process.execPath);
return e === void 0 ? void 0 : path.join(e, "npm", "bin", "npm-cli.js");
}
/** 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 };
}
/** A null entry explicitly selects a CLI command; undefined inherits the current script. */
function resolveOpenClawCliInvocation({ execPath: e = process.execPath, entryPath: t, args: n = process.argv.slice(2), execArgv: r = process.execArgv } = {}) {
const i = resolveStandaloneSidecarRoot(e);
if (i !== void 0 || t === null) {
if (i !== void 0 && t !== void 0 && t !== null) {
const e = path.relative(i, path.resolve(t));
if (e === ".." || e.startsWith(`..${path.sep}`) || path.isAbsolute(e)) throw new Error("A standalone OpenClaw executable cannot run another installation's entry script.");
}
return {
command: e,
args: [...n]
};
}
const a = t ?? process.argv[1];
if (!a) throw new Error("Cannot invoke the loose OpenClaw CLI without an entry script.");
return {
command: e,
args: [
...r,
a,
...n
]
};
}
/** Bind only this CLI child to its launcher; runtime flags belong before a loose entry. */
function resolveOpenClawCliRespawn({ env: e = process.env, ...t } = {}) {
const n = t.execPath ?? process.execPath;
return {
...resolveOpenClawCliInvocation({
...t,
execArgv: [
"--import",
CLI_PARENT_BOUND_PRELOAD,
...t.execArgv ?? process.execArgv
]
}),
env: isStandaloneRuntimeExecutable(n) ? {
...e,
[CLI_RESPAWN_PARENT_ENV]: String(process.pid)
} : e
};
}
//#endregion
export { readStandaloneRuntimeManifest as a, resolveOpenClawCliRespawn as c, resolveStandaloneSidecarRoot as d, CLI_RESPAWN_PARENT_ENV as f, watchCliRespawnParent as h, isStandaloneRuntimeExecutable as i, resolveRuntimeScriptEnv as l, requestCliParentTermination as m, getStandaloneRuntimeBuild as n, readStandaloneRuntimeManifestForRoot as o, getCliParentPid as p, isEmbeddedStandaloneModule as r, resolveOpenClawCliInvocation as s, getEmbeddedStandaloneRuntimeRoot as t, resolveStandaloneNpmCliPath as u };