refactor: migrate messaging plugins to sdk

This commit is contained in:
Peter Steinberger
2026-01-18 08:32:19 +00:00
parent 9241e21114
commit c5e19f5c67
63 changed files with 4082 additions and 376 deletions

View File

@@ -1,36 +1,25 @@
import type { ChannelId, ChannelPlugin } from "./types.js";
import type { ChatChannelId } from "../registry.js";
import type { PluginRegistry } from "../../plugins/registry.js";
import { getActivePluginRegistry } from "../../plugins/runtime.js";
type PluginLoader = () => Promise<ChannelPlugin>;
// Channel docking: load *one* plugin on-demand.
//
// This avoids importing `src/channels/plugins/index.ts` (intentionally heavy)
// from shared flows like outbound delivery / followup routing.
const LOADERS: Record<ChatChannelId, PluginLoader> = {
telegram: async () => (await import("./telegram.js")).telegramPlugin,
whatsapp: async () => (await import("./whatsapp.js")).whatsappPlugin,
discord: async () => (await import("./discord.js")).discordPlugin,
slack: async () => (await import("./slack.js")).slackPlugin,
signal: async () => (await import("./signal.js")).signalPlugin,
imessage: async () => (await import("./imessage.js")).imessagePlugin,
};
const cache = new Map<ChannelId, ChannelPlugin>();
let lastRegistry: PluginRegistry | null = null;
function ensureCacheForRegistry(registry: PluginRegistry | null) {
if (registry === lastRegistry) return;
cache.clear();
lastRegistry = registry;
}
export async function loadChannelPlugin(id: ChannelId): Promise<ChannelPlugin | undefined> {
const registry = getActivePluginRegistry();
ensureCacheForRegistry(registry);
const cached = cache.get(id);
if (cached) return cached;
const registry = getActivePluginRegistry();
const pluginEntry = registry?.channels.find((entry) => entry.plugin.id === id);
if (pluginEntry) {
cache.set(id, pluginEntry.plugin);
return pluginEntry.plugin;
}
const loader = LOADERS[id as ChatChannelId];
if (!loader) return undefined;
const plugin = await loader();
cache.set(id, plugin);
return plugin;
return undefined;
}