mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-30 17:10:21 +00:00
refactor: centralize channel plugin registry lookups
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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[] =>
|
||||
|
||||
Reference in New Issue
Block a user