修改import顺序,把魔法字变为枚举型

This commit is contained in:
UnCLAS-Prommer
2025-07-09 21:54:43 +08:00
parent 24e5b95cdb
commit d5cd0e8538
12 changed files with 98 additions and 185 deletions

View File

@@ -5,6 +5,9 @@ class ChatState(enum.Enum):
ABSENT = "没在看群"
NORMAL = "随便水群"
FOCUSED = "认真水群"
def __str__(self):
return self.name
class ChatStateInfo:

View File

@@ -16,14 +16,11 @@ class Heartflow:
async def get_or_create_subheartflow(self, subheartflow_id: Any) -> Optional["SubHeartflow"]:
"""获取或创建一个新的SubHeartflow实例"""
if subheartflow_id in self.subheartflows:
subflow = self.subheartflows.get(subheartflow_id)
if subflow:
if subflow := self.subheartflows.get(subheartflow_id):
return subflow
try:
new_subflow = SubHeartflow(
subheartflow_id,
)
new_subflow = SubHeartflow(subheartflow_id)
await new_subflow.initialize()

View File

@@ -123,7 +123,7 @@ class HeartFCMessageReceiver:
logger.debug(f"[{mes_name}][当前时段回复频率: {current_talk_frequency}]")
# 8. 关系处理
# 4. 关系处理
if global_config.relationship.enable_relationship:
await _process_relationship(message)

View File

@@ -72,27 +72,28 @@ class SubHeartflow:
停止 NormalChat 实例
切出 CHAT 状态时使用
"""
if self.normal_chat_instance:
logger.info(f"{self.log_prefix} 离开normal模式")
try:
logger.debug(f"{self.log_prefix} 开始调用 stop_chat()")
# 使用更短的超时时间,强制快速停止
await asyncio.wait_for(self.normal_chat_instance.stop_chat(), timeout=3.0)
logger.debug(f"{self.log_prefix} stop_chat() 调用完成")
except asyncio.TimeoutError:
logger.warning(f"{self.log_prefix} 停止 NormalChat 超时,强制清理")
# 超时强制清理实例
if not self.normal_chat_instance:
return
logger.info(f"{self.log_prefix} 离开normal模式")
try:
logger.debug(f"{self.log_prefix} 开始调用 stop_chat()")
# 使用更短的超时时间,强制快速停止
await asyncio.wait_for(self.normal_chat_instance.stop_chat(), timeout=3.0)
logger.debug(f"{self.log_prefix} stop_chat() 调用完成")
except asyncio.TimeoutError:
logger.warning(f"{self.log_prefix} 停止 NormalChat 超时强制清理")
# 超时时强制清理实例
self.normal_chat_instance = None
except Exception as e:
logger.error(f"{self.log_prefix} 停止 NormalChat 监控任务时出错: {e}")
# 出错时也要清理实例,避免状态不一致
self.normal_chat_instance = None
finally:
# 确保实例被清理
if self.normal_chat_instance:
logger.warning(f"{self.log_prefix} 强制清理 NormalChat 实例")
self.normal_chat_instance = None
except Exception as e:
logger.error(f"{self.log_prefix} 停止 NormalChat 监控任务时出错: {e}")
# 出错时也要清理实例,避免状态不一致
self.normal_chat_instance = None
finally:
# 确保实例被清理
if self.normal_chat_instance:
logger.warning(f"{self.log_prefix} 强制清理 NormalChat 实例")
self.normal_chat_instance = None
logger.debug(f"{self.log_prefix} _stop_normal_chat 完成")
logger.debug(f"{self.log_prefix} _stop_normal_chat 完成")
async def _start_normal_chat(self, rewind=False) -> bool:
"""
@@ -348,6 +349,4 @@ class SubHeartflow:
if elapsed_since_exit >= cooldown_duration:
return 1.0 # 冷却完成
# 计算进度0表示刚开始冷却1表示冷却完成
progress = elapsed_since_exit / cooldown_duration
return progress
return elapsed_since_exit / cooldown_duration