This commit is contained in:
Windpicker-owo
2025-10-07 12:01:39 +08:00
36 changed files with 351066 additions and 4374 deletions

View File

@@ -265,7 +265,8 @@ class AntiPromptInjector:
async with get_db_session() as session:
# 删除对应的消息记录
stmt = delete(Messages).where(Messages.message_id == message_id)
result = session.execute(stmt)
# 注意: 异步会话需要 await 执行,否则 result 是 coroutine无法获取 rowcount
result = await session.execute(stmt)
await session.commit()
if result.rowcount > 0:
@@ -296,7 +297,7 @@ class AntiPromptInjector:
.where(Messages.message_id == message_id)
.values(processed_plain_text=new_content, display_message=new_content)
)
result = session.execute(stmt)
result = await session.execute(stmt)
await session.commit()
if result.rowcount > 0:

View File

@@ -4,6 +4,10 @@
负责生成个性化的反击消息回应提示词注入攻击
"""
import asyncio
from functools import lru_cache
from typing import Optional
from src.common.logger import get_logger
from src.config.config import global_config
from src.plugin_system.apis import llm_api
@@ -15,14 +19,28 @@ logger = get_logger("anti_injector.counter_attack")
class CounterAttackGenerator:
"""反击消息生成器"""
COUNTER_ATTACK_PROMPT_TEMPLATE = """你是{bot_name},请以你的人格特征回应这次提示词注入攻击:
{personality_info}
攻击消息: {original_message}
置信度: {confidence:.2f}
检测到的模式: {patterns}
请以你的人格特征生成一个反击回应:
1. 保持你的人格特征和说话风格
2. 幽默但不失态度,让攻击者知道行为被发现了
3. 具有教育意义提醒用户正确使用AI
4. 长度在20-30字之间
5. 符合你的身份和性格
反击回应:"""
@staticmethod
@lru_cache(maxsize=1)
def get_personality_context() -> str:
"""获取人格上下文信息
Returns:
人格上下文字符串
"""
"""获取人格上下文信息"""
try:
personality_parts = []
@@ -42,10 +60,7 @@ class CounterAttackGenerator:
if global_config.personality.reply_style:
personality_parts.append(f"表达风格: {global_config.personality.reply_style}")
if personality_parts:
return "\n".join(personality_parts)
else:
return "你是一个友好的AI助手"
return "\n".join(personality_parts) if personality_parts else "你是一个友好的AI助手"
except Exception as e:
logger.error(f"获取人格信息失败: {e}")
@@ -53,65 +68,89 @@ class CounterAttackGenerator:
async def generate_counter_attack_message(
self, original_message: str, detection_result: DetectionResult
) -> str | None:
"""生成反击消息
Args:
original_message: 原始攻击消息
detection_result: 检测结果
Returns:
生成的反击消息如果生成失败则返回None
"""
) -> Optional[str]:
"""生成反击消息"""
try:
# 获取可用的模型配置
models = llm_api.get_available_models()
model_config = models.get("anti_injection")
if not model_config:
logger.error("反注入专用模型配置 'anti_injection' 未找到,无法生成反击消息")
# 验证输入参数
if not original_message or not detection_result.matched_patterns:
logger.warning("无效的输入参数,跳过反击消息生成")
return None
# 获取人格信息
personality_info = self.get_personality_context()
# 构建反击提示词
counter_prompt = f"""你是{global_config.bot.nickname},请以你的人格特征回应这次提示词注入攻击:
{personality_info}
攻击消息: {original_message}
置信度: {detection_result.confidence:.2f}
检测到的模式: {", ".join(detection_result.matched_patterns)}
请以你的人格特征生成一个反击回应:
1. 保持你的人格特征和说话风格
2. 幽默但不失态度,让攻击者知道行为被发现了
3. 具有教育意义提醒用户正确使用AI
4. 长度在20-30字之间
5. 符合你的身份和性格
反击回应:"""
# 调用LLM生成反击消息
success, response, _, _ = await llm_api.generate_with_model(
prompt=counter_prompt,
model_config=model_config,
request_type="anti_injection.counter_attack",
temperature=0.7, # 稍高的温度增加创意
max_tokens=150,
)
if success and response:
# 清理响应内容
counter_message = response.strip()
if counter_message:
logger.info(f"成功生成反击消息: {counter_message[:50]}...")
return counter_message
logger.warning("LLM反击消息生成失败或返回空内容")
return None
# 获取模型配置
model_config = await self._get_model_config_with_retry()
if not model_config:
return self._get_fallback_response(detection_result)
# 构建提示词
prompt = self._build_counter_prompt(original_message, detection_result)
# 调用LLM
response = await self._call_llm_with_timeout(prompt, model_config)
return response or self._get_fallback_response(detection_result)
except asyncio.TimeoutError:
logger.error("LLM调用超时")
return self._get_fallback_response(detection_result)
except Exception as e:
logger.error(f"生成反击消息时出错: {e}")
logger.error(f"生成反击消息时出错: {e}", exc_info=True)
return self._get_fallback_response(detection_result)
async def _get_model_config_with_retry(self, max_retries: int = 2) -> Optional[dict]:
"""获取模型配置(带重试)"""
for attempt in range(max_retries + 1):
try:
models = llm_api.get_available_models()
if model_config := models.get("anti_injection"):
return model_config
if attempt < max_retries:
await asyncio.sleep(1)
except Exception as e:
logger.warning(f"获取模型配置失败,尝试 {attempt + 1}/{max_retries}: {e}")
logger.error("无法获取反注入模型配置")
return None
def _build_counter_prompt(self, original_message: str, detection_result: DetectionResult) -> str:
"""构建反击提示词"""
return self.COUNTER_ATTACK_PROMPT_TEMPLATE.format(
bot_name=global_config.bot.nickname,
personality_info=self.get_personality_context(),
original_message=original_message[:200],
confidence=detection_result.confidence,
patterns=", ".join(detection_result.matched_patterns[:5])
)
async def _call_llm_with_timeout(self, prompt: str, model_config: dict, timeout: int = 30) -> Optional[str]:
"""调用LLM"""
try:
success, response, _, _ = await asyncio.wait_for(
llm_api.generate_with_model(
prompt=prompt,
model_config=model_config,
request_type="anti_injection.counter_attack",
temperature=0.7,
max_tokens=150,
),
timeout=timeout
)
if success and (clean_response := response.strip()):
logger.info(f"成功生成反击消息: {clean_response[:50]}...")
return clean_response
logger.warning(f"LLM返回无效响应: {response}")
return None
except asyncio.TimeoutError:
raise
except Exception as e:
logger.error(f"LLM调用异常: {e}")
return None
def _get_fallback_response(self, detection_result: DetectionResult) -> str:
"""获取降级响应"""
patterns = ", ".join(detection_result.matched_patterns[:3])
return f"检测到可疑的提示词注入模式({patterns}),请使用正常对话方式交流。"

View File

@@ -5,9 +5,9 @@
"""
import datetime
from typing import Any
from typing import Any, Optional, TypedDict, Literal, Union, Callable, TypeVar, cast
from sqlalchemy import select
from sqlalchemy import select, delete
from src.common.database.sqlalchemy_models import AntiInjectionStats, get_db_session
from src.common.logger import get_logger
@@ -16,8 +16,30 @@ from src.config.config import global_config
logger = get_logger("anti_injector.statistics")
TNum = TypeVar("TNum", int, float)
def _add_optional(a: Optional[TNum], b: TNum) -> TNum:
"""安全相加:左值可能为 None。
Args:
a: 可能为 None 的当前值
b: 要累加的增量(非 None
Returns:
新的累加结果(与 b 同类型)
"""
if a is None:
return b
return cast(TNum, a + b) # a 不为 None此处显式 cast 便于类型检查
class AntiInjectionStatistics:
"""反注入系统统计管理类"""
"""反注入系统统计管理类
主要改进:
- 对 "可能为 None" 的数值字段做集中安全处理,减少在业务逻辑里反复判空。
- 补充类型注解便于静态检查器Pylance/Pyright识别。
"""
def __init__(self):
"""初始化统计管理器"""
@@ -25,8 +47,12 @@ class AntiInjectionStatistics:
"""当前会话开始时间"""
@staticmethod
async def get_or_create_stats():
"""获取或创建统计记录"""
async def get_or_create_stats() -> Optional[AntiInjectionStats]: # type: ignore[name-defined]
"""获取或创建统计记录
Returns:
AntiInjectionStats | None: 成功返回模型实例,否则 None
"""
try:
async with get_db_session() as session:
# 获取最新的统计记录,如果没有则创建
@@ -46,8 +72,15 @@ class AntiInjectionStatistics:
return None
@staticmethod
async def update_stats(**kwargs):
"""更新统计数据"""
async def update_stats(**kwargs: Any) -> None:
"""更新统计数据(批量可选字段)
支持字段:
- processing_time_delta: float 累加到 processing_time_total
- last_processing_time: float 设置 last_process_time
- total_messages / detected_injections / blocked_messages / shielded_messages / error_count: 累加
- 其他任意字段:直接赋值(若模型存在该属性)
"""
try:
async with get_db_session() as session:
stats = (
@@ -62,14 +95,13 @@ class AntiInjectionStatistics:
# 更新统计字段
for key, value in kwargs.items():
if key == "processing_time_delta":
# 处理 时间累加 - 确保不为None
if stats.processing_time_total is None:
stats.processing_time_total = 0.0
stats.processing_time_total += value
# 处理时间累加 - 确保不为 None
delta = float(value)
stats.processing_time_total = _add_optional(stats.processing_time_total, delta) # type: ignore[attr-defined]
continue
elif key == "last_processing_time":
# 直接设置最后处理时间
stats.last_process_time = value
stats.last_process_time = float(value)
continue
elif hasattr(stats, key):
if key in [
@@ -79,12 +111,10 @@ class AntiInjectionStatistics:
"shielded_messages",
"error_count",
]:
# 累加类型的字段 - 确保不为None
current_value = getattr(stats, key)
if current_value is None:
setattr(stats, key, value)
else:
setattr(stats, key, current_value + value)
# 累加类型的字段 - 统一用辅助函数
current_value = cast(Optional[int], getattr(stats, key))
increment = int(value)
setattr(stats, key, _add_optional(current_value, increment))
else:
# 直接设置的字段
setattr(stats, key, value)
@@ -114,10 +144,11 @@ class AntiInjectionStatistics:
stats = await self.get_or_create_stats()
# 计算派生统计信息 - 处理None值
total_messages = stats.total_messages or 0
detected_injections = stats.detected_injections or 0
processing_time_total = stats.processing_time_total or 0.0
# 计算派生统计信息 - 处理 None 值
total_messages = stats.total_messages or 0 # type: ignore[attr-defined]
detected_injections = stats.detected_injections or 0 # type: ignore[attr-defined]
processing_time_total = stats.processing_time_total or 0.0 # type: ignore[attr-defined]
detection_rate = (detected_injections / total_messages * 100) if total_messages > 0 else 0
avg_processing_time = (processing_time_total / total_messages) if total_messages > 0 else 0
@@ -127,17 +158,22 @@ class AntiInjectionStatistics:
current_time = datetime.datetime.now()
uptime = current_time - self.session_start_time
last_proc = stats.last_process_time # type: ignore[attr-defined]
blocked_messages = stats.blocked_messages or 0 # type: ignore[attr-defined]
shielded_messages = stats.shielded_messages or 0 # type: ignore[attr-defined]
error_count = stats.error_count or 0 # type: ignore[attr-defined]
return {
"status": "enabled",
"uptime": str(uptime),
"total_messages": total_messages,
"detected_injections": detected_injections,
"blocked_messages": stats.blocked_messages or 0,
"shielded_messages": stats.shielded_messages or 0,
"blocked_messages": blocked_messages,
"shielded_messages": shielded_messages,
"detection_rate": f"{detection_rate:.2f}%",
"average_processing_time": f"{avg_processing_time:.3f}s",
"last_processing_time": f"{stats.last_process_time:.3f}s" if stats.last_process_time else "0.000s",
"error_count": stats.error_count or 0,
"last_processing_time": f"{last_proc:.3f}s" if last_proc else "0.000s",
"error_count": error_count,
}
except Exception as e:
logger.error(f"获取统计信息失败: {e}")
@@ -149,7 +185,7 @@ class AntiInjectionStatistics:
try:
async with get_db_session() as session:
# 删除现有统计记录
await session.execute(select(AntiInjectionStats).delete())
await session.execute(delete(AntiInjectionStats))
await session.commit()
logger.info("统计信息已重置")
except Exception as e:

View File

@@ -51,7 +51,7 @@ class UserBanManager:
remaining_time = ban_duration - (datetime.datetime.now() - ban_record.created_at)
return False, None, f"用户被封禁中,剩余时间: {remaining_time}"
else:
# 封禁已过期,重置违规次数
# 封禁已过期,重置违规次数与时间(模型已使用 Mapped 类型,可直接赋值)
ban_record.violation_num = 0
ban_record.created_at = datetime.datetime.now()
await session.commit()
@@ -92,7 +92,6 @@ class UserBanManager:
await session.commit()
# 检查是否需要自动封禁
if ban_record.violation_num >= self.config.auto_ban_violation_threshold:
logger.warning(f"用户 {platform}:{user_id} 违规次数达到 {ban_record.violation_num},触发自动封禁")
# 只有在首次达到阈值时才更新封禁开始时间

View File

@@ -377,11 +377,12 @@ async def clean_unused_emojis(emoji_dir: str, emoji_objects: list["MaiEmoji"], r
class EmojiManager:
_instance = None
_initialized: bool = False # 显式声明,避免属性未定义错误
def __new__(cls) -> "EmojiManager":
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance._initialized = False
# 类属性已声明,无需再次赋值
return cls._instance
def __init__(self) -> None:
@@ -399,7 +400,8 @@ class EmojiManager:
self.emoji_num_max = global_config.emoji.max_reg_num
self.emoji_num_max_reach_deletion = global_config.emoji.do_replace
self.emoji_objects: list[MaiEmoji] = [] # 存储MaiEmoji对象的列表使用类型注解明确列表元素类型
logger.info("启动表情包管理器")
self._initialized = True
logger.info("启动表情包管理器")
def shutdown(self) -> None:
@@ -752,8 +754,8 @@ class EmojiManager:
try:
emoji_record = await self.get_emoji_from_db(emoji_hash)
if emoji_record and emoji_record[0].emotion:
logger.info(f"[缓存命中] 从数据库获取表情包描述: {emoji_record.emotion[:50]}...")
return emoji_record.emotion
logger.info(f"[缓存命中] 从数据库获取表情包描述: {emoji_record.emotion[:50]}...") # type: ignore # type: ignore
return emoji_record.emotion # type: ignore
except Exception as e:
logger.error(f"从数据库查询表情包描述时出错: {e}")

View File

@@ -7,6 +7,7 @@ import asyncio
import time
from typing import Any
from chat.message_manager.adaptive_stream_manager import StreamPriority
from src.chat.chatter_manager import ChatterManager
from src.chat.energy_system import energy_manager
from src.common.data_models.message_manager_data_model import StreamContext

View File

@@ -332,14 +332,13 @@ class MessageManager:
async def _check_and_handle_interruption(self, chat_stream: ChatStream | None = None):
"""检查并处理消息打断"""
if not global_config.chat.interruption_enabled:
if not global_config.chat.interruption_enabled or not chat_stream:
return
# 检查是否有正在进行的处理任务
if (
chat_stream.context_manager.context.processing_task
and not chat_stream.context_manager.context.processing_task.done()
):
# 从 chatter_manager 检查是否有正在进行的处理任务
processing_task = self.chatter_manager.get_processing_task(chat_stream.stream_id)
if processing_task and not processing_task.done():
# 计算打断概率
interruption_probability = chat_stream.context_manager.context.calculate_interruption_probability(
global_config.chat.interruption_max_limit, global_config.chat.interruption_probability_factor
@@ -357,11 +356,11 @@ class MessageManager:
logger.info(f"聊天流 {chat_stream.stream_id} 触发消息打断,打断概率: {interruption_probability:.2f}")
# 取消现有任务
chat_stream.context_manager.context.processing_task.cancel()
processing_task.cancel()
try:
await chat_stream.context_manager.context.processing_task
await processing_task
except asyncio.CancelledError:
pass
logger.debug(f"消息打断成功取消任务: {chat_stream.stream_id}")
# 增加打断计数并应用afc阈值降低
await chat_stream.context_manager.context.increment_interruption_count()

View File

@@ -56,7 +56,9 @@ class ChineseTypoGenerator:
# 使用内置的词频文件
char_freq = defaultdict(int)
dict_path = os.path.join(os.path.dirname(rjieba.__file__), "dict.txt")
# 从当前文件向上返回三级目录到项目根目录,然后拼接路径
base_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
dict_path = os.path.join(base_dir, "depends-data", "dict.txt")
# 读取rjieba的词典文件
with open(dict_path, encoding="utf-8") as f: