mm_config 2.8.0 → 2.9.0

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/index.js CHANGED
@@ -10,13 +10,12 @@ module.exports = {
10
10
  Config,
11
11
  admin,
12
12
  configAdmin(scope, config) {
13
- return admin.get(scope || 'os', config);
13
+ const name = scope || 'os';
14
+ const existed = admin.has(name);
15
+ const instance = admin.get(name, config);
16
+ if (!existed && typeof instance.restore === 'function') {
17
+ instance.restore();
18
+ }
19
+ return instance;
14
20
  }
15
21
  };
16
-
17
- /* mm-tool-logic-export 工具能力层:其它模块可 require("<pkg>").tools.<name> 直接引用 */
18
- Object.defineProperty(module.exports, 'tools', { enumerable: true, configurable: true,
19
- get: function () { return require('./lib/tools'); } });
20
-
21
- /* S1 能力下沉:业务实现可在模块之间直接引用(不经过工具注册面) */
22
- Object.defineProperty(module.exports, 'runPathLogic', { enumerable: true, value: require('./lib/run_path_logic.js') });
package/lib/conf.js CHANGED
@@ -1,11 +1,20 @@
1
1
  const { Base } = require('mm_mod');
2
- const { push, fullname, hasFile, loadJson, saveJson } = require('mm_helper');
2
+ const { push } = require('mm_helper');
3
+ const { fullname, hasFile, loadJson, saveJson } = require('mm_files');
4
+ const { varsAdmin } = require('mm_vars');
5
+ const { reg } = require('mm_admin');
3
6
 
4
7
  /**
5
- * 配置字典(私有)
6
- * @type {object}
8
+ * 获取配置缓存字典(可变缓存,存于 mm_vars,键为配置文件全路径)
9
+ * @returns {object} 配置缓存字典
7
10
  */
8
- const dict = {};
11
+ function getDict() {
12
+ const vars = varsAdmin('mm_config');
13
+ if (!vars.has('CONF_DICT')) {
14
+ vars.set('CONF_DICT', {});
15
+ }
16
+ return vars.get('CONF_DICT');
17
+ }
9
18
 
10
19
  /**
11
20
  * 配置类
@@ -268,7 +277,7 @@ Config.prototype.keys = async function () {
268
277
  * 销毁
269
278
  */
270
279
  Config.prototype.destroy = function () {
271
- delete dict[this.config.file];
280
+ delete getDict()[this.config.file];
272
281
  };
273
282
 
274
283
  /**
@@ -294,6 +303,7 @@ function create(file, options = {}, config = {}) {
294
303
  cfg.saveSync(); // 确保文件存在
295
304
  }
296
305
  // 注册配置字典
306
+ const dict = getDict();
297
307
  dict[f] = cfg;
298
308
  return dict[f].getProxy();
299
309
  }
@@ -312,6 +322,7 @@ async function createAsync(file, options = {}, config = {}) {
312
322
  await cfg.saveAsync(); // 确保文件存在
313
323
  }
314
324
  // 注册配置字典
325
+ const dict = getDict();
315
326
  dict[f] = cfg;
316
327
  return dict[f].getAsyncProxy();
317
328
  }
@@ -324,6 +335,7 @@ async function createAsync(file, options = {}, config = {}) {
324
335
  function clear(file) {
325
336
  if (!file) return clearAll();
326
337
  let f = fullname(file);
338
+ let dict = getDict();
327
339
  if (dict[f]) {
328
340
  delete dict[f];
329
341
  }
@@ -333,6 +345,7 @@ function clear(file) {
333
345
  * 清理所有配置缓存
334
346
  */
