@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,138 +0,0 @@
1
- // src/scripts/image.ts
2
- import { config } from 'dotenv';
3
- config();
4
- import { HumanMessage } from '@langchain/core/messages';
5
- import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
6
- import { ToolEndHandler, ModelEndHandler, createMetadataAggregator, } from '@/events';
7
- // @ts-expect-error — example module not in current codebase
8
- import { fetchRandomImageTool } from '@/tools/example';
9
- import { getLLMConfig } from '@/utils/llmConfig';
10
- import { getArgs } from '@/scripts/args';
11
- import { GraphEvents } from '@/common';
12
- import { Run } from '@/run';
13
- const conversationHistory = [];
14
- async function testCodeExecution() {
15
- const { userName, location, provider, currentDate } = await getArgs();
16
- const { contentParts, aggregateContent } = createContentAggregator();
17
- const customHandlers = {
18
- [GraphEvents.TOOL_END]: new ToolEndHandler(),
19
- [GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
20
- [GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
21
- [GraphEvents.ON_RUN_STEP_COMPLETED]: {
22
- handle: (event, data) => {
23
- console.log('====== ON_RUN_STEP_COMPLETED ======');
24
- console.dir(data, { depth: null });
25
- aggregateContent({
26
- event,
27
- data: data,
28
- });
29
- },
30
- },
31
- [GraphEvents.ON_RUN_STEP]: {
32
- handle: (event, data) => {
33
- console.log('====== ON_RUN_STEP ======');
34
- console.dir(data, { depth: null });
35
- aggregateContent({ event, data: data });
36
- },
37
- },
38
- [GraphEvents.ON_RUN_STEP_DELTA]: {
39
- handle: (event, data) => {
40
- console.log('====== ON_RUN_STEP_DELTA ======');
41
- console.dir(data, { depth: null });
42
- aggregateContent({ event, data: data });
43
- },
44
- },
45
- [GraphEvents.ON_MESSAGE_DELTA]: {
46
- handle: (event, data) => {
47
- console.log('====== ON_MESSAGE_DELTA ======');
48
- console.dir(data, { depth: null });
49
- aggregateContent({ event, data: data });
50
- },
51
- },
52
- [GraphEvents.TOOL_START]: {
53
- handle: (_event, data, metadata) => {
54
- console.log('====== TOOL_START ======');
55
- console.dir(data, { depth: null });
56
- },
57
- },
58
- };
59
- const llmConfig = getLLMConfig(provider);
60
- const run = await Run.create({
61
- runId: 'message-num-1',
62
- graphConfig: {
63
- type: 'standard',
64
- llmConfig,
65
- tools: [fetchRandomImageTool],
66
- // tools: [fetchRandomImageURL],
67
- instructions: 'You are a friendly AI assistant with internet capabilities. Always address the user by their name.',
68
- additional_instructions: `The user's name is ${userName} and they are located in ${location}.`,
69
- },
70
- returnContent: true,
71
- skipCleanup: true,
72
- customHandlers,
73
- });
74
- const config = {
75
- configurable: {
76
- provider,
77
- thread_id: 'conversation-num-1',
78
- },
79
- streamMode: 'values',
80
- version: 'v2',
81
- };
82
- console.log('Fetch Random Image');
83
- const userMessage1 = `Hi ${userName} here. Please get me 2 random images. Describe them after you receive them.`;
84
- conversationHistory.push(new HumanMessage(userMessage1));
85
- let inputs = {
86
- messages: conversationHistory,
87
- };
88
- const finalContentParts1 = await run.processStream(inputs, config);
89
- const finalMessages1 = run.getRunMessages();
90
- if (finalMessages1) {
91
- conversationHistory.push(...finalMessages1);
92
- }
93
- console.log('\n\n====================\n\n');
94
- console.dir(contentParts, { depth: null });
95
- console.log('Test 2: Follow up with another message');
96
- const userMessage2 = `thanks, you're the best!`;
97
- conversationHistory.push(new HumanMessage(userMessage2));
98
- inputs = {
99
- messages: conversationHistory,
100
- };
101
- const finalContentParts2 = await run.processStream(inputs, config, {
102
- keepContent: true,
103
- });
104
- const finalMessages2 = run.getRunMessages();
105
- if (finalMessages2) {
106
- conversationHistory.push(...finalMessages2);
107
- }
108
- console.log('\n\n====================\n\n');
109
- console.dir(contentParts, { depth: null });
110
- const { handleLLMEnd, collected } = createMetadataAggregator();
111
- const titleResult = await run.generateTitle({
112
- provider,
113
- inputText: userMessage2,
114
- contentParts,
115
- chainOptions: {
116
- callbacks: [
117
- {
118
- handleLLMEnd,
119
- },
120
- ],
121
- },
122
- });
123
- console.log('Generated Title:', titleResult);
124
- console.log('Collected metadata:', collected);
125
- }
126
- process.on('unhandledRejection', (reason, promise) => {
127
- console.error('Unhandled Rejection at:', promise, 'reason:', reason);
128
- console.log('Conversation history:');
129
- console.dir(conversationHistory, { depth: null });
130
- process.exit(1);
131
- });
132
- testCodeExecution().catch((err) => {
133
- console.error(err);
134
- console.log('Conversation history:');
135
- console.dir(conversationHistory, { depth: null });
136
- process.exit(1);
137
- });
138
- //# sourceMappingURL=image.js.map
@@ -1,83 +0,0 @@
1
- // src/scripts/cli.ts
2
- import { config } from 'dotenv';
3
- config();
4
- import z from 'zod';
5
- import { tool } from '@langchain/core/tools';
6
- import { HumanMessage } from '@langchain/core/messages';
7
- import { getArgs } from '@/scripts/args';
8
- import { Providers } from '@/common';
9
- import { Run } from '@/run';
10
- const conversationHistory = [];
11
- const memoryKv = {};
12
- const setMemory = tool(async ({ key, value }) => {
13
- if (!/^[a-z_]+$/.test(key)) {
14
- throw new Error('Key must only contain lowercase letters and underscores');
15
- }
16
- memoryKv[key] = value;
17
- return { ok: true };
18
- }, {
19
- name: 'set_memory',
20
- description: 'Saves important data about the user into memory.',
21
- schema: z.object({
22
- key: z
23
- .string()
24
- .describe('The key of the memory value. Always use lowercase and underscores, no other characters.'),
25
- value: z
26
- .string()
27
- .describe('Value can be anything represented as a string'),
28
- }),
29
- });
30
- async function testStandardStreaming() {
31
- const { userName, provider } = await getArgs();
32
- const run = await Run.create({
33
- runId: 'memory-run',
34
- graphConfig: {
35
- type: 'standard',
36
- llmConfig: {
37
- provider: Providers.OPENAI,
38
- model: 'gpt-4o-mini',
39
- temperature: 0.5,
40
- streaming: false,
41
- },
42
- tools: [setMemory],
43
- instructions: 'You can use the `set_memory` tool to save important data about the user into memory. If there is nothing to note about the user specifically, respond with `nothing`.',
44
- toolEnd: true,
45
- },
46
- returnContent: true,
47
- skipCleanup: true,
48
- });
49
- const config = {
50
- configurable: {
51
- provider,
52
- thread_id: 'conversation-num-1',
53
- },
54
- streamMode: 'values',
55
- version: 'v2',
56
- };
57
- console.log('Test 1: Simple message test');
58
- const userMessage = `hi`;
59
- conversationHistory.push(new HumanMessage(userMessage));
60
- const inputs = {
61
- messages: conversationHistory,
62
- };
63
- await run.processStream(inputs, config);
64
- const finalMessages = run.getRunMessages();
65
- if (finalMessages) {
66
- conversationHistory.push(...finalMessages);
67
- console.dir(conversationHistory, { depth: null });
68
- }
69
- console.log('\n\n====================\n\n');
70
- console.dir(memoryKv, { depth: null });
71
- }
72
- process.on('unhandledRejection', (reason, promise) => {
73
- console.error('Unhandled Rejection at:', promise, 'reason:', reason);
74
- console.log('Conversation history:');
75
- process.exit(1);
76
- });
77
- testStandardStreaming().catch((err) => {
78
- console.error(err);
79
- console.log('Conversation history:');
80
- console.dir(conversationHistory, { depth: null });
81
- process.exit(1);
82
- });
83
- //# sourceMappingURL=memory.js.map
@@ -1,271 +0,0 @@
1
- import { config } from 'dotenv';
2
- config();
3
- import { HumanMessage, getBufferString, } from '@langchain/core/messages';
4
- import { Run } from '@/run';
5
- import { Providers, GraphEvents } from '@/common';
6
- import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
7
- import { ToolEndHandler } from '@/events';
8
- /**
9
- * Helper function to create sequential chain edges with buffer string prompts
10
- *
11
- * @param agentIds - Array of agent IDs in order of execution
12
- * @returns Array of edges configured for sequential chain with buffer prompts
13
- */
14
- function createSequentialChainEdges(agentIds) {
15
- const edges = [];
16
- for (let i = 0; i < agentIds.length - 1; i++) {
17
- const fromAgent = agentIds[i];
18
- const toAgent = agentIds[i + 1];
19
- edges.push({
20
- from: fromAgent,
21
- to: toAgent,
22
- edgeType: 'sequence',
23
- // Use a prompt function to create the buffer string from all previous results
24
- prompt: (messages, startIndex) => {
25
- // Get only the messages from this run (after startIndex)
26
- const runMessages = messages.slice(startIndex);
27
- // Create buffer string from run messages
28
- const bufferString = getBufferString(runMessages);
29
- // Format the prompt for the next agent
30
- return `Based on the following conversation and analysis from previous agents, please provide your insights:\n\n${bufferString}\n\nPlease add your specific expertise and perspective to this discussion.`;
31
- },
32
- // Critical: exclude previous results so only the prompt is passed
33
- excludeResults: true,
34
- description: `Sequential chain from ${fromAgent} to ${toAgent}`,
35
- });
36
- }
37
- return edges;
38
- }
39
- const conversationHistory = [];
40
- /**
41
- * Example of sequential agent chain mimicking the old chain behavior
42
- *
43
- * Graph structure:
44
- * START -> researcher -> analyst -> reviewer -> summarizer -> END
45
- *
46
- * Each agent receives a buffer string of all previous results
47
- */
48
- async function testSequentialAgentChain() {
49
- console.log('Testing Sequential Agent Chain (Old Chain Pattern)...\n');
50
- // Set up content aggregator
51
- const { contentParts, aggregateContent } = createContentAggregator();
52
- // Define four agents with specific roles
53
- const agents = [
54
- {
55
- agentId: 'researcher',
56
- provider: Providers.ANTHROPIC,
57
- clientOptions: {
58
- modelName: 'claude-haiku-4-5',
59
- apiKey: process.env.ANTHROPIC_API_KEY,
60
- },
61
- instructions: `You are a Research Agent specializing in gathering initial information.
62
- Your role is to:
63
- 1. Identify key aspects of the user's query
64
- 2. List important factors to consider
65
- 3. Provide initial research findings
66
-
67
- Format your response with clear sections and bullet points.
68
- Start with "RESEARCH FINDINGS:" and be thorough but concise.`,
69
- maxContextTokens: 8000,
70
- },
71
- {
72
- agentId: 'analyst',
73
- provider: Providers.ANTHROPIC,
74
- clientOptions: {
75
- modelName: 'claude-haiku-4-5',
76
- apiKey: process.env.ANTHROPIC_API_KEY,
77
- },
78
- instructions: `You are an Analysis Agent that builds upon research findings.
79
- Your role is to:
80
- 1. Analyze the research provided by the previous agent
81
- 2. Identify patterns, risks, and opportunities
82
- 3. Provide deeper analytical insights
83
-
84
- Start with "ANALYSIS:" and structure your response with clear categories.
85
- Reference specific points from the research when relevant.`,
86
- maxContextTokens: 8000,
87
- },
88
- {
89
- agentId: 'reviewer',
90
- provider: Providers.ANTHROPIC,
91
- clientOptions: {
92
- modelName: 'claude-haiku-4-5',
93
- apiKey: process.env.ANTHROPIC_API_KEY,
94
- },
95
- instructions: `You are a Critical Review Agent that evaluates the work done so far.
96
- Your role is to:
97
- 1. Review the research and analysis from previous agents
98
- 2. Identify any gaps or areas that need more attention
99
- 3. Suggest improvements or additional considerations
100
-
101
- Start with "CRITICAL REVIEW:" and be constructive in your feedback.
102
- Highlight both strengths and areas for improvement.`,
103
- maxContextTokens: 8000,
104
- },
105
- {
106
- agentId: 'summarizer',
107
- provider: Providers.ANTHROPIC,
108
- clientOptions: {
109
- modelName: 'claude-haiku-4-5',
110
- apiKey: process.env.ANTHROPIC_API_KEY,
111
- },
112
- instructions: `You are a Summary Agent that creates the final comprehensive output.
113
- Your role is to:
114
- 1. Synthesize all insights from the researcher, analyst, and reviewer
115
- 2. Create a cohesive, actionable summary
116
- 3. Provide clear recommendations or conclusions
117
-
118
- Start with "EXECUTIVE SUMMARY:" followed by key sections.
119
- End with "KEY RECOMMENDATIONS:" or "CONCLUSIONS:" as appropriate.`,
120
- maxContextTokens: 8000,
121
- },
122
- ];
123
- // Create sequential chain edges using our helper function
124
- const agentIds = agents.map((a) => a.agentId);
125
- const edges = createSequentialChainEdges(agentIds);
126
- // Track agent progression
127
- let currentAgent = '';
128
- const startTime = Date.now();
129
- let messageCount = 0;
130
- // Create custom handlers with extensive metadata logging
131
- const customHandlers = {
132
- [GraphEvents.TOOL_END]: new ToolEndHandler(),
133
- [GraphEvents.CHAT_MODEL_END]: {
134
- handle: (_event, _data, metadata) => {
135
- console.log('\n====== CHAT_MODEL_END METADATA ======');
136
- console.dir(metadata, { depth: null });
137
- const elapsed = Date.now() - startTime;
138
- console.log(`⏱️ COMPLETED at ${elapsed}ms`);
139
- },
140
- },
141
- [GraphEvents.CHAT_MODEL_START]: {
142
- handle: (_event, _data, metadata) => {
143
- console.log('\n====== CHAT_MODEL_START METADATA ======');
144
- console.dir(metadata, { depth: null });
145
- const elapsed = Date.now() - startTime;
146
- console.log(`⏱️ STARTED at ${elapsed}ms`);
147
- },
148
- },
149
- [GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
150
- [GraphEvents.ON_RUN_STEP]: {
151
- handle: (event, data, metadata) => {
152
- const runStepData = data;
153
- console.log('\n====== ON_RUN_STEP ======');
154
- console.log('DATA:');
155
- console.dir(data, { depth: null });
156
- console.log('METADATA:');
157
- console.dir(metadata, { depth: null });
158
- if (runStepData?.name) {
159
- currentAgent = runStepData.name;
160
- console.log(`\n→ ${currentAgent} is processing...`);
161
- }
162
- aggregateContent({ event, data: data });
163
- },
164
- },
165
- [GraphEvents.ON_RUN_STEP_COMPLETED]: {
166
- handle: (event, data, metadata) => {
167
- const runStepData = data;
168
- console.log('\n====== ON_RUN_STEP_COMPLETED ======');
169
- console.log('DATA:');
170
- console.dir(data, { depth: null });
171
- console.log('METADATA:');
172
- console.dir(metadata, { depth: null });
173
- if (runStepData?.name) {
174
- console.log(`✓ ${runStepData.name} completed`);
175
- }
176
- aggregateContent({
177
- event,
178
- data: data,
179
- });
180
- },
181
- },
182
- [GraphEvents.ON_RUN_STEP_DELTA]: {
183
- handle: (event, data, metadata) => {
184
- console.log('\n====== ON_RUN_STEP_DELTA ======');
185
- console.log('DATA:');
186
- console.dir(data, { depth: null });
187
- console.log('METADATA:');
188
- console.dir(metadata, { depth: null });
189
- aggregateContent({ event, data: data });
190
- },
191
- },
192
- [GraphEvents.ON_MESSAGE_DELTA]: {
193
- handle: (event, data, metadata) => {
194
- messageCount++;
195
- // Only log first few message deltas to avoid spam
196
- if (messageCount <= 3) {
197
- console.log('\n====== ON_MESSAGE_DELTA ======');
198
- console.log('DATA:');
199
- console.dir(data, { depth: null });
200
- console.log('METADATA:');
201
- console.dir(metadata, { depth: null });
202
- }
203
- aggregateContent({ event, data: data });
204
- },
205
- },
206
- };
207
- // Create multi-agent run configuration
208
- const runConfig = {
209
- runId: `sequential-chain-${Date.now()}`,
210
- graphConfig: {
211
- type: 'multi-agent',
212
- agents,
213
- edges,
214
- },
215
- customHandlers,
216
- returnContent: true,
217
- skipCleanup: true,
218
- };
219
- try {
220
- // Create and execute the run
221
- const run = await Run.create(runConfig);
222
- // Test with a complex question that benefits from multiple perspectives
223
- const userMessage = 'I want to launch a new mobile app for personal finance management. What should I consider?';
224
- conversationHistory.push(new HumanMessage(userMessage));
225
- console.log(`User: "${userMessage}"\n`);
226
- console.log('Starting sequential agent chain...\n');
227
- const config = {
228
- configurable: {
229
- thread_id: 'sequential-chain-1',
230
- },
231
- streamMode: 'values',
232
- version: 'v2',
233
- };
234
- // Process with streaming
235
- const inputs = {
236
- messages: conversationHistory,
237
- };
238
- const finalContentParts = await run.processStream(inputs, config);
239
- const finalMessages = run.getRunMessages();
240
- if (finalMessages) {
241
- conversationHistory.push(...finalMessages);
242
- }
243
- console.log('\n\n=== Final Output ===');
244
- console.log('Sequential chain completed successfully!');
245
- console.log(`Total content parts: ${contentParts.length}`);
246
- // Display the buffer accumulation
247
- console.log('\n=== Agent Outputs (Buffer Accumulation) ===');
248
- const aiMessages = conversationHistory.filter((msg) => msg._getType() === 'ai');
249
- aiMessages.forEach((msg, index) => {
250
- console.log(`\n--- Agent ${index + 1}: ${agentIds[index]} ---`);
251
- console.log(msg.content);
252
- // Show buffer preview for next agent (except last)
253
- if (index < aiMessages.length - 1) {
254
- const bufferSoFar = getBufferString(aiMessages.slice(0, index + 1));
255
- console.log(`\n[Buffer passed to ${agentIds[index + 1]}]: ${bufferSoFar.slice(0, 150)}...`);
256
- }
257
- });
258
- // Demonstrate that each agent built upon previous results
259
- console.log('\n=== Chain Analysis ===');
260
- console.log('1. Researcher provided initial findings');
261
- console.log('2. Analyst built upon research with deeper insights');
262
- console.log('3. Reviewer critiqued and identified gaps');
263
- console.log('4. Summarizer synthesized everything into actionable output');
264
- }
265
- catch (error) {
266
- console.error('Error in sequential agent chain test:', error);
267
- }
268
- }
269
- // Run the test
270
- testSequentialAgentChain();
271
- //# sourceMappingURL=multi-agent-chain.js.map
@@ -1,185 +0,0 @@
1
- import { config } from 'dotenv';
2
- config();
3
- import { HumanMessage } from '@langchain/core/messages';
4
- import { Run } from '@/run';
5
- import { Providers, GraphEvents } from '@/common';
6
- import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
7
- import { ToolEndHandler, ModelEndHandler } from '@/events';
8
- const conversationHistory = [];
9
- /**
10
- * Example of conditional multi-agent system
11
- *
12
- * Graph structure:
13
- * START -> classifier
14
- * classifier -> technical_expert (if technical question)
15
- * classifier -> business_expert (if business question)
16
- * classifier -> general_assistant (otherwise)
17
- * [all experts] -> END
18
- */
19
- async function testConditionalMultiAgent() {
20
- console.log('Testing Conditional Multi-Agent System...\n');
21
- // Set up content aggregator
22
- const { contentParts, aggregateContent } = createContentAggregator();
23
- // Define specialized agents
24
- const agents = [
25
- {
26
- agentId: 'classifier',
27
- provider: Providers.ANTHROPIC,
28
- clientOptions: {
29
- modelName: 'claude-haiku-4-5',
30
- apiKey: process.env.ANTHROPIC_API_KEY,
31
- },
32
- instructions: 'You are a query classifier. Analyze user questions and determine if they are technical, business-related, or general.',
33
- maxContextTokens: 8000,
34
- },
35
- {
36
- agentId: 'technical_expert',
37
- provider: Providers.ANTHROPIC,
38
- clientOptions: {
39
- modelName: 'claude-haiku-4-5',
40
- apiKey: process.env.ANTHROPIC_API_KEY,
41
- },
42
- instructions: 'You are a technical expert. Provide detailed technical answers about programming, systems, and technology.',
43
- maxContextTokens: 8000,
44
- },
45
- {
46
- agentId: 'business_expert',
47
- provider: Providers.ANTHROPIC,
48
- clientOptions: {
49
- modelName: 'claude-haiku-4-5',
50
- apiKey: process.env.ANTHROPIC_API_KEY,
51
- },
52
- instructions: 'You are a business expert. Provide insights on business strategy, operations, and management.',
53
- maxContextTokens: 8000,
54
- },
55
- {
56
- agentId: 'general_assistant',
57
- provider: Providers.ANTHROPIC,
58
- clientOptions: {
59
- modelName: 'claude-haiku-4-5',
60
- apiKey: process.env.ANTHROPIC_API_KEY,
61
- },
62
- instructions: 'You are a helpful general assistant. Answer questions on a wide range of topics.',
63
- maxContextTokens: 8000,
64
- },
65
- ];
66
- // Define conditional edges
67
- // These create handoff tools with conditional routing logic
68
- const edges = [
69
- {
70
- from: 'classifier',
71
- to: ['technical_expert', 'business_expert', 'general_assistant'],
72
- description: 'Route to appropriate expert based on query type',
73
- condition: (state) => {
74
- // Simple keyword-based routing for demo
75
- // In a real system, this would use the classifier's analysis
76
- const lastMessage = state.messages[state.messages.length - 1];
77
- const content = lastMessage.content?.toString().toLowerCase() || '';
78
- if (content.includes('code') ||
79
- content.includes('programming') ||
80
- content.includes('technical')) {
81
- return 'technical_expert';
82
- }
83
- else if (content.includes('business') ||
84
- content.includes('strategy') ||
85
- content.includes('market')) {
86
- return 'business_expert';
87
- }
88
- else {
89
- return 'general_assistant';
90
- }
91
- },
92
- },
93
- ];
94
- // Track selected expert
95
- let selectedExpert = '';
96
- // Create custom handlers
97
- const customHandlers = {
98
- [GraphEvents.TOOL_END]: new ToolEndHandler(),
99
- [GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
100
- [GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
101
- [GraphEvents.ON_RUN_STEP_COMPLETED]: {
102
- handle: (event, data) => {
103
- aggregateContent({
104
- event,
105
- data: data,
106
- });
107
- },
108
- },
109
- [GraphEvents.ON_RUN_STEP]: {
110
- handle: (event, data) => {
111
- const runStepData = data;
112
- if (runStepData?.name && runStepData.name !== 'classifier') {
113
- selectedExpert = runStepData.name;
114
- console.log(`Routing to: ${selectedExpert}`);
115
- }
116
- aggregateContent({ event, data: data });
117
- },
118
- },
119
- [GraphEvents.ON_RUN_STEP_DELTA]: {
120
- handle: (event, data) => {
121
- aggregateContent({ event, data: data });
122
- },
123
- },
124
- [GraphEvents.ON_MESSAGE_DELTA]: {
125
- handle: (event, data) => {
126
- aggregateContent({ event, data: data });
127
- },
128
- },
129
- };
130
- // Create multi-agent run configuration
131
- const runConfig = {
132
- runId: `conditional-multi-agent-${Date.now()}`,
133
- graphConfig: {
134
- type: 'multi-agent',
135
- agents,
136
- edges,
137
- },
138
- customHandlers,
139
- returnContent: true,
140
- skipCleanup: true,
141
- };
142
- try {
143
- // Create and execute the run
144
- const run = await Run.create(runConfig);
145
- // Test with different types of questions
146
- const testQuestions = [
147
- 'How do I implement a binary search tree in Python?',
148
- 'What are the key strategies for market expansion?',
149
- 'What is the capital of France?',
150
- ];
151
- const config = {
152
- configurable: {
153
- thread_id: 'conditional-conversation-1',
154
- },
155
- streamMode: 'values',
156
- version: 'v2',
157
- };
158
- for (const question of testQuestions) {
159
- console.log(`\n--- Processing: "${question}" ---\n`);
160
- // Reset for each question
161
- selectedExpert = '';
162
- conversationHistory.length = 0;
163
- conversationHistory.push(new HumanMessage(question));
164
- // Process with streaming
165
- const inputs = {
166
- messages: conversationHistory,
167
- };
168
- const finalContentParts = await run.processStream(inputs, config);
169
- const finalMessages = run.getRunMessages();
170
- if (finalMessages) {
171
- conversationHistory.push(...finalMessages);
172
- }
173
- console.log(`\n\nExpert used: ${selectedExpert}`);
174
- console.log('Content parts:', contentParts.length);
175
- console.log('---');
176
- console.dir(contentParts, { depth: null });
177
- }
178
- }
179
- catch (error) {
180
- console.error('Error in conditional multi-agent test:', error);
181
- }
182
- }
183
- // Run the test
184
- testConditionalMultiAgent();
185
- //# sourceMappingURL=multi-agent-conditional.js.map