refactor(context): 统一兴趣度计算的同步与异步调用
重构了 `_calculate_message_interest` 方法,使其能够同时兼容同步和异步调用场景。 通过内部嵌套一个异步函数 `_get_score` 来封装核心的兴趣度计算逻辑,并根据是否存在正在运行的 asyncio 事件循环来决定是直接 `await` 还是使用 `asyncio.run()` 执行。这消除了对独立同步和异步方法的需要,简化了代码结构,并提高了在不同执行上下文中的健壮性。 同时,优化了异常处理和日志记录,对插件加载失败和计算失败提供了更清晰的调试信息。
This commit is contained in:
@@ -226,56 +226,42 @@ class SingleStreamContextManager:
|
|||||||
self.access_count += 1
|
self.access_count += 1
|
||||||
|
|
||||||
async def _calculate_message_interest(self, message: DatabaseMessages) -> float:
|
async def _calculate_message_interest(self, message: DatabaseMessages) -> float:
|
||||||
"""异步实现:使用插件的异步评分器正确 await 计算兴趣度并返回分数。"""
|
"""
|
||||||
try:
|
异步计算消息的兴趣度。
|
||||||
|
此方法通过检查当前是否存在正在运行的 asyncio 事件循环来兼容同步和异步调用。
|
||||||
|
"""
|
||||||
|
# 内部异步函数,封装实际的计算逻辑
|
||||||
|
async def _get_score():
|
||||||
try:
|
try:
|
||||||
from src.plugins.built_in.affinity_flow_chatter.interest_scoring import (
|
from src.plugins.built_in.affinity_flow_chatter.interest_scoring import (
|
||||||
chatter_interest_scoring_system,
|
chatter_interest_scoring_system,
|
||||||
)
|
)
|
||||||
try:
|
interest_score = await chatter_interest_scoring_system._calculate_single_message_score(
|
||||||
interest_score = await chatter_interest_scoring_system._calculate_single_message_score(
|
message=message, bot_nickname=global_config.bot.nickname
|
||||||
message=message, bot_nickname=global_config.bot.nickname
|
|
||||||
)
|
|
||||||
interest_value = interest_score.total_score
|
|
||||||
logger.debug(f"使用插件内部系统计算兴趣度: {interest_value:.3f}")
|
|
||||||
return interest_value
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning(f"插件内部兴趣度计算失败: {e}")
|
|
||||||
return 0.5
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning(f"插件内部兴趣度计算加载失败,使用默认值: {e}")
|
|
||||||
return 0.5
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"计算消息兴趣度失败: {e}")
|
|
||||||
return 0.5
|
|
||||||
|
|
||||||
async def _calculate_message_interest_async(self, message: DatabaseMessages) -> float:
|
|
||||||
"""异步实现:使用插件的异步评分器正确 await 计算兴趣度并返回分数。"""
|
|
||||||
try:
|
|
||||||
try:
|
|
||||||
from src.plugins.built_in.affinity_flow_chatter.interest_scoring import (
|
|
||||||
chatter_interest_scoring_system,
|
|
||||||
)
|
)
|
||||||
|
interest_value = interest_score.total_score
|
||||||
# 直接 await 插件的异步方法
|
logger.debug(f"使用插件内部系统计算兴趣度: {interest_value:.3f}")
|
||||||
try:
|
return interest_value
|
||||||
interest_score = await chatter_interest_scoring_system._calculate_single_message_score(
|
except ImportError as e:
|
||||||
message=message, bot_nickname=global_config.bot.nickname
|
logger.debug(f"兴趣度计算插件加载失败,可能未启用: {e}")
|
||||||
)
|
return 0.5
|
||||||
interest_value = interest_score.total_score
|
|
||||||
logger.debug(f"使用插件内部系统计算兴趣度: {interest_value:.3f}")
|
|
||||||
return interest_value
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning(f"插件内部兴趣度计算失败: {e}")
|
|
||||||
return 0.5
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"插件内部兴趣度计算加载失败,使用默认值: {e}")
|
# 在某些情况下(例如机器人自己的消息),没有兴趣度是正常的
|
||||||
|
logger.info(f"插件内部兴趣度计算失败,使用默认值: {e}")
|
||||||
return 0.5
|
return 0.5
|
||||||
|
|
||||||
except Exception as e:
|
# 检查并获取当前事件循环
|
||||||
logger.error(f"计算消息兴趣度失败: {e}")
|
try:
|
||||||
return 0.5
|
loop = asyncio.get_running_loop()
|
||||||
|
except RuntimeError: # 'RuntimeError: There is no current event loop...'
|
||||||
|
loop = None
|
||||||
|
|
||||||
|
if loop and loop.is_running():
|
||||||
|
# 如果事件循环正在运行,直接 await
|
||||||
|
return await _get_score()
|
||||||
|
else:
|
||||||
|
# 否则,使用 asyncio.run() 来安全执行
|
||||||
|
return asyncio.run(_get_score())
|
||||||
|
|
||||||
async def add_message_async(self, message: DatabaseMessages, skip_energy_update: bool = False) -> bool:
|
async def add_message_async(self, message: DatabaseMessages, skip_energy_update: bool = False) -> bool:
|
||||||
"""异步实现的 add_message:将消息添加到 context,并 await 能量更新与分发。"""
|
"""异步实现的 add_message:将消息添加到 context,并 await 能量更新与分发。"""
|
||||||
|
|||||||
Reference in New Issue
Block a user