@aidejs/core 0.1.0-alpha.0 → 0.1.0-alpha.2
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/package.json +36 -26
- package/src/Tool.js +2 -2
- package/src/index.js +69 -30
- package/src/utils/OpenAI.js +4 -10
- package/types/index.d.ts +1 -1
package/package.json
CHANGED
|
@@ -1,26 +1,36 @@
|
|
|
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
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@aidejs/core",
|
|
3
|
+
"version": "0.1.0-alpha.2",
|
|
4
|
+
"description": "@aidejs/core",
|
|
5
|
+
"main": "./src/index.js",
|
|
6
|
+
"typings": "./types/index.d.ts",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"scripts": {},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/NN-Studio/Aidejs.git",
|
|
12
|
+
"directory": "packages/core"
|
|
13
|
+
},
|
|
14
|
+
"author": {
|
|
15
|
+
"name": "zxl20070701",
|
|
16
|
+
"url": "https://zxl20070701.github.io/notebook/home.html"
|
|
17
|
+
},
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/NN-Studio/Aidejs/issues"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://github.com/NN-Studio/Aidejs",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"oipage": "^2.0.0"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"AI",
|
|
28
|
+
"Agent",
|
|
29
|
+
"ChatGPT",
|
|
30
|
+
"Aidejs",
|
|
31
|
+
"OpenClaw",
|
|
32
|
+
"skills",
|
|
33
|
+
"MCP",
|
|
34
|
+
"core"
|
|
35
|
+
]
|
|
36
|
+
}
|
package/src/Tool.js
CHANGED
|
@@ -22,11 +22,11 @@ module.exports = class Tool {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
// 工具调用
|
|
25
|
-
invoke(name, args) {
|
|
25
|
+
invoke(aidejs, name, args) {
|
|
26
26
|
if (!this.tools[name]) {
|
|
27
27
|
throw new Error(`工具${name}不存在`);
|
|
28
28
|
}
|
|
29
|
-
return this.tools[name].valueOf(args);
|
|
29
|
+
return this.tools[name].valueOf.call(aidejs, args);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
};
|
package/src/index.js
CHANGED
|
@@ -22,16 +22,19 @@ module.exports = class Aidejs {
|
|
|
22
22
|
// 注册工具
|
|
23
23
|
static registerTool(name, value) {
|
|
24
24
|
Aidejs.tool.register(name, value);
|
|
25
|
+
return Aidejs;
|
|
25
26
|
}
|
|
26
27
|
|
|
27
|
-
//
|
|
28
|
-
static use(plugin) {
|
|
29
|
-
plugin.install(Aidejs);
|
|
28
|
+
// 安装插件
|
|
29
|
+
static use(plugin, options = {}) {
|
|
30
|
+
plugin.install(Aidejs, options);
|
|
30
31
|
return Aidejs;
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
// 执行任务
|
|
34
|
-
task(message, logback, inputback) {
|
|
35
|
+
task(message, logback, inputback, thinkback) {
|
|
36
|
+
let _this = this;
|
|
37
|
+
|
|
35
38
|
let messages = [], lastIndex = -1, toolCalls = false;
|
|
36
39
|
|
|
37
40
|
let resetLastIndex = () => {
|
|
@@ -71,37 +74,73 @@ module.exports = class Aidejs {
|
|
|
71
74
|
}
|
|
72
75
|
}, function (chunk) {
|
|
73
76
|
try {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
} else {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
77
|
+
|
|
78
|
+
// 执行一次回复
|
|
79
|
+
let runChoice = (choiceIndex) => {
|
|
80
|
+
if (choiceIndex >= chunk.choices.length) { return; }
|
|
81
|
+
|
|
82
|
+
let choice = chunk.choices[choiceIndex];
|
|
83
|
+
|
|
84
|
+
if (choice.delta.content) {
|
|
85
|
+
if (logback) logback(choice.delta.content);
|
|
86
|
+
} else if (choice.delta.reasoning) {
|
|
87
|
+
if (thinkback) thinkback(choice.delta.reasoning);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (choice.delta.tool_calls) {
|
|
91
|
+
|
|
92
|
+
// 执行一次工具
|
|
93
|
+
let runTool = (toolIndex) => {
|
|
94
|
+
if (toolIndex >= choice.delta.tool_calls.length) {
|
|
95
|
+
toolCalls = true;
|
|
96
|
+
runChoice(choiceIndex + 1);
|
|
97
|
+
} else {
|
|
98
|
+
let tool_call = choice.delta.tool_calls[toolIndex];
|
|
99
|
+
let result = Aidejs.tool.invoke(_this, tool_call.function.name, JSON.parse(tool_call.function.arguments));
|
|
100
|
+
|
|
101
|
+
let toolback = function (value) {
|
|
102
|
+
messages.push({
|
|
103
|
+
role: "tool",
|
|
104
|
+
tool_name: tool_call.function.name,
|
|
105
|
+
content: typeof value === "string" ? value : (JSON.stringify(value) + "")
|
|
106
|
+
});
|
|
107
|
+
runTool(toolIndex + 1);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
if (typeof result === "object" && result.constructor === Promise) {
|
|
111
|
+
result.then(function (value) {
|
|
112
|
+
toolback(value);
|
|
113
|
+
}).catch(function (error) {
|
|
114
|
+
toolback(error + "");
|
|
115
|
+
});
|
|
116
|
+
} else {
|
|
117
|
+
toolback(result);
|
|
118
|
+
}
|
|
94
119
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
120
|
+
};
|
|
121
|
+
runTool(0);
|
|
122
|
+
} else {
|
|
123
|
+
if (messages[lastIndex].role !== "assistant") resetLastIndex();
|
|
124
|
+
messages[lastIndex].content += choice.delta.content || "";
|
|
125
|
+
runChoice(choiceIndex + 1);
|
|
100
126
|
}
|
|
101
|
-
|
|
127
|
+
|
|
128
|
+
};
|
|
129
|
+
runChoice(0);
|
|
102
130
|
} catch (e) {
|
|
103
131
|
console.error("处理智能体响应时发生错误:", e);
|
|
104
132
|
}
|
|
133
|
+
}, function () {
|
|
134
|
+
if (toolCalls) {
|
|
135
|
+
toolCalls = false;
|
|
136
|
+
runChat();
|
|
137
|
+
} else {
|
|
138
|
+
runInputback();
|
|
139
|
+
}
|
|
140
|
+
}, function (e) {
|
|
141
|
+
console.error("对接LLM解答问题时发生错误:", e);
|
|
142
|
+
toolCalls = false;
|
|
143
|
+
runInputback();
|
|
105
144
|
});
|
|
106
145
|
|
|
107
146
|
resetLastIndex();
|
package/src/utils/OpenAI.js
CHANGED
|
@@ -16,19 +16,13 @@ module.exports = function (url, options = {}, databack, callback, errorback) {
|
|
|
16
16
|
method: "POST",
|
|
17
17
|
headers: options.header || {}
|
|
18
18
|
}, (res) => {
|
|
19
|
-
|
|
20
|
-
let data = "";
|
|
21
19
|
res.on('data', (chunk) => {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
// 有callback的情况下,data累积chunk,等end事件触发时一次性调用callback,适合非流式处理
|
|
26
|
-
if (callback) data += chunk;
|
|
27
|
-
|
|
28
|
-
if (databack) databack(toJSON(chunk));
|
|
20
|
+
// 这里data不累积,直接把chunk传给databack,流式处理
|
|
21
|
+
// 如何后续希望callback中返回数据,那么data累积chunk,等end事件触发时一次性调用callback,非流式处理
|
|
22
|
+
if (databack) databack(toJSON(chunk.toString().replace(/^data: /, "")));
|
|
29
23
|
});
|
|
30
24
|
res.on('end', () => {
|
|
31
|
-
if (callback) callback(
|
|
25
|
+
if (callback) callback();
|
|
32
26
|
});
|
|
33
27
|
});
|
|
34
28
|
|