@aidejs/core 0.1.0-alpha.0 → 0.1.0-alpha.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.
Files changed (3) hide show
  1. package/README.md +7 -7
  2. package/package.json +36 -26
  3. package/src/index.js +42 -11
package/README.md CHANGED
@@ -1,14 +1,8 @@
1
- <img src="./docs/images/logo.png" />
1
+ <img src="https://NN-Studio.github.io/Aidejs/images/logo.png" />
2
2
 
3
3
  # [Aidejs](https://github.com/NN-Studio/Aidejs)
4
4
  一个用于构建人工智能代理的JavaScript框架,它提供了一组工具和库,使创建智能应用程序变得轻松和高效
5
5
 
6
- ## 功能列表
7
-
8
- 通过类似拼积木的方式,你可以轻松地组合各个模块完成个性化的 AI 应用开发。
9
-
10
- 下面是可用模块:
11
-
12
6
  <p>
13
7
  <a href="https://github.com/NN-Studio/Aidejs/issues">
14
8
  <img src="https://img.shields.io/github/issues/NN-Studio/Aidejs" alt="issue">
@@ -21,6 +15,12 @@
21
15
  </a>
22
16
  </p>
23
17
 
18
+ ## 功能列表
19
+
20
+ 通过类似拼积木的方式,你可以轻松地组合各个模块完成个性化的 AI 应用开发。
21
+
22
+ 下面是可用模块:
23
+
24
24
  <table>
25
25
  <thead>
26
26
  <tr>
package/package.json CHANGED
@@ -1,26 +1,36 @@
1
- {
2
- "name": "@aidejs/core",
3
- "version": "0.1.0-alpha.0",
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
- }
1
+ {
2
+ "name": "@aidejs/core",
3
+ "version": "0.1.0-alpha.1",
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/index.js CHANGED
@@ -22,6 +22,7 @@ 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
  // 安装插件,比如可用工具tool或技能skill等
@@ -71,7 +72,12 @@ module.exports = class Aidejs {
71
72
  }
72
73
  }, function (chunk) {
73
74
  try {
74
- for (let choice of chunk.choices) {
75
+
76
+ // 执行一次回复
77
+ let runChoice = (choiceIndex) => {
78
+ if (choiceIndex >= chunk.choices.length) return;
79
+
80
+ let choice = chunk.choices[choiceIndex];
75
81
 
76
82
  if (choice.finish_reason) {
77
83
  if (toolCalls) {
@@ -84,21 +90,46 @@ module.exports = class Aidejs {
84
90
  logback(choice.delta.content || "");
85
91
 
86
92
  if (choice.delta.tool_calls) {
87
- for (let tool_call of choice.delta.tool_calls) {
88
- let result = Aidejs.tool.invoke(tool_call.function.name, JSON.parse(tool_call.function.arguments));
89
- messages.push({
90
- role: "tool",
91
- tool_name: tool_call.function.name,
92
- content: typeof result === "string" ? result : (JSON.stringify(result) + "")
93
- });
94
- }
95
- toolCalls = true;
93
+
94
+ // 执行一次工具
95
+ let runTool = (toolIndex) => {
96
+ if (toolIndex >= choice.delta.tool_calls.length) {
97
+ toolCalls = true;
98
+ runChoice(choiceIndex + 1);
99
+ } else {
100
+ let tool_call = choice.delta.tool_calls[toolIndex];
101
+ let result = Aidejs.tool.invoke(tool_call.function.name, JSON.parse(tool_call.function.arguments));
102
+
103
+ let toolback = function (value) {
104
+ messages.push({
105
+ role: "tool",
106
+ tool_name: tool_call.function.name,
107
+ content: typeof value === "string" ? value : (JSON.stringify(value) + "")
108
+ });
109
+ runTool(toolIndex + 1);
110
+ };
111
+
112
+ if (typeof result === "object" && result.constructor === Promise) {
113
+ result.then(function (value) {
114
+ toolback(value);
115
+ }).catch(function (error) {
116
+ toolback(error + "");
117
+ });
118
+ } else {
119
+ toolback(result);
120
+ }
121
+ }
122
+ };
123
+ runTool(0);
96
124
  } else {
97
125
  if (messages[lastIndex].role !== "assistant") resetLastIndex();
98
126
  messages[lastIndex].content += choice.delta.content || "";
127
+ runChoice(choiceIndex + 1);
99
128
  }
100
129
  }
101
- }
130
+
131
+ };
132
+ runChoice(0);
102
133
  } catch (e) {
103
134
  console.error("处理智能体响应时发生错误:", e);
104
135
  }