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