@aidejs/core 0.1.0-alpha.3 → 0.1.0-beta.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/CHANGELOG CHANGED
@@ -1,4 +1,8 @@
1
1
  v0.1.0:
2
- date:
2
+ date:2026-04-07
3
3
  changes:
4
- - 初始化版本
4
+ - 初始化版本
5
+ 1、基本的LLM交互控制
6
+ * 支持新建任务、实时反馈、输出思考过程
7
+ * 可扩展tools能力
8
+ 2、常用的若干工具tools
package/README.md CHANGED
@@ -39,7 +39,7 @@
39
39
  <tbody>
40
40
  <tr>
41
41
  <td>@aidejs/core</td>
42
- <td>必选项,基础库,提供了最核心的LLM交互功能和插件系统</td>
42
+ <td>必选项,核心库,提供了最核心的LLM交互功能和插件系统</td>
43
43
  <td>
44
44
  <a href="https://zxl20070701.github.io/toolbox/#/npm-download?packages=@aidejs/core&interval=7">
45
45
  <img src="https://img.shields.io/npm/dm/@aidejs/core.svg" alt="downloads">
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aidejs/core",
3
- "version": "0.1.0-alpha.3",
3
+ "version": "0.1.0-beta.1",
4
4
  "description": "@aidejs/core",
5
5
  "main": "./src/index.js",
6
6
  "typings": "./types/index.d.ts",
package/src/Memory.js ADDED
@@ -0,0 +1,41 @@
1
+ module.exports = class Memory {
2
+
3
+ constructor(messages = []) {
4
+ this.messages = messages;
5
+ this.message_value = ""; // 当前流缓存值
6
+ this.message_type = ""; // 当前流类型
7
+ }
8
+
9
+ // 直接添加一次新信息
10
+ // 前置的流会关闭,开始一次新的待整理流
11
+ push(value) {
12
+ this.generate("");
13
+ this.messages.push(value);
14
+ }
15
+
16
+ // 完结或整理一次流信息
17
+ generate(newType) {
18
+ if (this.message_value) {
19
+ this.messages.push({
20
+ role: this.message_type,
21
+ content: this.message_value
22
+ });
23
+ }
24
+
25
+ this.message_type = newType;
26
+ this.message_value = "";
27
+ }
28
+
29
+ // 输入一个词元的助手信息
30
+ assistant(lexeme) {
31
+ if (this.message_type !== "assistant") this.generate("assistant");
32
+ this.message_value += lexeme;
33
+ }
34
+
35
+ // 获取记忆内容
36
+ valueOf() {
37
+ this.generate("");
38
+ return this.messages;
39
+ }
40
+
41
+ }
@@ -1,6 +1,6 @@
1
1
  const https = require('https');
2
2
  const http = require('http');
3
- const toJSON = require("./toJSON");
3
+ const { toJSON } = require("./utils");
4
4
 
