refactor: centralize channel plugin registry lookups

This commit is contained in:
Peter Steinberger
2026-03-22 18:05:53 +00:00
parent 2c919078e1
commit 17739910e9
2 changed files with 32 additions and 52 deletions

View File

@@ -23,6 +23,22 @@ function listRegisteredChannelPluginEntries(): RegisteredChannelPluginEntry[] {
return globalState[REGISTRY_STATE]?.registry?.channels ?? [];
}
function findRegisteredChannelPluginEntry(
normalizedKey: string,
): RegisteredChannelPluginEntry | undefined {
return listRegisteredChannelPluginEntries().find((entry) => {
const id = String(entry.plugin.id ?? "")
.trim()
.toLowerCase();
if (id && id === normalizedKey) {
return true;
}
return (entry.plugin.meta?.aliases ?? []).some(
(alias) => alias.trim().toLowerCase() === normalizedKey,
);
});
}
const CHAT_CHANNEL_META: Record<ChatChannelId, ChannelMeta> = {
telegram: {
id: "telegram",
@@ -167,17 +183,18 @@ export function normalizeAnyChannelId(raw?: string | null): ChannelId | null {
if (!key) {
return null;
}
return findRegisteredChannelPluginEntry(key)?.plugin.id ?? null;
}
const hit = listRegisteredChannelPluginEntries().find((entry) => {
const id = String(entry.plugin.id ?? "")
.trim()
.toLowerCase();
if (id && id === key) {
return true;
}
return (entry.plugin.meta?.aliases ?? []).some((alias) => alias.trim().toLowerCase() === key);
export function listRegisteredChannelPluginIds(): ChannelId[] {
return listRegisteredChannelPluginEntries().flatMap((entry) => {
const id = entry.plugin.id?.trim();
return id ? [id as ChannelId] : [];
});
return hit?.plugin.id ?? null;
}
export function listRegisteredChannelPluginAliases(): string[] {
return listRegisteredChannelPluginEntries().flatMap((entry) => entry.plugin.meta?.aliases ?? []);
}
export function formatChannelPrimerLine(meta: ChatChannelMeta): string {

View File

@@ -1,8 +1,11 @@
import type { ChannelId } from "../channels/plugins/types.js";
import {
CHANNEL_IDS,
listRegisteredChannelPluginAliases,
listRegisteredChannelPluginIds,
listChatChannelAliases,
normalizeChatChannelId,
normalizeAnyChannelId,
} from "../channels/registry.js";
import {
GATEWAY_CLIENT_MODES,
@@ -15,20 +18,6 @@ import {
export const INTERNAL_MESSAGE_CHANNEL = "webchat" as const;
export type InternalMessageChannel = typeof INTERNAL_MESSAGE_CHANNEL;
const REGISTRY_STATE = Symbol.for("openclaw.pluginRegistryState");
type PluginRegistryStateLike = {
registry?: {
channels?: Array<{
plugin: {
id: string;
meta: {
aliases?: string[];
};
};
}>;
} | null;
};
const MARKDOWN_CAPABLE_CHANNELS = new Set<string>([
"slack",
@@ -77,41 +66,15 @@ export function normalizeMessageChannel(raw?: string | null): string | undefined
if (builtIn) {
return builtIn;
}
const channels =
(
globalThis as typeof globalThis & {
[REGISTRY_STATE]?: PluginRegistryStateLike;
}
)[REGISTRY_STATE]?.registry?.channels ?? [];
const pluginMatch = channels.find((entry) => {
if (entry.plugin.id.toLowerCase() === normalized) {
return true;
}
return (entry.plugin.meta.aliases ?? []).some(
(alias) => alias.trim().toLowerCase() === normalized,
);
});
return pluginMatch?.plugin.id ?? normalized;
return normalizeAnyChannelId(normalized) ?? normalized;
}
const listPluginChannelIds = (): string[] => {
const channels =
(
globalThis as typeof globalThis & {
[REGISTRY_STATE]?: PluginRegistryStateLike;
}
)[REGISTRY_STATE]?.registry?.channels ?? [];
return channels.map((entry) => entry.plugin.id);
return listRegisteredChannelPluginIds();
};
const listPluginChannelAliases = (): string[] => {
const channels =
(
globalThis as typeof globalThis & {
[REGISTRY_STATE]?: PluginRegistryStateLike;
}
)[REGISTRY_STATE]?.registry?.channels ?? [];
return channels.flatMap((entry) => entry.plugin.meta.aliases ?? []);
return listRegisteredChannelPluginAliases();
};
export const listDeliverableMessageChannels = (): ChannelId[] =>