fix: 修复processing记录bug

This commit is contained in:
Rock Chin 2022-12-08 21:58:02 +08:00
parent acf6110a85
commit b39bc076c9
3 changed files with 45 additions and 20 deletions

View File

@ -85,6 +85,11 @@ class DatabaseManager:
update `sessions` set `status` = 'explicitly_closed' where `name` = '{}' and `create_timestamp` = {}
""".format(session_name, create_timestamp))
def set_session_ongoing(self, session_name: str, create_timestamp: int):
self.cursor.execute("""
update `sessions` set `status` = 'on_going' where `name` = '{}' and `create_timestamp` = {}
""".format(session_name, create_timestamp))
# 记载还没过期的session数据
def load_valid_sessions(self) -> dict:
# 从数据库中加载所有还没过期的session
@ -202,6 +207,7 @@ class DatabaseManager:
return sessions
def get_inst() -> DatabaseManager:
global inst
return inst

View File

@ -56,6 +56,8 @@ class Session:
last_interact_timestamp = 0
just_switched_to_exist_session = False
def __init__(self, name: str):
self.name = name
self.create_timestamp = int(time.time())
@ -82,6 +84,11 @@ class Session:
res_ans = '\n\n'.join(res_ans_spt)
self.prompt += "{}".format(res_ans) + '\n'
if self.just_switched_to_exist_session:
self.just_switched_to_exist_session = False
self.set_ongoing()
return res_ans
def persistence(self):
@ -106,6 +113,11 @@ class Session:
self.prompt = ''
self.create_timestamp = int(time.time())
self.last_interact_timestamp = int(time.time())
self.just_switched_to_exist_session = False
# 将本session的数据库状态设置为on_going
def set_ongoing(self):
pkg.database.manager.get_inst().set_session_ongoing(self.name, self.create_timestamp)
# 切换到上一个session
def last_session(self):
@ -118,6 +130,8 @@ class Session:
self.create_timestamp = last_one['create_timestamp']
self.last_interact_timestamp = last_one['last_interact_timestamp']
self.prompt = last_one['prompt']
just_switched = True
return self
def next_session(self):
@ -130,6 +144,8 @@ class Session:
self.create_timestamp = next_one['create_timestamp']
self.last_interact_timestamp = next_one['last_interact_timestamp']
self.prompt = next_one['prompt']
just_switched = True
return self
def list_history(self, capacity: int = 10, page: int = 0):

View File

@ -2,6 +2,7 @@ from mirai import At, GroupMessage, MessageEvent, Mirai, Plain, StrangerMessage,
import pkg.openai.session
from func_timeout import func_set_timeout, FunctionTimedOut
import datetime
import logging
help_text = """帮助信息:
!help - 显示帮助
@ -114,7 +115,7 @@ class QQBotManager:
if current != -1:
reply += ",当前会话是 #{}\n".format(current)
else:
reply += ",当前处于全新会话"
reply += ",当前处于全新会话或不在此页"
else: # 消息
session = pkg.openai.session.get_session(session_name)
@ -126,6 +127,7 @@ class QQBotManager:
return reply
async def on_person_message(self, event: MessageEvent):
global processing
if "person_{}".format(event.sender.id) in processing:
return await self.bot.send(event, "err:正在处理中,请稍后再试")
@ -158,6 +160,7 @@ class QQBotManager:
return await self.bot.send(event, reply)
async def on_group_message(self, event: GroupMessage):
global processing
if "group_{}".format(event.group.id) in processing:
return await self.bot.send(event, "err:正在处理中,请稍后再试")