@librechat/agents 3.0.0-rc8 → 3.0.0-rc9
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 +103 -12
- package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +103 -12
- package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
- package/dist/types/graphs/MultiAgentGraph.d.ts +11 -1
- package/package.json +3 -1
- package/src/graphs/MultiAgentGraph.ts +120 -13
- 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/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-supervisor.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/test-custom-prompt-key.d.ts +0 -2
- package/dist/types/scripts/test-handoff-input.d.ts +0 -1
- package/dist/types/scripts/test-multi-agent-list-handoff.d.ts +0 -2
- package/dist/types/scripts/test-tools-before-handoff.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,197 @@
|
|
|
1
|
+
import { config } from 'dotenv';
|
|
2
|
+
config();
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
HumanMessage,
|
|
6
|
+
BaseMessage,
|
|
7
|
+
getBufferString,
|
|
8
|
+
} from '@langchain/core/messages';
|
|
9
|
+
import { Run } from '@/run';
|
|
10
|
+
import { Providers, GraphEvents } from '@/common';
|
|
11
|
+
import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
|
|
12
|
+
import { ToolEndHandler, ModelEndHandler } from '@/events';
|
|
13
|
+
import type * as t from '@/types';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Create edges for a document review chain where each agent adds their expertise
|
|
17
|
+
*/
|
|
18
|
+
function createDocumentReviewChain(agentIds: string[]): t.GraphEdge[] {
|
|
19
|
+
const edges: t.GraphEdge[] = [];
|
|
20
|
+
|
|
21
|
+
for (let i = 0; i < agentIds.length - 1; i++) {
|
|
22
|
+
edges.push({
|
|
23
|
+
from: agentIds[i],
|
|
24
|
+
to: agentIds[i + 1],
|
|
25
|
+
edgeType: 'direct',
|
|
26
|
+
prompt: (messages: BaseMessage[], startIndex: number) => {
|
|
27
|
+
const runMessages = messages.slice(startIndex);
|
|
28
|
+
const bufferString = getBufferString(runMessages);
|
|
29
|
+
|
|
30
|
+
// Custom prompt that maintains context of document review
|
|
31
|
+
return `You are reviewing a document. Here is the analysis so far from previous reviewers:\n\n${bufferString}\n\nPlease add your specific review based on your expertise. Build upon previous insights without repeating them.`;
|
|
32
|
+
},
|
|
33
|
+
excludeResults: true,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return edges;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function testDocumentReviewChain() {
|
|
41
|
+
console.log('Testing Document Review Chain...\n');
|
|
42
|
+
|
|
43
|
+
const { contentParts, aggregateContent } = createContentAggregator();
|
|
44
|
+
|
|
45
|
+
// Define specialized document reviewers
|
|
46
|
+
const agents: t.AgentInputs[] = [
|
|
47
|
+
{
|
|
48
|
+
agentId: 'grammar_checker',
|
|
49
|
+
provider: Providers.ANTHROPIC,
|
|
50
|
+
clientOptions: {
|
|
51
|
+
modelName: 'claude-3-5-sonnet-latest',
|
|
52
|
+
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
53
|
+
},
|
|
54
|
+
instructions: `You are a Grammar and Style Checker.
|
|
55
|
+
Focus on:
|
|
56
|
+
- Grammar errors
|
|
57
|
+
- Spelling mistakes
|
|
58
|
+
- Sentence structure
|
|
59
|
+
- Writing clarity
|
|
60
|
+
|
|
61
|
+
Start with "GRAMMAR & STYLE CHECK:" and list issues found.`,
|
|
62
|
+
maxContextTokens: 4000,
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
agentId: 'fact_checker',
|
|
66
|
+
provider: Providers.ANTHROPIC,
|
|
67
|
+
clientOptions: {
|
|
68
|
+
modelName: 'claude-3-5-sonnet-latest',
|
|
69
|
+
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
70
|
+
},
|
|
71
|
+
instructions: `You are a Fact Checker.
|
|
72
|
+
Focus on:
|
|
73
|
+
- Accuracy of claims
|
|
74
|
+
- Data verification needs
|
|
75
|
+
- Source requirements
|
|
76
|
+
- Logical consistency
|
|
77
|
+
|
|
78
|
+
Start with "FACT CHECK:" and note any claims that need verification.`,
|
|
79
|
+
maxContextTokens: 4000,
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
agentId: 'tone_reviewer',
|
|
83
|
+
provider: Providers.ANTHROPIC,
|
|
84
|
+
clientOptions: {
|
|
85
|
+
modelName: 'claude-3-5-sonnet-latest',
|
|
86
|
+
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
87
|
+
},
|
|
88
|
+
instructions: `You are a Tone and Audience Reviewer.
|
|
89
|
+
Focus on:
|
|
90
|
+
- Appropriate tone for target audience
|
|
91
|
+
- Consistency of voice
|
|
92
|
+
- Engagement level
|
|
93
|
+
- Cultural sensitivity
|
|
94
|
+
|
|
95
|
+
Start with "TONE & AUDIENCE REVIEW:" and provide specific feedback.`,
|
|
96
|
+
maxContextTokens: 4000,
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
agentId: 'final_editor',
|
|
100
|
+
provider: Providers.ANTHROPIC,
|
|
101
|
+
clientOptions: {
|
|
102
|
+
modelName: 'claude-3-5-sonnet-latest',
|
|
103
|
+
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
104
|
+
},
|
|
105
|
+
instructions: `You are the Final Editor.
|
|
106
|
+
Based on all previous reviews:
|
|
107
|
+
1. Summarize key issues found
|
|
108
|
+
2. Prioritize changes needed
|
|
109
|
+
3. Provide final recommendation (approve/revise/reject)
|
|
110
|
+
|
|
111
|
+
Start with "FINAL EDITORIAL DECISION:" and be decisive.`,
|
|
112
|
+
maxContextTokens: 4000,
|
|
113
|
+
},
|
|
114
|
+
];
|
|
115
|
+
|
|
116
|
+
const agentIds = agents.map((a) => a.agentId);
|
|
117
|
+
const edges = createDocumentReviewChain(agentIds);
|
|
118
|
+
|
|
119
|
+
// Custom handlers (simplified)
|
|
120
|
+
const customHandlers = {
|
|
121
|
+
[GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
|
|
122
|
+
[GraphEvents.ON_RUN_STEP]: {
|
|
123
|
+
handle: (
|
|
124
|
+
event: GraphEvents.ON_RUN_STEP,
|
|
125
|
+
data: t.StreamEventData
|
|
126
|
+
): void => {
|
|
127
|
+
const runStepData = data as any;
|
|
128
|
+
if (runStepData?.name) {
|
|
129
|
+
console.log(`\n✍️ ${runStepData.name} reviewing...`);
|
|
130
|
+
}
|
|
131
|
+
aggregateContent({ event, data: data as t.RunStep });
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const runConfig: t.RunConfig = {
|
|
137
|
+
runId: `doc-review-chain-${Date.now()}`,
|
|
138
|
+
graphConfig: {
|
|
139
|
+
type: 'multi-agent',
|
|
140
|
+
agents,
|
|
141
|
+
edges,
|
|
142
|
+
},
|
|
143
|
+
customHandlers,
|
|
144
|
+
returnContent: true,
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
try {
|
|
148
|
+
const run = await Run.create(runConfig);
|
|
149
|
+
|
|
150
|
+
// Sample document to review
|
|
151
|
+
const documentToReview = `
|
|
152
|
+
The Impact of Artificial Intelligence on Modern Business
|
|
153
|
+
|
|
154
|
+
Artificial Intelligence (AI) is revolutionizing how businesses operate in 2024. Studies show that 85% of companies have implemented some form of AI, leading to average productivity gains of 40%.
|
|
155
|
+
|
|
156
|
+
Key benefits includes:
|
|
157
|
+
- Automated decision-making reducing human error by 90%
|
|
158
|
+
- Cost savings of up to $1 billion annually for large corporations
|
|
159
|
+
- Enhanced customer experiences through 24/7 AI support
|
|
160
|
+
|
|
161
|
+
However, challenges remain. Many organizations struggles with data privacy concerns and the need for specialized talent. The future of AI in business looks bright, but companies must carefully navigate these obstacles to fully realize it's potential.
|
|
162
|
+
`;
|
|
163
|
+
|
|
164
|
+
const userMessage = `Please review this document:\n\n${documentToReview}`;
|
|
165
|
+
const messages = [new HumanMessage(userMessage)];
|
|
166
|
+
|
|
167
|
+
console.log('Document submitted for review...\n');
|
|
168
|
+
|
|
169
|
+
const config = {
|
|
170
|
+
configurable: { thread_id: 'doc-review-1' },
|
|
171
|
+
streamMode: 'values',
|
|
172
|
+
version: 'v2' as const,
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
await run.processStream({ messages }, config);
|
|
176
|
+
|
|
177
|
+
console.log('\n=== Review Chain Complete ===');
|
|
178
|
+
|
|
179
|
+
// Show how each reviewer built upon previous feedback
|
|
180
|
+
const runMessages = run.getRunMessages();
|
|
181
|
+
if (runMessages) {
|
|
182
|
+
console.log('\nReview progression:');
|
|
183
|
+
runMessages.forEach((msg, i) => {
|
|
184
|
+
if (msg._getType() === 'ai') {
|
|
185
|
+
console.log(`\nStep ${i + 1}: ${agentIds[Math.floor(i / 2)]}`);
|
|
186
|
+
console.log('---');
|
|
187
|
+
console.log(msg.content.toString().slice(0, 200) + '...');
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
} catch (error) {
|
|
192
|
+
console.error('Error in document review chain:', error);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Run the test
|
|
197
|
+
// testDocumentReviewChain();
|
|
@@ -0,0 +1,310 @@
|
|
|
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, Constants } 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 hybrid multi-agent system combining handoff and sequential patterns
|
|
15
|
+
*
|
|
16
|
+
* Graph structure:
|
|
17
|
+
* START -> primary_agent -> agent_b -> agent_c -> END
|
|
18
|
+
* |
|
|
19
|
+
* └─> standalone_agent -> END
|
|
20
|
+
*
|
|
21
|
+
* Because primary_agent has BOTH handoff and direct edges:
|
|
22
|
+
* - Uses Command-based routing for exclusive execution
|
|
23
|
+
* - The primary agent can either:
|
|
24
|
+
* 1. Handoff to standalone_agent (direct edge to agent_b is cancelled)
|
|
25
|
+
* 2. OR continue to agent_b -> agent_c (if no handoff occurs)
|
|
26
|
+
*
|
|
27
|
+
* This is automatic behavior when an agent has both edge types.
|
|
28
|
+
*/
|
|
29
|
+
async function testHybridMultiAgent() {
|
|
30
|
+
console.log('Testing Hybrid Multi-Agent System (Sequential + Handoff)...\n');
|
|
31
|
+
|
|
32
|
+
// Define agents
|
|
33
|
+
const agents: t.AgentInputs[] = [
|
|
34
|
+
{
|
|
35
|
+
agentId: 'primary_agent',
|
|
36
|
+
provider: Providers.OPENAI,
|
|
37
|
+
clientOptions: {
|
|
38
|
+
modelName: 'gpt-4.1-mini',
|
|
39
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
40
|
+
},
|
|
41
|
+
instructions: `You are the Primary Agent in a hybrid workflow.
|
|
42
|
+
|
|
43
|
+
You have TWO options:
|
|
44
|
+
1. If the request requires specialized expertise (complex analysis, deep technical knowledge, etc.),
|
|
45
|
+
use the "transfer_to_standalone_agent" tool to hand off to the Standalone Specialist
|
|
46
|
+
2. If the request is straightforward and can be handled through standard processing,
|
|
47
|
+
just provide your initial response and it will automatically continue to Agent B
|
|
48
|
+
|
|
49
|
+
Be decisive - either handoff immediately or provide your response.
|
|
50
|
+
Start your response with "PRIMARY AGENT:" if you're handling it yourself.`,
|
|
51
|
+
maxContextTokens: 8000,
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
agentId: 'standalone_agent',
|
|
55
|
+
provider: Providers.OPENAI,
|
|
56
|
+
clientOptions: {
|
|
57
|
+
modelName: 'gpt-4.1',
|
|
58
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
59
|
+
},
|
|
60
|
+
instructions: `You are a Standalone Specialist Agent.
|
|
61
|
+
You only receive requests that require specialized expertise.
|
|
62
|
+
|
|
63
|
+
Provide a comprehensive, expert-level response to the request.
|
|
64
|
+
Start your response with "STANDALONE SPECIALIST:"
|
|
65
|
+
End with "Specialized analysis complete."`,
|
|
66
|
+
maxContextTokens: 8000,
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
agentId: 'agent_b',
|
|
70
|
+
provider: Providers.OPENAI,
|
|
71
|
+
clientOptions: {
|
|
72
|
+
modelName: 'gpt-4.1',
|
|
73
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
74
|
+
},
|
|
75
|
+
instructions: `You are Agent B in a sequential workflow.
|
|
76
|
+
You receive requests that the Primary Agent decided to handle through standard processing.
|
|
77
|
+
|
|
78
|
+
Your job is to:
|
|
79
|
+
1. Build upon the Primary Agent's initial response
|
|
80
|
+
2. Add additional processing or analysis (keep it brief, 2-3 sentences)
|
|
81
|
+
3. Prepare the information for final processing by Agent C
|
|
82
|
+
|
|
83
|
+
Start your response with "AGENT B:" and end with "Passing to final processing..."`,
|
|
84
|
+
maxContextTokens: 8000,
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
agentId: 'agent_c',
|
|
88
|
+
provider: Providers.OPENAI,
|
|
89
|
+
clientOptions: {
|
|
90
|
+
modelName: 'gpt-4.1',
|
|
91
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
92
|
+
},
|
|
93
|
+
instructions: `You are Agent C, the final agent in the sequential workflow.
|
|
94
|
+
|
|
95
|
+
Your job is to:
|
|
96
|
+
1. Review all previous processing from Primary Agent and Agent B
|
|
97
|
+
2. Provide a final summary or conclusion
|
|
98
|
+
3. Complete the standard workflow
|
|
99
|
+
|
|
100
|
+
Start your response with "AGENT C:" and end with "Standard workflow complete."`,
|
|
101
|
+
maxContextTokens: 8000,
|
|
102
|
+
},
|
|
103
|
+
];
|
|
104
|
+
|
|
105
|
+
// Define edges combining handoff and direct patterns
|
|
106
|
+
const edges: t.GraphEdge[] = [
|
|
107
|
+
// Handoff edge: primary can transfer to standalone
|
|
108
|
+
{
|
|
109
|
+
from: 'primary_agent',
|
|
110
|
+
to: 'standalone_agent',
|
|
111
|
+
edgeType: 'handoff',
|
|
112
|
+
description: 'Transfer to standalone specialist for complex requests',
|
|
113
|
+
prompt: 'Specific instructions for the specialist',
|
|
114
|
+
},
|
|
115
|
+
// Direct edge - exclusive with handoffs (automatic when agent has both types)
|
|
116
|
+
{
|
|
117
|
+
from: 'primary_agent',
|
|
118
|
+
to: 'agent_b',
|
|
119
|
+
edgeType: 'direct',
|
|
120
|
+
description: 'Continue to Agent B only if no handoff occurs',
|
|
121
|
+
},
|
|
122
|
+
// Direct edge: agent_b automatically continues to agent_c
|
|
123
|
+
{
|
|
124
|
+
from: 'agent_b',
|
|
125
|
+
to: 'agent_c',
|
|
126
|
+
edgeType: 'direct',
|
|
127
|
+
description: 'Automatic progression from B to C',
|
|
128
|
+
},
|
|
129
|
+
];
|
|
130
|
+
|
|
131
|
+
try {
|
|
132
|
+
// Test with different queries
|
|
133
|
+
const testQueries = [
|
|
134
|
+
{
|
|
135
|
+
query: 'What is the capital of France?',
|
|
136
|
+
expectedPath: 'sequential',
|
|
137
|
+
description: 'Simple query - should go through sequential flow',
|
|
138
|
+
},
|
|
139
|
+
// {
|
|
140
|
+
// query: 'Design a distributed microservices architecture for a real-time trading platform with sub-millisecond latency requirements',
|
|
141
|
+
// expectedPath: 'handoff',
|
|
142
|
+
// description: 'Complex query - should handoff to specialist',
|
|
143
|
+
// },
|
|
144
|
+
];
|
|
145
|
+
|
|
146
|
+
const config = {
|
|
147
|
+
configurable: {
|
|
148
|
+
thread_id: 'hybrid-conversation-1',
|
|
149
|
+
},
|
|
150
|
+
streamMode: 'values',
|
|
151
|
+
version: 'v2' as const,
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
for (const test of testQueries) {
|
|
155
|
+
console.log(`\n${'='.repeat(70)}`);
|
|
156
|
+
console.log(`TEST: ${test.description}`);
|
|
157
|
+
console.log(`QUERY: "${test.query}"`);
|
|
158
|
+
console.log(`EXPECTED PATH: ${test.expectedPath}`);
|
|
159
|
+
console.log('='.repeat(70));
|
|
160
|
+
|
|
161
|
+
// Reset state
|
|
162
|
+
conversationHistory.length = 0;
|
|
163
|
+
conversationHistory.push(new HumanMessage(test.query));
|
|
164
|
+
|
|
165
|
+
// Create separate content aggregator for each test
|
|
166
|
+
const { contentParts, aggregateContent } = createContentAggregator();
|
|
167
|
+
|
|
168
|
+
// Track agent progression for this test
|
|
169
|
+
let currentAgent = '';
|
|
170
|
+
let handoffOccurred = false;
|
|
171
|
+
|
|
172
|
+
// Create custom handlers for this test
|
|
173
|
+
const customHandlers = {
|
|
174
|
+
[GraphEvents.TOOL_END]: new ToolEndHandler(),
|
|
175
|
+
[GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
|
|
176
|
+
[GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
|
|
177
|
+
[GraphEvents.ON_RUN_STEP]: {
|
|
178
|
+
handle: (
|
|
179
|
+
event: GraphEvents.ON_RUN_STEP,
|
|
180
|
+
data: t.StreamEventData
|
|
181
|
+
): void => {
|
|
182
|
+
const runStepData = data as any;
|
|
183
|
+
if (runStepData?.name) {
|
|
184
|
+
currentAgent = runStepData.name;
|
|
185
|
+
console.log(`\n[${currentAgent}] Processing...`);
|
|
186
|
+
}
|
|
187
|
+
aggregateContent({ event, data: data as t.RunStep });
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
[GraphEvents.ON_RUN_STEP_COMPLETED]: {
|
|
191
|
+
handle: (
|
|
192
|
+
event: GraphEvents.ON_RUN_STEP_COMPLETED,
|
|
193
|
+
data: t.StreamEventData
|
|
194
|
+
): void => {
|
|
195
|
+
const runStepData = data as any;
|
|
196
|
+
if (runStepData?.name) {
|
|
197
|
+
console.log(`✓ ${runStepData.name} completed`);
|
|
198
|
+
}
|
|
199
|
+
aggregateContent({
|
|
200
|
+
event,
|
|
201
|
+
data: data as unknown as { result: t.ToolEndEvent },
|
|
202
|
+
});
|
|
203
|
+
},
|
|
204
|
+
},
|
|
205
|
+
[GraphEvents.ON_MESSAGE_DELTA]: {
|
|
206
|
+
handle: (
|
|
207
|
+
event: GraphEvents.ON_MESSAGE_DELTA,
|
|
208
|
+
data: t.StreamEventData
|
|
209
|
+
): void => {
|
|
210
|
+
// console.dir(data, { depth: null });
|
|
211
|
+
aggregateContent({ event, data: data as t.MessageDeltaEvent });
|
|
212
|
+
},
|
|
213
|
+
},
|
|
214
|
+
[GraphEvents.TOOL_START]: {
|
|
215
|
+
handle: (
|
|
216
|
+
_event: string,
|
|
217
|
+
data: t.StreamEventData,
|
|
218
|
+
metadata?: Record<string, unknown>
|
|
219
|
+
): void => {
|
|
220
|
+
const toolData = data as any;
|
|
221
|
+
if (toolData?.name?.startsWith(Constants.LC_TRANSFER_TO_)) {
|
|
222
|
+
const specialist = toolData.name.replace(
|
|
223
|
+
Constants.LC_TRANSFER_TO_,
|
|
224
|
+
''
|
|
225
|
+
);
|
|
226
|
+
console.log(`\n🔀 Transferring to ${specialist}...`);
|
|
227
|
+
handoffOccurred = true;
|
|
228
|
+
}
|
|
229
|
+
},
|
|
230
|
+
},
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
// Create a new run configuration for each test
|
|
234
|
+
const runConfig: t.RunConfig = {
|
|
235
|
+
runId: `hybrid-multi-agent-${test.expectedPath}-${Date.now()}`,
|
|
236
|
+
graphConfig: {
|
|
237
|
+
type: 'multi-agent',
|
|
238
|
+
agents,
|
|
239
|
+
edges,
|
|
240
|
+
},
|
|
241
|
+
customHandlers,
|
|
242
|
+
returnContent: true,
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
// Create and execute a new run for this test
|
|
246
|
+
const run = await Run.create(runConfig);
|
|
247
|
+
|
|
248
|
+
console.log('\nProcessing request...');
|
|
249
|
+
|
|
250
|
+
// Process with streaming
|
|
251
|
+
const inputs = {
|
|
252
|
+
messages: conversationHistory,
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
const finalContentParts = await run.processStream(inputs, config);
|
|
256
|
+
const finalMessages = run.getRunMessages();
|
|
257
|
+
|
|
258
|
+
if (finalMessages) {
|
|
259
|
+
conversationHistory.push(...finalMessages);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// Show path taken
|
|
263
|
+
console.log(`\n${'─'.repeat(70)}`);
|
|
264
|
+
console.log('PATH ANALYSIS:');
|
|
265
|
+
console.log(`- Query type: ${test.expectedPath}`);
|
|
266
|
+
console.log(`- Handoff occurred: ${handoffOccurred ? 'YES' : 'NO'}`);
|
|
267
|
+
console.log(
|
|
268
|
+
`- Sequential path runs: ${handoffOccurred ? 'NO (exclusive routing)' : 'YES'}`
|
|
269
|
+
);
|
|
270
|
+
console.log(
|
|
271
|
+
`- Result: ${
|
|
272
|
+
(test.expectedPath === 'handoff' &&
|
|
273
|
+
handoffOccurred &&
|
|
274
|
+
!test.query.includes('continue')) ||
|
|
275
|
+
(test.expectedPath === 'sequential' && !handoffOccurred)
|
|
276
|
+
? '✅ CORRECT'
|
|
277
|
+
: '❌ INCORRECT'
|
|
278
|
+
}`
|
|
279
|
+
);
|
|
280
|
+
console.log('─'.repeat(70));
|
|
281
|
+
|
|
282
|
+
// Display the responses
|
|
283
|
+
const aiMessages = conversationHistory.filter(
|
|
284
|
+
(msg) => msg._getType() === 'ai'
|
|
285
|
+
);
|
|
286
|
+
console.log('\n--- Agent Responses ---');
|
|
287
|
+
aiMessages.forEach((msg, index) => {
|
|
288
|
+
console.log(`\nResponse ${index + 1}:`);
|
|
289
|
+
console.log(msg.content);
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// Final summary
|
|
294
|
+
console.log(`\n${'='.repeat(70)}`);
|
|
295
|
+
console.log('HYBRID WORKFLOW TEST COMPLETE');
|
|
296
|
+
console.log('='.repeat(70));
|
|
297
|
+
console.log('\nThis test demonstrates automatic exclusive routing:');
|
|
298
|
+
console.log('- When an agent has BOTH handoff and direct edges');
|
|
299
|
+
console.log('- It uses Command-based routing for exclusive execution');
|
|
300
|
+
console.log('- Either handoff OR direct edges execute, never both');
|
|
301
|
+
console.log(
|
|
302
|
+
'\nThis prevents duplicate processing in delegation scenarios!'
|
|
303
|
+
);
|
|
304
|
+
} catch (error) {
|
|
305
|
+
console.error('Error in hybrid multi-agent test:', error);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// Run the test
|
|
310
|
+
testHybridMultiAgent();
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function capitalizeFirstLetter(string: string): string;
|