@illuma-ai/agents 1.1.21 → 1.1.22

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 (241) hide show
  1. package/dist/cjs/graphs/Graph.cjs +12 -1
  2. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  3. package/dist/cjs/graphs/MultiAgentGraph.cjs +85 -1
  4. package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
  5. package/dist/cjs/run.cjs +20 -9
  6. package/dist/cjs/run.cjs.map +1 -1
  7. package/dist/esm/graphs/Graph.mjs +12 -1
  8. package/dist/esm/graphs/Graph.mjs.map +1 -1
  9. package/dist/esm/graphs/MultiAgentGraph.mjs +85 -1
  10. package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
  11. package/dist/esm/run.mjs +20 -9
  12. package/dist/esm/run.mjs.map +1 -1
  13. package/dist/types/graphs/MultiAgentGraph.d.ts +17 -0
  14. package/package.json +1 -1
  15. package/src/graphs/Graph.ts +12 -1
  16. package/src/graphs/MultiAgentGraph.ts +105 -1
  17. package/src/graphs/__tests__/multi-agent-delegate.test.ts +191 -0
  18. package/src/run.ts +20 -11
  19. package/src/scripts/test-bedrock-handoff-autonomous.ts +231 -0
  20. package/src/agents/AgentContext.js +0 -782
  21. package/src/agents/AgentContext.test.js +0 -421
  22. package/src/agents/__tests__/AgentContext.test.js +0 -678
  23. package/src/agents/__tests__/resolveStructuredOutputMode.test.js +0 -117
  24. package/src/common/enum.js +0 -192
  25. package/src/common/index.js +0 -3
  26. package/src/events.js +0 -166
  27. package/src/graphs/Graph.js +0 -1857
  28. package/src/graphs/MultiAgentGraph.js +0 -1092
  29. package/src/graphs/__tests__/structured-output.integration.test.js +0 -624
  30. package/src/graphs/__tests__/structured-output.test.js +0 -144
  31. package/src/graphs/contextManagement.e2e.test.js +0 -718
  32. package/src/graphs/contextManagement.test.js +0 -485
  33. package/src/graphs/handoffValidation.test.js +0 -276
  34. package/src/graphs/index.js +0 -3
  35. package/src/index.js +0 -28
  36. package/src/instrumentation.js +0 -21
  37. package/src/llm/anthropic/index.js +0 -319
  38. package/src/llm/anthropic/types.js +0 -46
  39. package/src/llm/anthropic/utils/message_inputs.js +0 -627
  40. package/src/llm/anthropic/utils/message_outputs.js +0 -290
  41. package/src/llm/anthropic/utils/output_parsers.js +0 -89
  42. package/src/llm/anthropic/utils/tools.js +0 -25
  43. package/src/llm/bedrock/__tests__/bedrock-caching.test.js +0 -392
  44. package/src/llm/bedrock/index.js +0 -303
  45. package/src/llm/bedrock/types.js +0 -2
  46. package/src/llm/bedrock/utils/index.js +0 -6
  47. package/src/llm/bedrock/utils/message_inputs.js +0 -463
  48. package/src/llm/bedrock/utils/message_outputs.js +0 -269
  49. package/src/llm/fake.js +0 -92
  50. package/src/llm/google/index.js +0 -215
  51. package/src/llm/google/types.js +0 -12
  52. package/src/llm/google/utils/common.js +0 -670
  53. package/src/llm/google/utils/tools.js +0 -111
  54. package/src/llm/google/utils/zod_to_genai_parameters.js +0 -47
  55. package/src/llm/openai/index.js +0 -1033
  56. package/src/llm/openai/types.js +0 -2
  57. package/src/llm/openai/utils/index.js +0 -756
  58. package/src/llm/openai/utils/isReasoningModel.test.js +0 -79
  59. package/src/llm/openrouter/index.js +0 -261
  60. package/src/llm/openrouter/reasoning.test.js +0 -181
  61. package/src/llm/providers.js +0 -36
  62. package/src/llm/text.js +0 -65
  63. package/src/llm/vertexai/index.js +0 -402
  64. package/src/messages/__tests__/tools.test.js +0 -392
  65. package/src/messages/cache.js +0 -404
  66. package/src/messages/cache.test.js +0 -1167
  67. package/src/messages/content.js +0 -48
  68. package/src/messages/content.test.js +0 -314
  69. package/src/messages/core.js +0 -359
  70. package/src/messages/ensureThinkingBlock.test.js +0 -997
  71. package/src/messages/format.js +0 -973
  72. package/src/messages/formatAgentMessages.test.js +0 -2278
  73. package/src/messages/formatAgentMessages.tools.test.js +0 -362
  74. package/src/messages/formatMessage.test.js +0 -608
  75. package/src/messages/ids.js +0 -18
  76. package/src/messages/index.js +0 -9
  77. package/src/messages/labelContentByAgent.test.js +0 -725
  78. package/src/messages/prune.js +0 -438
  79. package/src/messages/reducer.js +0 -60
  80. package/src/messages/shiftIndexTokenCountMap.test.js +0 -63
  81. package/src/messages/summarize.js +0 -146
  82. package/src/messages/summarize.test.js +0 -332
  83. package/src/messages/tools.js +0 -90
  84. package/src/mockStream.js +0 -81
  85. package/src/prompts/collab.js +0 -7
  86. package/src/prompts/index.js +0 -3
  87. package/src/prompts/taskmanager.js +0 -58
  88. package/src/run.js +0 -427
  89. package/src/schemas/index.js +0 -3
  90. package/src/schemas/schema-preparation.test.js +0 -370
  91. package/src/schemas/validate.js +0 -314
  92. package/src/schemas/validate.test.js +0 -264
  93. package/src/scripts/abort.js +0 -127
  94. package/src/scripts/ant_web_search.js +0 -130
  95. package/src/scripts/ant_web_search_edge_case.js +0 -133
  96. package/src/scripts/ant_web_search_error_edge_case.js +0 -119
  97. package/src/scripts/args.js +0 -41
  98. package/src/scripts/bedrock-cache-debug.js +0 -186
  99. package/src/scripts/bedrock-content-aggregation-test.js +0 -195
  100. package/src/scripts/bedrock-merge-test.js +0 -80
  101. package/src/scripts/bedrock-parallel-tools-test.js +0 -150
  102. package/src/scripts/caching.js +0 -106
  103. package/src/scripts/cli.js +0 -152
  104. package/src/scripts/cli2.js +0 -119
  105. package/src/scripts/cli3.js +0 -163
  106. package/src/scripts/cli4.js +0 -165
  107. package/src/scripts/cli5.js +0 -165
  108. package/src/scripts/code_exec.js +0 -171
  109. package/src/scripts/code_exec_files.js +0 -180
  110. package/src/scripts/code_exec_multi_session.js +0 -185
  111. package/src/scripts/code_exec_ptc.js +0 -265
  112. package/src/scripts/code_exec_session.js +0 -217
  113. package/src/scripts/code_exec_simple.js +0 -120
  114. package/src/scripts/content.js +0 -111
  115. package/src/scripts/empty_input.js +0 -125
  116. package/src/scripts/handoff-test.js +0 -96
  117. package/src/scripts/image.js +0 -138
  118. package/src/scripts/memory.js +0 -83
  119. package/src/scripts/multi-agent-chain.js +0 -271
  120. package/src/scripts/multi-agent-conditional.js +0 -185
  121. package/src/scripts/multi-agent-document-review-chain.js +0 -171
  122. package/src/scripts/multi-agent-hybrid-flow.js +0 -264
  123. package/src/scripts/multi-agent-parallel-start.js +0 -214
  124. package/src/scripts/multi-agent-parallel.js +0 -346
  125. package/src/scripts/multi-agent-sequence.js +0 -184
  126. package/src/scripts/multi-agent-supervisor.js +0 -324
  127. package/src/scripts/multi-agent-test.js +0 -147
  128. package/src/scripts/parallel-asymmetric-tools-test.js +0 -202
  129. package/src/scripts/parallel-full-metadata-test.js +0 -176
  130. package/src/scripts/parallel-tools-test.js +0 -256
  131. package/src/scripts/programmatic_exec.js +0 -277
  132. package/src/scripts/programmatic_exec_agent.js +0 -168
  133. package/src/scripts/search.js +0 -118
  134. package/src/scripts/sequential-full-metadata-test.js +0 -143
  135. package/src/scripts/simple.js +0 -174
  136. package/src/scripts/single-agent-metadata-test.js +0 -152
  137. package/src/scripts/stream.js +0 -113
  138. package/src/scripts/test-custom-prompt-key.js +0 -132
  139. package/src/scripts/test-handoff-input.js +0 -143
  140. package/src/scripts/test-handoff-preamble.js +0 -227
  141. package/src/scripts/test-handoff-steering.js +0 -353
  142. package/src/scripts/test-multi-agent-list-handoff.js +0 -318
  143. package/src/scripts/test-parallel-agent-labeling.js +0 -253
  144. package/src/scripts/test-parallel-handoffs.js +0 -229
  145. package/src/scripts/test-thinking-handoff-bedrock.js +0 -132
  146. package/src/scripts/test-thinking-handoff.js +0 -132
  147. package/src/scripts/test-thinking-to-thinking-handoff-bedrock.js +0 -140
  148. package/src/scripts/test-tool-before-handoff-role-order.js +0 -223
  149. package/src/scripts/test-tools-before-handoff.js +0 -187
  150. package/src/scripts/test_code_api.js +0 -263
  151. package/src/scripts/thinking-bedrock.js +0 -128
  152. package/src/scripts/thinking-vertexai.js +0 -130
  153. package/src/scripts/thinking.js +0 -134
  154. package/src/scripts/tool_search.js +0 -114
  155. package/src/scripts/tools.js +0 -125
  156. package/src/specs/agent-handoffs-bedrock.integration.test.js +0 -280
  157. package/src/specs/agent-handoffs.test.js +0 -924
  158. package/src/specs/anthropic.simple.test.js +0 -287
  159. package/src/specs/azure.simple.test.js +0 -381
  160. package/src/specs/cache.simple.test.js +0 -282
  161. package/src/specs/custom-event-await.test.js +0 -148
  162. package/src/specs/deepseek.simple.test.js +0 -189
  163. package/src/specs/emergency-prune.test.js +0 -308
  164. package/src/specs/moonshot.simple.test.js +0 -237
  165. package/src/specs/observability.integration.test.js +0 -1337
  166. package/src/specs/openai.simple.test.js +0 -233
  167. package/src/specs/openrouter.simple.test.js +0 -202
  168. package/src/specs/prune.test.js +0 -733
  169. package/src/specs/reasoning.test.js +0 -144
  170. package/src/specs/spec.utils.js +0 -4
  171. package/src/specs/thinking-handoff.test.js +0 -486
  172. package/src/specs/thinking-prune.test.js +0 -600
  173. package/src/specs/token-distribution-edge-case.test.js +0 -246
  174. package/src/specs/token-memoization.test.js +0 -32
  175. package/src/specs/tokens.test.js +0 -49
  176. package/src/specs/tool-error.test.js +0 -139
  177. package/src/splitStream.js +0 -204
  178. package/src/splitStream.test.js +0 -504
  179. package/src/stream.js +0 -650
  180. package/src/stream.test.js +0 -225
  181. package/src/test/mockTools.js +0 -340
  182. package/src/tools/BrowserTools.js +0 -245
  183. package/src/tools/Calculator.js +0 -38
  184. package/src/tools/Calculator.test.js +0 -225
  185. package/src/tools/CodeExecutor.js +0 -233
  186. package/src/tools/ProgrammaticToolCalling.js +0 -602
  187. package/src/tools/StreamingToolCallBuffer.js +0 -179
  188. package/src/tools/ToolNode.js +0 -930
  189. package/src/tools/ToolSearch.js +0 -904
  190. package/src/tools/__tests__/BrowserTools.test.js +0 -306
  191. package/src/tools/__tests__/ProgrammaticToolCalling.integration.test.js +0 -276
  192. package/src/tools/__tests__/ProgrammaticToolCalling.test.js +0 -807
  193. package/src/tools/__tests__/StreamingToolCallBuffer.test.js +0 -175
  194. package/src/tools/__tests__/ToolApproval.test.js +0 -675
  195. package/src/tools/__tests__/ToolNode.recovery.test.js +0 -200
  196. package/src/tools/__tests__/ToolNode.session.test.js +0 -319
  197. package/src/tools/__tests__/ToolSearch.integration.test.js +0 -125
  198. package/src/tools/__tests__/ToolSearch.test.js +0 -812
  199. package/src/tools/__tests__/handlers.test.js +0 -799
  200. package/src/tools/__tests__/truncation-recovery.integration.test.js +0 -362
  201. package/src/tools/handlers.js +0 -306
  202. package/src/tools/schema.js +0 -25
  203. package/src/tools/search/anthropic.js +0 -34
  204. package/src/tools/search/content.js +0 -116
  205. package/src/tools/search/content.test.js +0 -133
  206. package/src/tools/search/firecrawl.js +0 -173
  207. package/src/tools/search/format.js +0 -198
  208. package/src/tools/search/highlights.js +0 -241
  209. package/src/tools/search/index.js +0 -3
  210. package/src/tools/search/jina-reranker.test.js +0 -106
  211. package/src/tools/search/rerankers.js +0 -165
  212. package/src/tools/search/schema.js +0 -102
  213. package/src/tools/search/search.js +0 -561
  214. package/src/tools/search/serper-scraper.js +0 -126
  215. package/src/tools/search/test.js +0 -129
  216. package/src/tools/search/tool.js +0 -453
  217. package/src/tools/search/types.js +0 -2
  218. package/src/tools/search/utils.js +0 -59
  219. package/src/types/graph.js +0 -24
  220. package/src/types/graph.test.js +0 -192
  221. package/src/types/index.js +0 -7
  222. package/src/types/llm.js +0 -2
  223. package/src/types/messages.js +0 -2
  224. package/src/types/run.js +0 -2
  225. package/src/types/stream.js +0 -2
  226. package/src/types/tools.js +0 -2
  227. package/src/utils/contextAnalytics.js +0 -79
  228. package/src/utils/contextAnalytics.test.js +0 -166
  229. package/src/utils/events.js +0 -26
  230. package/src/utils/graph.js +0 -11
  231. package/src/utils/handlers.js +0 -65
  232. package/src/utils/index.js +0 -10
  233. package/src/utils/llm.js +0 -21
  234. package/src/utils/llmConfig.js +0 -205
  235. package/src/utils/logging.js +0 -37
  236. package/src/utils/misc.js +0 -51
  237. package/src/utils/run.js +0 -69
  238. package/src/utils/schema.js +0 -21
  239. package/src/utils/title.js +0 -119
  240. package/src/utils/tokens.js +0 -92
  241. package/src/utils/toonFormat.js +0 -379
