@librechat/agents 2.3.0 → 2.3.2

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