@aigne/core 1.15.0 → 1.17.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 (194) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.md +9 -7
  3. package/README.zh.md +9 -7
  4. package/lib/cjs/agents/agent.d.ts +168 -29
  5. package/lib/cjs/agents/agent.js +152 -56
  6. package/lib/cjs/agents/ai-agent.d.ts +7 -7
  7. package/lib/cjs/agents/ai-agent.js +19 -15
  8. package/lib/{esm/models → cjs/agents}/chat-model.d.ts +29 -19
  9. package/lib/cjs/{models → agents}/chat-model.js +56 -15
  10. package/lib/cjs/agents/guide-rail-agent.d.ts +62 -0
  11. package/lib/cjs/agents/guide-rail-agent.js +14 -0
  12. package/lib/cjs/agents/mcp-agent.d.ts +3 -4
  13. package/lib/cjs/agents/mcp-agent.js +11 -11
  14. package/lib/cjs/agents/team-agent.d.ts +7 -8
  15. package/lib/cjs/agents/team-agent.js +10 -10
  16. package/lib/cjs/agents/user-agent.d.ts +4 -4
  17. package/lib/cjs/agents/user-agent.js +10 -10
  18. package/lib/cjs/aigne/aigne.d.ts +13 -11
  19. package/lib/cjs/aigne/aigne.js +7 -6
  20. package/lib/cjs/aigne/context.d.ts +19 -8
  21. package/lib/cjs/aigne/context.js +27 -7
  22. package/lib/cjs/index.d.ts +2 -1
  23. package/lib/cjs/index.js +2 -1
  24. package/lib/cjs/loader/agent-yaml.d.ts +2 -2
  25. package/lib/cjs/loader/index.d.ts +18 -11
  26. package/lib/cjs/loader/index.js +8 -27
  27. package/lib/cjs/memory/default-memory/default-memory-storage/index.d.ts +30 -0
  28. package/lib/cjs/memory/default-memory/default-memory-storage/index.js +69 -0
  29. package/lib/cjs/memory/default-memory/default-memory-storage/migrate.d.ts +7 -0
  30. package/lib/cjs/memory/default-memory/default-memory-storage/migrate.js +53 -0
  31. package/lib/cjs/memory/default-memory/default-memory-storage/migrations/20250523165801-init.d.ts +7 -0
  32. package/lib/cjs/memory/default-memory/default-memory-storage/migrations/20250523165801-init.js +33 -0
  33. package/lib/cjs/memory/default-memory/default-memory-storage/models/memory.d.ts +10 -0
  34. package/lib/cjs/memory/default-memory/default-memory-storage/models/memory.js +32 -0
  35. package/lib/cjs/memory/default-memory/index.d.ts +10 -0
  36. package/lib/cjs/memory/{default-memory.js → default-memory/index.js} +16 -25
  37. package/lib/cjs/memory/default-memory/storage.d.ts +13 -0
  38. package/lib/cjs/memory/default-memory/storage.js +6 -0
  39. package/lib/cjs/memory/memory.d.ts +3 -2
  40. package/lib/cjs/memory/memory.js +1 -1
  41. package/lib/cjs/memory/retriever.d.ts +2 -2
  42. package/lib/cjs/prompt/prompt-builder.d.ts +10 -4
  43. package/lib/cjs/prompt/prompt-builder.js +4 -4
  44. package/lib/cjs/prompt/template.d.ts +3 -3
  45. package/lib/cjs/utils/json-schema.js +1 -1
  46. package/lib/cjs/utils/logger.d.ts +33 -8
  47. package/lib/cjs/utils/logger.js +63 -5
  48. package/lib/cjs/utils/model-utils.d.ts +1 -1
  49. package/lib/cjs/utils/stream-utils.d.ts +3 -1
  50. package/lib/cjs/utils/stream-utils.js +31 -1
  51. package/lib/cjs/utils/type-utils.d.ts +7 -2
  52. package/lib/dts/agents/agent.d.ts +168 -29
  53. package/lib/dts/agents/ai-agent.d.ts +7 -7
  54. package/lib/{cjs/models → dts/agents}/chat-model.d.ts +29 -19
  55. package/lib/dts/agents/guide-rail-agent.d.ts +62 -0
  56. package/lib/dts/agents/mcp-agent.d.ts +3 -4
  57. package/lib/dts/agents/team-agent.d.ts +7 -8
  58. package/lib/dts/agents/user-agent.d.ts +4 -4
  59. package/lib/dts/aigne/aigne.d.ts +13 -11
  60. package/lib/dts/aigne/context.d.ts +19 -8
  61. package/lib/dts/index.d.ts +2 -1
  62. package/lib/dts/loader/agent-yaml.d.ts +2 -2
  63. package/lib/dts/loader/index.d.ts +18 -11
  64. package/lib/dts/memory/default-memory/default-memory-storage/index.d.ts +30 -0
  65. package/lib/dts/memory/default-memory/default-memory-storage/migrate.d.ts +7 -0
  66. package/lib/dts/memory/default-memory/default-memory-storage/migrations/20250523165801-init.d.ts +7 -0
  67. package/lib/dts/memory/default-memory/default-memory-storage/models/memory.d.ts +10 -0
  68. package/lib/dts/memory/default-memory/index.d.ts +10 -0
  69. package/lib/dts/memory/default-memory/storage.d.ts +13 -0
  70. package/lib/dts/memory/memory.d.ts +3 -2
  71. package/lib/dts/memory/retriever.d.ts +2 -2
  72. package/lib/dts/prompt/prompt-builder.d.ts +10 -4
  73. package/lib/dts/prompt/template.d.ts +3 -3
  74. package/lib/dts/utils/logger.d.ts +33 -8
  75. package/lib/dts/utils/model-utils.d.ts +1 -1
  76. package/lib/dts/utils/stream-utils.d.ts +3 -1
  77. package/lib/dts/utils/type-utils.d.ts +7 -2
  78. package/lib/esm/agents/agent.d.ts +168 -29
  79. package/lib/esm/agents/agent.js +152 -56
  80. package/lib/esm/agents/ai-agent.d.ts +7 -7
  81. package/lib/esm/agents/ai-agent.js +18 -14
  82. package/lib/{dts/models → esm/agents}/chat-model.d.ts +29 -19
  83. package/lib/esm/{models → agents}/chat-model.js +56 -15
  84. package/lib/esm/agents/guide-rail-agent.d.ts +62 -0
  85. package/lib/esm/agents/guide-rail-agent.js +11 -0
  86. package/lib/esm/agents/mcp-agent.d.ts +3 -4
  87. package/lib/esm/agents/mcp-agent.js +11 -11
  88. package/lib/esm/agents/team-agent.d.ts +7 -8
  89. package/lib/esm/agents/team-agent.js +10 -10
  90. package/lib/esm/agents/user-agent.d.ts +4 -4
  91. package/lib/esm/agents/user-agent.js +10 -10
  92. package/lib/esm/aigne/aigne.d.ts +13 -11
  93. package/lib/esm/aigne/aigne.js +7 -6
  94. package/lib/esm/aigne/context.d.ts +19 -8
  95. package/lib/esm/aigne/context.js +28 -8
  96. package/lib/esm/index.d.ts +2 -1
  97. package/lib/esm/index.js +2 -1
  98. package/lib/esm/loader/agent-yaml.d.ts +2 -2
  99. package/lib/esm/loader/index.d.ts +18 -11
  100. package/lib/esm/loader/index.js +8 -27
  101. package/lib/esm/memory/default-memory/default-memory-storage/index.d.ts +30 -0
  102. package/lib/esm/memory/default-memory/default-memory-storage/index.js +64 -0
  103. package/lib/esm/memory/default-memory/default-memory-storage/migrate.d.ts +7 -0
  104. package/lib/esm/memory/default-memory/default-memory-storage/migrate.js +16 -0
  105. package/lib/esm/memory/default-memory/default-memory-storage/migrations/20250523165801-init.d.ts +7 -0
  106. package/lib/esm/memory/default-memory/default-memory-storage/migrations/20250523165801-init.js +28 -0
  107. package/lib/esm/memory/default-memory/default-memory-storage/models/memory.d.ts +10 -0
  108. package/lib/esm/memory/default-memory/default-memory-storage/models/memory.js +29 -0
  109. package/lib/esm/memory/default-memory/index.d.ts +10 -0
  110. package/lib/esm/memory/{default-memory.js → default-memory/index.js} +15 -24
  111. package/lib/esm/memory/default-memory/storage.d.ts +13 -0
  112. package/lib/esm/memory/default-memory/storage.js +2 -0
  113. package/lib/esm/memory/memory.d.ts +3 -2
  114. package/lib/esm/memory/memory.js +2 -2
  115. package/lib/esm/memory/retriever.d.ts +2 -2
  116. package/lib/esm/prompt/prompt-builder.d.ts +10 -4
  117. package/lib/esm/prompt/prompt-builder.js +4 -4
  118. package/lib/esm/prompt/template.d.ts +3 -3
  119. package/lib/esm/utils/json-schema.js +1 -1
  120. package/lib/esm/utils/logger.d.ts +33 -8
  121. package/lib/esm/utils/logger.js +61 -4
  122. package/lib/esm/utils/model-utils.d.ts +1 -1
  123. package/lib/esm/utils/stream-utils.d.ts +3 -1
  124. package/lib/esm/utils/stream-utils.js +29 -1
  125. package/lib/esm/utils/type-utils.d.ts +7 -2
  126. package/package.json +4 -20
  127. package/lib/cjs/client/client.d.ts +0 -97
  128. package/lib/cjs/client/client.js +0 -87
  129. package/lib/cjs/client/index.d.ts +0 -1
  130. package/lib/cjs/client/index.js +0 -17
  131. package/lib/cjs/memory/default-memory.d.ts +0 -16
  132. package/lib/cjs/models/bedrock-chat-model.d.ts +0 -79
  133. package/lib/cjs/models/bedrock-chat-model.js +0 -303
  134. package/lib/cjs/models/claude-chat-model.d.ts +0 -114
  135. package/lib/cjs/models/claude-chat-model.js +0 -317
  136. package/lib/cjs/models/deepseek-chat-model.d.ts +0 -23
  137. package/lib/cjs/models/deepseek-chat-model.js +0 -35
  138. package/lib/cjs/models/gemini-chat-model.d.ts +0 -23
  139. package/lib/cjs/models/gemini-chat-model.js +0 -35
  140. package/lib/cjs/models/ollama-chat-model.d.ts +0 -22
  141. package/lib/cjs/models/ollama-chat-model.js +0 -34
  142. package/lib/cjs/models/open-router-chat-model.d.ts +0 -22
  143. package/lib/cjs/models/open-router-chat-model.js +0 -34
  144. package/lib/cjs/models/openai-chat-model.d.ts +0 -166
  145. package/lib/cjs/models/openai-chat-model.js +0 -415
  146. package/lib/cjs/models/xai-chat-model.d.ts +0 -21
  147. package/lib/cjs/models/xai-chat-model.js +0 -33
  148. package/lib/cjs/server/error.d.ts +0 -15
  149. package/lib/cjs/server/error.js +0 -22
  150. package/lib/cjs/server/index.d.ts +0 -2
  151. package/lib/cjs/server/index.js +0 -18
  152. package/lib/cjs/server/server.d.ts +0 -135
  153. package/lib/cjs/server/server.js +0 -187
  154. package/lib/dts/client/client.d.ts +0 -97
  155. package/lib/dts/client/index.d.ts +0 -1
  156. package/lib/dts/memory/default-memory.d.ts +0 -16
  157. package/lib/dts/models/bedrock-chat-model.d.ts +0 -79
  158. package/lib/dts/models/claude-chat-model.d.ts +0 -114
  159. package/lib/dts/models/deepseek-chat-model.d.ts +0 -23
  160. package/lib/dts/models/gemini-chat-model.d.ts +0 -23
  161. package/lib/dts/models/ollama-chat-model.d.ts +0 -22
  162. package/lib/dts/models/open-router-chat-model.d.ts +0 -22
  163. package/lib/dts/models/openai-chat-model.d.ts +0 -166
  164. package/lib/dts/models/xai-chat-model.d.ts +0 -21
  165. package/lib/dts/server/error.d.ts +0 -15
  166. package/lib/dts/server/index.d.ts +0 -2
  167. package/lib/dts/server/server.d.ts +0 -135
  168. package/lib/esm/client/client.d.ts +0 -97
  169. package/lib/esm/client/client.js +0 -83
  170. package/lib/esm/client/index.d.ts +0 -1
  171. package/lib/esm/client/index.js +0 -1
  172. package/lib/esm/memory/default-memory.d.ts +0 -16
  173. package/lib/esm/models/bedrock-chat-model.d.ts +0 -79
  174. package/lib/esm/models/bedrock-chat-model.js +0 -298
  175. package/lib/esm/models/claude-chat-model.d.ts +0 -114
  176. package/lib/esm/models/claude-chat-model.js +0 -310
  177. package/lib/esm/models/deepseek-chat-model.d.ts +0 -23
  178. package/lib/esm/models/deepseek-chat-model.js +0 -31
  179. package/lib/esm/models/gemini-chat-model.d.ts +0 -23
  180. package/lib/esm/models/gemini-chat-model.js +0 -31
  181. package/lib/esm/models/ollama-chat-model.d.ts +0 -22
  182. package/lib/esm/models/ollama-chat-model.js +0 -30
  183. package/lib/esm/models/open-router-chat-model.d.ts +0 -22
  184. package/lib/esm/models/open-router-chat-model.js +0 -30
  185. package/lib/esm/models/openai-chat-model.d.ts +0 -166
  186. package/lib/esm/models/openai-chat-model.js +0 -405
  187. package/lib/esm/models/xai-chat-model.d.ts +0 -21
  188. package/lib/esm/models/xai-chat-model.js +0 -29
  189. package/lib/esm/server/error.d.ts +0 -15
  190. package/lib/esm/server/error.js +0 -18
  191. package/lib/esm/server/index.d.ts +0 -2
  192. package/lib/esm/server/index.js +0 -2
  193. package/lib/esm/server/server.d.ts +0 -135
  194. package/lib/esm/server/server.js +0 -180
