feat(planner): 引入双模动作激活机制与混合触发类型

为了更精细地控制动作在不同聊天模式下的行为,并提升决策效率,本次更新引入了全新的动作激活机制。

- **双模激活**: 为 Action 新增 `normal_activation_type` 和 `focus_activation_type` 属性,允许插件在 `NORMAL` 和 `FOCUS` 模式下拥有不同的激活策略,使行为更符合上下文。

- **混合触发**: 新增 `KEYWORD_OR_LLM_JUDGE` 激活类型。该类型会先进行快速的关键词匹配,若未匹配成功,则回退至 LLM 进行判断,兼顾了响应速度和智能化。

- **流程优化**: 重构了 `PlanGenerator` 的动作筛选逻辑,使其在生成计划前,就根据当前聊天模式和简单的激活规则进行预筛选,为后续的 LLM 决策提供更精准、更高效的候选动作列表。
This commit is contained in:
tt-P607
2025-09-17 20:27:19 +08:00
committed by Windpicker-owo
parent c64a1f8ea5
commit 4890771a87
5 changed files with 60 additions and 54 deletions

View File

@@ -10,7 +10,7 @@ from src.llm_models.utils_model import LLMRequest
from src.chat.message_receive.chat_stream import get_chat_manager, ChatMessageContext
from src.chat.planner_actions.action_manager import ActionManager
from src.chat.utils.chat_message_builder import get_raw_msg_before_timestamp_with_chat, build_readable_messages
from src.plugin_system.base.component_types import ActionInfo, ActionActivationType
from src.plugin_system.base.component_types import ActionInfo, ActionActivationType, ChatMode
from src.plugin_system.core.global_announcement_manager import global_announcement_manager
if TYPE_CHECKING:
@@ -43,10 +43,7 @@ class ActionModifier:
self._cache_expiry_time = 30 # 缓存过期时间(秒)
self._last_context_hash = None # 上次上下文的哈希值
async def modify_actions(
self,
message_content: str = "",
): # sourcery skip: use-named-expression
async def modify_actions(self, mode: ChatMode, message_content: str = ""):
"""
动作修改流程,整合传统观察处理和新的激活类型判定
@@ -146,6 +143,7 @@ class ActionModifier:
removals_s3 = await self._get_deactivated_actions_by_type(
current_using_actions,
chat_content,
mode,
)
# 应用第三阶段的移除
@@ -178,6 +176,7 @@ class ActionModifier:
self,
actions_with_info: Dict[str, ActionInfo],
chat_content: str = "",
mode: ChatMode = ChatMode.NORMAL,
) -> List[tuple[str, str]]:
"""
根据激活类型过滤,返回需要停用的动作列表及原因
@@ -198,34 +197,26 @@ class ActionModifier:
random.shuffle(actions_to_check)
for action_name, action_info in actions_to_check:
activation_type = action_info.activation_type or action_info.focus_activation_type
if mode == ChatMode.FOCUS:
activation_type = action_info.focus_activation_type
else:
activation_type = action_info.normal_activation_type
if activation_type == ActionActivationType.ALWAYS:
continue # 总是激活,无需处理
continue
elif activation_type == ActionActivationType.RANDOM:
probability = action_info.random_activation_probability
probability = action_info.random_activation_probability
if random.random() >= probability:
reason = f"RANDOM类型未触发概率{probability}"
deactivated_actions.append((action_name, reason))
logger.debug(f"{self.log_prefix}未激活动作: {action_name},原因: {reason}")
if random.random() >= action_info.random_activation_probability:
deactivated_actions.append((action_name, f"RANDOM类型未触发概率{action_info.random_activation_probability}"))
elif activation_type == ActionActivationType.KEYWORD:
if not self._check_keyword_activation(action_name, action_info, chat_content):
keywords = action_info.activation_keywords
reason = f"关键词未匹配(关键词: {keywords}"
deactivated_actions.append((action_name, reason))
logger.debug(f"{self.log_prefix}未激活动作: {action_name},原因: {reason}")
deactivated_actions.append((action_name, f"关键词未匹配(关键词: {action_info.activation_keywords}"))
elif activation_type == ActionActivationType.LLM_JUDGE:
llm_judge_actions[action_name] = action_info
elif activation_type == ActionActivationType.KEYWORD_OR_LLM_JUDGE:
if not self._check_keyword_activation(action_name, action_info, chat_content):
llm_judge_actions[action_name] = action_info
elif activation_type == ActionActivationType.NEVER:
reason = "激活类型为never"
deactivated_actions.append((action_name, reason))
logger.debug(f"{self.log_prefix}未激活动作: {action_name},原因: 激活类型为never")
deactivated_actions.append((action_name, "激活类型为never"))
else:
logger.warning(f"{self.log_prefix}未知的激活类型: {activation_type},跳过处理")