@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,78 +1,462 @@
1
1
  import { inspect } from "node:util";
2
- import { type ZodType } from "zod";
2
+ import { ZodObject, type ZodType } from "zod";
3
3
  import type { Context } from "../aigne/context.js";
4
+ import type { MessagePayload } from "../aigne/message-queue.js";
5
+ import type { MemoryAgent } from "../memory/memory.js";
4
6
  import { type Nullish, type PromiseOrValue } from "../utils/type-utils.js";
5
- import { AgentMemory, type AgentMemoryOptions } from "./memory.js";
6
7
  import { type TransferAgentOutput } from "./types.js";
8
+ export * from "./types.js";
9
+ /**
10
+ * Basic message type that can contain any key-value pairs
11
+ */
7
12
  export type Message = Record<string, unknown>;
13
+ /**
14
+ * Topics the agent subscribes to, can be a single topic string or an array of topic strings
15
+ */
8
16
  export type SubscribeTopic = string | string[];
17
+ /**
18
+ * Topics the agent publishes to, can be:
19
+ * - A single topic string
20
+ * - An array of topic strings
21
+ * - A function that receives the output and returns topic(s)
22
+ *
23
+ * @template O The agent output message type
24
+ */
9
25
  export type PublishTopic<O extends Message> = string | string[] | ((output: O) => PromiseOrValue<Nullish<string | string[]>>);
26
+ /**
27
+ * Configuration options for an agent
28
+ *
29
+ * @template I The agent input message type
30
+ * @template O The agent output message type
31
+ */
10
32
  export interface AgentOptions<I extends Message = Message, O extends Message = Message> {
33
+ /**
34
+ * Topics the agent should subscribe to
35
+ *
36
+ * These topics determine which messages the agent will receive
37
+ * from the system
38
+ */
11
39
  subscribeTopic?: SubscribeTopic;
40
+ /**
41
+ * Topics the agent should publish to
42
+ *
43
+ * These topics determine where the agent's output messages
44
+ * will be sent in the system
45
+ */
12
46
  publishTopic?: PublishTopic<O>;
47
+ /**
48
+ * Name of the agent
49
+ *
50
+ * Used for identification and logging. Defaults to the constructor name
51
+ * if not specified
52
+ */
13
53
  name?: string;
54
+ /**
55
+ * Description of the agent
56
+ *
57
+ * A human-readable description of what the agent does, useful
58
+ * for documentation and debugging
59
+ */
14
60
  description?: string;
61
+ /**
62
+ * Zod schema defining the input message structure
63
+ *
64
+ * Used to validate that input messages conform to the expected format
65
+ */
15
66
  inputSchema?: AgentInputOutputSchema<I>;
67
+ /**
68
+ * Zod schema defining the output message structure
69
+ *
70
+ * Used to validate that output messages conform to the expected format
71
+ */
16
72
  outputSchema?: AgentInputOutputSchema<O>;
73
+ /**
74
+ * Whether to include input in the output
75
+ *
76
+ * When true, the agent will merge input fields into the output object
77
+ */
17
78
  includeInputInOutput?: boolean;
79
+ /**
80
+ * List of skills (other agents or functions) this agent has
81
+ *
82
+ * These skills can be used by the agent to delegate tasks or
83
+ * extend its capabilities
84
+ */
18
85
  skills?: (Agent | FunctionAgentFn)[];
86
+ /**
87
+ * Whether to disable emitting events for agent actions
88
+ *
89
+ * When true, the agent won't emit events like agentStarted,
90
+ * agentSucceed, or agentFailed
91
+ */
19
92
  disableEvents?: boolean;
20
- memory?: AgentMemory | AgentMemoryOptions | true;
93
+ /**
94
+ * One or more memory agents this agent can use
95
+ */
96
+ memory?: MemoryAgent | MemoryAgent[];
21
97
  }
