@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
@@ -1,3 +1,4 @@
1
+ import { SpanStatusCode, context, trace } from "@opentelemetry/api";
1
2
  import equal from "fast-deep-equal";
2
3
  import { Emitter } from "strict-event-emitter";
3
4
  import { v7 } from "uuid";
@@ -6,6 +7,7 @@ import { Agent, isAgentResponseDelta, isEmptyChunk, } from "../agents/agent.js";
6
7
  import { isTransferAgentOutput, transferAgentOutputKey, } from "../agents/types.js";
7
8
  import { UserAgent } from "../agents/user-agent.js";
8
9
  import { AgentResponseProgressStream } from "../utils/event-stream.js";
10
+ import { logger } from "../utils/logger.js";
9
11
  import { promiseWithResolvers } from "../utils/promise.js";
10
12
  import { agentResponseStreamToObject, asyncGeneratorToReadableStream, mergeReadableStreams, onAgentResponseStreamEnd, } from "../utils/stream-utils.js";
11
13
  import { checkArguments, isEmpty, isNil, omit, } from "../utils/type-utils.js";
@@ -15,24 +17,47 @@ import { newEmptyContextUsage } from "./usage.js";
15
17
  * @hidden
16
18
  */
17
19
  export class AIGNEContext {
18
- constructor(...[parent, ...args]) {
19
- if (parent instanceof AIGNEContext) {
20
- this.parentId = parent.id;
20
+ constructor(parent, { reset } = {}) {
21
+ const tracer = parent?.observer?.tracer;
22
+ if (parent instanceof AIGNEContext && !reset) {
21
23
  this.internal = parent.internal;
24
+ this.parentId = parent.id;
25
+ this.rootId = parent.rootId;
26
+ if (parent.span) {
27
+ const parentContext = trace.setSpan(context.active(), parent.span);
28
+ this.span = tracer?.startSpan("childAIGNEContext", undefined, parentContext);
29
+ }
30
+ else {
31
+ if (!process.env.AIGNE_OBSERVABILITY_DISABLED) {
32
+ throw new Error("parent span is not set");
33
+ }
34
+ }
22
35
  }
23
36
  else {
24
- this.internal = new AIGNEContextShared(parent, ...args);
37
+ this.internal = new AIGNEContextShared(parent);
38
+ this.span = tracer?.startSpan("AIGNEContext");
39
+ // 修改了 rootId 是否会之前的有影响?,之前为 this.id
40
+ this.rootId = this.span?.spanContext?.().traceId ?? v7();
25
41
  }
42
+ this.id = this.span?.spanContext()?.spanId ?? v7();
26
43
  }
44
+ id;
27
45
  parentId;
28
- id = v7();
46
+ rootId;
47
+ span;
29
48
  internal;
49
+ get messageQueue() {
50
+ return this.internal.messageQueue;
51
+ }
30
52
  get model() {
31
53
  return this.internal.model;
32
54
  }
33
55
  get skills() {
34
56
  return this.internal.skills;
35
57
  }
58
+ get observer() {
59
+ return this.internal.observer;
60
+ }
36
61
  get limits() {
37
62
  return this.internal.limits;
38
63
  }
@@ -55,9 +80,7 @@ export class AIGNEContext {
55
80
  this.internal.memories = memories;
56
81
  }
57
82
  newContext({ reset } = {}) {
58
- if (reset)
59
- return new AIGNEContext(this, { userContext: {} });
60
- return new AIGNEContext(this);
83
+ return new AIGNEContext(this, { reset });
61
84
  }
62
85
  invoke = ((agent, message, options) => {
63
86
  checkArguments("AIGNEContext.invoke", aigneContextInvokeArgsSchema, {
@@ -79,7 +102,7 @@ export class AIGNEContext {
79
102
  activeAgent: agent,
80
103
  });
81
104
  }
82
- const newContext = this.newContext();
105
+ const newContext = options?.newContext === false ? this : this.newContext();
83
106
  return Promise.resolve(newContext.internal.invoke(agent, message, newContext, options)).then(async (response) => {
84
107
  if (!options?.streaming) {
85
108
  let { __activeAgent__: activeAgent, ...output } = await agentResponseStreamToObject(response);
@@ -156,8 +179,55 @@ export class AIGNEContext {
156
179
  timestamp: Date.now(),
157
180
  };
158
181
  const newArgs = [b, ...args.slice(1)];
182
+ this.trace(eventName, args, b);
159
183
  return this.internal.events.emit(eventName, ...newArgs);
160
184
  }
185
+ async trace(eventName, args, b) {
186
+ const span = this.span;
187
+ if (!span)
188
+ return;
189
+ try {
190
+ switch (eventName) {
191
+ case "agentStarted": {
192
+ const { agent, input } = args[0];
193
+ span.updateName(agent.name);
194
+ span.setAttribute("custom.trace_id", this.rootId);
195
+ span.setAttribute("custom.span_id", this.id);
196
+ if (this.parentId) {
197
+ span.setAttribute("custom.parent_id", this.parentId);
198
+ }
199
+ span.setAttribute("custom.started_at", b.timestamp);
200
+ span.setAttribute("input", JSON.stringify(input));
201
+ span.setAttribute("agentTag", agent.tag ?? "UnknownAgent");
202
+ break;
203
+ }
204
+ case "agentSucceed": {
205
+ const { output } = args[0];
206
+ try {
207
+ span.setAttribute("output", JSON.stringify(output));
208
+ }
209
+ catch (_e) {
210
+ logger.error("parse output error", _e.message);
211
+ span.setAttribute("output", JSON.stringify({}));
212
+ }
213
+ span.setStatus({ code: SpanStatusCode.OK, message: "Agent succeed" });
214
+ span.end();
215
+ await this.observer?.traceExporter?.forceFlush?.();
216
+ break;
217
+ }
218
+ case "agentFailed": {
219
+ const { error } = args[0];
220
+ span.setStatus({ code: SpanStatusCode.ERROR, message: error.message });
221
+ span.end();
222
+ await this.observer?.traceExporter?.forceFlush?.();
223
+ break;
224
+ }
225
+ }
226
+ }
227
+ catch (err) {
228
+ logger.error("AIGNEContext.trace observer error", { eventName, error: err });
229
+ }
230
+ }
161
231
  on(eventName, listener) {
162
232
  this.internal.events.on(eventName, listener);
163
233
  return this;
@@ -173,11 +243,10 @@ export class AIGNEContext {
173
243
  }
174
244
  class AIGNEContextShared {
175
245
  parent;
176
- constructor(parent, overrides) {
246
+ span;
247
+ constructor(parent) {
177
248
  this.parent = parent;
178
249
  this.messageQueue = this.parent?.messageQueue ?? new MessageQueue();
179
- this.userContext = overrides?.userContext ?? {};
180
- this.memories = overrides?.memories ?? [];
181
250
  }
182
251
  messageQueue;
183
252
  events = new Emitter();
@@ -187,12 +256,15 @@ class AIGNEContextShared {
187
256
  get skills() {
188
257
  return this.parent?.skills;
189
258
  }
259
+ get observer() {
260
+ return this.parent?.observer;
261
+ }
190
262
  get limits() {
191
263
  return this.parent?.limits;
192
264
  }
193
265
  usage = newEmptyContextUsage();
194
- userContext;
195
- memories;
266
+ userContext = {};
267
+ memories = [];
196
268
  abortController = new AbortController();
197
269
  timer;
198
270
  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
  */
@@ -12,6 +12,7 @@ export const newMemoryId = () => v7();
12
12
  * instead provides memory management capabilities to the system.
13
13
  */
14
14
  export class MemoryAgent extends Agent {
15
+ tag = "MemoryAgent";
15
16
  /**
16
17
  * Creates a new MemoryAgent instance.
17
18
  */
@@ -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
  *
@@ -31,6 +31,7 @@ export const memoryRecorderOutputSchema = z.object({
31
31
  * implementations of the process method to handle the actual storage logic.
32
32
  */
33
33
  export class MemoryRecorder extends Agent {
34
+ tag = "MemoryRecorderAgent";
34
35
  /**
35
36
  * Creates a new MemoryRecorder instance with predefined input and output schemas.
36
37
  *
@@ -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
  *
@@ -32,6 +32,7 @@ export const memoryRetrieverOutputSchema = z.object({
32
32
  * implementations of the process method to handle the actual retrieval logic.
33
33
  */
34
34
  export class MemoryRetriever extends Agent {
35
+ tag = "MemoryRetrieverAgent";
35
36
  /**
36
37
  * Creates a new MemoryRetriever instance with predefined input and output schemas.
37
38
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aigne/core",
3
- "version": "1.21.0",
3
+ "version": "1.22.0",
4
4
  "description": "AIGNE core library for building AI-powered applications",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -81,6 +81,7 @@
81
81
  "yaml": "^2.7.1",
82
82
  "zod": "^3.24.4",
83
83
  "zod-to-json-schema": "^3.24.5",
84
+ "@aigne/observability": "^0.1.0",
84
85
  "@aigne/platform-helpers": "^0.1.2"
85
86
  },
86
87
  "devDependencies": {