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