@librechat/agents 3.0.0-rc1 ā 3.0.0-rc10
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/common/enum.cjs +1 -0
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +0 -1
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs +229 -44
- package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
- package/dist/cjs/llm/openai/index.cjs +33 -0
- package/dist/cjs/llm/openai/index.cjs.map +1 -1
- package/dist/cjs/run.cjs +28 -15
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/stream.cjs +1 -1
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/esm/common/enum.mjs +1 -0
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +0 -1
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +230 -45
- package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
- package/dist/esm/llm/openai/index.mjs +33 -0
- package/dist/esm/llm/openai/index.mjs.map +1 -1
- package/dist/esm/run.mjs +28 -15
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/stream.mjs +1 -1
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/types/common/enum.d.ts +2 -1
- package/dist/types/graphs/MultiAgentGraph.d.ts +12 -2
- package/dist/types/llm/openai/index.d.ts +10 -0
- package/dist/types/run.d.ts +1 -1
- package/dist/types/types/graph.d.ts +38 -4
- package/dist/types/types/llm.d.ts +1 -0
- package/dist/types/types/run.d.ts +5 -1
- package/package.json +10 -2
- package/src/common/enum.ts +1 -0
- package/src/graphs/Graph.ts +0 -1
- package/src/graphs/MultiAgentGraph.ts +267 -50
- package/src/llm/openai/index.ts +41 -0
- package/src/run.ts +38 -27
- package/src/scripts/multi-agent-chain.ts +278 -0
- package/src/scripts/multi-agent-document-review-chain.ts +197 -0
- package/src/scripts/multi-agent-hybrid-flow.ts +310 -0
- package/src/scripts/multi-agent-parallel.ts +27 -23
- package/src/scripts/multi-agent-supervisor.ts +362 -0
- package/src/scripts/test-custom-prompt-key.ts +145 -0
- package/src/scripts/test-handoff-input.ts +170 -0
- package/src/scripts/test-multi-agent-list-handoff.ts +261 -0
- package/src/scripts/test-tools-before-handoff.ts +233 -0
- package/src/stream.ts +4 -1
- package/src/types/graph.ts +51 -5
- package/src/types/llm.ts +1 -0
- package/src/types/run.ts +6 -1
- package/dist/types/scripts/abort.d.ts +0 -1
- package/dist/types/scripts/ant_web_search.d.ts +0 -1
- package/dist/types/scripts/args.d.ts +0 -7
- package/dist/types/scripts/caching.d.ts +0 -1
- package/dist/types/scripts/cli.d.ts +0 -1
- package/dist/types/scripts/cli2.d.ts +0 -1
- package/dist/types/scripts/cli3.d.ts +0 -1
- package/dist/types/scripts/cli4.d.ts +0 -1
- package/dist/types/scripts/cli5.d.ts +0 -1
- package/dist/types/scripts/code_exec.d.ts +0 -1
- package/dist/types/scripts/code_exec_files.d.ts +0 -1
- package/dist/types/scripts/code_exec_simple.d.ts +0 -1
- package/dist/types/scripts/content.d.ts +0 -1
- package/dist/types/scripts/empty_input.d.ts +0 -1
- package/dist/types/scripts/handoff-test.d.ts +0 -1
- package/dist/types/scripts/image.d.ts +0 -1
- package/dist/types/scripts/memory.d.ts +0 -1
- package/dist/types/scripts/multi-agent-conditional.d.ts +0 -1
- package/dist/types/scripts/multi-agent-parallel.d.ts +0 -1
- package/dist/types/scripts/multi-agent-sequence.d.ts +0 -1
- package/dist/types/scripts/multi-agent-test.d.ts +0 -1
- package/dist/types/scripts/search.d.ts +0 -1
- package/dist/types/scripts/simple.d.ts +0 -1
- package/dist/types/scripts/stream.d.ts +0 -1
- package/dist/types/scripts/thinking.d.ts +0 -1
- package/dist/types/scripts/tools.d.ts +0 -1
- package/dist/types/specs/spec.utils.d.ts +0 -1
- package/src/scripts/multi-agent-example-output.md +0 -110
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
import { config } from 'dotenv';
|
|
2
|
+
config();
|
|
3
|
+
|
|
4
|
+
import { HumanMessage, BaseMessage } 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
|
+
const conversationHistory: BaseMessage[] = [];
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Example of supervisor-based multi-agent system
|
|
15
|
+
*
|
|
16
|
+
* The supervisor has handoff tools for 5 different specialists.
|
|
17
|
+
* To demonstrate the concept while respecting LangGraph constraints,
|
|
18
|
+
* we show two approaches:
|
|
19
|
+
*
|
|
20
|
+
* 1. All 5 specialists exist but share the same adaptive configuration
|
|
21
|
+
* 2. Only 2 agents total by using a single adaptive specialist (requires workaround)
|
|
22
|
+
*/
|
|
23
|
+
async function testSupervisorMultiAgent() {
|
|
24
|
+
console.log('Testing Supervisor-Based Multi-Agent System...\n');
|
|
25
|
+
|
|
26
|
+
// NOTE: To truly have only 2 agents with 5 handoff tools, you would need:
|
|
27
|
+
// 1. Custom tool implementation (see multi-agent-supervisor-mock.ts)
|
|
28
|
+
// 2. Or modify MultiAgentGraph to support "virtual" agents
|
|
29
|
+
// 3. Or use a single conditional edge with role parameter
|
|
30
|
+
//
|
|
31
|
+
// This example shows the concept using 6 agents that share configuration
|
|
32
|
+
|
|
33
|
+
// Set up content aggregator
|
|
34
|
+
const { contentParts, aggregateContent } = createContentAggregator();
|
|
35
|
+
|
|
36
|
+
// Define configurations for all possible specialists
|
|
37
|
+
const specialistConfigs = {
|
|
38
|
+
data_analyst: {
|
|
39
|
+
provider: Providers.OPENAI,
|
|
40
|
+
clientOptions: {
|
|
41
|
+
modelName: 'gpt-4.1',
|
|
42
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
43
|
+
},
|
|
44
|
+
instructions: `You are a Data Analyst specialist. Your expertise includes:
|
|
45
|
+
- Statistical analysis and data visualization
|
|
46
|
+
- SQL queries and database optimization
|
|
47
|
+
- Python/R for data science
|
|
48
|
+
- Machine learning model evaluation
|
|
49
|
+
- A/B testing and experiment design
|
|
50
|
+
|
|
51
|
+
Follow the supervisor's specific instructions carefully.`,
|
|
52
|
+
maxContextTokens: 8000,
|
|
53
|
+
},
|
|
54
|
+
security_expert: {
|
|
55
|
+
provider: Providers.OPENAI,
|
|
56
|
+
clientOptions: {
|
|
57
|
+
modelName: 'gpt-4.1',
|
|
58
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
59
|
+
},
|
|
60
|
+
instructions: `You are a Security Expert. Your expertise includes:
|
|
61
|
+
- Cybersecurity best practices
|
|
62
|
+
- Vulnerability assessment and penetration testing
|
|
63
|
+
- Security architecture and threat modeling
|
|
64
|
+
- Compliance (GDPR, HIPAA, SOC2, etc.)
|
|
65
|
+
- Incident response and forensics
|
|
66
|
+
|
|
67
|
+
Follow the supervisor's specific instructions carefully.`,
|
|
68
|
+
maxContextTokens: 8000,
|
|
69
|
+
},
|
|
70
|
+
product_designer: {
|
|
71
|
+
provider: Providers.OPENAI,
|
|
72
|
+
clientOptions: {
|
|
73
|
+
modelName: 'gpt-4.1',
|
|
74
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
75
|
+
},
|
|
76
|
+
instructions: `You are a Product Designer. Your expertise includes:
|
|
77
|
+
- User experience (UX) design principles
|
|
78
|
+
- User interface (UI) design and prototyping
|
|
79
|
+
- Design systems and component libraries
|
|
80
|
+
- User research and usability testing
|
|
81
|
+
- Accessibility and inclusive design
|
|
82
|
+
|
|
83
|
+
Follow the supervisor's specific instructions carefully.`,
|
|
84
|
+
maxContextTokens: 8000,
|
|
85
|
+
},
|
|
86
|
+
devops_engineer: {
|
|
87
|
+
provider: Providers.OPENAI,
|
|
88
|
+
clientOptions: {
|
|
89
|
+
modelName: 'gpt-4.1',
|
|
90
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
91
|
+
},
|
|
92
|
+
instructions: `You are a DevOps Engineer. Your expertise includes:
|
|
93
|
+
- CI/CD pipeline design and optimization
|
|
94
|
+
- Infrastructure as Code (Terraform, CloudFormation)
|
|
95
|
+
- Container orchestration (Kubernetes, Docker)
|
|
96
|
+
- Cloud platforms (AWS, GCP, Azure)
|
|
97
|
+
- Monitoring, logging, and observability
|
|
98
|
+
|
|
99
|
+
Follow the supervisor's specific instructions carefully.`,
|
|
100
|
+
maxContextTokens: 8000,
|
|
101
|
+
},
|
|
102
|
+
legal_advisor: {
|
|
103
|
+
provider: Providers.OPENAI,
|
|
104
|
+
clientOptions: {
|
|
105
|
+
modelName: 'gpt-4.1',
|
|
106
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
107
|
+
},
|
|
108
|
+
instructions: `You are a Legal Advisor specializing in technology. Your expertise includes:
|
|
109
|
+
- Software licensing and open source compliance
|
|
110
|
+
- Data privacy and protection laws
|
|
111
|
+
- Intellectual property and patents
|
|
112
|
+
- Contract review and negotiation
|
|
113
|
+
- Regulatory compliance for tech companies
|
|
114
|
+
|
|
115
|
+
Follow the supervisor's specific instructions carefully.`,
|
|
116
|
+
maxContextTokens: 8000,
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
// Track which specialist role was selected
|
|
121
|
+
let selectedRole = '';
|
|
122
|
+
let roleInstructions = '';
|
|
123
|
+
|
|
124
|
+
// Create custom handlers
|
|
125
|
+
const customHandlers = {
|
|
126
|
+
[GraphEvents.TOOL_END]: new ToolEndHandler(),
|
|
127
|
+
[GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
|
|
128
|
+
[GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
|
|
129
|
+
[GraphEvents.ON_RUN_STEP]: {
|
|
130
|
+
handle: (
|
|
131
|
+
event: GraphEvents.ON_RUN_STEP,
|
|
132
|
+
data: t.StreamEventData
|
|
133
|
+
): void => {
|
|
134
|
+
const runStepData = data as any;
|
|
135
|
+
if (runStepData?.name) {
|
|
136
|
+
console.log(`\n[${runStepData.name}] Processing...`);
|
|
137
|
+
}
|
|
138
|
+
aggregateContent({ event, data: data as t.RunStep });
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
[GraphEvents.ON_RUN_STEP_COMPLETED]: {
|
|
142
|
+
handle: (
|
|
143
|
+
event: GraphEvents.ON_RUN_STEP_COMPLETED,
|
|
144
|
+
data: t.StreamEventData
|
|
145
|
+
): void => {
|
|
146
|
+
aggregateContent({
|
|
147
|
+
event,
|
|
148
|
+
data: data as unknown as { result: t.ToolEndEvent },
|
|
149
|
+
});
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
[GraphEvents.ON_MESSAGE_DELTA]: {
|
|
153
|
+
handle: (
|
|
154
|
+
event: GraphEvents.ON_MESSAGE_DELTA,
|
|
155
|
+
data: t.StreamEventData
|
|
156
|
+
): void => {
|
|
157
|
+
console.dir(data, { depth: null });
|
|
158
|
+
aggregateContent({ event, data: data as t.MessageDeltaEvent });
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
[GraphEvents.TOOL_START]: {
|
|
162
|
+
handle: (
|
|
163
|
+
_event: string,
|
|
164
|
+
data: t.StreamEventData,
|
|
165
|
+
metadata?: Record<string, unknown>
|
|
166
|
+
): void => {
|
|
167
|
+
const toolData = data as any;
|
|
168
|
+
if (toolData?.name?.includes('transfer_to_')) {
|
|
169
|
+
const specialist = toolData.name.replace('transfer_to_', '');
|
|
170
|
+
console.log(`\nš Transferring to ${specialist}...`);
|
|
171
|
+
selectedRole = specialist;
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
// Function to create the graph with supervisor having multiple handoff options
|
|
178
|
+
function createSupervisorGraph(): t.RunConfig {
|
|
179
|
+
console.log(`\nCreating graph with supervisor and 5 specialist agents.`);
|
|
180
|
+
console.log('All specialists share the same adaptive configuration.\n');
|
|
181
|
+
|
|
182
|
+
// Define the adaptive specialist configuration that will be reused
|
|
183
|
+
const specialistConfig = {
|
|
184
|
+
provider: Providers.OPENAI,
|
|
185
|
+
clientOptions: {
|
|
186
|
+
modelName: 'gpt-4.1',
|
|
187
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
188
|
+
},
|
|
189
|
+
instructions: `You are an Adaptive Specialist. Your agent ID indicates your role:
|
|
190
|
+
|
|
191
|
+
- data_analyst: Focus on statistical analysis, metrics, ML evaluation, A/B testing
|
|
192
|
+
- security_expert: Focus on cybersecurity, vulnerability assessment, compliance
|
|
193
|
+
- product_designer: Focus on UX/UI design, user research, accessibility
|
|
194
|
+
- devops_engineer: Focus on CI/CD, infrastructure, cloud platforms, monitoring
|
|
195
|
+
- legal_advisor: Focus on licensing, privacy laws, contracts, regulatory compliance
|
|
196
|
+
|
|
197
|
+
The supervisor will provide specific instructions. Follow them while maintaining your expert perspective.`,
|
|
198
|
+
maxContextTokens: 8000,
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
// Create the graph with supervisor and all 5 specialists
|
|
202
|
+
// All specialists share the same adaptive configuration
|
|
203
|
+
const agents: t.AgentInputs[] = [
|
|
204
|
+
{
|
|
205
|
+
agentId: 'supervisor',
|
|
206
|
+
provider: Providers.OPENAI,
|
|
207
|
+
clientOptions: {
|
|
208
|
+
modelName: 'gpt-4.1-mini',
|
|
209
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
210
|
+
},
|
|
211
|
+
instructions: `You are a Task Supervisor with access to 5 specialist agents:
|
|
212
|
+
1. transfer_to_data_analyst - For statistical analysis and metrics
|
|
213
|
+
2. transfer_to_security_expert - For cybersecurity and vulnerability assessment
|
|
214
|
+
3. transfer_to_product_designer - For UX/UI design
|
|
215
|
+
4. transfer_to_devops_engineer - For infrastructure and deployment
|
|
216
|
+
5. transfer_to_legal_advisor - For compliance and licensing
|
|
217
|
+
|
|
218
|
+
Your role is to:
|
|
219
|
+
1. Analyze the incoming request
|
|
220
|
+
2. Decide which specialist is best suited
|
|
221
|
+
3. Use the appropriate transfer tool (e.g., transfer_to_data_analyst)
|
|
222
|
+
4. Provide specific instructions to guide their work
|
|
223
|
+
|
|
224
|
+
Be specific about what you need from the specialist.`,
|
|
225
|
+
maxContextTokens: 8000,
|
|
226
|
+
},
|
|
227
|
+
// Include all 5 specialists with the same adaptive configuration
|
|
228
|
+
{
|
|
229
|
+
agentId: 'data_analyst',
|
|
230
|
+
...specialistConfig,
|
|
231
|
+
},
|
|
232
|
+
{
|
|
233
|
+
agentId: 'security_expert',
|
|
234
|
+
...specialistConfig,
|
|
235
|
+
},
|
|
236
|
+
{
|
|
237
|
+
agentId: 'product_designer',
|
|
238
|
+
...specialistConfig,
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
agentId: 'devops_engineer',
|
|
242
|
+
...specialistConfig,
|
|
243
|
+
},
|
|
244
|
+
{
|
|
245
|
+
agentId: 'legal_advisor',
|
|
246
|
+
...specialistConfig,
|
|
247
|
+
},
|
|
248
|
+
];
|
|
249
|
+
|
|
250
|
+
// Create edges from supervisor to all 5 specialists
|
|
251
|
+
const edges: t.GraphEdge[] = [
|
|
252
|
+
{
|
|
253
|
+
from: 'supervisor',
|
|
254
|
+
to: 'data_analyst',
|
|
255
|
+
description:
|
|
256
|
+
'Transfer to data analyst for statistical analysis and metrics',
|
|
257
|
+
edgeType: 'handoff',
|
|
258
|
+
},
|
|
259
|
+
{
|
|
260
|
+
from: 'supervisor',
|
|
261
|
+
to: 'security_expert',
|
|
262
|
+
description: 'Transfer to security expert for cybersecurity assessment',
|
|
263
|
+
edgeType: 'handoff',
|
|
264
|
+
},
|
|
265
|
+
{
|
|
266
|
+
from: 'supervisor',
|
|
267
|
+
to: 'product_designer',
|
|
268
|
+
description: 'Transfer to product designer for UX/UI design',
|
|
269
|
+
edgeType: 'handoff',
|
|
270
|
+
},
|
|
271
|
+
{
|
|
272
|
+
from: 'supervisor',
|
|
273
|
+
to: 'devops_engineer',
|
|
274
|
+
description:
|
|
275
|
+
'Transfer to DevOps engineer for infrastructure and deployment',
|
|
276
|
+
edgeType: 'handoff',
|
|
277
|
+
},
|
|
278
|
+
{
|
|
279
|
+
from: 'supervisor',
|
|
280
|
+
to: 'legal_advisor',
|
|
281
|
+
description: 'Transfer to legal advisor for compliance and licensing',
|
|
282
|
+
edgeType: 'handoff',
|
|
283
|
+
},
|
|
284
|
+
];
|
|
285
|
+
|
|
286
|
+
return {
|
|
287
|
+
runId: `supervisor-multi-agent-${Date.now()}`,
|
|
288
|
+
graphConfig: {
|
|
289
|
+
type: 'multi-agent',
|
|
290
|
+
agents,
|
|
291
|
+
edges,
|
|
292
|
+
},
|
|
293
|
+
customHandlers,
|
|
294
|
+
returnContent: true,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
try {
|
|
299
|
+
// Test with different queries
|
|
300
|
+
const testQueries = [
|
|
301
|
+
'How can we analyze user engagement metrics to improve our product?',
|
|
302
|
+
// 'What security measures should we implement for our new API?',
|
|
303
|
+
// 'Can you help design a better onboarding flow for our mobile app?',
|
|
304
|
+
// 'We need to set up a CI/CD pipeline for our microservices.',
|
|
305
|
+
// 'What are the legal implications of using GPL-licensed code in our product?',
|
|
306
|
+
];
|
|
307
|
+
|
|
308
|
+
const config = {
|
|
309
|
+
configurable: {
|
|
310
|
+
thread_id: 'supervisor-conversation-1',
|
|
311
|
+
},
|
|
312
|
+
streamMode: 'values',
|
|
313
|
+
version: 'v2' as const,
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
for (const query of testQueries) {
|
|
317
|
+
console.log(`\n${'='.repeat(60)}`);
|
|
318
|
+
console.log(`USER QUERY: "${query}"`);
|
|
319
|
+
console.log('='.repeat(60));
|
|
320
|
+
|
|
321
|
+
// Reset conversation
|
|
322
|
+
conversationHistory.length = 0;
|
|
323
|
+
conversationHistory.push(new HumanMessage(query));
|
|
324
|
+
|
|
325
|
+
// Create graph with supervisor having 5 handoff tools to 1 adaptive specialist
|
|
326
|
+
const runConfig = createSupervisorGraph();
|
|
327
|
+
const run = await Run.create(runConfig);
|
|
328
|
+
|
|
329
|
+
console.log('Processing request...');
|
|
330
|
+
|
|
331
|
+
// Process with streaming
|
|
332
|
+
const inputs = {
|
|
333
|
+
messages: conversationHistory,
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
const finalContentParts = await run.processStream(inputs, config);
|
|
337
|
+
const finalMessages = run.getRunMessages();
|
|
338
|
+
|
|
339
|
+
if (finalMessages) {
|
|
340
|
+
conversationHistory.push(...finalMessages);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// Show summary
|
|
344
|
+
console.log(`\n${'ā'.repeat(60)}`);
|
|
345
|
+
console.log(`Agents in graph: 6 total (supervisor + 5 specialists)`);
|
|
346
|
+
console.log(`All specialists share the same adaptive configuration`);
|
|
347
|
+
console.log(
|
|
348
|
+
`Supervisor tools: transfer_to_data_analyst, transfer_to_security_expert,`
|
|
349
|
+
);
|
|
350
|
+
console.log(
|
|
351
|
+
` transfer_to_product_designer, transfer_to_devops_engineer,`
|
|
352
|
+
);
|
|
353
|
+
console.log(` transfer_to_legal_advisor`);
|
|
354
|
+
console.log('ā'.repeat(60));
|
|
355
|
+
}
|
|
356
|
+
} catch (error) {
|
|
357
|
+
console.error('Error in supervisor multi-agent test:', error);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// Run the test
|
|
362
|
+
testSupervisorMultiAgent();
|
|
@@ -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-3-5-sonnet-latest',
|
|
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-3-5-sonnet-latest',
|
|
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-3-5-sonnet-latest',
|
|
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-3-5-sonnet-latest',
|
|
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-3-5-sonnet-latest',
|
|
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-3-5-sonnet-latest',
|
|
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-3-5-sonnet-latest',
|
|
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);
|