From e7ed81c189dc0de0f8eacbb25d018454532f1fa2 Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Mon, 8 Jun 2026 11:33:59 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20send()=20method=20structure=20=E2=80=94?= =?UTF-8?q?=20restore=20button-parsing=20+=20REST-post=20code=20that=20fel?= =?UTF-8?q?l=20out=20of=20method=20body?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The send() method in adapter.py had a structural defect where the button-parsing, template-handling, and rest.post_message code (98 lines) was at module level instead of inside the send() method body. send() ended after room-resolution without a return statement, causing it to return None. _send_with_retry() in base.py then crashed with AttributeError: 'NoneType' object has no attribute 'success'. This made every Rocket.Chat response silently fail — the agent processed messages but could never deliver replies. Fix: moved the orphaned code block back into the send() method body where it belongs, after the room-resolution logic and before _find_fallback_room_for_user. --- plugin/adapter.py | 82 +++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/plugin/adapter.py b/plugin/adapter.py index b344b17..2fd5087 100644 --- a/plugin/adapter.py +++ b/plugin/adapter.py @@ -1070,47 +1070,6 @@ class RocketChatAdapter(BasePlatformAdapter): 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}") - def _find_fallback_room_for_user(self, chat_id: str) -> Optional[str]: - """Fallback method to find a room for a user when direct mapping fails. - - Args: - chat_id: Virtual chat_id like "user:username" - - Returns: - Room ID if found, None otherwise - """ - if not chat_id.startswith("user:"): - return None - - target_username = chat_id.split(":", 1)[1].lower() - - # Try to find any DM room involving this user - for room_id, room_info in self._room_info.items(): - if room_info.get("type") == "d": - # Try to get room details with members - try: - room_detail = self.rest._request("GET", f"/api/v1/rooms.info?roomId={room_id}", headers={ - "X-Auth-Token": self.rest.auth_token, - "X-User-Id": self.rest.user_id, - }) - if room_detail and room_detail.get("success"): - room_data = room_detail.get("room", {}) - usernames = room_data.get("usernames", []) - if target_username in [u.lower() for u in usernames]: - return room_id - except Exception as e: - logger.debug("[rocketchat] Could not get room info for %s: %s", room_id, e) - - # Last resort: return first available room (better than nothing) - if self._subscribed_rooms: - first_room = next(iter(self._subscribed_rooms)) - logger.warning("[rocketchat] Using first available room %s as ultimate fallback for %s", - first_room, chat_id) - return first_room - - return None - - # ── Minimal-Fix: Parse [[BUTTONS:...]] markup from agent response text ── # This allows the agent to request buttons by embedding markup in its # response text, without requiring the gateway to pass metadata. @@ -1209,6 +1168,47 @@ class RocketChatAdapter(BasePlatformAdapter): return SendResult(success=True, message_id=str(int(time.time() * 1000))) return SendResult(success=False, error="chat.postMessage failed") + def _find_fallback_room_for_user(self, chat_id: str) -> Optional[str]: + """Fallback method to find a room for a user when direct mapping fails. + + Args: + chat_id: Virtual chat_id like "user:username" + + Returns: + Room ID if found, None otherwise + """ + if not chat_id.startswith("user:"): + return None + + target_username = chat_id.split(":", 1)[1].lower() + + # Try to find any DM room involving this user + for room_id, room_info in self._room_info.items(): + if room_info.get("type") == "d": + # Try to get room details with members + try: + room_detail = self.rest._request("GET", f"/api/v1/rooms.info?roomId={room_id}", headers={ + "X-Auth-Token": self.rest.auth_token, + "X-User-Id": self.rest.user_id, + }) + if room_detail and room_detail.get("success"): + room_data = room_detail.get("room", {}) + usernames = room_data.get("usernames", []) + if target_username in [u.lower() for u in usernames]: + return room_id + except Exception as e: + logger.debug("[rocketchat] Could not get room info for %s: %s", room_id, e) + + # Last resort: return first available room (better than nothing) + if self._subscribed_rooms: + first_room = next(iter(self._subscribed_rooms)) + logger.warning("[rocketchat] Using first available room %s as ultimate fallback for %s", + first_room, chat_id) + return first_room + + return None + + async def send_typing(self, chat_id: str, metadata=None) -> None: pass