@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.
@@ -1,5 +1,5 @@
1
1
  // src/agents/__tests__/AgentContext.test.ts
2
- import { HumanMessage } from '@langchain/core/messages';
2
+ import { AIMessage, HumanMessage, ToolMessage } from '@langchain/core/messages';
3
3
  import { AgentContext } from '../AgentContext';
4
4
  import { Providers } from '@/common';
5
5
  import { addBedrockCacheControl } from '@/messages/cache';
@@ -79,7 +79,7 @@ describe('AgentContext', () => {
79
79
  );
80
80
  });
81
81
 
82
- it('marks only stable system text for Anthropic prompt caching', async () => {
82
+ it('moves Anthropic dynamic instructions behind stable history', async () => {
83
83
  const ctx = createBasicContext({
84
84
  agentConfig: {
85
85
  provider: Providers.ANTHROPIC,
@@ -89,18 +89,39 @@ describe('AgentContext', () => {
89
89
  },
90
90
  });
91
91
 
92
- const result = await ctx.systemRunnable!.invoke([]);
92
+ const result = await ctx.systemRunnable!.invoke([
93
+ new HumanMessage('Hello'),
94
+ new HumanMessage('Second'),
95
+ ]);
93
96
  const content = result[0].content as TestSystemContentBlock[];
94
- expect(content).toHaveLength(2);
95
- expect(content[0]).toMatchObject({
96
- type: 'text',
97
- text: 'Stable instructions',
98
- cache_control: { type: 'ephemeral' },
99
- });
100
- expect(content[1]).toEqual({
101
- type: 'text',
102
- text: 'Dynamic instructions',
97
+ expect(content).toEqual([
98
+ {
99
+ type: 'text',
100
+ text: 'Stable instructions',
101
+ cache_control: { type: 'ephemeral' },
102
+ },
103
+ ]);
104
+ expect(result[1].content).toBe('Hello');
105
+ expect(result[2].content).toBe('Dynamic instructions');
106
+ expect(result[3].content).toBe('Second');
107
+ });
108
+
109
+ it('places Anthropic dynamic instructions before a single latest user prompt', async () => {
110
+ const ctx = createBasicContext({
111
+ agentConfig: {
112
+ provider: Providers.ANTHROPIC,
113
+ clientOptions: { model: 'claude-3-5-sonnet', promptCache: true },
114
+ instructions: 'Stable instructions',
115
+ additional_instructions: 'Dynamic instructions',
116
+ },
103
117
  });
118
+
119
+ const result = await ctx.systemRunnable!.invoke([
120
+ new HumanMessage('Latest'),
121
+ ]);
122
+
123
+ expect(result[1].content).toBe('Dynamic instructions');
124
+ expect(result[2].content).toBe('Latest');
104
125
  });
105
126
 
106
127
  it('omits Anthropic cache control when only dynamic system text exists', async () => {
@@ -119,7 +140,7 @@ describe('AgentContext', () => {
119
140
  expect(content[0]).not.toHaveProperty('cache_control');
120
141
  });
121
142
 
122
- it('keeps cross-run summaries in the dynamic Anthropic system tail', async () => {
143
+ it('keeps cross-run summaries in the dynamic Anthropic tail', async () => {
123
144
  const ctx = createBasicContext({
124
145
  agentConfig: {
125
146
  provider: Providers.ANTHROPIC,
@@ -131,12 +152,11 @@ describe('AgentContext', () => {
131
152
 
132
153
  const result = await ctx.systemRunnable!.invoke([]);
133
154
  const content = result[0].content as TestSystemContentBlock[];
134
- expect(content).toHaveLength(2);
155
+ expect(content).toHaveLength(1);
135
156
  expect(content[0]).toHaveProperty('cache_control');
136
- expect(content[1]).toEqual({
137
- type: 'text',
138
- text: '## Conversation Summary\n\nPrior summary',
139
- });
157
+ expect(result[1].content).toBe(
158
+ '## Conversation Summary\n\nPrior summary'
159
+ );
140
160
  });
141
161
 
142
162
  it('places the Bedrock cache point before dynamic system text', async () => {
@@ -198,7 +218,7 @@ describe('AgentContext', () => {
198
218
  );
199
219
  });
200
220
 
201
- it('marks stable OpenRouter system text and keeps first user message stable', async () => {
221
+ it('moves OpenRouter dynamic instructions behind stable history', async () => {
202
222
  const ctx = createBasicContext({
203
223
  agentConfig: {
204
224
  provider: Providers.OPENROUTER,
@@ -223,7 +243,6 @@ describe('AgentContext', () => {
223
243
  cache_control: { type: 'ephemeral' },
224
244
  },
225
245
  ]);
226
- expect(result[1]).toBeInstanceOf(HumanMessage);
227
246
  expect(result[1].content).toBe('Hello');
228
247
  expect(result[2].content).toBe('Dynamic instructions');
229
248
  expect(result[3].content).toBe('Second');
@@ -298,7 +317,7 @@ describe('AgentContext', () => {
298
317
  expect(result[3].content).toBe('Second');
299
318
  });
300
319
 
301
- it('adds OpenRouter body cache points when there is no dynamic tail', async () => {
320
+ it('keeps the first OpenRouter user message before single-turn dynamic instructions', async () => {
302
321
  const ctx = createBasicContext({
303
322
  agentConfig: {
304
323
  provider: Providers.OPENROUTER,
@@ -307,6 +326,126 @@ describe('AgentContext', () => {
307
326
  promptCache: true,
308
327
  },
309
328
  instructions: 'Stable instructions',
329
+ additional_instructions: 'Dynamic instructions',
330
+ },
331
+ });
332
+
333
+ const result = await ctx.systemRunnable!.invoke([
334
+ new HumanMessage('Latest'),
335
+ ]);
336
+
337
+ expect(result[1].content).toBe('Latest');
338
+ expect(result[2].content).toBe('Dynamic instructions');
339
+ });
340
+
341
+ it('caches stable Anthropic history before dynamic instructions', async () => {
342
+ const ctx = createBasicContext({
343
+ agentConfig: {
344
+ provider: Providers.ANTHROPIC,
345
+ clientOptions: {
346
+ model: 'claude-3-5-sonnet',
347
+ promptCache: true,
348
+ },
349
+ instructions: 'Stable instructions',
350
+ additional_instructions: 'Dynamic instructions',
351
+ },
352
+ });
353
+
354
+ const result = await ctx.systemRunnable!.invoke([
355
+ new HumanMessage('First'),
356
+ new AIMessage('Stable assistant history'),
357
+ new HumanMessage('Latest'),
358
+ ]);
359
+ const stableHistory = result[2].content as TestSystemContentBlock[];
360
+
361
+ expect(result[1].content).toBe('First');
362
+ expect(stableHistory[0]).toMatchObject({
363
+ type: 'text',
364
+ text: 'Stable assistant history',
365
+ cache_control: { type: 'ephemeral' },
366
+ });
367
+ expect(result[3].content).toBe('Dynamic instructions');
368
+ expect(result[4].content).toBe('Latest');
369
+ });
370
+
371
+ it('does not place Anthropic dynamic instructions between tool calls and results', async () => {
372
+ const ctx = createBasicContext({
373
+ agentConfig: {
374
+ provider: Providers.ANTHROPIC,
375
+ clientOptions: {
376
+ model: 'claude-3-5-sonnet',
377
+ promptCache: true,
378
+ },
379
+ instructions: 'Stable instructions',
380
+ additional_instructions: 'Dynamic instructions',
381
+ },
382
+ });
383
+
384
+ const result = await ctx.systemRunnable!.invoke([
385
+ new HumanMessage('Use the tool'),
386
+ new AIMessage({
387
+ content: '',
388
+ tool_calls: [
389
+ {
390
+ id: 'call_1',
391
+ name: 'calculator',
392
+ args: { expression: '2+2' },
393
+ type: 'tool_call',
394
+ },
395
+ ],
396
+ }),
397
+ new ToolMessage({
398
+ content: '4',
399
+ name: 'calculator',
400
+ tool_call_id: 'call_1',
401
+ }),
402
+ ]);
403
+
404
+ expect(result[1].content).toBe('Use the tool');
405
+ expect((result[2] as AIMessage).tool_calls?.[0]?.id).toBe('call_1');
406
+ expect(result[3].getType()).toBe('tool');
407
+ expect(result[4].content).toBe('Dynamic instructions');
408
+ });
409
+
410
+ it('caches stable OpenRouter history before dynamic instructions', async () => {
411
+ const ctx = createBasicContext({
412
+ agentConfig: {
413
+ provider: Providers.OPENROUTER,
414
+ clientOptions: {
415
+ model: 'anthropic/claude-haiku-4.5',
416
+ promptCache: true,
417
+ },
418
+ instructions: 'Stable instructions',
419
+ additional_instructions: 'Dynamic instructions',
420
+ },
421
+ });
422
+
423
+ const result = await ctx.systemRunnable!.invoke([
424
+ new HumanMessage('First'),
425
+ new AIMessage('Stable assistant history'),
426
+ new HumanMessage('Latest'),
427
+ ]);
428
+ const stableHistory = result[2].content as TestSystemContentBlock[];
429
+
430
+ expect(result[1].content).toBe('First');
431
+ expect(stableHistory[0]).toMatchObject({
432
+ type: 'text',
433
+ text: 'Stable assistant history',
434
+ cache_control: { type: 'ephemeral' },
435
+ });
436
+ expect(result[3].content).toBe('Dynamic instructions');
437
+ expect(result[4].content).toBe('Latest');
438
+ });
439
+
440
+ it('adds OpenRouter body cache points when there is no dynamic tail', async () => {
441
+ const ctx = createBasicContext({
442
+ agentConfig: {
443
+ provider: Providers.OPENROUTER,
444
+ clientOptions: {
445
+ model: 'google/gemini-3.1-pro-preview',
446
+ promptCache: true,
447
+ },
448
+ instructions: 'Stable instructions',
310
449
  },
311
450
  });
312
451
 
@@ -325,7 +464,7 @@ describe('AgentContext', () => {
325
464
  agentConfig: {
326
465
  provider: Providers.OPENROUTER,
327
466
  clientOptions: {
328
- model: 'anthropic/claude-haiku-4.5',
467
+ model: 'google/gemini-3.1-pro-preview',
329
468
  promptCache: true,
330
469
  },
331
470
  instructions: 'Stable instructions',
@@ -707,7 +846,7 @@ describe('AgentContext', () => {
707
846
  agentConfig: {
708
847
  provider: Providers.OPENROUTER,
709
848
  clientOptions: {
710
- model: 'anthropic/claude-haiku-4.5',
849
+ model: 'google/gemini-3.1-pro-preview',
711
850
  promptCache: true,
712
851
  },
713
852
  instructions: 'Stable',
@@ -733,7 +872,7 @@ describe('AgentContext', () => {
733
872
  agentConfig: {
734
873
  provider: Providers.OPENROUTER,
735
874
  clientOptions: {
736
- model: 'anthropic/claude-haiku-4.5',
875
+ model: 'google/gemini-3.1-pro-preview',
737
876
  promptCache: true,
738
877
  },
739
878
  instructions: 'Stable instructions',
@@ -1,13 +1,18 @@
1
1
  import { expect } from '@jest/globals';
2
2
  import { HumanMessage } from '@langchain/core/messages';
3
3
  import type { UsageMetadata } from '@langchain/core/messages';
4
+ import type { ClientOptions } from '@langchain/openai';
4
5
  import type * as t from '@/types';
5
6
  import { GraphEvents, Providers } from '@/common';
6
7
  import { AgentContext } from '../AgentContext';
7
8
  import { ModelEndHandler } from '@/events';
8
9
  import { Run } from '@/run';
10
+ import type { ChatOpenRouterInput } from '@/llm/openrouter';
9
11
 
10
- type LivePromptCacheProvider = Providers.ANTHROPIC | Providers.BEDROCK;
12
+ type LivePromptCacheProvider =
13
+ | Providers.ANTHROPIC
14
+ | Providers.BEDROCK
15
+ | Providers.OPENROUTER;
11
16
 
12
17
  type PromptCacheExpectedSystemBlock =
13
18
  | { type: 'text'; text: string; cache_control?: { type: 'ephemeral' } }
@@ -15,7 +20,8 @@ type PromptCacheExpectedSystemBlock =
15
20
 
16
21
  type LivePromptCacheClientOptions =
17
22
  | t.ClientOptions
18
- | t.BedrockAnthropicClientOptions;
23
+ | t.BedrockAnthropicClientOptions
24
+ | (ChatOpenRouterInput & { configuration?: ClientOptions });
19
25
 
20
26
  export function buildStableInstructions({
21
27
  nonce,
@@ -240,6 +240,149 @@ function isCachePoint(block: MessageContentComplex): boolean {
240
240
  return 'cachePoint' in block && !('type' in block);
241
241
  }
242
242
 
243
+ function getMessageRole(message: MessageWithContent): string | undefined {
244
+ if (message instanceof BaseMessage) {
245
+ return message.getType();
246
+ }
247
+ if ('role' in message && typeof message.role === 'string') {
248
+ return message.role;
249
+ }
250
+ return undefined;
251
+ }
252
+
253
+ function isCacheableConversationMessage(message: MessageWithContent): boolean {
254
+ const role = getMessageRole(message);
255
+ return (
256
+ role === 'human' || role === 'user' || role === 'ai' || role === 'assistant'
257
+ );
258
+ }
259
+
260
+ function isAssistantConversationMessage(message: MessageWithContent): boolean {
261
+ const role = getMessageRole(message);
262
+ return role === 'ai' || role === 'assistant';
263
+ }
264
+
265
+ function hasCacheMarker(message: MessageWithContent): boolean {
266
+ return (
267
+ Array.isArray(message.content) &&
268
+ message.content.some((block) => 'cache_control' in block)
269
+ );
270
+ }
271
+
272
+ function addCacheControlToRecentMessages<
273
+ T extends AnthropicMessage | BaseMessage,
274
+ >(
275
+ messages: T[],
276
+ maxCachePoints: number,
277
+ canUseMessage: (message: MessageWithContent) => boolean
278
+ ): T[] {
279
+ if (
280
+ !Array.isArray(messages) ||
281
+ messages.length === 0 ||
282
+ maxCachePoints <= 0
283
+ ) {
284
+ return messages;
285
+ }
286
+
287
+ const updatedMessages: T[] = [...messages];
288
+ let cachePointsAdded = 0;
289
+
290
+ for (let i = updatedMessages.length - 1; i >= 0; i--) {
291
+ const originalMessage = updatedMessages[i];
292
+ const content = originalMessage.content;
293
+ const hasArrayContent = Array.isArray(content);
294
+ const canAddCache =
295
+ cachePointsAdded < maxCachePoints && canUseMessage(originalMessage);
296
+
297
+ if (!canAddCache && !hasArrayContent) {
298
+ continue;
299
+ }
300
+
301
+ let workingContent: MessageContentComplex[];
302
+ let modified = false;
303
+
304
+ if (hasArrayContent) {
305
+ const src = content as MessageContentComplex[];
306
+ workingContent = [];
307
+ let lastNonEmptyTextIndex = -1;
308
+
309
+ for (let j = 0; j < src.length; j++) {
310
+ const block = src[j];
311
+ if (isCachePoint(block)) {
312
+ modified = true;
313
+ continue;
314
+ }
315
+
316
+ const cloned = { ...block };
317
+ if ('cache_control' in cloned) {
318
+ delete (cloned as Record<string, unknown>).cache_control;
319
+ modified = true;
320
+ }
321
+
322
+ if ('type' in cloned && cloned.type === 'text') {
323
+ const text = (cloned as { text?: string }).text;
324
+ if (text != null && text.trim() !== '') {
325
+ lastNonEmptyTextIndex = workingContent.length;
326
+ }
327
+ }
328
+ workingContent.push(cloned as MessageContentComplex);
329
+ }
330
+
331
+ if (canAddCache && lastNonEmptyTextIndex >= 0) {
332
+ (
333
+ workingContent[lastNonEmptyTextIndex] as Anthropic.TextBlockParam
334
+ ).cache_control = {
335
+ type: 'ephemeral',
336
+ };
337
+ cachePointsAdded++;
338
+ modified = true;
339
+ }
340
+
341
+ if (!modified) {
342
+ continue;
343
+ }
344
+ } else if (
345
+ typeof content === 'string' &&
346
+ content.trim() !== '' &&
347
+ canAddCache
348
+ ) {
349
+ workingContent = [
350
+ { type: 'text', text: content, cache_control: { type: 'ephemeral' } },
351
+ ] as unknown as MessageContentComplex[];
352
+ cachePointsAdded++;
353
+ } else {
354
+ continue;
355
+ }
356
+
357
+ updatedMessages[i] = cloneMessage(
358
+ originalMessage as MessageWithContent,
359
+ workingContent
360
+ ) as T;
361
+ }
362
+
363
+ return updatedMessages;
364
+ }
365
+
366
+ export function addCacheControlToStablePrefixMessages<
367
+ T extends AnthropicMessage | BaseMessage,
368
+ >(messages: T[], maxCachePoints: number): T[] {
369
+ const assistantMarked = addCacheControlToRecentMessages(
370
+ messages,
371
+ maxCachePoints,
372
+ isAssistantConversationMessage
373
+ );
374
+
375
+ if (assistantMarked.some(hasCacheMarker)) {
376
+ return assistantMarked;
377
+ }
378
+
379
+ return addCacheControlToRecentMessages(
380
+ messages,
381
+ maxCachePoints,
382
+ isCacheableConversationMessage
383
+ );
384
+ }
385
+
243
386
  /**
244
387
  * Checks if a message's content has Anthropic cache_control fields.
245
388
  */