fix: send() method structure — restore button-parsing + REST-post code that fell out of method body

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.
This commit is contained in:
Hermes Agent
2026-06-08 11:33:59 +02:00
parent 83a5d262c7
commit e7ed81c189
+41 -41
View File
@@ -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