fix(chat): 增强回复生成的健壮性,增加超时和类型检查

在 `DefaultReplyer` 的提示词构建流程中,为并行的子任务(如记忆、工具、关系等)增加了15秒的超时机制。这可以防止因某个子任务耗时过长或卡死而导致整个回复生成过程停滞。

同时,在能量系统中增加了对计算器分数和兴趣值的类型检查,确保它们是数值类型,避免了潜在的 `TypeError` 异常,提高了系统的稳定性。
This commit is contained in:
tt-P607
2025-10-02 23:22:00 +08:00
parent 58688c5e49
commit 844d4f3362
2 changed files with 40 additions and 24 deletions

View File

@@ -94,14 +94,10 @@ class InterestEnergyCalculator(EnergyCalculator):
for msg in messages:
interest_value = getattr(msg, "interest_value", None)
if interest_value is not None:
try:
interest_float = float(interest_value)
if 0.0 <= interest_float <= 1.0:
total_interest += interest_float
valid_messages += 1
except (ValueError, TypeError):
continue
if isinstance(interest_value, (int, float)):
if 0.0 <= interest_value <= 1.0:
total_interest += interest_value
valid_messages += 1
if valid_messages > 0:
avg_interest = total_interest / valid_messages
@@ -315,7 +311,12 @@ class EnergyManager:
weight = calculator.get_weight()
component_scores[calculator.__class__.__name__] = score
# 确保 score 是 float 类型
if not isinstance(score, (int, float)):
logger.warning(f"计算器 {calculator.__class__.__name__} 返回了非数值类型: {type(score)},跳过此组件")
continue
component_scores[calculator.__class__.__name__] = float(score)
total_weight += weight
logger.debug(f"{calculator.__class__.__name__} 能量: {score:.3f} (权重: {weight:.3f})")