refactor: move commands to seperated plugin

This commit is contained in:
Junyan Qin
2025-09-15 18:10:18 +08:00
parent 6741850081
commit 521a941792
14 changed files with 186 additions and 468 deletions

View File

@@ -39,9 +39,9 @@ class CommandManager:
set_path(cls, [])
# 应用命令权限配置
for cls in operator.preregistered_operators:
if cls.path in self.ap.instance_config.data['command']['privilege']:
cls.lowest_privilege = self.ap.instance_config.data['command']['privilege'][cls.path]
# for cls in operator.preregistered_operators:
# if cls.path in self.ap.instance_config.data['command']['privilege']:
# cls.lowest_privilege = self.ap.instance_config.data['command']['privilege'][cls.path]
# 实例化所有类
self.cmd_list = [cls(self.ap) for cls in operator.preregistered_operators]

View File

@@ -1,44 +0,0 @@
from __future__ import annotations
import typing
from .. import operator
from langbot_plugin.api.entities.builtin.command import context as command_context, errors as command_errors
@operator.operator_class(name='cmd', help='显示命令列表', usage='!cmd\n!cmd <命令名称>')
class CmdOperator(operator.CommandOperator):
"""命令列表"""
async def execute(
self, context: command_context.ExecuteContext
) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
"""执行"""
if len(context.crt_params) == 0:
reply_str = '当前所有命令: \n\n'
for cmd in self.ap.cmd_mgr.cmd_list:
if cmd.parent_class is None:
reply_str += f'{cmd.name}: {cmd.help}\n'
reply_str += '\n使用 !cmd <命令名称> 查看命令的详细帮助'
yield command_context.CommandReturn(text=reply_str.strip())
else:
cmd_name = context.crt_params[0]
cmd = None
for _cmd in self.ap.cmd_mgr.cmd_list:
if (cmd_name == _cmd.name or cmd_name in _cmd.alias) and (_cmd.parent_class is None):
cmd = _cmd
break
if cmd is None:
yield command_context.CommandReturn(error=command_errors.CommandNotFoundError(cmd_name))
else:
reply_str = f'{cmd.name}: {cmd.help}\n\n'
reply_str += f'使用方法: \n{cmd.usage}'
yield command_context.CommandReturn(text=reply_str.strip())

View File

@@ -1,48 +1,48 @@
from __future__ import annotations
# from __future__ import annotations
import typing
# import typing
from .. import operator
from langbot_plugin.api.entities.builtin.command import context as command_context, errors as command_errors
# from .. import operator
# from langbot_plugin.api.entities.builtin.command import context as command_context, errors as command_errors
@operator.operator_class(name='del', help='删除当前会话的历史记录', usage='!del <序号>\n!del all')
class DelOperator(operator.CommandOperator):
async def execute(
self, context: command_context.ExecuteContext
) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
if context.session.conversations:
delete_index = 0
if len(context.crt_params) > 0:
try:
delete_index = int(context.crt_params[0])
except Exception:
yield command_context.CommandReturn(error=command_errors.CommandOperationError('索引必须是整数'))
return
# @operator.operator_class(name='del', help='删除当前会话的历史记录', usage='!del <序号>\n!del all')
# class DelOperator(operator.CommandOperator):
# async def execute(
# self, context: command_context.ExecuteContext
# ) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
# if context.session.conversations:
# delete_index = 0
# if len(context.crt_params) > 0:
# try:
# delete_index = int(context.crt_params[0])
# except Exception:
# yield command_context.CommandReturn(error=command_errors.CommandOperationError('索引必须是整数'))
# return
if delete_index < 0 or delete_index >= len(context.session.conversations):
yield command_context.CommandReturn(error=command_errors.CommandOperationError('索引超出范围'))
return
# if delete_index < 0 or delete_index >= len(context.session.conversations):
# yield command_context.CommandReturn(error=command_errors.CommandOperationError('索引超出范围'))
# return
# 倒序
to_delete_index = len(context.session.conversations) - 1 - delete_index
# # 倒序
# to_delete_index = len(context.session.conversations) - 1 - delete_index
if context.session.conversations[to_delete_index] == context.session.using_conversation:
context.session.using_conversation = None
# if context.session.conversations[to_delete_index] == context.session.using_conversation:
# context.session.using_conversation = None
del context.session.conversations[to_delete_index]
# del context.session.conversations[to_delete_index]
yield command_context.CommandReturn(text=f'已删除对话: {delete_index}')
else:
yield command_context.CommandReturn(error=command_errors.CommandOperationError('当前没有对话'))
# yield command_context.CommandReturn(text=f'已删除对话: {delete_index}')
# else:
# yield command_context.CommandReturn(error=command_errors.CommandOperationError('当前没有对话'))
@operator.operator_class(name='all', help='删除此会话的所有历史记录', parent_class=DelOperator)
class DelAllOperator(operator.CommandOperator):
async def execute(
self, context: command_context.ExecuteContext
) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
context.session.conversations = []
context.session.using_conversation = None
# @operator.operator_class(name='all', help='删除此会话的所有历史记录', parent_class=DelOperator)
# class DelAllOperator(operator.CommandOperator):
# async def execute(
# self, context: command_context.ExecuteContext
# ) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
# context.session.conversations = []
# context.session.using_conversation = None
yield command_context.CommandReturn(text='已删除所有对话')
# yield command_context.CommandReturn(text='已删除所有对话')

