feat(v6): Phase 1 — Origin-First Guard + LLM Fix + Proaktiver Broadcast
P3: LLM_CONFIG base_url jetzt aus OLLAMA_BASE_URL Env-Var (config.py:150)
I1: LLM_ROOM_PROMPT umgeschrieben — STAY/ROUTE statt CATCH_ALL,
Ursprungsraum-Kontext, Few-Shot-Beispiele, Catch-All nicht als Option
(config.py:175-195, router.py:_llm_room_lookup)
P2: Origin-First Guard (Schritt -0.5) in router.py:route():
- origin_room_id leer (echte DM) → normaler Flow
- origin_room ist Catch-All → stay_in_room
- origin_room ist excluded → ignore
- LLM sagt STAY → stay_in_room
- LLM == origin_room → stay_in_room
- LLM == catch_all_room → stay_in_room (kein DM-Routing!)
- LLM == anderer Raum → Move-Request (Butler-Prinzip)
Catch-All-Schutz: DM-Nachrichten skippen Routing-Flow (router.py:270-282)
Move-Request-Routing-Fix (adapter.py):
- 1️⃣ Rüberkopieren → _CURRENT_ORIGIN_ROOM = Zielraum
- 2️⃣ Generell hier bleiben → Sticky-Eintrag
- 3️⃣ Hier bleiben → nur diesmal, kein Sticky
- Button-Labels aktualisiert: 1️⃣ Rüber / 2️⃣ Generell / 3️⃣ Hier
B6: Proaktiver Raum-Broadcast (tools.py):
- Neues Tool rocketchat_broadcast mit 5-Min-Cooldown
- Format: menschenähnlich, kein Bot-Ton
- Registriert in _TOOLS mit 📢 Emoji
This commit is contained in:
+109
-67
@@ -218,22 +218,26 @@ class BidirectionalRouter:
|
||||
# ── Public API ──────────────────────────────────────────────────────
|
||||
|
||||
async def route(self, text: str, origin_room_id: str = "") -> RoutingDecision:
|
||||
"""Klassifiziere und route eine Nachricht (korrigierter Flow).
|
||||
"""Klassifiziere und route eine Nachricht (v6 Flow mit Origin-First Guard).
|
||||
|
||||
Der neue Routing-Flow (Architekturskizze):
|
||||
1. origin_room_id in excluded_rooms? → IGNORE (silent)
|
||||
2. Raum-Zustand prüfen (persistenter Zustand)
|
||||
3. Meta-Fragen-Erkennung
|
||||
4. Keyword-Match in RoomEntry.keywords → Zielraum (schnell, zuverlässig)
|
||||
5. FAISS Phase-1: Raum-Index + Keyword-Booster
|
||||
- FAISS-Score >= FAISS_AUTO_ROUTE → direkt routen (kein LLM)
|
||||
- Score unter Schwelle → Fall-through zu Embedding + LLM
|
||||
6. Embedding-Vorfilter: binär "Smalltalk" vs. "potenzieller Auftrag"
|
||||
- Klarer Smalltalk → stay_in_room
|
||||
- Unsicher → weiter zu LLM-Raum-Router
|
||||
7. LLM-Raum-Router: LLM_ROOM_PROMPT + Raumliste mit Keywords → room_id direkt
|
||||
8. Raum gefunden → Move-Request-Button (Butler-Prinzip: ✅Ja/📍Hier)
|
||||
9. Kein Treffer → catch_all_room
|
||||
Der neue Routing-Flow (v6):
|
||||
-0.5 ORIGIN-FIRST GUARD (VOR allen anderen Schritten):
|
||||
1. origin_room_id leer (echte DM) → normaler Flow
|
||||
2. origin_room ist Catch-All-Raum → stay_in_room
|
||||
3. origin_room ist excluded → ignore
|
||||
0. Excluded rooms check
|
||||
1. Raum-Zustand prüfen (persistenter Zustand)
|
||||
2. Meta-Fragen-Erkennung
|
||||
3. Keyword-Match in RoomEntry.keywords → Zielraum (schnell, zuverlässig)
|
||||
3.5 FAISS Phase-1: Raum-Index + Keyword-Booster
|
||||
4. Embedding-Vorfilter: binär "Smalltalk" vs. "potenzieller Auftrag"
|
||||
5. LLM-Raum-Router: LLM_ROOM_PROMPT + Raumliste → STAY oder room_id
|
||||
6. ORIGIN-FIRST GUARD (nach LLM):
|
||||
- LLM sagt STAY → stay_in_room
|
||||
- LLM == origin_room → stay_in_room
|
||||
- LLM == catch_all_room → stay_in_room (kein DM-Routing aus Projekträumen!)
|
||||
- LLM == anderer Raum → Move-Request (Butler-Prinzip)
|
||||
7. LLM-Fehler → stay_in_room
|
||||
|
||||
Args:
|
||||
text: Der Nachrichtentext.
|
||||
@@ -443,31 +447,14 @@ class BidirectionalRouter:
|
||||
|
||||
# ── Schritt 5: LLM-Raum-Router (zentraler Prompt) ────────────
|
||||
# LLM_ROOM_PROMPT erhält Nachricht + Raumliste mit Keywords,
|
||||
# liefert room_id direkt (keine Kategorien, kein Name-Matching)
|
||||
llm_room, llm_failed = await self._llm_room_lookup(text)
|
||||
if llm_room is not None:
|
||||
room_name = llm_room.name.replace("#", "") if llm_room.name.startswith("#") else llm_room.name
|
||||
logger.info(
|
||||
"LLM-Raum-Router: '%s' → %s (%s)",
|
||||
text[:80], llm_room.name, llm_room.room_id[:8],
|
||||
)
|
||||
# liefert STAY oder room_id direkt (keine Kategorien, kein Name-Matching)
|
||||
llm_room, llm_stay, llm_failed = await self._llm_room_lookup(text, origin_room_id)
|
||||
|
||||
# ── Schritt 6: Move-Request-Button (Butler-Prinzip) ──────
|
||||
# Vor dem Routing: Button an Andreas — "Soll ich nach [Raum] kopieren?"
|
||||
# ✅Ja → Route, 📍Hier → stay_in_room
|
||||
return RoutingDecision(
|
||||
category="work_order",
|
||||
target_room=llm_room.room_id,
|
||||
confidence=0.90,
|
||||
matched_room_name=llm_room.name,
|
||||
move_request_sent=True,
|
||||
move_request_target_name=llm_room.name,
|
||||
move_request_target_id=llm_room.room_id,
|
||||
)
|
||||
|
||||
# ── Schritt 7: LLM-Fehler oder kein Treffer ──────────────────
|
||||
# ── Schritt 6: ORIGIN-FIRST GUARD (v6) ──────────────────────
|
||||
# Paradigmenwechsel: Ursprungsraum entscheidet WOHIN die Antwort geht.
|
||||
# Archivar schlägt nur VERLEGUNG vor (Move-Request).
|
||||
if llm_failed:
|
||||
# FIX 1: Timeout/Exception → Fallback auf Ursprungsraum, NICHT Catch-All
|
||||
# LLM-Aufruf fehlgeschlagen → stay_in_room
|
||||
logger.warning(
|
||||
"LLM-Raum-Router: Aufruf fehlgeschlagen für '%.80s...' → stay_in_room (Ursprungsraum)",
|
||||
text,
|
||||
@@ -479,20 +466,65 @@ class BidirectionalRouter:
|
||||
stay_in_room=True,
|
||||
is_catch_all=False,
|
||||
)
|
||||
else:
|
||||
# LLM erfolgreich, aber kein Match → catch_all
|
||||
|
||||
if llm_stay or llm_room is None:
|
||||
# LLM sagt STAY oder unsicher → stay_in_room
|
||||
logger.info(
|
||||
"LLM-Raum-Router: kein Treffer für '%.80s...' → catch_all", text
|
||||
"LLM-Raum-Router: STAY für '%.80s...' → stay_in_room", text[:80]
|
||||
)
|
||||
catch_all = self._room_map.catch_all_room
|
||||
return RoutingDecision(
|
||||
category="direct_question",
|
||||
target_room=catch_all.room_id if catch_all else "",
|
||||
target_room="",
|
||||
confidence=0.0,
|
||||
is_catch_all=True,
|
||||
stay_in_room=True,
|
||||
)
|
||||
|
||||
# LLM hat einen Raum vorgeschlagen — Origin-First Guard prüfen
|
||||
catch_all = self._room_map.catch_all_room
|
||||
catch_all_id = catch_all.room_id if catch_all else ""
|
||||
|
||||
# Guard 1: LLM == origin_room → stay_in_room
|
||||
if origin_room_id and llm_room.room_id == origin_room_id:
|
||||
logger.info(
|
||||
"Origin-First Guard: LLM schlägt Ursprungsraum vor → stay_in_room"
|
||||
)
|
||||
return RoutingDecision(
|
||||
category="direct_question",
|
||||
target_room="",
|
||||
confidence=0.0,
|
||||
stay_in_room=True,
|
||||
)
|
||||
|
||||
# Guard 2: LLM == catch_all_room → stay_in_room
|
||||
# (Kein DM-Routing aus Projekträumen!)
|
||||
if catch_all_id and llm_room.room_id == catch_all_id:
|
||||
logger.info(
|
||||
"Origin-First Guard: LLM schlägt Catch-All vor → stay_in_room"
|
||||
)
|
||||
return RoutingDecision(
|
||||
category="direct_question",
|
||||
target_room="",
|
||||
confidence=0.0,
|
||||
stay_in_room=True,
|
||||
)
|
||||
|
||||
# Guard 3: LLM == anderer Raum → Move-Request (Butler-Prinzip)
|
||||
room_name = llm_room.name.replace("#", "") if llm_room.name.startswith("#") else llm_room.name
|
||||
logger.info(
|
||||
"LLM-Raum-Router: '%s' → %s (%s) — Move-Request pending",
|
||||
text[:80], llm_room.name, llm_room.room_id[:8],
|
||||
)
|
||||
|
||||
return RoutingDecision(
|
||||
category="work_order",
|
||||
target_room=llm_room.room_id,
|
||||
confidence=0.90,
|
||||
matched_room_name=llm_room.name,
|
||||
move_request_sent=True,
|
||||
move_request_target_name=llm_room.name,
|
||||
move_request_target_id=llm_room.room_id,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
# ── Catch-All-Schutz: unerwarteter Fehler → Fallback ──────
|
||||
logger.error(
|
||||
@@ -546,7 +578,7 @@ class BidirectionalRouter:
|
||||
|
||||
return catch_all.room_id
|
||||
|
||||
async def _llm_room_lookup(self, text: str) -> Tuple[Optional[RoomEntry], bool]:
|
||||
async def _llm_room_lookup(self, text: str, origin_room_id: str = "") -> Tuple[Optional[RoomEntry], bool, bool]:
|
||||
"""Fragt das LLM: „In welchen Raum gehört diese Nachricht?“
|
||||
|
||||
Baut eine Raum-Liste mit IDs, Namen und ALLEN Keywords aus der RoomMap
|
||||
@@ -559,13 +591,15 @@ class BidirectionalRouter:
|
||||
|
||||
Args:
|
||||
text: Der Nachrichtentext.
|
||||
origin_room_id: Rocket.Chat Raum-ID des Ursprungsraums (für Kontext).
|
||||
|
||||
Returns:
|
||||
(RoomEntry, False) — LLM erfolgreich, Raum gefunden
|
||||
(None, False) — LLM erfolgreich, kein Match (CATCH_ALL)
|
||||
(None, True) — LLM-Aufruf fehlgeschlagen (Exception/Timeout)
|
||||
(RoomEntry, False, False) — LLM erfolgreich, Raum gefunden (ROUTE)
|
||||
(None, False, False) — LLM erfolgreich, STAY (kein Routing)
|
||||
(None, False, True) — LLM-Aufruf fehlgeschlagen (Exception/Timeout)
|
||||
"""
|
||||
# Build room list for LLM prompt: "ID | Name | Keywords"
|
||||
# Nur Nicht-Catch-All-Räume als Optionen (Catch-All ist nie ein Routing-Ziel)
|
||||
room_list_parts = []
|
||||
for room in self._room_map.rooms.values():
|
||||
if room.catch_all:
|
||||
@@ -576,47 +610,55 @@ class BidirectionalRouter:
|
||||
room_list_parts.append(
|
||||
f"{room.room_id} | {room.name} | {kw_str}"
|
||||
)
|
||||
# Catch-All als Fallback
|
||||
catch_all_id = (
|
||||
self._room_map.catch_all_room.room_id
|
||||
if self._room_map.catch_all_room
|
||||
else "CATCH_ALL"
|
||||
)
|
||||
room_list_parts.append(
|
||||
f"{catch_all_id} | CATCH_ALL | (Fallback für unklare Nachrichten)"
|
||||
)
|
||||
|
||||
if not room_list_parts:
|
||||
# Keine Nicht-Catch-All-Räume → STAY
|
||||
return None, False, False
|
||||
|
||||
room_list = "\n".join(room_list_parts)
|
||||
|
||||
prompt = LLM_ROOM_PROMPT.format(room_list=room_list, text=text)
|
||||
# Ursprungsraum-Name für Kontext
|
||||
origin_name = ""
|
||||
if origin_room_id:
|
||||
origin_room = self._room_map.get_room_by_id(origin_room_id)
|
||||
if origin_room:
|
||||
origin_name = origin_room.name
|
||||
|
||||
# Try LLM — expects room ID directly
|
||||
prompt = LLM_ROOM_PROMPT.format(
|
||||
room_list=room_list,
|
||||
text=text,
|
||||
origin_room=origin_name or "unbekannt",
|
||||
)
|
||||
|
||||
# Try LLM — expects room ID directly or "STAY"
|
||||
# Semaphore schützt vor parallelen Ollama-Aufrufen (qwen2.5:7b = single-request)
|
||||
async with self._llm_semaphore:
|
||||
llm_result = await self._classifier._llm_raw(prompt)
|
||||
if llm_result is None:
|
||||
return None, True # LLM-Aufruf fehlgeschlagen
|
||||
return None, False, True # LLM-Aufruf fehlgeschlagen
|
||||
|
||||
# Parse LLM response — should be a room ID or "CATCH_ALL"
|
||||
# Parse LLM response — should be a room ID or "STAY"
|
||||
response = llm_result.strip()
|
||||
|
||||
if response.upper() in ("CATCH_ALL", "CATCH-ALL", "CATCHALL"):
|
||||
return None, False # LLM erfolgreich, kein Match
|
||||
# STAY = Nachricht bleibt im Ursprungsraum
|
||||
if response.upper() == "STAY":
|
||||
return None, False, False # LLM erfolgreich, STAY
|
||||
|
||||
# Direct lookup by room ID (LLM returns the ID directly)
|
||||
room = self._room_map.get_room_by_id(response)
|
||||
if room is not None:
|
||||
return room, False
|
||||
return room, False, False
|
||||
|
||||
# Fallback: try name matching (for LLMs that return names instead of IDs)
|
||||
response_lower = response.lower()
|
||||
for room in self._room_map.rooms.values():
|
||||
if response_lower == room.name.lower():
|
||||
return room, False
|
||||
return room, False, False
|
||||
if response_lower in room.name.lower():
|
||||
return room, False
|
||||
return room, False, False
|
||||
|
||||
logger.debug("LLM returned unknown room: %s", response)
|
||||
return None, False
|
||||
return None, False, False # Unbekannte Antwort → STAY
|
||||
|
||||
# ── Route Validation ─────────────────────────────────────────────
|
||||
|
||||
|
||||
Reference in New Issue
Block a user