mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-07-20 13:47:15 +08:00
plugin: avoid mess after session expiration
This commit is contained in:
@@ -3,6 +3,8 @@
|
|||||||
from bridge.bridge import Bridge
|
from bridge.bridge import Bridge
|
||||||
from bridge.context import ContextType
|
from bridge.context import ContextType
|
||||||
from bridge.reply import Reply, ReplyType
|
from bridge.reply import Reply, ReplyType
|
||||||
|
from common.expired_dict import ExpiredDict
|
||||||
|
from config import conf
|
||||||
import plugins
|
import plugins
|
||||||
from plugins import *
|
from plugins import *
|
||||||
from common.log import logger
|
from common.log import logger
|
||||||
@@ -38,7 +40,11 @@ class Dungeon(Plugin):
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context
|
self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context
|
||||||
logger.info("[Dungeon] inited")
|
logger.info("[Dungeon] inited")
|
||||||
self.games = {}
|
# 目前没有设计session过期事件,这里先暂时使用过期字典
|
||||||
|
if conf().get('expires_in_seconds'):
|
||||||
|
self.games = ExpiredDict(conf().get('expires_in_seconds'))
|
||||||
|
else:
|
||||||
|
self.games = dict()
|
||||||
|
|
||||||
def on_handle_context(self, e_context: EventContext):
|
def on_handle_context(self, e_context: EventContext):
|
||||||
|
|
||||||
|
|||||||
@@ -14,14 +14,17 @@ class RolePlay():
|
|||||||
def __init__(self, bot, sessionid, desc, wrapper=None):
|
def __init__(self, bot, sessionid, desc, wrapper=None):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.sessionid = sessionid
|
self.sessionid = sessionid
|
||||||
bot.sessions.clear_session(sessionid)
|
|
||||||
bot.sessions.build_session(sessionid, desc)
|
|
||||||
self.wrapper = wrapper or "%s" # 用于包装用户输入
|
self.wrapper = wrapper or "%s" # 用于包装用户输入
|
||||||
|
self.desc = desc
|
||||||
|
|
||||||
def reset(self):
|
def reset(self):
|
||||||
self.bot.sessions.clear_session(self.sessionid)
|
self.bot.sessions.clear_session(self.sessionid)
|
||||||
|
|
||||||
def action(self, user_action):
|
def action(self, user_action):
|
||||||
|
session = self.bot.sessions.build_session(self.sessionid, self.desc)
|
||||||
|
if session[0]['role'] == 'system' and session[0]['content'] != self.desc: # 目前没有触发session过期事件,这里先简单判断,然后重置
|
||||||
|
self.reset()
|
||||||
|
self.bot.sessions.build_session(self.sessionid, self.desc)
|
||||||
prompt = self.wrapper % user_action
|
prompt = self.wrapper % user_action
|
||||||
return prompt
|
return prompt
|
||||||
|
|
||||||
@@ -105,7 +108,7 @@ class Role(Plugin):
|
|||||||
e_context.action = EventAction.BREAK_PASS
|
e_context.action = EventAction.BREAK_PASS
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
self.roleplays[sessionid] = RolePlay(bot, sessionid, self.roles[role][desckey],self.roles[role].get("wrapper","%s"))
|
self.roleplays[sessionid] = RolePlay(bot, sessionid, self.roles[role][desckey], self.roles[role].get("wrapper","%s"))
|
||||||
reply = Reply(ReplyType.INFO, f"角色设定为 {role} :\n"+self.roles[role][desckey])
|
reply = Reply(ReplyType.INFO, f"角色设定为 {role} :\n"+self.roles[role][desckey])
|
||||||
e_context['reply'] = reply
|
e_context['reply'] = reply
|
||||||
e_context.action = EventAction.BREAK_PASS
|
e_context.action = EventAction.BREAK_PASS
|
||||||
|
|||||||
Reference in New Issue
Block a user