您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

82 行
3.7KB

  1. # encoding:utf-8
  2. from bridge.bridge import Bridge
  3. from bridge.context import ContextType
  4. from bridge.reply import Reply, ReplyType
  5. from common.expired_dict import ExpiredDict
  6. from config import conf
  7. import plugins
  8. from plugins import *
  9. from common.log import logger
  10. # https://github.com/bupticybee/ChineseAiDungeonChatGPT
  11. class StoryTeller():
  12. def __init__(self, bot, sessionid, story):
  13. self.bot = bot
  14. self.sessionid = sessionid
  15. bot.sessions.clear_session(sessionid)
  16. self.first_interact = True
  17. self.story = story
  18. def reset(self):
  19. self.bot.sessions.clear_session(self.sessionid)
  20. self.first_interact = True
  21. def action(self, user_action):
  22. if user_action[-1] != "。":
  23. user_action = user_action + "。"
  24. if self.first_interact:
  25. prompt = """现在来充当一个冒险文字游戏,描述时候注意节奏,不要太快,仔细描述各个人物的心情和周边环境。一次只需写四到六句话。
  26. 开头是,""" + self.story + " " + user_action
  27. self.first_interact = False
  28. else:
  29. prompt = """继续,一次只需要续写四到六句话,总共就只讲5分钟内发生的事情。""" + user_action
  30. return prompt
  31. @plugins.register(name="Dungeon", desc="A plugin to play dungeon game", version="1.0", author="lanvent", desire_priority= 0)
  32. class Dungeon(Plugin):
  33. def __init__(self):
  34. super().__init__()
  35. self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context
  36. logger.info("[Dungeon] inited")
  37. # 目前没有设计session过期事件,这里先暂时使用过期字典
  38. if conf().get('expires_in_seconds'):
  39. self.games = ExpiredDict(conf().get('expires_in_seconds'))
  40. else:
  41. self.games = dict()
  42. def on_handle_context(self, e_context: EventContext):
  43. if e_context['context'].type != ContextType.TEXT:
  44. return
  45. bottype = Bridge().get_bot_type("chat")
  46. if bottype != "chatGPT":
  47. return
  48. bot = Bridge().get_bot("chat")
  49. content = e_context['context'].content[:]
  50. clist = e_context['context'].content.split(maxsplit=1)
  51. sessionid = e_context['context']['session_id']
  52. logger.debug("[Dungeon] on_handle_context. content: %s" % clist)
  53. if clist[0] == "$停止冒险":
  54. if sessionid in self.games:
  55. self.games[sessionid].reset()
  56. del self.games[sessionid]
  57. reply = Reply(ReplyType.INFO, "冒险结束!")
  58. e_context['reply'] = reply
  59. e_context.action = EventAction.BREAK_PASS
  60. elif clist[0] == "$开始冒险" or sessionid in self.games:
  61. if sessionid not in self.games or clist[0] == "$开始冒险":
  62. if len(clist)>1 :
  63. story = clist[1]
  64. else:
  65. story = "你在树林里冒险,指不定会从哪里蹦出来一些奇怪的东西,你握紧手上的手枪,希望这次冒险能够找到一些值钱的东西,你往树林深处走去。"
  66. self.games[sessionid] = StoryTeller(bot, sessionid, story)
  67. reply = Reply(ReplyType.INFO, "冒险开始,你可以输入任意内容,让故事继续下去。故事背景是:" + story)
  68. e_context['reply'] = reply
  69. e_context.action = EventAction.BREAK_PASS # 事件结束,并跳过处理context的默认逻辑
  70. else:
  71. prompt = self.games[sessionid].action(content)
  72. e_context['context'].type = ContextType.TEXT
  73. e_context['context'].content = prompt
  74. e_context.action = EventAction.CONTINUE