UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

157 lines (156 loc) • 5.61 kB
import { c as normalizeOptionalLowercaseString, l as normalizeOptionalString, o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs"; import { b as uniqueStrings, f as normalizeStringEntries } from "./string-normalization-DLmfYZXI.mjs"; import { t as dedupeByKey } from "./dedupe-by-key-Cpowmfct.mjs"; import { t as summarizeStringEntries } from "./string-sample-DyCB5Vr0.mjs"; import "./chat-target-prefixes-f6GGnTH0.mjs"; //#region src/channels/allowlists/resolve-utils.ts function dedupeAllowlistEntries(e, t = normalizeLowercaseStringOrEmpty) { return dedupeByKey(e.map((e) => e.trim()).filter(Boolean), t); } function mergeAllowlist(e) { return dedupeAllowlistEntries([...(e.existing ?? []).map(String), ...e.additions]); } function buildAllowlistResolutionSummary(e, t) { const n = new Map(e.map((e) => [e.input, e])); const resolvedOk = (e) => Boolean(e.resolved && e.id); const r = t?.formatResolved ?? ((e) => e.id === e.input ? null : `${e.input}→${e.id}`); const i = t?.formatUnresolved ?? ((e) => e.input); const a = e.filter(resolvedOk).map(r).filter((e) => e !== null); const o = e.filter(resolvedOk).map((e) => e.id).filter((e) => Boolean(e)); return { resolvedMap: n, mapping: a, unresolved: e.filter((e) => !resolvedOk(e)).map(i), additions: o }; } /** Replaces resolvable user entries with canonical ids while preserving unresolved entries and `*`. */ function canonicalizeAllowlistWithResolvedIds(e) { const t = []; for (const n of e.existing ?? []) { const r = normalizeOptionalString(n) ?? ""; if (!r) continue; if (r === "*") { t.push(r); continue; } const i = e.resolvedMap.get(r); t.push(i?.resolved && i.id ? i.id : r); } return dedupeAllowlistEntries(t, e.entryKey); } function patchAllowlistUsersInConfigEntries(e) { const t = { ...e.entries }; for (const [n, r] of Object.entries(e.entries)) { if (!r || typeof r !== "object") continue; const i = r.users; if (!Array.isArray(i) || i.length === 0) continue; let a; if (e.strategy === "canonicalize") a = canonicalizeAllowlistWithResolvedIds({ existing: i, resolvedMap: e.resolvedMap, entryKey: e.entryKey }); else { const t = e.resolvedMap; const n = []; for (const e of i) { const r = normalizeOptionalString(e) ?? ""; const i = t.get(r); if (i?.resolved && i.id) n.push(i.id); } a = mergeAllowlist({ existing: i, additions: n }); } t[n] = { ...r, users: a }; } return t; } /** Collects concrete user lookup targets from one config entry, excluding wildcard policy entries. */ function addAllowlistUserEntriesFromConfigEntry(e, t) { if (!t || typeof t !== "object") return; const n = t.users; if (!Array.isArray(n)) return; for (const t of n) { const n = normalizeOptionalString(t) ?? ""; if (n && n !== "*") e.add(n); } } function summarizeMapping(e, t, n, r) { if (t.length > 0) r.log?.(`${e} resolved: ${summarizeStringEntries({ entries: t, limit: 6 })}`); if (n.length > 0) r.log?.(`${e} unresolved: ${summarizeStringEntries({ entries: n, limit: 6 })}`); } //#endregion //#region src/plugin-sdk/allow-from.ts /** Lowercase and optionally strip prefixes from allowlist entries before sender comparisons. */ function formatAllowFromLowercase(e) { return normalizeStringEntries(e.allowFrom).map((t) => e.stripPrefixRe ? t.replace(e.stripPrefixRe, "") : t).map((e) => normalizeOptionalLowercaseString(e)).filter((e) => Boolean(e)); } /** Normalize allowlist entries through a channel-provided parser or canonicalizer. */ function formatNormalizedAllowFromEntries(e) { return normalizeStringEntries(e.allowFrom).map((t) => e.normalizeEntry(t)).filter((e) => Boolean(e)); } /** Parse, validate, and deduplicate setup allow-from entries with wildcard support. */ function parseAllowFromEntries(e, t) { const n = []; for (const r of normalizeStringEntries(e.split(/[\n,;]+/g))) { if (r === "*") { n.push(r); continue; } const e = t(r); if ("error" in e) return { entries: [], error: e.error }; n.push(e.value); } return { entries: uniqueStrings(normalizeStringEntries(n)) }; } /** Resolve basic setup allow-from entries when a channel token is available. */ async function resolveBasicAllowFromEntries(e) { const t = e.token?.trim(); if (!t) return e.entries.map((e) => ({ input: e, resolved: false, id: null })); return (await e.resolveEntries({ token: t, entries: e.entries })).map((e) => ({ input: e.input, resolved: e.resolved, id: e.id ?? null })); } /** Check whether a sender id matches a simple normalized allowlist with wildcard support. */ function isNormalizedSenderAllowed(e) { const t = formatAllowFromLowercase({ allowFrom: e.allowFrom, stripPrefixRe: e.stripPrefixRe }); if (t.length === 0) return false; if (t.includes("*")) return true; const n = normalizeOptionalLowercaseString(String(e.senderId)); return n ? t.includes(n) : false; } /** Map allowlist inputs sequentially so resolver side effects stay ordered and predictable. */ async function mapAllowlistResolutionInputs(e) { const t = []; for (const n of e.inputs) t.push(await e.mapInput(n)); return t; } //#endregion export { parseAllowFromEntries as a, buildAllowlistResolutionSummary as c, patchAllowlistUsersInConfigEntries as d, summarizeMapping as f, mapAllowlistResolutionInputs as i, canonicalizeAllowlistWithResolvedIds as l, formatNormalizedAllowFromEntries as n, resolveBasicAllowFromEntries as o, isNormalizedSenderAllowed as r, addAllowlistUserEntriesFromConfigEntry as s, formatAllowFromLowercase as t, mergeAllowlist as u };