@aigne/core 1.21.0 → 1.22.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 (60) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/lib/cjs/agents/agent.d.ts +2 -0
  3. package/lib/cjs/agents/agent.js +2 -0
  4. package/lib/cjs/agents/ai-agent.d.ts +1 -0
  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 +5 -0
  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 +10 -1
  15. package/lib/cjs/aigne/aigne.js +19 -2
  16. package/lib/cjs/aigne/context.d.ts +25 -4
  17. package/lib/cjs/aigne/context.js +86 -14
  18. package/lib/cjs/loader/agent-yaml.d.ts +1 -1
  19. package/lib/cjs/memory/memory.d.ts +1 -0
  20. package/lib/cjs/memory/memory.js +1 -0
  21. package/lib/cjs/memory/recorder.d.ts +1 -0
  22. package/lib/cjs/memory/recorder.js +1 -0
  23. package/lib/cjs/memory/retriever.d.ts +1 -0
  24. package/lib/cjs/memory/retriever.js +1 -0
  25. package/lib/dts/agents/agent.d.ts +2 -0
  26. package/lib/dts/agents/ai-agent.d.ts +1 -0
  27. package/lib/dts/agents/chat-model.d.ts +1 -0
  28. package/lib/dts/agents/mcp-agent.d.ts +5 -0
  29. package/lib/dts/agents/team-agent.d.ts +1 -0
  30. package/lib/dts/agents/user-agent.d.ts +1 -0
  31. package/lib/dts/aigne/aigne.d.ts +10 -1
  32. package/lib/dts/aigne/context.d.ts +25 -4
  33. package/lib/dts/loader/agent-yaml.d.ts +1 -1
  34. package/lib/dts/memory/memory.d.ts +1 -0
  35. package/lib/dts/memory/recorder.d.ts +1 -0
  36. package/lib/dts/memory/retriever.d.ts +1 -0
  37. package/lib/esm/agents/agent.d.ts +2 -0
  38. package/lib/esm/agents/agent.js +2 -0
  39. package/lib/esm/agents/ai-agent.d.ts +1 -0
  40. package/lib/esm/agents/ai-agent.js +1 -0
  41. package/lib/esm/agents/chat-model.d.ts +1 -0
  42. package/lib/esm/agents/chat-model.js +1 -0
  43. package/lib/esm/agents/mcp-agent.d.ts +5 -0
  44. package/lib/esm/agents/mcp-agent.js +5 -0
  45. package/lib/esm/agents/team-agent.d.ts +1 -0
  46. package/lib/esm/agents/team-agent.js +1 -0
  47. package/lib/esm/agents/user-agent.d.ts +1 -0
  48. package/lib/esm/agents/user-agent.js +6 -3
  49. package/lib/esm/aigne/aigne.d.ts +10 -1
  50. package/lib/esm/aigne/aigne.js +19 -2
  51. package/lib/esm/aigne/context.d.ts +25 -4
  52. package/lib/esm/aigne/context.js +86 -14
  53. package/lib/esm/loader/agent-yaml.d.ts +1 -1
  54. package/lib/esm/memory/memory.d.ts +1 -0
  55. package/lib/esm/memory/memory.js +1 -0
  56. package/lib/esm/memory/recorder.d.ts +1 -0
  57. package/lib/esm/memory/recorder.js +1 -0
  58. package/lib/esm/memory/retriever.d.ts +1 -0
  59. package/lib/esm/memory/retriever.js +1 -0
  60. package/package.json +2 -1
@@ -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
@@ -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
  *
@@ -186,22 +187,26 @@ export interface MCPBaseOptions<I extends Message = Message, O extends Message =
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>;
@@ -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
  *
