openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
171 lines (170 loc) • 5.58 kB
JavaScript
import { n as isTruthyEnvValue } from "./env-HehwidXj.mjs";
import { a as requestNodeHostLauncherRestart, n as isNodeHostLauncherChild } from "./launcher-client-DcylVxuB.mjs";
import { s as redactSensitiveText } from "./redact-Mj7pppxC.mjs";
import { n as ignoreMissingPath } from "./errno-DFcoqIT9.mjs";
import { r as resolveStateDir } from "./state-dir-DiG0gXzD.mjs";
import { d as sleepWithAbort } from "./src-BPmQBLJp.mjs";
import "./paths-DAcAg5aV.mjs";
import { n as resolveOpenClawPackageRoot } from "./openclaw-root-C-ayo09z.mjs";
import { t as VERSION } from "./version-GlxUMdxI.mjs";
import { l as resolveEffectiveUpdateChannel } from "./update-channels-CDc4OFaS.mjs";
import { t as createConfigIO } from "./io.factory-DEa_bi2j.mjs";
import "./io-4QsrsWTJ.mjs";
import { n as runCommandWithTimeout } from "./exec-runner-Br73vG-X.mjs";
import "./exec-aRIvS9s4.mjs";
import { c as resolveUpdateInstallKind, l as resolveUpdateRegistryTarget, n as compareSemverStrings, o as resolveNpmChannelTag } from "./update-check-BfPo_IXH.mjs";
import path from "node:path";
import fs from "node:fs/promises";
//#region src/node-host/auto-update.ts
const CHECK_INTERVAL_MS = 36e5;
const IDLE_CHECK_INTERVAL_MS = 3e4;
const MIN_ACTIVATION_INTERVAL_MS = 12 * CHECK_INTERVAL_MS;
/** Owns discovery and idle admission; the launcher owns executable activation. */
function startNodeHostAutoUpdate(e) {
const t = new AbortController();
const n = AbortSignal.any([e.signal, t.signal]);
const r = process.env;
const i = resolveStateDir(r);
const a = createConfigIO({
env: r,
observe: false,
pluginValidation: "skip"
});
let o;
let s = false;
let c = false;
const readPolicy = async () => {
const e = await a.readConfigFileSnapshot();
n.throwIfAborted();
if (!e.valid) throw new Error("Node auto-update deferred: fix the invalid OpenClaw configuration first.");
const t = resolveEffectiveUpdateChannel({
configChannel: e.config.update?.channel,
currentVersion: VERSION,
installKind: "package"
}).channel;
return {
enabled: e.config.nodeHost?.autoUpdate?.enabled !== false && e.config.update?.checkOnStart !== false && !isTruthyEnvValue(r.OPENCLAW_NO_AUTO_UPDATE) && !isTruthyEnvValue(r.OPENCLAW_NO_RESPAWN) && (t === "stable" || t === "beta"),
channel: t
};
};
const activationAllowed = async () => {
try {
const e = await fs.lstat(path.join(i, "node-runtime", "current"));
n.throwIfAborted();
return Date.now() - e.mtimeMs >= MIN_ACTIVATION_INTERVAL_MS;
} catch (e) {
ignoreMissingPath(e);
return true;
}
};
const tryActivate = async () => {
const t = o;
if (!t) return CHECK_INTERVAL_MS;
n.throwIfAborted();
if (!await e.runtime.tryPauseForUpdate()) {
if (!c) {
e.log(`node auto-update ${t.version} is ready; waiting for active work to finish`);
c = true;
}
return IDLE_CHECK_INTERVAL_MS;
}
try {
const { assertNodeRuntimeUpdateCompatible: r } = await import("./auto-update-compatibility-PlkQpBZ0.mjs");
n.throwIfAborted();
await r({
packageRoot: t.packageRoot,
stateDir: i,
signal: n
});
const a = await activationAllowed();
const c = await readPolicy();
n.throwIfAborted();
if (!c.enabled || c.channel !== t.channel || !a) {
o = void 0;
return CHECK_INTERVAL_MS;
}
await requestNodeHostLauncherRestart({
runtimeRoot: t.runtimeRoot,
version: t.version
});
s = true;
e.log(`node auto-update restarting into ${t.version}`);
e.onRestartAccepted();
return CHECK_INTERVAL_MS;
} finally {
if (!s) e.runtime.resumeAfterUpdate();
}
};
const check = async () => {
const t = await readPolicy();
if (!t.enabled) {
o = void 0;
return CHECK_INTERVAL_MS;
}
if (o) {
if (o.channel !== t.channel) {
o = void 0;
return CHECK_INTERVAL_MS;
}
return await tryActivate();
}
const a = await resolveNpmChannelTag({
channel: t.channel,
env: r,
signal: n,
...process.versions.bun ? resolveUpdateRegistryTarget({ env: r }) : {},
runCommand: process.versions.bun ? void 0 : (e, t) => runCommandWithTimeout(e, {
...t,
signal: n
})
});
n.throwIfAborted();
if (a.error) throw new Error(`Node auto-update discovery failed: ${a.error}`);
if (!a.version || (compareSemverStrings(a.version, VERSION) ?? 0) <= 0) return CHECK_INTERVAL_MS;
if (!await activationAllowed()) return CHECK_INTERVAL_MS;
e.log(`node auto-update preparing ${a.version}`);
const { prepareNodeRuntimeUpdate: s } = await import("./auto-update-install-BcnKiHSJ.mjs");
n.throwIfAborted();
const l = await s({
targetVersion: a.version,
stateDir: i,
signal: n
});
n.throwIfAborted();
for (const t of l.warnings ?? []) e.log(redactSensitiveText(t));
o = {
...l,
channel: t.channel
};
c = false;
return await tryActivate();
};
const l = (async () => {
if (!isNodeHostLauncherChild()) return;
const t = await resolveOpenClawPackageRoot({
moduleUrl: import.meta.url,
argv1: process.argv[1]
});
if (!t || await resolveUpdateInstallKind(t, { signal: n }) !== "package") return;
while (!n.aborted) {
let t = CHECK_INTERVAL_MS;
try {
t = await check();
} catch (t) {
if (n.aborted) break;
o = void 0;
e.log(redactSensitiveText(String(t)));
}
if (s) break;
await sleepWithAbort(t, n, { ref: false });
}
})().catch((t) => {
if (!n.aborted) e.log(`node auto-update stopped: ${redactSensitiveText(String(t))}`);
});
return { stop: async () => {
t.abort();
await l;
} };
}
//#endregion
export { startNodeHostAutoUpdate };