@juspay/neurolink 5.1.0 → 5.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 (166) hide show
  1. package/CHANGELOG.md +15 -9
  2. package/README.md +123 -126
  3. package/dist/agent/direct-tools.d.ts +6 -6
  4. package/dist/cli/commands/config.d.ts +3 -3
  5. package/dist/cli/commands/mcp.js +8 -7
  6. package/dist/cli/factories/command-factory.d.ts +4 -0
  7. package/dist/cli/factories/command-factory.js +57 -3
  8. package/dist/cli/index.js +87 -140
  9. package/dist/core/base-provider.d.ts +423 -0
  10. package/dist/core/base-provider.js +365 -0
  11. package/dist/core/constants.d.ts +1 -1
  12. package/dist/core/constants.js +1 -1
  13. package/dist/core/dynamic-models.d.ts +6 -6
  14. package/dist/core/evaluation.d.ts +19 -80
  15. package/dist/core/evaluation.js +185 -484
  16. package/dist/core/factory.d.ts +3 -3
  17. package/dist/core/factory.js +31 -91
  18. package/dist/core/service-registry.d.ts +47 -0
  19. package/dist/core/service-registry.js +112 -0
  20. package/dist/core/types.d.ts +8 -1
  21. package/dist/factories/compatibility-factory.js +1 -1
  22. package/dist/factories/provider-factory.d.ts +72 -0
  23. package/dist/factories/provider-factory.js +144 -0
  24. package/dist/factories/provider-registry.d.ts +38 -0
  25. package/dist/factories/provider-registry.js +107 -0
  26. package/dist/index.d.ts +4 -3
  27. package/dist/index.js +2 -4
  28. package/dist/lib/agent/direct-tools.d.ts +6 -6
  29. package/dist/lib/core/base-provider.d.ts +423 -0
  30. package/dist/lib/core/base-provider.js +365 -0
  31. package/dist/lib/core/constants.d.ts +1 -1
  32. package/dist/lib/core/constants.js +1 -1
  33. package/dist/lib/core/dynamic-models.d.ts +6 -6
  34. package/dist/lib/core/evaluation.d.ts +19 -80
  35. package/dist/lib/core/evaluation.js +185 -484
  36. package/dist/lib/core/factory.d.ts +3 -3
  37. package/dist/lib/core/factory.js +30 -91
  38. package/dist/lib/core/service-registry.d.ts +47 -0
  39. package/dist/lib/core/service-registry.js +112 -0
  40. package/dist/lib/core/types.d.ts +8 -1
  41. package/dist/lib/factories/compatibility-factory.js +1 -1
  42. package/dist/lib/factories/provider-factory.d.ts +72 -0
  43. package/dist/lib/factories/provider-factory.js +144 -0
  44. package/dist/lib/factories/provider-registry.d.ts +38 -0
  45. package/dist/lib/factories/provider-registry.js +107 -0
  46. package/dist/lib/index.d.ts +4 -3
  47. package/dist/lib/index.js +2 -4
  48. package/dist/lib/mcp/config.js +28 -3
  49. package/dist/lib/mcp/function-calling.js +1 -1
  50. package/dist/lib/mcp/initialize-tools.d.ts +1 -1
  51. package/dist/lib/mcp/initialize-tools.js +45 -1
  52. package/dist/lib/mcp/initialize.js +16 -6
  53. package/dist/lib/mcp/servers/agent/direct-tools-server.d.ts +8 -0
  54. package/dist/lib/mcp/servers/agent/direct-tools-server.js +109 -0
  55. package/dist/lib/mcp/servers/ai-providers/ai-core-server.js +3 -1
  56. package/dist/lib/mcp/servers/ai-providers/ai-workflow-tools.d.ts +2 -2
  57. package/dist/lib/mcp/unified-registry.d.ts +4 -0
  58. package/dist/lib/mcp/unified-registry.js +42 -9
  59. package/dist/lib/neurolink.d.ts +156 -117
  60. package/dist/lib/neurolink.js +619 -404
  61. package/dist/lib/providers/amazon-bedrock.d.ts +32 -0
  62. package/dist/lib/providers/amazon-bedrock.js +143 -0
  63. package/dist/lib/providers/analytics-helper.js +7 -4
  64. package/dist/lib/providers/anthropic-baseprovider.d.ts +23 -0
  65. package/dist/lib/providers/anthropic-baseprovider.js +114 -0
  66. package/dist/lib/providers/anthropic.d.ts +19 -43
  67. package/dist/lib/providers/anthropic.js +82 -306
  68. package/dist/lib/providers/azure-openai.d.ts +20 -0
  69. package/dist/lib/providers/azure-openai.js +89 -0
  70. package/dist/lib/providers/google-ai-studio.d.ts +23 -0
  71. package/dist/lib/providers/google-ai-studio.js +107 -0
  72. package/dist/lib/providers/google-vertex.d.ts +47 -0
  73. package/dist/lib/providers/google-vertex.js +205 -0
  74. package/dist/lib/providers/huggingFace.d.ts +32 -25
  75. package/dist/lib/providers/huggingFace.js +97 -431
  76. package/dist/lib/providers/index.d.ts +9 -9
  77. package/dist/lib/providers/index.js +9 -9
  78. package/dist/lib/providers/mcp-provider.js +4 -0
  79. package/dist/lib/providers/mistral.d.ts +42 -0
  80. package/dist/lib/providers/mistral.js +160 -0
  81. package/dist/lib/providers/ollama.d.ts +52 -36
  82. package/dist/lib/providers/ollama.js +297 -520
  83. package/dist/lib/providers/openAI.d.ts +19 -18
  84. package/dist/lib/providers/openAI.js +76 -275
  85. package/dist/lib/sdk/tool-extension.d.ts +181 -0
  86. package/dist/lib/sdk/tool-extension.js +283 -0
  87. package/dist/lib/sdk/tool-registration.d.ts +95 -0
  88. package/dist/lib/sdk/tool-registration.js +167 -0
  89. package/dist/lib/types/generate-types.d.ts +1 -0
  90. package/dist/lib/types/mcp-types.d.ts +116 -0
  91. package/dist/lib/types/mcp-types.js +5 -0
  92. package/dist/lib/types/stream-types.d.ts +30 -18
  93. package/dist/lib/types/universal-provider-options.d.ts +87 -0
  94. package/dist/lib/types/universal-provider-options.js +53 -0
  95. package/dist/mcp/config.js +28 -3
  96. package/dist/mcp/function-calling.js +1 -1
  97. package/dist/mcp/initialize-tools.d.ts +1 -1
  98. package/dist/mcp/initialize-tools.js +45 -1
  99. package/dist/mcp/initialize.js +16 -6
  100. package/dist/mcp/servers/agent/direct-tools-server.d.ts +8 -0
  101. package/dist/mcp/servers/agent/direct-tools-server.js +109 -0
  102. package/dist/mcp/servers/ai-providers/ai-core-server.js +3 -1
  103. package/dist/mcp/servers/ai-providers/ai-workflow-tools.d.ts +2 -2
  104. package/dist/mcp/unified-registry.d.ts +4 -0
  105. package/dist/mcp/unified-registry.js +42 -9
  106. package/dist/neurolink.d.ts +156 -117
  107. package/dist/neurolink.js +619 -404
  108. package/dist/providers/amazon-bedrock.d.ts +32 -0
  109. package/dist/providers/amazon-bedrock.js +143 -0
  110. package/dist/providers/analytics-helper.js +7 -4
  111. package/dist/providers/anthropic-baseprovider.d.ts +23 -0
  112. package/dist/providers/anthropic-baseprovider.js +114 -0
  113. package/dist/providers/anthropic.d.ts +19 -43
  114. package/dist/providers/anthropic.js +81 -305
  115. package/dist/providers/azure-openai.d.ts +20 -0
  116. package/dist/providers/azure-openai.js +89 -0
  117. package/dist/providers/google-ai-studio.d.ts +23 -0
  118. package/dist/providers/google-ai-studio.js +108 -0
  119. package/dist/providers/google-vertex.d.ts +47 -0
  120. package/dist/providers/google-vertex.js +205 -0
  121. package/dist/providers/huggingFace.d.ts +32 -25
  122. package/dist/providers/huggingFace.js +96 -430
  123. package/dist/providers/index.d.ts +9 -9
  124. package/dist/providers/index.js +9 -9
  125. package/dist/providers/mcp-provider.js +4 -0
  126. package/dist/providers/mistral.d.ts +42 -0
  127. package/dist/providers/mistral.js +160 -0
  128. package/dist/providers/ollama.d.ts +52 -36
  129. package/dist/providers/ollama.js +297 -519
  130. package/dist/providers/openAI.d.ts +19 -18
  131. package/dist/providers/openAI.js +76 -276
  132. package/dist/sdk/tool-extension.d.ts +181 -0
  133. package/dist/sdk/tool-extension.js +283 -0
  134. package/dist/sdk/tool-registration.d.ts +95 -0
  135. package/dist/sdk/tool-registration.js +168 -0
  136. package/dist/types/generate-types.d.ts +1 -0
  137. package/dist/types/mcp-types.d.ts +116 -0
  138. package/dist/types/mcp-types.js +5 -0
  139. package/dist/types/stream-types.d.ts +30 -18
  140. package/dist/types/universal-provider-options.d.ts +87 -0
  141. package/dist/types/universal-provider-options.js +53 -0
  142. package/package.json +15 -10
  143. package/dist/lib/providers/agent-enhanced-provider.d.ts +0 -93
  144. package/dist/lib/providers/agent-enhanced-provider.js +0 -605
  145. package/dist/lib/providers/amazonBedrock.d.ts +0 -28
  146. package/dist/lib/providers/amazonBedrock.js +0 -364
  147. package/dist/lib/providers/azureOpenAI.d.ts +0 -42
  148. package/dist/lib/providers/azureOpenAI.js +0 -347
  149. package/dist/lib/providers/googleAIStudio.d.ts +0 -42
  150. package/dist/lib/providers/googleAIStudio.js +0 -364
  151. package/dist/lib/providers/googleVertexAI.d.ts +0 -34
  152. package/dist/lib/providers/googleVertexAI.js +0 -547
  153. package/dist/lib/providers/mistralAI.d.ts +0 -37
  154. package/dist/lib/providers/mistralAI.js +0 -325
  155. package/dist/providers/agent-enhanced-provider.d.ts +0 -93
  156. package/dist/providers/agent-enhanced-provider.js +0 -606
  157. package/dist/providers/amazonBedrock.d.ts +0 -28
  158. package/dist/providers/amazonBedrock.js +0 -364
  159. package/dist/providers/azureOpenAI.d.ts +0 -42
  160. package/dist/providers/azureOpenAI.js +0 -348
  161. package/dist/providers/googleAIStudio.d.ts +0 -42
  162. package/dist/providers/googleAIStudio.js +0 -364
  163. package/dist/providers/googleVertexAI.d.ts +0 -34
  164. package/dist/providers/googleVertexAI.js +0 -547
  165. package/dist/providers/mistralAI.d.ts +0 -37
  166. package/dist/providers/mistralAI.js +0 -325
