@juspay/neurolink 7.28.1 → 7.29.1

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 (35) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/core/baseProvider.js +4 -2
  3. package/dist/index.d.ts +2 -3
  4. package/dist/index.js +1 -2
  5. package/dist/lib/core/baseProvider.js +4 -2
  6. package/dist/lib/core/dynamicModels.d.ts +6 -6
  7. package/dist/lib/index.d.ts +2 -3
  8. package/dist/lib/index.js +1 -2
  9. package/dist/lib/middleware/builtin/analytics.js +13 -14
  10. package/dist/lib/middleware/builtin/guardrails.d.ts +20 -0
  11. package/dist/lib/middleware/builtin/guardrails.js +87 -0
  12. package/dist/lib/middleware/factory.d.ts +29 -14
  13. package/dist/lib/middleware/factory.js +136 -110
  14. package/dist/lib/middleware/index.d.ts +3 -49
  15. package/dist/lib/middleware/index.js +4 -58
  16. package/dist/lib/middleware/registry.d.ts +1 -3
  17. package/dist/lib/middleware/registry.js +4 -5
  18. package/dist/lib/middleware/types.d.ts +3 -1
  19. package/dist/lib/neurolink.d.ts +297 -4
  20. package/dist/lib/neurolink.js +297 -4
  21. package/dist/lib/providers/googleVertex.js +13 -4
  22. package/dist/middleware/builtin/analytics.js +13 -14
  23. package/dist/middleware/builtin/guardrails.d.ts +20 -0
  24. package/dist/middleware/builtin/guardrails.js +87 -0
  25. package/dist/middleware/factory.d.ts +29 -14
  26. package/dist/middleware/factory.js +136 -110
  27. package/dist/middleware/index.d.ts +3 -49
  28. package/dist/middleware/index.js +4 -58
  29. package/dist/middleware/registry.d.ts +1 -3
  30. package/dist/middleware/registry.js +4 -5
  31. package/dist/middleware/types.d.ts +3 -1
  32. package/dist/neurolink.d.ts +297 -4
  33. package/dist/neurolink.js +297 -4
  34. package/dist/providers/googleVertex.js +13 -4
  35. package/package.json +1 -1
package/dist/neurolink.js CHANGED
@@ -65,6 +65,34 @@ export class NeuroLink {
65
65
  }
66
66
  // Conversation memory support
67
67
  conversationMemory;
