illuma-agents 1.0.37 → 1.0.39

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 (139) hide show
  1. package/dist/cjs/agents/AgentContext.cjs +112 -14
  2. package/dist/cjs/agents/AgentContext.cjs.map +1 -1
  3. package/dist/cjs/common/enum.cjs +5 -1
  4. package/dist/cjs/common/enum.cjs.map +1 -1
  5. package/dist/cjs/graphs/Graph.cjs +148 -8
  6. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  7. package/dist/cjs/graphs/MultiAgentGraph.cjs +277 -11
  8. package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
  9. package/dist/cjs/llm/bedrock/index.cjs +128 -61
  10. package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
  11. package/dist/cjs/main.cjs +22 -7
  12. package/dist/cjs/main.cjs.map +1 -1
  13. package/dist/cjs/messages/cache.cjs +140 -46
  14. package/dist/cjs/messages/cache.cjs.map +1 -1
  15. package/dist/cjs/messages/core.cjs +1 -1
  16. package/dist/cjs/messages/core.cjs.map +1 -1
  17. package/dist/cjs/messages/tools.cjs +2 -2
  18. package/dist/cjs/messages/tools.cjs.map +1 -1
  19. package/dist/cjs/schemas/validate.cjs +173 -0
  20. package/dist/cjs/schemas/validate.cjs.map +1 -0
  21. package/dist/cjs/stream.cjs +4 -2
  22. package/dist/cjs/stream.cjs.map +1 -1
  23. package/dist/cjs/tools/BrowserTools.cjs.map +1 -1
  24. package/dist/cjs/tools/CodeExecutor.cjs +22 -21
  25. package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
  26. package/dist/cjs/tools/ProgrammaticToolCalling.cjs +14 -11
  27. package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
  28. package/dist/cjs/tools/ToolNode.cjs +101 -2
  29. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  30. package/dist/cjs/tools/ToolSearch.cjs +862 -0
  31. package/dist/cjs/tools/ToolSearch.cjs.map +1 -0
  32. package/dist/esm/agents/AgentContext.mjs +112 -14
  33. package/dist/esm/agents/AgentContext.mjs.map +1 -1
  34. package/dist/esm/common/enum.mjs +5 -1
  35. package/dist/esm/common/enum.mjs.map +1 -1
  36. package/dist/esm/graphs/Graph.mjs +149 -9
  37. package/dist/esm/graphs/Graph.mjs.map +1 -1
  38. package/dist/esm/graphs/MultiAgentGraph.mjs +278 -12
  39. package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
  40. package/dist/esm/llm/bedrock/index.mjs +127 -60
  41. package/dist/esm/llm/bedrock/index.mjs.map +1 -1
  42. package/dist/esm/main.mjs +2 -1
  43. package/dist/esm/main.mjs.map +1 -1
  44. package/dist/esm/messages/cache.mjs +140 -46
  45. package/dist/esm/messages/cache.mjs.map +1 -1
  46. package/dist/esm/messages/core.mjs +1 -1
  47. package/dist/esm/messages/core.mjs.map +1 -1
  48. package/dist/esm/messages/tools.mjs +2 -2
  49. package/dist/esm/messages/tools.mjs.map +1 -1
  50. package/dist/esm/schemas/validate.mjs +167 -0
  51. package/dist/esm/schemas/validate.mjs.map +1 -0
  52. package/dist/esm/stream.mjs +4 -2
  53. package/dist/esm/stream.mjs.map +1 -1
  54. package/dist/esm/tools/BrowserTools.mjs.map +1 -1
  55. package/dist/esm/tools/CodeExecutor.mjs +22 -21
  56. package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
  57. package/dist/esm/tools/ProgrammaticToolCalling.mjs +14 -11
  58. package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
  59. package/dist/esm/tools/ToolNode.mjs +102 -3
  60. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  61. package/dist/esm/tools/ToolSearch.mjs +827 -0
  62. package/dist/esm/tools/ToolSearch.mjs.map +1 -0
  63. package/dist/types/agents/AgentContext.d.ts +51 -1
  64. package/dist/types/common/enum.d.ts +6 -2
  65. package/dist/types/graphs/Graph.d.ts +12 -0
  66. package/dist/types/graphs/MultiAgentGraph.d.ts +16 -0
  67. package/dist/types/index.d.ts +2 -1
  68. package/dist/types/llm/bedrock/index.d.ts +89 -11
  69. package/dist/types/llm/bedrock/types.d.ts +27 -0
  70. package/dist/types/llm/bedrock/utils/index.d.ts +5 -0
  71. package/dist/types/llm/bedrock/utils/message_inputs.d.ts +31 -0
  72. package/dist/types/llm/bedrock/utils/message_outputs.d.ts +33 -0
  73. package/dist/types/messages/cache.d.ts +4 -1
  74. package/dist/types/schemas/index.d.ts +1 -0
  75. package/dist/types/schemas/validate.d.ts +36 -0
  76. package/dist/types/tools/CodeExecutor.d.ts +0 -3
  77. package/dist/types/tools/ProgrammaticToolCalling.d.ts +0 -3
  78. package/dist/types/tools/ToolNode.d.ts +3 -1
  79. package/dist/types/tools/ToolSearch.d.ts +148 -0
  80. package/dist/types/types/graph.d.ts +71 -0
  81. package/dist/types/types/llm.d.ts +3 -1
  82. package/dist/types/types/tools.d.ts +42 -2
  83. package/package.json +13 -6
  84. package/src/agents/AgentContext.test.ts +312 -0
  85. package/src/agents/AgentContext.ts +144 -16
  86. package/src/common/enum.ts +5 -1
  87. package/src/graphs/Graph.ts +214 -13
  88. package/src/graphs/MultiAgentGraph.ts +350 -13
  89. package/src/index.ts +4 -1
  90. package/src/llm/bedrock/index.ts +221 -99
  91. package/src/llm/bedrock/llm.spec.ts +616 -0
  92. package/src/llm/bedrock/types.ts +51 -0
  93. package/src/llm/bedrock/utils/index.ts +18 -0
  94. package/src/llm/bedrock/utils/message_inputs.ts +563 -0
  95. package/src/llm/bedrock/utils/message_outputs.ts +310 -0
  96. package/src/messages/__tests__/tools.test.ts +21 -21
  97. package/src/messages/cache.test.ts +304 -0
  98. package/src/messages/cache.ts +183 -53
  99. package/src/messages/core.ts +1 -1
  100. package/src/messages/tools.ts +2 -2
  101. package/src/schemas/index.ts +2 -0
  102. package/src/schemas/validate.test.ts +358 -0
  103. package/src/schemas/validate.ts +238 -0
  104. package/src/scripts/caching.ts +27 -19
  105. package/src/scripts/code_exec_files.ts +58 -15
  106. package/src/scripts/code_exec_multi_session.ts +241 -0
  107. package/src/scripts/code_exec_session.ts +282 -0
  108. package/src/scripts/multi-agent-conditional.ts +1 -0
  109. package/src/scripts/multi-agent-supervisor.ts +1 -0
  110. package/src/scripts/programmatic_exec_agent.ts +4 -4
  111. package/src/scripts/test-handoff-preamble.ts +277 -0
  112. package/src/scripts/test-parallel-handoffs.ts +291 -0
  113. package/src/scripts/test-tools-before-handoff.ts +8 -4
  114. package/src/scripts/test_code_api.ts +361 -0
  115. package/src/scripts/thinking-bedrock.ts +159 -0
  116. package/src/scripts/thinking.ts +39 -18
  117. package/src/scripts/{tool_search_regex.ts → tool_search.ts} +5 -5
  118. package/src/scripts/tools.ts +7 -3
  119. package/src/specs/cache.simple.test.ts +396 -0
  120. package/src/stream.ts +4 -2
  121. package/src/tools/BrowserTools.ts +39 -17
  122. package/src/tools/CodeExecutor.ts +26 -23
  123. package/src/tools/ProgrammaticToolCalling.ts +18 -14
  124. package/src/tools/ToolNode.ts +114 -1
  125. package/src/tools/ToolSearch.ts +1041 -0
  126. package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +0 -2
  127. package/src/tools/__tests__/{ToolSearchRegex.integration.test.ts → ToolSearch.integration.test.ts} +6 -6
  128. package/src/tools/__tests__/ToolSearch.test.ts +1003 -0
  129. package/src/types/graph.test.ts +183 -0
  130. package/src/types/graph.ts +73 -0
  131. package/src/types/llm.ts +3 -1
  132. package/src/types/tools.ts +51 -2
  133. package/dist/cjs/tools/ToolSearchRegex.cjs +0 -455
  134. package/dist/cjs/tools/ToolSearchRegex.cjs.map +0 -1
  135. package/dist/esm/tools/ToolSearchRegex.mjs +0 -448
  136. package/dist/esm/tools/ToolSearchRegex.mjs.map +0 -1
  137. package/dist/types/tools/ToolSearchRegex.d.ts +0 -80
  138. package/src/tools/ToolSearchRegex.ts +0 -535
  139. package/src/tools/__tests__/ToolSearchRegex.test.ts +0 -232
