@aigne/core 1.21.0 → 1.23.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.
Files changed (63) hide show
  1. package/CHANGELOG.md +37 -84
  2. package/lib/cjs/agents/agent.d.ts +17 -10
  3. package/lib/cjs/agents/agent.js +7 -3
  4. package/lib/cjs/agents/ai-agent.d.ts +2 -1
  5. package/lib/cjs/agents/ai-agent.js +1 -0
  6. package/lib/cjs/agents/chat-model.d.ts +1 -0
  7. package/lib/cjs/agents/chat-model.js +1 -0
  8. package/lib/cjs/agents/mcp-agent.d.ts +6 -1
  9. package/lib/cjs/agents/mcp-agent.js +5 -0
  10. package/lib/cjs/agents/team-agent.d.ts +1 -0
  11. package/lib/cjs/agents/team-agent.js +1 -0
  12. package/lib/cjs/agents/user-agent.d.ts +1 -0
  13. package/lib/cjs/agents/user-agent.js +6 -3
  14. package/lib/cjs/aigne/aigne.d.ts +14 -5
  15. package/lib/cjs/aigne/aigne.js +21 -2
  16. package/lib/cjs/aigne/context.d.ts +27 -6
  17. package/lib/cjs/aigne/context.js +86 -14
  18. package/lib/cjs/loader/agent-yaml.d.ts +1 -1
  19. package/lib/cjs/loader/index.d.ts +2 -2
  20. package/lib/cjs/memory/memory.d.ts +6 -3
  21. package/lib/cjs/memory/memory.js +17 -2
  22. package/lib/cjs/memory/recorder.d.ts +15 -4
  23. package/lib/cjs/memory/recorder.js +14 -1
  24. package/lib/cjs/memory/retriever.d.ts +12 -5
  25. package/lib/cjs/memory/retriever.js +9 -0
  26. package/lib/dts/agents/agent.d.ts +17 -10
  27. package/lib/dts/agents/ai-agent.d.ts +2 -1
  28. package/lib/dts/agents/chat-model.d.ts +1 -0
  29. package/lib/dts/agents/mcp-agent.d.ts +6 -1
  30. package/lib/dts/agents/team-agent.d.ts +1 -0
  31. package/lib/dts/agents/user-agent.d.ts +1 -0
  32. package/lib/dts/aigne/aigne.d.ts +14 -5
  33. package/lib/dts/aigne/context.d.ts +27 -6
  34. package/lib/dts/loader/agent-yaml.d.ts +1 -1
  35. package/lib/dts/loader/index.d.ts +2 -2
  36. package/lib/dts/memory/memory.d.ts +6 -3
  37. package/lib/dts/memory/recorder.d.ts +15 -4
  38. package/lib/dts/memory/retriever.d.ts +12 -5
  39. package/lib/esm/agents/agent.d.ts +17 -10
  40. package/lib/esm/agents/agent.js +7 -3
  41. package/lib/esm/agents/ai-agent.d.ts +2 -1
  42. package/lib/esm/agents/ai-agent.js +1 -0
  43. package/lib/esm/agents/chat-model.d.ts +1 -0
  44. package/lib/esm/agents/chat-model.js +1 -0
  45. package/lib/esm/agents/mcp-agent.d.ts +6 -1
  46. package/lib/esm/agents/mcp-agent.js +5 -0
  47. package/lib/esm/agents/team-agent.d.ts +1 -0
  48. package/lib/esm/agents/team-agent.js +1 -0
  49. package/lib/esm/agents/user-agent.d.ts +1 -0
  50. package/lib/esm/agents/user-agent.js +6 -3
  51. package/lib/esm/aigne/aigne.d.ts +14 -5
  52. package/lib/esm/aigne/aigne.js +21 -2
  53. package/lib/esm/aigne/context.d.ts +27 -6
  54. package/lib/esm/aigne/context.js +86 -14
  55. package/lib/esm/loader/agent-yaml.d.ts +1 -1
  56. package/lib/esm/loader/index.d.ts +2 -2
  57. package/lib/esm/memory/memory.d.ts +6 -3
  58. package/lib/esm/memory/memory.js +17 -2
  59. package/lib/esm/memory/recorder.d.ts +15 -4
  60. package/lib/esm/memory/recorder.js +15 -2
  61. package/lib/esm/memory/retriever.d.ts +12 -5
  62. package/lib/esm/memory/retriever.js +10 -1
  63. package/package.json +3 -2
@@ -1,3 +1,5 @@
1
+ import type { AIGNEObserver } from "@aigne/observability";
2
+ import type { Span } from "@opentelemetry/api";
1
3
  import { Emitter } from "strict-event-emitter";
