feat: load channel plugins

This commit is contained in:
Peter Steinberger
2026-01-15 02:42:41 +00:00
parent b1e3d79eaa
commit 2b4a68e276
49 changed files with 494 additions and 159 deletions

View File

@@ -7,6 +7,7 @@ import { slackPlugin } from "./slack.js";
import { telegramPlugin } from "./telegram.js";
import type { ChannelId, ChannelPlugin } from "./types.js";
import { whatsappPlugin } from "./whatsapp.js";
import { getActivePluginRegistry } from "../../plugins/runtime.js";
// Channel plugins registry (runtime).
//
@@ -18,7 +19,7 @@ import { whatsappPlugin } from "./whatsapp.js";
// - add `<id>Plugin` import + entry in `resolveChannels()`
// - add an entry to `src/channels/dock.ts` for shared behavior (capabilities, allowFrom, threading, …)
// - add ids/aliases in `src/channels/registry.ts`
function resolveChannels(): ChannelPlugin[] {
function resolveCoreChannels(): ChannelPlugin[] {
return [
telegramPlugin,
whatsappPlugin,
@@ -30,8 +31,27 @@ function resolveChannels(): ChannelPlugin[] {
];
}
function listPluginChannels(): ChannelPlugin[] {
const registry = getActivePluginRegistry();
if (!registry) return [];
return registry.channels.map((entry) => entry.plugin);
}
function dedupeChannels(channels: ChannelPlugin[]): ChannelPlugin[] {
const seen = new Set<string>();
const resolved: ChannelPlugin[] = [];
for (const plugin of channels) {
const id = String(plugin.id).trim();
if (!id || seen.has(id)) continue;
seen.add(id);
resolved.push(plugin);
}
return resolved;
}
export function listChannelPlugins(): ChannelPlugin[] {
return resolveChannels().sort((a, b) => {
const combined = dedupeChannels([...resolveCoreChannels(), ...listPluginChannels()]);
return combined.sort((a, b) => {
const indexA = CHAT_CHANNEL_ORDER.indexOf(a.id as ChatChannelId);
const indexB = CHAT_CHANNEL_ORDER.indexOf(b.id as ChatChannelId);
const orderA = a.meta.order ?? (indexA === -1 ? 999 : indexA);
@@ -42,13 +62,24 @@ export function listChannelPlugins(): ChannelPlugin[] {
}
export function getChannelPlugin(id: ChannelId): ChannelPlugin | undefined {
return resolveChannels().find((plugin) => plugin.id === id);
const resolvedId = String(id).trim();
if (!resolvedId) return undefined;
return listChannelPlugins().find((plugin) => plugin.id === resolvedId);
}
export function normalizeChannelId(raw?: string | null): ChannelId | null {
// Channel docking: keep input normalization centralized in src/channels/registry.ts
// so CLI/API/protocol can rely on stable aliases without plugin init side effects.
return normalizeChatChannelId(raw);
const normalized = normalizeChatChannelId(raw);
if (normalized) return normalized;
const trimmed = raw?.trim();
if (!trimmed) return null;
const key = trimmed.toLowerCase();
const plugin = listChannelPlugins().find((entry) => {
if (entry.id.toLowerCase() === key) return true;
return (entry.meta.aliases ?? []).some((alias) => alias.trim().toLowerCase() === key);
});
return plugin?.id ?? null;
}
export {