@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
@@ -78,12 +78,12 @@ class ChatModel extends agent_js_1.Agent {
78
78
  * Primarily checks if token usage exceeds limits, throwing an exception if limits are exceeded
79
79
  *
80
80
  * @param input Input message
81
- * @param context Execution context
81
+ * @param options Options for invoking the agent
82
82
  * @throws Error if token usage exceeds maximum limit
83
83
  */
84
- async preprocess(input, context) {
85
- super.preprocess(input, context);
86
- const { limits, usage } = context;
84
+ async preprocess(input, options) {
85
+ super.preprocess(input, options);
86
+ const { limits, usage } = options.context;
87
87
  const usedTokens = usage.outputTokens + usage.inputTokens;
88
88
  if (limits?.maxTokens && usedTokens >= limits.maxTokens) {
89
89
  throw new Error(`Exceeded max tokens ${usedTokens}/${limits.maxTokens}`);
@@ -112,9 +112,9 @@ class ChatModel extends agent_js_1.Agent {
112
112
  *
113
113
  * @param input Input message
114
114
  * @param output Output message
115
- * @param context Execution context
115
+ * @param options Options for invoking the agent
116
116
  */
117
- postprocess(input, output, context) {
117
+ async postprocess(input, output, options) {
118
118
  // Restore original tool names in the output
119
119
  if (output.toolCalls?.length) {
120
120
  const toolsMap = input._toolsMap;
@@ -128,11 +128,11 @@ class ChatModel extends agent_js_1.Agent {
128
128
  }
129
129
  }
130
130
  }
131
- super.postprocess(input, output, context);
131
+ super.postprocess(input, output, options);
132
132
  const { usage } = output;
133
133
  if (usage) {
134
- context.usage.outputTokens += usage.outputTokens;
135
- context.usage.inputTokens += usage.inputTokens;
134
+ options.context.usage.outputTokens += usage.outputTokens;
135
+ options.context.usage.inputTokens += usage.inputTokens;
136
136
  }
137
137
  }
138
138
  }
@@ -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
  *
@@ -183,10 +183,10 @@ class MCPAgent extends agent_js_1.Agent {
183
183
  * throws an error if called.
184
184
  *
185
185
  * @param _input Input message (unused)
186
- * @param _context Execution context (unused)
186
+ * @param _options AgentInvokeOptions (unused)
187
187
  * @throws Error This method always throws an error since MCPAgent is not directly invokable
188
188
  */
189
- async process(_input, _context) {
189
+ async process(_input, _options) {
190
190
  throw new Error("Method not implemented.");
191
191
  }
192
192
  /**
@@ -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
  }
@@ -86,15 +86,15 @@ class TeamAgent extends agent_js_1.Agent {
86
86
  * - In parallel mode: Process input through all agents simultaneously and combine their outputs
87
87
  *
88
88
  * @param input The message to process
89
- * @param context The execution context
89
+ * @param options The invocation options
90
90
  * @returns A stream of message chunks that collectively form the response
91
91
  */
92
- process(input, context) {
92
+ process(input, options) {
93
93
  switch (this.mode) {
94
94
  case ProcessMode.sequential:
95
- return this._processSequential(input, context);
95
+ return this._processSequential(input, options);
96
96
  case ProcessMode.parallel:
97
- return this._processParallel(input, context);
97
+ return this._processParallel(input, options);
98
98
  }
99
99
  }
100
100
  /**
@@ -107,18 +107,18 @@ class TeamAgent extends agent_js_1.Agent {
107
107
  * 4. Updates the team's agent list with any changes that occurred during processing
108
108
  *
109
109
  * @param input The message to process
110
- * @param context The execution context
110
+ * @param options The invocation options
111
111
  * @returns A stream of message chunks from all agents
112
112
  *
113
113
  * @private
114
114
  */
115
- async *_processSequential(input, context) {
115
+ async *_processSequential(input, options) {
116
116
  const output = {};
117
117
  // Clone the agents to run, so that we can update the agents list during the loop
118
118
  const agents = [...this.skills];
119
119
  const newAgents = [];
120
120
  for (const agent of agents) {
121
- const [o, transferToAgent] = await context.invoke(agent, { ...input, ...output }, { returnActiveAgent: true, streaming: true });
121
+ const [o, transferToAgent] = await options.context.invoke(agent, { ...input, ...output }, { returnActiveAgent: true, streaming: true });
122
122
  for await (const chunk of o) {
123
123
  yield chunk;
124
124
  (0, stream_utils_js_1.mergeAgentResponseChunk)(output, chunk);
@@ -137,13 +137,13 @@ class TeamAgent extends agent_js_1.Agent {
137
137
  * 3. Updates the team's agent list with any changes that occurred during processing
138
138
  *
139
139
  * @param input The message to process
140
- * @param context The execution context
140
+ * @param options The invocation options
141
141
  * @returns A stream of combined message chunks from all agents
142
142
  *
143
143
  * @private
144
144
  */
145
- async *_processParallel(input, context) {
146
- const result = await Promise.all(this.skills.map((agent) => context.invoke(agent, input, { returnActiveAgent: true, streaming: true })));
145
+ async *_processParallel(input, options) {
146
+ const result = await Promise.all(this.skills.map((agent) => options.context.invoke(agent, input, { returnActiveAgent: true, streaming: true })));
147
147
  const streams = result.map((i) => i[0]);
148
148
  const read = async (index, reader) => {
149
149
  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
  }
@@ -22,21 +22,21 @@ class UserAgent extends agent_js_1.Agent {
22
22
  if (this._process)
23
23
  super.subscribeToTopics(context);
24
24
  }
25
- async publishToTopics(output, context) {
25
+ async publishToTopics(output, options) {
26
26
  if (this._process)
27
- super.publishToTopics(output, context);
27
+ super.publishToTopics(output, options);
28
28
  }
29
- invoke = ((input, context, options) => {
30
- if (!context)
29
+ invoke = ((input, options = {}) => {
30
+ if (!options.context)
31
31
  this.context = this.context.newContext({ reset: true });
32
- return super.invoke(input, context ?? this.context, options);
32
+ return super.invoke(input, { ...options, context: this.context });
33
33
  });
34
- async process(input, context) {
34
+ async process(input, options) {
35
35
  if (this._process) {
36
- return this._process(input, context);
36
+ return this._process(input, options);
37
37
  }
38
38
  if (this.activeAgent) {
39
- const [output, agent] = await context.invoke(this.activeAgent, input, {
39
+ const [output, agent] = await options.context.invoke(this.activeAgent, input, {
40
40
  returnActiveAgent: true,
41
41
  streaming: true,
42
42
  });
@@ -47,7 +47,7 @@ class UserAgent extends agent_js_1.Agent {
47
47
  }
48
48
  const publicTopic = typeof this.publishTopic === "function" ? await this.publishTopic(input) : this.publishTopic;
49
49
  if (publicTopic?.length) {
50
- context.publish(publicTopic, input);
50
+ options.context.publish(publicTopic, input);
51
51
  if (this.subscribeTopic) {
52
52
  return this.subscribe(this.subscribeTopic).then((res) => res.message);
53
53
  }
@@ -80,7 +80,7 @@ class UserAgent extends agent_js_1.Agent {
80
80
  },
81
81
  });
82
82
  }
83
- checkAgentInvokesUsage(_context) {
83
+ checkAgentInvokesUsage(_options) {
84
84
  // ignore calls usage check for UserAgent
85
85
  }
86
86
  }
@@ -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.
@@ -109,8 +109,8 @@ class AIGNE {
109
109
  *
110
110
  * @returns A new AIGNEContext instance bound to this AIGNE.
111
111
  */
112
- newContext() {
113
- return new context_js_1.AIGNEContext(this);
112
+ newContext(options) {
113
+ return new context_js_1.AIGNEContext(this, options);
114
114
  }
115
115
  invoke(agent, message, options) {
116
116
  return new context_js_1.AIGNEContext(this).invoke(agent, message, options);
@@ -122,13 +122,14 @@ class AIGNE {
122
122
  *
123
123
  * @param topic - The topic or array of topics to publish the message to
124
124
  * @param payload - The message payload to be delivered to subscribers
125
+ * @param options - Optional configuration parameters for the publish operation
125
126
  *
126
127
  * @example
127
128
  * Here's an example of how to publish a message:
128
129
  * {@includeCode ../../test/aigne/aigne.test.ts#example-publish-message}
129
130
  */
130
- publish(topic, payload) {
131
- return new context_js_1.AIGNEContext(this).publish(topic, payload);
131
+ publish(topic, payload, options) {
132
+ return new context_js_1.AIGNEContext(this).publish(topic, payload, options);
132
133
  }
133
134
  subscribe(topic, listener) {
134
135
  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;
@@ -19,13 +19,13 @@ const usage_js_1 = require("./usage.js");
19
19
  * @hidden
20
20
  */
21
21
  class AIGNEContext {
22
- constructor(parent) {
22
+ constructor(...[parent, ...args]) {
23
23
  if (parent instanceof AIGNEContext) {
24
24
  this.parentId = parent.id;
25
25
  this.internal = parent.internal;
26
26
  }
27
27
  else {
28
- this.internal = new AIGNEContextInternal(parent);
28
+ this.internal = new AIGNEContextShared(parent, ...args);
29
29
  }
30
30
  }
31
31
  parentId;
@@ -46,9 +46,15 @@ class AIGNEContext {
46
46
  get usage() {
47
47
  return this.internal.usage;
48
48
  }
49
+ get userContext() {
50
+ return this.internal.userContext;
51
+ }
52
+ set userContext(userContext) {
53
+ this.internal.userContext = userContext;
54
+ }
49
55
  newContext({ reset } = {}) {
50
56
  if (reset)
51
- return new AIGNEContext(this.internal);
57
+ return new AIGNEContext(this, { userContext: {} });
52
58
  return new AIGNEContext(this);
53
59
  }
54
60
  invoke = ((agent, message, options) => {
@@ -57,6 +63,8 @@ class AIGNEContext {
57
63
  message,
58
64
  options,
59
65
  });
66
+ if (options?.userContext)
67
+ Object.assign(this.userContext, options.userContext);
60
68
  if ((0, type_utils_js_1.isNil)(message)) {
61
69
  return user_agent_js_1.UserAgent.from({
62
70
  context: this,
@@ -96,7 +104,9 @@ class AIGNEContext {
96
104
  return stream;
97
105
  });
98
106
  });
99
- publish = ((topic, payload) => {
107
+ publish = ((topic, payload, options) => {
108
+ if (options?.userContext)
109
+ Object.assign(this.userContext, options.userContext);
100
110
  return this.internal.messageQueue.publish(topic, {
101
111
  ...(0, message_queue_js_1.toMessagePayload)(payload),
102
112
  context: this,
@@ -132,11 +142,12 @@ class AIGNEContext {
132
142
  }
133
143
  }
134
144
  exports.AIGNEContext = AIGNEContext;
135
- class AIGNEContextInternal {
145
+ class AIGNEContextShared {
136
146
  parent;
137
- constructor(parent) {
147
+ constructor(parent, overrides) {
138
148
  this.parent = parent;
139
149
  this.messageQueue = this.parent?.messageQueue ?? new message_queue_js_1.MessageQueue();
150
+ this.userContext = overrides?.userContext ?? {};
140
151
  }
141
152
  messageQueue;
142
153
  events = new node_events_1.default();
@@ -150,6 +161,7 @@ class AIGNEContextInternal {
150
161
  return this.parent?.limits;
151
162
  }
152
163
  usage = (0, usage_js_1.newEmptyContextUsage)();
164
+ userContext;
153
165
  abortController = new AbortController();
154
166
  timer;
155
167
  initTimeout() {
@@ -176,12 +188,13 @@ class AIGNEContextInternal {
176
188
  const result = {};
177
189
  if (options?.sourceAgent && activeAgent !== options.sourceAgent) {
178
190
  options.sourceAgent.hooks.onHandoff?.({
191
+ context,
179
192
  source: options.sourceAgent,
180
193
  target: activeAgent,
181
194
  input,
182
195
  });
183
196
  }
184
- const stream = await activeAgent.invoke(input, context, { streaming: true });
197
+ const stream = await activeAgent.invoke(input, { ...options, context, streaming: true });
185
198
  for await (const value of stream) {
186
199
  if (value.delta.text) {
187
200
  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/cjs/index.js CHANGED
@@ -16,11 +16,12 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./agents/agent.js"), exports);
18
18
  __exportStar(require("./agents/ai-agent.js"), exports);
19
+ __exportStar(require("./agents/chat-model.js"), exports);
20
+ __exportStar(require("./agents/guide-rail-agent.js"), exports);
19
21
  __exportStar(require("./agents/mcp-agent.js"), exports);
20
22
  __exportStar(require("./agents/team-agent.js"), exports);
21
23
  __exportStar(require("./agents/types.js"), exports);
22
24
  __exportStar(require("./agents/user-agent.js"), exports);
23
25
  __exportStar(require("./aigne/index.js"), exports);
24
- __exportStar(require("./agents/chat-model.js"), exports);
25
26
  __exportStar(require("./prompt/prompt-builder.js"), exports);
26
27
  __exportStar(require("./prompt/template.js"), exports);
@@ -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;