UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

145 lines (144 loc) • 4.37 kB
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { t as isPlainObject } from "./plain-object-CGY5w7fM.mjs"; import "./utils-C8YECn2Q.mjs"; import { n as formatConfigPatchPath, r as isMergePatchObjectKeyAllowed } from "./patch-replace-paths-GzVnRbCn.mjs"; import { isDeepStrictEqual } from "node:util"; //#region src/config/merge-patch.ts /** Builds a merge patch; ID-keyed array mode emits changed fields for upserts. */ function createMergePatch(e, t, n = {}) { if (!isRecord(e) || !isRecord(t)) return structuredClone(t); const r = {}; const i = /* @__PURE__ */ new Set([...Object.keys(e), ...Object.keys(t)]); for (const a of i) { const i = Object.hasOwn(e, a); if (!Object.hasOwn(t, a)) { r[a] = null; continue; } const o = t[a]; if (!i) { r[a] = structuredClone(o); continue; } const s = e[a]; if (n.mergeObjectArraysById && isIdKeyedArray(s) && isIdKeyedArray(o)) { const e = new Map(s.map((e) => [e.id, e])); const t = []; for (const r of o) { const i = e.get(r.id); const a = createMergePatch(i, applyMergePatch(i, r, n), n); if (isRecord(a) && Object.keys(a).length > 0) t.push({ ...a, id: r.id }); } if (t.length > 0) r[a] = t; continue; } if (isRecord(s) && isRecord(o)) { const e = createMergePatch(s, o, n); if (isRecord(e) && Object.keys(e).length === 0) continue; r[a] = e; continue; } if (!isDeepStrictEqual(s, o)) r[a] = structuredClone(o); } return r; } /** Whether a merge patch would replace a value changed since its source was read. */ function mergePatchConflicts(e, t, n, r = {}) { if (r.mergeObjectArraysById && isIdKeyedArray(e) && isIdKeyedArray(t) && isIdKeyedArray(n)) { const i = new Map(e.map((e) => [e.id, e])); const a = new Map(t.map((e) => [e.id, e])); return n.some((e) => mergePatchConflicts(i.get(e.id), a.get(e.id), e, r)); } if (!isRecord(n)) return !isDeepStrictEqual(e, t); const i = isRecord(e); const a = isRecord(t); if (i !== a && e !== void 0) return true; if (!i && !a && !isDeepStrictEqual(e, t)) return true; const o = i ? e : {}; const s = a ? t : {}; return Object.entries(n).some(([e, t]) => mergePatchConflicts(o[e], s[e], t, r)); } function isObjectWithStringId(e) { if (!isPlainObject(e)) return false; return typeof e.id === "string" && e.id.length > 0; } function isIdKeyedArray(e) { return Array.isArray(e) && e.every(isObjectWithStringId); } /** * Merge arrays of object-like entries keyed by `id`. * * Contract: * - Base array must be fully id-keyed; otherwise return undefined (caller should replace). * - Patch entries with valid id merge by id (or append when the id is new). * - Patch entries without valid id append as-is, avoiding destructive full-array replacement. */ function mergeObjectArraysById(e, t, n, r) { const i = [...e]; const a = /* @__PURE__ */ new Map(); for (const [e, t] of i.entries()) { if (!isObjectWithStringId(t)) return; a.set(t.id, e); } for (const e of t) { if (!isObjectWithStringId(e)) { i.push(structuredClone(e)); continue; } const t = a.get(e.id); if (t === void 0) { i.push(structuredClone(e)); a.set(e.id, i.length - 1); continue; } i[t] = applyMergePatch(i[t], e, { ...n, path: `${r}[]` }); } return i; } /** * Applies an RFC 7396-style object merge patch with OpenClaw config safeguards. * * Non-object patches replace the base, `null` deletes keys, blocked prototype * keys are ignored outside schema-owned record-key paths, and id-keyed arrays * may merge when the caller opts in. */ function applyMergePatch(e, t, n = {}) { if (!isPlainObject(t)) return t; const r = isPlainObject(e) ? { ...e } : {}; for (const [e, i] of Object.entries(t)) { const t = formatConfigPatchPath(n.path, e); if (!isMergePatchObjectKeyAllowed(e, n.path)) continue; if (i === null) { delete r[e]; continue; } if (n.mergeObjectArraysById && Array.isArray(r[e]) && Array.isArray(i)) { if (n.replaceArrayPaths?.has(t)) { r[e] = i; continue; } const a = mergeObjectArraysById(r[e], i, n, t); if (a) { r[e] = a; continue; } } if (isPlainObject(i)) { r[e] = applyMergePatch(r[e], i, { ...n, path: t }); continue; } r[e] = i; } return r; } //#endregion export { createMergePatch as n, mergePatchConflicts as r, applyMergePatch as t };