-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
54 lines (45 loc) · 1.38 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Agent {
constructor() {
this.func = null; // self exec function
this.next = null; // next agent
}
setFunc(asyncFunction) {
this.func = asyncFunction;
return this; // allow chain
}
setNext(nextAgent) {
this.next = nextAgent;
return this; // allow chain
}
async execute(input) {
if (!this.func) {
throw new Error("Agent function is not defined.");
}
const result = await this.func(input);
if (this.next) {
return this.next.execute(result); // pass the result to the next Agent
}
return result; // if there are no next Agent, just return the result
}
}
// 定义异步函数
const asyncFunc1 = (content) =>
new Promise((resolve) => {
setTimeout(() => {
resolve(content + " from asyncFunc1");
}, 1000);
});
const asyncFunc2 = (content) =>
new Promise((resolve) => {
setTimeout(() => {
resolve(content + " from asyncFunc2");
}, 1000);
});
// 实例化Agent并设置其函数和下一个Agent
const agentA = new Agent().setFunc(asyncFunc1);
const agentB = new Agent().setFunc(asyncFunc2);
agentA.setNext(agentB); // 设置agentA的下一个执行者为agentB
// 启动执行链
agentA.execute("Initial content").then((result) => {
console.log(result); // 输出最终结果
});