@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
@@ -1,38 +1,285 @@
1
- import { Agent } from "../agents/agent.js";
1
+ import { Agent, type AgentResponse, type AgentResponseStream, type Message } from "../agents/agent.js";
2
+ import type { UserAgent } from "../agents/user-agent.js";
2
3
  import { ChatModel } from "../models/chat-model.js";
3
- import { AIGNEContext, type Context } from "./context.js";
4
- import { MessageQueue } from "./message-queue.js";
4
+ import { AIGNEContext, type InvokeOptions } from "./context.js";
5
+ import { type MessagePayload, MessageQueue, type MessageQueueListener, type Unsubscribe } from "./message-queue.js";
5
6
  import type { ContextLimits } from "./usage.js";
7
+ /**
8
+ * Options for the AIGNE class.
9
+ */
6
10
  export interface AIGNEOptions {
11
+ /**
12
+ * The name of the AIGNE instance.
13
+ */
7
14
  name?: string;
15
+ /**
16
+ * The description of the AIGNE instance.
17
+ */
8
18
  description?: string;
19
+ /**
20
+ * Global model to use for all agents not specifying a model.
21
+ */
9
22
  model?: ChatModel;
23
+ /**
24
+ * Skills to use for the AIGNE instance.
25
+ */
10
26
  skills?: Agent[];
27
+ /**
28
+ * Agents to use for the AIGNE instance.
29
+ */
11
30
  agents?: Agent[];
31
+ /**
32
+ * Limits for the AIGNE instance, such as timeout, max tokens, max invocations, etc.
33
+ */
12
34
  limits?: ContextLimits;
13
35
  }
