illuma-agents 1.0.2 → 1.0.4

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 (225) hide show
  1. package/LICENSE +25 -21
  2. package/dist/cjs/agents/AgentContext.cjs +222 -0
  3. package/dist/cjs/agents/AgentContext.cjs.map +1 -0
  4. package/dist/cjs/common/enum.cjs +5 -4
  5. package/dist/cjs/common/enum.cjs.map +1 -1
  6. package/dist/cjs/events.cjs +7 -5
  7. package/dist/cjs/events.cjs.map +1 -1
  8. package/dist/cjs/graphs/Graph.cjs +328 -207
  9. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  10. package/dist/cjs/graphs/MultiAgentGraph.cjs +507 -0
  11. package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -0
  12. package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
  13. package/dist/cjs/llm/google/index.cjs.map +1 -1
  14. package/dist/cjs/llm/ollama/index.cjs.map +1 -1
  15. package/dist/cjs/llm/openai/index.cjs +35 -0
  16. package/dist/cjs/llm/openai/index.cjs.map +1 -1
  17. package/dist/cjs/llm/openai/utils/index.cjs +3 -1
  18. package/dist/cjs/llm/openai/utils/index.cjs.map +1 -1
  19. package/dist/cjs/llm/openrouter/index.cjs.map +1 -1
  20. package/dist/cjs/llm/providers.cjs +0 -2
  21. package/dist/cjs/llm/providers.cjs.map +1 -1
  22. package/dist/cjs/llm/vertexai/index.cjs.map +1 -1
  23. package/dist/cjs/main.cjs +12 -1
  24. package/dist/cjs/main.cjs.map +1 -1
  25. package/dist/cjs/messages/cache.cjs +123 -0
  26. package/dist/cjs/messages/cache.cjs.map +1 -0
  27. package/dist/cjs/messages/content.cjs +53 -0
  28. package/dist/cjs/messages/content.cjs.map +1 -0
  29. package/dist/cjs/messages/format.cjs +17 -29
  30. package/dist/cjs/messages/format.cjs.map +1 -1
  31. package/dist/cjs/run.cjs +119 -74
  32. package/dist/cjs/run.cjs.map +1 -1
  33. package/dist/cjs/stream.cjs +77 -73
  34. package/dist/cjs/stream.cjs.map +1 -1
  35. package/dist/cjs/tools/Calculator.cjs +45 -0
  36. package/dist/cjs/tools/Calculator.cjs.map +1 -0
  37. package/dist/cjs/tools/CodeExecutor.cjs +22 -22
  38. package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
  39. package/dist/cjs/tools/ToolNode.cjs +5 -3
  40. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  41. package/dist/cjs/tools/handlers.cjs +20 -20
  42. package/dist/cjs/tools/handlers.cjs.map +1 -1
  43. package/dist/cjs/utils/events.cjs +31 -0
  44. package/dist/cjs/utils/events.cjs.map +1 -0
  45. package/dist/cjs/utils/handlers.cjs +70 -0
  46. package/dist/cjs/utils/handlers.cjs.map +1 -0
  47. package/dist/cjs/utils/tokens.cjs +54 -7
  48. package/dist/cjs/utils/tokens.cjs.map +1 -1
  49. package/dist/esm/agents/AgentContext.mjs +220 -0
  50. package/dist/esm/agents/AgentContext.mjs.map +1 -0
  51. package/dist/esm/common/enum.mjs +5 -4
  52. package/dist/esm/common/enum.mjs.map +1 -1
  53. package/dist/esm/events.mjs +7 -5
  54. package/dist/esm/events.mjs.map +1 -1
  55. package/dist/esm/graphs/Graph.mjs +330 -209
  56. package/dist/esm/graphs/Graph.mjs.map +1 -1
  57. package/dist/esm/graphs/MultiAgentGraph.mjs +505 -0
  58. package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -0
  59. package/dist/esm/llm/anthropic/index.mjs.map +1 -1
  60. package/dist/esm/llm/google/index.mjs.map +1 -1
  61. package/dist/esm/llm/ollama/index.mjs.map +1 -1
  62. package/dist/esm/llm/openai/index.mjs +35 -0
  63. package/dist/esm/llm/openai/index.mjs.map +1 -1
  64. package/dist/esm/llm/openai/utils/index.mjs +3 -1
  65. package/dist/esm/llm/openai/utils/index.mjs.map +1 -1
  66. package/dist/esm/llm/openrouter/index.mjs.map +1 -1
  67. package/dist/esm/llm/providers.mjs +0 -2
  68. package/dist/esm/llm/providers.mjs.map +1 -1
  69. package/dist/esm/llm/vertexai/index.mjs.map +1 -1
  70. package/dist/esm/main.mjs +7 -2
  71. package/dist/esm/main.mjs.map +1 -1
  72. package/dist/esm/messages/cache.mjs +120 -0
  73. package/dist/esm/messages/cache.mjs.map +1 -0
  74. package/dist/esm/messages/content.mjs +51 -0
  75. package/dist/esm/messages/content.mjs.map +1 -0
  76. package/dist/esm/messages/format.mjs +18 -29
  77. package/dist/esm/messages/format.mjs.map +1 -1
  78. package/dist/esm/run.mjs +119 -74
  79. package/dist/esm/run.mjs.map +1 -1
  80. package/dist/esm/stream.mjs +77 -73
  81. package/dist/esm/stream.mjs.map +1 -1
  82. package/dist/esm/tools/Calculator.mjs +24 -0
  83. package/dist/esm/tools/Calculator.mjs.map +1 -0
  84. package/dist/esm/tools/CodeExecutor.mjs +22 -22
  85. package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
  86. package/dist/esm/tools/ToolNode.mjs +5 -3
  87. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  88. package/dist/esm/tools/handlers.mjs +20 -20
  89. package/dist/esm/tools/handlers.mjs.map +1 -1
  90. package/dist/esm/utils/events.mjs +29 -0
  91. package/dist/esm/utils/events.mjs.map +1 -0
  92. package/dist/esm/utils/handlers.mjs +68 -0
  93. package/dist/esm/utils/handlers.mjs.map +1 -0
  94. package/dist/esm/utils/tokens.mjs +54 -8
  95. package/dist/esm/utils/tokens.mjs.map +1 -1
  96. package/dist/types/agents/AgentContext.d.ts +94 -0
  97. package/dist/types/common/enum.d.ts +7 -5
  98. package/dist/types/events.d.ts +3 -3
  99. package/dist/types/graphs/Graph.d.ts +60 -66
  100. package/dist/types/graphs/MultiAgentGraph.d.ts +47 -0
  101. package/dist/types/graphs/index.d.ts +1 -0
  102. package/dist/types/index.d.ts +1 -0
  103. package/dist/types/llm/openai/index.d.ts +10 -0
  104. package/dist/types/messages/cache.d.ts +20 -0
  105. package/dist/types/messages/content.d.ts +7 -0
  106. package/dist/types/messages/format.d.ts +1 -7
  107. package/dist/types/messages/index.d.ts +2 -0
  108. package/dist/types/messages/reducer.d.ts +9 -0
  109. package/dist/types/run.d.ts +16 -10
  110. package/dist/types/stream.d.ts +4 -3
  111. package/dist/types/tools/Calculator.d.ts +8 -0
  112. package/dist/types/tools/ToolNode.d.ts +1 -1
  113. package/dist/types/tools/handlers.d.ts +9 -7
  114. package/dist/types/tools/search/tool.d.ts +4 -4
  115. package/dist/types/types/graph.d.ts +124 -11
  116. package/dist/types/types/llm.d.ts +13 -9
  117. package/dist/types/types/messages.d.ts +4 -0
  118. package/dist/types/types/run.d.ts +46 -8
  119. package/dist/types/types/stream.d.ts +3 -2
  120. package/dist/types/utils/events.d.ts +6 -0
  121. package/dist/types/utils/handlers.d.ts +34 -0
  122. package/dist/types/utils/index.d.ts +1 -0
  123. package/dist/types/utils/tokens.d.ts +24 -0
  124. package/package.json +162 -145
  125. package/src/agents/AgentContext.ts +323 -0
  126. package/src/common/enum.ts +177 -176
  127. package/src/events.ts +197 -191
  128. package/src/graphs/Graph.ts +1058 -846
  129. package/src/graphs/MultiAgentGraph.ts +598 -0
  130. package/src/graphs/index.ts +2 -1
  131. package/src/index.ts +25 -24
  132. package/src/llm/anthropic/index.ts +413 -413
  133. package/src/llm/google/index.ts +222 -222
  134. package/src/llm/google/utils/zod_to_genai_parameters.ts +86 -88
  135. package/src/llm/ollama/index.ts +92 -92
  136. package/src/llm/openai/index.ts +894 -853
  137. package/src/llm/openai/utils/index.ts +920 -918
  138. package/src/llm/openrouter/index.ts +60 -60
  139. package/src/llm/providers.ts +55 -57
  140. package/src/llm/vertexai/index.ts +360 -360
  141. package/src/messages/cache.test.ts +461 -0
  142. package/src/messages/cache.ts +151 -0
  143. package/src/messages/content.test.ts +362 -0
  144. package/src/messages/content.ts +63 -0
  145. package/src/messages/format.ts +611 -625
  146. package/src/messages/formatAgentMessages.test.ts +1144 -917
  147. package/src/messages/index.ts +6 -4
  148. package/src/messages/reducer.ts +80 -0
  149. package/src/run.ts +447 -381
  150. package/src/scripts/abort.ts +157 -138
  151. package/src/scripts/ant_web_search.ts +158 -158
  152. package/src/scripts/cli.ts +172 -167
  153. package/src/scripts/cli2.ts +133 -125
  154. package/src/scripts/cli3.ts +184 -178
  155. package/src/scripts/cli4.ts +191 -184
  156. package/src/scripts/cli5.ts +191 -184
  157. package/src/scripts/code_exec.ts +213 -214
  158. package/src/scripts/code_exec_simple.ts +147 -129
  159. package/src/scripts/content.ts +138 -120
  160. package/src/scripts/handoff-test.ts +135 -0
  161. package/src/scripts/multi-agent-chain.ts +278 -0
  162. package/src/scripts/multi-agent-conditional.ts +220 -0
  163. package/src/scripts/multi-agent-document-review-chain.ts +197 -0
  164. package/src/scripts/multi-agent-hybrid-flow.ts +310 -0
  165. package/src/scripts/multi-agent-parallel.ts +343 -0
  166. package/src/scripts/multi-agent-sequence.ts +212 -0
  167. package/src/scripts/multi-agent-supervisor.ts +364 -0
  168. package/src/scripts/multi-agent-test.ts +186 -0
  169. package/src/scripts/search.ts +146 -150
  170. package/src/scripts/simple.ts +225 -225
  171. package/src/scripts/stream.ts +140 -122
  172. package/src/scripts/test-custom-prompt-key.ts +145 -0
  173. package/src/scripts/test-handoff-input.ts +170 -0
  174. package/src/scripts/test-multi-agent-list-handoff.ts +261 -0
  175. package/src/scripts/test-tools-before-handoff.ts +222 -0
  176. package/src/scripts/tools.ts +153 -155
  177. package/src/specs/agent-handoffs.test.ts +889 -0
  178. package/src/specs/anthropic.simple.test.ts +320 -317
  179. package/src/specs/azure.simple.test.ts +325 -316
  180. package/src/specs/openai.simple.test.ts +311 -316
  181. package/src/specs/openrouter.simple.test.ts +107 -0
  182. package/src/specs/prune.test.ts +758 -763
  183. package/src/specs/reasoning.test.ts +201 -165
  184. package/src/specs/thinking-prune.test.ts +769 -703
  185. package/src/specs/token-memoization.test.ts +39 -0
  186. package/src/stream.ts +664 -651
  187. package/src/tools/Calculator.test.ts +278 -0
  188. package/src/tools/Calculator.ts +25 -0
  189. package/src/tools/CodeExecutor.ts +220 -220
  190. package/src/tools/ToolNode.ts +170 -170
  191. package/src/tools/handlers.ts +341 -336
  192. package/src/types/graph.ts +372 -185
  193. package/src/types/llm.ts +141 -140
  194. package/src/types/messages.ts +4 -0
  195. package/src/types/run.ts +128 -89
  196. package/src/types/stream.ts +401 -400
  197. package/src/utils/events.ts +32 -0
  198. package/src/utils/handlers.ts +107 -0
  199. package/src/utils/index.ts +6 -5
  200. package/src/utils/llmConfig.ts +183 -183
  201. package/src/utils/tokens.ts +129 -70
  202. package/dist/types/scripts/abort.d.ts +0 -1
  203. package/dist/types/scripts/ant_web_search.d.ts +0 -1
  204. package/dist/types/scripts/args.d.ts +0 -7
  205. package/dist/types/scripts/caching.d.ts +0 -1
  206. package/dist/types/scripts/cli.d.ts +0 -1
  207. package/dist/types/scripts/cli2.d.ts +0 -1
  208. package/dist/types/scripts/cli3.d.ts +0 -1
  209. package/dist/types/scripts/cli4.d.ts +0 -1
  210. package/dist/types/scripts/cli5.d.ts +0 -1
  211. package/dist/types/scripts/code_exec.d.ts +0 -1
  212. package/dist/types/scripts/code_exec_files.d.ts +0 -1
  213. package/dist/types/scripts/code_exec_simple.d.ts +0 -1
  214. package/dist/types/scripts/content.d.ts +0 -1
  215. package/dist/types/scripts/empty_input.d.ts +0 -1
  216. package/dist/types/scripts/image.d.ts +0 -1
  217. package/dist/types/scripts/memory.d.ts +0 -1
  218. package/dist/types/scripts/search.d.ts +0 -1
  219. package/dist/types/scripts/simple.d.ts +0 -1
  220. package/dist/types/scripts/stream.d.ts +0 -1
  221. package/dist/types/scripts/thinking.d.ts +0 -1
  222. package/dist/types/scripts/tools.d.ts +0 -1
  223. package/dist/types/specs/spec.utils.d.ts +0 -1
  224. package/dist/types/tools/example.d.ts +0 -78
  225. package/src/tools/example.ts +0 -129
