Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

179 lines
7.1KB

  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 bridge import bridge
  8. @plugins.register(
  9. name="linkai",
  10. desc="A plugin that supports knowledge base and midjourney drawing.",
  11. version="0.1.0",
  12. author="https://link-ai.tech",
  13. )
  14. class LinkAI(Plugin):
  15. def __init__(self):
  16. super().__init__()
  17. self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context
  18. self.config = super().load_config()
  19. if not self.config:
  20. # 未加载到配置,使用模板中的配置
  21. self.config = self._load_config_template()
  22. if self.config:
  23. self.mj_bot = MJBot(self.config.get("midjourney"))
  24. logger.info("[LinkAI] inited")
  25. def on_handle_context(self, e_context: EventContext):
  26. """
  27. 消息处理逻辑
  28. :param e_context: 消息上下文
  29. """
  30. if not self.config:
  31. return
  32. context = e_context['context']
  33. if context.type not in [ContextType.TEXT, ContextType.IMAGE, ContextType.IMAGE_CREATE]:
  34. # filter content no need solve
  35. return
  36. mj_type = self.mj_bot.judge_mj_task_type(e_context)
  37. if mj_type:
  38. # MJ作图任务处理
  39. self.mj_bot.process_mj_task(mj_type, e_context)
  40. return
  41. if context.content.startswith(f"{_get_trigger_prefix()}linkai"):
  42. # 应用管理功能
  43. self._process_admin_cmd(e_context)
  44. return
  45. if self._is_chat_task(e_context):
  46. # 文本对话任务处理
  47. self._process_chat_task(e_context)
  48. # 插件管理功能
  49. def _process_admin_cmd(self, e_context: EventContext):
  50. context = e_context['context']
  51. cmd = context.content.split()
  52. if len(cmd) == 1 or (len(cmd) == 2 and cmd[1] == "help"):
  53. _set_reply_text(self.get_help_text(verbose=True), e_context, level=ReplyType.INFO)
  54. return
  55. if len(cmd) == 2 and (cmd[1] == "open" or cmd[1] == "close"):
  56. # 知识库开关指令
  57. if not _is_admin(e_context):
  58. _set_reply_text("需要管理员权限执行", e_context, level=ReplyType.ERROR)
  59. return
  60. is_open = True
  61. tips_text = "开启"
  62. if cmd[1] == "close":
  63. tips_text = "关闭"
  64. is_open = False
  65. conf()["use_linkai"] = is_open
  66. bridge.Bridge().reset_bot()
  67. _set_reply_text(f"LinkAI对话功能{tips_text}", e_context, level=ReplyType.INFO)
  68. return
  69. if len(cmd) == 3 and cmd[1] == "app":
  70. # 知识库应用切换指令
  71. if not context.kwargs.get("isgroup"):
  72. _set_reply_text("该指令需在群聊中使用", e_context, level=ReplyType.ERROR)
  73. return
  74. if not _is_admin(e_context):
  75. _set_reply_text("需要管理员权限执行", e_context, level=ReplyType.ERROR)
  76. return
  77. app_code = cmd[2]
  78. group_name = context.kwargs.get("msg").from_user_nickname
  79. group_mapping = self.config.get("group_app_map")
  80. if group_mapping:
  81. group_mapping[group_name] = app_code
  82. else:
  83. self.config["group_app_map"] = {group_name: app_code}
  84. # 保存插件配置
  85. super().save_config(self.config)
  86. _set_reply_text(f"应用设置成功: {app_code}", e_context, level=ReplyType.INFO)
  87. else:
  88. _set_reply_text(f"指令错误,请输入{_get_trigger_prefix()}linkai help 获取帮助", e_context,
  89. level=ReplyType.INFO)
  90. return
  91. # LinkAI 对话任务处理
  92. def _is_chat_task(self, e_context: EventContext):
  93. context = e_context['context']
  94. # 群聊应用管理
  95. return self.config.get("group_app_map") and context.kwargs.get("isgroup")
  96. def _process_chat_task(self, e_context: EventContext):
  97. """
  98. 处理LinkAI对话任务
  99. :param e_context: 对话上下文
  100. """
  101. context = e_context['context']
  102. # 群聊应用管理
  103. group_name = context.kwargs.get("msg").from_user_nickname
  104. app_code = self._fetch_group_app_code(group_name)
  105. if app_code:
  106. context.kwargs['app_code'] = app_code
  107. def _fetch_group_app_code(self, group_name: str) -> str:
  108. """
  109. 根据群聊名称获取对应的应用code
  110. :param group_name: 群聊名称
  111. :return: 应用code
  112. """
  113. group_mapping = self.config.get("group_app_map")
  114. if group_mapping:
  115. app_code = group_mapping.get(group_name) or group_mapping.get("ALL_GROUP")
  116. return app_code
  117. def get_help_text(self, verbose=False, **kwargs):
  118. trigger_prefix = _get_trigger_prefix()
  119. help_text = "用于集成 LinkAI 提供的知识库、Midjourney绘画等能力。\n\n"
  120. if not verbose:
  121. return help_text
  122. help_text += f'📖 知识库\n - 群聊中指定应用: {trigger_prefix}linkai app 应用编码\n'
  123. help_text += f' - {trigger_prefix}linkai open: 开启对话\n'
  124. help_text += f' - {trigger_prefix}linkai close: 关闭对话\n'
  125. help_text += f'\n例如: \n"{trigger_prefix}linkai app Kv2fXJcH"\n\n'
  126. help_text += f"🎨 绘画\n - 生成: {trigger_prefix}mj 描述词1, 描述词2.. \n - 放大: {trigger_prefix}mju 图片ID 图片序号\n - 变换: {trigger_prefix}mjv 图片ID 图片序号\n - 重置: {trigger_prefix}mjr 图片ID"
  127. help_text += f"\n\n例如:\n\"{trigger_prefix}mj a little cat, white --ar 9:16\"\n\"{trigger_prefix}mju 11055927171882 2\""
  128. help_text += f"\n\"{trigger_prefix}mjv 11055927171882 2\"\n\"{trigger_prefix}mjr 11055927171882\""
  129. return help_text
  130. def _load_config_template(self):
  131. logger.debug("No LinkAI plugin config.json, use plugins/linkai/config.json.template")
  132. try:
  133. plugin_config_path = os.path.join(self.path, "config.json.template")
  134. if os.path.exists(plugin_config_path):
  135. with open(plugin_config_path, "r", encoding="utf-8") as f:
  136. plugin_conf = json.load(f)
  137. plugin_conf["midjourney"]["enabled"] = False
  138. return plugin_conf
  139. except Exception as e:
  140. logger.exception(e)
  141. # 静态方法
  142. def _is_admin(e_context: EventContext) -> bool:
  143. """
  144. 判断消息是否由管理员用户发送
  145. :param e_context: 消息上下文
  146. :return: True: 是, False: 否
  147. """
  148. context = e_context["context"]
  149. if context["isgroup"]:
  150. return context.kwargs.get("msg").actual_user_id in global_config["admin_users"]
  151. else:
  152. return context["receiver"] in global_config["admin_users"]
  153. def _set_reply_text(content: str, e_context: EventContext, level: ReplyType = ReplyType.ERROR):
  154. reply = Reply(level, content)
  155. e_context["reply"] = reply
  156. e_context.action = EventAction.BREAK_PASS
  157. def _get_trigger_prefix():
  158. return conf().get("plugin_trigger_prefix", "$")