@juspay/neurolink 7.54.0 → 8.0.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.
package/dist/neurolink.js CHANGED
@@ -44,7 +44,7 @@ import { directToolsServer } from "./mcp/servers/agent/directToolsServer.js";
44
44
  // Import orchestration components
45
45
  import { ModelRouter } from "./utils/modelRouter.js";
46
46
  import { BinaryTaskClassifier } from "./utils/taskClassifier.js";
47
- import { initializeOpenTelemetry, shutdownOpenTelemetry, flushOpenTelemetry, getLangfuseHealthStatus, } from "./services/server/ai/observability/instrumentation.js";
47
+ import { initializeOpenTelemetry, shutdownOpenTelemetry, flushOpenTelemetry, getLangfuseHealthStatus, setLangfuseContext, } from "./services/server/ai/observability/instrumentation.js";
48
48
  export class NeuroLink {
49
49
  mcpInitialized = false;
50
50
  emitter = new EventEmitter();
@@ -94,6 +94,40 @@ export class NeuroLink {
94
94
  // Mem0 memory instance and config for conversation context
95
95
  mem0Instance;
96
96
  mem0Config;
97
+ /**
98
+ * Extract and set Langfuse context from options with proper async scoping
99
+ */
100
+ async setLangfuseContextFromOptions(options, callback) {
101
+ if (options.context &&
102
+ typeof options.context === "object" &&
103
+ options.context !== null) {
104
+ try {
105
+ const ctx = options.context;
106
+ if (ctx.userId || ctx.sessionId) {
107
+ return await new Promise((resolve, reject) => {
108
+ setLangfuseContext({
109
+ userId: typeof ctx.userId === "string" ? ctx.userId : null,
110
+ sessionId: typeof ctx.sessionId === "string" ? ctx.sessionId : null,
111
+ }, async () => {
112
+ try {
113
+ const result = await callback();
114
+ resolve(result);
115
+ }
116
+ catch (error) {
117
+ reject(error);
118
+ }
119
+ });
120
+ });
121
+ }
122
+ }
123
+ catch (error) {
124
+ logger.warn("Failed to set Langfuse context from options", {
125
+ error: error instanceof Error ? error.message : String(error),
126
+ });
127
+ }
128
+ }
129
+ return await callback();
130
+ }
97
131
  /**
98
132
  * Simple sync config setup for mem0
99
133
  */
@@ -518,7 +552,7 @@ export class NeuroLink {
518
552
  langfuseInitStartTimeNs: langfuseInitStartTime.toString(),
519
553
  message: "Starting Langfuse observability initialization",
520
554
  });
521
- // Initialize OpenTelemetry FIRST (required for Langfuse v4)
555
+ // Initialize OpenTelemetry (sets defaults from config)
522
556
  initializeOpenTelemetry(langfuseConfig);
523
557
  const healthStatus = getLangfuseHealthStatus();
524
558
  const langfuseInitDurationNs = process.hrtime.bigint() - langfuseInitStartTime;
@@ -1146,198 +1180,201 @@ export class NeuroLink {
1146
1180
  if (!options.input?.text || typeof options.input.text !== "string") {
1147
1181
  throw new Error("Input text is required and must be a non-empty string");
1148
1182
  }
1149
- if (this.conversationMemoryConfig?.conversationMemory?.mem0Enabled &&
1150
- options.context?.userId) {
1151
- try {
1152
- const mem0 = await this.ensureMem0Ready();
1153
- if (!mem0) {
1154
- logger.debug("Mem0 not available, continuing without memory retrieval");
1183
+ // Set session and user IDs from context for Langfuse spans and execute with proper async scoping
1184
+ return await this.setLangfuseContextFromOptions(options, async () => {
1185
+ if (this.conversationMemoryConfig?.conversationMemory?.mem0Enabled &&
1186
+ options.context?.userId) {
1187
+ try {
1188
+ const mem0 = await this.ensureMem0Ready();
1189
+ if (!mem0) {
1190
+ logger.debug("Mem0 not available, continuing without memory retrieval");
1191
+ }
1192
+ else {
1193
+ const memories = await mem0.search(options.input.text, {
1194
+ userId: options.context.userId,
1195
+ limit: 5,
1196
+ });
1197
+ if (memories?.results?.length > 0) {
1198
+ // Enhance the input with memory context
1199
+ const memoryContext = memories.results
1200
+ .map((m) => m.memory)
1201
+ .join("\n");
1202
+ options.input.text = this.formatMemoryContext(memoryContext, options.input.text);
1203
+ }
1204
+ }
1155
1205
  }
1156
- else {
1157
- const memories = await mem0.search(options.input.text, {
1158
- userId: options.context.userId,
1159
- limit: 5,
1206
+ catch (error) {
1207
+ logger.warn("Mem0 memory retrieval failed:", error);
1208
+ }
1209
+ }
1210
+ const startTime = Date.now();
1211
+ // Apply orchestration if enabled and no specific provider/model requested
1212
+ if (this.enableOrchestration && !options.provider && !options.model) {
1213
+ try {
1214
+ const orchestratedOptions = await this.applyOrchestration(options);
1215
+ logger.debug("Orchestration applied", {
1216
+ originalProvider: options.provider || "auto",
1217
+ orchestratedProvider: orchestratedOptions.provider,
1218
+ orchestratedModel: orchestratedOptions.model,
1219
+ prompt: options.input.text.substring(0, 100),
1160
1220
  });
1161
- if (memories?.results?.length > 0) {
1162
- // Enhance the input with memory context
1163
- const memoryContext = memories.results
1164
- .map((m) => m.memory)
1165
- .join("\n");
1166
- options.input.text = this.formatMemoryContext(memoryContext, options.input.text);
1167
- }
1221
+ // Use orchestrated options
1222
+ Object.assign(options, orchestratedOptions);
1223
+ }
1224
+ catch (error) {
1225
+ logger.warn("Orchestration failed, continuing with original options", {
1226
+ error: error instanceof Error ? error.message : String(error),
1227
+ originalProvider: options.provider || "auto",
1228
+ });
1229
+ // Continue with original options if orchestration fails
1168
1230
  }
1169
1231
  }
1170
- catch (error) {
1171
- logger.warn("Mem0 memory retrieval failed:", error);
1232
+ // Emit generation start event (NeuroLink format - keep existing)
1233
+ this.emitter.emit("generation:start", {
1234
+ provider: options.provider || "auto",
1235
+ timestamp: startTime,
1236
+ });
1237
+ // ADD: Bedrock-compatible response:start event
1238
+ this.emitter.emit("response:start");
1239
+ // ADD: Bedrock-compatible message event
1240
+ this.emitter.emit("message", `Starting ${options.provider || "auto"} text generation...`);
1241
+ // Process factory configuration
1242
+ const factoryResult = processFactoryOptions(options);
1243
+ // Validate factory configuration if present
1244
+ if (factoryResult.hasFactoryConfig && options.factoryConfig) {
1245
+ const validation = validateFactoryConfig(options.factoryConfig);
1246
+ if (!validation.isValid) {
1247
+ logger.warn("Invalid factory configuration detected", {
1248
+ errors: validation.errors,
1249
+ });
1250
+ // Continue with warning rather than throwing - graceful degradation
1251
+ }
1172
1252
  }
1173
- }
1174
- const startTime = Date.now();
1175
- // Apply orchestration if enabled and no specific provider/model requested
1176
- if (this.enableOrchestration && !options.provider && !options.model) {
1177
- try {
1178
- const orchestratedOptions = await this.applyOrchestration(options);
1179
- logger.debug("Orchestration applied", {
1180
- originalProvider: options.provider || "auto",
1181
- orchestratedProvider: orchestratedOptions.provider,
1182
- orchestratedModel: orchestratedOptions.model,
1183
- prompt: options.input.text.substring(0, 100),
1184
- });
1185
- // Use orchestrated options
1186
- Object.assign(options, orchestratedOptions);
1253
+ // 🔧 CRITICAL FIX: Convert to TextGenerationOptions while preserving the input object for multimodal support
1254
+ const baseOptions = {
1255
+ prompt: options.input.text,
1256
+ provider: options.provider,
1257
+ model: options.model,
1258
+ temperature: options.temperature,
1259
+ maxTokens: options.maxTokens,
1260
+ systemPrompt: options.systemPrompt,
1261
+ schema: options.schema,
1262
+ output: options.output,
1263
+ disableTools: options.disableTools,
1264
+ enableAnalytics: options.enableAnalytics,
1265
+ enableEvaluation: options.enableEvaluation,
1266
+ context: options.context,
1267
+ evaluationDomain: options.evaluationDomain,
1268
+ toolUsageContext: options.toolUsageContext,
1269
+ input: options.input, // This includes text, images, and content arrays
1270
+ region: options.region,
1271
+ };
1272
+ // Apply factory enhancement using centralized utilities
1273
+ const textOptions = enhanceTextGenerationOptions(baseOptions, factoryResult);
1274
+ // Pass conversation memory config if available
1275
+ if (this.conversationMemory) {
1276
+ textOptions.conversationMemoryConfig = this.conversationMemory.config;
1277
+ // Include original prompt for context summarization
1278
+ textOptions.originalPrompt = originalPrompt;
1187
1279
  }
1188
- catch (error) {
1189
- logger.warn("Orchestration failed, continuing with original options", {
1190
- error: error instanceof Error ? error.message : String(error),
1191
- originalProvider: options.provider || "auto",
1280
+ // Detect and execute domain-specific tools
1281
+ const { toolResults, enhancedPrompt } = await this.detectAndExecuteTools(textOptions.prompt || options.input.text, factoryResult.domainType);
1282
+ // Update prompt with tool results if available
1283
+ if (enhancedPrompt !== textOptions.prompt) {
1284
+ textOptions.prompt = enhancedPrompt;
1285
+ logger.debug("Enhanced prompt with tool results", {
1286
+ originalLength: options.input.text.length,
1287
+ enhancedLength: enhancedPrompt.length,
1288
+ toolResults: toolResults.length,
1192
1289
  });
1193
- // Continue with original options if orchestration fails
1194
1290
  }
1195
- }
1196
- // Emit generation start event (NeuroLink format - keep existing)
1197
- this.emitter.emit("generation:start", {
1198
- provider: options.provider || "auto",
1199
- timestamp: startTime,
1200
- });
1201
- // ADD: Bedrock-compatible response:start event
1202
- this.emitter.emit("response:start");
1203
- // ADD: Bedrock-compatible message event
1204
- this.emitter.emit("message", `Starting ${options.provider || "auto"} text generation...`);
1205
- // Process factory configuration
1206
- const factoryResult = processFactoryOptions(options);
1207
- // Validate factory configuration if present
1208
- if (factoryResult.hasFactoryConfig && options.factoryConfig) {
1209
- const validation = validateFactoryConfig(options.factoryConfig);
1210
- if (!validation.isValid) {
1211
- logger.warn("Invalid factory configuration detected", {
1212
- errors: validation.errors,
1291
+ // Use redesigned generation logic
1292
+ const textResult = await this.generateTextInternal(textOptions);
1293
+ // Emit generation completion event (NeuroLink format - enhanced with content)
1294
+ this.emitter.emit("generation:end", {
1295
+ provider: textResult.provider,
1296
+ responseTime: Date.now() - startTime,
1297
+ toolsUsed: textResult.toolsUsed,
1298
+ timestamp: Date.now(),
1299
+ result: textResult, // Enhanced: include full result
1300
+ });
1301
+ // ADD: Bedrock-compatible response:end event with content
1302
+ this.emitter.emit("response:end", textResult.content || "");
1303
+ // ADD: Bedrock-compatible message event
1304
+ this.emitter.emit("message", `Generation completed in ${Date.now() - startTime}ms`);
1305
+ // Convert back to GenerateResult
1306
+ const generateResult = {
1307
+ content: textResult.content,
1308
+ provider: textResult.provider,
1309
+ model: textResult.model,
1310
+ usage: textResult.usage
1311
+ ? {
1312
+ input: textResult.usage.input || 0,
1313
+ output: textResult.usage.output || 0,
1314
+ total: textResult.usage.total || 0,
1315
+ }
1316
+ : undefined,
1317
+ responseTime: textResult.responseTime,
1318
+ toolsUsed: textResult.toolsUsed,
1319
+ toolExecutions: transformToolExecutions(textResult.toolExecutions),
1320
+ enhancedWithTools: textResult.enhancedWithTools,
1321
+ availableTools: transformAvailableTools(textResult.availableTools),
1322
+ analytics: textResult.analytics,
1323
+ evaluation: textResult.evaluation
1324
+ ? {
1325
+ ...textResult.evaluation,
1326
+ isOffTopic: textResult.evaluation
1327
+ .isOffTopic ?? false,
1328
+ alertSeverity: textResult.evaluation
1329
+ .alertSeverity ??
1330
+ "none",
1331
+ reasoning: textResult.evaluation
1332
+ .reasoning ?? "No evaluation provided",
1333
+ evaluationModel: textResult.evaluation
1334
+ .evaluationModel ?? "unknown",
1335
+ evaluationTime: textResult.evaluation
1336
+ .evaluationTime ?? Date.now(),
1337
+ // Include evaluationDomain from original options
1338
+ evaluationDomain: textResult.evaluation
1339
+ .evaluationDomain ??
1340
+ textOptions.evaluationDomain ??
1341
+ factoryResult.domainType,
1342
+ }
1343
+ : undefined,
1344
+ };
1345
+ if (this.conversationMemoryConfig?.conversationMemory?.mem0Enabled &&
1346
+ options.context?.userId &&
1347
+ generateResult.content) {
1348
+ // Non-blocking memory storage - run in background
1349
+ setImmediate(async () => {
1350
+ try {
1351
+ const mem0 = await this.ensureMem0Ready();
1352
+ if (mem0) {
1353
+ // Store complete conversation turn (user + AI messages)
1354
+ const conversationTurn = [
1355
+ { role: "user", content: options.input.text },
1356
+ { role: "system", content: generateResult.content },
1357
+ ];
1358
+ await mem0.add(JSON.stringify(conversationTurn), {
1359
+ userId: options.context?.userId,
1360
+ metadata: {
1361
+ timestamp: new Date().toISOString(),
1362
+ provider: generateResult.provider,
1363
+ model: generateResult.model,
1364
+ type: "conversation_turn",
1365
+ async_mode: true,
1366
+ },
1367
+ });
1368
+ }
1369
+ }
1370
+ catch (error) {
1371
+ // Non-blocking: Log error but don't fail the generation
1372
+ logger.warn("Mem0 memory storage failed:", error);
1373
+ }
1213
1374
  });
1214
- // Continue with warning rather than throwing - graceful degradation
1215
1375
  }
1216
- }
1217
- // 🔧 CRITICAL FIX: Convert to TextGenerationOptions while preserving the input object for multimodal support
1218
- const baseOptions = {
1219
- prompt: options.input.text,
1220
- provider: options.provider,
1221
- model: options.model,
1222
- temperature: options.temperature,
1223
- maxTokens: options.maxTokens,
1224
- systemPrompt: options.systemPrompt,
1225
- schema: options.schema,
1226
- output: options.output,
1227
- disableTools: options.disableTools,
1228
- enableAnalytics: options.enableAnalytics,
1229
- enableEvaluation: options.enableEvaluation,
1230
- context: options.context,
1231
- evaluationDomain: options.evaluationDomain,
1232
- toolUsageContext: options.toolUsageContext,
1233
- input: options.input, // This includes text, images, and content arrays
1234
- region: options.region,
1235
- };
1236
- // Apply factory enhancement using centralized utilities
1237
- const textOptions = enhanceTextGenerationOptions(baseOptions, factoryResult);
1238
- // Pass conversation memory config if available
1239
- if (this.conversationMemory) {
1240
- textOptions.conversationMemoryConfig = this.conversationMemory.config;
1241
- // Include original prompt for context summarization
1242
- textOptions.originalPrompt = originalPrompt;
1243
- }
1244
- // Detect and execute domain-specific tools
1245
- const { toolResults, enhancedPrompt } = await this.detectAndExecuteTools(textOptions.prompt || options.input.text, factoryResult.domainType);
1246
- // Update prompt with tool results if available
1247
- if (enhancedPrompt !== textOptions.prompt) {
1248
- textOptions.prompt = enhancedPrompt;
1249
- logger.debug("Enhanced prompt with tool results", {
1250
- originalLength: options.input.text.length,
1251
- enhancedLength: enhancedPrompt.length,
1252
- toolResults: toolResults.length,
1253
- });
1254
- }
1255
- // Use redesigned generation logic
1256
- const textResult = await this.generateTextInternal(textOptions);
1257
- // Emit generation completion event (NeuroLink format - enhanced with content)
1258
- this.emitter.emit("generation:end", {
1259
- provider: textResult.provider,
1260
- responseTime: Date.now() - startTime,
1261
- toolsUsed: textResult.toolsUsed,
1262
- timestamp: Date.now(),
1263
- result: textResult, // Enhanced: include full result
1376
+ return generateResult;
1264
1377
  });
1265
- // ADD: Bedrock-compatible response:end event with content
1266
- this.emitter.emit("response:end", textResult.content || "");
1267
- // ADD: Bedrock-compatible message event
1268
- this.emitter.emit("message", `Generation completed in ${Date.now() - startTime}ms`);
1269
- // Convert back to GenerateResult
1270
- const generateResult = {
1271
- content: textResult.content,
1272
- provider: textResult.provider,
1273
- model: textResult.model,
1274
- usage: textResult.usage
1275
- ? {
1276
- input: textResult.usage.input || 0,
1277
- output: textResult.usage.output || 0,
1278
- total: textResult.usage.total || 0,
1279
- }
1280
- : undefined,
1281
- responseTime: textResult.responseTime,
1282
- toolsUsed: textResult.toolsUsed,
1283
- toolExecutions: transformToolExecutions(textResult.toolExecutions),
1284
- enhancedWithTools: textResult.enhancedWithTools,
1285
- availableTools: transformAvailableTools(textResult.availableTools),
1286
- analytics: textResult.analytics,
1287
- evaluation: textResult.evaluation
1288
- ? {
1289
- ...textResult.evaluation,
1290
- isOffTopic: textResult.evaluation
1291
- .isOffTopic ?? false,
1292
- alertSeverity: textResult.evaluation
1293
- .alertSeverity ??
1294
- "none",
1295
- reasoning: textResult.evaluation
1296
- .reasoning ?? "No evaluation provided",
1297
- evaluationModel: textResult.evaluation
1298
- .evaluationModel ?? "unknown",
1299
- evaluationTime: textResult.evaluation
1300
- .evaluationTime ?? Date.now(),
1301
- // Include evaluationDomain from original options
1302
- evaluationDomain: textResult.evaluation
1303
- .evaluationDomain ??
1304
- textOptions.evaluationDomain ??
1305
- factoryResult.domainType,
1306
- }
1307
- : undefined,
1308
- };
1309
- if (this.conversationMemoryConfig?.conversationMemory?.mem0Enabled &&
1310
- options.context?.userId &&
1311
- generateResult.content) {
1312
- // Non-blocking memory storage - run in background
1313
- setImmediate(async () => {
1314
- try {
1315
- const mem0 = await this.ensureMem0Ready();
1316
- if (mem0) {
1317
- // Store complete conversation turn (user + AI messages)
1318
- const conversationTurn = [
1319
- { role: "user", content: options.input.text },
1320
- { role: "system", content: generateResult.content },
1321
- ];
1322
- await mem0.add(JSON.stringify(conversationTurn), {
1323
- userId: options.context?.userId,
1324
- metadata: {
1325
- timestamp: new Date().toISOString(),
1326
- provider: generateResult.provider,
1327
- model: generateResult.model,
1328
- type: "conversation_turn",
1329
- async_mode: true,
1330
- },
1331
- });
1332
- }
1333
- }
1334
- catch (error) {
1335
- // Non-blocking: Log error but don't fail the generation
1336
- logger.warn("Mem0 memory storage failed:", error);
1337
- }
1338
- });
1339
- }
1340
- return generateResult;
1341
1378
  }
1342
1379
  /**
1343
1380
  * BACKWARD COMPATIBILITY: Legacy generateText method
@@ -1863,153 +1900,155 @@ export class NeuroLink {
1863
1900
  const originalPrompt = options.input.text; // Store the original prompt for memory storage
1864
1901
  await this.validateStreamInput(options);
1865
1902
  this.emitStreamStartEvents(options, startTime);
1866
- let enhancedOptions;
1867
- let factoryResult;
1868
- try {
1869
- // Initialize conversation memory if needed (for lazy loading)
1870
- await this.initializeConversationMemoryForGeneration(streamId, startTime, hrTimeStart);
1871
- // Initialize MCP
1872
- await this.initializeMCP();
1873
- const _originalPrompt = options.input.text;
1874
- if (this.conversationMemoryConfig?.conversationMemory?.mem0Enabled &&
1875
- options.context?.userId) {
1876
- try {
1877
- const mem0 = await this.ensureMem0Ready();
1878
- if (!mem0) {
1879
- // Continue without memories if mem0 is not available
1880
- logger.debug("Mem0 not available, continuing without memory retrieval");
1881
- }
1882
- else {
1883
- const memories = await mem0.search(options.input.text, {
1884
- userId: options.context.userId,
1885
- limit: 5,
1886
- });
1887
- if (memories?.results?.length > 0) {
1888
- // Enhance the input with memory context
1889
- const memoryContext = memories.results
1890
- .map((m) => m.memory)
1891
- .join("\n");
1892
- options.input.text = this.formatMemoryContext(memoryContext, options.input.text);
1903
+ // Set session and user IDs from context for Langfuse spans and execute with proper async scoping
1904
+ return await this.setLangfuseContextFromOptions(options, async () => {
1905
+ let enhancedOptions;
1906
+ let factoryResult;
1907
+ try {
1908
+ // Initialize conversation memory if needed (for lazy loading)
1909
+ await this.initializeConversationMemoryForGeneration(streamId, startTime, hrTimeStart);
1910
+ // Initialize MCP
1911
+ await this.initializeMCP();
1912
+ const _originalPrompt = options.input.text;
1913
+ if (this.conversationMemoryConfig?.conversationMemory?.mem0Enabled &&
1914
+ options.context?.userId) {
1915
+ try {
1916
+ const mem0 = await this.ensureMem0Ready();
1917
+ if (!mem0) {
1918
+ // Continue without memories if mem0 is not available
1919
+ logger.debug("Mem0 not available, continuing without memory retrieval");
1920
+ }
1921
+ else {
1922
+ const memories = await mem0.search(options.input.text, {
1923
+ userId: options.context.userId,
1924
+ limit: 5,
1925
+ });
1926
+ if (memories?.results?.length > 0) {
1927
+ // Enhance the input with memory context
1928
+ const memoryContext = memories.results
1929
+ .map((m) => m.memory)
1930
+ .join("\n");
1931
+ options.input.text = this.formatMemoryContext(memoryContext, options.input.text);
1932
+ }
1893
1933
  }
1894
1934
  }
1935
+ catch (error) {
1936
+ // Non-blocking: Log error but continue with streaming
1937
+ logger.warn("Mem0 memory retrieval failed:", error);
1938
+ }
1895
1939
  }
1896
- catch (error) {
1897
- // Non-blocking: Log error but continue with streaming
1898
- logger.warn("Mem0 memory retrieval failed:", error);
1899
- }
1900
- }
1901
- // Apply orchestration if enabled and no specific provider/model requested
1902
- if (this.enableOrchestration && !options.provider && !options.model) {
1903
- try {
1904
- const orchestratedOptions = await this.applyStreamOrchestration(options);
1905
- logger.debug("Stream orchestration applied", {
1906
- originalProvider: options.provider || "auto",
1907
- orchestratedProvider: orchestratedOptions.provider,
1908
- orchestratedModel: orchestratedOptions.model,
1909
- prompt: options.input.text?.substring(0, 100),
1910
- });
1911
- // Use orchestrated options
1912
- Object.assign(options, orchestratedOptions);
1913
- }
1914
- catch (error) {
1915
- logger.warn("Stream orchestration failed, continuing with original options", {
1916
- error: error instanceof Error ? error.message : String(error),
1917
- originalProvider: options.provider || "auto",
1918
- });
1919
- // Continue with original options if orchestration fails
1920
- }
1921
- }
1922
- factoryResult = processStreamingFactoryOptions(options);
1923
- enhancedOptions = createCleanStreamOptions(options);
1924
- if (options.input?.text) {
1925
- const { toolResults: _toolResults, enhancedPrompt } = await this.detectAndExecuteTools(options.input.text, undefined);
1926
- if (enhancedPrompt !== options.input.text) {
1927
- enhancedOptions.input.text = enhancedPrompt;
1940
+ // Apply orchestration if enabled and no specific provider/model requested
1941
+ if (this.enableOrchestration && !options.provider && !options.model) {
1942
+ try {
1943
+ const orchestratedOptions = await this.applyStreamOrchestration(options);
1944
+ logger.debug("Stream orchestration applied", {
1945
+ originalProvider: options.provider || "auto",
1946
+ orchestratedProvider: orchestratedOptions.provider,
1947
+ orchestratedModel: orchestratedOptions.model,
1948
+ prompt: options.input.text?.substring(0, 100),
1949
+ });
1950
+ // Use orchestrated options
1951
+ Object.assign(options, orchestratedOptions);
1952
+ }
1953
+ catch (error) {
1954
+ logger.warn("Stream orchestration failed, continuing with original options", {
1955
+ error: error instanceof Error ? error.message : String(error),
1956
+ originalProvider: options.provider || "auto",
1957
+ });
1958
+ // Continue with original options if orchestration fails
1959
+ }
1928
1960
  }
1929
- }
1930
- const { stream: mcpStream, provider: providerName } = await this.createMCPStream(enhancedOptions);
1931
- // Create a wrapper around the stream that accumulates content
1932
- let accumulatedContent = "";
1933
- const processedStream = (async function* (self) {
1934
- try {
1935
- for await (const chunk of mcpStream) {
1936
- if (chunk &&
1937
- "content" in chunk &&
1938
- typeof chunk.content === "string") {
1939
- accumulatedContent += chunk.content;
1940
- // Emit chunk event for compatibility
1941
- self.emitter.emit("response:chunk", chunk.content);
1942
- }
1943
- yield chunk; // Preserve original streaming behavior
1961
+ factoryResult = processStreamingFactoryOptions(options);
1962
+ enhancedOptions = createCleanStreamOptions(options);
1963
+ if (options.input?.text) {
1964
+ const { toolResults: _toolResults, enhancedPrompt } = await this.detectAndExecuteTools(options.input.text, undefined);
1965
+ if (enhancedPrompt !== options.input.text) {
1966
+ enhancedOptions.input.text = enhancedPrompt;
1944
1967
  }
1945
1968
  }
1946
- finally {
1947
- // Store memory after stream consumption is complete
1948
- if (self.conversationMemory && enhancedOptions.context?.sessionId) {
1949
- const sessionId = enhancedOptions.context?.sessionId;
1950
- const userId = enhancedOptions.context
1951
- ?.userId;
1952
- try {
1953
- await self.conversationMemory.storeConversationTurn(sessionId, userId, originalPrompt ?? "", accumulatedContent, new Date(startTime));
1954
- logger.debug("Stream conversation turn stored", {
1955
- sessionId,
1956
- userInputLength: originalPrompt?.length ?? 0,
1957
- responseLength: accumulatedContent.length,
1958
- });
1959
- }
1960
- catch (error) {
1961
- logger.warn("Failed to store stream conversation turn", {
1962
- error: error instanceof Error ? error.message : String(error),
1963
- });
1969
+ const { stream: mcpStream, provider: providerName } = await this.createMCPStream(enhancedOptions);
1970
+ // Create a wrapper around the stream that accumulates content
1971
+ let accumulatedContent = "";
1972
+ const processedStream = (async function* (self) {
1973
+ try {
1974
+ for await (const chunk of mcpStream) {
1975
+ if (chunk &&
1976
+ "content" in chunk &&
1977
+ typeof chunk.content === "string") {
1978
+ accumulatedContent += chunk.content;
1979
+ // Emit chunk event for compatibility
1980
+ self.emitter.emit("response:chunk", chunk.content);
1981
+ }
1982
+ yield chunk; // Preserve original streaming behavior
1964
1983
  }
1965
1984
  }
1966
- if (self.conversationMemoryConfig?.conversationMemory?.mem0Enabled &&
1967
- enhancedOptions.context?.userId &&
1968
- accumulatedContent.trim()) {
1969
- // Non-blocking memory storage - run in background
1970
- setImmediate(async () => {
1985
+ finally {
1986
+ // Store memory after stream consumption is complete
1987
+ if (self.conversationMemory && enhancedOptions.context?.sessionId) {
1988
+ const sessionId = enhancedOptions.context?.sessionId;
1989
+ const userId = enhancedOptions.context?.userId;
1971
1990
  try {
1972
- const mem0 = await self.ensureMem0Ready();
1973
- if (mem0) {
1974
- // Store complete conversation turn (user + AI messages)
1975
- const conversationTurn = [
1976
- { role: "user", content: originalPrompt },
1977
- { role: "system", content: accumulatedContent.trim() },
1978
- ];
1979
- await mem0.add(JSON.stringify(conversationTurn), {
1980
- userId: enhancedOptions.context?.userId,
1981
- metadata: {
1982
- timestamp: new Date().toISOString(),
1983
- type: "conversation_turn_stream",
1984
- userMessage: originalPrompt,
1985
- async_mode: true,
1986
- aiResponse: accumulatedContent.trim(),
1987
- },
1988
- });
1989
- }
1991
+ await self.conversationMemory.storeConversationTurn(sessionId, userId, originalPrompt ?? "", accumulatedContent, new Date(startTime));
1992
+ logger.debug("Stream conversation turn stored", {
1993
+ sessionId,
1994
+ userInputLength: originalPrompt?.length ?? 0,
1995
+ responseLength: accumulatedContent.length,
1996
+ });
1990
1997
  }
1991
1998
  catch (error) {
1992
- logger.warn("Mem0 memory storage failed:", error);
1999
+ logger.warn("Failed to store stream conversation turn", {
2000
+ error: error instanceof Error ? error.message : String(error),
2001
+ });
1993
2002
  }
1994
- });
2003
+ }
2004
+ if (self.conversationMemoryConfig?.conversationMemory?.mem0Enabled &&
2005
+ enhancedOptions.context?.userId &&
2006
+ accumulatedContent.trim()) {
2007
+ // Non-blocking memory storage - run in background
2008
+ setImmediate(async () => {
2009
+ try {
2010
+ const mem0 = await self.ensureMem0Ready();
2011
+ if (mem0) {
2012
+ // Store complete conversation turn (user + AI messages)
2013
+ const conversationTurn = [
2014
+ { role: "user", content: originalPrompt },
2015
+ { role: "system", content: accumulatedContent.trim() },
2016
+ ];
2017
+ await mem0.add(JSON.stringify(conversationTurn), {
2018
+ userId: enhancedOptions.context?.userId,
2019
+ metadata: {
2020
+ timestamp: new Date().toISOString(),
2021
+ type: "conversation_turn_stream",
2022
+ userMessage: originalPrompt,
2023
+ async_mode: true,
2024
+ aiResponse: accumulatedContent.trim(),
2025
+ },
2026
+ });
2027
+ }
2028
+ }
2029
+ catch (error) {
2030
+ logger.warn("Mem0 memory storage failed:", error);
2031
+ }
2032
+ });
2033
+ }
1995
2034
  }
1996
- }
1997
- })(this);
1998
- const streamResult = await this.processStreamResult(mcpStream, enhancedOptions, factoryResult);
1999
- const responseTime = Date.now() - startTime;
2000
- this.emitStreamEndEvents(streamResult);
2001
- return this.createStreamResponse(streamResult, processedStream, {
2002
- providerName,
2003
- options,
2004
- startTime,
2005
- responseTime,
2006
- streamId,
2007
- fallback: false,
2008
- });
2009
- }
2010
- catch (error) {
2011
- return this.handleStreamError(error, options, startTime, streamId, undefined, undefined);
2012
- }
2035
+ })(this);
2036
+ const streamResult = await this.processStreamResult(mcpStream, enhancedOptions, factoryResult);
2037
+ const responseTime = Date.now() - startTime;
2038
+ this.emitStreamEndEvents(streamResult);
2039
+ return this.createStreamResponse(streamResult, processedStream, {
2040
+ providerName,
2041
+ options,
2042
+ startTime,
2043
+ responseTime,
2044
+ streamId,
2045
+ fallback: false,
2046
+ });
2047
+ }
2048
+ catch (error) {
2049
+ return this.handleStreamError(error, options, startTime, streamId, undefined, undefined);
2050
+ }
2051
+ });
2013
2052
  }
2014
2053
  /**
2015
2054
  * Validate stream input with comprehensive error reporting