From 521a9417927c709ba231a277941c85373a80b2d4 Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Mon, 15 Sep 2025 18:10:18 +0800 Subject: [PATCH] refactor: move commands to seperated plugin --- pkg/command/cmdmgr.py | 6 +- pkg/command/operators/cmd.py | 44 -------- pkg/command/operators/delc.py | 72 ++++++------- pkg/command/operators/func.py | 27 ----- pkg/command/operators/help.py | 18 ---- pkg/command/operators/last.py | 54 +++++----- pkg/command/operators/list.py | 74 ++++++------- pkg/command/operators/next.py | 54 +++++----- pkg/command/operators/plugin.py | 171 ------------------------------- pkg/command/operators/prompt.py | 34 +++--- pkg/command/operators/resend.py | 44 ++++---- pkg/command/operators/reset.py | 17 --- pkg/command/operators/version.py | 22 ---- pkg/plugin/handler.py | 17 +++ 14 files changed, 186 insertions(+), 468 deletions(-) delete mode 100644 pkg/command/operators/cmd.py delete mode 100644 pkg/command/operators/func.py delete mode 100644 pkg/command/operators/help.py delete mode 100644 pkg/command/operators/plugin.py delete mode 100644 pkg/command/operators/reset.py delete mode 100644 pkg/command/operators/version.py diff --git a/pkg/command/cmdmgr.py b/pkg/command/cmdmgr.py index 19684ac6..51dda81e 100644 --- a/pkg/command/cmdmgr.py +++ b/pkg/command/cmdmgr.py @@ -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] diff --git a/pkg/command/operators/cmd.py b/pkg/command/operators/cmd.py deleted file mode 100644 index cb0c3554..00000000 --- a/pkg/command/operators/cmd.py +++ /dev/null @@ -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()) diff --git a/pkg/command/operators/delc.py b/pkg/command/operators/delc.py index 06db3d1e..86df4811 100644 --- a/pkg/command/operators/delc.py +++ b/pkg/command/operators/delc.py @@ -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='已删除所有对话') diff --git a/pkg/command/operators/func.py b/pkg/command/operators/func.py deleted file mode 100644 index e7828a51..00000000 --- a/pkg/command/operators/func.py +++ /dev/null @@ -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) diff --git a/pkg/command/operators/help.py b/pkg/command/operators/help.py deleted file mode 100644 index 609f05ad..00000000 --- a/pkg/command/operators/help.py +++ /dev/null @@ -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) diff --git a/pkg/command/operators/last.py b/pkg/command/operators/last.py index 3f92e2e2..376beca0 100644 --- a/pkg/command/operators/last.py +++ b/pkg/command/operators/last.py @@ -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('当前没有对话')) diff --git a/pkg/command/operators/list.py b/pkg/command/operators/list.py index ca1bf8e9..1c8c052c 100644 --- a/pkg/command/operators/list.py +++ b/pkg/command/operators/list.py @@ -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}') diff --git a/pkg/command/operators/next.py b/pkg/command/operators/next.py index 87cc565c..03bce4a0 100644 --- a/pkg/command/operators/next.py +++ b/pkg/command/operators/next.py @@ -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('当前没有对话')) diff --git a/pkg/command/operators/plugin.py b/pkg/command/operators/plugin.py deleted file mode 100644 index 1c135bd4..00000000 --- a/pkg/command/operators/plugin.py +++ /dev/null @@ -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))) diff --git a/pkg/command/operators/prompt.py b/pkg/command/operators/prompt.py index b43be2cf..13eec9bb 100644 --- a/pkg/command/operators/prompt.py +++ b/pkg/command/operators/prompt.py @@ -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) diff --git a/pkg/command/operators/resend.py b/pkg/command/operators/resend.py index 14bfee99..56d54169 100644 --- a/pkg/command/operators/resend.py +++ b/pkg/command/operators/resend.py @@ -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='已删除最后一次请求记录') diff --git a/pkg/command/operators/reset.py b/pkg/command/operators/reset.py deleted file mode 100644 index 0c85fb32..00000000 --- a/pkg/command/operators/reset.py +++ /dev/null @@ -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='已重置当前会话') diff --git a/pkg/command/operators/version.py b/pkg/command/operators/version.py deleted file mode 100644 index 5b3c3358..00000000 --- a/pkg/command/operators/version.py +++ /dev/null @@ -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()) diff --git a/pkg/plugin/handler.py b/pkg/plugin/handler.py index 36d11d09..ce3bd6d4 100644 --- a/pkg/plugin/handler.py +++ b/pkg/plugin/handler.py @@ -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"""