@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,284 +0,0 @@
1
- /**
2
- * NeuroLink SDK Tool Extension System
3
- * Allows developers to register custom tools that integrate with AI providers
4
- */
5
- import { z } from "zod";
6
- import { tool as createAISDKTool } from "ai";
7
- import { zodToJsonSchema } from "zod-to-json-schema";
8
- import { logger } from "../utils/logger.js";
9
- /**
10
- * Converts a custom tool to Vercel AI SDK format
11
- */
12
- export function convertToAISDKTool(name, customTool) {
13
- // Convert parameters to JSON schema if needed
14
- let parametersSchema = {};
15
- let zodSchema;
16
- if (customTool.parameters) {
17
- if ("parse" in customTool.parameters && "_def" in customTool.parameters) {
18
- // It's a Zod schema
19
- zodSchema = customTool.parameters;
20
- parametersSchema = zodToJsonSchema(zodSchema);
21
- }
22
- else {
23
- // It's already a JSON schema - convert to Zod
24
- parametersSchema = customTool.parameters;
25
- zodSchema = z.object({}).passthrough(); // Allow any properties
26
- }
27
- }
28
- else {
29
- zodSchema = z.object({});
30
- }
31
- return createAISDKTool({
32
- description: customTool.description,
33
- parameters: zodSchema,
34
- execute: async (args) => {
35
- try {
36
- // Apply timeout if configured
37
- if (customTool.config?.timeout) {
38
- return await Promise.race([
39
- customTool.execute(args),
40
- new Promise((_, reject) => setTimeout(() => reject(new Error(`Tool ${name} timed out`)), customTool.config.timeout)),
41
- ]);
42
- }
43
- return await customTool.execute(args);
44
- }
45
- catch (error) {
46
- logger.error(`Tool ${name} execution failed:`, error);
47
- throw error;
48
- }
49
- },
50
- });
51
- }
52
- /**
53
- * Tool registry for managing custom tools
54
- */
55
- export class ToolRegistry {
56
- tools = new Map();
57
- middleware = [];
58
- permissions = {};
59
- rateLimits = new Map();
60
- /**
61
- * Simple rate limiting check with automatic cleanup
62
- */
63
- checkRateLimit(name, rateLimit) {
64
- const now = Date.now();
65
- // Clean up expired entries to prevent memory leaks
66
- for (const [key, limit] of this.rateLimits.entries()) {
67
- if (limit.resetTime <= now) {
68
- this.rateLimits.delete(key);
69
- }
70
- }
71
- const limit = this.rateLimits.get(name);
72
- if (limit && limit.resetTime > now) {
73
- if (limit.count >= rateLimit.requests) {
74
- throw new Error(`Tool ${name} rate limit exceeded`);
75
- }
76
- limit.count++;
77
- }
78
- else {
79
- this.rateLimits.set(name, {
80
- count: 1,
81
- resetTime: now + rateLimit.window,
82
- });
83
- }
84
- }
85
- /**
86
- * Register a custom tool
87
- */
88
- register(name, tool) {
89
- if (this.tools.has(name)) {
90
- logger.warn(`Tool ${name} already registered, overwriting`);
91
- }
92
- this.tools.set(name, tool);
93
- logger.info(`Registered custom tool: ${name}`);
94
- }
95
- /**
96
- * Register multiple tools at once
97
- */
98
- registerMany(tools) {
99
- Object.entries(tools).forEach(([name, tool]) => {
100
- this.register(name, tool);
101
- });
102
- }
103
- /**
104
- * Unregister a tool
105
- */
106
- unregister(name) {
107
- const result = this.tools.delete(name);
108
- if (result) {
109
- logger.info(`Unregistered tool: ${name}`);
110
- }
111
- return result;
112
- }
113
- /**
114
- * Get a tool by name
115
- */
116
- get(name) {
117
- return this.tools.get(name);
118
- }
119
- /**
120
- * Get all registered tools
121
- */
122
- getAll() {
123
- return new Map(this.tools);
124
- }
125
- /**
126
- * Convert all tools to AI SDK format
127
- */
128
- toAISDKTools() {
129
- const aiTools = {};
130
- for (const [name, tool] of this.tools) {
131
- if (this.isToolAllowed(name)) {
132
- aiTools[name] = convertToAISDKTool(name, tool);
133
- }
134
- }
135
- return aiTools;
136
- }
137
- /**
138
- * Add middleware
139
- */
140
- use(middleware) {
141
- this.middleware.push(middleware);
142
- }
143
- /**
144
- * Set permissions
145
- */
146
- setPermissions(permissions) {
147
- this.permissions = permissions;
148
- }
149
- /**
150
- * Check if a tool is allowed
151
- */
152
- isToolAllowed(name) {
153
- // Check denylist first
154
- if (this.permissions.denylist?.includes(name)) {
155
- return false;
156
- }
157
- // Check allowlist if specified
158
- if (this.permissions.allowlist &&
159
- !this.permissions.allowlist.includes(name)) {
160
- return false;
161
- }
162
- return true;
163
- }
164
- /**
165
- * Execute a tool with middleware
166
- */
167
- async execute(name, args, context) {
168
- const tool = this.tools.get(name);
169
- if (!tool) {
170
- throw new Error(`Tool ${name} not found`);
171
- }
172
- // Check permissions
173
- if (!this.isToolAllowed(name)) {
174
- throw new Error(`Tool ${name} is not allowed`);
175
- }
176
- // Check custom validator
177
- if (this.permissions.customValidator) {
178
- const allowed = await this.permissions.customValidator(name, args);
179
- if (!allowed) {
180
- throw new Error(`Tool ${name} execution denied by custom validator`);
181
- }
182
- }
183
- // Check rate limit
184
- if (tool.config?.rateLimit) {
185
- this.checkRateLimit(name, tool.config.rateLimit);
186
- }
187
- // Build middleware chain
188
- let index = 0;
189
- const next = async () => {
190
- if (index < this.middleware.length) {
191
- const middleware = this.middleware[index++];
192
- return middleware(name, args, next, context);
193
- }
194
- else {
195
- // Execute the actual tool
196
- return tool.execute(args, context);
197
- }
198
- };
199
- return next();
200
- }
201
- }
202
- /**
203
- * Create a simple tool helper
204
- */
205
- export function createTool(config) {
206
- return config;
207
- }
208
- /**
209
- * Create an async tool helper
210
- */
211
- export function createAsyncTool(config) {
212
- return config;
213
- }
214
- /**
215
- * Create a batch tool that processes multiple items
216
- */
217
- export function createBatchTool(config) {
218
- return {
219
- ...config,
220
- execute: async (args, context) => {
221
- const { items } = args;
222
- const batchSize = config.batchSize || 10;
223
- const results = [];
224
- for (let i = 0; i < items.length; i += batchSize) {
225
- const batch = items.slice(i, i + batchSize);
226
- const batchResults = await Promise.all(batch.map((item) => config.processItem(item, context)));
227
- results.push(...batchResults);
228
- }
229
- return results;
230
- },
231
- };
232
- }
233
- /**
234
- * Tool testing utilities
235
- */
236
- export const TestUtils = {
237
- /**
238
- * Create a mock tool context
239
- */
240
- mockContext(overrides) {
241
- return {
242
- callTool: async (name, args) => {
243
- logger.debug(`Mock tool call: ${name}`, args);
244
- return {};
245
- },
246
- session: {
247
- id: "test-session",
248
- userId: "test-user",
249
- provider: "test-provider",
250
- model: "test-model",
251
- },
252
- logger,
253
- ...overrides,
254
- };
255
- },
256
- /**
257
- * Test a tool with mock data
258
- */
259
- async testTool(tool, testCases) {
260
- const context = TestUtils.mockContext();
261
- const results = [];
262
- for (const testCase of testCases) {
263
- try {
264
- const result = await tool.execute(testCase.input, context);
265
- results.push({
266
- input: testCase.input,
267
- output: result,
268
- success: true,
269
- matches: testCase.expected
270
- ? JSON.stringify(result) === JSON.stringify(testCase.expected)
271
- : undefined,
272
- });
273
- }
274
- catch (error) {
275
- results.push({
276
- input: testCase.input,
277
- error: error instanceof Error ? error.message : String(error),
278
- success: false,
279
- });
280
- }
281
- }
282
- return results;
283
- },
284
- };
@@ -1,29 +0,0 @@
1
- import { EventEmitter } from "events";
2
- import type { StreamingSession, StreamingConfig, StreamingPoolConfig, StreamingMetrics, StreamingHealthStatus, BufferConfig } from "../types.js";
3
- export declare class StreamingManager extends EventEmitter {
4
- private activeSessions;
5
- private streamingPools;
6
- private metrics;
7
- private healthCheckInterval?;
8
- private startTime;
9
- constructor();
10
- createStreamingSession(config: StreamingConfig): Promise<StreamingSession>;
11
- terminateStreamingSession(sessionId: string): Promise<void>;
12
- pauseStreamingSession(sessionId: string): Promise<void>;
13
- resumeStreamingSession(sessionId: string): Promise<void>;
14
- optimizeStreamingLatency(sessionId: string): Promise<void>;
15
- enableStreamingCompression(sessionId: string): Promise<void>;
16
- configureStreamingBuffering(sessionId: string, bufferConfig: BufferConfig): Promise<void>;
17
- createStreamingPool(poolId: string, config: StreamingPoolConfig): Promise<void>;
18
- balanceStreamingLoad(poolId: string): Promise<void>;
19
- scaleStreamingCapacity(poolId: string, scale: number): Promise<void>;
20
- getStreamingMetrics(sessionId?: string): StreamingMetrics;
21
- getStreamingHealthStatus(): StreamingHealthStatus;
22
- private updateGlobalMetrics;
23
- private startHealthMonitoring;
24
- private roundRobinBalance;
25
- private leastConnectionsBalance;
26
- private weightedBalance;
27
- private adaptiveBalance;
28
- destroy(): void;
29
- }
@@ -1,245 +0,0 @@
1
- import { EventEmitter } from "events";
2
- import { randomUUID } from "crypto";
3
- import { logger } from "../../utils/logger.js";
4
- export class StreamingManager extends EventEmitter {
5
- activeSessions = new Map();
6
- streamingPools = new Map();
7
- metrics;
8
- healthCheckInterval;
9
- startTime;
10
- constructor() {
11
- super();
12
- this.startTime = Date.now();
13
- this.metrics = {
14
- activeSessions: 0,
15
- totalBytesTransferred: 0,
16
- averageLatency: 0,
17
- throughputBps: 0,
18
- errorRate: 0,
19
- connectionCount: 0,
20
- uptime: 0,
21
- };
22
- this.startHealthMonitoring();
23
- }
24
- // Stream Lifecycle Management
25
- async createStreamingSession(config) {
26
- const sessionId = randomUUID();
27
- const session = {
28
- id: sessionId,
29
- connectionId: config.provider, // Temporary, should be actual connection ID
30
- provider: config.provider,
31
- status: "active",
32
- startTime: Date.now(),
33
- lastActivity: Date.now(),
34
- config,
35
- metrics: {
36
- bytesTransferred: 0,
37
- messagesCount: 0,
38
- averageLatency: 0,
39
- errorCount: 0,
40
- },
41
- };
42
- this.activeSessions.set(sessionId, session);
43
- this.updateGlobalMetrics();
44
- logger.debug(`[Streaming Manager] Created session ${sessionId} for provider ${config.provider}`);
45
- this.emit("session-created", session);
46
- return session;
47
- }
48
- async terminateStreamingSession(sessionId) {
49
- const session = this.activeSessions.get(sessionId);
50
- if (!session) {
51
- throw new Error(`Session ${sessionId} not found`);
52
- }
53
- session.status = "terminated";
54
- this.activeSessions.delete(sessionId);
55
- this.updateGlobalMetrics();
56
- logger.debug(`[Streaming Manager] Terminated session ${sessionId}`);
57
- this.emit("session-terminated", session);
58
- }
59
- async pauseStreamingSession(sessionId) {
60
- const session = this.activeSessions.get(sessionId);
61
- if (!session) {
62
- throw new Error(`Session ${sessionId} not found`);
63
- }
64
- if (session.status === "active") {
65
- session.status = "paused";
66
- logger.debug(`[Streaming Manager] Paused session ${sessionId}`);
67
- this.emit("session-paused", session);
68
- }
69
- }
70
- async resumeStreamingSession(sessionId) {
71
- const session = this.activeSessions.get(sessionId);
72
- if (!session) {
73
- throw new Error(`Session ${sessionId} not found`);
74
- }
75
- if (session.status === "paused") {
76
- session.status = "active";
77
- session.lastActivity = Date.now();
78
- logger.debug(`[Streaming Manager] Resumed session ${sessionId}`);
79
- this.emit("session-resumed", session);
80
- }
81
- }
82
- // Stream Optimization
83
- async optimizeStreamingLatency(sessionId) {
84
- const session = this.activeSessions.get(sessionId);
85
- if (!session) {
86
- return;
87
- }
88
- // Adaptive optimization based on current metrics
89
- const currentLatency = session.metrics.averageLatency;
90
- const targetLatency = session.config.latencyTarget;
91
- if (currentLatency > targetLatency * 1.2) {
92
- // Increase buffer size for better throughput
93
- session.config.bufferSize = Math.min(session.config.bufferSize * 1.5, 16384);
94
- session.config.streamingMode = "buffered";
95
- }
96
- else if (currentLatency < targetLatency * 0.8) {
97
- // Decrease buffer size for better latency
98
- session.config.bufferSize = Math.max(session.config.bufferSize * 0.8, 1024);
99
- session.config.streamingMode = "real-time";
100
- }
101
- logger.debug(`[Streaming Manager] Optimized session ${sessionId}: latency=${currentLatency}ms, mode=${session.config.streamingMode}`);
102
- }
103
- async enableStreamingCompression(sessionId) {
104
- const session = this.activeSessions.get(sessionId);
105
- if (!session) {
106
- return;
107
- }
108
- session.config.compressionEnabled = true;
109
- logger.debug(`[Streaming Manager] Enabled compression for session ${sessionId}`);
110
- }
111
- async configureStreamingBuffering(sessionId, bufferConfig) {
112
- const session = this.activeSessions.get(sessionId);
113
- if (!session) {
114
- return;
115
- }
116
- session.config.bufferSize = bufferConfig.maxSize;
117
- session.config.maxChunkSize = Math.min(session.config.maxChunkSize, bufferConfig.flushThreshold);
118
- logger.debug(`[Streaming Manager] Updated buffer config for session ${sessionId}:`, bufferConfig);
119
- }
120
- // Multi-Stream Coordination
121
- async createStreamingPool(poolId, config) {
122
- const pool = {
123
- id: poolId,
124
- maxSessions: config.maxConcurrentSessions,
125
- activeSessions: new Set(),
126
- config,
127
- loadBalancer: config.loadBalancing,
128
- };
129
- this.streamingPools.set(poolId, pool);
130
- logger.debug(`[Streaming Manager] Created pool ${poolId} with max ${config.maxConcurrentSessions} sessions`);
131
- }
132
- async balanceStreamingLoad(poolId) {
133
- const pool = this.streamingPools.get(poolId);
134
- if (!pool) {
135
- return;
136
- }
137
- const activeSessions = Array.from(pool.activeSessions)
138
- .map((sessionId) => this.activeSessions.get(sessionId))
139
- .filter((session) => session && session.status === "active");
140
- // Implement load balancing strategies
141
- switch (pool.loadBalancer) {
142
- case "round-robin":
143
- this.roundRobinBalance(activeSessions);
144
- break;
145
- case "least-connections":
146
- this.leastConnectionsBalance(activeSessions);
147
- break;
148
- case "weighted":
149
- this.weightedBalance(activeSessions);
150
- break;
151
- case "adaptive":
152
- this.adaptiveBalance(activeSessions);
153
- break;
154
- }
155
- }
156
- async scaleStreamingCapacity(poolId, scale) {
157
- const pool = this.streamingPools.get(poolId);
158
- if (!pool) {
159
- return;
160
- }
161
- const newMaxSessions = Math.max(1, Math.floor(pool.maxSessions * scale));
162
- pool.maxSessions = newMaxSessions;
163
- pool.config.maxConcurrentSessions = newMaxSessions;
164
- logger.debug(`[Streaming Manager] Scaled pool ${poolId} to ${newMaxSessions} max sessions (${scale}x)`);
165
- }
166
- // Performance Monitoring
167
- getStreamingMetrics(sessionId) {
168
- if (sessionId) {
169
- const session = this.activeSessions.get(sessionId);
170
- if (session) {
171
- return {
172
- sessionId,
173
- activeSessions: 1,
174
- totalBytesTransferred: session.metrics.bytesTransferred,
175
- averageLatency: session.metrics.averageLatency,
176
- throughputBps: session.metrics.bytesTransferred /
177
- ((Date.now() - session.startTime) / 1000),
178
- errorRate: session.metrics.errorCount /
179
- Math.max(session.metrics.messagesCount, 1),
180
- connectionCount: 1,
181
- uptime: Date.now() - session.startTime,
182
- };
183
- }
184
- }
185
- return { ...this.metrics, uptime: Date.now() - this.startTime };
186
- }
187
- getStreamingHealthStatus() {
188
- const metrics = this.getStreamingMetrics();
189
- const issues = [];
190
- let status = "healthy";
191
- if (metrics.errorRate > 0.1) {
192
- issues.push(`High error rate: ${(metrics.errorRate * 100).toFixed(1)}%`);
193
- status = "degraded";
194
- }
195
- if (metrics.averageLatency > 1000) {
196
- issues.push(`High latency: ${metrics.averageLatency}ms`);
197
- status = status === "healthy" ? "degraded" : "unhealthy";
198
- }
199
- if (metrics.activeSessions === 0 && this.activeSessions.size > 0) {
200
- issues.push("Session count mismatch");
201
- status = "unhealthy";
202
- }
203
- return {
204
- status,
205
- activeSessions: metrics.activeSessions,
206
- errorRate: metrics.errorRate,
207
- averageLatency: metrics.averageLatency,
208
- lastHealthCheck: Date.now(),
209
- issues,
210
- };
211
- }
212
- // Private helper methods
213
- updateGlobalMetrics() {
214
- this.metrics.activeSessions = this.activeSessions.size;
215
- this.metrics.connectionCount = this.activeSessions.size;
216
- }
217
- startHealthMonitoring() {
218
- this.healthCheckInterval = setInterval(() => {
219
- const health = this.getStreamingHealthStatus();
220
- if (health.status !== "healthy") {
221
- logger.debug("[Streaming Manager] Health check:", health);
222
- this.emit("health-warning", health);
223
- }
224
- }, 30000); // Check every 30 seconds
225
- }
226
- roundRobinBalance(sessions) {
227
- // Round-robin implementation
228
- }
229
- leastConnectionsBalance(sessions) {
230
- // Least connections implementation
231
- }
232
- weightedBalance(sessions) {
233
- // Weighted load balancing implementation
234
- }
235
- adaptiveBalance(sessions) {
236
- // Adaptive load balancing implementation
237
- }
238
- destroy() {
239
- if (this.healthCheckInterval) {
240
- clearInterval(this.healthCheckInterval);
241
- }
242
- this.activeSessions.clear();
243
- this.streamingPools.clear();
244
- }
245
- }