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