@@ -1,600 +0,0 @@
1
- // src/specs/thinking-prune.test.ts
2
- import { HumanMessage, AIMessage, SystemMessage, ToolMessage } from '@langchain/core/messages';
3
- import { createPruneMessages } from '@/messages/prune';
4
- // Create a simple token counter for testing
5
- const createTestTokenCounter = () => {
6
- return (message) => {
7
- // Use type assertion to help TypeScript understand the type
8
- const content = message.content;
9
- // Handle string content
10
- if (typeof content === 'string') {
11
- return content.length;
12
- }
13
- // Handle array content
14
- if (Array.isArray(content)) {
15
- let totalLength = 0;
16
- for (const item of content) {
17
- if (typeof item === 'string') {
18
- totalLength += item.length;
19
- }
20
- else if (typeof item === 'object') {
21
- if (item.type === 'thinking' && typeof item.thinking === 'string') {
22
- totalLength += item.thinking.length;
23
- }
24
- else if ('text' in item && typeof item.text === 'string') {
25
- totalLength += item.text.length;
26
- }
27
- else if ('input' in item && typeof item.input === 'string') {
28
- totalLength += item.input.length;
29
- }
30
- }
31
- }
32
- return totalLength;
33
- }
34
- // Default case - if content is null, undefined, or any other type
35
- return 0;
36
- };
37
- };
38
- describe('Prune Messages with Thinking Mode Tests', () => {
39
- jest.setTimeout(30000);
40
- it('should preserve thinking blocks when pruning with thinking mode enabled', () => {
41
- // Create a token counter
42
- const tokenCounter = createTestTokenCounter();
43
- // Create messages based on the example provided
44
- const userMessage = new HumanMessage({
45
- content: [
46
- {
47
- type: 'text',
48
- text: '/home/user/Illuma/gistfile1.txt\n\nread it 200 lines at a time\n\nthere are 5000 lines\n\ndo not stop until done',
49
- },
50
- ],
51
- });
52
- const assistantMessageWithThinking = new AIMessage({
53
- content: [
54
- {
55
- type: 'thinking',
56
- thinking: 'The user is asking me to read a file located at `/home/user/Illuma/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.',
57
- },
58
- {
59
- type: 'text',
60
- 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:',
61
- },
62
- {
63
- type: 'tool_use',
64
- id: 'toolu_01YApWuFsEQCuBFDgYXmcmeZ',
65
- name: 'text_editor_mcp_textEditor',
66
- input: '{"command": "view", "path": "/home/user/Illuma/gistfile1.txt", "description": "Viewing lines 1-200 of the file", "view_range": [1,200]}',
67
- },
68
- ],
69
- });
70
- const toolResponseMessage1 = new ToolMessage({
71
- content: [
72
- {
73
- type: 'text',
74
- text: '{"success":true,"message":"File content (truncated):"',
75
- },
76
- ],
77
- tool_call_id: 'toolu_01YApWuFsEQCuBFDgYXmcmeZ',
78
- name: 'text_editor_mcp_textEditor',
79
- });
80
- const assistantMessage2 = new AIMessage({
81
- content: [
82
- {
83
- type: 'text',
84
- text: 'It looks like the file content is clipped in the response. I\'ll continue with the next chunks:',
85
- },
86
- {
87
- type: 'tool_use',
88
- id: 'toolu_01VnyMQ4CvEd6zLDxxtTd6d4',
89
- name: 'text_editor_mcp_textEditor',
90
- input: '{"command": "view", "path": "/home/user/Illuma/gistfile1.txt", "description": "Viewing lines 201-400 of the file", "view_range": [201,400]}',
91
- },
92
- ],
93
- });
94
- const toolResponseMessage2 = new ToolMessage({
95
- content: [
96
- {
97
- type: 'text',
98
- text: '{"success":true,"message":"File content (truncated):"',
99
- },
100
- ],
101
- tool_call_id: 'toolu_01VnyMQ4CvEd6zLDxxtTd6d4',
102
- name: 'text_editor_mcp_textEditor',
103
- });
104
- const assistantMessage3 = new AIMessage({
105
- content: [
106
- {
107
- type: 'text',
108
- text: 'Continuing with the next chunk:',
109
- },
110
- {
111
- type: 'tool_use',
112
- id: 'toolu_01TZKs4nnBc58BYXKz1Mw4fp',
113
- name: 'text_editor_mcp_textEditor',
114
- input: '{"command": "view", "path": "/home/user/Illuma/gistfile1.txt", "description": "Viewing lines 401-600 of the file", "view_range": [401,600]}',
115
- },
116
- ],
117
- });
118
- const toolResponseMessage3 = new ToolMessage({
119
- content: [
120
- {
121
- type: 'text',
122
- text: '{"success":true,"message":"File content (truncated):"',
123
- },
124
- ],
125
- tool_call_id: 'toolu_01TZKs4nnBc58BYXKz1Mw4fp',
126
- name: 'text_editor_mcp_textEditor',
127
- });
128
- const assistantMessage4 = new AIMessage({
129
- content: [
130
- {
131
- type: 'text',
132
- text: 'Let\'s continue with the next chunk:',
133
- },
134
- {
135
- type: 'tool_use',
136
- id: 'toolu_01TZgBacNxjx1QNUpJg9hca5',
137
- name: 'text_editor_mcp_textEditor',
138
- input: '{"command": "view", "path": "/home/user/Illuma/gistfile1.txt", "description": "Viewing lines 601-800 of the file", "view_range": [601,800]}',
139
- },
140
- ],
141
- });
142
- const toolResponseMessage4 = new ToolMessage({
143
- content: [
144
- {
145
- type: 'text',
146
- text: '{"success":true,"message":"File content (truncated):"',
147
- },
148
- ],
149
- tool_call_id: 'toolu_01TZgBacNxjx1QNUpJg9hca5',
150
- name: 'text_editor_mcp_textEditor',
151
- });
152
- const messages = [
153
- userMessage,
154
- assistantMessageWithThinking,
155
- toolResponseMessage1,
156
- assistantMessage2,
157
- toolResponseMessage2,
158
- assistantMessage3,
159
- toolResponseMessage3,
160
- assistantMessage4,
161
- toolResponseMessage4,
162
- ];
163
- // Create indexTokenCountMap based on the example provided
164
- const indexTokenCountMap = {
165
- '0': 617, // userMessage
166
- '1': 52, // assistantMessageWithThinking
167
- '2': 4995, // toolResponseMessage1
168
- '3': 307, // assistantMessage2
169
- '4': 9359, // toolResponseMessage2
170
- '5': 178, // assistantMessage3
171
- '6': 5463, // toolResponseMessage3
172
- '7': 125, // assistantMessage4
173
- '8': 4264, // toolResponseMessage4
174
- };
175
- // Create pruneMessages function with thinking mode enabled
176
- const pruneMessages = createPruneMessages({
177
- maxTokens: 19800,
178
- startIndex: 0,
179
- tokenCounter,
180
- indexTokenCountMap: { ...indexTokenCountMap },
181
- thinkingEnabled: true,
182
- });
183
- // Prune messages
184
- const result = pruneMessages({
185
- messages,
186
- usageMetadata: {
187
- input_tokens: 25254,
188
- output_tokens: 106,
189
- total_tokens: 25360,
190
- input_token_details: {
191
- cache_read: 0,
192
- cache_creation: 0,
193
- },
194
- },
195
- startType: 'human',
196
- });
197
- // Verify that the first assistant message in the pruned context has a thinking block
198
- expect(result.context.length).toBeGreaterThan(0);
199
- // Find the first assistant message in the pruned context
200
- const firstAssistantIndex = result.context.findIndex(msg => msg.getType() === 'ai');
201
- expect(firstAssistantIndex).toBe(0);
202
- const firstAssistantMsg = result.context[firstAssistantIndex];
203
- expect(Array.isArray(firstAssistantMsg.content)).toBe(true);
204
- // Verify that the first assistant message has a thinking block
205
- const hasThinkingBlock = firstAssistantMsg.content.some((item) => typeof item === 'object' && item.type === 'thinking');
206
- expect(hasThinkingBlock).toBe(true);
207
- // Verify that the thinking block is from the original assistant message
208
- const thinkingBlock = firstAssistantMsg.content.find((item) => typeof item === 'object' && item.type === 'thinking');
209
- expect(thinkingBlock).toBeDefined();
210
- expect(thinkingBlock.thinking).toContain('The user is asking me to read a file');
211
- });
212
- it('should handle token recalculation when inserting thinking blocks', () => {
213
- // Create a token counter
214
- const tokenCounter = createTestTokenCounter();
215
- // Create a message with thinking block
216
- const assistantMessageWithThinking = new AIMessage({
217
- content: [
218
- {
219
- type: 'thinking',
220
- thinking: 'This is a thinking block',
221
- },
222
- {
223
- type: 'text',
224
- text: 'Response with thinking',
225
- },
226
- ],
227
- });
228
- // Create a message without thinking block
229
- const assistantMessageWithoutThinking = new AIMessage({
230
- content: [
231
- {
232
- type: 'text',
233
- text: 'Response without thinking',
234
- },
235
- ],
236
- });
237
- const messages = [
238
- new SystemMessage('System instruction'),
239
- new HumanMessage('Hello'),
240
- assistantMessageWithThinking,
241
- new HumanMessage('Next message'),
242
- assistantMessageWithoutThinking,
243
- ];
244
- // Calculate token counts for each message
245
- const indexTokenCountMap = {};
246
- for (let i = 0; i < messages.length; i++) {
247
- indexTokenCountMap[i] = tokenCounter(messages[i]);
248
- }
249
- // Create pruneMessages function with thinking mode enabled
250
- const pruneMessages = createPruneMessages({
251
- maxTokens: 50, // Set a low token limit to force pruning
252
- startIndex: 0,
253
- tokenCounter,
254
- indexTokenCountMap: { ...indexTokenCountMap },
255
- thinkingEnabled: true,
256
- });
257
- // Prune messages
258
- const result = pruneMessages({ messages });
259
- // Verify that the pruned context has fewer messages than the original
260
- expect(result.context.length).toBeLessThan(messages.length);
261
- });
262
- it('should not modify messages when under token limit', () => {
263
- // Create a token counter
264
- const tokenCounter = createTestTokenCounter();
265
- // Create a message with thinking block
266
- const assistantMessageWithThinking = new AIMessage({
267
- content: [
268
- {
269
- type: 'thinking',
270
- thinking: 'This is a thinking block',
271
- },
272
- {
273
- type: 'text',
274
- text: 'Response with thinking',
275
- },
276
- ],
277
- });
278
- const messages = [
279
- new SystemMessage('System instruction'),
280
- new HumanMessage('Hello'),
281
- assistantMessageWithThinking,
282
- ];
283
- // Calculate token counts for each message
284
- const indexTokenCountMap = {};
285
- for (let i = 0; i < messages.length; i++) {
286
- indexTokenCountMap[i] = tokenCounter(messages[i]);
287
- }
288
- // Create pruneMessages function with thinking mode enabled
289
- const pruneMessages = createPruneMessages({
290
- maxTokens: 1000, // Set a high token limit to avoid pruning
291
- startIndex: 0,
292
- tokenCounter,
293
- indexTokenCountMap: { ...indexTokenCountMap },
294
- thinkingEnabled: true,
295
- });
296
- // Prune messages
297
- const result = pruneMessages({ messages });
298
- // Verify that all messages are preserved
299
- expect(result.context.length).toBe(messages.length);
300
- expect(result.context).toEqual(messages);
301
- });
302
- it('should handle the case when no thinking blocks are found', () => {
303
- // Create a token counter
304
- const tokenCounter = createTestTokenCounter();
305
- // Create messages without thinking blocks
306
- const messages = [
307
- new SystemMessage('System instruction'),
308
- new HumanMessage('Hello'),
309
- new AIMessage('Response without thinking'),
310
- new HumanMessage('Next message'),
311
- new AIMessage('Another response without thinking'),
312
- ];
313
- // Calculate token counts for each message
314
- const indexTokenCountMap = {};
315
- for (let i = 0; i < messages.length; i++) {
316
- indexTokenCountMap[i] = tokenCounter(messages[i]);
317
- }
318
- // Create pruneMessages function with thinking mode enabled
319
- const pruneMessages = createPruneMessages({
320
- maxTokens: 50, // Set a low token limit to force pruning
321
- startIndex: 0,
322
- tokenCounter,
323
- indexTokenCountMap: { ...indexTokenCountMap },
324
- thinkingEnabled: true,
325
- });
326
- // Prune messages
327
- const result = pruneMessages({ messages });
328
- // Verify that the pruned context has fewer messages than the original
329
- expect(result.context.length).toBeLessThan(messages.length);
330
- // The function should not throw an error even though no thinking blocks are found
331
- expect(() => pruneMessages({ messages })).not.toThrow();
332
- });
333
- it('should preserve AI <--> tool message correspondences when pruning', () => {
334
- // Create a token counter
335
- const tokenCounter = createTestTokenCounter();
336
- // Create messages with tool calls
337
- const assistantMessageWithToolCall = new AIMessage({
338
- content: [
339
- {
340
- type: 'text',
341
- text: 'Let me check that file:',
342
- },
343
- {
344
- type: 'tool_use',
345
- id: 'tool123',
346
- name: 'text_editor_mcp_textEditor',
347
- input: '{"command": "view", "path": "/path/to/file.txt"}',
348
- },
349
- ],
350
- });
351
- const toolResponseMessage = new ToolMessage({
352
- content: [
353
- {
354
- type: 'text',
355
- text: '{"success":true,"message":"File content"}',
356
- },
357
- ],
358
- tool_call_id: 'tool123',
359
- name: 'text_editor_mcp_textEditor',
360
- });
361
- const assistantMessageWithThinking = new AIMessage({
362
- content: [
363
- {
364
- type: 'thinking',
365
- thinking: 'This is a thinking block',
366
- },
367
- {
368
- type: 'text',
369
- text: 'Response with thinking',
370
- },
371
- ],
372
- });
373
- const messages = [
374
- new SystemMessage('System instruction'),
375
- new HumanMessage('Hello'),
376
- assistantMessageWithToolCall,
377
- toolResponseMessage,
378
- new HumanMessage('Next message'),
379
- assistantMessageWithThinking,
380
- ];
381
- // Calculate token counts for each message
382
- const indexTokenCountMap = {};
383
- for (let i = 0; i < messages.length; i++) {
384
- indexTokenCountMap[i] = tokenCounter(messages[i]);
385
- }
386
- // Create pruneMessages function with thinking mode enabled and a low token limit
387
- const pruneMessages = createPruneMessages({
388
- maxTokens: 100, // Set a low token limit to force pruning
389
- startIndex: 0,
390
- tokenCounter,
391
- indexTokenCountMap: { ...indexTokenCountMap },
392
- thinkingEnabled: true,
393
- });
394
- // Prune messages
395
- const result = pruneMessages({ messages });
396
- // Find assistant message with tool call and its corresponding tool message in the pruned context
397
- const assistantIndex = result.context.findIndex(msg => msg.getType() === 'ai' &&
398
- Array.isArray(msg.content) &&
399
- msg.content.some(item => typeof item === 'object' && item.type === 'tool_use' && item.id === 'tool123'));
400
- // If the assistant message with tool call is in the context, its corresponding tool message should also be there
401
- if (assistantIndex !== -1) {
402
- const toolIndex = result.context.findIndex(msg => msg.getType() === 'tool' &&
403
- 'tool_call_id' in msg &&
404
- msg.tool_call_id === 'tool123');
405
- expect(toolIndex).not.toBe(-1);
406
- }
407
- // If the tool message is in the context, its corresponding assistant message should also be there
408
- const toolIndex = result.context.findIndex(msg => msg.getType() === 'tool' &&
409
- 'tool_call_id' in msg &&
410
- msg.tool_call_id === 'tool123');
411
- if (toolIndex !== -1) {
412
- const assistantWithToolIndex = result.context.findIndex(msg => msg.getType() === 'ai' &&
413
- Array.isArray(msg.content) &&
414
- msg.content.some(item => typeof item === 'object' && item.type === 'tool_use' && item.id === 'tool123'));
415
- expect(assistantWithToolIndex).not.toBe(-1);
416
- }
417
- });
418
- it('should ensure an assistant message with thinking appears in the latest sequence of assistant/tool messages', () => {
419
- // Create a token counter
420
- const tokenCounter = createTestTokenCounter();
421
- // Create messages with the latest message being an assistant message with thinking
422
- const assistantMessageWithThinking = new AIMessage({
423
- content: [
424
- {
425
- type: 'thinking',
426
- thinking: 'This is a thinking block',
427
- },
428
- {
429
- type: 'text',
430
- text: 'Response with thinking',
431
- },
432
- ],
433
- });
434
- // Create an assistant message with tool use
435
- const assistantMessageWithToolUse = new AIMessage({
436
- content: [
437
- {
438
- type: 'text',
439
- text: 'Let me check that file:',
440
- },
441
- {
442
- type: 'tool_use',
443
- id: 'tool123',
444
- name: 'text_editor_mcp_textEditor',
445
- input: '{"command": "view", "path": "/path/to/file.txt"}',
446
- },
447
- ],
448
- });
449
- // Create a tool response message
450
- const toolResponseMessage = new ToolMessage({
451
- content: [
452
- {
453
- type: 'text',
454
- text: '{"success":true,"message":"File content"}',
455
- },
456
- ],
457
- tool_call_id: 'tool123',
458
- name: 'text_editor_mcp_textEditor',
459
- });
460
- // Test case without system message
461
- const messagesWithoutSystem = [
462
- new HumanMessage('Hello'),
463
- assistantMessageWithToolUse,
464
- toolResponseMessage,
465
- new HumanMessage('Next message'),
466
- assistantMessageWithThinking, // Latest message is an assistant message with thinking
467
- ];
468
- // Calculate token counts for each message
469
- const indexTokenCountMapWithoutSystem = {};
470
- for (let i = 0; i < messagesWithoutSystem.length; i++) {
471
- indexTokenCountMapWithoutSystem[i] = tokenCounter(messagesWithoutSystem[i]);
472
- }
473
- // Create pruneMessages function with thinking mode enabled
474
- const pruneMessagesWithoutSystem = createPruneMessages({
475
- maxTokens: 100, // Set a token limit to force some pruning
476
- startIndex: 0,
477
- tokenCounter,
478
- indexTokenCountMap: { ...indexTokenCountMapWithoutSystem },
479
- thinkingEnabled: true,
480
- });
481
- // Prune messages
482
- const resultWithoutSystem = pruneMessagesWithoutSystem({ messages: messagesWithoutSystem });
483
- // Verify that the pruned context contains at least one message
484
- expect(resultWithoutSystem.context.length).toBeGreaterThan(0);
485
- // Find all assistant messages in the latest sequence (after the last human message)
486
- const lastHumanIndex = resultWithoutSystem.context.map(msg => msg.getType()).lastIndexOf('human');
487
- const assistantMessagesAfterLastHuman = resultWithoutSystem.context.slice(lastHumanIndex + 1)
488
- .filter(msg => msg.getType() === 'ai');
489
- // Verify that at least one assistant message exists in the latest sequence
490
- expect(assistantMessagesAfterLastHuman.length).toBeGreaterThan(0);
491
- // Verify that at least one of these assistant messages has a thinking block
492
- const hasThinkingBlock = assistantMessagesAfterLastHuman.some(msg => {
493
- const content = msg.content;
494
- return Array.isArray(content) && content.some(item => typeof item === 'object' && item.type === 'thinking');
495
- });
496
- expect(hasThinkingBlock).toBe(true);
497
- // Test case with system message
498
- const messagesWithSystem = [
499
- new SystemMessage('System instruction'),
500
- new HumanMessage('Hello'),
501
- assistantMessageWithToolUse,
502
- toolResponseMessage,
503
- new HumanMessage('Next message'),
504
- assistantMessageWithThinking, // Latest message is an assistant message with thinking
505
- ];
506
- // Calculate token counts for each message
507
- const indexTokenCountMapWithSystem = {};
508
- for (let i = 0; i < messagesWithSystem.length; i++) {
509
- indexTokenCountMapWithSystem[i] = tokenCounter(messagesWithSystem[i]);
510
- }
511
- // Create pruneMessages function with thinking mode enabled
512
- const pruneMessagesWithSystem = createPruneMessages({
513
- maxTokens: 120, // Set a token limit to force some pruning but keep system message
514
- startIndex: 0,
515
- tokenCounter,
516
- indexTokenCountMap: { ...indexTokenCountMapWithSystem },
517
- thinkingEnabled: true,
518
- });
519
- // Prune messages
520
- const resultWithSystem = pruneMessagesWithSystem({ messages: messagesWithSystem });
521
- // Verify that the system message remains first
522
- expect(resultWithSystem.context.length).toBeGreaterThan(1);
523
- expect(resultWithSystem.context[0].getType()).toBe('system');
524
- // Find all assistant messages in the latest sequence (after the last human message)
525
- const lastHumanIndexWithSystem = resultWithSystem.context.map(msg => msg.getType()).lastIndexOf('human');
526
- const assistantMessagesAfterLastHumanWithSystem = resultWithSystem.context.slice(lastHumanIndexWithSystem + 1)
527
- .filter(msg => msg.getType() === 'ai');
528
- // Verify that at least one assistant message exists in the latest sequence
529
- expect(assistantMessagesAfterLastHumanWithSystem.length).toBeGreaterThan(0);
530
- // Verify that at least one of these assistant messages has a thinking block
531
- const hasThinkingBlockWithSystem = assistantMessagesAfterLastHumanWithSystem.some(msg => {
532
- const content = msg.content;
533
- return Array.isArray(content) && content.some(item => typeof item === 'object' && item.type === 'thinking');
534
- });
535
- expect(hasThinkingBlockWithSystem).toBe(true);
536
- });
537
- it('should look for thinking blocks starting from the most recent messages', () => {
538
- // Create a token counter
539
- const tokenCounter = createTestTokenCounter();
540
- // Create messages with multiple thinking blocks
541
- const olderAssistantMessageWithThinking = new AIMessage({
542
- content: [
543
- {
544
- type: 'thinking',
545
- thinking: 'This is an older thinking block',
546
- },
547
- {
548
- type: 'text',
549
- text: 'Older response with thinking',
550
- },
551
- ],
552
- });
553
- const newerAssistantMessageWithThinking = new AIMessage({
554
- content: [
555
- {
556
- type: 'thinking',
557
- thinking: 'This is a newer thinking block',
558
- },
559
- {
560
- type: 'text',
561
- text: 'Newer response with thinking',
562
- },
563
- ],
564
- });
565
- const messages = [
566
- new SystemMessage('System instruction'),
567
- new HumanMessage('Hello'),
568
- olderAssistantMessageWithThinking,
569
- new HumanMessage('Next message'),
570
- newerAssistantMessageWithThinking,
571
- ];
572
- // Calculate token counts for each message
573
- const indexTokenCountMap = {};
574
- for (let i = 0; i < messages.length; i++) {
575
- indexTokenCountMap[i] = tokenCounter(messages[i]);
576
- }
577
- // Create pruneMessages function with thinking mode enabled
578
- // Set a token limit that will force pruning of the older assistant message
579
- const pruneMessages = createPruneMessages({
580
- maxTokens: 80,
581
- startIndex: 0,
582
- tokenCounter,
583
- indexTokenCountMap: { ...indexTokenCountMap },
584
- thinkingEnabled: true,
585
- });
586
- // Prune messages
587
- const result = pruneMessages({ messages });
588
- // Find the first assistant message in the pruned context
589
- const firstAssistantIndex = result.context.findIndex(msg => msg.getType() === 'ai');
590
- expect(firstAssistantIndex).not.toBe(-1);
591
- const firstAssistantMsg = result.context[firstAssistantIndex];
592
- expect(Array.isArray(firstAssistantMsg.content)).toBe(true);
593
- // Verify that the first assistant message has a thinking block
594
- const thinkingBlock = firstAssistantMsg.content.find(item => typeof item === 'object' && item.type === 'thinking');
595
- expect(thinkingBlock).toBeDefined();
596
- // Verify that it's the newer thinking block
597
- expect(thinkingBlock.thinking).toContain('newer thinking block');
598
- });
599
- });
600
- //# sourceMappingURL=thinking-prune.test.js.map