@@ -1,122 +1,140 @@
1
- /* eslint-disable no-console */
2
- // src/scripts/cli.ts
3
- import { config } from 'dotenv';
4
- config();
5
- import { HumanMessage, BaseMessage } from '@langchain/core/messages';
6
- import { TavilySearchResults } from '@langchain/community/tools/tavily_search';
7
- import type * as t from '@/types';
8
- import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
9
- import { ToolEndHandler } from '@/events';
10
-
11
-
12
- import { getArgs } from '@/scripts/args';
13
- import { Run } from '@/run';
14
- import { GraphEvents, Callback } from '@/common';
15
- import { getLLMConfig } from '@/utils/llmConfig';
16
-
17
- const conversationHistory: BaseMessage[] = [];
18
- async function testStandardStreaming(): Promise<void> {
19
- const { userName, location, provider, currentDate } = await getArgs();
20
- const { contentParts, aggregateContent } = createContentAggregator();
21
- const customHandlers = {
22
- [GraphEvents.TOOL_END]: new ToolEndHandler(),
23
- // [GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
24
- [GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
25
- [GraphEvents.ON_RUN_STEP_COMPLETED]: {
26
- handle: (event: GraphEvents.ON_RUN_STEP_COMPLETED, data: t.StreamEventData): void => {
27
- console.log('====== ON_RUN_STEP_COMPLETED ======');
28
- // console.dir(data, { depth: null });
29
- aggregateContent({ event, data: data as unknown as { result: t.ToolEndEvent } });
30
- }
31
- },
32
- [GraphEvents.ON_RUN_STEP]: {
33
- handle: (event: GraphEvents.ON_RUN_STEP, data: t.StreamEventData): void => {
34
- console.log('====== ON_RUN_STEP ======');
35
- console.dir(data, { depth: null });
36
- aggregateContent({ event, data: data as t.RunStep });
37
- }
38
- },
39
- [GraphEvents.ON_RUN_STEP_DELTA]: {
40
- handle: (event: GraphEvents.ON_RUN_STEP_DELTA, data: t.StreamEventData): void => {
41
- console.log('====== ON_RUN_STEP_DELTA ======');
42
- console.dir(data, { depth: null });
43
- aggregateContent({ event, data: data as t.RunStepDeltaEvent });
44
- }
45
- },
46
- [GraphEvents.ON_MESSAGE_DELTA]: {
47
- handle: (event: GraphEvents.ON_MESSAGE_DELTA, data: t.StreamEventData): void => {
48
- console.log('====== ON_MESSAGE_DELTA ======');
49
- console.dir(data, { depth: null });
50
- aggregateContent({ event, data: data as t.MessageDeltaEvent });
51
- }
52
- },
53
- [GraphEvents.TOOL_START]: {
54
- handle: (_event: string, data: t.StreamEventData, metadata?: Record<string, unknown>): void => {
55
- console.log('====== TOOL_START ======');
56
- // console.dir(data, { depth: null });
57
- }
58
- },
59
- };
60
-
61
- const llmConfig = getLLMConfig(provider);
62
-
63
- const run = await Run.create<t.IState>({
64
- runId: 'test-run-id',
65
- graphConfig: {
66
- type: 'standard',
67
- llmConfig,
68
- tools: [new TavilySearchResults()],
69
- instructions: 'You are a friendly AI assistant. Always address the user by their name.',
70
- additional_instructions: `The user's name is ${userName} and they are located in ${location}.`,
71
- streamBuffer: 3000,
72
- },
73
- returnContent: true,
74
- customHandlers,
75
- });
76
-
77
- const config = {
78
- configurable: {
79
- provider,
80
- thread_id: 'conversation-num-1',
81
- },
82
- streamMode: 'values',
83
- version: 'v2' as const,
84
- };
85
-
86
- console.log('Test 1: Weather query (content parts test)');
87
-
88
- const userMessage = `
89
- Make a search for the weather in ${location} today, which is ${currentDate}.
90
- Before making the search, please let me know what you're about to do, then immediately start searching without hesitation.
91
- Make sure to always refer to me by name, which is ${userName}.
92
- After giving me a thorough summary, tell me a joke about the weather forecast we went over.
93
- `;
94
-
95
- conversationHistory.push(new HumanMessage(userMessage));
96
-
97
- const inputs = {
98
- messages: conversationHistory,
99
- };
100
- const finalContentParts = await run.processStream(inputs, config);
101
- const finalMessages = run.getRunMessages();
102
- if (finalMessages) {
103
- conversationHistory.push(...finalMessages);
104
- console.dir(conversationHistory, { depth: null });
105
- }
106
- // console.dir(finalContentParts, { depth: null });
107
- console.log('\n\n====================\n\n');
108
- // console.dir(contentParts, { depth: null });
109
- }
110
-
111
- process.on('unhandledRejection', (reason, promise) => {
112
- console.error('Unhandled Rejection at:', promise, 'reason:', reason);
113
- console.log('Conversation history:');
114
- process.exit(1);
115
- });
116
-
117
- testStandardStreaming().catch((err) => {
118
- console.error(err);
119
- console.log('Conversation history:');
120
- console.dir(conversationHistory, { depth: null });
121
- process.exit(1);
122
- });
1
+ /* eslint-disable no-console */
2
+ // src/scripts/cli.ts
3
+ import { config } from 'dotenv';
4
+ config();
5
+ import { HumanMessage, BaseMessage } from '@langchain/core/messages';
6
+ import type * as t from '@/types';
7
+ import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
8
+ import { ToolEndHandler } from '@/events';
9
+
10
+ import { getArgs } from '@/scripts/args';
11
+ import { Run } from '@/run';
12
+ import { GraphEvents, Callback } from '@/common';
13
+ import { getLLMConfig } from '@/utils/llmConfig';
14
+
15
+ const conversationHistory: BaseMessage[] = [];
16
+ async function testStandardStreaming(): Promise<void> {
17
+ const { userName, location, provider, currentDate } = await getArgs();
18
+ const { contentParts, aggregateContent } = createContentAggregator();
19
+ const customHandlers = {
20
+ [GraphEvents.TOOL_END]: new ToolEndHandler(),
21
+ // [GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
22
+ [GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
23
+ [GraphEvents.ON_RUN_STEP_COMPLETED]: {
24
+ handle: (
25
+ event: GraphEvents.ON_RUN_STEP_COMPLETED,
26
+ data: t.StreamEventData
27
+ ): void => {
28
+ console.log('====== ON_RUN_STEP_COMPLETED ======');
29
+ // console.dir(data, { depth: null });
30
+ aggregateContent({
31
+ event,
32
+ data: data as unknown as { result: t.ToolEndEvent },
33
+ });
34
+ },
35
+ },
36
+ [GraphEvents.ON_RUN_STEP]: {
37
+ handle: (
38
+ event: GraphEvents.ON_RUN_STEP,
39
+ data: t.StreamEventData
40
+ ): void => {
41
+ console.log('====== ON_RUN_STEP ======');
42
+ console.dir(data, { depth: null });
43
+ aggregateContent({ event, data: data as t.RunStep });
44
+ },
45
+ },
46
+ [GraphEvents.ON_RUN_STEP_DELTA]: {
47
+ handle: (
48
+ event: GraphEvents.ON_RUN_STEP_DELTA,
49
+ data: t.StreamEventData
50
+ ): void => {
51
+ console.log('====== ON_RUN_STEP_DELTA ======');
52
+ console.dir(data, { depth: null });
53
+ aggregateContent({ event, data: data as t.RunStepDeltaEvent });
54
+ },
55
+ },
56
+ [GraphEvents.ON_MESSAGE_DELTA]: {
57
+ handle: (
58
+ event: GraphEvents.ON_MESSAGE_DELTA,
59
+ data: t.StreamEventData
60
+ ): void => {
61
+ console.log('====== ON_MESSAGE_DELTA ======');
62
+ console.dir(data, { depth: null });
63
+ aggregateContent({ event, data: data as t.MessageDeltaEvent });
64
+ },
65
+ },
66
+ [GraphEvents.TOOL_START]: {
67
+ handle: (
68
+ _event: string,
69
+ data: t.StreamEventData,
70
+ metadata?: Record<string, unknown>
71
+ ): void => {
72
+ console.log('====== TOOL_START ======');
73
+ // console.dir(data, { depth: null });
74
+ },
75
+ },
76
+ };
77
+
78
+ const llmConfig = getLLMConfig(provider);
79
+
80
+ const run = await Run.create<t.IState>({
81
+ runId: 'test-run-id',
82
+ graphConfig: {
83
+ type: 'standard',
84
+ llmConfig,
85
+ tools: [],
86
+ instructions:
87
+ 'You are a friendly AI assistant. Always address the user by their name.',
88
+ additional_instructions: `The user's name is ${userName} and they are located in ${location}.`,
89
+ streamBuffer: 3000,
90
+ },
91
+ returnContent: true,
92
+ customHandlers,
93
+ });
94
+
95
+ const config = {
96
+ configurable: {
97
+ provider,
98
+ thread_id: 'conversation-num-1',
99
+ },
100
+ streamMode: 'values',
101
+ version: 'v2' as const,
102
+ };
103
+
104
+ console.log('Test 1: Weather query (content parts test)');
105
+
106
+ const userMessage = `
107
+ Make a search for the weather in ${location} today, which is ${currentDate}.
108
+ Before making the search, please let me know what you're about to do, then immediately start searching without hesitation.
109
+ Make sure to always refer to me by name, which is ${userName}.
110
+ After giving me a thorough summary, tell me a joke about the weather forecast we went over.
111
+ `;
112
+
113
+ conversationHistory.push(new HumanMessage(userMessage));
114
+
115
+ const inputs = {
116
+ messages: conversationHistory,
117
+ };
118
+ const finalContentParts = await run.processStream(inputs, config);
119
+ const finalMessages = run.getRunMessages();
120
+ if (finalMessages) {
121
+ conversationHistory.push(...finalMessages);
122
+ console.dir(conversationHistory, { depth: null });
123
+ }
124
+ // console.dir(finalContentParts, { depth: null });
125
+ console.log('\n\n====================\n\n');
126
+ // console.dir(contentParts, { depth: null });
127
+ }
128
+
129
+ process.on('unhandledRejection', (reason, promise) => {
130
+ console.error('Unhandled Rejection at:', promise, 'reason:', reason);
131
+ console.log('Conversation history:');
132
+ process.exit(1);
133
+ });
134
+
135
+ testStandardStreaming().catch((err) => {
136
+ console.error(err);
137
+ console.log('Conversation history:');
138
+ console.dir(conversationHistory, { depth: null });
139
+ process.exit(1);
140
+ });
@@ -0,0 +1,145 @@
1
+ #!/usr/bin/env bun
2
+
3
+ import { config } from 'dotenv';
4
+ config();
5
+
6
+ import { HumanMessage } from '@langchain/core/messages';
7
+ import { Run } from '@/run';
8
+ import { Providers } from '@/common';
9
+ import type * as t from '@/types';
10
+
11
+ /**
12
+ * Test the custom promptKey feature for handoff edges
13
+ * This demonstrates how to use custom parameter names instead of "instructions"
14
+ */
15
+ async function testCustomPromptKey() {
16
+ console.log('Testing Custom Prompt Key Feature...\n');
17
+
18
+ const runConfig: t.RunConfig = {
19
+ runId: `test-custom-prompt-key-${Date.now()}`,
20
+ graphConfig: {
21
+ type: 'multi-agent',
22
+ agents: [
23
+ {
24
+ agentId: 'supervisor',
25
+ provider: Providers.ANTHROPIC,
26
+ clientOptions: {
27
+ modelName: 'claude-haiku-4-5',
28
+ apiKey: process.env.ANTHROPIC_API_KEY,
29
+ },
30
+ instructions: `You are a Task Supervisor managing different agents:
31
+
32
+ 1. transfer_to_researcher - For research tasks (uses "query" parameter)
33
+ 2. transfer_to_designer - For design tasks (uses "requirements" parameter)
34
+ 3. transfer_to_coder - For coding tasks (uses "specification" parameter)
35
+
36
+ Each agent expects different parameter names in their handoff tools.
37
+ Pay attention to the parameter names when calling each tool.`,
38
+ maxContextTokens: 8000,
39
+ },
40
+ {
41
+ agentId: 'researcher',
42
+ provider: Providers.ANTHROPIC,
43
+ clientOptions: {
44
+ modelName: 'claude-haiku-4-5',
45
+ apiKey: process.env.ANTHROPIC_API_KEY,
46
+ },
47
+ instructions: `You are a Research Agent. You receive research queries to investigate.
48
+ Look for the "Query:" field in the transfer message.`,
49
+ maxContextTokens: 8000,
50
+ },
51
+ {
52
+ agentId: 'designer',
53
+ provider: Providers.ANTHROPIC,
54
+ clientOptions: {
55
+ modelName: 'claude-haiku-4-5',
56
+ apiKey: process.env.ANTHROPIC_API_KEY,
57
+ },
58
+ instructions: `You are a Design Agent. You receive design requirements to implement.
59
+ Look for the "Requirements:" field in the transfer message.`,
60
+ maxContextTokens: 8000,
61
+ },
62
+ {
63
+ agentId: 'coder',
64
+ provider: Providers.ANTHROPIC,
65
+ clientOptions: {
66
+ modelName: 'claude-haiku-4-5',
67
+ apiKey: process.env.ANTHROPIC_API_KEY,
68
+ },
69
+ instructions: `You are a Coding Agent. You receive technical specifications to implement.
70
+ Look for the "Specification:" field in the transfer message.`,
71
+ maxContextTokens: 8000,
72
+ },
73
+ ],
74
+ edges: [
75
+ {
76
+ from: 'supervisor',
77
+ to: 'researcher',
78
+ edgeType: 'handoff',
79
+ // Custom parameter name: "query"
80
+ prompt: 'The research question or topic to investigate',
81
+ promptKey: 'query',
82
+ },
83
+ {
84
+ from: 'supervisor',
85
+ to: 'designer',
86
+ edgeType: 'handoff',
87
+ // Custom parameter name: "requirements"
88
+ prompt: 'The design requirements and constraints',
89
+ promptKey: 'requirements',
90
+ },
91
+ {
92
+ from: 'supervisor',
93
+ to: 'coder',
94
+ edgeType: 'handoff',
95
+ // Custom parameter name: "specification"
96
+ prompt: 'The technical specification for the code to implement',
97
+ promptKey: 'specification',
98
+ },
99
+ ],
100
+ },
101
+ };
102
+
103
+ const run = await Run.create(runConfig);
104
+
105
+ // Test queries for different agents
106
+ const testQueries = [
107
+ // 'Research the latest trends in sustainable energy storage technologies',
108
+ 'Design a mobile app interface for a fitness tracking application',
109
+ // 'Write a Python function that calculates the Fibonacci sequence recursively',
110
+ ];
111
+
112
+ const config = {
113
+ configurable: {
114
+ thread_id: 'custom-prompt-key-test-1',
115
+ },
116
+ streamMode: 'values',
117
+ version: 'v2' as const,
118
+ };
119
+
120
+ for (const query of testQueries) {
121
+ console.log(`\n${'='.repeat(60)}`);
122
+ console.log(`USER QUERY: "${query}"`);
123
+ console.log('='.repeat(60));
124
+
125
+ const inputs = {
126
+ messages: [new HumanMessage(query)],
127
+ };
128
+
129
+ await run.processStream(inputs, config);
130
+
131
+ console.log(`\n${'─'.repeat(60)}`);
132
+ console.log('Each agent receives instructions via their custom parameter:');
133
+ console.log('- Researcher expects "query"');
134
+ console.log('- Designer expects "requirements"');
135
+ console.log('- Coder expects "specification"');
136
+ console.log('─'.repeat(60));
137
+ }
138
+
139
+ console.log('\n\nDemonstration complete!');
140
+ console.log('The promptKey feature allows for more semantic parameter names');
141
+ console.log('that better match the domain and purpose of each agent.');
142
+ }
143
+
144
+ // Run the test
145
+ testCustomPromptKey().catch(console.error);
@@ -0,0 +1,170 @@
1
+ import { config } from 'dotenv';
2
+ config();
3
+
4
+ import { HumanMessage } from '@langchain/core/messages';
5
+ import { Run } from '@/run';
6
+ import { Providers, GraphEvents } from '@/common';
7
+ import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
8
+ import { ToolEndHandler, ModelEndHandler } from '@/events';
9
+ import type * as t from '@/types';
10
+
11
+ /**
12
+ * Test the new handoff input feature using the prompt field
13
+ * This demonstrates how supervisors can pass specific instructions to specialists
14
+ */
15
+ async function testHandoffInput() {
16
+ console.log('Testing Handoff Input Feature...\n');
17
+ // Set up content aggregator
18
+ const { contentParts, aggregateContent } = createContentAggregator();
19
+
20
+ // Track which specialist role was selected
21
+ let selectedRole = '';
22
+ let roleInstructions = '';
23
+
24
+ // Create custom handlers
25
+ const customHandlers = {
26
+ [GraphEvents.TOOL_END]: new ToolEndHandler(),
27
+ [GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
28
+ [GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
29
+ [GraphEvents.ON_RUN_STEP]: {
30
+ handle: (
31
+ event: GraphEvents.ON_RUN_STEP,
32
+ data: t.StreamEventData
33
+ ): void => {
34
+ const runStepData = data as any;
35
+ if (runStepData?.name) {
36
+ console.log(`\n[${runStepData.name}] Processing...`);
37
+ }
38
+ aggregateContent({ event, data: data as t.RunStep });
39
+ },
40
+ },
41
+ [GraphEvents.ON_RUN_STEP_COMPLETED]: {
42
+ handle: (
43
+ event: GraphEvents.ON_RUN_STEP_COMPLETED,
44
+ data: t.StreamEventData
45
+ ): void => {
46
+ aggregateContent({
47
+ event,
48
+ data: data as unknown as { result: t.ToolEndEvent },
49
+ });
50
+ },
51
+ },
52
+ [GraphEvents.ON_MESSAGE_DELTA]: {
53
+ handle: (
54
+ event: GraphEvents.ON_MESSAGE_DELTA,
55
+ data: t.StreamEventData
56
+ ): void => {
57
+ console.dir(data, { depth: null });
58
+ aggregateContent({ event, data: data as t.MessageDeltaEvent });
59
+ },
60
+ },
61
+ [GraphEvents.TOOL_START]: {
62
+ handle: (
63
+ _event: string,
64
+ data: t.StreamEventData,
65
+ metadata?: Record<string, unknown>
66
+ ): void => {
67
+ const toolData = data as any;
68
+ if (toolData?.name?.includes('transfer_to_')) {
69
+ const specialist = toolData.name.replace('transfer_to_', '');
70
+ console.log(`\nšŸ”€ Transferring to ${specialist}...`);
71
+ selectedRole = specialist;
72
+ }
73
+ },
74
+ },
75
+ };
76
+
77
+ const runConfig: t.RunConfig = {
78
+ customHandlers,
79
+ runId: `test-handoff-input-${Date.now()}`,
80
+ graphConfig: {
81
+ type: 'multi-agent',
82
+ agents: [
83
+ {
84
+ agentId: 'supervisor',
85
+ provider: Providers.ANTHROPIC,
86
+ clientOptions: {
87
+ modelName: 'claude-haiku-4-5',
88
+ apiKey: process.env.ANTHROPIC_API_KEY,
89
+ },
90
+ instructions: `You are a Task Supervisor. You have access to two specialist agents:
91
+
92
+ 1. transfer_to_analyst - For data analysis tasks
93
+ 2. transfer_to_writer - For content creation tasks
94
+
95
+ When transferring to a specialist, you MUST provide specific instructions
96
+ in the tool call to guide their work. Be detailed about what you need.`,
97
+ maxContextTokens: 8000,
98
+ },
99
+ {
100
+ agentId: 'analyst',
101
+ provider: Providers.ANTHROPIC,
102
+ clientOptions: {
103
+ modelName: 'claude-haiku-4-5',
104
+ apiKey: process.env.ANTHROPIC_API_KEY,
105
+ },
106
+ instructions: `You are a Data Analyst. Follow the supervisor's instructions carefully.
107
+ When you receive instructions, acknowledge them and perform the requested analysis.`,
108
+ maxContextTokens: 8000,
109
+ },
110
+ {
111
+ agentId: 'writer',
112
+ provider: Providers.ANTHROPIC,
113
+ clientOptions: {
114
+ modelName: 'claude-haiku-4-5',
115
+ apiKey: process.env.ANTHROPIC_API_KEY,
116
+ },
117
+ instructions: `You are a Content Writer. Follow the supervisor's instructions carefully.
118
+ When you receive instructions, acknowledge them and create the requested content.`,
119
+ maxContextTokens: 8000,
120
+ },
121
+ ],
122
+ edges: [
123
+ {
124
+ from: 'supervisor',
125
+ to: ['analyst', 'writer'],
126
+ edgeType: 'handoff',
127
+ // This prompt field now serves as the description for the input parameter
128
+ prompt:
129
+ 'Specific instructions for the specialist to follow. Be detailed about what analysis to perform, what data to focus on, or what content to create.',
130
+ },
131
+ ],
132
+ },
133
+ };
134
+
135
+ const run = await Run.create(runConfig);
136
+
137
+ // Test queries that should result in different handoffs with specific instructions
138
+ const testQueries = [
139
+ // 'Analyze our Q4 sales data and identify the top 3 performing products',
140
+ 'Write a blog post about the benefits of remote work for software developers',
141
+ ];
142
+
143
+ const config = {
144
+ configurable: {
145
+ thread_id: 'handoff-input-test-1',
146
+ },
147
+ streamMode: 'values',
148
+ version: 'v2' as const,
149
+ };
150
+
151
+ for (const query of testQueries) {
152
+ console.log(`\n${'='.repeat(60)}`);
153
+ console.log(`USER QUERY: "${query}"`);
154
+ console.log('='.repeat(60));
155
+
156
+ const inputs = {
157
+ messages: [new HumanMessage(query)],
158
+ };
159
+
160
+ const finalContentParts = await run.processStream(inputs, config);
161
+
162
+ console.log(`\n${'─'.repeat(60)}`);
163
+ console.log('Notice how the supervisor passes specific instructions');
164
+ console.log('to the specialist through the handoff tool input parameter.');
165
+ console.log('─'.repeat(60));
166
+ }
167
+ }
168
+
169
+ // Run the test
170
+ testHandoffInput().catch(console.error);