34 lines
930 B
Python
34 lines
930 B
Python
|
|
from __future__ import annotations
|
|||
|
|
|
|||
|
|
import re
|
|||
|
|
|
|||
|
|
_TOOL_CALL_RE = re.compile(r"\[TOOL_CALL:", re.IGNORECASE)
|
|||
|
|
_ASSISTANT_MARKER_RE = re.compile(r"\[ASSISTANT_SYSTEM\]", re.IGNORECASE)
|
|||
|
|
|
|||
|
|
|
|||
|
|
class ReflectionGuard:
|
|||
|
|
"""反射防护:检测入站消息中是否包含 Assistant 内部标记。
|
|||
|
|
|
|||
|
|
当出站消息包含 [TOOL_CALL:...] 等内部标记,且被 Bridge 反射回
|
|||
|
|
入站事件时,应丢弃此类消息,避免 Assistant 将其解析为指令。
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
def __init__(self):
|
|||
|
|
self._blocked_count = 0
|
|||
|
|
|
|||
|
|
def is_reflection(self, text: str) -> bool:
|
|||
|
|
if not text:
|
|||
|
|
return False
|
|||
|
|
if _TOOL_CALL_RE.search(text):
|
|||
|
|
return True
|
|||
|
|
if _ASSISTANT_MARKER_RE.search(text):
|
|||
|
|
return True
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
def mark_blocked(self) -> None:
|
|||
|
|
self._blocked_count += 1
|
|||
|
|
|
|||
|
|
@property
|
|||
|
|
def blocked_count(self) -> int:
|
|||
|
|
return self._blocked_count
|