from __future__ import annotations from adapter.max.handlers.chat import ChatHandler as MaxChatHandler from adapter.max.handlers.help import get_help from adapter.max.store import ChatStore from sdk.prototype_state import PrototypeStateStore from core.handler import EventDispatcher from core.protocol import IncomingCallback, IncomingCommand, OutgoingMessage _SINGLE_DIALOG_HINT = ( "В MAX один диалог с ботом. Чтобы сбросить контекст агента, используйте /clear или /reset." ) def register_max_handlers( dispatcher: EventDispatcher, *, chat_store: ChatStore, max_chat_handler: MaxChatHandler, prototype_state: PrototypeStateStore, ) -> None: async def _room_or_error( event: IncomingCommand, ) -> tuple[str, str] | list[OutgoingMessage]: room = chat_store.get_room_by_platform_chat_id(event.chat_id) if room is None: return [ OutgoingMessage( chat_id=event.chat_id, text="Состояние ещё не готово. Напишите сообщение ещё раз.", ) ] return room.max_chat_id, room.platform_chat_id async def handle_max_help(event: IncomingCommand, auth_mgr, platform, chat_mgr, settings_mgr): # noqa: ARG001 if not await auth_mgr.is_authenticated(event.user_id): return [ OutgoingMessage( chat_id=event.chat_id, text="Введите /start чтобы начать.", ) ] return [OutgoingMessage(chat_id=event.chat_id, text=get_help())] async def handle_max_no_multichat(event: IncomingCommand, auth_mgr, platform, chat_mgr, settings_mgr): # noqa: ARG001 if not await auth_mgr.is_authenticated(event.user_id): return [ OutgoingMessage( chat_id=event.chat_id, text="Введите /start чтобы начать.", ) ] _ = event return [OutgoingMessage(chat_id=event.chat_id, text=_SINGLE_DIALOG_HINT)] async def handle_max_clear(event: IncomingCommand, auth_mgr, platform, chat_mgr, settings_mgr): # noqa: ARG001 if not await auth_mgr.is_authenticated(event.user_id): return [ OutgoingMessage( chat_id=event.chat_id, text="Введите /start чтобы начать.", ) ] routed = await _room_or_error(event) if isinstance(routed, list): return routed max_chat_id, platform_chat_old = routed await prototype_state.clear_current_session(platform_chat_old) await chat_mgr.archive(platform_chat_old, event.user_id) max_chat_handler.handle_clear(max_chat_id) room = chat_store.get_room_by_max_chat_id(max_chat_id) if room is None: return [ OutgoingMessage( chat_id=platform_chat_old, text="Не удалось сбросить контекст.", ) ] platform_chat_new = room.platform_chat_id await chat_mgr.get_or_create( user_id=event.user_id, chat_id=platform_chat_new, platform="max", surface_ref=max_chat_id, name=room.name, ) return [ OutgoingMessage( chat_id=platform_chat_new, text="Контекст агента сброшен.", ) ] for cmd in ("new", "rename", "archive", "chats"): dispatcher.register(IncomingCommand, cmd, handle_max_no_multichat) dispatcher.register(IncomingCommand, "reset", handle_max_clear) dispatcher.register(IncomingCommand, "clear", handle_max_clear) dispatcher.register(IncomingCommand, "help", handle_max_help) async def handle_max_plain_callback(event: IncomingCallback, auth_mgr, platform, chat_mgr, settings_mgr): # noqa: ARG001 payload = str(event.payload.get("payload", "")) return [ OutgoingMessage( chat_id=event.chat_id, text=f"Неизвестное действие кнопки: {payload}", ) ] dispatcher.register(IncomingCallback, "max_callback", handle_max_plain_callback)