@librechat/agents 3.1.82 → 3.1.83
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/agents/AgentContext.cjs +43 -21
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/main.cjs +1 -0
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/messages/cache.cjs +96 -0
- package/dist/cjs/messages/cache.cjs.map +1 -1
- package/dist/esm/agents/AgentContext.mjs +44 -22
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/main.mjs +1 -1
- package/dist/esm/messages/cache.mjs +96 -1
- package/dist/esm/messages/cache.mjs.map +1 -1
- package/dist/types/agents/AgentContext.d.ts +4 -2
- package/dist/types/agents/__tests__/promptCacheLiveHelpers.d.ts +6 -2
- package/dist/types/messages/cache.d.ts +1 -0
- package/package.json +1 -1
- package/src/agents/AgentContext.ts +70 -27
- package/src/agents/__tests__/AgentContext.anthropic.live.test.ts +0 -4
- package/src/agents/__tests__/AgentContext.openrouter.live.test.ts +128 -0
- package/src/agents/__tests__/AgentContext.test.ts +163 -24
- package/src/agents/__tests__/promptCacheLiveHelpers.ts +8 -2
- package/src/messages/cache.ts +143 -0
package/dist/esm/main.mjs
CHANGED
|
@@ -6,7 +6,7 @@ export { convertMessagesToContent, findLastIndex, formatAnthropicArtifactContent
|
|
|
6
6
|
export { getMessageId } from './messages/ids.mjs';
|
|
7
7
|
export { DEFAULT_RESERVE_RATIO, ORIGINAL_CONTENT_MAX_CHARS, calculateTotalTokens, checkValidNumber, createPruneMessages, enforceOriginalContentCap, getMessagesWithinTokenLimit, maskConsumedToolResults, preFlightTruncateToolCallInputs, preFlightTruncateToolResults, repairOrphanedToolMessages, sanitizeOrphanToolBlocks } from './messages/prune.mjs';
|
|
8
8
|
export { ensureThinkingBlockInMessages, formatAgentMessages, formatFromLangChain, formatLangChainMessages, formatMediaMessage, formatMessage, labelContentByAgent, shiftIndexTokenCountMap } from './messages/format.mjs';
|
|
9
|
-
export { addBedrockCacheControl, addCacheControl, stripAnthropicCacheControl, stripBedrockCacheControl } from './messages/cache.mjs';
|
|
9
|
+
export { addBedrockCacheControl, addCacheControl, addCacheControlToStablePrefixMessages, stripAnthropicCacheControl, stripBedrockCacheControl } from './messages/cache.mjs';
|
|
10
10
|
export { makeIsDeferred, partitionAndMarkAnthropicToolCache } from './messages/anthropicToolCache.mjs';
|
|
11
11
|
export { formatContentStrings } from './messages/content.mjs';
|
|
12
12
|
export { extractToolDiscoveries, hasToolSearchInCurrentTurn } from './messages/tools.mjs';
|
|
@@ -170,6 +170,101 @@ function addCacheControl(messages) {
|
|
|
170
170
|
function isCachePoint(block) {
|
|
171
171
|
return 'cachePoint' in block && !('type' in block);
|
|
172
172
|
}
|
|
173
|
+
function getMessageRole(message) {
|
|
174
|
+
if (message instanceof BaseMessage) {
|
|
175
|
+
return message.getType();
|
|
176
|
+
}
|
|
177
|
+
if ('role' in message && typeof message.role === 'string') {
|
|
178
|
+
return message.role;
|
|
179
|
+
}
|
|
180
|
+
return undefined;
|
|
181
|
+
}
|
|
182
|
+
function isCacheableConversationMessage(message) {
|
|
183
|
+
const role = getMessageRole(message);
|
|
184
|
+
return (role === 'human' || role === 'user' || role === 'ai' || role === 'assistant');
|
|
185
|
+
}
|
|
186
|
+
function isAssistantConversationMessage(message) {
|
|
187
|
+
const role = getMessageRole(message);
|
|
188
|
+
return role === 'ai' || role === 'assistant';
|
|
189
|
+
}
|
|
190
|
+
function hasCacheMarker(message) {
|
|
191
|
+
return (Array.isArray(message.content) &&
|
|
192
|
+
message.content.some((block) => 'cache_control' in block));
|
|
193
|
+
}
|
|
194
|
+
function addCacheControlToRecentMessages(messages, maxCachePoints, canUseMessage) {
|
|
195
|
+
if (!Array.isArray(messages) ||
|
|
196
|
+
messages.length === 0 ||
|
|
197
|
+
maxCachePoints <= 0) {
|
|
198
|
+
return messages;
|
|
199
|
+
}
|
|
200
|
+
const updatedMessages = [...messages];
|
|
201
|
+
let cachePointsAdded = 0;
|
|
202
|
+
for (let i = updatedMessages.length - 1; i >= 0; i--) {
|
|
203
|
+
const originalMessage = updatedMessages[i];
|
|
204
|
+
const content = originalMessage.content;
|
|
205
|
+
const hasArrayContent = Array.isArray(content);
|
|
206
|
+
const canAddCache = cachePointsAdded < maxCachePoints && canUseMessage(originalMessage);
|
|
207
|
+
if (!canAddCache && !hasArrayContent) {
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
let workingContent;
|
|
211
|
+
let modified = false;
|
|
212
|
+
if (hasArrayContent) {
|
|
213
|
+
const src = content;
|
|
214
|
+
workingContent = [];
|
|
215
|
+
let lastNonEmptyTextIndex = -1;
|
|
216
|
+
for (let j = 0; j < src.length; j++) {
|
|
217
|
+
const block = src[j];
|
|
218
|
+
if (isCachePoint(block)) {
|
|
219
|
+
modified = true;
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
const cloned = { ...block };
|
|
223
|
+
if ('cache_control' in cloned) {
|
|
224
|
+
delete cloned.cache_control;
|
|
225
|
+
modified = true;
|
|
226
|
+
}
|
|
227
|
+
if ('type' in cloned && cloned.type === 'text') {
|
|
228
|
+
const text = cloned.text;
|
|
229
|
+
if (text != null && text.trim() !== '') {
|
|
230
|
+
lastNonEmptyTextIndex = workingContent.length;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
workingContent.push(cloned);
|
|
234
|
+
}
|
|
235
|
+
if (canAddCache && lastNonEmptyTextIndex >= 0) {
|
|
236
|
+
workingContent[lastNonEmptyTextIndex].cache_control = {
|
|
237
|
+
type: 'ephemeral',
|
|
238
|
+
};
|
|
239
|
+
cachePointsAdded++;
|
|
240
|
+
modified = true;
|
|
241
|
+
}
|
|
242
|
+
if (!modified) {
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
else if (typeof content === 'string' &&
|
|
247
|
+
content.trim() !== '' &&
|
|
248
|
+
canAddCache) {
|
|
249
|
+
workingContent = [
|
|
250
|
+
{ type: 'text', text: content, cache_control: { type: 'ephemeral' } },
|
|
251
|
+
];
|
|
252
|
+
cachePointsAdded++;
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
updatedMessages[i] = cloneMessage(originalMessage, workingContent);
|
|
258
|
+
}
|
|
259
|
+
return updatedMessages;
|
|
260
|
+
}
|
|
261
|
+
function addCacheControlToStablePrefixMessages(messages, maxCachePoints) {
|
|
262
|
+
const assistantMarked = addCacheControlToRecentMessages(messages, maxCachePoints, isAssistantConversationMessage);
|
|
263
|
+
if (assistantMarked.some(hasCacheMarker)) {
|
|
264
|
+
return assistantMarked;
|
|
265
|
+
}
|
|
266
|
+
return addCacheControlToRecentMessages(messages, maxCachePoints, isCacheableConversationMessage);
|
|
267
|
+
}
|
|
173
268
|
/**
|
|
174
269
|
* Checks if a message's content has Anthropic cache_control fields.
|
|
175
270
|
*/
|
|
@@ -335,5 +430,5 @@ function addBedrockCacheControl(messages) {
|
|
|
335
430
|
return updatedMessages;
|
|
336
431
|
}
|
|
337
432
|
|
|
338
|
-
export { addBedrockCacheControl, addCacheControl, stripAnthropicCacheControl, stripBedrockCacheControl };
|
|
433
|
+
export { addBedrockCacheControl, addCacheControl, addCacheControlToStablePrefixMessages, stripAnthropicCacheControl, stripBedrockCacheControl };
|
|
339
434
|
//# sourceMappingURL=cache.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.mjs","sources":["../../../src/messages/cache.ts"],"sourcesContent":["import {\n AIMessage,\n BaseMessage,\n ToolMessage,\n HumanMessage,\n SystemMessage,\n MessageContentComplex,\n} from '@langchain/core/messages';\nimport type { AnthropicMessage } from '@/types/messages';\nimport type Anthropic from '@anthropic-ai/sdk';\nimport { ContentTypes } from '@/common/enum';\nimport { toLangChainContent } from './langchain';\n\ntype MessageWithContent = {\n content?: string | MessageContentComplex[];\n};\n\ntype MessageContentWithCacheControl = MessageContentComplex & {\n cache_control?: unknown;\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 new content. For LangChain BaseMessage instances,\n * constructs a proper class instance so that `instanceof` checks are preserved\n * in downstream code (e.g., ensureThinkingBlockInMessages).\n * For plain objects (AnthropicMessage), uses object spread.\n */\nfunction cloneMessage<T extends MessageWithContent>(\n message: T,\n content: string | MessageContentComplex[]\n): T {\n if (message instanceof BaseMessage) {\n const baseParams = {\n content: toLangChainContent(content),\n additional_kwargs: { ...message.additional_kwargs },\n response_metadata: { ...message.response_metadata },\n id: message.id,\n name: message.name,\n };\n\n const msgType = message.getType();\n switch (msgType) {\n case 'ai':\n return new AIMessage({\n ...baseParams,\n tool_calls: (message as unknown as AIMessage).tool_calls,\n }) as unknown as T;\n case 'human':\n return new HumanMessage(baseParams) as unknown as T;\n case 'system':\n return new SystemMessage(baseParams) as unknown as T;\n case 'tool':\n return new ToolMessage({\n ...baseParams,\n tool_call_id: (message as unknown as ToolMessage).tool_call_id,\n }) as unknown as T;\n default:\n break;\n }\n }\n\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\nfunction stripAnthropicCacheControlFromBlocks(\n content: MessageContentComplex[]\n): { content: MessageContentComplex[]; modified: boolean } {\n let modified = false;\n const strippedContent = content.map((block) => {\n if (!('cache_control' in block)) {\n return block;\n }\n\n const cloned: MessageContentWithCacheControl = { ...block };\n delete cloned.cache_control;\n modified = true;\n return cloned;\n });\n\n return { content: strippedContent, modified };\n}\n\nfunction sanitizeBedrockSystemMessage<T extends MessageWithContent>(\n message: T\n): T {\n const content = message.content;\n if (!Array.isArray(content)) {\n return message;\n }\n\n const stripped = stripAnthropicCacheControlFromBlocks(content);\n if (!stripped.modified) {\n return message;\n }\n\n return cloneMessage(message, stripped.content);\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 const hasArrayContent = Array.isArray(content);\n const needsCacheAdd =\n userMessagesModified < 2 &&\n isUserMessage &&\n (typeof content === 'string' || hasArrayContent);\n\n // Skip messages that don't need any work\n if (!needsCacheAdd && !hasArrayContent) {\n continue;\n }\n\n let workingContent: MessageContentComplex[];\n let modified = false;\n\n if (hasArrayContent) {\n // Single pass: clone blocks, strip cache markers and cache points,\n // find last text block index for cache insertion — all at once.\n const src = content as MessageContentComplex[];\n workingContent = [];\n let lastTextIndex = -1;\n for (let j = 0; j < src.length; j++) {\n const block = src[j];\n if (isCachePoint(block)) {\n modified = true;\n continue; // skip cache point blocks\n }\n const cloned = { ...block };\n if ('cache_control' in cloned) {\n delete (cloned as Record<string, unknown>).cache_control;\n modified = true;\n }\n if ('type' in cloned && cloned.type === 'text') {\n lastTextIndex = workingContent.length;\n }\n workingContent.push(cloned as MessageContentComplex);\n }\n\n if (!modified && !needsCacheAdd) {\n continue; // nothing to strip and no cache to add\n }\n\n // Add cache control to the last text block for user messages\n if (needsCacheAdd && lastTextIndex >= 0) {\n (\n workingContent[lastTextIndex] as Anthropic.TextBlockParam\n ).cache_control = {\n type: 'ephemeral',\n };\n userMessagesModified++;\n }\n } else if (typeof content === 'string' && needsCacheAdd) {\n workingContent = [\n { type: 'text', text: content, cache_control: { type: 'ephemeral' } },\n ] as unknown as MessageContentComplex[];\n userMessagesModified++;\n } else {\n continue;\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 MessageWithContent & { getType?: () => string; role?: string },\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 messageType =\n 'getType' in originalMessage &&\n typeof originalMessage.getType === 'function'\n ? originalMessage.getType()\n : undefined;\n const messageRole =\n 'role' in originalMessage && typeof originalMessage.role === 'string'\n ? originalMessage.role\n : undefined;\n\n const isSystemMessage =\n messageType === 'system' || messageRole === 'system';\n if (isSystemMessage) {\n updatedMessages[i] = sanitizeBedrockSystemMessage(originalMessage);\n continue;\n }\n\n const isToolMessage = messageType === 'tool' || messageRole === 'tool';\n const content = originalMessage.content;\n const hasArrayContent = Array.isArray(content);\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 (!needsCacheAdd && !hasArrayContent) {\n continue;\n }\n\n let workingContent: MessageContentComplex[];\n let modified = false;\n\n if (hasArrayContent) {\n // Single pass: clone blocks, strip cache markers, find last\n // non-empty text block for cache point insertion — all at once.\n const src = content as MessageContentComplex[];\n workingContent = [];\n let lastNonEmptyTextIndex = -1;\n for (let j = 0; j < src.length; j++) {\n const block = src[j];\n if (isCachePoint(block)) {\n modified = true;\n continue;\n }\n const cloned = { ...block };\n if ('cache_control' in cloned) {\n delete (cloned as Record<string, unknown>).cache_control;\n modified = true;\n }\n const type = (cloned as { type?: string }).type;\n if (type === ContentTypes.TEXT || type === 'text') {\n const text = (cloned as { text?: string }).text;\n if (text != null && text.trim() !== '') {\n lastNonEmptyTextIndex = workingContent.length;\n }\n }\n workingContent.push(cloned as MessageContentComplex);\n }\n\n if (!modified && !needsCacheAdd) {\n continue;\n }\n\n // Insert cache point after the last non-empty text block.\n // Skip if no cacheable text content exists (whitespace-only messages).\n if (needsCacheAdd && lastNonEmptyTextIndex >= 0) {\n workingContent.splice(lastNonEmptyTextIndex + 1, 0, {\n cachePoint: { type: 'default' },\n } as MessageContentComplex);\n messagesModified++;\n }\n } else if (typeof content === 'string' && needsCacheAdd) {\n workingContent = [\n { type: ContentTypes.TEXT, text: content },\n { cachePoint: { type: 'default' } } as MessageContentComplex,\n ];\n messagesModified++;\n } else {\n continue;\n }\n\n updatedMessages[i] = cloneMessage(originalMessage, workingContent);\n }\n\n return updatedMessages;\n}\n"],"names":[],"mappings":";;;;AAqBA;;AAEG;AACH,SAAS,gBAAgB,CACvB,OAAU,EAAA;AAEV,IAAA,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;AAC/B,QAAA,OAAO,OAAO;IAChB;AACA,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;IACpD;AACA,IAAA,OAAO,OAAO;AAChB;AAEA;;;;;AAKG;AACH,SAAS,YAAY,CACnB,OAAU,EACV,OAAyC,EAAA;AAEzC,IAAA,IAAI,OAAO,YAAY,WAAW,EAAE;AAClC,QAAA,MAAM,UAAU,GAAG;AACjB,YAAA,OAAO,EAAE,kBAAkB,CAAC,OAAO,CAAC;AACpC,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;YACd,IAAI,EAAE,OAAO,CAAC,IAAI;SACnB;AAED,QAAA,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,EAAE;QACjC,QAAQ,OAAO;AACf,YAAA,KAAK,IAAI;gBACP,OAAO,IAAI,SAAS,CAAC;AACnB,oBAAA,GAAG,UAAU;oBACb,UAAU,EAAG,OAAgC,CAAC,UAAU;AACzD,iBAAA,CAAiB;AACpB,YAAA,KAAK,OAAO;AACV,gBAAA,OAAO,IAAI,YAAY,CAAC,UAAU,CAAiB;AACrD,YAAA,KAAK,QAAQ;AACX,gBAAA,OAAO,IAAI,aAAa,CAAC,UAAU,CAAiB;AACtD,YAAA,KAAK,MAAM;gBACT,OAAO,IAAI,WAAW,CA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,aAAa,IAAI,qBAAqB,IAAI,CAAC,EAAE;gBAC/C,cAAc,CAAC,MAAM,CAAC,qBAAqB,GAAG,CAAC,EAAE,CAAC,EAAE;AAClD,oBAAA,UAAU,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;AACP,iBAAA,CAAC;AAC3B,gBAAA,gBAAgB,EAAE;YACpB;QACF;AAAO,aAAA,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,aAAa,EAAE;AACvD,YAAA,cAAc,GAAG;gBACf,EAAE,IAAI,EAAE,YAAY,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE;AAC1C,gBAAA,EAAE,UAAU,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,EAA2B;aAC7D;AACD,YAAA,gBAAgB,EAAE;QACpB;aAAO;YACL;QACF;QAEA,eAAe,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,eAAe,EAAE,cAAc,CAAC;IACpE;AAEA,IAAA,OAAO,eAAe;AACxB;;;;"}
|
|
1
|
+
{"version":3,"file":"cache.mjs","sources":["../../../src/messages/cache.ts"],"sourcesContent":["import {\n AIMessage,\n BaseMessage,\n ToolMessage,\n HumanMessage,\n SystemMessage,\n MessageContentComplex,\n} from '@langchain/core/messages';\nimport type { AnthropicMessage } from '@/types/messages';\nimport type Anthropic from '@anthropic-ai/sdk';\nimport { ContentTypes } from '@/common/enum';\nimport { toLangChainContent } from './langchain';\n\ntype MessageWithContent = {\n content?: string | MessageContentComplex[];\n};\n\ntype MessageContentWithCacheControl = MessageContentComplex & {\n cache_control?: unknown;\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 new content. For LangChain BaseMessage instances,\n * constructs a proper class instance so that `instanceof` checks are preserved\n * in downstream code (e.g., ensureThinkingBlockInMessages).\n * For plain objects (AnthropicMessage), uses object spread.\n */\nfunction cloneMessage<T extends MessageWithContent>(\n message: T,\n content: string | MessageContentComplex[]\n): T {\n if (message instanceof BaseMessage) {\n const baseParams = {\n content: toLangChainContent(content),\n additional_kwargs: { ...message.additional_kwargs },\n response_metadata: { ...message.response_metadata },\n id: message.id,\n name: message.name,\n };\n\n const msgType = message.getType();\n switch (msgType) {\n case 'ai':\n return new AIMessage({\n ...baseParams,\n tool_calls: (message as unknown as AIMessage).tool_calls,\n }) as unknown as T;\n case 'human':\n return new HumanMessage(baseParams) as unknown as T;\n case 'system':\n return new SystemMessage(baseParams) as unknown as T;\n case 'tool':\n return new ToolMessage({\n ...baseParams,\n tool_call_id: (message as unknown as ToolMessage).tool_call_id,\n }) as unknown as T;\n default:\n break;\n }\n }\n\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\nfunction stripAnthropicCacheControlFromBlocks(\n content: MessageContentComplex[]\n): { content: MessageContentComplex[]; modified: boolean } {\n let modified = false;\n const strippedContent = content.map((block) => {\n if (!('cache_control' in block)) {\n return block;\n }\n\n const cloned: MessageContentWithCacheControl = { ...block };\n delete cloned.cache_control;\n modified = true;\n return cloned;\n });\n\n return { content: strippedContent, modified };\n}\n\nfunction sanitizeBedrockSystemMessage<T extends MessageWithContent>(\n message: T\n): T {\n const content = message.content;\n if (!Array.isArray(content)) {\n return message;\n }\n\n const stripped = stripAnthropicCacheControlFromBlocks(content);\n if (!stripped.modified) {\n return message;\n }\n\n return cloneMessage(message, stripped.content);\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 const hasArrayContent = Array.isArray(content);\n const needsCacheAdd =\n userMessagesModified < 2 &&\n isUserMessage &&\n (typeof content === 'string' || hasArrayContent);\n\n // Skip messages that don't need any work\n if (!needsCacheAdd && !hasArrayContent) {\n continue;\n }\n\n let workingContent: MessageContentComplex[];\n let modified = false;\n\n if (hasArrayContent) {\n // Single pass: clone blocks, strip cache markers and cache points,\n // find last text block index for cache insertion — all at once.\n const src = content as MessageContentComplex[];\n workingContent = [];\n let lastTextIndex = -1;\n for (let j = 0; j < src.length; j++) {\n const block = src[j];\n if (isCachePoint(block)) {\n modified = true;\n continue; // skip cache point blocks\n }\n const cloned = { ...block };\n if ('cache_control' in cloned) {\n delete (cloned as Record<string, unknown>).cache_control;\n modified = true;\n }\n if ('type' in cloned && cloned.type === 'text') {\n lastTextIndex = workingContent.length;\n }\n workingContent.push(cloned as MessageContentComplex);\n }\n\n if (!modified && !needsCacheAdd) {\n continue; // nothing to strip and no cache to add\n }\n\n // Add cache control to the last text block for user messages\n if (needsCacheAdd && lastTextIndex >= 0) {\n (\n workingContent[lastTextIndex] as Anthropic.TextBlockParam\n ).cache_control = {\n type: 'ephemeral',\n };\n userMessagesModified++;\n }\n } else if (typeof content === 'string' && needsCacheAdd) {\n workingContent = [\n { type: 'text', text: content, cache_control: { type: 'ephemeral' } },\n ] as unknown as MessageContentComplex[];\n userMessagesModified++;\n } else {\n continue;\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\nfunction getMessageRole(message: MessageWithContent): string | undefined {\n if (message instanceof BaseMessage) {\n return message.getType();\n }\n if ('role' in message && typeof message.role === 'string') {\n return message.role;\n }\n return undefined;\n}\n\nfunction isCacheableConversationMessage(message: MessageWithContent): boolean {\n const role = getMessageRole(message);\n return (\n role === 'human' || role === 'user' || role === 'ai' || role === 'assistant'\n );\n}\n\nfunction isAssistantConversationMessage(message: MessageWithContent): boolean {\n const role = getMessageRole(message);\n return role === 'ai' || role === 'assistant';\n}\n\nfunction hasCacheMarker(message: MessageWithContent): boolean {\n return (\n Array.isArray(message.content) &&\n message.content.some((block) => 'cache_control' in block)\n );\n}\n\nfunction addCacheControlToRecentMessages<\n T extends AnthropicMessage | BaseMessage,\n>(\n messages: T[],\n maxCachePoints: number,\n canUseMessage: (message: MessageWithContent) => boolean\n): T[] {\n if (\n !Array.isArray(messages) ||\n messages.length === 0 ||\n maxCachePoints <= 0\n ) {\n return messages;\n }\n\n const updatedMessages: T[] = [...messages];\n let cachePointsAdded = 0;\n\n for (let i = updatedMessages.length - 1; i >= 0; i--) {\n const originalMessage = updatedMessages[i];\n const content = originalMessage.content;\n const hasArrayContent = Array.isArray(content);\n const canAddCache =\n cachePointsAdded < maxCachePoints && canUseMessage(originalMessage);\n\n if (!canAddCache && !hasArrayContent) {\n continue;\n }\n\n let workingContent: MessageContentComplex[];\n let modified = false;\n\n if (hasArrayContent) {\n const src = content as MessageContentComplex[];\n workingContent = [];\n let lastNonEmptyTextIndex = -1;\n\n for (let j = 0; j < src.length; j++) {\n const block = src[j];\n if (isCachePoint(block)) {\n modified = true;\n continue;\n }\n\n const cloned = { ...block };\n if ('cache_control' in cloned) {\n delete (cloned as Record<string, unknown>).cache_control;\n modified = true;\n }\n\n if ('type' in cloned && cloned.type === 'text') {\n const text = (cloned as { text?: string }).text;\n if (text != null && text.trim() !== '') {\n lastNonEmptyTextIndex = workingContent.length;\n }\n }\n workingContent.push(cloned as MessageContentComplex);\n }\n\n if (canAddCache && lastNonEmptyTextIndex >= 0) {\n (\n workingContent[lastNonEmptyTextIndex] as Anthropic.TextBlockParam\n ).cache_control = {\n type: 'ephemeral',\n };\n cachePointsAdded++;\n modified = true;\n }\n\n if (!modified) {\n continue;\n }\n } else if (\n typeof content === 'string' &&\n content.trim() !== '' &&\n canAddCache\n ) {\n workingContent = [\n { type: 'text', text: content, cache_control: { type: 'ephemeral' } },\n ] as unknown as MessageContentComplex[];\n cachePointsAdded++;\n } else {\n continue;\n }\n\n updatedMessages[i] = cloneMessage(\n originalMessage as MessageWithContent,\n workingContent\n ) as T;\n }\n\n return updatedMessages;\n}\n\nexport function addCacheControlToStablePrefixMessages<\n T extends AnthropicMessage | BaseMessage,\n>(messages: T[], maxCachePoints: number): T[] {\n const assistantMarked = addCacheControlToRecentMessages(\n messages,\n maxCachePoints,\n isAssistantConversationMessage\n );\n\n if (assistantMarked.some(hasCacheMarker)) {\n return assistantMarked;\n }\n\n return addCacheControlToRecentMessages(\n messages,\n maxCachePoints,\n isCacheableConversationMessage\n );\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 MessageWithContent & { getType?: () => string; role?: string },\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 messageType =\n 'getType' in originalMessage &&\n typeof originalMessage.getType === 'function'\n ? originalMessage.getType()\n : undefined;\n const messageRole =\n 'role' in originalMessage && typeof originalMessage.role === 'string'\n ? originalMessage.role\n : undefined;\n\n const isSystemMessage =\n messageType === 'system' || messageRole === 'system';\n if (isSystemMessage) {\n updatedMessages[i] = sanitizeBedrockSystemMessage(originalMessage);\n continue;\n }\n\n const isToolMessage = messageType === 'tool' || messageRole === 'tool';\n const content = originalMessage.content;\n const hasArrayContent = Array.isArray(content);\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 (!needsCacheAdd && !hasArrayContent) {\n continue;\n }\n\n let workingContent: MessageContentComplex[];\n let modified = false;\n\n if (hasArrayContent) {\n // Single pass: clone blocks, strip cache markers, find last\n // non-empty text block for cache point insertion — all at once.\n const src = content as MessageContentComplex[];\n workingContent = [];\n let lastNonEmptyTextIndex = -1;\n for (let j = 0; j < src.length; j++) {\n const block = src[j];\n if (isCachePoint(block)) {\n modified = true;\n continue;\n }\n const cloned = { ...block };\n if ('cache_control' in cloned) {\n delete (cloned as Record<string, unknown>).cache_control;\n modified = true;\n }\n const type = (cloned as { type?: string }).type;\n if (type === ContentTypes.TEXT || type === 'text') {\n const text = (cloned as { text?: string }).text;\n if (text != null && text.trim() !== '') {\n lastNonEmptyTextIndex = workingContent.length;\n }\n }\n workingContent.push(cloned as MessageContentComplex);\n }\n\n if (!modified && !needsCacheAdd) {\n continue;\n }\n\n // Insert cache point after the last non-empty text block.\n // Skip if no cacheable text content exists (whitespace-only messages).\n if (needsCacheAdd && lastNonEmptyTextIndex >= 0) {\n workingContent.splice(lastNonEmptyTextIndex + 1, 0, {\n cachePoint: { type: 'default' },\n } as MessageContentComplex);\n messagesModified++;\n }\n } else if (typeof content === 'string' && needsCacheAdd) {\n workingContent = [\n { type: ContentTypes.TEXT, text: content },\n { cachePoint: { type: 'default' } } as MessageContentComplex,\n ];\n messagesModified++;\n } else {\n continue;\n }\n\n updatedMessages[i] = cloneMessage(originalMessage, workingContent);\n }\n\n return updatedMessages;\n}\n"],"names":[],"mappings":";;;;AAqBA;;AAEG;AACH,SAAS,gBAAgB,CACvB,OAAU,EAAA;AAEV,IAAA,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;AAC/B,QAAA,OAAO,OAAO;IAChB;AACA,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;IACpD;AACA,IAAA,OAAO,OAAO;AAChB;AAEA;;;;;AAKG;AACH,SAAS,YAAY,CACnB,OAAU,EACV,OAAyC,EAAA;AAEzC,IAAA,IAAI,OAAO,YAAY,WAAW,EAAE;AAClC,QAAA,MAAM,UAAU,GAAG;AACjB,YAAA,OAAO,EAAE,kBAAkB,CAAC,OAAO,CAAC;AACpC,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;YACd,IAAI,EAAE,OAAO,CAAC,IAAI;SACnB;AAED,QAAA,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,EAAE;QACjC,QAAQ,OA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AAA,IAAI,CAAC,QAAQ,IAAI,CAAC,aAAa,EAAE;gBAC/B;YACF;;;AAIA,YAAA,IAAI,aAAa,IAAI,qBAAqB,IAAI,CAAC,EAAE;gBAC/C,cAAc,CAAC,MAAM,CAAC,qBAAqB,GAAG,CAAC,EAAE,CAAC,EAAE;AAClD,oBAAA,UAAU,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;AACP,iBAAA,CAAC;AAC3B,gBAAA,gBAAgB,EAAE;YACpB;QACF;AAAO,aAAA,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,aAAa,EAAE;AACvD,YAAA,cAAc,GAAG;gBACf,EAAE,IAAI,EAAE,YAAY,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE;AAC1C,gBAAA,EAAE,UAAU,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,EAA2B;aAC7D;AACD,YAAA,gBAAgB,EAAE;QACpB;aAAO;YACL;QACF;QAEA,eAAe,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,eAAe,EAAE,cAAc,CAAC;IACpE;AAEA,IAAA,OAAO,eAAe;AACxB;;;;"}
|
|
@@ -227,8 +227,10 @@ export declare class AgentContext {
|
|
|
227
227
|
*/
|
|
228
228
|
private buildSystemRunnable;
|
|
229
229
|
private buildSummaryHumanMessage;
|
|
230
|
-
private
|
|
231
|
-
private
|
|
230
|
+
private buildPromptCacheDynamicTail;
|
|
231
|
+
private buildBodyWithPromptCacheDynamicTail;
|
|
232
|
+
private getPromptCacheDynamicTailIndex;
|
|
233
|
+
private addStablePromptCacheMarkers;
|
|
232
234
|
private getPromptCacheProvider;
|
|
233
235
|
private hasBedrockPromptCache;
|
|
234
236
|
private buildSystemMessage;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { UsageMetadata } from '@langchain/core/messages';
|
|
2
|
+
import type { ClientOptions } from '@langchain/openai';
|
|
2
3
|
import type * as t from '@/types';
|
|
3
4
|
import { Providers } from '@/common';
|
|
4
|
-
type
|
|
5
|
+
import type { ChatOpenRouterInput } from '@/llm/openrouter';
|
|
6
|
+
type LivePromptCacheProvider = Providers.ANTHROPIC | Providers.BEDROCK | Providers.OPENROUTER;
|
|
5
7
|
type PromptCacheExpectedSystemBlock = {
|
|
6
8
|
type: 'text';
|
|
7
9
|
text: string;
|
|
@@ -13,7 +15,9 @@ type PromptCacheExpectedSystemBlock = {
|
|
|
13
15
|
type: 'default';
|
|
14
16
|
};
|
|
15
17
|
};
|
|
16
|
-
type LivePromptCacheClientOptions = t.ClientOptions | t.BedrockAnthropicClientOptions
|
|
18
|
+
type LivePromptCacheClientOptions = t.ClientOptions | t.BedrockAnthropicClientOptions | (ChatOpenRouterInput & {
|
|
19
|
+
configuration?: ClientOptions;
|
|
20
|
+
});
|
|
17
21
|
export declare function buildStableInstructions({ nonce, providerLabel, }: {
|
|
18
22
|
nonce: string;
|
|
19
23
|
providerLabel: string;
|
|
@@ -13,6 +13,7 @@ type MessageWithContent = {
|
|
|
13
13
|
* @returns - A new array of message objects with cache control added.
|
|
14
14
|
*/
|
|
15
15
|
export declare function addCacheControl<T extends AnthropicMessage | BaseMessage>(messages: T[]): T[];
|
|
16
|
+
export declare function addCacheControlToStablePrefixMessages<T extends AnthropicMessage | BaseMessage>(messages: T[], maxCachePoints: number): T[];
|
|
16
17
|
/**
|
|
17
18
|
* Removes all Anthropic cache_control fields from messages
|
|
18
19
|
* Used when switching from Anthropic to Bedrock provider
|
package/package.json
CHANGED
|
@@ -16,7 +16,10 @@ import {
|
|
|
16
16
|
Providers,
|
|
17
17
|
} from '@/common';
|
|
18
18
|
import { createSchemaOnlyTools } from '@/tools/schema';
|
|
19
|
-
import {
|
|
19
|
+
import {
|
|
20
|
+
addCacheControl,
|
|
21
|
+
addCacheControlToStablePrefixMessages,
|
|
22
|
+
} from '@/messages/cache';
|
|
20
23
|
import { DEFAULT_RESERVE_RATIO } from '@/messages';
|
|
21
24
|
import { toJsonSchema } from '@/utils/schema';
|
|
22
25
|
|
|
@@ -584,24 +587,24 @@ export class AgentContext {
|
|
|
584
587
|
}
|
|
585
588
|
|
|
586
589
|
const promptCacheProvider = this.getPromptCacheProvider();
|
|
587
|
-
const
|
|
588
|
-
promptCacheProvider
|
|
590
|
+
const shouldMoveDynamicInstructions =
|
|
591
|
+
promptCacheProvider != null &&
|
|
589
592
|
stableInstructions !== '' &&
|
|
590
593
|
dynamicInstructions !== '';
|
|
591
594
|
const systemMessage = this.buildSystemMessage({
|
|
592
595
|
stableInstructions,
|
|
593
596
|
dynamicInstructions,
|
|
594
597
|
promptCacheProvider,
|
|
598
|
+
shouldMoveDynamicInstructions,
|
|
595
599
|
});
|
|
596
600
|
|
|
597
601
|
if (this.tokenCounter) {
|
|
598
602
|
this.systemMessageTokens = systemMessage
|
|
599
603
|
? this.tokenCounter(systemMessage)
|
|
600
604
|
: 0;
|
|
601
|
-
this.dynamicInstructionTokens =
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
: 0;
|
|
605
|
+
this.dynamicInstructionTokens = shouldMoveDynamicInstructions
|
|
606
|
+
? this.tokenCounter(new HumanMessage(dynamicInstructions))
|
|
607
|
+
: 0;
|
|
605
608
|
}
|
|
606
609
|
|
|
607
610
|
return RunnableLambda.from((messages: BaseMessage[]) => {
|
|
@@ -616,16 +619,20 @@ export class AgentContext {
|
|
|
616
619
|
this.summaryText !== '';
|
|
617
620
|
|
|
618
621
|
const bodyWithSummary =
|
|
619
|
-
hasSummaryBody && promptCacheProvider
|
|
622
|
+
hasSummaryBody && promptCacheProvider == null
|
|
620
623
|
? [this.buildSummaryHumanMessage(promptCacheProvider), ...messages]
|
|
621
624
|
: messages;
|
|
622
|
-
const dynamicTail = this.
|
|
625
|
+
const dynamicTail = this.buildPromptCacheDynamicTail({
|
|
623
626
|
dynamicInstructions,
|
|
624
627
|
hasSummaryBody,
|
|
625
628
|
promptCacheProvider,
|
|
626
|
-
|
|
629
|
+
shouldMoveDynamicInstructions,
|
|
627
630
|
});
|
|
628
|
-
let body = this.
|
|
631
|
+
let body = this.buildBodyWithPromptCacheDynamicTail(
|
|
632
|
+
bodyWithSummary,
|
|
633
|
+
dynamicTail,
|
|
634
|
+
promptCacheProvider
|
|
635
|
+
);
|
|
629
636
|
|
|
630
637
|
if (
|
|
631
638
|
promptCacheProvider != null &&
|
|
@@ -662,22 +669,22 @@ export class AgentContext {
|
|
|
662
669
|
});
|
|
663
670
|
}
|
|
664
671
|
|
|
665
|
-
private
|
|
672
|
+
private buildPromptCacheDynamicTail({
|
|
666
673
|
dynamicInstructions,
|
|
667
674
|
hasSummaryBody,
|
|
668
675
|
promptCacheProvider,
|
|
669
|
-
|
|
676
|
+
shouldMoveDynamicInstructions,
|
|
670
677
|
}: {
|
|
671
678
|
dynamicInstructions: string;
|
|
672
679
|
hasSummaryBody: boolean;
|
|
673
680
|
promptCacheProvider: PromptCacheProvider | undefined;
|
|
674
|
-
|
|
681
|
+
shouldMoveDynamicInstructions: boolean;
|
|
675
682
|
}): BaseMessage[] {
|
|
676
|
-
if (promptCacheProvider
|
|
683
|
+
if (promptCacheProvider == null) {
|
|
677
684
|
return [];
|
|
678
685
|
}
|
|
679
686
|
|
|
680
|
-
const dynamicTail =
|
|
687
|
+
const dynamicTail = shouldMoveDynamicInstructions
|
|
681
688
|
? [new HumanMessage(dynamicInstructions)]
|
|
682
689
|
: [];
|
|
683
690
|
|
|
@@ -685,22 +692,59 @@ export class AgentContext {
|
|
|
685
692
|
return dynamicTail;
|
|
686
693
|
}
|
|
687
694
|
|
|
688
|
-
return [...dynamicTail, this.buildSummaryHumanMessage(
|
|
695
|
+
return [...dynamicTail, this.buildSummaryHumanMessage(undefined)];
|
|
689
696
|
}
|
|
690
697
|
|
|
691
|
-
private
|
|
698
|
+
private buildBodyWithPromptCacheDynamicTail(
|
|
692
699
|
messages: BaseMessage[],
|
|
693
|
-
tail: BaseMessage[]
|
|
700
|
+
tail: BaseMessage[],
|
|
701
|
+
promptCacheProvider: PromptCacheProvider | undefined
|
|
694
702
|
): BaseMessage[] {
|
|
695
703
|
if (tail.length === 0) {
|
|
696
704
|
return messages;
|
|
697
705
|
}
|
|
698
706
|
|
|
699
|
-
|
|
700
|
-
|
|
707
|
+
const tailIndex = this.getPromptCacheDynamicTailIndex(
|
|
708
|
+
messages,
|
|
709
|
+
promptCacheProvider
|
|
710
|
+
);
|
|
711
|
+
const stablePrefix = messages.slice(0, tailIndex);
|
|
712
|
+
const trailingMessages = messages.slice(tailIndex);
|
|
713
|
+
const cacheablePrefix = this.addStablePromptCacheMarkers(stablePrefix);
|
|
714
|
+
|
|
715
|
+
return [...cacheablePrefix, ...tail, ...trailingMessages];
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
private getPromptCacheDynamicTailIndex(
|
|
719
|
+
messages: BaseMessage[],
|
|
720
|
+
promptCacheProvider: PromptCacheProvider | undefined
|
|
721
|
+
): number {
|
|
722
|
+
const lastIndex = messages.length - 1;
|
|
723
|
+
|
|
724
|
+
if (lastIndex < 0) {
|
|
725
|
+
return 0;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
if (promptCacheProvider === Providers.OPENROUTER && messages.length === 1) {
|
|
729
|
+
return messages.length;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
if (messages[lastIndex].getType() === 'human') {
|
|
733
|
+
return lastIndex;
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
return messages.length;
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
private addStablePromptCacheMarkers(messages: BaseMessage[]): BaseMessage[] {
|
|
740
|
+
if (messages.length <= 1) {
|
|
741
|
+
return messages;
|
|
701
742
|
}
|
|
702
743
|
|
|
703
|
-
return [
|
|
744
|
+
return [
|
|
745
|
+
messages[0],
|
|
746
|
+
...addCacheControlToStablePrefixMessages(messages.slice(1), 2),
|
|
747
|
+
];
|
|
704
748
|
}
|
|
705
749
|
|
|
706
750
|
private getPromptCacheProvider(): PromptCacheProvider | undefined {
|
|
@@ -739,10 +783,12 @@ export class AgentContext {
|
|
|
739
783
|
stableInstructions,
|
|
740
784
|
dynamicInstructions,
|
|
741
785
|
promptCacheProvider,
|
|
786
|
+
shouldMoveDynamicInstructions,
|
|
742
787
|
}: {
|
|
743
788
|
stableInstructions: string;
|
|
744
789
|
dynamicInstructions: string;
|
|
745
790
|
promptCacheProvider: PromptCacheProvider | undefined;
|
|
791
|
+
shouldMoveDynamicInstructions: boolean;
|
|
746
792
|
}): SystemMessage | undefined {
|
|
747
793
|
if (!stableInstructions && !dynamicInstructions) {
|
|
748
794
|
return undefined;
|
|
@@ -757,16 +803,13 @@ export class AgentContext {
|
|
|
757
803
|
cache_control: { type: 'ephemeral' },
|
|
758
804
|
});
|
|
759
805
|
}
|
|
760
|
-
if (dynamicInstructions) {
|
|
806
|
+
if (dynamicInstructions && !shouldMoveDynamicInstructions) {
|
|
761
807
|
content.push({ type: 'text', text: dynamicInstructions });
|
|
762
808
|
}
|
|
763
809
|
return new SystemMessage({ content } as BaseMessageFields);
|
|
764
810
|
}
|
|
765
811
|
|
|
766
|
-
if (
|
|
767
|
-
promptCacheProvider === Providers.OPENROUTER &&
|
|
768
|
-
!stableInstructions
|
|
769
|
-
) {
|
|
812
|
+
if (promptCacheProvider === Providers.OPENROUTER && !stableInstructions) {
|
|
770
813
|
return new SystemMessage(dynamicInstructions);
|
|
771
814
|
}
|
|
772
815
|
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
// src/agents/__tests__/AgentContext.openrouter.live.test.ts
|
|
2
|
+
/**
|
|
3
|
+
* Live OpenRouter prompt-cache verification.
|
|
4
|
+
*
|
|
5
|
+
* Run with:
|
|
6
|
+
* RUN_OPENROUTER_PROMPT_CACHE_LIVE_TESTS=1 OPENROUTER_API_KEY=... npm test -- AgentContext.openrouter.live.test.ts --runInBand
|
|
7
|
+
*/
|
|
8
|
+
import { config as dotenvConfig } from 'dotenv';
|
|
9
|
+
dotenvConfig({ path: process.env.DOTENV_CONFIG_PATH ?? '.env' });
|
|
10
|
+
|
|
11
|
+
import { describe, expect, it } from '@jest/globals';
|
|
12
|
+
import type { ClientOptions } from '@langchain/openai';
|
|
13
|
+
import {
|
|
14
|
+
runLiveTurn,
|
|
15
|
+
assertSystemPayloadShape,
|
|
16
|
+
buildDynamicInstructions,
|
|
17
|
+
buildStableInstructions,
|
|
18
|
+
waitForCachePropagation,
|
|
19
|
+
} from './promptCacheLiveHelpers';
|
|
20
|
+
import type { ChatOpenRouterInput } from '@/llm/openrouter';
|
|
21
|
+
import { Providers } from '@/common';
|
|
22
|
+
|
|
23
|
+
const apiKey = process.env.OPENROUTER_API_KEY ?? process.env.OPENROUTER_KEY;
|
|
24
|
+
const shouldRunLive =
|
|
25
|
+
process.env.RUN_OPENROUTER_PROMPT_CACHE_LIVE_TESTS === '1' &&
|
|
26
|
+
apiKey != null &&
|
|
27
|
+
apiKey !== '';
|
|
28
|
+
|
|
29
|
+
const describeIfLive = shouldRunLive ? describe : describe.skip;
|
|
30
|
+
|
|
31
|
+
const model =
|
|
32
|
+
process.env.OPENROUTER_PROMPT_CACHE_MODEL ?? 'anthropic/claude-sonnet-4.6';
|
|
33
|
+
const providerLabel = 'OpenRouter';
|
|
34
|
+
type OpenRouterLiveClientOptions = ChatOpenRouterInput & {
|
|
35
|
+
configuration?: ClientOptions;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
function createClientOptions(): OpenRouterLiveClientOptions {
|
|
39
|
+
if (apiKey == null || apiKey === '') {
|
|
40
|
+
throw new Error('OPENROUTER_API_KEY is required');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const reasoning = model.startsWith('google/gemini-3')
|
|
44
|
+
? { max_tokens: 16 }
|
|
45
|
+
: undefined;
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
model,
|
|
49
|
+
apiKey,
|
|
50
|
+
temperature: 0,
|
|
51
|
+
maxTokens: 256,
|
|
52
|
+
streaming: true,
|
|
53
|
+
streamUsage: true,
|
|
54
|
+
promptCache: true,
|
|
55
|
+
configuration: {
|
|
56
|
+
baseURL:
|
|
57
|
+
process.env.OPENROUTER_BASE_URL ?? 'https://openrouter.ai/api/v1',
|
|
58
|
+
defaultHeaders: {
|
|
59
|
+
'HTTP-Referer': 'https://librechat.ai',
|
|
60
|
+
'X-Title': 'LibreChat OpenRouter Prompt Cache Live Test',
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
...(reasoning != null ? { reasoning } : {}),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
describeIfLive('AgentContext OpenRouter prompt cache live API', () => {
|
|
68
|
+
it('keeps dynamic instructions outside the cached system prefix', async () => {
|
|
69
|
+
const nonce = `agent-openrouter-cache-live-${Date.now()}`;
|
|
70
|
+
const clientOptions = createClientOptions();
|
|
71
|
+
const stableInstructions = buildStableInstructions({
|
|
72
|
+
nonce,
|
|
73
|
+
providerLabel,
|
|
74
|
+
});
|
|
75
|
+
const firstDynamicInstructions = buildDynamicInstructions({
|
|
76
|
+
marker: 'alpha',
|
|
77
|
+
tailDescription:
|
|
78
|
+
'The Dynamic Marker line is runtime context and must remain outside the cached prefix.',
|
|
79
|
+
});
|
|
80
|
+
const secondDynamicInstructions = buildDynamicInstructions({
|
|
81
|
+
marker: 'bravo',
|
|
82
|
+
tailDescription:
|
|
83
|
+
'The Dynamic Marker line is runtime context and must remain outside the cached prefix.',
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
await assertSystemPayloadShape({
|
|
87
|
+
agentId: 'live-openrouter-cache-shape-check',
|
|
88
|
+
provider: Providers.OPENROUTER,
|
|
89
|
+
clientOptions,
|
|
90
|
+
stableInstructions,
|
|
91
|
+
dynamicInstructions: firstDynamicInstructions,
|
|
92
|
+
expectedContent: [
|
|
93
|
+
{
|
|
94
|
+
type: 'text',
|
|
95
|
+
text: stableInstructions,
|
|
96
|
+
cache_control: { type: 'ephemeral' },
|
|
97
|
+
},
|
|
98
|
+
],
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const first = await runLiveTurn({
|
|
102
|
+
provider: Providers.OPENROUTER,
|
|
103
|
+
providerLabel,
|
|
104
|
+
clientOptions,
|
|
105
|
+
runId: `${nonce}-first`,
|
|
106
|
+
threadId: `${nonce}-thread`,
|
|
107
|
+
stableInstructions,
|
|
108
|
+
dynamicInstructions: firstDynamicInstructions,
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
expect(first.text.toLowerCase()).toContain('alpha');
|
|
112
|
+
|
|
113
|
+
await waitForCachePropagation();
|
|
114
|
+
|
|
115
|
+
const second = await runLiveTurn({
|
|
116
|
+
provider: Providers.OPENROUTER,
|
|
117
|
+
providerLabel,
|
|
118
|
+
clientOptions,
|
|
119
|
+
runId: `${nonce}-second`,
|
|
120
|
+
threadId: `${nonce}-thread`,
|
|
121
|
+
stableInstructions,
|
|
122
|
+
dynamicInstructions: secondDynamicInstructions,
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
expect(second.text.toLowerCase()).toContain('bravo');
|
|
126
|
+
expect(second.usage.input_token_details?.cache_read).toBeGreaterThan(0);
|
|
127
|
+
}, 120_000);
|
|
128
|
+
});
|