98
+ export declare const agentOptionsSchema: ZodObject<{
99
+ [key in keyof AgentOptions]: ZodType<AgentOptions[key]>;
100
+ }>;
22
101
  export interface AgentInvokeOptions {
102
+ /**
103
+ * Whether to enable streaming response
104
+ *
105
+ * When true, the invoke method returns a ReadableStream that emits
106
+ * chunks of the response as they become available, allowing for
107
+ * real-time display of results
108
+ *
109
+ * When false or undefined, the invoke method waits for full completion
110
+ * and returns the final JSON result
111
+ */
23
112
  streaming?: boolean;
24
113
  }
114
+ /**
115
+ * Agent is the base class for all agents.
116
+ * It provides a mechanism for defining input/output schemas and implementing processing logic,
117
+ * serving as the foundation of the entire agent system.
118
+ *
119
+ * By extending the Agent class and implementing the process method, you can create custom agents
120
+ * with various capabilities:
121
+ * - Process structured input and output data
122
+ * - Validate data formats using schemas
123
+ * - Communicate between agents through contexts
124
+ * - Support streaming or non-streaming responses
125
+ * - Maintain memory of past interactions
126
+ * - Output in multiple formats (JSON/text)
127
+ * - Forward tasks to other agents
128
+ *
129
+ * @template I The input message type the agent accepts
130
+ * @template O The output message type the agent returns
131
+ *
132
+ * @example
133
+ * Here's an example of how to create a custom agent:
134
+ * {@includeCode ../../test/agents/agent.test.ts#example-custom-agent}
135
+ */
25
136
  export declare abstract class Agent<I extends Message = Message, O extends Message = Message> {
26
- constructor({ inputSchema, outputSchema, ...options }: AgentOptions<I, O>);
27
- readonly memory?: AgentMemory;
137
+ constructor(options?: AgentOptions<I, O>);
138
+ /**
139
+ * List of memories this agent can use
140
+ */
141
+ readonly memories: MemoryAgent[];
142
+ /**
143
+ * Name of the agent, used for identification and logging
144
+ *
145
+ * Defaults to the class constructor name if not specified in options
146
+ */
28
147
  readonly name: string;
29
148
  /**
30
- * Default topic this agent will subscribe to
149
+ * Default topic this agent subscribes to
150
+ *
151
+ * Each agent has a default topic in the format "$agent_[agent name]"
152
+ * The agent automatically subscribes to this topic to receive messages
153
+ *
154
+ * @returns The default topic string
31
155
  */
32
156
  get topic(): string;
157
+ /**
158
+ * Description of the agent's purpose and capabilities
159
+ *
160
+ * Useful for documentation and when agents need to understand
161
+ * each other's roles in a multi-agent system
162
+ */
33
163
  readonly description?: string;
34
164
  private readonly _inputSchema?;
35
165
  private readonly _outputSchema?;
166
+ /**
167
+ * Get the input data schema for this agent
168
+ *
169
+ * Used to validate that input messages conform to required format
170
+ * If no input schema is set, returns an empty object schema by default
171
+ *
172
+ * @returns The Zod type definition for input data
173
+ */
36
174
  get inputSchema(): ZodType<I>;
175
+ /**
176
+ * Get the output data schema for this agent
177
+ *
178
+ * Used to validate that output messages conform to required format
179
+ * If no output schema is set, returns an empty object schema by default
180
+ *
181
+ * @returns The Zod type definition for output data
182
+ */
37
183
  get outputSchema(): ZodType<O>;
184
+ /**
185
+ * Whether to include the original input in the output
186
+ *
187
+ * When true, the agent will merge input fields into the output object
188
+ */
38
189
  readonly includeInputInOutput?: boolean;
190
+ /**
191
+ * Topics the agent subscribes to for receiving messages
192
+ *
193
+ * Can be a single topic string or an array of topic strings
194
+ */
39
195
  readonly subscribeTopic?: SubscribeTopic;
196
+ /**
197
+ * Topics the agent publishes to for sending messages
198
+ *
199
+ * Can be a string, array of strings, or a function that determines
200
+ * topics based on the output
201
+ */
40
202
  readonly publishTopic?: PublishTopic<Message>;
203
+ /**
204
+ * Collection of skills (other agents) this agent can use
205
+ *
206
+ * Skills can be accessed by name or by array index, allowing
207
+ * the agent to delegate tasks to specialized sub-agents
208
+ */
41
209
  readonly skills: Agent<Message, Message>[] & {
42
210
  [key: string]: Agent<Message, Message>;
43
211
  };
212
+ /**
213
+ * Whether to disable emitting events for agent actions
214
+ *
215
+ * When true, the agent won't emit events like agentStarted,
216
+ * agentSucceed, or agentFailed
217
+ */
44
218
  private disableEvents?;
219
+ private subscriptions;
45
220
  /**
46
221
  * Attach agent to context:
47
- * - subscribe to topic and invoke process method when message received
48
- * - subscribe to memory topic if memory is enabled
49
- * @param context Context to attach
222
+ * - Subscribe to topics and invoke process method when messages are received
223
+ * - Subscribe to memory topics if memory is enabled
224
+ *
225
+ * Agents can receive messages and respond through the topic subscription system,
226
+ * enabling inter-agent communication.
227
+ *
228
+ * @param context Context to attach to
50
229
  */
51
230
  attach(context: Pick<Context, "subscribe">): void;
231
+ protected subscribeToTopics(context: Pick<Context, "subscribe">): void;
232
+ onMessage({ message, context }: MessagePayload): Promise<void>;
233
+ /**
234
+ * Add skills (other agents or functions) to this agent
235
+ *
236
+ * Skills allow agents to reuse functionality from other agents,
237
+ * building more complex behaviors.
238
+ *
239
+ * @param skills List of skills to add, can be Agent instances or functions
240
+ */
52
241
  addSkill(...skills: (Agent | FunctionAgentFn)[]): void;
242
+ /**
243
+ * Check if the agent is invokable
244
+ *
245
+ * An agent is invokable if it has implemented the process method
246
+ */
53
247
  get isInvokable(): boolean;
248
+ /**
249
+ * Check context status to ensure it hasn't timed out
250
+ *
251
+ * @param context The context to check
252
+ * @throws Error if the context has timed out
253
+ */
54
254
  private checkContextStatus;
55
255
  private newDefaultContext;
56
- invoke(input: I | string, context: Context | undefined, options: AgentInvokeOptions & {
57
- streaming: true;
58
- }): Promise<AgentResponseStream<O>>;
256
+ /**
257
+ * Invoke the agent with regular (non-streaming) response
258
+ *
259
+ * Regular mode waits for the agent to complete processing and return the final result,
260
+ * suitable for scenarios where a complete result is needed at once.
261
+ *
262
+ * @param input Input message to the agent, can be a string or structured object
263
+ * @param context Execution context, providing environment and resource access
264
+ * @param options Invocation options, must set streaming to false or leave unset
265
+ * @returns Final JSON response
266
+ *
267
+ * @example
268
+ * Here's an example of invoking an agent with regular mode:
269
+ * {@includeCode ../../test/agents/agent.test.ts#example-invoke}
270
+ */
59
271
  invoke(input: I | string, context?: Context, options?: AgentInvokeOptions & {
60
272
  streaming?: false;
61
273
  }): Promise<O>;
274
+ /**
275
+ * Invoke the agent with streaming response
276
+ *
277
+ * Streaming responses allow the agent to return results incrementally,
278
+ * suitable for scenarios requiring real-time progress updates, such as
279
+ * chat bot typing effects.
280
+ *
281
+ * @param input Input message to the agent, can be a string or structured object
282
+ * @param context Execution context, providing environment and resource access
283
+ * @param options Invocation options, must set streaming to true for this overload
284
+ * @returns Streaming response object
285
+ *
286
+ * @example
287
+ * Here's an example of invoking an agent with streaming response:
288
+ * {@includeCode ../../test/agents/agent.test.ts#example-invoke-streaming}
289
+ */
290
+ invoke(input: I | string, context: Context | undefined, options: {
291
+ streaming: true;
292
+ }): Promise<AgentResponseStream<O>>;
293
+ /**
294
+ * General signature for invoking the agent
295
+ *
296
+ * Returns either streaming or regular response based on the streaming parameter in options
297
+ *
298
+ * @param input Input message to the agent
299
+ * @param context Execution context
300
+ * @param options Invocation options
301
+ * @returns Agent response (streaming or regular)
302
+ */
62
303
  invoke(input: I | string, context?: Context, options?: AgentInvokeOptions): Promise<AgentResponse<O>>;
304
+ /**
305
+ * Process agent output
306
+ *
307
+ * Validates output format, applies post-processing operations, and triggers success events
308
+ *
309
+ * @param input Original input message
310
+ * @param output Raw output produced by the agent
311
+ * @param context Execution context
312
+ * @returns Final processed output
313
+ */
63
314
  private processAgentOutput;
315
+ /**
316
+ * Process errors that occur during agent execution
317
+ *
318
+ * Logs error information, triggers failure events, and re-throws the error
319
+ *
320
+ * @param error Caught error
321
+ * @param context Execution context
322
+ * @throws Always throws the received error
323
+ */
64
324
  private processAgentError;
325
+ /**
326
+ * Check agent invocation usage to prevent exceeding limits
327
+ *
328
+ * If the context has a maximum invocation limit set, checks if the limit
329
+ * has been exceeded and increments the invocation counter
330
+ *
331
+ * @param context Execution context
332
+ * @throws Error if maximum invocation limit is exceeded
333
+ */
65
334
  protected checkAgentInvokesUsage(context: Context): void;
335
+ /**
336
+ * Pre-processing operations before handling input
337
+ *
338
+ * Preparatory work done before executing the agent's main processing logic, including:
339
+ * - Checking context status
340
+ * - Verifying invocation limits
341
+ *
342
+ * @param _ Input message (unused)
343
+ * @param context Execution context
344
+ */
66
345
  protected preprocess(_: I, context: Context): void;
346
+ /**
347
+ * Post-processing operations after handling output
348
+ *
349
+ * Operations performed after the agent produces output, including:
350
+ * - Checking context status
351
+ * - Adding interaction records to memory
352
+ *
353
+ * @param input Input message
354
+ * @param output Output message
355
+ * @param context Execution context
356
+ */
67
357
  protected postprocess(input: I, output: O, context: Context): void;
68
- abstract process(input: I, context: Context, options?: AgentInvokeOptions): PromiseOrValue<AgentProcessResult<O>>;
358
+ protected publishToTopics(output: Message, context: Context): Promise<void>;
359
+ /**
360
+ * Core processing method of the agent, must be implemented in subclasses
361
+ *
362
+ * This is the main functionality implementation of the agent, processing input and
363
+ * generating output. Can return various types of results:
364
+ * - Regular object response
365
+ * - Streaming response
366
+ * - Async generator
367
+ * - Another agent instance (transfer agent)
368
+ *
369
+ * @param input Input message
370
+ * @param context Execution context
371
+ * @returns Processing result
372
+ *
373
+ * @example
374
+ * Example of returning a direct object:
375
+ * {@includeCode ../../test/agents/agent.test.ts#example-process-direct-response}
376
+ *
377
+ * @example
378
+ * Example of returning a streaming response:
379
+ * {@includeCode ../../test/agents/agent.test.ts#example-process-streaming-response}
380
+ *
381
+ * @example
382
+ * Example of using an async generator:
383
+ * {@includeCode ../../test/agents/agent.test.ts#example-process-async-generator}
384
+ *
385
+ * @example
386
+ * Example of transfer to another agent:
387
+ * {@includeCode ../../test/agents/agent.test.ts#example-process-transfer}
388
+ */
389
+ abstract process(input: I, context: Context): PromiseOrValue<AgentProcessResult<O>>;
390
+ /**
391
+ * Shut down the agent and clean up resources
392
+ *
393
+ * Primarily used to clean up memory and other resources to prevent memory leaks
394
+ *
395
+ * @example
396
+ * Here's an example of shutting down an agent:
397
+ * {@includeCode ../../test/agents/agent.test.ts#example-agent-shutdown}
398
+ *
399
+ * @example
400
+ * Here's an example of shutting down an agent by using statement:
401
+ * {@includeCode ../../test/agents/agent.test.ts#example-agent-shutdown-by-using}
402
+ */
69
403
  shutdown(): Promise<void>;
404
+ /**
405
+ * Custom object inspection behavior
406
+ *
407
+ * When using Node.js's util.inspect function to inspect an agent,
408
+ * only the agent's name will be shown, making output more concise
409
+ *
410
+ * @returns Agent name
411
+ */
70
412
  [inspect.custom](): string;
413
+ /**
414
+ * Async dispose method for shutdown the agent
415
+ *
416
+ * @example
417
+ * Here's an example of shutting down an agent by using statement:
418
+ * {@includeCode ../../test/agents/agent.test.ts#example-agent-shutdown-by-using}
419
+ */
420
+ [Symbol.asyncDispose](): Promise<void>;
71
421
  }
