2 Commits

Author SHA1 Message Date
Hermes Agent f47ffc93f4 fix(archivar): Raum-Name-Fallback + nummerierte Optionen statt Buttons
- Bug 1: 'Zielraum' als Platzhalter → jetzt 3-Stufen-Fallback (matched_room_name → room_info → room_map → ID-Fragment)
- Bug 2: Native Buttons existieren nicht im Adapter → nummerierte Text-Optionen (1/2/3)
2026-06-08 14:32:31 +02:00
Hermes Agent e7ed81c189 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.
2026-06-08 11:33:59 +02:00
+59 -45
View File
@@ -675,13 +675,27 @@ class RocketChatAdapter(BasePlatformAdapter):
and decision.target_room != room_id
and not getattr(decision, 'is_unrouteable', False)):
target_name = getattr(decision, 'matched_room_name', None) or 'Zielraum'
target_name = getattr(decision, 'matched_room_name', None)
if not target_name:
# Fallback: Raum-Name aus room_info oder room_map
target_name = self._room_info.get(decision.target_room, {}).get("name", "")
if not target_name:
# Letzter Fallback: room_map (wenn bekannt)
for rid, entry in self.archivar._room_map.rooms.items():
if rid == decision.target_room:
target_name = entry.name
break
if not target_name:
target_name = decision.target_room[:12] + "…" # ID-Fragment als Notlösung
if is_auftrag:
# Typ 2: Buttons — User entscheidet
# Typ 2: Nummerierte Optionen (keine nativen Buttons im Adapter)
note = (
f"Dein Auftrag passt thematisch besser in **{target_name}**.\n"
f"Soll ich den Auftrag dorthin kopieren und dort abarbeiten?"
f"Antworte mit einer Zahl:\n"
f"1 = Ja, rüberkopieren und dort abarbeiten\n"
f"2 = Hier bleiben\n"
f"3 = Generell hier bleiben"
)
self._pending_move_requests[msg_id] = {
"text": text,
@@ -691,7 +705,7 @@ class RocketChatAdapter(BasePlatformAdapter):
"username": username,
}
self.rest.post_message(room_id, note)
# Kein Forwarding — auf Button-Antwort warten
# Kein Forwarding — auf numerische Antwort warten
forwarded = True
else:
# Typ 1: Silent Copy — Nachricht in Zielraum kopieren
@@ -1070,47 +1084,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 +1182,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