@aigne/core 1.72.0-beta.2 → 1.72.0-beta.4

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 (105) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/lib/cjs/agents/agent.d.ts +31 -1
  3. package/lib/cjs/agents/agent.js +13 -0
  4. package/lib/cjs/agents/ai-agent.d.ts +7 -0
  5. package/lib/cjs/agents/ai-agent.js +85 -3
  6. package/lib/cjs/agents/chat-model.d.ts +153 -0
  7. package/lib/cjs/agents/chat-model.js +35 -1
  8. package/lib/cjs/agents/image-agent.d.ts +17 -1
  9. package/lib/cjs/agents/image-agent.js +16 -0
  10. package/lib/cjs/agents/image-model.d.ts +14 -4
  11. package/lib/cjs/agents/mcp-agent.d.ts +17 -0
  12. package/lib/cjs/agents/mcp-agent.js +18 -0
  13. package/lib/cjs/agents/team-agent.d.ts +55 -0
  14. package/lib/cjs/agents/team-agent.js +31 -0
  15. package/lib/cjs/agents/transform-agent.d.ts +12 -0
  16. package/lib/cjs/agents/transform-agent.js +13 -0
  17. package/lib/cjs/agents/video-model.d.ts +14 -4
  18. package/lib/cjs/aigne/usage.d.ts +4 -0
  19. package/lib/cjs/aigne/usage.js +6 -0
  20. package/lib/cjs/loader/agent-yaml.d.ts +5 -63
  21. package/lib/cjs/loader/agent-yaml.js +4 -129
  22. package/lib/cjs/loader/agents.d.ts +4 -0
  23. package/lib/cjs/loader/agents.js +17 -0
  24. package/lib/cjs/loader/index.d.ts +18 -14
  25. package/lib/cjs/loader/index.js +20 -81
  26. package/lib/cjs/loader/schema.d.ts +21 -6
  27. package/lib/cjs/loader/schema.js +60 -1
  28. package/lib/cjs/prompt/prompt-builder.js +0 -1
  29. package/lib/cjs/prompt/skills/afs/agent-skill/agent-skill.d.ts +17 -0
  30. package/lib/cjs/prompt/skills/afs/agent-skill/agent-skill.js +63 -0
  31. package/lib/cjs/prompt/skills/afs/agent-skill/skill-loader.d.ts +13 -0
  32. package/lib/cjs/prompt/skills/afs/agent-skill/skill-loader.js +61 -0
  33. package/lib/cjs/prompt/skills/afs/delete.js +15 -3
  34. package/lib/cjs/prompt/skills/afs/edit.js +30 -7
  35. package/lib/cjs/prompt/skills/afs/exec.js +17 -6
  36. package/lib/cjs/prompt/skills/afs/index.js +4 -1
  37. package/lib/cjs/prompt/skills/afs/list.js +26 -10
  38. package/lib/cjs/prompt/skills/afs/read.js +14 -3
  39. package/lib/cjs/prompt/skills/afs/rename.js +18 -4
  40. package/lib/cjs/prompt/skills/afs/search.js +21 -5
  41. package/lib/cjs/prompt/skills/afs/write.js +18 -4
  42. package/lib/cjs/prompt/template.d.ts +92 -17
  43. package/lib/cjs/prompt/template.js +46 -17
  44. package/lib/dts/agents/agent.d.ts +31 -1
  45. package/lib/dts/agents/ai-agent.d.ts +7 -0
  46. package/lib/dts/agents/chat-model.d.ts +153 -0
  47. package/lib/dts/agents/image-agent.d.ts +17 -1
  48. package/lib/dts/agents/image-model.d.ts +14 -4
  49. package/lib/dts/agents/mcp-agent.d.ts +17 -0
  50. package/lib/dts/agents/team-agent.d.ts +55 -0
  51. package/lib/dts/agents/transform-agent.d.ts +12 -0
  52. package/lib/dts/agents/video-model.d.ts +14 -4
  53. package/lib/dts/aigne/context.d.ts +2 -2
  54. package/lib/dts/aigne/usage.d.ts +4 -0
  55. package/lib/dts/loader/agent-yaml.d.ts +5 -63
  56. package/lib/dts/loader/agents.d.ts +4 -0
  57. package/lib/dts/loader/index.d.ts +18 -14
  58. package/lib/dts/loader/schema.d.ts +21 -6
  59. package/lib/dts/prompt/skills/afs/agent-skill/agent-skill.d.ts +17 -0
  60. package/lib/dts/prompt/skills/afs/agent-skill/skill-loader.d.ts +13 -0
  61. package/lib/dts/prompt/template.d.ts +92 -17
  62. package/lib/esm/agents/agent.d.ts +31 -1
  63. package/lib/esm/agents/agent.js +13 -0
  64. package/lib/esm/agents/ai-agent.d.ts +7 -0
  65. package/lib/esm/agents/ai-agent.js +85 -3
  66. package/lib/esm/agents/chat-model.d.ts +153 -0
  67. package/lib/esm/agents/chat-model.js +34 -0
  68. package/lib/esm/agents/image-agent.d.ts +17 -1
  69. package/lib/esm/agents/image-agent.js +16 -0
  70. package/lib/esm/agents/image-model.d.ts +14 -4
  71. package/lib/esm/agents/mcp-agent.d.ts +17 -0
  72. package/lib/esm/agents/mcp-agent.js +18 -0
  73. package/lib/esm/agents/team-agent.d.ts +55 -0
  74. package/lib/esm/agents/team-agent.js +31 -0
  75. package/lib/esm/agents/transform-agent.d.ts +12 -0
  76. package/lib/esm/agents/transform-agent.js +13 -0
  77. package/lib/esm/agents/video-model.d.ts +14 -4
  78. package/lib/esm/aigne/context.d.ts +2 -2
  79. package/lib/esm/aigne/usage.d.ts +4 -0
  80. package/lib/esm/aigne/usage.js +6 -0
  81. package/lib/esm/loader/agent-yaml.d.ts +5 -63
  82. package/lib/esm/loader/agent-yaml.js +4 -128
  83. package/lib/esm/loader/agents.d.ts +4 -0
  84. package/lib/esm/loader/agents.js +14 -0
  85. package/lib/esm/loader/index.d.ts +18 -14
  86. package/lib/esm/loader/index.js +21 -81
  87. package/lib/esm/loader/schema.d.ts +21 -6
  88. package/lib/esm/loader/schema.js +57 -0
  89. package/lib/esm/prompt/prompt-builder.js +1 -2
  90. package/lib/esm/prompt/skills/afs/agent-skill/agent-skill.d.ts +17 -0
  91. package/lib/esm/prompt/skills/afs/agent-skill/agent-skill.js +59 -0
  92. package/lib/esm/prompt/skills/afs/agent-skill/skill-loader.d.ts +13 -0
  93. package/lib/esm/prompt/skills/afs/agent-skill/skill-loader.js +53 -0
  94. package/lib/esm/prompt/skills/afs/delete.js +15 -3
  95. package/lib/esm/prompt/skills/afs/edit.js +30 -7
  96. package/lib/esm/prompt/skills/afs/exec.js +17 -6
  97. package/lib/esm/prompt/skills/afs/index.js +4 -1
  98. package/lib/esm/prompt/skills/afs/list.js +26 -10
  99. package/lib/esm/prompt/skills/afs/read.js +14 -3
  100. package/lib/esm/prompt/skills/afs/rename.js +18 -4
  101. package/lib/esm/prompt/skills/afs/search.js +21 -5
  102. package/lib/esm/prompt/skills/afs/write.js +18 -4
  103. package/lib/esm/prompt/template.d.ts +92 -17
  104. package/lib/esm/prompt/template.js +46 -17
  105. package/package.json +5 -4
