@juspay/neurolink 9.51.4 → 9.53.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 (186) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +19 -0
  3. package/dist/agent/directTools.d.ts +2 -2
  4. package/dist/auth/errors.d.ts +1 -1
  5. package/dist/auth/middleware/AuthMiddleware.d.ts +1 -1
  6. package/dist/auth/providers/BaseAuthProvider.d.ts +1 -1
  7. package/dist/autoresearch/config.d.ts +11 -0
  8. package/dist/autoresearch/config.js +108 -0
  9. package/dist/autoresearch/errors.d.ts +40 -0
  10. package/dist/autoresearch/errors.js +20 -0
  11. package/dist/autoresearch/index.d.ts +23 -0
  12. package/dist/autoresearch/index.js +34 -0
  13. package/dist/autoresearch/phasePolicy.d.ts +9 -0
  14. package/dist/autoresearch/phasePolicy.js +69 -0
  15. package/dist/autoresearch/promptCompiler.d.ts +15 -0
  16. package/dist/autoresearch/promptCompiler.js +120 -0
  17. package/dist/autoresearch/repoPolicy.d.ts +32 -0
  18. package/dist/autoresearch/repoPolicy.js +128 -0
  19. package/dist/autoresearch/resultRecorder.d.ts +20 -0
  20. package/dist/autoresearch/resultRecorder.js +130 -0
  21. package/dist/autoresearch/runner.d.ts +10 -0
  22. package/dist/autoresearch/runner.js +102 -0
  23. package/dist/autoresearch/stateStore.d.ts +12 -0
  24. package/dist/autoresearch/stateStore.js +163 -0
  25. package/dist/autoresearch/summaryParser.d.ts +16 -0
  26. package/dist/autoresearch/summaryParser.js +94 -0
  27. package/dist/autoresearch/tools.d.ts +257 -0
  28. package/dist/autoresearch/tools.js +617 -0
  29. package/dist/autoresearch/worker.d.ts +71 -0
  30. package/dist/autoresearch/worker.js +417 -0
  31. package/dist/browser/neurolink.min.js +340 -324
  32. package/dist/cli/commands/autoresearch.d.ts +41 -0
  33. package/dist/cli/commands/autoresearch.js +487 -0
  34. package/dist/cli/commands/config.d.ts +1 -1
  35. package/dist/cli/commands/task.d.ts +2 -0
  36. package/dist/cli/commands/task.js +32 -3
  37. package/dist/cli/loop/optionsSchema.d.ts +1 -1
  38. package/dist/cli/parser.js +4 -1
  39. package/dist/core/baseProvider.js +18 -0
  40. package/dist/core/factory.d.ts +2 -2
  41. package/dist/core/factory.js +4 -4
  42. package/dist/evaluation/errors/EvaluationError.d.ts +1 -1
  43. package/dist/factories/providerFactory.d.ts +4 -4
  44. package/dist/factories/providerFactory.js +20 -7
  45. package/dist/factories/providerRegistry.d.ts +5 -0
  46. package/dist/factories/providerRegistry.js +45 -26
  47. package/dist/lib/agent/directTools.d.ts +2 -2
  48. package/dist/lib/auth/errors.d.ts +1 -1
  49. package/dist/lib/auth/middleware/AuthMiddleware.d.ts +1 -1
  50. package/dist/lib/auth/providers/BaseAuthProvider.d.ts +1 -1
  51. package/dist/lib/autoresearch/config.d.ts +11 -0
  52. package/dist/lib/autoresearch/config.js +109 -0
  53. package/dist/lib/autoresearch/errors.d.ts +40 -0
  54. package/dist/lib/autoresearch/errors.js +21 -0
  55. package/dist/lib/autoresearch/index.d.ts +23 -0
  56. package/dist/lib/autoresearch/index.js +35 -0
  57. package/dist/lib/autoresearch/phasePolicy.d.ts +9 -0
  58. package/dist/lib/autoresearch/phasePolicy.js +70 -0
  59. package/dist/lib/autoresearch/promptCompiler.d.ts +15 -0
  60. package/dist/lib/autoresearch/promptCompiler.js +121 -0
  61. package/dist/lib/autoresearch/repoPolicy.d.ts +32 -0
  62. package/dist/lib/autoresearch/repoPolicy.js +129 -0
  63. package/dist/lib/autoresearch/resultRecorder.d.ts +20 -0
  64. package/dist/lib/autoresearch/resultRecorder.js +131 -0
  65. package/dist/lib/autoresearch/runner.d.ts +10 -0
  66. package/dist/lib/autoresearch/runner.js +103 -0
  67. package/dist/lib/autoresearch/stateStore.d.ts +12 -0
  68. package/dist/lib/autoresearch/stateStore.js +164 -0
  69. package/dist/lib/autoresearch/summaryParser.d.ts +16 -0
  70. package/dist/lib/autoresearch/summaryParser.js +95 -0
  71. package/dist/lib/autoresearch/tools.d.ts +257 -0
  72. package/dist/lib/autoresearch/tools.js +618 -0
  73. package/dist/lib/autoresearch/worker.d.ts +71 -0
  74. package/dist/lib/autoresearch/worker.js +418 -0
  75. package/dist/lib/core/baseProvider.js +18 -0
  76. package/dist/lib/core/factory.d.ts +2 -2
  77. package/dist/lib/core/factory.js +4 -4
  78. package/dist/lib/evaluation/errors/EvaluationError.d.ts +1 -1
  79. package/dist/lib/factories/providerFactory.d.ts +4 -4
  80. package/dist/lib/factories/providerFactory.js +20 -7
  81. package/dist/lib/factories/providerRegistry.d.ts +5 -0
  82. package/dist/lib/factories/providerRegistry.js +45 -26
  83. package/dist/lib/files/fileTools.d.ts +1 -1
  84. package/dist/lib/neurolink.d.ts +21 -0
  85. package/dist/lib/neurolink.js +91 -8
  86. package/dist/lib/providers/amazonBedrock.d.ts +6 -1
  87. package/dist/lib/providers/amazonBedrock.js +14 -2
  88. package/dist/lib/providers/amazonSagemaker.d.ts +7 -1
  89. package/dist/lib/providers/amazonSagemaker.js +21 -3
  90. package/dist/lib/providers/anthropic.d.ts +4 -1
  91. package/dist/lib/providers/anthropic.js +18 -5
  92. package/dist/lib/providers/azureOpenai.d.ts +2 -1
  93. package/dist/lib/providers/azureOpenai.js +10 -5
  94. package/dist/lib/providers/googleAiStudio.d.ts +4 -1
  95. package/dist/lib/providers/googleAiStudio.js +6 -7
  96. package/dist/lib/providers/googleVertex.d.ts +3 -1
  97. package/dist/lib/providers/googleVertex.js +96 -17
  98. package/dist/lib/providers/huggingFace.d.ts +2 -1
  99. package/dist/lib/providers/huggingFace.js +4 -4
  100. package/dist/lib/providers/litellm.d.ts +5 -1
  101. package/dist/lib/providers/litellm.js +16 -11
  102. package/dist/lib/providers/mistral.d.ts +2 -1
  103. package/dist/lib/providers/mistral.js +2 -2
  104. package/dist/lib/providers/ollama.d.ts +3 -1
  105. package/dist/lib/providers/ollama.js +2 -2
  106. package/dist/lib/providers/openAI.d.ts +5 -1
  107. package/dist/lib/providers/openAI.js +15 -5
  108. package/dist/lib/providers/openRouter.d.ts +5 -1
  109. package/dist/lib/providers/openRouter.js +19 -7
  110. package/dist/lib/providers/openaiCompatible.d.ts +4 -1
  111. package/dist/lib/providers/openaiCompatible.js +18 -4
  112. package/dist/lib/tasks/autoresearchTaskExecutor.d.ts +32 -0
  113. package/dist/lib/tasks/autoresearchTaskExecutor.js +303 -0
  114. package/dist/lib/tasks/errors.d.ts +3 -1
  115. package/dist/lib/tasks/errors.js +1 -0
  116. package/dist/lib/tasks/taskExecutor.d.ts +4 -2
  117. package/dist/lib/tasks/taskExecutor.js +8 -1
  118. package/dist/lib/tasks/taskManager.js +27 -3
  119. package/dist/lib/tasks/tools/taskTools.d.ts +1 -1
  120. package/dist/lib/telemetry/attributes.d.ts +15 -0
  121. package/dist/lib/telemetry/attributes.js +16 -0
  122. package/dist/lib/telemetry/tracers.d.ts +1 -0
  123. package/dist/lib/telemetry/tracers.js +1 -0
  124. package/dist/lib/types/autoresearchTypes.d.ts +194 -0
  125. package/dist/lib/types/autoresearchTypes.js +18 -0
  126. package/dist/lib/types/common.d.ts +11 -0
  127. package/dist/lib/types/configTypes.d.ts +7 -0
  128. package/dist/lib/types/generateTypes.d.ts +13 -0
  129. package/dist/lib/types/index.d.ts +16 -14
  130. package/dist/lib/types/index.js +21 -17
  131. package/dist/lib/types/providers.d.ts +75 -0
  132. package/dist/lib/types/streamTypes.d.ts +7 -1
  133. package/dist/lib/types/taskTypes.d.ts +38 -0
  134. package/dist/lib/workflow/config.d.ts +3 -3
  135. package/dist/neurolink.d.ts +21 -0
  136. package/dist/neurolink.js +91 -8
  137. package/dist/providers/amazonBedrock.d.ts +6 -1
  138. package/dist/providers/amazonBedrock.js +14 -2
  139. package/dist/providers/amazonSagemaker.d.ts +7 -1
  140. package/dist/providers/amazonSagemaker.js +21 -3
  141. package/dist/providers/anthropic.d.ts +4 -1
  142. package/dist/providers/anthropic.js +18 -5
  143. package/dist/providers/azureOpenai.d.ts +2 -1
  144. package/dist/providers/azureOpenai.js +10 -5
  145. package/dist/providers/googleAiStudio.d.ts +4 -1
  146. package/dist/providers/googleAiStudio.js +6 -7
  147. package/dist/providers/googleVertex.d.ts +3 -1
  148. package/dist/providers/googleVertex.js +96 -17
  149. package/dist/providers/huggingFace.d.ts +2 -1
  150. package/dist/providers/huggingFace.js +4 -4
  151. package/dist/providers/litellm.d.ts +5 -1
  152. package/dist/providers/litellm.js +16 -11
  153. package/dist/providers/mistral.d.ts +2 -1
  154. package/dist/providers/mistral.js +2 -2
  155. package/dist/providers/ollama.d.ts +3 -1
  156. package/dist/providers/ollama.js +2 -2
  157. package/dist/providers/openAI.d.ts +5 -1
  158. package/dist/providers/openAI.js +15 -5
  159. package/dist/providers/openRouter.d.ts +5 -1
  160. package/dist/providers/openRouter.js +19 -7
  161. package/dist/providers/openaiCompatible.d.ts +4 -1
  162. package/dist/providers/openaiCompatible.js +18 -4
  163. package/dist/rag/errors/RAGError.d.ts +1 -1
  164. package/dist/tasks/autoresearchTaskExecutor.d.ts +32 -0
  165. package/dist/tasks/autoresearchTaskExecutor.js +302 -0
  166. package/dist/tasks/errors.d.ts +3 -1
  167. package/dist/tasks/errors.js +1 -0
  168. package/dist/tasks/taskExecutor.d.ts +4 -2
  169. package/dist/tasks/taskExecutor.js +8 -1
  170. package/dist/tasks/taskManager.js +27 -3
  171. package/dist/tasks/tools/taskTools.d.ts +1 -1
  172. package/dist/telemetry/attributes.d.ts +15 -0
  173. package/dist/telemetry/attributes.js +16 -0
  174. package/dist/telemetry/tracers.d.ts +1 -0
  175. package/dist/telemetry/tracers.js +1 -0
  176. package/dist/types/autoresearchTypes.d.ts +194 -0
  177. package/dist/types/autoresearchTypes.js +17 -0
  178. package/dist/types/common.d.ts +11 -0
  179. package/dist/types/configTypes.d.ts +7 -0
  180. package/dist/types/generateTypes.d.ts +13 -0
  181. package/dist/types/index.d.ts +16 -14
  182. package/dist/types/index.js +21 -17
  183. package/dist/types/providers.d.ts +75 -0
  184. package/dist/types/streamTypes.d.ts +7 -1
  185. package/dist/types/taskTypes.d.ts +38 -0
  186. package/package.json +3 -2
