@aigne/core 1.11.0 → 1.12.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 +15 -0
- package/README.md +11 -10
- package/README.zh.md +7 -6
- package/lib/cjs/agents/agent.d.ts +20 -20
- package/lib/cjs/agents/agent.js +25 -26
- package/lib/cjs/agents/ai-agent.d.ts +17 -18
- package/lib/cjs/agents/ai-agent.js +6 -6
- package/lib/cjs/agents/mcp-agent.d.ts +10 -4
- package/lib/cjs/agents/mcp-agent.js +12 -6
- package/lib/cjs/agents/memory.d.ts +1 -1
- package/lib/cjs/agents/team-agent.d.ts +28 -0
- package/lib/cjs/agents/team-agent.js +93 -0
- package/lib/cjs/agents/user-agent.d.ts +9 -10
- package/lib/cjs/agents/user-agent.js +10 -13
- package/lib/{esm/execution-engine/execution-engine.d.ts → cjs/aigne/aigne.d.ts} +9 -12
- package/lib/cjs/{execution-engine/execution-engine.js → aigne/aigne.js} +19 -19
- package/lib/cjs/{execution-engine → aigne}/context.d.ts +31 -32
- package/lib/cjs/{execution-engine → aigne}/context.js +30 -40
- package/lib/cjs/aigne/index.d.ts +4 -0
- package/lib/cjs/{execution-engine → aigne}/index.js +2 -2
- package/lib/cjs/{execution-engine → aigne}/usage.d.ts +1 -1
- package/lib/cjs/client/client.d.ts +19 -0
- package/lib/cjs/client/client.js +49 -0
- package/lib/cjs/index.d.ts +2 -1
- package/lib/cjs/index.js +2 -1
- package/lib/cjs/loader/agent-yaml.d.ts +3 -3
- package/lib/cjs/loader/agent-yaml.js +10 -3
- package/lib/cjs/loader/index.d.ts +9 -9
- package/lib/cjs/loader/index.js +6 -6
- package/lib/cjs/models/chat-model.d.ts +1 -1
- package/lib/cjs/models/claude-chat-model.d.ts +3 -3
- package/lib/cjs/models/openai-chat-model.d.ts +3 -3
- package/lib/cjs/prompt/prompt-builder.d.ts +1 -1
- package/lib/cjs/prompt/prompt-builder.js +3 -3
- package/lib/cjs/server/error.d.ts +4 -0
- package/lib/cjs/server/error.js +11 -0
- package/lib/cjs/server/server.d.ts +54 -0
- package/lib/cjs/server/server.js +130 -0
- package/lib/cjs/utils/event-stream.d.ts +11 -0
- package/lib/cjs/utils/event-stream.js +91 -0
- package/lib/cjs/utils/mcp-utils.js +4 -1
- package/lib/cjs/utils/stream-utils.d.ts +7 -2
- package/lib/cjs/utils/stream-utils.js +43 -34
- package/lib/cjs/utils/type-utils.d.ts +4 -2
- package/lib/cjs/utils/type-utils.js +10 -2
- package/lib/dts/agents/agent.d.ts +20 -20
- package/lib/dts/agents/ai-agent.d.ts +17 -18
- package/lib/dts/agents/mcp-agent.d.ts +10 -4
- package/lib/dts/agents/memory.d.ts +1 -1
- package/lib/dts/agents/team-agent.d.ts +28 -0
- package/lib/dts/agents/user-agent.d.ts +9 -10
- package/lib/dts/{execution-engine/execution-engine.d.ts → aigne/aigne.d.ts} +9 -12
- package/lib/dts/{execution-engine → aigne}/context.d.ts +31 -32
- package/lib/dts/aigne/index.d.ts +4 -0
- package/lib/dts/{execution-engine → aigne}/usage.d.ts +1 -1
- package/lib/dts/client/client.d.ts +19 -0
- package/lib/dts/index.d.ts +2 -1
- package/lib/dts/loader/agent-yaml.d.ts +3 -3
- package/lib/dts/loader/index.d.ts +9 -9
- package/lib/dts/models/chat-model.d.ts +1 -1
- package/lib/dts/models/claude-chat-model.d.ts +3 -3
- package/lib/dts/models/openai-chat-model.d.ts +3 -3
- package/lib/dts/prompt/prompt-builder.d.ts +1 -1
- package/lib/dts/server/error.d.ts +4 -0
- package/lib/dts/server/server.d.ts +54 -0
- package/lib/dts/utils/event-stream.d.ts +11 -0
- package/lib/dts/utils/stream-utils.d.ts +7 -2
- package/lib/dts/utils/type-utils.d.ts +4 -2
- package/lib/esm/agents/agent.d.ts +20 -20
- package/lib/esm/agents/agent.js +25 -26
- package/lib/esm/agents/ai-agent.d.ts +17 -18
- package/lib/esm/agents/ai-agent.js +6 -6
- package/lib/esm/agents/mcp-agent.d.ts +10 -4
- package/lib/esm/agents/mcp-agent.js +12 -6
- package/lib/esm/agents/memory.d.ts +1 -1
- package/lib/esm/agents/team-agent.d.ts +28 -0
- package/lib/esm/agents/team-agent.js +89 -0
- package/lib/esm/agents/user-agent.d.ts +9 -10
- package/lib/esm/agents/user-agent.js +11 -14
- package/lib/{cjs/execution-engine/execution-engine.d.ts → esm/aigne/aigne.d.ts} +9 -12
- package/lib/esm/{execution-engine/execution-engine.js → aigne/aigne.js} +18 -18
- package/lib/esm/{execution-engine → aigne}/context.d.ts +31 -32
- package/lib/esm/{execution-engine → aigne}/context.js +28 -38
- package/lib/esm/aigne/index.d.ts +4 -0
- package/lib/esm/aigne/index.js +4 -0
- package/lib/esm/{execution-engine → aigne}/usage.d.ts +1 -1
- package/lib/esm/client/client.d.ts +19 -0
- package/lib/esm/client/client.js +45 -0
- package/lib/esm/index.d.ts +2 -1
- package/lib/esm/index.js +2 -1
- package/lib/esm/loader/agent-yaml.d.ts +3 -3
- package/lib/esm/loader/agent-yaml.js +10 -3
- package/lib/esm/loader/index.d.ts +9 -9
- package/lib/esm/loader/index.js +6 -6
- package/lib/esm/models/chat-model.d.ts +1 -1
- package/lib/esm/models/claude-chat-model.d.ts +3 -3
- package/lib/esm/models/openai-chat-model.d.ts +3 -3
- package/lib/esm/prompt/prompt-builder.d.ts +1 -1
- package/lib/esm/prompt/prompt-builder.js +3 -3
- package/lib/esm/server/error.d.ts +4 -0
- package/lib/esm/server/error.js +7 -0
- package/lib/esm/server/server.d.ts +54 -0
- package/lib/esm/server/server.js +123 -0
- package/lib/esm/utils/event-stream.d.ts +11 -0
- package/lib/esm/utils/event-stream.js +85 -0
- package/lib/esm/utils/mcp-utils.js +4 -1
- package/lib/esm/utils/stream-utils.d.ts +7 -2
- package/lib/esm/utils/stream-utils.js +42 -33
- package/lib/esm/utils/type-utils.d.ts +4 -2
- package/lib/esm/utils/type-utils.js +9 -2
- package/package.json +13 -4
- package/lib/cjs/execution-engine/index.d.ts +0 -4
- package/lib/cjs/execution-engine/utils.d.ts +0 -4
- package/lib/cjs/execution-engine/utils.js +0 -34
- package/lib/dts/execution-engine/index.d.ts +0 -4
- package/lib/dts/execution-engine/utils.d.ts +0 -4
- package/lib/esm/execution-engine/index.d.ts +0 -4
- package/lib/esm/execution-engine/index.js +0 -4
- package/lib/esm/execution-engine/utils.d.ts +0 -4
- package/lib/esm/execution-engine/utils.js +0 -30
- /package/lib/cjs/{execution-engine → aigne}/message-queue.d.ts +0 -0
- /package/lib/cjs/{execution-engine → aigne}/message-queue.js +0 -0
- /package/lib/cjs/{execution-engine → aigne}/usage.js +0 -0
- /package/lib/dts/{execution-engine → aigne}/message-queue.d.ts +0 -0
- /package/lib/esm/{execution-engine → aigne}/message-queue.d.ts +0 -0
- /package/lib/esm/{execution-engine → aigne}/message-queue.js +0 -0
- /package/lib/esm/{execution-engine → aigne}/usage.js +0 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TeamAgent = exports.ProcessMode = void 0;
|
|
4
|
+
const stream_utils_js_1 = require("../utils/stream-utils.js");
|
|
5
|
+
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
6
|
+
const agent_js_1 = require("./agent.js");
|
|
7
|
+
var ProcessMode;
|
|
8
|
+
(function (ProcessMode) {
|
|
9
|
+
/**
|
|
10
|
+
* Process the agents one by one, passing the output of each agent to the next.
|
|
11
|
+
*/
|
|
12
|
+
ProcessMode["sequential"] = "sequential";
|
|
13
|
+
/**
|
|
14
|
+
* Process all agents in parallel, merging the output of all agents.
|
|
15
|
+
*/
|
|
16
|
+
ProcessMode["parallel"] = "parallel";
|
|
17
|
+
})(ProcessMode || (exports.ProcessMode = ProcessMode = {}));
|
|
18
|
+
class TeamAgent extends agent_js_1.Agent {
|
|
19
|
+
static from(options) {
|
|
20
|
+
return new TeamAgent(options);
|
|
21
|
+
}
|
|
22
|
+
constructor(options) {
|
|
23
|
+
super(options);
|
|
24
|
+
this.mode = options.mode ?? ProcessMode.sequential;
|
|
25
|
+
}
|
|
26
|
+
mode;
|
|
27
|
+
process(input, context) {
|
|
28
|
+
switch (this.mode) {
|
|
29
|
+
case ProcessMode.sequential:
|
|
30
|
+
return this._processSequential(input, context);
|
|
31
|
+
case ProcessMode.parallel:
|
|
32
|
+
return this._processParallel(input, context);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async *_processSequential(input, context) {
|
|
36
|
+
const output = {};
|
|
37
|
+
// Clone the agents to run, so that we can update the agents list during the loop
|
|
38
|
+
const agents = [...this.skills];
|
|
39
|
+
const newAgents = [];
|
|
40
|
+
for (const agent of agents) {
|
|
41
|
+
const [o, transferToAgent] = await context.invoke(agent, { ...input, ...output }, { returnActiveAgent: true, streaming: true });
|
|
42
|
+
for await (const chunk of (0, stream_utils_js_1.readableStreamToAsyncIterator)(o)) {
|
|
43
|
+
yield chunk;
|
|
44
|
+
(0, stream_utils_js_1.mergeAgentResponseChunk)(output, chunk);
|
|
45
|
+
}
|
|
46
|
+
newAgents.push(await transferToAgent);
|
|
47
|
+
}
|
|
48
|
+
this.skills.splice(0);
|
|
49
|
+
this.skills.push(...newAgents);
|
|
50
|
+
}
|
|
51
|
+
async *_processParallel(input, context) {
|
|
52
|
+
const result = await Promise.all(this.skills.map((agent) => context.invoke(agent, input, { returnActiveAgent: true, streaming: true })));
|
|
53
|
+
const streams = result.map((i) => i[0]);
|
|
54
|
+
const read = async (index, reader) => {
|
|
55
|
+
const promise = reader.read();
|
|
56
|
+
return promise.then((result) => ({ ...result, reader, index }));
|
|
57
|
+
};
|
|
58
|
+
const tasks = new Map(streams.map((stream, index) => [index, read(index, stream.getReader())]));
|
|
59
|
+
// NOTE: Flag to check if the output key is used by agent at the index,
|
|
60
|
+
const outputKeyUsed = new Map();
|
|
61
|
+
while (tasks.size) {
|
|
62
|
+
const { value, done, reader, index } = await Promise.race(tasks.values());
|
|
63
|
+
tasks.delete(index);
|
|
64
|
+
if (!done) {
|
|
65
|
+
tasks.set(index, read(index, reader));
|
|
66
|
+
}
|
|
67
|
+
if (value) {
|
|
68
|
+
let { delta: { text, ...delta }, } = value;
|
|
69
|
+
if (text) {
|
|
70
|
+
for (const key of Object.keys(text)) {
|
|
71
|
+
// the output key is unused, add to map to lock it
|
|
72
|
+
if (!outputKeyUsed.has(key)) {
|
|
73
|
+
outputKeyUsed.set(key, index);
|
|
74
|
+
}
|
|
75
|
+
// the output key is used by the agent at the index, abandon it
|
|
76
|
+
else if (outputKeyUsed.get(key) !== index) {
|
|
77
|
+
delete text[key];
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if ((0, type_utils_js_1.isEmpty)(text)) {
|
|
81
|
+
text = undefined;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (!(0, type_utils_js_1.isEmpty)(delta.json) || !(0, type_utils_js_1.isEmpty)(text))
|
|
85
|
+
yield { delta: { ...delta, text } };
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const agents = await Promise.all(result.map((i) => i[1]));
|
|
89
|
+
this.skills.splice(0);
|
|
90
|
+
this.skills.push(...agents);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
exports.TeamAgent = TeamAgent;
|
|
@@ -1,26 +1,25 @@
|
|
|
1
1
|
import { ReadableStream } from "node:stream/web";
|
|
2
|
-
import { type Context
|
|
3
|
-
import type { MessagePayload } from "../
|
|
4
|
-
import { type
|
|
5
|
-
import { Agent, type AgentOptions, type AgentProcessAsyncGenerator, type Message } from "./agent.js";
|
|
2
|
+
import { type Context } from "../aigne/context.js";
|
|
3
|
+
import type { MessagePayload } from "../aigne/message-queue.js";
|
|
4
|
+
import { type Agent, type AgentOptions, type AgentProcessResult, FunctionAgent, type FunctionAgentFn, type Message } from "./agent.js";
|
|
6
5
|
export interface UserAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
|
|
7
6
|
context: Context;
|
|
8
|
-
process?:
|
|
9
|
-
activeAgent?:
|
|
7
|
+
process?: FunctionAgentFn<I, O>;
|
|
8
|
+
activeAgent?: Agent;
|
|
10
9
|
}
|
|
11
|
-
export declare class UserAgent<I extends Message = Message, O extends Message = Message> extends
|
|
10
|
+
export declare class UserAgent<I extends Message = Message, O extends Message = Message> extends FunctionAgent<I, O> {
|
|
12
11
|
static from<I extends Message, O extends Message>(options: UserAgentOptions<I, O>): UserAgent<I, O>;
|
|
13
12
|
constructor(options: UserAgentOptions<I, O>);
|
|
14
13
|
context: Context;
|
|
15
14
|
private _process?;
|
|
16
15
|
private activeAgent?;
|
|
17
|
-
|
|
18
|
-
process(input: I, context: Context):
|
|
16
|
+
invoke: Agent<I, O>["invoke"];
|
|
17
|
+
process(input: I, context: Context): Promise<AgentProcessResult<O>>;
|
|
19
18
|
publish: Context["publish"];
|
|
20
19
|
subscribe: Context["subscribe"];
|
|
21
20
|
unsubscribe: Context["unsubscribe"];
|
|
22
21
|
get stream(): ReadableStream<MessagePayload & {
|
|
23
22
|
topic: string;
|
|
24
23
|
}>;
|
|
25
|
-
protected
|
|
24
|
+
protected checkAgentInvokesUsage(_context: Context): void;
|
|
26
25
|
}
|
|
@@ -2,11 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.UserAgent = void 0;
|
|
4
4
|
const web_1 = require("node:stream/web");
|
|
5
|
-
const context_js_1 = require("../
|
|
6
|
-
const stream_utils_js_1 = require("../utils/stream-utils.js");
|
|
5
|
+
const context_js_1 = require("../aigne/context.js");
|
|
7
6
|
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
8
7
|
const agent_js_1 = require("./agent.js");
|
|
9
|
-
class UserAgent extends agent_js_1.
|
|
8
|
+
class UserAgent extends agent_js_1.FunctionAgent {
|
|
10
9
|
static from(options) {
|
|
11
10
|
return new UserAgent(options);
|
|
12
11
|
}
|
|
@@ -19,31 +18,29 @@ class UserAgent extends agent_js_1.Agent {
|
|
|
19
18
|
context;
|
|
20
19
|
_process;
|
|
21
20
|
activeAgent;
|
|
22
|
-
|
|
21
|
+
invoke = ((input, context, options) => {
|
|
23
22
|
if (!context)
|
|
24
23
|
this.context = this.context.newContext({ reset: true });
|
|
25
|
-
return super.
|
|
24
|
+
return super.invoke(input, context ?? this.context, options);
|
|
26
25
|
});
|
|
27
|
-
async
|
|
26
|
+
async process(input, context) {
|
|
28
27
|
if (this._process) {
|
|
29
|
-
|
|
30
|
-
return;
|
|
28
|
+
return this._process(input, context);
|
|
31
29
|
}
|
|
32
30
|
if (this.activeAgent) {
|
|
33
|
-
const [output, agent] = await context.
|
|
31
|
+
const [output, agent] = await context.invoke(this.activeAgent, input, {
|
|
34
32
|
returnActiveAgent: true,
|
|
35
33
|
streaming: true,
|
|
36
34
|
});
|
|
37
35
|
agent.then((agent) => {
|
|
38
36
|
this.activeAgent = agent;
|
|
39
37
|
});
|
|
40
|
-
|
|
41
|
-
return;
|
|
38
|
+
return output;
|
|
42
39
|
}
|
|
43
40
|
const publicTopic = typeof this.publishTopic === "function" ? await this.publishTopic(input) : this.publishTopic;
|
|
44
41
|
if (publicTopic?.length) {
|
|
45
42
|
context.publish(publicTopic, (0, context_js_1.createPublishMessage)(input, this));
|
|
46
|
-
return;
|
|
43
|
+
return {};
|
|
47
44
|
}
|
|
48
45
|
throw new Error("UserAgent must have a process function or a publishTopic");
|
|
49
46
|
}
|
|
@@ -72,7 +69,7 @@ class UserAgent extends agent_js_1.Agent {
|
|
|
72
69
|
},
|
|
73
70
|
});
|
|
74
71
|
}
|
|
75
|
-
|
|
72
|
+
checkAgentInvokesUsage(_context) {
|
|
76
73
|
// ignore calls usage check for UserAgent
|
|
77
74
|
}
|
|
78
75
|
}
|
|
@@ -1,29 +1,26 @@
|
|
|
1
1
|
import { Agent } from "../agents/agent.js";
|
|
2
2
|
import { ChatModel } from "../models/chat-model.js";
|
|
3
|
-
import { type Context
|
|
3
|
+
import { AIGNEContext, type Context } from "./context.js";
|
|
4
4
|
import { MessageQueue } from "./message-queue.js";
|
|
5
5
|
import type { ContextLimits } from "./usage.js";
|
|
6
|
-
export interface
|
|
6
|
+
export interface AIGNEOptions {
|
|
7
7
|
name?: string;
|
|
8
8
|
description?: string;
|
|
9
9
|
model?: ChatModel;
|
|
10
|
-
|
|
10
|
+
skills?: Agent[];
|
|
11
11
|
agents?: Agent[];
|
|
12
12
|
limits?: ContextLimits;
|
|
13
13
|
}
|
|
14
|
-
export
|
|
15
|
-
returnActiveAgent?: boolean;
|
|
16
|
-
}
|
|
17
|
-
export declare class ExecutionEngine {
|
|
14
|
+
export declare class AIGNE {
|
|
18
15
|
static load({ path, ...options }: {
|
|
19
16
|
path: string;
|
|
20
|
-
} &
|
|
21
|
-
constructor(options?:
|
|
17
|
+
} & AIGNEOptions): Promise<AIGNE>;
|
|
18
|
+
constructor(options?: AIGNEOptions);
|
|
22
19
|
name?: string;
|
|
23
20
|
description?: string;
|
|
24
21
|
readonly messageQueue: MessageQueue;
|
|
25
22
|
model?: ChatModel;
|
|
26
|
-
readonly
|
|
23
|
+
readonly skills: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>[] & {
|
|
27
24
|
[key: string]: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>;
|
|
28
25
|
};
|
|
29
26
|
readonly agents: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>[] & {
|
|
@@ -31,9 +28,9 @@ export declare class ExecutionEngine {
|
|
|
31
28
|
};
|
|
32
29
|
limits?: ContextLimits;
|
|
33
30
|
addAgent(...agents: Agent[]): void;
|
|
34
|
-
newContext():
|
|
31
|
+
newContext(): AIGNEContext;
|
|
35
32
|
publish: Context["publish"];
|
|
36
|
-
|
|
33
|
+
invoke: Context["invoke"];
|
|
37
34
|
subscribe: Context["subscribe"];
|
|
38
35
|
unsubscribe: Context["unsubscribe"];
|
|
39
36
|
shutdown(): Promise<void>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.AIGNE = void 0;
|
|
4
4
|
const zod_1 = require("zod");
|
|
5
5
|
const agent_js_1 = require("../agents/agent.js");
|
|
6
6
|
const index_js_1 = require("../loader/index.js");
|
|
@@ -8,27 +8,27 @@ const chat_model_js_1 = require("../models/chat-model.js");
|
|
|
8
8
|
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
9
9
|
const context_js_1 = require("./context.js");
|
|
10
10
|
const message_queue_js_1 = require("./message-queue.js");
|
|
11
|
-
class
|
|
11
|
+
class AIGNE {
|
|
12
12
|
static async load({ path, ...options }) {
|
|
13
|
-
const { model, agents,
|
|
14
|
-
return new
|
|
13
|
+
const { model, agents, skills, ...aigne } = await (0, index_js_1.load)({ path });
|
|
14
|
+
return new AIGNE({
|
|
15
15
|
...options,
|
|
16
16
|
model: options.model || model,
|
|
17
17
|
name: options.name || aigne.name || undefined,
|
|
18
18
|
description: options.description || aigne.description || undefined,
|
|
19
19
|
agents: agents.concat(options.agents ?? []),
|
|
20
|
-
|
|
20
|
+
skills: skills.concat(options.skills ?? []),
|
|
21
21
|
});
|
|
22
22
|
}
|
|
23
23
|
constructor(options) {
|
|
24
24
|
if (options)
|
|
25
|
-
(0, type_utils_js_1.checkArguments)("
|
|
25
|
+
(0, type_utils_js_1.checkArguments)("AIGNE", aigneOptionsSchema, options);
|
|
26
26
|
this.name = options?.name;
|
|
27
27
|
this.description = options?.description;
|
|
28
28
|
this.model = options?.model;
|
|
29
29
|
this.limits = options?.limits;
|
|
30
|
-
if (options?.
|
|
31
|
-
this.
|
|
30
|
+
if (options?.skills?.length)
|
|
31
|
+
this.skills.push(...options.skills);
|
|
32
32
|
if (options?.agents?.length)
|
|
33
33
|
this.addAgent(...options.agents);
|
|
34
34
|
this.initProcessExitHandler();
|
|
@@ -37,24 +37,24 @@ class ExecutionEngine {
|
|
|
37
37
|
description;
|
|
38
38
|
messageQueue = new message_queue_js_1.MessageQueue();
|
|
39
39
|
model;
|
|
40
|
-
|
|
40
|
+
skills = (0, type_utils_js_1.createAccessorArray)([], (arr, name) => arr.find((i) => i.name === name));
|
|
41
41
|
agents = (0, type_utils_js_1.createAccessorArray)([], (arr, name) => arr.find((i) => i.name === name));
|
|
42
42
|
limits;
|
|
43
43
|
addAgent(...agents) {
|
|
44
|
-
(0, type_utils_js_1.checkArguments)("
|
|
44
|
+
(0, type_utils_js_1.checkArguments)("AIGNE.addAgent", aigneAddAgentArgsSchema, agents);
|
|
45
45
|
for (const agent of agents) {
|
|
46
46
|
this.agents.push(agent);
|
|
47
47
|
agent.attach(this);
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
50
|
newContext() {
|
|
51
|
-
return new context_js_1.
|
|
51
|
+
return new context_js_1.AIGNEContext(this);
|
|
52
52
|
}
|
|
53
53
|
publish = ((...args) => {
|
|
54
|
-
return new context_js_1.
|
|
54
|
+
return new context_js_1.AIGNEContext(this).publish(...args);
|
|
55
55
|
});
|
|
56
|
-
|
|
57
|
-
return new context_js_1.
|
|
56
|
+
invoke = ((...args) => {
|
|
57
|
+
return new context_js_1.AIGNEContext(this).invoke(...args);
|
|
58
58
|
});
|
|
59
59
|
subscribe = ((...args) => {
|
|
60
60
|
return this.messageQueue.subscribe(...args);
|
|
@@ -63,7 +63,7 @@ class ExecutionEngine {
|
|
|
63
63
|
this.messageQueue.unsubscribe(...args);
|
|
64
64
|
});
|
|
65
65
|
async shutdown() {
|
|
66
|
-
for (const tool of this.
|
|
66
|
+
for (const tool of this.skills) {
|
|
67
67
|
await tool.shutdown();
|
|
68
68
|
}
|
|
69
69
|
for (const agent of this.agents) {
|
|
@@ -76,10 +76,10 @@ class ExecutionEngine {
|
|
|
76
76
|
process.on("exit", shutdownAndExit);
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
|
-
exports.
|
|
80
|
-
const
|
|
79
|
+
exports.AIGNE = AIGNE;
|
|
80
|
+
const aigneOptionsSchema = zod_1.z.object({
|
|
81
81
|
model: zod_1.z.instanceof(chat_model_js_1.ChatModel).optional(),
|
|
82
|
-
|
|
82
|
+
skills: zod_1.z.array(zod_1.z.instanceof(agent_js_1.Agent)).optional(),
|
|
83
83
|
agents: zod_1.z.array(zod_1.z.instanceof(agent_js_1.Agent)).optional(),
|
|
84
84
|
});
|
|
85
|
-
const
|
|
85
|
+
const aigneAddAgentArgsSchema = zod_1.z.array(zod_1.z.instanceof(agent_js_1.Agent));
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import EventEmitter from "node:events";
|
|
2
|
-
import { Agent, type
|
|
2
|
+
import { Agent, type AgentInvokeOptions, type AgentProcessAsyncGenerator, type AgentResponse, type AgentResponseStream, type Message } from "../agents/agent.js";
|
|
3
3
|
import { UserAgent } from "../agents/user-agent.js";
|
|
4
4
|
import type { ChatModel } from "../models/chat-model.js";
|
|
5
5
|
import { type OmitPropertiesFromArrayFirstElement } from "../utils/type-utils.js";
|
|
6
6
|
import type { Args, Listener, TypedEventEmitter } from "../utils/typed-event-emtter.js";
|
|
7
7
|
import { type MessagePayload, MessageQueue, type MessageQueueListener, type Unsubscribe } from "./message-queue.js";
|
|
8
8
|
import { type ContextLimits, type ContextUsage } from "./usage.js";
|
|
9
|
-
export type Runnable<I extends Message = Message, O extends Message = Message> = Agent<I, O> | FunctionAgentFn;
|
|
10
9
|
export interface AgentEvent {
|
|
11
10
|
parentContextId?: string;
|
|
12
11
|
contextId: string;
|
|
@@ -27,53 +26,53 @@ export interface ContextEventMap {
|
|
|
27
26
|
export type ContextEmitEventMap = {
|
|
28
27
|
[K in keyof ContextEventMap]: OmitPropertiesFromArrayFirstElement<ContextEventMap[K], "contextId" | "parentContextId" | "timestamp">;
|
|
29
28
|
};
|
|
30
|
-
export interface
|
|
29
|
+
export interface InvokeOptions extends AgentInvokeOptions {
|
|
31
30
|
returnActiveAgent?: boolean;
|
|
32
31
|
disableTransfer?: boolean;
|
|
33
32
|
}
|
|
34
33
|
export interface Context extends TypedEventEmitter<ContextEventMap, ContextEmitEventMap> {
|
|
35
34
|
model?: ChatModel;
|
|
36
|
-
|
|
35
|
+
skills?: Agent[];
|
|
37
36
|
usage: ContextUsage;
|
|
38
37
|
limits?: ContextLimits;
|
|
39
38
|
status?: "normal" | "timeout";
|
|
40
39
|
/**
|
|
41
|
-
* Create a user agent to consistently
|
|
42
|
-
* @param agent Agent to
|
|
40
|
+
* Create a user agent to consistently invoke an agent
|
|
41
|
+
* @param agent Agent to invoke
|
|
43
42
|
* @returns User agent
|
|
44
43
|
*/
|
|
45
|
-
|
|
44
|
+
invoke<I extends Message, O extends Message>(agent: Agent<I, O>): UserAgent<I, O>;
|
|
46
45
|
/**
|
|
47
|
-
*
|
|
48
|
-
* @param agent Agent to
|
|
46
|
+
* Invoke an agent with a message and return the output and the active agent
|
|
47
|
+
* @param agent Agent to invoke
|
|
49
48
|
* @param message Message to pass to the agent
|
|
50
49
|
* @param options.returnActiveAgent return the active agent
|
|
51
50
|
* @param options.streaming return a stream of the output
|
|
52
51
|
* @returns the output of the agent and the final active agent
|
|
53
52
|
*/
|
|
54
|
-
|
|
53
|
+
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options: InvokeOptions & {
|
|
55
54
|
returnActiveAgent: true;
|
|
56
55
|
streaming?: false;
|
|
57
|
-
}): Promise<[O,
|
|
58
|
-
|
|
56
|
+
}): Promise<[O, Agent]>;
|
|
57
|
+
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options: InvokeOptions & {
|
|
59
58
|
returnActiveAgent: true;
|
|
60
59
|
streaming: true;
|
|
61
|
-
}): Promise<[AgentResponseStream<O>, Promise<
|
|
60
|
+
}): Promise<[AgentResponseStream<O>, Promise<Agent>]>;
|
|
62
61
|
/**
|
|
63
|
-
*
|
|
64
|
-
* @param agent Agent to
|
|
62
|
+
* Invoke an agent with a message
|
|
63
|
+
* @param agent Agent to invoke
|
|
65
64
|
* @param message Message to pass to the agent
|
|
66
65
|
* @returns the output of the agent
|
|
67
66
|
*/
|
|
68
|
-
|
|
67
|
+
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options?: InvokeOptions & {
|
|
69
68
|
streaming?: false;
|
|
70
69
|
}): Promise<O>;
|
|
71
|
-
|
|
70
|
+
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, message: I | string, options: InvokeOptions & {
|
|
72
71
|
streaming: true;
|
|
73
72
|
}): Promise<AgentResponseStream<O>>;
|
|
74
|
-
|
|
73
|
+
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]>;
|
|
75
74
|
/**
|
|
76
|
-
* Publish a message to a topic, the
|
|
75
|
+
* Publish a message to a topic, the aigne will invoke the listeners of the topic
|
|
77
76
|
* @param topic topic name, or an array of topic names
|
|
78
77
|
* @param payload message to publish
|
|
79
78
|
*/
|
|
@@ -96,21 +95,21 @@ export interface Context extends TypedEventEmitter<ContextEventMap, ContextEmitE
|
|
|
96
95
|
}): Context;
|
|
97
96
|
}
|
|
98
97
|
export declare function createPublishMessage(message: string | Message, from?: Agent): Omit<MessagePayload, "context">;
|
|
99
|
-
export declare class
|
|
100
|
-
constructor(parent?: ConstructorParameters<typeof
|
|
98
|
+
export declare class AIGNEContext implements Context {
|
|
99
|
+
constructor(parent?: ConstructorParameters<typeof AIGNEContextInternal>[0]);
|
|
101
100
|
parentId?: string;
|
|
102
101
|
id: string;
|
|
103
|
-
readonly internal:
|
|
102
|
+
readonly internal: AIGNEContextInternal;
|
|
104
103
|
get model(): ChatModel | undefined;
|
|
105
|
-
get
|
|
104
|
+
get skills(): Agent<Message, Message>[] | undefined;
|
|
106
105
|
get limits(): ContextLimits | undefined;
|
|
107
106
|
get status(): "normal" | "timeout";
|
|
108
107
|
get usage(): ContextUsage;
|
|
109
108
|
newContext({ reset }?: {
|
|
110
109
|
reset?: boolean;
|
|
111
|
-
}):
|
|
112
|
-
|
|
113
|
-
private
|
|
110
|
+
}): AIGNEContext;
|
|
111
|
+
invoke: Context["invoke"];
|
|
112
|
+
private onInvokeSuccess;
|
|
114
113
|
publish: Context["publish"];
|
|
115
114
|
subscribe: Context["subscribe"];
|
|
116
115
|
unsubscribe: Context["unsubscribe"];
|
|
@@ -119,24 +118,24 @@ export declare class ExecutionContext implements Context {
|
|
|
119
118
|
once<K extends keyof ContextEventMap>(eventName: K, listener: Listener<K, ContextEventMap>): this;
|
|
120
119
|
off<K extends keyof ContextEventMap>(eventName: K, listener: Listener<K, ContextEventMap>): this;
|
|
121
120
|
}
|
|
122
|
-
declare class
|
|
121
|
+
declare class AIGNEContextInternal {
|
|
123
122
|
private readonly parent?;
|
|
124
|
-
constructor(parent?: (Pick<Context, "model" | "
|
|
123
|
+
constructor(parent?: (Pick<Context, "model" | "skills" | "limits"> & {
|
|
125
124
|
messageQueue?: MessageQueue;
|
|
126
125
|
}) | undefined);
|
|
127
126
|
readonly messageQueue: MessageQueue;
|
|
128
127
|
readonly events: EventEmitter<ContextEventMap>;
|
|
129
128
|
get model(): ChatModel | undefined;
|
|
130
|
-
get
|
|
129
|
+
get skills(): Agent<Message, Message>[] | undefined;
|
|
131
130
|
get limits(): ContextLimits | undefined;
|
|
132
131
|
usage: ContextUsage;
|
|
133
132
|
private abortController;
|
|
134
133
|
private timer?;
|
|
135
134
|
private initTimeout;
|
|
136
135
|
get status(): "normal" | "timeout";
|
|
137
|
-
|
|
138
|
-
__activeAgent__:
|
|
136
|
+
invoke<I extends Message, O extends Message>(agent: Agent<I, O>, input: I, context: Context, options?: InvokeOptions): AgentProcessAsyncGenerator<O & {
|
|
137
|
+
__activeAgent__: Agent;
|
|
139
138
|
}>;
|
|
140
|
-
private
|
|
139
|
+
private invokeAgent;
|
|
141
140
|
}
|
|
142
141
|
export {};
|
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.AIGNEContext = void 0;
|
|
7
7
|
exports.createPublishMessage = createPublishMessage;
|
|
8
8
|
const node_events_1 = __importDefault(require("node:events"));
|
|
9
9
|
const uuid_1 = require("uuid");
|
|
@@ -23,14 +23,14 @@ function createPublishMessage(message, from) {
|
|
|
23
23
|
message: (0, prompt_builder_js_1.createMessage)(message),
|
|
24
24
|
};
|
|
25
25
|
}
|
|
26
|
-
class
|
|
26
|
+
class AIGNEContext {
|
|
27
27
|
constructor(parent) {
|
|
28
|
-
if (parent instanceof
|
|
28
|
+
if (parent instanceof AIGNEContext) {
|
|
29
29
|
this.parentId = parent.id;
|
|
30
30
|
this.internal = parent.internal;
|
|
31
31
|
}
|
|
32
32
|
else {
|
|
33
|
-
this.internal = new
|
|
33
|
+
this.internal = new AIGNEContextInternal(parent);
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
parentId;
|
|
@@ -39,8 +39,8 @@ class ExecutionContext {
|
|
|
39
39
|
get model() {
|
|
40
40
|
return this.internal.model;
|
|
41
41
|
}
|
|
42
|
-
get
|
|
43
|
-
return this.internal.
|
|
42
|
+
get skills() {
|
|
43
|
+
return this.internal.skills;
|
|
44
44
|
}
|
|
45
45
|
get limits() {
|
|
46
46
|
return this.internal.limits;
|
|
@@ -53,11 +53,11 @@ class ExecutionContext {
|
|
|
53
53
|
}
|
|
54
54
|
newContext({ reset } = {}) {
|
|
55
55
|
if (reset)
|
|
56
|
-
return new
|
|
57
|
-
return new
|
|
56
|
+
return new AIGNEContext(this.internal);
|
|
57
|
+
return new AIGNEContext(this);
|
|
58
58
|
}
|
|
59
|
-
|
|
60
|
-
(0, type_utils_js_1.checkArguments)("
|
|
59
|
+
invoke = ((agent, message, options) => {
|
|
60
|
+
(0, type_utils_js_1.checkArguments)("AIGNEContext.invoke", aigneContextInvokeArgsSchema, {
|
|
61
61
|
agent,
|
|
62
62
|
message,
|
|
63
63
|
options,
|
|
@@ -70,10 +70,10 @@ class ExecutionContext {
|
|
|
70
70
|
}
|
|
71
71
|
const newContext = this.newContext();
|
|
72
72
|
const msg = (0, prompt_builder_js_1.createMessage)(message);
|
|
73
|
-
return Promise.resolve(newContext.internal.
|
|
73
|
+
return Promise.resolve(newContext.internal.invoke(agent, msg, newContext, options)).then(async (response) => {
|
|
74
74
|
if (!options?.streaming) {
|
|
75
75
|
const { __activeAgent__: activeAgent, ...output } = await (0, stream_utils_js_1.agentResponseStreamToObject)(response);
|
|
76
|
-
this.
|
|
76
|
+
this.onInvokeSuccess(activeAgent, output, newContext);
|
|
77
77
|
if (options?.returnActiveAgent) {
|
|
78
78
|
return [output, activeAgent];
|
|
79
79
|
}
|
|
@@ -81,7 +81,7 @@ class ExecutionContext {
|
|
|
81
81
|
}
|
|
82
82
|
const activeAgentPromise = Promise.withResolvers();
|
|
83
83
|
const stream = (0, stream_utils_js_1.onAgentResponseStreamEnd)((0, stream_utils_js_1.asyncGeneratorToReadableStream)(response), async ({ __activeAgent__: activeAgent, ...output }) => {
|
|
84
|
-
this.
|
|
84
|
+
this.onInvokeSuccess(activeAgent, output, newContext);
|
|
85
85
|
activeAgentPromise.resolve(activeAgent);
|
|
86
86
|
}, {
|
|
87
87
|
processChunk(chunk) {
|
|
@@ -103,7 +103,7 @@ class ExecutionContext {
|
|
|
103
103
|
return stream;
|
|
104
104
|
});
|
|
105
105
|
});
|
|
106
|
-
async
|
|
106
|
+
async onInvokeSuccess(activeAgent, output, context) {
|
|
107
107
|
if (activeAgent instanceof agent_js_1.Agent) {
|
|
108
108
|
const publishTopics = typeof activeAgent.publishTopic === "function"
|
|
109
109
|
? await activeAgent.publishTopic(output)
|
|
@@ -145,8 +145,8 @@ class ExecutionContext {
|
|
|
145
145
|
return this;
|
|
146
146
|
}
|
|
147
147
|
}
|
|
148
|
-
exports.
|
|
149
|
-
class
|
|
148
|
+
exports.AIGNEContext = AIGNEContext;
|
|
149
|
+
class AIGNEContextInternal {
|
|
150
150
|
parent;
|
|
151
151
|
constructor(parent) {
|
|
152
152
|
this.parent = parent;
|
|
@@ -157,8 +157,8 @@ class ExecutionContextInternal {
|
|
|
157
157
|
get model() {
|
|
158
158
|
return this.parent?.model;
|
|
159
159
|
}
|
|
160
|
-
get
|
|
161
|
-
return this.parent?.
|
|
160
|
+
get skills() {
|
|
161
|
+
return this.parent?.skills;
|
|
162
162
|
}
|
|
163
163
|
get limits() {
|
|
164
164
|
return this.parent?.limits;
|
|
@@ -179,33 +179,23 @@ class ExecutionContextInternal {
|
|
|
179
179
|
get status() {
|
|
180
180
|
return this.abortController.signal.aborted ? "timeout" : "normal";
|
|
181
181
|
}
|
|
182
|
-
|
|
182
|
+
invoke(agent, input, context, options) {
|
|
183
183
|
this.initTimeout();
|
|
184
|
-
return withAbortSignal(this.abortController.signal, new Error("
|
|
184
|
+
return withAbortSignal(this.abortController.signal, new Error("AIGNEContext is timeout"), () => this.invokeAgent(agent, input, context, options));
|
|
185
185
|
}
|
|
186
|
-
async *
|
|
186
|
+
async *invokeAgent(agent, input, context, options) {
|
|
187
187
|
let activeAgent = agent;
|
|
188
188
|
let output;
|
|
189
189
|
for (;;) {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
if (value.delta.text) {
|
|
199
|
-
yield { delta: { text: value.delta.text } };
|
|
200
|
-
}
|
|
201
|
-
if (value.delta.json) {
|
|
202
|
-
Object.assign(result, value.delta.json);
|
|
203
|
-
}
|
|
190
|
+
const result = {};
|
|
191
|
+
const stream = await activeAgent.invoke(input, context, { streaming: true });
|
|
192
|
+
for await (const value of (0, stream_utils_js_1.readableStreamToAsyncIterator)(stream)) {
|
|
193
|
+
if (value.delta.text) {
|
|
194
|
+
yield { delta: { text: value.delta.text } };
|
|
195
|
+
}
|
|
196
|
+
if (value.delta.json) {
|
|
197
|
+
Object.assign(result, value.delta.json);
|
|
204
198
|
}
|
|
205
|
-
}
|
|
206
|
-
if (result instanceof agent_js_1.Agent) {
|
|
207
|
-
activeAgent = result;
|
|
208
|
-
continue;
|
|
209
199
|
}
|
|
210
200
|
if (!options?.disableTransfer) {
|
|
211
201
|
const transferToAgent = (0, types_js_1.isTransferAgentOutput)(result)
|
|
@@ -250,7 +240,7 @@ async function* withAbortSignal(signal, error, fn) {
|
|
|
250
240
|
signal.removeEventListener("abort", listener);
|
|
251
241
|
}
|
|
252
242
|
}
|
|
253
|
-
const
|
|
243
|
+
const aigneContextInvokeArgsSchema = zod_1.z.object({
|
|
254
244
|
agent: zod_1.z.union([zod_1.z.function(), zod_1.z.instanceof(agent_js_1.Agent)]),
|
|
255
245
|
message: zod_1.z.union([zod_1.z.record(zod_1.z.unknown()), zod_1.z.string()]).optional(),
|
|
256
246
|
options: zod_1.z.object({ returnActiveAgent: zod_1.z.boolean().optional() }).optional(),
|
|
@@ -15,6 +15,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./context.js"), exports);
|
|
18
|
-
__exportStar(require("./
|
|
18
|
+
__exportStar(require("./aigne.js"), exports);
|
|
19
19
|
__exportStar(require("./message-queue.js"), exports);
|
|
20
|
-
__exportStar(require("./
|
|
20
|
+
__exportStar(require("./usage.js"), exports);
|