Repository navigation
Expand file tree
/
Copy pathplugin-manager.js
More file actions
981 lines (887 loc) · 33.2 KB
/
Copy pathplugin-manager.js
File metadata and controls
981 lines (887 loc) · 33.2 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
/**
* OpenCode Go Plugin System - Microkernel Core (plugin-manager.js)
* =================================================================
* Baseline: Router v3.12.0+ / Architecture Spec v1.0.0
*
* Core Capabilities:
* 1. Multi-Slot Capabilities Contract (service, tools, agents, routes, ui)
* 2. Pre-Activation Topology Check (Ports, Hardware/VRAM, Namespaced Collision, Conflicts)
* 3. Dual-Track Client Dispatch (Master-MCP dynamic bus + ClientAdapter atomic injection)
* 4. Granular Client Scopes ([x] OpenCode, [x] OpenChamber, [x] Codex)
* 5. Windows Silent Daemon Management (windowsHide: true, AV-Immune compliance, taskkill /PID /T /F)
* 6. Local Plugins Scanning & Dynamic Hot-Reload
*/
'use strict';
const fs = require('fs');
const path = require('path');
const net = require('net');
const child_process = require('child_process');
const os = require('os');
// Helper: atomic JSON read/write
function parseJsonSafe(filePath, defaultVal = {}) {
try {
if (!fs.existsSync(filePath)) return defaultVal;
const raw = fs.readFileSync(filePath, 'utf8').replace(/^\uFEFF/, '');
if (!raw.trim()) return defaultVal;
return JSON.parse(raw);
} catch (_) {
return defaultVal;
}
}
function writeJsonAtomic(filePath, data) {
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
const content = JSON.stringify(data, null, 2);
const tempPath = `${filePath}.tmp.${Date.now()}.${Math.random().toString(36).slice(2, 7)}`;
try {
fs.writeFileSync(tempPath, content, 'utf8');
fs.renameSync(tempPath, filePath);
return true;
} catch (err) {
try { if (fs.existsSync(tempPath)) fs.unlinkSync(tempPath); } catch (_) {}
// Windows file lock (EBUSY / EPERM) resilience retry loop
for (let attempt = 0; attempt < 5; attempt++) {
try {
fs.writeFileSync(filePath, content, 'utf8');
return true;
} catch (writeErr) {
if ((writeErr.code === 'EBUSY' || writeErr.code === 'EPERM') && attempt < 4) {
const start = Date.now();
while (Date.now() - start < 20) {}
continue;
}
break;
}
}
return false;
}
}
class PluginManager {
constructor(options = {}) {
this.rootDir = options.rootDir || path.resolve(__dirname);
this.pluginsDir = options.pluginsDir || path.join(this.rootDir, 'plugins');
this.customPluginsDir = path.join(this.pluginsDir, 'custom');
this.configPath = options.configPath || path.join(this.rootDir, 'config.json');
this.routerPort = options.routerPort || 4010;
this.logger = options.logger || console;
// In-memory registries
this.discoveredPlugins = new Map(); // id -> Manifest object
this.loadedPlugins = new Map(); // id -> Plugin module instance
this.activeTools = new Map(); // toolName -> { pluginId, toolDef, handler }
this.activeServices = new Map(); // id -> { pid, mode, startedAt, healthEndpoint }
this.topologyDiagnostics = new Map();// id -> Preflight report
// Ensure directory exists
try {
if (!fs.existsSync(this.pluginsDir)) {
fs.mkdirSync(this.pluginsDir, { recursive: true });
}
if (!fs.existsSync(this.customPluginsDir)) {
fs.mkdirSync(this.customPluginsDir, { recursive: true });
}
} catch (_) {}
}
/**
* Load user configuration for plugins
*/
getPluginsConfig() {
const config = parseJsonSafe(this.configPath, {});
if (!config.plugins || typeof config.plugins !== 'object') {
config.plugins = {};
}
return config.plugins;
}
/**
* Persist plugin configuration atomically
*/
savePluginConfig(pluginId, pluginCfg) {
const config = parseJsonSafe(this.configPath, {});
if (!config.plugins || typeof config.plugins !== 'object') {
config.plugins = {};
}
config.plugins[pluginId] = Object.assign({}, config.plugins[pluginId] || {}, pluginCfg);
writeJsonAtomic(this.configPath, config);
return config.plugins[pluginId];
}
/**
* Validate standard plugin.json manifest
*/
validateManifest(rawManifest, dirPath) {
if (!rawManifest || typeof rawManifest !== 'object') {
throw new Error('Manifest 必须是有效的 JSON 对象');
}
const { id, name, version } = rawManifest;
if (!id || typeof id !== 'string' || !/^[a-z0-9_-]+$/i.test(id)) {
throw new Error(`插件 id 格式非法: "${id}" (仅允许英文字母、数字、短横线与下划线)`);
}
if (!name || typeof name !== 'string') {
throw new Error(`插件 "${id}" 缺少有效的 name 字段`);
}
if (!version || typeof version !== 'string') {
throw new Error(`插件 "${id}" 缺少有效的 version 字段`);
}
const entry = rawManifest.entry || 'index.js';
if (typeof entry !== 'string') {
throw new Error(`插件 "${id}" entry 必须为字符串`);
}
const isCrossPlatformUnsafePath = (p) => {
if (typeof p !== 'string' || !p.trim()) return true;
if (path.isAbsolute(p) || path.win32.isAbsolute(p) || path.posix.isAbsolute(p)) return true;
if (/^[a-zA-Z]:[\\/]|^[\\/]/.test(p)) return true;
const normPosix = path.posix.normalize(p.replace(/\\/g, '/'));
if (normPosix === '..' || normPosix.startsWith('../') || normPosix.includes('/../')) return true;
const resolved = path.resolve(dirPath, p.replace(/\\/g, path.sep));
const rel = path.relative(dirPath, resolved);
return rel.startsWith('..') || path.isAbsolute(rel);
};
if (isCrossPlatformUnsafePath(entry)) {
throw new Error(`插件 "${id}" 入口 entry ("${entry}") 试图越权逃逸出插件目录`);
}
const entryFullPath = path.resolve(dirPath, entry.replace(/\\/g, path.sep));
// Validate ui.cardComponent if specified
if (rawManifest.capabilities?.ui?.cardComponent) {
const cardComp = rawManifest.capabilities.ui.cardComponent;
if (typeof cardComp === 'string') {
if (isCrossPlatformUnsafePath(cardComp)) {
throw new Error(`插件 "${id}" UI 组件路径 ("${cardComp}") 试图越权逃逸出插件目录`);
}
}
}
// Normalize capabilities
const capabilities = Object.assign({
service: null,
tools: [],
agents: [],
routes: [],
ui: null
}, rawManifest.capabilities || {});
// Normalize system
const system = Object.assign({
platforms: ['win32', 'linux', 'darwin'],
requiresNode: '>=18.0.0',
ports: [],
hardware: { gpu: false, recommendedVramMb: 0, minVramMb: 0 },
conflictsWith: [],
requires: []
}, rawManifest.system || {});
// Normalize clientScopes
const clientScopes = Object.assign({
supported: ['opencode', 'openchamber', 'codex'],
defaultEnabled: ['opencode', 'codex']
}, rawManifest.clientScopes || {});
return {
...rawManifest,
id,
name,
version,
entry,
entryFullPath,
dirPath,
category: rawManifest.category || 'general',
capabilities,
system,
clientScopes
};
}
/**
* Determine whether a plugin is enabled (Single Source of Truth)
*/
isPluginEnabled(pluginId, manifest = null, pluginsCfg = null) {
const cfg = (pluginsCfg || this.getPluginsConfig())[pluginId];
if (cfg && cfg.enabled !== undefined) {
return Boolean(cfg.enabled);
}
const m = manifest || this.discoveredPlugins.get(pluginId);
if (!m) return false;
return Boolean(
(m.clientScopes?.defaultEnabled && m.clientScopes.defaultEnabled.length > 0) ||
m.category === 'service' ||
m.category === 'agent'
);
}
/**
* Scan plugins from plugins/ and plugins/custom/
*/
scanPlugins() {
this.discoveredPlugins.clear();
const searchDirs = [this.pluginsDir, this.customPluginsDir];
for (const baseDir of searchDirs) {
if (!fs.existsSync(baseDir)) continue;
let entries = [];
try {
entries = fs.readdirSync(baseDir, { withFileTypes: true });
} catch (_) {
continue;
}
for (const entry of entries) {
if (!entry.isDirectory()) continue;
if (entry.name === 'custom') continue; // Skip custom container itself
const pluginDir = path.join(baseDir, entry.name);
const manifestPath = path.join(pluginDir, 'plugin.json');
if (fs.existsSync(manifestPath)) {
try {
const raw = parseJsonSafe(manifestPath, null);
if (raw) {
const manifest = this.validateManifest(raw, pluginDir);
if (this.discoveredPlugins.has(manifest.id)) {
this.logger.warn(`[PluginManager] 发现重复插件 ID "${manifest.id}" (${pluginDir}),已跳过覆盖`);
} else {
this.discoveredPlugins.set(manifest.id, manifest);
}
}
} catch (err) {
this.logger.warn(`[PluginManager] 解析插件 "${entry.name}" manifest 失败: ${err.message}`);
}
}
}
}
return Array.from(this.discoveredPlugins.values());
}
/**
* Probe if a local TCP port is in use
*/
async _isPortInUse(port) {
const p = parseInt(port, 10);
if (isNaN(p) || p < 1 || p > 65535) return false;
return new Promise((resolve) => {
try {
const socket = new net.Socket();
socket.setTimeout(250);
socket.once('connect', () => {
socket.destroy();
resolve(true);
});
socket.once('timeout', () => {
socket.destroy();
resolve(false);
});
socket.once('error', () => {
socket.destroy();
resolve(false);
});
socket.connect(p, '127.0.0.1');
} catch (_) {
resolve(false);
}
});
}
/**
* Pre-flight Topology Check:
* 1. Port conflict probe (against routerPort, other enabled plugins, live TCP listening)
* 2. Hardware / VRAM budget evaluation
* 3. Tool collision check & auto-namespacing
* 4. Conflicts & Requirements check
*/
async checkTopology(pluginId, options = {}) {
const manifest = this.discoveredPlugins.get(pluginId);
if (!manifest) {
return { ok: false, errors: [`插件 "${pluginId}" 未被发现或不存在`], warnings: [] };
}
const errors = [];
const warnings = [];
const collisionFixes = {};
const pluginsCfg = this.getPluginsConfig();
// 1. Port Conflict Probe with robust number parsing
const rawPorts = [];
if (Array.isArray(manifest.system.ports)) {
rawPorts.push(...manifest.system.ports);
}
if (manifest.capabilities.service?.defaultPort) {
rawPorts.push(manifest.capabilities.service.defaultPort);
}
const ports = [];
for (const rawP of rawPorts) {
const pNum = parseInt(rawP, 10);
if (isNaN(pNum) || pNum < 1 || pNum > 65535) {
errors.push(`端口非法: 声明端口 "${rawP}" 无效 (必须为 1~65535 之间的有效端口号)`);
continue;
}
if (!ports.includes(pNum)) {
ports.push(pNum);
}
}
for (const port of ports) {
if (port === this.routerPort) {
errors.push(`端口冲突: 插件声明端口 ${port} 与智能网关核心端口重叠!`);
}
// Check against other enabled plugins
for (const [otherId, otherManifest] of this.discoveredPlugins.entries()) {
if (otherId === pluginId) continue;
const isOtherEnabled = this.isPluginEnabled(otherId, otherManifest, pluginsCfg);
if (!isOtherEnabled) continue;
const otherRawPorts = (otherManifest.system.ports || []).concat(otherManifest.capabilities.service?.defaultPort ? [otherManifest.capabilities.service.defaultPort] : []);
const otherPorts = otherRawPorts.map(p => parseInt(p, 10)).filter(p => !isNaN(p));
if (otherPorts.includes(port)) {
errors.push(`端口冲突: 端口 ${port} 已被启用的插件 "${otherManifest.name}" (${otherId}) 占用`);
}
}
// Live socket probe (if service is not already running by this plugin)
const currentService = this.activeServices.get(pluginId);
if (!currentService) {
try {
const inUse = await this._isPortInUse(port);
if (inUse) {
warnings.push(`端口已被外部服务占用: 端口 ${port} 当前已有监听服务响应。若为该插件的已有常驻实例,则可直接复用;若为其他程序请注意避让。`);
}
} catch (_) {}
}
}
// 2. Hardware / VRAM Budget
if (manifest.system.hardware?.gpu) {
const recVram = manifest.system.hardware.recommendedVramMb || 0;
const minVram = manifest.system.hardware.minVramMb || 0;
if (recVram > 4096) {
warnings.push(`高显存需求提示: 该插件推荐显存 ${recVram}MB (最低 ${minVram}MB),多插件并发时将自动进入显存排队互斥锁。`);
}
}
// 3. Tool Collision Check
const tools = manifest.capabilities.tools || [];
for (const tool of tools) {
const toolName = tool.name;
for (const [existingToolName, existingOwner] of this.activeTools.entries()) {
if (existingOwner.pluginId !== pluginId && existingToolName === toolName) {
const namespacedName = `${pluginId}_${toolName}`;
collisionFixes[toolName] = namespacedName;
warnings.push(`工具命名冲突: 工具名 "${toolName}" 已被插件 "${existingOwner.pluginId}" 注册,已自动安全映射为 "${namespacedName}" 隔离防撞车。`);
}
}
}
// 4. Conflicts & Requirements
if (Array.isArray(manifest.system.conflictsWith)) {
for (const confId of manifest.system.conflictsWith) {
const confManifest = this.discoveredPlugins.get(confId);
const isConfEnabled = this.isPluginEnabled(confId, confManifest, pluginsCfg);
if (isConfEnabled) {
errors.push(`互斥依赖冲突: 插件与已启用的 "${confId}" 存在互斥声明 (conflictsWith),不能同时激活!`);
}
}
}
if (Array.isArray(manifest.system.requires)) {
for (const reqId of manifest.system.requires) {
const reqManifest = this.discoveredPlugins.get(reqId);
const isReqEnabled = this.isPluginEnabled(reqId, reqManifest, pluginsCfg);
if (!isReqEnabled) {
errors.push(`缺失前置依赖: 插件需要 "${reqId}" 前置插件就绪,请先安装并启用该插件。`);
}
}
}
const report = {
ok: errors.length === 0,
errors,
warnings,
collisionFixes
};
this.topologyDiagnostics.set(pluginId, report);
return report;
}
/**
* Load plugin code and instantiate
*/
async loadPlugin(pluginId) {
let manifest = this.discoveredPlugins.get(pluginId);
if (!manifest) {
this.scanPlugins();
manifest = this.discoveredPlugins.get(pluginId);
}
if (!manifest) {
throw new Error(`未找到插件 "${pluginId}"`);
}
if (this.loadedPlugins.has(pluginId)) {
return this.loadedPlugins.get(pluginId);
}
let pluginModule = null;
if (fs.existsSync(manifest.entryFullPath)) {
try {
// Clear require cache for clean hot reload
delete require.cache[require.resolve(manifest.entryFullPath)];
pluginModule = require(manifest.entryFullPath);
} catch (err) {
this.logger.error(`[PluginManager] 加载插件 "${pluginId}" 入口失败: ${err.message}`);
throw err;
}
} else {
// Default virtual module if only manifest
pluginModule = {
async probe() { return { status: 'healthy', message: '仅清单模式运行' }; }
};
}
const context = {
pluginId,
manifest,
dir: manifest.dirPath,
rootDir: this.rootDir,
routerPort: this.routerPort,
logger: this.logger,
config: parseJsonSafe(this.configPath, {}),
registerTool: (toolDef, handler) => {
this.registerTool(pluginId, toolDef, handler);
}
};
if (typeof pluginModule.init === 'function') {
try {
await pluginModule.init(context);
} catch (err) {
this.unregisterTools(pluginId);
this.loadedPlugins.delete(pluginId);
this.logger.error(`[PluginManager] 插件 "${pluginId}" init() 钩子执行异常: ${err.message}`);
throw err;
}
}
this.loadedPlugins.set(pluginId, { module: pluginModule, context, manifest });
return this.loadedPlugins.get(pluginId);
}
/**
* Register a tool for a plugin
*/
registerTool(pluginId, toolDef, handler) {
if (!toolDef || !toolDef.name) return;
const topology = this.topologyDiagnostics.get(pluginId);
let finalName = toolDef.name;
if (topology && topology.collisionFixes && topology.collisionFixes[toolDef.name]) {
finalName = topology.collisionFixes[toolDef.name];
} else if (this.activeTools.has(finalName) && this.activeTools.get(finalName).pluginId !== pluginId) {
finalName = `${pluginId}_${toolDef.name}`;
}
// Rule 13: Probe parameters, inputSchema, input_schema; never let {} mask definitions; ensure { type: 'object', properties: {} }
let rawSchema = toolDef.parameters || toolDef.inputSchema || toolDef.input_schema;
if (!rawSchema || typeof rawSchema !== 'object' || Object.keys(rawSchema).length === 0) {
rawSchema = { type: 'object', properties: {} };
} else if (!rawSchema.type) {
rawSchema = { type: 'object', properties: {}, ...rawSchema };
}
this.activeTools.set(finalName, {
name: finalName,
originalName: toolDef.name,
pluginId,
description: toolDef.description || '',
parameters: rawSchema,
inputSchema: rawSchema,
handler
});
}
/**
* Unregister all tools belonging to a plugin
*/
unregisterTools(pluginId) {
for (const [name, entry] of this.activeTools.entries()) {
if (entry.pluginId === pluginId) {
this.activeTools.delete(name);
}
}
}
/**
* Enable a plugin with topology preflight check
*/
async enablePlugin(pluginId, options = {}) {
const topology = await this.checkTopology(pluginId, options);
if (!topology.ok && options.force !== true) {
return {
success: false,
pluginId,
errors: topology.errors,
warnings: topology.warnings
};
}
// Load plugin instance (calls init(context), which can dynamically register tools)
let instance = this.loadedPlugins.get(pluginId);
if (!instance) {
this.unregisterTools(pluginId);
instance = await this.loadPlugin(pluginId);
}
const manifest = instance.manifest;
// Register manifest declared tools IF not already registered by init(context)
if (Array.isArray(manifest.capabilities.tools)) {
for (const t of manifest.capabilities.tools) {
const mappedName = (topology.collisionFixes && topology.collisionFixes[t.name]) ? topology.collisionFixes[t.name] : t.name;
if (!this.activeTools.has(mappedName)) {
this.registerTool(pluginId, t, (args, ctx) => {
if (typeof instance.module.executeTool === 'function') {
return instance.module.executeTool(t.name, args, ctx);
}
throw new Error(`插件 "${pluginId}" 未实现 executeTool 钩子`);
});
}
}
}
// Determine client scopes
const pluginsCfg = this.getPluginsConfig();
const existing = pluginsCfg[pluginId] || {};
const clientScopes = options.clientScopes || existing.clients || {
opencode: manifest.clientScopes.defaultEnabled.includes('opencode'),
openchamber: manifest.clientScopes.defaultEnabled.includes('openchamber'),
codex: manifest.clientScopes.defaultEnabled.includes('codex')
};
// Save state atomically
if (options.persist !== false) {
this.savePluginConfig(pluginId, {
enabled: true,
clients: clientScopes,
lastEnabledAt: Date.now()
});
}
return {
success: true,
pluginId,
clientScopes,
warnings: topology.warnings
};
}
/**
* Disable a plugin cleanly
*/
async disablePlugin(pluginId) {
// 1. Stop daemon service if running
if (this.activeServices.has(pluginId)) {
try {
await this.stopService(pluginId);
} catch (_) {}
}
// 2. Call stop lifecycle hook if loaded
const instance = this.loadedPlugins.get(pluginId);
if (instance && typeof instance.module.stop === 'function') {
try {
await instance.module.stop(instance.context);
} catch (err) {
this.logger.warn(`[PluginManager] 插件 "${pluginId}" stop() 钩子报错: ${err.message}`);
}
}
// 3. Unregister tools
this.unregisterTools(pluginId);
this.loadedPlugins.delete(pluginId);
// 4. Save state atomically
this.savePluginConfig(pluginId, {
enabled: false,
lastDisabledAt: Date.now()
});
return {
success: true,
pluginId
};
}
/**
* Update client scopes for a plugin
*/
setClientScopes(pluginId, clientScopes) {
const pluginsCfg = this.getPluginsConfig();
const existing = pluginsCfg[pluginId] || { enabled: true };
const mergedScopes = Object.assign({
opencode: false,
openchamber: false,
codex: false
}, existing.clients || {}, clientScopes);
this.savePluginConfig(pluginId, {
clients: mergedScopes
});
return mergedScopes;
}
/**
* Execute an active tool
*/
async executeTool(toolName, args = {}, clientCaller = null) {
const safeArgs = (args && typeof args === 'object' && !Array.isArray(args)) ? args : {};
let toolEntry = this.activeTools.get(toolName);
if (!toolEntry) {
// Support clients that prefix tools with mcp__<server>__
const cleanName = (toolName || '').replace(/^mcp__[a-zA-Z0-9_-]+__/, '');
toolEntry = this.activeTools.get(cleanName);
}
if (!toolEntry) {
// Lazy auto-activation: if an enabled plugin declared this tool but has not loaded yet
const cleanName = (toolName || '').replace(/^mcp__[a-zA-Z0-9_-]+__/, '');
for (const [id, manifest] of this.discoveredPlugins.entries()) {
const declaredTools = manifest.capabilities?.tools || [];
if (declaredTools.some(t => t.name === toolName || t.name === cleanName)) {
if (this.isPluginEnabled(id, manifest)) {
await this.enablePlugin(id, { persist: false });
toolEntry = this.activeTools.get(toolName) || this.activeTools.get(cleanName);
break;
}
}
}
}
if (!toolEntry) {
throw new Error(`未找到工具 "${toolName}" 或其所属插件未启用`);
}
const { pluginId, originalName, handler } = toolEntry;
const pluginsCfg = this.getPluginsConfig();
const pluginCfg = pluginsCfg[pluginId];
const manifest = this.discoveredPlugins.get(pluginId);
if (!this.isPluginEnabled(pluginId, manifest, pluginsCfg)) {
throw new Error(`工具 "${toolName}" 所属插件 "${pluginId}" 已停用`);
}
// Client scope enforcement
if (clientCaller) {
if (manifest?.clientScopes?.supported && !manifest.clientScopes.supported.includes(clientCaller)) {
throw new Error(`工具 "${toolName}" 所属插件 "${pluginId}" 不支持客户端 "${clientCaller}"`);
}
if (pluginCfg && pluginCfg.clients && pluginCfg.clients[clientCaller] !== undefined) {
if (!pluginCfg.clients[clientCaller]) {
throw new Error(`工具 "${toolName}" 已在客户端 "${clientCaller}" 中被禁用`);
}
} else if (manifest && manifest.clientScopes?.defaultEnabled) {
if (!manifest.clientScopes.defaultEnabled.includes(clientCaller)) {
throw new Error(`工具 "${toolName}" 已在客户端 "${clientCaller}" 中被禁用`);
}
}
}
const instance = this.loadedPlugins.get(pluginId);
const context = instance ? instance.context : { pluginId, routerPort: this.routerPort };
if (typeof handler === 'function') {
return await handler(safeArgs, context);
} else if (instance && typeof instance.module.executeTool === 'function') {
return await instance.module.executeTool(originalName || toolName, safeArgs, context);
}
throw new Error(`插件 "${pluginId}" 无法处理工具调用 "${toolName}"`);
}
/**
* Get active tools filtered by client caller
*/
getActiveTools(clientCaller = null) {
const tools = [];
const pluginsCfg = this.getPluginsConfig();
for (const [name, entry] of this.activeTools.entries()) {
const { pluginId, description, parameters, inputSchema } = entry;
const pluginCfg = pluginsCfg[pluginId];
const manifest = this.discoveredPlugins.get(pluginId);
if (!this.isPluginEnabled(pluginId, manifest, pluginsCfg)) continue;
// Filter by clientCaller if specified
if (clientCaller) {
if (manifest?.clientScopes?.supported && !manifest.clientScopes.supported.includes(clientCaller)) {
continue;
}
if (pluginCfg && pluginCfg.clients && pluginCfg.clients[clientCaller] !== undefined) {
if (!pluginCfg.clients[clientCaller]) continue;
} else if (manifest && manifest.clientScopes?.defaultEnabled) {
if (!manifest.clientScopes.defaultEnabled.includes(clientCaller)) continue;
}
}
tools.push({
name,
description,
parameters: parameters || inputSchema,
inputSchema: inputSchema || parameters
});
}
return tools;
}
/**
* Determine if a PID belongs to an immune system, gateway, or Codex process
* Rigidly shields:
* 1. System idle & kernel processes (PID <= 4)
* 2. Gateway and parent processes (process.pid, process.ppid)
* 3. OpenAI Codex suite (codex.exe, codex-windows-sandbox-service, codex-computer-use-swift)
* 4. OpenCode & OpenChamber binaries
* 5. Static historical / test mock PIDs (25076)
*/
isProtectedPid(pid) {
const pidNum = parseInt(pid, 10);
if (isNaN(pidNum) || pidNum <= 4) return true;
if (pidNum === process.pid || pidNum === process.ppid) return true;
if (pidNum === 25076) return true;
// Dynamic inspection on Windows
if (process.platform === 'win32') {
try {
const out = child_process.execFileSync('tasklist', ['/FI', `PID eq ${pidNum}`, '/FO', 'CSV', '/NH'], {
encoding: 'utf8',
windowsHide: true,
stdio: ['ignore', 'pipe', 'ignore'],
timeout: 2500
});
if (out && typeof out === 'string') {
const lower = out.toLowerCase();
if (
lower.includes('codex') ||
lower.includes('opencode') ||
lower.includes('openchamber')
) {
return true;
}
// Secondary defense: if node.exe, inspect command line to protect gateway and sub-agents
if (lower.includes('node')) {
try {
const cmdline = child_process.execFileSync('powershell.exe', [
'-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass',
'-Command', `(Get-CimInstance Win32_Process -Filter "ProcessId = ${pidNum}" -ErrorAction SilentlyContinue).CommandLine`
], {
encoding: 'utf8',
windowsHide: true,
timeout: 2500,
stdio: ['ignore', 'pipe', 'ignore']
});
const cmdLower = (cmdline || '').toLowerCase();
if (
cmdLower.includes('server.js') ||
cmdLower.includes('opencode') ||
cmdLower.includes('mcp-master') ||
cmdLower.includes('openchamber')
) {
return true;
}
} catch (_) {}
}
}
} catch (_) {}
} else {
// Dynamic inspection on Linux / macOS
try {
const cmdlinePath = `/proc/${pidNum}/cmdline`;
const commPath = `/proc/${pidNum}/comm`;
let procInfo = '';
if (fs.existsSync(cmdlinePath)) {
procInfo += fs.readFileSync(cmdlinePath, 'utf8').toLowerCase();
}
if (fs.existsSync(commPath)) {
procInfo += fs.readFileSync(commPath, 'utf8').toLowerCase();
}
if (
procInfo.includes('codex') ||
procInfo.includes('opencode') ||
procInfo.includes('openchamber') ||
procInfo.includes('server.js') ||
procInfo.includes('mcp-master')
) {
return true;
}
} catch (_) {}
}
return false;
}
/**
* Windows Silent Service Daemon Launcher (AV-Immune Compliance)
* 1. Node.js native child_process.spawn
* 2. windowsHide: true, stdio: 'ignore', detached: true
* 3. Clear parameters array (no shell concatenation injection)
* 4. Safe PID recording
*/
async startService(pluginId, mode = 'gpu') {
let manifest = this.discoveredPlugins.get(pluginId);
if (!manifest) {
this.scanPlugins();
manifest = this.discoveredPlugins.get(pluginId);
}
if (!manifest) throw new Error(`插件 "${pluginId}" 不存在`);
const capService = manifest.capabilities.service;
if (!capService || !capService.daemonSupported) {
throw new Error(`插件 "${pluginId}" 未声明后台守护服务能力 (daemonSupported: false)`);
}
const instance = await this.loadPlugin(pluginId);
if (typeof instance.module.startService === 'function') {
const res = await instance.module.startService(mode, instance.context);
if (res && res.pid) {
const pidNum = parseInt(res.pid, 10);
if (!this.isProtectedPid(pidNum)) {
this.activeServices.set(pluginId, {
pid: pidNum,
mode,
startedAt: Date.now(),
healthEndpoint: capService.healthEndpoint
});
}
}
return res;
}
throw new Error(`插件 "${pluginId}" 入口未实现 startService(mode, context) 方法`);
}
/**
* Stop service daemon cleanly using standard taskkill
*/
async stopService(pluginId) {
// Red Line: Rigidly verify PID protection upfront before any termination attempt
const sInfo = this.activeServices.get(pluginId);
if (sInfo && sInfo.pid) {
const pidNum = parseInt(sInfo.pid, 10);
if (this.isProtectedPid(pidNum)) {
this.activeServices.delete(pluginId);
return { success: false, message: `受保护的关键进程或非法 PID (${sInfo.pid}),安全屏障已拒绝终止` };
}
}
const instance = this.loadedPlugins.get(pluginId);
if (instance && typeof instance.module.stopService === 'function') {
const res = await instance.module.stopService(instance.context);
this.activeServices.delete(pluginId);
return res;
}
if (sInfo && sInfo.pid) {
const pidNum = parseInt(sInfo.pid, 10);
if (process.platform === 'win32') {
try {
child_process.spawnSync('taskkill', ['/PID', String(pidNum), '/T', '/F'], {
windowsHide: true,
stdio: 'ignore'
});
} catch (_) {}
} else {
try {
process.kill(pidNum, 'SIGTERM');
} catch (_) {}
}
this.activeServices.delete(pluginId);
return { success: true, message: `服务进程 (PID: ${pidNum}) 已安全终止` };
}
return { success: true, message: '未检测到运行中的常驻服务' };
}
/**
* Probe service health
*/
async probeService(pluginId) {
const instance = this.loadedPlugins.get(pluginId);
if (instance && typeof instance.module.probe === 'function') {
return await instance.module.probe(instance.context);
}
const manifest = this.discoveredPlugins.get(pluginId);
if (manifest && manifest.capabilities.service?.healthEndpoint) {
const inUse = await this._isPortInUse(manifest.capabilities.service.defaultPort || 80);
return {
status: inUse ? 'healthy' : 'stopped',
endpoint: manifest.capabilities.service.healthEndpoint,
isOnline: inUse
};
}
return { status: 'healthy', message: '插件正常' };
}
/**
* List all discovered plugins with enriched status
*/
listPlugins() {
this.scanPlugins();
const pluginsCfg = this.getPluginsConfig();
const list = [];
for (const [id, manifest] of this.discoveredPlugins.entries()) {
const userCfg = pluginsCfg[id] || {};
const enabled = this.isPluginEnabled(id, manifest, pluginsCfg);
const clients = userCfg.clients || {
opencode: manifest.clientScopes.defaultEnabled.includes('opencode'),
openchamber: manifest.clientScopes.defaultEnabled.includes('openchamber'),
codex: manifest.clientScopes.defaultEnabled.includes('codex')
};
const service = this.activeServices.get(id) || null;
const diag = this.topologyDiagnostics.get(id) || null;
list.push({
id,
name: manifest.name,
version: manifest.version,
description: manifest.description,
author: manifest.author || 'unknown',
category: manifest.category,
enabled,
clients,
capabilities: manifest.capabilities,
system: manifest.system,
clientScopes: manifest.clientScopes,
service,
topology: diag,
toolCount: (manifest.capabilities.tools || []).length,
agentCount: (manifest.capabilities.agents || []).length
});
}
return list;
}
}
function isProtectedPid(pid) {
return PluginManager.prototype.isProtectedPid(pid);
}
module.exports = {
PluginManager,
isProtectedPid,
parseJsonSafe,
writeJsonAtomic
};