@librechat/agents 2.2.8 → 2.3.0

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.
@@ -0,0 +1,655 @@
1
+ // src/specs/thinking-prune.test.ts
2
+ import { HumanMessage, AIMessage, SystemMessage, BaseMessage, ToolMessage } from '@langchain/core/messages';
3
+ import type * as t from '@/types';
4
+ import { createPruneMessages } from '@/messages/prune';
5
+
6
+ // Create a simple token counter for testing
7
+ const createTestTokenCounter = (): t.TokenCounter => {
8
+ return (message: BaseMessage): number => {
9
+ // Use type assertion to help TypeScript understand the type
10
+ const content = message.content as string | Array<any> | undefined;
11
+
12
+ // Handle string content
13
+ if (typeof content === 'string') {
14
+ return content.length;
15
+ }
16
+
17
+ // Handle array content
18
+ if (Array.isArray(content)) {
19
+ let totalLength = 0;
20
+
21
+ for (const item of content) {
22
+ if (typeof item === 'string') {
23
+ totalLength += item.length;
24
+ } else if (item && typeof item === 'object') {
25
+ if (item.type === 'thinking' && typeof item.thinking === 'string') {
26
+ totalLength += item.thinking.length;
27
+ } else if ('text' in item && typeof item.text === 'string') {
28
+ totalLength += item.text.length;
29
+ } else if ('input' in item && typeof item.input === 'string') {
30
+ totalLength += item.input.length;
31
+ }
32
+ }
33
+ }
34
+
35
+ return totalLength;
36
+ }
37
+
38
+ // Default case - if content is null, undefined, or any other type
39
+ return 0;
40
+ };
41
+ };
42
+
43
+ describe('Prune Messages with Thinking Mode Tests', () => {
44
+ jest.setTimeout(30000);
45
+
46
+ it('should preserve thinking blocks when pruning with thinking mode enabled', () => {
47
+ // Create a token counter
48
+ const tokenCounter = createTestTokenCounter();
49
+
50
+ // Create messages based on the example provided
51
+ const userMessage = new HumanMessage({
52
+ content: [
53
+ {
54
+ type: "text",
55
+ text: "/home/danny/LibreChat/gistfile1.txt\n\nread it 200 lines at a time\n\nthere are 5000 lines\n\ndo not stop until done",
56
+ },
57
+ ],
58
+ });
59
+
60
+ const assistantMessageWithThinking = new AIMessage({
61
+ content: [
62
+ {
63
+ type: "thinking",
64
+ thinking: "The user is asking me to read a file located at `/home/danny/LibreChat/gistfile1.txt` in chunks of 200 lines at a time, mentioning that the file has 5000 lines total. They want me to continue reading through the entire file without stopping.\n\nI'll need to use the text editor tool to view the file in chunks of 200 lines each. Since the file has 5000 lines, I'll need to view it in 25 chunks (5000 ÷ 200 = 25).\n\nI'll need to make multiple calls to the text editor with the `view` command, specifying different line ranges for each call.\n\nLet me plan out the approach:\n1. Start with lines 1-200\n2. Then 201-400\n3. Then 401-600\n4. And so on until I reach 4801-5000\n\nFor each call, I'll use the `view` command with the specific line range in the `view_range` parameter. I'll continue until I've shown all 5000 lines as requested.",
65
+ },
66
+ {
67
+ type: "text",
68
+ text: "I'll read the file for you in chunks of 200 lines at a time until all 5000 lines are displayed.\n\nLet me start with the first 200 lines:",
69
+ },
70
+ {
71
+ type: "tool_use",
72
+ id: "toolu_01YApWuFsEQCuBFDgYXmcmeZ",
73
+ name: "text_editor_mcp_textEditor",
74
+ input: "{\"command\": \"view\", \"path\": \"/home/danny/LibreChat/gistfile1.txt\", \"description\": \"Viewing lines 1-200 of the file\", \"view_range\": [1,200]}",
75
+ },
76
+ ],
77
+ });
78
+
79
+ const toolResponseMessage1 = new ToolMessage({
80
+ content: [
81
+ {
82
+ type: "text",
83
+ text: "{\"success\":true,\"message\":\"File content (truncated):\"",
84
+ },
85
+ ],
86
+ tool_call_id: "toolu_01YApWuFsEQCuBFDgYXmcmeZ",
87
+ name: "text_editor_mcp_textEditor",
88
+ });
89
+
90
+ const assistantMessage2 = new AIMessage({
91
+ content: [
92
+ {
93
+ type: "text",
94
+ text: "It looks like the file content is clipped in the response. I'll continue with the next chunks:",
95
+ },
96
+ {
97
+ type: "tool_use",
98
+ id: "toolu_01VnyMQ4CvEd6zLDxxtTd6d4",
99
+ name: "text_editor_mcp_textEditor",
100
+ input: "{\"command\": \"view\", \"path\": \"/home/danny/LibreChat/gistfile1.txt\", \"description\": \"Viewing lines 201-400 of the file\", \"view_range\": [201,400]}",
101
+ },
102
+ ],
103
+ });
104
+
105
+ const toolResponseMessage2 = new ToolMessage({
106
+ content: [
107
+ {
108
+ type: "text",
109
+ text: "{\"success\":true,\"message\":\"File content (truncated):\"",
110
+ },
111
+ ],
112
+ tool_call_id: "toolu_01VnyMQ4CvEd6zLDxxtTd6d4",
113
+ name: "text_editor_mcp_textEditor",
114
+ });
115
+
116
+ const assistantMessage3 = new AIMessage({
117
+ content: [
118
+ {
119
+ type: "text",
120
+ text: "Continuing with the next chunk:",
121
+ },
122
+ {
123
+ type: "tool_use",
124
+ id: "toolu_01TZKs4nnBc58BYXKz1Mw4fp",
125
+ name: "text_editor_mcp_textEditor",
126
+ input: "{\"command\": \"view\", \"path\": \"/home/danny/LibreChat/gistfile1.txt\", \"description\": \"Viewing lines 401-600 of the file\", \"view_range\": [401,600]}",
127
+ },
128
+ ],
129
+ });
130
+
131
+ const toolResponseMessage3 = new ToolMessage({
132
+ content: [
133
+ {
134
+ type: "text",
135
+ text: "{\"success\":true,\"message\":\"File content (truncated):\"",
136
+ },
137
+ ],
138
+ tool_call_id: "toolu_01TZKs4nnBc58BYXKz1Mw4fp",
139
+ name: "text_editor_mcp_textEditor",
140
+ });
141
+
142
+ const assistantMessage4 = new AIMessage({
143
+ content: [
144
+ {
145
+ type: "text",
146
+ text: "Let's continue with the next chunk:",
147
+ },
148
+ {
149
+ type: "tool_use",
150
+ id: "toolu_01TZgBacNxjx1QNUpJg9hca5",
151
+ name: "text_editor_mcp_textEditor",
152
+ input: "{\"command\": \"view\", \"path\": \"/home/danny/LibreChat/gistfile1.txt\", \"description\": \"Viewing lines 601-800 of the file\", \"view_range\": [601,800]}",
153
+ },
154
+ ],
155
+ });
156
+
157
+ const toolResponseMessage4 = new ToolMessage({
158
+ content: [
159
+ {
160
+ type: "text",
161
+ text: "{\"success\":true,\"message\":\"File content (truncated):\"",
162
+ },
163
+ ],
164
+ tool_call_id: "toolu_01TZgBacNxjx1QNUpJg9hca5",
165
+ name: "text_editor_mcp_textEditor",
166
+ });
167
+
168
+ const messages = [
169
+ userMessage,
170
+ assistantMessageWithThinking,
171
+ toolResponseMessage1,
172
+ assistantMessage2,
173
+ toolResponseMessage2,
174
+ assistantMessage3,
175
+ toolResponseMessage3,
176
+ assistantMessage4,
177
+ toolResponseMessage4,
178
+ ];
179
+
180
+ // Create indexTokenCountMap based on the example provided
181
+ const indexTokenCountMap = {
182
+ "0": 617, // userMessage
183
+ "1": 52, // assistantMessageWithThinking
184
+ "2": 4995, // toolResponseMessage1
185
+ "3": 307, // assistantMessage2
186
+ "4": 9359, // toolResponseMessage2
187
+ "5": 178, // assistantMessage3
188
+ "6": 5463, // toolResponseMessage3
189
+ "7": 125, // assistantMessage4
190
+ "8": 4264, // toolResponseMessage4
191
+ };
192
+
193
+ // Create pruneMessages function with thinking mode enabled
194
+ const pruneMessages = createPruneMessages({
195
+ maxTokens: 19800,
196
+ startIndex: 0,
197
+ tokenCounter,
198
+ indexTokenCountMap: { ...indexTokenCountMap },
199
+ thinkingEnabled: true,
200
+ });
201
+
202
+ // Prune messages
203
+ const result = pruneMessages({
204
+ messages,
205
+ usageMetadata: {
206
+ input_tokens: 25254,
207
+ output_tokens: 106,
208
+ total_tokens: 25360,
209
+ input_token_details: {
210
+ cache_read: 0,
211
+ cache_creation: 0,
212
+ },
213
+ },
214
+ startOnMessageType: 'human',
215
+ });
216
+
217
+ // Verify that the first assistant message in the pruned context has a thinking block
218
+ expect(result.context.length).toBeGreaterThan(0);
219
+
220
+ // Find the first assistant message in the pruned context
221
+ const firstAssistantIndex = result.context.findIndex(msg => msg.getType() === 'ai');
222
+ expect(firstAssistantIndex).toBe(0);
223
+
224
+ const firstAssistantMsg = result.context[firstAssistantIndex];
225
+ expect(Array.isArray(firstAssistantMsg.content)).toBe(true);
226
+
227
+ // Verify that the first assistant message has a thinking block
228
+ const hasThinkingBlock = (firstAssistantMsg.content as any[]).some(item =>
229
+ item && typeof item === 'object' && item.type === 'thinking');
230
+ expect(hasThinkingBlock).toBe(true);
231
+
232
+ // Verify that the thinking block is from the original assistant message
233
+ const thinkingBlock = (firstAssistantMsg.content as any[]).find(item =>
234
+ item && typeof item === 'object' && item.type === 'thinking');
235
+ expect(thinkingBlock).toBeDefined();
236
+ expect(thinkingBlock.thinking).toContain("The user is asking me to read a file");
237
+ });
238
+
239
+ it('should handle token recalculation when inserting thinking blocks', () => {
240
+ // Create a token counter
241
+ const tokenCounter = createTestTokenCounter();
242
+
243
+ // Create a message with thinking block
244
+ const assistantMessageWithThinking = new AIMessage({
245
+ content: [
246
+ {
247
+ type: "thinking",
248
+ thinking: "This is a thinking block",
249
+ },
250
+ {
251
+ type: "text",
252
+ text: "Response with thinking",
253
+ },
254
+ ],
255
+ });
256
+
257
+ // Create a message without thinking block
258
+ const assistantMessageWithoutThinking = new AIMessage({
259
+ content: [
260
+ {
261
+ type: "text",
262
+ text: "Response without thinking",
263
+ },
264
+ ],
265
+ });
266
+
267
+ const messages = [
268
+ new SystemMessage("System instruction"),
269
+ new HumanMessage("Hello"),
270
+ assistantMessageWithThinking,
271
+ new HumanMessage("Next message"),
272
+ assistantMessageWithoutThinking,
273
+ ];
274
+
275
+ // Calculate token counts for each message
276
+ const indexTokenCountMap: Record<string, number> = {};
277
+ for (let i = 0; i < messages.length; i++) {
278
+ indexTokenCountMap[i] = tokenCounter(messages[i]);
279
+ }
280
+
281
+ // Create pruneMessages function with thinking mode enabled
282
+ const pruneMessages = createPruneMessages({
283
+ maxTokens: 50, // Set a low token limit to force pruning
284
+ startIndex: 0,
285
+ tokenCounter,
286
+ indexTokenCountMap: { ...indexTokenCountMap },
287
+ thinkingEnabled: true,
288
+ });
289
+
290
+ // Prune messages
291
+ const result = pruneMessages({ messages });
292
+
293
+ // Verify that the pruned context has fewer messages than the original
294
+ expect(result.context.length).toBeLessThan(messages.length);
295
+
296
+ // Find the first assistant message in the pruned context
297
+ const firstAssistantIndex = result.context.findIndex(msg => msg.getType() === 'ai');
298
+ expect(firstAssistantIndex).toBeGreaterThan(-1);
299
+
300
+ const firstAssistantMsg = result.context[firstAssistantIndex];
301
+ expect(Array.isArray(firstAssistantMsg.content)).toBe(true);
302
+
303
+ // Verify that the first assistant message has a thinking block
304
+ const hasThinkingBlock = (firstAssistantMsg.content as any[]).some(item =>
305
+ item && typeof item === 'object' && item.type === 'thinking');
306
+ expect(hasThinkingBlock).toBe(true);
307
+ });
308
+
309
+ it('should not modify messages when under token limit', () => {
310
+ // Create a token counter
311
+ const tokenCounter = createTestTokenCounter();
312
+
313
+ // Create a message with thinking block
314
+ const assistantMessageWithThinking = new AIMessage({
315
+ content: [
316
+ {
317
+ type: "thinking",
318
+ thinking: "This is a thinking block",
319
+ },
320
+ {
321
+ type: "text",
322
+ text: "Response with thinking",
323
+ },
324
+ ],
325
+ });
326
+
327
+ const messages = [
328
+ new SystemMessage("System instruction"),
329
+ new HumanMessage("Hello"),
330
+ assistantMessageWithThinking,
331
+ ];
332
+
333
+ // Calculate token counts for each message
334
+ const indexTokenCountMap: Record<string, number> = {};
335
+ for (let i = 0; i < messages.length; i++) {
336
+ indexTokenCountMap[i] = tokenCounter(messages[i]);
337
+ }
338
+
339
+ // Create pruneMessages function with thinking mode enabled
340
+ const pruneMessages = createPruneMessages({
341
+ maxTokens: 1000, // Set a high token limit to avoid pruning
342
+ startIndex: 0,
343
+ tokenCounter,
344
+ indexTokenCountMap: { ...indexTokenCountMap },
345
+ thinkingEnabled: true,
346
+ });
347
+
348
+ // Prune messages
349
+ const result = pruneMessages({ messages });
350
+
351
+ // Verify that all messages are preserved
352
+ expect(result.context.length).toBe(messages.length);
353
+ expect(result.context).toEqual(messages);
354
+ });
355
+
356
+ it('should handle the case when no thinking blocks are found', () => {
357
+ // Create a token counter
358
+ const tokenCounter = createTestTokenCounter();
359
+
360
+ // Create messages without thinking blocks
361
+ const messages = [
362
+ new SystemMessage("System instruction"),
363
+ new HumanMessage("Hello"),
364
+ new AIMessage("Response without thinking"),
365
+ new HumanMessage("Next message"),
366
+ new AIMessage("Another response without thinking"),
367
+ ];
368
+
369
+ // Calculate token counts for each message
370
+ const indexTokenCountMap: Record<string, number> = {};
371
+ for (let i = 0; i < messages.length; i++) {
372
+ indexTokenCountMap[i] = tokenCounter(messages[i]);
373
+ }
374
+
375
+ // Create pruneMessages function with thinking mode enabled
376
+ const pruneMessages = createPruneMessages({
377
+ maxTokens: 50, // Set a low token limit to force pruning
378
+ startIndex: 0,
379
+ tokenCounter,
380
+ indexTokenCountMap: { ...indexTokenCountMap },
381
+ thinkingEnabled: true,
382
+ });
383
+
384
+ // Prune messages
385
+ const result = pruneMessages({ messages });
386
+
387
+ // Verify that the pruned context has fewer messages than the original
388
+ expect(result.context.length).toBeLessThan(messages.length);
389
+
390
+ // The function should not throw an error even though no thinking blocks are found
391
+ expect(() => pruneMessages({ messages })).not.toThrow();
392
+ });
393
+
394
+ it('should preserve AI <--> tool message correspondences when pruning', () => {
395
+ // Create a token counter
396
+ const tokenCounter = createTestTokenCounter();
397
+
398
+ // Create messages with tool calls
399
+ const assistantMessageWithToolCall = new AIMessage({
400
+ content: [
401
+ {
402
+ type: "text",
403
+ text: "Let me check that file:",
404
+ },
405
+ {
406
+ type: "tool_use",
407
+ id: "tool123",
408
+ name: "text_editor_mcp_textEditor",
409
+ input: "{\"command\": \"view\", \"path\": \"/path/to/file.txt\"}",
410
+ },
411
+ ],
412
+ });
413
+
414
+ const toolResponseMessage = new ToolMessage({
415
+ content: [
416
+ {
417
+ type: "text",
418
+ text: "{\"success\":true,\"message\":\"File content\"}",
419
+ },
420
+ ],
421
+ tool_call_id: "tool123",
422
+ name: "text_editor_mcp_textEditor",
423
+ });
424
+
425
+ const assistantMessageWithThinking = new AIMessage({
426
+ content: [
427
+ {
428
+ type: "thinking",
429
+ thinking: "This is a thinking block",
430
+ },
431
+ {
432
+ type: "text",
433
+ text: "Response with thinking",
434
+ },
435
+ ],
436
+ });
437
+
438
+ const messages = [
439
+ new SystemMessage("System instruction"),
440
+ new HumanMessage("Hello"),
441
+ assistantMessageWithToolCall,
442
+ toolResponseMessage,
443
+ new HumanMessage("Next message"),
444
+ assistantMessageWithThinking,
445
+ ];
446
+
447
+ // Calculate token counts for each message
448
+ const indexTokenCountMap: Record<string, number> = {};
449
+ for (let i = 0; i < messages.length; i++) {
450
+ indexTokenCountMap[i] = tokenCounter(messages[i]);
451
+ }
452
+
453
+ // Create pruneMessages function with thinking mode enabled and a low token limit
454
+ const pruneMessages = createPruneMessages({
455
+ maxTokens: 100, // Set a low token limit to force pruning
456
+ startIndex: 0,
457
+ tokenCounter,
458
+ indexTokenCountMap: { ...indexTokenCountMap },
459
+ thinkingEnabled: true,
460
+ });
461
+
462
+ // Prune messages
463
+ const result = pruneMessages({ messages });
464
+
465
+ // Find assistant message with tool call and its corresponding tool message in the pruned context
466
+ const assistantIndex = result.context.findIndex(msg =>
467
+ msg.getType() === 'ai' &&
468
+ Array.isArray(msg.content) &&
469
+ msg.content.some(item => item && typeof item === 'object' && item.type === 'tool_use' && item.id === 'tool123')
470
+ );
471
+
472
+ // If the assistant message with tool call is in the context, its corresponding tool message should also be there
473
+ if (assistantIndex !== -1) {
474
+ const toolIndex = result.context.findIndex(msg =>
475
+ msg.getType() === 'tool' &&
476
+ 'tool_call_id' in msg &&
477
+ msg.tool_call_id === 'tool123'
478
+ );
479
+
480
+ expect(toolIndex).not.toBe(-1);
481
+ }
482
+
483
+ // If the tool message is in the context, its corresponding assistant message should also be there
484
+ const toolIndex = result.context.findIndex(msg =>
485
+ msg.getType() === 'tool' &&
486
+ 'tool_call_id' in msg &&
487
+ msg.tool_call_id === 'tool123'
488
+ );
489
+
490
+ if (toolIndex !== -1) {
491
+ const assistantWithToolIndex = result.context.findIndex(msg =>
492
+ msg.getType() === 'ai' &&
493
+ Array.isArray(msg.content) &&
494
+ msg.content.some(item => item && typeof item === 'object' && item.type === 'tool_use' && item.id === 'tool123')
495
+ );
496
+
497
+ expect(assistantWithToolIndex).not.toBe(-1);
498
+ }
499
+ });
500
+
501
+ it('should ensure an assistant message appears early in the context when the latest message is an assistant message', () => {
502
+ // Create a token counter
503
+ const tokenCounter = createTestTokenCounter();
504
+
505
+ // Create messages with the latest message being an assistant message
506
+ const assistantMessageWithThinking = new AIMessage({
507
+ content: [
508
+ {
509
+ type: "thinking",
510
+ thinking: "This is a thinking block",
511
+ },
512
+ {
513
+ type: "text",
514
+ text: "Response with thinking",
515
+ },
516
+ ],
517
+ });
518
+
519
+ // Test case without system message
520
+ const messagesWithoutSystem = [
521
+ new HumanMessage("Hello"),
522
+ new AIMessage("First assistant response"),
523
+ new HumanMessage("Next message"),
524
+ assistantMessageWithThinking, // Latest message is an assistant message
525
+ ];
526
+
527
+ // Calculate token counts for each message
528
+ const indexTokenCountMapWithoutSystem: Record<string, number> = {};
529
+ for (let i = 0; i < messagesWithoutSystem.length; i++) {
530
+ indexTokenCountMapWithoutSystem[i] = tokenCounter(messagesWithoutSystem[i]);
531
+ }
532
+
533
+ // Create pruneMessages function with thinking mode enabled
534
+ const pruneMessagesWithoutSystem = createPruneMessages({
535
+ maxTokens: 60, // Set a lower token limit to force more pruning
536
+ startIndex: 0,
537
+ tokenCounter,
538
+ indexTokenCountMap: { ...indexTokenCountMapWithoutSystem },
539
+ thinkingEnabled: true,
540
+ });
541
+
542
+ // Prune messages
543
+ const resultWithoutSystem = pruneMessagesWithoutSystem({ messages: messagesWithoutSystem });
544
+
545
+ // Verify that the first message in the pruned context is an assistant message when no system message
546
+ expect(resultWithoutSystem.context.length).toBeGreaterThan(0);
547
+ expect(resultWithoutSystem.context[0].getType()).toBe('ai');
548
+
549
+ // Test case with system message
550
+ const messagesWithSystem = [
551
+ new SystemMessage("System instruction"),
552
+ new HumanMessage("Hello"),
553
+ new AIMessage("First assistant response"),
554
+ new HumanMessage("Next message"),
555
+ assistantMessageWithThinking, // Latest message is an assistant message
556
+ ];
557
+
558
+ // Calculate token counts for each message
559
+ const indexTokenCountMapWithSystem: Record<string, number> = {};
560
+ for (let i = 0; i < messagesWithSystem.length; i++) {
561
+ indexTokenCountMapWithSystem[i] = tokenCounter(messagesWithSystem[i]);
562
+ }
563
+
564
+ // Create pruneMessages function with thinking mode enabled
565
+ const pruneMessagesWithSystem = createPruneMessages({
566
+ maxTokens: 70, // Set a token limit to force some pruning but keep system message
567
+ startIndex: 0,
568
+ tokenCounter,
569
+ indexTokenCountMap: { ...indexTokenCountMapWithSystem },
570
+ thinkingEnabled: true,
571
+ });
572
+
573
+ // Prune messages
574
+ const resultWithSystem = pruneMessagesWithSystem({ messages: messagesWithSystem });
575
+
576
+ // Verify that the system message remains first, followed by an assistant message
577
+ expect(resultWithSystem.context.length).toBeGreaterThan(1);
578
+ expect(resultWithSystem.context[0].getType()).toBe('system');
579
+ expect(resultWithSystem.context[1].getType()).toBe('ai');
580
+ });
581
+
582
+ it('should look for thinking blocks starting from the most recent messages', () => {
583
+ // Create a token counter
584
+ const tokenCounter = createTestTokenCounter();
585
+
586
+ // Create messages with multiple thinking blocks
587
+ const olderAssistantMessageWithThinking = new AIMessage({
588
+ content: [
589
+ {
590
+ type: "thinking",
591
+ thinking: "This is an older thinking block",
592
+ },
593
+ {
594
+ type: "text",
595
+ text: "Older response with thinking",
596
+ },
597
+ ],
598
+ });
599
+
600
+ const newerAssistantMessageWithThinking = new AIMessage({
601
+ content: [
602
+ {
603
+ type: "thinking",
604
+ thinking: "This is a newer thinking block",
605
+ },
606
+ {
607
+ type: "text",
608
+ text: "Newer response with thinking",
609
+ },
610
+ ],
611
+ });
612
+
613
+ const messages = [
614
+ new SystemMessage("System instruction"),
615
+ new HumanMessage("Hello"),
616
+ olderAssistantMessageWithThinking,
617
+ new HumanMessage("Next message"),
618
+ newerAssistantMessageWithThinking,
619
+ ];
620
+
621
+ // Calculate token counts for each message
622
+ const indexTokenCountMap: Record<string, number> = {};
623
+ for (let i = 0; i < messages.length; i++) {
624
+ indexTokenCountMap[i] = tokenCounter(messages[i]);
625
+ }
626
+
627
+ // Create pruneMessages function with thinking mode enabled
628
+ // Set a token limit that will force pruning of the older assistant message
629
+ const pruneMessages = createPruneMessages({
630
+ maxTokens: 80,
631
+ startIndex: 0,
632
+ tokenCounter,
633
+ indexTokenCountMap: { ...indexTokenCountMap },
634
+ thinkingEnabled: true,
635
+ });
636
+
637
+ // Prune messages
638
+ const result = pruneMessages({ messages });
639
+
640
+ // Find the first assistant message in the pruned context
641
+ const firstAssistantIndex = result.context.findIndex(msg => msg.getType() === 'ai');
642
+ expect(firstAssistantIndex).not.toBe(-1);
643
+
644
+ const firstAssistantMsg = result.context[firstAssistantIndex];
645
+ expect(Array.isArray(firstAssistantMsg.content)).toBe(true);
646
+
647
+ // Verify that the first assistant message has a thinking block
648
+ const thinkingBlock = (firstAssistantMsg.content as any[]).find(item =>
649
+ item && typeof item === 'object' && item.type === 'thinking');
650
+ expect(thinkingBlock).toBeDefined();
651
+
652
+ // Verify that it's the newer thinking block
653
+ expect(thinkingBlock.thinking).toContain("newer thinking block");
654
+ });
655
+ });