2
4
  import { Agent, type AgentInvokeOptions, type AgentProcessAsyncGenerator, type AgentResponse, type AgentResponseStream, type Message } from "../agents/agent.js";
3
5
  import type { ChatModel } from "../agents/chat-model.js";
@@ -45,6 +47,13 @@ export interface InvokeOptions<U extends UserContext = UserContext> extends Part
45
47
  returnMetadata?: boolean;
46
48
  disableTransfer?: boolean;
47
49
  sourceAgent?: Agent;
50
+ /**
51
+ * Whether to create a new context for this invocation.
52
+ * If false, the invocation will use the current context.
53
+ *
54
+ * @default true
55
+ */
56
+ newContext?: boolean;
48
57
  }
49
58
  /**
50
59
  * @hidden
@@ -57,8 +66,11 @@ export interface UserContext extends Record<string, unknown> {
57
66
  export interface Context<U extends UserContext = UserContext> extends TypedEventEmitter<ContextEventMap, ContextEmitEventMap> {
58
67
  id: string;
59
68
  parentId?: string;
69
+ rootId: string;
60
70
  model?: ChatModel;
61
71
  skills?: Agent[];
72
+ observer?: AIGNEObserver;
73
+ span?: Span;
62
74
  usage: ContextUsage;
63
75
  limits?: ContextLimits;
64
76
  status?: "normal" | "timeout";
@@ -126,12 +138,18 @@ export interface Context<U extends UserContext = UserContext> extends TypedEvent
126
138
  * @hidden
127
139
  */
