fix:修复循环导入问题
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -310,4 +310,4 @@ src/plugins/test_plugin_pic/actions/pic_action_config.toml
|
|||||||
run_pet.bat
|
run_pet.bat
|
||||||
|
|
||||||
# 忽略 /src/plugins 但保留特定目录
|
# 忽略 /src/plugins 但保留特定目录
|
||||||
/plugins/*
|
plugins/*
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
from typing import Dict, List, Optional, Type, Any
|
from typing import Dict, List, Optional, Type, Any
|
||||||
from src.plugin_system.base.base_action import BaseAction, _ACTION_REGISTRY
|
from src.plugin_system.base.base_action import BaseAction
|
||||||
from src.chat.heart_flow.observation.observation import Observation
|
from src.chat.heart_flow.observation.observation import Observation
|
||||||
from src.chat.focus_chat.replyer.default_replyer import DefaultReplyer
|
from src.chat.focus_chat.replyer.default_replyer import DefaultReplyer
|
||||||
from src.chat.focus_chat.expressors.default_expressor import DefaultExpressor
|
from src.chat.focus_chat.expressors.default_expressor import DefaultExpressor
|
||||||
@@ -121,8 +121,12 @@ class ActionManager:
|
|||||||
加载所有通过装饰器注册的动作
|
加载所有通过装饰器注册的动作
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
# 从_ACTION_REGISTRY获取所有已注册动作
|
# 从组件注册中心获取所有已注册的action
|
||||||
for action_name, action_class in _ACTION_REGISTRY.items():
|
from src.plugin_system.core.component_registry import component_registry
|
||||||
|
action_registry = component_registry.get_action_registry()
|
||||||
|
|
||||||
|
# 从action_registry获取所有已注册动作
|
||||||
|
for action_name, action_class in action_registry.items():
|
||||||
# 获取动作相关信息
|
# 获取动作相关信息
|
||||||
|
|
||||||
# 不读取插件动作和基类
|
# 不读取插件动作和基类
|
||||||
@@ -186,11 +190,11 @@ class ActionManager:
|
|||||||
"""
|
"""
|
||||||
加载所有插件目录中的动作
|
加载所有插件目录中的动作
|
||||||
|
|
||||||
注意:插件动作的实际导入已经在main.py中完成,这里只需要从_ACTION_REGISTRY获取
|
注意:插件动作的实际导入已经在main.py中完成,这里只需要从action_registry获取
|
||||||
同时也从新插件系统的component_registry获取Action组件
|
同时也从新插件系统的component_registry获取Action组件
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
# 从旧的_ACTION_REGISTRY获取插件动作
|
# 从旧的action_registry获取插件动作
|
||||||
self._load_registered_actions()
|
self._load_registered_actions()
|
||||||
logger.debug("从旧注册表加载插件动作成功")
|
logger.debug("从旧注册表加载插件动作成功")
|
||||||
|
|
||||||
@@ -311,7 +315,9 @@ class ActionManager:
|
|||||||
)
|
)
|
||||||
|
|
||||||
# 旧系统的动作创建逻辑
|
# 旧系统的动作创建逻辑
|
||||||
handler_class = _ACTION_REGISTRY.get(action_name)
|
from src.plugin_system.core.component_registry import component_registry
|
||||||
|
action_registry = component_registry.get_action_registry()
|
||||||
|
handler_class = action_registry.get(action_name)
|
||||||
if not handler_class:
|
if not handler_class:
|
||||||
logger.warning(f"未注册的动作类型: {action_name}")
|
logger.warning(f"未注册的动作类型: {action_name}")
|
||||||
return None
|
return None
|
||||||
@@ -575,4 +581,6 @@ class ActionManager:
|
|||||||
Returns:
|
Returns:
|
||||||
Optional[Type[BaseAction]]: 动作处理器类,如果不存在则返回None
|
Optional[Type[BaseAction]]: 动作处理器类,如果不存在则返回None
|
||||||
"""
|
"""
|
||||||
return _ACTION_REGISTRY.get(action_name)
|
from src.plugin_system.core.component_registry import component_registry
|
||||||
|
action_registry = component_registry.get_action_registry()
|
||||||
|
return action_registry.get(action_name)
|
||||||
|
|||||||
@@ -1,18 +1,31 @@
|
|||||||
from typing import Optional, List, Any
|
from typing import Optional, List, Any, Tuple
|
||||||
from src.common.logger import get_logger
|
from src.common.logger import get_logger
|
||||||
from src.chat.heart_flow.heartflow import heartflow
|
|
||||||
from src.chat.heart_flow.sub_heartflow import SubHeartflow, ChatState
|
|
||||||
|
|
||||||
logger = get_logger("hearflow_api")
|
logger = get_logger("hearflow_api")
|
||||||
|
|
||||||
|
|
||||||
|
def _get_heartflow():
|
||||||
|
"""获取heartflow实例的延迟导入函数"""
|
||||||
|
from src.chat.heart_flow.heartflow import heartflow
|
||||||
|
return heartflow
|
||||||
|
|
||||||
|
|
||||||
|
def _get_subheartflow_types():
|
||||||
|
"""获取SubHeartflow和ChatState类型的延迟导入函数"""
|
||||||
|
from src.chat.heart_flow.sub_heartflow import SubHeartflow, ChatState
|
||||||
|
return SubHeartflow, ChatState
|
||||||
|
|
||||||
|
|
||||||
class HearflowAPI:
|
class HearflowAPI:
|
||||||
"""心流API模块
|
"""心流API模块
|
||||||
|
|
||||||
提供与心流和子心流相关的操作接口
|
提供与心流和子心流相关的操作接口
|
||||||
"""
|
"""
|
||||||
|
|
||||||
async def get_sub_hearflow_by_chat_id(self, chat_id: str) -> Optional[SubHeartflow]:
|
def __init__(self):
|
||||||
|
self.log_prefix = "[HearflowAPI]"
|
||||||
|
|
||||||
|
async def get_sub_hearflow_by_chat_id(self, chat_id: str) -> Optional[Any]:
|
||||||
"""根据chat_id获取指定的sub_hearflow实例
|
"""根据chat_id获取指定的sub_hearflow实例
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -21,20 +34,31 @@ class HearflowAPI:
|
|||||||
Returns:
|
Returns:
|
||||||
Optional[SubHeartflow]: sub_hearflow实例,如果不存在则返回None
|
Optional[SubHeartflow]: sub_hearflow实例,如果不存在则返回None
|
||||||
"""
|
"""
|
||||||
try:
|
# 使用延迟导入
|
||||||
# 直接从subheartflow_manager获取已存在的子心流
|
heartflow = _get_heartflow()
|
||||||
# 使用锁来确保线程安全
|
|
||||||
async with heartflow.subheartflow_manager._lock:
|
# 直接从subheartflow_manager获取已存在的子心流
|
||||||
subflow = heartflow.subheartflow_manager.subheartflows.get(chat_id)
|
# 使用锁来确保线程安全
|
||||||
if subflow and not subflow.should_stop:
|
async with heartflow.subheartflow_manager._lock:
|
||||||
logger.debug(f"{self.log_prefix} 成功获取子心流实例: {chat_id}")
|
subflow = heartflow.subheartflow_manager.subheartflows.get(chat_id)
|
||||||
return subflow
|
if subflow and not subflow.should_stop:
|
||||||
else:
|
logger.debug(f"{self.log_prefix} 成功获取子心流实例: {chat_id}")
|
||||||
logger.debug(f"{self.log_prefix} 子心流不存在或已停止: {chat_id}")
|
return subflow
|
||||||
return None
|
else:
|
||||||
except Exception as e:
|
logger.debug(f"{self.log_prefix} 子心流不存在或已停止: {chat_id}")
|
||||||
logger.error(f"{self.log_prefix} 获取子心流实例时出错: {e}")
|
return None
|
||||||
return None
|
|
||||||
|
async def get_or_create_sub_hearflow_by_chat_id(self, chat_id: str) -> Optional[Any]:
|
||||||
|
"""根据chat_id获取或创建sub_hearflow实例
|
||||||
|
|
||||||
|
Args:
|
||||||
|
chat_id: 聊天ID
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Optional[SubHeartflow]: sub_hearflow实例,创建失败时返回None
|
||||||
|
"""
|
||||||
|
heartflow = _get_heartflow()
|
||||||
|
return await heartflow.get_or_create_subheartflow(chat_id)
|
||||||
|
|
||||||
def get_all_sub_hearflow_ids(self) -> List[str]:
|
def get_all_sub_hearflow_ids(self) -> List[str]:
|
||||||
"""获取所有子心流的ID列表
|
"""获取所有子心流的ID列表
|
||||||
@@ -42,31 +66,25 @@ class HearflowAPI:
|
|||||||
Returns:
|
Returns:
|
||||||
List[str]: 所有子心流的ID列表
|
List[str]: 所有子心流的ID列表
|
||||||
"""
|
"""
|
||||||
try:
|
heartflow = _get_heartflow()
|
||||||
all_subflows = heartflow.subheartflow_manager.get_all_subheartflows()
|
all_subflows = heartflow.subheartflow_manager.get_all_subheartflows()
|
||||||
chat_ids = [subflow.chat_id for subflow in all_subflows if not subflow.should_stop]
|
chat_ids = [subflow.chat_id for subflow in all_subflows if not subflow.should_stop]
|
||||||
logger.debug(f"{self.log_prefix} 获取到 {len(chat_ids)} 个活跃的子心流ID")
|
logger.debug(f"{self.log_prefix} 获取到 {len(chat_ids)} 个活跃的子心流ID")
|
||||||
return chat_ids
|
return chat_ids
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"{self.log_prefix} 获取子心流ID列表时出错: {e}")
|
|
||||||
return []
|
|
||||||
|
|
||||||
def get_all_sub_hearflows(self) -> List[SubHeartflow]:
|
def get_all_sub_hearflows(self) -> List[Any]:
|
||||||
"""获取所有子心流实例
|
"""获取所有子心流实例
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
List[SubHeartflow]: 所有活跃的子心流实例列表
|
List[SubHeartflow]: 所有活跃的子心流实例列表
|
||||||
"""
|
"""
|
||||||
try:
|
heartflow = _get_heartflow()
|
||||||
all_subflows = heartflow.subheartflow_manager.get_all_subheartflows()
|
all_subflows = heartflow.subheartflow_manager.get_all_subheartflows()
|
||||||
active_subflows = [subflow for subflow in all_subflows if not subflow.should_stop]
|
active_subflows = [subflow for subflow in all_subflows if not subflow.should_stop]
|
||||||
logger.debug(f"{self.log_prefix} 获取到 {len(active_subflows)} 个活跃的子心流实例")
|
logger.debug(f"{self.log_prefix} 获取到 {len(active_subflows)} 个活跃的子心流实例")
|
||||||
return active_subflows
|
return active_subflows
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"{self.log_prefix} 获取子心流实例列表时出错: {e}")
|
|
||||||
return []
|
|
||||||
|
|
||||||
async def get_sub_hearflow_chat_state(self, chat_id: str) -> Optional[ChatState]:
|
async def get_sub_hearflow_chat_state(self, chat_id: str) -> Optional[Any]:
|
||||||
"""获取指定子心流的聊天状态
|
"""获取指定子心流的聊天状态
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -75,30 +93,60 @@ class HearflowAPI:
|
|||||||
Returns:
|
Returns:
|
||||||
Optional[ChatState]: 聊天状态,如果子心流不存在则返回None
|
Optional[ChatState]: 聊天状态,如果子心流不存在则返回None
|
||||||
"""
|
"""
|
||||||
try:
|
subflow = await self.get_sub_hearflow_by_chat_id(chat_id)
|
||||||
subflow = await self.get_sub_hearflow_by_chat_id(chat_id)
|
if subflow:
|
||||||
if subflow:
|
return subflow.chat_state.chat_status
|
||||||
return subflow.chat_state.chat_status
|
return None
|
||||||
return None
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"{self.log_prefix} 获取子心流聊天状态时出错: {e}")
|
|
||||||
return None
|
|
||||||
|
|
||||||
async def set_sub_hearflow_chat_state(self, chat_id: str, target_state: ChatState) -> bool:
|
async def set_sub_hearflow_chat_state(self, chat_id: str, target_state: Any) -> bool:
|
||||||
"""设置指定子心流的聊天状态
|
"""设置指定子心流的聊天状态
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
chat_id: 聊天ID
|
chat_id: 聊天ID
|
||||||
target_state: 目标状态
|
target_state: 目标状态(ChatState枚举值)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
bool: 是否设置成功
|
bool: 是否设置成功
|
||||||
"""
|
"""
|
||||||
try:
|
heartflow = _get_heartflow()
|
||||||
return await heartflow.subheartflow_manager.force_change_state(chat_id, target_state)
|
return await heartflow.subheartflow_manager.force_change_state(chat_id, target_state)
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"{self.log_prefix} 设置子心流聊天状态时出错: {e}")
|
async def get_sub_hearflow_replyer_and_expressor(self, chat_id: str) -> Tuple[Optional[Any], Optional[Any]]:
|
||||||
return False
|
"""根据chat_id获取指定子心流的replyer和expressor实例
|
||||||
|
|
||||||
|
Args:
|
||||||
|
chat_id: 聊天ID
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Tuple[Optional[Any], Optional[Any]]: (replyer实例, expressor实例),如果子心流不存在或未处于FOCUSED状态,返回(None, None)
|
||||||
|
"""
|
||||||
|
subflow = await self.get_sub_hearflow_by_chat_id(chat_id)
|
||||||
|
if not subflow:
|
||||||
|
logger.debug(f"{self.log_prefix} 子心流不存在: {chat_id}")
|
||||||
|
return None, None
|
||||||
|
|
||||||
|
# 使用延迟导入获取ChatState
|
||||||
|
_, ChatState = _get_subheartflow_types()
|
||||||
|
|
||||||
|
# 检查子心流是否处于FOCUSED状态且有HeartFC实例
|
||||||
|
if subflow.chat_state.chat_status != ChatState.FOCUSED:
|
||||||
|
logger.debug(f"{self.log_prefix} 子心流 {chat_id} 未处于FOCUSED状态,当前状态: {subflow.chat_state.chat_status.value}")
|
||||||
|
return None, None
|
||||||
|
|
||||||
|
if not subflow.heart_fc_instance:
|
||||||
|
logger.debug(f"{self.log_prefix} 子心流 {chat_id} 没有HeartFC实例")
|
||||||
|
return None, None
|
||||||
|
|
||||||
|
# 返回replyer和expressor实例
|
||||||
|
replyer = subflow.heart_fc_instance.replyer
|
||||||
|
expressor = subflow.heart_fc_instance.expressor
|
||||||
|
|
||||||
|
if replyer and expressor:
|
||||||
|
logger.debug(f"{self.log_prefix} 成功获取子心流 {chat_id} 的replyer和expressor")
|
||||||
|
else:
|
||||||
|
logger.warning(f"{self.log_prefix} 子心流 {chat_id} 的replyer或expressor为空")
|
||||||
|
|
||||||
|
return replyer, expressor
|
||||||
|
|
||||||
async def get_sub_hearflow_replyer(self, chat_id: str) -> Optional[Any]:
|
async def get_sub_hearflow_replyer(self, chat_id: str) -> Optional[Any]:
|
||||||
"""根据chat_id获取指定子心流的replyer实例
|
"""根据chat_id获取指定子心流的replyer实例
|
||||||
@@ -109,12 +157,8 @@ class HearflowAPI:
|
|||||||
Returns:
|
Returns:
|
||||||
Optional[Any]: replyer实例,如果不存在则返回None
|
Optional[Any]: replyer实例,如果不存在则返回None
|
||||||
"""
|
"""
|
||||||
try:
|
replyer, _ = await self.get_sub_hearflow_replyer_and_expressor(chat_id)
|
||||||
replyer, _ = await self.get_sub_hearflow_replyer_and_expressor(chat_id)
|
return replyer
|
||||||
return replyer
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"{self.log_prefix} 获取子心流replyer时出错: {e}")
|
|
||||||
return None
|
|
||||||
|
|
||||||
async def get_sub_hearflow_expressor(self, chat_id: str) -> Optional[Any]:
|
async def get_sub_hearflow_expressor(self, chat_id: str) -> Optional[Any]:
|
||||||
"""根据chat_id获取指定子心流的expressor实例
|
"""根据chat_id获取指定子心流的expressor实例
|
||||||
@@ -125,9 +169,5 @@ class HearflowAPI:
|
|||||||
Returns:
|
Returns:
|
||||||
Optional[Any]: expressor实例,如果不存在则返回None
|
Optional[Any]: expressor实例,如果不存在则返回None
|
||||||
"""
|
"""
|
||||||
try:
|
_, expressor = await self.get_sub_hearflow_replyer_and_expressor(chat_id)
|
||||||
_, expressor = await self.get_sub_hearflow_replyer_and_expressor(chat_id)
|
return expressor
|
||||||
return expressor
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"{self.log_prefix} 获取子心流expressor时出错: {e}")
|
|
||||||
return None
|
|
||||||
|
|||||||
Reference in New Issue
Block a user