@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
package/CHANGELOG.md
CHANGED
|
@@ -22,6 +22,13 @@
|
|
|
22
22
|
* rename @aigne/core-next to @aigne/core ([3a81009](https://github.com/AIGNE-io/aigne-framework/commit/3a8100962c81813217b687ae28e8de604419c622))
|
|
23
23
|
* use text resource from MCP correctly ([8b9eba8](https://github.com/AIGNE-io/aigne-framework/commit/8b9eba83352ec096a2a5d4f410d4c4bde7420bce))
|
|
24
24
|
|
|
25
|
+
## [1.17.0](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.16.0...core-v1.17.0) (2025-05-25)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
### Features
|
|
29
|
+
|
|
30
|
+
* add user context support ([#131](https://github.com/AIGNE-io/aigne-framework/issues/131)) ([4dd9d20](https://github.com/AIGNE-io/aigne-framework/commit/4dd9d20953f6ac33933723db56efd9b44bafeb02))
|
|
31
|
+
|
|
25
32
|
## [1.16.0](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.15.0...core-v1.16.0) (2025-05-23)
|
|
26
33
|
|
|
27
34
|
|
|
@@ -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/cjs/agents/agent.js
CHANGED
|
@@ -292,68 +292,70 @@ class Agent {
|
|
|
292
292
|
/**
|
|
293
293
|
* Check context status to ensure it hasn't timed out
|
|
294
294
|
*
|
|
295
|
-
* @param
|
|
295
|
+
* @param options Invocation options containing context
|
|
296
296
|
* @throws Error if the context has timed out
|
|
297
297
|
*/
|
|
298
|
-
checkContextStatus(
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
throw new Error(`AIGNE for agent ${this.name} has timed out`);
|
|
303
|
-
}
|
|
298
|
+
checkContextStatus(options) {
|
|
299
|
+
const { status } = options.context;
|
|
300
|
+
if (status === "timeout") {
|
|
301
|
+
throw new Error(`AIGNE for agent ${this.name} has timed out`);
|
|
304
302
|
}
|
|
305
303
|
}
|
|
306
304
|
async newDefaultContext() {
|
|
307
305
|
return Promise.resolve().then(() => __importStar(require("../aigne/context.js"))).then((m) => new m.AIGNEContext());
|
|
308
306
|
}
|
|
309
|
-
async invoke(input,
|
|
310
|
-
const
|
|
307
|
+
async invoke(input, options = {}) {
|
|
308
|
+
const opts = {
|
|
309
|
+
...options,
|
|
310
|
+
context: options.context ?? (await this.newDefaultContext()),
|
|
311
|
+
};
|
|
311
312
|
const message = typeof input === "string" ? (0, prompt_builder_js_1.createMessage)(input) : input;
|
|
312
313
|
logger_js_1.logger.debug("Invoke agent %s started with input: %O", this.name, input);
|
|
313
314
|
if (!this.disableEvents)
|
|
314
|
-
|
|
315
|
+
opts.context.emit("agentStarted", { agent: this, input: message });
|
|
315
316
|
try {
|
|
316
|
-
await this.hooks.onStart?.({ input: message });
|
|
317
|
+
await this.hooks.onStart?.({ context: opts.context, input: message });
|
|
317
318
|
const parsedInput = (0, type_utils_js_1.checkArguments)(`Agent ${this.name} input`, this.inputSchema, message);
|
|
318
|
-
await this.preprocess(parsedInput,
|
|
319
|
-
this.checkContextStatus(
|
|
320
|
-
let response = await this.process(parsedInput,
|
|
319
|
+
await this.preprocess(parsedInput, opts);
|
|
320
|
+
this.checkContextStatus(opts);
|
|
321
|
+
let response = await this.process(parsedInput, opts);
|
|
321
322
|
if (response instanceof Agent) {
|
|
322
323
|
response = (0, types_js_1.transferToAgentOutput)(response);
|
|
323
324
|
}
|
|
324
|
-
if (
|
|
325
|
+
if (opts.streaming) {
|
|
325
326
|
const stream = response instanceof ReadableStream
|
|
326
327
|
? response
|
|
327
328
|
: (0, stream_utils_js_1.isAsyncGenerator)(response)
|
|
328
329
|
? (0, stream_utils_js_1.asyncGeneratorToReadableStream)(response)
|
|
329
330
|
: (0, stream_utils_js_1.objectToAgentResponseStream)(response);
|
|
330
331
|
return this.checkResponseByGuideRails(message, (0, stream_utils_js_1.onAgentResponseStreamEnd)(stream, async (result) => {
|
|
331
|
-
return await this.processAgentOutput(parsedInput, result,
|
|
332
|
+
return await this.processAgentOutput(parsedInput, result, opts);
|
|
332
333
|
}, {
|
|
333
334
|
errorCallback: async (error) => {
|
|
334
|
-
return await this.processAgentError(message, error,
|
|
335
|
+
return await this.processAgentError(message, error, opts);
|
|
335
336
|
},
|
|
336
|
-
}),
|
|
337
|
+
}), opts);
|
|
337
338
|
}
|
|
338
339
|
return await this.checkResponseByGuideRails(message, this.processAgentOutput(parsedInput, response instanceof ReadableStream
|
|
339
340
|
? await (0, stream_utils_js_1.agentResponseStreamToObject)(response)
|
|
340
341
|
: (0, stream_utils_js_1.isAsyncGenerator)(response)
|
|
341
342
|
? await (0, stream_utils_js_1.agentResponseStreamToObject)(response)
|
|
342
|
-
: response,
|
|
343
|
+
: response, opts), opts);
|
|
343
344
|
}
|
|
344
345
|
catch (error) {
|
|
345
|
-
throw await this.processAgentError(message, error,
|
|
346
|
+
throw await this.processAgentError(message, error, opts);
|
|
346
347
|
}
|
|
347
348
|
}
|
|
348
|
-
async invokeSkill(skill, input,
|
|
349
|
-
|
|
349
|
+
async invokeSkill(skill, input, options) {
|
|
350
|
+
const { context } = options;
|
|
351
|
+
await this.hooks.onSkillStart?.({ context, skill, input });
|
|
350
352
|
try {
|
|
351
353
|
const output = await context.invoke(skill, input);
|
|
352
|
-
await this.hooks.onSkillEnd?.({ skill, input, output });
|
|
354
|
+
await this.hooks.onSkillEnd?.({ context, skill, input, output });
|
|
353
355
|
return output;
|
|
354
356
|
}
|
|
355
357
|
catch (error) {
|
|
356
|
-
await this.hooks.onSkillEnd?.({ skill, input, error });
|
|
358
|
+
await this.hooks.onSkillEnd?.({ context, skill, input, error });
|
|
357
359
|
throw error;
|
|
358
360
|
}
|
|
359
361
|
}
|
|
@@ -364,17 +366,18 @@ class Agent {
|
|
|
364
366
|
*
|
|
365
367
|
* @param input Original input message
|
|
366
368
|
* @param output Raw output produced by the agent
|
|
367
|
-
* @param
|
|
369
|
+
* @param options Invocation options
|
|
368
370
|
* @returns Final processed output
|
|
369
371
|
*/
|
|
370
|
-
async processAgentOutput(input, output,
|
|
372
|
+
async processAgentOutput(input, output, options) {
|
|
373
|
+
const { context } = options;
|
|
371
374
|
const parsedOutput = (0, type_utils_js_1.checkArguments)(`Agent ${this.name} output`, this.outputSchema, output);
|
|
372
375
|
const finalOutput = this.includeInputInOutput ? { ...input, ...parsedOutput } : parsedOutput;
|
|
373
|
-
await this.postprocess(input, finalOutput,
|
|
376
|
+
await this.postprocess(input, finalOutput, options);
|
|
374
377
|
logger_js_1.logger.debug("Invoke agent %s succeed with output: %O", this.name, finalOutput);
|
|
375
378
|
if (!this.disableEvents)
|
|
376
379
|
context.emit("agentSucceed", { agent: this, output: finalOutput });
|
|
377
|
-
await this.hooks.onEnd?.({ input, output: finalOutput });
|
|
380
|
+
await this.hooks.onEnd?.({ context, input, output: finalOutput });
|
|
378
381
|
return finalOutput;
|
|
379
382
|
}
|
|
380
383
|
/**
|
|
@@ -383,13 +386,14 @@ class Agent {
|
|
|
383
386
|
* Logs error information, triggers failure events, and re-throws the error
|
|
384
387
|
*
|
|
385
388
|
* @param error Caught error
|
|
386
|
-
* @param
|
|
389
|
+
* @param options Invocation options
|
|
387
390
|
*/
|
|
388
|
-
async processAgentError(input, error,
|
|
391
|
+
async processAgentError(input, error, options) {
|
|
389
392
|
logger_js_1.logger.error("Invoke agent %s failed with error: %O", this.name, error);
|
|
390
393
|
if (!this.disableEvents)
|
|
391
|
-
context.emit("agentFailed", { agent: this, error });
|
|
392
|
-
|
|
394
|
+
options.context.emit("agentFailed", { agent: this, error });
|
|
395
|
+
const { context } = options;
|
|
396
|
+
await this.hooks.onEnd?.({ context, input, error });
|
|
393
397
|
return error;
|
|
394
398
|
}
|
|
395
399
|
/**
|
|
@@ -398,11 +402,11 @@ class Agent {
|
|
|
398
402
|
* If the context has a maximum invocation limit set, checks if the limit
|
|
399
403
|
* has been exceeded and increments the invocation counter
|
|
400
404
|
*
|
|
401
|
-
* @param context
|
|
405
|
+
* @param options Invocation options containing context and limits
|
|
402
406
|
* @throws Error if maximum invocation limit is exceeded
|
|
403
407
|
*/
|
|
404
|
-
checkAgentInvokesUsage(
|
|
405
|
-
const { limits, usage } = context;
|
|
408
|
+
checkAgentInvokesUsage(options) {
|
|
409
|
+
const { limits, usage } = options.context;
|
|
406
410
|
if (limits?.maxAgentInvokes && usage.agentCalls >= limits.maxAgentInvokes) {
|
|
407
411
|
throw new Error(`Exceeded max agent invokes ${usage.agentCalls}/${limits.maxAgentInvokes}`);
|
|
408
412
|
}
|
|
@@ -416,19 +420,19 @@ class Agent {
|
|
|
416
420
|
* - Verifying invocation limits
|
|
417
421
|
*
|
|
418
422
|
* @param _ Input message (unused)
|
|
419
|
-
* @param
|
|
423
|
+
* @param options Options for agent invocation
|
|
420
424
|
*/
|
|
421
|
-
preprocess(_,
|
|
422
|
-
this.checkContextStatus(
|
|
423
|
-
this.checkAgentInvokesUsage(
|
|
425
|
+
async preprocess(_, options) {
|
|
426
|
+
this.checkContextStatus(options);
|
|
427
|
+
this.checkAgentInvokesUsage(options);
|
|
424
428
|
}
|
|
425
|
-
async checkResponseByGuideRails(input, output,
|
|
429
|
+
async checkResponseByGuideRails(input, output, options) {
|
|
426
430
|
if (!this.guideRails?.length)
|
|
427
431
|
return output;
|
|
428
432
|
const result = await output;
|
|
429
433
|
if (result instanceof ReadableStream) {
|
|
430
434
|
return (0, stream_utils_js_1.onAgentResponseStreamEnd)(result, async (result) => {
|
|
431
|
-
const error = await this.runGuideRails(input, result,
|
|
435
|
+
const error = await this.runGuideRails(input, result, options);
|
|
432
436
|
if (error) {
|
|
433
437
|
return {
|
|
434
438
|
...(await this.onGuideRailError(error)),
|
|
@@ -437,13 +441,13 @@ class Agent {
|
|
|
437
441
|
}
|
|
438
442
|
});
|
|
439
443
|
}
|
|
440
|
-
const error = await this.runGuideRails(input, result,
|
|
444
|
+
const error = await this.runGuideRails(input, result, options);
|
|
441
445
|
if (!error)
|
|
442
446
|
return output;
|
|
443
447
|
return { ...(await this.onGuideRailError(error)), $status: "GuideRailError" };
|
|
444
448
|
}
|
|
445
|
-
async runGuideRails(input, output,
|
|
446
|
-
const result = await Promise.all((this.guideRails ?? []).map((i) => context.invoke(i, { input, output })));
|
|
449
|
+
async runGuideRails(input, output, options) {
|
|
450
|
+
const result = await Promise.all((this.guideRails ?? []).map((i) => options.context.invoke(i, { input, output })));
|
|
447
451
|
return result.find((i) => !!i.abort);
|
|
448
452
|
}
|
|
449
453
|
/**
|
|
@@ -473,26 +477,26 @@ class Agent {
|
|
|
473
477
|
*
|
|
474
478
|
* @param input Input message
|
|
475
479
|
* @param output Output message
|
|
476
|
-
* @param
|
|
480
|
+
* @param options Options for agent invocation
|
|
477
481
|
*/
|
|
478
|
-
postprocess(input, output,
|
|
479
|
-
this.checkContextStatus(
|
|
480
|
-
this.publishToTopics(output,
|
|
482
|
+
async postprocess(input, output, options) {
|
|
483
|
+
this.checkContextStatus(options);
|
|
484
|
+
this.publishToTopics(output, options);
|
|
481
485
|
for (const memory of this.memories) {
|
|
482
486
|
if (memory.autoUpdate) {
|
|
483
|
-
memory.record({
|
|
487
|
+
await memory.record({
|
|
484
488
|
content: [
|
|
485
489
|
{ role: "user", content: input },
|
|
486
490
|
{ role: "agent", content: (0, types_js_1.replaceTransferAgentToName)(output), source: this.name },
|
|
487
491
|
],
|
|
488
|
-
}, context);
|
|
492
|
+
}, options.context);
|
|
489
493
|
}
|
|
490
494
|
}
|
|
491
495
|
}
|
|
492
|
-
async publishToTopics(output,
|
|
496
|
+
async publishToTopics(output, options) {
|
|
493
497
|
const publishTopics = typeof this.publishTopic === "function" ? await this.publishTopic(output) : this.publishTopic;
|
|
494
498
|
if (publishTopics?.length) {
|
|
495
|
-
context.publish(publishTopics, {
|
|
499
|
+
options.context.publish(publishTopics, {
|
|
496
500
|
role: this.constructor.name === "UserAgent" ? "user" : "agent",
|
|
497
501
|
source: this.name,
|
|
498
502
|
message: output,
|
|
@@ -645,11 +649,11 @@ class FunctionAgent extends Agent {
|
|
|
645
649
|
* Process input implementation, calls the configured processing function
|
|
646
650
|
*
|
|
647
651
|
* @param input Input message
|
|
648
|
-
* @param
|
|
652
|
+
* @param options Invocation options
|
|
649
653
|
* @returns Processing result
|
|
650
654
|
*/
|
|
651
|
-
process(input,
|
|
652
|
-
return this._process(input,
|
|
655
|
+
process(input, options) {
|
|
656
|
+
return this._process(input, options);
|
|
653
657
|
}
|
|
654
658
|
}
|
|
655
659
|
exports.FunctionAgent = FunctionAgent;
|
|
@@ -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
|
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AIAgent = exports.aiAgentOptionsSchema = exports.aiAgentToolChoiceSchema = exports.AIAgentToolChoice = void 0;
|
|
4
4
|
const zod_1 = require("zod");
|
|
5
|
-
const
|
|
5
|
+
const index_js_1 = require("../memory/default-memory/index.js");
|
|
6
6
|
const memory_js_1 = require("../memory/memory.js");
|
|
7
7
|
const prompt_builder_js_1 = require("../prompt/prompt-builder.js");
|
|
8
8
|
const template_js_1 = require("../prompt/template.js");
|
|
@@ -107,7 +107,7 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
107
107
|
? undefined
|
|
108
108
|
: Array.isArray(options.memory) || options.memory instanceof memory_js_1.MemoryAgent
|
|
109
109
|
? options.memory
|
|
110
|
-
: new
|
|
110
|
+
: new index_js_1.DefaultMemory(options.memory === true ? {} : options.memory),
|
|
111
111
|
});
|
|
112
112
|
(0, type_utils_js_1.checkArguments)("AIAgent", exports.aiAgentOptionsSchema, options);
|
|
113
113
|
this.model = options.model;
|
|
@@ -188,26 +188,26 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
188
188
|
*
|
|
189
189
|
* @protected
|
|
190
190
|
*/
|
|
191
|
-
async *process(input,
|
|
192
|
-
const model = this.model ?? context.model;
|
|
191
|
+
async *process(input, options) {
|
|
192
|
+
const model = this.model ?? options.context.model;
|
|
193
193
|
if (!model)
|
|
194
194
|
throw new Error("model is required to run AIAgent");
|
|
195
195
|
const { toolAgents, ...modelInput } = await this.instructions.build({
|
|
196
196
|
agent: this,
|
|
197
197
|
input,
|
|
198
198
|
model,
|
|
199
|
-
context,
|
|
199
|
+
context: options.context,
|
|
200
200
|
});
|
|
201
201
|
const toolsMap = new Map(toolAgents?.map((i) => [i.name, i]));
|
|
202
202
|
if (this.toolChoice === "router") {
|
|
203
|
-
yield* this._processRouter(input, model, modelInput,
|
|
203
|
+
yield* this._processRouter(input, model, modelInput, options, toolsMap);
|
|
204
204
|
return;
|
|
205
205
|
}
|
|
206
206
|
const toolCallMessages = [];
|
|
207
207
|
const outputKey = this.outputKey || prompt_builder_js_1.MESSAGE_KEY;
|
|
208
208
|
for (;;) {
|
|
209
209
|
const modelOutput = {};
|
|
210
|
-
const stream = await context.invoke(model, { ...modelInput, messages: modelInput.messages.concat(toolCallMessages) }, { streaming: true });
|
|
210
|
+
const stream = await options.context.invoke(model, { ...modelInput, messages: modelInput.messages.concat(toolCallMessages) }, { streaming: true });
|
|
211
211
|
for await (const value of stream) {
|
|
212
212
|
if (value.delta.text?.text) {
|
|
213
213
|
yield { delta: { text: { [outputKey]: value.delta.text.text } } };
|
|
@@ -225,7 +225,7 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
225
225
|
if (!tool)
|
|
226
226
|
throw new Error(`Tool not found: ${call.function.name}`);
|
|
227
227
|
// NOTE: should pass both arguments (model generated) and input (user provided) to the tool
|
|
228
|
-
const output = await this.invokeSkill(tool, { ...input, ...call.function.arguments },
|
|
228
|
+
const output = await this.invokeSkill(tool, { ...input, ...call.function.arguments }, options).catch((error) => {
|
|
229
229
|
if (!this.catchToolsError) {
|
|
230
230
|
return Promise.reject(error);
|
|
231
231
|
}
|
|
@@ -275,15 +275,15 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
275
275
|
*
|
|
276
276
|
* @protected
|
|
277
277
|
*/
|
|
278
|
-
async *_processRouter(input, model, modelInput,
|
|
279
|
-
const { toolCalls: [call] = [], } = await context.invoke(model, modelInput);
|
|
278
|
+
async *_processRouter(input, model, modelInput, options, toolsMap) {
|
|
279
|
+
const { toolCalls: [call] = [], } = await options.context.invoke(model, modelInput);
|
|
280
280
|
if (!call) {
|
|
281
281
|
throw new Error("Router toolChoice requires exactly one tool to be executed");
|
|
282
282
|
}
|
|
283
283
|
const tool = toolsMap.get(call.function.name);
|
|
284
284
|
if (!tool)
|
|
285
285
|
throw new Error(`Tool not found: ${call.function.name}`);
|
|
286
|
-
const stream = await context.invoke(tool, { ...call.function.arguments, ...input }, { streaming: true, sourceAgent: this });
|
|
286
|
+
const stream = await options.context.invoke(tool, { ...call.function.arguments, ...input }, { streaming: true, sourceAgent: this });
|
|
287
287
|
yield* stream;
|
|
288
288
|
}
|
|
289
289
|
}
|
|
@@ -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
|