@juspay/neurolink 6.1.0 → 6.2.1

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.
Files changed (303) hide show
  1. package/CHANGELOG.md +16 -6
  2. package/dist/agent/direct-tools.js +6 -5
  3. package/dist/cli/commands/config.d.ts +13 -13
  4. package/dist/cli/index.js +3 -9
  5. package/dist/config/configManager.js +11 -10
  6. package/dist/core/analytics.d.ts +11 -1
  7. package/dist/core/analytics.js +2 -2
  8. package/dist/core/base-provider.js +4 -18
  9. package/dist/core/dynamic-models.d.ts +8 -8
  10. package/dist/core/factory.js +3 -11
  11. package/dist/index.d.ts +2 -4
  12. package/dist/index.js +2 -11
  13. package/dist/lib/agent/direct-tools.js +6 -5
  14. package/dist/lib/config/configManager.js +11 -10
  15. package/dist/lib/core/analytics.d.ts +11 -1
  16. package/dist/lib/core/analytics.js +2 -2
  17. package/dist/lib/core/base-provider.js +4 -18
  18. package/dist/lib/core/dynamic-models.d.ts +8 -8
  19. package/dist/lib/core/factory.js +3 -11
  20. package/dist/lib/index.d.ts +2 -4
  21. package/dist/lib/index.js +2 -11
  22. package/dist/lib/mcp/factory.d.ts +1 -1
  23. package/dist/lib/mcp/index.d.ts +9 -21
  24. package/dist/lib/mcp/index.js +16 -57
  25. package/dist/lib/mcp/registry.js +1 -1
  26. package/dist/lib/mcp/servers/ai-providers/ai-analysis-tools.js +2 -1
  27. package/dist/lib/mcp/tool-registry.js +1 -1
  28. package/dist/lib/neurolink.d.ts +1 -9
  29. package/dist/lib/neurolink.js +22 -71
  30. package/dist/lib/providers/amazon-bedrock.js +5 -16
  31. package/dist/lib/providers/anthropic-baseprovider.js +3 -21
  32. package/dist/lib/providers/anthropic.js +4 -7
  33. package/dist/lib/providers/azure-openai.js +6 -3
  34. package/dist/lib/providers/google-vertex.js +5 -9
  35. package/dist/lib/providers/huggingFace.js +5 -10
  36. package/dist/lib/providers/mistral.js +5 -8
  37. package/dist/lib/providers/openAI.js +4 -7
  38. package/dist/lib/telemetry/index.js +2 -1
  39. package/dist/lib/utils/logger.d.ts +62 -2
  40. package/dist/lib/utils/logger.js +174 -9
  41. package/dist/lib/utils/providerConfig.d.ts +117 -0
  42. package/dist/lib/utils/providerConfig.js +353 -0
  43. package/dist/lib/utils/providerUtils.d.ts +2 -1
  44. package/dist/lib/utils/providerUtils.js +53 -36
  45. package/dist/lib/utils/timeout.d.ts +72 -1
  46. package/dist/lib/utils/timeout.js +203 -2
  47. package/dist/mcp/factory.d.ts +1 -1
  48. package/dist/mcp/index.d.ts +9 -21
  49. package/dist/mcp/index.js +16 -57
  50. package/dist/mcp/registry.js +1 -1
  51. package/dist/mcp/servers/ai-providers/ai-analysis-tools.js +2 -1
  52. package/dist/mcp/tool-registry.js +1 -1
  53. package/dist/neurolink.d.ts +1 -9
  54. package/dist/neurolink.js +22 -71
  55. package/dist/providers/amazon-bedrock.js +5 -16
  56. package/dist/providers/anthropic-baseprovider.js +3 -21
  57. package/dist/providers/anthropic.js +4 -7
  58. package/dist/providers/azure-openai.js +6 -3
  59. package/dist/providers/google-vertex.js +5 -9
  60. package/dist/providers/huggingFace.js +5 -10
  61. package/dist/providers/mistral.js +5 -8
  62. package/dist/providers/openAI.js +4 -7
  63. package/dist/telemetry/index.js +2 -1
  64. package/dist/utils/logger.d.ts +62 -2
  65. package/dist/utils/logger.js +174 -9
  66. package/dist/utils/providerConfig.d.ts +117 -0
  67. package/dist/utils/providerConfig.js +353 -0
  68. package/dist/utils/providerUtils.d.ts +2 -1
  69. package/dist/utils/providerUtils.js +53 -36
  70. package/dist/utils/timeout.d.ts +72 -1
  71. package/dist/utils/timeout.js +203 -2
  72. package/package.json +1 -1
  73. package/dist/chat/client-utils.d.ts +0 -95
  74. package/dist/chat/client-utils.js +0 -315
  75. package/dist/chat/index.d.ts +0 -24
  76. package/dist/chat/index.js +0 -33
  77. package/dist/chat/session-storage.d.ts +0 -77
  78. package/dist/chat/session-storage.js +0 -233
  79. package/dist/chat/session.d.ts +0 -96
  80. package/dist/chat/session.js +0 -257
  81. package/dist/chat/sse-handler.d.ts +0 -49
  82. package/dist/chat/sse-handler.js +0 -259
  83. package/dist/chat/types.d.ts +0 -74
  84. package/dist/chat/types.js +0 -5
  85. package/dist/chat/websocket-chat-handler.d.ts +0 -37
  86. package/dist/chat/websocket-chat-handler.js +0 -262
  87. package/dist/cli/commands/mcp.d.ts +0 -20
  88. package/dist/cli/commands/mcp.js +0 -1272
  89. package/dist/core/defaults.d.ts +0 -19
  90. package/dist/core/defaults.js +0 -29
  91. package/dist/core/evaluation-config.d.ts +0 -29
  92. package/dist/core/evaluation-config.js +0 -144
  93. package/dist/factories/compatibility-factory.d.ts +0 -35
  94. package/dist/factories/compatibility-factory.js +0 -71
  95. package/dist/factories/provider-generate-factory.d.ts +0 -20
  96. package/dist/factories/provider-generate-factory.js +0 -93
  97. package/dist/lib/chat/client-utils.d.ts +0 -95
  98. package/dist/lib/chat/client-utils.js +0 -315
  99. package/dist/lib/chat/index.d.ts +0 -24
  100. package/dist/lib/chat/index.js +0 -33
  101. package/dist/lib/chat/session-storage.d.ts +0 -77
  102. package/dist/lib/chat/session-storage.js +0 -233
  103. package/dist/lib/chat/session.d.ts +0 -96
  104. package/dist/lib/chat/session.js +0 -257
  105. package/dist/lib/chat/sse-handler.d.ts +0 -49
  106. package/dist/lib/chat/sse-handler.js +0 -259
  107. package/dist/lib/chat/types.d.ts +0 -74
  108. package/dist/lib/chat/types.js +0 -5
  109. package/dist/lib/chat/websocket-chat-handler.d.ts +0 -37
  110. package/dist/lib/chat/websocket-chat-handler.js +0 -262
  111. package/dist/lib/core/defaults.d.ts +0 -19
  112. package/dist/lib/core/defaults.js +0 -29
  113. package/dist/lib/core/evaluation-config.d.ts +0 -29
  114. package/dist/lib/core/evaluation-config.js +0 -144
  115. package/dist/lib/factories/compatibility-factory.d.ts +0 -35
  116. package/dist/lib/factories/compatibility-factory.js +0 -71
  117. package/dist/lib/factories/provider-generate-factory.d.ts +0 -20
  118. package/dist/lib/factories/provider-generate-factory.js +0 -93
  119. package/dist/lib/mcp/adapters/plugin-bridge.d.ts +0 -40
  120. package/dist/lib/mcp/adapters/plugin-bridge.js +0 -89
  121. package/dist/lib/mcp/auto-discovery.d.ts +0 -62
  122. package/dist/lib/mcp/auto-discovery.js +0 -149
  123. package/dist/lib/mcp/client.d.ts +0 -68
  124. package/dist/lib/mcp/client.js +0 -248
  125. package/dist/lib/mcp/config.d.ts +0 -31
  126. package/dist/lib/mcp/config.js +0 -99
  127. package/dist/lib/mcp/context-manager.d.ts +0 -171
  128. package/dist/lib/mcp/context-manager.js +0 -362
  129. package/dist/lib/mcp/contracts/mcp-contract.d.ts +0 -169
  130. package/dist/lib/mcp/contracts/mcp-contract.js +0 -58
  131. package/dist/lib/mcp/core/plugin-manager.d.ts +0 -46
  132. package/dist/lib/mcp/core/plugin-manager.js +0 -110
  133. package/dist/lib/mcp/demo/plugin-demo.d.ts +0 -20
  134. package/dist/lib/mcp/demo/plugin-demo.js +0 -118
  135. package/dist/lib/mcp/dynamic-chain-executor.d.ts +0 -225
  136. package/dist/lib/mcp/dynamic-chain-executor.js +0 -489
  137. package/dist/lib/mcp/dynamic-orchestrator.d.ts +0 -115
  138. package/dist/lib/mcp/dynamic-orchestrator.js +0 -351
  139. package/dist/lib/mcp/ecosystem.d.ts +0 -75
  140. package/dist/lib/mcp/ecosystem.js +0 -161
  141. package/dist/lib/mcp/error-manager.d.ts +0 -254
  142. package/dist/lib/mcp/error-manager.js +0 -501
  143. package/dist/lib/mcp/error-recovery.d.ts +0 -159
  144. package/dist/lib/mcp/error-recovery.js +0 -405
  145. package/dist/lib/mcp/external-client.d.ts +0 -88
  146. package/dist/lib/mcp/external-client.js +0 -331
  147. package/dist/lib/mcp/external-manager.d.ts +0 -112
  148. package/dist/lib/mcp/external-manager.js +0 -308
  149. package/dist/lib/mcp/function-calling.d.ts +0 -65
  150. package/dist/lib/mcp/function-calling.js +0 -642
  151. package/dist/lib/mcp/health-monitor.d.ts +0 -257
  152. package/dist/lib/mcp/health-monitor.js +0 -630
  153. package/dist/lib/mcp/initialize-tools.d.ts +0 -29
  154. package/dist/lib/mcp/initialize-tools.js +0 -261
  155. package/dist/lib/mcp/initialize.d.ts +0 -18
  156. package/dist/lib/mcp/initialize.js +0 -62
  157. package/dist/lib/mcp/logging.d.ts +0 -38
  158. package/dist/lib/mcp/logging.js +0 -100
  159. package/dist/lib/mcp/manager.d.ts +0 -68
  160. package/dist/lib/mcp/manager.js +0 -176
  161. package/dist/lib/mcp/neurolink-mcp-client.d.ts +0 -97
  162. package/dist/lib/mcp/neurolink-mcp-client.js +0 -462
  163. package/dist/lib/mcp/orchestrator.d.ts +0 -302
  164. package/dist/lib/mcp/orchestrator.js +0 -703
  165. package/dist/lib/mcp/plugin-manager.d.ts +0 -98
  166. package/dist/lib/mcp/plugin-manager.js +0 -296
  167. package/dist/lib/mcp/plugins/core/filesystem-mcp.d.ts +0 -36
  168. package/dist/lib/mcp/plugins/core/filesystem-mcp.js +0 -142
  169. package/dist/lib/mcp/plugins/filesystem-mcp.d.ts +0 -37
  170. package/dist/lib/mcp/plugins/filesystem-mcp.js +0 -54
  171. package/dist/lib/mcp/security-manager.d.ts +0 -87
  172. package/dist/lib/mcp/security-manager.js +0 -344
  173. package/dist/lib/mcp/semaphore-manager.d.ts +0 -137
  174. package/dist/lib/mcp/semaphore-manager.js +0 -329
  175. package/dist/lib/mcp/session-manager.d.ts +0 -187
  176. package/dist/lib/mcp/session-manager.js +0 -400
  177. package/dist/lib/mcp/session-persistence.d.ts +0 -93
  178. package/dist/lib/mcp/session-persistence.js +0 -301
  179. package/dist/lib/mcp/tool-integration.d.ts +0 -58
  180. package/dist/lib/mcp/tool-integration.js +0 -203
  181. package/dist/lib/mcp/transport-manager.d.ts +0 -154
  182. package/dist/lib/mcp/transport-manager.js +0 -334
  183. package/dist/lib/mcp/unified-mcp.d.ts +0 -133
  184. package/dist/lib/mcp/unified-mcp.js +0 -251
  185. package/dist/lib/mcp/unified-registry.d.ts +0 -165
  186. package/dist/lib/mcp/unified-registry.js +0 -538
  187. package/dist/lib/providers/analytics-helper.d.ts +0 -38
  188. package/dist/lib/providers/analytics-helper.js +0 -216
  189. package/dist/lib/providers/function-calling-provider.d.ts +0 -142
  190. package/dist/lib/providers/function-calling-provider.js +0 -630
  191. package/dist/lib/providers/mcp-provider.d.ts +0 -75
  192. package/dist/lib/providers/mcp-provider.js +0 -283
  193. package/dist/lib/providers/timeout-wrapper.d.ts +0 -40
  194. package/dist/lib/providers/timeout-wrapper.js +0 -100
  195. package/dist/lib/sdk/tool-extension.d.ts +0 -181
  196. package/dist/lib/sdk/tool-extension.js +0 -284
  197. package/dist/lib/services/streaming/streaming-manager.d.ts +0 -29
  198. package/dist/lib/services/streaming/streaming-manager.js +0 -245
  199. package/dist/lib/services/types.d.ts +0 -156
  200. package/dist/lib/services/types.js +0 -2
  201. package/dist/lib/services/websocket/websocket-server.d.ts +0 -34
  202. package/dist/lib/services/websocket/websocket-server.js +0 -305
  203. package/dist/lib/utils/provider-validation.d.ts +0 -36
  204. package/dist/lib/utils/provider-validation.js +0 -625
  205. package/dist/lib/utils/providerUtils-fixed.d.ts +0 -8
  206. package/dist/lib/utils/providerUtils-fixed.js +0 -94
  207. package/dist/lib/utils/streaming-utils.d.ts +0 -79
  208. package/dist/lib/utils/streaming-utils.js +0 -198
  209. package/dist/lib/utils/timeout-manager.d.ts +0 -75
  210. package/dist/lib/utils/timeout-manager.js +0 -244
  211. package/dist/mcp/adapters/plugin-bridge.d.ts +0 -40
  212. package/dist/mcp/adapters/plugin-bridge.js +0 -89
  213. package/dist/mcp/auto-discovery.d.ts +0 -62
  214. package/dist/mcp/auto-discovery.js +0 -149
  215. package/dist/mcp/client.d.ts +0 -68
  216. package/dist/mcp/client.js +0 -248
  217. package/dist/mcp/config.d.ts +0 -31
  218. package/dist/mcp/config.js +0 -99
  219. package/dist/mcp/context-manager.d.ts +0 -171
  220. package/dist/mcp/context-manager.js +0 -362
  221. package/dist/mcp/contracts/mcp-contract.d.ts +0 -169
  222. package/dist/mcp/contracts/mcp-contract.js +0 -58
  223. package/dist/mcp/core/plugin-manager.d.ts +0 -46
  224. package/dist/mcp/core/plugin-manager.js +0 -110
  225. package/dist/mcp/demo/plugin-demo.d.ts +0 -20
  226. package/dist/mcp/demo/plugin-demo.js +0 -118
  227. package/dist/mcp/dynamic-chain-executor.d.ts +0 -225
  228. package/dist/mcp/dynamic-chain-executor.js +0 -489
  229. package/dist/mcp/dynamic-orchestrator.d.ts +0 -115
  230. package/dist/mcp/dynamic-orchestrator.js +0 -351
  231. package/dist/mcp/ecosystem.d.ts +0 -75
  232. package/dist/mcp/ecosystem.js +0 -162
  233. package/dist/mcp/error-manager.d.ts +0 -254
  234. package/dist/mcp/error-manager.js +0 -501
  235. package/dist/mcp/error-recovery.d.ts +0 -159
  236. package/dist/mcp/error-recovery.js +0 -405
  237. package/dist/mcp/external-client.d.ts +0 -88
  238. package/dist/mcp/external-client.js +0 -331
  239. package/dist/mcp/external-manager.d.ts +0 -112
  240. package/dist/mcp/external-manager.js +0 -308
  241. package/dist/mcp/function-calling.d.ts +0 -65
  242. package/dist/mcp/function-calling.js +0 -642
  243. package/dist/mcp/health-monitor.d.ts +0 -257
  244. package/dist/mcp/health-monitor.js +0 -630
  245. package/dist/mcp/initialize-tools.d.ts +0 -29
  246. package/dist/mcp/initialize-tools.js +0 -262
  247. package/dist/mcp/initialize.d.ts +0 -18
  248. package/dist/mcp/initialize.js +0 -62
  249. package/dist/mcp/logging.d.ts +0 -38
  250. package/dist/mcp/logging.js +0 -100
  251. package/dist/mcp/manager.d.ts +0 -68
  252. package/dist/mcp/manager.js +0 -176
  253. package/dist/mcp/neurolink-mcp-client.d.ts +0 -97
  254. package/dist/mcp/neurolink-mcp-client.js +0 -462
  255. package/dist/mcp/orchestrator.d.ts +0 -302
  256. package/dist/mcp/orchestrator.js +0 -703
  257. package/dist/mcp/plugin-manager.d.ts +0 -98
  258. package/dist/mcp/plugin-manager.js +0 -297
  259. package/dist/mcp/plugins/core/filesystem-mcp.d.ts +0 -36
  260. package/dist/mcp/plugins/core/filesystem-mcp.js +0 -142
  261. package/dist/mcp/plugins/core/neurolink-mcp.json +0 -17
  262. package/dist/mcp/plugins/filesystem-mcp.d.ts +0 -37
  263. package/dist/mcp/plugins/filesystem-mcp.js +0 -54
  264. package/dist/mcp/security-manager.d.ts +0 -87
  265. package/dist/mcp/security-manager.js +0 -344
  266. package/dist/mcp/semaphore-manager.d.ts +0 -137
  267. package/dist/mcp/semaphore-manager.js +0 -329
  268. package/dist/mcp/session-manager.d.ts +0 -187
  269. package/dist/mcp/session-manager.js +0 -400
  270. package/dist/mcp/session-persistence.d.ts +0 -93
  271. package/dist/mcp/session-persistence.js +0 -302
  272. package/dist/mcp/tool-integration.d.ts +0 -58
  273. package/dist/mcp/tool-integration.js +0 -203
  274. package/dist/mcp/transport-manager.d.ts +0 -154
  275. package/dist/mcp/transport-manager.js +0 -335
  276. package/dist/mcp/unified-mcp.d.ts +0 -133
  277. package/dist/mcp/unified-mcp.js +0 -251
  278. package/dist/mcp/unified-registry.d.ts +0 -165
  279. package/dist/mcp/unified-registry.js +0 -539
  280. package/dist/providers/analytics-helper.d.ts +0 -38
  281. package/dist/providers/analytics-helper.js +0 -216
  282. package/dist/providers/function-calling-provider.d.ts +0 -142
  283. package/dist/providers/function-calling-provider.js +0 -630
  284. package/dist/providers/mcp-provider.d.ts +0 -75
  285. package/dist/providers/mcp-provider.js +0 -283
  286. package/dist/providers/timeout-wrapper.d.ts +0 -40
  287. package/dist/providers/timeout-wrapper.js +0 -100
  288. package/dist/sdk/tool-extension.d.ts +0 -181
  289. package/dist/sdk/tool-extension.js +0 -284
  290. package/dist/services/streaming/streaming-manager.d.ts +0 -29
  291. package/dist/services/streaming/streaming-manager.js +0 -245
  292. package/dist/services/types.d.ts +0 -156
  293. package/dist/services/types.js +0 -2
  294. package/dist/services/websocket/websocket-server.d.ts +0 -34
  295. package/dist/services/websocket/websocket-server.js +0 -306
  296. package/dist/utils/provider-validation.d.ts +0 -36
  297. package/dist/utils/provider-validation.js +0 -625
  298. package/dist/utils/providerUtils-fixed.d.ts +0 -8
  299. package/dist/utils/providerUtils-fixed.js +0 -94
  300. package/dist/utils/streaming-utils.d.ts +0 -79
  301. package/dist/utils/streaming-utils.js +0 -198
  302. package/dist/utils/timeout-manager.d.ts +0 -75
  303. 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>;