@@ -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;
@@ -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.
@@ -98,6 +103,10 @@ export declare class AIGNE<U extends UserContext = UserContext> {
98
103
  readonly agents: Agent<Message, Message>[] & {
99
104
  [key: string]: Agent<Message, Message>;
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,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
151
  get skills(): Agent<Message, Message>[] | 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
183
  get skills(): Agent<Message, Message>[] | 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;
@@ -21,6 +21,7 @@ export interface MemoryAgentOptions extends Partial<Pick<MemoryAgent, "recorder"
21
21
  * instead provides memory management capabilities to the system.
22
22
  */
23
23
  export declare class MemoryAgent extends Agent {
24
+ tag: string;
24
25
  /**
25
26
  * Creates a new MemoryAgent instance.
26
27
  */
@@ -77,6 +77,7 @@ export declare const memoryRecorderOutputSchema: z.ZodObject<{
77
77
  * implementations of the process method to handle the actual storage logic.
78
78
  */
79
79
  export declare abstract class MemoryRecorder extends Agent<MemoryRecorderInput, MemoryRecorderOutput> {
80
+ tag: string;
80
81
  /**
81
82
  * Creates a new MemoryRecorder instance with predefined input and output schemas.
82
83
  *
@@ -90,6 +90,7 @@ export declare const memoryRetrieverOutputSchema: z.ZodObject<{
90
90
  * implementations of the process method to handle the actual retrieval logic.
91
91
  */
92
92
  export declare abstract class MemoryRetriever extends Agent<MemoryRetrieverInput, MemoryRetrieverOutput> {
93
+ tag: string;
93
94
  /**
94
95
  * Creates a new MemoryRetriever instance with predefined input and output schemas.
95
96
  *
@@ -176,6 +176,7 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
176
176
  * List of memories this agent can use
177
177
  */
178
178
  readonly memories: MemoryAgent[];
179
+ tag?: string;
179
180
  /**
180
181
  * Maximum number of memory items to retrieve
181
182
  */
@@ -723,6 +724,7 @@ export interface FunctionAgentOptions<I extends Message = Message, O extends Mes
723
724
  * {@includeCode ../../test/agents/agent.test.ts#example-function-agent}
724
725
  */
725
726
  export declare class FunctionAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
727
+ tag: string;
726
728
  /**
727
729
  * Create a function agent from a function or options
728
730
  *
@@ -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
  */
@@ -596,6 +597,7 @@ function checkAgentInputOutputSchema(schema) {
596
597
  * {@includeCode ../../test/agents/agent.test.ts#example-function-agent}
597
598
  */
598
599
  export class FunctionAgent extends Agent {
600
+ tag = "FunctionAgent";
599
601
  /**
600
602
  * Create a function agent from a function or options
601
603
  *
@@ -132,6 +132,7 @@ export declare const aiAgentOptionsSchema: ZodObject<{
132
132
  * {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-basic}
133
133
  */
134
134
  export declare class AIAgent<I extends Message = Message, O extends Message = Message> 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
  *
@@ -186,22 +187,26 @@ export interface MCPBaseOptions<I extends Message = Message, O extends Message =
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.
@@ -98,6 +103,10 @@ export declare class AIGNE<U extends UserContext = UserContext> {
98
103
  readonly agents: Agent<Message, Message>[] & {
99
104
  [key: string]: Agent<Message, Message>;
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,16 @@ 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
+ const context = new AIGNEContext(this);
129
+ return context.invoke(agent, message, { ...options, newContext: false });
114
130
  }
115
131
  /**
116
132
  * Publishes a message to the message queue for inter-agent communication.
@@ -188,5 +204,6 @@ const aigneOptionsSchema = z.object({
188
204
  model: z.instanceof(ChatModel).optional(),
189
205
  skills: z.array(z.instanceof(Agent)).optional(),
190
206
  agents: z.array(z.instanceof(Agent)).optional(),
207
+ observer: z.instanceof(AIGNEObserver).optional(),
191
208
  });
192
209
  const aigneAddAgentArgsSchema = z.array(z.instanceof(Agent));
@@ -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
151
  get skills(): Agent<Message, Message>[] | 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
183
  get skills(): Agent<Message, Message>[] | undefined;
184
+ get observer(): AIGNEObserver | undefined;
164
185
  get limits(): ContextLimits | undefined;
165
186
  usage: ContextUsage;
166
187
  userContext: Context["userContext"];