View File

@@ -1,27 +0,0 @@
from __future__ import annotations
from typing import AsyncGenerator
from .. import operator
from langbot_plugin.api.entities.builtin.command import context as command_context
@operator.operator_class(name='func', help='查看所有已注册的内容函数', usage='!func')
class FuncOperator(operator.CommandOperator):
async def execute(
self, context: command_context.ExecuteContext
) -> AsyncGenerator[command_context.CommandReturn, None]:
reply_str = '当前已启用的内容函数: \n\n'
index = 1
all_functions = await self.ap.tool_mgr.get_all_tools()
for func in all_functions:
reply_str += '{}. {}:\n{}\n\n'.format(
index,
func.name,
func.description,
)
index += 1
yield command_context.CommandReturn(text=reply_str)

View File

@@ -1,18 +0,0 @@
from __future__ import annotations
import typing
from .. import operator
from langbot_plugin.api.entities.builtin.command import context as command_context
@operator.operator_class(name='help', help='显示帮助', usage='!help\n!help <命令名称>')
class HelpOperator(operator.CommandOperator):
async def execute(
self, context: command_context.ExecuteContext
) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
help = 'LangBot - 大语言模型原生即时通信机器人平台\n链接https://langbot.app'
help += '\n发送命令 !cmd 可查看命令列表'
yield command_context.CommandReturn(text=help)

View File

@@ -1,33 +1,33 @@
from __future__ import annotations
# from __future__ import annotations
import typing
# import typing
from .. import operator
from langbot_plugin.api.entities.builtin.command import context as command_context, errors as command_errors
# from .. import operator
# from langbot_plugin.api.entities.builtin.command import context as command_context, errors as command_errors
@operator.operator_class(name='last', help='切换到前一个对话', usage='!last')
class LastOperator(operator.CommandOperator):
async def execute(
self, context: command_context.ExecuteContext
) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
if context.session.conversations:
# 找到当前会话的上一个会话
for index in range(len(context.session.conversations) - 1, -1, -1):
if context.session.conversations[index] == context.session.using_conversation:
if index == 0:
yield command_context.CommandReturn(
error=command_errors.CommandOperationError('已经是第一个对话了')
)
return
else:
context.session.using_conversation = context.session.conversations[index - 1]
time_str = context.session.using_conversation.create_time.strftime('%Y-%m-%d %H:%M:%S')
# @operator.operator_class(name='last', help='切换到前一个对话', usage='!last')
# class LastOperator(operator.CommandOperator):
# async def execute(
# self, context: command_context.ExecuteContext
# ) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
# if context.session.conversations:
# # 找到当前会话的上一个会话
# for index in range(len(context.session.conversations) - 1, -1, -1):
# if context.session.conversations[index] == context.session.using_conversation:
# if index == 0:
# yield command_context.CommandReturn(
# error=command_errors.CommandOperationError('已经是第一个对话了')
# )
# return
# else:
# context.session.using_conversation = context.session.conversations[index - 1]
# time_str = context.session.using_conversation.create_time.strftime('%Y-%m-%d %H:%M:%S')
yield command_context.CommandReturn(
text=f'已切换到上一个对话: {index} {time_str}: {context.session.using_conversation.messages[0].readable_str()}'
)
return
else:
yield command_context.CommandReturn(error=command_errors.CommandOperationError('当前没有对话'))
# yield command_context.CommandReturn(
# text=f'已切换到上一个对话: {index} {time_str}: {context.session.using_conversation.messages[0].readable_str()}'
# )
# return
# else:
# yield command_context.CommandReturn(error=command_errors.CommandOperationError('当前没有对话'))

