refc:重构插件api,补全文档,合并expressor和replyer,分离reply和sender,新log浏览器

This commit is contained in:
SengokuCola
2025-06-19 20:20:34 +08:00
parent 7e05ede846
commit ab28b94e33
63 changed files with 5285 additions and 8316 deletions

View File

@@ -1,27 +0,0 @@
# 核心动作插件配置文件
[plugin]
name = "core_actions"
description = "系统核心动作插件"
version = "0.2"
author = "built-in"
enabled = true
[no_reply]
# 等待新消息的超时时间(秒)
waiting_timeout = 1200
[emoji]
# 表情动作配置
enabled = true
# 在Normal模式下的随机激活概率
random_probability = 0.1
# 是否启用智能表情选择
smart_selection = true
# LLM判断相关配置
[emoji.llm_judge]
# 是否启用LLM智能判断
enabled = true
# 自定义判断提示词(可选)
custom_prompt = ""

View File

@@ -5,18 +5,18 @@
这是系统的内置插件,提供基础的聊天交互功能
"""
import re
from typing import List, Tuple, Type, Optional
import time
from typing import List, Tuple, Type
# 导入新插件系统
from src.plugin_system import BasePlugin, register_plugin, BaseAction, ComponentInfo, ActionActivationType, ChatMode
from src.plugin_system.base.base_command import BaseCommand
from src.plugin_system.base.config_types import ConfigField
# 导入依赖的系统组件
from src.common.logger import get_logger
from src.chat.heart_flow.observation.chatting_observation import ChattingObservation
from src.chat.focus_chat.hfc_utils import create_empty_anchor_message
# 导入API模块 - 标准Python包方式
from src.plugin_system.apis import emoji_api, generator_api, message_api
logger = get_logger("core_actions")
@@ -35,11 +35,11 @@ class ReplyAction(BaseAction):
# 动作基本信息
action_name = "reply"
action_description = "参与聊天回复,处理文本和表情的发送"
action_description = "参与聊天回复,发送文本进行表达"
# 动作参数定义
action_parameters = {
"reply_to": "如果是明确回复某个人的发言请在reply_to参数中指定格式用户名:发言内容如果不是reply_to的值设为none"
"reply_to": "你要回复的对方的发言内容,格式:(用户名:发言内容),可以为none"
}
# 动作使用场景
@@ -52,40 +52,52 @@ class ReplyAction(BaseAction):
"""执行回复动作"""
logger.info(f"{self.log_prefix} 决定回复: {self.reasoning}")
start_time = self.action_data.get("loop_start_time", time.time())
try:
# 获取聊天观察
chatting_observation = self._get_chatting_observation()
if not chatting_observation:
return False, "未找到聊天观察"
# 处理回复目标
anchor_message = await self._resolve_reply_target(chatting_observation)
# 获取回复器服务
replyer = self.api.get_service("replyer")
if not replyer:
logger.error(f"{self.log_prefix} 未找到回复器服务")
return False, "回复器服务不可用"
# 执行回复
success, reply_set = await replyer.deal_reply(
cycle_timers=self.cycle_timers,
success, reply_set = await generator_api.generate_reply(
chat_stream=self.chat_stream,
action_data=self.action_data,
anchor_message=anchor_message,
reasoning=self.reasoning,
thinking_id=self.thinking_id,
platform=self.platform,
chat_id=self.chat_id,
is_group=self.is_group
)
# 检查从start_time以来的新消息数量
# 获取动作触发时间或使用默认值
current_time = time.time()
new_message_count = message_api.count_new_messages(
chat_id=self.chat_id,
start_time=start_time,
end_time=current_time
)
# 根据新消息数量决定是否使用reply_to
need_reply = new_message_count >= 4
logger.info(f"{self.log_prefix}{start_time}{current_time}共有{new_message_count}条新消息,{'使用' if need_reply else '不使用'}reply_to")
# 构建回复文本
reply_text = self._build_reply_text(reply_set)
reply_text = ""
first_reply = False
for reply_seg in reply_set:
data = reply_seg[1]
if not first_reply and need_reply:
await self.send_text(
content=data,
reply_to=self.action_data.get("reply_to", "")
)
else:
await self.send_text(content=data)
first_reply = True
reply_text += data
# 存储动作记录
await self.api.store_action_info(
await self.store_action_info(
action_build_into_prompt=False,
action_prompt_display=reply_text,
action_done=True,
thinking_id=self.thinking_id,
action_data=self.action_data,
)
# 重置NoReplyAction的连续计数器
@@ -97,47 +109,6 @@ class ReplyAction(BaseAction):
logger.error(f"{self.log_prefix} 回复动作执行失败: {e}")
return False, f"回复失败: {str(e)}"
def _get_chatting_observation(self) -> Optional[ChattingObservation]:
"""获取聊天观察对象"""
observations = self.api.get_service("observations") or []
for obs in observations:
if isinstance(obs, ChattingObservation):
return obs
return None
async def _resolve_reply_target(self, chatting_observation: ChattingObservation):
"""解析回复目标消息"""
reply_to = self.action_data.get("reply_to", "none")
if ":" in reply_to or "" in reply_to:
# 解析回复目标格式:用户名:消息内容
parts = re.split(pattern=r"[:]", string=reply_to, maxsplit=1)
if len(parts) == 2:
target = parts[1].strip()
anchor_message = chatting_observation.search_message_by_text(target)
if anchor_message:
chat_stream = self.api.get_service("chat_stream")
if chat_stream:
anchor_message.update_chat_stream(chat_stream)
return anchor_message
# 创建空锚点消息
logger.info(f"{self.log_prefix} 未找到锚点消息,创建占位符")
chat_stream = self.api.get_service("chat_stream")
if chat_stream:
return await create_empty_anchor_message(chat_stream.platform, chat_stream.group_info, chat_stream)
return None
def _build_reply_text(self, reply_set) -> str:
"""构建回复文本"""
reply_text = ""
if reply_set:
for reply in reply_set:
reply_type = reply[0]
data = reply[1]
if reply_type in ["text", "emoji"]:
reply_text += data
return reply_text
class NoReplyAction(BaseAction):
@@ -178,30 +149,26 @@ class NoReplyAction(BaseAction):
count = NoReplyAction._consecutive_count
# 计算本次等待时间
timeout = self._calculate_waiting_time(count)
if count <= len(self._waiting_stages):
# 前3次使用预设时间
stage_time = self._waiting_stages[count - 1]
# 如果WAITING_TIME_THRESHOLD更小则使用它
timeout = min(stage_time, self.waiting_timeout)
else:
# 第4次及以后使用WAITING_TIME_THRESHOLD
timeout = self.waiting_timeout
logger.info(f"{self.log_prefix} 选择不回复(第{count}次连续),等待新消息中... (超时: {timeout}秒)")
# 等待新消息或达到时间上限
result = await self.api.wait_for_new_message(timeout)
result = await self.wait_for_new_message(timeout)
# 如果有新消息或者超时都不重置计数器因为可能还会继续no_reply
return result
except Exception as e:
logger.error(f"{self.log_prefix} 不回复动作执行失败: {e}")
return False, f"不回复动作执行失败: {e}"
def _calculate_waiting_time(self, consecutive_count: int) -> int:
"""根据连续次数计算等待时间"""
if consecutive_count <= len(self._waiting_stages):
# 前3次使用预设时间
stage_time = self._waiting_stages[consecutive_count - 1]
# 如果WAITING_TIME_THRESHOLD更小则使用它
return min(stage_time, self.waiting_timeout)
else:
# 第4次及以后使用WAITING_TIME_THRESHOLD
return self.waiting_timeout
return False, f"不回复动作执行失败: {e}"
@classmethod
def reset_consecutive_count(cls):
@@ -248,56 +215,33 @@ class EmojiAction(BaseAction):
logger.info(f"{self.log_prefix} 决定发送表情")
try:
# 创建空锚点消息
anchor_message = await self._create_anchor_message()
if not anchor_message:
return False, "无法创建锚点消息"
# 获取回复器服务
replyer = self.api.get_service("replyer")
if not replyer:
logger.error(f"{self.log_prefix} 未找到回复器服务")
return False, "回复器服务不可用"
# 执行表情处理
success, reply_set = await replyer.deal_emoji(
cycle_timers=self.cycle_timers,
action_data=self.action_data,
anchor_message=anchor_message,
thinking_id=self.thinking_id,
)
# 构建回复文本
reply_text = self._build_reply_text(reply_set)
# 1. 根据描述选择表情包
description = self.action_data.get("description", "")
emoji_result = await emoji_api.get_by_description(description)
if not emoji_result:
logger.warning(f"{self.log_prefix} 未找到匹配描述 '{description}' 的表情包")
return False, f"未找到匹配 '{description}' 的表情包"
emoji_base64, emoji_description, matched_emotion = emoji_result
logger.info(f"{self.log_prefix} 找到表情包: {emoji_description}, 匹配情感: {matched_emotion}")
# 使用BaseAction的便捷方法发送表情包
success = await self.send_emoji(emoji_base64)
if not success:
logger.error(f"{self.log_prefix} 表情包发送失败")
return False, "表情包发送失败"
# 重置NoReplyAction的连续计数器
NoReplyAction.reset_consecutive_count()
return success, reply_text
return True, f"发送表情包: {emoji_description}"
except Exception as e:
logger.error(f"{self.log_prefix} 表情动作执行失败: {e}")
return False, f"表情发送失败: {str(e)}"
async def _create_anchor_message(self):
"""创建锚点消息"""
chat_stream = self.api.get_service("chat_stream")
if chat_stream:
logger.info(f"{self.log_prefix} 为表情包创建占位符")
return await create_empty_anchor_message(chat_stream.platform, chat_stream.group_info, chat_stream)
return None
def _build_reply_text(self, reply_set) -> str:
"""构建回复文本"""
reply_text = ""
if reply_set:
for reply in reply_set:
reply_type = reply[0]
data = reply[1]
if reply_type in ["text", "emoji"]:
reply_text += data
return reply_text
class ChangeToFocusChatAction(BaseAction):
"""切换到专注聊天动作 - 从普通模式切换到专注模式"""
@@ -314,6 +258,7 @@ class ChangeToFocusChatAction(BaseAction):
# 动作参数定义
action_parameters = {}
apex = 111
# 动作使用场景
action_require = [
"你想要进入专注聊天模式",
@@ -437,8 +382,6 @@ class CoreActionsPlugin(BasePlugin):
"enable_emoji": ConfigField(type=bool, default=True, description="是否启用'表情'动作"),
"enable_change_to_focus": ConfigField(type=bool, default=True, description="是否启用'切换到专注模式'动作"),
"enable_exit_focus": ConfigField(type=bool, default=True, description="是否启用'退出专注模式'动作"),
"enable_ping_command": ConfigField(type=bool, default=True, description="是否启用'/ping'测试命令"),
"enable_log_command": ConfigField(type=bool, default=True, description="是否启用'/log'日志命令"),
},
"no_reply": {
"waiting_timeout": ConfigField(
@@ -482,73 +425,137 @@ class CoreActionsPlugin(BasePlugin):
components.append((ExitFocusChatAction.get_action_info(), ExitFocusChatAction))
if self.get_config("components.enable_change_to_focus", True):
components.append((ChangeToFocusChatAction.get_action_info(), ChangeToFocusChatAction))
if self.get_config("components.enable_ping_command", True):
components.append(
(PingCommand.get_command_info(name="ping", description="测试机器人响应,拦截后续处理"), PingCommand)
)
if self.get_config("components.enable_log_command", True):
components.append(
(LogCommand.get_command_info(name="log", description="记录消息到日志,不拦截后续处理"), LogCommand)
)
# components.append((DeepReplyAction.get_action_info(), DeepReplyAction))
return components
# ===== 示例Command组件 =====
class PingCommand(BaseCommand):
"""Ping命令 - 测试响应,拦截消息处理"""
command_pattern = r"^/ping(\s+(?P<message>.+))?$"
command_help = "测试机器人响应 - 拦截后续处理"
command_examples = ["/ping", "/ping 测试消息"]
intercept_message = True # 拦截消息,不继续处理
async def execute(self) -> Tuple[bool, Optional[str]]:
"""执行ping命令"""
try:
message = self.matched_groups.get("message", "")
reply_text = f"🏓 Pong! {message}" if message else "🏓 Pong!"
await self.send_text(reply_text)
return True, f"发送ping响应: {reply_text}"
except Exception as e:
logger.error(f"Ping命令执行失败: {e}")
return False, f"执行失败: {str(e)}"
class LogCommand(BaseCommand):
"""日志命令 - 记录消息但不拦截后续处理"""
# class DeepReplyAction(BaseAction):
# """回复动作 - 参与聊天回复"""
command_pattern = r"^/log(\s+(?P<level>debug|info|warn|error))?$"
command_help = "记录当前消息到日志 - 不拦截后续处理"
command_examples = ["/log", "/log info", "/log debug"]
intercept_message = False # 不拦截消息,继续后续处理
# # 激活设置
# focus_activation_type = ActionActivationType.ALWAYS
# normal_activation_type = ActionActivationType.NEVER
# mode_enable = ChatMode.FOCUS
# parallel_action = False
async def execute(self) -> Tuple[bool, Optional[str]]:
"""执行日志命令"""
try:
level = self.matched_groups.get("level", "info")
user_nickname = self.message.message_info.user_info.user_nickname
content = self.message.processed_plain_text
# # 动作基本信息
# action_name = "deep_reply"
# action_description = "参与聊天回复,关注某个话题,对聊天内容进行深度思考,给出回复"
log_message = f"[{level.upper()}] 用户 {user_nickname}: {content}"
# # 动作参数定义
# action_parameters = {
# "topic": "想要思考的话题"
# }
# 根据级别记录日志
if level == "debug":
logger.debug(log_message)
elif level == "warn":
logger.warning(log_message)
elif level == "error":
logger.error(log_message)
else:
logger.info(log_message)
# # 动作使用场景
# action_require = ["有些问题需要深度思考", "某个问题可能涉及多个方面", "某个问题涉及专业领域或者需要专业知识","这个问题讨论的很激烈,需要深度思考"]
# 不发送回复,让消息继续处理
return True, f"已记录到{level}级别日志"
# # 关联类型
# associated_types = ["text"]
except Exception as e:
logger.error(f"Log命令执行失败: {e}")
return False, f"执行失败: {str(e)}"
# async def execute(self) -> Tuple[bool, str]:
# """执行回复动作"""
# logger.info(f"{self.log_prefix} 决定深度思考")
# try:
# # 获取聊天观察
# chatting_observation = self._get_chatting_observation()
# if not chatting_observation:
# return False, "未找到聊天观察"
# talking_message_str = chatting_observation.talking_message_str
# # 处理回复目标
# chat_stream = self.api.get_service("chat_stream")
# anchor_message = await create_empty_anchor_message(chat_stream.platform, chat_stream.group_info, chat_stream)
# llm_model = self.api.get_available_models().replyer_1
# prompt = f"""
# {talking_message_str}
# 在上面的聊天中,你对{self.action_data.get("topic", "")}感兴趣,形成深刻观点,请你思考,总结成一份学术论文,APA标准格式
# """
# success, response, reasoning, model_name = await self.api.generate_with_model(prompt, llm_model)
# print(prompt)
# print(f"DeepReplyAction: {response}")
# # prompt = f"""
# # {talking_message_str}
# # 在上面的聊天中,你对{self.action_data.get("topic", "")}感兴趣,请你思考
# # """
# extra_info_block = self.action_data.get("extra_info_block", "")
# extra_info_block += response
# # extra_info_block += f"\n--------------------------------\n注意这是最重要的内容你现在可以用比较长的篇幅来表达你的观点不要只回复一个字或者几个字\n由于你进入了深度思考模式所以下方聊天记录的回复要求不再适用请你自由的表达不论字数长短限制\n\n--------------------------------\n注意你现在可以用比较长的篇幅来表达你的观点不要只回复一个字或者几个字\n由于你进入了深度思考模式所以下方聊天记录的回复要求不再适用请你自由的表达不论字数长短限制\n"
# # extra_info_block += f"\n--------------------------------\n注意优先关注这句你现在可以用比较长的篇幅来表达你的观点不要只回复一个字或者几个字\n由于你进入了深度思考模式所以下方聊天记录的回复要求不再适用请你自由的表达不论字数长短限制\n\n--------------------------------\n注意你现在可以用比较长的篇幅来表达你的观点不要只回复一个字或者几个字\n由于你进入了深度思考模式所以其他的回复要求不再适用请你自由的表达不论字数长短限制\n"
# self.action_data["extra_info_block"] = extra_info_block
# # 获取回复器服务
# # replyer = self.api.get_service("replyer")
# # if not replyer:
# # logger.error(f"{self.log_prefix} 未找到回复器服务")
# # return False, "回复器服务不可用"
# # await self.send_message_by_expressor(extra_info_block)
# await self.send_text(extra_info_block)
# # 执行回复
# # success, reply_set = await replyer.deal_reply(
# # cycle_timers=self.cycle_timers,
# # action_data=self.action_data,
# # anchor_message=anchor_message,
# # reasoning=self.reasoning,
# # thinking_id=self.thinking_id,
# # )
# # 构建回复文本
# reply_text = "self._build_reply_text(reply_set)"
# # 存储动作记录
# await self.api.store_action_info(
# action_build_into_prompt=False,
# action_prompt_display=reply_text,
# action_done=True,
# thinking_id=self.thinking_id,
# action_data=self.action_data,
# )
# # 重置NoReplyAction的连续计数器
# NoReplyAction.reset_consecutive_count()
# return success, reply_text
# except Exception as e:
# logger.error(f"{self.log_prefix} 回复动作执行失败: {e}")
# return False, f"回复失败: {str(e)}"
# def _get_chatting_observation(self) -> Optional[ChattingObservation]:
# """获取聊天观察对象"""
# observations = self.api.get_service("observations") or []
# for obs in observations:
# if isinstance(obs, ChattingObservation):
# return obs
# return None
# def _build_reply_text(self, reply_set) -> str:
# """构建回复文本"""
# reply_text = ""
# if reply_set:
# for reply in reply_set:
# data = reply[1]
# reply_text += data
# return reply_text

