/** * Plugin System Interfaces * Defines the contract for OpenWA plugins */ import { HookManager, HookEvent, HookHandler } from '../hooks'; // ============================================================================ // Plugin Types // ============================================================================ export enum PluginType { ENGINE = 'engine', // WhatsApp engine (whatsapp-web.js, baileys, etc.) STORAGE = 'storage', // Storage backends (local, S3, GCS, etc.) QUEUE = 'queue', // Queue systems (Redis, RabbitMQ, etc.) AUTH = 'auth', // Authentication providers EXTENSION = 'extension', // General extensions (auto-reply, scheduler, etc.) } export enum PluginStatus { INSTALLED = 'installed', ENABLED = 'enabled', DISABLED = 'disabled', ERROR = 'error', } // ============================================================================ // Plugin Manifest // ============================================================================ export interface PluginManifest { id: string; // Unique identifier (e.g., 'whatsapp-web.js', 'auto-reply') name: string; // Display name version: string; // Semver type: PluginType; description?: string; author?: string; homepage?: string; repository?: string; license?: string; // Entry point main: string; // Relative path to main file // Dependencies dependencies?: Record; peerDependencies?: Record; // Configuration schema (optional, for UI generation) configSchema?: PluginConfigSchema; // Hooks this plugin listens to hooks?: HookEvent[]; // Features provided by this plugin provides?: string[]; // Required features from other plugins requires?: string[]; } export interface PluginConfigSchema { type: 'object'; properties: Record< string, { type: 'string' | 'number' | 'boolean' | 'array' | 'object'; title?: string; description?: string; default?: unknown; enum?: unknown[]; required?: boolean; secret?: boolean; // For sensitive values like API keys } >; } // ============================================================================ // Plugin Context (passed to plugin on initialization) // ============================================================================ export interface PluginContext { // Plugin info pluginId: string; manifest: PluginManifest; // Configuration config: Record; // Hook system hookManager: HookManager; // Logger instance for this plugin logger: PluginLogger; // Storage for plugin data storage: PluginStorage; // Register a hook handler registerHook: (event: HookEvent, handler: HookHandler, priority?: number) => void; // Get service from DI container (limited access) getService: (token: string) => T | undefined; } export interface PluginLogger { log: (message: string, meta?: Record) => void; debug: (message: string, meta?: Record) => void; warn: (message: string, meta?: Record) => void; error: (message: string, error?: unknown, meta?: Record) => void; } export interface PluginStorage { get: (key: string) => Promise; set: (key: string, value: T) => Promise; delete: (key: string) => Promise; list: (prefix?: string) => Promise; } // ============================================================================ // Plugin Interface (what plugins must implement) // ============================================================================ export interface IPlugin { // Lifecycle hooks onLoad?: (context: PluginContext) => Promise; onEnable?: (context: PluginContext) => Promise; onDisable?: (context: PluginContext) => Promise; onUnload?: (context: PluginContext) => Promise; // Configuration change handler onConfigChange?: (context: PluginContext, newConfig: Record) => Promise; // Health check (for dashboard monitoring) healthCheck?: () => Promise<{ healthy: boolean; message?: string }>; } // ============================================================================ // Engine Plugin Interface (extends IPlugin for engine-specific methods) // ============================================================================ export interface IEnginePlugin extends IPlugin { type: PluginType.ENGINE; // Engine factory method createEngine: (config: Record) => unknown; // Get supported features getFeatures: () => string[]; } // ============================================================================ // Plugin Instance (runtime representation) // ============================================================================ export interface PluginInstance { manifest: PluginManifest; status: PluginStatus; config: Record; instance: IPlugin | null; error?: string; loadedAt?: Date; enabledAt?: Date; } // ============================================================================ // Plugin Registry Entry (for storage) // ============================================================================ export interface PluginRegistryEntry { id: string; type: PluginType; name: string; version: string; status: PluginStatus; config: Record; builtIn: boolean; // True for bundled plugins installedAt: Date; updatedAt: Date; }