import { Injectable, type OnModuleInit, type OnModuleDestroy } from '@nestjs/common'; import { db } from '../../db/database'; import { pluginsEnabled } from './kill-switch'; import { decrypt_api_key } from '../../services/apiKeyCrypto'; import { PluginSupervisor, type PluginRouteInfo } from './supervisor/plugin-supervisor'; import fs from 'node:fs'; import { createRealRpcHost, closePluginDataDb } from './host/create-rpc-host'; import { removePluginData } from './host/plugin-data.service'; import { isKnownPermission } from './protocol/envelope'; import { discoverPlugins } from './install/discovery'; import { pluginCodeDir } from './paths'; /** * Owns the isolated-plugin runtime lifecycle inside NestJS (#plugins, M2). * Bridges the DB registry (`plugins` rows) to the process supervisor: activate * spawns the child with its granted permissions + decrypted instance config, * deactivate kills it, and status/errors are persisted back to the DB. Boots all * `active` plugins on startup when the runtime is enabled. */ interface PluginRow { id: string; status: string; permissions: string; granted_permissions: string; config: string; } @Injectable() export class PluginRuntimeService implements OnModuleInit, OnModuleDestroy { private readonly supervisor = new PluginSupervisor(createRealRpcHost, { onStatus: (id, status, error) => { db.prepare('UPDATE plugins SET status = ?, last_error = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?').run( status, error ?? null, id, ); }, onLog: (id, level, msg) => { if (level === 'error' || level === 'warn') { db.prepare('INSERT INTO plugin_error_log (plugin_id, level, message) VALUES (?, ?, ?)').run(id, level, msg); } }, }); onModuleInit(): void { if (!pluginsEnabled()) return; // Discover plugins placed on the volume (registers new ones inactive), then // boot the ones an admin had already activated. try { discoverPlugins(db); } catch { /* discovery must never block boot */ } // Boot everything the admin left ENABLED, regardless of the last runtime // status — a crash or a bad deploy set status='error' but must not silently // keep the plugin down forever. const enabled = db.prepare('SELECT id FROM plugins WHERE enabled = 1').all() as Array<{ id: string }>; for (const { id } of enabled) { this.activate(id).catch(() => { /* status is persisted as error by the supervisor hook */ }); } } /** Re-scan the plugins volume on demand (admin action). */ rescan(): { discovered: string[]; skipped: string[] } { return discoverPlugins(db); } async onModuleDestroy(): Promise { await this.supervisor.shutdownAll(); } /** Spawn a plugin from its DB row (granted permissions + decrypted config). */ async activate(id: string): Promise { const row = db.prepare('SELECT id, status, permissions, granted_permissions, config FROM plugins WHERE id = ?').get(id) as | PluginRow | undefined; if (!row) throw new Error(`plugin ${id} not found`); // Activating IS the consent gate: grant the plugin's DECLARED permissions // (the admin reviewed them on the consent screen), persist, then spawn. const declared = parseArray(row.permissions).filter(isKnownPermission); // Mark it enabled (admin intent) so it reboots after restarts/crashes. db.prepare('UPDATE plugins SET granted_permissions = ?, enabled = 1 WHERE id = ?').run(JSON.stringify(declared), id); const config = decryptConfig(parseObject(row.config)); const egress = declared.filter((p) => p.startsWith('http:outbound:')).map((p) => p.slice('http:outbound:'.length)); await this.supervisor.activate(id, new Set(declared), config, egress); } async deactivate(id: string): Promise { await this.supervisor.disable(id); closePluginDataDb(id); db.prepare("UPDATE plugins SET status = 'inactive', enabled = 0, updated_at = CURRENT_TIMESTAMP WHERE id = ?").run(id); } /** Stop the plugin, remove its code, and optionally delete all its data. */ async uninstall(id: string, deleteData: boolean): Promise { await this.supervisor.disable(id); closePluginDataDb(id); // Code always goes; the DB metadata + fields go so it disappears from the UI. fs.rmSync(pluginCodeDir(id), { recursive: true, force: true }); db.prepare('DELETE FROM plugins WHERE id = ?').run(id); db.prepare('DELETE FROM plugin_settings_fields WHERE plugin_id = ?').run(id); if (deleteData) { removePluginData(id); db.prepare('DELETE FROM plugin_error_log WHERE plugin_id = ?').run(id); db.prepare("DELETE FROM settings WHERE key LIKE ?").run(`plugin:${id}:%`); } } isActive(id: string): boolean { return this.supervisor.isActive(id); } /** Declared outbound hosts (from http:outbound: grants) for the frame CSP. */ outboundHostsOf(id: string): string[] { const row = db.prepare('SELECT granted_permissions FROM plugins WHERE id = ?').get(id) as | { granted_permissions: string } | undefined; if (!row) return []; return parseArray(row.granted_permissions) .filter((p) => p.startsWith('http:outbound:')) .map((p) => p.slice('http:outbound:'.length)) .filter(Boolean); } routesOf(id: string): PluginRouteInfo[] { return this.supervisor.routesOf(id); } invoke(id: string, method: string, params: Record): Promise { return this.supervisor.invoke(id, method, params); } } function parseArray(json: string): string[] { try { const v = JSON.parse(json || '[]'); return Array.isArray(v) ? v.map(String) : []; } catch { return []; } } function parseObject(json: string): Record { try { const v = JSON.parse(json || '{}'); return v && typeof v === 'object' && !Array.isArray(v) ? (v as Record) : {}; } catch { return {}; } } /** Decrypt secret config values transparently (decrypt_api_key passes plaintext through). */ function decryptConfig(config: Record): Record { const out: Record = {}; for (const [k, v] of Object.entries(config)) { out[k] = typeof v === 'string' ? decrypt_api_key(v) : v; } return out; }