@@ -94,6 +94,7 @@ export declare const videoModelInputSchema: z.ZodObject<{
94
94
  prompt: string;
95
95
  model?: string | undefined;
96
96
  modelOptions?: Record<string, unknown> | undefined;
97
+ outputFileType?: "local" | "url" | "file" | undefined;
97
98
  image?: {
98
99
  type: "url";
99
100
  url: string;
@@ -110,13 +111,13 @@ export declare const videoModelInputSchema: z.ZodObject<{
110
111
  filename?: string | undefined;
111
112
  mimeType?: string | undefined;
112
113
  } | undefined;
113
- outputFileType?: "local" | "file" | "url" | undefined;
114
114
  size?: string | undefined;
115
115
  seconds?: string | undefined;
116
116
  }, {
117
117
  prompt: string;
118
118
  model?: string | undefined;
119
119
  modelOptions?: Record<string, unknown> | undefined;
120
+ outputFileType?: "local" | "url" | "file" | undefined;
120
121
  image?: {
121
122
  type: "url";
122
123
  url: string;
@@ -133,7 +134,6 @@ export declare const videoModelInputSchema: z.ZodObject<{
133
134
  filename?: string | undefined;
134
135
  mimeType?: string | undefined;
135
136
  } | undefined;
136
- outputFileType?: "local" | "file" | "url" | undefined;
137
137
  size?: string | undefined;
138
138
  seconds?: string | undefined;
139
139
  }>;
@@ -203,16 +203,22 @@ export declare const videoModelOutputSchema: z.ZodObject<{
203
203
  inputTokens: z.ZodNumber;
204
204
  outputTokens: z.ZodNumber;
205
205
  aigneHubCredits: ZodType<number | undefined, z.ZodTypeDef, number | undefined>;
206
+ cacheCreationInputTokens: ZodType<number | undefined, z.ZodTypeDef, number | undefined>;
207
+ cacheReadInputTokens: ZodType<number | undefined, z.ZodTypeDef, number | undefined>;
206
208
  creditPrefix: ZodType<"$" | "€" | "¥" | undefined, z.ZodTypeDef, "$" | "€" | "¥" | undefined>;
207
209
  }, "strip", z.ZodTypeAny, {
208
210
  inputTokens: number;
209
211
  outputTokens: number;
210
212
  aigneHubCredits?: number | undefined;
213
+ cacheCreationInputTokens?: number | undefined;
214
+ cacheReadInputTokens?: number | undefined;
211
215
  creditPrefix?: "$" | "€" | "¥" | undefined;
212
216
  }, {
213
217
  inputTokens: number;
214
218
  outputTokens: number;
215
219
  aigneHubCredits?: number | undefined;
220
+ cacheCreationInputTokens?: number | undefined;
221
+ cacheReadInputTokens?: number | undefined;
216
222
  creditPrefix?: "$" | "€" | "¥" | undefined;
217
223
  }>>;
218
224
  model: z.ZodOptional<z.ZodString>;
@@ -234,13 +240,15 @@ export declare const videoModelOutputSchema: z.ZodObject<{
234
240
  filename?: string | undefined;
235
241
  mimeType?: string | undefined;
236
242
  })[];
237
- model?: string | undefined;
238
243
  usage?: {
239
244
  inputTokens: number;
240
245
  outputTokens: number;
241
246
  aigneHubCredits?: number | undefined;
247
+ cacheCreationInputTokens?: number | undefined;
248
+ cacheReadInputTokens?: number | undefined;
242
249
  creditPrefix?: "$" | "€" | "¥" | undefined;
243
250
  } | undefined;
251
+ model?: string | undefined;
244
252
  seconds?: number | undefined;
245
253
  }, {
246
254
  videos: ({
@@ -259,12 +267,14 @@ export declare const videoModelOutputSchema: z.ZodObject<{
259
267
  filename?: string | undefined;
260
268
  mimeType?: string | undefined;
261
269
  })[];
262
- model?: string | undefined;
263
270
  usage?: {
264
271
  inputTokens: number;
265
272
  outputTokens: number;
266
273
  aigneHubCredits?: number | undefined;
274
+ cacheCreationInputTokens?: number | undefined;
275
+ cacheReadInputTokens?: number | undefined;
267
276
  creditPrefix?: "$" | "€" | "¥" | undefined;
268
277
  } | undefined;
278
+ model?: string | undefined;
269
279
  seconds?: number | undefined;
270
280
  }>;
@@ -159,7 +159,7 @@ export declare class AIGNEContext implements Context {
159
159
  readonly internal: AIGNEContextShared;
160
160
  get messageQueue(): MessageQueue;
161
161
  get model(): ChatModel | undefined;
162
- get imageModel(): ImageModel<import("../agents/image-model.js").ImageModelInput, import("../agents/image-model.js").ImageModelOutput> | undefined;
162
+ get imageModel(): ImageModel<import("../index.js").ImageModelInput, import("../index.js").ImageModelOutput> | undefined;
163
163
  get skills(): Agent<any, any>[] | undefined;
164
164
  get agents(): Agent<any, any>[];
165
165
  get observer(): AIGNEObserver | undefined;
@@ -197,7 +197,7 @@ declare class AIGNEContextShared {
197
197
  readonly messageQueue: MessageQueue;
198
198
  readonly events: Emitter<any>;
199
199
  get model(): ChatModel | undefined;
200
- get imageModel(): ImageModel<import("../agents/image-model.js").ImageModelInput, import("../agents/image-model.js").ImageModelOutput> | undefined;
200
+ get imageModel(): ImageModel<import("../index.js").ImageModelInput, import("../index.js").ImageModelOutput> | undefined;
201
201
  get skills(): Agent<any, any>[] | undefined;
202
202
  get agents(): Agent<any, any>[];
203
203
  get observer(): AIGNEObserver | undefined;
@@ -8,6 +8,10 @@ export interface ContextUsage {
8
8
  creditPrefix?: "$" | "€" | "¥";
9
9
  agentCalls: number;
10
10
  duration: number;
11
+ /** Number of tokens written to cache (first time caching) */
12
+ cacheCreationInputTokens: number;
13
+ /** Number of tokens read from cache (cache hit) */
14
+ cacheReadInputTokens: number;
11
15
  }
12
16
  /**
13
17
  * @hidden
@@ -8,6 +8,8 @@ export function newEmptyContextUsage() {
8
8
  aigneHubCredits: 0,
9
9
  agentCalls: 0,
10
10
  duration: 0,
11
+ cacheCreationInputTokens: 0,
12
+ cacheReadInputTokens: 0,
11
13
  };
12
14
  }
13
15
  export function mergeContextUsage(usage, additional) {
@@ -21,4 +23,8 @@ export function mergeContextUsage(usage, additional) {
21
23
  usage.agentCalls += additional.agentCalls;
22
24
  if (additional.duration)
23
25
  usage.duration += additional.duration;
26
+ if (additional.cacheCreationInputTokens)
27
+ usage.cacheCreationInputTokens += additional.cacheCreationInputTokens;
28
+ if (additional.cacheReadInputTokens)
29
+ usage.cacheReadInputTokens += additional.cacheReadInputTokens;
24
30
  }
@@ -1,9 +1,6 @@
1
1
  import type { AFSOptions } from "@aigne/afs";
2
2
  import { type ZodType, z } from "zod";
3
- import type { AgentClass, AgentHooks, FunctionAgentFn, TaskRenderMode } from "../agents/agent.js";
4
- import { AIAgentToolChoice } from "../agents/ai-agent.js";
5
- import { type Role } from "../agents/chat-model.js";
6
- import { ProcessMode, type ReflectionMode } from "../agents/team-agent.js";
3
+ import type { AgentHooks, TaskRenderMode } from "../agents/agent.js";
7
4
  import type { LoadOptions } from "./index.js";
8
5
  import { chatModelSchema, imageModelSchema } from "./schema.js";
9
6
  export interface HooksSchema {
@@ -45,7 +42,8 @@ export interface AFSContextSchema {
45
42
  presets?: Record<string, AFSContextPresetSchema>;
46
43
  };
47
44
  }
48
- export interface BaseAgentSchema {
45
+ export interface AgentSchema {
46
+ type: string;
49
47
  name?: string;
50
48
  description?: string;
51
49
  model?: z.infer<typeof chatModelSchema>;
@@ -67,67 +65,11 @@ export interface BaseAgentSchema {
67
65
  context?: AFSContextSchema;
68
66
  });
69
67
  shareAFS?: boolean;
68
+ [key: string]: unknown;
70
69
  }
71
- export type Instructions = {
72
- role: Exclude<Role, "tool">;
73
- content: string;
74
- path: string;
75
- }[];
76
- export interface AIAgentSchema extends BaseAgentSchema {
77
- type: "ai";
78
- instructions?: Instructions;
79
- autoReorderSystemMessages?: boolean;
80
- autoMergeSystemMessages?: boolean;
81
- inputKey?: string;
82
- inputFileKey?: string;
83
- outputKey?: string;
84
- outputFileKey?: string;
85
- toolChoice?: AIAgentToolChoice;
86
- toolCallsConcurrency?: number;
87
- keepTextInToolUses?: boolean;
88
- }
89
- export interface ImageAgentSchema extends BaseAgentSchema {
90
- type: "image";
91
- instructions: Instructions;
92
- inputFileKey?: string;
93
- }
94
- export interface MCPAgentSchema extends BaseAgentSchema {
95
- type: "mcp";
96
- url?: string;
97
- command?: string;
98
- args?: string[];
99
- }
100
- export interface TeamAgentSchema extends BaseAgentSchema {
101
- type: "team";
102
- mode?: ProcessMode;
103
- iterateOn?: string;
104
- concurrency?: number;
105
- iterateWithPreviousOutput?: boolean;
106
- includeAllStepsOutput?: boolean;
107
- reflection?: Omit<ReflectionMode, "reviewer"> & {
108
- reviewer: NestAgentSchema;
109
- };
110
- }
111
- export interface TransformAgentSchema extends BaseAgentSchema {
112
- type: "transform";
113
- jsonata: string;
114
- }
115
- export interface FunctionAgentSchema extends BaseAgentSchema {
116
- type: "function";
117
- process: FunctionAgentFn;
118
- }
119
- export interface ThirdAgentSchema extends BaseAgentSchema {
120
- agentClass?: AgentClass;
121
- type: "";
122
- [key: string]: any;
123
- }
124
- export type AgentSchema = AIAgentSchema | ImageAgentSchema | MCPAgentSchema | TeamAgentSchema | TransformAgentSchema | FunctionAgentSchema | ThirdAgentSchema;
125
70
  export declare function parseAgentFile(path: string, data: any, options: LoadOptions): Promise<AgentSchema>;
126
71
  export declare function loadAgentFromYamlFile(path: string, options: LoadOptions): Promise<AgentSchema>;
127
- export declare const getInstructionsSchema: ({ filepath }: {
128
- filepath: string;
129
- }) => ZodType<Instructions>;
130
- export declare const getAgentSchema: ({ filepath, options, }: {
72
+ export declare const getAgentSchema: ({ filepath }: {
131
73
  filepath: string;
132
74
  options?: LoadOptions;
133
75
  }) => ZodType<AgentSchema, z.ZodTypeDef, AgentSchema>;
@@ -2,12 +2,8 @@ import { jsonSchemaToZod } from "@aigne/json-schema-to-zod";
2
2
  import { nodejs } from "@aigne/platform-helpers/nodejs/index.js";
3
3
  import { parse } from "yaml";
4
4
  import { z } from "zod";
5
- import { AIAgentToolChoice } from "../agents/ai-agent.js";
6
- import { roleSchema } from "../agents/chat-model.js";
7
- import { ProcessMode } from "../agents/team-agent.js";
8
5
  import { tryOrThrow } from "../utils/type-utils.js";
9
- import { codeToFunctionAgentFn } from "./function-agent.js";
10
- import { camelizeSchema, chatModelSchema, defaultInputSchema, imageModelSchema, inputOutputSchema, optionalize, preprocessSchema, } from "./schema.js";
6
+ import { camelizeSchema, chatModelSchema, defaultInputSchema, imageModelSchema, inputOutputSchema, optionalize, } from "./schema.js";
11
7
  export async function parseAgentFile(path, data, options) {
12
8
  const agentSchema = getAgentSchema({ filepath: path, options });
13
9
  return agentSchema.parseAsync({
@@ -25,41 +21,7 @@ export async function loadAgentFromYamlFile(path, options) {
25
21
  }, options), (error) => new Error(`Failed to validate agent definition from ${path}: ${error.message}`));
26
22
  return agent;
27
23
  }
28
- const instructionItemSchema = z.union([
29
- z.object({
30
- role: roleSchema.default("system"),
31
- url: z.string(),
32
- }),
33
- z.object({
34
- role: roleSchema.default("system"),
35
- content: z.string(),
36
- }),
37
- ]);
38
- const parseInstructionItem = ({ filepath }) => async ({ role, ...v }) => {
39
- if (role === "tool")
40
- throw new Error(`'tool' role is not allowed in instruction item in agent file ${filepath}`);
41
- if ("content" in v && typeof v.content === "string") {
42
- return { role, content: v.content, path: filepath };
43
- }
44
- if ("url" in v && typeof v.url === "string") {
45
- const url = nodejs.path.isAbsolute(v.url)
46
- ? v.url
47
- : nodejs.path.join(nodejs.path.dirname(filepath), v.url);
48
- return nodejs.fs.readFile(url, "utf8").then((content) => ({ role, content, path: url }));
49
- }
50
- throw new Error(`Invalid instruction item in agent file ${filepath}. Expected 'content' or 'url' property`);
51
- };
52
- export const getInstructionsSchema = ({ filepath }) => z
53
- .union([z.string(), instructionItemSchema, z.array(instructionItemSchema)])
54
- .transform(async (v) => {
55
- if (typeof v === "string")
56
- return [{ role: "system", content: v, path: filepath }];
57
- if (Array.isArray(v)) {
58
- return Promise.all(v.map((item) => parseInstructionItem({ filepath })(item)));
59
- }
60
- return [await parseInstructionItem({ filepath })(v)];
61
- });
62
- export const getAgentSchema = ({ filepath, options, }) => {
24
+ export const getAgentSchema = ({ filepath }) => {
63
25
  const agentSchema = z.lazy(() => {
64
26
  const nestAgentSchema = z.lazy(() => z.union([
65
27
  agentSchema,
@@ -95,6 +57,7 @@ export const getAgentSchema = ({ filepath, options, }) => {
95
57
  }))),
96
58
  });
97
59
  const baseAgentSchema = z.object({
60
+ type: z.string(),
98
61
  name: optionalize(z.string()),
99
62
  alias: optionalize(z.array(z.string())),
100
63
  description: optionalize(z.string()),
@@ -133,94 +96,7 @@ export const getAgentSchema = ({ filepath, options, }) => {
133
96
  ])),
134
97
  shareAFS: optionalize(z.boolean()),
135
98
  });
136
- const instructionsSchema = getInstructionsSchema({ filepath: filepath });
137
- return camelizeSchema(preprocessSchema(async (json) => {
138
- if (typeof json === "object" &&
139
- json &&
140
- "type" in json &&
141
- typeof json.type === "string" &&
142
- !["ai", "image", "mcp", "team", "transform", "function"].includes(json.type)) {
143
- if (!options?.require)
144
- throw new Error(`Module loader is not provided to load agent type module ${json.type} from ${filepath}`);
145
- const Mod = await options.require(json.type, { parent: filepath });
146
- if (typeof Mod?.default?.prototype?.constructor !== "function") {
147
- throw new Error(`The agent type module ${json.type} does not export a default Agent class`);
148
- }
149
- Object.assign(json, { agentClass: Mod.default });
150
- }
151
- return json;
152
- }, z.union([
153
- z
154
- .object({
155
- type: z.string(),
156
- agentClass: z.custom((v) => typeof v?.prototype?.constructor === "function"),
157
- })
158
- .extend(baseAgentSchema.shape)
159
- .passthrough(),
160
- z.discriminatedUnion("type", [
161
- z
162
- .object({
163
- type: z.literal("ai"),
164
- instructions: optionalize(instructionsSchema),
165
- autoReorderSystemMessages: optionalize(z.boolean()),
166
- autoMergeSystemMessages: optionalize(z.boolean()),
167
- inputKey: optionalize(z.string()),
168
- outputKey: optionalize(z.string()),
169
- inputFileKey: optionalize(z.string()),
170
- outputFileKey: optionalize(z.string()),
171
- toolChoice: optionalize(z.nativeEnum(AIAgentToolChoice)),
172
- toolCallsConcurrency: optionalize(z.number().int().min(0)),
173
- keepTextInToolUses: optionalize(z.boolean()),
174
- catchToolsError: optionalize(z.boolean()),
175
- structuredStreamMode: optionalize(z.boolean()),
176
- })
177
- .extend(baseAgentSchema.shape),
178
- z
179
- .object({
180
- type: z.literal("image"),
181
- instructions: instructionsSchema,
182
- inputFileKey: optionalize(z.string()),
183
- })
184
- .extend(baseAgentSchema.shape),
185
- z
186
- .object({
187
- type: z.literal("mcp"),
188
- url: optionalize(z.string()),
189
- command: optionalize(z.string()),
190
- args: optionalize(z.array(z.string())),
191
- })
192
- .extend(baseAgentSchema.shape),
193
- z
194
- .object({
195
- type: z.literal("team"),
196
- mode: optionalize(z.nativeEnum(ProcessMode)),
197
- iterateOn: optionalize(z.string()),
198
- concurrency: optionalize(z.number().int().min(1)),
199
- iterateWithPreviousOutput: optionalize(z.boolean()),
200
- includeAllStepsOutput: optionalize(z.boolean()),
201
- reflection: camelizeSchema(optionalize(z.object({
202
- reviewer: nestAgentSchema,
203
- isApproved: z.string(),
204
- maxIterations: optionalize(z.number().int().min(1)),
205
- returnLastOnMaxIterations: optionalize(z.boolean()),
206
- customErrorMessage: optionalize(z.string()),
207
- }))),
208
- })
209
- .extend(baseAgentSchema.shape),
210
- z
211
- .object({
212
- type: z.literal("transform"),
213
- jsonata: z.string(),
214
- })
215
- .extend(baseAgentSchema.shape),
216
- z
217
- .object({
218
- type: z.literal("function"),
219
- process: z.preprocess((v) => (typeof v === "string" ? codeToFunctionAgentFn(v) : v), z.custom()),
220
- })
221
- .extend(baseAgentSchema.shape),
222
- ]),
223
- ])));
99
+ return camelizeSchema(baseAgentSchema.passthrough());
224
100
  });
225
101
  return agentSchema;
226
102
  };
@@ -0,0 +1,4 @@
1
+ import type { AgentClass } from "../agents/types.ts";
2
+ export declare const builtinAgents: {
3
+ [type: string]: AgentClass;
4
+ };
@@ -0,0 +1,14 @@
1
+ import { FunctionAgent } from "../agents/agent.js";
2
+ import { AIAgent } from "../agents/ai-agent.js";
3
+ import { ImageAgent } from "../agents/image-agent.js";
4
+ import { MCPAgent } from "../agents/mcp-agent.js";
5
+ import { TeamAgent } from "../agents/team-agent.js";
6
+ import { TransformAgent } from "../agents/transform-agent.js";
7
+ export const builtinAgents = {
8
+ ai: AIAgent,
9
+ function: FunctionAgent,
10
+ image: ImageAgent,
11
+ mcp: MCPAgent,
12
+ team: TeamAgent,
13
+ transform: TransformAgent,
14
+ };
@@ -5,9 +5,8 @@ import type { ChatModel } from "../agents/chat-model.js";
5
5
  import type { ImageModel } from "../agents/image-model.js";
6
6
  import type { AIGNEOptions } from "../aigne/aigne.js";
7
7
  import type { MemoryAgent, MemoryAgentOptions } from "../memory/memory.js";
8
- import { PromptBuilder } from "../prompt/prompt-builder.js";
9
8
  import { type PromiseOrValue } from "../utils/type-utils.js";
10
- import { type Instructions, loadAgentFromYamlFile, type NestAgentSchema } from "./agent-yaml.js";
9
+ import { loadAgentFromYamlFile, type NestAgentSchema } from "./agent-yaml.js";
11
10
  export interface LoadOptions {
12
11
  memories?: {
13
12
  new (parameters?: MemoryAgentOptions): MemoryAgent;
@@ -19,7 +18,10 @@ export interface LoadOptions {
19
18
  availableModules?: {
20
19
  module: string;
21
20
  alias?: string[];
22
- create: (options?: Record<string, any>) => PromiseOrValue<AFSModule>;
21
+ load: (options: {
22
+ filepath: string;
23
+ parsed?: object;
24
+ }) => PromiseOrValue<AFSModule>;
23
25
  }[];
24
26
  };
25
27
  aigne?: z.infer<typeof aigneFileSchema>;
@@ -27,6 +29,9 @@ export interface LoadOptions {
27
29
  parent?: string;
28
30
  }) => Promise<any>;
29
31
  }
32
+ export interface AgentLoadOptions extends LoadOptions {
33
+ loadNestAgent: (path: string, agent: NestAgentSchema, options: LoadOptions, agentOptions?: AgentOptions<any, any> & Record<string, unknown>) => Promise<Agent>;
34
+ }
30
35
  export declare function load(path: string, options?: LoadOptions): Promise<AIGNEOptions>;
31
36
  export declare function loadAgent(path: string, options: LoadOptions, agentOptions?: AgentOptions): Promise<Agent>;
32
37
  export declare function loadNestAgent(path: string, agent: NestAgentSchema, options: LoadOptions, agentOptions?: AgentOptions<any, any> & Record<string, unknown>): Promise<Agent>;
@@ -67,9 +72,9 @@ declare const aigneFileSchema: z.ZodObject<{
67
72
  } | undefined, z.ZodTypeDef, number | {
68
73
  $get: string;
69
74
  } | undefined>;
70
- thinkingEffort: ZodType<number | "high" | "medium" | "low" | "minimal" | {
75
+ thinkingEffort: ZodType<number | "high" | "low" | "medium" | "minimal" | {
71
76
  $get: string;
72
- } | undefined, z.ZodTypeDef, number | "high" | "medium" | "low" | "minimal" | {
77
+ } | undefined, z.ZodTypeDef, number | "high" | "low" | "medium" | "minimal" | {
73
78
  $get: string;
74
79
  } | undefined>;
75
80
  }, z.ZodTypeAny, "passthrough"> | undefined, z.ZodTypeDef, z.objectInputType<{
@@ -98,9 +103,9 @@ declare const aigneFileSchema: z.ZodObject<{
98
103
  } | undefined, z.ZodTypeDef, number | {
99
104
  $get: string;
100
105
  } | undefined>;
101
- thinkingEffort: ZodType<number | "high" | "medium" | "low" | "minimal" | {
106
+ thinkingEffort: ZodType<number | "high" | "low" | "medium" | "minimal" | {
102
107
  $get: string;
103
- } | undefined, z.ZodTypeDef, number | "high" | "medium" | "low" | "minimal" | {
108
+ } | undefined, z.ZodTypeDef, number | "high" | "low" | "medium" | "minimal" | {
104
109
  $get: string;
105
110
  } | undefined>;
106
111
  }, z.ZodTypeAny, "passthrough"> | undefined>;
@@ -132,6 +137,7 @@ declare const aigneFileSchema: z.ZodObject<{
132
137
  chat?: string | undefined;
133
138
  } | undefined>;
134
139
  }, "strip", z.ZodTypeAny, {
140
+ name?: string | undefined;
135
141
  model?: z.objectInputType<{
136
142
  model: ZodType<string | {
137
143
  $get: string;
@@ -158,13 +164,12 @@ declare const aigneFileSchema: z.ZodObject<{
158
164
  } | undefined, z.ZodTypeDef, number | {
159
165
  $get: string;
160
166
  } | undefined>;
161
- thinkingEffort: ZodType<number | "high" | "medium" | "low" | "minimal" | {
167
+ thinkingEffort: ZodType<number | "high" | "low" | "medium" | "minimal" | {
162
168
  $get: string;
163
- } | undefined, z.ZodTypeDef, number | "high" | "medium" | "low" | "minimal" | {
169
+ } | undefined, z.ZodTypeDef, number | "high" | "low" | "medium" | "minimal" | {
164
170
  $get: string;
165
171
  } | undefined>;
166
172
  }, z.ZodTypeAny, "passthrough"> | undefined;
167
- name?: string | undefined;
168
173
  description?: string | undefined;
169
174
  imageModel?: z.objectInputType<{
170
175
  model: ZodType<string | {
@@ -183,6 +188,7 @@ declare const aigneFileSchema: z.ZodObject<{
183
188
  chat?: string | undefined;
184
189
  } | undefined;
185
190
  }, {
191
+ name?: string | undefined;
186
192
  model?: z.objectInputType<{
187
193
  model: ZodType<string | {
188
194
  $get: string;
@@ -209,13 +215,12 @@ declare const aigneFileSchema: z.ZodObject<{
209
215
  } | undefined, z.ZodTypeDef, number | {
210
216
  $get: string;
211
217
  } | undefined>;
212
- thinkingEffort: ZodType<number | "high" | "medium" | "low" | "minimal" | {
218
+ thinkingEffort: ZodType<number | "high" | "low" | "medium" | "minimal" | {
213
219
  $get: string;
214
- } | undefined, z.ZodTypeDef, number | "high" | "medium" | "low" | "minimal" | {
220
+ } | undefined, z.ZodTypeDef, number | "high" | "low" | "medium" | "minimal" | {
215
221
  $get: string;
216
222
  } | undefined>;
217
223
  }, z.ZodTypeAny, "passthrough"> | undefined;
218
- name?: string | undefined;
219
224
  description?: string | undefined;
220
225
  imageModel?: z.objectInputType<{
221
226
  model: ZodType<string | {
@@ -239,5 +244,4 @@ export declare function loadAIGNEFile(path: string): Promise<{
239
244
  rootDir: string;
240
245
  }>;
241
246
  export declare function findAIGNEFile(path: string): Promise<string>;
242
- export declare function instructionsToPromptBuilder(instructions: Instructions): PromptBuilder;
243
247
  export {};
@@ -2,18 +2,12 @@ import { AFS } from "@aigne/afs";
2
2
  import { nodejs } from "@aigne/platform-helpers/nodejs/index.js";
3
3
  import { parse } from "yaml";
4
4
  import { z } from "zod";
5
- import { Agent, FunctionAgent } from "../agents/agent.js";
6
- import { AIAgent } from "../agents/ai-agent.js";
7
- import { ImageAgent } from "../agents/image-agent.js";
8
- import { MCPAgent } from "../agents/mcp-agent.js";
9
- import { TeamAgent } from "../agents/team-agent.js";
10
- import { TransformAgent } from "../agents/transform-agent.js";
11
- import { PromptBuilder } from "../prompt/prompt-builder.js";
12
- import { ChatMessagesTemplate, parseChatMessages } from "../prompt/template.js";
5
+ import { Agent } from "../agents/agent.js";
13
6
  import { isAgent } from "../utils/agent-utils.js";
14
7
  import { flat, isNil, isNonNullable, omitBy, tryOrThrow, } from "../utils/type-utils.js";
15
8
  import { loadAgentFromJsFile } from "./agent-js.js";
16
9
  import { loadAgentFromYamlFile, } from "./agent-yaml.js";
10
+ import { builtinAgents } from "./agents.js";
17
11
  import { camelizeSchema, chatModelSchema, imageModelSchema, optionalize } from "./schema.js";
18
12
  const AIGNE_FILE_NAME = ["aigne.yaml", "aigne.yml"];
19
13
  export async function load(path, options = {}) {
@@ -154,7 +148,10 @@ export async function parseAgent(path, agent, options, agentOptions) {
154
148
  const mod = options?.afs?.availableModules?.find((mod) => mod.module === moduleName || mod.alias?.includes(moduleName));
155
149
  if (!mod)
156
150
  throw new Error(`AFS module not found: ${typeof m === "string" ? m : m.module}`);
157
- const module = await mod.create(typeof m === "string" ? {} : m.options);
151
+ const module = await mod.load({
152
+ filepath: path,
153
+ parsed: typeof m === "string" ? {} : m.options,
154
+ });
158
155
  afs.mount(module);
159
156
  }
160
157
  }
@@ -186,73 +183,24 @@ export async function parseAgent(path, agent, options, agentOptions) {
186
183
  skills: [...(agentOptions?.skills || []), ...skills],
187
184
  afs: afs || agentOptions?.afs,
188
185
  };
189
- let instructions;
190
- if ("instructions" in agent && agent.instructions && ["ai", "image"].includes(agent.type)) {
191
- instructions = instructionsToPromptBuilder(agent.instructions);
192
- }
193
- switch (agent.type) {
194
- case "ai": {
195
- return AIAgent.from({
196
- ...baseOptions,
197
- instructions,
198
- });
199
- }
200
- case "image": {
201
- if (!instructions)
202
- throw new Error(`Missing required instructions for image agent at path: ${path}`);
203
- return ImageAgent.from({
204
- ...baseOptions,
205
- instructions,
206
- });
207
- }
208
- case "mcp": {
209
- if (agent.url) {
210
- return MCPAgent.from({
211
- ...baseOptions,
212
- url: agent.url,
213
- });
214
- }
215
- if (agent.command) {
216
- return MCPAgent.from({
217
- ...baseOptions,
218
- command: agent.command,
219
- args: agent.args,
220
- });
221
- }
222
- throw new Error(`Missing url or command in mcp agent: ${path}`);
223
- }
224
- case "team": {
225
- return TeamAgent.from({
226
- ...baseOptions,
227
- mode: agent.mode,
228
- iterateOn: agent.iterateOn,
229
- reflection: agent.reflection && {
230
- ...agent.reflection,
231
- reviewer: await loadNestAgent(path, agent.reflection.reviewer, options),
232
- },
233
- });
234
- }
235
- case "transform": {
236
- return TransformAgent.from({
237
- ...baseOptions,
238
- jsonata: agent.jsonata,
239
- });
240
- }
241
- case "function": {
242
- return FunctionAgent.from({
243
- ...baseOptions,
244
- process: agent.process,
245
- });
186
+ let agentClass = builtinAgents[agent.type];
187
+ if (!agentClass) {
188
+ if (!options?.require)
189
+ throw new Error(`Module loader is not provided to load agent type module ${agent.type} from ${path}`);
190
+ const Mod = await options.require(agent.type, { parent: path });
191
+ if (typeof Mod?.default?.prototype?.constructor !== "function") {
192
+ throw new Error(`The agent type module ${agent.type} does not export a default Agent class`);
246
193
  }
194
+ agentClass = Mod.default;
247
195
  }
248
- if ("agentClass" in agent && agent.agentClass) {
249
- return await agent.agentClass.load({
250
- filepath: path,
251
- parsed: baseOptions,
252
- options,
253
- });
196
+ if (!agentClass) {
197
+ throw new Error(`Unsupported agent type: ${agent.type} from ${path}`);
254
198
  }
255
- throw new Error(`Unsupported agent type: ${"type" in agent ? agent.type : "unknown"} at path: ${path}`);
199
+ return await agentClass.load({
200
+ filepath: path,
201
+ parsed: baseOptions,
202
+ options: { ...options, loadNestAgent },
203
+ });
256
204
  }
257
205
  async function loadMemory(memories, provider, options) {
258
206
  const M = !provider
@@ -308,11 +256,3 @@ export async function findAIGNEFile(path) {
308
256
  }
309
257
  throw new Error(`aigne.yaml not found in ${path}. Please ensure you are in the correct directory or provide a valid path.`);
310
258
  }
311
- export function instructionsToPromptBuilder(instructions) {
312
- return new PromptBuilder({
313
- instructions: ChatMessagesTemplate.from(parseChatMessages(instructions.map((i) => ({
314
- ...i,
315
- options: { workingDir: nodejs.path.dirname(i.path) },
316
- })))),
317
- });
318
- }