UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

120 lines (119 loc) • 5.05 kB
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import "./src-H3RMXyFL.mjs"; import { t as expectDefined } from "./expect-DDakjvvQ.mjs"; import { r as resolvePathViaExistingAncestorSync } from "./boundary-path-C2gXEiXe.mjs"; import { i as INCLUDE_KEY, s as isInternalIncludeWriteTarget } from "./includes-wjVi5Ymt.mjs"; import "./utils-C8YECn2Q.mjs"; import path from "node:path"; import { isDeepStrictEqual } from "node:util"; //#region src/config/include-write-boundary.ts function collectInto(e, t, n, r) { if (isDeepStrictEqual(e, t)) return; if (!isRecord(e) || !isRecord(t)) { r.push([...n]); return; } for (const i of /* @__PURE__ */ new Set([...Object.keys(e), ...Object.keys(t)])) collectInto(Object.hasOwn(e, i) ? e[i] : void 0, Object.hasOwn(t, i) ? t[i] : void 0, [...n, i], r); } /** * Lists the changed keyed paths between two authored configs. Arrays and * primitives compare whole, so a changed array reports its own path rather than * per-index paths an include boundary could not own positionally. */ function collectChangedConfigPaths(e, t) { const n = []; collectInto(e, t, [], n); const r = n.some((e) => e.length === 0); return { paths: r ? [] : n, rootChanged: r }; } function getLegacyTopLevelIncludeBoundary(e) { if (!isRecord(e.snapshot.parsed) || e.changed.rootChanged) return null; const t = new Set(e.changed.paths.map((e) => e[0]).filter((e) => e !== void 0)); if (t.size !== 1) return null; const n = expectDefined([...t][0], "changed top-level key at 0"); const r = e.snapshot.parsed[n]; if (!isRecord(r)) return null; const i = r[INCLUDE_KEY]; if (Object.keys(r).length !== 1 || typeof i !== "string") return null; const a = path.dirname(e.snapshot.path); return { boundaryPath: [n], includePath: path.normalize(path.isAbsolute(i) ? i : path.resolve(a, i)) }; } function isPathPrefix(e, t) { return e.length <= t.length && e.every((e, n) => e === t[n]); } function pathsEqual(e, t) { return e.length === t.length && e.every((e, n) => e === t[n]); } function pathTouchesAgentRoster(e) { return [["agents", "entries"], ["agents", "list"]].some((t) => isPathPrefix(e, t) || isPathPrefix(t, e)); } function isKeyedAgentEntryPath(e) { return e.length === 3 && e[0] === "agents" && e[1] === "entries"; } function isSoleOwner(e) { return e.kind === "single" && !e.hasSiblingOverrides; } /** * Finds the deepest authored include that solely owns every changed path. * * A boundary is writable only when it names exactly one file, carries no * sibling overrides, every enclosing include is itself a sole owner — * otherwise an ancestor could merge over the included content — and no deeper * include was recorded beneath it, since a file that still authors $include * directives cannot absorb a write. The deepest such boundary wins so the * write stays in the narrowest owning file rather than flattening a nested * include into its parent. */ function resolveIncludeWriteBoundary(e) { const t = e.provenance; if (!t || e.changed.rootChanged || e.changed.paths.length === 0) return null; if (t.some((e) => e.path.length === 0)) return null; const n = t.flatMap((e) => e.targetPaths ?? (e.targetPath ? [e.targetPath] : [])); const r = new Map([...new Set(n)].map((e) => [e, resolvePathViaExistingAncestorSync(e)])); let i = null; for (const n of t) { if (!isSoleOwner(n) || !n.targetPath || n.hasArrayAncestor) continue; if (t.some((e) => e !== n && e.path.length <= n.path.length && isPathPrefix(e.path, n.path) && !isSoleOwner(e))) continue; if (t.some((e) => e !== n && e.path.length > n.path.length && isPathPrefix(n.path, e.path))) continue; if (!e.changed.paths.every((e) => isPathPrefix(n.path, e))) continue; const a = r.get(n.targetPath); if (t.some((e) => !pathsEqual(e.path, n.path) && (e.targetPaths ?? (e.targetPath ? [e.targetPath] : [])).some((e) => r.get(e) === a))) continue; if (n.path.length > (i?.boundaryPath.length ?? 0)) i = { boundaryPath: n.path, includePath: n.targetPath }; } return i; } /** Exact keyed-entry includes whose authored pointers may survive a root-owned roster edit. */ function resolveKeyedAgentEntryIncludePreservation(e) { const t = e.provenance; if (!t) return null; const n = t.filter((e) => pathTouchesAgentRoster(e.path)); if (n.length === 0 || n.some((e) => !isKeyedAgentEntryPath(e.path))) return null; const r = []; for (const i of n) { if (n.filter((e) => pathsEqual(e.path, i.path)).length !== 1) return null; const a = resolveIncludeWriteBoundary({ provenance: t, changed: { paths: [[...i.path, "$value"]], rootChanged: false } }); if (!a || !pathsEqual(a.boundaryPath, i.path) || a.includePath !== i.targetPath || !isInternalIncludeWriteTarget({ configPath: e.configPath, includePath: a.includePath })) return null; r.push([...i.path]); } return { includePaths: r }; } //#endregion export { resolveKeyedAgentEntryIncludePreservation as i, getLegacyTopLevelIncludeBoundary as n, resolveIncludeWriteBoundary as r, collectChangedConfigPaths as t };