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