View File

@@ -26,6 +26,8 @@ from src.plugin_system.base.base_command import BaseCommand
from src.plugin_system.base.component_types import ComponentInfo, ActionActivationType, ChatMode
from src.plugin_system.base.config_types import ConfigField
from src.common.logger import get_logger
# 导入配置API可选的简便方法
from src.plugin_system.apis import person_api, generator_api
logger = get_logger("mute_plugin")
@@ -110,8 +112,8 @@ class MuteAction(BaseAction):
return False, error_msg
# 获取时长限制配置
min_duration = self.api.get_config("mute.min_duration", 60)
max_duration = self.api.get_config("mute.max_duration", 2592000)
min_duration = self.get_config("mute.min_duration", 60)
max_duration = self.get_config("mute.max_duration", 2592000)
# 验证时长格式并转换
try:
@@ -133,72 +135,65 @@ class MuteAction(BaseAction):
except (ValueError, TypeError):
error_msg = f"禁言时长格式无效: {duration}"
logger.error(f"{self.log_prefix} {error_msg}")
await self.send_text("禁言时长必须是数字哦~")
# await self.send_text("禁言时长必须是数字哦~")
return False, error_msg
# 获取用户ID
try:
platform, user_id = await self.api.get_user_id_by_person_name(target)
except Exception as e:
error_msg = f"查找用户ID时出错: {e}"
logger.error(f"{self.log_prefix} {error_msg}")
await self.send_text("查找用户信息时出现问题~")
return False, error_msg
person_id = person_api.get_person_id_by_name(target)
user_id = await person_api.get_person_value(person_id,"user_id")
if not user_id:
error_msg = f"未找到用户 {target} 的ID"
await self.send_text(f"找不到 {target} 这个人呢~")
logger.error(f"{self.log_prefix} {error_msg}")
return False, error_msg
# 格式化时长显示
enable_formatting = self.api.get_config("mute.enable_duration_formatting", True)
enable_formatting = self.get_config("mute.enable_duration_formatting", True)
time_str = self._format_duration(duration_int) if enable_formatting else f"{duration_int}"
# 获取模板化消息
message = self._get_template_message(target, time_str, reason)
# await self.send_text(message)
await self.send_message_by_expressor(message)
result_status,result_message = await generator_api.rewrite_reply(
chat_stream=self.chat_stream,
reply_data={
"raw_reply": message,
"reason": reason,
}
)
if result_status:
for reply_seg in result_message:
data = reply_seg[1]
await self.send_text(data)
# 发送群聊禁言命令
success = await self.send_command(
command_name="GROUP_BAN",
args={"qq_id": str(user_id), "duration": str(duration_int)},
display_message="发送禁言命令",
storage_message=False
)
if success:
logger.info(f"{self.log_prefix} 成功发送禁言命令,用户 {target}({user_id}),时长 {duration_int}")
# 存储动作信息
await self.api.store_action_info(
await self.store_action_info(
action_build_into_prompt=True,
action_prompt_display=f"尝试禁言了用户 {target},时长 {time_str},原因:{reason}",
action_done=True,
thinking_id=self.thinking_id,
action_data={
"target": target,
"user_id": user_id,
"duration": duration_int,
"duration_str": time_str,
"reason": reason,
},
)
return True, f"成功禁言 {target},时长 {time_str}"
else:
error_msg = "发送禁言命令失败"
logger.error(f"{self.log_prefix} {error_msg}")
await self.send_text("执行禁言动作失败")
return False, error_msg
def _get_template_message(self, target: str, duration_str: str, reason: str) -> str:
"""获取模板化的禁言消息"""
templates = self.api.get_config(
"mute.templates",
[
"好的,禁言 {target} {duration},理由:{reason}",
"收到,对 {target} 执行禁言 {duration},因为{reason}",
"明白了,禁言 {target} {duration},原因是{reason}",
],
templates = self.get_config(
"mute.templates"
)
template = random.choice(templates)
@@ -258,8 +253,8 @@ class MuteCommand(BaseCommand):
return False, "参数不完整"
# 获取时长限制配置
min_duration = self.api.get_config("mute.min_duration", 60)
max_duration = self.api.get_config("mute.max_duration", 2592000)
min_duration = self.get_config("mute.min_duration", 60)
max_duration = self.get_config("mute.max_duration", 2592000)
# 验证时长
try:
@@ -281,19 +276,16 @@ class MuteCommand(BaseCommand):
return False, "时长格式错误"
# 获取用户ID
try:
platform, user_id = await self.api.get_user_id_by_person_name(target)
except Exception as e:
logger.error(f"{self.log_prefix} 查找用户ID时出错: {e}")
await self.send_text("❌ 查找用户信息时出现问题")
return False, str(e)
person_id = person_api.get_person_id_by_name(target)
user_id = person_api.get_person_value(person_id, "user_id")
if not user_id:
error_msg = f"未找到用户 {target} 的ID"
await self.send_text(f"❌ 找不到用户: {target}")
return False, "用户不存在"
logger.error(f"{self.log_prefix} {error_msg}")
return False, error_msg
# 格式化时长显示
enable_formatting = self.api.get_config("mute.enable_duration_formatting", True)
enable_formatting = self.get_config("mute.enable_duration_formatting", True)
time_str = self._format_duration(duration_int) if enable_formatting else f"{duration_int}"
logger.info(f"{self.log_prefix} 执行禁言命令: {target}({user_id}) -> {time_str}")
@@ -323,14 +315,7 @@ class MuteCommand(BaseCommand):
def _get_template_message(self, target: str, duration_str: str, reason: str) -> str:
"""获取模板化的禁言消息"""
templates = self.api.get_config(
"mute.templates",
[
"✅ 已禁言 {target} {duration},理由:{reason}",
"🔇 对 {target} 执行禁言 {duration},因为{reason}",
"⛔ 禁言 {target} {duration},原因:{reason}",
],
)
templates = self.get_config("mute.templates")
template = random.choice(templates)
return template.format(target=target, duration=duration_str, reason=reason)

View File

@@ -1,534 +0,0 @@
"""
拍照插件
功能特性:
- Action: 生成一张自拍照prompt由人设和模板生成
- Command: 展示最近生成的照片
#此插件并不完善
#此插件并不完善
#此插件并不完善
#此插件并不完善
#此插件并不完善
#此插件并不完善
#此插件并不完善
包含组件:
- 拍照Action - 生成自拍照
- 展示照片Command - 展示最近生成的照片
"""
from typing import List, Tuple, Type, Optional
import random
import datetime
import json
import os
import asyncio
import urllib.request
import urllib.error
import base64
import traceback
from src.plugin_system.base.base_plugin import BasePlugin, register_plugin
from src.plugin_system.base.base_action import BaseAction
from src.plugin_system.base.base_command import BaseCommand
from src.plugin_system.base.component_types import ComponentInfo, ActionActivationType, ChatMode
from src.plugin_system.base.config_types import ConfigField
from src.common.logger import get_logger
logger = get_logger("take_picture_plugin")
# 定义数据目录常量
DATA_DIR = os.path.join("data", "take_picture_data")
# 确保数据目录存在
os.makedirs(DATA_DIR, exist_ok=True)
# 创建全局锁
file_lock = asyncio.Lock()
class TakePictureAction(BaseAction):
"""生成一张自拍照"""
focus_activation_type = ActionActivationType.KEYWORD
normal_activation_type = ActionActivationType.KEYWORD
mode_enable = ChatMode.ALL
parallel_action = False
action_name = "take_picture"
action_description = "生成一张用手机拍摄,比如自拍或者近照"
activation_keywords = ["拍张照", "自拍", "发张照片", "看看你", "你的照片"]
keyword_case_sensitive = False
action_parameters = {}
action_require = [
"当用户想看你的照片时使用",
"当用户让你发自拍时使用"
"当想随手拍眼前的场景时使用"
]
associated_types = ["text","image"]
# 内置的Prompt模板如果配置文件中没有定义将使用这些模板
DEFAULT_PROMPT_TEMPLATES = [
"极其频繁无奇的iPhone自拍照没有明确的主体或构图感就是随手一拍的快照照片略带运动模糊阳光或室内打光不均匀导致的轻微曝光过度整体呈现出一种刻意的平庸感就像是从口袋里拿手机时不小心拍到的一张自拍。主角是{name}{personality}"
]
# 简单的请求缓存,避免短时间内重复请求
_request_cache = {}
async def execute(self) -> Tuple[bool, Optional[str]]:
logger.info(f"{self.log_prefix} 执行拍照动作")
try:
# 配置验证
http_base_url = self.api.get_config("api.base_url")
http_api_key = self.api.get_config("api.volcano_generate_api_key")
if not (http_base_url and http_api_key):
error_msg = "抱歉照片生成功能所需的API配置如API地址或密钥不完整无法提供服务。"
await self.send_text(error_msg)
logger.error(f"{self.log_prefix} HTTP调用配置缺失: base_url 或 volcano_generate_api_key.")
return False, "API配置不完整"
# API密钥验证
if http_api_key == "YOUR_DOUBAO_API_KEY_HERE":
error_msg = "照片生成功能尚未配置请设置正确的API密钥。"
await self.send_text(error_msg)
logger.error(f"{self.log_prefix} API密钥未配置")
return False, "API密钥未配置"
# 获取全局配置信息
bot_nickname = self.api.get_global_config("bot.nickname", "麦麦")
bot_personality = self.api.get_global_config("personality.personality_core", "")
personality_sides = self.api.get_global_config("personality.personality_sides", [])
if personality_sides:
bot_personality += random.choice(personality_sides)
# 准备模板变量
template_vars = {
"name": bot_nickname,
"personality": bot_personality
}
logger.info(f"{self.log_prefix} 使用的全局配置: name={bot_nickname}, personality={bot_personality}")
# 尝试从配置文件获取模板,如果没有则使用默认模板
templates = self.api.get_config("picture.prompt_templates", self.DEFAULT_PROMPT_TEMPLATES)
if not templates:
logger.warning(f"{self.log_prefix} 未找到有效的提示词模板,使用默认模板")
templates = self.DEFAULT_PROMPT_TEMPLATES
prompt_template = random.choice(templates)
# 填充模板
final_prompt = prompt_template.format(**template_vars)
logger.info(f"{self.log_prefix} 生成的最终Prompt: {final_prompt}")
# 从配置获取参数
model = self.api.get_config("picture.default_model", "doubao-seedream-3-0-t2i-250415")
style = self.api.get_config("picture.default_style", "动漫")
size = self.api.get_config("picture.default_size", "1024x1024")
watermark = self.api.get_config("picture.default_watermark", True)
guidance_scale = self.api.get_config("picture.default_guidance_scale", 2.5)
seed = self.api.get_config("picture.default_seed", 42)
# 检查缓存
enable_cache = self.api.get_config("storage.enable_cache", True)
if enable_cache:
cache_key = self._get_cache_key(final_prompt, model, size)
if cache_key in self._request_cache:
cached_result = self._request_cache[cache_key]
logger.info(f"{self.log_prefix} 使用缓存的图片结果")
await self.send_text("我之前拍过类似的照片,用之前的结果~")
# 直接发送缓存的结果
send_success = await self._send_image(cached_result)
if send_success:
await self.send_text("这是我的照片,好看吗?")
return True, "照片已发送(缓存)"
else:
# 缓存失败,清除这个缓存项并继续正常流程
del self._request_cache[cache_key]
await self.send_text(f"正在为你拍照,请稍候...")
try:
seed = random.randint(1, 1000000)
success, result = await asyncio.to_thread(
self._make_http_image_request,
prompt=final_prompt,
model=model,
size=size,
seed=seed,
guidance_scale=guidance_scale,
watermark=watermark,
)
except Exception as e:
logger.error(f"{self.log_prefix} (HTTP) 异步请求执行失败: {e!r}", exc_info=True)
traceback.print_exc()
success = False
result = f"照片生成服务遇到意外问题: {str(e)[:100]}"
if success:
image_url = result
logger.info(f"{self.log_prefix} 图片URL获取成功: {image_url[:70]}... 下载并编码.")
try:
encode_success, encode_result = await asyncio.to_thread(self._download_and_encode_base64, image_url)
except Exception as e:
logger.error(f"{self.log_prefix} (B64) 异步下载/编码失败: {e!r}", exc_info=True)
traceback.print_exc()
encode_success = False
encode_result = f"图片下载或编码时发生内部错误: {str(e)[:100]}"
if encode_success:
base64_image_string = encode_result
# 更新缓存
if enable_cache:
self._update_cache(final_prompt, model, size, base64_image_string)
# 发送图片
send_success = await self._send_image(base64_image_string)
if send_success:
# 存储到文件
await self._store_picture_info(final_prompt, image_url)
logger.info(f"{self.log_prefix} 成功生成并存储照片: {image_url}")
await self.send_text("当当当当~这是我刚拍的照片,好看吗?")
return True, f"成功生成照片: {image_url}"
else:
await self.send_text("照片生成了,但发送失败了,可能是格式问题...")
return False, "照片发送失败"
else:
await self.send_text(f"照片下载失败: {encode_result}")
return False, encode_result
else:
await self.send_text(f"哎呀,拍照失败了: {result}")
return False, result
except Exception as e:
logger.error(f"{self.log_prefix} 执行拍照动作失败: {e}", exc_info=True)
traceback.print_exc()
await self.send_text("呜呜,拍照的时候出了一点小问题...")
return False, str(e)
async def _store_picture_info(self, prompt: str, image_url: str):
"""将照片信息存入日志文件"""
log_file = self.api.get_config("storage.log_file", "picture_log.json")
log_path = os.path.join(DATA_DIR, log_file)
max_photos = self.api.get_config("storage.max_photos", 50)
async with file_lock:
try:
if os.path.exists(log_path):
with open(log_path, 'r', encoding='utf-8') as f:
log_data = json.load(f)
else:
log_data = []
except (json.JSONDecodeError, FileNotFoundError):
log_data = []
# 添加新照片
log_data.append({
"prompt": prompt,
"image_url": image_url,
"timestamp": datetime.datetime.now().isoformat()
})
# 如果超过最大数量,删除最旧的
if len(log_data) > max_photos:
log_data = sorted(log_data, key=lambda x: x.get('timestamp', ''), reverse=True)[:max_photos]
try:
with open(log_path, 'w', encoding='utf-8') as f:
json.dump(log_data, f, ensure_ascii=False, indent=4)
except Exception as e:
logger.error(f"{self.log_prefix} 写入照片日志文件失败: {e}", exc_info=True)
def _make_http_image_request(
self, prompt: str, model: str, size: str, seed: int, guidance_scale: float, watermark: bool
) -> Tuple[bool, str]:
"""发送HTTP请求到火山引擎豆包API生成图片"""
try:
base_url = self.api.get_config("api.base_url")
api_key = self.api.get_config("api.volcano_generate_api_key")
# 构建请求URL和头部
endpoint = f"{base_url.rstrip('/')}/images/generations"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}",
}
# 构建请求体
request_body = {
"model": model,
"prompt": prompt,
"response_format": "url",
"size": size,
"seed": seed,
"guidance_scale": guidance_scale,
"watermark": watermark,
"api-key": api_key,
}
# 创建请求对象
req = urllib.request.Request(
endpoint,
data=json.dumps(request_body).encode("utf-8"),
headers=headers,
method="POST",
)
# 发送请求并获取响应
with urllib.request.urlopen(req, timeout=60) as response:
response_data = json.loads(response.read().decode("utf-8"))
# 解析响应
image_url = None
if (
isinstance(response_data.get("data"), list)
and response_data["data"]
and isinstance(response_data["data"][0], dict)
):
image_url = response_data["data"][0].get("url")
elif response_data.get("url"):
image_url = response_data.get("url")
if image_url:
return True, image_url
else:
error_msg = response_data.get("error", {}).get("message", "未知错误")
logger.error(f"API返回错误: {error_msg}")
return False, f"API错误: {error_msg}"
except urllib.error.HTTPError as e:
error_body = e.read().decode("utf-8")
logger.error(f"HTTP错误 {e.code}: {error_body}")
return False, f"HTTP错误 {e.code}: {error_body[:100]}..."
except Exception as e:
logger.error(f"请求异常: {e}", exc_info=True)
return False, f"请求异常: {str(e)}"
def _download_and_encode_base64(self, image_url: str) -> Tuple[bool, str]:
"""下载图片并转换为Base64编码"""
try:
with urllib.request.urlopen(image_url) as response:
image_data = response.read()
base64_encoded = base64.b64encode(image_data).decode('utf-8')
return True, base64_encoded
except Exception as e:
logger.error(f"图片下载编码失败: {e}", exc_info=True)
return False, str(e)
async def _send_image(self, base64_image: str) -> bool:
"""发送图片"""
try:
# 使用聊天流信息确定发送目标
chat_stream = self.api.get_service("chat_stream")
if not chat_stream:
logger.error(f"{self.log_prefix} 没有可用的聊天流发送图片")
return False
if chat_stream.group_info:
# 群聊
return await self.api.send_message_to_target(
message_type="image",
content=base64_image,
platform=chat_stream.platform,
target_id=str(chat_stream.group_info.group_id),
is_group=True,
display_message="发送生成的照片",
)
else:
# 私聊
return await self.api.send_message_to_target(
message_type="image",
content=base64_image,
platform=chat_stream.platform,
target_id=str(chat_stream.user_info.user_id),
is_group=False,
display_message="发送生成的照片",
)
except Exception as e:
logger.error(f"{self.log_prefix} 发送图片时出错: {e}")
return False
@classmethod
def _get_cache_key(cls, description: str, model: str, size: str) -> str:
"""生成缓存键"""
return f"{description}|{model}|{size}"
def _update_cache(self, description: str, model: str, size: str, base64_image: str):
"""更新缓存"""
max_cache_size = self.api.get_config("storage.max_cache_size", 10)
cache_key = self._get_cache_key(description, model, size)
# 添加到缓存
self._request_cache[cache_key] = base64_image
# 如果缓存超过最大大小,删除最旧的项
if len(self._request_cache) > max_cache_size:
oldest_key = next(iter(self._request_cache))
del self._request_cache[oldest_key]
class ShowRecentPicturesCommand(BaseCommand):
"""展示最近生成的照片"""
command_name = "show_recent_pictures"
command_description = "展示最近生成的5张照片"
command_pattern = r"^/show_pics$"
command_help = "用法: /show_pics"
command_examples = ["/show_pics"]
intercept_message = True
async def execute(self) -> Tuple[bool, Optional[str]]:
logger.info(f"{self.log_prefix} 执行展示最近照片命令")
log_file = self.api.get_config("storage.log_file", "picture_log.json")
log_path = os.path.join(DATA_DIR, log_file)
async with file_lock:
try:
if not os.path.exists(log_path):
await self.send_text("最近还没有拍过照片哦,快让我自拍一张吧!")
return True, "没有照片日志文件"
with open(log_path, 'r', encoding='utf-8') as f:
log_data = json.load(f)
if not log_data:
await self.send_text("最近还没有拍过照片哦,快让我自拍一张吧!")
return True, "没有照片"
# 获取最新的5张照片
recent_pics = sorted(log_data, key=lambda x: x['timestamp'], reverse=True)[:5]
# 先发送文本消息
await self.send_text("这是我最近拍的几张照片~")
# 逐个发送图片
for pic in recent_pics:
# 尝试获取图片URL
image_url = pic.get('image_url')
if image_url:
try:
# 下载图片并转换为Base64
with urllib.request.urlopen(image_url) as response:
image_data = response.read()
base64_encoded = base64.b64encode(image_data).decode('utf-8')
# 发送图片
await self.send_type(
message_type="image",
content=base64_encoded,
display_message="发送最近的照片"
)
except Exception as e:
logger.error(f"{self.log_prefix} 下载或发送照片失败: {e}", exc_info=True)
return True, "成功展示最近的照片"
except json.JSONDecodeError:
await self.send_text("照片记录文件好像损坏了...")
return False, "JSON解码错误"
except Exception as e:
logger.error(f"{self.log_prefix} 展示照片失败: {e}", exc_info=True)
await self.send_text("哎呀,查找照片的时候出错了。")
return False, str(e)
@register_plugin
class TakePicturePlugin(BasePlugin):
"""拍照插件"""
plugin_name = "take_picture_plugin"
plugin_description = "提供生成自拍照和展示最近照片的功能"
plugin_version = "1.0.0"
plugin_author = "SengokuCola"
enable_plugin = True
config_file_name = "config.toml"
# 配置节描述
config_section_descriptions = {
"plugin": "插件基本信息配置",
"api": "API相关配置包含火山引擎API的访问信息",
"components": "组件启用控制",
"picture": "拍照功能核心配置",
"storage": "照片存储相关配置",
}
# 配置Schema定义
config_schema = {
"plugin": {
"name": ConfigField(type=str, default="take_picture_plugin", description="插件名称", required=True),
"version": ConfigField(type=str, default="1.3.0", description="插件版本号"),
"enabled": ConfigField(type=bool, default=False, description="是否启用插件"),
"description": ConfigField(type=str, default="提供生成自拍照和展示最近照片的功能", description="插件描述", required=True),
},
"api": {
"base_url": ConfigField(
type=str,
default="https://ark.cn-beijing.volces.com/api/v3",
description="API基础URL",
example="https://api.example.com/v1",
),
"volcano_generate_api_key": ConfigField(
type=str, default="YOUR_DOUBAO_API_KEY_HERE", description="火山引擎豆包API密钥", required=True
),
},
"components": {
"enable_take_picture_action": ConfigField(type=bool, default=True, description="是否启用拍照Action"),
"enable_show_pics_command": ConfigField(type=bool, default=True, description="是否启用展示照片Command"),
},
"picture": {
"default_model": ConfigField(
type=str,
default="doubao-seedream-3-0-t2i-250415",
description="默认使用的文生图模型",
choices=["doubao-seedream-3-0-t2i-250415", "doubao-seedream-2-0-t2i"],
),
"default_style": ConfigField(type=str, default="动漫", description="默认图片风格"),
"default_size": ConfigField(
type=str,
default="1024x1024",
description="默认图片尺寸",
example="1024x1024",
choices=["1024x1024", "1024x1280", "1280x1024", "1024x1536", "1536x1024"],
),
"default_watermark": ConfigField(type=bool, default=True, description="是否默认添加水印"),
"default_guidance_scale": ConfigField(
type=float, default=2.5, description="模型指导强度,影响图片与提示的关联性", example="2.0"
),
"default_seed": ConfigField(type=int, default=42, description="随机种子,用于复现图片"),
"prompt_templates": ConfigField(
type=list,
default=TakePictureAction.DEFAULT_PROMPT_TEMPLATES,
description="用于生成自拍照的prompt模板"
),
},
"storage": {
"max_photos": ConfigField(type=int, default=50, description="最大保存的照片数量"),
"log_file": ConfigField(type=str, default="picture_log.json", description="照片日志文件名"),
"enable_cache": ConfigField(type=bool, default=True, description="是否启用请求缓存"),
"max_cache_size": ConfigField(type=int, default=10, description="最大缓存数量"),
}
}
def get_plugin_components(self) -> List[Tuple[ComponentInfo, Type]]:
"""返回插件包含的组件列表"""
components = []
if self.get_config("components.enable_take_picture_action", True):
components.append((TakePictureAction.get_action_info(), TakePictureAction))
if self.get_config("components.enable_show_pics_command", True):
components.append((ShowRecentPicturesCommand.get_command_info(), ShowRecentPicturesCommand))
return components

View File

@@ -57,7 +57,7 @@ class TTSAction(BaseAction):
try:
# 发送TTS消息
await self.send_type(type="tts_text", text=processed_text)
await self.send_custom(message_type="tts_text", content=processed_text)
logger.info(f"{self.log_prefix} TTS动作执行成功文本长度: {len(processed_text)}")
return True, "TTS动作执行成功"

View File

@@ -62,7 +62,7 @@ class VTBAction(BaseAction):
try:
# 发送VTB动作消息 - 使用新版本的send_type方法
await self.send_type(type="vtb_text", text=processed_text)
await self.send_custom(message_type="vtb_text", content=processed_text)
logger.info(f"{self.log_prefix} VTB动作执行成功文本内容: {processed_text}")
return True, "VTB动作执行成功"