openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
107 lines (106 loc) • 4.85 kB
JavaScript
import { m as resolveAgentWorkspaceDir, w as tryResolveAmbientOwnerAgentId } from "./agent-scope-config-DcbEhP0R.js";
import { t as pruneMapToMaxSize } from "./map-size-CNcWiFKu.js";
import { S as resolveRuntimeConfigCacheKey } from "./runtime-snapshot-BaQikjTR.js";
import "./agent-scope-DbtJyKUL.js";
import { d as resolveDiscoverableScopedChannelPluginIds } from "./channel-presence-policy-_eDSlojg.js";
import { f as getActivePluginRegistryVersion, u as getActivePluginRegistry } from "./runtime-BL4wZfTq.js";
import { a as getPluginRuntimeGatewayRequestScope } from "./gateway-request-scope-BCMYlsDI.js";
import { t as applyPluginAutoEnable } from "./plugin-auto-enable-Tv_C18cS.js";
import { n as withActivatedPluginIds } from "./activation-context-CigELUUd.js";
import { n as loadPluginRegistryHandle } from "./loader-DPiOPJjR.js";
import "./channel-plugin-ids-DIyH-LxI.js";
//#region src/infra/outbound/channel-bootstrap.runtime.ts
const MAX_BOOTSTRAP_CHANNEL_OUTCOMES_PER_CONFIG = 64;
let bootstrapRegistryGeneration;
const bootstrapRegistriesByConfig = /* @__PURE__ */ new Map();
function cacheBootstrapOutcome(registries, key, outcome) {
registries.delete(key);
registries.set(key, outcome);
pruneMapToMaxSize(registries, MAX_BOOTSTRAP_CHANNEL_OUTCOMES_PER_CONFIG);
}
function resolveBootstrapRegistryGeneration() {
return String(getActivePluginRegistryVersion());
}
function resolveBootstrapRegistries(cfg) {
const registryGeneration = resolveBootstrapRegistryGeneration();
if (registryGeneration !== bootstrapRegistryGeneration) {
bootstrapRegistryGeneration = registryGeneration;
bootstrapRegistriesByConfig.clear();
}
const configKey = resolveRuntimeConfigCacheKey(cfg);
const existing = bootstrapRegistriesByConfig.get(configKey);
if (existing) {
bootstrapRegistriesByConfig.delete(configKey);
bootstrapRegistriesByConfig.set(configKey, existing);
return existing;
}
pruneMapToMaxSize(bootstrapRegistriesByConfig, 63);
const registries = /* @__PURE__ */ new Map();
bootstrapRegistriesByConfig.set(configKey, registries);
return registries;
}
/** Clears the per-generation channel bootstrap handle cache for isolated tests. */
function resetOutboundChannelBootstrapStateForTests() {
bootstrapRegistryGeneration = void 0;
bootstrapRegistriesByConfig.clear();
}
function channelEntryCanSend(entry) {
return Boolean(entry?.plugin?.outbound?.sendText ?? entry?.plugin?.message?.send?.text);
}
function findChannelEntry(registry, channel) {
return registry?.channels?.find((entry) => entry?.plugin?.id === channel);
}
function resolveSendCapableRegistry(registry, channel) {
return registry && channelEntryCanSend(findChannelEntry(registry, channel)) ? registry : void 0;
}
/** Loads runtime plugins on demand when a selected outbound channel has only a setup shell. */
function bootstrapOutboundChannelPlugin(params) {
const cfg = params.cfg;
if (!cfg) return;
const scopedRegistry = getPluginRuntimeGatewayRequestScope()?.pluginRegistry;
const scopedEntry = findChannelEntry(scopedRegistry ?? null, params.channel);
const activeSendRegistry = resolveSendCapableRegistry(scopedEntry ? scopedRegistry : getActivePluginRegistry(), params.channel);
if (activeSendRegistry) return activeSendRegistry;
const agentId = tryResolveAmbientOwnerAgentId(cfg, params.agentId);
const outcomeKey = `${agentId ?? ""}\0${params.channel}`;
const registries = scopedEntry ? void 0 : resolveBootstrapRegistries(cfg);
if (registries) {
const cachedRegistry = registries.get(outcomeKey);
if (cachedRegistry !== void 0) {
cacheBootstrapOutcome(registries, outcomeKey, cachedRegistry);
return resolveSendCapableRegistry(cachedRegistry, params.channel);
}
}
const autoEnabled = applyPluginAutoEnable({ config: cfg });
const workspaceDir = agentId === void 0 ? void 0 : resolveAgentWorkspaceDir(cfg, agentId);
const pluginIds = resolveDiscoverableScopedChannelPluginIds({
config: autoEnabled.config,
activationSourceConfig: cfg,
channelIds: [params.channel],
workspaceDir,
env: process.env
});
const activatedConfig = withActivatedPluginIds({
config: autoEnabled.config,
pluginIds
}) ?? autoEnabled.config;
const activatedSourceConfig = withActivatedPluginIds({
config: cfg,
pluginIds
}) ?? cfg;
let sendRegistry;
try {
sendRegistry = resolveSendCapableRegistry(loadPluginRegistryHandle({
config: activatedConfig,
activationSourceConfig: activatedSourceConfig,
autoEnabledReasons: autoEnabled.autoEnabledReasons,
onlyPluginIds: pluginIds,
workspaceDir,
runtimeOptions: { allowGatewaySubagentBinding: true }
}), params.channel);
} catch {}
if (registries) cacheBootstrapOutcome(registries, outcomeKey, sendRegistry ?? null);
return sendRegistry;
}
//#endregion
export { resetOutboundChannelBootstrapStateForTests as n, bootstrapOutboundChannelPlugin as t };