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