@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,135 @@
|
|
|
1
|
+
import { config } from 'dotenv';
|
|
2
|
+
config();
|
|
3
|
+
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { tool } from '@langchain/core/tools';
|
|
6
|
+
import { ChatAnthropic } from '@langchain/anthropic';
|
|
7
|
+
import { createReactAgent } from '@langchain/langgraph/prebuilt';
|
|
8
|
+
import {
|
|
9
|
+
StateGraph,
|
|
10
|
+
MessagesAnnotation,
|
|
11
|
+
Command,
|
|
12
|
+
START,
|
|
13
|
+
getCurrentTaskInput,
|
|
14
|
+
END,
|
|
15
|
+
} from '@langchain/langgraph';
|
|
16
|
+
import { ToolMessage } from '@langchain/core/messages';
|
|
17
|
+
|
|
18
|
+
interface CreateHandoffToolParams {
|
|
19
|
+
agentName: string;
|
|
20
|
+
description?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const createHandoffTool = ({
|
|
24
|
+
agentName,
|
|
25
|
+
description,
|
|
26
|
+
}: CreateHandoffToolParams) => {
|
|
27
|
+
const toolName = `transfer_to_${agentName}`;
|
|
28
|
+
const toolDescription = description || `Ask agent '${agentName}' for help`;
|
|
29
|
+
|
|
30
|
+
const handoffTool = tool(
|
|
31
|
+
async (_, config) => {
|
|
32
|
+
const toolMessage = new ToolMessage({
|
|
33
|
+
content: `Successfully transferred to ${agentName}`,
|
|
34
|
+
name: toolName,
|
|
35
|
+
tool_call_id: config.toolCall.id,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// inject the current agent state
|
|
39
|
+
const state =
|
|
40
|
+
getCurrentTaskInput() as (typeof MessagesAnnotation)['State'];
|
|
41
|
+
return new Command({
|
|
42
|
+
goto: agentName,
|
|
43
|
+
update: { messages: state.messages.concat(toolMessage) },
|
|
44
|
+
graph: Command.PARENT,
|
|
45
|
+
});
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
name: toolName,
|
|
49
|
+
schema: z.object({}),
|
|
50
|
+
description: toolDescription,
|
|
51
|
+
}
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
return handoffTool;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const bookHotel = tool(
|
|
58
|
+
async (input: { hotel_name: string }) => {
|
|
59
|
+
return `Successfully booked a stay at ${input.hotel_name}.`;
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
name: 'book_hotel',
|
|
63
|
+
description: 'Book a hotel',
|
|
64
|
+
schema: z.object({
|
|
65
|
+
hotel_name: z.string().describe('The name of the hotel to book'),
|
|
66
|
+
}),
|
|
67
|
+
}
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
const bookFlight = tool(
|
|
71
|
+
async (input: { from_airport: string; to_airport: string }) => {
|
|
72
|
+
return `Successfully booked a flight from ${input.from_airport} to ${input.to_airport}.`;
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
name: 'book_flight',
|
|
76
|
+
description: 'Book a flight',
|
|
77
|
+
schema: z.object({
|
|
78
|
+
from_airport: z.string().describe('The departure airport code'),
|
|
79
|
+
to_airport: z.string().describe('The arrival airport code'),
|
|
80
|
+
}),
|
|
81
|
+
}
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
const transferToHotelAssistant = createHandoffTool({
|
|
85
|
+
agentName: 'hotel_assistant',
|
|
86
|
+
description: 'Transfer user to the hotel-booking assistant.',
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const transferToFlightAssistant = createHandoffTool({
|
|
90
|
+
agentName: 'flight_assistant',
|
|
91
|
+
description: 'Transfer user to the flight-booking assistant.',
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const llm = new ChatAnthropic({
|
|
95
|
+
modelName: 'claude-3-5-sonnet-latest',
|
|
96
|
+
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
const flightAssistant = createReactAgent({
|
|
100
|
+
llm,
|
|
101
|
+
tools: [bookFlight, transferToHotelAssistant],
|
|
102
|
+
prompt: 'You are a flight booking assistant',
|
|
103
|
+
name: 'flight_assistant',
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const hotelAssistant = createReactAgent({
|
|
107
|
+
llm,
|
|
108
|
+
tools: [bookHotel, transferToFlightAssistant],
|
|
109
|
+
prompt: 'You are a hotel booking assistant',
|
|
110
|
+
name: 'hotel_assistant',
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
const multiAgentGraph = new StateGraph(MessagesAnnotation)
|
|
114
|
+
.addNode('flight_assistant', flightAssistant, {
|
|
115
|
+
ends: ['hotel_assistant', END],
|
|
116
|
+
})
|
|
117
|
+
.addNode('hotel_assistant', hotelAssistant, {
|
|
118
|
+
ends: ['flight_assistant', END],
|
|
119
|
+
})
|
|
120
|
+
.addEdge(START, 'flight_assistant')
|
|
121
|
+
.compile();
|
|
122
|
+
|
|
123
|
+
const stream = await multiAgentGraph.stream({
|
|
124
|
+
messages: [
|
|
125
|
+
{
|
|
126
|
+
role: 'user',
|
|
127
|
+
content: 'book a flight from BOS to JFK and a stay at McKittrick Hotel',
|
|
128
|
+
},
|
|
129
|
+
],
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
for await (const chunk of stream) {
|
|
133
|
+
console.log(chunk);
|
|
134
|
+
console.log('\n');
|
|
135
|
+
}
|
package/src/scripts/image.ts
CHANGED
|
@@ -5,14 +5,17 @@ import { HumanMessage, AIMessage, BaseMessage } from '@langchain/core/messages';
|
|
|
5
5
|
import type { RunnableConfig } from '@langchain/core/runnables';
|
|
6
6
|
import type * as t from '@/types';
|
|
7
7
|
import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
ToolEndHandler,
|
|
10
|
+
ModelEndHandler,
|
|
11
|
+
createMetadataAggregator,
|
|
12
|
+
} from '@/events';
|
|
9
13
|
import { fetchRandomImageTool, fetchRandomImageURL } from '@/tools/example';
|
|
10
14
|
import { getLLMConfig } from '@/utils/llmConfig';
|
|
11
15
|
import { getArgs } from '@/scripts/args';
|
|
12
16
|
import { GraphEvents } from '@/common';
|
|
13
17
|
import { Run } from '@/run';
|
|
14
18
|
|
|
15
|
-
|
|
16
19
|
const conversationHistory: BaseMessage[] = [];
|
|
17
20
|
|
|
18
21
|
async function testCodeExecution(): Promise<void> {
|
|
@@ -23,38 +26,57 @@ async function testCodeExecution(): Promise<void> {
|
|
|
23
26
|
[GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
|
|
24
27
|
[GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
|
|
25
28
|
[GraphEvents.ON_RUN_STEP_COMPLETED]: {
|
|
26
|
-
handle: (
|
|
29
|
+
handle: (
|
|
30
|
+
event: GraphEvents.ON_RUN_STEP_COMPLETED,
|
|
31
|
+
data: t.StreamEventData
|
|
32
|
+
): void => {
|
|
27
33
|
console.log('====== ON_RUN_STEP_COMPLETED ======');
|
|
28
34
|
console.dir(data, { depth: null });
|
|
29
|
-
aggregateContent({
|
|
30
|
-
|
|
35
|
+
aggregateContent({
|
|
36
|
+
event,
|
|
37
|
+
data: data as unknown as { result: t.ToolEndEvent },
|
|
38
|
+
});
|
|
39
|
+
},
|
|
31
40
|
},
|
|
32
41
|
[GraphEvents.ON_RUN_STEP]: {
|
|
33
|
-
handle: (
|
|
42
|
+
handle: (
|
|
43
|
+
event: GraphEvents.ON_RUN_STEP,
|
|
44
|
+
data: t.StreamEventData
|
|
45
|
+
): void => {
|
|
34
46
|
console.log('====== ON_RUN_STEP ======');
|
|
35
47
|
console.dir(data, { depth: null });
|
|
36
48
|
aggregateContent({ event, data: data as t.RunStep });
|
|
37
|
-
}
|
|
49
|
+
},
|
|
38
50
|
},
|
|
39
51
|
[GraphEvents.ON_RUN_STEP_DELTA]: {
|
|
40
|
-
handle: (
|
|
52
|
+
handle: (
|
|
53
|
+
event: GraphEvents.ON_RUN_STEP_DELTA,
|
|
54
|
+
data: t.StreamEventData
|
|
55
|
+
): void => {
|
|
41
56
|
console.log('====== ON_RUN_STEP_DELTA ======');
|
|
42
57
|
console.dir(data, { depth: null });
|
|
43
58
|
aggregateContent({ event, data: data as t.RunStepDeltaEvent });
|
|
44
|
-
}
|
|
59
|
+
},
|
|
45
60
|
},
|
|
46
61
|
[GraphEvents.ON_MESSAGE_DELTA]: {
|
|
47
|
-
handle: (
|
|
62
|
+
handle: (
|
|
63
|
+
event: GraphEvents.ON_MESSAGE_DELTA,
|
|
64
|
+
data: t.StreamEventData
|
|
65
|
+
): void => {
|
|
48
66
|
console.log('====== ON_MESSAGE_DELTA ======');
|
|
49
67
|
console.dir(data, { depth: null });
|
|
50
68
|
aggregateContent({ event, data: data as t.MessageDeltaEvent });
|
|
51
|
-
}
|
|
69
|
+
},
|
|
52
70
|
},
|
|
53
71
|
[GraphEvents.TOOL_START]: {
|
|
54
|
-
handle: (
|
|
72
|
+
handle: (
|
|
73
|
+
_event: string,
|
|
74
|
+
data: t.StreamEventData,
|
|
75
|
+
metadata?: Record<string, unknown>
|
|
76
|
+
): void => {
|
|
55
77
|
console.log('====== TOOL_START ======');
|
|
56
78
|
console.dir(data, { depth: null });
|
|
57
|
-
}
|
|
79
|
+
},
|
|
58
80
|
},
|
|
59
81
|
};
|
|
60
82
|
|
|
@@ -67,14 +89,19 @@ async function testCodeExecution(): Promise<void> {
|
|
|
67
89
|
llmConfig,
|
|
68
90
|
tools: [fetchRandomImageTool],
|
|
69
91
|
// tools: [fetchRandomImageURL],
|
|
70
|
-
instructions:
|
|
92
|
+
instructions:
|
|
93
|
+
'You are a friendly AI assistant with internet capabilities. Always address the user by their name.',
|
|
71
94
|
additional_instructions: `The user's name is ${userName} and they are located in ${location}.`,
|
|
72
95
|
},
|
|
73
96
|
returnContent: true,
|
|
74
97
|
customHandlers,
|
|
75
98
|
});
|
|
76
99
|
|
|
77
|
-
const config: Partial<RunnableConfig> & {
|
|
100
|
+
const config: Partial<RunnableConfig> & {
|
|
101
|
+
version: 'v1' | 'v2';
|
|
102
|
+
run_id?: string;
|
|
103
|
+
streamMode: string;
|
|
104
|
+
} = {
|
|
78
105
|
configurable: {
|
|
79
106
|
provider,
|
|
80
107
|
thread_id: 'conversation-num-1',
|
|
@@ -109,7 +136,9 @@ async function testCodeExecution(): Promise<void> {
|
|
|
109
136
|
inputs = {
|
|
110
137
|
messages: conversationHistory,
|
|
111
138
|
};
|
|
112
|
-
const finalContentParts2 = await run.processStream(inputs, config, {
|
|
139
|
+
const finalContentParts2 = await run.processStream(inputs, config, {
|
|
140
|
+
keepContent: true,
|
|
141
|
+
});
|
|
113
142
|
const finalMessages2 = run.getRunMessages();
|
|
114
143
|
if (finalMessages2) {
|
|
115
144
|
conversationHistory.push(...finalMessages2);
|
|
@@ -119,12 +148,15 @@ async function testCodeExecution(): Promise<void> {
|
|
|
119
148
|
|
|
120
149
|
const { handleLLMEnd, collected } = createMetadataAggregator();
|
|
121
150
|
const titleResult = await run.generateTitle({
|
|
151
|
+
provider,
|
|
122
152
|
inputText: userMessage2,
|
|
123
153
|
contentParts,
|
|
124
154
|
chainOptions: {
|
|
125
|
-
callbacks: [
|
|
126
|
-
|
|
127
|
-
|
|
155
|
+
callbacks: [
|
|
156
|
+
{
|
|
157
|
+
handleLLMEnd,
|
|
158
|
+
},
|
|
159
|
+
],
|
|
128
160
|
},
|
|
129
161
|
});
|
|
130
162
|
console.log('Generated Title:', titleResult);
|
|
@@ -143,4 +175,4 @@ testCodeExecution().catch((err) => {
|
|
|
143
175
|
console.log('Conversation history:');
|
|
144
176
|
console.dir(conversationHistory, { depth: null });
|
|
145
177
|
process.exit(1);
|
|
146
|
-
});
|
|
178
|
+
});
|
|
@@ -0,0 +1,278 @@
|
|
|
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
|
+
* Helper function to create sequential chain edges with buffer string prompts
|
|
17
|
+
*
|
|
18
|
+
* @param agentIds - Array of agent IDs in order of execution
|
|
19
|
+
* @returns Array of edges configured for sequential chain with buffer prompts
|
|
20
|
+
*/
|
|
21
|
+
function createSequentialChainEdges(agentIds: string[]): t.GraphEdge[] {
|
|
22
|
+
const edges: t.GraphEdge[] = [];
|
|
23
|
+
|
|
24
|
+
for (let i = 0; i < agentIds.length - 1; i++) {
|
|
25
|
+
const fromAgent = agentIds[i];
|
|
26
|
+
const toAgent = agentIds[i + 1];
|
|
27
|
+
|
|
28
|
+
edges.push({
|
|
29
|
+
from: fromAgent,
|
|
30
|
+
to: toAgent,
|
|
31
|
+
edgeType: 'direct',
|
|
32
|
+
// Use a prompt function to create the buffer string from all previous results
|
|
33
|
+
prompt: (messages: BaseMessage[], startIndex: number) => {
|
|
34
|
+
// Get only the messages from this run (after startIndex)
|
|
35
|
+
const runMessages = messages.slice(startIndex);
|
|
36
|
+
|
|
37
|
+
// Create buffer string from run messages
|
|
38
|
+
const bufferString = getBufferString(runMessages);
|
|
39
|
+
|
|
40
|
+
// Format the prompt for the next agent
|
|
41
|
+
return `Based on the following conversation and analysis from previous agents, please provide your insights:\n\n${bufferString}\n\nPlease add your specific expertise and perspective to this discussion.`;
|
|
42
|
+
},
|
|
43
|
+
// Critical: exclude previous results so only the prompt is passed
|
|
44
|
+
excludeResults: true,
|
|
45
|
+
description: `Sequential chain from ${fromAgent} to ${toAgent}`,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return edges;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const conversationHistory: BaseMessage[] = [];
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Example of sequential agent chain mimicking the old chain behavior
|
|
56
|
+
*
|
|
57
|
+
* Graph structure:
|
|
58
|
+
* START -> researcher -> analyst -> reviewer -> summarizer -> END
|
|
59
|
+
*
|
|
60
|
+
* Each agent receives a buffer string of all previous results
|
|
61
|
+
*/
|
|
62
|
+
async function testSequentialAgentChain() {
|
|
63
|
+
console.log('Testing Sequential Agent Chain (Old Chain Pattern)...\n');
|
|
64
|
+
|
|
65
|
+
// Set up content aggregator
|
|
66
|
+
const { contentParts, aggregateContent } = createContentAggregator();
|
|
67
|
+
|
|
68
|
+
// Define four agents with specific roles
|
|
69
|
+
const agents: t.AgentInputs[] = [
|
|
70
|
+
{
|
|
71
|
+
agentId: 'researcher',
|
|
72
|
+
provider: Providers.ANTHROPIC,
|
|
73
|
+
clientOptions: {
|
|
74
|
+
modelName: 'claude-3-5-sonnet-latest',
|
|
75
|
+
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
76
|
+
},
|
|
77
|
+
instructions: `You are a Research Agent specializing in gathering initial information.
|
|
78
|
+
Your role is to:
|
|
79
|
+
1. Identify key aspects of the user's query
|
|
80
|
+
2. List important factors to consider
|
|
81
|
+
3. Provide initial research findings
|
|
82
|
+
|
|
83
|
+
Format your response with clear sections and bullet points.
|
|
84
|
+
Start with "RESEARCH FINDINGS:" and be thorough but concise.`,
|
|
85
|
+
maxContextTokens: 8000,
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
agentId: 'analyst',
|
|
89
|
+
provider: Providers.ANTHROPIC,
|
|
90
|
+
clientOptions: {
|
|
91
|
+
modelName: 'claude-3-5-sonnet-latest',
|
|
92
|
+
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
93
|
+
},
|
|
94
|
+
instructions: `You are an Analysis Agent that builds upon research findings.
|
|
95
|
+
Your role is to:
|
|
96
|
+
1. Analyze the research provided by the previous agent
|
|
97
|
+
2. Identify patterns, risks, and opportunities
|
|
98
|
+
3. Provide deeper analytical insights
|
|
99
|
+
|
|
100
|
+
Start with "ANALYSIS:" and structure your response with clear categories.
|
|
101
|
+
Reference specific points from the research when relevant.`,
|
|
102
|
+
maxContextTokens: 8000,
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
agentId: 'reviewer',
|
|
106
|
+
provider: Providers.ANTHROPIC,
|
|
107
|
+
clientOptions: {
|
|
108
|
+
modelName: 'claude-3-5-sonnet-latest',
|
|
109
|
+
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
110
|
+
},
|
|
111
|
+
instructions: `You are a Critical Review Agent that evaluates the work done so far.
|
|
112
|
+
Your role is to:
|
|
113
|
+
1. Review the research and analysis from previous agents
|
|
114
|
+
2. Identify any gaps or areas that need more attention
|
|
115
|
+
3. Suggest improvements or additional considerations
|
|
116
|
+
|
|
117
|
+
Start with "CRITICAL REVIEW:" and be constructive in your feedback.
|
|
118
|
+
Highlight both strengths and areas for improvement.`,
|
|
119
|
+
maxContextTokens: 8000,
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
agentId: 'summarizer',
|
|
123
|
+
provider: Providers.ANTHROPIC,
|
|
124
|
+
clientOptions: {
|
|
125
|
+
modelName: 'claude-3-5-sonnet-latest',
|
|
126
|
+
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
127
|
+
},
|
|
128
|
+
instructions: `You are a Summary Agent that creates the final comprehensive output.
|
|
129
|
+
Your role is to:
|
|
130
|
+
1. Synthesize all insights from the researcher, analyst, and reviewer
|
|
131
|
+
2. Create a cohesive, actionable summary
|
|
132
|
+
3. Provide clear recommendations or conclusions
|
|
133
|
+
|
|
134
|
+
Start with "EXECUTIVE SUMMARY:" followed by key sections.
|
|
135
|
+
End with "KEY RECOMMENDATIONS:" or "CONCLUSIONS:" as appropriate.`,
|
|
136
|
+
maxContextTokens: 8000,
|
|
137
|
+
},
|
|
138
|
+
];
|
|
139
|
+
|
|
140
|
+
// Create sequential chain edges using our helper function
|
|
141
|
+
const agentIds = agents.map((a) => a.agentId);
|
|
142
|
+
const edges = createSequentialChainEdges(agentIds);
|
|
143
|
+
|
|
144
|
+
// Track agent progression
|
|
145
|
+
let currentAgent = '';
|
|
146
|
+
|
|
147
|
+
// Create custom handlers
|
|
148
|
+
const customHandlers = {
|
|
149
|
+
[GraphEvents.TOOL_END]: new ToolEndHandler(),
|
|
150
|
+
[GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
|
|
151
|
+
[GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
|
|
152
|
+
[GraphEvents.ON_RUN_STEP]: {
|
|
153
|
+
handle: (
|
|
154
|
+
event: GraphEvents.ON_RUN_STEP,
|
|
155
|
+
data: t.StreamEventData
|
|
156
|
+
): void => {
|
|
157
|
+
const runStepData = data as any;
|
|
158
|
+
if (runStepData?.name) {
|
|
159
|
+
currentAgent = runStepData.name;
|
|
160
|
+
console.log(`\n→ ${currentAgent} is processing...`);
|
|
161
|
+
}
|
|
162
|
+
aggregateContent({ event, data: data as t.RunStep });
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
[GraphEvents.ON_RUN_STEP_COMPLETED]: {
|
|
166
|
+
handle: (
|
|
167
|
+
event: GraphEvents.ON_RUN_STEP_COMPLETED,
|
|
168
|
+
data: t.StreamEventData
|
|
169
|
+
): void => {
|
|
170
|
+
const runStepData = data as any;
|
|
171
|
+
if (runStepData?.name) {
|
|
172
|
+
console.log(`✓ ${runStepData.name} completed`);
|
|
173
|
+
}
|
|
174
|
+
aggregateContent({
|
|
175
|
+
event,
|
|
176
|
+
data: data as unknown as { result: t.ToolEndEvent },
|
|
177
|
+
});
|
|
178
|
+
},
|
|
179
|
+
},
|
|
180
|
+
[GraphEvents.ON_RUN_STEP_DELTA]: {
|
|
181
|
+
handle: (
|
|
182
|
+
event: GraphEvents.ON_RUN_STEP_DELTA,
|
|
183
|
+
data: t.StreamEventData
|
|
184
|
+
): void => {
|
|
185
|
+
aggregateContent({ event, data: data as t.RunStepDeltaEvent });
|
|
186
|
+
},
|
|
187
|
+
},
|
|
188
|
+
[GraphEvents.ON_MESSAGE_DELTA]: {
|
|
189
|
+
handle: (
|
|
190
|
+
event: GraphEvents.ON_MESSAGE_DELTA,
|
|
191
|
+
data: t.StreamEventData
|
|
192
|
+
): void => {
|
|
193
|
+
aggregateContent({ event, data: data as t.MessageDeltaEvent });
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
// Create multi-agent run configuration
|
|
199
|
+
const runConfig: t.RunConfig = {
|
|
200
|
+
runId: `sequential-chain-${Date.now()}`,
|
|
201
|
+
graphConfig: {
|
|
202
|
+
type: 'multi-agent',
|
|
203
|
+
agents,
|
|
204
|
+
edges,
|
|
205
|
+
},
|
|
206
|
+
customHandlers,
|
|
207
|
+
returnContent: true,
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
try {
|
|
211
|
+
// Create and execute the run
|
|
212
|
+
const run = await Run.create(runConfig);
|
|
213
|
+
|
|
214
|
+
// Test with a complex question that benefits from multiple perspectives
|
|
215
|
+
const userMessage =
|
|
216
|
+
'I want to launch a new mobile app for personal finance management. What should I consider?';
|
|
217
|
+
conversationHistory.push(new HumanMessage(userMessage));
|
|
218
|
+
|
|
219
|
+
console.log(`User: "${userMessage}"\n`);
|
|
220
|
+
console.log('Starting sequential agent chain...\n');
|
|
221
|
+
|
|
222
|
+
const config = {
|
|
223
|
+
configurable: {
|
|
224
|
+
thread_id: 'sequential-chain-1',
|
|
225
|
+
},
|
|
226
|
+
streamMode: 'values',
|
|
227
|
+
version: 'v2' as const,
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
// Process with streaming
|
|
231
|
+
const inputs = {
|
|
232
|
+
messages: conversationHistory,
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
const finalContentParts = await run.processStream(inputs, config);
|
|
236
|
+
const finalMessages = run.getRunMessages();
|
|
237
|
+
|
|
238
|
+
if (finalMessages) {
|
|
239
|
+
conversationHistory.push(...finalMessages);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
console.log('\n\n=== Final Output ===');
|
|
243
|
+
console.log('Sequential chain completed successfully!');
|
|
244
|
+
console.log(`Total content parts: ${contentParts.length}`);
|
|
245
|
+
|
|
246
|
+
// Display the buffer accumulation
|
|
247
|
+
console.log('\n=== Agent Outputs (Buffer Accumulation) ===');
|
|
248
|
+
|
|
249
|
+
const aiMessages = conversationHistory.filter(
|
|
250
|
+
(msg) => msg._getType() === 'ai'
|
|
251
|
+
);
|
|
252
|
+
|
|
253
|
+
aiMessages.forEach((msg, index) => {
|
|
254
|
+
console.log(`\n--- Agent ${index + 1}: ${agentIds[index]} ---`);
|
|
255
|
+
console.log(msg.content);
|
|
256
|
+
|
|
257
|
+
// Show buffer preview for next agent (except last)
|
|
258
|
+
if (index < aiMessages.length - 1) {
|
|
259
|
+
const bufferSoFar = getBufferString(aiMessages.slice(0, index + 1));
|
|
260
|
+
console.log(
|
|
261
|
+
`\n[Buffer passed to ${agentIds[index + 1]}]: ${bufferSoFar.slice(0, 150)}...`
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
// Demonstrate that each agent built upon previous results
|
|
267
|
+
console.log('\n=== Chain Analysis ===');
|
|
268
|
+
console.log('1. Researcher provided initial findings');
|
|
269
|
+
console.log('2. Analyst built upon research with deeper insights');
|
|
270
|
+
console.log('3. Reviewer critiqued and identified gaps');
|
|
271
|
+
console.log('4. Summarizer synthesized everything into actionable output');
|
|
272
|
+
} catch (error) {
|
|
273
|
+
console.error('Error in sequential agent chain test:', error);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Run the test
|
|
278
|
+
testSequentialAgentChain();
|