Vanshcc commited on
Commit
5901fb0
·
verified ·
1 Parent(s): c2473a9

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +46 -0
  2. memory.py +75 -0
app.py CHANGED
@@ -364,6 +364,52 @@ def render_sidebar():
364
  st.session_state.memory = None
365
  st.success("Disconnected!")
366
  st.rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
367
 
368
 
369
  def initialize_chatbot(custom_db_params=None, api_key=None, model=None) -> bool:
 
364
  st.session_state.memory = None
365
  st.success("Disconnected!")
366
  st.rerun()
367
+
368
+ st.divider()
369
+
370
+ # Chat History Section
371
+ if st.session_state.memory:
372
+ st.subheader("🕰️ Chat History")
373
+ sessions = st.session_state.memory.get_user_sessions()
374
+
375
+ if not sessions:
376
+ st.caption("No previous chats found.")
377
+ else:
378
+ for session in sessions:
379
+ # Highlight current session
380
+ is_current = session["id"] == st.session_state.session_id
381
+ icon = "🟢" if is_current else "💬"
382
+
383
+ if st.button(
384
+ f"{icon} {session['title']}",
385
+ key=f"hist_{session['id']}",
386
+ use_container_width=True,
387
+ type="secondary" if not is_current else "primary"
388
+ ):
389
+ if not is_current:
390
+ # Load selected session
391
+ st.session_state.session_id = session["id"]
392
+ st.session_state.memory.session_id = session["id"]
393
+ st.session_state.memory.messages = [] # Clear current state local cache
394
+
395
+ # Load from DB
396
+ msgs = st.session_state.memory.load_session(session["id"])
397
+ st.session_state.messages = msgs
398
+
399
+ # Re-populate memory object messages list for context
400
+ # (We need to convert dicts back to ChatMessage objects implicitly or just rely on reload)
401
+ # Actually, we should probably re-init the memory to be safe or manually populate
402
+ # Let's manually populate to keep the connection valid
403
+ from memory import ChatMessage
404
+ st.session_state.memory.messages = [
405
+ ChatMessage(
406
+ role=m['role'],
407
+ content=m['content'],
408
+ metadata=m.get('metadata')
409
+ ) for m in msgs
410
+ ]
411
+
412
+ st.rerun()
413
 
414
 
415
  def initialize_chatbot(custom_db_params=None, api_key=None, model=None) -> bool:
memory.py CHANGED
@@ -360,6 +360,81 @@ class ChatMemory:
360
  except Exception as e:
361
  logger.warning(f"Failed to clear user history from DB: {e}")
362
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
363
 
364
  class ConversationSummaryMemory:
365
  """
 
360
  except Exception as e:
361
  logger.warning(f"Failed to clear user history from DB: {e}")
362
 
363
+ def get_user_sessions(self) -> List[Dict[str, Any]]:
364
+ """Get a list of all chat sessions for the current user."""
365
+ if not self.db:
366
+ return []
367
+
368
+ try:
369
+ # Group by session_id and get the first message time + preview
370
+ # Note: This query needs to be compatible with supported DBs
371
+ query = """
372
+ SELECT session_id, MIN(created_at) as created_at,
373
+ (SELECT content FROM _chatbot_memory m2
374
+ WHERE m2.session_id = m1.session_id
375
+ AND role = 'user'
376
+ ORDER BY id ASC LIMIT 1) as title
377
+ FROM _chatbot_memory m1
378
+ WHERE user_id = :user_id
379
+ GROUP BY session_id
380
+ ORDER BY created_at DESC
381
+ """
382
+ rows = self.db.execute_query(query, {"user_id": self.user_id})
383
+
384
+ sessions = []
385
+ for row in rows:
386
+ title = row.get("title") or "New Chat"
387
+ if len(title) > 30:
388
+ title = title[:30] + "..."
389
+
390
+ sessions.append({
391
+ "id": row.get("session_id"),
392
+ "created_at": row.get("created_at"),
393
+ "title": title
394
+ })
395
+ return sessions
396
+ except Exception as e:
397
+ logger.warning(f"Failed to fetch user sessions: {e}")
398
+ return []
399
+
400
+ def load_session(self, session_id: str) -> List[Dict]:
401
+ """Load detailed messages for a specific session."""
402
+ if not self.db:
403
+ return []
404
+
405
+ try:
406
+ query = """
407
+ SELECT role, content, metadata
408
+ FROM _chatbot_memory
409
+ WHERE session_id = :session_id AND user_id = :user_id
410
+ ORDER BY id ASC
411
+ """
412
+ rows = self.db.execute_query(query, {
413
+ "session_id": session_id,
414
+ "user_id": self.user_id
415
+ })
416
+
417
+ messages = []
418
+ for row in rows:
419
+ meta = row.get("metadata")
420
+ if isinstance(meta, str):
421
+ try:
422
+ meta = json.loads(meta)
423
+ except:
424
+ meta = {}
425
+ elif meta is None:
426
+ meta = {}
427
+
428
+ messages.append({
429
+ "role": row.get("role"),
430
+ "content": row.get("content"),
431
+ "metadata": meta
432
+ })
433
+ return messages
434
+ except Exception as e:
435
+ logger.warning(f"Failed to load session {session_id}: {e}")
436
+ return []
437
+
438
 
439
  class ConversationSummaryMemory:
440
  """