128
140
  export declare class AIGNEContext implements Context {
129
- constructor(...[parent, ...args]: ConstructorParameters<typeof AIGNEContextShared>);
130
- parentId?: string;
141
+ constructor(parent?: ConstructorParameters<typeof AIGNEContextShared>[0], { reset }?: {
142
+ reset?: boolean;
143
+ });
131
144
  id: string;
145
+ parentId?: string;
146
+ rootId: string;
147
+ span?: Span;
132
148
  readonly internal: AIGNEContextShared;
149
+ get messageQueue(): MessageQueue;
133
150
  get model(): ChatModel | undefined;
134
- get skills(): Agent<Message, Message>[] | undefined;
151
+ get skills(): Agent<any, any>[] | undefined;
152
+ get observer(): AIGNEObserver | undefined;
135
153
  get limits(): ContextLimits | undefined;
136
154
  get status(): "normal" | "timeout";
137
155
  get usage(): ContextUsage;
@@ -148,19 +166,22 @@ export declare class AIGNEContext implements Context {
148
166
  subscribe: Context["subscribe"];
149
167
  unsubscribe: Context["unsubscribe"];
150
168
  emit<K extends keyof ContextEmitEventMap>(eventName: K, ...args: Args<K, ContextEmitEventMap>): boolean;
169
+ private trace;
151
170
  on<K extends keyof ContextEventMap>(eventName: K, listener: Listener<K, ContextEventMap>): this;
152
171
  once<K extends keyof ContextEventMap>(eventName: K, listener: Listener<K, ContextEventMap>): this;
153
172
  off<K extends keyof ContextEventMap>(eventName: K, listener: Listener<K, ContextEventMap>): this;
154
173
  }
155
174
  declare class AIGNEContextShared {
156
175
  private readonly parent?;
157
- constructor(parent?: (Pick<Context, "model" | "skills" | "limits"> & {
176
+ span?: Span;
177
+ constructor(parent?: (Pick<Context, "model" | "skills" | "limits" | "observer"> & {
158
178
  messageQueue?: MessageQueue;
159
- }) | undefined, overrides?: Partial<Context>);
179
+ }) | undefined);
160
180
  readonly messageQueue: MessageQueue;
161
181
  readonly events: Emitter<any>;
162
182
  get model(): ChatModel | undefined;
163
- get skills(): Agent<Message, Message>[] | undefined;
183
+ get skills(): Agent<any, any>[] | undefined;
184
+ get observer(): AIGNEObserver | undefined;
164
185
  get limits(): ContextLimits | undefined;
165
186
  usage: ContextUsage;
166
187
  userContext: Context["userContext"];
@@ -4,8 +4,8 @@ import { ProcessMode } from "../agents/team-agent.js";
4
4
  export declare function loadAgentFromYamlFile(path: string): Promise<{
5
5
  type: "mcp";
6
6
  url?: string | undefined;
7
- args?: string[] | undefined;
8
7
  command?: string | undefined;
8
+ args?: string[] | undefined;
9
9
  } | {
10
10
  type: "team";
11
11
  name: string;
@@ -17,8 +17,8 @@ export interface LoadOptions {
17
17
  }
18
18
  export declare function load(options: LoadOptions): Promise<{
19
19
  model: ChatModel | undefined;
20
- agents: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>[];
21
- skills: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>[];
20
+ agents: Agent<any, any>[];
21
+ skills: Agent<any, any>[];
22
22
  name?: string | null | undefined;
23
23
  description?: string | null | undefined;
24
24
  chat_model?: {
@@ -1,8 +1,8 @@
1
1
  import { Agent, type AgentInvokeOptions, type AgentOptions, type Message } from "../agents/agent.js";
2
2
  import type { Context } from "../aigne/context.js";
3
3
  import type { MessagePayload } from "../aigne/message-queue.js";
4
- import type { MemoryRecorder, MemoryRecorderInput, MemoryRecorderOutput } from "./recorder.js";
5
- import type { MemoryRetriever, MemoryRetrieverInput, MemoryRetrieverOutput } from "./retriever.js";
4
+ import { MemoryRecorder, type MemoryRecorderInput, type MemoryRecorderOptions, type MemoryRecorderOutput } from "./recorder.js";
5
+ import { MemoryRetriever, type MemoryRetrieverInput, type MemoryRetrieverOptions, type MemoryRetrieverOutput } from "./retriever.js";
6
6
  export interface Memory {
7
7
  id: string;
8
8
  sessionId?: string | null;
@@ -10,7 +10,9 @@ export interface Memory {
10
10
  createdAt: string;
11
11
  }
12
12
  export declare const newMemoryId: () => string;
13
- export interface MemoryAgentOptions extends Partial<Pick<MemoryAgent, "recorder" | "retriever" | "autoUpdate">>, Pick<AgentOptions, "subscribeTopic" | "skills"> {
13
+ export interface MemoryAgentOptions extends Partial<Pick<MemoryAgent, "autoUpdate">>, Pick<AgentOptions, "subscribeTopic" | "skills"> {
14
+ recorder?: MemoryRecorder | MemoryRecorderOptions["process"] | MemoryRecorderOptions;
15
+ retriever?: MemoryRetriever | MemoryRetrieverOptions["process"] | MemoryRetrieverOptions;
14
16
  }
15
17
  /**
16
18
  * A specialized agent responsible for managing, storing, and retrieving memories within the agent system.
@@ -21,6 +23,7 @@ export interface MemoryAgentOptions extends Partial<Pick<MemoryAgent, "recorder"
21
23
  * instead provides memory management capabilities to the system.
22
24
  */
23
25
  export declare class MemoryAgent extends Agent {
26
+ tag: string;
24
27
  /**
25
28
  * Creates a new MemoryAgent instance.
26
29
  */
@@ -1,5 +1,6 @@
1
1
  import { type ZodType, z } from "zod";
2
- import { Agent, type AgentOptions, type Message } from "../agents/agent.js";
2
+ import { Agent, type AgentInvokeOptions, type AgentOptions, type AgentProcessResult, type FunctionAgentFn, type Message } from "../agents/agent.js";
3
+ import type { PromiseOrValue } from "../utils/type-utils.js";
3
4
  import type { Memory } from "./memory.js";
4
5
  /**
5
6
  * Input for memory recording operations.
@@ -13,7 +14,11 @@ export interface MemoryRecorderInput extends Message {
13
14
  * Array of content items to record as memories.
14
15
  * Each item in this array will typically be converted into a separate memory entry.
15
16
  */
16
- content: unknown[];
17
+ content: {
18
+ role: "user" | "agent";
19
+ content: Message;
20
+ source?: string;
21
+ }[];
17
22
  }
18
23
  /**
19
24
  * @hidden
@@ -62,6 +67,9 @@ export declare const memoryRecorderOutputSchema: z.ZodObject<{
62
67
  createdAt: string;
63
68
  }[];
64
69
  }>;
70
+ export interface MemoryRecorderOptions extends Omit<AgentOptions<MemoryRecorderInput, MemoryRecorderOutput>, "inputSchema" | "outputSchema"> {
71
+ process?: FunctionAgentFn<MemoryRecorderInput, MemoryRecorderOutput>;
72
+ }
65
73
  /**
66
74
  * Abstract base class for agents that record and store memories.
67
75
  *
@@ -76,11 +84,14 @@ export declare const memoryRecorderOutputSchema: z.ZodObject<{
76
84
  * Custom implementations should extend this class and provide concrete
77
85
  * implementations of the process method to handle the actual storage logic.
78
86
  */
79
- export declare abstract class MemoryRecorder extends Agent<MemoryRecorderInput, MemoryRecorderOutput> {
87
+ export declare class MemoryRecorder extends Agent<MemoryRecorderInput, MemoryRecorderOutput> {
88
+ tag: string;
80
89
  /**
81
90
  * Creates a new MemoryRecorder instance with predefined input and output schemas.
82
91
  *
83
92
  * @param options - Configuration options for the memory recorder agent
84
93
  */
85
- constructor(options: Omit<AgentOptions<MemoryRecorderInput, MemoryRecorderOutput>, "inputSchema" | "outputSchema">);
94
+ constructor(options: MemoryRecorderOptions);
95
+ private _process?;
96
+ process(input: MemoryRecorderInput, options: AgentInvokeOptions): PromiseOrValue<AgentProcessResult<MemoryRecorderOutput>>;
86
97
  }
@@ -1,5 +1,6 @@
1
1
  import { z } from "zod";
2
- import { Agent, type AgentOptions, type Message } from "../agents/agent.js";
2
+ import { Agent, type AgentInvokeOptions, type AgentOptions, type AgentProcessResult, type FunctionAgentFn, type Message } from "../agents/agent.js";
3
+ import type { PromiseOrValue } from "../utils/type-utils.js";
3
4
  import type { Memory } from "./memory.js";
4
5
  /**
5
6
  * Input for memory retrieval operations.
@@ -39,11 +40,11 @@ export declare const memoryRetrieverInputSchema: z.ZodObject<{
39
40
  limit: z.ZodOptional<z.ZodNumber>;
40
41
  search: z.ZodOptional<z.ZodString>;
41
42
  }, "strip", z.ZodTypeAny, {
42
- limit?: number | undefined;
43
43
  search?: string | undefined;
44
- }, {
45
44
  limit?: number | undefined;
45
+ }, {
46
46
  search?: string | undefined;
47
+ limit?: number | undefined;
47
48
  }>;
48
49
  /**
49
50
  * @hidden
@@ -75,6 +76,9 @@ export declare const memoryRetrieverOutputSchema: z.ZodObject<{
75
76
  createdAt: string;
76
77
  }[];
77
78
  }>;
79
+ export interface MemoryRetrieverOptions extends Omit<AgentOptions<MemoryRetrieverInput, MemoryRetrieverOutput>, "inputSchema" | "outputSchema"> {
80
+ process?: FunctionAgentFn<MemoryRetrieverInput, MemoryRetrieverOutput>;
81
+ }
78
82
  /**
79
83
  * Abstract base class for agents that retrieve memories from storage.
80
84
  *
@@ -89,11 +93,14 @@ export declare const memoryRetrieverOutputSchema: z.ZodObject<{
89
93
  * Custom implementations should extend this class and provide concrete
90
94
  * implementations of the process method to handle the actual retrieval logic.
91
95
  */
92
- export declare abstract class MemoryRetriever extends Agent<MemoryRetrieverInput, MemoryRetrieverOutput> {
96
+ export declare class MemoryRetriever extends Agent<MemoryRetrieverInput, MemoryRetrieverOutput> {
97
+ tag: string;
93
98
  /**
94
99
  * Creates a new MemoryRetriever instance with predefined input and output schemas.
95
100
  *
96
101
  * @param options - Configuration options for the memory retriever agent
97
102
  */
98
- constructor(options: Omit<AgentOptions<MemoryRetrieverInput, MemoryRetrieverOutput>, "inputSchema" | "outputSchema">);
103
+ constructor(options: MemoryRetrieverOptions);
104
+ private _process?;
105
+ process(input: MemoryRetrieverInput, options: AgentInvokeOptions): PromiseOrValue<AgentProcessResult<MemoryRetrieverOutput>>;
99
106
  }
@@ -37,7 +37,7 @@ export type PublishTopic<O extends Message> = string | string[] | ((output: O) =
37
37
  * @template I The agent input message type
38
38
  * @template O The agent output message type
39
39
  */
40
- export interface AgentOptions<I extends Message = Message, O extends Message = Message> extends Partial<Pick<Agent, "guideRails" | "hooks">> {
40
+ export interface AgentOptions<I extends Message = Message, O extends Message = Message> extends Partial<Pick<Agent, "guideRails">> {
41
41
  /**
42
42
  * Topics the agent should subscribe to
43
43
  *
@@ -106,6 +106,7 @@ export interface AgentOptions<I extends Message = Message, O extends Message = M
106
106
  * Maximum number of memory items to retrieve
107
107
  */
108
108
  maxRetrieveMemoryCount?: number;
109
+ hooks?: AgentHooks<I, O>;
109
110
  }
110
111
  export declare const agentOptionsSchema: ZodObject<{
111
112
  [key in keyof AgentOptions]: ZodType<AgentOptions[key]>;
@@ -161,7 +162,7 @@ export interface AgentInvokeOptions<U extends UserContext = UserContext> {
161
162
  * Here's an example of how to create a custom agent:
162
163
  * {@includeCode ../../test/agents/agent.test.ts#example-custom-agent}
163
164
  */
164
- export declare abstract class Agent<I extends Message = Message, O extends Message = Message> {
165
+ export declare abstract class Agent<I extends Message = any, O extends Message = any> {
165
166
  /**
166
167
  * Custom object inspection behavior
167
168
  *
@@ -176,6 +177,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
176
177
  * List of memories this agent can use
177
178
  */
178
179
  readonly memories: MemoryAgent[];
180
+ tag?: string;
179
181
  /**
180
182
  * Maximum number of memory items to retrieve
181
183
  */
@@ -190,7 +192,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
190
192
  * Here's an example of using hooks:
191
193
  * {@includeCode ../../test/agents/agent.test.ts#example-agent-hooks}
192
194
  */
193
- readonly hooks: AgentHooks;
195
+ readonly hooks: AgentHooks<I, O>;
194
196
  /**
195
197
  * List of GuideRail agents applied to this agent
196
198
  *
@@ -276,8 +278,8 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
276
278
  * Skills can be accessed by name or by array index, allowing
277
279
  * the agent to delegate tasks to specialized sub-agents
278
280
  */
279
- readonly skills: Agent<Message, Message>[] & {
280
- [key: string]: Agent<Message, Message>;
281
+ readonly skills: Agent<any, any>[] & {
282
+ [key: string]: Agent<any, any>;
281
283
  };
282
284
  /**
283
285
  * Whether to disable emitting events for agent actions
@@ -518,7 +520,9 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
518
520
  onStart?: (event: {
519
521
  context: Context;
520
522
  input: I;
521
- }) => PromiseOrValue<void>;
523
+ }) => PromiseOrValue<void | {
524
+ input?: I;
525
+ }>;
522
526
  /**
523
527
  * Called when agent processing completes or fails
524
528
  *
@@ -533,7 +537,9 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
533
537
  input: I;
534
538
  output: O;
535
539
  error: Error;
536
- }, "output", "error">) => PromiseOrValue<void>;
540
+ }, "output", "error">) => PromiseOrValue<void | {
541
+ output?: O;
542
+ }>;
537
543
  /**
538
544
  * Called before a skill (sub-agent) is invoked
539
545
  *
@@ -545,7 +551,7 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
545
551
  onSkillStart?: (event: {
546
552
  context: Context;
547
553
  skill: Agent;
548
- input: I;
554
+ input: Message;
549
555
  }) => PromiseOrValue<void>;
550
556
  /**
551
557
  * Called after a skill (sub-agent) completes or fails
@@ -559,8 +565,8 @@ export interface AgentHooks<I extends Message = Message, O extends Message = Mes
559
565
  onSkillEnd?: (event: XOr<{
560
566
  context: Context;
561
567
  skill: Agent;
562
- input: I;
563
- output: O;
568
+ input: Message;
569
+ output: Message;
564
570
  error: Error;
565
571
  }, "output", "error">) => PromiseOrValue<void>;
566
572
  /**
@@ -723,6 +729,7 @@ export interface FunctionAgentOptions<I extends Message = Message, O extends Mes
723
729
  * {@includeCode ../../test/agents/agent.test.ts#example-function-agent}
724
730
  */
725
731
  export declare class FunctionAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
732
+ tag: string;
726
733
  /**
727
734
  * Create a function agent from a function or options
728
735
  *
@@ -83,6 +83,7 @@ export class Agent {
83
83
  * List of memories this agent can use
84
84
  */
85
85
  memories = [];
86
+ tag;
86
87
  /**
87
88
  * Maximum number of memory items to retrieve
88
89
  */
@@ -303,8 +304,8 @@ export class Agent {
303
304
  if (!this.disableEvents)
304
305
  opts.context.emit("agentStarted", { agent: this, input });
305
306
  try {
306
- await this.hooks.onStart?.({ context: opts.context, input });
307
- const parsedInput = checkArguments(`Agent ${this.name} input`, this.inputSchema, input);
307
+ let parsedInput = (await this.hooks.onStart?.({ context: opts.context, input }))?.input ?? input;
308
+ parsedInput = checkArguments(`Agent ${this.name} input`, this.inputSchema, input);
308
309
  await this.preprocess(parsedInput, opts);
309
310
  this.checkContextStatus(opts);
310
311
  let response = await this.process(parsedInput, opts);
@@ -367,7 +368,9 @@ export class Agent {
367
368
  logger.debug("Invoke agent %s succeed with output: %O", this.name, finalOutput);
368
369
  if (!this.disableEvents)
369
370
  context.emit("agentSucceed", { agent: this, output: finalOutput });
370
- await this.hooks.onEnd?.({ context, input, output: finalOutput });
371
+ const o = (await this.hooks.onEnd?.({ context, input, output: finalOutput }))?.output;
372
+ if (o)
373
+ return o;
371
374
  return finalOutput;
372
375
  }
373
376
  /**
@@ -596,6 +599,7 @@ function checkAgentInputOutputSchema(schema) {
596
599
  * {@includeCode ../../test/agents/agent.test.ts#example-function-agent}
597
600
  */
598
601
  export class FunctionAgent extends Agent {
602
+ tag = "FunctionAgent";
599
603
  /**
600
604
  * Create a function agent from a function or options
601
605
  *
@@ -131,7 +131,8 @@ export declare const aiAgentOptionsSchema: ZodObject<{
131
131
  * Basic AIAgent creation:
132
132
  * {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-basic}
133
133
  */
134
- export declare class AIAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
134
+ export declare class AIAgent<I extends Message = any, O extends Message = any> extends Agent<I, O> {
135
+ tag: string;
135
136
  /**
136
137
  * Create an AIAgent with the specified options
137
138
  *
@@ -77,6 +77,7 @@ export const aiAgentOptionsSchema = agentOptionsSchema.extend({
77
77
  * {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-basic}
78
78
  */
79
79
  export class AIAgent extends Agent {
80
+ tag = "AIAgent";
80
81
  /**
81
82
  * Create an AIAgent with the specified options
82
83
  *
@@ -24,6 +24,7 @@ import { Agent, type AgentInvokeOptions, type AgentProcessResult, type Message }
24
24
  * {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model-tools}
25
25
  */
26
26
  export declare abstract class ChatModel extends Agent<ChatModelInput, ChatModelOutput> {
27
+ tag: string;
27
28
  constructor();
28
29
  /**
29
30
  * Indicates whether the model supports parallel tool calls
@@ -24,6 +24,7 @@ import { Agent } from "./agent.js";
24
24
  * {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model-tools}
25
25
  */
26
26
  export class ChatModel extends Agent {
27
+ tag = "ChatModelAgent";
27
28
  constructor() {
28
29
  super({
29
30
  inputSchema: chatModelInputSchema,
@@ -57,6 +57,7 @@ export type SSEServerParameters = {
57
57
  * {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-from-sse}
58
58
  */
59
59
  export declare class MCPAgent extends Agent {
60
+ tag: string;
60
61
  /**
61
62
  * Create an MCPAgent from a connection to an SSE server.
62
63
  *
@@ -182,26 +183,30 @@ declare class ClientWithReconnect extends Client {
182
183
  private reconnect;
183
184
  request<T extends ZodType<object>>(request: Request, resultSchema: T, options?: RequestOptions): Promise<z.infer<T>>;
184
185
  }
185
- export interface MCPBaseOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
186
+ export interface MCPBaseOptions<I extends Message = any, O extends Message = any> extends AgentOptions<I, O> {
186
187
  client: ClientWithReconnect;
187
188
  }
188
189
  export declare abstract class MCPBase<I extends Message, O extends Message> extends Agent<I, O> {
190
+ tag: string;
189
191
  constructor(options: MCPBaseOptions<I, O>);
190
192
  protected client: ClientWithReconnect;
191
193
  }
192
194
  export declare class MCPTool extends MCPBase<Message, CallToolResult> {
195
+ tag: string;
193
196
  process(input: Message): Promise<CallToolResult>;
194
197
  }
195
198
  export interface MCPPromptInput extends Record<string, unknown> {
196
199
  [key: string]: string;
197
200
  }
198
201
  export declare class MCPPrompt extends MCPBase<MCPPromptInput, GetPromptResult> {
202
+ tag: string;
199
203
  process(input: MCPPromptInput): Promise<GetPromptResult>;
200
204
  }
201
205
  export interface MCPResourceOptions extends MCPBaseOptions<MCPPromptInput, ReadResourceResult> {
202
206
  uri: string;
203
207
  }
204
208
  export declare class MCPResource extends MCPBase<MCPPromptInput, ReadResourceResult> {
209
+ tag: string;
205
210
  constructor(options: MCPResourceOptions);
206
211
  uri: string;
207
212
  process(input: MCPPromptInput): Promise<ReadResourceResult>;
@@ -49,6 +49,7 @@ function getMCPServerString(options) {
49
49
  * {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-from-sse}
50
50
  */
51
51
  export class MCPAgent extends Agent {
52
+ tag = "MCPAgent";
52
53
  static from(options) {
53
54
  checkArguments("MCPAgent.from", mcpAgentOptionsSchema, options);
54
55
  if (isSSEServerParameters(options)) {
@@ -244,6 +245,7 @@ class ClientWithReconnect extends Client {
244
245
  }
245
246
  }
246
247
  export class MCPBase extends Agent {
248
+ tag = "MCPBase";
247
249
  constructor(options) {
248
250
  super(options);
249
251
  this.client = options.client;
@@ -251,18 +253,21 @@ export class MCPBase extends Agent {
251
253
  client;
252
254
  }
253
255
  export class MCPTool extends MCPBase {
256
+ tag = "MCPTool";
254
257
  async process(input) {
255
258
  const result = await this.client.callTool({ name: this.name, arguments: input });
256
259
  return result;
257
260
  }
258
261
  }
259
262
  export class MCPPrompt extends MCPBase {
263
+ tag = "MCPPrompt";
260
264
  async process(input) {
261
265
  const result = await this.client.getPrompt({ name: this.name, arguments: input });
262
266
  return result;
263
267
  }
264
268
  }
265
269
  export class MCPResource extends MCPBase {
270
+ tag = "MCPResource";
266
271
  constructor(options) {
267
272
  super(options);
268
273
  this.uri = options.uri;
@@ -52,6 +52,7 @@ export interface TeamAgentOptions<I extends Message, O extends Message> extends
52
52
  * {@includeCode ../../test/agents/team-agent.test.ts#example-team-agent-sequential}
53
53
  */
54
54
  export declare class TeamAgent<I extends Message, O extends Message> extends Agent<I, O> {
55
+ tag: string;
55
56
  /**
56
57
  * Create a TeamAgent from the provided options.
57
58
  *
@@ -42,6 +42,7 @@ export var ProcessMode;
42
42
  * {@includeCode ../../test/agents/team-agent.test.ts#example-team-agent-sequential}
43
43
  */
44
44
  export class TeamAgent extends Agent {
45
+ tag = "TeamAgent";
45
46
  /**
46
47
  * Create a TeamAgent from the provided options.
47
48
  *
@@ -7,6 +7,7 @@ export interface UserAgentOptions<I extends Message = Message, O extends Message
7
7
  activeAgent?: Agent;
8
8
  }
9
9
  export declare class UserAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
10
+ tag: string;
10
11
  static from<I extends Message, O extends Message>(options: UserAgentOptions<I, O>): UserAgent<I, O>;
11
12
  constructor(options: UserAgentOptions<I, O>);
12
13
  context: Context;
@@ -2,6 +2,7 @@ import { toMessagePayload } from "../aigne/message-queue.js";
2
2
  import { orArrayToArray } from "../utils/type-utils.js";
3
3
  import { Agent, } from "./agent.js";
4
4
  export class UserAgent extends Agent {
5
+ tag = "UserAgent";
5
6
  static from(options) {
6
7
  return new UserAgent(options);
7
8
  }
@@ -23,9 +24,8 @@ export class UserAgent extends Agent {
23
24
  super.publishToTopics(output, options);
24
25
  }
25
26
  invoke = ((input, options = {}) => {
26
- if (!options.context)
27
- this.context = this.context.newContext({ reset: true });
28
- return super.invoke(input, { ...options, context: this.context });
27
+ options.context ??= this.context.newContext({ reset: true });
28
+ return super.invoke(input, options);
29
29
  });
30
30
  async process(input, options) {
31
31
  if (this._process) {
@@ -35,6 +35,9 @@ export class UserAgent extends Agent {
35
35
  const [output, agent] = await options.context.invoke(this.activeAgent, input, {
36
36
  returnActiveAgent: true,
37
37
  streaming: true,
38
+ // Do not create a new context for the nested agent invocation,
39
+ // We are resetting the context in the override invoke method
40
+ newContext: false,
38
41
  });
39
42
  agent.then((agent) => {
40
43
  this.activeAgent = agent;
@@ -1,3 +1,4 @@
1
+ import { AIGNEObserver } from "@aigne/observability";
1
2
  import { Agent, type AgentResponse, type AgentResponseStream, type Message } from "../agents/agent.js";
2
3
  import { ChatModel } from "../agents/chat-model.js";
3
4
  import type { UserAgent } from "../agents/user-agent.js";
@@ -33,6 +34,10 @@ export interface AIGNEOptions {
33
34
  * Limits for the AIGNE instance, such as timeout, max tokens, max invocations, etc.
34
35
  */
35
36
  limits?: ContextLimits;
37
+ /**
38
+ * Observer for the AIGNE instance.
39
+ */
40
+ observer?: AIGNEObserver;
36
41
  }
37
42
  /**
38
43
  * AIGNE is a class that orchestrates multiple agents to build complex AI applications.
@@ -88,16 +93,20 @@ export declare class AIGNE<U extends UserContext = UserContext> {
88
93
  * Collection of skill agents available to this AIGNE instance.
89
94
  * Provides indexed access by skill name.
90
95
  */
91
- readonly skills: Agent<Message, Message>[] & {
92
- [key: string]: Agent<Message, Message>;
96
+ readonly skills: Agent<any, any>[] & {
97
+ [key: string]: Agent<any, any>;
93
98
  };
94
99
  /**
95
100
  * Collection of primary agents managed by this AIGNE instance.
96
101
  * Provides indexed access by agent name.
97
102
  */
98
- readonly agents: Agent<Message, Message>[] & {
99
- [key: string]: Agent<Message, Message>;
103
+ readonly agents: Agent<any, any>[] & {
104
+ [key: string]: Agent<any, any>;
100
105
  };
106
+ /**
107
+ * Observer for the AIGNE instance.
108
+ */
109
+ readonly observer?: AIGNEObserver;
101
110
  /**
102
111
  * Adds one or more agents to this AIGNE instance.
103
112
  * Each agent is attached to this AIGNE instance, allowing it to access the AIGNE's resources.
@@ -111,7 +120,7 @@ export declare class AIGNE<U extends UserContext = UserContext> {
111
120
  *
112
121
  * @returns A new AIGNEContext instance bound to this AIGNE.
113
122
  */
114
- newContext(options?: Partial<Context>): AIGNEContext;
123
+ newContext(options?: Partial<Pick<Context, "userContext" | "memories">>): AIGNEContext;
115
124
  /**
116
125
  * Creates a user agent for consistent interactions with a specified agent.
117
126
  * This method allows you to create a wrapper around an agent for repeated invocations.
@@ -1,3 +1,4 @@
1
+ import { AIGNEObserver } from "@aigne/observability";
1
2
  import { z } from "zod";
2
3
  import { Agent, } from "../agents/agent.js";
3
4
  import { ChatModel } from "../agents/chat-model.js";
@@ -49,10 +50,15 @@ export class AIGNE {
49
50
  this.description = options?.description;
50
51
  this.model = options?.model;
51
52
  this.limits = options?.limits;
53
+ this.observer =
54
+ process.env.AIGNE_OBSERVABILITY_DISABLED === "true"
55
+ ? undefined
56
+ : (options?.observer ?? new AIGNEObserver());
52
57
  if (options?.skills?.length)
53
58
  this.skills.push(...options.skills);
54
59
  if (options?.agents?.length)
55
60
  this.addAgent(...options.agents);
61
+ this.observer?.serve();
56
62
  this.initProcessExitHandler();
57
63
  }
58
64
  /**
@@ -87,6 +93,10 @@ export class AIGNE {
87
93
  * Provides indexed access by agent name.
88
94
  */
89
95
  agents = createAccessorArray([], (arr, name) => arr.find((i) => i.name === name));
96
+ /**
97
+ * Observer for the AIGNE instance.
98
+ */
99
+ observer;
90
100
  /**
91
101
  * Adds one or more agents to this AIGNE instance.
92
102
  * Each agent is attached to this AIGNE instance, allowing it to access the AIGNE's resources.
@@ -107,10 +117,17 @@ export class AIGNE {
107
117
  * @returns A new AIGNEContext instance bound to this AIGNE.
108
118
  */
109
119
  newContext(options) {
110
- return new AIGNEContext(this, options);
120
+ const context = new AIGNEContext(this);
121
+ if (options?.userContext)
122
+ context.userContext = options.userContext;
123
+ if (options?.memories)
124
+ context.memories = options.memories;
125
+ return context;
111
126
  }
112
127
  invoke(agent, message, options) {
113
- return new AIGNEContext(this).invoke(agent, message, options);
128
+ this.observer?.serve();
129
+ const context = new AIGNEContext(this);
130
+ return context.invoke(agent, message, { ...options, newContext: false });
114
131
  }
115
132
  /**
116
133
  * Publishes a message to the message queue for inter-agent communication.
@@ -126,6 +143,7 @@ export class AIGNE {
126
143
  * {@includeCode ../../test/aigne/aigne.test.ts#example-publish-message}
127
144
  */
128
145
  publish(topic, payload, options) {
146
+ this.observer?.serve();
129
147
  return new AIGNEContext(this).publish(topic, payload, options);
130
148
  }
131
149
  subscribe(topic, listener) {
@@ -188,5 +206,6 @@ const aigneOptionsSchema = z.object({
188
206
  model: z.instanceof(ChatModel).optional(),
189
207
  skills: z.array(z.instanceof(Agent)).optional(),
190
208
  agents: z.array(z.instanceof(Agent)).optional(),
209
+ observer: z.instanceof(AIGNEObserver).optional(),
191
210
  });
192
211
  const aigneAddAgentArgsSchema = z.array(z.instanceof(Agent));