@aigne/core 1.1.0-beta.5 → 1.3.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 +81 -0
- package/lib/cjs/agents/agent.d.ts +37 -33
- package/lib/cjs/agents/agent.js +68 -28
- package/lib/cjs/agents/ai-agent.d.ts +8 -12
- package/lib/cjs/agents/ai-agent.js +17 -35
- package/lib/cjs/agents/mcp-agent.d.ts +25 -9
- package/lib/cjs/agents/mcp-agent.js +61 -14
- package/lib/cjs/agents/memory.d.ts +26 -0
- package/lib/cjs/agents/memory.js +38 -0
- package/lib/cjs/agents/types.d.ts +4 -3
- package/lib/cjs/agents/types.js +11 -1
- package/lib/cjs/agents/user-agent.d.ts +24 -0
- package/lib/cjs/agents/user-agent.js +62 -0
- package/lib/cjs/execution-engine/context.d.ts +35 -5
- package/lib/cjs/execution-engine/execution-engine.d.ts +64 -0
- package/lib/cjs/execution-engine/execution-engine.js +136 -0
- package/lib/cjs/execution-engine/index.d.ts +4 -46
- package/lib/cjs/execution-engine/index.js +17 -193
- package/lib/cjs/execution-engine/message-queue.d.ts +17 -2
- package/lib/cjs/execution-engine/message-queue.js +37 -1
- package/lib/cjs/execution-engine/utils.d.ts +4 -0
- package/lib/cjs/execution-engine/utils.js +32 -0
- package/lib/cjs/index.d.ts +14 -11
- package/lib/cjs/index.js +14 -11
- package/lib/{dts/models/chat.d.ts → cjs/models/chat-model.d.ts} +15 -12
- package/lib/cjs/models/{chat.js → chat-model.js} +5 -5
- package/lib/cjs/models/claude-chat-model.d.ts +17 -0
- package/lib/cjs/models/claude-chat-model.js +199 -0
- package/lib/cjs/models/{chat-openai.d.ts → openai-chat-model.d.ts} +2 -2
- package/lib/cjs/models/{chat-openai.js → openai-chat-model.js} +8 -8
- package/lib/cjs/prompt/prompt-builder.d.ts +14 -14
- package/lib/cjs/prompt/prompt-builder.js +62 -67
- package/lib/cjs/prompt/template.d.ts +9 -21
- package/lib/cjs/prompt/template.js +3 -5
- package/lib/cjs/utils/json-schema.d.ts +3 -0
- package/lib/cjs/utils/json-schema.js +23 -0
- package/lib/cjs/utils/mcp-utils.d.ts +3 -2
- package/lib/cjs/utils/mcp-utils.js +29 -17
- package/lib/cjs/utils/run-chat-loop.d.ts +5 -4
- package/lib/cjs/utils/run-chat-loop.js +25 -4
- package/lib/cjs/utils/type-utils.d.ts +3 -0
- package/lib/cjs/utils/type-utils.js +8 -8
- package/lib/dts/agents/agent.d.ts +37 -33
- package/lib/dts/agents/ai-agent.d.ts +8 -12
- package/lib/dts/agents/mcp-agent.d.ts +25 -9
- package/lib/dts/agents/memory.d.ts +26 -0
- package/lib/dts/agents/types.d.ts +4 -3
- package/lib/dts/agents/user-agent.d.ts +24 -0
- package/lib/dts/execution-engine/context.d.ts +35 -5
- package/lib/dts/execution-engine/execution-engine.d.ts +64 -0
- package/lib/dts/execution-engine/index.d.ts +4 -46
- package/lib/dts/execution-engine/message-queue.d.ts +17 -2
- package/lib/dts/execution-engine/utils.d.ts +4 -0
- package/lib/dts/index.d.ts +14 -11
- package/lib/{esm/models/chat.d.ts → dts/models/chat-model.d.ts} +15 -12
- package/lib/dts/models/claude-chat-model.d.ts +17 -0
- package/lib/dts/models/{chat-openai.d.ts → openai-chat-model.d.ts} +2 -2
- package/lib/dts/prompt/prompt-builder.d.ts +14 -14
- package/lib/dts/prompt/template.d.ts +9 -21
- package/lib/dts/utils/json-schema.d.ts +3 -0
- package/lib/dts/utils/mcp-utils.d.ts +3 -2
- package/lib/dts/utils/run-chat-loop.d.ts +5 -4
- package/lib/dts/utils/type-utils.d.ts +3 -0
- package/lib/esm/agents/agent.d.ts +37 -33
- package/lib/esm/agents/agent.js +67 -24
- package/lib/esm/agents/ai-agent.d.ts +8 -12
- package/lib/esm/agents/ai-agent.js +13 -31
- package/lib/esm/agents/mcp-agent.d.ts +25 -9
- package/lib/esm/agents/mcp-agent.js +54 -8
- package/lib/esm/agents/memory.d.ts +26 -0
- package/lib/esm/agents/memory.js +34 -0
- package/lib/esm/agents/types.d.ts +4 -3
- package/lib/esm/agents/types.js +10 -1
- package/lib/esm/agents/user-agent.d.ts +24 -0
- package/lib/esm/agents/user-agent.js +58 -0
- package/lib/esm/execution-engine/context.d.ts +35 -5
- package/lib/esm/execution-engine/execution-engine.d.ts +64 -0
- package/lib/esm/execution-engine/execution-engine.js +129 -0
- package/lib/esm/execution-engine/index.d.ts +4 -46
- package/lib/esm/execution-engine/index.js +4 -188
- package/lib/esm/execution-engine/message-queue.d.ts +17 -2
- package/lib/esm/execution-engine/message-queue.js +37 -1
- package/lib/esm/execution-engine/utils.d.ts +4 -0
- package/lib/esm/execution-engine/utils.js +28 -0
- package/lib/esm/index.d.ts +14 -11
- package/lib/esm/index.js +14 -11
- package/lib/{cjs/models/chat.d.ts → esm/models/chat-model.d.ts} +15 -12
- package/lib/esm/models/{chat.js → chat-model.js} +4 -4
- package/lib/esm/models/claude-chat-model.d.ts +17 -0
- package/lib/esm/models/claude-chat-model.js +192 -0
- package/lib/esm/models/{chat-openai.d.ts → openai-chat-model.d.ts} +2 -2
- package/lib/esm/models/{chat-openai.js → openai-chat-model.js} +5 -5
- package/lib/esm/prompt/prompt-builder.d.ts +14 -14
- package/lib/esm/prompt/prompt-builder.js +56 -58
- package/lib/esm/prompt/template.d.ts +9 -21
- package/lib/esm/prompt/template.js +3 -5
- package/lib/esm/utils/json-schema.d.ts +3 -0
- package/lib/esm/utils/json-schema.js +20 -0
- package/lib/esm/utils/mcp-utils.d.ts +3 -2
- package/lib/esm/utils/mcp-utils.js +26 -15
- package/lib/esm/utils/run-chat-loop.d.ts +5 -4
- package/lib/esm/utils/run-chat-loop.js +25 -4
- package/lib/esm/utils/type-utils.d.ts +3 -0
- package/lib/esm/utils/type-utils.js +4 -2
- package/package.json +31 -30
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Context } from "../execution-engine/context.js";
|
|
2
|
+
import type { Message } from "./agent.js";
|
|
3
|
+
export interface AgentMemoryOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Enable memory, default is true
|
|
6
|
+
*/
|
|
7
|
+
enabled?: boolean;
|
|
8
|
+
subscribeTopic?: string | string[];
|
|
9
|
+
maxMemoriesInChat?: number;
|
|
10
|
+
}
|
|
11
|
+
export interface Memory {
|
|
12
|
+
role: "user" | "agent";
|
|
13
|
+
content: Message;
|
|
14
|
+
source?: string;
|
|
15
|
+
}
|
|
16
|
+
export declare class AgentMemory {
|
|
17
|
+
constructor(options: AgentMemoryOptions);
|
|
18
|
+
enabled?: boolean;
|
|
19
|
+
subscribeTopic?: string | string[];
|
|
20
|
+
maxMemoriesInChat?: number;
|
|
21
|
+
memories: Memory[];
|
|
22
|
+
private subscriptions;
|
|
23
|
+
addMemory(memory: Memory): void;
|
|
24
|
+
attach(context: Context): void;
|
|
25
|
+
detach(): void;
|
|
26
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AgentMemory = void 0;
|
|
4
|
+
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
5
|
+
class AgentMemory {
|
|
6
|
+
constructor(options) {
|
|
7
|
+
this.enabled = options.enabled ?? true;
|
|
8
|
+
this.subscribeTopic = options.subscribeTopic;
|
|
9
|
+
this.maxMemoriesInChat = options.maxMemoriesInChat;
|
|
10
|
+
}
|
|
11
|
+
enabled;
|
|
12
|
+
subscribeTopic;
|
|
13
|
+
maxMemoriesInChat;
|
|
14
|
+
memories = [];
|
|
15
|
+
subscriptions = [];
|
|
16
|
+
addMemory(memory) {
|
|
17
|
+
if (this.memories.at(-1)?.content === memory.content)
|
|
18
|
+
return;
|
|
19
|
+
// TODO: save all memories into database instead of in-memory,
|
|
20
|
+
// and give every memory a unique id to avoid duplication
|
|
21
|
+
this.memories.push(memory);
|
|
22
|
+
}
|
|
23
|
+
attach(context) {
|
|
24
|
+
for (const topic of (0, type_utils_js_1.orArrayToArray)(this.subscribeTopic)) {
|
|
25
|
+
const sub = context.subscribe(topic, ({ role, message, source }) => {
|
|
26
|
+
this.addMemory({ role, source, content: message });
|
|
27
|
+
});
|
|
28
|
+
this.subscriptions.push(sub);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
detach() {
|
|
32
|
+
for (const sub of this.subscriptions) {
|
|
33
|
+
sub();
|
|
34
|
+
}
|
|
35
|
+
this.subscriptions = [];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.AgentMemory = AgentMemory;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import type { Agent,
|
|
1
|
+
import type { Agent, Message } from "./agent.js";
|
|
2
2
|
export declare const transferAgentOutputKey = "$transferAgentTo";
|
|
3
|
-
export interface TransferAgentOutput extends
|
|
3
|
+
export interface TransferAgentOutput extends Message {
|
|
4
4
|
[transferAgentOutputKey]: {
|
|
5
5
|
agent: Agent;
|
|
6
6
|
};
|
|
7
7
|
}
|
|
8
8
|
export declare function transferToAgentOutput(agent: Agent): TransferAgentOutput;
|
|
9
|
-
export declare function isTransferAgentOutput(output:
|
|
9
|
+
export declare function isTransferAgentOutput(output: Message): output is TransferAgentOutput;
|
|
10
|
+
export declare function replaceTransferAgentToName(output: Message): Message;
|
package/lib/cjs/agents/types.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.transferAgentOutputKey = void 0;
|
|
4
4
|
exports.transferToAgentOutput = transferToAgentOutput;
|
|
5
5
|
exports.isTransferAgentOutput = isTransferAgentOutput;
|
|
6
|
+
exports.replaceTransferAgentToName = replaceTransferAgentToName;
|
|
6
7
|
exports.transferAgentOutputKey = "$transferAgentTo";
|
|
7
8
|
function transferToAgentOutput(agent) {
|
|
8
9
|
return {
|
|
@@ -12,5 +13,14 @@ function transferToAgentOutput(agent) {
|
|
|
12
13
|
};
|
|
13
14
|
}
|
|
14
15
|
function isTransferAgentOutput(output) {
|
|
15
|
-
return exports.transferAgentOutputKey
|
|
16
|
+
return !!output[exports.transferAgentOutputKey]?.agent;
|
|
17
|
+
}
|
|
18
|
+
function replaceTransferAgentToName(output) {
|
|
19
|
+
if (isTransferAgentOutput(output)) {
|
|
20
|
+
return {
|
|
21
|
+
...output,
|
|
22
|
+
[exports.transferAgentOutputKey]: output[exports.transferAgentOutputKey].agent.name,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
return output;
|
|
16
26
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Context } from "../execution-engine/context.js";
|
|
2
|
+
import type { MessagePayload, MessageQueueListener, Unsubscribe } from "../execution-engine/message-queue.js";
|
|
3
|
+
import { type PromiseOrValue } from "../utils/type-utils.js";
|
|
4
|
+
import { Agent, type AgentOptions, type Message } from "./agent.js";
|
|
5
|
+
export interface UserAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
|
|
6
|
+
context?: Context;
|
|
7
|
+
process?: (input: I, context: Context) => PromiseOrValue<O>;
|
|
8
|
+
}
|
|
9
|
+
export declare class UserAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
|
|
10
|
+
static from<I extends Message, O extends Message>(options: UserAgentOptions<I, O>): UserAgent<I, O>;
|
|
11
|
+
constructor(options: UserAgentOptions<I, O>);
|
|
12
|
+
private context?;
|
|
13
|
+
private get ctx();
|
|
14
|
+
private _process?;
|
|
15
|
+
process(input: I, context?: Context): Promise<O>;
|
|
16
|
+
publish(topic: string | string[], message: Message | string): void;
|
|
17
|
+
subscribe(topic: string, listener?: undefined): Promise<MessagePayload>;
|
|
18
|
+
subscribe(topic: string, listener: MessageQueueListener): Unsubscribe;
|
|
19
|
+
subscribe(topic: string, listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
|
|
20
|
+
unsubscribe(topic: string, listener: MessageQueueListener): void;
|
|
21
|
+
get stream(): ReadableStream<MessagePayload & {
|
|
22
|
+
topic: string;
|
|
23
|
+
}>;
|
|
24
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UserAgent = void 0;
|
|
4
|
+
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
5
|
+
const agent_js_1 = require("./agent.js");
|
|
6
|
+
class UserAgent extends agent_js_1.Agent {
|
|
7
|
+
static from(options) {
|
|
8
|
+
return new UserAgent(options);
|
|
9
|
+
}
|
|
10
|
+
constructor(options) {
|
|
11
|
+
super({ ...options, disableLogging: true });
|
|
12
|
+
this._process = options.process;
|
|
13
|
+
this.context = options.context;
|
|
14
|
+
}
|
|
15
|
+
context;
|
|
16
|
+
get ctx() {
|
|
17
|
+
if (!this.context)
|
|
18
|
+
throw new Error("UserAgent must have a context");
|
|
19
|
+
return this.context;
|
|
20
|
+
}
|
|
21
|
+
_process;
|
|
22
|
+
async process(input, context) {
|
|
23
|
+
const ctx = context ?? this.context;
|
|
24
|
+
if (!ctx)
|
|
25
|
+
throw new Error("UserAgent must have a context");
|
|
26
|
+
if (this._process) {
|
|
27
|
+
return this._process(input, ctx);
|
|
28
|
+
}
|
|
29
|
+
const publicTopic = typeof this.publishTopic === "function" ? await this.publishTopic(input) : this.publishTopic;
|
|
30
|
+
if (publicTopic?.length) {
|
|
31
|
+
ctx.publish(publicTopic, input, this);
|
|
32
|
+
return {};
|
|
33
|
+
}
|
|
34
|
+
throw new Error("UserAgent must have a process function or a publishTopic");
|
|
35
|
+
}
|
|
36
|
+
publish(topic, message) {
|
|
37
|
+
return this.ctx.publish(topic, message, this);
|
|
38
|
+
}
|
|
39
|
+
subscribe(topic, listener) {
|
|
40
|
+
return this.ctx.subscribe(topic, listener);
|
|
41
|
+
}
|
|
42
|
+
unsubscribe(topic, listener) {
|
|
43
|
+
this.ctx.unsubscribe(topic, listener);
|
|
44
|
+
}
|
|
45
|
+
get stream() {
|
|
46
|
+
let subscriptions = [];
|
|
47
|
+
return new ReadableStream({
|
|
48
|
+
start: (controller) => {
|
|
49
|
+
const subscribeTopic = (0, type_utils_js_1.orArrayToArray)(this.subscribeTopic);
|
|
50
|
+
subscriptions = subscribeTopic.map((topic) => this.subscribe(topic, (message) => {
|
|
51
|
+
controller.enqueue({ ...message, topic });
|
|
52
|
+
}));
|
|
53
|
+
},
|
|
54
|
+
cancel: () => {
|
|
55
|
+
for (const unsubscribe of subscriptions) {
|
|
56
|
+
unsubscribe();
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
exports.UserAgent = UserAgent;
|
|
@@ -1,10 +1,40 @@
|
|
|
1
1
|
import type EventEmitter from "node:events";
|
|
2
|
-
import type { Agent,
|
|
3
|
-
import type { ChatModel } from "../models/chat";
|
|
2
|
+
import type { Agent, FunctionAgentFn, Message } from "../agents/agent.js";
|
|
3
|
+
import type { ChatModel } from "../models/chat-model.js";
|
|
4
|
+
import type { MessagePayload, MessageQueueListener, Unsubscribe } from "./message-queue.js";
|
|
5
|
+
export type Runnable<I extends Message = Message, O extends Message = Message> = Agent<I, O> | FunctionAgentFn;
|
|
4
6
|
export interface Context extends EventEmitter {
|
|
5
7
|
model?: ChatModel;
|
|
6
8
|
tools?: Agent[];
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Call an agent with a message
|
|
11
|
+
* @param agent Agent to call
|
|
12
|
+
* @param message Message to pass to the agent
|
|
13
|
+
* @returns the output of the agent
|
|
14
|
+
*/
|
|
15
|
+
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string): Promise<O>;
|
|
16
|
+
/**
|
|
17
|
+
* Call an agent with a message and return the output and the active agent
|
|
18
|
+
* @param agent Agent to call
|
|
19
|
+
* @param message Message to pass to the agent
|
|
20
|
+
* @param options.returnActiveAgent return the active agent
|
|
21
|
+
* @returns the output of the agent and the final active agent
|
|
22
|
+
*/
|
|
23
|
+
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options: {
|
|
24
|
+
returnActiveAgent?: true;
|
|
25
|
+
}): Promise<[O, Runnable]>;
|
|
26
|
+
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options?: {
|
|
27
|
+
returnActiveAgent?: boolean;
|
|
28
|
+
}): Promise<O | [O, Runnable]>;
|
|
29
|
+
/**
|
|
30
|
+
* Publish a message to a topic, the engine will call the listeners of the topic
|
|
31
|
+
* @param topic topic name, or an array of topic names
|
|
32
|
+
* @param message message to publish
|
|
33
|
+
* @param from the agent who publish the message, if not provided, it will be treated as a user message
|
|
34
|
+
*/
|
|
35
|
+
publish(topic: string | string[], message: Message | string, from?: Agent): void;
|
|
36
|
+
subscribe(topic: string, listener?: undefined): Promise<MessagePayload>;
|
|
37
|
+
subscribe(topic: string, listener: MessageQueueListener): Unsubscribe;
|
|
38
|
+
subscribe(topic: string, listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
|
|
39
|
+
unsubscribe(topic: string, listener: MessageQueueListener): void;
|
|
10
40
|
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import EventEmitter from "node:events";
|
|
2
|
+
import { Agent, type Message } from "../agents/agent.js";
|
|
3
|
+
import { UserAgent } from "../agents/user-agent.js";
|
|
4
|
+
import type { ChatModel } from "../models/chat-model.js";
|
|
5
|
+
import type { Context, Runnable } from "./context.js";
|
|
6
|
+
import { type MessagePayload, MessageQueue, type MessageQueueListener, type Unsubscribe } from "./message-queue.js";
|
|
7
|
+
export interface ExecutionEngineOptions {
|
|
8
|
+
model?: ChatModel;
|
|
9
|
+
tools?: Agent[];
|
|
10
|
+
agents?: Agent[];
|
|
11
|
+
}
|
|
12
|
+
export declare class ExecutionEngine extends EventEmitter implements Context {
|
|
13
|
+
constructor(options?: ExecutionEngineOptions);
|
|
14
|
+
readonly messageQueue: MessageQueue;
|
|
15
|
+
model?: ChatModel;
|
|
16
|
+
tools: Agent[];
|
|
17
|
+
private agents;
|
|
18
|
+
addAgent(...agents: Agent[]): void;
|
|
19
|
+
private createMessageFromInput;
|
|
20
|
+
/**
|
|
21
|
+
* Publish a message to a topic, the engine will call the listeners of the topic
|
|
22
|
+
* @param topic topic name, or an array of topic names
|
|
23
|
+
* @param message message to publish
|
|
24
|
+
* @param from the agent who publish the message, if not provided, it will be treated as a user message
|
|
25
|
+
*/
|
|
26
|
+
publish(topic: string | string[], message: Message | string, from?: Agent): void;
|
|
27
|
+
/**
|
|
28
|
+
* Create a user agent to consistently call an agent
|
|
29
|
+
* @param agent Agent to call
|
|
30
|
+
* @returns User agent
|
|
31
|
+
*/
|
|
32
|
+
call<I extends Message, O extends Message>(agent: Runnable<I, O>): UserAgent<I, O>;
|
|
33
|
+
/**
|
|
34
|
+
* Call an agent with a message
|
|
35
|
+
* @param agent Agent to call
|
|
36
|
+
* @param message Message to pass to the agent
|
|
37
|
+
* @returns the output of the agent
|
|
38
|
+
*/
|
|
39
|
+
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string): Promise<O>;
|
|
40
|
+
/**
|
|
41
|
+
* Call an agent with a message and return the output and the active agent
|
|
42
|
+
* @param agent Agent to call
|
|
43
|
+
* @param message Message to pass to the agent
|
|
44
|
+
* @param options.returnActiveAgent return the active agent
|
|
45
|
+
* @returns the output of the agent and the final active agent
|
|
46
|
+
*/
|
|
47
|
+
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options: {
|
|
48
|
+
returnActiveAgent?: true;
|
|
49
|
+
}): Promise<[O, Runnable]>;
|
|
50
|
+
/**
|
|
51
|
+
* Call an agent with a message and return the output and the active agent
|
|
52
|
+
* @param agent Agent to call
|
|
53
|
+
* @param message Message to pass to the agent
|
|
54
|
+
* @returns the output of the agent and the final active agent
|
|
55
|
+
*/
|
|
56
|
+
private _call;
|
|
57
|
+
subscribe(topic: string, listener?: undefined): Promise<MessagePayload>;
|
|
58
|
+
subscribe(topic: string, listener: MessageQueueListener): Unsubscribe;
|
|
59
|
+
subscribe(topic: string, listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
|
|
60
|
+
unsubscribe(topic: string, listener: MessageQueueListener): void;
|
|
61
|
+
private callAgent;
|
|
62
|
+
shutdown(): Promise<void>;
|
|
63
|
+
private initProcessExitHandler;
|
|
64
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ExecutionEngine = void 0;
|
|
7
|
+
const node_events_1 = __importDefault(require("node:events"));
|
|
8
|
+
const lodash_es_1 = require("lodash-es");
|
|
9
|
+
const agent_js_1 = require("../agents/agent.js");
|
|
10
|
+
const types_js_1 = require("../agents/types.js");
|
|
11
|
+
const user_agent_js_1 = require("../agents/user-agent.js");
|
|
12
|
+
const prompt_builder_js_1 = require("../prompt/prompt-builder.js");
|
|
13
|
+
const message_queue_js_1 = require("./message-queue.js");
|
|
14
|
+
class ExecutionEngine extends node_events_1.default {
|
|
15
|
+
constructor(options) {
|
|
16
|
+
super();
|
|
17
|
+
this.model = options?.model;
|
|
18
|
+
this.tools = options?.tools ?? [];
|
|
19
|
+
if (options?.agents?.length)
|
|
20
|
+
this.addAgent(...options.agents);
|
|
21
|
+
this.initProcessExitHandler();
|
|
22
|
+
}
|
|
23
|
+
messageQueue = new message_queue_js_1.MessageQueue();
|
|
24
|
+
model;
|
|
25
|
+
tools;
|
|
26
|
+
agents = [];
|
|
27
|
+
addAgent(...agents) {
|
|
28
|
+
for (const agent of agents) {
|
|
29
|
+
this.agents.push(agent);
|
|
30
|
+
agent.attach(this);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
createMessageFromInput(message) {
|
|
34
|
+
return typeof message === "string" ? (0, prompt_builder_js_1.createMessage)(message) : message;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Publish a message to a topic, the engine will call the listeners of the topic
|
|
38
|
+
* @param topic topic name, or an array of topic names
|
|
39
|
+
* @param message message to publish
|
|
40
|
+
* @param from the agent who publish the message, if not provided, it will be treated as a user message
|
|
41
|
+
*/
|
|
42
|
+
publish(topic, message, from) {
|
|
43
|
+
const request = {
|
|
44
|
+
role: !from || from instanceof user_agent_js_1.UserAgent ? "user" : "agent",
|
|
45
|
+
source: from?.name,
|
|
46
|
+
message: this.createMessageFromInput(message),
|
|
47
|
+
};
|
|
48
|
+
this.messageQueue.publish(topic, request);
|
|
49
|
+
}
|
|
50
|
+
call(agent, message, options) {
|
|
51
|
+
if ((0, lodash_es_1.isNil)(message)) {
|
|
52
|
+
let activeAgent = agent;
|
|
53
|
+
return user_agent_js_1.UserAgent.from({
|
|
54
|
+
context: this,
|
|
55
|
+
process: async (input, context) => {
|
|
56
|
+
const [output, agent] = await context.call(activeAgent, input, {
|
|
57
|
+
returnActiveAgent: true,
|
|
58
|
+
});
|
|
59
|
+
activeAgent = agent;
|
|
60
|
+
return output;
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
return this._call(agent, this.createMessageFromInput(message)).then((result) => options?.returnActiveAgent ? [result.output, result.agent] : result.output);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Call an agent with a message and return the output and the active agent
|
|
68
|
+
* @param agent Agent to call
|
|
69
|
+
* @param message Message to pass to the agent
|
|
70
|
+
* @returns the output of the agent and the final active agent
|
|
71
|
+
*/
|
|
72
|
+
async _call(agent, message) {
|
|
73
|
+
const { output, agent: activeAgent } = await this.callAgent(agent, message);
|
|
74
|
+
if (activeAgent instanceof agent_js_1.Agent) {
|
|
75
|
+
const publishTopics = typeof activeAgent.publishTopic === "function"
|
|
76
|
+
? await activeAgent.publishTopic(output)
|
|
77
|
+
: activeAgent.publishTopic;
|
|
78
|
+
if (publishTopics?.length) {
|
|
79
|
+
this.publish(publishTopics, output, activeAgent);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return { output, agent: activeAgent };
|
|
83
|
+
}
|
|
84
|
+
subscribe(topic, listener) {
|
|
85
|
+
return this.messageQueue.subscribe(topic, listener);
|
|
86
|
+
}
|
|
87
|
+
unsubscribe(topic, listener) {
|
|
88
|
+
this.messageQueue.unsubscribe(topic, listener);
|
|
89
|
+
}
|
|
90
|
+
async callAgent(agent, input) {
|
|
91
|
+
let activeAgent = agent;
|
|
92
|
+
let output;
|
|
93
|
+
for (;;) {
|
|
94
|
+
let result;
|
|
95
|
+
if (typeof activeAgent === "function") {
|
|
96
|
+
result = await activeAgent(input, this);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
result = await activeAgent.call(input, this);
|
|
100
|
+
}
|
|
101
|
+
if (!(result instanceof agent_js_1.Agent))
|
|
102
|
+
output = result;
|
|
103
|
+
const transferToAgent = result instanceof agent_js_1.Agent
|
|
104
|
+
? result
|
|
105
|
+
: (0, types_js_1.isTransferAgentOutput)(result)
|
|
106
|
+
? result[types_js_1.transferAgentOutputKey].agent
|
|
107
|
+
: undefined;
|
|
108
|
+
if (transferToAgent) {
|
|
109
|
+
activeAgent = transferToAgent;
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (!output)
|
|
116
|
+
throw new Error("Unexpected empty output");
|
|
117
|
+
return {
|
|
118
|
+
agent: activeAgent,
|
|
119
|
+
output,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
async shutdown() {
|
|
123
|
+
for (const tool of this.tools) {
|
|
124
|
+
await tool.shutdown();
|
|
125
|
+
}
|
|
126
|
+
for (const agent of this.agents) {
|
|
127
|
+
await agent.shutdown();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
initProcessExitHandler() {
|
|
131
|
+
const shutdownAndExit = () => this.shutdown().finally(() => process.exit(0));
|
|
132
|
+
process.on("SIGINT", shutdownAndExit);
|
|
133
|
+
process.on("exit", shutdownAndExit);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
exports.ExecutionEngine = ExecutionEngine;
|
|
@@ -1,46 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export interface ExecutionEngineOptions {
|
|
6
|
-
model?: ChatModel;
|
|
7
|
-
tools?: Agent[];
|
|
8
|
-
agents?: Agent[];
|
|
9
|
-
}
|
|
10
|
-
export interface ExecutionEngineRunOptions {
|
|
11
|
-
concurrency?: boolean;
|
|
12
|
-
}
|
|
13
|
-
export declare class ExecutionEngine extends EventEmitter implements Context {
|
|
14
|
-
constructor(options?: ExecutionEngineOptions);
|
|
15
|
-
private messageQueue;
|
|
16
|
-
model?: ChatModel;
|
|
17
|
-
tools: Agent[];
|
|
18
|
-
private agents;
|
|
19
|
-
private agentListeners;
|
|
20
|
-
addAgent(...agents: Agent[]): void;
|
|
21
|
-
private attachAgentSubscriptions;
|
|
22
|
-
publish(topic: string, message: unknown): void;
|
|
23
|
-
subscribe(topic: string, listener: (message: AgentOutput) => void): void;
|
|
24
|
-
unsubscribe(topic: string, listener: (message: AgentOutput) => void): void;
|
|
25
|
-
run(agent: Agent): Promise<UserAgent>;
|
|
26
|
-
run(input: AgentInput | string): Promise<AgentOutput>;
|
|
27
|
-
run(input: AgentInput | string, ...agents: Agent[]): Promise<AgentOutput>;
|
|
28
|
-
run(input: AgentInput | string, options: ExecutionEngineRunOptions, ...agents: Agent[]): Promise<AgentOutput>;
|
|
29
|
-
private splitOptionsAndAgents;
|
|
30
|
-
private runSequential;
|
|
31
|
-
private runParallel;
|
|
32
|
-
private publishUserInputTopic;
|
|
33
|
-
private runChatLoop;
|
|
34
|
-
private callAgent;
|
|
35
|
-
shutdown(): Promise<void>;
|
|
36
|
-
private initProcessExitHandler;
|
|
37
|
-
}
|
|
38
|
-
export declare class UserAgent<I extends AgentInput = AgentInput, O extends AgentOutput = AgentOutput> extends Agent<I, O> {
|
|
39
|
-
options: AgentOptions<I, O> & {
|
|
40
|
-
run: (input: I) => Promise<O>;
|
|
41
|
-
};
|
|
42
|
-
constructor(options: AgentOptions<I, O> & {
|
|
43
|
-
run: (input: I) => Promise<O>;
|
|
44
|
-
});
|
|
45
|
-
process(input: I): Promise<O>;
|
|
46
|
-
}
|
|
1
|
+
export * from "./context.js";
|
|
2
|
+
export * from "./execution-engine.js";
|
|
3
|
+
export * from "./message-queue.js";
|
|
4
|
+
export * from "./utils.js";
|