5
5
  module.exports = function (url, options = {}, databack, callback, errorback) {
6
6
 
package/src/Tool.js CHANGED
@@ -1,4 +1,4 @@
1
- const debug = require("./utils/debug");
1
+ const { debug } = require("./utils");
2
2
 
3
3
  module.exports = class Tool {
4
4
 
package/src/index.js CHANGED
@@ -1,12 +1,12 @@
1
1
  const { initOption } = require("oipage/nodejs/option/index");
2
- const OpenAI = require("./utils/OpenAI");
2
+ const OpenAI = require("./OpenAI");
3
3
  const Tool = require("./Tool");
4
- const debug = require("./utils/debug");
4
+ const Memory = require("./Memory");
5
+ const { debug } = require("./utils");
5
6
 
6
7
  module.exports = class Aidejs {
7
8
 
8
- // 工具管理对象
9
- static tool = new Tool();
9
+ static tool = new Tool(); // 工具管理对象
10
10
 
11
11
  constructor(options = {}) {
12
12
  this.options = initOption(options, {
@@ -36,22 +36,15 @@ module.exports = class Aidejs {
36
36
 
37
37
  // 执行任务
38
38
  task(message, logback, inputback, thinkback) {
39
- let _this = this;
39
+ let _this = this, toolCalls = false;
40
40
 
41
- let messages = [..._this.options.messages], lastIndex = -1, toolCalls = false;
42
-
43
- let resetLastIndex = () => {
44
- messages.push({
45
- role: "assistant",
46
- content: ""
47
- });
48
- lastIndex = messages.length - 1;
49
- };
41
+ // 记忆管理对象
42
+ let memory = new Memory([..._this.options.messages]);
50
43
 
51
44
  let runInputback = () => {
52
45
  if (inputback) {
53
46
  inputback().then(message => {
54
- messages.push({
47
+ memory.push({
55
48
  role: "user",
56
49
  content: message
57
50
  });
@@ -67,21 +60,21 @@ module.exports = class Aidejs {
67
60
  let runChat = () => {
68
61
  let tools = Aidejs.tool.valueOf();
69
62
 
70
- if (this.options.debug) {
71
- debug("OpenAI messages", messages);
63
+ if (_this.options.debug) {
64
+ debug("OpenAI messages", memory.valueOf());
72
65
  debug("OpenAI tools", tools.map(function (funItem) {
73
66
  return funItem.function.name + " " + funItem.function.description;
74
67
  }));
75
68
  }
76
69
 
77
- OpenAI(this.options.url + "/chat/completions", {
70
+ OpenAI(_this.options.url + "/chat/completions", {
78
71
  header: {
79
- Authorization: `Bearer ${this.options.apiKey}`
72
+ Authorization: `Bearer ${_this.options.apiKey}`
80
73
  },
81
74
  params: {
82
75
 
83
76
  // 必需,模型ID,也就是你选择的具体哪个模型
84
- model: this.options.model,
77
+ model: _this.options.model,
85
78
 
86
79
  // 必需,提供的array类型的消息列表,包含从头到尾的对话历史
87
80
  // 比如值是一个数组,每个条目可以是下列类型之一:
@@ -124,7 +117,7 @@ module.exports = class Aidejs {
124
117
  // content:必须提供的string类型,表示工具消息的内容,一般是把函数调用的结果描述在这里
125
118
  // role:必须提供的string类型,表示消息作者的角色,对于tool message应该是"tool"
126
119
  // tool_call_id:必须提供的string类型,表示本次消息是对哪个函数调用的结果反馈
127
- messages,
120
+ messages: memory.valueOf(),
128
121
 
129
122
  // 用户可选的一个字段,是array类型,表示可供模型选择的一个工具列表
130
123
  // 列表中最多支持 128 个tool
@@ -151,11 +144,14 @@ module.exports = class Aidejs {
151
144
 
152
145
  if (choice.delta.content) {
153
146
  if (logback) logback(choice.delta.content);
147
+ memory.assistant(choice.delta.content);
154
148
  } else if (choice.delta.reasoning) {
155
149
  if (thinkback) thinkback(choice.delta.reasoning);
156
150
  }
157
151
 
158
152
  if (choice.delta.tool_calls) {
153
+ memory.push(choice.delta);
154
+
159
155
  if (_this.options.debug) debug("tool_calls", choice.delta.tool_calls);
160
156
 
161
157
  // 执行一次工具
@@ -168,7 +164,7 @@ module.exports = class Aidejs {
168
164
  let result = Aidejs.tool.invoke(_this, tool_call.function.name, JSON.parse(tool_call.function.arguments));
169
165
 
170
166
  let toolback = function (value) {
171
- messages.push({
167
+ memory.push({
172
168
  role: "tool",
173
169
  tool_call_id: tool_call.id,
174
170
  tool_name: tool_call.function.name,
@@ -181,7 +177,7 @@ module.exports = class Aidejs {
181
177
  result.then(function (value) {
182
178
  toolback(value);
183
179
  }).catch(function (error) {
184
- toolback(error + "");
180
+ toolback(error || "失败");
185
181
  });
186
182
  } else {
187
183
  toolback(result);
@@ -190,8 +186,6 @@ module.exports = class Aidejs {
190
186
  };
191
187
  runTool(0);
192
188
  } else {
193
- if (messages[lastIndex].role !== "assistant") resetLastIndex();
194
- messages[lastIndex].content += choice.delta.content || "";
195
189
  runChoice(choiceIndex + 1);
196
190
  }
197
191
 
@@ -212,12 +206,10 @@ module.exports = class Aidejs {
212
206
  toolCalls = false;
213
207
  runInputback();
214
208
  });
215
-
216
- resetLastIndex();
217
209
  };
218
210
 
219
211
  if (message) {
220
- messages.push({
212
+ memory.push({
221
213
  role: "user",
222
214
  content: message
223
215
  });
@@ -225,8 +217,6 @@ module.exports = class Aidejs {
225
217
  } else {
226
218
  runInputback();
227
219
  }
228
-
229
- return this;
230
220
  }
231
221
 
232
222
  };
package/src/utils.js ADDED
@@ -0,0 +1,17 @@
1
+ // 打印调试语句
2
+ exports.debug = function (name, content) {
3
+ console.log("\n\x1b[33m【" + name + "】 " + new Date().toString() + "\x1b[0m");
4
+ console.log(content);
5
+ console.log("\x1b[33m[DONE]\x1b[0m\n");
6
+ };
7
+
8
+ // 字符串转JSON
9
+ // 适配LLM返回数据格式
10
+ exports.toJSON = function (value) {
11
+ try {
12
+ value = value.trim().replace(/data: \[DONE\]$/, "");
13
+ return JSON.parse(value);
14
+ } catch (e) {
15
+ return value;
16
+ }
17
+ };
package/types/index.d.ts CHANGED
@@ -16,7 +16,7 @@ export default class Aidejs {
16
16
  * @param name
17
17
  * @param value
18
18
  */
19
- static registerTool(name: string, value: any): void
19
+ static registerTool(name: string, value: any): Aidejs
20
20
 
21
21
  /**
22
22
  * 安装插件
@@ -28,6 +28,6 @@ export default class Aidejs {
28
28
  * 执行任务
29
29
  * @param message
30
30
  */
31
- task(message?: string, logback?: (message: string) => void, inputback?: () => Promise<string>, thinkback?: (think: string) => void): this
31
+ task(message?: string, logback?: (message: string) => void, inputback?: () => Promise<string>, thinkback?: (think: string) => void): void
32
32
 
33
33
  }
@@ -1,5 +0,0 @@
1
- module.exports = function (name, content) {
2
- console.log("\n\x1b[33m【" + name + "】 " + new Date().toString() + "\x1b[0m");
3
- console.log(content);
4
- console.log("\x1b[33m---------------------------------------------------------------------\x1b[0m\n");
5
- }
@@ -1,8 +0,0 @@
1
- module.exports = function (value) {
2
- try {
3
- value = value.trim().replace(/data: \[DONE\]$/, "");
4
- return JSON.parse(value);
5
- } catch (e) {
6
- return value;
7
- }
8
- }