feat(deploy): finalize MVP deployment and file transfer approach
This commit is contained in:
parent
6369721876
commit
0f79494fbe
43 changed files with 3078 additions and 645 deletions
|
|
@ -22,6 +22,31 @@ def _default_room_name(chat_id: str) -> str:
|
|||
return f"Чат {suffix}"
|
||||
|
||||
|
||||
def default_agent_notice() -> str:
|
||||
return (
|
||||
"Внимание: ваш Matrix ID не найден в конфиге агентов. "
|
||||
"Пока используется агент по умолчанию. После добавления вас в конфиг "
|
||||
"бот переключит существующие комнаты на назначенного агента."
|
||||
)
|
||||
|
||||
|
||||
async def _invite_if_possible(client: Any, room_id: str, matrix_user_id: str) -> bool:
|
||||
room_invite = getattr(client, "room_invite", None)
|
||||
if not callable(room_invite):
|
||||
return False
|
||||
try:
|
||||
await room_invite(room_id, matrix_user_id)
|
||||
return True
|
||||
except Exception as exc:
|
||||
logger.warning(
|
||||
"matrix_workspace_reinvite_failed",
|
||||
room_id=room_id,
|
||||
user=matrix_user_id,
|
||||
error=str(exc),
|
||||
)
|
||||
return False
|
||||
|
||||
|
||||
async def provision_workspace_chat(
|
||||
client: Any,
|
||||
matrix_user_id: str,
|
||||
|
|
@ -68,10 +93,11 @@ async def provision_workspace_chat(
|
|||
room_name = room_name_override or _default_room_name(chat_id)
|
||||
|
||||
agent_id = None
|
||||
agent_assignment = "none"
|
||||
if registry is not None:
|
||||
agent_id = registry.get_agent_id_for_user(matrix_user_id)
|
||||
if agent_id is None and registry.agents:
|
||||
agent_id = registry.agents[0].agent_id
|
||||
assignment = registry.resolve_agent_for_user(matrix_user_id)
|
||||
agent_id = assignment.agent_id
|
||||
agent_assignment = assignment.source
|
||||
|
||||
chat_resp = await client.room_create(
|
||||
name=room_name,
|
||||
|
|
@ -110,6 +136,7 @@ async def provision_workspace_chat(
|
|||
"space_id": space_id,
|
||||
"platform_chat_id": platform_chat_id,
|
||||
"agent_id": agent_id,
|
||||
"agent_assignment": agent_assignment,
|
||||
},
|
||||
)
|
||||
await chat_mgr.get_or_create(
|
||||
|
|
@ -126,6 +153,64 @@ async def provision_workspace_chat(
|
|||
"chat_room_id": chat_room_id,
|
||||
"chat_id": chat_id,
|
||||
"room_name": room_name,
|
||||
"agent_assignment": agent_assignment,
|
||||
"agent_id": agent_id,
|
||||
}
|
||||
|
||||
|
||||
async def restore_workspace_access(
|
||||
client: Any,
|
||||
matrix_user_id: str,
|
||||
display_name: str,
|
||||
platform,
|
||||
store,
|
||||
auth_mgr,
|
||||
chat_mgr,
|
||||
registry: AgentRegistry | None = None,
|
||||
) -> dict:
|
||||
user_meta = await get_user_meta(store, matrix_user_id) or {}
|
||||
space_id = user_meta.get("space_id")
|
||||
if not space_id:
|
||||
created = await provision_workspace_chat(
|
||||
client,
|
||||
matrix_user_id,
|
||||
display_name,
|
||||
platform,
|
||||
store,
|
||||
auth_mgr,
|
||||
chat_mgr,
|
||||
room_name_override="Чат 1",
|
||||
registry=registry,
|
||||
)
|
||||
return {**created, "reinvited_rooms": [], "created_new_chat": True}
|
||||
|
||||
await auth_mgr.confirm(matrix_user_id)
|
||||
await _invite_if_possible(client, space_id, matrix_user_id)
|
||||
|
||||
chats = await chat_mgr.list_active(matrix_user_id)
|
||||
if not chats:
|
||||
created = await provision_workspace_chat(
|
||||
client,
|
||||
matrix_user_id,
|
||||
display_name,
|
||||
platform,
|
||||
store,
|
||||
auth_mgr,
|
||||
chat_mgr,
|
||||
registry=registry,
|
||||
)
|
||||
return {**created, "reinvited_rooms": [], "created_new_chat": True}
|
||||
|
||||
reinvited_rooms = []
|
||||
for chat in chats:
|
||||
if chat.surface_ref:
|
||||
if await _invite_if_possible(client, chat.surface_ref, matrix_user_id):
|
||||
reinvited_rooms.append(chat.surface_ref)
|
||||
|
||||
return {
|
||||
"space_id": space_id,
|
||||
"reinvited_rooms": reinvited_rooms,
|
||||
"created_new_chat": False,
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -146,6 +231,29 @@ async def handle_invite(
|
|||
|
||||
existing = await get_user_meta(store, matrix_user_id)
|
||||
if existing and existing.get("space_id"):
|
||||
restored = await restore_workspace_access(
|
||||
client,
|
||||
matrix_user_id,
|
||||
display_name,
|
||||
platform,
|
||||
store,
|
||||
auth_mgr,
|
||||
chat_mgr,
|
||||
registry=registry,
|
||||
)
|
||||
body = "Я отправил повторные приглашения в пространство Lambda и рабочие чаты."
|
||||
if restored.get("created_new_chat"):
|
||||
body = (
|
||||
f"Создал новый рабочий чат {restored['room_name']} "
|
||||
f"({restored['chat_id']}) и отправил приглашение."
|
||||
)
|
||||
if restored.get("agent_assignment") == "default":
|
||||
body = f"{body}\n\n{default_agent_notice()}"
|
||||
await client.room_send(
|
||||
room.room_id,
|
||||
"m.room.message",
|
||||
{"msgtype": "m.text", "body": body},
|
||||
)
|
||||
return
|
||||
|
||||
try:
|
||||
|
|
@ -168,6 +276,8 @@ async def handle_invite(
|
|||
f"Привет, {created['user'].display_name or matrix_user_id}! Пиши — я здесь.\n\n"
|
||||
"Команды: !new · !chats · !rename · !archive · !clear · !help"
|
||||
)
|
||||
if created.get("agent_assignment") == "default":
|
||||
welcome = f"{welcome}\n\n{default_agent_notice()}"
|
||||
await client.room_send(
|
||||
created["chat_room_id"],
|
||||
"m.room.message",
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ from nio.api import RoomVisibility
|
|||
from nio.responses import RoomCreateError
|
||||
|
||||
from adapter.matrix.agent_registry import AgentRegistry
|
||||
from adapter.matrix.handlers.auth import default_agent_notice
|
||||
from adapter.matrix.store import (
|
||||
get_user_meta,
|
||||
next_chat_id,
|
||||
|
|
@ -107,10 +108,11 @@ def make_handle_new_chat(
|
|||
)
|
||||
|
||||
agent_id = None
|
||||
agent_assignment = "none"
|
||||
if registry is not None:
|
||||
agent_id = registry.get_agent_id_for_user(event.user_id)
|
||||
if agent_id is None and registry.agents:
|
||||
agent_id = registry.agents[0].agent_id
|
||||
assignment = registry.resolve_agent_for_user(event.user_id)
|
||||
agent_id = assignment.agent_id
|
||||
agent_assignment = assignment.source
|
||||
|
||||
room_meta: dict = {
|
||||
"room_type": "chat",
|
||||
|
|
@ -120,6 +122,7 @@ def make_handle_new_chat(
|
|||
"space_id": space_id,
|
||||
"platform_chat_id": platform_chat_id,
|
||||
"agent_id": agent_id,
|
||||
"agent_assignment": agent_assignment,
|
||||
}
|
||||
await set_room_meta(store, room_id, room_meta)
|
||||
ctx = await chat_mgr.get_or_create(
|
||||
|
|
@ -129,10 +132,13 @@ def make_handle_new_chat(
|
|||
surface_ref=room_id,
|
||||
name=room_name,
|
||||
)
|
||||
text = f"Создан чат: {ctx.display_name} ({ctx.chat_id})"
|
||||
if agent_assignment == "default":
|
||||
text = f"{text}\n\n{default_agent_notice()}"
|
||||
return [
|
||||
OutgoingMessage(
|
||||
chat_id=event.chat_id,
|
||||
text=f"Создан чат: {ctx.display_name} ({ctx.chat_id})",
|
||||
text=text,
|
||||
)
|
||||
]
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue