homelab-brain/homelab-ai-bot/rag_mode.py
Homelab Cursor 44d80d2a9e feat(bot): Schalter Betriebsart Unterlagen (RAG zuerst)
- Neue Tastaturzeile: Unterlagen AUS/AN zeigt Modus und schaltet um.
- document_mode in ask_with_tools: erzwingt lokales Modell und RAG-Pflicht
  wie bei Doc-Keywords (Session wird bei Suche wie bisher bereinigt).
- Optional: doku:/rag: Prefix fuer einmalige Suche ohne Modus.
- Sprache und Hilfetext ergaenzt.
2026-03-26 17:05:06 +01:00

57 lines
1.4 KiB
Python

"""Pro-Chat: Betriebsart Unterlagen zuerst (RAG vor Web)."""
from __future__ import annotations
from typing import Optional, Tuple
_active: dict[str, bool] = {}
BTN_OFF = "📁 Unterlagen: AUS"
BTN_ON = "📁 Unterlagen: AN"
def is_document_mode(channel_key: str) -> bool:
return _active.get(channel_key, False)
def set_document_mode(channel_key: str, on: bool) -> None:
if on:
_active[channel_key] = True
else:
_active.pop(channel_key, None)
def toggle_document_mode(channel_key: str) -> bool:
cur = is_document_mode(channel_key)
set_document_mode(channel_key, not cur)
return not cur
def keyboard_label(channel_key: str) -> str:
return BTN_ON if is_document_mode(channel_key) else BTN_OFF
def is_mode_button(text: str) -> bool:
t = (text or "").strip()
return t in (BTN_ON, BTN_OFF)
def handle_mode_button(text: str, channel_key: str) -> Optional[bool]:
"""Returns True if turned ON, False if OFF, None if not a mode button."""
t = (text or "").strip()
if t == BTN_OFF:
set_document_mode(channel_key, True)
return True
if t == BTN_ON:
set_document_mode(channel_key, False)
return False
return None
def strip_document_prefix(text: str) -> Tuple[str, bool]:
t = (text or "").strip()
low = t.lower()
for p in ("doku:", "rag:", "#doku"):
if low.startswith(p):
return t[len(p) :].lstrip(), True
return t, False