UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

499 lines (498 loc) • 20.6 kB
import { c as normalizeOptionalLowercaseString, l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { m as asSafeIntegerInRange } from "./number-coercion-D6u-4YD8.mjs"; import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { i as normalizeArrayBackedTrimmedStringList, r as filterStringEntries } from "./string-normalization-DLmfYZXI.mjs"; import { r as isDeeplyFrozenPlainData } from "./immutable-data-Y7xVjZoy.mjs"; import { i as normalizeChatChannelId } from "./ids-BRSWrLzE.mjs"; import { t as normalizePluginPolicyId } from "./plugin-policy-id-CkilMowp.mjs"; //#region src/plugins/slots.ts /** Applies mutually exclusive plugin slot selection for memory and context-engine plugins. */ const SLOT_BY_KIND = { memory: "memory", "context-engine": "contextEngine" }; const DEFAULT_SLOT_BY_KEY = { memory: "memory-core", contextEngine: "legacy" }; const PLUGIN_SLOT_KEYS = Object.keys(DEFAULT_SLOT_BY_KEY); /** Normalize a kind field to an array for uniform iteration. */ function normalizeKinds(e) { if (!e) return []; return Array.isArray(e) ? e : [e]; } /** Check whether a plugin's kind field includes a specific kind. */ function hasKind(e, t) { return normalizeKinds(e).includes(t); } /** Order-insensitive equality check for two kind values (string or array). */ function kindsEqual(e, t) { const n = normalizeKinds(e).toSorted(); const r = normalizeKinds(t).toSorted(); return n.length === r.length && n.every((e, t) => e === r[t]); } /** Return all slot keys that a plugin's kind field maps to. */ function slotKeysForPluginKind(e) { return normalizeKinds(e).map((e) => SLOT_BY_KIND[e]).filter((e) => e != null); } /** Returns the implicit plugin id that owns a slot before config overrides it. */ function defaultSlotIdForKey(e) { return DEFAULT_SLOT_BY_KEY[e]; } /** Raw `plugins.slots[key]`: `none` turns the slot off, blank leaves it unset. */ function normalizeSlotValue(e) { const t = normalizeOptionalString(e); if (!t) return; if (normalizeOptionalLowercaseString(t) === "none") return null; return t; } function resolveSlotSelection(e, t) { const n = normalizeSlotValue(t); if (n === void 0) return { kind: "default", pluginId: defaultSlotIdForKey(e) }; return n === null ? { kind: "off" } : { kind: "pinned", pluginId: n }; } /** Retained declarations support cleanup even when the manifest cannot be read. */ function resolveRetainedContextEngineIds(e, t, n = [t]) { const r = Object.values(e.plugins?.installs ?? {}).flatMap((e) => Object.entries(e.contextEngineIdsByPlugin ?? {})); const i = r.filter(([e]) => e === t); const a = i.length ? i.flatMap(([, e]) => e) : [t]; return [...new Set(a)].filter((e) => !r.some(([t, r]) => !n.includes(t) && r.includes(e))); } /** Resets every slot currently owned by a plugin to its implicit default. */ function resetPluginSlotsToDefaults(e, t, n) { if (!e) return e; const r = { ...e }; let i = false; for (const a of PLUGIN_SLOT_KEYS) { if (!(a === "contextEngine" ? n ?? [t] : [t]).includes(e[a] ?? "")) continue; delete r[a]; i = true; } return i ? Object.keys(r).length === 0 ? void 0 : r : e; } /** Updates config so the selected plugin owns all slots implied by its kind. */ function applyExclusiveSlotSelection(e) { const t = slotKeysForPluginKind(e.selectedKind); if (t.length === 0) return e.config; const n = e.config.plugins ?? {}; let r = false; const i = { ...n.entries }; const a = { ...n.slots }; for (const n of t) { const t = a[n]; let i = e.selectedId; if (n === "contextEngine" && e.contextEngineIds !== void 0) { const r = e.contextEngineIds; const a = r[0]; if (t && r.includes(t)) continue; if (r.length !== 1 || !a) { e.warn?.(`Plugin "${e.selectedId}" declares multiple context engines. Set plugins.slots.contextEngine explicitly to one of: ${r.join(", ")}. The current selection is unchanged.`); continue; } if (t && t !== defaultSlotIdForKey(n) && (t !== e.selectedId || e.legacyContextEngineOwnerId !== e.selectedId)) { e.warn?.(`Preserved explicit context engine selection "${t}". To use "${e.selectedId}", set plugins.slots.contextEngine to "${r[0]}".`); continue; } i = a; } const o = i === defaultSlotIdForKey(n) ? void 0 : i; if (o === void 0) delete a[n]; else a[n] = o; if (t !== o) r = true; } if (!r) return e.config; const { slots: o, ...s } = n; return { ...e.config, plugins: { ...s, ...Object.keys(a).length > 0 ? { slots: a } : {}, entries: i } }; } //#endregion //#region src/plugins/config-normalization-shared.ts /** Default plugin id normalizer for already-canonical ids. */ const identityNormalizePluginId = (e) => e.trim(); function normalizePluginConfigList(e, t) { return filterStringEntries(e).map((e) => t(e)).filter(Boolean); } function normalizeHookTimeoutMs(e) { return asSafeIntegerInRange(e, { min: 1, max: 6e5 }); } function normalizeHookTimeouts(e) { if (!isRecord(e)) return; const t = {}; for (const [n, r] of Object.entries(e)) { const e = normalizeHookTimeoutMs(r); if (e !== void 0) t[n] = e; } return Object.keys(t).length > 0 ? t : void 0; } function normalizePluginHooks(e) { if (!isRecord(e)) return; const t = {}; for (const n of ["allowPromptInjection", "allowConversationAccess"]) if (typeof e[n] === "boolean") t[n] = e[n]; const n = normalizeHookTimeoutMs(e.timeoutMs); const r = normalizeHookTimeouts(e.timeouts); if (n !== void 0) t.timeoutMs = n; if (r !== void 0) t.timeouts = r; return Object.keys(t).length > 0 ? t : void 0; } function normalizePluginModelConfig(e, t) { if (!isRecord(e)) return; const n = {}; if (typeof e.allowModelOverride === "boolean") n.allowModelOverride = e.allowModelOverride; for (const [r, i] of [["allowedModels", "hasAllowedModelsConfig"], ["allowedCompletionModels", "hasAllowedCompletionModelsConfig"]]) { if (r === "allowedCompletionModels" && t !== "llm") continue; const a = normalizeArrayBackedTrimmedStringList(e[r]); if (a) { n[i] = true; if (a.length > 0) n[r] = a; } } if (t === "llm") { for (const t of ["allowAuthProfileOverride", "allowAgentIdOverride"]) if (typeof e[t] === "boolean") n[t] = e[t]; } return Object.keys(n).length > 0 ? n : void 0; } function normalizePluginEntries(e, t) { if (!isRecord(e)) return {}; const n = {}; for (const [r, i] of Object.entries(e)) { const e = t(r); if (!e) continue; if (!isRecord(i)) { n[e] = {}; continue; } const a = i; n[e] = { ...n[e], enabled: typeof a.enabled === "boolean" ? a.enabled : n[e]?.enabled, hooks: normalizePluginHooks(a.hooks) ?? n[e]?.hooks, subagent: normalizePluginModelConfig(a.subagent, "subagent") ?? n[e]?.subagent, llm: normalizePluginModelConfig(a.llm, "llm") ?? n[e]?.llm, config: "config" in a ? a.config : n[e]?.config }; } return n; } /** Normalizes plugin config while allowing callers to resolve aliases first. */ function normalizePluginsConfigWithResolverCore(e, t = identityNormalizePluginId) { const n = resolveSlotSelection("memory", e?.slots?.memory); const resolvePolicyId = (e) => normalizePluginPolicyId(t(e)); return { enabled: e?.enabled ?? true, allow: normalizePluginConfigList(e?.allow, resolvePolicyId), deny: normalizePluginConfigList(e?.deny, resolvePolicyId), loadPaths: normalizePluginConfigList(e?.load?.paths, identityNormalizePluginId), slots: { memory: n.kind === "off" ? null : n.pluginId, contextEngine: normalizeSlotValue(e?.slots?.contextEngine) }, entries: normalizePluginEntries(e?.entries, resolvePolicyId) }; } /** * Enables an owner for any enabled channel; disables it only when all channels are off. * Unspecified channels leave the plugin's own activation policy in control. */ function resolveChannelConfigEnablement(e, t, n) { const r = e?.channels; if (!r || n?.length === 0) return; const i = (n?.length ? n.map((e) => normalizeChatChannelId(e) ?? e) : [normalizeChatChannelId(t)]).map((e) => { const t = e ? r[e] : void 0; return isRecord(t) ? t.enabled : void 0; }); if (i.includes(true)) return true; return i.every((e) => e === false) ? false : void 0; } //#endregion //#region src/plugins/config-activation-shared.ts function resolvePluginActivationStateShared(e) { const t = e.activationSource ?? { plugins: e.config, rootConfig: e.rootConfig }; const n = normalizePluginPolicyId(e.id); let r; if (t.plugins.entries[n]?.enabled === true) r = "enabled in config"; else if (e.origin === "bundled" && resolveChannelConfigEnablement(t.rootConfig, e.id, e.channelIds) === true) r = "channel enabled in config"; else if (t.plugins.slots.memory === e.id) r = "selected memory slot"; else if (t.plugins.contextEngineOwnerId !== void 0 ? t.plugins.contextEngineOwnerId === n : t.plugins.slots.contextEngine === e.id) r = "selected context engine slot"; else if (e.origin !== "bundled" && t.plugins.allow.includes(n)) r = "selected in allowlist"; const decision = (e, t = {}) => ({ enabled: e !== "disabled", activated: e !== "disabled", explicitlyEnabled: r !== void 0, source: e, reason: void 0, ...t }); if (!e.config.enabled) return decision("disabled", { reason: "plugins disabled" }); const i = normalizePluginPolicyId(e.id); if (e.config.deny.includes(i)) return decision("disabled", { reason: "blocked by denylist" }); const a = e.config.entries[i]; if (a?.enabled === false) return decision("disabled", { reason: "disabled in config" }); if (resolveChannelConfigEnablement(t.rootConfig ?? e.rootConfig, e.id, e.channelIds) === false) return decision("disabled", { reason: "channel disabled in config" }); const o = e.config.allow.includes(i); if (e.origin === "workspace" && !o && a?.enabled !== true && r !== "selected context engine slot") return decision("disabled", { reason: "workspace plugin (disabled by default)" }); if (e.config.slots.memory === e.id) return decision("explicit", { explicitlyEnabled: true, reason: "selected memory slot" }); if (e.config.contextEngineOwnerId !== void 0 ? e.config.contextEngineOwnerId === i : e.config.slots.contextEngine === e.id) return decision("explicit", { explicitlyEnabled: true, reason: "selected context engine slot" }); if (e.allowBundledChannelExplicitBypassesAllowlist === true && r === "channel enabled in config") return decision("explicit", { explicitlyEnabled: true, reason: r }); if (e.config.allow.length > 0 && !o) return decision("disabled", { reason: "not in allowlist" }); if (r) return decision("explicit", { explicitlyEnabled: true, reason: r }); const s = e.autoEnabledReason || (a?.enabled === true ? "enabled by effective config" : void 0) || (e.origin === "bundled" && resolveChannelConfigEnablement(e.rootConfig, e.id, e.channelIds) === true ? "channel configured" : void 0); if (e.origin === "bundled" && e.enabledByDefault === true && (!s || t.plugins.allow.length === 0 || t.plugins.allow.includes(i))) return decision("default", { explicitlyEnabled: false, reason: "bundled default enablement" }); if (s) return decision("auto", { explicitlyEnabled: false, reason: s }); if (e.origin === "bundled") return decision("disabled", { explicitlyEnabled: false, reason: "bundled (disabled by default)" }); return decision("default"); } function resolveMemorySlotDecisionShared(e) { if (!(Array.isArray(e.kind) ? e.kind.includes("memory") : e.kind === "memory")) return { enabled: true }; const t = Array.isArray(e.kind) && e.kind.length > 1; if (e.slot === null) return t ? { enabled: true } : { enabled: false, reason: "memory slot disabled" }; if (typeof e.slot === "string") { if (e.slot === e.id) return { enabled: true, selected: true }; return t ? { enabled: true } : { enabled: false, reason: `memory slot set to "${e.slot}"` }; } if (e.selectedId && e.selectedId !== e.id) return t ? { enabled: true } : { enabled: false, reason: `memory slot already filled by "${e.selectedId}"` }; return { enabled: true, selected: true }; } //#endregion //#region src/plugins/config-state.ts const BUILT_IN_PLUGIN_ALIAS_LOOKUP = /* @__PURE__ */ new Map([ ["google-gemini-cli", "google"], ["minimax-portal", "minimax"], ["minimax-portal-auth", "minimax"] ]); const RETIRED_PLUGIN_IDS = /* @__PURE__ */ new Set([ "google-antigravity-auth", "google-gemini-cli-auth", "skill-workshop", "webhooks" ]); /** Normalizes user/config plugin ids into the canonical lowercase key form. */ function normalizePluginId(e) { const t = normalizePluginPolicyId(e); return BUILT_IN_PLUGIN_ALIAS_LOOKUP.get(t) ?? t; } function isRetiredPluginId(e) { return RETIRED_PLUGIN_IDS.has(normalizePluginId(e)); } /** Identifies the credential-free marker that records an explicit plugin disable decision. */ function isExplicitPluginDisableMarker(e) { return isRecord(e) && e.enabled === false && Object.keys(e).length === 1; } /** Builds caller-owned policy without exposing the host's prepared objects. */ const createNormalizedPluginsConfig = (e) => normalizePluginsConfigWithResolverCore(e, normalizePluginId); const normalizePluginsConfig = (e) => { if (preparedRuntimePluginsConfig && preparedRuntimePluginsConfig.source === e) return preparedRuntimePluginsConfig.value; const t = e ?? EMPTY_PLUGINS_CONFIG; if (!isDeeplyFrozenPlainData(t)) return createNormalizedPluginsConfig(t); let n = frozenPluginsConfigs.get(t); if (!n) { n = createSharedNormalizedPluginsConfig(t); frozenPluginsConfigs.set(t, n); } return n; }; let preparedRuntimePluginsConfig; const EMPTY_PLUGINS_CONFIG = Object.freeze({}); const frozenPluginsConfigs = /* @__PURE__ */ new WeakMap(); function createSharedNormalizedPluginsConfig(e) { const t = createNormalizedPluginsConfig(e); for (const e of Object.values(t.entries)) { Object.freeze(e.hooks?.timeouts); Object.freeze(e.hooks); Object.freeze(e.subagent?.allowedModels); Object.freeze(e.subagent); Object.freeze(e.llm?.allowedModels); Object.freeze(e.llm?.allowedCompletionModels); Object.freeze(e.llm); Object.freeze(e); } Object.freeze(t.entries); for (const e of [ t.allow, t.deny, t.loadPaths, t.slots ]) Object.freeze(e); return Object.freeze(t); } /** Runtime config publication owns replacement, including same-object refreshes and teardown. */ function prepareRuntimePluginsConfig(e) { preparedRuntimePluginsConfig = e ? { source: e.plugins, value: createSharedNormalizedPluginsConfig(e.plugins) } : void 0; } /** Resolves the enabled plugin selected to own the context-engine slot. */ function resolveSelectedContextEnginePluginId(e, t) { const n = normalizePluginsConfig(e?.plugins); return resolveSelectedContextEnginePluginIdFromConfig(n, n.slots.contextEngine, t); } function isContextEngineOwnerEligible(e, t, n) { if (!e.enabled || !t || e.deny.includes(t) || e.entries[t]?.enabled === false) return false; return t === n || (e.entries[t]?.enabled === true || e.allow.includes(t)) && (e.allow.length === 0 || e.allow.includes(t)); } /** Declared owners participate in selection and collision checks only when policy permits them. */ function resolveEligibleContextEngineDeclaredOwners(e, t, n, r = normalizePluginId) { const i = n.filter((e) => t && e.contextEngineIds?.includes(t)); const a = t ? normalizePluginPolicyId(r(t)) : void 0; return { hasDeclarations: i.length > 0, pluginIds: a ? [...new Set(i.map((e) => e.id))].filter((t) => isContextEngineOwnerEligible(e, normalizePluginPolicyId(r(t)), a)) : [] }; } /** All independently eligible claimants must be known before any runtime module imports. */ function resolveEligibleContextEngineOwnerIds(e, t, n, r = normalizePluginId) { if (!e.enabled || !t || t === defaultSlotIdForKey("contextEngine")) return []; const i = resolveEligibleContextEngineDeclaredOwners(e, t, n, r); const a = normalizePluginPolicyId(r(t)); const o = n.filter((e) => normalizePluginPolicyId(r(e.id)) === a && e.contextEngineIds === void 0).map((e) => e.id); const s = !i.hasDeclarations || e.entries[a]?.enabled === true || e.allow.includes(a); return [.../* @__PURE__ */ new Set([...i.pluginIds, ...o.length > 0 && s && isContextEngineOwnerEligible(e, a, a) ? o : []])]; } function resolveSelectedContextEnginePluginIdFromConfig(e, t, n, r = normalizePluginId) { if (!e.enabled || !t || t === defaultSlotIdForKey("contextEngine")) return; const i = resolveEligibleContextEngineOwnerIds(e, t, n, r); return i.length === 1 ? i[0] : void 0; } /** Carries prepared ownership without changing the registered engine selector. */ function withContextEngineOwner(e, t, n = normalizePluginId) { const r = resolveSelectedContextEnginePluginIdFromConfig(e, e.slots.contextEngine, t, n); return { ...e, contextEngineOwnerId: r ? normalizePluginPolicyId(n(r)) : null }; } /** Canonicalizes one plugin entry and its policy-list ids before a targeted mutation. */ function normalizePluginTargetConfig(e, t) { const n = normalizePluginId(t); const r = normalizePluginsConfig(e.plugins); const i = e.plugins?.entries ?? {}; const a = Object.keys(i).some((e) => normalizePluginId(e) === n); const o = Object.fromEntries(Object.entries(i).filter(([e]) => normalizePluginId(e) !== n)); if (a) { const { config: e, ...t } = r.entries[n] ?? {}; o[n] = { ...Object.fromEntries(Object.entries(t).filter(([, e]) => e !== void 0)), ...isRecord(e) ? { config: e } : {} }; } return { ...e, plugins: { ...e.plugins, ...Array.isArray(e.plugins?.allow) ? { allow: r.allow } : {}, ...Array.isArray(e.plugins?.deny) ? { deny: r.deny } : {}, entries: o } }; } function createPluginActivationSource(e) { return { plugins: e.plugins ?? normalizePluginsConfig(e.config?.plugins), rootConfig: e.config }; } const hasExplicitMemorySlot = (e) => Boolean(e?.slots && Object.hasOwn(e.slots, "memory")); const hasExplicitMemoryEntry = (e) => Boolean(e?.entries && Object.hasOwn(e.entries, defaultSlotIdForKey("memory"))); function hasExplicitPluginConfig(e) { if (!e) return false; return typeof e.enabled === "boolean" || Array.isArray(e.allow) && e.allow.length > 0 || Array.isArray(e.deny) && e.deny.length > 0 || Array.isArray(e.load?.paths) && e.load.paths.length > 0 || Boolean(e.slots && Object.keys(e.slots).length > 0) || Boolean(e.entries && Object.keys(e.entries).length > 0); } function applyTestPluginDefaults(e, t = process.env) { if (!t.VITEST) return e; const n = e.plugins; const r = hasExplicitPluginConfig(n); if (r && (hasExplicitMemorySlot(n) || hasExplicitMemoryEntry(n))) return e; return { ...e, plugins: { ...n, ...!r ? { enabled: false } : {}, slots: { ...n?.slots, memory: "none" } } }; } function isTestDefaultMemorySlotDisabled(e, t = process.env) { return Boolean(t.VITEST) && !hasExplicitMemorySlot(e.plugins) && !hasExplicitMemoryEntry(e.plugins); } function resolveEffectivePluginActivationState(e) { return resolvePluginActivationStateShared({ ...e, config: e.config ?? normalizePluginsConfig(e.rootConfig?.plugins), allowBundledChannelExplicitBypassesAllowlist: true }); } function toEnableStateResult(e) { return e.enabled ? { enabled: true } : { enabled: false, reason: e.reason }; } const resolveEnableState = (e, t, n, r) => toEnableStateResult(resolveEffectivePluginActivationState({ id: e, origin: t, config: n, enabledByDefault: r })); const resolveEffectiveEnableState = (e) => toEnableStateResult(resolveEffectivePluginActivationState(e)); function resolveMemorySlotDecision(e) { return resolveMemorySlotDecisionShared(e); } //#endregion export { resetPluginSlotsToDefaults as A, normalizePluginsConfigWithResolverCore as C, hasKind as D, defaultSlotIdForKey as E, kindsEqual as O, normalizePluginConfigList as S, applyExclusiveSlotSelection as T, resolveMemorySlotDecision as _, isExplicitPluginDisableMarker as a, withContextEngineOwner as b, normalizePluginId as c, prepareRuntimePluginsConfig as d, resolveEffectiveEnableState as f, resolveEnableState as g, resolveEligibleContextEngineOwnerIds as h, hasExplicitPluginConfig as i, resolveRetainedContextEngineIds as j, normalizeSlotValue as k, normalizePluginTargetConfig as l, resolveEligibleContextEngineDeclaredOwners as m, createNormalizedPluginsConfig as n, isRetiredPluginId as o, resolveEffectivePluginActivationState as p, createPluginActivationSource as r, isTestDefaultMemorySlotDisabled as s, applyTestPluginDefaults as t, normalizePluginsConfig as u, resolveSelectedContextEnginePluginId as v, resolveChannelConfigEnablement as w, resolvePluginActivationStateShared as x, resolveSelectedContextEnginePluginIdFromConfig as y };