@librechat/agents 2.4.322 → 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/agents/AgentContext.cjs +218 -0
- package/dist/cjs/agents/AgentContext.cjs.map +1 -0
- package/dist/cjs/common/enum.cjs +15 -5
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/events.cjs +10 -6
- package/dist/cjs/events.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +309 -213
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs +507 -0
- package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -0
- package/dist/cjs/llm/anthropic/index.cjs +54 -9
- package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/types.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +52 -6
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs +22 -2
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/tools.cjs +29 -0
- package/dist/cjs/llm/anthropic/utils/tools.cjs.map +1 -0
- package/dist/cjs/llm/google/index.cjs +144 -0
- package/dist/cjs/llm/google/index.cjs.map +1 -0
- package/dist/cjs/llm/google/utils/common.cjs +477 -0
- package/dist/cjs/llm/google/utils/common.cjs.map +1 -0
- package/dist/cjs/llm/ollama/index.cjs +67 -0
- package/dist/cjs/llm/ollama/index.cjs.map +1 -0
- package/dist/cjs/llm/ollama/utils.cjs +158 -0
- package/dist/cjs/llm/ollama/utils.cjs.map +1 -0
- package/dist/cjs/llm/openai/index.cjs +422 -3
- package/dist/cjs/llm/openai/index.cjs.map +1 -1
- package/dist/cjs/llm/openai/utils/index.cjs +672 -0
- package/dist/cjs/llm/openai/utils/index.cjs.map +1 -0
- package/dist/cjs/llm/providers.cjs +15 -15
- package/dist/cjs/llm/providers.cjs.map +1 -1
- package/dist/cjs/llm/text.cjs +14 -3
- package/dist/cjs/llm/text.cjs.map +1 -1
- package/dist/cjs/llm/vertexai/index.cjs +330 -0
- package/dist/cjs/llm/vertexai/index.cjs.map +1 -0
- package/dist/cjs/main.cjs +11 -0
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/run.cjs +137 -85
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/stream.cjs +86 -52
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +10 -4
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/handlers.cjs +119 -13
- package/dist/cjs/tools/handlers.cjs.map +1 -1
- package/dist/cjs/tools/search/anthropic.cjs +40 -0
- package/dist/cjs/tools/search/anthropic.cjs.map +1 -0
- package/dist/cjs/tools/search/firecrawl.cjs +55 -9
- package/dist/cjs/tools/search/firecrawl.cjs.map +1 -1
- package/dist/cjs/tools/search/format.cjs +6 -6
- package/dist/cjs/tools/search/format.cjs.map +1 -1
- package/dist/cjs/tools/search/rerankers.cjs +7 -29
- package/dist/cjs/tools/search/rerankers.cjs.map +1 -1
- package/dist/cjs/tools/search/search.cjs +86 -16
- package/dist/cjs/tools/search/search.cjs.map +1 -1
- package/dist/cjs/tools/search/tool.cjs +4 -2
- package/dist/cjs/tools/search/tool.cjs.map +1 -1
- package/dist/cjs/tools/search/utils.cjs +1 -1
- package/dist/cjs/tools/search/utils.cjs.map +1 -1
- package/dist/cjs/utils/events.cjs +31 -0
- package/dist/cjs/utils/events.cjs.map +1 -0
- package/dist/cjs/utils/title.cjs +57 -21
- package/dist/cjs/utils/title.cjs.map +1 -1
- package/dist/cjs/utils/tokens.cjs +54 -7
- package/dist/cjs/utils/tokens.cjs.map +1 -1
- package/dist/esm/agents/AgentContext.mjs +216 -0
- package/dist/esm/agents/AgentContext.mjs.map +1 -0
- package/dist/esm/common/enum.mjs +16 -6
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/events.mjs +10 -6
- package/dist/esm/events.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +311 -215
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +505 -0
- package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -0
- package/dist/esm/llm/anthropic/index.mjs +54 -9
- package/dist/esm/llm/anthropic/index.mjs.map +1 -1
- package/dist/esm/llm/anthropic/types.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs +52 -6
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs +22 -2
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/tools.mjs +27 -0
- package/dist/esm/llm/anthropic/utils/tools.mjs.map +1 -0
- package/dist/esm/llm/google/index.mjs +142 -0
- package/dist/esm/llm/google/index.mjs.map +1 -0
- package/dist/esm/llm/google/utils/common.mjs +471 -0
- package/dist/esm/llm/google/utils/common.mjs.map +1 -0
- package/dist/esm/llm/ollama/index.mjs +65 -0
- package/dist/esm/llm/ollama/index.mjs.map +1 -0
- package/dist/esm/llm/ollama/utils.mjs +155 -0
- package/dist/esm/llm/ollama/utils.mjs.map +1 -0
- package/dist/esm/llm/openai/index.mjs +421 -4
- package/dist/esm/llm/openai/index.mjs.map +1 -1
- package/dist/esm/llm/openai/utils/index.mjs +666 -0
- package/dist/esm/llm/openai/utils/index.mjs.map +1 -0
- package/dist/esm/llm/providers.mjs +5 -5
- package/dist/esm/llm/providers.mjs.map +1 -1
- package/dist/esm/llm/text.mjs +14 -3
- package/dist/esm/llm/text.mjs.map +1 -1
- package/dist/esm/llm/vertexai/index.mjs +328 -0
- package/dist/esm/llm/vertexai/index.mjs.map +1 -0
- package/dist/esm/main.mjs +6 -5
- package/dist/esm/main.mjs.map +1 -1
- package/dist/esm/run.mjs +138 -87
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/stream.mjs +88 -55
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +10 -4
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/handlers.mjs +119 -15
- package/dist/esm/tools/handlers.mjs.map +1 -1
- package/dist/esm/tools/search/anthropic.mjs +37 -0
- package/dist/esm/tools/search/anthropic.mjs.map +1 -0
- package/dist/esm/tools/search/firecrawl.mjs +55 -9
- package/dist/esm/tools/search/firecrawl.mjs.map +1 -1
- package/dist/esm/tools/search/format.mjs +7 -7
- package/dist/esm/tools/search/format.mjs.map +1 -1
- package/dist/esm/tools/search/rerankers.mjs +7 -29
- package/dist/esm/tools/search/rerankers.mjs.map +1 -1
- package/dist/esm/tools/search/search.mjs +86 -16
- package/dist/esm/tools/search/search.mjs.map +1 -1
- package/dist/esm/tools/search/tool.mjs +4 -2
- package/dist/esm/tools/search/tool.mjs.map +1 -1
- package/dist/esm/tools/search/utils.mjs +1 -1
- package/dist/esm/tools/search/utils.mjs.map +1 -1
- package/dist/esm/utils/events.mjs +29 -0
- package/dist/esm/utils/events.mjs.map +1 -0
- package/dist/esm/utils/title.mjs +57 -22
- package/dist/esm/utils/title.mjs.map +1 -1
- package/dist/esm/utils/tokens.mjs +54 -8
- package/dist/esm/utils/tokens.mjs.map +1 -1
- package/dist/types/agents/AgentContext.d.ts +91 -0
- package/dist/types/common/enum.d.ts +17 -7
- package/dist/types/events.d.ts +5 -4
- package/dist/types/graphs/Graph.d.ts +64 -67
- package/dist/types/graphs/MultiAgentGraph.d.ts +47 -0
- package/dist/types/graphs/index.d.ts +1 -0
- package/dist/types/llm/anthropic/index.d.ts +11 -0
- package/dist/types/llm/anthropic/types.d.ts +9 -3
- package/dist/types/llm/anthropic/utils/message_inputs.d.ts +1 -1
- package/dist/types/llm/anthropic/utils/output_parsers.d.ts +4 -4
- package/dist/types/llm/anthropic/utils/tools.d.ts +3 -0
- package/dist/types/llm/google/index.d.ts +13 -0
- package/dist/types/llm/google/types.d.ts +32 -0
- package/dist/types/llm/google/utils/common.d.ts +19 -0
- package/dist/types/llm/google/utils/tools.d.ts +10 -0
- package/dist/types/llm/google/utils/zod_to_genai_parameters.d.ts +14 -0
- package/dist/types/llm/ollama/index.d.ts +7 -0
- package/dist/types/llm/ollama/utils.d.ts +7 -0
- package/dist/types/llm/openai/index.d.ts +82 -3
- package/dist/types/llm/openai/types.d.ts +10 -0
- package/dist/types/llm/openai/utils/index.d.ts +20 -0
- package/dist/types/llm/text.d.ts +1 -1
- package/dist/types/llm/vertexai/index.d.ts +293 -0
- package/dist/types/messages/reducer.d.ts +9 -0
- package/dist/types/run.d.ts +19 -12
- package/dist/types/stream.d.ts +10 -3
- package/dist/types/tools/CodeExecutor.d.ts +2 -2
- package/dist/types/tools/ToolNode.d.ts +1 -1
- package/dist/types/tools/handlers.d.ts +17 -4
- package/dist/types/tools/search/anthropic.d.ts +16 -0
- package/dist/types/tools/search/firecrawl.d.ts +15 -0
- package/dist/types/tools/search/rerankers.d.ts +0 -1
- package/dist/types/tools/search/types.d.ts +30 -9
- package/dist/types/types/graph.d.ts +129 -15
- package/dist/types/types/llm.d.ts +25 -10
- package/dist/types/types/run.d.ts +50 -8
- package/dist/types/types/stream.d.ts +16 -2
- package/dist/types/types/tools.d.ts +1 -1
- package/dist/types/utils/events.d.ts +6 -0
- package/dist/types/utils/title.d.ts +2 -1
- package/dist/types/utils/tokens.d.ts +24 -0
- package/package.json +41 -17
- package/src/agents/AgentContext.ts +315 -0
- package/src/common/enum.ts +15 -5
- package/src/events.ts +24 -13
- package/src/graphs/Graph.ts +495 -313
- package/src/graphs/MultiAgentGraph.ts +598 -0
- package/src/graphs/index.ts +2 -1
- package/src/llm/anthropic/Jacob_Lee_Resume_2023.pdf +0 -0
- package/src/llm/anthropic/index.ts +78 -13
- package/src/llm/anthropic/llm.spec.ts +491 -115
- package/src/llm/anthropic/types.ts +39 -3
- package/src/llm/anthropic/utils/message_inputs.ts +67 -11
- package/src/llm/anthropic/utils/message_outputs.ts +21 -2
- package/src/llm/anthropic/utils/output_parsers.ts +25 -6
- package/src/llm/anthropic/utils/tools.ts +29 -0
- package/src/llm/google/index.ts +218 -0
- package/src/llm/google/types.ts +43 -0
- package/src/llm/google/utils/common.ts +646 -0
- package/src/llm/google/utils/tools.ts +160 -0
- package/src/llm/google/utils/zod_to_genai_parameters.ts +86 -0
- package/src/llm/ollama/index.ts +89 -0
- package/src/llm/ollama/utils.ts +193 -0
- package/src/llm/openai/index.ts +641 -14
- package/src/llm/openai/types.ts +24 -0
- package/src/llm/openai/utils/index.ts +912 -0
- package/src/llm/openai/utils/isReasoningModel.test.ts +90 -0
- package/src/llm/providers.ts +10 -9
- package/src/llm/text.ts +26 -7
- package/src/llm/vertexai/index.ts +360 -0
- package/src/messages/reducer.ts +80 -0
- package/src/run.ts +196 -116
- package/src/scripts/ant_web_search.ts +158 -0
- package/src/scripts/args.ts +12 -8
- package/src/scripts/cli4.ts +29 -21
- package/src/scripts/cli5.ts +29 -21
- package/src/scripts/code_exec.ts +54 -23
- package/src/scripts/code_exec_files.ts +48 -17
- package/src/scripts/code_exec_simple.ts +46 -27
- package/src/scripts/handoff-test.ts +135 -0
- package/src/scripts/image.ts +52 -20
- package/src/scripts/multi-agent-chain.ts +278 -0
- package/src/scripts/multi-agent-conditional.ts +220 -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 +341 -0
- package/src/scripts/multi-agent-sequence.ts +212 -0
- package/src/scripts/multi-agent-supervisor.ts +362 -0
- package/src/scripts/multi-agent-test.ts +186 -0
- package/src/scripts/search.ts +1 -9
- package/src/scripts/simple.ts +25 -10
- 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/scripts/tools.ts +48 -18
- package/src/specs/anthropic.simple.test.ts +150 -34
- package/src/specs/azure.simple.test.ts +325 -0
- package/src/specs/openai.simple.test.ts +140 -33
- package/src/specs/openrouter.simple.test.ts +107 -0
- package/src/specs/prune.test.ts +4 -9
- package/src/specs/reasoning.test.ts +80 -44
- package/src/specs/token-memoization.test.ts +39 -0
- package/src/stream.test.ts +94 -0
- package/src/stream.ts +143 -61
- package/src/tools/ToolNode.ts +21 -7
- package/src/tools/handlers.ts +192 -18
- package/src/tools/search/anthropic.ts +51 -0
- package/src/tools/search/firecrawl.ts +69 -20
- package/src/tools/search/format.ts +6 -8
- package/src/tools/search/rerankers.ts +7 -40
- package/src/tools/search/search.ts +97 -16
- package/src/tools/search/tool.ts +5 -2
- package/src/tools/search/types.ts +30 -10
- package/src/tools/search/utils.ts +1 -1
- package/src/types/graph.ts +318 -103
- package/src/types/llm.ts +26 -12
- package/src/types/run.ts +56 -13
- package/src/types/stream.ts +22 -1
- package/src/types/tools.ts +16 -10
- package/src/utils/events.ts +32 -0
- package/src/utils/llmConfig.ts +19 -7
- package/src/utils/title.ts +104 -30
- package/src/utils/tokens.ts +69 -10
- package/dist/types/scripts/abort.d.ts +0 -1
- package/dist/types/scripts/args.d.ts +0 -6
- 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/image.d.ts +0 -1
- package/dist/types/scripts/memory.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
|
@@ -0,0 +1,212 @@
|
|
|
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 simple sequential multi-agent system
|
|
15
|
+
*
|
|
16
|
+
* Graph structure:
|
|
17
|
+
* START -> agent_a -> agent_b -> agent_c -> END
|
|
18
|
+
*
|
|
19
|
+
* No conditions, no tools, just automatic sequential flow
|
|
20
|
+
*/
|
|
21
|
+
async function testSequentialMultiAgent() {
|
|
22
|
+
console.log('Testing Sequential Multi-Agent System (A → B → C)...\n');
|
|
23
|
+
|
|
24
|
+
// Set up content aggregator
|
|
25
|
+
const { contentParts, aggregateContent } = createContentAggregator();
|
|
26
|
+
|
|
27
|
+
// Define three simple agents
|
|
28
|
+
const agents: t.AgentInputs[] = [
|
|
29
|
+
{
|
|
30
|
+
agentId: 'agent_a',
|
|
31
|
+
provider: Providers.ANTHROPIC,
|
|
32
|
+
clientOptions: {
|
|
33
|
+
modelName: 'claude-3-5-sonnet-latest',
|
|
34
|
+
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
35
|
+
},
|
|
36
|
+
instructions: `You are Agent A, the first agent in a sequential workflow.
|
|
37
|
+
Your job is to:
|
|
38
|
+
1. Receive the initial user request
|
|
39
|
+
2. Process it and add your perspective (keep it brief, 2-3 sentences)
|
|
40
|
+
3. Pass it along to Agent B
|
|
41
|
+
|
|
42
|
+
Start your response with "AGENT A:" and end with "Passing to Agent B..."`,
|
|
43
|
+
maxContextTokens: 8000,
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
agentId: 'agent_b',
|
|
47
|
+
provider: Providers.ANTHROPIC,
|
|
48
|
+
clientOptions: {
|
|
49
|
+
modelName: 'claude-3-5-sonnet-latest',
|
|
50
|
+
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
51
|
+
},
|
|
52
|
+
instructions: `You are Agent B, the second agent in a sequential workflow.
|
|
53
|
+
Your job is to:
|
|
54
|
+
1. Receive the context from Agent A
|
|
55
|
+
2. Add your own analysis or perspective (keep it brief, 2-3 sentences)
|
|
56
|
+
3. Pass it along to Agent C
|
|
57
|
+
|
|
58
|
+
Start your response with "AGENT B:" and end with "Passing to Agent C..."`,
|
|
59
|
+
maxContextTokens: 8000,
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
agentId: 'agent_c',
|
|
63
|
+
provider: Providers.ANTHROPIC,
|
|
64
|
+
clientOptions: {
|
|
65
|
+
modelName: 'claude-3-5-sonnet-latest',
|
|
66
|
+
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
67
|
+
},
|
|
68
|
+
instructions: `You are Agent C, the final agent in a sequential workflow.
|
|
69
|
+
Your job is to:
|
|
70
|
+
1. Receive the context from Agents A and B
|
|
71
|
+
2. Provide a final summary or conclusion based on all previous inputs
|
|
72
|
+
3. Complete the workflow
|
|
73
|
+
|
|
74
|
+
Start your response with "AGENT C:" and end with "Workflow complete."`,
|
|
75
|
+
maxContextTokens: 8000,
|
|
76
|
+
},
|
|
77
|
+
];
|
|
78
|
+
|
|
79
|
+
// Define sequential edges using 'direct' type to avoid tool creation
|
|
80
|
+
// This creates automatic transitions without requiring agents to call tools
|
|
81
|
+
const edges: t.GraphEdge[] = [
|
|
82
|
+
{
|
|
83
|
+
from: 'agent_a',
|
|
84
|
+
to: 'agent_b',
|
|
85
|
+
edgeType: 'direct', // This creates direct edges without tools
|
|
86
|
+
description: 'Automatic transition from A to B',
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
from: 'agent_b',
|
|
90
|
+
to: 'agent_c',
|
|
91
|
+
edgeType: 'direct', // This creates direct edges without tools
|
|
92
|
+
description: 'Automatic transition from B to C',
|
|
93
|
+
},
|
|
94
|
+
];
|
|
95
|
+
|
|
96
|
+
// Track agent progression
|
|
97
|
+
let currentAgent = '';
|
|
98
|
+
|
|
99
|
+
// Create custom handlers
|
|
100
|
+
const customHandlers = {
|
|
101
|
+
[GraphEvents.TOOL_END]: new ToolEndHandler(),
|
|
102
|
+
[GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
|
|
103
|
+
[GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
|
|
104
|
+
[GraphEvents.ON_RUN_STEP]: {
|
|
105
|
+
handle: (
|
|
106
|
+
event: GraphEvents.ON_RUN_STEP,
|
|
107
|
+
data: t.StreamEventData
|
|
108
|
+
): void => {
|
|
109
|
+
const runStepData = data as any;
|
|
110
|
+
if (runStepData?.name) {
|
|
111
|
+
currentAgent = runStepData.name;
|
|
112
|
+
console.log(`\n→ ${currentAgent} is processing...`);
|
|
113
|
+
}
|
|
114
|
+
aggregateContent({ event, data: data as t.RunStep });
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
[GraphEvents.ON_RUN_STEP_COMPLETED]: {
|
|
118
|
+
handle: (
|
|
119
|
+
event: GraphEvents.ON_RUN_STEP_COMPLETED,
|
|
120
|
+
data: t.StreamEventData
|
|
121
|
+
): void => {
|
|
122
|
+
const runStepData = data as any;
|
|
123
|
+
if (runStepData?.name) {
|
|
124
|
+
console.log(`✓ ${runStepData.name} completed`);
|
|
125
|
+
}
|
|
126
|
+
aggregateContent({
|
|
127
|
+
event,
|
|
128
|
+
data: data as unknown as { result: t.ToolEndEvent },
|
|
129
|
+
});
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
[GraphEvents.ON_RUN_STEP_DELTA]: {
|
|
133
|
+
handle: (
|
|
134
|
+
event: GraphEvents.ON_RUN_STEP_DELTA,
|
|
135
|
+
data: t.StreamEventData
|
|
136
|
+
): void => {
|
|
137
|
+
aggregateContent({ event, data: data as t.RunStepDeltaEvent });
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
[GraphEvents.ON_MESSAGE_DELTA]: {
|
|
141
|
+
handle: (
|
|
142
|
+
event: GraphEvents.ON_MESSAGE_DELTA,
|
|
143
|
+
data: t.StreamEventData
|
|
144
|
+
): void => {
|
|
145
|
+
aggregateContent({ event, data: data as t.MessageDeltaEvent });
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
// Create multi-agent run configuration
|
|
151
|
+
const runConfig: t.RunConfig = {
|
|
152
|
+
runId: `sequential-multi-agent-${Date.now()}`,
|
|
153
|
+
graphConfig: {
|
|
154
|
+
type: 'multi-agent',
|
|
155
|
+
agents,
|
|
156
|
+
edges,
|
|
157
|
+
},
|
|
158
|
+
customHandlers,
|
|
159
|
+
returnContent: true,
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
try {
|
|
163
|
+
// Create and execute the run
|
|
164
|
+
const run = await Run.create(runConfig);
|
|
165
|
+
|
|
166
|
+
// Test with a simple question
|
|
167
|
+
const userMessage =
|
|
168
|
+
'What are the key considerations for building a recommendation system?';
|
|
169
|
+
conversationHistory.push(new HumanMessage(userMessage));
|
|
170
|
+
|
|
171
|
+
console.log(`User: "${userMessage}"\n`);
|
|
172
|
+
console.log('Starting sequential workflow...\n');
|
|
173
|
+
|
|
174
|
+
const config = {
|
|
175
|
+
configurable: {
|
|
176
|
+
thread_id: 'sequential-conversation-1',
|
|
177
|
+
},
|
|
178
|
+
streamMode: 'values',
|
|
179
|
+
version: 'v2' as const,
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
// Process with streaming
|
|
183
|
+
const inputs = {
|
|
184
|
+
messages: conversationHistory,
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
const finalContentParts = await run.processStream(inputs, config);
|
|
188
|
+
const finalMessages = run.getRunMessages();
|
|
189
|
+
|
|
190
|
+
if (finalMessages) {
|
|
191
|
+
conversationHistory.push(...finalMessages);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
console.log('\n\n=== Final Output ===');
|
|
195
|
+
console.log('Sequential flow completed successfully!');
|
|
196
|
+
console.log(`Total content parts: ${contentParts.length}`);
|
|
197
|
+
|
|
198
|
+
// Display the sequential responses
|
|
199
|
+
const aiMessages = conversationHistory.filter(
|
|
200
|
+
(msg) => msg._getType() === 'ai'
|
|
201
|
+
);
|
|
202
|
+
aiMessages.forEach((msg, index) => {
|
|
203
|
+
console.log(`\n--- Response ${index + 1} ---`);
|
|
204
|
+
console.log(msg.content);
|
|
205
|
+
});
|
|
206
|
+
} catch (error) {
|
|
207
|
+
console.error('Error in sequential multi-agent test:', error);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Run the test
|
|
212
|
+
testSequentialMultiAgent();
|
|
@@ -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();
|