View File

@@ -1,51 +1,51 @@
from __future__ import annotations
# from __future__ import annotations
import typing
# import typing
from .. import operator
from langbot_plugin.api.entities.builtin.command import context as command_context, errors as command_errors
# from .. import operator
# from langbot_plugin.api.entities.builtin.command import context as command_context, errors as command_errors
@operator.operator_class(name='list', help='列出此会话中的所有历史对话', usage='!list\n!list <页码>')
class ListOperator(operator.CommandOperator):
async def execute(
self, context: command_context.ExecuteContext
) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
page = 0
# @operator.operator_class(name='list', help='列出此会话中的所有历史对话', usage='!list\n!list <页码>')
# class ListOperator(operator.CommandOperator):
# async def execute(
# self, context: command_context.ExecuteContext
# ) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
# page = 0
if len(context.crt_params) > 0:
try:
page = int(context.crt_params[0] - 1)
except Exception:
yield command_context.CommandReturn(error=command_errors.CommandOperationError('页码应为整数'))
return
# if len(context.crt_params) > 0:
# try:
# page = int(context.crt_params[0] - 1)
# except Exception:
# yield command_context.CommandReturn(error=command_errors.CommandOperationError('页码应为整数'))
# return
record_per_page = 10
# record_per_page = 10
content = ''
# content = ''
index = 0
# index = 0
using_conv_index = 0
# using_conv_index = 0
for conv in context.session.conversations[::-1]:
time_str = conv.create_time.strftime('%Y-%m-%d %H:%M:%S')
# for conv in context.session.conversations[::-1]:
# time_str = conv.create_time.strftime('%Y-%m-%d %H:%M:%S')
if conv == context.session.using_conversation:
using_conv_index = index
# if conv == context.session.using_conversation:
# using_conv_index = index
if index >= page * record_per_page and index < (page + 1) * record_per_page:
content += (
f'{index} {time_str}: {conv.messages[0].readable_str() if len(conv.messages) > 0 else "无内容"}\n'
)
index += 1
# if index >= page * record_per_page and index < (page + 1) * record_per_page:
# content += (
# f'{index} {time_str}: {conv.messages[0].readable_str() if len(conv.messages) > 0 else "无内容"}\n'
# )
# index += 1
if content == '':
content = ''
else:
if context.session.using_conversation is None:
content += '\n当前处于新会话'
else:
content += f'\n当前会话: {using_conv_index} {context.session.using_conversation.create_time.strftime("%Y-%m-%d %H:%M:%S")}: {context.session.using_conversation.messages[0].readable_str() if len(context.session.using_conversation.messages) > 0 else "无内容"}'
# if content == '':
# content = '无'
# else:
# if context.session.using_conversation is None:
# content += '\n当前处于新会话'
# else:
# content += f'\n当前会话: {using_conv_index} {context.session.using_conversation.create_time.strftime("%Y-%m-%d %H:%M:%S")}: {context.session.using_conversation.messages[0].readable_str() if len(context.session.using_conversation.messages) > 0 else "无内容"}'
yield command_context.CommandReturn(text=f'{page + 1} 页 (时间倒序):\n{content}')
# yield command_context.CommandReturn(text=f'第 {page + 1} 页 (时间倒序):\n{content}')

View File

