You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

288 lines
12KB

  1. import plugins
  2. from bridge.context import ContextType
  3. from bridge.reply import Reply, ReplyType
  4. from config import global_config
  5. from plugins import *
  6. from .midjourney import MJBot
  7. from .summary import LinkSummary
  8. from bridge import bridge
  9. from common.expired_dict import ExpiredDict
  10. from common import const
  11. import os
  12. @plugins.register(
  13. name="linkai",
  14. desc="A plugin that supports knowledge base and midjourney drawing.",
  15. version="0.1.0",
  16. author="https://link-ai.tech",
  17. desire_priority=99
  18. )
  19. class LinkAI(Plugin):
  20. def __init__(self):
  21. super().__init__()
  22. self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context
  23. self.config = super().load_config()
  24. if not self.config:
  25. # 未加载到配置,使用模板中的配置
  26. self.config = self._load_config_template()
  27. if self.config:
  28. self.mj_bot = MJBot(self.config.get("midjourney"))
  29. self.sum_config = {}
  30. if self.config:
  31. self.sum_config = self.config.get("summary")
  32. logger.info("[LinkAI] inited")
  33. def on_handle_context(self, e_context: EventContext):
  34. """
  35. 消息处理逻辑
  36. :param e_context: 消息上下文
  37. """
  38. if not self.config:
  39. return
  40. context = e_context['context']
  41. if context.type not in [ContextType.TEXT, ContextType.IMAGE, ContextType.IMAGE_CREATE, ContextType.FILE, ContextType.SHARING]:
  42. # filter content no need solve
  43. return
  44. if context.type == ContextType.FILE and self._is_summary_open(context):
  45. # 文件处理
  46. context.get("msg").prepare()
  47. file_path = context.content
  48. if not LinkSummary().check_file(file_path, self.sum_config):
  49. return
  50. _send_info(e_context, "正在为你加速生成摘要,请稍后")
  51. res = LinkSummary().summary_file(file_path)
  52. if not res:
  53. _set_reply_text("总结出现异常,请稍后再试吧", e_context)
  54. return
  55. USER_FILE_MAP[_find_user_id(context) + "-sum_id"] = res.get("summary_id")
  56. _set_reply_text(res.get("summary") + "\n\n💬 发送 \"开启对话\" 可以开启与文件内容的对话", e_context, level=ReplyType.TEXT)
  57. os.remove(file_path)
  58. return
  59. if (context.type == ContextType.SHARING and self._is_summary_open(context)) or \
  60. (context.type == ContextType.TEXT and LinkSummary().check_url(context.content)):
  61. if not LinkSummary().check_url(context.content):
  62. return
  63. _send_info(e_context, "正在为你加速生成摘要,请稍后")
  64. res = LinkSummary().summary_url(context.content)
  65. if not res:
  66. _set_reply_text("总结出现异常,请稍后再试吧", e_context)
  67. return
  68. _set_reply_text(res.get("summary") + "\n\n💬 发送 \"开启对话\" 可以开启与文章内容的对话", e_context, level=ReplyType.TEXT)
  69. USER_FILE_MAP[_find_user_id(context) + "-sum_id"] = res.get("summary_id")
  70. return
  71. mj_type = self.mj_bot.judge_mj_task_type(e_context)
  72. if mj_type:
  73. # MJ作图任务处理
  74. self.mj_bot.process_mj_task(mj_type, e_context)
  75. return
  76. if context.content.startswith(f"{_get_trigger_prefix()}linkai"):
  77. # 应用管理功能
  78. self._process_admin_cmd(e_context)
  79. return
  80. if context.type == ContextType.TEXT and context.content == "开启对话" and _find_sum_id(context):
  81. # 文本对话
  82. _send_info(e_context, "正在为你开启对话,请稍后")
  83. res = LinkSummary().summary_chat(_find_sum_id(context))
  84. if not res:
  85. _set_reply_text("开启对话失败,请稍后再试吧", e_context)
  86. return
  87. USER_FILE_MAP[_find_user_id(context) + "-file_id"] = res.get("file_id")
  88. _set_reply_text("💡你可以问我关于这篇文章的任何问题,例如:\n\n" + res.get("questions") + "\n\n发送 \"退出对话\" 可以关闭与文章的对话", e_context, level=ReplyType.TEXT)
  89. return
  90. if context.type == ContextType.TEXT and context.content == "退出对话" and _find_file_id(context):
  91. del USER_FILE_MAP[_find_user_id(context) + "-file_id"]
  92. bot = bridge.Bridge().find_chat_bot(const.LINKAI)
  93. bot.sessions.clear_session(context["session_id"])
  94. _set_reply_text("对话已退出", e_context, level=ReplyType.TEXT)
  95. return
  96. if context.type == ContextType.TEXT and _find_file_id(context):
  97. bot = bridge.Bridge().find_chat_bot(const.LINKAI)
  98. context.kwargs["file_id"] = _find_file_id(context)
  99. reply = bot.reply(context.content, context)
  100. e_context["reply"] = reply
  101. e_context.action = EventAction.BREAK_PASS
  102. return
  103. if self._is_chat_task(e_context):
  104. # 文本对话任务处理
  105. self._process_chat_task(e_context)
  106. # 插件管理功能
  107. def _process_admin_cmd(self, e_context: EventContext):
  108. context = e_context['context']
  109. cmd = context.content.split()
  110. if len(cmd) == 1 or (len(cmd) == 2 and cmd[1] == "help"):
  111. _set_reply_text(self.get_help_text(verbose=True), e_context, level=ReplyType.INFO)
  112. return
  113. if len(cmd) == 2 and (cmd[1] == "open" or cmd[1] == "close"):
  114. # 知识库开关指令
  115. if not _is_admin(e_context):
  116. _set_reply_text("需要管理员权限执行", e_context, level=ReplyType.ERROR)
  117. return
  118. is_open = True
  119. tips_text = "开启"
  120. if cmd[1] == "close":
  121. tips_text = "关闭"
  122. is_open = False
  123. conf()["use_linkai"] = is_open
  124. bridge.Bridge().reset_bot()
  125. _set_reply_text(f"LinkAI对话功能{tips_text}", e_context, level=ReplyType.INFO)
  126. return
  127. if len(cmd) == 3 and cmd[1] == "app":
  128. # 知识库应用切换指令
  129. if not context.kwargs.get("isgroup"):
  130. _set_reply_text("该指令需在群聊中使用", e_context, level=ReplyType.ERROR)
  131. return
  132. if not _is_admin(e_context):
  133. _set_reply_text("需要管理员权限执行", e_context, level=ReplyType.ERROR)
  134. return
  135. app_code = cmd[2]
  136. group_name = context.kwargs.get("msg").from_user_nickname
  137. group_mapping = self.config.get("group_app_map")
  138. if group_mapping:
  139. group_mapping[group_name] = app_code
  140. else:
  141. self.config["group_app_map"] = {group_name: app_code}
  142. # 保存插件配置
  143. super().save_config(self.config)
  144. _set_reply_text(f"应用设置成功: {app_code}", e_context, level=ReplyType.INFO)
  145. if len(cmd) == 3 and cmd[1] == "sum" and (cmd[2] == "open" or cmd[2] == "close"):
  146. # 知识库开关指令
  147. if not _is_admin(e_context):
  148. _set_reply_text("需要管理员权限执行", e_context, level=ReplyType.ERROR)
  149. return
  150. is_open = True
  151. tips_text = "开启"
  152. if cmd[2] == "close":
  153. tips_text = "关闭"
  154. is_open = False
  155. self.sum_config["enabled"] = is_open
  156. _set_reply_text(f"文章总结功能{tips_text}", e_context, level=ReplyType.INFO)
  157. else:
  158. _set_reply_text(f"指令错误,请输入{_get_trigger_prefix()}linkai help 获取帮助", e_context,
  159. level=ReplyType.INFO)
  160. return
  161. def _is_summary_open(self, context) -> bool:
  162. if not self.sum_config or not self.sum_config.get("enabled"):
  163. return False
  164. if not context.kwargs.get("isgroup") and not self.sum_config.get("group_enabled"):
  165. return False
  166. return True
  167. # LinkAI 对话任务处理
  168. def _is_chat_task(self, e_context: EventContext):
  169. context = e_context['context']
  170. # 群聊应用管理
  171. return self.config.get("group_app_map") and context.kwargs.get("isgroup")
  172. def _process_chat_task(self, e_context: EventContext):
  173. """
  174. 处理LinkAI对话任务
  175. :param e_context: 对话上下文
  176. """
  177. context = e_context['context']
  178. # 群聊应用管理
  179. group_name = context.get("msg").from_user_nickname
  180. app_code = self._fetch_group_app_code(group_name)
  181. if app_code:
  182. context.kwargs['app_code'] = app_code
  183. def _fetch_group_app_code(self, group_name: str) -> str:
  184. """
  185. 根据群聊名称获取对应的应用code
  186. :param group_name: 群聊名称
  187. :return: 应用code
  188. """
  189. group_mapping = self.config.get("group_app_map")
  190. if group_mapping:
  191. app_code = group_mapping.get(group_name) or group_mapping.get("ALL_GROUP")
  192. return app_code
  193. def get_help_text(self, verbose=False, **kwargs):
  194. trigger_prefix = _get_trigger_prefix()
  195. help_text = "用于集成 LinkAI 提供的知识库、Midjourney绘画、文档总结对话等能力。\n\n"
  196. if not verbose:
  197. return help_text
  198. help_text += f'📖 知识库\n - 群聊中指定应用: {trigger_prefix}linkai app 应用编码\n'
  199. help_text += f' - {trigger_prefix}linkai open: 开启对话\n'
  200. help_text += f' - {trigger_prefix}linkai close: 关闭对话\n'
  201. help_text += f'\n例如: \n"{trigger_prefix}linkai app Kv2fXJcH"\n\n'
  202. help_text += f"🎨 绘画\n - 生成: {trigger_prefix}mj 描述词1, 描述词2.. \n - 放大: {trigger_prefix}mju 图片ID 图片序号\n - 变换: {trigger_prefix}mjv 图片ID 图片序号\n - 重置: {trigger_prefix}mjr 图片ID"
  203. help_text += f"\n\n例如:\n\"{trigger_prefix}mj a little cat, white --ar 9:16\"\n\"{trigger_prefix}mju 11055927171882 2\""
  204. help_text += f"\n\"{trigger_prefix}mjv 11055927171882 2\"\n\"{trigger_prefix}mjr 11055927171882\""
  205. help_text += f"\n\n💡 文档总结和对话\n - 开启: {trigger_prefix}linkai sum open\n - 使用: 发送文件、公众号文章等可生成摘要,并与内容对话"
  206. return help_text
  207. def _load_config_template(self):
  208. logger.debug("No LinkAI plugin config.json, use plugins/linkai/config.json.template")
  209. try:
  210. plugin_config_path = os.path.join(self.path, "config.json.template")
  211. if os.path.exists(plugin_config_path):
  212. with open(plugin_config_path, "r", encoding="utf-8") as f:
  213. plugin_conf = json.load(f)
  214. plugin_conf["midjourney"]["enabled"] = False
  215. plugin_conf["summary"]["enabled"] = False
  216. return plugin_conf
  217. except Exception as e:
  218. logger.exception(e)
  219. def _send_info(e_context: EventContext, content: str):
  220. reply = Reply(ReplyType.TEXT, content)
  221. channel = e_context["channel"]
  222. channel.send(reply, e_context["context"])
  223. # 静态方法
  224. def _is_admin(e_context: EventContext) -> bool:
  225. """
  226. 判断消息是否由管理员用户发送
  227. :param e_context: 消息上下文
  228. :return: True: 是, False: 否
  229. """
  230. context = e_context["context"]
  231. if context["isgroup"]:
  232. return context.kwargs.get("msg").actual_user_id in global_config["admin_users"]
  233. else:
  234. return context["receiver"] in global_config["admin_users"]
  235. def _find_user_id(context):
  236. if context["isgroup"]:
  237. return context.kwargs.get("msg").actual_user_id
  238. else:
  239. return context["receiver"]
  240. def _set_reply_text(content: str, e_context: EventContext, level: ReplyType = ReplyType.ERROR):
  241. reply = Reply(level, content)
  242. e_context["reply"] = reply
  243. e_context.action = EventAction.BREAK_PASS
  244. def _get_trigger_prefix():
  245. return conf().get("plugin_trigger_prefix", "$")
  246. def _find_sum_id(context):
  247. return USER_FILE_MAP.get(_find_user_id(context) + "-sum_id")
  248. def _find_file_id(context):
  249. return USER_FILE_MAP.get(_find_user_id(context) + "-file_id")
  250. USER_FILE_MAP = ExpiredDict(conf().get("expires_in_seconds") or 60 * 60)