56 lines
1.5 KiB
Python
56 lines
1.5 KiB
Python
lines = []
|
||
with open('src/components/SandboxSimulation.tsx', 'r') as f:
|
||
lines = f.readlines()
|
||
|
||
for i, line in enumerate(lines):
|
||
if "// Perform rule matching" in line:
|
||
start_idx = i
|
||
break
|
||
|
||
for i in range(start_idx, len(lines)):
|
||
if " // Send single manual input" in lines[i]:
|
||
end_idx = i
|
||
break
|
||
|
||
replacement = """ // 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
|
||
};
|
||
});
|
||
};
|
||
|
||
"""
|
||
|
||
out = "".join(lines[:start_idx]) + replacement + "".join(lines[end_idx:])
|
||
|
||
with open('src/components/SandboxSimulation.tsx', 'w') as f:
|
||
f.write(out)
|