@@ -1,32 +1,32 @@
from __future__ import annotations
# from __future__ import annotations
import typing
# import typing
from .. import operator
from langbot_plugin.api.entities.builtin.command import context as command_context, errors as command_errors
# from .. import operator
# from langbot_plugin.api.entities.builtin.command import context as command_context, errors as command_errors
@operator.operator_class(name='next', help='切换到后一个对话', usage='!next')
class NextOperator(operator.CommandOperator):
async def execute(
self, context: command_context.ExecuteContext
) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
if context.session.conversations:
# 找到当前会话的下一个会话
for index in range(len(context.session.conversations)):
if context.session.conversations[index] == context.session.using_conversation:
if index == len(context.session.conversations) - 1:
yield command_context.CommandReturn(
error=command_errors.CommandOperationError('已经是最后一个对话了')
)
return
else:
context.session.using_conversation = context.session.conversations[index + 1]
time_str = context.session.using_conversation.create_time.strftime('%Y-%m-%d %H:%M:%S')
# @operator.operator_class(name='next', help='切换到后一个对话', usage='!next')
# class NextOperator(operator.CommandOperator):
# async def execute(
# self, context: command_context.ExecuteContext
# ) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
# if context.session.conversations:
# # 找到当前会话的下一个会话
# for index in range(len(context.session.conversations)):
# if context.session.conversations[index] == context.session.using_conversation:
# if index == len(context.session.conversations) - 1:
# yield command_context.CommandReturn(
# error=command_errors.CommandOperationError('已经是最后一个对话了')
# )
# return
# else:
# context.session.using_conversation = context.session.conversations[index + 1]
# time_str = context.session.using_conversation.create_time.strftime('%Y-%m-%d %H:%M:%S')
yield command_context.CommandReturn(
text=f'已切换到后一个对话: {index} {time_str}: {context.session.using_conversation.messages[0].content}'
)
return
else:
yield command_context.CommandReturn(error=command_errors.CommandOperationError('当前没有对话'))
# yield command_context.CommandReturn(
# text=f'已切换到后一个对话: {index} {time_str}: {context.session.using_conversation.messages[0].content}'
# )
# return
# else:
# yield command_context.CommandReturn(error=command_errors.CommandOperationError('当前没有对话'))

View File

