UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

320 lines (319 loc) • 11.6 kB
import { o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs"; import { f as normalizeStringEntries } from "./string-normalization-DLmfYZXI.mjs"; import "./session-key-CqAyZeVk.mjs"; import { n as normalizeAccountId, t as DEFAULT_ACCOUNT_ID } from "./account-id-BqKT3GuY.mjs"; import { r as resolveChannelAccountEntry } from "./account-lookup-FhQZVFfo.mjs"; import { t as buildAccountScopedDmSecurityPolicy } from "./helpers-ELZNDAnv.mjs"; import { a as setAccountEnabledInConfigSection, i as deleteAccountFromConfigSection, o as setTopLevelChannelEnabledInConfigSection, r as clearTopLevelChannelConfigFields, s as writeChannelSection } from "./config-helpers-DUK5R_HK.mjs"; //#region src/channels/plugins/config-write-policy-shared.ts function resolveChannelConfig(e, t) { if (!t) return; const n = e.channels?.[t]; return n != null && typeof n === "object" && !Array.isArray(n) ? n : void 0; } function resolveChannelConfigWritesShared(e) { const t = resolveChannelConfig(e.cfg, e.channelId); if (!t || !e.channelId) return true; return (resolveChannelAccountEntry(t.accounts, normalizeAccountId(e.accountId), e.channelId)?.configWrites ?? t.configWrites) !== false; } function authorizeConfigWriteShared(e) { if (e.allowBypass) return { allowed: true }; if (e.target?.kind === "ambiguous") return { allowed: false, reason: "ambiguous-target" }; const t = e.target; const n = [["origin", e.origin], ["target", t && t.kind !== "global" ? t.scope : void 0]]; for (const [t, r] of n) if (r?.channelId && !resolveChannelConfigWritesShared({ cfg: e.cfg, channelId: r.channelId, accountId: r.accountId })) return { allowed: false, reason: t === "origin" ? "origin-disabled" : "target-disabled", blockedScope: { kind: t, scope: r } }; return { allowed: true }; } function resolveExplicitConfigWriteTargetShared(e) { if (!e.channelId) return { kind: "global" }; const t = normalizeAccountId(e.accountId); if (t === "default") return { kind: "channel", scope: { channelId: e.channelId } }; return { kind: "account", scope: { channelId: e.channelId, accountId: t } }; } function resolveConfigWriteTargetFromPathShared(e) { if (e.path[0] !== "channels") return { kind: "global" }; if (e.path.length < 2) return { kind: "ambiguous", scopes: [] }; const t = e.normalizeChannelId(e.path[1] ?? ""); if (!t) return { kind: "ambiguous", scopes: [] }; if (e.path.length === 2) return { kind: "ambiguous", scopes: [{ channelId: t }] }; if (e.path[2] !== "accounts") return { kind: "channel", scope: { channelId: t } }; if (e.path.length < 4) return { kind: "ambiguous", scopes: [{ channelId: t }] }; return resolveExplicitConfigWriteTargetShared({ channelId: t, accountId: e.path[3] }); } function canBypassConfigWritePolicyShared(e) { return e.isInternalMessageChannel(e.channel) && e.gatewayClientScopes?.includes("operator.admin") === true; } function formatConfigWriteDeniedMessageShared(e) { if (e.result.reason === "ambiguous-target") return "⚠️ Channel-initiated /config writes cannot replace channels, channel roots, or accounts collections. Use a more specific path or gateway operator.admin."; const t = e.result.blockedScope?.scope; return `⚠️ Config writes are disabled for ${t?.channelId ?? e.fallbackChannelId ?? "this channel"}. Set ${t?.channelId ? t.accountId ? `channels.${t.channelId}.accounts.${t.accountId}.configWrites=true` : `channels.${t.channelId}.configWrites=true` : e.fallbackChannelId ? `channels.${e.fallbackChannelId}.configWrites=true` : "channels.<channel>.configWrites=true"} to enable.`; } //#endregion //#region src/channels/plugins/config-writes.ts const authorizeConfigWrite = authorizeConfigWriteShared; const resolveExplicitConfigWriteTarget = resolveExplicitConfigWriteTargetShared; const formatConfigWriteDeniedMessage = formatConfigWriteDeniedMessageShared; function resolveConfigWriteTargetFromPath(e) { return resolveConfigWriteTargetFromPathShared({ path: e, normalizeChannelId: normalizeLowercaseStringOrEmpty }); } function canBypassConfigWritePolicy(e) { return canBypassConfigWritePolicyShared({ ...e, isInternalMessageChannel: (e) => normalizeLowercaseStringOrEmpty(e) === "webchat" }); } //#endregion //#region src/plugin-sdk/channel-config-helpers.ts /** * Channel config adapter and DM-access helpers for plugin setup. * * Not part of the config-schema facade trio despite the similar name: those * export Zod schema builders, while this subpath owns config CRUD adapters, * config-write authorization, and DM access/policy resolution for accounts. */ /** Returns whether config writes are enabled for a channel/account target. */ function resolveChannelConfigWrites(e) { return resolveChannelConfigWritesShared(e); } /** Coerce mixed allowlist config values into plain strings without trimming or deduping. */ function mapAllowFromEntries(e) { return (e ?? []).map((e) => String(e)); } /** Normalize user-facing allowlist entries the same way config and doctor flows expect. */ function formatTrimmedAllowFromEntries(e) { return normalizeStringEntries(e); } /** Collapse nullable config scalars into a trimmed optional string. */ function resolveOptionalConfigString(e) { if (e == null) return; return String(e).trim() || void 0; } /** Adapt `{ cfg, accountId }` accessors to callback sites that pass positional args. */ function adaptScopedAccountAccessor(e) { return (t, n) => e({ cfg: t, accountId: n }); } /** Build the shared allowlist/default target adapter surface for account-scoped channel configs. */ function createScopedAccountConfigAccessors(e) { const t = { resolveAllowFrom({ cfg: t, accountId: n }) { return mapAllowFromEntries(e.resolveAllowFrom(e.resolveAccount({ cfg: t, accountId: n }))); }, formatAllowFrom({ allowFrom: t }) { return e.formatAllowFrom(t); } }; if (!e.resolveDefaultTo) return t; return { ...t, resolveDefaultTo({ cfg: t, accountId: n }) { return resolveOptionalConfigString(e.resolveDefaultTo?.(e.resolveAccount({ cfg: t, accountId: n }))); } }; } function createChannelConfigAdapterFromBase(e) { const t = e.resolveAccessorAccount != null ? createScopedAccountConfigAccessors({ resolveAccount: e.resolveAccessorAccount, resolveAllowFrom: e.resolveAllowFrom, formatAllowFrom: e.formatAllowFrom, resolveDefaultTo: e.resolveDefaultTo }) : createScopedAccountConfigAccessors({ resolveAccount: e.resolveAccountForAccessors, resolveAllowFrom: e.resolveAllowFrom, formatAllowFrom: e.formatAllowFrom, resolveDefaultTo: e.resolveDefaultTo }); return { ...e.base, ...t }; } /** Build the common CRUD/config helpers for channels that store multiple named accounts. */ function createScopedChannelConfigBase(e) { const { listAccountIds: t, resolveAccount: n, inspectAccount: r, defaultAccountId: i } = e; return { listAccountIds: (e) => t(e), resolveAccount: (e, t) => n(e, t), inspectAccount: r ? (e, t) => r(e, t) : void 0, defaultAccountId: (e) => i(e), setAccountEnabled({ cfg: t, accountId: n, enabled: r }) { return setAccountEnabledInConfigSection({ cfg: t, sectionKey: e.sectionKey, accountKeyPolicy: e.accountKeyPolicy, accountId: normalizeAccountId(n), enabled: r, allowTopLevel: e.allowTopLevel ?? true }); }, deleteAccount({ cfg: t, accountId: n }) { return deleteAccountFromConfigSection({ cfg: t, sectionKey: e.sectionKey, accountKeyPolicy: e.accountKeyPolicy, accountId: normalizeAccountId(n), clearBaseFields: e.clearBaseFields }); } }; } function createScopedChannelConfigAdapter(e) { return createChannelConfigAdapterFromBase({ ...e, base: createScopedChannelConfigBase({ ...e }), resolveAccountForAccessors({ cfg: t, accountId: n }) { return e.resolveAccount(t, n); } }); } /** Build CRUD/config helpers for top-level single-account channels. */ function createTopLevelChannelConfigBase(e) { return { listAccountIds(t) { return e.listAccountIds?.(t) ?? ["default"]; }, resolveAccount(t) { return e.resolveAccount(t); }, inspectAccount: e.inspectAccount ? (t) => e.inspectAccount?.(t) : void 0, defaultAccountId(t) { return e.defaultAccountId?.(t) ?? "default"; }, setAccountEnabled({ cfg: t, enabled: n }) { return setTopLevelChannelEnabledInConfigSection({ cfg: t, sectionKey: e.sectionKey, enabled: n }); }, deleteAccount({ cfg: t }) { return e.deleteMode === "clear-fields" ? clearTopLevelChannelConfigFields({ cfg: t, sectionKey: e.sectionKey, clearBaseFields: e.clearBaseFields ?? [] }) : writeChannelSection(t, e.sectionKey, void 0); } }; } function createTopLevelChannelConfigAdapter(e) { return createChannelConfigAdapterFromBase({ ...e, base: createTopLevelChannelConfigBase({ ...e }), resolveAccountForAccessors({ cfg: t }) { return e.resolveAccount(t); } }); } /** Build CRUD/config helpers for channels where the default account lives at channel root and named accounts live under `accounts`. */ function createHybridChannelConfigBase(e) { const t = createScopedChannelConfigBase({ ...e, allowTopLevel: false }); return { ...t, setAccountEnabled(n) { return normalizeAccountId(n.accountId) === "default" ? setTopLevelChannelEnabledInConfigSection({ cfg: n.cfg, sectionKey: e.sectionKey, enabled: n.enabled }) : t.setAccountEnabled(n); }, deleteAccount(n) { return normalizeAccountId(n.accountId) === "default" && e.preserveSectionOnDefaultDelete ? clearTopLevelChannelConfigFields({ cfg: n.cfg, sectionKey: e.sectionKey, clearBaseFields: e.clearBaseFields }) : t.deleteAccount(n); } }; } function createHybridChannelConfigAdapter(e) { return createChannelConfigAdapterFromBase({ ...e, base: createHybridChannelConfigBase({ ...e }), resolveAccountForAccessors({ cfg: t, accountId: n }) { return e.resolveAccount(t, n); } }); } /** Convert account-specific DM security fields into the shared runtime policy resolver shape. */ function createScopedDmSecurityResolver(e) { return ({ cfg: t, accountId: n, account: r }) => { const i = e.resolveAccess?.({ cfg: t, accountId: n, account: r }); return buildAccountScopedDmSecurityPolicy({ cfg: t, channelKey: e.channelKey, accountId: n, fallbackAccountId: e.resolveFallbackAccountId?.(r) ?? r.accountId, policy: i?.dmPolicy ?? e.resolvePolicy(r), allowFrom: i?.allowFrom ?? e.resolveAllowFrom(r) ?? [], defaultPolicy: e.defaultPolicy, allowFromPathSuffix: e.allowFromPathSuffix, policyPathSuffix: e.policyPathSuffix, approveChannelId: e.approveChannelId, approveHint: e.approveHint, normalizeEntry: e.normalizeEntry, classifyEntryAuthentication: e.classifyEntryAuthentication, inheritSharedDefaultsFromDefaultAccount: e.inheritSharedDefaultsFromDefaultAccount }); }; } //#endregion export { resolveConfigWriteTargetFromPath as _, createScopedChannelConfigAdapter as a, createTopLevelChannelConfigAdapter as c, mapAllowFromEntries as d, resolveChannelConfigWrites as f, formatConfigWriteDeniedMessage as g, canBypassConfigWritePolicy as h, createScopedAccountConfigAccessors as i, createTopLevelChannelConfigBase as l, authorizeConfigWrite as m, createHybridChannelConfigAdapter as n, createScopedChannelConfigBase as o, resolveOptionalConfigString as p, createHybridChannelConfigBase as r, createScopedDmSecurityResolver as s, adaptScopedAccountAccessor as t, formatTrimmedAllowFromEntries as u, resolveExplicitConfigWriteTarget as v };