openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
484 lines (483 loc) • 18.5 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { a as resolveEnvNormalizationKeys, c as clearFsSafeEnvFallback, i as normalizeZaiEnv, l as fsSafeEnvInput, t as expandEnvNormalizationKeys, u as normalizeFsSafeNativeEnv } from "./env-HehwidXj.mjs";
import { i as asOptionalObjectRecord } from "./record-coerce-CVPuDqcc.mjs";
import { n as containsEnvVarReference } from "./env-substitution-CgVeOqUd.mjs";
import { n as isDangerousHostEnvOverrideVarName, o as normalizeEnvVarKey, r as isDangerousHostEnvVarName } from "./host-env-security-BrTYS4pH.mjs";
import { t as VERSION } from "./version-GlxUMdxI.mjs";
import { r as shouldWarnOnTouchedVersion } from "./version-DkjdPwZ3.mjs";
import { AsyncLocalStorage } from "node:async_hooks";
//#region src/config/future-version-guard.ts
/** Override env var for intentional older-binary destructive config actions. */
const ALLOW_OLDER_BINARY_DESTRUCTIVE_ACTIONS_ENV = "OPENCLAW_ALLOW_OLDER_BINARY_DESTRUCTIVE_ACTIONS";
function allowOlderBinaryDestructiveActions(e) {
const t = e[ALLOW_OLDER_BINARY_DESTRUCTIVE_ACTIONS_ENV]?.trim().toLowerCase();
return t === "1" || t === "true" || t === "yes";
}
function resolveTouchedVersion(e) {
const readSourceVersion = (e) => {
const t = asOptionalObjectRecord(asOptionalObjectRecord(e)?.meta);
return normalizeOptionalString(t?.lastTouchedVersion);
};
return readSourceVersion(e.snapshot?.sourceConfig) ?? readSourceVersion(e.snapshot?.config) ?? readSourceVersion(e.config) ?? null;
}
/** Resolves whether a destructive action should be blocked by future config metadata. */
function resolveFutureConfigActionBlock(e) {
if (allowOlderBinaryDestructiveActions(e.env ?? process.env)) return null;
const t = e.currentVersion ?? VERSION;
const n = resolveTouchedVersion(e);
if (!n || !shouldWarnOnTouchedVersion(t, n)) return null;
return {
action: e.action,
currentVersion: t,
touchedVersion: n,
message: `Refusing to ${e.action} because this OpenClaw binary (${t}) is older than the config last written by OpenClaw ${n}.`,
hints: ["Run the newer openclaw binary on PATH, or reinstall the intended gateway service from the newer install.", e.serviceMode ? "For an intentional downgrade, restore a verified pre-update backup with its matching OpenClaw release. See https://docs.openclaw.ai/install/updating/rollback-and-recovery#downgrade." : `Set ${ALLOW_OLDER_BINARY_DESTRUCTIVE_ACTIONS_ENV}=1 only for an intentional downgrade or recovery action.`]
};
}
/** Formats a future-config action block for CLI/service error output. */
function formatFutureConfigActionBlock(e) {
return [e.message, ...e.hints].join("\n");
}
//#endregion
//#region src/config/config-env-values.ts
/** Returns whether a config-controlled environment entry is safe to apply at runtime. */
function isConfigRuntimeEnvVarAllowed(e, t) {
const n = e.toUpperCase();
return Boolean(t.trim()) && n !== "OPENCLAW_ALLOW_OLDER_BINARY_DESTRUCTIVE_ACTIONS" && n !== "OPENCLAW_INCLUDE_ROOTS" && n !== "OPENCLAW_CONFIG_READONLY" && n !== "OPENCLAW_CONFIG_READONLY_NOTICE" && n !== "OPENCLAW_DISABLE_GATEWAY_LOCAL_TERMINALS" && n !== "OPENCLAW_DISABLE_CRON_HOST_EXECUTION" && !isDangerousHostEnvVarName(e) && !isDangerousHostEnvOverrideVarName(e) && !containsEnvVarReference(t);
}
/** Collects config env vars safe to inject into runtime process environments. */
function collectConfigRuntimeEnvVars(e) {
const t = e?.env;
if (!t) return {};
const n = {};
const r = [...Object.entries(t.vars ?? {}), ...Object.entries(t).filter(([e]) => e !== "shellEnv" && e !== "vars")];
for (const [e, t] of r) {
if (typeof t !== "string") continue;
const r = normalizeEnvVarKey(e, { portable: true });
if (r && isConfigRuntimeEnvVarAllowed(r, t)) n[r] = t;
}
return n;
}
function findCaseInsensitiveEnvKey(e, t) {
if (Object.hasOwn(e, t)) return t;
const n = t.toUpperCase();
return Object.keys(e).find((e) => e.toUpperCase() === n);
}
function envSnapshotKey(e) {
return process.platform === "win32" ? e.toUpperCase() : e;
}
function snapshotEnvByPlatformKey(e) {
const t = /* @__PURE__ */ new Map();
for (const [n, r] of Object.entries(fsSafeEnvInput(e))) {
const e = envSnapshotKey(n);
if (!t.has(e)) t.set(e, {
key: n,
value: r
});
}
return t;
}
function envSnapshotEntriesEqual(e, t) {
return e?.key === t?.key && e?.value === t?.value;
}
function replaceEnvSnapshotEntry(e, t, n) {
clearFsSafeEnvFallback(e);
if (t) delete e[t.key];
if (n?.value !== void 0) e[n.key] = n.value;
}
function indexConfigRuntimeEnvValues(e) {
const t = /* @__PURE__ */ new Map();
for (const [n, r] of Object.entries(e)) for (const e of resolveEnvNormalizationKeys(n)) {
const n = t.get(e) ?? /* @__PURE__ */ new Set();
n.add(r);
t.set(e, n);
}
return t;
}
function snapshotEnvProperties(e) {
return new Map(Object.entries(fsSafeEnvInput(e)).map(([e, t]) => [e, {
key: e,
value: t
}]));
}
//#endregion
//#region src/config/config-env-vars.ts
const appliedConfigEnvOwnership = /* @__PURE__ */ new WeakMap();
function resolveAppliedConfigEnvOwnership(e) {
return {
...appliedConfigEnvOwnership.get(e),
...e === process.env ? publishedConfigRuntimeEnvState.ownedEnv : {}
};
}
/** Capture read-time provenance separately from runtime publication authority. */
function snapshotEnv(e) {
const t = cloneEnvWithPlatformSemantics(e);
appliedConfigEnvOwnership.set(t, { ...appliedConfigEnvOwnership.get(e) });
return t;
}
/** Roll back a rejected read only while both its values and provenance are current. */
function restoreEnvChangesIfUnchanged(e) {
const t = snapshotEnvByPlatformKey(e.before);
const n = snapshotEnvByPlatformKey(e.after);
const r = snapshotEnvByPlatformKey(e.env);
const i = snapshotEnvByPlatformKey(appliedConfigEnvOwnership.get(e.before) ?? {});
const a = snapshotEnvByPlatformKey(appliedConfigEnvOwnership.get(e.after) ?? {});
const o = { ...appliedConfigEnvOwnership.get(e.env) };
const s = snapshotEnvByPlatformKey(o);
const c = new Set(t.keys());
for (const e of [
n,
i,
a
]) e.forEach((e, t) => c.add(t));
for (const l of c) {
if (!envSnapshotEntriesEqual(r.get(l), n.get(l)) || !envSnapshotEntriesEqual(s.get(l), a.get(l))) continue;
if (!envSnapshotEntriesEqual(t.get(l), n.get(l))) replaceEnvSnapshotEntry(e.env, r.get(l), t.get(l));
replaceEnvSnapshotEntry(o, s.get(l), i.get(l));
}
appliedConfigEnvOwnership.set(e.env, o);
normalizeFsSafeNativeEnv(e.env);
}
const configReadEnvChanges = new AsyncLocalStorage();
/** Retain only synchronous reader effects for a containing write's compensation. */
async function withConfigReadEnvChanges(e, t) {
const n = {
env: e,
receipts: [],
active: true
};
try {
return await configReadEnvChanges.run(n, () => t(() => {
for (const e of n.receipts.toReversed()) e();
}));
} finally {
n.active = false;
}
}
function snapshotConfigEnvOwnership(e, t) {
return new Map(Object.entries(appliedConfigEnvOwnership.get(e) ?? {}).map(([n, r]) => {
return [(!t.has(n) && Object.hasOwn(e, n) ? [...t.keys()].find((e) => e.toUpperCase() === n.toUpperCase()) : n) ?? n, {
key: n,
value: r
}];
}));
}
/** Awaited validation may change other keys; the producer owns only its synchronous effects. */
function captureConfigReadEnvMutation(e, t, n) {
const r = snapshotEnvProperties(e);
const i = snapshotConfigEnvOwnership(e, r);
const a = configReadEnvChanges.getStore();
try {
return t();
} finally {
const t = snapshotEnvProperties(e);
const o = snapshotConfigEnvOwnership(e, t);
const s = [.../* @__PURE__ */ new Set([
...r.keys(),
...t.keys(),
...i.keys(),
...o.keys()
])].map((e) => ({
key: e,
before: r.get(e),
after: t.get(e),
beforeOwned: i.get(e),
afterOwned: o.get(e)
})).filter((e) => !envSnapshotEntriesEqual(e.before, e.after) || !envSnapshotEntriesEqual(e.beforeOwned, e.afterOwned));
const c = /* @__PURE__ */ new Set();
for (const t of s) {
const n = t.before;
if (!n || t.after || !Object.hasOwn(e, n.key)) continue;
const r = s.filter((t) => !t.before && t.after && !c.has(t.key) && t.key.toUpperCase() === n.key.toUpperCase() && e[n.key] === t.after.value);
if (r.length !== 1) continue;
const i = r[0];
t.after = i.after;
t.afterOwned = i.afterOwned;
c.add(i.key);
}
let l = true;
const restore = () => {
if (!l) return;
l = false;
const t = { ...appliedConfigEnvOwnership.get(e) };
const n = snapshotEnvProperties(e);
const r = snapshotConfigEnvOwnership(e, n);
for (const i of s) {
if (c.has(i.key)) continue;
const a = i.after?.key ?? i.before?.key ?? i.key;
if (!(i.after ? envSnapshotEntriesEqual(n.get(a), i.after) : !n.has(a)) || !envSnapshotEntriesEqual(r.get(a), i.afterOwned)) continue;
if (!envSnapshotEntriesEqual(i.before, i.after)) replaceEnvSnapshotEntry(e, n.get(a), i.before);
replaceEnvSnapshotEntry(t, r.get(a), i.beforeOwned);
}
appliedConfigEnvOwnership.set(e, t);
normalizeFsSafeNativeEnv(e);
};
n?.(restore);
if (a?.active && a.env === e) a.receipts.push(restore);
}
}
function cloneEnvWithPlatformSemantics(e) {
let t = { ...fsSafeEnvInput(e) };
if (process.platform === "win32") t = new Proxy(t, {
deleteProperty(e, t) {
if (typeof t !== "string") return Reflect.deleteProperty(e, t);
const n = findCaseInsensitiveEnvKey(e, t);
return n ? Reflect.deleteProperty(e, n) : true;
},
get(e, t, n) {
if (typeof t !== "string") return Reflect.get(e, t, n);
const r = findCaseInsensitiveEnvKey(e, t);
return r ? e[r] : Reflect.get(e, t, n);
},
getOwnPropertyDescriptor(e, t) {
if (typeof t !== "string") return Reflect.getOwnPropertyDescriptor(e, t);
const n = findCaseInsensitiveEnvKey(e, t);
if (!n) return;
return {
configurable: true,
enumerable: true,
value: e[n],
writable: true
};
},
has(e, t) {
return typeof t === "string" ? findCaseInsensitiveEnvKey(e, t) !== void 0 : Reflect.has(e, t);
},
set(e, t, n) {
if (typeof t !== "string") return Reflect.set(e, t, n);
e[findCaseInsensitiveEnvKey(e, t) ?? t] = n;
return true;
}
});
appliedConfigEnvOwnership.set(t, resolveAppliedConfigEnvOwnership(e));
normalizeFsSafeNativeEnv(t);
return t;
}
function createConfigRuntimeEnv(e, t = process.env) {
const n = cloneEnvWithPlatformSemantics(t);
applyConfigEnvVars(e, n);
return n;
}
let publishedConfigRuntimeEnvState = {
ownedEnv: {},
sourceConfig: null
};
function getPublishedConfigRuntimeEnvState() {
return publishedConfigRuntimeEnvState;
}
function collectConfigRuntimeEnvOwnership(e, t, n, r = {}) {
const i = fsSafeEnvInput(t);
const a = fsSafeEnvInput(n);
const o = {};
const s = new Set((r.replacedLowerPrecedenceKeys ?? []).map(envSnapshotKey));
for (const [t, n] of Object.entries(collectConfigRuntimeEnvVars(e))) for (const e of resolveEnvNormalizationKeys(t)) {
const t = findCaseInsensitiveEnvKey(a, e);
if (!t || a[t] !== n) continue;
const r = findCaseInsensitiveEnvKey(i, e);
if (r && i[r] === n && !s.has(envSnapshotKey(t))) continue;
o[t] = n;
}
return o;
}
function filterConfigRuntimeEnvOwnership(e, t, n) {
const r = fsSafeEnvInput(t);
const i = indexConfigRuntimeEnvValues(collectConfigRuntimeEnvVars(e));
const a = {};
for (const [e, t] of Object.entries(n)) {
const n = resolveEnvNormalizationKeys(e)[0] ?? e;
const o = findCaseInsensitiveEnvKey(r, e);
if (o && r[o] === t && i.get(n)?.has(t)) a[o] = t;
}
return a;
}
function initializePublishedConfigRuntimeEnv(e, t = {}) {
publishedConfigRuntimeEnvState = {
ownedEnv: filterConfigRuntimeEnvOwnership(e, process.env, t.preserveExistingOwnership ? {
...publishedConfigRuntimeEnvState.ownedEnv,
...t.ownedEnv
} : t.ownedEnv ?? {}),
sourceConfig: e
};
}
function resetPublishedConfigRuntimeEnv(e = {}) {
if (!e.preserveOwnership) appliedConfigEnvOwnership.delete(process.env);
publishedConfigRuntimeEnvState = e.preserveOwnership ? { ...publishedConfigRuntimeEnvState } : {
ownedEnv: {},
sourceConfig: null
};
}
function createConfigRuntimeEnvBase(e, t = process.env, n = {}) {
const r = cloneEnvWithPlatformSemantics(t);
clearFsSafeEnvFallback(r);
const i = filterConfigRuntimeEnvOwnership(e, t, n.ownedEnv ?? resolveAppliedConfigEnvOwnership(t));
for (const [e, t] of Object.entries(i)) {
if (n.preservedKeys?.has(e.toUpperCase())) continue;
if (r[e] === t) delete r[e];
}
normalizeFsSafeNativeEnv(r);
return r;
}
function prepareConfigRuntimeEnv(e) {
const t = e.env ?? process.env;
const n = snapshotEnvByPlatformKey(t);
const r = createConfigRuntimeEnvBase(e.previousConfig, t);
const i = { ...fsSafeEnvInput(r) };
applyConfigEnvVars(e.nextConfig, r);
const a = collectConfigRuntimeEnvOwnership(e.nextConfig, i, r);
return prepareConfigRuntimeEnvPublication({
targetEnv: t,
before: n,
preparedEnv: r,
configState: {
sourceConfig: e.nextConfig,
ownedEnv: a
}
});
}
function prepareConfigRuntimeEnvLoad(e) {
const t = e.env ?? process.env;
const n = cloneEnvWithPlatformSemantics(t);
const r = snapshotEnvByPlatformKey(n);
const i = createConfigRuntimeEnvBase(e.previousConfig, t, { preservedKeys: e.preservedKeys });
const a = snapshotEnvByPlatformKey(i);
const o = Object.fromEntries(Object.entries(resolveAppliedConfigEnvOwnership(t)).filter(([e, t]) => a.get(envSnapshotKey(e))?.value === t));
let s = cloneEnvWithPlatformSemantics(i);
return {
env: i,
captureDotEnvBaseline: () => {
s = cloneEnvWithPlatformSemantics(i);
},
prepare: (e) => {
const n = cloneEnvWithPlatformSemantics(i);
return prepareConfigRuntimeEnvPublication({
targetEnv: t,
before: r,
preparedEnv: n,
configState: {
sourceConfig: e,
ownedEnv: {
...filterConfigRuntimeEnvOwnership(e, n, o),
...collectConfigRuntimeEnvOwnership(e, s, n)
}
}
});
},
prepareFailure: () => {
const e = cloneEnvWithPlatformSemantics(n);
const i = snapshotEnvByPlatformKey(s);
for (const t of /* @__PURE__ */ new Set([...a.keys(), ...i.keys()])) {
const n = r.get(t);
const o = a.get(t);
const s = i.get(t);
if (envSnapshotEntriesEqual(n, o) && !envSnapshotEntriesEqual(o, s)) replaceEnvSnapshotEntry(e, n, s);
}
return prepareConfigRuntimeEnvPublication({
targetEnv: t,
before: r,
preparedEnv: e
});
}
};
}
function prepareConfigRuntimeEnvPublication(e) {
const { targetEnv: t, before: n, preparedEnv: r } = e;
normalizeFsSafeNativeEnv(r);
const i = snapshotEnvByPlatformKey(r);
return {
env: r,
publish: () => {
const r = t === process.env;
const a = publishedConfigRuntimeEnvState;
const o = resolveAppliedConfigEnvOwnership(t);
const s = /* @__PURE__ */ new Map();
const c = /* @__PURE__ */ new Set([...n.keys(), ...i.keys()]);
let l;
for (const e of c) {
const r = n.get(e);
const a = i.get(e);
const o = (l ??= snapshotEnvByPlatformKey(t)).get(e);
if (envSnapshotEntriesEqual(r, a) || !envSnapshotEntriesEqual(o, r)) continue;
s.set(e, {
before: o,
after: a
});
if (!envSnapshotEntriesEqual(o, a)) replaceEnvSnapshotEntry(t, o, a);
}
normalizeFsSafeNativeEnv(t);
let u = null;
if (r) {
const n = {};
let r;
for (const [a, c] of Object.entries(e.configState?.ownedEnv ?? {})) {
const e = envSnapshotKey(a);
const l = (r ??= snapshotEnvByPlatformKey(t)).get(e);
const u = i.get(e);
const d = findCaseInsensitiveEnvKey(o, a);
if (l?.value === c && envSnapshotEntriesEqual(l, u) && (s.has(e) || d !== void 0 && o[d] === c)) n[l.key] = c;
}
publishedConfigRuntimeEnvState = {
ownedEnv: e.configState ? n : a.ownedEnv,
sourceConfig: e.configState?.sourceConfig ?? a.sourceConfig
};
u = publishedConfigRuntimeEnvState;
}
let d = true;
const settle = (e) => {
if (!d) return;
d = false;
if (e) return;
if (u && publishedConfigRuntimeEnvState !== u) return;
let n;
for (const [e, r] of s) {
const i = (n ??= snapshotEnvByPlatformKey(t)).get(e);
if (!envSnapshotEntriesEqual(i, r.after)) continue;
replaceEnvSnapshotEntry(t, i, r.before);
}
normalizeFsSafeNativeEnv(t);
if (u) publishedConfigRuntimeEnvState = { ...a };
};
return Object.assign(() => settle(false), { commit: () => settle(true) });
}
};
}
/** Applies config env vars to an environment without overwriting existing non-empty values. */
function applyConfigEnvVars(e, t = process.env, n = {}) {
clearFsSafeEnvFallback(t);
const r = { ...t };
const i = resolveAppliedConfigEnvOwnership(t);
const a = collectConfigRuntimeEnvVars(e);
const o = Object.entries(n.lowerPrecedenceEnv ?? {});
const s = new Map(o.map(([e, t]) => [envSnapshotKey(e), t]));
const c = expandEnvNormalizationKeys(Object.keys(a));
const l = indexConfigRuntimeEnvValues(a);
const u = /* @__PURE__ */ new Map();
for (const e of Object.keys(a)) {
const n = resolveEnvNormalizationKeys(e);
const r = n.map((e) => [e, t[e]]).find(([e, t]) => t?.trim() && s.get(e) !== t && !l.get(e)?.has(t))?.[1];
if (r !== void 0) for (const e of n) u.set(e, r);
}
const d = [];
for (const [e, n] of o) if (c.has(envSnapshotKey(e)) && t[e] === n) {
delete t[e];
d.push(e);
}
if (d.length > 0) n.onLowerPrecedenceKeysReplaced?.(d);
for (const [e, n] of Object.entries(a)) {
const r = u.get(envSnapshotKey(e));
if (r !== void 0) {
t[e] = r;
continue;
}
const i = t[e];
if (i?.trim() && s.get(envSnapshotKey(e)) !== i) continue;
t[e] = n;
}
normalizeZaiEnv(t);
appliedConfigEnvOwnership.set(t, {
...filterConfigRuntimeEnvOwnership(e, t, i),
...collectConfigRuntimeEnvOwnership(e, r, t, { replacedLowerPrecedenceKeys: d })
});
normalizeFsSafeNativeEnv(t);
}
//#endregion
export { ALLOW_OLDER_BINARY_DESTRUCTIVE_ACTIONS_ENV as _, createConfigRuntimeEnv as a, initializePublishedConfigRuntimeEnv as c, resetPublishedConfigRuntimeEnv as d, restoreEnvChangesIfUnchanged as f, isConfigRuntimeEnvVarAllowed as g, collectConfigRuntimeEnvVars as h, collectConfigRuntimeEnvOwnership as i, prepareConfigRuntimeEnv as l, withConfigReadEnvChanges as m, captureConfigReadEnvMutation as n, createConfigRuntimeEnvBase as o, snapshotEnv as p, cloneEnvWithPlatformSemantics as r, getPublishedConfigRuntimeEnvState as s, applyConfigEnvVars as t, prepareConfigRuntimeEnvLoad as u, formatFutureConfigActionBlock as v, resolveFutureConfigActionBlock as y };