ui und prompt

This commit is contained in:
Lars 2025-12-10 18:13:35 +01:00
parent 5c0a36c9ea
commit 67d865d373
2 changed files with 127 additions and 50 deletions

View File

@ -66,46 +66,101 @@ 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):
"""
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"}
clean_meta = {}
extra_content = []
# 1. Title/Titel Normalisierung
if "titel" in meta and "title" not in meta:
meta["title"] = meta.pop("titel")
# 2. Tags Normalisierung (Synonyme)
tag_candidates = ["tags", "emotionale_keywords", "keywords", "schluesselwoerter"]
all_tags = []
for key in tag_candidates:
if key in meta:
val = meta[key]
if isinstance(val, list): all_tags.extend(val)
elif isinstance(val, str): all_tags.extend([t.strip() for t in val.split(",")])
# 3. Filterung und Verschiebung
for key, val in meta.items():
if key in ALLOWED_KEYS:
clean_meta[key] = val
elif key in tag_candidates:
pass # Schon oben behandelt
else:
# Unerlaubtes Feld (z.B. 'situation') -> Ab in den Body!
if val and isinstance(val, str):
header = key.replace("_", " ").title()
extra_content.append(f"## {header}\n{val}\n")
if all_tags:
clean_meta["tags"] = list(set(all_tags))
# 4. Body Zusammenbau
if extra_content:
new_section = "\n".join(extra_content)
final_body = f"{new_section}\n{body}"
else:
final_body = body
return clean_meta, final_body
def parse_markdown_draft(full_text): def parse_markdown_draft(full_text):
""" """
Versucht extrem tolerant, Frontmatter und Body zu trennen. Robustes Parsing + Sanitization.
""" """
clean_text = full_text clean_text = full_text
# 1. Versuch: Codeblock isolieren # 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()
# 2. Versuch: Frontmatter finden (--- YAML ---) # Frontmatter splitten
# Verbesserter Regex: Sucht nach dem ersten Vorkommen von --- am Zeilenanfang parts = re.split(r"^---+\s*$", clean_text, maxsplit=2, flags=re.MULTILINE)
pattern_fm = r"(-{3,})\s*(.*?)\s*\1\s*(.*)"
match_fm = re.search(pattern_fm, clean_text, re.DOTALL)
meta = {} meta = {}
body = clean_text body = clean_text
if match_fm: if len(parts) >= 3:
yaml_str = match_fm.group(2) yaml_str = parts[1]
body_content = match_fm.group(3) body_candidate = parts[2]
try: try:
parsed = yaml.safe_load(yaml_str) parsed = yaml.safe_load(yaml_str)
if isinstance(parsed, dict): if isinstance(parsed, dict):
meta = parsed meta = parsed
body = body_content.strip() body = body_candidate.strip()
except Exception: except Exception:
pass # YAML kaputt -> alles als Body behandeln pass
return meta, body return normalize_meta_and_body(meta, body)
def build_markdown_doc(meta, body): def build_markdown_doc(meta, body):
"""Baut das finale Dokument zusammen."""
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]}" safe_title = re.sub(r'[^a-zA-Z0-9]', '-', meta.get('title', 'note')).lower()[:30]
meta["id"] = f"{datetime.now().strftime('%Y%m%d')}-{safe_title}-{uuid.uuid4().hex[:4]}"
meta["updated"] = datetime.now().strftime("%Y-%m-%d") meta["updated"] = datetime.now().strftime("%Y-%m-%d")
# Sortierung für UX
ordered_meta = {}
prio_keys = ["id", "type", "title", "status", "tags"]
for k in prio_keys:
if k in meta: ordered_meta[k] = meta.pop(k)
ordered_meta.update(meta)
try: try:
yaml_str = yaml.dump(meta, default_flow_style=None, sort_keys=False, allow_unicode=True).strip() yaml_str = yaml.dump(ordered_meta, default_flow_style=None, sort_keys=False, allow_unicode=True).strip()
except: except:
yaml_str = "error: generating_yaml" yaml_str = "error: generating_yaml"
@ -151,7 +206,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("DEBUG MODE ACTIVATED") st.caption("v2.3.2 | WP-10 UI")
mode = st.radio("Modus", ["💬 Chat", "📝 Manueller Editor"], index=0) mode = st.radio("Modus", ["💬 Chat", "📝 Manueller Editor"], index=0)
st.divider() st.divider()
st.subheader("⚙️ Settings") st.subheader("⚙️ Settings")
@ -169,43 +224,60 @@ def render_draft_editor(msg):
qid = msg.get('query_id', str(uuid.uuid4())) qid = msg.get('query_id', str(uuid.uuid4()))
key_base = f"draft_{qid}" key_base = f"draft_{qid}"
# 1. Init
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"])
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", "")
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}_init"] = True st.session_state[f"{key_base}_init"] = True
# 2. UI
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")
# Metadata Controls # Metadata
c1, c2 = st.columns([1, 2]) c1, c2 = st.columns([2, 1])
with c1: with c1:
new_title = st.text_input("Titel", value=st.session_state.get(f"{key_base}_title", ""), key=f"{key_base}_inp_title")
with c2:
known_types = ["concept", "project", "decision", "experience", "journal", "person", "value", "goal", "principle", "default"] known_types = ["concept", "project", "decision", "experience", "journal", "person", "value", "goal", "principle", "default"]
curr_type = st.session_state.get(f"{key_base}_type", "default") curr_type = st.session_state.get(f"{key_base}_type", "default")
if curr_type not in known_types: known_types.append(curr_type) if curr_type not in known_types: known_types.append(curr_type)
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:
new_tags = st.text_input("Tags", value=st.session_state.get(f"{key_base}_tags", ""), key=f"{key_base}_inp_tags")
# Editor / Preview Tabs new_tags = st.text_input("Tags (kommagetrennt)", value=st.session_state.get(f"{key_base}_tags", ""), key=f"{key_base}_inp_tags")
tab_edit, tab_view = st.tabs(["✏️ Editor", "👁️ Vorschau"])
# Tabs
tab_edit, tab_view = st.tabs(["✏️ Inhalt", "👁️ Vorschau"])
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(
"Inhalt", "Body",
value=st.session_state.get(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"
) )
# Live Reassembly # Reassembly
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 = {"id": "generated_on_save", "type": new_type, "status": "draft", "tags": final_tags_list} final_meta = st.session_state.get(f"{key_base}_meta", {}).copy()
final_meta.update({
"id": "generated_on_save",
"type": new_type,
"title": new_title,
"status": "draft",
"tags": final_tags_list
})
final_doc = build_markdown_doc(final_meta, new_body) final_doc = build_markdown_doc(final_meta, new_body)
with tab_view: with tab_view:
@ -218,42 +290,42 @@ def render_draft_editor(msg):
# Actions # Actions
b1, b2 = st.columns([1, 1]) b1, b2 = st.columns([1, 1])
with b1: with b1:
st.download_button("💾 Download .md", data=final_doc, file_name=f"draft_{new_type}.md", mime="text/markdown") fname = f"{datetime.now().strftime('%Y%m%d')}-{new_type}.md"
st.download_button("💾 Download .md", data=final_doc, file_name=fname, mime="text/markdown")
with b2: with b2:
if st.button("📋 Code Copy", 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")
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 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 Info # Meta
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)
# --- WICHTIG: DEBUGGING JETZT GANZ OBEN --- # Debugging (Always visible for safety)
with st.expander("🐞 Debug Raw Payload", expanded=False): with st.expander("🐞 Debug Raw Payload", expanded=False):
st.text("Hier siehst du, was das Backend wirklich geschickt hat:")
st.json(msg) st.json(msg)
# --- CONTENT LOGIC --- # Logic
if intent == "INTERVIEW": if intent == "INTERVIEW":
render_draft_editor(msg) render_draft_editor(msg)
else: else:
st.markdown(msg["content"]) st.markdown(msg["content"])
# Sources & Feedback # 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"]:
with st.expander(f"📄 {hit.get('note_id', '?')} ({hit.get('total_score', 0):.2f})"): with st.expander(f"📄 {hit.get('note_id', '?')} ({hit.get('total_score', 0):.2f})"):
st.markdown(f"_{hit.get('source', {}).get('text', '')[:300]}..._") st.markdown(f"_{hit.get('source', {}).get('text', '')[:300]}..._")
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.get("query_id"), nid=hit.get('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)
@ -265,7 +337,6 @@ def render_chat_interface(top_k, explain):
else: else:
st.markdown(msg["content"]) st.markdown(msg["content"])
# Input Logic
if prompt := st.chat_input("Frage Mindnet..."): if prompt := st.chat_input("Frage Mindnet..."):
st.session_state.messages.append({"role": "user", "content": prompt}) st.session_state.messages.append({"role": "user", "content": prompt})
st.rerun() st.rerun()

View File

@ -94,41 +94,47 @@ technical_template: |
- Kurze Erklärung des Ansatzes. - Kurze Erklärung des Ansatzes.
- Markdown Code-Block (Copy-Paste fertig). - Markdown Code-Block (Copy-Paste fertig).
- Wichtige Edge-Cases. - Wichtige Edge-Cases.
# config/prompts.yaml
# config/prompts.yaml
# --------------------------------------------------------- # ---------------------------------------------------------
# 5. INTERVIEW: Der "One-Shot Extractor" (Performance Mode) # 5. INTERVIEW: Der "One-Shot Extractor" (Performance Mode)
# --------------------------------------------------------- # ---------------------------------------------------------
interview_template: | interview_template: |
TASK: TASK:
Erstelle einen Markdown-Entwurf für eine Notiz vom Typ '{target_type}'. Erstelle einen Markdown-Entwurf für eine Notiz vom Typ '{target_type}'.
SCHEMA (Pflichtfelder): SCHEMA (Inhaltliche Pflichtfelder für den Body):
{schema_fields} {schema_fields}
USER INPUT: USER INPUT:
"{query}" "{query}"
ANWEISUNG: ANWEISUNG:
1. Extrahiere ALLE Informationen aus dem User Input, die du finden kannst. 1. Extrahiere Informationen aus dem Input.
2. Mappe sie auf die Schema-Felder. 2. Generiere validen Markdown.
3. Für Felder, die im Input FEHLEN, schreibe den Platzhalter "[TODO: Bitte ergänzen]".
4. Generiere SOFORT den Markdown-Codeblock. Stelle KEINE Rückfragen. OUTPUT REGELN (STRIKT BEACHTEN):
A. FRONTMATTER (YAML):
- Darf NUR folgende Felder enthalten: [type, status, title, tags].
- Schreibe KEINE inhaltlichen Sätze (wie 'Situation', 'Ziel') in das YAML!
- Setze 'status: draft'.
B. BODY (Markdown):
- Nutze für jedes Schema-Feld eine Markdown-Überschrift (## Feldname).
- Schreibe den Inhalt DARUNTER.
- Nutze "[TODO: Ergänzen]", wenn Infos fehlen.
HINWEIS ZUM TYP: HINWEIS ZUM TYP:
{schema_hint} {schema_hint}
OUTPUT FORMAT: OUTPUT FORMAT BEISPIEL:
```markdown ```markdown
--- ---
type: {target_type} type: {target_type}
status: draft status: draft
... title: ...
tags: [...]
--- ---
# Titel (oder [TODO]) # Titel der Notiz
## Feldname ## Erstes Schema Feld
Inhalt... Der Inhalt hier...