@juspay/neurolink 5.0.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 (214) hide show
  1. package/CHANGELOG.md +20 -7
  2. package/README.md +160 -172
  3. package/dist/agent/direct-tools.d.ts +6 -6
  4. package/dist/chat/sse-handler.js +5 -4
  5. package/dist/chat/websocket-chat-handler.js +9 -9
  6. package/dist/cli/commands/config.d.ts +3 -3
  7. package/dist/cli/commands/mcp.js +9 -8
  8. package/dist/cli/commands/ollama.js +3 -3
  9. package/dist/cli/factories/command-factory.d.ts +18 -0
  10. package/dist/cli/factories/command-factory.js +183 -0
  11. package/dist/cli/index.js +105 -157
  12. package/dist/cli/utils/interactive-setup.js +2 -2
  13. package/dist/core/base-provider.d.ts +423 -0
  14. package/dist/core/base-provider.js +365 -0
  15. package/dist/core/constants.d.ts +1 -1
  16. package/dist/core/constants.js +1 -1
  17. package/dist/core/dynamic-models.d.ts +6 -6
  18. package/dist/core/evaluation.d.ts +19 -80
  19. package/dist/core/evaluation.js +185 -484
  20. package/dist/core/factory.d.ts +3 -3
  21. package/dist/core/factory.js +31 -91
  22. package/dist/core/service-registry.d.ts +47 -0
  23. package/dist/core/service-registry.js +112 -0
  24. package/dist/core/types.d.ts +49 -49
  25. package/dist/core/types.js +1 -0
  26. package/dist/factories/compatibility-factory.d.ts +20 -0
  27. package/dist/factories/compatibility-factory.js +69 -0
  28. package/dist/factories/provider-factory.d.ts +72 -0
  29. package/dist/factories/provider-factory.js +144 -0
  30. package/dist/factories/provider-generate-factory.d.ts +20 -0
  31. package/dist/factories/provider-generate-factory.js +87 -0
  32. package/dist/factories/provider-registry.d.ts +38 -0
  33. package/dist/factories/provider-registry.js +107 -0
  34. package/dist/index.d.ts +8 -5
  35. package/dist/index.js +5 -5
  36. package/dist/lib/agent/direct-tools.d.ts +6 -6
  37. package/dist/lib/chat/sse-handler.js +5 -4
  38. package/dist/lib/chat/websocket-chat-handler.js +9 -9
  39. package/dist/lib/core/base-provider.d.ts +423 -0
  40. package/dist/lib/core/base-provider.js +365 -0
  41. package/dist/lib/core/constants.d.ts +1 -1
  42. package/dist/lib/core/constants.js +1 -1
  43. package/dist/lib/core/dynamic-models.d.ts +6 -6
  44. package/dist/lib/core/evaluation.d.ts +19 -80
  45. package/dist/lib/core/evaluation.js +185 -484
  46. package/dist/lib/core/factory.d.ts +3 -3
  47. package/dist/lib/core/factory.js +30 -91
  48. package/dist/lib/core/service-registry.d.ts +47 -0
  49. package/dist/lib/core/service-registry.js +112 -0
  50. package/dist/lib/core/types.d.ts +49 -49
  51. package/dist/lib/core/types.js +1 -0
  52. package/dist/lib/factories/compatibility-factory.d.ts +20 -0
  53. package/dist/lib/factories/compatibility-factory.js +69 -0
  54. package/dist/lib/factories/provider-factory.d.ts +72 -0
  55. package/dist/lib/factories/provider-factory.js +144 -0
  56. package/dist/lib/factories/provider-generate-factory.d.ts +20 -0
  57. package/dist/lib/factories/provider-generate-factory.js +87 -0
  58. package/dist/lib/factories/provider-registry.d.ts +38 -0
  59. package/dist/lib/factories/provider-registry.js +107 -0
  60. package/dist/lib/index.d.ts +8 -5
  61. package/dist/lib/index.js +5 -5
  62. package/dist/lib/mcp/client.js +5 -5
  63. package/dist/lib/mcp/config.js +28 -3
  64. package/dist/lib/mcp/dynamic-orchestrator.js +8 -8
  65. package/dist/lib/mcp/external-client.js +2 -2
  66. package/dist/lib/mcp/factory.d.ts +1 -1
  67. package/dist/lib/mcp/factory.js +1 -1
  68. package/dist/lib/mcp/function-calling.js +1 -1
  69. package/dist/lib/mcp/initialize-tools.d.ts +1 -1
  70. package/dist/lib/mcp/initialize-tools.js +45 -1
  71. package/dist/lib/mcp/initialize.js +16 -6
  72. package/dist/lib/mcp/neurolink-mcp-client.js +10 -10
  73. package/dist/lib/mcp/orchestrator.js +4 -4
  74. package/dist/lib/mcp/servers/agent/direct-tools-server.d.ts +8 -0
  75. package/dist/lib/mcp/servers/agent/direct-tools-server.js +109 -0
  76. package/dist/lib/mcp/servers/ai-providers/ai-analysis-tools.js +10 -10
  77. package/dist/lib/mcp/servers/ai-providers/ai-core-server.js +8 -6
  78. package/dist/lib/mcp/servers/ai-providers/ai-workflow-tools.d.ts +2 -2
  79. package/dist/lib/mcp/servers/ai-providers/ai-workflow-tools.js +16 -16
  80. package/dist/lib/mcp/unified-registry.d.ts +4 -0
  81. package/dist/lib/mcp/unified-registry.js +42 -9
  82. package/dist/lib/neurolink.d.ts +161 -174
  83. package/dist/lib/neurolink.js +723 -397
  84. package/dist/lib/providers/amazon-bedrock.d.ts +32 -0
  85. package/dist/lib/providers/amazon-bedrock.js +143 -0
  86. package/dist/lib/providers/analytics-helper.js +7 -4
  87. package/dist/lib/providers/anthropic-baseprovider.d.ts +23 -0
  88. package/dist/lib/providers/anthropic-baseprovider.js +114 -0
  89. package/dist/lib/providers/anthropic.d.ts +19 -39
  90. package/dist/lib/providers/anthropic.js +84 -378
  91. package/dist/lib/providers/azure-openai.d.ts +20 -0
  92. package/dist/lib/providers/azure-openai.js +89 -0
  93. package/dist/lib/providers/function-calling-provider.d.ts +14 -12
  94. package/dist/lib/providers/function-calling-provider.js +114 -64
  95. package/dist/lib/providers/google-ai-studio.d.ts +23 -0
  96. package/dist/lib/providers/google-ai-studio.js +107 -0
  97. package/dist/lib/providers/google-vertex.d.ts +47 -0
  98. package/dist/lib/providers/google-vertex.js +205 -0
  99. package/dist/lib/providers/huggingFace.d.ts +33 -27
  100. package/dist/lib/providers/huggingFace.js +103 -400
  101. package/dist/lib/providers/index.d.ts +9 -9
  102. package/dist/lib/providers/index.js +9 -9
  103. package/dist/lib/providers/mcp-provider.d.ts +13 -8
  104. package/dist/lib/providers/mcp-provider.js +63 -18
  105. package/dist/lib/providers/mistral.d.ts +42 -0
  106. package/dist/lib/providers/mistral.js +160 -0
  107. package/dist/lib/providers/ollama.d.ts +52 -35
  108. package/dist/lib/providers/ollama.js +297 -477
  109. package/dist/lib/providers/openAI.d.ts +21 -21
  110. package/dist/lib/providers/openAI.js +81 -245
  111. package/dist/lib/sdk/tool-extension.d.ts +181 -0
  112. package/dist/lib/sdk/tool-extension.js +283 -0
  113. package/dist/lib/sdk/tool-registration.d.ts +95 -0
  114. package/dist/lib/sdk/tool-registration.js +167 -0
  115. package/dist/lib/types/generate-types.d.ts +80 -0
  116. package/dist/lib/types/generate-types.js +1 -0
  117. package/dist/lib/types/mcp-types.d.ts +116 -0
  118. package/dist/lib/types/mcp-types.js +5 -0
  119. package/dist/lib/types/stream-types.d.ts +95 -0
  120. package/dist/lib/types/stream-types.js +1 -0
  121. package/dist/lib/types/universal-provider-options.d.ts +87 -0
  122. package/dist/lib/types/universal-provider-options.js +53 -0
  123. package/dist/lib/utils/providerUtils-fixed.js +1 -1
  124. package/dist/lib/utils/streaming-utils.d.ts +14 -2
  125. package/dist/lib/utils/streaming-utils.js +0 -3
  126. package/dist/mcp/client.js +5 -5
  127. package/dist/mcp/config.js +28 -3
  128. package/dist/mcp/dynamic-orchestrator.js +8 -8
  129. package/dist/mcp/external-client.js +2 -2
  130. package/dist/mcp/factory.d.ts +1 -1
  131. package/dist/mcp/factory.js +1 -1
  132. package/dist/mcp/function-calling.js +1 -1
  133. package/dist/mcp/initialize-tools.d.ts +1 -1
  134. package/dist/mcp/initialize-tools.js +45 -1
  135. package/dist/mcp/initialize.js +16 -6
  136. package/dist/mcp/neurolink-mcp-client.js +10 -10
  137. package/dist/mcp/orchestrator.js +4 -4
  138. package/dist/mcp/servers/agent/direct-tools-server.d.ts +8 -0
  139. package/dist/mcp/servers/agent/direct-tools-server.js +109 -0
  140. package/dist/mcp/servers/ai-providers/ai-analysis-tools.js +10 -10
  141. package/dist/mcp/servers/ai-providers/ai-core-server.js +8 -6
  142. package/dist/mcp/servers/ai-providers/ai-workflow-tools.d.ts +2 -2
  143. package/dist/mcp/servers/ai-providers/ai-workflow-tools.js +16 -16
  144. package/dist/mcp/unified-registry.d.ts +4 -0
  145. package/dist/mcp/unified-registry.js +42 -9
  146. package/dist/neurolink.d.ts +161 -174
  147. package/dist/neurolink.js +723 -397
  148. package/dist/providers/amazon-bedrock.d.ts +32 -0
  149. package/dist/providers/amazon-bedrock.js +143 -0
  150. package/dist/providers/analytics-helper.js +7 -4
  151. package/dist/providers/anthropic-baseprovider.d.ts +23 -0
  152. package/dist/providers/anthropic-baseprovider.js +114 -0
  153. package/dist/providers/anthropic.d.ts +19 -39
  154. package/dist/providers/anthropic.js +83 -377
  155. package/dist/providers/azure-openai.d.ts +20 -0
  156. package/dist/providers/azure-openai.js +89 -0
  157. package/dist/providers/function-calling-provider.d.ts +14 -12
  158. package/dist/providers/function-calling-provider.js +114 -64
  159. package/dist/providers/google-ai-studio.d.ts +23 -0
  160. package/dist/providers/google-ai-studio.js +108 -0
  161. package/dist/providers/google-vertex.d.ts +47 -0
  162. package/dist/providers/google-vertex.js +205 -0
  163. package/dist/providers/huggingFace.d.ts +33 -27
  164. package/dist/providers/huggingFace.js +102 -399
  165. package/dist/providers/index.d.ts +9 -9
  166. package/dist/providers/index.js +9 -9
  167. package/dist/providers/mcp-provider.d.ts +13 -8
  168. package/dist/providers/mcp-provider.js +63 -18
  169. package/dist/providers/mistral.d.ts +42 -0
  170. package/dist/providers/mistral.js +160 -0
  171. package/dist/providers/ollama.d.ts +52 -35
  172. package/dist/providers/ollama.js +297 -476
  173. package/dist/providers/openAI.d.ts +21 -21
  174. package/dist/providers/openAI.js +81 -246
  175. package/dist/sdk/tool-extension.d.ts +181 -0
  176. package/dist/sdk/tool-extension.js +283 -0
  177. package/dist/sdk/tool-registration.d.ts +95 -0
  178. package/dist/sdk/tool-registration.js +168 -0
  179. package/dist/types/generate-types.d.ts +80 -0
  180. package/dist/types/generate-types.js +1 -0
  181. package/dist/types/mcp-types.d.ts +116 -0
  182. package/dist/types/mcp-types.js +5 -0
  183. package/dist/types/stream-types.d.ts +95 -0
  184. package/dist/types/stream-types.js +1 -0
  185. package/dist/types/universal-provider-options.d.ts +87 -0
  186. package/dist/types/universal-provider-options.js +53 -0
  187. package/dist/utils/providerUtils-fixed.js +1 -1
  188. package/dist/utils/streaming-utils.d.ts +14 -2
  189. package/dist/utils/streaming-utils.js +0 -3
  190. package/package.json +15 -10
  191. package/dist/lib/providers/agent-enhanced-provider.d.ts +0 -89
  192. package/dist/lib/providers/agent-enhanced-provider.js +0 -614
  193. package/dist/lib/providers/amazonBedrock.d.ts +0 -19
  194. package/dist/lib/providers/amazonBedrock.js +0 -334
  195. package/dist/lib/providers/azureOpenAI.d.ts +0 -39
  196. package/dist/lib/providers/azureOpenAI.js +0 -436
  197. package/dist/lib/providers/googleAIStudio.d.ts +0 -49
  198. package/dist/lib/providers/googleAIStudio.js +0 -333
  199. package/dist/lib/providers/googleVertexAI.d.ts +0 -38
  200. package/dist/lib/providers/googleVertexAI.js +0 -519
  201. package/dist/lib/providers/mistralAI.d.ts +0 -34
  202. package/dist/lib/providers/mistralAI.js +0 -294
  203. package/dist/providers/agent-enhanced-provider.d.ts +0 -89
  204. package/dist/providers/agent-enhanced-provider.js +0 -614
  205. package/dist/providers/amazonBedrock.d.ts +0 -19
  206. package/dist/providers/amazonBedrock.js +0 -334
  207. package/dist/providers/azureOpenAI.d.ts +0 -39
  208. package/dist/providers/azureOpenAI.js +0 -437
  209. package/dist/providers/googleAIStudio.d.ts +0 -49
  210. package/dist/providers/googleAIStudio.js +0 -333
  211. package/dist/providers/googleVertexAI.d.ts +0 -38
  212. package/dist/providers/googleVertexAI.js +0 -519
  213. package/dist/providers/mistralAI.d.ts +0 -34
  214. package/dist/providers/mistralAI.js +0 -294
