style: 格式化代码

This commit is contained in:
John Richard
2025-10-02 19:38:39 +08:00
parent d5627b0661
commit ecb02cae31
111 changed files with 2344 additions and 2316 deletions

View File

@@ -2,7 +2,7 @@ import time
import asyncio
from abc import ABC, abstractmethod
from typing import Tuple, Optional, List, Dict, Any
from typing import Tuple, Optional, List, Dict
from src.common.logger import get_logger
from src.chat.message_receive.chat_stream import ChatStream
@@ -27,7 +27,7 @@ class BaseAction(ABC):
- parallel_action: 是否允许并行执行
- random_activation_probability: 随机激活概率
- llm_judge_prompt: LLM判断提示词
二步Action相关属性
- is_two_step_action: 是否为二步Action
- step_one_description: 第一步的描述
@@ -434,7 +434,9 @@ class BaseAction(ABC):
# 确保获取的是Action组件
if component_info.component_type != ComponentType.ACTION:
logger.error(f"{log_prefix} 尝试调用的组件 '{action_name}' 不是一个Action而是一个 '{component_info.component_type.value}'")
logger.error(
f"{log_prefix} 尝试调用的组件 '{action_name}' 不是一个Action而是一个 '{component_info.component_type.value}'"
)
return False, f"组件 '{action_name}' 不是一个有效的Action"
plugin_config = component_registry.get_plugin_config(component_info.plugin_name)
@@ -527,20 +529,20 @@ class BaseAction(ABC):
# 第一步展示可用的子Action
available_actions = [sub_action[0] for sub_action in self.sub_actions]
description = self.step_one_description or f"{self.action_name}支持以下操作"
actions_list = "\n".join([f"- {action}: {desc}" for action, desc, _ in self.sub_actions])
response = f"{description}\n\n可用操作:\n{actions_list}\n\n请选择要执行的操作。"
return True, response
else:
# 验证选择的子Action是否有效
valid_actions = [sub_action[0] for sub_action in self.sub_actions]
if selected_action not in valid_actions:
return False, f"无效的操作选择: {selected_action}。可用操作: {valid_actions}"
# 保存选择的子Action
self._selected_sub_action = selected_action
# 调用第二步执行
return await self.execute_step_two(selected_action)
@@ -571,7 +573,7 @@ class BaseAction(ABC):
# 如果是二步Action自动处理第一步
if self.is_two_step_action:
return await self.handle_step_one()
# 普通Action由子类实现
pass

View File

@@ -1,12 +1,12 @@
from abc import ABC, abstractmethod
from typing import List, Optional, TYPE_CHECKING
from typing import List, TYPE_CHECKING
from src.common.data_models.message_manager_data_model import StreamContext
from .component_types import ChatType
from src.plugin_system.base.component_types import ChatterInfo, ComponentType
if TYPE_CHECKING:
from src.chat.planner_actions.action_manager import ChatterActionManager
from src.plugins.built_in.affinity_flow_chatter.planner import ChatterActionPlanner as ActionPlanner
class BaseChatter(ABC):
chatter_name: str = ""
@@ -15,7 +15,7 @@ class BaseChatter(ABC):
"""Chatter组件的描述"""
chat_types: List[ChatType] = [ChatType.PRIVATE, ChatType.GROUP]
def __init__(self, stream_id: str, action_manager: 'ChatterActionManager'):
def __init__(self, stream_id: str, action_manager: "ChatterActionManager"):
"""
初始化聊天处理器
@@ -45,11 +45,10 @@ class BaseChatter(ABC):
Returns:
ChatterInfo对象
"""
return ChatterInfo(
name=cls.chatter_name,
description=cls.chatter_description or "No description provided.",
chat_type_allow=cls.chat_types[0],
component_type=ComponentType.CHATTER,
)

View File

@@ -64,7 +64,15 @@ class BaseTool(ABC):
return {
"name": cls.name,
"description": cls.step_one_description or cls.description,
"parameters": [("action", ToolParamType.STRING, "选择要执行的操作", True, [sub_tool[0] for sub_tool in cls.sub_tools])]
"parameters": [
(
"action",
ToolParamType.STRING,
"选择要执行的操作",
True,
[sub_tool[0] for sub_tool in cls.sub_tools],
)
],
}
else:
# 普通工具需要parameters
@@ -88,12 +96,8 @@ class BaseTool(ABC):
# 查找对应的子工具
for sub_name, sub_desc, sub_params in cls.sub_tools:
if sub_name == sub_tool_name:
return {
"name": f"{cls.name}_{sub_tool_name}",
"description": sub_desc,
"parameters": sub_params
}
return {"name": f"{cls.name}_{sub_tool_name}", "description": sub_desc, "parameters": sub_params}
raise ValueError(f"未找到子工具: {sub_tool_name}")
@classmethod
@@ -105,14 +109,10 @@ class BaseTool(ABC):
"""
if not cls.is_two_step_tool:
return []
definitions = []
for sub_name, sub_desc, sub_params in cls.sub_tools:
definitions.append({
"name": f"{cls.name}_{sub_name}",
"description": sub_desc,
"parameters": sub_params
})
definitions.append({"name": f"{cls.name}_{sub_name}", "description": sub_desc, "parameters": sub_params})
return definitions
@classmethod
@@ -144,7 +144,7 @@ class BaseTool(ABC):
# 如果是二步工具,处理第一步调用
if self.is_two_step_tool and "action" in function_args:
return await self._handle_step_one(function_args)
raise NotImplementedError("子类必须实现execute方法")
async def _handle_step_one(self, function_args: dict[str, Any]) -> dict[str, Any]:
@@ -174,17 +174,13 @@ class BaseTool(ABC):
sub_name, sub_desc, sub_params = sub_tool_found
# 返回第二步工具定义
step_two_definition = {
"name": f"{self.name}_{sub_name}",
"description": sub_desc,
"parameters": sub_params
}
step_two_definition = {"name": f"{self.name}_{sub_name}", "description": sub_desc, "parameters": sub_params}
return {
"type": "two_step_tool_step_one",
"content": f"已选择操作: {action}。请使用以下工具进行具体调用:",
"next_tool_definition": step_two_definition,
"selected_action": action
"selected_action": action,
}
async def execute_step_two(self, sub_tool_name: str, function_args: dict[str, Any]) -> dict[str, Any]:

View File

@@ -40,7 +40,7 @@ class ActionActivationType(Enum):
# 聊天模式枚举
class ChatMode(Enum):
"""聊天模式枚举"""
FOCUS = "focus" # 专注模式
NORMAL = "normal" # Normal聊天模式
PROACTIVE = "proactive" # 主动思考模式

View File

@@ -294,9 +294,7 @@ class PluginBase(ABC):
changed = False
# 内部递归函数
def _sync_dicts(
schema_dict: Dict[str, Any], user_dict: Dict[str, Any], parent_key: str = ""
) -> Dict[str, Any]:
def _sync_dicts(schema_dict: Dict[str, Any], user_dict: Dict[str, Any], parent_key: str = "") -> Dict[str, Any]:
nonlocal changed
synced_dict = schema_dict.copy()