增加了卸载和重载插件功能
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
from typing import Dict, List, Optional, Type
|
||||
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
|
||||
from src.common.logger import get_logger
|
||||
@@ -27,24 +29,12 @@ class ActionManager:
|
||||
# 当前正在使用的动作集合,默认加载默认动作
|
||||
self._using_actions: Dict[str, ActionInfo] = {}
|
||||
|
||||
# 加载插件动作
|
||||
self._load_plugin_actions()
|
||||
# 初始化管理器注册表
|
||||
self._load_plugin_system_actions()
|
||||
|
||||
# 初始化时将默认动作加载到使用中的动作
|
||||
self._using_actions = component_registry.get_default_actions()
|
||||
|
||||
def _load_plugin_actions(self) -> None:
|
||||
"""
|
||||
加载所有插件系统中的动作
|
||||
"""
|
||||
try:
|
||||
# 从新插件系统获取Action组件
|
||||
self._load_plugin_system_actions()
|
||||
logger.debug("从插件系统加载Action组件成功")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"加载插件动作失败: {e}")
|
||||
|
||||
def _load_plugin_system_actions(self) -> None:
|
||||
"""从插件系统的component_registry加载Action组件"""
|
||||
try:
|
||||
@@ -58,18 +48,16 @@ class ActionManager:
|
||||
|
||||
self._registered_actions[action_name] = action_info
|
||||
|
||||
logger.debug(
|
||||
f"从插件系统加载Action组件: {action_name} (插件: {getattr(action_info, 'plugin_name', 'unknown')})"
|
||||
)
|
||||
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}")
|
||||
import traceback
|
||||
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
# === 执行Action方法 ===
|
||||
|
||||
def create_action(
|
||||
self,
|
||||
action_name: str,
|
||||
@@ -147,28 +135,37 @@ class ActionManager:
|
||||
"""获取当前正在使用的动作集合"""
|
||||
return self._using_actions.copy()
|
||||
|
||||
def add_action_to_using(self, action_name: str) -> bool:
|
||||
"""
|
||||
添加已注册的动作到当前使用的动作集
|
||||
# === 增删Action方法 ===
|
||||
def add_action(self, action_name: str) -> bool:
|
||||
"""增加一个Action到管理器
|
||||
|
||||
Args:
|
||||
Parameters:
|
||||
action_name: 动作名称
|
||||
|
||||
Returns:
|
||||
bool: 添加是否成功
|
||||
"""
|
||||
if action_name not in self._registered_actions:
|
||||
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
|
||||
|
||||
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} 到使用集")
|
||||
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:
|
||||
"""
|
||||
从当前使用的动作集中移除指定动作
|
||||
@@ -187,79 +184,52 @@ class ActionManager:
|
||||
logger.debug(f"已从使用集中移除动作 {action_name}")
|
||||
return True
|
||||
|
||||
# def add_action(self, action_name: str, description: str, parameters: Dict = None, require: List = None) -> bool:
|
||||
# """
|
||||
# 添加新的动作到注册集
|
||||
|
||||
# Args:
|
||||
# action_name: 动作名称
|
||||
# description: 动作描述
|
||||
# parameters: 动作参数定义,默认为空字典
|
||||
# require: 动作依赖项,默认为空列表
|
||||
|
||||
# Returns:
|
||||
# bool: 添加是否成功
|
||||
# """
|
||||
# if action_name in self._registered_actions:
|
||||
# return False
|
||||
|
||||
# if parameters is None:
|
||||
# parameters = {}
|
||||
# if require is None:
|
||||
# require = []
|
||||
|
||||
# action_info = {"description": description, "parameters": parameters, "require": require}
|
||||
|
||||
# self._registered_actions[action_name] = action_info
|
||||
# return True
|
||||
|
||||
def remove_action(self, action_name: str) -> bool:
|
||||
"""从注册集移除指定动作"""
|
||||
if action_name not in self._registered_actions:
|
||||
return False
|
||||
del self._registered_actions[action_name]
|
||||
# 如果在使用集中也存在,一并移除
|
||||
if action_name in self._using_actions:
|
||||
del self._using_actions[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 restore_actions(self) -> None:
|
||||
"""恢复到默认动作集"""
|
||||
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_system_action_if_needed(self, action_name: str) -> bool:
|
||||
"""
|
||||
根据需要添加系统动作到使用集
|
||||
# def add_action_to_using(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
|
||||
# Args:
|
||||
# action_name: 动作名称
|
||||
|
||||
def get_action(self, action_name: str) -> Optional[Type[BaseAction]]:
|
||||
"""
|
||||
获取指定动作的处理器类
|
||||
# Returns:
|
||||
# bool: 添加是否成功
|
||||
# """
|
||||
# if action_name not in self._registered_actions:
|
||||
# logger.warning(f"添加失败: 动作 {action_name} 未注册")
|
||||
# return False
|
||||
|
||||
Args:
|
||||
action_name: 动作名称
|
||||
# if action_name in self._using_actions:
|
||||
# logger.info(f"动作 {action_name} 已经在使用中")
|
||||
# return True
|
||||
|
||||
Returns:
|
||||
Optional[Type[BaseAction]]: 动作处理器类,如果不存在则返回None
|
||||
"""
|
||||
from src.plugin_system.core.component_registry import component_registry
|
||||
# self._using_actions[action_name] = self._registered_actions[action_name]
|
||||
# logger.info(f"添加动作 {action_name} 到使用集")
|
||||
# return True
|
||||
|
||||
return component_registry.get_component_class(action_name, ComponentType.ACTION) # type: ignore
|
||||
# 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
|
||||
|
||||
@@ -47,7 +47,6 @@ class ActionModifier:
|
||||
|
||||
async def modify_actions(
|
||||
self,
|
||||
history_loop=None,
|
||||
message_content: str = "",
|
||||
): # sourcery skip: use-named-expression
|
||||
"""
|
||||
@@ -318,7 +317,7 @@ class ActionModifier:
|
||||
action_name: str,
|
||||
action_info: ActionInfo,
|
||||
chat_content: str = "",
|
||||
) -> bool:
|
||||
) -> bool: # sourcery skip: move-assign-in-block, use-named-expression
|
||||
"""
|
||||
使用LLM判定是否应该激活某个action
|
||||
|
||||
|
||||
@@ -113,7 +113,6 @@ class ActionPlanner:
|
||||
|
||||
try:
|
||||
is_group_chat = True
|
||||
|
||||
is_group_chat, chat_target_info = get_chat_type_and_target_info(self.chat_id)
|
||||
logger.debug(f"{self.log_prefix}获取到聊天信息 - 群聊: {is_group_chat}, 目标信息: {chat_target_info}")
|
||||
|
||||
@@ -234,10 +233,13 @@ class ActionPlanner:
|
||||
"is_parallel": is_parallel,
|
||||
}
|
||||
|
||||
return {
|
||||
"action_result": action_result,
|
||||
"action_prompt": prompt,
|
||||
}, target_message
|
||||
return (
|
||||
{
|
||||
"action_result": action_result,
|
||||
"action_prompt": prompt,
|
||||
},
|
||||
target_message,
|
||||
)
|
||||
|
||||
async def build_planner_prompt(
|
||||
self,
|
||||
|
||||
@@ -619,9 +619,7 @@ def get_chat_type_and_target_info(chat_id: str) -> Tuple[bool, Optional[Dict]]:
|
||||
chat_target_info = None
|
||||
|
||||
try:
|
||||
chat_stream = get_chat_manager().get_stream(chat_id)
|
||||
|
||||
if chat_stream:
|
||||
if chat_stream := get_chat_manager().get_stream(chat_id):
|
||||
if chat_stream.group_info:
|
||||
is_group_chat = True
|
||||
chat_target_info = None # Explicitly None for group chat
|
||||
@@ -660,8 +658,6 @@ def get_chat_type_and_target_info(chat_id: str) -> Tuple[bool, Optional[Dict]]:
|
||||
chat_target_info = target_info
|
||||
else:
|
||||
logger.warning(f"无法获取 chat_stream for {chat_id} in utils")
|
||||
# Keep defaults: is_group_chat=False, chat_target_info=None
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"获取聊天类型和目标信息时出错 for {chat_id}: {e}", exc_info=True)
|
||||
# Keep defaults on error
|
||||
|
||||
Reference in New Issue
Block a user