ricecoder_sessions/
models.rs

1//! Core data models for sessions, messages, and background agents
2
3use chrono::{DateTime, Duration, Utc};
4use serde::{Deserialize, Serialize};
5use std::collections::HashMap;
6use uuid::Uuid;
7
8/// Represents a session with its context, history, and metadata
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct Session {
11    /// Unique identifier for the session
12    pub id: String,
13    /// Human-readable name for the session
14    pub name: String,
15    /// When the session was created
16    pub created_at: DateTime<Utc>,
17    /// When the session was last updated
18    pub updated_at: DateTime<Utc>,
19    /// Current status of the session
20    pub status: SessionStatus,
21    /// Session context (project, provider, model, etc.)
22    pub context: SessionContext,
23    /// Conversation history
24    pub history: Vec<Message>,
25    /// Background agents running in this session
26    pub background_agents: Vec<BackgroundAgent>,
27}
28
29impl Session {
30    /// Create a new session with default values
31    pub fn new(name: String, context: SessionContext) -> Self {
32        let now = Utc::now();
33        Self {
34            id: Uuid::new_v4().to_string(),
35            name,
36            created_at: now,
37            updated_at: now,
38            status: SessionStatus::Active,
39            context,
40            history: Vec::new(),
41            background_agents: Vec::new(),
42        }
43    }
44}
45
46/// Status of a session
47#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
48pub enum SessionStatus {
49    /// Session is currently active
50    Active,
51    /// Session is paused
52    Paused,
53    /// Session is archived
54    Archived,
55}
56
57/// Context for a session (project, provider, model, etc.)
58#[derive(Debug, Clone, Serialize, Deserialize)]
59pub struct SessionContext {
60    /// Path to the project directory
61    pub project_path: Option<String>,
62    /// AI provider (e.g., "openai", "anthropic")
63    pub provider: String,
64    /// Model name (e.g., "gpt-4", "claude-3")
65    pub model: String,
66    /// Session mode (Chat, Code, Vibe)
67    pub mode: SessionMode,
68    /// Files included in the session context
69    pub files: Vec<String>,
70    /// Custom context data
71    pub custom: HashMap<String, serde_json::Value>,
72}
73
74impl SessionContext {
75    /// Create a new session context
76    pub fn new(provider: String, model: String, mode: SessionMode) -> Self {
77        Self {
78            project_path: None,
79            provider,
80            model,
81            mode,
82            files: Vec::new(),
83            custom: HashMap::new(),
84        }
85    }
86}
87
88/// Session mode
89#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
90pub enum SessionMode {
91    /// Chat mode for conversations
92    Chat,
93    /// Code mode for code generation and analysis
94    Code,
95    /// Vibe mode for creative tasks
96    Vibe,
97}
98
99/// A message in the conversation history
100#[derive(Debug, Clone, Serialize, Deserialize)]
101pub struct Message {
102    /// Unique identifier for the message
103    pub id: String,
104    /// Role of the message sender
105    pub role: MessageRole,
106    /// Message content
107    pub content: String,
108    /// When the message was created
109    pub timestamp: DateTime<Utc>,
110    /// Additional metadata about the message
111    pub metadata: MessageMetadata,
112}
113
114impl Message {
115    /// Create a new message
116    pub fn new(role: MessageRole, content: String) -> Self {
117        Self {
118            id: Uuid::new_v4().to_string(),
119            role,
120            content,
121            timestamp: Utc::now(),
122            metadata: MessageMetadata::default(),
123        }
124    }
125}
126
127/// Role of a message sender
128#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
129pub enum MessageRole {
130    /// User message
131    User,
132    /// Assistant message
133    Assistant,
134    /// System message
135    System,
136}
137
138/// Metadata about a message
139#[derive(Debug, Clone, Default, Serialize, Deserialize)]
140pub struct MessageMetadata {
141    /// Number of tokens in the message
142    pub tokens: Option<usize>,
143    /// Model used to generate the message
144    pub model: Option<String>,
145    /// Duration of message generation
146    pub duration: Option<Duration>,
147}
148
149/// A background agent running in a session
150#[derive(Debug, Clone, Serialize, Deserialize)]
151pub struct BackgroundAgent {
152    /// Unique identifier for the agent
153    pub id: String,
154    /// Type of agent (e.g., "code_review", "diff_analysis")
155    pub agent_type: String,
156    /// Current status of the agent
157    pub status: AgentStatus,
158    /// Task being executed
159    pub task: Option<String>,
160    /// When the agent was started
161    pub started_at: DateTime<Utc>,
162    /// When the agent completed (if finished)
163    pub completed_at: Option<DateTime<Utc>>,
164}
165
166impl BackgroundAgent {
167    /// Create a new background agent
168    pub fn new(agent_type: String, task: Option<String>) -> Self {
169        Self {
170            id: Uuid::new_v4().to_string(),
171            agent_type,
172            status: AgentStatus::Running,
173            task,
174            started_at: Utc::now(),
175            completed_at: None,
176        }
177    }
178}
179
180/// Status of a background agent
181#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
182pub enum AgentStatus {
183    /// Agent is currently running
184    Running,
185    /// Agent has completed successfully
186    Completed,
187    /// Agent failed
188    Failed,
189    /// Agent was cancelled
190    Cancelled,
191}