Spaces:
Sleeping
Sleeping
File size: 3,415 Bytes
965274a 288e434 965274a 288e434 965274a |
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 |
import uuid
import json
import os
from datetime import datetime
from config import SESSION_MEMORY_LIMIT
class SessionManager:
def __init__(self):
self.session_id = None
self.conversation_history = []
self.last_response_audio = None
self.sessions_dir = "sessions"
# Create sessions directory if it doesn't exist
if not os.path.exists(self.sessions_dir):
os.makedirs(self.sessions_dir)
def start_session(self, session_id=None):
"""Start a new session or resume an existing one"""
if session_id:
self.session_id = session_id
self._load_session(session_id)
else:
self.session_id = str(uuid.uuid4())
self.conversation_history = []
print(f"Session active with ID: {self.session_id}")
return self.session_id
def add_interaction(self, user_text, nova_text):
"""Add a conversation turn to the history"""
# Make sure we have valid content, especially for automated greetings
if not user_text or user_text.strip() == "":
user_text = "..." # Placeholder for empty user text (e.g., when Nova speaks first)
interaction = {
"timestamp": datetime.now().isoformat(),
"user": user_text,
"nova": nova_text
}
# Print to console for visibility
print(f"\nUser: {user_text}")
print(f"Nova: {nova_text}\n")
self.conversation_history.append(interaction)
# Keep conversation history within the limit
if len(self.conversation_history) > SESSION_MEMORY_LIMIT:
self.conversation_history.pop(0)
# Save the session after each interaction
self._save_session()
def get_conversation_context(self):
"""Return formatted conversation history for context"""
if not self.conversation_history:
return ""
context = ""
for interaction in self.conversation_history:
context += f"User: {interaction['user']}\nNova: {interaction['nova']}\n"
return context
def set_last_response(self, audio_data):
"""Store the last audio response for replay"""
self.last_response_audio = audio_data
def get_last_response(self):
"""Get the last audio response for replay"""
return self.last_response_audio
def _save_session(self):
"""Save the session to disk"""
session_file = os.path.join(self.sessions_dir, f"{self.session_id}.json")
with open(session_file, 'w') as f:
json.dump({
"session_id": self.session_id,
"conversation_history": self.conversation_history
}, f)
def _load_session(self, session_id):
"""Load a session from disk"""
session_file = os.path.join(self.sessions_dir, f"{session_id}.json")
if os.path.exists(session_file):
with open(session_file, 'r') as f:
data = json.load(f)
self.conversation_history = data.get("conversation_history", [])
print(f"Loaded existing session with {len(self.conversation_history)} interactions")
else:
self.conversation_history = []
print("Creating new session history")
|