@juspay/neurolink 7.26.1 → 7.28.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 (33) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/cli/commands/config.d.ts +3 -3
  3. package/dist/lib/mcp/toolDiscoveryService.js +1 -1
  4. package/dist/lib/neurolink.d.ts +7 -1
  5. package/dist/lib/neurolink.js +1130 -56
  6. package/dist/lib/providers/amazonBedrock.d.ts +2 -2
  7. package/dist/lib/providers/amazonBedrock.js +16 -7
  8. package/dist/lib/providers/googleVertex.d.ts +28 -3
  9. package/dist/lib/providers/googleVertex.js +1132 -84
  10. package/dist/lib/providers/litellm.d.ts +1 -1
  11. package/dist/lib/providers/litellm.js +7 -4
  12. package/dist/lib/providers/openaiCompatible.d.ts +1 -1
  13. package/dist/lib/providers/openaiCompatible.js +7 -4
  14. package/dist/lib/proxy/proxyFetch.js +124 -2
  15. package/dist/lib/utils/providerHealth.d.ts +57 -1
  16. package/dist/lib/utils/providerHealth.js +638 -33
  17. package/dist/lib/utils/transformationUtils.js +3 -3
  18. package/dist/mcp/toolDiscoveryService.js +1 -1
  19. package/dist/neurolink.d.ts +7 -1
  20. package/dist/neurolink.js +1130 -56
  21. package/dist/providers/amazonBedrock.d.ts +2 -2
  22. package/dist/providers/amazonBedrock.js +16 -7
  23. package/dist/providers/googleVertex.d.ts +28 -3
  24. package/dist/providers/googleVertex.js +1132 -84
  25. package/dist/providers/litellm.d.ts +1 -1
  26. package/dist/providers/litellm.js +7 -4
  27. package/dist/providers/openaiCompatible.d.ts +1 -1
  28. package/dist/providers/openaiCompatible.js +7 -4
  29. package/dist/proxy/proxyFetch.js +124 -2
  30. package/dist/utils/providerHealth.d.ts +57 -1
  31. package/dist/utils/providerHealth.js +638 -33
  32. package/dist/utils/transformationUtils.js +3 -3
  33. package/package.json +1 -1
