@aigne/core 1.33.2 → 1.36.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 (43) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/lib/cjs/agents/agent.d.ts +71 -10
  3. package/lib/cjs/agents/agent.js +73 -28
  4. package/lib/cjs/agents/ai-agent.js +6 -3
  5. package/lib/cjs/agents/team-agent.js +3 -13
  6. package/lib/cjs/aigne/context.d.ts +1 -0
  7. package/lib/cjs/aigne/context.js +25 -10
  8. package/lib/cjs/loader/agent-js.d.ts +2 -11
  9. package/lib/cjs/loader/agent-js.js +4 -8
  10. package/lib/cjs/loader/agent-yaml.d.ts +18 -2
  11. package/lib/cjs/loader/agent-yaml.js +32 -13
  12. package/lib/cjs/loader/index.d.ts +2 -2
  13. package/lib/cjs/loader/index.js +48 -15
  14. package/lib/cjs/loader/schema.d.ts +10 -0
  15. package/lib/cjs/loader/schema.js +17 -1
  16. package/lib/cjs/package.json +3 -1
  17. package/lib/cjs/utils/type-utils.d.ts +1 -1
  18. package/lib/cjs/utils/type-utils.js +2 -4
  19. package/lib/dts/agents/agent.d.ts +71 -10
  20. package/lib/dts/aigne/context.d.ts +1 -0
  21. package/lib/dts/loader/agent-js.d.ts +2 -11
  22. package/lib/dts/loader/agent-yaml.d.ts +18 -2
  23. package/lib/dts/loader/index.d.ts +2 -2
  24. package/lib/dts/loader/schema.d.ts +10 -0
  25. package/lib/dts/utils/type-utils.d.ts +1 -1
  26. package/lib/esm/agents/agent.d.ts +71 -10
  27. package/lib/esm/agents/agent.js +73 -28
  28. package/lib/esm/agents/ai-agent.js +6 -3
  29. package/lib/esm/agents/team-agent.js +3 -13
  30. package/lib/esm/aigne/context.d.ts +1 -0
  31. package/lib/esm/aigne/context.js +25 -10
  32. package/lib/esm/loader/agent-js.d.ts +2 -11
  33. package/lib/esm/loader/agent-js.js +5 -6
  34. package/lib/esm/loader/agent-yaml.d.ts +18 -2
  35. package/lib/esm/loader/agent-yaml.js +33 -11
  36. package/lib/esm/loader/index.d.ts +2 -2
  37. package/lib/esm/loader/index.js +49 -16
  38. package/lib/esm/loader/schema.d.ts +10 -0
  39. package/lib/esm/loader/schema.js +12 -0
  40. package/lib/esm/package.json +3 -1
  41. package/lib/esm/utils/type-utils.d.ts +1 -1
  42. package/lib/esm/utils/type-utils.js +2 -4
  43. package/package.json +4 -4
@@ -1,12 +1,8 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.loadAgentFromYamlFile = loadAgentFromYamlFile;
7
4
  const json_schema_to_zod_1 = require("@aigne/json-schema-to-zod");
8
5
  const index_js_1 = require("@aigne/platform-helpers/nodejs/index.js");
9
- const camelize_ts_1 = __importDefault(require("camelize-ts"));
10
6
  const yaml_1 = require("yaml");
11
7
  const zod_1 = require("zod");
12
8
  const ai_agent_js_1 = require("../agents/ai-agent.js");
@@ -15,21 +11,41 @@ const type_utils_js_1 = require("../utils/type-utils.js");
15
11
  const schema_js_1 = require("./schema.js");
