angepasste UI
This commit is contained in:
parent
8639791914
commit
d2ee48555a
|
|
@ -14,6 +14,8 @@ load_dotenv()
|
||||||
API_BASE_URL = os.getenv("MINDNET_API_URL", "http://localhost:8002")
|
API_BASE_URL = os.getenv("MINDNET_API_URL", "http://localhost:8002")
|
||||||
CHAT_ENDPOINT = f"{API_BASE_URL}/chat"
|
CHAT_ENDPOINT = f"{API_BASE_URL}/chat"
|
||||||
FEEDBACK_ENDPOINT = f"{API_BASE_URL}/feedback"
|
FEEDBACK_ENDPOINT = f"{API_BASE_URL}/feedback"
|
||||||
|
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")
|
HISTORY_FILE = Path("data/logs/search_history.jsonl")
|
||||||
|
|
||||||
# Timeout Strategy
|
# Timeout Strategy
|
||||||
|
|
@ -52,10 +54,13 @@ st.markdown("""
|
||||||
font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif;
|
font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif;
|
||||||
}
|
}
|
||||||
|
|
||||||
.debug-info {
|
.suggestion-card {
|
||||||
font-size: 0.7rem;
|
border-left: 3px solid #1a73e8;
|
||||||
color: #888;
|
background-color: #ffffff;
|
||||||
margin-bottom: 5px;
|
padding: 10px;
|
||||||
|
margin-bottom: 8px;
|
||||||
|
border-radius: 4px;
|
||||||
|
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
""", unsafe_allow_html=True)
|
""", unsafe_allow_html=True)
|
||||||
|
|
@ -67,20 +72,14 @@ if "user_id" not in st.session_state: st.session_state.user_id = str(uuid.uuid4(
|
||||||
# --- HELPER FUNCTIONS ---
|
# --- HELPER FUNCTIONS ---
|
||||||
|
|
||||||
def normalize_meta_and_body(meta, body):
|
def normalize_meta_and_body(meta, body):
|
||||||
"""
|
"""Sanitizer: Stellt sicher, dass nur erlaubte Felder im Frontmatter bleiben."""
|
||||||
Sanitizer: Stellt sicher, dass nur erlaubte Felder im Frontmatter bleiben.
|
|
||||||
Alles andere wird in den Body verschoben (Repair-Strategie).
|
|
||||||
"""
|
|
||||||
ALLOWED_KEYS = {"title", "type", "status", "tags", "id", "created", "updated", "aliases", "lang"}
|
ALLOWED_KEYS = {"title", "type", "status", "tags", "id", "created", "updated", "aliases", "lang"}
|
||||||
|
|
||||||
clean_meta = {}
|
clean_meta = {}
|
||||||
extra_content = []
|
extra_content = []
|
||||||
|
|
||||||
# 1. Title/Titel Normalisierung
|
|
||||||
if "titel" in meta and "title" not in meta:
|
if "titel" in meta and "title" not in meta:
|
||||||
meta["title"] = meta.pop("titel")
|
meta["title"] = meta.pop("titel")
|
||||||
|
|
||||||
# 2. Tags Normalisierung (Synonyme)
|
|
||||||
tag_candidates = ["tags", "emotionale_keywords", "keywords", "schluesselwoerter"]
|
tag_candidates = ["tags", "emotionale_keywords", "keywords", "schluesselwoerter"]
|
||||||
all_tags = []
|
all_tags = []
|
||||||
for key in tag_candidates:
|
for key in tag_candidates:
|
||||||
|
|
@ -89,14 +88,12 @@ def normalize_meta_and_body(meta, body):
|
||||||
if isinstance(val, list): all_tags.extend(val)
|
if isinstance(val, list): all_tags.extend(val)
|
||||||
elif isinstance(val, str): all_tags.extend([t.strip() for t in val.split(",")])
|
elif isinstance(val, str): all_tags.extend([t.strip() for t in val.split(",")])
|
||||||
|
|
||||||
# 3. Filterung und Verschiebung
|
|
||||||
for key, val in meta.items():
|
for key, val in meta.items():
|
||||||
if key in ALLOWED_KEYS:
|
if key in ALLOWED_KEYS:
|
||||||
clean_meta[key] = val
|
clean_meta[key] = val
|
||||||
elif key in tag_candidates:
|
elif key in tag_candidates:
|
||||||
pass # Schon oben behandelt
|
pass
|
||||||
else:
|
else:
|
||||||
# Unerlaubtes Feld (z.B. 'situation') -> Ab in den Body!
|
|
||||||
if val and isinstance(val, str):
|
if val and isinstance(val, str):
|
||||||
header = key.replace("_", " ").title()
|
header = key.replace("_", " ").title()
|
||||||
extra_content.append(f"## {header}\n{val}\n")
|
extra_content.append(f"## {header}\n{val}\n")
|
||||||
|
|
@ -104,7 +101,6 @@ def normalize_meta_and_body(meta, body):
|
||||||
if all_tags:
|
if all_tags:
|
||||||
clean_meta["tags"] = list(set(all_tags))
|
clean_meta["tags"] = list(set(all_tags))
|
||||||
|
|
||||||
# 4. Body Zusammenbau
|
|
||||||
if extra_content:
|
if extra_content:
|
||||||
new_section = "\n".join(extra_content)
|
new_section = "\n".join(extra_content)
|
||||||
final_body = f"{new_section}\n{body}"
|
final_body = f"{new_section}\n{body}"
|
||||||
|
|
@ -114,18 +110,14 @@ def normalize_meta_and_body(meta, body):
|
||||||
return clean_meta, final_body
|
return clean_meta, final_body
|
||||||
|
|
||||||
def parse_markdown_draft(full_text):
|
def parse_markdown_draft(full_text):
|
||||||
"""
|
"""Robustes Parsing + Sanitization."""
|
||||||
Robustes Parsing + Sanitization.
|
|
||||||
"""
|
|
||||||
clean_text = full_text
|
clean_text = full_text
|
||||||
|
|
||||||
# Codeblock entfernen
|
|
||||||
pattern_block = r"```(?:markdown|md)?\s*(.*?)\s*```"
|
pattern_block = r"```(?:markdown|md)?\s*(.*?)\s*```"
|
||||||
match_block = re.search(pattern_block, full_text, re.DOTALL | re.IGNORECASE)
|
match_block = re.search(pattern_block, full_text, re.DOTALL | re.IGNORECASE)
|
||||||
if match_block:
|
if match_block:
|
||||||
clean_text = match_block.group(1).strip()
|
clean_text = match_block.group(1).strip()
|
||||||
|
|
||||||
# Frontmatter splitten
|
|
||||||
parts = re.split(r"^---+\s*$", clean_text, maxsplit=2, flags=re.MULTILINE)
|
parts = re.split(r"^---+\s*$", clean_text, maxsplit=2, flags=re.MULTILINE)
|
||||||
|
|
||||||
meta = {}
|
meta = {}
|
||||||
|
|
@ -152,7 +144,6 @@ def build_markdown_doc(meta, body):
|
||||||
|
|
||||||
meta["updated"] = datetime.now().strftime("%Y-%m-%d")
|
meta["updated"] = datetime.now().strftime("%Y-%m-%d")
|
||||||
|
|
||||||
# Sortierung für UX
|
|
||||||
ordered_meta = {}
|
ordered_meta = {}
|
||||||
prio_keys = ["id", "type", "title", "status", "tags"]
|
prio_keys = ["id", "type", "title", "status", "tags"]
|
||||||
for k in prio_keys:
|
for k in prio_keys:
|
||||||
|
|
@ -183,6 +174,8 @@ def load_history_from_logs(limit=10):
|
||||||
except: pass
|
except: pass
|
||||||
return queries
|
return queries
|
||||||
|
|
||||||
|
# --- API CLIENT ---
|
||||||
|
|
||||||
def send_chat_message(message: str, top_k: int, explain: bool):
|
def send_chat_message(message: str, top_k: int, explain: bool):
|
||||||
try:
|
try:
|
||||||
response = requests.post(
|
response = requests.post(
|
||||||
|
|
@ -195,6 +188,32 @@ def send_chat_message(message: str, top_k: int, explain: bool):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return {"error": str(e)}
|
return {"error": str(e)}
|
||||||
|
|
||||||
|
def analyze_draft_text(text: str, n_type: str):
|
||||||
|
"""Ruft den neuen Intelligence-Service (WP-11) auf."""
|
||||||
|
try:
|
||||||
|
response = requests.post(
|
||||||
|
INGEST_ANALYZE_ENDPOINT,
|
||||||
|
json={"text": text, "type": n_type},
|
||||||
|
timeout=10
|
||||||
|
)
|
||||||
|
response.raise_for_status()
|
||||||
|
return response.json()
|
||||||
|
except Exception as e:
|
||||||
|
return {"error": str(e)}
|
||||||
|
|
||||||
|
def save_draft_to_vault(markdown_content: str, filename: str = None):
|
||||||
|
"""Ruft den neuen Persistence-Service (WP-11) auf."""
|
||||||
|
try:
|
||||||
|
response = requests.post(
|
||||||
|
INGEST_SAVE_ENDPOINT,
|
||||||
|
json={"markdown_content": markdown_content, "filename": filename},
|
||||||
|
timeout=30 # Indizierung kann dauern
|
||||||
|
)
|
||||||
|
response.raise_for_status()
|
||||||
|
return response.json()
|
||||||
|
except Exception as e:
|
||||||
|
return {"error": str(e)}
|
||||||
|
|
||||||
def submit_feedback(query_id, node_id, score, comment=None):
|
def submit_feedback(query_id, node_id, score, comment=None):
|
||||||
try:
|
try:
|
||||||
requests.post(FEEDBACK_ENDPOINT, json={"query_id": query_id, "node_id": node_id, "score": score, "comment": comment}, timeout=2)
|
requests.post(FEEDBACK_ENDPOINT, json={"query_id": query_id, "node_id": node_id, "score": score, "comment": comment}, timeout=2)
|
||||||
|
|
@ -230,15 +249,14 @@ def render_draft_editor(msg):
|
||||||
|
|
||||||
st.session_state[f"{key_base}_type"] = meta.get("type", "default")
|
st.session_state[f"{key_base}_type"] = meta.get("type", "default")
|
||||||
st.session_state[f"{key_base}_title"] = meta.get("title", "")
|
st.session_state[f"{key_base}_title"] = meta.get("title", "")
|
||||||
|
|
||||||
tags_raw = meta.get("tags", [])
|
tags_raw = meta.get("tags", [])
|
||||||
st.session_state[f"{key_base}_tags"] = ", ".join(tags_raw) if isinstance(tags_raw, list) else str(tags_raw)
|
st.session_state[f"{key_base}_tags"] = ", ".join(tags_raw) if isinstance(tags_raw, list) else str(tags_raw)
|
||||||
|
|
||||||
st.session_state[f"{key_base}_body"] = body.strip()
|
st.session_state[f"{key_base}_body"] = body.strip()
|
||||||
st.session_state[f"{key_base}_meta"] = meta
|
st.session_state[f"{key_base}_meta"] = meta
|
||||||
|
st.session_state[f"{key_base}_suggestions"] = []
|
||||||
st.session_state[f"{key_base}_init"] = True
|
st.session_state[f"{key_base}_init"] = True
|
||||||
|
|
||||||
# 2. UI
|
# 2. UI Layout
|
||||||
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")
|
||||||
|
|
||||||
|
|
@ -254,11 +272,21 @@ def render_draft_editor(msg):
|
||||||
|
|
||||||
new_tags = st.text_input("Tags (kommagetrennt)", value=st.session_state.get(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")
|
||||||
|
|
||||||
# Tabs
|
# Tabs (Jetzt mit "Intelligence")
|
||||||
tab_edit, tab_view = st.tabs(["✏️ Inhalt", "👁️ Vorschau"])
|
tab_edit, tab_intel, tab_view = st.tabs(["✏️ Inhalt", "🧠 Intelligence", "👁️ Vorschau"])
|
||||||
|
|
||||||
|
# Live Reassembly für alle Tabs
|
||||||
|
final_tags_list = [t.strip() for t in new_tags.split(",") if t.strip()]
|
||||||
|
final_meta = {
|
||||||
|
"id": "generated_on_save",
|
||||||
|
"type": new_type,
|
||||||
|
"title": new_title,
|
||||||
|
"status": "draft",
|
||||||
|
"tags": final_tags_list
|
||||||
|
}
|
||||||
|
|
||||||
|
# --- TAB 1: EDITOR ---
|
||||||
with tab_edit:
|
with tab_edit:
|
||||||
st.caption("Bearbeite hier den Inhalt. Metadaten (oben) werden automatisch hinzugefügt.")
|
|
||||||
new_body = st.text_area(
|
new_body = st.text_area(
|
||||||
"Body",
|
"Body",
|
||||||
value=st.session_state.get(f"{key_base}_body", ""),
|
value=st.session_state.get(f"{key_base}_body", ""),
|
||||||
|
|
@ -267,19 +295,43 @@ def render_draft_editor(msg):
|
||||||
label_visibility="collapsed"
|
label_visibility="collapsed"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Reassembly
|
# --- TAB 2: INTELLIGENCE (WP-11 Features) ---
|
||||||
final_tags_list = [t.strip() for t in new_tags.split(",") if t.strip()]
|
with tab_intel:
|
||||||
final_meta = st.session_state.get(f"{key_base}_meta", {}).copy()
|
st.info("Klicke auf 'Analysieren', um Verknüpfungen zu finden.")
|
||||||
final_meta.update({
|
|
||||||
"id": "generated_on_save",
|
if st.button("🔍 Draft Analysieren", key=f"{key_base}_analyze"):
|
||||||
"type": new_type,
|
with st.spinner("Analysiere Text und suche Verknüpfungen..."):
|
||||||
"title": new_title,
|
analysis = analyze_draft_text(new_body, new_type)
|
||||||
"status": "draft",
|
if "error" in analysis:
|
||||||
"tags": final_tags_list
|
st.error(f"Fehler: {analysis['error']}")
|
||||||
})
|
else:
|
||||||
|
st.session_state[f"{key_base}_suggestions"] = analysis.get("suggestions", [])
|
||||||
final_doc = build_markdown_doc(final_meta, new_body)
|
if not analysis.get("suggestions"):
|
||||||
|
st.warning("Keine offensichtlichen Verknüpfungen gefunden.")
|
||||||
|
|
||||||
|
suggestions = st.session_state.get(f"{key_base}_suggestions", [])
|
||||||
|
if suggestions:
|
||||||
|
st.markdown(f"**{len(suggestions)} Vorschläge gefunden:**")
|
||||||
|
for idx, sugg in enumerate(suggestions):
|
||||||
|
with st.container():
|
||||||
|
st.markdown(f"""
|
||||||
|
<div class="suggestion-card">
|
||||||
|
<b>{sugg['target_title']}</b> <small>({sugg['type']})</small><br>
|
||||||
|
<i>Grund: {sugg.get('reason', 'N/A')}</i><br>
|
||||||
|
<code>{sugg['suggested_markdown']}</code>
|
||||||
|
</div>
|
||||||
|
""", unsafe_allow_html=True)
|
||||||
|
|
||||||
|
if st.button("➕ Einfügen", key=f"{key_base}_add_{idx}"):
|
||||||
|
# Append to body
|
||||||
|
current_body = st.session_state[f"{key_base}_body"]
|
||||||
|
updated_body = f"{current_body}\n\n{sugg['suggested_markdown']}"
|
||||||
|
st.session_state[f"{key_base}_body"] = updated_body
|
||||||
|
st.toast(f"Link zu '{sugg['target_title']}' eingefügt!")
|
||||||
|
st.rerun()
|
||||||
|
|
||||||
|
# --- TAB 3: PREVIEW ---
|
||||||
|
final_doc = build_markdown_doc(final_meta, st.session_state.get(f"{key_base}_body", ""))
|
||||||
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)
|
st.markdown(final_doc)
|
||||||
|
|
@ -287,11 +339,23 @@ def render_draft_editor(msg):
|
||||||
|
|
||||||
st.markdown("---")
|
st.markdown("---")
|
||||||
|
|
||||||
# Actions
|
# Actions (SAVE & EXPORT)
|
||||||
b1, b2 = st.columns([1, 1])
|
b1, b2 = st.columns([1, 1])
|
||||||
with b1:
|
with b1:
|
||||||
fname = f"{datetime.now().strftime('%Y%m%d')}-{new_type}.md"
|
# Echter Save Button
|
||||||
st.download_button("💾 Download .md", data=final_doc, file_name=fname, mime="text/markdown")
|
if st.button("💾 Speichern & Indizieren", type="primary", key=f"{key_base}_save"):
|
||||||
|
with st.spinner("Speichere im Vault..."):
|
||||||
|
# Generiere Filename
|
||||||
|
safe_title = re.sub(r'[^a-zA-Z0-9]', '-', new_title).lower()[:30]
|
||||||
|
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 beim Speichern: {result['error']}")
|
||||||
|
else:
|
||||||
|
st.success(f"Gespeichert: {result.get('file_path')}")
|
||||||
|
st.balloons()
|
||||||
with b2:
|
with b2:
|
||||||
if st.button("📋 Code anzeigen", key=f"{key_base}_btn_copy"):
|
if st.button("📋 Code anzeigen", key=f"{key_base}_btn_copy"):
|
||||||
st.code(final_doc, language="markdown")
|
st.code(final_doc, language="markdown")
|
||||||
|
|
@ -303,13 +367,12 @@ def render_chat_interface(top_k, explain):
|
||||||
for idx, msg in enumerate(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":
|
||||||
# Meta
|
# Header
|
||||||
intent = msg.get("intent", "UNKNOWN")
|
intent = msg.get("intent", "UNKNOWN")
|
||||||
src = msg.get("intent_source", "?")
|
src = msg.get("intent_source", "?")
|
||||||
icon = {"EMPATHY":"❤️", "DECISION":"⚖️", "CODING":"💻", "FACT":"📚", "INTERVIEW":"📝"}.get(intent, "🧠")
|
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)
|
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)
|
||||||
|
|
||||||
# Debugging (Always visible for safety)
|
|
||||||
with st.expander("🐞 Debug Raw Payload", expanded=False):
|
with st.expander("🐞 Debug Raw Payload", expanded=False):
|
||||||
st.json(msg)
|
st.json(msg)
|
||||||
|
|
||||||
|
|
@ -364,9 +427,17 @@ def render_manual_editor():
|
||||||
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")
|
||||||
body = st.text_area("Inhalt", height=400, placeholder="# Titel\n\nText...")
|
body = st.text_area("Inhalt", height=400, placeholder="# Titel\n\nText...")
|
||||||
if st.button("Code anzeigen"):
|
|
||||||
|
if st.button("Speichern (Via API)"):
|
||||||
meta = {"type": n_type, "status": "draft", "tags": [t.strip() for t in tags.split(",")]}
|
meta = {"type": n_type, "status": "draft", "tags": [t.strip() for t in tags.split(",")]}
|
||||||
st.code(build_markdown_doc(meta, body), language="markdown")
|
doc = build_markdown_doc(meta, body)
|
||||||
|
|
||||||
|
# Test Call
|
||||||
|
res = save_draft_to_vault(doc, filename=f"manual-{uuid.uuid4().hex[:6]}.md")
|
||||||
|
if "error" in res:
|
||||||
|
st.error(res["error"])
|
||||||
|
else:
|
||||||
|
st.success(f"Gespeichert: {res.get('file_path')}")
|
||||||
|
|
||||||
mode, top_k, explain = render_sidebar()
|
mode, top_k, explain = render_sidebar()
|
||||||
if mode == "💬 Chat":
|
if mode == "💬 Chat":
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user