422
+ /**
423
+ * Response type for an agent, can be:
424
+ * - Direct response object
425
+ * - Output transferred to another agent
426
+ * - Streaming response
427
+ *
428
+ * @template T Response data type
429
+ */
72
430
  export type AgentResponse<T> = T | TransferAgentOutput | AgentResponseStream<T>;
431
+ /**
432
+ * Streaming response type for an agent
433
+ *
434
+ * @template T Response data type
435
+ */
73
436
  export type AgentResponseStream<T> = ReadableStream<AgentResponseChunk<T>>;
437
+ /**
438
+ * Data chunk type for streaming responses
439
+ *
440
+ * @template T Response data type
441
+ */
74
442
  export type AgentResponseChunk<T> = AgentResponseDelta<T>;
443
+ /**
444
+ * Check if a response chunk is empty
445
+ *
446
+ * @template T Response data type
447
+ * @param chunk The response chunk to check
448
+ * @returns True if the chunk is empty
449
+ */
75
450
  export declare function isEmptyChunk<T>(chunk: AgentResponseChunk<T>): boolean;
451
+ /**
452
+ * Incremental data structure for agent responses
453
+ *
454
+ * Used to represent a single incremental update in a streaming response
455
+ *
456
+ * @template T Response data type
457
+ * @property delta.text - Text format incremental update
458
+ * @property delta.json - JSON format incremental update
459
+ */
76
460
  export interface AgentResponseDelta<T> {
77
461
  delta: {
78
462
  text?: Partial<{
@@ -83,16 +467,139 @@ export interface AgentResponseDelta<T> {
83
467
  json?: Partial<T | TransferAgentOutput>;
84
468
  };
85
469
  }
470
+ /**
471
+ * Creates a text delta for streaming responses
472
+ *
473
+ * This utility function creates an AgentResponseDelta object with only the text part,
474
+ * useful for incrementally building streaming text responses in agents.
475
+ *
476
+ * @template T Agent message type extending Message
477
+ * @param textDelta The text content to include in the delta update
478
+ * @returns An AgentResponseDelta with the text delta wrapped in the expected structure
479
+ */
480
+ export declare function textDelta<T extends Message>(textDelta: NonNullable<AgentResponseDelta<T>["delta"]["text"]>): AgentResponseDelta<T>;
481
+ /**
482
+ * Creates a JSON delta for streaming responses
483
+ *
484
+ * This utility function creates an AgentResponseDelta object with only the JSON part,
485
+ * useful for incrementally building structured data responses in streaming mode.
486
+ *
487
+ * @template T Agent message type extending Message
488
+ * @param jsonDelta The JSON data to include in the delta update
489
+ * @returns An AgentResponseDelta with the JSON delta wrapped in the expected structure
490
+ */
491
+ export declare function jsonDelta<T extends Message>(jsonDelta: NonNullable<AgentResponseDelta<T>["delta"]["json"]>): AgentResponseDelta<T>;
492
+ /**
493
+ * Async generator type for agent processing
494
+ *
495
+ * Used to generate streaming response data
496
+ *
497
+ * @template O Agent output message type
498
+ */
86
499
  export type AgentProcessAsyncGenerator<O extends Message> = AsyncGenerator<AgentResponseChunk<O>, Partial<O | TransferAgentOutput> | undefined | void>;
500
+ /**
501
+ * Result type for agent processing method, can be:
502
+ * - Direct or streaming response
503
+ * - Async generator
504
+ * - Another agent instance (for task forwarding)
505
+ *
506
+ * @template O Agent output message type
507
+ */
87
508
  export type AgentProcessResult<O extends Message> = AgentResponse<O> | AgentProcessAsyncGenerator<O> | Agent;
509
+ /**
510
+ * Schema definition type for agent input/output
511
+ *
512
+ * Can be a Zod type definition or a function that returns a Zod type
513
+ *
514
+ * @template I Agent input/output message type
515
+ */
88
516
  export type AgentInputOutputSchema<I extends Message = Message> = ZodType<I> | ((agent: Agent) => ZodType<I>);
517
+ /**
518
+ * Configuration options for a function agent
519
+ *
520
+ * Extends the base agent options and adds function implementation
521
+ *
522
+ * @template I Agent input message type
523
+ * @template O Agent output message type
524
+ */
89
525
  export interface FunctionAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
90
- fn?: FunctionAgentFn<I, O>;
526
+ /**
527
+ * Function implementing the agent's processing logic
528
+ *
529
+ * This function is called by the process method to handle input
530
+ * and generate output
531
+ */
532
+ process: FunctionAgentFn<I, O>;
91
533
  }
534
+ /**
535
+ * Function agent class, implements agent logic through a function
536
+ *
537
+ * Provides a convenient way to create agents using functions without
538
+ * needing to extend the Agent class
539
+ *
540
+ * @template I Agent input message type
541
+ * @template O Agent output message type
542
+ *
543
+ * @example
544
+ * Here's an example of creating a function agent:
545
+ * {@includeCode ../../test/agents/agent.test.ts#example-function-agent}
546
+ */
92
547
  export declare class FunctionAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
548
+ /**
549
+ * Create a function agent from a function or options
550
+ *
551
+ * Provides a convenient factory method to create an agent directly from a function
552
+ *
553
+ * @param options Function agent options or function
554
+ * @returns New function agent instance
555
+ *
556
+ * @example
557
+ * Here's an example of creating a function agent from a function:
558
+ * {@includeCode ../../test/agents/agent.test.ts#example-function-agent-from-function}
559
+ *
560
+ * @example
561
+ * Here's an example of creating a function agent without basic agent options:
562
+ * {@includeCode ../../test/agents/agent.test.ts#example-function-agent}
563
+ *
564
+ * @example
565
+ * Here's an example of creating a function agent from a function returning a stream:
566
+ * {@includeCode ../../test/agents/agent.test.ts#example-function-agent-stream}
567
+ *
568
+ * @example
569
+ * Here's an example of creating a function agent from a function returning an async generator:
570
+ * {@includeCode ../../test/agents/agent.test.ts#example-function-agent-async-generator}
571
+ */
93
572
  static from<I extends Message, O extends Message>(options: FunctionAgentOptions<I, O> | FunctionAgentFn<I, O>): FunctionAgent<I, O>;
573
+ /**
574
+ * Create a function agent instance
575
+ *
576
+ * @param options Function agent configuration options
577
+ */
94
578
  constructor(options: FunctionAgentOptions<I, O>);
95
- fn: FunctionAgentFn<I, O>;
579
+ /**
580
+ * Stores the function used to process agent input and generate output
581
+ *
582
+ * @private
583
+ */
584
+ _process: FunctionAgentFn<I, O>;
585
+ /**
586
+ * Process input implementation, calls the configured processing function
587
+ *
588
+ * @param input Input message
589
+ * @param context Execution context
590
+ * @returns Processing result
591
+ */
96
592
  process(input: I, context: Context): PromiseOrValue<AgentProcessResult<O>>;
97
593
  }
594
+ /**
595
+ * Function type for function agents
596
+ *
597
+ * Defines the function signature for processing messages in a function agent
598
+ *
599
+ * @template I Agent input message type
600
+ * @template O Agent output message type
601
+ * @param input Input message
602
+ * @param context Execution context
603
+ * @returns Processing result, can be synchronous or asynchronous
604
+ */
98
605
  export type FunctionAgentFn<I extends Message = any, O extends Message = any> = (input: I, context: Context) => PromiseOrValue<AgentProcessResult<O>>;