use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
path::PathBuf,
time::{Duration, SystemTime},
};
use crate::{analytics::ConversationAnalytics, ChatConfig, Message, SessionMetrics};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PersistentChatSession {
pub session_id: String,
pub config: ChatConfig,
pub messages: Vec<Message>,
pub created_at: SystemTime,
pub last_accessed: SystemTime,
pub metrics: SessionMetrics,
pub user_preferences: HashMap<String, serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PersistenceConfig {
pub enabled: bool,
pub storage_path: PathBuf,
pub backup_path: PathBuf,
pub auto_save_interval: Duration,
pub session_ttl: Duration,
pub max_sessions: usize,
pub compression_enabled: bool,
pub encryption_enabled: bool,
pub encryption_key: Option<String>,
pub backup_retention_days: usize,
pub checkpoint_interval: Duration,
}
impl Default for PersistenceConfig {
fn default() -> Self {
Self {
enabled: true,
storage_path: PathBuf::from("data/sessions"),
backup_path: PathBuf::from("data/backups"),
auto_save_interval: Duration::from_secs(60), session_ttl: Duration::from_secs(86400 * 7), max_sessions: 10000,
compression_enabled: true,
encryption_enabled: false, encryption_key: None, backup_retention_days: 30,
checkpoint_interval: Duration::from_secs(300), }
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PersistedSession {
pub session_id: String,
pub config: ChatConfig,
pub messages: Vec<Message>,
pub created_at: SystemTime,
pub last_accessed: SystemTime,
pub metrics: SessionMetrics,
pub analytics: Option<ConversationAnalytics>,
pub user_preferences: HashMap<String, serde_json::Value>,
pub conversation_state: ConversationState,
pub checksum: String,
}
#[derive(Debug, Clone)]
pub struct SessionWithDirtyFlag {
pub session: PersistedSession,
pub dirty: bool,
pub last_saved: Option<SystemTime>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ConversationState {
pub current_topic: Option<String>,
pub context_window: Vec<String>,
pub entity_history: Vec<EntityReference>,
pub query_history: Vec<QueryContext>,
pub user_intent_history: Vec<String>,
pub conversation_flow: ConversationFlow,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EntityReference {
pub entity_uri: String,
pub entity_label: String,
pub first_mentioned: SystemTime,
pub mention_count: usize,
pub last_context: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryContext {
pub sparql_query: String,
pub natural_language: String,
pub intent: String,
pub success: bool,
pub timestamp: SystemTime,
pub execution_time_ms: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConversationFlow {
pub current_phase: ConversationPhase,
pub topic_transitions: Vec<TopicTransition>,
pub interaction_patterns: Vec<InteractionPattern>,
pub complexity_level: f32,
}
impl Default for ConversationFlow {
fn default() -> Self {
Self {
current_phase: ConversationPhase::Introduction,
topic_transitions: Vec::new(),
interaction_patterns: Vec::new(),
complexity_level: 1.0,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ConversationPhase {
Introduction,
Exploration,
DeepDive,
Analysis,
Conclusion,
Abandoned,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TopicTransition {
pub from_topic: Option<String>,
pub to_topic: String,
pub transition_type: TransitionType,
pub timestamp: SystemTime,
pub confidence: f32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TransitionType {
Natural,
UserInitiated,
Clarification,
Digression,
Return,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InteractionPattern {
pub pattern_type: InteractionType,
pub frequency: usize,
pub last_occurrence: SystemTime,
pub effectiveness: f32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum InteractionType {
QuestionAnswer,
ExploratorySearch,
ComparativeAnalysis,
DeepInvestigation,
QuickLookup,
IterativeRefinement,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RecoveryInfo {
pub session_id: String,
pub last_checkpoint: SystemTime,
pub backup_available: bool,
pub corruption_detected: bool,
pub recovery_strategies: Vec<RecoveryStrategy>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RecoveryStrategy {
LoadFromCheckpoint,
LoadFromBackup,
PartialRecovery,
CreateNew,
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct PersistenceStats {
pub total_sessions_saved: usize,
pub total_sessions_loaded: usize,
pub save_failures: usize,
pub load_failures: usize,
pub recovery_operations: usize,
pub corrupted_sessions: usize,
pub total_backup_size: u64,
pub average_save_time_ms: f64,
pub average_load_time_ms: f64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionInfo {
pub session_id: String,
pub created_at: SystemTime,
pub modified_at: SystemTime,
pub size_bytes: u64,
pub has_backup: bool,
}