@aigne/core 1.71.0-beta.1 → 1.71.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.
- package/CHANGELOG.md +43 -0
- package/lib/cjs/agents/agent.d.ts +6 -0
- package/lib/cjs/agents/agent.js +4 -1
- package/lib/cjs/aigne/aigne.d.ts +6 -1
- package/lib/cjs/aigne/aigne.js +13 -4
- package/lib/cjs/aigne/context.d.ts +2 -2
- package/lib/cjs/aigne/context.js +5 -22
- package/lib/cjs/loader/agent-js.d.ts +2 -1
- package/lib/cjs/loader/agent-js.js +2 -2
- package/lib/cjs/loader/agent-yaml.d.ts +4 -3
- package/lib/cjs/loader/agent-yaml.js +21 -17
- package/lib/cjs/loader/index.d.ts +3 -3
- package/lib/cjs/loader/index.js +1 -1
- package/lib/cjs/loader/schema.d.ts +1 -0
- package/lib/cjs/loader/schema.js +4 -0
- package/lib/cjs/prompt/filters/index.js +11 -9
- package/lib/cjs/prompt/prompt-builder.d.ts +4 -0
- package/lib/cjs/prompt/prompt-builder.js +55 -1
- package/lib/cjs/prompt/prompts/afs-builtin-prompt.d.ts +1 -0
- package/lib/cjs/prompt/prompts/afs-builtin-prompt.js +7 -4
- package/lib/cjs/prompt/skills/afs.js +7 -0
- package/lib/cjs/utils/event-stream.js +1 -0
- package/lib/dts/agents/agent.d.ts +6 -0
- package/lib/dts/aigne/aigne.d.ts +6 -1
- package/lib/dts/aigne/context.d.ts +2 -2
- package/lib/dts/loader/agent-js.d.ts +2 -1
- package/lib/dts/loader/agent-yaml.d.ts +4 -3
- package/lib/dts/loader/index.d.ts +3 -3
- package/lib/dts/loader/schema.d.ts +1 -0
- package/lib/dts/prompt/prompt-builder.d.ts +4 -0
- package/lib/dts/prompt/prompts/afs-builtin-prompt.d.ts +1 -0
- package/lib/esm/agents/agent.d.ts +6 -0
- package/lib/esm/agents/agent.js +4 -1
- package/lib/esm/aigne/aigne.d.ts +6 -1
- package/lib/esm/aigne/aigne.js +13 -4
- package/lib/esm/aigne/context.d.ts +2 -2
- package/lib/esm/aigne/context.js +5 -22
- package/lib/esm/loader/agent-js.d.ts +2 -1
- package/lib/esm/loader/agent-js.js +2 -2
- package/lib/esm/loader/agent-yaml.d.ts +4 -3
- package/lib/esm/loader/agent-yaml.js +22 -18
- package/lib/esm/loader/index.d.ts +3 -3
- package/lib/esm/loader/index.js +1 -1
- package/lib/esm/loader/schema.d.ts +1 -0
- package/lib/esm/loader/schema.js +3 -0
- package/lib/esm/prompt/filters/index.js +11 -9
- package/lib/esm/prompt/prompt-builder.d.ts +4 -0
- package/lib/esm/prompt/prompt-builder.js +57 -3
- package/lib/esm/prompt/prompts/afs-builtin-prompt.d.ts +1 -0
- package/lib/esm/prompt/prompts/afs-builtin-prompt.js +6 -3
- package/lib/esm/prompt/skills/afs.js +7 -0
- package/lib/esm/utils/event-stream.js +2 -1
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,48 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.71.0-beta.4](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.71.0-beta.3...core-v1.71.0-beta.4) (2025-12-10)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* bump version ([af04b69](https://github.com/AIGNE-io/aigne-framework/commit/af04b6931951afa35d52065430acc7fef4b10087))
|
|
9
|
+
* **core:** support load third agent in skills ([#819](https://github.com/AIGNE-io/aigne-framework/issues/819)) ([bcbb140](https://github.com/AIGNE-io/aigne-framework/commit/bcbb1404d2fe9c709d99a8c28883b21dd107a844))
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
### Dependencies
|
|
13
|
+
|
|
14
|
+
* The following workspace dependencies were updated
|
|
15
|
+
* dependencies
|
|
16
|
+
* @aigne/afs bumped to 1.3.0-beta.2
|
|
17
|
+
* @aigne/afs-history bumped to 1.1.3-beta.2
|
|
18
|
+
* @aigne/observability-api bumped to 0.11.13-beta.1
|
|
19
|
+
* @aigne/platform-helpers bumped to 0.6.6-beta
|
|
20
|
+
|
|
21
|
+
## [1.71.0-beta.3](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.71.0-beta.2...core-v1.71.0-beta.3) (2025-12-10)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
### Bug Fixes
|
|
25
|
+
|
|
26
|
+
* **afs:** add case-sensitive option for search with case-insensitive default ([#814](https://github.com/AIGNE-io/aigne-framework/issues/814)) ([9dc9446](https://github.com/AIGNE-io/aigne-framework/commit/9dc944635104fc311e7756b4bde0a20275cfe8ec))
|
|
27
|
+
* **core:** disable Immer autofreeze to return mutable response objects ([#817](https://github.com/AIGNE-io/aigne-framework/issues/817)) ([a3d0651](https://github.com/AIGNE-io/aigne-framework/commit/a3d06512cdadb9d85f92b7e8d2fd85b4f35a804b))
|
|
28
|
+
* **observability:** improve trace shutdown handling and exit status ([#813](https://github.com/AIGNE-io/aigne-framework/issues/813)) ([6215f13](https://github.com/AIGNE-io/aigne-framework/commit/6215f13243b23103c1793a4559798f0e90722384))
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
### Dependencies
|
|
32
|
+
|
|
33
|
+
* The following workspace dependencies were updated
|
|
34
|
+
* dependencies
|
|
35
|
+
* @aigne/afs bumped to 1.3.0-beta.1
|
|
36
|
+
* @aigne/afs-history bumped to 1.1.3-beta.1
|
|
37
|
+
* @aigne/observability-api bumped to 0.11.13-beta
|
|
38
|
+
|
|
39
|
+
## [1.71.0-beta.2](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.71.0-beta.1...core-v1.71.0-beta.2) (2025-12-09)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
### Bug Fixes
|
|
43
|
+
|
|
44
|
+
* **orchestrator:** enhance prompts with detailed guidance ([#811](https://github.com/AIGNE-io/aigne-framework/issues/811)) ([5656f38](https://github.com/AIGNE-io/aigne-framework/commit/5656f38c09e458e18b90e962a5e85c96755be2e4))
|
|
45
|
+
|
|
3
46
|
## [1.71.0-beta.1](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.71.0-beta...core-v1.71.0-beta.1) (2025-12-08)
|
|
4
47
|
|
|
5
48
|
|
|
@@ -118,6 +118,7 @@ export interface AgentOptions<I extends Message = Message, O extends Message = M
|
|
|
118
118
|
* agentSucceed, or agentFailed
|
|
119
119
|
*/
|
|
120
120
|
disableEvents?: boolean;
|
|
121
|
+
historyConfig?: Agent["historyConfig"];
|
|
121
122
|
/**
|
|
122
123
|
* One or more memory agents this agent can use
|
|
123
124
|
*/
|
|
@@ -346,6 +347,11 @@ export declare abstract class Agent<I extends Message = any, O extends Message =
|
|
|
346
347
|
* agentSucceed, or agentFailed
|
|
347
348
|
*/
|
|
348
349
|
private disableEvents?;
|
|
350
|
+
historyConfig?: {
|
|
351
|
+
disabled?: boolean;
|
|
352
|
+
maxTokens?: number;
|
|
353
|
+
maxItems?: number;
|
|
354
|
+
};
|
|
349
355
|
private subscriptions;
|
|
350
356
|
/**
|
|
351
357
|
* Attach agent to context:
|
package/lib/cjs/agents/agent.js
CHANGED
|
@@ -151,6 +151,7 @@ class Agent {
|
|
|
151
151
|
if (options.skills?.length)
|
|
152
152
|
this.skills.push(...options.skills.map(functionToAgent));
|
|
153
153
|
this.disableEvents = options.disableEvents;
|
|
154
|
+
this.historyConfig = options.historyConfig;
|
|
154
155
|
if (Array.isArray(options.memory)) {
|
|
155
156
|
this.memories.push(...options.memory);
|
|
156
157
|
}
|
|
@@ -324,6 +325,7 @@ class Agent {
|
|
|
324
325
|
* agentSucceed, or agentFailed
|
|
325
326
|
*/
|
|
326
327
|
disableEvents;
|
|
328
|
+
historyConfig;
|
|
327
329
|
subscriptions = [];
|
|
328
330
|
/**
|
|
329
331
|
* Attach agent to context:
|
|
@@ -567,7 +569,8 @@ class Agent {
|
|
|
567
569
|
const o = await this.callHooks(["onSuccess", "onEnd"], { input, output: finalOutput }, options);
|
|
568
570
|
if (o?.output)
|
|
569
571
|
finalOutput = o.output;
|
|
570
|
-
this.
|
|
572
|
+
if (this.historyConfig?.disabled !== true)
|
|
573
|
+
this.afs?.emit("agentSucceed", { input, output: finalOutput });
|
|
571
574
|
if (!this.disableEvents)
|
|
572
575
|
context.emit("agentSucceed", { agent: this, output: finalOutput });
|
|
573
576
|
return finalOutput;
|
package/lib/cjs/aigne/aigne.d.ts
CHANGED
|
@@ -113,6 +113,10 @@ export declare class AIGNE<U extends UserContext = UserContext> {
|
|
|
113
113
|
* @hidden
|
|
114
114
|
*/
|
|
115
115
|
readonly messageQueue: MessageQueue;
|
|
116
|
+
/**
|
|
117
|
+
* Collection of all context IDs created by this AIGNE instance.
|
|
118
|
+
*/
|
|
119
|
+
readonly contextIds: Set<string>;
|
|
116
120
|
/**
|
|
117
121
|
* Collection of skill agents available to this AIGNE instance.
|
|
118
122
|
* Provides indexed access by skill name.
|
|
@@ -324,7 +328,8 @@ export declare class AIGNE<U extends UserContext = UserContext> {
|
|
|
324
328
|
[Symbol.asyncDispose](): Promise<void>;
|
|
325
329
|
/**
|
|
326
330
|
* Initializes handlers for process exit events to ensure clean shutdown.
|
|
327
|
-
* This registers handlers for SIGINT
|
|
331
|
+
* This registers handlers for SIGINT/SIGTERM to properly terminate all agents.
|
|
332
|
+
* Note: 'exit' event cannot run async code, so we handle cleanup in signal handlers.
|
|
328
333
|
*/
|
|
329
334
|
private initProcessExitHandler;
|
|
330
335
|
}
|
package/lib/cjs/aigne/aigne.js
CHANGED
|
@@ -99,6 +99,10 @@ class AIGNE {
|
|
|
99
99
|
* @hidden
|
|
100
100
|
*/
|
|
101
101
|
messageQueue = new message_queue_js_1.MessageQueue();
|
|
102
|
+
/**
|
|
103
|
+
* Collection of all context IDs created by this AIGNE instance.
|
|
104
|
+
*/
|
|
105
|
+
contextIds = new Set();
|
|
102
106
|
/**
|
|
103
107
|
* Collection of skill agents available to this AIGNE instance.
|
|
104
108
|
* Provides indexed access by skill name.
|
|
@@ -200,7 +204,7 @@ class AIGNE {
|
|
|
200
204
|
*/
|
|
201
205
|
async shutdown() {
|
|
202
206
|
// Close observer first to flush any pending traces
|
|
203
|
-
await this.observer?.close();
|
|
207
|
+
await this.observer?.close(Array.from(this.contextIds));
|
|
204
208
|
for (const tool of this.skills) {
|
|
205
209
|
await tool.shutdown();
|
|
206
210
|
}
|
|
@@ -220,12 +224,17 @@ class AIGNE {
|
|
|
220
224
|
}
|
|
221
225
|
/**
|
|
222
226
|
* Initializes handlers for process exit events to ensure clean shutdown.
|
|
223
|
-
* This registers handlers for SIGINT
|
|
227
|
+
* This registers handlers for SIGINT/SIGTERM to properly terminate all agents.
|
|
228
|
+
* Note: 'exit' event cannot run async code, so we handle cleanup in signal handlers.
|
|
224
229
|
*/
|
|
225
230
|
initProcessExitHandler() {
|
|
226
|
-
const
|
|
231
|
+
const originalExit = process.exit;
|
|
232
|
+
// @ts-ignore
|
|
233
|
+
process.exit = (...args) => {
|
|
234
|
+
this.shutdown().finally(() => originalExit(...args));
|
|
235
|
+
};
|
|
236
|
+
const shutdownAndExit = () => this.shutdown().finally(() => originalExit(0));
|
|
227
237
|
process.on("SIGINT", shutdownAndExit);
|
|
228
|
-
process.on("exit", shutdownAndExit);
|
|
229
238
|
}
|
|
230
239
|
}
|
|
231
240
|
exports.AIGNE = AIGNE;
|
|
@@ -149,7 +149,6 @@ export interface Context<U extends UserContext = UserContext> extends TypedEvent
|
|
|
149
149
|
* @hidden
|
|
150
150
|
*/
|
|
151
151
|
export declare class AIGNEContext implements Context {
|
|
152
|
-
static exitCount: number;
|
|
153
152
|
constructor(parent?: ConstructorParameters<typeof AIGNEContextShared>[0], { reset }?: {
|
|
154
153
|
reset?: boolean;
|
|
155
154
|
});
|
|
@@ -183,7 +182,6 @@ export declare class AIGNEContext implements Context {
|
|
|
183
182
|
subscribe: Context["subscribe"];
|
|
184
183
|
unsubscribe: Context["unsubscribe"];
|
|
185
184
|
emit<K extends keyof ContextEmitEventMap>(eventName: K, ...args: Args<K, ContextEmitEventMap>): boolean;
|
|
186
|
-
initProcessExitHandler(): void;
|
|
187
185
|
private trace;
|
|
188
186
|
on<K extends keyof ContextEventMap>(eventName: K, listener: Listener<K, ContextEventMap>): this;
|
|
189
187
|
once<K extends keyof ContextEventMap>(eventName: K, listener: Listener<K, ContextEventMap>): this;
|
|
@@ -194,6 +192,7 @@ declare class AIGNEContextShared {
|
|
|
194
192
|
constructor(parent?: (Pick<Context, "model" | "imageModel" | "agents" | "skills" | "limits" | "observer" | "metadata"> & {
|
|
195
193
|
messageQueue?: MessageQueue;
|
|
196
194
|
events?: Emitter<any>;
|
|
195
|
+
contextIds?: Set<string>;
|
|
197
196
|
}) | undefined);
|
|
198
197
|
readonly messageQueue: MessageQueue;
|
|
199
198
|
readonly events: Emitter<any>;
|
|
@@ -205,6 +204,7 @@ declare class AIGNEContextShared {
|
|
|
205
204
|
get metadata(): AIGNEMetadata | undefined;
|
|
206
205
|
get limits(): ContextLimits | undefined;
|
|
207
206
|
usage: ContextUsage;
|
|
207
|
+
contextIds: Set<string>;
|
|
208
208
|
userContext: Context["userContext"];
|
|
209
209
|
memories: Context["memories"];
|
|
210
210
|
hooks: AgentHooks[];
|
package/lib/cjs/aigne/context.js
CHANGED
|
@@ -23,7 +23,6 @@ const usage_js_1 = require("./usage.js");
|
|
|
23
23
|
* @hidden
|
|
24
24
|
*/
|
|
25
25
|
class AIGNEContext {
|
|
26
|
-
static exitCount = 0;
|
|
27
26
|
constructor(parent, { reset } = {}) {
|
|
28
27
|
const tracer = parent?.observer?.tracer;
|
|
29
28
|
if (parent instanceof AIGNEContext && !reset) {
|
|
@@ -47,7 +46,7 @@ class AIGNEContext {
|
|
|
47
46
|
this.rootId = this.span?.spanContext?.().traceId ?? (0, uuid_1.v7)();
|
|
48
47
|
}
|
|
49
48
|
this.id = this.span?.spanContext()?.spanId ?? (0, uuid_1.v7)();
|
|
50
|
-
this.
|
|
49
|
+
this.internal.contextIds.add(this.id);
|
|
51
50
|
}
|
|
52
51
|
id;
|
|
53
52
|
parentId;
|
|
@@ -203,26 +202,6 @@ class AIGNEContext {
|
|
|
203
202
|
this.trace(eventName, args, b);
|
|
204
203
|
return this.internal.events.emit(eventName, ...newArgs);
|
|
205
204
|
}
|
|
206
|
-
initProcessExitHandler() {
|
|
207
|
-
if (this.parentId)
|
|
208
|
-
return;
|
|
209
|
-
AIGNEContext.exitCount++;
|
|
210
|
-
process.once("SIGINT", async () => {
|
|
211
|
-
try {
|
|
212
|
-
if (process.env.AIGNE_OBSERVABILITY_DISABLED)
|
|
213
|
-
return;
|
|
214
|
-
await this.observer?.update(this.id, {
|
|
215
|
-
status: { code: api_1.SpanStatusCode.ERROR, message: "SIGINT" },
|
|
216
|
-
});
|
|
217
|
-
}
|
|
218
|
-
finally {
|
|
219
|
-
AIGNEContext.exitCount--;
|
|
220
|
-
if (AIGNEContext.exitCount === 0) {
|
|
221
|
-
process.exit(0);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
205
|
async trace(eventName, args, b) {
|
|
227
206
|
if (process.env.AIGNE_OBSERVABILITY_DISABLED)
|
|
228
207
|
return;
|
|
@@ -258,12 +237,14 @@ class AIGNEContext {
|
|
|
258
237
|
await this.observer?.update(this.id, { output });
|
|
259
238
|
span.setStatus({ code: api_1.SpanStatusCode.OK });
|
|
260
239
|
span.end();
|
|
240
|
+
this.internal.contextIds.delete(this.id);
|
|
261
241
|
break;
|
|
262
242
|
}
|
|
263
243
|
case "agentFailed": {
|
|
264
244
|
const { error } = args[0];
|
|
265
245
|
span.setStatus({ code: api_1.SpanStatusCode.ERROR, message: error.message });
|
|
266
246
|
span.end();
|
|
247
|
+
this.internal.contextIds.delete(this.id);
|
|
267
248
|
break;
|
|
268
249
|
}
|
|
269
250
|
}
|
|
@@ -292,6 +273,7 @@ class AIGNEContextShared {
|
|
|
292
273
|
this.parent = parent;
|
|
293
274
|
this.messageQueue = this.parent?.messageQueue ?? new message_queue_js_1.MessageQueue();
|
|
294
275
|
this.events = this.parent?.events ?? new strict_event_emitter_1.Emitter();
|
|
276
|
+
this.contextIds = this.parent?.contextIds ?? new Set();
|
|
295
277
|
}
|
|
296
278
|
messageQueue;
|
|
297
279
|
events;
|
|
@@ -317,6 +299,7 @@ class AIGNEContextShared {
|
|
|
317
299
|
return this.parent?.limits;
|
|
318
300
|
}
|
|
319
301
|
usage = (0, usage_js_1.newEmptyContextUsage)();
|
|
302
|
+
contextIds;
|
|
320
303
|
userContext = {};
|
|
321
304
|
memories = [];
|
|
322
305
|
hooks = [];
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import type { LoadOptions } from "./index.js";
|
|
2
|
+
export declare function loadAgentFromJsFile(path: string, options: LoadOptions): Promise<import("../index.js").Agent<any, any> | import("./agent-yaml.js").AgentSchema>;
|
|
@@ -7,7 +7,7 @@ const type_utils_js_1 = require("../utils/type-utils.js");
|
|
|
7
7
|
const agent_yaml_js_1 = require("./agent-yaml.js");
|
|
8
8
|
const error_js_1 = require("./error.js");
|
|
9
9
|
const importFn = new Function("path", "return import(path)");
|
|
10
|
-
async function loadAgentFromJsFile(path) {
|
|
10
|
+
async function loadAgentFromJsFile(path, options) {
|
|
11
11
|
const url = index_js_1.nodejs.path.isAbsolute(path) ? index_js_1.nodejs.url.pathToFileURL(path).toString() : path;
|
|
12
12
|
const { default: agent } = await (0, type_utils_js_1.tryOrThrow)(() => importFn(url), (error) => new error_js_1.LoadJsAgentError(`Failed to load agent definition from ${url}: ${error.message}`));
|
|
13
13
|
if ((0, agent_utils_js_1.isAgent)(agent))
|
|
@@ -17,5 +17,5 @@ async function loadAgentFromJsFile(path) {
|
|
|
17
17
|
process: agent,
|
|
18
18
|
name: agent.agent_name || agent.agentName || agent.name,
|
|
19
19
|
...agent,
|
|
20
|
-
}), (error) => new Error(`Failed to parse agent from ${path}: ${error.message}`));
|
|
20
|
+
}, options), (error) => new Error(`Failed to parse agent from ${path}: ${error.message}`));
|
|
21
21
|
}
|
|
@@ -101,13 +101,14 @@ export interface ThirdAgentSchema extends BaseAgentSchema {
|
|
|
101
101
|
[key: string]: any;
|
|
102
102
|
}
|
|
103
103
|
export type AgentSchema = AIAgentSchema | ImageAgentSchema | MCPAgentSchema | TeamAgentSchema | TransformAgentSchema | FunctionAgentSchema | ThirdAgentSchema;
|
|
104
|
-
export declare function parseAgentFile(path: string, data: any): Promise<AgentSchema>;
|
|
105
|
-
export declare function loadAgentFromYamlFile(path: string, options
|
|
104
|
+
export declare function parseAgentFile(path: string, data: any, options: LoadOptions): Promise<AgentSchema>;
|
|
105
|
+
export declare function loadAgentFromYamlFile(path: string, options: LoadOptions): Promise<AgentSchema>;
|
|
106
106
|
export declare const getInstructionsSchema: ({ filepath }: {
|
|
107
107
|
filepath: string;
|
|
108
108
|
}) => ZodType<Instructions>;
|
|
109
|
-
export declare const getAgentSchema: ({ filepath }: {
|
|
109
|
+
export declare const getAgentSchema: ({ filepath, options, }: {
|
|
110
110
|
filepath: string;
|
|
111
|
+
options?: LoadOptions;
|
|
111
112
|
}) => ZodType<AgentSchema, z.ZodTypeDef, AgentSchema>;
|
|
112
113
|
export declare const getNestAgentSchema: ({ filepath, }: {
|
|
113
114
|
filepath: string;
|
|
@@ -13,8 +13,8 @@ const team_agent_js_1 = require("../agents/team-agent.js");
|
|
|
13
13
|
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
14
14
|
const function_agent_js_1 = require("./function-agent.js");
|
|
15
15
|
const schema_js_1 = require("./schema.js");
|
|
16
|
-
async function parseAgentFile(path, data) {
|
|
17
|
-
const agentSchema = (0, exports.getAgentSchema)({ filepath: path });
|
|
16
|
+
async function parseAgentFile(path, data, options) {
|
|
17
|
+
const agentSchema = (0, exports.getAgentSchema)({ filepath: path, options });
|
|
18
18
|
return agentSchema.parseAsync({
|
|
19
19
|
...data,
|
|
20
20
|
model: data.model || data.chatModel || data.chat_model,
|
|
@@ -23,22 +23,11 @@ async function parseAgentFile(path, data) {
|
|
|
23
23
|
async function loadAgentFromYamlFile(path, options) {
|
|
24
24
|
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}`));
|
|
25
25
|
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}`));
|
|
26
|
-
if (!["ai", "image", "mcp", "team", "transform", "function"].includes(json?.type)) {
|
|
27
|
-
if (typeof json?.type === "string") {
|
|
28
|
-
if (!options?.require)
|
|
29
|
-
throw new Error(`Module loader is not provided to load agent type module ${json.type} from ${path}`);
|
|
30
|
-
const Mod = await options.require(json.type, { parent: path });
|
|
31
|
-
if (typeof Mod?.default?.prototype?.constructor !== "function") {
|
|
32
|
-
throw new Error(`The agent type module ${json.type} does not export a default Agent class`);
|
|
33
|
-
}
|
|
34
|
-
json.agentClass = Mod.default;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
26
|
const agent = await (0, type_utils_js_1.tryOrThrow)(async () => await parseAgentFile(path, {
|
|
38
27
|
...json,
|
|
39
28
|
type: json.type ?? "ai",
|
|
40
29
|
skills: json.skills ?? json.tools,
|
|
41
|
-
}), (error) => new Error(`Failed to validate agent definition from ${path}: ${error.message}`));
|
|
30
|
+
}, options), (error) => new Error(`Failed to validate agent definition from ${path}: ${error.message}`));
|
|
42
31
|
return agent;
|
|
43
32
|
}
|
|
44
33
|
const instructionItemSchema = zod_1.z.union([
|
|
@@ -76,7 +65,7 @@ const getInstructionsSchema = ({ filepath }) => zod_1.z
|
|
|
76
65
|
return [await parseInstructionItem({ filepath })(v)];
|
|
77
66
|
});
|
|
78
67
|
exports.getInstructionsSchema = getInstructionsSchema;
|
|
79
|
-
const getAgentSchema = ({ filepath }) => {
|
|
68
|
+
const getAgentSchema = ({ filepath, options, }) => {
|
|
80
69
|
const agentSchema = zod_1.z.lazy(() => {
|
|
81
70
|
const nestAgentSchema = zod_1.z.lazy(() => zod_1.z.union([
|
|
82
71
|
agentSchema,
|
|
@@ -133,7 +122,22 @@ const getAgentSchema = ({ filepath }) => {
|
|
|
133
122
|
shareAFS: (0, schema_js_1.optionalize)(zod_1.z.boolean()),
|
|
134
123
|
});
|
|
135
124
|
const instructionsSchema = (0, exports.getInstructionsSchema)({ filepath: filepath });
|
|
136
|
-
return (0, schema_js_1.camelizeSchema)(
|
|
125
|
+
return (0, schema_js_1.camelizeSchema)((0, schema_js_1.preprocessSchema)(async (json) => {
|
|
126
|
+
if (typeof json === "object" &&
|
|
127
|
+
json &&
|
|
128
|
+
"type" in json &&
|
|
129
|
+
typeof json.type === "string" &&
|
|
130
|
+
!["ai", "image", "mcp", "team", "transform", "function"].includes(json.type)) {
|
|
131
|
+
if (!options?.require)
|
|
132
|
+
throw new Error(`Module loader is not provided to load agent type module ${json.type} from ${filepath}`);
|
|
133
|
+
const Mod = await options.require(json.type, { parent: filepath });
|
|
134
|
+
if (typeof Mod?.default?.prototype?.constructor !== "function") {
|
|
135
|
+
throw new Error(`The agent type module ${json.type} does not export a default Agent class`);
|
|
136
|
+
}
|
|
137
|
+
Object.assign(json, { agentClass: Mod.default });
|
|
138
|
+
}
|
|
139
|
+
return json;
|
|
140
|
+
}, zod_1.z.union([
|
|
137
141
|
zod_1.z
|
|
138
142
|
.object({
|
|
139
143
|
type: zod_1.z.string(),
|
|
@@ -204,7 +208,7 @@ const getAgentSchema = ({ filepath }) => {
|
|
|
204
208
|
})
|
|
205
209
|
.extend(baseAgentSchema.shape),
|
|
206
210
|
]),
|
|
207
|
-
]));
|
|
211
|
+
])));
|
|
208
212
|
});
|
|
209
213
|
return agentSchema;
|
|
210
214
|
};
|
|
@@ -28,9 +28,9 @@ export interface LoadOptions {
|
|
|
28
28
|
}) => Promise<any>;
|
|
29
29
|
}
|
|
30
30
|
export declare function load(path: string, options?: LoadOptions): Promise<AIGNEOptions>;
|
|
31
|
-
export declare function loadAgent(path: string, options
|
|
32
|
-
export declare function loadNestAgent(path: string, agent: NestAgentSchema, options
|
|
33
|
-
export declare function parseAgent(path: string, agent: Awaited<ReturnType<typeof loadAgentFromYamlFile>>, options
|
|
31
|
+
export declare function loadAgent(path: string, options: LoadOptions, agentOptions?: AgentOptions): Promise<Agent>;
|
|
32
|
+
export declare function loadNestAgent(path: string, agent: NestAgentSchema, options: LoadOptions, agentOptions?: AgentOptions<any, any> & Record<string, unknown>): Promise<Agent>;
|
|
33
|
+
export declare function parseAgent(path: string, agent: Awaited<ReturnType<typeof loadAgentFromYamlFile>>, options: LoadOptions, agentOptions?: AgentOptions): Promise<Agent>;
|
|
34
34
|
type CliAgent = string | {
|
|
35
35
|
url?: string;
|
|
36
36
|
name?: string;
|
package/lib/cjs/loader/index.js
CHANGED
|
@@ -70,7 +70,7 @@ async function load(path, options = {}) {
|
|
|
70
70
|
}
|
|
71
71
|
async function loadAgent(path, options, agentOptions) {
|
|
72
72
|
if ([".js", ".mjs", ".ts", ".mts"].includes(index_js_1.nodejs.path.extname(path))) {
|
|
73
|
-
const agent = await (0, agent_js_js_1.loadAgentFromJsFile)(path);
|
|
73
|
+
const agent = await (0, agent_js_js_1.loadAgentFromJsFile)(path, options);
|
|
74
74
|
if (agent instanceof agent_js_1.Agent)
|
|
75
75
|
return agent;
|
|
76
76
|
return parseAgent(path, agent, options, agentOptions);
|
|
@@ -148,4 +148,5 @@ export declare function optionalize<T>(schema: ZodType<T>): ZodType<T | undefine
|
|
|
148
148
|
export declare function camelizeSchema<T extends ZodType>(schema: T, { shallow }?: {
|
|
149
149
|
shallow?: boolean;
|
|
150
150
|
}): T;
|
|
151
|
+
export declare function preprocessSchema<T extends ZodType>(fn: (data: unknown) => unknown, schema: T): T;
|
|
151
152
|
export {};
|
package/lib/cjs/loader/schema.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.imageModelSchema = exports.chatModelSchema = exports.defaultInputSchema = exports.inputOutputSchema = void 0;
|
|
4
4
|
exports.optionalize = optionalize;
|
|
5
5
|
exports.camelizeSchema = camelizeSchema;
|
|
6
|
+
exports.preprocessSchema = preprocessSchema;
|
|
6
7
|
const index_js_1 = require("@aigne/platform-helpers/nodejs/index.js");
|
|
7
8
|
const yaml_1 = require("yaml");
|
|
8
9
|
const zod_1 = require("zod");
|
|
@@ -97,3 +98,6 @@ function optionalize(schema) {
|
|
|
97
98
|
function camelizeSchema(schema, { shallow = true } = {}) {
|
|
98
99
|
return zod_1.z.preprocess((v) => ((0, type_utils_js_1.isRecord)(v) ? (0, camelize_js_1.camelize)(v, shallow) : v), schema);
|
|
99
100
|
}
|
|
101
|
+
function preprocessSchema(fn, schema) {
|
|
102
|
+
return zod_1.z.preprocess(fn, schema);
|
|
103
|
+
}
|
|
@@ -37,13 +37,15 @@ exports.setupFilters = setupFilters;
|
|
|
37
37
|
const tson = __importStar(require("@zenoaihq/tson"));
|
|
38
38
|
const yaml_1 = require("yaml");
|
|
39
39
|
function setupFilters(env) {
|
|
40
|
-
env.addFilter("yaml.stringify", (obj) => {
|
|
41
|
-
|
|
42
|
-
});
|
|
43
|
-
env.addFilter("json.stringify", (obj, ...args) => {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
40
|
+
env.addFilter("yaml.stringify", async (obj, cb) => {
|
|
41
|
+
cb(null, (0, yaml_1.stringify)(await obj));
|
|
42
|
+
}, true);
|
|
43
|
+
env.addFilter("json.stringify", async (obj, ...args) => {
|
|
44
|
+
const jsonArgs = args.slice(0, -1);
|
|
45
|
+
const cb = args[args.length - 1];
|
|
46
|
+
cb(null, JSON.stringify(await obj, ...jsonArgs));
|
|
47
|
+
}, true);
|
|
48
|
+
env.addFilter("tson.stringify", async (obj, cb) => {
|
|
49
|
+
cb(null, tson.dumps(await obj));
|
|
50
|
+
}, true);
|
|
49
51
|
}
|
|
@@ -37,6 +37,10 @@ export declare class PromptBuilder {
|
|
|
37
37
|
}>;
|
|
38
38
|
private getTemplateVariables;
|
|
39
39
|
private buildMessages;
|
|
40
|
+
getHistories(options: PromptBuildOptions): Promise<{
|
|
41
|
+
role: "user" | "agent";
|
|
42
|
+
content: unknown;
|
|
43
|
+
}[]>;
|
|
40
44
|
private refineMessages;
|
|
41
45
|
private convertMemoriesToMessages;
|
|
42
46
|
private buildResponseFormat;
|
|
@@ -95,10 +95,36 @@ class PromptBuilder {
|
|
|
95
95
|
};
|
|
96
96
|
}
|
|
97
97
|
getTemplateVariables(options) {
|
|
98
|
+
const self = this;
|
|
98
99
|
return {
|
|
99
100
|
userContext: options.context?.userContext,
|
|
100
101
|
...options.context?.userContext,
|
|
101
102
|
...options.input,
|
|
103
|
+
$afs: {
|
|
104
|
+
get enabled() {
|
|
105
|
+
return !!options.agent?.afs;
|
|
106
|
+
},
|
|
107
|
+
description: afs_builtin_prompt_js_1.AFS_DESCRIPTION_PROMPT_TEMPLATE,
|
|
108
|
+
get modules() {
|
|
109
|
+
return options.agent?.afs
|
|
110
|
+
?.listModules()
|
|
111
|
+
.then((list) => list.map((i) => (0, type_utils_js_1.pick)(i, ["name", "path", "description"])));
|
|
112
|
+
},
|
|
113
|
+
get histories() {
|
|
114
|
+
return self.getHistories(options);
|
|
115
|
+
},
|
|
116
|
+
get skills() {
|
|
117
|
+
const afs = options.agent?.afs;
|
|
118
|
+
if (!afs)
|
|
119
|
+
return [];
|
|
120
|
+
return (0, afs_js_1.getAFSSkills)(afs).then((skills) => skills.map((s) => (0, type_utils_js_1.pick)(s, ["name", "description"])));
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
$agent: {
|
|
124
|
+
get skills() {
|
|
125
|
+
return options.agent?.skills.map((s) => (0, type_utils_js_1.pick)(s, ["name", "description"]));
|
|
126
|
+
},
|
|
127
|
+
},
|
|
102
128
|
};
|
|
103
129
|
}
|
|
104
130
|
async buildMessages(options) {
|
|
@@ -120,7 +146,7 @@ class PromptBuilder {
|
|
|
120
146
|
memories.push(...options.context.memories);
|
|
121
147
|
}
|
|
122
148
|
const afs = options.agent?.afs;
|
|
123
|
-
if (afs) {
|
|
149
|
+
if (afs && options.agent?.historyConfig?.disabled !== true) {
|
|
124
150
|
const historyModule = (await afs.listModules()).find((m) => m.module instanceof afs_history_1.AFSHistory);
|
|
125
151
|
messages.push(await template_js_1.SystemMessageTemplate.from(await (0, afs_builtin_prompt_js_1.getAFSSystemPrompt)(afs)).format({}));
|
|
126
152
|
if (historyModule) {
|
|
@@ -198,6 +224,34 @@ class PromptBuilder {
|
|
|
198
224
|
messages.push(...otherCustomMessages);
|
|
199
225
|
return this.refineMessages(options, messages);
|
|
200
226
|
}
|
|
227
|
+
async getHistories(options) {
|
|
228
|
+
const { agent } = options;
|
|
229
|
+
const afs = agent?.afs;
|
|
230
|
+
if (!afs)
|
|
231
|
+
return [];
|
|
232
|
+
const historyModule = (await afs.listModules()).find((m) => m.module instanceof afs_history_1.AFSHistory);
|
|
233
|
+
if (!historyModule)
|
|
234
|
+
return [];
|
|
235
|
+
const { list: history } = await afs.list(historyModule.path, {
|
|
236
|
+
limit: agent.historyConfig?.maxItems || 10,
|
|
237
|
+
orderBy: [["createdAt", "desc"]],
|
|
238
|
+
});
|
|
239
|
+
return history
|
|
240
|
+
.reverse()
|
|
241
|
+
.map((i) => {
|
|
242
|
+
if (!i.content)
|
|
243
|
+
return;
|
|
244
|
+
const { input, output } = i.content;
|
|
245
|
+
if (!input || !output)
|
|
246
|
+
return;
|
|
247
|
+
return [
|
|
248
|
+
{ role: "user", content: input },
|
|
249
|
+
{ role: "agent", content: output },
|
|
250
|
+
];
|
|
251
|
+
})
|
|
252
|
+
.filter(type_utils_js_1.isNonNullable)
|
|
253
|
+
.flat();
|
|
254
|
+
}
|
|
201
255
|
refineMessages(options, messages) {
|
|
202
256
|
const { autoReorderSystemMessages, autoMergeSystemMessages } = options.agent ?? {};
|
|
203
257
|
if (!autoReorderSystemMessages && !autoMergeSystemMessages)
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import type { AFS } from "@aigne/afs";
|
|
2
2
|
export declare function getAFSSystemPrompt(afs: AFS): Promise<string>;
|
|
3
3
|
export declare const AFS_EXECUTABLE_TOOLS_PROMPT_TEMPLATE = "<afs_executable_tools>\nHere are the executable tools available in the AFS you can use:\n\n{{ tools | yaml.stringify }}\n</afs_executable_tools>\n";
|
|
4
|
+
export declare const AFS_DESCRIPTION_PROMPT_TEMPLATE = "AFS (Agentic File System) provides tools to interact with a virtual file system,\nallowing you to list, search, read, and write files, or execute a useful tool from the available modules.\nYou can use these tools to manage and retrieve files as needed.\n";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AFS_EXECUTABLE_TOOLS_PROMPT_TEMPLATE = void 0;
|
|
3
|
+
exports.AFS_DESCRIPTION_PROMPT_TEMPLATE = exports.AFS_EXECUTABLE_TOOLS_PROMPT_TEMPLATE = void 0;
|
|
4
4
|
exports.getAFSSystemPrompt = getAFSSystemPrompt;
|
|
5
5
|
const yaml_1 = require("yaml");
|
|
6
6
|
const type_utils_js_1 = require("../../utils/type-utils.js");
|
|
@@ -8,9 +8,7 @@ async function getAFSSystemPrompt(afs) {
|
|
|
8
8
|
return `\
|
|
9
9
|
|
|
10
10
|
<afs_usage>
|
|
11
|
-
|
|
12
|
-
allowing you to list, search, read, and write files, or execute a useful tool from the available modules.
|
|
13
|
-
Use these tools to manage and retrieve files as needed.
|
|
11
|
+
${exports.AFS_DESCRIPTION_PROMPT_TEMPLATE}
|
|
14
12
|
|
|
15
13
|
Provided modules:
|
|
16
14
|
${(0, yaml_1.stringify)((await afs.listModules()).map((i) => (0, type_utils_js_1.pick)(i, ["name", "path", "description"])))}
|
|
@@ -31,3 +29,8 @@ Here are the executable tools available in the AFS you can use:
|
|
|
31
29
|
{{ tools | yaml.stringify }}
|
|
32
30
|
</afs_executable_tools>
|
|
33
31
|
`;
|
|
32
|
+
exports.AFS_DESCRIPTION_PROMPT_TEMPLATE = `\
|
|
33
|
+
AFS (Agentic File System) provides tools to interact with a virtual file system,
|
|
34
|
+
allowing you to list, search, read, and write files, or execute a useful tool from the available modules.
|
|
35
|
+
You can use these tools to manage and retrieve files as needed.
|
|
36
|
+
`;
|
|
@@ -22,6 +22,7 @@ async function getAFSSkills(afs) {
|
|
|
22
22
|
return {
|
|
23
23
|
status: "success",
|
|
24
24
|
tool: "afs_list",
|
|
25
|
+
path: input.path,
|
|
25
26
|
options: input.options,
|
|
26
27
|
message,
|
|
27
28
|
result,
|
|
@@ -39,6 +40,10 @@ async function getAFSSkills(afs) {
|
|
|
39
40
|
options: zod_1.z
|
|
40
41
|
.object({
|
|
41
42
|
limit: zod_1.z.number().optional().describe("Maximum number of entries to return"),
|
|
43
|
+
caseSensitive: zod_1.z
|
|
44
|
+
.boolean()
|
|
45
|
+
.optional()
|
|
46
|
+
.describe("Whether the search is case sensitive, default is false"),
|
|
42
47
|
})
|
|
43
48
|
.optional(),
|
|
44
49
|
}),
|
|
@@ -47,6 +52,7 @@ async function getAFSSkills(afs) {
|
|
|
47
52
|
return {
|
|
48
53
|
status: "success",
|
|
49
54
|
tool: "afs_search",
|
|
55
|
+
path: input.path,
|
|
50
56
|
query: input.query,
|
|
51
57
|
options: input.options,
|
|
52
58
|
...result,
|
|
@@ -83,6 +89,7 @@ Usage:
|
|
|
83
89
|
status: "success",
|
|
84
90
|
tool: "afs_read",
|
|
85
91
|
path: input.path,
|
|
92
|
+
withLineNumbers: input.withLineNumbers,
|
|
86
93
|
...result,
|
|
87
94
|
result: {
|
|
88
95
|
...result.result,
|
|
@@ -5,6 +5,7 @@ const eventsource_parser_1 = require("eventsource-parser");
|
|
|
5
5
|
const immer_1 = require("immer");
|
|
6
6
|
const agent_js_1 = require("../agents/agent.js");
|
|
7
7
|
const type_utils_js_1 = require("./type-utils.js");
|
|
8
|
+
(0, immer_1.setAutoFreeze)(false);
|
|
8
9
|
class EventStreamParser extends TransformStream {
|
|
9
10
|
constructor() {
|
|
10
11
|
let parser;
|
|
@@ -118,6 +118,7 @@ export interface AgentOptions<I extends Message = Message, O extends Message = M
|
|
|
118
118
|
* agentSucceed, or agentFailed
|
|
119
119
|
*/
|
|
120
120
|
disableEvents?: boolean;
|
|
121
|
+
historyConfig?: Agent["historyConfig"];
|
|
121
122
|
/**
|
|
122
123
|
* One or more memory agents this agent can use
|
|
123
124
|
*/
|
|
@@ -346,6 +347,11 @@ export declare abstract class Agent<I extends Message = any, O extends Message =
|
|
|
346
347
|
* agentSucceed, or agentFailed
|
|
347
348
|
*/
|
|
348
349
|
private disableEvents?;
|
|
350
|
+
historyConfig?: {
|
|
351
|
+
disabled?: boolean;
|
|
352
|
+
maxTokens?: number;
|
|
353
|
+
maxItems?: number;
|
|
354
|
+
};
|
|
349
355
|
private subscriptions;
|
|
350
356
|
/**
|
|
351
357
|
* Attach agent to context:
|