mm_datas 5.3.1 → 5.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.
package/lib/store.js CHANGED
@@ -1,414 +1,21 @@
1
1
  'use strict';
2
2
 
3
3
  /**
4
- * 可插拔存储层:内存 / JSON 文件
5
- * 生产环境可替换成 S5 适配器(实现同一组方法即可,world 不感知实现)
4
+ * 可插拔存储层:内存 / JSON 文件 / 混合布局
5
+ * 生产环境可替换成适配器(实现同一组方法即可,world 不感知实现)
6
6
  */
7
- const fs = require('fs');
8
- const path = require('path');
9
- const { clone, project } = require('./model.js');
10
- const B = require('./backend.js');
11
- const Persist = require('mm_persist').Persist;
12
7
 
13
- function compare(a, b) {
14
- if (a === b) return 0;
15
- if (a === undefined || a === null) return -1;
16
- if (b === undefined || b === null) return 1;
17
- if (typeof a === 'number' && typeof b === 'number') return a - b;
18
- return String(a) < String(b) ? -1 : 1;
19
- }
20
-
21
- function isOperatorObject(cond) {
22
- if (!cond || typeof cond !== 'object' || Array.isArray(cond)) return false;
23
- const keys = Object.keys(cond);
24
- return keys.length > 0 && keys.every(function (k) { return k.charAt(0) === '$'; });
25
- }
26
-
27
- function matchValue(actual, cond) {
28
- if (isOperatorObject(cond)) {
29
- for (const op in cond) {
30
- const expected = cond[op];
31
- switch (op) {
32
- case '$eq': if (actual !== expected) return false; break;
33
- case '$ne': if (actual === expected) return false; break;
34
- case '$gt': if (!(actual > expected)) return false; break;
35
- case '$gte': if (!(actual >= expected)) return false; break;
36
- case '$lt': if (!(actual < expected)) return false; break;
37
- case '$lte': if (!(actual <= expected)) return false; break;
38
- case '$in':
39
- if (!Array.isArray(expected) || expected.indexOf(actual) === -1) return false;
40
- break;
41
- case '$nin':
42
- if (Array.isArray(expected) && expected.indexOf(actual) !== -1) return false;
43
- break;
44
- case '$like':
45
- if (typeof actual !== 'string' || actual.indexOf(String(expected)) === -1) return false;
46
- break;
47
- case '$contains':
48
- if (!Array.isArray(actual) || actual.indexOf(expected) === -1) return false;
49
- break;
50
- default: return false;
51
- }
52
- }
53
- return true;
54
- }
55
- if (Array.isArray(cond)) return cond.indexOf(actual) !== -1;
56
- if (cond && typeof cond === 'object') {
57
- return JSON.stringify(actual) === JSON.stringify(cond);
58
- }
59
- return actual === cond;
60
- }
61
-
62
- function match(row, query) {
63
- if (!query || typeof query !== 'object') return true;
64
- for (const k in query) {
65
- if (k === '$or') {
66
- const list = query[k] || [];
67
- let hit = false;
68
- for (let i = 0; i < list.length; i++) if (match(row, list[i])) { hit = true; break; }
69
- if (!hit) return false;
70
- } else if (k === '$and') {
71
- const list = query[k] || [];
72
- for (let i = 0; i < list.length; i++) if (!match(row, list[i])) return false;
73
- } else if (!matchValue(row[k], query[k])) {
74
- return false;
75
- }
76
- }
77
- return true;
78
- }
79
-
80
- function parseOrder(order) {
81
- if (!order) return null;
82
- if (Array.isArray(order)) {
83
- return { field: order[0], desc: /desc/i.test(String(order[1] || '')) };
84
- }
85
- if (typeof order === 'object') {
86
- return {
87
- field: order.field || order.by,
88
- desc: !!(order.desc || /desc/i.test(String(order.order || '')))
89
- };
90
- }
91
- const parts = String(order).trim().split(/\s+/);
92
- return { field: parts[0], desc: /desc/i.test(parts[1] || '') };
93
- }
94
-
95
- class MemoryStore extends Persist {
96
- constructor() {
97
- super({ persist: false });
98
- this.tables = new Map();
99
- this.calls = { list: 0, get: 0, insert: 0, update: 0, remove: 0 };
100
- this.backend = 'memory';
101
- }
102
-
103
- table(name) {
104
- if (!this.tables.has(name)) this.tables.set(name, []);
105
- return this.tables.get(name);
106
- }
107
-
108
- tableNames() { return Array.from(this.tables.keys()); }
109
-
110
- setTable(name, rows) { this.tables.set(name, rows || []); }
111
-
112
- clear(name) { this.tables.set(name, []); }
113
-
114
- /** 彻底移除一张表(clear 会留下空表,落盘时会被写成空 .json) */
115
- dropTable(name) { this.tables.delete(name); }
116
-
117
- getRows(name) { return this.tables.get(name) || []; }
118
-
119
- insert(name, row) {
120
- this.calls.insert++;
121
- this.table(name).push(clone(row));
122
- return row;
123
- }
124
-
125
- update(name, query, updater) {
126
- this.calls.update++;
127
- const rows = this.table(name);
128
- let n = 0;
129
- for (let i = 0; i < rows.length; i++) {
130
- if (match(rows[i], query)) { updater(rows[i]); n++; }
131
- }
132
- return n;
133
- }
134
-
135
- remove(name, query) {
136
- this.calls.remove++;
137
- const rows = this.table(name);
138
- const kept = [];
139
- let n = 0;
140
- for (let i = 0; i < rows.length; i++) {
141
- if (match(rows[i], query)) n++;
142
- else kept.push(rows[i]);
143
- }
144
- this.tables.set(name, kept);
145
- return n;
146
- }
147
-
148
- count(name, query) {
149
- const rows = this.getRows(name);
150
- let n = 0;
151
- for (let i = 0; i < rows.length; i++) if (match(rows[i], query)) n++;
152
- return n;
153
- }
154
-
155
- exists(name, query) {
156
- const rows = this.getRows(name);
157
- for (let i = 0; i < rows.length; i++) if (match(rows[i], query)) return true;
158
- return false;
159
- }
160
-
161
- get(name, query, key) {
162
- this.calls.get++;
163
- const rows = this.getRows(name);
164
- const q = (typeof query !== 'object' || query === null) ? (function () { const o = {}; o[key] = query; return o; })() : query;
165
- for (let i = 0; i < rows.length; i++) if (match(rows[i], q)) return clone(rows[i]);
166
- return null;
167
- }
168
-
169
- list(name, query, options, key) {
170
- this.calls.list++;
171
- options = options || {};
172
- query = query || {};
173
- if (typeof query !== 'object' || Array.isArray(query)) {
174
- const q = {};
175
- q[key] = query;
176
- query = q;
177
- }
178
- let rows = this.getRows(name).filter(function (r) { return match(r, query); });
179
- const ord = parseOrder(options.order);
180
- if (ord && ord.field) {
181
- rows = rows.slice().sort(function (a, b) {
182
- const c = compare(a[ord.field], b[ord.field]);
183
- return ord.desc ? -c : c;
184
- });
185
- }
186
- const total = rows.length;
187
- let start = 0;
188
- if (options.cursor !== undefined && options.cursor !== null && key) {
189
- const idx = rows.findIndex(function (r) { return String(r[key]) === String(options.cursor); });
190
- if (idx >= 0) start = idx + 1;
191
- } else if (options.offset) {
192
- start = Math.max(0, parseInt(options.offset, 10) || 0);
193
- }
194
- let limit = options.limit === undefined ? 1000 : parseInt(options.limit, 10);
195
- if (isNaN(limit)) limit = 1000;
196
- const end = limit < 0 ? rows.length : Math.min(rows.length, start + limit);
197
- let page = rows.slice(start, end).map(function (r) { return project(r, options.fields, key); });
198
- const next_cursor = end < rows.length && page.length ? page[page.length - 1][key] : null;
199
- return { list: page, count: total, next_cursor: next_cursor === undefined ? null : next_cursor };
200
- }
201
-
202
- save() { return { ok: true, backend: 'memory' }; }
203
- load() { return { ok: true, backend: 'memory', table_names: [] }; }
204
- }
205
-
206
- class JsonFileStore extends MemoryStore {
207
- constructor(dir, options) {
208
- super();
209
- this.dir = dir;
210
- this.backend = 'json';
211
- this.persist = !(options && options.persist === false);
212
- this.autoSave = !!(options && options.autoSave);
213
- this.flush_interval = Math.max(0, Number(options && options.flush_interval) || 0);
214
- }
215
-
216
- save(dir) {
217
- if (dir) this.dir = dir;
218
- fs.mkdirSync(this.dir, { recursive: true });
219
- const names = this.tableNames();
220
- for (let i = 0; i < names.length; i++) {
221
- const file = path.join(this.dir, names[i] + '.json');
222
- fs.writeFileSync(file, JSON.stringify(this.getRows(names[i]), null, 2), 'utf8');
223
- }
224
- fs.writeFileSync(path.join(this.dir, '_tables.json'), JSON.stringify(names, null, 2), 'utf8');
225
- this.dirty = false;
226
- return { ok: true, backend: 'json', dir: this.dir, tables: names.length };
227
- }
228
-
229
- load(modelNames, dir) {
230
- if (dir) this.dir = dir;
231
- if (!fs.existsSync(this.dir)) return { ok: false, reason: '目录不存在' };
232
- const meta = path.join(this.dir, '_tables.json');
233
- let names = modelNames;
234
- if (!names && fs.existsSync(meta)) names = JSON.parse(fs.readFileSync(meta, 'utf8'));
235
- if (!names) return { ok: false, reason: '缺少 _tables.json' };
236
- for (let i = 0; i < names.length; i++) {
237
- const file = path.join(this.dir, names[i] + '.json');
238
- if (fs.existsSync(file)) {
239
- this.setTable(names[i], JSON.parse(fs.readFileSync(file, 'utf8')));
240
- }
241
- }
242
- return { ok: true, backend: 'json', dir: this.dir, tables: names.length, table_names: names };
243
- }
244
- }
8
+ const MemoryStore = require('./memory_store.js');
9
+ const JsonFileStore = require('./json_file_store.js');
10
+ const HybridStore = require('./hybrid_store.js');
11
+ const { match, parseOrder } = require('./store_query.js');
245
12
 
