364 lines
16 KiB
Python
364 lines
16 KiB
Python
import streamlit as st
|
||
import uuid
|
||
import re
|
||
from datetime import datetime
|
||
from streamlit_agraph import agraph, Config
|
||
from qdrant_client import models
|
||
|
||
# Importe aus den anderen Modulen
|
||
from ui_utils import parse_markdown_draft, build_markdown_doc, load_history_from_logs, slugify
|
||
from ui_api import save_draft_to_vault, analyze_draft_text, send_chat_message, submit_feedback
|
||
from ui_config import HISTORY_FILE, COLLECTION_PREFIX, GRAPH_COLORS
|
||
|
||
def render_sidebar():
|
||
"""
|
||
Rendert die Sidebar mit Modus-Auswahl und Verlauf.
|
||
"""
|
||
with st.sidebar:
|
||
st.title("🧠 mindnet")
|
||
st.caption("v2.6 | WP-19 Graph View")
|
||
mode = st.radio("Modus", ["💬 Chat", "📝 Manueller Editor", "🕸️ Graph Explorer"], index=0)
|
||
|
||
st.divider()
|
||
st.subheader("⚙️ Settings")
|
||
top_k = st.slider("Quellen (Top-K)", 1, 10, 5)
|
||
explain = st.toggle("Explanation Layer", True)
|
||
|
||
st.divider()
|
||
st.subheader("🕒 Verlauf")
|
||
for q in load_history_from_logs(HISTORY_FILE, 8):
|
||
if st.button(f"🔎 {q[:25]}...", key=f"hist_{q}", use_container_width=True):
|
||
st.session_state.messages.append({"role": "user", "content": q})
|
||
st.rerun()
|
||
return mode, top_k, explain
|
||
|
||
def render_draft_editor(msg):
|
||
"""
|
||
Rendert den Editor für Drafts (genutzt im Chat bei INTERVIEW Intent oder im manuellen Modus).
|
||
"""
|
||
if "query_id" not in msg or not msg["query_id"]:
|
||
msg["query_id"] = str(uuid.uuid4())
|
||
|
||
qid = msg["query_id"]
|
||
key_base = f"draft_{qid}"
|
||
|
||
# State Keys für Persistenz
|
||
data_meta_key = f"{key_base}_data_meta"
|
||
data_sugg_key = f"{key_base}_data_suggestions"
|
||
widget_body_key = f"{key_base}_widget_body"
|
||
data_body_key = f"{key_base}_data_body"
|
||
|
||
# --- INIT STATE ---
|
||
if f"{key_base}_init" not in st.session_state:
|
||
meta, body = parse_markdown_draft(msg["content"])
|
||
if "type" not in meta: meta["type"] = "default"
|
||
if "title" not in meta: meta["title"] = ""
|
||
tags = meta.get("tags", [])
|
||
meta["tags_str"] = ", ".join(tags) if isinstance(tags, list) else str(tags)
|
||
|
||
st.session_state[data_meta_key] = meta
|
||
st.session_state[data_sugg_key] = []
|
||
st.session_state[data_body_key] = body.strip()
|
||
|
||
# Widget States initialisieren
|
||
st.session_state[f"{key_base}_wdg_title"] = meta["title"]
|
||
st.session_state[f"{key_base}_wdg_type"] = meta["type"]
|
||
st.session_state[f"{key_base}_wdg_tags"] = meta["tags_str"]
|
||
st.session_state[f"{key_base}_init"] = True
|
||
|
||
# --- STATE RESURRECTION ---
|
||
if widget_body_key not in st.session_state and data_body_key in st.session_state:
|
||
st.session_state[widget_body_key] = st.session_state[data_body_key]
|
||
|
||
# --- CALLBACKS ---
|
||
def _sync_meta():
|
||
meta = st.session_state[data_meta_key]
|
||
meta["title"] = st.session_state.get(f"{key_base}_wdg_title", "")
|
||
meta["type"] = st.session_state.get(f"{key_base}_wdg_type", "default")
|
||
meta["tags_str"] = st.session_state.get(f"{key_base}_wdg_tags", "")
|
||
st.session_state[data_meta_key] = meta
|
||
|
||
def _sync_body():
|
||
st.session_state[data_body_key] = st.session_state[widget_body_key]
|
||
|
||
def _insert_text(text_to_insert):
|
||
current = st.session_state.get(widget_body_key, "")
|
||
new_text = f"{current}\n\n{text_to_insert}"
|
||
st.session_state[widget_body_key] = new_text
|
||
st.session_state[data_body_key] = new_text
|
||
|
||
def _remove_text(text_to_remove):
|
||
current = st.session_state.get(widget_body_key, "")
|
||
new_text = current.replace(text_to_remove, "").strip()
|
||
st.session_state[widget_body_key] = new_text
|
||
st.session_state[data_body_key] = new_text
|
||
|
||
# --- UI LAYOUT ---
|
||
st.markdown(f'<div class="draft-box">', unsafe_allow_html=True)
|
||
st.markdown("### 📝 Entwurf bearbeiten")
|
||
|
||
meta_ref = st.session_state[data_meta_key]
|
||
|
||
c1, c2 = st.columns([2, 1])
|
||
with c1:
|
||
st.text_input("Titel", key=f"{key_base}_wdg_title", on_change=_sync_meta)
|
||
with c2:
|
||
known_types = ["concept", "project", "decision", "experience", "journal", "value", "goal", "principle", "risk", "belief"]
|
||
curr_type = st.session_state.get(f"{key_base}_wdg_type", meta_ref["type"])
|
||
if curr_type not in known_types: known_types.append(curr_type)
|
||
st.selectbox("Typ", known_types, key=f"{key_base}_wdg_type", on_change=_sync_meta)
|
||
|
||
st.text_input("Tags", key=f"{key_base}_wdg_tags", on_change=_sync_meta)
|
||
|
||
tab_edit, tab_intel, tab_view = st.tabs(["✏️ Inhalt", "🧠 Intelligence", "👁️ Vorschau"])
|
||
|
||
with tab_edit:
|
||
st.text_area("Body", key=widget_body_key, height=500, on_change=_sync_body, label_visibility="collapsed")
|
||
|
||
with tab_intel:
|
||
st.info("Klicke auf 'Analysieren', um Verknüpfungen für den AKTUELLEN Text zu finden.")
|
||
|
||
if st.button("🔍 Analyse starten", key=f"{key_base}_analyze"):
|
||
st.session_state[data_sugg_key] = []
|
||
text_to_analyze = st.session_state.get(widget_body_key, st.session_state.get(data_body_key, ""))
|
||
current_doc_type = st.session_state.get(f"{key_base}_wdg_type", "concept")
|
||
|
||
with st.spinner("Analysiere..."):
|
||
analysis = analyze_draft_text(text_to_analyze, current_doc_type)
|
||
if "error" in analysis:
|
||
st.error(f"Fehler: {analysis['error']}")
|
||
else:
|
||
suggestions = analysis.get("suggestions", [])
|
||
st.session_state[data_sugg_key] = suggestions
|
||
if not suggestions: st.warning("Keine Vorschläge gefunden.")
|
||
else: st.success(f"{len(suggestions)} Vorschläge gefunden.")
|
||
|
||
suggestions = st.session_state[data_sugg_key]
|
||
if suggestions:
|
||
current_text_state = st.session_state.get(widget_body_key, "")
|
||
for idx, sugg in enumerate(suggestions):
|
||
link_text = sugg.get('suggested_markdown', '')
|
||
is_inserted = link_text in current_text_state
|
||
|
||
bg_color = "#e6fffa" if is_inserted else "#ffffff"
|
||
border = "3px solid #28a745" if is_inserted else "3px solid #1a73e8"
|
||
|
||
st.markdown(f"""
|
||
<div style="border-left: {border}; background-color: {bg_color}; padding: 10px; margin-bottom: 8px; border-radius: 4px; box-shadow: 0 1px 3px rgba(0,0,0,0.1);">
|
||
<b>{sugg.get('target_title')}</b> <small>({sugg.get('type')})</small><br>
|
||
<i>{sugg.get('reason')}</i><br>
|
||
<code>{link_text}</code>
|
||
</div>
|
||
""", unsafe_allow_html=True)
|
||
|
||
if is_inserted:
|
||
st.button("❌ Entfernen", key=f"del_{idx}_{key_base}", on_click=_remove_text, args=(link_text,))
|
||
else:
|
||
st.button("➕ Einfügen", key=f"add_{idx}_{key_base}", on_click=_insert_text, args=(link_text,))
|
||
|
||
final_tags_str = st.session_state.get(f"{key_base}_wdg_tags", "")
|
||
final_tags = [t.strip() for t in final_tags_str.split(",") if t.strip()]
|
||
|
||
final_meta = {
|
||
"id": "generated_on_save",
|
||
"type": st.session_state.get(f"{key_base}_wdg_type", "default"),
|
||
"title": st.session_state.get(f"{key_base}_wdg_title", "").strip(),
|
||
"status": "draft",
|
||
"tags": final_tags
|
||
}
|
||
final_body = st.session_state.get(widget_body_key, st.session_state[data_body_key])
|
||
|
||
if not final_meta["title"]:
|
||
h1_match = re.search(r"^#\s+(.*)$", final_body, re.MULTILINE)
|
||
if h1_match: final_meta["title"] = h1_match.group(1).strip()
|
||
|
||
final_doc = build_markdown_doc(final_meta, final_body)
|
||
|
||
with tab_view:
|
||
st.markdown('<div class="preview-box">', unsafe_allow_html=True)
|
||
st.markdown(final_doc)
|
||
st.markdown('</div>', unsafe_allow_html=True)
|
||
|
||
st.markdown("---")
|
||
|
||
b1, b2 = st.columns([1, 1])
|
||
with b1:
|
||
if st.button("💾 Speichern & Indizieren", type="primary", key=f"{key_base}_save"):
|
||
with st.spinner("Speichere im Vault..."):
|
||
raw_title = final_meta.get("title", "")
|
||
if not raw_title:
|
||
clean_body = re.sub(r"[#*_\[\]()]", "", final_body).strip()
|
||
raw_title = clean_body[:40] if clean_body else "draft"
|
||
safe_title = slugify(raw_title)[:60] or "draft"
|
||
fname = f"{datetime.now().strftime('%Y%m%d')}-{safe_title}.md"
|
||
|
||
result = save_draft_to_vault(final_doc, filename=fname)
|
||
if "error" in result: st.error(f"Fehler: {result['error']}")
|
||
else:
|
||
st.success(f"Gespeichert: {result.get('file_path')}")
|
||
st.balloons()
|
||
with b2:
|
||
if st.button("📋 Code anzeigen", key=f"{key_base}_btn_copy"):
|
||
st.code(final_doc, language="markdown")
|
||
|
||
st.markdown("</div>", unsafe_allow_html=True)
|
||
|
||
def render_chat_interface(top_k, explain):
|
||
"""
|
||
Rendert den Chat-Verlauf und das Eingabefeld.
|
||
"""
|
||
for idx, msg in enumerate(st.session_state.messages):
|
||
with st.chat_message(msg["role"]):
|
||
if msg["role"] == "assistant":
|
||
intent = msg.get("intent", "UNKNOWN")
|
||
src = msg.get("intent_source", "?")
|
||
icon = {"EMPATHY":"❤️", "DECISION":"⚖️", "CODING":"💻", "FACT":"📚", "INTERVIEW":"📝"}.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)
|
||
|
||
with st.expander("🐞 Debug Raw Payload", expanded=False):
|
||
st.json(msg)
|
||
|
||
if intent == "INTERVIEW":
|
||
render_draft_editor(msg)
|
||
else:
|
||
st.markdown(msg["content"])
|
||
|
||
if "sources" in msg and msg["sources"]:
|
||
for hit in msg["sources"]:
|
||
with st.expander(f"📄 {hit.get('note_id', '?')} ({hit.get('total_score', 0):.2f})"):
|
||
st.markdown(f"_{hit.get('source', {}).get('text', '')[:300]}..._")
|
||
if hit.get('explanation'):
|
||
st.caption(f"Grund: {hit['explanation']['reasons'][0]['message']}")
|
||
|
||
def _cb(qid=msg.get("query_id"), nid=hit.get('node_id')):
|
||
val = st.session_state.get(f"fb_src_{qid}_{nid}")
|
||
if val is not None: submit_feedback(qid, nid, val+1)
|
||
st.feedback("faces", key=f"fb_src_{msg.get('query_id')}_{hit.get('node_id')}", on_change=_cb)
|
||
|
||
if "query_id" in msg:
|
||
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))
|
||
else:
|
||
st.markdown(msg["content"])
|
||
|
||
if prompt := st.chat_input("Frage Mindnet..."):
|
||
st.session_state.messages.append({"role": "user", "content": prompt})
|
||
st.rerun()
|
||
|
||
if len(st.session_state.messages) > 0 and st.session_state.messages[-1]["role"] == "user":
|
||
with st.chat_message("assistant"):
|
||
with st.spinner("Thinking..."):
|
||
resp = send_chat_message(st.session_state.messages[-1]["content"], top_k, explain)
|
||
if "error" in resp:
|
||
st.error(resp["error"])
|
||
else:
|
||
st.session_state.messages.append({
|
||
"role": "assistant",
|
||
"content": resp.get("answer"),
|
||
"intent": resp.get("intent", "FACT"),
|
||
"intent_source": resp.get("intent_source", "Unknown"),
|
||
"sources": resp.get("sources", []),
|
||
"query_id": resp.get("query_id")
|
||
})
|
||
st.rerun()
|
||
|
||
def render_manual_editor():
|
||
"""Rendert einen leeren Editor für manuelle Eingaben."""
|
||
mock_msg = {
|
||
"content": "---\ntype: concept\ntitle: Neue Notiz\nstatus: draft\ntags: []\n---\n# Titel\n",
|
||
"query_id": "manual_mode_v2"
|
||
}
|
||
render_draft_editor(mock_msg)
|
||
|
||
def render_graph_explorer(graph_service):
|
||
"""
|
||
Rendert den erweiterten Graph Explorer (WP-19).
|
||
"""
|
||
st.header("🕸️ Graph Explorer")
|
||
|
||
# State Management für Graph Navigation
|
||
if "graph_center_id" not in st.session_state:
|
||
st.session_state.graph_center_id = None
|
||
|
||
col_ctrl, col_graph = st.columns([1, 4])
|
||
|
||
with col_ctrl:
|
||
st.subheader("Fokus")
|
||
|
||
search_term = st.text_input("Suche Notiz", placeholder="Titel eingeben...")
|
||
|
||
options = {}
|
||
if search_term:
|
||
hits, _ = graph_service.client.scroll(
|
||
collection_name=f"{COLLECTION_PREFIX}_notes",
|
||
scroll_filter=models.Filter(
|
||
must=[models.FieldCondition(key="title", match=models.MatchText(text=search_term))]
|
||
),
|
||
limit=10
|
||
)
|
||
options = {h.payload['title']: h.payload['note_id'] for h in hits}
|
||
|
||
if options:
|
||
selected_title = st.selectbox("Ergebnisse:", list(options.keys()))
|
||
if st.button("Laden", use_container_width=True):
|
||
st.session_state.graph_center_id = options[selected_title]
|
||
st.rerun()
|
||
|
||
st.divider()
|
||
st.caption("Legende (Wichtigste Typen)")
|
||
for k, v in list(GRAPH_COLORS.items())[:8]:
|
||
st.markdown(f"<span style='color:{v}'>●</span> {k}", unsafe_allow_html=True)
|
||
st.caption("Weitere Farben siehe `ui_config.py`")
|
||
|
||
st.divider()
|
||
depth_val = st.slider("Tiefe (Tier)", 1, 3, 2, help="Level 1 = Nachbarn, Level 2 = Nachbarn der Nachbarn")
|
||
st.info("💡 Tipp: Ein Klick auf einen Knoten zentriert die Ansicht neu.")
|
||
|
||
with col_graph:
|
||
center_id = st.session_state.graph_center_id
|
||
|
||
if center_id:
|
||
with st.spinner(f"Lade Graph für {center_id} (Tiefe {depth_val})..."):
|
||
|
||
nodes, edges = graph_service.get_ego_graph(center_id, depth=depth_val)
|
||
|
||
if not nodes:
|
||
st.warning("Keine Daten gefunden. Vielleicht existiert die Notiz nicht mehr?")
|
||
else:
|
||
# CONFIG: ForceAtlas2Based für maximale Entzerrung
|
||
config = Config(
|
||
width=1000,
|
||
height=800,
|
||
directed=True,
|
||
physics=True,
|
||
hierarchical=False,
|
||
nodeHighlightBehavior=True,
|
||
highlightColor="#F7A7A6",
|
||
collapsible=False,
|
||
# Solver Wechsel: ForceAtlas2Based ist besser für Entzerrung
|
||
solver="forceAtlas2Based",
|
||
forceAtlas2Based={
|
||
"theta": 0.5,
|
||
"gravitationalConstant": -100, # Starke Abstoßung
|
||
"centralGravity": 0.005, # Sehr schwacher Zug zur Mitte (verhindert Klumpen)
|
||
"springConstant": 0.08,
|
||
"springLength": 150, # Längere Kanten
|
||
"damping": 0.4,
|
||
"avoidOverlap": 1 # Versucht aktiv, Überlappungen zu vermeiden
|
||
},
|
||
stabilization={
|
||
"enabled": True,
|
||
"iterations": 1000
|
||
}
|
||
)
|
||
|
||
st.caption(f"Zentrum: **{center_id}** | Knoten: {len(nodes)} | Kanten: {len(edges)}")
|
||
|
||
return_value = agraph(nodes=nodes, edges=edges, config=config)
|
||
|
||
# NAVIGATION LOGIK
|
||
if return_value and return_value != center_id:
|
||
st.session_state.graph_center_id = return_value
|
||
st.rerun()
|
||
else:
|
||
st.info("👈 Wähle links eine Notiz, um den Graphen zu starten.") |