68
+ /**
69
+ * Creates a new NeuroLink instance for AI text generation with MCP tool integration.
70
+ *
71
+ * @param config - Optional configuration object
72
+ * @param config.conversationMemory - Configuration for conversation memory features
73
+ * @param config.conversationMemory.enabled - Whether to enable conversation memory (default: false)
74
+ * @param config.conversationMemory.maxSessions - Maximum number of concurrent sessions (default: 100)
75
+ * @param config.conversationMemory.maxTurnsPerSession - Maximum conversation turns per session (default: 50)
76
+ *
77
+ * @example
78
+ * ```typescript
79
+ * // Basic usage
80
+ * const neurolink = new NeuroLink();
81
+ *
82
+ * // With conversation memory
83
+ * const neurolink = new NeuroLink({
84
+ * conversationMemory: {
85
+ * enabled: true,
86
+ * maxSessions: 50,
87
+ * maxTurnsPerSession: 20
88
+ * }
89
+ * });
90
+ * ```
91
+ *
92
+ * @throws {Error} When provider registry setup fails
93
+ * @throws {Error} When conversation memory initialization fails (if enabled)
94
+ * @throws {Error} When external server manager initialization fails
95
+ */
68
96
  constructor(config) {
69
97
  // 🚀 EXHAUSTIVE LOGGING POINT C001: CONSTRUCTOR ENTRY
70
98
  const constructorStartTime = Date.now();
@@ -724,6 +752,54 @@ export class NeuroLink {
724
752
  this.contextManager = new ContextManager(this.generateTextInternal.bind(this), contextConfig);
725
753
  logger.info("[NeuroLink] Automatic context summarization enabled.");
726
754
  }
755
+ /**
756
+ * Generate AI content using the best available provider with MCP tool integration.
757
+ * This is the primary method for text generation with full feature support.
758
+ *
759
+ * @param optionsOrPrompt - Either a string prompt or a comprehensive GenerateOptions object
760
+ * @param optionsOrPrompt.input - Input configuration object
761
+ * @param optionsOrPrompt.input.text - The text prompt to send to the AI (required)
762
+ * @param optionsOrPrompt.provider - AI provider to use ('auto', 'openai', 'anthropic', etc.)
763
+ * @param optionsOrPrompt.model - Specific model to use (e.g., 'gpt-4', 'claude-3-opus')
764
+ * @param optionsOrPrompt.temperature - Randomness in response (0.0 = deterministic, 2.0 = very random)
765
+ * @param optionsOrPrompt.maxTokens - Maximum tokens in response
766
+ * @param optionsOrPrompt.systemPrompt - System message to set AI behavior
767
+ * @param optionsOrPrompt.disableTools - Whether to disable MCP tool usage
768
+ * @param optionsOrPrompt.enableAnalytics - Whether to include usage analytics
769
+ * @param optionsOrPrompt.enableEvaluation - Whether to include response quality evaluation
770
+ * @param optionsOrPrompt.context - Additional context for the request
771
+ * @param optionsOrPrompt.evaluationDomain - Domain for specialized evaluation
772
+ * @param optionsOrPrompt.toolUsageContext - Context for tool usage decisions
773
+ *
774
+ * @returns Promise resolving to GenerateResult with content, usage data, and optional analytics
775
+ *
776
+ * @example
777
+ * ```typescript
778
+ * // Simple usage with string prompt
779
+ * const result = await neurolink.generate("What is artificial intelligence?");
780
+ * console.log(result.content);
781
+ *
782
+ * // Advanced usage with options
783
+ * const result = await neurolink.generate({
784
+ * input: { text: "Explain quantum computing" },
785
+ * provider: "openai",
786
+ * model: "gpt-4",
787
+ * temperature: 0.7,
788
+ * maxTokens: 500,
789
+ * enableAnalytics: true,
790
+ * enableEvaluation: true,
791
+ * context: { domain: "science", level: "intermediate" }
792
+ * });
793
+ *
794
+ * // Access analytics and evaluation data
795
+ * console.log(result.analytics?.usage);
796
+ * console.log(result.evaluation?.relevance);
797
+ * ```
798
+ *
799
+ * @throws {Error} When input text is missing or invalid
800
+ * @throws {Error} When all providers fail to generate content
801
+ * @throws {Error} When conversation memory operations fail (if enabled)
802
+ */
727
803
  async generate(optionsOrPrompt) {
728
804
  const originalPrompt = this._extractOriginalPrompt(optionsOrPrompt);
729
805
  // Convert string prompt to full options
@@ -1439,8 +1515,55 @@ export class NeuroLink {
1439
1515
  return stringStream();
1440
1516
  }
1441
1517
  /**
1442
- * PRIMARY METHOD: Stream content using AI (recommended for new code)
1443
- * Future-ready for multi-modal capabilities with current text focus
1518
+ * Stream AI-generated content in real-time using the best available provider.
1519
+ * This method provides real-time streaming of AI responses with full MCP tool integration.
1520
+ *
1521
+ * @param options - Stream configuration options
1522
+ * @param options.input - Input configuration object
1523
+ * @param options.input.text - The text prompt to send to the AI (required)
1524
+ * @param options.provider - AI provider to use ('auto', 'openai', 'anthropic', etc.)
1525
+ * @param options.model - Specific model to use (e.g., 'gpt-4', 'claude-3-opus')
1526
+ * @param options.temperature - Randomness in response (0.0 = deterministic, 2.0 = very random)
1527
+ * @param options.maxTokens - Maximum tokens in response
1528
+ * @param options.systemPrompt - System message to set AI behavior
1529
+ * @param options.disableTools - Whether to disable MCP tool usage
1530
+ * @param options.enableAnalytics - Whether to include usage analytics
1531
+ * @param options.enableEvaluation - Whether to include response quality evaluation
1532
+ * @param options.context - Additional context for the request
1533
+ * @param options.evaluationDomain - Domain for specialized evaluation
1534
+ *
1535
+ * @returns Promise resolving to StreamResult with an async iterable stream
1536
+ *
1537
+ * @example
1538
+ * ```typescript
1539
+ * // Basic streaming usage
1540
+ * const result = await neurolink.stream({
1541
+ * input: { text: "Tell me a story about space exploration" }
1542
+ * });
1543
+ *
1544
+ * // Consume the stream
1545
+ * for await (const chunk of result.stream) {
1546
+ * process.stdout.write(chunk.content);
1547
+ * }
1548
+ *
1549
+ * // Advanced streaming with options
1550
+ * const result = await neurolink.stream({
1551
+ * input: { text: "Explain machine learning" },
1552
+ * provider: "openai",
1553
+ * model: "gpt-4",
1554
+ * temperature: 0.7,
1555
+ * enableAnalytics: true,
1556
+ * context: { domain: "education", audience: "beginners" }
1557
+ * });
1558
+ *
1559
+ * // Access metadata and analytics
1560
+ * console.log(result.provider);
1561
+ * console.log(result.analytics?.usage);
1562
+ * ```
1563
+ *
1564
+ * @throws {Error} When input text is missing or invalid
1565
+ * @throws {Error} When all providers fail to generate content
1566
+ * @throws {Error} When conversation memory operations fail (if enabled)
1444
1567
  */
1445
1568
  async stream(options) {
1446
1569
  const startTime = Date.now();
@@ -1957,8 +2080,178 @@ export class NeuroLink {
1957
2080
  }
1958
2081
  }
1959
2082
  /**
1960
- * Get the EventEmitter to listen to NeuroLink events
1961
- * @returns EventEmitter instance
2083
+ * Get the EventEmitter instance to listen to NeuroLink events for real-time monitoring and debugging.
2084
+ * This method provides access to the internal event system that emits events during AI generation,
2085
+ * tool execution, streaming, and other operations for comprehensive observability.
2086
+ *
2087
+ * @returns EventEmitter instance that emits various NeuroLink operation events
2088
+ *
2089
+ * @example
2090
+ * ```typescript
2091
+ * // Basic event listening setup
2092
+ * const neurolink = new NeuroLink();
2093
+ * const emitter = neurolink.getEventEmitter();
2094
+ *
2095
+ * // Listen to generation events
2096
+ * emitter.on('generation:start', (event) => {
2097
+ * console.log(`Generation started with provider: ${event.provider}`);
2098
+ * console.log(`Started at: ${new Date(event.timestamp)}`);
2099
+ * });
2100
+ *
2101
+ * emitter.on('generation:end', (event) => {
2102
+ * console.log(`Generation completed in ${event.responseTime}ms`);
2103
+ * console.log(`Tools used: ${event.toolsUsed?.length || 0}`);
2104
+ * });
2105
+ *
2106
+ * // Listen to streaming events
2107
+ * emitter.on('stream:start', (event) => {
2108
+ * console.log(`Streaming started with provider: ${event.provider}`);
2109
+ * });
2110
+ *
2111
+ * emitter.on('stream:end', (event) => {
2112
+ * console.log(`Streaming completed in ${event.responseTime}ms`);
2113
+ * if (event.fallback) console.log('Used fallback streaming');
2114
+ * });
2115
+ *
2116
+ * // Listen to tool execution events
2117
+ * emitter.on('tool:start', (event) => {
2118
+ * console.log(`Tool execution started: ${event.toolName}`);
2119
+ * });
2120
+ *
2121
+ * emitter.on('tool:end', (event) => {
2122
+ * console.log(`Tool ${event.toolName} ${event.success ? 'succeeded' : 'failed'}`);
2123
+ * console.log(`Execution time: ${event.responseTime}ms`);
2124
+ * });
2125
+ *
2126
+ * // Listen to tool registration events
2127
+ * emitter.on('tools-register:start', (event) => {
2128
+ * console.log(`Registering tool: ${event.toolName}`);
2129
+ * });
2130
+ *
2131
+ * emitter.on('tools-register:end', (event) => {
2132
+ * console.log(`Tool registration ${event.success ? 'succeeded' : 'failed'}: ${event.toolName}`);
2133
+ * });
2134
+ *
2135
+ * // Listen to external MCP server events
2136
+ * emitter.on('externalMCP:serverConnected', (event) => {
2137
+ * console.log(`External MCP server connected: ${event.serverId}`);
2138
+ * console.log(`Tools available: ${event.toolCount || 0}`);
2139
+ * });
2140
+ *
2141
+ * emitter.on('externalMCP:serverDisconnected', (event) => {
2142
+ * console.log(`External MCP server disconnected: ${event.serverId}`);
2143
+ * console.log(`Reason: ${event.reason || 'Unknown'}`);
2144
+ * });
2145
+ *
2146
+ * emitter.on('externalMCP:toolDiscovered', (event) => {
2147
+ * console.log(`New tool discovered: ${event.toolName} from ${event.serverId}`);
2148
+ * });
2149
+ *
2150
+ * // Advanced usage with error handling
2151
+ * emitter.on('error', (error) => {
2152
+ * console.error('NeuroLink error:', error);
2153
+ * });
2154
+ *
2155
+ * // Clean up event listeners when done
2156
+ * function cleanup() {
2157
+ * emitter.removeAllListeners();
2158
+ * }
2159
+ *
2160
+ * process.on('SIGINT', cleanup);
2161
+ * process.on('SIGTERM', cleanup);
2162
+ * ```
2163
+ *
2164
+ * @example
2165
+ * ```typescript
2166
+ * // Advanced monitoring with metrics collection
2167
+ * const neurolink = new NeuroLink();
2168
+ * const emitter = neurolink.getEventEmitter();
2169
+ * const metrics = {
2170
+ * generations: 0,
2171
+ * totalResponseTime: 0,
2172
+ * toolExecutions: 0,
2173
+ * failures: 0
2174
+ * };
2175
+ *
2176
+ * // Collect performance metrics
2177
+ * emitter.on('generation:end', (event) => {
2178
+ * metrics.generations++;
2179
+ * metrics.totalResponseTime += event.responseTime;
2180
+ * metrics.toolExecutions += event.toolsUsed?.length || 0;
2181
+ * });
2182
+ *
2183
+ * emitter.on('tool:end', (event) => {
2184
+ * if (!event.success) {
2185
+ * metrics.failures++;
2186
+ * }
2187
+ * });
2188
+ *
2189
+ * // Log metrics every 10 seconds
2190
+ * setInterval(() => {
2191
+ * const avgResponseTime = metrics.generations > 0
2192
+ * ? metrics.totalResponseTime / metrics.generations
2193
+ * : 0;
2194
+ *
2195
+ * console.log('NeuroLink Metrics:', {
2196
+ * totalGenerations: metrics.generations,
2197
+ * averageResponseTime: `${avgResponseTime.toFixed(2)}ms`,
2198
+ * totalToolExecutions: metrics.toolExecutions,
2199
+ * failureRate: `${((metrics.failures / (metrics.toolExecutions || 1)) * 100).toFixed(2)}%`
2200
+ * });
2201
+ * }, 10000);
2202
+ * ```
2203
+ *
2204
+ * **Available Events:**
2205
+ *
2206
+ * **Generation Events:**
2207
+ * - `generation:start` - Fired when text generation begins
2208
+ * - `{ provider: string, timestamp: number }`
2209
+ * - `generation:end` - Fired when text generation completes
2210
+ * - `{ provider: string, responseTime: number, toolsUsed?: string[], timestamp: number }`
2211
+ *
2212
+ * **Streaming Events:**
2213
+ * - `stream:start` - Fired when streaming begins
2214
+ * - `{ provider: string, timestamp: number }`
2215
+ * - `stream:end` - Fired when streaming completes
2216
+ * - `{ provider: string, responseTime: number, fallback?: boolean }`
2217
+ *
2218
+ * **Tool Events:**
2219
+ * - `tool:start` - Fired when tool execution begins
2220
+ * - `{ toolName: string, timestamp: number }`
2221
+ * - `tool:end` - Fired when tool execution completes
2222
+ * - `{ toolName: string, responseTime: number, success: boolean, timestamp: number }`
2223
+ * - `tools-register:start` - Fired when tool registration begins
2224
+ * - `{ toolName: string, timestamp: number }`
2225
+ * - `tools-register:end` - Fired when tool registration completes
2226
+ * - `{ toolName: string, success: boolean, timestamp: number }`
2227
+ *
2228
+ * **External MCP Events:**
2229
+ * - `externalMCP:serverConnected` - Fired when external MCP server connects
2230
+ * - `{ serverId: string, toolCount?: number, timestamp: number }`
2231
+ * - `externalMCP:serverDisconnected` - Fired when external MCP server disconnects
2232
+ * - `{ serverId: string, reason?: string, timestamp: number }`
2233
+ * - `externalMCP:serverFailed` - Fired when external MCP server fails
2234
+ * - `{ serverId: string, error: string, timestamp: number }`
2235
+ * - `externalMCP:toolDiscovered` - Fired when external MCP tool is discovered
2236
+ * - `{ toolName: string, serverId: string, timestamp: number }`
2237
+ * - `externalMCP:toolRemoved` - Fired when external MCP tool is removed
2238
+ * - `{ toolName: string, serverId: string, timestamp: number }`
2239
+ * - `externalMCP:serverAdded` - Fired when external MCP server is added
2240
+ * - `{ serverId: string, config: MCPServerInfo, toolCount: number, timestamp: number }`
2241
+ * - `externalMCP:serverRemoved` - Fired when external MCP server is removed
2242
+ * - `{ serverId: string, timestamp: number }`
2243
+ *
2244
+ * **Error Events:**
2245
+ * - `error` - Fired when an error occurs
2246
+ * - `{ error: Error, context?: object }`
2247
+ *
2248
+ * @throws {Error} This method does not throw errors as it returns the internal EventEmitter
2249
+ *
2250
+ * @since 1.0.0
2251
+ * @see {@link https://nodejs.org/api/events.html} Node.js EventEmitter documentation
2252
+ * @see {@link NeuroLink.generate} for events related to text generation
2253
+ * @see {@link NeuroLink.stream} for events related to streaming
2254
+ * @see {@link NeuroLink.executeTool} for events related to tool execution
1962
2255
  */
1963
2256
  getEventEmitter() {
1964
2257
  return this.emitter;
@@ -105,10 +105,10 @@ const createVertexSettings = async () => {
105
105
  // Silent error handling for runtime credentials file creation
106
106
  }
107
107
  }
108
- // 🎯 OPTION 1: Check for principal account authentication (existing flow with debug logs)
108
+ // 🎯 OPTION 1: Check for principal account authentication (Accept any valid GOOGLE_APPLICATION_CREDENTIALS file (service account OR ADC))
109
109
  if (process.env.GOOGLE_APPLICATION_CREDENTIALS) {
110
110
  const credentialsPath = process.env.GOOGLE_APPLICATION_CREDENTIALS;
111
- // 🚨 CRITICAL FIX: Check if the credentials file actually exists
111
+ // Check if the credentials file exists
112
112
  let fileExists = false;
113
113
  try {
114
114
  fileExists = fs.existsSync(credentialsPath);
@@ -1115,8 +1115,9 @@ export class GoogleVertexProvider extends BaseProvider {
1115
1115
  method: authValidation.method,
1116
1116
  issues: authValidation.issues,
1117
1117
  solutions: [
1118
- "Option 1: Set GOOGLE_APPLICATION_CREDENTIALS to valid service account file",
1118
+ "Option 1: Set GOOGLE_APPLICATION_CREDENTIALS to valid service account OR ADC file",
1119
1119
  "Option 2: Set individual env vars: GOOGLE_AUTH_CLIENT_EMAIL, GOOGLE_AUTH_PRIVATE_KEY",
1120
+ "Option 3: Use gcloud auth application-default login for ADC",
1120
1121
  "Documentation: https://cloud.google.com/docs/authentication/provide-credentials-adc",
1121
1122
  ],
1122
1123
  });
@@ -1268,8 +1269,16 @@ export class GoogleVertexProvider extends BaseProvider {
1268
1269
  result.method = "service_account_file";
1269
1270
  return result;
1270
1271
  }
1272
+ else if (credentials.client_id &&
1273
+ credentials.client_secret &&
1274
+ credentials.refresh_token &&
1275
+ credentials.type !== "service_account") {
1276
+ result.isValid = true;
1277
+ result.method = "application_default_credentials";
1278
+ return result;
1279
+ }
1271
1280
  else {
1272
- result.issues.push("Service account file missing required fields");
1281
+ result.issues.push("Credentials file missing required fields (not service account or ADC format)");
1273
1282
  }
1274
1283
  }
1275
1284
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juspay/neurolink",
3
- "version": "7.28.1",
3
+ "version": "7.29.1",
4
4
  "description": "Universal AI Development Platform with working MCP integration, multi-provider support, and professional CLI. Built-in tools operational, 58+ external MCP servers discoverable. Connect to filesystem, GitHub, database operations, and more. Build, test, and deploy AI applications with 9 major providers: OpenAI, Anthropic, Google AI, AWS Bedrock, Azure, Hugging Face, Ollama, and Mistral AI.",
5
5
  "author": {
6
6
  "name": "Juspay Technologies",