Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

linkai.py 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import plugins
  2. from bridge.context import ContextType
  3. from bridge.reply import Reply, ReplyType
  4. from plugins import *
  5. from .midjourney import MJBot
  6. from .summary import LinkSummary
  7. from bridge import bridge
  8. from common.expired_dict import ExpiredDict
  9. from common import const
  10. import os
  11. from .utils import Util
  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(f"[LinkAI] inited, config={self.config}")
  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 in [ContextType.FILE, ContextType.IMAGE] 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. if context.type != ContextType.IMAGE:
  51. _send_info(e_context, "正在为你加速生成摘要,请稍后")
  52. res = LinkSummary().summary_file(file_path)
  53. if not res:
  54. if context.type != ContextType.IMAGE:
  55. _set_reply_text("因为神秘力量无法获取内容,请稍后再试吧", e_context, level=ReplyType.TEXT)
  56. return
  57. summary_text = res.get("summary")
  58. if context.type != ContextType.IMAGE:
  59. USER_FILE_MAP[_find_user_id(context) + "-sum_id"] = res.get("summary_id")
  60. summary_text += "\n\n💬 发送 \"开启对话\" 可以开启与文件内容的对话"
  61. _set_reply_text(summary_text, e_context, level=ReplyType.TEXT)
  62. os.remove(file_path)
  63. return
  64. if (context.type == ContextType.SHARING and self._is_summary_open(context)) or \
  65. (context.type == ContextType.TEXT and LinkSummary().check_url(context.content)):
  66. if not LinkSummary().check_url(context.content):
  67. return
  68. _send_info(e_context, "正在为你加速生成摘要,请稍后")
  69. res = LinkSummary().summary_url(context.content)
  70. if not res:
  71. _set_reply_text("因为神秘力量无法获取文章内容,请稍后再试吧~", e_context, level=ReplyType.TEXT)
  72. return
  73. _set_reply_text(res.get("summary") + "\n\n💬 发送 \"开启对话\" 可以开启与文章内容的对话", e_context, level=ReplyType.TEXT)
  74. USER_FILE_MAP[_find_user_id(context) + "-sum_id"] = res.get("summary_id")
  75. return
  76. mj_type = self.mj_bot.judge_mj_task_type(e_context)
  77. if mj_type:
  78. # MJ作图任务处理
  79. self.mj_bot.process_mj_task(mj_type, e_context)
  80. return
  81. if context.content.startswith(f"{_get_trigger_prefix()}linkai"):
  82. # 应用管理功能
  83. self._process_admin_cmd(e_context)
  84. return
  85. if context.type == ContextType.TEXT and context.content == "开启对话" and _find_sum_id(context):
  86. # 文本对话
  87. _send_info(e_context, "正在为你开启对话,请稍后")
  88. res = LinkSummary().summary_chat(_find_sum_id(context))
  89. if not res:
  90. _set_reply_text("开启对话失败,请稍后再试吧", e_context)
  91. return
  92. USER_FILE_MAP[_find_user_id(context) + "-file_id"] = res.get("file_id")
  93. _set_reply_text("💡你可以问我关于这篇文章的任何问题,例如:\n\n" + res.get("questions") + "\n\n发送 \"退出对话\" 可以关闭与文章的对话", e_context, level=ReplyType.TEXT)
  94. return
  95. if context.type == ContextType.TEXT and context.content == "退出对话" and _find_file_id(context):
  96. del USER_FILE_MAP[_find_user_id(context) + "-file_id"]
  97. bot = bridge.Bridge().find_chat_bot(const.LINKAI)
  98. bot.sessions.clear_session(context["session_id"])
  99. _set_reply_text("对话已退出", e_context, level=ReplyType.TEXT)
  100. return
  101. if context.type == ContextType.TEXT and _find_file_id(context):
  102. bot = bridge.Bridge().find_chat_bot(const.LINKAI)
  103. context.kwargs["file_id"] = _find_file_id(context)
  104. reply = bot.reply(context.content, context)
  105. e_context["reply"] = reply
  106. e_context.action = EventAction.BREAK_PASS
  107. return
  108. if self._is_chat_task(e_context):
  109. # 文本对话任务处理
  110. self._process_chat_task(e_context)
  111. # 插件管理功能
  112. def _process_admin_cmd(self, e_context: EventContext):
  113. context = e_context['context']
  114. cmd = context.content.split()
  115. if len(cmd) == 1 or (len(cmd) == 2 and cmd[1] == "help"):
  116. _set_reply_text(self.get_help_text(verbose=True), e_context, level=ReplyType.INFO)
  117. return
  118. if len(cmd) == 2 and (cmd[1] == "open" or cmd[1] == "close"):
  119. # 知识库开关指令
  120. if not Util.is_admin(e_context):
  121. _set_reply_text("需要管理员权限执行", e_context, level=ReplyType.ERROR)
  122. return
  123. is_open = True
  124. tips_text = "开启"
  125. if cmd[1] == "close":
  126. tips_text = "关闭"
  127. is_open = False
  128. conf()["use_linkai"] = is_open
  129. bridge.Bridge().reset_bot()
  130. _set_reply_text(f"LinkAI对话功能{tips_text}", e_context, level=ReplyType.INFO)
  131. return
  132. if len(cmd) == 3 and cmd[1] == "app":
  133. # 知识库应用切换指令
  134. if not context.kwargs.get("isgroup"):
  135. _set_reply_text("该指令需在群聊中使用", e_context, level=ReplyType.ERROR)
  136. return
  137. if not Util.is_admin(e_context):
  138. _set_reply_text("需要管理员权限执行", e_context, level=ReplyType.ERROR)
  139. return
  140. app_code = cmd[2]
  141. group_name = context.kwargs.get("msg").from_user_nickname
  142. group_mapping = self.config.get("group_app_map")
  143. if group_mapping:
  144. group_mapping[group_name] = app_code
  145. else:
  146. self.config["group_app_map"] = {group_name: app_code}
  147. # 保存插件配置
  148. super().save_config(self.config)
  149. _set_reply_text(f"应用设置成功: {app_code}", e_context, level=ReplyType.INFO)
  150. return
  151. if len(cmd) == 3 and cmd[1] == "sum" and (cmd[2] == "open" or cmd[2] == "close"):
  152. # 知识库开关指令
  153. if not Util.is_admin(e_context):
  154. _set_reply_text("需要管理员权限执行", e_context, level=ReplyType.ERROR)
  155. return
  156. is_open = True
  157. tips_text = "开启"
  158. if cmd[2] == "close":
  159. tips_text = "关闭"
  160. is_open = False
  161. if not self.sum_config:
  162. _set_reply_text(f"插件未启用summary功能,请参考以下链添加插件配置\n\nhttps://github.com/zhayujie/chatgpt-on-wechat/blob/master/plugins/linkai/README.md", e_context, level=ReplyType.INFO)
  163. else:
  164. self.sum_config["enabled"] = is_open
  165. _set_reply_text(f"文章总结功能{tips_text}", e_context, level=ReplyType.INFO)
  166. return
  167. _set_reply_text(f"指令错误,请输入{_get_trigger_prefix()}linkai help 获取帮助", e_context,
  168. level=ReplyType.INFO)
  169. return
  170. def _is_summary_open(self, context) -> bool:
  171. if not self.sum_config or not self.sum_config.get("enabled"):
  172. return False
  173. if context.kwargs.get("isgroup") and not self.sum_config.get("group_enabled"):
  174. return False
  175. support_type = self.sum_config.get("type") or ["FILE", "SHARING"]
  176. if context.type.name not in support_type:
  177. return False
  178. return True
  179. # LinkAI 对话任务处理
  180. def _is_chat_task(self, e_context: EventContext):
  181. context = e_context['context']
  182. # 群聊应用管理
  183. return self.config.get("group_app_map") and context.kwargs.get("isgroup")
  184. def _process_chat_task(self, e_context: EventContext):
  185. """
  186. 处理LinkAI对话任务
  187. :param e_context: 对话上下文
  188. """
  189. context = e_context['context']
  190. # 群聊应用管理
  191. group_name = context.get("msg").from_user_nickname
  192. app_code = self._fetch_group_app_code(group_name)
  193. if app_code:
  194. context.kwargs['app_code'] = app_code
  195. def _fetch_group_app_code(self, group_name: str) -> str:
  196. """
  197. 根据群聊名称获取对应的应用code
  198. :param group_name: 群聊名称
  199. :return: 应用code
  200. """
  201. group_mapping = self.config.get("group_app_map")
  202. if group_mapping:
  203. app_code = group_mapping.get(group_name) or group_mapping.get("ALL_GROUP")
  204. return app_code
  205. def get_help_text(self, verbose=False, **kwargs):
  206. trigger_prefix = _get_trigger_prefix()
  207. help_text = "用于集成 LinkAI 提供的知识库、Midjourney绘画、文档总结、联网搜索等能力。\n\n"
  208. if not verbose:
  209. return help_text
  210. help_text += f'📖 知识库\n - 群聊中指定应用: {trigger_prefix}linkai app 应用编码\n'
  211. help_text += f' - {trigger_prefix}linkai open: 开启对话\n'
  212. help_text += f' - {trigger_prefix}linkai close: 关闭对话\n'
  213. help_text += f'\n例如: \n"{trigger_prefix}linkai app Kv2fXJcH"\n\n'
  214. help_text += f"🎨 绘画\n - 生成: {trigger_prefix}mj 描述词1, 描述词2.. \n - 放大: {trigger_prefix}mju 图片ID 图片序号\n - 变换: {trigger_prefix}mjv 图片ID 图片序号\n - 重置: {trigger_prefix}mjr 图片ID"
  215. help_text += f"\n\n例如:\n\"{trigger_prefix}mj a little cat, white --ar 9:16\"\n\"{trigger_prefix}mju 11055927171882 2\""
  216. help_text += f"\n\"{trigger_prefix}mjv 11055927171882 2\"\n\"{trigger_prefix}mjr 11055927171882\""
  217. help_text += f"\n\n💡 文档总结和对话\n - 开启: {trigger_prefix}linkai sum open\n - 使用: 发送文件、公众号文章等可生成摘要,并与内容对话"
  218. return help_text
  219. def _load_config_template(self):
  220. logger.debug("No LinkAI plugin config.json, use plugins/linkai/config.json.template")
  221. try:
  222. plugin_config_path = os.path.join(self.path, "config.json.template")
  223. if os.path.exists(plugin_config_path):
  224. with open(plugin_config_path, "r", encoding="utf-8") as f:
  225. plugin_conf = json.load(f)
  226. plugin_conf["midjourney"]["enabled"] = False
  227. plugin_conf["summary"]["enabled"] = False
  228. return plugin_conf
  229. except Exception as e:
  230. logger.exception(e)
  231. def _send_info(e_context: EventContext, content: str):
  232. reply = Reply(ReplyType.TEXT, content)
  233. channel = e_context["channel"]
  234. channel.send(reply, e_context["context"])
  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. user_id = _find_user_id(context)
  250. if user_id:
  251. return USER_FILE_MAP.get(user_id + "-file_id")
  252. USER_FILE_MAP = ExpiredDict(conf().get("expires_in_seconds") or 60 * 30)