mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-08 14:56:03 +00:00
feat: 完成会话管理所有指令
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
from mirai import At, GroupMessage, MessageEvent, Mirai, Plain, StrangerMessage, WebSocketAdapter, FriendMessage, Image
|
||||
import pkg.openai.session
|
||||
from func_timeout import func_set_timeout, FunctionTimedOut
|
||||
import datetime
|
||||
|
||||
help_text = """帮助信息:
|
||||
!help - 显示帮助
|
||||
!reset - 重置会话
|
||||
!last - 切换到上一次的对话
|
||||
!next - 切换到下一次的对话
|
||||
"""
|
||||
!prompt - 显示当前对话所有内容
|
||||
!list - 列出所有历史会话"""
|
||||
|
||||
inst = None
|
||||
|
||||
@@ -58,17 +60,62 @@ class QQBotManager:
|
||||
session_name = "{}_{}".format(launcher_type, launcher_id)
|
||||
|
||||
if text_message.startswith('!') or text_message.startswith("!"): # 指令
|
||||
cmd = text_message[1:].strip()
|
||||
cmd = text_message[1:].strip().split(' ')[0]
|
||||
|
||||
params = text_message[1:].strip().split(' ')[1:]
|
||||
if cmd == 'help':
|
||||
reply = "[bot]" + help_text
|
||||
elif cmd == 'reset':
|
||||
pkg.openai.session.get_session(session_name).reset(explicit=True)
|
||||
reply = "[bot]会话已重置"
|
||||
elif cmd == 'last':
|
||||
pass
|
||||
result = pkg.openai.session.get_session(session_name).last_session()
|
||||
if result is None:
|
||||
reply = "[bot]没有上一次的对话"
|
||||
else:
|
||||
datetime_str = datetime.datetime.fromtimestamp(result.create_timestamp).strftime('%Y-%m-%d %H:%M:%S')
|
||||
reply = "[bot]已切换到上一次的对话:\n创建时间:{}\n".format(datetime_str) + result.prompt[:min(100, len(result.prompt))] + \
|
||||
("..." if len(result.prompt) > 100 else "#END#")
|
||||
elif cmd == 'next':
|
||||
pass
|
||||
result = pkg.openai.session.get_session(session_name).next_session()
|
||||
if result is None:
|
||||
reply = "[bot]没有下一次的对话"
|
||||
else:
|
||||
datetime_str = datetime.datetime.fromtimestamp(result.create_timestamp).strftime('%Y-%m-%d %H:%M:%S')
|
||||
reply = "[bot]已切换到下一次的对话:\n创建时间:{}\n".format(datetime_str) + result.prompt[:min(100, len(result.prompt))] + \
|
||||
("..." if len(result.prompt) > 100 else "#END#")
|
||||
elif cmd == 'prompt':
|
||||
reply = "[bot]当前对话所有内容:\n" + pkg.openai.session.get_session(session_name).prompt
|
||||
elif cmd == 'list':
|
||||
pkg.openai.session.get_session(session_name).persistence()
|
||||
page = 0
|
||||
|
||||
if len(params) > 0:
|
||||
try:
|
||||
page = int(params[0])
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
results = pkg.openai.session.get_session(session_name).list_history(page=page)
|
||||
if len(results) == 0:
|
||||
reply = "[bot]第{}页没有历史会话".format(page)
|
||||
else:
|
||||
reply = "[bot]历史会话 第{}页:\n".format(page)
|
||||
current = -1
|
||||
for i in range(len(results)):
|
||||
# 时间(使用create_timestamp转换) 序号 部分内容
|
||||
datetime_obj = datetime.datetime.fromtimestamp(results[i]['create_timestamp'])
|
||||
reply += "#{} 创建:{} {}\n".format(i + page * 10, datetime_obj.strftime("%Y-%m-%d %H:%M:%S"),
|
||||
results[i]['prompt'][:min(20, len(results[i]['prompt']))])
|
||||
if results[i]['create_timestamp'] == pkg.openai.session.get_session(session_name).create_timestamp:
|
||||
current = i + page * 10
|
||||
|
||||
reply += "以上信息倒序排列"
|
||||
if current != -1:
|
||||
reply += ",当前会话是 #{}\n".format(current)
|
||||
else:
|
||||
reply += ",当前处于全新会话"
|
||||
|
||||
else: # 消息
|
||||
session = pkg.openai.session.get_session(session_name)
|
||||
reply = "[GPT]" + session.append(text_message)
|
||||
|
||||
Reference in New Issue
Block a user