@juspay/neurolink 7.29.2 → 7.30.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/dist/cli/commands/config.d.ts +83 -83
- package/dist/cli/commands/mcp.js +39 -9
- package/dist/cli/commands/models.js +25 -21
- package/dist/cli/commands/ollama.js +2 -2
- package/dist/cli/factories/commandFactory.d.ts +8 -0
- package/dist/cli/factories/commandFactory.js +65 -65
- package/dist/cli/factories/ollamaCommandFactory.js +3 -1
- package/dist/cli/factories/sagemakerCommandFactory.js +3 -2
- package/dist/cli/index.d.ts +1 -1
- package/dist/cli/index.js +11 -11
- package/dist/cli/utils/envManager.js +5 -5
- package/dist/cli/utils/ollamaUtils.d.ts +12 -0
- package/dist/cli/utils/ollamaUtils.js +58 -42
- package/dist/config/configManager.js +5 -2
- package/dist/config/conversationMemoryConfig.js +5 -0
- package/dist/core/analytics.d.ts +2 -24
- package/dist/core/analytics.js +12 -17
- package/dist/core/baseProvider.d.ts +30 -1
- package/dist/core/baseProvider.js +180 -198
- package/dist/core/conversationMemoryManager.d.ts +9 -15
- package/dist/core/conversationMemoryManager.js +98 -57
- package/dist/core/dynamicModels.d.ts +4 -4
- package/dist/core/dynamicModels.js +7 -7
- package/dist/core/evaluation.d.ts +9 -9
- package/dist/core/evaluation.js +117 -65
- package/dist/core/evaluationProviders.d.ts +18 -2
- package/dist/core/evaluationProviders.js +15 -13
- package/dist/core/modelConfiguration.d.ts +63 -0
- package/dist/core/modelConfiguration.js +354 -290
- package/dist/core/streamAnalytics.d.ts +10 -5
- package/dist/core/streamAnalytics.js +10 -10
- package/dist/core/types.d.ts +22 -110
- package/dist/core/types.js +13 -0
- package/dist/factories/providerFactory.js +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/lib/config/configManager.js +5 -2
- package/dist/lib/config/conversationMemoryConfig.js +5 -0
- package/dist/lib/core/analytics.d.ts +2 -24
- package/dist/lib/core/analytics.js +12 -17
- package/dist/lib/core/baseProvider.d.ts +30 -1
- package/dist/lib/core/baseProvider.js +180 -198
- package/dist/lib/core/conversationMemoryManager.d.ts +9 -15
- package/dist/lib/core/conversationMemoryManager.js +98 -57
- package/dist/lib/core/dynamicModels.js +7 -7
- package/dist/lib/core/evaluation.d.ts +9 -9
- package/dist/lib/core/evaluation.js +117 -65
- package/dist/lib/core/evaluationProviders.d.ts +18 -2
- package/dist/lib/core/evaluationProviders.js +15 -13
- package/dist/lib/core/modelConfiguration.d.ts +63 -0
- package/dist/lib/core/modelConfiguration.js +354 -290
- package/dist/lib/core/streamAnalytics.d.ts +10 -5
- package/dist/lib/core/streamAnalytics.js +10 -10
- package/dist/lib/core/types.d.ts +22 -110
- package/dist/lib/core/types.js +13 -0
- package/dist/lib/factories/providerFactory.js +1 -1
- package/dist/lib/index.d.ts +2 -1
- package/dist/lib/mcp/externalServerManager.js +15 -6
- package/dist/lib/mcp/factory.js +1 -1
- package/dist/lib/mcp/index.d.ts +1 -1
- package/dist/lib/mcp/index.js +1 -1
- package/dist/lib/mcp/mcpCircuitBreaker.js +5 -1
- package/dist/lib/mcp/mcpClientFactory.js +3 -0
- package/dist/lib/mcp/registry.d.ts +3 -3
- package/dist/lib/mcp/registry.js +3 -3
- package/dist/lib/mcp/servers/aiProviders/aiAnalysisTools.js +5 -5
- package/dist/lib/mcp/servers/aiProviders/aiWorkflowTools.js +6 -6
- package/dist/lib/mcp/servers/utilities/utilityServer.js +1 -1
- package/dist/lib/mcp/toolDiscoveryService.js +8 -2
- package/dist/lib/mcp/toolRegistry.js +4 -4
- package/dist/lib/middleware/builtin/analytics.js +4 -4
- package/dist/lib/middleware/builtin/guardrails.js +2 -2
- package/dist/lib/middleware/registry.js +11 -2
- package/dist/lib/models/modelRegistry.d.ts +1 -1
- package/dist/lib/models/modelRegistry.js +3 -3
- package/dist/lib/models/modelResolver.d.ts +1 -1
- package/dist/lib/models/modelResolver.js +2 -2
- package/dist/lib/neurolink.d.ts +116 -9
- package/dist/lib/neurolink.js +718 -956
- package/dist/lib/providers/amazonSagemaker.d.ts +1 -1
- package/dist/lib/providers/amazonSagemaker.js +12 -3
- package/dist/lib/providers/anthropic.d.ts +1 -1
- package/dist/lib/providers/anthropic.js +7 -6
- package/dist/lib/providers/anthropicBaseProvider.d.ts +1 -1
- package/dist/lib/providers/anthropicBaseProvider.js +4 -3
- package/dist/lib/providers/azureOpenai.d.ts +1 -1
- package/dist/lib/providers/azureOpenai.js +1 -1
- package/dist/lib/providers/googleAiStudio.d.ts +1 -1
- package/dist/lib/providers/googleAiStudio.js +2 -2
- package/dist/lib/providers/googleVertex.d.ts +40 -0
- package/dist/lib/providers/googleVertex.js +330 -274
- package/dist/lib/providers/huggingFace.js +1 -1
- package/dist/lib/providers/mistral.d.ts +1 -1
- package/dist/lib/providers/mistral.js +2 -2
- package/dist/lib/providers/ollama.d.ts +4 -0
- package/dist/lib/providers/ollama.js +38 -18
- package/dist/lib/providers/openAI.d.ts +1 -1
- package/dist/lib/providers/openAI.js +2 -2
- package/dist/lib/providers/sagemaker/adaptive-semaphore.js +7 -4
- package/dist/lib/providers/sagemaker/client.js +13 -3
- package/dist/lib/providers/sagemaker/config.js +5 -1
- package/dist/lib/providers/sagemaker/detection.js +19 -9
- package/dist/lib/providers/sagemaker/errors.d.ts +8 -1
- package/dist/lib/providers/sagemaker/errors.js +103 -20
- package/dist/lib/providers/sagemaker/language-model.d.ts +3 -3
- package/dist/lib/providers/sagemaker/language-model.js +4 -4
- package/dist/lib/providers/sagemaker/parsers.js +14 -6
- package/dist/lib/providers/sagemaker/streaming.js +14 -3
- package/dist/lib/providers/sagemaker/types.d.ts +1 -1
- package/dist/lib/proxy/awsProxyIntegration.js +1 -1
- package/dist/lib/sdk/toolRegistration.d.ts +1 -1
- package/dist/lib/types/cli.d.ts +80 -8
- package/dist/lib/types/contextTypes.js +2 -2
- package/dist/lib/types/conversationTypes.d.ts +10 -0
- package/dist/lib/types/generateTypes.d.ts +2 -5
- package/dist/lib/types/providers.d.ts +81 -19
- package/dist/lib/types/providers.js +6 -6
- package/dist/lib/types/streamTypes.d.ts +4 -6
- package/dist/lib/types/typeAliases.d.ts +1 -1
- package/dist/lib/utils/analyticsUtils.d.ts +33 -0
- package/dist/lib/utils/analyticsUtils.js +76 -0
- package/dist/lib/utils/conversationMemoryUtils.d.ts +1 -2
- package/dist/lib/utils/conversationMemoryUtils.js +6 -7
- package/dist/lib/utils/errorHandling.js +4 -1
- package/dist/lib/utils/evaluationUtils.d.ts +27 -0
- package/dist/lib/utils/evaluationUtils.js +131 -0
- package/dist/lib/utils/optionsUtils.js +10 -1
- package/dist/lib/utils/performance.d.ts +1 -1
- package/dist/lib/utils/performance.js +15 -3
- package/dist/lib/utils/providerHealth.d.ts +48 -0
- package/dist/lib/utils/providerHealth.js +199 -254
- package/dist/lib/utils/providerUtils.js +2 -2
- package/dist/lib/utils/timeout.js +8 -3
- package/dist/mcp/externalServerManager.js +15 -6
- package/dist/mcp/factory.js +1 -1
- package/dist/mcp/index.d.ts +1 -1
- package/dist/mcp/index.js +1 -1
- package/dist/mcp/mcpCircuitBreaker.js +5 -1
- package/dist/mcp/mcpClientFactory.js +3 -0
- package/dist/mcp/registry.d.ts +3 -3
- package/dist/mcp/registry.js +3 -3
- package/dist/mcp/servers/aiProviders/aiAnalysisTools.js +5 -5
- package/dist/mcp/servers/aiProviders/aiWorkflowTools.js +6 -6
- package/dist/mcp/servers/utilities/utilityServer.js +1 -1
- package/dist/mcp/toolDiscoveryService.js +8 -2
- package/dist/mcp/toolRegistry.js +4 -4
- package/dist/middleware/builtin/analytics.js +4 -4
- package/dist/middleware/builtin/guardrails.js +2 -2
- package/dist/middleware/registry.js +11 -2
- package/dist/models/modelRegistry.d.ts +1 -1
- package/dist/models/modelRegistry.js +3 -3
- package/dist/models/modelResolver.d.ts +1 -1
- package/dist/models/modelResolver.js +2 -2
- package/dist/neurolink.d.ts +116 -9
- package/dist/neurolink.js +718 -956
- package/dist/providers/amazonSagemaker.d.ts +1 -1
- package/dist/providers/amazonSagemaker.js +12 -3
- package/dist/providers/anthropic.d.ts +1 -1
- package/dist/providers/anthropic.js +7 -6
- package/dist/providers/anthropicBaseProvider.d.ts +1 -1
- package/dist/providers/anthropicBaseProvider.js +4 -3
- package/dist/providers/azureOpenai.d.ts +1 -1
- package/dist/providers/azureOpenai.js +1 -1
- package/dist/providers/googleAiStudio.d.ts +1 -1
- package/dist/providers/googleAiStudio.js +2 -2
- package/dist/providers/googleVertex.d.ts +40 -0
- package/dist/providers/googleVertex.js +330 -274
- package/dist/providers/huggingFace.js +1 -1
- package/dist/providers/mistral.d.ts +1 -1
- package/dist/providers/mistral.js +2 -2
- package/dist/providers/ollama.d.ts +4 -0
- package/dist/providers/ollama.js +38 -18
- package/dist/providers/openAI.d.ts +1 -1
- package/dist/providers/openAI.js +2 -2
- package/dist/providers/sagemaker/adaptive-semaphore.js +7 -4
- package/dist/providers/sagemaker/client.js +13 -3
- package/dist/providers/sagemaker/config.js +5 -1
- package/dist/providers/sagemaker/detection.js +19 -9
- package/dist/providers/sagemaker/errors.d.ts +8 -1
- package/dist/providers/sagemaker/errors.js +103 -20
- package/dist/providers/sagemaker/language-model.d.ts +3 -3
- package/dist/providers/sagemaker/language-model.js +4 -4
- package/dist/providers/sagemaker/parsers.js +14 -6
- package/dist/providers/sagemaker/streaming.js +14 -3
- package/dist/providers/sagemaker/types.d.ts +1 -1
- package/dist/proxy/awsProxyIntegration.js +1 -1
- package/dist/sdk/toolRegistration.d.ts +1 -1
- package/dist/types/cli.d.ts +80 -8
- package/dist/types/contextTypes.js +2 -2
- package/dist/types/conversationTypes.d.ts +10 -0
- package/dist/types/generateTypes.d.ts +2 -5
- package/dist/types/providers.d.ts +81 -19
- package/dist/types/providers.js +6 -6
- package/dist/types/streamTypes.d.ts +4 -6
- package/dist/types/typeAliases.d.ts +1 -1
- package/dist/utils/analyticsUtils.d.ts +33 -0
- package/dist/utils/analyticsUtils.js +76 -0
- package/dist/utils/conversationMemoryUtils.d.ts +1 -2
- package/dist/utils/conversationMemoryUtils.js +6 -7
- package/dist/utils/errorHandling.js +4 -1
- package/dist/utils/evaluationUtils.d.ts +27 -0
- package/dist/utils/evaluationUtils.js +131 -0
- package/dist/utils/optionsUtils.js +10 -1
- package/dist/utils/performance.d.ts +1 -1
- package/dist/utils/performance.js +15 -3
- package/dist/utils/providerHealth.d.ts +48 -0
- package/dist/utils/providerHealth.js +199 -254
- package/dist/utils/providerUtils.js +2 -2
- package/dist/utils/timeout.js +8 -3
- package/package.json +1 -1
- package/dist/context/ContextManager.d.ts +0 -28
- package/dist/context/ContextManager.js +0 -113
- package/dist/context/config.d.ts +0 -5
- package/dist/context/config.js +0 -42
- package/dist/context/types.d.ts +0 -20
- package/dist/context/types.js +0 -1
- package/dist/context/utils.d.ts +0 -7
- package/dist/context/utils.js +0 -8
- package/dist/lib/context/ContextManager.d.ts +0 -28
- package/dist/lib/context/ContextManager.js +0 -113
- package/dist/lib/context/config.d.ts +0 -5
- package/dist/lib/context/config.js +0 -42
- package/dist/lib/context/types.d.ts +0 -20
- package/dist/lib/context/types.js +0 -1
- package/dist/lib/context/utils.d.ts +0 -7
- package/dist/lib/context/utils.js +0 -8
package/dist/context/config.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { formatHistoryToString } from "./utils.js";
|
|
2
|
-
/**
|
|
3
|
-
* Estimates the word count of a conversation history.
|
|
4
|
-
*/
|
|
5
|
-
function estimateWordCount(history) {
|
|
6
|
-
if (!history || history.length === 0) {
|
|
7
|
-
return 0;
|
|
8
|
-
}
|
|
9
|
-
return history.reduce((acc, msg) => acc +
|
|
10
|
-
(msg.content
|
|
11
|
-
.trim()
|
|
12
|
-
.split(/\s+/)
|
|
13
|
-
.filter((word) => word.length > 0).length || 0), 0);
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Generates the default prompt for summarization.
|
|
17
|
-
*/
|
|
18
|
-
function getDefaultSummarizationPrompt(history, wordLimit) {
|
|
19
|
-
const formattedHistory = formatHistoryToString(history);
|
|
20
|
-
return `
|
|
21
|
-
You are a context summarization AI. Your task is to condense the following conversation history for another AI assistant.
|
|
22
|
-
The summary must be a concise, third-person narrative that retains all critical information. Pay special attention to retaining key entities, technical details, decisions made, and any specific dates or times mentioned.
|
|
23
|
-
Ensure the summary flows logically and is ready to be used as context for the next turn in the conversation.
|
|
24
|
-
Please keep the summary under ${wordLimit} words.
|
|
25
|
-
|
|
26
|
-
Conversation History to Summarize:
|
|
27
|
-
---
|
|
28
|
-
${formattedHistory}
|
|
29
|
-
---
|
|
30
|
-
`.trim();
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Default configuration for the ContextManager.
|
|
34
|
-
*/
|
|
35
|
-
export const defaultContextConfig = {
|
|
36
|
-
highWaterMarkWords: 3000,
|
|
37
|
-
lowWaterMarkWords: 800,
|
|
38
|
-
summarizationModel: "gemini-2.5-flash",
|
|
39
|
-
summarizationProvider: "googlevertex",
|
|
40
|
-
getSummarizationPrompt: getDefaultSummarizationPrompt,
|
|
41
|
-
estimateWordCount: estimateWordCount,
|
|
42
|
-
};
|
package/dist/context/types.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export interface ChatMessage {
|
|
2
|
-
/** Role of the message sender */
|
|
3
|
-
role: "user" | "assistant" | "system";
|
|
4
|
-
/** Content of the message */
|
|
5
|
-
content: string;
|
|
6
|
-
/** Cached word count for performance */
|
|
7
|
-
wordCount?: number;
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* Defines the configuration for the ContextManager.
|
|
11
|
-
* This allows for easy customization of the summarization behavior.
|
|
12
|
-
*/
|
|
13
|
-
export interface ContextManagerConfig {
|
|
14
|
-
highWaterMarkWords: number;
|
|
15
|
-
lowWaterMarkWords: number;
|
|
16
|
-
summarizationModel: string;
|
|
17
|
-
summarizationProvider: string;
|
|
18
|
-
getSummarizationPrompt: (history: ChatMessage[], wordLimit: number) => string;
|
|
19
|
-
estimateWordCount: (history: ChatMessage[]) => number;
|
|
20
|
-
}
|
package/dist/context/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/context/utils.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { ChatMessage } from "./types.js";
|
|
2
|
-
/**
|
|
3
|
-
* Formats a chat history array into a single string for use in a prompt.
|
|
4
|
-
* @param history The array of ChatMessage objects.
|
|
5
|
-
* @returns A formatted string representing the conversation.
|
|
6
|
-
*/
|
|
7
|
-
export declare function formatHistoryToString(history: ChatMessage[]): string;
|
package/dist/context/utils.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Formats a chat history array into a single string for use in a prompt.
|
|
3
|
-
* @param history The array of ChatMessage objects.
|
|
4
|
-
* @returns A formatted string representing the conversation.
|
|
5
|
-
*/
|
|
6
|
-
export function formatHistoryToString(history) {
|
|
7
|
-
return history.map((msg) => `${msg.role}: ${msg.content}`).join("\n\n");
|
|
8
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { TextGenerationOptions, TextGenerationResult } from "../core/types.js";
|
|
2
|
-
import type { ContextManagerConfig } from "./types.js";
|
|
3
|
-
type InternalGenerator = (options: TextGenerationOptions) => Promise<TextGenerationResult>;
|
|
4
|
-
/**
|
|
5
|
-
* Manages conversation context, automatically summarizing it when it
|
|
6
|
-
* exceeds a specified word count limit.
|
|
7
|
-
*/
|
|
8
|
-
export declare class ContextManager {
|
|
9
|
-
private static readonly SUMMARIZATION_FAILED_WARNING;
|
|
10
|
-
private static readonly SUMMARIZATION_EMPTY_WARNING;
|
|
11
|
-
private history;
|
|
12
|
-
private wordCount;
|
|
13
|
-
private readonly internalGenerator;
|
|
14
|
-
private readonly config;
|
|
15
|
-
constructor(generatorFunction: InternalGenerator, config: ContextManagerConfig, initialContext?: string);
|
|
16
|
-
addTurn(role: "user" | "assistant", message: string): Promise<void>;
|
|
17
|
-
/**
|
|
18
|
-
* Formats the history including the latest user turn for the prompt, without modifying the permanent history.
|
|
19
|
-
*/
|
|
20
|
-
getContextForPrompt(role: "user", message: string): string;
|
|
21
|
-
getCurrentContext(): string;
|
|
22
|
-
private _summarize;
|
|
23
|
-
/**
|
|
24
|
-
* Truncates the history to a specific word count, preserving the most recent messages.
|
|
25
|
-
*/
|
|
26
|
-
private _truncateHistory;
|
|
27
|
-
}
|
|
28
|
-
export {};
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { logger } from "../utils/logger.js";
|
|
2
|
-
import { formatHistoryToString } from "./utils.js";
|
|
3
|
-
/**
|
|
4
|
-
* Manages conversation context, automatically summarizing it when it
|
|
5
|
-
* exceeds a specified word count limit.
|
|
6
|
-
*/
|
|
7
|
-
export class ContextManager {
|
|
8
|
-
static SUMMARIZATION_FAILED_WARNING = "[System Warning: Context summarization failed. Conversation history has been truncated.]";
|
|
9
|
-
static SUMMARIZATION_EMPTY_WARNING = "[System Warning: Context summarization failed to return valid content. Conversation history has been truncated.]";
|
|
10
|
-
history;
|
|
11
|
-
wordCount;
|
|
12
|
-
internalGenerator;
|
|
13
|
-
config;
|
|
14
|
-
constructor(generatorFunction, config, initialContext = "This is the start of the conversation.") {
|
|
15
|
-
this.internalGenerator = generatorFunction;
|
|
16
|
-
this.config = config;
|
|
17
|
-
const initialMessage = {
|
|
18
|
-
role: "system",
|
|
19
|
-
content: initialContext,
|
|
20
|
-
};
|
|
21
|
-
initialMessage.wordCount = this.config.estimateWordCount([initialMessage]);
|
|
22
|
-
this.history = [initialMessage];
|
|
23
|
-
this.wordCount = initialMessage.wordCount;
|
|
24
|
-
}
|
|
25
|
-
async addTurn(role, message) {
|
|
26
|
-
const newMessage = { role, content: message };
|
|
27
|
-
newMessage.wordCount = this.config.estimateWordCount([newMessage]);
|
|
28
|
-
this.history.push(newMessage);
|
|
29
|
-
this.wordCount += newMessage.wordCount;
|
|
30
|
-
logger.info(`[ContextManager] Current word count: ${this.wordCount} / ${this.config.highWaterMarkWords}`);
|
|
31
|
-
if (this.wordCount > this.config.highWaterMarkWords) {
|
|
32
|
-
await this._summarize();
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Formats the history including the latest user turn for the prompt, without modifying the permanent history.
|
|
37
|
-
*/
|
|
38
|
-
getContextForPrompt(role, message) {
|
|
39
|
-
const tempHistory = [...this.history, { role, content: message }];
|
|
40
|
-
return formatHistoryToString(tempHistory);
|
|
41
|
-
}
|
|
42
|
-
getCurrentContext() {
|
|
43
|
-
// Format the history into a single string for the provider prompt
|
|
44
|
-
return formatHistoryToString(this.history);
|
|
45
|
-
}
|
|
46
|
-
async _summarize() {
|
|
47
|
-
try {
|
|
48
|
-
const prompt = this.config.getSummarizationPrompt(this.history, this.config.lowWaterMarkWords);
|
|
49
|
-
// Construct options for the internal method, bypassing the main 'generate' entry point
|
|
50
|
-
const textOptions = {
|
|
51
|
-
prompt,
|
|
52
|
-
provider: this.config.summarizationProvider,
|
|
53
|
-
model: this.config.summarizationModel,
|
|
54
|
-
// Ensure summarization does not trigger more context management or tools
|
|
55
|
-
disableTools: true,
|
|
56
|
-
};
|
|
57
|
-
// Call the internal generation function directly to avoid recursion
|
|
58
|
-
const result = await this.internalGenerator(textOptions);
|
|
59
|
-
if (typeof result.content === "string" && result.content.length > 0) {
|
|
60
|
-
// Replace the history with a single system message containing the summary
|
|
61
|
-
const newHistory = [
|
|
62
|
-
{ role: "system", content: result.content },
|
|
63
|
-
];
|
|
64
|
-
this.history = newHistory;
|
|
65
|
-
this.wordCount = this.config.estimateWordCount(this.history);
|
|
66
|
-
logger.info(`[ContextManager] Summarization complete. New history length: ${this.wordCount} words.`);
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
logger.warn("[ContextManager] Summarization returned empty or non-string content; truncating history as a fallback.");
|
|
70
|
-
this._truncateHistory(this.config.lowWaterMarkWords);
|
|
71
|
-
this.history.unshift({
|
|
72
|
-
role: "system",
|
|
73
|
-
content: ContextManager.SUMMARIZATION_EMPTY_WARNING,
|
|
74
|
-
});
|
|
75
|
-
this.wordCount = this.config.estimateWordCount(this.history);
|
|
76
|
-
}
|
|
77
|
-
logger.debug(`[ContextManager] New history: ${JSON.stringify(this.history)}`);
|
|
78
|
-
}
|
|
79
|
-
catch (error) {
|
|
80
|
-
logger.error("Context summarization failed:", { error });
|
|
81
|
-
// Fallback strategy: truncate the history to the target word count.
|
|
82
|
-
this._truncateHistory(this.config.lowWaterMarkWords);
|
|
83
|
-
this.history.unshift({
|
|
84
|
-
role: "system",
|
|
85
|
-
content: ContextManager.SUMMARIZATION_FAILED_WARNING,
|
|
86
|
-
});
|
|
87
|
-
this.wordCount = this.config.estimateWordCount(this.history);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Truncates the history to a specific word count, preserving the most recent messages.
|
|
92
|
-
*/
|
|
93
|
-
_truncateHistory(wordLimit) {
|
|
94
|
-
if (this.wordCount <= wordLimit) {
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
let runningCount = 0;
|
|
98
|
-
let sliceIndex = this.history.length;
|
|
99
|
-
for (let i = this.history.length - 1; i >= 0; i--) {
|
|
100
|
-
let wordCount = this.history[i].wordCount;
|
|
101
|
-
if (wordCount === undefined) {
|
|
102
|
-
logger.warn(`[ContextManager] Word count cache missing for message at index ${i}. Recalculating.`);
|
|
103
|
-
wordCount = this.config.estimateWordCount([this.history[i]]);
|
|
104
|
-
}
|
|
105
|
-
runningCount += wordCount;
|
|
106
|
-
if (runningCount > wordLimit) {
|
|
107
|
-
sliceIndex = i + 1;
|
|
108
|
-
break;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
this.history = this.history.slice(sliceIndex);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { formatHistoryToString } from "./utils.js";
|
|
2
|
-
/**
|
|
3
|
-
* Estimates the word count of a conversation history.
|
|
4
|
-
*/
|
|
5
|
-
function estimateWordCount(history) {
|
|
6
|
-
if (!history || history.length === 0) {
|
|
7
|
-
return 0;
|
|
8
|
-
}
|
|
9
|
-
return history.reduce((acc, msg) => acc +
|
|
10
|
-
(msg.content
|
|
11
|
-
.trim()
|
|
12
|
-
.split(/\s+/)
|
|
13
|
-
.filter((word) => word.length > 0).length || 0), 0);
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Generates the default prompt for summarization.
|
|
17
|
-
*/
|
|
18
|
-
function getDefaultSummarizationPrompt(history, wordLimit) {
|
|
19
|
-
const formattedHistory = formatHistoryToString(history);
|
|
20
|
-
return `
|
|
21
|
-
You are a context summarization AI. Your task is to condense the following conversation history for another AI assistant.
|
|
22
|
-
The summary must be a concise, third-person narrative that retains all critical information. Pay special attention to retaining key entities, technical details, decisions made, and any specific dates or times mentioned.
|
|
23
|
-
Ensure the summary flows logically and is ready to be used as context for the next turn in the conversation.
|
|
24
|
-
Please keep the summary under ${wordLimit} words.
|
|
25
|
-
|
|
26
|
-
Conversation History to Summarize:
|
|
27
|
-
---
|
|
28
|
-
${formattedHistory}
|
|
29
|
-
---
|
|
30
|
-
`.trim();
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Default configuration for the ContextManager.
|
|
34
|
-
*/
|
|
35
|
-
export const defaultContextConfig = {
|
|
36
|
-
highWaterMarkWords: 3000,
|
|
37
|
-
lowWaterMarkWords: 800,
|
|
38
|
-
summarizationModel: "gemini-2.5-flash",
|
|
39
|
-
summarizationProvider: "googlevertex",
|
|
40
|
-
getSummarizationPrompt: getDefaultSummarizationPrompt,
|
|
41
|
-
estimateWordCount: estimateWordCount,
|
|
42
|
-
};
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export interface ChatMessage {
|
|
2
|
-
/** Role of the message sender */
|
|
3
|
-
role: "user" | "assistant" | "system";
|
|
4
|
-
/** Content of the message */
|
|
5
|
-
content: string;
|
|
6
|
-
/** Cached word count for performance */
|
|
7
|
-
wordCount?: number;
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* Defines the configuration for the ContextManager.
|
|
11
|
-
* This allows for easy customization of the summarization behavior.
|
|
12
|
-
*/
|
|
13
|
-
export interface ContextManagerConfig {
|
|
14
|
-
highWaterMarkWords: number;
|
|
15
|
-
lowWaterMarkWords: number;
|
|
16
|
-
summarizationModel: string;
|
|
17
|
-
summarizationProvider: string;
|
|
18
|
-
getSummarizationPrompt: (history: ChatMessage[], wordLimit: number) => string;
|
|
19
|
-
estimateWordCount: (history: ChatMessage[]) => number;
|
|
20
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { ChatMessage } from "./types.js";
|
|
2
|
-
/**
|
|
3
|
-
* Formats a chat history array into a single string for use in a prompt.
|
|
4
|
-
* @param history The array of ChatMessage objects.
|
|
5
|
-
* @returns A formatted string representing the conversation.
|
|
6
|
-
*/
|
|
7
|
-
export declare function formatHistoryToString(history: ChatMessage[]): string;
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Formats a chat history array into a single string for use in a prompt.
|
|
3
|
-
* @param history The array of ChatMessage objects.
|
|
4
|
-
* @returns A formatted string representing the conversation.
|
|
5
|
-
*/
|
|
6
|
-
export function formatHistoryToString(history) {
|
|
7
|
-
return history.map((msg) => `${msg.role}: ${msg.content}`).join("\n\n");
|
|
8
|
-
}
|