@juspay/neurolink 4.1.0 → 4.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 (75) hide show
  1. package/CHANGELOG.md +14 -2
  2. package/README.md +105 -116
  3. package/dist/cli/commands/mcp.d.ts +11 -0
  4. package/dist/cli/commands/mcp.js +332 -223
  5. package/dist/cli/index.js +69 -8
  6. package/dist/core/factory.js +2 -2
  7. package/dist/index.d.ts +1 -1
  8. package/dist/index.js +1 -1
  9. package/dist/lib/core/factory.js +2 -2
  10. package/dist/lib/index.d.ts +1 -1
  11. package/dist/lib/index.js +1 -1
  12. package/dist/lib/mcp/context-manager.d.ts +6 -0
  13. package/dist/lib/mcp/context-manager.js +8 -0
  14. package/dist/lib/mcp/contracts/mcpContract.d.ts +1 -0
  15. package/dist/lib/mcp/external-client.js +6 -2
  16. package/dist/lib/mcp/initialize.d.ts +2 -1
  17. package/dist/lib/mcp/initialize.js +8 -7
  18. package/dist/lib/mcp/orchestrator.js +9 -0
  19. package/dist/lib/mcp/registry.d.ts +1 -1
  20. package/dist/lib/mcp/servers/ai-providers/ai-analysis-tools.js +1 -1
  21. package/dist/lib/mcp/servers/ai-providers/ai-core-server.js +3 -3
  22. package/dist/lib/mcp/servers/ai-providers/ai-workflow-tools.d.ts +2 -2
  23. package/dist/lib/mcp/servers/ai-providers/ai-workflow-tools.js +1 -1
  24. package/dist/lib/mcp/session-manager.js +1 -1
  25. package/dist/lib/mcp/session-persistence.js +1 -1
  26. package/dist/lib/mcp/tool-registry.d.ts +31 -11
  27. package/dist/lib/mcp/tool-registry.js +226 -38
  28. package/dist/lib/mcp/unified-mcp.d.ts +12 -2
  29. package/dist/lib/mcp/unified-registry.d.ts +21 -7
  30. package/dist/lib/mcp/unified-registry.js +179 -17
  31. package/dist/lib/neurolink.js +17 -25
  32. package/dist/lib/providers/googleVertexAI.js +19 -1
  33. package/dist/lib/providers/openAI.js +18 -1
  34. package/dist/lib/utils/provider-setup-messages.d.ts +8 -0
  35. package/dist/lib/utils/provider-setup-messages.js +120 -0
  36. package/dist/lib/utils/provider-validation.d.ts +35 -0
  37. package/dist/lib/utils/provider-validation.js +625 -0
  38. package/dist/lib/utils/providerUtils-fixed.js +20 -1
  39. package/dist/lib/utils/providerUtils.d.ts +2 -2
  40. package/dist/lib/utils/providerUtils.js +38 -7
  41. package/dist/lib/utils/timeout-manager.d.ts +75 -0
  42. package/dist/lib/utils/timeout-manager.js +244 -0
  43. package/dist/mcp/context-manager.d.ts +6 -0
  44. package/dist/mcp/context-manager.js +8 -0
  45. package/dist/mcp/contracts/mcpContract.d.ts +1 -0
  46. package/dist/mcp/external-client.js +6 -2
  47. package/dist/mcp/initialize.d.ts +2 -1
  48. package/dist/mcp/initialize.js +8 -7
  49. package/dist/mcp/orchestrator.js +9 -0
  50. package/dist/mcp/plugins/core/neurolink-mcp.json +15 -15
  51. package/dist/mcp/registry.d.ts +1 -1
  52. package/dist/mcp/servers/ai-providers/ai-analysis-tools.js +1 -1
  53. package/dist/mcp/servers/ai-providers/ai-core-server.js +3 -3
  54. package/dist/mcp/servers/ai-providers/ai-workflow-tools.d.ts +2 -2
  55. package/dist/mcp/servers/ai-providers/ai-workflow-tools.js +1 -1
  56. package/dist/mcp/session-manager.js +1 -1
  57. package/dist/mcp/session-persistence.js +1 -1
  58. package/dist/mcp/tool-registry.d.ts +31 -11
  59. package/dist/mcp/tool-registry.js +226 -38
  60. package/dist/mcp/unified-mcp.d.ts +12 -2
  61. package/dist/mcp/unified-registry.d.ts +21 -7
  62. package/dist/mcp/unified-registry.js +179 -17
  63. package/dist/neurolink.js +17 -25
  64. package/dist/providers/googleVertexAI.js +19 -1
  65. package/dist/providers/openAI.js +18 -1
  66. package/dist/utils/provider-setup-messages.d.ts +8 -0
  67. package/dist/utils/provider-setup-messages.js +120 -0
  68. package/dist/utils/provider-validation.d.ts +35 -0
  69. package/dist/utils/provider-validation.js +625 -0
  70. package/dist/utils/providerUtils-fixed.js +20 -1
  71. package/dist/utils/providerUtils.d.ts +2 -2
  72. package/dist/utils/providerUtils.js +38 -7
  73. package/dist/utils/timeout-manager.d.ts +75 -0
  74. package/dist/utils/timeout-manager.js +244 -0
  75. package/package.json +245 -245
