feat(waiting): 添加连续等待超时次数上限配置,优化等待策略
This commit is contained in:
@@ -950,6 +950,12 @@ class KokoroFlowChatterWaitingConfig(ValidatedConfigBase):
|
||||
le=10.0,
|
||||
description="等待时长倍率,用于整体放大或缩短LLM给出的等待时间",
|
||||
)
|
||||
max_consecutive_timeouts: int = Field(
|
||||
default=3,
|
||||
ge=0,
|
||||
le=10,
|
||||
description="允许的连续等待超时次数上限,达到后不再等待用户回复 (0 表示不限制)",
|
||||
)
|
||||
|
||||
|
||||
class KokoroFlowChatterConfig(ValidatedConfigBase):
|
||||
|
||||
@@ -179,11 +179,26 @@ class KokoroFlowChatter(BaseChatter):
|
||||
)
|
||||
|
||||
# 10. 执行动作
|
||||
adjusted_wait = apply_wait_duration_rules(plan_response.max_wait_seconds)
|
||||
if adjusted_wait != plan_response.max_wait_seconds:
|
||||
raw_wait = plan_response.max_wait_seconds
|
||||
adjusted_wait = apply_wait_duration_rules(
|
||||
raw_wait,
|
||||
session.consecutive_timeout_count,
|
||||
)
|
||||
timeout_limit = max(0, self._config.waiting.max_consecutive_timeouts)
|
||||
if (
|
||||
timeout_limit
|
||||
and session.consecutive_timeout_count >= timeout_limit
|
||||
and raw_wait > 0
|
||||
and adjusted_wait == 0
|
||||
):
|
||||
logger.info(
|
||||
"[KFC] 连续等待 %s 次未收到回复,暂停继续等待",
|
||||
session.consecutive_timeout_count,
|
||||
)
|
||||
elif adjusted_wait != raw_wait:
|
||||
logger.debug(
|
||||
"[KFC] 调整等待时长: raw=%ss adjusted=%ss",
|
||||
plan_response.max_wait_seconds,
|
||||
raw_wait,
|
||||
adjusted_wait,
|
||||
)
|
||||
plan_response.max_wait_seconds = adjusted_wait
|
||||
|
||||
@@ -51,6 +51,9 @@ class WaitingDefaults:
|
||||
# 等待时长倍率(>1 放大等待时间,<1 缩短)
|
||||
wait_duration_multiplier: float = 1.0
|
||||
|
||||
# 连续等待超时上限(达到后不再继续等待,0 表示不限制)
|
||||
max_consecutive_timeouts: int = 3
|
||||
|
||||
|
||||
@dataclass
|
||||
class ProactiveConfig:
|
||||
@@ -206,6 +209,7 @@ def load_config() -> KokoroFlowChatterConfig:
|
||||
min_wait_seconds=getattr(wait_cfg, 'min_wait_seconds', 30),
|
||||
max_wait_seconds=getattr(wait_cfg, 'max_wait_seconds', 1800),
|
||||
wait_duration_multiplier=getattr(wait_cfg, 'wait_duration_multiplier', 1.0),
|
||||
max_consecutive_timeouts=getattr(wait_cfg, 'max_consecutive_timeouts', 3),
|
||||
)
|
||||
|
||||
# 主动思考配置 - 支持 proactive 和 proactive_thinking 两种写法
|
||||
@@ -268,7 +272,7 @@ def reload_config() -> KokoroFlowChatterConfig:
|
||||
return _config
|
||||
|
||||
|
||||
def apply_wait_duration_rules(raw_wait_seconds: int) -> int:
|
||||
def apply_wait_duration_rules(raw_wait_seconds: int, consecutive_timeouts: int = 0) -> int:
|
||||
"""根据配置计算最终的等待时间"""
|
||||
if raw_wait_seconds <= 0:
|
||||
return 0
|
||||
@@ -291,4 +295,10 @@ def apply_wait_duration_rules(raw_wait_seconds: int) -> int:
|
||||
if min_wait > 0:
|
||||
adjusted = max(adjusted, min_wait)
|
||||
|
||||
return max(adjusted, 0)
|
||||
adjusted = max(adjusted, 0)
|
||||
|
||||
timeout_limit = max(0, waiting_cfg.max_consecutive_timeouts)
|
||||
if timeout_limit and consecutive_timeouts >= timeout_limit:
|
||||
return 0
|
||||
|
||||
return adjusted
|
||||
|
||||
@@ -83,6 +83,7 @@ class ProactiveThinker:
|
||||
"""加载配置 - 使用统一的配置系统"""
|
||||
config = get_config()
|
||||
proactive_cfg = config.proactive
|
||||
self._waiting_cfg = config.waiting
|
||||
|
||||
# 工作模式
|
||||
self._mode = config.mode
|
||||
@@ -461,11 +462,26 @@ class ProactiveThinker:
|
||||
action.params["situation_type"] = "timeout"
|
||||
action.params["extra_context"] = extra_context
|
||||
|
||||
adjusted_wait = apply_wait_duration_rules(plan_response.max_wait_seconds)
|
||||
if adjusted_wait != plan_response.max_wait_seconds:
|
||||
raw_wait = plan_response.max_wait_seconds
|
||||
adjusted_wait = apply_wait_duration_rules(
|
||||
raw_wait,
|
||||
session.consecutive_timeout_count,
|
||||
)
|
||||
timeout_limit = max(0, getattr(self._waiting_cfg, "max_consecutive_timeouts", 0))
|
||||
if (
|
||||
timeout_limit
|
||||
and session.consecutive_timeout_count >= timeout_limit
|
||||
and raw_wait > 0
|
||||
and adjusted_wait == 0
|
||||
):
|
||||
logger.info(
|
||||
"[ProactiveThinker] 连续等待 %s 次未获回复,停止继续等待",
|
||||
session.consecutive_timeout_count,
|
||||
)
|
||||
elif adjusted_wait != raw_wait:
|
||||
logger.debug(
|
||||
"[ProactiveThinker] 调整超时等待: raw=%ss adjusted=%ss",
|
||||
plan_response.max_wait_seconds,
|
||||
raw_wait,
|
||||
adjusted_wait,
|
||||
)
|
||||
plan_response.max_wait_seconds = adjusted_wait
|
||||
@@ -693,11 +709,26 @@ class ProactiveThinker:
|
||||
action.params["situation_type"] = "proactive"
|
||||
action.params["extra_context"] = extra_context
|
||||
|
||||
adjusted_wait = apply_wait_duration_rules(plan_response.max_wait_seconds)
|
||||
if adjusted_wait != plan_response.max_wait_seconds:
|
||||
raw_wait = plan_response.max_wait_seconds
|
||||
adjusted_wait = apply_wait_duration_rules(
|
||||
raw_wait,
|
||||
session.consecutive_timeout_count,
|
||||
)
|
||||
timeout_limit = max(0, getattr(self._waiting_cfg, "max_consecutive_timeouts", 0))
|
||||
if (
|
||||
timeout_limit
|
||||
and session.consecutive_timeout_count >= timeout_limit
|
||||
and raw_wait > 0
|
||||
and adjusted_wait == 0
|
||||
):
|
||||
logger.info(
|
||||
"[ProactiveThinker] 连续等待 %s 次未获回复,主动无需再等",
|
||||
session.consecutive_timeout_count,
|
||||
)
|
||||
elif adjusted_wait != raw_wait:
|
||||
logger.debug(
|
||||
"[ProactiveThinker] 调整主动等待: raw=%ss adjusted=%ss",
|
||||
plan_response.max_wait_seconds,
|
||||
raw_wait,
|
||||
adjusted_wait,
|
||||
)
|
||||
plan_response.max_wait_seconds = adjusted_wait
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
[inner]
|
||||
version = "7.9.7"
|
||||
version = "7.9.8"
|
||||
|
||||
#----以下是给开发人员阅读的,如果你只是部署了MoFox-Bot,不需要阅读----
|
||||
#如果你想要修改配置文件,请递增version的值
|
||||
@@ -628,6 +628,7 @@ default_max_wait_seconds = 300 # LLM 未给出等待时间时的默认值
|
||||
min_wait_seconds = 30 # 允许的最短等待时间,防止太快打扰用户
|
||||
max_wait_seconds = 1800 # 允许的最长等待时间(秒)
|
||||
wait_duration_multiplier = 1.0 # 对 LLM 给出的等待时间应用的倍率(>1 放大,<1 缩短)
|
||||
max_consecutive_timeouts = 3 # 连续等待超时达到该次数后,强制不再继续等待(0 表示不限制)
|
||||
|
||||
# --- 私聊专属主动思考配置 ---
|
||||
# 注意:这是KFC专属的主动思考配置,只有当KFC启用时才生效。
|
||||
|
||||
Reference in New Issue
Block a user