@@ -1,325 +0,0 @@
1
- import { createMistral } from "@ai-sdk/mistral";
2
- import { streamText, generateText, Output } from "ai";
3
- import { logger } from "../utils/logger.js";
4
- import { createTimeoutController, TimeoutError, getDefaultTimeout, } from "../utils/timeout.js";
5
- import { DEFAULT_MAX_TOKENS } from "../core/constants.js";
6
- import { evaluateResponse } from "../core/evaluation.js";
7
- // Default system context
8
- const DEFAULT_SYSTEM_CONTEXT = {
9
- systemPrompt: "You are a helpful AI assistant.",
10
- };
11
- // Configuration helpers
12
- const getMistralApiKey = () => {
13
- const apiKey = process.env.MISTRAL_API_KEY;
14
- if (!apiKey) {
15
- throw new Error("MISTRAL_API_KEY environment variable is not set");
16
- }
17
- return apiKey;
18
- };
19
- const getMistralModelId = () => {
20
- return process.env.MISTRAL_MODEL || "mistral-small";
21
- };
22
- const hasValidAuth = () => {
23
- return !!process.env.MISTRAL_API_KEY;
24
- };
25
- // Lazy initialization cache
26
- let _mistralClient = null;
27
- function getMistralClient() {
28
- if (!_mistralClient) {
29
- const apiKey = getMistralApiKey();
30
- _mistralClient = createMistral({
31
- apiKey,
32
- baseURL: process.env.MISTRAL_ENDPOINT || "https://api.mistral.ai/v1",
33
- });
34
- }
35
- return _mistralClient;
36
- }
37
- // Mistral AI class with enhanced error handling
38
- export class MistralAI {
39
- modelName;
40
- client;
41
- /**
42
- * Initializes a new instance of MistralAI
43
- * @param modelName - Optional model name to override the default from config
44
- */
45
- constructor(modelName) {
46
- const functionTag = "MistralAI.constructor";
47
- this.modelName = modelName || getMistralModelId();
48
- try {
49
- this.client = getMistralClient();
50
- logger.debug(`[${functionTag}] Initialization started`, {
51
- modelName: this.modelName,
52
- hasApiKey: hasValidAuth(),
53
- });
54
- logger.debug(`[${functionTag}] Initialization completed`, {
55
- modelName: this.modelName,
56
- success: true,
57
- });
58
- }
59
- catch (err) {
60
- logger.error(`[${functionTag}] Initialization failed`, {
61
- message: "Error in initializing Mistral AI",
62
- modelName: this.modelName,
63
- error: err instanceof Error ? err.message : String(err),
64
- stack: err instanceof Error ? err.stack : undefined,
65
- });
66
- throw err;
67
- }
68
- }
69
- /**
70
- * Gets the appropriate model instance
71
- * @private
72
- */
73
- getModel() {
74
- logger.debug("MistralAI.getModel - Mistral AI model selected", {
75
- modelName: this.modelName,
76
- });
77
- return this.client(this.modelName);
78
- }
79
- /**
80
- * LEGACY METHOD: Use stream() instead for new code
81
- * @deprecated Use stream() method instead
82
- */
83
- /**
84
- * PRIMARY METHOD: Stream content using AI (recommended for new code)
85
- * Future-ready for multi-modal capabilities with current text focus
86
- */
87
- async stream(optionsOrPrompt, analysisSchema) {
88
- const functionTag = "MistralAI.stream";
89
- const provider = "mistral";
90
- let chunkCount = 0;
91
- const startTime = Date.now();
92
- try {
93
- // Parse parameters - support both string and options object
94
- const options = typeof optionsOrPrompt === "string"
95
- ? { input: { text: optionsOrPrompt } }
96
- : optionsOrPrompt;
97
- // Validate input
98
- if (!options?.input?.text ||
99
- typeof options.input.text !== "string" ||
100
- options.input.text.trim() === "") {
101
- throw new Error("Stream options must include input.text as a non-empty string");
102
- }
103
- // Extract parameters
104
- const { prompt = options.input.text, temperature = 0.7, maxTokens = DEFAULT_MAX_TOKENS, systemPrompt = DEFAULT_SYSTEM_CONTEXT.systemPrompt, schema, timeout = getDefaultTimeout(provider, "stream"), } = options;
105
- // Use schema from options or fallback parameter
106
- const finalSchema = schema || analysisSchema;
107
- logger.debug(`[${functionTag}] Stream request started`, {
108
- provider,
109
- modelName: this.modelName,
110
- promptLength: prompt.length,
111
- temperature,
112
- maxTokens,
113
- hasSchema: !!finalSchema,
114
- timeout,
115
- });
116
- const model = this.getModel();
117
- // Create timeout controller if timeout is specified
118
- const timeoutController = createTimeoutController(timeout, provider, "stream");
119
- const streamOptions = {
120
- model: model,
121
- prompt: prompt,
122
- system: systemPrompt,
123
- temperature,
124
- maxTokens,
125
- // Add abort signal if available
126
- ...(timeoutController && {
127
- abortSignal: timeoutController.controller.signal,
128
- }),
129
- onError: (event) => {
130
- const error = event.error;
131
- const errorMessage = error instanceof Error ? error.message : String(error);
132
- const errorStack = error instanceof Error ? error.stack : undefined;
133
- logger.debug(`[${functionTag}] Stream error`, {
134
- provider,
135
- modelName: this.modelName,
136
- error: errorMessage,
137
- stack: errorStack,
138
- promptLength: prompt.length,
139
- chunkCount,
140
- });
141
- },
142
- onFinish: (event) => {
143
- logger.debug(`[${functionTag}] Stream finished`, {
144
- provider,
145
- modelName: this.modelName,
146
- finishReason: event.finishReason,
147
- usage: event.usage,
148
- totalChunks: chunkCount,
149
- promptLength: prompt.length,
150
- responseLength: event.text?.length || 0,
151
- });
152
- },
153
- onChunk: (event) => {
154
- chunkCount++;
155
- logger.debug(`[${functionTag}] Stream chunk`, {
156
- provider,
157
- modelName: this.modelName,
158
- chunkNumber: chunkCount,
159
- chunkLength: event.chunk.text?.length || 0,
160
- chunkType: event.chunk.type,
161
- });
162
- },
163
- };
164
- if (finalSchema) {
165
- streamOptions.experimental_output = Output.object({
166
- schema: finalSchema,
167
- });
168
- }
169
- const result = streamText(streamOptions);
170
- logger.debug(`[${functionTag}] Stream request completed`, {
171
- provider,
172
- modelName: this.modelName,
173
- });
174
- // Convert to StreamResult format
175
- return {
176
- stream: (async function* () {
177
- for await (const chunk of result.textStream) {
178
- yield { content: chunk };
179
- }
180
- })(),
181
- provider: "mistral",
182
- model: this.modelName,
183
- metadata: {
184
- streamId: `mistral-${Date.now()}`,
185
- startTime,
186
- },
187
- };
188
- }
189
- catch (err) {
190
- // Log timeout errors specifically
191
- if (err instanceof TimeoutError) {
192
- logger.debug(`[${functionTag}] Timeout error`, {
193
- provider,
194
- modelName: this.modelName,
195
- timeout: err.timeout,
196
- message: err.message,
197
- });
198
- }
199
- else {
200
- logger.debug(`[${functionTag}] Exception`, {
201
- provider,
202
- modelName: this.modelName,
203
- message: "Error in streaming content",
204
- err: String(err),
205
- });
206
- }
207
- throw err; // Re-throw error to trigger fallback
208
- }
209
- }
210
- /**
211
- * Processes text using non-streaming approach with optional schema validation
212
- * @param prompt - The input text prompt to analyze
213
- * @param analysisSchema - Optional Zod schema or Schema object for output validation
214
- * @returns Promise resolving to GenerateResult or null if operation fails
215
- */
216
- async generate(optionsOrPrompt, analysisSchema) {
217
- const functionTag = "MistralAI.generate";
218
- const provider = "mistral";
219
- const startTime = Date.now();
220
- try {
221
- // Parse parameters - support both string and options object
222
- const options = typeof optionsOrPrompt === "string"
223
- ? { prompt: optionsOrPrompt }
224
- : optionsOrPrompt;
225
- const { prompt, temperature = 0.7, maxTokens = DEFAULT_MAX_TOKENS, systemPrompt = DEFAULT_SYSTEM_CONTEXT.systemPrompt, schema, timeout = getDefaultTimeout(provider, "generate"), } = options;
226
- // Use schema from options or fallback parameter
227
- const finalSchema = schema || analysisSchema;
228
- logger.debug(`[${functionTag}] Generate request started`, {
229
- provider,
230
- modelName: this.modelName,
231
- promptLength: prompt.length,
232
- temperature,
233
- maxTokens,
234
- timeout,
235
- });
236
- const model = this.getModel();
237
- // Create timeout controller if timeout is specified
238
- const timeoutController = createTimeoutController(timeout, provider, "generate");
239
- const generateOptions = {
240
- model: model,
241
- prompt: prompt,
242
- system: systemPrompt,
243
- temperature,
244
- maxTokens,
245
- // Add abort signal if available
246
- ...(timeoutController && {
247
- abortSignal: timeoutController.controller.signal,
248
- }),
249
- };
250
- if (finalSchema) {
251
- generateOptions.experimental_output = Output.object({
252
- schema: finalSchema,
253
- });
254
- }
255
- try {
256
- const result = await generateText(generateOptions);
257
- // Clean up timeout if successful
258
- timeoutController?.cleanup();
259
- logger.debug(`[${functionTag}] Generate text completed`, {
260
- provider,
261
- modelName: this.modelName,
262
- usage: result.usage,
263
- finishReason: result.finishReason,
264
- responseLength: result.text?.length || 0,
265
- timeout,
266
- });
267
- // Add analytics if enabled
268
- if (options.enableAnalytics) {
269
- result.analytics = {
270
- provider,
271
- model: this.modelName,
272
- tokens: result.usage,
273
- responseTime: Date.now() - startTime,
274
- context: options.context,
275
- };
276
- }
277
- // Add evaluation if enabled
278
- if (options.enableEvaluation) {
279
- result.evaluation = await evaluateResponse(prompt, result.text, options.context);
280
- }
281
- return {
282
- content: result.text,
283
- provider: "mistral",
284
- model: this.modelName,
285
- usage: result.usage
286
- ? {
287
- inputTokens: result.usage.promptTokens,
288
- outputTokens: result.usage.completionTokens,
289
- totalTokens: result.usage.totalTokens,
290
- }
291
- : undefined,
292
- responseTime: Date.now() - startTime,
293
- };
294
- }
295
- finally {
296
- // Always cleanup timeout
297
- timeoutController?.cleanup();
298
- }
299
- }
300
- catch (err) {
301
- // Log timeout errors specifically
302
- if (err instanceof TimeoutError) {
303
- logger.error(`[${functionTag}] Timeout error`, {
304
- provider,
305
- modelName: this.modelName,
306
- timeout: err.timeout,
307
- message: err.message,
308
- });
309
- }
310
- else {
311
- logger.error(`[${functionTag}] Exception`, {
312
- provider,
313
- modelName: this.modelName,
314
- message: "Error in generating text",
315
- err: String(err),
316
- });
317
- }
318
- throw err; // Re-throw error to trigger fallback
319
- }
320
- }
321
- async gen(optionsOrPrompt, analysisSchema) {
322
- return this.generate(optionsOrPrompt, analysisSchema);
323
- }
324
- }
325
- export default MistralAI;