forked from lioensky/VCPToolBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.js
More file actions
executable file
·1210 lines (1065 loc) · 66.5 KB
/
Plugin.js
File metadata and controls
executable file
·1210 lines (1065 loc) · 66.5 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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Plugin.js
const fs = require('fs').promises;
const path = require('path');
const { spawn } = require('child_process');
const schedule = require('node-schedule');
const dotenv = require('dotenv'); // Ensures dotenv is available
const FileFetcherServer = require('./FileFetcherServer.js');
const express = require('express'); // For plugin API routing
const chokidar = require('chokidar');
const { getAuthCode } = require('./modules/captchaDecoder'); // 导入统一的解码函数
const { VectorDBManager } = require('./VectorDBManager.js');
const PLUGIN_DIR = path.join(__dirname, 'Plugin');
const manifestFileName = 'plugin-manifest.json';
const PREPROCESSOR_ORDER_FILE = path.join(__dirname, 'preprocessor_order.json');
class PluginManager {
constructor() {
this.plugins = new Map(); // 存储所有插件(本地和分布式)
this.staticPlaceholderValues = new Map();
this.scheduledJobs = new Map();
this.messagePreprocessors = new Map();
this.preprocessorOrder = []; // 新增:用于存储预处理器的最终加载顺序
this.serviceModules = new Map();
this.projectBasePath = null;
this.individualPluginDescriptions = new Map(); // New map for individual descriptions
this.debugMode = (process.env.DebugMode || "False").toLowerCase() === "true";
this.webSocketServer = null; // 为 WebSocketServer 实例占位
this.isReloading = false;
this.reloadTimeout = null;
this.vectorDBManager = new VectorDBManager();
}
setWebSocketServer(wss) {
this.webSocketServer = wss;
if (this.debugMode) console.log('[PluginManager] WebSocketServer instance has been set.');
}
async _getDecryptedAuthCode() {
try {
const authCodePath = path.join(__dirname, 'Plugin', 'UserAuth', 'code.bin');
// 使用正确的 getAuthCode 函数,并传递文件路径
return await getAuthCode(authCodePath);
} catch (error) {
if (this.debugMode) {
console.error('[PluginManager] Failed to read or decrypt auth code for plugin execution:', error.message);
}
return null; // Return null if code cannot be obtained
}
}
setProjectBasePath(basePath) {
this.projectBasePath = basePath;
if (this.debugMode) console.log(`[PluginManager] Project base path set to: ${this.projectBasePath}`);
}
_getPluginConfig(pluginManifest) {
const config = {};
const globalEnv = process.env;
const pluginSpecificEnv = pluginManifest.pluginSpecificEnvConfig || {};
if (pluginManifest.configSchema) {
for (const key in pluginManifest.configSchema) {
const expectedType = pluginManifest.configSchema[key];
let rawValue;
if (pluginSpecificEnv.hasOwnProperty(key)) {
rawValue = pluginSpecificEnv[key];
} else if (globalEnv.hasOwnProperty(key)) {
rawValue = globalEnv[key];
} else {
continue;
}
let value = rawValue;
if (expectedType === 'integer') {
value = parseInt(value, 10);
if (isNaN(value)) {
if (this.debugMode) console.warn(`[PluginManager] Config key '${key}' for ${pluginManifest.name} expected integer, got NaN from raw value '${rawValue}'. Using undefined.`);
value = undefined;
}
} else if (expectedType === 'boolean') {
value = String(value).toLowerCase() === 'true';
}
config[key] = value;
}
}
if (pluginSpecificEnv.hasOwnProperty('DebugMode')) {
config.DebugMode = String(pluginSpecificEnv.DebugMode).toLowerCase() === 'true';
} else if (globalEnv.hasOwnProperty('DebugMode')) {
config.DebugMode = String(globalEnv.DebugMode).toLowerCase() === 'true';
} else if (!config.hasOwnProperty('DebugMode')) {
config.DebugMode = false;
}
return config;
}
getResolvedPluginConfigValue(pluginName, configKey) {
const pluginManifest = this.plugins.get(pluginName);
if (!pluginManifest) {
return undefined;
}
const effectiveConfig = this._getPluginConfig(pluginManifest);
return effectiveConfig ? effectiveConfig[configKey] : undefined;
}
async _executeStaticPluginCommand(plugin) {
if (!plugin || plugin.pluginType !== 'static' || !plugin.entryPoint || !plugin.entryPoint.command) {
console.error(`[PluginManager] Invalid static plugin or command for execution: ${plugin ? plugin.name : 'Unknown'}`);
return Promise.reject(new Error(`Invalid static plugin or command for ${plugin ? plugin.name : 'Unknown'}`));
}
return new Promise((resolve, reject) => {
const pluginConfig = this._getPluginConfig(plugin);
const envForProcess = { ...process.env };
for (const key in pluginConfig) {
if (pluginConfig.hasOwnProperty(key) && pluginConfig[key] !== undefined) {
envForProcess[key] = String(pluginConfig[key]);
}
}
if (this.projectBasePath) { // Add projectBasePath for static plugins too if needed
envForProcess.PROJECT_BASE_PATH = this.projectBasePath;
}
const [command, ...args] = plugin.entryPoint.command.split(' ');
const pluginProcess = spawn(command, args, { cwd: plugin.basePath, shell: true, env: envForProcess, windowsHide: true });
let output = '';
let errorOutput = '';
let processExited = false;
const timeoutDuration = plugin.communication?.timeout || 30000;
const timeoutId = setTimeout(() => {
if (!processExited) {
console.error(`[PluginManager] Static plugin "${plugin.name}" execution timed out after ${timeoutDuration}ms.`); // Keep error
pluginProcess.kill('SIGKILL');
reject(new Error(`Static plugin "${plugin.name}" execution timed out.`));
}
}, timeoutDuration);
pluginProcess.stdout.on('data', (data) => { output += data.toString(); });
pluginProcess.stderr.on('data', (data) => { errorOutput += data.toString(); });
pluginProcess.on('error', (err) => {
processExited = true;
clearTimeout(timeoutId);
console.error(`[PluginManager] Failed to start static plugin ${plugin.name}: ${err.message}`);
reject(err);
});
pluginProcess.on('exit', (code, signal) => {
processExited = true;
clearTimeout(timeoutId);
if (signal === 'SIGKILL') {
return;
}
if (code !== 0) {
const errMsg = `Static plugin ${plugin.name} exited with code ${code}. Stderr: ${errorOutput.trim()}`;
console.error(`[PluginManager] ${errMsg}`);
reject(new Error(errMsg));
} else {
if (errorOutput.trim() && this.debugMode) {
console.warn(`[PluginManager] Static plugin ${plugin.name} produced stderr output: ${errorOutput.trim()}`);
}
resolve(output.trim());
}
});
});
}
async _updateStaticPluginValue(plugin) {
let newValue = null;
let executionError = null;
try {
if (this.debugMode) console.log(`[PluginManager] Updating static plugin: ${plugin.name}`);
newValue = await this._executeStaticPluginCommand(plugin);
} catch (error) {
console.error(`[PluginManager] Error executing static plugin ${plugin.name} script:`, error.message);
executionError = error;
}
if (plugin.capabilities && plugin.capabilities.systemPromptPlaceholders) {
plugin.capabilities.systemPromptPlaceholders.forEach(ph => {
const placeholderKey = ph.placeholder;
const currentValueEntry = this.staticPlaceholderValues.get(placeholderKey);
const currentValue = currentValueEntry ? currentValueEntry.value : undefined;
if (newValue !== null && newValue.trim() !== "") {
this.staticPlaceholderValues.set(placeholderKey, { value: newValue.trim(), serverId: 'local' });
if (this.debugMode) console.log(`[PluginManager] Placeholder ${placeholderKey} for ${plugin.name} updated with value: "${(newValue.trim()).substring(0,70)}..."`);
} else if (executionError) {
const errorMessage = `[Error updating ${plugin.name}: ${executionError.message.substring(0,100)}...]`;
if (!currentValue || (currentValue && currentValue.startsWith("[Error"))) {
this.staticPlaceholderValues.set(placeholderKey, { value: errorMessage, serverId: 'local' });
if (this.debugMode) console.warn(`[PluginManager] Placeholder ${placeholderKey} for ${plugin.name} set to error state: ${errorMessage}`);
} else {
if (this.debugMode) console.warn(`[PluginManager] Placeholder ${placeholderKey} for ${plugin.name} failed to update. Keeping stale value: "${(currentValue || "").substring(0,70)}..."`);
}
} else {
if (this.debugMode) console.warn(`[PluginManager] Static plugin ${plugin.name} produced no new output for ${placeholderKey}. Keeping stale value (if any).`);
if (!currentValueEntry) {
this.staticPlaceholderValues.set(placeholderKey, { value: `[${plugin.name} data currently unavailable]`, serverId: 'local' });
if (this.debugMode) console.log(`[PluginManager] Placeholder ${placeholderKey} for ${plugin.name} set to 'unavailable'.`);
}
}
});
}
}
async initializeStaticPlugins() {
console.log('[PluginManager] Initializing static plugins...');
for (const plugin of this.plugins.values()) {
if (plugin.pluginType === 'static') {
// Immediately set a "loading" state for the placeholder.
if (plugin.capabilities && plugin.capabilities.systemPromptPlaceholders) {
plugin.capabilities.systemPromptPlaceholders.forEach(ph => {
this.staticPlaceholderValues.set(ph.placeholder, { value: `[${plugin.displayName} a-zheng-zai-jia-zai-zhong... ]`, serverId: 'local' });
});
}
// Trigger the first update in the background (fire and forget).
this._updateStaticPluginValue(plugin).catch(err => {
console.error(`[PluginManager] Initial background update for ${plugin.name} failed: ${err.message}`);
});
// Set up the scheduled recurring updates.
if (plugin.refreshIntervalCron) {
if (this.scheduledJobs.has(plugin.name)) {
this.scheduledJobs.get(plugin.name).cancel();
}
try {
const job = schedule.scheduleJob(plugin.refreshIntervalCron, () => {
if (this.debugMode) console.log(`[PluginManager] Scheduled update for static plugin: ${plugin.name}`);
this._updateStaticPluginValue(plugin).catch(err => {
console.error(`[PluginManager] Scheduled background update for ${plugin.name} failed: ${err.message}`);
});
});
this.scheduledJobs.set(plugin.name, job);
if (this.debugMode) console.log(`[PluginManager] Scheduled ${plugin.name} with cron: ${plugin.refreshIntervalCron}`);
} catch (e) {
console.error(`[PluginManager] Invalid cron string for ${plugin.name}: ${plugin.refreshIntervalCron}. Error: ${e.message}`);
}
}
}
}
console.log('[PluginManager] Static plugins initialization process has been started (updates will run in the background).');
}
async prewarmPythonPlugins() {
console.log('[PluginManager] Checking for Python plugins to pre-warm...');
if (this.plugins.has('SciCalculator')) {
console.log('[PluginManager] SciCalculator found. Starting pre-warming of Python scientific libraries in the background.');
try {
const command = 'python';
const args = ['-c', 'import sympy, scipy.stats, scipy.integrate, numpy'];
const prewarmProcess = spawn(command, args, {
// 移除 shell: true
windowsHide: true
});
prewarmProcess.on('error', (err) => {
console.warn(`[PluginManager] Python pre-warming process failed to start. Is Python installed and in the system's PATH? Error: ${err.message}`);
});
prewarmProcess.stderr.on('data', (data) => {
console.warn(`[PluginManager] Python pre-warming process stderr: ${data.toString().trim()}`);
});
prewarmProcess.on('exit', (code) => {
if (code === 0) {
console.log('[PluginManager] Python scientific libraries pre-warmed successfully.');
} else {
console.warn(`[PluginManager] Python pre-warming process exited with code ${code}. Please ensure required libraries are installed (pip install sympy scipy numpy).`);
}
});
} catch (e) {
console.error(`[PluginManager] An exception occurred while spawning the Python pre-warming process: ${e.message}`);
}
} else {
if (this.debugMode) console.log('[PluginManager] SciCalculator not found, skipping Python pre-warming.');
}
}
getPlaceholderValue(placeholder) {
const entry = this.staticPlaceholderValues.get(placeholder);
return entry ? entry.value : `[Placeholder ${placeholder} not found]`;
}
async executeMessagePreprocessor(pluginName, messages) {
const processorModule = this.messagePreprocessors.get(pluginName);
const pluginManifest = this.plugins.get(pluginName);
if (!processorModule || !pluginManifest) {
console.error(`[PluginManager] Message preprocessor plugin "${pluginName}" not found.`);
return messages;
}
if (typeof processorModule.processMessages !== 'function') {
console.error(`[PluginManager] Plugin "${pluginName}" does not have 'processMessages' function.`);
return messages;
}
try {
if (this.debugMode) console.log(`[PluginManager] Executing message preprocessor: ${pluginName}`);
const pluginSpecificConfig = this._getPluginConfig(pluginManifest);
const processedMessages = await processorModule.processMessages(messages, pluginSpecificConfig);
if (this.debugMode) console.log(`[PluginManager] Message preprocessor ${pluginName} finished.`);
return processedMessages;
} catch (error) {
console.error(`[PluginManager] Error in message preprocessor ${pluginName}:`, error);
return messages;
}
}
async shutdownAllPlugins() {
console.log('[PluginManager] Shutting down all plugins...'); // Keep
// --- Shutdown VectorDBManager first to stop background processing ---
if (this.vectorDBManager && typeof this.vectorDBManager.shutdown === 'function') {
try {
if (this.debugMode) console.log('[PluginManager] Calling shutdown for VectorDBManager...');
await this.vectorDBManager.shutdown();
} catch (error) {
console.error('[PluginManager] Error during shutdown of VectorDBManager:', error);
}
}
for (const [name, pluginModuleData] of this.messagePreprocessors) {
const pluginModule = pluginModuleData.module || pluginModuleData;
if (pluginModule && typeof pluginModule.shutdown === 'function') {
try {
if (this.debugMode) console.log(`[PluginManager] Calling shutdown for ${name}...`);
await pluginModule.shutdown();
} catch (error) {
console.error(`[PluginManager] Error during shutdown of plugin ${name}:`, error); // Keep error
}
}
}
for (const [name, serviceData] of this.serviceModules) {
if (serviceData.module && typeof serviceData.module.shutdown === 'function') {
try {
if (this.debugMode) console.log(`[PluginManager] Calling shutdown for service plugin ${name}...`);
await serviceData.module.shutdown();
} catch (error) {
console.error(`[PluginManager] Error during shutdown of service plugin ${name}:`, error); // Keep error
}
}
}
for (const job of this.scheduledJobs.values()) {
job.cancel();
}
this.scheduledJobs.clear();
console.log('[PluginManager] All plugin shutdown processes initiated and scheduled jobs cancelled.'); // Keep
}
async loadPlugins() {
console.log('[PluginManager] Starting plugin discovery...');
// 1. 清理现有插件状态
const localPlugins = new Map();
for (const [name, manifest] of this.plugins.entries()) {
if (!manifest.isDistributed) {
localPlugins.set(name, manifest);
}
}
this.plugins = localPlugins;
this.messagePreprocessors.clear();
this.staticPlaceholderValues.clear();
this.serviceModules.clear();
const discoveredPreprocessors = new Map();
const modulesToInitialize = [];
try {
// 2. 发现并加载所有插件模块,但不初始化
const pluginFolders = await fs.readdir(PLUGIN_DIR, { withFileTypes: true });
for (const folder of pluginFolders) {
if (folder.isDirectory()) {
const pluginPath = path.join(PLUGIN_DIR, folder.name);
const manifestPath = path.join(pluginPath, manifestFileName);
try {
const manifestContent = await fs.readFile(manifestPath, 'utf-8');
const manifest = JSON.parse(manifestContent);
if (!manifest.name || !manifest.pluginType || !manifest.entryPoint) continue;
if (this.plugins.has(manifest.name)) continue;
manifest.basePath = pluginPath;
manifest.pluginSpecificEnvConfig = {};
try {
const pluginEnvContent = await fs.readFile(path.join(pluginPath, 'config.env'), 'utf-8');
manifest.pluginSpecificEnvConfig = dotenv.parse(pluginEnvContent);
} catch (envError) {
if (envError.code !== 'ENOENT') console.warn(`[PluginManager] Error reading config.env for ${manifest.name}:`, envError.message);
}
this.plugins.set(manifest.name, manifest);
console.log(`[PluginManager] Loaded manifest: ${manifest.displayName} (${manifest.name}, Type: ${manifest.pluginType})`);
const isPreprocessor = manifest.pluginType === 'messagePreprocessor' || manifest.pluginType === 'hybridservice';
const isService = manifest.pluginType === 'service' || manifest.pluginType === 'hybridservice';
if ((isPreprocessor || isService) && manifest.entryPoint.script && manifest.communication?.protocol === 'direct') {
try {
const scriptPath = path.join(pluginPath, manifest.entryPoint.script);
const module = require(scriptPath);
modulesToInitialize.push({ manifest, module });
if (isPreprocessor && typeof module.processMessages === 'function') {
discoveredPreprocessors.set(manifest.name, module);
}
if (isService) {
this.serviceModules.set(manifest.name, { manifest, module });
}
} catch (e) {
console.error(`[PluginManager] Error loading module for ${manifest.name}:`, e);
}
}
} catch (error) {
if (error.code !== 'ENOENT' && !(error instanceof SyntaxError)) {
console.error(`[PluginManager] Error loading plugin from ${folder.name}:`, error);
}
}
}
}
// 3. 确定预处理器加载顺序
const availablePlugins = new Set(discoveredPreprocessors.keys());
let finalOrder = [];
try {
const orderContent = await fs.readFile(PREPROCESSOR_ORDER_FILE, 'utf-8');
const savedOrder = JSON.parse(orderContent);
if (Array.isArray(savedOrder)) {
savedOrder.forEach(pluginName => {
if (availablePlugins.has(pluginName)) {
finalOrder.push(pluginName);
availablePlugins.delete(pluginName);
}
});
}
} catch (error) {
if (error.code !== 'ENOENT') console.error(`[PluginManager] Error reading existing ${PREPROCESSOR_ORDER_FILE}:`, error);
}
finalOrder.push(...Array.from(availablePlugins).sort());
// 4. 注册预处理器
for (const pluginName of finalOrder) {
this.messagePreprocessors.set(pluginName, discoveredPreprocessors.get(pluginName));
}
this.preprocessorOrder = finalOrder;
if (finalOrder.length > 0) console.log('[PluginManager] Final message preprocessor order: ' + finalOrder.join(' -> '));
// 5. 初始化共享服务 (VectorDBManager)
if (this.vectorDBManager) {
await this.vectorDBManager.initialize();
}
// 6. 按顺序初始化所有模块
const allModulesMap = new Map(modulesToInitialize.map(m => [m.manifest.name, m]));
const initializationOrder = [...this.preprocessorOrder];
allModulesMap.forEach((_, name) => {
if (!initializationOrder.includes(name)) {
initializationOrder.push(name);
}
});
for (const pluginName of initializationOrder) {
const item = allModulesMap.get(pluginName);
if (!item || typeof item.module.initialize !== 'function') continue;
const { manifest, module } = item;
try {
const initialConfig = this._getPluginConfig(manifest);
initialConfig.PORT = process.env.PORT;
initialConfig.Key = process.env.Key;
initialConfig.PROJECT_BASE_PATH = this.projectBasePath;
const dependencies = { vcpLogFunctions: this.getVCPLogFunctions() };
// --- 注入 VectorDBManager ---
if (manifest.name === 'RAGDiaryPlugin') {
dependencies.vectorDBManager = this.vectorDBManager;
}
// --- LightMemo 特殊依赖注入 ---
if (manifest.name === 'LightMemo') {
const ragPluginModule = this.messagePreprocessors.get('RAGDiaryPlugin');
if (ragPluginModule && ragPluginModule.vectorDBManager && typeof ragPluginModule.getSingleEmbedding === 'function') {
dependencies.vectorDBManager = ragPluginModule.vectorDBManager;
dependencies.getSingleEmbedding = ragPluginModule.getSingleEmbedding.bind(ragPluginModule);
if (this.debugMode) console.log(`[PluginManager] Injected VectorDBManager and getSingleEmbedding into LightMemo.`);
} else {
console.error(`[PluginManager] Critical dependency failure: RAGDiaryPlugin or its components not available for LightMemo injection.`);
}
}
// --- 注入结束 ---
await module.initialize(initialConfig, dependencies);
} catch (e) {
console.error(`[PluginManager] Error initializing module for ${manifest.name}:`, e);
}
}
this.buildVCPDescription();
console.log(`[PluginManager] Plugin discovery finished. Loaded ${this.plugins.size} plugins.`);
} catch (error) {
if (error.code === 'ENOENT') console.error(`[PluginManager] Plugin directory ${PLUGIN_DIR} not found.`);
else console.error('[PluginManager] Error reading plugin directory:', error);
}
}
buildVCPDescription() {
this.individualPluginDescriptions.clear(); // Clear previous descriptions
let overallLog = ['[PluginManager] Building individual VCP descriptions:'];
for (const plugin of this.plugins.values()) {
if (plugin.capabilities && plugin.capabilities.invocationCommands && plugin.capabilities.invocationCommands.length > 0) {
let pluginSpecificDescriptions = [];
plugin.capabilities.invocationCommands.forEach(cmd => {
if (cmd.description) {
let commandDescription = `- ${plugin.displayName} (${plugin.name}) - 命令: ${cmd.command || 'N/A'}:\n`; // Assuming cmd might have a 'command' field or similar identifier
const indentedCmdDescription = cmd.description.split('\n').map(line => ` ${line}`).join('\n');
commandDescription += `${indentedCmdDescription}`;
if (cmd.example) {
const exampleHeader = `\n 调用示例:\n`;
const indentedExample = cmd.example.split('\n').map(line => ` ${line}`).join('\n');
commandDescription += exampleHeader + indentedExample;
}
pluginSpecificDescriptions.push(commandDescription);
}
});
if (pluginSpecificDescriptions.length > 0) {
const placeholderKey = `VCP${plugin.name}`;
const fullDescriptionForPlugin = pluginSpecificDescriptions.join('\n\n');
this.individualPluginDescriptions.set(placeholderKey, fullDescriptionForPlugin);
overallLog.push(` - Generated description for {{${placeholderKey}}} (Length: ${fullDescriptionForPlugin.length})`);
}
}
}
if (this.individualPluginDescriptions.size === 0) {
overallLog.push(" - No VCP plugins with invocation commands found to generate descriptions for.");
}
if (this.debugMode) console.log(overallLog.join('\n'));
}
// New method to get all individual descriptions
getIndividualPluginDescriptions() {
return this.individualPluginDescriptions;
}
// getVCPDescription() { // This method is no longer needed as VCPDescription is deprecated
// return this.vcpDescription;
// }
getPlugin(name) {
return this.plugins.get(name);
}
getServiceModule(name) {
return this.serviceModules.get(name)?.module;
}
// 新增:获取 VCPLog 插件的推送函数,供其他插件依赖注入
getVCPLogFunctions() {
const vcpLogModule = this.getServiceModule('VCPLog');
if (vcpLogModule) {
return {
pushVcpLog: vcpLogModule.pushVcpLog,
pushVcpInfo: vcpLogModule.pushVcpInfo
};
}
return { pushVcpLog: () => {}, pushVcpInfo: () => {} };
}
async processToolCall(toolName, toolArgs, requestIp = null) {
const plugin = this.plugins.get(toolName);
if (!plugin) {
throw new Error(`[PluginManager] Plugin "${toolName}" not found for tool call.`);
}
// Helper function to generate a timestamp string
const _getFormattedLocalTimestamp = () => {
const date = new Date();
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
const hours = date.getHours().toString().padStart(2, '0');
const minutes = date.getMinutes().toString().padStart(2, '0');
const seconds = date.getSeconds().toString().padStart(2, '0');
const milliseconds = date.getMilliseconds().toString().padStart(3, '0');
const timezoneOffsetMinutes = date.getTimezoneOffset();
const offsetSign = timezoneOffsetMinutes > 0 ? "-" : "+";
const offsetHours = Math.abs(Math.floor(timezoneOffsetMinutes / 60)).toString().padStart(2, '0');
const offsetMinutes = Math.abs(timezoneOffsetMinutes % 60).toString().padStart(2, '0');
const timezoneString = `${offsetSign}${offsetHours}:${offsetMinutes}`;
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}.${milliseconds}${timezoneString}`;
};
const maidNameFromArgs = toolArgs && toolArgs.maid ? toolArgs.maid : null;
const pluginSpecificArgs = { ...toolArgs };
if (maidNameFromArgs) {
// The 'maid' parameter is intentionally passed through for plugins like DeepMemo.
// delete pluginSpecificArgs.maid;
}
try {
let resultFromPlugin;
if (plugin.isDistributed) {
// --- 分布式插件调用逻辑 ---
if (!this.webSocketServer) {
throw new Error('[PluginManager] WebSocketServer is not initialized. Cannot call distributed tool.');
}
if (this.debugMode) console.log(`[PluginManager] Processing distributed tool call for: ${toolName} on server ${plugin.serverId}`);
resultFromPlugin = await this.webSocketServer.executeDistributedTool(plugin.serverId, toolName, pluginSpecificArgs);
// 分布式工具的返回结果应该已经是JS对象了
} else if (toolName === 'ChromeControl' && plugin.communication?.protocol === 'direct') {
// --- ChromeControl 特殊处理逻辑 ---
if (!this.webSocketServer) {
throw new Error('[PluginManager] WebSocketServer is not initialized. Cannot call ChromeControl tool.');
}
if (this.debugMode) console.log(`[PluginManager] Processing direct WebSocket tool call for: ${toolName}`);
const command = pluginSpecificArgs.command;
delete pluginSpecificArgs.command;
resultFromPlugin = await this.webSocketServer.forwardCommandToChrome(command, pluginSpecificArgs);
} else if (plugin.pluginType === 'hybridservice' && plugin.communication?.protocol === 'direct') {
// --- 混合服务插件直接调用逻辑 ---
if (this.debugMode) console.log(`[PluginManager] Processing direct tool call for hybrid service: ${toolName}`);
const serviceModule = this.getServiceModule(toolName);
if (serviceModule && typeof serviceModule.processToolCall === 'function') {
resultFromPlugin = await serviceModule.processToolCall(pluginSpecificArgs);
} else {
throw new Error(`[PluginManager] Hybrid service plugin "${toolName}" does not have a processToolCall function.`);
}
} else {
// --- 本地插件调用逻辑 (现有逻辑) ---
if (!((plugin.pluginType === 'synchronous' || plugin.pluginType === 'asynchronous') && plugin.communication?.protocol === 'stdio')) {
throw new Error(`[PluginManager] Local plugin "${toolName}" (type: ${plugin.pluginType}) is not a supported stdio plugin for direct tool call.`);
}
let executionParam = null;
if (Object.keys(pluginSpecificArgs).length > 0) {
executionParam = JSON.stringify(pluginSpecificArgs);
}
const logParam = executionParam ? (executionParam.length > 100 ? executionParam.substring(0, 100) + '...' : executionParam) : null;
if (this.debugMode) console.log(`[PluginManager] Calling local executePlugin for: ${toolName} with prepared param:`, logParam);
const pluginOutput = await this.executePlugin(toolName, executionParam, requestIp); // Returns {status, result/error}
if (pluginOutput.status === "success") {
if (typeof pluginOutput.result === 'string') {
try {
// If the result is a string, try to parse it as JSON.
resultFromPlugin = JSON.parse(pluginOutput.result);
} catch (parseError) {
// If parsing fails, wrap it. This is for plugins that return plain text.
if (this.debugMode) console.warn(`[PluginManager] Local plugin ${toolName} result string was not valid JSON. Original: "${pluginOutput.result.substring(0, 100)}"`);
resultFromPlugin = { original_plugin_output: pluginOutput.result };
}
} else {
// If the result is already an object (as with our new image plugins), use it directly.
resultFromPlugin = pluginOutput.result;
}
} else {
// 检查是否是文件未找到的特定错误
if (pluginOutput.code === 'FILE_NOT_FOUND_LOCALLY' && pluginOutput.fileUrl && requestIp) {
if (this.debugMode) console.log(`[PluginManager] Plugin '${toolName}' reported local file not found. Attempting to fetch via FileFetcherServer...`);
try {
const { buffer, mimeType } = await FileFetcherServer.fetchFile(pluginOutput.fileUrl, requestIp);
const base64Data = buffer.toString('base64');
const dataUri = `data:${mimeType};base64,${base64Data}`;
if (this.debugMode) console.log(`[PluginManager] Successfully fetched file as data URI. Retrying plugin call...`);
// 新的重试逻辑:精确替换失败的参数
const newToolArgs = { ...toolArgs };
const failedParam = pluginOutput.failedParameter; // e.g., "image_url1"
if (failedParam && newToolArgs[failedParam]) {
// 删除旧的 file:// url 参数
delete newToolArgs[failedParam];
// 添加新的 base64 参数。我们使用一个新的键来避免命名冲突,
// 并且让插件知道这是一个已经处理过的 base64 数据。
// e.g., "image_base64_1"
// 关键修复:确保正确地从 "image_url_1" 提取出 "1"
const paramIndex = failedParam.replace('image_url_', '');
const newParamKey = `image_base64_${paramIndex}`;
newToolArgs[newParamKey] = dataUri;
if (this.debugMode) console.log(`[PluginManager] Retrying with '${failedParam}' replaced by '${newParamKey}'.`);
} else {
// 旧的后备逻辑,用于兼容单个 image_url 的情况
delete newToolArgs.image_url;
newToolArgs.image_base64 = dataUri;
if (this.debugMode) console.log(`[PluginManager] 'failedParameter' not specified. Falling back to replacing 'image_url' with 'image_base64'.`);
}
// 直接返回重试调用的结果
return await this.processToolCall(toolName, newToolArgs, requestIp);
} catch (fetchError) {
throw new Error(JSON.stringify({
plugin_error: `Plugin reported local file not found, but remote fetch failed: ${fetchError.message}`,
original_plugin_error: pluginOutput.error
}));
}
} else {
throw new Error(JSON.stringify({ plugin_error: pluginOutput.error || `Plugin "${toolName}" reported an unspecified error.` }));
}
}
}
// --- 通用结果处理 ---
let finalResultObject = (typeof resultFromPlugin === 'object' && resultFromPlugin !== null) ? resultFromPlugin : { original_plugin_output: resultFromPlugin };
if (maidNameFromArgs) {
finalResultObject.MaidName = maidNameFromArgs;
}
finalResultObject.timestamp = _getFormattedLocalTimestamp();
return finalResultObject;
} catch (e) {
console.error(`[PluginManager processToolCall] Error during execution for plugin ${toolName}:`, e.message);
let errorObject;
try {
errorObject = JSON.parse(e.message);
} catch (jsonParseError) {
errorObject = { plugin_execution_error: e.message || 'Unknown plugin execution error' };
}
if (maidNameFromArgs && !errorObject.MaidName) {
errorObject.MaidName = maidNameFromArgs;
}
if (!errorObject.timestamp) {
errorObject.timestamp = _getFormattedLocalTimestamp();
}
throw new Error(JSON.stringify(errorObject));
}
}
async executePlugin(pluginName, inputData, requestIp = null) {
const plugin = this.plugins.get(pluginName);
if (!plugin) {
// This case should ideally be caught by processToolCall before calling executePlugin
throw new Error(`[PluginManager executePlugin] Plugin "${pluginName}" not found.`);
}
// Validations for pluginType, communication, entryPoint remain important
if (!((plugin.pluginType === 'synchronous' || plugin.pluginType === 'asynchronous') && plugin.communication?.protocol === 'stdio')) {
throw new Error(`[PluginManager executePlugin] Plugin "${pluginName}" (type: ${plugin.pluginType}, protocol: ${plugin.communication?.protocol}) is not a supported stdio plugin. Expected synchronous or asynchronous stdio plugin.`);
}
if (!plugin.entryPoint || !plugin.entryPoint.command) {
throw new Error(`[PluginManager executePlugin] Entry point command undefined for plugin "${pluginName}".`);
}
const pluginConfig = this._getPluginConfig(plugin);
const envForProcess = { ...process.env };
for (const key in pluginConfig) {
if (pluginConfig.hasOwnProperty(key) && pluginConfig[key] !== undefined) {
envForProcess[key] = String(pluginConfig[key]);
}
}
const additionalEnv = {};
if (this.projectBasePath) {
additionalEnv.PROJECT_BASE_PATH = this.projectBasePath;
} else {
if (this.debugMode) console.warn("[PluginManager executePlugin] projectBasePath not set, PROJECT_BASE_PATH will not be available to plugins.");
}
// 如果插件需要管理员权限,则获取解密后的验证码并注入环境变量
if (plugin.requiresAdmin) {
const decryptedCode = await this._getDecryptedAuthCode();
if (decryptedCode) {
additionalEnv.DECRYPTED_AUTH_CODE = decryptedCode;
if (this.debugMode) console.log(`[PluginManager] Injected DECRYPTED_AUTH_CODE for admin-required plugin: ${pluginName}`);
} else {
if (this.debugMode) console.warn(`[PluginManager] Could not get decrypted auth code for admin-required plugin: ${pluginName}. Execution will proceed without it.`);
}
}
// 将 requestIp 添加到环境变量
if (requestIp) {
additionalEnv.VCP_REQUEST_IP = requestIp;
}
if (process.env.PORT) {
additionalEnv.SERVER_PORT = process.env.PORT;
}
const imageServerKey = this.getResolvedPluginConfigValue('ImageServer', 'Image_Key');
if (imageServerKey) {
additionalEnv.IMAGESERVER_IMAGE_KEY = imageServerKey;
}
// Pass CALLBACK_BASE_URL and PLUGIN_NAME to asynchronous plugins
if (plugin.pluginType === 'asynchronous') {
const callbackBaseUrl = pluginConfig.CALLBACK_BASE_URL || process.env.CALLBACK_BASE_URL; // Prefer plugin-specific, then global
if (callbackBaseUrl) {
additionalEnv.CALLBACK_BASE_URL = callbackBaseUrl;
} else {
if (this.debugMode) console.warn(`[PluginManager executePlugin] CALLBACK_BASE_URL not configured for asynchronous plugin ${pluginName}. Callback functionality might be impaired.`);
}
additionalEnv.PLUGIN_NAME_FOR_CALLBACK = pluginName; // Pass the plugin's name
}
// Force Python stdio encoding to UTF-8
additionalEnv.PYTHONIOENCODING = 'utf-8';
const finalEnv = { ...envForProcess, ...additionalEnv };
if (this.debugMode && plugin.pluginType === 'asynchronous') {
console.log(`[PluginManager executePlugin] Final ENV for async plugin ${pluginName}:`, JSON.stringify(finalEnv, null, 2).substring(0, 500) + "...");
}
return new Promise((resolve, reject) => {
if (this.debugMode) console.log(`[PluginManager executePlugin Internal] For plugin "${pluginName}", manifest entryPoint command is: "${plugin.entryPoint.command}"`);
const [command, ...args] = plugin.entryPoint.command.split(' ');
if (this.debugMode) console.log(`[PluginManager executePlugin Internal] Attempting to spawn command: "${command}" with args: [${args.join(', ')}] in cwd: ${plugin.basePath}`);
const pluginProcess = spawn(command, args, { cwd: plugin.basePath, shell: true, env: finalEnv, windowsHide: true });
let outputBuffer = ''; // Buffer to accumulate data chunks
let errorOutput = '';
let processExited = false;
let initialResponseSent = false; // Flag for async plugins
const isAsyncPlugin = plugin.pluginType === 'asynchronous';
const timeoutDuration = plugin.communication.timeout || (isAsyncPlugin ? 1800000 : 60000); // Use manifest timeout, or 30min for async, 1min for sync
const timeoutId = setTimeout(() => {
if (!processExited && !initialResponseSent && isAsyncPlugin) {
// For async, if initial response not sent by timeout, it's an error for that phase
console.error(`[PluginManager executePlugin Internal] Async plugin "${pluginName}" initial response timed out after ${timeoutDuration}ms.`);
pluginProcess.kill('SIGKILL'); // Kill if no initial response
reject(new Error(`Plugin "${pluginName}" initial response timed out.`));
} else if (!processExited && !isAsyncPlugin) {
// For sync plugins, or if async initial response was sent but process hangs
console.error(`[PluginManager executePlugin Internal] Plugin "${pluginName}" execution timed out after ${timeoutDuration}ms.`);
pluginProcess.kill('SIGKILL');
reject(new Error(`Plugin "${pluginName}" execution timed out.`));
} else if (!processExited && isAsyncPlugin && initialResponseSent) {
// Async plugin's initial response was sent, but the process is still running (e.g. for background tasks)
// We let it run, but log if it exceeds the overall timeout.
// The process will be managed by its own non-daemon threads.
if (this.debugMode) console.log(`[PluginManager executePlugin Internal] Async plugin "${pluginName}" process is still running in background after timeout. This is expected for non-daemon threads.`);
}
}, timeoutDuration);
pluginProcess.stdout.setEncoding('utf8');
pluginProcess.stdout.on('data', (data) => {
if (processExited || (isAsyncPlugin && initialResponseSent)) {
// If async and initial response sent, or process exited, ignore further stdout for this Promise.
// The plugin's background task might still log to its own stdout, but we don't collect it here.
if (this.debugMode && isAsyncPlugin && initialResponseSent) console.log(`[PluginManager executePlugin Internal] Async plugin ${pluginName} (initial response sent) produced more stdout: ${data.substring(0,100)}...`);
return;
}
outputBuffer += data;
try {
// Try to parse a complete JSON object from the buffer.
// This is a simple check; for robust streaming JSON, a more complex parser is needed.
// We assume the first complete JSON is the one we want for async initial response.
const potentialJsonMatch = outputBuffer.match(/(\{[\s\S]*?\})(?:\s|$)/);
if (potentialJsonMatch && potentialJsonMatch[1]) {
const jsonString = potentialJsonMatch[1];
const parsedOutput = JSON.parse(jsonString);
if (parsedOutput && (parsedOutput.status === "success" || parsedOutput.status === "error")) {
if (isAsyncPlugin) {
if (!initialResponseSent) {
if (this.debugMode) console.log(`[PluginManager executePlugin Internal] Async plugin "${pluginName}" sent initial JSON response. Resolving promise.`);
initialResponseSent = true;
// For async, we resolve with the first valid JSON and let the process continue if it has non-daemon threads.
// We don't clear the main timeout here for async, as the process might still need to be killed if it misbehaves badly later.
// However, the primary purpose of this promise is fulfilled.
resolve(parsedOutput);
// We don't return or clear outputBuffer here, as more data might be part of a *synchronous* plugin's single large JSON output.
}
} else { // Synchronous plugin
// For sync plugins, we wait for 'exit' to ensure all output is collected.
// This block within 'data' event is more for validating if the output *looks* like our expected JSON.
// The actual resolve for sync plugins happens in 'exit'.
if (this.debugMode) console.log(`[PluginManager executePlugin Internal] Sync plugin "${pluginName}" current output buffer contains a potential JSON.`);
}
}
}
} catch (e) {
// Incomplete JSON or invalid JSON, wait for more data or 'exit' event.
if (this.debugMode && outputBuffer.length > 2) console.log(`[PluginManager executePlugin Internal] Plugin "${pluginName}" stdout buffer not yet a complete JSON or invalid. Buffer: ${outputBuffer.substring(0,100)}...`);
}
});
pluginProcess.stderr.setEncoding('utf8');
pluginProcess.stderr.on('data', (data) => {
errorOutput += data;
if (this.debugMode) console.warn(`[PluginManager executePlugin Internal stderr] Plugin "${pluginName}": ${data.trim()}`);
});
pluginProcess.on('error', (err) => {
processExited = true; clearTimeout(timeoutId);
if (!initialResponseSent) { // Only reject if initial response (for async) or any response (for sync) hasn't been sent
reject(new Error(`Failed to start plugin "${pluginName}": ${err.message}`));
} else if (this.debugMode) {
console.error(`[PluginManager executePlugin Internal] Error after initial response for async plugin "${pluginName}": ${err.message}. Process might have been expected to continue.`);
}
});
pluginProcess.on('exit', (code, signal) => {
processExited = true;
clearTimeout(timeoutId); // Clear the main timeout once the process exits.
if (isAsyncPlugin && initialResponseSent) {
// For async plugins where initial response was already sent, log exit but don't re-resolve/reject.
if (this.debugMode) console.log(`[PluginManager executePlugin Internal] Async plugin "${pluginName}" process exited with code ${code}, signal ${signal} after initial response was sent.`);
return;
}
// If we are here, it's either a sync plugin, or an async plugin whose initial response was NOT sent before exit.
if (signal === 'SIGKILL') { // Typically means timeout killed it
if (!initialResponseSent) reject(new Error(`Plugin "${pluginName}" execution timed out or was killed.`));
return;
}
try {
const parsedOutput = JSON.parse(outputBuffer.trim()); // Use accumulated outputBuffer
if (parsedOutput && (parsedOutput.status === "success" || parsedOutput.status === "error")) {
if (code !== 0 && parsedOutput.status === "success" && this.debugMode) {
console.warn(`[PluginManager executePlugin Internal] Plugin "${pluginName}" exited with code ${code} but reported success in JSON. Trusting JSON.`);
}
if (code === 0 && parsedOutput.status === "error" && this.debugMode) {
console.warn(`[PluginManager executePlugin Internal] Plugin "${pluginName}" exited with code 0 but reported error in JSON. Trusting JSON.`);
}
if (errorOutput.trim()) parsedOutput.pluginStderr = errorOutput.trim();
if (!initialResponseSent) resolve(parsedOutput); // Ensure resolve only once
else if (this.debugMode) console.log(`[PluginManager executePlugin Internal] Plugin ${pluginName} exited, initial async response already sent.`);
return;
}
if (this.debugMode) console.warn(`[PluginManager executePlugin Internal] Plugin "${pluginName}" final stdout was not in the expected JSON format: ${outputBuffer.trim().substring(0,100)}`);
} catch (e) {
if (this.debugMode) console.warn(`[PluginManager executePlugin Internal] Failed to parse final stdout JSON from plugin "${pluginName}". Error: ${e.message}. Stdout: ${outputBuffer.trim().substring(0,100)}`);
}
if (!initialResponseSent) { // Only reject if no response has been sent yet
if (code !== 0) {
let detailedError = `Plugin "${pluginName}" exited with code ${code}.`;
if (outputBuffer.trim()) detailedError += ` Stdout: ${outputBuffer.trim().substring(0, 200)}`;
if (errorOutput.trim()) detailedError += ` Stderr: ${errorOutput.trim().substring(0, 200)}`;
reject(new Error(detailedError));
} else {
// Exit code 0, but no valid initial JSON response was sent/parsed.
reject(new Error(`Plugin "${pluginName}" exited successfully but did not provide a valid initial JSON response. Stdout: ${outputBuffer.trim().substring(0,200)}`));
}
}
});
try {
if (inputData !== undefined && inputData !== null) {
pluginProcess.stdin.write(inputData.toString());
}
pluginProcess.stdin.end();
} catch (e) {
console.error(`[PluginManager executePlugin Internal] Stdin write error for "${pluginName}": ${e.message}`);
if (!initialResponseSent) { // Only reject if no response has been sent yet
reject(new Error(`Stdin write error for "${pluginName}": ${e.message}`));
}
}
});
}
initializeServices(app, adminApiRouter, projectBasePath) {
if (!app) {
console.error('[PluginManager] Cannot initialize services without Express app instance.');
return;
}
if (!adminApiRouter) {
console.error('[PluginManager] Cannot initialize services without adminApiRouter instance.');
return;
}
if (!projectBasePath) {
console.error('[PluginManager] Cannot initialize services without projectBasePath.'); // Keep error
return;
}
console.log('[PluginManager] Initializing service plugins...'); // Keep
for (const [name, serviceData] of this.serviceModules) {
try {
const pluginConfig = this._getPluginConfig(serviceData.manifest);
const manifest = serviceData.manifest;
const module = serviceData.module;
// 新的、带命名空间的API路由注册机制
if (manifest.hasApiRoutes && typeof module.registerApiRoutes === 'function') {
if (this.debugMode) console.log(`[PluginManager] Registering namespaced API routes for service plugin: ${name}`);
const pluginRouter = express.Router();
// 将 router 和其他上下文传递给插件
module.registerApiRoutes(pluginRouter, pluginConfig, projectBasePath, this.webSocketServer);
// 统一挂载到带命名空间的前缀下
app.use(`/api/plugins/${name}`, pluginRouter);