16
12
  async function loadAgentFromYamlFile(path) {
17
13
  const agentSchema = zod_1.z.lazy(() => {
14
+ const nestAgentSchema = zod_1.z.lazy(() => zod_1.z.union([
15
+ agentSchema,
16
+ zod_1.z.string(),
17
+ (0, schema_js_1.camelizeSchema)(zod_1.z.object({
18
+ url: zod_1.z.string(),
19
+ defaultInput: (0, schema_js_1.optionalize)(schema_js_1.defaultInputSchema),
20
+ hooks: (0, schema_js_1.optionalize)(zod_1.z.union([hooksSchema, zod_1.z.array(hooksSchema)])),
21
+ })),
22
+ ]));
23
+ const hooksSchema = (0, schema_js_1.camelizeSchema)(zod_1.z.object({
24
+ onStart: (0, schema_js_1.optionalize)(nestAgentSchema),
25
+ onSuccess: (0, schema_js_1.optionalize)(nestAgentSchema),
26
+ onError: (0, schema_js_1.optionalize)(nestAgentSchema),
27
+ onEnd: (0, schema_js_1.optionalize)(nestAgentSchema),
28
+ onSkillStart: (0, schema_js_1.optionalize)(nestAgentSchema),
29
+ onSkillEnd: (0, schema_js_1.optionalize)(nestAgentSchema),
30
+ onHandoff: (0, schema_js_1.optionalize)(nestAgentSchema),
31
+ }));
18
32
  const baseAgentSchema = zod_1.z.object({
19
33
  name: (0, schema_js_1.optionalize)(zod_1.z.string()),
20
34
  description: (0, schema_js_1.optionalize)(zod_1.z.string()),
21
35
  inputSchema: (0, schema_js_1.optionalize)((0, schema_js_1.inputOutputSchema)({ path })).transform((v) => v ? (0, json_schema_to_zod_1.jsonSchemaToZod)(v) : undefined),
36
+ defaultInput: (0, schema_js_1.optionalize)(schema_js_1.defaultInputSchema),
22
37
  outputSchema: (0, schema_js_1.optionalize)((0, schema_js_1.inputOutputSchema)({ path })).transform((v) => v ? (0, json_schema_to_zod_1.jsonSchemaToZod)(v) : undefined),
23
- skills: (0, schema_js_1.optionalize)(zod_1.z.array(zod_1.z.union([zod_1.z.string(), agentSchema]))),
38
+ hooks: (0, schema_js_1.optionalize)(zod_1.z.union([hooksSchema, zod_1.z.array(hooksSchema)])),
39
+ skills: (0, schema_js_1.optionalize)(zod_1.z.array(nestAgentSchema)),
24
40
  memory: (0, schema_js_1.optionalize)(zod_1.z.union([
25
41
  zod_1.z.boolean(),
26
- zod_1.z.object({
42
+ (0, schema_js_1.camelizeSchema)(zod_1.z.object({
27
43
  provider: zod_1.z.string(),
28
44
  subscribeTopic: (0, schema_js_1.optionalize)(zod_1.z.array(zod_1.z.string())),
29
- }),
45
+ })),
30
46
  ])),
31
47
  });
32
- return zod_1.z.discriminatedUnion("type", [
48
+ return (0, schema_js_1.camelizeSchema)(zod_1.z.discriminatedUnion("type", [
33
49
  zod_1.z
34
50
  .object({
35
51
  type: zod_1.z.literal("ai"),
@@ -40,18 +56,21 @@ async function loadAgentFromYamlFile(path) {
40
56
  }),
41
57
  ])).transform((v) => typeof v === "string"
42
58
  ? v
43
- : v && index_js_1.nodejs.fs.readFile(index_js_1.nodejs.path.join(index_js_1.nodejs.path.dirname(path), v.url), "utf8")),
59
+ : v &&
60
+ index_js_1.nodejs.fs.readFile(index_js_1.nodejs.path.join(index_js_1.nodejs.path.dirname(path), v.url), "utf8")),
44
61
  inputKey: (0, schema_js_1.optionalize)(zod_1.z.string()),
45
62
  outputKey: (0, schema_js_1.optionalize)(zod_1.z.string()),
46
63
  toolChoice: (0, schema_js_1.optionalize)(zod_1.z.nativeEnum(ai_agent_js_1.AIAgentToolChoice)),
47
64
  })
48
65
  .extend(baseAgentSchema.shape),
49
- zod_1.z.object({
66
+ zod_1.z
67
+ .object({
50
68
  type: zod_1.z.literal("mcp"),
51
69
  url: (0, schema_js_1.optionalize)(zod_1.z.string()),
52
70
  command: (0, schema_js_1.optionalize)(zod_1.z.string()),
53
71
  args: (0, schema_js_1.optionalize)(zod_1.z.array(zod_1.z.string())),
54
- }),
72
+ })
73
+ .extend(baseAgentSchema.shape),
55
74
  zod_1.z
56
75
  .object({
57
76
  type: zod_1.z.literal("team"),
@@ -65,10 +84,10 @@ async function loadAgentFromYamlFile(path) {
65
84
  jsonata: zod_1.z.string(),
66
85
  })
67
86
  .extend(baseAgentSchema.shape),
68
- ]);
87
+ ]));
69
88
  });
