@aigne/core 1.13.0 → 1.14.0
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/CHANGELOG.md +15 -0
- package/README.md +13 -26
- package/README.zh.md +24 -37
- package/lib/cjs/agents/agent.d.ts +522 -15
- package/lib/cjs/agents/agent.js +357 -36
- package/lib/cjs/agents/ai-agent.d.ts +210 -52
- package/lib/cjs/agents/ai-agent.js +182 -24
- package/lib/cjs/agents/mcp-agent.d.ts +112 -0
- package/lib/cjs/agents/mcp-agent.js +79 -1
- package/lib/cjs/agents/team-agent.d.ts +99 -0
- package/lib/cjs/agents/team-agent.js +94 -0
- package/lib/cjs/agents/user-agent.d.ts +6 -4
- package/lib/cjs/agents/user-agent.js +16 -5
- package/lib/cjs/aigne/aigne.d.ts +263 -16
- package/lib/cjs/aigne/aigne.js +130 -20
- package/lib/cjs/aigne/context.d.ts +24 -8
- package/lib/cjs/aigne/context.js +8 -22
- package/lib/cjs/aigne/message-queue.d.ts +26 -4
- package/lib/cjs/aigne/message-queue.js +42 -7
- package/lib/cjs/aigne/usage.d.ts +9 -0
- package/lib/cjs/aigne/usage.js +3 -0
- package/lib/cjs/client/client.d.ts +81 -3
- package/lib/cjs/client/client.js +38 -0
- package/lib/cjs/client/index.d.ts +1 -0
- package/lib/cjs/client/index.js +17 -0
- package/lib/cjs/index.d.ts +0 -1
- package/lib/cjs/index.js +0 -1
- package/lib/cjs/loader/agent-js.d.ts +1 -1
- package/lib/cjs/loader/agent-js.js +2 -2
- package/lib/cjs/loader/agent-yaml.d.ts +3 -2
- package/lib/cjs/loader/agent-yaml.js +2 -1
- package/lib/cjs/loader/index.d.ts +4 -4
- package/lib/cjs/memory/default-memory.d.ts +16 -0
- package/lib/cjs/memory/default-memory.js +70 -0
- package/lib/cjs/memory/index.d.ts +3 -0
- package/lib/cjs/memory/index.js +19 -0
- package/lib/cjs/memory/memory.d.ts +89 -0
- package/lib/cjs/memory/memory.js +132 -0
- package/lib/cjs/memory/recorder.d.ts +86 -0
- package/lib/cjs/memory/recorder.js +50 -0
- package/lib/cjs/memory/retriever.d.ts +99 -0
- package/lib/cjs/memory/retriever.js +51 -0
- package/lib/cjs/models/bedrock-chat-model.d.ts +12 -3
- package/lib/cjs/models/bedrock-chat-model.js +54 -24
- package/lib/cjs/models/chat-model.d.ts +278 -1
- package/lib/cjs/models/chat-model.js +54 -0
- package/lib/cjs/models/claude-chat-model.d.ts +49 -3
- package/lib/cjs/models/claude-chat-model.js +34 -2
- package/lib/cjs/models/deepseek-chat-model.d.ts +16 -0
- package/lib/cjs/models/deepseek-chat-model.js +16 -0
- package/lib/cjs/models/gemini-chat-model.d.ts +14 -0
- package/lib/cjs/models/gemini-chat-model.js +14 -0
- package/lib/cjs/models/ollama-chat-model.d.ts +16 -0
- package/lib/cjs/models/ollama-chat-model.js +16 -0
- package/lib/cjs/models/open-router-chat-model.d.ts +16 -0
- package/lib/cjs/models/open-router-chat-model.js +16 -0
- package/lib/cjs/models/openai-chat-model.d.ts +67 -3
- package/lib/cjs/models/openai-chat-model.js +47 -2
- package/lib/cjs/models/xai-chat-model.d.ts +16 -0
- package/lib/cjs/models/xai-chat-model.js +16 -0
- package/lib/cjs/prompt/prompt-builder.d.ts +4 -4
- package/lib/cjs/prompt/prompt-builder.js +19 -18
- package/lib/cjs/prompt/prompts/memory-message-template.d.ts +1 -0
- package/lib/cjs/prompt/prompts/memory-message-template.js +10 -0
- package/lib/cjs/prompt/template.js +5 -1
- package/lib/cjs/server/error.d.ts +11 -0
- package/lib/cjs/server/error.js +11 -0
- package/lib/cjs/server/index.d.ts +2 -0
- package/lib/cjs/server/index.js +18 -0
- package/lib/cjs/server/server.d.ts +89 -8
- package/lib/cjs/server/server.js +58 -0
- package/lib/cjs/utils/fs.d.ts +2 -0
- package/lib/cjs/utils/fs.js +25 -0
- package/lib/cjs/utils/prompts.d.ts +1 -0
- package/lib/cjs/utils/prompts.js +11 -2
- package/lib/cjs/utils/type-utils.d.ts +1 -0
- package/lib/cjs/utils/type-utils.js +14 -0
- package/lib/dts/agents/agent.d.ts +522 -15
- package/lib/dts/agents/ai-agent.d.ts +210 -52
- package/lib/dts/agents/mcp-agent.d.ts +112 -0
- package/lib/dts/agents/team-agent.d.ts +99 -0
- package/lib/dts/agents/user-agent.d.ts +6 -4
- package/lib/dts/aigne/aigne.d.ts +263 -16
- package/lib/dts/aigne/context.d.ts +24 -8
- package/lib/dts/aigne/message-queue.d.ts +26 -4
- package/lib/dts/aigne/usage.d.ts +9 -0
- package/lib/dts/client/client.d.ts +81 -3
- package/lib/dts/client/index.d.ts +1 -0
- package/lib/dts/index.d.ts +0 -1
- package/lib/dts/loader/agent-js.d.ts +1 -1
- package/lib/dts/loader/agent-yaml.d.ts +3 -2
- package/lib/dts/loader/index.d.ts +4 -4
- package/lib/dts/memory/default-memory.d.ts +16 -0
- package/lib/dts/memory/index.d.ts +3 -0
- package/lib/dts/memory/memory.d.ts +89 -0
- package/lib/dts/memory/recorder.d.ts +86 -0
- package/lib/dts/memory/retriever.d.ts +99 -0
- package/lib/dts/models/bedrock-chat-model.d.ts +12 -3
- package/lib/dts/models/chat-model.d.ts +278 -1
- package/lib/dts/models/claude-chat-model.d.ts +49 -3
- package/lib/dts/models/deepseek-chat-model.d.ts +16 -0
- package/lib/dts/models/gemini-chat-model.d.ts +14 -0
- package/lib/dts/models/ollama-chat-model.d.ts +16 -0
- package/lib/dts/models/open-router-chat-model.d.ts +16 -0
- package/lib/dts/models/openai-chat-model.d.ts +67 -3
- package/lib/dts/models/xai-chat-model.d.ts +16 -0
- package/lib/dts/prompt/prompt-builder.d.ts +4 -4
- package/lib/dts/prompt/prompts/memory-message-template.d.ts +1 -0
- package/lib/dts/server/error.d.ts +11 -0
- package/lib/dts/server/index.d.ts +2 -0
- package/lib/dts/server/server.d.ts +89 -8
- package/lib/dts/utils/fs.d.ts +2 -0
- package/lib/dts/utils/prompts.d.ts +1 -0
- package/lib/dts/utils/type-utils.d.ts +1 -0
- package/lib/esm/agents/agent.d.ts +522 -15
- package/lib/esm/agents/agent.js +351 -35
- package/lib/esm/agents/ai-agent.d.ts +210 -52
- package/lib/esm/agents/ai-agent.js +183 -25
- package/lib/esm/agents/mcp-agent.d.ts +112 -0
- package/lib/esm/agents/mcp-agent.js +79 -1
- package/lib/esm/agents/team-agent.d.ts +99 -0
- package/lib/esm/agents/team-agent.js +94 -0
- package/lib/esm/agents/user-agent.d.ts +6 -4
- package/lib/esm/agents/user-agent.js +17 -6
- package/lib/esm/aigne/aigne.d.ts +263 -16
- package/lib/esm/aigne/aigne.js +132 -22
- package/lib/esm/aigne/context.d.ts +24 -8
- package/lib/esm/aigne/context.js +9 -22
- package/lib/esm/aigne/message-queue.d.ts +26 -4
- package/lib/esm/aigne/message-queue.js +42 -8
- package/lib/esm/aigne/usage.d.ts +9 -0
- package/lib/esm/aigne/usage.js +3 -0
- package/lib/esm/client/client.d.ts +81 -3
- package/lib/esm/client/client.js +38 -0
- package/lib/esm/client/index.d.ts +1 -0
- package/lib/esm/client/index.js +1 -0
- package/lib/esm/index.d.ts +0 -1
- package/lib/esm/index.js +0 -1
- package/lib/esm/loader/agent-js.d.ts +1 -1
- package/lib/esm/loader/agent-js.js +2 -2
- package/lib/esm/loader/agent-yaml.d.ts +3 -2
- package/lib/esm/loader/agent-yaml.js +2 -1
- package/lib/esm/loader/index.d.ts +4 -4
- package/lib/esm/memory/default-memory.d.ts +16 -0
- package/lib/esm/memory/default-memory.js +63 -0
- package/lib/esm/memory/index.d.ts +3 -0
- package/lib/esm/memory/index.js +3 -0
- package/lib/esm/memory/memory.d.ts +89 -0
- package/lib/esm/memory/memory.js +127 -0
- package/lib/esm/memory/recorder.d.ts +86 -0
- package/lib/esm/memory/recorder.js +46 -0
- package/lib/esm/memory/retriever.d.ts +99 -0
- package/lib/esm/memory/retriever.js +47 -0
- package/lib/esm/models/bedrock-chat-model.d.ts +12 -3
- package/lib/esm/models/bedrock-chat-model.js +56 -26
- package/lib/esm/models/chat-model.d.ts +278 -1
- package/lib/esm/models/chat-model.js +54 -0
- package/lib/esm/models/claude-chat-model.d.ts +49 -3
- package/lib/esm/models/claude-chat-model.js +35 -3
- package/lib/esm/models/deepseek-chat-model.d.ts +16 -0
- package/lib/esm/models/deepseek-chat-model.js +16 -0
- package/lib/esm/models/gemini-chat-model.d.ts +14 -0
- package/lib/esm/models/gemini-chat-model.js +14 -0
- package/lib/esm/models/ollama-chat-model.d.ts +16 -0
- package/lib/esm/models/ollama-chat-model.js +16 -0
- package/lib/esm/models/open-router-chat-model.d.ts +16 -0
- package/lib/esm/models/open-router-chat-model.js +16 -0
- package/lib/esm/models/openai-chat-model.d.ts +67 -3
- package/lib/esm/models/openai-chat-model.js +47 -2
- package/lib/esm/models/xai-chat-model.d.ts +16 -0
- package/lib/esm/models/xai-chat-model.js +16 -0
- package/lib/esm/prompt/prompt-builder.d.ts +4 -4
- package/lib/esm/prompt/prompt-builder.js +20 -19
- package/lib/esm/prompt/prompts/memory-message-template.d.ts +1 -0
- package/lib/esm/prompt/prompts/memory-message-template.js +7 -0
- package/lib/esm/prompt/template.js +5 -1
- package/lib/esm/server/error.d.ts +11 -0
- package/lib/esm/server/error.js +11 -0
- package/lib/esm/server/index.d.ts +2 -0
- package/lib/esm/server/index.js +2 -0
- package/lib/esm/server/server.d.ts +89 -8
- package/lib/esm/server/server.js +58 -0
- package/lib/esm/utils/fs.d.ts +2 -0
- package/lib/esm/utils/fs.js +21 -0
- package/lib/esm/utils/prompts.d.ts +1 -0
- package/lib/esm/utils/prompts.js +10 -2
- package/lib/esm/utils/type-utils.d.ts +1 -0
- package/lib/esm/utils/type-utils.js +13 -0
- package/package.json +14 -11
- package/lib/cjs/agents/memory.d.ts +0 -26
- package/lib/cjs/agents/memory.js +0 -45
- package/lib/dts/agents/memory.d.ts +0 -26
- package/lib/esm/agents/memory.d.ts +0 -26
- package/lib/esm/agents/memory.js +0 -41
- /package/{LICENSE → LICENSE.md} +0 -0
|
@@ -32,6 +32,21 @@ function getMCPServerString(options) {
|
|
|
32
32
|
}
|
|
33
33
|
return "unknown";
|
|
34
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* MCPAgent is a specialized agent for interacting with MCP (Model Context Protocol) servers.
|
|
37
|
+
* It provides the ability to connect to remote MCP servers using different transport methods,
|
|
38
|
+
* and access their tools, prompts, and resources.
|
|
39
|
+
*
|
|
40
|
+
* MCPAgent serves as a bridge between your application and MCP servers, allowing you to:
|
|
41
|
+
* - Connect to MCP servers over HTTP/SSE or stdio
|
|
42
|
+
* - Access server tools as agent skills
|
|
43
|
+
* - Utilize server prompts and resources
|
|
44
|
+
* - Manage server connections with automatic reconnection
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* Here's an example of creating an MCPAgent with SSE transport:
|
|
48
|
+
* {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-from-sse}
|
|
49
|
+
*/
|
|
35
50
|
export class MCPAgent extends Agent {
|
|
36
51
|
static from(options) {
|
|
37
52
|
checkArguments("MCPAgent.from", mcpAgentOptionsSchema, options);
|
|
@@ -102,6 +117,15 @@ export class MCPAgent extends Agent {
|
|
|
102
117
|
resources,
|
|
103
118
|
});
|
|
104
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* Create an MCPAgent instance directly with a configured client.
|
|
122
|
+
*
|
|
123
|
+
* @param options MCPAgent configuration options, including client instance
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* Here's an example of creating an MCPAgent with an existing client:
|
|
127
|
+
* {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-direct}
|
|
128
|
+
*/
|
|
105
129
|
constructor(options) {
|
|
106
130
|
super(options);
|
|
107
131
|
this.client = options.client;
|
|
@@ -110,17 +134,71 @@ export class MCPAgent extends Agent {
|
|
|
110
134
|
if (options.resources?.length)
|
|
111
135
|
this.resources.push(...options.resources);
|
|
112
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* The MCP client instance used for communication with the MCP server.
|
|
139
|
+
*
|
|
140
|
+
* This client manages the connection to the MCP server and provides
|
|
141
|
+
* methods for interacting with server-provided functionality.
|
|
142
|
+
*/
|
|
113
143
|
client;
|
|
144
|
+
/**
|
|
145
|
+
* Array of MCP prompts available from the connected server.
|
|
146
|
+
*
|
|
147
|
+
* Prompts can be accessed by index or by name.
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* Here's an example of accessing prompts:
|
|
151
|
+
* {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-prompts}
|
|
152
|
+
*/
|
|
114
153
|
prompts = createAccessorArray([], (arr, name) => arr.find((i) => i.name === name));
|
|
154
|
+
/**
|
|
155
|
+
* Array of MCP resources available from the connected server.
|
|
156
|
+
*
|
|
157
|
+
* Resources can be accessed by index or by name.
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* Here's an example of accessing resources:
|
|
161
|
+
* {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-resources}
|
|
162
|
+
*/
|
|
115
163
|
resources = createAccessorArray([], (arr, name) => arr.find((i) => i.name === name));
|
|
164
|
+
/**
|
|
165
|
+
* Check if the agent is invokable.
|
|
166
|
+
*
|
|
167
|
+
* MCPAgent itself is not directly invokable as it acts as a container
|
|
168
|
+
* for tools, prompts, and resources. Always returns false.
|
|
169
|
+
*/
|
|
116
170
|
get isInvokable() {
|
|
117
171
|
return false;
|
|
118
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* Process method required by Agent interface.
|
|
175
|
+
*
|
|
176
|
+
* Since MCPAgent itself is not directly invokable, this method
|
|
177
|
+
* throws an error if called.
|
|
178
|
+
*
|
|
179
|
+
* @param _input Input message (unused)
|
|
180
|
+
* @param _context Execution context (unused)
|
|
181
|
+
* @throws Error This method always throws an error since MCPAgent is not directly invokable
|
|
182
|
+
*/
|
|
119
183
|
async process(_input, _context) {
|
|
120
184
|
throw new Error("Method not implemented.");
|
|
121
185
|
}
|
|
186
|
+
/**
|
|
187
|
+
* Shut down the agent and close the MCP connection.
|
|
188
|
+
*
|
|
189
|
+
* This method cleans up resources and closes the connection
|
|
190
|
+
* to the MCP server.
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* Here's an example of shutting down an MCPAgent:
|
|
194
|
+
* {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-shutdown}
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* Here's an example of shutting down an MCPAgent by using statement:
|
|
198
|
+
* {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-shutdown-by-using}
|
|
199
|
+
*/
|
|
122
200
|
async shutdown() {
|
|
123
|
-
super.shutdown();
|
|
201
|
+
await super.shutdown();
|
|
124
202
|
await this.client.close();
|
|
125
203
|
}
|
|
126
204
|
}
|
|
@@ -1,16 +1,33 @@
|
|
|
1
1
|
import type { Context } from "../aigne/context.js";
|
|
2
2
|
import { type PromiseOrValue } from "../utils/type-utils.js";
|
|
3
3
|
import { Agent, type AgentOptions, type AgentProcessResult, type Message } from "./agent.js";
|
|
4
|
+
/**
|
|
5
|
+
* Defines the processing modes available for a TeamAgent.
|
|
6
|
+
*
|
|
7
|
+
* The processing mode determines how the agents within a team are executed
|
|
8
|
+
* and how their outputs are combined.
|
|
9
|
+
*/
|
|
4
10
|
export declare enum ProcessMode {
|
|
5
11
|
/**
|
|
6
12
|
* Process the agents one by one, passing the output of each agent to the next.
|
|
13
|
+
*
|
|
14
|
+
* In sequential mode, agents execute in order, with each agent receiving the
|
|
15
|
+
* combined output from all previous agents as part of its input.
|
|
7
16
|
*/
|
|
8
17
|
sequential = "sequential",
|
|
9
18
|
/**
|
|
10
19
|
* Process all agents in parallel, merging the output of all agents.
|
|
20
|
+
*
|
|
21
|
+
* In parallel mode, all agents execute simultaneously, each receiving the same
|
|
22
|
+
* initial input. Their outputs are then combined based on output key ownership.
|
|
11
23
|
*/
|
|
12
24
|
parallel = "parallel"
|
|
13
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Configuration options for creating a TeamAgent.
|
|
28
|
+
*
|
|
29
|
+
* These options extend the base AgentOptions and add team-specific settings.
|
|
30
|
+
*/
|
|
14
31
|
export interface TeamAgentOptions<I extends Message, O extends Message> extends AgentOptions<I, O> {
|
|
15
32
|
/**
|
|
16
33
|
* The method to process the agents in the team.
|
|
@@ -18,11 +35,93 @@ export interface TeamAgentOptions<I extends Message, O extends Message> extends
|
|
|
18
35
|
*/
|
|
19
36
|
mode?: ProcessMode;
|
|
20
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* TeamAgent coordinates a group of agents working together to accomplish tasks.
|
|
40
|
+
*
|
|
41
|
+
* A TeamAgent manages a collection of agents (its skills) and orchestrates their
|
|
42
|
+
* execution according to a specified processing mode. It provides mechanisms for
|
|
43
|
+
* agents to work either sequentially (one after another) or in parallel (all at once),
|
|
44
|
+
* with appropriate handling of their outputs.
|
|
45
|
+
*
|
|
46
|
+
* TeamAgent is particularly useful for:
|
|
47
|
+
* - Creating agent workflows where output from one agent feeds into another
|
|
48
|
+
* - Executing multiple agents simultaneously and combining their results
|
|
49
|
+
* - Building complex agent systems with specialized components working together
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* Here's an example of creating a sequential TeamAgent:
|
|
53
|
+
* {@includeCode ../../test/agents/team-agent.test.ts#example-team-agent-sequential}
|
|
54
|
+
*/
|
|
21
55
|
export declare class TeamAgent<I extends Message, O extends Message> extends Agent<I, O> {
|
|
56
|
+
/**
|
|
57
|
+
* Create a TeamAgent from the provided options.
|
|
58
|
+
*
|
|
59
|
+
* @param options Configuration options for the TeamAgent
|
|
60
|
+
* @returns A new TeamAgent instance
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* Here's an example of creating a sequential TeamAgent:
|
|
64
|
+
* {@includeCode ../../test/agents/team-agent.test.ts#example-team-agent-sequential}
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* Here's an example of creating a parallel TeamAgent:
|
|
68
|
+
* {@includeCode ../../test/agents/team-agent.test.ts#example-team-agent-parallel}
|
|
69
|
+
*/
|
|
22
70
|
static from<I extends Message, O extends Message>(options: TeamAgentOptions<I, O>): TeamAgent<I, O>;
|
|
71
|
+
/**
|
|
72
|
+
* Create a new TeamAgent instance.
|
|
73
|
+
*
|
|
74
|
+
* @param options Configuration options for the TeamAgent
|
|
75
|
+
*/
|
|
23
76
|
constructor(options: TeamAgentOptions<I, O>);
|
|
77
|
+
/**
|
|
78
|
+
* The processing mode that determines how agents in the team are executed.
|
|
79
|
+
*
|
|
80
|
+
* This can be either sequential (one after another) or parallel (all at once).
|
|
81
|
+
*/
|
|
24
82
|
mode: ProcessMode;
|
|
83
|
+
/**
|
|
84
|
+
* Process an input message by routing it through the team's agents.
|
|
85
|
+
*
|
|
86
|
+
* Depending on the team's processing mode, this will either:
|
|
87
|
+
* - In sequential mode: Pass input through each agent in sequence, with each agent
|
|
88
|
+
* receiving the combined output from previous agents
|
|
89
|
+
* - In parallel mode: Process input through all agents simultaneously and combine their outputs
|
|
90
|
+
*
|
|
91
|
+
* @param input The message to process
|
|
92
|
+
* @param context The execution context
|
|
93
|
+
* @returns A stream of message chunks that collectively form the response
|
|
94
|
+
*/
|
|
25
95
|
process(input: I, context: Context): PromiseOrValue<AgentProcessResult<O>>;
|
|
96
|
+
/**
|
|
97
|
+
* Process input sequentially through each agent in the team.
|
|
98
|
+
*
|
|
99
|
+
* This method:
|
|
100
|
+
* 1. Executes each agent in order
|
|
101
|
+
* 2. Passes the combined output from previous agents to the next agent
|
|
102
|
+
* 3. Yields output chunks as they become available
|
|
103
|
+
* 4. Updates the team's agent list with any changes that occurred during processing
|
|
104
|
+
*
|
|
105
|
+
* @param input The message to process
|
|
106
|
+
* @param context The execution context
|
|
107
|
+
* @returns A stream of message chunks from all agents
|
|
108
|
+
*
|
|
109
|
+
* @private
|
|
110
|
+
*/
|
|
26
111
|
_processSequential(input: I, context: Context): PromiseOrValue<AgentProcessResult<O>>;
|
|
112
|
+
/**
|
|
113
|
+
* Process input in parallel through all agents in the team.
|
|
114
|
+
*
|
|
115
|
+
* This method:
|
|
116
|
+
* 1. Executes all agents simultaneously with the same input
|
|
117
|
+
* 2. Yields combined output chunks
|
|
118
|
+
* 3. Updates the team's agent list with any changes that occurred during processing
|
|
119
|
+
*
|
|
120
|
+
* @param input The message to process
|
|
121
|
+
* @param context The execution context
|
|
122
|
+
* @returns A stream of combined message chunks from all agents
|
|
123
|
+
*
|
|
124
|
+
* @private
|
|
125
|
+
*/
|
|
27
126
|
_processParallel(input: I, context: Context): PromiseOrValue<AgentProcessResult<O>>;
|
|
28
127
|
}
|
|
@@ -1,26 +1,91 @@
|
|
|
1
1
|
import { mergeAgentResponseChunk, readableStreamToAsyncIterator } from "../utils/stream-utils.js";
|
|
2
2
|
import { isEmpty } from "../utils/type-utils.js";
|
|
3
3
|
import { Agent, } from "./agent.js";
|
|
4
|
+
/**
|
|
5
|
+
* Defines the processing modes available for a TeamAgent.
|
|
6
|
+
*
|
|
7
|
+
* The processing mode determines how the agents within a team are executed
|
|
8
|
+
* and how their outputs are combined.
|
|
9
|
+
*/
|
|
4
10
|
export var ProcessMode;
|
|
5
11
|
(function (ProcessMode) {
|
|
6
12
|
/**
|
|
7
13
|
* Process the agents one by one, passing the output of each agent to the next.
|
|
14
|
+
*
|
|
15
|
+
* In sequential mode, agents execute in order, with each agent receiving the
|
|
16
|
+
* combined output from all previous agents as part of its input.
|
|
8
17
|
*/
|
|
9
18
|
ProcessMode["sequential"] = "sequential";
|
|
10
19
|
/**
|
|
11
20
|
* Process all agents in parallel, merging the output of all agents.
|
|
21
|
+
*
|
|
22
|
+
* In parallel mode, all agents execute simultaneously, each receiving the same
|
|
23
|
+
* initial input. Their outputs are then combined based on output key ownership.
|
|
12
24
|
*/
|
|
13
25
|
ProcessMode["parallel"] = "parallel";
|
|
14
26
|
})(ProcessMode || (ProcessMode = {}));
|
|
27
|
+
/**
|
|
28
|
+
* TeamAgent coordinates a group of agents working together to accomplish tasks.
|
|
29
|
+
*
|
|
30
|
+
* A TeamAgent manages a collection of agents (its skills) and orchestrates their
|
|
31
|
+
* execution according to a specified processing mode. It provides mechanisms for
|
|
32
|
+
* agents to work either sequentially (one after another) or in parallel (all at once),
|
|
33
|
+
* with appropriate handling of their outputs.
|
|
34
|
+
*
|
|
35
|
+
* TeamAgent is particularly useful for:
|
|
36
|
+
* - Creating agent workflows where output from one agent feeds into another
|
|
37
|
+
* - Executing multiple agents simultaneously and combining their results
|
|
38
|
+
* - Building complex agent systems with specialized components working together
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* Here's an example of creating a sequential TeamAgent:
|
|
42
|
+
* {@includeCode ../../test/agents/team-agent.test.ts#example-team-agent-sequential}
|
|
43
|
+
*/
|
|
15
44
|
export class TeamAgent extends Agent {
|
|
45
|
+
/**
|
|
46
|
+
* Create a TeamAgent from the provided options.
|
|
47
|
+
*
|
|
48
|
+
* @param options Configuration options for the TeamAgent
|
|
49
|
+
* @returns A new TeamAgent instance
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* Here's an example of creating a sequential TeamAgent:
|
|
53
|
+
* {@includeCode ../../test/agents/team-agent.test.ts#example-team-agent-sequential}
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* Here's an example of creating a parallel TeamAgent:
|
|
57
|
+
* {@includeCode ../../test/agents/team-agent.test.ts#example-team-agent-parallel}
|
|
58
|
+
*/
|
|
16
59
|
static from(options) {
|
|
17
60
|
return new TeamAgent(options);
|
|
18
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Create a new TeamAgent instance.
|
|
64
|
+
*
|
|
65
|
+
* @param options Configuration options for the TeamAgent
|
|
66
|
+
*/
|
|
19
67
|
constructor(options) {
|
|
20
68
|
super(options);
|
|
21
69
|
this.mode = options.mode ?? ProcessMode.sequential;
|
|
22
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* The processing mode that determines how agents in the team are executed.
|
|
73
|
+
*
|
|
74
|
+
* This can be either sequential (one after another) or parallel (all at once).
|
|
75
|
+
*/
|
|
23
76
|
mode;
|
|
77
|
+
/**
|
|
78
|
+
* Process an input message by routing it through the team's agents.
|
|
79
|
+
*
|
|
80
|
+
* Depending on the team's processing mode, this will either:
|
|
81
|
+
* - In sequential mode: Pass input through each agent in sequence, with each agent
|
|
82
|
+
* receiving the combined output from previous agents
|
|
83
|
+
* - In parallel mode: Process input through all agents simultaneously and combine their outputs
|
|
84
|
+
*
|
|
85
|
+
* @param input The message to process
|
|
86
|
+
* @param context The execution context
|
|
87
|
+
* @returns A stream of message chunks that collectively form the response
|
|
88
|
+
*/
|
|
24
89
|
process(input, context) {
|
|
25
90
|
switch (this.mode) {
|
|
26
91
|
case ProcessMode.sequential:
|
|
@@ -29,6 +94,21 @@ export class TeamAgent extends Agent {
|
|
|
29
94
|
return this._processParallel(input, context);
|
|
30
95
|
}
|
|
31
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* Process input sequentially through each agent in the team.
|
|
99
|
+
*
|
|
100
|
+
* This method:
|
|
101
|
+
* 1. Executes each agent in order
|
|
102
|
+
* 2. Passes the combined output from previous agents to the next agent
|
|
103
|
+
* 3. Yields output chunks as they become available
|
|
104
|
+
* 4. Updates the team's agent list with any changes that occurred during processing
|
|
105
|
+
*
|
|
106
|
+
* @param input The message to process
|
|
107
|
+
* @param context The execution context
|
|
108
|
+
* @returns A stream of message chunks from all agents
|
|
109
|
+
*
|
|
110
|
+
* @private
|
|
111
|
+
*/
|
|
32
112
|
async *_processSequential(input, context) {
|
|
33
113
|
const output = {};
|
|
34
114
|
// Clone the agents to run, so that we can update the agents list during the loop
|
|
@@ -45,6 +125,20 @@ export class TeamAgent extends Agent {
|
|
|
45
125
|
this.skills.splice(0);
|
|
46
126
|
this.skills.push(...newAgents);
|
|
47
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* Process input in parallel through all agents in the team.
|
|
130
|
+
*
|
|
131
|
+
* This method:
|
|
132
|
+
* 1. Executes all agents simultaneously with the same input
|
|
133
|
+
* 2. Yields combined output chunks
|
|
134
|
+
* 3. Updates the team's agent list with any changes that occurred during processing
|
|
135
|
+
*
|
|
136
|
+
* @param input The message to process
|
|
137
|
+
* @param context The execution context
|
|
138
|
+
* @returns A stream of combined message chunks from all agents
|
|
139
|
+
*
|
|
140
|
+
* @private
|
|
141
|
+
*/
|
|
48
142
|
async *_processParallel(input, context) {
|
|
49
143
|
const result = await Promise.all(this.skills.map((agent) => context.invoke(agent, input, { returnActiveAgent: true, streaming: true })));
|
|
50
144
|
const streams = result.map((i) => i[0]);
|
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
import { ReadableStream } from "node:stream/web";
|
|
2
|
-
import {
|
|
3
|
-
import type
|
|
4
|
-
import {
|
|
2
|
+
import type { Context } from "../aigne/context.js";
|
|
3
|
+
import { type MessagePayload } from "../aigne/message-queue.js";
|
|
4
|
+
import { Agent, type AgentOptions, type AgentProcessResult, type FunctionAgentFn, type Message } from "./agent.js";
|
|
5
5
|
export interface UserAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
|
|
6
6
|
context: Context;
|
|
7
7
|
process?: FunctionAgentFn<I, O>;
|
|
8
8
|
activeAgent?: Agent;
|
|
9
9
|
}
|
|
10
|
-
export declare class UserAgent<I extends Message = Message, O extends Message = Message> extends
|
|
10
|
+
export declare class UserAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
|
|
11
11
|
static from<I extends Message, O extends Message>(options: UserAgentOptions<I, O>): UserAgent<I, O>;
|
|
12
12
|
constructor(options: UserAgentOptions<I, O>);
|
|
13
13
|
context: Context;
|
|
14
14
|
private _process?;
|
|
15
15
|
private activeAgent?;
|
|
16
|
+
protected subscribeToTopics(context: Pick<Context, "subscribe">): void;
|
|
17
|
+
protected publishToTopics(output: O, context: Context): Promise<void>;
|
|
16
18
|
invoke: Agent<I, O>["invoke"];
|
|
17
19
|
process(input: I, context: Context): Promise<AgentProcessResult<O>>;
|
|
18
20
|
publish: Context["publish"];
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { ReadableStream } from "node:stream/web";
|
|
2
|
-
import {
|
|
2
|
+
import { toMessagePayload } from "../aigne/message-queue.js";
|
|
3
3
|
import { orArrayToArray } from "../utils/type-utils.js";
|
|
4
|
-
import {
|
|
5
|
-
export class UserAgent extends
|
|
4
|
+
import { Agent, } from "./agent.js";
|
|
5
|
+
export class UserAgent extends Agent {
|
|
6
6
|
static from(options) {
|
|
7
7
|
return new UserAgent(options);
|
|
8
8
|
}
|
|
@@ -15,6 +15,14 @@ export class UserAgent extends FunctionAgent {
|
|
|
15
15
|
context;
|
|
16
16
|
_process;
|
|
17
17
|
activeAgent;
|
|
18
|
+
subscribeToTopics(context) {
|
|
19
|
+
if (this._process)
|
|
20
|
+
super.subscribeToTopics(context);
|
|
21
|
+
}
|
|
22
|
+
async publishToTopics(output, context) {
|
|
23
|
+
if (this._process)
|
|
24
|
+
super.publishToTopics(output, context);
|
|
25
|
+
}
|
|
18
26
|
invoke = ((input, context, options) => {
|
|
19
27
|
if (!context)
|
|
20
28
|
this.context = this.context.newContext({ reset: true });
|
|
@@ -36,13 +44,16 @@ export class UserAgent extends FunctionAgent {
|
|
|
36
44
|
}
|
|
37
45
|
const publicTopic = typeof this.publishTopic === "function" ? await this.publishTopic(input) : this.publishTopic;
|
|
38
46
|
if (publicTopic?.length) {
|
|
39
|
-
context.publish(publicTopic,
|
|
47
|
+
context.publish(publicTopic, input);
|
|
48
|
+
if (this.subscribeTopic) {
|
|
49
|
+
return this.subscribe(this.subscribeTopic).then((res) => res.message);
|
|
50
|
+
}
|
|
40
51
|
return {};
|
|
41
52
|
}
|
|
42
53
|
throw new Error("UserAgent must have a process function or a publishTopic");
|
|
43
54
|
}
|
|
44
|
-
publish = ((
|
|
45
|
-
return this.context.publish(
|
|
55
|
+
publish = ((topic, payload) => {
|
|
56
|
+
return this.context.publish(topic, toMessagePayload(payload, { role: "user", source: this.name }));
|
|
46
57
|
});
|
|
47
58
|
subscribe = ((...args) => {
|
|
48
59
|
return this.context.subscribe(...args);
|