From 26e2cf085833067fd854584e021b0209e41e13b9 Mon Sep 17 00:00:00 2001 From: SengokuCola <1026294844@qq.com> Date: Sat, 11 Oct 2025 23:48:56 +0800 Subject: [PATCH] =?UTF-8?q?update:=E5=86=85=E7=BD=AEbetter=5Ffrequency?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 +- plugins/BetterFrequency/README.md | 75 ++++++++++ plugins/BetterFrequency/_manifest.json | 56 +++++++ .../frequency_adjust_action.py | 121 ++++++++++++++++ plugins/BetterFrequency/plugin.py | 137 ++++++++++++++++++ 5 files changed, 392 insertions(+), 1 deletion(-) create mode 100644 plugins/BetterFrequency/README.md create mode 100644 plugins/BetterFrequency/_manifest.json create mode 100644 plugins/BetterFrequency/frequency_adjust_action.py create mode 100644 plugins/BetterFrequency/plugin.py diff --git a/.gitignore b/.gitignore index e43df6d2..a8aeb3f9 100644 --- a/.gitignore +++ b/.gitignore @@ -323,7 +323,9 @@ run_pet.bat !/plugins/emoji_manage_plugin !/plugins/take_picture_plugin !/plugins/deep_think -!/plugins/BetterFrequency +!/plugins/BetterFrequency/ +!/plugins/BetterFrequency/* +!/plugins/BetterFrequenc !/plugins/__init__.py config.toml diff --git a/plugins/BetterFrequency/README.md b/plugins/BetterFrequency/README.md new file mode 100644 index 00000000..6da16772 --- /dev/null +++ b/plugins/BetterFrequency/README.md @@ -0,0 +1,75 @@ +# BetterFrequency 频率控制插件 + +这是一个用于控制MaiBot聊天频率的插件,支持实时调整talk_frequency参数。 + +## 功能特性 + +- 💬 **Talk Frequency控制**: 调整机器人的发言频率 +- 📊 **状态显示**: 实时查看当前频率控制状态 +- ⚡ **实时生效**: 设置后立即生效,无需重启 +- 💾 **不保存消息**: 命令执行反馈不会保存到数据库 +- 🚀 **简化命令**: 支持完整命令和简化命令两种形式 + +## 命令列表 + +### 1. 设置Talk Frequency +``` +/chat talk_frequency <数字> # 完整命令 +/chat t <数字> # 简化命令 +``` +- 功能:设置当前聊天的talk_frequency调整值 +- 参数:支持0到1之间的数值 +- 示例: + - `/chat talk_frequency 1.0` 或 `/chat t 1.0` - 设置发言频率调整为1.0(最高频率) + - `/chat talk_frequency 0.5` 或 `/chat t 0.5` - 设置发言频率调整为0.5 + - `/chat talk_frequency 0.0` 或 `/chat t 0.0` - 设置发言频率调整为0.0(最低频率) + +### 2. 显示当前状态 +``` +/chat show # 完整命令 +/chat s # 简化命令 +``` +- 功能:显示当前聊天的频率控制状态 +- 显示内容: + - 当前talk_frequency值 + - 可用命令提示(包含简化命令) + +## 配置说明 + +插件配置文件 `config.toml` 包含以下选项: + +```toml +[plugin] +name = "better_frequency_plugin" +version = "1.0.0" +enabled = true + +[frequency] +default_talk_adjust = 1.0 # 默认talk_frequency调整值 +max_adjust_value = 1.0 # 最大调整值 +min_adjust_value = 0.0 # 最小调整值 +``` + +## 使用场景 + +- **提高机器人活跃度**: 设置较高的talk_frequency值(接近1.0) +- **降低机器人活跃度**: 设置较低的talk_frequency值(接近0.0) +- **精细调节**: 使用小数进行微调 +- **实时监控**: 通过show命令查看当前状态 +- **快速操作**: 使用简化命令提高操作效率 + +## 注意事项 + +1. 调整值会立即生效,影响当前聊天的机器人行为 +2. 命令执行反馈消息不会保存到数据库 +3. 支持0到1之间的数值 +4. 每个聊天都有独立的频率控制设置 +5. 简化命令和完整命令功能完全相同,可根据个人习惯选择 + +## 技术实现 + +- 基于MaiCore插件系统开发 +- 使用frequency_api进行频率控制操作 +- 使用send_api发送反馈消息 +- 支持异步操作和错误处理 +- 正则表达式支持多种命令格式 diff --git a/plugins/BetterFrequency/_manifest.json b/plugins/BetterFrequency/_manifest.json new file mode 100644 index 00000000..bc07c000 --- /dev/null +++ b/plugins/BetterFrequency/_manifest.json @@ -0,0 +1,56 @@ +{ + "manifest_version": 1, + "name": "发言频率控制插件|BetterFrequency Plugin", + "version": "2.0.0", + "description": "控制聊天频率,支持设置focus_value和talk_frequency调整值,提供完整命令和简化命令", + "author": { + "name": "SengokuCola", + "url": "https://github.com/MaiM-with-u" + }, + "license": "GPL-v3.0-or-later", + + "host_application": { + "min_version": "0.10.3" + }, + "homepage_url": "https://github.com/SengokuCola/BetterFrequency", + "repository_url": "https://github.com/SengokuCola/BetterFrequency", + "keywords": ["frequency", "control", "talk_frequency", "plugin", "shortcut"], + "categories": ["Chat", "Frequency", "Control"], + + "default_locale": "zh-CN", + "locales_path": "_locales", + + "plugin_info": { + "is_built_in": false, + "plugin_type": "frequency", + "components": [ + { + "type": "command", + "name": "set_talk_frequency", + "description": "设置当前聊天的talk_frequency调整值", + "pattern": "/chat talk_frequency <数字> 或 /chat t <数字>" + }, + { + "type": "action", + "name": "frequency_adjust", + "description": "调整当前聊天的发言频率", + "pattern": "/chat frequency_adjust <数字> 或 /chat f <数字>" + }, + { + "type": "command", + "name": "show_frequency", + "description": "显示当前聊天的频率控制状态", + "pattern": "/chat show 或 /chat s" + } + ], + "features": [ + "设置talk_frequency调整值", + "调整当前聊天的发言频率", + "显示当前频率控制状态", + "实时频率控制调整", + "命令执行反馈(不保存消息)", + "支持完整命令和简化命令", + "快速操作支持" + ] + } +} \ No newline at end of file diff --git a/plugins/BetterFrequency/frequency_adjust_action.py b/plugins/BetterFrequency/frequency_adjust_action.py new file mode 100644 index 00000000..9c1e4741 --- /dev/null +++ b/plugins/BetterFrequency/frequency_adjust_action.py @@ -0,0 +1,121 @@ +from typing import Tuple + +# 导入新插件系统 +from src.plugin_system import BaseAction, ActionActivationType + +# 导入依赖的系统组件 +from src.common.logger import get_logger + +# 导入API模块 +from src.plugin_system.apis import frequency_api, send_api, config_api, generator_api + +logger = get_logger("frequency_adjust") + + +class FrequencyAdjustAction(BaseAction): + """频率调节动作 - 调整聊天发言频率""" + + activation_type = ActionActivationType.LLM_JUDGE + parallel_action = False + + # 动作基本信息 + action_name = "frequency_adjust" + + action_description = "调整当前聊天的发言频率" + + # 动作参数定义 + action_parameters = { + "direction": "调整方向:'increase'(增加)或'decrease'(降低)", + } + + # 动作使用场景 + bot_name = config_api.get_global_config("bot.nickname") + + + action_require = [ + f"当用户提到 {bot_name} 太安静或太活跃时使用", + f"有人提到 {bot_name} 的发言太多或太少", + f"需要根据聊天氛围调整 {bot_name} 的活跃度", + ] + + # 关联类型 + associated_types = ["text"] + + async def execute(self) -> Tuple[bool, str]: + """执行频率调节动作""" + try: + # 1. 获取动作参数 + direction = self.action_data.get("direction") + # multiply = 1.2 + # multiply = self.action_data.get("multiply") + + if not direction: + error_msg = "缺少必要的参数:direction或multiply" + logger.error(f"{self.log_prefix} {error_msg}") + return False, error_msg + + # 2. 获取当前频率值 + current_frequency = frequency_api.get_current_talk_frequency(self.chat_id) + + # 3. 计算新的频率值(使用比率而不是绝对值) + # calculated_frequency = current_frequency * multiply + if direction == "increase": + calculated_frequency = current_frequency * 1.2 + if calculated_frequency > 1.0: + new_frequency = 1.0 + action_desc = f"增加到最大值" + # 记录超出限制的action + logger.warning(f"{self.log_prefix} 尝试调整频率超出最大值: current={current_frequency:.2f}, calculated={calculated_frequency:.2f}") + await self.store_action_info( + action_build_into_prompt=True, + action_prompt_display=f"你尝试调整发言频率到{calculated_frequency:.2f},但最大值只能为1.0,已设置为最大值", + action_done=True, + ) + return True, f"调整发言频率超出限制: {current_frequency:.2f} → {new_frequency:.2f}" + else: + new_frequency = calculated_frequency + action_desc = f"增加" + elif direction == "decrease": + calculated_frequency = current_frequency * 0.8 + new_frequency = max(0.0, calculated_frequency) + action_desc = f"降低" + else: + error_msg = f"无效的调整方向: {direction}" + logger.error(f"{self.log_prefix} {error_msg}") + return False, error_msg + + # 4. 设置新的频率值 + frequency_api.set_talk_frequency_adjust(self.chat_id, new_frequency) + + # 5. 发送反馈消息 + feedback_msg = f"已{action_desc}发言频率:{current_frequency:.2f} → {new_frequency:.2f}" + result_status, data = await generator_api.rewrite_reply( + chat_stream=self.chat_stream, + reply_data={ + "raw_reply": feedback_msg, + "reason": "表达自己已经调整了发言频率,不一定要说具体数值,可以有趣一些", + }, + ) + + + if result_status: + for reply_seg in data.reply_set.reply_data: + send_data = reply_seg.content + await self.send_text(send_data) + logger.info(f"{self.log_prefix} {send_data}") + + # 6. 存储动作信息(仅在未超出限制时) + if calculated_frequency <= 1.0: + await self.store_action_info( + action_build_into_prompt=True, + action_prompt_display=f"你{action_desc}了发言频率,从{current_frequency:.2f}调整到{new_frequency:.2f}", + action_done=True, + ) + + return True, f"成功调整发言频率: {current_frequency:.2f} → {new_frequency:.2f}" + + except Exception as e: + error_msg = f"频率调节失败: {str(e)}" + logger.error(f"{self.log_prefix} {error_msg}", exc_info=True) + await self.send_text("频率调节失败") + return False, error_msg \ No newline at end of file diff --git a/plugins/BetterFrequency/plugin.py b/plugins/BetterFrequency/plugin.py new file mode 100644 index 00000000..2c50cb5e --- /dev/null +++ b/plugins/BetterFrequency/plugin.py @@ -0,0 +1,137 @@ +from typing import List, Tuple, Type, Any, Optional +from src.plugin_system import ( + BasePlugin, + register_plugin, + BaseCommand, + ComponentInfo, + ConfigField +) +from src.plugin_system.apis import send_api, frequency_api +from .frequency_adjust_action import FrequencyAdjustAction + + +class SetTalkFrequencyCommand(BaseCommand): + """设置当前聊天的talk_frequency值""" + command_name = "set_talk_frequency" + command_description = "设置当前聊天的talk_frequency值:/chat talk_frequency <数字> 或 /chat t <数字>" + command_pattern = r"^/chat\s+(?:talk_frequency|t)\s+(?P[+-]?\d*\.?\d+)$" + + async def execute(self) -> Tuple[bool, Optional[str], bool]: + try: + # 获取命令参数 - 使用命名捕获组 + if not self.matched_groups or "value" not in self.matched_groups: + return False, "命令格式错误", False + + value_str = self.matched_groups["value"] + if not value_str: + return False, "无法获取数值参数", False + + value = float(value_str) + + # 获取聊天流ID + if not self.message.chat_stream or not hasattr(self.message.chat_stream, "stream_id"): + return False, "无法获取聊天流信息", False + + chat_id = self.message.chat_stream.stream_id + + # 设置talk_frequency + frequency_api.set_talk_frequency_adjust(chat_id, value) + + # 发送反馈消息(不保存到数据库) + await send_api.text_to_stream( + f"已设置当前聊天的talk_frequency调整值为: {value}", + chat_id, + storage_message=False + ) + + return True, None, False + + except ValueError: + error_msg = "数值格式错误,请输入有效的数字" + await self.send_text(error_msg, storage_message=False) + return False, error_msg, False + except Exception as e: + error_msg = f"设置talk_frequency失败: {str(e)}" + await self.send_text(error_msg, storage_message=False) + return False, error_msg, False + + +class ShowFrequencyCommand(BaseCommand): + """显示当前聊天的频率控制状态""" + command_name = "show_frequency" + command_description = "显示当前聊天的频率控制状态:/chat show 或 /chat s" + command_pattern = r"^/chat\s+(?:show|s)$" + + async def execute(self) -> Tuple[bool, Optional[str], bool]: + try: + # 获取聊天流ID + if not self.message.chat_stream or not hasattr(self.message.chat_stream, "stream_id"): + return False, "无法获取聊天流信息", False + + chat_id = self.message.chat_stream.stream_id + + # 获取当前频率控制状态 + current_talk_frequency = frequency_api.get_current_talk_frequency(chat_id) + talk_frequency_adjust = frequency_api.get_talk_frequency_adjust(chat_id) + + # 构建显示消息 + status_msg = f"""当前聊天频率控制状态 +Talk Frequency (发言频率): + • 当前值: {current_talk_frequency:.2f} + +使用命令: + • /chat talk_frequency <数字> 或 /chat t <数字> - 设置发言频率调整 + • /chat show 或 /chat s - 显示当前状态""" + + # 发送状态消息(不保存到数据库) + await send_api.text_to_stream(status_msg, chat_id, storage_message=False) + + return True, None, False + + except Exception as e: + error_msg = f"获取频率控制状态失败: {str(e)}" + # 使用内置的send_text方法发送错误消息 + await self.send_text(error_msg, storage_message=False) + return False, error_msg, False + + +# ===== 插件注册 ===== + + +@register_plugin +class BetterFrequencyPlugin(BasePlugin): + """BetterFrequency插件 - 控制聊天频率的插件""" + + # 插件基本信息 + plugin_name: str = "better_frequency_plugin" + enable_plugin: bool = True + dependencies: List[str] = [] + python_dependencies: List[str] = [] + config_file_name: str = "config.toml" + + # 配置节描述 + config_section_descriptions = { + "plugin": "插件基本信息", + "frequency": "频率控制配置" + } + + # 配置Schema定义 + config_schema: dict = { + "plugin": { + "name": ConfigField(type=str, default="better_frequency_plugin", description="插件名称"), + "version": ConfigField(type=str, default="1.0.0", description="插件版本"), + "enabled": ConfigField(type=bool, default=True, description="是否启用插件"), + }, + "frequency": { + "default_talk_adjust": ConfigField(type=float, default=1.0, description="默认talk_frequency调整值"), + "max_adjust_value": ConfigField(type=float, default=1.0, description="最大调整值"), + "min_adjust_value": ConfigField(type=float, default=0.0, description="最小调整值"), + } + } + + def get_plugin_components(self) -> List[Tuple[ComponentInfo, Type]]: + return [ + (SetTalkFrequencyCommand.get_command_info(), SetTalkFrequencyCommand), + (ShowFrequencyCommand.get_command_info(), ShowFrequencyCommand), + (FrequencyAdjustAction.get_action_info(), FrequencyAdjustAction), + ]