335
347
  function clearAll() {
348
+ let dict = getDict();
336
349
  Object.keys(dict).forEach(key => {
337
350
  delete dict[key];
338
351
  });
@@ -350,22 +363,20 @@ function conf(options, file, config = {}) {
350
363
  }
351
364
 
352
365
  /**
353
- * 配置实例单例
354
- * @type {Config|null}
355
- */
356
- let config_instance = null;
357
-
358
- /**
359
- * 获取配置单例
366
+ * 获取配置单例(实例由 mm_admin 实例池管理,避免模块级单例变量)
360
367
  * @param {object} options - 配置对象
361
368
  * @param {object} config - 配置选项
362
369
  * @returns {Config} 配置实例
363
370
  */
364
371
  function getConfig(options, config) {
365
- if (!config_instance) {
366
- config_instance = new Config(config, options);
372
+ const admin = reg('config.conf');
373
+ if (!admin.getClass()) {
374
+ admin.setClass(Config);
375
+ }
376
+ if (!admin.has('default')) {
377
+ admin.set('default', new Config(config, options));
367
378
  }
368
- return config_instance;
379
+ return admin.get('default');
369
380
  }
370
381
 
371
382
  // 模块导出
package/lib/config.js CHANGED
@@ -1,4 +1,88 @@
1
1
  const KvStore = require('mm_kv_store').KvStore;
2
+ require('mm_files');
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+
6
+ /**
7
+ * 判断指定目录是否为 mm_os 项目
8
+ * 模块内部能力:工具不得重复实现,应通过 configAdmin 调用。
9
+ * @param {string} dir_path 待检测目录
10
+ * @returns {boolean} 是否为 mm_os 项目
11
+ */
12
+ function _checkIsMmOs(dir_path) {
13
+ // 检测指标1: index.js中存在 MM_os 或 require('mm_os')
14
+ const index_js_path = path.join(dir_path, 'index.js');
15
+ if (fs.existsSync(index_js_path)) {
16
+ try {
17
+ const content = fs.readFileSync(index_js_path, 'utf-8');
18
+ if (content.includes('MM_os') || content.includes("require('mm_os')") || content.includes('require("mm_os")')) {
19
+ return true;
20
+ }
21
+ } catch (e) {
22
+ // 忽略读取错误
23
+ }
24
+ }
25
+
26
+ // 检测指标2: node_modules/mm_os 存在
27
+ const mm_os_module_path = path.join(dir_path, 'node_modules', 'mm_os');
28
+ if (fs.existsSync(mm_os_module_path)) {
29
+ return true;
30
+ }
31
+
32
+ // 检测指标3: app/ 目录 + config/ 目录同时存在(典型mm_os结构)
33
+ const app_path = path.join(dir_path, 'app');
34
+ const config_path = path.join(dir_path, 'config');
35
+ if (fs.existsSync(app_path) && fs.existsSync(config_path)) {
36
+ // 进一步验证:检查app下是否有多个应用模块目录
37
+ try {
38
+ const apps = fs.readdirSync(app_path);
39
+ const has_app_modules = apps.some(name => {
40
+ const app_dir = path.join(app_path, name);
41
+ return fs.statSync(app_dir).isDirectory() &&
42
+ (fs.existsSync(path.join(app_dir, 'app.json')) || fs.existsSync(path.join(app_dir, 'index.js')));
43
+ });
44
+ if (has_app_modules) {
45
+ return true;
46
+ }
47
+ } catch (e) {
48
+ // 忽略
49
+ }
50
+ }
51
+
52
+ return false;
53
+ }
54
+
55
+ /**
56
+ * 获取 mm_os 版本号
57
+ * @param {string} dir_path 项目根目录
58
+ * @returns {string} mm_os 版本号,取不到时为空串
59
+ */
60
+ function _getMmOsVersion(dir_path) {
61
+ try {
62
+ const pkg_path = path.join(dir_path, 'node_modules', 'mm_os', 'package.json');
63
+ if (fs.existsSync(pkg_path)) {
64
+ const pkg = JSON.parse(fs.readFileSync(pkg_path, 'utf-8'));
65
+ return pkg.version || '';
66
+ }
67
+ } catch (e) {
68
+ // 忽略
69
+ }
70
+ return '';
71
+ }
72
+
73
+ /**
74
+ * 获取 mm_os 项目的关键子目录路径
75
+ * @param {string} dir_path 项目根目录
76
+ * @returns {object} 关键子目录路径表
77
+ */
78
+ function _getProjectPaths(dir_path) {
79
+ return {
80
+ app: path.join(dir_path, 'app'),
81
+ config: path.join(dir_path, 'config'),
82
+ com: path.join(dir_path, 'com'),
83
+ static: path.join(dir_path, 'static')
84
+ };
85
+ }
2
86
 
3
87
  /**
4
88
  * 固定配置存储类,仅允许在系统启动时设置一次,设置后不能修改
@@ -29,6 +113,148 @@ class Config extends KvStore {
29
113
  _isLoadOverwritable() {
30
114
  return false;
31
115
  }
116
+
117
+ /**
118
+ * 获取当前运行根目录及 mm_os 项目信息
119
+ * 工具 get_run_path 通过 configAdmin 调用本方法,自身不重复实现检测逻辑。
120
+ * @param {boolean} check_only 仅返回 is_mm_os 判断,不返回版本与子目录
121
+ * @returns {object} 运行路径信息
122
+ */
123
+ getRunPath(check_only) {
124
+ try {
125
+ const run_path = process.cwd() + path.sep;
126
+ const is_mm_os = _checkIsMmOs(run_path);
127
+
128
+ const result = {
129
+ run_path: run_path,
130
+ is_mm_os: is_mm_os,
131
+ code: 0,
132
+ msg: is_mm_os ? '当前为mm_os项目' : '当前不是mm_os项目'
133
+ };
134
+
135
+ if (is_mm_os && !check_only) {
136
+ result.mm_os_version = _getMmOsVersion(run_path);
137
+ result.paths = {};
138
+ for (const [key, dir_path] of Object.entries(_getProjectPaths(run_path))) {
139
+ if (fs.existsSync(dir_path)) {
140
+ result.paths[key] = dir_path;
141
+ }
142
+ }
143
+ }
144
+
145
+ return result;
146
+ } catch (error) {
147
+ return {
148
+ run_path: process.cwd(),
149
+ is_mm_os: false,
150
+ code: -1,
151
+ msg: '获取运行路径失败: ' + error.message,
152
+ error: {
153
+ name: error.name,
154
+ message: error.message,
155
+ stack: error.stack
156
+ }
157
+ };
158
+ }
159
+ }
32
160
  }
33
161
 
34
- module.exports = Config;
162
+ /**
163
+ * 获取配置文件路径(模块内部能力,工具不得重复声明)
164
+ * @param {object} params 步骤参数
165
+ * @param {object} host 调用方宿主(保留工具原有的 this 语义)
166
+ * @returns {string} 配置文件路径
167
+ */
168
+ function _getFilePath(params, host) {
169
+ var opts = (host && host.config && host.config.options) || {};
170
+ var default_path = './config';
171
+ if (
172
+ opts.file_path &&
173
+ typeof opts.file_path === 'object' &&
174
+ opts.file_path.default !== undefined
175
+ ) {
176
+ default_path = opts.file_path.default;
177
+ } else if (opts.file_path) {
178
+ default_path = opts.file_path;
179
+ }
180
+ return params.file_path || default_path;
181
+ }
182
+
183
+ /**
184
+ * 配置读写能力:供 config 工具通过 configAdmin 调用。
185
+ * 工具只负责参数路由,不再在自身文件内声明实现。
186
+ * @param {object} params 步骤参数
187
+ * @param {object} ctx 上下文对象
188
+ * @param {object} host 调用方宿主
189
+ * @returns {object} 操作结果
190
+ */
191
+ Config.prototype.runConfig = async function (params, ctx, host) {
192
+ var action = params.action || 'get';
193
+ var key = params.key;
194
+ var value = params.value;
195
+ var scope = params.scope || 'default';
196
+
197
+ var file_path = _getFilePath(params, host);
198
+ // 从磁盘加载已有配置,避免每次 create 新建空配置导致数据丢失
199
+ var loaded = {};
200
+ try {
201
+ loaded = file_path.loadJson() || {};
202
+ } catch (_e) {
203
+ void _e;
204
+ }
205
+ var config = require('./conf.js').create(file_path, loaded);
206
+
207
+ var scope_obj;
208
+ switch (action) {
209
+ case 'get':
210
+ scope_obj = config[scope] || {};
211
+ ctx.output = { obj: scope_obj[key] };
212
+ break;
213
+ case 'list':
214
+ scope_obj = config[scope] || {};
215
+ ctx.output = { list: Object.keys(scope_obj), count: Object.keys(scope_obj).length };
216
+ break;
217
+ case 'values':
218
+ scope_obj = config[scope] || {};
219
+ ctx.output = { list: Object.values(scope_obj), count: Object.keys(scope_obj).length };
220
+ break;
221
+ case 'set':
222
+ scope_obj = config[scope] || {};
223
+ scope_obj[key] = value;
224
+ config[scope] = scope_obj;
225
+ ctx.output = { msg: '配置设置成功' };
226
+ break;
227
+ case 'update':
228
+ scope_obj = config[scope] || {};
229
+ scope_obj[key] = { ...(scope_obj[key] || {}), ...value };
230
+ config[scope] = scope_obj;
231
+ ctx.output = { msg: '配置更新成功' };
232
+ break;
233
+ case 'clear':
234
+ config[scope] = {};
235
+ ctx.output = { msg: '配置清空成功' };
236
+ break;
237
+ case 'del':
238
+ scope_obj = config[scope] || {};
239
+ if (key in scope_obj) {
240
+ delete scope_obj[key];
241
+ config[scope] = scope_obj;
242
+ ctx.output = { msg: '配置删除成功' };
243
+ } else {
244
+ ctx.output = { msg: '配置删除失败' };
245
+ }
246
+ break;
247
+ case 'has':
248
+ scope_obj = config[scope] || {};
249
+ ctx.output = { obj: key in scope_obj };
250
+ break;
251
+ default:
252
+ ctx.output = {
253
+ error: { code: 'MGR_UNKNOWN_ACTION', message: '不支持的操作: ' + action }
254
+ };
255
+ }
256
+
257
+ return ctx.output;
258
+ };
259
+
260
+ module.exports = Config;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_config",
3
- "version": "2.8.0",
3
+ "version": "2.9.0",
4
4
  "description": "Super Meimei Configuration Synchronizer - Automatically sync configuration changes to JSON files",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -27,8 +27,8 @@
27
27
  "settings",
28
28
  "automation"
29
29
  ],
