@aigne/core 1.16.0 → 1.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/lib/cjs/agents/agent.d.ts +42 -26
  3. package/lib/cjs/agents/agent.js +59 -55
  4. package/lib/cjs/agents/ai-agent.d.ts +4 -5
  5. package/lib/cjs/agents/ai-agent.js +11 -11
  6. package/lib/cjs/agents/chat-model.d.ts +7 -8
  7. package/lib/cjs/agents/chat-model.js +9 -9
  8. package/lib/cjs/agents/mcp-agent.d.ts +3 -4
  9. package/lib/cjs/agents/mcp-agent.js +2 -2
  10. package/lib/cjs/agents/team-agent.d.ts +7 -8
  11. package/lib/cjs/agents/team-agent.js +10 -10
  12. package/lib/cjs/agents/user-agent.d.ts +4 -4
  13. package/lib/cjs/agents/user-agent.js +10 -10
  14. package/lib/cjs/aigne/aigne.d.ts +10 -9
  15. package/lib/cjs/aigne/aigne.js +5 -4
  16. package/lib/cjs/aigne/context.d.ts +17 -7
  17. package/lib/cjs/aigne/context.js +20 -7
  18. package/lib/cjs/index.d.ts +2 -1
  19. package/lib/cjs/index.js +2 -1
  20. package/lib/cjs/loader/agent-yaml.d.ts +1 -1
  21. package/lib/cjs/memory/default-memory/default-memory-storage/index.d.ts +30 -0
  22. package/lib/cjs/memory/default-memory/default-memory-storage/index.js +69 -0
  23. package/lib/cjs/memory/default-memory/default-memory-storage/migrate.d.ts +7 -0
  24. package/lib/cjs/memory/default-memory/default-memory-storage/migrate.js +53 -0
  25. package/lib/cjs/memory/default-memory/default-memory-storage/migrations/20250523165801-init.d.ts +7 -0
  26. package/lib/cjs/memory/default-memory/default-memory-storage/migrations/20250523165801-init.js +33 -0
  27. package/lib/cjs/memory/default-memory/default-memory-storage/models/memory.d.ts +10 -0
  28. package/lib/cjs/memory/default-memory/default-memory-storage/models/memory.js +32 -0
  29. package/lib/cjs/memory/default-memory/index.d.ts +10 -0
  30. package/lib/cjs/memory/{default-memory.js → default-memory/index.js} +16 -25
  31. package/lib/cjs/memory/default-memory/storage.d.ts +13 -0
  32. package/lib/cjs/memory/default-memory/storage.js +6 -0
  33. package/lib/cjs/memory/memory.d.ts +3 -2
  34. package/lib/cjs/memory/memory.js +1 -1
  35. package/lib/cjs/prompt/prompt-builder.d.ts +7 -1
  36. package/lib/cjs/prompt/prompt-builder.js +4 -4
  37. package/lib/cjs/utils/type-utils.d.ts +2 -2
  38. package/lib/dts/agents/agent.d.ts +42 -26
  39. package/lib/dts/agents/ai-agent.d.ts +4 -5
  40. package/lib/dts/agents/chat-model.d.ts +7 -8
  41. package/lib/dts/agents/mcp-agent.d.ts +3 -4
  42. package/lib/dts/agents/team-agent.d.ts +7 -8
  43. package/lib/dts/agents/user-agent.d.ts +4 -4
  44. package/lib/dts/aigne/aigne.d.ts +10 -9
  45. package/lib/dts/aigne/context.d.ts +17 -7
  46. package/lib/dts/index.d.ts +2 -1
  47. package/lib/dts/loader/agent-yaml.d.ts +1 -1
  48. package/lib/dts/memory/default-memory/default-memory-storage/index.d.ts +30 -0
  49. package/lib/dts/memory/default-memory/default-memory-storage/migrate.d.ts +7 -0
  50. package/lib/dts/memory/default-memory/default-memory-storage/migrations/20250523165801-init.d.ts +7 -0
  51. package/lib/dts/memory/default-memory/default-memory-storage/models/memory.d.ts +10 -0
  52. package/lib/dts/memory/default-memory/index.d.ts +10 -0
  53. package/lib/dts/memory/default-memory/storage.d.ts +13 -0
  54. package/lib/dts/memory/memory.d.ts +3 -2
  55. package/lib/dts/prompt/prompt-builder.d.ts +7 -1
  56. package/lib/dts/utils/type-utils.d.ts +2 -2
  57. package/lib/esm/agents/agent.d.ts +42 -26
  58. package/lib/esm/agents/agent.js +59 -55
  59. package/lib/esm/agents/ai-agent.d.ts +4 -5
  60. package/lib/esm/agents/ai-agent.js +10 -10
  61. package/lib/esm/agents/chat-model.d.ts +7 -8
  62. package/lib/esm/agents/chat-model.js +9 -9
  63. package/lib/esm/agents/mcp-agent.d.ts +3 -4
  64. package/lib/esm/agents/mcp-agent.js +2 -2
  65. package/lib/esm/agents/team-agent.d.ts +7 -8
  66. package/lib/esm/agents/team-agent.js +10 -10
  67. package/lib/esm/agents/user-agent.d.ts +4 -4
  68. package/lib/esm/agents/user-agent.js +10 -10
  69. package/lib/esm/aigne/aigne.d.ts +10 -9
  70. package/lib/esm/aigne/aigne.js +5 -4
  71. package/lib/esm/aigne/context.d.ts +17 -7
  72. package/lib/esm/aigne/context.js +21 -8
  73. package/lib/esm/index.d.ts +2 -1
  74. package/lib/esm/index.js +2 -1
  75. package/lib/esm/loader/agent-yaml.d.ts +1 -1
  76. package/lib/esm/memory/default-memory/default-memory-storage/index.d.ts +30 -0
  77. package/lib/esm/memory/default-memory/default-memory-storage/index.js +64 -0
  78. package/lib/esm/memory/default-memory/default-memory-storage/migrate.d.ts +7 -0
  79. package/lib/esm/memory/default-memory/default-memory-storage/migrate.js +16 -0
  80. package/lib/esm/memory/default-memory/default-memory-storage/migrations/20250523165801-init.d.ts +7 -0
  81. package/lib/esm/memory/default-memory/default-memory-storage/migrations/20250523165801-init.js +28 -0
  82. package/lib/esm/memory/default-memory/default-memory-storage/models/memory.d.ts +10 -0
  83. package/lib/esm/memory/default-memory/default-memory-storage/models/memory.js +29 -0
  84. package/lib/esm/memory/default-memory/index.d.ts +10 -0
  85. package/lib/esm/memory/{default-memory.js → default-memory/index.js} +15 -24
  86. package/lib/esm/memory/default-memory/storage.d.ts +13 -0
  87. package/lib/esm/memory/default-memory/storage.js +2 -0
  88. package/lib/esm/memory/memory.d.ts +3 -2
  89. package/lib/esm/memory/memory.js +2 -2
  90. package/lib/esm/prompt/prompt-builder.d.ts +7 -1
  91. package/lib/esm/prompt/prompt-builder.js +4 -4
  92. package/lib/esm/utils/type-utils.d.ts +2 -2
  93. package/package.json +4 -1
  94. package/lib/cjs/memory/default-memory.d.ts +0 -16
  95. package/lib/dts/memory/default-memory.d.ts +0 -16
  96. package/lib/esm/memory/default-memory.d.ts +0 -16
