@aigne/core 1.16.0 → 1.18.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 +37 -16
- package/lib/cjs/agents/agent.d.ts +69 -37
- package/lib/cjs/agents/agent.js +93 -65
- package/lib/cjs/agents/ai-agent.d.ts +4 -7
- package/lib/cjs/agents/ai-agent.js +10 -19
- package/lib/cjs/agents/chat-model.d.ts +7 -8
- package/lib/cjs/agents/chat-model.js +9 -9
- package/lib/cjs/agents/mcp-agent.d.ts +4 -5
- package/lib/cjs/agents/mcp-agent.js +7 -13
- package/lib/cjs/agents/team-agent.d.ts +7 -8
- package/lib/cjs/agents/team-agent.js +10 -10
- package/lib/cjs/agents/user-agent.d.ts +4 -5
- package/lib/cjs/agents/user-agent.js +11 -12
- package/lib/cjs/aigne/aigne.d.ts +10 -9
- package/lib/cjs/aigne/aigne.js +5 -4
- package/lib/cjs/aigne/context.d.ts +18 -9
- package/lib/cjs/aigne/context.js +23 -12
- package/lib/cjs/aigne/message-queue.d.ts +6 -2
- package/lib/cjs/aigne/message-queue.js +2 -2
- package/lib/cjs/index.d.ts +3 -1
- package/lib/cjs/index.js +3 -1
- package/lib/cjs/loader/agent-js.d.ts +1 -1
- package/lib/cjs/loader/agent-yaml.d.ts +3 -2
- package/lib/cjs/loader/agent-yaml.js +3 -2
- package/lib/cjs/loader/index.d.ts +5 -1
- package/lib/cjs/loader/index.js +25 -16
- package/lib/cjs/memory/memory.d.ts +3 -2
- package/lib/cjs/memory/memory.js +1 -1
- package/lib/cjs/memory/retriever.d.ts +2 -2
- package/lib/cjs/prompt/prompt-builder.d.ts +9 -7
- package/lib/cjs/prompt/prompt-builder.js +14 -10
- package/lib/cjs/prompt/template.js +1 -3
- package/lib/cjs/utils/logger.js +3 -3
- package/lib/cjs/utils/nodejs.d.ts +8 -0
- package/lib/cjs/utils/nodejs.js +24 -0
- package/lib/cjs/utils/type-utils.d.ts +2 -2
- package/lib/dts/agents/agent.d.ts +69 -37
- package/lib/dts/agents/ai-agent.d.ts +4 -7
- package/lib/dts/agents/chat-model.d.ts +7 -8
- package/lib/dts/agents/mcp-agent.d.ts +4 -5
- package/lib/dts/agents/team-agent.d.ts +7 -8
- package/lib/dts/agents/user-agent.d.ts +4 -5
- package/lib/dts/aigne/aigne.d.ts +10 -9
- package/lib/dts/aigne/context.d.ts +18 -9
- package/lib/dts/aigne/message-queue.d.ts +6 -2
- package/lib/dts/index.d.ts +3 -1
- package/lib/dts/loader/agent-js.d.ts +1 -1
- package/lib/dts/loader/agent-yaml.d.ts +3 -2
- package/lib/dts/loader/index.d.ts +5 -1
- package/lib/dts/memory/memory.d.ts +3 -2
- package/lib/dts/memory/retriever.d.ts +2 -2
- package/lib/dts/prompt/prompt-builder.d.ts +9 -7
- package/lib/dts/utils/nodejs.d.ts +8 -0
- package/lib/dts/utils/type-utils.d.ts +2 -2
- package/lib/esm/agents/agent.d.ts +69 -37
- package/lib/esm/agents/agent.js +94 -66
- package/lib/esm/agents/ai-agent.d.ts +4 -7
- package/lib/esm/agents/ai-agent.js +10 -19
- package/lib/esm/agents/chat-model.d.ts +7 -8
- package/lib/esm/agents/chat-model.js +9 -9
- package/lib/esm/agents/mcp-agent.d.ts +4 -5
- package/lib/esm/agents/mcp-agent.js +7 -13
- package/lib/esm/agents/team-agent.d.ts +7 -8
- package/lib/esm/agents/team-agent.js +10 -10
- package/lib/esm/agents/user-agent.d.ts +4 -5
- package/lib/esm/agents/user-agent.js +10 -11
- package/lib/esm/aigne/aigne.d.ts +10 -9
- package/lib/esm/aigne/aigne.js +5 -4
- package/lib/esm/aigne/context.d.ts +18 -9
- package/lib/esm/aigne/context.js +24 -10
- package/lib/esm/aigne/message-queue.d.ts +6 -2
- package/lib/esm/aigne/message-queue.js +2 -2
- package/lib/esm/index.d.ts +3 -1
- package/lib/esm/index.js +3 -1
- package/lib/esm/loader/agent-js.d.ts +1 -1
- package/lib/esm/loader/agent-yaml.d.ts +3 -2
- package/lib/esm/loader/agent-yaml.js +3 -2
- package/lib/esm/loader/index.d.ts +5 -1
- package/lib/esm/loader/index.js +25 -16
- package/lib/esm/memory/memory.d.ts +3 -2
- package/lib/esm/memory/memory.js +2 -2
- package/lib/esm/memory/retriever.d.ts +2 -2
- package/lib/esm/prompt/prompt-builder.d.ts +9 -7
- package/lib/esm/prompt/prompt-builder.js +15 -11
- package/lib/esm/prompt/template.js +1 -3
- package/lib/esm/utils/logger.js +3 -3
- package/lib/esm/utils/nodejs.d.ts +8 -0
- package/lib/esm/utils/nodejs.js +21 -0
- package/lib/esm/utils/type-utils.d.ts +2 -2
- package/package.json +4 -3
- package/lib/cjs/memory/default-memory.d.ts +0 -16
- package/lib/cjs/memory/default-memory.js +0 -70
- package/lib/cjs/utils/fs.d.ts +0 -2
- package/lib/cjs/utils/fs.js +0 -25
- package/lib/dts/memory/default-memory.d.ts +0 -16
- package/lib/dts/utils/fs.d.ts +0 -2
- package/lib/esm/memory/default-memory.d.ts +0 -16
- package/lib/esm/memory/default-memory.js +0 -63
- package/lib/esm/utils/fs.d.ts +0 -2
- package/lib/esm/utils/fs.js +0 -21
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import { inspect } from "node:util";
|
|
2
1
|
import { ZodObject, type ZodType } from "zod";
|
|
3
|
-
import type { Context } from "../aigne/context.js";
|
|
2
|
+
import type { Context, UserContext } from "../aigne/context.js";
|
|
4
3
|
import type { MessagePayload } from "../aigne/message-queue.js";
|
|
5
|
-
import type { MemoryAgent } from "../memory/memory.js";
|
|
4
|
+
import type { Memory, MemoryAgent } from "../memory/memory.js";
|
|
5
|
+
import type { MemoryRecorderInput } from "../memory/recorder.js";
|
|
6
|
+
import type { MemoryRetrieverInput } from "../memory/retriever.js";
|
|
7
|
+
import { nodejs } from "../utils/nodejs.js";
|
|
6
8
|
import { type Nullish, type PromiseOrValue, type XOr } from "../utils/type-utils.js";
|
|
7
9
|
import type { GuideRailAgent, GuideRailAgentOutput } from "./guide-rail-agent.js";
|
|
8
10
|
import { type TransferAgentOutput } from "./types.js";
|
|
@@ -95,11 +97,29 @@ export interface AgentOptions<I extends Message = Message, O extends Message = M
|
|
|
95
97
|
* One or more memory agents this agent can use
|
|
96
98
|
*/
|
|
97
99
|
memory?: MemoryAgent | MemoryAgent[];
|
|
100
|
+
/**
|
|
101
|
+
* Maximum number of memory items to retrieve
|
|
102
|
+
*/
|
|
103
|
+
maxRetrieveMemoryCount?: number;
|
|
98
104
|
}
|
|
99
105
|
export declare const agentOptionsSchema: ZodObject<{
|
|
100
106
|
[key in keyof AgentOptions]: ZodType<AgentOptions[key]>;
|
|
101
107
|
}>;
|
|
102
|
-
export interface AgentInvokeOptions {
|
|
108
|
+
export interface AgentInvokeOptions<U extends UserContext = UserContext> {
|
|
109
|
+
/**
|
|
110
|
+
* The execution context for the agent
|
|
111
|
+
*
|
|
112
|
+
* The context provides the runtime environment for agent execution, including:
|
|
113
|
+
* - Event emission and subscription management
|
|
114
|
+
* - Inter-agent communication and message passing
|
|
115
|
+
* - Resource usage tracking and limits enforcement
|
|
116
|
+
* - Timeout and status management
|
|
117
|
+
* - Memory and state management across agent invocations
|
|
118
|
+
*
|
|
119
|
+
* Each agent invocation requires a context to coordinate with the broader
|
|
120
|
+
* agent system and maintain proper isolation and resource control.
|
|
121
|
+
*/
|
|
122
|
+
context: Context<U>;
|
|
103
123
|
/**
|
|
104
124
|
* Whether to enable streaming response
|
|
105
125
|
*
|
|
@@ -111,6 +131,8 @@ export interface AgentInvokeOptions {
|
|
|
111
131
|
* and returns the final JSON result
|
|
112
132
|
*/
|
|
113
133
|
streaming?: boolean;
|
|
134
|
+
userContext?: U;
|
|
135
|
+
memories?: Pick<Memory, "content">[];
|
|
114
136
|
}
|
|
115
137
|
/**
|
|
116
138
|
* Agent is the base class for all agents.
|
|
@@ -135,11 +157,24 @@ export interface AgentInvokeOptions {
|
|
|
135
157
|
* {@includeCode ../../test/agents/agent.test.ts#example-custom-agent}
|
|
136
158
|
*/
|
|
137
159
|
export declare abstract class Agent<I extends Message = Message, O extends Message = Message> {
|
|
160
|
+
/**
|
|
161
|
+
* Custom object inspection behavior
|
|
162
|
+
*
|
|
163
|
+
* When using Node.js's util.inspect function to inspect an agent,
|
|
164
|
+
* only the agent's name will be shown, making output more concise
|
|
165
|
+
*
|
|
166
|
+
* @returns Agent name
|
|
167
|
+
*/
|
|
168
|
+
[nodejs.customInspect]: () => string;
|
|
138
169
|
constructor(options?: AgentOptions<I, O>);
|
|
139
170
|
/**
|
|
140
171
|
* List of memories this agent can use
|
|
141
172
|
*/
|
|
142
173
|
readonly memories: MemoryAgent[];
|
|
174
|
+
/**
|
|
175
|
+
* Maximum number of memory items to retrieve
|
|
176
|
+
*/
|
|
177
|
+
maxRetrieveMemoryCount?: number;
|
|
143
178
|
/**
|
|
144
179
|
* Lifecycle hooks for agent processing.
|
|
145
180
|
*
|
|
@@ -278,11 +313,15 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
278
313
|
/**
|
|
279
314
|
* Check context status to ensure it hasn't timed out
|
|
280
315
|
*
|
|
281
|
-
* @param
|
|
316
|
+
* @param options Invocation options containing context
|
|
282
317
|
* @throws Error if the context has timed out
|
|
283
318
|
*/
|
|
284
319
|
private checkContextStatus;
|
|
285
320
|
private newDefaultContext;
|
|
321
|
+
retrieveMemories(input: Pick<MemoryRetrieverInput, "limit"> & {
|
|
322
|
+
search?: Message | string;
|
|
323
|
+
}, options: Pick<AgentInvokeOptions, "context">): Promise<Pick<Memory, "content">[]>;
|
|
324
|
+
recordMemories(input: MemoryRecorderInput, options: Pick<AgentInvokeOptions, "context">): Promise<void>;
|
|
286
325
|
/**
|
|
287
326
|
* Invoke the agent with regular (non-streaming) response
|
|
288
327
|
*
|
|
@@ -290,7 +329,6 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
290
329
|
* suitable for scenarios where a complete result is needed at once.
|
|
291
330
|
*
|
|
292
331
|
* @param input Input message to the agent, can be a string or structured object
|
|
293
|
-
* @param context Execution context, providing environment and resource access
|
|
294
332
|
* @param options Invocation options, must set streaming to false or leave unset
|
|
295
333
|
* @returns Final JSON response
|
|
296
334
|
*
|
|
@@ -298,7 +336,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
298
336
|
* Here's an example of invoking an agent with regular mode:
|
|
299
337
|
* {@includeCode ../../test/agents/agent.test.ts#example-invoke}
|
|
300
338
|
*/
|
|
301
|
-
invoke(input: I | string,
|
|
339
|
+
invoke(input: I | string, options?: Partial<AgentInvokeOptions> & {
|
|
302
340
|
streaming?: false;
|
|
303
341
|
}): Promise<O>;
|
|
304
342
|
/**
|
|
@@ -309,7 +347,6 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
309
347
|
* chat bot typing effects.
|
|
310
348
|
*
|
|
311
349
|
* @param input Input message to the agent, can be a string or structured object
|
|
312
|
-
* @param context Execution context, providing environment and resource access
|
|
313
350
|
* @param options Invocation options, must set streaming to true for this overload
|
|
314
351
|
* @returns Streaming response object
|
|
315
352
|
*
|
|
@@ -317,7 +354,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
317
354
|
* Here's an example of invoking an agent with streaming response:
|
|
318
355
|
* {@includeCode ../../test/agents/agent.test.ts#example-invoke-streaming}
|
|
319
356
|
*/
|
|
320
|
-
invoke(input: I | string,
|
|
357
|
+
invoke(input: I | string, options: Partial<AgentInvokeOptions> & {
|
|
321
358
|
streaming: true;
|
|
322
359
|
}): Promise<AgentResponseStream<O>>;
|
|
323
360
|
/**
|
|
@@ -326,12 +363,11 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
326
363
|
* Returns either streaming or regular response based on the streaming parameter in options
|
|
327
364
|
*
|
|
328
365
|
* @param input Input message to the agent
|
|
329
|
-
* @param context Execution context
|
|
330
366
|
* @param options Invocation options
|
|
331
367
|
* @returns Agent response (streaming or regular)
|
|
332
368
|
*/
|
|
333
|
-
invoke(input: I | string,
|
|
334
|
-
protected invokeSkill<I extends Message, O extends Message>(skill: Agent<I, O>, input: I,
|
|
369
|
+
invoke(input: I | string, options?: Partial<AgentInvokeOptions>): Promise<AgentResponse<O>>;
|
|
370
|
+
protected invokeSkill<I extends Message, O extends Message>(skill: Agent<I, O>, input: I, options: AgentInvokeOptions): Promise<O>;
|
|
335
371
|
/**
|
|
336
372
|
* Process agent output
|
|
337
373
|
*
|
|
@@ -339,7 +375,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
339
375
|
*
|
|
340
376
|
* @param input Original input message
|
|
341
377
|
* @param output Raw output produced by the agent
|
|
342
|
-
* @param
|
|
378
|
+
* @param options Invocation options
|
|
343
379
|
* @returns Final processed output
|
|
344
380
|
*/
|
|
345
381
|
private processAgentOutput;
|
|
@@ -349,7 +385,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
349
385
|
* Logs error information, triggers failure events, and re-throws the error
|
|
350
386
|
*
|
|
351
387
|
* @param error Caught error
|
|
352
|
-
* @param
|
|
388
|
+
* @param options Invocation options
|
|
353
389
|
*/
|
|
354
390
|
private processAgentError;
|
|
355
391
|
/**
|
|
@@ -358,10 +394,10 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
358
394
|
* If the context has a maximum invocation limit set, checks if the limit
|
|
359
395
|
* has been exceeded and increments the invocation counter
|
|
360
396
|
*
|
|
361
|
-
* @param context
|
|
397
|
+
* @param options Invocation options containing context and limits
|
|
362
398
|
* @throws Error if maximum invocation limit is exceeded
|
|
363
399
|
*/
|
|
364
|
-
protected checkAgentInvokesUsage(
|
|
400
|
+
protected checkAgentInvokesUsage(options: AgentInvokeOptions): void;
|
|
365
401
|
/**
|
|
366
402
|
* Pre-processing operations before handling input
|
|
367
403
|
*
|
|
@@ -370,9 +406,9 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
370
406
|
* - Verifying invocation limits
|
|
371
407
|
*
|
|
372
408
|
* @param _ Input message (unused)
|
|
373
|
-
* @param
|
|
409
|
+
* @param options Options for agent invocation
|
|
374
410
|
*/
|
|
375
|
-
protected preprocess(_: I,
|
|
411
|
+
protected preprocess(_: I, options: AgentInvokeOptions): Promise<void>;
|
|
376
412
|
private checkResponseByGuideRails;
|
|
377
413
|
private runGuideRails;
|
|
378
414
|
/**
|
|
@@ -400,10 +436,10 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
400
436
|
*
|
|
401
437
|
* @param input Input message
|
|
402
438
|
* @param output Output message
|
|
403
|
-
* @param
|
|
439
|
+
* @param options Options for agent invocation
|
|
404
440
|
*/
|
|
405
|
-
protected postprocess(input: I, output: O,
|
|
406
|
-
protected publishToTopics(output: Message,
|
|
441
|
+
protected postprocess(input: I, output: O, options: AgentInvokeOptions): Promise<void>;
|
|
442
|
+
protected publishToTopics(output: Message, options: AgentInvokeOptions): Promise<void>;
|
|
407
443
|
/**
|
|
408
444
|
* Core processing method of the agent, must be implemented in subclasses
|
|
409
445
|
*
|
|
@@ -415,7 +451,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
415
451
|
* - Another agent instance (transfer agent)
|
|
416
452
|
*
|
|
417
453
|
* @param input Input message
|
|
418
|
-
* @param
|
|
454
|
+
* @param options Options for agent invocation
|
|
419
455
|
* @returns Processing result
|
|
420
456
|
*
|
|
421
457
|
* @example
|
|
@@ -434,7 +470,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
434
470
|
* Example of transfer to another agent:
|
|
435
471
|
* {@includeCode ../../test/agents/agent.test.ts#example-process-transfer}
|
|
436
472
|
*/
|
|
437
|
-
abstract process(input: I,
|
|
473
|
+
abstract process(input: I, options: AgentInvokeOptions): PromiseOrValue<AgentProcessResult<O>>;
|
|
438
474
|
/**
|
|
439
475
|
* Shut down the agent and clean up resources
|
|
440
476
|
*
|
|
@@ -449,15 +485,6 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
449
485
|
* {@includeCode ../../test/agents/agent.test.ts#example-agent-shutdown-by-using}
|
|
450
486
|
*/
|
|
451
487
|
shutdown(): Promise<void>;
|
|
452
|
-
/**
|
|
453
|
-
* Custom object inspection behavior
|
|
454
|
-
*
|
|
455
|
-
* When using Node.js's util.inspect function to inspect an agent,
|
|
456
|
-
* only the agent's name will be shown, making output more concise
|
|
457
|
-
*
|
|
458
|
-
* @returns Agent name
|
|
459
|
-
*/
|
|
460
|
-
[inspect.custom](): string;
|
|
461
488
|
/**
|
|
462
489
|
* Async dispose method for shutdown the agent
|
|
463
490
|
*
|
|
@@ -484,6 +511,7 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
|
|
|
484
511
|
* @param event Object containing the input message
|
|
485
512
|
*/
|
|
486
513
|
onStart?: (event: {
|
|
514
|
+
context: Context;
|
|
487
515
|
input: I;
|
|
488
516
|
}) => PromiseOrValue<void>;
|
|
489
517
|
/**
|
|
@@ -496,6 +524,7 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
|
|
|
496
524
|
* @param event Object containing the input message and either output or error
|
|
497
525
|
*/
|
|
498
526
|
onEnd?: (event: XOr<{
|
|
527
|
+
context: Context;
|
|
499
528
|
input: I;
|
|
500
529
|
output: O;
|
|
501
530
|
error: Error;
|
|
@@ -509,6 +538,7 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
|
|
|
509
538
|
* @param event Object containing the skill being used and input message
|
|
510
539
|
*/
|
|
511
540
|
onSkillStart?: (event: {
|
|
541
|
+
context: Context;
|
|
512
542
|
skill: Agent;
|
|
513
543
|
input: I;
|
|
514
544
|
}) => PromiseOrValue<void>;
|
|
@@ -522,6 +552,7 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
|
|
|
522
552
|
* @param event Object containing the skill used, input message, and either output or error
|
|
523
553
|
*/
|
|
524
554
|
onSkillEnd?: (event: XOr<{
|
|
555
|
+
context: Context;
|
|
525
556
|
skill: Agent;
|
|
526
557
|
input: I;
|
|
527
558
|
output: O;
|
|
@@ -537,6 +568,7 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
|
|
|
537
568
|
* @param event Object containing the source agent, target agent, and input message
|
|
538
569
|
*/
|
|
539
570
|
onHandoff?: (event: {
|
|
571
|
+
context: Context;
|
|
540
572
|
source: Agent;
|
|
541
573
|
target: Agent;
|
|
542
574
|
input: I;
|
|
@@ -587,7 +619,7 @@ export interface AgentResponseDelta<T> {
|
|
|
587
619
|
}> | Partial<{
|
|
588
620
|
[key: string]: string;
|
|
589
621
|
}>;
|
|
590
|
-
json?: Partial<T | TransferAgentOutput
|
|
622
|
+
json?: Partial<T> | TransferAgentOutput;
|
|
591
623
|
};
|
|
592
624
|
}
|
|
593
625
|
/**
|
|
@@ -619,7 +651,7 @@ export declare function jsonDelta<T extends Message>(jsonDelta: NonNullable<Agen
|
|
|
619
651
|
*
|
|
620
652
|
* @template O Agent output message type
|
|
621
653
|
*/
|
|
622
|
-
export type AgentProcessAsyncGenerator<O extends Message> = AsyncGenerator<AgentResponseChunk<O>, Partial<O | TransferAgentOutput
|
|
654
|
+
export type AgentProcessAsyncGenerator<O extends Message> = AsyncGenerator<AgentResponseChunk<O>, Partial<O> | TransferAgentOutput | undefined | void>;
|
|
623
655
|
/**
|
|
624
656
|
* Result type for agent processing method, can be:
|
|
625
657
|
* - Direct or streaming response
|
|
@@ -709,10 +741,10 @@ export declare class FunctionAgent<I extends Message = Message, O extends Messag
|
|
|
709
741
|
* Process input implementation, calls the configured processing function
|
|
710
742
|
*
|
|
711
743
|
* @param input Input message
|
|
712
|
-
* @param
|
|
744
|
+
* @param options Invocation options
|
|
713
745
|
* @returns Processing result
|
|
714
746
|
*/
|
|
715
|
-
process(input: I,
|
|
747
|
+
process(input: I, options: AgentInvokeOptions): PromiseOrValue<AgentProcessResult<O>>;
|
|
716
748
|
}
|
|
717
749
|
/**
|
|
718
750
|
* Function type for function agents
|
|
@@ -725,4 +757,4 @@ export declare class FunctionAgent<I extends Message = Message, O extends Messag
|
|
|
725
757
|
* @param context Execution context
|
|
726
758
|
* @returns Processing result, can be synchronous or asynchronous
|
|
727
759
|
*/
|
|
728
|
-
export type FunctionAgentFn<I extends Message = any, O extends Message = any> = (input: I,
|
|
760
|
+
export type FunctionAgentFn<I extends Message = any, O extends Message = any> = (input: I, options: AgentInvokeOptions) => PromiseOrValue<AgentProcessResult<O>>;
|
package/lib/esm/agents/agent.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { inspect } from "node:util";
|
|
2
1
|
import { ZodObject, z } from "zod";
|
|
3
|
-
import { createMessage } from "../prompt/prompt-builder.js";
|
|
2
|
+
import { createMessage, getMessage } from "../prompt/prompt-builder.js";
|
|
4
3
|
import { logger } from "../utils/logger.js";
|
|
4
|
+
import { nodejs } from "../utils/nodejs.js";
|
|
5
5
|
import { agentResponseStreamToObject, asyncGeneratorToReadableStream, isAsyncGenerator, objectToAgentResponseStream, onAgentResponseStreamEnd, } from "../utils/stream-utils.js";
|
|
6
6
|
import { checkArguments, createAccessorArray, isEmpty, orArrayToArray, } from "../utils/type-utils.js";
|
|
7
7
|
import { replaceTransferAgentToName, transferToAgentOutput, } from "./types.js";
|
|
@@ -19,6 +19,7 @@ export const agentOptionsSchema = z.object({
|
|
|
19
19
|
skills: z.array(z.union([z.custom(), z.custom()])).optional(),
|
|
20
20
|
disableEvents: z.boolean().optional(),
|
|
21
21
|
memory: z.union([z.custom(), z.array(z.custom())]).optional(),
|
|
22
|
+
maxRetrieveMemoryCount: z.number().optional(),
|
|
22
23
|
hooks: z
|
|
23
24
|
.object({
|
|
24
25
|
onStart: z.custom().optional(),
|
|
@@ -75,6 +76,7 @@ export class Agent {
|
|
|
75
76
|
else if (options.memory) {
|
|
76
77
|
this.memories.push(options.memory);
|
|
77
78
|
}
|
|
79
|
+
this.maxRetrieveMemoryCount = options.maxRetrieveMemoryCount;
|
|
78
80
|
this.hooks = options.hooks ?? {};
|
|
79
81
|
this.guideRails = options.guideRails;
|
|
80
82
|
}
|
|
@@ -82,6 +84,10 @@ export class Agent {
|
|
|
82
84
|
* List of memories this agent can use
|
|
83
85
|
*/
|
|
84
86
|
memories = [];
|
|
87
|
+
/**
|
|
88
|
+
* Maximum number of memory items to retrieve
|
|
89
|
+
*/
|
|
90
|
+
maxRetrieveMemoryCount;
|
|
85
91
|
/**
|
|
86
92
|
* Lifecycle hooks for agent processing.
|
|
87
93
|
*
|
|
@@ -250,68 +256,92 @@ export class Agent {
|
|
|
250
256
|
/**
|
|
251
257
|
* Check context status to ensure it hasn't timed out
|
|
252
258
|
*
|
|
253
|
-
* @param
|
|
259
|
+
* @param options Invocation options containing context
|
|
254
260
|
* @throws Error if the context has timed out
|
|
255
261
|
*/
|
|
256
|
-
checkContextStatus(
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
throw new Error(`AIGNE for agent ${this.name} has timed out`);
|
|
261
|
-
}
|
|
262
|
+
checkContextStatus(options) {
|
|
263
|
+
const { status } = options.context;
|
|
264
|
+
if (status === "timeout") {
|
|
265
|
+
throw new Error(`AIGNE for agent ${this.name} has timed out`);
|
|
262
266
|
}
|
|
263
267
|
}
|
|
264
268
|
async newDefaultContext() {
|
|
265
269
|
return import("../aigne/context.js").then((m) => new m.AIGNEContext());
|
|
266
270
|
}
|
|
267
|
-
async
|
|
268
|
-
const
|
|
271
|
+
async retrieveMemories(input, options) {
|
|
272
|
+
const memories = [];
|
|
273
|
+
for (const memory of this.memories) {
|
|
274
|
+
const ms = (await memory.retrieve({
|
|
275
|
+
...input,
|
|
276
|
+
search: typeof input === "string" ? input : input && getMessage(input),
|
|
277
|
+
limit: input.limit ?? this.maxRetrieveMemoryCount,
|
|
278
|
+
}, options.context)).memories;
|
|
279
|
+
memories.push(...ms);
|
|
280
|
+
}
|
|
281
|
+
return memories;
|
|
282
|
+
}
|
|
283
|
+
async recordMemories(input, options) {
|
|
284
|
+
for (const memory of this.memories) {
|
|
285
|
+
if (memory.autoUpdate) {
|
|
286
|
+
await memory.record(input, options.context);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
async invoke(input, options = {}) {
|
|
291
|
+
const opts = {
|
|
292
|
+
...options,
|
|
293
|
+
context: options.context ?? (await this.newDefaultContext()),
|
|
294
|
+
};
|
|
295
|
+
if (options.userContext) {
|
|
296
|
+
Object.assign(opts.context.userContext, options.userContext);
|
|
297
|
+
}
|
|
269
298
|
const message = typeof input === "string" ? createMessage(input) : input;
|
|
270
299
|
logger.debug("Invoke agent %s started with input: %O", this.name, input);
|
|
271
300
|
if (!this.disableEvents)
|
|
272
|
-
|
|
301
|
+
opts.context.emit("agentStarted", { agent: this, input: message });
|
|
273
302
|
try {
|
|
274
|
-
await this.hooks.onStart?.({ input: message });
|
|
303
|
+
await this.hooks.onStart?.({ context: opts.context, input: message });
|
|
275
304
|
const parsedInput = checkArguments(`Agent ${this.name} input`, this.inputSchema, message);
|
|
276
|
-
await this.preprocess(parsedInput,
|
|
277
|
-
this.checkContextStatus(
|
|
278
|
-
let response = await this.process(parsedInput,
|
|
305
|
+
await this.preprocess(parsedInput, opts);
|
|
306
|
+
this.checkContextStatus(opts);
|
|
307
|
+
let response = await this.process(parsedInput, opts);
|
|
279
308
|
if (response instanceof Agent) {
|
|
280
309
|
response = transferToAgentOutput(response);
|
|
281
310
|
}
|
|
282
|
-
if (
|
|
311
|
+
if (opts.streaming) {
|
|
283
312
|
const stream = response instanceof ReadableStream
|
|
284
313
|
? response
|
|
285
314
|
: isAsyncGenerator(response)
|
|
286
315
|
? asyncGeneratorToReadableStream(response)
|
|
287
316
|
: objectToAgentResponseStream(response);
|
|
288
317
|
return this.checkResponseByGuideRails(message, onAgentResponseStreamEnd(stream, async (result) => {
|
|
289
|
-
return await this.processAgentOutput(parsedInput, result,
|
|
318
|
+
return await this.processAgentOutput(parsedInput, result, opts);
|
|
290
319
|
}, {
|
|
291
320
|
errorCallback: async (error) => {
|
|
292
|
-
return await this.processAgentError(message, error,
|
|
321
|
+
return await this.processAgentError(message, error, opts);
|
|
293
322
|
},
|
|
294
|
-
}),
|
|
323
|
+
}), opts);
|
|
295
324
|
}
|
|
296
325
|
return await this.checkResponseByGuideRails(message, this.processAgentOutput(parsedInput, response instanceof ReadableStream
|
|
297
326
|
? await agentResponseStreamToObject(response)
|
|
298
327
|
: isAsyncGenerator(response)
|
|
299
328
|
? await agentResponseStreamToObject(response)
|
|
300
|
-
: response,
|
|
329
|
+
: response, opts), opts);
|
|
301
330
|
}
|
|
302
331
|
catch (error) {
|
|
303
|
-
throw await this.processAgentError(message, error,
|
|
332
|
+
throw await this.processAgentError(message, error, opts);
|
|
304
333
|
}
|
|
305
334
|
}
|
|
306
|
-
async invokeSkill(skill, input,
|
|
307
|
-
|
|
335
|
+
async invokeSkill(skill, input, options) {
|
|
336
|
+
const { context } = options;
|
|
337
|
+
await this.hooks.onSkillStart?.({ context, skill, input });
|
|
308
338
|
try {
|
|
309
339
|
const output = await context.invoke(skill, input);
|
|
310
|
-
await this.hooks.onSkillEnd?.({ skill, input, output });
|
|
340
|
+
await this.hooks.onSkillEnd?.({ context, skill, input, output });
|
|
311
341
|
return output;
|
|
312
342
|
}
|
|
313
343
|
catch (error) {
|
|
314
|
-
await this.hooks.onSkillEnd?.({ skill, input, error });
|
|
344
|
+
await this.hooks.onSkillEnd?.({ context, skill, input, error });
|
|
315
345
|
throw error;
|
|
316
346
|
}
|
|
317
347
|
}
|
|
@@ -322,17 +352,18 @@ export class Agent {
|
|
|
322
352
|
*
|
|
323
353
|
* @param input Original input message
|
|
324
354
|
* @param output Raw output produced by the agent
|
|
325
|
-
* @param
|
|
355
|
+
* @param options Invocation options
|
|
326
356
|
* @returns Final processed output
|
|
327
357
|
*/
|
|
328
|
-
async processAgentOutput(input, output,
|
|
358
|
+
async processAgentOutput(input, output, options) {
|
|
359
|
+
const { context } = options;
|
|
329
360
|
const parsedOutput = checkArguments(`Agent ${this.name} output`, this.outputSchema, output);
|
|
330
361
|
const finalOutput = this.includeInputInOutput ? { ...input, ...parsedOutput } : parsedOutput;
|
|
331
|
-
await this.postprocess(input, finalOutput,
|
|
362
|
+
await this.postprocess(input, finalOutput, options);
|
|
332
363
|
logger.debug("Invoke agent %s succeed with output: %O", this.name, finalOutput);
|
|
333
364
|
if (!this.disableEvents)
|
|
334
365
|
context.emit("agentSucceed", { agent: this, output: finalOutput });
|
|
335
|
-
await this.hooks.onEnd?.({ input, output: finalOutput });
|
|
366
|
+
await this.hooks.onEnd?.({ context, input, output: finalOutput });
|
|
336
367
|
return finalOutput;
|
|
337
368
|
}
|
|
338
369
|
/**
|
|
@@ -341,13 +372,14 @@ export class Agent {
|
|
|
341
372
|
* Logs error information, triggers failure events, and re-throws the error
|
|
342
373
|
*
|
|
343
374
|
* @param error Caught error
|
|
344
|
-
* @param
|
|
375
|
+
* @param options Invocation options
|
|
345
376
|
*/
|
|
346
|
-
async processAgentError(input, error,
|
|
377
|
+
async processAgentError(input, error, options) {
|
|
347
378
|
logger.error("Invoke agent %s failed with error: %O", this.name, error);
|
|
348
379
|
if (!this.disableEvents)
|
|
349
|
-
context.emit("agentFailed", { agent: this, error });
|
|
350
|
-
|
|
380
|
+
options.context.emit("agentFailed", { agent: this, error });
|
|
381
|
+
const { context } = options;
|
|
382
|
+
await this.hooks.onEnd?.({ context, input, error });
|
|
351
383
|
return error;
|
|
352
384
|
}
|
|
353
385
|
/**
|
|
@@ -356,11 +388,11 @@ export class Agent {
|
|
|
356
388
|
* If the context has a maximum invocation limit set, checks if the limit
|
|
357
389
|
* has been exceeded and increments the invocation counter
|
|
358
390
|
*
|
|
359
|
-
* @param context
|
|
391
|
+
* @param options Invocation options containing context and limits
|
|
360
392
|
* @throws Error if maximum invocation limit is exceeded
|
|
361
393
|
*/
|
|
362
|
-
checkAgentInvokesUsage(
|
|
363
|
-
const { limits, usage } = context;
|
|
394
|
+
checkAgentInvokesUsage(options) {
|
|
395
|
+
const { limits, usage } = options.context;
|
|
364
396
|
if (limits?.maxAgentInvokes && usage.agentCalls >= limits.maxAgentInvokes) {
|
|
365
397
|
throw new Error(`Exceeded max agent invokes ${usage.agentCalls}/${limits.maxAgentInvokes}`);
|
|
366
398
|
}
|
|
@@ -374,19 +406,19 @@ export class Agent {
|
|
|
374
406
|
* - Verifying invocation limits
|
|
375
407
|
*
|
|
376
408
|
* @param _ Input message (unused)
|
|
377
|
-
* @param
|
|
409
|
+
* @param options Options for agent invocation
|
|
378
410
|
*/
|
|
379
|
-
preprocess(_,
|
|
380
|
-
this.checkContextStatus(
|
|
381
|
-
this.checkAgentInvokesUsage(
|
|
411
|
+
async preprocess(_, options) {
|
|
412
|
+
this.checkContextStatus(options);
|
|
413
|
+
this.checkAgentInvokesUsage(options);
|
|
382
414
|
}
|
|
383
|
-
async checkResponseByGuideRails(input, output,
|
|
415
|
+
async checkResponseByGuideRails(input, output, options) {
|
|
384
416
|
if (!this.guideRails?.length)
|
|
385
417
|
return output;
|
|
386
418
|
const result = await output;
|
|
387
419
|
if (result instanceof ReadableStream) {
|
|
388
420
|
return onAgentResponseStreamEnd(result, async (result) => {
|
|
389
|
-
const error = await this.runGuideRails(input, result,
|
|
421
|
+
const error = await this.runGuideRails(input, result, options);
|
|
390
422
|
if (error) {
|
|
391
423
|
return {
|
|
392
424
|
...(await this.onGuideRailError(error)),
|
|
@@ -395,13 +427,13 @@ export class Agent {
|
|
|
395
427
|
}
|
|
396
428
|
});
|
|
397
429
|
}
|
|
398
|
-
const error = await this.runGuideRails(input, result,
|
|
430
|
+
const error = await this.runGuideRails(input, result, options);
|
|
399
431
|
if (!error)
|
|
400
432
|
return output;
|
|
401
433
|
return { ...(await this.onGuideRailError(error)), $status: "GuideRailError" };
|
|
402
434
|
}
|
|
403
|
-
async runGuideRails(input, output,
|
|
404
|
-
const result = await Promise.all((this.guideRails ?? []).map((i) => context.invoke(i, { input, output })));
|
|
435
|
+
async runGuideRails(input, output, options) {
|
|
436
|
+
const result = await Promise.all((this.guideRails ?? []).map((i) => options.context.invoke(i, { input, output })));
|
|
405
437
|
return result.find((i) => !!i.abort);
|
|
406
438
|
}
|
|
407
439
|
/**
|
|
@@ -431,26 +463,22 @@ export class Agent {
|
|
|
431
463
|
*
|
|
432
464
|
* @param input Input message
|
|
433
465
|
* @param output Output message
|
|
434
|
-
* @param
|
|
466
|
+
* @param options Options for agent invocation
|
|
435
467
|
*/
|
|
436
|
-
postprocess(input, output,
|
|
437
|
-
this.checkContextStatus(
|
|
438
|
-
this.publishToTopics(output,
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
],
|
|
446
|
-
}, context);
|
|
447
|
-
}
|
|
448
|
-
}
|
|
468
|
+
async postprocess(input, output, options) {
|
|
469
|
+
this.checkContextStatus(options);
|
|
470
|
+
this.publishToTopics(output, options);
|
|
471
|
+
await this.recordMemories({
|
|
472
|
+
content: [
|
|
473
|
+
{ role: "user", content: input },
|
|
474
|
+
{ role: "agent", content: replaceTransferAgentToName(output), source: this.name },
|
|
475
|
+
],
|
|
476
|
+
}, options);
|
|
449
477
|
}
|
|
450
|
-
async publishToTopics(output,
|
|
478
|
+
async publishToTopics(output, options) {
|
|
451
479
|
const publishTopics = typeof this.publishTopic === "function" ? await this.publishTopic(output) : this.publishTopic;
|
|
452
480
|
if (publishTopics?.length) {
|
|
453
|
-
context.publish(publishTopics, {
|
|
481
|
+
options.context.publish(publishTopics, {
|
|
454
482
|
role: this.constructor.name === "UserAgent" ? "user" : "agent",
|
|
455
483
|
source: this.name,
|
|
456
484
|
message: output,
|
|
@@ -487,7 +515,7 @@ export class Agent {
|
|
|
487
515
|
*
|
|
488
516
|
* @returns Agent name
|
|
489
517
|
*/
|
|
490
|
-
[
|
|
518
|
+
[nodejs.customInspect]() {
|
|
491
519
|
return this.name;
|
|
492
520
|
}
|
|
493
521
|
/**
|
|
@@ -602,11 +630,11 @@ export class FunctionAgent extends Agent {
|
|
|
602
630
|
* Process input implementation, calls the configured processing function
|
|
603
631
|
*
|
|
604
632
|
* @param input Input message
|
|
605
|
-
* @param
|
|
633
|
+
* @param options Invocation options
|
|
606
634
|
* @returns Processing result
|
|
607
635
|
*/
|
|
608
|
-
process(input,
|
|
609
|
-
return this._process(input,
|
|
636
|
+
process(input, options) {
|
|
637
|
+
return this._process(input, options);
|
|
610
638
|
}
|
|
611
639
|
}
|
|
612
640
|
function functionToAgent(agent) {
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { type ZodObject, type ZodType, z } from "zod";
|
|
2
|
-
import type { Context } from "../aigne/context.js";
|
|
3
|
-
import { type DefaultMemoryOptions } from "../memory/default-memory.js";
|
|
4
2
|
import { PromptBuilder } from "../prompt/prompt-builder.js";
|
|
5
|
-
import { Agent, type AgentOptions, type AgentProcessAsyncGenerator, type Message } from "./agent.js";
|
|
3
|
+
import { Agent, type AgentInvokeOptions, type AgentOptions, type AgentProcessAsyncGenerator, type Message } from "./agent.js";
|
|
6
4
|
import { ChatModel, type ChatModelInput } from "./chat-model.js";
|
|
7
5
|
import type { GuideRailAgentOutput } from "./guide-rail-agent.js";
|
|
8
6
|
/**
|
|
@@ -14,7 +12,7 @@ import type { GuideRailAgentOutput } from "./guide-rail-agent.js";
|
|
|
14
12
|
* @template I The input message type the agent accepts
|
|
15
13
|
* @template O The output message type the agent returns
|
|
16
14
|
*/
|
|
17
|
-
export interface AIAgentOptions<I extends Message = Message, O extends Message = Message> extends
|
|
15
|
+
export interface AIAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
|
|
18
16
|
/**
|
|
19
17
|
* The language model to use for this agent
|
|
20
18
|
*
|
|
@@ -66,7 +64,6 @@ export interface AIAgentOptions<I extends Message = Message, O extends Message =
|
|
|
66
64
|
* The template receives a {{memories}} variable containing serialized memory content.
|
|
67
65
|
*/
|
|
68
66
|
memoryPromptTemplate?: string;
|
|
69
|
-
memory?: AgentOptions<I, O>["memory"] | DefaultMemoryOptions | true;
|
|
70
67
|
}
|
|
71
68
|
/**
|
|
72
69
|
* Tool choice options for AI agents
|
|
@@ -215,7 +212,7 @@ export declare class AIAgent<I extends Message = Message, O extends Message = Me
|
|
|
215
212
|
*
|
|
216
213
|
* @protected
|
|
217
214
|
*/
|
|
218
|
-
process(input: I,
|
|
215
|
+
process(input: I, options: AgentInvokeOptions): AgentProcessAsyncGenerator<O>;
|
|
219
216
|
protected onGuideRailError(error: GuideRailAgentOutput): Promise<O | GuideRailAgentOutput>;
|
|
220
217
|
/**
|
|
221
218
|
* Process router mode requests
|
|
@@ -225,5 +222,5 @@ export declare class AIAgent<I extends Message = Message, O extends Message = Me
|
|
|
225
222
|
*
|
|
226
223
|
* @protected
|
|
227
224
|
*/
|
|
228
|
-
_processRouter(input: I, model: ChatModel, modelInput: ChatModelInput,
|
|
225
|
+
_processRouter(input: I, model: ChatModel, modelInput: ChatModelInput, options: AgentInvokeOptions, toolsMap: Map<string, Agent>): AgentProcessAsyncGenerator<O>;
|
|
229
226
|
}
|