infinity_daemon/
session_store.rs1use infinity_agent_core::ThreadId;
2use std::{collections::HashMap, path::PathBuf};
3
4use serde::{Deserialize, Serialize};
5use tokio::sync::mpsc;
6
7#[derive(Serialize, Deserialize, Clone, Debug)]
8pub struct SessionEntry {
9 pub cwd: PathBuf,
10 #[serde(default)]
12 pub shut_down: bool,
13 #[serde(default)]
15 pub idle: bool,
16 #[serde(default)]
18 pub archived: bool,
19}
20
21impl SessionEntry {
22 pub fn status(&self, has_pending_choices: bool) -> infinity_protocol::SessionStatus {
23 if self.archived {
24 infinity_protocol::SessionStatus::Archived
25 } else if has_pending_choices {
26 infinity_protocol::SessionStatus::WaitingForChoice
27 } else if self.idle {
28 infinity_protocol::SessionStatus::Idle
29 } else if self.shut_down {
30 infinity_protocol::SessionStatus::Stopped
31 } else {
32 infinity_protocol::SessionStatus::Running
33 }
34 }
35}
36
37#[derive(Serialize, Deserialize)]
38pub struct SessionStore {
39 pub sessions: HashMap<ThreadId, SessionEntry>,
40 #[serde(skip)]
41 path: String,
42 #[serde(skip)]
43 change_tx: Option<mpsc::UnboundedSender<ThreadId>>,
44}
45
46impl SessionStore {
47 pub fn load(path: &str, change_tx: mpsc::UnboundedSender<ThreadId>) -> Self {
48 let sessions = std::fs::read_to_string(path)
49 .ok()
50 .and_then(|s| {
51 if let Ok(store) = serde_json::from_str::<Self>(&s) {
53 return Some(store.sessions);
54 }
55 #[derive(Deserialize)]
57 struct LegacyEntry {
58 thread_id: ThreadId,
59 }
60 #[derive(Deserialize)]
61 struct LegacyStore {
62 sessions: Vec<LegacyEntry>,
63 }
64 if let Ok(legacy) = serde_json::from_str::<LegacyStore>(&s) {
65 let map = legacy
66 .sessions
67 .into_iter()
68 .map(|e| {
69 (
70 e.thread_id,
71 SessionEntry {
72 cwd: std::env::current_dir()
73 .expect("failed to get current directory"),
74 shut_down: false,
75 idle: false,
76 archived: false,
77 },
78 )
79 })
80 .collect();
81 return Some(map);
82 }
83 None
84 })
85 .unwrap_or_default();
86
87 Self {
88 sessions,
89 path: path.to_owned(),
90 change_tx: Some(change_tx),
91 }
92 }
93
94 pub fn save(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
95 let json = serde_json::to_string_pretty(self)?;
96 std::fs::write(&self.path, json)?;
97 Ok(())
98 }
99
100 pub fn notify(&self, session_id: &ThreadId<str>) {
101 if let Some(ref tx) = self.change_tx {
102 let _ = tx.send(session_id.to_owned());
103 }
104 }
105
106 pub fn create(&mut self, session_id: &ThreadId<str>, cwd: PathBuf) {
107 self.sessions.insert(
108 session_id.to_owned(),
109 SessionEntry {
110 cwd,
111 shut_down: false,
112 idle: false,
113 archived: false,
114 },
115 );
116 self.notify(session_id);
117 }
118
119 pub fn get_cwd(&self, session_id: &ThreadId<str>) -> &PathBuf {
120 &self
121 .sessions
122 .get(session_id)
123 .expect("bug: session not found in store")
124 .cwd
125 }
126
127 pub fn mark_shut_down(&mut self, session_id: &ThreadId<str>) {
128 if let Some(entry) = self.sessions.get_mut(session_id) {
129 entry.shut_down = true;
130 self.notify(session_id);
131 }
132 }
133
134 pub fn clear_shut_down(&mut self, session_id: &ThreadId<str>) -> bool {
135 tracing::trace!("Clearing shut down status for {}", session_id);
136 if let Some(entry) = self.sessions.get_mut(session_id)
137 && entry.shut_down
138 {
139 entry.shut_down = false;
140 self.notify(session_id);
141 return true;
142 }
143 false
144 }
145
146 pub fn mark_idle(&mut self, session_id: &ThreadId<str>) {
147 if let Some(entry) = self.sessions.get_mut(session_id)
148 && !entry.idle
149 {
150 entry.idle = true;
151 self.notify(session_id);
152 }
153 }
154
155 pub fn clear_idle(&mut self, session_id: &ThreadId<str>) -> bool {
156 if let Some(entry) = self.sessions.get_mut(session_id)
157 && entry.idle
158 {
159 entry.idle = false;
160 self.notify(session_id);
161 return true;
162 }
163 false
164 }
165
166 pub fn is_idle(&self, session_id: &ThreadId<str>) -> bool {
167 self.sessions
168 .get(session_id)
169 .map(|e| e.idle)
170 .unwrap_or(false)
171 }
172
173 pub fn is_shut_down(&self, session_id: &ThreadId<str>) -> bool {
174 self.sessions
175 .get(session_id)
176 .map(|e| e.shut_down)
177 .unwrap_or(false)
178 }
179
180 pub fn mark_archived(&mut self, session_id: &ThreadId<str>) {
181 if let Some(entry) = self.sessions.get_mut(session_id) {
182 entry.archived = true;
183 self.notify(session_id);
184 }
185 }
186}