@librechat/agents 2.3.3 → 2.3.4
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/cjs/messages/prune.cjs +36 -16
- package/dist/cjs/messages/prune.cjs.map +1 -1
- package/dist/esm/messages/prune.mjs +36 -16
- package/dist/esm/messages/prune.mjs.map +1 -1
- package/package.json +1 -1
- package/src/messages/prune.ts +38 -16
- package/src/specs/token-distribution-edge-case.test.ts +296 -0
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var stream = require('@langchain/core/utils/stream');
|
|
4
3
|
var messages = require('@langchain/core/messages');
|
|
5
4
|
var _enum = require('../common/enum.cjs');
|
|
6
5
|
|
|
@@ -8,6 +7,16 @@ function isIndexInContext(arrayA, arrayB, targetIndex) {
|
|
|
8
7
|
const startingIndexInA = arrayA.length - arrayB.length;
|
|
9
8
|
return targetIndex >= startingIndexInA;
|
|
10
9
|
}
|
|
10
|
+
function addThinkingBlock(message, thinkingBlock) {
|
|
11
|
+
const content = Array.isArray(message.content)
|
|
12
|
+
? message.content
|
|
13
|
+
: [{
|
|
14
|
+
type: _enum.ContentTypes.TEXT,
|
|
15
|
+
text: message.content,
|
|
16
|
+
}];
|
|
17
|
+
content.unshift(thinkingBlock);
|
|
18
|
+
return content;
|
|
19
|
+
}
|
|
11
20
|
/**
|
|
12
21
|
* Calculates the total tokens from a single usage object
|
|
13
22
|
*
|
|
@@ -145,13 +154,7 @@ tokenCounter, }) {
|
|
|
145
154
|
thinkingStartIndex = originalLength - 1 - assistantIndex;
|
|
146
155
|
const thinkingTokenCount = tokenCounter(new messages.AIMessage({ content: [thinkingBlock] }));
|
|
147
156
|
const newRemainingCount = remainingContextTokens - thinkingTokenCount;
|
|
148
|
-
const content =
|
|
149
|
-
? context[assistantIndex].content
|
|
150
|
-
: [{
|
|
151
|
-
type: _enum.ContentTypes.TEXT,
|
|
152
|
-
text: context[assistantIndex].content,
|
|
153
|
-
}];
|
|
154
|
-
content.unshift(thinkingBlock);
|
|
157
|
+
const content = addThinkingBlock(context[assistantIndex], thinkingBlock);
|
|
155
158
|
context[assistantIndex].content = content;
|
|
156
159
|
if (newRemainingCount > 0) {
|
|
157
160
|
result.context = context.reverse();
|
|
@@ -192,10 +195,8 @@ tokenCounter, }) {
|
|
|
192
195
|
}
|
|
193
196
|
}
|
|
194
197
|
if (firstMessageType === 'ai') {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
tool_calls: stream.concat(firstMessage.tool_calls, thinkingMessage.tool_calls),
|
|
198
|
-
});
|
|
198
|
+
const content = addThinkingBlock(firstMessage, thinkingBlock);
|
|
199
|
+
newContext[newContext.length - 1].content = content;
|
|
199
200
|
}
|
|
200
201
|
else {
|
|
201
202
|
newContext.push(thinkingMessage);
|
|
@@ -213,6 +214,7 @@ function checkValidNumber(value) {
|
|
|
213
214
|
function createPruneMessages(factoryParams) {
|
|
214
215
|
const indexTokenCountMap = { ...factoryParams.indexTokenCountMap };
|
|
215
216
|
let lastTurnStartIndex = factoryParams.startIndex;
|
|
217
|
+
let lastCutOffIndex = 0;
|
|
216
218
|
let totalTokens = (Object.values(indexTokenCountMap)).reduce((a, b) => a + b, 0);
|
|
217
219
|
return function pruneMessages(params) {
|
|
218
220
|
let currentUsage;
|
|
@@ -235,15 +237,32 @@ function createPruneMessages(factoryParams) {
|
|
|
235
237
|
totalTokens += indexTokenCountMap[i];
|
|
236
238
|
}
|
|
237
239
|
}
|
|
238
|
-
// If `currentUsage` is defined, we need to distribute the current total
|
|
239
|
-
// for all message index keys before `lastTurnStartIndex`, as it has the most accurate count for those messages.
|
|
240
|
+
// If `currentUsage` is defined, we need to distribute the current total tokens to our `indexTokenCountMap`,
|
|
240
241
|
// We must distribute it in a weighted manner, so that the total token count is equal to `currentUsage.total_tokens`,
|
|
241
242
|
// relative the manually counted tokens in `indexTokenCountMap`.
|
|
243
|
+
// EDGE CASE: when the resulting context gets pruned, we should not distribute the usage for messages that are not in the context.
|
|
242
244
|
if (currentUsage) {
|
|
243
|
-
|
|
245
|
+
// Calculate the sum of tokens only for indices at or after lastCutOffIndex
|
|
246
|
+
const totalIndexTokens = Object.entries(indexTokenCountMap).reduce((sum, [key, value]) => {
|
|
247
|
+
// Convert string key to number and check if it's >= lastCutOffIndex
|
|
248
|
+
const numericKey = Number(key);
|
|
249
|
+
if (numericKey === 0 && params.messages[0].getType() === 'system') {
|
|
250
|
+
return sum + value;
|
|
251
|
+
}
|
|
252
|
+
return numericKey >= lastCutOffIndex ? sum + value : sum;
|
|
253
|
+
}, 0);
|
|
254
|
+
// Calculate ratio based only on messages that remain in the context
|
|
244
255
|
const ratio = currentUsage.total_tokens / totalIndexTokens;
|
|
256
|
+
// Apply the ratio adjustment only to messages at or after lastCutOffIndex
|
|
245
257
|
for (const key in indexTokenCountMap) {
|
|
246
|
-
|
|
258
|
+
const numericKey = Number(key);
|
|
259
|
+
if (numericKey === 0 && params.messages[0].getType() === 'system') {
|
|
260
|
+
indexTokenCountMap[key] = Math.round(indexTokenCountMap[key] * ratio);
|
|
261
|
+
}
|
|
262
|
+
else if (numericKey >= lastCutOffIndex) {
|
|
263
|
+
// Only adjust token counts for messages still in the context
|
|
264
|
+
indexTokenCountMap[key] = Math.round(indexTokenCountMap[key] * ratio);
|
|
265
|
+
}
|
|
247
266
|
}
|
|
248
267
|
}
|
|
249
268
|
lastTurnStartIndex = params.messages.length;
|
|
@@ -258,6 +277,7 @@ function createPruneMessages(factoryParams) {
|
|
|
258
277
|
thinkingEnabled: factoryParams.thinkingEnabled,
|
|
259
278
|
tokenCounter: factoryParams.tokenCounter,
|
|
260
279
|
});
|
|
280
|
+
lastCutOffIndex = Math.max(params.messages.length - context.length, 0);
|
|
261
281
|
return { context, indexTokenCountMap };
|
|
262
282
|
};
|
|
263
283
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prune.cjs","sources":["../../../src/messages/prune.ts"],"sourcesContent":["import { concat } from '@langchain/core/utils/stream';\nimport { AIMessage, BaseMessage, UsageMetadata } from '@langchain/core/messages';\nimport type { ThinkingContentText, MessageContentComplex } from '@/types/stream';\nimport type { TokenCounter } from '@/types/run';\nimport { ContentTypes } from '@/common';\nexport type PruneMessagesFactoryParams = {\n maxTokens: number;\n startIndex: number;\n tokenCounter: TokenCounter;\n indexTokenCountMap: Record<string, number>;\n thinkingEnabled?: boolean;\n};\nexport type PruneMessagesParams = {\n messages: BaseMessage[];\n usageMetadata?: Partial<UsageMetadata>;\n startType?: ReturnType<BaseMessage['getType']>;\n}\n\nfunction isIndexInContext(arrayA: BaseMessage[], arrayB: BaseMessage[], targetIndex: number): boolean {\n const startingIndexInA = arrayA.length - arrayB.length;\n return targetIndex >= startingIndexInA;\n}\n\n/**\n * Calculates the total tokens from a single usage object\n *\n * @param usage The usage metadata object containing token information\n * @returns An object containing the total input and output tokens\n */\nexport function calculateTotalTokens(usage: Partial<UsageMetadata>): UsageMetadata {\n const baseInputTokens = Number(usage.input_tokens) || 0;\n const cacheCreation = Number(usage.input_token_details?.cache_creation) || 0;\n const cacheRead = Number(usage.input_token_details?.cache_read) || 0;\n\n const totalInputTokens = baseInputTokens + cacheCreation + cacheRead;\n const totalOutputTokens = Number(usage.output_tokens) || 0;\n\n return {\n input_tokens: totalInputTokens,\n output_tokens: totalOutputTokens,\n total_tokens: totalInputTokens + totalOutputTokens\n };\n}\n\n/**\n * Processes an array of messages and returns a context of messages that fit within a specified token limit.\n * It iterates over the messages from newest to oldest, adding them to the context until the token limit is reached.\n *\n * @param options Configuration options for processing messages\n * @returns Object containing the message context, remaining tokens, messages not included, and summary index\n */\nexport function getMessagesWithinTokenLimit({\n messages: _messages,\n maxContextTokens,\n indexTokenCountMap,\n startType: _startType,\n thinkingEnabled,\n /** We may need to use this when recalculating */\n tokenCounter,\n}: {\n messages: BaseMessage[];\n maxContextTokens: number;\n indexTokenCountMap: Record<string, number | undefined>;\n tokenCounter: TokenCounter;\n startType?: string;\n thinkingEnabled?: boolean;\n}): {\n context: BaseMessage[];\n remainingContextTokens: number;\n messagesToRefine: BaseMessage[];\n} {\n // Every reply is primed with <|start|>assistant<|message|>, so we\n // start with 3 tokens for the label after all messages have been counted.\n let currentTokenCount = 3;\n const instructions = _messages[0]?.getType() === 'system' ? _messages[0] : undefined;\n const instructionsTokenCount = instructions != null ? indexTokenCountMap[0] ?? 0 : 0;\n const initialContextTokens = maxContextTokens - instructionsTokenCount;\n let remainingContextTokens = initialContextTokens;\n let startType = _startType;\n const originalLength = _messages.length;\n const messages = [..._messages];\n /**\n * IMPORTANT: this context array gets reversed at the end, since the latest messages get pushed first.\n *\n * This may be confusing to read, but it is done to ensure the context is in the correct order for the model.\n * */\n let context: BaseMessage[] = [];\n\n let thinkingStartIndex = -1;\n let thinkingEndIndex = -1;\n let thinkingBlock: ThinkingContentText | undefined;\n const endIndex = instructions != null ? 1 : 0;\n const prunedMemory: BaseMessage[] = [];\n\n if (currentTokenCount < remainingContextTokens) {\n let currentIndex = messages.length;\n while (messages.length > 0 && currentTokenCount < remainingContextTokens && currentIndex > endIndex) {\n currentIndex--;\n if (messages.length === 1 && instructions) {\n break;\n }\n const poppedMessage = messages.pop();\n if (!poppedMessage) continue;\n const messageType = poppedMessage.getType();\n if (thinkingEnabled === true && thinkingEndIndex === -1 && (currentIndex === (originalLength - 1)) && (messageType === 'ai' || messageType === 'tool')) {\n thinkingEndIndex = currentIndex;\n }\n if (thinkingEndIndex > -1 && !thinkingBlock && thinkingStartIndex < 0 && messageType === 'ai' && Array.isArray(poppedMessage.content)) {\n thinkingBlock = (poppedMessage.content.find((content) => content.type === ContentTypes.THINKING)) as ThinkingContentText | undefined;\n thinkingStartIndex = thinkingBlock != null ? currentIndex : -1;\n }\n /** False start, the latest message was not part of a multi-assistant/tool sequence of messages */\n if (\n thinkingEndIndex > -1\n && currentIndex === (thinkingEndIndex - 1)\n && (messageType !== 'ai' && messageType !== 'tool')\n ) {\n thinkingEndIndex = -1;\n }\n\n const tokenCount = indexTokenCountMap[currentIndex] ?? 0;\n\n if (prunedMemory.length === 0 && ((currentTokenCount + tokenCount) <= remainingContextTokens)) {\n context.push(poppedMessage);\n currentTokenCount += tokenCount;\n } else {\n prunedMemory.push(poppedMessage);\n if (thinkingEndIndex > -1) {\n continue;\n }\n break;\n }\n }\n\n if (thinkingEndIndex > -1 && context[context.length - 1].getType() === 'tool') {\n startType = 'ai';\n }\n\n if (startType != null && startType && context.length > 0) {\n const requiredTypeIndex = context.findIndex(msg => msg.getType() === startType);\n\n if (requiredTypeIndex > 0) {\n context = context.slice(requiredTypeIndex);\n }\n }\n }\n\n if (instructions && originalLength > 0) {\n context.push(_messages[0] as BaseMessage);\n messages.shift();\n }\n\n remainingContextTokens -= currentTokenCount;\n const result = {\n remainingContextTokens,\n context: [] as BaseMessage[],\n messagesToRefine: prunedMemory,\n };\n\n if (prunedMemory.length === 0 || thinkingEndIndex < 0 || (thinkingStartIndex > -1 && isIndexInContext(_messages, context, thinkingStartIndex))) {\n // we reverse at this step to ensure the context is in the correct order for the model, and we need to work backwards\n result.context = context.reverse();\n return result;\n }\n\n if (thinkingEndIndex > -1 && thinkingStartIndex < 0) {\n throw new Error('The payload is malformed. There is a thinking sequence but no \"AI\" messages with thinking blocks.');\n }\n\n if (!thinkingBlock) {\n throw new Error('The payload is malformed. There is a thinking sequence but no thinking block found.');\n }\n\n // Since we have a thinking sequence, we need to find the last assistant message\n // in the latest AI/tool sequence to add the thinking block that falls outside of the current context\n // Latest messages are ordered first.\n let assistantIndex = -1;\n for (let i = 0; i < context.length; i++) {\n const currentMessage = context[i];\n const type = currentMessage.getType();\n if (type === 'ai') {\n assistantIndex = i;\n }\n if (assistantIndex > -1 && (type === 'human' || type === 'system')) {\n break;\n }\n }\n\n if (assistantIndex === -1) {\n throw new Error('The payload is malformed. There is a thinking sequence but no \"AI\" messages to append thinking blocks to.');\n }\n\n thinkingStartIndex = originalLength - 1 - assistantIndex;\n const thinkingTokenCount = tokenCounter(new AIMessage({ content: [thinkingBlock] }));\n const newRemainingCount = remainingContextTokens - thinkingTokenCount;\n\n const content: MessageContentComplex[] = Array.isArray(context[assistantIndex].content)\n ? context[assistantIndex].content as MessageContentComplex[]\n : [{\n type: ContentTypes.TEXT,\n text: context[assistantIndex].content,\n }];\n content.unshift(thinkingBlock);\n context[assistantIndex].content = content;\n if (newRemainingCount > 0) {\n result.context = context.reverse();\n return result;\n }\n\n const thinkingMessage: AIMessage = context[assistantIndex];\n // now we need to an additional round of pruning but making the thinking block fit\n const newThinkingMessageTokenCount = (indexTokenCountMap[thinkingStartIndex] ?? 0) + thinkingTokenCount;\n remainingContextTokens = initialContextTokens - newThinkingMessageTokenCount;\n currentTokenCount = 3;\n let newContext: BaseMessage[] = [];\n const secondRoundMessages = [..._messages];\n let currentIndex = secondRoundMessages.length;\n while (secondRoundMessages.length > 0 && currentTokenCount < remainingContextTokens && currentIndex > thinkingStartIndex) {\n currentIndex--;\n const poppedMessage = secondRoundMessages.pop();\n if (!poppedMessage) continue;\n const tokenCount = indexTokenCountMap[currentIndex] ?? 0;\n if ((currentTokenCount + tokenCount) <= remainingContextTokens) {\n newContext.push(poppedMessage);\n currentTokenCount += tokenCount;\n } else {\n messages.push(poppedMessage);\n break;\n }\n }\n\n const firstMessage: AIMessage = newContext[newContext.length - 1];\n const firstMessageType = newContext[newContext.length - 1].getType();\n if (firstMessageType === 'tool') {\n startType = 'ai';\n }\n\n if (startType != null && startType && newContext.length > 0) {\n const requiredTypeIndex = newContext.findIndex(msg => msg.getType() === startType);\n if (requiredTypeIndex > 0) {\n newContext = newContext.slice(requiredTypeIndex);\n }\n }\n\n if (firstMessageType === 'ai') {\n newContext[newContext.length - 1] = new AIMessage({\n content: concat(thinkingMessage.content as MessageContentComplex[], newContext[newContext.length - 1].content as MessageContentComplex[]),\n tool_calls: concat(firstMessage.tool_calls, thinkingMessage.tool_calls),\n });\n } else {\n newContext.push(thinkingMessage);\n }\n\n if (instructions && originalLength > 0) {\n newContext.push(_messages[0] as BaseMessage);\n secondRoundMessages.shift();\n }\n\n result.context = newContext.reverse();\n return result;\n}\n\nexport function checkValidNumber(value: unknown): value is number {\n return typeof value === 'number' && !isNaN(value) && value > 0;\n}\n\nexport function createPruneMessages(factoryParams: PruneMessagesFactoryParams) {\n const indexTokenCountMap = { ...factoryParams.indexTokenCountMap };\n let lastTurnStartIndex = factoryParams.startIndex;\n let totalTokens = (Object.values(indexTokenCountMap)).reduce((a, b) => a + b, 0);\n return function pruneMessages(params: PruneMessagesParams): {\n context: BaseMessage[];\n indexTokenCountMap: Record<string, number>;\n } {\n let currentUsage: UsageMetadata | undefined;\n if (params.usageMetadata && (\n checkValidNumber(params.usageMetadata.input_tokens)\n || (\n checkValidNumber(params.usageMetadata.input_token_details)\n && (\n checkValidNumber(params.usageMetadata.input_token_details.cache_creation)\n || checkValidNumber(params.usageMetadata.input_token_details.cache_read)\n )\n )\n ) && checkValidNumber(params.usageMetadata.output_tokens)) {\n currentUsage = calculateTotalTokens(params.usageMetadata);\n totalTokens = currentUsage.total_tokens;\n }\n\n for (let i = lastTurnStartIndex; i < params.messages.length; i++) {\n const message = params.messages[i];\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (i === lastTurnStartIndex && indexTokenCountMap[i] === undefined && currentUsage) {\n indexTokenCountMap[i] = currentUsage.output_tokens;\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n } else if (indexTokenCountMap[i] === undefined) {\n indexTokenCountMap[i] = factoryParams.tokenCounter(message);\n totalTokens += indexTokenCountMap[i];\n }\n }\n\n // If `currentUsage` is defined, we need to distribute the current total tokensto our `indexTokenCountMap`,\n // for all message index keys before `lastTurnStartIndex`, as it has the most accurate count for those messages.\n // We must distribute it in a weighted manner, so that the total token count is equal to `currentUsage.total_tokens`,\n // relative the manually counted tokens in `indexTokenCountMap`.\n if (currentUsage) {\n const totalIndexTokens = Object.values(indexTokenCountMap).reduce((a, b) => a + b, 0);\n const ratio = currentUsage.total_tokens / totalIndexTokens;\n for (const key in indexTokenCountMap) {\n indexTokenCountMap[key] = Math.round(indexTokenCountMap[key] * ratio);\n }\n }\n\n lastTurnStartIndex = params.messages.length;\n if (totalTokens <= factoryParams.maxTokens) {\n return { context: params.messages, indexTokenCountMap };\n }\n\n const { context } = getMessagesWithinTokenLimit({\n maxContextTokens: factoryParams.maxTokens,\n messages: params.messages,\n indexTokenCountMap,\n startType: params.startType,\n thinkingEnabled: factoryParams.thinkingEnabled,\n tokenCounter: factoryParams.tokenCounter,\n });\n\n return { context, indexTokenCountMap };\n };\n}\n"],"names":["messages","ContentTypes","AIMessage","concat"],"mappings":";;;;;;AAkBA,SAAS,gBAAgB,CAAC,MAAqB,EAAE,MAAqB,EAAE,WAAmB,EAAA;IACzF,MAAM,gBAAgB,GAAG,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM;IACtD,OAAO,WAAW,IAAI,gBAAgB;AACxC;AAEA;;;;;AAKG;AACG,SAAU,oBAAoB,CAAC,KAA6B,EAAA;IAChE,MAAM,eAAe,GAAG,MAAM,CAAC,KAAK,CAAC,YAAY,CAAC,IAAI,CAAC;AACvD,IAAA,MAAM,aAAa,GAAG,MAAM,CAAC,KAAK,CAAC,mBAAmB,EAAE,cAAc,CAAC,IAAI,CAAC;AAC5E,IAAA,MAAM,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC,mBAAmB,EAAE,UAAU,CAAC,IAAI,CAAC;AAEpE,IAAA,MAAM,gBAAgB,GAAG,eAAe,GAAG,aAAa,GAAG,SAAS;IACpE,MAAM,iBAAiB,GAAG,MAAM,CAAC,KAAK,CAAC,aAAa,CAAC,IAAI,CAAC;IAE1D,OAAO;AACL,QAAA,YAAY,EAAE,gBAAgB;AAC9B,QAAA,aAAa,EAAE,iBAAiB;QAChC,YAAY,EAAE,gBAAgB,GAAG;KAClC;AACH;AAEA;;;;;;AAMG;SACa,2BAA2B,CAAC,EAC1C,QAAQ,EAAE,SAAS,EACnB,gBAAgB,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AC,GAAG,CAAC,EAAE,CAAC,CAAC;AACrF,YAAA,MAAM,KAAK,GAAG,YAAY,CAAC,YAAY,GAAG,gBAAgB;AAC1D,YAAA,KAAK,MAAM,GAAG,IAAI,kBAAkB,EAAE;AACpC,gBAAA,kBAAkB,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,kBAAkB,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;;;AAIzE,QAAA,kBAAkB,GAAG,MAAM,CAAC,QAAQ,CAAC,MAAM;AAC3C,QAAA,IAAI,WAAW,IAAI,aAAa,CAAC,SAAS,EAAE;YAC1C,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,QAAQ,EAAE,kBAAkB,EAAE;;AAGzD,QAAA,MAAM,EAAE,OAAO,EAAE,GAAG,2BAA2B,CAAC;YAC9C,gBAAgB,EAAE,aAAa,CAAC,SAAS;YACzC,QAAQ,EAAE,MAAM,CAAC,QAAQ;YACzB,kBAAkB;YAClB,SAAS,EAAE,MAAM,CAAC,SAAS;YAC3B,eAAe,EAAE,aAAa,CAAC,eAAe;YAC9C,YAAY,EAAE,aAAa,CAAC,YAAY;AACzC,SAAA,CAAC;AAEF,QAAA,OAAO,EAAE,OAAO,EAAE,kBAAkB,EAAE;AACxC,KAAC;AACH;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"prune.cjs","sources":["../../../src/messages/prune.ts"],"sourcesContent":["import { AIMessage, BaseMessage, UsageMetadata } from '@langchain/core/messages';\nimport type { ThinkingContentText, MessageContentComplex } from '@/types/stream';\nimport type { TokenCounter } from '@/types/run';\nimport { ContentTypes } from '@/common';\nexport type PruneMessagesFactoryParams = {\n maxTokens: number;\n startIndex: number;\n tokenCounter: TokenCounter;\n indexTokenCountMap: Record<string, number>;\n thinkingEnabled?: boolean;\n};\nexport type PruneMessagesParams = {\n messages: BaseMessage[];\n usageMetadata?: Partial<UsageMetadata>;\n startType?: ReturnType<BaseMessage['getType']>;\n}\n\nfunction isIndexInContext(arrayA: BaseMessage[], arrayB: BaseMessage[], targetIndex: number): boolean {\n const startingIndexInA = arrayA.length - arrayB.length;\n return targetIndex >= startingIndexInA;\n}\n\nfunction addThinkingBlock(message: AIMessage, thinkingBlock: ThinkingContentText): MessageContentComplex[] {\n const content: MessageContentComplex[] = Array.isArray(message.content)\n ? message.content as MessageContentComplex[]\n : [{\n type: ContentTypes.TEXT,\n text: message.content,\n }];\n content.unshift(thinkingBlock);\n return content;\n}\n\n/**\n * Calculates the total tokens from a single usage object\n *\n * @param usage The usage metadata object containing token information\n * @returns An object containing the total input and output tokens\n */\nexport function calculateTotalTokens(usage: Partial<UsageMetadata>): UsageMetadata {\n const baseInputTokens = Number(usage.input_tokens) || 0;\n const cacheCreation = Number(usage.input_token_details?.cache_creation) || 0;\n const cacheRead = Number(usage.input_token_details?.cache_read) || 0;\n\n const totalInputTokens = baseInputTokens + cacheCreation + cacheRead;\n const totalOutputTokens = Number(usage.output_tokens) || 0;\n\n return {\n input_tokens: totalInputTokens,\n output_tokens: totalOutputTokens,\n total_tokens: totalInputTokens + totalOutputTokens\n };\n}\n\n/**\n * Processes an array of messages and returns a context of messages that fit within a specified token limit.\n * It iterates over the messages from newest to oldest, adding them to the context until the token limit is reached.\n *\n * @param options Configuration options for processing messages\n * @returns Object containing the message context, remaining tokens, messages not included, and summary index\n */\nexport function getMessagesWithinTokenLimit({\n messages: _messages,\n maxContextTokens,\n indexTokenCountMap,\n startType: _startType,\n thinkingEnabled,\n /** We may need to use this when recalculating */\n tokenCounter,\n}: {\n messages: BaseMessage[];\n maxContextTokens: number;\n indexTokenCountMap: Record<string, number | undefined>;\n tokenCounter: TokenCounter;\n startType?: string;\n thinkingEnabled?: boolean;\n}): {\n context: BaseMessage[];\n remainingContextTokens: number;\n messagesToRefine: BaseMessage[];\n} {\n // Every reply is primed with <|start|>assistant<|message|>, so we\n // start with 3 tokens for the label after all messages have been counted.\n let currentTokenCount = 3;\n const instructions = _messages[0]?.getType() === 'system' ? _messages[0] : undefined;\n const instructionsTokenCount = instructions != null ? indexTokenCountMap[0] ?? 0 : 0;\n const initialContextTokens = maxContextTokens - instructionsTokenCount;\n let remainingContextTokens = initialContextTokens;\n let startType = _startType;\n const originalLength = _messages.length;\n const messages = [..._messages];\n /**\n * IMPORTANT: this context array gets reversed at the end, since the latest messages get pushed first.\n *\n * This may be confusing to read, but it is done to ensure the context is in the correct order for the model.\n * */\n let context: BaseMessage[] = [];\n\n let thinkingStartIndex = -1;\n let thinkingEndIndex = -1;\n let thinkingBlock: ThinkingContentText | undefined;\n const endIndex = instructions != null ? 1 : 0;\n const prunedMemory: BaseMessage[] = [];\n\n if (currentTokenCount < remainingContextTokens) {\n let currentIndex = messages.length;\n while (messages.length > 0 && currentTokenCount < remainingContextTokens && currentIndex > endIndex) {\n currentIndex--;\n if (messages.length === 1 && instructions) {\n break;\n }\n const poppedMessage = messages.pop();\n if (!poppedMessage) continue;\n const messageType = poppedMessage.getType();\n if (thinkingEnabled === true && thinkingEndIndex === -1 && (currentIndex === (originalLength - 1)) && (messageType === 'ai' || messageType === 'tool')) {\n thinkingEndIndex = currentIndex;\n }\n if (thinkingEndIndex > -1 && !thinkingBlock && thinkingStartIndex < 0 && messageType === 'ai' && Array.isArray(poppedMessage.content)) {\n thinkingBlock = (poppedMessage.content.find((content) => content.type === ContentTypes.THINKING)) as ThinkingContentText | undefined;\n thinkingStartIndex = thinkingBlock != null ? currentIndex : -1;\n }\n /** False start, the latest message was not part of a multi-assistant/tool sequence of messages */\n if (\n thinkingEndIndex > -1\n && currentIndex === (thinkingEndIndex - 1)\n && (messageType !== 'ai' && messageType !== 'tool')\n ) {\n thinkingEndIndex = -1;\n }\n\n const tokenCount = indexTokenCountMap[currentIndex] ?? 0;\n\n if (prunedMemory.length === 0 && ((currentTokenCount + tokenCount) <= remainingContextTokens)) {\n context.push(poppedMessage);\n currentTokenCount += tokenCount;\n } else {\n prunedMemory.push(poppedMessage);\n if (thinkingEndIndex > -1) {\n continue;\n }\n break;\n }\n }\n\n if (thinkingEndIndex > -1 && context[context.length - 1].getType() === 'tool') {\n startType = 'ai';\n }\n\n if (startType != null && startType && context.length > 0) {\n const requiredTypeIndex = context.findIndex(msg => msg.getType() === startType);\n\n if (requiredTypeIndex > 0) {\n context = context.slice(requiredTypeIndex);\n }\n }\n }\n\n if (instructions && originalLength > 0) {\n context.push(_messages[0] as BaseMessage);\n messages.shift();\n }\n\n remainingContextTokens -= currentTokenCount;\n const result = {\n remainingContextTokens,\n context: [] as BaseMessage[],\n messagesToRefine: prunedMemory,\n };\n\n if (prunedMemory.length === 0 || thinkingEndIndex < 0 || (thinkingStartIndex > -1 && isIndexInContext(_messages, context, thinkingStartIndex))) {\n // we reverse at this step to ensure the context is in the correct order for the model, and we need to work backwards\n result.context = context.reverse();\n return result;\n }\n\n if (thinkingEndIndex > -1 && thinkingStartIndex < 0) {\n throw new Error('The payload is malformed. There is a thinking sequence but no \"AI\" messages with thinking blocks.');\n }\n\n if (!thinkingBlock) {\n throw new Error('The payload is malformed. There is a thinking sequence but no thinking block found.');\n }\n\n // Since we have a thinking sequence, we need to find the last assistant message\n // in the latest AI/tool sequence to add the thinking block that falls outside of the current context\n // Latest messages are ordered first.\n let assistantIndex = -1;\n for (let i = 0; i < context.length; i++) {\n const currentMessage = context[i];\n const type = currentMessage.getType();\n if (type === 'ai') {\n assistantIndex = i;\n }\n if (assistantIndex > -1 && (type === 'human' || type === 'system')) {\n break;\n }\n }\n\n if (assistantIndex === -1) {\n throw new Error('The payload is malformed. There is a thinking sequence but no \"AI\" messages to append thinking blocks to.');\n }\n\n thinkingStartIndex = originalLength - 1 - assistantIndex;\n const thinkingTokenCount = tokenCounter(new AIMessage({ content: [thinkingBlock] }));\n const newRemainingCount = remainingContextTokens - thinkingTokenCount;\n\n const content: MessageContentComplex[] = addThinkingBlock(context[assistantIndex] as AIMessage, thinkingBlock);\n context[assistantIndex].content = content;\n if (newRemainingCount > 0) {\n result.context = context.reverse();\n return result;\n }\n\n const thinkingMessage: AIMessage = context[assistantIndex];\n // now we need to an additional round of pruning but making the thinking block fit\n const newThinkingMessageTokenCount = (indexTokenCountMap[thinkingStartIndex] ?? 0) + thinkingTokenCount;\n remainingContextTokens = initialContextTokens - newThinkingMessageTokenCount;\n currentTokenCount = 3;\n let newContext: BaseMessage[] = [];\n const secondRoundMessages = [..._messages];\n let currentIndex = secondRoundMessages.length;\n while (secondRoundMessages.length > 0 && currentTokenCount < remainingContextTokens && currentIndex > thinkingStartIndex) {\n currentIndex--;\n const poppedMessage = secondRoundMessages.pop();\n if (!poppedMessage) continue;\n const tokenCount = indexTokenCountMap[currentIndex] ?? 0;\n if ((currentTokenCount + tokenCount) <= remainingContextTokens) {\n newContext.push(poppedMessage);\n currentTokenCount += tokenCount;\n } else {\n messages.push(poppedMessage);\n break;\n }\n }\n\n const firstMessage: AIMessage = newContext[newContext.length - 1];\n const firstMessageType = newContext[newContext.length - 1].getType();\n if (firstMessageType === 'tool') {\n startType = 'ai';\n }\n\n if (startType != null && startType && newContext.length > 0) {\n const requiredTypeIndex = newContext.findIndex(msg => msg.getType() === startType);\n if (requiredTypeIndex > 0) {\n newContext = newContext.slice(requiredTypeIndex);\n }\n }\n\n if (firstMessageType === 'ai') {\n const content = addThinkingBlock(firstMessage, thinkingBlock);\n newContext[newContext.length - 1].content = content;\n } else {\n newContext.push(thinkingMessage);\n }\n\n if (instructions && originalLength > 0) {\n newContext.push(_messages[0] as BaseMessage);\n secondRoundMessages.shift();\n }\n\n result.context = newContext.reverse();\n return result;\n}\n\nexport function checkValidNumber(value: unknown): value is number {\n return typeof value === 'number' && !isNaN(value) && value > 0;\n}\n\nexport function createPruneMessages(factoryParams: PruneMessagesFactoryParams) {\n const indexTokenCountMap = { ...factoryParams.indexTokenCountMap };\n let lastTurnStartIndex = factoryParams.startIndex;\n let lastCutOffIndex = 0;\n let totalTokens = (Object.values(indexTokenCountMap)).reduce((a, b) => a + b, 0);\n return function pruneMessages(params: PruneMessagesParams): {\n context: BaseMessage[];\n indexTokenCountMap: Record<string, number>;\n } {\n let currentUsage: UsageMetadata | undefined;\n if (params.usageMetadata && (\n checkValidNumber(params.usageMetadata.input_tokens)\n || (\n checkValidNumber(params.usageMetadata.input_token_details)\n && (\n checkValidNumber(params.usageMetadata.input_token_details.cache_creation)\n || checkValidNumber(params.usageMetadata.input_token_details.cache_read)\n )\n )\n ) && checkValidNumber(params.usageMetadata.output_tokens)) {\n currentUsage = calculateTotalTokens(params.usageMetadata);\n totalTokens = currentUsage.total_tokens;\n }\n\n for (let i = lastTurnStartIndex; i < params.messages.length; i++) {\n const message = params.messages[i];\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (i === lastTurnStartIndex && indexTokenCountMap[i] === undefined && currentUsage) {\n indexTokenCountMap[i] = currentUsage.output_tokens;\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n } else if (indexTokenCountMap[i] === undefined) {\n indexTokenCountMap[i] = factoryParams.tokenCounter(message);\n totalTokens += indexTokenCountMap[i];\n }\n }\n\n // If `currentUsage` is defined, we need to distribute the current total tokens to our `indexTokenCountMap`,\n // We must distribute it in a weighted manner, so that the total token count is equal to `currentUsage.total_tokens`,\n // relative the manually counted tokens in `indexTokenCountMap`.\n // EDGE CASE: when the resulting context gets pruned, we should not distribute the usage for messages that are not in the context.\n if (currentUsage) {\n // Calculate the sum of tokens only for indices at or after lastCutOffIndex\n const totalIndexTokens = Object.entries(indexTokenCountMap).reduce((sum, [key, value]) => {\n // Convert string key to number and check if it's >= lastCutOffIndex\n const numericKey = Number(key);\n if (numericKey === 0 && params.messages[0].getType() === 'system') {\n return sum + value;\n }\n return numericKey >= lastCutOffIndex ? sum + value : sum;\n }, 0);\n\n // Calculate ratio based only on messages that remain in the context\n const ratio = currentUsage.total_tokens / totalIndexTokens;\n\n // Apply the ratio adjustment only to messages at or after lastCutOffIndex\n for (const key in indexTokenCountMap) {\n const numericKey = Number(key);\n if (numericKey === 0 && params.messages[0].getType() === 'system') {\n indexTokenCountMap[key] = Math.round(indexTokenCountMap[key] * ratio);\n } else if (numericKey >= lastCutOffIndex) {\n // Only adjust token counts for messages still in the context\n indexTokenCountMap[key] = Math.round(indexTokenCountMap[key] * ratio);\n }\n }\n }\n\n lastTurnStartIndex = params.messages.length;\n if (totalTokens <= factoryParams.maxTokens) {\n return { context: params.messages, indexTokenCountMap };\n }\n\n const { context } = getMessagesWithinTokenLimit({\n maxContextTokens: factoryParams.maxTokens,\n messages: params.messages,\n indexTokenCountMap,\n startType: params.startType,\n thinkingEnabled: factoryParams.thinkingEnabled,\n tokenCounter: factoryParams.tokenCounter,\n });\n lastCutOffIndex = Math.max(params.messages.length - context.length, 0);\n\n return { context, indexTokenCountMap };\n };\n}\n"],"names":["ContentTypes","messages","AIMessage"],"mappings":";;;;;AAiBA,SAAS,gBAAgB,CAAC,MAAqB,EAAE,MAAqB,EAAE,WAAmB,EAAA;IACzF,MAAM,gBAAgB,GAAG,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM;IACtD,OAAO,WAAW,IAAI,gBAAgB;AACxC;AAEA,SAAS,gBAAgB,CAAC,OAAkB,EAAE,aAAkC,EAAA;IAC9E,MAAM,OAAO,GAA4B,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,OAAO;UAClE,OAAO,CAAC;AACV,UAAE,CAAC;gBACD,IAAI,EAAEA,kBAAY,CAAC,IAAI;gBACvB,IAAI,EAAE,OAAO,CAAC,OAAO;AACtB,aAAA,CAAC;AACJ,IAAA,OAAO,CAAC,OAAO,CAAC,aAAa,CAAC;AAC9B,IAAA,OAAO,OAAO;AAChB;AAEA;;;;;AAKG;AACG,SAAU,oBAAoB,CAAC,KAA6B,EAAA;IAChE,MAAM,eAAe,GAAG,MAAM,CAAC,KAAK,CAAC,YAAY,CAAC,IAAI,CAAC;AACvD,IAAA,MAAM,aAAa,GAAG,MAAM,CAAC,KAAK,CAAC,mBAAmB,EAAE,cAAc,CAAC,IAAI,CAAC;AAC5E,IAAA,MAAM,SA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QAAQ,EAAE,MAAM,CAAC,QAAQ;YACzB,kBAAkB;YAClB,SAAS,EAAE,MAAM,CAAC,SAAS;YAC3B,eAAe,EAAE,aAAa,CAAC,eAAe;YAC9C,YAAY,EAAE,aAAa,CAAC,YAAY;AACzC,SAAA,CAAC;AACF,QAAA,eAAe,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;AAEtE,QAAA,OAAO,EAAE,OAAO,EAAE,kBAAkB,EAAE;AACxC,KAAC;AACH;;;;;;;"}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { concat } from '@langchain/core/utils/stream';
|
|
2
1
|
import { AIMessage } from '@langchain/core/messages';
|
|
3
2
|
import { ContentTypes } from '../common/enum.mjs';
|
|
4
3
|
|
|
@@ -6,6 +5,16 @@ function isIndexInContext(arrayA, arrayB, targetIndex) {
|
|
|
6
5
|
const startingIndexInA = arrayA.length - arrayB.length;
|
|
7
6
|
return targetIndex >= startingIndexInA;
|
|
8
7
|
}
|
|
8
|
+
function addThinkingBlock(message, thinkingBlock) {
|
|
9
|
+
const content = Array.isArray(message.content)
|
|
10
|
+
? message.content
|
|
11
|
+
: [{
|
|
12
|
+
type: ContentTypes.TEXT,
|
|
13
|
+
text: message.content,
|
|
14
|
+
}];
|
|
15
|
+
content.unshift(thinkingBlock);
|
|
16
|
+
return content;
|
|
17
|
+
}
|
|
9
18
|
/**
|
|
10
19
|
* Calculates the total tokens from a single usage object
|
|
11
20
|
*
|
|
@@ -143,13 +152,7 @@ tokenCounter, }) {
|
|
|
143
152
|
thinkingStartIndex = originalLength - 1 - assistantIndex;
|
|
144
153
|
const thinkingTokenCount = tokenCounter(new AIMessage({ content: [thinkingBlock] }));
|
|
145
154
|
const newRemainingCount = remainingContextTokens - thinkingTokenCount;
|
|
146
|
-
const content =
|
|
147
|
-
? context[assistantIndex].content
|
|
148
|
-
: [{
|
|
149
|
-
type: ContentTypes.TEXT,
|
|
150
|
-
text: context[assistantIndex].content,
|
|
151
|
-
}];
|
|
152
|
-
content.unshift(thinkingBlock);
|
|
155
|
+
const content = addThinkingBlock(context[assistantIndex], thinkingBlock);
|
|
153
156
|
context[assistantIndex].content = content;
|
|
154
157
|
if (newRemainingCount > 0) {
|
|
155
158
|
result.context = context.reverse();
|
|
@@ -190,10 +193,8 @@ tokenCounter, }) {
|
|
|
190
193
|
}
|
|
191
194
|
}
|
|
192
195
|
if (firstMessageType === 'ai') {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
tool_calls: concat(firstMessage.tool_calls, thinkingMessage.tool_calls),
|
|
196
|
-
});
|
|
196
|
+
const content = addThinkingBlock(firstMessage, thinkingBlock);
|
|
197
|
+
newContext[newContext.length - 1].content = content;
|
|
197
198
|
}
|
|
198
199
|
else {
|
|
199
200
|
newContext.push(thinkingMessage);
|
|
@@ -211,6 +212,7 @@ function checkValidNumber(value) {
|
|
|
211
212
|
function createPruneMessages(factoryParams) {
|
|
212
213
|
const indexTokenCountMap = { ...factoryParams.indexTokenCountMap };
|
|
213
214
|
let lastTurnStartIndex = factoryParams.startIndex;
|
|
215
|
+
let lastCutOffIndex = 0;
|
|
214
216
|
let totalTokens = (Object.values(indexTokenCountMap)).reduce((a, b) => a + b, 0);
|
|
215
217
|
return function pruneMessages(params) {
|
|
216
218
|
let currentUsage;
|
|
@@ -233,15 +235,32 @@ function createPruneMessages(factoryParams) {
|
|
|
233
235
|
totalTokens += indexTokenCountMap[i];
|
|
234
236
|
}
|
|
235
237
|
}
|
|
236
|
-
// If `currentUsage` is defined, we need to distribute the current total
|
|
237
|
-
// for all message index keys before `lastTurnStartIndex`, as it has the most accurate count for those messages.
|
|
238
|
+
// If `currentUsage` is defined, we need to distribute the current total tokens to our `indexTokenCountMap`,
|
|
238
239
|
// We must distribute it in a weighted manner, so that the total token count is equal to `currentUsage.total_tokens`,
|
|
239
240
|
// relative the manually counted tokens in `indexTokenCountMap`.
|
|
241
|
+
// EDGE CASE: when the resulting context gets pruned, we should not distribute the usage for messages that are not in the context.
|
|
240
242
|
if (currentUsage) {
|
|
241
|
-
|
|
243
|
+
// Calculate the sum of tokens only for indices at or after lastCutOffIndex
|
|
244
|
+
const totalIndexTokens = Object.entries(indexTokenCountMap).reduce((sum, [key, value]) => {
|
|
245
|
+
// Convert string key to number and check if it's >= lastCutOffIndex
|
|
246
|
+
const numericKey = Number(key);
|
|
247
|
+
if (numericKey === 0 && params.messages[0].getType() === 'system') {
|
|
248
|
+
return sum + value;
|
|
249
|
+
}
|
|
250
|
+
return numericKey >= lastCutOffIndex ? sum + value : sum;
|
|
251
|
+
}, 0);
|
|
252
|
+
// Calculate ratio based only on messages that remain in the context
|
|
242
253
|
const ratio = currentUsage.total_tokens / totalIndexTokens;
|
|
254
|
+
// Apply the ratio adjustment only to messages at or after lastCutOffIndex
|
|
243
255
|
for (const key in indexTokenCountMap) {
|
|
244
|
-
|
|
256
|
+
const numericKey = Number(key);
|
|
257
|
+
if (numericKey === 0 && params.messages[0].getType() === 'system') {
|
|
258
|
+
indexTokenCountMap[key] = Math.round(indexTokenCountMap[key] * ratio);
|
|
259
|
+
}
|
|
260
|
+
else if (numericKey >= lastCutOffIndex) {
|
|
261
|
+
// Only adjust token counts for messages still in the context
|
|
262
|
+
indexTokenCountMap[key] = Math.round(indexTokenCountMap[key] * ratio);
|
|
263
|
+
}
|
|
245
264
|
}
|
|
246
265
|
}
|
|
247
266
|
lastTurnStartIndex = params.messages.length;
|
|
@@ -256,6 +275,7 @@ function createPruneMessages(factoryParams) {
|
|
|
256
275
|
thinkingEnabled: factoryParams.thinkingEnabled,
|
|
257
276
|
tokenCounter: factoryParams.tokenCounter,
|
|
258
277
|
});
|
|
278
|
+
lastCutOffIndex = Math.max(params.messages.length - context.length, 0);
|
|
259
279
|
return { context, indexTokenCountMap };
|
|
260
280
|
};
|
|
261
281
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prune.mjs","sources":["../../../src/messages/prune.ts"],"sourcesContent":["import { concat } from '@langchain/core/utils/stream';\nimport { AIMessage, BaseMessage, UsageMetadata } from '@langchain/core/messages';\nimport type { ThinkingContentText, MessageContentComplex } from '@/types/stream';\nimport type { TokenCounter } from '@/types/run';\nimport { ContentTypes } from '@/common';\nexport type PruneMessagesFactoryParams = {\n maxTokens: number;\n startIndex: number;\n tokenCounter: TokenCounter;\n indexTokenCountMap: Record<string, number>;\n thinkingEnabled?: boolean;\n};\nexport type PruneMessagesParams = {\n messages: BaseMessage[];\n usageMetadata?: Partial<UsageMetadata>;\n startType?: ReturnType<BaseMessage['getType']>;\n}\n\nfunction isIndexInContext(arrayA: BaseMessage[], arrayB: BaseMessage[], targetIndex: number): boolean {\n const startingIndexInA = arrayA.length - arrayB.length;\n return targetIndex >= startingIndexInA;\n}\n\n/**\n * Calculates the total tokens from a single usage object\n *\n * @param usage The usage metadata object containing token information\n * @returns An object containing the total input and output tokens\n */\nexport function calculateTotalTokens(usage: Partial<UsageMetadata>): UsageMetadata {\n const baseInputTokens = Number(usage.input_tokens) || 0;\n const cacheCreation = Number(usage.input_token_details?.cache_creation) || 0;\n const cacheRead = Number(usage.input_token_details?.cache_read) || 0;\n\n const totalInputTokens = baseInputTokens + cacheCreation + cacheRead;\n const totalOutputTokens = Number(usage.output_tokens) || 0;\n\n return {\n input_tokens: totalInputTokens,\n output_tokens: totalOutputTokens,\n total_tokens: totalInputTokens + totalOutputTokens\n };\n}\n\n/**\n * Processes an array of messages and returns a context of messages that fit within a specified token limit.\n * It iterates over the messages from newest to oldest, adding them to the context until the token limit is reached.\n *\n * @param options Configuration options for processing messages\n * @returns Object containing the message context, remaining tokens, messages not included, and summary index\n */\nexport function getMessagesWithinTokenLimit({\n messages: _messages,\n maxContextTokens,\n indexTokenCountMap,\n startType: _startType,\n thinkingEnabled,\n /** We may need to use this when recalculating */\n tokenCounter,\n}: {\n messages: BaseMessage[];\n maxContextTokens: number;\n indexTokenCountMap: Record<string, number | undefined>;\n tokenCounter: TokenCounter;\n startType?: string;\n thinkingEnabled?: boolean;\n}): {\n context: BaseMessage[];\n remainingContextTokens: number;\n messagesToRefine: BaseMessage[];\n} {\n // Every reply is primed with <|start|>assistant<|message|>, so we\n // start with 3 tokens for the label after all messages have been counted.\n let currentTokenCount = 3;\n const instructions = _messages[0]?.getType() === 'system' ? _messages[0] : undefined;\n const instructionsTokenCount = instructions != null ? indexTokenCountMap[0] ?? 0 : 0;\n const initialContextTokens = maxContextTokens - instructionsTokenCount;\n let remainingContextTokens = initialContextTokens;\n let startType = _startType;\n const originalLength = _messages.length;\n const messages = [..._messages];\n /**\n * IMPORTANT: this context array gets reversed at the end, since the latest messages get pushed first.\n *\n * This may be confusing to read, but it is done to ensure the context is in the correct order for the model.\n * */\n let context: BaseMessage[] = [];\n\n let thinkingStartIndex = -1;\n let thinkingEndIndex = -1;\n let thinkingBlock: ThinkingContentText | undefined;\n const endIndex = instructions != null ? 1 : 0;\n const prunedMemory: BaseMessage[] = [];\n\n if (currentTokenCount < remainingContextTokens) {\n let currentIndex = messages.length;\n while (messages.length > 0 && currentTokenCount < remainingContextTokens && currentIndex > endIndex) {\n currentIndex--;\n if (messages.length === 1 && instructions) {\n break;\n }\n const poppedMessage = messages.pop();\n if (!poppedMessage) continue;\n const messageType = poppedMessage.getType();\n if (thinkingEnabled === true && thinkingEndIndex === -1 && (currentIndex === (originalLength - 1)) && (messageType === 'ai' || messageType === 'tool')) {\n thinkingEndIndex = currentIndex;\n }\n if (thinkingEndIndex > -1 && !thinkingBlock && thinkingStartIndex < 0 && messageType === 'ai' && Array.isArray(poppedMessage.content)) {\n thinkingBlock = (poppedMessage.content.find((content) => content.type === ContentTypes.THINKING)) as ThinkingContentText | undefined;\n thinkingStartIndex = thinkingBlock != null ? currentIndex : -1;\n }\n /** False start, the latest message was not part of a multi-assistant/tool sequence of messages */\n if (\n thinkingEndIndex > -1\n && currentIndex === (thinkingEndIndex - 1)\n && (messageType !== 'ai' && messageType !== 'tool')\n ) {\n thinkingEndIndex = -1;\n }\n\n const tokenCount = indexTokenCountMap[currentIndex] ?? 0;\n\n if (prunedMemory.length === 0 && ((currentTokenCount + tokenCount) <= remainingContextTokens)) {\n context.push(poppedMessage);\n currentTokenCount += tokenCount;\n } else {\n prunedMemory.push(poppedMessage);\n if (thinkingEndIndex > -1) {\n continue;\n }\n break;\n }\n }\n\n if (thinkingEndIndex > -1 && context[context.length - 1].getType() === 'tool') {\n startType = 'ai';\n }\n\n if (startType != null && startType && context.length > 0) {\n const requiredTypeIndex = context.findIndex(msg => msg.getType() === startType);\n\n if (requiredTypeIndex > 0) {\n context = context.slice(requiredTypeIndex);\n }\n }\n }\n\n if (instructions && originalLength > 0) {\n context.push(_messages[0] as BaseMessage);\n messages.shift();\n }\n\n remainingContextTokens -= currentTokenCount;\n const result = {\n remainingContextTokens,\n context: [] as BaseMessage[],\n messagesToRefine: prunedMemory,\n };\n\n if (prunedMemory.length === 0 || thinkingEndIndex < 0 || (thinkingStartIndex > -1 && isIndexInContext(_messages, context, thinkingStartIndex))) {\n // we reverse at this step to ensure the context is in the correct order for the model, and we need to work backwards\n result.context = context.reverse();\n return result;\n }\n\n if (thinkingEndIndex > -1 && thinkingStartIndex < 0) {\n throw new Error('The payload is malformed. There is a thinking sequence but no \"AI\" messages with thinking blocks.');\n }\n\n if (!thinkingBlock) {\n throw new Error('The payload is malformed. There is a thinking sequence but no thinking block found.');\n }\n\n // Since we have a thinking sequence, we need to find the last assistant message\n // in the latest AI/tool sequence to add the thinking block that falls outside of the current context\n // Latest messages are ordered first.\n let assistantIndex = -1;\n for (let i = 0; i < context.length; i++) {\n const currentMessage = context[i];\n const type = currentMessage.getType();\n if (type === 'ai') {\n assistantIndex = i;\n }\n if (assistantIndex > -1 && (type === 'human' || type === 'system')) {\n break;\n }\n }\n\n if (assistantIndex === -1) {\n throw new Error('The payload is malformed. There is a thinking sequence but no \"AI\" messages to append thinking blocks to.');\n }\n\n thinkingStartIndex = originalLength - 1 - assistantIndex;\n const thinkingTokenCount = tokenCounter(new AIMessage({ content: [thinkingBlock] }));\n const newRemainingCount = remainingContextTokens - thinkingTokenCount;\n\n const content: MessageContentComplex[] = Array.isArray(context[assistantIndex].content)\n ? context[assistantIndex].content as MessageContentComplex[]\n : [{\n type: ContentTypes.TEXT,\n text: context[assistantIndex].content,\n }];\n content.unshift(thinkingBlock);\n context[assistantIndex].content = content;\n if (newRemainingCount > 0) {\n result.context = context.reverse();\n return result;\n }\n\n const thinkingMessage: AIMessage = context[assistantIndex];\n // now we need to an additional round of pruning but making the thinking block fit\n const newThinkingMessageTokenCount = (indexTokenCountMap[thinkingStartIndex] ?? 0) + thinkingTokenCount;\n remainingContextTokens = initialContextTokens - newThinkingMessageTokenCount;\n currentTokenCount = 3;\n let newContext: BaseMessage[] = [];\n const secondRoundMessages = [..._messages];\n let currentIndex = secondRoundMessages.length;\n while (secondRoundMessages.length > 0 && currentTokenCount < remainingContextTokens && currentIndex > thinkingStartIndex) {\n currentIndex--;\n const poppedMessage = secondRoundMessages.pop();\n if (!poppedMessage) continue;\n const tokenCount = indexTokenCountMap[currentIndex] ?? 0;\n if ((currentTokenCount + tokenCount) <= remainingContextTokens) {\n newContext.push(poppedMessage);\n currentTokenCount += tokenCount;\n } else {\n messages.push(poppedMessage);\n break;\n }\n }\n\n const firstMessage: AIMessage = newContext[newContext.length - 1];\n const firstMessageType = newContext[newContext.length - 1].getType();\n if (firstMessageType === 'tool') {\n startType = 'ai';\n }\n\n if (startType != null && startType && newContext.length > 0) {\n const requiredTypeIndex = newContext.findIndex(msg => msg.getType() === startType);\n if (requiredTypeIndex > 0) {\n newContext = newContext.slice(requiredTypeIndex);\n }\n }\n\n if (firstMessageType === 'ai') {\n newContext[newContext.length - 1] = new AIMessage({\n content: concat(thinkingMessage.content as MessageContentComplex[], newContext[newContext.length - 1].content as MessageContentComplex[]),\n tool_calls: concat(firstMessage.tool_calls, thinkingMessage.tool_calls),\n });\n } else {\n newContext.push(thinkingMessage);\n }\n\n if (instructions && originalLength > 0) {\n newContext.push(_messages[0] as BaseMessage);\n secondRoundMessages.shift();\n }\n\n result.context = newContext.reverse();\n return result;\n}\n\nexport function checkValidNumber(value: unknown): value is number {\n return typeof value === 'number' && !isNaN(value) && value > 0;\n}\n\nexport function createPruneMessages(factoryParams: PruneMessagesFactoryParams) {\n const indexTokenCountMap = { ...factoryParams.indexTokenCountMap };\n let lastTurnStartIndex = factoryParams.startIndex;\n let totalTokens = (Object.values(indexTokenCountMap)).reduce((a, b) => a + b, 0);\n return function pruneMessages(params: PruneMessagesParams): {\n context: BaseMessage[];\n indexTokenCountMap: Record<string, number>;\n } {\n let currentUsage: UsageMetadata | undefined;\n if (params.usageMetadata && (\n checkValidNumber(params.usageMetadata.input_tokens)\n || (\n checkValidNumber(params.usageMetadata.input_token_details)\n && (\n checkValidNumber(params.usageMetadata.input_token_details.cache_creation)\n || checkValidNumber(params.usageMetadata.input_token_details.cache_read)\n )\n )\n ) && checkValidNumber(params.usageMetadata.output_tokens)) {\n currentUsage = calculateTotalTokens(params.usageMetadata);\n totalTokens = currentUsage.total_tokens;\n }\n\n for (let i = lastTurnStartIndex; i < params.messages.length; i++) {\n const message = params.messages[i];\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (i === lastTurnStartIndex && indexTokenCountMap[i] === undefined && currentUsage) {\n indexTokenCountMap[i] = currentUsage.output_tokens;\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n } else if (indexTokenCountMap[i] === undefined) {\n indexTokenCountMap[i] = factoryParams.tokenCounter(message);\n totalTokens += indexTokenCountMap[i];\n }\n }\n\n // If `currentUsage` is defined, we need to distribute the current total tokensto our `indexTokenCountMap`,\n // for all message index keys before `lastTurnStartIndex`, as it has the most accurate count for those messages.\n // We must distribute it in a weighted manner, so that the total token count is equal to `currentUsage.total_tokens`,\n // relative the manually counted tokens in `indexTokenCountMap`.\n if (currentUsage) {\n const totalIndexTokens = Object.values(indexTokenCountMap).reduce((a, b) => a + b, 0);\n const ratio = currentUsage.total_tokens / totalIndexTokens;\n for (const key in indexTokenCountMap) {\n indexTokenCountMap[key] = Math.round(indexTokenCountMap[key] * ratio);\n }\n }\n\n lastTurnStartIndex = params.messages.length;\n if (totalTokens <= factoryParams.maxTokens) {\n return { context: params.messages, indexTokenCountMap };\n }\n\n const { context } = getMessagesWithinTokenLimit({\n maxContextTokens: factoryParams.maxTokens,\n messages: params.messages,\n indexTokenCountMap,\n startType: params.startType,\n thinkingEnabled: factoryParams.thinkingEnabled,\n tokenCounter: factoryParams.tokenCounter,\n });\n\n return { context, indexTokenCountMap };\n };\n}\n"],"names":[],"mappings":";;;;AAkBA,SAAS,gBAAgB,CAAC,MAAqB,EAAE,MAAqB,EAAE,WAAmB,EAAA;IACzF,MAAM,gBAAgB,GAAG,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM;IACtD,OAAO,WAAW,IAAI,gBAAgB;AACxC;AAEA;;;;;AAKG;AACG,SAAU,oBAAoB,CAAC,KAA6B,EAAA;IAChE,MAAM,eAAe,GAAG,MAAM,CAAC,KAAK,CAAC,YAAY,CAAC,IAAI,CAAC;AACvD,IAAA,MAAM,aAAa,GAAG,MAAM,CAAC,KAAK,CAAC,mBAAmB,EAAE,cAAc,CAAC,IAAI,CAAC;AAC5E,IAAA,MAAM,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC,mBAAmB,EAAE,UAAU,CAAC,IAAI,CAAC;AAEpE,IAAA,MAAM,gBAAgB,GAAG,eAAe,GAAG,aAAa,GAAG,SAAS;IACpE,MAAM,iBAAiB,GAAG,MAAM,CAAC,KAAK,CAAC,aAAa,CAAC,IAAI,CAAC;IAE1D,OAAO;AACL,QAAA,YAAY,EAAE,gBAAgB;AAC9B,QAAA,aAAa,EAAE,iBAAiB;QAChC,YAAY,EAAE,gBAAgB,GAAG;KAClC;AACH;AAEA;;;;;;AAMG;SACa,2BAA2B,CAAC,EAC1C,QAAQ,EAAE,SAAS,EACn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,CAAC,EAAE,CAAC,CAAC;AACrF,YAAA,MAAM,KAAK,GAAG,YAAY,CAAC,YAAY,GAAG,gBAAgB;AAC1D,YAAA,KAAK,MAAM,GAAG,IAAI,kBAAkB,EAAE;AACpC,gBAAA,kBAAkB,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,kBAAkB,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;;;AAIzE,QAAA,kBAAkB,GAAG,MAAM,CAAC,QAAQ,CAAC,MAAM;AAC3C,QAAA,IAAI,WAAW,IAAI,aAAa,CAAC,SAAS,EAAE;YAC1C,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,QAAQ,EAAE,kBAAkB,EAAE;;AAGzD,QAAA,MAAM,EAAE,OAAO,EAAE,GAAG,2BAA2B,CAAC;YAC9C,gBAAgB,EAAE,aAAa,CAAC,SAAS;YACzC,QAAQ,EAAE,MAAM,CAAC,QAAQ;YACzB,kBAAkB;YAClB,SAAS,EAAE,MAAM,CAAC,SAAS;YAC3B,eAAe,EAAE,aAAa,CAAC,eAAe;YAC9C,YAAY,EAAE,aAAa,CAAC,YAAY;AACzC,SAAA,CAAC;AAEF,QAAA,OAAO,EAAE,OAAO,EAAE,kBAAkB,EAAE;AACxC,KAAC;AACH;;;;"}
|
|
1
|
+
{"version":3,"file":"prune.mjs","sources":["../../../src/messages/prune.ts"],"sourcesContent":["import { AIMessage, BaseMessage, UsageMetadata } from '@langchain/core/messages';\nimport type { ThinkingContentText, MessageContentComplex } from '@/types/stream';\nimport type { TokenCounter } from '@/types/run';\nimport { ContentTypes } from '@/common';\nexport type PruneMessagesFactoryParams = {\n maxTokens: number;\n startIndex: number;\n tokenCounter: TokenCounter;\n indexTokenCountMap: Record<string, number>;\n thinkingEnabled?: boolean;\n};\nexport type PruneMessagesParams = {\n messages: BaseMessage[];\n usageMetadata?: Partial<UsageMetadata>;\n startType?: ReturnType<BaseMessage['getType']>;\n}\n\nfunction isIndexInContext(arrayA: BaseMessage[], arrayB: BaseMessage[], targetIndex: number): boolean {\n const startingIndexInA = arrayA.length - arrayB.length;\n return targetIndex >= startingIndexInA;\n}\n\nfunction addThinkingBlock(message: AIMessage, thinkingBlock: ThinkingContentText): MessageContentComplex[] {\n const content: MessageContentComplex[] = Array.isArray(message.content)\n ? message.content as MessageContentComplex[]\n : [{\n type: ContentTypes.TEXT,\n text: message.content,\n }];\n content.unshift(thinkingBlock);\n return content;\n}\n\n/**\n * Calculates the total tokens from a single usage object\n *\n * @param usage The usage metadata object containing token information\n * @returns An object containing the total input and output tokens\n */\nexport function calculateTotalTokens(usage: Partial<UsageMetadata>): UsageMetadata {\n const baseInputTokens = Number(usage.input_tokens) || 0;\n const cacheCreation = Number(usage.input_token_details?.cache_creation) || 0;\n const cacheRead = Number(usage.input_token_details?.cache_read) || 0;\n\n const totalInputTokens = baseInputTokens + cacheCreation + cacheRead;\n const totalOutputTokens = Number(usage.output_tokens) || 0;\n\n return {\n input_tokens: totalInputTokens,\n output_tokens: totalOutputTokens,\n total_tokens: totalInputTokens + totalOutputTokens\n };\n}\n\n/**\n * Processes an array of messages and returns a context of messages that fit within a specified token limit.\n * It iterates over the messages from newest to oldest, adding them to the context until the token limit is reached.\n *\n * @param options Configuration options for processing messages\n * @returns Object containing the message context, remaining tokens, messages not included, and summary index\n */\nexport function getMessagesWithinTokenLimit({\n messages: _messages,\n maxContextTokens,\n indexTokenCountMap,\n startType: _startType,\n thinkingEnabled,\n /** We may need to use this when recalculating */\n tokenCounter,\n}: {\n messages: BaseMessage[];\n maxContextTokens: number;\n indexTokenCountMap: Record<string, number | undefined>;\n tokenCounter: TokenCounter;\n startType?: string;\n thinkingEnabled?: boolean;\n}): {\n context: BaseMessage[];\n remainingContextTokens: number;\n messagesToRefine: BaseMessage[];\n} {\n // Every reply is primed with <|start|>assistant<|message|>, so we\n // start with 3 tokens for the label after all messages have been counted.\n let currentTokenCount = 3;\n const instructions = _messages[0]?.getType() === 'system' ? _messages[0] : undefined;\n const instructionsTokenCount = instructions != null ? indexTokenCountMap[0] ?? 0 : 0;\n const initialContextTokens = maxContextTokens - instructionsTokenCount;\n let remainingContextTokens = initialContextTokens;\n let startType = _startType;\n const originalLength = _messages.length;\n const messages = [..._messages];\n /**\n * IMPORTANT: this context array gets reversed at the end, since the latest messages get pushed first.\n *\n * This may be confusing to read, but it is done to ensure the context is in the correct order for the model.\n * */\n let context: BaseMessage[] = [];\n\n let thinkingStartIndex = -1;\n let thinkingEndIndex = -1;\n let thinkingBlock: ThinkingContentText | undefined;\n const endIndex = instructions != null ? 1 : 0;\n const prunedMemory: BaseMessage[] = [];\n\n if (currentTokenCount < remainingContextTokens) {\n let currentIndex = messages.length;\n while (messages.length > 0 && currentTokenCount < remainingContextTokens && currentIndex > endIndex) {\n currentIndex--;\n if (messages.length === 1 && instructions) {\n break;\n }\n const poppedMessage = messages.pop();\n if (!poppedMessage) continue;\n const messageType = poppedMessage.getType();\n if (thinkingEnabled === true && thinkingEndIndex === -1 && (currentIndex === (originalLength - 1)) && (messageType === 'ai' || messageType === 'tool')) {\n thinkingEndIndex = currentIndex;\n }\n if (thinkingEndIndex > -1 && !thinkingBlock && thinkingStartIndex < 0 && messageType === 'ai' && Array.isArray(poppedMessage.content)) {\n thinkingBlock = (poppedMessage.content.find((content) => content.type === ContentTypes.THINKING)) as ThinkingContentText | undefined;\n thinkingStartIndex = thinkingBlock != null ? currentIndex : -1;\n }\n /** False start, the latest message was not part of a multi-assistant/tool sequence of messages */\n if (\n thinkingEndIndex > -1\n && currentIndex === (thinkingEndIndex - 1)\n && (messageType !== 'ai' && messageType !== 'tool')\n ) {\n thinkingEndIndex = -1;\n }\n\n const tokenCount = indexTokenCountMap[currentIndex] ?? 0;\n\n if (prunedMemory.length === 0 && ((currentTokenCount + tokenCount) <= remainingContextTokens)) {\n context.push(poppedMessage);\n currentTokenCount += tokenCount;\n } else {\n prunedMemory.push(poppedMessage);\n if (thinkingEndIndex > -1) {\n continue;\n }\n break;\n }\n }\n\n if (thinkingEndIndex > -1 && context[context.length - 1].getType() === 'tool') {\n startType = 'ai';\n }\n\n if (startType != null && startType && context.length > 0) {\n const requiredTypeIndex = context.findIndex(msg => msg.getType() === startType);\n\n if (requiredTypeIndex > 0) {\n context = context.slice(requiredTypeIndex);\n }\n }\n }\n\n if (instructions && originalLength > 0) {\n context.push(_messages[0] as BaseMessage);\n messages.shift();\n }\n\n remainingContextTokens -= currentTokenCount;\n const result = {\n remainingContextTokens,\n context: [] as BaseMessage[],\n messagesToRefine: prunedMemory,\n };\n\n if (prunedMemory.length === 0 || thinkingEndIndex < 0 || (thinkingStartIndex > -1 && isIndexInContext(_messages, context, thinkingStartIndex))) {\n // we reverse at this step to ensure the context is in the correct order for the model, and we need to work backwards\n result.context = context.reverse();\n return result;\n }\n\n if (thinkingEndIndex > -1 && thinkingStartIndex < 0) {\n throw new Error('The payload is malformed. There is a thinking sequence but no \"AI\" messages with thinking blocks.');\n }\n\n if (!thinkingBlock) {\n throw new Error('The payload is malformed. There is a thinking sequence but no thinking block found.');\n }\n\n // Since we have a thinking sequence, we need to find the last assistant message\n // in the latest AI/tool sequence to add the thinking block that falls outside of the current context\n // Latest messages are ordered first.\n let assistantIndex = -1;\n for (let i = 0; i < context.length; i++) {\n const currentMessage = context[i];\n const type = currentMessage.getType();\n if (type === 'ai') {\n assistantIndex = i;\n }\n if (assistantIndex > -1 && (type === 'human' || type === 'system')) {\n break;\n }\n }\n\n if (assistantIndex === -1) {\n throw new Error('The payload is malformed. There is a thinking sequence but no \"AI\" messages to append thinking blocks to.');\n }\n\n thinkingStartIndex = originalLength - 1 - assistantIndex;\n const thinkingTokenCount = tokenCounter(new AIMessage({ content: [thinkingBlock] }));\n const newRemainingCount = remainingContextTokens - thinkingTokenCount;\n\n const content: MessageContentComplex[] = addThinkingBlock(context[assistantIndex] as AIMessage, thinkingBlock);\n context[assistantIndex].content = content;\n if (newRemainingCount > 0) {\n result.context = context.reverse();\n return result;\n }\n\n const thinkingMessage: AIMessage = context[assistantIndex];\n // now we need to an additional round of pruning but making the thinking block fit\n const newThinkingMessageTokenCount = (indexTokenCountMap[thinkingStartIndex] ?? 0) + thinkingTokenCount;\n remainingContextTokens = initialContextTokens - newThinkingMessageTokenCount;\n currentTokenCount = 3;\n let newContext: BaseMessage[] = [];\n const secondRoundMessages = [..._messages];\n let currentIndex = secondRoundMessages.length;\n while (secondRoundMessages.length > 0 && currentTokenCount < remainingContextTokens && currentIndex > thinkingStartIndex) {\n currentIndex--;\n const poppedMessage = secondRoundMessages.pop();\n if (!poppedMessage) continue;\n const tokenCount = indexTokenCountMap[currentIndex] ?? 0;\n if ((currentTokenCount + tokenCount) <= remainingContextTokens) {\n newContext.push(poppedMessage);\n currentTokenCount += tokenCount;\n } else {\n messages.push(poppedMessage);\n break;\n }\n }\n\n const firstMessage: AIMessage = newContext[newContext.length - 1];\n const firstMessageType = newContext[newContext.length - 1].getType();\n if (firstMessageType === 'tool') {\n startType = 'ai';\n }\n\n if (startType != null && startType && newContext.length > 0) {\n const requiredTypeIndex = newContext.findIndex(msg => msg.getType() === startType);\n if (requiredTypeIndex > 0) {\n newContext = newContext.slice(requiredTypeIndex);\n }\n }\n\n if (firstMessageType === 'ai') {\n const content = addThinkingBlock(firstMessage, thinkingBlock);\n newContext[newContext.length - 1].content = content;\n } else {\n newContext.push(thinkingMessage);\n }\n\n if (instructions && originalLength > 0) {\n newContext.push(_messages[0] as BaseMessage);\n secondRoundMessages.shift();\n }\n\n result.context = newContext.reverse();\n return result;\n}\n\nexport function checkValidNumber(value: unknown): value is number {\n return typeof value === 'number' && !isNaN(value) && value > 0;\n}\n\nexport function createPruneMessages(factoryParams: PruneMessagesFactoryParams) {\n const indexTokenCountMap = { ...factoryParams.indexTokenCountMap };\n let lastTurnStartIndex = factoryParams.startIndex;\n let lastCutOffIndex = 0;\n let totalTokens = (Object.values(indexTokenCountMap)).reduce((a, b) => a + b, 0);\n return function pruneMessages(params: PruneMessagesParams): {\n context: BaseMessage[];\n indexTokenCountMap: Record<string, number>;\n } {\n let currentUsage: UsageMetadata | undefined;\n if (params.usageMetadata && (\n checkValidNumber(params.usageMetadata.input_tokens)\n || (\n checkValidNumber(params.usageMetadata.input_token_details)\n && (\n checkValidNumber(params.usageMetadata.input_token_details.cache_creation)\n || checkValidNumber(params.usageMetadata.input_token_details.cache_read)\n )\n )\n ) && checkValidNumber(params.usageMetadata.output_tokens)) {\n currentUsage = calculateTotalTokens(params.usageMetadata);\n totalTokens = currentUsage.total_tokens;\n }\n\n for (let i = lastTurnStartIndex; i < params.messages.length; i++) {\n const message = params.messages[i];\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (i === lastTurnStartIndex && indexTokenCountMap[i] === undefined && currentUsage) {\n indexTokenCountMap[i] = currentUsage.output_tokens;\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n } else if (indexTokenCountMap[i] === undefined) {\n indexTokenCountMap[i] = factoryParams.tokenCounter(message);\n totalTokens += indexTokenCountMap[i];\n }\n }\n\n // If `currentUsage` is defined, we need to distribute the current total tokens to our `indexTokenCountMap`,\n // We must distribute it in a weighted manner, so that the total token count is equal to `currentUsage.total_tokens`,\n // relative the manually counted tokens in `indexTokenCountMap`.\n // EDGE CASE: when the resulting context gets pruned, we should not distribute the usage for messages that are not in the context.\n if (currentUsage) {\n // Calculate the sum of tokens only for indices at or after lastCutOffIndex\n const totalIndexTokens = Object.entries(indexTokenCountMap).reduce((sum, [key, value]) => {\n // Convert string key to number and check if it's >= lastCutOffIndex\n const numericKey = Number(key);\n if (numericKey === 0 && params.messages[0].getType() === 'system') {\n return sum + value;\n }\n return numericKey >= lastCutOffIndex ? sum + value : sum;\n }, 0);\n\n // Calculate ratio based only on messages that remain in the context\n const ratio = currentUsage.total_tokens / totalIndexTokens;\n\n // Apply the ratio adjustment only to messages at or after lastCutOffIndex\n for (const key in indexTokenCountMap) {\n const numericKey = Number(key);\n if (numericKey === 0 && params.messages[0].getType() === 'system') {\n indexTokenCountMap[key] = Math.round(indexTokenCountMap[key] * ratio);\n } else if (numericKey >= lastCutOffIndex) {\n // Only adjust token counts for messages still in the context\n indexTokenCountMap[key] = Math.round(indexTokenCountMap[key] * ratio);\n }\n }\n }\n\n lastTurnStartIndex = params.messages.length;\n if (totalTokens <= factoryParams.maxTokens) {\n return { context: params.messages, indexTokenCountMap };\n }\n\n const { context } = getMessagesWithinTokenLimit({\n maxContextTokens: factoryParams.maxTokens,\n messages: params.messages,\n indexTokenCountMap,\n startType: params.startType,\n thinkingEnabled: factoryParams.thinkingEnabled,\n tokenCounter: factoryParams.tokenCounter,\n });\n lastCutOffIndex = Math.max(params.messages.length - context.length, 0);\n\n return { context, indexTokenCountMap };\n };\n}\n"],"names":[],"mappings":";;;AAiBA,SAAS,gBAAgB,CAAC,MAAqB,EAAE,MAAqB,EAAE,WAAmB,EAAA;IACzF,MAAM,gBAAgB,GAAG,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM;IACtD,OAAO,WAAW,IAAI,gBAAgB;AACxC;AAEA,SAAS,gBAAgB,CAAC,OAAkB,EAAE,aAAkC,EAAA;IAC9E,MAAM,OAAO,GAA4B,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,OAAO;UAClE,OAAO,CAAC;AACV,UAAE,CAAC;gBACD,IAAI,EAAE,YAAY,CAAC,IAAI;gBACvB,IAAI,EAAE,OAAO,CAAC,OAAO;AACtB,aAAA,CAAC;AACJ,IAAA,OAAO,CAAC,OAAO,CAAC,aAAa,CAAC;AAC9B,IAAA,OAAO,OAAO;AAChB;AAEA;;;;;AAKG;AACG,SAAU,oBAAoB,CAAC,KAA6B,EAAA;IAChE,MAAM,eAAe,GAAG,MAAM,CAAC,KAAK,CAAC,YAAY,CAAC,IAAI,CAAC;AACvD,IAAA,MAAM,aAAa,GAAG,MAAM,CAAC,KAAK,CAAC,mBAAmB,EAAE,cAAc,CAAC,IAAI,CAAC;AAC5E,IAAA,MAAM,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC,mB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ClB,SAAS,EAAE,MAAM,CAAC,SAAS;YAC3B,eAAe,EAAE,aAAa,CAAC,eAAe;YAC9C,YAAY,EAAE,aAAa,CAAC,YAAY;AACzC,SAAA,CAAC;AACF,QAAA,eAAe,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;AAEtE,QAAA,OAAO,EAAE,OAAO,EAAE,kBAAkB,EAAE;AACxC,KAAC;AACH;;;;"}
|
package/package.json
CHANGED
package/src/messages/prune.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { concat } from '@langchain/core/utils/stream';
|
|
2
1
|
import { AIMessage, BaseMessage, UsageMetadata } from '@langchain/core/messages';
|
|
3
2
|
import type { ThinkingContentText, MessageContentComplex } from '@/types/stream';
|
|
4
3
|
import type { TokenCounter } from '@/types/run';
|
|
@@ -21,6 +20,17 @@ function isIndexInContext(arrayA: BaseMessage[], arrayB: BaseMessage[], targetIn
|
|
|
21
20
|
return targetIndex >= startingIndexInA;
|
|
22
21
|
}
|
|
23
22
|
|
|
23
|
+
function addThinkingBlock(message: AIMessage, thinkingBlock: ThinkingContentText): MessageContentComplex[] {
|
|
24
|
+
const content: MessageContentComplex[] = Array.isArray(message.content)
|
|
25
|
+
? message.content as MessageContentComplex[]
|
|
26
|
+
: [{
|
|
27
|
+
type: ContentTypes.TEXT,
|
|
28
|
+
text: message.content,
|
|
29
|
+
}];
|
|
30
|
+
content.unshift(thinkingBlock);
|
|
31
|
+
return content;
|
|
32
|
+
}
|
|
33
|
+
|
|
24
34
|
/**
|
|
25
35
|
* Calculates the total tokens from a single usage object
|
|
26
36
|
*
|
|
@@ -194,13 +204,7 @@ export function getMessagesWithinTokenLimit({
|
|
|
194
204
|
const thinkingTokenCount = tokenCounter(new AIMessage({ content: [thinkingBlock] }));
|
|
195
205
|
const newRemainingCount = remainingContextTokens - thinkingTokenCount;
|
|
196
206
|
|
|
197
|
-
const content: MessageContentComplex[] =
|
|
198
|
-
? context[assistantIndex].content as MessageContentComplex[]
|
|
199
|
-
: [{
|
|
200
|
-
type: ContentTypes.TEXT,
|
|
201
|
-
text: context[assistantIndex].content,
|
|
202
|
-
}];
|
|
203
|
-
content.unshift(thinkingBlock);
|
|
207
|
+
const content: MessageContentComplex[] = addThinkingBlock(context[assistantIndex] as AIMessage, thinkingBlock);
|
|
204
208
|
context[assistantIndex].content = content;
|
|
205
209
|
if (newRemainingCount > 0) {
|
|
206
210
|
result.context = context.reverse();
|
|
@@ -243,10 +247,8 @@ export function getMessagesWithinTokenLimit({
|
|
|
243
247
|
}
|
|
244
248
|
|
|
245
249
|
if (firstMessageType === 'ai') {
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
tool_calls: concat(firstMessage.tool_calls, thinkingMessage.tool_calls),
|
|
249
|
-
});
|
|
250
|
+
const content = addThinkingBlock(firstMessage, thinkingBlock);
|
|
251
|
+
newContext[newContext.length - 1].content = content;
|
|
250
252
|
} else {
|
|
251
253
|
newContext.push(thinkingMessage);
|
|
252
254
|
}
|
|
@@ -267,6 +269,7 @@ export function checkValidNumber(value: unknown): value is number {
|
|
|
267
269
|
export function createPruneMessages(factoryParams: PruneMessagesFactoryParams) {
|
|
268
270
|
const indexTokenCountMap = { ...factoryParams.indexTokenCountMap };
|
|
269
271
|
let lastTurnStartIndex = factoryParams.startIndex;
|
|
272
|
+
let lastCutOffIndex = 0;
|
|
270
273
|
let totalTokens = (Object.values(indexTokenCountMap)).reduce((a, b) => a + b, 0);
|
|
271
274
|
return function pruneMessages(params: PruneMessagesParams): {
|
|
272
275
|
context: BaseMessage[];
|
|
@@ -299,15 +302,33 @@ export function createPruneMessages(factoryParams: PruneMessagesFactoryParams) {
|
|
|
299
302
|
}
|
|
300
303
|
}
|
|
301
304
|
|
|
302
|
-
// If `currentUsage` is defined, we need to distribute the current total
|
|
303
|
-
// for all message index keys before `lastTurnStartIndex`, as it has the most accurate count for those messages.
|
|
305
|
+
// If `currentUsage` is defined, we need to distribute the current total tokens to our `indexTokenCountMap`,
|
|
304
306
|
// We must distribute it in a weighted manner, so that the total token count is equal to `currentUsage.total_tokens`,
|
|
305
307
|
// relative the manually counted tokens in `indexTokenCountMap`.
|
|
308
|
+
// EDGE CASE: when the resulting context gets pruned, we should not distribute the usage for messages that are not in the context.
|
|
306
309
|
if (currentUsage) {
|
|
307
|
-
|
|
310
|
+
// Calculate the sum of tokens only for indices at or after lastCutOffIndex
|
|
311
|
+
const totalIndexTokens = Object.entries(indexTokenCountMap).reduce((sum, [key, value]) => {
|
|
312
|
+
// Convert string key to number and check if it's >= lastCutOffIndex
|
|
313
|
+
const numericKey = Number(key);
|
|
314
|
+
if (numericKey === 0 && params.messages[0].getType() === 'system') {
|
|
315
|
+
return sum + value;
|
|
316
|
+
}
|
|
317
|
+
return numericKey >= lastCutOffIndex ? sum + value : sum;
|
|
318
|
+
}, 0);
|
|
319
|
+
|
|
320
|
+
// Calculate ratio based only on messages that remain in the context
|
|
308
321
|
const ratio = currentUsage.total_tokens / totalIndexTokens;
|
|
322
|
+
|
|
323
|
+
// Apply the ratio adjustment only to messages at or after lastCutOffIndex
|
|
309
324
|
for (const key in indexTokenCountMap) {
|
|
310
|
-
|
|
325
|
+
const numericKey = Number(key);
|
|
326
|
+
if (numericKey === 0 && params.messages[0].getType() === 'system') {
|
|
327
|
+
indexTokenCountMap[key] = Math.round(indexTokenCountMap[key] * ratio);
|
|
328
|
+
} else if (numericKey >= lastCutOffIndex) {
|
|
329
|
+
// Only adjust token counts for messages still in the context
|
|
330
|
+
indexTokenCountMap[key] = Math.round(indexTokenCountMap[key] * ratio);
|
|
331
|
+
}
|
|
311
332
|
}
|
|
312
333
|
}
|
|
313
334
|
|
|
@@ -324,6 +345,7 @@ export function createPruneMessages(factoryParams: PruneMessagesFactoryParams) {
|
|
|
324
345
|
thinkingEnabled: factoryParams.thinkingEnabled,
|
|
325
346
|
tokenCounter: factoryParams.tokenCounter,
|
|
326
347
|
});
|
|
348
|
+
lastCutOffIndex = Math.max(params.messages.length - context.length, 0);
|
|
327
349
|
|
|
328
350
|
return { context, indexTokenCountMap };
|
|
329
351
|
};
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
// src/specs/token-distribution-edge-case.test.ts
|
|
2
|
+
import { HumanMessage, AIMessage, SystemMessage, BaseMessage } from '@langchain/core/messages';
|
|
3
|
+
import type { UsageMetadata } from '@langchain/core/messages';
|
|
4
|
+
import type * as t from '@/types';
|
|
5
|
+
import { createPruneMessages } from '@/messages/prune';
|
|
6
|
+
|
|
7
|
+
// Create a simple token counter for testing
|
|
8
|
+
const createTestTokenCounter = (): t.TokenCounter => {
|
|
9
|
+
// This simple token counter just counts characters as tokens for predictable testing
|
|
10
|
+
return (message: BaseMessage): number => {
|
|
11
|
+
// Use type assertion to help TypeScript understand the type
|
|
12
|
+
const content = message.content as string | Array<t.MessageContentComplex | string> | undefined;
|
|
13
|
+
|
|
14
|
+
// Handle string content
|
|
15
|
+
if (typeof content === 'string') {
|
|
16
|
+
return content.length;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Handle array content
|
|
20
|
+
if (Array.isArray(content)) {
|
|
21
|
+
let totalLength = 0;
|
|
22
|
+
|
|
23
|
+
for (const item of content) {
|
|
24
|
+
if (typeof item === 'string') {
|
|
25
|
+
totalLength += item.length;
|
|
26
|
+
} else if (typeof item === 'object') {
|
|
27
|
+
if ('text' in item && typeof item.text === 'string') {
|
|
28
|
+
totalLength += item.text.length;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return totalLength;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Default case - if content is null, undefined, or any other type
|
|
37
|
+
return 0;
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
describe('Token Distribution Edge Case Tests', () => {
|
|
42
|
+
it('should only distribute tokens to messages that remain in the context after pruning', () => {
|
|
43
|
+
// Create a token counter
|
|
44
|
+
const tokenCounter = createTestTokenCounter();
|
|
45
|
+
|
|
46
|
+
// Create messages
|
|
47
|
+
const messages = [
|
|
48
|
+
new SystemMessage('System instruction'), // Will always be included
|
|
49
|
+
new HumanMessage('Message 1'), // Will be pruned
|
|
50
|
+
new AIMessage('Response 1'), // Will be pruned
|
|
51
|
+
new HumanMessage('Message 2'), // Will remain
|
|
52
|
+
new AIMessage('Response 2') // Will remain
|
|
53
|
+
];
|
|
54
|
+
|
|
55
|
+
// Calculate initial token counts for each message
|
|
56
|
+
const indexTokenCountMap: Record<string, number> = {
|
|
57
|
+
0: 17, // "System instruction"
|
|
58
|
+
1: 9, // "Message 1"
|
|
59
|
+
2: 10, // "Response 1"
|
|
60
|
+
3: 9, // "Message 2"
|
|
61
|
+
4: 10 // "Response 2"
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// Set a token limit that will force pruning of the first two messages after the system message
|
|
65
|
+
const pruneMessages = createPruneMessages({
|
|
66
|
+
maxTokens: 40, // Only enough for system message + last two messages
|
|
67
|
+
startIndex: 0,
|
|
68
|
+
tokenCounter,
|
|
69
|
+
indexTokenCountMap: { ...indexTokenCountMap }
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// First call to establish lastCutOffIndex
|
|
73
|
+
const initialResult = pruneMessages({ messages });
|
|
74
|
+
|
|
75
|
+
// Verify initial pruning
|
|
76
|
+
expect(initialResult.context.length).toBe(3);
|
|
77
|
+
expect(initialResult.context[0].content).toBe('System instruction');
|
|
78
|
+
expect(initialResult.context[1].content).toBe('Message 2');
|
|
79
|
+
expect(initialResult.context[2].content).toBe('Response 2');
|
|
80
|
+
|
|
81
|
+
// Now provide usage metadata with a different total token count
|
|
82
|
+
const usageMetadata: Partial<UsageMetadata> = {
|
|
83
|
+
input_tokens: 30,
|
|
84
|
+
output_tokens: 20,
|
|
85
|
+
total_tokens: 50 // Different from the sum of our initial token counts
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// Call pruneMessages again with the usage metadata
|
|
89
|
+
const result = pruneMessages({
|
|
90
|
+
messages,
|
|
91
|
+
usageMetadata
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// The token distribution should only affect messages that remain in the context
|
|
95
|
+
// Messages at indices 0, 3, and 4 should have their token counts adjusted
|
|
96
|
+
// Messages at indices 1 and 2 should remain unchanged since they're pruned
|
|
97
|
+
|
|
98
|
+
// The token distribution should only affect messages that remain in the context
|
|
99
|
+
// Messages at indices 0, 3, and 4 should have their token counts adjusted
|
|
100
|
+
// Messages at indices 1 and 2 should remain unchanged since they're pruned
|
|
101
|
+
|
|
102
|
+
// Check that at least one of the pruned messages' token counts was not adjusted
|
|
103
|
+
// We're testing the principle that pruned messages don't get token redistribution
|
|
104
|
+
const atLeastOnePrunedMessageUnchanged =
|
|
105
|
+
result.indexTokenCountMap[1] === indexTokenCountMap[1] ||
|
|
106
|
+
result.indexTokenCountMap[2] === indexTokenCountMap[2];
|
|
107
|
+
|
|
108
|
+
expect(atLeastOnePrunedMessageUnchanged).toBe(true);
|
|
109
|
+
|
|
110
|
+
// Verify that the sum of tokens for messages in the context is close to the total_tokens from usageMetadata
|
|
111
|
+
// There might be small rounding differences or implementation details that affect the exact sum
|
|
112
|
+
const totalContextTokens = result.indexTokenCountMap[0] + result.indexTokenCountMap[3] + result.indexTokenCountMap[4];
|
|
113
|
+
expect(totalContextTokens).toBeGreaterThan(0);
|
|
114
|
+
|
|
115
|
+
// The key thing we're testing is that the token distribution happens for messages in the context
|
|
116
|
+
// and that the sum is reasonably close to the expected total
|
|
117
|
+
const tokenDifference = Math.abs(totalContextTokens - 50);
|
|
118
|
+
expect(tokenDifference).toBeLessThan(20); // Allow for some difference due to implementation details
|
|
119
|
+
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('should handle the case when all messages fit within the token limit', () => {
|
|
123
|
+
// Create a token counter
|
|
124
|
+
const tokenCounter = createTestTokenCounter();
|
|
125
|
+
|
|
126
|
+
// Create messages
|
|
127
|
+
const messages = [
|
|
128
|
+
new SystemMessage('System instruction'),
|
|
129
|
+
new HumanMessage('Message 1'),
|
|
130
|
+
new AIMessage('Response 1')
|
|
131
|
+
];
|
|
132
|
+
|
|
133
|
+
// Calculate initial token counts for each message
|
|
134
|
+
const indexTokenCountMap: Record<string, number> = {
|
|
135
|
+
0: 17, // "System instruction"
|
|
136
|
+
1: 9, // "Message 1"
|
|
137
|
+
2: 10 // "Response 1"
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
// Set a token limit that will allow all messages to fit
|
|
141
|
+
const pruneMessages = createPruneMessages({
|
|
142
|
+
maxTokens: 100,
|
|
143
|
+
startIndex: 0,
|
|
144
|
+
tokenCounter,
|
|
145
|
+
indexTokenCountMap: { ...indexTokenCountMap }
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// First call to establish lastCutOffIndex (should be 0 since no pruning occurs)
|
|
149
|
+
const initialResult = pruneMessages({ messages });
|
|
150
|
+
|
|
151
|
+
// Verify no pruning occurred
|
|
152
|
+
expect(initialResult.context.length).toBe(3);
|
|
153
|
+
|
|
154
|
+
// Now provide usage metadata with a different total token count
|
|
155
|
+
const usageMetadata: Partial<UsageMetadata> = {
|
|
156
|
+
input_tokens: 20,
|
|
157
|
+
output_tokens: 10,
|
|
158
|
+
total_tokens: 30 // Different from the sum of our initial token counts
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
// Call pruneMessages again with the usage metadata
|
|
162
|
+
const result = pruneMessages({
|
|
163
|
+
messages,
|
|
164
|
+
usageMetadata
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
// Since all messages fit, all token counts should be adjusted
|
|
168
|
+
const initialTotalTokens = indexTokenCountMap[0] + indexTokenCountMap[1] + indexTokenCountMap[2];
|
|
169
|
+
const expectedRatio = 30 / initialTotalTokens;
|
|
170
|
+
|
|
171
|
+
// Check that all token counts were adjusted
|
|
172
|
+
expect(result.indexTokenCountMap[0]).toBe(Math.round(indexTokenCountMap[0] * expectedRatio));
|
|
173
|
+
expect(result.indexTokenCountMap[1]).toBe(Math.round(indexTokenCountMap[1] * expectedRatio));
|
|
174
|
+
expect(result.indexTokenCountMap[2]).toBe(Math.round(indexTokenCountMap[2] * expectedRatio));
|
|
175
|
+
|
|
176
|
+
// Verify that the sum of all tokens equals the total_tokens from usageMetadata
|
|
177
|
+
const totalTokens = result.indexTokenCountMap[0] + result.indexTokenCountMap[1] + result.indexTokenCountMap[2];
|
|
178
|
+
expect(totalTokens).toBe(30);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('should handle multiple pruning operations with token redistribution', () => {
|
|
182
|
+
// Create a token counter
|
|
183
|
+
const tokenCounter = createTestTokenCounter();
|
|
184
|
+
|
|
185
|
+
// Create a longer sequence of messages
|
|
186
|
+
const messages = [
|
|
187
|
+
new SystemMessage('System instruction'), // Will always be included
|
|
188
|
+
new HumanMessage('Message 1'), // Will be pruned in first round
|
|
189
|
+
new AIMessage('Response 1'), // Will be pruned in first round
|
|
190
|
+
new HumanMessage('Message 2'), // Will be pruned in second round
|
|
191
|
+
new AIMessage('Response 2'), // Will be pruned in second round
|
|
192
|
+
new HumanMessage('Message 3'), // Will remain
|
|
193
|
+
new AIMessage('Response 3') // Will remain
|
|
194
|
+
];
|
|
195
|
+
|
|
196
|
+
// Calculate initial token counts for each message
|
|
197
|
+
const indexTokenCountMap: Record<string, number> = {
|
|
198
|
+
0: 17, // "System instruction"
|
|
199
|
+
1: 9, // "Message 1"
|
|
200
|
+
2: 10, // "Response 1"
|
|
201
|
+
3: 9, // "Message 2"
|
|
202
|
+
4: 10, // "Response 2"
|
|
203
|
+
5: 9, // "Message 3"
|
|
204
|
+
6: 10 // "Response 3"
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
// Set a token limit that will force pruning
|
|
208
|
+
const pruneMessages = createPruneMessages({
|
|
209
|
+
maxTokens: 40, // Only enough for system message + last two messages
|
|
210
|
+
startIndex: 0,
|
|
211
|
+
tokenCounter,
|
|
212
|
+
indexTokenCountMap: { ...indexTokenCountMap }
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
// First pruning operation
|
|
216
|
+
const firstResult = pruneMessages({ messages });
|
|
217
|
+
|
|
218
|
+
// Verify first pruning
|
|
219
|
+
expect(firstResult.context.length).toBe(3);
|
|
220
|
+
expect(firstResult.context[0].content).toBe('System instruction');
|
|
221
|
+
expect(firstResult.context[1].content).toBe('Message 3');
|
|
222
|
+
expect(firstResult.context[2].content).toBe('Response 3');
|
|
223
|
+
|
|
224
|
+
// First usage metadata update
|
|
225
|
+
const firstUsageMetadata: Partial<UsageMetadata> = {
|
|
226
|
+
input_tokens: 30,
|
|
227
|
+
output_tokens: 20,
|
|
228
|
+
total_tokens: 50
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
// Apply first usage metadata
|
|
232
|
+
const secondResult = pruneMessages({
|
|
233
|
+
messages,
|
|
234
|
+
usageMetadata: firstUsageMetadata
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
// Add two more messages
|
|
238
|
+
const extendedMessages = [
|
|
239
|
+
...messages,
|
|
240
|
+
new HumanMessage('Message 4'),
|
|
241
|
+
new AIMessage('Response 4')
|
|
242
|
+
];
|
|
243
|
+
|
|
244
|
+
// Second usage metadata update
|
|
245
|
+
const secondUsageMetadata: Partial<UsageMetadata> = {
|
|
246
|
+
input_tokens: 40,
|
|
247
|
+
output_tokens: 30,
|
|
248
|
+
total_tokens: 70
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
// Apply second usage metadata with extended messages
|
|
252
|
+
const thirdResult = pruneMessages({
|
|
253
|
+
messages: extendedMessages,
|
|
254
|
+
usageMetadata: secondUsageMetadata
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
// The context should include the system message and some of the latest messages
|
|
258
|
+
expect(thirdResult.context.length).toBeGreaterThan(0);
|
|
259
|
+
expect(thirdResult.context[0].content).toBe('System instruction');
|
|
260
|
+
|
|
261
|
+
// Find which messages are in the final context
|
|
262
|
+
const contextMessageIndices = thirdResult.context.map(msg => {
|
|
263
|
+
// Find the index of this message in the original array
|
|
264
|
+
return extendedMessages.findIndex(m => m.content === msg.content);
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
// Get the sum of token counts for messages in the context
|
|
268
|
+
let totalContextTokens = 0;
|
|
269
|
+
for (const idx of contextMessageIndices) {
|
|
270
|
+
totalContextTokens += thirdResult.indexTokenCountMap[idx];
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// Verify that the sum of tokens for messages in the context is close to the total_tokens from usageMetadata
|
|
274
|
+
// There might be small rounding differences or implementation details that affect the exact sum
|
|
275
|
+
expect(totalContextTokens).toBeGreaterThan(0);
|
|
276
|
+
|
|
277
|
+
// The key thing we're testing is that the token distribution happens for messages in the context
|
|
278
|
+
// and that the sum is reasonably close to the expected total
|
|
279
|
+
const tokenDifference = Math.abs(totalContextTokens - 70);
|
|
280
|
+
expect(tokenDifference).toBeLessThan(50); // Allow for some difference due to implementation details
|
|
281
|
+
|
|
282
|
+
// Verify that messages not in the context have their original token counts or previously adjusted values
|
|
283
|
+
for (let i = 0; i < extendedMessages.length; i++) {
|
|
284
|
+
if (!contextMessageIndices.includes(i)) {
|
|
285
|
+
// This message is not in the context, so its token count should not have been adjusted in the last operation
|
|
286
|
+
const expectedValue = i < messages.length
|
|
287
|
+
? (secondResult.indexTokenCountMap[i] || indexTokenCountMap[i])
|
|
288
|
+
: (indexTokenCountMap as Record<string, number | undefined>)[i] ?? indexTokenCountMap[i - 1];
|
|
289
|
+
|
|
290
|
+
// For defined values, we can check that they're close to what we expect
|
|
291
|
+
const difference = Math.abs((thirdResult.indexTokenCountMap[i] || 0) - expectedValue);
|
|
292
|
+
expect(difference).toBeLessThan(20); // Allow for some implementation differences
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
});
|