mm_llm 1.3.0 → 1.3.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.
@@ -0,0 +1,123 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * 配额与事务分区
5
+ * @description 装配 Sqlite 的用户配额方法、全量清理、连接关闭与统一存储键、事务辅助方法
6
+ */
7
+
8
+ /**
9
+ * 装配配额与事务方法到 Sqlite 原型
10
+ * @param {class} Sqlite SQLite 存储类
11
+ */
12
+ function install(Sqlite) {
13
+ /**
14
+ * 读取用户配额
15
+ *
16
+ * 配额按用户存放(不区分智能体),未设置的用户返回 undefined,
17
+ * LLM 会据此视为无配额而拒绝调用。
18
+ * @param {Number} user_id 用户ID
19
+ * @returns {Number|undefined} 剩余配额
20
+ */
21
+ Sqlite.prototype.getQuota = function (user_id) {
22
+ let row = this._db.prepare('SELECT quota FROM quotas WHERE user_id = ?').get(this._key(user_id));
23
+ return row ? row.quota : undefined;
24
+ };
25
+
26
+ /**
27
+ * 设置用户配额
28
+ * @param {Number} user_id 用户ID
29
+ * @param {Number} quota 剩余配额
30
+ * @returns {Sqlite} 当前实例
31
+ */
32
+ Sqlite.prototype.setQuota = function (user_id, quota) {
33
+ this._db
34
+ .prepare(
35
+ 'INSERT INTO quotas(user_id, quota) VALUES(?, ?) ' +
36
+ 'ON CONFLICT(user_id) DO UPDATE SET quota = excluded.quota'
37
+ )
38
+ .run(this._key(user_id), quota);
39
+ return this;
40
+ };
41
+
42
+ /**
43
+ * 增减用户配额
44
+ *
45
+ * 由 SQLite 在库内完成加减并返回新值,便于调用后一次扣分,
46
+ * 避免读改写竞态。
47
+ * @param {Number} user_id 用户ID
48
+ * @param {Number} delta 增减量,扣分传负数
49
+ * @returns {Number} 增减后的配额
50
+ */
51
+ Sqlite.prototype.gainQuota = function (user_id, delta) {
52
+ let row = this._db
53
+ .prepare(
54
+ 'INSERT INTO quotas(user_id, quota) VALUES(?, ?) ' +
55
+ 'ON CONFLICT(user_id) DO UPDATE SET quota = quota + excluded.quota RETURNING quota'
56
+ )
57
+ .get(this._key(user_id), delta);
58
+ return row.quota;
59
+ };
60
+
61
+ /**
62
+ * 删除用户配额
63
+ * @param {Number} user_id 用户ID
64
+ * @returns {Boolean} 是否删除了已存在的配额
65
+ */
66
+ Sqlite.prototype.delQuota = function (user_id) {
67
+ let result = this._db.prepare('DELETE FROM quotas WHERE user_id = ?').run(this._key(user_id));
68
+ return result.changes > 0;
69
+ };
70
+
71
+ /**
72
+ * 清空全部数据
73
+ * @returns {Sqlite} 当前实例
74
+ */
75
+ Sqlite.prototype.clear = function () {
76
+ this._transaction(() => {
77
+ this._db.exec(
78
+ 'DELETE FROM messages; DELETE FROM sessions; DELETE FROM agents; ' +
79
+ 'DELETE FROM tools; DELETE FROM users; DELETE FROM options; DELETE FROM quotas;'
80
+ );
81
+ });
82
+ return this;
83
+ };
84
+
85
+ /**
86
+ * 关闭数据库连接
87
+ * @returns {void}
88
+ */
89
+ Sqlite.prototype.close = function () {
90
+ this._db.close();
91
+ };
92
+
93
+ /**
94
+ * 统一存储键的表示(私有方法)
95
+ *
96
+ * 表的标识列是文本类型,这里显式转换,避免数字与字符串形式
97
+ * 落到不同行。
98
+ * @param {*} value 原始标识
99
+ * @returns {String} 文本标识
100
+ */
101
+ Sqlite.prototype._key = function (value) {
102
+ return String(value);
103
+ };
104
+
105
+ /**
106
+ * 在事务中执行写操作(私有方法)
107
+ * @param {Function} handler 写操作
108
+ * @returns {*} handler 的返回值
109
+ */
110
+ Sqlite.prototype._transaction = function (handler) {
111
+ this._db.exec('BEGIN');
112
+ try {
113
+ let result = handler();
114
+ this._db.exec('COMMIT');
115
+ return result;
116
+ } catch (error) {
117
+ this._db.exec('ROLLBACK');
118
+ throw error;
119
+ }
120
+ };
121
+ }
122
+
123
+ module.exports = { install };
@@ -0,0 +1,189 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * 会话消息分区
5
+ * @description 装配 Sqlite 的会话消息读写方法:追加、覆盖、读取、会话判定与删除
6
+ */
7
+
8
+ /**
9
+ * 装配会话消息方法到 Sqlite 原型
10
+ * @param {class} Sqlite SQLite 存储类
11
+ */
12
+ function install(Sqlite) {
13
+ /**
14
+ * 追加单条消息
15
+ * @param {Number} user_id 用户ID
16
+ * @param {Number} agent_id 智能体ID
17
+ * @param {Number} session_id 会话ID
18
+ * @param {Object} message 消息对象,形如 { role, content }
19
+ * @returns {Sqlite} 当前实例
20
+ */
21
+ Sqlite.prototype.addMessage = function (user_id, agent_id, session_id, message) {
22
+ return this.addMessages(user_id, agent_id, session_id, [message]);
23
+ };
24
+
25
+ /**
26
+ * 批量追加消息
27
+ * @param {Number} user_id 用户ID
28
+ * @param {Number} agent_id 智能体ID
29
+ * @param {Number} session_id 会话ID
30
+ * @param {Array} messages 消息数组
31
+ * @returns {Sqlite} 当前实例
32
+ */
33
+ Sqlite.prototype.addMessages = function (user_id, agent_id, session_id, messages) {
34
+ let user = this._key(user_id);
35
+ let agent = this._key(agent_id);
36
+ let session = this._key(session_id);
37
+ let insert = this._db.prepare(
38
+ 'INSERT INTO messages(user_id, agent_id, session_id, seq, message) VALUES(?, ?, ?, ?, ?)'
39
+ );
40
+ this._transaction(() => {
41
+ this._ensureSession(user_id, agent_id, session_id);
42
+ let start = this._countMessages(user_id, agent_id, session_id);
43
+ for (let i = 0; i < messages.length; i++) {
44
+ insert.run(user, agent, session, start + i, JSON.stringify(messages[i]));
45
+ }
46
+ });
47
+ return this;
48
+ };
49
+
50
+ /**
51
+ * 覆盖写入会话消息
52
+ * @param {Number} user_id 用户ID
53
+ * @param {Number} agent_id 智能体ID
54
+ * @param {Number} session_id 会话ID
55
+ * @param {Array} messages 消息数组
56
+ * @returns {Sqlite} 当前实例
57
+ */
58
+ Sqlite.prototype.setMessages = function (user_id, agent_id, session_id, messages) {
59
+ let user = this._key(user_id);
60
+ let agent = this._key(agent_id);
61
+ let session = this._key(session_id);
62
+ let insert = this._db.prepare(
63
+ 'INSERT INTO messages(user_id, agent_id, session_id, seq, message) VALUES(?, ?, ?, ?, ?)'
64
+ );
65
+ let remove = this._db.prepare(
66
+ 'DELETE FROM messages WHERE user_id = ? AND agent_id = ? AND session_id = ?'
67
+ );
68
+ this._transaction(() => {
69
+ this._ensureSession(user_id, agent_id, session_id);
70
+ remove.run(user, agent, session);
71
+ for (let i = 0; i < messages.length; i++) {
72
+ insert.run(user, agent, session, i, JSON.stringify(messages[i]));
73
+ }
74
+ });
75
+ return this;
76
+ };
77
+
78
+ /**
79
+ * 读取会话消息
80
+ * @param {Number} user_id 用户ID
81
+ * @param {Number} agent_id 智能体ID
82
+ * @param {Number} session_id 会话ID
83
+ * @returns {Array} 消息数组
84
+ */
85
+ Sqlite.prototype.getMessages = function (user_id, agent_id, session_id) {
86
+ let rows = this._db
87
+ .prepare(
88
+ 'SELECT message FROM messages WHERE user_id = ? AND agent_id = ? AND session_id = ? ORDER BY seq'
89
+ )
90
+ .all(this._key(user_id), this._key(agent_id), this._key(session_id));
91
+ return rows.map((row) => JSON.parse(row.message));
92
+ };
93
+
94
+ /**
95
+ * 统计会话消息数量
96
+ * @param {Number} user_id 用户ID
97
+ * @param {Number} agent_id 智能体ID
98
+ * @param {Number} session_id 会话ID
99
+ * @returns {Number} 消息条数
100
+ */
101
+ Sqlite.prototype.countMessages = function (user_id, agent_id, session_id) {
102
+ return this._countMessages(user_id, agent_id, session_id);
103
+ };
104
+
105
+ /**
106
+ * 判断会话是否存在
107
+ * @param {Number} user_id 用户ID
108
+ * @param {Number} agent_id 智能体ID
109
+ * @param {Number} session_id 会话ID
110
+ * @returns {Boolean} 是否存在
111
+ */
112
+ Sqlite.prototype.hasSession = function (user_id, agent_id, session_id) {
113
+ let row = this._db
114
+ .prepare(
115
+ 'SELECT 1 AS found FROM sessions WHERE user_id = ? AND agent_id = ? AND session_id = ?'
116
+ )
117
+ .get(this._key(user_id), this._key(agent_id), this._key(session_id));
118
+ return Boolean(row);
119
+ };
120
+
121
+ /**
122
+ * 列出全部会话的三元组
123
+ * @returns {Array} 形如 [{ user_id, agent_id, session_id }] 的数组
124
+ */
125
+ Sqlite.prototype.listSessions = function () {
126
+ let rows = this._db
127
+ .prepare('SELECT user_id, agent_id, session_id FROM sessions ORDER BY rowid')
128
+ .all();
129
+ return rows.map((row) => ({
130
+ user_id: row.user_id,
131
+ agent_id: row.agent_id,
132
+ session_id: row.session_id
133
+ }));
134
+ };
135
+
136
+ /**
137
+ * 删除会话及其消息
138
+ * @param {Number} user_id 用户ID
139
+ * @param {Number} agent_id 智能体ID
140
+ * @param {Number} session_id 会话ID
141
+ * @returns {Boolean} 是否删除了已存在的会话
142
+ */
143
+ Sqlite.prototype.delSession = function (user_id, agent_id, session_id) {
144
+ let user = this._key(user_id);
145
+ let agent = this._key(agent_id);
146
+ let session = this._key(session_id);
147
+ let removeMessages = this._db.prepare(
148
+ 'DELETE FROM messages WHERE user_id = ? AND agent_id = ? AND session_id = ?'
149
+ );
150
+ let removeSession = this._db.prepare(
151
+ 'DELETE FROM sessions WHERE user_id = ? AND agent_id = ? AND session_id = ?'
152
+ );
153
+ return this._transaction(() => {
154
+ removeMessages.run(user, agent, session);
155
+ return removeSession.run(user, agent, session).changes > 0;
156
+ });
157
+ };
158
+
159
+ /**
160
+ * 统计指定会话的消息数量(私有方法)
161
+ * @param {Number} user_id 用户ID
162
+ * @param {Number} agent_id 智能体ID
163
+ * @param {Number} session_id 会话ID
164
+ * @returns {Number} 消息条数
165
+ */
166
+ Sqlite.prototype._countMessages = function (user_id, agent_id, session_id) {
167
+ let row = this._db
168
+ .prepare(
169
+ 'SELECT COUNT(*) AS total FROM messages WHERE user_id = ? AND agent_id = ? AND session_id = ?'
170
+ )
171
+ .get(this._key(user_id), this._key(agent_id), this._key(session_id));
172
+ return row.total;
173
+ };
174
+
175
+ /**
176
+ * 确保会话存在(私有方法)
177
+ * @param {Number} user_id 用户ID
178
+ * @param {Number} agent_id 智能体ID
179
+ * @param {Number} session_id 会话ID
180
+ * @returns {void}
181
+ */
182
+ Sqlite.prototype._ensureSession = function (user_id, agent_id, session_id) {
183
+ this._db
184
+ .prepare('INSERT OR IGNORE INTO sessions(user_id, agent_id, session_id) VALUES(?, ?, ?)')
185
+ .run(this._key(user_id), this._key(agent_id), this._key(session_id));
186
+ };
187
+ }
188
+
189
+ module.exports = { install };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_llm",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
4
4
  "description": "这是一个适用于多用户、多智能体、多技能、多工具的模块,方便打造多智能体系统。",
5
5
  "keywords": [
6
6
  "mm_mod",
@@ -13,8 +13,8 @@
13
13
  "multi-agent",
14
14
  "tool-calling"
15
15
  ],
16
- "layer": "infrastructure",
17
- "group": "adapters",
16
+ "layer": "action",
17
+ "group": "mode",
18
18
  "license": "ISC",
19
19
  "author": "qww",
20
20
  "type": "commonjs",
@@ -40,9 +40,9 @@
40
40
  "test": "node test.js"
41
41
  },
42
42
  "dependencies": {
43
- "mm_admin": "^1.1.0",
44
- "mm_machine": "^6.0.0",
45
- "mm_config": "^2.6.0",
46
- "openai": "^7.23.0"
43
+ "mm_admin": "^1.2.2",
44
+ "mm_machine": "^6.0.2",
45
+ "mm_config": "^2.9.3",
46
+ "openai": "^7.62.0"
47
47
  }
48
48
  }