@aigne/core 1.34.0 → 1.37.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -0
- package/lib/cjs/agents/agent.d.ts +71 -10
- package/lib/cjs/agents/agent.js +73 -28
- package/lib/cjs/agents/ai-agent.js +6 -3
- package/lib/cjs/agents/team-agent.d.ts +93 -1
- package/lib/cjs/agents/team-agent.js +45 -17
- package/lib/cjs/aigne/context.d.ts +1 -0
- package/lib/cjs/aigne/context.js +25 -10
- package/lib/cjs/loader/agent-js.d.ts +2 -17
- package/lib/cjs/loader/agent-js.js +4 -16
- package/lib/cjs/loader/agent-yaml.d.ts +29 -8
- package/lib/cjs/loader/agent-yaml.js +44 -15
- package/lib/cjs/loader/index.d.ts +2 -2
- package/lib/cjs/loader/index.js +55 -16
- package/lib/cjs/loader/schema.d.ts +10 -0
- package/lib/cjs/loader/schema.js +17 -1
- package/lib/cjs/utils/type-utils.d.ts +1 -1
- package/lib/cjs/utils/type-utils.js +2 -4
- package/lib/dts/agents/agent.d.ts +71 -10
- package/lib/dts/agents/team-agent.d.ts +93 -1
- package/lib/dts/aigne/context.d.ts +1 -0
- package/lib/dts/loader/agent-js.d.ts +2 -17
- package/lib/dts/loader/agent-yaml.d.ts +29 -8
- package/lib/dts/loader/index.d.ts +2 -2
- package/lib/dts/loader/schema.d.ts +10 -0
- package/lib/dts/utils/type-utils.d.ts +1 -1
- package/lib/esm/agents/agent.d.ts +71 -10
- package/lib/esm/agents/agent.js +73 -28
- package/lib/esm/agents/ai-agent.js +6 -3
- package/lib/esm/agents/team-agent.d.ts +93 -1
- package/lib/esm/agents/team-agent.js +44 -16
- package/lib/esm/aigne/context.d.ts +1 -0
- package/lib/esm/aigne/context.js +25 -10
- package/lib/esm/loader/agent-js.d.ts +2 -17
- package/lib/esm/loader/agent-js.js +4 -13
- package/lib/esm/loader/agent-yaml.d.ts +29 -8
- package/lib/esm/loader/agent-yaml.js +44 -13
- package/lib/esm/loader/index.d.ts +2 -2
- package/lib/esm/loader/index.js +56 -17
- package/lib/esm/loader/schema.d.ts +10 -0
- package/lib/esm/loader/schema.js +12 -0
- package/lib/esm/utils/type-utils.d.ts +1 -1
- package/lib/esm/utils/type-utils.js +2 -4
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -12,6 +12,32 @@
|
|
|
12
12
|
* dependencies
|
|
13
13
|
* @aigne/observability bumped to 0.1.0
|
|
14
14
|
|
|
15
|
+
## [1.37.0](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.36.0...core-v1.37.0) (2025-07-22)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Features
|
|
19
|
+
|
|
20
|
+
* **core:** add reflection mode support to TeamAgent ([#273](https://github.com/AIGNE-io/aigne-framework/issues/273)) ([4e2dad6](https://github.com/AIGNE-io/aigne-framework/commit/4e2dad687c1caefa231c7a7620651d060f8c8b9d))
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
### Bug Fixes
|
|
24
|
+
|
|
25
|
+
* **core:** function agent should use common schema from yaml definition ([#270](https://github.com/AIGNE-io/aigne-framework/issues/270)) ([076a489](https://github.com/AIGNE-io/aigne-framework/commit/076a4896948c397518e99df46c1a443ea43daa64))
|
|
26
|
+
|
|
27
|
+
## [1.36.0](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.35.0...core-v1.36.0) (2025-07-17)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
### Features
|
|
31
|
+
|
|
32
|
+
* **core:** support define hooks for agent in yaml ([#260](https://github.com/AIGNE-io/aigne-framework/issues/260)) ([c388e82](https://github.com/AIGNE-io/aigne-framework/commit/c388e8216134271af4d9c7def70862ea3c354c7f))
|
|
33
|
+
|
|
34
|
+
## [1.35.0](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.34.0...core-v1.35.0) (2025-07-17)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
### Features
|
|
38
|
+
|
|
39
|
+
* **core:** support custom default input value for agent ([#258](https://github.com/AIGNE-io/aigne-framework/issues/258)) ([352ac70](https://github.com/AIGNE-io/aigne-framework/commit/352ac70400fb7e28cc36c4f6dc9c591b0d64e546))
|
|
40
|
+
|
|
15
41
|
## [1.34.0](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.33.2...core-v1.34.0) (2025-07-15)
|
|
16
42
|
|
|
17
43
|
|
|
@@ -10,6 +10,7 @@ import { type Nullish, type PromiseOrValue, type XOr } from "../utils/type-utils
|
|
|
10
10
|
import type { GuideRailAgent, GuideRailAgentOutput } from "./guide-rail-agent.js";
|
|
11
11
|
import { type TransferAgentOutput } from "./types.js";
|
|
12
12
|
export * from "./types.js";
|
|
13
|
+
export declare const DEFAULT_INPUT_ACTION_GET = "$get";
|
|
13
14
|
/**
|
|
14
15
|
* Basic message type that can contain any key-value pairs
|
|
15
16
|
*/
|
|
@@ -72,6 +73,11 @@ export interface AgentOptions<I extends Message = Message, O extends Message = M
|
|
|
72
73
|
* Used to validate that input messages conform to the expected format
|
|
73
74
|
*/
|
|
74
75
|
inputSchema?: AgentInputOutputSchema<I>;
|
|
76
|
+
/**
|
|
77
|
+
* Default input message for the agent, it can be used to provide partial input
|
|
78
|
+
* or default values for the agent's input schema.
|
|
79
|
+
*/
|
|
80
|
+
defaultInput?: Agent<I, O>["defaultInput"];
|
|
75
81
|
/**
|
|
76
82
|
* Zod schema defining the output message structure
|
|
77
83
|
*
|
|
@@ -106,7 +112,7 @@ export interface AgentOptions<I extends Message = Message, O extends Message = M
|
|
|
106
112
|
* Maximum number of memory items to retrieve
|
|
107
113
|
*/
|
|
108
114
|
maxRetrieveMemoryCount?: number;
|
|
109
|
-
hooks?: AgentHooks<I, O
|
|
115
|
+
hooks?: AgentHooks<I, O> | AgentHooks<I, O>[];
|
|
110
116
|
}
|
|
111
117
|
export declare const agentOptionsSchema: ZodObject<{
|
|
112
118
|
[key in keyof AgentOptions]: ZodType<AgentOptions[key]>;
|
|
@@ -137,6 +143,10 @@ export interface AgentInvokeOptions<U extends UserContext = UserContext> {
|
|
|
137
143
|
* and returns the final JSON result
|
|
138
144
|
*/
|
|
139
145
|
streaming?: boolean;
|
|
146
|
+
/**
|
|
147
|
+
* Optional hooks for agent invocation
|
|
148
|
+
*/
|
|
149
|
+
hooks?: AgentHooks<any, any>;
|
|
140
150
|
}
|
|
141
151
|
/**
|
|
142
152
|
* Agent is the base class for all agents.
|
|
@@ -190,7 +200,7 @@ export declare abstract class Agent<I extends Message = any, O extends Message =
|
|
|
190
200
|
* Here's an example of using hooks:
|
|
191
201
|
* {@includeCode ../../test/agents/agent.test.ts#example-agent-hooks}
|
|
192
202
|
*/
|
|
193
|
-
readonly hooks: AgentHooks<I, O
|
|
203
|
+
readonly hooks: AgentHooks<I, O>[];
|
|
194
204
|
/**
|
|
195
205
|
* List of GuideRail agents applied to this agent
|
|
196
206
|
*
|
|
@@ -232,6 +242,11 @@ export declare abstract class Agent<I extends Message = any, O extends Message =
|
|
|
232
242
|
*/
|
|
233
243
|
readonly description?: string;
|
|
234
244
|
private readonly _inputSchema?;
|
|
245
|
+
defaultInput?: Partial<{
|
|
246
|
+
[key in keyof I]: {
|
|
247
|
+
[DEFAULT_INPUT_ACTION_GET]: string;
|
|
248
|
+
} | I[key];
|
|
249
|
+
}>;
|
|
235
250
|
private readonly _outputSchema?;
|
|
236
251
|
/**
|
|
237
252
|
* Get the input data schema for this agent
|
|
@@ -372,6 +387,8 @@ export declare abstract class Agent<I extends Message = any, O extends Message =
|
|
|
372
387
|
* @returns Agent response (streaming or regular)
|
|
373
388
|
*/
|
|
374
389
|
invoke(input: I & Message, options?: Partial<AgentInvokeOptions>): Promise<AgentResponse<O>>;
|
|
390
|
+
private callHooks;
|
|
391
|
+
private mergeDefaultInput;
|
|
375
392
|
protected invokeSkill<I extends Message, O extends Message>(skill: Agent<I, O>, input: I & Message, options: AgentInvokeOptions): Promise<O>;
|
|
376
393
|
/**
|
|
377
394
|
* Process agent output
|
|
@@ -517,11 +534,15 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
|
|
|
517
534
|
*
|
|
518
535
|
* @param event Object containing the input message
|
|
519
536
|
*/
|
|
520
|
-
onStart?: (event: {
|
|
537
|
+
onStart?: ((event: {
|
|
521
538
|
context: Context;
|
|
522
539
|
input: I;
|
|
523
540
|
}) => PromiseOrValue<void | {
|
|
524
541
|
input?: I;
|
|
542
|
+
}>) | Agent<{
|
|
543
|
+
input: I;
|
|
544
|
+
}, {
|
|
545
|
+
input?: I;
|
|
525
546
|
}>;
|
|
526
547
|
/**
|
|
527
548
|
* Called when agent processing completes or fails
|
|
@@ -532,13 +553,39 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
|
|
|
532
553
|
*
|
|
533
554
|
* @param event Object containing the input message and either output or error
|
|
534
555
|
*/
|
|
535
|
-
onEnd?: (event: XOr<{
|
|
556
|
+
onEnd?: ((event: XOr<{
|
|
536
557
|
context: Context;
|
|
537
558
|
input: I;
|
|
538
559
|
output: O;
|
|
539
560
|
error: Error;
|
|
540
561
|
}, "output", "error">) => PromiseOrValue<void | {
|
|
541
562
|
output?: O;
|
|
563
|
+
}>) | Agent<XOr<{
|
|
564
|
+
input: I;
|
|
565
|
+
output: O;
|
|
566
|
+
error: Error;
|
|
567
|
+
}, "output", "error">, {
|
|
568
|
+
output?: O;
|
|
569
|
+
}>;
|
|
570
|
+
onSuccess?: ((event: {
|
|
571
|
+
context: Context;
|
|
572
|
+
input: I;
|
|
573
|
+
output: O;
|
|
574
|
+
}) => PromiseOrValue<void | {
|
|
575
|
+
output?: O;
|
|
576
|
+
}>) | Agent<{
|
|
577
|
+
input: I;
|
|
578
|
+
output: O;
|
|
579
|
+
}, {
|
|
580
|
+
output?: O;
|
|
581
|
+
}>;
|
|
582
|
+
onError?: ((event: {
|
|
583
|
+
context: Context;
|
|
584
|
+
input: I;
|
|
585
|
+
error: Error;
|
|
586
|
+
}) => void) | Agent<{
|
|
587
|
+
input: I;
|
|
588
|
+
error: Error;
|
|
542
589
|
}>;
|
|
543
590
|
/**
|
|
544
591
|
* Called before a skill (sub-agent) is invoked
|
|
@@ -548,11 +595,14 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
|
|
|
548
595
|
*
|
|
549
596
|
* @param event Object containing the skill being used and input message
|
|
550
597
|
*/
|
|
551
|
-
onSkillStart?: (event: {
|
|
598
|
+
onSkillStart?: ((event: {
|
|
552
599
|
context: Context;
|
|
553
600
|
skill: Agent;
|
|
554
601
|
input: Message;
|
|
555
|
-
}) => PromiseOrValue<void
|
|
602
|
+
}) => PromiseOrValue<void>) | Agent<{
|
|
603
|
+
skill: Agent;
|
|
604
|
+
input: Message;
|
|
605
|
+
}>;
|
|
556
606
|
/**
|
|
557
607
|
* Called after a skill (sub-agent) completes or fails
|
|
558
608
|
*
|
|
@@ -562,13 +612,18 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
|
|
|
562
612
|
*
|
|
563
613
|
* @param event Object containing the skill used, input message, and either output or error
|
|
564
614
|
*/
|
|
565
|
-
onSkillEnd?: (event: XOr<{
|
|
615
|
+
onSkillEnd?: ((event: XOr<{
|
|
566
616
|
context: Context;
|
|
567
617
|
skill: Agent;
|
|
568
618
|
input: Message;
|
|
569
619
|
output: Message;
|
|
570
620
|
error: Error;
|
|
571
|
-
}, "output", "error">) => PromiseOrValue<void
|
|
621
|
+
}, "output", "error">) => PromiseOrValue<void>) | Agent<XOr<{
|
|
622
|
+
skill: Agent;
|
|
623
|
+
input: Message;
|
|
624
|
+
output: Message;
|
|
625
|
+
error: Error;
|
|
626
|
+
}, "output", "error">>;
|
|
572
627
|
/**
|
|
573
628
|
* Called when an agent hands off processing to another agent
|
|
574
629
|
*
|
|
@@ -578,13 +633,19 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
|
|
|
578
633
|
*
|
|
579
634
|
* @param event Object containing the source agent, target agent, and input message
|
|
580
635
|
*/
|
|
581
|
-
onHandoff?: (event: {
|
|
636
|
+
onHandoff?: ((event: {
|
|
582
637
|
context: Context;
|
|
583
638
|
source: Agent;
|
|
584
639
|
target: Agent;
|
|
585
640
|
input: I;
|
|
586
|
-
}) => PromiseOrValue<void
|
|
641
|
+
}) => PromiseOrValue<void>) | Agent<{
|
|
642
|
+
source: Agent;
|
|
643
|
+
target: Agent;
|
|
644
|
+
input: I;
|
|
645
|
+
}>;
|
|
587
646
|
}
|
|
647
|
+
export type AgentHookInput<H extends keyof AgentHooks, Hook extends AgentHooks[H] = AgentHooks[H]> = Hook extends (input: infer I) => any ? Omit<I, "context"> : never;
|
|
648
|
+
export type AgentHookOutput<H extends keyof AgentHooks, Hook extends AgentHooks[H] = AgentHooks[H]> = Hook extends (...args: any[]) => any ? Exclude<Awaited<ReturnType<Hook>>, void> | undefined : never;
|
|
588
649
|
/**
|
|
589
650
|
* Response type for an agent, can be:
|
|
590
651
|
* - Direct response object
|
package/lib/cjs/agents/agent.js
CHANGED
|
@@ -36,7 +36,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
36
36
|
};
|
|
37
37
|
})();
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.FunctionAgent = exports.Agent = exports.agentOptionsSchema = void 0;
|
|
39
|
+
exports.FunctionAgent = exports.Agent = exports.agentOptionsSchema = exports.DEFAULT_INPUT_ACTION_GET = void 0;
|
|
40
40
|
exports.isEmptyChunk = isEmptyChunk;
|
|
41
41
|
exports.isAgentResponseDelta = isAgentResponseDelta;
|
|
42
42
|
exports.isAgentResponseProgress = isAgentResponseProgress;
|
|
@@ -50,6 +50,14 @@ const stream_utils_js_1 = require("../utils/stream-utils.js");
|
|
|
50
50
|
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
51
51
|
const types_js_1 = require("./types.js");
|
|
52
52
|
__exportStar(require("./types.js"), exports);
|
|
53
|
+
exports.DEFAULT_INPUT_ACTION_GET = "$get";
|
|
54
|
+
const hooksSchema = zod_1.z.object({
|
|
55
|
+
onStart: zod_1.z.custom().optional(),
|
|
56
|
+
onEnd: zod_1.z.custom().optional(),
|
|
57
|
+
onSkillStart: zod_1.z.custom().optional(),
|
|
58
|
+
onSkillEnd: zod_1.z.custom().optional(),
|
|
59
|
+
onHandoff: zod_1.z.custom().optional(),
|
|
60
|
+
});
|
|
53
61
|
exports.agentOptionsSchema = zod_1.z.object({
|
|
54
62
|
subscribeTopic: zod_1.z.union([zod_1.z.string(), zod_1.z.array(zod_1.z.string())]).optional(),
|
|
55
63
|
publishTopic: zod_1.z
|
|
@@ -58,21 +66,14 @@ exports.agentOptionsSchema = zod_1.z.object({
|
|
|
58
66
|
name: zod_1.z.string().optional(),
|
|
59
67
|
description: zod_1.z.string().optional(),
|
|
60
68
|
inputSchema: zod_1.z.custom().optional(),
|
|
69
|
+
defaultInput: zod_1.z.record(zod_1.z.any()).optional(),
|
|
61
70
|
outputSchema: zod_1.z.custom().optional(),
|
|
62
71
|
includeInputInOutput: zod_1.z.boolean().optional(),
|
|
63
72
|
skills: zod_1.z.array(zod_1.z.union([zod_1.z.custom(), zod_1.z.custom()])).optional(),
|
|
64
73
|
disableEvents: zod_1.z.boolean().optional(),
|
|
65
74
|
memory: zod_1.z.union([zod_1.z.custom(), zod_1.z.array(zod_1.z.custom())]).optional(),
|
|
66
75
|
maxRetrieveMemoryCount: zod_1.z.number().optional(),
|
|
67
|
-
hooks: zod_1.z
|
|
68
|
-
.object({
|
|
69
|
-
onStart: zod_1.z.custom().optional(),
|
|
70
|
-
onEnd: zod_1.z.custom().optional(),
|
|
71
|
-
onSkillStart: zod_1.z.custom().optional(),
|
|
72
|
-
onSkillEnd: zod_1.z.custom().optional(),
|
|
73
|
-
onHandoff: zod_1.z.custom().optional(),
|
|
74
|
-
})
|
|
75
|
-
.optional(),
|
|
76
|
+
hooks: zod_1.z.union([zod_1.z.array(hooksSchema), hooksSchema]).optional(),
|
|
76
77
|
guideRails: zod_1.z.array(zod_1.z.custom()).optional(),
|
|
77
78
|
});
|
|
78
79
|
/**
|
|
@@ -107,6 +108,7 @@ class Agent {
|
|
|
107
108
|
if (outputSchema)
|
|
108
109
|
checkAgentInputOutputSchema(outputSchema);
|
|
109
110
|
this._inputSchema = inputSchema;
|
|
111
|
+
this.defaultInput = options.defaultInput;
|
|
110
112
|
this._outputSchema = outputSchema;
|
|
111
113
|
this.includeInputInOutput = options.includeInputInOutput;
|
|
112
114
|
this.subscribeTopic = options.subscribeTopic;
|
|
@@ -121,7 +123,7 @@ class Agent {
|
|
|
121
123
|
this.memories.push(options.memory);
|
|
122
124
|
}
|
|
123
125
|
this.maxRetrieveMemoryCount = options.maxRetrieveMemoryCount;
|
|
124
|
-
this.hooks = options.hooks
|
|
126
|
+
this.hooks = (0, type_utils_js_1.flat)(options.hooks);
|
|
125
127
|
this.guideRails = options.guideRails;
|
|
126
128
|
}
|
|
127
129
|
/**
|
|
@@ -187,6 +189,7 @@ class Agent {
|
|
|
187
189
|
*/
|
|
188
190
|
description;
|
|
189
191
|
_inputSchema;
|
|
192
|
+
defaultInput;
|
|
190
193
|
_outputSchema;
|
|
191
194
|
/**
|
|
192
195
|
* Get the input data schema for this agent
|
|
@@ -336,17 +339,23 @@ class Agent {
|
|
|
336
339
|
...options,
|
|
337
340
|
context: options.context ?? (await this.newDefaultContext()),
|
|
338
341
|
};
|
|
342
|
+
input = this.mergeDefaultInput(input);
|
|
339
343
|
logger_js_1.logger.debug("Invoke agent %s started with input: %O", this.name, input);
|
|
340
344
|
if (!this.disableEvents)
|
|
341
345
|
opts.context.emit("agentStarted", { agent: this, input });
|
|
342
346
|
try {
|
|
343
|
-
let
|
|
344
|
-
|
|
345
|
-
|
|
347
|
+
let response;
|
|
348
|
+
const s = await this.callHooks("onStart", { input }, opts);
|
|
349
|
+
if (s?.input)
|
|
350
|
+
input = s.input;
|
|
351
|
+
input = (0, type_utils_js_1.checkArguments)(`Agent ${this.name} input`, this.inputSchema, input);
|
|
352
|
+
await this.preprocess(input, opts);
|
|
346
353
|
this.checkContextStatus(opts);
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
response
|
|
354
|
+
if (!response) {
|
|
355
|
+
response = await this.process(input, opts);
|
|
356
|
+
if (response instanceof Agent) {
|
|
357
|
+
response = (0, types_js_1.transferToAgentOutput)(response);
|
|
358
|
+
}
|
|
350
359
|
}
|
|
351
360
|
if (opts.streaming) {
|
|
352
361
|
const stream = response instanceof ReadableStream
|
|
@@ -356,29 +365,62 @@ class Agent {
|
|
|
356
365
|
: (0, stream_utils_js_1.objectToAgentResponseStream)(response);
|
|
357
366
|
return this.checkResponseByGuideRails(input, (0, stream_utils_js_1.onAgentResponseStreamEnd)(stream, {
|
|
358
367
|
onResult: async (result) => {
|
|
359
|
-
return await this.processAgentOutput(
|
|
368
|
+
return await this.processAgentOutput(input, result, opts);
|
|
360
369
|
},
|
|
361
370
|
onError: async (error) => {
|
|
362
371
|
return await this.processAgentError(input, error, opts);
|
|
363
372
|
},
|
|
364
373
|
}), opts);
|
|
365
374
|
}
|
|
366
|
-
return await this.checkResponseByGuideRails(input, this.processAgentOutput(
|
|
375
|
+
return await this.checkResponseByGuideRails(input, this.processAgentOutput(input, await agentProcessResultToObject(response), opts), opts);
|
|
367
376
|
}
|
|
368
377
|
catch (error) {
|
|
369
378
|
throw await this.processAgentError(input, error, opts);
|
|
370
379
|
}
|
|
371
380
|
}
|
|
381
|
+
async callHooks(hook, input, options) {
|
|
382
|
+
const { context } = options;
|
|
383
|
+
const result = {};
|
|
384
|
+
for (const hooks of (0, type_utils_js_1.flat)(options.hooks, this.hooks)) {
|
|
385
|
+
const h = hooks[hook];
|
|
386
|
+
if (!h)
|
|
387
|
+
continue;
|
|
388
|
+
if (typeof h === "function") {
|
|
389
|
+
Object.assign(result, await h({ ...input, context }));
|
|
390
|
+
}
|
|
391
|
+
else {
|
|
392
|
+
Object.assign(result, await context.invoke(h, input));
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return result;
|
|
396
|
+
}
|
|
397
|
+
mergeDefaultInput(input) {
|
|
398
|
+
const defaultInput = Object.fromEntries(Object.entries(this.defaultInput ?? {}).filter(([, v]) => !(typeof v === "object" && exports.DEFAULT_INPUT_ACTION_GET in v)));
|
|
399
|
+
input = { ...defaultInput, ...input };
|
|
400
|
+
for (const key of Object.keys(this.defaultInput ?? {})) {
|
|
401
|
+
const v = this.defaultInput?.[key];
|
|
402
|
+
if (v &&
|
|
403
|
+
typeof v === "object" &&
|
|
404
|
+
exports.DEFAULT_INPUT_ACTION_GET in v &&
|
|
405
|
+
typeof v[exports.DEFAULT_INPUT_ACTION_GET] === "string" &&
|
|
406
|
+
(0, type_utils_js_1.isNil)(input[key])) {
|
|
407
|
+
const value = input[v[exports.DEFAULT_INPUT_ACTION_GET]];
|
|
408
|
+
if (!(0, type_utils_js_1.isNil)(value))
|
|
409
|
+
Object.assign(input, { [key]: value });
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
return input;
|
|
413
|
+
}
|
|
372
414
|
async invokeSkill(skill, input, options) {
|
|
373
415
|
const { context } = options;
|
|
374
|
-
await this.
|
|
416
|
+
await this.callHooks("onSkillStart", { skill, input }, options);
|
|
375
417
|
try {
|
|
376
418
|
const output = await context.invoke(skill, input);
|
|
377
|
-
await this.
|
|
419
|
+
await this.callHooks("onSkillEnd", { skill, input, output }, options);
|
|
378
420
|
return output;
|
|
379
421
|
}
|
|
380
422
|
catch (error) {
|
|
381
|
-
await this.
|
|
423
|
+
await this.callHooks("onSkillEnd", { skill, input, error }, options);
|
|
382
424
|
throw error;
|
|
383
425
|
}
|
|
384
426
|
}
|
|
@@ -398,14 +440,17 @@ class Agent {
|
|
|
398
440
|
throw new Error(`expect to return a record type such as {result: ...}, but got (${typeof output}): ${output}`);
|
|
399
441
|
}
|
|
400
442
|
const parsedOutput = (0, type_utils_js_1.checkArguments)(`Agent ${this.name} output`, this.outputSchema, output);
|
|
401
|
-
|
|
443
|
+
let finalOutput = this.includeInputInOutput ? { ...input, ...parsedOutput } : parsedOutput;
|
|
402
444
|
await this.postprocess(input, finalOutput, options);
|
|
403
445
|
logger_js_1.logger.debug("Invoke agent %s succeed with output: %O", this.name, finalOutput);
|
|
404
446
|
if (!this.disableEvents)
|
|
405
447
|
context.emit("agentSucceed", { agent: this, output: finalOutput });
|
|
406
|
-
|
|
407
|
-
if (o)
|
|
408
|
-
|
|
448
|
+
let o = await this.callHooks("onSuccess", { input, output: finalOutput }, options);
|
|
449
|
+
if (o?.output)
|
|
450
|
+
finalOutput = o.output;
|
|
451
|
+
o = await this.callHooks("onEnd", { input, output: finalOutput }, options);
|
|
452
|
+
if (o?.output)
|
|
453
|
+
finalOutput = o.output;
|
|
409
454
|
return finalOutput;
|
|
410
455
|
}
|
|
411
456
|
/**
|
|
@@ -420,8 +465,8 @@ class Agent {
|
|
|
420
465
|
logger_js_1.logger.error("Invoke agent %s failed with error: %O", this.name, error);
|
|
421
466
|
if (!this.disableEvents)
|
|
422
467
|
options.context.emit("agentFailed", { agent: this, error });
|
|
423
|
-
|
|
424
|
-
await this.
|
|
468
|
+
await this.callHooks("onError", { input, error }, options);
|
|
469
|
+
await this.callHooks("onEnd", { input, error }, options);
|
|
425
470
|
return error;
|
|
426
471
|
}
|
|
427
472
|
/**
|
|
@@ -243,7 +243,7 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
243
243
|
const outputKey = this.outputKey;
|
|
244
244
|
for (;;) {
|
|
245
245
|
const modelOutput = {};
|
|
246
|
-
let stream = await options.context.invoke(model, { ...modelInput, messages: modelInput.messages.concat(toolCallMessages) }, { streaming: true });
|
|
246
|
+
let stream = await options.context.invoke(model, { ...modelInput, messages: modelInput.messages.concat(toolCallMessages) }, { ...options, streaming: true });
|
|
247
247
|
if (this.structuredStreamMode) {
|
|
248
248
|
const { metadataStart, metadataEnd, parse } = this.customStructuredStreamInstructions || structured_stream_instructions_js_1.STRUCTURED_STREAM_INSTRUCTIONS;
|
|
249
249
|
stream = stream.pipeThrough(new structured_stream_extractor_js_1.ExtractMetadataTransform({ start: metadataStart, end: metadataEnd, parse }));
|
|
@@ -318,14 +318,17 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
318
318
|
* @protected
|
|
319
319
|
*/
|
|
320
320
|
async *_processRouter(input, model, modelInput, options, toolsMap) {
|
|
321
|
-
const { toolCalls: [call] = [] } = await options.context.invoke(model, modelInput
|
|
321
|
+
const { toolCalls: [call] = [] } = await options.context.invoke(model, modelInput, {
|
|
322
|
+
...options,
|
|
323
|
+
streaming: false,
|
|
324
|
+
});
|
|
322
325
|
if (!call) {
|
|
323
326
|
throw new Error("Router toolChoice requires exactly one tool to be executed");
|
|
324
327
|
}
|
|
325
328
|
const tool = toolsMap.get(call.function.name);
|
|
326
329
|
if (!tool)
|
|
327
330
|
throw new Error(`Tool not found: ${call.function.name}`);
|
|
328
|
-
const stream = await options.context.invoke(tool, { ...call.function.arguments, ...input }, { streaming: true, sourceAgent: this });
|
|
331
|
+
const stream = await options.context.invoke(tool, { ...call.function.arguments, ...input }, { ...options, streaming: true, sourceAgent: this });
|
|
329
332
|
return yield* stream;
|
|
330
333
|
}
|
|
331
334
|
}
|
|
@@ -22,6 +22,73 @@ export declare enum ProcessMode {
|
|
|
22
22
|
*/
|
|
23
23
|
parallel = "parallel"
|
|
24
24
|
}
|
|
25
|
+
export declare const DEFAULT_REFLECTION_MAX_ITERATIONS = 3;
|
|
26
|
+
/**
|
|
27
|
+
* Configuration for reflection mode processing in TeamAgent.
|
|
28
|
+
*
|
|
29
|
+
* Reflection mode enables iterative refinement of agent outputs through a review process.
|
|
30
|
+
* The team agent will repeatedly process the input and have a reviewer agent evaluate
|
|
31
|
+
* the output until it meets approval criteria or reaches the maximum iteration limit.
|
|
32
|
+
*
|
|
33
|
+
* This is particularly useful for:
|
|
34
|
+
* - Quality assurance workflows where outputs need validation
|
|
35
|
+
* - Iterative improvement processes where initial results can be refined
|
|
36
|
+
* - Self-correcting agent systems that learn from feedback
|
|
37
|
+
*/
|
|
38
|
+
export interface ReflectionMode {
|
|
39
|
+
/**
|
|
40
|
+
* The agent responsible for reviewing and providing feedback on the team's output.
|
|
41
|
+
*
|
|
42
|
+
* The reviewer agent receives the combined output from the team's processing
|
|
43
|
+
* and should provide feedback or evaluation that can be used to determine
|
|
44
|
+
* whether the output meets the required standards.
|
|
45
|
+
*/
|
|
46
|
+
reviewer: Agent;
|
|
47
|
+
/**
|
|
48
|
+
* Function that determines whether the reviewer's output indicates approval.
|
|
49
|
+
*
|
|
50
|
+
* This function receives the reviewer agent's output message and should return:
|
|
51
|
+
* - `true` or truthy value: The output is approved and processing should stop
|
|
52
|
+
* - `false` or falsy value: The output needs improvement and another iteration should run
|
|
53
|
+
*
|
|
54
|
+
* The function can be synchronous or asynchronous, allowing for complex approval logic
|
|
55
|
+
* including external validation, scoring systems, or human-in-the-loop approval.
|
|
56
|
+
*
|
|
57
|
+
* @param output - The message output from the reviewer agent
|
|
58
|
+
* @returns A boolean or truthy/falsy value indicating approval status
|
|
59
|
+
*/
|
|
60
|
+
isApproved: (output: Message) => PromiseOrValue<boolean | unknown>;
|
|
61
|
+
/**
|
|
62
|
+
* Maximum number of reflection iterations before giving up.
|
|
63
|
+
*
|
|
64
|
+
* This prevents infinite loops when the approval criteria cannot be met.
|
|
65
|
+
* If the maximum iterations are reached without approval, the process will
|
|
66
|
+
* throw an error indicating the reflection failed to converge.
|
|
67
|
+
*
|
|
68
|
+
* @default 3
|
|
69
|
+
*/
|
|
70
|
+
maxIterations?: number;
|
|
71
|
+
/**
|
|
72
|
+
* Controls the behavior when maximum iterations are reached without approval.
|
|
73
|
+
*
|
|
74
|
+
* When set to `true`, the TeamAgent will return the last generated output
|
|
75
|
+
* instead of throwing an error when the maximum number of reflection iterations
|
|
76
|
+
* is reached without the reviewer's approval.
|
|
77
|
+
*
|
|
78
|
+
* When set to `false` or undefined, the TeamAgent will throw an error
|
|
79
|
+
* indicating that the reflection process failed to converge within the
|
|
80
|
+
* maximum iteration limit.
|
|
81
|
+
*
|
|
82
|
+
* This option is useful for scenarios where:
|
|
83
|
+
* - You want to get the best available result even if it's not perfect
|
|
84
|
+
* - The approval criteria might be too strict for the given context
|
|
85
|
+
* - You prefer graceful degradation over complete failure
|
|
86
|
+
* - You want to implement custom error handling based on the returned result
|
|
87
|
+
*
|
|
88
|
+
* @default false
|
|
89
|
+
*/
|
|
90
|
+
returnLastOnMaxIterations?: boolean;
|
|
91
|
+
}
|
|
25
92
|
/**
|
|
26
93
|
* Configuration options for creating a TeamAgent.
|
|
27
94
|
*
|
|
@@ -33,6 +100,21 @@ export interface TeamAgentOptions<I extends Message, O extends Message> extends
|
|
|
33
100
|
* @default {ProcessMode.sequential}
|
|
34
101
|
*/
|
|
35
102
|
mode?: ProcessMode;
|
|
103
|
+
/**
|
|
104
|
+
* Configuration for reflection mode processing.
|
|
105
|
+
*
|
|
106
|
+
* When enabled, the TeamAgent will use an iterative refinement process where:
|
|
107
|
+
* 1. The team processes the input normally
|
|
108
|
+
* 2. A reviewer agent evaluates the output
|
|
109
|
+
* 3. If not approved, the process repeats with the previous output as context
|
|
110
|
+
* 4. This continues until approval or maximum iterations are reached
|
|
111
|
+
*
|
|
112
|
+
* This enables self-improving agent workflows that can iteratively refine
|
|
113
|
+
* their outputs based on feedback from a specialized reviewer agent.
|
|
114
|
+
*
|
|
115
|
+
* @see ReflectionMode for detailed configuration options
|
|
116
|
+
*/
|
|
117
|
+
reflection?: ReflectionMode;
|
|
36
118
|
/**
|
|
37
119
|
* Specifies which input field should be treated as an array for iterative processing.
|
|
38
120
|
*
|
|
@@ -116,6 +198,14 @@ export declare class TeamAgent<I extends Message, O extends Message> extends Age
|
|
|
116
198
|
* This can be either sequential (one after another) or parallel (all at once).
|
|
117
199
|
*/
|
|
118
200
|
mode: ProcessMode;
|
|
201
|
+
/**
|
|
202
|
+
* The reflection mode configuration with guaranteed maxIterations value.
|
|
203
|
+
*
|
|
204
|
+
* This is the internal representation after processing the user-provided
|
|
205
|
+
* reflection configuration, ensuring that maxIterations always has a value
|
|
206
|
+
* (defaulting to DEFAULT_REFLECTION_MAX_ITERATIONS if not specified).
|
|
207
|
+
*/
|
|
208
|
+
reflection?: ReflectionMode & Required<Pick<ReflectionMode, "maxIterations">>;
|
|
119
209
|
/**
|
|
120
210
|
* The input field key to iterate over when processing array inputs.
|
|
121
211
|
*
|
|
@@ -148,8 +238,10 @@ export declare class TeamAgent<I extends Message, O extends Message> extends Age
|
|
|
148
238
|
* @returns A stream of message chunks that collectively form the response
|
|
149
239
|
*/
|
|
150
240
|
process(input: I, options: AgentInvokeOptions): PromiseOrValue<AgentProcessResult<O>>;
|
|
241
|
+
private _processNonReflection;
|
|
242
|
+
private _processReflection;
|
|
151
243
|
private _processIterator;
|
|
152
|
-
private
|
|
244
|
+
private _processNonIterator;
|
|
153
245
|
/**
|
|
154
246
|
* Process input sequentially through each agent in the team.
|
|
155
247
|
*
|