@aigne/core 1.6.0 → 1.7.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 +14 -0
- package/lib/cjs/agents/agent.d.ts +5 -2
- package/lib/cjs/agents/agent.js +42 -24
- package/lib/cjs/agents/ai-agent.d.ts +8 -8
- package/lib/cjs/agents/ai-agent.js +5 -2
- package/lib/cjs/agents/mcp-agent.d.ts +11 -0
- package/lib/cjs/agents/mcp-agent.js +37 -23
- package/lib/cjs/agents/user-agent.d.ts +9 -9
- package/lib/cjs/agents/user-agent.js +26 -16
- package/lib/cjs/execution-engine/context.d.ts +84 -46
- package/lib/cjs/execution-engine/context.js +136 -98
- package/lib/cjs/execution-engine/execution-engine.d.ts +16 -47
- package/lib/cjs/execution-engine/execution-engine.js +13 -40
- package/lib/cjs/execution-engine/message-queue.d.ts +3 -3
- package/lib/cjs/execution-engine/message-queue.js +32 -2
- package/lib/cjs/execution-engine/usage.d.ts +11 -0
- package/lib/cjs/execution-engine/usage.js +10 -0
- package/lib/cjs/loader/index.js +1 -1
- package/lib/cjs/models/chat-model.d.ts +3 -2
- package/lib/cjs/models/chat-model.js +6 -5
- package/lib/cjs/models/claude-chat-model.js +10 -7
- package/lib/cjs/models/openai-chat-model.js +5 -2
- package/lib/cjs/prompt/prompt-builder.d.ts +1 -1
- package/lib/cjs/prompt/prompt-builder.js +3 -1
- package/lib/cjs/utils/json-schema.js +2 -2
- package/lib/cjs/utils/logger.d.ts +3 -15
- package/lib/cjs/utils/logger.js +3 -77
- package/lib/cjs/utils/mcp-utils.js +1 -5
- package/lib/cjs/utils/model-utils.js +2 -2
- package/lib/cjs/utils/type-utils.d.ts +1 -0
- package/lib/cjs/utils/typed-event-emtter.d.ts +10 -0
- package/lib/cjs/utils/typed-event-emtter.js +2 -0
- package/lib/dts/agents/agent.d.ts +5 -2
- package/lib/dts/agents/ai-agent.d.ts +8 -8
- package/lib/dts/agents/mcp-agent.d.ts +11 -0
- package/lib/dts/agents/user-agent.d.ts +9 -9
- package/lib/dts/execution-engine/context.d.ts +84 -46
- package/lib/dts/execution-engine/execution-engine.d.ts +16 -47
- package/lib/dts/execution-engine/message-queue.d.ts +3 -3
- package/lib/dts/execution-engine/usage.d.ts +11 -0
- package/lib/dts/models/chat-model.d.ts +3 -2
- package/lib/dts/prompt/prompt-builder.d.ts +1 -1
- package/lib/dts/utils/logger.d.ts +3 -15
- package/lib/dts/utils/type-utils.d.ts +1 -0
- package/lib/dts/utils/typed-event-emtter.d.ts +10 -0
- package/lib/esm/agents/agent.d.ts +5 -2
- package/lib/esm/agents/agent.js +42 -24
- package/lib/esm/agents/ai-agent.d.ts +8 -8
- package/lib/esm/agents/ai-agent.js +5 -2
- package/lib/esm/agents/mcp-agent.d.ts +11 -0
- package/lib/esm/agents/mcp-agent.js +38 -24
- package/lib/esm/agents/user-agent.d.ts +9 -9
- package/lib/esm/agents/user-agent.js +26 -16
- package/lib/esm/execution-engine/context.d.ts +84 -46
- package/lib/esm/execution-engine/context.js +135 -98
- package/lib/esm/execution-engine/execution-engine.d.ts +16 -47
- package/lib/esm/execution-engine/execution-engine.js +14 -38
- package/lib/esm/execution-engine/message-queue.d.ts +3 -3
- package/lib/esm/execution-engine/message-queue.js +33 -3
- package/lib/esm/execution-engine/usage.d.ts +11 -0
- package/lib/esm/execution-engine/usage.js +7 -0
- package/lib/esm/loader/index.js +2 -2
- package/lib/esm/models/chat-model.d.ts +3 -2
- package/lib/esm/models/chat-model.js +6 -5
- package/lib/esm/models/claude-chat-model.js +10 -7
- package/lib/esm/models/openai-chat-model.js +5 -2
- package/lib/esm/prompt/prompt-builder.d.ts +1 -1
- package/lib/esm/prompt/prompt-builder.js +3 -1
- package/lib/esm/utils/json-schema.js +2 -2
- package/lib/esm/utils/logger.d.ts +3 -15
- package/lib/esm/utils/logger.js +3 -77
- package/lib/esm/utils/mcp-utils.js +1 -5
- package/lib/esm/utils/model-utils.js +2 -2
- package/lib/esm/utils/type-utils.d.ts +1 -0
- package/lib/esm/utils/typed-event-emtter.d.ts +10 -0
- package/lib/esm/utils/typed-event-emtter.js +1 -0
- package/package.json +7 -8
- package/lib/cjs/utils/run-chat-loop.d.ts +0 -11
- package/lib/cjs/utils/run-chat-loop.js +0 -82
- package/lib/dts/utils/run-chat-loop.d.ts +0 -11
- package/lib/esm/utils/run-chat-loop.d.ts +0 -11
- package/lib/esm/utils/run-chat-loop.js +0 -76
|
@@ -2,19 +2,36 @@ import EventEmitter from "node:events";
|
|
|
2
2
|
import { Agent, type FunctionAgentFn, 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
|
+
import { type OmitPropertiesFromArrayFirstElement } from "../utils/type-utils.js";
|
|
6
|
+
import type { Args, Listener, TypedEventEmitter } from "../utils/typed-event-emtter.js";
|
|
5
7
|
import { type MessagePayload, MessageQueue, type MessageQueueListener, type Unsubscribe } from "./message-queue.js";
|
|
8
|
+
import { type ContextLimits, type ContextUsage } from "./usage.js";
|
|
6
9
|
export type Runnable<I extends Message = Message, O extends Message = Message> = Agent<I, O> | FunctionAgentFn;
|
|
7
|
-
export interface
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
export interface AgentEvent {
|
|
11
|
+
parentContextId?: string;
|
|
12
|
+
contextId: string;
|
|
13
|
+
timestamp: number;
|
|
14
|
+
agent: Agent;
|
|
11
15
|
}
|
|
12
|
-
export interface
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
+
export interface ContextEventMap {
|
|
17
|
+
agentStarted: [AgentEvent & {
|
|
18
|
+
input: Message;
|
|
19
|
+
}];
|
|
20
|
+
agentSucceed: [AgentEvent & {
|
|
21
|
+
output: Message;
|
|
22
|
+
}];
|
|
23
|
+
agentFailed: [AgentEvent & {
|
|
24
|
+
error: Error;
|
|
25
|
+
}];
|
|
16
26
|
}
|
|
17
|
-
export
|
|
27
|
+
export type ContextEmitEventMap = {
|
|
28
|
+
[K in keyof ContextEventMap]: OmitPropertiesFromArrayFirstElement<ContextEventMap[K], "contextId" | "parentContextId" | "timestamp">;
|
|
29
|
+
};
|
|
30
|
+
export interface CallOptions {
|
|
31
|
+
returnActiveAgent?: boolean;
|
|
32
|
+
disableTransfer?: boolean;
|
|
33
|
+
}
|
|
34
|
+
export interface Context extends TypedEventEmitter<ContextEventMap, ContextEmitEventMap> {
|
|
18
35
|
model?: ChatModel;
|
|
19
36
|
tools?: Agent[];
|
|
20
37
|
usage: ContextUsage;
|
|
@@ -26,13 +43,6 @@ export interface Context extends EventEmitter {
|
|
|
26
43
|
* @returns User agent
|
|
27
44
|
*/
|
|
28
45
|
call<I extends Message, O extends Message>(agent: Runnable<I, O>): UserAgent<I, O>;
|
|
29
|
-
/**
|
|
30
|
-
* Call an agent with a message
|
|
31
|
-
* @param agent Agent to call
|
|
32
|
-
* @param message Message to pass to the agent
|
|
33
|
-
* @returns the output of the agent
|
|
34
|
-
*/
|
|
35
|
-
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string): Promise<O>;
|
|
36
46
|
/**
|
|
37
47
|
* Call an agent with a message and return the output and the active agent
|
|
38
48
|
* @param agent Agent to call
|
|
@@ -40,54 +50,82 @@ export interface Context extends EventEmitter {
|
|
|
40
50
|
* @param options.returnActiveAgent return the active agent
|
|
41
51
|
* @returns the output of the agent and the final active agent
|
|
42
52
|
*/
|
|
43
|
-
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options: {
|
|
53
|
+
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options: CallOptions & {
|
|
44
54
|
returnActiveAgent: true;
|
|
45
55
|
}): Promise<[O, Runnable]>;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Call an agent with a message
|
|
58
|
+
* @param agent Agent to call
|
|
59
|
+
* @param message Message to pass to the agent
|
|
60
|
+
* @returns the output of the agent
|
|
61
|
+
*/
|
|
62
|
+
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options?: CallOptions): Promise<O>;
|
|
63
|
+
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message?: I | string, options?: CallOptions): UserAgent<I, O> | Promise<O | [O, Runnable]>;
|
|
49
64
|
/**
|
|
50
65
|
* Publish a message to a topic, the engine will call the listeners of the topic
|
|
51
66
|
* @param topic topic name, or an array of topic names
|
|
52
|
-
* @param
|
|
53
|
-
* @param from the agent who publish the message, if not provided, it will be treated as a user message
|
|
67
|
+
* @param payload message to publish
|
|
54
68
|
*/
|
|
55
|
-
publish(topic: string | string[],
|
|
69
|
+
publish(topic: string | string[], payload: Omit<MessagePayload, "context">): void;
|
|
56
70
|
subscribe(topic: string, listener?: undefined): Promise<MessagePayload>;
|
|
57
71
|
subscribe(topic: string, listener: MessageQueueListener): Unsubscribe;
|
|
58
72
|
subscribe(topic: string, listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
|
|
73
|
+
subscribe(topic: string, listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
|
|
59
74
|
unsubscribe(topic: string, listener: MessageQueueListener): void;
|
|
75
|
+
/**
|
|
76
|
+
* Create a child context with the same configuration as the parent context.
|
|
77
|
+
* If `reset` is true, the child context will have a new state (such as: usage).
|
|
78
|
+
*
|
|
79
|
+
* @param options
|
|
80
|
+
* @param options.reset create a new context with initial state (such as: usage)
|
|
81
|
+
* @returns new context
|
|
82
|
+
*/
|
|
83
|
+
newContext(options?: {
|
|
84
|
+
reset?: boolean;
|
|
85
|
+
}): Context;
|
|
86
|
+
}
|
|
87
|
+
export declare function createPublishMessage(message: string | Message, from?: Agent): Omit<MessagePayload, "context">;
|
|
88
|
+
export declare class ExecutionContext implements Context {
|
|
89
|
+
constructor(parent?: ConstructorParameters<typeof ExecutionContextInternal>[0]);
|
|
90
|
+
parentId?: string;
|
|
91
|
+
id: string;
|
|
92
|
+
readonly internal: ExecutionContextInternal;
|
|
93
|
+
get model(): ChatModel | undefined;
|
|
94
|
+
get tools(): Agent<Message, Message>[] | undefined;
|
|
95
|
+
get limits(): ContextLimits | undefined;
|
|
96
|
+
get status(): "normal" | "timeout";
|
|
97
|
+
get usage(): ContextUsage;
|
|
98
|
+
newContext({ reset }?: {
|
|
99
|
+
reset?: boolean;
|
|
100
|
+
}): ExecutionContext;
|
|
101
|
+
call: Context["call"];
|
|
102
|
+
publish: Context["publish"];
|
|
103
|
+
subscribe: Context["subscribe"];
|
|
104
|
+
unsubscribe: Context["unsubscribe"];
|
|
105
|
+
emit<K extends keyof ContextEmitEventMap>(eventName: K, ...args: Args<K, ContextEmitEventMap>): boolean;
|
|
106
|
+
on<K extends keyof ContextEventMap>(eventName: K, listener: Listener<K, ContextEventMap>): this;
|
|
107
|
+
once<K extends keyof ContextEventMap>(eventName: K, listener: Listener<K, ContextEventMap>): this;
|
|
108
|
+
off<K extends keyof ContextEventMap>(eventName: K, listener: Listener<K, ContextEventMap>): this;
|
|
60
109
|
}
|
|
61
|
-
|
|
62
|
-
private readonly
|
|
63
|
-
constructor(
|
|
64
|
-
model?: ChatModel;
|
|
65
|
-
tools?: Agent[];
|
|
66
|
-
limits?: ContextLimits;
|
|
110
|
+
declare class ExecutionContextInternal {
|
|
111
|
+
private readonly parent?;
|
|
112
|
+
constructor(parent?: (Pick<Context, "model" | "tools" | "limits"> & {
|
|
67
113
|
messageQueue?: MessageQueue;
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
114
|
+
}) | undefined);
|
|
115
|
+
readonly messageQueue: MessageQueue;
|
|
116
|
+
readonly events: EventEmitter<ContextEventMap>;
|
|
71
117
|
get model(): ChatModel | undefined;
|
|
72
118
|
get tools(): Agent<Message, Message>[] | undefined;
|
|
119
|
+
get limits(): ContextLimits | undefined;
|
|
73
120
|
usage: ContextUsage;
|
|
74
|
-
limits?: ContextLimits | undefined;
|
|
75
121
|
private abortController;
|
|
76
122
|
private timer?;
|
|
77
123
|
private initTimeout;
|
|
78
124
|
get status(): "normal" | "timeout";
|
|
79
|
-
call<I extends Message, O extends Message>(agent: Runnable<I, O
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}): Promise<[O, Runnable]>;
|
|
84
|
-
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message?: I | string, options?: {
|
|
85
|
-
returnActiveAgent?: boolean;
|
|
86
|
-
}): UserAgent<I, O> | Promise<O | [O, Runnable]>;
|
|
87
|
-
publish(topic: string | string[], message: Message | string, from?: Agent): void;
|
|
88
|
-
subscribe(topic: string, listener?: undefined): Promise<MessagePayload>;
|
|
89
|
-
subscribe(topic: string, listener: MessageQueueListener): Unsubscribe;
|
|
90
|
-
unsubscribe(topic: string, listener: MessageQueueListener): void;
|
|
91
|
-
emit(eventName: string | symbol, ...args: unknown[]): boolean;
|
|
125
|
+
call<I extends Message, O extends Message>(agent: Runnable<I, O>, input: I, context: Context, options?: CallOptions): Promise<{
|
|
126
|
+
agent: Runnable;
|
|
127
|
+
output: O;
|
|
128
|
+
}>;
|
|
92
129
|
private callAgent;
|
|
93
130
|
}
|
|
131
|
+
export {};
|
|
@@ -4,7 +4,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.ExecutionContext = void 0;
|
|
7
|
+
exports.createPublishMessage = createPublishMessage;
|
|
7
8
|
const node_events_1 = __importDefault(require("node:events"));
|
|
9
|
+
const uuid_1 = require("uuid");
|
|
8
10
|
const zod_1 = require("zod");
|
|
9
11
|
const agent_js_1 = require("../agents/agent.js");
|
|
10
12
|
const types_js_1 = require("../agents/types.js");
|
|
@@ -12,129 +14,174 @@ const user_agent_js_1 = require("../agents/user-agent.js");
|
|
|
12
14
|
const prompt_builder_js_1 = require("../prompt/prompt-builder.js");
|
|
13
15
|
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
14
16
|
const message_queue_js_1 = require("./message-queue.js");
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
const usage_js_1 = require("./usage.js");
|
|
18
|
+
function createPublishMessage(message, from) {
|
|
19
|
+
return {
|
|
20
|
+
role: !from || from instanceof user_agent_js_1.UserAgent ? "user" : "agent",
|
|
21
|
+
source: from?.name,
|
|
22
|
+
message: (0, prompt_builder_js_1.createMessage)(message),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
class ExecutionContext {
|
|
26
|
+
constructor(parent) {
|
|
27
|
+
if (parent instanceof ExecutionContext) {
|
|
28
|
+
this.parentId = parent.id;
|
|
29
|
+
this.internal = parent.internal;
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
this.internal = new ExecutionContextInternal(parent);
|
|
33
|
+
}
|
|
22
34
|
}
|
|
23
|
-
|
|
35
|
+
parentId;
|
|
36
|
+
id = (0, uuid_1.v7)();
|
|
37
|
+
internal;
|
|
24
38
|
get model() {
|
|
25
|
-
return this.
|
|
39
|
+
return this.internal.model;
|
|
26
40
|
}
|
|
27
41
|
get tools() {
|
|
28
|
-
return this.
|
|
42
|
+
return this.internal.tools;
|
|
29
43
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
completionTokens: 0,
|
|
33
|
-
agentCalls: 0,
|
|
34
|
-
};
|
|
35
|
-
limits;
|
|
36
|
-
abortController = new AbortController();
|
|
37
|
-
timer;
|
|
38
|
-
initTimeout() {
|
|
39
|
-
if (this.timer)
|
|
40
|
-
return;
|
|
41
|
-
const timeout = this.limits?.timeout;
|
|
42
|
-
if (timeout) {
|
|
43
|
-
this.timer = setTimeout(() => {
|
|
44
|
-
this.abortController.abort();
|
|
45
|
-
}, timeout);
|
|
46
|
-
}
|
|
44
|
+
get limits() {
|
|
45
|
+
return this.internal.limits;
|
|
47
46
|
}
|
|
48
47
|
get status() {
|
|
49
|
-
return this.
|
|
48
|
+
return this.internal.status;
|
|
49
|
+
}
|
|
50
|
+
get usage() {
|
|
51
|
+
return this.internal.usage;
|
|
50
52
|
}
|
|
51
|
-
|
|
52
|
-
(
|
|
53
|
+
newContext({ reset } = {}) {
|
|
54
|
+
if (reset)
|
|
55
|
+
return new ExecutionContext(this.internal);
|
|
56
|
+
return new ExecutionContext(this);
|
|
57
|
+
}
|
|
58
|
+
call = ((agent, message, options) => {
|
|
59
|
+
(0, type_utils_js_1.checkArguments)("ExecutionContext.call", executionContextCallArgsSchema, {
|
|
53
60
|
agent,
|
|
54
61
|
message,
|
|
55
62
|
options,
|
|
56
63
|
});
|
|
57
64
|
if ((0, type_utils_js_1.isNil)(message)) {
|
|
58
|
-
let activeAgent = agent;
|
|
59
65
|
return user_agent_js_1.UserAgent.from({
|
|
60
66
|
context: this,
|
|
61
|
-
|
|
62
|
-
const [output, agent] = await context.call(activeAgent, input, {
|
|
63
|
-
returnActiveAgent: true,
|
|
64
|
-
});
|
|
65
|
-
activeAgent = agent;
|
|
66
|
-
return output;
|
|
67
|
-
},
|
|
67
|
+
activeAgent: agent,
|
|
68
68
|
});
|
|
69
69
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
if (options?.returnActiveAgent) {
|
|
83
|
-
return [output, activeAgent];
|
|
70
|
+
const newContext = this.newContext();
|
|
71
|
+
const msg = (0, prompt_builder_js_1.createMessage)(message);
|
|
72
|
+
return newContext.internal
|
|
73
|
+
.call(agent, msg, newContext, options)
|
|
74
|
+
.then(async ({ output, agent: activeAgent }) => {
|
|
75
|
+
if (activeAgent instanceof agent_js_1.Agent) {
|
|
76
|
+
const publishTopics = typeof activeAgent.publishTopic === "function"
|
|
77
|
+
? await activeAgent.publishTopic(output)
|
|
78
|
+
: activeAgent.publishTopic;
|
|
79
|
+
if (publishTopics?.length) {
|
|
80
|
+
newContext.publish(publishTopics, createPublishMessage(output, activeAgent));
|
|
84
81
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
(0, type_utils_js_1.checkArguments)("ExecutionEngineContext.publish", executionEnginePublishArgsSchema, {
|
|
91
|
-
topic,
|
|
92
|
-
message,
|
|
93
|
-
from,
|
|
82
|
+
}
|
|
83
|
+
if (options?.returnActiveAgent) {
|
|
84
|
+
return [output, activeAgent];
|
|
85
|
+
}
|
|
86
|
+
return output;
|
|
94
87
|
});
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
88
|
+
});
|
|
89
|
+
publish = ((topic, payload) => {
|
|
90
|
+
return this.internal.messageQueue.publish(topic, { ...payload, context: this });
|
|
91
|
+
});
|
|
92
|
+
subscribe = ((...args) => {
|
|
93
|
+
return this.internal.messageQueue.subscribe(...args);
|
|
94
|
+
});
|
|
95
|
+
unsubscribe = ((...args) => {
|
|
96
|
+
return this.internal.messageQueue.unsubscribe(...args);
|
|
97
|
+
});
|
|
98
|
+
emit(eventName, ...args) {
|
|
99
|
+
const b = {
|
|
100
|
+
...args[0],
|
|
101
|
+
contextId: this.id,
|
|
102
|
+
parentContextId: this.parentId,
|
|
103
|
+
timestamp: Date.now(),
|
|
100
104
|
};
|
|
101
|
-
|
|
105
|
+
const newArgs = [b, ...args.slice(1)];
|
|
106
|
+
return this.internal.events.emit(eventName, ...newArgs);
|
|
102
107
|
}
|
|
103
|
-
|
|
104
|
-
|
|
108
|
+
on(eventName, listener) {
|
|
109
|
+
this.internal.events.on(eventName, listener);
|
|
110
|
+
return this;
|
|
105
111
|
}
|
|
106
|
-
|
|
107
|
-
this.
|
|
112
|
+
once(eventName, listener) {
|
|
113
|
+
this.internal.events.once(eventName, listener);
|
|
114
|
+
return this;
|
|
108
115
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
return super.emit(eventName, ...args);
|
|
116
|
+
off(eventName, listener) {
|
|
117
|
+
this.internal.events.off(eventName, listener);
|
|
118
|
+
return this;
|
|
113
119
|
}
|
|
114
|
-
|
|
120
|
+
}
|
|
121
|
+
exports.ExecutionContext = ExecutionContext;
|
|
122
|
+
class ExecutionContextInternal {
|
|
123
|
+
parent;
|
|
124
|
+
constructor(parent) {
|
|
125
|
+
this.parent = parent;
|
|
126
|
+
this.messageQueue = this.parent?.messageQueue ?? new message_queue_js_1.MessageQueue();
|
|
127
|
+
}
|
|
128
|
+
messageQueue;
|
|
129
|
+
events = new node_events_1.default();
|
|
130
|
+
get model() {
|
|
131
|
+
return this.parent?.model;
|
|
132
|
+
}
|
|
133
|
+
get tools() {
|
|
134
|
+
return this.parent?.tools;
|
|
135
|
+
}
|
|
136
|
+
get limits() {
|
|
137
|
+
return this.parent?.limits;
|
|
138
|
+
}
|
|
139
|
+
usage = (0, usage_js_1.newEmptyContextUsage)();
|
|
140
|
+
abortController = new AbortController();
|
|
141
|
+
timer;
|
|
142
|
+
initTimeout() {
|
|
143
|
+
if (this.timer)
|
|
144
|
+
return;
|
|
145
|
+
const timeout = this.limits?.timeout;
|
|
146
|
+
if (timeout) {
|
|
147
|
+
this.timer = setTimeout(() => {
|
|
148
|
+
this.abortController.abort();
|
|
149
|
+
}, timeout);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
get status() {
|
|
153
|
+
return this.abortController.signal.aborted ? "timeout" : "normal";
|
|
154
|
+
}
|
|
155
|
+
async call(agent, input, context, options) {
|
|
156
|
+
this.initTimeout();
|
|
157
|
+
return withAbortSignal(this.abortController.signal, new Error("ExecutionContext is timeout"), () => this.callAgent(agent, input, context, options));
|
|
158
|
+
}
|
|
159
|
+
async callAgent(agent, input, context, options) {
|
|
115
160
|
let activeAgent = agent;
|
|
116
161
|
let output;
|
|
117
162
|
for (;;) {
|
|
118
163
|
let result;
|
|
119
164
|
if (typeof activeAgent === "function") {
|
|
120
|
-
result = await activeAgent(input,
|
|
165
|
+
result = await activeAgent(input, context);
|
|
121
166
|
}
|
|
122
167
|
else {
|
|
123
|
-
result = await activeAgent.call(input,
|
|
168
|
+
result = await activeAgent.call(input, context);
|
|
169
|
+
}
|
|
170
|
+
if (result instanceof agent_js_1.Agent) {
|
|
171
|
+
activeAgent = result;
|
|
172
|
+
continue;
|
|
124
173
|
}
|
|
125
|
-
if (!
|
|
126
|
-
|
|
127
|
-
const transferToAgent = result instanceof agent_js_1.Agent
|
|
128
|
-
? result
|
|
129
|
-
: (0, types_js_1.isTransferAgentOutput)(result)
|
|
174
|
+
if (!options?.disableTransfer) {
|
|
175
|
+
const transferToAgent = (0, types_js_1.isTransferAgentOutput)(result)
|
|
130
176
|
? result[types_js_1.transferAgentOutputKey].agent
|
|
131
177
|
: undefined;
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
break;
|
|
178
|
+
if (transferToAgent) {
|
|
179
|
+
activeAgent = transferToAgent;
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
137
182
|
}
|
|
183
|
+
output = result;
|
|
184
|
+
break;
|
|
138
185
|
}
|
|
139
186
|
if (!output)
|
|
140
187
|
throw new Error("Unexpected empty output");
|
|
@@ -144,7 +191,6 @@ class ExecutionContext extends node_events_1.default {
|
|
|
144
191
|
};
|
|
145
192
|
}
|
|
146
193
|
}
|
|
147
|
-
exports.ExecutionContext = ExecutionContext;
|
|
148
194
|
function withAbortSignal(signal, error, fn) {
|
|
149
195
|
return new Promise((resolve, reject) => {
|
|
150
196
|
const listener = () => reject(error);
|
|
@@ -156,15 +202,7 @@ function withAbortSignal(signal, error, fn) {
|
|
|
156
202
|
});
|
|
157
203
|
});
|
|
158
204
|
}
|
|
159
|
-
|
|
160
|
-
return typeof message === "string" ? (0, prompt_builder_js_1.createMessage)(message) : message;
|
|
161
|
-
}
|
|
162
|
-
const executionEnginePublishArgsSchema = zod_1.z.object({
|
|
163
|
-
topic: zod_1.z.union([zod_1.z.string(), zod_1.z.array(zod_1.z.string())]),
|
|
164
|
-
message: zod_1.z.union([zod_1.z.string(), zod_1.z.record(zod_1.z.unknown())]),
|
|
165
|
-
from: zod_1.z.instanceof(agent_js_1.Agent).optional(),
|
|
166
|
-
});
|
|
167
|
-
const executionEngineCallArgsSchema = zod_1.z.object({
|
|
205
|
+
const executionContextCallArgsSchema = zod_1.z.object({
|
|
168
206
|
agent: zod_1.z.union([zod_1.z.function(), zod_1.z.instanceof(agent_js_1.Agent)]),
|
|
169
207
|
message: zod_1.z.union([zod_1.z.record(zod_1.z.unknown()), zod_1.z.string()]).optional(),
|
|
170
208
|
options: zod_1.z.object({ returnActiveAgent: zod_1.z.boolean().optional() }).optional(),
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { Agent, type Message } from "../agents/agent.js";
|
|
3
|
-
import type { UserAgent } from "../agents/user-agent.js";
|
|
1
|
+
import { Agent } from "../agents/agent.js";
|
|
4
2
|
import { ChatModel } from "../models/chat-model.js";
|
|
5
|
-
import { type
|
|
6
|
-
import {
|
|
3
|
+
import { type Context, ExecutionContext } from "./context.js";
|
|
4
|
+
import { MessageQueue } from "./message-queue.js";
|
|
5
|
+
import type { ContextLimits } from "./usage.js";
|
|
7
6
|
export interface ExecutionEngineOptions {
|
|
8
7
|
name?: string;
|
|
9
8
|
description?: string;
|
|
@@ -12,7 +11,10 @@ export interface ExecutionEngineOptions {
|
|
|
12
11
|
agents?: Agent[];
|
|
13
12
|
limits?: ContextLimits;
|
|
14
13
|
}
|
|
15
|
-
export
|
|
14
|
+
export interface ExecutionEngineRunOptions {
|
|
15
|
+
returnActiveAgent?: boolean;
|
|
16
|
+
}
|
|
17
|
+
export declare class ExecutionEngine {
|
|
16
18
|
static load({ path, ...options }: {
|
|
17
19
|
path: string;
|
|
18
20
|
} & ExecutionEngineOptions): Promise<ExecutionEngine>;
|
|
@@ -21,52 +23,19 @@ export declare class ExecutionEngine extends EventEmitter {
|
|
|
21
23
|
description?: string;
|
|
22
24
|
readonly messageQueue: MessageQueue;
|
|
23
25
|
model?: ChatModel;
|
|
24
|
-
readonly tools: Agent<Message, Message>[] & {
|
|
25
|
-
[key: string]: Agent<Message, Message>;
|
|
26
|
+
readonly tools: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>[] & {
|
|
27
|
+
[key: string]: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>;
|
|
26
28
|
};
|
|
27
|
-
readonly agents: Agent<Message, Message>[] & {
|
|
28
|
-
[key: string]: Agent<Message, Message>;
|
|
29
|
+
readonly agents: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>[] & {
|
|
30
|
+
[key: string]: Agent<import("../agents/agent.js").Message, import("../agents/agent.js").Message>;
|
|
29
31
|
};
|
|
30
32
|
limits?: ContextLimits;
|
|
31
33
|
addAgent(...agents: Agent[]): void;
|
|
32
34
|
newContext(): ExecutionContext;
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
* @param from the agent who publish the message, if not provided, it will be treated as a user message
|
|
38
|
-
*/
|
|
39
|
-
publish(topic: string | string[], message: Message | string, from?: Agent): void;
|
|
40
|
-
/**
|
|
41
|
-
* Create a user agent to consistently call an agent
|
|
42
|
-
* @param agent Agent to call
|
|
43
|
-
* @returns User agent
|
|
44
|
-
*/
|
|
45
|
-
call<I extends Message, O extends Message>(agent: Runnable<I, O>): UserAgent<I, O>;
|
|
46
|
-
/**
|
|
47
|
-
* Call an agent with a message
|
|
48
|
-
* @param agent Agent to call
|
|
49
|
-
* @param message Message to pass to the agent
|
|
50
|
-
* @returns the output of the agent
|
|
51
|
-
*/
|
|
52
|
-
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string): Promise<O>;
|
|
53
|
-
/**
|
|
54
|
-
* Call an agent with a message and return the output and the active agent
|
|
55
|
-
* @param agent Agent to call
|
|
56
|
-
* @param message Message to pass to the agent
|
|
57
|
-
* @param options.returnActiveAgent return the active agent
|
|
58
|
-
* @returns the output of the agent and the final active agent
|
|
59
|
-
*/
|
|
60
|
-
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options: {
|
|
61
|
-
returnActiveAgent: true;
|
|
62
|
-
}): Promise<[O, Runnable]>;
|
|
63
|
-
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message?: I | string, options?: {
|
|
64
|
-
returnActiveAgent?: boolean;
|
|
65
|
-
}): UserAgent<I, O> | Promise<O | [O, Runnable]>;
|
|
66
|
-
subscribe(topic: string, listener?: undefined): Promise<MessagePayload>;
|
|
67
|
-
subscribe(topic: string, listener: MessageQueueListener): Unsubscribe;
|
|
68
|
-
subscribe(topic: string, listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
|
|
69
|
-
unsubscribe(topic: string, listener: MessageQueueListener): void;
|
|
35
|
+
publish: Context["publish"];
|
|
36
|
+
call: Context["call"];
|
|
37
|
+
subscribe: Context["subscribe"];
|
|
38
|
+
unsubscribe: Context["unsubscribe"];
|
|
70
39
|
shutdown(): Promise<void>;
|
|
71
40
|
private initProcessExitHandler;
|
|
72
41
|
}
|
|
@@ -1,10 +1,6 @@
|
|
|
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.ExecutionEngine = void 0;
|
|
7
|
-
const node_events_1 = __importDefault(require("node:events"));
|
|
8
4
|
const zod_1 = require("zod");
|
|
9
5
|
const agent_js_1 = require("../agents/agent.js");
|
|
10
6
|
const index_js_1 = require("../loader/index.js");
|
|
@@ -12,7 +8,7 @@ const chat_model_js_1 = require("../models/chat-model.js");
|
|
|
12
8
|
const type_utils_js_1 = require("../utils/type-utils.js");
|
|
13
9
|
const context_js_1 = require("./context.js");
|
|
14
10
|
const message_queue_js_1 = require("./message-queue.js");
|
|
15
|
-
class ExecutionEngine
|
|
11
|
+
class ExecutionEngine {
|
|
16
12
|
static async load({ path, ...options }) {
|
|
17
13
|
const { model, agents, tools, ...aigne } = await (0, index_js_1.load)({ path });
|
|
18
14
|
return new ExecutionEngine({
|
|
@@ -27,7 +23,6 @@ class ExecutionEngine extends node_events_1.default {
|
|
|
27
23
|
constructor(options) {
|
|
28
24
|
if (options)
|
|
29
25
|
(0, type_utils_js_1.checkArguments)("ExecutionEngine", executionEngineOptionsSchema, options);
|
|
30
|
-
super();
|
|
31
26
|
this.name = options?.name;
|
|
32
27
|
this.description = options?.description;
|
|
33
28
|
this.model = options?.model;
|
|
@@ -55,32 +50,18 @@ class ExecutionEngine extends node_events_1.default {
|
|
|
55
50
|
newContext() {
|
|
56
51
|
return new context_js_1.ExecutionContext(this);
|
|
57
52
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
return
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
subscribe(topic, listener) {
|
|
71
|
-
(0, type_utils_js_1.checkArguments)("ExecutionEngine.subscribe", executionEngineSubscribeArgsSchema, {
|
|
72
|
-
topic,
|
|
73
|
-
listener,
|
|
74
|
-
});
|
|
75
|
-
return this.messageQueue.subscribe(topic, listener);
|
|
76
|
-
}
|
|
77
|
-
unsubscribe(topic, listener) {
|
|
78
|
-
(0, type_utils_js_1.checkArguments)("ExecutionEngine.unsubscribe", executionEngineUnsubscribeArgsSchema, {
|
|
79
|
-
topic,
|
|
80
|
-
listener,
|
|
81
|
-
});
|
|
82
|
-
this.messageQueue.unsubscribe(topic, listener);
|
|
83
|
-
}
|
|
53
|
+
publish = ((...args) => {
|
|
54
|
+
return new context_js_1.ExecutionContext(this).publish(...args);
|
|
55
|
+
});
|
|
56
|
+
call = ((...args) => {
|
|
57
|
+
return new context_js_1.ExecutionContext(this).call(...args);
|
|
58
|
+
});
|
|
59
|
+
subscribe = ((...args) => {
|
|
60
|
+
return this.messageQueue.subscribe(...args);
|
|
61
|
+
});
|
|
62
|
+
unsubscribe = ((...args) => {
|
|
63
|
+
this.messageQueue.unsubscribe(...args);
|
|
64
|
+
});
|
|
84
65
|
async shutdown() {
|
|
85
66
|
for (const tool of this.tools) {
|
|
86
67
|
await tool.shutdown();
|
|
@@ -102,11 +83,3 @@ const executionEngineOptionsSchema = zod_1.z.object({
|
|
|
102
83
|
agents: zod_1.z.array(zod_1.z.instanceof(agent_js_1.Agent)).optional(),
|
|
103
84
|
});
|
|
104
85
|
const executionEngineAddAgentArgsSchema = zod_1.z.array(zod_1.z.instanceof(agent_js_1.Agent));
|
|
105
|
-
const executionEngineSubscribeArgsSchema = zod_1.z.object({
|
|
106
|
-
topic: zod_1.z.string(),
|
|
107
|
-
listener: zod_1.z.function(zod_1.z.tuple([zod_1.z.any()]), zod_1.z.any()).optional(),
|
|
108
|
-
});
|
|
109
|
-
const executionEngineUnsubscribeArgsSchema = zod_1.z.object({
|
|
110
|
-
topic: zod_1.z.string(),
|
|
111
|
-
listener: zod_1.z.function(zod_1.z.tuple([zod_1.z.any()]), zod_1.z.any()),
|
|
112
|
-
});
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { EventEmitter } from "node:events";
|
|
1
2
|
import type { Message } from "../agents/agent.js";
|
|
2
3
|
import type { Context } from "./context.js";
|
|
3
4
|
export declare const UserInputTopic = "UserInputTopic";
|
|
@@ -9,11 +10,10 @@ export interface MessagePayload {
|
|
|
9
10
|
context: Context;
|
|
10
11
|
}
|
|
11
12
|
export type MessageQueueListener = (message: MessagePayload) => void;
|
|
12
|
-
export type MessageRequest = MessagePayload;
|
|
13
13
|
export type Unsubscribe = () => void;
|
|
14
14
|
export declare class MessageQueue {
|
|
15
|
-
|
|
16
|
-
publish(topic: string | string[],
|
|
15
|
+
events: EventEmitter<[never]>;
|
|
16
|
+
publish(topic: string | string[], payload: MessagePayload): void;
|
|
17
17
|
error(error: Error): void;
|
|
18
18
|
subscribe(topic: string, listener?: undefined): Promise<MessagePayload>;
|
|
19
19
|
subscribe(topic: string, listener: MessageQueueListener): Unsubscribe;
|