@@ -2,9 +2,11 @@
2
2
  * NeuroLink MCP-Aware AI Provider
3
3
  * Integrates MCP tools with AI providers following Lighthouse's pattern
4
4
  */
5
- import type { AIProvider, TextGenerationOptions, StreamTextOptions, EnhancedGenerateTextResult } from "../core/types.js";
6
- import type { StreamTextResult, ToolSet, Schema, GenerateTextResult } from "ai";
5
+ import type { AIProvider, TextGenerationOptions, EnhancedGenerateResult } from "../core/types.js";
6
+ import type { Schema } from "ai";
7
+ import type { GenerateResult } from "../types/generate-types.js";
7
8
  import type { ZodType, ZodTypeDef } from "zod";
9
+ import type { StreamOptions, StreamResult } from "../types/stream-types.js";
8
10
  /**
9
11
  * MCP-Aware Provider Configuration
10
12
  */
@@ -31,8 +33,12 @@ export declare class MCPAwareProvider implements AIProvider {
31
33
  * Initialize MCP tools for this session
32
34
  */
33
35
  private initializeMCP;
34
- generateText(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<GenerateTextResult<ToolSet, unknown> | null>;
35
- streamText(optionsOrPrompt: StreamTextOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<StreamTextResult<ToolSet, unknown> | null>;
36
+ /**
37
+ * PRIMARY METHOD: Stream content using AI (recommended for new code)
38
+ * Future-ready for multi-modal capabilities with current text focus
39
+ */
40
+ stream(optionsOrPrompt: StreamOptions | string, analysisSchema?: any): Promise<StreamResult>;
41
+ generate(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<GenerateResult>;
36
42
  /**
37
43
  * Detect if the prompt is requesting tool usage
38
44
  */
@@ -52,13 +58,12 @@ export declare class MCPAwareProvider implements AIProvider {
52
58
  */
53
59
  cleanup(): Promise<void>;
54
60
  /**
55
- * Alias for generateText() - CLI-SDK consistency
61
+ * Alias for generate() - CLI-SDK consistency
56
62
  */
57
- generate(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: any): Promise<EnhancedGenerateTextResult | null>;
58
63
  /**
59
- * Short alias for generateText() - CLI-SDK consistency
64
+ * Short alias for generate() - CLI-SDK consistency
60
65
  */
61
- gen(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: any): Promise<EnhancedGenerateTextResult | null>;
66
+ gen(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: any): Promise<EnhancedGenerateResult | null>;
62
67
  }
63
68
  /**
64
69
  * Create an MCP-aware provider
@@ -83,13 +83,50 @@ export class MCPAwareProvider {
83
83
  // Continue without MCP tools if initialization fails
84
84
  }
85
85
  }
86
- async generateText(optionsOrPrompt, analysisSchema) {
86
+ /**
87
+ * PRIMARY METHOD: Stream content using AI (recommended for new code)
88
+ * Future-ready for multi-modal capabilities with current text focus
89
+ */
90
+ async stream(optionsOrPrompt, analysisSchema) {
91
+ const functionTag = "MCPAwareProvider.stream";
92
+ const startTime = Date.now();
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
+ // Use base provider's stream implementation
104
+ const baseResult = await this.baseProvider.stream(options);
105
+ if (!baseResult) {
106
+ throw new Error("No stream response received from provider");
107
+ }
108
+ // Return the result with MCP metadata
109
+ return {
110
+ ...baseResult,
111
+ provider: "mcp",
112
+ model: options.model || "unknown",
113
+ metadata: {
114
+ streamId: `mcp-${Date.now()}`,
115
+ startTime,
116
+ },
117
+ };
118
+ }
119
+ async generate(optionsOrPrompt, analysisSchema) {
87
120
  // Ensure MCP is initialized
88
121
  await this.initializeMCP();
89
122
  // Parse options
90
123
  const options = typeof optionsOrPrompt === "string"
91
124
  ? { prompt: optionsOrPrompt }
92
125
  : optionsOrPrompt;
126
+ // Validate prompt is provided
127
+ if (!options.prompt || options.prompt.trim() === "") {
128
+ throw new Error("MCP Provider requires a valid prompt. Please provide a non-empty prompt string.");
129
+ }
93
130
  // Check if prompt requests tool usage
94
131
  const needsTools = this.detectToolRequest(options.prompt);
95
132
  if (needsTools && this.mcpInitialized) {
@@ -114,15 +151,19 @@ PARAMS: <json_params>
114
151
 
115
152
  Otherwise, provide a direct response.`;
116
153
  // Generate response with enhanced prompt
117
- const response = await this.baseProvider.generateText({
154
+ const response = await this.baseProvider.generate({
118
155
  ...options,
119
156
  prompt: enhancedPrompt,
120
157
  }, analysisSchema);
121
158
  if (!response) {
122
- return null;
159
+ return {
160
+ content: "No response generated",
161
+ provider: "mcp",
162
+ model: "unknown",
163
+ };
123
164
  }
124
165
  // Check if response includes tool invocation
125
- const toolMatch = response.text.match(/TOOL:\s*(\S+)\s*\nPARAMS:\s*({.*})/s);
166
+ const toolMatch = response.content.match(/TOOL:\s*(\S+)\s*\nPARAMS:\s*({.*})/s);
126
167
  if (toolMatch) {
127
168
  const toolName = toolMatch[1];
128
169
  const toolParams = JSON.parse(toolMatch[2]);
@@ -135,12 +176,16 @@ Tool ${toolName} was executed with result:
135
176
  ${JSON.stringify(toolResult, null, 2)}
136
177
 
137
178
  Please provide a response based on this information.`;
138
- const finalResponse = await this.baseProvider.generateText({
179
+ const finalResponse = await this.baseProvider.generate({
139
180
  ...options,
140
181
  prompt: finalPrompt,
141
182
  }, analysisSchema);
142
183
  if (!finalResponse) {
143
- return null;
184
+ return {
185
+ content: "Tool execution failed",
186
+ provider: "mcp",
187
+ model: "unknown",
188
+ };
144
189
  }
145
190
  // Return response (tool usage is tracked internally)
146
191
  return finalResponse;
@@ -148,12 +193,15 @@ Please provide a response based on this information.`;
148
193
  return response;
149
194
  }
150
195
  // Regular generation without tools
151
- return this.baseProvider.generateText(options);
152
- }
153
- async streamText(optionsOrPrompt, analysisSchema) {
154
- // For now, streaming doesn't support tool usage
155
- // This matches Lighthouse's approach where MCP is used for non-streaming requests
156
- return this.baseProvider.streamText(optionsOrPrompt, analysisSchema);
196
+ const result = await this.baseProvider.generate(options);
197
+ if (!result) {
198
+ return {
199
+ content: "Base provider returned no result",
200
+ provider: "mcp",
201
+ model: "unknown",
202
+ };
203
+ }
204
+ return result;
157
205
  }
158
206
  /**
159
207
  * Detect if the prompt is requesting tool usage
@@ -193,16 +241,13 @@ Please provide a response based on this information.`;
193
241
  }
194
242
  }
195
243
  /**
196
- * Alias for generateText() - CLI-SDK consistency
244
+ * Alias for generate() - CLI-SDK consistency
197
245
  */
198
- async generate(optionsOrPrompt, analysisSchema) {
199
- return this.generateText(optionsOrPrompt, analysisSchema);
200
- }
201
246
  /**
202
- * Short alias for generateText() - CLI-SDK consistency
247
+ * Short alias for generate() - CLI-SDK consistency
203
248
  */
204
249
  async gen(optionsOrPrompt, analysisSchema) {
205
- return this.generateText(optionsOrPrompt, analysisSchema);
250
+ return this.generate(optionsOrPrompt, analysisSchema);
206
251
  }
207
252
  }
208
253
  /**
@@ -0,0 +1,42 @@
1
+ import type { ZodType, ZodTypeDef } from "zod";
2
+ import { type Schema, type LanguageModelV1 } from "ai";
3
+ import type { AIProviderName } from "../core/types.js";
4
+ import type { StreamOptions, StreamResult } from "../types/stream-types.js";
5
+ import { BaseProvider } from "../core/base-provider.js";
6
+ /**
7
+ * Mistral AI Provider v2 - BaseProvider Implementation
8
+ *
9
+ * PHASE 3.6: Simple BaseProvider wrap around existing @ai-sdk/mistral implementation
10
+ *
11
+ * Features:
12
+ * - Extends BaseProvider for shared functionality
13
+ * - Uses pre-configured Mistral instance for efficiency
14
+ * - Enhanced error handling with setup guidance
15
+ * - Supports all Mistral models (mistral-small, mistral-medium, mistral-large)
16
+ */
17
+ export declare class MistralProvider extends BaseProvider {
18
+ private mistral;
19
+ private model;
20
+ constructor(modelName?: string, sdk?: any);
21
+ protected getProviderName(): AIProviderName;
22
+ protected getDefaultModel(): string;
23
+ /**
24
+ * Returns the Vercel AI SDK model instance for Mistral
25
+ */
26
+ protected getAISDKModel(): LanguageModelV1;
27
+ protected executeStream(options: StreamOptions, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<StreamResult>;
28
+ protected handleProviderError(error: any): Error;
29
+ private validateStreamOptions;
30
+ /**
31
+ * Check available Mistral models
32
+ * @returns Array of available model names
33
+ */
34
+ getAvailableModels(): string[];
35
+ /**
36
+ * Get recommended model based on use case
37
+ * @param useCase - The intended use case
38
+ * @returns Recommended model name
39
+ */
40
+ getRecommendedModel(useCase: "speed" | "balanced" | "quality"): string;
41
+ }
42
+ export default MistralProvider;
@@ -0,0 +1,160 @@
1
+ import { createMistral } from "@ai-sdk/mistral";
2
+ import { streamText } from "ai";
3
+ import { BaseProvider } from "../core/base-provider.js";
4
+ import { logger } from "../utils/logger.js";
5
+ import { TimeoutError, } from "../utils/timeout.js";
6
+ import { DEFAULT_MAX_TOKENS } from "../core/constants.js";
7
+ // Configuration helpers
8
+ const getMistralApiKey = () => {
9
+ const apiKey = process.env.MISTRAL_API_KEY;
10
+ if (!apiKey) {
11
+ throw new Error(`❌ Mistral AI Provider Configuration Error\n\nMissing required environment variable: MISTRAL_API_KEY\n\n🔧 Step 1: Get Mistral AI API Key\n1. Visit: https://console.mistral.ai/\n2. Sign in or create an account\n3. Go to API Keys section\n4. Create a new API key\n\n🔧 Step 2: Set Environment Variable\nAdd to your .env file:\nMISTRAL_API_KEY=your_api_key_here\n\n🔧 Step 3: Restart Application\nRestart your application to load the new environment variables.`);
12
+ }
13
+ return apiKey;
14
+ };
15
+ const getDefaultMistralModel = () => {
16
+ return process.env.MISTRAL_MODEL || "mistral-small";
17
+ };
18
+ const hasMistralCredentials = () => {
19
+ return !!process.env.MISTRAL_API_KEY;
20
+ };
21
+ /**
22
+ * Mistral AI Provider v2 - BaseProvider Implementation
23
+ *
24
+ * PHASE 3.6: Simple BaseProvider wrap around existing @ai-sdk/mistral implementation
25
+ *
26
+ * Features:
27
+ * - Extends BaseProvider for shared functionality
28
+ * - Uses pre-configured Mistral instance for efficiency
29
+ * - Enhanced error handling with setup guidance
30
+ * - Supports all Mistral models (mistral-small, mistral-medium, mistral-large)
31
+ */
32
+ export class MistralProvider extends BaseProvider {
33
+ mistral;
34
+ model;
35
+ constructor(modelName, sdk) {
36
+ super(modelName, "mistral", sdk);
37
+ // Validate Mistral API credentials
38
+ if (!hasMistralCredentials()) {
39
+ throw new Error(`❌ Mistral AI Provider Configuration Error\n\nMissing Mistral AI API key.\n\n🔧 Required Environment Variable:\nMISTRAL_API_KEY=your_api_key_here\n\n🔧 Get API Key:\n1. Visit: https://console.mistral.ai/\n2. Sign in or create account\n3. Generate API key\n4. Add to .env file\n\n🔧 Restart Application\nRestart your application to load the new environment variables.`);
40
+ }
41
+ // Initialize Mistral provider
42
+ this.mistral = createMistral({
43
+ apiKey: getMistralApiKey(),
44
+ });
45
+ // Pre-initialize model for efficiency
46
+ this.model = this.mistral(this.modelName || getDefaultMistralModel());
47
+ logger.debug("Mistral AI BaseProvider v2 initialized", {
48
+ modelName: this.modelName,
49
+ provider: this.providerName,
50
+ });
51
+ }
52
+ getProviderName() {
53
+ return "mistral";
54
+ }
55
+ getDefaultModel() {
56
+ return getDefaultMistralModel();
57
+ }
58
+ /**
59
+ * Returns the Vercel AI SDK model instance for Mistral
60
+ */
61
+ getAISDKModel() {
62
+ return this.model;
63
+ }
64
+ // executeGenerate removed - BaseProvider handles all generation with tools
65
+ async executeStream(options, analysisSchema) {
66
+ try {
67
+ this.validateStreamOptions(options);
68
+ const result = await streamText({
69
+ model: this.model,
70
+ prompt: options.input.text,
71
+ system: options.systemPrompt,
72
+ maxTokens: options.maxTokens || DEFAULT_MAX_TOKENS,
73
+ temperature: options.temperature,
74
+ });
75
+ return {
76
+ stream: (async function* () {
77
+ for await (const chunk of result.textStream) {
78
+ yield { content: chunk };
79
+ }
80
+ })(),
81
+ provider: this.providerName,
82
+ model: this.modelName,
83
+ };
84
+ }
85
+ catch (error) {
86
+ throw this.handleProviderError(error);
87
+ }
88
+ }
89
+ handleProviderError(error) {
90
+ if (error.name === "TimeoutError") {
91
+ return new TimeoutError(`Mistral AI request timed out. Consider increasing timeout or using a lighter model.`, this.defaultTimeout);
92
+ }
93
+ if (error.message?.includes("401") ||
94
+ error.message?.includes("Unauthorized")) {
95
+ return new Error(`❌ Mistral AI Authentication Error\n\nYour API key is invalid or expired.\n\n🔧 Steps to Fix:\n1. Check your MISTRAL_API_KEY in .env file\n2. Verify the API key is correct and active\n3. Generate a new API key if needed at https://console.mistral.ai/\n4. Restart your application after updating`);
96
+ }
97
+ if (error.message?.includes("403") ||
98
+ error.message?.includes("Forbidden")) {
99
+ return new Error(`❌ Mistral AI Access Denied\n\nYour account doesn't have permission to access this model.\n\n🔧 Possible Solutions:\n1. Check if your account has access to the model: ${this.modelName}\n2. Try a different model (e.g., 'mistral-small')\n3. Verify your subscription status\n4. Contact Mistral AI support if needed`);
100
+ }
101
+ if (error.message?.includes("429") ||
102
+ error.message?.includes("rate limit")) {
103
+ return new Error(`❌ Mistral AI Rate Limit Exceeded\n\n${error.message}\n\n🔧 Solutions:\n1. Wait a moment before retrying\n2. Reduce request frequency\n3. Check your usage quotas\n4. Consider upgrading your plan`);
104
+ }
105
+ if (error.message?.includes("400") ||
106
+ error.message?.includes("Bad Request")) {
107
+ return new Error(`❌ Mistral AI Invalid Request\n\n${error.message}\n\n🔧 Check:\n1. Input text is properly formatted\n2. Model name is correct: ${this.modelName}\n3. Parameters are within limits\n4. Request format matches API requirements`);
108
+ }
109
+ if (error.message?.includes("404") ||
110
+ error.message?.includes("Not Found")) {
111
+ return new Error(`❌ Mistral AI Model Not Found\n\nModel '${this.modelName}' is not available.\n\n🔧 Available Models:\n- mistral-small (fastest, cost-effective)\n- mistral-medium (balanced performance)\n- mistral-large (highest quality)\n\n🔧 Fix: Update MISTRAL_MODEL environment variable`);
112
+ }
113
+ return new Error(`❌ Mistral AI Provider Error\n\n${error.message || "Unknown error occurred"}\n\n🔧 Troubleshooting:\n1. Check API key and network connectivity\n2. Verify model availability\n3. Review request parameters\n4. Check Mistral AI status page`);
114
+ }
115
+ validateStreamOptions(options) {
116
+ if (!options.input?.text?.trim()) {
117
+ throw new Error("Prompt is required for streaming");
118
+ }
119
+ if (options.maxTokens &&
120
+ (options.maxTokens < 1 || options.maxTokens > 32768)) {
121
+ throw new Error("maxTokens must be between 1 and 32768 for Mistral AI");
122
+ }
123
+ if (options.temperature &&
124
+ (options.temperature < 0 || options.temperature > 1)) {
125
+ throw new Error("temperature must be between 0 and 1");
126
+ }
127
+ }
128
+ /**
129
+ * Check available Mistral models
130
+ * @returns Array of available model names
131
+ */
132
+ getAvailableModels() {
133
+ return [
134
+ "mistral-small",
135
+ "mistral-medium",
136
+ "mistral-large",
137
+ "mistral-7b-instruct",
138
+ "mistral-8x7b-instruct",
139
+ "mistral-8x22b-instruct",
140
+ ];
141
+ }
142
+ /**
143
+ * Get recommended model based on use case
144
+ * @param useCase - The intended use case
145
+ * @returns Recommended model name
146
+ */
147
+ getRecommendedModel(useCase) {
148
+ switch (useCase) {
149
+ case "speed":
150
+ return "mistral-small";
151
+ case "balanced":
152
+ return "mistral-medium";
153
+ case "quality":
154
+ return "mistral-large";
155
+ default:
156
+ return "mistral-small";
157
+ }
158
+ }
159
+ }
160
+ export default MistralProvider;
@@ -1,53 +1,70 @@
1
+ import type { AIProviderName } from "../core/types.js";
2
+ import type { LanguageModelV1 } from "ai";
3
+ import type { StreamOptions, StreamResult } from "../types/stream-types.js";
4
+ import type { ZodType, ZodTypeDef } from "zod";
5
+ import type { Schema } from "ai";
6
+ import { BaseProvider } from "../core/base-provider.js";
1
7
  /**
2
- * Ollama Provider for NeuroLink
8
+ * Ollama Provider v2 - BaseProvider Implementation
3
9
  *
4
- * Local AI model deployment and management using Ollama.
5
- * Provides offline AI capabilities with local model hosting.
10
+ * PHASE 3.7: BaseProvider wrap around existing custom Ollama implementation
6
11
  *
7
12
  * Features:
8
- * - Local model deployment (privacy-first)
9
- * - Model management (download, list, remove)
10
- * - Health checking and service validation
11
- * - Streaming and non-streaming text generation
13
+ * - Extends BaseProvider for shared functionality
14
+ * - Preserves custom OllamaLanguageModel implementation
15
+ * - Local model management and health checking
16
+ * - Enhanced error handling with Ollama-specific guidance
12
17
  */
13
- import type { AIProvider, TextGenerationOptions, StreamTextOptions, EnhancedGenerateTextResult } from "../core/types.js";
14
- import type { GenerateTextResult, StreamTextResult, ToolSet } from "ai";
15
- import type { ZodType, ZodTypeDef } from "zod";
16
- import type { Schema } from "ai";
17
- export declare class Ollama implements AIProvider {
18
+ export declare class OllamaProvider extends BaseProvider {
19
+ private ollamaModel;
18
20
  private baseUrl;
19
- private modelName;
20
- private defaultTimeout;
21
+ private timeout;
21
22
  constructor(modelName?: string);
23
+ protected getProviderName(): AIProviderName;
24
+ protected getDefaultModel(): string;
22
25
  /**
23
- * Gets the appropriate model instance
24
- * @private
25
- */
26
- private getModel;
27
- /**
28
- * Health check - verify Ollama service is running and accessible
26
+ * Returns the Vercel AI SDK model instance for Ollama
29
27
  */
30
- checkHealth(): Promise<boolean>;
28
+ protected getAISDKModel(): LanguageModelV1;
31
29
  /**
32
- * List available models on the Ollama instance
30
+ * Ollama tool/function calling support is currently disabled due to integration issues.
31
+ *
32
+ * **Current Issues:**
33
+ * 1. The OllamaLanguageModel from @ai-sdk/provider-utils doesn't properly integrate
34
+ * with BaseProvider's tool calling mechanism
35
+ * 2. Ollama models require specific prompt formatting for function calls that differs
36
+ * from the standardized AI SDK format
37
+ * 3. Tool response parsing and execution flow needs custom implementation
38
+ *
39
+ * **What's needed to enable tool support:**
40
+ * - Create a custom OllamaLanguageModel wrapper that handles tool schema formatting
41
+ * - Implement Ollama-specific tool calling prompt templates
42
+ * - Add proper response parsing for Ollama's function call format
43
+ * - Test with models that support function calling (llama3.1, mistral, etc.)
44
+ *
45
+ * **Tracking:**
46
+ * - See BaseProvider tool integration patterns in other providers
47
+ * - Monitor Ollama function calling documentation: https://ollama.com/blog/tool-support
48
+ * - Track AI SDK updates for better Ollama integration
49
+ *
50
+ * @returns false to disable tools by default
33
51
  */
34
- listModels(): Promise<string[]>;
52
+ supportsTools(): boolean;
53
+ protected executeStream(options: StreamOptions, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<StreamResult>;
54
+ private createOllamaStream;
55
+ protected handleProviderError(error: any): Error;
56
+ private validateStreamOptions;
35
57
  /**
36
- * Check if a specific model is available
37
- */
38
- isModelAvailable(modelName: string): Promise<boolean>;
39
- /**
40
- * Pull/download a model to the local Ollama instance
58
+ * Check if Ollama service is healthy and accessible
41
59
  */
42
- pullModel(modelName: string): Promise<void>;
60
+ private checkOllamaHealth;
43
61
  /**
44
- * Generate text using Ollama local models
62
+ * Get available models from Ollama
45
63
  */
46
- generateText(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<GenerateTextResult<ToolSet, unknown> | null>;
64
+ getAvailableModels(): Promise<string[]>;
47
65
  /**
48
- * Generate streaming text using Ollama local models
66
+ * Check if a specific model is available
49
67
  */
50
- streamText(optionsOrPrompt: StreamTextOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<StreamTextResult<ToolSet, unknown> | null>;
51
- generate(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: any): Promise<EnhancedGenerateTextResult | null>;
52
- gen(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: any): Promise<EnhancedGenerateTextResult | null>;
68
+ isModelAvailable(modelName: string): Promise<boolean>;
53
69
  }
70
+ export default OllamaProvider;