@@ -96,9 +96,9 @@ const hasGoogleCredentials = () => {
96
96
  // Module-level cache for runtime-created credentials file to avoid per-request writes
97
97
  let cachedCredentialsPath = null;
98
98
  // Enhanced Vertex settings creation with authentication fallback and proxy support
99
- const createVertexSettings = async (region) => {
100
- const location = region || getVertexLocation();
101
- const project = getVertexProjectId();
99
+ const createVertexSettings = async (region, credentials) => {
100
+ const location = credentials?.location || region || getVertexLocation();
101
+ const project = credentials?.projectId || getVertexProjectId();
102
102
  const baseSettings = {
103
103
  project,
104
104
  location,
@@ -115,6 +115,35 @@ const createVertexSettings = async (region) => {
115
115
  project,
116
116
  });
117
117
  }
118
+ // ── Per-request credentials (highest priority, never touches module-level cache) ──
119
+ if (credentials) {
120
+ // Express Mode: API key auth (Vertex Express)
121
+ if (credentials.apiKey) {
122
+ return { ...baseSettings, apiKey: credentials.apiKey };
123
+ }
124
+ // Resolve client_email / private_key from inline fields or serviceAccountKey JSON
125
+ const resolvedClientEmail = credentials.clientEmail ||
126
+ (credentials.serviceAccountKey
127
+ ? JSON.parse(credentials.serviceAccountKey)
128
+ .client_email
129
+ : undefined);
130
+ const resolvedPrivateKey = credentials.privateKey ||
131
+ (credentials.serviceAccountKey
132
+ ? JSON.parse(credentials.serviceAccountKey)
133
+ .private_key
134
+ : undefined);
135
+ if (resolvedClientEmail && resolvedPrivateKey) {
136
+ return {
137
+ ...baseSettings,
138
+ googleAuthOptions: {
139
+ credentials: {
140
+ client_email: resolvedClientEmail,
141
+ private_key: resolvedPrivateKey.replace(/\\n/g, "\n"),
142
+ },
143
+ },
144
+ };
145
+ }
146
+ }
118
147
  // 🎯 OPTION 2: Create credentials file from environment variables at runtime
119
148
  // This solves the problem where GOOGLE_APPLICATION_CREDENTIALS exists in ZSHRC locally
120
149
  // but the file doesn't exist on production servers
@@ -290,20 +319,21 @@ const createVertexSettings = async (region) => {
290
319
  return baseSettings;
291
320
  };
292
321
  // Create Anthropic-specific Vertex settings with the same authentication and proxy support
293
- const createVertexAnthropicSettings = async (region) => {
322
+ const createVertexAnthropicSettings = async (region, credentials) => {
294
323
  // The @ai-sdk/google-vertex SDK constructs Anthropic URLs as:
295
324
  // https://{location}-aiplatform.googleapis.com/...
296
325
  // When location is "global", this creates "https://global-aiplatform.googleapis.com"
297
326
  // which is invalid. The correct global endpoint omits the region prefix entirely.
298
327
  // Since the SDK doesn't handle this, redirect "global" to "us-east5" for Anthropic.
299
328
  const anthropicRegion = !region || region === "global" ? "us-east5" : region;
300
- const baseVertexSettings = await createVertexSettings(anthropicRegion);
329
+ const baseVertexSettings = await createVertexSettings(anthropicRegion, credentials);
301
330
  // GoogleVertexAnthropicProviderSettings extends GoogleVertexProviderSettings
302
331
  // so we can use the same settings with proper typing
303
332
  return {
304
333
  project: baseVertexSettings.project,
305
334
  location: baseVertexSettings.location,
306
335
  fetch: baseVertexSettings.fetch,
336
+ ...(baseVertexSettings.apiKey && { apiKey: baseVertexSettings.apiKey }),
307
337
  ...(baseVertexSettings.googleAuthOptions && {
308
338
  googleAuthOptions: baseVertexSettings.googleAuthOptions,
309
339
  }),
@@ -397,6 +427,7 @@ export class GoogleVertexProvider extends BaseProvider {
397
427
  location;
398
428
  registeredTools = new Map();
399
429
  toolContext = {};
430
+ credentials;
400
431
  // Memory-managed cache for model configuration lookups to avoid repeated calls
401
432
  // Uses WeakMap for automatic cleanup and bounded LRU for recently used models
402
433
  static modelConfigCache = new Map();
@@ -406,15 +437,17 @@ export class GoogleVertexProvider extends BaseProvider {
406
437
  // Memory-managed cache for maxTokens handling decisions to optimize streaming performance
407
438
  static maxTokensCache = new Map();
408
439
  static maxTokensCacheTime = 0;
409
- constructor(modelName, _providerName, sdk, region) {
440
+ constructor(modelName, _providerName, sdk, region, credentials) {
410
441
  super(modelName, "vertex", sdk);
442
+ this.credentials = credentials;
411
443
  // Validate Google Cloud credentials - now using consolidated utility
412
- if (!hasGoogleCredentials()) {
444
+ // Skip env-var validation when per-request credentials are provided
445
+ if (!credentials && !hasGoogleCredentials()) {
413
446
  validateApiKey(createGoogleAuthConfig());
414
447
  }
415
448
  // Initialize Google Cloud configuration
416
- this.projectId = getVertexProjectId();
417
- this.location = region || getVertexLocation();
449
+ this.projectId = credentials?.projectId || getVertexProjectId();
450
+ this.location = credentials?.location || region || getVertexLocation();
418
451
  logger.debug("[GoogleVertexProvider] Constructor initialized", {
419
452
  regionParam: region,
420
453
  resolvedLocation: this.location,
@@ -549,7 +582,7 @@ export class GoogleVertexProvider extends BaseProvider {
549
582
  message: "Starting Vertex settings creation with network configuration analysis",
550
583
  });
551
584
  try {
552
- const vertexSettings = await createVertexSettings(this.location);
585
+ const vertexSettings = await createVertexSettings(this.location, this.credentials);
553
586
  const vertexSettingsEndTime = process.hrtime.bigint();
554
587
  const vertexSettingsDurationNs = vertexSettingsEndTime - vertexSettingsStartTime;
555
588
  logger.debug(`[GoogleVertexProvider] ✅ LOG_POINT_V009_VERTEX_SETTINGS_SUCCESS`, {
@@ -1084,8 +1117,11 @@ export class GoogleVertexProvider extends BaseProvider {
1084
1117
  * Create @google/genai client configured for Vertex AI
1085
1118
  */
1086
1119
  async createVertexGenAIClient(regionOverride) {
1087
- const project = getVertexProjectId();
1088
- const location = regionOverride || this.location || getVertexLocation();
1120
+ const project = this.credentials?.projectId || getVertexProjectId();
1121
+ const location = this.credentials?.location ||
1122
+ regionOverride ||
1123
+ this.location ||
1124
+ getVertexLocation();
1089
1125
  const mod = await import("@google/genai");
1090
1126
  const ctor = mod.GoogleGenAI;
1091
1127
  if (!ctor) {
@@ -1099,7 +1135,42 @@ export class GoogleVertexProvider extends BaseProvider {
1099
1135
  });
1100
1136
  }
1101
1137
  const Ctor = ctor;
1102
- // Use vertexai mode with project and location
1138
+ // Per-request credentials: Express Mode (API key)
1139
+ if (this.credentials?.apiKey) {
1140
+ // Cast via unknown because GoogleGenAIClass union doesn't include apiKey+vertexai
1141
+ return new Ctor({
1142
+ vertexai: true,
1143
+ project,
1144
+ location,
1145
+ apiKey: this.credentials.apiKey,
1146
+ });
1147
+ }
1148
+ // Per-request credentials: inline service account
1149
+ if (this.credentials?.clientEmail || this.credentials?.serviceAccountKey) {
1150
+ const clientEmail = this.credentials.clientEmail ||
1151
+ (this.credentials.serviceAccountKey
1152
+ ? JSON.parse(this.credentials.serviceAccountKey).client_email
1153
+ : undefined);
1154
+ const privateKey = this.credentials.privateKey ||
1155
+ (this.credentials.serviceAccountKey
1156
+ ? JSON.parse(this.credentials.serviceAccountKey).private_key
1157
+ : undefined);
1158
+ if (clientEmail && privateKey) {
1159
+ // Cast via unknown because GoogleGenAIClass union doesn't include googleAuthOptions
1160
+ return new Ctor({
1161
+ vertexai: true,
1162
+ project,
1163
+ location,
1164
+ googleAuthOptions: {
1165
+ credentials: {
1166
+ client_email: clientEmail,
1167
+ private_key: privateKey.replace(/\\n/g, "\n"),
1168
+ },
1169
+ },
1170
+ });
1171
+ }
1172
+ }
1173
+ // Fallback: env-var / ADC auth
1103
1174
  return new Ctor({
1104
1175
  vertexai: true,
1105
1176
  project,
@@ -1849,7 +1920,15 @@ export class GoogleVertexProvider extends BaseProvider {
1849
1920
  return null;
1850
1921
  }
1851
1922
  // 2. Authentication Validation
1852
- const authValidation = await this.validateVertexAuthentication();
1923
+ // Per-request credentials bypass env-var auth checks entirely
1924
+ const hasPerRequestAuth = this.credentials &&
1925
+ (this.credentials.apiKey ||
1926
+ this.credentials.clientEmail ||
1927
+ this.credentials.privateKey ||
1928
+ this.credentials.serviceAccountKey);
1929
+ const authValidation = hasPerRequestAuth
1930
+ ? { isValid: true, method: "per_request_credentials", issues: [] }
1931
+ : await this.validateVertexAuthentication();
1853
1932
  if (!authValidation.isValid) {
1854
1933
  logger.error("[GoogleVertexProvider] ❌ Authentication validation failed", {
1855
1934
  validationId,
@@ -1927,7 +2006,7 @@ export class GoogleVertexProvider extends BaseProvider {
1927
2006
  projectId: projectValidation.projectId,
1928
2007
  region: projectValidation.region,
1929
2008
  });
1930
- const vertexAnthropicSettings = await createVertexAnthropicSettings(this.location);
2009
+ const vertexAnthropicSettings = await createVertexAnthropicSettings(this.location, this.credentials);
1931
2010
  // 7. Settings Validation
1932
2011
  if (!vertexAnthropicSettings.project ||
1933
2012
  !vertexAnthropicSettings.location) {
@@ -2844,7 +2923,7 @@ export class GoogleVertexProvider extends BaseProvider {
2844
2923
  });
2845
2924
  try {
2846
2925
  // Create the Vertex provider with current settings
2847
- const vertexSettings = await createVertexSettings(this.location);
2926
+ const vertexSettings = await createVertexSettings(this.location, this.credentials);
2848
2927
  const vertex = createVertex(vertexSettings);
2849
2928
  // Get the text embedding model
2850
2929
  const embeddingModel = vertex.textEmbeddingModel(embeddingModelName);
@@ -2883,7 +2962,7 @@ export class GoogleVertexProvider extends BaseProvider {
2883
2962
  count: texts.length,
2884
2963
  });
2885
2964
  try {
2886
- const vertexSettings = await createVertexSettings(this.location);
2965
+ const vertexSettings = await createVertexSettings(this.location, this.credentials);
2887
2966
  const vertex = createVertex(vertexSettings);
2888
2967
  const embeddingModel = vertex.textEmbeddingModel(embeddingModelName);
2889
2968
  const result = await embedMany({
@@ -2,6 +2,7 @@ import { type LanguageModel, type Schema } from "ai";
2
2
  import type { ZodType } from "zod";
3
3
  import type { AIProviderName } from "../constants/enums.js";
4
4
  import { BaseProvider } from "../core/baseProvider.js";
5
+ import type { NeurolinkCredentials } from "../types/providers.js";
5
6
  import type { StreamOptions, StreamResult } from "../types/streamTypes.js";
6
7
  /**
7
8
  * HuggingFace Provider - BaseProvider Implementation
@@ -9,7 +10,7 @@ import type { StreamOptions, StreamResult } from "../types/streamTypes.js";
9
10
  */
10
11
  export declare class HuggingFaceProvider extends BaseProvider {
11
12
  private model;
12
- constructor(modelName?: string);
13
+ constructor(modelName?: string, _sdk?: unknown, credentials?: NeurolinkCredentials["huggingFace"]);
13
14
  /**
14
15
  * HuggingFace Tool Calling Support (Enhanced 2025)
15
16
  *
@@ -14,21 +14,21 @@ const getHuggingFaceApiKey = () => {
14
14
  const getDefaultHuggingFaceModel = () => {
15
15
  return getProviderModel("HUGGINGFACE_MODEL", "microsoft/DialoGPT-medium");
16
16
  };
17
- // Note: hasHuggingFaceCredentials now directly imported from consolidated utility
17
+ // Note: hasNeurolinkCredentials["huggingFace"] now directly imported from consolidated utility
18
18
  /**
19
19
  * HuggingFace Provider - BaseProvider Implementation
20
20
  * Using AI SDK with HuggingFace's OpenAI-compatible endpoint
21
21
  */
22
22
  export class HuggingFaceProvider extends BaseProvider {
23
23
  model;
24
- constructor(modelName) {
24
+ constructor(modelName, _sdk, credentials) {
25
25
  super(modelName, "huggingface");
26
26
  // Get API key and validate
27
- const apiKey = getHuggingFaceApiKey();
27
+ const apiKey = credentials?.apiKey ?? getHuggingFaceApiKey();
28
28
  // Create HuggingFace provider using unified router endpoint (2025) with proxy support
29
29
  const huggingface = createOpenAI({
30
30
  apiKey: apiKey,
31
- baseURL: "https://router.huggingface.co/v1",
31
+ baseURL: credentials?.baseURL ?? "https://router.huggingface.co/v1",
32
32
  fetch: createProxyFetch(),
33
33
  });
34
34
  // Initialize model
@@ -9,10 +9,14 @@ import type { StreamOptions, StreamResult } from "../types/streamTypes.js";
9
9
  */
10
10
  export declare class LiteLLMProvider extends BaseProvider {
11
11
  private model;
12
+ private credentials?;
12
13
  private static modelsCache;
13
14
  private static modelsCacheTime;
14
15
  private static readonly MODELS_CACHE_DURATION;
15
- constructor(modelName?: string, sdk?: unknown);
16
+ constructor(modelName?: string, sdk?: unknown, _region?: string, credentials?: {
17
+ apiKey?: string;
18
+ baseURL?: string;
19
+ });
16
20
  protected getProviderName(): AIProviderName;
17
21
  protected getDefaultModel(): string;
18
22
  /**
@@ -1,6 +1,6 @@
1
1
  import { createOpenAI } from "@ai-sdk/openai";
2
2
  import { SpanKind, SpanStatusCode, trace } from "@opentelemetry/api";
3
- import { NoOutputGeneratedError, Output, streamText, } from "ai";
3
+ import { NoOutputGeneratedError, Output, stepCountIs, streamText, } from "ai";
4
4
  import { BaseProvider } from "../core/baseProvider.js";
5
5
  import { DEFAULT_MAX_STEPS } from "../core/constants.js";
6
6
  import { streamAnalyticsCollector } from "../core/streamAnalytics.js";
@@ -42,12 +42,15 @@ const getDefaultLiteLLMModel = () => {
42
42
  */
43
43
  export class LiteLLMProvider extends BaseProvider {
44
44
  model;
45
+ credentials;
45
46
  // Cache for available models to avoid repeated API calls
46
47
  static modelsCache = [];
47
48
  static modelsCacheTime = 0;
48
49
  static MODELS_CACHE_DURATION = 10 * 60 * 1000; // 10 minutes
49
- constructor(modelName, sdk) {
50
+ constructor(modelName, sdk, _region, credentials) {
50
51
  super(modelName, "litellm", sdk);
52
+ // Store per-request credentials for use in embed/embedMany/fetchModelsFromAPI
53
+ this.credentials = credentials;
51
54
  // Initialize LiteLLM using OpenAI SDK with explicit configuration
52
55
  const config = getLiteLLMConfig();
53
56
  // Create OpenAI SDK instance configured for LiteLLM proxy
@@ -56,8 +59,8 @@ export class LiteLLMProvider extends BaseProvider {
56
59
  // with a custom baseURL and apiKey. This ensures all requests are routed through the LiteLLM
57
60
  // proxy, allowing access to multiple models and custom authentication.
58
61
  const customOpenAI = createOpenAI({
59
- baseURL: config.baseURL,
60
- apiKey: config.apiKey,
62
+ baseURL: credentials?.baseURL ?? config.baseURL,
63
+ apiKey: credentials?.apiKey ?? config.apiKey,
61
64
  fetch: createProxyFetch(),
62
65
  });
63
66
  this.model = customOpenAI.chat(this.modelName || getDefaultLiteLLMModel());
@@ -162,7 +165,7 @@ export class LiteLLMProvider extends BaseProvider {
162
165
  Object.keys(tools).length > 0 && {
163
166
  tools,
164
167
  toolChoice: resolveToolChoice(options, tools, shouldUseTools),
165
- maxSteps: options.maxSteps || DEFAULT_MAX_STEPS,
168
+ stopWhen: stepCountIs(options.maxSteps || DEFAULT_MAX_STEPS),
166
169
  }),
167
170
  abortSignal: composeAbortSignals(options.abortSignal, timeoutController?.controller.signal),
168
171
  experimental_telemetry: this.telemetryHandler.getTelemetryConfig(options),
@@ -374,8 +377,8 @@ export class LiteLLMProvider extends BaseProvider {
374
377
  process.env.LITELLM_EMBEDDING_MODEL ||
375
378
  "gemini-embedding-001";
376
379
  const customOpenAI = createOpenAI({
377
- baseURL: config.baseURL,
378
- apiKey: config.apiKey,
380
+ baseURL: this.credentials?.baseURL ?? config.baseURL,
381
+ apiKey: this.credentials?.apiKey ?? config.apiKey,
379
382
  fetch: createProxyFetch(),
380
383
  });
381
384
  const embeddingModel = customOpenAI.textEmbeddingModel(embeddingModelName);
@@ -394,8 +397,8 @@ export class LiteLLMProvider extends BaseProvider {
394
397
  process.env.LITELLM_EMBEDDING_MODEL ||
395
398
  "gemini-embedding-001";
396
399
  const customOpenAI = createOpenAI({
397
- baseURL: config.baseURL,
398
- apiKey: config.apiKey,
400
+ baseURL: this.credentials?.baseURL ?? config.baseURL,
401
+ apiKey: this.credentials?.apiKey ?? config.apiKey,
399
402
  fetch: createProxyFetch(),
400
403
  });
401
404
  const embeddingModel = customOpenAI.textEmbeddingModel(embeddingModelName);
@@ -458,7 +461,9 @@ export class LiteLLMProvider extends BaseProvider {
458
461
  async fetchModelsFromAPI() {
459
462
  const functionTag = "LiteLLMProvider.fetchModelsFromAPI";
460
463
  const config = getLiteLLMConfig();
461
- const modelsUrl = `${config.baseURL}/v1/models`;
464
+ const resolvedBaseURL = this.credentials?.baseURL ?? config.baseURL;
465
+ const resolvedApiKey = this.credentials?.apiKey ?? config.apiKey;
466
+ const modelsUrl = `${resolvedBaseURL}/v1/models`;
462
467
  const controller = new AbortController();
463
468
  const timeoutId = setTimeout(() => controller.abort(), 5000); // 5 second timeout
464
469
  try {
@@ -467,7 +472,7 @@ export class LiteLLMProvider extends BaseProvider {
467
472
  const response = await proxyFetch(modelsUrl, {
468
473
  method: "GET",
469
474
  headers: {
470
- Authorization: `Bearer ${config.apiKey}`,
475
+ Authorization: `Bearer ${resolvedApiKey}`,
471
476
  "Content-Type": "application/json",
472
477
  },
473
478
  signal: controller.signal,
@@ -1,6 +1,7 @@
1
1
  import { type LanguageModel } from "ai";
2
2
  import type { AIProviderName } from "../constants/enums.js";
3
3
  import { BaseProvider } from "../core/baseProvider.js";
4
+ import type { NeurolinkCredentials } from "../types/providers.js";
4
5
  import type { StreamOptions, StreamResult } from "../types/streamTypes.js";
5
6
  import type { ValidationSchema } from "../types/typeAliases.js";
6
7
  /**
@@ -9,7 +10,7 @@ import type { ValidationSchema } from "../types/typeAliases.js";
9
10
  */
10
11
  export declare class MistralProvider extends BaseProvider {
11
12
  private model;
12
- constructor(modelName?: string, sdk?: unknown);
13
+ constructor(modelName?: string, sdk?: unknown, _region?: string, credentials?: NeurolinkCredentials["mistral"]);
13
14
  protected executeStream(options: StreamOptions, _analysisSchema?: ValidationSchema): Promise<StreamResult>;
14
15
  getProviderName(): AIProviderName;
15
16
  getDefaultModel(): string;
@@ -23,14 +23,14 @@ const getDefaultMistralModel = () => {
23
23
  */
24
24
  export class MistralProvider extends BaseProvider {
25
25
  model;
26
- constructor(modelName, sdk) {
26
+ constructor(modelName, sdk, _region, credentials) {
27
27
  // Type guard for NeuroLink parameter validation
28
28
  const validatedNeurolink = sdk && typeof sdk === "object" && "getInMemoryServers" in sdk
29
29
  ? sdk
30
30
  : undefined;
31
31
  super(modelName, "mistral", validatedNeurolink);
32
32
  // Initialize Mistral model with API key validation and proxy support
33
- const apiKey = getMistralApiKey();
33
+ const apiKey = credentials?.apiKey ?? getMistralApiKey();
34
34
  const mistral = createMistral({
35
35
  apiKey: apiKey,
36
36
  fetch: createProxyFetch(),
@@ -18,7 +18,9 @@ export declare class OllamaProvider extends BaseProvider {
18
18
  private ollamaModel;
19
19
  private baseUrl;
20
20
  private timeout;
21
- constructor(modelName?: string);
21
+ constructor(modelName?: string, credentials?: {
22
+ baseURL?: string;
23
+ });
22
24
  protected getProviderName(): AIProviderName;
23
25
  protected getDefaultModel(): string;
24
26
  /**
@@ -523,9 +523,9 @@ export class OllamaProvider extends BaseProvider {
523
523
  ollamaModel;
524
524
  baseUrl;
525
525
  timeout;
526
- constructor(modelName) {
526
+ constructor(modelName, credentials) {
527
527
  super(modelName, "ollama");
528
- this.baseUrl = getOllamaBaseUrl();
528
+ this.baseUrl = credentials?.baseURL ?? getOllamaBaseUrl();
529
529
  this.timeout = getOllamaTimeout();
530
530
  // Initialize Ollama model
531
531
  this.ollamaModel = new OllamaLanguageModel(this.modelName || getDefaultOllamaModel(), this.baseUrl, this.timeout);
@@ -10,7 +10,11 @@ import type { ValidationSchema } from "../types/typeAliases.js";
10
10
  */
11
11
  export declare class OpenAIProvider extends BaseProvider {
12
12
  private model;
13
- constructor(modelName?: string, neurolink?: NeuroLink);
13
+ private credentials?;
14
+ constructor(modelName?: string, neurolink?: NeuroLink, _region?: string, credentials?: {
15
+ apiKey?: string;
16
+ baseURL?: string;
17
+ });
14
18
  /**
15
19
  * Check if this provider supports tool/function calling
16
20
  */
@@ -36,11 +36,14 @@ const streamTracer = trace.getTracer("neurolink.provider.openai");
36
36
  */
37
37
  export class OpenAIProvider extends BaseProvider {
38
38
  model;
39
- constructor(modelName, neurolink) {
39
+ credentials;
40
+ constructor(modelName, neurolink, _region, credentials) {
40
41
  super(modelName || getOpenAIModel(), AIProviderName.OPENAI, neurolink);
42
+ this.credentials = credentials;
41
43
  // Initialize OpenAI provider with proxy support
42
44
  const openai = createOpenAI({
43
- apiKey: getOpenAIApiKey(),
45
+ apiKey: credentials?.apiKey ?? getOpenAIApiKey(),
46
+ ...(credentials?.baseURL ? { baseURL: credentials.baseURL } : {}),
44
47
  fetch: createProxyFetch(),
45
48
  });
46
49
  // Initialize model
@@ -554,9 +557,12 @@ export class OpenAIProvider extends BaseProvider {
554
557
  });
555
558
  try {
556
559
  // Create embedding model using the AI SDK
557
- // Create the OpenAI provider
560
+ // Create the OpenAI provider, preferring per-instance credentials over env vars
558
561
  const openai = createOpenAI({
559
- apiKey: getOpenAIApiKey(),
562
+ apiKey: this.credentials?.apiKey ?? getOpenAIApiKey(),
563
+ ...(this.credentials?.baseURL
564
+ ? { baseURL: this.credentials.baseURL }
565
+ : {}),
560
566
  fetch: createProxyFetch(),
561
567
  });
562
568
  // Get the text embedding model
@@ -596,8 +602,12 @@ export class OpenAIProvider extends BaseProvider {
596
602
  count: texts.length,
597
603
  });
598
604
  try {
605
+ // Prefer per-instance credentials over env vars
599
606
  const openai = createOpenAI({
600
- apiKey: getOpenAIApiKey(),
607
+ apiKey: this.credentials?.apiKey ?? getOpenAIApiKey(),
608
+ ...(this.credentials?.baseURL
609
+ ? { baseURL: this.credentials.baseURL }
610
+ : {}),
601
611
  fetch: createProxyFetch(),
602
612
  });
603
613
  const embeddingModel = openai.textEmbeddingModel(embeddingModelName);
@@ -10,12 +10,16 @@ import type { StreamOptions, StreamResult } from "../types/streamTypes.js";
10
10
  export declare class OpenRouterProvider extends BaseProvider {
11
11
  private model;
12
12
  private openRouterClient;
13
+ private config;
13
14
  private static modelsCache;
14
15
  private static modelsCacheTime;
15
16
  private static readonly MODELS_CACHE_DURATION;
16
17
  private static toolCapableModels;
17
18
  private static capabilitiesCached;
18
- constructor(modelName?: string, sdk?: unknown);
19
+ constructor(modelName?: string, sdk?: unknown, _region?: string, credentials?: {
20
+ apiKey?: string;
21
+ baseURL?: string;
22
+ });
19
23
  protected getProviderName(): AIProviderName;
20
24
  protected getDefaultModel(): string;
21
25
  /**
@@ -1,5 +1,5 @@
1
1
  import { createOpenRouter } from "@openrouter/ai-sdk-provider";
2
- import { NoOutputGeneratedError, Output, streamText, } from "ai";
2
+ import { NoOutputGeneratedError, Output, stepCountIs, streamText, } from "ai";
3
3
  import { AIProviderName } from "../constants/enums.js";
4
4
  import { BaseProvider } from "../core/baseProvider.js";
5
5
  import { DEFAULT_MAX_STEPS } from "../core/constants.js";
@@ -47,6 +47,7 @@ const getDefaultOpenRouterModel = () => {
47
47
  export class OpenRouterProvider extends BaseProvider {
48
48
  model;
49
49
  openRouterClient;
50
+ config;
50
51
  // Cache for available models to avoid repeated API calls
51
52
  static modelsCache = [];
52
53
  static modelsCacheTime = 0;
@@ -54,10 +55,20 @@ export class OpenRouterProvider extends BaseProvider {
54
55
  // Cache for model capabilities (which models support tools)
55
56
  static toolCapableModels = new Set();
56
57
  static capabilitiesCached = false;
57
- constructor(modelName, sdk) {
58
+ constructor(modelName, sdk, _region, credentials) {
58
59
  super(modelName, AIProviderName.OPENROUTER, sdk);
59
- // Initialize OpenRouter using the official SDK
60
- const config = getOpenRouterConfig();
60
+ // Build config: prefer credentials over env vars to avoid throwing when env vars are absent
61
+ if (credentials?.apiKey) {
62
+ this.config = {
63
+ apiKey: credentials.apiKey,
64
+ referer: process.env.OPENROUTER_REFERER,
65
+ appName: process.env.OPENROUTER_APP_NAME,
66
+ };
67
+ }
68
+ else {
69
+ this.config = getOpenRouterConfig(); // throws if OPENROUTER_API_KEY missing
70
+ }
71
+ const config = this.config;
61
72
  // Build headers for attribution on openrouter.ai/activity dashboard
62
73
  const headers = {};
63
74
  if (config.referer) {
@@ -69,6 +80,7 @@ export class OpenRouterProvider extends BaseProvider {
69
80
  // Create OpenRouter client with optional attribution headers
70
81
  this.openRouterClient = createOpenRouter({
71
82
  apiKey: config.apiKey,
83
+ ...(credentials?.baseURL ? { baseURL: credentials.baseURL } : {}),
72
84
  ...(Object.keys(headers).length > 0 && { headers }),
73
85
  });
74
86
  // Initialize model with OpenRouter client
@@ -236,7 +248,7 @@ export class OpenRouterProvider extends BaseProvider {
236
248
  Object.keys(tools).length > 0 && {
237
249
  tools,
238
250
  toolChoice: resolveToolChoice(options, tools, shouldUseTools),
239
- maxSteps: options.maxSteps || DEFAULT_MAX_STEPS,
251
+ stopWhen: stepCountIs(options.maxSteps || DEFAULT_MAX_STEPS),
240
252
  }),
241
253
  abortSignal: composeAbortSignals(options.abortSignal, timeoutController?.controller.signal),
242
254
  experimental_telemetry: this.telemetryHandler.getTelemetryConfig(options),
@@ -437,7 +449,7 @@ export class OpenRouterProvider extends BaseProvider {
437
449
  */
438
450
  async fetchModelsFromAPI() {
439
451
  const functionTag = "OpenRouterProvider.fetchModelsFromAPI";
440
- const config = getOpenRouterConfig();
452
+ const config = this.config;
441
453
  const modelsUrl = "https://openrouter.ai/api/v1/models";
442
454
  const controller = new AbortController();
443
455
  const timeoutId = setTimeout(() => controller.abort(), MODELS_DISCOVERY_TIMEOUT_MS);
@@ -499,7 +511,7 @@ export class OpenRouterProvider extends BaseProvider {
499
511
  return; // Already cached
500
512
  }
501
513
  try {
502
- const config = getOpenRouterConfig();
514
+ const config = this.config;
503
515
  const modelsUrl = "https://openrouter.ai/api/v1/models";
504
516
  const controller = new AbortController();
505
517
  const timeoutId = setTimeout(() => controller.abort(), MODELS_DISCOVERY_TIMEOUT_MS);
@@ -12,7 +12,10 @@ export declare class OpenAICompatibleProvider extends BaseProvider {
12
12
  private config;
13
13
  private discoveredModel?;
14
14
  private customOpenAI;
15
- constructor(modelName?: string, sdk?: unknown);
15
+ constructor(modelName?: string, sdk?: unknown, _region?: string, credentials?: {
16
+ apiKey?: string;
17
+ baseURL?: string;
18
+ });
16
19
  protected getProviderName(): AIProviderName;
17
20
  protected getDefaultModel(): string;
18
21
  /**
@@ -1,6 +1,7 @@
1
1
  import { createOpenAI } from "@ai-sdk/openai";
2
- import { NoOutputGeneratedError, streamText, } from "ai";
2
+ import { NoOutputGeneratedError, stepCountIs, streamText, } from "ai";
3
3
  import { BaseProvider } from "../core/baseProvider.js";
4
+ import { DEFAULT_MAX_STEPS } from "../core/constants.js";
4
5
  import { streamAnalyticsCollector } from "../core/streamAnalytics.js";
5
6
  import { createProxyFetch } from "../proxy/proxyFetch.js";
6
7
  import { logger } from "../utils/logger.js";
@@ -45,10 +46,22 @@ export class OpenAICompatibleProvider extends BaseProvider {
45
46
  config;
46
47
  discoveredModel;
47
48
  customOpenAI;
48
- constructor(modelName, sdk) {
49
+ constructor(modelName, sdk, _region, credentials) {
49
50
  super(modelName, "openai-compatible", sdk);
50
- // Initialize OpenAI Compatible configuration
51
- this.config = getOpenAICompatibleConfig();
51
+ // Build config: prefer credentials over env vars to avoid throwing when env vars are absent
52
+ if (credentials?.apiKey && credentials?.baseURL) {
53
+ this.config = {
54
+ apiKey: credentials.apiKey,
55
+ baseURL: credentials.baseURL,
56
+ };
57
+ }
58
+ else {
59
+ const envConfig = getOpenAICompatibleConfig(); // throws if env vars missing
60
+ this.config = {
61
+ apiKey: credentials?.apiKey ?? envConfig.apiKey,
62
+ baseURL: credentials?.baseURL ?? envConfig.baseURL,
63
+ };
64
+ }
52
65
  // Create OpenAI SDK instance configured for custom endpoint
53
66
  // This allows us to use OpenAI-compatible API by simply changing the baseURL
54
67
  this.customOpenAI = createOpenAI({
@@ -179,6 +192,7 @@ export class OpenAICompatibleProvider extends BaseProvider {
179
192
  : {}),
180
193
  tools,
181
194
  toolChoice: resolveToolChoice(options, tools, shouldUseTools),
195
+ stopWhen: stepCountIs(options.maxSteps || DEFAULT_MAX_STEPS),
182
196
  abortSignal: composeAbortSignals(options.abortSignal, timeoutController?.controller.signal),
183
197
  experimental_telemetry: this.telemetryHandler.getTelemetryConfig(options),
184
198
  onStepFinish: (event) => {
@@ -82,7 +82,7 @@ export declare const RAGErrorFactory: {
82
82
  readonly RETRY_EXHAUSTED: "RAG_RETRY_EXHAUSTED";
83
83
  readonly INVALID_CONFIGURATION: "RAG_INVALID_CONFIGURATION";
84
84
  };
85
- create: (code: "CHUNKING_ERROR" | "CHUNKING_INVALID_CONFIG" | "CHUNKING_STRATEGY_NOT_FOUND" | "CHUNKING_EMPTY_CONTENT" | "CHUNKING_SIZE_EXCEEDED" | "METADATA_EXTRACTION_ERROR" | "METADATA_EXTRACTION_TIMEOUT" | "METADATA_SCHEMA_INVALID" | "METADATA_EXTRACTOR_NOT_FOUND" | "EMBEDDING_ERROR" | "EMBEDDING_DIMENSION_MISMATCH" | "EMBEDDING_RATE_LIMIT" | "EMBEDDING_PROVIDER_ERROR" | "VECTOR_QUERY_ERROR" | "VECTOR_QUERY_TIMEOUT" | "VECTOR_STORE_UNAVAILABLE" | "VECTOR_STORE_CONNECTION_ERROR" | "VECTOR_INDEX_NOT_FOUND" | "RERANKER_ERROR" | "RERANKER_NOT_FOUND" | "RERANKER_API_ERROR" | "GRAPH_RAG_ERROR" | "GRAPH_TRAVERSAL_ERROR" | "GRAPH_NODE_NOT_FOUND" | "PIPELINE_ERROR" | "PIPELINE_STAGE_FAILED" | "PIPELINE_PARTIAL_FAILURE" | "CIRCUIT_BREAKER_OPEN" | "CIRCUIT_BREAKER_HALF_OPEN_LIMIT" | "OPERATION_TIMEOUT" | "RETRY_EXHAUSTED" | "INVALID_CONFIGURATION", message: string, options?: {
85
+ create: (code: "INVALID_CONFIGURATION" | "CHUNKING_ERROR" | "CHUNKING_INVALID_CONFIG" | "CHUNKING_STRATEGY_NOT_FOUND" | "CHUNKING_EMPTY_CONTENT" | "CHUNKING_SIZE_EXCEEDED" | "METADATA_EXTRACTION_ERROR" | "METADATA_EXTRACTION_TIMEOUT" | "METADATA_SCHEMA_INVALID" | "METADATA_EXTRACTOR_NOT_FOUND" | "EMBEDDING_ERROR" | "EMBEDDING_DIMENSION_MISMATCH" | "EMBEDDING_RATE_LIMIT" | "EMBEDDING_PROVIDER_ERROR" | "VECTOR_QUERY_ERROR" | "VECTOR_QUERY_TIMEOUT" | "VECTOR_STORE_UNAVAILABLE" | "VECTOR_STORE_CONNECTION_ERROR" | "VECTOR_INDEX_NOT_FOUND" | "RERANKER_ERROR" | "RERANKER_NOT_FOUND" | "RERANKER_API_ERROR" | "GRAPH_RAG_ERROR" | "GRAPH_TRAVERSAL_ERROR" | "GRAPH_NODE_NOT_FOUND" | "PIPELINE_ERROR" | "PIPELINE_STAGE_FAILED" | "PIPELINE_PARTIAL_FAILURE" | "CIRCUIT_BREAKER_OPEN" | "CIRCUIT_BREAKER_HALF_OPEN_LIMIT" | "OPERATION_TIMEOUT" | "RETRY_EXHAUSTED", message: string, options?: {
86
86
  retryable?: boolean;
87
87
  details?: Record<string, unknown>;
88
88
  cause?: Error;