@aigne/core 1.5.0 → 1.5.1-1
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/lib/cjs/agents/agent.d.ts +8 -4
- package/lib/cjs/agents/agent.js +69 -8
- package/lib/cjs/agents/ai-agent.d.ts +5 -5
- package/lib/cjs/agents/ai-agent.js +1 -3
- package/lib/cjs/agents/memory.d.ts +1 -1
- package/lib/cjs/agents/user-agent.d.ts +4 -3
- package/lib/cjs/agents/user-agent.js +5 -5
- package/lib/cjs/execution-engine/context.d.ts +58 -5
- package/lib/cjs/execution-engine/context.js +169 -0
- package/lib/cjs/execution-engine/execution-engine.d.ts +10 -13
- package/lib/cjs/execution-engine/execution-engine.js +7 -96
- package/lib/cjs/execution-engine/message-queue.d.ts +2 -0
- package/lib/cjs/execution-engine/utils.d.ts +2 -2
- package/lib/cjs/execution-engine/utils.js +11 -15
- package/lib/cjs/models/chat-model.d.ts +8 -0
- package/lib/cjs/models/chat-model.js +21 -0
- package/lib/cjs/models/claude-chat-model.js +24 -4
- package/lib/cjs/models/openai-chat-model.d.ts +2 -1
- package/lib/cjs/models/openai-chat-model.js +13 -1
- package/lib/cjs/models/xai-chat-model.d.ts +1 -2
- package/lib/cjs/models/xai-chat-model.js +0 -2
- package/lib/cjs/prompt/prompt-builder.js +3 -3
- package/lib/cjs/utils/json-schema.js +1 -2
- package/lib/cjs/utils/logger.d.ts +1 -1
- package/lib/cjs/utils/logger.js +1 -1
- package/lib/cjs/utils/model-utils.d.ts +3 -0
- package/lib/cjs/utils/model-utils.js +9 -0
- package/lib/cjs/utils/type-utils.d.ts +2 -3
- package/lib/cjs/utils/type-utils.js +16 -11
- package/lib/dts/agents/agent.d.ts +8 -4
- package/lib/dts/agents/ai-agent.d.ts +5 -5
- package/lib/dts/agents/memory.d.ts +1 -1
- package/lib/dts/agents/user-agent.d.ts +4 -3
- package/lib/dts/execution-engine/context.d.ts +58 -5
- package/lib/dts/execution-engine/execution-engine.d.ts +10 -13
- package/lib/dts/execution-engine/message-queue.d.ts +2 -0
- package/lib/dts/execution-engine/utils.d.ts +2 -2
- package/lib/dts/models/chat-model.d.ts +8 -0
- package/lib/dts/models/openai-chat-model.d.ts +2 -1
- package/lib/dts/models/xai-chat-model.d.ts +1 -2
- package/lib/dts/utils/logger.d.ts +1 -1
- package/lib/dts/utils/model-utils.d.ts +3 -0
- package/lib/dts/utils/type-utils.d.ts +2 -3
- package/lib/esm/agents/agent.d.ts +8 -4
- package/lib/esm/agents/agent.js +36 -8
- package/lib/esm/agents/ai-agent.d.ts +5 -5
- package/lib/esm/agents/ai-agent.js +1 -3
- package/lib/esm/agents/memory.d.ts +1 -1
- package/lib/esm/agents/user-agent.d.ts +4 -3
- package/lib/esm/agents/user-agent.js +5 -5
- package/lib/esm/execution-engine/context.d.ts +58 -5
- package/lib/esm/execution-engine/context.js +164 -1
- package/lib/esm/execution-engine/execution-engine.d.ts +10 -13
- package/lib/esm/execution-engine/execution-engine.js +7 -96
- package/lib/esm/execution-engine/message-queue.d.ts +2 -0
- package/lib/esm/execution-engine/utils.d.ts +2 -2
- package/lib/esm/execution-engine/utils.js +11 -15
- package/lib/esm/models/chat-model.d.ts +8 -0
- package/lib/esm/models/chat-model.js +21 -0
- package/lib/esm/models/claude-chat-model.js +24 -4
- package/lib/esm/models/openai-chat-model.d.ts +2 -1
- package/lib/esm/models/openai-chat-model.js +13 -1
- package/lib/esm/models/xai-chat-model.d.ts +1 -2
- package/lib/esm/models/xai-chat-model.js +0 -2
- package/lib/esm/prompt/prompt-builder.js +1 -1
- package/lib/esm/utils/json-schema.js +1 -2
- package/lib/esm/utils/logger.d.ts +1 -1
- package/lib/esm/utils/logger.js +1 -1
- package/lib/esm/utils/model-utils.d.ts +3 -0
- package/lib/esm/utils/model-utils.js +6 -0
- package/lib/esm/utils/type-utils.d.ts +2 -3
- package/lib/esm/utils/type-utils.js +12 -8
- package/package.json +4 -5
|
@@ -44,11 +44,15 @@ export declare abstract class Agent<I extends Message = Message, O extends Messa
|
|
|
44
44
|
* - subscribe to memory topic if memory is enabled
|
|
45
45
|
* @param context Context to attach
|
|
46
46
|
*/
|
|
47
|
-
attach(context: Context): void;
|
|
47
|
+
attach(context: Pick<Context, "subscribe">): void;
|
|
48
48
|
addTool<I extends Message, O extends Message>(tool: Agent<I, O> | FunctionAgentFn<I, O>): void;
|
|
49
49
|
get isCallable(): boolean;
|
|
50
|
+
private checkContextStatus;
|
|
51
|
+
private newDefaultContext;
|
|
50
52
|
call(input: I | string, context?: Context): Promise<O>;
|
|
51
|
-
|
|
53
|
+
protected preprocess(_: I, context: Context): void;
|
|
54
|
+
protected postprocess(input: I, output: O, context: Context): void;
|
|
55
|
+
abstract process(input: I, context: Context): Promise<O | TransferAgentOutput>;
|
|
52
56
|
shutdown(): Promise<void>;
|
|
53
57
|
}
|
|
54
58
|
export type AgentInputOutputSchema<I extends Message = Message> = ZodType<I> | ((agent: Agent) => ZodType<I>);
|
|
@@ -59,6 +63,6 @@ export declare class FunctionAgent<I extends Message = Message, O extends Messag
|
|
|
59
63
|
static from<I extends Message, O extends Message>(options: FunctionAgentOptions<I, O> | FunctionAgentFn<I, O>): FunctionAgent<I, O>;
|
|
60
64
|
constructor(options: FunctionAgentOptions<I, O>);
|
|
61
65
|
fn: FunctionAgentFn<I, O>;
|
|
62
|
-
process(input: I, context
|
|
66
|
+
process(input: I, context: Context): Promise<TransferAgentOutput | O>;
|
|
63
67
|
}
|
|
64
|
-
export type FunctionAgentFn<I extends Message = Message, O extends Message = Message> = (input: I, context
|
|
68
|
+
export type FunctionAgentFn<I extends Message = Message, O extends Message = Message> = (input: I, context: Context) => O | Promise<O> | Agent | Promise<Agent>;
|
package/lib/cjs/agents/agent.js
CHANGED
|
@@ -1,4 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
2
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
36
|
exports.FunctionAgent = exports.Agent = void 0;
|
|
4
37
|
const zod_1 = require("zod");
|
|
@@ -69,7 +102,7 @@ class Agent {
|
|
|
69
102
|
attach(context) {
|
|
70
103
|
this.memory?.attach(context);
|
|
71
104
|
for (const topic of (0, type_utils_js_1.orArrayToArray)(this.subscribeTopic).concat(this.topic)) {
|
|
72
|
-
context.subscribe(topic, async ({ message }) => {
|
|
105
|
+
context.subscribe(topic, async ({ message, context }) => {
|
|
73
106
|
try {
|
|
74
107
|
await context.call(this, message);
|
|
75
108
|
}
|
|
@@ -85,26 +118,54 @@ class Agent {
|
|
|
85
118
|
get isCallable() {
|
|
86
119
|
return !!this.process;
|
|
87
120
|
}
|
|
121
|
+
checkContextStatus(context) {
|
|
122
|
+
if (context) {
|
|
123
|
+
const { status } = context;
|
|
124
|
+
if (status === "timeout") {
|
|
125
|
+
throw new Error(`ExecutionEngine for agent ${this.name} has timed out`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
async newDefaultContext() {
|
|
130
|
+
return Promise.resolve().then(() => __importStar(require("../execution-engine/context.js"))).then((m) => new m.ExecutionContext());
|
|
131
|
+
}
|
|
88
132
|
async call(input, context) {
|
|
133
|
+
const ctx = context ?? (await this.newDefaultContext());
|
|
89
134
|
const _input = typeof input === "string" ? (0, prompt_builder_js_1.createMessage)(input) : input;
|
|
90
135
|
const parsedInput = this.inputSchema.parse(_input);
|
|
91
136
|
logger_js_1.logger.debug("Call agent %s start with input: %O", this.name, input);
|
|
92
|
-
|
|
137
|
+
this.preprocess(parsedInput, ctx);
|
|
138
|
+
this.checkContextStatus(ctx);
|
|
139
|
+
const result = this.process(parsedInput, ctx)
|
|
93
140
|
.then((output) => {
|
|
94
141
|
const parsedOutput = this.outputSchema.parse(output);
|
|
95
142
|
return this.includeInputInOutput ? { ...parsedInput, ...parsedOutput } : parsedOutput;
|
|
96
143
|
})
|
|
97
144
|
.then((output) => {
|
|
98
|
-
this.
|
|
99
|
-
this.memory?.addMemory({
|
|
100
|
-
role: "agent",
|
|
101
|
-
content: (0, types_js_1.replaceTransferAgentToName)(output),
|
|
102
|
-
source: this.name,
|
|
103
|
-
});
|
|
145
|
+
this.postprocess(parsedInput, output, ctx);
|
|
104
146
|
return output;
|
|
105
147
|
});
|
|
106
148
|
return logger_js_1.logger.debug.spinner(result, `Call agent ${this.name}`, (output) => logger_js_1.logger.debug("Call agent %s succeed with output: %O", this.name, (0, types_js_1.replaceTransferAgentToName)(output)), { disabled: this.disableLogging });
|
|
107
149
|
}
|
|
150
|
+
preprocess(_, context) {
|
|
151
|
+
this.checkContextStatus(context);
|
|
152
|
+
if (context) {
|
|
153
|
+
const { limits, usage } = context;
|
|
154
|
+
if (limits?.maxAgentCalls && usage.agentCalls >= limits.maxAgentCalls) {
|
|
155
|
+
throw new Error(`Exceeded max agent calls ${usage.agentCalls}/${limits.maxAgentCalls}`);
|
|
156
|
+
}
|
|
157
|
+
usage.agentCalls++;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
postprocess(input, output, context) {
|
|
161
|
+
this.checkContextStatus(context);
|
|
162
|
+
this.memory?.addMemory({ role: "user", content: input });
|
|
163
|
+
this.memory?.addMemory({
|
|
164
|
+
role: "agent",
|
|
165
|
+
content: (0, types_js_1.replaceTransferAgentToName)(output),
|
|
166
|
+
source: this.name,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
108
169
|
async shutdown() {
|
|
109
170
|
this.memory?.detach();
|
|
110
171
|
}
|
|
@@ -27,10 +27,10 @@ export declare const aiAgentOptionsSchema: z.ZodObject<{
|
|
|
27
27
|
disableLogging: z.ZodOptional<z.ZodBoolean>;
|
|
28
28
|
memory: z.ZodOptional<z.ZodUnion<[z.ZodBoolean, z.ZodAny, z.ZodAny]>>;
|
|
29
29
|
}, "strip", z.ZodTypeAny, {
|
|
30
|
+
description?: string | undefined;
|
|
30
31
|
tools?: (Agent<Message, Message> | ((...args: unknown[]) => unknown))[] | undefined;
|
|
31
|
-
toolChoice?: "auto" | "none" | "required" |
|
|
32
|
+
toolChoice?: Agent<Message, Message> | "auto" | "none" | "required" | "router" | undefined;
|
|
32
33
|
name?: string | undefined;
|
|
33
|
-
description?: string | undefined;
|
|
34
34
|
model?: ChatModel | undefined;
|
|
35
35
|
instructions?: string | PromptBuilder | undefined;
|
|
36
36
|
outputKey?: string | undefined;
|
|
@@ -42,10 +42,10 @@ export declare const aiAgentOptionsSchema: z.ZodObject<{
|
|
|
42
42
|
disableLogging?: boolean | undefined;
|
|
43
43
|
memory?: any;
|
|
44
44
|
}, {
|
|
45
|
+
description?: string | undefined;
|
|
45
46
|
tools?: (Agent<Message, Message> | ((...args: unknown[]) => unknown))[] | undefined;
|
|
46
|
-
toolChoice?: "auto" | "none" | "required" |
|
|
47
|
+
toolChoice?: Agent<Message, Message> | "auto" | "none" | "required" | "router" | undefined;
|
|
47
48
|
name?: string | undefined;
|
|
48
|
-
description?: string | undefined;
|
|
49
49
|
model?: ChatModel | undefined;
|
|
50
50
|
instructions?: string | PromptBuilder | undefined;
|
|
51
51
|
outputKey?: string | undefined;
|
|
@@ -64,5 +64,5 @@ export declare class AIAgent<I extends Message = Message, O extends Message = Me
|
|
|
64
64
|
instructions: PromptBuilder;
|
|
65
65
|
outputKey?: string;
|
|
66
66
|
toolChoice?: AIAgentToolChoice;
|
|
67
|
-
process(input: I, context
|
|
67
|
+
process(input: I, context: Context): Promise<import("./types.js").TransferAgentOutput | O>;
|
|
68
68
|
}
|
|
@@ -50,9 +50,7 @@ class AIAgent extends agent_js_1.Agent {
|
|
|
50
50
|
outputKey;
|
|
51
51
|
toolChoice;
|
|
52
52
|
async process(input, context) {
|
|
53
|
-
|
|
54
|
-
throw new Error("Context is required to run AIAgent");
|
|
55
|
-
const model = context?.model ?? this.model;
|
|
53
|
+
const model = context.model ?? this.model;
|
|
56
54
|
if (!model)
|
|
57
55
|
throw new Error("model is required to run AIAgent");
|
|
58
56
|
const { toolAgents, messages, ...modelInput } = await this.instructions.build({
|
|
@@ -3,16 +3,17 @@ import type { MessagePayload, MessageQueueListener, Unsubscribe } from "../execu
|
|
|
3
3
|
import { type PromiseOrValue } from "../utils/type-utils.js";
|
|
4
4
|
import { Agent, type AgentOptions, type Message } from "./agent.js";
|
|
5
5
|
export interface UserAgentOptions<I extends Message = Message, O extends Message = Message> extends AgentOptions<I, O> {
|
|
6
|
-
context
|
|
6
|
+
context: Context;
|
|
7
7
|
process?: (input: I, context: Context) => PromiseOrValue<O>;
|
|
8
8
|
}
|
|
9
9
|
export declare class UserAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
|
|
10
10
|
static from<I extends Message, O extends Message>(options: UserAgentOptions<I, O>): UserAgent<I, O>;
|
|
11
11
|
constructor(options: UserAgentOptions<I, O>);
|
|
12
|
-
|
|
12
|
+
readonly context: Context;
|
|
13
13
|
private get ctx();
|
|
14
14
|
private _process?;
|
|
15
|
-
|
|
15
|
+
call(input: string | I, context?: Context): Promise<O>;
|
|
16
|
+
process(input: I, context: Context): Promise<O>;
|
|
16
17
|
publish(topic: string | string[], message: Message | string): void;
|
|
17
18
|
subscribe(topic: string, listener?: undefined): Promise<MessagePayload>;
|
|
18
19
|
subscribe(topic: string, listener: MessageQueueListener): Unsubscribe;
|
|
@@ -19,16 +19,16 @@ class UserAgent extends agent_js_1.Agent {
|
|
|
19
19
|
return this.context;
|
|
20
20
|
}
|
|
21
21
|
_process;
|
|
22
|
+
call(input, context) {
|
|
23
|
+
return super.call(input, context ?? this.context);
|
|
24
|
+
}
|
|
22
25
|
async process(input, context) {
|
|
23
|
-
const ctx = context ?? this.context;
|
|
24
|
-
if (!ctx)
|
|
25
|
-
throw new Error("UserAgent must have a context");
|
|
26
26
|
if (this._process) {
|
|
27
|
-
return this._process(input,
|
|
27
|
+
return this._process(input, context);
|
|
28
28
|
}
|
|
29
29
|
const publicTopic = typeof this.publishTopic === "function" ? await this.publishTopic(input) : this.publishTopic;
|
|
30
30
|
if (publicTopic?.length) {
|
|
31
|
-
|
|
31
|
+
context.publish(publicTopic, input, this);
|
|
32
32
|
return {};
|
|
33
33
|
}
|
|
34
34
|
throw new Error("UserAgent must have a process function or a publishTopic");
|
|
@@ -1,11 +1,31 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import EventEmitter from "node:events";
|
|
2
|
+
import { Agent, type FunctionAgentFn, type Message } from "../agents/agent.js";
|
|
3
|
+
import { UserAgent } from "../agents/user-agent.js";
|
|
3
4
|
import type { ChatModel } from "../models/chat-model.js";
|
|
4
|
-
import type
|
|
5
|
+
import { type MessagePayload, MessageQueue, type MessageQueueListener, type Unsubscribe } from "./message-queue.js";
|
|
5
6
|
export type Runnable<I extends Message = Message, O extends Message = Message> = Agent<I, O> | FunctionAgentFn;
|
|
7
|
+
export interface ContextUsage {
|
|
8
|
+
promptTokens: number;
|
|
9
|
+
completionTokens: number;
|
|
10
|
+
agentCalls: number;
|
|
11
|
+
}
|
|
12
|
+
export interface ContextLimits {
|
|
13
|
+
maxTokens?: number;
|
|
14
|
+
maxAgentCalls?: number;
|
|
15
|
+
timeout?: number;
|
|
16
|
+
}
|
|
6
17
|
export interface Context extends EventEmitter {
|
|
7
18
|
model?: ChatModel;
|
|
8
19
|
tools?: Agent[];
|
|
20
|
+
usage: ContextUsage;
|
|
21
|
+
limits?: ContextLimits;
|
|
22
|
+
status?: "normal" | "timeout";
|
|
23
|
+
/**
|
|
24
|
+
* Create a user agent to consistently call an agent
|
|
25
|
+
* @param agent Agent to call
|
|
26
|
+
* @returns User agent
|
|
27
|
+
*/
|
|
28
|
+
call<I extends Message, O extends Message>(agent: Runnable<I, O>): UserAgent<I, O>;
|
|
9
29
|
/**
|
|
10
30
|
* Call an agent with a message
|
|
11
31
|
* @param agent Agent to call
|
|
@@ -21,11 +41,11 @@ export interface Context extends EventEmitter {
|
|
|
21
41
|
* @returns the output of the agent and the final active agent
|
|
22
42
|
*/
|
|
23
43
|
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options: {
|
|
24
|
-
returnActiveAgent
|
|
44
|
+
returnActiveAgent: true;
|
|
25
45
|
}): Promise<[O, Runnable]>;
|
|
26
46
|
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options?: {
|
|
27
47
|
returnActiveAgent?: boolean;
|
|
28
|
-
}): Promise<O | [O, Runnable]>;
|
|
48
|
+
}): UserAgent<I, O> | Promise<O | [O, Runnable]>;
|
|
29
49
|
/**
|
|
30
50
|
* Publish a message to a topic, the engine will call the listeners of the topic
|
|
31
51
|
* @param topic topic name, or an array of topic names
|
|
@@ -38,3 +58,36 @@ export interface Context extends EventEmitter {
|
|
|
38
58
|
subscribe(topic: string, listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
|
|
39
59
|
unsubscribe(topic: string, listener: MessageQueueListener): void;
|
|
40
60
|
}
|
|
61
|
+
export declare class ExecutionContext extends EventEmitter implements Context {
|
|
62
|
+
private readonly engine?;
|
|
63
|
+
constructor(engine?: {
|
|
64
|
+
model?: ChatModel;
|
|
65
|
+
tools?: Agent[];
|
|
66
|
+
limits?: ContextLimits;
|
|
67
|
+
messageQueue?: MessageQueue;
|
|
68
|
+
emit?: EventEmitter["emit"];
|
|
69
|
+
} | undefined);
|
|
70
|
+
private messageQueue;
|
|
71
|
+
get model(): ChatModel | undefined;
|
|
72
|
+
get tools(): Agent<Message, Message>[] | undefined;
|
|
73
|
+
usage: ContextUsage;
|
|
74
|
+
limits?: ContextLimits | undefined;
|
|
75
|
+
private abortController;
|
|
76
|
+
private timer?;
|
|
77
|
+
private initTimeout;
|
|
78
|
+
get status(): "normal" | "timeout";
|
|
79
|
+
call<I extends Message, O extends Message>(agent: Runnable<I, O>): UserAgent<I, O>;
|
|
80
|
+
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string): Promise<O>;
|
|
81
|
+
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options: {
|
|
82
|
+
returnActiveAgent: true;
|
|
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;
|
|
92
|
+
private callAgent;
|
|
93
|
+
}
|
|
@@ -1,2 +1,171 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ExecutionContext = void 0;
|
|
7
|
+
const node_events_1 = __importDefault(require("node:events"));
|
|
8
|
+
const zod_1 = require("zod");
|
|
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 type_utils_js_1 = require("../utils/type-utils.js");
|
|
14
|
+
const message_queue_js_1 = require("./message-queue.js");
|
|
15
|
+
class ExecutionContext extends node_events_1.default {
|
|
16
|
+
engine;
|
|
17
|
+
constructor(engine) {
|
|
18
|
+
super();
|
|
19
|
+
this.engine = engine;
|
|
20
|
+
this.limits = engine?.limits;
|
|
21
|
+
this.messageQueue = engine?.messageQueue ?? new message_queue_js_1.MessageQueue();
|
|
22
|
+
}
|
|
23
|
+
messageQueue;
|
|
24
|
+
get model() {
|
|
25
|
+
return this.engine?.model;
|
|
26
|
+
}
|
|
27
|
+
get tools() {
|
|
28
|
+
return this.engine?.tools;
|
|
29
|
+
}
|
|
30
|
+
usage = {
|
|
31
|
+
promptTokens: 0,
|
|
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
|
+
}
|
|
47
|
+
}
|
|
48
|
+
get status() {
|
|
49
|
+
return this.abortController.signal.aborted ? "timeout" : "normal";
|
|
50
|
+
}
|
|
51
|
+
call(agent, message, options) {
|
|
52
|
+
(0, type_utils_js_1.checkArguments)("ExecutionEngine._call", executionEngineCallArgsSchema, {
|
|
53
|
+
agent,
|
|
54
|
+
message,
|
|
55
|
+
options,
|
|
56
|
+
});
|
|
57
|
+
if ((0, type_utils_js_1.isNil)(message)) {
|
|
58
|
+
let activeAgent = agent;
|
|
59
|
+
return user_agent_js_1.UserAgent.from({
|
|
60
|
+
context: this,
|
|
61
|
+
process: async (input, context) => {
|
|
62
|
+
const [output, agent] = await context.call(activeAgent, input, {
|
|
63
|
+
returnActiveAgent: true,
|
|
64
|
+
});
|
|
65
|
+
activeAgent = agent;
|
|
66
|
+
return output;
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return withAbortSignal(this.abortController.signal, new Error("ExecutionEngine is timeout"), async () => {
|
|
71
|
+
this.initTimeout();
|
|
72
|
+
const msg = createMessageFromInput(message);
|
|
73
|
+
return this.callAgent(agent, msg).then(async ({ output, agent: activeAgent }) => {
|
|
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
|
+
if (options?.returnActiveAgent) {
|
|
83
|
+
return [output, activeAgent];
|
|
84
|
+
}
|
|
85
|
+
return output;
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
publish(topic, message, from) {
|
|
90
|
+
(0, type_utils_js_1.checkArguments)("ExecutionEngineContext.publish", executionEnginePublishArgsSchema, {
|
|
91
|
+
topic,
|
|
92
|
+
message,
|
|
93
|
+
from,
|
|
94
|
+
});
|
|
95
|
+
const request = {
|
|
96
|
+
role: !from || from instanceof user_agent_js_1.UserAgent ? "user" : "agent",
|
|
97
|
+
source: from?.name,
|
|
98
|
+
message: createMessageFromInput(message),
|
|
99
|
+
context: this,
|
|
100
|
+
};
|
|
101
|
+
this.messageQueue.publish(topic, request);
|
|
102
|
+
}
|
|
103
|
+
subscribe(topic, listener) {
|
|
104
|
+
return this.messageQueue.subscribe(topic, listener);
|
|
105
|
+
}
|
|
106
|
+
unsubscribe(topic, listener) {
|
|
107
|
+
this.messageQueue.unsubscribe(topic, listener);
|
|
108
|
+
}
|
|
109
|
+
emit(eventName, ...args) {
|
|
110
|
+
if (this.engine?.emit)
|
|
111
|
+
return this.engine.emit(eventName, ...args);
|
|
112
|
+
return super.emit(eventName, ...args);
|
|
113
|
+
}
|
|
114
|
+
async callAgent(agent, input) {
|
|
115
|
+
let activeAgent = agent;
|
|
116
|
+
let output;
|
|
117
|
+
for (;;) {
|
|
118
|
+
let result;
|
|
119
|
+
if (typeof activeAgent === "function") {
|
|
120
|
+
result = await activeAgent(input, this);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
result = await activeAgent.call(input, this);
|
|
124
|
+
}
|
|
125
|
+
if (!(result instanceof agent_js_1.Agent))
|
|
126
|
+
output = result;
|
|
127
|
+
const transferToAgent = result instanceof agent_js_1.Agent
|
|
128
|
+
? result
|
|
129
|
+
: (0, types_js_1.isTransferAgentOutput)(result)
|
|
130
|
+
? result[types_js_1.transferAgentOutputKey].agent
|
|
131
|
+
: undefined;
|
|
132
|
+
if (transferToAgent) {
|
|
133
|
+
activeAgent = transferToAgent;
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (!output)
|
|
140
|
+
throw new Error("Unexpected empty output");
|
|
141
|
+
return {
|
|
142
|
+
agent: activeAgent,
|
|
143
|
+
output,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
exports.ExecutionContext = ExecutionContext;
|
|
148
|
+
function withAbortSignal(signal, error, fn) {
|
|
149
|
+
return new Promise((resolve, reject) => {
|
|
150
|
+
const listener = () => reject(error);
|
|
151
|
+
signal.addEventListener("abort", listener);
|
|
152
|
+
fn()
|
|
153
|
+
.then(resolve, reject)
|
|
154
|
+
.finally(() => {
|
|
155
|
+
signal.removeEventListener("abort", listener);
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
function createMessageFromInput(message) {
|
|
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({
|
|
168
|
+
agent: zod_1.z.union([zod_1.z.function(), zod_1.z.instanceof(agent_js_1.Agent)]),
|
|
169
|
+
message: zod_1.z.union([zod_1.z.record(zod_1.z.unknown()), zod_1.z.string()]).optional(),
|
|
170
|
+
options: zod_1.z.object({ returnActiveAgent: zod_1.z.boolean().optional() }).optional(),
|
|
171
|
+
});
|
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
import EventEmitter from "node:events";
|
|
2
2
|
import { Agent, type Message } from "../agents/agent.js";
|
|
3
|
-
import { UserAgent } from "../agents/user-agent.js";
|
|
3
|
+
import type { UserAgent } from "../agents/user-agent.js";
|
|
4
4
|
import { ChatModel } from "../models/chat-model.js";
|
|
5
|
-
import type
|
|
5
|
+
import { type ContextLimits, ExecutionContext, type Runnable } from "./context.js";
|
|
6
6
|
import { type MessagePayload, MessageQueue, type MessageQueueListener, type Unsubscribe } from "./message-queue.js";
|
|
7
7
|
export interface ExecutionEngineOptions {
|
|
8
8
|
model?: ChatModel;
|
|
9
9
|
tools?: Agent[];
|
|
10
10
|
agents?: Agent[];
|
|
11
|
+
limits?: ContextLimits;
|
|
11
12
|
}
|
|
12
|
-
export declare class ExecutionEngine extends EventEmitter
|
|
13
|
+
export declare class ExecutionEngine extends EventEmitter {
|
|
13
14
|
constructor(options?: ExecutionEngineOptions);
|
|
14
15
|
readonly messageQueue: MessageQueue;
|
|
15
16
|
model?: ChatModel;
|
|
16
17
|
tools: Agent[];
|
|
17
18
|
private agents;
|
|
19
|
+
limits?: ContextLimits;
|
|
18
20
|
addAgent(...agents: Agent[]): void;
|
|
19
|
-
|
|
21
|
+
newContext(): ExecutionContext;
|
|
20
22
|
/**
|
|
21
23
|
* Publish a message to a topic, the engine will call the listeners of the topic
|
|
22
24
|
* @param topic topic name, or an array of topic names
|
|
@@ -45,20 +47,15 @@ export declare class ExecutionEngine extends EventEmitter implements Context {
|
|
|
45
47
|
* @returns the output of the agent and the final active agent
|
|
46
48
|
*/
|
|
47
49
|
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message: I | string, options: {
|
|
48
|
-
returnActiveAgent
|
|
50
|
+
returnActiveAgent: true;
|
|
49
51
|
}): Promise<[O, Runnable]>;
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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;
|
|
52
|
+
call<I extends Message, O extends Message>(agent: Runnable<I, O>, message?: I | string, options?: {
|
|
53
|
+
returnActiveAgent?: boolean;
|
|
54
|
+
}): UserAgent<I, O> | Promise<O | [O, Runnable]>;
|
|
57
55
|
subscribe(topic: string, listener?: undefined): Promise<MessagePayload>;
|
|
58
56
|
subscribe(topic: string, listener: MessageQueueListener): Unsubscribe;
|
|
59
57
|
subscribe(topic: string, listener?: MessageQueueListener): Unsubscribe | Promise<MessagePayload>;
|
|
60
58
|
unsubscribe(topic: string, listener: MessageQueueListener): void;
|
|
61
|
-
private callAgent;
|
|
62
59
|
shutdown(): Promise<void>;
|
|
63
60
|
private initProcessExitHandler;
|
|
64
61
|
}
|