mm_matcher 0.0.0-stage → 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/index.js ADDED
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+ /** 工具宿主入口:统一复用 mm_tool_host,避免各模块重复复制扫描骨架 */
3
+ const { createToolHost } = require('mm_tool_host');
4
+
5
+ module.exports = createToolHost(__dirname, { load: true });
6
+
7
+ /* mm-tool-logic-export 工具能力层:其它模块可 require("<pkg>").tools.<name> 直接引用 */
8
+ Object.defineProperty(module.exports, 'tools', { enumerable: true, configurable: true,
9
+ get: function () { return require('./lib/tools'); } });
@@ -0,0 +1,21 @@
1
+ const path = require('path');
2
+
3
+ /**
4
+ * 精确匹配
5
+ * @param {object} ctx 上下文对象
6
+ * @param {object} params 步骤参数
7
+ */
8
+ async function main(params, ctx, options) {
9
+ var source = params.source || '';
10
+ var target = params.target || '';
11
+ var matched = source === target;
12
+ ctx.output = {
13
+ matched: matched,
14
+ tip: matched ? '精确匹配成功' : '精确匹配失败',
15
+ source: source,
16
+ target: target
17
+ };
18
+ return ctx.output;
19
+ }
20
+
21
+ module.exports = { main };
@@ -0,0 +1,22 @@
1
+ const path = require('path');
2
+
3
+ /**
4
+ * 模糊匹配
5
+ * @param {object} ctx 上下文对象
6
+ * @param {object} params 步骤参数
7
+ */
8
+ async function main(params, ctx, options) {
9
+ var query = params.query || '';
10
+ var target = params.target || '';
11
+ var matched = target.indexOf(query) !== -1;
12
+ ctx.output = {
13
+ matched: matched,
14
+ tip: matched ? '模糊匹配成功' : '模糊匹配失败',
15
+ query: query,
16
+ target: target,
17
+ method: 'indexOf'
18
+ };
19
+ return ctx.output;
20
+ }
21
+
22
+ 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
+ });
@@ -0,0 +1,88 @@
1
+ /**
2
+ * MQTT主题匹配器
3
+ * 遵循 MQTT 通配规范: + 匹配单层, # 匹配零个或多个末级层级
4
+ * @param {object} ctx 上下文对象
5
+ * @param {object} params 步骤参数
6
+ */
7
+ const MULTI_WILDCARD = '#';
8
+
9
+ const SINGLE_WILDCARD = '+';
10
+
11
+ const LEVEL_SEPARATOR = '/';
12
+
13
+ /**
14
+ * 校验主题滤波器是否合法
15
+ * 通配符必须独占一层, 且 # 只能出现在末级
16
+ * @param {string} pattern 主题滤波器
17
+ * @returns {boolean} 合法返回 true
18
+ */
19
+ function _isValidFilter(pattern) {
20
+ var levels = String(pattern).split(LEVEL_SEPARATOR);
21
+ for (var i = 0; i < levels.length; i++) {
22
+ var level = levels[i];
23
+ if (level === MULTI_WILDCARD) {
24
+ if (i !== levels.length - 1) return false;
25
+ continue;
26
+ }
27
+ if (level.indexOf(MULTI_WILDCARD) !== -1) return false;
28
+ if (level === SINGLE_WILDCARD) continue;
29
+ if (level.indexOf(SINGLE_WILDCARD) !== -1) return false;
30
+ }
31
+ return true;
32
+ }
33
+
34
+ /**
35
+ * 转义正则元字符
36
+ * @param {string} text 待转义文本
37
+ * @returns {string} 转义结果
38
+ */
39
+ function _escape(text) {
40
+ return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
41
+ }
42
+
43
+ /**
44
+ * 由主题滤波器构造匹配正则
45
+ * # 需同时匹配父层级本身(如 sensor/# 匹配 sensor), 故不与分隔符拼接
46
+ * @param {string} pattern 主题滤波器
47
+ * @returns {RegExp} 匹配正则
48
+ */
49
+ function _buildRegex(pattern) {
50
+ var levels = String(pattern).split(LEVEL_SEPARATOR);
51
+ var source = '';
52
+ for (var i = 0; i < levels.length; i++) {
53
+ var level = levels[i];
54
+ if (level === MULTI_WILDCARD) {
55
+ source += i === 0 ? '.*' : '(?:/.*)?';
56
+ continue;
57
+ }
58
+ if (i > 0) source += LEVEL_SEPARATOR;
59
+ source += level === SINGLE_WILDCARD ? '[^/]*' : _escape(level);
60
+ }
61
+ return new RegExp('^' + source + '$');
62
+ }
63
+
64
+ async function main(params, ctx, options) {
65
+ var source = params.source || '';
66
+ var pattern = params.pattern || '';
67
+
68
+ if (!_isValidFilter(pattern)) {
69
+ ctx.output = {
70
+ matched: false,
71
+ tip: 'Topic滤波器不合法: ' + pattern,
72
+ topic: source,
73
+ pattern: pattern
74
+ };
75
+ return ctx.output;
76
+ }
77
+
78
+ var result = _buildRegex(pattern).test(String(source));
79
+ ctx.output = {
80
+ matched: result,
81
+ tip: result ? 'Topic匹配成功' : 'Topic匹配失败',
82
+ topic: source,
83
+ pattern: pattern
84
+ };
85
+ return ctx.output;
86
+ }
87
+
88
+ module.exports = { main };
@@ -0,0 +1,21 @@
1
+ const path = require('path');
2
+
3
+ /**
4
+ * 前缀匹配
5
+ * @param {object} ctx 上下文对象
6
+ * @param {object} params 步骤参数
7
+ */
8
+ async function main(params, ctx, options) {
9
+ const source = params.source || '';
10
+ const target = params.target || '';
11
+ const result = String(source).indexOf(String(target)) === 0;
12
+ ctx.output = {
13
+ matched: result,
14
+ tip: result ? '前缀匹配成功' : '前缀匹配失败',
15
+ source: source,
16
+ prefix_list: [target]
17
+ };
18
+ return ctx.output;
19
+ }
20
+
21
+ module.exports = { main };
@@ -0,0 +1,85 @@
1
+ /**
2
+ * 推荐匹配器
3
+ * 通用相似度推荐匹配
4
+ */
5
+ async function main(params, ctx, options) {
6
+ var source = params.source || '';
7
+ var pool = params.pool || [];
8
+ var options = params.options || {};
9
+ var limit = options.limit || 10;
10
+
11
+ if (!source || pool.length === 0) {
12
+ ctx.output = {
13
+ matched: false,
14
+ tip: '无推荐结果',
15
+ items: [],
16
+ source: source,
17
+ total: pool.length,
18
+ algo: 'token_hit'
19
+ };
20
+ return ctx.output;
21
+ }
22
+
23
+ var source_words = tokenize(source);
24
+ var scored = [];
25
+
26
+ for (var i = 0; i < pool.length; i++) {
27
+ var item = pool[i];
28
+ var name = item.name || '';
29
+ var tags = item.tags || [];
30
+ var desc = item.desc || '';
31
+ var target_text = name + ' ' + desc;
32
+
33
+ var tag_text = '';
34
+ if (Array.isArray(tags)) {
35
+ tag_text = tags.join(' ');
36
+ }
37
+
38
+ var text_score = calcHitScore(source_words, target_text);
39
+ var tag_score = calcHitScore(source_words, tag_text) * 0.5;
40
+
41
+ var score = text_score + tag_score;
42
+
43
+ scored.push({
44
+ id: item.id,
45
+ name: item.name || '',
46
+ score: score
47
+ });
48
+ }
49
+
50
+ scored.sort(function (a, b) {
51
+ return b.score - a.score;
52
+ });
53
+ var results = scored.slice(0, limit);
54
+
55
+ ctx.output = {
56
+ matched: true,
57
+ tip: '找到 ' + results.length + ' 个推荐',
58
+ items: results,
59
+ source: source,
60
+ total: pool.length,
61
+ algo: 'token_hit'
62
+ };
63
+ return ctx.output;
64
+ }
65
+
66
+ function tokenize(text) {
67
+ if (!text) return [];
68
+ return text.split(/[\s,,、。;;]+/).filter(function (w) {
69
+ return w.length > 0;
70
+ });
71
+ }
72
+
73
+ function calcHitScore(source_words, target_text) {
74
+ if (!target_text || source_words.length === 0) return 0;
75
+ var lower_target = target_text.toLowerCase();
76
+ var hits = 0;
77
+ for (var i = 0; i < source_words.length; i++) {
78
+ if (lower_target.indexOf(source_words[i].toLowerCase()) !== -1) {
79
+ hits++;
80
+ }
81
+ }
82
+ return hits / source_words.length;
83
+ }
84
+
85
+ module.exports = { main };
@@ -0,0 +1,31 @@
1
+ /**
2
+ * 正则匹配
3
+ * @param {object} ctx 上下文对象
4
+ * @param {object} params 步骤参数
5
+ */
6
+ async function main(params, ctx, options) {
7
+ var pattern = params.pattern || '';
8
+ var text = params.text || '';
9
+
10
+ var regex;
11
+ try {
12
+ regex = new RegExp(pattern);
13
+ } catch (_) {
14
+ ctx.output = {
15
+ matched: false,
16
+ tip: '正则表达式无效: ' + pattern,
17
+ error: { code: 'MATCH_REGEX_INVALID', message: '正则表达式无效: ' + pattern }
18
+ };
19
+ return ctx.output;
20
+ }
21
+ var result = regex.test(text);
22
+ ctx.output = {
23
+ matched: result,
24
+ tip: result ? '正则匹配成功' : '正则匹配失败',
25
+ pattern: pattern,
26
+ input: text
27
+ };
28
+ return ctx.output;
29
+ }
30
+
31
+ module.exports = { main };
@@ -0,0 +1,35 @@
1
+ /**
2
+ * 路由路径匹配器
3
+ * @param {object} ctx 上下文对象
4
+ * @param {object} params 步骤参数
5
+ */
6
+ async function main(params, ctx, options) {
7
+ const source = params.source || '';
8
+ const pattern = params.pattern || '';
9
+
10
+ const source_parts = source.split('/').filter(Boolean);
11
+ const pattern_parts = pattern.split('/').filter(Boolean);
12
+ if (source_parts.length !== pattern_parts.length) {
13
+ ctx.output = { matched: false, tip: '路由匹配失败', path: source, params: {} };
14
+ return ctx.output;
15
+ }
16
+ let matched = true;
17
+ const route_params = {};
18
+ for (let i = 0; i < pattern_parts.length; i++) {
19
+ if (pattern_parts[i].startsWith(':')) {
20
+ route_params[pattern_parts[i].slice(1)] = source_parts[i];
21
+ } else if (pattern_parts[i] !== source_parts[i]) {
22
+ matched = false;
23
+ break;
24
+ }
25
+ }
26
+ ctx.output = {
27
+ matched: matched,
28
+ tip: matched ? '路由匹配成功' : '路由匹配失败',
29
+ path: source,
30
+ params: route_params
31
+ };
32
+ return ctx.output;
33
+ }
34
+
35
+ module.exports = { main };
@@ -0,0 +1,152 @@
1
+ /**
2
+ * 规则引擎
3
+ * 用 global.__rule_sets 持久化规则集
4
+ */
5
+ async function main(params, ctx, options) {
6
+ var action = params.action;
7
+ var rule_name = params.rule_name || '';
8
+ var conditions = params.conditions || [];
9
+ var operator = params.operator || 'and';
10
+ var fact = params.fact || {};
11
+
12
+ if (!global.__rule_sets) {
13
+ global.__rule_sets = {};
14
+ }
15
+
16
+ var rules_count = Object.keys(global.__rule_sets).length;
17
+
18
+ switch (action) {
19
+ case 'add_rule': {
20
+ global.__rule_sets[rule_name] = {
21
+ rule_name: rule_name,
22
+ conditions: conditions,
23
+ operator: operator
24
+ };
25
+ ctx.output = {
26
+ msg: '规则执行完成',
27
+ results: { rule_name: rule_name, added: true },
28
+ rules_count: Object.keys(global.__rule_sets).length
29
+ };
30
+ return ctx.output;
31
+ }
32
+ case 'evaluate': {
33
+ var stored = global.__rule_sets[rule_name];
34
+ var eval_conditions;
35
+ var eval_operator;
36
+ if (stored) {
37
+ eval_conditions = stored.conditions;
38
+ eval_operator = stored.operator;
39
+ } else {
40
+ eval_conditions = conditions;
41
+ eval_operator = operator;
42
+ }
43
+ var matched = evaluateConditions(eval_conditions, eval_operator, fact);
44
+ ctx.output = {
45
+ msg: '规则执行完成',
46
+ results: { rule_name: rule_name, matched: matched, fact: fact },
47
+ rules_count: rules_count
48
+ };
49
+ return ctx.output;
50
+ }
51
+ case 'remove_rule': {
52
+ delete global.__rule_sets[rule_name];
53
+ ctx.output = {
54
+ msg: '规则执行完成',
55
+ results: { rule_name: rule_name, removed: true },
56
+ rules_count: Object.keys(global.__rule_sets).length
57
+ };
58
+ return ctx.output;
59
+ }
60
+ case 'list_rules': {
61
+ var rule_names = Object.keys(global.__rule_sets);
62
+ ctx.output = {
63
+ msg: '规则执行完成',
64
+ results: { rules: rule_names },
65
+ rules_count: rule_names.length
66
+ };
67
+ return ctx.output;
68
+ }
69
+ default: {
70
+ ctx.output = {
71
+ error: {
72
+ code: 'EXEC_UNKNOWN_ACTION',
73
+ message: '未知操作: ' + action
74
+ }
75
+ };
76
+ return ctx.output;
77
+ }
78
+ }
79
+ }
80
+
81
+ function evaluateConditions(conditions, operator, fact) {
82
+ if (!conditions || conditions.length === 0) {
83
+ return true;
84
+ }
85
+ var matched = true;
86
+ for (var i = 0; i < conditions.length; i++) {
87
+ var cond = conditions[i];
88
+ var fact_value = fact[cond.field];
89
+ var passed;
90
+ switch (cond.operator) {
91
+ case '=':
92
+ passed = fact_value === cond.value;
93
+ break;
94
+ case '!=':
95
+ passed = fact_value !== cond.value;
96
+ break;
97
+ case '>':
98
+ passed = fact_value > cond.value;
99
+ break;
100
+ case '>=':
101
+ passed = fact_value >= cond.value;
102
+ break;
103
+ case '<':
104
+ passed = fact_value < cond.value;
105
+ break;
106
+ case '<=':
107
+ passed = fact_value <= cond.value;
108
+ break;
109
+ case 'in':
110
+ passed = (cond.value || []).indexOf(fact_value) >= 0;
111
+ break;
112
+ case 'between':
113
+ passed = fact_value >= (cond.value || [])[0] && fact_value <= (cond.value || [])[1];
114
+ break;
115
+ case 'not_in':
116
+ passed = (cond.value || []).indexOf(fact_value) === -1;
117
+ break;
118
+ case 'contains':
119
+ passed = String(fact_value).indexOf(String(cond.value)) !== -1;
120
+ break;
121
+ case 'starts_with':
122
+ passed = String(fact_value).startsWith(String(cond.value));
123
+ break;
124
+ case 'ends_with':
125
+ passed = String(fact_value).endsWith(String(cond.value));
126
+ break;
127
+ case 'regex':
128
+ try {
129
+ passed = new RegExp(String(cond.value)).test(String(fact_value));
130
+ } catch (_) {
131
+ passed = false;
132
+ }
133
+ break;
134
+ default:
135
+ passed = true;
136
+ }
137
+ if (operator === 'and' && !passed) {
138
+ matched = false;
139
+ break;
140
+ }
141
+ if (operator === 'or' && passed) {
142
+ matched = true;
143
+ break;
144
+ }
145
+ if (i === conditions.length - 1 && operator === 'or') {
146
+ matched = passed;
147
+ }
148
+ }
149
+ return matched;
150
+ }
151
+
152
+ module.exports = { main };
@@ -0,0 +1,53 @@
1
+ const path = require('path');
2
+
3
+ /**
4
+ * 相似度匹配器 - 计算两个字符串的相似度
5
+ * @param {object} ctx 上下文对象
6
+ * @param {object} params 步骤参数
7
+ */
8
+ async function main(params, ctx, options) {
9
+ const source = params.source || '';
10
+ const target = params.target || '';
11
+ const threshold = params.threshold || 0.5;
12
+ const source_str = String(source);
13
+ const target_str = String(target);
14
+ const max_len = Math.max(source_str.length, target_str.length);
15
+ let distance = 0;
16
+ if (max_len === 0) {
17
+ ctx.output.similarity = 1;
18
+ } else {
19
+ const matrix = [];
20
+ for (let i = 0; i <= source_str.length; i++) {
21
+ matrix[i] = [i];
22
+ }
23
+ for (let j = 0; j <= target_str.length; j++) {
24
+ matrix[0][j] = j;
25
+ }
26
+ for (let i = 1; i <= source_str.length; i++) {
27
+ for (let j = 1; j <= target_str.length; j++) {
28
+ const cost = source_str[i - 1] === target_str[j - 1] ? 0 : 1;
29
+ matrix[i][j] = Math.min(
30
+ matrix[i - 1][j] + 1,
31
+ matrix[i][j - 1] + 1,
32
+ matrix[i - 1][j - 1] + cost
33
+ );
34
+ }
35
+ }
36
+ distance = matrix[source_str.length][target_str.length];
37
+ ctx.output.similarity = 1 - distance / max_len;
38
+ }
39
+ ctx.output.distance = distance;
40
+ const score = ctx.output.similarity;
41
+ ctx.output = {
42
+ matched: score >= threshold,
43
+ tip: score >= threshold ? '相似度达标' : '相似度不足',
44
+ score: score,
45
+ source: source_str,
46
+ target: target_str,
47
+ method: 'levenshtein',
48
+ threshold: threshold
49
+ };
50
+ return ctx.output;
51
+ }
52
+
53
+ module.exports = { main };
@@ -0,0 +1,25 @@
1
+ /**
2
+ * 通配符匹配
3
+ * @param {object} ctx 上下文对象
4
+ * @param {object} params 步骤参数
5
+ */
6
+ async function main(params, ctx, options) {
7
+ var source = params.source || '';
8
+ var pattern = params.pattern || '';
9
+
10
+ var escaped = String(pattern)
11
+ .replace(/[.+^${}()|[\]\\]/g, '\\$&')
12
+ .replace(/\*/g, '.*')
13
+ .replace(/\?/g, '.');
14
+ var regex = new RegExp('^' + escaped + '$');
15
+ var result = regex.test(String(source));
16
+ ctx.output = {
17
+ matched: result,
18
+ tip: result ? '通配符匹配成功' : '通配符匹配失败',
19
+ pattern: pattern,
20
+ input: String(source)
21
+ };
22
+ return ctx.output;
23
+ }
24
+
25
+ module.exports = { main };
package/package.json CHANGED
@@ -1,6 +1,37 @@
1
1
  {
2
- "name": "mm_matcher",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "mm_matcher",
3
+ "version": "1.0.1",
4
+ "description": "mm_workflow 匹配器工具包:精确、模糊、MQTT主题、前缀、推荐、正则、路由、规则引擎、相似度、通配符 等 10 个匹配工具模块",
5
+ "type": "commonjs",
6
+ "main": "index.js",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://gitee.com/qiuwenwu91/mm_matcher.git"
10
+ },
11
+ "keywords": [
12
+ "mm_mod",
13
+ "operator",
14
+ "matcher",
15
+ "超级美眉",
16
+ "工作流工具"
17
+ ],
18
+ "layer": "shared",
19
+ "group": "operator",
20
+ "operator": "matcher",
21
+ "author": "邱文武",
22
+ "license": "ISC",
23
+ "files": [
24
+ "index.js",
25
+ "lib",
26
+ "tool"
27
+ ],
28
+ "engines": {
29
+ "node": ">=16"
30
+ },
31
+ "scripts": {
32
+ "test": "node -e \"const t=require('./index.js').loadTools();console.log(Object.keys(t).length+' tools loaded')\""
33
+ },
34
+ "dependencies": {
35
+ "mm_tool_host": "^1.0.0"
36
+ }
37
+ }
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+ /**
3
+ * exact —— 工具契约适配层
4
+ * 业务实现归位于模块 lib/tools/exact/,本文件只把模块能力暴露给加载器。
5
+ * 其它模块可直接 require('mm_matcher').tools.exact 复用。
6
+ */
7
+ module.exports = require('../../lib/tools/exact');
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "exact",
3
+ "handler": "matcher",
4
+ "title": "精确匹配器",
5
+ "description": "精确比较 source 与 target 是否完全相等。适用场景:状态值、命令字、ID 等需严格相等的判断,作为分发器分支条件时。工作流提示:source 填待匹配文本、target 填目标文本,结果看 matched(boolean) 与 tip,可配合 dispatcher.condition 实现精确分流。",
6
+ "type": "core",
7
+ "params": {
8
+ "type": "object",
9
+ "properties": {
10
+ "source": {
11
+ "type": "string",
12
+ "description": "源文本"
13
+ },
14
+ "target": {
15
+ "type": "string",
16
+ "description": "目标文本"
17
+ }
18
+ },
19
+ "required": [
20
+ "source",
21
+ "target"
22
+ ]
23
+ },
24
+ "returns": {
25
+ "type": "object",
26
+ "properties": {
27
+ "matched": {
28
+ "type": "boolean",
29
+ "description": "是否精确匹配"
30
+ },
31
+ "tip": {
32
+ "type": "string",
33
+ "description": "匹配结果提示"
34
+ }
35
+ }
36
+ },
37
+ "options": {},
38
+ "group": "text"
39
+ }
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+ /**
3
+ * fuzzy —— 工具契约适配层
4
+ * 业务实现归位于模块 lib/tools/fuzzy/,本文件只把模块能力暴露给加载器。
5
+ * 其它模块可直接 require('mm_matcher').tools.fuzzy 复用。
6
+ */
7
+ module.exports = require('../../lib/tools/fuzzy');
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "fuzzy",
3
+ "handler": "matcher",
4
+ "title": "模糊匹配器",
5
+ "description": "子串模糊匹配,检查 target 是否包含 query(基于 indexOf)。适用场景:关键词命中判断、文本包含关系检查、不做大小写归一化的模糊过滤时。工作流提示:query 填搜索关键词、target 填被搜索文本,结果看 matched(boolean) 与 tip。",
6
+ "type": "core",
7
+ "params": {
8
+ "type": "object",
9
+ "properties": {
10
+ "query": {
11
+ "type": "string",
12
+ "description": "搜索关键词"
13
+ },
14
+ "target": {
15
+ "type": "string",
16
+ "description": "被搜索的目标文本"
17
+ }
18
+ },
19
+ "required": [
20
+ "query",
21
+ "target"
22
+ ]
23
+ },
24
+ "returns": {
25
+ "type": "object",
26
+ "properties": {
27
+ "matched": {
28
+ "type": "boolean",
29
+ "description": "target 是否包含 query"
30
+ },
31
+ "tip": {
32
+ "type": "string",
33
+ "description": "匹配结果提示"
34
+ },
35
+ "method": {
36
+ "type": "string",
37
+ "description": "匹配方法"
38
+ }
39
+ }
40
+ },
41
+ "options": {},
42
+ "group": "text"
43
+ }
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+ /**
3
+ * mqtt_topic —— 工具契约适配层
4
+ * 业务实现归位于模块 lib/tools/mqtt_topic/,本文件只把模块能力暴露给加载器。
5
+ * 其它模块可直接 require('mm_matcher').tools.mqtt_topic 复用。
6
+ */
7
+ module.exports = require('../../lib/tools/mqtt_topic');
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "mqtt_topic",
3
+ "handler": "matcher",
4
+ "title": "MQTT主题匹配器",
5
+ "description": "按 MQTT 主题规则匹配,支持 +(单级)与 #(多级)通配符。适用场景:物联网消息路由、订阅关系校验、报文按主题分发时。工作流提示:source 填实际主题、pattern 填带通配符的模式(如 sensor/+/temperature),结果看 matched(boolean) 与 tip。",
6
+ "type": "gateway",
7
+ "params": {
8
+ "type": "object",
9
+ "properties": {
10
+ "source": {
11
+ "type": "string",
12
+ "description": "待匹配的MQTT主题,如 sensor/living/temperature"
13
+ },
14
+ "pattern": {
15
+ "type": "string",
16
+ "description": "MQTT主题模式,如 sensor/+/temperature"
17
+ }
18
+ },
19
+ "required": [
20
+ "source",
21
+ "pattern"
22
+ ]
23
+ },
24
+ "returns": {
25
+ "type": "object",
26
+ "properties": {
27
+ "matched": {
28
+ "type": "boolean",
29
+ "description": "主题是否匹配模式"
30
+ },
31
+ "tip": {
32
+ "type": "string",
33
+ "description": "匹配结果提示"
34
+ },
35
+ "topic": {
36
+ "type": "string",
37
+ "description": "待匹配的MQTT主题"
38
+ }
39
+ }
40
+ },
41
+ "options": {},
42
+ "group": "route"
43
+ }
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+ /**
3
+ * prefix —— 工具契约适配层
4
+ * 业务实现归位于模块 lib/tools/prefix/,本文件只把模块能力暴露给加载器。
5
+ * 其它模块可直接 require('mm_matcher').tools.prefix 复用。
6
+ */
7
+ module.exports = require('../../lib/tools/prefix');
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "prefix",
3
+ "handler": "matcher",
4
+ "title": "前缀匹配器",
5
+ "description": "检查 source 是否以 target 作为前缀开头。适用场景:路由前缀识别、文件路径归属判断、命名空间匹配时。工作流提示:source 填待匹配文本、target 填前缀字符串,结果看 matched(boolean) 与 tip,prefix_list 返回匹配到的前缀列表。",
6
+ "type": "core",
7
+ "params": {
8
+ "type": "object",
9
+ "properties": {
10
+ "source": {
11
+ "type": "string",
12
+ "description": "待匹配文本"
13
+ },
14
+ "target": {
15
+ "type": "string",
16
+ "description": "前缀字符串"
17
+ }
18
+ },
19
+ "required": [
20
+ "source",
21
+ "target"
22
+ ]
23
+ },
24
+ "returns": {
25
+ "type": "object",
26
+ "properties": {
27
+ "matched": {
28
+ "type": "boolean",
29
+ "description": "source 是否以 target 为前缀"
30
+ },
31
+ "tip": {
32
+ "type": "string",
33
+ "description": "匹配结果提示"
34
+ },
35
+ "prefix_list": {
36
+ "type": "array",
37
+ "description": "前缀列表"
38
+ }
39
+ }
40
+ },
41
+ "options": {},
42
+ "group": "text"
43
+ }
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+ /**
3
+ * recommend —— 工具契约适配层
4
+ * 业务实现归位于模块 lib/tools/recommend/,本文件只把模块能力暴露给加载器。
5
+ * 其它模块可直接 require('mm_matcher').tools.recommend 复用。
6
+ */
7
+ module.exports = require('../../lib/tools/recommend');
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "recommend",
3
+ "handler": "matcher",
4
+ "title": "推荐匹配器",
5
+ "description": "基于相似度的通用推荐,对候选池打分排序后返回 topN 结果。适用场景:内容推荐、相似条目检索、候选排序时。工作流提示:source 填目标 ID 或特征向量、pool 填候选数组,options 传 limit 与 threshold,结果看 items(按相似度降序)与 total。",
6
+ "type": "core",
7
+ "params": {
8
+ "type": "object",
9
+ "properties": {
10
+ "source": {
11
+ "type": "string",
12
+ "description": "待推荐的目标 ID 或特征向量"
13
+ },
14
+ "pool": {
15
+ "type": "array",
16
+ "description": "候选推荐池数据"
17
+ },
18
+ "options": {
19
+ "type": "object",
20
+ "description": "推荐选项(limit 结果数量、threshold 阈值等)"
21
+ }
22
+ },
23
+ "required": [
24
+ "source",
25
+ "pool"
26
+ ]
27
+ },
28
+ "returns": {
29
+ "type": "object",
30
+ "properties": {
31
+ "matched": {
32
+ "type": "boolean",
33
+ "description": "是否有推荐结果"
34
+ },
35
+ "tip": {
36
+ "type": "string",
37
+ "description": "匹配结果提示"
38
+ },
39
+ "items": {
40
+ "type": "array",
41
+ "description": "推荐结果列表(按相似度降序)"
42
+ },
43
+ "total": {
44
+ "type": "number",
45
+ "description": "候选推荐池总数"
46
+ },
47
+ "algo": {
48
+ "type": "string",
49
+ "description": "推荐算法"
50
+ }
51
+ }
52
+ },
53
+ "options": {},
54
+ "group": "smart"
55
+ }
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+ /**
3
+ * regex —— 工具契约适配层
4
+ * 业务实现归位于模块 lib/tools/regex/,本文件只把模块能力暴露给加载器。
5
+ * 其它模块可直接 require('mm_matcher').tools.regex 复用。
6
+ */
7
+ module.exports = require('../../lib/tools/regex');
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "regex",
3
+ "handler": "matcher",
4
+ "title": "正则匹配器",
5
+ "description": "使用正则表达式对文本进行模式匹配。适用场景:日志关键字提取、格式判定、复杂模式命中检查时。工作流提示:text 填待匹配文本、pattern 填正则表达式(JS 语法),结果看 matched(boolean) 与 tip。",
6
+ "type": "core",
7
+ "params": {
8
+ "type": "object",
9
+ "properties": {
10
+ "text": {
11
+ "type": "string",
12
+ "description": "待匹配文本"
13
+ },
14
+ "pattern": {
15
+ "type": "string",
16
+ "description": "正则表达式字符串"
17
+ }
18
+ },
19
+ "required": [
20
+ "text",
21
+ "pattern"
22
+ ]
23
+ },
24
+ "returns": {
25
+ "type": "object",
26
+ "properties": {
27
+ "matched": {
28
+ "type": "boolean",
29
+ "description": "正则是否匹配成功"
30
+ },
31
+ "tip": {
32
+ "type": "string",
33
+ "description": "匹配结果提示"
34
+ },
35
+ "input": {
36
+ "type": "string",
37
+ "description": "待匹配文本"
38
+ }
39
+ }
40
+ },
41
+ "options": {},
42
+ "group": "text"
43
+ }
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+ /**
3
+ * route —— 工具契约适配层
4
+ * 业务实现归位于模块 lib/tools/route/,本文件只把模块能力暴露给加载器。
5
+ * 其它模块可直接 require('mm_matcher').tools.route 复用。
6
+ */
7
+ module.exports = require('../../lib/tools/route');
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "route",
3
+ "handler": "matcher",
4
+ "title": "路由路径匹配器",
5
+ "description": "匹配路由路径模式并提取动态参数(:id 语法)。适用场景:HTTP 接口路由分发、RESTful 路径解析、网关 URL 匹配时。工作流提示:source 填实际请求路径、pattern 填路由模式(如 /api/user/:id),结果看 matched(boolean) 与 params(动态参数对象)。",
6
+ "type": "gateway",
7
+ "params": {
8
+ "type": "object",
9
+ "properties": {
10
+ "source": {
11
+ "type": "string",
12
+ "description": "待匹配的请求路径,如 /api/user/123"
13
+ },
14
+ "pattern": {
15
+ "type": "string",
16
+ "description": "路由模式,如 /api/user/:id"
17
+ }
18
+ },
19
+ "required": [
20
+ "source",
21
+ "pattern"
22
+ ]
23
+ },
24
+ "returns": {
25
+ "type": "object",
26
+ "properties": {
27
+ "matched": {
28
+ "type": "boolean",
29
+ "description": "路径是否匹配路由模式"
30
+ },
31
+ "tip": {
32
+ "type": "string",
33
+ "description": "匹配结果提示"
34
+ },
35
+ "path": {
36
+ "type": "string",
37
+ "description": "待匹配的请求路径"
38
+ },
39
+ "params": {
40
+ "type": "object",
41
+ "description": "提取的动态路径参数,如 {id: '123'}"
42
+ }
43
+ }
44
+ },
45
+ "options": {},
46
+ "group": "route"
47
+ }
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+ /**
3
+ * rule_engine —— 工具契约适配层
4
+ * 业务实现归位于模块 lib/tools/rule_engine/,本文件只把模块能力暴露给加载器。
5
+ * 其它模块可直接 require('mm_matcher').tools.rule_engine 复用。
6
+ */
7
+ module.exports = require('../../lib/tools/rule_engine');
@@ -0,0 +1,172 @@
1
+ {
2
+ "name": "rule_engine",
3
+ "handler": "matcher",
4
+ "title": "规则匹配器",
5
+ "description": "通用条件规则引擎,支持规则注册、条件组合评估与规则管理。适用场景:复杂业务规则判断、多条件复合匹配、规则需动态增删时。工作流提示:add_rule 注册(conditions 每项 {field, operator, value}),evaluate 时填 fact,结果看 results.matched。",
6
+ "type": "core",
7
+ "params": {
8
+ "type": "object",
9
+ "properties": {
10
+ "action": {
11
+ "type": "string",
12
+ "description": "操作类型: add_rule(添加规则) / evaluate(评估) / remove_rule(移除) / list_rules(列出全部)",
13
+ "enum": [
14
+ "add_rule",
15
+ "evaluate",
16
+ "remove_rule",
17
+ "list_rules"
18
+ ]
19
+ },
20
+ "rule_name": {
21
+ "type": "string",
22
+ "description": "规则名称,作为规则的唯一标识"
23
+ },
24
+ "conditions": {
25
+ "type": "array",
26
+ "description": "条件列表,每项结构: { field, operator, value }",
27
+ "items": {
28
+ "type": "object",
29
+ "properties": {
30
+ "field": {
31
+ "type": "string",
32
+ "description": "事实数据中的字段名"
33
+ },
34
+ "operator": {
35
+ "type": "string",
36
+ "description": "比较运算符: =(等于) / !=(不等于) / >(大于) / >=(大等于) / <(小于) / <=(小等于) / in(包含) / not_in(排除) / between(区间) / contains(模糊) / starts_with(前缀) / ends_with(后缀) / regex(正则)",
37
+ "enum": [
38
+ "=",
39
+ "!=",
40
+ ">",
41
+ ">=",
42
+ "<",
43
+ "<=",
44
+ "in",
45
+ "not_in",
46
+ "between",
47
+ "contains",
48
+ "starts_with",
49
+ "ends_with",
50
+ "regex"
51
+ ]
52
+ },
53
+ "value": {
54
+ "description": "比较目标值,in 时为数组,between 时为 [min, max] 数组"
55
+ }
56
+ },
57
+ "required": [
58
+ "field",
59
+ "operator"
60
+ ]
61
+ }
62
+ },
63
+ "operator": {
64
+ "type": "string",
65
+ "description": "多条件组合逻辑: and(全部满足) / or(任一满足)",
66
+ "default": "and",
67
+ "enum": [
68
+ "and",
69
+ "or"
70
+ ]
71
+ },
72
+ "fact": {
73
+ "type": "object",
74
+ "description": "待评估的事实数据对象,键值对形式"
75
+ }
76
+ },
77
+ "required": [
78
+ "action"
79
+ ]
80
+ },
81
+ "returns": {
82
+ "type": "object",
83
+ "description": "根据 action 返回不同结构,统一包含 msg、results、rules_count 字段;异常时返回 error",
84
+ "properties": {
85
+ "msg": {
86
+ "type": "string",
87
+ "description": "固定返回 '规则执行完成'"
88
+ },
89
+ "results": {
90
+ "type": "object",
91
+ "description": "执行结果详情,结构因 action 而异",
92
+ "oneOf": [
93
+ {
94
+ "description": "add_rule 结果",
95
+ "properties": {
96
+ "rule_name": {
97
+ "type": "string"
98
+ },
99
+ "added": {
100
+ "type": "boolean",
101
+ "description": "true"
102
+ }
103
+ }
104
+ },
105
+ {
106
+ "description": "evaluate 结果",
107
+ "properties": {
108
+ "rule_name": {
109
+ "type": "string"
110
+ },
111
+ "matched": {
112
+ "type": "boolean",
113
+ "description": "条件是否全部/任一满足"
114
+ },
115
+ "fact": {
116
+ "type": "object",
117
+ "description": "传入的事实数据"
118
+ }
119
+ }
120
+ },
121
+ {
122
+ "description": "remove_rule 结果",
123
+ "properties": {
124
+ "rule_name": {
125
+ "type": "string"
126
+ },
127
+ "removed": {
128
+ "type": "boolean",
129
+ "description": "true"
130
+ }
131
+ }
132
+ },
133
+ {
134
+ "description": "list_rules 结果",
135
+ "properties": {
136
+ "rules": {
137
+ "type": "array",
138
+ "items": {
139
+ "type": "string"
140
+ },
141
+ "description": "所有已注册的规则名称列表"
142
+ }
143
+ }
144
+ }
145
+ ]
146
+ },
147
+ "rules_count": {
148
+ "type": "number",
149
+ "description": "当前已注册的规则总数"
150
+ },
151
+ "error": {
152
+ "type": "object",
153
+ "description": "错误信息,仅调用出错时返回",
154
+ "properties": {
155
+ "code": {
156
+ "type": "string",
157
+ "description": "固定值 EXEC_UNKNOWN_ACTION",
158
+ "enum": [
159
+ "EXEC_UNKNOWN_ACTION"
160
+ ]
161
+ },
162
+ "message": {
163
+ "type": "string",
164
+ "description": "未知操作: {action}"
165
+ }
166
+ }
167
+ }
168
+ }
169
+ },
170
+ "options": {},
171
+ "group": "smart"
172
+ }
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+ /**
3
+ * similarity —— 工具契约适配层
4
+ * 业务实现归位于模块 lib/tools/similarity/,本文件只把模块能力暴露给加载器。
5
+ * 其它模块可直接 require('mm_matcher').tools.similarity 复用。
6
+ */
7
+ module.exports = require('../../lib/tools/similarity');
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "similarity",
3
+ "handler": "matcher",
4
+ "title": "相似度匹配器",
5
+ "description": "基于编辑距离计算两段文本的相似度,返回 0-1 分数。适用场景:近似匹配、去重判定、纠错提示、模糊搜索排序时。工作流提示:source 与 target 填两段待比较文本,结果看 score(0-1,1 为完全相同)与 matched(boolean)(是否达到阈值)。",
6
+ "type": "core",
7
+ "params": {
8
+ "type": "object",
9
+ "properties": {
10
+ "source": {
11
+ "type": "string",
12
+ "description": "文本1"
13
+ },
14
+ "target": {
15
+ "type": "string",
16
+ "description": "文本2"
17
+ }
18
+ },
19
+ "required": [
20
+ "source",
21
+ "target"
22
+ ]
23
+ },
24
+ "returns": {
25
+ "type": "object",
26
+ "properties": {
27
+ "matched": {
28
+ "type": "boolean",
29
+ "description": "相似度是否达标"
30
+ },
31
+ "tip": {
32
+ "type": "string",
33
+ "description": "匹配结果提示"
34
+ },
35
+ "score": {
36
+ "type": "number",
37
+ "description": "相似度分数 (0-1),1 表示完全相同"
38
+ },
39
+ "method": {
40
+ "type": "string",
41
+ "description": "计算相似度的方法"
42
+ },
43
+ "threshold": {
44
+ "type": "number",
45
+ "description": "相似度阈值"
46
+ }
47
+ }
48
+ },
49
+ "options": {},
50
+ "group": "text"
51
+ }
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+ /**
3
+ * wildcard —— 工具契约适配层
4
+ * 业务实现归位于模块 lib/tools/wildcard/,本文件只把模块能力暴露给加载器。
5
+ * 其它模块可直接 require('mm_matcher').tools.wildcard 复用。
6
+ */
7
+ module.exports = require('../../lib/tools/wildcard');
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "wildcard",
3
+ "handler": "matcher",
4
+ "title": "通配符匹配器",
5
+ "description": "通配符模式匹配,支持 *(匹配任意字符)与 ?(匹配单个字符)。适用场景:文件类型过滤(如 *.js)、日志模式匹配、灵活文本规则命中时。工作流提示:source 填待匹配文本、pattern 填通配符模式(如 *.js 或 file?.txt),结果看 matched(boolean) 与 tip。",
6
+ "type": "core",
7
+ "params": {
8
+ "type": "object",
9
+ "properties": {
10
+ "source": {
11
+ "type": "string",
12
+ "description": "待匹配文本"
13
+ },
14
+ "pattern": {
15
+ "type": "string",
16
+ "description": "通配符模式,如 *.js 或 file?.txt"
17
+ }
18
+ },
19
+ "required": [
20
+ "source",
21
+ "pattern"
22
+ ]
23
+ },
24
+ "returns": {
25
+ "type": "object",
26
+ "properties": {
27
+ "matched": {
28
+ "type": "boolean",
29
+ "description": "文本是否匹配通配符模式"
30
+ },
31
+ "tip": {
32
+ "type": "string",
33
+ "description": "匹配结果提示"
34
+ },
35
+ "input": {
36
+ "type": "string",
37
+ "description": "待匹配文本"
38
+ }
39
+ }
40
+ },
41
+ "options": {},
42
+ "group": "text"
43
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.