@aidejs/core 0.1.0-alpha.1 → 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/README.md CHANGED
@@ -1,8 +1,14 @@
1
- <img src="https://NN-Studio.github.io/Aidejs/images/logo.png" />
1
+ <img src="./docs/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
+
6
12
  <p>
7
13
  <a href="https://github.com/NN-Studio/Aidejs/issues">
8
14
  <img src="https://img.shields.io/github/issues/NN-Studio/Aidejs" alt="issue">
@@ -15,12 +21,6 @@
15
21
  </a>
16
22
  </p>
17
23
 
18
- ## 功能列表
19
-
20
- 通过类似拼积木的方式,你可以轻松地组合各个模块完成个性化的 AI 应用开发。
21
-
22
- 下面是可用模块:
23
-
24
24
  <table>
25
25
  <thead>
26
26
  <tr>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aidejs/core",
3
- "version": "0.1.0-alpha.1",
3
+ "version": "0.1.0-alpha.2",
4
4
  "description": "@aidejs/core",
5
5
  "main": "./src/index.js",
6
6
  "typings": "./types/index.d.ts",
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
@@ -25,14 +25,16 @@ module.exports = class Aidejs {
25
25
  return Aidejs;
26
26
  }
27
27
 
28
- // 安装插件,比如可用工具tool或技能skill等
29
- static use(plugin) {
30
- plugin.install(Aidejs);
28
+ // 安装插件
29
+ static use(plugin, options = {}) {
30
+ plugin.install(Aidejs, options);
31
31
  return Aidejs;
32
32
  }
33
33
 
34
34
  // 执行任务
35
- task(message, logback, inputback) {
35
+ task(message, logback, inputback, thinkback) {
36
+ let _this = this;
37
+
36
38
  let messages = [], lastIndex = -1, toolCalls = false;
37
39
 
38
40
  let resetLastIndex = () => {
@@ -75,57 +77,52 @@ module.exports = class Aidejs {
75
77
 
76
78
  // 执行一次回复
77
79
  let runChoice = (choiceIndex) => {
78
- if (choiceIndex >= chunk.choices.length) return;
80
+ if (choiceIndex >= chunk.choices.length) { return; }
79
81
 
80
82
  let choice = chunk.choices[choiceIndex];
81
83
 
82
- if (choice.finish_reason) {
83
- if (toolCalls) {
84
- toolCalls = false;
85
- runChat();
86
- } else {
87
- runInputback();
88
- }
89
- } else {
90
- logback(choice.delta.content || "");
91
-
92
- if (choice.delta.tool_calls) {
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
+ }
93
89
 
94
- // 执行一次工具
95
- let runTool = (toolIndex) => {
96
- if (toolIndex >= choice.delta.tool_calls.length) {
97
- toolCalls = true;
98
- runChoice(choiceIndex + 1);
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
+ });
99
116
  } 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
- }
117
+ toolback(result);
121
118
  }
122
- };
123
- runTool(0);
124
- } else {
125
- if (messages[lastIndex].role !== "assistant") resetLastIndex();
126
- messages[lastIndex].content += choice.delta.content || "";
127
- runChoice(choiceIndex + 1);
128
- }
119
+ }
120
+ };
121
+ runTool(0);
122
+ } else {
123
+ if (messages[lastIndex].role !== "assistant") resetLastIndex();
124
+ messages[lastIndex].content += choice.delta.content || "";
125
+ runChoice(choiceIndex + 1);
129
126
  }
130
127
 
131
128
  };
@@ -133,6 +130,17 @@ module.exports = class Aidejs {
133
130
  } catch (e) {
134
131
  console.error("处理智能体响应时发生错误:", e);
135
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();
136
144
  });
137
145
 
138
146
  resetLastIndex();
@@ -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
- chunk = chunk.toString().replace(/^data: /, "");
23
-
24
- // 没有callback的情况下,data不累积,直接把chunk传给databack,适合流式处理
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(toJSON(data));
25
+ if (callback) callback();
32
26
  });
33
27
  });
34
28
 
package/types/index.d.ts CHANGED
@@ -20,7 +20,7 @@ export default class Aidejs {
20
20
  * 安装插件
21
21
  * @param plugin
22
22
  */
23
- static use(plugin: any): Aidejs
23
+ static use(plugin: any, options?: any): Aidejs
24
24
 
25
25
  /**
26
26
  * 执行任务