Merge branch 'afc' of https://github.com/MoFox-Studio/MoFox_Bot into afc
This commit is contained in:
@@ -194,8 +194,14 @@ class MessageManager:
|
|||||||
|
|
||||||
for message in unread_messages:
|
for message in unread_messages:
|
||||||
is_mentioned = message.is_mentioned or False
|
is_mentioned = message.is_mentioned or False
|
||||||
|
if not is_mentioned and not is_private:
|
||||||
|
bot_names = [global_config.bot.nickname] + global_config.bot.alias_names
|
||||||
|
if any(name in message.processed_plain_text for name in bot_names):
|
||||||
|
is_mentioned = True
|
||||||
|
logger.debug(f"通过关键词 '{next((name for name in bot_names if name in message.processed_plain_text), '')}' 匹配将消息标记为 'is_mentioned'")
|
||||||
|
|
||||||
if is_private or is_mentioned:
|
if is_private or is_mentioned:
|
||||||
if self.wakeup_manager.add_wakeup_value(is_private, is_mentioned):
|
if self.wakeup_manager.add_wakeup_value(is_private, is_mentioned, chat_id=stream_id):
|
||||||
was_woken_up = True
|
was_woken_up = True
|
||||||
break # 一旦被吵醒,就跳出循环并处理消息
|
break # 一旦被吵醒,就跳出循环并处理消息
|
||||||
|
|
||||||
@@ -204,6 +210,12 @@ class MessageManager:
|
|||||||
return # 退出,不处理消息
|
return # 退出,不处理消息
|
||||||
|
|
||||||
logger.info(f"Bot被聊天流 {stream_id} 中的消息吵醒,继续处理。")
|
logger.info(f"Bot被聊天流 {stream_id} 中的消息吵醒,继续处理。")
|
||||||
|
elif self.sleep_manager.is_woken_up():
|
||||||
|
angry_chat_id = self.wakeup_manager.angry_chat_id
|
||||||
|
if stream_id != angry_chat_id:
|
||||||
|
logger.debug(f"Bot处于WOKEN_UP状态,但当前流 {stream_id} 不是触发唤醒的流 {angry_chat_id},跳过处理。")
|
||||||
|
return # 退出,不处理此流的消息
|
||||||
|
logger.info(f"Bot处于WOKEN_UP状态,处理触发唤醒的流 {stream_id}。")
|
||||||
# --- 睡眠状态检查结束 ---
|
# --- 睡眠状态检查结束 ---
|
||||||
|
|
||||||
logger.debug(f"开始处理聊天流 {stream_id} 的 {len(unread_messages)} 条未读消息")
|
logger.debug(f"开始处理聊天流 {stream_id} 的 {len(unread_messages)} 条未读消息")
|
||||||
|
|||||||
@@ -39,6 +39,10 @@ class SleepManager:
|
|||||||
"""判断当前是否处于正在睡觉的状态。"""
|
"""判断当前是否处于正在睡觉的状态。"""
|
||||||
return self.context.current_state == SleepState.SLEEPING
|
return self.context.current_state == SleepState.SLEEPING
|
||||||
|
|
||||||
|
def is_woken_up(self) -> bool:
|
||||||
|
"""判断当前是否处于被吵醒的状态。"""
|
||||||
|
return self.context.current_state == SleepState.WOKEN_UP
|
||||||
|
|
||||||
async def update_sleep_state(self, wakeup_manager: Optional["WakeUpManager"] = None):
|
async def update_sleep_state(self, wakeup_manager: Optional["WakeUpManager"] = None):
|
||||||
"""
|
"""
|
||||||
更新睡眠状态的核心方法,实现状态机的主要逻辑。
|
更新睡眠状态的核心方法,实现状态机的主要逻辑。
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ class WakeUpManager:
|
|||||||
"""
|
"""
|
||||||
self.sleep_manager = sleep_manager
|
self.sleep_manager = sleep_manager
|
||||||
self.context = WakeUpContext() # 使用新的上下文管理器
|
self.context = WakeUpContext() # 使用新的上下文管理器
|
||||||
|
self.angry_chat_id: Optional[str] = None
|
||||||
self.last_decay_time = time.time()
|
self.last_decay_time = time.time()
|
||||||
self._decay_task: Optional[asyncio.Task] = None
|
self._decay_task: Optional[asyncio.Task] = None
|
||||||
self.is_running = False
|
self.is_running = False
|
||||||
@@ -87,7 +88,11 @@ class WakeUpManager:
|
|||||||
self.context.is_angry = False
|
self.context.is_angry = False
|
||||||
# 通知情绪管理系统清除愤怒状态
|
# 通知情绪管理系统清除愤怒状态
|
||||||
from src.mood.mood_manager import mood_manager
|
from src.mood.mood_manager import mood_manager
|
||||||
mood_manager.clear_angry_from_wakeup("global_mood")
|
if self.angry_chat_id:
|
||||||
|
mood_manager.clear_angry_from_wakeup(self.angry_chat_id)
|
||||||
|
self.angry_chat_id = None
|
||||||
|
else:
|
||||||
|
logger.warning("Angry state ended but no angry_chat_id was set.")
|
||||||
logger.info("愤怒状态结束,恢复正常")
|
logger.info("愤怒状态结束,恢复正常")
|
||||||
self.context.save()
|
self.context.save()
|
||||||
|
|
||||||
@@ -99,7 +104,7 @@ class WakeUpManager:
|
|||||||
logger.debug(f"唤醒度衰减: {old_value:.1f} -> {self.context.wakeup_value:.1f}")
|
logger.debug(f"唤醒度衰减: {old_value:.1f} -> {self.context.wakeup_value:.1f}")
|
||||||
self.context.save()
|
self.context.save()
|
||||||
|
|
||||||
def add_wakeup_value(self, is_private_chat: bool, is_mentioned: bool = False) -> bool:
|
def add_wakeup_value(self, is_private_chat: bool, is_mentioned: bool = False, chat_id: Optional[str] = None) -> bool:
|
||||||
"""
|
"""
|
||||||
增加唤醒度值
|
增加唤醒度值
|
||||||
|
|
||||||
@@ -148,23 +153,27 @@ class WakeUpManager:
|
|||||||
|
|
||||||
# 检查是否达到唤醒阈值
|
# 检查是否达到唤醒阈值
|
||||||
if self.context.wakeup_value >= self.wakeup_threshold:
|
if self.context.wakeup_value >= self.wakeup_threshold:
|
||||||
self._trigger_wakeup()
|
if not chat_id:
|
||||||
|
logger.error("Wakeup threshold reached, but no chat_id was provided. Cannot trigger wakeup.")
|
||||||
|
return False
|
||||||
|
self._trigger_wakeup(chat_id)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
self.context.save()
|
self.context.save()
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _trigger_wakeup(self):
|
def _trigger_wakeup(self, chat_id: str):
|
||||||
"""触发唤醒,进入愤怒状态"""
|
"""触发唤醒,进入愤怒状态"""
|
||||||
self.context.is_angry = True
|
self.context.is_angry = True
|
||||||
self.context.angry_start_time = time.time()
|
self.context.angry_start_time = time.time()
|
||||||
self.context.wakeup_value = 0.0 # 重置唤醒度
|
self.context.wakeup_value = 0.0 # 重置唤醒度
|
||||||
|
self.angry_chat_id = chat_id
|
||||||
|
|
||||||
self.context.save()
|
self.context.save()
|
||||||
|
|
||||||
# 通知情绪管理系统进入愤怒状态
|
# 通知情绪管理系统进入愤怒状态
|
||||||
from src.mood.mood_manager import mood_manager
|
from src.mood.mood_manager import mood_manager
|
||||||
mood_manager.set_angry_from_wakeup("global_mood")
|
mood_manager.set_angry_from_wakeup(chat_id)
|
||||||
|
|
||||||
# 通知SleepManager重置睡眠状态
|
# 通知SleepManager重置睡眠状态
|
||||||
self.sleep_manager.reset_sleep_state_after_wakeup()
|
self.sleep_manager.reset_sleep_state_after_wakeup()
|
||||||
@@ -185,7 +194,11 @@ class WakeUpManager:
|
|||||||
self.context.is_angry = False
|
self.context.is_angry = False
|
||||||
# 通知情绪管理系统清除愤怒状态
|
# 通知情绪管理系统清除愤怒状态
|
||||||
from src.mood.mood_manager import mood_manager
|
from src.mood.mood_manager import mood_manager
|
||||||
mood_manager.clear_angry_from_wakeup("global_mood")
|
if self.angry_chat_id:
|
||||||
|
mood_manager.clear_angry_from_wakeup(self.angry_chat_id)
|
||||||
|
self.angry_chat_id = None
|
||||||
|
else:
|
||||||
|
logger.warning("Angry state expired in check, but no angry_chat_id was set.")
|
||||||
logger.info("愤怒状态自动过期")
|
logger.info("愤怒状态自动过期")
|
||||||
return False
|
return False
|
||||||
return self.context.is_angry
|
return self.context.is_angry
|
||||||
|
|||||||
@@ -483,7 +483,8 @@ class Prompt:
|
|||||||
|
|
||||||
async def _build_expression_habits(self) -> Dict[str, Any]:
|
async def _build_expression_habits(self) -> Dict[str, Any]:
|
||||||
"""构建表达习惯"""
|
"""构建表达习惯"""
|
||||||
if not global_config.expression.enable_expression:
|
use_expression, _, _ = global_config.expression.get_expression_config_for_chat(self.parameters.chat_id)
|
||||||
|
if not use_expression:
|
||||||
return {"expression_habits_block": ""}
|
return {"expression_habits_block": ""}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -145,7 +145,7 @@ class LLMUsageRecorder:
|
|||||||
LLM使用情况记录器(SQLAlchemy版本)
|
LLM使用情况记录器(SQLAlchemy版本)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def record_usage_to_database(
|
async def record_usage_to_database(
|
||||||
self,
|
self,
|
||||||
model_info: ModelInfo,
|
model_info: ModelInfo,
|
||||||
model_usage: UsageRecord,
|
model_usage: UsageRecord,
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -151,12 +151,31 @@ class ChatterPlanFilter:
|
|||||||
identity_block = f"你的名字是{bot_name}{bot_nickname},你{bot_core_personality}:"
|
identity_block = f"你的名字是{bot_name}{bot_nickname},你{bot_core_personality}:"
|
||||||
|
|
||||||
schedule_block = ""
|
schedule_block = ""
|
||||||
if global_config.planning_system.schedule_enable:
|
# 优先检查是否被吵醒
|
||||||
|
from src.chat.message_manager.message_manager import message_manager
|
||||||
|
angry_prompt_addition = ""
|
||||||
|
wakeup_mgr = message_manager.wakeup_manager
|
||||||
|
|
||||||
|
# 双重检查确保愤怒状态不会丢失
|
||||||
|
# 检查1: 直接从 wakeup_manager 获取
|
||||||
|
if wakeup_mgr.is_in_angry_state():
|
||||||
|
angry_prompt_addition = wakeup_mgr.get_angry_prompt_addition()
|
||||||
|
|
||||||
|
# 检查2: 如果上面没获取到,再从 mood_manager 确认
|
||||||
|
if not angry_prompt_addition:
|
||||||
|
chat_mood_for_check = mood_manager.get_mood_by_chat_id(plan.chat_id)
|
||||||
|
if chat_mood_for_check.is_angry_from_wakeup:
|
||||||
|
angry_prompt_addition = global_config.sleep_system.angry_prompt
|
||||||
|
|
||||||
|
if angry_prompt_addition:
|
||||||
|
schedule_block = angry_prompt_addition
|
||||||
|
elif global_config.planning_system.schedule_enable:
|
||||||
if current_activity := schedule_manager.get_current_activity():
|
if current_activity := schedule_manager.get_current_activity():
|
||||||
schedule_block = f"你当前正在:{current_activity},但注意它与群聊的聊天无关。"
|
schedule_block = f"你当前正在:{current_activity},但注意它与群聊的聊天无关。"
|
||||||
|
|
||||||
mood_block = ""
|
mood_block = ""
|
||||||
if global_config.mood.enable_mood:
|
# 如果被吵醒,则心情也是愤怒的,不需要另外的情绪模块
|
||||||
|
if not angry_prompt_addition and global_config.mood.enable_mood:
|
||||||
chat_mood = mood_manager.get_mood_by_chat_id(plan.chat_id)
|
chat_mood = mood_manager.get_mood_by_chat_id(plan.chat_id)
|
||||||
mood_block = f"你现在的心情是:{chat_mood.mood_state}"
|
mood_block = f"你现在的心情是:{chat_mood.mood_state}"
|
||||||
|
|
||||||
|
|||||||
@@ -654,20 +654,30 @@ class QZoneService:
|
|||||||
end_idx = resp_text.rfind("}") + 1
|
end_idx = resp_text.rfind("}") + 1
|
||||||
if start_idx != -1 and end_idx != -1:
|
if start_idx != -1 and end_idx != -1:
|
||||||
json_str = resp_text[start_idx:end_idx]
|
json_str = resp_text[start_idx:end_idx]
|
||||||
upload_result = eval(json_str) # 与原版保持一致使用eval
|
try:
|
||||||
|
upload_result = orjson.loads(json_str)
|
||||||
|
except orjson.JSONDecodeError:
|
||||||
|
logger.error(f"图片上传响应JSON解析失败,原始响应: {resp_text}")
|
||||||
|
return None
|
||||||
|
|
||||||
logger.info(f"图片上传解析结果: {upload_result}")
|
logger.debug(f"图片上传解析结果: {upload_result}")
|
||||||
|
|
||||||
if upload_result.get("ret") == 0:
|
if upload_result.get("ret") == 0:
|
||||||
|
try:
|
||||||
# 使用原版的参数提取逻辑
|
# 使用原版的参数提取逻辑
|
||||||
picbo, richval = _get_picbo_and_richval(upload_result)
|
picbo, richval = _get_picbo_and_richval(upload_result)
|
||||||
logger.info(f"图片 {index + 1} 上传成功: picbo={picbo}")
|
logger.info(f"图片 {index + 1} 上传成功: picbo={picbo}")
|
||||||
return {"pic_bo": picbo, "richval": richval}
|
return {"pic_bo": picbo, "richval": richval}
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(
|
||||||
|
f"从上传结果中提取图片参数失败: {e}, 上传结果: {upload_result}", exc_info=True
|
||||||
|
)
|
||||||
|
return None
|
||||||
else:
|
else:
|
||||||
logger.error(f"图片 {index + 1} 上传失败: {upload_result}")
|
logger.error(f"图片 {index + 1} 上传失败: {upload_result}")
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
logger.error("无法解析上传响应")
|
logger.error(f"无法从响应中提取JSON内容: {resp_text}")
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
error_text = await response.text()
|
error_text = await response.text()
|
||||||
|
|||||||
Reference in New Issue
Block a user