From b1081e8cc3f8f9dbfed3226d9c85034f377a6939 Mon Sep 17 00:00:00 2001 From: Floke Date: Mon, 2 Mar 2026 19:32:07 +0000 Subject: [PATCH] [31388f42] Feat: Persist drafts and enhance UI warnings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit introduces two key improvements to the Lead Engine: 1. **Persistent Email Drafts:** - Adds a new function to . - Modifies to save generated email replies directly to the column in the database, ensuring they persist across sessions. - Removes the previous session-based state for drafts. 2. **Enhanced UI Visibility:** - Adds a warning icon (⚠️) directly to the lead expander's title if a lead is flagged as low-quality, making it easier to spot. --- lead-engine/app.py | 30 +++++++++++++++++------------- lead-engine/db.py | 8 ++++++++ 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/lead-engine/app.py b/lead-engine/app.py index 5927b2e2..9a1836ca 100644 --- a/lead-engine/app.py +++ b/lead-engine/app.py @@ -1,6 +1,6 @@ import streamlit as st import pandas as pd -from db import get_leads, init_db, reset_lead +from db import get_leads, init_db, reset_lead, update_lead_draft import json import re import os @@ -149,17 +149,19 @@ if not df.empty: # --- DYNAMIC TITLE --- source_icon = "🌐" if row.get('source') == 'Website-Formular' else "🤝" - title = f"{source_icon} {row.get('source', 'Lead')} | {date_str} | {row['company_name']}" + status_icon = "✅" if row.get('status') == 'synced' else "🆕" + + meta = {} + if row.get('lead_metadata'): + try: meta = json.loads(row['lead_metadata']) + except: pass + + quality_icon = "⚠️ " if meta.get('is_low_quality') else "" + + title = f"{quality_icon}{status_icon} {source_icon} {row.get('source', 'Lead')} | {date_str} | {row['company_name']}" with st.expander(title): - # Metadata Parsing - meta = {} - if row.get('lead_metadata'): - try: meta = json.loads(row['lead_metadata']) - except: pass - - # --- TOP SECTION: QUALITY WARNING --- - # Now directly checks the metadata from DB, which is more reliable + # The full warning message is still shown inside for clarity if meta.get('is_low_quality'): st.warning("⚠️ **Low Quality Lead detected** (Free-mail provider or missing company name). Please verify manually.") @@ -238,10 +240,12 @@ if not df.empty: with st.spinner("Writing email..."): ce_data = enrichment.get('ce_data', {}) draft = generate_email_draft(row.to_dict(), ce_data) - st.session_state[f"draft_{row['id']}"] = draft + update_lead_draft(row['id'], draft) # Save to DB + st.rerun() # Rerun to display the new draft from DB - if f"draft_{row['id']}" in st.session_state: - st.text_area("Email Entwurf", value=st.session_state[f"draft_{row['id']}"], height=400) + # Always display the draft from the database if it exists + if row.get('response_draft'): + st.text_area("Email Entwurf", value=row['response_draft'], height=400) st.button("📋 Copy to Clipboard", key=f"copy_{row['id']}", on_click=lambda: st.write("Copy functionality simulated")) else: st.info("Sync with Company Explorer first to generate a response.") diff --git a/lead-engine/db.py b/lead-engine/db.py index 5e07f330..2acec1bd 100644 --- a/lead-engine/db.py +++ b/lead-engine/db.py @@ -127,6 +127,14 @@ def update_lead_status(lead_id, status, response_draft=None): conn.commit() conn.close() +def update_lead_draft(lead_id, draft_text): + """Saves a generated email draft to the database.""" + conn = sqlite3.connect(DB_PATH) + c = conn.cursor() + c.execute('UPDATE leads SET response_draft = ? WHERE id = ?', (draft_text, lead_id)) + conn.commit() + conn.close() + def reset_lead(lead_id): """Resets a lead to 'new' status and clears enrichment data.""" conn = sqlite3.connect(DB_PATH)