UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

165 lines (164 loc) • 5.29 kB
import { r as asNullableRecord } from "./record-coerce-CVPuDqcc.mjs"; import { f as normalizeStringEntries } from "./string-normalization-DLmfYZXI.mjs"; //#region src/channels/plugins/dm-access.ts function normalizeChannelDmPolicy(e) { return e === "pairing" || e === "allowlist" || e === "open" || e === "disabled" ? e : void 0; } function resolveDmFieldPaths(e, t) { const n = t === "policy" ? "dmPolicy" : "allowFrom"; const r = t === "policy" ? "policy" : "allowFrom"; if (e === "nestedOnly") return { canonicalPath: ["dm", r], legacyPath: [n] }; return { canonicalPath: [n], legacyPath: ["dm", r] }; } function readPath(e, t) { let n = e; for (const e of t) { const t = asNullableRecord(n); if (!t) return; n = t[e]; } return n; } function deletePath(e, t) { const [n, r] = t; if (r === void 0) { if (e[n] === void 0) return false; delete e[n]; return true; } const i = asNullableRecord(e[n]); if (!i || i[r] === void 0) return false; delete i[r]; if (Object.keys(i).length === 0) delete e[n]; else e[n] = i; return true; } function writePath(e, t, n) { const [r, i] = t; if (i === void 0) { e[r] = n; return; } const a = asNullableRecord(e[r]); const o = a ? { ...a } : {}; o[i] = n; e[r] = o; } function allowFromListsMatch(e, t) { if (!Array.isArray(e) || !Array.isArray(t)) return false; const n = normalizeStringEntries(e); const r = normalizeStringEntries(t); if (n.length !== r.length) return false; return n.every((e, t) => e === r[t]); } function formatPath(e, t) { return `${e}.${t.join(".")}`; } function readCanonicalOrLegacy(e, t, n) { const r = resolveDmFieldPaths(t, n); return readPath(e, r.canonicalPath) ?? readPath(e, r.legacyPath); } function resolveChannelDmPolicy(e) { const t = e.mode ?? "topOnly"; const n = readCanonicalOrLegacy(e.account, t, "policy") ?? readCanonicalOrLegacy(e.parent, t, "policy") ?? e.defaultPolicy; return typeof n === "string" ? normalizeChannelDmPolicy(n) : void 0; } function resolveChannelDmAllowFrom(e) { const t = e.mode ?? "topOnly"; const n = readCanonicalOrLegacy(e.account, t, "allowFrom") ?? readCanonicalOrLegacy(e.parent, t, "allowFrom"); return Array.isArray(n) ? n : void 0; } function resolveChannelDmAccess(e) { return { dmPolicy: resolveChannelDmPolicy(e), allowFrom: resolveChannelDmAllowFrom(e) }; } function setCanonicalDmAllowFrom(e) { const t = resolveDmFieldPaths(e.mode, "allowFrom"); writePath(e.entry, t.canonicalPath, [...e.allowFrom]); if (deletePath(e.entry, t.legacyPath)) e.changes?.push(`- ${formatPath(e.pathPrefix, t.legacyPath)}: removed after moving allowlist to ${formatPath(e.pathPrefix, t.canonicalPath)}`); e.changes?.push(`- ${formatPath(e.pathPrefix, t.canonicalPath)}: ${e.reason}`); } function normalizeLegacyDmAliases(e) { const t = asNullableRecord(e.entry.dm); if (!t) return { entry: e.entry, changed: false }; const n = { ...t }; let r = { ...e.entry }; let i = false; for (const [t, a] of [["dmPolicy", "policy"], ["allowFrom", "allowFrom"]]) { if (t === "allowFrom" && e.promoteAllowFrom === false) continue; const o = r[t]; const s = n[a]; if (s === void 0) continue; if (o === void 0) { r[t] = s; e.changes.push(`Moved ${e.pathPrefix}.dm.${a} → ${e.pathPrefix}.${t}.`); } else if (t === "dmPolicy" ? o === s : allowFromListsMatch(o, s)) e.changes.push(`Removed ${e.pathPrefix}.dm.${a} (${t} already set).`); else continue; delete n[a]; i = true; } if (!i) return { entry: e.entry, changed: false }; if (Object.keys(n).length === 0) { const { dm: t, ...n } = r; r = n; e.changes.push(`Removed empty ${e.pathPrefix}.dm after migration.`); } else r = { ...r, dm: n }; return { entry: r, changed: true }; } function hasWildcard(e) { return e?.some((e) => String(e).trim() === "*") ?? false; } function ensureOpenDmPolicyAllowFromWildcard(e) { if (resolveChannelDmPolicy({ account: e.entry, mode: e.mode }) !== "open") return; const t = resolveDmFieldPaths(e.mode, "policy"); const n = readPath(e.entry, t.canonicalPath); const r = readPath(e.entry, t.legacyPath); if (n === void 0 && r === "open") { writePath(e.entry, t.canonicalPath, "open"); deletePath(e.entry, t.legacyPath); e.changes.push(`- ${formatPath(e.pathPrefix, t.canonicalPath)}: set to "open" (migrated from ${formatPath(e.pathPrefix, t.legacyPath)})`); } const i = resolveDmFieldPaths(e.mode, "allowFrom"); const a = readPath(e.entry, i.canonicalPath); const o = readPath(e.entry, i.legacyPath); const s = Array.isArray(a) ? a : Array.isArray(o) ? o : void 0; let c; const l = hasWildcard(s); if (l) { if (a !== void 0 || !s) return; c = s; } else c = [...s ?? [], "*"]; setCanonicalDmAllowFrom({ entry: e.entry, mode: e.mode, allowFrom: c, pathPrefix: e.pathPrefix, changes: e.changes, reason: l ? `moved wildcard allowlist from ${formatPath(e.pathPrefix, i.legacyPath)}` : Array.isArray(s) ? "added \"*\" (required by dmPolicy=\"open\")" : "set to [\"*\"] (required by dmPolicy=\"open\")" }); } //#endregion export { resolveChannelDmAllowFrom as a, resolveChannelDmAccess as i, normalizeChannelDmPolicy as n, resolveChannelDmPolicy as o, normalizeLegacyDmAliases as r, setCanonicalDmAllowFrom as s, ensureOpenDmPolicyAllowFromWildcard as t };