UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

295 lines (294 loc) • 12.6 kB
import "./src-H3RMXyFL.mjs"; import { t as expectDefined } from "./expect-DDakjvvQ.mjs"; import { t as isPlainObject } from "./plain-object-CGY5w7fM.mjs"; import { a as scanEnvTemplateTokens, i as resolveConfigEnvVars, n as containsEnvVarReference } from "./env-substitution-CgVeOqUd.mjs"; import { isDeepStrictEqual } from "node:util"; //#region src/config/env-preserve.ts function containsAuthoredEnvTemplate(e, t) { if (typeof e === "string") return t(e); return (Array.isArray(e) ? e : isPlainObject(e) ? Object.values(e) : []).some((e) => containsAuthoredEnvTemplate(e, t)); } function containsAuthoredUnescapedEnvTemplate(e) { return containsAuthoredEnvTemplate(e, containsEnvVarReference); } function containsAuthoredEscapedEnvTemplate(e) { return containsAuthoredEnvTemplate(e, (e) => scanEnvTemplateTokens(e).some((e) => e.kind === "escaped")); } function addEnvRefCounts(e, t, n, r) { for (const i of scanEnvTemplateTokens(t)) if (i.kind === n) { const t = e.get(i.name) ?? /* @__PURE__ */ new Map(); const n = JSON.stringify(r); t.set(n, (t.get(n) ?? 0) + 1); e.set(i.name, t); } } /** * Keyed by bare variable name: `${VAR}` and `${VAR:-x}` are one identity, so changing * an authored `$${VAR}` literal into either active reference is rejected. */ function countAuthoredEnvRefsByPath(e, t) { const n = /* @__PURE__ */ new Map(); const visit = (e, r) => { if (typeof e === "string") { addEnvRefCounts(n, e, t, r); return; } if (Array.isArray(e)) { e.forEach((e, t) => visit(e, [...r, String(t)])); return; } if (isPlainObject(e)) Object.entries(e).forEach(([e, t]) => visit(t, [...r, e])); }; visit(e, []); return n; } function containsUnaccountedActiveEscapedEnvRef(e, t, n, r, i, a) { const o = countAuthoredEnvRefsByPath(t, "escaped"); const s = countAuthoredEnvRefsByPath(e, "substitution"); const c = countAuthoredEnvRefsByPath(e, "escaped"); const l = /* @__PURE__ */ new Map(); const visit = (e, t, n, r) => { if (typeof e === "string" && typeof t === "string") { if (!isDeepStrictEqual(e, n)) return; addEnvRefCounts(l, t, "substitution", r); return; } if (Array.isArray(e) && Array.isArray(t) && Array.isArray(n)) { t.forEach((t, i) => visit(e[i], t, n[i], [...r, String(i)])); return; } if (isPlainObject(e) && isPlainObject(t) && isPlainObject(n)) Object.entries(t).forEach(([t, i]) => visit(e[t], i, n[t], [...r, t])); }; visit(n, r, i, []); const u = countAuthoredEnvRefsByPath(r, "escaped"); return [...o].some(([e, t]) => { const isExplicitActivation = (e) => { if (!t.has(e) || !a?.length) return false; const n = JSON.parse(e); return a.some((e) => e.every((e, t) => n[t] === e)); }; return [...s.get(e) ?? /* @__PURE__ */ new Map()].some(([t, n]) => !isExplicitActivation(t) && n > (l.get(e)?.get(t) ?? 0)) || [...t.keys()].some((t) => { const n = s.get(e)?.get(t) ?? 0; return !isExplicitActivation(t) && n > 0 && (c.get(e)?.get(t) ?? 0) < (u.get(e)?.get(t) ?? 0); }); }); } function preservesAuthoredEscapedEnvRefs(e, t) { const n = countAuthoredEnvRefsByPath(t, "escaped"); const r = countAuthoredEnvRefsByPath(e, "escaped"); return [...n].every(([e, t]) => [...t].every(([t, n]) => (r.get(e)?.get(t) ?? 0) >= n)); } var EnvRefArrayMutationError = class extends Error { constructor() { super("Config write would reorder or modify an array containing environment references."); this.name = "EnvRefArrayMutationError"; } }; function hasEnvVarRef(e) { return scanEnvTemplateTokens(e).length > 0; } function getArrayIdentityPathValue(e, t) { let n = e; for (const e of t) { if (!isPlainObject(n)) return; n = n[e]; } return n; } function resolveStableArrayIdentityMatch(e) { const t = e.parsed[e.parsedIndex]; if (!isPlainObject(t)) return { kind: "none" }; const n = ["id", "agentId"].find((e) => typeof t[e] === "string" && !hasEnvVarRef(t[e])); if (!n) return { kind: "none" }; const r = t[n]; const matchesIdentity = (e) => isPlainObject(e) && isDeepStrictEqual(e[n], r); if (e.parsed.filter(matchesIdentity).length !== 1) return { kind: "none" }; const i = e.incoming.flatMap((e, t) => matchesIdentity(e) ? [t] : []); return i.length === 1 ? { kind: "match", incomingIndex: expectDefined(i[0], "env preserve identity match") } : { kind: "invalid" }; } function collectLiteralArrayIdentityPaths(e, t = []) { if (typeof e === "string") return hasEnvVarRef(e) ? [] : [t]; if (!isPlainObject(e)) return []; return Object.entries(e).flatMap(([e, n]) => collectLiteralArrayIdentityPaths(n, [...t, e])); } function hasStableSameIndexLiteralShape(e) { const t = e.parsed[e.parsedIndex]; const n = collectLiteralArrayIdentityPaths(t); if (n.length === 0 || n.some((n) => { const r = getArrayIdentityPathValue(t, n); return !isDeepStrictEqual(getArrayIdentityPathValue(e.incoming[e.parsedIndex], n), r); })) return false; return n.some((n) => { const r = getArrayIdentityPathValue(t, n); const i = e.parsed.filter((e) => isDeepStrictEqual(getArrayIdentityPathValue(e, n), r)).length; const a = e.incoming.filter((e) => isDeepStrictEqual(getArrayIdentityPathValue(e, n), r)).length; return i === 1 && a === 1; }); } function matchesArrayElementAtSameIndex(e, t, n) { return isDeepStrictEqual(e, t) || isDeepStrictEqual(e, n); } function matchesRetainedArrayItem(e) { if (matchesArrayElementAtSameIndex(e.incoming[e.incomingIndex], e.parsed[e.parsedIndex], e.resolved[e.parsedIndex])) return true; const t = resolveStableArrayIdentityMatch(e); return t.kind === "match" && t.incomingIndex === e.incomingIndex; } function canMatchEditedArrayItemAtSameIndex(e) { if (e.incoming.length !== e.parsed.length) return false; return e.incoming.length === 1 || hasStableSameIndexLiteralShape(e) || e.parsed.every((t, n) => n === e.parsedIndex || matchesArrayElementAtSameIndex(e.incoming[n], t, e.resolved[n])); } function matchUniqueRetainedArrayItems(e) { if (e.incoming.length >= e.parsed.length) return; const matchIndexes = (t) => { const n = []; let r = t === 1 ? 0 : e.parsed.length - 1; for (let i = t === 1 ? 0 : e.incoming.length - 1; i >= 0 && i < e.incoming.length; i += t) { while (r >= 0 && r < e.parsed.length && !matchesRetainedArrayItem({ ...e, incomingIndex: i, parsedIndex: r })) r += t; if (r < 0 || r >= e.parsed.length) return; n[i] = r; r += t; } return n; }; const t = matchIndexes(1); if (!t || !isDeepStrictEqual(t, matchIndexes(-1))) return; return new Map(t.map((e, t) => [e, t])); } function matchUneditedTemplateArrayItems(e, t, n) { if (e.incoming.length === e.parsed.length && e.incoming.every((t, n) => matchesArrayElementAtSameIndex(t, e.parsed[n], e.resolved[n]))) return new Map(t.map((e) => [e, e])); const r = matchUniqueRetainedArrayItems(e); if (r) return new Map(t.flatMap((e) => { const t = r.get(e); if (t === void 0) return []; if (n?.has(t)) throw new EnvRefArrayMutationError(); return [[e, t]]; })); } function matchAuthoredTemplateArrayItems(e, t, n) { const r = e.parsed.flatMap((e, n) => (t === "substitution" ? containsAuthoredUnescapedEnvTemplate(e) : containsAuthoredEscapedEnvTemplate(e) && !containsAuthoredUnescapedEnvTemplate(e)) ? [n] : []); const i = matchUneditedTemplateArrayItems(e, r, n); if (i) return i; const a = /* @__PURE__ */ new Map(); const o = new Set(n); const addMatch = (e, t) => { if (o.has(t)) throw new EnvRefArrayMutationError(); a.set(e, t); o.add(t); }; for (const n of r) { const i = e.parsed[n]; const a = resolveStableArrayIdentityMatch({ ...e, parsedIndex: n }); if (a.kind === "match") { addMatch(n, a.incomingIndex); continue; } if (t === "escaped") { const t = e.resolved[n]; const s = e.incoming.flatMap((e, n) => !o.has(n) && isDeepStrictEqual(e, t) ? [n] : []); const c = r.filter((n) => isDeepStrictEqual(e.resolved[n], t)); const l = c.every((t) => isDeepStrictEqual(e.parsed[t], i)); if (s.length > 0 && s.length <= c.length && l) { const e = s.includes(n) ? n : s[0]; addMatch(n, expectDefined(e, "env preserve same index match")); continue; } if (s.length > 0) throw new EnvRefArrayMutationError(); if ((isPlainObject(i) || Array.isArray(i)) && a.kind === "none" && n < e.incoming.length && !o.has(n) && canMatchEditedArrayItemAtSameIndex({ ...e, parsedIndex: n })) addMatch(n, n); continue; } if (a.kind === "invalid") throw new EnvRefArrayMutationError(); if (n < e.incoming.length && matchesArrayElementAtSameIndex(e.incoming[n], i, e.resolved[n])) { const t = e.parsed.slice(0, n).every((t, n) => matchesArrayElementAtSameIndex(e.incoming[n], t, e.resolved[n])); const r = e.parsed.some((t, r) => r !== n && matchesArrayElementAtSameIndex(e.incoming[n], t, e.resolved[r])); const a = e.incoming.some((t, r) => r !== n && matchesArrayElementAtSameIndex(t, i, e.resolved[n])); if (!(e.incoming.length === e.parsed.length || t) || r || a) throw new EnvRefArrayMutationError(); addMatch(n, n); continue; } if (isPlainObject(i) || Array.isArray(i)) { if (!canMatchEditedArrayItemAtSameIndex({ ...e, parsedIndex: n })) throw new EnvRefArrayMutationError(); addMatch(n, n); continue; } if (e.incoming.some((t, r) => r !== n && matchesArrayElementAtSameIndex(t, i, e.resolved[n]))) throw new EnvRefArrayMutationError(); if (n < e.incoming.length) addMatch(n, n); } return a; } function resolveEnvVarRefsForComparison(e, t) { if (typeof e === "string") return hasEnvVarRef(e) ? resolveConfigEnvVars(e, t, { onMissing: () => {} }) : e; if (Array.isArray(e)) return e.map((e) => resolveEnvVarRefsForComparison(e, t)); if (isPlainObject(e)) return Object.fromEntries(Object.entries(e).map(([e, n]) => [e, resolveEnvVarRefsForComparison(n, t)])); return e; } /** Restore authored references wherever their resolved values match the incoming config. */ function restoreEnvVarRefs(e, t, n = process.env) { return restoreEnvVarRefsFromResolved(e, t, resolveEnvVarRefsForComparison(t, n)); } /** Restore only references owned by the matching authored/resolved planning read. */ function restoreEnvVarRefsFromResolved(e, t, n, r) { if (typeof e === "string" && typeof t === "string") { if (hasEnvVarRef(e) && r?.some((e) => e.length === 0)) return e; return hasEnvVarRef(t) && n === e ? t : e; } const childExplicitPaths = (e) => r?.flatMap((t) => t.length === 0 ? [t] : t[0] === e ? [t.slice(1)] : []); if (Array.isArray(e) && Array.isArray(t) && Array.isArray(n)) { if (!containsAuthoredUnescapedEnvTemplate(t) && !containsAuthoredEscapedEnvTemplate(t)) return e.map((e, r) => r < t.length ? restoreEnvVarRefsFromResolved(e, t[r], n[r], childExplicitPaths(String(r))) : e); const r = { incoming: e, parsed: t, resolved: n }; const i = matchAuthoredTemplateArrayItems(r, "substitution"); const a = matchAuthoredTemplateArrayItems(r, "escaped", new Set(i.values())); const o = new Map([...i, ...a]); const s = [...e]; const c = new Set(o.values()); for (const [r, i] of o) s[i] = restoreEnvVarRefsFromResolved(e[i], t[r], n[r], childExplicitPaths(String(i))); for (let r = 0; r < e.length && r < t.length; r += 1) if (!c.has(r) && !containsAuthoredUnescapedEnvTemplate(t[r]) && !containsAuthoredEscapedEnvTemplate(t[r])) s[r] = restoreEnvVarRefsFromResolved(e[r], t[r], n[r], childExplicitPaths(String(r))); const l = new Map([...o].map(([e, t]) => [t, e])); for (const [r, i] of t.entries()) { if (!containsAuthoredEscapedEnvTemplate(i)) continue; const a = o.get(r); if (a !== void 0 && preservesAuthoredEscapedEnvRefs(s[a], i)) continue; const c = resolveStableArrayIdentityMatch({ incoming: e, parsed: t, parsedIndex: r }); if (s.some((a, o) => { const s = l.get(o); return containsUnaccountedActiveEscapedEnvRef(a, i, e[o], s === void 0 ? void 0 : t[s], s === void 0 ? void 0 : n[s], s === r && c.kind === "match" && c.incomingIndex === o ? childExplicitPaths(String(o)) : void 0); })) throw new EnvRefArrayMutationError(); } return s; } if (isPlainObject(e) && isPlainObject(t) && isPlainObject(n)) { const r = {}; for (const [i, a] of Object.entries(e)) if (Object.hasOwn(t, i)) r[i] = restoreEnvVarRefsFromResolved(a, t[i], n[i], childExplicitPaths(i)); else r[i] = a; return r; } return e; } function resolveWriteEnvSnapshotForPath(e) { if (e.expectedConfigPath === void 0 || e.expectedConfigPath === e.actualConfigPath) return e.envSnapshotForRestore; } //#endregion export { restoreEnvVarRefs as n, restoreEnvVarRefsFromResolved as r, resolveWriteEnvSnapshotForPath as t };