@@ -0,0 +1,282 @@
1
+ // src/scripts/code_exec_session.ts
2
+ /**
3
+ * Test script for automatic session tracking in code execution tools.
4
+ *
5
+ * This tests the automatic session_id injection feature where:
6
+ * 1. First code execution generates files and returns a session_id
7
+ * 2. Session context is stored in Graph.sessions
8
+ * 3. Subsequent code executions automatically have access to previous files
9
+ * without the LLM needing to explicitly pass session_id
10
+ *
11
+ * Run with: npm run code_exec_session
12
+ */
13
+ import { config } from 'dotenv';
14
+ config();
15
+ import { HumanMessage, BaseMessage } from '@langchain/core/messages';
16
+ import type { RunnableConfig } from '@langchain/core/runnables';
17
+ import type * as t from '@/types';
18
+ import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
19
+ import {
20
+ ToolEndHandler,
21
+ ModelEndHandler,
22
+ createMetadataAggregator,
23
+ } from '@/events';
24
+ import { getLLMConfig } from '@/utils/llmConfig';
25
+ import { getArgs } from '@/scripts/args';
26
+ import { Constants, GraphEvents } from '@/common';
27
+ import { Run } from '@/run';
28
+ import { createCodeExecutionTool } from '@/tools/CodeExecutor';
29
+
30
+ const conversationHistory: BaseMessage[] = [];
31
+
32
+ /**
33
+ * Prints a formatted section header for test output
34
+ */
35
+ function printSection(title: string): void {
36
+ console.log('\n' + '='.repeat(60));
37
+ console.log(` ${title}`);
38
+ console.log('='.repeat(60) + '\n');
39
+ }
40
+
41
+ /**
42
+ * Prints session context from the graph for debugging
43
+ */
44
+ function printSessionContext(run: Run<t.IState>): void {
45
+ const graph = run.Graph;
46
+ if (!graph) {
47
+ console.log('[Session] No graph available');
48
+ return;
49
+ }
50
+
51
+ const session = graph.sessions.get(Constants.EXECUTE_CODE) as
52
+ | t.CodeSessionContext
53
+ | undefined;
54
+
55
+ if (!session) {
56
+ console.log('[Session] No session context stored yet');
57
+ return;
58
+ }
59
+
60
+ console.log('[Session] Current session context:');
61
+ console.log(` - session_id: ${session.session_id}`);
62
+ console.log(` - files: ${JSON.stringify(session.files, null, 2)}`);
63
+ console.log(
64
+ ` - lastUpdated: ${new Date(session.lastUpdated).toISOString()}`
65
+ );
66
+ }
67
+
68
+ async function testAutomaticSessionTracking(): Promise<void> {
69
+ const { userName, location, provider, currentDate } = await getArgs();
70
+ const { contentParts, aggregateContent } = createContentAggregator();
71
+
72
+ const customHandlers = {
73
+ [GraphEvents.TOOL_END]: new ToolEndHandler(),
74
+ [GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
75
+ [GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
76
+ [GraphEvents.ON_RUN_STEP_COMPLETED]: {
77
+ handle: (
78
+ event: GraphEvents.ON_RUN_STEP_COMPLETED,
79
+ data: t.StreamEventData
80
+ ): void => {
81
+ console.log('====== ON_RUN_STEP_COMPLETED ======');
82
+ console.dir(data, { depth: null });
83
+ aggregateContent({
84
+ event,
85
+ data: data as unknown as { result: t.ToolEndEvent },
86
+ });
87
+ },
88
+ },
89
+ [GraphEvents.ON_RUN_STEP]: {
90
+ handle: (
91
+ event: GraphEvents.ON_RUN_STEP,
92
+ data: t.StreamEventData
93
+ ): void => {
94
+ console.log('====== ON_RUN_STEP ======');
95
+ console.dir(data, { depth: null });
96
+ aggregateContent({ event, data: data as t.RunStep });
97
+ },
98
+ },
99
+ [GraphEvents.ON_RUN_STEP_DELTA]: {
100
+ handle: (
101
+ event: GraphEvents.ON_RUN_STEP_DELTA,
102
+ data: t.StreamEventData
103
+ ): void => {
104
+ aggregateContent({ event, data: data as t.RunStepDeltaEvent });
105
+ },
106
+ },
107
+ [GraphEvents.ON_MESSAGE_DELTA]: {
108
+ handle: (
109
+ event: GraphEvents.ON_MESSAGE_DELTA,
110
+ data: t.StreamEventData
111
+ ): void => {
112
+ aggregateContent({ event, data: data as t.MessageDeltaEvent });
113
+ },
114
+ },
115
+ [GraphEvents.TOOL_START]: {
116
+ handle: (
117
+ _event: string,
118
+ data: t.StreamEventData,
119
+ _metadata?: Record<string, unknown>
120
+ ): void => {
121
+ console.log('====== TOOL_START ======');
122
+ console.dir(data, { depth: null });
123
+ },
124
+ },
125
+ };
126
+
127
+ const llmConfig = getLLMConfig(provider);
128
+
129
+ const run = await Run.create<t.IState>({
130
+ runId: 'session-tracking-test-1',
131
+ graphConfig: {
132
+ type: 'standard',
133
+ llmConfig,
134
+ tools: [createCodeExecutionTool()],
135
+ instructions: `You are an AI assistant testing automatic file persistence.
136
+ When writing Python code:
137
+ - Use print() for all outputs
138
+ - Files from previous executions are automatically available in /mnt/data/
139
+ - Files are READ-ONLY; write modifications to NEW filenames
140
+ - IMPORTANT: Do NOT include session_id in your tool calls - it's handled automatically.`,
141
+ additional_instructions: `User: ${userName}, Location: ${location}, Date: ${currentDate}.`,
142
+ },
143
+ returnContent: true,
144
+ customHandlers,
145
+ });
146
+
147
+ const streamConfig: Partial<RunnableConfig> & {
148
+ version: 'v1' | 'v2';
149
+ run_id?: string;
150
+ streamMode: string;
151
+ } = {
152
+ configurable: {
153
+ provider,
154
+ thread_id: 'session-tracking-test',
155
+ },
156
+ streamMode: 'values',
157
+ version: 'v2' as const,
158
+ };
159
+
160
+ // =========================================================================
161
+ // Test 1: Create initial file (establishes session)
162
+ // =========================================================================
163
+ printSection('Test 1: Create Initial File');
164
+ console.log(
165
+ 'This test creates a file, which should establish a session context.\n'
166
+ );
167
+
168
+ const userMessage1 = `
169
+ Create a Python file that writes a simple JSON config file named "app_config.json" with the following content:
170
+ {
171
+ "app_name": "TestApp",
172
+ "version": "1.0.0",
173
+ "debug": true
174
+ }
175
+
176
+ After writing, print the contents to confirm it was created correctly.
177
+ `;
178
+
179
+ conversationHistory.push(new HumanMessage(userMessage1));
180
+ await run.processStream({ messages: conversationHistory }, streamConfig);
181
+
182
+ const finalMessages1 = run.getRunMessages();
183
+ if (finalMessages1) {
184
+ conversationHistory.push(...finalMessages1);
185
+ }
186
+
187
+ printSection('Session Context After Test 1');
188
+ printSessionContext(run);
189
+
190
+ // =========================================================================
191
+ // Test 2: Access previously created file (uses automatic session injection)
192
+ // =========================================================================
193
+ printSection('Test 2: Access Previous File (Automatic Session)');
194
+ console.log('This test reads the file created in Test 1.');
195
+ console.log(
196
+ 'The LLM does NOT need to provide session_id - it should be injected automatically.\n'
197
+ );
198
+
199
+ const userMessage2 = `
200
+ Now read the app_config.json file that was just created and:
201
+ 1. Print its contents
202
+ 2. Confirm the version is "1.0.0"
203
+
204
+ Note: You should be able to access this file from the previous execution automatically.
205
+ `;
206
+
207
+ conversationHistory.push(new HumanMessage(userMessage2));
208
+ await run.processStream({ messages: conversationHistory }, streamConfig);
209
+
210
+ const finalMessages2 = run.getRunMessages();
211
+ if (finalMessages2) {
212
+ conversationHistory.push(...finalMessages2);
213
+ }
214
+
215
+ printSection('Session Context After Test 2');
216
+ printSessionContext(run);
217
+
218
+ // =========================================================================
219
+ // Test 3: Modify file (write to new filename)
220
+ // =========================================================================
221
+ printSection('Test 3: Modify File (Write to New Filename)');
222
+ console.log(
223
+ 'This test modifies the config by reading the old file and writing a new one.\n'
224
+ );
225
+
226
+ const userMessage3 = `
227
+ Read app_config.json, update the version to "2.0.0" and debug to false,
228
+ then save it as "app_config_v2.json". Print both the old and new contents.
229
+ `;
230
+
231
+ conversationHistory.push(new HumanMessage(userMessage3));
232
+ await run.processStream({ messages: conversationHistory }, streamConfig);
233
+
234
+ const finalMessages3 = run.getRunMessages();
235
+ if (finalMessages3) {
236
+ conversationHistory.push(...finalMessages3);
237
+ }
238
+
239
+ printSection('Session Context After Test 3');
240
+ printSessionContext(run);
241
+
242
+ // =========================================================================
243
+ // Summary
244
+ // =========================================================================
245
+ printSection('Test Summary');
246
+ console.log('The automatic session tracking feature should have:');
247
+ console.log('1. Stored the session_id after the first code execution');
248
+ console.log('2. Automatically injected it into subsequent executions');
249
+ console.log('3. Accumulated file references across all executions');
250
+ console.log('\nCheck the session context output above to verify.\n');
251
+
252
+ // Generate title
253
+ const { handleLLMEnd, collected } = createMetadataAggregator();
254
+ const titleResult = await run.generateTitle({
255
+ provider,
256
+ inputText: 'Testing automatic session tracking for code execution',
257
+ contentParts,
258
+ chainOptions: {
259
+ callbacks: [{ handleLLMEnd }],
260
+ },
261
+ });
262
+ console.log('Generated Title:', titleResult);
263
+ console.log('Collected metadata:', collected);
264
+ }
265
+
266
+ process.on('unhandledRejection', (reason, promise) => {
267
+ console.error('Unhandled Rejection at:', promise, 'reason:', reason);
268
+ console.log('Conversation history:');
269
+ console.dir(conversationHistory, { depth: null });
270
+ process.exit(1);
271
+ });
272
+
273
+ process.on('uncaughtException', (err) => {
274
+ console.error('Uncaught Exception:', err);
275
+ });
276
+
277
+ testAutomaticSessionTracking().catch((err) => {
278
+ console.error(err);
279
+ console.log('Conversation history:');
280
+ console.dir(conversationHistory, { depth: null });
281
+ process.exit(1);
282
+ });
@@ -210,6 +210,7 @@ async function testConditionalMultiAgent() {
210
210
  console.log(`\n\nExpert used: ${selectedExpert}`);
211
211
  console.log('Content parts:', contentParts.length);
212
212
  console.log('---');
213
+ console.dir(contentParts, { depth: null });
213
214
  }
214
215
  } catch (error) {
215
216
  console.error('Error in conditional multi-agent test:', error);
@@ -353,6 +353,7 @@ async function testSupervisorMultiAgent() {
353
353
  );
354
354
  console.log(` transfer_to_legal_advisor`);
355
355
  console.log('─'.repeat(60));
356
+ console.dir(contentParts, { depth: null });
356
357
  }
357
358
  await sleep(3000);
358
359
  } catch (error) {
@@ -23,7 +23,7 @@ import type { RunnableConfig } from '@langchain/core/runnables';
23
23
  import type * as t from '@/types';
24
24
  import { createCodeExecutionTool } from '@/tools/CodeExecutor';
25
25
  import { createProgrammaticToolCallingTool } from '@/tools/ProgrammaticToolCalling';
26
- import { createToolSearchRegexTool } from '@/tools/ToolSearchRegex';
26
+ import { createToolSearch } from '@/tools/ToolSearch';
27
27
  import { getLLMConfig } from '@/utils/llmConfig';
28
28
  import { getArgs } from '@/scripts/args';
29
29
  import { Run } from '@/run';
@@ -40,7 +40,7 @@ import {
40
40
 
41
41
  /**
42
42
  * Tool registry only needs business logic tools that require filtering.
43
- * Special tools (execute_code, run_tools_with_code, tool_search_regex)
43
+ * Special tools (execute_code, run_tools_with_code, tool_search)
44
44
  * are always bound directly to the LLM and don't need registry entries.
45
45
  */
46
46
  function createAgentToolRegistry(): t.LCToolRegistry {
@@ -73,7 +73,7 @@ async function main(): Promise<void> {
73
73
  // Create special tools (PTC, code execution, tool search)
74
74
  const codeExecTool = createCodeExecutionTool();
75
75
  const ptcTool = createProgrammaticToolCallingTool();
76
- const toolSearchTool = createToolSearchRegexTool();
76
+ const toolSearchTool = createToolSearch();
77
77
 
78
78
  // Build complete tool list and map
79
79
  const allTools = [...mockTools, codeExecTool, ptcTool, toolSearchTool];
@@ -199,7 +199,7 @@ Use the run_tools_with_code tool to do this efficiently - don't call each tool s
199
199
  console.log('='.repeat(70));
200
200
  console.log('\nKey observations:');
201
201
  console.log(
202
- '1. LLM only sees tools with allowed_callers including "direct" (get_weather, execute_code, run_tools_with_code, tool_search_regex)'
202
+ '1. LLM only sees tools with allowed_callers including "direct" (get_weather, execute_code, run_tools_with_code, tool_search)'
203
203
  );
204
204
  console.log(
205
205
  '2. When PTC is invoked, ToolNode automatically injects programmatic tools (get_team_members, get_expenses, get_weather)'
@@ -0,0 +1,277 @@
1
+ /**
2
+ * Test script for multi-turn handoff behavior.
3
+ *
4
+ * This tests the fix for the issue where receiving agents would see transfer messages
5
+ * and prematurely produce end tokens, thinking the work was already done.
6
+ *
7
+ * The fix:
8
+ * 1. Filters out transfer tool calls and ToolMessages from the receiving agent's context
9
+ * 2. Injects any passthrough instructions as a HumanMessage to ground the receiving agent
10
+ */
11
+ import { config } from 'dotenv';
12
+ config();
13
+
14
+ import { HumanMessage, BaseMessage } from '@langchain/core/messages';
15
+ import type { RunnableConfig } from '@langchain/core/runnables';
16
+ import type * as t from '@/types';
17
+ import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
18
+ import { ToolEndHandler, ModelEndHandler } from '@/events';
19
+ import { getLLMConfig } from '@/utils/llmConfig';
20
+ import { GraphEvents, Providers } from '@/common';
21
+ import { Run } from '@/run';
22
+
23
+ const conversationHistory: BaseMessage[] = [];
24
+
25
+ /**
26
+ * Test multi-turn handoff between a coordinator and a specialist
27
+ */
28
+ async function testHandoffPreamble(): Promise<void> {
29
+ console.log('='.repeat(60));
30
+ console.log('Testing Multi-Turn Handoff with Preamble Injection');
31
+ console.log('='.repeat(60));
32
+ console.log('\nThis test verifies that:');
33
+ console.log('1. Transfer messages are filtered from receiving agent context');
34
+ console.log('2. Passthrough instructions are injected as a HumanMessage');
35
+ console.log('3. Multi-turn conversations work correctly after handoffs\n');
36
+
37
+ const { contentParts, aggregateContent } = createContentAggregator();
38
+
39
+ /** Track which agent is responding */
40
+ let currentAgent = '';
41
+
42
+ const customHandlers = {
43
+ [GraphEvents.TOOL_END]: new ToolEndHandler(),
44
+ [GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
45
+ [GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
46
+ [GraphEvents.ON_RUN_STEP]: {
47
+ handle: (
48
+ event: GraphEvents.ON_RUN_STEP,
49
+ data: t.StreamEventData
50
+ ): void => {
51
+ const runStep = data as t.RunStep;
52
+ if (runStep.agentId) {
53
+ currentAgent = runStep.agentId;
54
+ console.log(`\n[Agent: ${currentAgent}] Processing...`);
55
+ }
56
+ aggregateContent({ event, data: runStep });
57
+ },
58
+ },
59
+ [GraphEvents.ON_RUN_STEP_COMPLETED]: {
60
+ handle: (
61
+ event: GraphEvents.ON_RUN_STEP_COMPLETED,
62
+ data: t.StreamEventData
63
+ ): void => {
64
+ aggregateContent({
65
+ event,
66
+ data: data as unknown as { result: t.ToolEndEvent },
67
+ });
68
+ },
69
+ },
70
+ [GraphEvents.ON_MESSAGE_DELTA]: {
71
+ handle: (
72
+ event: GraphEvents.ON_MESSAGE_DELTA,
73
+ data: t.StreamEventData
74
+ ): void => {
75
+ console.log('====== ON_MESSAGE_DELTA ======');
76
+ console.dir(data, { depth: null });
77
+ aggregateContent({ event, data: data as t.MessageDeltaEvent });
78
+ },
79
+ },
80
+ [GraphEvents.TOOL_START]: {
81
+ handle: (
82
+ _event: string,
83
+ data: t.StreamEventData,
84
+ _metadata?: Record<string, unknown>
85
+ ): void => {
86
+ const toolData = data as { name?: string };
87
+ if (toolData?.name?.includes('transfer_to_')) {
88
+ const specialist = toolData.name.replace('lc_transfer_to_', '');
89
+ console.log(`\n🔀 Handing off to: ${specialist}`);
90
+ }
91
+ },
92
+ },
93
+ };
94
+
95
+ /**
96
+ * Create agents:
97
+ * - coordinator: Decides when to hand off to specialist
98
+ * - specialist: Handles specific tasks delegated by coordinator
99
+ */
100
+ const agents: t.AgentInputs[] = [
101
+ {
102
+ agentId: 'coordinator',
103
+ provider: Providers.OPENAI,
104
+ clientOptions: {
105
+ modelName: 'gpt-4.1-mini',
106
+ apiKey: process.env.OPENAI_API_KEY,
107
+ },
108
+ instructions: `You are a Task Coordinator. Your role is to:
109
+ 1. Understand user requests
110
+ 2. If the request involves technical analysis, use the transfer_to_specialist tool to hand off
111
+ 3. When handing off, provide clear instructions about what needs to be done
112
+
113
+ IMPORTANT: When using the handoff tool, include specific instructions for the specialist.`,
114
+ maxContextTokens: 8000,
115
+ },
116
+ {
117
+ agentId: 'specialist',
118
+ provider: Providers.OPENAI,
119
+ clientOptions: {
120
+ modelName: 'gpt-4.1-mini',
121
+ apiKey: process.env.OPENAI_API_KEY,
122
+ },
123
+ instructions: `You are a Technical Specialist. When you receive a request:
124
+ 1. Carefully read any instructions provided
125
+ 2. Provide a detailed technical response
126
+ 3. Do NOT just acknowledge - provide substantive help
127
+
128
+ IMPORTANT: You are the specialist - provide a complete, helpful response to the task.`,
129
+ maxContextTokens: 8000,
130
+ },
131
+ ];
132
+
133
+ /** Create handoff edge with passthrough instructions */
134
+ const edges: t.GraphEdge[] = [
135
+ {
136
+ from: 'coordinator',
137
+ to: 'specialist',
138
+ description: 'Transfer to technical specialist for analysis',
139
+ edgeType: 'handoff',
140
+ prompt: 'Specific instructions for the specialist about what to analyze',
141
+ promptKey: 'instructions',
142
+ },
143
+ ];
144
+
145
+ const runConfig: t.RunConfig = {
146
+ runId: `handoff-test-${Date.now()}`,
147
+ graphConfig: {
148
+ type: 'multi-agent',
149
+ agents,
150
+ edges,
151
+ },
152
+ customHandlers,
153
+ returnContent: true,
154
+ };
155
+
156
+ const run = await Run.create(runConfig);
157
+
158
+ const config: Partial<RunnableConfig> & {
159
+ version: 'v1' | 'v2';
160
+ streamMode: string;
161
+ } = {
162
+ configurable: {
163
+ thread_id: 'handoff-test-conversation-1',
164
+ },
165
+ streamMode: 'values',
166
+ version: 'v2' as const,
167
+ };
168
+
169
+ /** TURN 1: Initial request that triggers handoff */
170
+ console.log('\n' + '─'.repeat(60));
171
+ console.log('TURN 1: Initial request (should trigger handoff)');
172
+ console.log('─'.repeat(60));
173
+
174
+ const userMessage1 = `
175
+ Hi! Can you help me understand the time complexity of quicksort?
176
+ I need a technical explanation.
177
+ `;
178
+
179
+ conversationHistory.push(new HumanMessage(userMessage1));
180
+ console.log('\nUser:', userMessage1.trim());
181
+ console.log('\nResponse:');
182
+
183
+ let inputs = { messages: conversationHistory };
184
+ await run.processStream(inputs, config);
185
+ const messages1 = run.getRunMessages();
186
+ if (messages1) {
187
+ conversationHistory.push(...messages1);
188
+ }
189
+
190
+ console.log('\n');
191
+
192
+ /** TURN 2: Follow-up question to test multi-turn after handoff */
193
+ console.log('\n' + '─'.repeat(60));
194
+ console.log('TURN 2: Follow-up question (tests context after handoff)');
195
+ console.log('─'.repeat(60));
196
+
197
+ const userMessage2 = `
198
+ Thanks! Can you also explain the space complexity and when quicksort
199
+ might not be the best choice?
200
+ `;
201
+
202
+ conversationHistory.push(new HumanMessage(userMessage2));
203
+ console.log('\nUser:', userMessage2.trim());
204
+ console.log('\nResponse:');
205
+
206
+ inputs = { messages: conversationHistory };
207
+ await run.processStream(inputs, config);
208
+ const messages2 = run.getRunMessages();
209
+ if (messages2) {
210
+ conversationHistory.push(...messages2);
211
+ }
212
+
213
+ console.log('\n');
214
+
215
+ /** TURN 3: Another follow-up to verify sustained conversation */
216
+ console.log('\n' + '─'.repeat(60));
217
+ console.log('TURN 3: Third turn (tests sustained multi-turn)');
218
+ console.log('─'.repeat(60));
219
+
220
+ const userMessage3 = `
221
+ Great explanation! One more question - how does quicksort compare
222
+ to mergesort in practice?
223
+ `;
224
+
225
+ conversationHistory.push(new HumanMessage(userMessage3));
226
+ console.log('\nUser:', userMessage3.trim());
227
+ console.log('\nResponse:');
228
+
229
+ inputs = { messages: conversationHistory };
230
+ await run.processStream(inputs, config);
231
+ const messages3 = run.getRunMessages();
232
+ if (messages3) {
233
+ conversationHistory.push(...messages3);
234
+ }
235
+
236
+ /** Summary */
237
+ console.log('\n\n' + '='.repeat(60));
238
+ console.log('TEST SUMMARY');
239
+ console.log('='.repeat(60));
240
+ console.log('\nTotal messages in conversation:', conversationHistory.length);
241
+ console.log('\nMessage types:');
242
+
243
+ for (let i = 0; i < conversationHistory.length; i++) {
244
+ const msg = conversationHistory[i];
245
+ const type = msg.getType();
246
+ const preview =
247
+ typeof msg.content === 'string'
248
+ ? msg.content.slice(0, 50).replace(/\n/g, ' ')
249
+ : '[complex content]';
250
+ console.log(` ${i + 1}. [${type}] ${preview}...`);
251
+ }
252
+
253
+ console.log('\n✅ Test completed. Review the output above to verify:');
254
+ console.log(' - Specialist received and acted on instructions');
255
+ console.log(' - No premature end tokens after handoff');
256
+ console.log(' - Multi-turn conversation continued smoothly');
257
+
258
+ console.dir(contentParts, { depth: null });
259
+ }
260
+
261
+ process.on('unhandledRejection', (reason, promise) => {
262
+ console.error('Unhandled Rejection at:', promise, 'reason:', reason);
263
+ console.log('\nConversation history at failure:');
264
+ console.dir(conversationHistory, { depth: null });
265
+ process.exit(1);
266
+ });
267
+
268
+ process.on('uncaughtException', (err) => {
269
+ console.error('Uncaught Exception:', err);
270
+ });
271
+
272
+ testHandoffPreamble().catch((err) => {
273
+ console.error('Test failed:', err);
274
+ console.log('\nConversation history at failure:');
275
+ console.dir(conversationHistory, { depth: null });
276
+ process.exit(1);
277
+ });