foliko 1.0.75 → 1.0.76
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/.claude/settings.local.json +159 -157
- package/cli/bin/foliko.js +12 -12
- package/cli/src/commands/chat.js +143 -143
- package/cli/src/commands/list.js +93 -93
- package/cli/src/index.js +75 -75
- package/cli/src/ui/chat-ui.js +201 -201
- package/cli/src/utils/ansi.js +40 -40
- package/cli/src/utils/markdown.js +292 -292
- package/examples/ambient-example.js +194 -194
- package/examples/basic.js +115 -115
- package/examples/bootstrap.js +121 -121
- package/examples/mcp-example.js +56 -56
- package/examples/skill-example.js +49 -49
- package/examples/test-chat.js +137 -137
- package/examples/test-mcp.js +85 -85
- package/examples/test-reload.js +59 -59
- package/examples/test-telegram.js +50 -50
- package/examples/test-tg-bot.js +45 -45
- package/examples/test-tg-simple.js +47 -47
- package/examples/test-tg.js +62 -62
- package/examples/test-think.js +43 -43
- package/examples/test-web-plugin.js +103 -103
- package/examples/test-weixin-feishu.js +103 -103
- package/examples/workflow.js +158 -158
- package/package.json +1 -1
- package/plugins/ai-plugin.js +102 -102
- package/plugins/ambient-agent/EventWatcher.js +113 -113
- package/plugins/ambient-agent/ExplorerLoop.js +640 -640
- package/plugins/ambient-agent/GoalManager.js +197 -197
- package/plugins/ambient-agent/Reflector.js +95 -95
- package/plugins/ambient-agent/StateStore.js +90 -90
- package/plugins/ambient-agent/constants.js +101 -101
- package/plugins/ambient-agent/index.js +579 -579
- package/plugins/audit-plugin.js +187 -187
- package/plugins/default-plugins.js +662 -662
- package/plugins/email/constants.js +64 -64
- package/plugins/email/handlers.js +461 -461
- package/plugins/email/index.js +278 -278
- package/plugins/email/monitor.js +269 -269
- package/plugins/email/parser.js +138 -138
- package/plugins/email/reply.js +151 -151
- package/plugins/email/utils.js +124 -124
- package/plugins/feishu-plugin.js +481 -481
- package/plugins/file-system-plugin.js +826 -826
- package/plugins/install-plugin.js +199 -199
- package/plugins/python-executor-plugin.js +367 -367
- package/plugins/python-plugin-loader.js +481 -481
- package/plugins/rules-plugin.js +294 -294
- package/plugins/scheduler-plugin.js +691 -691
- package/plugins/session-plugin.js +369 -369
- package/plugins/shell-executor-plugin.js +197 -197
- package/plugins/storage-plugin.js +240 -240
- package/plugins/subagent-plugin.js +845 -845
- package/plugins/telegram-plugin.js +482 -482
- package/plugins/think-plugin.js +345 -345
- package/plugins/tools-plugin.js +196 -196
- package/plugins/web-plugin.js +606 -606
- package/plugins/weixin-plugin.js +545 -545
- package/src/capabilities/index.js +11 -11
- package/src/capabilities/skill-manager.js +609 -609
- package/src/capabilities/workflow-engine.js +1109 -1109
- package/src/core/agent-chat.js +882 -882
- package/src/core/agent.js +892 -892
- package/src/core/framework.js +465 -465
- package/src/core/index.js +19 -19
- package/src/core/plugin-base.js +219 -219
- package/src/core/plugin-manager.js +863 -863
- package/src/core/provider.js +114 -114
- package/src/core/sub-agent-config.js +264 -264
- package/src/core/system-prompt-builder.js +120 -120
- package/src/core/tool-registry.js +517 -517
- package/src/core/tool-router.js +297 -297
- package/src/executors/executor-base.js +58 -58
- package/src/executors/mcp-executor.js +741 -741
- package/src/index.js +25 -25
- package/src/utils/circuit-breaker.js +301 -301
- package/src/utils/error-boundary.js +363 -363
- package/src/utils/error.js +374 -374
- package/src/utils/event-emitter.js +97 -97
- package/src/utils/id.js +133 -133
- package/src/utils/index.js +217 -217
- package/src/utils/logger.js +181 -181
- package/src/utils/plugin-helpers.js +90 -90
- package/src/utils/retry.js +122 -122
- package/src/utils/sandbox.js +292 -292
- package/test/tool-registry-validation.test.js +218 -218
- package/website/script.js +136 -136
- package/foliko-1.0.75.tgz +0 -0
package/plugins/rules-plugin.js
CHANGED
|
@@ -1,295 +1,295 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Rules 规则引擎插件
|
|
3
|
-
* 控制工具调用权限、内容过滤、触发动作
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
const { Plugin } = require('../src/core/plugin-base')
|
|
7
|
-
const { logger } = require('../src/utils/logger')
|
|
8
|
-
const log = logger.child('RulesPlugin')
|
|
9
|
-
const { z } = require('zod')
|
|
10
|
-
const fs = require('fs')
|
|
11
|
-
const path = require('path')
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* 内置规则类型
|
|
15
|
-
*/
|
|
16
|
-
const RuleType = {
|
|
17
|
-
ALLOW: 'allow', // 允许
|
|
18
|
-
DENY: 'deny', // 拒绝
|
|
19
|
-
TRANSFORM: 'transform', // 转换
|
|
20
|
-
RATE_LIMIT: 'rate_limit' // 限流
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* 规则评估结果
|
|
25
|
-
*/
|
|
26
|
-
class RuleResult {
|
|
27
|
-
constructor(allowed, rule = null, transform = null) {
|
|
28
|
-
this.allowed = allowed
|
|
29
|
-
this.rule = rule
|
|
30
|
-
this.transform = transform
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
class RulesPlugin extends Plugin {
|
|
35
|
-
constructor(config = {}) {
|
|
36
|
-
super()
|
|
37
|
-
this.name = 'rules'
|
|
38
|
-
this.version = '1.0.0'
|
|
39
|
-
this.description = '规则引擎插件,用于控制工具调用权限、内容过滤、触发动作'
|
|
40
|
-
this.priority = 25
|
|
41
|
-
this.system = true
|
|
42
|
-
|
|
43
|
-
this.config = {
|
|
44
|
-
rulesDir: config.rulesDir || '.agent/rules',
|
|
45
|
-
autoLoad: config.autoLoad !== false
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
this._framework = null
|
|
49
|
-
this._rules = []
|
|
50
|
-
this._ruleStats = {
|
|
51
|
-
total: 0,
|
|
52
|
-
allowed: 0,
|
|
53
|
-
denied: 0,
|
|
54
|
-
transformed: 0
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
install(framework) {
|
|
59
|
-
this._framework = framework
|
|
60
|
-
return this
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
start(framework) {
|
|
64
|
-
// 自动加载规则文件
|
|
65
|
-
if (this.config.autoLoad && this.config.rulesDir) {
|
|
66
|
-
this.loadFromDirectory(this.config.rulesDir)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// 注册规则管理工具
|
|
70
|
-
framework.registerTool({
|
|
71
|
-
name: 'rules_list',
|
|
72
|
-
description: '列出所有规则',
|
|
73
|
-
inputSchema: z.object({}),
|
|
74
|
-
execute: async () => {
|
|
75
|
-
return {
|
|
76
|
-
success: true,
|
|
77
|
-
rules: this._rules.map(r => ({
|
|
78
|
-
name: r.name,
|
|
79
|
-
type: r.type,
|
|
80
|
-
target: r.target,
|
|
81
|
-
description: r.description,
|
|
82
|
-
enabled: r.enabled
|
|
83
|
-
})),
|
|
84
|
-
total: this._rules.length
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
})
|
|
88
|
-
|
|
89
|
-
framework.registerTool({
|
|
90
|
-
name: 'rules_add',
|
|
91
|
-
description: '添加规则',
|
|
92
|
-
inputSchema: z.object({
|
|
93
|
-
name: z.string().describe('规则名称'),
|
|
94
|
-
type: z.enum(['allow', 'deny', 'transform']).describe('规则类型'),
|
|
95
|
-
target: z.string().describe('目标 (工具名, *, @event-type)'),
|
|
96
|
-
pattern: z.string().optional().describe('匹配模式 (正则表达式)'),
|
|
97
|
-
description: z.string().optional().describe('规则描述'),
|
|
98
|
-
action: z.object({}).optional().describe('规则动作 (transform 时使用)')
|
|
99
|
-
}),
|
|
100
|
-
execute: async (args) => {
|
|
101
|
-
const rule = {
|
|
102
|
-
name: args.name,
|
|
103
|
-
type: args.type,
|
|
104
|
-
target: args.target,
|
|
105
|
-
pattern: args.pattern ? new RegExp(args.pattern) : null,
|
|
106
|
-
description: args.description || '',
|
|
107
|
-
action: args.action || {},
|
|
108
|
-
enabled: true
|
|
109
|
-
}
|
|
110
|
-
this._rules.push(rule)
|
|
111
|
-
return { success: true, rule: rule.name }
|
|
112
|
-
}
|
|
113
|
-
})
|
|
114
|
-
|
|
115
|
-
framework.registerTool({
|
|
116
|
-
name: 'rules_remove',
|
|
117
|
-
description: '移除规则',
|
|
118
|
-
inputSchema: z.object({
|
|
119
|
-
name: z.string().describe('规则名称')
|
|
120
|
-
}),
|
|
121
|
-
execute: async (args) => {
|
|
122
|
-
const index = this._rules.findIndex(r => r.name === args.name)
|
|
123
|
-
if (index === -1) {
|
|
124
|
-
return { success: false, error: 'Rule not found' }
|
|
125
|
-
}
|
|
126
|
-
this._rules.splice(index, 1)
|
|
127
|
-
return { success: true, removed: args.name }
|
|
128
|
-
}
|
|
129
|
-
})
|
|
130
|
-
|
|
131
|
-
framework.registerTool({
|
|
132
|
-
name: 'rules_stats',
|
|
133
|
-
description: '获取规则统计',
|
|
134
|
-
inputSchema: z.object({}),
|
|
135
|
-
execute: async () => {
|
|
136
|
-
return {
|
|
137
|
-
success: true,
|
|
138
|
-
stats: { ...this._ruleStats }
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
})
|
|
142
|
-
|
|
143
|
-
framework.registerTool({
|
|
144
|
-
name: 'rules_test',
|
|
145
|
-
description: '测试规则匹配',
|
|
146
|
-
inputSchema: z.object({
|
|
147
|
-
target: z.string().describe('目标 (工具名或事件类型)'),
|
|
148
|
-
input: z.object({}).optional().describe('输入参数')
|
|
149
|
-
}),
|
|
150
|
-
execute: async (args) => {
|
|
151
|
-
const result = this.evaluate(args.target, args.input || {})
|
|
152
|
-
return {
|
|
153
|
-
success: true,
|
|
154
|
-
target: args.target,
|
|
155
|
-
result: {
|
|
156
|
-
allowed: result.allowed,
|
|
157
|
-
ruleName: result.rule?.name || null,
|
|
158
|
-
ruleType: result.rule?.type || null
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
})
|
|
163
|
-
|
|
164
|
-
return this
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* 从目录加载规则文件
|
|
169
|
-
*/
|
|
170
|
-
loadFromDirectory(rulesDir) {
|
|
171
|
-
const resolvedDir = path.resolve(process.cwd(), rulesDir)
|
|
172
|
-
|
|
173
|
-
if (!fs.existsSync(resolvedDir)) {
|
|
174
|
-
log.info(` Rules directory not found: ${resolvedDir}`)
|
|
175
|
-
return
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
try {
|
|
179
|
-
const files = fs.readdirSync(resolvedDir).filter(f => f.endsWith('.json'))
|
|
180
|
-
|
|
181
|
-
for (const file of files) {
|
|
182
|
-
const filePath = path.join(resolvedDir, file)
|
|
183
|
-
const content = fs.readFileSync(filePath, 'utf-8')
|
|
184
|
-
const rules = JSON.parse(content)
|
|
185
|
-
|
|
186
|
-
for (const rule of rules) {
|
|
187
|
-
if (rule.pattern) {
|
|
188
|
-
rule.pattern = new RegExp(rule.pattern)
|
|
189
|
-
}
|
|
190
|
-
rule.enabled = rule.enabled !== false
|
|
191
|
-
this._rules.push(rule)
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
log.info(` Loaded ${rules.length} rules from ${file}`)
|
|
195
|
-
}
|
|
196
|
-
} catch (err) {
|
|
197
|
-
log.error(` Failed to load rules: ${err.message}`)
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* 评估目标是否符合规则
|
|
203
|
-
* @param {string} target - 目标 (工具名, *, @event-type)
|
|
204
|
-
* @param {Object} input - 输入参数
|
|
205
|
-
* @returns {RuleResult}
|
|
206
|
-
*/
|
|
207
|
-
evaluate(target, input = {}) {
|
|
208
|
-
this._ruleStats.total++
|
|
209
|
-
|
|
210
|
-
// 按优先级排序规则 (先加载的在前)
|
|
211
|
-
for (const rule of this._rules) {
|
|
212
|
-
if (!rule.enabled) continue
|
|
213
|
-
|
|
214
|
-
// 匹配目标
|
|
215
|
-
if (rule.target !== '*' && rule.target !== target) continue
|
|
216
|
-
|
|
217
|
-
// 匹配模式 (如果有)
|
|
218
|
-
if (rule.pattern) {
|
|
219
|
-
const targetStr = typeof input === 'string' ? input : JSON.stringify(input)
|
|
220
|
-
if (!rule.pattern.test(targetStr)) continue
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
// 评估规则
|
|
224
|
-
if (rule.type === RuleType.DENY) {
|
|
225
|
-
this._ruleStats.denied++
|
|
226
|
-
return new RuleResult(false, rule)
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
if (rule.type === RuleType.ALLOW) {
|
|
230
|
-
this._ruleStats.allowed++
|
|
231
|
-
return new RuleResult(true, rule)
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
if (rule.type === RuleType.TRANSFORM) {
|
|
235
|
-
this._ruleStats.transformed++
|
|
236
|
-
return new RuleResult(true, rule, rule.action)
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
// 没有匹配规则,默认允许
|
|
241
|
-
this._ruleStats.allowed++
|
|
242
|
-
return new RuleResult(true)
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
/**
|
|
246
|
-
* 检查工具调用是否允许
|
|
247
|
-
*/
|
|
248
|
-
checkToolCall(toolName, args) {
|
|
249
|
-
return this.evaluate(toolName, args)
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* 检查消息是否允许
|
|
254
|
-
*/
|
|
255
|
-
checkMessage(message, context) {
|
|
256
|
-
return this.evaluate('@message', { message, context })
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
/**
|
|
260
|
-
* 添加规则
|
|
261
|
-
*/
|
|
262
|
-
addRule(rule) {
|
|
263
|
-
this._rules.push(rule)
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* 移除规则
|
|
268
|
-
*/
|
|
269
|
-
removeRule(name) {
|
|
270
|
-
const index = this._rules.findIndex(r => r.name === name)
|
|
271
|
-
if (index !== -1) {
|
|
272
|
-
this._rules.splice(index, 1)
|
|
273
|
-
return true
|
|
274
|
-
}
|
|
275
|
-
return false
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
reload(framework) {
|
|
279
|
-
this._framework = framework
|
|
280
|
-
this._rules = []
|
|
281
|
-
this._ruleStats = { total: 0, allowed: 0, denied: 0, transformed: 0 }
|
|
282
|
-
|
|
283
|
-
if (this.config.autoLoad && this.config.rulesDir) {
|
|
284
|
-
this.loadFromDirectory(this.config.rulesDir)
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
uninstall(framework) {
|
|
289
|
-
this._rules = []
|
|
290
|
-
this._ruleStats = { total: 0, allowed: 0, denied: 0, transformed: 0 }
|
|
291
|
-
this._framework = null
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Rules 规则引擎插件
|
|
3
|
+
* 控制工具调用权限、内容过滤、触发动作
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const { Plugin } = require('../src/core/plugin-base')
|
|
7
|
+
const { logger } = require('../src/utils/logger')
|
|
8
|
+
const log = logger.child('RulesPlugin')
|
|
9
|
+
const { z } = require('zod')
|
|
10
|
+
const fs = require('fs')
|
|
11
|
+
const path = require('path')
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 内置规则类型
|
|
15
|
+
*/
|
|
16
|
+
const RuleType = {
|
|
17
|
+
ALLOW: 'allow', // 允许
|
|
18
|
+
DENY: 'deny', // 拒绝
|
|
19
|
+
TRANSFORM: 'transform', // 转换
|
|
20
|
+
RATE_LIMIT: 'rate_limit' // 限流
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 规则评估结果
|
|
25
|
+
*/
|
|
26
|
+
class RuleResult {
|
|
27
|
+
constructor(allowed, rule = null, transform = null) {
|
|
28
|
+
this.allowed = allowed
|
|
29
|
+
this.rule = rule
|
|
30
|
+
this.transform = transform
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
class RulesPlugin extends Plugin {
|
|
35
|
+
constructor(config = {}) {
|
|
36
|
+
super()
|
|
37
|
+
this.name = 'rules'
|
|
38
|
+
this.version = '1.0.0'
|
|
39
|
+
this.description = '规则引擎插件,用于控制工具调用权限、内容过滤、触发动作'
|
|
40
|
+
this.priority = 25
|
|
41
|
+
this.system = true
|
|
42
|
+
|
|
43
|
+
this.config = {
|
|
44
|
+
rulesDir: config.rulesDir || '.agent/rules',
|
|
45
|
+
autoLoad: config.autoLoad !== false
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
this._framework = null
|
|
49
|
+
this._rules = []
|
|
50
|
+
this._ruleStats = {
|
|
51
|
+
total: 0,
|
|
52
|
+
allowed: 0,
|
|
53
|
+
denied: 0,
|
|
54
|
+
transformed: 0
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
install(framework) {
|
|
59
|
+
this._framework = framework
|
|
60
|
+
return this
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
start(framework) {
|
|
64
|
+
// 自动加载规则文件
|
|
65
|
+
if (this.config.autoLoad && this.config.rulesDir) {
|
|
66
|
+
this.loadFromDirectory(this.config.rulesDir)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// 注册规则管理工具
|
|
70
|
+
framework.registerTool({
|
|
71
|
+
name: 'rules_list',
|
|
72
|
+
description: '列出所有规则',
|
|
73
|
+
inputSchema: z.object({}),
|
|
74
|
+
execute: async () => {
|
|
75
|
+
return {
|
|
76
|
+
success: true,
|
|
77
|
+
rules: this._rules.map(r => ({
|
|
78
|
+
name: r.name,
|
|
79
|
+
type: r.type,
|
|
80
|
+
target: r.target,
|
|
81
|
+
description: r.description,
|
|
82
|
+
enabled: r.enabled
|
|
83
|
+
})),
|
|
84
|
+
total: this._rules.length
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
framework.registerTool({
|
|
90
|
+
name: 'rules_add',
|
|
91
|
+
description: '添加规则',
|
|
92
|
+
inputSchema: z.object({
|
|
93
|
+
name: z.string().describe('规则名称'),
|
|
94
|
+
type: z.enum(['allow', 'deny', 'transform']).describe('规则类型'),
|
|
95
|
+
target: z.string().describe('目标 (工具名, *, @event-type)'),
|
|
96
|
+
pattern: z.string().optional().describe('匹配模式 (正则表达式)'),
|
|
97
|
+
description: z.string().optional().describe('规则描述'),
|
|
98
|
+
action: z.object({}).optional().describe('规则动作 (transform 时使用)')
|
|
99
|
+
}),
|
|
100
|
+
execute: async (args) => {
|
|
101
|
+
const rule = {
|
|
102
|
+
name: args.name,
|
|
103
|
+
type: args.type,
|
|
104
|
+
target: args.target,
|
|
105
|
+
pattern: args.pattern ? new RegExp(args.pattern) : null,
|
|
106
|
+
description: args.description || '',
|
|
107
|
+
action: args.action || {},
|
|
108
|
+
enabled: true
|
|
109
|
+
}
|
|
110
|
+
this._rules.push(rule)
|
|
111
|
+
return { success: true, rule: rule.name }
|
|
112
|
+
}
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
framework.registerTool({
|
|
116
|
+
name: 'rules_remove',
|
|
117
|
+
description: '移除规则',
|
|
118
|
+
inputSchema: z.object({
|
|
119
|
+
name: z.string().describe('规则名称')
|
|
120
|
+
}),
|
|
121
|
+
execute: async (args) => {
|
|
122
|
+
const index = this._rules.findIndex(r => r.name === args.name)
|
|
123
|
+
if (index === -1) {
|
|
124
|
+
return { success: false, error: 'Rule not found' }
|
|
125
|
+
}
|
|
126
|
+
this._rules.splice(index, 1)
|
|
127
|
+
return { success: true, removed: args.name }
|
|
128
|
+
}
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
framework.registerTool({
|
|
132
|
+
name: 'rules_stats',
|
|
133
|
+
description: '获取规则统计',
|
|
134
|
+
inputSchema: z.object({}),
|
|
135
|
+
execute: async () => {
|
|
136
|
+
return {
|
|
137
|
+
success: true,
|
|
138
|
+
stats: { ...this._ruleStats }
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
framework.registerTool({
|
|
144
|
+
name: 'rules_test',
|
|
145
|
+
description: '测试规则匹配',
|
|
146
|
+
inputSchema: z.object({
|
|
147
|
+
target: z.string().describe('目标 (工具名或事件类型)'),
|
|
148
|
+
input: z.object({}).optional().describe('输入参数')
|
|
149
|
+
}),
|
|
150
|
+
execute: async (args) => {
|
|
151
|
+
const result = this.evaluate(args.target, args.input || {})
|
|
152
|
+
return {
|
|
153
|
+
success: true,
|
|
154
|
+
target: args.target,
|
|
155
|
+
result: {
|
|
156
|
+
allowed: result.allowed,
|
|
157
|
+
ruleName: result.rule?.name || null,
|
|
158
|
+
ruleType: result.rule?.type || null
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
return this
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* 从目录加载规则文件
|
|
169
|
+
*/
|
|
170
|
+
loadFromDirectory(rulesDir) {
|
|
171
|
+
const resolvedDir = path.resolve(process.cwd(), rulesDir)
|
|
172
|
+
|
|
173
|
+
if (!fs.existsSync(resolvedDir)) {
|
|
174
|
+
log.info(` Rules directory not found: ${resolvedDir}`)
|
|
175
|
+
return
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
try {
|
|
179
|
+
const files = fs.readdirSync(resolvedDir).filter(f => f.endsWith('.json'))
|
|
180
|
+
|
|
181
|
+
for (const file of files) {
|
|
182
|
+
const filePath = path.join(resolvedDir, file)
|
|
183
|
+
const content = fs.readFileSync(filePath, 'utf-8')
|
|
184
|
+
const rules = JSON.parse(content)
|
|
185
|
+
|
|
186
|
+
for (const rule of rules) {
|
|
187
|
+
if (rule.pattern) {
|
|
188
|
+
rule.pattern = new RegExp(rule.pattern)
|
|
189
|
+
}
|
|
190
|
+
rule.enabled = rule.enabled !== false
|
|
191
|
+
this._rules.push(rule)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
log.info(` Loaded ${rules.length} rules from ${file}`)
|
|
195
|
+
}
|
|
196
|
+
} catch (err) {
|
|
197
|
+
log.error(` Failed to load rules: ${err.message}`)
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* 评估目标是否符合规则
|
|
203
|
+
* @param {string} target - 目标 (工具名, *, @event-type)
|
|
204
|
+
* @param {Object} input - 输入参数
|
|
205
|
+
* @returns {RuleResult}
|
|
206
|
+
*/
|
|
207
|
+
evaluate(target, input = {}) {
|
|
208
|
+
this._ruleStats.total++
|
|
209
|
+
|
|
210
|
+
// 按优先级排序规则 (先加载的在前)
|
|
211
|
+
for (const rule of this._rules) {
|
|
212
|
+
if (!rule.enabled) continue
|
|
213
|
+
|
|
214
|
+
// 匹配目标
|
|
215
|
+
if (rule.target !== '*' && rule.target !== target) continue
|
|
216
|
+
|
|
217
|
+
// 匹配模式 (如果有)
|
|
218
|
+
if (rule.pattern) {
|
|
219
|
+
const targetStr = typeof input === 'string' ? input : JSON.stringify(input)
|
|
220
|
+
if (!rule.pattern.test(targetStr)) continue
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// 评估规则
|
|
224
|
+
if (rule.type === RuleType.DENY) {
|
|
225
|
+
this._ruleStats.denied++
|
|
226
|
+
return new RuleResult(false, rule)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (rule.type === RuleType.ALLOW) {
|
|
230
|
+
this._ruleStats.allowed++
|
|
231
|
+
return new RuleResult(true, rule)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (rule.type === RuleType.TRANSFORM) {
|
|
235
|
+
this._ruleStats.transformed++
|
|
236
|
+
return new RuleResult(true, rule, rule.action)
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// 没有匹配规则,默认允许
|
|
241
|
+
this._ruleStats.allowed++
|
|
242
|
+
return new RuleResult(true)
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* 检查工具调用是否允许
|
|
247
|
+
*/
|
|
248
|
+
checkToolCall(toolName, args) {
|
|
249
|
+
return this.evaluate(toolName, args)
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* 检查消息是否允许
|
|
254
|
+
*/
|
|
255
|
+
checkMessage(message, context) {
|
|
256
|
+
return this.evaluate('@message', { message, context })
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* 添加规则
|
|
261
|
+
*/
|
|
262
|
+
addRule(rule) {
|
|
263
|
+
this._rules.push(rule)
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* 移除规则
|
|
268
|
+
*/
|
|
269
|
+
removeRule(name) {
|
|
270
|
+
const index = this._rules.findIndex(r => r.name === name)
|
|
271
|
+
if (index !== -1) {
|
|
272
|
+
this._rules.splice(index, 1)
|
|
273
|
+
return true
|
|
274
|
+
}
|
|
275
|
+
return false
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
reload(framework) {
|
|
279
|
+
this._framework = framework
|
|
280
|
+
this._rules = []
|
|
281
|
+
this._ruleStats = { total: 0, allowed: 0, denied: 0, transformed: 0 }
|
|
282
|
+
|
|
283
|
+
if (this.config.autoLoad && this.config.rulesDir) {
|
|
284
|
+
this.loadFromDirectory(this.config.rulesDir)
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
uninstall(framework) {
|
|
289
|
+
this._rules = []
|
|
290
|
+
this._ruleStats = { total: 0, allowed: 0, denied: 0, transformed: 0 }
|
|
291
|
+
this._framework = null
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
295
|
module.exports = { RulesPlugin }
|