@librechat/agents 3.0.0 → 3.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/common/enum.cjs +0 -1
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/llm/providers.cjs +0 -2
- package/dist/cjs/llm/providers.cjs.map +1 -1
- package/dist/cjs/main.cjs +2 -0
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/tools/Calculator.cjs +45 -0
- package/dist/cjs/tools/Calculator.cjs.map +1 -0
- package/dist/esm/common/enum.mjs +0 -1
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/llm/providers.mjs +0 -2
- package/dist/esm/llm/providers.mjs.map +1 -1
- package/dist/esm/main.mjs +1 -0
- package/dist/esm/main.mjs.map +1 -1
- package/dist/esm/tools/Calculator.mjs +24 -0
- package/dist/esm/tools/Calculator.mjs.map +1 -0
- package/dist/types/common/enum.d.ts +0 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/tools/Calculator.d.ts +8 -0
- package/dist/types/types/llm.d.ts +1 -6
- package/package.json +5 -3
- package/src/common/enum.ts +0 -1
- package/src/index.ts +1 -0
- package/src/llm/providers.ts +0 -2
- package/src/scripts/abort.ts +34 -15
- package/src/scripts/cli.ts +25 -20
- package/src/scripts/cli2.ts +23 -15
- package/src/scripts/cli3.ts +35 -29
- package/src/scripts/cli4.ts +1 -2
- package/src/scripts/cli5.ts +1 -2
- package/src/scripts/code_exec.ts +1 -2
- package/src/scripts/code_exec_simple.ts +1 -2
- package/src/scripts/content.ts +33 -15
- package/src/scripts/simple.ts +1 -2
- package/src/scripts/stream.ts +33 -15
- package/src/scripts/test-tools-before-handoff.ts +17 -28
- package/src/scripts/tools.ts +4 -6
- package/src/specs/anthropic.simple.test.ts +1 -1
- package/src/specs/azure.simple.test.ts +1 -1
- package/src/specs/openai.simple.test.ts +1 -1
- package/src/specs/openrouter.simple.test.ts +1 -1
- package/src/tools/Calculator.ts +25 -0
- package/src/types/llm.ts +0 -6
- package/dist/types/tools/example.d.ts +0 -78
- package/src/proto/CollabGraph.ts +0 -269
- package/src/proto/TaskManager.ts +0 -243
- package/src/proto/collab.ts +0 -200
- package/src/proto/collab_design.ts +0 -184
- package/src/proto/collab_design_v2.ts +0 -224
- package/src/proto/collab_design_v3.ts +0 -255
- package/src/proto/collab_design_v4.ts +0 -220
- package/src/proto/collab_design_v5.ts +0 -251
- package/src/proto/collab_graph.ts +0 -181
- package/src/proto/collab_original.ts +0 -123
- package/src/proto/example.ts +0 -93
- package/src/proto/example_new.ts +0 -68
- package/src/proto/example_old.ts +0 -201
- package/src/proto/example_test.ts +0 -152
- package/src/proto/example_test_anthropic.ts +0 -100
- package/src/proto/log_stream.ts +0 -202
- package/src/proto/main_collab_community_event.ts +0 -133
- package/src/proto/main_collab_design_v2.ts +0 -96
- package/src/proto/main_collab_design_v4.ts +0 -100
- package/src/proto/main_collab_design_v5.ts +0 -135
- package/src/proto/main_collab_global_analysis.ts +0 -122
- package/src/proto/main_collab_hackathon_event.ts +0 -153
- package/src/proto/main_collab_space_mission.ts +0 -153
- package/src/proto/main_philosophy.ts +0 -210
- package/src/proto/original_script.ts +0 -126
- package/src/proto/standard.ts +0 -100
- package/src/proto/stream.ts +0 -56
- package/src/proto/tasks.ts +0 -118
- package/src/proto/tools/global_analysis_tools.ts +0 -86
- package/src/proto/tools/space_mission_tools.ts +0 -60
- package/src/proto/vertexai.ts +0 -54
- package/src/tools/example.ts +0 -129
|
@@ -1,224 +0,0 @@
|
|
|
1
|
-
// src/collab.ts
|
|
2
|
-
import 'dotenv/config';
|
|
3
|
-
import { BaseMessage, HumanMessage } from '@langchain/core/messages';
|
|
4
|
-
import { END, StateGraphArgs, START, StateGraph } from '@langchain/langgraph';
|
|
5
|
-
import { AgentExecutor, createOpenAIToolsAgent } from 'langchain/agents';
|
|
6
|
-
import {
|
|
7
|
-
ChatPromptTemplate,
|
|
8
|
-
MessagesPlaceholder,
|
|
9
|
-
} from '@langchain/core/prompts';
|
|
10
|
-
import { Runnable, RunnableConfig } from '@langchain/core/runnables';
|
|
11
|
-
import { JsonOutputToolsParser } from 'langchain/output_parsers';
|
|
12
|
-
import { HandlerRegistry } from '@/events';
|
|
13
|
-
import { ChatOpenAI } from '@langchain/openai';
|
|
14
|
-
import { ChatBedrockConverse } from '@langchain/aws';
|
|
15
|
-
import { ChatAnthropic } from '@langchain/anthropic';
|
|
16
|
-
import { ChatMistralAI } from '@langchain/mistralai';
|
|
17
|
-
import { ChatVertexAI } from '@langchain/google-vertexai';
|
|
18
|
-
import { BedrockChat } from '@langchain/community/chat_models/bedrock/web';
|
|
19
|
-
import { Providers } from '@/common';
|
|
20
|
-
|
|
21
|
-
interface AgentStateChannels {
|
|
22
|
-
messages: BaseMessage[];
|
|
23
|
-
next: string;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export interface Member {
|
|
27
|
-
name: string;
|
|
28
|
-
systemPrompt: string;
|
|
29
|
-
tools: any[];
|
|
30
|
-
llmConfig: LLMConfig;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
interface LLMConfig {
|
|
34
|
-
provider: Providers;
|
|
35
|
-
[key: string]: any;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
interface SupervisorConfig {
|
|
39
|
-
systemPrompt?: string;
|
|
40
|
-
llmConfig: LLMConfig;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const llmProviders: Record<Providers, any> = {
|
|
44
|
-
[Providers.OPENAI]: ChatOpenAI,
|
|
45
|
-
[Providers.VERTEXAI]: ChatVertexAI,
|
|
46
|
-
[Providers.BEDROCK_LEGACY]: BedrockChat,
|
|
47
|
-
[Providers.MISTRALAI]: ChatMistralAI,
|
|
48
|
-
[Providers.BEDROCK]: ChatBedrockConverse,
|
|
49
|
-
[Providers.ANTHROPIC]: ChatAnthropic,
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
export class CollaborativeProcessor {
|
|
53
|
-
private graph: Runnable | null = null;
|
|
54
|
-
private handlerRegistry: HandlerRegistry;
|
|
55
|
-
private members: Member[];
|
|
56
|
-
private supervisorConfig: SupervisorConfig;
|
|
57
|
-
|
|
58
|
-
constructor(
|
|
59
|
-
members: Member[],
|
|
60
|
-
supervisorConfig: SupervisorConfig,
|
|
61
|
-
customHandlers?: Record<string, any>
|
|
62
|
-
) {
|
|
63
|
-
this.members = members;
|
|
64
|
-
this.supervisorConfig = supervisorConfig;
|
|
65
|
-
this.handlerRegistry = new HandlerRegistry();
|
|
66
|
-
if (customHandlers) {
|
|
67
|
-
for (const [eventType, handler] of Object.entries(customHandlers)) {
|
|
68
|
-
this.handlerRegistry.register(eventType, handler);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
async initialize(): Promise<void> {
|
|
74
|
-
this.graph = await this.createGraph();
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
private async createGraph(): Promise<Runnable> {
|
|
78
|
-
const agentStateChannels: StateGraphArgs['channels'] = {
|
|
79
|
-
messages: {
|
|
80
|
-
value: (x?: BaseMessage[], y?: BaseMessage[]) => (x ?? []).concat(y ?? []),
|
|
81
|
-
default: () => [],
|
|
82
|
-
},
|
|
83
|
-
next: {
|
|
84
|
-
value: (x?: string, y?: string) => y ?? x ?? END,
|
|
85
|
-
default: () => END,
|
|
86
|
-
},
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
async function createAgent(
|
|
90
|
-
llmConfig: LLMConfig,
|
|
91
|
-
tools: any[],
|
|
92
|
-
systemPrompt: string
|
|
93
|
-
): Promise<AgentExecutor> {
|
|
94
|
-
const { provider, ...clientOptions } = llmConfig;
|
|
95
|
-
const LLMClass = llmProviders[provider];
|
|
96
|
-
if (!LLMClass) {
|
|
97
|
-
throw new Error(`Unsupported LLM provider: ${provider}`);
|
|
98
|
-
}
|
|
99
|
-
const llm = new LLMClass(clientOptions);
|
|
100
|
-
|
|
101
|
-
const prompt = await ChatPromptTemplate.fromMessages([
|
|
102
|
-
['system', systemPrompt],
|
|
103
|
-
new MessagesPlaceholder('messages'),
|
|
104
|
-
new MessagesPlaceholder('agent_scratchpad'),
|
|
105
|
-
]);
|
|
106
|
-
const agent = await createOpenAIToolsAgent({ llm, tools, prompt });
|
|
107
|
-
return new AgentExecutor({ agent, tools });
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const memberNames = this.members.map(member => member.name);
|
|
111
|
-
|
|
112
|
-
const systemPrompt = this.supervisorConfig.systemPrompt ||
|
|
113
|
-
'You are a supervisor tasked with managing a conversation between the' +
|
|
114
|
-
' following workers: {members}. Given the following user request,' +
|
|
115
|
-
' respond with the worker to act next. Each worker will perform a' +
|
|
116
|
-
' task and respond with their results and status. When finished,' +
|
|
117
|
-
' respond with FINISH.';
|
|
118
|
-
const options = [END, ...memberNames];
|
|
119
|
-
|
|
120
|
-
const functionDef = {
|
|
121
|
-
name: 'route',
|
|
122
|
-
description: 'Select the next role.',
|
|
123
|
-
parameters: {
|
|
124
|
-
title: 'routeSchema',
|
|
125
|
-
type: 'object',
|
|
126
|
-
properties: {
|
|
127
|
-
next: {
|
|
128
|
-
title: 'Next',
|
|
129
|
-
anyOf: [
|
|
130
|
-
{ enum: options },
|
|
131
|
-
],
|
|
132
|
-
},
|
|
133
|
-
},
|
|
134
|
-
required: ['next'],
|
|
135
|
-
},
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
const toolDef = {
|
|
139
|
-
type: 'function',
|
|
140
|
-
function: functionDef,
|
|
141
|
-
} as const;
|
|
142
|
-
|
|
143
|
-
const prompt = ChatPromptTemplate.fromMessages([
|
|
144
|
-
['system', systemPrompt],
|
|
145
|
-
new MessagesPlaceholder('messages'),
|
|
146
|
-
[
|
|
147
|
-
'system',
|
|
148
|
-
'Given the conversation above, who should act next?' +
|
|
149
|
-
' Or should we FINISH? Select one of: {options}',
|
|
150
|
-
],
|
|
151
|
-
]);
|
|
152
|
-
|
|
153
|
-
const formattedPrompt = await prompt.partial({
|
|
154
|
-
options: options.join(', '),
|
|
155
|
-
members: memberNames.join(', '),
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
const { provider, ...clientOptions } = this.supervisorConfig.llmConfig;
|
|
159
|
-
const LLMClass = llmProviders[provider];
|
|
160
|
-
if (!LLMClass) {
|
|
161
|
-
throw new Error(`Unsupported LLM provider for supervisor: ${provider}`);
|
|
162
|
-
}
|
|
163
|
-
const llm = new LLMClass(clientOptions);
|
|
164
|
-
|
|
165
|
-
const supervisorChain = formattedPrompt
|
|
166
|
-
.pipe(llm.bindTools(
|
|
167
|
-
[toolDef],
|
|
168
|
-
{
|
|
169
|
-
tool_choice: { 'type': 'function', 'function': { 'name': 'route' } },
|
|
170
|
-
},
|
|
171
|
-
))
|
|
172
|
-
.pipe(new JsonOutputToolsParser())
|
|
173
|
-
.pipe((x) => (x[0].args));
|
|
174
|
-
|
|
175
|
-
const workflow = new StateGraph({
|
|
176
|
-
channels: agentStateChannels,
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
// Dynamically create agents and add nodes for each member
|
|
180
|
-
for (const member of this.members) {
|
|
181
|
-
const agent = await createAgent(member.llmConfig, member.tools, member.systemPrompt);
|
|
182
|
-
const node = async (
|
|
183
|
-
state: AgentStateChannels,
|
|
184
|
-
config?: RunnableConfig,
|
|
185
|
-
) => {
|
|
186
|
-
const result = await agent.invoke(state, config);
|
|
187
|
-
return {
|
|
188
|
-
messages: [
|
|
189
|
-
new HumanMessage({ content: result.output, name: member.name }),
|
|
190
|
-
],
|
|
191
|
-
};
|
|
192
|
-
};
|
|
193
|
-
workflow.addNode(member.name, node);
|
|
194
|
-
workflow.addEdge(member.name, 'supervisor');
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
workflow.addNode('supervisor', supervisorChain);
|
|
198
|
-
|
|
199
|
-
workflow.addConditionalEdges(
|
|
200
|
-
'supervisor',
|
|
201
|
-
(x: AgentStateChannels) => x.next,
|
|
202
|
-
);
|
|
203
|
-
|
|
204
|
-
workflow.addEdge(START, 'supervisor');
|
|
205
|
-
|
|
206
|
-
return workflow.compile();
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
async processStream(
|
|
210
|
-
inputs: { messages: BaseMessage[] },
|
|
211
|
-
config: Partial<RunnableConfig> & { version: 'v1' | 'v2' },
|
|
212
|
-
) {
|
|
213
|
-
if (!this.graph) {
|
|
214
|
-
throw new Error('CollaborativeProcessor not initialized. Call initialize() first.');
|
|
215
|
-
}
|
|
216
|
-
const stream = this.graph.streamEvents(inputs, config);
|
|
217
|
-
for await (const event of stream) {
|
|
218
|
-
const handler = this.handlerRegistry.getHandler(event.event);
|
|
219
|
-
if (handler) {
|
|
220
|
-
handler.handle(event.event, event.data);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
}
|
|
@@ -1,255 +0,0 @@
|
|
|
1
|
-
// src/collab_design_v2.ts
|
|
2
|
-
import 'dotenv/config';
|
|
3
|
-
import { BaseMessage, HumanMessage, AIMessage } from '@langchain/core/messages';
|
|
4
|
-
import { END, StateGraphArgs, START, StateGraph } from '@langchain/langgraph';
|
|
5
|
-
import { AgentExecutor, createOpenAIToolsAgent } from 'langchain/agents';
|
|
6
|
-
import {
|
|
7
|
-
ChatPromptTemplate,
|
|
8
|
-
MessagesPlaceholder,
|
|
9
|
-
} from '@langchain/core/prompts';
|
|
10
|
-
import { ChatOpenAI } from '@langchain/openai';
|
|
11
|
-
import { Runnable, RunnableConfig } from '@langchain/core/runnables';
|
|
12
|
-
import { JsonOutputToolsParser } from 'langchain/output_parsers';
|
|
13
|
-
import { HandlerRegistry } from '@/events';
|
|
14
|
-
import { ChatBedrockConverse } from '@langchain/aws';
|
|
15
|
-
import { ChatAnthropic } from '@langchain/anthropic';
|
|
16
|
-
import { ChatMistralAI } from '@langchain/mistralai';
|
|
17
|
-
import { ChatVertexAI } from '@langchain/google-vertexai';
|
|
18
|
-
import { BedrockChat } from '@langchain/community/chat_models/bedrock/web';
|
|
19
|
-
import { Providers } from '@/common';
|
|
20
|
-
|
|
21
|
-
interface AgentStateChannels {
|
|
22
|
-
messages: BaseMessage[];
|
|
23
|
-
next: string | string[];
|
|
24
|
-
parallelResults: { [key: string]: string };
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface Member {
|
|
28
|
-
name: string;
|
|
29
|
-
systemPrompt: string;
|
|
30
|
-
tools: any[];
|
|
31
|
-
llmConfig: LLMConfig;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
interface LLMConfig {
|
|
35
|
-
provider: Providers;
|
|
36
|
-
[key: string]: any;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const llmProviders: Record<Providers, any> = {
|
|
40
|
-
[Providers.OPENAI]: ChatOpenAI,
|
|
41
|
-
[Providers.VERTEXAI]: ChatVertexAI,
|
|
42
|
-
[Providers.BEDROCK_LEGACY]: BedrockChat,
|
|
43
|
-
[Providers.MISTRALAI]: ChatMistralAI,
|
|
44
|
-
[Providers.BEDROCK]: ChatBedrockConverse,
|
|
45
|
-
[Providers.ANTHROPIC]: ChatAnthropic,
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
export class CollaborativeProcessor {
|
|
49
|
-
private graph: Runnable | null = null;
|
|
50
|
-
private handlerRegistry: HandlerRegistry;
|
|
51
|
-
private members: Member[];
|
|
52
|
-
|
|
53
|
-
constructor(members: Member[], customHandlers?: Record<string, any>) {
|
|
54
|
-
this.members = members;
|
|
55
|
-
this.handlerRegistry = new HandlerRegistry();
|
|
56
|
-
if (customHandlers) {
|
|
57
|
-
for (const [eventType, handler] of Object.entries(customHandlers)) {
|
|
58
|
-
this.handlerRegistry.register(eventType, handler);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
async initialize(): Promise<void> {
|
|
64
|
-
this.graph = await this.createGraph();
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
private async createGraph(): Promise<Runnable> {
|
|
68
|
-
const agentStateChannels: StateGraphArgs['channels'] = {
|
|
69
|
-
messages: {
|
|
70
|
-
value: (x?: BaseMessage[], y?: BaseMessage[]) => (x ?? []).concat(y ?? []),
|
|
71
|
-
default: () => [],
|
|
72
|
-
},
|
|
73
|
-
next: {
|
|
74
|
-
value: (x?: string | string[], y?: string | string[]) => y ?? x ?? END,
|
|
75
|
-
default: () => END,
|
|
76
|
-
},
|
|
77
|
-
parallelResults: {
|
|
78
|
-
value: (x?: { [key: string]: string }, y?: { [key: string]: string }) => ({ ...x, ...y }),
|
|
79
|
-
default: () => ({}),
|
|
80
|
-
},
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
async function createAgent(
|
|
84
|
-
llmConfig: LLMConfig,
|
|
85
|
-
tools: any[],
|
|
86
|
-
systemPrompt: string
|
|
87
|
-
): Promise<AgentExecutor> {
|
|
88
|
-
const { provider, ...clientOptions } = llmConfig;
|
|
89
|
-
const LLMClass = llmProviders[provider];
|
|
90
|
-
if (!LLMClass) {
|
|
91
|
-
throw new Error(`Unsupported LLM provider: ${provider}`);
|
|
92
|
-
}
|
|
93
|
-
const llm = new LLMClass(clientOptions);
|
|
94
|
-
|
|
95
|
-
const prompt = await ChatPromptTemplate.fromMessages([
|
|
96
|
-
['system', systemPrompt],
|
|
97
|
-
new MessagesPlaceholder('messages'),
|
|
98
|
-
new MessagesPlaceholder('agent_scratchpad'),
|
|
99
|
-
]);
|
|
100
|
-
const agent = await createOpenAIToolsAgent({ llm, tools, prompt });
|
|
101
|
-
return new AgentExecutor({ agent, tools });
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const memberNames = this.members.map(member => member.name);
|
|
105
|
-
|
|
106
|
-
const systemPrompt =
|
|
107
|
-
'You are a supervisor tasked with managing a conversation between the' +
|
|
108
|
-
' following workers: {members}. Given the following user request,' +
|
|
109
|
-
' respond with the worker(s) to act next. You can choose multiple workers' +
|
|
110
|
-
' to act in parallel if appropriate. Each worker will perform a' +
|
|
111
|
-
' task and respond with their results and status. When finished,' +
|
|
112
|
-
' respond with FINISH.';
|
|
113
|
-
const options = [END, ...memberNames];
|
|
114
|
-
|
|
115
|
-
const functionDef = {
|
|
116
|
-
name: 'route',
|
|
117
|
-
description: 'Select the next role(s).',
|
|
118
|
-
parameters: {
|
|
119
|
-
title: 'routeSchema',
|
|
120
|
-
type: 'object',
|
|
121
|
-
properties: {
|
|
122
|
-
next: {
|
|
123
|
-
title: 'Next',
|
|
124
|
-
type: 'array',
|
|
125
|
-
items: {
|
|
126
|
-
type: 'string',
|
|
127
|
-
enum: options,
|
|
128
|
-
},
|
|
129
|
-
minItems: 1,
|
|
130
|
-
},
|
|
131
|
-
},
|
|
132
|
-
required: ['next'],
|
|
133
|
-
},
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
const toolDef = {
|
|
137
|
-
type: 'function',
|
|
138
|
-
function: functionDef,
|
|
139
|
-
} as const;
|
|
140
|
-
|
|
141
|
-
const prompt = ChatPromptTemplate.fromMessages([
|
|
142
|
-
['system', systemPrompt],
|
|
143
|
-
new MessagesPlaceholder('messages'),
|
|
144
|
-
[
|
|
145
|
-
'system',
|
|
146
|
-
'Given the conversation above, who should act next?' +
|
|
147
|
-
' You can choose multiple workers to act in parallel if appropriate.' +
|
|
148
|
-
' Or should we FINISH? Select from: {options}',
|
|
149
|
-
],
|
|
150
|
-
]);
|
|
151
|
-
|
|
152
|
-
const formattedPrompt = await prompt.partial({
|
|
153
|
-
options: options.join(', '),
|
|
154
|
-
members: memberNames.join(', '),
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
const llm = new ChatOpenAI({
|
|
158
|
-
modelName: 'gpt-4',
|
|
159
|
-
temperature: 0,
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
const supervisorChain = formattedPrompt
|
|
163
|
-
.pipe(llm.bindTools(
|
|
164
|
-
[toolDef],
|
|
165
|
-
{
|
|
166
|
-
tool_choice: { 'type': 'function', 'function': { 'name': 'route' } },
|
|
167
|
-
},
|
|
168
|
-
))
|
|
169
|
-
.pipe(new JsonOutputToolsParser())
|
|
170
|
-
.pipe((x) => x[0].args);
|
|
171
|
-
|
|
172
|
-
const workflow = new StateGraph({
|
|
173
|
-
channels: agentStateChannels,
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
// Dynamically create agents and add nodes for each member
|
|
177
|
-
for (const member of this.members) {
|
|
178
|
-
const agent = await createAgent(member.llmConfig, member.tools, member.systemPrompt);
|
|
179
|
-
const node = async (
|
|
180
|
-
state: AgentStateChannels,
|
|
181
|
-
config?: RunnableConfig,
|
|
182
|
-
) => {
|
|
183
|
-
const result = await agent.invoke(state, config);
|
|
184
|
-
return {
|
|
185
|
-
messages: [
|
|
186
|
-
new AIMessage({ content: result.output, name: member.name }),
|
|
187
|
-
],
|
|
188
|
-
parallelResults: { [member.name]: result.output },
|
|
189
|
-
};
|
|
190
|
-
};
|
|
191
|
-
workflow.addNode(member.name, node);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// Add aggregator node
|
|
195
|
-
workflow.addNode('aggregator', async (state: AgentStateChannels) => {
|
|
196
|
-
const aggregatedContent = Object.entries(state.parallelResults)
|
|
197
|
-
.map(([name, result]) => `${name}: ${result}`)
|
|
198
|
-
.join('\n\n');
|
|
199
|
-
return {
|
|
200
|
-
messages: [new AIMessage({ content: aggregatedContent, name: 'Aggregator' })],
|
|
201
|
-
parallelResults: {}, // Clear parallel results after aggregation
|
|
202
|
-
};
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
workflow.addNode('supervisor', async (state: AgentStateChannels) => {
|
|
206
|
-
const result = await supervisorChain.invoke(state);
|
|
207
|
-
return {
|
|
208
|
-
next: result.next,
|
|
209
|
-
};
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
// Add conditional edges for parallel execution
|
|
213
|
-
workflow.addConditionalEdges(
|
|
214
|
-
'supervisor',
|
|
215
|
-
(x: AgentStateChannels) => {
|
|
216
|
-
if (x.next === END) {
|
|
217
|
-
return [END];
|
|
218
|
-
}
|
|
219
|
-
return Array.isArray(x.next) ? x.next : [x.next];
|
|
220
|
-
},
|
|
221
|
-
{
|
|
222
|
-
...Object.fromEntries(this.members.map(m => [m.name, m.name])),
|
|
223
|
-
[END]: END,
|
|
224
|
-
}
|
|
225
|
-
);
|
|
226
|
-
|
|
227
|
-
// Add edges from all agent nodes to the aggregator
|
|
228
|
-
this.members.forEach(member => {
|
|
229
|
-
workflow.addEdge(member.name, 'aggregator');
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
// Add edge from aggregator to supervisor
|
|
233
|
-
workflow.addEdge('aggregator', 'supervisor');
|
|
234
|
-
|
|
235
|
-
workflow.addEdge(START, 'supervisor');
|
|
236
|
-
|
|
237
|
-
return workflow.compile();
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
async processStream(
|
|
241
|
-
inputs: { messages: BaseMessage[] },
|
|
242
|
-
config: Partial<RunnableConfig> & { version: 'v1' | 'v2' },
|
|
243
|
-
) {
|
|
244
|
-
if (!this.graph) {
|
|
245
|
-
throw new Error('CollaborativeProcessor not initialized. Call initialize() first.');
|
|
246
|
-
}
|
|
247
|
-
const stream = this.graph.streamEvents(inputs, config);
|
|
248
|
-
for await (const event of stream) {
|
|
249
|
-
const handler = this.handlerRegistry.getHandler(event.event);
|
|
250
|
-
if (handler) {
|
|
251
|
-
handler.handle(event.event, event.data);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
}
|
|
@@ -1,220 +0,0 @@
|
|
|
1
|
-
// src/collab_design_v4.ts
|
|
2
|
-
import 'dotenv/config';
|
|
3
|
-
import { BaseMessage, HumanMessage } from '@langchain/core/messages';
|
|
4
|
-
import { END, StateGraphArgs, START, StateGraph } from '@langchain/langgraph';
|
|
5
|
-
import { AgentExecutor, createOpenAIToolsAgent } from 'langchain/agents';
|
|
6
|
-
import {
|
|
7
|
-
ChatPromptTemplate,
|
|
8
|
-
MessagesPlaceholder,
|
|
9
|
-
} from '@langchain/core/prompts';
|
|
10
|
-
import { Runnable, RunnableConfig } from '@langchain/core/runnables';
|
|
11
|
-
import { JsonOutputToolsParser } from 'langchain/output_parsers';
|
|
12
|
-
import { HandlerRegistry } from '@/events';
|
|
13
|
-
import { ChatOpenAI } from '@langchain/openai';
|
|
14
|
-
import { ChatBedrockConverse } from '@langchain/aws';
|
|
15
|
-
import { ChatAnthropic } from '@langchain/anthropic';
|
|
16
|
-
import { ChatMistralAI } from '@langchain/mistralai';
|
|
17
|
-
import { ChatVertexAI } from '@langchain/google-vertexai';
|
|
18
|
-
import { BedrockChat } from '@langchain/community/chat_models/bedrock/web';
|
|
19
|
-
import { supervisorPrompt } from '@/prompts/collab';
|
|
20
|
-
import { Providers } from '@/common';
|
|
21
|
-
|
|
22
|
-
interface AgentStateChannels {
|
|
23
|
-
messages: BaseMessage[];
|
|
24
|
-
next: string;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface Member {
|
|
28
|
-
name: string;
|
|
29
|
-
systemPrompt: string;
|
|
30
|
-
tools: any[];
|
|
31
|
-
llmConfig: LLMConfig;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
interface LLMConfig {
|
|
35
|
-
provider: Providers;
|
|
36
|
-
[key: string]: any;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
interface SupervisorConfig {
|
|
40
|
-
systemPrompt?: string;
|
|
41
|
-
llmConfig: LLMConfig;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const llmProviders: Record<Providers, any> = {
|
|
45
|
-
[Providers.OPENAI]: ChatOpenAI,
|
|
46
|
-
[Providers.VERTEXAI]: ChatVertexAI,
|
|
47
|
-
[Providers.BEDROCK_LEGACY]: BedrockChat,
|
|
48
|
-
[Providers.MISTRALAI]: ChatMistralAI,
|
|
49
|
-
[Providers.BEDROCK]: ChatBedrockConverse,
|
|
50
|
-
[Providers.ANTHROPIC]: ChatAnthropic,
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
export class CollaborativeProcessor {
|
|
54
|
-
private graph: Runnable | null = null;
|
|
55
|
-
private handlerRegistry: HandlerRegistry;
|
|
56
|
-
private members: Member[];
|
|
57
|
-
private supervisorConfig: SupervisorConfig;
|
|
58
|
-
|
|
59
|
-
constructor(
|
|
60
|
-
members: Member[],
|
|
61
|
-
supervisorConfig: SupervisorConfig,
|
|
62
|
-
customHandlers?: Record<string, any>
|
|
63
|
-
) {
|
|
64
|
-
this.members = members;
|
|
65
|
-
this.supervisorConfig = supervisorConfig;
|
|
66
|
-
this.handlerRegistry = new HandlerRegistry();
|
|
67
|
-
if (customHandlers) {
|
|
68
|
-
for (const [eventType, handler] of Object.entries(customHandlers)) {
|
|
69
|
-
this.handlerRegistry.register(eventType, handler);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
async initialize(): Promise<void> {
|
|
75
|
-
this.graph = await this.createGraph();
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
private async createGraph(): Promise<Runnable> {
|
|
79
|
-
const agentStateChannels: StateGraphArgs['channels'] = {
|
|
80
|
-
messages: {
|
|
81
|
-
value: (x?: BaseMessage[], y?: BaseMessage[]) => (x ?? []).concat(y ?? []),
|
|
82
|
-
default: () => [],
|
|
83
|
-
},
|
|
84
|
-
next: {
|
|
85
|
-
value: (x?: string, y?: string) => y ?? x ?? END,
|
|
86
|
-
default: () => END,
|
|
87
|
-
},
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
async function createAgent(
|
|
91
|
-
llmConfig: LLMConfig,
|
|
92
|
-
tools: any[],
|
|
93
|
-
systemPrompt: string
|
|
94
|
-
): Promise<AgentExecutor> {
|
|
95
|
-
const { provider, ...clientOptions } = llmConfig;
|
|
96
|
-
const LLMClass = llmProviders[provider];
|
|
97
|
-
if (!LLMClass) {
|
|
98
|
-
throw new Error(`Unsupported LLM provider: ${provider}`);
|
|
99
|
-
}
|
|
100
|
-
const llm = new LLMClass(clientOptions);
|
|
101
|
-
|
|
102
|
-
const prompt = await ChatPromptTemplate.fromMessages([
|
|
103
|
-
['system', systemPrompt],
|
|
104
|
-
new MessagesPlaceholder('messages'),
|
|
105
|
-
new MessagesPlaceholder('agent_scratchpad'),
|
|
106
|
-
]);
|
|
107
|
-
const agent = await createOpenAIToolsAgent({ llm, tools, prompt });
|
|
108
|
-
return new AgentExecutor({ agent, tools });
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const memberNames = this.members.map(member => member.name);
|
|
112
|
-
|
|
113
|
-
const systemPrompt = this.supervisorConfig.systemPrompt || supervisorPrompt;
|
|
114
|
-
const options = [END, ...memberNames];
|
|
115
|
-
|
|
116
|
-
const functionDef = {
|
|
117
|
-
name: 'route',
|
|
118
|
-
description: 'Select the next role.',
|
|
119
|
-
parameters: {
|
|
120
|
-
title: 'routeSchema',
|
|
121
|
-
type: 'object',
|
|
122
|
-
properties: {
|
|
123
|
-
next: {
|
|
124
|
-
title: 'Next',
|
|
125
|
-
anyOf: [
|
|
126
|
-
{ enum: options },
|
|
127
|
-
],
|
|
128
|
-
},
|
|
129
|
-
},
|
|
130
|
-
required: ['next'],
|
|
131
|
-
},
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
const toolDef = {
|
|
135
|
-
type: 'function',
|
|
136
|
-
function: functionDef,
|
|
137
|
-
} as const;
|
|
138
|
-
|
|
139
|
-
const prompt = ChatPromptTemplate.fromMessages([
|
|
140
|
-
['system', systemPrompt],
|
|
141
|
-
new MessagesPlaceholder('messages'),
|
|
142
|
-
[
|
|
143
|
-
'system',
|
|
144
|
-
'Given the conversation above, who should act next?' +
|
|
145
|
-
' Or should we FINISH? Select one of: {options}',
|
|
146
|
-
],
|
|
147
|
-
]);
|
|
148
|
-
|
|
149
|
-
const formattedPrompt = await prompt.partial({
|
|
150
|
-
options: options.join(', '),
|
|
151
|
-
members: memberNames.join(', '),
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
const { provider, ...clientOptions } = this.supervisorConfig.llmConfig;
|
|
155
|
-
const LLMClass = llmProviders[provider];
|
|
156
|
-
if (!LLMClass) {
|
|
157
|
-
throw new Error(`Unsupported LLM provider for supervisor: ${provider}`);
|
|
158
|
-
}
|
|
159
|
-
const llm = new LLMClass(clientOptions);
|
|
160
|
-
|
|
161
|
-
const supervisorChain = formattedPrompt
|
|
162
|
-
.pipe(llm.bindTools(
|
|
163
|
-
[toolDef],
|
|
164
|
-
{
|
|
165
|
-
tool_choice: { 'type': 'function', 'function': { 'name': 'route' } },
|
|
166
|
-
},
|
|
167
|
-
))
|
|
168
|
-
.pipe(new JsonOutputToolsParser())
|
|
169
|
-
.pipe((x) => (x[0].args));
|
|
170
|
-
|
|
171
|
-
const workflow = new StateGraph({
|
|
172
|
-
channels: agentStateChannels,
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
// Dynamically create agents and add nodes for each member
|
|
176
|
-
for (const member of this.members) {
|
|
177
|
-
const agent = await createAgent(member.llmConfig, member.tools, member.systemPrompt);
|
|
178
|
-
const node = async (
|
|
179
|
-
state: AgentStateChannels,
|
|
180
|
-
config?: RunnableConfig,
|
|
181
|
-
) => {
|
|
182
|
-
const result = await agent.invoke(state, config);
|
|
183
|
-
return {
|
|
184
|
-
messages: [
|
|
185
|
-
new HumanMessage({ content: result.output, name: member.name }),
|
|
186
|
-
],
|
|
187
|
-
};
|
|
188
|
-
};
|
|
189
|
-
workflow.addNode(member.name, node);
|
|
190
|
-
workflow.addEdge(member.name, 'supervisor');
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
workflow.addNode('supervisor', supervisorChain);
|
|
194
|
-
|
|
195
|
-
workflow.addConditionalEdges(
|
|
196
|
-
'supervisor',
|
|
197
|
-
(x: AgentStateChannels) => x.next,
|
|
198
|
-
);
|
|
199
|
-
|
|
200
|
-
workflow.addEdge(START, 'supervisor');
|
|
201
|
-
|
|
202
|
-
return workflow.compile();
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
async processStream(
|
|
206
|
-
inputs: { messages: BaseMessage[] },
|
|
207
|
-
config: Partial<RunnableConfig> & { version: 'v1' | 'v2' },
|
|
208
|
-
) {
|
|
209
|
-
if (!this.graph) {
|
|
210
|
-
throw new Error('CollaborativeProcessor not initialized. Call initialize() first.');
|
|
211
|
-
}
|
|
212
|
-
const stream = this.graph.streamEvents(inputs, config);
|
|
213
|
-
for await (const event of stream) {
|
|
214
|
-
const handler = this.handlerRegistry.getHandler(event.event);
|
|
215
|
-
if (handler) {
|
|
216
|
-
handler.handle(event.event, event.data);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
}
|