Users can now list available agents with !agent and select one by number. Selection persists in user metadata (selected_agent_id). If the current room has no agent binding yet, selecting an agent binds it immediately so the user can start messaging without !new. Also updates the dispatcher test to reflect that real-mode platform is now RoutedPlatformClient, not a bare RealPlatformClient.
78 lines
3 KiB
Python
78 lines
3 KiB
Python
from __future__ import annotations
|
|
|
|
from collections.abc import Awaitable, Callable
|
|
|
|
from adapter.matrix.agent_registry import AgentRegistry
|
|
from adapter.matrix.store import (
|
|
get_platform_chat_id,
|
|
get_selected_agent_id,
|
|
get_room_meta,
|
|
next_platform_chat_id,
|
|
set_platform_chat_id,
|
|
set_room_agent_id,
|
|
set_selected_agent_id,
|
|
)
|
|
from core.protocol import IncomingCommand, OutgoingMessage
|
|
|
|
|
|
def make_handle_agent(store, registry: AgentRegistry) -> Callable[..., Awaitable[list]]:
|
|
async def handle_agent(
|
|
event: IncomingCommand, auth_mgr, platform, chat_mgr, settings_mgr
|
|
) -> list:
|
|
if not event.args:
|
|
selected_agent_id = await get_selected_agent_id(store, event.user_id)
|
|
lines = ["Доступные агенты:"]
|
|
for index, agent in enumerate(registry.agents, start=1):
|
|
suffix = " [текущий]" if agent.agent_id == selected_agent_id else ""
|
|
lines.append(f"{index}. {agent.label}{suffix}")
|
|
lines.extend(["", "Выбери агент: !agent <номер>"])
|
|
return [OutgoingMessage(chat_id=event.chat_id, text="\n".join(lines))]
|
|
|
|
try:
|
|
selected_index = int(event.args[0])
|
|
except ValueError:
|
|
return [
|
|
OutgoingMessage(
|
|
chat_id=event.chat_id,
|
|
text="Укажи номер агента из списка: !agent <номер>.",
|
|
)
|
|
]
|
|
|
|
if selected_index < 1 or selected_index > len(registry.agents):
|
|
return [
|
|
OutgoingMessage(
|
|
chat_id=event.chat_id,
|
|
text="Такого агента нет. Открой список через !agent.",
|
|
)
|
|
]
|
|
|
|
agent = registry.agents[selected_index - 1]
|
|
await set_selected_agent_id(store, event.user_id, agent.agent_id)
|
|
|
|
current_chat = await chat_mgr.get(event.chat_id, user_id=event.user_id)
|
|
if current_chat is not None and current_chat.surface_ref:
|
|
room_id = current_chat.surface_ref
|
|
room_meta = await get_room_meta(store, room_id)
|
|
if room_meta is not None and not room_meta.get("agent_id"):
|
|
await set_room_agent_id(store, room_id, agent.agent_id)
|
|
if await get_platform_chat_id(store, room_id) is None:
|
|
await set_platform_chat_id(
|
|
store,
|
|
room_id,
|
|
await next_platform_chat_id(store),
|
|
)
|
|
return [
|
|
OutgoingMessage(
|
|
chat_id=event.chat_id,
|
|
text=f"Агент {agent.label} выбран. Текущий чат готов к работе.",
|
|
)
|
|
]
|
|
|
|
return [
|
|
OutgoingMessage(
|
|
chat_id=event.chat_id,
|
|
text=f"Агент переключен на {agent.label}. Продолжай через !new.",
|
|
)
|
|
]
|
|
|
|
return handle_agent
|