UI erste Version

This commit is contained in:
Lars 2025-12-14 07:34:51 +01:00
parent 620858a575
commit 028ad7e941

View File

@ -10,6 +10,14 @@ from datetime import datetime
from pathlib import Path
from dotenv import load_dotenv
# --- WP-19 GRAPH IMPORTS ---
try:
from streamlit_agraph import agraph, Node, Edge, Config
from qdrant_client import QdrantClient, models
except ImportError:
st.error("Fehlende Bibliotheken! Bitte installiere: pip install streamlit-agraph qdrant-client")
st.stop()
# --- CONFIGURATION ---
load_dotenv()
API_BASE_URL = os.getenv("MINDNET_API_URL", "http://localhost:8002")
@ -19,17 +27,23 @@ INGEST_ANALYZE_ENDPOINT = f"{API_BASE_URL}/ingest/analyze"
INGEST_SAVE_ENDPOINT = f"{API_BASE_URL}/ingest/save"
HISTORY_FILE = Path("data/logs/search_history.jsonl")
# Qdrant Config (Direct Access for Graph)
QDRANT_URL = os.getenv("QDRANT_URL", "http://localhost:6333")
QDRANT_KEY = os.getenv("QDRANT_API_KEY", None)
if QDRANT_KEY == "": QDRANT_KEY = None
COLLECTION_PREFIX = os.getenv("COLLECTION_PREFIX", "mindnet")
# Timeout Strategy
timeout_setting = os.getenv("MINDNET_API_TIMEOUT") or os.getenv("MINDNET_LLM_TIMEOUT")
API_TIMEOUT = float(timeout_setting) if timeout_setting else 300.0
# --- PAGE SETUP ---
st.set_page_config(page_title="mindnet v2.5", page_icon="🧠", layout="wide")
st.set_page_config(page_title="mindnet v2.6", page_icon="🧠", layout="wide")
# --- CSS STYLING ---
st.markdown("""
<style>
.block-container { padding-top: 2rem; max_width: 1000px; margin: auto; }
.block-container { padding-top: 2rem; max_width: 1200px; margin: auto; }
.intent-badge {
background-color: #e8f0fe; color: #1a73e8;
@ -61,6 +75,25 @@ st.markdown("""
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())
# --- GRAPH STYLING CONFIG (WP-19) ---
GRAPH_COLORS = {
"project": "#ff9f43", # Orange
"concept": "#54a0ff", # Blau
"decision": "#5f27cd", # Lila
"risk": "#ff6b6b", # Rot
"person": "#1dd1a1", # Grün
"experience": "#feca57",# Gelb
"default": "#8395a7" # Grau
}
EDGE_COLORS = {
"depends_on": "#ff6b6b", # Rot (Blocker)
"blocks": "#ee5253", # Dunkelrot
"related_to": "#c8d6e5", # Hellgrau
"next": "#54a0ff", # Blau
"derived_from": "#ff9ff3"# Pink
}
# --- HELPER FUNCTIONS ---
def slugify(value):
@ -116,12 +149,7 @@ def normalize_meta_and_body(meta, body):
return clean_meta, final_body
def parse_markdown_draft(full_text):
"""
HEALING PARSER: Repariert kaputten LLM Output (z.B. fehlendes schließendes '---').
"""
clean_text = full_text.strip()
# 1. Code-Block Wrapper entfernen
pattern_block = r"```(?:markdown|md|yaml)?\s*(.*?)\s*```"
match_block = re.search(pattern_block, clean_text, re.DOTALL | re.IGNORECASE)
if match_block:
@ -131,26 +159,19 @@ def parse_markdown_draft(full_text):
body = clean_text
yaml_str = ""
# 2. Versuch A: Standard Split (Idealfall)
parts = re.split(r"^---+\s*$", clean_text, maxsplit=2, flags=re.MULTILINE)
if len(parts) >= 3:
yaml_str = parts[1]
body = parts[2]
# 3. Versuch B: Healing (Wenn LLM das schließende --- vergessen hat)
elif clean_text.startswith("---"):
# Wir suchen die erste Überschrift '#', da Frontmatter davor sein muss
# Pattern: Suche --- am Anfang, dann nimm alles bis zum ersten # am Zeilenanfang
fallback_match = re.search(r"^---\s*(.*?)(?=\n#)", clean_text, re.DOTALL | re.MULTILINE)
if fallback_match:
yaml_str = fallback_match.group(1)
# Der Body ist alles NACH dem YAML String (inklusive dem #)
body = clean_text.replace(f"---{yaml_str}", "", 1).strip()
# 4. YAML Parsing
if yaml_str:
yaml_str_clean = yaml_str.replace("#", "") # Tags cleanen
yaml_str_clean = yaml_str.replace("#", "")
try:
parsed = yaml.safe_load(yaml_str_clean)
if isinstance(parsed, dict):
@ -158,13 +179,11 @@ def parse_markdown_draft(full_text):
except Exception as e:
print(f"YAML Parsing Warning: {e}")
# Fallback: Titel aus H1
if not meta.get("title"):
h1_match = re.search(r"^#\s+(.*)$", body, re.MULTILINE)
if h1_match:
meta["title"] = h1_match.group(1).strip()
# Correction: type/status swap
if meta.get("type") == "draft":
meta["status"] = "draft"
meta["type"] = "experience"
@ -172,7 +191,6 @@ def parse_markdown_draft(full_text):
return normalize_meta_and_body(meta, body)
def build_markdown_doc(meta, body):
"""Baut das finale Dokument zusammen."""
if "id" not in meta or meta["id"] == "generated_on_save":
raw_title = meta.get('title', 'note')
clean_slug = slugify(raw_title)[:50] or "note"
@ -210,6 +228,120 @@ def load_history_from_logs(limit=10):
except: pass
return queries
# --- WP-19 GRAPH SERVICE ---
class GraphExplorerService:
def __init__(self, url, api_key=None, prefix="mindnet"):
self.client = QdrantClient(url=url, api_key=api_key)
self.prefix = prefix
self.notes_col = f"{prefix}_notes"
self.chunks_col = f"{prefix}_chunks"
self.edges_col = f"{prefix}_edges"
def get_ego_graph(self, center_note_id: str):
"""Erzeugt einen Ego-Graphen (Node + Nachbarn) für die Visualisierung."""
nodes = {} # id -> Node Object
edges_list = [] # List of Edge Objects
# 1. Zentrale Note laden
center_note = self._fetch_note(center_note_id)
if not center_note: return [], []
self._add_node(nodes, center_note, is_center=True)
# 2. Chunks der Note finden (Source Chunks)
scroll_filter = models.Filter(
must=[models.FieldCondition(key="note_id", match=models.MatchValue(value=center_note_id))]
)
chunks, _ = self.client.scroll(
collection_name=self.chunks_col, scroll_filter=scroll_filter, limit=100, with_payload=True
)
chunk_ids = [c.id for c in chunks]
# 3. Kanten finden
if chunk_ids:
edge_filter = models.Filter(
must=[models.FieldCondition(key="source_id", match=models.MatchAny(any=chunk_ids))]
)
raw_edges, _ = self.client.scroll(
collection_name=self.edges_col, scroll_filter=edge_filter, limit=100, with_payload=True
)
# 4. Targets auflösen
for re in raw_edges:
payload = re.payload
target_chunk_id = payload.get("target_id")
kind = payload.get("kind")
provenance = payload.get("provenance", "explicit")
target_note = self._resolve_note_from_chunk(target_chunk_id)
if target_note and target_note['note_id'] != center_note_id:
self._add_node(nodes, target_note)
# Styling
color = EDGE_COLORS.get(kind, "#bdc3c7")
is_smart = provenance != "explicit" and provenance != "rule"
label = f"{kind}"
if is_smart: label += " 🤖"
edges_list.append(Edge(
source=center_note_id,
target=target_note['note_id'],
label=label,
color=color,
dashes=is_smart,
title=f"Provenance: {provenance}"
))
return list(nodes.values()), edges_list
def _fetch_note(self, note_id):
res, _ = self.client.scroll(
collection_name=self.notes_col,
scroll_filter=models.Filter(must=[models.FieldCondition(key="note_id", match=models.MatchValue(value=note_id))]),
limit=1, with_payload=True
)
return res[0].payload if res else None
def _resolve_note_from_chunk(self, chunk_id_or_title):
if "#" in chunk_id_or_title:
res = self.client.retrieve(collection_name=self.chunks_col, ids=[chunk_id_or_title], with_payload=True)
if res:
parent_id = res[0].payload.get("note_id")
return self._fetch_note(parent_id)
else:
# Versuch: Direkter Match auf Note Titel (für WikiLinks)
# In Production sollte das optimiert werden (Cache)
res, _ = self.client.scroll(
collection_name=self.notes_col,
scroll_filter=models.Filter(must=[models.FieldCondition(key="title", match=models.MatchValue(value=chunk_id_or_title))]),
limit=1, with_payload=True
)
return res[0].payload if res else None
return None
def _add_node(self, node_dict, note_payload, is_center=False):
nid = note_payload.get("note_id")
if nid in node_dict: return
ntype = note_payload.get("type", "default")
color = GRAPH_COLORS.get(ntype, GRAPH_COLORS["default"])
size = 35 if is_center else 20
node_dict[nid] = Node(
id=nid,
label=note_payload.get("title", nid),
size=size,
color=color,
shape="dot",
title=f"Type: {ntype}\nTags: {note_payload.get('tags')}",
font={'color': 'black'}
)
# Init Graph Service
graph_service = GraphExplorerService(QDRANT_URL, QDRANT_KEY, COLLECTION_PREFIX)
# --- API CLIENT ---
def send_chat_message(message: str, top_k: int, explain: bool):
@ -226,11 +358,7 @@ def send_chat_message(message: str, top_k: int, explain: bool):
def analyze_draft_text(text: str, n_type: str):
try:
response = requests.post(
INGEST_ANALYZE_ENDPOINT,
json={"text": text, "type": n_type},
timeout=15
)
response = requests.post(INGEST_ANALYZE_ENDPOINT, json={"text": text, "type": n_type}, timeout=15)
response.raise_for_status()
return response.json()
except Exception as e:
@ -238,11 +366,7 @@ def analyze_draft_text(text: str, n_type: str):
def save_draft_to_vault(markdown_content: str, filename: str = None):
try:
response = requests.post(
INGEST_SAVE_ENDPOINT,
json={"markdown_content": markdown_content, "filename": filename},
timeout=API_TIMEOUT
)
response = requests.post(INGEST_SAVE_ENDPOINT, json={"markdown_content": markdown_content, "filename": filename}, timeout=API_TIMEOUT)
response.raise_for_status()
return response.json()
except Exception as e:
@ -259,8 +383,8 @@ def submit_feedback(query_id, node_id, score, comment=None):
def render_sidebar():
with st.sidebar:
st.title("🧠 mindnet")
st.caption("v2.5 | Healing Parser")
mode = st.radio("Modus", ["💬 Chat", "📝 Manueller Editor"], index=0)
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)
@ -286,7 +410,7 @@ def render_draft_editor(msg):
widget_body_key = f"{key_base}_widget_body"
data_body_key = f"{key_base}_data_body"
# --- 1. INIT STATE ---
# 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"
@ -294,23 +418,19 @@ def render_draft_editor(msg):
tags = meta.get("tags", [])
meta["tags_str"] = ", ".join(tags) if isinstance(tags, list) else str(tags)
# Persistent Data
st.session_state[data_meta_key] = meta
st.session_state[data_sugg_key] = []
st.session_state[data_body_key] = body.strip()
# Init Widgets Keys
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
# --- 2. RESURRECTION ---
# 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 ---
# CALLBACKS
def _sync_meta():
meta = st.session_state[data_meta_key]
meta["title"] = st.session_state.get(f"{key_base}_wdg_title", "")
@ -333,7 +453,7 @@ def render_draft_editor(msg):
st.session_state[widget_body_key] = new_text
st.session_state[data_body_key] = new_text
# --- UI LAYOUT ---
# UI LAYOUT
st.markdown(f'<div class="draft-box">', unsafe_allow_html=True)
st.markdown("### 📝 Entwurf bearbeiten")
@ -341,7 +461,6 @@ def render_draft_editor(msg):
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"])
@ -352,50 +471,34 @@ def render_draft_editor(msg):
tab_edit, tab_intel, tab_view = st.tabs(["✏️ Inhalt", "🧠 Intelligence", "👁️ Vorschau"])
# --- TAB 1: EDITOR ---
with tab_edit:
st.text_area(
"Body",
key=widget_body_key,
height=500,
on_change=_sync_body,
label_visibility="collapsed"
)
st.text_area("Body", key=widget_body_key, height=500, on_change=_sync_body, label_visibility="collapsed")
# --- TAB 2: INTELLIGENCE ---
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.")
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>
@ -403,16 +506,13 @@ def render_draft_editor(msg):
<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,))
# --- TAB 3: SAVE ---
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"),
@ -420,13 +520,10 @@ def render_draft_editor(msg):
"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()
if h1_match: final_meta["title"] = h1_match.group(1).strip()
final_doc = build_markdown_doc(final_meta, final_body)
@ -436,33 +533,26 @@ def render_draft_editor(msg):
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']}")
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):
for idx, msg in enumerate(st.session_state.messages):
with st.chat_message(msg["role"]):
@ -525,9 +615,68 @@ def render_manual_editor():
}
render_draft_editor(mock_msg)
def render_graph_explorer():
st.header("🕸️ Graph Explorer (WP-19)")
col_ctrl, col_graph = st.columns([1, 3])
with col_ctrl:
st.subheader("Fokus setzen")
search_term = st.text_input("Suche Notiz (Titel)", placeholder="z.B. Project Alpha")
selected_note_id = None
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("Wähle Notiz:", list(options.keys()))
selected_note_id = options[selected_title]
else:
st.warning("Keine Notiz gefunden.")
st.markdown("---")
st.markdown("**Legende:**")
st.markdown(f"🔴 **Blocker** (Risk/Block)")
st.markdown(f"🔵 **Konzept/Struktur**")
st.markdown(f"🟣 **Entscheidung**")
st.markdown(f"🤖 _Gestrichelt = Smart Edge (KI)_")
with col_graph:
if selected_note_id:
with st.spinner(f"Lade Graph für {selected_note_id}..."):
nodes, edges = graph_service.get_ego_graph(selected_note_id)
if not nodes:
st.error("Knoten konnte nicht geladen werden.")
else:
config = Config(
width=800,
height=600,
directed=True,
physics=True,
hierarchical=False,
nodeHighlightBehavior=True,
highlightColor="#F7A7A6",
collapsible=False
)
return_value = agraph(nodes=nodes, edges=edges, config=config)
if return_value:
st.info(f"Node geklickt: {return_value}")
else:
st.info("👈 Bitte wähle links eine Notiz aus, um den Graphen zu starten.")
# --- MAIN ---
mode, top_k, explain = render_sidebar()
if mode == "💬 Chat":
render_chat_interface(top_k, explain)
else:
render_manual_editor()
elif mode == "📝 Manueller Editor":
render_manual_editor()
elif mode == "🕸️ Graph Explorer":
render_graph_explorer()