45 lines
2.9 KiB
Python
45 lines
2.9 KiB
Python
import re
|
||
|
||
with open('src/components/SandboxSimulation.tsx', 'r') as f:
|
||
content = f.read()
|
||
|
||
pattern = r" // Perform rule matching\n let currentAlerts = \[\.\.\.prev\.alerts\];\n let silentCount = prev\.interceptCount;\n const activeRules = rules\.filter\(r => r\.enabled\);\n\n activeRules\.forEach\(rule => \{\n // Check if any keyword matches\n const matchedKeyword = rule\.keywords\.find\(kw => \n text\.toLowerCase\(\)\.includes\(kw\.toLowerCase\(\)\)\n \);\n\n if \(matchedKeyword\) \{\n // Check if this rule was already triggered in the current session\n const alreadyTriggered = currentAlerts\.some\(a => a\.ruleId === rule\.id\);\n\n if \(alreadyTriggered\) \{\n // Duplicate hit: silent interception\n silentCount \+= 1;\n // Silent notify\n addToast\(`规则「\$\{rule\.name\}」在 seq: \$\{nextSeq\} 中再次触发,已被系统去重静默拦截!`, 'info'\);\n \} else \{\n // Fresh alert!\n const alertId = `alert-\$\{rule\.id\}-\$\{Date\.now\(\)\}`;\n const newAlert: Alert = \{\n id: alertId,\n ruleId: rule\.id,\n ruleName: rule\.name,\n keyword: matchedKeyword,\n text: text\.trim\(\),\n seq: nextSeq,\n timestamp,\n \};\n currentAlerts = \[\.\.\.currentAlerts, newAlert\];\n addToast\(`⚠️ 触发预警: 「\$\{rule\.name\}」(命中词: \$\{matchedKeyword\})`, 'warning'\);\n \}\n \}\n \}\);\n\n return \{\n \.\.\.prev,\n lines: updatedLines,\n alerts: currentAlerts,\n interceptCount: silentCount\n \};"
|
||
|
||
new_handle = """ // Perform rule matching
|
||
let currentAlerts = [...prev.alerts];
|
||
const activeRules = rules.filter(r => r.enabled);
|
||
|
||
activeRules.forEach(rule => {
|
||
// Check if any keyword matches
|
||
const matchedKeyword = rule.keywords.find(kw =>
|
||
text.toLowerCase().includes(kw.toLowerCase())
|
||
);
|
||
|
||
if (matchedKeyword) {
|
||
// Fresh alert!
|
||
const alertId = `alert-${rule.id}-${Date.now()}`;
|
||
const newAlert: Alert = {
|
||
id: alertId,
|
||
ruleId: rule.id,
|
||
ruleName: rule.name,
|
||
keyword: matchedKeyword,
|
||
text: text.trim(),
|
||
seq: nextSeq,
|
||
timestamp,
|
||
};
|
||
currentAlerts = [...currentAlerts, newAlert];
|
||
addToast(`⚠️ 触发预警: 「${rule.name}」(命中词: ${matchedKeyword})`, 'warning');
|
||
}
|
||
});
|
||
|
||
return {
|
||
...prev,
|
||
lines: updatedLines,
|
||
alerts: currentAlerts
|
||
};"""
|
||
|
||
content = re.sub(pattern, new_handle, content, flags=re.DOTALL)
|
||
|
||
with open('src/components/SandboxSimulation.tsx', 'w') as f:
|
||
f.write(content)
|