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.

пре 1 година
пре 1 година
пре 1 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 self.config:
  20. self.mj_bot = MJBot(self.config.get("midjourney"))
  21. logger.info("[LinkAI] inited")
  22. def on_handle_context(self, e_context: EventContext):
  23. """
  24. 消息处理逻辑
  25. :param e_context: 消息上下文
  26. """
  27. if not self.config:
  28. return
  29. context = e_context['context']
  30. if context.type not in [ContextType.TEXT, ContextType.IMAGE, ContextType.IMAGE_CREATE]:
  31. # filter content no need solve
  32. return
  33. mj_type = self.mj_bot.judge_mj_task_type(e_context)
  34. if mj_type:
  35. # MJ作图任务处理
  36. self.mj_bot.process_mj_task(mj_type, e_context)
  37. return
  38. if context.content.startswith(f"{_get_trigger_prefix()}linkai"):
  39. # 应用管理功能
  40. self._process_admin_cmd(e_context)
  41. return
  42. if self._is_chat_task(e_context):
  43. # 文本对话任务处理
  44. self._process_chat_task(e_context)
  45. # 插件管理功能
  46. def _process_admin_cmd(self, e_context: EventContext):
  47. context = e_context['context']
  48. cmd = context.content.split()
  49. if len(cmd) == 1 or (len(cmd) == 2 and cmd[1] == "help"):
  50. _set_reply_text(self.get_help_text(verbose=True), e_context, level=ReplyType.INFO)
  51. return
  52. if len(cmd) == 2 and (cmd[1] == "open" or cmd[1] == "close"):
  53. # 知识库开关指令
  54. if not _is_admin(e_context):
  55. _set_reply_text("需要管理员权限执行", e_context, level=ReplyType.ERROR)
  56. return
  57. is_open = True
  58. tips_text = "开启"
  59. if cmd[1] == "close":
  60. tips_text = "关闭"
  61. is_open = False
  62. conf()["use_linkai"] = is_open
  63. bridge.Bridge().reset_bot()
  64. _set_reply_text(f"知识库功能已{tips_text}", e_context, level=ReplyType.INFO)
  65. return
  66. if len(cmd) == 3 and cmd[1] == "app":
  67. # 知识库应用切换指令
  68. if not context.kwargs.get("isgroup"):
  69. _set_reply_text("该指令需在群聊中使用", e_context, level=ReplyType.ERROR)
  70. return
  71. if not _is_admin(e_context):
  72. _set_reply_text("需要管理员权限执行", e_context, level=ReplyType.ERROR)
  73. return
  74. app_code = cmd[2]
  75. group_name = context.kwargs.get("msg").from_user_nickname
  76. group_mapping = self.config.get("group_app_map")
  77. if group_mapping:
  78. group_mapping[group_name] = app_code
  79. else:
  80. self.config["group_app_map"] = {group_name: app_code}
  81. # 保存插件配置
  82. super().save_config(self.config)
  83. _set_reply_text(f"应用设置成功: {app_code}", e_context, level=ReplyType.INFO)
  84. else:
  85. _set_reply_text(f"指令错误,请输入{_get_trigger_prefix()}linkai help 获取帮助", e_context,
  86. level=ReplyType.INFO)
  87. return
  88. # LinkAI 对话任务处理
  89. def _is_chat_task(self, e_context: EventContext):
  90. context = e_context['context']
  91. # 群聊应用管理
  92. return self.config.get("group_app_map") and context.kwargs.get("isgroup")
  93. def _process_chat_task(self, e_context: EventContext):
  94. """
  95. 处理LinkAI对话任务
  96. :param e_context: 对话上下文
  97. """
  98. context = e_context['context']
  99. # 群聊应用管理
  100. group_name = context.kwargs.get("msg").from_user_nickname
  101. app_code = self._fetch_group_app_code(group_name)
  102. if app_code:
  103. context.kwargs['app_code'] = app_code
  104. def _fetch_group_app_code(self, group_name: str) -> str:
  105. """
  106. 根据群聊名称获取对应的应用code
  107. :param group_name: 群聊名称
  108. :return: 应用code
  109. """
  110. group_mapping = self.config.get("group_app_map")
  111. if group_mapping:
  112. app_code = group_mapping.get(group_name) or group_mapping.get("ALL_GROUP")
  113. return app_code
  114. def get_help_text(self, verbose=False, **kwargs):
  115. trigger_prefix = _get_trigger_prefix()
  116. help_text = "用于集成 LinkAI 提供的知识库、Midjourney绘画等能力。\n\n"
  117. if not verbose:
  118. return help_text
  119. help_text += f'📖 知识库\n - 群聊中指定应用: {trigger_prefix}linkai app 应用编码\n\n例如: \n"$linkai app Kv2fXJcH"\n\n'
  120. help_text += f"🎨 绘画\n - 生成: {trigger_prefix}mj 描述词1, 描述词2.. \n - 放大: {trigger_prefix}mju 图片ID 图片序号\n - 变换: mjv 图片ID 图片序号\n - 重置: mjr 图片ID"
  121. help_text += f"\n\n例如:\n\"{trigger_prefix}mj a little cat, white --ar 9:16\"\n\"{trigger_prefix}mju 11055927171882 2\""
  122. help_text += f"\n\"{trigger_prefix}mjv 11055927171882 2\"\n\"{trigger_prefix}mjr 11055927171882\""
  123. return help_text
  124. # 静态方法
  125. def _is_admin(e_context: EventContext) -> bool:
  126. """
  127. 判断消息是否由管理员用户发送
  128. :param e_context: 消息上下文
  129. :return: True: 是, False: 否
  130. """
  131. context = e_context["context"]
  132. if context["isgroup"]:
  133. return context.kwargs.get("msg").actual_user_id in global_config["admin_users"]
  134. else:
  135. return context["receiver"] in global_config["admin_users"]
  136. def _set_reply_text(content: str, e_context: EventContext, level: ReplyType = ReplyType.ERROR):
  137. reply = Reply(level, content)
  138. e_context["reply"] = reply
  139. e_context.action = EventAction.BREAK_PASS
  140. def _get_trigger_prefix():
  141. return conf().get("plugin_trigger_prefix", "$")