@librechat/agents 3.0.773 → 3.0.774
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.
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var messages = require('@langchain/core/messages');
|
|
3
4
|
var _enum = require('../common/enum.cjs');
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Deep clones a message's content to prevent mutation of the original.
|
|
7
|
-
* Handles both string and array content types.
|
|
8
8
|
*/
|
|
9
9
|
function deepCloneContent(content) {
|
|
10
10
|
if (typeof content === 'string') {
|
|
@@ -16,36 +16,63 @@ function deepCloneContent(content) {
|
|
|
16
16
|
return content;
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
19
|
+
* Simple shallow clone with deep-cloned content.
|
|
20
|
+
* Used for stripping cache control where we don't need proper LangChain instances.
|
|
21
21
|
*/
|
|
22
|
-
function
|
|
23
|
-
|
|
24
|
-
return { ...message };
|
|
25
|
-
}
|
|
26
|
-
const clonedContent = deepCloneContent(message.content);
|
|
27
|
-
const cloned = {
|
|
22
|
+
function shallowCloneMessage(message) {
|
|
23
|
+
return {
|
|
28
24
|
...message,
|
|
29
|
-
content:
|
|
25
|
+
content: deepCloneContent(message.content ?? ''),
|
|
30
26
|
};
|
|
31
|
-
const lcKwargs = cloned.lc_kwargs;
|
|
32
|
-
if (lcKwargs != null) {
|
|
33
|
-
cloned.lc_kwargs = {
|
|
34
|
-
...lcKwargs,
|
|
35
|
-
content: clonedContent,
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
return cloned;
|
|
39
27
|
}
|
|
40
28
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
29
|
+
* Creates a new LangChain message instance with the given content.
|
|
30
|
+
* Required when adding cache points to ensure proper serialization.
|
|
43
31
|
*/
|
|
44
|
-
function
|
|
45
|
-
|
|
32
|
+
function createNewMessage(message, content) {
|
|
33
|
+
if ('getType' in message && typeof message.getType === 'function') {
|
|
34
|
+
const baseMsg = message;
|
|
35
|
+
const msgType = baseMsg.getType();
|
|
36
|
+
const baseFields = {
|
|
37
|
+
content,
|
|
38
|
+
name: baseMsg.name,
|
|
39
|
+
additional_kwargs: { ...baseMsg.additional_kwargs },
|
|
40
|
+
response_metadata: { ...baseMsg.response_metadata },
|
|
41
|
+
id: baseMsg.id,
|
|
42
|
+
};
|
|
43
|
+
switch (msgType) {
|
|
44
|
+
case 'human':
|
|
45
|
+
return new messages.HumanMessage(baseFields);
|
|
46
|
+
case 'ai': {
|
|
47
|
+
const aiMsg = baseMsg;
|
|
48
|
+
return new messages.AIMessage({
|
|
49
|
+
...baseFields,
|
|
50
|
+
tool_calls: aiMsg.tool_calls ? [...aiMsg.tool_calls] : [],
|
|
51
|
+
invalid_tool_calls: aiMsg.invalid_tool_calls
|
|
52
|
+
? [...aiMsg.invalid_tool_calls]
|
|
53
|
+
: [],
|
|
54
|
+
usage_metadata: aiMsg.usage_metadata,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
case 'system':
|
|
58
|
+
return new messages.SystemMessage(baseFields);
|
|
59
|
+
case 'tool': {
|
|
60
|
+
const toolMsg = baseMsg;
|
|
61
|
+
return new messages.ToolMessage({
|
|
62
|
+
...baseFields,
|
|
63
|
+
tool_call_id: toolMsg.tool_call_id,
|
|
64
|
+
status: toolMsg.status,
|
|
65
|
+
artifact: toolMsg.artifact,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const cloned = { ...message, content };
|
|
71
|
+
const lcKwargs = cloned.lc_kwargs;
|
|
46
72
|
if (lcKwargs != null) {
|
|
47
|
-
|
|
73
|
+
cloned.lc_kwargs = { ...lcKwargs, content };
|
|
48
74
|
}
|
|
75
|
+
return cloned;
|
|
49
76
|
}
|
|
50
77
|
/**
|
|
51
78
|
* Checks if a message's content needs cache control stripping.
|
|
@@ -89,7 +116,7 @@ function addCacheControl(messages) {
|
|
|
89
116
|
if (!needsStripping && !needsCacheAdd) {
|
|
90
117
|
continue;
|
|
91
118
|
}
|
|
92
|
-
const message =
|
|
119
|
+
const message = shallowCloneMessage(originalMessage);
|
|
93
120
|
updatedMessages[i] = message;
|
|
94
121
|
if (hasArrayContent) {
|
|
95
122
|
message.content = message.content.filter((block) => !isCachePoint(block));
|
|
@@ -101,7 +128,6 @@ function addCacheControl(messages) {
|
|
|
101
128
|
}
|
|
102
129
|
}
|
|
103
130
|
if (userMessagesModified >= 2 || !isUserMessage) {
|
|
104
|
-
syncLcKwargs(message);
|
|
105
131
|
continue;
|
|
106
132
|
}
|
|
107
133
|
if (typeof message.content === 'string') {
|
|
@@ -126,7 +152,6 @@ function addCacheControl(messages) {
|
|
|
126
152
|
}
|
|
127
153
|
}
|
|
128
154
|
}
|
|
129
|
-
syncLcKwargs(message);
|
|
130
155
|
}
|
|
131
156
|
return updatedMessages;
|
|
132
157
|
}
|
|
@@ -162,7 +187,7 @@ function stripAnthropicCacheControl(messages) {
|
|
|
162
187
|
if (!Array.isArray(content) || !hasAnthropicCacheControl(content)) {
|
|
163
188
|
continue;
|
|
164
189
|
}
|
|
165
|
-
const message =
|
|
190
|
+
const message = shallowCloneMessage(originalMessage);
|
|
166
191
|
updatedMessages[i] = message;
|
|
167
192
|
for (let j = 0; j < message.content.length; j++) {
|
|
168
193
|
const block = message.content[j];
|
|
@@ -199,7 +224,7 @@ function stripBedrockCacheControl(messages) {
|
|
|
199
224
|
if (!Array.isArray(content) || !hasBedrockCachePoint(content)) {
|
|
200
225
|
continue;
|
|
201
226
|
}
|
|
202
|
-
const message =
|
|
227
|
+
const message = shallowCloneMessage(originalMessage);
|
|
203
228
|
updatedMessages[i] = message;
|
|
204
229
|
message.content = message.content.filter((block) => !isCachePoint(block));
|
|
205
230
|
}
|
|
@@ -239,68 +264,67 @@ function addBedrockCacheControl(messages) {
|
|
|
239
264
|
if (!needsStripping && !needsCacheAdd) {
|
|
240
265
|
continue;
|
|
241
266
|
}
|
|
242
|
-
|
|
243
|
-
updatedMessages[i] = message;
|
|
267
|
+
let workingContent;
|
|
244
268
|
if (hasArrayContent) {
|
|
245
|
-
|
|
246
|
-
for (let j = 0; j <
|
|
247
|
-
const block =
|
|
269
|
+
workingContent = deepCloneContent(content).filter((block) => !isCachePoint(block));
|
|
270
|
+
for (let j = 0; j < workingContent.length; j++) {
|
|
271
|
+
const block = workingContent[j];
|
|
248
272
|
if ('cache_control' in block) {
|
|
249
273
|
delete block.cache_control;
|
|
250
274
|
}
|
|
251
275
|
}
|
|
252
276
|
}
|
|
277
|
+
else if (typeof content === 'string') {
|
|
278
|
+
workingContent = [{ type: _enum.ContentTypes.TEXT, text: content }];
|
|
279
|
+
}
|
|
280
|
+
else {
|
|
281
|
+
workingContent = [];
|
|
282
|
+
}
|
|
253
283
|
if (messagesModified >= 2 || isToolMessage || isEmptyString) {
|
|
254
|
-
|
|
284
|
+
updatedMessages[i] = shallowCloneMessage(originalMessage);
|
|
285
|
+
updatedMessages[i].content = workingContent;
|
|
255
286
|
continue;
|
|
256
287
|
}
|
|
257
|
-
if (
|
|
258
|
-
message.content = [
|
|
259
|
-
{ type: _enum.ContentTypes.TEXT, text: message.content },
|
|
260
|
-
{ cachePoint: { type: 'default' } },
|
|
261
|
-
];
|
|
262
|
-
messagesModified++;
|
|
263
|
-
syncLcKwargs(message);
|
|
288
|
+
if (workingContent.length === 0) {
|
|
264
289
|
continue;
|
|
265
290
|
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
if (block.
|
|
270
|
-
|
|
271
|
-
hasCacheableContent = true;
|
|
272
|
-
break;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
if (!hasCacheableContent) {
|
|
277
|
-
syncLcKwargs(message);
|
|
278
|
-
continue;
|
|
279
|
-
}
|
|
280
|
-
let inserted = false;
|
|
281
|
-
for (let j = message.content.length - 1; j >= 0; j--) {
|
|
282
|
-
const block = message.content[j];
|
|
283
|
-
const type = block.type;
|
|
284
|
-
if (type === _enum.ContentTypes.TEXT || type === 'text') {
|
|
285
|
-
const text = block.text;
|
|
286
|
-
if (text === '' || text === undefined) {
|
|
287
|
-
continue;
|
|
288
|
-
}
|
|
289
|
-
message.content.splice(j + 1, 0, {
|
|
290
|
-
cachePoint: { type: 'default' },
|
|
291
|
-
});
|
|
292
|
-
inserted = true;
|
|
291
|
+
let hasCacheableContent = false;
|
|
292
|
+
for (const block of workingContent) {
|
|
293
|
+
if (block.type === _enum.ContentTypes.TEXT) {
|
|
294
|
+
if (typeof block.text === 'string' && block.text !== '') {
|
|
295
|
+
hasCacheableContent = true;
|
|
293
296
|
break;
|
|
294
297
|
}
|
|
295
298
|
}
|
|
296
|
-
|
|
297
|
-
|
|
299
|
+
}
|
|
300
|
+
if (!hasCacheableContent) {
|
|
301
|
+
updatedMessages[i] = shallowCloneMessage(originalMessage);
|
|
302
|
+
updatedMessages[i].content = workingContent;
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
let inserted = false;
|
|
306
|
+
for (let j = workingContent.length - 1; j >= 0; j--) {
|
|
307
|
+
const block = workingContent[j];
|
|
308
|
+
const type = block.type;
|
|
309
|
+
if (type === _enum.ContentTypes.TEXT || type === 'text') {
|
|
310
|
+
const text = block.text;
|
|
311
|
+
if (text === '' || text === undefined) {
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
workingContent.splice(j + 1, 0, {
|
|
298
315
|
cachePoint: { type: 'default' },
|
|
299
316
|
});
|
|
317
|
+
inserted = true;
|
|
318
|
+
break;
|
|
300
319
|
}
|
|
301
|
-
messagesModified++;
|
|
302
320
|
}
|
|
303
|
-
|
|
321
|
+
if (!inserted) {
|
|
322
|
+
workingContent.push({
|
|
323
|
+
cachePoint: { type: 'default' },
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
updatedMessages[i] = createNewMessage(originalMessage, workingContent);
|
|
327
|
+
messagesModified++;
|
|
304
328
|
}
|
|
305
329
|
return updatedMessages;
|
|
306
330
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.cjs","sources":["../../../src/messages/cache.ts"],"sourcesContent":["import { BaseMessage, MessageContentComplex } from '@langchain/core/messages';\nimport type { AnthropicMessage } from '@/types/messages';\nimport type Anthropic from '@anthropic-ai/sdk';\nimport { ContentTypes } from '@/common/enum';\n\ntype MessageWithContent = {\n content?: string | MessageContentComplex[];\n};\n\n/**\n * Deep clones a message's content to prevent mutation of the original.\n * Handles both string and array content types.\n */\nfunction deepCloneContent<T extends string | MessageContentComplex[]>(\n content: T\n): T {\n if (typeof content === 'string') {\n return content;\n }\n if (Array.isArray(content)) {\n return content.map((block) => ({ ...block })) as T;\n }\n return content;\n}\n\n/**\n * Clones a message with deep-cloned content.\n * For LangChain messages, also syncs lc_kwargs.content.\n */\nfunction cloneMessageWithContent<T extends MessageWithContent>(message: T): T {\n if (message.content === undefined) {\n return { ...message };\n }\n\n const clonedContent = deepCloneContent(message.content);\n const cloned = {\n ...message,\n content: clonedContent,\n } as T;\n\n const lcKwargs = (cloned as Record<string, unknown>).lc_kwargs as\n | Record<string, unknown>\n | undefined;\n if (lcKwargs != null) {\n (cloned as Record<string, unknown>).lc_kwargs = {\n ...lcKwargs,\n content: clonedContent,\n };\n }\n\n return cloned;\n}\n\n/**\n * Syncs lc_kwargs.content with message.content after modifications.\n * Call this after all content modifications are done.\n */\nfunction syncLcKwargs<T extends MessageWithContent>(message: T): void {\n const lcKwargs = (message as Record<string, unknown>).lc_kwargs as\n | Record<string, unknown>\n | undefined;\n if (lcKwargs != null) {\n lcKwargs.content = message.content;\n }\n}\n\n/**\n * Checks if a message's content needs cache control stripping.\n * Returns true if content has cachePoint blocks or cache_control fields.\n */\nfunction needsCacheStripping(content: MessageContentComplex[]): boolean {\n for (let i = 0; i < content.length; i++) {\n const block = content[i];\n if (isCachePoint(block)) return true;\n if ('cache_control' in block) return true;\n }\n return false;\n}\n\n/**\n * Anthropic API: Adds cache control to the appropriate user messages in the payload.\n * Strips ALL existing cache control (both Anthropic and Bedrock formats) from all messages,\n * then adds fresh cache control to the last 2 user messages in a single backward pass.\n * This ensures we don't accumulate stale cache points across multiple turns.\n * Returns a new array - only clones messages that require modification.\n * @param messages - The array of message objects.\n * @returns - A new array of message objects with cache control added.\n */\nexport function addCacheControl<T extends AnthropicMessage | BaseMessage>(\n messages: T[]\n): T[] {\n if (!Array.isArray(messages) || messages.length < 2) {\n return messages;\n }\n\n const updatedMessages: T[] = [...messages];\n let userMessagesModified = 0;\n\n for (let i = updatedMessages.length - 1; i >= 0; i--) {\n const originalMessage = updatedMessages[i];\n const isUserMessage =\n ('getType' in originalMessage && originalMessage.getType() === 'human') ||\n ('role' in originalMessage && originalMessage.role === 'user');\n\n const hasArrayContent = Array.isArray(originalMessage.content);\n const needsStripping =\n hasArrayContent &&\n needsCacheStripping(originalMessage.content as MessageContentComplex[]);\n const needsCacheAdd =\n userMessagesModified < 2 &&\n isUserMessage &&\n (typeof originalMessage.content === 'string' || hasArrayContent);\n\n if (!needsStripping && !needsCacheAdd) {\n continue;\n }\n\n const message = cloneMessageWithContent(\n originalMessage as MessageWithContent\n ) as T;\n updatedMessages[i] = message;\n\n if (hasArrayContent) {\n message.content = (message.content as MessageContentComplex[]).filter(\n (block) => !isCachePoint(block as MessageContentComplex)\n ) as typeof message.content;\n\n for (\n let j = 0;\n j < (message.content as MessageContentComplex[]).length;\n j++\n ) {\n const block = (message.content as MessageContentComplex[])[j] as Record<\n string,\n unknown\n >;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n }\n\n if (userMessagesModified >= 2 || !isUserMessage) {\n syncLcKwargs(message);\n continue;\n }\n\n if (typeof message.content === 'string') {\n message.content = [\n {\n type: 'text',\n text: message.content,\n cache_control: { type: 'ephemeral' },\n },\n ];\n userMessagesModified++;\n } else if (Array.isArray(message.content)) {\n for (let j = message.content.length - 1; j >= 0; j--) {\n const contentPart = message.content[j];\n if ('type' in contentPart && contentPart.type === 'text') {\n (contentPart as Anthropic.TextBlockParam).cache_control = {\n type: 'ephemeral',\n };\n userMessagesModified++;\n break;\n }\n }\n }\n\n syncLcKwargs(message);\n }\n\n return updatedMessages;\n}\n\n/**\n * Checks if a content block is a cache point\n */\nfunction isCachePoint(block: MessageContentComplex): boolean {\n return 'cachePoint' in block && !('type' in block);\n}\n\n/**\n * Checks if a message's content has Anthropic cache_control fields.\n */\nfunction hasAnthropicCacheControl(content: MessageContentComplex[]): boolean {\n for (let i = 0; i < content.length; i++) {\n if ('cache_control' in content[i]) return true;\n }\n return false;\n}\n\n/**\n * Removes all Anthropic cache_control fields from messages\n * Used when switching from Anthropic to Bedrock provider\n * Returns a new array - only clones messages that require modification.\n */\nexport function stripAnthropicCacheControl<T extends MessageWithContent>(\n messages: T[]\n): T[] {\n if (!Array.isArray(messages)) {\n return messages;\n }\n\n const updatedMessages: T[] = [...messages];\n\n for (let i = 0; i < updatedMessages.length; i++) {\n const originalMessage = updatedMessages[i];\n const content = originalMessage.content;\n\n if (!Array.isArray(content) || !hasAnthropicCacheControl(content)) {\n continue;\n }\n\n const message = cloneMessageWithContent(originalMessage);\n updatedMessages[i] = message;\n\n for (\n let j = 0;\n j < (message.content as MessageContentComplex[]).length;\n j++\n ) {\n const block = (message.content as MessageContentComplex[])[j] as Record<\n string,\n unknown\n >;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n }\n\n return updatedMessages;\n}\n\n/**\n * Checks if a message's content has Bedrock cachePoint blocks.\n */\nfunction hasBedrockCachePoint(content: MessageContentComplex[]): boolean {\n for (let i = 0; i < content.length; i++) {\n if (isCachePoint(content[i])) return true;\n }\n return false;\n}\n\n/**\n * Removes all Bedrock cachePoint blocks from messages\n * Used when switching from Bedrock to Anthropic provider\n * Returns a new array - only clones messages that require modification.\n */\nexport function stripBedrockCacheControl<T extends MessageWithContent>(\n messages: T[]\n): T[] {\n if (!Array.isArray(messages)) {\n return messages;\n }\n\n const updatedMessages: T[] = [...messages];\n\n for (let i = 0; i < updatedMessages.length; i++) {\n const originalMessage = updatedMessages[i];\n const content = originalMessage.content;\n\n if (!Array.isArray(content) || !hasBedrockCachePoint(content)) {\n continue;\n }\n\n const message = cloneMessageWithContent(originalMessage);\n updatedMessages[i] = message;\n\n message.content = (message.content as MessageContentComplex[]).filter(\n (block) => !isCachePoint(block as MessageContentComplex)\n ) as typeof content;\n }\n\n return updatedMessages;\n}\n\n/**\n * Adds Bedrock Converse API cache points to the last two messages.\n * Inserts `{ cachePoint: { type: 'default' } }` as a separate content block\n * immediately after the last text block in each targeted message.\n * Strips ALL existing cache control (both Bedrock and Anthropic formats) from all messages,\n * then adds fresh cache points to the last 2 messages in a single backward pass.\n * This ensures we don't accumulate stale cache points across multiple turns.\n * Returns a new array - only clones messages that require modification.\n * @param messages - The array of message objects.\n * @returns - A new array of message objects with cache points added.\n */\nexport function addBedrockCacheControl<\n T extends Partial<BaseMessage> & MessageWithContent,\n>(messages: T[]): T[] {\n if (!Array.isArray(messages) || messages.length < 2) {\n return messages;\n }\n\n const updatedMessages: T[] = [...messages];\n let messagesModified = 0;\n\n for (let i = updatedMessages.length - 1; i >= 0; i--) {\n const originalMessage = updatedMessages[i];\n const isToolMessage =\n 'getType' in originalMessage &&\n typeof originalMessage.getType === 'function' &&\n originalMessage.getType() === 'tool';\n\n const content = originalMessage.content;\n const hasArrayContent = Array.isArray(content);\n const needsStripping =\n hasArrayContent &&\n needsCacheStripping(content as MessageContentComplex[]);\n const isEmptyString = typeof content === 'string' && content === '';\n const needsCacheAdd =\n messagesModified < 2 &&\n !isToolMessage &&\n !isEmptyString &&\n (typeof content === 'string' || hasArrayContent);\n\n if (!needsStripping && !needsCacheAdd) {\n continue;\n }\n\n const message = cloneMessageWithContent(originalMessage);\n updatedMessages[i] = message;\n\n if (hasArrayContent) {\n message.content = (message.content as MessageContentComplex[]).filter(\n (block) => !isCachePoint(block)\n ) as typeof content;\n\n for (\n let j = 0;\n j < (message.content as MessageContentComplex[]).length;\n j++\n ) {\n const block = (message.content as MessageContentComplex[])[j] as Record<\n string,\n unknown\n >;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n }\n\n if (messagesModified >= 2 || isToolMessage || isEmptyString) {\n syncLcKwargs(message);\n continue;\n }\n\n if (typeof message.content === 'string') {\n message.content = [\n { type: ContentTypes.TEXT, text: message.content },\n { cachePoint: { type: 'default' } },\n ] as MessageContentComplex[];\n messagesModified++;\n syncLcKwargs(message);\n continue;\n }\n\n if (Array.isArray(message.content)) {\n let hasCacheableContent = false;\n for (const block of message.content) {\n if (block.type === ContentTypes.TEXT) {\n if (typeof block.text === 'string' && block.text !== '') {\n hasCacheableContent = true;\n break;\n }\n }\n }\n\n if (!hasCacheableContent) {\n syncLcKwargs(message);\n continue;\n }\n\n let inserted = false;\n for (let j = message.content.length - 1; j >= 0; j--) {\n const block = message.content[j] as MessageContentComplex;\n const type = (block as { type?: string }).type;\n if (type === ContentTypes.TEXT || type === 'text') {\n const text = (block as { text?: string }).text;\n if (text === '' || text === undefined) {\n continue;\n }\n message.content.splice(j + 1, 0, {\n cachePoint: { type: 'default' },\n } as MessageContentComplex);\n inserted = true;\n break;\n }\n }\n if (!inserted) {\n message.content.push({\n cachePoint: { type: 'default' },\n } as MessageContentComplex);\n }\n messagesModified++;\n }\n\n syncLcKwargs(message);\n }\n\n return updatedMessages;\n}\n"],"names":["ContentTypes"],"mappings":";;;;AASA;;;AAGG;AACH,SAAS,gBAAgB,CACvB,OAAU,EAAA;AAEV,IAAA,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;AAC/B,QAAA,OAAO,OAAO;;AAEhB,IAAA,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE;AAC1B,QAAA,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,MAAM,EAAE,GAAG,KAAK,EAAE,CAAC,CAAM;;AAEpD,IAAA,OAAO,OAAO;AACh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KAAK,SAAS,EAAE;wBACrC;;oBAEF,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE;AAC/B,wBAAA,UAAU,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;AACP,qBAAA,CAAC;oBAC3B,QAAQ,GAAG,IAAI;oBACf;;;YAGJ,IAAI,CAAC,QAAQ,EAAE;AACb,gBAAA,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC;AACnB,oBAAA,UAAU,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;AACP,iBAAA,CAAC;;AAE7B,YAAA,gBAAgB,EAAE;;QAGpB,YAAY,CAAC,OAAO,CAAC;;AAGvB,IAAA,OAAO,eAAe;AACxB;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"cache.cjs","sources":["../../../src/messages/cache.ts"],"sourcesContent":["import {\n BaseMessage,\n MessageContentComplex,\n AIMessage,\n HumanMessage,\n SystemMessage,\n ToolMessage,\n} from '@langchain/core/messages';\nimport type { AnthropicMessage } from '@/types/messages';\nimport type Anthropic from '@anthropic-ai/sdk';\nimport { ContentTypes } from '@/common/enum';\n\ntype MessageWithContent = {\n content?: string | MessageContentComplex[];\n};\n\n/**\n * Deep clones a message's content to prevent mutation of the original.\n */\nfunction deepCloneContent<T extends string | MessageContentComplex[]>(\n content: T\n): T {\n if (typeof content === 'string') {\n return content;\n }\n if (Array.isArray(content)) {\n return content.map((block) => ({ ...block })) as T;\n }\n return content;\n}\n\n/**\n * Simple shallow clone with deep-cloned content.\n * Used for stripping cache control where we don't need proper LangChain instances.\n */\nfunction shallowCloneMessage<T extends MessageWithContent>(message: T): T {\n return {\n ...message,\n content: deepCloneContent(message.content ?? ''),\n } as T;\n}\n\n/**\n * Creates a new LangChain message instance with the given content.\n * Required when adding cache points to ensure proper serialization.\n */\nfunction createNewMessage<T extends MessageWithContent>(\n message: T,\n content: MessageContentComplex[]\n): T {\n if ('getType' in message && typeof message.getType === 'function') {\n const baseMsg = message as unknown as BaseMessage;\n const msgType = baseMsg.getType();\n\n const baseFields = {\n content,\n name: baseMsg.name,\n additional_kwargs: { ...baseMsg.additional_kwargs },\n response_metadata: { ...baseMsg.response_metadata },\n id: baseMsg.id,\n };\n\n switch (msgType) {\n case 'human':\n return new HumanMessage(baseFields) as unknown as T;\n case 'ai': {\n const aiMsg = baseMsg as AIMessage;\n return new AIMessage({\n ...baseFields,\n tool_calls: aiMsg.tool_calls ? [...aiMsg.tool_calls] : [],\n invalid_tool_calls: aiMsg.invalid_tool_calls\n ? [...aiMsg.invalid_tool_calls]\n : [],\n usage_metadata: aiMsg.usage_metadata,\n }) as unknown as T;\n }\n case 'system':\n return new SystemMessage(baseFields) as unknown as T;\n case 'tool': {\n const toolMsg = baseMsg as ToolMessage;\n return new ToolMessage({\n ...baseFields,\n tool_call_id: toolMsg.tool_call_id,\n status: toolMsg.status,\n artifact: toolMsg.artifact,\n }) as unknown as T;\n }\n default:\n break;\n }\n }\n\n const cloned = { ...message, content } as T;\n const lcKwargs = (cloned as Record<string, unknown>).lc_kwargs as\n | Record<string, unknown>\n | undefined;\n if (lcKwargs != null) {\n (cloned as Record<string, unknown>).lc_kwargs = { ...lcKwargs, content };\n }\n return cloned;\n}\n\n/**\n * Checks if a message's content needs cache control stripping.\n * Returns true if content has cachePoint blocks or cache_control fields.\n */\nfunction needsCacheStripping(content: MessageContentComplex[]): boolean {\n for (let i = 0; i < content.length; i++) {\n const block = content[i];\n if (isCachePoint(block)) return true;\n if ('cache_control' in block) return true;\n }\n return false;\n}\n\n/**\n * Anthropic API: Adds cache control to the appropriate user messages in the payload.\n * Strips ALL existing cache control (both Anthropic and Bedrock formats) from all messages,\n * then adds fresh cache control to the last 2 user messages in a single backward pass.\n * This ensures we don't accumulate stale cache points across multiple turns.\n * Returns a new array - only clones messages that require modification.\n * @param messages - The array of message objects.\n * @returns - A new array of message objects with cache control added.\n */\nexport function addCacheControl<T extends AnthropicMessage | BaseMessage>(\n messages: T[]\n): T[] {\n if (!Array.isArray(messages) || messages.length < 2) {\n return messages;\n }\n\n const updatedMessages: T[] = [...messages];\n let userMessagesModified = 0;\n\n for (let i = updatedMessages.length - 1; i >= 0; i--) {\n const originalMessage = updatedMessages[i];\n const isUserMessage =\n ('getType' in originalMessage && originalMessage.getType() === 'human') ||\n ('role' in originalMessage && originalMessage.role === 'user');\n\n const hasArrayContent = Array.isArray(originalMessage.content);\n const needsStripping =\n hasArrayContent &&\n needsCacheStripping(originalMessage.content as MessageContentComplex[]);\n const needsCacheAdd =\n userMessagesModified < 2 &&\n isUserMessage &&\n (typeof originalMessage.content === 'string' || hasArrayContent);\n\n if (!needsStripping && !needsCacheAdd) {\n continue;\n }\n\n const message = shallowCloneMessage(\n originalMessage as MessageWithContent\n ) as T;\n updatedMessages[i] = message;\n\n if (hasArrayContent) {\n message.content = (message.content as MessageContentComplex[]).filter(\n (block) => !isCachePoint(block as MessageContentComplex)\n ) as typeof message.content;\n\n for (\n let j = 0;\n j < (message.content as MessageContentComplex[]).length;\n j++\n ) {\n const block = (message.content as MessageContentComplex[])[j] as Record<\n string,\n unknown\n >;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n }\n\n if (userMessagesModified >= 2 || !isUserMessage) {\n continue;\n }\n\n if (typeof message.content === 'string') {\n message.content = [\n {\n type: 'text',\n text: message.content,\n cache_control: { type: 'ephemeral' },\n },\n ];\n userMessagesModified++;\n } else if (Array.isArray(message.content)) {\n for (let j = message.content.length - 1; j >= 0; j--) {\n const contentPart = message.content[j];\n if ('type' in contentPart && contentPart.type === 'text') {\n (contentPart as Anthropic.TextBlockParam).cache_control = {\n type: 'ephemeral',\n };\n userMessagesModified++;\n break;\n }\n }\n }\n }\n\n return updatedMessages;\n}\n\n/**\n * Checks if a content block is a cache point\n */\nfunction isCachePoint(block: MessageContentComplex): boolean {\n return 'cachePoint' in block && !('type' in block);\n}\n\n/**\n * Checks if a message's content has Anthropic cache_control fields.\n */\nfunction hasAnthropicCacheControl(content: MessageContentComplex[]): boolean {\n for (let i = 0; i < content.length; i++) {\n if ('cache_control' in content[i]) return true;\n }\n return false;\n}\n\n/**\n * Removes all Anthropic cache_control fields from messages\n * Used when switching from Anthropic to Bedrock provider\n * Returns a new array - only clones messages that require modification.\n */\nexport function stripAnthropicCacheControl<T extends MessageWithContent>(\n messages: T[]\n): T[] {\n if (!Array.isArray(messages)) {\n return messages;\n }\n\n const updatedMessages: T[] = [...messages];\n\n for (let i = 0; i < updatedMessages.length; i++) {\n const originalMessage = updatedMessages[i];\n const content = originalMessage.content;\n\n if (!Array.isArray(content) || !hasAnthropicCacheControl(content)) {\n continue;\n }\n\n const message = shallowCloneMessage(originalMessage);\n updatedMessages[i] = message;\n\n for (\n let j = 0;\n j < (message.content as MessageContentComplex[]).length;\n j++\n ) {\n const block = (message.content as MessageContentComplex[])[j] as Record<\n string,\n unknown\n >;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n }\n\n return updatedMessages;\n}\n\n/**\n * Checks if a message's content has Bedrock cachePoint blocks.\n */\nfunction hasBedrockCachePoint(content: MessageContentComplex[]): boolean {\n for (let i = 0; i < content.length; i++) {\n if (isCachePoint(content[i])) return true;\n }\n return false;\n}\n\n/**\n * Removes all Bedrock cachePoint blocks from messages\n * Used when switching from Bedrock to Anthropic provider\n * Returns a new array - only clones messages that require modification.\n */\nexport function stripBedrockCacheControl<T extends MessageWithContent>(\n messages: T[]\n): T[] {\n if (!Array.isArray(messages)) {\n return messages;\n }\n\n const updatedMessages: T[] = [...messages];\n\n for (let i = 0; i < updatedMessages.length; i++) {\n const originalMessage = updatedMessages[i];\n const content = originalMessage.content;\n\n if (!Array.isArray(content) || !hasBedrockCachePoint(content)) {\n continue;\n }\n\n const message = shallowCloneMessage(originalMessage);\n updatedMessages[i] = message;\n\n message.content = (message.content as MessageContentComplex[]).filter(\n (block) => !isCachePoint(block as MessageContentComplex)\n ) as typeof content;\n }\n\n return updatedMessages;\n}\n\n/**\n * Adds Bedrock Converse API cache points to the last two messages.\n * Inserts `{ cachePoint: { type: 'default' } }` as a separate content block\n * immediately after the last text block in each targeted message.\n * Strips ALL existing cache control (both Bedrock and Anthropic formats) from all messages,\n * then adds fresh cache points to the last 2 messages in a single backward pass.\n * This ensures we don't accumulate stale cache points across multiple turns.\n * Returns a new array - only clones messages that require modification.\n * @param messages - The array of message objects.\n * @returns - A new array of message objects with cache points added.\n */\nexport function addBedrockCacheControl<\n T extends Partial<BaseMessage> & MessageWithContent,\n>(messages: T[]): T[] {\n if (!Array.isArray(messages) || messages.length < 2) {\n return messages;\n }\n\n const updatedMessages: T[] = [...messages];\n let messagesModified = 0;\n\n for (let i = updatedMessages.length - 1; i >= 0; i--) {\n const originalMessage = updatedMessages[i];\n const isToolMessage =\n 'getType' in originalMessage &&\n typeof originalMessage.getType === 'function' &&\n originalMessage.getType() === 'tool';\n\n const content = originalMessage.content;\n const hasArrayContent = Array.isArray(content);\n const needsStripping =\n hasArrayContent &&\n needsCacheStripping(content as MessageContentComplex[]);\n const isEmptyString = typeof content === 'string' && content === '';\n const needsCacheAdd =\n messagesModified < 2 &&\n !isToolMessage &&\n !isEmptyString &&\n (typeof content === 'string' || hasArrayContent);\n\n if (!needsStripping && !needsCacheAdd) {\n continue;\n }\n\n let workingContent: MessageContentComplex[];\n\n if (hasArrayContent) {\n workingContent = deepCloneContent(\n content as MessageContentComplex[]\n ).filter((block) => !isCachePoint(block));\n\n for (let j = 0; j < workingContent.length; j++) {\n const block = workingContent[j] as Record<string, unknown>;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n } else if (typeof content === 'string') {\n workingContent = [{ type: ContentTypes.TEXT, text: content }];\n } else {\n workingContent = [];\n }\n\n if (messagesModified >= 2 || isToolMessage || isEmptyString) {\n updatedMessages[i] = shallowCloneMessage(originalMessage);\n (updatedMessages[i] as MessageWithContent).content = workingContent;\n continue;\n }\n\n if (workingContent.length === 0) {\n continue;\n }\n\n let hasCacheableContent = false;\n for (const block of workingContent) {\n if (block.type === ContentTypes.TEXT) {\n if (typeof block.text === 'string' && block.text !== '') {\n hasCacheableContent = true;\n break;\n }\n }\n }\n\n if (!hasCacheableContent) {\n updatedMessages[i] = shallowCloneMessage(originalMessage);\n (updatedMessages[i] as MessageWithContent).content = workingContent;\n continue;\n }\n\n let inserted = false;\n for (let j = workingContent.length - 1; j >= 0; j--) {\n const block = workingContent[j] as MessageContentComplex;\n const type = (block as { type?: string }).type;\n if (type === ContentTypes.TEXT || type === 'text') {\n const text = (block as { text?: string }).text;\n if (text === '' || text === undefined) {\n continue;\n }\n workingContent.splice(j + 1, 0, {\n cachePoint: { type: 'default' },\n } as MessageContentComplex);\n inserted = true;\n break;\n }\n }\n if (!inserted) {\n workingContent.push({\n cachePoint: { type: 'default' },\n } as MessageContentComplex);\n }\n\n updatedMessages[i] = createNewMessage(originalMessage, workingContent);\n messagesModified++;\n }\n\n return updatedMessages;\n}\n"],"names":["HumanMessage","AIMessage","SystemMessage","ToolMessage","ContentTypes"],"mappings":";;;;;AAgBA;;AAEG;AACH,SAAS,gBAAgB,CACvB,OAAU,EAAA;AAEV,IAAA,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;AAC/B,QAAA,OAAO,OAAO;;AAEhB,IAAA,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE;AAC1B,QAAA,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,MAAM,EAAE,GAAG,KAAK,EAAE,CAAC,CAAM;;AAEpD,IAAA,OAAO,OAAO;AAChB;AAEA;;;AAGG;AACH,SAAS,mBAAmB,CAA+B,OAAU,EAAA;IACnE,OAAO;AACL,QAAA,GAAG,OAAO;QACV,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,OAAO,IAAI,EAAE,CAAC;KAC5C;AACR;AAEA;;;AAGG;AACH,SAAS,gBAAgB,CACvB,OAAU,EACV,OAAgC,EAAA;IAEhC,IAAI,SAAS,IAAI,OAAO,IAAI,OAAO,OAAO,CAAC,OAA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,CAAC,CAAC,CAAC,GAAG,mBAAmB,CAAC,eAAe,CAAC;AACxD,YAAA,eAAe,CAAC,CAAC,CAAwB,CAAC,OAAO,GAAG,cAAc;YACnE;;QAGF,IAAI,QAAQ,GAAG,KAAK;AACpB,QAAA,KAAK,IAAI,CAAC,GAAG,cAAc,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE;AACnD,YAAA,MAAM,KAAK,GAAG,cAAc,CAAC,CAAC,CAA0B;AACxD,YAAA,MAAM,IAAI,GAAI,KAA2B,CAAC,IAAI;YAC9C,IAAI,IAAI,KAAKA,kBAAY,CAAC,IAAI,IAAI,IAAI,KAAK,MAAM,EAAE;AACjD,gBAAA,MAAM,IAAI,GAAI,KAA2B,CAAC,IAAI;gBAC9C,IAAI,IAAI,KAAK,EAAE,IAAI,IAAI,KAAK,SAAS,EAAE;oBACrC;;gBAEF,cAAc,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE;AAC9B,oBAAA,UAAU,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;AACP,iBAAA,CAAC;gBAC3B,QAAQ,GAAG,IAAI;gBACf;;;QAGJ,IAAI,CAAC,QAAQ,EAAE;YACb,cAAc,CAAC,IAAI,CAAC;AAClB,gBAAA,UAAU,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;AACP,aAAA,CAAC;;QAG7B,eAAe,CAAC,CAAC,CAAC,GAAG,gBAAgB,CAAC,eAAe,EAAE,cAAc,CAAC;AACtE,QAAA,gBAAgB,EAAE;;AAGpB,IAAA,OAAO,eAAe;AACxB;;;;;;;"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
import { ToolMessage, SystemMessage, AIMessage, HumanMessage } from '@langchain/core/messages';
|
|
1
2
|
import { ContentTypes } from '../common/enum.mjs';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Deep clones a message's content to prevent mutation of the original.
|
|
5
|
-
* Handles both string and array content types.
|
|
6
6
|
*/
|
|
7
7
|
function deepCloneContent(content) {
|
|
8
8
|
if (typeof content === 'string') {
|
|
@@ -14,36 +14,63 @@ function deepCloneContent(content) {
|
|
|
14
14
|
return content;
|
|
15
15
|
}
|
|
16
16
|
/**
|
|
17
|
-
*
|
|
18
|
-
*
|
|
17
|
+
* Simple shallow clone with deep-cloned content.
|
|
18
|
+
* Used for stripping cache control where we don't need proper LangChain instances.
|
|
19
19
|
*/
|
|
20
|
-
function
|
|
21
|
-
|
|
22
|
-
return { ...message };
|
|
23
|
-
}
|
|
24
|
-
const clonedContent = deepCloneContent(message.content);
|
|
25
|
-
const cloned = {
|
|
20
|
+
function shallowCloneMessage(message) {
|
|
21
|
+
return {
|
|
26
22
|
...message,
|
|
27
|
-
content:
|
|
23
|
+
content: deepCloneContent(message.content ?? ''),
|
|
28
24
|
};
|
|
29
|
-
const lcKwargs = cloned.lc_kwargs;
|
|
30
|
-
if (lcKwargs != null) {
|
|
31
|
-
cloned.lc_kwargs = {
|
|
32
|
-
...lcKwargs,
|
|
33
|
-
content: clonedContent,
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
return cloned;
|
|
37
25
|
}
|
|
38
26
|
/**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
27
|
+
* Creates a new LangChain message instance with the given content.
|
|
28
|
+
* Required when adding cache points to ensure proper serialization.
|
|
41
29
|
*/
|
|
42
|
-
function
|
|
43
|
-
|
|
30
|
+
function createNewMessage(message, content) {
|
|
31
|
+
if ('getType' in message && typeof message.getType === 'function') {
|
|
32
|
+
const baseMsg = message;
|
|
33
|
+
const msgType = baseMsg.getType();
|
|
34
|
+
const baseFields = {
|
|
35
|
+
content,
|
|
36
|
+
name: baseMsg.name,
|
|
37
|
+
additional_kwargs: { ...baseMsg.additional_kwargs },
|
|
38
|
+
response_metadata: { ...baseMsg.response_metadata },
|
|
39
|
+
id: baseMsg.id,
|
|
40
|
+
};
|
|
41
|
+
switch (msgType) {
|
|
42
|
+
case 'human':
|
|
43
|
+
return new HumanMessage(baseFields);
|
|
44
|
+
case 'ai': {
|
|
45
|
+
const aiMsg = baseMsg;
|
|
46
|
+
return new AIMessage({
|
|
47
|
+
...baseFields,
|
|
48
|
+
tool_calls: aiMsg.tool_calls ? [...aiMsg.tool_calls] : [],
|
|
49
|
+
invalid_tool_calls: aiMsg.invalid_tool_calls
|
|
50
|
+
? [...aiMsg.invalid_tool_calls]
|
|
51
|
+
: [],
|
|
52
|
+
usage_metadata: aiMsg.usage_metadata,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
case 'system':
|
|
56
|
+
return new SystemMessage(baseFields);
|
|
57
|
+
case 'tool': {
|
|
58
|
+
const toolMsg = baseMsg;
|
|
59
|
+
return new ToolMessage({
|
|
60
|
+
...baseFields,
|
|
61
|
+
tool_call_id: toolMsg.tool_call_id,
|
|
62
|
+
status: toolMsg.status,
|
|
63
|
+
artifact: toolMsg.artifact,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const cloned = { ...message, content };
|
|
69
|
+
const lcKwargs = cloned.lc_kwargs;
|
|
44
70
|
if (lcKwargs != null) {
|
|
45
|
-
|
|
71
|
+
cloned.lc_kwargs = { ...lcKwargs, content };
|
|
46
72
|
}
|
|
73
|
+
return cloned;
|
|
47
74
|
}
|
|
48
75
|
/**
|
|
49
76
|
* Checks if a message's content needs cache control stripping.
|
|
@@ -87,7 +114,7 @@ function addCacheControl(messages) {
|
|
|
87
114
|
if (!needsStripping && !needsCacheAdd) {
|
|
88
115
|
continue;
|
|
89
116
|
}
|
|
90
|
-
const message =
|
|
117
|
+
const message = shallowCloneMessage(originalMessage);
|
|
91
118
|
updatedMessages[i] = message;
|
|
92
119
|
if (hasArrayContent) {
|
|
93
120
|
message.content = message.content.filter((block) => !isCachePoint(block));
|
|
@@ -99,7 +126,6 @@ function addCacheControl(messages) {
|
|
|
99
126
|
}
|
|
100
127
|
}
|
|
101
128
|
if (userMessagesModified >= 2 || !isUserMessage) {
|
|
102
|
-
syncLcKwargs(message);
|
|
103
129
|
continue;
|
|
104
130
|
}
|
|
105
131
|
if (typeof message.content === 'string') {
|
|
@@ -124,7 +150,6 @@ function addCacheControl(messages) {
|
|
|
124
150
|
}
|
|
125
151
|
}
|
|
126
152
|
}
|
|
127
|
-
syncLcKwargs(message);
|
|
128
153
|
}
|
|
129
154
|
return updatedMessages;
|
|
130
155
|
}
|
|
@@ -160,7 +185,7 @@ function stripAnthropicCacheControl(messages) {
|
|
|
160
185
|
if (!Array.isArray(content) || !hasAnthropicCacheControl(content)) {
|
|
161
186
|
continue;
|
|
162
187
|
}
|
|
163
|
-
const message =
|
|
188
|
+
const message = shallowCloneMessage(originalMessage);
|
|
164
189
|
updatedMessages[i] = message;
|
|
165
190
|
for (let j = 0; j < message.content.length; j++) {
|
|
166
191
|
const block = message.content[j];
|
|
@@ -197,7 +222,7 @@ function stripBedrockCacheControl(messages) {
|
|
|
197
222
|
if (!Array.isArray(content) || !hasBedrockCachePoint(content)) {
|
|
198
223
|
continue;
|
|
199
224
|
}
|
|
200
|
-
const message =
|
|
225
|
+
const message = shallowCloneMessage(originalMessage);
|
|
201
226
|
updatedMessages[i] = message;
|
|
202
227
|
message.content = message.content.filter((block) => !isCachePoint(block));
|
|
203
228
|
}
|
|
@@ -237,68 +262,67 @@ function addBedrockCacheControl(messages) {
|
|
|
237
262
|
if (!needsStripping && !needsCacheAdd) {
|
|
238
263
|
continue;
|
|
239
264
|
}
|
|
240
|
-
|
|
241
|
-
updatedMessages[i] = message;
|
|
265
|
+
let workingContent;
|
|
242
266
|
if (hasArrayContent) {
|
|
243
|
-
|
|
244
|
-
for (let j = 0; j <
|
|
245
|
-
const block =
|
|
267
|
+
workingContent = deepCloneContent(content).filter((block) => !isCachePoint(block));
|
|
268
|
+
for (let j = 0; j < workingContent.length; j++) {
|
|
269
|
+
const block = workingContent[j];
|
|
246
270
|
if ('cache_control' in block) {
|
|
247
271
|
delete block.cache_control;
|
|
248
272
|
}
|
|
249
273
|
}
|
|
250
274
|
}
|
|
275
|
+
else if (typeof content === 'string') {
|
|
276
|
+
workingContent = [{ type: ContentTypes.TEXT, text: content }];
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
workingContent = [];
|
|
280
|
+
}
|
|
251
281
|
if (messagesModified >= 2 || isToolMessage || isEmptyString) {
|
|
252
|
-
|
|
282
|
+
updatedMessages[i] = shallowCloneMessage(originalMessage);
|
|
283
|
+
updatedMessages[i].content = workingContent;
|
|
253
284
|
continue;
|
|
254
285
|
}
|
|
255
|
-
if (
|
|
256
|
-
message.content = [
|
|
257
|
-
{ type: ContentTypes.TEXT, text: message.content },
|
|
258
|
-
{ cachePoint: { type: 'default' } },
|
|
259
|
-
];
|
|
260
|
-
messagesModified++;
|
|
261
|
-
syncLcKwargs(message);
|
|
286
|
+
if (workingContent.length === 0) {
|
|
262
287
|
continue;
|
|
263
288
|
}
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
if (block.
|
|
268
|
-
|
|
269
|
-
hasCacheableContent = true;
|
|
270
|
-
break;
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
if (!hasCacheableContent) {
|
|
275
|
-
syncLcKwargs(message);
|
|
276
|
-
continue;
|
|
277
|
-
}
|
|
278
|
-
let inserted = false;
|
|
279
|
-
for (let j = message.content.length - 1; j >= 0; j--) {
|
|
280
|
-
const block = message.content[j];
|
|
281
|
-
const type = block.type;
|
|
282
|
-
if (type === ContentTypes.TEXT || type === 'text') {
|
|
283
|
-
const text = block.text;
|
|
284
|
-
if (text === '' || text === undefined) {
|
|
285
|
-
continue;
|
|
286
|
-
}
|
|
287
|
-
message.content.splice(j + 1, 0, {
|
|
288
|
-
cachePoint: { type: 'default' },
|
|
289
|
-
});
|
|
290
|
-
inserted = true;
|
|
289
|
+
let hasCacheableContent = false;
|
|
290
|
+
for (const block of workingContent) {
|
|
291
|
+
if (block.type === ContentTypes.TEXT) {
|
|
292
|
+
if (typeof block.text === 'string' && block.text !== '') {
|
|
293
|
+
hasCacheableContent = true;
|
|
291
294
|
break;
|
|
292
295
|
}
|
|
293
296
|
}
|
|
294
|
-
|
|
295
|
-
|
|
297
|
+
}
|
|
298
|
+
if (!hasCacheableContent) {
|
|
299
|
+
updatedMessages[i] = shallowCloneMessage(originalMessage);
|
|
300
|
+
updatedMessages[i].content = workingContent;
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
let inserted = false;
|
|
304
|
+
for (let j = workingContent.length - 1; j >= 0; j--) {
|
|
305
|
+
const block = workingContent[j];
|
|
306
|
+
const type = block.type;
|
|
307
|
+
if (type === ContentTypes.TEXT || type === 'text') {
|
|
308
|
+
const text = block.text;
|
|
309
|
+
if (text === '' || text === undefined) {
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
workingContent.splice(j + 1, 0, {
|
|
296
313
|
cachePoint: { type: 'default' },
|
|
297
314
|
});
|
|
315
|
+
inserted = true;
|
|
316
|
+
break;
|
|
298
317
|
}
|
|
299
|
-
messagesModified++;
|
|
300
318
|
}
|
|
301
|
-
|
|
319
|
+
if (!inserted) {
|
|
320
|
+
workingContent.push({
|
|
321
|
+
cachePoint: { type: 'default' },
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
updatedMessages[i] = createNewMessage(originalMessage, workingContent);
|
|
325
|
+
messagesModified++;
|
|
302
326
|
}
|
|
303
327
|
return updatedMessages;
|
|
304
328
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.mjs","sources":["../../../src/messages/cache.ts"],"sourcesContent":["import { BaseMessage, MessageContentComplex } from '@langchain/core/messages';\nimport type { AnthropicMessage } from '@/types/messages';\nimport type Anthropic from '@anthropic-ai/sdk';\nimport { ContentTypes } from '@/common/enum';\n\ntype MessageWithContent = {\n content?: string | MessageContentComplex[];\n};\n\n/**\n * Deep clones a message's content to prevent mutation of the original.\n * Handles both string and array content types.\n */\nfunction deepCloneContent<T extends string | MessageContentComplex[]>(\n content: T\n): T {\n if (typeof content === 'string') {\n return content;\n }\n if (Array.isArray(content)) {\n return content.map((block) => ({ ...block })) as T;\n }\n return content;\n}\n\n/**\n * Clones a message with deep-cloned content.\n * For LangChain messages, also syncs lc_kwargs.content.\n */\nfunction cloneMessageWithContent<T extends MessageWithContent>(message: T): T {\n if (message.content === undefined) {\n return { ...message };\n }\n\n const clonedContent = deepCloneContent(message.content);\n const cloned = {\n ...message,\n content: clonedContent,\n } as T;\n\n const lcKwargs = (cloned as Record<string, unknown>).lc_kwargs as\n | Record<string, unknown>\n | undefined;\n if (lcKwargs != null) {\n (cloned as Record<string, unknown>).lc_kwargs = {\n ...lcKwargs,\n content: clonedContent,\n };\n }\n\n return cloned;\n}\n\n/**\n * Syncs lc_kwargs.content with message.content after modifications.\n * Call this after all content modifications are done.\n */\nfunction syncLcKwargs<T extends MessageWithContent>(message: T): void {\n const lcKwargs = (message as Record<string, unknown>).lc_kwargs as\n | Record<string, unknown>\n | undefined;\n if (lcKwargs != null) {\n lcKwargs.content = message.content;\n }\n}\n\n/**\n * Checks if a message's content needs cache control stripping.\n * Returns true if content has cachePoint blocks or cache_control fields.\n */\nfunction needsCacheStripping(content: MessageContentComplex[]): boolean {\n for (let i = 0; i < content.length; i++) {\n const block = content[i];\n if (isCachePoint(block)) return true;\n if ('cache_control' in block) return true;\n }\n return false;\n}\n\n/**\n * Anthropic API: Adds cache control to the appropriate user messages in the payload.\n * Strips ALL existing cache control (both Anthropic and Bedrock formats) from all messages,\n * then adds fresh cache control to the last 2 user messages in a single backward pass.\n * This ensures we don't accumulate stale cache points across multiple turns.\n * Returns a new array - only clones messages that require modification.\n * @param messages - The array of message objects.\n * @returns - A new array of message objects with cache control added.\n */\nexport function addCacheControl<T extends AnthropicMessage | BaseMessage>(\n messages: T[]\n): T[] {\n if (!Array.isArray(messages) || messages.length < 2) {\n return messages;\n }\n\n const updatedMessages: T[] = [...messages];\n let userMessagesModified = 0;\n\n for (let i = updatedMessages.length - 1; i >= 0; i--) {\n const originalMessage = updatedMessages[i];\n const isUserMessage =\n ('getType' in originalMessage && originalMessage.getType() === 'human') ||\n ('role' in originalMessage && originalMessage.role === 'user');\n\n const hasArrayContent = Array.isArray(originalMessage.content);\n const needsStripping =\n hasArrayContent &&\n needsCacheStripping(originalMessage.content as MessageContentComplex[]);\n const needsCacheAdd =\n userMessagesModified < 2 &&\n isUserMessage &&\n (typeof originalMessage.content === 'string' || hasArrayContent);\n\n if (!needsStripping && !needsCacheAdd) {\n continue;\n }\n\n const message = cloneMessageWithContent(\n originalMessage as MessageWithContent\n ) as T;\n updatedMessages[i] = message;\n\n if (hasArrayContent) {\n message.content = (message.content as MessageContentComplex[]).filter(\n (block) => !isCachePoint(block as MessageContentComplex)\n ) as typeof message.content;\n\n for (\n let j = 0;\n j < (message.content as MessageContentComplex[]).length;\n j++\n ) {\n const block = (message.content as MessageContentComplex[])[j] as Record<\n string,\n unknown\n >;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n }\n\n if (userMessagesModified >= 2 || !isUserMessage) {\n syncLcKwargs(message);\n continue;\n }\n\n if (typeof message.content === 'string') {\n message.content = [\n {\n type: 'text',\n text: message.content,\n cache_control: { type: 'ephemeral' },\n },\n ];\n userMessagesModified++;\n } else if (Array.isArray(message.content)) {\n for (let j = message.content.length - 1; j >= 0; j--) {\n const contentPart = message.content[j];\n if ('type' in contentPart && contentPart.type === 'text') {\n (contentPart as Anthropic.TextBlockParam).cache_control = {\n type: 'ephemeral',\n };\n userMessagesModified++;\n break;\n }\n }\n }\n\n syncLcKwargs(message);\n }\n\n return updatedMessages;\n}\n\n/**\n * Checks if a content block is a cache point\n */\nfunction isCachePoint(block: MessageContentComplex): boolean {\n return 'cachePoint' in block && !('type' in block);\n}\n\n/**\n * Checks if a message's content has Anthropic cache_control fields.\n */\nfunction hasAnthropicCacheControl(content: MessageContentComplex[]): boolean {\n for (let i = 0; i < content.length; i++) {\n if ('cache_control' in content[i]) return true;\n }\n return false;\n}\n\n/**\n * Removes all Anthropic cache_control fields from messages\n * Used when switching from Anthropic to Bedrock provider\n * Returns a new array - only clones messages that require modification.\n */\nexport function stripAnthropicCacheControl<T extends MessageWithContent>(\n messages: T[]\n): T[] {\n if (!Array.isArray(messages)) {\n return messages;\n }\n\n const updatedMessages: T[] = [...messages];\n\n for (let i = 0; i < updatedMessages.length; i++) {\n const originalMessage = updatedMessages[i];\n const content = originalMessage.content;\n\n if (!Array.isArray(content) || !hasAnthropicCacheControl(content)) {\n continue;\n }\n\n const message = cloneMessageWithContent(originalMessage);\n updatedMessages[i] = message;\n\n for (\n let j = 0;\n j < (message.content as MessageContentComplex[]).length;\n j++\n ) {\n const block = (message.content as MessageContentComplex[])[j] as Record<\n string,\n unknown\n >;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n }\n\n return updatedMessages;\n}\n\n/**\n * Checks if a message's content has Bedrock cachePoint blocks.\n */\nfunction hasBedrockCachePoint(content: MessageContentComplex[]): boolean {\n for (let i = 0; i < content.length; i++) {\n if (isCachePoint(content[i])) return true;\n }\n return false;\n}\n\n/**\n * Removes all Bedrock cachePoint blocks from messages\n * Used when switching from Bedrock to Anthropic provider\n * Returns a new array - only clones messages that require modification.\n */\nexport function stripBedrockCacheControl<T extends MessageWithContent>(\n messages: T[]\n): T[] {\n if (!Array.isArray(messages)) {\n return messages;\n }\n\n const updatedMessages: T[] = [...messages];\n\n for (let i = 0; i < updatedMessages.length; i++) {\n const originalMessage = updatedMessages[i];\n const content = originalMessage.content;\n\n if (!Array.isArray(content) || !hasBedrockCachePoint(content)) {\n continue;\n }\n\n const message = cloneMessageWithContent(originalMessage);\n updatedMessages[i] = message;\n\n message.content = (message.content as MessageContentComplex[]).filter(\n (block) => !isCachePoint(block as MessageContentComplex)\n ) as typeof content;\n }\n\n return updatedMessages;\n}\n\n/**\n * Adds Bedrock Converse API cache points to the last two messages.\n * Inserts `{ cachePoint: { type: 'default' } }` as a separate content block\n * immediately after the last text block in each targeted message.\n * Strips ALL existing cache control (both Bedrock and Anthropic formats) from all messages,\n * then adds fresh cache points to the last 2 messages in a single backward pass.\n * This ensures we don't accumulate stale cache points across multiple turns.\n * Returns a new array - only clones messages that require modification.\n * @param messages - The array of message objects.\n * @returns - A new array of message objects with cache points added.\n */\nexport function addBedrockCacheControl<\n T extends Partial<BaseMessage> & MessageWithContent,\n>(messages: T[]): T[] {\n if (!Array.isArray(messages) || messages.length < 2) {\n return messages;\n }\n\n const updatedMessages: T[] = [...messages];\n let messagesModified = 0;\n\n for (let i = updatedMessages.length - 1; i >= 0; i--) {\n const originalMessage = updatedMessages[i];\n const isToolMessage =\n 'getType' in originalMessage &&\n typeof originalMessage.getType === 'function' &&\n originalMessage.getType() === 'tool';\n\n const content = originalMessage.content;\n const hasArrayContent = Array.isArray(content);\n const needsStripping =\n hasArrayContent &&\n needsCacheStripping(content as MessageContentComplex[]);\n const isEmptyString = typeof content === 'string' && content === '';\n const needsCacheAdd =\n messagesModified < 2 &&\n !isToolMessage &&\n !isEmptyString &&\n (typeof content === 'string' || hasArrayContent);\n\n if (!needsStripping && !needsCacheAdd) {\n continue;\n }\n\n const message = cloneMessageWithContent(originalMessage);\n updatedMessages[i] = message;\n\n if (hasArrayContent) {\n message.content = (message.content as MessageContentComplex[]).filter(\n (block) => !isCachePoint(block)\n ) as typeof content;\n\n for (\n let j = 0;\n j < (message.content as MessageContentComplex[]).length;\n j++\n ) {\n const block = (message.content as MessageContentComplex[])[j] as Record<\n string,\n unknown\n >;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n }\n\n if (messagesModified >= 2 || isToolMessage || isEmptyString) {\n syncLcKwargs(message);\n continue;\n }\n\n if (typeof message.content === 'string') {\n message.content = [\n { type: ContentTypes.TEXT, text: message.content },\n { cachePoint: { type: 'default' } },\n ] as MessageContentComplex[];\n messagesModified++;\n syncLcKwargs(message);\n continue;\n }\n\n if (Array.isArray(message.content)) {\n let hasCacheableContent = false;\n for (const block of message.content) {\n if (block.type === ContentTypes.TEXT) {\n if (typeof block.text === 'string' && block.text !== '') {\n hasCacheableContent = true;\n break;\n }\n }\n }\n\n if (!hasCacheableContent) {\n syncLcKwargs(message);\n continue;\n }\n\n let inserted = false;\n for (let j = message.content.length - 1; j >= 0; j--) {\n const block = message.content[j] as MessageContentComplex;\n const type = (block as { type?: string }).type;\n if (type === ContentTypes.TEXT || type === 'text') {\n const text = (block as { text?: string }).text;\n if (text === '' || text === undefined) {\n continue;\n }\n message.content.splice(j + 1, 0, {\n cachePoint: { type: 'default' },\n } as MessageContentComplex);\n inserted = true;\n break;\n }\n }\n if (!inserted) {\n message.content.push({\n cachePoint: { type: 'default' },\n } as MessageContentComplex);\n }\n messagesModified++;\n }\n\n syncLcKwargs(message);\n }\n\n return updatedMessages;\n}\n"],"names":[],"mappings":";;AASA;;;AAGG;AACH,SAAS,gBAAgB,CACvB,OAAU,EAAA;AAEV,IAAA,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;AAC/B,QAAA,OAAO,OAAO;;AAEhB,IAAA,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE;AAC1B,QAAA,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,MAAM,EAAE,GAAG,KAAK,EAAE,CAAC,CAAM;;AAEpD,IAAA,OAAO,OAAO;AAChB;AAEA;;;AAGG;AACH,SAAS,uBAAuB,CAA+B,OAAU,EAAA;AACvE,IAAA,IAAI,OAAO,CAAC,OAAO,KAAK,SAAS,EAAE;AACjC,QAAA,OAAO,EAAE,GAAG,OAAO,EAAE;;IAGvB,MAAM,aAAa,GAAG,gBAAgB,CAAC,OAAO,CAAC,OAAO,CAAC;AACvD,IAAA,MAAM,MAAM,GAAG;AACb,QAAA,GAAG,OAAO;AACV,QAAA,OAAO,EAAE,aAAa;KAClB;AAEN,IAAA,MAAM,QAAQ,GAAI,MAAkC,CAAC,SAExC;AACb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AAC;;AAE7B,YAAA,gBAAgB,EAAE;;QAGpB,YAAY,CAAC,OAAO,CAAC;;AAGvB,IAAA,OAAO,eAAe;AACxB;;;;"}
|
|
1
|
+
{"version":3,"file":"cache.mjs","sources":["../../../src/messages/cache.ts"],"sourcesContent":["import {\n BaseMessage,\n MessageContentComplex,\n AIMessage,\n HumanMessage,\n SystemMessage,\n ToolMessage,\n} from '@langchain/core/messages';\nimport type { AnthropicMessage } from '@/types/messages';\nimport type Anthropic from '@anthropic-ai/sdk';\nimport { ContentTypes } from '@/common/enum';\n\ntype MessageWithContent = {\n content?: string | MessageContentComplex[];\n};\n\n/**\n * Deep clones a message's content to prevent mutation of the original.\n */\nfunction deepCloneContent<T extends string | MessageContentComplex[]>(\n content: T\n): T {\n if (typeof content === 'string') {\n return content;\n }\n if (Array.isArray(content)) {\n return content.map((block) => ({ ...block })) as T;\n }\n return content;\n}\n\n/**\n * Simple shallow clone with deep-cloned content.\n * Used for stripping cache control where we don't need proper LangChain instances.\n */\nfunction shallowCloneMessage<T extends MessageWithContent>(message: T): T {\n return {\n ...message,\n content: deepCloneContent(message.content ?? ''),\n } as T;\n}\n\n/**\n * Creates a new LangChain message instance with the given content.\n * Required when adding cache points to ensure proper serialization.\n */\nfunction createNewMessage<T extends MessageWithContent>(\n message: T,\n content: MessageContentComplex[]\n): T {\n if ('getType' in message && typeof message.getType === 'function') {\n const baseMsg = message as unknown as BaseMessage;\n const msgType = baseMsg.getType();\n\n const baseFields = {\n content,\n name: baseMsg.name,\n additional_kwargs: { ...baseMsg.additional_kwargs },\n response_metadata: { ...baseMsg.response_metadata },\n id: baseMsg.id,\n };\n\n switch (msgType) {\n case 'human':\n return new HumanMessage(baseFields) as unknown as T;\n case 'ai': {\n const aiMsg = baseMsg as AIMessage;\n return new AIMessage({\n ...baseFields,\n tool_calls: aiMsg.tool_calls ? [...aiMsg.tool_calls] : [],\n invalid_tool_calls: aiMsg.invalid_tool_calls\n ? [...aiMsg.invalid_tool_calls]\n : [],\n usage_metadata: aiMsg.usage_metadata,\n }) as unknown as T;\n }\n case 'system':\n return new SystemMessage(baseFields) as unknown as T;\n case 'tool': {\n const toolMsg = baseMsg as ToolMessage;\n return new ToolMessage({\n ...baseFields,\n tool_call_id: toolMsg.tool_call_id,\n status: toolMsg.status,\n artifact: toolMsg.artifact,\n }) as unknown as T;\n }\n default:\n break;\n }\n }\n\n const cloned = { ...message, content } as T;\n const lcKwargs = (cloned as Record<string, unknown>).lc_kwargs as\n | Record<string, unknown>\n | undefined;\n if (lcKwargs != null) {\n (cloned as Record<string, unknown>).lc_kwargs = { ...lcKwargs, content };\n }\n return cloned;\n}\n\n/**\n * Checks if a message's content needs cache control stripping.\n * Returns true if content has cachePoint blocks or cache_control fields.\n */\nfunction needsCacheStripping(content: MessageContentComplex[]): boolean {\n for (let i = 0; i < content.length; i++) {\n const block = content[i];\n if (isCachePoint(block)) return true;\n if ('cache_control' in block) return true;\n }\n return false;\n}\n\n/**\n * Anthropic API: Adds cache control to the appropriate user messages in the payload.\n * Strips ALL existing cache control (both Anthropic and Bedrock formats) from all messages,\n * then adds fresh cache control to the last 2 user messages in a single backward pass.\n * This ensures we don't accumulate stale cache points across multiple turns.\n * Returns a new array - only clones messages that require modification.\n * @param messages - The array of message objects.\n * @returns - A new array of message objects with cache control added.\n */\nexport function addCacheControl<T extends AnthropicMessage | BaseMessage>(\n messages: T[]\n): T[] {\n if (!Array.isArray(messages) || messages.length < 2) {\n return messages;\n }\n\n const updatedMessages: T[] = [...messages];\n let userMessagesModified = 0;\n\n for (let i = updatedMessages.length - 1; i >= 0; i--) {\n const originalMessage = updatedMessages[i];\n const isUserMessage =\n ('getType' in originalMessage && originalMessage.getType() === 'human') ||\n ('role' in originalMessage && originalMessage.role === 'user');\n\n const hasArrayContent = Array.isArray(originalMessage.content);\n const needsStripping =\n hasArrayContent &&\n needsCacheStripping(originalMessage.content as MessageContentComplex[]);\n const needsCacheAdd =\n userMessagesModified < 2 &&\n isUserMessage &&\n (typeof originalMessage.content === 'string' || hasArrayContent);\n\n if (!needsStripping && !needsCacheAdd) {\n continue;\n }\n\n const message = shallowCloneMessage(\n originalMessage as MessageWithContent\n ) as T;\n updatedMessages[i] = message;\n\n if (hasArrayContent) {\n message.content = (message.content as MessageContentComplex[]).filter(\n (block) => !isCachePoint(block as MessageContentComplex)\n ) as typeof message.content;\n\n for (\n let j = 0;\n j < (message.content as MessageContentComplex[]).length;\n j++\n ) {\n const block = (message.content as MessageContentComplex[])[j] as Record<\n string,\n unknown\n >;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n }\n\n if (userMessagesModified >= 2 || !isUserMessage) {\n continue;\n }\n\n if (typeof message.content === 'string') {\n message.content = [\n {\n type: 'text',\n text: message.content,\n cache_control: { type: 'ephemeral' },\n },\n ];\n userMessagesModified++;\n } else if (Array.isArray(message.content)) {\n for (let j = message.content.length - 1; j >= 0; j--) {\n const contentPart = message.content[j];\n if ('type' in contentPart && contentPart.type === 'text') {\n (contentPart as Anthropic.TextBlockParam).cache_control = {\n type: 'ephemeral',\n };\n userMessagesModified++;\n break;\n }\n }\n }\n }\n\n return updatedMessages;\n}\n\n/**\n * Checks if a content block is a cache point\n */\nfunction isCachePoint(block: MessageContentComplex): boolean {\n return 'cachePoint' in block && !('type' in block);\n}\n\n/**\n * Checks if a message's content has Anthropic cache_control fields.\n */\nfunction hasAnthropicCacheControl(content: MessageContentComplex[]): boolean {\n for (let i = 0; i < content.length; i++) {\n if ('cache_control' in content[i]) return true;\n }\n return false;\n}\n\n/**\n * Removes all Anthropic cache_control fields from messages\n * Used when switching from Anthropic to Bedrock provider\n * Returns a new array - only clones messages that require modification.\n */\nexport function stripAnthropicCacheControl<T extends MessageWithContent>(\n messages: T[]\n): T[] {\n if (!Array.isArray(messages)) {\n return messages;\n }\n\n const updatedMessages: T[] = [...messages];\n\n for (let i = 0; i < updatedMessages.length; i++) {\n const originalMessage = updatedMessages[i];\n const content = originalMessage.content;\n\n if (!Array.isArray(content) || !hasAnthropicCacheControl(content)) {\n continue;\n }\n\n const message = shallowCloneMessage(originalMessage);\n updatedMessages[i] = message;\n\n for (\n let j = 0;\n j < (message.content as MessageContentComplex[]).length;\n j++\n ) {\n const block = (message.content as MessageContentComplex[])[j] as Record<\n string,\n unknown\n >;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n }\n\n return updatedMessages;\n}\n\n/**\n * Checks if a message's content has Bedrock cachePoint blocks.\n */\nfunction hasBedrockCachePoint(content: MessageContentComplex[]): boolean {\n for (let i = 0; i < content.length; i++) {\n if (isCachePoint(content[i])) return true;\n }\n return false;\n}\n\n/**\n * Removes all Bedrock cachePoint blocks from messages\n * Used when switching from Bedrock to Anthropic provider\n * Returns a new array - only clones messages that require modification.\n */\nexport function stripBedrockCacheControl<T extends MessageWithContent>(\n messages: T[]\n): T[] {\n if (!Array.isArray(messages)) {\n return messages;\n }\n\n const updatedMessages: T[] = [...messages];\n\n for (let i = 0; i < updatedMessages.length; i++) {\n const originalMessage = updatedMessages[i];\n const content = originalMessage.content;\n\n if (!Array.isArray(content) || !hasBedrockCachePoint(content)) {\n continue;\n }\n\n const message = shallowCloneMessage(originalMessage);\n updatedMessages[i] = message;\n\n message.content = (message.content as MessageContentComplex[]).filter(\n (block) => !isCachePoint(block as MessageContentComplex)\n ) as typeof content;\n }\n\n return updatedMessages;\n}\n\n/**\n * Adds Bedrock Converse API cache points to the last two messages.\n * Inserts `{ cachePoint: { type: 'default' } }` as a separate content block\n * immediately after the last text block in each targeted message.\n * Strips ALL existing cache control (both Bedrock and Anthropic formats) from all messages,\n * then adds fresh cache points to the last 2 messages in a single backward pass.\n * This ensures we don't accumulate stale cache points across multiple turns.\n * Returns a new array - only clones messages that require modification.\n * @param messages - The array of message objects.\n * @returns - A new array of message objects with cache points added.\n */\nexport function addBedrockCacheControl<\n T extends Partial<BaseMessage> & MessageWithContent,\n>(messages: T[]): T[] {\n if (!Array.isArray(messages) || messages.length < 2) {\n return messages;\n }\n\n const updatedMessages: T[] = [...messages];\n let messagesModified = 0;\n\n for (let i = updatedMessages.length - 1; i >= 0; i--) {\n const originalMessage = updatedMessages[i];\n const isToolMessage =\n 'getType' in originalMessage &&\n typeof originalMessage.getType === 'function' &&\n originalMessage.getType() === 'tool';\n\n const content = originalMessage.content;\n const hasArrayContent = Array.isArray(content);\n const needsStripping =\n hasArrayContent &&\n needsCacheStripping(content as MessageContentComplex[]);\n const isEmptyString = typeof content === 'string' && content === '';\n const needsCacheAdd =\n messagesModified < 2 &&\n !isToolMessage &&\n !isEmptyString &&\n (typeof content === 'string' || hasArrayContent);\n\n if (!needsStripping && !needsCacheAdd) {\n continue;\n }\n\n let workingContent: MessageContentComplex[];\n\n if (hasArrayContent) {\n workingContent = deepCloneContent(\n content as MessageContentComplex[]\n ).filter((block) => !isCachePoint(block));\n\n for (let j = 0; j < workingContent.length; j++) {\n const block = workingContent[j] as Record<string, unknown>;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n } else if (typeof content === 'string') {\n workingContent = [{ type: ContentTypes.TEXT, text: content }];\n } else {\n workingContent = [];\n }\n\n if (messagesModified >= 2 || isToolMessage || isEmptyString) {\n updatedMessages[i] = shallowCloneMessage(originalMessage);\n (updatedMessages[i] as MessageWithContent).content = workingContent;\n continue;\n }\n\n if (workingContent.length === 0) {\n continue;\n }\n\n let hasCacheableContent = false;\n for (const block of workingContent) {\n if (block.type === ContentTypes.TEXT) {\n if (typeof block.text === 'string' && block.text !== '') {\n hasCacheableContent = true;\n break;\n }\n }\n }\n\n if (!hasCacheableContent) {\n updatedMessages[i] = shallowCloneMessage(originalMessage);\n (updatedMessages[i] as MessageWithContent).content = workingContent;\n continue;\n }\n\n let inserted = false;\n for (let j = workingContent.length - 1; j >= 0; j--) {\n const block = workingContent[j] as MessageContentComplex;\n const type = (block as { type?: string }).type;\n if (type === ContentTypes.TEXT || type === 'text') {\n const text = (block as { text?: string }).text;\n if (text === '' || text === undefined) {\n continue;\n }\n workingContent.splice(j + 1, 0, {\n cachePoint: { type: 'default' },\n } as MessageContentComplex);\n inserted = true;\n break;\n }\n }\n if (!inserted) {\n workingContent.push({\n cachePoint: { type: 'default' },\n } as MessageContentComplex);\n }\n\n updatedMessages[i] = createNewMessage(originalMessage, workingContent);\n messagesModified++;\n }\n\n return updatedMessages;\n}\n"],"names":[],"mappings":";;;AAgBA;;AAEG;AACH,SAAS,gBAAgB,CACvB,OAAU,EAAA;AAEV,IAAA,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;AAC/B,QAAA,OAAO,OAAO;;AAEhB,IAAA,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE;AAC1B,QAAA,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,MAAM,EAAE,GAAG,KAAK,EAAE,CAAC,CAAM;;AAEpD,IAAA,OAAO,OAAO;AAChB;AAEA;;;AAGG;AACH,SAAS,mBAAmB,CAA+B,OAAU,EAAA;IACnE,OAAO;AACL,QAAA,GAAG,OAAO;QACV,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,OAAO,IAAI,EAAE,CAAC;KAC5C;AACR;AAEA;;;AAGG;AACH,SAAS,gBAAgB,CACvB,OAAU,EACV,OAAgC,EAAA;IAEhC,IAAI,SAAS,IAAI,OAAO,IAAI,OAAO,OAAO,CAAC,OAAO,KAAK,UAAU,EAAE;QACjE,MAAM,OAAO,GAAG,OAAiC;AACjD,QAAA,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,EAAE;AAEjC,QAAA,MAAM,UAAU,GAAG;YACjB,OAAO;YACP,IAAI,EAAE,OAAO,CAAC,IAAI;AAClB,YAAA,iBAAiB,EAAE,EAAE,GAAG,OAAO,CAAC,iBAAiB,EAAE;AACnD,YAAA,iBAAiB,EAAE,EAAE,GAAG,OAAO,CAAC,iBAAiB,EAAE;YACnD,EAAE,EAAE,OAAO,CAAC,EAAE;SACf;QAED,QAAQ,OAAO;AACf,YAAA,KAAK,OAAO;AACV,gBAAA,OAAO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AAK,MAAM,EAAE;AACjD,gBAAA,MAAM,IAAI,GAAI,KAA2B,CAAC,IAAI;gBAC9C,IAAI,IAAI,KAAK,EAAE,IAAI,IAAI,KAAK,SAAS,EAAE;oBACrC;;gBAEF,cAAc,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE;AAC9B,oBAAA,UAAU,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;AACP,iBAAA,CAAC;gBAC3B,QAAQ,GAAG,IAAI;gBACf;;;QAGJ,IAAI,CAAC,QAAQ,EAAE;YACb,cAAc,CAAC,IAAI,CAAC;AAClB,gBAAA,UAAU,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;AACP,aAAA,CAAC;;QAG7B,eAAe,CAAC,CAAC,CAAC,GAAG,gBAAgB,CAAC,eAAe,EAAE,cAAc,CAAC;AACtE,QAAA,gBAAgB,EAAE;;AAGpB,IAAA,OAAO,eAAe;AACxB;;;;"}
|
package/package.json
CHANGED
package/src/messages/cache.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
BaseMessage,
|
|
3
|
+
MessageContentComplex,
|
|
4
|
+
AIMessage,
|
|
5
|
+
HumanMessage,
|
|
6
|
+
SystemMessage,
|
|
7
|
+
ToolMessage,
|
|
8
|
+
} from '@langchain/core/messages';
|
|
2
9
|
import type { AnthropicMessage } from '@/types/messages';
|
|
3
10
|
import type Anthropic from '@anthropic-ai/sdk';
|
|
4
11
|
import { ContentTypes } from '@/common/enum';
|
|
@@ -9,7 +16,6 @@ type MessageWithContent = {
|
|
|
9
16
|
|
|
10
17
|
/**
|
|
11
18
|
* Deep clones a message's content to prevent mutation of the original.
|
|
12
|
-
* Handles both string and array content types.
|
|
13
19
|
*/
|
|
14
20
|
function deepCloneContent<T extends string | MessageContentComplex[]>(
|
|
15
21
|
content: T
|
|
@@ -24,44 +30,74 @@ function deepCloneContent<T extends string | MessageContentComplex[]>(
|
|
|
24
30
|
}
|
|
25
31
|
|
|
26
32
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
33
|
+
* Simple shallow clone with deep-cloned content.
|
|
34
|
+
* Used for stripping cache control where we don't need proper LangChain instances.
|
|
29
35
|
*/
|
|
30
|
-
function
|
|
31
|
-
|
|
32
|
-
return { ...message };
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const clonedContent = deepCloneContent(message.content);
|
|
36
|
-
const cloned = {
|
|
36
|
+
function shallowCloneMessage<T extends MessageWithContent>(message: T): T {
|
|
37
|
+
return {
|
|
37
38
|
...message,
|
|
38
|
-
content:
|
|
39
|
+
content: deepCloneContent(message.content ?? ''),
|
|
39
40
|
} as T;
|
|
40
|
-
|
|
41
|
-
const lcKwargs = (cloned as Record<string, unknown>).lc_kwargs as
|
|
42
|
-
| Record<string, unknown>
|
|
43
|
-
| undefined;
|
|
44
|
-
if (lcKwargs != null) {
|
|
45
|
-
(cloned as Record<string, unknown>).lc_kwargs = {
|
|
46
|
-
...lcKwargs,
|
|
47
|
-
content: clonedContent,
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return cloned;
|
|
52
41
|
}
|
|
53
42
|
|
|
54
43
|
/**
|
|
55
|
-
*
|
|
56
|
-
*
|
|
44
|
+
* Creates a new LangChain message instance with the given content.
|
|
45
|
+
* Required when adding cache points to ensure proper serialization.
|
|
57
46
|
*/
|
|
58
|
-
function
|
|
59
|
-
|
|
47
|
+
function createNewMessage<T extends MessageWithContent>(
|
|
48
|
+
message: T,
|
|
49
|
+
content: MessageContentComplex[]
|
|
50
|
+
): T {
|
|
51
|
+
if ('getType' in message && typeof message.getType === 'function') {
|
|
52
|
+
const baseMsg = message as unknown as BaseMessage;
|
|
53
|
+
const msgType = baseMsg.getType();
|
|
54
|
+
|
|
55
|
+
const baseFields = {
|
|
56
|
+
content,
|
|
57
|
+
name: baseMsg.name,
|
|
58
|
+
additional_kwargs: { ...baseMsg.additional_kwargs },
|
|
59
|
+
response_metadata: { ...baseMsg.response_metadata },
|
|
60
|
+
id: baseMsg.id,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
switch (msgType) {
|
|
64
|
+
case 'human':
|
|
65
|
+
return new HumanMessage(baseFields) as unknown as T;
|
|
66
|
+
case 'ai': {
|
|
67
|
+
const aiMsg = baseMsg as AIMessage;
|
|
68
|
+
return new AIMessage({
|
|
69
|
+
...baseFields,
|
|
70
|
+
tool_calls: aiMsg.tool_calls ? [...aiMsg.tool_calls] : [],
|
|
71
|
+
invalid_tool_calls: aiMsg.invalid_tool_calls
|
|
72
|
+
? [...aiMsg.invalid_tool_calls]
|
|
73
|
+
: [],
|
|
74
|
+
usage_metadata: aiMsg.usage_metadata,
|
|
75
|
+
}) as unknown as T;
|
|
76
|
+
}
|
|
77
|
+
case 'system':
|
|
78
|
+
return new SystemMessage(baseFields) as unknown as T;
|
|
79
|
+
case 'tool': {
|
|
80
|
+
const toolMsg = baseMsg as ToolMessage;
|
|
81
|
+
return new ToolMessage({
|
|
82
|
+
...baseFields,
|
|
83
|
+
tool_call_id: toolMsg.tool_call_id,
|
|
84
|
+
status: toolMsg.status,
|
|
85
|
+
artifact: toolMsg.artifact,
|
|
86
|
+
}) as unknown as T;
|
|
87
|
+
}
|
|
88
|
+
default:
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const cloned = { ...message, content } as T;
|
|
94
|
+
const lcKwargs = (cloned as Record<string, unknown>).lc_kwargs as
|
|
60
95
|
| Record<string, unknown>
|
|
61
96
|
| undefined;
|
|
62
97
|
if (lcKwargs != null) {
|
|
63
|
-
|
|
98
|
+
(cloned as Record<string, unknown>).lc_kwargs = { ...lcKwargs, content };
|
|
64
99
|
}
|
|
100
|
+
return cloned;
|
|
65
101
|
}
|
|
66
102
|
|
|
67
103
|
/**
|
|
@@ -115,7 +151,7 @@ export function addCacheControl<T extends AnthropicMessage | BaseMessage>(
|
|
|
115
151
|
continue;
|
|
116
152
|
}
|
|
117
153
|
|
|
118
|
-
const message =
|
|
154
|
+
const message = shallowCloneMessage(
|
|
119
155
|
originalMessage as MessageWithContent
|
|
120
156
|
) as T;
|
|
121
157
|
updatedMessages[i] = message;
|
|
@@ -141,7 +177,6 @@ export function addCacheControl<T extends AnthropicMessage | BaseMessage>(
|
|
|
141
177
|
}
|
|
142
178
|
|
|
143
179
|
if (userMessagesModified >= 2 || !isUserMessage) {
|
|
144
|
-
syncLcKwargs(message);
|
|
145
180
|
continue;
|
|
146
181
|
}
|
|
147
182
|
|
|
@@ -166,8 +201,6 @@ export function addCacheControl<T extends AnthropicMessage | BaseMessage>(
|
|
|
166
201
|
}
|
|
167
202
|
}
|
|
168
203
|
}
|
|
169
|
-
|
|
170
|
-
syncLcKwargs(message);
|
|
171
204
|
}
|
|
172
205
|
|
|
173
206
|
return updatedMessages;
|
|
@@ -212,7 +245,7 @@ export function stripAnthropicCacheControl<T extends MessageWithContent>(
|
|
|
212
245
|
continue;
|
|
213
246
|
}
|
|
214
247
|
|
|
215
|
-
const message =
|
|
248
|
+
const message = shallowCloneMessage(originalMessage);
|
|
216
249
|
updatedMessages[i] = message;
|
|
217
250
|
|
|
218
251
|
for (
|
|
@@ -265,7 +298,7 @@ export function stripBedrockCacheControl<T extends MessageWithContent>(
|
|
|
265
298
|
continue;
|
|
266
299
|
}
|
|
267
300
|
|
|
268
|
-
const message =
|
|
301
|
+
const message = shallowCloneMessage(originalMessage);
|
|
269
302
|
updatedMessages[i] = message;
|
|
270
303
|
|
|
271
304
|
message.content = (message.content as MessageContentComplex[]).filter(
|
|
@@ -320,85 +353,75 @@ export function addBedrockCacheControl<
|
|
|
320
353
|
continue;
|
|
321
354
|
}
|
|
322
355
|
|
|
323
|
-
|
|
324
|
-
updatedMessages[i] = message;
|
|
356
|
+
let workingContent: MessageContentComplex[];
|
|
325
357
|
|
|
326
358
|
if (hasArrayContent) {
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
)
|
|
359
|
+
workingContent = deepCloneContent(
|
|
360
|
+
content as MessageContentComplex[]
|
|
361
|
+
).filter((block) => !isCachePoint(block));
|
|
330
362
|
|
|
331
|
-
for (
|
|
332
|
-
|
|
333
|
-
j < (message.content as MessageContentComplex[]).length;
|
|
334
|
-
j++
|
|
335
|
-
) {
|
|
336
|
-
const block = (message.content as MessageContentComplex[])[j] as Record<
|
|
337
|
-
string,
|
|
338
|
-
unknown
|
|
339
|
-
>;
|
|
363
|
+
for (let j = 0; j < workingContent.length; j++) {
|
|
364
|
+
const block = workingContent[j] as Record<string, unknown>;
|
|
340
365
|
if ('cache_control' in block) {
|
|
341
366
|
delete block.cache_control;
|
|
342
367
|
}
|
|
343
368
|
}
|
|
369
|
+
} else if (typeof content === 'string') {
|
|
370
|
+
workingContent = [{ type: ContentTypes.TEXT, text: content }];
|
|
371
|
+
} else {
|
|
372
|
+
workingContent = [];
|
|
344
373
|
}
|
|
345
374
|
|
|
346
375
|
if (messagesModified >= 2 || isToolMessage || isEmptyString) {
|
|
347
|
-
|
|
376
|
+
updatedMessages[i] = shallowCloneMessage(originalMessage);
|
|
377
|
+
(updatedMessages[i] as MessageWithContent).content = workingContent;
|
|
348
378
|
continue;
|
|
349
379
|
}
|
|
350
380
|
|
|
351
|
-
if (
|
|
352
|
-
message.content = [
|
|
353
|
-
{ type: ContentTypes.TEXT, text: message.content },
|
|
354
|
-
{ cachePoint: { type: 'default' } },
|
|
355
|
-
] as MessageContentComplex[];
|
|
356
|
-
messagesModified++;
|
|
357
|
-
syncLcKwargs(message);
|
|
381
|
+
if (workingContent.length === 0) {
|
|
358
382
|
continue;
|
|
359
383
|
}
|
|
360
384
|
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
if (block.
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
break;
|
|
368
|
-
}
|
|
385
|
+
let hasCacheableContent = false;
|
|
386
|
+
for (const block of workingContent) {
|
|
387
|
+
if (block.type === ContentTypes.TEXT) {
|
|
388
|
+
if (typeof block.text === 'string' && block.text !== '') {
|
|
389
|
+
hasCacheableContent = true;
|
|
390
|
+
break;
|
|
369
391
|
}
|
|
370
392
|
}
|
|
393
|
+
}
|
|
371
394
|
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
395
|
+
if (!hasCacheableContent) {
|
|
396
|
+
updatedMessages[i] = shallowCloneMessage(originalMessage);
|
|
397
|
+
(updatedMessages[i] as MessageWithContent).content = workingContent;
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
376
400
|
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
}
|
|
386
|
-
message.content.splice(j + 1, 0, {
|
|
387
|
-
cachePoint: { type: 'default' },
|
|
388
|
-
} as MessageContentComplex);
|
|
389
|
-
inserted = true;
|
|
390
|
-
break;
|
|
401
|
+
let inserted = false;
|
|
402
|
+
for (let j = workingContent.length - 1; j >= 0; j--) {
|
|
403
|
+
const block = workingContent[j] as MessageContentComplex;
|
|
404
|
+
const type = (block as { type?: string }).type;
|
|
405
|
+
if (type === ContentTypes.TEXT || type === 'text') {
|
|
406
|
+
const text = (block as { text?: string }).text;
|
|
407
|
+
if (text === '' || text === undefined) {
|
|
408
|
+
continue;
|
|
391
409
|
}
|
|
392
|
-
|
|
393
|
-
if (!inserted) {
|
|
394
|
-
message.content.push({
|
|
410
|
+
workingContent.splice(j + 1, 0, {
|
|
395
411
|
cachePoint: { type: 'default' },
|
|
396
412
|
} as MessageContentComplex);
|
|
413
|
+
inserted = true;
|
|
414
|
+
break;
|
|
397
415
|
}
|
|
398
|
-
|
|
416
|
+
}
|
|
417
|
+
if (!inserted) {
|
|
418
|
+
workingContent.push({
|
|
419
|
+
cachePoint: { type: 'default' },
|
|
420
|
+
} as MessageContentComplex);
|
|
399
421
|
}
|
|
400
422
|
|
|
401
|
-
|
|
423
|
+
updatedMessages[i] = createNewMessage(originalMessage, workingContent);
|
|
424
|
+
messagesModified++;
|
|
402
425
|
}
|
|
403
426
|
|
|
404
427
|
return updatedMessages;
|