mm_filter 1.0.0 → 1.0.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/README.md +10 -1
- package/index.js +8 -15
- package/lib/tools/filter_hook/index.js +51 -0
- package/lib/tools/index.js +16 -0
- package/package.json +4 -4
- package/test.js +1 -1
- package/tool/filter_hook/index.js +7 -0
- package/tool/filter_hook/tool.json +82 -0
package/README.md
CHANGED
|
@@ -38,7 +38,7 @@ filter.delFilter('content', 'upper');
|
|
|
38
38
|
filter.clear('content'); // 清空指定过滤
|
|
39
39
|
filter.clear(); // 清空全部
|
|
40
40
|
|
|
41
|
-
// 通过 Admin 工厂按作用域取实例(需安装
|
|
41
|
+
// 通过 Admin 工厂按作用域取实例(需安装 mm_admin)
|
|
42
42
|
var admin = filterAdmin('os', { /* config */ });
|
|
43
43
|
```
|
|
44
44
|
|
|
@@ -64,3 +64,12 @@ var admin = filterAdmin('os', { /* config */ });
|
|
|
64
64
|
|
|
65
65
|
`mm_tpl` 内部的 `Hook` 类是本模块(以及 `mm_action`、`mm_func`)的组合门面:
|
|
66
66
|
模板渲染中 `m.hookFilter(name, content)` 的调用最终由 `mm_filter` 执行。
|
|
67
|
+
|
|
68
|
+
## 工具契约(LLM 可调用)
|
|
69
|
+
|
|
70
|
+
本模块 `tool/` 下登记以下工具契约,实现位于 `lib/tools/<name>/`,`tool/<name>/index.js` 仅为薄适配层。
|
|
71
|
+
其它模块可直接 `require('<pkg>').tools.<name>` 复用实现,无需经过工具注册面。
|
|
72
|
+
|
|
73
|
+
| 工具名 | 标题 | 说明 |
|
|
74
|
+
| --- | --- | --- |
|
|
75
|
+
| `filter_hook` | 过滤钩子查询与管道执行 | 过滤清单、管道执行、删除、清空 |
|
package/index.js
CHANGED
|
@@ -1,16 +1,8 @@
|
|
|
1
1
|
const Filter = require('./lib/filter');
|
|
2
|
-
|
|
3
|
-
try {
|
|
4
|
-
Admin = require('mm_corejs').Admin;
|
|
5
|
-
} catch (e) {
|
|
6
|
-
// mm_corejs 未安装时仅提供核心类,Admin 工厂不可用
|
|
7
|
-
}
|
|
2
|
+
const { reg } = require('mm_admin');
|
|
8
3
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
admin = new Admin();
|
|
12
|
-
admin.setClass(Filter);
|
|
13
|
-
}
|
|
4
|
+
const admin = reg('mm_filter');
|
|
5
|
+
admin.setClass(Filter);
|
|
14
6
|
|
|
15
7
|
module.exports = {
|
|
16
8
|
Filter,
|
|
@@ -19,9 +11,10 @@ module.exports = {
|
|
|
19
11
|
if (!admin) {
|
|
20
12
|
throw new Error('mm_filter: mm_corejs 未安装,Admin 工厂不可用');
|
|
21
13
|
}
|
|
22
|
-
|
|
23
|
-
scope = 'os';
|
|
24
|
-
}
|
|
25
|
-
return admin.get(scope, config);
|
|
14
|
+
return admin.get(scope || 'os', config);
|
|
26
15
|
}
|
|
27
16
|
};
|
|
17
|
+
|
|
18
|
+
/* mm-tool-logic-export 工具能力层:其它模块可 require("mm_filter").tools.<name> 直接引用 */
|
|
19
|
+
Object.defineProperty(module.exports, 'tools', { enumerable: true, configurable: true,
|
|
20
|
+
get: function () { return require('./lib/tools'); } });
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* filter_hook —— 过滤钩子查询与管道执行工具
|
|
4
|
+
* 复用模块 Filter 实例的过滤注册表,管道语义由 runFilter 维护。
|
|
5
|
+
*/
|
|
6
|
+
const { filterAdmin } = require('../../../index.js');
|
|
7
|
+
|
|
8
|
+
async function main(params, ctx, options) {
|
|
9
|
+
void options;
|
|
10
|
+
const p = params || {};
|
|
11
|
+
const action = p.action || 'list';
|
|
12
|
+
const done = (data) => { ctx.output = data; return data; };
|
|
13
|
+
|
|
14
|
+
try {
|
|
15
|
+
const inst = filterAdmin(p.scope || 'os');
|
|
16
|
+
|
|
17
|
+
if (action === 'list') {
|
|
18
|
+
const names = inst.listFilters(p.name);
|
|
19
|
+
return done({ code: 0, msg: '成功', name: p.name || '', count: names.length, list: names });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (action === 'has') {
|
|
23
|
+
if (!p.name) throw new TypeError('name 不能为空');
|
|
24
|
+
return done({ code: 0, msg: '成功', name: p.name, exists: inst.hasFilter(p.name) });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (action === 'run') {
|
|
28
|
+
if (!p.name) throw new TypeError('name 不能为空');
|
|
29
|
+
const args = Array.isArray(p.args) ? p.args : [];
|
|
30
|
+
const value = inst.runFilter(p.name, p.value, ...args);
|
|
31
|
+
return done({ code: 0, msg: '执行完成', name: p.name, value });
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (action === 'del') {
|
|
35
|
+
if (!p.name || !p.alias) throw new TypeError('del 需要 name 与 alias');
|
|
36
|
+
const ok = inst.delFilter(p.name, p.alias);
|
|
37
|
+
return done({ code: 0, msg: ok ? '删除成功' : '别名不存在', name: p.name, alias: p.alias, deleted: !!ok });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (action === 'clear') {
|
|
41
|
+
inst.clear(p.name);
|
|
42
|
+
return done({ code: 0, msg: '已清空', name: p.name || '' });
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return done({ code: 10001, msg: '不支持的操作: ' + action });
|
|
46
|
+
} catch (error) {
|
|
47
|
+
return done({ code: 10000, msg: '执行失败:' + error.message, error: { name: error.name, message: error.message } });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
module.exports = { main };
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_filter",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "过滤钩子管理模块:提供过滤钩子 addFilter/delFilter/runFilter 的注册与管道执行能力",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"repository": {
|
|
@@ -14,8 +14,8 @@
|
|
|
14
14
|
"hook",
|
|
15
15
|
"过滤"
|
|
16
16
|
],
|
|
17
|
-
"layer": "
|
|
18
|
-
"group": "
|
|
17
|
+
"layer": "shared",
|
|
18
|
+
"group": "hooks",
|
|
19
19
|
"author": "邱文武",
|
|
20
20
|
"license": "ISC",
|
|
21
21
|
"engines": {
|
|
@@ -25,6 +25,6 @@
|
|
|
25
25
|
"test": "node test.js"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"
|
|
28
|
+
"mm_admin": "^1.1.0"
|
|
29
29
|
}
|
|
30
30
|
}
|
package/test.js
CHANGED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "filter_hook",
|
|
3
|
+
"title": "过滤钩子查询与管道执行",
|
|
4
|
+
"description": "基于 mm_filter 的过滤钩子能力:列出过滤名、判断是否存在、按管道语义依次执行过滤函数并返回最终值、删除或清空过滤。适用场景:确认某过滤链挂了哪些处理器、用现有过滤链加工一段文本/数据。注意:注册过滤需要传函数,LLM 侧只做查询/执行/清理。",
|
|
5
|
+
"category": "钩子系统",
|
|
6
|
+
"group": "hooks",
|
|
7
|
+
"type": "function",
|
|
8
|
+
"main": "./index.js",
|
|
9
|
+
"params": {
|
|
10
|
+
"type": "object",
|
|
11
|
+
"properties": {
|
|
12
|
+
"action": {
|
|
13
|
+
"type": "string",
|
|
14
|
+
"description": "操作类型: list(列出过滤)/has(判断存在)/run(管道执行)/del(删除过滤)/clear(清空过滤)",
|
|
15
|
+
"enum": [
|
|
16
|
+
"list",
|
|
17
|
+
"has",
|
|
18
|
+
"run",
|
|
19
|
+
"del",
|
|
20
|
+
"clear"
|
|
21
|
+
],
|
|
22
|
+
"default": "list"
|
|
23
|
+
},
|
|
24
|
+
"name": {
|
|
25
|
+
"type": "string",
|
|
26
|
+
"description": "过滤名称(list 可省略表示全部)"
|
|
27
|
+
},
|
|
28
|
+
"alias": {
|
|
29
|
+
"type": "string",
|
|
30
|
+
"description": "过滤别名(del 必填)"
|
|
31
|
+
},
|
|
32
|
+
"value": {
|
|
33
|
+
"description": "待过滤的初始值(run 必填)"
|
|
34
|
+
},
|
|
35
|
+
"args": {
|
|
36
|
+
"type": "array",
|
|
37
|
+
"description": "过滤函数的额外参数(run 可选)"
|
|
38
|
+
},
|
|
39
|
+
"scope": {
|
|
40
|
+
"type": "string",
|
|
41
|
+
"description": "实例作用域,缺省 os",
|
|
42
|
+
"default": "os"
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"required": [
|
|
46
|
+
"action"
|
|
47
|
+
]
|
|
48
|
+
},
|
|
49
|
+
"returns": {
|
|
50
|
+
"type": "object",
|
|
51
|
+
"properties": {
|
|
52
|
+
"code": {
|
|
53
|
+
"type": "number",
|
|
54
|
+
"description": "0 表示成功,1xxxx 表示业务/执行异常"
|
|
55
|
+
},
|
|
56
|
+
"msg": {
|
|
57
|
+
"type": "string",
|
|
58
|
+
"description": "结果描述"
|
|
59
|
+
},
|
|
60
|
+
"list": {
|
|
61
|
+
"type": "array",
|
|
62
|
+
"description": "过滤名称列表或过滤项摘要"
|
|
63
|
+
},
|
|
64
|
+
"count": {
|
|
65
|
+
"type": "number",
|
|
66
|
+
"description": "数量"
|
|
67
|
+
},
|
|
68
|
+
"exists": {
|
|
69
|
+
"type": "boolean",
|
|
70
|
+
"description": "是否存在(has)"
|
|
71
|
+
},
|
|
72
|
+
"value": {
|
|
73
|
+
"description": "过滤后的最终值(run)"
|
|
74
|
+
},
|
|
75
|
+
"error": {
|
|
76
|
+
"type": "object",
|
|
77
|
+
"description": "异常对象 { name, message }"
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
"handler": "manager"
|
|
82
|
+
}
|