@juspay/neurolink 5.1.0 → 5.3.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 (190) hide show
  1. package/CHANGELOG.md +21 -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 +63 -8
  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 +376 -0
  11. package/dist/core/constants.d.ts +2 -1
  12. package/dist/core/constants.js +2 -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 +376 -0
  31. package/dist/lib/core/constants.d.ts +2 -1
  32. package/dist/lib/core/constants.js +2 -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/client.d.ts +1 -0
  49. package/dist/lib/mcp/client.js +1 -0
  50. package/dist/lib/mcp/config.js +28 -3
  51. package/dist/lib/mcp/context-manager.d.ts +1 -0
  52. package/dist/lib/mcp/context-manager.js +8 -4
  53. package/dist/lib/mcp/function-calling.d.ts +13 -0
  54. package/dist/lib/mcp/function-calling.js +134 -35
  55. package/dist/lib/mcp/initialize-tools.d.ts +1 -1
  56. package/dist/lib/mcp/initialize-tools.js +45 -1
  57. package/dist/lib/mcp/initialize.js +16 -6
  58. package/dist/lib/mcp/neurolink-mcp-client.d.ts +1 -0
  59. package/dist/lib/mcp/neurolink-mcp-client.js +21 -5
  60. package/dist/lib/mcp/servers/agent/direct-tools-server.d.ts +8 -0
  61. package/dist/lib/mcp/servers/agent/direct-tools-server.js +109 -0
  62. package/dist/lib/mcp/servers/ai-providers/ai-core-server.js +3 -1
  63. package/dist/lib/mcp/servers/ai-providers/ai-workflow-tools.d.ts +2 -2
  64. package/dist/lib/mcp/unified-registry.d.ts +4 -0
  65. package/dist/lib/mcp/unified-registry.js +42 -9
  66. package/dist/lib/neurolink.d.ts +156 -117
  67. package/dist/lib/neurolink.js +619 -404
  68. package/dist/lib/providers/amazon-bedrock.d.ts +32 -0
  69. package/dist/lib/providers/amazon-bedrock.js +143 -0
  70. package/dist/lib/providers/analytics-helper.js +7 -4
  71. package/dist/lib/providers/anthropic-baseprovider.d.ts +23 -0
  72. package/dist/lib/providers/anthropic-baseprovider.js +114 -0
  73. package/dist/lib/providers/anthropic.d.ts +19 -43
  74. package/dist/lib/providers/anthropic.js +82 -306
  75. package/dist/lib/providers/azure-openai.d.ts +20 -0
  76. package/dist/lib/providers/azure-openai.js +89 -0
  77. package/dist/lib/providers/function-calling-provider.d.ts +64 -2
  78. package/dist/lib/providers/function-calling-provider.js +208 -9
  79. package/dist/lib/providers/google-ai-studio.d.ts +23 -0
  80. package/dist/lib/providers/google-ai-studio.js +107 -0
  81. package/dist/lib/providers/google-vertex.d.ts +47 -0
  82. package/dist/lib/providers/google-vertex.js +205 -0
  83. package/dist/lib/providers/huggingFace.d.ts +32 -25
  84. package/dist/lib/providers/huggingFace.js +97 -431
  85. package/dist/lib/providers/index.d.ts +9 -9
  86. package/dist/lib/providers/index.js +9 -9
  87. package/dist/lib/providers/mcp-provider.js +24 -5
  88. package/dist/lib/providers/mistral.d.ts +42 -0
  89. package/dist/lib/providers/mistral.js +160 -0
  90. package/dist/lib/providers/ollama.d.ts +52 -36
  91. package/dist/lib/providers/ollama.js +297 -520
  92. package/dist/lib/providers/openAI.d.ts +19 -18
  93. package/dist/lib/providers/openAI.js +76 -275
  94. package/dist/lib/sdk/tool-extension.d.ts +181 -0
  95. package/dist/lib/sdk/tool-extension.js +283 -0
  96. package/dist/lib/sdk/tool-registration.d.ts +95 -0
  97. package/dist/lib/sdk/tool-registration.js +167 -0
  98. package/dist/lib/services/streaming/streaming-manager.js +11 -10
  99. package/dist/lib/services/websocket/websocket-server.js +12 -11
  100. package/dist/lib/telemetry/telemetry-service.js +8 -7
  101. package/dist/lib/types/generate-types.d.ts +1 -0
  102. package/dist/lib/types/mcp-types.d.ts +116 -0
  103. package/dist/lib/types/mcp-types.js +5 -0
  104. package/dist/lib/types/stream-types.d.ts +30 -18
  105. package/dist/lib/types/universal-provider-options.d.ts +87 -0
  106. package/dist/lib/types/universal-provider-options.js +53 -0
  107. package/dist/mcp/client.d.ts +1 -0
  108. package/dist/mcp/client.js +1 -0
  109. package/dist/mcp/config.js +28 -3
  110. package/dist/mcp/context-manager.d.ts +1 -0
  111. package/dist/mcp/context-manager.js +8 -4
  112. package/dist/mcp/function-calling.d.ts +13 -0
  113. package/dist/mcp/function-calling.js +134 -35
  114. package/dist/mcp/initialize-tools.d.ts +1 -1
  115. package/dist/mcp/initialize-tools.js +45 -1
  116. package/dist/mcp/initialize.js +16 -6
  117. package/dist/mcp/neurolink-mcp-client.d.ts +1 -0
  118. package/dist/mcp/neurolink-mcp-client.js +21 -5
  119. package/dist/mcp/servers/agent/direct-tools-server.d.ts +8 -0
  120. package/dist/mcp/servers/agent/direct-tools-server.js +109 -0
  121. package/dist/mcp/servers/ai-providers/ai-core-server.js +3 -1
  122. package/dist/mcp/servers/ai-providers/ai-workflow-tools.d.ts +2 -2
  123. package/dist/mcp/unified-registry.d.ts +4 -0
  124. package/dist/mcp/unified-registry.js +42 -9
  125. package/dist/neurolink.d.ts +156 -117
  126. package/dist/neurolink.js +619 -404
  127. package/dist/providers/amazon-bedrock.d.ts +32 -0
  128. package/dist/providers/amazon-bedrock.js +143 -0
  129. package/dist/providers/analytics-helper.js +7 -4
  130. package/dist/providers/anthropic-baseprovider.d.ts +23 -0
  131. package/dist/providers/anthropic-baseprovider.js +114 -0
  132. package/dist/providers/anthropic.d.ts +19 -43
  133. package/dist/providers/anthropic.js +81 -305
  134. package/dist/providers/azure-openai.d.ts +20 -0
  135. package/dist/providers/azure-openai.js +89 -0
  136. package/dist/providers/function-calling-provider.d.ts +64 -2
  137. package/dist/providers/function-calling-provider.js +208 -9
  138. package/dist/providers/google-ai-studio.d.ts +23 -0
  139. package/dist/providers/google-ai-studio.js +108 -0
  140. package/dist/providers/google-vertex.d.ts +47 -0
  141. package/dist/providers/google-vertex.js +205 -0
  142. package/dist/providers/huggingFace.d.ts +32 -25
  143. package/dist/providers/huggingFace.js +96 -430
  144. package/dist/providers/index.d.ts +9 -9
  145. package/dist/providers/index.js +9 -9
  146. package/dist/providers/mcp-provider.js +24 -5
  147. package/dist/providers/mistral.d.ts +42 -0
  148. package/dist/providers/mistral.js +160 -0
  149. package/dist/providers/ollama.d.ts +52 -36
  150. package/dist/providers/ollama.js +297 -519
  151. package/dist/providers/openAI.d.ts +19 -18
  152. package/dist/providers/openAI.js +76 -276
  153. package/dist/sdk/tool-extension.d.ts +181 -0
  154. package/dist/sdk/tool-extension.js +283 -0
  155. package/dist/sdk/tool-registration.d.ts +95 -0
  156. package/dist/sdk/tool-registration.js +168 -0
  157. package/dist/services/streaming/streaming-manager.js +11 -10
  158. package/dist/services/websocket/websocket-server.js +12 -11
  159. package/dist/telemetry/telemetry-service.js +8 -7
  160. package/dist/types/generate-types.d.ts +1 -0
  161. package/dist/types/mcp-types.d.ts +116 -0
  162. package/dist/types/mcp-types.js +5 -0
  163. package/dist/types/stream-types.d.ts +30 -18
  164. package/dist/types/universal-provider-options.d.ts +87 -0
  165. package/dist/types/universal-provider-options.js +53 -0
  166. package/package.json +12 -5
  167. package/dist/lib/providers/agent-enhanced-provider.d.ts +0 -93
  168. package/dist/lib/providers/agent-enhanced-provider.js +0 -605
  169. package/dist/lib/providers/amazonBedrock.d.ts +0 -28
  170. package/dist/lib/providers/amazonBedrock.js +0 -364
  171. package/dist/lib/providers/azureOpenAI.d.ts +0 -42
  172. package/dist/lib/providers/azureOpenAI.js +0 -347
  173. package/dist/lib/providers/googleAIStudio.d.ts +0 -42
  174. package/dist/lib/providers/googleAIStudio.js +0 -364
  175. package/dist/lib/providers/googleVertexAI.d.ts +0 -34
  176. package/dist/lib/providers/googleVertexAI.js +0 -547
  177. package/dist/lib/providers/mistralAI.d.ts +0 -37
  178. package/dist/lib/providers/mistralAI.js +0 -325
  179. package/dist/providers/agent-enhanced-provider.d.ts +0 -93
  180. package/dist/providers/agent-enhanced-provider.js +0 -606
  181. package/dist/providers/amazonBedrock.d.ts +0 -28
  182. package/dist/providers/amazonBedrock.js +0 -364
  183. package/dist/providers/azureOpenAI.d.ts +0 -42
  184. package/dist/providers/azureOpenAI.js +0 -348
  185. package/dist/providers/googleAIStudio.d.ts +0 -42
  186. package/dist/providers/googleAIStudio.js +0 -364
  187. package/dist/providers/googleVertexAI.d.ts +0 -34
  188. package/dist/providers/googleVertexAI.js +0 -547
  189. package/dist/providers/mistralAI.d.ts +0 -37
  190. package/dist/providers/mistralAI.js +0 -325
