openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
347 lines (346 loc) • 15.4 kB
JavaScript
import { c as normalizeOptionalLowercaseString } from "./string-coerce-CCIiB4H7.mjs";
import { y as sortUniqueStrings } from "./string-normalization-DLmfYZXI.mjs";
import { t as isPluginEnabledByDefaultForPlatform } from "./default-enablement-DLXVIwwO.mjs";
import { p as resolveEffectivePluginActivationState, r as createPluginActivationSource, u as normalizePluginsConfig } from "./config-state-B1JlHgba.mjs";
import { t as hasMeaningfulChannelConfig } from "./channel-config-activation-BzEa8FnN.mjs";
import { n as resolveConfigWidePluginManifestRegistry } from "./io.plugin-metadata-CBwzHw3m.mjs";
import { n as isChannelConfigMetadataKey } from "./config-metadata-BVUrt758.mjs";
import { i as listPotentialConfiguredChannelPresenceSignalsAsync, r as listPotentialConfiguredChannelPresenceSignals, t as listExplicitlyDisabledChannelIdsForConfig } from "./config-presence-G9kglnWa.mjs";
import { i as hasChannelPackageState } from "./package-state-probes-BxreN8Qk.mjs";
import { a as resolveManifestOwnerBasePolicyBlock, n as isActivatedManifestOwner, r as isBundledManifestOwner, t as hasExplicitManifestOwnerTrust } from "./manifest-owner-policy-B8Brw4Zo.mjs";
import { n as loadPluginManifestRegistryForPluginRegistry } from "./plugin-registry-contributions-iAw3gSDD.mjs";
import { t as resolveManifestActivationPluginIds } from "./activation-planner-lvis97qI.mjs";
//#region src/plugins/channel-presence-policy.ts
const AMBIENT_ENV_SOURCES = /* @__PURE__ */ new Set(["env", "manifest-env"]);
const ANNOUNCE_SUPPRESSING_BLOCKED_REASONS = /* @__PURE__ */ new Set([
"plugins-disabled",
"blocked-by-denylist",
"plugin-disabled"
]);
function normalizeChannelIds(e) {
return sortUniqueStrings([...e].flatMap((e) => {
const t = normalizeOptionalLowercaseString(e);
return t ? [t] : [];
}));
}
/** True when config contains meaningful enabled channel settings. */
function hasExplicitChannelConfig(e) {
const t = e.config.channels;
if (!t || typeof t !== "object" || Array.isArray(t)) return false;
const n = t[e.channelId];
if (!n || typeof n !== "object" || Array.isArray(n)) return false;
const r = n.enabled;
if (r === false) return false;
return r === true || hasMeaningfulChannelConfig(n, e.channelId);
}
/** Lists explicitly configured channel ids, excluding global channel config keys. */
function listExplicitConfiguredChannelIdsForConfig(e) {
const t = e.channels;
if (!t || typeof t !== "object" || Array.isArray(t)) return [];
return Object.keys(t).flatMap((t) => {
const n = t.trim();
return n && !isChannelConfigMetadataKey(n) && hasExplicitChannelConfig({
config: e,
channelId: t
}) ? [n] : [];
}).toSorted((e, t) => e.localeCompare(t));
}
function recordDeclaresChannel(e, t) {
const n = normalizeOptionalLowercaseString(t) ?? "";
if (!n) return false;
return e.channels.some((e) => (normalizeOptionalLowercaseString(e) ?? "") === n);
}
function normalizeActivationBlockedReason(e) {
switch (e) {
case "plugins disabled": return "plugins-disabled";
case "blocked by denylist": return "blocked-by-denylist";
case "disabled in config":
case "channel disabled in config": return "plugin-disabled";
case "not in allowlist": return "not-in-allowlist";
case "workspace plugin (disabled by default)": return "workspace-disabled-by-default";
case "bundled (disabled by default)": return "bundled-disabled-by-default";
default: return "not-activated";
}
}
function resolveChannelPluginOwnerPolicy(e) {
const t = e.channelId !== void 0 && isBundledManifestOwner(e.plugin) && hasExplicitChannelConfig({
config: e.rootConfig,
channelId: e.channelId
});
let n = resolveManifestOwnerBasePolicyBlock({
plugin: e.plugin,
normalizedConfig: e.normalizedConfig,
allowRestrictiveAllowlistBypass: t
});
if (!n && !isBundledManifestOwner(e.plugin)) n = (e.plugin.origin === "global" || e.plugin.origin === "config" ? hasExplicitManifestOwnerTrust(e) : isActivatedManifestOwner(e)) ? null : "untrusted-plugin";
return {
explicitBundledChannelConfig: t,
blockedReason: n
};
}
function isChannelPluginEligibleForScopedOwnership(e) {
return resolveChannelPluginOwnerPolicy(e).blockedReason === null;
}
function addPolicySignal(e, t, n) {
const r = normalizeOptionalLowercaseString(t);
if (!r) return;
let i = e.get(r);
if (!i) {
i = /* @__PURE__ */ new Set();
e.set(r, i);
}
i.add(n);
}
function loadInstalledChannelManifestRecords(e) {
if (e.manifestRecords) return e.manifestRecords;
const t = e.env ?? process.env;
if (!e.workspaceDir) return resolveConfigWidePluginManifestRegistry({
config: e.config,
env: t
}).plugins;
return loadPluginManifestRegistryForPluginRegistry({
config: e.config,
workspaceDir: e.workspaceDir,
env: t,
includeDisabled: true
}).plugins;
}
function channelPresenceOptions(e) {
return {
persistedAuthChannelIds: e.manifestRecords ? new Set(normalizeChannelIds(e.manifestRecords.flatMap((e) => e.channels))) : void 0,
includePersistedAuthState: e.includePersistedAuthState,
ambientEnvTriggers: e.ambientEnvTriggers,
discovery: e.discovery
};
}
function resolveConfiguredChannelPresencePolicy(e) {
return resolveChannelPresencePolicyFromSignals(e, listPotentialConfiguredChannelPresenceSignals(e.config, e.env ?? process.env, channelPresenceOptions(e)));
}
async function resolveConfiguredChannelPresencePolicyAsync(e) {
const t = loadInstalledChannelManifestRecords(e);
const n = e.activationSourceConfig ?? e.config;
const r = normalizePluginsConfig(n.plugins);
const i = new Set(listExplicitlyDisabledChannelIdsForConfig(e.config));
const a = new Set(t.flatMap((e) => normalizeChannelIds(e.channels).filter((t) => !i.has(t) && isChannelPluginEligibleForScopedOwnership({
plugin: e,
channelId: t,
normalizedConfig: r,
rootConfig: n
}))));
return resolveChannelPresencePolicyFromSignals({
...e,
manifestRecords: t
}, await listPotentialConfiguredChannelPresenceSignalsAsync(e.config, e.env ?? process.env, {
...channelPresenceOptions(e),
persistedAuthChannelIds: a
}));
}
function resolveChannelPresencePolicyFromSignals(e, t) {
const n = e.env ?? process.env;
const r = loadInstalledChannelManifestRecords(e);
const i = new Set(listExplicitlyDisabledChannelIdsForConfig(e.config));
const a = /* @__PURE__ */ new Map();
const o = createPluginActivationSource({ config: e.activationSourceConfig ?? e.config });
let s;
let c;
if (e.ambientEnvTriggers !== "suppress") {
const i = new Set(t.filter((e) => e.source === "env").map((e) => normalizeOptionalLowercaseString(e.channelId)).filter((e) => Boolean(e)));
c = /* @__PURE__ */ new Set();
s = /* @__PURE__ */ new Set();
const a = o.rootConfig ?? e.config;
const l = o.plugins;
for (const t of r) {
const r = t.packageChannel;
const o = normalizeOptionalLowercaseString(r?.id);
const u = r?.configuredState;
const d = Boolean(u?.specifier && u.exportName);
if (!r || !o || !recordDeclaresChannel(t, o) || !(u?.env?.allOf?.length || u?.env?.anyOf?.length) && !d || !isChannelPluginEligibleForScopedOwnership({
plugin: t,
normalizedConfig: l,
rootConfig: a
})) continue;
s.add(o);
if ((!d || i.has(o)) && hasChannelPackageState({
entry: {
pluginId: t.id,
origin: t.origin,
rootDir: t.rootDir,
channel: r
},
metadataKey: "configuredState",
cfg: e.config,
env: n
})) c.add(o);
}
}
for (const t of listExplicitConfiguredChannelIdsForConfig(e.activationSourceConfig ?? e.config)) addPolicySignal(a, t, "explicit-config");
for (const e of t) {
const t = normalizeOptionalLowercaseString(e.channelId);
if (e.source === "config" || e.source === "env" && t && s?.has(t) && !c?.has(t)) continue;
addPolicySignal(a, e.channelId, e.source);
}
for (const e of c ?? []) addPolicySignal(a, e, "manifest-env");
for (const e of i) a.delete(e);
if (e.ambientEnvTriggers === "suppress") {
for (const [e, t] of a) if (t.size > 0 && [...t].every((e) => AMBIENT_ENV_SOURCES.has(e))) a.delete(e);
}
const l = o.plugins;
const u = [];
for (const t of sortUniqueStrings(a.keys())) {
const n = r.filter((e) => recordDeclaresChannel(e, t));
const i = [];
const s = new Set(n.length === 0 ? ["no-channel-owner"] : []);
for (const r of n) {
const n = resolveChannelPluginOwnerPolicy({
plugin: r,
channelId: t,
normalizedConfig: l,
rootConfig: o.rootConfig ?? e.config
});
let a = n.blockedReason;
if (!a && isBundledManifestOwner(r) && !n.explicitBundledChannelConfig) {
const t = resolveEffectivePluginActivationState({
id: r.id,
origin: r.origin,
channelIds: r.channels,
config: l,
rootConfig: e.config,
enabledByDefault: isPluginEnabledByDefaultForPlatform(r),
activationSource: o
});
a = t.enabled ? null : normalizeActivationBlockedReason(t.reason);
}
if (a) s.add(a);
else i.push(r.id);
}
u.push({
channelId: t,
sources: [...a.get(t) ?? []].toSorted((e, t) => e.localeCompare(t)),
effective: i.length > 0,
pluginIds: sortUniqueStrings(i),
blockedReasons: [...s].toSorted((e, t) => e.localeCompare(t))
});
}
return u;
}
function listGatewayActivatedChannelIds(e) {
return resolveConfiguredChannelPresencePolicy({
...e,
includePersistedAuthState: false
}).filter((e) => e.effective || e.blockedReasons.includes("bundled-disabled-by-default")).map((e) => e.channelId);
}
function listChannelIdsForOwnershipMigration(e) {
return resolveOwnershipMigrationChannelIds(e, listPotentialConfiguredChannelPresenceSignals(e.config, e.env ?? process.env, {
includePersistedAuthState: true,
ambientEnvTriggers: e.ambientEnvTriggers,
discovery: e.discovery
}));
}
async function listChannelIdsForOwnershipMigrationAsync(e) {
return resolveOwnershipMigrationChannelIds(e, await listPotentialConfiguredChannelPresenceSignalsAsync(e.config, e.env ?? process.env, {
includePersistedAuthState: true,
ambientEnvTriggers: e.ambientEnvTriggers,
discovery: e.discovery
}));
}
function resolveOwnershipMigrationChannelIds(e, t) {
const n = loadInstalledChannelManifestRecords(e);
const r = e.activationSourceConfig ?? e.config;
const i = normalizePluginsConfig(r.plugins);
const a = t.filter((e) => e.source === "persisted-auth").map((e) => e.channelId).filter((e) => n.some((t) => recordDeclaresChannel(t, e) && isChannelPluginEligibleForScopedOwnership({
plugin: t,
normalizedConfig: i,
rootConfig: r
})));
return normalizeChannelIds([...resolveChannelPresencePolicyFromSignals(e, t).filter((e) => e.effective || e.blockedReasons.includes("bundled-disabled-by-default")).map((e) => e.channelId), ...a]);
}
/** Lists channels that suppression removes because their only presence is ambient env. */
function listAmbientOnlyConfiguredChannelIds(e) {
return resolveConfiguredChannelPresencePolicy({
...e,
ambientEnvTriggers: "allow"
}).filter((e) => e.sources.length > 0 && e.sources.every((e) => AMBIENT_ENV_SOURCES.has(e))).map((e) => e.channelId);
}
/** Lists effective channel ids available to read-only scoped discovery. */
function listConfiguredChannelIdsForReadOnlyScope(e) {
return resolveConfiguredChannelPresencePolicy(e).filter((e) => e.effective).map((e) => e.channelId);
}
/** True when read-only scoped discovery has any effective configured channel. */
function hasConfiguredChannelsForReadOnlyScope(e) {
return resolveConfiguredChannelPresencePolicy(e).some((e) => e.effective);
}
async function listConfiguredChannelIdsForReadOnlyScopeAsync(e) {
return (await resolveConfiguredChannelPresencePolicyAsync(e)).filter((e) => e.effective).map((e) => e.channelId);
}
async function hasConfiguredChannelsForReadOnlyScopeAsync(e) {
return (await resolveConfiguredChannelPresencePolicyAsync(e)).some((e) => e.effective);
}
/** Lists channel ids that should be announced as configured for operators. */
function listConfiguredAnnounceChannelIdsForConfig(e) {
const t = new Set(listExplicitlyDisabledChannelIdsForConfig(e.config));
const n = e.activationSourceConfig ?? e.config;
const r = normalizePluginsConfig(n.plugins);
const i = resolveConfiguredChannelPresencePolicy({
config: e.config,
activationSourceConfig: n,
env: e.env,
includePersistedAuthState: false
});
const a = new Set(i.filter((e) => !e.effective && e.blockedReasons.some((e) => ANNOUNCE_SUPPRESSING_BLOCKED_REASONS.has(e))).map((e) => e.channelId));
return normalizeChannelIds([...listExplicitConfiguredChannelIdsForConfig(e.config).filter((e) => r.enabled && !r.deny.includes(e) && r.entries[e]?.enabled !== false && (r.allow.length === 0 || r.allow.includes(e))), ...i.filter((e) => e.effective).map((e) => e.channelId)]).filter((e) => !t.has(e) && !a.has(e));
}
/** Resolves plugin ids discoverable for scoped channel activation. */
function resolveDiscoverableScopedChannelPluginIds(e) {
const t = normalizeChannelIds(e.channelIds);
if (t.length === 0) return [];
const n = loadInstalledChannelManifestRecords(e);
const r = e.activationSourceConfig ?? e.config;
const i = normalizePluginsConfig(r.plugins);
const a = sortUniqueStrings(t.flatMap((t) => {
return resolveManifestActivationPluginIds({
trigger: {
kind: "channel",
channel: t
},
config: e.config,
workspaceDir: e.workspaceDir,
env: e.env,
manifestRecords: n,
allowRestrictiveAllowlistBypass: hasExplicitChannelConfig({
config: r,
channelId: t
})
});
}));
if (a.length === 0) return [];
const o = new Set(a);
return n.filter((e) => {
if (!o.has(e.id)) return false;
return isChannelPluginEligibleForScopedOwnership({
plugin: e,
normalizedConfig: i,
rootConfig: r,
channelId: t.find((t) => recordDeclaresChannel(e, t))
});
}).map((e) => e.id).toSorted((e, t) => e.localeCompare(t));
}
/** Resolves plugin ids that own currently configured channels. */
function resolveConfiguredChannelPluginIds(e) {
if (!normalizePluginsConfig((e.activationSourceConfig ?? e.config).plugins).enabled) return [];
const t = normalizeChannelIds([...listConfiguredChannelIdsForReadOnlyScope(e), ...listExplicitConfiguredChannelIdsForConfig(e.activationSourceConfig ?? e.config)]);
if (t.length === 0) return [];
return resolveDiscoverableScopedChannelPluginIds({
...e,
channelIds: t
});
}
async function resolveConfiguredChannelPluginIdsAsync(e) {
if (!normalizePluginsConfig((e.activationSourceConfig ?? e.config).plugins).enabled) return [];
const t = normalizeChannelIds([...await listConfiguredChannelIdsForReadOnlyScopeAsync(e), ...listExplicitConfiguredChannelIdsForConfig(e.activationSourceConfig ?? e.config)]);
return t.length === 0 ? [] : resolveDiscoverableScopedChannelPluginIds({
...e,
channelIds: t
});
}
//#endregion
export { listChannelIdsForOwnershipMigration as a, listConfiguredChannelIdsForReadOnlyScope as c, listGatewayActivatedChannelIds as d, resolveConfiguredChannelPluginIds as f, resolveDiscoverableScopedChannelPluginIds as h, listAmbientOnlyConfiguredChannelIds as i, listConfiguredChannelIdsForReadOnlyScopeAsync as l, resolveConfiguredChannelPresencePolicy as m, hasConfiguredChannelsForReadOnlyScopeAsync as n, listChannelIdsForOwnershipMigrationAsync as o, resolveConfiguredChannelPluginIdsAsync as p, hasExplicitChannelConfig as r, listConfiguredAnnounceChannelIdsForConfig as s, hasConfiguredChannelsForReadOnlyScope as t, listExplicitConfiguredChannelIdsForConfig as u };