openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
102 lines (101 loc) • 4.85 kB
JavaScript
import { o as getActivePluginRegistry, t as getActivePluginChannelRegistry } from "./runtime-B2ROiq5l.js";
import { n as getLoadedChannelPlugin, t as getChannelPlugin } from "./registry-MkxNn1Ue.js";
import "./plugins-t2ejWcVy.js";
import { i as normalizeMessageChannel, t as isDeliverableMessageChannel } from "./message-channel-normalize-BLLf0ubu.js";
import "./message-channel-BiOeMu0l.js";
import { t as bootstrapOutboundChannelPlugin } from "./channel-bootstrap.runtime-DjrqEQj-.js";
//#region src/infra/outbound/channel-resolution.ts
/** Normalizes a raw channel id and rejects non-deliverable/internal channels. */
function normalizeDeliverableOutboundChannel(raw) {
const normalized = normalizeMessageChannel(raw);
if (!normalized || !isDeliverableMessageChannel(normalized)) return;
return normalized;
}
function maybeBootstrapChannelPlugin(params) {
bootstrapOutboundChannelPlugin(params);
}
function resolveDirectFromRegistry(registry, channel) {
if (!registry) return;
for (const entry of registry.channels) {
const plugin = entry?.plugin;
if (plugin?.id === channel) return plugin;
}
}
function messageAdapterCanSendText(message) {
return typeof message?.send?.text === "function";
}
function resolveSendCapableMessageAdapter(plugin) {
const message = plugin?.message;
return messageAdapterCanSendText(message) ? message : void 0;
}
function channelPluginHasRuntimeOutboundSurface(plugin) {
return Boolean(plugin?.outbound ?? resolveSendCapableMessageAdapter(plugin));
}
function resolveRuntimeOutboundPlugin(plugin) {
return channelPluginHasRuntimeOutboundSurface(plugin) ? plugin : void 0;
}
function resolveRuntimeOutboundPluginCandidate(params) {
if (channelPluginHasRuntimeOutboundSurface(params.loaded)) return params.loaded;
if (params.runtime) return params.runtime;
if (channelPluginHasRuntimeOutboundSurface(params.bundled)) return params.bundled;
if (params.allowSetupShell) return params.loaded ?? params.setupFallback ?? params.bundled;
}
function resolveValueFromRuntimeRegistries(channel, resolveValue) {
const channelRegistry = getActivePluginChannelRegistry();
const channelPlugin = resolveDirectFromRegistry(channelRegistry, channel);
if (channelPlugin) {
const value = resolveValue(channelPlugin);
if (value !== void 0) return value;
}
const activeRegistry = getActivePluginRegistry();
if (activeRegistry && activeRegistry !== channelRegistry) {
const activePlugin = resolveDirectFromRegistry(activeRegistry, channel);
if (activePlugin) return resolveValue(activePlugin);
}
}
function resolveDirectFromRuntimeRegistries(channel) {
return resolveValueFromRuntimeRegistries(channel, (plugin) => plugin);
}
function resolveRuntimeOutboundPluginFromRuntimeRegistries(channel) {
return resolveValueFromRuntimeRegistries(channel, resolveRuntimeOutboundPlugin);
}
/** Resolves a deliverable outbound channel plugin, optionally bootstrapping it. */
function resolveOutboundChannelPlugin(params) {
const normalized = normalizeDeliverableOutboundChannel(params.channel);
if (!normalized) return;
const resolveLoaded = () => getLoadedChannelPlugin(normalized);
const resolve = () => getChannelPlugin(normalized);
const candidate = resolveRuntimeOutboundPluginCandidate({
loaded: resolveLoaded(),
runtime: resolveRuntimeOutboundPluginFromRuntimeRegistries(normalized),
setupFallback: resolveDirectFromRuntimeRegistries(normalized),
bundled: resolve(),
allowSetupShell: params.allowBootstrap !== true
});
if (candidate) return candidate;
if (params.allowBootstrap !== true) return;
maybeBootstrapChannelPlugin({
channel: normalized,
cfg: params.cfg
});
return resolveRuntimeOutboundPluginCandidate({
loaded: resolveLoaded(),
runtime: resolveRuntimeOutboundPluginFromRuntimeRegistries(normalized),
setupFallback: resolveDirectFromRuntimeRegistries(normalized),
bundled: resolve()
});
}
/** Resolves the message adapter for a deliverable outbound channel. */
function resolveOutboundChannelMessageAdapter(params) {
const normalized = normalizeDeliverableOutboundChannel(params.channel);
if (!normalized) return;
const current = resolveSendCapableMessageAdapter(getLoadedChannelPlugin(normalized)) ?? resolveValueFromRuntimeRegistries(normalized, resolveSendCapableMessageAdapter) ?? resolveSendCapableMessageAdapter(getChannelPlugin(normalized));
if (current || params.allowBootstrap !== true) return current;
maybeBootstrapChannelPlugin({
channel: normalized,
cfg: params.cfg
});
return resolveSendCapableMessageAdapter(getLoadedChannelPlugin(normalized)) ?? resolveValueFromRuntimeRegistries(normalized, resolveSendCapableMessageAdapter) ?? resolveSendCapableMessageAdapter(getChannelPlugin(normalized));
}
//#endregion
export { resolveOutboundChannelMessageAdapter as n, resolveOutboundChannelPlugin as r, normalizeDeliverableOutboundChannel as t };