debug mode gui

This commit is contained in:
Lars 2025-12-10 17:35:21 +01:00
parent 1fefc538ac
commit f50ae4c934

View File

@ -21,7 +21,7 @@ timeout_setting = os.getenv("MINDNET_API_TIMEOUT") or os.getenv("MINDNET_LLM_TIM
API_TIMEOUT = float(timeout_setting) if timeout_setting else 300.0 API_TIMEOUT = float(timeout_setting) if timeout_setting else 300.0
# --- PAGE SETUP --- # --- PAGE SETUP ---
st.set_page_config(page_title="mindnet v2.3.2", page_icon="🧠", layout="wide") st.set_page_config(page_title="mindnet v2.3.2 (Debug Mode)", page_icon="🐞", layout="wide")
# --- CSS STYLING --- # --- CSS STYLING ---
st.markdown(""" st.markdown("""
@ -35,7 +35,6 @@ st.markdown("""
border: 1px solid #d2e3fc; display: inline-block; margin-bottom: 0.5rem; border: 1px solid #d2e3fc; display: inline-block; margin-bottom: 0.5rem;
} }
/* Editor Box Styling */
.draft-box { .draft-box {
border: 1px solid #d0d7de; border: 1px solid #d0d7de;
border-radius: 6px; border-radius: 6px;
@ -45,7 +44,6 @@ st.markdown("""
margin-bottom: 10px; margin-bottom: 10px;
} }
/* Preview Styling mimics GitHub Markdown */
.preview-box { .preview-box {
border: 1px solid #e0e0e0; border: 1px solid #e0e0e0;
border-radius: 6px; border-radius: 6px;
@ -60,54 +58,57 @@ st.markdown("""
if "messages" not in st.session_state: st.session_state.messages = [] if "messages" not in st.session_state: st.session_state.messages = []
if "user_id" not in st.session_state: st.session_state.user_id = str(uuid.uuid4()) if "user_id" not in st.session_state: st.session_state.user_id = str(uuid.uuid4())
# --- HELPER FUNCTIONS --- # --- HELPER FUNCTIONS (ROBUST PARSING) ---
def parse_markdown_draft(full_text): def parse_markdown_draft(full_text):
""" """
Zerlegt die LLM-Antwort in Frontmatter (Dict) und Body (String). Versucht extrem tolerant, Frontmatter und Body zu trennen.
Robust gegen Einleitungstexte vor dem Codeblock.
""" """
# 1. Extrahiere Codeblock clean_text = full_text
pattern_block = r"```markdown\s*(.*?)\s*```"
match_block = re.search(pattern_block, full_text, re.DOTALL)
# Fallback: Wenn kein Codeblock, nimm ganzen Text # 1. Versuch: Markdown Fences entfernen (egal ob ```markdown, ```md oder nur ```)
clean_text = match_block.group(1).strip() if match_block else full_text # re.IGNORECASE und re.DOTALL sind wichtig!
pattern_block = r"```(?:markdown|md)?\s*(.*?)\s*```"
match_block = re.search(pattern_block, full_text, re.DOTALL | re.IGNORECASE)
# 2. Trenne Frontmatter (YAML) vom Body if match_block:
# Sucht nach --- am Anfang, gefolgt von YAML, gefolgt von --- clean_text = match_block.group(1).strip()
pattern_fm = r"^---\s+(.*?)\s+---\s*(.*)$" # Debugging Info im UI anzeigen ist schwer hier, wir verlassen uns auf den Return
# 2. Versuch: YAML Frontmatter finden
# Suche nach --- am Anfang (oder nach Whitespace), gefolgt von Inhalt, gefolgt von ---
pattern_fm = r"^\s*---\s+(.*?)\s+---\s*(.*)$"
match_fm = re.search(pattern_fm, clean_text, re.DOTALL) match_fm = re.search(pattern_fm, clean_text, re.DOTALL)
meta = {}
body = clean_text
if match_fm: if match_fm:
yaml_str = match_fm.group(1) yaml_str = match_fm.group(1)
body = match_fm.group(2) body = match_fm.group(2)
try: try:
meta = yaml.safe_load(yaml_str) or {} # YAML laden, aber Fehler abfangen
except Exception: parsed = yaml.safe_load(yaml_str)
meta = {} # Fallback bei kaputtem YAML if isinstance(parsed, dict):
return meta, body meta = parsed
else: except Exception as e:
# Kein Frontmatter gefunden -> Alles ist Body print(f"YAML Parsing Error: {e}") # Geht in Server Log
return {}, clean_text # Wir behalten body, meta bleibt leer
def generate_filename(meta): return meta, body
"""Generiert einen Dateinamen basierend auf Typ und Datum."""
date_str = datetime.now().strftime("%Y%m%d")
n_type = meta.get("type", "note")
# Versuche Titel aus Body zu raten wäre zu komplex, wir nehmen Generic
return f"{date_str}-{n_type}-draft.md"
def build_markdown_doc(meta, body): def build_markdown_doc(meta, body):
"""Baut das finale Dokument zusammen.""" """Baut das finale Dokument zusammen."""
# ID Generierung beim 'Speichern' (hier simuliert für Download)
if "id" not in meta or meta["id"] == "generated_on_save": if "id" not in meta or meta["id"] == "generated_on_save":
meta["id"] = f"{datetime.now().strftime('%Y%m%d')}-{meta.get('type', 'note')}-{uuid.uuid4().hex[:6]}" meta["id"] = f"{datetime.now().strftime('%Y%m%d')}-{meta.get('type', 'note')}-{uuid.uuid4().hex[:6]}"
# Updated timestamp
meta["updated"] = datetime.now().strftime("%Y-%m-%d") meta["updated"] = datetime.now().strftime("%Y-%m-%d")
yaml_str = yaml.dump(meta, default_flow_style=None, sort_keys=False, allow_unicode=True).strip() try:
yaml_str = yaml.dump(meta, default_flow_style=None, sort_keys=False, allow_unicode=True).strip()
except:
yaml_str = "error: generating_yaml"
return f"---\n{yaml_str}\n---\n\n{body}" return f"---\n{yaml_str}\n---\n\n{body}"
def load_history_from_logs(limit=10): def load_history_from_logs(limit=10):
@ -151,7 +152,7 @@ def submit_feedback(query_id, node_id, score, comment=None):
def render_sidebar(): def render_sidebar():
with st.sidebar: with st.sidebar:
st.title("🧠 mindnet") st.title("🧠 mindnet")
st.caption("v2.3.2 | WP-10 UI") st.caption("DEBUG MODE | WP-10 UI")
mode = st.radio("Modus", ["💬 Chat", "📝 Manueller Editor"], index=0) mode = st.radio("Modus", ["💬 Chat", "📝 Manueller Editor"], index=0)
@ -176,8 +177,11 @@ def render_draft_editor(msg):
key_base = f"draft_{qid}" key_base = f"draft_{qid}"
# 1. State Initialisierung (Nur einmal pro Nachricht) # 1. State Initialisierung (Nur einmal pro Nachricht)
# Wir parsen IMMER neu, wenn der Key noch nicht im State ist
if f"{key_base}_init" not in st.session_state: if f"{key_base}_init" not in st.session_state:
meta, body = parse_markdown_draft(msg["content"]) meta, body = parse_markdown_draft(msg["content"])
# Fallback Defaults
st.session_state[f"{key_base}_type"] = meta.get("type", "default") st.session_state[f"{key_base}_type"] = meta.get("type", "default")
# Tags Behandlung (Liste oder String) # Tags Behandlung (Liste oder String)
@ -193,39 +197,36 @@ def render_draft_editor(msg):
# 2. Editor Container # 2. Editor Container
st.markdown(f'<div class="draft-box">', unsafe_allow_html=True) st.markdown(f'<div class="draft-box">', unsafe_allow_html=True)
st.markdown("### 📝 Entwurf bearbeiten") st.markdown("### 📝 Entwurf bearbeiten")
st.caption("Das System hat diesen Entwurf vorbereitet. Ergänze die fehlenden [TODO] Bereiche.")
# 3. Metadaten (Grid Layout) # 3. Metadaten (Grid Layout)
c1, c2 = st.columns([1, 2]) c1, c2 = st.columns([1, 2])
with c1: with c1:
# Typen müssen mit types.yaml synchron sein known_types = ["concept", "project", "decision", "experience", "journal", "person", "value", "goal", "principle", "default"]
known_types = ["concept", "project", "decision", "experience", "journal", "person", "value", "goal", "principle"] curr_type = st.session_state.get(f"{key_base}_type", "default")
curr_type = st.session_state[f"{key_base}_type"]
if curr_type not in known_types: known_types.append(curr_type) if curr_type not in known_types: known_types.append(curr_type)
# Selectbox mit State-Sync
new_type = st.selectbox("Typ", known_types, index=known_types.index(curr_type), key=f"{key_base}_sel_type") new_type = st.selectbox("Typ", known_types, index=known_types.index(curr_type), key=f"{key_base}_sel_type")
with c2: with c2:
new_tags = st.text_input("Tags (kommagetrennt)", value=st.session_state[f"{key_base}_tags"], key=f"{key_base}_inp_tags") new_tags = st.text_input("Tags (kommagetrennt)", value=st.session_state.get(f"{key_base}_tags", ""), key=f"{key_base}_inp_tags")
# 4. Inhalt (Tabs) # 4. Inhalt (Tabs)
tab_edit, tab_view = st.tabs(["✏️ Editor", "👁️ Vorschau"]) tab_edit, tab_view = st.tabs(["✏️ Editor", "👁️ Vorschau"])
with tab_edit: with tab_edit:
# Hier landet der Body mit den ## Headings und [TODO]s
new_body = st.text_area( new_body = st.text_area(
"Inhalt", "Inhalt (Markdown Body)",
value=st.session_state[f"{key_base}_body"], value=st.session_state.get(f"{key_base}_body", ""),
height=500, height=500,
key=f"{key_base}_txt_body", key=f"{key_base}_txt_body",
label_visibility="collapsed", label_visibility="collapsed"
help="Hier kannst du Markdown schreiben."
) )
# Live-Zusammenbau für Vorschau & Download # Live-Zusammenbau
final_tags_list = [t.strip() for t in new_tags.split(",") if t.strip()] final_tags_list = [t.strip() for t in new_tags.split(",") if t.strip()]
final_meta = { final_meta = {
"id": "generated_on_save", # Placeholder, wird beim Download ersetzt "id": "generated_on_save",
"type": new_type, "type": new_type,
"status": "draft", "status": "draft",
"tags": final_tags_list "tags": final_tags_list
@ -234,47 +235,48 @@ def render_draft_editor(msg):
with tab_view: with tab_view:
st.markdown('<div class="preview-box">', unsafe_allow_html=True) st.markdown('<div class="preview-box">', unsafe_allow_html=True)
st.markdown(final_doc) # Rendered Markdown st.markdown(final_doc)
st.markdown('</div>', unsafe_allow_html=True) st.markdown('</div>', unsafe_allow_html=True)
st.markdown("---") st.markdown("---")
# 5. Actions (Writeback Simulation) # 5. Actions
b1, b2 = st.columns([1, 1]) b1, b2 = st.columns([1, 1])
with b1: with b1:
# Download Button (Client-Side)
st.download_button( st.download_button(
label="💾 Als .md Datei speichern", label="💾 Download .md",
data=final_doc, data=final_doc,
file_name=generate_filename(final_meta), file_name=generate_filename(final_meta),
mime="text/markdown" mime="text/markdown"
) )
with b2: with b2:
# Copy Button Logic if st.button("📋 Code Copy", key=f"{key_base}_btn_copy"):
if st.button("📋 Code anzeigen (Copy)", key=f"{key_base}_btn_copy"):
st.code(final_doc, language="markdown") st.code(final_doc, language="markdown")
st.markdown("</div>", unsafe_allow_html=True) st.markdown("</div>", unsafe_allow_html=True)
def render_chat_interface(top_k, explain): def render_chat_interface(top_k, explain):
for msg in st.session_state.messages: for idx, msg in enumerate(st.session_state.messages):
with st.chat_message(msg["role"]): with st.chat_message(msg["role"]):
if msg["role"] == "assistant": if msg["role"] == "assistant":
# Intent Badge # Intent Badge
if "intent" in msg: intent = msg.get("intent", "UNKNOWN")
intent = msg["intent"] src = msg.get("intent_source", "?")
icon = {"EMPATHY": "❤️", "DECISION": "⚖️", "CODING": "💻", "FACT": "📚", "INTERVIEW": "📝"}.get(intent, "🧠")
src = msg.get("intent_source", "?")
st.markdown(f'<div class="intent-badge">{icon} Intent: {intent} <span style="opacity:0.6; font-size:0.8em">({src})</span></div>', unsafe_allow_html=True)
# INTERVIEW Logic vs NORMAL Chat # Icon Mapping
if msg.get("intent") == "INTERVIEW": icon_map = {"EMPATHY":"❤️", "DECISION":"⚖️", "CODING":"💻", "FACT":"📚", "INTERVIEW":"📝"}
icon = icon_map.get(intent, "🧠")
st.markdown(f'<div class="intent-badge">{icon} Intent: {intent} <span style="opacity:0.6; font-size:0.8em">({src})</span></div>', unsafe_allow_html=True)
# --- LOGIC SWITCH ---
if intent == "INTERVIEW":
render_draft_editor(msg) render_draft_editor(msg)
else: else:
st.markdown(msg["content"]) st.markdown(msg["content"])
# Sources & Feedback (nur bei RAG sinnvoll) # --- SOURCES ---
if "sources" in msg and msg["sources"]: if "sources" in msg and msg["sources"]:
for hit in msg["sources"]: for hit in msg["sources"]:
score = hit.get('total_score', 0) score = hit.get('total_score', 0)
@ -284,18 +286,20 @@ def render_chat_interface(top_k, explain):
if hit.get('explanation'): if hit.get('explanation'):
st.caption(f"Grund: {hit['explanation']['reasons'][0]['message']}") st.caption(f"Grund: {hit['explanation']['reasons'][0]['message']}")
def _cb(qid=msg["query_id"], nid=hit['node_id']): def _cb(qid=msg.get("query_id"), nid=hit.get('node_id')):
val = st.session_state.get(f"fb_src_{qid}_{nid}") val = st.session_state.get(f"fb_src_{qid}_{nid}")
if val is not None: submit_feedback(qid, nid, val+1) if val is not None: submit_feedback(qid, nid, val+1)
st.feedback("faces", key=f"fb_src_{msg['query_id']}_{hit['node_id']}", on_change=_cb)
if "query_id" in msg: st.feedback("faces", key=f"fb_src_{msg.get('query_id')}_{hit.get('node_id')}", on_change=_cb)
qid = msg["query_id"]
st.feedback("stars", key=f"fb_glob_{qid}", on_change=lambda: submit_feedback(qid, "generated_answer", st.session_state[f"fb_glob_{qid}"]+1)) # --- DEBUGGING (NEU) ---
with st.expander("🐞 Debug Raw Payload"):
st.json(msg) # Zeigt exakt, was das Backend geschickt hat
else: else:
st.markdown(msg["content"]) st.markdown(msg["content"])
# Input Loop # Input
if prompt := st.chat_input("Frage Mindnet... (z.B. 'Neues Projekt anlegen')"): if prompt := st.chat_input("Frage Mindnet... (z.B. 'Neues Projekt anlegen')"):
st.session_state.messages.append({"role": "user", "content": prompt}) st.session_state.messages.append({"role": "user", "content": prompt})
st.rerun() st.rerun()
@ -322,8 +326,6 @@ def render_chat_interface(top_k, explain):
def render_manual_editor(): def render_manual_editor():
st.header("📝 Manueller Editor") st.header("📝 Manueller Editor")
st.info("Hier kannst du eine Notiz komplett von Null erstellen.") st.info("Hier kannst du eine Notiz komplett von Null erstellen.")
# Wiederverwendung der Editor-Logik wäre ideal, hier vereinfacht:
c1, c2 = st.columns([1, 2]) c1, c2 = st.columns([1, 2])
n_type = c1.selectbox("Typ", ["concept", "project", "decision", "experience", "value", "goal"]) n_type = c1.selectbox("Typ", ["concept", "project", "decision", "experience", "value", "goal"])
tags = c2.text_input("Tags") tags = c2.text_input("Tags")