@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/lib/dts/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
|
}
|
|
@@ -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[];
|
|
@@ -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>;
|
|
@@ -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;
|
|
@@ -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;
|
|
@@ -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 {};
|
|
@@ -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;
|
|
@@ -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";
|
|
@@ -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/esm/agents/agent.js
CHANGED
|
@@ -103,6 +103,7 @@ export class Agent {
|
|
|
103
103
|
if (options.skills?.length)
|
|
104
104
|
this.skills.push(...options.skills.map(functionToAgent));
|
|
105
105
|
this.disableEvents = options.disableEvents;
|
|
106
|
+
this.historyConfig = options.historyConfig;
|
|
106
107
|
if (Array.isArray(options.memory)) {
|
|
107
108
|
this.memories.push(...options.memory);
|
|
108
109
|
}
|
|
@@ -276,6 +277,7 @@ export class Agent {
|
|
|
276
277
|
* agentSucceed, or agentFailed
|
|
277
278
|
*/
|
|
278
279
|
disableEvents;
|
|
280
|
+
historyConfig;
|
|
279
281
|
subscriptions = [];
|
|
280
282
|
/**
|
|
281
283
|
* Attach agent to context:
|
|
@@ -519,7 +521,8 @@ export class Agent {
|
|
|
519
521
|
const o = await this.callHooks(["onSuccess", "onEnd"], { input, output: finalOutput }, options);
|
|
520
522
|
if (o?.output)
|
|
521
523
|
finalOutput = o.output;
|
|
522
|
-
this.
|
|
524
|
+
if (this.historyConfig?.disabled !== true)
|
|
525
|
+
this.afs?.emit("agentSucceed", { input, output: finalOutput });
|
|
523
526
|
if (!this.disableEvents)
|
|
524
527
|
context.emit("agentSucceed", { agent: this, output: finalOutput });
|
|
525
528
|
return finalOutput;
|
package/lib/esm/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/esm/aigne/aigne.js
CHANGED
|
@@ -96,6 +96,10 @@ export class AIGNE {
|
|
|
96
96
|
* @hidden
|
|
97
97
|
*/
|
|
98
98
|
messageQueue = new MessageQueue();
|
|
99
|
+
/**
|
|
100
|
+
* Collection of all context IDs created by this AIGNE instance.
|
|
101
|
+
*/
|
|
102
|
+
contextIds = new Set();
|
|
99
103
|
/**
|
|
100
104
|
* Collection of skill agents available to this AIGNE instance.
|
|
101
105
|
* Provides indexed access by skill name.
|
|
@@ -197,7 +201,7 @@ export class AIGNE {
|
|
|
197
201
|
*/
|
|
198
202
|
async shutdown() {
|
|
199
203
|
// Close observer first to flush any pending traces
|
|
200
|
-
await this.observer?.close();
|
|
204
|
+
await this.observer?.close(Array.from(this.contextIds));
|
|
201
205
|
for (const tool of this.skills) {
|
|
202
206
|
await tool.shutdown();
|
|
203
207
|
}
|
|
@@ -217,12 +221,17 @@ export class AIGNE {
|
|
|
217
221
|
}
|
|
218
222
|
/**
|
|
219
223
|
* Initializes handlers for process exit events to ensure clean shutdown.
|
|
220
|
-
* This registers handlers for SIGINT
|
|
224
|
+
* This registers handlers for SIGINT/SIGTERM to properly terminate all agents.
|
|
225
|
+
* Note: 'exit' event cannot run async code, so we handle cleanup in signal handlers.
|
|
221
226
|
*/
|
|
222
227
|
initProcessExitHandler() {
|
|
223
|
-
const
|
|
228
|
+
const originalExit = process.exit;
|
|
229
|
+
// @ts-ignore
|
|
230
|
+
process.exit = (...args) => {
|
|
231
|
+
this.shutdown().finally(() => originalExit(...args));
|
|
232
|
+
};
|
|
233
|
+
const shutdownAndExit = () => this.shutdown().finally(() => originalExit(0));
|
|
224
234
|
process.on("SIGINT", shutdownAndExit);
|
|
225
|
-
process.on("exit", shutdownAndExit);
|
|
226
235
|
}
|
|
227
236
|
}
|
|
228
237
|
const aigneOptionsSchema = z.object({
|
|
@@ -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/esm/aigne/context.js
CHANGED
|
@@ -17,7 +17,6 @@ import { newEmptyContextUsage } from "./usage.js";
|
|
|
17
17
|
* @hidden
|
|
18
18
|
*/
|
|
19
19
|
export class AIGNEContext {
|
|
20
|
-
static exitCount = 0;
|
|
21
20
|
constructor(parent, { reset } = {}) {
|
|
22
21
|
const tracer = parent?.observer?.tracer;
|
|
23
22
|
if (parent instanceof AIGNEContext && !reset) {
|
|
@@ -41,7 +40,7 @@ export class AIGNEContext {
|
|
|
41
40
|
this.rootId = this.span?.spanContext?.().traceId ?? v7();
|
|
42
41
|
}
|
|
43
42
|
this.id = this.span?.spanContext()?.spanId ?? v7();
|
|
44
|
-
this.
|
|
43
|
+
this.internal.contextIds.add(this.id);
|
|
45
44
|
}
|
|
46
45
|
id;
|
|
47
46
|
parentId;
|
|
@@ -197,26 +196,6 @@ export class AIGNEContext {
|
|
|
197
196
|
this.trace(eventName, args, b);
|
|
198
197
|
return this.internal.events.emit(eventName, ...newArgs);
|
|
199
198
|
}
|
|
200
|
-
initProcessExitHandler() {
|
|
201
|
-
if (this.parentId)
|
|
202
|
-
return;
|
|
203
|
-
AIGNEContext.exitCount++;
|
|
204
|
-
process.once("SIGINT", async () => {
|
|
205
|
-
try {
|
|
206
|
-
if (process.env.AIGNE_OBSERVABILITY_DISABLED)
|
|
207
|
-
return;
|
|
208
|
-
await this.observer?.update(this.id, {
|
|
209
|
-
status: { code: SpanStatusCode.ERROR, message: "SIGINT" },
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
finally {
|
|
213
|
-
AIGNEContext.exitCount--;
|
|
214
|
-
if (AIGNEContext.exitCount === 0) {
|
|
215
|
-
process.exit(0);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
199
|
async trace(eventName, args, b) {
|
|
221
200
|
if (process.env.AIGNE_OBSERVABILITY_DISABLED)
|
|
222
201
|
return;
|
|
@@ -252,12 +231,14 @@ export class AIGNEContext {
|
|
|
252
231
|
await this.observer?.update(this.id, { output });
|
|
253
232
|
span.setStatus({ code: SpanStatusCode.OK });
|
|
254
233
|
span.end();
|
|
234
|
+
this.internal.contextIds.delete(this.id);
|
|
255
235
|
break;
|
|
256
236
|
}
|
|
257
237
|
case "agentFailed": {
|
|
258
238
|
const { error } = args[0];
|
|
259
239
|
span.setStatus({ code: SpanStatusCode.ERROR, message: error.message });
|
|
260
240
|
span.end();
|
|
241
|
+
this.internal.contextIds.delete(this.id);
|
|
261
242
|
break;
|
|
262
243
|
}
|
|
263
244
|
}
|
|
@@ -285,6 +266,7 @@ class AIGNEContextShared {
|
|
|
285
266
|
this.parent = parent;
|
|
286
267
|
this.messageQueue = this.parent?.messageQueue ?? new MessageQueue();
|
|
287
268
|
this.events = this.parent?.events ?? new Emitter();
|
|
269
|
+
this.contextIds = this.parent?.contextIds ?? new Set();
|
|
288
270
|
}
|
|
289
271
|
messageQueue;
|
|
290
272
|
events;
|
|
@@ -310,6 +292,7 @@ class AIGNEContextShared {
|
|
|
310
292
|
return this.parent?.limits;
|
|
311
293
|
}
|
|
312
294
|
usage = newEmptyContextUsage();
|
|
295
|
+
contextIds;
|
|
313
296
|
userContext = {};
|
|
314
297
|
memories = [];
|
|
315
298
|
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>;
|
|
@@ -4,7 +4,7 @@ import { tryOrThrow } from "../utils/type-utils.js";
|
|
|
4
4
|
import { parseAgentFile } from "./agent-yaml.js";
|
|
5
5
|
import { LoadJsAgentError } from "./error.js";
|
|
6
6
|
const importFn = new Function("path", "return import(path)");
|
|
7
|
-
export async function loadAgentFromJsFile(path) {
|
|
7
|
+
export async function loadAgentFromJsFile(path, options) {
|
|
8
8
|
const url = nodejs.path.isAbsolute(path) ? nodejs.url.pathToFileURL(path).toString() : path;
|
|
9
9
|
const { default: agent } = await tryOrThrow(() => importFn(url), (error) => new LoadJsAgentError(`Failed to load agent definition from ${url}: ${error.message}`));
|
|
10
10
|
if (isAgent(agent))
|
|
@@ -14,5 +14,5 @@ export async function loadAgentFromJsFile(path) {
|
|
|
14
14
|
process: agent,
|
|
15
15
|
name: agent.agent_name || agent.agentName || agent.name,
|
|
16
16
|
...agent,
|
|
17
|
-
}), (error) => new Error(`Failed to parse agent from ${path}: ${error.message}`));
|
|
17
|
+
}, options), (error) => new Error(`Failed to parse agent from ${path}: ${error.message}`));
|
|
18
18
|
}
|
|
@@ -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;
|
|
@@ -7,9 +7,9 @@ import { roleSchema } from "../agents/chat-model.js";
|
|
|
7
7
|
import { ProcessMode } from "../agents/team-agent.js";
|
|
8
8
|
import { tryOrThrow } from "../utils/type-utils.js";
|
|
9
9
|
import { codeToFunctionAgentFn } from "./function-agent.js";
|
|
10
|
-
import { camelizeSchema, chatModelSchema, defaultInputSchema, imageModelSchema, inputOutputSchema, optionalize, } from "./schema.js";
|
|
11
|
-
export async function parseAgentFile(path, data) {
|
|
12
|
-
const agentSchema = getAgentSchema({ filepath: path });
|
|
10
|
+
import { camelizeSchema, chatModelSchema, defaultInputSchema, imageModelSchema, inputOutputSchema, optionalize, preprocessSchema, } from "./schema.js";
|
|
11
|
+
export async function parseAgentFile(path, data, options) {
|
|
12
|
+
const agentSchema = getAgentSchema({ filepath: path, options });
|
|
13
13
|
return agentSchema.parseAsync({
|
|
14
14
|
...data,
|
|
15
15
|
model: data.model || data.chatModel || data.chat_model,
|
|
@@ -18,22 +18,11 @@ export async function parseAgentFile(path, data) {
|
|
|
18
18
|
export async function loadAgentFromYamlFile(path, options) {
|
|
19
19
|
const raw = await tryOrThrow(() => nodejs.fs.readFile(path, "utf8"), (error) => new Error(`Failed to load agent definition from ${path}: ${error.message}`));
|
|
20
20
|
const json = tryOrThrow(() => parse(raw), (error) => new Error(`Failed to parse agent definition from ${path}: ${error.message}`));
|
|
21
|
-
if (!["ai", "image", "mcp", "team", "transform", "function"].includes(json?.type)) {
|
|
22
|
-
if (typeof json?.type === "string") {
|
|
23
|
-
if (!options?.require)
|
|
24
|
-
throw new Error(`Module loader is not provided to load agent type module ${json.type} from ${path}`);
|
|
25
|
-
const Mod = await options.require(json.type, { parent: path });
|
|
26
|
-
if (typeof Mod?.default?.prototype?.constructor !== "function") {
|
|
27
|
-
throw new Error(`The agent type module ${json.type} does not export a default Agent class`);
|
|
28
|
-
}
|
|
29
|
-
json.agentClass = Mod.default;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
21
|
const agent = await tryOrThrow(async () => await parseAgentFile(path, {
|
|
33
22
|
...json,
|
|
34
23
|
type: json.type ?? "ai",
|
|
35
24
|
skills: json.skills ?? json.tools,
|
|
36
|
-
}), (error) => new Error(`Failed to validate agent definition from ${path}: ${error.message}`));
|
|
25
|
+
}, options), (error) => new Error(`Failed to validate agent definition from ${path}: ${error.message}`));
|
|
37
26
|
return agent;
|
|
38
27
|
}
|
|
39
28
|
const instructionItemSchema = z.union([
|
|
@@ -70,7 +59,7 @@ export const getInstructionsSchema = ({ filepath }) => z
|
|
|
70
59
|
}
|
|
71
60
|
return [await parseInstructionItem({ filepath })(v)];
|
|
72
61
|
});
|
|
73
|
-
export const getAgentSchema = ({ filepath }) => {
|
|
62
|
+
export const getAgentSchema = ({ filepath, options, }) => {
|
|
74
63
|
const agentSchema = z.lazy(() => {
|
|
75
64
|
const nestAgentSchema = z.lazy(() => z.union([
|
|
76
65
|
agentSchema,
|
|
@@ -127,7 +116,22 @@ export const getAgentSchema = ({ filepath }) => {
|
|
|
127
116
|
shareAFS: optionalize(z.boolean()),
|
|
128
117
|
});
|
|
129
118
|
const instructionsSchema = getInstructionsSchema({ filepath: filepath });
|
|
130
|
-
return camelizeSchema(
|
|
119
|
+
return camelizeSchema(preprocessSchema(async (json) => {
|
|
120
|
+
if (typeof json === "object" &&
|
|
121
|
+
json &&
|
|
122
|
+
"type" in json &&
|
|
123
|
+
typeof json.type === "string" &&
|
|
124
|
+
!["ai", "image", "mcp", "team", "transform", "function"].includes(json.type)) {
|
|
125
|
+
if (!options?.require)
|
|
126
|
+
throw new Error(`Module loader is not provided to load agent type module ${json.type} from ${filepath}`);
|
|
127
|
+
const Mod = await options.require(json.type, { parent: filepath });
|
|
128
|
+
if (typeof Mod?.default?.prototype?.constructor !== "function") {
|
|
129
|
+
throw new Error(`The agent type module ${json.type} does not export a default Agent class`);
|
|
130
|
+
}
|
|
131
|
+
Object.assign(json, { agentClass: Mod.default });
|
|
132
|
+
}
|
|
133
|
+
return json;
|
|
134
|
+
}, z.union([
|
|
131
135
|
z
|
|
132
136
|
.object({
|
|
133
137
|
type: z.string(),
|
|
@@ -198,7 +202,7 @@ export const getAgentSchema = ({ filepath }) => {
|
|
|
198
202
|
})
|
|
199
203
|
.extend(baseAgentSchema.shape),
|
|
200
204
|
]),
|
|
201
|
-
]));
|
|
205
|
+
])));
|
|
202
206
|
});
|
|
203
207
|
return agentSchema;
|
|
204
208
|
};
|
|
@@ -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/esm/loader/index.js
CHANGED
|
@@ -61,7 +61,7 @@ export async function load(path, options = {}) {
|
|
|
61
61
|
}
|
|
62
62
|
export async function loadAgent(path, options, agentOptions) {
|
|
63
63
|
if ([".js", ".mjs", ".ts", ".mts"].includes(nodejs.path.extname(path))) {
|
|
64
|
-
const agent = await loadAgentFromJsFile(path);
|
|
64
|
+
const agent = await loadAgentFromJsFile(path, options);
|
|
65
65
|
if (agent instanceof Agent)
|
|
66
66
|
return agent;
|
|
67
67
|
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/esm/loader/schema.js
CHANGED
|
@@ -91,3 +91,6 @@ export function optionalize(schema) {
|
|
|
91
91
|
export function camelizeSchema(schema, { shallow = true } = {}) {
|
|
92
92
|
return z.preprocess((v) => (isRecord(v) ? camelize(v, shallow) : v), schema);
|
|
93
93
|
}
|
|
94
|
+
export function preprocessSchema(fn, schema) {
|
|
95
|
+
return z.preprocess(fn, schema);
|
|
96
|
+
}
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import * as tson from "@zenoaihq/tson";
|
|
2
2
|
import { stringify } from "yaml";
|
|
3
3
|
export function setupFilters(env) {
|
|
4
|
-
env.addFilter("yaml.stringify", (obj) => {
|
|
5
|
-
|
|
6
|
-
});
|
|
7
|
-
env.addFilter("json.stringify", (obj, ...args) => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
env.addFilter("yaml.stringify", async (obj, cb) => {
|
|
5
|
+
cb(null, stringify(await obj));
|
|
6
|
+
}, true);
|
|
7
|
+
env.addFilter("json.stringify", async (obj, ...args) => {
|
|
8
|
+
const jsonArgs = args.slice(0, -1);
|
|
9
|
+
const cb = args[args.length - 1];
|
|
10
|
+
cb(null, JSON.stringify(await obj, ...jsonArgs));
|
|
11
|
+
}, true);
|
|
12
|
+
env.addFilter("tson.stringify", async (obj, cb) => {
|
|
13
|
+
cb(null, tson.dumps(await obj));
|
|
14
|
+
}, true);
|
|
13
15
|
}
|
|
@@ -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;
|
|
@@ -8,8 +8,8 @@ import { DEFAULT_OUTPUT_FILE_KEY, DEFAULT_OUTPUT_KEY } from "../agents/ai-agent.
|
|
|
8
8
|
import { fileUnionContentsSchema } from "../agents/model.js";
|
|
9
9
|
import { optionalize } from "../loader/schema.js";
|
|
10
10
|
import { outputSchemaToResponseFormatSchema } from "../utils/json-schema.js";
|
|
11
|
-
import { checkArguments, flat, isNonNullable, isRecord, partition, unique, } from "../utils/type-utils.js";
|
|
12
|
-
import { AFS_EXECUTABLE_TOOLS_PROMPT_TEMPLATE, getAFSSystemPrompt, } from "./prompts/afs-builtin-prompt.js";
|
|
11
|
+
import { checkArguments, flat, isNonNullable, isRecord, partition, pick, unique, } from "../utils/type-utils.js";
|
|
12
|
+
import { AFS_DESCRIPTION_PROMPT_TEMPLATE, AFS_EXECUTABLE_TOOLS_PROMPT_TEMPLATE, getAFSSystemPrompt, } from "./prompts/afs-builtin-prompt.js";
|
|
13
13
|
import { MEMORY_MESSAGE_TEMPLATE } from "./prompts/memory-message-template.js";
|
|
14
14
|
import { STRUCTURED_STREAM_INSTRUCTIONS } from "./prompts/structured-stream-instructions.js";
|
|
15
15
|
import { getAFSSkills } from "./skills/afs.js";
|
|
@@ -92,10 +92,36 @@ export class PromptBuilder {
|
|
|
92
92
|
};
|
|
93
93
|
}
|
|
94
94
|
getTemplateVariables(options) {
|
|
95
|
+
const self = this;
|
|
95
96
|
return {
|
|
96
97
|
userContext: options.context?.userContext,
|
|
97
98
|
...options.context?.userContext,
|
|
98
99
|
...options.input,
|
|
100
|
+
$afs: {
|
|
101
|
+
get enabled() {
|
|
102
|
+
return !!options.agent?.afs;
|
|
103
|
+
},
|
|
104
|
+
description: AFS_DESCRIPTION_PROMPT_TEMPLATE,
|
|
105
|
+
get modules() {
|
|
106
|
+
return options.agent?.afs
|
|
107
|
+
?.listModules()
|
|
108
|
+
.then((list) => list.map((i) => pick(i, ["name", "path", "description"])));
|
|
109
|
+
},
|
|
110
|
+
get histories() {
|
|
111
|
+
return self.getHistories(options);
|
|
112
|
+
},
|
|
113
|
+
get skills() {
|
|
114
|
+
const afs = options.agent?.afs;
|
|
115
|
+
if (!afs)
|
|
116
|
+
return [];
|
|
117
|
+
return getAFSSkills(afs).then((skills) => skills.map((s) => pick(s, ["name", "description"])));
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
$agent: {
|
|
121
|
+
get skills() {
|
|
122
|
+
return options.agent?.skills.map((s) => pick(s, ["name", "description"]));
|
|
123
|
+
},
|
|
124
|
+
},
|
|
99
125
|
};
|
|
100
126
|
}
|
|
101
127
|
async buildMessages(options) {
|
|
@@ -117,7 +143,7 @@ export class PromptBuilder {
|
|
|
117
143
|
memories.push(...options.context.memories);
|
|
118
144
|
}
|
|
119
145
|
const afs = options.agent?.afs;
|
|
120
|
-
if (afs) {
|
|
146
|
+
if (afs && options.agent?.historyConfig?.disabled !== true) {
|
|
121
147
|
const historyModule = (await afs.listModules()).find((m) => m.module instanceof AFSHistory);
|
|
122
148
|
messages.push(await SystemMessageTemplate.from(await getAFSSystemPrompt(afs)).format({}));
|
|
123
149
|
if (historyModule) {
|
|
@@ -195,6 +221,34 @@ export class PromptBuilder {
|
|
|
195
221
|
messages.push(...otherCustomMessages);
|
|
196
222
|
return this.refineMessages(options, messages);
|
|
197
223
|
}
|
|
224
|
+
async getHistories(options) {
|
|
225
|
+
const { agent } = options;
|
|
226
|
+
const afs = agent?.afs;
|
|
227
|
+
if (!afs)
|
|
228
|
+
return [];
|
|
229
|
+
const historyModule = (await afs.listModules()).find((m) => m.module instanceof AFSHistory);
|
|
230
|
+
if (!historyModule)
|
|
231
|
+
return [];
|
|
232
|
+
const { list: history } = await afs.list(historyModule.path, {
|
|
233
|
+
limit: agent.historyConfig?.maxItems || 10,
|
|
234
|
+
orderBy: [["createdAt", "desc"]],
|
|
235
|
+
});
|
|
236
|
+
return history
|
|
237
|
+
.reverse()
|
|
238
|
+
.map((i) => {
|
|
239
|
+
if (!i.content)
|
|
240
|
+
return;
|
|
241
|
+
const { input, output } = i.content;
|
|
242
|
+
if (!input || !output)
|
|
243
|
+
return;
|
|
244
|
+
return [
|
|
245
|
+
{ role: "user", content: input },
|
|
246
|
+
{ role: "agent", content: output },
|
|
247
|
+
];
|
|
248
|
+
})
|
|
249
|
+
.filter(isNonNullable)
|
|
250
|
+
.flat();
|
|
251
|
+
}
|
|
198
252
|
refineMessages(options, messages) {
|
|
199
253
|
const { autoReorderSystemMessages, autoMergeSystemMessages } = options.agent ?? {};
|
|
200
254
|
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";
|