增加了组件的局部禁用方法
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
import traceback
|
||||
|
||||
from typing import Dict, Optional, Type
|
||||
from src.plugin_system.base.base_action import BaseAction
|
||||
from src.chat.message_receive.chat_stream import ChatStream
|
||||
@@ -24,38 +22,13 @@ class ActionManager:
|
||||
|
||||
def __init__(self):
|
||||
"""初始化动作管理器"""
|
||||
# 所有注册的动作集合
|
||||
self._registered_actions: Dict[str, ActionInfo] = {}
|
||||
|
||||
# 当前正在使用的动作集合,默认加载默认动作
|
||||
self._using_actions: Dict[str, ActionInfo] = {}
|
||||
|
||||
# 初始化管理器注册表
|
||||
self._load_plugin_system_actions()
|
||||
|
||||
# 初始化时将默认动作加载到使用中的动作
|
||||
self._using_actions = component_registry.get_default_actions()
|
||||
|
||||
def _load_plugin_system_actions(self) -> None:
|
||||
"""从插件系统的component_registry加载Action组件"""
|
||||
try:
|
||||
# 获取所有Action组件
|
||||
action_components: Dict[str, ActionInfo] = component_registry.get_components_by_type(ComponentType.ACTION) # type: ignore
|
||||
|
||||
for action_name, action_info in action_components.items():
|
||||
if action_name in self._registered_actions:
|
||||
logger.debug(f"Action组件 {action_name} 已存在,跳过")
|
||||
continue
|
||||
|
||||
self._registered_actions[action_name] = action_info
|
||||
|
||||
logger.debug(f"从插件系统加载Action组件: {action_name} (插件: {action_info.plugin_name})")
|
||||
|
||||
logger.info(f"加载了 {len(action_components)} 个Action动作")
|
||||
logger.debug("从插件系统加载Action组件成功")
|
||||
except Exception as e:
|
||||
logger.error(f"从插件系统加载Action组件失败: {e}")
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
# === 执行Action方法 ===
|
||||
|
||||
def create_action(
|
||||
@@ -127,44 +100,10 @@ class ActionManager:
|
||||
logger.error(traceback.format_exc())
|
||||
return None
|
||||
|
||||
def get_registered_actions(self) -> Dict[str, ActionInfo]:
|
||||
"""获取所有已注册的动作集"""
|
||||
return self._registered_actions.copy()
|
||||
|
||||
def get_using_actions(self) -> Dict[str, ActionInfo]:
|
||||
"""获取当前正在使用的动作集合"""
|
||||
return self._using_actions.copy()
|
||||
|
||||
# === 增删Action方法 ===
|
||||
def add_action(self, action_name: str) -> bool:
|
||||
"""增加一个Action到管理器
|
||||
|
||||
Parameters:
|
||||
action_name: 动作名称
|
||||
Returns:
|
||||
bool: 添加是否成功
|
||||
"""
|
||||
if action_name in self._registered_actions:
|
||||
return True
|
||||
component_info: ActionInfo = component_registry.get_component_info(action_name, ComponentType.ACTION) # type: ignore
|
||||
if not component_info:
|
||||
logger.warning(f"添加失败: 动作 {action_name} 未注册")
|
||||
return False
|
||||
self._registered_actions[action_name] = component_info
|
||||
return True
|
||||
|
||||
def remove_action(self, action_name: str) -> bool:
|
||||
"""从注册集移除指定动作
|
||||
Parameters:
|
||||
action_name: 动作名称
|
||||
Returns:
|
||||
bool: 移除是否成功
|
||||
"""
|
||||
if action_name not in self._registered_actions:
|
||||
return False
|
||||
del self._registered_actions[action_name]
|
||||
return True
|
||||
|
||||
# === Modify相关方法 ===
|
||||
def remove_action_from_using(self, action_name: str) -> bool:
|
||||
"""
|
||||
@@ -189,47 +128,3 @@ class ActionManager:
|
||||
actions_to_restore = list(self._using_actions.keys())
|
||||
self._using_actions = component_registry.get_default_actions()
|
||||
logger.debug(f"恢复动作集: 从 {actions_to_restore} 恢复到默认动作集 {list(self._using_actions.keys())}")
|
||||
|
||||
# def add_action_to_using(self, action_name: str) -> bool:
|
||||
|
||||
# """
|
||||
# 添加已注册的动作到当前使用的动作集
|
||||
|
||||
# Args:
|
||||
# action_name: 动作名称
|
||||
|
||||
# Returns:
|
||||
# bool: 添加是否成功
|
||||
# """
|
||||
# if action_name not in self._registered_actions:
|
||||
# logger.warning(f"添加失败: 动作 {action_name} 未注册")
|
||||
# return False
|
||||
|
||||
# if action_name in self._using_actions:
|
||||
# logger.info(f"动作 {action_name} 已经在使用中")
|
||||
# return True
|
||||
|
||||
# self._using_actions[action_name] = self._registered_actions[action_name]
|
||||
# logger.info(f"添加动作 {action_name} 到使用集")
|
||||
# return True
|
||||
|
||||
# def temporarily_remove_actions(self, actions_to_remove: List[str]) -> None:
|
||||
# """临时移除使用集中的指定动作"""
|
||||
# for name in actions_to_remove:
|
||||
# self._using_actions.pop(name, None)
|
||||
|
||||
# def add_system_action_if_needed(self, action_name: str) -> bool:
|
||||
# """
|
||||
# 根据需要添加系统动作到使用集
|
||||
|
||||
# Args:
|
||||
# action_name: 动作名称
|
||||
|
||||
# Returns:
|
||||
# bool: 是否成功添加
|
||||
# """
|
||||
# if action_name in self._registered_actions and action_name not in self._using_actions:
|
||||
# self._using_actions[action_name] = self._registered_actions[action_name]
|
||||
# logger.info(f"临时添加系统动作到使用集: {action_name}")
|
||||
# return True
|
||||
# return False
|
||||
|
||||
@@ -2,7 +2,7 @@ import random
|
||||
import asyncio
|
||||
import hashlib
|
||||
import time
|
||||
from typing import List, Any, Dict, TYPE_CHECKING
|
||||
from typing import List, Any, Dict, TYPE_CHECKING, Tuple
|
||||
|
||||
from src.common.logger import get_logger
|
||||
from src.config.config import global_config
|
||||
@@ -11,6 +11,7 @@ from src.chat.message_receive.chat_stream import get_chat_manager, ChatMessageCo
|
||||
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.core.global_announcement_manager import global_announcement_manager
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from src.chat.message_receive.chat_stream import ChatStream
|
||||
@@ -60,8 +61,9 @@ class ActionModifier:
|
||||
"""
|
||||
logger.debug(f"{self.log_prefix}开始完整动作修改流程")
|
||||
|
||||
removals_s1 = []
|
||||
removals_s2 = []
|
||||
removals_s1: List[Tuple[str, str]] = []
|
||||
removals_s2: List[Tuple[str, str]] = []
|
||||
removals_s3: List[Tuple[str, str]] = []
|
||||
|
||||
self.action_manager.restore_actions()
|
||||
all_actions = self.action_manager.get_using_actions()
|
||||
@@ -83,25 +85,28 @@ class ActionModifier:
|
||||
if message_content:
|
||||
chat_content = chat_content + "\n" + f"现在,最新的消息是:{message_content}"
|
||||
|
||||
# === 第一阶段:传统观察处理 ===
|
||||
# if history_loop:
|
||||
# removals_from_loop = await self.analyze_loop_actions(history_loop)
|
||||
# if removals_from_loop:
|
||||
# removals_s1.extend(removals_from_loop)
|
||||
# === 第一阶段:去除用户自行禁用的 ===
|
||||
disabled_actions = global_announcement_manager.get_disabled_chat_actions(self.chat_id)
|
||||
if disabled_actions:
|
||||
for disabled_action_name in disabled_actions:
|
||||
if disabled_action_name in all_actions:
|
||||
removals_s1.append((disabled_action_name, "用户自行禁用"))
|
||||
self.action_manager.remove_action_from_using(disabled_action_name)
|
||||
logger.debug(f"{self.log_prefix}阶段一移除动作: {disabled_action_name},原因: 用户自行禁用")
|
||||
|
||||
# 检查动作的关联类型
|
||||
# === 第二阶段:检查动作的关联类型 ===
|
||||
chat_context = self.chat_stream.context
|
||||
type_mismatched_actions = self._check_action_associated_types(all_actions, chat_context)
|
||||
|
||||
if type_mismatched_actions:
|
||||
removals_s1.extend(type_mismatched_actions)
|
||||
removals_s2.extend(type_mismatched_actions)
|
||||
|
||||
# 应用第一阶段的移除
|
||||
for action_name, reason in removals_s1:
|
||||
# 应用第二阶段的移除
|
||||
for action_name, reason in removals_s2:
|
||||
self.action_manager.remove_action_from_using(action_name)
|
||||
logger.debug(f"{self.log_prefix}阶段一移除动作: {action_name},原因: {reason}")
|
||||
logger.debug(f"{self.log_prefix}阶段二移除动作: {action_name},原因: {reason}")
|
||||
|
||||
# === 第二阶段:激活类型判定 ===
|
||||
# === 第三阶段:激活类型判定 ===
|
||||
if chat_content is not None:
|
||||
logger.debug(f"{self.log_prefix}开始激活类型判定阶段")
|
||||
|
||||
@@ -109,18 +114,18 @@ class ActionModifier:
|
||||
current_using_actions = self.action_manager.get_using_actions()
|
||||
|
||||
# 获取因激活类型判定而需要移除的动作
|
||||
removals_s2 = await self._get_deactivated_actions_by_type(
|
||||
removals_s3 = await self._get_deactivated_actions_by_type(
|
||||
current_using_actions,
|
||||
chat_content,
|
||||
)
|
||||
|
||||
# 应用第二阶段的移除
|
||||
for action_name, reason in removals_s2:
|
||||
# 应用第三阶段的移除
|
||||
for action_name, reason in removals_s3:
|
||||
self.action_manager.remove_action_from_using(action_name)
|
||||
logger.debug(f"{self.log_prefix}阶段二移除动作: {action_name},原因: {reason}")
|
||||
logger.debug(f"{self.log_prefix}阶段三移除动作: {action_name},原因: {reason}")
|
||||
|
||||
# === 统一日志记录 ===
|
||||
all_removals = removals_s1 + removals_s2
|
||||
all_removals = removals_s1 + removals_s2 + removals_s3
|
||||
removals_summary: str = ""
|
||||
if all_removals:
|
||||
removals_summary = " | ".join([f"{name}({reason})" for name, reason in all_removals])
|
||||
@@ -130,7 +135,7 @@ class ActionModifier:
|
||||
)
|
||||
|
||||
def _check_action_associated_types(self, all_actions: Dict[str, ActionInfo], chat_context: ChatMessageContext):
|
||||
type_mismatched_actions = []
|
||||
type_mismatched_actions: List[Tuple[str, str]] = []
|
||||
for action_name, action_info in all_actions.items():
|
||||
if action_info.associated_types and not chat_context.check_types(action_info.associated_types):
|
||||
associated_types_str = ", ".join(action_info.associated_types)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import json
|
||||
import time
|
||||
import traceback
|
||||
from typing import Dict, Any, Optional, Tuple
|
||||
from typing import Dict, Any, Optional, Tuple, List
|
||||
from rich.traceback import install
|
||||
from datetime import datetime
|
||||
from json_repair import repair_json
|
||||
@@ -19,8 +19,8 @@ from src.chat.utils.chat_message_builder import (
|
||||
from src.chat.utils.utils import get_chat_type_and_target_info
|
||||
from src.chat.planner_actions.action_manager import ActionManager
|
||||
from src.chat.message_receive.chat_stream import get_chat_manager
|
||||
from src.plugin_system.base.component_types import ActionInfo, ChatMode
|
||||
|
||||
from src.plugin_system.base.component_types import ActionInfo, ChatMode, ComponentType
|
||||
from src.plugin_system.core.component_registry import component_registry
|
||||
|
||||
logger = get_logger("planner")
|
||||
|
||||
@@ -99,7 +99,7 @@ class ActionPlanner:
|
||||
|
||||
async def plan(
|
||||
self, mode: ChatMode = ChatMode.FOCUS
|
||||
) -> Tuple[Dict[str, Dict[str, Any] | str], Optional[Dict[str, Any]]]: # sourcery skip: dict-comprehension
|
||||
) -> Tuple[Dict[str, Dict[str, Any] | str], Optional[Dict[str, Any]]]:
|
||||
"""
|
||||
规划器 (Planner): 使用LLM根据上下文决定做出什么动作。
|
||||
"""
|
||||
@@ -119,9 +119,11 @@ class ActionPlanner:
|
||||
current_available_actions_dict = self.action_manager.get_using_actions()
|
||||
|
||||
# 获取完整的动作信息
|
||||
all_registered_actions = self.action_manager.get_registered_actions()
|
||||
|
||||
for action_name in current_available_actions_dict.keys():
|
||||
all_registered_actions: List[ActionInfo] = list(
|
||||
component_registry.get_components_by_type(ComponentType.ACTION).values() # type: ignore
|
||||
)
|
||||
current_available_actions = {}
|
||||
for action_name in current_available_actions_dict:
|
||||
if action_name in all_registered_actions:
|
||||
current_available_actions[action_name] = all_registered_actions[action_name]
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user