UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

164 lines (163 loc) • 7.57 kB
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import "./src-H3RMXyFL.mjs"; import { t as expectDefined } from "./expect-DDakjvvQ.mjs"; import { t as isBlockedObjectKey } from "./prototype-keys-CLGi5xDj.mjs"; import { t as parseConfigPathArrayIndex } from "./path-array-index-qP-D1DoI.mjs"; import { r as parseConcreteConfigPath, s as toDotPath } from "./dot-path-BJ4tyWKJ.mjs"; import { t as normalizePluginPolicyId } from "./plugin-policy-id-CkilMowp.mjs"; import { o as readConfigWritePendingMigrations, r as formatDeferredPluginMigration } from "./deferred-plugin-migrations-C_CU0aKR.mjs"; import { t as collectPluginConfigContractMatches } from "./config-contract-matches-BYS12x_-.mjs"; import { isDeepStrictEqual } from "node:util"; //#region src/config/config-path-mutation.ts const MANAGED_CONFIG_UNSET_PATHS = [["plugins", "installs"]]; const WRITE_PRUNED_OBJECT = Symbol("write-pruned-object"); function unsetPathForWriteAt(e, t, n) { if (n >= t.length) return e; const r = expectDefined(t[n], "path segments entry at depth"); const i = n === t.length - 1; if (Array.isArray(e)) { const a = parseConfigPathArrayIndex(r); if (a === void 0 || a >= e.length) return e; const o = i ? WRITE_PRUNED_OBJECT : unsetPathForWriteAt(e[a], t, n + 1); if (Object.is(o, e[a])) return e; const s = e.slice(); if (o === WRITE_PRUNED_OBJECT) s.splice(a, 1); else s[a] = o; return s; } if (isBlockedObjectKey(r) || !isRecord(e) || !Object.hasOwn(e, r)) return e; const a = i ? WRITE_PRUNED_OBJECT : unsetPathForWriteAt(e[r], t, n + 1); if (Object.is(a, e[r])) return e; const o = { ...e }; if (a === WRITE_PRUNED_OBJECT) delete o[r]; else o[r] = a; return Object.keys(o).length === 0 ? WRITE_PRUNED_OBJECT : o; } function applyUnsetPathsForWrite(e, t) { let n = e; for (const e of t ?? []) { if (!Array.isArray(e) || e.length === 0) continue; const t = unsetPathForWriteAt(n, e, 0); if (t === WRITE_PRUNED_OBJECT) n = {}; else if (isRecord(t)) n = t; } return n; } function resolveManagedUnsetPathsForWrite(e) { const t = []; for (const e of MANAGED_CONFIG_UNSET_PATHS) t.push(Array.from(e)); for (const n of e ?? []) { if (!Array.isArray(n) || n.length === 0) continue; if (t.some((e) => isDeepStrictEqual(e, n))) continue; t.push([...n]); } return t; } //#endregion //#region src/config/deferred-plugin-migration-config.ts const snapshotMigrationFacts = /* @__PURE__ */ new WeakMap(); /** Revalidation consumes the same pending generation that admitted this source snapshot. */ function setDeferredPluginMigrationConfigFacts(e, t) { if (t !== void 0) snapshotMigrationFacts.set(e, structuredClone(t)); } function getDeferredPluginMigrationConfigFacts(e) { return isRecord(e) ? snapshotMigrationFacts.get(e) : void 0; } async function readConfigWriteMigrationFacts(e, t, n) { return await readConfigWritePendingMigrations(e.path, t, n?.deferredPluginMigrations ?? getDeferredPluginMigrationConfigFacts(e.sourceConfig)); } function readPathValue(e, t) { let n = e; for (const e of t) { if (isBlockedObjectKey(e)) return; if (Array.isArray(n)) { const t = parseConfigPathArrayIndex(e); n = t === void 0 ? void 0 : n[t]; } else n = isRecord(n) && Object.hasOwn(n, e) ? n[e] : void 0; } return n; } function uniquePaths(e) { return [...new Map(e.map((e) => [JSON.stringify(e), e])).values()]; } function pluginConfigPaths(e, t) { const n = isRecord(e) && isRecord(e.plugins) ? e.plugins.entries : void 0; return isRecord(n) ? Object.keys(n).filter((e) => normalizePluginPolicyId(e) === normalizePluginPolicyId(t)).map((e) => [ "plugins", "entries", e, "config" ]) : []; } /** Empty plugin config has no settings; opaque legacy values still belong to their owner. */ function hasDeferredPluginMigrationConfig(e, t) { if ((t.validationExcludedPaths ?? []).some((t) => readPathValue(e, t) !== void 0)) return true; const n = pluginConfigPaths(e, t.pluginId); return [...t.configPaths ?? [], ...n].some((t) => { const r = readPathValue(e, t); return r !== void 0 && !(n.some((e) => e.length === t.length && e.every((e, n) => e === t[n])) && isRecord(r) && Object.keys(r).length === 0); }); } /** Preserve only current source inputs owned by the deferred plugin or the shared session locator. */ function resolveDeferredPluginMigrationConfigPaths(e) { const t = (e.compatibilityMigrationPaths ?? []).flatMap((t) => collectPluginConfigContractMatches({ root: e.config, pathPattern: t }).map(({ path: e }) => parseConcreteConfigPath(e))); const n = uniquePaths([ ...pluginConfigPaths(e.config, e.pluginId), ["session", "store"], ...t ]).filter((t) => readPathValue(e.config, t) !== void 0); return { ...n.length > 0 ? { configPaths: n } : {}, ...t.length > 0 ? { validationExcludedPaths: uniquePaths(t) } : {} }; } function restorePath(e, t, n) { const [r, ...i] = n; if (r === void 0) return structuredClone(e); if (isBlockedObjectKey(r)) return t; if (Array.isArray(e)) { const n = parseConfigPathArrayIndex(r); if (n === void 0 || n >= e.length) return t; const a = Array.isArray(t) ? [...t] : []; a[n] = restorePath(e[n], a[n], i); return a; } if (!isRecord(e) || !Object.hasOwn(e, r)) return t; const a = isRecord(t) ? { ...t } : {}; a[r] = restorePath(e[r], a[r], i); return a; } function retainedPluginPaths(e, t, n) { return (e.configPaths ?? []).filter((r) => { if (r[0] !== "plugins" || r[1] !== "entries" || normalizePluginPolicyId(r[2] ?? "") !== normalizePluginPolicyId(e.pluginId)) return true; const i = r.slice(0, 3); return !n?.some((e) => isDeepStrictEqual(e, i)) || readPathValue(t, i) !== void 0; }); } /** Explicit edits must not report success after preservation restores their old values. */ function assertDeferredPluginMigrationConfigEditAllowed(e) { for (const t of e.pending) for (const n of retainedPluginPaths(t, e.nextConfig, e.unsetPaths)) if (e.editedPaths.some((e) => n.every((t, n) => e[n] === t) || e.every((e, t) => n[t] === e)) && !isDeepStrictEqual(readPathValue(e.sourceConfig, n), readPathValue(e.nextConfig, n))) throw new Error(`Cannot edit retained config at "${toDotPath(n)}". ${formatDeferredPluginMigration(t)}`); } /** The current config snapshot owns retained values; migration receipts contain paths only. */ function preserveDeferredPluginMigrationConfig(e) { const { explicitSetPaths: t, unsetPaths: n, auditOrigin: r } = e.writeOptions ?? {}; const i = applyUnsetPathsForWrite(e.nextConfig, n); if (e.pending.length > 0) assertDeferredPluginMigrationConfigEditAllowed({ ...e, nextConfig: i, unsetPaths: n, editedPaths: r === "config-rpc" ? [[]] : [...t ?? [], ...n ?? []] }); let a = e.nextConfig; for (const t of e.pending) for (const r of retainedPluginPaths(t, i, n)) if (r.length > 0) a = restorePath(e.sourceConfig, a, r); return isRecord(a) ? a : e.nextConfig; } /** Retained plugin-owned legacy fields are inert until their migration owner becomes available. */ function omitDeferredPluginMigrationConfig(e, t) { return isRecord(e) ? applyUnsetPathsForWrite(e, t?.flatMap((e) => e.validationExcludedPaths ?? [])) : e; } //#endregion export { preserveDeferredPluginMigrationConfig as a, setDeferredPluginMigrationConfigFacts as c, omitDeferredPluginMigrationConfig as i, applyUnsetPathsForWrite as l, getDeferredPluginMigrationConfigFacts as n, readConfigWriteMigrationFacts as o, hasDeferredPluginMigrationConfig as r, resolveDeferredPluginMigrationConfigPaths as s, assertDeferredPluginMigrationConfigEditAllowed as t, resolveManagedUnsetPathsForWrite as u };