feat: add matrix routed platform facade

This commit is contained in:
Mikhail Putilovskij 2026-04-24 13:22:05 +03:00
parent 7627012f24
commit 242f4aadd3
3 changed files with 376 additions and 8 deletions

View file

@ -30,11 +30,13 @@ from adapter.matrix.files import (
matrix_msgtype_for_attachment,
resolve_workspace_attachment_path,
)
from adapter.matrix.agent_registry import load_agent_registry
from adapter.matrix.handlers import register_matrix_handlers
from adapter.matrix.handlers.auth import handle_invite, provision_workspace_chat
from adapter.matrix.handlers.context_commands import (
LOAD_PROMPT,
)
from adapter.matrix.routed_platform import RoutedPlatformClient
from adapter.matrix.room_router import resolve_chat_id
from adapter.matrix.store import (
add_staged_attachment,
@ -118,13 +120,31 @@ def _agent_base_url_from_env() -> str:
return "http://127.0.0.1:8000"
def _build_platform_from_env() -> PlatformClient:
def _build_platform_from_env(*, store: StateStore, chat_mgr: ChatManager) -> PlatformClient:
backend = os.environ.get("MATRIX_PLATFORM_BACKEND", "mock").strip().lower()
if backend == "real":
prototype_state = PrototypeStateStore()
registry_path = os.environ.get("MATRIX_AGENT_REGISTRY_PATH", "").strip()
if registry_path:
registry = load_agent_registry(registry_path)
delegates = {
agent.agent_id: RealPlatformClient(
agent_id=agent.agent_id,
agent_base_url=_agent_base_url_from_env(),
prototype_state=prototype_state,
platform="matrix",
)
for agent in registry.agents
}
return RoutedPlatformClient(
chat_mgr=chat_mgr,
store=store,
delegates=delegates,
)
return RealPlatformClient(
agent_id="matrix-bot",
agent_base_url=_agent_base_url_from_env(),
prototype_state=PrototypeStateStore(),
prototype_state=prototype_state,
platform="matrix",
)
return MockPlatformClient()
@ -135,9 +155,10 @@ def build_runtime(
store: StateStore | None = None,
client: AsyncClient | None = None,
) -> MatrixRuntime:
platform = platform or _build_platform_from_env()
store = store or InMemoryStore()
chat_mgr = ChatManager(platform, store)
platform = platform or _build_platform_from_env(store=store, chat_mgr=chat_mgr)
chat_mgr = ChatManager(platform, store)
auth_mgr = AuthManager(platform, store)
settings_mgr = SettingsManager(platform, store)
prototype_state = getattr(platform, "_prototype_state", None)
@ -224,10 +245,7 @@ class MatrixBot:
)
return
local_chat_id = await resolve_chat_id(self.runtime.store, room.room_id, sender)
dispatch_chat_id = local_chat_id
if not body.startswith("!"):
dispatch_chat_id = (room_meta or {}).get("platform_chat_id") or local_chat_id
incoming = from_room_event(event, room_id=room.room_id, chat_id=dispatch_chat_id)
incoming = from_room_event(event, room_id=room.room_id, chat_id=local_chat_id)
if incoming is None:
return
if isinstance(incoming, IncomingCommand) and incoming.command in {
@ -274,7 +292,7 @@ class MatrixBot:
)
outgoing = [
OutgoingMessage(
chat_id=dispatch_chat_id,
chat_id=local_chat_id,
text="Сервис временно недоступен. Попробуйте ещё раз позже.",
)
]