@juspay/neurolink 6.0.0 → 6.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -3
- package/dist/cli/commands/config.d.ts +13 -13
- package/dist/cli/index.js +3 -9
- package/dist/core/analytics.d.ts +11 -1
- package/dist/core/analytics.js +2 -2
- package/dist/core/base-provider.js +4 -18
- package/dist/core/dynamic-models.d.ts +8 -8
- package/dist/core/factory.js +3 -11
- package/dist/index.d.ts +2 -4
- package/dist/index.js +2 -11
- package/dist/lib/core/analytics.d.ts +11 -1
- package/dist/lib/core/analytics.js +2 -2
- package/dist/lib/core/base-provider.js +4 -18
- package/dist/lib/core/dynamic-models.d.ts +8 -8
- package/dist/lib/core/factory.js +3 -11
- package/dist/lib/index.d.ts +2 -4
- package/dist/lib/index.js +2 -11
- package/dist/lib/mcp/factory.d.ts +1 -1
- package/dist/lib/mcp/index.d.ts +7 -19
- package/dist/lib/mcp/index.js +15 -56
- package/dist/lib/neurolink.d.ts +1 -9
- package/dist/lib/neurolink.js +21 -70
- package/dist/lib/providers/amazon-bedrock.js +5 -16
- package/dist/lib/providers/anthropic-baseprovider.js +3 -21
- package/dist/lib/providers/anthropic.js +4 -7
- package/dist/lib/providers/azure-openai.js +4 -2
- package/dist/lib/providers/google-vertex.js +5 -9
- package/dist/lib/providers/huggingFace.js +5 -10
- package/dist/lib/providers/mistral.js +5 -8
- package/dist/lib/providers/openAI.js +4 -7
- package/dist/lib/utils/providerConfig.d.ts +117 -0
- package/dist/lib/utils/providerConfig.js +353 -0
- package/dist/lib/utils/providerUtils.d.ts +2 -1
- package/dist/lib/utils/providerUtils.js +53 -36
- package/dist/lib/utils/timeout.d.ts +72 -1
- package/dist/lib/utils/timeout.js +203 -2
- package/dist/mcp/factory.d.ts +1 -1
- package/dist/mcp/index.d.ts +7 -19
- package/dist/mcp/index.js +15 -56
- package/dist/neurolink.d.ts +1 -9
- package/dist/neurolink.js +21 -70
- package/dist/providers/amazon-bedrock.js +5 -16
- package/dist/providers/anthropic-baseprovider.js +3 -21
- package/dist/providers/anthropic.js +4 -7
- package/dist/providers/azure-openai.js +4 -2
- package/dist/providers/google-vertex.js +5 -9
- package/dist/providers/huggingFace.js +5 -10
- package/dist/providers/mistral.js +5 -8
- package/dist/providers/openAI.js +4 -7
- package/dist/utils/providerConfig.d.ts +117 -0
- package/dist/utils/providerConfig.js +353 -0
- package/dist/utils/providerUtils.d.ts +2 -1
- package/dist/utils/providerUtils.js +53 -36
- package/dist/utils/timeout.d.ts +72 -1
- package/dist/utils/timeout.js +203 -2
- package/package.json +2 -1
- package/dist/chat/client-utils.d.ts +0 -95
- package/dist/chat/client-utils.js +0 -315
- package/dist/chat/index.d.ts +0 -24
- package/dist/chat/index.js +0 -33
- package/dist/chat/session-storage.d.ts +0 -77
- package/dist/chat/session-storage.js +0 -233
- package/dist/chat/session.d.ts +0 -96
- package/dist/chat/session.js +0 -257
- package/dist/chat/sse-handler.d.ts +0 -49
- package/dist/chat/sse-handler.js +0 -259
- package/dist/chat/types.d.ts +0 -74
- package/dist/chat/types.js +0 -5
- package/dist/chat/websocket-chat-handler.d.ts +0 -37
- package/dist/chat/websocket-chat-handler.js +0 -262
- package/dist/cli/commands/mcp.d.ts +0 -20
- package/dist/cli/commands/mcp.js +0 -1272
- package/dist/core/defaults.d.ts +0 -19
- package/dist/core/defaults.js +0 -29
- package/dist/core/evaluation-config.d.ts +0 -29
- package/dist/core/evaluation-config.js +0 -144
- package/dist/factories/compatibility-factory.d.ts +0 -35
- package/dist/factories/compatibility-factory.js +0 -71
- package/dist/factories/provider-generate-factory.d.ts +0 -20
- package/dist/factories/provider-generate-factory.js +0 -93
- package/dist/lib/chat/client-utils.d.ts +0 -95
- package/dist/lib/chat/client-utils.js +0 -315
- package/dist/lib/chat/index.d.ts +0 -24
- package/dist/lib/chat/index.js +0 -33
- package/dist/lib/chat/session-storage.d.ts +0 -77
- package/dist/lib/chat/session-storage.js +0 -233
- package/dist/lib/chat/session.d.ts +0 -96
- package/dist/lib/chat/session.js +0 -257
- package/dist/lib/chat/sse-handler.d.ts +0 -49
- package/dist/lib/chat/sse-handler.js +0 -259
- package/dist/lib/chat/types.d.ts +0 -74
- package/dist/lib/chat/types.js +0 -5
- package/dist/lib/chat/websocket-chat-handler.d.ts +0 -37
- package/dist/lib/chat/websocket-chat-handler.js +0 -262
- package/dist/lib/core/defaults.d.ts +0 -19
- package/dist/lib/core/defaults.js +0 -29
- package/dist/lib/core/evaluation-config.d.ts +0 -29
- package/dist/lib/core/evaluation-config.js +0 -144
- package/dist/lib/factories/compatibility-factory.d.ts +0 -35
- package/dist/lib/factories/compatibility-factory.js +0 -71
- package/dist/lib/factories/provider-generate-factory.d.ts +0 -20
- package/dist/lib/factories/provider-generate-factory.js +0 -93
- package/dist/lib/mcp/adapters/plugin-bridge.d.ts +0 -40
- package/dist/lib/mcp/adapters/plugin-bridge.js +0 -89
- package/dist/lib/mcp/auto-discovery.d.ts +0 -62
- package/dist/lib/mcp/auto-discovery.js +0 -149
- package/dist/lib/mcp/client.d.ts +0 -68
- package/dist/lib/mcp/client.js +0 -248
- package/dist/lib/mcp/config.d.ts +0 -31
- package/dist/lib/mcp/config.js +0 -99
- package/dist/lib/mcp/context-manager.d.ts +0 -171
- package/dist/lib/mcp/context-manager.js +0 -362
- package/dist/lib/mcp/contracts/mcp-contract.d.ts +0 -169
- package/dist/lib/mcp/contracts/mcp-contract.js +0 -58
- package/dist/lib/mcp/core/plugin-manager.d.ts +0 -46
- package/dist/lib/mcp/core/plugin-manager.js +0 -110
- package/dist/lib/mcp/demo/plugin-demo.d.ts +0 -20
- package/dist/lib/mcp/demo/plugin-demo.js +0 -118
- package/dist/lib/mcp/dynamic-chain-executor.d.ts +0 -225
- package/dist/lib/mcp/dynamic-chain-executor.js +0 -489
- package/dist/lib/mcp/dynamic-orchestrator.d.ts +0 -115
- package/dist/lib/mcp/dynamic-orchestrator.js +0 -351
- package/dist/lib/mcp/ecosystem.d.ts +0 -75
- package/dist/lib/mcp/ecosystem.js +0 -161
- package/dist/lib/mcp/error-manager.d.ts +0 -254
- package/dist/lib/mcp/error-manager.js +0 -501
- package/dist/lib/mcp/error-recovery.d.ts +0 -159
- package/dist/lib/mcp/error-recovery.js +0 -405
- package/dist/lib/mcp/external-client.d.ts +0 -88
- package/dist/lib/mcp/external-client.js +0 -331
- package/dist/lib/mcp/external-manager.d.ts +0 -112
- package/dist/lib/mcp/external-manager.js +0 -308
- package/dist/lib/mcp/function-calling.d.ts +0 -65
- package/dist/lib/mcp/function-calling.js +0 -642
- package/dist/lib/mcp/health-monitor.d.ts +0 -257
- package/dist/lib/mcp/health-monitor.js +0 -630
- package/dist/lib/mcp/initialize-tools.d.ts +0 -29
- package/dist/lib/mcp/initialize-tools.js +0 -261
- package/dist/lib/mcp/initialize.d.ts +0 -18
- package/dist/lib/mcp/initialize.js +0 -62
- package/dist/lib/mcp/manager.d.ts +0 -68
- package/dist/lib/mcp/manager.js +0 -176
- package/dist/lib/mcp/neurolink-mcp-client.d.ts +0 -97
- package/dist/lib/mcp/neurolink-mcp-client.js +0 -462
- package/dist/lib/mcp/orchestrator.d.ts +0 -302
- package/dist/lib/mcp/orchestrator.js +0 -703
- package/dist/lib/mcp/plugin-manager.d.ts +0 -98
- package/dist/lib/mcp/plugin-manager.js +0 -296
- package/dist/lib/mcp/plugins/core/filesystem-mcp.d.ts +0 -36
- package/dist/lib/mcp/plugins/core/filesystem-mcp.js +0 -142
- package/dist/lib/mcp/plugins/filesystem-mcp.d.ts +0 -37
- package/dist/lib/mcp/plugins/filesystem-mcp.js +0 -54
- package/dist/lib/mcp/security-manager.d.ts +0 -87
- package/dist/lib/mcp/security-manager.js +0 -344
- package/dist/lib/mcp/semaphore-manager.d.ts +0 -137
- package/dist/lib/mcp/semaphore-manager.js +0 -329
- package/dist/lib/mcp/session-manager.d.ts +0 -187
- package/dist/lib/mcp/session-manager.js +0 -400
- package/dist/lib/mcp/session-persistence.d.ts +0 -93
- package/dist/lib/mcp/session-persistence.js +0 -301
- package/dist/lib/mcp/tool-integration.d.ts +0 -58
- package/dist/lib/mcp/tool-integration.js +0 -203
- package/dist/lib/mcp/transport-manager.d.ts +0 -154
- package/dist/lib/mcp/transport-manager.js +0 -334
- package/dist/lib/mcp/unified-mcp.d.ts +0 -133
- package/dist/lib/mcp/unified-mcp.js +0 -251
- package/dist/lib/mcp/unified-registry.d.ts +0 -165
- package/dist/lib/mcp/unified-registry.js +0 -538
- package/dist/lib/providers/analytics-helper.d.ts +0 -38
- package/dist/lib/providers/analytics-helper.js +0 -216
- package/dist/lib/providers/function-calling-provider.d.ts +0 -142
- package/dist/lib/providers/function-calling-provider.js +0 -630
- package/dist/lib/providers/mcp-provider.d.ts +0 -75
- package/dist/lib/providers/mcp-provider.js +0 -283
- package/dist/lib/providers/timeout-wrapper.d.ts +0 -40
- package/dist/lib/providers/timeout-wrapper.js +0 -100
- package/dist/lib/sdk/tool-extension.d.ts +0 -181
- package/dist/lib/sdk/tool-extension.js +0 -284
- package/dist/lib/services/streaming/streaming-manager.d.ts +0 -29
- package/dist/lib/services/streaming/streaming-manager.js +0 -245
- package/dist/lib/services/types.d.ts +0 -156
- package/dist/lib/services/types.js +0 -2
- package/dist/lib/services/websocket/websocket-server.d.ts +0 -34
- package/dist/lib/services/websocket/websocket-server.js +0 -305
- package/dist/lib/utils/provider-validation.d.ts +0 -36
- package/dist/lib/utils/provider-validation.js +0 -625
- package/dist/lib/utils/providerUtils-fixed.d.ts +0 -8
- package/dist/lib/utils/providerUtils-fixed.js +0 -94
- package/dist/lib/utils/streaming-utils.d.ts +0 -79
- package/dist/lib/utils/streaming-utils.js +0 -198
- package/dist/lib/utils/timeout-manager.d.ts +0 -75
- package/dist/lib/utils/timeout-manager.js +0 -244
- package/dist/mcp/adapters/plugin-bridge.d.ts +0 -40
- package/dist/mcp/adapters/plugin-bridge.js +0 -89
- package/dist/mcp/auto-discovery.d.ts +0 -62
- package/dist/mcp/auto-discovery.js +0 -149
- package/dist/mcp/client.d.ts +0 -68
- package/dist/mcp/client.js +0 -248
- package/dist/mcp/config.d.ts +0 -31
- package/dist/mcp/config.js +0 -99
- package/dist/mcp/context-manager.d.ts +0 -171
- package/dist/mcp/context-manager.js +0 -362
- package/dist/mcp/contracts/mcp-contract.d.ts +0 -169
- package/dist/mcp/contracts/mcp-contract.js +0 -58
- package/dist/mcp/core/plugin-manager.d.ts +0 -46
- package/dist/mcp/core/plugin-manager.js +0 -110
- package/dist/mcp/demo/plugin-demo.d.ts +0 -20
- package/dist/mcp/demo/plugin-demo.js +0 -118
- package/dist/mcp/dynamic-chain-executor.d.ts +0 -225
- package/dist/mcp/dynamic-chain-executor.js +0 -489
- package/dist/mcp/dynamic-orchestrator.d.ts +0 -115
- package/dist/mcp/dynamic-orchestrator.js +0 -351
- package/dist/mcp/ecosystem.d.ts +0 -75
- package/dist/mcp/ecosystem.js +0 -162
- package/dist/mcp/error-manager.d.ts +0 -254
- package/dist/mcp/error-manager.js +0 -501
- package/dist/mcp/error-recovery.d.ts +0 -159
- package/dist/mcp/error-recovery.js +0 -405
- package/dist/mcp/external-client.d.ts +0 -88
- package/dist/mcp/external-client.js +0 -331
- package/dist/mcp/external-manager.d.ts +0 -112
- package/dist/mcp/external-manager.js +0 -308
- package/dist/mcp/function-calling.d.ts +0 -65
- package/dist/mcp/function-calling.js +0 -642
- package/dist/mcp/health-monitor.d.ts +0 -257
- package/dist/mcp/health-monitor.js +0 -630
- package/dist/mcp/initialize-tools.d.ts +0 -29
- package/dist/mcp/initialize-tools.js +0 -262
- package/dist/mcp/initialize.d.ts +0 -18
- package/dist/mcp/initialize.js +0 -62
- package/dist/mcp/manager.d.ts +0 -68
- package/dist/mcp/manager.js +0 -176
- package/dist/mcp/neurolink-mcp-client.d.ts +0 -97
- package/dist/mcp/neurolink-mcp-client.js +0 -462
- package/dist/mcp/orchestrator.d.ts +0 -302
- package/dist/mcp/orchestrator.js +0 -703
- package/dist/mcp/plugin-manager.d.ts +0 -98
- package/dist/mcp/plugin-manager.js +0 -297
- package/dist/mcp/plugins/core/filesystem-mcp.d.ts +0 -36
- package/dist/mcp/plugins/core/filesystem-mcp.js +0 -142
- package/dist/mcp/plugins/core/neurolink-mcp.json +0 -17
- package/dist/mcp/plugins/filesystem-mcp.d.ts +0 -37
- package/dist/mcp/plugins/filesystem-mcp.js +0 -54
- package/dist/mcp/security-manager.d.ts +0 -87
- package/dist/mcp/security-manager.js +0 -344
- package/dist/mcp/semaphore-manager.d.ts +0 -137
- package/dist/mcp/semaphore-manager.js +0 -329
- package/dist/mcp/session-manager.d.ts +0 -187
- package/dist/mcp/session-manager.js +0 -400
- package/dist/mcp/session-persistence.d.ts +0 -93
- package/dist/mcp/session-persistence.js +0 -302
- package/dist/mcp/tool-integration.d.ts +0 -58
- package/dist/mcp/tool-integration.js +0 -203
- package/dist/mcp/transport-manager.d.ts +0 -154
- package/dist/mcp/transport-manager.js +0 -335
- package/dist/mcp/unified-mcp.d.ts +0 -133
- package/dist/mcp/unified-mcp.js +0 -251
- package/dist/mcp/unified-registry.d.ts +0 -165
- package/dist/mcp/unified-registry.js +0 -539
- package/dist/providers/analytics-helper.d.ts +0 -38
- package/dist/providers/analytics-helper.js +0 -216
- package/dist/providers/function-calling-provider.d.ts +0 -142
- package/dist/providers/function-calling-provider.js +0 -630
- package/dist/providers/mcp-provider.d.ts +0 -75
- package/dist/providers/mcp-provider.js +0 -283
- package/dist/providers/timeout-wrapper.d.ts +0 -40
- package/dist/providers/timeout-wrapper.js +0 -100
- package/dist/sdk/tool-extension.d.ts +0 -181
- package/dist/sdk/tool-extension.js +0 -284
- package/dist/services/streaming/streaming-manager.d.ts +0 -29
- package/dist/services/streaming/streaming-manager.js +0 -245
- package/dist/services/types.d.ts +0 -156
- package/dist/services/types.js +0 -2
- package/dist/services/websocket/websocket-server.d.ts +0 -34
- package/dist/services/websocket/websocket-server.js +0 -306
- package/dist/utils/provider-validation.d.ts +0 -36
- package/dist/utils/provider-validation.js +0 -625
- package/dist/utils/providerUtils-fixed.d.ts +0 -8
- package/dist/utils/providerUtils-fixed.js +0 -94
- package/dist/utils/streaming-utils.d.ts +0 -79
- package/dist/utils/streaming-utils.js +0 -198
- package/dist/utils/timeout-manager.d.ts +0 -75
- package/dist/utils/timeout-manager.js +0 -244
|
@@ -1,329 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* NeuroLink MCP Semaphore Manager
|
|
3
|
-
* Prevents race conditions in concurrent tool executions using a robust semaphore pattern
|
|
4
|
-
* Based on proven patterns from 1MCP reference implementation
|
|
5
|
-
*/
|
|
6
|
-
/**
|
|
7
|
-
* Semaphore Manager for concurrent operation control
|
|
8
|
-
* Implements the proven semaphore pattern from 1MCP to prevent race conditions
|
|
9
|
-
*/
|
|
10
|
-
export class SemaphoreManager {
|
|
11
|
-
locks = new Map();
|
|
12
|
-
queues = new Map();
|
|
13
|
-
stats = new Map();
|
|
14
|
-
globalStats = {
|
|
15
|
-
activeOperations: 0,
|
|
16
|
-
queuedOperations: 0,
|
|
17
|
-
totalOperations: 0,
|
|
18
|
-
totalWaitTime: 0,
|
|
19
|
-
averageWaitTime: 0,
|
|
20
|
-
peakQueueDepth: 0,
|
|
21
|
-
lastActivity: Date.now(),
|
|
22
|
-
};
|
|
23
|
-
/**
|
|
24
|
-
* Acquire a semaphore and execute an operation
|
|
25
|
-
* Ensures exclusive access to resources identified by the key
|
|
26
|
-
*
|
|
27
|
-
* @param key Unique identifier for the resource
|
|
28
|
-
* @param operation Async operation to execute with exclusive access
|
|
29
|
-
* @param context Optional execution context for enhanced tracking
|
|
30
|
-
* @returns Result of the operation with timing metrics
|
|
31
|
-
*/
|
|
32
|
-
async acquire(key, operation, context) {
|
|
33
|
-
const startTime = Date.now();
|
|
34
|
-
let waitTime = 0;
|
|
35
|
-
let executionTime = 0;
|
|
36
|
-
let queueDepth = 0;
|
|
37
|
-
// Get or create queue for this key
|
|
38
|
-
const queue = this.queues.get(key) || [];
|
|
39
|
-
queueDepth = queue.length;
|
|
40
|
-
// Check if there's an active lock
|
|
41
|
-
const existingLock = this.locks.get(key);
|
|
42
|
-
if (existingLock) {
|
|
43
|
-
// Add to queue and wait
|
|
44
|
-
queueDepth++;
|
|
45
|
-
this.updateQueueDepth(key, queueDepth);
|
|
46
|
-
const waitPromise = new Promise((resolve, reject) => {
|
|
47
|
-
queue.push({ resolve, reject });
|
|
48
|
-
this.queues.set(key, queue);
|
|
49
|
-
});
|
|
50
|
-
if (process.env.NEUROLINK_DEBUG === "true") {
|
|
51
|
-
console.log(`[Semaphore] Operation waiting in queue for key: ${key} (depth: ${queueDepth})`);
|
|
52
|
-
}
|
|
53
|
-
// Wait for existing lock and our turn in queue
|
|
54
|
-
await existingLock;
|
|
55
|
-
await waitPromise;
|
|
56
|
-
waitTime = Date.now() - startTime;
|
|
57
|
-
}
|
|
58
|
-
// Create new lock for this operation
|
|
59
|
-
let lockResolve;
|
|
60
|
-
const lockPromise = new Promise((resolve) => {
|
|
61
|
-
lockResolve = resolve;
|
|
62
|
-
});
|
|
63
|
-
this.locks.set(key, lockPromise);
|
|
64
|
-
// Update statistics
|
|
65
|
-
this.incrementActiveOperations(key);
|
|
66
|
-
// Execute the operation
|
|
67
|
-
const executionStartTime = Date.now();
|
|
68
|
-
if (process.env.NEUROLINK_DEBUG === "true") {
|
|
69
|
-
console.log(`[Semaphore] Executing operation for key: ${key}`);
|
|
70
|
-
}
|
|
71
|
-
try {
|
|
72
|
-
const result = await operation();
|
|
73
|
-
executionTime = Math.max(1, Date.now() - executionStartTime); // Ensure at least 1ms
|
|
74
|
-
// Update statistics
|
|
75
|
-
this.updateStats(key, waitTime, executionTime);
|
|
76
|
-
if (process.env.NEUROLINK_DEBUG === "true") {
|
|
77
|
-
console.log(`[Semaphore] Operation completed successfully for key: ${key}`);
|
|
78
|
-
}
|
|
79
|
-
return {
|
|
80
|
-
success: true,
|
|
81
|
-
result,
|
|
82
|
-
waitTime,
|
|
83
|
-
executionTime,
|
|
84
|
-
queueDepth,
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
catch (error) {
|
|
88
|
-
executionTime = Math.max(1, Date.now() - executionStartTime); // Ensure at least 1ms
|
|
89
|
-
const errorObj = error instanceof Error ? error : new Error(String(error));
|
|
90
|
-
// Update statistics even for errors
|
|
91
|
-
this.updateStats(key, waitTime, executionTime);
|
|
92
|
-
if (process.env.NEUROLINK_DEBUG === "true") {
|
|
93
|
-
console.error(`[Semaphore] Operation failed for key: ${key}`, errorObj.message);
|
|
94
|
-
}
|
|
95
|
-
return {
|
|
96
|
-
success: false,
|
|
97
|
-
error: errorObj,
|
|
98
|
-
waitTime,
|
|
99
|
-
executionTime,
|
|
100
|
-
queueDepth,
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
finally {
|
|
104
|
-
// Release the lock
|
|
105
|
-
this.locks.delete(key);
|
|
106
|
-
if (lockResolve) {
|
|
107
|
-
lockResolve();
|
|
108
|
-
}
|
|
109
|
-
// Process queue
|
|
110
|
-
const queue = this.queues.get(key) || [];
|
|
111
|
-
if (queue.length > 0) {
|
|
112
|
-
const next = queue.shift();
|
|
113
|
-
if (queue.length === 0) {
|
|
114
|
-
this.queues.delete(key);
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
this.queues.set(key, queue);
|
|
118
|
-
}
|
|
119
|
-
// Allow next operation to proceed
|
|
120
|
-
next.resolve();
|
|
121
|
-
}
|
|
122
|
-
// Update statistics
|
|
123
|
-
this.decrementActiveOperations(key);
|
|
124
|
-
if (process.env.NEUROLINK_DEBUG === "true") {
|
|
125
|
-
console.log(`[Semaphore] Released lock for key: ${key}`);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* Try to acquire a semaphore without waiting
|
|
131
|
-
* Returns immediately if the resource is locked
|
|
132
|
-
*
|
|
133
|
-
* @param key Unique identifier for the resource
|
|
134
|
-
* @param operation Async operation to execute if lock is available
|
|
135
|
-
* @param context Optional execution context
|
|
136
|
-
* @returns Result of the operation or null if resource is locked
|
|
137
|
-
*/
|
|
138
|
-
async tryAcquire(key, operation, context) {
|
|
139
|
-
// Check if there's an active lock or queue
|
|
140
|
-
if (this.locks.has(key) || (this.queues.get(key) || []).length > 0) {
|
|
141
|
-
if (process.env.NEUROLINK_DEBUG === "true") {
|
|
142
|
-
console.log(`[Semaphore] tryAcquire failed - resource locked: ${key}`);
|
|
143
|
-
}
|
|
144
|
-
return null;
|
|
145
|
-
}
|
|
146
|
-
// No lock, proceed with normal acquire
|
|
147
|
-
return this.acquire(key, operation, context);
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Check if a resource is currently locked
|
|
151
|
-
*
|
|
152
|
-
* @param key Resource identifier
|
|
153
|
-
* @returns True if the resource is locked
|
|
154
|
-
*/
|
|
155
|
-
isLocked(key) {
|
|
156
|
-
return this.locks.has(key);
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Get the current queue depth for a resource
|
|
160
|
-
*
|
|
161
|
-
* @param key Resource identifier
|
|
162
|
-
* @returns Number of operations waiting for this resource
|
|
163
|
-
*/
|
|
164
|
-
getQueueDepth(key) {
|
|
165
|
-
const queue = this.queues.get(key) || [];
|
|
166
|
-
const hasLock = this.locks.has(key);
|
|
167
|
-
return queue.length + (hasLock ? 1 : 0);
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* Get statistics for a specific resource or global stats
|
|
171
|
-
*
|
|
172
|
-
* @param key Optional resource identifier
|
|
173
|
-
* @returns Semaphore statistics
|
|
174
|
-
*/
|
|
175
|
-
getStats(key) {
|
|
176
|
-
if (key) {
|
|
177
|
-
return (this.stats.get(key) || {
|
|
178
|
-
activeOperations: 0,
|
|
179
|
-
queuedOperations: 0,
|
|
180
|
-
totalOperations: 0,
|
|
181
|
-
totalWaitTime: 0,
|
|
182
|
-
averageWaitTime: 0,
|
|
183
|
-
peakQueueDepth: 0,
|
|
184
|
-
lastActivity: 0,
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
return { ...this.globalStats };
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* Clear all semaphores (use with caution)
|
|
191
|
-
* This will reject all pending operations
|
|
192
|
-
*/
|
|
193
|
-
clearAll() {
|
|
194
|
-
console.warn("[Semaphore] Clearing all semaphores - pending operations will be rejected");
|
|
195
|
-
// Reject all queued operations
|
|
196
|
-
for (const [key, queue] of this.queues) {
|
|
197
|
-
for (const op of queue) {
|
|
198
|
-
op.reject(new Error("Semaphore cleared"));
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
// Clear all data structures
|
|
202
|
-
this.locks.clear();
|
|
203
|
-
this.queues.clear();
|
|
204
|
-
this.stats.clear();
|
|
205
|
-
// Reset global stats
|
|
206
|
-
this.globalStats = {
|
|
207
|
-
activeOperations: 0,
|
|
208
|
-
queuedOperations: 0,
|
|
209
|
-
totalOperations: this.globalStats.totalOperations,
|
|
210
|
-
totalWaitTime: this.globalStats.totalWaitTime,
|
|
211
|
-
averageWaitTime: this.globalStats.averageWaitTime,
|
|
212
|
-
peakQueueDepth: this.globalStats.peakQueueDepth,
|
|
213
|
-
lastActivity: Date.now(),
|
|
214
|
-
};
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* Update queue depth statistics
|
|
218
|
-
*
|
|
219
|
-
* @private
|
|
220
|
-
*/
|
|
221
|
-
updateQueueDepth(key, depth) {
|
|
222
|
-
const keyStats = this.stats.get(key) || this.createEmptyStats();
|
|
223
|
-
if (depth > keyStats.peakQueueDepth) {
|
|
224
|
-
keyStats.peakQueueDepth = depth;
|
|
225
|
-
}
|
|
226
|
-
keyStats.queuedOperations = depth;
|
|
227
|
-
this.stats.set(key, keyStats);
|
|
228
|
-
// Update global stats
|
|
229
|
-
this.globalStats.queuedOperations = Array.from(this.stats.values()).reduce((total, stats) => total + stats.queuedOperations, 0);
|
|
230
|
-
// Update global peak
|
|
231
|
-
if (depth > this.globalStats.peakQueueDepth) {
|
|
232
|
-
this.globalStats.peakQueueDepth = depth;
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
/**
|
|
236
|
-
* Increment active operations counter
|
|
237
|
-
*
|
|
238
|
-
* @private
|
|
239
|
-
*/
|
|
240
|
-
incrementActiveOperations(key) {
|
|
241
|
-
const keyStats = this.stats.get(key) || this.createEmptyStats();
|
|
242
|
-
keyStats.activeOperations++;
|
|
243
|
-
keyStats.totalOperations++;
|
|
244
|
-
keyStats.lastActivity = Date.now();
|
|
245
|
-
this.stats.set(key, keyStats);
|
|
246
|
-
this.globalStats.activeOperations++;
|
|
247
|
-
this.globalStats.totalOperations++;
|
|
248
|
-
this.globalStats.lastActivity = Date.now();
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* Decrement active operations counter
|
|
252
|
-
*
|
|
253
|
-
* @private
|
|
254
|
-
*/
|
|
255
|
-
decrementActiveOperations(key) {
|
|
256
|
-
const keyStats = this.stats.get(key);
|
|
257
|
-
if (keyStats && keyStats.activeOperations > 0) {
|
|
258
|
-
keyStats.activeOperations--;
|
|
259
|
-
keyStats.lastActivity = Date.now();
|
|
260
|
-
}
|
|
261
|
-
if (this.globalStats.activeOperations > 0) {
|
|
262
|
-
this.globalStats.activeOperations--;
|
|
263
|
-
}
|
|
264
|
-
this.globalStats.lastActivity = Date.now();
|
|
265
|
-
}
|
|
266
|
-
/**
|
|
267
|
-
* Update timing statistics
|
|
268
|
-
*
|
|
269
|
-
* @private
|
|
270
|
-
*/
|
|
271
|
-
updateStats(key, waitTime, executionTime) {
|
|
272
|
-
const keyStats = this.stats.get(key) || this.createEmptyStats();
|
|
273
|
-
keyStats.totalWaitTime += waitTime;
|
|
274
|
-
keyStats.averageWaitTime =
|
|
275
|
-
keyStats.totalOperations > 0
|
|
276
|
-
? keyStats.totalWaitTime / keyStats.totalOperations
|
|
277
|
-
: 0;
|
|
278
|
-
keyStats.lastActivity = Date.now();
|
|
279
|
-
this.stats.set(key, keyStats);
|
|
280
|
-
// Update global stats
|
|
281
|
-
this.globalStats.totalWaitTime += waitTime;
|
|
282
|
-
this.globalStats.averageWaitTime =
|
|
283
|
-
this.globalStats.totalOperations > 0
|
|
284
|
-
? this.globalStats.totalWaitTime / this.globalStats.totalOperations
|
|
285
|
-
: 0;
|
|
286
|
-
}
|
|
287
|
-
/**
|
|
288
|
-
* Create empty statistics object
|
|
289
|
-
*
|
|
290
|
-
* @private
|
|
291
|
-
*/
|
|
292
|
-
createEmptyStats() {
|
|
293
|
-
return {
|
|
294
|
-
activeOperations: 0,
|
|
295
|
-
queuedOperations: 0,
|
|
296
|
-
totalOperations: 0,
|
|
297
|
-
totalWaitTime: 0,
|
|
298
|
-
averageWaitTime: 0,
|
|
299
|
-
peakQueueDepth: 0,
|
|
300
|
-
lastActivity: Date.now(),
|
|
301
|
-
};
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
/**
|
|
305
|
-
* Default semaphore manager instance
|
|
306
|
-
*/
|
|
307
|
-
export const defaultSemaphoreManager = new SemaphoreManager();
|
|
308
|
-
/**
|
|
309
|
-
* Utility function to acquire semaphore with default manager
|
|
310
|
-
*
|
|
311
|
-
* @param key Resource identifier
|
|
312
|
-
* @param operation Operation to execute
|
|
313
|
-
* @param context Optional execution context
|
|
314
|
-
* @returns Operation result with metrics
|
|
315
|
-
*/
|
|
316
|
-
export async function acquireSemaphore(key, operation, context) {
|
|
317
|
-
return defaultSemaphoreManager.acquire(key, operation, context);
|
|
318
|
-
}
|
|
319
|
-
/**
|
|
320
|
-
* Utility function to try acquiring semaphore without waiting
|
|
321
|
-
*
|
|
322
|
-
* @param key Resource identifier
|
|
323
|
-
* @param operation Operation to execute
|
|
324
|
-
* @param context Optional execution context
|
|
325
|
-
* @returns Operation result or null if locked
|
|
326
|
-
*/
|
|
327
|
-
export async function tryAcquireSemaphore(key, operation, context) {
|
|
328
|
-
return defaultSemaphoreManager.tryAcquire(key, operation, context);
|
|
329
|
-
}
|
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* NeuroLink MCP Session Management System
|
|
3
|
-
* Enables continuous tool calling with persistent state across executions
|
|
4
|
-
* Based on patterns from Cline's session management implementation
|
|
5
|
-
*/
|
|
6
|
-
import type { Unknown } from "../types/common.js";
|
|
7
|
-
import type { NeuroLinkExecutionContext, ToolResult } from "./factory.js";
|
|
8
|
-
import { type PersistenceOptions } from "./session-persistence.js";
|
|
9
|
-
/**
|
|
10
|
-
* Session state for orchestrator operations
|
|
11
|
-
*/
|
|
12
|
-
export interface OrchestratorSession {
|
|
13
|
-
id: string;
|
|
14
|
-
context: NeuroLinkExecutionContext;
|
|
15
|
-
toolHistory: ToolResult[];
|
|
16
|
-
state: Map<string, Unknown>;
|
|
17
|
-
metadata: {
|
|
18
|
-
userAgent?: string;
|
|
19
|
-
origin?: string;
|
|
20
|
-
tags?: string[];
|
|
21
|
-
};
|
|
22
|
-
createdAt: number;
|
|
23
|
-
lastActivity: number;
|
|
24
|
-
expiresAt: number;
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Session creation options
|
|
28
|
-
*/
|
|
29
|
-
export interface SessionOptions {
|
|
30
|
-
ttl?: number;
|
|
31
|
-
metadata?: {
|
|
32
|
-
userAgent?: string;
|
|
33
|
-
origin?: string;
|
|
34
|
-
tags?: string[];
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Session statistics
|
|
39
|
-
*/
|
|
40
|
-
export interface SessionStats {
|
|
41
|
-
activeSessions: number;
|
|
42
|
-
totalSessionsCreated: number;
|
|
43
|
-
totalSessionsExpired: number;
|
|
44
|
-
averageSessionDuration: number;
|
|
45
|
-
averageToolsPerSession: number;
|
|
46
|
-
peakConcurrentSessions: number;
|
|
47
|
-
lastCleanup: number;
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Session Manager for maintaining state across tool executions
|
|
51
|
-
* Implements session lifecycle management with automatic cleanup
|
|
52
|
-
*/
|
|
53
|
-
export declare class SessionManager {
|
|
54
|
-
private sessions;
|
|
55
|
-
private sessionCounter;
|
|
56
|
-
private stats;
|
|
57
|
-
private cleanupInterval;
|
|
58
|
-
private defaultTTL;
|
|
59
|
-
private cleanupIntervalMs;
|
|
60
|
-
private persistence;
|
|
61
|
-
private persistenceEnabled;
|
|
62
|
-
constructor(defaultTTL?: number, cleanupIntervalMs?: number, autoCleanup?: boolean, enablePersistence?: boolean);
|
|
63
|
-
/**
|
|
64
|
-
* Initialize session manager with persistence
|
|
65
|
-
*/
|
|
66
|
-
initialize(persistenceOptions?: PersistenceOptions): Promise<void>;
|
|
67
|
-
/**
|
|
68
|
-
* Create a new session
|
|
69
|
-
*
|
|
70
|
-
* @param context Execution context for the session
|
|
71
|
-
* @param options Session configuration options
|
|
72
|
-
* @returns Created session
|
|
73
|
-
*/
|
|
74
|
-
createSession(context: NeuroLinkExecutionContext, options?: SessionOptions): OrchestratorSession;
|
|
75
|
-
/**
|
|
76
|
-
* Get an existing session
|
|
77
|
-
*
|
|
78
|
-
* @param sessionId Session identifier
|
|
79
|
-
* @param extend Whether to extend the session's expiration
|
|
80
|
-
* @returns Session if found and not expired
|
|
81
|
-
*/
|
|
82
|
-
getSession(sessionId: string, extend?: boolean): OrchestratorSession | null;
|
|
83
|
-
/**
|
|
84
|
-
* Update session with new tool result
|
|
85
|
-
*
|
|
86
|
-
* @param sessionId Session identifier
|
|
87
|
-
* @param toolResult Result from tool execution
|
|
88
|
-
* @returns Updated session or null if not found
|
|
89
|
-
*/
|
|
90
|
-
updateSession(sessionId: string, toolResult: ToolResult): OrchestratorSession | null;
|
|
91
|
-
/**
|
|
92
|
-
* Set session state value
|
|
93
|
-
*
|
|
94
|
-
* @param sessionId Session identifier
|
|
95
|
-
* @param key State key
|
|
96
|
-
* @param value State value
|
|
97
|
-
* @returns Updated session or null if not found
|
|
98
|
-
*/
|
|
99
|
-
setSessionState(sessionId: string, key: string, value: Unknown): OrchestratorSession | null;
|
|
100
|
-
/**
|
|
101
|
-
* Get session state value
|
|
102
|
-
*
|
|
103
|
-
* @param sessionId Session identifier
|
|
104
|
-
* @param key State key
|
|
105
|
-
* @returns State value or undefined
|
|
106
|
-
*/
|
|
107
|
-
getSessionState(sessionId: string, key: string): Unknown;
|
|
108
|
-
/**
|
|
109
|
-
* Remove a session
|
|
110
|
-
*
|
|
111
|
-
* @param sessionId Session identifier
|
|
112
|
-
* @returns True if session was removed
|
|
113
|
-
*/
|
|
114
|
-
removeSession(sessionId: string): boolean;
|
|
115
|
-
/**
|
|
116
|
-
* Clean up expired sessions
|
|
117
|
-
*
|
|
118
|
-
* @returns Number of sessions cleaned up
|
|
119
|
-
*/
|
|
120
|
-
cleanup(): Promise<number>;
|
|
121
|
-
/**
|
|
122
|
-
* Get an existing session with async persistence loading
|
|
123
|
-
*
|
|
124
|
-
* @param sessionId Session identifier
|
|
125
|
-
* @param extend Whether to extend the session's expiration
|
|
126
|
-
* @returns Session if found and not expired, or null
|
|
127
|
-
*/
|
|
128
|
-
getSessionAsync(sessionId: string, extend?: boolean): Promise<OrchestratorSession | null>;
|
|
129
|
-
/**
|
|
130
|
-
* Get all active sessions
|
|
131
|
-
*
|
|
132
|
-
* @returns Array of active sessions
|
|
133
|
-
*/
|
|
134
|
-
getActiveSessions(): Promise<OrchestratorSession[]>;
|
|
135
|
-
/**
|
|
136
|
-
* Get session statistics
|
|
137
|
-
*
|
|
138
|
-
* @returns Session statistics
|
|
139
|
-
*/
|
|
140
|
-
getStats(): SessionStats;
|
|
141
|
-
/**
|
|
142
|
-
* Clear all sessions
|
|
143
|
-
*/
|
|
144
|
-
clearAll(): void;
|
|
145
|
-
/**
|
|
146
|
-
* Start automatic cleanup interval
|
|
147
|
-
*
|
|
148
|
-
* @private
|
|
149
|
-
*/
|
|
150
|
-
private startAutoCleanup;
|
|
151
|
-
/**
|
|
152
|
-
* Stop automatic cleanup interval
|
|
153
|
-
*/
|
|
154
|
-
stopAutoCleanup(): void;
|
|
155
|
-
/**
|
|
156
|
-
* Generate unique session ID
|
|
157
|
-
*
|
|
158
|
-
* @private
|
|
159
|
-
*/
|
|
160
|
-
private generateSessionId;
|
|
161
|
-
/**
|
|
162
|
-
* Update statistics
|
|
163
|
-
*
|
|
164
|
-
* @private
|
|
165
|
-
*/
|
|
166
|
-
private updateStats;
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* Default session manager instance
|
|
170
|
-
*/
|
|
171
|
-
export declare const defaultSessionManager: SessionManager;
|
|
172
|
-
/**
|
|
173
|
-
* Utility function to create session with default manager
|
|
174
|
-
*
|
|
175
|
-
* @param context Execution context
|
|
176
|
-
* @param options Session options
|
|
177
|
-
* @returns Created session
|
|
178
|
-
*/
|
|
179
|
-
export declare function createSession(context: NeuroLinkExecutionContext, options?: SessionOptions): Promise<OrchestratorSession>;
|
|
180
|
-
/**
|
|
181
|
-
* Utility function to get session with default manager
|
|
182
|
-
*
|
|
183
|
-
* @param sessionId Session identifier
|
|
184
|
-
* @param extend Whether to extend expiration
|
|
185
|
-
* @returns Session or null
|
|
186
|
-
*/
|
|
187
|
-
export declare function getSession(sessionId: string, extend?: boolean): Promise<OrchestratorSession | null>;
|