No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

293 líneas
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. return
  146. if len(cmd) == 3 and cmd[1] == "sum" and (cmd[2] == "open" or cmd[2] == "close"):
  147. # 知识库开关指令
  148. if not _is_admin(e_context):
  149. _set_reply_text("需要管理员权限执行", e_context, level=ReplyType.ERROR)
  150. return
  151. is_open = True
  152. tips_text = "开启"
  153. if cmd[2] == "close":
  154. tips_text = "关闭"
  155. is_open = False
  156. if not self.sum_config:
  157. _set_reply_text(f"插件未启用summary功能,请参考以下链添加插件配置\n\nhttps://github.com/zhayujie/chatgpt-on-wechat/blob/master/plugins/linkai/README.md", e_context, level=ReplyType.INFO)
  158. else:
  159. self.sum_config["enabled"] = is_open
  160. _set_reply_text(f"文章总结功能{tips_text}", e_context, level=ReplyType.INFO)
  161. return
  162. _set_reply_text(f"指令错误,请输入{_get_trigger_prefix()}linkai help 获取帮助", e_context,
  163. level=ReplyType.INFO)
  164. return
  165. def _is_summary_open(self, context) -> bool:
  166. if not self.sum_config or not self.sum_config.get("enabled"):
  167. return False
  168. if not context.kwargs.get("isgroup") and not self.sum_config.get("group_enabled"):
  169. return False
  170. return True
  171. # LinkAI 对话任务处理
  172. def _is_chat_task(self, e_context: EventContext):
  173. context = e_context['context']
  174. # 群聊应用管理
  175. return self.config.get("group_app_map") and context.kwargs.get("isgroup")
  176. def _process_chat_task(self, e_context: EventContext):
  177. """
  178. 处理LinkAI对话任务
  179. :param e_context: 对话上下文
  180. """
  181. context = e_context['context']
  182. # 群聊应用管理
  183. group_name = context.get("msg").from_user_nickname
  184. app_code = self._fetch_group_app_code(group_name)
  185. if app_code:
  186. context.kwargs['app_code'] = app_code
  187. def _fetch_group_app_code(self, group_name: str) -> str:
  188. """
  189. 根据群聊名称获取对应的应用code
  190. :param group_name: 群聊名称
  191. :return: 应用code
  192. """
  193. group_mapping = self.config.get("group_app_map")
  194. if group_mapping:
  195. app_code = group_mapping.get(group_name) or group_mapping.get("ALL_GROUP")
  196. return app_code
  197. def get_help_text(self, verbose=False, **kwargs):
  198. trigger_prefix = _get_trigger_prefix()
  199. help_text = "用于集成 LinkAI 提供的知识库、Midjourney绘画、文档总结对话等能力。\n\n"
  200. if not verbose:
  201. return help_text
  202. help_text += f'📖 知识库\n - 群聊中指定应用: {trigger_prefix}linkai app 应用编码\n'
  203. help_text += f' - {trigger_prefix}linkai open: 开启对话\n'
  204. help_text += f' - {trigger_prefix}linkai close: 关闭对话\n'
  205. help_text += f'\n例如: \n"{trigger_prefix}linkai app Kv2fXJcH"\n\n'
  206. help_text += f"🎨 绘画\n - 生成: {trigger_prefix}mj 描述词1, 描述词2.. \n - 放大: {trigger_prefix}mju 图片ID 图片序号\n - 变换: {trigger_prefix}mjv 图片ID 图片序号\n - 重置: {trigger_prefix}mjr 图片ID"
  207. help_text += f"\n\n例如:\n\"{trigger_prefix}mj a little cat, white --ar 9:16\"\n\"{trigger_prefix}mju 11055927171882 2\""
  208. help_text += f"\n\"{trigger_prefix}mjv 11055927171882 2\"\n\"{trigger_prefix}mjr 11055927171882\""
  209. help_text += f"\n\n💡 文档总结和对话\n - 开启: {trigger_prefix}linkai sum open\n - 使用: 发送文件、公众号文章等可生成摘要,并与内容对话"
  210. return help_text
  211. def _load_config_template(self):
  212. logger.debug("No LinkAI plugin config.json, use plugins/linkai/config.json.template")
  213. try:
  214. plugin_config_path = os.path.join(self.path, "config.json.template")
  215. if os.path.exists(plugin_config_path):
  216. with open(plugin_config_path, "r", encoding="utf-8") as f:
  217. plugin_conf = json.load(f)
  218. plugin_conf["midjourney"]["enabled"] = False
  219. plugin_conf["summary"]["enabled"] = False
  220. return plugin_conf
  221. except Exception as e:
  222. logger.exception(e)
  223. def _send_info(e_context: EventContext, content: str):
  224. reply = Reply(ReplyType.TEXT, content)
  225. channel = e_context["channel"]
  226. channel.send(reply, e_context["context"])
  227. # 静态方法
  228. def _is_admin(e_context: EventContext) -> bool:
  229. """
  230. 判断消息是否由管理员用户发送
  231. :param e_context: 消息上下文
  232. :return: True: 是, False: 否
  233. """
  234. context = e_context["context"]
  235. if context["isgroup"]:
  236. return context.kwargs.get("msg").actual_user_id in global_config["admin_users"]
  237. else:
  238. return context["receiver"] in global_config["admin_users"]
  239. def _find_user_id(context):
  240. if context["isgroup"]:
  241. return context.kwargs.get("msg").actual_user_id
  242. else:
  243. return context["receiver"]
  244. def _set_reply_text(content: str, e_context: EventContext, level: ReplyType = ReplyType.ERROR):
  245. reply = Reply(level, content)
  246. e_context["reply"] = reply
  247. e_context.action = EventAction.BREAK_PASS
  248. def _get_trigger_prefix():
  249. return conf().get("plugin_trigger_prefix", "$")
  250. def _find_sum_id(context):
  251. return USER_FILE_MAP.get(_find_user_id(context) + "-sum_id")
  252. def _find_file_id(context):
  253. return USER_FILE_MAP.get(_find_user_id(context) + "-file_id")
  254. USER_FILE_MAP = ExpiredDict(conf().get("expires_in_seconds") or 60 * 60)