@librechat/agents 3.0.775 → 3.0.776
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/cache.cjs +27 -77
- package/dist/cjs/messages/cache.cjs.map +1 -1
- package/dist/esm/messages/cache.mjs +27 -77
- package/dist/esm/messages/cache.mjs.map +1 -1
- package/package.json +3 -3
- package/src/messages/cache.test.ts +6 -12
- package/src/messages/cache.ts +48 -107
- package/src/specs/cache.simple.test.ts +396 -0
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var messages = require('@langchain/core/messages');
|
|
4
3
|
var _enum = require('../common/enum.cjs');
|
|
5
4
|
|
|
6
5
|
/**
|
|
@@ -16,61 +15,24 @@ function deepCloneContent(content) {
|
|
|
16
15
|
return content;
|
|
17
16
|
}
|
|
18
17
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
18
|
+
* Clones a message with deep-cloned content, explicitly excluding LangChain
|
|
19
|
+
* serialization metadata to prevent coercion issues.
|
|
21
20
|
*/
|
|
22
|
-
function
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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,
|
|
21
|
+
function cloneMessage(message, content) {
|
|
22
|
+
const { lc_kwargs: _lc_kwargs, lc_serializable: _lc_serializable, lc_namespace: _lc_namespace, ...rest } = message;
|
|
23
|
+
const cloned = { ...rest, content };
|
|
24
|
+
// LangChain messages don't have a direct 'role' property - derive it from getType()
|
|
25
|
+
if ('getType' in message &&
|
|
26
|
+
typeof message.getType === 'function' &&
|
|
27
|
+
!('role' in cloned)) {
|
|
28
|
+
const msgType = message.getType();
|
|
29
|
+
const roleMap = {
|
|
30
|
+
human: 'user',
|
|
31
|
+
ai: 'assistant',
|
|
32
|
+
system: 'system',
|
|
33
|
+
tool: 'tool',
|
|
42
34
|
};
|
|
43
|
-
|
|
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;
|
|
72
|
-
if (lcKwargs != null) {
|
|
73
|
-
cloned.lc_kwargs = { ...lcKwargs, content };
|
|
35
|
+
cloned.role = roleMap[msgType] || msgType;
|
|
74
36
|
}
|
|
75
37
|
return cloned;
|
|
76
38
|
}
|
|
@@ -136,29 +98,20 @@ function addCacheControl(messages) {
|
|
|
136
98
|
workingContent = [];
|
|
137
99
|
}
|
|
138
100
|
if (userMessagesModified >= 2 || !isUserMessage) {
|
|
139
|
-
updatedMessages[i] =
|
|
140
|
-
updatedMessages[i].content = workingContent;
|
|
101
|
+
updatedMessages[i] = cloneMessage(originalMessage, workingContent);
|
|
141
102
|
continue;
|
|
142
103
|
}
|
|
143
|
-
let cacheAdded = false;
|
|
144
104
|
for (let j = workingContent.length - 1; j >= 0; j--) {
|
|
145
105
|
const contentPart = workingContent[j];
|
|
146
106
|
if ('type' in contentPart && contentPart.type === 'text') {
|
|
147
107
|
contentPart.cache_control = {
|
|
148
108
|
type: 'ephemeral',
|
|
149
109
|
};
|
|
150
|
-
cacheAdded = true;
|
|
151
110
|
userMessagesModified++;
|
|
152
111
|
break;
|
|
153
112
|
}
|
|
154
113
|
}
|
|
155
|
-
|
|
156
|
-
updatedMessages[i] = createNewMessage(originalMessage, workingContent);
|
|
157
|
-
}
|
|
158
|
-
else {
|
|
159
|
-
updatedMessages[i] = shallowCloneMessage(originalMessage);
|
|
160
|
-
updatedMessages[i].content = workingContent;
|
|
161
|
-
}
|
|
114
|
+
updatedMessages[i] = cloneMessage(originalMessage, workingContent);
|
|
162
115
|
}
|
|
163
116
|
return updatedMessages;
|
|
164
117
|
}
|
|
@@ -194,14 +147,14 @@ function stripAnthropicCacheControl(messages) {
|
|
|
194
147
|
if (!Array.isArray(content) || !hasAnthropicCacheControl(content)) {
|
|
195
148
|
continue;
|
|
196
149
|
}
|
|
197
|
-
const
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
const block = message.content[j];
|
|
150
|
+
const clonedContent = deepCloneContent(content);
|
|
151
|
+
for (let j = 0; j < clonedContent.length; j++) {
|
|
152
|
+
const block = clonedContent[j];
|
|
201
153
|
if ('cache_control' in block) {
|
|
202
154
|
delete block.cache_control;
|
|
203
155
|
}
|
|
204
156
|
}
|
|
157
|
+
updatedMessages[i] = cloneMessage(originalMessage, clonedContent);
|
|
205
158
|
}
|
|
206
159
|
return updatedMessages;
|
|
207
160
|
}
|
|
@@ -231,9 +184,8 @@ function stripBedrockCacheControl(messages) {
|
|
|
231
184
|
if (!Array.isArray(content) || !hasBedrockCachePoint(content)) {
|
|
232
185
|
continue;
|
|
233
186
|
}
|
|
234
|
-
const
|
|
235
|
-
updatedMessages[i] =
|
|
236
|
-
message.content = message.content.filter((block) => !isCachePoint(block));
|
|
187
|
+
const clonedContent = deepCloneContent(content).filter((block) => !isCachePoint(block));
|
|
188
|
+
updatedMessages[i] = cloneMessage(originalMessage, clonedContent);
|
|
237
189
|
}
|
|
238
190
|
return updatedMessages;
|
|
239
191
|
}
|
|
@@ -288,8 +240,7 @@ function addBedrockCacheControl(messages) {
|
|
|
288
240
|
workingContent = [];
|
|
289
241
|
}
|
|
290
242
|
if (messagesModified >= 2 || isToolMessage || isEmptyString) {
|
|
291
|
-
updatedMessages[i] =
|
|
292
|
-
updatedMessages[i].content = workingContent;
|
|
243
|
+
updatedMessages[i] = cloneMessage(originalMessage, workingContent);
|
|
293
244
|
continue;
|
|
294
245
|
}
|
|
295
246
|
if (workingContent.length === 0) {
|
|
@@ -305,8 +256,7 @@ function addBedrockCacheControl(messages) {
|
|
|
305
256
|
}
|
|
306
257
|
}
|
|
307
258
|
if (!hasCacheableContent) {
|
|
308
|
-
updatedMessages[i] =
|
|
309
|
-
updatedMessages[i].content = workingContent;
|
|
259
|
+
updatedMessages[i] = cloneMessage(originalMessage, workingContent);
|
|
310
260
|
continue;
|
|
311
261
|
}
|
|
312
262
|
let inserted = false;
|
|
@@ -330,7 +280,7 @@ function addBedrockCacheControl(messages) {
|
|
|
330
280
|
cachePoint: { type: 'default' },
|
|
331
281
|
});
|
|
332
282
|
}
|
|
333
|
-
updatedMessages[i] =
|
|
283
|
+
updatedMessages[i] = cloneMessage(originalMessage, workingContent);
|
|
334
284
|
messagesModified++;
|
|
335
285
|
}
|
|
336
286
|
return updatedMessages;
|
|
@@ -1 +1 @@
|
|
|
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 content = originalMessage.content;\n const isUserMessage =\n ('getType' in originalMessage && originalMessage.getType() === 'human') ||\n ('role' in originalMessage && originalMessage.role === 'user');\n\n const hasArrayContent = Array.isArray(content);\n const needsStripping =\n hasArrayContent &&\n needsCacheStripping(content as MessageContentComplex[]);\n const needsCacheAdd =\n userMessagesModified < 2 &&\n isUserMessage &&\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 as MessageContentComplex));\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 = [\n { type: 'text', text: content },\n ] as MessageContentComplex[];\n } else {\n workingContent = [];\n }\n\n if (userMessagesModified >= 2 || !isUserMessage) {\n updatedMessages[i] = shallowCloneMessage(\n originalMessage as MessageWithContent\n ) as T;\n (updatedMessages[i] as MessageWithContent).content = workingContent;\n continue;\n }\n\n let cacheAdded = false;\n for (let j = workingContent.length - 1; j >= 0; j--) {\n const contentPart = workingContent[j];\n if ('type' in contentPart && contentPart.type === 'text') {\n (contentPart as Anthropic.TextBlockParam).cache_control = {\n type: 'ephemeral',\n };\n cacheAdded = true;\n userMessagesModified++;\n break;\n }\n }\n\n if (cacheAdded) {\n updatedMessages[i] = createNewMessage(\n originalMessage as MessageWithContent,\n workingContent\n ) as T;\n } else {\n updatedMessages[i] = shallowCloneMessage(\n originalMessage as MessageWithContent\n ) as T;\n (updatedMessages[i] as MessageWithContent).content = workingContent;\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,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,gBAA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,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
|
+
{"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 */\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, explicitly excluding LangChain\n * serialization metadata to prevent coercion issues.\n */\nfunction cloneMessage<T extends MessageWithContent>(\n message: T,\n content: string | MessageContentComplex[]\n): T {\n const {\n lc_kwargs: _lc_kwargs,\n lc_serializable: _lc_serializable,\n lc_namespace: _lc_namespace,\n ...rest\n } = message as T & {\n lc_kwargs?: unknown;\n lc_serializable?: unknown;\n lc_namespace?: unknown;\n };\n\n const cloned = { ...rest, content } as T;\n\n // LangChain messages don't have a direct 'role' property - derive it from getType()\n if (\n 'getType' in message &&\n typeof message.getType === 'function' &&\n !('role' in cloned)\n ) {\n const msgType = (message as unknown as BaseMessage).getType();\n const roleMap: Record<string, string> = {\n human: 'user',\n ai: 'assistant',\n system: 'system',\n tool: 'tool',\n };\n (cloned as Record<string, unknown>).role = roleMap[msgType] || msgType;\n }\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 content = originalMessage.content;\n const isUserMessage =\n ('getType' in originalMessage && originalMessage.getType() === 'human') ||\n ('role' in originalMessage && originalMessage.role === 'user');\n\n const hasArrayContent = Array.isArray(content);\n const needsStripping =\n hasArrayContent &&\n needsCacheStripping(content as MessageContentComplex[]);\n const needsCacheAdd =\n userMessagesModified < 2 &&\n isUserMessage &&\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 as MessageContentComplex));\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 = [\n { type: 'text', text: content },\n ] as MessageContentComplex[];\n } else {\n workingContent = [];\n }\n\n if (userMessagesModified >= 2 || !isUserMessage) {\n updatedMessages[i] = cloneMessage(\n originalMessage as MessageWithContent,\n workingContent\n ) as T;\n continue;\n }\n\n for (let j = workingContent.length - 1; j >= 0; j--) {\n const contentPart = workingContent[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 updatedMessages[i] = cloneMessage(\n originalMessage as MessageWithContent,\n workingContent\n ) as T;\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 clonedContent = deepCloneContent(content);\n for (let j = 0; j < clonedContent.length; j++) {\n const block = clonedContent[j] as Record<string, unknown>;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n updatedMessages[i] = cloneMessage(originalMessage, clonedContent);\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 clonedContent = deepCloneContent(content).filter(\n (block) => !isCachePoint(block as MessageContentComplex)\n );\n updatedMessages[i] = cloneMessage(originalMessage, clonedContent);\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] = cloneMessage(originalMessage, 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] = cloneMessage(originalMessage, 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] = cloneMessage(originalMessage, workingContent);\n messagesModified++;\n }\n\n return updatedMessages;\n}\n"],"names":["ContentTypes"],"mappings":";;;;AASA;;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,YAAY,CACnB,OAAU,EACV,OAAyC,EAAA;AAEzC,IAAA,MAAM,EACJ,SAAS,EAAE,UAAU,EACrB,eAAe,EAAE,gBAAgB,EACjC,YAAY,EAAE,aAAa,EAC3B,GAAG,IAAI,EACR,GAAG,OAIH;IAED,MAAM,MAAM,GAAG,EAAE,GAAG,IAAI,EAAE,OAAO,EAAO;;IAGxC,IACE,SAAS,IAAI,OAAO;AACpB,QAAA,OAAO,OAAO,CAAC,OAAO,KAAK,UAAU;AACrC,QAAA,EAAE,MAAM,IAAI,MAAM,CAAC,EACnB;AACA,QAAA,MAAM,OAAO,GAAI,OAAkC,CAAC,OAAO,EAAE;AAC7D,QAAA,MAAM,OAAO,GAA2B;AACtC,YAAA,KAAK,EAAE,MAAM;AACb,YAAA,EAAE,EAAE,WAAW;AACf,YAAA,MAAM,EAAE,QAAQ;AAChB,YAAA,IAAI,EAAE,MAAM;SACb;QACA,MAAkC,CAAC,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,OAAO;;AAGxE,IAAA,OAAO,MAAM;AACf;AAEA;;;AAGG;AACH,SAAS,mBAAmB,CAAC,OAAgC,EAAA;AAC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AAI,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,YAAY,CAAC,eAAe,EAAE,cAAc,CAAC;AAClE,QAAA,gBAAgB,EAAE;;AAGpB,IAAA,OAAO,eAAe;AACxB;;;;;;;"}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { ToolMessage, SystemMessage, AIMessage, HumanMessage } from '@langchain/core/messages';
|
|
2
1
|
import { ContentTypes } from '../common/enum.mjs';
|
|
3
2
|
|
|
4
3
|
/**
|
|
@@ -14,61 +13,24 @@ function deepCloneContent(content) {
|
|
|
14
13
|
return content;
|
|
15
14
|
}
|
|
16
15
|
/**
|
|
17
|
-
*
|
|
18
|
-
*
|
|
16
|
+
* Clones a message with deep-cloned content, explicitly excluding LangChain
|
|
17
|
+
* serialization metadata to prevent coercion issues.
|
|
19
18
|
*/
|
|
20
|
-
function
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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,
|
|
19
|
+
function cloneMessage(message, content) {
|
|
20
|
+
const { lc_kwargs: _lc_kwargs, lc_serializable: _lc_serializable, lc_namespace: _lc_namespace, ...rest } = message;
|
|
21
|
+
const cloned = { ...rest, content };
|
|
22
|
+
// LangChain messages don't have a direct 'role' property - derive it from getType()
|
|
23
|
+
if ('getType' in message &&
|
|
24
|
+
typeof message.getType === 'function' &&
|
|
25
|
+
!('role' in cloned)) {
|
|
26
|
+
const msgType = message.getType();
|
|
27
|
+
const roleMap = {
|
|
28
|
+
human: 'user',
|
|
29
|
+
ai: 'assistant',
|
|
30
|
+
system: 'system',
|
|
31
|
+
tool: 'tool',
|
|
40
32
|
};
|
|
41
|
-
|
|
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;
|
|
70
|
-
if (lcKwargs != null) {
|
|
71
|
-
cloned.lc_kwargs = { ...lcKwargs, content };
|
|
33
|
+
cloned.role = roleMap[msgType] || msgType;
|
|
72
34
|
}
|
|
73
35
|
return cloned;
|
|
74
36
|
}
|
|
@@ -134,29 +96,20 @@ function addCacheControl(messages) {
|
|
|
134
96
|
workingContent = [];
|
|
135
97
|
}
|
|
136
98
|
if (userMessagesModified >= 2 || !isUserMessage) {
|
|
137
|
-
updatedMessages[i] =
|
|
138
|
-
updatedMessages[i].content = workingContent;
|
|
99
|
+
updatedMessages[i] = cloneMessage(originalMessage, workingContent);
|
|
139
100
|
continue;
|
|
140
101
|
}
|
|
141
|
-
let cacheAdded = false;
|
|
142
102
|
for (let j = workingContent.length - 1; j >= 0; j--) {
|
|
143
103
|
const contentPart = workingContent[j];
|
|
144
104
|
if ('type' in contentPart && contentPart.type === 'text') {
|
|
145
105
|
contentPart.cache_control = {
|
|
146
106
|
type: 'ephemeral',
|
|
147
107
|
};
|
|
148
|
-
cacheAdded = true;
|
|
149
108
|
userMessagesModified++;
|
|
150
109
|
break;
|
|
151
110
|
}
|
|
152
111
|
}
|
|
153
|
-
|
|
154
|
-
updatedMessages[i] = createNewMessage(originalMessage, workingContent);
|
|
155
|
-
}
|
|
156
|
-
else {
|
|
157
|
-
updatedMessages[i] = shallowCloneMessage(originalMessage);
|
|
158
|
-
updatedMessages[i].content = workingContent;
|
|
159
|
-
}
|
|
112
|
+
updatedMessages[i] = cloneMessage(originalMessage, workingContent);
|
|
160
113
|
}
|
|
161
114
|
return updatedMessages;
|
|
162
115
|
}
|
|
@@ -192,14 +145,14 @@ function stripAnthropicCacheControl(messages) {
|
|
|
192
145
|
if (!Array.isArray(content) || !hasAnthropicCacheControl(content)) {
|
|
193
146
|
continue;
|
|
194
147
|
}
|
|
195
|
-
const
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
const block = message.content[j];
|
|
148
|
+
const clonedContent = deepCloneContent(content);
|
|
149
|
+
for (let j = 0; j < clonedContent.length; j++) {
|
|
150
|
+
const block = clonedContent[j];
|
|
199
151
|
if ('cache_control' in block) {
|
|
200
152
|
delete block.cache_control;
|
|
201
153
|
}
|
|
202
154
|
}
|
|
155
|
+
updatedMessages[i] = cloneMessage(originalMessage, clonedContent);
|
|
203
156
|
}
|
|
204
157
|
return updatedMessages;
|
|
205
158
|
}
|
|
@@ -229,9 +182,8 @@ function stripBedrockCacheControl(messages) {
|
|
|
229
182
|
if (!Array.isArray(content) || !hasBedrockCachePoint(content)) {
|
|
230
183
|
continue;
|
|
231
184
|
}
|
|
232
|
-
const
|
|
233
|
-
updatedMessages[i] =
|
|
234
|
-
message.content = message.content.filter((block) => !isCachePoint(block));
|
|
185
|
+
const clonedContent = deepCloneContent(content).filter((block) => !isCachePoint(block));
|
|
186
|
+
updatedMessages[i] = cloneMessage(originalMessage, clonedContent);
|
|
235
187
|
}
|
|
236
188
|
return updatedMessages;
|
|
237
189
|
}
|
|
@@ -286,8 +238,7 @@ function addBedrockCacheControl(messages) {
|
|
|
286
238
|
workingContent = [];
|
|
287
239
|
}
|
|
288
240
|
if (messagesModified >= 2 || isToolMessage || isEmptyString) {
|
|
289
|
-
updatedMessages[i] =
|
|
290
|
-
updatedMessages[i].content = workingContent;
|
|
241
|
+
updatedMessages[i] = cloneMessage(originalMessage, workingContent);
|
|
291
242
|
continue;
|
|
292
243
|
}
|
|
293
244
|
if (workingContent.length === 0) {
|
|
@@ -303,8 +254,7 @@ function addBedrockCacheControl(messages) {
|
|
|
303
254
|
}
|
|
304
255
|
}
|
|
305
256
|
if (!hasCacheableContent) {
|
|
306
|
-
updatedMessages[i] =
|
|
307
|
-
updatedMessages[i].content = workingContent;
|
|
257
|
+
updatedMessages[i] = cloneMessage(originalMessage, workingContent);
|
|
308
258
|
continue;
|
|
309
259
|
}
|
|
310
260
|
let inserted = false;
|
|
@@ -328,7 +278,7 @@ function addBedrockCacheControl(messages) {
|
|
|
328
278
|
cachePoint: { type: 'default' },
|
|
329
279
|
});
|
|
330
280
|
}
|
|
331
|
-
updatedMessages[i] =
|
|
281
|
+
updatedMessages[i] = cloneMessage(originalMessage, workingContent);
|
|
332
282
|
messagesModified++;
|
|
333
283
|
}
|
|
334
284
|
return updatedMessages;
|
|
@@ -1 +1 @@
|
|
|
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 content = originalMessage.content;\n const isUserMessage =\n ('getType' in originalMessage && originalMessage.getType() === 'human') ||\n ('role' in originalMessage && originalMessage.role === 'user');\n\n const hasArrayContent = Array.isArray(content);\n const needsStripping =\n hasArrayContent &&\n needsCacheStripping(content as MessageContentComplex[]);\n const needsCacheAdd =\n userMessagesModified < 2 &&\n isUserMessage &&\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 as MessageContentComplex));\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 = [\n { type: 'text', text: content },\n ] as MessageContentComplex[];\n } else {\n workingContent = [];\n }\n\n if (userMessagesModified >= 2 || !isUserMessage) {\n updatedMessages[i] = shallowCloneMessage(\n originalMessage as MessageWithContent\n ) as T;\n (updatedMessages[i] as MessageWithContent).content = workingContent;\n continue;\n }\n\n let cacheAdded = false;\n for (let j = workingContent.length - 1; j >= 0; j--) {\n const contentPart = workingContent[j];\n if ('type' in contentPart && contentPart.type === 'text') {\n (contentPart as Anthropic.TextBlockParam).cache_control = {\n type: 'ephemeral',\n };\n cacheAdded = true;\n userMessagesModified++;\n break;\n }\n }\n\n if (cacheAdded) {\n updatedMessages[i] = createNewMessage(\n originalMessage as MessageWithContent,\n workingContent\n ) as T;\n } else {\n updatedMessages[i] = shallowCloneMessage(\n originalMessage as MessageWithContent\n ) as T;\n (updatedMessages[i] as MessageWithContent).content = workingContent;\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,O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AC,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,KAAK,YAAY,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
|
+
{"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 */\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, explicitly excluding LangChain\n * serialization metadata to prevent coercion issues.\n */\nfunction cloneMessage<T extends MessageWithContent>(\n message: T,\n content: string | MessageContentComplex[]\n): T {\n const {\n lc_kwargs: _lc_kwargs,\n lc_serializable: _lc_serializable,\n lc_namespace: _lc_namespace,\n ...rest\n } = message as T & {\n lc_kwargs?: unknown;\n lc_serializable?: unknown;\n lc_namespace?: unknown;\n };\n\n const cloned = { ...rest, content } as T;\n\n // LangChain messages don't have a direct 'role' property - derive it from getType()\n if (\n 'getType' in message &&\n typeof message.getType === 'function' &&\n !('role' in cloned)\n ) {\n const msgType = (message as unknown as BaseMessage).getType();\n const roleMap: Record<string, string> = {\n human: 'user',\n ai: 'assistant',\n system: 'system',\n tool: 'tool',\n };\n (cloned as Record<string, unknown>).role = roleMap[msgType] || msgType;\n }\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 content = originalMessage.content;\n const isUserMessage =\n ('getType' in originalMessage && originalMessage.getType() === 'human') ||\n ('role' in originalMessage && originalMessage.role === 'user');\n\n const hasArrayContent = Array.isArray(content);\n const needsStripping =\n hasArrayContent &&\n needsCacheStripping(content as MessageContentComplex[]);\n const needsCacheAdd =\n userMessagesModified < 2 &&\n isUserMessage &&\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 as MessageContentComplex));\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 = [\n { type: 'text', text: content },\n ] as MessageContentComplex[];\n } else {\n workingContent = [];\n }\n\n if (userMessagesModified >= 2 || !isUserMessage) {\n updatedMessages[i] = cloneMessage(\n originalMessage as MessageWithContent,\n workingContent\n ) as T;\n continue;\n }\n\n for (let j = workingContent.length - 1; j >= 0; j--) {\n const contentPart = workingContent[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 updatedMessages[i] = cloneMessage(\n originalMessage as MessageWithContent,\n workingContent\n ) as T;\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 clonedContent = deepCloneContent(content);\n for (let j = 0; j < clonedContent.length; j++) {\n const block = clonedContent[j] as Record<string, unknown>;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n updatedMessages[i] = cloneMessage(originalMessage, clonedContent);\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 clonedContent = deepCloneContent(content).filter(\n (block) => !isCachePoint(block as MessageContentComplex)\n );\n updatedMessages[i] = cloneMessage(originalMessage, clonedContent);\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] = cloneMessage(originalMessage, 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] = cloneMessage(originalMessage, 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] = cloneMessage(originalMessage, workingContent);\n messagesModified++;\n }\n\n return updatedMessages;\n}\n"],"names":[],"mappings":";;AASA;;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,YAAY,CACnB,OAAU,EACV,OAAyC,EAAA;AAEzC,IAAA,MAAM,EACJ,SAAS,EAAE,UAAU,EACrB,eAAe,EAAE,gBAAgB,EACjC,YAAY,EAAE,aAAa,EAC3B,GAAG,IAAI,EACR,GAAG,OAIH;IAED,MAAM,MAAM,GAAG,EAAE,GAAG,IAAI,EAAE,OAAO,EAAO;;IAGxC,IACE,SAAS,IAAI,OAAO;AACpB,QAAA,OAAO,OAAO,CAAC,OAAO,KAAK,UAAU;AACrC,QAAA,EAAE,MAAM,IAAI,MAAM,CAAC,EACnB;AACA,QAAA,MAAM,OAAO,GAAI,OAAkC,CAAC,OAAO,EAAE;AAC7D,QAAA,MAAM,OAAO,GAA2B;AACtC,YAAA,KAAK,EAAE,MAAM;AACb,YAAA,EAAE,EAAE,WAAW;AACf,YAAA,MAAM,EAAE,QAAQ;AAChB,YAAA,IAAI,EAAE,MAAM;SACb;QACA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AACxD,YAAA,MAAM,IAAI,GAAI,KAA2B,CAAC,IAAI;YAC9C,IAAI,IAAI,KAAK,YAAY,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,YAAY,CAAC,eAAe,EAAE,cAAc,CAAC;AAClE,QAAA,gBAAgB,EAAE;;AAGpB,IAAA,OAAO,eAAe;AACxB;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@librechat/agents",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.776",
|
|
4
4
|
"main": "./dist/cjs/main.cjs",
|
|
5
5
|
"module": "./dist/esm/main.mjs",
|
|
6
6
|
"types": "./dist/types/index.d.ts",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"code_exec_files": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/code_exec_files.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
|
|
53
53
|
"code_exec_session": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/code_exec_session.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
|
|
54
54
|
"code_exec_simple": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/code_exec_simple.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
|
|
55
|
-
"simple": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/simple.ts --provider '
|
|
55
|
+
"simple": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/simple.ts --provider 'bedrock' --name 'Jo' --location 'New York, NY'",
|
|
56
56
|
"caching": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/caching.ts --name 'Jo' --location 'New York, NY'",
|
|
57
57
|
"thinking": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/thinking.ts --name 'Jo' --location 'New York, NY'",
|
|
58
58
|
"memory": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/memory.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
|
|
@@ -95,7 +95,7 @@
|
|
|
95
95
|
"start:collab5": "node --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/collab_design_v5.ts",
|
|
96
96
|
"start:dev": "node --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/main.ts",
|
|
97
97
|
"supervised": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/supervised.ts --provider anthropic --name Jo --location \"New York, NY\"",
|
|
98
|
-
"test": "jest",
|
|
98
|
+
"test": "NODE_OPTIONS='--experimental-vm-modules' jest",
|
|
99
99
|
"test:memory": "NODE_OPTIONS='--expose-gc' npx jest src/specs/title.memory-leak.test.ts",
|
|
100
100
|
"test:all": "npm test -- --testPathIgnorePatterns=title.memory-leak.test.ts && npm run test:memory",
|
|
101
101
|
"reinstall": "npm run clean && npm ci && rm -rf ./dist && npm run build",
|
|
@@ -921,7 +921,7 @@ describe('Immutability - addCacheControl does not mutate original messages', ()
|
|
|
921
921
|
expect('cache_control' in originalFirstBlock).toBe(true);
|
|
922
922
|
});
|
|
923
923
|
|
|
924
|
-
it('should
|
|
924
|
+
it('should remove lc_kwargs to prevent serialization mismatch for LangChain messages', () => {
|
|
925
925
|
type LangChainLikeMsg = TestMsg & {
|
|
926
926
|
lc_kwargs?: { content?: MessageContentComplex[] };
|
|
927
927
|
};
|
|
@@ -955,14 +955,11 @@ describe('Immutability - addCacheControl does not mutate original messages', ()
|
|
|
955
955
|
const resultFirst = result[0] as LangChainLikeMsg;
|
|
956
956
|
const resultThird = result[2] as LangChainLikeMsg;
|
|
957
957
|
|
|
958
|
-
expect(resultFirst.
|
|
959
|
-
expect(resultThird.
|
|
958
|
+
expect(resultFirst.lc_kwargs).toBeUndefined();
|
|
959
|
+
expect(resultThird.lc_kwargs).toBeUndefined();
|
|
960
960
|
|
|
961
961
|
const firstContent = resultFirst.content as MessageContentComplex[];
|
|
962
|
-
const firstLcContent = resultFirst.lc_kwargs
|
|
963
|
-
?.content as MessageContentComplex[];
|
|
964
962
|
expect('cache_control' in firstContent[0]).toBe(true);
|
|
965
|
-
expect('cache_control' in firstLcContent[0]).toBe(true);
|
|
966
963
|
|
|
967
964
|
const originalFirst = messagesWithLcKwargs[0];
|
|
968
965
|
const originalContent = originalFirst.content as MessageContentComplex[];
|
|
@@ -1100,7 +1097,7 @@ describe('Immutability - addBedrockCacheControl does not mutate original message
|
|
|
1100
1097
|
expect('cache_control' in anthropicFirstContent[0]).toBe(true);
|
|
1101
1098
|
});
|
|
1102
1099
|
|
|
1103
|
-
it('should
|
|
1100
|
+
it('should remove lc_kwargs to prevent serialization mismatch for LangChain messages', () => {
|
|
1104
1101
|
type LangChainLikeMsg = TestMsg & {
|
|
1105
1102
|
lc_kwargs?: { content?: MessageContentComplex[] };
|
|
1106
1103
|
};
|
|
@@ -1127,14 +1124,11 @@ describe('Immutability - addBedrockCacheControl does not mutate original message
|
|
|
1127
1124
|
const resultFirst = bedrockResult[0] as LangChainLikeMsg;
|
|
1128
1125
|
const resultSecond = bedrockResult[1] as LangChainLikeMsg;
|
|
1129
1126
|
|
|
1130
|
-
expect(resultFirst.
|
|
1131
|
-
expect(resultSecond.
|
|
1127
|
+
expect(resultFirst.lc_kwargs).toBeUndefined();
|
|
1128
|
+
expect(resultSecond.lc_kwargs).toBeUndefined();
|
|
1132
1129
|
|
|
1133
1130
|
const firstContent = resultFirst.content as MessageContentComplex[];
|
|
1134
|
-
const firstLcContent = resultFirst.lc_kwargs
|
|
1135
|
-
?.content as MessageContentComplex[];
|
|
1136
1131
|
expect(firstContent.some((b) => 'cachePoint' in b)).toBe(true);
|
|
1137
|
-
expect(firstLcContent.some((b) => 'cachePoint' in b)).toBe(true);
|
|
1138
1132
|
|
|
1139
1133
|
const originalFirst = messagesWithLcKwargs[0];
|
|
1140
1134
|
const originalContent = originalFirst.content as MessageContentComplex[];
|
package/src/messages/cache.ts
CHANGED
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
BaseMessage,
|
|
3
|
-
MessageContentComplex,
|
|
4
|
-
AIMessage,
|
|
5
|
-
HumanMessage,
|
|
6
|
-
SystemMessage,
|
|
7
|
-
ToolMessage,
|
|
8
|
-
} from '@langchain/core/messages';
|
|
1
|
+
import { BaseMessage, MessageContentComplex } from '@langchain/core/messages';
|
|
9
2
|
import type { AnthropicMessage } from '@/types/messages';
|
|
10
3
|
import type Anthropic from '@anthropic-ai/sdk';
|
|
11
4
|
import { ContentTypes } from '@/common/enum';
|
|
@@ -30,73 +23,42 @@ function deepCloneContent<T extends string | MessageContentComplex[]>(
|
|
|
30
23
|
}
|
|
31
24
|
|
|
32
25
|
/**
|
|
33
|
-
*
|
|
34
|
-
*
|
|
26
|
+
* Clones a message with deep-cloned content, explicitly excluding LangChain
|
|
27
|
+
* serialization metadata to prevent coercion issues.
|
|
35
28
|
*/
|
|
36
|
-
function
|
|
37
|
-
return {
|
|
38
|
-
...message,
|
|
39
|
-
content: deepCloneContent(message.content ?? ''),
|
|
40
|
-
} as T;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Creates a new LangChain message instance with the given content.
|
|
45
|
-
* Required when adding cache points to ensure proper serialization.
|
|
46
|
-
*/
|
|
47
|
-
function createNewMessage<T extends MessageWithContent>(
|
|
29
|
+
function cloneMessage<T extends MessageWithContent>(
|
|
48
30
|
message: T,
|
|
49
|
-
content: MessageContentComplex[]
|
|
31
|
+
content: string | MessageContentComplex[]
|
|
50
32
|
): T {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
33
|
+
const {
|
|
34
|
+
lc_kwargs: _lc_kwargs,
|
|
35
|
+
lc_serializable: _lc_serializable,
|
|
36
|
+
lc_namespace: _lc_namespace,
|
|
37
|
+
...rest
|
|
38
|
+
} = message as T & {
|
|
39
|
+
lc_kwargs?: unknown;
|
|
40
|
+
lc_serializable?: unknown;
|
|
41
|
+
lc_namespace?: unknown;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const cloned = { ...rest, content } as T;
|
|
45
|
+
|
|
46
|
+
// LangChain messages don't have a direct 'role' property - derive it from getType()
|
|
47
|
+
if (
|
|
48
|
+
'getType' in message &&
|
|
49
|
+
typeof message.getType === 'function' &&
|
|
50
|
+
!('role' in cloned)
|
|
51
|
+
) {
|
|
52
|
+
const msgType = (message as unknown as BaseMessage).getType();
|
|
53
|
+
const roleMap: Record<string, string> = {
|
|
54
|
+
human: 'user',
|
|
55
|
+
ai: 'assistant',
|
|
56
|
+
system: 'system',
|
|
57
|
+
tool: 'tool',
|
|
61
58
|
};
|
|
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
|
-
}
|
|
59
|
+
(cloned as Record<string, unknown>).role = roleMap[msgType] || msgType;
|
|
91
60
|
}
|
|
92
61
|
|
|
93
|
-
const cloned = { ...message, content } as T;
|
|
94
|
-
const lcKwargs = (cloned as Record<string, unknown>).lc_kwargs as
|
|
95
|
-
| Record<string, unknown>
|
|
96
|
-
| undefined;
|
|
97
|
-
if (lcKwargs != null) {
|
|
98
|
-
(cloned as Record<string, unknown>).lc_kwargs = { ...lcKwargs, content };
|
|
99
|
-
}
|
|
100
62
|
return cloned;
|
|
101
63
|
}
|
|
102
64
|
|
|
@@ -174,37 +136,28 @@ export function addCacheControl<T extends AnthropicMessage | BaseMessage>(
|
|
|
174
136
|
}
|
|
175
137
|
|
|
176
138
|
if (userMessagesModified >= 2 || !isUserMessage) {
|
|
177
|
-
updatedMessages[i] =
|
|
178
|
-
originalMessage as MessageWithContent
|
|
139
|
+
updatedMessages[i] = cloneMessage(
|
|
140
|
+
originalMessage as MessageWithContent,
|
|
141
|
+
workingContent
|
|
179
142
|
) as T;
|
|
180
|
-
(updatedMessages[i] as MessageWithContent).content = workingContent;
|
|
181
143
|
continue;
|
|
182
144
|
}
|
|
183
145
|
|
|
184
|
-
let cacheAdded = false;
|
|
185
146
|
for (let j = workingContent.length - 1; j >= 0; j--) {
|
|
186
147
|
const contentPart = workingContent[j];
|
|
187
148
|
if ('type' in contentPart && contentPart.type === 'text') {
|
|
188
149
|
(contentPart as Anthropic.TextBlockParam).cache_control = {
|
|
189
150
|
type: 'ephemeral',
|
|
190
151
|
};
|
|
191
|
-
cacheAdded = true;
|
|
192
152
|
userMessagesModified++;
|
|
193
153
|
break;
|
|
194
154
|
}
|
|
195
155
|
}
|
|
196
156
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
) as T;
|
|
202
|
-
} else {
|
|
203
|
-
updatedMessages[i] = shallowCloneMessage(
|
|
204
|
-
originalMessage as MessageWithContent
|
|
205
|
-
) as T;
|
|
206
|
-
(updatedMessages[i] as MessageWithContent).content = workingContent;
|
|
207
|
-
}
|
|
157
|
+
updatedMessages[i] = cloneMessage(
|
|
158
|
+
originalMessage as MessageWithContent,
|
|
159
|
+
workingContent
|
|
160
|
+
) as T;
|
|
208
161
|
}
|
|
209
162
|
|
|
210
163
|
return updatedMessages;
|
|
@@ -249,22 +202,14 @@ export function stripAnthropicCacheControl<T extends MessageWithContent>(
|
|
|
249
202
|
continue;
|
|
250
203
|
}
|
|
251
204
|
|
|
252
|
-
const
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
for (
|
|
256
|
-
let j = 0;
|
|
257
|
-
j < (message.content as MessageContentComplex[]).length;
|
|
258
|
-
j++
|
|
259
|
-
) {
|
|
260
|
-
const block = (message.content as MessageContentComplex[])[j] as Record<
|
|
261
|
-
string,
|
|
262
|
-
unknown
|
|
263
|
-
>;
|
|
205
|
+
const clonedContent = deepCloneContent(content);
|
|
206
|
+
for (let j = 0; j < clonedContent.length; j++) {
|
|
207
|
+
const block = clonedContent[j] as Record<string, unknown>;
|
|
264
208
|
if ('cache_control' in block) {
|
|
265
209
|
delete block.cache_control;
|
|
266
210
|
}
|
|
267
211
|
}
|
|
212
|
+
updatedMessages[i] = cloneMessage(originalMessage, clonedContent);
|
|
268
213
|
}
|
|
269
214
|
|
|
270
215
|
return updatedMessages;
|
|
@@ -302,12 +247,10 @@ export function stripBedrockCacheControl<T extends MessageWithContent>(
|
|
|
302
247
|
continue;
|
|
303
248
|
}
|
|
304
249
|
|
|
305
|
-
const
|
|
306
|
-
updatedMessages[i] = message;
|
|
307
|
-
|
|
308
|
-
message.content = (message.content as MessageContentComplex[]).filter(
|
|
250
|
+
const clonedContent = deepCloneContent(content).filter(
|
|
309
251
|
(block) => !isCachePoint(block as MessageContentComplex)
|
|
310
|
-
)
|
|
252
|
+
);
|
|
253
|
+
updatedMessages[i] = cloneMessage(originalMessage, clonedContent);
|
|
311
254
|
}
|
|
312
255
|
|
|
313
256
|
return updatedMessages;
|
|
@@ -377,8 +320,7 @@ export function addBedrockCacheControl<
|
|
|
377
320
|
}
|
|
378
321
|
|
|
379
322
|
if (messagesModified >= 2 || isToolMessage || isEmptyString) {
|
|
380
|
-
updatedMessages[i] =
|
|
381
|
-
(updatedMessages[i] as MessageWithContent).content = workingContent;
|
|
323
|
+
updatedMessages[i] = cloneMessage(originalMessage, workingContent);
|
|
382
324
|
continue;
|
|
383
325
|
}
|
|
384
326
|
|
|
@@ -397,8 +339,7 @@ export function addBedrockCacheControl<
|
|
|
397
339
|
}
|
|
398
340
|
|
|
399
341
|
if (!hasCacheableContent) {
|
|
400
|
-
updatedMessages[i] =
|
|
401
|
-
(updatedMessages[i] as MessageWithContent).content = workingContent;
|
|
342
|
+
updatedMessages[i] = cloneMessage(originalMessage, workingContent);
|
|
402
343
|
continue;
|
|
403
344
|
}
|
|
404
345
|
|
|
@@ -424,7 +365,7 @@ export function addBedrockCacheControl<
|
|
|
424
365
|
} as MessageContentComplex);
|
|
425
366
|
}
|
|
426
367
|
|
|
427
|
-
updatedMessages[i] =
|
|
368
|
+
updatedMessages[i] = cloneMessage(originalMessage, workingContent);
|
|
428
369
|
messagesModified++;
|
|
429
370
|
}
|
|
430
371
|
|
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
+
import { config } from 'dotenv';
|
|
4
|
+
config();
|
|
5
|
+
import { Calculator } from '@/tools/Calculator';
|
|
6
|
+
import {
|
|
7
|
+
AIMessage,
|
|
8
|
+
BaseMessage,
|
|
9
|
+
HumanMessage,
|
|
10
|
+
UsageMetadata,
|
|
11
|
+
} from '@langchain/core/messages';
|
|
12
|
+
import type * as t from '@/types';
|
|
13
|
+
import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
|
|
14
|
+
import { ModelEndHandler, ToolEndHandler } from '@/events';
|
|
15
|
+
import { capitalizeFirstLetter } from './spec.utils';
|
|
16
|
+
import { GraphEvents, Providers } from '@/common';
|
|
17
|
+
import { getLLMConfig } from '@/utils/llmConfig';
|
|
18
|
+
import { getArgs } from '@/scripts/args';
|
|
19
|
+
import { Run } from '@/run';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* These tests verify that prompt caching works correctly across multi-turn
|
|
23
|
+
* conversations and that messages are not mutated in place.
|
|
24
|
+
*/
|
|
25
|
+
describe('Prompt Caching Integration Tests', () => {
|
|
26
|
+
jest.setTimeout(120000);
|
|
27
|
+
|
|
28
|
+
const setupTest = (): {
|
|
29
|
+
collectedUsage: UsageMetadata[];
|
|
30
|
+
contentParts: Array<t.MessageContentComplex | undefined>;
|
|
31
|
+
customHandlers: Record<string | GraphEvents, t.EventHandler>;
|
|
32
|
+
} => {
|
|
33
|
+
const collectedUsage: UsageMetadata[] = [];
|
|
34
|
+
const { contentParts, aggregateContent } = createContentAggregator();
|
|
35
|
+
|
|
36
|
+
const customHandlers: Record<string | GraphEvents, t.EventHandler> = {
|
|
37
|
+
[GraphEvents.TOOL_END]: new ToolEndHandler(),
|
|
38
|
+
[GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(collectedUsage),
|
|
39
|
+
[GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
|
|
40
|
+
[GraphEvents.ON_RUN_STEP_COMPLETED]: {
|
|
41
|
+
handle: (
|
|
42
|
+
event: GraphEvents.ON_RUN_STEP_COMPLETED,
|
|
43
|
+
data: t.StreamEventData
|
|
44
|
+
): void => {
|
|
45
|
+
aggregateContent({
|
|
46
|
+
event,
|
|
47
|
+
data: data as unknown as { result: t.ToolEndEvent },
|
|
48
|
+
});
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
[GraphEvents.ON_RUN_STEP]: {
|
|
52
|
+
handle: (
|
|
53
|
+
event: GraphEvents.ON_RUN_STEP,
|
|
54
|
+
data: t.StreamEventData
|
|
55
|
+
): void => {
|
|
56
|
+
aggregateContent({ event, data: data as t.RunStep });
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
[GraphEvents.ON_RUN_STEP_DELTA]: {
|
|
60
|
+
handle: (
|
|
61
|
+
event: GraphEvents.ON_RUN_STEP_DELTA,
|
|
62
|
+
data: t.StreamEventData
|
|
63
|
+
): void => {
|
|
64
|
+
aggregateContent({ event, data: data as t.RunStepDeltaEvent });
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
[GraphEvents.ON_MESSAGE_DELTA]: {
|
|
68
|
+
handle: (
|
|
69
|
+
event: GraphEvents.ON_MESSAGE_DELTA,
|
|
70
|
+
data: t.StreamEventData
|
|
71
|
+
): void => {
|
|
72
|
+
aggregateContent({ event, data: data as t.MessageDeltaEvent });
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
return { collectedUsage, contentParts, customHandlers };
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const streamConfig = {
|
|
81
|
+
configurable: { thread_id: 'cache-test-thread' },
|
|
82
|
+
streamMode: 'values',
|
|
83
|
+
version: 'v2' as const,
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
describe('Anthropic Prompt Caching', () => {
|
|
87
|
+
const provider = Providers.ANTHROPIC;
|
|
88
|
+
|
|
89
|
+
test(`${capitalizeFirstLetter(provider)}: multi-turn conversation with caching should not corrupt messages`, async () => {
|
|
90
|
+
const { userName, location } = await getArgs();
|
|
91
|
+
const llmConfig = getLLMConfig(provider);
|
|
92
|
+
const { collectedUsage, customHandlers } = setupTest();
|
|
93
|
+
|
|
94
|
+
const run = await Run.create<t.IState>({
|
|
95
|
+
runId: 'cache-test-anthropic',
|
|
96
|
+
graphConfig: {
|
|
97
|
+
type: 'standard',
|
|
98
|
+
llmConfig: { ...llmConfig, promptCache: true } as t.LLMConfig,
|
|
99
|
+
tools: [new Calculator()],
|
|
100
|
+
instructions: 'You are a helpful assistant.',
|
|
101
|
+
additional_instructions: `User: ${userName}, Location: ${location}`,
|
|
102
|
+
},
|
|
103
|
+
returnContent: true,
|
|
104
|
+
customHandlers,
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// Turn 1
|
|
108
|
+
const turn1Messages: BaseMessage[] = [
|
|
109
|
+
new HumanMessage('Hello, what is 2+2?'),
|
|
110
|
+
];
|
|
111
|
+
const turn1ContentSnapshot = JSON.stringify(turn1Messages[0].content);
|
|
112
|
+
|
|
113
|
+
const turn1Result = await run.processStream(
|
|
114
|
+
{ messages: turn1Messages },
|
|
115
|
+
streamConfig
|
|
116
|
+
);
|
|
117
|
+
expect(turn1Result).toBeDefined();
|
|
118
|
+
|
|
119
|
+
// Verify original message was NOT mutated
|
|
120
|
+
expect(JSON.stringify(turn1Messages[0].content)).toBe(
|
|
121
|
+
turn1ContentSnapshot
|
|
122
|
+
);
|
|
123
|
+
expect((turn1Messages[0] as any).content).not.toContain('cache_control');
|
|
124
|
+
|
|
125
|
+
const turn1RunMessages = run.getRunMessages();
|
|
126
|
+
expect(turn1RunMessages).toBeDefined();
|
|
127
|
+
expect(turn1RunMessages!.length).toBeGreaterThan(0);
|
|
128
|
+
|
|
129
|
+
// Turn 2 - build on conversation
|
|
130
|
+
const turn2Messages: BaseMessage[] = [
|
|
131
|
+
...turn1Messages,
|
|
132
|
+
...turn1RunMessages!,
|
|
133
|
+
new HumanMessage('Now multiply that by 10'),
|
|
134
|
+
];
|
|
135
|
+
const turn2HumanContentSnapshot = JSON.stringify(
|
|
136
|
+
turn2Messages[turn2Messages.length - 1].content
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
const run2 = await Run.create<t.IState>({
|
|
140
|
+
runId: 'cache-test-anthropic-2',
|
|
141
|
+
graphConfig: {
|
|
142
|
+
type: 'standard',
|
|
143
|
+
llmConfig: { ...llmConfig, promptCache: true } as t.LLMConfig,
|
|
144
|
+
tools: [new Calculator()],
|
|
145
|
+
instructions: 'You are a helpful assistant.',
|
|
146
|
+
additional_instructions: `User: ${userName}, Location: ${location}`,
|
|
147
|
+
},
|
|
148
|
+
returnContent: true,
|
|
149
|
+
customHandlers,
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
const turn2Result = await run2.processStream(
|
|
153
|
+
{ messages: turn2Messages },
|
|
154
|
+
streamConfig
|
|
155
|
+
);
|
|
156
|
+
expect(turn2Result).toBeDefined();
|
|
157
|
+
|
|
158
|
+
// Verify messages were NOT mutated
|
|
159
|
+
expect(
|
|
160
|
+
JSON.stringify(turn2Messages[turn2Messages.length - 1].content)
|
|
161
|
+
).toBe(turn2HumanContentSnapshot);
|
|
162
|
+
|
|
163
|
+
// Check that we got cache read tokens (indicating caching worked)
|
|
164
|
+
console.log(`${provider} Usage:`, collectedUsage);
|
|
165
|
+
expect(collectedUsage.length).toBeGreaterThan(0);
|
|
166
|
+
|
|
167
|
+
console.log(
|
|
168
|
+
`${capitalizeFirstLetter(provider)} multi-turn caching test passed - messages not mutated`
|
|
169
|
+
);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test(`${capitalizeFirstLetter(provider)}: tool calls should work with caching enabled`, async () => {
|
|
173
|
+
const llmConfig = getLLMConfig(provider);
|
|
174
|
+
const { customHandlers } = setupTest();
|
|
175
|
+
|
|
176
|
+
const run = await Run.create<t.IState>({
|
|
177
|
+
runId: 'cache-test-anthropic-tools',
|
|
178
|
+
graphConfig: {
|
|
179
|
+
type: 'standard',
|
|
180
|
+
llmConfig: { ...llmConfig, promptCache: true } as t.LLMConfig,
|
|
181
|
+
tools: [new Calculator()],
|
|
182
|
+
instructions:
|
|
183
|
+
'You are a math assistant. Use the calculator tool for all calculations.',
|
|
184
|
+
},
|
|
185
|
+
returnContent: true,
|
|
186
|
+
customHandlers,
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
const messages: BaseMessage[] = [
|
|
190
|
+
new HumanMessage('Calculate 123 * 456 using the calculator'),
|
|
191
|
+
];
|
|
192
|
+
|
|
193
|
+
const result = await run.processStream({ messages }, streamConfig);
|
|
194
|
+
expect(result).toBeDefined();
|
|
195
|
+
|
|
196
|
+
const runMessages = run.getRunMessages();
|
|
197
|
+
expect(runMessages).toBeDefined();
|
|
198
|
+
|
|
199
|
+
// Should have used the calculator tool
|
|
200
|
+
const hasToolUse = runMessages?.some(
|
|
201
|
+
(msg) =>
|
|
202
|
+
msg._getType() === 'ai' &&
|
|
203
|
+
((msg as AIMessage).tool_calls?.length ?? 0) > 0
|
|
204
|
+
);
|
|
205
|
+
expect(hasToolUse).toBe(true);
|
|
206
|
+
|
|
207
|
+
console.log(
|
|
208
|
+
`${capitalizeFirstLetter(provider)} tool call with caching test passed`
|
|
209
|
+
);
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
describe('Bedrock Prompt Caching', () => {
|
|
214
|
+
const provider = Providers.BEDROCK;
|
|
215
|
+
|
|
216
|
+
test(`${capitalizeFirstLetter(provider)}: multi-turn conversation with caching should not corrupt messages`, async () => {
|
|
217
|
+
const { userName, location } = await getArgs();
|
|
218
|
+
const llmConfig = getLLMConfig(provider);
|
|
219
|
+
const { collectedUsage, customHandlers } = setupTest();
|
|
220
|
+
|
|
221
|
+
const run = await Run.create<t.IState>({
|
|
222
|
+
runId: 'cache-test-bedrock',
|
|
223
|
+
graphConfig: {
|
|
224
|
+
type: 'standard',
|
|
225
|
+
llmConfig: { ...llmConfig, promptCache: true } as t.LLMConfig,
|
|
226
|
+
tools: [new Calculator()],
|
|
227
|
+
instructions: 'You are a helpful assistant.',
|
|
228
|
+
additional_instructions: `User: ${userName}, Location: ${location}`,
|
|
229
|
+
},
|
|
230
|
+
returnContent: true,
|
|
231
|
+
customHandlers,
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
// Turn 1
|
|
235
|
+
const turn1Messages: BaseMessage[] = [
|
|
236
|
+
new HumanMessage('Hello, what is 5+5?'),
|
|
237
|
+
];
|
|
238
|
+
const turn1ContentSnapshot = JSON.stringify(turn1Messages[0].content);
|
|
239
|
+
|
|
240
|
+
const turn1Result = await run.processStream(
|
|
241
|
+
{ messages: turn1Messages },
|
|
242
|
+
streamConfig
|
|
243
|
+
);
|
|
244
|
+
expect(turn1Result).toBeDefined();
|
|
245
|
+
|
|
246
|
+
// Verify original message was NOT mutated
|
|
247
|
+
expect(JSON.stringify(turn1Messages[0].content)).toBe(
|
|
248
|
+
turn1ContentSnapshot
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
const turn1RunMessages = run.getRunMessages();
|
|
252
|
+
expect(turn1RunMessages).toBeDefined();
|
|
253
|
+
expect(turn1RunMessages!.length).toBeGreaterThan(0);
|
|
254
|
+
|
|
255
|
+
// Turn 2
|
|
256
|
+
const turn2Messages: BaseMessage[] = [
|
|
257
|
+
...turn1Messages,
|
|
258
|
+
...turn1RunMessages!,
|
|
259
|
+
new HumanMessage('Multiply that by 3'),
|
|
260
|
+
];
|
|
261
|
+
const turn2HumanContentSnapshot = JSON.stringify(
|
|
262
|
+
turn2Messages[turn2Messages.length - 1].content
|
|
263
|
+
);
|
|
264
|
+
|
|
265
|
+
const run2 = await Run.create<t.IState>({
|
|
266
|
+
runId: 'cache-test-bedrock-2',
|
|
267
|
+
graphConfig: {
|
|
268
|
+
type: 'standard',
|
|
269
|
+
llmConfig: { ...llmConfig, promptCache: true } as t.LLMConfig,
|
|
270
|
+
tools: [new Calculator()],
|
|
271
|
+
instructions: 'You are a helpful assistant.',
|
|
272
|
+
additional_instructions: `User: ${userName}, Location: ${location}`,
|
|
273
|
+
},
|
|
274
|
+
returnContent: true,
|
|
275
|
+
customHandlers,
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
const turn2Result = await run2.processStream(
|
|
279
|
+
{ messages: turn2Messages },
|
|
280
|
+
streamConfig
|
|
281
|
+
);
|
|
282
|
+
expect(turn2Result).toBeDefined();
|
|
283
|
+
|
|
284
|
+
// Verify messages were NOT mutated
|
|
285
|
+
expect(
|
|
286
|
+
JSON.stringify(turn2Messages[turn2Messages.length - 1].content)
|
|
287
|
+
).toBe(turn2HumanContentSnapshot);
|
|
288
|
+
|
|
289
|
+
console.log(`${provider} Usage:`, collectedUsage);
|
|
290
|
+
expect(collectedUsage.length).toBeGreaterThan(0);
|
|
291
|
+
|
|
292
|
+
console.log(
|
|
293
|
+
`${capitalizeFirstLetter(provider)} multi-turn caching test passed - messages not mutated`
|
|
294
|
+
);
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
test(`${capitalizeFirstLetter(provider)}: tool calls should work with caching enabled`, async () => {
|
|
298
|
+
const llmConfig = getLLMConfig(provider);
|
|
299
|
+
const { customHandlers } = setupTest();
|
|
300
|
+
|
|
301
|
+
const run = await Run.create<t.IState>({
|
|
302
|
+
runId: 'cache-test-bedrock-tools',
|
|
303
|
+
graphConfig: {
|
|
304
|
+
type: 'standard',
|
|
305
|
+
llmConfig: { ...llmConfig, promptCache: true } as t.LLMConfig,
|
|
306
|
+
tools: [new Calculator()],
|
|
307
|
+
instructions:
|
|
308
|
+
'You are a math assistant. Use the calculator tool for all calculations.',
|
|
309
|
+
},
|
|
310
|
+
returnContent: true,
|
|
311
|
+
customHandlers,
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
const messages: BaseMessage[] = [
|
|
315
|
+
new HumanMessage('Calculate 789 * 123 using the calculator'),
|
|
316
|
+
];
|
|
317
|
+
|
|
318
|
+
const result = await run.processStream({ messages }, streamConfig);
|
|
319
|
+
expect(result).toBeDefined();
|
|
320
|
+
|
|
321
|
+
const runMessages = run.getRunMessages();
|
|
322
|
+
expect(runMessages).toBeDefined();
|
|
323
|
+
|
|
324
|
+
// Should have used the calculator tool
|
|
325
|
+
const hasToolUse = runMessages?.some(
|
|
326
|
+
(msg) =>
|
|
327
|
+
msg._getType() === 'ai' &&
|
|
328
|
+
((msg as AIMessage).tool_calls?.length ?? 0) > 0
|
|
329
|
+
);
|
|
330
|
+
expect(hasToolUse).toBe(true);
|
|
331
|
+
|
|
332
|
+
console.log(
|
|
333
|
+
`${capitalizeFirstLetter(provider)} tool call with caching test passed`
|
|
334
|
+
);
|
|
335
|
+
});
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
describe('Cross-provider message isolation', () => {
|
|
339
|
+
test('Messages processed by Anthropic should not affect Bedrock processing', async () => {
|
|
340
|
+
const anthropicConfig = getLLMConfig(Providers.ANTHROPIC);
|
|
341
|
+
const bedrockConfig = getLLMConfig(Providers.BEDROCK);
|
|
342
|
+
const { customHandlers: handlers1 } = setupTest();
|
|
343
|
+
const { customHandlers: handlers2 } = setupTest();
|
|
344
|
+
|
|
345
|
+
// Create a shared message array
|
|
346
|
+
const sharedMessages: BaseMessage[] = [
|
|
347
|
+
new HumanMessage('Hello, what is the capital of France?'),
|
|
348
|
+
];
|
|
349
|
+
const originalContent = JSON.stringify(sharedMessages[0].content);
|
|
350
|
+
|
|
351
|
+
// Process with Anthropic first
|
|
352
|
+
const anthropicRun = await Run.create<t.IState>({
|
|
353
|
+
runId: 'cross-provider-anthropic',
|
|
354
|
+
graphConfig: {
|
|
355
|
+
type: 'standard',
|
|
356
|
+
llmConfig: { ...anthropicConfig, promptCache: true } as t.LLMConfig,
|
|
357
|
+
instructions: 'You are a helpful assistant.',
|
|
358
|
+
},
|
|
359
|
+
returnContent: true,
|
|
360
|
+
customHandlers: handlers1,
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
const anthropicResult = await anthropicRun.processStream(
|
|
364
|
+
{ messages: sharedMessages },
|
|
365
|
+
streamConfig
|
|
366
|
+
);
|
|
367
|
+
expect(anthropicResult).toBeDefined();
|
|
368
|
+
|
|
369
|
+
// Verify message not mutated
|
|
370
|
+
expect(JSON.stringify(sharedMessages[0].content)).toBe(originalContent);
|
|
371
|
+
|
|
372
|
+
// Now process with Bedrock using the SAME messages
|
|
373
|
+
const bedrockRun = await Run.create<t.IState>({
|
|
374
|
+
runId: 'cross-provider-bedrock',
|
|
375
|
+
graphConfig: {
|
|
376
|
+
type: 'standard',
|
|
377
|
+
llmConfig: { ...bedrockConfig, promptCache: true } as t.LLMConfig,
|
|
378
|
+
instructions: 'You are a helpful assistant.',
|
|
379
|
+
},
|
|
380
|
+
returnContent: true,
|
|
381
|
+
customHandlers: handlers2,
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
const bedrockResult = await bedrockRun.processStream(
|
|
385
|
+
{ messages: sharedMessages },
|
|
386
|
+
streamConfig
|
|
387
|
+
);
|
|
388
|
+
expect(bedrockResult).toBeDefined();
|
|
389
|
+
|
|
390
|
+
// Verify message STILL not mutated after both providers processed
|
|
391
|
+
expect(JSON.stringify(sharedMessages[0].content)).toBe(originalContent);
|
|
392
|
+
|
|
393
|
+
console.log('Cross-provider message isolation test passed');
|
|
394
|
+
});
|
|
395
|
+
});
|
|
396
|
+
});
|