@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
package/CHANGELOG.md CHANGED
@@ -5,6 +5,20 @@
5
5
 
6
6
  * add user context support ([#131](https://github.com/AIGNE-io/aigne-framework/issues/131)) ([4dd9d20](https://github.com/AIGNE-io/aigne-framework/commit/4dd9d20953f6ac33933723db56efd9b44bafeb02))
7
7
 
8
+ ## [1.22.0](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.21.0...core-v1.22.0) (2025-06-24)
9
+
10
+
11
+ ### Features
12
+
13
+ * support observability for cli and blocklet ([#155](https://github.com/AIGNE-io/aigne-framework/issues/155)) ([5baa705](https://github.com/AIGNE-io/aigne-framework/commit/5baa705a33cfdba1efc5ccbe18674c27513ca97d))
14
+
15
+
16
+ ### Dependencies
17
+
18
+ * The following workspace dependencies were updated
19
+ * dependencies
20
+ * @aigne/observability bumped to 0.1.0
21
+
8
22
  ## [1.21.0](https://github.com/AIGNE-io/aigne-framework/compare/core-v1.20.1...core-v1.21.0) (2025-06-20)
9
23
 
10
24
 
@@ -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
  *
@@ -127,6 +127,7 @@ class Agent {
127
127
  * List of memories this agent can use
128
128
  */
129
129
  memories = [];
130
+ tag;
130
131
  /**
131
132
  * Maximum number of memory items to retrieve
132
133
  */
@@ -641,6 +642,7 @@ function checkAgentInputOutputSchema(schema) {
641
642
  * {@includeCode ../../test/agents/agent.test.ts#example-function-agent}
642
643
  */
643
644
  class FunctionAgent extends Agent {
645
+ tag = "FunctionAgent";
644
646
  /**
645
647
  * Create a function agent from a function or options
646
648
  *
@@ -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
  *
@@ -80,6 +80,7 @@ exports.aiAgentOptionsSchema = agent_js_1.agentOptionsSchema.extend({
80
80
  * {@includeCode ../../test/agents/ai-agent.test.ts#example-ai-agent-basic}
81
81
  */
82
82
  class AIAgent extends agent_js_1.Agent {
83
+ tag = "AIAgent";
83
84
  /**
84
85
  * Create an AIAgent with the specified options
85
86
  *
@@ -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
@@ -27,6 +27,7 @@ const agent_js_1 = require("./agent.js");
27
27
  * {@includeCode ../../test/agents/chat-model.test.ts#example-chat-model-tools}
28
28
  */
29
29
  class ChatModel extends agent_js_1.Agent {
30
+ tag = "ChatModelAgent";
30
31
  constructor() {
31
32
  super({
32
33
  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>;
@@ -55,6 +55,7 @@ function getMCPServerString(options) {
55
55
  * {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-from-sse}
56
56
  */
57
57
  class MCPAgent extends agent_js_1.Agent {
58
+ tag = "MCPAgent";
58
59
  static from(options) {
59
60
  (0, type_utils_js_1.checkArguments)("MCPAgent.from", mcpAgentOptionsSchema, options);
60
61
  if (isSSEServerParameters(options)) {
@@ -251,6 +252,7 @@ class ClientWithReconnect extends index_js_2.Client {
251
252
  }
252
253
  }
253
254
  class MCPBase extends agent_js_1.Agent {
255
+ tag = "MCPBase";
254
256
  constructor(options) {
255
257
  super(options);
256
258
  this.client = options.client;
@@ -259,6 +261,7 @@ class MCPBase extends agent_js_1.Agent {
259
261
  }
260
262
  exports.MCPBase = MCPBase;
261
263
  class MCPTool extends MCPBase {
264
+ tag = "MCPTool";
262
265
  async process(input) {
263
266
  const result = await this.client.callTool({ name: this.name, arguments: input });
264
267
  return result;
@@ -266,6 +269,7 @@ class MCPTool extends MCPBase {
266
269
  }
267
270
  exports.MCPTool = MCPTool;
268
271
  class MCPPrompt extends MCPBase {
272
+ tag = "MCPPrompt";
269
273
  async process(input) {
270
274
  const result = await this.client.getPrompt({ name: this.name, arguments: input });
271
275
  return result;
@@ -273,6 +277,7 @@ class MCPPrompt extends MCPBase {
273
277
  }
274
278
  exports.MCPPrompt = MCPPrompt;
275
279
  class MCPResource extends MCPBase {
280
+ tag = "MCPResource";
276
281
  constructor(options) {
277
282
  super(options);
278
283
  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
  *
@@ -45,6 +45,7 @@ var ProcessMode;
45
45
  * {@includeCode ../../test/agents/team-agent.test.ts#example-team-agent-sequential}
46
46
  */
47
47
  class TeamAgent extends agent_js_1.Agent {
48
+ tag = "TeamAgent";
48
49
  /**
49
50
  * Create a TeamAgent from the provided options.
50
51
  *
@@ -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;
@@ -5,6 +5,7 @@ const message_queue_js_1 = require("../aigne/message-queue.js");
5
5
  const type_utils_js_1 = require("../utils/type-utils.js");
6
6
  const agent_js_1 = require("./agent.js");
7
7
  class UserAgent extends agent_js_1.Agent {
8
+ tag = "UserAgent";
8
9
  static from(options) {
9
10
  return new UserAgent(options);
10
11
  }
@@ -26,9 +27,8 @@ class UserAgent extends agent_js_1.Agent {
26
27
  super.publishToTopics(output, options);
27
28
  }
28
29
  invoke = ((input, options = {}) => {
29
- if (!options.context)
30
- this.context = this.context.newContext({ reset: true });
31
- return super.invoke(input, { ...options, context: this.context });
30
+ options.context ??= this.context.newContext({ reset: true });
31
+ return super.invoke(input, options);
32
32
  });
33
33
  async process(input, options) {
34
34
  if (this._process) {
@@ -38,6 +38,9 @@ class UserAgent extends agent_js_1.Agent {
38
38
  const [output, agent] = await options.context.invoke(this.activeAgent, input, {
39
39
  returnActiveAgent: true,
40
40
  streaming: true,
41
+ // Do not create a new context for the nested agent invocation,
42
+ // We are resetting the context in the override invoke method
43
+ newContext: false,
41
44
  });
42
45
  agent.then((agent) => {
43
46
  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,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AIGNE = void 0;
4
+ const observability_1 = require("@aigne/observability");
4
5
  const zod_1 = require("zod");
5
6
  const agent_js_1 = require("../agents/agent.js");
6
7
  const chat_model_js_1 = require("../agents/chat-model.js");
@@ -52,10 +53,15 @@ class AIGNE {
52
53
  this.description = options?.description;
53
54
  this.model = options?.model;
54
55
  this.limits = options?.limits;
56
+ this.observer =
57
+ process.env.AIGNE_OBSERVABILITY_DISABLED === "true"
58
+ ? undefined
59
+ : (options?.observer ?? new observability_1.AIGNEObserver());
55
60
  if (options?.skills?.length)
56
61
  this.skills.push(...options.skills);
57
62
  if (options?.agents?.length)
58
63
  this.addAgent(...options.agents);
64
+ this.observer?.serve();
59
65
  this.initProcessExitHandler();
60
66
  }
61
67
  /**
@@ -90,6 +96,10 @@ class AIGNE {
90
96
  * Provides indexed access by agent name.
91
97
  */
92
98
  agents = (0, type_utils_js_1.createAccessorArray)([], (arr, name) => arr.find((i) => i.name === name));
99
+ /**
100
+ * Observer for the AIGNE instance.
101
+ */
102
+ observer;
93
103
  /**
94
104
  * Adds one or more agents to this AIGNE instance.
95
105
  * Each agent is attached to this AIGNE instance, allowing it to access the AIGNE's resources.
@@ -110,10 +120,16 @@ class AIGNE {
110
120
  * @returns A new AIGNEContext instance bound to this AIGNE.
111
121
  */
112
122
  newContext(options) {
113
- return new context_js_1.AIGNEContext(this, options);
123
+ const context = new context_js_1.AIGNEContext(this);
124
+ if (options?.userContext)
125
+ context.userContext = options.userContext;
126
+ if (options?.memories)
127
+ context.memories = options.memories;
128
+ return context;
114
129
  }
115
130
  invoke(agent, message, options) {
116
- return new context_js_1.AIGNEContext(this).invoke(agent, message, options);
131
+ const context = new context_js_1.AIGNEContext(this);
132
+ return context.invoke(agent, message, { ...options, newContext: false });
117
133
  }
118
134
  /**
119
135
  * Publishes a message to the message queue for inter-agent communication.
@@ -192,5 +208,6 @@ const aigneOptionsSchema = zod_1.z.object({
192
208
  model: zod_1.z.instanceof(chat_model_js_1.ChatModel).optional(),
193
209
  skills: zod_1.z.array(zod_1.z.instanceof(agent_js_1.Agent)).optional(),
194
210
  agents: zod_1.z.array(zod_1.z.instanceof(agent_js_1.Agent)).optional(),
211
+ observer: zod_1.z.instanceof(observability_1.AIGNEObserver).optional(),
195
212
  });
196
213
  const aigneAddAgentArgsSchema = zod_1.z.array(zod_1.z.instanceof(agent_js_1.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"];
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.AIGNEContext = void 0;
7
+ const api_1 = require("@opentelemetry/api");
7
8
  const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));
8
9
  const strict_event_emitter_1 = require("strict-event-emitter");
9
10
  const uuid_1 = require("uuid");
@@ -12,6 +13,7 @@ const agent_js_1 = require("../agents/agent.js");
12
13
  const types_js_1 = require("../agents/types.js");
13
14
  const user_agent_js_1 = require("../agents/user-agent.js");
14
15
  const event_stream_js_1 = require("../utils/event-stream.js");
16
+ const logger_js_1 = require("../utils/logger.js");
15
17
  const promise_js_1 = require("../utils/promise.js");
16
18
  const stream_utils_js_1 = require("../utils/stream-utils.js");
17
19
  const type_utils_js_1 = require("../utils/type-utils.js");
@@ -21,24 +23,47 @@ const usage_js_1 = require("./usage.js");
21
23
  * @hidden
22
24
  */
23
25
  class AIGNEContext {
24
- constructor(...[parent, ...args]) {
25
- if (parent instanceof AIGNEContext) {
26
- this.parentId = parent.id;
26
+ constructor(parent, { reset } = {}) {
27
+ const tracer = parent?.observer?.tracer;
28
+ if (parent instanceof AIGNEContext && !reset) {
27
29
  this.internal = parent.internal;
30
+ this.parentId = parent.id;
31
+ this.rootId = parent.rootId;
32
+ if (parent.span) {
33
+ const parentContext = api_1.trace.setSpan(api_1.context.active(), parent.span);
34
+ this.span = tracer?.startSpan("childAIGNEContext", undefined, parentContext);
35
+ }
36
+ else {
37
+ if (!process.env.AIGNE_OBSERVABILITY_DISABLED) {
38
+ throw new Error("parent span is not set");
39
+ }
40
+ }
28
41
  }
29
42
  else {
30
- this.internal = new AIGNEContextShared(parent, ...args);
43
+ this.internal = new AIGNEContextShared(parent);
44
+ this.span = tracer?.startSpan("AIGNEContext");
45
+ // 修改了 rootId 是否会之前的有影响?,之前为 this.id
46
+ this.rootId = this.span?.spanContext?.().traceId ?? (0, uuid_1.v7)();
31
47
  }
48
+ this.id = this.span?.spanContext()?.spanId ?? (0, uuid_1.v7)();
32
49
  }
50
+ id;
33
51
  parentId;
34
- id = (0, uuid_1.v7)();
52
+ rootId;
53
+ span;
35
54
  internal;
55
+ get messageQueue() {
56
+ return this.internal.messageQueue;
57
+ }
36
58
  get model() {
37
59
  return this.internal.model;
38
60
  }
39
61
  get skills() {
40
62
  return this.internal.skills;
41
63
  }
64
+ get observer() {
65
+ return this.internal.observer;
66
+ }
42
67
  get limits() {
43
68
  return this.internal.limits;
44
69
  }
@@ -61,9 +86,7 @@ class AIGNEContext {
61
86
  this.internal.memories = memories;
62
87
  }
63
88
  newContext({ reset } = {}) {
64
- if (reset)
65
- return new AIGNEContext(this, { userContext: {} });
66
- return new AIGNEContext(this);
89
+ return new AIGNEContext(this, { reset });
67
90
  }
68
91
  invoke = ((agent, message, options) => {
69
92
  (0, type_utils_js_1.checkArguments)("AIGNEContext.invoke", aigneContextInvokeArgsSchema, {
@@ -85,7 +108,7 @@ class AIGNEContext {
85
108
  activeAgent: agent,
86
109
  });
87
110
  }
88
- const newContext = this.newContext();
111
+ const newContext = options?.newContext === false ? this : this.newContext();
89
112
  return Promise.resolve(newContext.internal.invoke(agent, message, newContext, options)).then(async (response) => {
90
113
  if (!options?.streaming) {
91
114
  let { __activeAgent__: activeAgent, ...output } = await (0, stream_utils_js_1.agentResponseStreamToObject)(response);
@@ -162,8 +185,55 @@ class AIGNEContext {
162
185
  timestamp: Date.now(),
163
186
  };
164
187
  const newArgs = [b, ...args.slice(1)];
188
+ this.trace(eventName, args, b);
165
189
  return this.internal.events.emit(eventName, ...newArgs);
166
190
  }
191
+ async trace(eventName, args, b) {
192
+ const span = this.span;
193
+ if (!span)
194
+ return;
195
+ try {
196
+ switch (eventName) {
197
+ case "agentStarted": {
198
+ const { agent, input } = args[0];
199
+ span.updateName(agent.name);
200
+ span.setAttribute("custom.trace_id", this.rootId);
201
+ span.setAttribute("custom.span_id", this.id);
202
+ if (this.parentId) {
203
+ span.setAttribute("custom.parent_id", this.parentId);
204
+ }
205
+ span.setAttribute("custom.started_at", b.timestamp);
206
+ span.setAttribute("input", JSON.stringify(input));
207
+ span.setAttribute("agentTag", agent.tag ?? "UnknownAgent");
208
+ break;
209
+ }
210
+ case "agentSucceed": {
211
+ const { output } = args[0];
212
+ try {
213
+ span.setAttribute("output", JSON.stringify(output));
214
+ }
215
+ catch (_e) {
216
+ logger_js_1.logger.error("parse output error", _e.message);
217
+ span.setAttribute("output", JSON.stringify({}));
218
+ }
219
+ span.setStatus({ code: api_1.SpanStatusCode.OK, message: "Agent succeed" });
220
+ span.end();
221
+ await this.observer?.traceExporter?.forceFlush?.();
222
+ break;
223
+ }
224
+ case "agentFailed": {
225
+ const { error } = args[0];
226
+ span.setStatus({ code: api_1.SpanStatusCode.ERROR, message: error.message });
227
+ span.end();
228
+ await this.observer?.traceExporter?.forceFlush?.();
229
+ break;
230
+ }
231
+ }
232
+ }
233
+ catch (err) {
234
+ logger_js_1.logger.error("AIGNEContext.trace observer error", { eventName, error: err });
235
+ }
236
+ }
167
237
  on(eventName, listener) {
168
238
  this.internal.events.on(eventName, listener);
169
239
  return this;
@@ -180,11 +250,10 @@ class AIGNEContext {
180
250
  exports.AIGNEContext = AIGNEContext;
181
251
  class AIGNEContextShared {
182
252
  parent;
183
- constructor(parent, overrides) {
253
+ span;
254
+ constructor(parent) {
184
255
  this.parent = parent;
185
256
  this.messageQueue = this.parent?.messageQueue ?? new message_queue_js_1.MessageQueue();
186
- this.userContext = overrides?.userContext ?? {};
187
- this.memories = overrides?.memories ?? [];
188
257
  }
189
258
  messageQueue;
190
259
  events = new strict_event_emitter_1.Emitter();
@@ -194,12 +263,15 @@ class AIGNEContextShared {
194
263
  get skills() {
195
264
  return this.parent?.skills;
196
265
  }
266
+ get observer() {
267
+ return this.parent?.observer;
268
+ }
197
269
  get limits() {
198
270
  return this.parent?.limits;
199
271
  }
200
272
  usage = (0, usage_js_1.newEmptyContextUsage)();
201
- userContext;
202
- memories;
273
+ userContext = {};
274
+ memories = [];
203
275
  abortController = new AbortController();
204
276
  timer;
205
277
  initTimeout() {
@@ -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
  */
@@ -16,6 +16,7 @@ exports.newMemoryId = newMemoryId;
16
16
  * instead provides memory management capabilities to the system.
17
17
  */
18
18
  class MemoryAgent extends agent_js_1.Agent {
19
+ tag = "MemoryAgent";
19
20
  /**
20
21
  * Creates a new MemoryAgent instance.
21
22
  */
@@ -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
  *
@@ -34,6 +34,7 @@ exports.memoryRecorderOutputSchema = zod_1.z.object({
34
34
  * implementations of the process method to handle the actual storage logic.
35
35
  */
36
36
  class MemoryRecorder extends agent_js_1.Agent {
37
+ tag = "MemoryRecorderAgent";
37
38
  /**
38
39
  * Creates a new MemoryRecorder instance with predefined input and output schemas.
39
40
  *
@@ -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
  *
@@ -35,6 +35,7 @@ exports.memoryRetrieverOutputSchema = zod_1.z.object({
35
35
  * implementations of the process method to handle the actual retrieval logic.
36
36
  */
37
37
  class MemoryRetriever extends agent_js_1.Agent {
38
+ tag = "MemoryRetrieverAgent";
38
39
  /**
39
40
  * Creates a new MemoryRetriever instance with predefined input and output schemas.
40
41
  *
@@ -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
  *
@@ -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
  *