-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin-loader.ts
More file actions
556 lines (478 loc) · 15.7 KB
/
Copy pathplugin-loader.ts
File metadata and controls
556 lines (478 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/**
* Plugin loader — handles the auto-loaded bootstrap plugin tier (`core/*`)
* and registry-managed user plugins (`plugins/*`).
*
* Boot sequence:
* 1. Gateway loads packages/db (always)
* 2. DB connects, creates _gateway schema
* 3. Gateway loads bootstrap plugins from core/ (always, not in registry)
* 4. Gateway loads user plugins from plugins/ (filtered by DB registry)
*
* Plugins in core/<name>/ are ordinary consumer plugins with one special rule:
* they are auto-loaded at boot.
*
* Bootstrap plugins:
* - Located at core/<name>/
* - Always loaded, cannot be uninstalled
* - Not tracked in _gateway.plugin_registry
*
* User plugins are installable/removable:
* - Installed via git clone into plugins/<name>/
* - Tracked in _gateway.plugin_registry
* - Lifecycle managed (install/uninstall/enable/disable)
*/
import * as fs from 'node:fs';
import * as path from 'node:path';
import { pathToFileURL } from 'node:url';
import { execSync } from 'node:child_process';
import { type Context, type FiberState, type Plugin } from 'cordis';
import type { GatewayConfig } from './config.js';
import { type DatabaseService } from '@tradinggoose/db';
import { GatewayPluginManifestSchema, type GatewayPluginManifest } from '@tradinggoose/shared';
// --- Types ---
export interface LoadedPlugin {
name: string;
path: string;
bootstrap: boolean;
manifest: GatewayPluginManifest;
fiber?: PluginFiber;
}
export interface InstallResult {
success: boolean;
name: string;
error?: string;
restartRequired?: boolean;
activationOrder?: string[];
}
export interface PluginFiber {
state: FiberState;
dispose(): Promise<unknown> | unknown;
}
export interface MarketplaceEntry {
name: string;
source: string;
description?: string;
version?: string;
category?: string;
}
export interface Marketplace {
name: string;
displayName?: string;
plugins: MarketplaceEntry[];
}
// --- Paths ---
export function corePluginsDir(): string {
// Core plugins live at project-root/core/ — same dir as plugin-loader.ts
const here = import.meta.dirname ?? path.dirname(new URL(import.meta.url).pathname);
return path.resolve(here, 'core');
}
// --- Source resolution ---
function sourceToGitUrl(source: string): string | null {
if (source.startsWith('github:')) {
return `https://github.com/${source.slice('github:'.length)}.git`;
}
if (source.startsWith('https://') || source.startsWith('git@')) {
return source;
}
return null;
}
function pluginNameFromSource(source: string): string {
const repo = source.includes('/') ? source.split('/').pop()! : source;
return repo
.replace(/\.git$/, '')
.replace(/^tg-plugin-/, '')
.replace(/^tradinggoose-plugin-/, '')
.replace(/^plugin-/, '');
}
interface PluginCandidate {
name: string;
pluginPath: string;
bootstrap: boolean;
manifest: GatewayPluginManifest;
}
export async function readPluginManifest(pluginPath: string): Promise<GatewayPluginManifest> {
const manifestPath = path.join(pluginPath, 'manifest.ts');
if (!fs.existsSync(manifestPath)) {
throw new Error(`Missing required manifest.ts in ${pluginPath}`);
}
const ref = `${pathToFileURL(manifestPath).href}?mtime=${fs.statSync(manifestPath).mtimeMs}`;
const mod = await import(ref);
const candidate = mod.manifest ?? mod.default ?? mod;
return GatewayPluginManifestSchema.parse(candidate);
}
function isCordisPlugin(value: unknown): value is Plugin<Context, any> {
return typeof value === 'function'
|| (typeof value === 'object' && value !== null && typeof (value as { apply?: unknown }).apply === 'function');
}
function normalizeImportedPlugin(mod: Record<string, unknown>): Plugin<Context, any> {
const plugin = mod.default ?? mod;
if (!isCordisPlugin(plugin)) {
throw new Error('Plugin entrypoint must export a Cordis plugin function or object');
}
if (!mod.default) {
return plugin;
}
const metadata = Object.fromEntries(
Object.entries(mod).filter(([key]) => key !== 'default' && key !== '__esModule'),
);
return Object.assign(plugin as object, metadata) as Plugin<Context, any>;
}
async function candidateFromPath(
name: string,
pluginPath: string,
bootstrap: boolean,
db?: DatabaseService,
): Promise<PluginCandidate | null> {
try {
return {
name,
pluginPath,
bootstrap,
manifest: await readPluginManifest(pluginPath),
};
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.error(`[loader] invalid manifest: ${name} — ${message}`);
await db?.logError?.({
source: 'core.plugin-loader',
message: `Invalid manifest for plugin "${name}"`,
stack: error instanceof Error ? error.stack : undefined,
context: { pluginPath, reason: message },
});
return null;
}
}
async function loadCandidateBatch(
candidates: PluginCandidate[],
ctx: Context,
config: GatewayConfig,
initialLoaded: LoadedPlugin[] = [],
onLoaded?: (plugin: LoadedPlugin) => void,
): Promise<LoadedPlugin[]> {
const db = getRootDb(ctx);
const dataDir = path.resolve(config.dataDir);
const loaded = [...initialLoaded];
for (const candidate of candidates.slice().sort((left, right) => left.name.localeCompare(right.name))) {
const result = await loadOne({
name: candidate.name,
pluginPath: candidate.pluginPath,
dataDir,
db,
ctx,
bootstrap: candidate.bootstrap,
manifest: candidate.manifest,
});
if (result) {
loaded.push(result);
onLoaded?.(result);
}
}
return loaded.slice(initialLoaded.length);
}
// --- Shared loader ---
interface LoadOptions {
name: string;
pluginPath: string;
dataDir: string;
db?: DatabaseService;
ctx: Context;
bootstrap: boolean;
manifest: GatewayPluginManifest;
}
async function loadOne(opts: LoadOptions): Promise<LoadedPlugin | null> {
const { name, pluginPath, dataDir, db, ctx, bootstrap, manifest } = opts;
const entryFile = path.join(pluginPath, 'index.ts');
if (!fs.existsSync(entryFile)) {
console.warn(`[loader] "${name}" missing index.ts at ${pluginPath}`);
return null;
}
try {
const pluginDataDir = path.join(dataDir, name);
fs.mkdirSync(pluginDataDir, { recursive: true });
if (db) {
await db.createPluginSchema(name);
const migrationsDir = path.join(pluginPath, 'migrations');
await db.runPluginMigrations(name, migrationsDir);
}
const mod = (await import(pathToFileURL(entryFile).href)) as Record<string, unknown>;
const plugin = normalizeImportedPlugin(mod);
const pluginCtx = db
? ctx
.isolate('db')
.isolate('dbRoot')
.extend()
: ctx;
if (db) {
pluginCtx.provide('db', db.createPluginDb(name));
}
const fiber = pluginCtx.plugin(plugin, { name, dataDir: pluginDataDir });
await fiber;
const tag = bootstrap ? 'bootstrap' : 'user';
console.log(`[loader] loaded (${tag}): ${name}`);
return {
name,
path: pluginPath,
bootstrap,
manifest,
fiber: isPluginFiber(fiber) ? fiber : undefined,
};
} catch (e) {
const message = e instanceof Error ? e.message : String(e);
console.error(`[loader] failed: ${name} — ${message}`);
await db?.logError?.({
source: 'core.plugin-loader',
message: `Failed to load plugin "${name}"`,
stack: e instanceof Error ? e.stack : undefined,
context: { bootstrap, pluginPath, reason: message },
});
return null;
}
}
// --- Core plugin loading ---
export async function loadCorePlugins(
ctx: Context,
config: GatewayConfig,
onLoaded?: (plugin: LoadedPlugin) => void,
): Promise<LoadedPlugin[]> {
const dir = corePluginsDir();
if (!fs.existsSync(dir)) {
return [];
}
const db = getRootDb(ctx);
const entries = fs.readdirSync(dir, { withFileTypes: true }).filter((e) => e.isDirectory());
const candidates: PluginCandidate[] = [];
console.log(`[loader] loading ${entries.length} bootstrap plugins`);
for (const entry of entries) {
const candidate = await candidateFromPath(entry.name, path.join(dir, entry.name), true, db);
if (candidate) candidates.push(candidate);
}
return loadCandidateBatch(candidates, ctx, config, [], onLoaded);
}
export async function loadCorePlugin(
ctx: Context,
config: GatewayConfig,
name: string,
): Promise<LoadedPlugin | null> {
const db = getRootDb(ctx);
const pluginPath = path.join(corePluginsDir(), name);
const candidate = await candidateFromPath(name, pluginPath, true, db);
if (!candidate) return null;
return loadOne({
name,
pluginPath,
dataDir: path.resolve(config.dataDir),
db,
ctx,
bootstrap: true,
manifest: candidate.manifest,
});
}
// --- User plugin loading (registry-driven) ---
export async function loadUserPlugins(
ctx: Context,
config: GatewayConfig,
initialLoaded: LoadedPlugin[] = [],
onLoaded?: (plugin: LoadedPlugin) => void,
): Promise<LoadedPlugin[]> {
const pluginDir = path.resolve(config.pluginDir);
fs.mkdirSync(pluginDir, { recursive: true });
const db = getRequiredRootDb(ctx);
const enabled = await db.getEnabledPlugins();
if (enabled.length === 0) {
console.log('[loader] no user plugins enabled');
return [];
}
console.log(`[loader] loading ${enabled.length} user plugins`);
const candidates: PluginCandidate[] = [];
for (const entry of enabled) {
const candidate = await candidateFromPath(entry.name, path.join(pluginDir, entry.name), false, db);
if (candidate) candidates.push(candidate);
}
return loadCandidateBatch(candidates, ctx, config, initialLoaded, onLoaded);
}
export async function loadUserPlugin(
ctx: Context,
config: GatewayConfig,
name: string,
): Promise<LoadedPlugin | null> {
const pluginPath = path.join(path.resolve(config.pluginDir), name);
const db = getRequiredRootDb(ctx);
const candidate = await candidateFromPath(name, pluginPath, false, db);
if (!candidate) return null;
return loadOne({
name,
pluginPath,
dataDir: path.resolve(config.dataDir),
db,
ctx,
bootstrap: false,
manifest: candidate.manifest,
});
}
/** Convenience: load bootstrap plugins from `core/*` then user plugins. */
export async function loadPlugins(
ctx: Context,
config: GatewayConfig,
onLoaded?: (plugin: LoadedPlugin) => void,
): Promise<LoadedPlugin[]> {
const bootstrap = await loadCorePlugins(ctx, config, onLoaded);
const user = await loadUserPlugins(ctx, config, bootstrap, onLoaded);
return [...bootstrap, ...user];
}
// --- Install / Uninstall (user plugins only) ---
export async function installPlugin(
source: string,
db: DatabaseService,
config: GatewayConfig,
): Promise<InstallResult> {
const pluginDir = path.resolve(config.pluginDir);
const name = pluginNameFromSource(source);
const pluginPath = path.join(pluginDir, name);
const all = await db.getAllPlugins();
if (all.some((p) => p.name === name)) {
return { success: false, name, error: `Plugin "${name}" is already registered` };
}
const gitUrl = sourceToGitUrl(source);
if (!gitUrl) {
await db.logError?.({
source: 'core.plugin-loader',
message: `Cannot install plugin "${name}" from unsupported source`,
context: { source },
});
return { success: false, name, error: `Cannot install from source: ${source}` };
}
try {
fs.mkdirSync(pluginDir, { recursive: true });
execSync(`git clone --depth 1 ${gitUrl} ${pluginPath}`, {
stdio: ['ignore', 'pipe', 'pipe'],
timeout: 60000,
});
} catch (e) {
if (fs.existsSync(pluginPath)) {
fs.rmSync(pluginPath, { recursive: true, force: true });
}
await db.logError?.({
source: 'core.plugin-loader',
message: `Git clone failed while installing plugin "${name}"`,
stack: e instanceof Error ? e.stack : undefined,
context: { source, gitUrl, pluginPath },
});
return {
success: false,
name,
error: `git clone failed: ${e instanceof Error ? e.message : String(e)}`,
};
}
if (!fs.existsSync(path.join(pluginPath, 'index.ts'))) {
fs.rmSync(pluginPath, { recursive: true, force: true });
await db.logError?.({
source: 'core.plugin-loader',
message: `Installed plugin "${name}" is missing index.ts`,
context: { source, pluginPath },
});
return { success: false, name, error: 'Plugin has no index.ts entry point' };
}
if (fs.existsSync(path.join(pluginPath, 'package.json'))) {
try {
execSync('bun install', {
cwd: pluginPath,
stdio: ['ignore', 'pipe', 'pipe'],
timeout: 120000,
});
} catch {
console.warn(`[installer] bun install failed for ${name} (continuing)`);
}
}
try {
await db.createPluginSchema(name);
const migrationsDir = path.join(pluginPath, 'migrations');
await db.runPluginMigrations(name, migrationsDir);
await db.registerPlugin(name, source);
} catch (e) {
await safeCleanupFailedInstall(name, db, pluginPath);
await db.logError?.({
source: 'core.plugin-loader',
message: `Failed to finalize install for plugin "${name}"`,
stack: e instanceof Error ? e.stack : undefined,
context: { source, pluginPath },
});
return {
success: false,
name,
error: `install failed: ${e instanceof Error ? e.message : String(e)}`,
};
}
console.log(`[installer] installed: ${name} from ${source}`);
return { success: true, name, restartRequired: false, activationOrder: [name] };
}
export async function uninstallPlugin(
name: string,
db: DatabaseService,
config: GatewayConfig,
opts?: { keepData?: boolean },
): Promise<InstallResult> {
const pluginDir = path.resolve(config.pluginDir);
const dataDir = path.resolve(config.dataDir);
const all = await db.getAllPlugins();
if (!all.some((p) => p.name === name)) {
return { success: false, name, error: `Plugin "${name}" is not registered` };
}
if (!opts?.keepData) {
await db.dropPluginSchema(name);
}
await db.unregisterPlugin(name);
const pluginPath = path.join(pluginDir, name);
if (fs.existsSync(pluginPath)) {
fs.rmSync(pluginPath, { recursive: true, force: true });
}
const pluginDataDir = path.join(dataDir, name);
if (fs.existsSync(pluginDataDir)) {
fs.rmSync(pluginDataDir, { recursive: true, force: true });
}
console.log(`[installer] uninstalled: ${name}`);
return { success: true, name, restartRequired: true };
}
// --- Marketplace ---
export async function fetchMarketplace(url: string): Promise<Marketplace | null> {
try {
const resp = await fetch(url);
if (!resp.ok) return null;
return (await resp.json()) as Marketplace;
} catch {
console.warn(`[marketplace] failed to fetch: ${url}`);
return null;
}
}
async function safeCleanupFailedInstall(
name: string,
db: DatabaseService,
pluginPath: string,
): Promise<void> {
try {
await db.dropPluginSchema(name);
} catch {}
try {
await db.unregisterPlugin(name);
} catch {}
if (fs.existsSync(pluginPath)) {
fs.rmSync(pluginPath, { recursive: true, force: true });
}
}
function isPluginFiber(value: unknown): value is PluginFiber {
return !!value && typeof value === 'object' && 'dispose' in value && typeof (value as any).dispose === 'function';
}
function getRootDb(ctx: Context): DatabaseService | undefined {
const root = ctx.root ?? ctx;
if (typeof root.get === 'function') {
return root.get('dbRoot') as DatabaseService | undefined;
}
const unsafeCtx = root as unknown as Record<string, unknown>;
return (unsafeCtx.dbRoot ?? unsafeCtx.db) as DatabaseService | undefined;
}
function getRequiredRootDb(ctx: Context): DatabaseService {
const db = getRootDb(ctx);
if (!db) {
throw new Error('Root database service "dbRoot" is not available');
}
return db;
}