@@ -75,12 +75,12 @@ export class ChatModel extends Agent {
75
75
  * Primarily checks if token usage exceeds limits, throwing an exception if limits are exceeded
76
76
  *
77
77
  * @param input Input message
78
- * @param context Execution context
78
+ * @param options Options for invoking the agent
79
79
  * @throws Error if token usage exceeds maximum limit
80
80
  */
81
- async preprocess(input, context) {
82
- super.preprocess(input, context);
83
- const { limits, usage } = context;
81
+ async preprocess(input, options) {
82
+ super.preprocess(input, options);
83
+ const { limits, usage } = options.context;
84
84
  const usedTokens = usage.outputTokens + usage.inputTokens;
85
85
  if (limits?.maxTokens && usedTokens >= limits.maxTokens) {
86
86
  throw new Error(`Exceeded max tokens ${usedTokens}/${limits.maxTokens}`);
@@ -109,9 +109,9 @@ export class ChatModel extends Agent {
109
109
  *
110
110
  * @param input Input message
111
111
  * @param output Output message
112
- * @param context Execution context
112
+ * @param options Options for invoking the agent
113
113
  */
114
- postprocess(input, output, context) {
114
+ async postprocess(input, output, options) {
115
115
  // Restore original tool names in the output
116
116
  if (output.toolCalls?.length) {
117
117
  const toolsMap = input._toolsMap;
@@ -125,11 +125,11 @@ export class ChatModel extends Agent {
125
125
  }
126
126
  }
127
127
  }
128
- super.postprocess(input, output, context);
128
+ super.postprocess(input, output, options);
129
129
  const { usage } = output;
130
130
  if (usage) {
131
- context.usage.outputTokens += usage.outputTokens;
132
- context.usage.inputTokens += usage.inputTokens;
131
+ options.context.usage.outputTokens += usage.outputTokens;
132
+ options.context.usage.inputTokens += usage.inputTokens;
133
133
  }
134
134
  }
135
135
  }
@@ -6,9 +6,8 @@ import type { RequestOptions } from "@modelcontextprotocol/sdk/shared/protocol.j
6
6
  import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
7
7
  import type { CallToolResult, GetPromptResult, Implementation, ReadResourceResult, Request } from "@modelcontextprotocol/sdk/types.js";
8
8
  import { type ZodType, z } from "zod";
9
- import type { Context } from "../aigne/context.js";
10
9
  import { type PromiseOrValue } from "../utils/type-utils.js";
11
- import { Agent, type AgentOptions, type Message } from "./agent.js";
10
+ import { Agent, type AgentInvokeOptions, type AgentOptions, type Message } from "./agent.js";
12
11
  export interface MCPAgentOptions extends AgentOptions {
13
12
  client: Client;
14
13
  prompts?: MCPPrompt[];
@@ -150,10 +149,10 @@ export declare class MCPAgent extends Agent {
150
149
  * throws an error if called.
151
150
  *
152
151
  * @param _input Input message (unused)
153
- * @param _context Execution context (unused)
152
+ * @param _options AgentInvokeOptions (unused)
154
153
  * @throws Error This method always throws an error since MCPAgent is not directly invokable
155
154
  */
156
- process(_input: Message, _context?: Context): Promise<Message>;
155
+ process(_input: Message, _options: AgentInvokeOptions): Promise<Message>;
157
156
  /**
158
157
  * Shut down the agent and close the MCP connection.
159
158
  *
@@ -177,10 +177,10 @@ export class MCPAgent extends Agent {
177
177
  * throws an error if called.
178
178
  *
179
179
  * @param _input Input message (unused)
180
- * @param _context Execution context (unused)
180
+ * @param _options AgentInvokeOptions (unused)
181
181
  * @throws Error This method always throws an error since MCPAgent is not directly invokable
182
182
  */
183
- async process(_input, _context) {
183
+ async process(_input, _options) {
184
184
  throw new Error("Method not implemented.");
185
185
  }
186
186
  /**
@@ -1,6 +1,5 @@
1
- import type { Context } from "../aigne/context.js";
2
1
  import { type PromiseOrValue } from "../utils/type-utils.js";
3
- import { Agent, type AgentOptions, type AgentProcessResult, type Message } from "./agent.js";
2
+ import { Agent, type AgentInvokeOptions, type AgentOptions, type AgentProcessResult, type Message } from "./agent.js";
4
3
  /**
5
4
  * Defines the processing modes available for a TeamAgent.
6
5
  *
@@ -89,10 +88,10 @@ export declare class TeamAgent<I extends Message, O extends Message> extends Age
89
88
  * - In parallel mode: Process input through all agents simultaneously and combine their outputs
90
89
  *
91
90
  * @param input The message to process
92
- * @param context The execution context
91
+ * @param options The invocation options
93
92
  * @returns A stream of message chunks that collectively form the response
94
93
  */
95
- process(input: I, context: Context): PromiseOrValue<AgentProcessResult<O>>;
94
+ process(input: I, options: AgentInvokeOptions): PromiseOrValue<AgentProcessResult<O>>;
96
95
  /**
97
96
  * Process input sequentially through each agent in the team.
98
97
  *
@@ -103,12 +102,12 @@ export declare class TeamAgent<I extends Message, O extends Message> extends Age
103
102
  * 4. Updates the team's agent list with any changes that occurred during processing
104
103
  *
105
104
  * @param input The message to process
106
- * @param context The execution context
105
+ * @param options The invocation options
107
106
  * @returns A stream of message chunks from all agents
108
107
  *
109
108
  * @private
110
109
  */
111
- _processSequential(input: I, context: Context): PromiseOrValue<AgentProcessResult<O>>;
110
+ _processSequential(input: I, options: AgentInvokeOptions): PromiseOrValue<AgentProcessResult<O>>;
112
111
  /**
113
112
  * Process input in parallel through all agents in the team.
114
113
  *
@@ -118,10 +117,10 @@ export declare class TeamAgent<I extends Message, O extends Message> extends Age
118
117
  * 3. Updates the team's agent list with any changes that occurred during processing
119
118
  *
120
119
  * @param input The message to process
121
- * @param context The execution context
120
+ * @param options The invocation options
122
121
  * @returns A stream of combined message chunks from all agents
123
122
  *
124
123
  * @private
125
124
  */
126
- _processParallel(input: I, context: Context): PromiseOrValue<AgentProcessResult<O>>;
125
+ _processParallel(input: I, options: AgentInvokeOptions): PromiseOrValue<AgentProcessResult<O>>;
127
126
  }
@@ -83,15 +83,15 @@ export class TeamAgent extends Agent {
83
83
  * - In parallel mode: Process input through all agents simultaneously and combine their outputs
84
84
  *
85
85
  * @param input The message to process
86
- * @param context The execution context
86
+ * @param options The invocation options
87
87
  * @returns A stream of message chunks that collectively form the response
88
88
  */
89
- process(input, context) {
89
+ process(input, options) {
90
90
  switch (this.mode) {
91
91
  case ProcessMode.sequential:
92
- return this._processSequential(input, context);
92
+ return this._processSequential(input, options);
93
93
  case ProcessMode.parallel:
94
- return this._processParallel(input, context);
94
+ return this._processParallel(input, options);
95
95
  }
96
96
  }
97
97
  /**
@@ -104,18 +104,18 @@ export class TeamAgent extends Agent {
104
104
  * 4. Updates the team's agent list with any changes that occurred during processing
105
105
  *
106
106
  * @param input The message to process
107
- * @param context The execution context
107
+ * @param options The invocation options
108
108
  * @returns A stream of message chunks from all agents
109
109
  *
110
110
  * @private
111
111
  */
112
- async *_processSequential(input, context) {
112
+ async *_processSequential(input, options) {
113
113
  const output = {};
114
114
  // Clone the agents to run, so that we can update the agents list during the loop
115
115
  const agents = [...this.skills];
116
116
  const newAgents = [];
117
117
  for (const agent of agents) {
118
- const [o, transferToAgent] = await context.invoke(agent, { ...input, ...output }, { returnActiveAgent: true, streaming: true });
118
+ const [o, transferToAgent] = await options.context.invoke(agent, { ...input, ...output }, { returnActiveAgent: true, streaming: true });
119
119
  for await (const chunk of o) {
120
120
  yield chunk;
121
121
  mergeAgentResponseChunk(output, chunk);
@@ -134,13 +134,13 @@ export class TeamAgent extends Agent {
134
134
  * 3. Updates the team's agent list with any changes that occurred during processing
135
135
  *
136
136
  * @param input The message to process
137
- * @param context The execution context
137
+ * @param options The invocation options
138
138
  * @returns A stream of combined message chunks from all agents
139
139
  *
140
140
  * @private
141
141
  */
142
- async *_processParallel(input, context) {
143
- const result = await Promise.all(this.skills.map((agent) => context.invoke(agent, input, { returnActiveAgent: true, streaming: true })));
142
+ async *_processParallel(input, options) {
143
+ const result = await Promise.all(this.skills.map((agent) => options.context.invoke(agent, input, { returnActiveAgent: true, streaming: true })));
144
144
  const streams = result.map((i) => i[0]);
145
145
  const read = async (index, reader) => {
146
146
  const promise = reader.read();
@@ -1,7 +1,7 @@
1
1
  import { ReadableStream } from "node:stream/web";
2
2
  import type { Context } from "../aigne/context.js";
3
3
  import { type MessagePayload } from "../aigne/message-queue.js";
4
- import { Agent, type AgentOptions, type AgentProcessResult, type FunctionAgentFn, type Message } from "./agent.js";
4
+ import { Agent, type AgentInvokeOptions, type AgentOptions, type AgentProcessResult, type FunctionAgentFn, type Message } from "./agent.js";
5
5
  export interface UserAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
6
6
  context: Context;
7
7
  process?: FunctionAgentFn<I, O>;
@@ -14,14 +14,14 @@ export declare class UserAgent<I extends Message = Message, O extends Message =
14
14
  private _process?;
15
15
  private activeAgent?;
16
16
  protected subscribeToTopics(context: Pick<Context, "subscribe">): void;
17
- protected publishToTopics(output: O, context: Context): Promise<void>;
17
+ protected publishToTopics(output: O, options: AgentInvokeOptions): Promise<void>;
18
18
  invoke: Agent<I, O>["invoke"];
19
- process(input: I, context: Context): Promise<AgentProcessResult<O>>;
19
+ process(input: I, options: AgentInvokeOptions): Promise<AgentProcessResult<O>>;
20
20
  publish: Context["publish"];
21
21
  subscribe: Context["subscribe"];
22
22
  unsubscribe: Context["unsubscribe"];
23
23
  get stream(): ReadableStream<MessagePayload & {
24
24
  topic: string;
25
25
  }>;
26
- protected checkAgentInvokesUsage(_context: Context): void;
26
+ protected checkAgentInvokesUsage(_options: AgentInvokeOptions): void;
27
27
  }
@@ -19,21 +19,21 @@ export class UserAgent extends Agent {
19
19
  if (this._process)
20
20
  super.subscribeToTopics(context);
21
21
  }
22
- async publishToTopics(output, context) {
22
+ async publishToTopics(output, options) {
23
23
  if (this._process)
24
- super.publishToTopics(output, context);
24
+ super.publishToTopics(output, options);
25
25
  }
26
- invoke = ((input, context, options) => {
27
- if (!context)
26
+ invoke = ((input, options = {}) => {
27
+ if (!options.context)
28
28
  this.context = this.context.newContext({ reset: true });
29
- return super.invoke(input, context ?? this.context, options);
29
+ return super.invoke(input, { ...options, context: this.context });
30
30
  });
31
- async process(input, context) {
31
+ async process(input, options) {
32
32
  if (this._process) {
33
- return this._process(input, context);
33
+ return this._process(input, options);
34
34
  }
35
35
  if (this.activeAgent) {
36
- const [output, agent] = await context.invoke(this.activeAgent, input, {
36
+ const [output, agent] = await options.context.invoke(this.activeAgent, input, {
37
37
  returnActiveAgent: true,
38
38
  streaming: true,
39
39
  });
@@ -44,7 +44,7 @@ export class UserAgent extends Agent {
44
44
  }
45
45
  const publicTopic = typeof this.publishTopic === "function" ? await this.publishTopic(input) : this.publishTopic;
46
46
  if (publicTopic?.length) {
47
- context.publish(publicTopic, input);
47
+ options.context.publish(publicTopic, input);
48
48
  if (this.subscribeTopic) {
49
49
  return this.subscribe(this.subscribeTopic).then((res) => res.message);
50
50
  }
@@ -77,7 +77,7 @@ export class UserAgent extends Agent {
77
77
  },
78
78
  });
79
79
  }
80
- checkAgentInvokesUsage(_context) {
80
+ checkAgentInvokesUsage(_options) {
81
81
  // ignore calls usage check for UserAgent
82
82
  }
83
83
  }
@@ -2,7 +2,7 @@ import { Agent, type AgentResponse, type AgentResponseStream, type Message } fro
2
2
  import { ChatModel } from "../agents/chat-model.js";
3
3
  import type { UserAgent } from "../agents/user-agent.js";
4
4
  import { type LoadOptions } from "../loader/index.js";
5
- import { AIGNEContext, type InvokeOptions } from "./context.js";
5
+ import { AIGNEContext, type Context, type InvokeOptions, type UserContext } from "./context.js";
6
6
  import { type MessagePayload, MessageQueue, type MessageQueueListener, type Unsubscribe } from "./message-queue.js";
7
7
  import type { ContextLimits } from "./usage.js";
8
8
  /**
@@ -46,7 +46,7 @@ export interface AIGNEOptions {
46
46
  * Here's an example of how to use AIGNE with streaming response:
47
47
  * {@includeCode ../../test/aigne/aigne.test.ts#example-streaming}
48
48
  */
49
- export declare class AIGNE {
49
+ export declare class AIGNE<U extends UserContext = UserContext> {
50
50
  /**
51
51
  * Loads an AIGNE instance from a directory containing an aigne.yaml file and agent definitions.
52
52
  * This static method provides a convenient way to initialize an AIGNE system from configuration files.
@@ -111,7 +111,7 @@ export declare class AIGNE {
111
111
  *
112
112
  * @returns A new AIGNEContext instance bound to this AIGNE.
113
113
  */
114
- newContext(): AIGNEContext;
114
+ newContext(options?: Partial<Context>): AIGNEContext;
115
115
  /**
116
116
  * Creates a user agent for consistent interactions with a specified agent.
117
117
  * This method allows you to create a wrapper around an agent for repeated invocations.
@@ -134,7 +134,7 @@ export declare class AIGNE {
134
134
  * @param options.streaming - Must be false to return a response stream
135
135
  * @returns A promise resolving to a tuple containing the agent's response and the final active agent
136
136
  */
137
- invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options: InvokeOptions & {
137
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options: InvokeOptions<U> & {
138
138
  returnActiveAgent: true;
139
139
  streaming?: false;
140
140
  }): Promise<[O, Agent]>;
@@ -148,7 +148,7 @@ export declare class AIGNE {
148
148
  * @param options.streaming - Must be true to return a response stream
149
149
  * @returns A promise resolving to a tuple containing the agent's response stream and a promise for the final agent
150
150
  */
151
- invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options: InvokeOptions & {
151
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options: InvokeOptions<U> & {
152
152
  returnActiveAgent: true;
153
153
  streaming: true;
154
154
  }): Promise<[AgentResponseStream<O>, Promise<Agent>]>;
@@ -165,7 +165,7 @@ export declare class AIGNE {
165
165
  * Here's a simple example of how to invoke an agent:
166
166
  * {@includeCode ../../test/aigne/aigne.test.ts#example-simple}
167
167
  */
168
- invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options?: InvokeOptions & {
168
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options?: InvokeOptions<U> & {
169
169
  returnActiveAgent?: false;
170
170
  streaming?: false;
171
171
  }): Promise<O>;
@@ -182,7 +182,7 @@ export declare class AIGNE {
182
182
  * Here's an example of how to invoke an agent with streaming response:
183
183
  * {@includeCode ../../test/aigne/aigne.test.ts#example-streaming}
184
184
  */
185
- invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options: InvokeOptions & {
185
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options: InvokeOptions<U> & {
186
186
  returnActiveAgent?: false;
187
187
  streaming: true;
188
188
  }): Promise<AgentResponseStream<O>>;
@@ -196,7 +196,7 @@ export declare class AIGNE {
196
196
  * @returns Either a UserAgent (when no message provided) or a promise resolving to the agent's response
197
197
  * with optional active agent information based on the provided options
198
198
  */
199
- invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message?: I | string, options?: InvokeOptions): UserAgent<I, O> | Promise<AgentResponse<O> | [AgentResponse<O>, Agent]>;
199
+ invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message?: I | string, options?: InvokeOptions<U>): UserAgent<I, O> | Promise<AgentResponse<O> | [AgentResponse<O>, Agent]>;
200
200
  /**
201
201
  * Publishes a message to the message queue for inter-agent communication.
202
202
  * This method broadcasts a message to all subscribers of the specified topic(s).
@@ -204,12 +204,13 @@ export declare class AIGNE {
204
204
  *
205
205
  * @param topic - The topic or array of topics to publish the message to
206
206
  * @param payload - The message payload to be delivered to subscribers
207
+ * @param options - Optional configuration parameters for the publish operation
207
208
  *
208
209
  * @example
209
210
  * Here's an example of how to publish a message:
210
211
  * {@includeCode ../../test/aigne/aigne.test.ts#example-publish-message}
211
212
  */
212
- publish(topic: string | string[], payload: Omit<MessagePayload, "context"> | Message | string): void;
213
+ publish(topic: string | string[], payload: Omit<MessagePayload, "context"> | Message | string, options?: InvokeOptions<U>): void;
213
214
  /**
214
215
  * Subscribes to receive the next message on a specific topic.
215
216
  * This overload returns a Promise that resolves with the next message published to the topic.
@@ -106,8 +106,8 @@ export class AIGNE {
106
106
  *
107
107
  * @returns A new AIGNEContext instance bound to this AIGNE.
108
108
  */
109
- newContext() {
110
- return new AIGNEContext(this);
109
+ newContext(options) {
110
+ return new AIGNEContext(this, options);
111
111
  }
112
112
  invoke(agent, message, options) {
113
113
  return new AIGNEContext(this).invoke(agent, message, options);
@@ -119,13 +119,14 @@ export class AIGNE {
119
119
  *
120
120
  * @param topic - The topic or array of topics to publish the message to
121
121
  * @param payload - The message payload to be delivered to subscribers
122
+ * @param options - Optional configuration parameters for the publish operation
122
123
  *
123
124
  * @example
124
125
  * Here's an example of how to publish a message:
125
126
  * {@includeCode ../../test/aigne/aigne.test.ts#example-publish-message}
126
127
  */
127
- publish(topic, payload) {
128
- return new AIGNEContext(this).publish(topic, payload);
128
+ publish(topic, payload, options) {
129
+ return new AIGNEContext(this).publish(topic, payload, options);
129
130
  }
130
131
  subscribe(topic, listener) {
131
132
  return this.messageQueue.subscribe(topic, listener);
@@ -38,20 +38,27 @@ export type ContextEmitEventMap = {
38
38
  /**
39
39
  * @hidden
40
40
  */
41
- export interface InvokeOptions extends AgentInvokeOptions {
41
+ export interface InvokeOptions<U extends UserContext = UserContext> extends Partial<Omit<AgentInvokeOptions<U>, "context">> {
42
42
  returnActiveAgent?: boolean;
43
43
  disableTransfer?: boolean;
44
44
  sourceAgent?: Agent;
45
+ userContext?: U;
45
46
  }
46
47
  /**
47
48
  * @hidden
48
49
  */
49
- export interface Context extends TypedEventEmitter<ContextEventMap, ContextEmitEventMap> {
50
+ export interface UserContext extends Record<string, unknown> {
51
+ }
52
+ /**
53
+ * @hidden
54
+ */
55
+ export interface Context<U extends UserContext = UserContext> extends TypedEventEmitter<ContextEventMap, ContextEmitEventMap> {
50
56
  model?: ChatModel;
51
57
  skills?: Agent[];
52
58
  usage: ContextUsage;
53
59
  limits?: ContextLimits;
54
60
  status?: "normal" | "timeout";
61
+ userContext: U;
55
62
  /**
56
63
  * Create a user agent to consistently invoke an agent
57
64
  * @param agent Agent to invoke
@@ -92,7 +99,7 @@ export interface Context extends TypedEventEmitter<ContextEventMap, ContextEmitE
92
99
  * @param topic topic name, or an array of topic names
93
100
  * @param payload message to publish
94
101
  */
95
- publish(topic: string | string[], payload: Omit<MessagePayload, "context"> | Message | string): void;
102
+ publish(topic: string | string[], payload: Omit<MessagePayload, "context"> | Message | string, options?: InvokeOptions): void;
96
103
  subscribe(topic: string | string[], listener?: undefined): Promise<MessagePayload>;
97
104
  subscribe(topic: string | string[], listener: MessageQueueListener): Unsubscribe;
98
105
  subscribe(topic: string | string[], listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
@@ -114,15 +121,17 @@ export interface Context extends TypedEventEmitter<ContextEventMap, ContextEmitE
114
121
  * @hidden
115
122
  */
116
123
  export declare class AIGNEContext implements Context {
117
- constructor(parent?: ConstructorParameters<typeof AIGNEContextInternal>[0]);
124
+ constructor(...[parent, ...args]: ConstructorParameters<typeof AIGNEContextShared>);
118
125
  parentId?: string;
119
126
  id: string;
120
- readonly internal: AIGNEContextInternal;
127
+ readonly internal: AIGNEContextShared;
121
128
  get model(): ChatModel | undefined;
122
129
  get skills(): Agent<Message, Message>[] | undefined;
123
130
  get limits(): ContextLimits | undefined;
124
131
  get status(): "normal" | "timeout";
125
132
  get usage(): ContextUsage;
133
+ get userContext(): Context["userContext"];
134
+ set userContext(userContext: Context["userContext"]);
126
135
  newContext({ reset }?: {
127
136
  reset?: boolean;
128
137
  }): AIGNEContext;
@@ -135,17 +144,18 @@ export declare class AIGNEContext implements Context {
135
144
  once<K extends keyof ContextEventMap>(eventName: K, listener: Listener<K, ContextEventMap>): this;
136
145
  off<K extends keyof ContextEventMap>(eventName: K, listener: Listener<K, ContextEventMap>): this;
137
146
  }
138
- declare class AIGNEContextInternal {
147
+ declare class AIGNEContextShared {
139
148
  private readonly parent?;
140
149
  constructor(parent?: (Pick<Context, "model" | "skills" | "limits"> & {
141
150
  messageQueue?: MessageQueue;
142
- }) | undefined);
151
+ }) | undefined, overrides?: Partial<Context>);
143
152
  readonly messageQueue: MessageQueue;
144
153
  readonly events: EventEmitter<ContextEventMap>;
145
154
  get model(): ChatModel | undefined;
146
155
  get skills(): Agent<Message, Message>[] | undefined;
147
156
  get limits(): ContextLimits | undefined;
148
157
  usage: ContextUsage;
158
+ userContext: Context["userContext"];
149
159
  private abortController;
150
160
  private timer?;
151
161
  private initTimeout;
@@ -2,7 +2,7 @@ import EventEmitter from "node:events";
2
2
  import { v7 } from "uuid";
3
3
  import { z } from "zod";
4
4
  import { Agent, } from "../agents/agent.js";
5
- import { isTransferAgentOutput, transferAgentOutputKey } from "../agents/types.js";
5
+ import { isTransferAgentOutput, transferAgentOutputKey, } from "../agents/types.js";
6
6
  import { UserAgent } from "../agents/user-agent.js";
7
7
  import { createMessage } from "../prompt/prompt-builder.js";
8
8
  import { agentResponseStreamToObject, asyncGeneratorToReadableStream, onAgentResponseStreamEnd, } from "../utils/stream-utils.js";
@@ -13,13 +13,13 @@ import { newEmptyContextUsage } from "./usage.js";
13
13
  * @hidden
14
14
  */
15
15
  export class AIGNEContext {
16
- constructor(parent) {
16
+ constructor(...[parent, ...args]) {
17
17
  if (parent instanceof AIGNEContext) {
18
18
  this.parentId = parent.id;
19
19
  this.internal = parent.internal;
20
20
  }
21
21
  else {
22
- this.internal = new AIGNEContextInternal(parent);
22
+ this.internal = new AIGNEContextShared(parent, ...args);
23
23
  }
24
24
  }
25
25
  parentId;
@@ -40,9 +40,15 @@ export class AIGNEContext {
40
40
  get usage() {
41
41
  return this.internal.usage;
42
42
  }
43
+ get userContext() {
44
+ return this.internal.userContext;
45
+ }
46
+ set userContext(userContext) {
47
+ this.internal.userContext = userContext;
48
+ }
43
49
  newContext({ reset } = {}) {
44
50
  if (reset)
45
- return new AIGNEContext(this.internal);
51
+ return new AIGNEContext(this, { userContext: {} });
46
52
  return new AIGNEContext(this);
47
53
  }
48
54
  invoke = ((agent, message, options) => {
@@ -51,6 +57,8 @@ export class AIGNEContext {
51
57
  message,
52
58
  options,
53
59
  });
60
+ if (options?.userContext)
61
+ Object.assign(this.userContext, options.userContext);
54
62
  if (isNil(message)) {
55
63
  return UserAgent.from({
56
64
  context: this,
@@ -90,7 +98,9 @@ export class AIGNEContext {
90
98
  return stream;
91
99
  });
92
100
  });
93
- publish = ((topic, payload) => {
101
+ publish = ((topic, payload, options) => {
102
+ if (options?.userContext)
103
+ Object.assign(this.userContext, options.userContext);
94
104
  return this.internal.messageQueue.publish(topic, {
95
105
  ...toMessagePayload(payload),
96
106
  context: this,
@@ -125,11 +135,12 @@ export class AIGNEContext {
125
135
  return this;
126
136
  }
127
137
  }
128
- class AIGNEContextInternal {
138
+ class AIGNEContextShared {
129
139
  parent;
130
- constructor(parent) {
140
+ constructor(parent, overrides) {
131
141
  this.parent = parent;
132
142
  this.messageQueue = this.parent?.messageQueue ?? new MessageQueue();
143
+ this.userContext = overrides?.userContext ?? {};
133
144
  }
134
145
  messageQueue;
135
146
  events = new EventEmitter();
@@ -143,6 +154,7 @@ class AIGNEContextInternal {
143
154
  return this.parent?.limits;
144
155
  }
145
156
  usage = newEmptyContextUsage();
157
+ userContext;
146
158
  abortController = new AbortController();
147
159
  timer;
148
160
  initTimeout() {
@@ -169,12 +181,13 @@ class AIGNEContextInternal {
169
181
  const result = {};
170
182
  if (options?.sourceAgent && activeAgent !== options.sourceAgent) {
171
183
  options.sourceAgent.hooks.onHandoff?.({
184
+ context,
172
185
  source: options.sourceAgent,
173
186
  target: activeAgent,
174
187
  input,
175
188
  });
176
189
  }
177
- const stream = await activeAgent.invoke(input, context, { streaming: true });
190
+ const stream = await activeAgent.invoke(input, { ...options, context, streaming: true });
178
191
  for await (const value of stream) {
179
192
  if (value.delta.text) {
180
193
  yield { delta: { text: value.delta.text } };
@@ -1,10 +1,11 @@
1
1
  export * from "./agents/agent.js";
2
2
  export * from "./agents/ai-agent.js";
3
+ export * from "./agents/chat-model.js";
4
+ export * from "./agents/guide-rail-agent.js";
3
5
  export * from "./agents/mcp-agent.js";
4
6
  export * from "./agents/team-agent.js";
5
7
  export * from "./agents/types.js";
6
8
  export * from "./agents/user-agent.js";
7
9
  export * from "./aigne/index.js";
8
- export * from "./agents/chat-model.js";
9
10
  export * from "./prompt/prompt-builder.js";
10
11
  export * from "./prompt/template.js";
package/lib/esm/index.js CHANGED
@@ -1,10 +1,11 @@
1
1
  export * from "./agents/agent.js";
2
2
  export * from "./agents/ai-agent.js";
3
+ export * from "./agents/chat-model.js";
4
+ export * from "./agents/guide-rail-agent.js";
3
5
  export * from "./agents/mcp-agent.js";
4
6
  export * from "./agents/team-agent.js";
5
7
  export * from "./agents/types.js";
6
8
  export * from "./agents/user-agent.js";
7
9
  export * from "./aigne/index.js";
8
- export * from "./agents/chat-model.js";
9
10
  export * from "./prompt/prompt-builder.js";
10
11
  export * from "./prompt/template.js";
@@ -24,6 +24,6 @@ export declare function loadAgentFromYamlFile(path: string): Promise<{
24
24
  } | {
25
25
  type: "mcp";
26
26
  url?: string | undefined;
27
- command?: string | undefined;
28
27
  args?: string[] | undefined;
28
+ command?: string | undefined;
29
29
  }>;
@@ -0,0 +1,30 @@
1
+ import type { AgentInvokeOptions } from "../../../agents/agent.js";
2
+ import "sqlite3";
3
+ import { Sequelize } from "sequelize";
4
+ import type { Context } from "../../../aigne/context.js";
5
+ import type { PromiseOrValue } from "../../../utils/type-utils.js";
6
+ import type { Memory } from "../../memory.js";
7
+ import { MemoryStorage } from "../storage.js";
8
+ export interface DefaultMemoryStorageOptions {
9
+ path?: string;
10
+ getSessionId?: (context: Context) => PromiseOrValue<string>;
11
+ }
12
+ export declare class DefaultMemoryStorage extends MemoryStorage {
13
+ options?: DefaultMemoryStorageOptions | undefined;
14
+ memories: {
15
+ [groupId: string]: Memory[];
16
+ };
17
+ constructor(options?: DefaultMemoryStorageOptions | undefined);
18
+ private _models?;
19
+ private get models();
20
+ private convertMemory;
21
+ search(query: {
22
+ limit?: number;
23
+ }, { context }: AgentInvokeOptions): Promise<{
24
+ result: Memory[];
25
+ }>;
26
+ create(memory: Pick<Memory, "content">, { context }: AgentInvokeOptions): Promise<{
27
+ result: Memory;
28
+ }>;
29
+ }
30
+ export declare function initSequelize(path?: string): Sequelize;