@@ -1,547 +0,0 @@
1
- import { createVertex, } from "@ai-sdk/google-vertex";
2
- // Cache for anthropic module to avoid repeated imports
3
- let _createVertexAnthropic = null;
4
- let _anthropicImportAttempted = false;
5
- // Function to dynamically import anthropic support
6
- async function getCreateVertexAnthropic() {
7
- if (_anthropicImportAttempted) {
8
- return _createVertexAnthropic;
9
- }
10
- _anthropicImportAttempted = true;
11
- try {
12
- // Try to import the anthropic module - available in @ai-sdk/google-vertex ^2.2.0+
13
- const anthropicModule = await import("@ai-sdk/google-vertex/anthropic");
14
- _createVertexAnthropic = anthropicModule.createVertexAnthropic;
15
- logger.debug("[GoogleVertexAI] Anthropic module successfully loaded");
16
- return _createVertexAnthropic;
17
- }
18
- catch (error) {
19
- // Anthropic module not available
20
- logger.warn("[GoogleVertexAI] Anthropic module not available. Install @ai-sdk/google-vertex ^2.2.0 for Anthropic model support.");
21
- return null;
22
- }
23
- }
24
- import { streamText, generateText, Output, } from "ai";
25
- import { logger } from "../utils/logger.js";
26
- import { createTimeoutController, TimeoutError, getDefaultTimeout, } from "../utils/timeout.js";
27
- import { DEFAULT_MAX_TOKENS } from "../core/constants.js";
28
- import { createProxyFetch } from "../proxy/proxy-fetch.js";
29
- import { evaluateResponse } from "../core/evaluation.js";
30
- // Default system context
31
- const DEFAULT_SYSTEM_CONTEXT = {
32
- systemPrompt: "You are a helpful AI assistant.",
33
- };
34
- // Configuration helpers
35
- const getGCPVertexBreezeProjectId = () => {
36
- const projectId = process.env.GOOGLE_VERTEX_PROJECT;
37
- if (!projectId) {
38
- // 🔧 FIX: Enhanced error message with setup instructions
39
- throw new Error(`❌ VERTEX Provider Configuration Error
40
-
41
- Missing required environment variables: GOOGLE_VERTEX_PROJECT
42
-
43
- 🔧 Step 1: Get Credentials
44
- Set up Google Cloud project and download service account JSON
45
-
46
- 💡 Step 2: Add to your .env file (or export in CLI):
47
- GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account.json"
48
- GOOGLE_VERTEX_PROJECT="your-gcp-project-id"
49
- GOOGLE_VERTEX_LOCATION="us-central1"
50
- # Optional:
51
- VERTEX_MODEL="gemini-2.5-pro"
52
-
53
- 🚀 Step 3: Test the setup:
54
- npx neurolink generate "Hello" --provider vertex
55
-
56
- 📖 Full setup guide: https://docs.neurolink.ai/providers/vertex`);
57
- }
58
- return projectId;
59
- };
60
- const getGCPVertexBreezeLocation = () => {
61
- return process.env.GOOGLE_VERTEX_LOCATION || "us-east5";
62
- };
63
- const getGoogleApplicationCredentials = () => {
64
- return process.env.GOOGLE_APPLICATION_CREDENTIALS;
65
- };
66
- const getGoogleServiceAccountKey = () => {
67
- return process.env.GOOGLE_SERVICE_ACCOUNT_KEY;
68
- };
69
- const getGoogleClientEmail = () => {
70
- return process.env.GOOGLE_AUTH_CLIENT_EMAIL;
71
- };
72
- const getGooglePrivateKey = () => {
73
- return process.env.GOOGLE_AUTH_PRIVATE_KEY;
74
- };
75
- const getVertexModelId = () => {
76
- return process.env.VERTEX_MODEL_ID || "claude-sonnet-4@20250514";
77
- };
78
- const hasPrincipalAccountAuth = () => {
79
- return !!getGoogleApplicationCredentials();
80
- };
81
- const hasServiceAccountKeyAuth = () => {
82
- return !!getGoogleServiceAccountKey();
83
- };
84
- const hasServiceAccountEnvAuth = () => {
85
- return !!(getGoogleClientEmail() && getGooglePrivateKey());
86
- };
87
- const hasValidAuth = () => {
88
- return (hasPrincipalAccountAuth() ||
89
- hasServiceAccountKeyAuth() ||
90
- hasServiceAccountEnvAuth());
91
- };
92
- // Setup environment for Google authentication
93
- const setupGoogleAuth = async () => {
94
- const functionTag = "setupGoogleAuth";
95
- // Method 2: Service Account Key (JSON string) - Create temporary file
96
- if (hasServiceAccountKeyAuth() && !hasPrincipalAccountAuth()) {
97
- const serviceAccountKey = getGoogleServiceAccountKey();
98
- logger.debug(`[${functionTag}] Service account key auth (JSON string)`, {
99
- hasServiceAccountKey: !!serviceAccountKey,
100
- authMethod: "service_account_key",
101
- });
102
- try {
103
- // Parse to validate JSON
104
- JSON.parse(serviceAccountKey);
105
- // Write to temporary file and set environment variable using dynamic imports
106
- const { writeFileSync } = await import("fs");
107
- const { join } = await import("path");
108
- const { tmpdir } = await import("os");
109
- const tempFile = join(tmpdir(), `gcp-credentials-${Date.now()}.json`);
110
- writeFileSync(tempFile, serviceAccountKey);
111
- process.env.GOOGLE_APPLICATION_CREDENTIALS = tempFile;
112
- logger.debug(`[${functionTag}] Created temporary credentials file`, {
113
- tempFile: "[CREATED]",
114
- authMethod: "service_account_key_temp_file",
115
- });
116
- }
117
- catch (error) {
118
- logger.error(`[${functionTag}] Failed to parse service account key`, {
119
- error: error instanceof Error ? error.message : String(error),
120
- });
121
- throw new Error("Invalid GOOGLE_SERVICE_ACCOUNT_KEY format. Must be valid JSON.");
122
- }
123
- }
124
- // Method 3: Service Account Environment Variables - Set as individual env vars
125
- if (hasServiceAccountEnvAuth() &&
126
- !hasPrincipalAccountAuth() &&
127
- !hasServiceAccountKeyAuth()) {
128
- const clientEmail = getGoogleClientEmail();
129
- const privateKey = getGooglePrivateKey();
130
- logger.debug(`[${functionTag}] Service account env auth (separate variables)`, {
131
- hasClientEmail: !!clientEmail,
132
- hasPrivateKey: !!privateKey,
133
- authMethod: "service_account_env",
134
- });
135
- // Create service account object and write to temporary file
136
- const serviceAccount = {
137
- type: "service_account",
138
- project_id: getGCPVertexBreezeProjectId(),
139
- client_email: clientEmail,
140
- private_key: privateKey.replace(/\\n/g, "\n"),
141
- auth_uri: "https://accounts.google.com/o/oauth2/auth",
142
- token_uri: "https://oauth2.googleapis.com/token",
143
- };
144
- try {
145
- // Use dynamic imports for ESM compatibility
146
- const { writeFileSync } = await import("fs");
147
- const { join } = await import("path");
148
- const { tmpdir } = await import("os");
149
- const tempFile = join(tmpdir(), `gcp-credentials-env-${Date.now()}.json`);
150
- writeFileSync(tempFile, JSON.stringify(serviceAccount, null, 2));
151
- process.env.GOOGLE_APPLICATION_CREDENTIALS = tempFile;
152
- logger.debug(`[${functionTag}] Created temporary credentials file from env vars`, {
153
- tempFile: "[CREATED]",
154
- authMethod: "service_account_env_temp_file",
155
- });
156
- }
157
- catch (error) {
158
- logger.error(`[${functionTag}] Failed to create service account file from env vars`, {
159
- error: error instanceof Error ? error.message : String(error),
160
- });
161
- throw new Error("Failed to create temporary service account file from environment variables.");
162
- }
163
- }
164
- };
165
- // Vertex AI setup with multiple authentication support
166
- const createVertexSettings = async () => {
167
- const functionTag = "createVertexSettings";
168
- // Setup authentication first
169
- await setupGoogleAuth();
170
- const proxyFetch = createProxyFetch();
171
- const baseSettings = {
172
- project: getGCPVertexBreezeProjectId(),
173
- location: getGCPVertexBreezeLocation(),
174
- fetch: proxyFetch,
175
- };
176
- // Method 1: Principal Account Authentication (file path) - Recommended for production
177
- if (hasPrincipalAccountAuth()) {
178
- const credentialsPath = getGoogleApplicationCredentials();
179
- logger.debug(`[${functionTag}] Principal account auth (file path)`, {
180
- credentialsPath: credentialsPath ? "[PROVIDED]" : "[NOT_PROVIDED]",
181
- authMethod: "principal_account_file",
182
- });
183
- return baseSettings;
184
- }
185
- // Method 2 & 3: Other methods now set GOOGLE_APPLICATION_CREDENTIALS in setupGoogleAuth()
186
- if (hasServiceAccountKeyAuth() || hasServiceAccountEnvAuth()) {
187
- logger.debug(`[${functionTag}] Alternative auth method configured`, {
188
- authMethod: hasServiceAccountKeyAuth()
189
- ? "service_account_key"
190
- : "service_account_env",
191
- credentialsSet: !!process.env.GOOGLE_APPLICATION_CREDENTIALS,
192
- });
193
- return baseSettings;
194
- }
195
- // No valid authentication found
196
- logger.error(`[${functionTag}] No valid authentication method found`, {
197
- authMethod: "none",
198
- hasPrincipalAccount: hasPrincipalAccountAuth(),
199
- hasServiceAccountKey: hasServiceAccountKeyAuth(),
200
- hasServiceAccountEnv: hasServiceAccountEnvAuth(),
201
- availableMethods: [
202
- "GOOGLE_APPLICATION_CREDENTIALS (file path)",
203
- "GOOGLE_SERVICE_ACCOUNT_KEY (JSON string)",
204
- "GOOGLE_AUTH_CLIENT_EMAIL + GOOGLE_AUTH_PRIVATE_KEY (env vars)",
205
- ],
206
- });
207
- throw new Error("No valid Google Vertex AI authentication found. Please provide one of:\n" +
208
- "1. GOOGLE_APPLICATION_CREDENTIALS (path to service account file)\n" +
209
- "2. GOOGLE_SERVICE_ACCOUNT_KEY (JSON string of service account)\n" +
210
- "3. GOOGLE_AUTH_CLIENT_EMAIL + GOOGLE_AUTH_PRIVATE_KEY (environment variables)");
211
- };
212
- // Helper function to determine if a model is an Anthropic model
213
- const isAnthropicModel = (modelName) => {
214
- // Anthropic models in Vertex AI contain "claude" anywhere in the model name
215
- return modelName.toLowerCase().includes("claude");
216
- };
217
- // Lazy initialization cache
218
- let _vertex = null;
219
- async function getVertexInstance() {
220
- if (!_vertex) {
221
- const settings = await createVertexSettings();
222
- _vertex = createVertex(settings);
223
- }
224
- return _vertex;
225
- }
226
- // Google Vertex AI class with enhanced error handling and Anthropic model support
227
- export class GoogleVertexAI {
228
- modelName;
229
- /**
230
- * Initializes a new instance of GoogleVertexAI
231
- * @param modelName - Optional model name to override the default from config
232
- */
233
- constructor(modelName) {
234
- const functionTag = "GoogleVertexAI.constructor";
235
- this.modelName = modelName || getVertexModelId();
236
- try {
237
- logger.debug(`[${functionTag}] Initialization started`, {
238
- modelName: this.modelName,
239
- isAnthropic: isAnthropicModel(this.modelName),
240
- });
241
- const hasPrincipal = hasPrincipalAccountAuth();
242
- logger.debug(`[${functionTag}] Authentication validation`, {
243
- hasPrincipalAccountAuth: hasPrincipal,
244
- projectId: getGCPVertexBreezeProjectId() || "MISSING",
245
- location: getGCPVertexBreezeLocation() || "MISSING",
246
- });
247
- if (hasPrincipal) {
248
- logger.debug(`[${functionTag}] Auth method selected`, {
249
- authMethod: "principal_account",
250
- hasGoogleApplicationCredentials: !!getGoogleApplicationCredentials(),
251
- });
252
- }
253
- else {
254
- logger.warn(`[${functionTag}] Auth method missing`, {
255
- authMethod: "none",
256
- hasPrincipalAccountAuth: hasPrincipal,
257
- });
258
- }
259
- logger.debug(`[${functionTag}] Initialization completed`, {
260
- modelName: this.modelName,
261
- isAnthropic: isAnthropicModel(this.modelName),
262
- authMethod: hasPrincipalAccountAuth() ? "principal_account" : "none",
263
- success: true,
264
- });
265
- }
266
- catch (err) {
267
- logger.error(`[${functionTag}] Initialization failed`, {
268
- message: "Error in initializing Google Vertex AI",
269
- modelName: this.modelName,
270
- isAnthropic: isAnthropicModel(this.modelName),
271
- error: err instanceof Error ? err.message : String(err),
272
- stack: err instanceof Error ? err.stack : undefined,
273
- });
274
- }
275
- }
276
- /**
277
- * Gets the appropriate model instance (Google or Anthropic)
278
- * @private
279
- */
280
- async getModel() {
281
- if (isAnthropicModel(this.modelName)) {
282
- logger.debug("GoogleVertexAI.getModel - Anthropic model selected", {
283
- modelName: this.modelName,
284
- });
285
- const createVertexAnthropic = await getCreateVertexAnthropic();
286
- if (!createVertexAnthropic) {
287
- throw new Error(`Anthropic model "${this.modelName}" requested but @ai-sdk/google-vertex/anthropic is not available. ` +
288
- "Please install @ai-sdk/google-vertex ^2.2.0 or use a Google model instead.");
289
- }
290
- const settings = await createVertexSettings();
291
- const vertexAnthropic = createVertexAnthropic(settings);
292
- return vertexAnthropic(this.modelName);
293
- }
294
- const vertex = await getVertexInstance();
295
- return vertex(this.modelName);
296
- }
297
- /**
298
- * Processes text using non-streaming approach with optional schema validation
299
- * @param prompt - The input text prompt to analyze
300
- * @param analysisSchema - Optional Zod schema or Schema object for output validation
301
- * @returns Promise resolving to GenerateResult or null if operation fails
302
- */
303
- async generate(optionsOrPrompt, analysisSchema) {
304
- const functionTag = "GoogleVertexAI.generate";
305
- const provider = "vertex";
306
- const startTime = Date.now();
307
- try {
308
- // Parse parameters - support both string and options object
309
- const options = typeof optionsOrPrompt === "string"
310
- ? { prompt: optionsOrPrompt }
311
- : optionsOrPrompt;
312
- const { prompt, temperature = 0.7, maxTokens = DEFAULT_MAX_TOKENS, systemPrompt = DEFAULT_SYSTEM_CONTEXT.systemPrompt, schema, timeout = getDefaultTimeout(provider, "generate"), } = options;
313
- // Use schema from options or fallback parameter
314
- const finalSchema = schema || analysisSchema;
315
- logger.debug(`[${functionTag}] Generate request started`, {
316
- provider,
317
- modelName: this.modelName,
318
- isAnthropic: isAnthropicModel(this.modelName),
319
- promptLength: prompt.length,
320
- temperature,
321
- maxTokens,
322
- timeout,
323
- });
324
- const model = await this.getModel();
325
- // Create timeout controller if timeout is specified
326
- const timeoutController = createTimeoutController(timeout, provider, "generate");
327
- const generateOptions = {
328
- model: model,
329
- prompt: prompt,
330
- system: systemPrompt,
331
- temperature,
332
- maxTokens,
333
- // Add abort signal if available
334
- ...(timeoutController && {
335
- abortSignal: timeoutController.controller.signal,
336
- }),
337
- };
338
- if (finalSchema) {
339
- generateOptions.experimental_output = Output.object({
340
- schema: finalSchema,
341
- });
342
- }
343
- try {
344
- const result = await generateText(generateOptions);
345
- // Clean up timeout if successful
346
- timeoutController?.cleanup();
347
- logger.debug(`[${functionTag}] Generate text completed`, {
348
- provider,
349
- modelName: this.modelName,
350
- usage: result.usage,
351
- finishReason: result.finishReason,
352
- responseLength: result.text?.length || 0,
353
- timeout,
354
- });
355
- // Add analytics if enabled
356
- if (options.enableAnalytics) {
357
- result.analytics = {
358
- provider,
359
- model: this.modelName,
360
- tokens: result.usage,
361
- responseTime: Date.now() - startTime,
362
- context: options.context,
363
- };
364
- }
365
- // Add evaluation if enabled
366
- if (options.enableEvaluation) {
367
- result.evaluation = await evaluateResponse(prompt, result.text, options.context);
368
- }
369
- return {
370
- content: result.text,
371
- provider: "vertex",
372
- model: this.modelName,
373
- usage: result.usage
374
- ? {
375
- inputTokens: result.usage.promptTokens,
376
- outputTokens: result.usage.completionTokens,
377
- totalTokens: result.usage.totalTokens,
378
- }
379
- : undefined,
380
- responseTime: Date.now() - startTime,
381
- };
382
- }
383
- finally {
384
- // Always cleanup timeout
385
- timeoutController?.cleanup();
386
- }
387
- }
388
- catch (err) {
389
- // Log timeout errors specifically
390
- if (err instanceof TimeoutError) {
391
- logger.error(`[${functionTag}] Timeout error`, {
392
- provider,
393
- modelName: this.modelName,
394
- isAnthropic: isAnthropicModel(this.modelName),
395
- timeout: err.timeout,
396
- message: err.message,
397
- });
398
- }
399
- else {
400
- logger.error(`[${functionTag}] Exception`, {
401
- provider,
402
- modelName: this.modelName,
403
- message: "Error in generating text",
404
- err: String(err),
405
- });
406
- }
407
- throw err; // Re-throw error to trigger fallback
408
- }
409
- }
410
- /**
411
- * PRIMARY METHOD: Stream content using AI (recommended for new code)
412
- * Future-ready for multi-modal capabilities with current text focus
413
- */
414
- async stream(optionsOrPrompt, analysisSchema) {
415
- const functionTag = "GoogleVertexAI.stream";
416
- const startTime = Date.now();
417
- try {
418
- // Parse parameters - support both string and options object
419
- const options = typeof optionsOrPrompt === "string"
420
- ? { input: { text: optionsOrPrompt } }
421
- : optionsOrPrompt;
422
- // Validate input
423
- if (!options?.input?.text ||
424
- typeof options.input.text !== "string" ||
425
- options.input.text.trim() === "") {
426
- throw new Error("Stream options must include input.text as a non-empty string");
427
- }
428
- logger.debug(`[${functionTag}] Stream request started`, {
429
- provider: "vertex",
430
- modelName: this.modelName,
431
- isAnthropic: isAnthropicModel(this.modelName),
432
- promptLength: options.input.text.length,
433
- hasSchema: !!analysisSchema,
434
- });
435
- // Convert StreamOptions for internal use
436
- const convertedOptions = {
437
- prompt: options.input.text,
438
- provider: options.provider,
439
- model: options.model,
440
- temperature: options.temperature,
441
- maxTokens: options.maxTokens,
442
- systemPrompt: options.systemPrompt,
443
- timeout: options.timeout,
444
- schema: options.schema,
445
- tools: options.tools,
446
- };
447
- const { prompt, temperature = 0.7, maxTokens = DEFAULT_MAX_TOKENS, systemPrompt = DEFAULT_SYSTEM_CONTEXT.systemPrompt, schema, timeout = getDefaultTimeout("vertex", "stream"), } = convertedOptions;
448
- // Use schema from options or fallback parameter
449
- const finalSchema = schema || analysisSchema;
450
- logger.debug(`[${functionTag}] Stream request details`, {
451
- provider: "vertex",
452
- modelName: this.modelName,
453
- isAnthropic: isAnthropicModel(this.modelName),
454
- promptLength: prompt.length,
455
- temperature,
456
- maxTokens,
457
- hasSchema: !!finalSchema,
458
- timeout,
459
- });
460
- const model = await this.getModel();
461
- // Create timeout controller if timeout is specified
462
- const timeoutController = createTimeoutController(timeout, "vertex", "stream");
463
- let chunkCount = 0;
464
- const streamOptions = {
465
- model: model,
466
- prompt: prompt,
467
- system: systemPrompt,
468
- temperature,
469
- maxTokens,
470
- // Add abort signal if available
471
- ...(timeoutController && {
472
- abortSignal: timeoutController.controller.signal,
473
- }),
474
- onError: (event) => {
475
- const error = event.error;
476
- const errorMessage = error instanceof Error ? error.message : String(error);
477
- const errorStack = error instanceof Error ? error.stack : undefined;
478
- logger.error(`[${functionTag}] Stream error`, {
479
- provider: "vertex",
480
- modelName: this.modelName,
481
- error: errorMessage,
482
- stack: errorStack,
483
- promptLength: prompt.length,
484
- chunkCount,
485
- });
486
- },
487
- onFinish: (event) => {
488
- logger.debug(`[${functionTag}] Stream finished`, {
489
- provider: "vertex",
490
- modelName: this.modelName,
491
- finishReason: event.finishReason,
492
- usage: event.usage,
493
- totalChunks: chunkCount,
494
- promptLength: prompt.length,
495
- responseLength: event.text?.length || 0,
496
- });
497
- },
498
- onChunk: (event) => {
499
- chunkCount++;
500
- logger.debug(`[${functionTag}] Stream chunk`, {
501
- provider: "vertex",
502
- modelName: this.modelName,
503
- chunkNumber: chunkCount,
504
- chunkLength: event.chunk.text?.length || 0,
505
- chunkType: event.chunk.type,
506
- });
507
- },
508
- };
509
- if (finalSchema) {
510
- streamOptions.experimental_output = Output.object({
511
- schema: finalSchema,
512
- });
513
- }
514
- const result = streamText(streamOptions);
515
- // Convert to StreamResult format
516
- return {
517
- stream: (async function* () {
518
- for await (const chunk of result.textStream) {
519
- yield { content: chunk };
520
- }
521
- })(),
522
- provider: "vertex",
523
- model: this.modelName,
524
- metadata: {
525
- streamId: `vertex-${Date.now()}`,
526
- startTime,
527
- },
528
- };
529
- }
530
- catch (err) {
531
- logger.error(`[${functionTag}] Exception`, {
532
- provider: "vertex",
533
- modelName: this.modelName,
534
- isAnthropic: isAnthropicModel(this.modelName),
535
- message: "Error in streaming text",
536
- err: String(err),
537
- });
538
- throw err; // Re-throw error to trigger fallback
539
- }
540
- }
541
- /**
542
- * Short alias for generate() - CLI-SDK consistency
543
- */
544
- async gen(optionsOrPrompt, analysisSchema) {
545
- return this.generate(optionsOrPrompt, analysisSchema);
546
- }
547
- }
@@ -1,37 +0,0 @@
1
- import type { ZodType, ZodTypeDef } from "zod";
2
- import { type Schema } from "ai";
3
- import type { GenerateResult } from "../types/generate-types.js";
4
- import type { StreamOptions, StreamResult } from "../types/stream-types.js";
5
- import type { AIProvider, TextGenerationOptions, EnhancedGenerateResult } from "../core/types.js";
6
- export declare class MistralAI implements AIProvider {
7
- private modelName;
8
- private client;
9
- /**
10
- * Initializes a new instance of MistralAI
11
- * @param modelName - Optional model name to override the default from config
12
- */
13
- constructor(modelName?: string | null);
14
- /**
15
- * Gets the appropriate model instance
16
- * @private
17
- */
18
- private getModel;
19
- /**
20
- * LEGACY METHOD: Use stream() instead for new code
21
- * @deprecated Use stream() method instead
22
- */
23
- /**
24
- * PRIMARY METHOD: Stream content using AI (recommended for new code)
25
- * Future-ready for multi-modal capabilities with current text focus
26
- */
27
- stream(optionsOrPrompt: StreamOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<StreamResult>;
28
- /**
29
- * Processes text using non-streaming approach with optional schema validation
30
- * @param prompt - The input text prompt to analyze
31
- * @param analysisSchema - Optional Zod schema or Schema object for output validation
32
- * @returns Promise resolving to GenerateResult or null if operation fails
33
- */
34
- generate(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: ZodType<unknown, ZodTypeDef, unknown> | Schema<unknown>): Promise<GenerateResult>;
35
- gen(optionsOrPrompt: TextGenerationOptions | string, analysisSchema?: any): Promise<EnhancedGenerateResult | null>;
36
- }
37
- export default MistralAI;