diff --git a/plugin/adapter.py b/plugin/adapter.py index 90a7a45..be9abe6 100644 --- a/plugin/adapter.py +++ b/plugin/adapter.py @@ -1328,27 +1328,34 @@ class RocketChatAdapter(BasePlatformAdapter): # We must resolve it to a real Rocket.Chat room_id for REST API. real_room_id = chat_id if chat_id.startswith("user:"): - # Priority 1: ContextVar from the message that triggered this turn - origin = _CURRENT_ORIGIN_ROOM.get() - if origin: - real_room_id = origin - logger.debug("[rocketchat] send via ContextVar: %s → %s", chat_id, real_room_id) + # Priority 0: Archivar-Kontext — wenn der Archivar einen thematischen + # Zielraum gesetzt hat, geht die Antwort dorthin (Agent-Autonomie). + thematic_target = _THEMATIC_TARGET_ROOM.get() + if thematic_target and thematic_target != _CURRENT_ORIGIN_ROOM.get(): + real_room_id = thematic_target + logger.debug("[rocketchat] send via Archivar target: %s → %s", chat_id, real_room_id) else: - # Priority 2: proaktive Nachricht — use most recent room for this user - rooms = self._user_rooms.get(chat_id, []) - if rooms: - real_room_id = rooms[-1] # last room user was active in - logger.debug("[rocketchat] send via user_map (proactive): %s → %s", chat_id, real_room_id) + # Priority 1: ContextVar from the message that triggered this turn + origin = _CURRENT_ORIGIN_ROOM.get() + if origin: + real_room_id = origin + logger.debug("[rocketchat] send via ContextVar: %s → %s", chat_id, real_room_id) else: - # Fallback: try to find any room with recent activity involving this user - logger.warning("[rocketchat] No mapped room for %s, attempting fallback", chat_id) - fallback_room = self._find_fallback_room_for_user(chat_id) - if fallback_room: - real_room_id = fallback_room - logger.info("[rocketchat] Using fallback room %s for %s", fallback_room, chat_id) + # Priority 2: proaktive Nachricht — use most recent room for this user + rooms = self._user_rooms.get(chat_id, []) + if rooms: + real_room_id = rooms[-1] # last room user was active in + logger.debug("[rocketchat] send via user_map (proactive): %s → %s", chat_id, real_room_id) else: - logger.error("[rocketchat] No real room for %s, cannot send", chat_id) - return SendResult(success=False, error=f"No real room for virtual chat_id {chat_id}") + # Fallback: try to find any room with recent activity involving this user + logger.warning("[rocketchat] No mapped room for %s, attempting fallback", chat_id) + fallback_room = self._find_fallback_room_for_user(chat_id) + if fallback_room: + real_room_id = fallback_room + logger.info("[rocketchat] Using fallback room %s for %s", fallback_room, chat_id) + else: + logger.error("[rocketchat] No real room for %s, cannot send", chat_id) + return SendResult(success=False, error=f"No real room for virtual chat_id {chat_id}") # ── Minimal-Fix: Parse [[BUTTONS:...]] markup from agent response text ── # This allows the agent to request buttons by embedding markup in its @@ -1528,8 +1535,15 @@ class RocketChatAdapter(BasePlatformAdapter): # We must resolve it to a real Rocket.Chat room_id for REST API. real_room_id = chat_id if chat_id.startswith("user:"): - # Priority 1: ContextVar from the message that triggered this turn - origin = _CURRENT_ORIGIN_ROOM.get() + # Priority 0: Archivar-Kontext — wenn der Archivar einen thematischen + # Zielraum gesetzt hat, geht die Antwort dorthin (Agent-Autonomie). + thematic_target = _THEMATIC_TARGET_ROOM.get() + if thematic_target and thematic_target != _CURRENT_ORIGIN_ROOM.get(): + real_room_id = thematic_target + logger.debug("[rocketchat] send via Archivar target: %s → %s", chat_id, real_room_id) + else: + # Priority 1: ContextVar from the message that triggered this turn + origin = _CURRENT_ORIGIN_ROOM.get() if origin: real_room_id = origin logger.debug("[rocketchat] send via ContextVar: %s → %s", chat_id, real_room_id)