107 lines
3.6 KiB
Python
107 lines
3.6 KiB
Python
import re
|
||
|
||
with open('src/components/SandboxSimulation.tsx', 'r') as f:
|
||
content = f.read()
|
||
|
||
# 1. remove interceptCount from initial states
|
||
content = re.sub(r" alerts: \[\],\n interceptCount: 0\n \}\);", " alerts: []\n });", content)
|
||
content = re.sub(r" alerts: \[\],\n interceptCount: 0\n \}\);", " alerts: []\n });", content)
|
||
|
||
# 2. handleSendLine
|
||
old_handle = """ // Perform rule matching
|
||
let currentAlerts = [...prev.alerts];
|
||
let silentCount = prev.interceptCount;
|
||
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) {
|
||
// Check if this rule was already triggered in the current session
|
||
const alreadyTriggered = currentAlerts.some(a => a.ruleId === rule.id);
|
||
|
||
if (alreadyTriggered) {
|
||
// Duplicate hit: silent interception
|
||
silentCount += 1;
|
||
// Silent notify
|
||
addToast(`规则「${rule.name}」在 seq: ${nextSeq} 中再次触发,已被系统去重静默拦截!`, 'info');
|
||
} else {
|
||
// 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,
|
||
interceptCount: silentCount
|
||
};"""
|
||
|
||
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 = content.replace(old_handle, new_handle)
|
||
|
||
# 3. UI
|
||
ui_to_remove = """ <div className="flex items-center space-x-1 font-mono text-[10px] font-bold">
|
||
<span className="text-slate-400">去重静默拦截:</span>
|
||
<span className={`px-1.5 py-0.2 rounded ${
|
||
session.interceptCount > 0
|
||
? 'bg-amber-100 text-amber-800'
|
||
: 'bg-slate-200 text-slate-600'
|
||
}`}>
|
||
{session.interceptCount} 次
|
||
</span>
|
||
</div>"""
|
||
|
||
content = content.replace(ui_to_remove, "")
|
||
|
||
with open('src/components/SandboxSimulation.tsx', 'w') as f:
|
||
f.write(content)
|