diff --git a/src/plugins/willing/mode_classical.py b/src/plugins/willing/mode_classical.py index d9450f028..05a6bc7fd 100644 --- a/src/plugins/willing/mode_classical.py +++ b/src/plugins/willing/mode_classical.py @@ -1,14 +1,10 @@ import asyncio -from typing import Dict -from ..chat.chat_stream import ChatStream -from ..config.config import global_config +from .willing_manager import BaseWillingManager -class WillingManager: +class ClassicalWillingManager(BaseWillingManager): def __init__(self): - self.chat_reply_willing: Dict[str, float] = {} # 存储每个聊天流的回复意愿 - self._decay_task = None - self._started = False + super().__init__() async def _decay_reply_willing(self): """定期衰减回复意愿""" @@ -16,87 +12,51 @@ class WillingManager: await asyncio.sleep(1) for chat_id in self.chat_reply_willing: self.chat_reply_willing[chat_id] = max(0, self.chat_reply_willing[chat_id] * 0.9) + self.logger.info(self.chat_reply_willing[chat_id]) - def get_willing(self, chat_stream: ChatStream) -> float: - """获取指定聊天流的回复意愿""" - if chat_stream: - return self.chat_reply_willing.get(chat_stream.stream_id, 0) - return 0 + async def async_task_starter(self): + asyncio.create_task(self._decay_reply_willing) - def set_willing(self, chat_id: str, willing: float): - """设置指定聊天流的回复意愿""" - self.chat_reply_willing[chat_id] = willing - - async def change_reply_willing_received( - self, - chat_stream: ChatStream, - is_mentioned_bot: bool = False, - config=None, - is_emoji: bool = False, - interested_rate: float = 0, - sender_id: str = None, - ) -> float: - """改变指定聊天流的回复意愿并返回回复概率""" - chat_id = chat_stream.stream_id + async def get_reply_probability(self, message_id): + willing_info = self.ongoing_messages[message_id] + chat_id = willing_info.chat_id current_willing = self.chat_reply_willing.get(chat_id, 0) - interested_rate = interested_rate * config.response_interested_rate_amplifier + interested_rate = willing_info.interested_rate * self.global_config.response_interested_rate_amplifier if interested_rate > 0.4: current_willing += interested_rate - 0.3 - if is_mentioned_bot and current_willing < 1.0: + if willing_info.is_mentioned_bot and current_willing < 1.0: current_willing += 1 - elif is_mentioned_bot: + elif willing_info.is_mentioned_bot: current_willing += 0.05 - if is_emoji: - current_willing *= global_config.emoji_response_penalty + if willing_info.is_emoji: + if self.global_config.emoji_response_penalty != 0: + current_willing *= self.global_config.emoji_response_penalty + else: + return 0 self.chat_reply_willing[chat_id] = min(current_willing, 3.0) - reply_probability = min(max((current_willing - 0.5), 0.01) * config.response_willing_amplifier * 2, 1) + reply_probability = min(max((current_willing - 0.5), 0.01) * self.global_config.response_willing_amplifier * 2, 1) # 检查群组权限(如果是群聊) - if chat_stream.group_info and config: - if chat_stream.group_info.group_id not in config.talk_allowed_groups: - current_willing = 0 - reply_probability = 0 - - if chat_stream.group_info.group_id in config.talk_frequency_down_groups: - reply_probability = reply_probability / config.down_frequency_rate + if willing_info.group_info and willing_info.group_info.group_id in self.global_config.talk_frequency_down_groups: + reply_probability = reply_probability / self.global_config.down_frequency_rate return reply_probability + + async def before_generate_reply_handle(self, message_id): + chat_id = self.ongoing_messages[message_id].chat_id + current_willing = self.chat_reply_willing.get(chat_id, 0) + self.chat_reply_willing[chat_id] = max(0, current_willing - 1.8) - def change_reply_willing_sent(self, chat_stream: ChatStream): - """发送消息后降低聊天流的回复意愿""" - if chat_stream: - chat_id = chat_stream.stream_id - current_willing = self.chat_reply_willing.get(chat_id, 0) - self.chat_reply_willing[chat_id] = max(0, current_willing - 1.8) - - def change_reply_willing_not_sent(self, chat_stream: ChatStream): - """未发送消息后降低聊天流的回复意愿""" - if chat_stream: - chat_id = chat_stream.stream_id - current_willing = self.chat_reply_willing.get(chat_id, 0) - self.chat_reply_willing[chat_id] = max(0, current_willing - 0) - - def change_reply_willing_after_sent(self, chat_stream: ChatStream): - """发送消息后提高聊天流的回复意愿""" - if chat_stream: - chat_id = chat_stream.stream_id - current_willing = self.chat_reply_willing.get(chat_id, 0) - if current_willing < 1: - self.chat_reply_willing[chat_id] = min(1, current_willing + 0.4) - - async def ensure_started(self): - """确保衰减任务已启动""" - if not self._started: - if self._decay_task is None: - self._decay_task = asyncio.create_task(self._decay_reply_willing()) - self._started = True - - -# 创建全局实例 -willing_manager = WillingManager() + async def after_generate_reply_handle(self, message_id): + chat_id = self.ongoing_messages[message_id].chat_id + current_willing = self.chat_reply_willing.get(chat_id, 0) + if current_willing < 1: + self.chat_reply_willing[chat_id] = min(1, current_willing + 0.4) + + \ No newline at end of file diff --git a/src/plugins/willing/mode_custom.py b/src/plugins/willing/mode_custom.py index 0f32c0c75..786c779b4 100644 --- a/src/plugins/willing/mode_custom.py +++ b/src/plugins/willing/mode_custom.py @@ -1,101 +1,7 @@ -import asyncio -from typing import Dict -from ..chat.chat_stream import ChatStream +from .willing_manager import BaseWillingManager -class WillingManager: +class CustomWillingManager(BaseWillingManager): def __init__(self): - self.chat_reply_willing: Dict[str, float] = {} # 存储每个聊天流的回复意愿 - self._decay_task = None - self._started = False + super().__init__() - async def _decay_reply_willing(self): - """定期衰减回复意愿""" - while True: - await asyncio.sleep(1) - for chat_id in self.chat_reply_willing: - self.chat_reply_willing[chat_id] = max(0, self.chat_reply_willing[chat_id] * 0.9) - - def get_willing(self, chat_stream: ChatStream) -> float: - """获取指定聊天流的回复意愿""" - if chat_stream: - return self.chat_reply_willing.get(chat_stream.stream_id, 0) - return 0 - - def set_willing(self, chat_id: str, willing: float): - """设置指定聊天流的回复意愿""" - self.chat_reply_willing[chat_id] = willing - - async def change_reply_willing_received( - self, - chat_stream: ChatStream, - is_mentioned_bot: bool = False, - config=None, - is_emoji: bool = False, - interested_rate: float = 0, - sender_id: str = None, - ) -> float: - """改变指定聊天流的回复意愿并返回回复概率""" - chat_id = chat_stream.stream_id - current_willing = self.chat_reply_willing.get(chat_id, 0) - - interested_rate = interested_rate * config.response_interested_rate_amplifier - - if interested_rate > 0.4: - current_willing += interested_rate - 0.3 - - if is_mentioned_bot and current_willing < 1.0: - current_willing += 1 - elif is_mentioned_bot: - current_willing += 0.05 - - if is_emoji: - current_willing *= 0.2 - - self.chat_reply_willing[chat_id] = min(current_willing, 3.0) - - reply_probability = min(max((current_willing - 0.5), 0.01) * config.response_willing_amplifier * 2, 1) - - # 检查群组权限(如果是群聊) - if chat_stream.group_info and config: - if chat_stream.group_info.group_id not in config.talk_allowed_groups: - current_willing = 0 - reply_probability = 0 - - if chat_stream.group_info.group_id in config.talk_frequency_down_groups: - reply_probability = reply_probability / config.down_frequency_rate - - return reply_probability - - def change_reply_willing_sent(self, chat_stream: ChatStream): - """发送消息后降低聊天流的回复意愿""" - if chat_stream: - chat_id = chat_stream.stream_id - current_willing = self.chat_reply_willing.get(chat_id, 0) - self.chat_reply_willing[chat_id] = max(0, current_willing - 1.8) - - def change_reply_willing_not_sent(self, chat_stream: ChatStream): - """未发送消息后降低聊天流的回复意愿""" - if chat_stream: - chat_id = chat_stream.stream_id - current_willing = self.chat_reply_willing.get(chat_id, 0) - self.chat_reply_willing[chat_id] = max(0, current_willing - 0) - - def change_reply_willing_after_sent(self, chat_stream: ChatStream): - """发送消息后提高聊天流的回复意愿""" - if chat_stream: - chat_id = chat_stream.stream_id - current_willing = self.chat_reply_willing.get(chat_id, 0) - if current_willing < 1: - self.chat_reply_willing[chat_id] = min(1, current_willing + 0.4) - - async def ensure_started(self): - """确保衰减任务已启动""" - if not self._started: - if self._decay_task is None: - self._decay_task = asyncio.create_task(self._decay_reply_willing()) - self._started = True - - -# 创建全局实例 -willing_manager = WillingManager()