diff --git a/README.md b/README.md index 3381a3cfe..a4f532895 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@
QQ Group +

--- diff --git a/docker-compose.yml b/docker-compose.yml index 63ba59661..45ad1d635 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,9 +2,9 @@ services: core: container_name: MoFox-Bot #### prod #### - image: hunuon/mofox:latest + image: ericterminal/mofox:latest #### dev #### - # image: hunuon/mofox:dev + # image: ericterminal/mofox:dev environment: - TZ=Asia/Shanghai volumes: diff --git a/pyproject.toml b/pyproject.toml index 051f74109..d5d481934 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -79,6 +79,7 @@ dependencies = [ "rjieba>=0.1.13", "fastmcp>=2.13.0", "mofox-wire", + "jinja2>=3.1.0" ] [[tool.uv.index]] diff --git a/src/chat/planner_actions/action_manager.py b/src/chat/planner_actions/action_manager.py index 3346a1075..ef8b24657 100644 --- a/src/chat/planner_actions/action_manager.py +++ b/src/chat/planner_actions/action_manager.py @@ -30,11 +30,9 @@ class ChatterActionManager: def __init__(self): """初始化动作管理器""" - # 当前正在使用的动作集合,默认加载默认动作 + # 当前正在使用的动作集合,在规划开始时加载 self._using_actions: dict[str, ActionInfo] = {} - - # 初始化时将默认动作加载到使用中的动作 - self._using_actions = component_registry.get_default_actions() + self.chat_id: str | None = None self.log_prefix: str = "ChatterActionManager" # 批量存储支持 @@ -42,6 +40,12 @@ class ChatterActionManager: self._pending_actions = [] self._current_chat_id = None + async def load_actions(self, stream_id: str | None): + """根据 stream_id 加载当前可用的动作""" + self.chat_id = stream_id + self._using_actions = component_registry.get_default_actions(stream_id) + logger.debug(f"已为 stream '{stream_id}' 加载 {len(self._using_actions)} 个可用动作: {list(self._using_actions.keys())}") + # === 执行Action方法 === @staticmethod @@ -136,11 +140,12 @@ class ChatterActionManager: logger.debug(f"已从使用集中移除动作 {action_name}") return True - def restore_actions(self) -> None: - """恢复到默认动作集""" + async def restore_actions(self) -> None: + """恢复到当前 stream_id 的默认动作集""" 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())}") + # 使用 self.chat_id 来恢复当前上下文的动作 + await self.load_actions(self.chat_id) + logger.debug(f"恢复动作集: 从 {actions_to_restore} 恢复到 stream '{self.chat_id}' 的默认动作集 {list(self._using_actions.keys())}") async def execute_action( self, diff --git a/src/chat/planner_actions/action_modifier.py b/src/chat/planner_actions/action_modifier.py index db6c84804..ca057d8e7 100644 --- a/src/chat/planner_actions/action_modifier.py +++ b/src/chat/planner_actions/action_modifier.py @@ -11,7 +11,7 @@ from src.common.logger import get_logger from src.config.config import global_config, model_config from src.llm_models.utils_model import LLMRequest from src.plugin_system.base.component_types import ActionInfo -from src.plugin_system.core.global_announcement_manager import global_announcement_manager + if TYPE_CHECKING: from src.common.data_models.message_manager_data_model import StreamContext @@ -69,6 +69,16 @@ class ActionModifier: """ # 初始化log_prefix await self._initialize_log_prefix() + # 根据 stream_id 加载当前可用的动作 + await self.action_manager.load_actions(self.chat_id) + from src.plugin_system.base.component_types import ComponentType + from src.plugin_system.core.component_registry import component_registry + # 计算并记录禁用的动作数量 + all_registered_actions = component_registry.get_components_by_type(ComponentType.ACTION) + loaded_actions_count = len(self.action_manager.get_using_actions()) + disabled_actions_count = len(all_registered_actions) - loaded_actions_count + if disabled_actions_count > 0: + logger.info(f"{self.log_prefix} 用户禁用了 {disabled_actions_count} 个动作。") logger.debug(f"{self.log_prefix}开始完整动作修改流程") @@ -76,7 +86,6 @@ class ActionModifier: 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() # === 第0阶段:根据聊天类型过滤动作 === @@ -127,15 +136,6 @@ class ActionModifier: if message_content: chat_content = chat_content + "\n" + f"现在,最新的消息是:{message_content}" - # === 第一阶段:去除用户自行禁用的 === - 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},原因: 用户自行禁用") - # === 第二阶段:检查动作的关联类型 === if not self.chat_stream: logger.error(f"{self.log_prefix} chat_stream 未初始化,无法执行第二阶段") diff --git a/src/chat/utils/prompt_component_manager.py b/src/chat/utils/prompt_component_manager.py index d06e8a285..a0c77436a 100644 --- a/src/chat/utils/prompt_component_manager.py +++ b/src/chat/utils/prompt_component_manager.py @@ -29,14 +29,13 @@ class PromptComponentManager: def __init__(self): """初始化管理器实例。""" - # _dynamic_rules 是管理器的核心状态,存储所有注入规则。 + # _dynamic_rules 存储通过 API 在运行时动态添加/修改的规则。 + # 这是实现提示词动态性的核心数据结构。 # 结构: { - # "target_prompt_name": { - # "prompt_component_name": (InjectionRule, content_provider, source) + # "target_prompt_name": { // 目标 Prompt 的名称 + # "prompt_component_name": (InjectionRule, content_provider, source) // 注入组件的规则详情 # } # } - # content_provider 是一个异步函数,用于在应用规则时动态生成注入内容。 - # source 记录了规则的来源(例如 "static_default" 或 "runtime")。 self._dynamic_rules: dict[str, dict[str, tuple[InjectionRule, Callable[..., Awaitable[str]], str]]] = {} self._lock = asyncio.Lock() # 使用异步锁确保对 _dynamic_rules 的并发访问安全。 self._initialized = False # 标记静态规则是否已加载,防止重复加载。 @@ -139,9 +138,13 @@ class PromptComponentManager: Returns: bool: 如果成功添加或更新,则返回 True。 """ + # 加锁以保证多协程环境下的数据一致性 async with self._lock: + # 遍历所有待添加的规则 for rule in rules: + # 使用 setdefault 确保目标 prompt 的规则字典存在 target_rules = self._dynamic_rules.setdefault(rule.target_prompt, {}) + # 添加或覆盖指定组件的规则、内容提供者和来源 target_rules[prompt_name] = (rule, content_provider, source) logger.info(f"成功添加/更新注入规则: '{prompt_name}' -> '{rule.target_prompt}' (来源: {source})") return True @@ -163,15 +166,16 @@ class PromptComponentManager: 如果未找到该组件的任何现有规则(无法复用),则返回 False。 """ async with self._lock: - # 步骤 1: 查找现有的 content_provider 和 source + # 步骤 1: 遍历所有动态规则,查找指定组件已存在的 provider 和 source found_provider: Callable[..., Awaitable[str]] | None = None found_source: str | None = None for target_rules in self._dynamic_rules.values(): if prompt_name in target_rules: + # 如果找到,记录其 provider 和 source 并跳出循环 _, found_provider, found_source = target_rules[prompt_name] break - # 步骤 2: 如果找不到 provider,则操作失败 + # 步骤 2: 如果遍历完仍未找到 provider,说明该组件无任何规则,无法复用 if not found_provider: logger.warning( f"尝试为组件 '{prompt_name}' 添加规则失败: " @@ -180,7 +184,7 @@ class PromptComponentManager: return False # 步骤 3: 使用找到的 provider 和 source 添加新规则 - source_to_use = found_source or "runtime" # 提供一个默认值以防万一 + source_to_use = found_source or "runtime" # 如果 source 为 None,提供默认值 target_rules = self._dynamic_rules.setdefault(rule.target_prompt, {}) target_rules[prompt_name] = (rule, found_provider, source_to_use) logger.info( @@ -201,13 +205,16 @@ class PromptComponentManager: bool: 如果成功移除,则返回 True;如果规则不存在,则返回 False。 """ async with self._lock: + # 检查目标和组件规则是否存在 if target_prompt in self._dynamic_rules and prompt_name in self._dynamic_rules[target_prompt]: + # 存在则删除 del self._dynamic_rules[target_prompt][prompt_name] - # 如果目标下已无任何规则,则清理掉这个键 + # 如果删除后,该目标下已无任何规则,则清理掉这个目标键,保持数据结构整洁 if not self._dynamic_rules[target_prompt]: del self._dynamic_rules[target_prompt] logger.info(f"成功移除注入规则: '{prompt_name}' from '{target_prompt}'") return True + # 如果规则不存在,记录警告并返回 False logger.warning(f"尝试移除注入规则失败: 未找到 '{prompt_name}' on '{target_prompt}'") return False @@ -228,7 +235,9 @@ class PromptComponentManager: async with self._lock: # 创建一个目标列表的副本进行迭代,因为我们可能会在循环中修改字典 for target_prompt in list(self._dynamic_rules.keys()): + # 检查当前目标下是否存在该组件的规则 if prompt_name in self._dynamic_rules[target_prompt]: + # 存在则删除 del self._dynamic_rules[target_prompt][prompt_name] removed = True logger.info(f"成功移除注入规则: '{prompt_name}' from '{target_prompt}'") @@ -243,6 +252,77 @@ class PromptComponentManager: return removed # --- 核心注入逻辑 --- + def _create_content_provider( + self, component_name: str, component_class: type[BasePrompt] + ) -> Callable[[PromptParameters, str], Awaitable[str]]: + """为指定的组件类创建一个标准化的内容提供者闭包。""" + + async def content_provider(params: PromptParameters, target_prompt_name: str) -> str: + """实际执行内容生成的异步函数。""" + try: + # 从注册表获取组件信息,用于后续获取插件配置 + p_info = component_registry.get_component_info(component_name, ComponentType.PROMPT) + plugin_config = {} + if isinstance(p_info, PromptInfo): + # 获取该组件所属插件的配置 + plugin_config = component_registry.get_plugin_config(p_info.plugin_name) + + # 实例化组件,并传入所需参数 + instance = component_class( + params=params, plugin_config=plugin_config, target_prompt_name=target_prompt_name + ) + # 执行组件的 execute 方法以生成内容 + result = await instance.execute() + # 确保返回的是字符串 + return str(result) if result is not None else "" + except Exception as e: + # 捕获并记录执行过程中的任何异常,返回空字符串以避免注入失败 + logger.error(f"执行规则提供者 '{component_name}' 时出错: {e}", exc_info=True) + return "" + + return content_provider + + async def _build_rules_for_target(self, target_prompt_name: str) -> list: + """在注入时动态构建目标的所有有效规则列表。""" + all_rules = [] + + # 1. 从 component_registry 获取所有静态组件的规则 + static_components = component_registry.get_components_by_type(ComponentType.PROMPT) + for name, info in static_components.items(): + if not isinstance(info, PromptInfo): + continue + + # 实时检查组件是否已启用,跳过禁用的组件 + if not component_registry.is_component_available(name, ComponentType.PROMPT): + continue + + # 获取组件的类定义 + component_class = component_registry.get_component_class(name, ComponentType.PROMPT) + if not (component_class and issubclass(component_class, BasePrompt)): + continue + + # 为该组件创建一个内容提供者 + provider = self._create_content_provider(name, component_class) + # 遍历组件定义的所有注入规则 + for rule in info.injection_rules: + # 如果规则的目标与当前目标匹配,则添加到列表中 + if rule.target_prompt == target_prompt_name: + all_rules.append((rule, provider, "static")) + + # 2. 从 _dynamic_rules 获取所有纯运行时规则 + async with self._lock: + runtime_rules = self._dynamic_rules.get(target_prompt_name, {}) + for name, (rule, provider, source) in runtime_rules.items(): + # 检查该运行时规则是否关联到一个已注册的静态组件 + static_info = component_registry.get_component_info(name, ComponentType.PROMPT) + # 如果关联的静态组件存在且被禁用,则跳过此运行时规则 + if static_info and not component_registry.is_component_available(name, ComponentType.PROMPT): + logger.debug(f"跳过运行时规则 '{name}',因为它关联的静态组件当前已禁用。") + continue + # 将有效的运行时规则添加到列表 + all_rules.append((rule, provider, source)) + + return all_rules async def apply_injections( self, target_prompt_name: str, original_template: str, params: PromptParameters @@ -268,20 +348,19 @@ class PromptComponentManager: Returns: str: 应用了所有注入规则后,最终生成的提示词模板字符串。 """ - if not self._initialized: - self.load_static_rules() - - rules_for_target = list(self._dynamic_rules.get(target_prompt_name, {}).values()) + # 构建适用于当前目标的所有规则 + rules_for_target = await self._build_rules_for_target(target_prompt_name) if not rules_for_target: + # 如果没有规则,直接返回原始模板 return original_template # --- 占位符保护机制 --- + # 1. 保护: 找到所有 {placeholder} 并用临时标记替换 placeholders = re.findall(r"({[^{}]+})", original_template) placeholder_map: dict[str, str] = { f"__PROMPT_PLACEHOLDER_{i}__": p for i, p in enumerate(placeholders) - } + } - # 1. 保护: 将占位符替换为临时标记 protected_template = original_template for marker, placeholder in placeholder_map.items(): protected_template = protected_template.replace(placeholder, marker) @@ -290,6 +369,7 @@ class PromptComponentManager: for rule, _, source in rules_for_target: if rule.injection_type in (InjectionType.REMOVE, InjectionType.REPLACE) and rule.target_content: try: + # 检查规则的 target_content (正则) 是否可能匹配到任何一个占位符 for p in placeholders: if re.search(rule.target_content, p): logger.warning( @@ -297,10 +377,10 @@ class PromptComponentManager: f"规则 `target_content` ('{rule.target_content}') " f"可能会影响核心占位符 '{p}'。为保证系统稳定,该占位符已被保护,不会被此规则修改。" ) - # 只对每个规则警告一次 + # 每个规则只警告一次 break except re.error: - # 正则表达式本身有误,后面执行时会再次捕获,这里可忽略 + # 如果正则表达式本身有误,后续执行时会捕获,此处可忽略 pass # 3. 安全执行: 按优先级排序并应用规则 @@ -309,13 +389,16 @@ class PromptComponentManager: modified_template = protected_template for rule, provider, source in rules_for_target: content = "" + # REMOVE 类型不需要生成内容 if rule.injection_type != InjectionType.REMOVE: try: + # 调用内容提供者生成要注入的文本 content = await provider(params, target_prompt_name) except Exception as e: logger.error(f"执行规则 '{rule}' (来源: {source}) 的内容提供者时失败: {e}") continue + # 应用注入规则 try: if rule.injection_type == InjectionType.PREPEND: if content: @@ -328,6 +411,7 @@ class PromptComponentManager: modified_template = re.sub(rule.target_content, str(content), modified_template) elif rule.injection_type == InjectionType.INSERT_AFTER: if content and rule.target_content: + # 使用 \\g<0> 在匹配项后插入内容 replacement = f"\\g<0>\n{content}" modified_template = re.sub(rule.target_content, replacement, modified_template) elif rule.injection_type == InjectionType.REMOVE: @@ -338,7 +422,7 @@ class PromptComponentManager: except Exception as e: logger.error(f"应用注入规则 '{rule}' (来源: {source}) 失败: {e}") - # 4. 占位符恢复 + # 4. 占位符恢复: 将临时标记替换回原始的占位符 final_template = modified_template for marker, placeholder in placeholder_map.items(): final_template = final_template.replace(marker, placeholder) @@ -362,8 +446,9 @@ class PromptComponentManager: str: 模拟生成的最终提示词模板字符串。如果找不到模板,则返回错误信息。 """ try: - # 从全局提示词管理器获取最原始的模板内容 + # 动态导入以避免循环依赖 from src.chat.utils.prompt import global_prompt_manager + # 从全局管理器获取原始的、未经修改的提示词对象 original_prompt = global_prompt_manager._prompts.get(target_prompt_name) if not original_prompt: logger.warning(f"无法预览 '{target_prompt_name}',因为找不到这个核心 Prompt。") @@ -373,14 +458,16 @@ class PromptComponentManager: logger.warning(f"无法预览 '{target_prompt_name}',因为找不到这个核心 Prompt。") return f"Error: Prompt '{target_prompt_name}' not found." - # 直接调用核心注入逻辑来模拟结果 + # 直接调用核心注入逻辑来模拟并返回结果 return await self.apply_injections(target_prompt_name, original_template, params) # --- 状态观测与查询 API --- def get_core_prompts(self) -> list[str]: """获取所有已注册的核心提示词模板名称列表(即所有可注入的目标)。""" + # 动态导入以避免循环依赖 from src.chat.utils.prompt import global_prompt_manager + # 返回所有核心 prompt 的名称列表 return list(global_prompt_manager._prompts.keys()) def get_core_prompt_contents(self, prompt_name: str | None = None) -> list[list[str]]: @@ -400,60 +487,50 @@ class PromptComponentManager: from src.chat.utils.prompt import global_prompt_manager if prompt_name: + # 如果指定了名称,则查找并返回单个模板 prompt = global_prompt_manager._prompts.get(prompt_name) return [[prompt_name, prompt.template]] if prompt else [] + # 如果未指定名称,则返回所有模板的列表 return [[name, prompt.template] for name, prompt in global_prompt_manager._prompts.items()] - def get_registered_prompt_component_info(self) -> list[PromptInfo]: + async def get_registered_prompt_component_info(self) -> list[PromptInfo]: """ 获取所有已注册和动态添加的Prompt组件信息,并反映当前的注入规则状态。 - - 该方法会合并静态注册的组件信息和运行时的动态注入规则, - 确保返回的 `PromptInfo` 列表能够准确地反映系统当前的完整状态。 - - Returns: - list[PromptInfo]: 一个包含所有静态和动态Prompt组件信息的列表。 - 每个组件的 `injection_rules` 都会被更新为当前实际生效的规则。 + 此方法现在直接从 component_registry 获取静态组件信息,并合并纯运行时的组件信息。 """ - # 步骤 1: 获取所有静态注册的组件信息,并使用深拷贝以避免修改原始数据 - static_components = component_registry.get_components_by_type(ComponentType.PROMPT) - # 使用深拷贝以避免修改原始注册表数据 - info_dict: dict[str, PromptInfo] = { - name: copy.deepcopy(info) for name, info in static_components.items() if isinstance(info, PromptInfo) - } + # 从注册表获取所有已注册的静态 Prompt 组件信息 + all_components = component_registry.get_components_by_type(ComponentType.PROMPT) + info_list = [info for info in all_components.values() if isinstance(info, PromptInfo)] - # 步骤 2: 遍历动态规则,识别并创建纯动态组件的 PromptInfo - all_dynamic_component_names = set() - for target, rules in self._dynamic_rules.items(): - for prompt_name, (rule, _, source) in rules.items(): - all_dynamic_component_names.add(prompt_name) + # 检查并合并仅在运行时通过 API 添加的“纯动态”组件 + async with self._lock: + runtime_component_names = set() + # 收集所有动态规则中涉及的组件名称 + for rules in self._dynamic_rules.values(): + runtime_component_names.update(rules.keys()) - for name in all_dynamic_component_names: - if name not in info_dict: - # 这是一个纯动态组件,为其创建一个新的 PromptInfo - info_dict[name] = PromptInfo( + static_component_names = {info.name for info in info_list} + # 找出那些只存在于动态规则中,但未在静态组件中注册的名称 + pure_dynamic_names = runtime_component_names - static_component_names + + for name in pure_dynamic_names: + # 为这些“纯动态”组件创建一个临时的信息对象 + dynamic_info = PromptInfo( name=name, component_type=ComponentType.PROMPT, - description="Dynamically added component", - plugin_name="runtime", # 动态组件通常没有插件归属 + description="Dynamically added runtime component", + plugin_name="runtime", is_built_in=False, ) + # 从动态规则中收集并关联其所有注入规则 + for target, rules_in_target in self._dynamic_rules.items(): + if name in rules_in_target: + rule, _, _ = rules_in_target[name] + dynamic_info.injection_rules.append(rule) + info_list.append(dynamic_info) - # 步骤 3: 清空所有组件的注入规则,准备用当前状态重新填充 - for info in info_dict.values(): - info.injection_rules = [] - - # 步骤 4: 再次遍历动态规则,为每个组件重建其 injection_rules 列表 - for target, rules in self._dynamic_rules.items(): - for prompt_name, (rule, _, _) in rules.items(): - if prompt_name in info_dict: - # 确保规则是 InjectionRule 的实例 - if isinstance(rule, InjectionRule): - info_dict[prompt_name].injection_rules.append(rule) - - # 步骤 5: 返回最终的 PromptInfo 对象列表 - return list(info_dict.values()) + return info_list async def get_injection_info( self, @@ -462,60 +539,51 @@ class PromptComponentManager: ) -> dict[str, list[dict]]: """ 获取注入信息的映射图,可按目标筛选,并可控制信息的详细程度。 - - - `get_injection_info()` 返回所有目标的摘要注入信息。 - - `get_injection_info(target_prompt="...")` 返回指定目标的摘要注入信息。 - - `get_injection_info(detailed=True)` 返回所有目标的详细注入信息。 - - `get_injection_info(target_prompt="...", detailed=True)` 返回指定目标的详细注入信息。 - - Args: - target_prompt (str, optional): 如果指定,仅返回该目标的注入信息。 - detailed (bool, optional): 如果为 True,则返回包含注入类型和内容的详细信息。 - 默认为 False,返回摘要信息。 - - Returns: - dict[str, list[dict]]: 一个字典,键是目标提示词名称, - 值是按优先级排序的注入信息列表。 + 此方法现在动态构建信息,以反映当前启用的组件和规则。 """ info_map = {} - async with self._lock: - all_targets = set(self._dynamic_rules.keys()) | set(self.get_core_prompts()) + all_core_prompts = self.get_core_prompts() + # 确定要处理的目标:如果指定了有效的目标,则只处理它;否则处理所有核心 prompt + targets_to_process = [target_prompt] if target_prompt and target_prompt in all_core_prompts else all_core_prompts - # 如果指定了目标,则只处理该目标 - targets_to_process = [target_prompt] if target_prompt and target_prompt in all_targets else sorted(all_targets) + for target in targets_to_process: + # 动态构建该目标的所有有效规则 + rules_for_target = await self._build_rules_for_target(target) + if not rules_for_target: + info_map[target] = [] + continue - for target in targets_to_process: - rules = self._dynamic_rules.get(target, {}) - if not rules: - info_map[target] = [] - continue + info_list = [] + for rule, _, source in rules_for_target: + # 从规则对象中获取其所属组件的名称 + prompt_name = rule.owner_component + if detailed: + # 如果需要详细信息,则添加更多字段 + info_list.append( + { + "name": prompt_name, + "priority": rule.priority, + "source": source, + "injection_type": rule.injection_type.value, + "target_content": rule.target_content, + } + ) + else: + # 否则只添加基本信息 + info_list.append({"name": prompt_name, "priority": rule.priority, "source": source}) - info_list = [] - for prompt_name, (rule, _, source) in rules.items(): - if detailed: - info_list.append( - { - "name": prompt_name, - "priority": rule.priority, - "source": source, - "injection_type": rule.injection_type.value, - "target_content": rule.target_content, - } - ) - else: - info_list.append({"name": prompt_name, "priority": rule.priority, "source": source}) - - info_list.sort(key=lambda x: x["priority"]) - info_map[target] = info_list + # 按优先级对结果进行排序 + info_list.sort(key=lambda x: x["priority"]) + info_map[target] = info_list return info_map - def get_injection_rules( + async def get_injection_rules( self, target_prompt: str | None = None, component_name: str | None = None, ) -> dict[str, dict[str, "InjectionRule"]]: """ - 获取动态注入规则,可通过目标或组件名称进行筛选。 + 获取所有(包括静态和运行时)注入规则,可通过目标或组件名称进行筛选。 - 不提供任何参数时,返回所有规则。 - 提供 `target_prompt` 时,仅返回注入到该目标的规则。 @@ -527,44 +595,44 @@ class PromptComponentManager: component_name (str, optional): 按注入组件名称筛选。 Returns: - dict[str, dict[str, InjectionRule]]: 一个深拷贝的规则字典。 + dict[str, dict[str, InjectionRule]]: 一个包含所有匹配规则的深拷贝字典。 结构: { "target_prompt": { "component_name": InjectionRule } } """ - rules_copy = {} - # 筛选目标 - targets_to_check = [target_prompt] if target_prompt else self._dynamic_rules.keys() + all_rules: dict[str, dict[str, InjectionRule]] = {} - for target in targets_to_check: - if target not in self._dynamic_rules: + # 1. 收集所有静态组件的规则 + static_components = component_registry.get_components_by_type(ComponentType.PROMPT) + for name, info in static_components.items(): + if not isinstance(info, PromptInfo): + continue + # 如果指定了 component_name 且不匹配,则跳过此组件 + if component_name and name != component_name: continue - rules_for_target = self._dynamic_rules[target] - target_copy = {} + for rule in info.injection_rules: + # 如果指定了 target_prompt 且不匹配,则跳过此规则 + if target_prompt and rule.target_prompt != target_prompt: + continue + target_dict = all_rules.setdefault(rule.target_prompt, {}) + target_dict[name] = rule - # 筛选组件 - if component_name: - if component_name in rules_for_target: - rule, _, _ = rules_for_target[component_name] - target_copy[component_name] = rule - else: - for name, (rule, _, _) in rules_for_target.items(): - target_copy[name] = rule + # 2. 收集并合并所有运行时规则 + async with self._lock: + for target, rules_in_target in self._dynamic_rules.items(): + # 如果指定了 target_prompt 且不匹配,则跳过此目标下的所有规则 + if target_prompt and target != target_prompt: + continue - if target_copy: - rules_copy[target] = target_copy + for name, (rule, _, _) in rules_in_target.items(): + # 如果指定了 component_name 且不匹配,则跳过此规则 + if component_name and name != component_name: + continue + target_dict = all_rules.setdefault(target, {}) + # 运行时规则会覆盖同名的静态规则 + target_dict[name] = rule - # 如果是按组件筛选且未指定目标,则需遍历所有目标 - if component_name and not target_prompt: - found_rules = {} - for target, rules in self._dynamic_rules.items(): - if component_name in rules: - rule, _, _ = rules[component_name] - if target not in found_rules: - found_rules[target] = {} - found_rules[target][component_name] = rule - return copy.deepcopy(found_rules) - - return copy.deepcopy(rules_copy) + # 返回深拷贝以防止外部修改影响内部状态 + return copy.deepcopy(all_rules) # 创建全局单例 (Singleton) diff --git a/src/individuality/not_using/offline_llm.py b/src/individuality/not_using/offline_llm.py deleted file mode 100644 index 752293ab8..000000000 --- a/src/individuality/not_using/offline_llm.py +++ /dev/null @@ -1,127 +0,0 @@ -import asyncio -import os -import time - -import aiohttp -import requests -from rich.traceback import install - -from src.common.logger import get_logger -from src.common.tcp_connector import get_tcp_connector - -install(extra_lines=3) - -logger = get_logger("offline_llm") - - -class LLMRequestOff: - def __init__(self, model_name="Pro/deepseek-ai/DeepSeek-V3", **kwargs): - self.model_name = model_name - self.params = kwargs - self.api_key = os.getenv("SILICONFLOW_KEY") - self.base_url = os.getenv("SILICONFLOW_BASE_URL") - - if not self.api_key or not self.base_url: - raise ValueError("环境变量未正确加载:SILICONFLOW_KEY 或 SILICONFLOW_BASE_URL 未设置") - - # logger.info(f"API URL: {self.base_url}") # 使用 logger 记录 base_url - - def generate_response(self, prompt: str) -> str | tuple[str, str]: - """根据输入的提示生成模型的响应""" - headers = {"Authorization": f"Bearer {self.api_key}", "Content-Type": "application/json"} - - # 构建请求体 - data = { - "model": self.model_name, - "messages": [{"role": "user", "content": prompt}], - "temperature": 0.4, - **self.params, - } - - # 发送请求到完整的 chat/completions 端点 - api_url = f"{self.base_url.rstrip('/')}/chat/completions" # type: ignore - logger.info(f"Request URL: {api_url}") # 记录请求的 URL - - max_retries = 3 - base_wait_time = 15 # 基础等待时间(秒) - - for retry in range(max_retries): - try: - response = requests.post(api_url, headers=headers, json=data) - - if response.status_code == 429: - wait_time = base_wait_time * (2**retry) # 指数退避 - logger.warning(f"遇到请求限制(429),等待{wait_time}秒后重试...") - time.sleep(wait_time) - continue - - response.raise_for_status() # 检查其他响应状态 - - result = response.json() - if "choices" in result and len(result["choices"]) > 0: - content = result["choices"][0]["message"]["content"] - reasoning_content = result["choices"][0]["message"].get("reasoning_content", "") - return content, reasoning_content - return "没有返回结果", "" - - except Exception as e: - if retry < max_retries - 1: # 如果还有重试机会 - wait_time = base_wait_time * (2**retry) - logger.error(f"[回复]请求失败,等待{wait_time}秒后重试... 错误: {e!s}") - time.sleep(wait_time) - else: - logger.error(f"请求失败: {e!s}") - return f"请求失败: {e!s}", "" - - logger.error("达到最大重试次数,请求仍然失败") - return "达到最大重试次数,请求仍然失败", "" - - async def generate_response_async(self, prompt: str) -> str | tuple[str, str]: - """异步方式根据输入的提示生成模型的响应""" - headers = {"Authorization": f"Bearer {self.api_key}", "Content-Type": "application/json"} - - # 构建请求体 - data = { - "model": self.model_name, - "messages": [{"role": "user", "content": prompt}], - "temperature": 0.5, - **self.params, - } - - # 发送请求到完整的 chat/completions 端点 - api_url = f"{self.base_url.rstrip('/')}/chat/completions" # type: ignore - logger.info(f"Request URL: {api_url}") # 记录请求的 URL - - max_retries = 3 - base_wait_time = 15 - - async with aiohttp.ClientSession(connector=await get_tcp_connector()) as session: - for retry in range(max_retries): - try: - async with session.post(api_url, headers=headers, json=data) as response: - if response.status == 429: - wait_time = base_wait_time * (2**retry) # 指数退避 - logger.warning(f"遇到请求限制(429),等待{wait_time}秒后重试...") - await asyncio.sleep(wait_time) - continue - - response.raise_for_status() # 检查其他响应状态 - - result = await response.json() - if "choices" in result and len(result["choices"]) > 0: - content = result["choices"][0]["message"]["content"] - reasoning_content = result["choices"][0]["message"].get("reasoning_content", "") - return content, reasoning_content - return "没有返回结果", "" - - except Exception as e: - if retry < max_retries - 1: # 如果还有重试机会 - wait_time = base_wait_time * (2**retry) - logger.error(f"[回复]请求失败,等待{wait_time}秒后重试... 错误: {e!s}") - await asyncio.sleep(wait_time) - else: - logger.error(f"请求失败: {e!s}") - return f"请求失败: {e!s}", "" - - logger.error("达到最大重试次数,请求仍然失败") - return "达到最大重试次数,请求仍然失败", "" diff --git a/src/individuality/not_using/per_bf_gen.py b/src/individuality/not_using/per_bf_gen.py deleted file mode 100644 index 326a94aaf..000000000 --- a/src/individuality/not_using/per_bf_gen.py +++ /dev/null @@ -1,307 +0,0 @@ -import os -import random -import sys - -import orjson -import toml -from dotenv import load_dotenv -from tqdm import tqdm - -# 添加项目根目录到 Python 路径 -root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")) -sys.path.append(root_path) - -# 加载配置文件 -config_path = os.path.join(root_path, "config", "bot_config.toml") -with open(config_path, encoding="utf-8") as f: - config = toml.load(f) - -# 现在可以导入src模块 -from individuality.not_using.scene import get_scene_by_factor, PERSONALITY_SCENES # noqa E402 -from individuality.not_using.questionnaire import FACTOR_DESCRIPTIONS -from individuality.not_using.offline_llm import LLMRequestOff - -# 加载环境变量 -env_path = os.path.join(root_path, ".env") -if os.path.exists(env_path): - print(f"从 {env_path} 加载环境变量") - load_dotenv(env_path) -else: - print(f"未找到环境变量文件: {env_path}") - print("将使用默认配置") - - -def adapt_scene(scene: str) -> str: - personality_core = config["personality"]["personality_core"] - personality_side = config["personality"]["personality_side"] - personality_side = random.choice(personality_side) - identitys = config["identity"]["identity"] - identity = random.choice(identitys) - - """ - 根据config中的属性,改编场景使其更适合当前角色 - - Args: - scene: 原始场景描述 - - Returns: - str: 改编后的场景描述 - """ - try: - prompt = f""" -这是一个参与人格测评的角色形象: -- 昵称: {config["bot"]["nickname"]} -- 性别: {config["identity"]["gender"]} -- 年龄: {config["identity"]["age"]}岁 -- 外貌: {config["identity"]["appearance"]} -- 性格核心: {personality_core} -- 性格侧面: {personality_side} -- 身份细节: {identity} - -请根据上述形象,改编以下场景,在测评中,用户将根据该场景给出上述角色形象的反应: -{scene} -保持场景的本质不变,但最好贴近生活且具体,并且让它更适合这个角色。 -改编后的场景应该自然、连贯,并考虑角色的年龄、身份和性格特点。只返回改编后的场景描述,不要包含其他说明。注意{config["bot"]["nickname"]}是面对这个场景的人,而不是场景的其他人。场景中不会有其描述, -现在,请你给出改编后的场景描述 -""" - - llm = LLMRequestOff(model_name=config["model"]["llm_normal"]["name"]) - adapted_scene, _ = llm.generate_response(prompt) - - # 检查返回的场景是否为空或错误信息 - if not adapted_scene or "错误" in adapted_scene or "失败" in adapted_scene: - print("场景改编失败,将使用原始场景") - return scene - - return adapted_scene - except Exception as e: - print(f"场景改编过程出错:{e!s},将使用原始场景") - return scene - - -class PersonalityEvaluatorDirect: - def __init__(self): - self.personality_traits = {"开放性": 0, "严谨性": 0, "外向性": 0, "宜人性": 0, "神经质": 0} - self.scenarios = [] - self.final_scores: dict[str, float] = {"开放性": 0, "严谨性": 0, "外向性": 0, "宜人性": 0, "神经质": 0} - self.dimension_counts = dict.fromkeys(self.final_scores, 0) - - # 为每个人格特质获取对应的场景 - for trait in PERSONALITY_SCENES: - scenes = get_scene_by_factor(trait) - if not scenes: - continue - - # 从每个维度选择3个场景 - import random - - scene_keys = list(scenes.keys()) - selected_scenes = random.sample(scene_keys, min(3, len(scene_keys))) - - for scene_key in selected_scenes: - scene = scenes[scene_key] - - # 为每个场景添加评估维度 - # 主维度是当前特质,次维度随机选择一个其他特质 - other_traits = [t for t in PERSONALITY_SCENES if t != trait] - secondary_trait = random.choice(other_traits) - - self.scenarios.append( - {"场景": scene["scenario"], "评估维度": [trait, secondary_trait], "场景编号": scene_key} - ) - - self.llm = LLMRequestOff() - - def evaluate_response(self, scenario: str, response: str, dimensions: list[str]) -> dict[str, float]: - """ - 使用 DeepSeek AI 评估用户对特定场景的反应 - """ - # 构建维度描述 - dimension_descriptions = [f"- {dim}:{desc}" for dim in dimensions if (desc := FACTOR_DESCRIPTIONS.get(dim, ""))] - - dimensions_text = "\n".join(dimension_descriptions) - - prompt = f"""请根据以下场景和用户描述,评估用户在大五人格模型中的相关维度得分(1-6分)。 - -场景描述: -{scenario} - -用户回应: -{response} - -需要评估的维度说明: -{dimensions_text} - -请按照以下格式输出评估结果(仅输出JSON格式): -{{ - "{dimensions[0]}": 分数, - "{dimensions[1]}": 分数 -}} - -评分标准: -1 = 非常不符合该维度特征 -2 = 比较不符合该维度特征 -3 = 有点不符合该维度特征 -4 = 有点符合该维度特征 -5 = 比较符合该维度特征 -6 = 非常符合该维度特征 - -请根据用户的回应,结合场景和维度说明进行评分。确保分数在1-6之间,并给出合理的评估。""" - - try: - ai_response, _ = self.llm.generate_response(prompt) - # 尝试从AI响应中提取JSON部分 - start_idx = ai_response.find("{") - end_idx = ai_response.rfind("}") + 1 - if start_idx != -1 and end_idx != 0: - json_str = ai_response[start_idx:end_idx] - scores = orjson.loads(json_str) - # 确保所有分数在1-6之间 - return {k: max(1, min(6, float(v))) for k, v in scores.items()} - else: - print("AI响应格式不正确,使用默认评分") - return dict.fromkeys(dimensions, 3.5) - except Exception as e: - print(f"评估过程出错:{e!s}") - return dict.fromkeys(dimensions, 3.5) - - def run_evaluation(self): - """ - 运行整个评估过程 - """ - print(f"欢迎使用{config['bot']['nickname']}形象创建程序!") - print("接下来,将给您呈现一系列有关您bot的场景(共15个)。") - print("请想象您的bot在以下场景下会做什么,并描述您的bot的反应。") - print("每个场景都会进行不同方面的评估。") - print("\n角色基本信息:") - print(f"- 昵称:{config['bot']['nickname']}") - print(f"- 性格核心:{config['personality']['personality_core']}") - print(f"- 性格侧面:{config['personality']['personality_side']}") - print(f"- 身份细节:{config['identity']['identity']}") - print("\n准备好了吗?按回车键开始...") - input() - - total_scenarios = len(self.scenarios) - progress_bar = tqdm( - total=total_scenarios, - desc="场景进度", - ncols=100, - bar_format="{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}]", - ) - - for _i, scenario_data in enumerate(self.scenarios, 1): - # print(f"\n{'-' * 20} 场景 {i}/{total_scenarios} - {scenario_data['场景编号']} {'-' * 20}") - - # 改编场景,使其更适合当前角色 - print(f"{config['bot']['nickname']}祈祷中...") - adapted_scene = adapt_scene(scenario_data["场景"]) - scenario_data["改编场景"] = adapted_scene - - print(adapted_scene) - print(f"\n请描述{config['bot']['nickname']}在这种情况下会如何反应:") - response = input().strip() - - if not response: - print("反应描述不能为空!") - continue - - print("\n正在评估您的描述...") - scores = self.evaluate_response(adapted_scene, response, scenario_data["评估维度"]) - - # 更新最终分数 - for dimension, score in scores.items(): - self.final_scores[dimension] += score - self.dimension_counts[dimension] += 1 - - print("\n当前评估结果:") - print("-" * 30) - for dimension, score in scores.items(): - print(f"{dimension}: {score}/6") - - # 更新进度条 - progress_bar.update(1) - - # if i < total_scenarios: - # print("\n按回车键继续下一个场景...") - # input() - - progress_bar.close() - - # 计算平均分 - for dimension in self.final_scores: - if self.dimension_counts[dimension] > 0: - self.final_scores[dimension] = round(self.final_scores[dimension] / self.dimension_counts[dimension], 2) - - print("\n" + "=" * 50) - print(f" {config['bot']['nickname']}的人格特征评估结果 ".center(50)) - print("=" * 50) - for trait, score in self.final_scores.items(): - print(f"{trait}: {score}/6".ljust(20) + f"测试场景数:{self.dimension_counts[trait]}".rjust(30)) - print("=" * 50) - - # 返回评估结果 - return self.get_result() - - def get_result(self): - """ - 获取评估结果 - """ - return { - "final_scores": self.final_scores, - "dimension_counts": self.dimension_counts, - "scenarios": self.scenarios, - "bot_info": { - "nickname": config["bot"]["nickname"], - "gender": config["identity"]["gender"], - "age": config["identity"]["age"], - "height": config["identity"]["height"], - "weight": config["identity"]["weight"], - "appearance": config["identity"]["appearance"], - "personality_core": config["personality"]["personality_core"], - "personality_side": config["personality"]["personality_side"], - "identity": config["identity"]["identity"], - }, - } - - -def main(): - evaluator = PersonalityEvaluatorDirect() - result = evaluator.run_evaluation() - - # 准备简化的结果数据 - simplified_result = { - "openness": round(result["final_scores"]["开放性"] / 6, 1), # 转换为0-1范围 - "conscientiousness": round(result["final_scores"]["严谨性"] / 6, 1), - "extraversion": round(result["final_scores"]["外向性"] / 6, 1), - "agreeableness": round(result["final_scores"]["宜人性"] / 6, 1), - "neuroticism": round(result["final_scores"]["神经质"] / 6, 1), - "bot_nickname": config["bot"]["nickname"], - } - - # 确保目录存在 - save_dir = os.path.join(root_path, "data", "personality") - os.makedirs(save_dir, exist_ok=True) - - # 创建文件名,替换可能的非法字符 - bot_name = config["bot"]["nickname"] - # 替换Windows文件名中不允许的字符 - for char in ["\\", "/", ":", "*", "?", '"', "<", ">", "|"]: - bot_name = bot_name.replace(char, "_") - - file_name = f"{bot_name}_personality.per" - save_path = os.path.join(save_dir, file_name) - - # 保存简化的结果 - with open(save_path, "w", encoding="utf-8") as f: - f.write(orjson.dumps(simplified_result, option=orjson.OPT_INDENT_2).decode("utf-8")) - - print(f"\n结果已保存到 {save_path}") - - # 同时保存完整结果到results目录 - os.makedirs("results", exist_ok=True) - with open("results/personality_result.json", "w", encoding="utf-8") as f: - f.write(orjson.dumps(result, option=orjson.OPT_INDENT_2).decode("utf-8")) - - -if __name__ == "__main__": - main() diff --git a/src/individuality/not_using/questionnaire.py b/src/individuality/not_using/questionnaire.py deleted file mode 100644 index 8e965061d..000000000 --- a/src/individuality/not_using/questionnaire.py +++ /dev/null @@ -1,142 +0,0 @@ -# 人格测试问卷题目 -# 王孟成, 戴晓阳, & 姚树桥. (2011). -# 中国大五人格问卷的初步编制Ⅲ:简式版的制定及信效度检验. 中国临床心理学杂志, 19(04), Article 04. - -# 王孟成, 戴晓阳, & 姚树桥. (2010). -# 中国大五人格问卷的初步编制Ⅰ:理论框架与信度分析. 中国临床心理学杂志, 18(05), Article 05. - -PERSONALITY_QUESTIONS = [ - # 神经质维度 (F1) - {"id": 1, "content": "我常担心有什么不好的事情要发生", "factor": "神经质", "reverse_scoring": False}, - {"id": 2, "content": "我常感到害怕", "factor": "神经质", "reverse_scoring": False}, - {"id": 3, "content": "有时我觉得自己一无是处", "factor": "神经质", "reverse_scoring": False}, - {"id": 4, "content": "我很少感到忧郁或沮丧", "factor": "神经质", "reverse_scoring": True}, - {"id": 5, "content": "别人一句漫不经心的话,我常会联系在自己身上", "factor": "神经质", "reverse_scoring": False}, - {"id": 6, "content": "在面对压力时,我有种快要崩溃的感觉", "factor": "神经质", "reverse_scoring": False}, - {"id": 7, "content": "我常担忧一些无关紧要的事情", "factor": "神经质", "reverse_scoring": False}, - {"id": 8, "content": "我常常感到内心不踏实", "factor": "神经质", "reverse_scoring": False}, - # 严谨性维度 (F2) - {"id": 9, "content": "在工作上,我常只求能应付过去便可", "factor": "严谨性", "reverse_scoring": True}, - {"id": 10, "content": "一旦确定了目标,我会坚持努力地实现它", "factor": "严谨性", "reverse_scoring": False}, - {"id": 11, "content": "我常常是仔细考虑之后才做出决定", "factor": "严谨性", "reverse_scoring": False}, - {"id": 12, "content": "别人认为我是个慎重的人", "factor": "严谨性", "reverse_scoring": False}, - {"id": 13, "content": "做事讲究逻辑和条理是我的一个特点", "factor": "严谨性", "reverse_scoring": False}, - {"id": 14, "content": "我喜欢一开头就把事情计划好", "factor": "严谨性", "reverse_scoring": False}, - {"id": 15, "content": "我工作或学习很勤奋", "factor": "严谨性", "reverse_scoring": False}, - {"id": 16, "content": "我是个倾尽全力做事的人", "factor": "严谨性", "reverse_scoring": False}, - # 宜人性维度 (F3) - { - "id": 17, - "content": "尽管人类社会存在着一些阴暗的东西(如战争、罪恶、欺诈),我仍然相信人性总的来说是善良的", - "factor": "宜人性", - "reverse_scoring": False, - }, - {"id": 18, "content": "我觉得大部分人基本上是心怀善意的", "factor": "宜人性", "reverse_scoring": False}, - {"id": 19, "content": "虽然社会上有骗子,但我觉得大部分人还是可信的", "factor": "宜人性", "reverse_scoring": False}, - {"id": 20, "content": "我不太关心别人是否受到不公正的待遇", "factor": "宜人性", "reverse_scoring": True}, - {"id": 21, "content": "我时常觉得别人的痛苦与我无关", "factor": "宜人性", "reverse_scoring": True}, - {"id": 22, "content": "我常为那些遭遇不幸的人感到难过", "factor": "宜人性", "reverse_scoring": False}, - {"id": 23, "content": "我是那种只照顾好自己,不替别人担忧的人", "factor": "宜人性", "reverse_scoring": True}, - {"id": 24, "content": "当别人向我诉说不幸时,我常感到难过", "factor": "宜人性", "reverse_scoring": False}, - # 开放性维度 (F4) - {"id": 25, "content": "我的想象力相当丰富", "factor": "开放性", "reverse_scoring": False}, - {"id": 26, "content": "我头脑中经常充满生动的画面", "factor": "开放性", "reverse_scoring": False}, - {"id": 27, "content": "我对许多事情有着很强的好奇心", "factor": "开放性", "reverse_scoring": False}, - {"id": 28, "content": "我喜欢冒险", "factor": "开放性", "reverse_scoring": False}, - {"id": 29, "content": "我是个勇于冒险,突破常规的人", "factor": "开放性", "reverse_scoring": False}, - {"id": 30, "content": "我身上具有别人没有的冒险精神", "factor": "开放性", "reverse_scoring": False}, - { - "id": 31, - "content": "我渴望学习一些新东西,即使它们与我的日常生活无关", - "factor": "开放性", - "reverse_scoring": False, - }, - { - "id": 32, - "content": "我很愿意也很容易接受那些新事物、新观点、新想法", - "factor": "开放性", - "reverse_scoring": False, - }, - # 外向性维度 (F5) - {"id": 33, "content": "我喜欢参加社交与娱乐聚会", "factor": "外向性", "reverse_scoring": False}, - {"id": 34, "content": "我对人多的聚会感到乏味", "factor": "外向性", "reverse_scoring": True}, - {"id": 35, "content": "我尽量避免参加人多的聚会和嘈杂的环境", "factor": "外向性", "reverse_scoring": True}, - {"id": 36, "content": "在热闹的聚会上,我常常表现主动并尽情玩耍", "factor": "外向性", "reverse_scoring": False}, - {"id": 37, "content": "有我在的场合一般不会冷场", "factor": "外向性", "reverse_scoring": False}, - {"id": 38, "content": "我希望成为领导者而不是被领导者", "factor": "外向性", "reverse_scoring": False}, - {"id": 39, "content": "在一个团体中,我希望处于领导地位", "factor": "外向性", "reverse_scoring": False}, - {"id": 40, "content": "别人多认为我是一个热情和友好的人", "factor": "外向性", "reverse_scoring": False}, -] - -# 因子维度说明 -FACTOR_DESCRIPTIONS = { - "外向性": { - "description": "反映个体神经系统的强弱和动力特征。外向性主要表现为个体在人际交往和社交活动中的倾向性," - "包括对社交活动的兴趣、" - "对人群的态度、社交互动中的主动程度以及在群体中的影响力。高分者倾向于积极参与社交活动,乐于与人交往,善于表达自我," - "并往往在群体中发挥领导作用;低分者则倾向于独处,不喜欢热闹的社交场合,表现出内向、安静的特征。", - "trait_words": ["热情", "活力", "社交", "主动"], - "subfactors": { - "合群性": "个体愿意与他人聚在一起,即接近人群的倾向;高分表现乐群、好交际,低分表现封闭、独处", - "热情": "个体对待别人时所表现出的态度;高分表现热情好客,低分表现冷淡", - "支配性": "个体喜欢指使、操纵他人,倾向于领导别人的特点;高分表现好强、发号施令,低分表现顺从、低调", - "活跃": "个体精力充沛,活跃、主动性等特点;高分表现活跃,低分表现安静", - }, - }, - "神经质": { - "description": "反映个体情绪的状态和体验内心苦恼的倾向性。这个维度主要关注个体在面对压力、" - "挫折和日常生活挑战时的情绪稳定性和适应能力。它包含了对焦虑、抑郁、愤怒等负面情绪的敏感程度," - "以及个体对这些情绪的调节和控制能力。高分者容易体验负面情绪,对压力较为敏感,情绪波动较大;" - "低分者则表现出较强的情绪稳定性,能够较好地应对压力和挫折。", - "trait_words": ["稳定", "沉着", "从容", "坚韧"], - "subfactors": { - "焦虑": "个体体验焦虑感的个体差异;高分表现坐立不安,低分表现平静", - "抑郁": "个体体验抑郁情感的个体差异;高分表现郁郁寡欢,低分表现平静", - "敏感多疑": "个体常常关注自己的内心活动,行为和过于意识人对自己的看法、评价;高分表现敏感多疑," - "低分表现淡定、自信", - "脆弱性": "个体在危机或困难面前无力、脆弱的特点;高分表现无能、易受伤、逃避,低分表现坚强", - "愤怒-敌意": "个体准备体验愤怒,及相关情绪的状态;高分表现暴躁易怒,低分表现平静", - }, - }, - "严谨性": { - "description": "反映个体在目标导向行为上的组织、坚持和动机特征。这个维度体现了个体在工作、" - "学习等目标性活动中的自我约束和行为管理能力。它涉及到个体的责任感、自律性、计划性、条理性以及完成任务的态度。" - "高分者往往表现出强烈的责任心、良好的组织能力、谨慎的决策风格和持续的努力精神;低分者则可能表现出随意性强、" - "缺乏规划、做事马虎或易放弃的特点。", - "trait_words": ["负责", "自律", "条理", "勤奋"], - "subfactors": { - "责任心": "个体对待任务和他人认真负责,以及对自己承诺的信守;高分表现有责任心、负责任," - "低分表现推卸责任、逃避处罚", - "自我控制": "个体约束自己的能力,及自始至终的坚持性;高分表现自制、有毅力,低分表现冲动、无毅力", - "审慎性": "个体在采取具体行动前的心理状态;高分表现谨慎、小心,低分表现鲁莽、草率", - "条理性": "个体处理事务和工作的秩序,条理和逻辑性;高分表现整洁、有秩序,低分表现混乱、遗漏", - "勤奋": "个体工作和学习的努力程度及为达到目标而表现出的进取精神;高分表现勤奋、刻苦,低分表现懒散", - }, - }, - "开放性": { - "description": "反映个体对新异事物、新观念和新经验的接受程度,以及在思维和行为方面的创新倾向。" - "这个维度体现了个体在认知和体验方面的广度、深度和灵活性。它包括对艺术的欣赏能力、对知识的求知欲、想象力的丰富程度," - "以及对冒险和创新的态度。高分者往往具有丰富的想象力、广泛的兴趣、开放的思维方式和创新的倾向;低分者则倾向于保守、" - "传统,喜欢熟悉和常规的事物。", - "trait_words": ["创新", "好奇", "艺术", "冒险"], - "subfactors": { - "幻想": "个体富于幻想和想象的水平;高分表现想象力丰富,低分表现想象力匮乏", - "审美": "个体对于艺术和美的敏感与热爱程度;高分表现富有艺术气息,低分表现一般对艺术不敏感", - "好奇心": "个体对未知事物的态度;高分表现兴趣广泛、好奇心浓,低分表现兴趣少、无好奇心", - "冒险精神": "个体愿意尝试有风险活动的个体差异;高分表现好冒险,低分表现保守", - "价值观念": "个体对新事物、新观念、怪异想法的态度;高分表现开放、坦然接受新事物,低分则相反", - }, - }, - "宜人性": { - "description": "反映个体在人际关系中的亲和倾向,体现了对他人的关心、同情和合作意愿。" - "这个维度主要关注个体与他人互动时的态度和行为特征,包括对他人的信任程度、同理心水平、" - "助人意愿以及在人际冲突中的处理方式。高分者通常表现出友善、富有同情心、乐于助人的特质,善于与他人建立和谐关系;" - "低分者则可能表现出较少的人际关注,在社交互动中更注重自身利益,较少考虑他人感受。", - "trait_words": ["友善", "同理", "信任", "合作"], - "subfactors": { - "信任": "个体对他人和/或他人言论的相信程度;高分表现信任他人,低分表现怀疑", - "体贴": "个体对别人的兴趣和需要的关注程度;高分表现体贴、温存,低分表现冷漠、不在乎", - "同情": "个体对处于不利地位的人或物的态度;高分表现富有同情心,低分表现冷漠", - }, - }, -} diff --git a/src/individuality/not_using/scene.py b/src/individuality/not_using/scene.py deleted file mode 100644 index 9c16358e6..000000000 --- a/src/individuality/not_using/scene.py +++ /dev/null @@ -1,44 +0,0 @@ -import os -from typing import Any - -import orjson - - -def load_scenes() -> dict[str, Any]: - """ - 从JSON文件加载场景数据 - - Returns: - Dict: 包含所有场景的字典 - """ - current_dir = os.path.dirname(os.path.abspath(__file__)) - json_path = os.path.join(current_dir, "template_scene.json") - - with open(json_path, encoding="utf-8") as f: - return orjson.loads(f.read()) - - -PERSONALITY_SCENES = load_scenes() - - -def get_scene_by_factor(factor: str) -> dict | None: - """ - 根据人格因子获取对应的情景测试 - - Args: - factor (str): 人格因子名称 - - Returns: - dict: 包含情景描述的字典 - """ - return PERSONALITY_SCENES.get(factor, None) - - -def get_all_scenes() -> dict: - """ - 获取所有情景测试 - - Returns: - Dict: 所有情景测试的字典 - """ - return PERSONALITY_SCENES diff --git a/src/individuality/not_using/template_scene.json b/src/individuality/not_using/template_scene.json deleted file mode 100644 index a6542e75d..000000000 --- a/src/individuality/not_using/template_scene.json +++ /dev/null @@ -1,112 +0,0 @@ -{ - "外向性": { - "场景1": { - "scenario": "你刚刚搬到一个新的城市工作。今天是你入职的第一天,在公司的电梯里,一位同事微笑着和你打招呼:\n\n同事:「嗨!你是新来的同事吧?我是市场部的小林。」\n\n同事看起来很友善,还主动介绍说:「待会午饭时间,我们部门有几个人准备一起去楼下新开的餐厅,你要一起来吗?可以认识一下其他同事。」", - "explanation": "这个场景通过职场社交情境,观察个体对于新环境、新社交圈的态度和反应倾向。" - }, - "场景2": { - "scenario": "在大学班级群里,班长发起了一个组织班级联谊活动的投票:\n\n班长:「大家好!下周末我们准备举办一次班级联谊活动,地点在学校附近的KTV。想请大家报名参加,也欢迎大家邀请其他班级的同学!」\n\n已经有几个同学在群里积极响应,有人@你问你要不要一起参加。", - "explanation": "通过班级活动场景,观察个体对群体社交活动的参与意愿。" - }, - "场景3": { - "scenario": "你在社交平台上发布了一条动态,收到了很多陌生网友的评论和私信:\n\n网友A:「你说的这个观点很有意思!想和你多交流一下。」\n\n网友B:「我也对这个话题很感兴趣,要不要建个群一起讨论?」", - "explanation": "通过网络社交场景,观察个体对线上社交的态度。" - }, - "场景4": { - "scenario": "你暗恋的对象今天主动来找你:\n\n对方:「那个...我最近在准备一个演讲比赛,听说你口才很好。能不能请你帮我看看演讲稿,顺便给我一些建议?如果你有时间的话,可以一起吃个饭聊聊。」", - "explanation": "通过恋爱情境,观察个体在面对心仪对象时的社交表现。" - }, - "场景5": { - "scenario": "在一次线下读书会上,主持人突然点名让你分享读后感:\n\n主持人:「听说你对这本书很有见解,能不能和大家分享一下你的想法?」\n\n现场有二十多个陌生的读书爱好者,都期待地看着你。", - "explanation": "通过即兴发言场景,观察个体的社交表现欲和公众表达能力。" - } - }, - "神经质": { - "场景1": { - "scenario": "你正在准备一个重要的项目演示,这关系到你的晋升机会。就在演示前30分钟,你收到了主管发来的消息:\n\n主管:「临时有个变动,CEO也会来听你的演示。他对这个项目特别感兴趣。」\n\n正当你准备回复时,主管又发来一条:「对了,能不能把演示时间压缩到15分钟?CEO下午还有其他安排。你之前准备的是30分钟的版本对吧?」", - "explanation": "这个场景通过突发的压力情境,观察个体在面对计划外变化时的情绪反应和调节能力。" - }, - "场景2": { - "scenario": "期末考试前一天晚上,你收到了好朋友发来的消息:\n\n好朋友:「不好意思这么晚打扰你...我看你平时成绩很好,能不能帮我解答几个问题?我真的很担心明天的考试。」\n\n你看了看时间,已经是晚上11点,而你原本计划的复习还没完成。", - "explanation": "通过考试压力场景,观察个体在时间紧张时的情绪管理。" - }, - "场景3": { - "scenario": "你在社交媒体上发表的一个观点引发了争议,有不少人开始批评你:\n\n网友A:「这种观点也好意思说出来,真是无知。」\n\n网友B:「建议楼主先去补补课再来发言。」\n\n评论区里的负面评论越来越多,还有人开始人身攻击。", - "explanation": "通过网络争议场景,观察个体面对批评时的心理承受能力。" - }, - "场景4": { - "scenario": "你和恋人约好今天一起看电影,但在约定时间前半小时,对方发来消息:\n\n恋人:「对不起,我临时有点事,可能要迟到一会儿。」\n\n二十分钟后,对方又发来消息:「可能要再等等,抱歉!」\n\n电影快要开始了,但对方还是没有出现。", - "explanation": "通过恋爱情境,观察个体对不确定性的忍耐程度。" - }, - "场景5": { - "scenario": "在一次重要的小组展示中,你的组员在演示途中突然卡壳了:\n\n组员小声对你说:「我忘词了,接下来的部分是什么来着...」\n\n台下的老师和同学都在等待,气氛有些尴尬。", - "explanation": "通过公开场合的突发状况,观察个体的应急反应和压力处理能力。" - } - }, - "严谨性": { - "场景1": { - "scenario": "你是团队的项目负责人,刚刚接手了一个为期两个月的重要项目。在第一次团队会议上:\n\n小王:「老大,我觉得两个月时间很充裕,我们先做着看吧,遇到问题再解决。」\n\n小张:「要不要先列个时间表?不过感觉太详细的计划也没必要,点到为止就行。」\n\n小李:「客户那边说如果能提前完成有奖励,我觉得我们可以先做快一点的部分。」", - "explanation": "这个场景通过项目管理情境,体现个体在工作方法、计划性和责任心方面的特征。" - }, - "场景2": { - "scenario": "期末小组作业,组长让大家分工完成一份研究报告。在截止日期前三天:\n\n组员A:「我的部分大概写完了,感觉还行。」\n\n组员B:「我这边可能还要一天才能完成,最近太忙了。」\n\n组员C发来一份没有任何引用出处、可能存在抄袭的内容:「我写完了,你们看看怎么样?」", - "explanation": "通过学习场景,观察个体对学术规范和质量要求的重视程度。" - }, - "场景3": { - "scenario": "你在一个兴趣小组的群聊中,大家正在讨论举办一次线下活动:\n\n成员A:「到时候见面就知道具体怎么玩了!」\n\n成员B:「对啊,随意一点挺好的。」\n\n成员C:「人来了自然就热闹了。」", - "explanation": "通过活动组织场景,观察个体对活动计划的态度。" - }, - "场景4": { - "scenario": "你的好友小明邀请你一起参加一个重要的演出活动,他说:\n\n小明:「到时候我们就即兴发挥吧!不用排练了,我相信我们的默契。」\n\n距离演出还有三天,但节目内容、配乐和服装都还没有确定。", - "explanation": "通过演出准备场景,观察个体的计划性和对不确定性的接受程度。" - }, - "场景5": { - "scenario": "在一个重要的团队项目中,你发现一个同事的工作存在明显错误:\n\n同事:「差不多就行了,反正领导也看不出来。」\n\n这个错误可能不会立即造成问题,但长期来看可能会影响项目质量。", - "explanation": "通过工作质量场景,观察个体对细节和标准的坚持程度。" - } - }, - "开放性": { - "场景1": { - "scenario": "周末下午,你的好友小美兴致勃勃地给你打电话:\n\n小美:「我刚发现一个特别有意思的沉浸式艺术展!不是传统那种挂画的展览,而是把整个空间都变成了艺术品。观众要穿特制的服装,还要带上VR眼镜,好像还有AI实时互动!」\n\n小美继续说:「虽然票价不便宜,但听说体验很独特。网上评价两极分化,有人说是前所未有的艺术革新,也有人说是哗众取宠。要不要周末一起去体验一下?」", - "explanation": "这个场景通过新型艺术体验,反映个体对创新事物的接受程度和尝试意愿。" - }, - "场景2": { - "scenario": "在一节创意写作课上,老师提出了一个特别的作业:\n\n老师:「下周的作业是用AI写作工具协助创作一篇小说。你们可以自由探索如何与AI合作,打破传统写作方式。」\n\n班上随即展开了激烈讨论,有人认为这是对创作的亵渎,也有人对这种新形式感到兴奋。", - "explanation": "通过新技术应用场景,观察个体对创新学习方式的态度。" - }, - "场景3": { - "scenario": "在社交媒体上,你看到一个朋友分享了一种新的学习方式:\n\n「最近我在尝试'沉浸式学习',就是完全投入到一个全新的领域。比如学习一门陌生的语言,或者尝试完全不同的职业技能。虽然过程会很辛苦,但这种打破舒适圈的感觉真的很棒!」\n\n评论区里争论不断,有人认为这种学习方式效率高,也有人觉得太激进。", - "explanation": "通过新型学习方式,观察个体对创新和挑战的态度。" - }, - "场景4": { - "scenario": "你的朋友向你推荐了一种新的饮食方式:\n\n朋友:「我最近在尝试'未来食品',比如人造肉、3D打印食物、昆虫蛋白等。这不仅对环境友好,营养也很均衡。要不要一起来尝试看看?」\n\n这个提议让你感到好奇又犹豫,你之前从未尝试过这些新型食物。", - "explanation": "通过饮食创新场景,观察个体对新事物的接受度和尝试精神。" - }, - "场景5": { - "scenario": "在一次朋友聚会上,大家正在讨论未来职业规划:\n\n朋友A:「我准备辞职去做自媒体,专门介绍一些小众的文化和艺术。」\n\n朋友B:「我想去学习生物科技,准备转行做人造肉研发。」\n\n朋友C:「我在考虑加入一个区块链创业项目,虽然风险很大。」", - "explanation": "通过职业选择场景,观察个体对新兴领域的探索意愿。" - } - }, - "宜人性": { - "场景1": { - "scenario": "在回家的公交车上,你遇到这样一幕:\n\n一位老奶奶颤颤巍巍地上了车,车上座位已经坐满了。她站在你旁边,看起来很疲惫。这时你听到前排两个年轻人的对话:\n\n年轻人A:「那个老太太好像站不稳,看起来挺累的。」\n\n年轻人B:「现在的老年人真是...我看她包里还有菜,肯定是去菜市场买完菜回来的,这么多人都不知道叫子女开车接送。」\n\n就在这时,老奶奶一个趔趄,差点摔倒。她扶住了扶手,但包里的东西洒了一些出来。", - "explanation": "这个场景通过公共场合的助人情境,体现个体的同理心和对他人需求的关注程度。" - }, - "场景2": { - "scenario": "在班级群里,有同学发起为生病住院的同学捐款:\n\n同学A:「大家好,小林最近得了重病住院,医药费很贵,家里负担很重。我们要不要一起帮帮他?」\n\n同学B:「我觉得这是他家里的事,我们不方便参与吧。」\n\n同学C:「但是都是同学一场,帮帮忙也是应该的。」", - "explanation": "通过同学互助场景,观察个体的助人意愿和同理心。" - }, - "场景3": { - "scenario": "在一个网络讨论组里,有人发布了求助信息:\n\n求助者:「最近心情很低落,感觉生活很压抑,不知道该怎么办...」\n\n评论区里已经有一些回复:\n「生活本来就是这样,想开点!」\n「你这样子太消极了,要积极面对。」\n「谁还没点烦心事啊,过段时间就好了。」", - "explanation": "通过网络互助场景,观察个体的共情能力和安慰方式。" - }, - "场景4": { - "scenario": "你的朋友向你倾诉工作压力:\n\n朋友:「最近工作真的好累,感觉快坚持不下去了...」\n\n但今天你也遇到了很多烦心事,心情也不太好。", - "explanation": "通过感情关系场景,观察个体在自身状态不佳时的关怀能力。" - }, - "场景5": { - "scenario": "在一次团队项目中,新来的同事小王因为经验不足,造成了一个严重的错误。在部门会议上:\n\n主管:「这个错误造成了很大的损失,是谁负责的这部分?」\n\n小王看起来很紧张,欲言又止。你知道是他造成的错误,同时你也是这个项目的共同负责人。", - "explanation": "通过职场情境,观察个体在面对他人过错时的态度和处理方式。" - } - } -} \ No newline at end of file diff --git a/src/main.py b/src/main.py index 2d7d8c82a..c1100c1a0 100644 --- a/src/main.py +++ b/src/main.py @@ -389,7 +389,7 @@ class MainSystem: MoFox_Bot(第三方修改版) 全部组件已成功启动! ========================================================= -🌐 项目地址: https://github.com/MoFox-Studio/MoFox_Bot +🌐 项目地址: https://github.com/MoFox-Studio/MoFox-Core 🏠 官方项目: https://github.com/MaiM-with-u/MaiBot ========================================================= 这是基于原版MMC的社区改版,包含增强功能和优化(同时也有更多的'特性') diff --git a/src/plugin_system/apis/plugin_manage_api.py b/src/plugin_system/apis/plugin_manage_api.py index d7a802b8c..ccd05dc94 100644 --- a/src/plugin_system/apis/plugin_manage_api.py +++ b/src/plugin_system/apis/plugin_manage_api.py @@ -1,117 +1,536 @@ -def list_loaded_plugins() -> list[str]: +""" +Plugin Manage API +================= + +该模块提供了用于管理插件和组件生命周期、状态和信息查询的核心API。 +功能包括插件的加载、重载、注册、扫描,组件的启用/禁用,以及系统状态报告的生成。 +所有函数都设计为异步或同步,以适应不同的调用上下文。 +""" + +import os +from typing import Any, Literal + +from src.common.logger import get_logger +from src.plugin_system.base.component_types import ComponentType +from src.plugin_system.core.component_registry import ComponentInfo, component_registry +from src.plugin_system.core.plugin_manager import plugin_manager + +# 初始化日志记录器 +logger = get_logger("plugin_manage_api") + + +# -------------------------------------------------------------------------------- +# Section 1: 插件生命周期管理 (Plugin Lifecycle Management) +# -------------------------------------------------------------------------------- +# 该部分包含控制插件加载、重载、注册和发现的核心功能。 + + +async def reload_all_plugins() -> bool: """ - 列出所有当前加载的插件。 + 重新加载所有当前已成功加载的插件。 + + 此操作会遍历所有已加载的插件,逐一进行卸载和重新加载。 + 如果任何一个插件重载失败,整个过程会继续,但最终返回 False。 Returns: - List[str]: 当前加载的插件名称列表。 + bool: 如果所有插件都成功重载,则为 True,否则为 False。 """ - from src.plugin_system.core.plugin_manager import plugin_manager + logger.info("开始重新加载所有插件...") + # 使用 list() 创建一个当前已加载插件列表的副本,以避免在迭代过程中修改原始列表 + loaded_plugins = list(plugin_manager.list_loaded_plugins()) + all_success = True - return plugin_manager.list_loaded_plugins() + # 遍历副本列表中的每个插件进行重载 + for plugin_name in loaded_plugins: + try: + success = await reload_plugin(plugin_name) + if not success: + all_success = False + logger.error(f"重载插件 {plugin_name} 失败。") + except Exception as e: + all_success = False + logger.error(f"重载插件 {plugin_name} 时发生未知异常: {e}", exc_info=True) + + logger.info("所有插件重载完毕。") + return all_success -def list_registered_plugins() -> list[str]: +async def reload_plugin(name: str) -> bool: """ - 列出所有已注册的插件。 + 重新加载指定的单个插件。 - Returns: - List[str]: 已注册的插件名称列表。 - """ - from src.plugin_system.core.plugin_manager import plugin_manager - - return plugin_manager.list_registered_plugins() - - -def get_plugin_path(plugin_name: str) -> str: - """ - 获取指定插件的路径。 + 该函数首先检查插件是否已注册,然后调用插件管理器执行重载操作。 Args: - plugin_name (str): 插件名称。 + name (str): 要重载的插件的名称。 Returns: - str: 插件目录的绝对路径。 + bool: 如果插件成功重载,则为 True。 Raises: - ValueError: 如果插件不存在。 + ValueError: 如果插件未在插件管理器中注册。 """ - from src.plugin_system.core.plugin_manager import plugin_manager + # 验证插件是否存在于注册列表中 + if name not in plugin_manager.list_registered_plugins(): + raise ValueError(f"插件 '{name}' 未注册,无法重载。") + # 调用插件管理器的核心重载方法 + return await plugin_manager.reload_registered_plugin(name) - if plugin_path := plugin_manager.get_plugin_path(plugin_name): - return plugin_path + +def rescan_and_register_plugins(load_after_register: bool = True) -> tuple[int, int]: + """ + 重新扫描所有插件目录,以发现并注册新插件。 + + 此函数会触发插件管理器扫描其配置的所有插件目录。 + 可以选择在注册新发现的插件后立即加载它们。 + + Args: + load_after_register (bool): 如果为 True,新发现的插件将在注册后立即被加载。默认为 True。 + + Returns: + tuple[int, int]: 一个元组,包含 (成功加载的插件数量, 加载失败的插件数量)。 + """ + # 扫描插件目录,获取新注册成功和失败的数量 + success_count, fail_count = plugin_manager.rescan_plugin_directory() + + # 如果不需要在注册后加载,则直接返回扫描结果 + if not load_after_register: + return success_count, fail_count + + # 找出新注册但尚未加载的插件 + newly_registered = [ + p for p in plugin_manager.list_registered_plugins() if p not in plugin_manager.list_loaded_plugins() + ] + + loaded_success_count = 0 + # 尝试加载所有新注册的插件 + for plugin_name in newly_registered: + status, _ = plugin_manager.load_registered_plugin_classes(plugin_name) + if status: + loaded_success_count += 1 + + # 计算总的成功和失败数量 + total_failed = fail_count + (len(newly_registered) - loaded_success_count) + return loaded_success_count, total_failed + + +def register_plugin_from_file(plugin_name: str, load_after_register: bool = True) -> bool: + """ + 从插件目录中查找、注册并选择性地加载一个指定的插件。 + + 如果插件已经加载,此函数将直接返回 True。 + 如果插件未注册,它会遍历所有插件目录以查找匹配的插件文件夹。 + + Args: + plugin_name (str): 插件的名称(通常是其目录名)。 + load_after_register (bool): 注册成功后是否立即加载该插件。默认为 True。 + + Returns: + bool: 如果插件成功注册(并且根据参数成功加载),则为 True。 + """ + # 如果插件已经加载,无需执行任何操作 + if plugin_name in plugin_manager.list_loaded_plugins(): + logger.warning(f"插件 '{plugin_name}' 已经加载,无需重复注册。") + return True + + # 如果插件尚未注册,则开始搜索流程 + if plugin_name not in plugin_manager.list_registered_plugins(): + logger.info(f"插件 '{plugin_name}' 未注册,开始在插件目录中搜索...") + found_path = None + + # 遍历所有配置的插件目录 + for directory in plugin_manager.plugin_directories: + potential_path = os.path.join(directory, plugin_name) + # 检查是否存在与插件同名的目录 + if os.path.isdir(potential_path): + found_path = potential_path + break + + # 如果未找到插件目录,则报告错误 + if not found_path: + logger.error(f"在所有插件目录中都未找到名为 '{plugin_name}' 的插件。") + return False + + # 检查插件的核心 'plugin.py' 文件是否存在 + plugin_file = os.path.join(found_path, "plugin.py") + if not os.path.exists(plugin_file): + logger.error(f"在插件目录 '{found_path}' 中未找到核心的 plugin.py 文件。") + return False + + # 尝试从文件加载插件模块 + module = plugin_manager._load_plugin_module_file(plugin_file) + if not module: + logger.error(f"从文件 '{plugin_file}' 加载插件模块失败。") + return False + + # 验证模块加载后,插件是否已成功注册 + if plugin_name not in plugin_manager.list_registered_plugins(): + logger.error(f"插件 '{plugin_name}' 在加载模块后依然未能成功注册。请检查插件定义。") + return False + + logger.info(f"插件 '{plugin_name}' 已成功发现并注册。") + + # 根据参数决定是否在注册后立即加载插件 + if load_after_register: + logger.info(f"正在加载插件 '{plugin_name}'...") + status, _ = plugin_manager.load_registered_plugin_classes(plugin_name) + return status + + return True + + +# -------------------------------------------------------------------------------- +# Section 2: 组件状态管理 (Component State Management) +# -------------------------------------------------------------------------------- +# 这部分 API 负责控制单个组件的启用和禁用状态,支持全局和局部(临时)范围。 + + +async def set_component_enabled(name: str, component_type: ComponentType, enabled: bool) -> bool: + """ + 在全局范围内启用或禁用一个组件。 + + 此更改会直接修改组件在注册表中的状态,但此状态是临时的,不会持久化到配置文件中。 + 包含一个保护机制,防止禁用最后一个已启用的 Chatter 组件。 + + Args: + name (str): 要操作的组件的名称。 + component_type (ComponentType): 组件的类型。 + enabled (bool): True 表示启用, False 表示禁用。 + + Returns: + bool: 如果操作成功,则为 True。 + """ + # 特殊保护:确保系统中至少有一个 Chatter 组件处于启用状态 + if component_type == ComponentType.CHATTER and not enabled: + enabled_chatters = component_registry.get_enabled_components_by_type(ComponentType.CHATTER) + # 如果当前启用的 Chatter 少于等于1个,并且要禁用的就是它,则阻止操作 + if len(enabled_chatters) <= 1 and name in enabled_chatters: + logger.warning(f"操作被阻止:不能禁用最后一个启用的 Chatter 组件 ('{name}')。") + return False + + # 获取组件信息 + component_info = component_registry.get_component_info(name, component_type) + if not component_info: + logger.error(f"未找到组件 {name} ({component_type.value}),无法更改其状态。") + return False + + # 直接修改组件实例的 enabled 状态 + component_info.enabled = enabled + logger.info(f"组件 {name} ({component_type.value}) 的全局状态已设置为: {enabled}") + return True + + +def set_component_enabled_local(stream_id: str, name: str, component_type: ComponentType, enabled: bool) -> bool: + """ + 在一个特定的 stream_id 上下文中临时启用或禁用组件。 + + 此状态仅存在于内存中,并且只对指定的 stream_id 有效,不影响全局组件状态。 + 同样包含对 Chatter 组件的保护机制。 + + Args: + stream_id (str): 唯一的上下文标识符,例如一个会话ID。 + name (str): 组件名称。 + component_type (ComponentType): 组件类型。 + enabled (bool): True 为启用, False 为禁用。 + + Returns: + bool: 如果操作成功,则为 True。 + """ + # 首先,验证组件是否存在 + component_info = component_registry.get_component_info(name, component_type) + if not component_info: + logger.error(f"尝试设置局部状态失败:未找到组件 {name} ({component_type.value})。") + return False + + # Chatter 唯一性保护(在 stream_id 上下文中) + if component_type == ComponentType.CHATTER and not enabled: + # 检查当前 stream_id 上下文中启用的 Chatter + enabled_chatters = component_registry.get_enabled_components_by_type(ComponentType.CHATTER, stream_id=stream_id) + if len(enabled_chatters) <= 1 and name in enabled_chatters: + logger.warning(f"操作被阻止:在 stream '{stream_id}' 中,不能禁用最后一个启用的 Chatter 组件 ('{name}')。") + return False + + # 设置局部状态 + component_registry.set_local_component_state(stream_id, name, component_type, enabled) + logger.info(f"在 stream '{stream_id}' 中,组件 {name} ({component_type.value}) 的局部状态已设置为: {enabled}") + return True + + +# -------------------------------------------------------------------------------- +# Section 3: 信息查询与报告 (Information Querying & Reporting) +# -------------------------------------------------------------------------------- +# 这部分 API 用于获取关于插件和组件的详细信息、列表和统计数据。 + + +def get_system_report() -> dict[str, Any]: + """ + 生成一份详细的系统状态报告。 + + 报告包含已加载插件、失败插件和组件的全面信息,是调试和监控系统状态的核心工具。 + + Returns: + dict[str, Any]: 包含系统、插件和组件状态的详细报告字典。 + """ + loaded_plugins_info = {} + # 遍历所有已加载的插件实例 + for name, instance in plugin_manager.loaded_plugins.items(): + plugin_info = component_registry.get_plugin_info(name) + if not plugin_info: + continue + + # 收集该插件下所有组件的详细信息 + components_details = [ + { + "name": comp_info.name, + "component_type": comp_info.component_type.value, + "description": comp_info.description, + "enabled": comp_info.enabled, + } + for comp_info in plugin_info.components + ] + + # 构建单个插件的信息字典 + # 元数据从 PluginInfo 获取,而启用状态(enable_plugin)从插件实例获取 + loaded_plugins_info[name] = { + "display_name": plugin_info.display_name or name, + "version": plugin_info.version, + "author": plugin_info.author, + "enabled": instance.enable_plugin, + "components": components_details, + } + + # 构建最终的完整报告 + report = { + "system_info": { + "loaded_plugins_count": len(plugin_manager.loaded_plugins), + "total_components_count": component_registry.get_registry_stats().get("total_components", 0), + }, + "plugins": loaded_plugins_info, + "failed_plugins": plugin_manager.failed_plugins, + } + return report + + +def get_plugin_details(plugin_name: str) -> dict[str, Any] | None: + """ + 获取单个插件的详细报告。 + + 报告内容包括插件的元数据、所有组件的详细信息及其当前状态。 + 这是 `get_system_report` 的单插件聚焦版本。 + + Args: + plugin_name (str): 要查询的插件名称。 + + Returns: + dict | None: 包含插件详细信息的字典,如果插件未注册则返回 None。 + """ + plugin_info = component_registry.get_plugin_info(plugin_name) + if not plugin_info: + logger.warning(f"尝试获取插件详情失败:未找到名为 '{plugin_name}' 的插件。") + return None + + # 收集该插件下所有组件的信息 + components_details = [ + { + "name": comp_info.name, + "component_type": comp_info.component_type.value, + "description": comp_info.description, + "enabled": comp_info.enabled, + } + for comp_info in plugin_info.components + ] + + # 获取插件实例以检查其启用状态 + plugin_instance = plugin_manager.get_plugin_instance(plugin_name) + is_enabled = plugin_instance.enable_plugin if plugin_instance else False + + # 组装详细信息字典 + return { + "name": plugin_info.name, + "display_name": plugin_info.display_name or plugin_info.name, + "version": plugin_info.version, + "author": plugin_info.author, + "license": plugin_info.license, + "description": plugin_info.description, + "enabled": is_enabled, + "status": "loaded" if is_plugin_loaded(plugin_name) else "registered", + "components": components_details, + } + + +def list_plugins(status: Literal["loaded", "registered", "failed"]) -> list[str]: + """ + 根据指定的状态列出插件名称列表。 + + 提供了一种快速、便捷的方式来监控和调试插件系统,而无需解析完整的系统报告。 + + Args: + status (str): 插件状态,可选值为 'loaded', 'registered', 'failed'。 + + Returns: + list[str]: 对应状态的插件名称列表。 + + Raises: + ValueError: 如果传入了无效的状态字符串。 + """ + if status == "loaded": + # 返回所有当前已成功加载的插件 + return plugin_manager.list_loaded_plugins() + if status == "registered": + # 返回所有已注册(但不一定已加载)的插件 + return plugin_manager.list_registered_plugins() + if status == "failed": + # 返回所有加载失败的插件的名称 + return list(plugin_manager.failed_plugins.keys()) + # 如果状态无效,则引发错误 + raise ValueError(f"无效的插件状态: '{status}'。有效选项为 'loaded', 'registered', 'failed'。") + + +def list_components(component_type: ComponentType, enabled_only: bool = True) -> list[dict[str, Any]]: + """ + 列出指定类型的所有组件的详细信息。 + + 这是查找和管理组件的核心功能,例如,获取所有可用的工具或所有注册的聊天器。 + + Args: + component_type (ComponentType): 要查询的组件类型。 + enabled_only (bool, optional): 是否只返回已启用的组件。默认为 True。 + + Returns: + list[dict[str, Any]]: 一个包含组件信息字典的列表。 + """ + # 根据 enabled_only 参数决定是获取所有组件还是仅获取已启用的组件 + if enabled_only: + components = component_registry.get_enabled_components_by_type(component_type) else: - raise ValueError(f"插件 '{plugin_name}' 不存在。") + components = component_registry.get_components_by_type(component_type) + + # 将组件信息格式化为字典列表 + return [ + { + "name": info.name, + "plugin_name": info.plugin_name, + "description": info.description, + "enabled": info.enabled, + } + for info in components.values() + ] -async def remove_plugin(plugin_name: str) -> bool: +def search_components_by_name( + name_keyword: str, + component_type: ComponentType | None = None, + case_sensitive: bool = False, + exact_match: bool = False, +) -> list[dict[str, Any]]: """ - 卸载指定的插件。 + 根据名称关键字搜索组件,支持模糊匹配和精确匹配。 - **此函数是异步的,确保在异步环境中调用。** + 极大地增强了组件的可发现性,用户无需知道完整名称即可找到所需组件。 Args: - plugin_name (str): 要卸载的插件名称。 + name_keyword (str): 用于搜索的名称关键字。 + component_type (ComponentType | None, optional): 如果提供,则只在该类型中搜索。默认为 None (搜索所有类型)。 + case_sensitive (bool, optional): 是否进行大小写敏感的搜索。默认为 False。 + exact_match (bool, optional): 是否进行精确匹配。默认为 False (模糊匹配)。 Returns: - bool: 卸载是否成功。 + list[dict[str, Any]]: 匹配的组件信息字典的列表。 """ - from src.plugin_system.core.plugin_manager import plugin_manager + results = [] + # 如果未指定组件类型,则搜索所有类型 + types_to_search = [component_type] if component_type else list(ComponentType) - return await plugin_manager.remove_registered_plugin(plugin_name) + # 根据是否大小写敏感,预处理搜索关键字 + compare_str = name_keyword if case_sensitive else name_keyword.lower() + + # 遍历要搜索的组件类型 + for comp_type in types_to_search: + all_components = component_registry.get_components_by_type(comp_type) + for name, info in all_components.items(): + # 同样地,预处理组件名称 + target_name = name if case_sensitive else name.lower() + + # 根据 exact_match 参数决定使用精确比较还是模糊包含检查 + is_match = (compare_str == target_name) if exact_match else (compare_str in target_name) + + # 如果匹配,则将组件信息添加到结果列表 + if is_match: + results.append( + { + "name": info.name, + "component_type": info.component_type.value, + "plugin_name": info.plugin_name, + "description": info.description, + "enabled": info.enabled, + } + ) + return results -async def reload_plugin(plugin_name: str) -> bool: +def get_component_info(name: str, component_type: ComponentType) -> ComponentInfo | None: """ - 重新加载指定的插件。 - - **此函数是异步的,确保在异步环境中调用。** + 获取任何一个已注册组件的详细信息对象。 Args: - plugin_name (str): 要重新加载的插件名称。 + name (str): 组件的唯一名称。 + component_type (ComponentType): 组件的类型。 Returns: - bool: 重新加载是否成功。 + ComponentInfo | None: 包含组件完整信息的 ComponentInfo 对象,如果找不到则返回 None。 """ - from src.plugin_system.core.plugin_manager import plugin_manager - - return await plugin_manager.reload_registered_plugin(plugin_name) + return component_registry.get_component_info(name, component_type) -def load_plugin(plugin_name: str) -> tuple[bool, int]: +def get_component_count(component_type: ComponentType, stream_id: str | None = None) -> int: """ - 加载指定的插件。 + 获取指定类型的已加载并启用的组件的总数。 + + 可以根据 `stream_id` 考虑局部状态,从而获得特定上下文中的组件数量。 Args: - plugin_name (str): 要加载的插件名称。 + component_type (ComponentType): 要查询的组件类型。 + stream_id (str | None): 可选的上下文ID。如果提供,将计入局部状态。 Returns: - Tuple[bool, int]: 加载是否成功,成功或失败个数。 + int: 该类型下已启用的组件的数量。 """ - from src.plugin_system.core.plugin_manager import plugin_manager - - return plugin_manager.load_registered_plugin_classes(plugin_name) + return len(component_registry.get_enabled_components_by_type(component_type, stream_id=stream_id)) -def add_plugin_directory(plugin_directory: str) -> bool: +# -------------------------------------------------------------------------------- +# Section 4: 工具函数 (Utility Helpers) +# -------------------------------------------------------------------------------- +# 这部分提供了一些轻量级的辅助函数,用于快速检查状态。 + + +def is_plugin_loaded(plugin_name: str) -> bool: """ - 添加插件目录。 + 快速检查一个插件当前是否已成功加载。 + + 这是一个比 `get_plugin_details` 更轻量级的检查方法,适用于需要快速布尔值判断的场景。 Args: - plugin_directory (str): 要添加的插件目录路径。 + plugin_name (str): 要检查的插件名称。 + Returns: - bool: 添加是否成功。 + bool: 如果插件已加载,则为 True,否则为 False。 """ - from src.plugin_system.core.plugin_manager import plugin_manager - - return plugin_manager.add_plugin_directory(plugin_directory) + return plugin_name in plugin_manager.list_loaded_plugins() -def rescan_plugin_directory() -> tuple[int, int]: +def get_component_plugin(component_name: str, component_type: ComponentType) -> str | None: """ - 重新扫描插件目录,加载新插件。 + 查找一个特定组件属于哪个插件。 + + 在调试或管理组件时,此函数能够方便地追溯其定义的源头。 + + Args: + component_name (str): 组件的名称。 + component_type (ComponentType): 组件的类型。 + Returns: - Tuple[int, int]: 成功加载的插件数量和失败的插件数量。 + str | None: 组件所属的插件名称,如果找不到组件则返回 None。 """ - from src.plugin_system.core.plugin_manager import plugin_manager - - return plugin_manager.rescan_plugin_directory() + component_info = component_registry.get_component_info(component_name, component_type) + return component_info.plugin_name if component_info else None diff --git a/src/plugin_system/apis/tool_api.py b/src/plugin_system/apis/tool_api.py index e59e3dd99..d5a16c326 100644 --- a/src/plugin_system/apis/tool_api.py +++ b/src/plugin_system/apis/tool_api.py @@ -30,7 +30,7 @@ def get_tool_instance(tool_name: str, chat_stream: Any = None) -> BaseTool | Non return tool_class(plugin_config, chat_stream) if tool_class else None -def get_llm_available_tool_definitions() -> list[dict[str, Any]]: +def get_llm_available_tool_definitions(stream_id : str | None) -> list[dict[str, Any]]: """获取LLM可用的工具定义列表(包括 MCP 工具) Returns: @@ -38,7 +38,7 @@ def get_llm_available_tool_definitions() -> list[dict[str, Any]]: """ from src.plugin_system.core import component_registry - llm_available_tools = component_registry.get_llm_available_tools() + llm_available_tools = component_registry.get_llm_available_tools(stream_id) tool_definitions = [] # 获取常规工具定义 diff --git a/src/plugin_system/base/base_action.py b/src/plugin_system/base/base_action.py index bd325fb49..27e877ff5 100644 --- a/src/plugin_system/base/base_action.py +++ b/src/plugin_system/base/base_action.py @@ -109,6 +109,8 @@ class BaseAction(ABC): action_message: 消息数据 **kwargs: 其他参数 """ + if plugin_config is None: + plugin_config = {} self.action_data = action_data self.reasoning = reasoning self.cycle_timers = cycle_timers diff --git a/src/plugin_system/core/component_registry.py b/src/plugin_system/core/component_registry.py index 357b843ed..088cb19da 100644 --- a/src/plugin_system/core/component_registry.py +++ b/src/plugin_system/core/component_registry.py @@ -1,12 +1,16 @@ from __future__ import annotations +# --- 标准库导入 --- import re from pathlib import Path from re import Pattern from typing import Any, cast +# --- 第三方库导入 --- +import toml from fastapi import Depends +# --- 项目内模块导入 --- from src.common.logger import get_logger from src.config.config import global_config as bot_config from src.plugin_system.base.base_action import BaseAction @@ -35,9 +39,11 @@ from src.plugin_system.base.component_types import ( ) from src.plugin_system.base.plus_command import PlusCommand, create_legacy_command_adapter +# --- 日志记录器 --- logger = get_logger("component_registry") -# 统一的组件类类型别名 +# --- 类型别名 --- +# 统一的组件类类型别名,方便类型提示 ComponentClassType = ( type[BaseCommand] | type[BaseAction] @@ -53,63 +59,100 @@ ComponentClassType = ( def _assign_plugin_attrs(cls: Any, plugin_name: str, plugin_config: dict) -> None: - """为组件类动态赋予插件相关属性(避免在各注册函数中重复代码)。""" + """ + 为组件类动态赋予插件相关属性。 + + 这是一个辅助函数,用于避免在各个注册函数中重复编写相同的属性设置代码。 + + Args: + cls (Any): 需要设置属性的组件类。 + plugin_name (str): 插件的名称。 + plugin_config (dict): 插件的配置信息。 + """ setattr(cls, "plugin_name", plugin_name) setattr(cls, "plugin_config", plugin_config) class ComponentRegistry: - """统一的组件注册中心 + """ + 统一的组件注册中心。 - 负责管理所有插件组件的注册、查询和生命周期管理 + 该类是插件系统的核心,负责管理所有插件组件的注册、发现、状态管理和生命周期。 + 它为不同类型的组件(如 Command, Action, Tool 等)提供了统一的注册接口和专门的查询方法。 + + 主要职责: + - 注册和取消注册插件与组件。 + - 按类型和名称存储和索引所有组件。 + - 管理组件的全局启用/禁用状态。 + - 支持会话级别的组件局部(临时)启用/禁用状态。 + - 提供按类型、名称或状态查询组件的方法。 + - 处理组件之间的依赖和冲突。 + - 动态加载和管理 MCP (Model-Copilot-Plugin) 工具。 """ def __init__(self): - # 命名空间式组件名构成法 f"{component_type}.{component_name}" + """初始化组件注册中心,创建所有必要的注册表。""" + # --- 通用注册表 --- + # 核心注册表,存储所有组件的信息,使用命名空间式键 f"{component_type}.{component_name}" self._components: dict[str, "ComponentInfo"] = {} - """组件注册表 命名空间式组件名 -> 组件信息""" + # 按类型分类的组件注册表,方便按类型快速查找 self._components_by_type: dict["ComponentType", dict[str, "ComponentInfo"]] = { types: {} for types in ComponentType } - """类型 -> 组件原名称 -> 组件信息""" - # 组件类注册表(命名空间式组件名 -> 组件类) + # 存储组件类本身,用于实例化 self._components_classes: dict[str, ComponentClassType] = {} - """命名空间式组件名 -> 组件类""" - # 插件注册表 + # --- 插件注册表 --- self._plugins: dict[str, "PluginInfo"] = {} - """插件名 -> 插件信息""" - # Action特定注册表 + # --- 特定类型组件的专用注册表 --- + + # Action self._action_registry: dict[str, type["BaseAction"]] = {} - """Action注册表 action名 -> action类""" - self._default_actions: dict[str, "ActionInfo"] = {} - """默认动作集,即启用的Action集,用于重置ActionManager状态""" + self._default_actions: dict[str, "ActionInfo"] = {} # 存储全局启用的Action - # Command特定注册表 + # Command (旧版) self._command_registry: dict[str, type["BaseCommand"]] = {} - """Command类注册表 command名 -> command类""" - self._command_patterns: dict[Pattern, str] = {} - """编译后的正则 -> command名""" + self._command_patterns: dict[Pattern, str] = {} # 编译后的正则表达式 -> command名 - # 工具特定注册表 - self._tool_registry: dict[str, type["BaseTool"]] = {} # 工具名 -> 工具类 - self._llm_available_tools: dict[str, type["BaseTool"]] = {} # llm可用的工具名 -> 工具类 + # PlusCommand (新版) + self._plus_command_registry: dict[str, type[PlusCommand]] = {} - # MCP 工具注册表(运行时动态加载) - self._mcp_tools: list[Any] = [] # MCP 工具适配器实例列表 - self._mcp_tools_loaded = False # MCP 工具是否已加载 + # Tool + self._tool_registry: dict[str, type["BaseTool"]] = {} + self._llm_available_tools: dict[str, type["BaseTool"]] = {} # 存储全局启用的Tool - # EventHandler特定注册表 + # EventHandler self._event_handler_registry: dict[str, type["BaseEventHandler"]] = {} - """event_handler名 -> event_handler类""" self._enabled_event_handlers: dict[str, type["BaseEventHandler"]] = {} - """启用的事件处理器 event_handler名 -> event_handler类""" + # Chatter self._chatter_registry: dict[str, type["BaseChatter"]] = {} - """chatter名 -> chatter类""" self._enabled_chatter_registry: dict[str, type["BaseChatter"]] = {} - """启用的chatter名 -> chatter类""" + + # InterestCalculator + self._interest_calculator_registry: dict[str, type["BaseInterestCalculator"]] = {} + self._enabled_interest_calculator_registry: dict[str, type["BaseInterestCalculator"]] = {} + + # Prompt + self._prompt_registry: dict[str, type[BasePrompt]] = {} + self._enabled_prompt_registry: dict[str, type[BasePrompt]] = {} + + # MCP (Model-Copilot-Plugin) Tools + self._mcp_tools: list[Any] = [] # 存储 MCP 工具适配器实例 + self._mcp_tools_loaded = False # 标记 MCP 工具是否已加载 + + # --- 状态管理 --- + # 局部组件状态管理器,用于在特定会话中临时覆盖全局状态 + self._local_component_states: dict[str, dict[tuple[str, ComponentType], bool]] = {} + # 定义不支持局部状态管理的组件类型集合 + self._no_local_state_types: set[ComponentType] = { + ComponentType.ROUTER, + ComponentType.EVENT_HANDLER, + ComponentType.PROMPT, + # 根据设计,COMMAND 和 PLUS_COMMAND 也不应支持局部状态 + } + logger.info("组件注册中心初始化完成") self._interest_calculator_registry: dict[str, type["BaseInterestCalculator"]] = {} @@ -130,13 +173,14 @@ class ComponentRegistry: # == 注册方法 == def register_plugin(self, plugin_info: PluginInfo) -> bool: - """注册插件 + """ + 注册一个插件。 Args: - plugin_info: 插件信息 + plugin_info (PluginInfo): 包含插件元数据的信息对象。 Returns: - bool: 是否注册成功 + bool: 如果插件是新的并成功注册,则返回 True;如果插件已存在,则返回 False。 """ plugin_name = plugin_info.name @@ -151,20 +195,25 @@ class ComponentRegistry: def register_component( self, self_component_info: ComponentInfo, component_class: ComponentClassType ) -> bool: - """注册组件 + """ + 注册一个组件。 + + 这是所有组件注册的统一入口点。它会验证组件信息,然后根据组件类型分发到 + 特定的内部注册方法。 Args: - component_info (ComponentInfo): 组件信息 - component_class (Type[Union[BaseCommand, BaseAction, BaseEventHandler]]): 组件类 + component_info (ComponentInfo): 组件的元数据信息。 + component_class (ComponentClassType): 组件的类定义。 Returns: - bool: 是否注册成功 + bool: 注册成功返回 True,否则返回 False。 """ - component_info = self_component_info # 局部别名 + component_info = self_component_info # 创建局部别名以缩短行长 component_name = component_info.name component_type = component_info.component_type plugin_name = getattr(component_info, "plugin_name", "unknown") + # --- 名称合法性检查 --- if "." in component_name: logger.error(f"组件名称 '{component_name}' 包含非法字符 '.',请使用下划线替代") return False @@ -172,55 +221,53 @@ class ComponentRegistry: logger.error(f"插件名称 '{plugin_name}' 包含非法字符 '.',请使用下划线替代") return False + # --- 冲突检查 --- namespaced_name = f"{component_type.value}.{component_name}" if namespaced_name in self._components: existing_info = self._components[namespaced_name] existing_plugin = getattr(existing_info, "plugin_name", "unknown") logger.warning( - f"组件名冲突: '{plugin_name}' 插件的 {component_type} 类型组件 '{component_name}' 已被插件 '{existing_plugin}' 注册,跳过此组件注册" + f"组件名冲突: '{plugin_name}' 插件的 {component_type} 类型组件 '{component_name}' " + f"已被插件 '{existing_plugin}' 注册,跳过此组件注册" ) return False + # --- 通用注册 --- self._components[namespaced_name] = component_info self._components_by_type[component_type][component_name] = component_info self._components_classes[namespaced_name] = component_class + # --- 按类型分发到特定注册方法 --- + ret = False # 初始化返回值为 False match component_type: case ComponentType.ACTION: - assert isinstance(component_info, ActionInfo) - assert issubclass(component_class, BaseAction) + assert isinstance(component_info, ActionInfo) and issubclass(component_class, BaseAction) ret = self._register_action_component(component_info, component_class) case ComponentType.COMMAND: - assert isinstance(component_info, CommandInfo) - assert issubclass(component_class, BaseCommand) + assert isinstance(component_info, CommandInfo) and issubclass(component_class, BaseCommand) ret = self._register_command_component(component_info, component_class) case ComponentType.PLUS_COMMAND: - assert isinstance(component_info, PlusCommandInfo) - assert issubclass(component_class, PlusCommand) + assert isinstance(component_info, PlusCommandInfo) and issubclass(component_class, PlusCommand) ret = self._register_plus_command_component(component_info, component_class) case ComponentType.TOOL: - assert isinstance(component_info, ToolInfo) - assert issubclass(component_class, BaseTool) + assert isinstance(component_info, ToolInfo) and issubclass(component_class, BaseTool) ret = self._register_tool_component(component_info, component_class) case ComponentType.EVENT_HANDLER: - assert isinstance(component_info, EventHandlerInfo) - assert issubclass(component_class, BaseEventHandler) + assert isinstance(component_info, EventHandlerInfo) and issubclass(component_class, BaseEventHandler) ret = self._register_event_handler_component(component_info, component_class) case ComponentType.CHATTER: - assert isinstance(component_info, ChatterInfo) - assert issubclass(component_class, BaseChatter) + assert isinstance(component_info, ChatterInfo) and issubclass(component_class, BaseChatter) ret = self._register_chatter_component(component_info, component_class) case ComponentType.INTEREST_CALCULATOR: - assert isinstance(component_info, InterestCalculatorInfo) - assert issubclass(component_class, BaseInterestCalculator) + assert isinstance(component_info, InterestCalculatorInfo) and issubclass( + component_class, BaseInterestCalculator + ) ret = self._register_interest_calculator_component(component_info, component_class) case ComponentType.PROMPT: - assert isinstance(component_info, PromptInfo) - assert issubclass(component_class, BasePrompt) + assert isinstance(component_info, PromptInfo) and issubclass(component_class, BasePrompt) ret = self._register_prompt_component(component_info, component_class) case ComponentType.ROUTER: - assert isinstance(component_info, RouterInfo) - assert issubclass(component_class, BaseRouterComponent) + assert isinstance(component_info, RouterInfo) and issubclass(component_class, BaseRouterComponent) ret = self._register_router_component(component_info, component_class) case ComponentType.ADAPTER: assert isinstance(component_info, AdapterInfo) @@ -525,28 +572,12 @@ class ComponentRegistry: self._components.pop(namespaced_name, None) self._components_by_type[component_type].pop(component_name, None) self._components_classes.pop(namespaced_name, None) - - logger.info(f"组件 {component_name} ({component_type}) 已完全移除") - return True - - except Exception as e: - logger.error(f"移除组件 {component_name} ({component_type}) 时发生错误: {e}") return False - def remove_plugin_registry(self, plugin_name: str) -> bool: - """移除插件注册信息 - - Args: - plugin_name: 插件名称 - - Returns: - bool: 是否成功移除 - """ - if plugin_name not in self._plugins: - logger.warning(f"插件 {plugin_name} 未注册,无法移除") - return False - del self._plugins[plugin_name] - logger.info(f"插件 {plugin_name} 已移除") + logger.debug( + f"已注册{component_type}组件: '{component_name}' -> '{namespaced_name}' " + f"({component_class.__name__}) [插件: {plugin_name}]" + ) return True # === 组件全局启用/禁用方法 === @@ -1026,13 +1057,16 @@ class ComponentRegistry: # === 组件移除相关 === async def unregister_plugin(self, plugin_name: str) -> bool: - """卸载插件及其所有组件 + """ + 卸载一个插件及其所有关联的组件。 + + 这是一个高级操作,会依次移除插件的所有组件,然后移除插件本身的注册信息。 Args: - plugin_name: 插件名称 + plugin_name (str): 要卸载的插件的名称。 Returns: - bool: 是否成功卸载 + bool: 如果所有组件和插件本身都成功卸载,则返回 True;否则返回 False。 """ plugin_info = self.get_plugin_info(plugin_name) if not plugin_info: @@ -1041,9 +1075,7 @@ class ComponentRegistry: logger.info(f"开始卸载插件: {plugin_name}") - # 记录卸载失败的组件 failed_components = [] - # 逐个移除插件的所有组件 for component_info in plugin_info.components: try: @@ -1058,19 +1090,710 @@ class ComponentRegistry: logger.error(f"移除组件 {component_info.name} 时发生异常: {e}") failed_components.append(f"{component_info.component_type}.{component_info.name}") - # 移除插件注册信息 - plugin_removed = self.remove_plugin_registry(plugin_name) + # 移除插件的注册信息 + plugin_removed = self._remove_plugin_registry(plugin_name) if failed_components: logger.warning(f"插件 {plugin_name} 部分组件卸载失败: {failed_components}") return False - elif not plugin_removed: + if not plugin_removed: logger.error(f"插件 {plugin_name} 注册信息移除失败") return False - else: - logger.info(f"插件 {plugin_name} 卸载成功") + + logger.info(f"插件 {plugin_name} 卸载成功") + return True + + async def remove_component(self, component_name: str, component_type: ComponentType, plugin_name: str) -> bool: + """ + 从注册中心移除一个指定的组件。 + + Args: + component_name (str): 要移除的组件的名称。 + component_type (ComponentType): 组件的类型。 + plugin_name (str): 组件所属的插件名称 (用于日志和验证)。 + + Returns: + bool: 移除成功返回 True,否则返回 False。 + """ + target_component_class = self.get_component_class(component_name, component_type) + if not target_component_class: + logger.warning(f"组件 {component_name} ({component_type.value}) 未注册,无法移除") + return False + + try: + # --- 特定类型的清理操作 --- + match component_type: + case ComponentType.ACTION: + self._action_registry.pop(component_name, None) + self._default_actions.pop(component_name, None) + case ComponentType.COMMAND: + self._command_registry.pop(component_name, None) + keys_to_remove = [k for k, v in self._command_patterns.items() if v == component_name] + for key in keys_to_remove: + self._command_patterns.pop(key, None) + case ComponentType.PLUS_COMMAND: + self._plus_command_registry.pop(component_name, None) + case ComponentType.TOOL: + self._tool_registry.pop(component_name, None) + self._llm_available_tools.pop(component_name, None) + case ComponentType.EVENT_HANDLER: + from .event_manager import event_manager # 延迟导入 + event_manager.remove_event_handler(component_name) + case ComponentType.CHATTER: + self._chatter_registry.pop(component_name, None) + self._enabled_chatter_registry.pop(component_name, None) + case ComponentType.INTEREST_CALCULATOR: + self._interest_calculator_registry.pop(component_name, None) + self._enabled_interest_calculator_registry.pop(component_name, None) + case ComponentType.PROMPT: + self._prompt_registry.pop(component_name, None) + self._enabled_prompt_registry.pop(component_name, None) + case ComponentType.ROUTER: + logger.warning(f"Router组件 '{component_name}' 的HTTP端点无法在运行时动态移除,将在下次重启后生效。") + case _: + logger.warning(f"未知的组件类型: {component_type},无法进行特定的清理操作") + return False + + # --- 通用注册信息的清理 --- + namespaced_name = f"{component_type.value}.{component_name}" + self._components.pop(namespaced_name, None) + self._components_by_type[component_type].pop(component_name, None) + self._components_classes.pop(namespaced_name, None) + + logger.info(f"组件 {component_name} ({component_type.value}) 已从插件 '{plugin_name}' 中完全移除") return True + except Exception as e: + logger.error(f"移除组件 {component_name} ({component_type.value}) 时发生错误: {e}", exc_info=True) + return False -# 创建全局组件注册中心实例 + # ================================================================= + # == 内部注册辅助方法 (_register_* Methods) + # ================================================================= + + def _register_action_component(self, action_info: ActionInfo, action_class: type[BaseAction]) -> bool: + """注册Action组件到Action特定注册表。""" + action_name = action_info.name + _assign_plugin_attrs(action_class, action_info.plugin_name, self.get_plugin_config(action_info.plugin_name) or {}) + self._action_registry[action_name] = action_class + if action_info.enabled: + self._default_actions[action_name] = action_info + return True + + def _register_command_component(self, command_info: CommandInfo, command_class: type[BaseCommand]) -> bool: + """通过适配器将旧版Command注册为PlusCommand。""" + logger.warning( + f"检测到旧版Command组件 '{command_info.name}' (来自插件: {command_info.plugin_name})。" + "它将通过兼容层运行,但建议尽快迁移到PlusCommand以获得更好的性能和功能。" + ) + # 使用适配器将其转换为PlusCommand + adapted_class = create_legacy_command_adapter(command_class) + plus_command_info = adapted_class.get_plus_command_info() + plus_command_info.plugin_name = command_info.plugin_name # 继承插件名 + + return self._register_plus_command_component(plus_command_info, adapted_class) + + def _register_plus_command_component( + self, plus_command_info: PlusCommandInfo, plus_command_class: type[PlusCommand] + ) -> bool: + """注册PlusCommand组件到特定注册表。""" + plus_command_name = plus_command_info.name + _assign_plugin_attrs( + plus_command_class, + plus_command_info.plugin_name, + self.get_plugin_config(plus_command_info.plugin_name) or {}, + ) + self._plus_command_registry[plus_command_name] = plus_command_class + logger.debug(f"已注册PlusCommand组件: {plus_command_name}") + return True + + def _register_tool_component(self, tool_info: ToolInfo, tool_class: type[BaseTool]) -> bool: + """注册Tool组件到Tool特定注册表。""" + tool_name = tool_info.name + _assign_plugin_attrs(tool_class, tool_info.plugin_name, self.get_plugin_config(tool_info.plugin_name) or {}) + self._tool_registry[tool_name] = tool_class + if tool_info.enabled: + self._llm_available_tools[tool_name] = tool_class + return True + + def _register_event_handler_component( + self, handler_info: EventHandlerInfo, handler_class: type[BaseEventHandler] + ) -> bool: + """注册EventHandler组件并订阅事件。""" + handler_name = handler_info.name + _assign_plugin_attrs( + handler_class, handler_info.plugin_name, self.get_plugin_config(handler_info.plugin_name) or {} + ) + self._event_handler_registry[handler_name] = handler_class + if not handler_info.enabled: + logger.warning(f"EventHandler组件 {handler_name} 未启用,仅注册信息,不订阅事件") + return True # 未启用但注册成功 + + # 延迟导入以避免循环依赖 + from src.plugin_system.core.event_manager import event_manager + return event_manager.register_event_handler( + handler_class, self.get_plugin_config(handler_info.plugin_name) or {} + ) + + def _register_chatter_component(self, chatter_info: ChatterInfo, chatter_class: type[BaseChatter]) -> bool: + """注册Chatter组件到Chatter特定注册表。""" + chatter_name = chatter_info.name + _assign_plugin_attrs( + chatter_class, chatter_info.plugin_name, self.get_plugin_config(chatter_info.plugin_name) or {} + ) + self._chatter_registry[chatter_name] = chatter_class + if chatter_info.enabled: + self._enabled_chatter_registry[chatter_name] = chatter_class + logger.debug(f"已注册Chatter组件: {chatter_name}") + return True + + def _register_interest_calculator_component( + self, + interest_calculator_info: "InterestCalculatorInfo", + interest_calculator_class: type["BaseInterestCalculator"], + ) -> bool: + """注册InterestCalculator组件到特定注册表。""" + calculator_name = interest_calculator_info.name + _assign_plugin_attrs( + interest_calculator_class, + interest_calculator_info.plugin_name, + self.get_plugin_config(interest_calculator_info.plugin_name) or {}, + ) + self._interest_calculator_registry[calculator_name] = interest_calculator_class + if interest_calculator_info.enabled: + self._enabled_interest_calculator_registry[calculator_name] = interest_calculator_class + logger.debug(f"已注册InterestCalculator组件: {calculator_name}") + return True + + def _register_prompt_component(self, prompt_info: PromptInfo, prompt_class: type[BasePrompt]) -> bool: + """注册Prompt组件到Prompt特定注册表。""" + prompt_name = prompt_info.name + _assign_plugin_attrs( + prompt_class, prompt_info.plugin_name, self.get_plugin_config(prompt_info.plugin_name) or {} + ) + self._prompt_registry[prompt_name] = prompt_class + if prompt_info.enabled: + self._enabled_prompt_registry[prompt_name] = prompt_class + logger.debug(f"已注册Prompt组件: {prompt_name}") + return True + + def _register_router_component(self, router_info: RouterInfo, router_class: type[BaseRouterComponent]) -> bool: + """注册Router组件并将其HTTP端点挂载到主FastAPI应用。""" + if not bot_config.plugin_http_system.enable_plugin_http_endpoints: + logger.info("插件HTTP端点功能已禁用,跳过路由注册") + return True + + try: + from src.common.server import get_global_server + + router_name = router_info.name + plugin_name = router_info.plugin_name + + # 实例化组件以获取其配置好的APIRouter实例 + component_instance = router_class() + plugin_router = component_instance.router + + # 获取全局FastAPI应用实例 + server = get_global_server() + + # 生成唯一的URL前缀,格式为 /plugins/{plugin_name} + prefix = f"/plugins/{plugin_name}" + + # 将插件的路由包含到主应用中 + server.app.include_router(plugin_router, prefix=prefix, tags=[plugin_name]) + + logger.debug(f"成功将插件 '{plugin_name}' 的路由组件 '{router_name}' 挂载到: {prefix}") + return True + + except Exception as e: + logger.error(f"注册路由组件 '{router_info.name}' 时出错: {e}", exc_info=True) + return False + + def _remove_plugin_registry(self, plugin_name: str) -> bool: + """ + (内部方法) 仅移除插件的注册信息。 + + Args: + plugin_name (str): 插件名称。 + + Returns: + bool: 是否成功移除。 + """ + if plugin_name not in self._plugins: + logger.warning(f"插件 {plugin_name} 未注册,无法移除其注册信息") + return False + del self._plugins[plugin_name] + logger.info(f"插件 {plugin_name} 的注册信息已移除") + return True + + # ================================================================= + # == 组件状态管理 (Component State Management) + # ================================================================= + + def enable_component(self, component_name: str, component_type: ComponentType) -> bool: + """ + 全局启用一个组件。 + + Args: + component_name (str): 组件名称。 + component_type (ComponentType): 组件类型。 + + Returns: + bool: 启用成功返回 True,失败返回 False。 + """ + target_component_class = self.get_component_class(component_name, component_type) + target_component_info = self.get_component_info(component_name, component_type) + if not target_component_class or not target_component_info: + logger.warning(f"组件 {component_name} ({component_type.value}) 未注册,无法启用") + return False + + target_component_info.enabled = True + # 更新通用注册表中的状态 + namespaced_name = f"{component_type.value}.{component_name}" + self._components[namespaced_name].enabled = True + self._components_by_type[component_type][component_name].enabled = True + + # 更新特定类型的启用列表 + match component_type: + case ComponentType.ACTION: + assert isinstance(target_component_info, ActionInfo) + self._default_actions[component_name] = target_component_info + case ComponentType.COMMAND: + # 旧版Command通过PlusCommand启用,这里无需操作 + pass + case ComponentType.TOOL: + assert issubclass(target_component_class, BaseTool) + self._llm_available_tools[component_name] = target_component_class + case ComponentType.EVENT_HANDLER: + assert issubclass(target_component_class, BaseEventHandler) + self._enabled_event_handlers[component_name] = target_component_class + from .event_manager import event_manager + cfg = self.get_plugin_config(target_component_info.plugin_name) or {} + event_manager.register_event_handler(target_component_class, cfg) + case ComponentType.CHATTER: + assert issubclass(target_component_class, BaseChatter) + self._enabled_chatter_registry[component_name] = target_component_class + case ComponentType.INTEREST_CALCULATOR: + assert issubclass(target_component_class, BaseInterestCalculator) + self._enabled_interest_calculator_registry[component_name] = target_component_class + case ComponentType.PROMPT: + assert issubclass(target_component_class, BasePrompt) + self._enabled_prompt_registry[component_name] = target_component_class + + logger.info(f"组件 {component_name} ({component_type.value}) 已全局启用") + return True + + async def disable_component(self, component_name: str, component_type: ComponentType) -> bool: + """ + 全局禁用一个组件。 + + Args: + component_name (str): 组件名称。 + component_type (ComponentType): 组件类型。 + + Returns: + bool: 禁用成功返回 True,失败返回 False。 + """ + target_component_info = self.get_component_info(component_name, component_type) + if not target_component_info: + logger.warning(f"组件 {component_name} ({component_type.value}) 未注册,无法禁用") + return False + + target_component_info.enabled = False + # 更新通用注册表中的状态 + namespaced_name = f"{component_type.value}.{component_name}" + if namespaced_name in self._components: + self._components[namespaced_name].enabled = False + if component_name in self._components_by_type[component_type]: + self._components_by_type[component_type][component_name].enabled = False + + try: + # 从特定类型的启用列表中移除 + match component_type: + case ComponentType.ACTION: + self._default_actions.pop(component_name, None) + case ComponentType.COMMAND: + # 旧版Command通过PlusCommand禁用,这里无需操作 + pass + case ComponentType.TOOL: + self._llm_available_tools.pop(component_name, None) + case ComponentType.EVENT_HANDLER: + self._enabled_event_handlers.pop(component_name, None) + from .event_manager import event_manager + # 从事件管理器中取消订阅 + event_manager.remove_event_handler(component_name) + case ComponentType.CHATTER: + self._enabled_chatter_registry.pop(component_name, None) + case ComponentType.INTEREST_CALCULATOR: + self._enabled_interest_calculator_registry.pop(component_name, None) + case ComponentType.PROMPT: + self._enabled_prompt_registry.pop(component_name, None) + + logger.info(f"组件 {component_name} ({component_type.value}) 已全局禁用") + return True + except Exception as e: + logger.error(f"禁用组件 {component_name} ({component_type.value}) 时发生错误: {e}", exc_info=True) + # 即使出错,也尝试将状态标记为禁用 + return False + + # ================================================================= + # == 查询方法 (Query Methods) + # ================================================================= + + def get_component_info( + self, component_name: str, component_type: ComponentType | None = None + ) -> ComponentInfo | None: + """ + 获取组件信息,支持自动命名空间解析。 + + 如果只提供 `component_name`,它会尝试在所有类型中查找。如果找到多个同名但不同类型的 + 组件,会发出警告并返回第一个找到的。 + + Args: + component_name (str): 组件名称,可以是原始名称或命名空间化的名称 (如 "action.my_action")。 + component_type (ComponentType, optional): 组件类型。如果提供,将只在该类型中查找。 + + Returns: + ComponentInfo | None: 找到的组件信息对象,或 None。 + """ + # 1. 如果已经是命名空间化的名称,直接查找 + if "." in component_name: + return self._components.get(component_name) + + # 2. 如果指定了组件类型,构造命名空间化的名称查找 + if component_type: + namespaced_name = f"{component_type.value}.{component_name}" + return self._components.get(namespaced_name) + + # 3. 如果没有指定类型,遍历所有类型查找 + candidates = [] + for c_type in ComponentType: + namespaced_name = f"{c_type.value}.{component_name}" + if component_info := self._components.get(namespaced_name): + candidates.append(component_info) + + if len(candidates) == 1: + return candidates[0] + if len(candidates) > 1: + types_found = [info.component_type.value for info in candidates] + logger.warning( + f"组件名称 '{component_name}' 在多个类型中存在: {types_found}。" + f"返回第一个匹配项 (类型: {candidates[0].component_type.value})。请使用 component_type 参数指定类型以消除歧义。" + ) + return candidates[0] + + # 4. 都没找到 + return None + + def get_component_class( + self, component_name: str, component_type: ComponentType | None = None + ) -> ComponentClassType | None: + """ + 获取组件的类定义,支持自动命名空间解析。 + + 逻辑与 `get_component_info` 类似。 + + Args: + component_name (str): 组件名称。 + component_type (ComponentType, optional): 组件类型。 + + Returns: + ComponentClassType | None: 找到的组件类,或 None。 + """ + # 1. 如果已经是命名空间化的名称,直接查找 + if "." in component_name: + return self._components_classes.get(component_name) + + # 2. 如果指定了组件类型,构造命名空间化的名称查找 + if component_type: + namespaced_name = f"{component_type.value}.{component_name}" + return self._components_classes.get(namespaced_name) + + # 3. 如果没有指定类型,遍历所有类型查找 + info = self.get_component_info(component_name) # 复用 get_component_info 的查找和消歧义逻辑 + if info: + namespaced_name = f"{info.component_type.value}.{info.name}" + return self._components_classes.get(namespaced_name) + + # 4. 都没找到 + return None + + def get_components_by_type(self, component_type: ComponentType) -> dict[str, ComponentInfo]: + """获取指定类型的所有已注册组件(无论是否启用)。""" + return self._components_by_type.get(component_type, {}).copy() + + def get_enabled_components_by_type( + self, component_type: ComponentType, stream_id: str | None = None + ) -> dict[str, ComponentInfo]: + """ + 获取指定类型的所有可用组件。 + + 这会同时考虑全局启用状态和 `stream_id` 对应的局部状态。 + + Args: + component_type (ComponentType): 要查询的组件类型。 + stream_id (str, optional): 会话ID,用于检查局部状态覆盖。 + + Returns: + dict[str, ComponentInfo]: 一个包含可用组件名称和信息的字典。 + """ + all_components = self.get_components_by_type(component_type) + return { + name: info + for name, info in all_components.items() + if self.is_component_available(name, component_type, stream_id) + } + + # ================================================================= + # == 特定类型查询方法 (Type-Specific Query Methods) + # ================================================================= + + # --- Action --- + def get_action_registry(self) -> dict[str, type[BaseAction]]: + """获取所有已注册的Action类。""" + return self._action_registry.copy() + + def get_default_actions(self, stream_id: str | None = None) -> dict[str, ActionInfo]: + """获取所有可用的Action信息(考虑全局和局部状态)。""" + return cast( + dict[str, ActionInfo], + self.get_enabled_components_by_type(ComponentType.ACTION, stream_id), + ) + + # --- PlusCommand --- + def get_plus_command_registry(self) -> dict[str, type[PlusCommand]]: + """获取所有已注册的PlusCommand类。""" + return self._plus_command_registry.copy() + + def get_available_plus_commands_info(self, stream_id: str | None = None) -> dict[str, PlusCommandInfo]: + """获取所有可用的PlusCommand信息(考虑全局和局部状态)。""" + return cast( + dict[str, PlusCommandInfo], + self.get_enabled_components_by_type(ComponentType.PLUS_COMMAND, stream_id), + ) + + # --- Tool --- + def get_tool_registry(self) -> dict[str, type[BaseTool]]: + """获取所有已注册的Tool类。""" + return self._tool_registry.copy() + + def get_llm_available_tools(self, stream_id: str | None = None) -> dict[str, type[BaseTool]]: + """获取所有对LLM可用的Tool类(考虑全局和局部状态)。""" + all_tools = self.get_tool_registry() + available_tools = {} + for name, tool_class in all_tools.items(): + if self.is_component_available(name, ComponentType.TOOL, stream_id): + available_tools[name] = tool_class + return available_tools + + # --- EventHandler --- + def get_event_handler_registry(self) -> dict[str, type[BaseEventHandler]]: + """获取所有已注册的EventHandler类。""" + return self._event_handler_registry.copy() + + def get_enabled_event_handlers(self) -> dict[str, type[BaseEventHandler]]: + """获取所有已启用的EventHandler类。""" + return self._enabled_event_handlers.copy() + + # --- Chatter --- + def get_chatter_registry(self) -> dict[str, type[BaseChatter]]: + """获取所有已注册的Chatter类。""" + return self._chatter_registry.copy() + + def get_enabled_chatter_registry(self, stream_id: str | None = None) -> dict[str, type[BaseChatter]]: + """获取所有可用的Chatter类(考虑全局和局部状态)。""" + all_chatters = self.get_chatter_registry() + available_chatters = {} + for name, chatter_class in all_chatters.items(): + if self.is_component_available(name, ComponentType.CHATTER, stream_id): + available_chatters[name] = chatter_class + return available_chatters + + # --- Prompt --- + def get_prompt_registry(self) -> dict[str, type[BasePrompt]]: + """获取所有已注册的Prompt类。""" + return self._prompt_registry.copy() + + def get_enabled_prompt_registry(self) -> dict[str, type[BasePrompt]]: + """获取所有已启用的Prompt类。""" + return self._enabled_prompt_registry.copy() + + # --- 插件 --- + def get_plugin_info(self, plugin_name: str) -> PluginInfo | None: + """获取指定插件的信息。""" + return self._plugins.get(plugin_name) + + def get_all_plugins(self) -> dict[str, PluginInfo]: + """获取所有已注册的插件。""" + return self._plugins.copy() + + def get_plugin_components(self, plugin_name: str) -> list["ComponentInfo"]: + """获取指定插件下的所有组件信息。""" + plugin_info = self.get_plugin_info(plugin_name) + return plugin_info.components if plugin_info else [] + + def get_plugin_config(self, plugin_name: str) -> dict | None: + """ + 获取插件的配置信息。 + + 它会首先尝试从已加载的插件实例中获取,如果失败,则尝试从文件系统读取。 + + Args: + plugin_name (str): 插件名称。 + + Returns: + dict | None: 插件的配置字典,如果找不到则返回 None。 + """ + # 延迟导入以避免循环依赖 + from src.plugin_system.core.plugin_manager import plugin_manager + + plugin_instance = plugin_manager.get_plugin_instance(plugin_name) + if plugin_instance and plugin_instance.config: + return plugin_instance.config + + # 如果插件实例不存在,尝试从配置文件读取 + try: + config_path = Path("config") / "plugins" / plugin_name / "config.toml" + if config_path.exists(): + with open(config_path, encoding="utf-8") as f: + config_data = toml.load(f) + logger.debug(f"从配置文件延迟加载了插件 {plugin_name} 的配置") + return config_data + except Exception as e: + logger.debug(f"读取插件 {plugin_name} 配置文件失败: {e}") + + return None + + # ================================================================= + # == 局部状态管理 (Local State Management) + # ================================================================= + + def set_local_component_state( + self, stream_id: str, component_name: str, component_type: ComponentType, enabled: bool + ) -> bool: + """ + 为指定的会话(stream_id)设置组件的局部(临时)状态。 + + 这允许在单个对话流中动态启用或禁用组件,而不影响全局设置。 + + Args: + stream_id (str): 唯一的会话ID。 + component_name (str): 组件名称。 + component_type (ComponentType): 组件类型。 + enabled (bool): True 表示启用,False 表示禁用。 + + Returns: + bool: 设置成功返回 True,如果组件类型不支持局部状态则返回 False。 + """ + if component_type in self._no_local_state_types: + logger.warning( + f"组件类型 {component_type.value} 不支持局部状态管理。" + f"尝试为 '{component_name}' 设置局部状态的操作将被忽略。" + ) + return False + + if stream_id not in self._local_component_states: + self._local_component_states[stream_id] = {} + + state_key = (component_name, component_type) + self._local_component_states[stream_id][state_key] = enabled + logger.debug( + f"已为 stream '{stream_id}' 设置局部状态: {component_name} ({component_type.value}) -> {'启用' if enabled else '禁用'}" + ) + return True + + def is_component_available( + self, component_name: str, component_type: ComponentType, stream_id: str | None = None + ) -> bool: + """ + 检查一个组件在给定上下文中是否可用。 + + 检查顺序: + 1. 组件是否存在。 + 2. (如果提供了 stream_id) 是否有局部状态覆盖。 + 3. 全局启用状态。 + + Args: + component_name (str): 组件名称。 + component_type (ComponentType): 组件类型。 + stream_id (str, optional): 会话ID。 + + Returns: + bool: 如果组件可用,则返回 True。 + """ + component_info = self.get_component_info(component_name, component_type) + + # 1. 检查组件是否存在 + if not component_info: + return False + + # 2. 如果组件类型不支持局部状态,直接返回其全局状态 + if component_type in self._no_local_state_types: + return component_info.enabled + + # 3. 如果提供了 stream_id,检查是否存在局部状态覆盖 + if stream_id and stream_id in self._local_component_states: + state_key = (component_name, component_type) + local_state = self._local_component_states[stream_id].get(state_key) + if local_state is not None: + return local_state # 局部状态存在,直接返回 + + # 4. 如果没有局部状态覆盖,返回全局状态 + return component_info.enabled + + # ================================================================= + # == MCP 工具相关方法 (MCP Tool Methods) + # ================================================================= + + async def load_mcp_tools(self) -> None: + """ + 异步加载所有 MCP (Model-Copilot-Plugin) 工具。 + + 此方法会动态导入并实例化 MCP 工具适配器。为避免重复加载,它会检查一个标志位。 + """ + if self._mcp_tools_loaded: + logger.debug("MCP 工具已加载,跳过") + return + + try: + from .mcp_tool_adapter import load_mcp_tools_as_adapters + + logger.info("开始加载 MCP 工具...") + self._mcp_tools = await load_mcp_tools_as_adapters() + self._mcp_tools_loaded = True + logger.info(f"MCP 工具加载完成,共 {len(self._mcp_tools)} 个工具") + except Exception as e: + logger.error(f"加载 MCP 工具失败: {e}", exc_info=True) + self._mcp_tools = [] + self._mcp_tools_loaded = True # 标记为已尝试加载,避免重复失败 + + def get_mcp_tools(self) -> list["BaseTool"]: + """获取所有已加载的 MCP 工具适配器实例。""" + return self._mcp_tools.copy() + + def is_mcp_tool(self, tool_name: str) -> bool: + """检查一个工具名称是否代表一个 MCP 工具(基于命名约定)。""" + return tool_name.startswith("mcp_") + + # ================================================================= + # == 统计与辅助方法 (Statistics and Helper Methods) + # ================================================================= + + def get_registry_stats(self) -> dict[str, Any]: + """获取注册中心的统计信息,用于调试和监控。""" + stats = {component_type.value: 0 for component_type in ComponentType} + for component in self._components.values(): + stats[component.component_type.value] += 1 + + return { + "total_plugins": len(self._plugins), + "total_components": len(self._components), + "enabled_components": len([c for c in self._components.values() if c.enabled]), + "mcp_tools_loaded": len(self._mcp_tools), + "components_by_type": stats, + } + + +# --- 全局实例 --- +# 创建全局唯一的组件注册中心实例,供项目各处使用 component_registry = ComponentRegistry() diff --git a/src/plugin_system/core/event_manager.py b/src/plugin_system/core/event_manager.py index fdd937035..f9e4de553 100644 --- a/src/plugin_system/core/event_manager.py +++ b/src/plugin_system/core/event_manager.py @@ -220,6 +220,36 @@ class EventManager: """ return self._event_handlers.copy() + def remove_event_handler(self, handler_name: str) -> bool: + """ + 完全移除一个事件处理器,包括其所有订阅。 + + Args: + handler_name (str): 要移除的事件处理器的名称。 + + Returns: + bool: 如果成功移除则返回 True,否则返回 False。 + """ + if handler_name not in self._event_handlers: + logger.warning(f"事件处理器 {handler_name} 未注册,无需移除。") + return False + + # 从主注册表中删除 + del self._event_handlers[handler_name] + logger.debug(f"事件处理器 {handler_name} 已从主注册表移除。") + + # 遍历所有事件,取消其订阅 + for event in self._events.values(): + # 创建订阅者列表的副本进行迭代,以安全地修改原始列表 + for subscriber in list(event.subscribers): + if getattr(subscriber, 'handler_name', None) == handler_name: + event.subscribers.remove(subscriber) + logger.debug(f"事件处理器 {handler_name} 已从事件 {event.name} 取消订阅。") + + logger.info(f"事件处理器 {handler_name} 已被完全移除。") + return True + + def subscribe_handler_to_event(self, handler_name: str, event_name: EventType | str) -> bool: """订阅事件处理器到指定事件 diff --git a/src/plugin_system/core/mcp_client_manager.py b/src/plugin_system/core/mcp_client_manager.py index ffcec9e1c..4ab10971a 100644 --- a/src/plugin_system/core/mcp_client_manager.py +++ b/src/plugin_system/core/mcp_client_manager.py @@ -146,9 +146,9 @@ class MCPClientManager: try: client = await self._create_client(server_config) self.clients[server_name] = client - logger.info(f"✅ MCP 服务器 '{server_name}' 连接成功") + logger.info(f" MCP 服务器 '{server_name}' 连接成功") except Exception as e: - logger.error(f"❌ 连接 MCP 服务器 '{server_name}' 失败: {e}") + logger.error(f" 连接 MCP 服务器 '{server_name}' 失败: {e}") continue self._initialized = True diff --git a/src/plugin_system/core/mcp_tool_adapter.py b/src/plugin_system/core/mcp_tool_adapter.py index ec5faf441..47e1547b7 100644 --- a/src/plugin_system/core/mcp_tool_adapter.py +++ b/src/plugin_system/core/mcp_tool_adapter.py @@ -47,7 +47,7 @@ class MCPToolAdapter(BaseTool): self.available_for_llm = True # MCP 工具默认可供 LLM 使用 # 转换参数定义 - self.parameters = self._convert_parameters(mcp_tool.inputSchema) + self.parameters: list[tuple[str, ToolParamType, str, bool, list[str] | None]] = self._convert_parameters(mcp_tool.inputSchema) logger.debug(f"创建 MCP 工具适配器: {self.name}") @@ -238,9 +238,9 @@ async def load_mcp_tools_as_adapters() -> list[MCPToolAdapter]: try: adapter = MCPToolAdapter.from_mcp_tool(server_name, mcp_tool) adapters.append(adapter) - logger.debug(f" ✓ 加载工具: {adapter.name}") + logger.debug(f" 加载工具: {adapter.name}") except Exception as e: - logger.error(f" ✗ 创建工具适配器失败: {mcp_tool.name} | 错误: {e}") + logger.error(f" 创建工具适配器失败: {mcp_tool.name} | 错误: {e}") continue logger.info(f"MCP 工具加载完成: 成功 {len(adapters)}/{total_tools} 个") diff --git a/src/plugin_system/core/plugin_manager.py b/src/plugin_system/core/plugin_manager.py index f2dc12d63..f1d4479c0 100644 --- a/src/plugin_system/core/plugin_manager.py +++ b/src/plugin_system/core/plugin_manager.py @@ -121,7 +121,7 @@ class PluginManager: if not module or not hasattr(module, "__plugin_meta__"): self.failed_plugins[plugin_name] = "插件模块中缺少 __plugin_meta__" - logger.error(f"❌ 插件加载失败: {plugin_name} - 缺少 __plugin_meta__") + logger.error(f" 插件加载失败: {plugin_name} - 缺少 __plugin_meta__") return False, 1 metadata: PluginMetadata = getattr(module, "__plugin_meta__") @@ -171,7 +171,7 @@ class PluginManager: return True, 1 else: self.failed_plugins[plugin_name] = "插件注册失败" - logger.error(f"❌ 插件注册失败: {plugin_name}") + logger.error(f" 插件注册失败: {plugin_name}") return False, 1 except Exception as e: @@ -249,13 +249,11 @@ class PluginManager: if plugin_name not in self.loaded_plugins: logger.warning(f"插件 {plugin_name} 未加载") return False - plugin_instance = self.loaded_plugins[plugin_name] - plugin_info = plugin_instance.plugin_info - success = True - for component in plugin_info.components: - success &= await component_registry.remove_component(component.name, component.component_type, plugin_name) - success &= component_registry.remove_plugin_registry(plugin_name) - del self.loaded_plugins[plugin_name] + # 调用 component_registry 中统一的卸载方法 + success = await component_registry.unregister_plugin(plugin_name) + if success: + # 从已加载插件中移除 + del self.loaded_plugins[plugin_name] return success async def reload_registered_plugin(self, plugin_name: str) -> bool: @@ -417,14 +415,14 @@ class PluginManager: if not success: error_msg = f"Python依赖检查失败: {', '.join(errors)}" self.failed_plugins[plugin_name] = error_msg - logger.error(f"❌ 插件加载失败: {plugin_name} - {error_msg}") + logger.error(f" 插件加载失败: {plugin_name} - {error_msg}") return None # 依赖检查失败,不加载该模块 # 2. 检查插件依赖 if not self._check_plugin_dependencies(metadata): error_msg = f"插件依赖检查失败: 请确保依赖 {metadata.dependencies} 已正确安装并加载。" self.failed_plugins[plugin_name] = error_msg - logger.error(f"❌ 插件加载失败: {plugin_name} - {error_msg}") + logger.error(f" 插件加载失败: {plugin_name} - {error_msg}") return None # 插件依赖检查失败 # --- 依赖检查逻辑结束 --- @@ -486,7 +484,7 @@ class PluginManager: # 📋 显示插件加载总览 if total_registered > 0: - logger.info("🎉 插件系统加载完成!") + logger.info(" 插件系统加载完成!") logger.info( f"📊 总览: {total_registered}个插件, {total_components}个组件 (Action: {action_count}, Command: {command_count}, Tool: {tool_count}, PlusCommand: {plus_command_count}, EventHandler: {event_handler_count}, Chatter: {chatter_count}, Prompt: {prompt_count}, Router: {router_count}, Adapter: {adapter_count})" ) diff --git a/src/plugin_system/core/stream_tool_history.py b/src/plugin_system/core/stream_tool_history.py index fd440a04a..6a2cfc997 100644 --- a/src/plugin_system/core/stream_tool_history.py +++ b/src/plugin_system/core/stream_tool_history.py @@ -245,7 +245,7 @@ class StreamToolHistoryManager: lines = ["## 🔧 最近工具调用记录"] for i, record in enumerate(recent_records, 1): - status_icon = "✅" if record.status == "success" else "❌" if record.status == "error" else "⏳" + status_icon = "success" if record.status == "success" else "error" if record.status == "error" else "pending" # 格式化参数 args_preview = self._format_args_preview(record.args) diff --git a/src/plugin_system/core/tool_use.py b/src/plugin_system/core/tool_use.py index 0b739aa9b..b79565f89 100644 --- a/src/plugin_system/core/tool_use.py +++ b/src/plugin_system/core/tool_use.py @@ -217,12 +217,11 @@ class ToolExecutor: return tool_results, [], "" def _get_tool_definitions(self) -> list[dict[str, Any]]: - all_tools = get_llm_available_tool_definitions() - user_disabled_tools = global_announcement_manager.get_disabled_chat_tools(self.chat_id) + all_tools = get_llm_available_tool_definitions(self.chat_id) # 获取基础工具定义(包括二步工具的第一步) tool_definitions = [ - definition for definition in all_tools if definition.get("function", {}).get("name") not in user_disabled_tools + definition for definition in all_tools if definition.get("function", {}).get("name") ] # 检查是否有待处理的二步工具第二步调用 diff --git a/src/plugin_system/utils/dependency_manager.py b/src/plugin_system/utils/dependency_manager.py index 4d5e48a9d..2939d8bb6 100644 --- a/src/plugin_system/utils/dependency_manager.py +++ b/src/plugin_system/utils/dependency_manager.py @@ -110,19 +110,19 @@ class DependencyManager: for package in packages: try: if self._install_single_package(package, plugin_name): - logger.info(f"{log_prefix}✅ 成功安装: {package}") + logger.info(f"{log_prefix} 成功安装: {package}") else: failed_packages.append(package) - logger.error(f"{log_prefix}❌ 安装失败: {package}") + logger.error(f"{log_prefix} 安装失败: {package}") except Exception as e: failed_packages.append(package) - logger.error(f"{log_prefix}❌ 安装 {package} 时发生异常: {e!s}") + logger.error(f"{log_prefix} 安装 {package} 时发生异常: {e!s}") success = len(failed_packages) == 0 if success: - logger.info(f"{log_prefix}🎉 所有依赖安装完成") + logger.info(f"{log_prefix} 所有依赖安装完成") else: - logger.error(f"{log_prefix}⚠️ 部分依赖安装失败: {failed_packages}") + logger.error(f"{log_prefix} 部分依赖安装失败: {failed_packages}") return success, failed_packages diff --git a/src/plugins/built_in/affinity_flow_chatter/core/affinity_interest_calculator.py b/src/plugins/built_in/affinity_flow_chatter/core/affinity_interest_calculator.py index 612654a0e..0148bf6ca 100644 --- a/src/plugins/built_in/affinity_flow_chatter/core/affinity_interest_calculator.py +++ b/src/plugins/built_in/affinity_flow_chatter/core/affinity_interest_calculator.py @@ -223,7 +223,7 @@ class AffinityInterestCalculator(BaseInterestCalculator): return 0.0 except asyncio.TimeoutError: - logger.warning("⏱️ 兴趣匹配计算超时(>1.5秒),返回默认分值0.5以保留其他分数") + logger.warning("[超时] 兴趣匹配计算超时(>1.5秒),返回默认分值0.5以保留其他分数") return 0.5 # 超时时返回默认分值,避免丢失提及分和关系分 except Exception as e: logger.warning(f"智能兴趣匹配失败: {e}") diff --git a/src/plugins/built_in/affinity_flow_chatter/planner/plan_filter.py b/src/plugins/built_in/affinity_flow_chatter/planner/plan_filter.py index b391c382e..35ba414ea 100644 --- a/src/plugins/built_in/affinity_flow_chatter/planner/plan_filter.py +++ b/src/plugins/built_in/affinity_flow_chatter/planner/plan_filter.py @@ -551,10 +551,24 @@ class ChatterPlanFilter: available_actions=plan.available_actions, ) else: + # 如果LLM没有指定target_message_id,统一使用最新消息 + target_message_dict = self._get_latest_message(message_id_list) + action_message_obj = None + if target_message_dict: + from src.common.data_models.database_data_model import DatabaseMessages + try: + action_message_obj = DatabaseMessages(**target_message_dict) + except Exception as e: + logger.error( + f"[{action}] 无法将默认的最新消息转换为 DatabaseMessages 对象: {e}", + exc_info=True, + ) + return ActionPlannerInfo( action_type=action, reasoning=reasoning, action_data=action_data, + action_message=action_message_obj, ) except Exception as e: logger.error(f"解析单个action时出错: {e}") diff --git a/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_event.py b/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_event.py index 2f8b4154f..b20dfd5e8 100644 --- a/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_event.py +++ b/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_event.py @@ -85,11 +85,11 @@ class ProactiveThinkingReplyHandler(BaseEventHandler): if success: if was_paused: - logger.info(f"✅ 聊天流 {stream_id} 主动思考已恢复并重置") + logger.info(f"[成功] 聊天流 {stream_id} 主动思考已恢复并重置") else: - logger.debug(f"✅ 聊天流 {stream_id} 主动思考任务已重置") + logger.debug(f"[成功] 聊天流 {stream_id} 主动思考任务已重置") else: - logger.warning(f"❌ 重置聊天流 {stream_id} 主动思考任务失败") + logger.warning(f"[错误] 重置聊天流 {stream_id} 主动思考任务失败") except Exception as e: logger.error(f"❌ 处理reply事件时出错: {e}") diff --git a/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_executor.py b/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_executor.py index 25214cefb..7b9e96867 100644 --- a/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_executor.py +++ b/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_executor.py @@ -25,18 +25,149 @@ logger = get_logger("proactive_thinking_executor") # == Prompt Templates # ================================================================================================== -# 决策 Prompt -decision_prompt_template = Prompt( +# --- 群聊场景 --- +decision_prompt_template_group = Prompt( """{time_block} 你的人设是: {bot_personality} -你正在考虑是否要在与 "{stream_name}" 的对话中主动说些什么。 +你正在考虑是否要在 **群聊 "{stream_name}"** 中主动说些什么。 【你当前的心情】 {current_mood} -【聊天环境信息】 +【群聊环境信息】 +- 整体印象: {stream_impression} +- 聊天风格: {chat_style} +- 常见话题: {topic_keywords} +- 你的兴趣程度: {interest_score:.2f}/1.0 +{last_decision_text} + +【最近的聊天记录】 +{recent_chat_history} + +请根据以上信息,决定你现在应该做什么: + +**选项1:什么都不做 (do_nothing)** +- **适用场景**: + - **最高优先级**:如果【最近的聊天记录】中最后一条消息是你自己发的,必须选择此项。 + - 其他情况:群里气氛不适合你说话、最近对话很活跃、没什么特别想说的、或者此时说话会显得突兀。 +- 心情影响:如果心情不好(如生气、难过),可能更倾向于保持沉默。 + +**选项2:简单冒个泡 (simple_bubble)** +- 适用场景:群里有些冷清,你想缓和气氛或开启新的互动。 +- 方式:说一句轻松随意的话,旨在建立或维持连接。 +- 心情影响:心情会影响你冒泡的方式和内容。 + +**选项3:发起一次有目的的互动 (throw_topic)** +- 适用场景:你想延续对话、表达关心、或深入讨论某个具体话题。 +- **【互动类型1:延续约定或提醒】(最高优先级)**:检查最近的聊天记录,是否存在可以延续的互动。例如,如果昨晚的最后一条消息是“晚安”,现在是早上,一个“早安”的回应是绝佳的选择。如果之前提到过某个约定(如“待会聊”),现在可以主动跟进。 +- **【互动类型2:展现真诚的关心】(次高优先级)**:如果不存在可延续的约定,请仔细阅读聊天记录,寻找**群友**提及的个人状况(如天气、出行、身体、情绪、工作学习等),并主动表达关心。 +- **【互动类型3:开启新话题】**:当以上两点都不适用时,可以考虑开启一个你感兴趣的新话题。 +- 心情影响:心情会影响你想发起互动的方式和内容。 + +请以JSON格式回复你的决策: +{{ + "action": "do_nothing" | "simple_bubble" | "throw_topic", + "reasoning": "你的决策理由(请结合你的心情、群聊环境和对话历史进行分析)", + "topic": "(仅当action=throw_topic时填写)你的互动意图(如:回应晚安并说早安、关心大家的考试情况、讨论新游戏)" +}} + +注意: +1. 兴趣度较低(<0.4)时或者最近聊天很活跃(不到1小时),倾向于 `do_nothing` 或 `simple_bubble`。 +2. 你的心情会影响你的行动倾向和表达方式。 +3. 参考上次决策,避免重复,并可根据上次的互动效果调整策略。 +4. 只有在真的有感而发时才选择 `throw_topic`。 +5. 保持你的人设,确保行为一致性。 +""", + name="proactive_thinking_decision_group", +) + +simple_bubble_reply_prompt_template_group = Prompt( + """{time_block} +你的人设是: +{bot_personality} + +距离上次对话已经有一段时间了,你决定在群里主动说些什么,轻松地开启新的互动。 + +【你当前的心情】 +{current_mood} + +【群聊环境】 +- 整体印象: {stream_impression} +- 聊天风格: {chat_style} + +【最近的聊天记录】 +{recent_chat_history} +{expression_habits} +请生成一条简短的消息,用于**在群聊中冒泡**。 +【要求】 +1. 风格简短随意(5-20字) +2. 不要提出明确的话题或问题,可以是问候、表达心情或一句随口的话。 +3. 符合你的人设和当前聊天风格。 +4. **你的心情应该影响消息的内容和语气**。 +5. 如果有表达方式参考,在合适时自然使用。 +6. 合理参考历史记录。 +直接输出消息内容,不要解释:""", + name="proactive_thinking_simple_bubble_group", +) + +throw_topic_reply_prompt_template_group = Prompt( + """{time_block} +你的人设是: +{bot_personality} + +你决定在 **群聊 "{stream_name}"** 中主动发起一次互动。 + +【你当前的心情】 +{current_mood} + +【群聊环境】 +- 整体印象: {stream_impression} +- 聊天风格: {chat_style} +- 常见话题: {topic_keywords} + +【最近的聊天记录】 +{recent_chat_history} + +【你的互动意图】 +{topic} +{expression_habits} +【构思指南】 +请根据你的互动意图,并参考最近的聊天记录,生成一条有温度的、**适合在群聊中说**的消息。 + +- 如果意图是**延续约定**(如回应“晚安”),请直接生成对应的问候。 +- 如果意图是**表达关心**(如跟进群友提到的事),请生成自然、真诚的关心话语。 +- 如果意图是**开启新话题**:请严格遵守以下“新话题构思三步法”: + 1. **寻找灵感**:**首选**从【最近的聊天记录】中寻找一个可以自然延续的**生活化**细节。**严禁**引入与聊天记录完全无关的、凭空出现的话题。如果记录为空,可以根据你的【人设】,提出一个**非常普适、开放式**的生活化问题或感想。 + 2. **确定风格**:请**确保新话题与最近的聊天内容有关联**,自然地引入话题,避免过于跳脱。 + 3. **最终检查**:你提出的话题是否合理?是否贴近现实和聊天内容?说话方式是否正常?是否像一个真正的人类? + +请根据这个意图,生成一条消息,要求: +1. 要与最近的聊天记录相关,自然地引入话题或表达关心。 +2. 长度适中(20-40字)。 +3. 结合最近的聊天记录确保对话连贯,不要显得突兀。 +4. 符合你的人设和当前聊天风格。 +5. **你的心情会影响你的表达方式**。 +6. 如果有表达方式参考,在合适时自然使用。 + +直接输出消息内容,不要解释:""", + name="proactive_thinking_throw_topic_group", +) + + +# --- 私聊场景 --- +decision_prompt_template_private = Prompt( + """{time_block} +你的人设是: +{bot_personality} + +你正在考虑是否要主动与 **"{stream_name}"** 说些什么。 + +【你当前的心情】 +{current_mood} + +【与对方的聊天信息】 - 整体印象: {stream_impression} - 聊天风格: {chat_style} - 常见话题: {topic_keywords} @@ -52,22 +183,22 @@ decision_prompt_template = Prompt( - 适用场景:气氛不适合说话、最近对话很活跃、没什么特别想说的、或者此时说话会显得突兀。 - 心情影响:如果心情不好(如生气、难过),可能更倾向于保持沉默。 -**选项2:简单冒个泡 (simple_bubble)** -- 适用场景:对话有些冷清,你想缓和气氛或开启新的互动。 +**选项2:简单问候一下 (simple_bubble)** +- 适用场景:对话有些冷清,你想开启新的互动。 - 方式:说一句轻松随意的话,旨在建立或维持连接。 -- 心情影响:心情会影响你冒泡的方式和内容。 +- 心情影响:心情会影响你问候的方式和内容。 **选项3:发起一次有目的的互动 (throw_topic)** - 适用场景:你想延续对话、表达关心、或深入讨论某个具体话题。 - **【互动类型1:延续约定或提醒】(最高优先级)**:检查最近的聊天记录,是否存在可以延续的互动。例如,如果昨晚的最后一条消息是“晚安”,现在是早上,一个“早安”的回应是绝佳的选择。如果之前提到过某个约定(如“待会聊”),现在可以主动跟进。 -- **【互动类型2:展现真诚的关心】(次高优先级)**:如果不存在可延续的约定,请仔细阅读聊天记录,寻找对方提及的个人状况(如天气、出行、身体、情绪、工作学习等),并主动表达关心。 +- **【互动类型2:展现真诚的关心】(次高优先级)**:如果不存在可延续的约定,请仔细阅读聊天记录,寻找**对方**提及的个人状况(如天气、出行、身体、情绪、工作学习等),并主动表达关心。 - **【互动类型3:开启新话题】**:当以上两点都不适用时,可以考虑开启一个你感兴趣的新话题。 - 心情影响:心情会影响你想发起互动的方式和内容。 请以JSON格式回复你的决策: {{ "action": "do_nothing" | "simple_bubble" | "throw_topic", - "reasoning": "你的决策理由(请结合你的心情、聊天环境和对话历史进行分析)", + "reasoning": "你的决策理由(请结合你的心情、与对方的聊天情况和对话历史进行分析)", "topic": "(仅当action=throw_topic时填写)你的互动意图(如:回应晚安并说早安、关心对方的考试情况、讨论新游戏)" }} @@ -78,28 +209,27 @@ decision_prompt_template = Prompt( 4. 只有在真的有感而发时才选择 `throw_topic`。 5. 保持你的人设,确保行为一致性。 """, - name="proactive_thinking_decision", + name="proactive_thinking_decision_private", ) -# 冒泡回复 Prompt -simple_bubble_reply_prompt_template = Prompt( +simple_bubble_reply_prompt_template_private = Prompt( """{time_block} 你的人设是: {bot_personality} -距离上次对话已经有一段时间了,你决定主动说些什么,轻松地开启新的互动。 +距离上次和 **"{stream_name}"** 对话已经有一段时间了,你决定主动说些什么,轻松地开启新的互动。 【你当前的心情】 {current_mood} -【聊天环境】 +【与对方的聊天环境】 - 整体印象: {stream_impression} - 聊天风格: {chat_style} 【最近的聊天记录】 {recent_chat_history} {expression_habits} -请生成一条简短的消息,用于水群。 +请生成一条简短的消息,用于**私聊中轻松地打个招呼**。 【要求】 1. 风格简短随意(5-20字) 2. 不要提出明确的话题或问题,可以是问候、表达心情或一句随口的话。 @@ -108,21 +238,20 @@ simple_bubble_reply_prompt_template = Prompt( 5. 如果有表达方式参考,在合适时自然使用。 6. 合理参考历史记录。 直接输出消息内容,不要解释:""", - name="proactive_thinking_simple_bubble", + name="proactive_thinking_simple_bubble_private", ) -# 抛出话题回复 Prompt -throw_topic_reply_prompt_template = Prompt( +throw_topic_reply_prompt_template_private = Prompt( """{time_block} 你的人设是: {bot_personality} -你决定在与 "{stream_name}" 的对话中主动发起一次互动。 +你决定在与 **"{stream_name}"** 的私聊中主动发起一次互动。 【你当前的心情】 {current_mood} -【聊天环境】 +【与对方的聊天环境】 - 整体印象: {stream_impression} - 聊天风格: {chat_style} - 常见话题: {topic_keywords} @@ -134,11 +263,14 @@ throw_topic_reply_prompt_template = Prompt( {topic} {expression_habits} 【构思指南】 -请根据你的互动意图,并参考最近的聊天记录,生成一条有温度的消息。 +请根据你的互动意图,并参考最近的聊天记录,生成一条有温度的、**适合在私聊中说**的消息。 - 如果意图是**延续约定**(如回应“晚安”),请直接生成对应的问候。 -- 如果意图是**表达关心**(如跟进对方提到的事),请生成自然、真诚的关心话语。 -- 如果意图是**开启新话题**,请**确保新话题与最近的聊天内容有关联**,自然地引入话题,避免过于跳脱。 - +- 如果意ت意图是**表达关心**(如跟进对方提到的事),请生成自然、真诚的关心话语。 +- 如果意图是**开启新话题**:请严格遵守以下“新话题构思三步法”: + 1. **寻找灵感**:**首选**从【最近的聊天记录】中寻找一个可以自然延续的**生活化**细节。**严禁**引入与聊天记录完全无关的、凭空出现的话题。如果记录为空,可以根据你的【人设】,提出一个**非常普适、开放式**的生活化问题或感想。 + 2. **确定风格**:请**确保新话题与最近的聊天内容有关联**,自然地引入话题,避免过于跳脱。 + 3. **最终检查**:你提出的话题是否合理?是否贴近现实和聊天内容?说话方式是否正常?是否像一个真正的人类? + 请根据这个意图,生成一条消息,要求: 1. 要与最近的聊天记录相关,自然地引入话题或表达关心。 2. 长度适中(20-40字)。 @@ -148,7 +280,7 @@ throw_topic_reply_prompt_template = Prompt( 6. 如果有表达方式参考,在合适时自然使用。 直接输出消息内容,不要解释:""", - name="proactive_thinking_throw_topic", + name="proactive_thinking_throw_topic_private", ) @@ -194,7 +326,7 @@ class ProactiveThinkingPlanner: # 2. 获取最近的聊天记录 recent_messages = await message_api.get_recent_messages( chat_id=stream_id, - limit=40, + limit=global_config.chat.max_context_size, limit_mode="latest", hours=24 ) @@ -237,9 +369,13 @@ class ProactiveThinkingPlanner: logger.warning(f"获取上次决策失败: {e}") # 6. 构建上下文 + # 7. 判断聊天类型 + chat_type = "group" if "group" in stream_id else "private" + context = { "stream_id": stream_id, "stream_name": stream_data.get("stream_name", "未知"), + "chat_type": chat_type, "stream_impression": stream_data.get("stream_impression_text", "暂无印象"), "chat_style": stream_data.get("stream_chat_style", "未知"), "topic_keywords": stream_data.get("stream_topic_keywords", ""), @@ -318,6 +454,13 @@ class ProactiveThinkingPlanner: if last_topic: last_decision_text += f"\n- 话题: {last_topic}" + # 根据聊天类型选择不同的决策Prompt + chat_type = context.get("chat_type", "group") + if chat_type == "private": + decision_prompt_template = decision_prompt_template_private + else: + decision_prompt_template = decision_prompt_template_group + decision_prompt = decision_prompt_template.format( time_block=context["time_block"], bot_personality=context["bot_personality"], @@ -378,10 +521,20 @@ class ProactiveThinkingPlanner: stream_id=context.get("stream_id", ""), chat_history=context.get("recent_chat_history", "") ) + # 根据聊天类型选择不同的回复Prompt + chat_type = context.get("chat_type", "group") + if chat_type == "private": + simple_template = simple_bubble_reply_prompt_template_private + throw_template = throw_topic_reply_prompt_template_private + else: + simple_template = simple_bubble_reply_prompt_template_group + throw_template = throw_topic_reply_prompt_template_group + if action == "simple_bubble": - reply_prompt = simple_bubble_reply_prompt_template.format( + reply_prompt = simple_template.format( time_block=context["time_block"], bot_personality=context["bot_personality"], + stream_name=context["stream_name"], current_mood=context.get("current_mood", "感觉很平静"), stream_impression=context["stream_impression"], chat_style=context["chat_style"], @@ -389,7 +542,7 @@ class ProactiveThinkingPlanner: expression_habits=expression_habits, ) else: # throw_topic - reply_prompt = throw_topic_reply_prompt_template.format( + reply_prompt = throw_template.format( time_block=context["time_block"], bot_personality=context["bot_personality"], stream_name=context["stream_name"], @@ -550,11 +703,11 @@ async def execute_proactive_thinking(stream_id: str): # 尝试获取锁,如果已被占用则跳过本次执行(防止重复) if lock.locked(): - logger.warning(f"⚠️ 主动思考跳过:聊天流 {stream_id} 已有正在执行的主动思考任务") + logger.warning(f"[警告] 主动思考跳过:聊天流 {stream_id} 已有正在执行的主动思考任务") return async with lock: - logger.debug(f"🤔 开始主动思考 {stream_id}") + logger.debug(f"[思考] 开始主动思考 {stream_id}") try: # 0. 前置检查 @@ -565,8 +718,11 @@ async def execute_proactive_thinking(stream_id: str): chat_stream = await chat_manager.get_stream(stream_id) if chat_stream and chat_stream.context.is_chatter_processing: - logger.warning(f"⚠️ 主动思考跳过:聊天流 {stream_id} 的 chatter 正在处理消息") - return + logger.warning(f"[警告] 主动思考等待:聊天流 {stream_id} 的 chatter 正在处理消息,等待3秒后重试...") + await asyncio.sleep(3) + if chat_stream.context.is_chatter_processing: + logger.warning(f"[警告] 主动思考跳过:聊天流 {stream_id} 的 chatter 仍在处理消息") + return except Exception as e: logger.warning(f"检查 chatter 处理状态时出错: {e},继续执行") @@ -634,7 +790,7 @@ async def execute_proactive_thinking(stream_id: str): return elif action == "simple_bubble": - logger.info(f"💬 决策:冒个泡。理由:{reasoning}") + logger.info(f"[决策] 决策:冒个泡。理由:{reasoning}") proactive_thinking_scheduler.record_decision(stream_id, action, reasoning, None) @@ -646,7 +802,7 @@ async def execute_proactive_thinking(stream_id: str): stream_id=stream_id, text=reply, ) - logger.info("✅ 已发送冒泡消息") + logger.info("[成功] 已发送冒泡消息") # 增加每日计数 proactive_thinking_scheduler._increment_daily_count(stream_id) diff --git a/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_scheduler.py b/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_scheduler.py index b8f11ec9f..afe8820c3 100644 --- a/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_scheduler.py +++ b/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_scheduler.py @@ -361,7 +361,7 @@ class ProactiveThinkingScheduler: self._paused_streams.discard(stream_id) success = await unified_scheduler.remove_schedule(schedule_id) - logger.debug(f"⏹️ 取消主动思考 {stream_id}") + logger.debug(f"[取消] 取消主动思考 {stream_id}") return success @@ -482,7 +482,7 @@ class ProactiveThinkingScheduler: minutes = (remaining_seconds % 3600) // 60 time_str = f"{hours}小时{minutes}分钟后" - status = "⏸️ 暂停中" if is_paused else "✅ 活跃" + status = "[暂停] 暂停中" if is_paused else "[活跃] 活跃" logger.info( f"[{i:2d}] {status} | {stream_name}\n" diff --git a/src/plugins/built_in/maizone_refactored/services/scheduler_service.py b/src/plugins/built_in/maizone_refactored/services/scheduler_service.py index d5437c0fa..ac994c224 100644 --- a/src/plugins/built_in/maizone_refactored/services/scheduler_service.py +++ b/src/plugins/built_in/maizone_refactored/services/scheduler_service.py @@ -63,35 +63,6 @@ class SchedulerService: pass # 任务取消是正常操作 logger.info("基于日程表的说说定时发送任务已停止。") - async def _generate_random_topic(self) -> str | None: - """ - 使用小模型生成一个随机的说说主题。 - """ - try: - logger.info("尝试生成随机说说主题...") - prompt = "请生成一个有趣、简短、积极向上的日常一句话,适合作为社交媒体的动态内容,例如关于天气、心情、动漫、游戏或者某个小发现。请直接返回这句话,不要包含任何多余的解释或标签。" - - task_config = global_model_config.model_task_config.get_task("utils_small") - if not task_config: - logger.error("未找到名为 'utils_small' 的模型任务配置。") - return None - - success, content, _, _ = await llm_api.generate_with_model( - model_config=task_config, - prompt=prompt, - max_tokens=150, - temperature=0.9, - ) - - if success and content and content.strip(): - logger.info(f"成功生成随机主题: {content.strip()}") - return content.strip() - logger.warning("LLM未能生成有效的主题。") - return None - except Exception as e: - logger.error(f"生成随机主题时发生错误: {e}") - return None - async def _schedule_loop(self): """ 定时任务的核心循环。 @@ -140,21 +111,13 @@ class SchedulerService: activity_placeholder = "No Schedule - Random" if not await self._is_processed(hour_str, activity_placeholder): logger.info("没有日程活动,但开启了无日程发送功能,准备生成随机主题。") - topic = await self._generate_random_topic() - if topic: - result = await self.qzone_service.send_feed(topic=topic, stream_id=None) - await self._mark_as_processed( + result = await self.qzone_service.send_feed(topic="随意发挥",stream_id=None) + await self._mark_as_processed( hour_str, activity_placeholder, result.get("success", False), result.get("message", ""), ) - else: - logger.error("未能生成随机主题,本次不发送。") - # 即使生成失败,也标记为已处理,防止本小时内反复尝试 - await self._mark_as_processed( - hour_str, activity_placeholder, False, "Failed to generate topic" - ) else: logger.info(f"当前小时 {hour_str} 已执行过无日程发送任务,本次跳过。") diff --git a/src/plugins/built_in/system_management/plugin.py b/src/plugins/built_in/system_management/plugin.py index d3f9ed83e..72bc826c6 100644 --- a/src/plugins/built_in/system_management/plugin.py +++ b/src/plugins/built_in/system_management/plugin.py @@ -11,6 +11,7 @@ from typing import ClassVar from src.chat.utils.prompt_component_manager import prompt_component_manager from src.chat.utils.prompt_params import PromptParameters from src.plugin_system.apis import ( + chat_api, plugin_manage_api, ) from src.plugin_system.apis.logging_api import get_logger @@ -21,6 +22,7 @@ from src.plugin_system.base.base_plugin import BasePlugin from src.plugin_system.base.command_args import CommandArgs from src.plugin_system.base.component_types import ( ChatType, + ComponentType, PermissionNodeField, PlusCommandInfo, ) @@ -96,16 +98,16 @@ class SystemCommand(PlusCommand): help_text = """🔌 插件管理命令帮助 📋 基本操作: • `/system plugin help` - 显示插件管理帮助 -• `/system plugin list` - 列出所有注册的插件 -• `/system plugin list_enabled` - 列出所有加载(启用)的插件 +• `/system plugin report` - 查看系统插件报告 • `/system plugin rescan` - 重新扫描所有插件目录 ⚙️ 插件控制: • `/system plugin load <插件名>` - 加载指定插件 -• `/system plugin unload <插件名>` - 卸载指定插件 • `/system plugin reload <插件名>` - 重新加载指定插件 -• `/system plugin force_reload <插件名>` - 强制重载指定插件 -• `/system plugin add_dir <目录路径>` - 添加插件目录 +• `/system plugin reload_all` - 重新加载所有插件 +🎯 局部控制 (需要 `system.plugin.manage.local` 权限): +• `/system plugin enable_local <名称> [group <群号> | private ]` - 在指定会话局部启用组件 +• `/system plugin disable_local <名称> [group <群号> | private ]` - 在指定会话局部禁用组件 """ elif target == "permission": help_text = """📋 权限管理命令帮助 @@ -150,20 +152,20 @@ class SystemCommand(PlusCommand): if action in ["help", "帮助"]: await self._show_help("plugin") - elif action in ["list", "列表"]: - await self._list_registered_plugins() - elif action in ["list_enabled", "已启用"]: - await self._list_loaded_plugins() + elif action in ["report", "报告"]: + await self._show_system_report() elif action in ["rescan", "重扫"]: await self._rescan_plugin_dirs() elif action in ["load", "加载"] and len(remaining_args) > 0: await self._load_plugin(remaining_args[0]) - elif action in ["unload", "卸载"] and len(remaining_args) > 0: - await self._unload_plugin(remaining_args[0]) elif action in ["reload", "重载"] and len(remaining_args) > 0: await self._reload_plugin(remaining_args[0]) - elif action in ["force_reload", "强制重载"] and len(remaining_args) > 0: - await self._force_reload_plugin(remaining_args[0]) + elif action in ["reload_all", "重载全部"]: + await self._reload_all_plugins() + elif action in ["enable_local", "局部启用"] and len(remaining_args) >= 1: + await self._set_local_component_state(remaining_args, enabled=True) + elif action in ["disable_local", "局部禁用"] and len(remaining_args) >= 1: + await self._set_local_component_state(remaining_args, enabled=False) else: await self.send_text("❌ 插件管理命令不合法\n使用 /system plugin help 查看帮助") @@ -316,7 +318,7 @@ class SystemCommand(PlusCommand): @require_permission("prompt.view", deny_message="❌ 你没有查看提示词注入信息的权限") async def _list_prompt_components(self): """列出所有已注册的提示词组件""" - components = prompt_component_manager.get_registered_prompt_component_info() + components = await prompt_component_manager.get_registered_prompt_component_info() if not components: await self.send_text("🧩 当前没有已注册的提示词组件") return @@ -398,7 +400,7 @@ class SystemCommand(PlusCommand): @require_permission("prompt.view", deny_message="❌ 你没有查看提示词组件信息的权限") async def _show_prompt_component_info(self, component_name: str): """显示特定提示词组件的详细信息""" - all_components = prompt_component_manager.get_registered_prompt_component_info() + all_components = await prompt_component_manager.get_registered_prompt_component_info() target_component = next((comp for comp in all_components if comp.name == component_name), None) @@ -429,61 +431,151 @@ class SystemCommand(PlusCommand): # Permission Management Section # ================================================================= - async def _list_loaded_plugins(self): - """列出已加载的插件""" - plugins = plugin_manage_api.list_loaded_plugins() - await self.send_text(f"📦 已加载的插件: {', '.join(plugins) if plugins else '无'}") + @require_permission("plugin.manage", deny_message="❌ 你没有权限查看插件报告") + async def _show_system_report(self): + """显示系统插件报告""" + report = plugin_manage_api.get_system_report() + + response_parts = [ + "📊 **系统插件报告**", + f" - 已加载插件: {report['system_info']['loaded_plugins_count']}", + f" - 组件总数: {report['system_info']['total_components_count']}", + ] - async def _list_registered_plugins(self): - """列出已注册的插件""" - plugins = plugin_manage_api.list_registered_plugins() - await self.send_text(f"📋 已注册的插件: {', '.join(plugins) if plugins else '无'}") + if report["plugins"]: + response_parts.append("\n✅ **已加载插件:**") + for name, info in report["plugins"].items(): + response_parts.append(f" • **{info['display_name']} (`{name}`)** v{info['version']} by {info['author']}") + + if report["failed_plugins"]: + response_parts.append("\n❌ **加载失败的插件:**") + for name, error in report["failed_plugins"].items(): + response_parts.append(f" • **`{name}`**: {error}") + + await self._send_long_message("\n".join(response_parts)) + + @require_permission("plugin.manage", deny_message="❌ 你没有权限扫描插件") async def _rescan_plugin_dirs(self): """重新扫描插件目录""" - plugin_manage_api.rescan_plugin_directory() - await self.send_text("🔄 插件目录重新扫描已启动") + await self.send_text("🔄 正在重新扫描插件目录...") + success, fail = plugin_manage_api.rescan_and_register_plugins(load_after_register=True) + await self.send_text(f"✅ 扫描完成!\n新增成功: {success}个, 新增失败: {fail}个。") + @require_permission("plugin.manage", deny_message="❌ 你没有权限加载插件") async def _load_plugin(self, plugin_name: str): """加载指定插件""" - success, count = plugin_manage_api.load_plugin(plugin_name) + success = plugin_manage_api.register_plugin_from_file(plugin_name, load_after_register=True) if success: await self.send_text(f"✅ 插件加载成功: `{plugin_name}`") else: - if count == 0: - await self.send_text(f"⚠️ 插件 `{plugin_name}` 为禁用状态") - else: - await self.send_text(f"❌ 插件加载失败: `{plugin_name}`") + await self.send_text(f"❌ 插件加载失败: `{plugin_name}`。请检查日志获取详细信息。") - async def _unload_plugin(self, plugin_name: str): - """卸载指定插件""" - success = await plugin_manage_api.remove_plugin(plugin_name) - if success: - await self.send_text(f"✅ 插件卸载成功: `{plugin_name}`") - else: - await self.send_text(f"❌ 插件卸载失败: `{plugin_name}`") + @require_permission("plugin.manage", deny_message="❌ 你没有权限重载插件") async def _reload_plugin(self, plugin_name: str): """重新加载指定插件""" - success = await plugin_manage_api.reload_plugin(plugin_name) - if success: - await self.send_text(f"✅ 插件重新加载成功: `{plugin_name}`") - else: - await self.send_text(f"❌ 插件重新加载失败: `{plugin_name}`") - - async def _force_reload_plugin(self, plugin_name: str): - """强制重载指定插件(深度清理)""" - await self.send_text(f"🔄 开始强制重载插件: `{plugin_name}`... (注意: 实际执行reload)") try: success = await plugin_manage_api.reload_plugin(plugin_name) if success: - await self.send_text(f"✅ 插件重载成功: `{plugin_name}`") + await self.send_text(f"✅ 插件重新加载成功: `{plugin_name}`") else: - await self.send_text(f"❌ 插件重载失败: `{plugin_name}`") - except Exception as e: - await self.send_text(f"❌ 重载过程中发生错误: {e!s}") + await self.send_text(f"❌ 插件重新加载失败: `{plugin_name}`") + except ValueError as e: + await self.send_text(f"❌ 操作失败: {e}") + @require_permission("plugin.manage", deny_message="❌ 你没有权限重载所有插件") + async def _reload_all_plugins(self): + """重新加载所有插件""" + await self.send_text("🔄 正在重新加载所有插件...") + success = await plugin_manage_api.reload_all_plugins() + if success: + await self.send_text("✅ 所有插件已成功重载。") + else: + await self.send_text("⚠️ 部分插件重载失败,请检查日志。") + + @require_permission("plugin.manage.local", deny_message="❌ 你没有局部管理插件组件的权限") + async def _set_local_component_state(self, args: list[str], enabled: bool): + """在局部范围内启用或禁用一个组件""" + # 命令格式: [group | private ] + if not args: + action = "enable_local" if enabled else "disable_local" + await self.send_text(f"❌ 用法: /system plugin {action} <名称> [group <群号> | private ]") + return + + comp_name = args[0] + context_args = args[1:] + stream_id = self.message.chat_info.stream_id # 默认作用于当前会话 + + # 1. 搜索组件 + found_components = plugin_manage_api.search_components_by_name(comp_name, exact_match=True) + + if not found_components: + await self.send_text(f"❌ 未找到名为 '{comp_name}' 的组件。") + return + + if len(found_components) > 1: + suggestions = "\n".join([f"- `{c['name']}` (类型: {c['component_type']})" for c in found_components]) + await self.send_text(f"❌ 发现多个名为 '{comp_name}' 的组件,操作已取消。\n找到的组件:\n{suggestions}") + return + + component_info = found_components[0] + comp_type_str = component_info["component_type"] + component_type = ComponentType(comp_type_str) + + # 2. 增加禁用保护 + if not enabled: # 如果是禁用操作 + # 定义不可禁用的核心组件类型 + protected_types = [ + ComponentType.INTEREST_CALCULATOR, + ComponentType.PROMPT, + ComponentType.ROUTER, + ] + if component_type in protected_types: + await self.send_text(f"❌ 无法局部禁用核心组件 '{comp_name}' ({comp_type_str})。") + return + + # 3. 解析上下文 + if len(context_args) >= 2: + context_type = context_args[0].lower() + context_id = context_args[1] + + target_stream = None + if context_type == "group": + target_stream = chat_api.get_stream_by_group_id( + group_id=context_id, + platform=self.message.chat_info.platform + ) + elif context_type == "private": + target_stream = chat_api.get_stream_by_user_id( + user_id=context_id, + platform=self.message.chat_info.platform + ) + else: + await self.send_text("❌ 无效的作用域类型,请使用 'group' 或 'private'。") + return + + if not target_stream: + await self.send_text(f"❌ 在当前平台找不到指定的 {context_type}: `{context_id}`。") + return + + stream_id = target_stream.stream_id + + # 4. 执行操作 + success = plugin_manage_api.set_component_enabled_local( + stream_id=stream_id, + name=comp_name, + component_type=component_type, + enabled=enabled + ) + + action_text = "启用" if enabled else "禁用" + if success: + await self.send_text(f"✅ 在会话 `{stream_id}` 中,已成功将组件 `{comp_name}` ({comp_type_str}) 设置为 {action_text} 状态。") + else: + await self.send_text(f"❌ 操作失败。可能无法禁用最后一个启用的 Chatter,或组件不存在。请检查日志。") + # ================================================================= # Permission Management Section @@ -729,4 +821,8 @@ class SystemManagementPlugin(BasePlugin): node_name="schedule.manage", description="定时任务管理:暂停和恢复定时任务", ), + PermissionNodeField( + node_name="plugin.manage.local", + description="局部插件管理:在指定会话中启用或禁用组件", + ), ] diff --git a/src/plugins/built_in/tts_voice_plugin/plugin.py b/src/plugins/built_in/tts_voice_plugin/plugin.py index 583d1f64e..305b8b96b 100644 --- a/src/plugins/built_in/tts_voice_plugin/plugin.py +++ b/src/plugins/built_in/tts_voice_plugin/plugin.py @@ -28,7 +28,7 @@ class TTSVoicePlugin(BasePlugin): plugin_description = "基于GPT-SoVITS的文本转语音插件(重构版)" plugin_version = "3.1.2" plugin_author = "Kilo Code & 靚仔" - enable_plugin = False + enable_plugin = True config_file_name = "config.toml" dependencies: ClassVar[list[str]] = [] diff --git a/src/plugins/built_in/web_search_tool/utils/api_key_manager.py b/src/plugins/built_in/web_search_tool/utils/api_key_manager.py index bff72b97e..68dd6af2c 100644 --- a/src/plugins/built_in/web_search_tool/utils/api_key_manager.py +++ b/src/plugins/built_in/web_search_tool/utils/api_key_manager.py @@ -42,7 +42,7 @@ class APIKeyManager(Generic[T]): try: self.clients = [client_factory(key) for key in valid_keys] self.client_cycle = itertools.cycle(self.clients) - logger.info(f"🔑 {service_name} 成功加载 {len(valid_keys)} 个 API 密钥") + logger.info(f" {service_name} 成功加载 {len(valid_keys)} 个 API 密钥") except Exception as e: logger.error(f"❌ 初始化 {service_name} 客户端失败: {e}") self.clients = [] @@ -61,6 +61,7 @@ class APIKeyManager(Generic[T]): if not self.is_available(): return None + assert self.client_cycle is not None return next(self.client_cycle) def get_client_count(self) -> int: diff --git a/src/utils/json_parser.py b/src/utils/json_parser.py index 33a971cfa..4d1bf0ce9 100644 --- a/src/utils/json_parser.py +++ b/src/utils/json_parser.py @@ -58,7 +58,7 @@ def extract_and_parse_json(response: str, *, strict: bool = False) -> dict[str, # 步骤 2: 尝试直接解析 try: result = orjson.loads(cleaned) - logger.debug(f"✅ JSON 直接解析成功,类型: {type(result).__name__}") + logger.debug(f" JSON 直接解析成功,类型: {type(result).__name__}") return result except Exception as direct_error: logger.debug(f"直接解析失败: {type(direct_error).__name__}: {direct_error}") @@ -70,10 +70,10 @@ def extract_and_parse_json(response: str, *, strict: bool = False) -> dict[str, # repair_json 可能返回字符串或已解析的对象 if isinstance(repaired, str): result = orjson.loads(repaired) - logger.debug(f"✅ JSON 修复后解析成功(字符串模式),类型: {type(result).__name__}") + logger.debug(f" JSON 修复后解析成功(字符串模式),类型: {type(result).__name__}") else: result = repaired - logger.debug(f"✅ JSON 修复后解析成功(对象模式),类型: {type(result).__name__}") + logger.debug(f" JSON 修复后解析成功(对象模式),类型: {type(result).__name__}") return result @@ -93,7 +93,7 @@ def extract_and_parse_json(response: str, *, strict: bool = False) -> dict[str, return {} except Exception as e: - logger.error(f"❌ JSON 解析过程出现异常: {type(e).__name__}: {e}") + logger.error(f" JSON 解析过程出现异常: {type(e).__name__}: {e}") if strict: return None return {} if not response.strip().startswith("[") else [] diff --git a/ui_log_adapter.py b/ui_log_adapter.py index d72c94352..bc52d4826 100644 --- a/ui_log_adapter.py +++ b/ui_log_adapter.py @@ -99,8 +99,8 @@ class UILogHandler(logging.Handler): if record.levelname == "DEBUG": return - emoji_map = {"info": "📝", "warning": "⚠️", "error": "❌", "debug": "🔍"} - formatted_msg = f"{emoji_map.get(ui_level, '📝')} {msg}" + emoji_map = {"info": "", "warning": "", "error": "", "debug": ""} + formatted_msg = msg self._send_log_with_retry(formatted_msg, ui_level) # 可选:记录发送状态