from datetime import datetime, timezone import streamlit as st from chatbot import chat from memory import delete_all_memories, get_all_memories st.set_page_config(page_title="MemoryMind", page_icon="๐Ÿง ", layout="wide") # --- session state --- if "chat_history" not in st.session_state: st.session_state.chat_history = [] if "last_used_ids" not in st.session_state: st.session_state.last_used_ids = [] if "confirm_reset" not in st.session_state: st.session_state.confirm_reset = False def _relative_time(iso_str: str) -> str: try: dt = datetime.fromisoformat(iso_str) seconds = int((datetime.now(timezone.utc) - dt).total_seconds()) if seconds < 60: return "just now" if seconds < 3600: return f"{seconds // 60}m ago" if seconds < 86400: return f"{seconds // 3600}h ago" return f"{seconds // 86400}d ago" except Exception: return "" # --- sidebar --- with st.sidebar: st.title("๐Ÿง  MemoryMind") st.divider() username = st.text_input("Who are you?", placeholder="Enter your name...", key="username_input") user_id = username.strip().lower().replace(" ", "_") or "guest" st.divider() memories = get_all_memories(user_id=user_id) st.caption(f"{len(memories)} fact{'s' if len(memories) != 1 else ''} stored for **{username or 'guest'}**") st.divider() if not memories: st.caption("No memories yet. Start chatting!") else: for m in memories: is_used = m["id"] in st.session_state.last_used_ids meta = m["metadata"] # staleness โ€” gray out if not used in 7+ days try: last_used_dt = datetime.fromisoformat(meta.get("last_used", meta.get("timestamp", ""))) days_stale = (datetime.now(timezone.utc) - last_used_dt).days is_stale = days_stale >= 7 except Exception: is_stale = False border_color = "#4caf50" if is_used else ("#555" if is_stale else "#444") bg_color = "#1a2e1a" if is_used else "#1a1a1a" text_color = "#888" if is_stale else "#e0e0e0" checkmark = " โœ“" if is_used else "" confidence = meta.get("confidence", 1) confidence_stars = "โ˜…" * min(confidence, 5) st.markdown( f"""
{m['fact']}{checkmark}
{meta.get('type', 'โ€”')}  ยท {_relative_time(meta.get('timestamp', ''))}  ยท used {meta.get('usage_count', 0)}ร—  ยท {confidence_stars}
""", unsafe_allow_html=True, ) st.divider() if st.session_state.confirm_reset: st.warning(f"Wipe all memories for **{username or 'guest'}**?") col1, col2 = st.columns(2) with col1: if st.button("Yes, reset", type="primary", use_container_width=True): delete_all_memories(user_id=user_id) st.session_state.chat_history = [] st.session_state.last_used_ids = [] st.session_state.confirm_reset = False st.rerun() with col2: if st.button("Cancel", use_container_width=True): st.session_state.confirm_reset = False st.rerun() else: if st.button("๐Ÿ—‘๏ธ Reset Memory", use_container_width=True): st.session_state.confirm_reset = True st.rerun() # --- main chat area --- st.title("MemoryMind") st.caption("The chatbot that actually remembers you.") for message in st.session_state.chat_history: with st.chat_message(message["role"]): st.write(message["content"]) if prompt := st.chat_input("Say something..."): with st.chat_message("user"): st.write(prompt) with st.chat_message("assistant"): with st.spinner("Thinking..."): reply, used_ids = chat(prompt, st.session_state.chat_history, user_id=user_id) st.write(reply) if used_ids: st.caption(f"๐Ÿ’ก {len(used_ids)} memor{'ies' if len(used_ids) != 1 else 'y'} used") st.session_state.chat_history.append({"role": "user", "content": prompt}) st.session_state.chat_history.append({"role": "assistant", "content": reply}) st.session_state.last_used_ids = used_ids st.rerun()