@juspay/neurolink 7.11.1 → 7.13.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/CHANGELOG.md +12 -0
- package/README.md +16 -0
- package/dist/config/conversationMemoryConfig.d.ts +27 -0
- package/dist/config/conversationMemoryConfig.js +39 -0
- package/dist/context/ContextManager.d.ts +28 -0
- package/dist/context/ContextManager.js +102 -0
- package/dist/context/config.d.ts +5 -0
- package/dist/context/config.js +38 -0
- package/dist/context/types.d.ts +20 -0
- package/dist/context/types.js +1 -0
- package/dist/context/utils.d.ts +7 -0
- package/dist/context/utils.js +8 -0
- package/dist/core/baseProvider.js +4 -2
- package/dist/core/conversationMemoryManager.d.ts +41 -0
- package/dist/core/conversationMemoryManager.js +152 -0
- package/dist/core/types.d.ts +2 -0
- package/dist/lib/config/conversationMemoryConfig.d.ts +27 -0
- package/dist/lib/config/conversationMemoryConfig.js +39 -0
- package/dist/lib/context/ContextManager.d.ts +28 -0
- package/dist/lib/context/ContextManager.js +102 -0
- package/dist/lib/context/config.d.ts +5 -0
- package/dist/lib/context/config.js +38 -0
- package/dist/lib/context/types.d.ts +20 -0
- package/dist/lib/context/types.js +1 -0
- package/dist/lib/context/utils.d.ts +7 -0
- package/dist/lib/context/utils.js +8 -0
- package/dist/lib/core/baseProvider.js +4 -2
- package/dist/lib/core/conversationMemoryManager.d.ts +41 -0
- package/dist/lib/core/conversationMemoryManager.js +152 -0
- package/dist/lib/core/types.d.ts +2 -0
- package/dist/lib/neurolink.d.ts +39 -4
- package/dist/lib/neurolink.js +106 -5
- package/dist/lib/providers/amazonBedrock.js +4 -2
- package/dist/lib/providers/anthropic.js +4 -2
- package/dist/lib/providers/azureOpenai.js +4 -2
- package/dist/lib/providers/googleAiStudio.js +4 -2
- package/dist/lib/providers/googleVertex.js +4 -2
- package/dist/lib/providers/huggingFace.js +4 -2
- package/dist/lib/providers/litellm.js +4 -2
- package/dist/lib/providers/mistral.js +3 -2
- package/dist/lib/providers/openAI.js +4 -2
- package/dist/lib/types/conversationTypes.d.ts +95 -0
- package/dist/lib/types/conversationTypes.js +17 -0
- package/dist/lib/types/streamTypes.d.ts +2 -0
- package/dist/lib/utils/conversationMemoryUtils.d.ts +22 -0
- package/dist/lib/utils/conversationMemoryUtils.js +77 -0
- package/dist/lib/utils/messageBuilder.d.ts +13 -0
- package/dist/lib/utils/messageBuilder.js +48 -0
- package/dist/neurolink.d.ts +39 -4
- package/dist/neurolink.js +106 -5
- package/dist/providers/amazonBedrock.js +4 -2
- package/dist/providers/anthropic.js +4 -2
- package/dist/providers/azureOpenai.js +4 -2
- package/dist/providers/googleAiStudio.js +4 -2
- package/dist/providers/googleVertex.js +4 -2
- package/dist/providers/huggingFace.js +4 -2
- package/dist/providers/litellm.js +4 -2
- package/dist/providers/mistral.js +3 -2
- package/dist/providers/openAI.js +4 -2
- package/dist/types/conversationTypes.d.ts +95 -0
- package/dist/types/conversationTypes.js +17 -0
- package/dist/types/streamTypes.d.ts +2 -0
- package/dist/utils/conversationMemoryUtils.d.ts +22 -0
- package/dist/utils/conversationMemoryUtils.js +77 -0
- package/dist/utils/messageBuilder.d.ts +13 -0
- package/dist/utils/messageBuilder.js +48 -0
- package/package.json +1 -1
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Message Builder Utility
|
|
3
|
+
* Centralized logic for building message arrays from TextGenerationOptions
|
|
4
|
+
*/
|
|
5
|
+
import type { ChatMessage } from "../types/conversationTypes.js";
|
|
6
|
+
import type { TextGenerationOptions } from "../core/types.js";
|
|
7
|
+
import type { StreamOptions } from "../types/streamTypes.js";
|
|
8
|
+
/**
|
|
9
|
+
* Build a properly formatted message array for AI providers
|
|
10
|
+
* Combines system prompt, conversation history, and current user prompt
|
|
11
|
+
* Supports both TextGenerationOptions and StreamOptions
|
|
12
|
+
*/
|
|
13
|
+
export declare function buildMessagesArray(options: TextGenerationOptions | StreamOptions): ChatMessage[];
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Message Builder Utility
|
|
3
|
+
* Centralized logic for building message arrays from TextGenerationOptions
|
|
4
|
+
*/
|
|
5
|
+
import { CONVERSATION_INSTRUCTIONS } from "../config/conversationMemoryConfig.js";
|
|
6
|
+
/**
|
|
7
|
+
* Build a properly formatted message array for AI providers
|
|
8
|
+
* Combines system prompt, conversation history, and current user prompt
|
|
9
|
+
* Supports both TextGenerationOptions and StreamOptions
|
|
10
|
+
*/
|
|
11
|
+
export function buildMessagesArray(options) {
|
|
12
|
+
const messages = [];
|
|
13
|
+
// Check if conversation history exists
|
|
14
|
+
const hasConversationHistory = options.conversationMessages && options.conversationMessages.length > 0;
|
|
15
|
+
// Build enhanced system prompt
|
|
16
|
+
let systemPrompt = options.systemPrompt?.trim() || "";
|
|
17
|
+
// Add conversation-aware instructions when history exists
|
|
18
|
+
if (hasConversationHistory) {
|
|
19
|
+
systemPrompt = `${systemPrompt.trim()}${CONVERSATION_INSTRUCTIONS}`;
|
|
20
|
+
}
|
|
21
|
+
// Add system message if we have one
|
|
22
|
+
if (systemPrompt.trim()) {
|
|
23
|
+
messages.push({
|
|
24
|
+
role: "system",
|
|
25
|
+
content: systemPrompt.trim(),
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
// Add conversation history if available
|
|
29
|
+
if (hasConversationHistory && options.conversationMessages) {
|
|
30
|
+
messages.push(...options.conversationMessages);
|
|
31
|
+
}
|
|
32
|
+
// Add current user prompt (required)
|
|
33
|
+
// Handle both TextGenerationOptions (prompt field) and StreamOptions (input.text field)
|
|
34
|
+
let currentPrompt;
|
|
35
|
+
if ("prompt" in options && options.prompt) {
|
|
36
|
+
currentPrompt = options.prompt;
|
|
37
|
+
}
|
|
38
|
+
else if ("input" in options && options.input?.text) {
|
|
39
|
+
currentPrompt = options.input.text;
|
|
40
|
+
}
|
|
41
|
+
if (currentPrompt?.trim()) {
|
|
42
|
+
messages.push({
|
|
43
|
+
role: "user",
|
|
44
|
+
content: currentPrompt.trim(),
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return messages;
|
|
48
|
+
}
|
package/dist/neurolink.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ import type { StreamOptions, StreamResult } from "./types/streamTypes.js";
|
|
|
11
11
|
import type { SimpleTool } from "./sdk/toolRegistration.js";
|
|
12
12
|
import type { InMemoryMCPServerConfig } from "./types/mcpTypes.js";
|
|
13
13
|
import { EventEmitter } from "events";
|
|
14
|
+
import type { ConversationMemoryConfig } from "./types/conversationTypes.js";
|
|
14
15
|
export interface ProviderStatus {
|
|
15
16
|
provider: string;
|
|
16
17
|
status: "working" | "failed" | "not-configured";
|
|
@@ -40,9 +41,11 @@ export interface MCPServerInfo {
|
|
|
40
41
|
hasServer: boolean;
|
|
41
42
|
metadata?: unknown;
|
|
42
43
|
}
|
|
44
|
+
import type { ContextManagerConfig } from "./context/types.js";
|
|
43
45
|
export declare class NeuroLink {
|
|
44
46
|
private mcpInitialized;
|
|
45
47
|
private emitter;
|
|
48
|
+
private contextManager;
|
|
46
49
|
private customTools;
|
|
47
50
|
private inMemoryServers;
|
|
48
51
|
private toolCircuitBreakers;
|
|
@@ -55,7 +58,10 @@ export declare class NeuroLink {
|
|
|
55
58
|
* @param success - Whether the tool execution was successful
|
|
56
59
|
*/
|
|
57
60
|
private emitToolEndEvent;
|
|
58
|
-
|
|
61
|
+
private conversationMemory?;
|
|
62
|
+
constructor(config?: {
|
|
63
|
+
conversationMemory?: Partial<ConversationMemoryConfig>;
|
|
64
|
+
});
|
|
59
65
|
/**
|
|
60
66
|
* Initialize MCP registry with enhanced error handling and resource cleanup
|
|
61
67
|
* Uses isolated async context to prevent hanging
|
|
@@ -65,6 +71,21 @@ export declare class NeuroLink {
|
|
|
65
71
|
* MAIN ENTRY POINT: Enhanced generate method with new function signature
|
|
66
72
|
* Replaces both generateText and legacy methods
|
|
67
73
|
*/
|
|
74
|
+
/**
|
|
75
|
+
* Extracts the original prompt text from the provided input.
|
|
76
|
+
* If a string is provided, it returns the string directly.
|
|
77
|
+
* If a GenerateOptions object is provided, it returns the input text from the object.
|
|
78
|
+
* @param optionsOrPrompt The prompt input, either as a string or a GenerateOptions object.
|
|
79
|
+
* @returns The original prompt text as a string.
|
|
80
|
+
*/
|
|
81
|
+
private _extractOriginalPrompt;
|
|
82
|
+
/**
|
|
83
|
+
* Enables automatic context summarization for the NeuroLink instance.
|
|
84
|
+
* Once enabled, the instance will maintain conversation history and
|
|
85
|
+
* automatically summarize it when it exceeds token limits.
|
|
86
|
+
* @param config Optional configuration to override default summarization settings.
|
|
87
|
+
*/
|
|
88
|
+
enableContextSummarization(config?: Partial<ContextManagerConfig>): void;
|
|
68
89
|
generate(optionsOrPrompt: GenerateOptions | string): Promise<GenerateResult>;
|
|
69
90
|
/**
|
|
70
91
|
* BACKWARD COMPATIBILITY: Legacy generateText method
|
|
@@ -75,9 +96,11 @@ export declare class NeuroLink {
|
|
|
75
96
|
* REDESIGNED INTERNAL GENERATION - NO CIRCULAR DEPENDENCIES
|
|
76
97
|
*
|
|
77
98
|
* This method implements a clean fallback chain:
|
|
78
|
-
* 1.
|
|
79
|
-
* 2.
|
|
80
|
-
* 3.
|
|
99
|
+
* 1. Initialize conversation memory if enabled
|
|
100
|
+
* 2. Inject conversation history into prompt
|
|
101
|
+
* 3. Try MCP-enhanced generation if available
|
|
102
|
+
* 4. Fall back to direct provider generation
|
|
103
|
+
* 5. Store conversation turn for future context
|
|
81
104
|
*/
|
|
82
105
|
private generateTextInternal;
|
|
83
106
|
/**
|
|
@@ -367,6 +390,18 @@ export declare class NeuroLink {
|
|
|
367
390
|
recommendations: string[];
|
|
368
391
|
}>;
|
|
369
392
|
};
|
|
393
|
+
/**
|
|
394
|
+
* Get conversation memory statistics (public API)
|
|
395
|
+
*/
|
|
396
|
+
getConversationStats(): Promise<import("./types/conversationTypes.js").ConversationMemoryStats>;
|
|
397
|
+
/**
|
|
398
|
+
* Clear conversation history for a specific session (public API)
|
|
399
|
+
*/
|
|
400
|
+
clearConversationSession(sessionId: string): Promise<boolean>;
|
|
401
|
+
/**
|
|
402
|
+
* Clear all conversation history (public API)
|
|
403
|
+
*/
|
|
404
|
+
clearAllConversations(): Promise<void>;
|
|
370
405
|
}
|
|
371
406
|
export declare const neurolink: NeuroLink;
|
|
372
407
|
export default neurolink;
|
package/dist/neurolink.js
CHANGED
|
@@ -27,10 +27,15 @@ import { processFactoryOptions, enhanceTextGenerationOptions, validateFactoryCon
|
|
|
27
27
|
// Enhanced error handling imports
|
|
28
28
|
import { ErrorFactory, NeuroLinkError, withTimeout, withRetry, isRetriableError, logStructuredError, CircuitBreaker, } from "./utils/errorHandling.js";
|
|
29
29
|
import { EventEmitter } from "events";
|
|
30
|
+
import { ConversationMemoryManager } from "./core/conversationMemoryManager.js";
|
|
31
|
+
import { applyConversationMemoryDefaults, getConversationMessages, storeConversationTurn, } from "./utils/conversationMemoryUtils.js";
|
|
32
|
+
import { ContextManager } from "./context/ContextManager.js";
|
|
33
|
+
import { defaultContextConfig } from "./context/config.js";
|
|
30
34
|
// Core types imported from core/types.js
|
|
31
35
|
export class NeuroLink {
|
|
32
36
|
mcpInitialized = false;
|
|
33
37
|
emitter = new EventEmitter();
|
|
38
|
+
contextManager = null;
|
|
34
39
|
// Tool registration support
|
|
35
40
|
customTools = new Map();
|
|
36
41
|
inMemoryServers = new Map();
|
|
@@ -52,11 +57,22 @@ export class NeuroLink {
|
|
|
52
57
|
timestamp: Date.now(),
|
|
53
58
|
});
|
|
54
59
|
}
|
|
55
|
-
|
|
60
|
+
// Conversation memory support
|
|
61
|
+
conversationMemory;
|
|
62
|
+
constructor(config) {
|
|
56
63
|
// SDK always disables manual MCP config for security
|
|
57
64
|
ProviderRegistry.setOptions({
|
|
58
65
|
enableManualMCP: false,
|
|
59
66
|
});
|
|
67
|
+
// Initialize conversation memory if enabled
|
|
68
|
+
if (config?.conversationMemory?.enabled) {
|
|
69
|
+
const memoryConfig = applyConversationMemoryDefaults(config.conversationMemory);
|
|
70
|
+
this.conversationMemory = new ConversationMemoryManager(memoryConfig);
|
|
71
|
+
logger.info("NeuroLink initialized with conversation memory", {
|
|
72
|
+
maxSessions: memoryConfig.maxSessions,
|
|
73
|
+
maxTurnsPerSession: memoryConfig.maxTurnsPerSession,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
60
76
|
}
|
|
61
77
|
/**
|
|
62
78
|
* Initialize MCP registry with enhanced error handling and resource cleanup
|
|
@@ -107,7 +123,33 @@ export class NeuroLink {
|
|
|
107
123
|
* MAIN ENTRY POINT: Enhanced generate method with new function signature
|
|
108
124
|
* Replaces both generateText and legacy methods
|
|
109
125
|
*/
|
|
126
|
+
/**
|
|
127
|
+
* Extracts the original prompt text from the provided input.
|
|
128
|
+
* If a string is provided, it returns the string directly.
|
|
129
|
+
* If a GenerateOptions object is provided, it returns the input text from the object.
|
|
130
|
+
* @param optionsOrPrompt The prompt input, either as a string or a GenerateOptions object.
|
|
131
|
+
* @returns The original prompt text as a string.
|
|
132
|
+
*/
|
|
133
|
+
_extractOriginalPrompt(optionsOrPrompt) {
|
|
134
|
+
return typeof optionsOrPrompt === 'string' ? optionsOrPrompt : optionsOrPrompt.input.text;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Enables automatic context summarization for the NeuroLink instance.
|
|
138
|
+
* Once enabled, the instance will maintain conversation history and
|
|
139
|
+
* automatically summarize it when it exceeds token limits.
|
|
140
|
+
* @param config Optional configuration to override default summarization settings.
|
|
141
|
+
*/
|
|
142
|
+
enableContextSummarization(config) {
|
|
143
|
+
const contextConfig = {
|
|
144
|
+
...defaultContextConfig,
|
|
145
|
+
...config,
|
|
146
|
+
};
|
|
147
|
+
// Pass the internal generator function directly, bound to the correct `this` context.
|
|
148
|
+
this.contextManager = new ContextManager(this.generateTextInternal.bind(this), contextConfig);
|
|
149
|
+
logger.info("[NeuroLink] Automatic context summarization enabled.");
|
|
150
|
+
}
|
|
110
151
|
async generate(optionsOrPrompt) {
|
|
152
|
+
const originalPrompt = this._extractOriginalPrompt(optionsOrPrompt);
|
|
111
153
|
// Convert string prompt to full options
|
|
112
154
|
const options = typeof optionsOrPrompt === "string"
|
|
113
155
|
? { input: { text: optionsOrPrompt } }
|
|
@@ -116,6 +158,11 @@ export class NeuroLink {
|
|
|
116
158
|
if (!options.input?.text || typeof options.input.text !== "string") {
|
|
117
159
|
throw new Error("Input text is required and must be a non-empty string");
|
|
118
160
|
}
|
|
161
|
+
// Handle Context Management if enabled
|
|
162
|
+
if (this.contextManager) {
|
|
163
|
+
// Get the full context for the prompt without permanently adding the user's turn yet
|
|
164
|
+
options.input.text = this.contextManager.getContextForPrompt("user", options.input.text);
|
|
165
|
+
}
|
|
119
166
|
const startTime = Date.now();
|
|
120
167
|
// Emit generation start event
|
|
121
168
|
this.emitter.emit("generation:start", {
|
|
@@ -227,6 +274,11 @@ export class NeuroLink {
|
|
|
227
274
|
}
|
|
228
275
|
: undefined,
|
|
229
276
|
};
|
|
277
|
+
// Add both the user's turn and the AI's response to the permanent history
|
|
278
|
+
if (this.contextManager) {
|
|
279
|
+
await this.contextManager.addTurn("user", originalPrompt);
|
|
280
|
+
await this.contextManager.addTurn("assistant", generateResult.content);
|
|
281
|
+
}
|
|
230
282
|
return generateResult;
|
|
231
283
|
}
|
|
232
284
|
/**
|
|
@@ -247,9 +299,11 @@ export class NeuroLink {
|
|
|
247
299
|
* REDESIGNED INTERNAL GENERATION - NO CIRCULAR DEPENDENCIES
|
|
248
300
|
*
|
|
249
301
|
* This method implements a clean fallback chain:
|
|
250
|
-
* 1.
|
|
251
|
-
* 2.
|
|
252
|
-
* 3.
|
|
302
|
+
* 1. Initialize conversation memory if enabled
|
|
303
|
+
* 2. Inject conversation history into prompt
|
|
304
|
+
* 3. Try MCP-enhanced generation if available
|
|
305
|
+
* 4. Fall back to direct provider generation
|
|
306
|
+
* 5. Store conversation turn for future context
|
|
253
307
|
*/
|
|
254
308
|
async generateTextInternal(options) {
|
|
255
309
|
const startTime = Date.now();
|
|
@@ -257,14 +311,21 @@ export class NeuroLink {
|
|
|
257
311
|
logger.debug(`[${functionTag}] Starting generation`, {
|
|
258
312
|
provider: options.provider || "auto",
|
|
259
313
|
promptLength: options.prompt?.length || 0,
|
|
314
|
+
hasConversationMemory: !!this.conversationMemory,
|
|
260
315
|
});
|
|
261
316
|
try {
|
|
317
|
+
// Initialize conversation memory if enabled
|
|
318
|
+
if (this.conversationMemory) {
|
|
319
|
+
await this.conversationMemory.initialize();
|
|
320
|
+
}
|
|
262
321
|
// Try MCP-enhanced generation first (if not explicitly disabled)
|
|
263
322
|
if (!options.disableTools) {
|
|
264
323
|
try {
|
|
265
324
|
const mcpResult = await this.tryMCPGeneration(options);
|
|
266
325
|
if (mcpResult && mcpResult.content) {
|
|
267
326
|
logger.debug(`[${functionTag}] MCP generation successful`);
|
|
327
|
+
// Store conversation turn
|
|
328
|
+
await storeConversationTurn(this.conversationMemory, options, mcpResult);
|
|
268
329
|
return mcpResult;
|
|
269
330
|
}
|
|
270
331
|
}
|
|
@@ -277,6 +338,8 @@ export class NeuroLink {
|
|
|
277
338
|
// Fall back to direct provider generation
|
|
278
339
|
const directResult = await this.directProviderGeneration(options);
|
|
279
340
|
logger.debug(`[${functionTag}] Direct generation successful`);
|
|
341
|
+
// Store conversation turn
|
|
342
|
+
await storeConversationTurn(this.conversationMemory, options, directResult);
|
|
280
343
|
return directResult;
|
|
281
344
|
}
|
|
282
345
|
catch (error) {
|
|
@@ -334,6 +397,8 @@ export class NeuroLink {
|
|
|
334
397
|
}
|
|
335
398
|
// Create tool-aware system prompt
|
|
336
399
|
const enhancedSystemPrompt = this.createToolAwareSystemPrompt(options.systemPrompt, availableTools);
|
|
400
|
+
// Get conversation messages for context
|
|
401
|
+
const conversationMessages = await getConversationMessages(this.conversationMemory, options);
|
|
337
402
|
// Create provider and generate
|
|
338
403
|
const provider = await AIProviderFactory.createProvider(providerName, options.model, !options.disableTools, // Pass disableTools as inverse of enableMCP
|
|
339
404
|
this);
|
|
@@ -345,6 +410,7 @@ export class NeuroLink {
|
|
|
345
410
|
const result = await provider.generate({
|
|
346
411
|
...options,
|
|
347
412
|
systemPrompt: enhancedSystemPrompt,
|
|
413
|
+
conversationMessages, // Inject conversation history
|
|
348
414
|
});
|
|
349
415
|
const responseTime = Date.now() - startTime;
|
|
350
416
|
// Check if result is meaningful
|
|
@@ -413,6 +479,8 @@ export class NeuroLink {
|
|
|
413
479
|
for (const providerName of tryProviders) {
|
|
414
480
|
try {
|
|
415
481
|
logger.debug(`[${functionTag}] Attempting provider: ${providerName}`);
|
|
482
|
+
// Get conversation messages for context
|
|
483
|
+
const conversationMessages = await getConversationMessages(this.conversationMemory, options);
|
|
416
484
|
const provider = await AIProviderFactory.createProvider(providerName, options.model, !options.disableTools, // Pass disableTools as inverse of enableMCP
|
|
417
485
|
this);
|
|
418
486
|
// Enable tool execution for direct provider generation using BaseProvider method
|
|
@@ -420,7 +488,10 @@ export class NeuroLink {
|
|
|
420
488
|
customTools: this.customTools,
|
|
421
489
|
executeTool: this.executeTool.bind(this),
|
|
422
490
|
}, functionTag);
|
|
423
|
-
const result = await provider.generate(
|
|
491
|
+
const result = await provider.generate({
|
|
492
|
+
...options,
|
|
493
|
+
conversationMessages, // Inject conversation history
|
|
494
|
+
});
|
|
424
495
|
const responseTime = Date.now() - startTime;
|
|
425
496
|
if (!result) {
|
|
426
497
|
throw new Error(`Provider ${providerName} returned null result`);
|
|
@@ -1577,6 +1648,36 @@ export class NeuroLink {
|
|
|
1577
1648
|
tools,
|
|
1578
1649
|
};
|
|
1579
1650
|
}
|
|
1651
|
+
// ============================================================================
|
|
1652
|
+
// CONVERSATION MEMORY PUBLIC API
|
|
1653
|
+
// ============================================================================
|
|
1654
|
+
/**
|
|
1655
|
+
* Get conversation memory statistics (public API)
|
|
1656
|
+
*/
|
|
1657
|
+
async getConversationStats() {
|
|
1658
|
+
if (!this.conversationMemory) {
|
|
1659
|
+
throw new Error("Conversation memory is not enabled");
|
|
1660
|
+
}
|
|
1661
|
+
return await this.conversationMemory.getStats();
|
|
1662
|
+
}
|
|
1663
|
+
/**
|
|
1664
|
+
* Clear conversation history for a specific session (public API)
|
|
1665
|
+
*/
|
|
1666
|
+
async clearConversationSession(sessionId) {
|
|
1667
|
+
if (!this.conversationMemory) {
|
|
1668
|
+
throw new Error("Conversation memory is not enabled");
|
|
1669
|
+
}
|
|
1670
|
+
return await this.conversationMemory.clearSession(sessionId);
|
|
1671
|
+
}
|
|
1672
|
+
/**
|
|
1673
|
+
* Clear all conversation history (public API)
|
|
1674
|
+
*/
|
|
1675
|
+
async clearAllConversations() {
|
|
1676
|
+
if (!this.conversationMemory) {
|
|
1677
|
+
throw new Error("Conversation memory is not enabled");
|
|
1678
|
+
}
|
|
1679
|
+
await this.conversationMemory.clearAllSessions();
|
|
1680
|
+
}
|
|
1580
1681
|
}
|
|
1581
1682
|
// Create default instance
|
|
1582
1683
|
export const neurolink = new NeuroLink();
|
|
@@ -5,6 +5,7 @@ import { logger } from "../utils/logger.js";
|
|
|
5
5
|
import { TimeoutError, } from "../utils/timeout.js";
|
|
6
6
|
import { DEFAULT_MAX_TOKENS } from "../core/constants.js";
|
|
7
7
|
import { validateApiKey, createAWSAccessKeyConfig, createAWSSecretConfig, getAWSRegion, getAWSSessionToken, } from "../utils/providerConfig.js";
|
|
8
|
+
import { buildMessagesArray } from "../utils/messageBuilder.js";
|
|
8
9
|
// Configuration helpers
|
|
9
10
|
const getBedrockModelId = () => {
|
|
10
11
|
return (process.env.BEDROCK_MODEL ||
|
|
@@ -79,10 +80,11 @@ export class AmazonBedrockProvider extends BaseProvider {
|
|
|
79
80
|
async executeStream(options, analysisSchema) {
|
|
80
81
|
try {
|
|
81
82
|
this.validateStreamOptions(options);
|
|
83
|
+
// Build message array from options
|
|
84
|
+
const messages = buildMessagesArray(options);
|
|
82
85
|
const result = await streamText({
|
|
83
86
|
model: this.model,
|
|
84
|
-
|
|
85
|
-
system: options.systemPrompt,
|
|
87
|
+
messages: messages,
|
|
86
88
|
maxTokens: options.maxTokens || DEFAULT_MAX_TOKENS,
|
|
87
89
|
temperature: options.temperature,
|
|
88
90
|
});
|
|
@@ -5,6 +5,7 @@ import { logger } from "../utils/logger.js";
|
|
|
5
5
|
import { createTimeoutController, TimeoutError, } from "../utils/timeout.js";
|
|
6
6
|
import { DEFAULT_MAX_TOKENS, DEFAULT_MAX_STEPS } from "../core/constants.js";
|
|
7
7
|
import { validateApiKey, createAnthropicConfig, getProviderModel, } from "../utils/providerConfig.js";
|
|
8
|
+
import { buildMessagesArray } from "../utils/messageBuilder.js";
|
|
8
9
|
// Configuration helpers - now using consolidated utility
|
|
9
10
|
const getAnthropicApiKey = () => {
|
|
10
11
|
return validateApiKey(createAnthropicConfig());
|
|
@@ -94,10 +95,11 @@ export class AnthropicProvider extends BaseProvider {
|
|
|
94
95
|
// ✅ Get tools for streaming (same as generate method)
|
|
95
96
|
const shouldUseTools = !options.disableTools && this.supportsTools();
|
|
96
97
|
const tools = shouldUseTools ? await this.getAllTools() : {};
|
|
98
|
+
// Build message array from options
|
|
99
|
+
const messages = buildMessagesArray(options);
|
|
97
100
|
const result = await streamText({
|
|
98
101
|
model: this.model,
|
|
99
|
-
|
|
100
|
-
system: options.systemPrompt || undefined,
|
|
102
|
+
messages: messages,
|
|
101
103
|
temperature: options.temperature,
|
|
102
104
|
maxTokens: options.maxTokens || DEFAULT_MAX_TOKENS,
|
|
103
105
|
tools,
|
|
@@ -3,6 +3,7 @@ import { streamText } from "ai";
|
|
|
3
3
|
import { BaseProvider } from "../core/baseProvider.js";
|
|
4
4
|
import { validateApiKey, createAzureAPIKeyConfig, createAzureEndpointConfig, } from "../utils/providerConfig.js";
|
|
5
5
|
import { logger } from "../utils/logger.js";
|
|
6
|
+
import { buildMessagesArray } from "../utils/messageBuilder.js";
|
|
6
7
|
export class AzureOpenAIProvider extends BaseProvider {
|
|
7
8
|
apiKey;
|
|
8
9
|
resourceName;
|
|
@@ -69,12 +70,13 @@ export class AzureOpenAIProvider extends BaseProvider {
|
|
|
69
70
|
// executeGenerate removed - BaseProvider handles all generation with tools
|
|
70
71
|
async executeStream(options, analysisSchema) {
|
|
71
72
|
try {
|
|
73
|
+
// Build message array from options
|
|
74
|
+
const messages = buildMessagesArray(options);
|
|
72
75
|
const stream = await streamText({
|
|
73
76
|
model: this.azureProvider(this.deployment),
|
|
74
|
-
|
|
77
|
+
messages: messages,
|
|
75
78
|
maxTokens: options.maxTokens || 1000,
|
|
76
79
|
temperature: options.temperature || 0.7,
|
|
77
|
-
system: options.systemPrompt,
|
|
78
80
|
});
|
|
79
81
|
return {
|
|
80
82
|
stream: (async function* () {
|
|
@@ -6,6 +6,7 @@ import { logger } from "../utils/logger.js";
|
|
|
6
6
|
import { createTimeoutController, TimeoutError, } from "../utils/timeout.js";
|
|
7
7
|
import { DEFAULT_MAX_TOKENS, DEFAULT_MAX_STEPS } from "../core/constants.js";
|
|
8
8
|
import { streamAnalyticsCollector } from "../core/streamAnalytics.js";
|
|
9
|
+
import { buildMessagesArray } from "../utils/messageBuilder.js";
|
|
9
10
|
// Environment variable setup
|
|
10
11
|
if (!process.env.GOOGLE_GENERATIVE_AI_API_KEY &&
|
|
11
12
|
process.env.GOOGLE_AI_API_KEY) {
|
|
@@ -76,10 +77,11 @@ export class GoogleAIStudioProvider extends BaseProvider {
|
|
|
76
77
|
// Get tools consistently with generate method
|
|
77
78
|
const shouldUseTools = !options.disableTools && this.supportsTools();
|
|
78
79
|
const tools = shouldUseTools ? await this.getAllTools() : {};
|
|
80
|
+
// Build message array from options
|
|
81
|
+
const messages = buildMessagesArray(options);
|
|
79
82
|
const result = await streamText({
|
|
80
83
|
model,
|
|
81
|
-
|
|
82
|
-
system: options.systemPrompt,
|
|
84
|
+
messages: messages,
|
|
83
85
|
temperature: options.temperature,
|
|
84
86
|
maxTokens: options.maxTokens || DEFAULT_MAX_TOKENS,
|
|
85
87
|
tools,
|
|
@@ -6,6 +6,7 @@ import { createTimeoutController, TimeoutError, } from "../utils/timeout.js";
|
|
|
6
6
|
import { DEFAULT_MAX_TOKENS, DEFAULT_MAX_STEPS } from "../core/constants.js";
|
|
7
7
|
import { ModelConfigurationManager } from "../core/modelConfiguration.js";
|
|
8
8
|
import { validateApiKey, createVertexProjectConfig, createGoogleAuthConfig, } from "../utils/providerConfig.js";
|
|
9
|
+
import { buildMessagesArray } from "../utils/messageBuilder.js";
|
|
9
10
|
// Cache for anthropic module to avoid repeated imports
|
|
10
11
|
let _createVertexAnthropic = null;
|
|
11
12
|
let _anthropicImportAttempted = false;
|
|
@@ -189,6 +190,8 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
189
190
|
const timeoutController = createTimeoutController(timeout, this.providerName, "stream");
|
|
190
191
|
try {
|
|
191
192
|
this.validateStreamOptions(options);
|
|
193
|
+
// Build message array from options
|
|
194
|
+
const messages = buildMessagesArray(options);
|
|
192
195
|
logger.debug(`${functionTag}: Starting stream request`, {
|
|
193
196
|
modelName: this.modelName,
|
|
194
197
|
promptLength: options.input.text.length,
|
|
@@ -209,8 +212,7 @@ export class GoogleVertexProvider extends BaseProvider {
|
|
|
209
212
|
// Build complete stream options with proper typing
|
|
210
213
|
let streamOptions = {
|
|
211
214
|
model: model,
|
|
212
|
-
|
|
213
|
-
system: options.systemPrompt,
|
|
215
|
+
messages: messages,
|
|
214
216
|
temperature: options.temperature,
|
|
215
217
|
...(maxTokens && { maxTokens }),
|
|
216
218
|
tools,
|
|
@@ -5,6 +5,7 @@ import { logger } from "../utils/logger.js";
|
|
|
5
5
|
import { createTimeoutController, TimeoutError } from "../utils/timeout.js";
|
|
6
6
|
import { DEFAULT_MAX_TOKENS } from "../core/constants.js";
|
|
7
7
|
import { validateApiKey, createHuggingFaceConfig, getProviderModel, } from "../utils/providerConfig.js";
|
|
8
|
+
import { buildMessagesArray } from "../utils/messageBuilder.js";
|
|
8
9
|
// Configuration helpers - now using consolidated utility
|
|
9
10
|
const getHuggingFaceApiKey = () => {
|
|
10
11
|
return validateApiKey(createHuggingFaceConfig());
|
|
@@ -111,10 +112,11 @@ export class HuggingFaceProvider extends BaseProvider {
|
|
|
111
112
|
try {
|
|
112
113
|
// Enhanced tool handling for HuggingFace models
|
|
113
114
|
const streamOptions = this.prepareStreamOptions(options, analysisSchema);
|
|
115
|
+
// Build message array from options
|
|
116
|
+
const messages = buildMessagesArray(options);
|
|
114
117
|
const result = await streamText({
|
|
115
118
|
model: this.model,
|
|
116
|
-
|
|
117
|
-
system: streamOptions.system,
|
|
119
|
+
messages: messages,
|
|
118
120
|
temperature: options.temperature,
|
|
119
121
|
maxTokens: options.maxTokens || DEFAULT_MAX_TOKENS,
|
|
120
122
|
tools: streamOptions.tools, // Tools format conversion handled by prepareStreamOptions
|
|
@@ -6,6 +6,7 @@ import { createTimeoutController, TimeoutError, } from "../utils/timeout.js";
|
|
|
6
6
|
import { DEFAULT_MAX_TOKENS } from "../core/constants.js";
|
|
7
7
|
import { getProviderModel } from "../utils/providerConfig.js";
|
|
8
8
|
import { streamAnalyticsCollector } from "../core/streamAnalytics.js";
|
|
9
|
+
import { buildMessagesArray } from "../utils/messageBuilder.js";
|
|
9
10
|
// Configuration helpers
|
|
10
11
|
const getLiteLLMConfig = () => {
|
|
11
12
|
return {
|
|
@@ -118,10 +119,11 @@ export class LiteLLMProvider extends BaseProvider {
|
|
|
118
119
|
const timeout = this.getTimeout(options);
|
|
119
120
|
const timeoutController = createTimeoutController(timeout, this.providerName, "stream");
|
|
120
121
|
try {
|
|
122
|
+
// Build message array from options
|
|
123
|
+
const messages = buildMessagesArray(options);
|
|
121
124
|
const result = await streamText({
|
|
122
125
|
model: this.model,
|
|
123
|
-
|
|
124
|
-
system: options.systemPrompt,
|
|
126
|
+
messages: messages,
|
|
125
127
|
temperature: options.temperature,
|
|
126
128
|
maxTokens: options.maxTokens || DEFAULT_MAX_TOKENS,
|
|
127
129
|
tools: options.tools,
|
|
@@ -6,6 +6,7 @@ import { createTimeoutController, TimeoutError, } from "../utils/timeout.js";
|
|
|
6
6
|
import { DEFAULT_MAX_TOKENS, DEFAULT_MAX_STEPS } from "../core/constants.js";
|
|
7
7
|
import { validateApiKey, createMistralConfig, getProviderModel, } from "../utils/providerConfig.js";
|
|
8
8
|
import { streamAnalyticsCollector } from "../core/streamAnalytics.js";
|
|
9
|
+
import { buildMessagesArray } from "../utils/messageBuilder.js";
|
|
9
10
|
// Configuration helpers - now using consolidated utility
|
|
10
11
|
const getMistralApiKey = () => {
|
|
11
12
|
return validateApiKey(createMistralConfig());
|
|
@@ -46,10 +47,10 @@ export class MistralProvider extends BaseProvider {
|
|
|
46
47
|
// Get tools consistently with generate method
|
|
47
48
|
const shouldUseTools = !options.disableTools && this.supportsTools();
|
|
48
49
|
const tools = shouldUseTools ? await this.getAllTools() : {};
|
|
50
|
+
const messages = buildMessagesArray(options);
|
|
49
51
|
const result = await streamText({
|
|
50
52
|
model: this.model,
|
|
51
|
-
|
|
52
|
-
system: options.systemPrompt,
|
|
53
|
+
messages: messages,
|
|
53
54
|
temperature: options.temperature,
|
|
54
55
|
maxTokens: options.maxTokens || DEFAULT_MAX_TOKENS,
|
|
55
56
|
tools,
|
package/dist/providers/openAI.js
CHANGED
|
@@ -7,6 +7,7 @@ import { createTimeoutController, TimeoutError, } from "../utils/timeout.js";
|
|
|
7
7
|
import { DEFAULT_MAX_TOKENS, DEFAULT_MAX_STEPS } from "../core/constants.js";
|
|
8
8
|
import { validateApiKey, createOpenAIConfig, getProviderModel, } from "../utils/providerConfig.js";
|
|
9
9
|
import { streamAnalyticsCollector } from "../core/streamAnalytics.js";
|
|
10
|
+
import { buildMessagesArray } from "../utils/messageBuilder.js";
|
|
10
11
|
// Configuration helpers - now using consolidated utility
|
|
11
12
|
const getOpenAIApiKey = () => {
|
|
12
13
|
return validateApiKey(createOpenAIConfig());
|
|
@@ -77,10 +78,11 @@ export class OpenAIProvider extends BaseProvider {
|
|
|
77
78
|
// Get tools consistently with generate method
|
|
78
79
|
const shouldUseTools = !options.disableTools && this.supportsTools();
|
|
79
80
|
const tools = shouldUseTools ? await this.getAllTools() : {};
|
|
81
|
+
// Build message array from options
|
|
82
|
+
const messages = buildMessagesArray(options);
|
|
80
83
|
const result = await streamText({
|
|
81
84
|
model: this.model,
|
|
82
|
-
|
|
83
|
-
system: options.systemPrompt,
|
|
85
|
+
messages: messages,
|
|
84
86
|
temperature: options.temperature,
|
|
85
87
|
maxTokens: options.maxTokens || DEFAULT_MAX_TOKENS,
|
|
86
88
|
tools,
|