ruff
This commit is contained in:
committed by
Windpicker-owo
parent
e65ab14f94
commit
950b086063
@@ -57,6 +57,9 @@ async def get_replyer(
|
||||
raise ValueError("chat_stream 和 chat_id 不可均为空")
|
||||
try:
|
||||
logger.debug(f"[GeneratorAPI] 正在获取回复器,chat_id: {chat_id}, chat_stream: {'有' if chat_stream else '无'}")
|
||||
# 动态导入避免循环依赖
|
||||
from src.chat.replyer.replyer_manager import replyer_manager
|
||||
|
||||
return await replyer_manager.get_replyer(
|
||||
chat_stream=chat_stream,
|
||||
chat_id=chat_id,
|
||||
|
||||
@@ -39,6 +39,7 @@ def get_llm_available_tool_definitions():
|
||||
# 添加MCP工具
|
||||
try:
|
||||
from src.plugin_system.utils.mcp_tool_provider import mcp_tool_provider
|
||||
|
||||
mcp_tools = mcp_tool_provider.get_mcp_tool_definitions()
|
||||
tool_definitions.extend(mcp_tools)
|
||||
if mcp_tools:
|
||||
|
||||
@@ -86,7 +86,9 @@ class HandlerResultsCollection:
|
||||
|
||||
|
||||
class BaseEvent:
|
||||
def __init__(self, name: str, allowed_subscribers: list[str] | None = None, allowed_triggers: list[str] | None = None):
|
||||
def __init__(
|
||||
self, name: str, allowed_subscribers: list[str] | None = None, allowed_triggers: list[str] | None = None
|
||||
):
|
||||
self.name = name
|
||||
self.enabled = True
|
||||
self.allowed_subscribers = allowed_subscribers # 记录事件处理器名
|
||||
|
||||
@@ -28,7 +28,7 @@ class InterestCalculationResult:
|
||||
should_reply: bool = False,
|
||||
should_act: bool = False,
|
||||
error_message: str | None = None,
|
||||
calculation_time: float = 0.0
|
||||
calculation_time: float = 0.0,
|
||||
):
|
||||
self.success = success
|
||||
self.message_id = message_id
|
||||
@@ -51,17 +51,19 @@ class InterestCalculationResult:
|
||||
"should_act": self.should_act,
|
||||
"error_message": self.error_message,
|
||||
"calculation_time": self.calculation_time,
|
||||
"timestamp": self.timestamp
|
||||
"timestamp": self.timestamp,
|
||||
}
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return (f"InterestCalculationResult("
|
||||
f"success={self.success}, "
|
||||
f"message_id={self.message_id}, "
|
||||
f"interest_value={self.interest_value:.3f}, "
|
||||
f"should_take_action={self.should_take_action}, "
|
||||
f"should_reply={self.should_reply}, "
|
||||
f"should_act={self.should_act})")
|
||||
return (
|
||||
f"InterestCalculationResult("
|
||||
f"success={self.success}, "
|
||||
f"message_id={self.message_id}, "
|
||||
f"interest_value={self.interest_value:.3f}, "
|
||||
f"should_take_action={self.should_take_action}, "
|
||||
f"should_reply={self.should_reply}, "
|
||||
f"should_act={self.should_act})"
|
||||
)
|
||||
|
||||
|
||||
class BaseInterestCalculator(ABC):
|
||||
@@ -144,7 +146,7 @@ class BaseInterestCalculator(ABC):
|
||||
"failed_calculations": self._failed_calculations,
|
||||
"success_rate": 1.0 - (self._failed_calculations / max(1, self._total_calculations)),
|
||||
"average_calculation_time": self._average_calculation_time,
|
||||
"last_calculation_time": self._last_calculation_time
|
||||
"last_calculation_time": self._last_calculation_time,
|
||||
}
|
||||
|
||||
def _update_statistics(self, result: InterestCalculationResult):
|
||||
@@ -159,8 +161,7 @@ class BaseInterestCalculator(ABC):
|
||||
else:
|
||||
alpha = 0.1 # 指数移动平均的平滑因子
|
||||
self._average_calculation_time = (
|
||||
alpha * result.calculation_time +
|
||||
(1 - alpha) * self._average_calculation_time
|
||||
alpha * result.calculation_time + (1 - alpha) * self._average_calculation_time
|
||||
)
|
||||
|
||||
self._last_calculation_time = result.timestamp
|
||||
@@ -172,7 +173,7 @@ class BaseInterestCalculator(ABC):
|
||||
success=False,
|
||||
message_id=getattr(message, "message_id", ""),
|
||||
interest_value=0.0,
|
||||
error_message="组件未启用"
|
||||
error_message="组件未启用",
|
||||
)
|
||||
|
||||
start_time = time.time()
|
||||
@@ -187,7 +188,7 @@ class BaseInterestCalculator(ABC):
|
||||
message_id=getattr(message, "message_id", ""),
|
||||
interest_value=0.0,
|
||||
error_message=f"计算执行失败: {e!s}",
|
||||
calculation_time=time.time() - start_time
|
||||
calculation_time=time.time() - start_time,
|
||||
)
|
||||
self._update_statistics(result)
|
||||
return result
|
||||
@@ -214,7 +215,9 @@ class BaseInterestCalculator(ABC):
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return (f"{self.__class__.__name__}("
|
||||
f"name={self.component_name}, "
|
||||
f"version={self.component_version}, "
|
||||
f"enabled={self._enabled})")
|
||||
return (
|
||||
f"{self.__class__.__name__}("
|
||||
f"name={self.component_name}, "
|
||||
f"version={self.component_version}, "
|
||||
f"enabled={self._enabled})"
|
||||
)
|
||||
|
||||
@@ -60,7 +60,9 @@ class BasePlugin(PluginBase):
|
||||
if hasattr(component_class, "get_interest_calculator_info"):
|
||||
return component_class.get_interest_calculator_info()
|
||||
else:
|
||||
logger.warning(f"InterestCalculator类 {component_class.__name__} 缺少 get_interest_calculator_info 方法")
|
||||
logger.warning(
|
||||
f"InterestCalculator类 {component_class.__name__} 缺少 get_interest_calculator_info 方法"
|
||||
)
|
||||
return None
|
||||
|
||||
elif component_type == ComponentType.PLUS_COMMAND:
|
||||
@@ -96,6 +98,7 @@ class BasePlugin(PluginBase):
|
||||
对应类型的ComponentInfo对象
|
||||
"""
|
||||
return cls._get_component_info_from_class(component_class, component_type)
|
||||
|
||||
@abstractmethod
|
||||
def get_plugin_components(
|
||||
self,
|
||||
|
||||
@@ -7,6 +7,7 @@ class PluginMetadata:
|
||||
"""
|
||||
插件元数据,用于存储插件的开发者信息和用户帮助信息。
|
||||
"""
|
||||
|
||||
name: str # 插件名称 (供用户查看)
|
||||
description: str # 插件功能描述
|
||||
usage: str # 插件使用方法
|
||||
|
||||
@@ -319,7 +319,9 @@ class ComponentRegistry:
|
||||
return True
|
||||
|
||||
def _register_interest_calculator_component(
|
||||
self, interest_calculator_info: "InterestCalculatorInfo", interest_calculator_class: type["BaseInterestCalculator"]
|
||||
self,
|
||||
interest_calculator_info: "InterestCalculatorInfo",
|
||||
interest_calculator_class: type["BaseInterestCalculator"],
|
||||
) -> bool:
|
||||
"""注册InterestCalculator组件到特定注册表"""
|
||||
calculator_name = interest_calculator_info.name
|
||||
@@ -327,7 +329,9 @@ class ComponentRegistry:
|
||||
if not calculator_name:
|
||||
logger.error(f"InterestCalculator组件 {interest_calculator_class.__name__} 必须指定名称")
|
||||
return False
|
||||
if not isinstance(interest_calculator_info, InterestCalculatorInfo) or not issubclass(interest_calculator_class, BaseInterestCalculator):
|
||||
if not isinstance(interest_calculator_info, InterestCalculatorInfo) or not issubclass(
|
||||
interest_calculator_class, BaseInterestCalculator
|
||||
):
|
||||
logger.error(f"注册失败: {calculator_name} 不是有效的InterestCalculator")
|
||||
return False
|
||||
|
||||
|
||||
@@ -67,6 +67,7 @@ class ToolExecutor:
|
||||
"""异步初始化log_prefix和chat_stream"""
|
||||
if not self._log_prefix_initialized:
|
||||
from src.chat.message_receive.chat_stream import get_chat_manager
|
||||
|
||||
self.chat_stream = await get_chat_manager().get_stream(self.chat_id)
|
||||
stream_name = await get_chat_manager().get_stream_name(self.chat_id)
|
||||
self.log_prefix = f"[{stream_name or self.chat_id}]"
|
||||
@@ -283,6 +284,7 @@ class ToolExecutor:
|
||||
# 检查是否是MCP工具
|
||||
try:
|
||||
from src.plugin_system.utils.mcp_tool_provider import mcp_tool_provider
|
||||
|
||||
if function_name in mcp_tool_provider.mcp_tools:
|
||||
logger.info(f"{self.log_prefix}执行MCP工具: {function_name}")
|
||||
result = await mcp_tool_provider.call_mcp_tool(function_name, function_args)
|
||||
|
||||
Reference in New Issue
Block a user