@@ -1,171 +0,0 @@
from __future__ import annotations
import typing
import traceback
from .. import operator
from langbot_plugin.api.entities.builtin.command import context as command_context, errors as command_errors
@operator.operator_class(
name='plugin',
help='插件操作',
usage='!plugin\n!plugin get <插件仓库地址>\n!plugin update\n!plugin del <插件名>\n!plugin on <插件名>\n!plugin off <插件名>',
)
class PluginOperator(operator.CommandOperator):
async def execute(
self, context: command_context.ExecuteContext
) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
plugin_list = self.ap.plugin_mgr.plugins()
reply_str = '所有插件({}):\n'.format(len(plugin_list))
idx = 0
for plugin in plugin_list:
reply_str += '\n#{} {} {}\n{}\nv{}\n作者: {}\n'.format(
(idx + 1),
plugin.plugin_name,
'[已禁用]' if not plugin.enabled else '',
plugin.plugin_description,
plugin.plugin_version,
plugin.plugin_author,
)
idx += 1
yield command_context.CommandReturn(text=reply_str)
@operator.operator_class(name='get', help='安装插件', privilege=2, parent_class=PluginOperator)
class PluginGetOperator(operator.CommandOperator):
async def execute(
self, context: command_context.ExecuteContext
) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
if len(context.crt_params) == 0:
yield command_context.CommandReturn(error=command_errors.ParamNotEnoughError('请提供插件仓库地址'))
else:
repo = context.crt_params[0]
yield command_context.CommandReturn(text='正在安装插件...')
try:
await self.ap.plugin_mgr.install_plugin(repo)
yield command_context.CommandReturn(text='插件安装成功,请重启程序以加载插件')
except Exception as e:
traceback.print_exc()
yield command_context.CommandReturn(error=command_errors.CommandError('插件安装失败: ' + str(e)))
@operator.operator_class(name='update', help='更新插件', privilege=2, parent_class=PluginOperator)
class PluginUpdateOperator(operator.CommandOperator):
async def execute(
self, context: command_context.ExecuteContext
) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
if len(context.crt_params) == 0:
yield command_context.CommandReturn(error=command_errors.ParamNotEnoughError('请提供插件名称'))
else:
plugin_name = context.crt_params[0]
try:
plugin_container = self.ap.plugin_mgr.get_plugin_by_name(plugin_name)
if plugin_container is not None:
yield command_context.CommandReturn(text='正在更新插件...')
await self.ap.plugin_mgr.update_plugin(plugin_name)
yield command_context.CommandReturn(text='插件更新成功,请重启程序以加载插件')
else:
yield command_context.CommandReturn(error=command_errors.CommandError('插件更新失败: 未找到插件'))
except Exception as e:
traceback.print_exc()
yield command_context.CommandReturn(error=command_errors.CommandError('插件更新失败: ' + str(e)))
@operator.operator_class(name='all', help='更新所有插件', privilege=2, parent_class=PluginUpdateOperator)
class PluginUpdateAllOperator(operator.CommandOperator):
async def execute(
self, context: command_context.ExecuteContext
) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
try:
plugins = [p.plugin_name for p in self.ap.plugin_mgr.plugins()]
if plugins:
yield command_context.CommandReturn(text='正在更新插件...')
updated = []
try:
for plugin_name in plugins:
await self.ap.plugin_mgr.update_plugin(plugin_name)
updated.append(plugin_name)
except Exception as e:
traceback.print_exc()
yield command_context.CommandReturn(error=command_errors.CommandError('插件更新失败: ' + str(e)))
yield command_context.CommandReturn(text='已更新插件: {}'.format(', '.join(updated)))
else:
yield command_context.CommandReturn(text='没有可更新的插件')
except Exception as e:
traceback.print_exc()
yield command_context.CommandReturn(error=command_errors.CommandError('插件更新失败: ' + str(e)))
@operator.operator_class(name='del', help='删除插件', privilege=2, parent_class=PluginOperator)
class PluginDelOperator(operator.CommandOperator):
async def execute(
self, context: command_context.ExecuteContext
) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
if len(context.crt_params) == 0:
yield command_context.CommandReturn(error=command_errors.ParamNotEnoughError('请提供插件名称'))
else:
plugin_name = context.crt_params[0]
try:
plugin_container = self.ap.plugin_mgr.get_plugin_by_name(plugin_name)
if plugin_container is not None:
yield command_context.CommandReturn(text='正在删除插件...')
await self.ap.plugin_mgr.uninstall_plugin(plugin_name)
yield command_context.CommandReturn(text='插件删除成功,请重启程序以加载插件')
else:
yield command_context.CommandReturn(error=command_errors.CommandError('插件删除失败: 未找到插件'))
except Exception as e:
traceback.print_exc()
yield command_context.CommandReturn(error=command_errors.CommandError('插件删除失败: ' + str(e)))
@operator.operator_class(name='on', help='启用插件', privilege=2, parent_class=PluginOperator)
class PluginEnableOperator(operator.CommandOperator):
async def execute(
self, context: command_context.ExecuteContext
) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
if len(context.crt_params) == 0:
yield command_context.CommandReturn(error=command_errors.ParamNotEnoughError('请提供插件名称'))
else:
plugin_name = context.crt_params[0]
try:
if await self.ap.plugin_mgr.update_plugin_switch(plugin_name, True):
yield command_context.CommandReturn(text='已启用插件: {}'.format(plugin_name))
else:
yield command_context.CommandReturn(
error=command_errors.CommandError('插件状态修改失败: 未找到插件 {}'.format(plugin_name))
)
except Exception as e:
traceback.print_exc()
yield command_context.CommandReturn(error=command_errors.CommandError('插件状态修改失败: ' + str(e)))
@operator.operator_class(name='off', help='禁用插件', privilege=2, parent_class=PluginOperator)
class PluginDisableOperator(operator.CommandOperator):
async def execute(
self, context: command_context.ExecuteContext
) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
if len(context.crt_params) == 0:
yield command_context.CommandReturn(error=command_errors.ParamNotEnoughError('请提供插件名称'))
else:
plugin_name = context.crt_params[0]
try:
if await self.ap.plugin_mgr.update_plugin_switch(plugin_name, False):
yield command_context.CommandReturn(text='已禁用插件: {}'.format(plugin_name))
else:
yield command_context.CommandReturn(
error=command_errors.CommandError('插件状态修改失败: 未找到插件 {}'.format(plugin_name))
)
except Exception as e:
traceback.print_exc()
yield command_context.CommandReturn(error=command_errors.CommandError('插件状态修改失败: ' + str(e)))

View File

@@ -1,23 +1,23 @@
from __future__ import annotations
# from __future__ import annotations
import typing
# import typing
from .. import operator
from langbot_plugin.api.entities.builtin.command import context as command_context, errors as command_errors
# from .. import operator
# from langbot_plugin.api.entities.builtin.command import context as command_context, errors as command_errors
@operator.operator_class(name='prompt', help='查看当前对话的前文', usage='!prompt')
class PromptOperator(operator.CommandOperator):
async def execute(
self, context: command_context.ExecuteContext
) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
"""执行"""
if context.session.using_conversation is None:
yield command_context.CommandReturn(error=command_errors.CommandOperationError('当前没有对话'))
else:
reply_str = '当前对话所有内容:\n\n'
# @operator.operator_class(name='prompt', help='查看当前对话的前文', usage='!prompt')
# class PromptOperator(operator.CommandOperator):
# async def execute(
# self, context: command_context.ExecuteContext
# ) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
# """执行"""
# if context.session.using_conversation is None:
# yield command_context.CommandReturn(error=command_errors.CommandOperationError('当前没有对话'))
# else:
# reply_str = '当前对话所有内容:\n\n'
for msg in context.session.using_conversation.messages:
reply_str += f'{msg.role}: {msg.content}\n'
# for msg in context.session.using_conversation.messages:
# reply_str += f'{msg.role}: {msg.content}\n'
yield command_context.CommandReturn(text=reply_str)
# yield command_context.CommandReturn(text=reply_str)