@@ -1,9 +1,10 @@
1
1
  import { inspect } from "node:util";
2
2
  import { ZodObject, type ZodType } from "zod";
3
- import type { Context } from "../aigne/context.js";
3
+ import type { Context, UserContext } from "../aigne/context.js";
4
4
  import type { MessagePayload } from "../aigne/message-queue.js";
5
5
  import type { MemoryAgent } from "../memory/memory.js";
6
- import { type Nullish, type PromiseOrValue } from "../utils/type-utils.js";
6
+ import { type Nullish, type PromiseOrValue, type XOr } from "../utils/type-utils.js";
7
+ import type { GuideRailAgent, GuideRailAgentOutput } from "./guide-rail-agent.js";
7
8
  import { type TransferAgentOutput } from "./types.js";
8
9
  export * from "./types.js";
9
10
  /**
@@ -29,7 +30,7 @@ export type PublishTopic<O extends Message> = string | string[] | ((output: O) =
29
30
  * @template I The agent input message type
30
31
  * @template O The agent output message type
31
32
  */
32
- export interface AgentOptions<I extends Message = Message, O extends Message = Message> {
33
+ export interface AgentOptions<I extends Message = Message, O extends Message = Message> extends Partial<Pick<Agent, "guideRails" | "hooks">> {
33
34
  /**
34
35
  * Topics the agent should subscribe to
35
36
  *
@@ -98,7 +99,21 @@ export interface AgentOptions<I extends Message = Message, O extends Message = M
98
99
  export declare const agentOptionsSchema: ZodObject<{
99
100
  [key in keyof AgentOptions]: ZodType<AgentOptions[key]>;
100
101
  }>;
101
- export interface AgentInvokeOptions {
102
+ export interface AgentInvokeOptions<U extends UserContext = UserContext> {
103
+ /**
104
+ * The execution context for the agent
105
+ *
106
+ * The context provides the runtime environment for agent execution, including:
107
+ * - Event emission and subscription management
108
+ * - Inter-agent communication and message passing
109
+ * - Resource usage tracking and limits enforcement
110
+ * - Timeout and status management
111
+ * - Memory and state management across agent invocations
112
+ *
113
+ * Each agent invocation requires a context to coordinate with the broader
114
+ * agent system and maintain proper isolation and resource control.
115
+ */
116
+ context: Context<U>;
102
117
  /**
103
118
  * Whether to enable streaming response
104
119
  *
@@ -139,6 +154,35 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
139
154
  * List of memories this agent can use
140
155
  */
141
156
  readonly memories: MemoryAgent[];
157
+ /**
158
+ * Lifecycle hooks for agent processing.
159
+ *
160
+ * Hooks enable tracing, logging, monitoring, and custom behavior
161
+ * without modifying the core agent implementation
162
+ *
163
+ * @example
164
+ * Here's an example of using hooks:
165
+ * {@includeCode ../../test/agents/agent.test.ts#example-agent-hooks}
166
+ */
167
+ readonly hooks: AgentHooks;
168
+ /**
169
+ * List of GuideRail agents applied to this agent
170
+ *
171
+ * GuideRail agents validate, transform, or control the message flow by:
172
+ * - Enforcing rules and safety policies
173
+ * - Validating inputs/outputs against specific criteria
174
+ * - Implementing business logic validations
175
+ * - Monitoring and auditing agent behavior
176
+ *
177
+ * Each GuideRail agent can examine both input and expected output,
178
+ * and has the ability to abort the process with an explanation
179
+ *
180
+ * @example
181
+ * Here's an example of using GuideRail agents:
182
+ *
183
+ * {@includeCode ../../test/agents/agent.test.ts#example-agent-guide-rails}
184
+ */
185
+ readonly guideRails?: GuideRailAgent[];
142
186
  /**
143
187
  * Name of the agent, used for identification and logging
144
188
  *
@@ -248,7 +292,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
248
292
  /**
249
293
  * Check context status to ensure it hasn't timed out
250
294
  *
251
- * @param context The context to check
295
+ * @param options Invocation options containing context
252
296
  * @throws Error if the context has timed out
253
297
  */
254
298
  private checkContextStatus;
@@ -260,7 +304,6 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
260
304
  * suitable for scenarios where a complete result is needed at once.
261
305
  *
262
306
  * @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
307
  * @param options Invocation options, must set streaming to false or leave unset
265
308
  * @returns Final JSON response
266
309
  *
@@ -268,7 +311,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
268
311
  * Here's an example of invoking an agent with regular mode:
269
312
  * {@includeCode ../../test/agents/agent.test.ts#example-invoke}
270
313
  */
271
- invoke(input: I | string, context?: Context, options?: AgentInvokeOptions & {
314
+ invoke(input: I | string, options?: Partial<AgentInvokeOptions> & {
272
315
  streaming?: false;
273
316
  }): Promise<O>;
274
317
  /**
@@ -279,7 +322,6 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
279
322
  * chat bot typing effects.
280
323
  *
281
324
  * @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
325
  * @param options Invocation options, must set streaming to true for this overload
284
326
  * @returns Streaming response object
285
327
  *
@@ -287,7 +329,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
287
329
  * Here's an example of invoking an agent with streaming response:
288
330
  * {@includeCode ../../test/agents/agent.test.ts#example-invoke-streaming}
289
331
  */
290
- invoke(input: I | string, context: Context | undefined, options: {
332
+ invoke(input: I | string, options: Partial<AgentInvokeOptions> & {
291
333
  streaming: true;
292
334
  }): Promise<AgentResponseStream<O>>;
293
335
  /**
@@ -296,11 +338,11 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
296
338
  * Returns either streaming or regular response based on the streaming parameter in options
297
339
  *
298
340
  * @param input Input message to the agent
299
- * @param context Execution context
300
341
  * @param options Invocation options
301
342
  * @returns Agent response (streaming or regular)
302
343
  */
303
- invoke(input: I | string, context?: Context, options?: AgentInvokeOptions): Promise<AgentResponse<O>>;
344
+ invoke(input: I | string, options?: Partial<AgentInvokeOptions>): Promise<AgentResponse<O>>;
345
+ protected invokeSkill<I extends Message, O extends Message>(skill: Agent<I, O>, input: I, options: AgentInvokeOptions): Promise<O>;
304
346
  /**
305
347
  * Process agent output
306
348
  *
@@ -308,7 +350,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
308
350
  *
309
351
  * @param input Original input message
310
352
  * @param output Raw output produced by the agent
311
- * @param context Execution context
353
+ * @param options Invocation options
312
354
  * @returns Final processed output
313
355
  */
314
356
  private processAgentOutput;
@@ -318,8 +360,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
318
360
  * Logs error information, triggers failure events, and re-throws the error
319
361
  *
320
362
  * @param error Caught error
321
- * @param context Execution context
322
- * @throws Always throws the received error
363
+ * @param options Invocation options
323
364
  */
324
365
  private processAgentError;
325
366
  /**
@@ -328,10 +369,10 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
328
369
  * If the context has a maximum invocation limit set, checks if the limit
329
370
  * has been exceeded and increments the invocation counter
330
371
  *
331
- * @param context Execution context
372
+ * @param options Invocation options containing context and limits
332
373
  * @throws Error if maximum invocation limit is exceeded
333
374
  */
334
- protected checkAgentInvokesUsage(context: Context): void;
375
+ protected checkAgentInvokesUsage(options: AgentInvokeOptions): void;
335
376
  /**
336
377
  * Pre-processing operations before handling input
337
378
  *
@@ -340,9 +381,27 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
340
381
  * - Verifying invocation limits
341
382
  *
342
383
  * @param _ Input message (unused)
343
- * @param context Execution context
384
+ * @param options Options for agent invocation
344
385
  */
345
- protected preprocess(_: I, context: Context): void;
386
+ protected preprocess(_: I, options: AgentInvokeOptions): Promise<void>;
387
+ private checkResponseByGuideRails;
388
+ private runGuideRails;
389
+ /**
390
+ * Handle errors detected by GuideRail agents
391
+ *
392
+ * This method is called when a GuideRail agent aborts the process, providing
393
+ * a way for agents to customize error handling behavior. By default, it simply
394
+ * returns the original error, but subclasses can override this method to:
395
+ * - Transform the error into a more specific response
396
+ * - Apply recovery strategies
397
+ * - Log or report the error in a custom format
398
+ * - Return a fallback output instead of an error
399
+ *
400
+ * @param error The GuideRail agent output containing abort=true and a reason
401
+ * @returns Either the original/modified error or a substitute output object
402
+ * which will be tagged with $status: "GuideRailError"
403
+ */
404
+ protected onGuideRailError(error: GuideRailAgentOutput): Promise<O | GuideRailAgentOutput>;
346
405
  /**
347
406
  * Post-processing operations after handling output
348
407
  *
@@ -352,10 +411,10 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
352
411
  *
353
412
  * @param input Input message
354
413
  * @param output Output message
355
- * @param context Execution context
414
+ * @param options Options for agent invocation
356
415
  */
357
- protected postprocess(input: I, output: O, context: Context): void;
358
- protected publishToTopics(output: Message, context: Context): Promise<void>;
416
+ protected postprocess(input: I, output: O, options: AgentInvokeOptions): Promise<void>;
417
+ protected publishToTopics(output: Message, options: AgentInvokeOptions): Promise<void>;
359
418
  /**
360
419
  * Core processing method of the agent, must be implemented in subclasses
361
420
  *
@@ -367,7 +426,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
367
426
  * - Another agent instance (transfer agent)
368
427
  *
369
428
  * @param input Input message
370
- * @param context Execution context
429
+ * @param options Options for agent invocation
371
430
  * @returns Processing result
372
431
  *
373
432
  * @example
@@ -386,7 +445,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
386
445
  * Example of transfer to another agent:
387
446
  * {@includeCode ../../test/agents/agent.test.ts#example-process-transfer}
388
447
  */
389
- abstract process(input: I, context: Context): PromiseOrValue<AgentProcessResult<O>>;
448
+ abstract process(input: I, options: AgentInvokeOptions): PromiseOrValue<AgentProcessResult<O>>;
390
449
  /**
391
450
  * Shut down the agent and clean up resources
392
451
  *
@@ -419,6 +478,86 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
419
478
  */
420
479
  [Symbol.asyncDispose](): Promise<void>;
421
480
  }
481
+ /**
482
+ * Lifecycle hooks for agent execution
483
+ *
484
+ * Hooks provide a way to intercept and extend agent behavior at key points during
485
+ * the agent's lifecycle, enabling custom functionality like logging, monitoring,
486
+ * tracing, error handling, and more.
487
+ */
488
+ export interface AgentHooks<I extends Message = Message, O extends Message = Message> {
489
+ /**
490
+ * Called when agent processing begins
491
+ *
492
+ * This hook runs before the agent processes input, allowing for
493
+ * setup operations, logging, or input transformations.
494
+ *
495
+ * @param event Object containing the input message
496
+ */
497
+ onStart?: (event: {
498
+ context: Context;
499
+ input: I;
500
+ }) => PromiseOrValue<void>;
501
+ /**
502
+ * Called when agent processing completes or fails
503
+ *
504
+ * This hook runs after processing finishes, receiving either the output
505
+ * or an error if processing failed. Useful for cleanup operations,
506
+ * logging results, or error handling.
507
+ *
508
+ * @param event Object containing the input message and either output or error
509
+ */
510
+ onEnd?: (event: XOr<{
511
+ context: Context;
512
+ input: I;
513
+ output: O;
514
+ error: Error;
515
+ }, "output", "error">) => PromiseOrValue<void>;
516
+ /**
517
+ * Called before a skill (sub-agent) is invoked
518
+ *
519
+ * This hook runs when the agent delegates work to a skill,
520
+ * allowing for tracking skill usage or transforming input to the skill.
521
+ *
522
+ * @param event Object containing the skill being used and input message
523
+ */
524
+ onSkillStart?: (event: {
525
+ context: Context;
526
+ skill: Agent;
527
+ input: I;
528
+ }) => PromiseOrValue<void>;
529
+ /**
530
+ * Called after a skill (sub-agent) completes or fails
531
+ *
532
+ * This hook runs when a skill finishes execution, receiving either the output
533
+ * or an error if the skill failed. Useful for monitoring skill performance
534
+ * or handling skill-specific errors.
535
+ *
536
+ * @param event Object containing the skill used, input message, and either output or error
537
+ */
538
+ onSkillEnd?: (event: XOr<{
539
+ context: Context;
540
+ skill: Agent;
541
+ input: I;
542
+ output: O;
543
+ error: Error;
544
+ }, "output", "error">) => PromiseOrValue<void>;
545
+ /**
546
+ * Called when an agent hands off processing to another agent
547
+ *
548
+ * This hook runs when a source agent transfers control to a target agent,
549
+ * allowing for tracking of handoffs between agents and monitoring the flow
550
+ * of processing in multi-agent systems.
551
+ *
552
+ * @param event Object containing the source agent, target agent, and input message
553
+ */
554
+ onHandoff?: (event: {
555
+ context: Context;
556
+ source: Agent;
557
+ target: Agent;
558
+ input: I;
559
+ }) => PromiseOrValue<void>;
560
+ }
422
561
  /**
423
562
  * Response type for an agent, can be:
424
563
  * - Direct response object
@@ -427,7 +566,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
427
566
  *
428
567
  * @template T Response data type
429
568
  */
430
- export type AgentResponse<T> = T | TransferAgentOutput | AgentResponseStream<T>;
569
+ export type AgentResponse<T> = T | AgentResponseStream<T> | TransferAgentOutput | GuideRailAgentOutput;
431
570
  /**
432
571
  * Streaming response type for an agent
433
572
  *
@@ -464,7 +603,7 @@ export interface AgentResponseDelta<T> {
464
603
  }> | Partial<{
465
604
  [key: string]: string;
466
605
  }>;
467
- json?: Partial<T | TransferAgentOutput>;
606
+ json?: Partial<T> | TransferAgentOutput;
468
607
  };
469
608
  }
470
609
  /**
@@ -496,7 +635,7 @@ export declare function jsonDelta<T extends Message>(jsonDelta: NonNullable<Agen
496
635
  *
497
636
  * @template O Agent output message type
498
637
  */
499
- export type AgentProcessAsyncGenerator<O extends Message> = AsyncGenerator<AgentResponseChunk<O>, Partial<O | TransferAgentOutput> | undefined | void>;
638
+ export type AgentProcessAsyncGenerator<O extends Message> = AsyncGenerator<AgentResponseChunk<O>, Partial<O> | TransferAgentOutput | undefined | void>;
500
639
  /**
501
640
  * Result type for agent processing method, can be:
502
641
  * - Direct or streaming response
@@ -586,10 +725,10 @@ export declare class FunctionAgent<I extends Message = Message, O extends Messag
586
725
  * Process input implementation, calls the configured processing function
587
726
  *
588
727
  * @param input Input message
589
- * @param context Execution context
728
+ * @param options Invocation options
590
729
  * @returns Processing result
591
730
  */
592
- process(input: I, context: Context): PromiseOrValue<AgentProcessResult<O>>;
731
+ process(input: I, options: AgentInvokeOptions): PromiseOrValue<AgentProcessResult<O>>;
593
732
  }
594
733
  /**
595
734
  * Function type for function agents
@@ -602,4 +741,4 @@ export declare class FunctionAgent<I extends Message = Message, O extends Messag
602
741
  * @param context Execution context
603
742
  * @returns Processing result, can be synchronous or asynchronous
604
743
  */
605
- export type FunctionAgentFn<I extends Message = any, O extends Message = any> = (input: I, context: Context) => PromiseOrValue<AgentProcessResult<O>>;
744
+ export type FunctionAgentFn<I extends Message = any, O extends Message = any> = (input: I, options: AgentInvokeOptions) => PromiseOrValue<AgentProcessResult<O>>;
@@ -1,10 +1,9 @@
1
1
  import { type ZodObject, type ZodType, z } from "zod";
2
- import type { Context } from "../aigne/context.js";
3
- import { type DefaultMemoryOptions } from "../memory/default-memory.js";
4
- import { ChatModel } from "../models/chat-model.js";
5
- import type { ChatModelInput } from "../models/chat-model.js";
2
+ import { type DefaultMemoryOptions } from "../memory/default-memory/index.js";
6
3
  import { PromptBuilder } from "../prompt/prompt-builder.js";
7
- import { Agent, type AgentOptions, type AgentProcessAsyncGenerator, type Message } from "./agent.js";
4
+ import { Agent, type AgentInvokeOptions, type AgentOptions, type AgentProcessAsyncGenerator, type Message } from "./agent.js";
5
+ import { ChatModel, type ChatModelInput } from "./chat-model.js";
6
+ import type { GuideRailAgentOutput } from "./guide-rail-agent.js";
8
7
  /**
9
8
  * Configuration options for an AI Agent
10
9
  *
@@ -215,7 +214,8 @@ export declare class AIAgent<I extends Message = Message, O extends Message = Me
215
214
  *
216
215
  * @protected
217
216
  */
218
- process(input: I, context: Context): AgentProcessAsyncGenerator<O>;
217
+ process(input: I, options: AgentInvokeOptions): AgentProcessAsyncGenerator<O>;
218
+ protected onGuideRailError(error: GuideRailAgentOutput): Promise<O | GuideRailAgentOutput>;
219
219
  /**
220
220
  * Process router mode requests
221
221
  *
@@ -224,5 +224,5 @@ export declare class AIAgent<I extends Message = Message, O extends Message = Me
224
224
  *
225
225
  * @protected
226
226
  */
227
- _processRouter(input: I, model: ChatModel, modelInput: ChatModelInput, context: Context, toolsMap: Map<string, Agent>): AgentProcessAsyncGenerator<O>;
227
+ _processRouter(input: I, model: ChatModel, modelInput: ChatModelInput, options: AgentInvokeOptions, toolsMap: Map<string, Agent>): AgentProcessAsyncGenerator<O>;
228
228
  }
@@ -1,6 +1,5 @@
1
- import { Agent, type AgentProcessResult, type Message } from "../agents/agent.js";
2
- import type { Context } from "../aigne/context.js";
3
1
  import type { PromiseOrValue } from "../utils/type-utils.js";
2
+ import { Agent, type AgentInvokeOptions, type AgentProcessResult, type Message } from "./agent.js";
4
3
  /**
5
4
  * ChatModel is an abstract base class for interacting with Large Language Models (LLMs).
6
5
  *
@@ -10,19 +9,19 @@ import type { PromiseOrValue } from "../utils/type-utils.js";
10
9
  *
11
10
  * @example
12
11
  * Here's how to implement a custom ChatModel:
13
- * {@includeCode ../../test/models/chat-model.test.ts#example-chat-model}
12
+ * {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model}
14
13
  *
15
14
  * @example
16
15
  * Here's an example showing streaming response with readable stream:
17
- * {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-streaming}
16
+ * {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model-streaming}
18
17
  *
19
18
  * @example
20
19
  * Here's an example showing streaming response with async generator:
21
- * {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-streaming-async-generator}
20
+ * {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model-streaming-async-generator}
22
21
  *
23
22
  * @example
24
23
  * Here's an example with tool calls:
25
- * {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-tools}
24
+ * {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model-tools}
26
25
  */
27
26
  export declare abstract class ChatModel extends Agent<ChatModelInput, ChatModelOutput> {
28
27
  constructor();
@@ -44,16 +43,27 @@ export declare abstract class ChatModel extends Agent<ChatModelInput, ChatModelO
44
43
  supportsParallelToolCalls: boolean;
45
44
  };
46
45
  private validateToolNames;
46
+ /**
47
+ * Normalizes tool names to ensure compatibility with language models
48
+ *
49
+ * This method converts tool names to a format that complies with model requirements
50
+ * by replacing hyphens and whitespace characters with underscores. The normalized
51
+ * names are used for tool calls while preserving the original names for reference.
52
+ *
53
+ * @param name - The original tool name to normalize
54
+ * @returns A promise that resolves to the normalized tool name
55
+ */
56
+ protected normalizeToolName(name: string): Promise<string>;
47
57
  /**
48
58
  * Performs preprocessing operations before handling input
49
59
  *
50
60
  * Primarily checks if token usage exceeds limits, throwing an exception if limits are exceeded
51
61
  *
52
62
  * @param input Input message
53
- * @param context Execution context
63
+ * @param options Options for invoking the agent
54
64
  * @throws Error if token usage exceeds maximum limit
55
65
  */
56
- protected preprocess(input: ChatModelInput, context: Context): void;
66
+ protected preprocess(input: ChatModelInput, options: AgentInvokeOptions): Promise<void>;
57
67
  /**
58
68
  * Performs postprocessing operations after handling output
59
69
  *
@@ -61,9 +71,9 @@ export declare abstract class ChatModel extends Agent<ChatModelInput, ChatModelO
61
71
  *
62
72
  * @param input Input message
63
73
  * @param output Output message
64
- * @param context Execution context
74
+ * @param options Options for invoking the agent
65
75
  */
66
- protected postprocess(input: ChatModelInput, output: ChatModelOutput, context: Context): void;
76
+ protected postprocess(input: ChatModelInput, output: ChatModelOutput, options: AgentInvokeOptions): Promise<void>;
67
77
  /**
68
78
  * Processes input messages and generates model responses
69
79
  *
@@ -81,10 +91,10 @@ export declare abstract class ChatModel extends Agent<ChatModelInput, ChatModelO
81
91
  * - Tool call processing if applicable
82
92
  *
83
93
  * @param input - The standardized input containing messages and model options
84
- * @param context - The execution context with settings and state
94
+ * @param options - The options for invoking the agent, including context and limits
85
95
  * @returns A promise or direct value containing the model's response
86
96
  */
87
- abstract process(input: ChatModelInput, context: Context): PromiseOrValue<AgentProcessResult<ChatModelOutput>>;
97
+ abstract process(input: ChatModelInput, options: AgentInvokeOptions): PromiseOrValue<AgentProcessResult<ChatModelOutput>>;
88
98
  }
89
99
  /**
90
100
  * Input message format for ChatModel
@@ -94,11 +104,11 @@ export declare abstract class ChatModel extends Agent<ChatModelInput, ChatModelO
94
104
  *
95
105
  * @example
96
106
  * Here's a basic ChatModel input example:
97
- * {@includeCode ../../test/models/chat-model.test.ts#example-chat-model}
107
+ * {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model}
98
108
  *
99
109
  * @example
100
110
  * Here's an example with tool calling:
101
- * {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-tools}
111
+ * {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model-tools}
102
112
  */
103
113
  export interface ChatModelInput extends Message {
104
114
  /**
@@ -213,7 +223,7 @@ export type ChatModelInputResponseFormat = {
213
223
  *
214
224
  * @example
215
225
  * Here's an example showing how to use tools:
216
- * {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-tools}
226
+ * {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model-tools}
217
227
  */
218
228
  export interface ChatModelInputTool {
219
229
  /**
@@ -249,7 +259,7 @@ export interface ChatModelInputTool {
249
259
  *
250
260
  * @example
251
261
  * Here's an example showing how to use tools:
252
- * {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-tools}
262
+ * {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model-tools}
253
263
  */
254
264
  export type ChatModelInputToolChoice = "auto" | "none" | "required" | {
255
265
  type: "function";
@@ -296,11 +306,11 @@ export interface ChatModelOptions {
296
306
  *
297
307
  * @example
298
308
  * Here's a basic output example:
299
- * {@includeCode ../../test/models/chat-model.test.ts#example-chat-model}
309
+ * {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model}
300
310
  *
301
311
  * @example
302
312
  * Here's an example with tool calls:
303
- * {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-tools}
313
+ * {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model-tools}
304
314
  */
305
315
  export interface ChatModelOutput extends Message {
306
316
  /**
@@ -331,7 +341,7 @@ export interface ChatModelOutput extends Message {
331
341
  *
332
342
  * @example
333
343
  * Here's an example with tool calls:
334
- * {@includeCode ../../test/models/chat-model.test.ts#example-chat-model-tools}
344
+ * {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model-tools}
335
345
  */
336
346
  export interface ChatModelOutputToolCall {
337
347
  /**
@@ -0,0 +1,62 @@
1
+ import type { Agent, AgentOptions, Message } from "./agent.js";
2
+ /**
3
+ * Input interface for GuideRail agents
4
+ *
5
+ * GuideRail agents receive both input and expected output, allowing them to
6
+ * validate, transform, or control the flow of messages between other agents.
7
+ */
8
+ export interface GuideRailAgentInput extends Message {
9
+ /**
10
+ * The input data to be processed
11
+ *
12
+ * This is the original message that would be sent to the target agent
13
+ */
14
+ input?: unknown;
15
+ /**
16
+ * The expected output data
17
+ *
18
+ * This is what the target agent is expected to produce, allowing
19
+ * the GuideRail agent to validate or transform the data flow
20
+ */
21
+ output?: unknown;
22
+ }
23
+ /**
24
+ * Output interface for GuideRail agents
25
+ *
26
+ * GuideRail agents can either allow the process to continue or abort it with a reason.
27
+ * This provides a mechanism for enforcing rules, validating data, and controlling
28
+ * the execution flow of the agent system.
29
+ */
30
+ export interface GuideRailAgentOutput extends Message {
31
+ /**
32
+ * Whether to abort the current process
33
+ *
34
+ * When true, the agent system should stop the current execution flow
35
+ * and prevent further processing based on this input/output pair
36
+ *
37
+ * @default false
38
+ */
39
+ abort?: boolean;
40
+ /**
41
+ * Reason for aborting the process
42
+ *
43
+ * When abort is true, this provides a human-readable explanation
44
+ * for why the process was stopped
45
+ */
46
+ reason?: string;
47
+ }
48
+ /**
49
+ * GuideRail agent type definition
50
+ *
51
+ * GuideRail agents act as validators, transformers, or controllers for the message
52
+ * flow between agents. They can enforce rules, perform safety checks, ensure data
53
+ * quality, or implement business logic validations.
54
+ *
55
+ * Use GuideRail agents when you need to:
56
+ * - Validate inputs or outputs against specific criteria
57
+ * - Enforce security or safety policies
58
+ * - Implement business rules that control agent interactions
59
+ * - Monitor and audit agent behavior
60
+ */
61
+ export type GuideRailAgent = Agent<GuideRailAgentInput, GuideRailAgentOutput>;
62
+ export declare const guideRailAgentOptions: AgentOptions<GuideRailAgentInput, GuideRailAgentOutput>;
@@ -6,9 +6,8 @@ import type { RequestOptions } from "@modelcontextprotocol/sdk/shared/protocol.j
6
6
  import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
7
7
  import type { CallToolResult, GetPromptResult, Implementation, ReadResourceResult, Request } from "@modelcontextprotocol/sdk/types.js";
8
8
  import { type ZodType, z } from "zod";
9
- import type { Context } from "../aigne/context.js";
10
9
  import { type PromiseOrValue } from "../utils/type-utils.js";
11
- import { Agent, type AgentOptions, type Message } from "./agent.js";
10
+ import { Agent, type AgentInvokeOptions, type AgentOptions, type Message } from "./agent.js";
12
11
  export interface MCPAgentOptions extends AgentOptions {
13
12
  client: Client;
14
13
  prompts?: MCPPrompt[];
@@ -150,10 +149,10 @@ export declare class MCPAgent extends Agent {
150
149
  * throws an error if called.
151
150
  *
152
151
  * @param _input Input message (unused)
153
- * @param _context Execution context (unused)
152
+ * @param _options AgentInvokeOptions (unused)
154
153
  * @throws Error This method always throws an error since MCPAgent is not directly invokable
155
154
  */
156
- process(_input: Message, _context?: Context): Promise<Message>;
155
+ process(_input: Message, _options: AgentInvokeOptions): Promise<Message>;
157
156
  /**
158
157
  * Shut down the agent and close the MCP connection.
159
158
  *
@@ -1,6 +1,5 @@
1
- import type { Context } from "../aigne/context.js";
2
1
  import { type PromiseOrValue } from "../utils/type-utils.js";
3
- import { Agent, type AgentOptions, type AgentProcessResult, type Message } from "./agent.js";
2
+ import { Agent, type AgentInvokeOptions, type AgentOptions, type AgentProcessResult, type Message } from "./agent.js";
4
3
  /**
5
4
  * Defines the processing modes available for a TeamAgent.
6
5
  *
@@ -89,10 +88,10 @@ export declare class TeamAgent<I extends Message, O extends Message> extends Age
89
88
  * - In parallel mode: Process input through all agents simultaneously and combine their outputs
90
89
  *
91
90
  * @param input The message to process
92
- * @param context The execution context
91
+ * @param options The invocation options
93
92
  * @returns A stream of message chunks that collectively form the response
94
93
  */
95
- process(input: I, context: Context): PromiseOrValue<AgentProcessResult<O>>;
94
+ process(input: I, options: AgentInvokeOptions): PromiseOrValue<AgentProcessResult<O>>;
96
95
  /**
97
96
  * Process input sequentially through each agent in the team.
98
97
  *
@@ -103,12 +102,12 @@ export declare class TeamAgent<I extends Message, O extends Message> extends Age
103
102
  * 4. Updates the team's agent list with any changes that occurred during processing
104
103
  *
105
104
  * @param input The message to process
106
- * @param context The execution context
105
+ * @param options The invocation options
107
106
  * @returns A stream of message chunks from all agents
108
107
  *
109
108
  * @private
110
109
  */
111
- _processSequential(input: I, context: Context): PromiseOrValue<AgentProcessResult<O>>;
110
+ _processSequential(input: I, options: AgentInvokeOptions): PromiseOrValue<AgentProcessResult<O>>;
112
111
  /**
113
112
  * Process input in parallel through all agents in the team.
114
113
  *
@@ -118,10 +117,10 @@ export declare class TeamAgent<I extends Message, O extends Message> extends Age
118
117
  * 3. Updates the team's agent list with any changes that occurred during processing
119
118
  *
120
119
  * @param input The message to process
121
- * @param context The execution context
120
+ * @param options The invocation options
122
121
  * @returns A stream of combined message chunks from all agents
123
122
  *
124
123
  * @private
125
124
  */
126
- _processParallel(input: I, context: Context): PromiseOrValue<AgentProcessResult<O>>;
125
+ _processParallel(input: I, options: AgentInvokeOptions): PromiseOrValue<AgentProcessResult<O>>;
127
126
  }