@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,246 +0,0 @@
1
- // src/specs/token-distribution-edge-case.test.ts
2
- import { HumanMessage, AIMessage, SystemMessage, } from '@langchain/core/messages';
3
- import { createPruneMessages } from '@/messages/prune';
4
- // Create a simple token counter for testing
5
- const createTestTokenCounter = () => {
6
- // This simple token counter just counts characters as tokens for predictable testing
7
- return (message) => {
8
- // Use type assertion to help TypeScript understand the type
9
- const content = message.content;
10
- // Handle string content
11
- if (typeof content === 'string') {
12
- return content.length;
13
- }
14
- // Handle array content
15
- if (Array.isArray(content)) {
16
- let totalLength = 0;
17
- for (const item of content) {
18
- if (typeof item === 'string') {
19
- totalLength += item.length;
20
- }
21
- else if (typeof item === 'object') {
22
- if ('text' in item && typeof item.text === 'string') {
23
- totalLength += item.text.length;
24
- }
25
- }
26
- }
27
- return totalLength;
28
- }
29
- // Default case - if content is null, undefined, or any other type
30
- return 0;
31
- };
32
- };
33
- describe('Token Distribution Edge Case Tests', () => {
34
- it('should only distribute tokens to messages that remain in the context after pruning', () => {
35
- // Create a token counter
36
- const tokenCounter = createTestTokenCounter();
37
- // Create messages
38
- const messages = [
39
- new SystemMessage('System instruction'), // Will always be included
40
- new HumanMessage('Message 1'), // Will be pruned
41
- new AIMessage('Response 1'), // Will be pruned
42
- new HumanMessage('Message 2'), // Will remain
43
- new AIMessage('Response 2'), // Will remain
44
- ];
45
- // Calculate initial token counts for each message
46
- const indexTokenCountMap = {
47
- 0: 17, // "System instruction"
48
- 1: 9, // "Message 1"
49
- 2: 10, // "Response 1"
50
- 3: 9, // "Message 2"
51
- 4: 10, // "Response 2"
52
- };
53
- // Set a token limit that will force pruning of the first two messages after the system message
54
- const pruneMessages = createPruneMessages({
55
- maxTokens: 40, // Only enough for system message + last two messages
56
- startIndex: 0,
57
- tokenCounter,
58
- indexTokenCountMap: { ...indexTokenCountMap },
59
- });
60
- // First call to establish lastCutOffIndex
61
- const initialResult = pruneMessages({ messages });
62
- // Verify initial pruning
63
- expect(initialResult.context.length).toBe(3);
64
- expect(initialResult.context[0].content).toBe('System instruction');
65
- expect(initialResult.context[1].content).toBe('Message 2');
66
- expect(initialResult.context[2].content).toBe('Response 2');
67
- // Now provide usage metadata with a different total token count
68
- const usageMetadata = {
69
- input_tokens: 30,
70
- output_tokens: 20,
71
- total_tokens: 50, // Different from the sum of our initial token counts
72
- };
73
- // Call pruneMessages again with the usage metadata
74
- const result = pruneMessages({
75
- messages,
76
- usageMetadata,
77
- });
78
- // The token distribution should only affect messages that remain in the context
79
- // Messages at indices 0, 3, and 4 should have their token counts adjusted
80
- // Messages at indices 1 and 2 should remain unchanged since they're pruned
81
- // The token distribution should only affect messages that remain in the context
82
- // Messages at indices 0, 3, and 4 should have their token counts adjusted
83
- // Messages at indices 1 and 2 should remain unchanged since they're pruned
84
- // Check that at least one of the pruned messages' token counts was not adjusted
85
- // We're testing the principle that pruned messages don't get token redistribution
86
- const atLeastOnePrunedMessageUnchanged = result.indexTokenCountMap[1] === indexTokenCountMap[1] ||
87
- result.indexTokenCountMap[2] === indexTokenCountMap[2];
88
- expect(atLeastOnePrunedMessageUnchanged).toBe(true);
89
- // Verify that the sum of tokens for messages in the context is close to the total_tokens from usageMetadata
90
- // There might be small rounding differences or implementation details that affect the exact sum
91
- const totalContextTokens = (result.indexTokenCountMap[0] ?? 0) +
92
- (result.indexTokenCountMap[3] ?? 0) +
93
- (result.indexTokenCountMap[4] ?? 0);
94
- expect(totalContextTokens).toBeGreaterThan(0);
95
- // The key thing we're testing is that the token distribution happens for messages in the context
96
- // and that the sum is reasonably close to the expected total
97
- const tokenDifference = Math.abs(totalContextTokens - 50);
98
- expect(tokenDifference).toBeLessThan(20); // Allow for some difference due to implementation details
99
- });
100
- it('should handle the case when all messages fit within the token limit', () => {
101
- // Create a token counter
102
- const tokenCounter = createTestTokenCounter();
103
- // Create messages
104
- const messages = [
105
- new SystemMessage('System instruction'),
106
- new HumanMessage('Message 1'),
107
- new AIMessage('Response 1'),
108
- ];
109
- // Calculate initial token counts for each message
110
- const indexTokenCountMap = {
111
- 0: 17, // "System instruction"
112
- 1: 9, // "Message 1"
113
- 2: 10, // "Response 1"
114
- };
115
- // Set a token limit that will allow all messages to fit
116
- const pruneMessages = createPruneMessages({
117
- maxTokens: 100,
118
- startIndex: 0,
119
- tokenCounter,
120
- indexTokenCountMap: { ...indexTokenCountMap },
121
- });
122
- // First call to establish lastCutOffIndex (should be 0 since no pruning occurs)
123
- const initialResult = pruneMessages({ messages });
124
- // Verify no pruning occurred
125
- expect(initialResult.context.length).toBe(3);
126
- // Now provide usage metadata with a different total token count
127
- const usageMetadata = {
128
- input_tokens: 20,
129
- output_tokens: 10,
130
- total_tokens: 30, // Different from the sum of our initial token counts
131
- };
132
- // Call pruneMessages again with the usage metadata
133
- const result = pruneMessages({
134
- messages,
135
- usageMetadata,
136
- });
137
- // Since all messages fit, all token counts should be adjusted
138
- const initialTotalTokens = indexTokenCountMap[0] + indexTokenCountMap[1] + indexTokenCountMap[2];
139
- const expectedRatio = 30 / initialTotalTokens;
140
- // Check that all token counts were adjusted
141
- expect(result.indexTokenCountMap[0]).toBe(Math.round(indexTokenCountMap[0] * expectedRatio));
142
- expect(result.indexTokenCountMap[1]).toBe(Math.round(indexTokenCountMap[1] * expectedRatio));
143
- expect(result.indexTokenCountMap[2]).toBe(Math.round(indexTokenCountMap[2] * expectedRatio));
144
- // Verify that the sum of all tokens equals the total_tokens from usageMetadata
145
- const totalTokens = (result.indexTokenCountMap[0] ?? 0) +
146
- (result.indexTokenCountMap[1] ?? 0) +
147
- (result.indexTokenCountMap[2] ?? 0);
148
- expect(totalTokens).toBe(30);
149
- });
150
- it('should handle multiple pruning operations with token redistribution', () => {
151
- // Create a token counter
152
- const tokenCounter = createTestTokenCounter();
153
- // Create a longer sequence of messages
154
- const messages = [
155
- new SystemMessage('System instruction'), // Will always be included
156
- new HumanMessage('Message 1'), // Will be pruned in first round
157
- new AIMessage('Response 1'), // Will be pruned in first round
158
- new HumanMessage('Message 2'), // Will be pruned in second round
159
- new AIMessage('Response 2'), // Will be pruned in second round
160
- new HumanMessage('Message 3'), // Will remain
161
- new AIMessage('Response 3'), // Will remain
162
- ];
163
- // Calculate initial token counts for each message
164
- const indexTokenCountMap = {
165
- 0: 17, // "System instruction"
166
- 1: 9, // "Message 1"
167
- 2: 10, // "Response 1"
168
- 3: 9, // "Message 2"
169
- 4: 10, // "Response 2"
170
- 5: 9, // "Message 3"
171
- 6: 10, // "Response 3"
172
- };
173
- // Set a token limit that will force pruning
174
- const pruneMessages = createPruneMessages({
175
- maxTokens: 40, // Only enough for system message + last two messages
176
- startIndex: 0,
177
- tokenCounter,
178
- indexTokenCountMap: { ...indexTokenCountMap },
179
- });
180
- // First pruning operation
181
- const firstResult = pruneMessages({ messages });
182
- // Verify first pruning
183
- expect(firstResult.context.length).toBe(3);
184
- expect(firstResult.context[0].content).toBe('System instruction');
185
- expect(firstResult.context[1].content).toBe('Message 3');
186
- expect(firstResult.context[2].content).toBe('Response 3');
187
- // First usage metadata update
188
- const firstUsageMetadata = {
189
- input_tokens: 30,
190
- output_tokens: 20,
191
- total_tokens: 50,
192
- };
193
- // Apply first usage metadata
194
- const secondResult = pruneMessages({
195
- messages,
196
- usageMetadata: firstUsageMetadata,
197
- });
198
- // Add two more messages
199
- messages.push(new HumanMessage('Message 4'));
200
- const extendedMessages = [...messages, new AIMessage('Response 4')];
201
- // Second usage metadata update
202
- const secondUsageMetadata = {
203
- input_tokens: 30,
204
- output_tokens: 20,
205
- total_tokens: 50,
206
- };
207
- // Apply second usage metadata with extended messages
208
- const thirdResult = pruneMessages({
209
- messages: extendedMessages,
210
- usageMetadata: secondUsageMetadata,
211
- });
212
- // The context should include the system message and some of the latest messages
213
- expect(thirdResult.context.length).toBeGreaterThan(0);
214
- expect(thirdResult.context[0].content).toBe('System instruction');
215
- expect(thirdResult.context[1].content).toBe('Response 4');
216
- // Find which messages are in the final context
217
- const contextMessageIndices = thirdResult.context.map((msg) => {
218
- // Find the index of this message in the original array
219
- return extendedMessages.findIndex((m) => m.content === msg.content);
220
- });
221
- // Get the sum of token counts for messages in the context
222
- let totalContextTokens = 0;
223
- for (const idx of contextMessageIndices) {
224
- totalContextTokens += thirdResult.indexTokenCountMap[idx] ?? 0;
225
- }
226
- // Verify that the sum of tokens for messages in the context is close to the total_tokens from usageMetadata
227
- // There might be small rounding differences or implementation details that affect the exact sum
228
- expect(totalContextTokens).toBeGreaterThan(0);
229
- // The key thing we're testing is that the token distribution happens for messages in the context
230
- // and that the sum is reasonably close to the expected total
231
- const tokenDifference = Math.abs(totalContextTokens - 70);
232
- expect(tokenDifference).toBeLessThan(50); // Allow for some difference due to implementation details
233
- // Verify that messages not in the context have their original token counts or previously adjusted values
234
- for (let i = 0; i < extendedMessages.length; i++) {
235
- if (!contextMessageIndices.includes(i)) {
236
- const expectedValue = i < messages.length
237
- ? (secondResult.indexTokenCountMap[i] ?? 0) || indexTokenCountMap[i]
238
- : (indexTokenCountMap[i] ??
239
- 0);
240
- const difference = Math.abs((thirdResult.indexTokenCountMap[i] ?? 0) - expectedValue);
241
- expect(difference).toBe(0);
242
- }
243
- }
244
- });
245
- });
246
- //# sourceMappingURL=token-distribution-edge-case.test.js.map
@@ -1,32 +0,0 @@
1
- import { HumanMessage } from '@langchain/core/messages';
2
- import { createTokenCounter, TokenEncoderManager } from '@/utils/tokens';
3
- jest.setTimeout(5000);
4
- describe('Token encoder memoization', () => {
5
- beforeEach(() => {
6
- TokenEncoderManager.reset();
7
- });
8
- test('reuses encoder across counters (no external fetch)', async () => {
9
- expect(TokenEncoderManager.isInitialized()).toBe(false);
10
- const counter1 = await createTokenCounter();
11
- expect(TokenEncoderManager.isInitialized()).toBe(true);
12
- const counter2 = await createTokenCounter();
13
- const m1 = new HumanMessage('hello world');
14
- const m2 = new HumanMessage('another short text');
15
- const c11 = counter1(m1);
16
- const c12 = counter1(m2);
17
- const c21 = counter2(m1);
18
- const c22 = counter2(m2);
19
- expect(c11).toBeGreaterThan(0);
20
- expect(c12).toBeGreaterThan(0);
21
- // Same counter instance is reused
22
- expect(c21).toBe(c11);
23
- expect(c22).toBe(c12);
24
- });
25
- test('reset clears cached tokenizers', async () => {
26
- await createTokenCounter();
27
- expect(TokenEncoderManager.isInitialized()).toBe(true);
28
- TokenEncoderManager.reset();
29
- expect(TokenEncoderManager.isInitialized()).toBe(false);
30
- });
31
- });
32
- //# sourceMappingURL=token-memoization.test.js.map
@@ -1,49 +0,0 @@
1
- import { HumanMessage } from '@langchain/core/messages';
2
- import { encodingForModel, createTokenCounter, TokenEncoderManager, } from '@/utils/tokens';
3
- describe('encodingForModel', () => {
4
- test('returns claude for Claude model strings', () => {
5
- expect(encodingForModel('claude-3-5-sonnet-20241022')).toBe('claude');
6
- expect(encodingForModel('claude-3-haiku-20240307')).toBe('claude');
7
- });
8
- test('handles Bedrock Claude ARNs', () => {
9
- expect(encodingForModel('anthropic.claude-3-5-sonnet-20241022-v2:0')).toBe('claude');
10
- });
11
- test('is case-insensitive', () => {
12
- expect(encodingForModel('CLAUDE-3-HAIKU')).toBe('claude');
13
- expect(encodingForModel('Claude-3-Opus')).toBe('claude');
14
- });
15
- test('returns o200k_base for non-Claude models', () => {
16
- expect(encodingForModel('gpt-4o')).toBe('o200k_base');
17
- expect(encodingForModel('gemini-2.0-flash')).toBe('o200k_base');
18
- expect(encodingForModel('mistral-large')).toBe('o200k_base');
19
- });
20
- test('returns o200k_base for empty string', () => {
21
- expect(encodingForModel('')).toBe('o200k_base');
22
- });
23
- });
24
- describe('createTokenCounter with different encodings', () => {
25
- beforeEach(() => {
26
- TokenEncoderManager.reset();
27
- });
28
- test('claude encoding produces valid token counts', async () => {
29
- const counter = await createTokenCounter('claude');
30
- const msg = new HumanMessage('Hello, world!');
31
- const count = counter(msg);
32
- expect(count).toBeGreaterThan(0);
33
- });
34
- test('o200k_base encoding produces valid token counts', async () => {
35
- const counter = await createTokenCounter('o200k_base');
36
- const msg = new HumanMessage('Hello, world!');
37
- const count = counter(msg);
38
- expect(count).toBeGreaterThan(0);
39
- });
40
- test('both encodings can be initialized and used independently', async () => {
41
- const claudeCounter = await createTokenCounter('claude');
42
- const o200kCounter = await createTokenCounter('o200k_base');
43
- expect(TokenEncoderManager.isInitialized()).toBe(true);
44
- const msg = new HumanMessage('Test message for both encodings');
45
- expect(claudeCounter(msg)).toBeGreaterThan(0);
46
- expect(o200kCounter(msg)).toBeGreaterThan(0);
47
- });
48
- });
49
- //# sourceMappingURL=tokens.test.js.map
@@ -1,139 +0,0 @@
1
- import { config } from 'dotenv';
2
- config();
3
- import { tool } from '@langchain/core/tools';
4
- import { HumanMessage } from '@langchain/core/messages';
5
- import { ToolEndHandler, ModelEndHandler } from '@/events';
6
- import { createContentAggregator } from '@/stream';
7
- import { GraphEvents, Providers } from '@/common';
8
- import { getLLMConfig } from '@/utils/llmConfig';
9
- import { getArgs } from '@/scripts/args';
10
- import { StandardGraph } from '@/graphs';
11
- import { Run } from '@/run';
12
- const errorTool = tool(async () => {
13
- throw new Error('this is a test error I threw on purpose');
14
- }, {
15
- name: 'errorTool',
16
- description: 'A tool that always throws an error',
17
- schema: {
18
- type: 'object',
19
- properties: {
20
- input: { type: 'string' },
21
- },
22
- required: [],
23
- },
24
- });
25
- describe('Tool Error Handling Tests', () => {
26
- jest.setTimeout(30000);
27
- let run;
28
- let contentParts;
29
- let conversationHistory;
30
- let aggregateContent;
31
- let handleToolCallErrorSpy;
32
- const config = {
33
- configurable: {
34
- thread_id: 'conversation-num-1',
35
- },
36
- streamMode: 'values',
37
- version: 'v2',
38
- };
39
- beforeEach(async () => {
40
- conversationHistory = [];
41
- const { contentParts: parts, aggregateContent: ac } = createContentAggregator();
42
- aggregateContent = ac;
43
- contentParts = parts;
44
- // Spy on the static method instead of the instance method
45
- handleToolCallErrorSpy = jest.spyOn(StandardGraph, 'handleToolCallErrorStatic');
46
- });
47
- afterEach(() => {
48
- handleToolCallErrorSpy.mockRestore();
49
- });
50
- const onMessageDeltaSpy = jest.fn();
51
- const onRunStepSpy = jest.fn();
52
- const onRunStepCompletedSpy = jest.fn();
53
- afterAll(() => {
54
- onMessageDeltaSpy.mockReset();
55
- onRunStepSpy.mockReset();
56
- onRunStepCompletedSpy.mockReset();
57
- });
58
- const setupCustomHandlers = () => ({
59
- [GraphEvents.TOOL_END]: new ToolEndHandler(),
60
- [GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
61
- [GraphEvents.ON_RUN_STEP_COMPLETED]: {
62
- handle: (event, data) => {
63
- if (data.result['type'] === 'tool_call') {
64
- run.Graph?.overrideTestModel(['Looks like there was an error calling the tool.'], 5);
65
- }
66
- onRunStepCompletedSpy(event, data);
67
- aggregateContent({
68
- event,
69
- data: data,
70
- });
71
- },
72
- },
73
- [GraphEvents.ON_RUN_STEP]: {
74
- handle: (event, data, metadata, graph) => {
75
- const runStepData = data;
76
- onRunStepSpy(event, runStepData, metadata, graph);
77
- aggregateContent({ event, data: runStepData });
78
- },
79
- },
80
- [GraphEvents.ON_RUN_STEP_DELTA]: {
81
- handle: (event, data) => {
82
- aggregateContent({ event, data: data });
83
- },
84
- },
85
- [GraphEvents.ON_MESSAGE_DELTA]: {
86
- handle: (event, data, metadata, graph) => {
87
- onMessageDeltaSpy(event, data, metadata, graph);
88
- aggregateContent({ event, data: data });
89
- },
90
- },
91
- });
92
- test('should handle tool call errors correctly', async () => {
93
- const { userName, location } = await getArgs();
94
- const llmConfig = getLLMConfig(Providers.OPENAI);
95
- const customHandlers = setupCustomHandlers();
96
- // Create the run instance
97
- run = await Run.create({
98
- runId: 'test-run-id',
99
- graphConfig: {
100
- type: 'standard',
101
- llmConfig,
102
- tools: [errorTool],
103
- instructions: 'You are a helpful AI assistant.',
104
- additional_instructions: `The user's name is ${userName} and they are located in ${location}.`,
105
- },
106
- returnContent: true,
107
- skipCleanup: true,
108
- customHandlers,
109
- });
110
- const toolCalls = [
111
- {
112
- name: 'errorTool',
113
- args: {
114
- input: 'test input',
115
- },
116
- id: 'call_test123',
117
- type: 'tool_call',
118
- },
119
- ];
120
- const firstResponse = 'Let me try calling the tool';
121
- run.Graph?.overrideTestModel([firstResponse], 5, toolCalls);
122
- const userMessage = 'Use the error tool';
123
- conversationHistory.push(new HumanMessage(userMessage));
124
- const inputs = {
125
- messages: conversationHistory,
126
- };
127
- await run.processStream(inputs, config);
128
- // Verify handleToolCallError was called
129
- expect(handleToolCallErrorSpy).toHaveBeenCalled();
130
- // Find the tool call content part
131
- const toolCallPart = contentParts.find((part) => part.type === 'tool_call');
132
- // Verify the error message in contentParts
133
- expect(toolCallPart).toBeDefined();
134
- expect(toolCallPart?.tool_call?.args).toEqual(JSON.stringify(toolCalls[0].args));
135
- expect(toolCallPart?.tool_call?.output).toContain('Error processing tool');
136
- expect(toolCallPart?.tool_call?.output).toContain('this is a test error I threw on purpose');
137
- });
138
- });
139
- //# sourceMappingURL=tool-error.test.js.map
@@ -1,204 +0,0 @@
1
- import { nanoid } from 'nanoid';
2
- import { ContentTypes, GraphEvents, StepTypes } from '@/common';
3
- export const SEPARATORS = [
4
- '. ',
5
- '?',
6
- '!',
7
- '۔',
8
- '- ',
9
- '。',
10
- '‥',
11
- ';',
12
- '¡',
13
- '¿',
14
- '\n',
15
- '```',
16
- ];
17
- export class SplitStreamHandler {
18
- inCodeBlock = false;
19
- inThinkBlock = false;
20
- accumulate;
21
- tokens = [];
22
- lastToken = '';
23
- reasoningTokens = [];
24
- currentStepId;
25
- currentMessageId;
26
- currentType;
27
- currentLength = 0;
28
- reasoningKey = 'reasoning_content';
29
- currentIndex = -1;
30
- blockThreshold = 4500;
31
- /** The run ID AKA the Message ID associated with the complete generation */
32
- runId;
33
- handlers;
34
- constructor({ runId, handlers, accumulate, reasoningKey, blockThreshold, }) {
35
- this.runId = runId;
36
- this.handlers = handlers;
37
- if (reasoningKey) {
38
- this.reasoningKey = reasoningKey;
39
- }
40
- if (blockThreshold != null) {
41
- this.blockThreshold = blockThreshold;
42
- }
43
- this.accumulate = accumulate ?? false;
44
- }
45
- getMessageId = () => {
46
- const messageId = this.currentMessageId;
47
- if (messageId != null && messageId) {
48
- return messageId;
49
- }
50
- return undefined;
51
- };
52
- createMessageStep = (type) => {
53
- if (type != null && this.currentType !== type) {
54
- this.currentType = type;
55
- }
56
- this.currentLength = 0;
57
- this.currentIndex += 1;
58
- this.currentStepId = `step_${nanoid()}`;
59
- this.currentMessageId = `msg_${nanoid()}`;
60
- return [this.currentStepId, this.currentMessageId];
61
- };
62
- dispatchRunStep = (stepId, stepDetails) => {
63
- const runStep = {
64
- id: stepId,
65
- runId: this.runId,
66
- type: stepDetails.type,
67
- index: this.currentIndex,
68
- stepDetails,
69
- // usage: null,
70
- };
71
- this.handlers?.[GraphEvents.ON_RUN_STEP]?.({
72
- event: GraphEvents.ON_RUN_STEP,
73
- data: runStep,
74
- });
75
- };
76
- dispatchMessageDelta = (stepId, delta) => {
77
- const messageDelta = {
78
- id: stepId,
79
- delta,
80
- };
81
- this.handlers?.[GraphEvents.ON_MESSAGE_DELTA]?.({
82
- event: GraphEvents.ON_MESSAGE_DELTA,
83
- data: messageDelta,
84
- });
85
- };
86
- dispatchReasoningDelta = (stepId, delta) => {
87
- const reasoningDelta = {
88
- id: stepId,
89
- delta,
90
- };
91
- this.handlers?.[GraphEvents.ON_REASONING_DELTA]?.({
92
- event: GraphEvents.ON_REASONING_DELTA,
93
- data: reasoningDelta,
94
- });
95
- };
96
- handleContent = (content, _type) => {
97
- let type = _type;
98
- if (this.inThinkBlock && type === ContentTypes.TEXT) {
99
- type = ContentTypes.THINK;
100
- }
101
- if (this.accumulate) {
102
- if (type === ContentTypes.THINK) {
103
- this.reasoningTokens.push(content);
104
- }
105
- else {
106
- this.tokens.push(content);
107
- }
108
- }
109
- if (this.currentType !== type) {
110
- const [newStepId, newMessageId] = this.createMessageStep(type);
111
- this.dispatchRunStep(newStepId, {
112
- type: StepTypes.MESSAGE_CREATION,
113
- message_creation: {
114
- message_id: newMessageId,
115
- },
116
- });
117
- }
118
- const stepId = this.currentStepId ?? '';
119
- if (type === ContentTypes.THINK) {
120
- this.dispatchReasoningDelta(stepId, {
121
- content: [
122
- {
123
- type: ContentTypes.THINK,
124
- think: content,
125
- },
126
- ],
127
- });
128
- }
129
- else {
130
- this.dispatchMessageDelta(stepId, {
131
- content: [
132
- {
133
- type: ContentTypes.TEXT,
134
- text: content,
135
- },
136
- ],
137
- });
138
- }
139
- this.currentLength += content.length;
140
- if (this.inCodeBlock) {
141
- return;
142
- }
143
- if (this.currentLength > this.blockThreshold &&
144
- SEPARATORS.some((sep) => content.includes(sep))) {
145
- const [newStepId, newMessageId] = this.createMessageStep(type);
146
- this.dispatchRunStep(newStepId, {
147
- type: StepTypes.MESSAGE_CREATION,
148
- message_creation: {
149
- message_id: newMessageId,
150
- },
151
- });
152
- }
153
- };
154
- getDeltaContent(chunk) {
155
- return chunk?.choices?.[0]?.delta?.content ?? '';
156
- }
157
- getReasoningDelta(chunk) {
158
- return (chunk?.choices?.[0]?.delta?.[this.reasoningKey] ??
159
- '');
160
- }
161
- handle(chunk) {
162
- if (!chunk) {
163
- return;
164
- }
165
- const content = this.getDeltaContent(chunk);
166
- const reasoning_content = this.getReasoningDelta(chunk);
167
- if (!content.length && !reasoning_content.length) {
168
- return;
169
- }
170
- if (content.includes('```')) {
171
- this.inCodeBlock = !this.inCodeBlock;
172
- }
173
- if (content.includes('<think>') && !this.inCodeBlock) {
174
- this.inThinkBlock = true;
175
- }
176
- else if (this.lastToken.includes('</think>') && !this.inCodeBlock) {
177
- this.inThinkBlock = false;
178
- }
179
- this.lastToken = content;
180
- const message_id = this.getMessageId() ?? '';
181
- if (!message_id) {
182
- const initialContentType = this.inThinkBlock
183
- ? ContentTypes.THINK
184
- : ContentTypes.TEXT;
185
- const initialType = reasoning_content
186
- ? ContentTypes.THINK
187
- : initialContentType;
188
- const [stepId, message_id] = this.createMessageStep(initialType);
189
- this.dispatchRunStep(stepId, {
190
- type: StepTypes.MESSAGE_CREATION,
191
- message_creation: {
192
- message_id,
193
- },
194
- });
195
- }
196
- if (reasoning_content) {
197
- this.handleContent(reasoning_content, ContentTypes.THINK);
198
- }
199
- else {
200
- this.handleContent(content, ContentTypes.TEXT);
201
- }
202
- }
203
- }
204
- //# sourceMappingURL=splitStream.js.map