36
+ /**
37
+ * AIGNE is a class that orchestrates multiple agents to build complex AI applications.
38
+ * It serves as the central coordination point for agent interactions, message passing, and execution flow.
39
+ *
40
+ * @example
41
+ * Here's a simple example of how to use AIGNE:
42
+ * {@includeCode ../../test/aigne/aigne.test.ts#example-simple}
43
+ *
44
+ * @example
45
+ * Here's an example of how to use AIGNE with streaming response:
46
+ * {@includeCode ../../test/aigne/aigne.test.ts#example-streaming}
47
+ */
14
48
  export declare class AIGNE {
15
- static load({ path, ...options }: {
16
- path: string;
17
- } & AIGNEOptions): Promise<AIGNE>;
49
+ /**
50
+ * Loads an AIGNE instance from a directory containing an aigne.yaml file and agent definitions.
51
+ * This static method provides a convenient way to initialize an AIGNE system from configuration files.
52
+ *
53
+ * @param path - Path to the directory containing the aigne.yaml file.
54
+ * @param options - Options to override the loaded configuration.
55
+ * @returns A fully initialized AIGNE instance with configured agents and skills.
56
+ */
57
+ static load(path: string, options?: AIGNEOptions): Promise<AIGNE>;
58
+ /**
59
+ * Creates a new AIGNE instance with the specified options.
60
+ *
61
+ * @param options - Configuration options for the AIGNE instance including name, description, model, and agents.
62
+ */
18
63
  constructor(options?: AIGNEOptions);
64
+ /**
65
+ * Optional name identifier for this AIGNE instance.
66
+ */
19
67
  name?: string;
68
+ /**
69
+ * Optional description of this AIGNE instance's purpose or functionality.
70
+ */
20
71
  description?: string;
21
- readonly messageQueue: MessageQueue;
72
+ /**
73
+ * Global model to use for all agents that don't specify their own model.
74
+ */
22
75
  model?: ChatModel;
23
- readonly skills: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>[] & {
24
- [key: string]: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>;
76
+ /**
77
+ * Usage limits applied to this AIGNE instance's execution.
78
+ */
79
+ limits?: ContextLimits;
80
+ /**
81
+ * Message queue for handling inter-agent communication.
82
+ *
83
+ * @hidden
84
+ */
85
+ readonly messageQueue: MessageQueue;
86
+ /**
87
+ * Collection of skill agents available to this AIGNE instance.
88
+ * Provides indexed access by skill name.
89
+ */
90
+ readonly skills: Agent<Message, Message>[] & {
91
+ [key: string]: Agent<Message, Message>;
25
92
  };
26
- readonly agents: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>[] & {
27
- [key: string]: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>;
93
+ /**
94
+ * Collection of primary agents managed by this AIGNE instance.
95
+ * Provides indexed access by agent name.
96
+ */
97
+ readonly agents: Agent<Message, Message>[] & {
98
+ [key: string]: Agent<Message, Message>;
28
99
  };
29
- limits?: ContextLimits;
100
+ /**
101
+ * Adds one or more agents to this AIGNE instance.
102
+ * Each agent is attached to this AIGNE instance, allowing it to access the AIGNE's resources.
103
+ *
104
+ * @param agents - One or more Agent instances to add to this AIGNE.
105
+ */
30
106
  addAgent(...agents: Agent[]): void;
107
+ /**
108
+ * Creates a new execution context for this AIGNE instance.
109
+ * Contexts isolate state for different flows or conversations.
110
+ *
111
+ * @returns A new AIGNEContext instance bound to this AIGNE.
112
+ */
31
113
  newContext(): AIGNEContext;
32
- publish: Context["publish"];
33
- invoke: Context["invoke"];
34
- subscribe: Context["subscribe"];
35
- unsubscribe: Context["unsubscribe"];
114
+ /**
115
+ * Creates a user agent for consistent interactions with a specified agent.
116
+ * This method allows you to create a wrapper around an agent for repeated invocations.
117
+ *
118
+ * @param agent - Target agent to be wrapped for consistent invocation
119
+ * @returns A user agent instance that provides a convenient interface for interacting with the target agent
120
+ *
121
+ * @example
122
+ * Here's an example of how to create a user agent and invoke it consistently:
123
+ * {@includeCode ../../test/aigne/aigne.test.ts#example-user-agent}
124
+ */
125
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>): UserAgent<I, O>;
126
+ /**
127
+ * Invokes an agent with a message and returns both the output and the active agent.
128
+ * This overload is useful when you need to track which agent was ultimately responsible for generating the response.
129
+ *
130
+ * @param agent - Target agent to invoke
131
+ * @param message - Input message to send to the agent (can be a string or a structured message object)
132
+ * @param options.returnActiveAgent - Must be true to return the final active agent
133
+ * @param options.streaming - Must be false to return a response stream
134
+ * @returns A promise resolving to a tuple containing the agent's response and the final active agent
135
+ */
136
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options: InvokeOptions & {
137
+ returnActiveAgent: true;
138
+ streaming?: false;
139
+ }): Promise<[O, Agent]>;
140
+ /**
141
+ * Invokes an agent with a message and returns both a stream of the response and the active agent.
142
+ * This overload is useful when you need streaming responses while also tracking which agent provided them.
143
+ *
144
+ * @param agent - Target agent to invoke
145
+ * @param message - Input message to send to the agent (can be a string or a structured message object)
146
+ * @param options.returnActiveAgent - Must be true to return the final active agent
147
+ * @param options.streaming - Must be true to return a response stream
148
+ * @returns A promise resolving to a tuple containing the agent's response stream and a promise for the final agent
149
+ */
150
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options: InvokeOptions & {
151
+ returnActiveAgent: true;
152
+ streaming: true;
153
+ }): Promise<[AgentResponseStream<O>, Promise<Agent>]>;
154
+ /**
155
+ * Invokes an agent with a message and returns just the output.
156
+ * This is the standard way to invoke an agent when you only need the response.
157
+ *
158
+ * @param agent - Target agent to invoke
159
+ * @param message - Input message to send to the agent (can be a string or a structured message object)
160
+ * @param options - Optional configuration parameters for the invocation
161
+ * @returns A promise resolving to the agent's complete response
162
+ *
163
+ * @example
164
+ * Here's a simple example of how to invoke an agent:
165
+ * {@includeCode ../../test/aigne/aigne.test.ts#example-simple}
166
+ */
167
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options?: InvokeOptions & {
168
+ returnActiveAgent?: false;
169
+ streaming?: false;
170
+ }): Promise<O>;
171
+ /**
172
+ * Invokes an agent with a message and returns a stream of the response.
173
+ * This allows processing the response incrementally as it's being generated.
174
+ *
175
+ * @param agent - Target agent to invoke
176
+ * @param message - Input message to send to the agent (can be a string or a structured message object)
177
+ * @param options - Configuration with streaming enabled to receive incremental response chunks
178
+ * @returns A promise resolving to a stream of the agent's response that can be consumed incrementally
179
+ *
180
+ * @example
181
+ * Here's an example of how to invoke an agent with streaming response:
182
+ * {@includeCode ../../test/aigne/aigne.test.ts#example-streaming}
183
+ */
184
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options: InvokeOptions & {
185
+ returnActiveAgent?: false;
186
+ streaming: true;
187
+ }): Promise<AgentResponseStream<O>>;
188
+ /**
189
+ * General implementation signature that handles all overload cases.
190
+ * This unified signature supports all the different invocation patterns defined by the overloads.
191
+ *
192
+ * @param agent - Target agent to invoke or wrap
193
+ * @param message - Optional input message to send to the agent
194
+ * @param options - Optional configuration parameters for the invocation
195
+ * @returns Either a UserAgent (when no message provided) or a promise resolving to the agent's response
196
+ * with optional active agent information based on the provided options
197
+ */
198
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message?: I | string, options?: InvokeOptions): UserAgent<I, O> | Promise<AgentResponse<O> | [AgentResponse<O>, Agent]>;
199
+ /**
200
+ * Publishes a message to the message queue for inter-agent communication.
201
+ * This method broadcasts a message to all subscribers of the specified topic(s).
202
+ * It creates a new context internally and delegates to the context's publish method.
203
+ *
204
+ * @param topic - The topic or array of topics to publish the message to
205
+ * @param payload - The message payload to be delivered to subscribers
206
+ *
207
+ * @example
208
+ * Here's an example of how to publish a message:
209
+ * {@includeCode ../../test/aigne/aigne.test.ts#example-publish-message}
210
+ */
211
+ publish(topic: string | string[], payload: Omit<MessagePayload, "context"> | Message | string): void;
212
+ /**
213
+ * Subscribes to receive the next message on a specific topic.
214
+ * This overload returns a Promise that resolves with the next message published to the topic.
215
+ * It's useful for one-time message handling or when using async/await patterns.
216
+ *
217
+ * @param topic - The topic to subscribe to
218
+ * @returns A Promise that resolves with the next message payload published to the specified topic
219
+ *
220
+ * @example
221
+ * Here's an example of how to subscribe to a topic and receive the next message:
222
+ * {@includeCode ../../test/aigne/aigne.test.ts#example-publish-message}
223
+ */
224
+ subscribe(topic: string | string[], listener?: undefined): Promise<MessagePayload>;
225
+ /**
226
+ * Subscribes to messages on a specific topic with a listener callback.
227
+ * This overload registers a listener function that will be called for each message published to the topic.
228
+ * It's useful for continuous message handling or event-driven architectures.
229
+ *
230
+ * @param topic - The topic to subscribe to
231
+ * @param listener - Callback function that will be invoked when messages arrive on the specified topic
232
+ * @returns An Unsubscribe function that can be called to cancel the subscription
233
+ *
234
+ * @example
235
+ * Here's an example of how to subscribe to a topic with a listener:
236
+ * {@includeCode ../../test/aigne/aigne.test.ts#example-subscribe-topic}
237
+ */
238
+ subscribe(topic: string | string[], listener: MessageQueueListener): Unsubscribe;
239
+ /**
240
+ * Generic subscribe signature that handles both Promise and listener patterns.
241
+ * This is the implementation signature that supports both overloaded behaviors.
242
+ *
243
+ * @param topic - The topic to subscribe to
244
+ * @param listener - Optional callback function
245
+ * @returns Either a Promise for the next message or an Unsubscribe function
246
+ */
247
+ subscribe(topic: string | string[], listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
248
+ /**
249
+ * Unsubscribes a listener from a specific topic in the message queue.
250
+ * This method stops a previously registered listener from receiving further messages.
251
+ * It should be called when message processing is complete or when the component is no longer interested
252
+ * in messages published to the specified topic.
253
+ *
254
+ * @param topic - The topic to unsubscribe from
255
+ * @param listener - The listener function that was previously subscribed to the topic
256
+ *
257
+ * @example
258
+ * {@includeCode ../../test/aigne/aigne.test.ts#example-subscribe-topic}
259
+ */
260
+ unsubscribe(topic: string | string[], listener: MessageQueueListener): void;
261
+ /**
262
+ * Gracefully shuts down the AIGNE instance and all its agents and skills.
263
+ * This ensures proper cleanup of resources before termination.
264
+ *
265
+ * @returns A promise that resolves when shutdown is complete.
266
+ *
267
+ * @example
268
+ * Here's an example of shutdown an AIGNE instance:
269
+ * {@includeCode ../../test/aigne/aigne.test.ts#example-shutdown}
270
+ */
36
271
  shutdown(): Promise<void>;
272
+ /**
273
+ * Asynchronous dispose method for the AIGNE instance.
274
+ *
275
+ * @example
276
+ * Here's an example of using async dispose:
277
+ * {@includeCode ../../test/aigne/aigne.test.ts#example-shutdown-using}
278
+ */
279
+ [Symbol.asyncDispose](): Promise<void>;
280
+ /**
281
+ * Initializes handlers for process exit events to ensure clean shutdown.
282
+ * This registers handlers for SIGINT and exit events to properly terminate all agents.
283
+ */
37
284
  private initProcessExitHandler;
38
285
  }
