This commit is contained in:
SengokuCola
2025-04-23 15:32:24 +08:00
parent b4054544c4
commit d241e1922a
3 changed files with 50 additions and 61 deletions

View File

@@ -275,7 +275,6 @@ class SubHeartflow:
self.gpt_instance = ResponseGenerator() # 响应生成器 self.gpt_instance = ResponseGenerator() # 响应生成器
self.tool_user_instance = ToolUser() # 工具使用模块 self.tool_user_instance = ToolUser() # 工具使用模块
self.log_prefix = chat_manager.get_stream_name(self.subheartflow_id) or self.subheartflow_id self.log_prefix = chat_manager.get_stream_name(self.subheartflow_id) or self.subheartflow_id
async def set_chat_state(self, new_state: "ChatState", current_states_num: tuple = ()): async def set_chat_state(self, new_state: "ChatState", current_states_num: tuple = ()):
@@ -308,8 +307,7 @@ class SubHeartflow:
chat_stream = chat_manager.get_stream(self.chat_id) chat_stream = chat_manager.get_stream(self.chat_id)
self.normal_chat_instance = NormalChat( self.normal_chat_instance = NormalChat(
chat_stream=chat_stream, chat_stream=chat_stream, interest_dict=self.get_interest_dict()
interest_dict=self.get_interest_dict()
) )
await self.normal_chat_instance.start_monitoring_interest() await self.normal_chat_instance.start_monitoring_interest()
# NormalChat 启动/停止逻辑将在下面处理 # NormalChat 启动/停止逻辑将在下面处理
@@ -336,7 +334,6 @@ class SubHeartflow:
logger.error(f"{log_prefix} 停止 NormalChat 兴趣监控时出错: {e}") logger.error(f"{log_prefix} 停止 NormalChat 兴趣监控时出错: {e}")
logger.error(traceback.format_exc()) logger.error(traceback.format_exc())
try: try:
self.heart_fc_instance = HeartFChatting( self.heart_fc_instance = HeartFChatting(
chat_id=self.chat_id, chat_id=self.chat_id,
gpt_instance=self.gpt_instance, gpt_instance=self.gpt_instance,
@@ -394,7 +391,6 @@ class SubHeartflow:
else: else:
logger.warning(f"{log_prefix} NormalChat 实例不可用,无法管理其监控任务。") logger.warning(f"{log_prefix} NormalChat 实例不可用,无法管理其监控任务。")
async def subheartflow_start_working(self): async def subheartflow_start_working(self):
"""启动子心流的后台任务 """启动子心流的后台任务
@@ -410,8 +406,6 @@ class SubHeartflow:
logger.info(f"{self.log_prefix} 子心流后台任务已停止。") logger.info(f"{self.log_prefix} 子心流后台任务已停止。")
async def do_thinking_before_reply( async def do_thinking_before_reply(
self, self,
extra_info: str, extra_info: str,
@@ -550,6 +544,7 @@ class SubHeartflow:
def clear_interest_dict(self): def clear_interest_dict(self):
self.interest_chatting.interest_dict.clear() self.interest_chatting.interest_dict.clear()
async def shutdown(self): async def shutdown(self):
"""安全地关闭子心流及其管理的任务""" """安全地关闭子心流及其管理的任务"""
if self.should_stop: if self.should_stop:

View File

@@ -104,9 +104,11 @@ class SubHeartflowManager:
# 设置状态为ABSENT释放资源 # 设置状态为ABSENT释放资源
if subheartflow.chat_state.chat_status != ChatState.ABSENT: if subheartflow.chat_state.chat_status != ChatState.ABSENT:
logger.debug(f"[子心流管理] 设置 {stream_name} 状态为ABSENT") logger.debug(f"[子心流管理] 设置 {stream_name} 状态为ABSENT")
states_num =(self.count_subflows_by_state(ChatState.ABSENT), states_num = (
self.count_subflows_by_state(ChatState.ABSENT),
self.count_subflows_by_state(ChatState.CHAT), self.count_subflows_by_state(ChatState.CHAT),
self.count_subflows_by_state(ChatState.FOCUSED)) self.count_subflows_by_state(ChatState.FOCUSED),
)
await subheartflow.set_chat_state(ChatState.ABSENT, states_num) await subheartflow.set_chat_state(ChatState.ABSENT, states_num)
else: else:
logger.debug(f"[子心流管理] {stream_name} 已是ABSENT状态") logger.debug(f"[子心流管理] {stream_name} 已是ABSENT状态")
@@ -224,9 +226,11 @@ class SubHeartflowManager:
logger.debug(f"[激活] 正在激活子心流{stream_name}") logger.debug(f"[激活] 正在激活子心流{stream_name}")
states_num =(self.count_subflows_by_state(ChatState.ABSENT), states_num = (
self.count_subflows_by_state(ChatState.ABSENT),
self.count_subflows_by_state(ChatState.CHAT), self.count_subflows_by_state(ChatState.CHAT),
self.count_subflows_by_state(ChatState.FOCUSED)) self.count_subflows_by_state(ChatState.FOCUSED),
)
await flow.set_chat_state(ChatState.CHAT, states_num) await flow.set_chat_state(ChatState.CHAT, states_num)
@@ -272,9 +276,11 @@ class SubHeartflowManager:
should_promote = await sub_hf.should_evaluate_reply() should_promote = await sub_hf.should_evaluate_reply()
if should_promote: if should_promote:
logger.info(f"{log_prefix} 兴趣评估触发升级: CHAT -> FOCUSED") logger.info(f"{log_prefix} 兴趣评估触发升级: CHAT -> FOCUSED")
states_num =(self.count_subflows_by_state(ChatState.ABSENT), states_num = (
self.count_subflows_by_state(ChatState.ABSENT),
self.count_subflows_by_state(ChatState.CHAT), self.count_subflows_by_state(ChatState.CHAT),
self.count_subflows_by_state(ChatState.FOCUSED)) self.count_subflows_by_state(ChatState.FOCUSED),
)
await sub_hf.set_chat_state(ChatState.FOCUSED, states_num) await sub_hf.set_chat_state(ChatState.FOCUSED, states_num)
if ( if (
self.subheartflows.get(flow_id) self.subheartflows.get(flow_id)
@@ -343,15 +349,6 @@ class SubHeartflowManager:
else: else:
logger.warning(f"尝试停用不存在的 SubHeartflow: {subheartflow_id}") logger.warning(f"尝试停用不存在的 SubHeartflow: {subheartflow_id}")
async def deactivate_all_subflows(self):
"""停用并移除所有子心流。"""
logger.info("正在停用所有 SubHeartflows...")
# 获取当前所有 ID避免在迭代时修改字典
all_ids = self.get_all_subheartflows_ids()
tasks = [self.deactivate_subflow(sub_id) for sub_id in all_ids]
await asyncio.gather(*tasks)
logger.info("所有 SubHeartflows 已停用。")
async def cleanup_inactive_subflows(self, inactive_threshold_seconds: int): async def cleanup_inactive_subflows(self, inactive_threshold_seconds: int):
"""清理长时间不活跃的子心流。""" """清理长时间不活跃的子心流。"""
current_time = time.time() current_time = time.time()

View File

@@ -54,7 +54,6 @@ class NormalChat:
# 改为实例方法 # 改为实例方法
async def _create_thinking_message(self, message: MessageRecv) -> str: async def _create_thinking_message(self, message: MessageRecv) -> str:
"""创建思考消息""" """创建思考消息"""
userinfo = message.message_info.user_info
messageinfo = message.message_info messageinfo = message.message_info
bot_user_info = UserInfo( bot_user_info = UserInfo(
@@ -208,8 +207,6 @@ class NormalChat:
return return
timing_results = {} timing_results = {}
userinfo = message.message_info.user_info
messageinfo = message.message_info
reply_probability = 1.0 if is_mentioned else 0.0 # 如果被提及基础概率为1否则需要意愿判断 reply_probability = 1.0 if is_mentioned else 0.0 # 如果被提及基础概率为1否则需要意愿判断