@aigne/core 1.16.0 → 1.18.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.
- package/CHANGELOG.md +37 -16
- package/lib/cjs/agents/agent.d.ts +69 -37
- package/lib/cjs/agents/agent.js +93 -65
- package/lib/cjs/agents/ai-agent.d.ts +4 -7
- package/lib/cjs/agents/ai-agent.js +10 -19
- package/lib/cjs/agents/chat-model.d.ts +7 -8
- package/lib/cjs/agents/chat-model.js +9 -9
- package/lib/cjs/agents/mcp-agent.d.ts +4 -5
- package/lib/cjs/agents/mcp-agent.js +7 -13
- package/lib/cjs/agents/team-agent.d.ts +7 -8
- package/lib/cjs/agents/team-agent.js +10 -10
- package/lib/cjs/agents/user-agent.d.ts +4 -5
- package/lib/cjs/agents/user-agent.js +11 -12
- package/lib/cjs/aigne/aigne.d.ts +10 -9
- package/lib/cjs/aigne/aigne.js +5 -4
- package/lib/cjs/aigne/context.d.ts +18 -9
- package/lib/cjs/aigne/context.js +23 -12
- package/lib/cjs/aigne/message-queue.d.ts +6 -2
- package/lib/cjs/aigne/message-queue.js +2 -2
- package/lib/cjs/index.d.ts +3 -1
- package/lib/cjs/index.js +3 -1
- package/lib/cjs/loader/agent-js.d.ts +1 -1
- package/lib/cjs/loader/agent-yaml.d.ts +3 -2
- package/lib/cjs/loader/agent-yaml.js +3 -2
- package/lib/cjs/loader/index.d.ts +5 -1
- package/lib/cjs/loader/index.js +25 -16
- package/lib/cjs/memory/memory.d.ts +3 -2
- package/lib/cjs/memory/memory.js +1 -1
- package/lib/cjs/memory/retriever.d.ts +2 -2
- package/lib/cjs/prompt/prompt-builder.d.ts +9 -7
- package/lib/cjs/prompt/prompt-builder.js +14 -10
- package/lib/cjs/prompt/template.js +1 -3
- package/lib/cjs/utils/logger.js +3 -3
- package/lib/cjs/utils/nodejs.d.ts +8 -0
- package/lib/cjs/utils/nodejs.js +24 -0
- package/lib/cjs/utils/type-utils.d.ts +2 -2
- package/lib/dts/agents/agent.d.ts +69 -37
- package/lib/dts/agents/ai-agent.d.ts +4 -7
- package/lib/dts/agents/chat-model.d.ts +7 -8
- package/lib/dts/agents/mcp-agent.d.ts +4 -5
- package/lib/dts/agents/team-agent.d.ts +7 -8
- package/lib/dts/agents/user-agent.d.ts +4 -5
- package/lib/dts/aigne/aigne.d.ts +10 -9
- package/lib/dts/aigne/context.d.ts +18 -9
- package/lib/dts/aigne/message-queue.d.ts +6 -2
- package/lib/dts/index.d.ts +3 -1
- package/lib/dts/loader/agent-js.d.ts +1 -1
- package/lib/dts/loader/agent-yaml.d.ts +3 -2
- package/lib/dts/loader/index.d.ts +5 -1
- package/lib/dts/memory/memory.d.ts +3 -2
- package/lib/dts/memory/retriever.d.ts +2 -2
- package/lib/dts/prompt/prompt-builder.d.ts +9 -7
- package/lib/dts/utils/nodejs.d.ts +8 -0
- package/lib/dts/utils/type-utils.d.ts +2 -2
- package/lib/esm/agents/agent.d.ts +69 -37
- package/lib/esm/agents/agent.js +94 -66
- package/lib/esm/agents/ai-agent.d.ts +4 -7
- package/lib/esm/agents/ai-agent.js +10 -19
- package/lib/esm/agents/chat-model.d.ts +7 -8
- package/lib/esm/agents/chat-model.js +9 -9
- package/lib/esm/agents/mcp-agent.d.ts +4 -5
- package/lib/esm/agents/mcp-agent.js +7 -13
- package/lib/esm/agents/team-agent.d.ts +7 -8
- package/lib/esm/agents/team-agent.js +10 -10
- package/lib/esm/agents/user-agent.d.ts +4 -5
- package/lib/esm/agents/user-agent.js +10 -11
- package/lib/esm/aigne/aigne.d.ts +10 -9
- package/lib/esm/aigne/aigne.js +5 -4
- package/lib/esm/aigne/context.d.ts +18 -9
- package/lib/esm/aigne/context.js +24 -10
- package/lib/esm/aigne/message-queue.d.ts +6 -2
- package/lib/esm/aigne/message-queue.js +2 -2
- package/lib/esm/index.d.ts +3 -1
- package/lib/esm/index.js +3 -1
- package/lib/esm/loader/agent-js.d.ts +1 -1
- package/lib/esm/loader/agent-yaml.d.ts +3 -2
- package/lib/esm/loader/agent-yaml.js +3 -2
- package/lib/esm/loader/index.d.ts +5 -1
- package/lib/esm/loader/index.js +25 -16
- package/lib/esm/memory/memory.d.ts +3 -2
- package/lib/esm/memory/memory.js +2 -2
- package/lib/esm/memory/retriever.d.ts +2 -2
- package/lib/esm/prompt/prompt-builder.d.ts +9 -7
- package/lib/esm/prompt/prompt-builder.js +15 -11
- package/lib/esm/prompt/template.js +1 -3
- package/lib/esm/utils/logger.js +3 -3
- package/lib/esm/utils/nodejs.d.ts +8 -0
- package/lib/esm/utils/nodejs.js +21 -0
- package/lib/esm/utils/type-utils.d.ts +2 -2
- package/package.json +4 -3
- package/lib/cjs/memory/default-memory.d.ts +0 -16
- package/lib/cjs/memory/default-memory.js +0 -70
- package/lib/cjs/utils/fs.d.ts +0 -2
- package/lib/cjs/utils/fs.js +0 -25
- package/lib/dts/memory/default-memory.d.ts +0 -16
- package/lib/dts/utils/fs.d.ts +0 -2
- package/lib/esm/memory/default-memory.d.ts +0 -16
- package/lib/esm/memory/default-memory.js +0 -63
- package/lib/esm/utils/fs.d.ts +0 -2
- package/lib/esm/utils/fs.js +0 -21
|
@@ -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
|
|
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,
|
|
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
|
|
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,
|
|
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
|
|
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,
|
|
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
|
|
89
|
+
* @param options The invocation options
|
|
90
90
|
* @returns A stream of message chunks that collectively form the response
|
|
91
91
|
*/
|
|
92
|
-
process(input,
|
|
92
|
+
process(input, options) {
|
|
93
93
|
switch (this.mode) {
|
|
94
94
|
case ProcessMode.sequential:
|
|
95
|
-
return this._processSequential(input,
|
|
95
|
+
return this._processSequential(input, options);
|
|
96
96
|
case ProcessMode.parallel:
|
|
97
|
-
return this._processParallel(input,
|
|
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
|
|
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,
|
|
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
|
|
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,
|
|
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,6 @@
|
|
|
1
|
-
import { ReadableStream } from "node:stream/web";
|
|
2
1
|
import type { Context } from "../aigne/context.js";
|
|
3
2
|
import { type MessagePayload } from "../aigne/message-queue.js";
|
|
4
|
-
import { Agent, type AgentOptions, type AgentProcessResult, type FunctionAgentFn, type Message } from "./agent.js";
|
|
3
|
+
import { Agent, type AgentInvokeOptions, type AgentOptions, type AgentProcessResult, type FunctionAgentFn, type Message } from "./agent.js";
|
|
5
4
|
export interface UserAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
|
|
6
5
|
context: Context;
|
|
7
6
|
process?: FunctionAgentFn<I, O>;
|
|
@@ -14,14 +13,14 @@ export declare class UserAgent<I extends Message = Message, O extends Message =
|
|
|
14
13
|
private _process?;
|
|
15
14
|
private activeAgent?;
|
|
16
15
|
protected subscribeToTopics(context: Pick<Context, "subscribe">): void;
|
|
17
|
-
protected publishToTopics(output: O,
|
|
16
|
+
protected publishToTopics(output: O, options: AgentInvokeOptions): Promise<void>;
|
|
18
17
|
invoke: Agent<I, O>["invoke"];
|
|
19
|
-
process(input: I,
|
|
18
|
+
process(input: I, options: AgentInvokeOptions): Promise<AgentProcessResult<O>>;
|
|
20
19
|
publish: Context["publish"];
|
|
21
20
|
subscribe: Context["subscribe"];
|
|
22
21
|
unsubscribe: Context["unsubscribe"];
|
|
23
22
|
get stream(): ReadableStream<MessagePayload & {
|
|
24
23
|
topic: string;
|
|
25
24
|
}>;
|
|
26
|
-
protected checkAgentInvokesUsage(
|
|
25
|
+
protected checkAgentInvokesUsage(_options: AgentInvokeOptions): void;
|
|
27
26
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.UserAgent = void 0;
|
|
4
|
-
const web_1 = require("node:stream/web");
|
|
5
4
|
const message_queue_js_1 = require("../aigne/message-queue.js");
|
|
6
5
|
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
7
6
|
const agent_js_1 = require("./agent.js");
|
|
@@ -22,21 +21,21 @@ class UserAgent extends agent_js_1.Agent {
|
|
|
22
21
|
if (this._process)
|
|
23
22
|
super.subscribeToTopics(context);
|
|
24
23
|
}
|
|
25
|
-
async publishToTopics(output,
|
|
24
|
+
async publishToTopics(output, options) {
|
|
26
25
|
if (this._process)
|
|
27
|
-
super.publishToTopics(output,
|
|
26
|
+
super.publishToTopics(output, options);
|
|
28
27
|
}
|
|
29
|
-
invoke = ((input,
|
|
30
|
-
if (!context)
|
|
28
|
+
invoke = ((input, options = {}) => {
|
|
29
|
+
if (!options.context)
|
|
31
30
|
this.context = this.context.newContext({ reset: true });
|
|
32
|
-
return super.invoke(input, context
|
|
31
|
+
return super.invoke(input, { ...options, context: this.context });
|
|
33
32
|
});
|
|
34
|
-
async process(input,
|
|
33
|
+
async process(input, options) {
|
|
35
34
|
if (this._process) {
|
|
36
|
-
return this._process(input,
|
|
35
|
+
return this._process(input, options);
|
|
37
36
|
}
|
|
38
37
|
if (this.activeAgent) {
|
|
39
|
-
const [output, agent] = await context.invoke(this.activeAgent, input, {
|
|
38
|
+
const [output, agent] = await options.context.invoke(this.activeAgent, input, {
|
|
40
39
|
returnActiveAgent: true,
|
|
41
40
|
streaming: true,
|
|
42
41
|
});
|
|
@@ -47,7 +46,7 @@ class UserAgent extends agent_js_1.Agent {
|
|
|
47
46
|
}
|
|
48
47
|
const publicTopic = typeof this.publishTopic === "function" ? await this.publishTopic(input) : this.publishTopic;
|
|
49
48
|
if (publicTopic?.length) {
|
|
50
|
-
context.publish(publicTopic, input);
|
|
49
|
+
options.context.publish(publicTopic, input);
|
|
51
50
|
if (this.subscribeTopic) {
|
|
52
51
|
return this.subscribe(this.subscribeTopic).then((res) => res.message);
|
|
53
52
|
}
|
|
@@ -66,7 +65,7 @@ class UserAgent extends agent_js_1.Agent {
|
|
|
66
65
|
});
|
|
67
66
|
get stream() {
|
|
68
67
|
let subscriptions = [];
|
|
69
|
-
return new
|
|
68
|
+
return new ReadableStream({
|
|
70
69
|
start: (controller) => {
|
|
71
70
|
const subscribeTopic = (0, type_utils_js_1.orArrayToArray)(this.subscribeTopic);
|
|
72
71
|
subscriptions = subscribeTopic.map((topic) => this.subscribe(topic, (message) => {
|
|
@@ -80,7 +79,7 @@ class UserAgent extends agent_js_1.Agent {
|
|
|
80
79
|
},
|
|
81
80
|
});
|
|
82
81
|
}
|
|
83
|
-
checkAgentInvokesUsage(
|
|
82
|
+
checkAgentInvokesUsage(_options) {
|
|
84
83
|
// ignore calls usage check for UserAgent
|
|
85
84
|
}
|
|
86
85
|
}
|
package/lib/cjs/aigne/aigne.d.ts
CHANGED
|
@@ -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.
|
package/lib/cjs/aigne/aigne.js
CHANGED
|
@@ -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);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Emitter } from "strict-event-emitter";
|
|
2
2
|
import { Agent, type AgentInvokeOptions, type AgentProcessAsyncGenerator, type AgentResponse, type AgentResponseStream, type Message } from "../agents/agent.js";
|
|
3
3
|
import type { ChatModel } from "../agents/chat-model.js";
|
|
4
4
|
import { UserAgent } from "../agents/user-agent.js";
|
|
@@ -38,7 +38,7 @@ 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;
|
|
@@ -46,12 +46,18 @@ export interface InvokeOptions extends AgentInvokeOptions {
|
|
|
46
46
|
/**
|
|
47
47
|
* @hidden
|
|
48
48
|
*/
|
|
49
|
-
export interface
|
|
49
|
+
export interface UserContext extends Record<string, unknown> {
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* @hidden
|
|
53
|
+
*/
|
|
54
|
+
export interface Context<U extends UserContext = UserContext> extends TypedEventEmitter<ContextEventMap, ContextEmitEventMap> {
|
|
50
55
|
model?: ChatModel;
|
|
51
56
|
skills?: Agent[];
|
|
52
57
|
usage: ContextUsage;
|
|
53
58
|
limits?: ContextLimits;
|
|
54
59
|
status?: "normal" | "timeout";
|
|
60
|
+
userContext: U;
|
|
55
61
|
/**
|
|
56
62
|
* Create a user agent to consistently invoke an agent
|
|
57
63
|
* @param agent Agent to invoke
|
|
@@ -92,7 +98,7 @@ export interface Context extends TypedEventEmitter<ContextEventMap, ContextEmitE
|
|
|
92
98
|
* @param topic topic name, or an array of topic names
|
|
93
99
|
* @param payload message to publish
|
|
94
100
|
*/
|
|
95
|
-
publish(topic: string | string[], payload: Omit<MessagePayload, "context"> | Message | string): void;
|
|
101
|
+
publish(topic: string | string[], payload: Omit<MessagePayload, "context"> | Message | string, options?: InvokeOptions): void;
|
|
96
102
|
subscribe(topic: string | string[], listener?: undefined): Promise<MessagePayload>;
|
|
97
103
|
subscribe(topic: string | string[], listener: MessageQueueListener): Unsubscribe;
|
|
98
104
|
subscribe(topic: string | string[], listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
|
|
@@ -114,15 +120,17 @@ export interface Context extends TypedEventEmitter<ContextEventMap, ContextEmitE
|
|
|
114
120
|
* @hidden
|
|
115
121
|
*/
|
|
116
122
|
export declare class AIGNEContext implements Context {
|
|
117
|
-
constructor(parent
|
|
123
|
+
constructor(...[parent, ...args]: ConstructorParameters<typeof AIGNEContextShared>);
|
|
118
124
|
parentId?: string;
|
|
119
125
|
id: string;
|
|
120
|
-
readonly internal:
|
|
126
|
+
readonly internal: AIGNEContextShared;
|
|
121
127
|
get model(): ChatModel | undefined;
|
|
122
128
|
get skills(): Agent<Message, Message>[] | undefined;
|
|
123
129
|
get limits(): ContextLimits | undefined;
|
|
124
130
|
get status(): "normal" | "timeout";
|
|
125
131
|
get usage(): ContextUsage;
|
|
132
|
+
get userContext(): Context["userContext"];
|
|
133
|
+
set userContext(userContext: Context["userContext"]);
|
|
126
134
|
newContext({ reset }?: {
|
|
127
135
|
reset?: boolean;
|
|
128
136
|
}): AIGNEContext;
|
|
@@ -135,17 +143,18 @@ export declare class AIGNEContext implements Context {
|
|
|
135
143
|
once<K extends keyof ContextEventMap>(eventName: K, listener: Listener<K, ContextEventMap>): this;
|
|
136
144
|
off<K extends keyof ContextEventMap>(eventName: K, listener: Listener<K, ContextEventMap>): this;
|
|
137
145
|
}
|
|
138
|
-
declare class
|
|
146
|
+
declare class AIGNEContextShared {
|
|
139
147
|
private readonly parent?;
|
|
140
148
|
constructor(parent?: (Pick<Context, "model" | "skills" | "limits"> & {
|
|
141
149
|
messageQueue?: MessageQueue;
|
|
142
|
-
}) | undefined);
|
|
150
|
+
}) | undefined, overrides?: Partial<Context>);
|
|
143
151
|
readonly messageQueue: MessageQueue;
|
|
144
|
-
readonly events:
|
|
152
|
+
readonly events: Emitter<any>;
|
|
145
153
|
get model(): ChatModel | undefined;
|
|
146
154
|
get skills(): Agent<Message, Message>[] | undefined;
|
|
147
155
|
get limits(): ContextLimits | undefined;
|
|
148
156
|
usage: ContextUsage;
|
|
157
|
+
userContext: Context["userContext"];
|
|
149
158
|
private abortController;
|
|
150
159
|
private timer?;
|
|
151
160
|
private initTimeout;
|
package/lib/cjs/aigne/context.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.AIGNEContext = void 0;
|
|
7
|
-
const
|
|
4
|
+
const strict_event_emitter_1 = require("strict-event-emitter");
|
|
8
5
|
const uuid_1 = require("uuid");
|
|
9
6
|
const zod_1 = require("zod");
|
|
10
7
|
const agent_js_1 = require("../agents/agent.js");
|
|
@@ -19,13 +16,13 @@ const usage_js_1 = require("./usage.js");
|
|
|
19
16
|
* @hidden
|
|
20
17
|
*/
|
|
21
18
|
class AIGNEContext {
|
|
22
|
-
constructor(parent) {
|
|
19
|
+
constructor(...[parent, ...args]) {
|
|
23
20
|
if (parent instanceof AIGNEContext) {
|
|
24
21
|
this.parentId = parent.id;
|
|
25
22
|
this.internal = parent.internal;
|
|
26
23
|
}
|
|
27
24
|
else {
|
|
28
|
-
this.internal = new
|
|
25
|
+
this.internal = new AIGNEContextShared(parent, ...args);
|
|
29
26
|
}
|
|
30
27
|
}
|
|
31
28
|
parentId;
|
|
@@ -46,9 +43,15 @@ class AIGNEContext {
|
|
|
46
43
|
get usage() {
|
|
47
44
|
return this.internal.usage;
|
|
48
45
|
}
|
|
46
|
+
get userContext() {
|
|
47
|
+
return this.internal.userContext;
|
|
48
|
+
}
|
|
49
|
+
set userContext(userContext) {
|
|
50
|
+
this.internal.userContext = userContext;
|
|
51
|
+
}
|
|
49
52
|
newContext({ reset } = {}) {
|
|
50
53
|
if (reset)
|
|
51
|
-
return new AIGNEContext(this
|
|
54
|
+
return new AIGNEContext(this, { userContext: {} });
|
|
52
55
|
return new AIGNEContext(this);
|
|
53
56
|
}
|
|
54
57
|
invoke = ((agent, message, options) => {
|
|
@@ -57,6 +60,8 @@ class AIGNEContext {
|
|
|
57
60
|
message,
|
|
58
61
|
options,
|
|
59
62
|
});
|
|
63
|
+
if (options?.userContext)
|
|
64
|
+
Object.assign(this.userContext, options.userContext);
|
|
60
65
|
if ((0, type_utils_js_1.isNil)(message)) {
|
|
61
66
|
return user_agent_js_1.UserAgent.from({
|
|
62
67
|
context: this,
|
|
@@ -96,7 +101,9 @@ class AIGNEContext {
|
|
|
96
101
|
return stream;
|
|
97
102
|
});
|
|
98
103
|
});
|
|
99
|
-
publish = ((topic, payload) => {
|
|
104
|
+
publish = ((topic, payload, options) => {
|
|
105
|
+
if (options?.userContext)
|
|
106
|
+
Object.assign(this.userContext, options.userContext);
|
|
100
107
|
return this.internal.messageQueue.publish(topic, {
|
|
101
108
|
...(0, message_queue_js_1.toMessagePayload)(payload),
|
|
102
109
|
context: this,
|
|
@@ -132,14 +139,16 @@ class AIGNEContext {
|
|
|
132
139
|
}
|
|
133
140
|
}
|
|
134
141
|
exports.AIGNEContext = AIGNEContext;
|
|
135
|
-
class
|
|
142
|
+
class AIGNEContextShared {
|
|
136
143
|
parent;
|
|
137
|
-
constructor(parent) {
|
|
144
|
+
constructor(parent, overrides) {
|
|
138
145
|
this.parent = parent;
|
|
139
146
|
this.messageQueue = this.parent?.messageQueue ?? new message_queue_js_1.MessageQueue();
|
|
147
|
+
this.userContext = overrides?.userContext ?? {};
|
|
140
148
|
}
|
|
141
149
|
messageQueue;
|
|
142
|
-
|
|
150
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
151
|
+
events = new strict_event_emitter_1.Emitter();
|
|
143
152
|
get model() {
|
|
144
153
|
return this.parent?.model;
|
|
145
154
|
}
|
|
@@ -150,6 +159,7 @@ class AIGNEContextInternal {
|
|
|
150
159
|
return this.parent?.limits;
|
|
151
160
|
}
|
|
152
161
|
usage = (0, usage_js_1.newEmptyContextUsage)();
|
|
162
|
+
userContext;
|
|
153
163
|
abortController = new AbortController();
|
|
154
164
|
timer;
|
|
155
165
|
initTimeout() {
|
|
@@ -176,12 +186,13 @@ class AIGNEContextInternal {
|
|
|
176
186
|
const result = {};
|
|
177
187
|
if (options?.sourceAgent && activeAgent !== options.sourceAgent) {
|
|
178
188
|
options.sourceAgent.hooks.onHandoff?.({
|
|
189
|
+
context,
|
|
179
190
|
source: options.sourceAgent,
|
|
180
191
|
target: activeAgent,
|
|
181
192
|
input,
|
|
182
193
|
});
|
|
183
194
|
}
|
|
184
|
-
const stream = await activeAgent.invoke(input, context,
|
|
195
|
+
const stream = await activeAgent.invoke(input, { ...options, context, streaming: true });
|
|
185
196
|
for await (const value of stream) {
|
|
186
197
|
if (value.delta.text) {
|
|
187
198
|
yield { delta: { text: value.delta.text } };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Emitter, type EventMap } from "strict-event-emitter";
|
|
2
2
|
import type { Message } from "../agents/agent.js";
|
|
3
3
|
import type { Context } from "./context.js";
|
|
4
4
|
/**
|
|
@@ -30,11 +30,14 @@ export type MessageQueueListener = (message: MessagePayload) => void;
|
|
|
30
30
|
* @hidden
|
|
31
31
|
*/
|
|
32
32
|
export type Unsubscribe = () => void;
|
|
33
|
+
interface MessageQueueEventMap extends EventMap {
|
|
34
|
+
[key: string]: any[];
|
|
35
|
+
}
|
|
33
36
|
/**
|
|
34
37
|
* @hidden
|
|
35
38
|
*/
|
|
36
39
|
export declare class MessageQueue {
|
|
37
|
-
events:
|
|
40
|
+
events: Emitter<MessageQueueEventMap>;
|
|
38
41
|
publish(topic: string | string[], payload: MessagePayload): void;
|
|
39
42
|
error(error: Error): void;
|
|
40
43
|
subscribe(topic: string | string[], listener?: undefined): Promise<MessagePayload>;
|
|
@@ -42,3 +45,4 @@ export declare class MessageQueue {
|
|
|
42
45
|
subscribe(topic: string | string[], listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
|
|
43
46
|
unsubscribe(topic: string | string[], listener: MessageQueueListener): void;
|
|
44
47
|
}
|
|
48
|
+
export {};
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MessageQueue = exports.UserOutputTopic = exports.UserInputTopic = void 0;
|
|
4
4
|
exports.toMessagePayload = toMessagePayload;
|
|
5
|
-
const
|
|
5
|
+
const strict_event_emitter_1 = require("strict-event-emitter");
|
|
6
6
|
const zod_1 = require("zod");
|
|
7
7
|
const prompt_builder_js_1 = require("../prompt/prompt-builder.js");
|
|
8
8
|
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
@@ -40,7 +40,7 @@ function toMessagePayload(payload, options) {
|
|
|
40
40
|
* @hidden
|
|
41
41
|
*/
|
|
42
42
|
class MessageQueue {
|
|
43
|
-
events = new
|
|
43
|
+
events = new strict_event_emitter_1.Emitter();
|
|
44
44
|
publish(topic, payload) {
|
|
45
45
|
(0, type_utils_js_1.checkArguments)("MessageQueue.publish", publishArgsSchema, {
|
|
46
46
|
topic,
|
package/lib/cjs/index.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
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 "./
|
|
10
|
+
export * from "./memory/index.js";
|
|
9
11
|
export * from "./prompt/prompt-builder.js";
|
|
10
12
|
export * from "./prompt/template.js";
|
package/lib/cjs/index.js
CHANGED
|
@@ -16,11 +16,13 @@ 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("./
|
|
26
|
+
__exportStar(require("./memory/index.js"), exports);
|
|
25
27
|
__exportStar(require("./prompt/prompt-builder.js"), exports);
|
|
26
28
|
__exportStar(require("./prompt/template.js"), exports);
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { type ZodObject, type ZodType, z } from "zod";
|
|
2
2
|
import type { Message } from "../agents/agent.js";
|
|
3
3
|
export declare function loadAgentFromJsFile(path: string): Promise<{
|
|
4
|
-
name: string;
|
|
5
4
|
process: (args_0: Message) => Message;
|
|
5
|
+
name: string;
|
|
6
6
|
description?: string | undefined;
|
|
7
7
|
inputSchema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
|
|
8
8
|
[x: string]: any;
|
|
@@ -5,10 +5,11 @@ export declare function loadAgentFromYamlFile(path: string): Promise<{
|
|
|
5
5
|
name: string;
|
|
6
6
|
description?: string | undefined;
|
|
7
7
|
skills?: string[] | undefined;
|
|
8
|
+
instructions?: string | undefined;
|
|
8
9
|
memory?: true | {
|
|
10
|
+
provider: string;
|
|
9
11
|
subscribeTopic?: string[] | undefined;
|
|
10
12
|
} | undefined;
|
|
11
|
-
instructions?: string | undefined;
|
|
12
13
|
inputSchema?: ZodObject<Record<string, ZodType<any, z.ZodTypeDef, any>>, z.UnknownKeysParam, z.ZodTypeAny, {
|
|
13
14
|
[x: string]: any;
|
|
14
15
|
}, {
|
|
@@ -24,6 +25,6 @@ export declare function loadAgentFromYamlFile(path: string): Promise<{
|
|
|
24
25
|
} | {
|
|
25
26
|
type: "mcp";
|
|
26
27
|
url?: string | undefined;
|
|
27
|
-
command?: string | undefined;
|
|
28
28
|
args?: string[] | undefined;
|
|
29
|
+
command?: string | undefined;
|
|
29
30
|
}>;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.loadAgentFromYamlFile = loadAgentFromYamlFile;
|
|
4
|
-
const promises_1 = require("node:fs/promises");
|
|
5
4
|
const json_schema_to_zod_1 = require("@aigne/json-schema-to-zod");
|
|
6
5
|
const yaml_1 = require("yaml");
|
|
7
6
|
const zod_1 = require("zod");
|
|
8
7
|
const ai_agent_js_1 = require("../agents/ai-agent.js");
|
|
9
8
|
const camelize_js_1 = require("../utils/camelize.js");
|
|
9
|
+
const nodejs_js_1 = require("../utils/nodejs.js");
|
|
10
10
|
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
11
11
|
const schema_js_1 = require("./schema.js");
|
|
12
12
|
const agentFileSchema = zod_1.z.discriminatedUnion("type", [
|
|
@@ -43,6 +43,7 @@ const agentFileSchema = zod_1.z.discriminatedUnion("type", [
|
|
|
43
43
|
.union([
|
|
44
44
|
zod_1.z.boolean(),
|
|
45
45
|
zod_1.z.object({
|
|
46
|
+
provider: zod_1.z.string(),
|
|
46
47
|
subscribe_topic: zod_1.z
|
|
47
48
|
.array(zod_1.z.string())
|
|
48
49
|
.nullish()
|
|
@@ -69,7 +70,7 @@ const agentFileSchema = zod_1.z.discriminatedUnion("type", [
|
|
|
69
70
|
}),
|
|
70
71
|
]);
|
|
71
72
|
async function loadAgentFromYamlFile(path) {
|
|
72
|
-
const raw = await (0, type_utils_js_1.tryOrThrow)(() =>
|
|
73
|
+
const raw = await (0, type_utils_js_1.tryOrThrow)(() => nodejs_js_1.nodejs.fs.readFile(path, "utf8"), (error) => new Error(`Failed to load agent definition from ${path}: ${error.message}`));
|
|
73
74
|
const json = await (0, type_utils_js_1.tryOrThrow)(() => (0, yaml_1.parse)(raw), (error) => new Error(`Failed to parse agent definition from ${path}: ${error.message}`));
|
|
74
75
|
const agent = (0, type_utils_js_1.tryOrThrow)(() => (0, camelize_js_1.customCamelize)(agentFileSchema.parse({
|
|
75
76
|
...json,
|