246
13
  /**
247
- * v5 默认布局:每种模型对应 data/ 下的一个目录
248
- * - kind:static → data/<name>/000001.json(一条记录一个文件,主键 6 位补零)
249
- * - kind:dynamic → mm_cache 缓存 + mm_mongodb 持久;依赖不可用时回退
250
- * data/<name>/_rows.json,保证离线可跑
251
- * - mm_sql 为可选数据源适配器,启用后 flush 时同步一份关系型副本
252
- *
253
- * 同步 CRUD 始终走内存工作集(MemoryStore),外部依赖只在显式异步入口调用:
254
- * connect() / flush() / restore() / cacheGet() / cacheList()。
14
+ * 按类型创建存储实例
15
+ * @param {string} type 存储类型(memory/json/file/hybrid/rowfile/auto)
16
+ * @param {object} [config] 存储配置
17
+ * @returns {MemoryStore|JsonFileStore|HybridStore} 存储实例
255
18
  */
256
- class HybridStore extends MemoryStore {
257
- constructor(config) {
258
- super();
259
- config = config || {};
260
- this.dir = config.dir || path.join(process.cwd(), 'data');
261
- this.backend = 'hybrid';
262
- this.persist = config.persist !== false;
263
- this.autoSave = !!config.autoSave;
264
- this.flush_interval = Math.max(0, Number(config.flush_interval) || 0);
265
- this.namespace = config.namespace || 'mm_datas';
266
- // 文件层也走 jsondbAdmin 实例池,scope 需带上命名空间与目录
267
- this.layout = new B.RowFileLayout(this.dir, this.namespace);
268
- this.adapters = B.createAdapters(config);
269
- }
270
-
271
- _model(name) { return this.models ? this.models.get(name) : null; }
272
- /** 落盘只分两类:static → 逐文件;其余(dynamic / link / 未定义)→ 缓存 + 持久 */
273
- _kind(name) { const m = this._model(name); return m && m.kind === 'static' ? 'static' : 'dynamic'; }
274
- _key(name) { const m = this._model(name); return m ? m.key : 'id'; }
275
-
276
- /** 同步落盘:静态逐文件 + 动态本地回退快照(不触碰外部依赖) */
277
- save(dir) {
278
- if (dir) { this.dir = dir; this.layout._bind(dir); }
279
- fs.mkdirSync(this.dir, { recursive: true });
280
- const names = this.tableNames().filter(function (n) { return n.charAt(0) !== '_'; });
281
- let staticFiles = 0;
282
- let dynamicFiles = 0;
283
- for (let i = 0; i < names.length; i++) {
284
- const name = names[i];
285
- const rows = this.getRows(name);
286
- if (this._kind(name) === 'static') {
287
- staticFiles += this.layout.saveStatic(name, this._key(name), rows);
288
- } else {
289
- dynamicFiles += this.layout.saveDynamic(name, rows);
290
- }
291
- }
292
- this.dirty = false;
293
- return {
294
- ok: true, backend: 'hybrid', dir: this.dir, tables: names.length,
295
- table_names: names, static_files: staticFiles, dynamic_fallback: dynamicFiles
296
- };
297
- }
298
- /** 从 data/ 下的模型目录恢复;动态集合可被 mm_mongodb / mm_sql 覆盖 */
299
- load(modelNames, dir) {
300
- const that = this;
301
- const res = this.layout.load(function (name, rows) { that.setTable(name, rows); }, dir);
302
- if (dir) this.dir = dir;
303
- // 以模型定义为准修正 kind(模型先于数据恢复装载)
304
- if (res.collections) {
305
- for (let i = 0; i < res.collections.length; i++) {
306
- res.collections[i].kind = this._kind(res.collections[i].name);
307
- }
308
- }
309
- return res;
310
- }
311
-
312
- /** 显式连接可选依赖(未安装 / 未启用 / 连不上都不抛错) */
313
- async connect() {
314
- const out = await B.openAll(this.adapters);
315
- out.file_layer = await this.layout.open();
316
- return out;
317
- }
318
-
319
- backends() {
320
- return {
321
- backend: 'hybrid', dir: this.dir, dependencies: B.probe(),
322
- // 文件层单独报告,不塞进 adapters 以免破坏既有三适配器的契约
323
- file_layer: this.layout.status(),
324
- adapters: {
325
- cache: this.adapters.cache.status(),
326
- mongodb: this.adapters.mongo.status(),
327
- sql: this.adapters.sql.status()
328
- }
329
- };
330
- }
331
-
332
- /** 把内存工作集推到外部层:静态逐文件、动态缓存 + 持久 + 可选 SQL */
333
- async flush() {
334
- const report = {
335
- ok: true, static_files: 0, cache: 0, mongodb: 0, sql: 0,
336
- local_snapshot: 0, file_fallback: 0, collections: []
337
- };
338
- const names = this.tableNames().filter(function (n) { return n.charAt(0) !== '_'; });
339
- for (let i = 0; i < names.length; i++) {
340
- const name = names[i];
341
- const rows = this.getRows(name);
342
- const one = { name: name, kind: this._kind(name), rows: rows.length };
343
- if (one.kind === 'static') {
344
- one.files = await this.layout.flushStatic(name, this._key(name), rows);
345
- report.static_files += one.files;
346
- } else {
347
- await this.adapters.cache.set('rows:' + this.namespace + ':' + name, rows);
348
- report.cache++;
349
- const pushed = await this.adapters.mongo.push(name, rows);
350
- if (pushed.ok) { report.mongodb++; one.mongodb = true; }
351
- else { report.file_fallback++; one.file_fallback = true; }
352
- // 本地快照始终保留一份:MongoDB 不可用时仍可恢复,也保证目录布局完整
353
- await this.layout.flushDynamic(name, rows);
354
- report.local_snapshot++;
355
- const sql = await this.adapters.sql.push(name, this._key(name), rows);
356
- if (sql.ok) { report.sql++; one.sql = sql.table; }
357
- }
358
- report.collections.push(one);
359
- }
360
- return report;
361
- }
362
-
363
- /**
364
- * 恢复:集合清单以模型定义为准(目录里可能没有动态集合),
365
- * 先读 data/ 下的静态逐文件与动态本地快照,再用 mm_mongodb / mm_sql 覆盖动态集合。
366
- */
367
- async restore(dir) {
368
- const that = this;
369
- const loaded = await this.layout.restore(function (name, rows) { that.setTable(name, rows); }, dir);
370
- if (dir) this.dir = dir;
371
- // 以模型定义为准修正 kind(模型先于数据恢复装载)
372
- if (loaded.collections) {
373
- for (let i = 0; i < loaded.collections.length; i++) {
374
- loaded.collections[i].kind = this._kind(loaded.collections[i].name);
375
- }
376
- }
377
- const names = this.models ? Array.from(this.models.keys()) : [];
378
- for (let i = 0; i < loaded.table_names.length; i++) {
379
- if (names.indexOf(loaded.table_names[i]) === -1) names.push(loaded.table_names[i]);
380
- }
381
- names.sort();
382
- const sources = {};
383
- for (let i = 0; i < names.length; i++) {
384
- const name = names[i];
385
- if (this._kind(name) !== 'dynamic') {
386
- sources[name] = this.layout.backend === 'mm_jsondb' ? 'mm_jsondb' : 'static-files';
387
- continue;
388
- }
389
- let rows = null;
390
- let source = '';
391
- if (this.adapters.mongo.connected) {
392
- try { rows = await this.adapters.mongo.pull(name); } catch (e) { rows = null; }
393
- if (Array.isArray(rows)) source = 'mm_mongodb';
394
- }
395
- if (rows === null && this.adapters.sql.connected) {
396
- try { rows = await this.adapters.sql.pull(name); } catch (e) { rows = null; }
397
- if (Array.isArray(rows)) source = 'mm_sql';
398
- }
399
- if (Array.isArray(rows)) {
400
- this.setTable(name, rows);
401
- sources[name] = source;
402
- } else {
403
- if (loaded.table_names.indexOf(name) === -1) this.setTable(name, []);
404
- sources[name] = 'local-snapshot';
405
- }
406
- }
407
- this.seq.clear();
408
- return { ok: true, backend: 'hybrid', dir: this.dir, table_names: names, sources: sources };
409
- }
410
- }
411
-
412
19
  function createStore(type, config) {
413
20
  config = config || {};
414
21
  if (type === 'json' || type === 'file') {
@@ -421,10 +28,10 @@ function createStore(type, config) {
421
28
  }
422
29
 
423
30
  module.exports = {
424
- MemoryStore: MemoryStore,
425
- JsonFileStore: JsonFileStore,
426
- HybridStore: HybridStore,
427
- createStore: createStore,
428
- match: match,
429
- parseOrder: parseOrder
31
+ MemoryStore,
32
+ JsonFileStore,
33
+ HybridStore,
34
+ createStore,
35
+ match,
36
+ parseOrder
430
37
  };
@@ -0,0 +1,119 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * 内存查询助手
5
+ * 可插拔存储层共用的条件匹配、排序解析工具。
6
+ */
7
+
8
+ /**
9
+ * 通用比较(数字按大小,其余按字符串序)
10
+ * @param {*} a 比较值 a
11
+ * @param {*} b 比较值 b
12
+ * @returns {number} a<b 为 -1,a>b 为 1,相等为 0
13
+ */
14
+ function compare(a, b) {
15
+ if (a === b) return 0;
16
+ if (a === undefined || a === null) return -1;
17
+ if (b === undefined || b === null) return 1;
18
+ if (typeof a === 'number' && typeof b === 'number') return a - b;
19
+ return String(a) < String(b) ? -1 : 1;
20
+ }
21
+
22
+ /**
23
+ * 判断条件是否为操作符对象(所有键都以 $ 开头)
24
+ * @param {*} cond 条件
25
+ * @returns {boolean} 是操作符对象返回 true
26
+ */
27
+ function isOperatorObject(cond) {
28
+ if (!cond || typeof cond !== 'object' || Array.isArray(cond)) return false;
29
+ let keys = Object.keys(cond);
30
+ return keys.length > 0 && keys.every(function (k) { return k.charAt(0) === '$'; });
31
+ }
32
+
33
+ /**
34
+ * 单字段条件匹配
35
+ * @param {*} actual 实际值
36
+ * @param {*} cond 条件(操作符对象 / 数组 / 对象 / 标量)
37
+ * @returns {boolean} 匹配返回 true
38
+ */
39
+ function matchValue(actual, cond) {
40
+ if (isOperatorObject(cond)) {
41
+ for (let op in cond) {
42
+ let expected = cond[op];
43
+ switch (op) {
44
+ case '$eq': if (actual !== expected) return false; break;
45
+ case '$ne': if (actual === expected) return false; break;
46
+ case '$gt': if (!(actual > expected)) return false; break;
47
+ case '$gte': if (!(actual >= expected)) return false; break;
48
+ case '$lt': if (!(actual < expected)) return false; break;
49
+ case '$lte': if (!(actual <= expected)) return false; break;
50
+ case '$in':
51
+ if (!Array.isArray(expected) || expected.indexOf(actual) === -1) return false;
52
+ break;
53
+ case '$nin':
54
+ if (Array.isArray(expected) && expected.indexOf(actual) !== -1) return false;
55
+ break;
56
+ case '$like':
57
+ if (typeof actual !== 'string' || actual.indexOf(String(expected)) === -1) return false;
58
+ break;
59
+ case '$contains':
60
+ if (!Array.isArray(actual) || actual.indexOf(expected) === -1) return false;
61
+ break;
62
+ // 未知操作符按不匹配处理,避免误判成相等导致误删/误改
63
+ default: return false;
64
+ }
65
+ }
66
+ return true;
67
+ }
68
+ if (Array.isArray(cond)) return cond.indexOf(actual) !== -1;
69
+ if (cond && typeof cond === 'object') {
70
+ return JSON.stringify(actual) === JSON.stringify(cond);
71
+ }
72
+ return actual === cond;
73
+ }
74
+
75
+ /**
76
+ * 行记录与查询条件匹配(支持 $or / $and)
77
+ * @param {object} row 记录
78
+ * @param {object} query 查询条件
79
+ * @returns {boolean} 匹配返回 true
80
+ */
81
+ function match(row, query) {
82
+ if (!query || typeof query !== 'object') return true;
83
+ for (let k in query) {
84
+ if (k === '$or') {
85
+ let list = query[k] || [];
86
+ let hit = false;
87
+ for (let i = 0; i < list.length; i++) if (match(row, list[i])) { hit = true; break; }
88
+ if (!hit) return false;
89
+ } else if (k === '$and') {
90
+ let list = query[k] || [];
91
+ for (let i = 0; i < list.length; i++) if (!match(row, list[i])) return false;
92
+ } else if (!matchValue(row[k], query[k])) {
93
+ return false;
94
+ }
95
+ }
96
+ return true;
97
+ }
98
+
99
+ /**
100
+ * 解析排序参数(数组 / 对象 / 字符串三种形态)
101
+ * @param {Array|object|string} order 排序参数
102
+ * @returns {{field: string, desc: boolean}|null} 排序描述,空参数为 null
103
+ */
104
+ function parseOrder(order) {
105
+ if (!order) return null;
106
+ if (Array.isArray(order)) {
107
+ return { field: order[0], desc: /desc/i.test(String(order[1] || '')) };
108
+ }
109
+ if (typeof order === 'object') {
110
+ return {
111
+ field: order.field || order.by,
112
+ desc: !!(order.desc || /desc/i.test(String(order.order || '')))
113
+ };
114
+ }
115
+ let parts = String(order).trim().split(/\s+/);
116
+ return { field: parts[0], desc: /desc/i.test(parts[1] || '') };
117
+ }
118
+
119
+ module.exports = { compare, isOperatorObject, matchValue, match, parseOrder };
@@ -0,0 +1,106 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+
4
+ const TOOL_DIR = path.join(__dirname, '..', 'tool');
5
+
6
+ /**
7
+ * AI 工具契约扫描(非类文件)
8
+ * 扫描 tool/ 目录下各工具的 tool.json,供 mm_ai / mm_workflow 等宿主使用。
9
+ * 两个参数视图必须同源:parameters 给 LLM(OpenAI function calling 格式),
10
+ * params 给仓库工作流扫描(kernel_catalog / kernel_validator / workflow_engine)
11
+ */
12
+
13
+ /**
14
+ * 扫描工具目录,返回含 tool.json 的工具名列表
15
+ * @param {string} tool_dir - 工具目录路径
16
+ * @returns {Array<string>} 工具名数组
17
+ */
18
+ function scan_tool_names(tool_dir) {
19
+ if (!fs.existsSync(tool_dir)) return [];
20
+ return fs.readdirSync(tool_dir, { withFileTypes: true })
21
+ .filter(function (e) { return e.isDirectory() && fs.existsSync(path.join(tool_dir, e.name, 'tool.json')); })
22
+ .map(function (e) { return e.name; });
23
+ }
24
+
25
+ /**
26
+ * 读取工具契约配置(parameters 与 params 双向同步)
27
+ * @param {string} tool_dir - 工具目录路径
28
+ * @param {string} name - 工具名
29
+ * @returns {object|null} 工具配置对象,不存在时返回 null
30
+ */
31
+ function read_tool_config(tool_dir, name) {
32
+ let file = path.join(tool_dir, name, 'tool.json');
33
+ if (!fs.existsSync(file)) return null;
34
+ let cfg = JSON.parse(fs.readFileSync(file, 'utf8'));
35
+ if (cfg.parameters && !cfg.params) cfg.params = cfg.parameters;
36
+ if (cfg.params && !cfg.parameters) cfg.parameters = cfg.params;
37
+ return cfg;
38
+ }
39
+
40
+ /**
41
+ * 导出标准 OpenAI function calling 工具定义
42
+ * { type:'function', function:{ name, description, parameters } }
43
+ * 返回值可直接交给 mm_llm 的 setToolSchema / LLM 的 tools 数组
44
+ * @param {string} tool_dir - 工具目录路径
45
+ * @param {string} name - 工具名
46
+ * @returns {object|null} function calling 工具定义,工具不存在时返回 null
47
+ */
48
+ function function_tool_config(tool_dir, name) {
49
+ let cfg = read_tool_config(tool_dir, name);
50
+ if (!cfg) return null;
51
+ return {
52
+ type: 'function',
53
+ function: {
54
+ name: cfg.name || name,
55
+ description: cfg.description || '',
56
+ parameters: cfg.parameters || cfg.params || { type: 'object', properties: {} }
57
+ }
58
+ };
59
+ }
60
+
61
+ /**
62
+ * 列出全部工具名(绑定模块 tool 目录)
63
+ * @returns {Array<string>} 工具名数组
64
+ */
65
+ function list_tools() {
66
+ return scan_tool_names(TOOL_DIR);
67
+ }
68
+
69
+ /**
70
+ * 读取工具契约配置(绑定模块 tool 目录)
71
+ * @param {string} name - 工具名
72
+ * @returns {object|null} 工具配置对象
73
+ */
74
+ function get_tool_config(name) {
75
+ return read_tool_config(TOOL_DIR, name);
76
+ }
77
+
78
+ /**
79
+ * 获取标准 function calling 工具定义(绑定模块 tool 目录)
80
+ * @param {string} name - 工具名
81
+ * @returns {object|null} function calling 工具定义
82
+ */
83
+ function get_function_tool(name) {
84
+ return function_tool_config(TOOL_DIR, name);
85
+ }
86
+
87
+ /**
88
+ * 列出全部工具的 function calling 定义
89
+ * @returns {Array<object>} function calling 工具定义数组
90
+ */
91
+ function list_function_tools() {
92
+ return scan_tool_names(TOOL_DIR)
93
+ .map(function (n) { return function_tool_config(TOOL_DIR, n); })
94
+ .filter(Boolean);
95
+ }
96
+
97
+ module.exports = {
98
+ TOOL_DIR,
99
+ scan_tool_names,
100
+ read_tool_config,
101
+ function_tool_config,
102
+ list_tools,
103
+ get_tool_config,
104
+ get_function_tool,
105
+ list_function_tools
106
+ };
package/package.json CHANGED
@@ -1,8 +1,7 @@
1
1
  {
2
2
  "name": "mm_datas",
3
- "version": "5.3.1",
4
- "description": "基于 mm_admin 实例池的通用数据管理模块:*_model.json 决定集合,标准 JSON Schema(draft-07 子集,禁用 int/integer)由 mm_validator 校验;静态数据经 mm_jsondb 的 Store 文件层(pad:6 由模块自身补零)按 data/<集合>/000001.json 逐条落盘,动态数据走 mm_cache 缓存 + mm_mongodb 持久,mm_sql 作为可选数据源适配器;四个依赖统一经 jsondbAdmin/cacheAdmin/mongodbAdmin/sqlAdmin 获取;工具参数契约采用 OpenAI function calling 的 parameters 格式",
5
-
3
+ "version": "5.3.2",
4
+ "description": "基于 mm_admin 实例池的通用数据管理模块:*_model.json 决定集合,标准 JSON Schema(draft-07 子集,禁用 int/integer)由 mm_validator 校验;静态数据经 mm_jsondb 的 Store 文件层(pad:6 由模块自身补零)按 data/<集合>/000001.json 逐条落盘,动态数据走 mm_cache 缓存 + 本地文件快照,显式启用 mm_mongodb / mm_sql 后再同步外部副本;四个依赖均为硬依赖,连接或调用失败直接抛错;工具参数契约采用 OpenAI function calling 的 parameters 格式",
6
5
  "main": "index.js",
7
6
  "repository": {
8
7
  "type": "git",