@@ -66,46 +66,348 @@ export class NeuroLink {
66
66
  // Conversation memory support
67
67
  conversationMemory;
68
68
  constructor(config) {
69
- // SDK always disables manual MCP config for security
70
- ProviderRegistry.setOptions({
71
- enableManualMCP: false,
69
+ // 🚀 EXHAUSTIVE LOGGING POINT C001: CONSTRUCTOR ENTRY
70
+ const constructorStartTime = Date.now();
71
+ const constructorHrTimeStart = process.hrtime.bigint();
72
+ const constructorId = `neurolink-constructor-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
73
+ logger.debug(`[NeuroLink] 🏗️ LOG_POINT_C001_CONSTRUCTOR_START`, {
74
+ logPoint: "C001_CONSTRUCTOR_START",
75
+ constructorId,
76
+ timestamp: new Date().toISOString(),
77
+ constructorStartTime,
78
+ constructorHrTimeStart: constructorHrTimeStart.toString(),
79
+ // Configuration analysis
80
+ hasConfig: !!config,
81
+ configType: typeof config,
82
+ configKeys: config ? Object.keys(config) : [],
83
+ configSize: config ? JSON.stringify(config).length : 0,
84
+ // Conversation memory config analysis
85
+ hasConversationMemoryConfig: !!config?.conversationMemory,
86
+ conversationMemoryEnabled: config?.conversationMemory?.enabled || false,
87
+ conversationMemoryKeys: config?.conversationMemory
88
+ ? Object.keys(config.conversationMemory)
89
+ : [],
90
+ // Environment context
91
+ nodeVersion: process.version,
92
+ platform: process.platform,
93
+ arch: process.arch,
94
+ nodeEnv: process.env.NODE_ENV || "UNKNOWN",
95
+ // Memory and performance baseline
96
+ memoryUsage: process.memoryUsage(),
97
+ cpuUsage: process.cpuUsage(),
98
+ uptime: process.uptime(),
99
+ // Process PID and parent info
100
+ pid: process.pid,
101
+ ppid: process.ppid,
102
+ message: "NeuroLink constructor initialization starting with comprehensive environment analysis",
103
+ });
104
+ // 🚀 EXHAUSTIVE LOGGING POINT C002: PROVIDER REGISTRY SETUP
105
+ const registrySetupStartTime = process.hrtime.bigint();
106
+ logger.debug(`[NeuroLink] 🏗️ LOG_POINT_C002_PROVIDER_REGISTRY_SETUP_START`, {
107
+ logPoint: "C002_PROVIDER_REGISTRY_SETUP_START",
108
+ constructorId,
109
+ timestamp: new Date().toISOString(),
110
+ elapsedMs: Date.now() - constructorStartTime,
111
+ elapsedNs: (process.hrtime.bigint() - constructorHrTimeStart).toString(),
112
+ registrySetupStartTimeNs: registrySetupStartTime.toString(),
113
+ message: "Starting ProviderRegistry configuration for security",
72
114
  });
73
- // Initialize conversation memory if enabled
115
+ // SDK always disables manual MCP config for security
116
+ try {
117
+ ProviderRegistry.setOptions({
118
+ enableManualMCP: false,
119
+ });
120
+ const registrySetupEndTime = process.hrtime.bigint();
121
+ const registrySetupDurationNs = registrySetupEndTime - registrySetupStartTime;
122
+ logger.debug(`[NeuroLink] ✅ LOG_POINT_C003_PROVIDER_REGISTRY_SETUP_SUCCESS`, {
123
+ logPoint: "C003_PROVIDER_REGISTRY_SETUP_SUCCESS",
124
+ constructorId,
125
+ timestamp: new Date().toISOString(),
126
+ elapsedMs: Date.now() - constructorStartTime,
127
+ elapsedNs: (process.hrtime.bigint() - constructorHrTimeStart).toString(),
128
+ registrySetupDurationNs: registrySetupDurationNs.toString(),
129
+ registrySetupDurationMs: Number(registrySetupDurationNs) / 1000000,
130
+ enableManualMCP: false,
131
+ message: "ProviderRegistry configured successfully with security settings",
132
+ });
133
+ }
134
+ catch (error) {
135
+ const registrySetupErrorTime = process.hrtime.bigint();
136
+ const registrySetupDurationNs = registrySetupErrorTime - registrySetupStartTime;
137
+ logger.error(`[NeuroLink] ❌ LOG_POINT_C004_PROVIDER_REGISTRY_SETUP_ERROR`, {
138
+ logPoint: "C004_PROVIDER_REGISTRY_SETUP_ERROR",
139
+ constructorId,
140
+ timestamp: new Date().toISOString(),
141
+ elapsedMs: Date.now() - constructorStartTime,
142
+ elapsedNs: (process.hrtime.bigint() - constructorHrTimeStart).toString(),
143
+ registrySetupDurationNs: registrySetupDurationNs.toString(),
144
+ registrySetupDurationMs: Number(registrySetupDurationNs) / 1000000,
145
+ error: error instanceof Error ? error.message : String(error),
146
+ errorName: error instanceof Error ? error.name : "UnknownError",
147
+ errorStack: error instanceof Error ? error.stack : undefined,
148
+ message: "ProviderRegistry setup failed - critical initialization error",
149
+ });
150
+ throw error;
151
+ }
152
+ // 🚀 EXHAUSTIVE LOGGING POINT C005: CONVERSATION MEMORY INITIALIZATION
74
153
  if (config?.conversationMemory?.enabled) {
75
- const memoryConfig = applyConversationMemoryDefaults(config.conversationMemory);
76
- this.conversationMemory = new ConversationMemoryManager(memoryConfig);
77
- logger.info("NeuroLink initialized with conversation memory", {
78
- maxSessions: memoryConfig.maxSessions,
79
- maxTurnsPerSession: memoryConfig.maxTurnsPerSession,
154
+ const memoryInitStartTime = process.hrtime.bigint();
155
+ logger.debug(`[NeuroLink] 🧠 LOG_POINT_C005_MEMORY_INIT_START`, {
156
+ logPoint: "C005_MEMORY_INIT_START",
157
+ constructorId,
158
+ timestamp: new Date().toISOString(),
159
+ elapsedMs: Date.now() - constructorStartTime,
160
+ elapsedNs: (process.hrtime.bigint() - constructorHrTimeStart).toString(),
161
+ memoryInitStartTimeNs: memoryInitStartTime.toString(),
162
+ // Detailed memory config analysis
163
+ memoryConfig: {
164
+ enabled: config.conversationMemory.enabled,
165
+ maxSessions: config.conversationMemory.maxSessions,
166
+ maxTurnsPerSession: config.conversationMemory.maxTurnsPerSession,
167
+ keys: Object.keys(config.conversationMemory),
168
+ },
169
+ message: "Starting conversation memory initialization",
170
+ });
171
+ try {
172
+ const memoryConfig = applyConversationMemoryDefaults(config.conversationMemory);
173
+ const memoryManagerCreateStartTime = process.hrtime.bigint();
174
+ this.conversationMemory = new ConversationMemoryManager(memoryConfig);
175
+ const memoryManagerCreateEndTime = process.hrtime.bigint();
176
+ const memoryManagerCreateDurationNs = memoryManagerCreateEndTime - memoryManagerCreateStartTime;
177
+ const memoryInitEndTime = process.hrtime.bigint();
178
+ const memoryInitDurationNs = memoryInitEndTime - memoryInitStartTime;
179
+ logger.info(`[NeuroLink] ✅ LOG_POINT_C006_MEMORY_INIT_SUCCESS`, {
180
+ logPoint: "C006_MEMORY_INIT_SUCCESS",
181
+ constructorId,
182
+ timestamp: new Date().toISOString(),
183
+ elapsedMs: Date.now() - constructorStartTime,
184
+ elapsedNs: (process.hrtime.bigint() - constructorHrTimeStart).toString(),
185
+ memoryInitDurationNs: memoryInitDurationNs.toString(),
186
+ memoryInitDurationMs: Number(memoryInitDurationNs) / 1000000,
187
+ memoryManagerCreateDurationNs: memoryManagerCreateDurationNs.toString(),
188
+ memoryManagerCreateDurationMs: Number(memoryManagerCreateDurationNs) / 1000000,
189
+ // Final memory configuration
190
+ finalMemoryConfig: {
191
+ maxSessions: memoryConfig.maxSessions,
192
+ maxTurnsPerSession: memoryConfig.maxTurnsPerSession,
193
+ },
194
+ // Memory usage after initialization
195
+ memoryUsageAfterInit: process.memoryUsage(),
196
+ message: "NeuroLink initialized with conversation memory successfully",
197
+ });
198
+ }
199
+ catch (error) {
200
+ const memoryInitErrorTime = process.hrtime.bigint();
201
+ const memoryInitDurationNs = memoryInitErrorTime - memoryInitStartTime;
202
+ logger.error(`[NeuroLink] ❌ LOG_POINT_C007_MEMORY_INIT_ERROR`, {
203
+ logPoint: "C007_MEMORY_INIT_ERROR",
204
+ constructorId,
205
+ timestamp: new Date().toISOString(),
206
+ elapsedMs: Date.now() - constructorStartTime,
207
+ elapsedNs: (process.hrtime.bigint() - constructorHrTimeStart).toString(),
208
+ memoryInitDurationNs: memoryInitDurationNs.toString(),
209
+ memoryInitDurationMs: Number(memoryInitDurationNs) / 1000000,
210
+ error: error instanceof Error ? error.message : String(error),
211
+ errorName: error instanceof Error ? error.name : "UnknownError",
212
+ errorStack: error instanceof Error ? error.stack : undefined,
213
+ memoryConfig: config.conversationMemory,
214
+ message: "Conversation memory initialization failed",
215
+ });
216
+ throw error;
217
+ }
218
+ }
219
+ else {
220
+ logger.debug(`[NeuroLink] 🚫 LOG_POINT_C008_MEMORY_DISABLED`, {
221
+ logPoint: "C008_MEMORY_DISABLED",
222
+ constructorId,
223
+ timestamp: new Date().toISOString(),
224
+ elapsedMs: Date.now() - constructorStartTime,
225
+ elapsedNs: (process.hrtime.bigint() - constructorHrTimeStart).toString(),
226
+ hasConfig: !!config,
227
+ hasMemoryConfig: !!config?.conversationMemory,
228
+ memoryEnabled: config?.conversationMemory?.enabled || false,
229
+ reason: !config
230
+ ? "NO_CONFIG"
231
+ : !config.conversationMemory
232
+ ? "NO_MEMORY_CONFIG"
233
+ : !config.conversationMemory.enabled
234
+ ? "MEMORY_DISABLED"
235
+ : "UNKNOWN",
236
+ message: "Conversation memory not enabled - skipping initialization",
80
237
  });
81
238
  }
82
- // Initialize external server manager with main registry integration
83
- this.externalServerManager = new ExternalServerManager({
84
- maxServers: 20,
85
- defaultTimeout: 15000,
86
- enableAutoRestart: true,
87
- enablePerformanceMonitoring: true,
88
- }, {
89
- enableMainRegistryIntegration: true, // Enable integration with main toolRegistry
90
- });
91
- // Forward external server events
92
- this.externalServerManager.on("connected", (event) => {
93
- this.emitter.emit("externalMCP:serverConnected", event);
94
- });
95
- this.externalServerManager.on("disconnected", (event) => {
96
- this.emitter.emit("externalMCP:serverDisconnected", event);
239
+ // 🚀 EXHAUSTIVE LOGGING POINT C009: EXTERNAL SERVER MANAGER INITIALIZATION
240
+ const externalServerInitStartTime = process.hrtime.bigint();
241
+ logger.debug(`[NeuroLink] 🌐 LOG_POINT_C009_EXTERNAL_SERVER_INIT_START`, {
242
+ logPoint: "C009_EXTERNAL_SERVER_INIT_START",
243
+ constructorId,
244
+ timestamp: new Date().toISOString(),
245
+ elapsedMs: Date.now() - constructorStartTime,
246
+ elapsedNs: (process.hrtime.bigint() - constructorHrTimeStart).toString(),
247
+ externalServerInitStartTimeNs: externalServerInitStartTime.toString(),
248
+ serverManagerConfig: {
249
+ maxServers: 20,
250
+ defaultTimeout: 15000,
251
+ enableAutoRestart: true,
252
+ enablePerformanceMonitoring: true,
253
+ },
254
+ registryIntegrationConfig: {
255
+ enableMainRegistryIntegration: true,
256
+ },
257
+ message: "Starting external server manager initialization",
97
258
  });
98
- this.externalServerManager.on("failed", (event) => {
99
- this.emitter.emit("externalMCP:serverFailed", event);
100
- });
101
- this.externalServerManager.on("toolDiscovered", (event) => {
102
- this.emitter.emit("externalMCP:toolDiscovered", event);
103
- // Tools are already registered on server connection, no need to duplicate here
104
- });
105
- this.externalServerManager.on("toolRemoved", (event) => {
106
- this.emitter.emit("externalMCP:toolRemoved", event);
107
- // Unregister removed tools from main tool registry
108
- this.unregisterExternalMCPToolFromRegistry(event.toolName);
259
+ try {
260
+ // Initialize external server manager with main registry integration
261
+ this.externalServerManager = new ExternalServerManager({
262
+ maxServers: 20,
263
+ defaultTimeout: 15000,
264
+ enableAutoRestart: true,
265
+ enablePerformanceMonitoring: true,
266
+ }, {
267
+ enableMainRegistryIntegration: true, // Enable integration with main toolRegistry
268
+ });
269
+ const externalServerInitEndTime = process.hrtime.bigint();
270
+ const externalServerInitDurationNs = externalServerInitEndTime - externalServerInitStartTime;
271
+ logger.debug(`[NeuroLink] ✅ LOG_POINT_C010_EXTERNAL_SERVER_INIT_SUCCESS`, {
272
+ logPoint: "C010_EXTERNAL_SERVER_INIT_SUCCESS",
273
+ constructorId,
274
+ timestamp: new Date().toISOString(),
275
+ elapsedMs: Date.now() - constructorStartTime,
276
+ elapsedNs: (process.hrtime.bigint() - constructorHrTimeStart).toString(),
277
+ externalServerInitDurationNs: externalServerInitDurationNs.toString(),
278
+ externalServerInitDurationMs: Number(externalServerInitDurationNs) / 1000000,
279
+ hasExternalServerManager: !!this.externalServerManager,
280
+ message: "External server manager initialized successfully",
281
+ });
282
+ // 🚀 EXHAUSTIVE LOGGING POINT C011: EVENT HANDLER SETUP
283
+ const eventHandlerSetupStartTime = process.hrtime.bigint();
284
+ logger.debug(`[NeuroLink] 🔗 LOG_POINT_C011_EVENT_HANDLER_SETUP_START`, {
285
+ logPoint: "C011_EVENT_HANDLER_SETUP_START",
286
+ constructorId,
287
+ timestamp: new Date().toISOString(),
288
+ elapsedMs: Date.now() - constructorStartTime,
289
+ elapsedNs: (process.hrtime.bigint() - constructorHrTimeStart).toString(),
290
+ eventHandlerSetupStartTimeNs: eventHandlerSetupStartTime.toString(),
291
+ message: "Setting up external server event handlers",
292
+ });
293
+ // Forward external server events with detailed logging
294
+ this.externalServerManager.on("connected", (event) => {
295
+ logger.debug(`[NeuroLink] 🔗 EXTERNAL_SERVER_EVENT_CONNECTED`, {
296
+ constructorId,
297
+ eventType: "connected",
298
+ event,
299
+ timestamp: new Date().toISOString(),
300
+ message: "External MCP server connected event received",
301
+ });
302
+ this.emitter.emit("externalMCP:serverConnected", event);
303
+ });
304
+ this.externalServerManager.on("disconnected", (event) => {
305
+ logger.debug(`[NeuroLink] 🔗 EXTERNAL_SERVER_EVENT_DISCONNECTED`, {
306
+ constructorId,
307
+ eventType: "disconnected",
308
+ event,
309
+ timestamp: new Date().toISOString(),
310
+ message: "External MCP server disconnected event received",
311
+ });
312
+ this.emitter.emit("externalMCP:serverDisconnected", event);
313
+ });
314
+ this.externalServerManager.on("failed", (event) => {
315
+ logger.warn(`[NeuroLink] 🔗 EXTERNAL_SERVER_EVENT_FAILED`, {
316
+ constructorId,
317
+ eventType: "failed",
318
+ event,
319
+ timestamp: new Date().toISOString(),
320
+ message: "External MCP server failed event received",
321
+ });
322
+ this.emitter.emit("externalMCP:serverFailed", event);
323
+ });
324
+ this.externalServerManager.on("toolDiscovered", (event) => {
325
+ logger.debug(`[NeuroLink] 🔗 EXTERNAL_SERVER_EVENT_TOOL_DISCOVERED`, {
326
+ constructorId,
327
+ eventType: "toolDiscovered",
328
+ toolName: event.toolName,
329
+ serverId: event.serverId,
330
+ timestamp: new Date().toISOString(),
331
+ message: "External MCP tool discovered event received",
332
+ });
333
+ this.emitter.emit("externalMCP:toolDiscovered", event);
334
+ // Tools are already registered on server connection, no need to duplicate here
335
+ });
336
+ this.externalServerManager.on("toolRemoved", (event) => {
337
+ logger.debug(`[NeuroLink] 🔗 EXTERNAL_SERVER_EVENT_TOOL_REMOVED`, {
338
+ constructorId,
339
+ eventType: "toolRemoved",
340
+ toolName: event.toolName,
341
+ serverId: event.serverId,
342
+ timestamp: new Date().toISOString(),
343
+ message: "External MCP tool removed event received",
344
+ });
345
+ this.emitter.emit("externalMCP:toolRemoved", event);
346
+ // Unregister removed tools from main tool registry
347
+ this.unregisterExternalMCPToolFromRegistry(event.toolName);
348
+ });
349
+ const eventHandlerSetupEndTime = process.hrtime.bigint();
350
+ const eventHandlerSetupDurationNs = eventHandlerSetupEndTime - eventHandlerSetupStartTime;
351
+ logger.debug(`[NeuroLink] ✅ LOG_POINT_C012_EVENT_HANDLER_SETUP_SUCCESS`, {
352
+ logPoint: "C012_EVENT_HANDLER_SETUP_SUCCESS",
353
+ constructorId,
354
+ timestamp: new Date().toISOString(),
355
+ elapsedMs: Date.now() - constructorStartTime,
356
+ elapsedNs: (process.hrtime.bigint() - constructorHrTimeStart).toString(),
357
+ eventHandlerSetupDurationNs: eventHandlerSetupDurationNs.toString(),
358
+ eventHandlerSetupDurationMs: Number(eventHandlerSetupDurationNs) / 1000000,
359
+ eventHandlersCount: 5,
360
+ eventHandlerTypes: [
361
+ "connected",
362
+ "disconnected",
363
+ "failed",
364
+ "toolDiscovered",
365
+ "toolRemoved",
366
+ ],
367
+ message: "Event handlers set up successfully",
368
+ });
369
+ }
370
+ catch (error) {
371
+ const externalServerInitErrorTime = process.hrtime.bigint();
372
+ const externalServerInitDurationNs = externalServerInitErrorTime - externalServerInitStartTime;
373
+ logger.error(`[NeuroLink] ❌ LOG_POINT_C013_EXTERNAL_SERVER_INIT_ERROR`, {
374
+ logPoint: "C013_EXTERNAL_SERVER_INIT_ERROR",
375
+ constructorId,
376
+ timestamp: new Date().toISOString(),
377
+ elapsedMs: Date.now() - constructorStartTime,
378
+ elapsedNs: (process.hrtime.bigint() - constructorHrTimeStart).toString(),
379
+ externalServerInitDurationNs: externalServerInitDurationNs.toString(),
380
+ externalServerInitDurationMs: Number(externalServerInitDurationNs) / 1000000,
381
+ error: error instanceof Error ? error.message : String(error),
382
+ errorName: error instanceof Error ? error.name : "UnknownError",
383
+ errorStack: error instanceof Error ? error.stack : undefined,
384
+ message: "External server manager initialization failed",
385
+ });
386
+ throw error;
387
+ }
388
+ // 🚀 EXHAUSTIVE LOGGING POINT C014: CONSTRUCTOR COMPLETION
389
+ const constructorEndTime = process.hrtime.bigint();
390
+ const constructorDurationNs = constructorEndTime - constructorHrTimeStart;
391
+ logger.info(`[NeuroLink] 🏁 LOG_POINT_C014_CONSTRUCTOR_COMPLETE`, {
392
+ logPoint: "C014_CONSTRUCTOR_COMPLETE",
393
+ constructorId,
394
+ timestamp: new Date().toISOString(),
395
+ constructorDurationNs: constructorDurationNs.toString(),
396
+ constructorDurationMs: Number(constructorDurationNs) / 1000000,
397
+ totalElapsedMs: Date.now() - constructorStartTime,
398
+ // Final state summary
399
+ finalState: {
400
+ hasConversationMemory: !!this.conversationMemory,
401
+ hasExternalServerManager: !!this.externalServerManager,
402
+ hasEmitter: !!this.emitter,
403
+ mcpInitialized: this.mcpInitialized,
404
+ toolCircuitBreakersCount: this.toolCircuitBreakers.size,
405
+ toolExecutionMetricsCount: this.toolExecutionMetrics.size,
406
+ },
407
+ // Final memory usage
408
+ finalMemoryUsage: process.memoryUsage(),
409
+ finalCpuUsage: process.cpuUsage(),
410
+ message: "NeuroLink constructor completed successfully with all components initialized",
109
411
  });
110
412
  }
111
413
  /**
@@ -113,41 +415,244 @@ export class NeuroLink {
113
415
  * Uses isolated async context to prevent hanging
114
416
  */
115
417
  async initializeMCP() {
418
+ // 🚀 EXHAUSTIVE LOGGING POINT M001: MCP INITIALIZATION ENTRY CHECK
419
+ const mcpInitId = `mcp-init-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
420
+ const mcpInitStartTime = Date.now();
421
+ const mcpInitHrTimeStart = process.hrtime.bigint();
422
+ logger.debug(`[NeuroLink] 🔧 LOG_POINT_M001_MCP_INIT_ENTRY`, {
423
+ logPoint: "M001_MCP_INIT_ENTRY",
424
+ mcpInitId,
425
+ timestamp: new Date().toISOString(),
426
+ mcpInitStartTime,
427
+ mcpInitHrTimeStart: mcpInitHrTimeStart.toString(),
428
+ mcpInitialized: this.mcpInitialized,
429
+ hasExternalServerManager: !!this.externalServerManager,
430
+ memoryUsage: process.memoryUsage(),
431
+ cpuUsage: process.cpuUsage(),
432
+ message: "MCP initialization entry point - checking if already initialized",
433
+ });
116
434
  if (this.mcpInitialized) {
435
+ logger.debug(`[NeuroLink] ✅ LOG_POINT_M002_MCP_ALREADY_INITIALIZED`, {
436
+ logPoint: "M002_MCP_ALREADY_INITIALIZED",
437
+ mcpInitId,
438
+ timestamp: new Date().toISOString(),
439
+ elapsedMs: Date.now() - mcpInitStartTime,
440
+ elapsedNs: (process.hrtime.bigint() - mcpInitHrTimeStart).toString(),
441
+ mcpInitialized: this.mcpInitialized,
442
+ message: "MCP already initialized - skipping initialization",
443
+ });
117
444
  return;
118
445
  }
119
- // Track memory usage during MCP initialization
120
- const { MemoryManager } = await import("./utils/performance.js");
121
- const startMemory = MemoryManager.getMemoryUsageMB();
122
- const initStartTime = Date.now();
446
+ // 🚀 EXHAUSTIVE LOGGING POINT M003: PERFORMANCE MANAGER IMPORT
447
+ const performanceImportStartTime = process.hrtime.bigint();
448
+ logger.debug(`[NeuroLink] 📊 LOG_POINT_M003_PERFORMANCE_IMPORT_START`, {
449
+ logPoint: "M003_PERFORMANCE_IMPORT_START",
450
+ mcpInitId,
451
+ timestamp: new Date().toISOString(),
452
+ elapsedMs: Date.now() - mcpInitStartTime,
453
+ elapsedNs: (process.hrtime.bigint() - mcpInitHrTimeStart).toString(),
454
+ performanceImportStartTimeNs: performanceImportStartTime.toString(),
455
+ message: "Starting MemoryManager import for performance tracking",
456
+ });
457
+ let MemoryManager;
458
+ let startMemory;
123
459
  try {
460
+ // Track memory usage during MCP initialization
461
+ const moduleImport = await import("./utils/performance.js");
462
+ MemoryManager = moduleImport.MemoryManager;
463
+ startMemory = MemoryManager.getMemoryUsageMB();
464
+ const performanceImportEndTime = process.hrtime.bigint();
465
+ const performanceImportDurationNs = performanceImportEndTime - performanceImportStartTime;
466
+ logger.debug(`[NeuroLink] ✅ LOG_POINT_M004_PERFORMANCE_IMPORT_SUCCESS`, {
467
+ logPoint: "M004_PERFORMANCE_IMPORT_SUCCESS",
468
+ mcpInitId,
469
+ timestamp: new Date().toISOString(),
470
+ elapsedMs: Date.now() - mcpInitStartTime,
471
+ elapsedNs: (process.hrtime.bigint() - mcpInitHrTimeStart).toString(),
472
+ performanceImportDurationNs: performanceImportDurationNs.toString(),
473
+ performanceImportDurationMs: Number(performanceImportDurationNs) / 1000000,
474
+ hasMemoryManager: !!MemoryManager,
475
+ startMemory,
476
+ message: "MemoryManager imported successfully",
477
+ });
478
+ }
479
+ catch (error) {
480
+ const performanceImportErrorTime = process.hrtime.bigint();
481
+ const performanceImportDurationNs = performanceImportErrorTime - performanceImportStartTime;
482
+ logger.warn(`[NeuroLink] ⚠️ LOG_POINT_M005_PERFORMANCE_IMPORT_ERROR`, {
483
+ logPoint: "M005_PERFORMANCE_IMPORT_ERROR",
484
+ mcpInitId,
485
+ timestamp: new Date().toISOString(),
486
+ elapsedMs: Date.now() - mcpInitStartTime,
487
+ elapsedNs: (process.hrtime.bigint() - mcpInitHrTimeStart).toString(),
488
+ performanceImportDurationNs: performanceImportDurationNs.toString(),
489
+ performanceImportDurationMs: Number(performanceImportDurationNs) / 1000000,
490
+ error: error instanceof Error ? error.message : String(error),
491
+ errorName: error instanceof Error ? error.name : "UnknownError",
492
+ message: "MemoryManager import failed - continuing without performance tracking",
493
+ });
494
+ // Continue without performance tracking
495
+ startMemory = { heapUsed: 0, heapTotal: 0, rss: 0, external: 0 };
496
+ }
497
+ try {
498
+ // 🚀 EXHAUSTIVE LOGGING POINT M006: MAIN MCP INITIALIZATION START
499
+ logger.info(`[NeuroLink] 🚀 LOG_POINT_M006_MCP_MAIN_INIT_START`, {
500
+ logPoint: "M006_MCP_MAIN_INIT_START",
501
+ mcpInitId,
502
+ timestamp: new Date().toISOString(),
503
+ elapsedMs: Date.now() - mcpInitStartTime,
504
+ elapsedNs: (process.hrtime.bigint() - mcpInitHrTimeStart).toString(),
505
+ startMemory,
506
+ message: "Starting isolated MCP initialization process",
507
+ });
124
508
  mcpLogger.debug("[NeuroLink] Starting isolated MCP initialization...");
125
- // Initialize tool registry with timeout protection
509
+ // 🚀 EXHAUSTIVE LOGGING POINT M007: TOOL REGISTRY TIMEOUT SETUP
510
+ const toolRegistryStartTime = process.hrtime.bigint();
126
511
  const initTimeout = 3000; // 3 second timeout
512
+ logger.debug(`[NeuroLink] ⏱️ LOG_POINT_M007_TOOL_REGISTRY_TIMEOUT_SETUP`, {
513
+ logPoint: "M007_TOOL_REGISTRY_TIMEOUT_SETUP",
514
+ mcpInitId,
515
+ timestamp: new Date().toISOString(),
516
+ elapsedMs: Date.now() - mcpInitStartTime,
517
+ elapsedNs: (process.hrtime.bigint() - mcpInitHrTimeStart).toString(),
518
+ toolRegistryStartTimeNs: toolRegistryStartTime.toString(),
519
+ initTimeoutMs: initTimeout,
520
+ message: "Setting up tool registry initialization with timeout protection",
521
+ });
522
+ // Initialize tool registry with timeout protection
127
523
  await Promise.race([
128
524
  Promise.resolve(), // toolRegistry doesn't need explicit initialization
129
525
  new Promise((_, reject) => {
130
526
  setTimeout(() => reject(new Error("MCP initialization timeout")), initTimeout);
131
527
  }),
132
528
  ]);
529
+ const toolRegistryEndTime = process.hrtime.bigint();
530
+ const toolRegistryDurationNs = toolRegistryEndTime - toolRegistryStartTime;
531
+ logger.debug(`[NeuroLink] ✅ LOG_POINT_M008_TOOL_REGISTRY_SUCCESS`, {
532
+ logPoint: "M008_TOOL_REGISTRY_SUCCESS",
533
+ mcpInitId,
534
+ timestamp: new Date().toISOString(),
535
+ elapsedMs: Date.now() - mcpInitStartTime,
536
+ elapsedNs: (process.hrtime.bigint() - mcpInitHrTimeStart).toString(),
537
+ toolRegistryDurationNs: toolRegistryDurationNs.toString(),
538
+ toolRegistryDurationMs: Number(toolRegistryDurationNs) / 1000000,
539
+ message: "Tool registry initialization completed within timeout",
540
+ });
541
+ // 🚀 EXHAUSTIVE LOGGING POINT M009: PROVIDER REGISTRY START
542
+ const providerRegistryStartTime = process.hrtime.bigint();
543
+ logger.debug(`[NeuroLink] 🏭 LOG_POINT_M009_PROVIDER_REGISTRY_START`, {
544
+ logPoint: "M009_PROVIDER_REGISTRY_START",
545
+ mcpInitId,
546
+ timestamp: new Date().toISOString(),
547
+ elapsedMs: Date.now() - mcpInitStartTime,
548
+ elapsedNs: (process.hrtime.bigint() - mcpInitHrTimeStart).toString(),
549
+ providerRegistryStartTimeNs: providerRegistryStartTime.toString(),
550
+ message: "Starting provider registry registration with lazy loading",
551
+ });
133
552
  // Register all providers with lazy loading support
134
553
  await ProviderRegistry.registerAllProviders();
554
+ const providerRegistryEndTime = process.hrtime.bigint();
555
+ const providerRegistryDurationNs = providerRegistryEndTime - providerRegistryStartTime;
556
+ logger.debug(`[NeuroLink] ✅ LOG_POINT_M010_PROVIDER_REGISTRY_SUCCESS`, {
557
+ logPoint: "M010_PROVIDER_REGISTRY_SUCCESS",
558
+ mcpInitId,
559
+ timestamp: new Date().toISOString(),
560
+ elapsedMs: Date.now() - mcpInitStartTime,
561
+ elapsedNs: (process.hrtime.bigint() - mcpInitHrTimeStart).toString(),
562
+ providerRegistryDurationNs: providerRegistryDurationNs.toString(),
563
+ providerRegistryDurationMs: Number(providerRegistryDurationNs) / 1000000,
564
+ message: "Provider registry registration completed successfully",
565
+ });
566
+ // 🚀 EXHAUSTIVE LOGGING POINT M011: DIRECT TOOLS SERVER REGISTRATION
567
+ const directToolsStartTime = process.hrtime.bigint();
568
+ logger.debug(`[NeuroLink] 🛠️ LOG_POINT_M011_DIRECT_TOOLS_START`, {
569
+ logPoint: "M011_DIRECT_TOOLS_START",
570
+ mcpInitId,
571
+ timestamp: new Date().toISOString(),
572
+ elapsedMs: Date.now() - mcpInitStartTime,
573
+ elapsedNs: (process.hrtime.bigint() - mcpInitHrTimeStart).toString(),
574
+ directToolsStartTimeNs: directToolsStartTime.toString(),
575
+ serverId: "neurolink-direct",
576
+ message: "Starting direct tools server registration",
577
+ });
135
578
  // Register the direct tools server to make websearch and other tools available
136
579
  try {
137
580
  // Use the server ID string for registration instead of the server object
138
581
  await toolRegistry.registerServer("neurolink-direct", directToolsServer);
582
+ const directToolsSuccessTime = process.hrtime.bigint();
583
+ const directToolsDurationNs = directToolsSuccessTime - directToolsStartTime;
584
+ logger.debug(`[NeuroLink] ✅ LOG_POINT_M012_DIRECT_TOOLS_SUCCESS`, {
585
+ logPoint: "M012_DIRECT_TOOLS_SUCCESS",
586
+ mcpInitId,
587
+ timestamp: new Date().toISOString(),
588
+ elapsedMs: Date.now() - mcpInitStartTime,
589
+ elapsedNs: (process.hrtime.bigint() - mcpInitHrTimeStart).toString(),
590
+ directToolsDurationNs: directToolsDurationNs.toString(),
591
+ directToolsDurationMs: Number(directToolsDurationNs) / 1000000,
592
+ serverId: "neurolink-direct",
593
+ message: "Direct tools server registered successfully",
594
+ });
139
595
  mcpLogger.debug("[NeuroLink] Direct tools server registered successfully", {
140
596
  serverId: "neurolink-direct",
141
597
  });
142
598
  }
143
599
  catch (error) {
600
+ const directToolsErrorTime = process.hrtime.bigint();
601
+ const directToolsDurationNs = directToolsErrorTime - directToolsStartTime;
602
+ logger.warn(`[NeuroLink] ⚠️ LOG_POINT_M013_DIRECT_TOOLS_ERROR`, {
603
+ logPoint: "M013_DIRECT_TOOLS_ERROR",
604
+ mcpInitId,
605
+ timestamp: new Date().toISOString(),
606
+ elapsedMs: Date.now() - mcpInitStartTime,
607
+ elapsedNs: (process.hrtime.bigint() - mcpInitHrTimeStart).toString(),
608
+ directToolsDurationNs: directToolsDurationNs.toString(),
609
+ directToolsDurationMs: Number(directToolsDurationNs) / 1000000,
610
+ error: error instanceof Error ? error.message : String(error),
611
+ errorName: error instanceof Error ? error.name : "UnknownError",
612
+ errorStack: error instanceof Error ? error.stack : undefined,
613
+ serverId: "neurolink-direct",
614
+ message: "Direct tools server registration failed but continuing",
615
+ });
144
616
  mcpLogger.warn("[NeuroLink] Failed to register direct tools server", {
145
617
  error: error instanceof Error ? error.message : String(error),
146
618
  });
147
619
  }
620
+ // 🚀 EXHAUSTIVE LOGGING POINT M014: MCP CONFIG LOADING START
621
+ const mcpConfigStartTime = process.hrtime.bigint();
622
+ logger.debug(`[NeuroLink] 📄 LOG_POINT_M014_MCP_CONFIG_START`, {
623
+ logPoint: "M014_MCP_CONFIG_START",
624
+ mcpInitId,
625
+ timestamp: new Date().toISOString(),
626
+ elapsedMs: Date.now() - mcpInitStartTime,
627
+ elapsedNs: (process.hrtime.bigint() - mcpInitHrTimeStart).toString(),
628
+ mcpConfigStartTimeNs: mcpConfigStartTime.toString(),
629
+ hasExternalServerManager: !!this.externalServerManager,
630
+ message: "Starting MCP configuration loading from .mcp-config.json",
631
+ });
148
632
  // Load MCP configuration from .mcp-config.json using ExternalServerManager
149
633
  try {
150
634
  const configResult = await this.externalServerManager.loadMCPConfiguration();
635
+ const mcpConfigSuccessTime = process.hrtime.bigint();
636
+ const mcpConfigDurationNs = mcpConfigSuccessTime - mcpConfigStartTime;
637
+ logger.debug(`[NeuroLink] ✅ LOG_POINT_M015_MCP_CONFIG_SUCCESS`, {
638
+ logPoint: "M015_MCP_CONFIG_SUCCESS",
639
+ mcpInitId,
640
+ timestamp: new Date().toISOString(),
641
+ elapsedMs: Date.now() - mcpInitStartTime,
642
+ elapsedNs: (process.hrtime.bigint() - mcpInitHrTimeStart).toString(),
643
+ mcpConfigDurationNs: mcpConfigDurationNs.toString(),
644
+ mcpConfigDurationMs: Number(mcpConfigDurationNs) / 1000000,
645
+ serversLoaded: configResult.serversLoaded,
646
+ errorsCount: configResult.errors.length,
647
+ configResult: {
648
+ serversLoaded: configResult.serversLoaded,
649
+ errors: configResult.errors.map((err) => ({
650
+ message: err instanceof Error ? err.message : String(err),
651
+ name: err instanceof Error ? err.name : "UnknownError",
652
+ })),
653
+ },
654
+ message: "MCP configuration loaded successfully",
655
+ });
151
656
  mcpLogger.debug("[NeuroLink] MCP configuration loaded successfully", {
152
657
  serversLoaded: configResult.serversLoaded,
153
658
  errors: configResult.errors.length,
@@ -167,9 +672,11 @@ export class NeuroLink {
167
672
  }
168
673
  this.mcpInitialized = true;
169
674
  // Monitor memory usage and provide cleanup suggestions
170
- const endMemory = MemoryManager.getMemoryUsageMB();
675
+ const endMemory = MemoryManager
676
+ ? MemoryManager.getMemoryUsageMB()
677
+ : { heapUsed: 0, heapTotal: 0, rss: 0, external: 0 };
171
678
  const memoryDelta = endMemory.heapUsed - startMemory.heapUsed;
172
- const initTime = Date.now() - initStartTime;
679
+ const initTime = Date.now() - mcpInitStartTime;
173
680
  mcpLogger.debug("[NeuroLink] MCP initialization completed successfully", {
174
681
  initTime: `${initTime}ms`,
175
682
  memoryUsed: `${memoryDelta}MB`,
@@ -357,28 +864,208 @@ export class NeuroLink {
357
864
  * 5. Store conversation turn for future context
358
865
  */
359
866
  async generateTextInternal(options) {
360
- const startTime = Date.now();
867
+ // 🚀 EXHAUSTIVE LOGGING POINT G001: GENERATE TEXT INTERNAL ENTRY
868
+ const generateInternalId = `generate-internal-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
869
+ const generateInternalStartTime = Date.now();
870
+ const generateInternalHrTimeStart = process.hrtime.bigint();
361
871
  const functionTag = "NeuroLink.generateTextInternal";
872
+ logger.debug(`[NeuroLink] 🎯 LOG_POINT_G001_GENERATE_INTERNAL_START`, {
873
+ logPoint: "G001_GENERATE_INTERNAL_START",
874
+ generateInternalId,
875
+ timestamp: new Date().toISOString(),
876
+ generateInternalStartTime,
877
+ generateInternalHrTimeStart: generateInternalHrTimeStart.toString(),
878
+ // 📊 Input analysis
879
+ inputAnalysis: {
880
+ provider: options.provider || "auto",
881
+ providerType: typeof options.provider,
882
+ isAutoProvider: options.provider === "auto" || !options.provider,
883
+ model: options.model || "NOT_SET",
884
+ modelType: typeof options.model,
885
+ temperature: options.temperature,
886
+ temperatureType: typeof options.temperature,
887
+ maxTokens: options.maxTokens,
888
+ maxTokensType: typeof options.maxTokens,
889
+ promptLength: options.prompt?.length || 0,
890
+ promptPreview: options.prompt?.substring(0, 200) || "NO_PROMPT",
891
+ hasSystemPrompt: !!options.systemPrompt,
892
+ systemPromptLength: options.systemPrompt?.length || 0,
893
+ disableTools: options.disableTools || false,
894
+ enableAnalytics: options.enableAnalytics || false,
895
+ enableEvaluation: options.enableEvaluation || false,
896
+ hasContext: !!options.context,
897
+ contextKeys: options.context ? Object.keys(options.context) : [],
898
+ evaluationDomain: options.evaluationDomain || "NOT_SET",
899
+ toolUsageContext: options.toolUsageContext || "NOT_SET",
900
+ },
901
+ // 🧠 Memory and initialization state
902
+ instanceState: {
903
+ hasConversationMemory: !!this.conversationMemory,
904
+ conversationMemoryType: this.conversationMemory?.constructor?.name || "NOT_SET",
905
+ mcpInitialized: this.mcpInitialized,
906
+ hasProviderRegistry: !!AIProviderFactory,
907
+ providerRegistrySize: 0, // Not accessible as size property
908
+ hasToolRegistry: !!toolRegistry,
909
+ toolRegistrySize: 0, // Not accessible as size property
910
+ hasExternalServerManager: !!this.externalServerManager,
911
+ hasContextManager: !!this.contextManager,
912
+ },
913
+ // 🔧 Environment context
914
+ environmentContext: {
915
+ nodeVersion: process.version,
916
+ platform: process.platform,
917
+ arch: process.arch,
918
+ memoryUsage: process.memoryUsage(),
919
+ cpuUsage: process.cpuUsage(),
920
+ uptime: process.uptime(),
921
+ },
922
+ message: "Starting generateTextInternal with comprehensive input analysis",
923
+ });
362
924
  logger.debug(`[${functionTag}] Starting generation`, {
363
925
  provider: options.provider || "auto",
364
926
  promptLength: options.prompt?.length || 0,
365
927
  hasConversationMemory: !!this.conversationMemory,
366
928
  });
367
929
  try {
930
+ // 🚀 EXHAUSTIVE LOGGING POINT G002: CONVERSATION MEMORY INITIALIZATION
931
+ const conversationMemoryStartTime = process.hrtime.bigint();
932
+ logger.debug(`[NeuroLink] 🧠 LOG_POINT_G002_CONVERSATION_MEMORY_CHECK`, {
933
+ logPoint: "G002_CONVERSATION_MEMORY_CHECK",
934
+ generateInternalId,
935
+ timestamp: new Date().toISOString(),
936
+ elapsedMs: Date.now() - generateInternalStartTime,
937
+ elapsedNs: (process.hrtime.bigint() - generateInternalHrTimeStart).toString(),
938
+ conversationMemoryStartTimeNs: conversationMemoryStartTime.toString(),
939
+ hasConversationMemory: !!this.conversationMemory,
940
+ conversationMemoryEnabled: !!this.conversationMemory,
941
+ conversationMemoryType: this.conversationMemory?.constructor?.name || "NOT_AVAILABLE",
942
+ message: "Checking conversation memory initialization requirement",
943
+ });
368
944
  // Initialize conversation memory if enabled
369
945
  if (this.conversationMemory) {
946
+ logger.debug(`[NeuroLink] 🧠 LOG_POINT_G003_CONVERSATION_MEMORY_INIT_START`, {
947
+ logPoint: "G003_CONVERSATION_MEMORY_INIT_START",
948
+ generateInternalId,
949
+ timestamp: new Date().toISOString(),
950
+ elapsedMs: Date.now() - generateInternalStartTime,
951
+ elapsedNs: (process.hrtime.bigint() - generateInternalHrTimeStart).toString(),
952
+ message: "Starting conversation memory initialization",
953
+ });
370
954
  await this.conversationMemory.initialize();
955
+ const conversationMemoryEndTime = process.hrtime.bigint();
956
+ const conversationMemoryDurationNs = conversationMemoryEndTime - conversationMemoryStartTime;
957
+ logger.debug(`[NeuroLink] ✅ LOG_POINT_G004_CONVERSATION_MEMORY_INIT_SUCCESS`, {
958
+ logPoint: "G004_CONVERSATION_MEMORY_INIT_SUCCESS",
959
+ generateInternalId,
960
+ timestamp: new Date().toISOString(),
961
+ elapsedMs: Date.now() - generateInternalStartTime,
962
+ elapsedNs: (process.hrtime.bigint() - generateInternalHrTimeStart).toString(),
963
+ conversationMemoryDurationNs: conversationMemoryDurationNs.toString(),
964
+ conversationMemoryDurationMs: Number(conversationMemoryDurationNs) / 1000000,
965
+ message: "Conversation memory initialization completed successfully",
966
+ });
371
967
  }
968
+ // 🚀 EXHAUSTIVE LOGGING POINT G005: MCP GENERATION BRANCH DECISION
969
+ const mcpDecisionStartTime = process.hrtime.bigint();
970
+ logger.debug(`[NeuroLink] 🔧 LOG_POINT_G005_MCP_DECISION_CHECK`, {
971
+ logPoint: "G005_MCP_DECISION_CHECK",
972
+ generateInternalId,
973
+ timestamp: new Date().toISOString(),
974
+ elapsedMs: Date.now() - generateInternalStartTime,
975
+ elapsedNs: (process.hrtime.bigint() - generateInternalHrTimeStart).toString(),
976
+ mcpDecisionStartTimeNs: mcpDecisionStartTime.toString(),
977
+ // 🎯 MCP decision factors
978
+ mcpDecisionFactors: {
979
+ disableTools: options.disableTools || false,
980
+ toolsEnabled: !options.disableTools,
981
+ mcpInitialized: this.mcpInitialized,
982
+ hasExternalServerManager: !!this.externalServerManager,
983
+ hasToolRegistry: !!toolRegistry,
984
+ toolRegistrySize: 0, // Not accessible as size property
985
+ shouldTryMCP: !options.disableTools,
986
+ },
987
+ // 🔍 MCP readiness analysis
988
+ mcpReadinessAnalysis: {
989
+ mcpAvailable: !options.disableTools && this.mcpInitialized,
990
+ componentsReady: {
991
+ externalServerManager: !!this.externalServerManager,
992
+ toolRegistry: !!toolRegistry,
993
+ providerRegistry: !!AIProviderFactory,
994
+ },
995
+ },
996
+ message: "Analyzing MCP generation eligibility and readiness",
997
+ });
372
998
  // Try MCP-enhanced generation first (if not explicitly disabled)
373
999
  if (!options.disableTools) {
374
- let mcpAttempts = 0;
1000
+ // 🚀 EXHAUSTIVE LOGGING POINT G006: MCP RETRY LOOP INITIALIZATION
1001
+ const mcpAttempts = 0;
375
1002
  const maxMcpRetries = 2; // Allow retries for tool-related failures
376
- while (mcpAttempts <= maxMcpRetries) {
1003
+ const mcpRetryLoopStartTime = process.hrtime.bigint();
1004
+ logger.debug(`[NeuroLink] 🔄 LOG_POINT_G006_MCP_RETRY_LOOP_START`, {
1005
+ logPoint: "G006_MCP_RETRY_LOOP_START",
1006
+ generateInternalId,
1007
+ timestamp: new Date().toISOString(),
1008
+ elapsedMs: Date.now() - generateInternalStartTime,
1009
+ elapsedNs: (process.hrtime.bigint() - generateInternalHrTimeStart).toString(),
1010
+ mcpRetryLoopStartTimeNs: mcpRetryLoopStartTime.toString(),
1011
+ maxMcpRetries,
1012
+ totalPossibleAttempts: maxMcpRetries + 1,
1013
+ currentAttempt: mcpAttempts + 1,
1014
+ message: "Starting MCP generation retry loop with failure tolerance",
1015
+ });
1016
+ const maxAttempts = maxMcpRetries + 1;
1017
+ for (let attempt = 1; attempt <= maxAttempts; attempt++) {
377
1018
  try {
378
- logger.debug(`[${functionTag}] Attempting MCP generation (attempt ${mcpAttempts + 1}/${maxMcpRetries + 1})...`);
1019
+ // 🚀 EXHAUSTIVE LOGGING POINT G007: MCP GENERATION ATTEMPT
1020
+ const mcpAttemptStartTime = process.hrtime.bigint();
1021
+ logger.debug(`[NeuroLink] 🎯 LOG_POINT_G007_MCP_ATTEMPT_START`, {
1022
+ logPoint: "G007_MCP_ATTEMPT_START",
1023
+ generateInternalId,
1024
+ timestamp: new Date().toISOString(),
1025
+ elapsedMs: Date.now() - generateInternalStartTime,
1026
+ elapsedNs: (process.hrtime.bigint() - generateInternalHrTimeStart).toString(),
1027
+ mcpAttemptStartTimeNs: mcpAttemptStartTime.toString(),
1028
+ currentAttempt: attempt,
1029
+ maxAttempts,
1030
+ isFirstAttempt: attempt === 1,
1031
+ isLastAttempt: attempt === maxAttempts,
1032
+ attemptType: attempt === 1 ? "INITIAL" : "RETRY",
1033
+ message: `Attempting MCP generation (attempt ${attempt}/${maxAttempts})`,
1034
+ });
1035
+ logger.debug(`[${functionTag}] Attempting MCP generation (attempt ${attempt}/${maxAttempts})...`);
379
1036
  const mcpResult = await this.tryMCPGeneration(options);
1037
+ // 🚀 EXHAUSTIVE LOGGING POINT G008: MCP GENERATION ATTEMPT RESULT
1038
+ const mcpAttemptEndTime = process.hrtime.bigint();
1039
+ const mcpAttemptDurationNs = mcpAttemptEndTime - mcpAttemptStartTime;
1040
+ logger.debug(`[NeuroLink] 📊 LOG_POINT_G008_MCP_ATTEMPT_RESULT`, {
1041
+ logPoint: "G008_MCP_ATTEMPT_RESULT",
1042
+ generateInternalId,
1043
+ timestamp: new Date().toISOString(),
1044
+ elapsedMs: Date.now() - generateInternalStartTime,
1045
+ elapsedNs: (process.hrtime.bigint() - generateInternalHrTimeStart).toString(),
1046
+ mcpAttemptDurationNs: mcpAttemptDurationNs.toString(),
1047
+ mcpAttemptDurationMs: Number(mcpAttemptDurationNs) / 1000000,
1048
+ currentAttempt: attempt,
1049
+ // 🎯 Result analysis
1050
+ resultAnalysis: {
1051
+ hasResult: !!mcpResult,
1052
+ resultType: typeof mcpResult,
1053
+ hasContent: !!(mcpResult && mcpResult.content),
1054
+ contentLength: mcpResult?.content?.length || 0,
1055
+ contentPreview: mcpResult?.content?.substring(0, 200) || "NO_CONTENT",
1056
+ hasToolExecutions: !!(mcpResult &&
1057
+ mcpResult.toolExecutions &&
1058
+ mcpResult.toolExecutions.length > 0),
1059
+ toolExecutionsCount: mcpResult?.toolExecutions?.length || 0,
1060
+ toolsUsedCount: mcpResult?.toolsUsed?.length || 0,
1061
+ provider: mcpResult?.provider || "NOT_SET",
1062
+ responseTime: mcpResult?.responseTime || 0,
1063
+ enhancedWithTools: mcpResult?.enhancedWithTools || false,
1064
+ },
1065
+ message: `MCP generation attempt ${attempt} completed - analyzing result`,
1066
+ });
380
1067
  if (mcpResult && mcpResult.content) {
381
- logger.debug(`[${functionTag}] MCP generation successful on attempt ${mcpAttempts + 1}`, {
1068
+ logger.debug(`[${functionTag}] MCP generation successful on attempt ${attempt}`, {
382
1069
  contentLength: mcpResult.content.length,
383
1070
  toolsUsed: mcpResult.toolsUsed?.length || 0,
384
1071
  toolExecutions: mcpResult.toolExecutions?.length || 0,
@@ -388,7 +1075,7 @@ export class NeuroLink {
388
1075
  return mcpResult;
389
1076
  }
390
1077
  else {
391
- logger.debug(`[${functionTag}] MCP generation returned empty result on attempt ${mcpAttempts + 1}:`, {
1078
+ logger.debug(`[${functionTag}] MCP generation returned empty result on attempt ${attempt}:`, {
392
1079
  hasResult: !!mcpResult,
393
1080
  hasContent: !!(mcpResult && mcpResult.content),
394
1081
  contentLength: mcpResult?.content?.length || 0,
@@ -406,20 +1093,18 @@ export class NeuroLink {
406
1093
  }
407
1094
  }
408
1095
  catch (error) {
409
- mcpAttempts++;
410
- logger.debug(`[${functionTag}] MCP generation failed on attempt ${mcpAttempts}/${maxMcpRetries + 1}`, {
1096
+ logger.debug(`[${functionTag}] MCP generation failed on attempt ${attempt}/${maxAttempts}`, {
411
1097
  error: error instanceof Error ? error.message : String(error),
412
- willRetry: mcpAttempts <= maxMcpRetries,
1098
+ willRetry: attempt < maxAttempts,
413
1099
  });
414
1100
  // If this was the last attempt, break and fall back
415
- if (mcpAttempts > maxMcpRetries) {
1101
+ if (attempt >= maxAttempts) {
416
1102
  logger.debug(`[${functionTag}] All MCP attempts exhausted, falling back to direct generation`);
417
1103
  break;
418
1104
  }
419
1105
  // Small delay before retry to allow transient issues to resolve
420
1106
  await new Promise((resolve) => setTimeout(resolve, 500));
421
1107
  }
422
- mcpAttempts++;
423
1108
  }
424
1109
  }
425
1110
  // Fall back to direct provider generation
@@ -440,12 +1125,88 @@ export class NeuroLink {
440
1125
  * Try MCP-enhanced generation (no fallback recursion)
441
1126
  */
442
1127
  async tryMCPGeneration(options) {
1128
+ // 🚀 EXHAUSTIVE LOGGING POINT T001: TRY MCP GENERATION ENTRY
1129
+ const tryMCPId = `try-mcp-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
1130
+ const tryMCPStartTime = Date.now();
1131
+ const tryMCPHrTimeStart = process.hrtime.bigint();
443
1132
  const functionTag = "NeuroLink.tryMCPGeneration";
444
- const startTime = Date.now();
1133
+ logger.debug(`[NeuroLink] 🚀 LOG_POINT_T001_TRY_MCP_START`, {
1134
+ logPoint: "T001_TRY_MCP_START",
1135
+ tryMCPId,
1136
+ timestamp: new Date().toISOString(),
1137
+ tryMCPStartTime,
1138
+ tryMCPHrTimeStart: tryMCPHrTimeStart.toString(),
1139
+ // 📊 Input options analysis
1140
+ optionsAnalysis: {
1141
+ provider: options.provider || "auto",
1142
+ isAutoProvider: options.provider === "auto" || !options.provider,
1143
+ model: options.model || "NOT_SET",
1144
+ promptLength: options.prompt?.length || 0,
1145
+ promptPreview: options.prompt?.substring(0, 150) || "NO_PROMPT",
1146
+ hasSystemPrompt: !!options.systemPrompt,
1147
+ systemPromptLength: options.systemPrompt?.length || 0,
1148
+ disableTools: options.disableTools || false,
1149
+ enableAnalytics: options.enableAnalytics || false,
1150
+ temperature: options.temperature,
1151
+ maxTokens: options.maxTokens,
1152
+ },
1153
+ // 🔧 MCP state analysis
1154
+ mcpStateAnalysis: {
1155
+ mcpInitialized: this.mcpInitialized,
1156
+ hasExternalServerManager: !!this.externalServerManager,
1157
+ hasToolRegistry: !!toolRegistry,
1158
+ toolRegistrySize: 0, // Not accessible as size property
1159
+ hasProviderRegistry: !!AIProviderFactory,
1160
+ providerRegistrySize: 0, // Not accessible as size property
1161
+ },
1162
+ message: "Starting MCP-enhanced generation attempt with comprehensive analysis",
1163
+ });
445
1164
  try {
1165
+ // 🚀 EXHAUSTIVE LOGGING POINT T002: MCP INITIALIZATION CHECK
1166
+ const mcpInitCheckStartTime = process.hrtime.bigint();
1167
+ logger.debug(`[NeuroLink] 🔧 LOG_POINT_T002_MCP_INIT_CHECK`, {
1168
+ logPoint: "T002_MCP_INIT_CHECK",
1169
+ tryMCPId,
1170
+ timestamp: new Date().toISOString(),
1171
+ elapsedMs: Date.now() - tryMCPStartTime,
1172
+ elapsedNs: (process.hrtime.bigint() - tryMCPHrTimeStart).toString(),
1173
+ mcpInitCheckStartTimeNs: mcpInitCheckStartTime.toString(),
1174
+ mcpInitializedBefore: this.mcpInitialized,
1175
+ needsInitialization: !this.mcpInitialized,
1176
+ message: "Checking MCP initialization status before generation",
1177
+ });
446
1178
  // Initialize MCP if needed
447
1179
  await this.initializeMCP();
1180
+ const mcpInitCheckEndTime = process.hrtime.bigint();
1181
+ const mcpInitCheckDurationNs = mcpInitCheckEndTime - mcpInitCheckStartTime;
1182
+ logger.debug(`[NeuroLink] ✅ LOG_POINT_T003_MCP_INIT_CHECK_COMPLETE`, {
1183
+ logPoint: "T003_MCP_INIT_CHECK_COMPLETE",
1184
+ tryMCPId,
1185
+ timestamp: new Date().toISOString(),
1186
+ elapsedMs: Date.now() - tryMCPStartTime,
1187
+ elapsedNs: (process.hrtime.bigint() - tryMCPHrTimeStart).toString(),
1188
+ mcpInitCheckDurationNs: mcpInitCheckDurationNs.toString(),
1189
+ mcpInitCheckDurationMs: Number(mcpInitCheckDurationNs) / 1000000,
1190
+ mcpInitializedAfter: this.mcpInitialized,
1191
+ initializationSuccessful: this.mcpInitialized,
1192
+ message: "MCP initialization check completed",
1193
+ });
448
1194
  if (!this.mcpInitialized) {
1195
+ logger.warn(`[NeuroLink] ⚠️ LOG_POINT_T004_MCP_NOT_AVAILABLE`, {
1196
+ logPoint: "T004_MCP_NOT_AVAILABLE",
1197
+ tryMCPId,
1198
+ timestamp: new Date().toISOString(),
1199
+ elapsedMs: Date.now() - tryMCPStartTime,
1200
+ elapsedNs: (process.hrtime.bigint() - tryMCPHrTimeStart).toString(),
1201
+ mcpInitialized: this.mcpInitialized,
1202
+ mcpComponents: {
1203
+ hasExternalServerManager: !!this.externalServerManager,
1204
+ hasToolRegistry: !!toolRegistry,
1205
+ hasProviderRegistry: !!AIProviderFactory,
1206
+ },
1207
+ fallbackReason: "MCP_NOT_INITIALIZED",
1208
+ message: "MCP not available - returning null for fallback to direct generation",
1209
+ });
449
1210
  return null; // Skip MCP if not available
450
1211
  }
451
1212
  // Context creation removed - was never used
@@ -472,7 +1233,7 @@ export class NeuroLink {
472
1233
  systemPrompt: enhancedSystemPrompt,
473
1234
  conversationMessages, // Inject conversation history
474
1235
  });
475
- const responseTime = Date.now() - startTime;
1236
+ const responseTime = Date.now() - tryMCPStartTime;
476
1237
  // Enhanced result validation - consider tool executions as valid results
477
1238
  const hasContent = result && result.content && result.content.trim().length > 0;
478
1239
  const hasToolExecutions = result && result.toolExecutions && result.toolExecutions.length > 0;
@@ -683,13 +1444,246 @@ export class NeuroLink {
683
1444
  */
684
1445
  async stream(options) {
685
1446
  const startTime = Date.now();
1447
+ const hrTimeStart = process.hrtime.bigint();
686
1448
  const functionTag = "NeuroLink.stream";
687
- // Validate input
1449
+ const streamId = `neurolink-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
1450
+ const journeyStartTime = new Date().toISOString();
1451
+ // 🚀 EXHAUSTIVE LOGGING POINT 1: MAIN NEUROLINK STREAM ENTRY POINT
1452
+ logger.debug(`[NeuroLink] 🎯 LOG_POINT_001_STREAM_ENTRY_START`, {
1453
+ logPoint: "001_STREAM_ENTRY_START",
1454
+ streamId,
1455
+ timestamp: journeyStartTime,
1456
+ functionTag,
1457
+ startTime,
1458
+ hrTimeStart: hrTimeStart.toString(),
1459
+ nodeVersion: process.version,
1460
+ platform: process.platform,
1461
+ arch: process.arch,
1462
+ memoryUsage: process.memoryUsage(),
1463
+ cpuUsage: process.cpuUsage(),
1464
+ // Comprehensive input validation
1465
+ hasOptions: !!options,
1466
+ optionsType: typeof options,
1467
+ optionsKeys: options ? Object.keys(options) : [],
1468
+ optionsSize: options ? JSON.stringify(options).length : 0,
1469
+ // Deep input analysis
1470
+ hasInput: !!options?.input,
1471
+ inputType: typeof options?.input,
1472
+ inputKeys: options?.input ? Object.keys(options.input) : [],
1473
+ hasInputText: !!options?.input?.text,
1474
+ inputTextType: typeof options?.input?.text,
1475
+ inputTextLength: options?.input?.text?.length || 0,
1476
+ inputTextPreview: options?.input?.text?.substring(0, 200) || "NO_TEXT",
1477
+ inputTextHash: options?.input?.text
1478
+ ? "hash-" +
1479
+ options.input.text.length +
1480
+ "-" +
1481
+ Math.random().toString(36).substr(2, 8)
1482
+ : "NO_HASH",
1483
+ // Provider configuration analysis
1484
+ hasProvider: !!options?.provider,
1485
+ providerValue: options?.provider || "NOT_SET",
1486
+ isAutoProvider: options?.provider === "auto" || !options?.provider,
1487
+ // Model configuration analysis
1488
+ hasModel: !!options?.model,
1489
+ modelValue: options?.model || "NOT_SET",
1490
+ isDefaultModel: !options?.model,
1491
+ // Advanced configuration analysis
1492
+ hasSystemPrompt: !!options?.systemPrompt,
1493
+ systemPromptLength: options?.systemPrompt?.length || 0,
1494
+ systemPromptPreview: options?.systemPrompt?.substring(0, 100) || "NO_SYSTEM_PROMPT",
1495
+ hasTemperature: !!options?.temperature,
1496
+ temperatureValue: options?.temperature || "NOT_SET",
1497
+ isValidTemperature: typeof options?.temperature === "number" &&
1498
+ options.temperature >= 0 &&
1499
+ options.temperature <= 2,
1500
+ hasMaxTokens: !!options?.maxTokens,
1501
+ maxTokensValue: options?.maxTokens || "NOT_SET",
1502
+ isValidMaxTokens: typeof options?.maxTokens === "number" && options.maxTokens > 0,
1503
+ hasDisableTools: options?.disableTools !== undefined,
1504
+ disableToolsValue: options?.disableTools || false,
1505
+ hasContext: !!options?.context,
1506
+ contextKeys: options?.context ? Object.keys(options.context) : [],
1507
+ contextSize: options?.context
1508
+ ? JSON.stringify(options.context).length
1509
+ : 0,
1510
+ // Environment analysis
1511
+ nodeEnv: process.env.NODE_ENV || "UNKNOWN",
1512
+ hasProxyConfig: !!(process.env.HTTP_PROXY ||
1513
+ process.env.HTTPS_PROXY ||
1514
+ process.env.http_proxy ||
1515
+ process.env.https_proxy),
1516
+ httpProxy: process.env.HTTP_PROXY || process.env.http_proxy || "NOT_SET",
1517
+ httpsProxy: process.env.HTTPS_PROXY || process.env.https_proxy || "NOT_SET",
1518
+ hasGoogleCredentials: !!(process.env.GOOGLE_APPLICATION_CREDENTIALS ||
1519
+ process.env.GOOGLE_SERVICE_ACCOUNT_KEY),
1520
+ googleCredentialsPath: process.env.GOOGLE_APPLICATION_CREDENTIALS || "NOT_SET",
1521
+ message: "EXHAUSTIVE NeuroLink main stream method entry point with comprehensive environment analysis",
1522
+ });
1523
+ // 🚀 EXHAUSTIVE LOGGING POINT 2: MEMORY AND PERFORMANCE BASELINE
1524
+ const memoryBaseline = process.memoryUsage();
1525
+ const cpuBaseline = process.cpuUsage();
1526
+ logger.debug(`[NeuroLink] 🎯 LOG_POINT_002_PERFORMANCE_BASELINE`, {
1527
+ logPoint: "002_PERFORMANCE_BASELINE",
1528
+ streamId,
1529
+ timestamp: new Date().toISOString(),
1530
+ elapsedMs: Date.now() - startTime,
1531
+ elapsedNs: (process.hrtime.bigint() - hrTimeStart).toString(),
1532
+ memoryBaseline: {
1533
+ rss: memoryBaseline.rss,
1534
+ heapTotal: memoryBaseline.heapTotal,
1535
+ heapUsed: memoryBaseline.heapUsed,
1536
+ external: memoryBaseline.external,
1537
+ arrayBuffers: memoryBaseline.arrayBuffers,
1538
+ },
1539
+ cpuBaseline: {
1540
+ user: cpuBaseline.user,
1541
+ system: cpuBaseline.system,
1542
+ },
1543
+ gcStats: global.gc
1544
+ ? (() => {
1545
+ try {
1546
+ global.gc();
1547
+ return process.memoryUsage();
1548
+ }
1549
+ catch (e) {
1550
+ return null;
1551
+ }
1552
+ })()
1553
+ : null,
1554
+ message: "Performance baseline metrics captured for stream processing",
1555
+ });
1556
+ // 🚀 EXHAUSTIVE LOGGING POINT 3: INPUT VALIDATION START
1557
+ const validationStartTime = process.hrtime.bigint();
1558
+ logger.debug(`[NeuroLink] 🎯 LOG_POINT_003_VALIDATION_START`, {
1559
+ logPoint: "003_VALIDATION_START",
1560
+ streamId,
1561
+ timestamp: new Date().toISOString(),
1562
+ elapsedMs: Date.now() - startTime,
1563
+ elapsedNs: (process.hrtime.bigint() - hrTimeStart).toString(),
1564
+ validationStartTimeNs: validationStartTime.toString(),
1565
+ message: "Starting comprehensive input validation process",
1566
+ });
1567
+ // 🚀 EXHAUSTIVE LOGGING POINT 4: DETAILED VALIDATION CHECKS
1568
+ logger.debug(`[NeuroLink] 🎯 LOG_POINT_004_VALIDATION_CHECKS`, {
1569
+ logPoint: "004_VALIDATION_CHECKS",
1570
+ streamId,
1571
+ timestamp: new Date().toISOString(),
1572
+ elapsedMs: Date.now() - startTime,
1573
+ checks: {
1574
+ hasOptions: !!options,
1575
+ optionsIsObject: typeof options === "object" && options !== null,
1576
+ hasInput: !!(options && options.input),
1577
+ inputIsObject: !!(options &&
1578
+ options.input &&
1579
+ typeof options.input === "object"),
1580
+ hasInputText: !!(options && options.input && options.input.text),
1581
+ inputTextIsString: !!(options &&
1582
+ options.input &&
1583
+ typeof options.input.text === "string"),
1584
+ inputTextNotEmpty: !!(options &&
1585
+ options.input &&
1586
+ options.input.text &&
1587
+ options.input.text.trim() !== ""),
1588
+ inputTextLength: options?.input?.text?.length || 0,
1589
+ inputTextTrimmedLength: options?.input?.text?.trim()?.length || 0,
1590
+ },
1591
+ optionsStructure: options
1592
+ ? {
1593
+ keys: Object.keys(options),
1594
+ inputKeys: options.input ? Object.keys(options.input) : null,
1595
+ inputTextType: typeof options.input?.text,
1596
+ inputTextValue: options.input?.text?.substring(0, 50) +
1597
+ (options.input?.text?.length > 50 ? "..." : ""),
1598
+ inputTextCharCodes: options.input?.text
1599
+ ? Array.from(options.input.text.substring(0, 10)).map((c) => c.charCodeAt(0))
1600
+ : null,
1601
+ }
1602
+ : null,
1603
+ message: "Detailed validation checks performed",
1604
+ });
1605
+ // Validate input with comprehensive error reporting
688
1606
  if (!options?.input?.text ||
689
1607
  typeof options.input.text !== "string" ||
690
1608
  options.input.text.trim() === "") {
1609
+ const validationFailTime = process.hrtime.bigint();
1610
+ const validationDurationNs = validationFailTime - validationStartTime;
1611
+ logger.debug(`[NeuroLink] 💥 LOG_POINT_005_VALIDATION_FAILED`, {
1612
+ logPoint: "005_VALIDATION_FAILED",
1613
+ streamId,
1614
+ timestamp: new Date().toISOString(),
1615
+ elapsedMs: Date.now() - startTime,
1616
+ elapsedNs: (process.hrtime.bigint() - hrTimeStart).toString(),
1617
+ validationDurationNs: validationDurationNs.toString(),
1618
+ validationDurationMs: Number(validationDurationNs) / 1000000,
1619
+ validationError: "Stream options must include input.text as a non-empty string",
1620
+ // Detailed failure analysis
1621
+ failureReason: !options
1622
+ ? "NO_OPTIONS"
1623
+ : !options.input
1624
+ ? "NO_INPUT"
1625
+ : !options.input.text
1626
+ ? "NO_INPUT_TEXT"
1627
+ : typeof options.input.text !== "string"
1628
+ ? "INPUT_TEXT_NOT_STRING"
1629
+ : options.input.text.trim() === ""
1630
+ ? "INPUT_TEXT_EMPTY_OR_WHITESPACE"
1631
+ : "UNKNOWN",
1632
+ // Deep analysis for debugging
1633
+ hasOptions: !!options,
1634
+ optionsType: typeof options,
1635
+ hasInput: !!options?.input,
1636
+ inputType: typeof options?.input,
1637
+ hasInputText: !!options?.input?.text,
1638
+ inputTextType: typeof options?.input?.text,
1639
+ inputTextValue: options?.input?.text,
1640
+ inputTextLength: options?.input?.text?.length || 0,
1641
+ inputTextTrimmed: options?.input?.text?.trim(),
1642
+ inputTextTrimmedLength: options?.input?.text?.trim()?.length || 0,
1643
+ // Character analysis for whitespace debugging
1644
+ inputTextCharacters: options?.input?.text
1645
+ ? Array.from(options.input.text)
1646
+ .map((char, index) => ({
1647
+ index,
1648
+ char,
1649
+ charCode: char.charCodeAt(0),
1650
+ isWhitespace: /\s/.test(char),
1651
+ }))
1652
+ .slice(0, 20)
1653
+ : null,
1654
+ memoryAtFailure: process.memoryUsage(),
1655
+ cpuAtFailure: process.cpuUsage(),
1656
+ message: "EXHAUSTIVE validation failure analysis with character-level debugging",
1657
+ });
691
1658
  throw new Error("Stream options must include input.text as a non-empty string");
692
1659
  }
1660
+ // 🚀 EXHAUSTIVE LOGGING POINT 6: VALIDATION SUCCESS
1661
+ const validationSuccessTime = process.hrtime.bigint();
1662
+ const validationDurationNs = validationSuccessTime - validationStartTime;
1663
+ logger.debug(`[NeuroLink] ✅ LOG_POINT_006_VALIDATION_SUCCESS`, {
1664
+ logPoint: "006_VALIDATION_SUCCESS",
1665
+ streamId,
1666
+ timestamp: new Date().toISOString(),
1667
+ elapsedMs: Date.now() - startTime,
1668
+ elapsedNs: (process.hrtime.bigint() - hrTimeStart).toString(),
1669
+ validationDurationNs: validationDurationNs.toString(),
1670
+ validationDurationMs: Number(validationDurationNs) / 1000000,
1671
+ inputTextValid: true,
1672
+ inputTextLength: options.input.text.length,
1673
+ inputTextTrimmedLength: options.input.text.trim().length,
1674
+ inputTextPreview: options.input.text.substring(0, 100),
1675
+ inputTextHash: "hash-" +
1676
+ options.input.text.length +
1677
+ "-" +
1678
+ Math.random().toString(36).substr(2, 8),
1679
+ inputTextWordCount: options.input.text
1680
+ .split(/\s+/)
1681
+ .filter((word) => word.length > 0).length,
1682
+ inputTextLineCount: options.input.text.split(/\n/).length,
1683
+ memoryAfterValidation: process.memoryUsage(),
1684
+ cpuAfterValidation: process.cpuUsage(),
1685
+ message: "EXHAUSTIVE validation success - proceeding with stream processing",
1686
+ });
693
1687
  // Emit stream start event
694
1688
  this.emitter.emit("stream:start", {
695
1689
  provider: options.provider || "auto",
@@ -1396,12 +2390,61 @@ export class NeuroLink {
1396
2390
  * @returns Array of available tools with metadata
1397
2391
  */
1398
2392
  async getAllAvailableTools() {
2393
+ // 🚀 EXHAUSTIVE LOGGING POINT A001: GET ALL AVAILABLE TOOLS ENTRY
2394
+ const getAllToolsId = `get-all-tools-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
2395
+ const getAllToolsStartTime = Date.now();
2396
+ const getAllToolsHrTimeStart = process.hrtime.bigint();
2397
+ logger.debug(`[NeuroLink] 🛠️ LOG_POINT_A001_GET_ALL_TOOLS_START`, {
2398
+ logPoint: "A001_GET_ALL_TOOLS_START",
2399
+ getAllToolsId,
2400
+ timestamp: new Date().toISOString(),
2401
+ getAllToolsStartTime,
2402
+ getAllToolsHrTimeStart: getAllToolsHrTimeStart.toString(),
2403
+ // 🔧 Tool registry state
2404
+ toolRegistryState: {
2405
+ hasToolRegistry: !!toolRegistry,
2406
+ toolRegistrySize: 0, // Not accessible as size property
2407
+ toolRegistryType: toolRegistry?.constructor?.name || "NOT_SET",
2408
+ hasExternalServerManager: !!this.externalServerManager,
2409
+ externalServerManagerType: this.externalServerManager?.constructor?.name || "NOT_SET",
2410
+ },
2411
+ // 🌐 MCP state
2412
+ mcpState: {
2413
+ mcpInitialized: this.mcpInitialized,
2414
+ hasProviderRegistry: !!AIProviderFactory,
2415
+ providerRegistrySize: 0, // Not accessible as size property
2416
+ },
2417
+ message: "Starting comprehensive tool discovery across all sources",
2418
+ });
1399
2419
  // Track memory usage for tool listing operations
1400
2420
  const { MemoryManager } = await import("./utils/performance.js");
1401
2421
  const startMemory = MemoryManager.getMemoryUsageMB();
2422
+ logger.debug(`[NeuroLink] 📊 LOG_POINT_A002_MEMORY_BASELINE`, {
2423
+ logPoint: "A002_MEMORY_BASELINE",
2424
+ getAllToolsId,
2425
+ timestamp: new Date().toISOString(),
2426
+ elapsedMs: Date.now() - getAllToolsStartTime,
2427
+ elapsedNs: (process.hrtime.bigint() - getAllToolsHrTimeStart).toString(),
2428
+ memoryBaseline: startMemory,
2429
+ heapUsed: startMemory.heapUsed,
2430
+ heapTotal: startMemory.heapTotal,
2431
+ external: startMemory.external,
2432
+ message: "Established memory baseline before tool enumeration",
2433
+ });
1402
2434
  try {
1403
2435
  // Optimized: Collect all tools with minimal object creation
1404
2436
  const allTools = new Map();
2437
+ // 🚀 EXHAUSTIVE LOGGING POINT A003: MCP TOOLS COLLECTION START
2438
+ const mcpToolsStartTime = process.hrtime.bigint();
2439
+ logger.debug(`[NeuroLink] 🔧 LOG_POINT_A003_MCP_TOOLS_START`, {
2440
+ logPoint: "A003_MCP_TOOLS_START",
2441
+ getAllToolsId,
2442
+ timestamp: new Date().toISOString(),
2443
+ elapsedMs: Date.now() - getAllToolsStartTime,
2444
+ elapsedNs: (process.hrtime.bigint() - getAllToolsHrTimeStart).toString(),
2445
+ mcpToolsStartTimeNs: mcpToolsStartTime.toString(),
2446
+ message: "Starting MCP server tools collection",
2447
+ });
1405
2448
  // 1. Add MCP server tools (built-in direct tools)
1406
2449
  const mcpToolsRaw = await toolRegistry.listTools();
1407
2450
  for (const tool of mcpToolsRaw) {
@@ -2002,6 +3045,37 @@ export class NeuroLink {
2002
3045
  }
2003
3046
  return await this.conversationMemory.getStats();
2004
3047
  }
3048
+ /**
3049
+ * Get complete conversation history for a specific session (public API)
3050
+ * @param sessionId - The session ID to retrieve history for
3051
+ * @returns Array of ChatMessage objects in chronological order, or empty array if session doesn't exist
3052
+ */
3053
+ async getConversationHistory(sessionId) {
3054
+ if (!this.conversationMemory) {
3055
+ throw new Error("Conversation memory is not enabled");
3056
+ }
3057
+ if (!sessionId || typeof sessionId !== "string") {
3058
+ throw new Error("Session ID must be a non-empty string");
3059
+ }
3060
+ try {
3061
+ // Use the existing buildContextMessages method to get the complete history
3062
+ const messages = this.conversationMemory.buildContextMessages(sessionId);
3063
+ logger.debug("Retrieved conversation history", {
3064
+ sessionId,
3065
+ messageCount: messages.length,
3066
+ turnCount: messages.length / 2, // Each turn = user + assistant message
3067
+ });
3068
+ return messages;
3069
+ }
3070
+ catch (error) {
3071
+ logger.error("Failed to retrieve conversation history", {
3072
+ sessionId,
3073
+ error: error instanceof Error ? error.message : String(error),
3074
+ });
3075
+ // Return empty array for graceful handling of missing sessions
3076
+ return [];
3077
+ }
3078
+ }
2005
3079
  /**
2006
3080
  * Clear conversation history for a specific session (public API)
2007
3081
  */