View File

@@ -1,29 +1,29 @@
from __future__ import annotations
# from __future__ import annotations
import typing
# import typing
from .. import operator
from langbot_plugin.api.entities.builtin.command import context as command_context, errors as command_errors
# from .. import operator
# from langbot_plugin.api.entities.builtin.command import context as command_context, errors as command_errors
@operator.operator_class(name='resend', help='重发当前会话的最后一条消息', usage='!resend')
class ResendOperator(operator.CommandOperator):
async def execute(
self, context: command_context.ExecuteContext
) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
# 回滚到最后一条用户message前
if context.session.using_conversation is None:
yield command_context.CommandReturn(error=command_errors.CommandError('当前没有对话'))
else:
conv_msg = context.session.using_conversation.messages
# @operator.operator_class(name='resend', help='重发当前会话的最后一条消息', usage='!resend')
# class ResendOperator(operator.CommandOperator):
# async def execute(
# self, context: command_context.ExecuteContext
# ) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
# # 回滚到最后一条用户message前
# if context.session.using_conversation is None:
# yield command_context.CommandReturn(error=command_errors.CommandError('当前没有对话'))
# else:
# conv_msg = context.session.using_conversation.messages
# 倒序一直删到最后一条用户message
while len(conv_msg) > 0 and conv_msg[-1].role != 'user':
conv_msg.pop()
# # 倒序一直删到最后一条用户message
# while len(conv_msg) > 0 and conv_msg[-1].role != 'user':
# conv_msg.pop()
if len(conv_msg) > 0:
# 删除最后一条用户message
conv_msg.pop()
# if len(conv_msg) > 0:
# # 删除最后一条用户message
# conv_msg.pop()
# 不重发了,提示用户已删除就行了
yield command_context.CommandReturn(text='已删除最后一次请求记录')
# # 不重发了,提示用户已删除就行了
# yield command_context.CommandReturn(text='已删除最后一次请求记录')

View File

@@ -1,17 +0,0 @@
from __future__ import annotations
import typing
from .. import operator
from langbot_plugin.api.entities.builtin.command import context as command_context
@operator.operator_class(name='reset', help='重置当前会话', usage='!reset')
class ResetOperator(operator.CommandOperator):
async def execute(
self, context: command_context.ExecuteContext
) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
"""执行"""
context.session.using_conversation = None
yield command_context.CommandReturn(text='已重置当前会话')

View File

@@ -1,22 +0,0 @@
from __future__ import annotations
import typing
from .. import operator
from langbot_plugin.api.entities.builtin.command import context as command_context
@operator.operator_class(name='version', help='显示版本信息', usage='!version')
class VersionCommand(operator.CommandOperator):
async def execute(
self, context: command_context.ExecuteContext
) -> typing.AsyncGenerator[command_context.CommandReturn, None]:
reply_str = f'当前版本: \n{self.ap.ver_mgr.get_current_version()}'
try:
if await self.ap.ver_mgr.is_new_version_available():
reply_str += '\n\n有新版本可用。'
except Exception:
pass
yield command_context.CommandReturn(text=reply_str.strip())

View File

@@ -216,6 +216,23 @@ class RuntimeConnectionHandler(handler.Handler):
},
)
@self.action(PluginToRuntimeAction.CREATE_NEW_CONVERSATION)
async def create_new_conversation(data: dict[str, Any]) -> handler.ActionResponse:
"""Create new conversation"""
query_id = data['query_id']
if query_id not in self.ap.query_pool.cached_queries:
return handler.ActionResponse.error(
message=f'Query with query_id {query_id} not found',
)
query = self.ap.query_pool.cached_queries[query_id]
query.session.using_conversation = None
return handler.ActionResponse.success(
data={},
)
@self.action(PluginToRuntimeAction.GET_LANGBOT_VERSION)
async def get_langbot_version(data: dict[str, Any]) -> handler.ActionResponse:
"""Get langbot version"""