Spaces:
Sleeping
Sleeping
Download src/core/plugins/plugin-loader.service.ts from jinksQspider/openWa: direct link, hf CLI and curl.
- Browser
- Download file 10.6 kB
-
https://huggingface.co/spaces/jinksQspider/openWa/resolve/main/src/core/plugins/plugin-loader.service.ts
- Command line
-
hf download hf://spaces/jinksQspider/openWa/src/core/plugins/plugin-loader.service.ts
-
curl -L -o plugin-loader.service.ts https://huggingface.co/spaces/jinksQspider/openWa/resolve/main/src/core/plugins/plugin-loader.service.ts
10.6 kB
| import { Injectable, OnModuleInit } from '@nestjs/common'; | |
| import { ConfigService } from '@nestjs/config'; | |
| import * as fs from 'fs'; | |
| import * as path from 'path'; | |
| import { createLogger } from '../../common/services/logger.service'; | |
| import { HookManager } from '../hooks'; | |
| import { | |
| PluginManifest, | |
| PluginInstance, | |
| PluginStatus, | |
| PluginContext, | |
| IPlugin, | |
| PluginType, | |
| PluginLogger, | |
| } from './plugin.interfaces'; | |
| import { PluginStorageService } from './plugin-storage.service'; | |
| () | |
| export class PluginLoaderService implements OnModuleInit { | |
| private readonly logger = createLogger('PluginLoaderService'); | |
| private readonly plugins = new Map<string, PluginInstance>(); | |
| private readonly pluginsDir: string; | |
| constructor( | |
| private readonly configService: ConfigService, | |
| private readonly hookManager: HookManager, | |
| private readonly pluginStorage: PluginStorageService, | |
| ) { | |
| this.pluginsDir = this.configService.get<string>('plugins.dir') ?? './plugins'; | |
| } | |
| onModuleInit(): void { | |
| // Load built-in plugins first (synchronous registration) | |
| this.loadBuiltInPlugins(); | |
| // Then load user plugins if directory exists | |
| if (fs.existsSync(this.pluginsDir)) { | |
| this.loadPluginsFromDirectory(this.pluginsDir); | |
| } | |
| this.logger.log(`Loaded ${this.plugins.size} plugins`, { | |
| action: 'plugins_loaded', | |
| count: this.plugins.size, | |
| }); | |
| } | |
| private loadBuiltInPlugins(): void { | |
| // Built-in plugins are registered programmatically | |
| // This will be used by Phase 4 to register engine plugins | |
| this.logger.debug('Built-in plugins loading point (Phase 4)', { | |
| action: 'builtin_plugins_init', | |
| }); | |
| } | |
| private loadPluginsFromDirectory(dir: string): void { | |
| const entries = fs.readdirSync(dir, { withFileTypes: true }); | |
| for (const entry of entries) { | |
| if (!entry.isDirectory()) continue; | |
| const pluginPath = path.join(dir, entry.name); | |
| const manifestPath = path.join(pluginPath, 'manifest.json'); | |
| if (!fs.existsSync(manifestPath)) { | |
| this.logger.warn(`Plugin ${entry.name} missing manifest.json`, { | |
| pluginPath, | |
| action: 'manifest_missing', | |
| }); | |
| continue; | |
| } | |
| try { | |
| this.loadPlugin(pluginPath); | |
| } catch (error) { | |
| this.logger.error( | |
| `Failed to load plugin ${entry.name}`, | |
| error instanceof Error ? error.message : String(error), | |
| { pluginPath, action: 'plugin_load_failed' }, | |
| ); | |
| } | |
| } | |
| } | |
| loadPlugin(pluginPath: string): PluginInstance { | |
| const manifestPath = path.join(pluginPath, 'manifest.json'); | |
| const manifestContent = fs.readFileSync(manifestPath, 'utf-8'); | |
| const manifest = JSON.parse(manifestContent) as PluginManifest; | |
| // Validate manifest | |
| if (!manifest.id || !manifest.name || !manifest.version || !manifest.type || !manifest.main) { | |
| throw new Error(`Invalid manifest: missing required fields`); | |
| } | |
| // Check if plugin already loaded | |
| if (this.plugins.has(manifest.id)) { | |
| throw new Error(`Plugin ${manifest.id} is already loaded`); | |
| } | |
| // Load stored config | |
| const storedConfig = this.pluginStorage.getPluginConfig(manifest.id); | |
| const pluginInstance: PluginInstance = { | |
| manifest, | |
| status: PluginStatus.INSTALLED, | |
| config: storedConfig ?? {}, | |
| instance: null, | |
| loadedAt: new Date(), | |
| }; | |
| this.plugins.set(manifest.id, pluginInstance); | |
| this.logger.log(`Plugin loaded: ${manifest.name} v${manifest.version}`, { | |
| pluginId: manifest.id, | |
| type: manifest.type, | |
| action: 'plugin_loaded', | |
| }); | |
| return pluginInstance; | |
| } | |
| async enablePlugin(pluginId: string): Promise<void> { | |
| const plugin = this.plugins.get(pluginId); | |
| if (!plugin) { | |
| throw new Error(`Plugin ${pluginId} not found`); | |
| } | |
| if (plugin.status === PluginStatus.ENABLED) { | |
| return; // Already enabled | |
| } | |
| try { | |
| // Create plugin context | |
| const context = this.createPluginContext(plugin); | |
| // Load the plugin instance if not already loaded | |
| if (!plugin.instance) { | |
| const mainPath = path.join(this.pluginsDir, pluginId, plugin.manifest.main); | |
| // Dynamic require for user plugins | |
| // eslint-disable-next-line @typescript-eslint/no-require-imports | |
| const pluginModule = require(mainPath) as { default?: new () => IPlugin }; | |
| if (pluginModule.default) { | |
| plugin.instance = new pluginModule.default(); | |
| } else { | |
| throw new Error(`Plugin ${pluginId} does not export a default class`); | |
| } | |
| } | |
| // Call lifecycle hooks | |
| if (plugin.instance.onLoad) { | |
| await plugin.instance.onLoad(context); | |
| } | |
| if (plugin.instance.onEnable) { | |
| await plugin.instance.onEnable(context); | |
| } | |
| plugin.status = PluginStatus.ENABLED; | |
| plugin.enabledAt = new Date(); | |
| plugin.error = undefined; | |
| // Persist status | |
| this.pluginStorage.setPluginStatus(pluginId, PluginStatus.ENABLED); | |
| this.logger.log(`Plugin enabled: ${plugin.manifest.name}`, { | |
| pluginId, | |
| action: 'plugin_enabled', | |
| }); | |
| } catch (error) { | |
| plugin.status = PluginStatus.ERROR; | |
| plugin.error = error instanceof Error ? error.message : String(error); | |
| this.pluginStorage.setPluginStatus(pluginId, PluginStatus.ERROR); | |
| throw error; | |
| } | |
| } | |
| async disablePlugin(pluginId: string): Promise<void> { | |
| const plugin = this.plugins.get(pluginId); | |
| if (!plugin) { | |
| throw new Error(`Plugin ${pluginId} not found`); | |
| } | |
| if (plugin.status !== PluginStatus.ENABLED) { | |
| return; // Not enabled | |
| } | |
| try { | |
| const context = this.createPluginContext(plugin); | |
| if (plugin.instance?.onDisable) { | |
| await plugin.instance.onDisable(context); | |
| } | |
| // Unregister all hooks for this plugin | |
| this.hookManager.unregisterPlugin(pluginId); | |
| plugin.status = PluginStatus.DISABLED; | |
| this.pluginStorage.setPluginStatus(pluginId, PluginStatus.DISABLED); | |
| this.logger.log(`Plugin disabled: ${plugin.manifest.name}`, { | |
| pluginId, | |
| action: 'plugin_disabled', | |
| }); | |
| } catch (error) { | |
| plugin.status = PluginStatus.ERROR; | |
| plugin.error = error instanceof Error ? error.message : String(error); | |
| throw error; | |
| } | |
| } | |
| async unloadPlugin(pluginId: string): Promise<void> { | |
| const plugin = this.plugins.get(pluginId); | |
| if (!plugin) { | |
| throw new Error(`Plugin ${pluginId} not found`); | |
| } | |
| // Disable first if enabled | |
| if (plugin.status === PluginStatus.ENABLED) { | |
| await this.disablePlugin(pluginId); | |
| } | |
| // Call onUnload | |
| if (plugin.instance?.onUnload) { | |
| const context = this.createPluginContext(plugin); | |
| await plugin.instance.onUnload(context); | |
| } | |
| this.plugins.delete(pluginId); | |
| this.logger.log(`Plugin unloaded: ${plugin.manifest.name}`, { | |
| pluginId, | |
| action: 'plugin_unloaded', | |
| }); | |
| } | |
| updatePluginConfig(pluginId: string, config: Record<string, unknown>): void { | |
| const plugin = this.plugins.get(pluginId); | |
| if (!plugin) { | |
| throw new Error(`Plugin ${pluginId} not found`); | |
| } | |
| plugin.config = { ...plugin.config, ...config }; | |
| // Persist config | |
| this.pluginStorage.setPluginConfig(pluginId, plugin.config); | |
| // Notify plugin of config change (async, fire and forget) | |
| if (plugin.instance?.onConfigChange && plugin.status === PluginStatus.ENABLED) { | |
| const context = this.createPluginContext(plugin); | |
| void plugin.instance.onConfigChange(context, plugin.config); | |
| } | |
| this.logger.debug(`Plugin config updated: ${pluginId}`, { | |
| pluginId, | |
| action: 'plugin_config_updated', | |
| }); | |
| } | |
| private createPluginContext(plugin: PluginInstance): PluginContext { | |
| const pluginLogger: PluginLogger = { | |
| log: (message, meta) => | |
| this.logger.log(`[${plugin.manifest.id}] ${message}`, { ...meta, pluginId: plugin.manifest.id }), | |
| debug: (message, meta) => | |
| this.logger.debug(`[${plugin.manifest.id}] ${message}`, { ...meta, pluginId: plugin.manifest.id }), | |
| warn: (message, meta) => | |
| this.logger.warn(`[${plugin.manifest.id}] ${message}`, { ...meta, pluginId: plugin.manifest.id }), | |
| error: (message, error, meta) => | |
| this.logger.error( | |
| `[${plugin.manifest.id}] ${message}`, | |
| error instanceof Error ? error.message : String(error), | |
| { ...meta, pluginId: plugin.manifest.id }, | |
| ), | |
| }; | |
| return { | |
| pluginId: plugin.manifest.id, | |
| manifest: plugin.manifest, | |
| config: plugin.config, | |
| hookManager: this.hookManager, | |
| logger: pluginLogger, | |
| storage: this.pluginStorage.createPluginStorage(plugin.manifest.id), | |
| registerHook: (event, handler, priority) => { | |
| this.hookManager.register(plugin.manifest.id, event, handler, priority); | |
| }, | |
| getService: <T>(): T | undefined => { | |
| // Limited service access for sandboxing | |
| // Only expose safe services | |
| return undefined; | |
| }, | |
| }; | |
| } | |
| // ============================================================================ | |
| // Query Methods | |
| // ============================================================================ | |
| getPlugin(pluginId: string): PluginInstance | undefined { | |
| return this.plugins.get(pluginId); | |
| } | |
| getAllPlugins(): PluginInstance[] { | |
| return Array.from(this.plugins.values()); | |
| } | |
| getPluginsByType(type: PluginType): PluginInstance[] { | |
| return this.getAllPlugins().filter(p => p.manifest.type === type); | |
| } | |
| getEnabledPlugins(): PluginInstance[] { | |
| return this.getAllPlugins().filter(p => p.status === PluginStatus.ENABLED); | |
| } | |
| isPluginEnabled(pluginId: string): boolean { | |
| const plugin = this.plugins.get(pluginId); | |
| return plugin?.status === PluginStatus.ENABLED; | |
| } | |
| // ============================================================================ | |
| // Built-in Plugin Registration (for Phase 4) | |
| // ============================================================================ | |
| registerBuiltInPlugin(manifest: PluginManifest, instance: IPlugin): void { | |
| const pluginInstance: PluginInstance = { | |
| manifest, | |
| status: PluginStatus.INSTALLED, | |
| config: {}, | |
| instance, | |
| loadedAt: new Date(), | |
| }; | |
| this.plugins.set(manifest.id, pluginInstance); | |
| this.logger.debug(`Built-in plugin registered: ${manifest.name}`, { | |
| pluginId: manifest.id, | |
| action: 'builtin_plugin_registered', | |
| }); | |
| } | |
| } | |