70
89
  const raw = await (0, type_utils_js_1.tryOrThrow)(() => index_js_1.nodejs.fs.readFile(path, "utf8"), (error) => new Error(`Failed to load agent definition from ${path}: ${error.message}`));
71
- const json = (0, type_utils_js_1.tryOrThrow)(() => (0, camelize_ts_1.default)((0, yaml_1.parse)(raw)), (error) => new Error(`Failed to parse agent definition from ${path}: ${error.message}`));
90
+ const json = (0, type_utils_js_1.tryOrThrow)(() => (0, yaml_1.parse)(raw), (error) => new Error(`Failed to parse agent definition from ${path}: ${error.message}`));
72
91
  const agent = await (0, type_utils_js_1.tryOrThrow)(async () => await agentSchema.parseAsync({
73
92
  ...json,
74
93
  type: json.type ?? "ai",
@@ -1,6 +1,6 @@
1
1
  import type { Camelize } from "camelize-ts";
2
2
  import { z } from "zod";
3
- import { Agent } from "../agents/agent.js";
3
+ import { Agent, type AgentOptions } from "../agents/agent.js";
4
4
  import type { ChatModel, ChatModelOptions } from "../agents/chat-model.js";
5
5
  import type { AIGNEOptions } from "../aigne/aigne.js";
6
6
  import type { MemoryAgent, MemoryAgentOptions } from "../memory/memory.js";
@@ -25,7 +25,7 @@ export interface LoadOptions {
25
25
  path: string;
26
26
  }
27
27
  export declare function load(options: LoadOptions): Promise<AIGNEOptions>;
28
- export declare function loadAgent(path: string, options?: LoadOptions): Promise<Agent>;
28
+ export declare function loadAgent(path: string, options?: LoadOptions, agentOptions?: AgentOptions): Promise<Agent>;
29
29
  export declare function loadModel(models: LoadableModel[], model?: Camelize<z.infer<typeof aigneFileSchema>["model"]>, modelOptions?: ChatModelOptions): Promise<ChatModel | undefined>;
30
30
  declare const aigneFileSchema: z.ZodObject<{
31
31
  name: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -39,7 +39,7 @@ async function load(options) {
39
39
  skills,
40
40
  };
41
41
  }
42
- async function loadAgent(path, options) {
42
+ async function loadAgent(path, options, agentOptions) {
43
43
  if ([".js", ".mjs", ".ts", ".mts"].includes(index_js_1.nodejs.path.extname(path))) {
44
44
  const agent = await (0, agent_js_js_1.loadAgentFromJsFile)(path);
45
45
  if (agent instanceof agent_js_1.Agent)
@@ -48,38 +48,74 @@ async function loadAgent(path, options) {
48
48
  }
49
49
  if ([".yml", ".yaml"].includes(index_js_1.nodejs.path.extname(path))) {
50
50
  const agent = await (0, agent_yaml_js_1.loadAgentFromYamlFile)(path);
51
- return parseAgent(path, agent, options);
51
+ return parseAgent(path, agent, options, agentOptions);
52
52
  }
53
53
  throw new Error(`Unsupported agent file type: ${path}`);
54
54
  }
55
- async function parseAgent(path, agent, options) {
55
+ async function loadNestAgent(path, agent, options) {
56
+ return typeof agent === "object" && "type" in agent
57
+ ? parseAgent(path, agent, options)
58
+ : typeof agent === "string"
59
+ ? loadAgent(index_js_1.nodejs.path.join(index_js_1.nodejs.path.dirname(path), agent), options)
60
+ : loadAgent(index_js_1.nodejs.path.join(index_js_1.nodejs.path.dirname(path), agent.url), options, {
61
+ defaultInput: agent.defaultInput,
62
+ hooks: await parseHooks(path, agent.hooks, options),
63
+ });
64
+ }
65
+ async function parseHooks(path, hooks, options) {
66
+ hooks = [hooks].flat().filter(type_utils_js_1.isNonNullable);
67
+ if (!hooks.length)
68
+ return undefined;
69
+ return await Promise.all(hooks.map(async (hook) => ({
70
+ onStart: hook.onStart ? await loadNestAgent(path, hook.onStart, options) : undefined,
71
+ onSuccess: hook.onSuccess ? await loadNestAgent(path, hook.onSuccess, options) : undefined,
72
+ onError: hook.onError ? await loadNestAgent(path, hook.onError, options) : undefined,
73
+ onEnd: hook.onEnd ? await loadNestAgent(path, hook.onEnd, options) : undefined,
74
+ onSkillStart: hook.onSkillStart
75
+ ? await loadNestAgent(path, hook.onSkillStart, options)
76
+ : undefined,
77
+ onSkillEnd: hook.onSkillEnd
78
+ ? await loadNestAgent(path, hook.onSkillEnd, options)
79
+ : undefined,
80
+ onHandoff: hook.onHandoff ? await loadNestAgent(path, hook.onHandoff, options) : undefined,
81
+ })));
82
+ }
83
+ async function parseAgent(path, agent, options, agentOptions) {
56
84
  const skills = "skills" in agent
57
85
  ? agent.skills &&
58
- (await Promise.all(agent.skills.map((skill) => typeof skill === "string"
59
- ? loadAgent(index_js_1.nodejs.path.join(index_js_1.nodejs.path.dirname(path), skill), options)
60
- : parseAgent(path, skill, options))))
86
+ (await Promise.all(agent.skills.map((skill) => loadNestAgent(path, skill, options))))
61
87
  : undefined;
62
88
  const memory = "memory" in agent && options?.memories?.length
63
89
  ? await loadMemory(options.memories, typeof agent.memory === "object" ? agent.memory.provider : undefined, typeof agent.memory === "object" ? agent.memory : {})
64
90
  : undefined;
91
+ const baseOptions = {
92
+ ...agentOptions,
93
+ ...agent,
94
+ skills,
95
+ memory,
96
+ hooks: [
97
+ ...((await parseHooks(path, agent.hooks, options)) ?? []),
98
+ ...[agentOptions?.hooks].flat().filter(type_utils_js_1.isNonNullable),
99
+ ],
100
+ };
65
101
  switch (agent.type) {
66
102
  case "ai": {
67
103
  return ai_agent_js_1.AIAgent.from({
68
- ...agent,
104
+ ...baseOptions,
69
105
  instructions: agent.instructions &&
70
106
  prompt_builder_js_1.PromptBuilder.from(agent.instructions, { workingDir: index_js_1.nodejs.path.dirname(path) }),
71
- memory,
72
- skills,
73
107
  });
74
108
  }
75
109
  case "mcp": {
76
110
  if (agent.url) {
77
111
  return mcp_agent_js_1.MCPAgent.from({
112
+ ...baseOptions,
78
113
  url: agent.url,
79
114
  });
80
115
  }
81
116
  if (agent.command) {
82
117
  return mcp_agent_js_1.MCPAgent.from({
118
+ ...baseOptions,
83
119
  command: agent.command,
84
120
  args: agent.args,
85
121
  });
@@ -88,16 +124,13 @@ async function parseAgent(path, agent, options) {
88
124
  }
89
125
  case "team": {
90
126
  return team_agent_js_1.TeamAgent.from({
91
- ...agent,
92
- memory,
93
- skills,
127
+ ...baseOptions,
94
128
  });
95
129
  }
96
130
  case "transform": {
97
131
  return transform_agent_js_1.TransformAgent.from({
98
- ...agent,
99
- memory,
100
- skills,
132
+ ...baseOptions,
133
+ jsonata: agent.jsonata,
101
134
  });
102
135
  }
103
136
  }
@@ -22,4 +22,14 @@ export declare const inputOutputSchema: ({ path }: {
22
22
  required?: string[] | undefined;
23
23
  additionalProperties?: boolean | undefined;
24
24
  }>]>;
25
+ export declare const defaultInputSchema: z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodObject<{
26
+ $get: z.ZodString;
27
+ }, "strip", z.ZodTypeAny, {
28
+ $get: string;
29
+ }, {
30
+ $get: string;
31
+ }>, z.ZodUnknown]>>;
25
32
  export declare function optionalize<T>(schema: ZodType<T>): ZodType<T | undefined>;
33
+ export declare function camelizeSchema<T>(schema: ZodType<T>, { shallow }?: {
34
+ shallow?: boolean;
35
+ }): ZodType<T>;
@@ -1,10 +1,17 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.inputOutputSchema = void 0;
6
+ exports.defaultInputSchema = exports.inputOutputSchema = void 0;
4
7
  exports.optionalize = optionalize;
8
+ exports.camelizeSchema = camelizeSchema;
5
9
  const index_js_1 = require("@aigne/platform-helpers/nodejs/index.js");
10
+ const camelize_ts_1 = __importDefault(require("camelize-ts"));
6
11
  const yaml_1 = require("yaml");
7
12
  const zod_1 = require("zod");
13
+ const agent_js_1 = require("../agents/agent.js");
14
+ const type_utils_js_1 = require("../utils/type-utils.js");
8
15
  const inputOutputSchema = ({ path }) => {
9
16
  const includeExternalSchema = async (schema) => {
10
17
  if (schema?.type === "object" && schema.properties) {
@@ -50,6 +57,15 @@ const inputOutputSchema = ({ path }) => {
50
57
  ]);
51
58
  };
52
59
  exports.inputOutputSchema = inputOutputSchema;
60
+ exports.defaultInputSchema = zod_1.z.record(zod_1.z.string(), zod_1.z.union([
61
+ zod_1.z.object({
62
+ [agent_js_1.DEFAULT_INPUT_ACTION_GET]: zod_1.z.string(),
63
+ }),
64
+ zod_1.z.unknown(),
65
+ ]));
53
66
  function optionalize(schema) {
54
67
  return schema.nullish().transform((v) => v ?? undefined);
55
68
  }
69
+ function camelizeSchema(schema, { shallow = true } = {}) {
70
+ return zod_1.z.preprocess((v) => ((0, type_utils_js_1.isRecord)(v) ? (0, camelize_ts_1.default)(v, shallow) : v), schema);
71
+ }
@@ -1 +1,3 @@
1
- {"type": "commonjs"}
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -19,7 +19,7 @@ export declare function pick<T extends object, K extends keyof T>(obj: T, ...key
19
19
  export declare function omit<T extends object, K extends keyof T>(obj: T, ...keys: (K | K[])[]): Omit<T, K>;
20
20
  export declare function omitDeep<T, K>(obj: T, ...keys: (K | K[])[]): unknown;
21
21
  export declare function omitBy<T extends Record<string, unknown>, K extends keyof T>(obj: T, predicate: (value: T[K], key: K) => boolean): Partial<T>;
22
- export declare function flat<T>(value?: T | T[]): T[];
22
+ export declare function flat<T>(...value: (T | T[])[]): NonNullable<T>[];
23
23
  export declare function createAccessorArray<T>(array: T[], accessor: (array: T[], name: string) => T | undefined): T[] & {
24
24
  [key: string]: T;
25
25
  };
@@ -102,10 +102,8 @@ function omitBy(obj, predicate) {
102
102
  return !predicate(value, k);
103
103
  }));
104
104
  }
105
- function flat(value) {
106
- if (isNil(value))
107
- return [];
108
- return Array.isArray(value) ? value : [value];
105
+ function flat(...value) {
106
+ return value.flat().filter(isNonNullable);
109
107
  }
110
108
  function createAccessorArray(array, accessor) {
111
109
  return new Proxy(array, {
@@ -10,6 +10,7 @@ import { type Nullish, type PromiseOrValue, type XOr } from "../utils/type-utils
10
10
  import type { GuideRailAgent, GuideRailAgentOutput } from "./guide-rail-agent.js";
11
11
  import { type TransferAgentOutput } from "./types.js";
12
12
  export * from "./types.js";
13
+ export declare const DEFAULT_INPUT_ACTION_GET = "$get";
13
14
  /**
14
15
  * Basic message type that can contain any key-value pairs
15
16
  */
@@ -72,6 +73,11 @@ export interface AgentOptions<I extends Message = Message, O extends Message = M
72
73
  * Used to validate that input messages conform to the expected format
73
74
  */
74
75
  inputSchema?: AgentInputOutputSchema<I>;
76
+ /**
77
+ * Default input message for the agent, it can be used to provide partial input
78
+ * or default values for the agent's input schema.
79
+ */
80
+ defaultInput?: Agent<I, O>["defaultInput"];
75
81
  /**
76
82
  * Zod schema defining the output message structure
77
83
  *
@@ -106,7 +112,7 @@ export interface AgentOptions<I extends Message = Message, O extends Message = M
106
112
  * Maximum number of memory items to retrieve
107
113
  */
108
114
  maxRetrieveMemoryCount?: number;
109
- hooks?: AgentHooks<I, O>;
115
+ hooks?: AgentHooks<I, O> | AgentHooks<I, O>[];
110
116
  }
111
117
  export declare const agentOptionsSchema: ZodObject<{
112
118
  [key in keyof AgentOptions]: ZodType<AgentOptions[key]>;
@@ -137,6 +143,10 @@ export interface AgentInvokeOptions<U extends UserContext = UserContext> {
137
143
  * and returns the final JSON result
138
144
  */
139
145
  streaming?: boolean;
146
+ /**
147
+ * Optional hooks for agent invocation
148
+ */
149
+ hooks?: AgentHooks<any, any>;
140
150
  }
141
151
  /**
142
152
  * Agent is the base class for all agents.
@@ -190,7 +200,7 @@ export declare abstract class Agent<I extends Message = any, O extends Message =
190
200
  * Here's an example of using hooks:
191
201
  * {@includeCode ../../test/agents/agent.test.ts#example-agent-hooks}
192
202
  */
193
- readonly hooks: AgentHooks<I, O>;
203
+ readonly hooks: AgentHooks<I, O>[];
194
204
  /**
195
205
  * List of GuideRail agents applied to this agent
196
206
  *
@@ -232,6 +242,11 @@ export declare abstract class Agent<I extends Message = any, O extends Message =
232
242
  */
233
243
  readonly description?: string;
234
244
  private readonly _inputSchema?;
245
+ defaultInput?: Partial<{
246
+ [key in keyof I]: {
247
+ [DEFAULT_INPUT_ACTION_GET]: string;
248
+ } | I[key];
249
+ }>;
235
250
  private readonly _outputSchema?;
236
251
  /**
237
252
  * Get the input data schema for this agent
@@ -372,6 +387,8 @@ export declare abstract class Agent<I extends Message = any, O extends Message =
372
387
  * @returns Agent response (streaming or regular)
373
388
  */
374
389
  invoke(input: I & Message, options?: Partial<AgentInvokeOptions>): Promise<AgentResponse<O>>;
390
+ private callHooks;
391
+ private mergeDefaultInput;
375
392
  protected invokeSkill<I extends Message, O extends Message>(skill: Agent<I, O>, input: I & Message, options: AgentInvokeOptions): Promise<O>;
376
393
  /**
377
394
  * Process agent output
@@ -517,11 +534,15 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
517
534
  *
518
535
  * @param event Object containing the input message
519
536
  */
520
- onStart?: (event: {
537
+ onStart?: ((event: {
521
538
  context: Context;
522
539
  input: I;
523
540
  }) => PromiseOrValue<void | {
524
541
  input?: I;
542
+ }>) | Agent<{
543
+ input: I;
544
+ }, {
545
+ input?: I;
525
546
  }>;
526
547
  /**
527
548
  * Called when agent processing completes or fails
@@ -532,13 +553,39 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
532
553
  *
533
554
  * @param event Object containing the input message and either output or error
534
555
  */
535
- onEnd?: (event: XOr<{
556
+ onEnd?: ((event: XOr<{
536
557
  context: Context;
537
558
  input: I;
538
559
  output: O;
539
560
  error: Error;
540
561
  }, "output", "error">) => PromiseOrValue<void | {
541
562
  output?: O;
563
+ }>) | Agent<XOr<{
564
+ input: I;
565
+ output: O;
566
+ error: Error;
567
+ }, "output", "error">, {
568
+ output?: O;
569
+ }>;
570
+ onSuccess?: ((event: {
571
+ context: Context;
572
+ input: I;
573
+ output: O;
574
+ }) => PromiseOrValue<void | {
575
+ output?: O;
576
+ }>) | Agent<{
577
+ input: I;
578
+ output: O;
579
+ }, {
580
+ output?: O;
581
+ }>;
582
+ onError?: ((event: {
583
+ context: Context;
584
+ input: I;
585
+ error: Error;
586
+ }) => void) | Agent<{
587
+ input: I;
588
+ error: Error;
542
589
  }>;
543
590
  /**
544
591
  * Called before a skill (sub-agent) is invoked
@@ -548,11 +595,14 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
548
595
  *
549
596
  * @param event Object containing the skill being used and input message
550
597
  */
551
- onSkillStart?: (event: {
598
+ onSkillStart?: ((event: {
552
599
  context: Context;
553
600
  skill: Agent;
554
601
  input: Message;
555
- }) => PromiseOrValue<void>;
602
+ }) => PromiseOrValue<void>) | Agent<{
603
+ skill: Agent;
604
+ input: Message;
605
+ }>;
556
606
  /**
557
607
  * Called after a skill (sub-agent) completes or fails
558
608
  *
@@ -562,13 +612,18 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
562
612
  *
563
613
  * @param event Object containing the skill used, input message, and either output or error
564
614
  */
565
- onSkillEnd?: (event: XOr<{
615
+ onSkillEnd?: ((event: XOr<{
566
616
  context: Context;
567
617
  skill: Agent;
568
618
  input: Message;
569
619
  output: Message;
570
620
  error: Error;
571
- }, "output", "error">) => PromiseOrValue<void>;
621
+ }, "output", "error">) => PromiseOrValue<void>) | Agent<XOr<{
622
+ skill: Agent;
623
+ input: Message;
624
+ output: Message;
625
+ error: Error;
626
+ }, "output", "error">>;
572
627
  /**
573
628
  * Called when an agent hands off processing to another agent
574
629
  *
@@ -578,13 +633,19 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
578
633
  *
579
634
  * @param event Object containing the source agent, target agent, and input message
580
635
  */
581
- onHandoff?: (event: {
636
+ onHandoff?: ((event: {
582
637
  context: Context;
583
638
  source: Agent;
584
639
  target: Agent;
585
640
  input: I;
586
- }) => PromiseOrValue<void>;
641
+ }) => PromiseOrValue<void>) | Agent<{
642
+ source: Agent;
643
+ target: Agent;
644
+ input: I;
645
+ }>;
587
646
  }
647
+ export type AgentHookInput<H extends keyof AgentHooks, Hook extends AgentHooks[H] = AgentHooks[H]> = Hook extends (input: infer I) => any ? Omit<I, "context"> : never;
648
+ export type AgentHookOutput<H extends keyof AgentHooks, Hook extends AgentHooks[H] = AgentHooks[H]> = Hook extends (...args: any[]) => any ? Exclude<Awaited<ReturnType<Hook>>, void> | undefined : never;
588
649
  /**
589
650
  * Response type for an agent, can be:
590
651
  * - Direct response object
@@ -180,6 +180,7 @@ declare class AIGNEContextShared {
180
180
  span?: Span;
181
181
  constructor(parent?: (Pick<Context, "model" | "skills" | "limits" | "observer"> & {
182
182
  messageQueue?: MessageQueue;
183
+ events?: Emitter<any>;
183
184
  }) | undefined);
184
185
  readonly messageQueue: MessageQueue;
185
186
  readonly events: Emitter<any>;
@@ -1,17 +1,8 @@
1
- import { type ZodObject, type ZodType, z } from "zod";
2
1
  import { Agent, type FunctionAgentFn } from "../agents/agent.js";
3
2
  export declare function loadAgentFromJsFile(path: string): Promise<Agent<any, any> | {
4
3
  process: FunctionAgentFn;
5
4
  name: string;
6
5
  description?: string | undefined;
7
- inputSchema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
8
- [x: string]: any;
9
- }, {
10
- [x: string]: any;
11
- }> | undefined;
12
- outputSchema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
13
- [x: string]: any;
14
- }, {
15
- [x: string]: any;
16
- }> | undefined;
6
+ inputSchema?: any;
7
+ outputSchema?: any;
17
8
  }>;
@@ -1,12 +1,28 @@
1
1
  import { type ZodType } from "zod";
2
2
  import { AIAgentToolChoice } from "../agents/ai-agent.js";
3
3
  import { ProcessMode } from "../agents/team-agent.js";
4
+ export interface HooksSchema {
5
+ onStart?: NestAgentSchema;
6
+ onSuccess?: NestAgentSchema;
7
+ onError?: NestAgentSchema;
8
+ onEnd?: NestAgentSchema;
9
+ onSkillStart?: NestAgentSchema;
10
+ onSkillEnd?: NestAgentSchema;
11
+ onHandoff?: NestAgentSchema;
12
+ }
13
+ export type NestAgentSchema = string | {
14
+ url: string;
15
+ defaultInput?: Record<string, any>;
16
+ hooks?: HooksSchema | HooksSchema[];
17
+ } | AgentSchema;
4
18
  interface BaseAgentSchema {
5
19
  name?: string;
6
20
  description?: string;
7
21
  inputSchema?: ZodType<Record<string, any>>;
22
+ defaultInput?: Record<string, any>;
8
23
  outputSchema?: ZodType<Record<string, any>>;
9
- skills?: (string | AgentSchema)[];
24
+ skills?: NestAgentSchema[];
25
+ hooks?: HooksSchema | HooksSchema[];
10
26
  memory?: boolean | {
11
27
  provider: string;
12
28
  subscribeTopic?: string[];
@@ -19,7 +35,7 @@ interface AIAgentSchema extends BaseAgentSchema {
19
35
  outputKey?: string;
20
36
  toolChoice?: AIAgentToolChoice;
21
37
  }
22
- interface MCPAgentSchema {
38
+ interface MCPAgentSchema extends BaseAgentSchema {
23
39
  type: "mcp";
24
40
  url?: string;
25
41
  command?: string;
@@ -1,6 +1,6 @@
1
1
  import type { Camelize } from "camelize-ts";
2
2
  import { z } from "zod";
3
- import { Agent } from "../agents/agent.js";
3
+ import { Agent, type AgentOptions } from "../agents/agent.js";
4
4
  import type { ChatModel, ChatModelOptions } from "../agents/chat-model.js";
5
5
  import type { AIGNEOptions } from "../aigne/aigne.js";
6
6
  import type { MemoryAgent, MemoryAgentOptions } from "../memory/memory.js";
@@ -25,7 +25,7 @@ export interface LoadOptions {
25
25
  path: string;
26
26
  }
27
27
  export declare function load(options: LoadOptions): Promise<AIGNEOptions>;
28
- export declare function loadAgent(path: string, options?: LoadOptions): Promise<Agent>;
28
+ export declare function loadAgent(path: string, options?: LoadOptions, agentOptions?: AgentOptions): Promise<Agent>;
29
29
  export declare function loadModel(models: LoadableModel[], model?: Camelize<z.infer<typeof aigneFileSchema>["model"]>, modelOptions?: ChatModelOptions): Promise<ChatModel | undefined>;
30
30
  declare const aigneFileSchema: z.ZodObject<{
31
31
  name: z.ZodType<string | undefined, z.ZodTypeDef, string | undefined>;
@@ -22,4 +22,14 @@ export declare const inputOutputSchema: ({ path }: {
22
22
  required?: string[] | undefined;
23
23
  additionalProperties?: boolean | undefined;
24
24
  }>]>;
25
+ export declare const defaultInputSchema: z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodObject<{
26
+ $get: z.ZodString;
27
+ }, "strip", z.ZodTypeAny, {
28
+ $get: string;
29
+ }, {
30
+ $get: string;
31
+ }>, z.ZodUnknown]>>;
25
32
  export declare function optionalize<T>(schema: ZodType<T>): ZodType<T | undefined>;
33
+ export declare function camelizeSchema<T>(schema: ZodType<T>, { shallow }?: {
34
+ shallow?: boolean;
35
+ }): ZodType<T>;
@@ -19,7 +19,7 @@ export declare function pick<T extends object, K extends keyof T>(obj: T, ...key
19
19
  export declare function omit<T extends object, K extends keyof T>(obj: T, ...keys: (K | K[])[]): Omit<T, K>;
20
20
  export declare function omitDeep<T, K>(obj: T, ...keys: (K | K[])[]): unknown;
21
21
  export declare function omitBy<T extends Record<string, unknown>, K extends keyof T>(obj: T, predicate: (value: T[K], key: K) => boolean): Partial<T>;
22
- export declare function flat<T>(value?: T | T[]): T[];
22
+ export declare function flat<T>(...value: (T | T[])[]): NonNullable<T>[];
23
23
  export declare function createAccessorArray<T>(array: T[], accessor: (array: T[], name: string) => T | undefined): T[] & {
24
24
  [key: string]: T;
25
25
  };