feat(matrix): implement !reset via new platform_chat_id

Instead of calling a /reset endpoint on platform-agent, !reset now
generates a new thread_id (platform_chat_id) for the room. The old
WebSocket connection is closed and the next message creates a fresh
context automatically. No platform changes required.
This commit is contained in:
Mikhail Putilovskij 2026-04-19 21:20:31 +03:00
parent 4a5260ca79
commit 73c472ecc4
4 changed files with 45 additions and 46 deletions

View file

@ -10,6 +10,7 @@ from adapter.matrix.handlers.confirm import make_handle_cancel, make_handle_conf
from adapter.matrix.handlers.context_commands import (
make_handle_context,
make_handle_load,
make_handle_reset,
make_handle_save,
)
from adapter.matrix.handlers.settings import (
@ -43,7 +44,7 @@ def register_matrix_handlers(
dispatcher.register(IncomingCommand, "archive", make_handle_archive(client, store))
dispatcher.register(IncomingCommand, "help", handle_help)
dispatcher.register(IncomingCommand, "settings", handle_settings)
dispatcher.register(IncomingCommand, "reset", handle_settings)
dispatcher.register(IncomingCommand, "reset", make_handle_reset(store, prototype_state) if prototype_state is not None else handle_settings)
dispatcher.register(IncomingCommand, "settings_skills", handle_settings_skills)
dispatcher.register(IncomingCommand, "settings_connectors", handle_settings_connectors)
dispatcher.register(IncomingCommand, "settings_soul", handle_settings_soul)

View file

@ -7,7 +7,7 @@ from typing import TYPE_CHECKING
import httpx
import structlog
from adapter.matrix.store import get_room_meta, set_load_pending, set_reset_pending
from adapter.matrix.store import get_room_meta, set_load_pending, set_platform_chat_id
from core.protocol import IncomingCommand, OutgoingEvent, OutgoingMessage
if TYPE_CHECKING:
@ -123,23 +123,26 @@ def make_handle_load(store: "StateStore", prototype_state: "PrototypeStateStore"
return handle_load
def make_handle_reset(store: "StateStore", agent_base_url: str):
def make_handle_reset(store: "StateStore", prototype_state: "PrototypeStateStore"):
async def handle_reset(
event: IncomingCommand, auth_mgr, platform, chat_mgr, settings_mgr
) -> list[OutgoingEvent]:
import time
room_id = await _resolve_room_id(event, chat_mgr)
await set_reset_pending(store, event.user_id, room_id, {"active": True})
return [
OutgoingMessage(
chat_id=event.chat_id,
text=(
"Сбросить контекст агента? Выбери:\n"
" !yes - сбросить\n"
" !save [имя] - сохранить и сбросить\n"
" !no - отмена"
),
)
]
room_meta = await get_room_meta(store, room_id)
old_chat_id = (room_meta or {}).get("platform_chat_id") or room_id
new_chat_id = f"matrix:{room_id}#{int(time.time())}"
await set_platform_chat_id(store, room_id, new_chat_id)
disconnect = getattr(platform, "disconnect_chat", None)
if callable(disconnect):
await disconnect(old_chat_id)
await prototype_state.clear_current_session(new_chat_id)
return [OutgoingMessage(chat_id=event.chat_id, text="Контекст сброшен. Агент не помнит предыдущий разговор.")]
return handle_reset