@@ -8,18 +8,43 @@ const chat_model_js_1 = require("../models/chat-model.js");
8
8
  const type_utils_js_1 = require("../utils/type-utils.js");
9
9
  const context_js_1 = require("./context.js");
10
10
  const message_queue_js_1 = require("./message-queue.js");
11
+ /**
12
+ * AIGNE is a class that orchestrates multiple agents to build complex AI applications.
13
+ * It serves as the central coordination point for agent interactions, message passing, and execution flow.
14
+ *
15
+ * @example
16
+ * Here's a simple example of how to use AIGNE:
17
+ * {@includeCode ../../test/aigne/aigne.test.ts#example-simple}
18
+ *
19
+ * @example
20
+ * Here's an example of how to use AIGNE with streaming response:
21
+ * {@includeCode ../../test/aigne/aigne.test.ts#example-streaming}
22
+ */
11
23
  class AIGNE {
12
- static async load({ path, ...options }) {
24
+ /**
25
+ * Loads an AIGNE instance from a directory containing an aigne.yaml file and agent definitions.
26
+ * This static method provides a convenient way to initialize an AIGNE system from configuration files.
27
+ *
28
+ * @param path - Path to the directory containing the aigne.yaml file.
29
+ * @param options - Options to override the loaded configuration.
30
+ * @returns A fully initialized AIGNE instance with configured agents and skills.
31
+ */
32
+ static async load(path, options) {
13
33
  const { model, agents, skills, ...aigne } = await (0, index_js_1.load)({ path });
14
34
  return new AIGNE({
15
35
  ...options,
16
- model: options.model || model,
17
- name: options.name || aigne.name || undefined,
18
- description: options.description || aigne.description || undefined,
19
- agents: agents.concat(options.agents ?? []),
20
- skills: skills.concat(options.skills ?? []),
36
+ model: options?.model || model,
37
+ name: options?.name || aigne.name || undefined,
38
+ description: options?.description || aigne.description || undefined,
39
+ agents: agents.concat(options?.agents ?? []),
40
+ skills: skills.concat(options?.skills ?? []),
21
41
  });
22
42
  }
43
+ /**
44
+ * Creates a new AIGNE instance with the specified options.
45
+ *
46
+ * @param options - Configuration options for the AIGNE instance including name, description, model, and agents.
47
+ */
23
48
  constructor(options) {
24
49
  if (options)
25
50
  (0, type_utils_js_1.checkArguments)("AIGNE", aigneOptionsSchema, options);
@@ -33,13 +58,44 @@ class AIGNE {
33
58
  this.addAgent(...options.agents);
34
59
  this.initProcessExitHandler();
35
60
  }
61
+ /**
62
+ * Optional name identifier for this AIGNE instance.
63
+ */
36
64
  name;
65
+ /**
66
+ * Optional description of this AIGNE instance's purpose or functionality.
67
+ */
37
68
  description;
38
- messageQueue = new message_queue_js_1.MessageQueue();
69
+ /**
70
+ * Global model to use for all agents that don't specify their own model.
71
+ */
39
72
  model;
73
+ /**
74
+ * Usage limits applied to this AIGNE instance's execution.
75
+ */
76
+ limits;
77
+ /**
78
+ * Message queue for handling inter-agent communication.
79
+ *
80
+ * @hidden
81
+ */
82
+ messageQueue = new message_queue_js_1.MessageQueue();
83
+ /**
84
+ * Collection of skill agents available to this AIGNE instance.
85
+ * Provides indexed access by skill name.
86
+ */
40
87
  skills = (0, type_utils_js_1.createAccessorArray)([], (arr, name) => arr.find((i) => i.name === name));
88
+ /**
89
+ * Collection of primary agents managed by this AIGNE instance.
90
+ * Provides indexed access by agent name.
91
+ */
41
92
  agents = (0, type_utils_js_1.createAccessorArray)([], (arr, name) => arr.find((i) => i.name === name));
42
- limits;
93
+ /**
94
+ * Adds one or more agents to this AIGNE instance.
95
+ * Each agent is attached to this AIGNE instance, allowing it to access the AIGNE's resources.
96
+ *
97
+ * @param agents - One or more Agent instances to add to this AIGNE.
98
+ */
43
99
  addAgent(...agents) {
44
100
  (0, type_utils_js_1.checkArguments)("AIGNE.addAgent", aigneAddAgentArgsSchema, agents);
45
101
  for (const agent of agents) {
@@ -47,21 +103,61 @@ class AIGNE {
47
103
  agent.attach(this);
48
104
  }
49
105
  }
106
+ /**
107
+ * Creates a new execution context for this AIGNE instance.
108
+ * Contexts isolate state for different flows or conversations.
109
+ *
110
+ * @returns A new AIGNEContext instance bound to this AIGNE.
111
+ */
50
112
  newContext() {
51
113
  return new context_js_1.AIGNEContext(this);
52
114
  }
53
- publish = ((...args) => {
54
- return new context_js_1.AIGNEContext(this).publish(...args);
55
- });
56
- invoke = ((...args) => {
57
- return new context_js_1.AIGNEContext(this).invoke(...args);
58
- });
59
- subscribe = ((...args) => {
60
- return this.messageQueue.subscribe(...args);
61
- });
62
- unsubscribe = ((...args) => {
63
- this.messageQueue.unsubscribe(...args);
64
- });
115
+ invoke(agent, message, options) {
116
+ return new context_js_1.AIGNEContext(this).invoke(agent, message, options);
117
+ }
118
+ /**
119
+ * Publishes a message to the message queue for inter-agent communication.
120
+ * This method broadcasts a message to all subscribers of the specified topic(s).
121
+ * It creates a new context internally and delegates to the context's publish method.
122
+ *
123
+ * @param topic - The topic or array of topics to publish the message to
124
+ * @param payload - The message payload to be delivered to subscribers
125
+ *
126
+ * @example
127
+ * Here's an example of how to publish a message:
128
+ * {@includeCode ../../test/aigne/aigne.test.ts#example-publish-message}
129
+ */
130
+ publish(topic, payload) {
131
+ return new context_js_1.AIGNEContext(this).publish(topic, payload);
132
+ }
133
+ subscribe(topic, listener) {
134
+ return this.messageQueue.subscribe(topic, listener);
135
+ }
136
+ /**
137
+ * Unsubscribes a listener from a specific topic in the message queue.
138
+ * This method stops a previously registered listener from receiving further messages.
139
+ * It should be called when message processing is complete or when the component is no longer interested
140
+ * in messages published to the specified topic.
141
+ *
142
+ * @param topic - The topic to unsubscribe from
143
+ * @param listener - The listener function that was previously subscribed to the topic
144
+ *
145
+ * @example
146
+ * {@includeCode ../../test/aigne/aigne.test.ts#example-subscribe-topic}
147
+ */
148
+ unsubscribe(topic, listener) {
149
+ this.messageQueue.unsubscribe(topic, listener);
150
+ }
151
+ /**
152
+ * Gracefully shuts down the AIGNE instance and all its agents and skills.
153
+ * This ensures proper cleanup of resources before termination.
154
+ *
155
+ * @returns A promise that resolves when shutdown is complete.
156
+ *
157
+ * @example
158
+ * Here's an example of shutdown an AIGNE instance:
159
+ * {@includeCode ../../test/aigne/aigne.test.ts#example-shutdown}
160
+ */
65
161
  async shutdown() {
66
162
  for (const tool of this.skills) {
67
163
  await tool.shutdown();
@@ -70,6 +166,20 @@ class AIGNE {
70
166
  await agent.shutdown();
71
167
  }
72
168
  }
169
+ /**
170
+ * Asynchronous dispose method for the AIGNE instance.
171
+ *
172
+ * @example
173
+ * Here's an example of using async dispose:
174
+ * {@includeCode ../../test/aigne/aigne.test.ts#example-shutdown-using}
175
+ */
176
+ async [Symbol.asyncDispose]() {
177
+ await this.shutdown();
178
+ }
179
+ /**
180
+ * Initializes handlers for process exit events to ensure clean shutdown.
181
+ * This registers handlers for SIGINT and exit events to properly terminate all agents.
182
+ */
73
183
  initProcessExitHandler() {
74
184
  const shutdownAndExit = () => this.shutdown().finally(() => process.exit(0));
75
185
  process.on("SIGINT", shutdownAndExit);
@@ -6,12 +6,18 @@ import { type OmitPropertiesFromArrayFirstElement } from "../utils/type-utils.js
6
6
  import type { Args, Listener, TypedEventEmitter } from "../utils/typed-event-emtter.js";
7
7
  import { type MessagePayload, MessageQueue, type MessageQueueListener, type Unsubscribe } from "./message-queue.js";
8
8
  import { type ContextLimits, type ContextUsage } from "./usage.js";
9
+ /**
10
+ * @hidden
11
+ */
9
12
  export interface AgentEvent {
10
13
  parentContextId?: string;
11
14
  contextId: string;
12
15
  timestamp: number;
13
16
  agent: Agent;
14
17
  }
18
+ /**
19
+ * @hidden
20
+ */
15
21
  export interface ContextEventMap {
16
22
  agentStarted: [AgentEvent & {
17
23
  input: Message;
@@ -23,13 +29,22 @@ export interface ContextEventMap {
23
29
  error: Error;
24
30
  }];
25
31
  }
32
+ /**
33
+ * @hidden
34
+ */
26
35
  export type ContextEmitEventMap = {
27
36
  [K in keyof ContextEventMap]: OmitPropertiesFromArrayFirstElement<ContextEventMap[K], "contextId" | "parentContextId" | "timestamp">;
28
37
  };
38
+ /**
39
+ * @hidden
40
+ */
29
41
  export interface InvokeOptions extends AgentInvokeOptions {
30
42
  returnActiveAgent?: boolean;
31
43
  disableTransfer?: boolean;
32
44
  }
45
+ /**
46
+ * @hidden
47
+ */
33
48
  export interface Context extends TypedEventEmitter<ContextEventMap, ContextEmitEventMap> {
34
49
  model?: ChatModel;
35
50
  skills?: Agent[];
@@ -76,12 +91,12 @@ export interface Context extends TypedEventEmitter<ContextEventMap, ContextEmitE
76
91
  * @param topic topic name, or an array of topic names
77
92
  * @param payload message to publish
78
93
  */
79
- publish(topic: string | string[], payload: Omit<MessagePayload, "context">): void;
80
- subscribe(topic: string, listener?: undefined): Promise<MessagePayload>;
81
- subscribe(topic: string, listener: MessageQueueListener): Unsubscribe;
82
- subscribe(topic: string, listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
83
- subscribe(topic: string, listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
84
- unsubscribe(topic: string, listener: MessageQueueListener): void;
94
+ publish(topic: string | string[], payload: Omit<MessagePayload, "context"> | Message | string): void;
95
+ subscribe(topic: string | string[], listener?: undefined): Promise<MessagePayload>;
96
+ subscribe(topic: string | string[], listener: MessageQueueListener): Unsubscribe;
97
+ subscribe(topic: string | string[], listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
98
+ subscribe(topic: string | string[], listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
99
+ unsubscribe(topic: string | string[], listener: MessageQueueListener): void;
85
100
  /**
86
101
  * Create a child context with the same configuration as the parent context.
87
102
  * If `reset` is true, the child context will have a new state (such as: usage).
@@ -94,7 +109,9 @@ export interface Context extends TypedEventEmitter<ContextEventMap, ContextEmitE
94
109
  reset?: boolean;
95
110
  }): Context;
96
111
  }
97
- export declare function createPublishMessage(message: string | Message, from?: Agent): Omit<MessagePayload, "context">;
112
+ /**
113
+ * @hidden
114
+ */
98
115
  export declare class AIGNEContext implements Context {
99
116
  constructor(parent?: ConstructorParameters<typeof AIGNEContextInternal>[0]);
100
117
  parentId?: string;
@@ -109,7 +126,6 @@ export declare class AIGNEContext implements Context {
109
126
  reset?: boolean;
110
127
  }): AIGNEContext;
111
128
  invoke: Context["invoke"];
112
- private onInvokeSuccess;
113
129
  publish: Context["publish"];
114
130
  subscribe: Context["subscribe"];
115
131
  unsubscribe: Context["unsubscribe"];
@@ -4,7 +4,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.AIGNEContext = void 0;
7
- exports.createPublishMessage = createPublishMessage;
8
7
  const node_events_1 = __importDefault(require("node:events"));
9
8
  const uuid_1 = require("uuid");
10
9
  const zod_1 = require("zod");
@@ -16,13 +15,9 @@ const stream_utils_js_1 = require("../utils/stream-utils.js");
16
15
  const type_utils_js_1 = require("../utils/type-utils.js");
17
16
  const message_queue_js_1 = require("./message-queue.js");
18
17
  const usage_js_1 = require("./usage.js");
19
- function createPublishMessage(message, from) {
20
- return {
21
- role: !from || from instanceof user_agent_js_1.UserAgent ? "user" : "agent",
22
- source: from?.name,
23
- message: (0, prompt_builder_js_1.createMessage)(message),
24
- };
25
- }
18
+ /**
19
+ * @hidden
20
+ */
26
21
  class AIGNEContext {
27
22
  constructor(parent) {
28
23
  if (parent instanceof AIGNEContext) {
@@ -73,15 +68,13 @@ class AIGNEContext {
73
68
  return Promise.resolve(newContext.internal.invoke(agent, msg, newContext, options)).then(async (response) => {
74
69
  if (!options?.streaming) {
75
70
  const { __activeAgent__: activeAgent, ...output } = await (0, stream_utils_js_1.agentResponseStreamToObject)(response);
76
- this.onInvokeSuccess(activeAgent, output, newContext);
77
71
  if (options?.returnActiveAgent) {
78
72
  return [output, activeAgent];
79
73
  }
80
74
  return output;
81
75
  }
82
76
  const activeAgentPromise = Promise.withResolvers();
83
- const stream = (0, stream_utils_js_1.onAgentResponseStreamEnd)((0, stream_utils_js_1.asyncGeneratorToReadableStream)(response), async ({ __activeAgent__: activeAgent, ...output }) => {
84
- this.onInvokeSuccess(activeAgent, output, newContext);
77
+ const stream = (0, stream_utils_js_1.onAgentResponseStreamEnd)((0, stream_utils_js_1.asyncGeneratorToReadableStream)(response), async ({ __activeAgent__: activeAgent }) => {
85
78
  activeAgentPromise.resolve(activeAgent);
86
79
  }, {
87
80
  processChunk(chunk) {
@@ -103,18 +96,11 @@ class AIGNEContext {
103
96
  return stream;
104
97
  });
105
98
  });
106
- async onInvokeSuccess(activeAgent, output, context) {
107
- if (activeAgent instanceof agent_js_1.Agent) {
108
- const publishTopics = typeof activeAgent.publishTopic === "function"
109
- ? await activeAgent.publishTopic(output)
110
- : activeAgent.publishTopic;
111
- if (publishTopics?.length) {
112
- context.publish(publishTopics, createPublishMessage(output, activeAgent));
113
- }
114
- }
115
- }
116
99
  publish = ((topic, payload) => {
117
- return this.internal.messageQueue.publish(topic, { ...payload, context: this });
100
+ return this.internal.messageQueue.publish(topic, {
101
+ ...(0, message_queue_js_1.toMessagePayload)(payload),
102
+ context: this,
103
+ });
118
104
  });
119
105
  subscribe = ((...args) => {
120
106
  return this.internal.messageQueue.subscribe(...args);