30
- "layer": "infrastructure",
31
- "group": "state",
30
+ "layer": "store",
31
+ "group": "data",
32
32
  "author": "邱文武",
33
33
  "license": "ISC",
34
34
  "bugs": {
@@ -50,10 +50,11 @@
50
50
  "dependencies": {
51
51
  "mm_admin": "^1.1.0",
52
52
  "mm_eslint": "^1.0.0",
53
+ "mm_files": "^1.4.0",
53
54
  "mm_helper": "^1.0.0",
54
55
  "mm_kv_store": "^1.0.0",
55
56
  "mm_mod": "^1.0.1",
56
- "mm_os": "^1.0.0"
57
+ "mm_vars": "^1.1.0"
57
58
  },
58
59
  "devDependencies": {
59
60
  "eslint": "^10.9.1",
@@ -1,7 +1,61 @@
1
- 'use strict';
2
1
  /**
3
- * config —— 工具契约适配层
4
- * 业务实现归位于模块 lib/tools/config/,本文件只把模块能力暴露给加载器。
5
- * 其它模块可直接 require('mm_config').tools.config 复用。
2
+ * config - 配置管理器
3
+ *
4
+ * 架构约定:本文件只保留 main(params, ctx, options) 作为 LLM 参数路由,
5
+ * 真实读写能力由模块根 index.js 导出的 configAdmin 提供(LLM 操作模块的途径)。
6
+ * 工具内不得直接 require 底层库、不得另行声明业务函数。
6
7
  */
7
- module.exports = require('../../lib/tools/config');
8
+ const { configAdmin } = require('../../index.js');
9
+
10
+ /**
11
+ * 配置操作入口
12
+ * @param {object} params 步骤参数
13
+ * @param {object} ctx 上下文对象
14
+ * @param {object} options 运行选项
15
+ * @returns {object} 操作结果
16
+ */
17
+ async function main(params, ctx, options) {
18
+ void options;
19
+ const p = params || {};
20
+ const config = configAdmin(p.scope || 'os');
21
+ if (p.action === 'batch') {
22
+ return _runBatch(config, p, ctx, this);
23
+ }
24
+ return config.runConfig(p, ctx, this);
25
+ }
26
+
27
+ /**
28
+ * 批量写入配置项,逐项复用模块的 set 能力
29
+ * @param {object} config 配置实例
30
+ * @param {object} p 步骤参数
31
+ * @param {object} ctx 上下文对象
32
+ * @param {object} host 调用方宿主
33
+ * @returns {object} 操作结果
34
+ */
35
+ async function _runBatch(config, p, ctx, host) {
36
+ const items = p.items;
37
+ if (!items || typeof items !== 'object' || Array.isArray(items)) {
38
+ ctx.output = {
39
+ code: 10000,
40
+ msg: '执行失败:items 必须是配置项对象',
41
+ error: { name: 'TypeError', message: 'items 必须是配置项对象' }
42
+ };
43
+ return ctx.output;
44
+ }
45
+ try {
46
+ const keys = Object.keys(items);
47
+ for (const key of keys) {
48
+ await config.runConfig({ ...p, action: 'set', key, value: items[key] }, ctx, host);
49
+ }
50
+ ctx.output = { code: 0, msg: '批量设置成功', count: keys.length, keys };
51
+ } catch (error) {
52
+ ctx.output = {
53
+ code: 10000,
54
+ msg: '执行失败:' + error.message,
55
+ error: { name: error.name, message: error.message }
56
+ };
57
+ }
58
+ return ctx.output;
59
+ }
60
+
61
+ module.exports = { main };
@@ -2,14 +2,14 @@
2
2
  "name": "config",
3
3
  "handler": "manager",
4
4
  "title": "配置管理器",
5
- "description": "管理系统配置项的读取与更新。适用场景:需要动态读写全局或分组的键值配置时。工作流提示:action 选 get/set/update/list/values/clear/del/has,scope 指定分组作用域,key 指定配置键、value 传配置值,读取结果在 obj/list、写入结果在 msg。",
5
+ "description": "管理系统配置项的读取与更新。适用场景:需要动态读写全局或分组的键值配置时。工作流提示:action 选 get/set/update/list/values/clear/del/has/batch,scope 指定分组作用域,key 指定配置键、value 传配置值,batch 用 items 一次写入多个配置项,读取结果在 obj/list、写入结果在 msg。",
6
6
  "type": "data",
7
7
  "params": {
8
8
  "type": "object",
9
9
  "properties": {
10
10
  "action": {
11
11
  "type": "string",
12
- "description": "操作类型: get/set/update/list/values/clear/del/has",
12
+ "description": "操作类型: get/set/update/list/values/clear/del/has/batch",
13
13
  "enum": [
14
14
  "get",
15
15
  "set",
@@ -18,7 +18,8 @@
18
18
  "values",
19
19
  "clear",
20
20
  "del",
21
- "has"
21
+ "has",
22
+ "batch"
22
23
  ]
23
24
  },
24
25
  "scope": {
@@ -32,6 +33,10 @@
32
33
  },
33
34
  "value": {
34
35
  "description": "配置值(set时必填)"
36
+ },
37
+ "items": {
38
+ "type": "object",
39
+ "description": "批量配置项对象(batch时必填,键为配置键、值为配置值)"
35
40
  }
36
41
  },
37
42
  "required": [
@@ -51,11 +56,15 @@
51
56
  },
52
57
  "count": {
53
58
  "type": "number",
54
- "description": "列表条目数(list 操作)"
59
+ "description": "列表条目数(list 操作)或批量写入条目数(batch 操作)"
60
+ },
61
+ "keys": {
62
+ "type": "array",
63
+ "description": "批量写入的配置键(batch 操作)"
55
64
  },
56
65
  "msg": {
57
66
  "type": "string",
58
- "description": "写操作的结果描述(set/del 操作)"
67
+ "description": "写操作的结果描述(set/del/batch 操作)"
59
68
  }
60
69
  }
61
70
  },
@@ -1,7 +1,24 @@
1
- 'use strict';
2
1
  /**
3
- * get_run_path —— 工具契约适配层
4
- * 业务实现归位于模块 lib/tools/get_run_path/,本文件只把模块能力暴露给加载器。
5
- * 其它模块可直接 require('mm_config').tools.get_run_path 复用。
2
+ * get_run_path - 获取当前程序运行根目录
3
+ *
4
+ * 架构约定:本文件只保留 main(params, ctx, options) 作为 LLM 参数路由,
5
+ * 真实检测能力由模块根 index.js 导出的 configAdmin 提供(LLM 操作模块的途径)。
6
+ * 工具内不得直接 require 底层库、不得另行声明业务函数。
6
7
  */
7
- module.exports = require('../../lib/tools/get_run_path');
8
+ const { configAdmin } = require('../../index.js');
9
+
10
+ /**
11
+ * 主函数
12
+ * @param {object} params 步骤参数
13
+ * @param {object} ctx 上下文对象
14
+ * @param {object} options 运行选项
15
+ * @returns {object} 运行路径信息
16
+ */
17
+ async function main(params, ctx, options) {
18
+ void ctx;
19
+ void options;
20
+ const config = configAdmin((params && params.scope) || 'os');
21
+ return config.getRunPath(!!(params && params.check_only === true));
22
+ }
23
+
24
+ module.exports = { main };
@@ -1,7 +1,147 @@
1
- 'use strict';
2
1
  /**
3
- * path —— 工具契约适配层
4
- * 业务实现归位于模块 lib/tools/path/,本文件只把模块能力暴露给加载器。
5
- * 其它模块可直接 require('mm_config').tools.path 复用。
2
+ * 路径查询器
3
+ * 获取应用运行路径、当前模块根路径、项目模块目录等关键路径,
4
+ * 支持相对路径转绝对路径与存在性判断
5
+ * 运行路径读取本包 configAdmin('os') 的 run_path 配置,缺省回落到 process.cwd()
6
+ * @param {object} params 步骤参数
7
+ * @param {object} ctx 上下文对象
8
+ * @param {object} options 模块配置
9
+ * @returns {object} 查询结果
6
10
  */
7
- module.exports = require('../../lib/tools/path');
11
+ const path = require('path');
12
+ const fs = require('fs');
13
+ const { configAdmin } = require('../../index.js');
14
+
15
+ /**
16
+ * 获取应用运行路径
17
+ * @returns {Promise<string>} 应用运行路径
18
+ */
19
+ async function _getRunPath() {
20
+ var run_path = (await configAdmin('os').get('run_path')) || '';
21
+ return run_path || process.cwd();
22
+ }
23
+
24
+ /**
25
+ * 获取当前模块根路径
26
+ * 优先取模块 options.self_path 配置,缺省回落到应用运行路径
27
+ * @param {object} mod 模块实例(this)
28
+ * @returns {Promise<string>} 模块根路径
29
+ */
30
+ async function _getSelfPath(mod) {
31
+ var opts = (mod && typeof mod.getOptions === 'function' && mod.getOptions()) || {};
32
+ if (opts.self_path) return opts.self_path;
33
+ return await _getRunPath();
34
+ }
35
+
36
+ /**
37
+ * 获取应用运行路径下的子目录
38
+ * @param {string} sub 子路径
39
+ * @returns {Promise<string>} 子目录绝对路径
40
+ */
41
+ async function _getAppSubPath(sub) {
42
+ return path.join(await _getRunPath(), sub);
43
+ }
44
+
45
+ /**
46
+ * 相对路径转绝对路径,绝对路径原样返回
47
+ * base 为 self 时基于当前模块根路径解析,否则基于应用运行路径
48
+ * @param {string} target 目标路径
49
+ * @param {string} base 基准目录: run/self
50
+ * @param {string} self_path 当前模块根路径
51
+ * @returns {Promise<string>} 绝对路径
52
+ */
53
+ async function _getAbsolute(target, base, self_path) {
54
+ if (path.isAbsolute(target)) return target;
55
+ var root = base === 'self' ? self_path : await _getRunPath();
56
+ return path.resolve(root, target);
57
+ }
58
+
59
+ /**
60
+ * 判断路径是否存在
61
+ * @param {string} file_path 文件路径
62
+ * @returns {boolean} 是否存在
63
+ */
64
+ function _exists(file_path) {
65
+ try {
66
+ return fs.existsSync(file_path);
67
+ } catch (_e) {
68
+ void _e;
69
+ return false;
70
+ }
71
+ }
72
+
73
+ /**
74
+ * 获取全部关键路径
75
+ * @param {string} self_path 当前模块根路径
76
+ * @returns {Promise<object>} 路径集合对象
77
+ */
78
+ async function _getAllPaths(self_path) {
79
+ var run_path = await _getRunPath();
80
+ return {
81
+ run_path: run_path,
82
+ module_dir: path.join(run_path, 'common', 'mod'),
83
+ workflow_dir: path.join(run_path, 'common', 'workflow'),
84
+ script_dir: path.join(run_path, 'common', 'script'),
85
+ templates_dir: path.join(run_path, 'templates'),
86
+ data_dir: path.join(run_path, 'data'),
87
+ self_path: self_path,
88
+ self_module_dir: path.join(self_path, 'common', 'mod'),
89
+ self_templates_dir: path.join(self_path, 'templates')
90
+ };
91
+ }
92
+
93
+ async function main(params, ctx, options) {
94
+ var action = params.action || 'info';
95
+ var target = params.target || '';
96
+ var base = params.base || 'run';
97
+ var self_path = await _getSelfPath(this);
98
+
99
+ switch (action) {
100
+ case 'info':
101
+ ctx.output = await _getAllPaths(self_path);
102
+ break;
103
+ case 'run':
104
+ ctx.output = { path: await _getRunPath() };
105
+ break;
106
+ case 'module':
107
+ ctx.output = { path: await _getAppSubPath('common/mod') };
108
+ break;
109
+ case 'workflow':
110
+ ctx.output = { path: await _getAppSubPath('common/workflow') };
111
+ break;
112
+ case 'script':
113
+ ctx.output = { path: await _getAppSubPath('common/script') };
114
+ break;
115
+ case 'templates':
116
+ ctx.output = { path: await _getAppSubPath('templates') };
117
+ break;
118
+ case 'data':
119
+ ctx.output = { path: await _getAppSubPath('data') };
120
+ break;
121
+ case 'self':
122
+ ctx.output = { path: self_path };
123
+ break;
124
+ case 'self_module':
125
+ ctx.output = { path: path.join(self_path, 'common', 'mod') };
126
+ break;
127
+ case 'self_templates':
128
+ ctx.output = { path: path.join(self_path, 'templates') };
129
+ break;
130
+ case 'absolute': {
131
+ var abs_path = await _getAbsolute(target, base, self_path);
132
+ ctx.output = { path: abs_path, exists: _exists(abs_path) };
133
+ break;
134
+ }
135
+ case 'exists':
136
+ ctx.output = { obj: _exists(await _getAbsolute(target, base, self_path)) };
137
+ break;
138
+ default:
139
+ ctx.output = {
140
+ error: { code: 'QUERY_UNKNOWN_ACTION', message: '不支持的操作: ' + action }
141
+ };
142
+ }
143
+
144
+ return ctx.output;
145
+ }
146
+
147
+ module.exports = { main };
@@ -1,134 +0,0 @@
1
- /**
2
- * get_run_path - 获取当前程序运行根目录
3
- *
4
- * 检测当前运行路径是否为mm_os项目,若是则返回关键路径信息,
5
- * 方便智能体直接在当前项目下开发插件/应用/API等模块。
6
- */
7
- const fs = require('fs');
8
- const path = require('path');
9
-
10
- /**
11
- * 判断指定目录是否为mm_os项目
12
- */
13
- function checkIsMmOs(dir_path) {
14
- // 检测指标1: index.js中存在 MM_os 或 require('mm_os')
15
- const index_js_path = path.join(dir_path, 'index.js');
16
- if (fs.existsSync(index_js_path)) {
17
- try {
18
- const content = fs.readFileSync(index_js_path, 'utf-8');
19
- if (content.includes('MM_os') || content.includes("require('mm_os')") || content.includes('require("mm_os")')) {
20
- return true;
21
- }
22
- } catch (e) {
23
- // 忽略读取错误
24
- }
25
- }
26
-
27
- // 检测指标2: node_modules/mm_os 存在
28
- const mm_os_module_path = path.join(dir_path, 'node_modules', 'mm_os');
29
- if (fs.existsSync(mm_os_module_path)) {
30
- return true;
31
- }
32
-
33
- // 检测指标3: app/ 目录 + config/ 目录同时存在(典型mm_os结构)
34
- const app_path = path.join(dir_path, 'app');
35
- const config_path = path.join(dir_path, 'config');
36
- if (fs.existsSync(app_path) && fs.existsSync(config_path)) {
37
- // 进一步验证:检查app下是否有多个应用模块目录
38
- try {
39
- const apps = fs.readdirSync(app_path);
40
- const has_app_modules = apps.some(name => {
41
- const app_dir = path.join(app_path, name);
42
- return fs.statSync(app_dir).isDirectory() &&
43
- (fs.existsSync(path.join(app_dir, 'app.json')) || fs.existsSync(path.join(app_dir, 'index.js')));
44
- });
45
- if (has_app_modules) {
46
- return true;
47
- }
48
- } catch (e) {
49
- // 忽略
50
- }
51
- }
52
-
53
- return false;
54
- }
55
-
56
- /**
57
- * 获取mm_os版本号
58
- */
59
- function getMmOsVersion(dir_path) {
60
- try {
61
- const pkg_path = path.join(dir_path, 'node_modules', 'mm_os', 'package.json');
62
- if (fs.existsSync(pkg_path)) {
63
- const pkg = JSON.parse(fs.readFileSync(pkg_path, 'utf-8'));
64
- return pkg.version || '';
65
- }
66
- } catch (e) {
67
- // 忽略
68
- }
69
- return '';
70
- }
71
-
72
- /**
73
- * 获取mm_os项目的关键子目录路径
74
- */
75
- function getProjectPaths(dir_path) {
76
- return {
77
- app: path.join(dir_path, 'app'),
78
- config: path.join(dir_path, 'config'),
79
- com: path.join(dir_path, 'com'),
80
- static: path.join(dir_path, 'static')
81
- };
82
- }
83
-
84
- /**
85
- * 主函数
86
- */
87
- async function main(params, ctx, options) {
88
- try {
89
- const check_only = params && params.check_only === true;
90
- const run_path = process.cwd() + path.sep;
91
-
92
- // 检测是否为mm_os项目
93
- const is_mm_os = checkIsMmOs(run_path);
94
-
95
- let result = {
96
- run_path: run_path,
97
- is_mm_os: is_mm_os,
98
- code: 0,
99
- msg: is_mm_os ? '当前为mm_os项目' : '当前不是mm_os项目'
100
- };
101
-
102
- if (is_mm_os && !check_only) {
103
- // 获取mm_os版本
104
- result.mm_os_version = getMmOsVersion(run_path);
105
-
106
- // 获取关键子目录路径
107
- const sub_paths = getProjectPaths(run_path);
108
- result.paths = {};
109
-
110
- // 只返回实际存在的目录
111
- for (const [key, dir_path] of Object.entries(sub_paths)) {
112
- if (fs.existsSync(dir_path)) {
113
- result.paths[key] = dir_path;
114
- }
115
- }
116
- }
117
-
118
- return result;
119
- } catch (error) {
120
- return {
121
- run_path: process.cwd(),
122
- is_mm_os: false,
123
- code: -1,
124
- msg: '获取运行路径失败: ' + error.message,
125
- error: {
126
- name: error.name,
127
- message: error.message,
128
- stack: error.stack
129
- }
130
- };
131
- }
132
- }
133
-
134
- exports.main = main;
@@ -1,100 +0,0 @@
1
- const mm_config = require('mm_config');
2
-
3
- /**
4
- * 配置管理器
5
- * 基于 mm_config 实现文件持久化配置,支持 scope 命名空间
6
- * @param {object} ctx 上下文对象
7
- * @param {object} params 步骤参数
8
- */
9
- async function main(params, ctx, options) {
10
- var action = params.action || 'get';
11
- var key = params.key;
12
- var value = params.value;
13
- var scope = params.scope || 'default';
14
-
15
- var file_path = _getFilePath(params, this);
16
- // 从磁盘加载已有配置,避免每次 create 新建空配置导致数据丢失
17
- var loaded = {};
18
- try {
19
- loaded = file_path.loadJson() || {};
20
- } catch (_e) {
21
- void _e;
22
- }
23
- var config = mm_config.create(file_path, loaded);
24
-
25
- var scope_obj;
26
- switch (action) {
27
- case 'get':
28
- scope_obj = config[scope] || {};
29
- ctx.output = { obj: scope_obj[key] };
30
- break;
31
- case 'list':
32
- scope_obj = config[scope] || {};
33
- ctx.output = { list: Object.keys(scope_obj), count: Object.keys(scope_obj).length };
34
- break;
35
- case 'values':
36
- scope_obj = config[scope] || {};
37
- ctx.output = { list: Object.values(scope_obj), count: Object.keys(scope_obj).length };
38
- break;
39
- case 'set':
40
- scope_obj = config[scope] || {};
41
- scope_obj[key] = value;
42
- config[scope] = scope_obj;
43
- ctx.output = { msg: '配置设置成功' };
44
- break;
45
- case 'update':
46
- scope_obj = config[scope] || {};
47
- scope_obj[key] = { ...(scope_obj[key] || {}), ...value };
48
- config[scope] = scope_obj;
49
- ctx.output = { msg: '配置更新成功' };
50
- break;
51
- case 'clear':
52
- config[scope] = {};
53
- ctx.output = { msg: '配置清空成功' };
54
- break;
55
- case 'del':
56
- scope_obj = config[scope] || {};
57
- if (key in scope_obj) {
58
- delete scope_obj[key];
59
- config[scope] = scope_obj;
60
- ctx.output = { msg: '配置删除成功' };
61
- } else {
62
- ctx.output = { msg: '配置删除失败' };
63
- }
64
- break;
65
- case 'has':
66
- scope_obj = config[scope] || {};
67
- ctx.output = { obj: key in scope_obj };
68
- break;
69
- default:
70
- ctx.output = {
71
- error: { code: 'MGR_UNKNOWN_ACTION', message: '不支持的操作: ' + action }
72
- };
73
- }
74
-
75
- return ctx.output;
76
- }
77
-
78
- /**
79
- * 获取配置文件路径
80
- * 优先从 params.file_path 获取,fallback 到模块 options 配置
81
- * @param {object} params 步骤参数
82
- * @param {object} ctx_this 模块上下文
83
- * @returns {string} 配置文件路径
84
- */
85
- function _getFilePath(params, ctx_this) {
86
- var opts = (ctx_this && ctx_this.config && ctx_this.config.options) || {};
87
- var default_path = './config';
88
- if (
89
- opts.file_path &&
90
- typeof opts.file_path === 'object' &&
91
- opts.file_path.default !== undefined
92
- ) {
93
- default_path = opts.file_path.default;
94
- } else if (opts.file_path) {
95
- default_path = opts.file_path;
96
- }
97
- return params.file_path || default_path;
98
- }
99
-
100
- module.exports = { main };
@@ -1,7 +0,0 @@
1
- 'use strict';
2
- /**
3
- * get_run_path —— 工具契约适配层
4
- * 业务实现归位于模块 lib/run_path_logic.js,本文件只把模块能力暴露给工具加载器。
5
- * 其它模块可直接 require('mm_config').runPathLogic 复用,无需经过工具注册面。
6
- */
7
- module.exports = require('../../run_path_logic.js');
@@ -1,16 +0,0 @@
1
- 'use strict';
2
- const fs = require('fs');
3
- const path = require('path');
4
- /** 模块能力层:懒加载工具实现,避免模块入口被 require 时加载全部实现 */
5
- module.exports = new Proxy({}, {
6
- get(_t, key) {
7
- if (typeof key !== 'string') return undefined;
8
- if (!fs.existsSync(path.join(__dirname, key, 'index.js'))) return undefined;
9
- return require('./' + key);
10
- },
11
- has(_t, key) {
12
- return typeof key === 'string' && fs.existsSync(path.join(__dirname, key, 'index.js'));
13
- },
14
- ownKeys() { return fs.readdirSync(__dirname, { withFileTypes: true }).filter(e => e.isDirectory()).map(e => e.name); },
15
- getOwnPropertyDescriptor(_t, key) { return this.has(_t, key) ? { enumerable: true, configurable: true } : undefined; }
16
- });
@@ -1,147 +0,0 @@
1
- /**
2
- * 路径查询器
3
- * 获取应用运行路径、当前模块根路径、项目模块目录等关键路径,
4
- * 支持相对路径转绝对路径与存在性判断
5
- * 运行路径读取本包 configAdmin('os') 的 run_path 配置,缺省回落到 process.cwd()
6
- * @param {object} params 步骤参数
7
- * @param {object} ctx 上下文对象
8
- * @param {object} options 模块配置
9
- * @returns {object} 查询结果
10
- */
11
- const path = require('path');
12
- const fs = require('fs');
13
- const { configAdmin } = require('../../../index.js');
14
-
15
- /**
16
- * 获取应用运行路径
17
- * @returns {Promise<string>} 应用运行路径
18
- */
19
- async function _getRunPath() {
20
- var run_path = (await configAdmin('os').get('run_path')) || '';
21
- return run_path || process.cwd();
22
- }
23
-
24
- /**
25
- * 获取当前模块根路径
26
- * 优先取模块 options.self_path 配置,缺省回落到应用运行路径
27
- * @param {object} mod 模块实例(this)
28
- * @returns {Promise<string>} 模块根路径
29
- */
30
- async function _getSelfPath(mod) {
31
- var opts = (mod && typeof mod.getOptions === 'function' && mod.getOptions()) || {};
32
- if (opts.self_path) return opts.self_path;
33
- return await _getRunPath();
34
- }
35
-
36
- /**
37
- * 获取应用运行路径下的子目录
38
- * @param {string} sub 子路径
39
- * @returns {Promise<string>} 子目录绝对路径
40
- */
41
- async function _getAppSubPath(sub) {
42
- return path.join(await _getRunPath(), sub);
43
- }
44
-
45
- /**
46
- * 相对路径转绝对路径,绝对路径原样返回
47
- * base 为 self 时基于当前模块根路径解析,否则基于应用运行路径
48
- * @param {string} target 目标路径
49
- * @param {string} base 基准目录: run/self
50
- * @param {string} self_path 当前模块根路径
51
- * @returns {Promise<string>} 绝对路径
52
- */
53
- async function _getAbsolute(target, base, self_path) {
54
- if (path.isAbsolute(target)) return target;
55
- var root = base === 'self' ? self_path : await _getRunPath();
56
- return path.resolve(root, target);
57
- }
58
-
59
- /**
60
- * 判断路径是否存在
61
- * @param {string} file_path 文件路径
62
- * @returns {boolean} 是否存在
63
- */
64
- function _exists(file_path) {
65
- try {
66
- return fs.existsSync(file_path);
67
- } catch (_e) {
68
- void _e;
69
- return false;
70
- }
71
- }
72
-
73
- /**
74
- * 获取全部关键路径
75
- * @param {string} self_path 当前模块根路径
76
- * @returns {Promise<object>} 路径集合对象
77
- */
78
- async function _getAllPaths(self_path) {
79
- var run_path = await _getRunPath();
80
- return {
81
- run_path: run_path,
82
- module_dir: path.join(run_path, 'common', 'mod'),
83
- workflow_dir: path.join(run_path, 'common', 'workflow'),
84
- script_dir: path.join(run_path, 'common', 'script'),
85
- templates_dir: path.join(run_path, 'templates'),
86
- data_dir: path.join(run_path, 'data'),
87
- self_path: self_path,
88
- self_module_dir: path.join(self_path, 'common', 'mod'),
89
- self_templates_dir: path.join(self_path, 'templates')
90
- };
91
- }
92
-
93
- async function main(params, ctx, options) {
94
- var action = params.action || 'info';
95
- var target = params.target || '';
96
- var base = params.base || 'run';
97
- var self_path = await _getSelfPath(this);
98
-
99
- switch (action) {
100
- case 'info':
101
- ctx.output = await _getAllPaths(self_path);
102
- break;
103
- case 'run':
104
- ctx.output = { path: await _getRunPath() };
105
- break;
106
- case 'module':
107
- ctx.output = { path: await _getAppSubPath('common/mod') };
108
- break;
109
- case 'workflow':
110
- ctx.output = { path: await _getAppSubPath('common/workflow') };
111
- break;
112
- case 'script':
113
- ctx.output = { path: await _getAppSubPath('common/script') };
114
- break;
115
- case 'templates':
116
- ctx.output = { path: await _getAppSubPath('templates') };
117
- break;
118
- case 'data':
119
- ctx.output = { path: await _getAppSubPath('data') };
120
- break;
121
- case 'self':
122
- ctx.output = { path: self_path };
123
- break;
124
- case 'self_module':
125
- ctx.output = { path: path.join(self_path, 'common', 'mod') };
126
- break;
127
- case 'self_templates':
128
- ctx.output = { path: path.join(self_path, 'templates') };
129
- break;
130
- case 'absolute': {
131
- var abs_path = await _getAbsolute(target, base, self_path);
132
- ctx.output = { path: abs_path, exists: _exists(abs_path) };
133
- break;
134
- }
135
- case 'exists':
136
- ctx.output = { obj: _exists(await _getAbsolute(target, base, self_path)) };
137
- break;
138
- default:
139
- ctx.output = {
140
- error: { code: 'QUERY_UNKNOWN_ACTION', message: '不支持的操作: ' + action }
141
- };
142
- }
143
-
144
- return ctx.output;
145
- }
146
-
147
- module.exports = { main };