File size: 5,306 Bytes
e3af942 453a4d2 e3af942 453a4d2 841713f 453a4d2 841713f c1d31db 841713f 453a4d2 e3af942 453a4d2 e3af942 453a4d2 e3af942 453a4d2 e3af942 453a4d2 e3af942 453a4d2 e3af942 453a4d2 e3af942 453a4d2 c1d31db 453a4d2 c1d31db 453a4d2 c1d31db 453a4d2 c1d31db 453a4d2 c1d31db 453a4d2 c1d31db cbb270b c1d31db e5c3d7b c1d31db e5c3d7b c1d31db cbb270b c1d31db 453a4d2 c1d31db 453a4d2 c1d31db 453a4d2 c1d31db cbb270b c1d31db cbb270b 111aff8 7472291 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
"""
This module handles session state initialization and management.
"""
import streamlit as st
import uuid
from database import get_db_client
def initialize_session_state():
"""
Initialize all session state variables needed for the application.
"""
# Database client
if 'db_client' not in st.session_state:
try:
st.session_state.db_client = get_db_client()
except Exception as e:
st.error(f"Failed to initialize database: {str(e)}")
st.session_state.db_client = None
# Unique consultation ID for tracking sessions
if 'consultation_id' not in st.session_state:
st.session_state.consultation_id = str(uuid.uuid4())[:8]
# Create new conversation in database
if st.session_state.db_client:
try:
st.session_state.db_client.create_conversation(st.session_state.consultation_id)
except Exception as e:
st.error(f"Failed to create conversation in database: {str(e)}")
# Main conversation history (now acts as a cache)
if 'history' not in st.session_state:
st.session_state.history = []
# Try to load from database if consultation_id exists
if hasattr(st.session_state, 'consultation_id') and st.session_state.db_client:
try:
db_history = st.session_state.db_client.get_conversation_history(st.session_state.consultation_id)
st.session_state.history = db_history
except Exception as e:
# Silently continue with empty history if database fetch fails
pass
# RAG feature toggle
if 'use_rag' not in st.session_state:
st.session_state.use_rag = True
# Processing state for showing typing indicator
if 'processing' not in st.session_state:
st.session_state.processing = False
# Report generation state
if 'show_report_form' not in st.session_state:
st.session_state.show_report_form = False
if 'report_step' not in st.session_state:
st.session_state.report_step = 0
# Patient information for reports
if 'patient_info' not in st.session_state:
st.session_state.patient_info = {"name": "", "age": "", "gender": ""}
# PDF report data
if 'pdf_data' not in st.session_state:
st.session_state.pdf_data = None
# Email form visibility
if 'show_email_form' not in st.session_state:
st.session_state.show_email_form = False
def add_message_to_history(message):
"""
Add a message to the conversation history and persist it to database.
Args:
message (dict): The message to add to history
"""
# Add to local history
st.session_state.history.append(message)
# Persist to database if available
if hasattr(st.session_state, 'db_client') and st.session_state.db_client:
try:
st.session_state.db_client.save_message(
st.session_state.consultation_id,
message
)
except Exception as e:
st.error(f"Failed to save message to database: {str(e)}")
def get_full_history():
"""
Get the complete conversation history from the database.
Returns:
list: Full conversation history
"""
if not hasattr(st.session_state, 'consultation_id'):
print("No consultation_id in session state")
return []
if not hasattr(st.session_state, 'db_client') or not st.session_state.db_client:
print("No database client available")
return st.session_state.history if hasattr(st.session_state, 'history') else []
try:
db_history = st.session_state.db_client.get_conversation_history(
st.session_state.consultation_id
)
# Update the local cache with the database results
st.session_state.history = db_history
# For debugging
print(f"Retrieved {len(db_history)} messages from database for consultation {st.session_state.consultation_id}")
return db_history
except Exception as e:
print(f"Failed to retrieve history from database: {str(e)}")
# Fallback to session state history if database retrieval fails
return st.session_state.history if hasattr(st.session_state, 'history') else []
def end_conversation():
"""
End the current conversation and clean up resources.
"""
if hasattr(st.session_state, 'db_client') and st.session_state.db_client:
try:
st.session_state.db_client.delete_conversation(
st.session_state.consultation_id
)
except Exception as e:
st.error(f"Failed to delete conversation from database: {str(e)}")
# Reset session state
st.session_state.history = []
st.session_state.consultation_id = str(uuid.uuid4())[:8]
# Create new conversation in database
if hasattr(st.session_state, 'db_client') and st.session_state.db_client:
try:
st.session_state.db_client.create_conversation(st.session_state.consultation_id)
except Exception as e:
st.error(f"Failed to create new conversation in database: {str(e)}")
|