尝试加入权限内容。

This commit is contained in:
liuwei
2025-03-18 14:51:37 +08:00
parent bcca2dab28
commit adfa3505a5
3 changed files with 33 additions and 112 deletions

View File

@@ -1,14 +1,14 @@
import os
import time
import requests
import json
from typing import Dict, Any, Tuple, Optional, List
import requests
from message_storage.message_to_db import MessageStorage
from plugin_common.plugin_interface import PluginStatus
from plugin_common.message_plugin_interface import MessagePluginInterface
from message_summary.compress_chat_data import compress_chat_data
from message_summary.markdown_to_image import convert_md_str_to_image
from plugin_common.message_plugin_interface import MessagePluginInterface
from plugin_common.plugin_interface import PluginStatus
from robot_cmd.robot_command import GroupBotManager, Feature, PermissionStatus
class MessageSummaryPlugin(MessagePluginInterface):
@@ -41,6 +41,7 @@ class MessageSummaryPlugin(MessagePluginInterface):
def initialize(self, context: Dict[str, Any]) -> bool:
"""初始化插件"""
try:
# 从插件配置中获取API密钥和URL
api_config = self._config.get("api", {})
self._api_key = api_config.get("api_key", "app-McGLzBhBjeBCSEi7n83MtuTo")
@@ -49,22 +50,25 @@ class MessageSummaryPlugin(MessagePluginInterface):
self.all_contacts = context["all_contacts"]
self.message_storage = MessageStorage()
print(f"初始化 {self.name} 插件成功")
self.LOG.info(f"初始化 {self.name} 插件成功")
return True
except Exception as e:
print(f"初始化 {self.name} 插件失败: {e}")
if hasattr(self, 'LOG'):
self.LOG.error(f"初始化 {self.name} 插件失败: {e}")
else:
print(f"初始化 {self.name} 插件失败: {e}")
return False
def start(self) -> bool:
"""启动插件"""
self.status = PluginStatus.RUNNING
print(f"{self.name} 插件已启动")
self.LOG.info(f"{self.name} 插件已启动")
return True
def stop(self) -> bool:
"""停止插件"""
self.status = PluginStatus.STOPPED
print(f"{self.name} 插件已停止")
self.LOG.info(f"{self.name} 插件已停止")
return True
def process_message(self, message: Dict[str, Any]) -> Tuple[bool, Optional[str]]:
@@ -87,10 +91,11 @@ class MessageSummaryPlugin(MessagePluginInterface):
if wcf:
wcf.send_text("只支持群聊消息总结", message.get("sender"))
return True, None
# 权限判断
gbm: GroupBotManager = message.get("gbm")
if gbm and gbm.get_group_permission(group_id, Feature.AI_CAPABILITY) == PermissionStatus.DISABLED:
return True, None
# 从消息历史中获取群聊记录
# 这里需要根据实际情况从系统上下文或数据库中获取群聊记录
# 为简化示例,这里假设从消息中提取
chat_content = self.message_storage.get_messages(group_id, self.all_contacts)
if len(chat_content) < 100:
return False, None
@@ -109,27 +114,18 @@ class MessageSummaryPlugin(MessagePluginInterface):
return True, None
except Exception as e:
print(f"处理消息总结命令失败: {e}")
self.LOG.error(f"处理消息总结命令失败: {e}")
return False, None
def _generate_summary(self, chat_content: str, group_id: str) -> Tuple[str, Optional[str]]:
"""生成总结"""
"""
使用Dify API生成群聊消息总结
Args:
content: 需要总结的群聊消息内容
Returns:
生成的总结内容和图片路径
"""
# Dify API配置
content_compress = chat_content
try:
content_compress = compress_chat_data(chat_content)
print(f"压缩内容成功:{len(content_compress)}--{len(chat_content)}")
self.LOG.info(f"压缩内容成功:{len(content_compress)}--{len(chat_content)}")
except Exception as e:
print(f"压缩内容失败:{e}")
self.LOG.error(f"压缩内容失败:{e}")
# 准备请求数据
data = {
@@ -154,8 +150,8 @@ class MessageSummaryPlugin(MessagePluginInterface):
# 解析响应
response_data = response.json()
print(f"Dify API响应状态码: {response.status_code}")
print(f"响应数据: {json.dumps(response_data, ensure_ascii=False, indent=2)}")
self.LOG.info(f"Dify API响应状态码: {response.status_code}")
self.LOG.debug(f"响应数据: {json.dumps(response_data, ensure_ascii=False, indent=2)}")
# 提取回答内容
answer = response_data.get("answer", "")
@@ -175,18 +171,18 @@ class MessageSummaryPlugin(MessagePluginInterface):
try:
spath = convert_md_str_to_image(answer, "output.png")
except Exception as e:
print(f"生成image失败:{e}")
self.LOG.error(f"生成image失败:{e}")
# 返回文本内容和图片路径
return answer, spath
except requests.exceptions.RequestException as e:
print(f"请求Dify API时出错: {e}")
self.LOG.error(f"请求Dify API时出错: {e}")
return f"生成总结时出错: {str(e)}", None
except json.JSONDecodeError as e:
print(f"解析Dify API响应时出错: {e}")
self.LOG.error(f"解析Dify API响应时出错: {e}")
return "解析API响应时出错", None
except Exception as e:
print(f"处理总结时出现未知错误: {e}")
self.LOG.error(f"处理总结时出现未知错误: {e}")
return f"生成总结时出现未知错误: {str(e)}", None