import type { OutboundSendDeps } from "../infra/outbound/send-deps.js"; import { createLazyRuntimeSurface } from "../shared/lazy-runtime.js"; import { createOutboundSendDepsFromCliSource } from "./outbound-send-mapping.js"; /** * Lazy-loaded per-channel send functions, keyed by channel ID. * Values are proxy functions that dynamically import the real module on first use. */ export type CliDeps = { [channelId: string]: unknown }; type RuntimeSend = { sendMessage: (...args: unknown[]) => Promise; }; type RuntimeSendModule = { runtimeSend: RuntimeSend; }; // Per-channel module caches for lazy loading. const senderCache = new Map>(); /** * Create a lazy-loading send function proxy for a channel. * The channel's module is loaded on first call and cached for reuse. */ function createLazySender( channelId: string, loader: () => Promise, ): (...args: unknown[]) => Promise { const loadRuntimeSend = createLazyRuntimeSurface(loader, ({ runtimeSend }) => runtimeSend); return async (...args: unknown[]) => { let cached = senderCache.get(channelId); if (!cached) { cached = loadRuntimeSend(); senderCache.set(channelId, cached); } const runtimeSend = await cached; return await runtimeSend.sendMessage(...args); }; } export function createDefaultDeps(): CliDeps { // Keep the default dependency barrel limited to lazy senders so callers that // only need outbound deps do not pull channel runtime boundaries on import. return { whatsapp: createLazySender( "whatsapp", () => import("./send-runtime/whatsapp.js") as Promise, ), telegram: createLazySender( "telegram", () => import("./send-runtime/telegram.js") as Promise, ), discord: createLazySender( "discord", () => import("./send-runtime/discord.js") as Promise, ), slack: createLazySender( "slack", () => import("./send-runtime/slack.js") as Promise, ), signal: createLazySender( "signal", () => import("./send-runtime/signal.js") as Promise, ), imessage: createLazySender( "imessage", () => import("./send-runtime/imessage.js") as Promise, ), }; } export function createOutboundSendDeps(deps: CliDeps): OutboundSendDeps { return createOutboundSendDepsFromCliSource(deps); }