@@ -0,0 +1,625 @@
1
+ /**
2
+ * Enhanced Provider Validation Utilities
3
+ *
4
+ * Fixes false positives in provider status checking by implementing:
5
+ * - API key format validation
6
+ * - Lightweight authentication checks
7
+ * - Proper error classification
8
+ * - Rate-limit friendly validation
9
+ */
10
+ import { hasProviderEnvVars } from "./providerUtils.js";
11
+ import { defaultTimeoutManager } from "./timeout-manager.js";
12
+ /**
13
+ * API key format validation rules for different providers
14
+ */
15
+ const API_KEY_FORMATS = {
16
+ openai: /^sk-[A-Za-z0-9]{48,}$/,
17
+ anthropic: /^sk-ant-[A-Za-z0-9\-_]{95,}$/,
18
+ "google-ai": /^AIza[A-Za-z0-9\-_]{35}$/,
19
+ huggingface: /^hf_[A-Za-z0-9]{37}$/,
20
+ mistral: /^[A-Za-z0-9]{32}$/,
21
+ // Azure and AWS have more complex validation patterns
22
+ azure: /^[A-Za-z0-9]{32,}$/,
23
+ aws: /^[A-Z0-9]{20}$/, // Access Key ID format
24
+ };
25
+ /**
26
+ * Get API key for a provider from environment variables
27
+ */
28
+ function getProviderApiKey(provider) {
29
+ switch (provider.toLowerCase()) {
30
+ case "openai":
31
+ return process.env.OPENAI_API_KEY || null;
32
+ case "anthropic":
33
+ return process.env.ANTHROPIC_API_KEY || null;
34
+ case "google-ai":
35
+ return (process.env.GOOGLE_AI_API_KEY ||
36
+ process.env.GOOGLE_GENERATIVE_AI_API_KEY ||
37
+ null);
38
+ case "azure":
39
+ return process.env.AZURE_OPENAI_API_KEY || null;
40
+ case "huggingface":
41
+ return process.env.HUGGINGFACE_API_KEY || process.env.HF_TOKEN || null;
42
+ case "mistral":
43
+ return process.env.MISTRAL_API_KEY || null;
44
+ case "aws":
45
+ case "bedrock":
46
+ return process.env.AWS_ACCESS_KEY_ID || null;
47
+ case "vertex":
48
+ // Vertex uses service account JSON, not a simple API key
49
+ return (process.env.GOOGLE_APPLICATION_CREDENTIALS ||
50
+ process.env.GOOGLE_VERTEX_PROJECT ||
51
+ null);
52
+ default:
53
+ return null;
54
+ }
55
+ }
56
+ /**
57
+ * Validate API key format for a specific provider
58
+ */
59
+ export function validateApiKeyFormat(provider, apiKey) {
60
+ const format = API_KEY_FORMATS[provider.toLowerCase()];
61
+ if (!format) {
62
+ // No format validation available, assume valid if not empty
63
+ return apiKey.length > 0;
64
+ }
65
+ return format.test(apiKey);
66
+ }
67
+ /**
68
+ * Lightweight authentication check using minimal API calls
69
+ */
70
+ async function validateAuthentication(provider, apiKey) {
71
+ const startTime = Date.now();
72
+ try {
73
+ const result = await defaultTimeoutManager.executeWithTimeout(async () => {
74
+ switch (provider.toLowerCase()) {
75
+ case "openai":
76
+ return await validateOpenAIAuth(apiKey);
77
+ case "anthropic":
78
+ return await validateAnthropicAuth(apiKey);
79
+ case "google-ai":
80
+ return await validateGoogleAIAuth(apiKey);
81
+ case "huggingface":
82
+ return await validateHuggingFaceAuth(apiKey);
83
+ case "mistral":
84
+ return await validateMistralAuth(apiKey);
85
+ case "azure":
86
+ return await validateAzureAuth(apiKey);
87
+ case "bedrock":
88
+ case "aws":
89
+ return await validateBedrockAuth(apiKey);
90
+ case "vertex":
91
+ return await validateVertexAuth(apiKey);
92
+ default:
93
+ return {
94
+ success: false,
95
+ error: "Validation not implemented for this provider",
96
+ errorType: "unknown",
97
+ };
98
+ }
99
+ }, {
100
+ operation: `auth-check-${provider}`,
101
+ timeout: 10000, // 10 second timeout for auth checks
102
+ retryOnTimeout: false,
103
+ });
104
+ if (result.success && result.data) {
105
+ return {
106
+ ...result.data,
107
+ responseTime: Date.now() - startTime,
108
+ };
109
+ }
110
+ else {
111
+ return {
112
+ success: false,
113
+ error: result.error?.message || "Authentication check failed",
114
+ errorType: "network",
115
+ responseTime: Date.now() - startTime,
116
+ };
117
+ }
118
+ }
119
+ catch (error) {
120
+ return {
121
+ success: false,
122
+ error: error instanceof Error ? error.message : String(error),
123
+ errorType: "unknown",
124
+ responseTime: Date.now() - startTime,
125
+ };
126
+ }
127
+ }
128
+ /**
129
+ * OpenAI authentication validation using models endpoint
130
+ */
131
+ async function validateOpenAIAuth(apiKey) {
132
+ const response = await fetch("https://api.openai.com/v1/models", {
133
+ method: "GET",
134
+ headers: {
135
+ Authorization: `Bearer ${apiKey}`,
136
+ "User-Agent": "NeuroLink-CLI/4.1.1",
137
+ },
138
+ });
139
+ if (response.ok) {
140
+ return { success: true };
141
+ }
142
+ else if (response.status === 401) {
143
+ return {
144
+ success: false,
145
+ error: "Invalid API key",
146
+ errorType: "auth",
147
+ };
148
+ }
149
+ else if (response.status === 429) {
150
+ return {
151
+ success: false,
152
+ error: "Rate limit exceeded",
153
+ errorType: "quota",
154
+ };
155
+ }
156
+ else {
157
+ const error = await response.text();
158
+ return {
159
+ success: false,
160
+ error: `API error: ${response.status}`,
161
+ errorType: "network",
162
+ };
163
+ }
164
+ }
165
+ /**
166
+ * Anthropic authentication validation using messages endpoint (minimal)
167
+ */
168
+ async function validateAnthropicAuth(apiKey) {
169
+ // Use a minimal message request to check auth
170
+ const response = await fetch("https://api.anthropic.com/v1/messages", {
171
+ method: "POST",
172
+ headers: {
173
+ "Content-Type": "application/json",
174
+ "x-api-key": apiKey,
175
+ "anthropic-version": "2023-06-01",
176
+ "User-Agent": "NeuroLink-CLI/4.1.1",
177
+ },
178
+ body: JSON.stringify({
179
+ model: "claude-3-haiku-20240307",
180
+ max_tokens: 1,
181
+ messages: [{ role: "user", content: "test" }],
182
+ }),
183
+ });
184
+ if (response.ok) {
185
+ return { success: true };
186
+ }
187
+ else if (response.status === 401) {
188
+ return {
189
+ success: false,
190
+ error: "Invalid API key",
191
+ errorType: "auth",
192
+ };
193
+ }
194
+ else if (response.status === 429) {
195
+ return {
196
+ success: false,
197
+ error: "Rate limit exceeded",
198
+ errorType: "quota",
199
+ };
200
+ }
201
+ else {
202
+ return {
203
+ success: false,
204
+ error: `API error: ${response.status}`,
205
+ errorType: "network",
206
+ };
207
+ }
208
+ }
209
+ /**
210
+ * Google AI authentication validation
211
+ */
212
+ async function validateGoogleAIAuth(apiKey) {
213
+ // Use the models endpoint for lightweight validation
214
+ const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models?key=${apiKey}`, {
215
+ method: "GET",
216
+ headers: {
217
+ "User-Agent": "NeuroLink-CLI/4.1.1",
218
+ },
219
+ });
220
+ if (response.ok) {
221
+ return { success: true };
222
+ }
223
+ else if (response.status === 400 || response.status === 401) {
224
+ return {
225
+ success: false,
226
+ error: "Invalid API key",
227
+ errorType: "auth",
228
+ };
229
+ }
230
+ else if (response.status === 429) {
231
+ return {
232
+ success: false,
233
+ error: "Rate limit exceeded",
234
+ errorType: "quota",
235
+ };
236
+ }
237
+ else {
238
+ return {
239
+ success: false,
240
+ error: `API error: ${response.status}`,
241
+ errorType: "network",
242
+ };
243
+ }
244
+ }
245
+ /**
246
+ * HuggingFace authentication validation
247
+ */
248
+ async function validateHuggingFaceAuth(apiKey) {
249
+ const response = await fetch("https://huggingface.co/api/whoami", {
250
+ method: "GET",
251
+ headers: {
252
+ Authorization: `Bearer ${apiKey}`,
253
+ "User-Agent": "NeuroLink-CLI/4.1.1",
254
+ },
255
+ });
256
+ if (response.ok) {
257
+ return { success: true };
258
+ }
259
+ else if (response.status === 401) {
260
+ return {
261
+ success: false,
262
+ error: "Invalid API key",
263
+ errorType: "auth",
264
+ };
265
+ }
266
+ else {
267
+ return {
268
+ success: false,
269
+ error: `API error: ${response.status}`,
270
+ errorType: "network",
271
+ };
272
+ }
273
+ }
274
+ /**
275
+ * Mistral authentication validation
276
+ */
277
+ async function validateMistralAuth(apiKey) {
278
+ const response = await fetch("https://api.mistral.ai/v1/models", {
279
+ method: "GET",
280
+ headers: {
281
+ Authorization: `Bearer ${apiKey}`,
282
+ "User-Agent": "NeuroLink-CLI/4.1.1",
283
+ },
284
+ });
285
+ if (response.ok) {
286
+ return { success: true };
287
+ }
288
+ else if (response.status === 401) {
289
+ return {
290
+ success: false,
291
+ error: "Invalid API key",
292
+ errorType: "auth",
293
+ };
294
+ }
295
+ else if (response.status === 429) {
296
+ return {
297
+ success: false,
298
+ error: "Rate limit exceeded",
299
+ errorType: "quota",
300
+ };
301
+ }
302
+ else {
303
+ return {
304
+ success: false,
305
+ error: `API error: ${response.status}`,
306
+ errorType: "network",
307
+ };
308
+ }
309
+ }
310
+ /**
311
+ * Azure OpenAI authentication validation
312
+ */
313
+ async function validateAzureAuth(apiKey) {
314
+ // Azure validation is more complex as it requires endpoint URL
315
+ // For now, we can only validate format and basic connectivity
316
+ const endpoint = process.env.AZURE_OPENAI_ENDPOINT;
317
+ if (!endpoint) {
318
+ return {
319
+ success: false,
320
+ error: "AZURE_OPENAI_ENDPOINT not configured",
321
+ errorType: "config",
322
+ };
323
+ }
324
+ // Try to validate with a models call
325
+ const response = await fetch(`${endpoint}/openai/models?api-version=2024-02-01`, {
326
+ method: "GET",
327
+ headers: {
328
+ "api-key": apiKey,
329
+ "User-Agent": "NeuroLink-CLI/4.1.1",
330
+ },
331
+ });
332
+ if (response.ok) {
333
+ return { success: true };
334
+ }
335
+ else if (response.status === 401) {
336
+ return {
337
+ success: false,
338
+ error: "Invalid API key",
339
+ errorType: "auth",
340
+ };
341
+ }
342
+ else if (response.status === 429) {
343
+ return {
344
+ success: false,
345
+ error: "Rate limit exceeded",
346
+ errorType: "quota",
347
+ };
348
+ }
349
+ else {
350
+ return {
351
+ success: false,
352
+ error: `API error: ${response.status}`,
353
+ errorType: "network",
354
+ };
355
+ }
356
+ }
357
+ /**
358
+ * AWS Bedrock authentication validation
359
+ */
360
+ async function validateBedrockAuth(accessKeyId) {
361
+ // Check if required AWS environment variables are present
362
+ const secretKey = process.env.AWS_SECRET_ACCESS_KEY;
363
+ const region = process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION || "us-east-1";
364
+ if (!secretKey) {
365
+ return {
366
+ success: false,
367
+ error: "AWS_SECRET_ACCESS_KEY not configured",
368
+ errorType: "config",
369
+ };
370
+ }
371
+ // For Bedrock, we'll do a lightweight check using STS GetCallerIdentity
372
+ // This validates AWS credentials without making actual model calls
373
+ try {
374
+ // This is a simplified check - in a real implementation, you'd use AWS SDK
375
+ // For now, we'll just validate that the access key format is correct
376
+ const accessKeyPattern = /^AKIA[0-9A-Z]{16}$|^ASIA[0-9A-Z]{16}$/;
377
+ if (!accessKeyPattern.test(accessKeyId)) {
378
+ return {
379
+ success: false,
380
+ error: "Invalid AWS Access Key ID format",
381
+ errorType: "format",
382
+ };
383
+ }
384
+ // Return success for format validation - actual AWS auth would require SDK
385
+ return { success: true };
386
+ }
387
+ catch (error) {
388
+ return {
389
+ success: false,
390
+ error: "AWS credentials validation failed",
391
+ errorType: "auth",
392
+ };
393
+ }
394
+ }
395
+ /**
396
+ * Google Vertex AI authentication validation
397
+ */
398
+ async function validateVertexAuth(credentialPath) {
399
+ // Vertex AI uses service account JSON files or Application Default Credentials
400
+ try {
401
+ if (credentialPath && credentialPath.endsWith(".json")) {
402
+ // Check if the service account file exists and is valid JSON
403
+ const fs = await import("fs");
404
+ if (!fs.existsSync(credentialPath)) {
405
+ return {
406
+ success: false,
407
+ error: "Service account file not found",
408
+ errorType: "config",
409
+ };
410
+ }
411
+ try {
412
+ const serviceAccount = JSON.parse(fs.readFileSync(credentialPath, "utf8"));
413
+ if (!serviceAccount.type || serviceAccount.type !== "service_account") {
414
+ return {
415
+ success: false,
416
+ error: "Invalid service account file format",
417
+ errorType: "format",
418
+ };
419
+ }
420
+ return { success: true };
421
+ }
422
+ catch {
423
+ return {
424
+ success: false,
425
+ error: "Invalid service account JSON format",
426
+ errorType: "format",
427
+ };
428
+ }
429
+ }
430
+ else if (process.env.GOOGLE_VERTEX_PROJECT) {
431
+ // If project ID is configured, assume ADC is being used
432
+ return { success: true };
433
+ }
434
+ else {
435
+ return {
436
+ success: false,
437
+ error: "No valid Vertex AI credentials found",
438
+ errorType: "config",
439
+ };
440
+ }
441
+ }
442
+ catch (error) {
443
+ return {
444
+ success: false,
445
+ error: "Vertex AI credentials validation failed",
446
+ errorType: "auth",
447
+ };
448
+ }
449
+ }
450
+ /**
451
+ * Special validation for Ollama (local service)
452
+ */
453
+ async function validateOllamaAvailability() {
454
+ try {
455
+ const result = await defaultTimeoutManager.executeWithTimeout(async () => {
456
+ const response = await fetch("http://localhost:11434/api/tags", {
457
+ method: "GET",
458
+ });
459
+ if (!response.ok) {
460
+ return { available: false, error: "Ollama service not responding" };
461
+ }
462
+ const data = await response.json();
463
+ const models = data.models || [];
464
+ return {
465
+ available: true,
466
+ models: models.map((m) => m.name),
467
+ hasModels: models.length > 0,
468
+ };
469
+ }, {
470
+ operation: "ollama-check",
471
+ timeout: 5000,
472
+ });
473
+ if (result.success && result.data) {
474
+ const { available, models, hasModels, error } = result.data;
475
+ return {
476
+ configured: true,
477
+ formatValid: true,
478
+ authenticated: available,
479
+ available: available && (hasModels || false),
480
+ error: !available
481
+ ? error
482
+ : !hasModels
483
+ ? 'No models installed. Run "ollama pull <model-name>"'
484
+ : undefined,
485
+ errorType: !available ? "network" : !hasModels ? "config" : undefined,
486
+ responseTime: result.executionTime,
487
+ details: { models, modelCount: models?.length || 0 },
488
+ };
489
+ }
490
+ else {
491
+ return {
492
+ configured: true,
493
+ formatValid: true,
494
+ authenticated: false,
495
+ available: false,
496
+ error: 'Ollama service not running. Start with "ollama serve"',
497
+ errorType: "network",
498
+ responseTime: result.executionTime,
499
+ };
500
+ }
501
+ }
502
+ catch (error) {
503
+ return {
504
+ configured: true,
505
+ formatValid: true,
506
+ authenticated: false,
507
+ available: false,
508
+ error: "Failed to check Ollama service",
509
+ errorType: "network",
510
+ };
511
+ }
512
+ }
513
+ /**
514
+ * Comprehensive provider validation that prevents false positives
515
+ */
516
+ export async function validateProvider(provider) {
517
+ // Special case for Ollama (no API key required)
518
+ if (provider.toLowerCase() === "ollama") {
519
+ return await validateOllamaAvailability();
520
+ }
521
+ // Step 1: Check if provider environment variables are configured
522
+ const configured = hasProviderEnvVars(provider);
523
+ if (!configured) {
524
+ return {
525
+ configured: false,
526
+ formatValid: false,
527
+ authenticated: false,
528
+ available: false,
529
+ error: "Missing required environment variables",
530
+ errorType: "config",
531
+ };
532
+ }
533
+ // Step 2: Get and validate API key format
534
+ const apiKey = getProviderApiKey(provider);
535
+ if (!apiKey) {
536
+ return {
537
+ configured: false,
538
+ formatValid: false,
539
+ authenticated: false,
540
+ available: false,
541
+ error: "API key not found in environment",
542
+ errorType: "config",
543
+ };
544
+ }
545
+ const formatValid = validateApiKeyFormat(provider, apiKey);
546
+ if (!formatValid) {
547
+ return {
548
+ configured: true,
549
+ formatValid: false,
550
+ authenticated: false,
551
+ available: false,
552
+ error: "API key format is invalid",
553
+ errorType: "format",
554
+ };
555
+ }
556
+ // Step 3: Perform lightweight authentication check
557
+ const authResult = await validateAuthentication(provider, apiKey);
558
+ return {
559
+ configured: true,
560
+ formatValid: true,
561
+ authenticated: authResult.success,
562
+ available: authResult.success,
563
+ error: authResult.error,
564
+ errorType: authResult.errorType,
565
+ responseTime: authResult.responseTime,
566
+ };
567
+ }
568
+ /**
569
+ * Batch validate multiple providers efficiently
570
+ */
571
+ export async function validateProviders(providers) {
572
+ const results = {};
573
+ // 🔧 FIX: Add timeout handling for provider validation
574
+ const validationPromises = providers.map(async (provider) => {
575
+ const timeoutPromise = new Promise((_, reject) => setTimeout(() => reject(new Error("Timeout")), 5000));
576
+ try {
577
+ const result = await Promise.race([
578
+ validateProvider(provider),
579
+ timeoutPromise,
580
+ ]);
581
+ return { provider, result };
582
+ }
583
+ catch (error) {
584
+ return {
585
+ provider,
586
+ result: {
587
+ configured: false,
588
+ formatValid: false,
589
+ authenticated: false,
590
+ available: false,
591
+ error: error instanceof Error ? error.message : "Timeout",
592
+ errorType: "network",
593
+ },
594
+ };
595
+ }
596
+ });
597
+ const validationResults = await Promise.allSettled(validationPromises);
598
+ validationResults.forEach((promiseResult, index) => {
599
+ const provider = providers[index];
600
+ if (promiseResult.status === "fulfilled") {
601
+ results[provider] = promiseResult.value.result;
602
+ }
603
+ else {
604
+ results[provider] = {
605
+ configured: false,
606
+ formatValid: false,
607
+ authenticated: false,
608
+ available: false,
609
+ error: "Validation failed: " + promiseResult.reason,
610
+ errorType: "unknown",
611
+ };
612
+ }
613
+ });
614
+ return results;
615
+ }
616
+ /**
617
+ * Check if provider validation should be cached (to avoid rate limits)
618
+ */
619
+ export function shouldCacheValidation(result) {
620
+ // Cache successful validations and format errors (don't change often)
621
+ // Don't cache network errors or quota errors (can be temporary)
622
+ return (result.available ||
623
+ result.errorType === "format" ||
624
+ result.errorType === "config");
625
+ }
@@ -9,6 +9,25 @@ import { hasProviderEnvVars } from "./providerUtils.js";
9
9
  * @returns The best provider name to use
10
10
  */
11
11
  export async function getBestProvider(requestedProvider) {
12
+ // 🔧 FIX: Check for explicit default provider in env
13
+ if (process.env.DEFAULT_PROVIDER &&
14
+ (await isProviderAvailable(process.env.DEFAULT_PROVIDER))) {
15
+ logger.debug(`[getBestProvider] Using DEFAULT_PROVIDER: ${process.env.DEFAULT_PROVIDER}`);
16
+ return process.env.DEFAULT_PROVIDER;
17
+ }
18
+ // 🔧 FIX: Special case for Ollama when explicitly configured
19
+ if (process.env.OLLAMA_BASE_URL && process.env.OLLAMA_MODEL) {
20
+ // Quick connectivity check for Ollama
21
+ try {
22
+ if (await isProviderAvailable("ollama")) {
23
+ logger.debug(`[getBestProvider] Prioritizing working local Ollama`);
24
+ return "ollama"; // Prioritize working local AI
25
+ }
26
+ }
27
+ catch {
28
+ // Fall through to cloud providers
29
+ }
30
+ }
12
31
  const providers = [
13
32
  "google-ai",
14
33
  "anthropic",
@@ -18,7 +37,7 @@ export async function getBestProvider(requestedProvider) {
18
37
  "azure",
19
38
  "huggingface",
20
39
  "bedrock",
21
- "ollama",
40
+ "ollama", // Keep as fallback
22
41
  ];
23
42
  if (requestedProvider && requestedProvider !== "auto") {
24
43
  if (await isProviderAvailable(requestedProvider)) {
@@ -1,9 +1,9 @@
1
1
  /**
2
- * Get the best available provider based on preferences and availability
2
+ * Get the best available provider based on preferences and availability (async)
3
3
  * @param requestedProvider - Optional preferred provider name
4
4
  * @returns The best provider name to use
5
5
  */
6
- export declare function getBestProviderSync(requestedProvider?: string): string;
6
+ export declare function getBestProvider(requestedProvider?: string): Promise<string>;
7
7
  /**
8
8
  * Check if a provider has the minimum required environment variables
9
9
  * NOTE: This only checks if variables exist, not if they're valid