@librechat/agents 3.0.61 ā 3.0.62
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.
- package/dist/cjs/graphs/MultiAgentGraph.cjs +142 -8
- package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +143 -9
- package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
- package/dist/types/graphs/MultiAgentGraph.d.ts +10 -0
- package/package.json +2 -1
- package/src/graphs/MultiAgentGraph.ts +183 -10
- package/src/scripts/multi-agent-conditional.ts +1 -0
- package/src/scripts/multi-agent-supervisor.ts +1 -0
- package/src/scripts/test-handoff-preamble.ts +275 -0
|
@@ -0,0 +1,275 @@
|
|
|
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
|
+
|
|
259
|
+
process.on('unhandledRejection', (reason, promise) => {
|
|
260
|
+
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
|
|
261
|
+
console.log('\nConversation history at failure:');
|
|
262
|
+
console.dir(conversationHistory, { depth: null });
|
|
263
|
+
process.exit(1);
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
process.on('uncaughtException', (err) => {
|
|
267
|
+
console.error('Uncaught Exception:', err);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
testHandoffPreamble().catch((err) => {
|
|
271
|
+
console.error('Test failed:', err);
|
|
272
|
+
console.log('\nConversation history at failure:');
|
|
273
|
+
console.dir(conversationHistory, { depth: null });
|
|
274
|
+
process.exit(1);
|
|
275
|
+
});
|