UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

276 lines (275 loc) • 9.07 kB
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { g as normalizeUniqueStringEntries } from "./string-normalization-DLmfYZXI.mjs"; import { t as isBlockedObjectKey } from "./prototype-keys-CLGi5xDj.mjs"; import { o as readObjectPath } from "./dot-path-BJ4tyWKJ.mjs"; import "./utils-C8YECn2Q.mjs"; import "./session-key-CqAyZeVk.mjs"; import { n as normalizeAccountId, t as DEFAULT_ACCOUNT_ID } from "./account-id-BqKT3GuY.mjs"; import { i as resolveChannelAccountKey } from "./account-lookup-FhQZVFfo.mjs"; //#region src/plugin-sdk/allowlist-config-edit.ts const DM_ALLOWLIST_CONFIG_PATHS = { readPaths: [["allowFrom"]], writePath: ["allowFrom"] }; const GROUP_ALLOWLIST_CONFIG_PATHS = { readPaths: [["groupAllowFrom"]], writePath: ["groupAllowFrom"] }; const LEGACY_DM_ALLOWLIST_CONFIG_PATHS = { readPaths: [["allowFrom"], ["dm", "allowFrom"]], writePath: ["allowFrom"], cleanupPaths: [["dm", "allowFrom"]] }; /** Resolve modern DM/group allowlist paths for account-scoped channel config writes. */ function resolveDmGroupAllowlistConfigPaths(e) { return e === "dm" ? DM_ALLOWLIST_CONFIG_PATHS : GROUP_ALLOWLIST_CONFIG_PATHS; } /** Resolve DM-only paths that still read and clean up the old nested dm.allowFrom location. */ function resolveLegacyDmAllowlistConfigPaths(e) { return e === "dm" ? LEGACY_DM_ALLOWLIST_CONFIG_PATHS : null; } /** Coerce stored allowlist entries into presentable non-empty strings. */ function readConfiguredAllowlistEntries(e) { return (e ?? []).map(String).filter(Boolean); } /** Collect labeled allowlist overrides from a flat keyed record. */ function collectAllowlistOverridesFromRecord(e) { const t = []; for (const [n, r] of Object.entries(e.record ?? {})) { if (!r) continue; const i = readConfiguredAllowlistEntries(e.resolveEntries(r)); if (i.length === 0) continue; t.push({ label: e.label(n, r), entries: i }); } return t; } /** Collect labeled allowlist overrides from an outer record with nested child records. */ function collectNestedAllowlistOverridesFromRecord(e) { const t = []; for (const [n, r] of Object.entries(e.record ?? {})) { if (!r) continue; const i = readConfiguredAllowlistEntries(e.resolveOuterEntries(r)); if (i.length > 0) t.push({ label: e.outerLabel(n, r), entries: i }); t.push(...collectAllowlistOverridesFromRecord({ record: e.resolveChildren(r), label: (t, r) => e.innerLabel(n, t, r), resolveEntries: e.resolveInnerEntries })); } return t; } /** Build an account-scoped flat override resolver from a keyed allowlist record. */ function createFlatAllowlistOverrideResolver(e) { return (t) => collectAllowlistOverridesFromRecord({ record: e.resolveRecord(t), label: e.label, resolveEntries: e.resolveEntries }); } /** Build an account-scoped nested override resolver from hierarchical allowlist records. */ function createNestedAllowlistOverrideResolver(e) { return (t) => collectNestedAllowlistOverridesFromRecord({ record: e.resolveRecord(t), outerLabel: e.outerLabel, resolveOuterEntries: e.resolveOuterEntries, resolveChildren: e.resolveChildren, innerLabel: e.innerLabel, resolveInnerEntries: e.resolveInnerEntries }); } /** Build the common account-scoped token-gated allowlist name resolver. */ function createAccountScopedAllowlistNameResolver(e) { return async ({ cfg: t, accountId: n, entries: r }) => { const i = e.resolveAccount({ cfg: t, accountId: n }); const a = e.resolveToken(i)?.trim(); if (!a) return []; return await e.resolveNames({ token: a, entries: r }); }; } function resolveAccountScopedWriteTarget(e, t, n) { const r = e.channels ??= {}; const i = r[t] ??= {}; const a = normalizeAccountId(n); if (isBlockedObjectKey(a) || a === "default" && !(i.accounts && typeof i.accounts === "object")) return { target: i, pathPrefix: `channels.${t}`, writeTarget: { kind: "channel", scope: { channelId: t } } }; const o = i.accounts ??= {}; const s = resolveChannelAccountKey(o, a, t, (e) => e) ?? a; const c = o[s]; if (!c || typeof c !== "object") o[s] = {}; return { target: o[s], pathPrefix: `channels.${t}.accounts.${s}`, writeTarget: { kind: "account", scope: { channelId: t, accountId: a } } }; } function setNestedValue(e, t, n) { const r = t.at(-1); if (r === void 0) return; let i = e; for (const e of t.slice(0, -1)) { const t = i[e]; if (!t || typeof t !== "object") i[e] = {}; i = i[e]; } i[r] = n; } function deleteNestedValue(e, t) { const n = t.at(-1); if (n === void 0) return; if (t.length === 1) { delete e[n]; return; } const r = readObjectPath(e, t.slice(0, -1)); if (!isRecord(r)) return; delete r[n]; } function applyAccountScopedAllowlistConfigEdit(e) { const t = resolveAccountScopedWriteTarget(e.parsedConfig, e.channelId, e.accountId); const n = []; let r = false; for (const i of e.paths.readPaths) { const e = readObjectPath(t.target, i); if (!Array.isArray(e)) continue; r = true; for (const t of e) n.push(t); } const i = normalizeUniqueStringEntries(r ? n : e.resolveEffectiveEntries?.() ?? []); const a = e.normalize([e.entry]); if (a.length === 0) return { kind: "invalid-entry" }; const o = e.normalize(i); const shouldMatch = (e) => a.includes(e); let s = false; let c = i; const l = o.some(shouldMatch); if (e.action === "add") { if (!l) { c = [...i, e.entry.trim()]; s = true; } } else { c = i.filter((t) => { return !e.normalize([t]).some(shouldMatch); }); s = c.length !== i.length; } if (s) { setNestedValue(t.target, e.paths.writePath, c); for (const n of e.paths.cleanupPaths ?? []) deleteNestedValue(t.target, n); } return { kind: "ok", changed: s, pathLabel: `${t.pathPrefix}.${e.paths.writePath.join(".")}`, writeTarget: t.writeTarget }; } /** Build the default account-scoped allowlist editor used by channel plugins with config-backed lists. */ function buildAccountScopedAllowlistConfigEditor(e) { return ({ cfg: t, parsedConfig: n, accountId: r, scope: i, action: a, entry: o }) => { const s = e.resolvePaths(i); if (!s) return null; return applyAccountScopedAllowlistConfigEdit({ parsedConfig: n, channelId: e.channelId, accountId: r, action: a, entry: o, normalize: (n) => e.normalize({ cfg: t, accountId: r, values: n }), resolveEffectiveEntries: () => e.resolveEffectiveEntries?.({ cfg: t, accountId: r, scope: i }), paths: s }); }; } function buildAccountAllowlistAdapter(e) { return { supportsScope: e.supportsScope, readConfig: ({ cfg: t, accountId: n }) => e.readConfig(e.resolveAccount({ cfg: t, accountId: n }), { cfg: t, accountId: n }), applyConfigEdit: buildAccountScopedAllowlistConfigEditor({ channelId: e.channelId, normalize: e.normalize, resolveEffectiveEntries: ({ cfg: t, accountId: n, scope: r }) => e.resolveEntries(e.resolveAccount({ cfg: t, accountId: n }), r, { cfg: t, accountId: n }), resolvePaths: e.resolvePaths }) }; } /** Build the common DM/group allowlist adapter used by channels that store both lists in config. */ function buildDmGroupAccountAllowlistAdapter(e) { return buildAccountAllowlistAdapter({ channelId: e.channelId, resolveAccount: e.resolveAccount, normalize: e.normalize, supportsScope: ({ scope: e }) => e === "dm" || e === "group" || e === "all", resolvePaths: resolveDmGroupAllowlistConfigPaths, resolveEntries: (t, n, r) => n === "dm" ? e.resolveDmAllowFrom(t, r) : e.resolveGroupAllowFrom(t), readConfig: (t, n) => ({ dmAllowFrom: readConfiguredAllowlistEntries(e.resolveDmAllowFrom(t, n)), groupAllowFrom: readConfiguredAllowlistEntries(e.resolveGroupAllowFrom(t)), dmPolicy: e.resolveDmPolicy?.(t) ?? void 0, groupPolicy: e.resolveGroupPolicy?.(t) ?? void 0, groupOverrides: e.resolveGroupOverrides?.(t) }) }); } /** Build the common DM-only allowlist adapter for channels with legacy dm.allowFrom fallback paths. */ function buildLegacyDmAccountAllowlistAdapter(e) { return buildAccountAllowlistAdapter({ channelId: e.channelId, resolveAccount: e.resolveAccount, normalize: e.normalize, supportsScope: ({ scope: e }) => e === "dm", resolvePaths: resolveLegacyDmAllowlistConfigPaths, resolveEntries: (t, n, r) => e.resolveDmAllowFrom(t, r), readConfig: (t, n) => ({ dmAllowFrom: readConfiguredAllowlistEntries(e.resolveDmAllowFrom(t, n)), groupPolicy: e.resolveGroupPolicy?.(t) ?? void 0, groupOverrides: e.resolveGroupOverrides?.(t) }) }); } //#endregion export { collectNestedAllowlistOverridesFromRecord as a, createNestedAllowlistOverrideResolver as c, resolveLegacyDmAllowlistConfigPaths as d, collectAllowlistOverridesFromRecord as i, readConfiguredAllowlistEntries as l, buildDmGroupAccountAllowlistAdapter as n, createAccountScopedAllowlistNameResolver as o, buildLegacyDmAccountAllowlistAdapter as r, createFlatAllowlistOverrideResolver as s, buildAccountScopedAllowlistConfigEditor as t, resolveDmGroupAllowlistConfigPaths as u };