@aigne/core 1.16.0 → 1.17.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 +7 -0
- package/lib/cjs/agents/agent.d.ts +42 -26
- package/lib/cjs/agents/agent.js +59 -55
- package/lib/cjs/agents/ai-agent.d.ts +4 -5
- package/lib/cjs/agents/ai-agent.js +11 -11
- 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 +3 -4
- package/lib/cjs/agents/mcp-agent.js +2 -2
- 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 -4
- package/lib/cjs/agents/user-agent.js +10 -10
- package/lib/cjs/aigne/aigne.d.ts +10 -9
- package/lib/cjs/aigne/aigne.js +5 -4
- package/lib/cjs/aigne/context.d.ts +17 -7
- package/lib/cjs/aigne/context.js +20 -7
- package/lib/cjs/index.d.ts +2 -1
- package/lib/cjs/index.js +2 -1
- package/lib/cjs/loader/agent-yaml.d.ts +1 -1
- package/lib/cjs/memory/default-memory/default-memory-storage/index.d.ts +30 -0
- package/lib/cjs/memory/default-memory/default-memory-storage/index.js +69 -0
- package/lib/cjs/memory/default-memory/default-memory-storage/migrate.d.ts +7 -0
- package/lib/cjs/memory/default-memory/default-memory-storage/migrate.js +53 -0
- package/lib/cjs/memory/default-memory/default-memory-storage/migrations/20250523165801-init.d.ts +7 -0
- package/lib/cjs/memory/default-memory/default-memory-storage/migrations/20250523165801-init.js +33 -0
- package/lib/cjs/memory/default-memory/default-memory-storage/models/memory.d.ts +10 -0
- package/lib/cjs/memory/default-memory/default-memory-storage/models/memory.js +32 -0
- package/lib/cjs/memory/default-memory/index.d.ts +10 -0
- package/lib/cjs/memory/{default-memory.js → default-memory/index.js} +16 -25
- package/lib/cjs/memory/default-memory/storage.d.ts +13 -0
- package/lib/cjs/memory/default-memory/storage.js +6 -0
- package/lib/cjs/memory/memory.d.ts +3 -2
- package/lib/cjs/memory/memory.js +1 -1
- package/lib/cjs/prompt/prompt-builder.d.ts +7 -1
- package/lib/cjs/prompt/prompt-builder.js +4 -4
- package/lib/cjs/utils/type-utils.d.ts +2 -2
- package/lib/dts/agents/agent.d.ts +42 -26
- package/lib/dts/agents/ai-agent.d.ts +4 -5
- package/lib/dts/agents/chat-model.d.ts +7 -8
- package/lib/dts/agents/mcp-agent.d.ts +3 -4
- package/lib/dts/agents/team-agent.d.ts +7 -8
- package/lib/dts/agents/user-agent.d.ts +4 -4
- package/lib/dts/aigne/aigne.d.ts +10 -9
- package/lib/dts/aigne/context.d.ts +17 -7
- package/lib/dts/index.d.ts +2 -1
- package/lib/dts/loader/agent-yaml.d.ts +1 -1
- package/lib/dts/memory/default-memory/default-memory-storage/index.d.ts +30 -0
- package/lib/dts/memory/default-memory/default-memory-storage/migrate.d.ts +7 -0
- package/lib/dts/memory/default-memory/default-memory-storage/migrations/20250523165801-init.d.ts +7 -0
- package/lib/dts/memory/default-memory/default-memory-storage/models/memory.d.ts +10 -0
- package/lib/dts/memory/default-memory/index.d.ts +10 -0
- package/lib/dts/memory/default-memory/storage.d.ts +13 -0
- package/lib/dts/memory/memory.d.ts +3 -2
- package/lib/dts/prompt/prompt-builder.d.ts +7 -1
- package/lib/dts/utils/type-utils.d.ts +2 -2
- package/lib/esm/agents/agent.d.ts +42 -26
- package/lib/esm/agents/agent.js +59 -55
- package/lib/esm/agents/ai-agent.d.ts +4 -5
- package/lib/esm/agents/ai-agent.js +10 -10
- 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 +3 -4
- package/lib/esm/agents/mcp-agent.js +2 -2
- 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 -4
- package/lib/esm/agents/user-agent.js +10 -10
- package/lib/esm/aigne/aigne.d.ts +10 -9
- package/lib/esm/aigne/aigne.js +5 -4
- package/lib/esm/aigne/context.d.ts +17 -7
- package/lib/esm/aigne/context.js +21 -8
- package/lib/esm/index.d.ts +2 -1
- package/lib/esm/index.js +2 -1
- package/lib/esm/loader/agent-yaml.d.ts +1 -1
- package/lib/esm/memory/default-memory/default-memory-storage/index.d.ts +30 -0
- package/lib/esm/memory/default-memory/default-memory-storage/index.js +64 -0
- package/lib/esm/memory/default-memory/default-memory-storage/migrate.d.ts +7 -0
- package/lib/esm/memory/default-memory/default-memory-storage/migrate.js +16 -0
- package/lib/esm/memory/default-memory/default-memory-storage/migrations/20250523165801-init.d.ts +7 -0
- package/lib/esm/memory/default-memory/default-memory-storage/migrations/20250523165801-init.js +28 -0
- package/lib/esm/memory/default-memory/default-memory-storage/models/memory.d.ts +10 -0
- package/lib/esm/memory/default-memory/default-memory-storage/models/memory.js +29 -0
- package/lib/esm/memory/default-memory/index.d.ts +10 -0
- package/lib/esm/memory/{default-memory.js → default-memory/index.js} +15 -24
- package/lib/esm/memory/default-memory/storage.d.ts +13 -0
- package/lib/esm/memory/default-memory/storage.js +2 -0
- package/lib/esm/memory/memory.d.ts +3 -2
- package/lib/esm/memory/memory.js +2 -2
- package/lib/esm/prompt/prompt-builder.d.ts +7 -1
- package/lib/esm/prompt/prompt-builder.js +4 -4
- package/lib/esm/utils/type-utils.d.ts +2 -2
- package/package.json +4 -1
- package/lib/cjs/memory/default-memory.d.ts +0 -16
- package/lib/dts/memory/default-memory.d.ts +0 -16
- package/lib/esm/memory/default-memory.d.ts +0 -16
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { inspect } from "node:util";
|
|
2
2
|
import { ZodObject, type ZodType } from "zod";
|
|
3
|
-
import type { Context } from "../aigne/context.js";
|
|
3
|
+
import type { Context, UserContext } from "../aigne/context.js";
|
|
4
4
|
import type { MessagePayload } from "../aigne/message-queue.js";
|
|
5
5
|
import type { MemoryAgent } from "../memory/memory.js";
|
|
6
6
|
import { type Nullish, type PromiseOrValue, type XOr } from "../utils/type-utils.js";
|
|
@@ -99,7 +99,21 @@ export interface AgentOptions<I extends Message = Message, O extends Message = M
|
|
|
99
99
|
export declare const agentOptionsSchema: ZodObject<{
|
|
100
100
|
[key in keyof AgentOptions]: ZodType<AgentOptions[key]>;
|
|
101
101
|
}>;
|
|
102
|
-
export interface AgentInvokeOptions {
|
|
102
|
+
export interface AgentInvokeOptions<U extends UserContext = UserContext> {
|
|
103
|
+
/**
|
|
104
|
+
* The execution context for the agent
|
|
105
|
+
*
|
|
106
|
+
* The context provides the runtime environment for agent execution, including:
|
|
107
|
+
* - Event emission and subscription management
|
|
108
|
+
* - Inter-agent communication and message passing
|
|
109
|
+
* - Resource usage tracking and limits enforcement
|
|
110
|
+
* - Timeout and status management
|
|
111
|
+
* - Memory and state management across agent invocations
|
|
112
|
+
*
|
|
113
|
+
* Each agent invocation requires a context to coordinate with the broader
|
|
114
|
+
* agent system and maintain proper isolation and resource control.
|
|
115
|
+
*/
|
|
116
|
+
context: Context<U>;
|
|
103
117
|
/**
|
|
104
118
|
* Whether to enable streaming response
|
|
105
119
|
*
|
|
@@ -278,7 +292,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
278
292
|
/**
|
|
279
293
|
* Check context status to ensure it hasn't timed out
|
|
280
294
|
*
|
|
281
|
-
* @param
|
|
295
|
+
* @param options Invocation options containing context
|
|
282
296
|
* @throws Error if the context has timed out
|
|
283
297
|
*/
|
|
284
298
|
private checkContextStatus;
|
|
@@ -290,7 +304,6 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
290
304
|
* suitable for scenarios where a complete result is needed at once.
|
|
291
305
|
*
|
|
292
306
|
* @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
307
|
* @param options Invocation options, must set streaming to false or leave unset
|
|
295
308
|
* @returns Final JSON response
|
|
296
309
|
*
|
|
@@ -298,7 +311,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
298
311
|
* Here's an example of invoking an agent with regular mode:
|
|
299
312
|
* {@includeCode ../../test/agents/agent.test.ts#example-invoke}
|
|
300
313
|
*/
|
|
301
|
-
invoke(input: I | string,
|
|
314
|
+
invoke(input: I | string, options?: Partial<AgentInvokeOptions> & {
|
|
302
315
|
streaming?: false;
|
|
303
316
|
}): Promise<O>;
|
|
304
317
|
/**
|
|
@@ -309,7 +322,6 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
309
322
|
* chat bot typing effects.
|
|
310
323
|
*
|
|
311
324
|
* @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
325
|
* @param options Invocation options, must set streaming to true for this overload
|
|
314
326
|
* @returns Streaming response object
|
|
315
327
|
*
|
|
@@ -317,7 +329,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
317
329
|
* Here's an example of invoking an agent with streaming response:
|
|
318
330
|
* {@includeCode ../../test/agents/agent.test.ts#example-invoke-streaming}
|
|
319
331
|
*/
|
|
320
|
-
invoke(input: I | string,
|
|
332
|
+
invoke(input: I | string, options: Partial<AgentInvokeOptions> & {
|
|
321
333
|
streaming: true;
|
|
322
334
|
}): Promise<AgentResponseStream<O>>;
|
|
323
335
|
/**
|
|
@@ -326,12 +338,11 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
326
338
|
* Returns either streaming or regular response based on the streaming parameter in options
|
|
327
339
|
*
|
|
328
340
|
* @param input Input message to the agent
|
|
329
|
-
* @param context Execution context
|
|
330
341
|
* @param options Invocation options
|
|
331
342
|
* @returns Agent response (streaming or regular)
|
|
332
343
|
*/
|
|
333
|
-
invoke(input: I | string,
|
|
334
|
-
protected invokeSkill<I extends Message, O extends Message>(skill: Agent<I, O>, input: I,
|
|
344
|
+
invoke(input: I | string, options?: Partial<AgentInvokeOptions>): Promise<AgentResponse<O>>;
|
|
345
|
+
protected invokeSkill<I extends Message, O extends Message>(skill: Agent<I, O>, input: I, options: AgentInvokeOptions): Promise<O>;
|
|
335
346
|
/**
|
|
336
347
|
* Process agent output
|
|
337
348
|
*
|
|
@@ -339,7 +350,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
339
350
|
*
|
|
340
351
|
* @param input Original input message
|
|
341
352
|
* @param output Raw output produced by the agent
|
|
342
|
-
* @param
|
|
353
|
+
* @param options Invocation options
|
|
343
354
|
* @returns Final processed output
|
|
344
355
|
*/
|
|
345
356
|
private processAgentOutput;
|
|
@@ -349,7 +360,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
349
360
|
* Logs error information, triggers failure events, and re-throws the error
|
|
350
361
|
*
|
|
351
362
|
* @param error Caught error
|
|
352
|
-
* @param
|
|
363
|
+
* @param options Invocation options
|
|
353
364
|
*/
|
|
354
365
|
private processAgentError;
|
|
355
366
|
/**
|
|
@@ -358,10 +369,10 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
358
369
|
* If the context has a maximum invocation limit set, checks if the limit
|
|
359
370
|
* has been exceeded and increments the invocation counter
|
|
360
371
|
*
|
|
361
|
-
* @param context
|
|
372
|
+
* @param options Invocation options containing context and limits
|
|
362
373
|
* @throws Error if maximum invocation limit is exceeded
|
|
363
374
|
*/
|
|
364
|
-
protected checkAgentInvokesUsage(
|
|
375
|
+
protected checkAgentInvokesUsage(options: AgentInvokeOptions): void;
|
|
365
376
|
/**
|
|
366
377
|
* Pre-processing operations before handling input
|
|
367
378
|
*
|
|
@@ -370,9 +381,9 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
370
381
|
* - Verifying invocation limits
|
|
371
382
|
*
|
|
372
383
|
* @param _ Input message (unused)
|
|
373
|
-
* @param
|
|
384
|
+
* @param options Options for agent invocation
|
|
374
385
|
*/
|
|
375
|
-
protected preprocess(_: I,
|
|
386
|
+
protected preprocess(_: I, options: AgentInvokeOptions): Promise<void>;
|
|
376
387
|
private checkResponseByGuideRails;
|
|
377
388
|
private runGuideRails;
|
|
378
389
|
/**
|
|
@@ -400,10 +411,10 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
400
411
|
*
|
|
401
412
|
* @param input Input message
|
|
402
413
|
* @param output Output message
|
|
403
|
-
* @param
|
|
414
|
+
* @param options Options for agent invocation
|
|
404
415
|
*/
|
|
405
|
-
protected postprocess(input: I, output: O,
|
|
406
|
-
protected publishToTopics(output: Message,
|
|
416
|
+
protected postprocess(input: I, output: O, options: AgentInvokeOptions): Promise<void>;
|
|
417
|
+
protected publishToTopics(output: Message, options: AgentInvokeOptions): Promise<void>;
|
|
407
418
|
/**
|
|
408
419
|
* Core processing method of the agent, must be implemented in subclasses
|
|
409
420
|
*
|
|
@@ -415,7 +426,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
415
426
|
* - Another agent instance (transfer agent)
|
|
416
427
|
*
|
|
417
428
|
* @param input Input message
|
|
418
|
-
* @param
|
|
429
|
+
* @param options Options for agent invocation
|
|
419
430
|
* @returns Processing result
|
|
420
431
|
*
|
|
421
432
|
* @example
|
|
@@ -434,7 +445,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
434
445
|
* Example of transfer to another agent:
|
|
435
446
|
* {@includeCode ../../test/agents/agent.test.ts#example-process-transfer}
|
|
436
447
|
*/
|
|
437
|
-
abstract process(input: I,
|
|
448
|
+
abstract process(input: I, options: AgentInvokeOptions): PromiseOrValue<AgentProcessResult<O>>;
|
|
438
449
|
/**
|
|
439
450
|
* Shut down the agent and clean up resources
|
|
440
451
|
*
|
|
@@ -484,6 +495,7 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
|
|
|
484
495
|
* @param event Object containing the input message
|
|
485
496
|
*/
|
|
486
497
|
onStart?: (event: {
|
|
498
|
+
context: Context;
|
|
487
499
|
input: I;
|
|
488
500
|
}) => PromiseOrValue<void>;
|
|
489
501
|
/**
|
|
@@ -496,6 +508,7 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
|
|
|
496
508
|
* @param event Object containing the input message and either output or error
|
|
497
509
|
*/
|
|
498
510
|
onEnd?: (event: XOr<{
|
|
511
|
+
context: Context;
|
|
499
512
|
input: I;
|
|
500
513
|
output: O;
|
|
501
514
|
error: Error;
|
|
@@ -509,6 +522,7 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
|
|
|
509
522
|
* @param event Object containing the skill being used and input message
|
|
510
523
|
*/
|
|
511
524
|
onSkillStart?: (event: {
|
|
525
|
+
context: Context;
|
|
512
526
|
skill: Agent;
|
|
513
527
|
input: I;
|
|
514
528
|
}) => PromiseOrValue<void>;
|
|
@@ -522,6 +536,7 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
|
|
|
522
536
|
* @param event Object containing the skill used, input message, and either output or error
|
|
523
537
|
*/
|
|
524
538
|
onSkillEnd?: (event: XOr<{
|
|
539
|
+
context: Context;
|
|
525
540
|
skill: Agent;
|
|
526
541
|
input: I;
|
|
527
542
|
output: O;
|
|
@@ -537,6 +552,7 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
|
|
|
537
552
|
* @param event Object containing the source agent, target agent, and input message
|
|
538
553
|
*/
|
|
539
554
|
onHandoff?: (event: {
|
|
555
|
+
context: Context;
|
|
540
556
|
source: Agent;
|
|
541
557
|
target: Agent;
|
|
542
558
|
input: I;
|
|
@@ -587,7 +603,7 @@ export interface AgentResponseDelta<T> {
|
|
|
587
603
|
}> | Partial<{
|
|
588
604
|
[key: string]: string;
|
|
589
605
|
}>;
|
|
590
|
-
json?: Partial<T | TransferAgentOutput
|
|
606
|
+
json?: Partial<T> | TransferAgentOutput;
|
|
591
607
|
};
|
|
592
608
|
}
|
|
593
609
|
/**
|
|
@@ -619,7 +635,7 @@ export declare function jsonDelta<T extends Message>(jsonDelta: NonNullable<Agen
|
|
|
619
635
|
*
|
|
620
636
|
* @template O Agent output message type
|
|
621
637
|
*/
|
|
622
|
-
export type AgentProcessAsyncGenerator<O extends Message> = AsyncGenerator<AgentResponseChunk<O>, Partial<O | TransferAgentOutput
|
|
638
|
+
export type AgentProcessAsyncGenerator<O extends Message> = AsyncGenerator<AgentResponseChunk<O>, Partial<O> | TransferAgentOutput | undefined | void>;
|
|
623
639
|
/**
|
|
624
640
|
* Result type for agent processing method, can be:
|
|
625
641
|
* - Direct or streaming response
|
|
@@ -709,10 +725,10 @@ export declare class FunctionAgent<I extends Message = Message, O extends Messag
|
|
|
709
725
|
* Process input implementation, calls the configured processing function
|
|
710
726
|
*
|
|
711
727
|
* @param input Input message
|
|
712
|
-
* @param
|
|
728
|
+
* @param options Invocation options
|
|
713
729
|
* @returns Processing result
|
|
714
730
|
*/
|
|
715
|
-
process(input: I,
|
|
731
|
+
process(input: I, options: AgentInvokeOptions): PromiseOrValue<AgentProcessResult<O>>;
|
|
716
732
|
}
|
|
717
733
|
/**
|
|
718
734
|
* Function type for function agents
|
|
@@ -725,4 +741,4 @@ export declare class FunctionAgent<I extends Message = Message, O extends Messag
|
|
|
725
741
|
* @param context Execution context
|
|
726
742
|
* @returns Processing result, can be synchronous or asynchronous
|
|
727
743
|
*/
|
|
728
|
-
export type FunctionAgentFn<I extends Message = any, O extends Message = any> = (input: I,
|
|
744
|
+
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
|
@@ -250,68 +250,70 @@ export class Agent {
|
|
|
250
250
|
/**
|
|
251
251
|
* Check context status to ensure it hasn't timed out
|
|
252
252
|
*
|
|
253
|
-
* @param
|
|
253
|
+
* @param options Invocation options containing context
|
|
254
254
|
* @throws Error if the context has timed out
|
|
255
255
|
*/
|
|
256
|
-
checkContextStatus(
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
throw new Error(`AIGNE for agent ${this.name} has timed out`);
|
|
261
|
-
}
|
|
256
|
+
checkContextStatus(options) {
|
|
257
|
+
const { status } = options.context;
|
|
258
|
+
if (status === "timeout") {
|
|
259
|
+
throw new Error(`AIGNE for agent ${this.name} has timed out`);
|
|
262
260
|
}
|
|
263
261
|
}
|
|
264
262
|
async newDefaultContext() {
|
|
265
263
|
return import("../aigne/context.js").then((m) => new m.AIGNEContext());
|
|
266
264
|
}
|
|
267
|
-
async invoke(input,
|
|
268
|
-
const
|
|
265
|
+
async invoke(input, options = {}) {
|
|
266
|
+
const opts = {
|
|
267
|
+
...options,
|
|
268
|
+
context: options.context ?? (await this.newDefaultContext()),
|
|
269
|
+
};
|
|
269
270
|
const message = typeof input === "string" ? createMessage(input) : input;
|
|
270
271
|
logger.debug("Invoke agent %s started with input: %O", this.name, input);
|
|
271
272
|
if (!this.disableEvents)
|
|
272
|
-
|
|
273
|
+
opts.context.emit("agentStarted", { agent: this, input: message });
|
|
273
274
|
try {
|
|
274
|
-
await this.hooks.onStart?.({ input: message });
|
|
275
|
+
await this.hooks.onStart?.({ context: opts.context, input: message });
|
|
275
276
|
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,
|
|
277
|
+
await this.preprocess(parsedInput, opts);
|
|
278
|
+
this.checkContextStatus(opts);
|
|
279
|
+
let response = await this.process(parsedInput, opts);
|
|
279
280
|
if (response instanceof Agent) {
|
|
280
281
|
response = transferToAgentOutput(response);
|
|
281
282
|
}
|
|
282
|
-
if (
|
|
283
|
+
if (opts.streaming) {
|
|
283
284
|
const stream = response instanceof ReadableStream
|
|
284
285
|
? response
|
|
285
286
|
: isAsyncGenerator(response)
|
|
286
287
|
? asyncGeneratorToReadableStream(response)
|
|
287
288
|
: objectToAgentResponseStream(response);
|
|
288
289
|
return this.checkResponseByGuideRails(message, onAgentResponseStreamEnd(stream, async (result) => {
|
|
289
|
-
return await this.processAgentOutput(parsedInput, result,
|
|
290
|
+
return await this.processAgentOutput(parsedInput, result, opts);
|
|
290
291
|
}, {
|
|
291
292
|
errorCallback: async (error) => {
|
|
292
|
-
return await this.processAgentError(message, error,
|
|
293
|
+
return await this.processAgentError(message, error, opts);
|
|
293
294
|
},
|
|
294
|
-
}),
|
|
295
|
+
}), opts);
|
|
295
296
|
}
|
|
296
297
|
return await this.checkResponseByGuideRails(message, this.processAgentOutput(parsedInput, response instanceof ReadableStream
|
|
297
298
|
? await agentResponseStreamToObject(response)
|
|
298
299
|
: isAsyncGenerator(response)
|
|
299
300
|
? await agentResponseStreamToObject(response)
|
|
300
|
-
: response,
|
|
301
|
+
: response, opts), opts);
|
|
301
302
|
}
|
|
302
303
|
catch (error) {
|
|
303
|
-
throw await this.processAgentError(message, error,
|
|
304
|
+
throw await this.processAgentError(message, error, opts);
|
|
304
305
|
}
|
|
305
306
|
}
|
|
306
|
-
async invokeSkill(skill, input,
|
|
307
|
-
|
|
307
|
+
async invokeSkill(skill, input, options) {
|
|
308
|
+
const { context } = options;
|
|
309
|
+
await this.hooks.onSkillStart?.({ context, skill, input });
|
|
308
310
|
try {
|
|
309
311
|
const output = await context.invoke(skill, input);
|
|
310
|
-
await this.hooks.onSkillEnd?.({ skill, input, output });
|
|
312
|
+
await this.hooks.onSkillEnd?.({ context, skill, input, output });
|
|
311
313
|
return output;
|
|
312
314
|
}
|
|
313
315
|
catch (error) {
|
|
314
|
-
await this.hooks.onSkillEnd?.({ skill, input, error });
|
|
316
|
+
await this.hooks.onSkillEnd?.({ context, skill, input, error });
|
|
315
317
|
throw error;
|
|
316
318
|
}
|
|
317
319
|
}
|
|
@@ -322,17 +324,18 @@ export class Agent {
|
|
|
322
324
|
*
|
|
323
325
|
* @param input Original input message
|
|
324
326
|
* @param output Raw output produced by the agent
|
|
325
|
-
* @param
|
|
327
|
+
* @param options Invocation options
|
|
326
328
|
* @returns Final processed output
|
|
327
329
|
*/
|
|
328
|
-
async processAgentOutput(input, output,
|
|
330
|
+
async processAgentOutput(input, output, options) {
|
|
331
|
+
const { context } = options;
|
|
329
332
|
const parsedOutput = checkArguments(`Agent ${this.name} output`, this.outputSchema, output);
|
|
330
333
|
const finalOutput = this.includeInputInOutput ? { ...input, ...parsedOutput } : parsedOutput;
|
|
331
|
-
await this.postprocess(input, finalOutput,
|
|
334
|
+
await this.postprocess(input, finalOutput, options);
|
|
332
335
|
logger.debug("Invoke agent %s succeed with output: %O", this.name, finalOutput);
|
|
333
336
|
if (!this.disableEvents)
|
|
334
337
|
context.emit("agentSucceed", { agent: this, output: finalOutput });
|
|
335
|
-
await this.hooks.onEnd?.({ input, output: finalOutput });
|
|
338
|
+
await this.hooks.onEnd?.({ context, input, output: finalOutput });
|
|
336
339
|
return finalOutput;
|
|
337
340
|
}
|
|
338
341
|
/**
|
|
@@ -341,13 +344,14 @@ export class Agent {
|
|
|
341
344
|
* Logs error information, triggers failure events, and re-throws the error
|
|
342
345
|
*
|
|
343
346
|
* @param error Caught error
|
|
344
|
-
* @param
|
|
347
|
+
* @param options Invocation options
|
|
345
348
|
*/
|
|
346
|
-
async processAgentError(input, error,
|
|
349
|
+
async processAgentError(input, error, options) {
|
|
347
350
|
logger.error("Invoke agent %s failed with error: %O", this.name, error);
|
|
348
351
|
if (!this.disableEvents)
|
|
349
|
-
context.emit("agentFailed", { agent: this, error });
|
|
350
|
-
|
|
352
|
+
options.context.emit("agentFailed", { agent: this, error });
|
|
353
|
+
const { context } = options;
|
|
354
|
+
await this.hooks.onEnd?.({ context, input, error });
|
|
351
355
|
return error;
|
|
352
356
|
}
|
|
353
357
|
/**
|
|
@@ -356,11 +360,11 @@ export class Agent {
|
|
|
356
360
|
* If the context has a maximum invocation limit set, checks if the limit
|
|
357
361
|
* has been exceeded and increments the invocation counter
|
|
358
362
|
*
|
|
359
|
-
* @param context
|
|
363
|
+
* @param options Invocation options containing context and limits
|
|
360
364
|
* @throws Error if maximum invocation limit is exceeded
|
|
361
365
|
*/
|
|
362
|
-
checkAgentInvokesUsage(
|
|
363
|
-
const { limits, usage } = context;
|
|
366
|
+
checkAgentInvokesUsage(options) {
|
|
367
|
+
const { limits, usage } = options.context;
|
|
364
368
|
if (limits?.maxAgentInvokes && usage.agentCalls >= limits.maxAgentInvokes) {
|
|
365
369
|
throw new Error(`Exceeded max agent invokes ${usage.agentCalls}/${limits.maxAgentInvokes}`);
|
|
366
370
|
}
|
|
@@ -374,19 +378,19 @@ export class Agent {
|
|
|
374
378
|
* - Verifying invocation limits
|
|
375
379
|
*
|
|
376
380
|
* @param _ Input message (unused)
|
|
377
|
-
* @param
|
|
381
|
+
* @param options Options for agent invocation
|
|
378
382
|
*/
|
|
379
|
-
preprocess(_,
|
|
380
|
-
this.checkContextStatus(
|
|
381
|
-
this.checkAgentInvokesUsage(
|
|
383
|
+
async preprocess(_, options) {
|
|
384
|
+
this.checkContextStatus(options);
|
|
385
|
+
this.checkAgentInvokesUsage(options);
|
|
382
386
|
}
|
|
383
|
-
async checkResponseByGuideRails(input, output,
|
|
387
|
+
async checkResponseByGuideRails(input, output, options) {
|
|
384
388
|
if (!this.guideRails?.length)
|
|
385
389
|
return output;
|
|
386
390
|
const result = await output;
|
|
387
391
|
if (result instanceof ReadableStream) {
|
|
388
392
|
return onAgentResponseStreamEnd(result, async (result) => {
|
|
389
|
-
const error = await this.runGuideRails(input, result,
|
|
393
|
+
const error = await this.runGuideRails(input, result, options);
|
|
390
394
|
if (error) {
|
|
391
395
|
return {
|
|
392
396
|
...(await this.onGuideRailError(error)),
|
|
@@ -395,13 +399,13 @@ export class Agent {
|
|
|
395
399
|
}
|
|
396
400
|
});
|
|
397
401
|
}
|
|
398
|
-
const error = await this.runGuideRails(input, result,
|
|
402
|
+
const error = await this.runGuideRails(input, result, options);
|
|
399
403
|
if (!error)
|
|
400
404
|
return output;
|
|
401
405
|
return { ...(await this.onGuideRailError(error)), $status: "GuideRailError" };
|
|
402
406
|
}
|
|
403
|
-
async runGuideRails(input, output,
|
|
404
|
-
const result = await Promise.all((this.guideRails ?? []).map((i) => context.invoke(i, { input, output })));
|
|
407
|
+
async runGuideRails(input, output, options) {
|
|
408
|
+
const result = await Promise.all((this.guideRails ?? []).map((i) => options.context.invoke(i, { input, output })));
|
|
405
409
|
return result.find((i) => !!i.abort);
|
|
406
410
|
}
|
|
407
411
|
/**
|
|
@@ -431,26 +435,26 @@ export class Agent {
|
|
|
431
435
|
*
|
|
432
436
|
* @param input Input message
|
|
433
437
|
* @param output Output message
|
|
434
|
-
* @param
|
|
438
|
+
* @param options Options for agent invocation
|
|
435
439
|
*/
|
|
436
|
-
postprocess(input, output,
|
|
437
|
-
this.checkContextStatus(
|
|
438
|
-
this.publishToTopics(output,
|
|
440
|
+
async postprocess(input, output, options) {
|
|
441
|
+
this.checkContextStatus(options);
|
|
442
|
+
this.publishToTopics(output, options);
|
|
439
443
|
for (const memory of this.memories) {
|
|
440
444
|
if (memory.autoUpdate) {
|
|
441
|
-
memory.record({
|
|
445
|
+
await memory.record({
|
|
442
446
|
content: [
|
|
443
447
|
{ role: "user", content: input },
|
|
444
448
|
{ role: "agent", content: replaceTransferAgentToName(output), source: this.name },
|
|
445
449
|
],
|
|
446
|
-
}, context);
|
|
450
|
+
}, options.context);
|
|
447
451
|
}
|
|
448
452
|
}
|
|
449
453
|
}
|
|
450
|
-
async publishToTopics(output,
|
|
454
|
+
async publishToTopics(output, options) {
|
|
451
455
|
const publishTopics = typeof this.publishTopic === "function" ? await this.publishTopic(output) : this.publishTopic;
|
|
452
456
|
if (publishTopics?.length) {
|
|
453
|
-
context.publish(publishTopics, {
|
|
457
|
+
options.context.publish(publishTopics, {
|
|
454
458
|
role: this.constructor.name === "UserAgent" ? "user" : "agent",
|
|
455
459
|
source: this.name,
|
|
456
460
|
message: output,
|
|
@@ -602,11 +606,11 @@ export class FunctionAgent extends Agent {
|
|
|
602
606
|
* Process input implementation, calls the configured processing function
|
|
603
607
|
*
|
|
604
608
|
* @param input Input message
|
|
605
|
-
* @param
|
|
609
|
+
* @param options Invocation options
|
|
606
610
|
* @returns Processing result
|
|
607
611
|
*/
|
|
608
|
-
process(input,
|
|
609
|
-
return this._process(input,
|
|
612
|
+
process(input, options) {
|
|
613
|
+
return this._process(input, options);
|
|
610
614
|
}
|
|
611
615
|
}
|
|
612
616
|
function functionToAgent(agent) {
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { type ZodObject, type ZodType, z } from "zod";
|
|
2
|
-
import type
|
|
3
|
-
import { type DefaultMemoryOptions } from "../memory/default-memory.js";
|
|
2
|
+
import { type DefaultMemoryOptions } from "../memory/default-memory/index.js";
|
|
4
3
|
import { PromptBuilder } from "../prompt/prompt-builder.js";
|
|
5
|
-
import { Agent, type AgentOptions, type AgentProcessAsyncGenerator, type Message } from "./agent.js";
|
|
4
|
+
import { Agent, type AgentInvokeOptions, type AgentOptions, type AgentProcessAsyncGenerator, type Message } from "./agent.js";
|
|
6
5
|
import { ChatModel, type ChatModelInput } from "./chat-model.js";
|
|
7
6
|
import type { GuideRailAgentOutput } from "./guide-rail-agent.js";
|
|
8
7
|
/**
|
|
@@ -215,7 +214,7 @@ export declare class AIAgent<I extends Message = Message, O extends Message = Me
|
|
|
215
214
|
*
|
|
216
215
|
* @protected
|
|
217
216
|
*/
|
|
218
|
-
process(input: I,
|
|
217
|
+
process(input: I, options: AgentInvokeOptions): AgentProcessAsyncGenerator<O>;
|
|
219
218
|
protected onGuideRailError(error: GuideRailAgentOutput): Promise<O | GuideRailAgentOutput>;
|
|
220
219
|
/**
|
|
221
220
|
* Process router mode requests
|
|
@@ -225,5 +224,5 @@ export declare class AIAgent<I extends Message = Message, O extends Message = Me
|
|
|
225
224
|
*
|
|
226
225
|
* @protected
|
|
227
226
|
*/
|
|
228
|
-
_processRouter(input: I, model: ChatModel, modelInput: ChatModelInput,
|
|
227
|
+
_processRouter(input: I, model: ChatModel, modelInput: ChatModelInput, options: AgentInvokeOptions, toolsMap: Map<string, Agent>): AgentProcessAsyncGenerator<O>;
|
|
229
228
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
import { DefaultMemory } from "../memory/default-memory.js";
|
|
2
|
+
import { DefaultMemory } from "../memory/default-memory/index.js";
|
|
3
3
|
import { MemoryAgent } from "../memory/memory.js";
|
|
4
4
|
import { MESSAGE_KEY, PromptBuilder } from "../prompt/prompt-builder.js";
|
|
5
5
|
import { AgentMessageTemplate, ToolMessageTemplate } from "../prompt/template.js";
|
|
@@ -185,26 +185,26 @@ export class AIAgent extends Agent {
|
|
|
185
185
|
*
|
|
186
186
|
* @protected
|
|
187
187
|
*/
|
|
188
|
-
async *process(input,
|
|
189
|
-
const model = this.model ?? context.model;
|
|
188
|
+
async *process(input, options) {
|
|
189
|
+
const model = this.model ?? options.context.model;
|
|
190
190
|
if (!model)
|
|
191
191
|
throw new Error("model is required to run AIAgent");
|
|
192
192
|
const { toolAgents, ...modelInput } = await this.instructions.build({
|
|
193
193
|
agent: this,
|
|
194
194
|
input,
|
|
195
195
|
model,
|
|
196
|
-
context,
|
|
196
|
+
context: options.context,
|
|
197
197
|
});
|
|
198
198
|
const toolsMap = new Map(toolAgents?.map((i) => [i.name, i]));
|
|
199
199
|
if (this.toolChoice === "router") {
|
|
200
|
-
yield* this._processRouter(input, model, modelInput,
|
|
200
|
+
yield* this._processRouter(input, model, modelInput, options, toolsMap);
|
|
201
201
|
return;
|
|
202
202
|
}
|
|
203
203
|
const toolCallMessages = [];
|
|
204
204
|
const outputKey = this.outputKey || MESSAGE_KEY;
|
|
205
205
|
for (;;) {
|
|
206
206
|
const modelOutput = {};
|
|
207
|
-
const stream = await context.invoke(model, { ...modelInput, messages: modelInput.messages.concat(toolCallMessages) }, { streaming: true });
|
|
207
|
+
const stream = await options.context.invoke(model, { ...modelInput, messages: modelInput.messages.concat(toolCallMessages) }, { streaming: true });
|
|
208
208
|
for await (const value of stream) {
|
|
209
209
|
if (value.delta.text?.text) {
|
|
210
210
|
yield { delta: { text: { [outputKey]: value.delta.text.text } } };
|
|
@@ -222,7 +222,7 @@ export class AIAgent extends Agent {
|
|
|
222
222
|
if (!tool)
|
|
223
223
|
throw new Error(`Tool not found: ${call.function.name}`);
|
|
224
224
|
// NOTE: should pass both arguments (model generated) and input (user provided) to the tool
|
|
225
|
-
const output = await this.invokeSkill(tool, { ...input, ...call.function.arguments },
|
|
225
|
+
const output = await this.invokeSkill(tool, { ...input, ...call.function.arguments }, options).catch((error) => {
|
|
226
226
|
if (!this.catchToolsError) {
|
|
227
227
|
return Promise.reject(error);
|
|
228
228
|
}
|
|
@@ -272,15 +272,15 @@ export class AIAgent extends Agent {
|
|
|
272
272
|
*
|
|
273
273
|
* @protected
|
|
274
274
|
*/
|
|
275
|
-
async *_processRouter(input, model, modelInput,
|
|
276
|
-
const { toolCalls: [call] = [], } = await context.invoke(model, modelInput);
|
|
275
|
+
async *_processRouter(input, model, modelInput, options, toolsMap) {
|
|
276
|
+
const { toolCalls: [call] = [], } = await options.context.invoke(model, modelInput);
|
|
277
277
|
if (!call) {
|
|
278
278
|
throw new Error("Router toolChoice requires exactly one tool to be executed");
|
|
279
279
|
}
|
|
280
280
|
const tool = toolsMap.get(call.function.name);
|
|
281
281
|
if (!tool)
|
|
282
282
|
throw new Error(`Tool not found: ${call.function.name}`);
|
|
283
|
-
const stream = await context.invoke(tool, { ...call.function.arguments, ...input }, { streaming: true, sourceAgent: this });
|
|
283
|
+
const stream = await options.context.invoke(tool, { ...call.function.arguments, ...input }, { streaming: true, sourceAgent: this });
|
|
284
284
|
yield* stream;
|
|
285
285
|
}
|
|
286
286
|
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type { Context } from "../aigne/context.js";
|
|
2
1
|
import type { PromiseOrValue } from "../utils/type-utils.js";
|
|
3
|
-
import { Agent, type AgentProcessResult, type Message } from "./agent.js";
|
|
2
|
+
import { Agent, type AgentInvokeOptions, type AgentProcessResult, type Message } from "./agent.js";
|
|
4
3
|
/**
|
|
5
4
|
* ChatModel is an abstract base class for interacting with Large Language Models (LLMs).
|
|
6
5
|
*
|
|
@@ -61,10 +60,10 @@ export declare abstract class ChatModel extends Agent<ChatModelInput, ChatModelO
|
|
|
61
60
|
* Primarily checks if token usage exceeds limits, throwing an exception if limits are exceeded
|
|
62
61
|
*
|
|
63
62
|
* @param input Input message
|
|
64
|
-
* @param
|
|
63
|
+
* @param options Options for invoking the agent
|
|
65
64
|
* @throws Error if token usage exceeds maximum limit
|
|
66
65
|
*/
|
|
67
|
-
protected preprocess(input: ChatModelInput,
|
|
66
|
+
protected preprocess(input: ChatModelInput, options: AgentInvokeOptions): Promise<void>;
|
|
68
67
|
/**
|
|
69
68
|
* Performs postprocessing operations after handling output
|
|
70
69
|
*
|
|
@@ -72,9 +71,9 @@ export declare abstract class ChatModel extends Agent<ChatModelInput, ChatModelO
|
|
|
72
71
|
*
|
|
73
72
|
* @param input Input message
|
|
74
73
|
* @param output Output message
|
|
75
|
-
* @param
|
|
74
|
+
* @param options Options for invoking the agent
|
|
76
75
|
*/
|
|
77
|
-
protected postprocess(input: ChatModelInput, output: ChatModelOutput,
|
|
76
|
+
protected postprocess(input: ChatModelInput, output: ChatModelOutput, options: AgentInvokeOptions): Promise<void>;
|
|
78
77
|
/**
|
|
79
78
|
* Processes input messages and generates model responses
|
|
80
79
|
*
|
|
@@ -92,10 +91,10 @@ export declare abstract class ChatModel extends Agent<ChatModelInput, ChatModelO
|
|
|
92
91
|
* - Tool call processing if applicable
|
|
93
92
|
*
|
|
94
93
|
* @param input - The standardized input containing messages and model options
|
|
95
|
-
* @param
|
|
94
|
+
* @param options - The options for invoking the agent, including context and limits
|
|
96
95
|
* @returns A promise or direct value containing the model's response
|
|
97
96
|
*/
|
|
98
|
-
abstract process(input: ChatModelInput,
|
|
97
|
+
abstract process(input: ChatModelInput, options: AgentInvokeOptions): PromiseOrValue<AgentProcessResult<ChatModelOutput>>;
|
|
99
98
|
}
|
|
100
99
|
/**
|
|
101
100
|
* Input message format for ChatModel
|